4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_OSD
46 #include <linux/module.h>
47 #include <linux/user_namespace.h>
48 #ifdef HAVE_UIDGID_HEADER
49 # include <linux/uidgid.h>
52 /* LUSTRE_VERSION_CODE */
53 #include <lustre_ver.h>
54 /* prerequisite for linux/xattr.h */
55 #include <linux/types.h>
56 /* prerequisite for linux/xattr.h */
58 /* XATTR_{REPLACE,CREATE} */
59 #include <linux/xattr.h>
61 #include <ldiskfs/ldiskfs.h>
62 #include <ldiskfs/xattr.h>
63 #include <ldiskfs/ldiskfs_extents.h>
66 * struct OBD_{ALLOC,FREE}*()
69 #include <obd_support.h>
70 /* struct ptlrpc_thread */
71 #include <lustre_net.h>
72 #include <lustre_fid.h>
74 #include <lustre_param.h>
76 #include "osd_internal.h"
77 #include "osd_dynlocks.h"
79 /* llo_* api support */
80 #include <md_object.h>
81 #include <lustre_quota.h>
83 #include <lustre_linkea.h>
86 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
87 "ldiskfs with parallel directory operations");
89 int ldiskfs_track_declares_assert;
90 CFS_MODULE_PARM(ldiskfs_track_declares_assert, "i", int, 0644,
91 "LBUG during tracking of declares");
93 /* Slab to allocate dynlocks */
94 struct kmem_cache *dynlock_cachep;
96 /* Slab to allocate osd_it_ea */
97 struct kmem_cache *osd_itea_cachep;
99 static struct lu_kmem_descr ldiskfs_caches[] = {
101 .ckd_cache = &dynlock_cachep,
102 .ckd_name = "dynlock_cache",
103 .ckd_size = sizeof(struct dynlock_handle)
106 .ckd_cache = &osd_itea_cachep,
107 .ckd_name = "osd_itea_cache",
108 .ckd_size = sizeof(struct osd_it_ea)
115 static const char dot[] = ".";
116 static const char dotdot[] = "..";
117 static const char remote_obj_dir[] = "REM_OBJ_DIR";
119 static const struct lu_object_operations osd_lu_obj_ops;
120 static const struct dt_object_operations osd_obj_ops;
121 static const struct dt_object_operations osd_obj_ea_ops;
122 static const struct dt_object_operations osd_obj_otable_it_ops;
123 static const struct dt_index_operations osd_index_iam_ops;
124 static const struct dt_index_operations osd_index_ea_ops;
126 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
127 const struct lu_fid *fid);
128 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
129 struct osd_device *osd);
131 int osd_trans_declare_op2rb[] = {
132 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
133 [OSD_OT_PUNCH] = OSD_OT_MAX,
134 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
135 [OSD_OT_CREATE] = OSD_OT_DESTROY,
136 [OSD_OT_DESTROY] = OSD_OT_CREATE,
137 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
138 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
139 [OSD_OT_WRITE] = OSD_OT_WRITE,
140 [OSD_OT_INSERT] = OSD_OT_DELETE,
141 [OSD_OT_DELETE] = OSD_OT_INSERT,
142 [OSD_OT_QUOTA] = OSD_OT_MAX,
145 static int osd_has_index(const struct osd_object *obj)
147 return obj->oo_dt.do_index_ops != NULL;
150 static int osd_object_invariant(const struct lu_object *l)
152 return osd_invariant(osd_obj(l));
156 * Concurrency: doesn't matter
160 * Concurrency: doesn't matter
162 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
164 struct osd_thread_info *oti = osd_oti_get(env);
165 return oti->oti_w_locks > 0 && o->oo_owner == env;
169 * Concurrency: doesn't access mutable data
171 static int osd_root_get(const struct lu_env *env,
172 struct dt_device *dev, struct lu_fid *f)
174 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
179 * the following set of functions are used to maintain per-thread
180 * cache of FID->ino mapping. this mechanism is needed to resolve
181 * FID to inode at dt_insert() which in turn stores ino in the
182 * directory entries to keep ldiskfs compatible with ext[34].
183 * due to locking-originated restrictions we can't lookup ino
184 * using LU cache (deadlock is possible). lookup using OI is quite
185 * expensive. so instead we maintain this cache and methods like
186 * dt_create() fill it. so in the majority of cases dt_insert() is
187 * able to find needed mapping in lockless manner.
189 static struct osd_idmap_cache *
190 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
191 const struct lu_fid *fid)
193 struct osd_thread_info *oti = osd_oti_get(env);
194 struct osd_idmap_cache *idc = oti->oti_ins_cache;
196 for (i = 0; i < oti->oti_ins_cache_used; i++) {
197 if (!lu_fid_eq(&idc[i].oic_fid, fid))
199 if (idc[i].oic_dev != osd)
208 static struct osd_idmap_cache *
209 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
210 const struct lu_fid *fid)
212 struct osd_thread_info *oti = osd_oti_get(env);
213 struct osd_idmap_cache *idc;
216 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
217 i = oti->oti_ins_cache_size * 2;
219 i = OSD_INS_CACHE_SIZE;
220 OBD_ALLOC(idc, sizeof(*idc) * i);
222 return ERR_PTR(-ENOMEM);
223 if (oti->oti_ins_cache != NULL) {
224 memcpy(idc, oti->oti_ins_cache,
225 oti->oti_ins_cache_used * sizeof(*idc));
226 OBD_FREE(oti->oti_ins_cache,
227 oti->oti_ins_cache_used * sizeof(*idc));
229 oti->oti_ins_cache = idc;
230 oti->oti_ins_cache_size = i;
233 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
236 idc->oic_lid.oii_ino = 0;
237 idc->oic_lid.oii_gen = 0;
244 * lookup mapping for the given fid in the cache, initialize a
245 * new one if not found. the initialization checks whether the
246 * object is local or remote. for local objects, OI is used to
247 * learn ino/generation. the function is used when the caller
248 * has no information about the object, e.g. at dt_insert().
250 static struct osd_idmap_cache *
251 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
252 const struct lu_fid *fid)
254 struct osd_idmap_cache *idc;
257 idc = osd_idc_find(env, osd, fid);
258 LASSERT(!IS_ERR(idc));
262 /* new mapping is needed */
263 idc = osd_idc_add(env, osd, fid);
268 rc = osd_remote_fid(env, osd, fid);
269 if (unlikely(rc < 0))
273 /* the object is local, lookup in OI */
274 /* XXX: probably cheaper to lookup in LU first? */
275 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
277 if (unlikely(rc < 0)) {
278 CERROR("can't lookup: rc = %d\n", rc);
282 /* the object is remote */
290 * lookup mapping for given FID and fill it from the given object.
291 * the object is lolcal by definition.
293 static int osd_idc_find_and_init(const struct lu_env *env,
294 struct osd_device *osd,
295 struct osd_object *obj)
297 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
298 struct osd_idmap_cache *idc;
300 idc = osd_idc_find(env, osd, fid);
301 LASSERT(!IS_ERR(idc));
303 if (obj->oo_inode == NULL)
305 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
306 LASSERT(idc->oic_lid.oii_ino == 0);
307 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
308 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
313 /* new mapping is needed */
314 idc = osd_idc_add(env, osd, fid);
318 if (obj->oo_inode != NULL) {
319 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
320 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
326 * OSD object methods.
330 * Concurrency: no concurrent access is possible that early in object
333 static struct lu_object *osd_object_alloc(const struct lu_env *env,
334 const struct lu_object_header *hdr,
337 struct osd_object *mo;
343 l = &mo->oo_dt.do_lu;
344 dt_object_init(&mo->oo_dt, NULL, d);
345 mo->oo_dt.do_ops = &osd_obj_ea_ops;
346 l->lo_ops = &osd_lu_obj_ops;
347 init_rwsem(&mo->oo_sem);
348 init_rwsem(&mo->oo_ext_idx_sem);
349 spin_lock_init(&mo->oo_guard);
356 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
357 struct dentry *dentry, struct lustre_mdt_attrs *lma)
361 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
362 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
363 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
365 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
366 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
368 lustre_lma_swab(lma);
369 /* Check LMA compatibility */
370 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
371 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
372 "for fid = "DFID", ino = %lu\n",
373 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
374 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
375 PFID(&lma->lma_self_fid), inode->i_ino);
378 } else if (rc == 0) {
386 * retrieve object from backend ext fs.
388 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
389 struct osd_inode_id *id)
391 struct inode *inode = NULL;
393 /* if we look for an inode withing a running
394 * transaction, then we risk to deadlock */
395 /* osd_dirent_check_repair() breaks this */
396 /*LASSERT(current->journal_info == NULL);*/
398 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
400 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
401 id->oii_ino, PTR_ERR(inode));
402 } else if (id->oii_gen != OSD_OII_NOGEN &&
403 inode->i_generation != id->oii_gen) {
404 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
405 "i_generation = %u\n",
406 id->oii_ino, id->oii_gen, inode->i_generation);
408 inode = ERR_PTR(-ESTALE);
409 } else if (inode->i_nlink == 0) {
410 /* due to parallel readdir and unlink,
411 * we can have dead inode here. */
412 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
414 inode = ERR_PTR(-ESTALE);
415 } else if (is_bad_inode(inode)) {
416 CWARN("%.16s: bad inode: ino = %u\n",
417 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
419 inode = ERR_PTR(-ENOENT);
421 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
422 if (id->oii_gen == OSD_OII_NOGEN)
423 osd_id_gen(id, inode->i_ino, inode->i_generation);
425 /* Do not update file c/mtime in ldiskfs.
426 * NB: we don't have any lock to protect this because we don't
427 * have reference on osd_object now, but contention with
428 * another lookup + attr_set can't happen in the tiny window
429 * between if (...) and set S_NOCMTIME. */
430 if (!(inode->i_flags & S_NOCMTIME))
431 inode->i_flags |= S_NOCMTIME;
436 int osd_ldiskfs_add_entry(struct osd_thread_info *info,
437 handle_t *handle, struct dentry *child,
438 struct inode *inode, struct htree_lock *hlock)
442 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
443 if (rc == -ENOBUFS || rc == -ENOSPC) {
444 char fidbuf[FID_LEN + 1];
445 struct lustre_mdt_attrs lma;
446 struct lu_fid fid = { };
448 struct dentry *p_dentry = child->d_parent;
450 rc2 = osd_get_lma(info, p_dentry->d_inode, p_dentry,
453 fid = lma.lma_self_fid;
454 snprintf(fidbuf, sizeof(fidbuf), DFID, PFID(&fid));
455 } else if (rc2 == -ENODATA) {
456 if (unlikely(p_dentry->d_inode ==
457 inode->i_sb->s_root->d_inode))
458 lu_local_obj_fid(&fid, OSD_FS_ROOT_OID);
459 else if (info->oti_dev && !info->oti_dev->od_is_ost &&
460 fid_seq_is_mdt0(fid_seq(&fid)))
461 lu_igif_build(&fid, p_dentry->d_inode->i_ino,
462 p_dentry->d_inode->i_generation);
463 snprintf(fidbuf, sizeof(fidbuf), DFID, PFID(&fid));
465 snprintf(fidbuf, FID_LEN, "%s", "unknown");
469 errstr = "has reached";
471 errstr = "is approaching";
472 CWARN("%.16s: directory (inode: %lu FID: %s) %s maximum entry limit\n",
473 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
474 p_dentry->d_inode->i_ino, fidbuf, errstr);
475 /* ignore such error now */
483 static struct inode *
484 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
485 struct osd_inode_id *id, struct lu_fid *fid)
487 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
491 inode = osd_iget(info, dev, id);
495 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
497 *fid = lma->lma_self_fid;
498 } else if (rc == -ENODATA) {
499 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
500 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
502 lu_igif_build(fid, inode->i_ino, inode->i_generation);
510 static struct inode *osd_iget_check(struct osd_thread_info *info,
511 struct osd_device *dev,
512 const struct lu_fid *fid,
513 struct osd_inode_id *id,
520 /* The cached OI mapping is trustable. If we cannot locate the inode
521 * via the cached OI mapping, then return the failure to the caller
522 * directly without further OI checking. */
524 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
527 if (cached || (rc != -ENOENT && rc != -ESTALE)) {
528 CDEBUG(D_INODE, "no inode: ino = %u, rc = %d\n",
537 if (is_bad_inode(inode)) {
540 CDEBUG(D_INODE, "bad inode: ino = %u\n", id->oii_ino);
548 if (id->oii_gen != OSD_OII_NOGEN &&
549 inode->i_generation != id->oii_gen) {
552 CDEBUG(D_INODE, "unmatched inode: ino = %u, "
553 "oii_gen = %u, i_generation = %u\n",
554 id->oii_ino, id->oii_gen, inode->i_generation);
562 if (inode->i_nlink == 0) {
565 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
573 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
577 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
579 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
580 /* XXX: There are four possible cases:
582 * Backup/restore caused the OI invalid.
584 * Someone unlinked the object but NOT removed
585 * the OI mapping, such as mount target device
586 * as ldiskfs, and modify something directly.
588 * Someone just removed the object between the
589 * former oi_lookup and the iget. It is normal.
590 * 4. Other failure cases.
592 * Generally, when the device is mounted, it will
593 * auto check whether the system is restored from
594 * file-level backup or not. We trust such detect
595 * to distinguish the 1st case from the 2nd case. */
597 if (!IS_ERR(inode) && inode->i_generation != 0 &&
598 inode->i_generation == id->oii_gen)
599 /* "id->oii_gen != OSD_OII_NOGEN" is for
600 * "@cached == false" case. */
605 /* If the OI mapping was in OI file before the
606 * osd_iget_check(), but now, it is disappear,
607 * then it must be removed by race. That is a
608 * normal race case. */
611 if (id->oii_gen == OSD_OII_NOGEN)
612 osd_id_gen(id, inode->i_ino, inode->i_generation);
614 /* Do not update file c/mtime in ldiskfs.
615 * NB: we don't have any lock to protect this because we don't
616 * have reference on osd_object now, but contention with
617 * another lookup + attr_set can't happen in the tiny window
618 * between if (...) and set S_NOCMTIME. */
619 if (!(inode->i_flags & S_NOCMTIME))
620 inode->i_flags |= S_NOCMTIME;
637 * \retval +v: new filter_fid, does not contain self-fid
638 * \retval 0: filter_fid_old, contains self-fid
639 * \retval -v: other failure cases
641 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
642 struct dentry *dentry, struct lu_fid *fid)
644 struct filter_fid_old *ff = &info->oti_ff;
645 struct ost_id *ostid = &info->oti_ostid;
648 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
649 if (rc == sizeof(*ff)) {
651 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
652 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
653 /* XXX: use 0 as the index for compatibility, the caller will
654 * handle index related issues when necessarry. */
655 ostid_to_fid(fid, ostid, 0);
656 } else if (rc == sizeof(struct filter_fid)) {
658 } else if (rc >= 0) {
665 static int osd_lma_self_repair(struct osd_thread_info *info,
666 struct osd_device *osd, struct inode *inode,
667 const struct lu_fid *fid, __u32 compat)
672 LASSERT(current->journal_info == NULL);
674 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
675 osd_dto_credits_noquota[DTO_XATTR_SET]);
678 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
683 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
685 CWARN("%s: cannot self repair the LMA: rc = %d\n",
687 ldiskfs_journal_stop(jh);
691 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
693 struct osd_thread_info *info = osd_oti_get(env);
694 struct osd_device *osd = osd_obj2dev(obj);
695 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
696 struct inode *inode = obj->oo_inode;
697 struct dentry *dentry = &info->oti_obj_dentry;
698 struct lu_fid *fid = NULL;
699 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
703 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
704 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
705 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
706 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
707 fid = &lma->lma_self_fid;
708 rc = osd_get_idif(info, inode, dentry, fid);
709 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
710 /* For the given OST-object, if it has neither LMA nor
711 * FID in XATTR_NAME_FID, then the given FID (which is
712 * contained in the @obj, from client RPC for locating
713 * the OST-object) is trusted. We use it to generate
715 osd_lma_self_repair(info, osd, inode, rfid,
726 lustre_lma_swab(lma);
727 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
728 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
729 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
730 "fid = "DFID", ino = %lu\n", osd_name(osd),
731 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
732 PFID(rfid), inode->i_ino);
735 fid = &lma->lma_self_fid;
739 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
740 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
741 struct ost_id *oi = &info->oti_ostid;
742 struct lu_fid *fid1 = &info->oti_fid3;
743 __u32 idx = fid_idif_ost_idx(rfid);
745 /* For old IDIF, the OST index is not part of the IDIF,
746 * Means that different OSTs may have the same IDIFs.
747 * Under such case, we need to make some compatible
748 * check to make sure to trigger OI scrub properly. */
749 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
750 /* Given @rfid is new, LMA is old. */
751 fid_to_ostid(fid, oi);
752 ostid_to_fid(fid1, oi, idx);
753 if (lu_fid_eq(fid1, rfid)) {
754 if (osd->od_index_in_idif)
755 osd_lma_self_repair(info, osd,
769 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
770 const struct lu_fid *fid,
771 const struct lu_object_conf *conf)
773 struct osd_thread_info *info;
774 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
775 struct osd_device *dev;
776 struct osd_idmap_cache *oic;
777 struct osd_inode_id *id;
779 struct osd_scrub *scrub;
780 struct scrub_file *sf;
784 bool triggered = false;
787 LINVRNT(osd_invariant(obj));
788 LASSERT(obj->oo_inode == NULL);
789 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
792 scrub = &dev->od_scrub;
793 sf = &scrub->os_file;
794 info = osd_oti_get(env);
796 oic = &info->oti_cache;
798 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
801 /* For the object is created as locking anchor, or for the object to
802 * be created on disk. No need to osd_oi_lookup() at here because FID
803 * shouldn't never be re-used, if it's really a duplicate FID from
804 * unexpected reason, we should be able to detect it later by calling
805 * do_create->osd_oi_insert(). */
806 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
807 GOTO(out, result = 0);
809 /* Search order: 1. per-thread cache. */
810 if (lu_fid_eq(fid, &oic->oic_fid) &&
811 likely(oic->oic_dev == dev)) {
817 if (!list_empty(&scrub->os_inconsistent_items)) {
818 /* Search order: 2. OI scrub pending list. */
819 result = osd_oii_lookup(dev, fid, id);
825 /* Search order: 3. OI files. */
826 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
827 if (result == -ENOENT) {
828 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
829 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
830 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
832 GOTO(out, result = 0);
841 inode = osd_iget_check(info, dev, fid, id, cached);
843 result = PTR_ERR(inode);
844 if (result == -ENOENT || result == -ESTALE)
845 GOTO(out, result = -ENOENT);
847 if (result == -EREMCHG) {
850 if (unlikely(triggered))
851 GOTO(out, result = saved);
854 if (thread_is_running(&scrub->os_thread)) {
855 result = -EINPROGRESS;
856 } else if (!dev->od_noscrub) {
857 result = osd_scrub_start(dev, SS_AUTO_FULL |
858 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
859 LCONSOLE_WARN("%.16s: trigger OI scrub by RPC "
860 "for "DFID", rc = %d [1]\n",
861 osd_name(dev), PFID(fid), result);
862 if (result == 0 || result == -EALREADY)
863 result = -EINPROGRESS;
870 if (fid_is_on_ost(info, dev, fid, OI_CHECK_FLD))
873 /* We still have chance to get the valid inode: for the
874 * object which is referenced by remote name entry, the
875 * object on the local MDT will be linked under the dir
876 * of "/REMOTE_PARENT_DIR" with its FID string as name.
878 * We do not know whether the object for the given FID
879 * is referenced by some remote name entry or not, and
880 * especially for DNE II, a multiple-linked object may
881 * have many name entries reside on many MDTs.
883 * To simplify the operation, OSD will not distinguish
884 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
885 * only happened for the RPC from other MDT during the
886 * OI scrub, or for the client side RPC with FID only,
887 * such as FID to path, or from old connected client. */
889 result = osd_lookup_in_remote_parent(info, dev,
902 obj->oo_inode = inode;
903 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
905 result = osd_check_lma(env, obj);
907 if (result == -ENODATA) {
909 result = osd_oi_lookup(info, dev, fid, id,
912 /* result == -ENOENT means that the OI
913 * mapping has been removed by race,
914 * the target inode belongs to other
917 * Others error also can be returned
920 obj->oo_inode = NULL;
923 /* result == 0 means the cached OI
924 * mapping is still in the OI file,
925 * the target the inode is valid. */
928 /* The current OI mapping is from the OI file,
929 * since the inode has been found via
930 * osd_iget_check(), no need recheck OI. */
937 obj->oo_inode = NULL;
938 if (result != -EREMCHG)
942 result = osd_oi_lookup(info, dev, fid, id,
944 /* result == -ENOENT means the cached OI mapping
945 * has been removed from the OI file by race,
946 * above target inode belongs to other object.
948 * Others error also can be returned directly. */
952 /* result == 0, goto trigger */
954 /* The current OI mapping is from the OI file,
955 * since the inode has been found via
956 * osd_iget_check(), no need recheck OI. */
963 obj->oo_compat_dot_created = 1;
964 obj->oo_compat_dotdot_created = 1;
966 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
967 GOTO(out, result = 0);
969 LASSERT(obj->oo_hl_head == NULL);
970 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
971 if (obj->oo_hl_head == NULL) {
972 obj->oo_inode = NULL;
974 GOTO(out, result = -ENOMEM);
976 GOTO(out, result = 0);
979 if (result != 0 && cached)
980 fid_zero(&oic->oic_fid);
982 LINVRNT(osd_invariant(obj));
987 * Concurrency: shouldn't matter.
989 static void osd_object_init0(struct osd_object *obj)
991 LASSERT(obj->oo_inode != NULL);
992 obj->oo_dt.do_body_ops = &osd_body_ops;
993 obj->oo_dt.do_lu.lo_header->loh_attr |=
994 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
998 * Concurrency: no concurrent access is possible that early in object
1001 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1002 const struct lu_object_conf *conf)
1004 struct osd_object *obj = osd_obj(l);
1007 LINVRNT(osd_invariant(obj));
1009 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1010 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1011 l->lo_header->loh_attr |= LOHA_EXISTS;
1015 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1016 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1017 if (result == 0 && obj->oo_inode != NULL) {
1018 struct osd_thread_info *oti = osd_oti_get(env);
1019 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
1021 osd_object_init0(obj);
1022 result = osd_get_lma(oti, obj->oo_inode,
1023 &oti->oti_obj_dentry, lma);
1025 /* Convert LMAI flags to lustre LMA flags
1026 * and cache it to oo_lma_flags */
1028 lma_to_lustre_flags(lma->lma_incompat);
1029 } else if (result == -ENODATA) {
1034 LINVRNT(osd_invariant(obj));
1039 * Concurrency: no concurrent access is possible that late in object
1042 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1044 struct osd_object *obj = osd_obj(l);
1046 LINVRNT(osd_invariant(obj));
1048 dt_object_fini(&obj->oo_dt);
1049 if (obj->oo_hl_head != NULL)
1050 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1055 * Concurrency: no concurrent access is possible that late in object
1058 static void osd_index_fini(struct osd_object *o)
1060 struct iam_container *bag;
1062 if (o->oo_dir != NULL) {
1063 bag = &o->oo_dir->od_container;
1064 if (o->oo_inode != NULL) {
1065 if (bag->ic_object == o->oo_inode)
1066 iam_container_fini(bag);
1068 OBD_FREE_PTR(o->oo_dir);
1074 * Concurrency: no concurrent access is possible that late in object
1075 * life-cycle (for all existing callers, that is. New callers have to provide
1076 * their own locking.)
1078 static int osd_inode_unlinked(const struct inode *inode)
1080 return inode->i_nlink == 0;
1084 OSD_TXN_OI_DELETE_CREDITS = 20,
1085 OSD_TXN_INODE_DELETE_CREDITS = 20
1092 #if OSD_THANDLE_STATS
1094 * Set time when the handle is allocated
1096 static void osd_th_alloced(struct osd_thandle *oth)
1098 oth->oth_alloced = cfs_time_current();
1102 * Set time when the handle started
1104 static void osd_th_started(struct osd_thandle *oth)
1106 oth->oth_started = cfs_time_current();
1110 * Helper function to convert time interval to microseconds packed in
1113 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
1117 cfs_duration_usec(cfs_time_sub(end, start), &val);
1118 return val.tv_sec * 1000000 + val.tv_usec;
1122 * Check whether the we deal with this handle for too long.
1124 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1125 cfs_time_t alloced, cfs_time_t started,
1128 cfs_time_t now = cfs_time_current();
1130 LASSERT(dev != NULL);
1132 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1133 interval_to_usec(alloced, started));
1134 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1135 interval_to_usec(started, closed));
1136 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1137 interval_to_usec(closed, now));
1139 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
1140 CWARN("transaction handle %p was open for too long: "
1141 "now "CFS_TIME_T" ,"
1142 "alloced "CFS_TIME_T" ,"
1143 "started "CFS_TIME_T" ,"
1144 "closed "CFS_TIME_T"\n",
1145 oth, now, alloced, started, closed);
1146 libcfs_debug_dumpstack(NULL);
1150 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1152 cfs_time_t __closed = cfs_time_current(); \
1153 cfs_time_t __alloced = oth->oth_alloced; \
1154 cfs_time_t __started = oth->oth_started; \
1157 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1160 #else /* OSD_THANDLE_STATS */
1162 #define osd_th_alloced(h) do {} while(0)
1163 #define osd_th_started(h) do {} while(0)
1164 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1166 #endif /* OSD_THANDLE_STATS */
1169 * Concurrency: doesn't access mutable data.
1171 static int osd_param_is_not_sane(const struct osd_device *dev,
1172 const struct thandle *th)
1174 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1176 return oh->ot_credits > osd_transaction_size(dev);
1180 * Concurrency: shouldn't matter.
1182 static void osd_trans_commit_cb(struct super_block *sb,
1183 struct ldiskfs_journal_cb_entry *jcb, int error)
1185 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1186 struct thandle *th = &oh->ot_super;
1187 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1188 struct dt_txn_commit_cb *dcb, *tmp;
1190 LASSERT(oh->ot_handle == NULL);
1193 CERROR("transaction @0x%p commit error: %d\n", th, error);
1195 dt_txn_hook_commit(th);
1197 /* call per-transaction callbacks if any */
1198 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1200 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1201 "commit callback entry: magic=%x name='%s'\n",
1202 dcb->dcb_magic, dcb->dcb_name);
1203 list_del_init(&dcb->dcb_linkage);
1204 dcb->dcb_func(NULL, th, dcb, error);
1207 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1211 lu_context_exit(&th->th_ctx);
1212 lu_context_fini(&th->th_ctx);
1216 static struct thandle *osd_trans_create(const struct lu_env *env,
1217 struct dt_device *d)
1219 struct osd_thread_info *oti = osd_oti_get(env);
1220 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1221 struct osd_thandle *oh;
1225 /* on pending IO in this thread should left from prev. request */
1226 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1228 th = ERR_PTR(-ENOMEM);
1229 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1231 oh->ot_quota_trans = &oti->oti_quota_trans;
1232 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1236 th->th_tags = LCT_TX_HANDLE;
1238 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1239 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1242 memset(oti->oti_declare_ops, 0,
1243 sizeof(oti->oti_declare_ops));
1244 memset(oti->oti_declare_ops_cred, 0,
1245 sizeof(oti->oti_declare_ops_cred));
1246 memset(oti->oti_declare_ops_used, 0,
1247 sizeof(oti->oti_declare_ops_used));
1252 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1254 struct osd_thread_info *oti = osd_oti_get(env);
1255 struct osd_thandle *oh;
1257 oh = container_of0(th, struct osd_thandle, ot_super);
1258 LASSERT(oh != NULL);
1260 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1261 oti->oti_declare_ops[OSD_OT_CREATE],
1262 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1263 oti->oti_declare_ops_used[OSD_OT_CREATE],
1264 oti->oti_declare_ops[OSD_OT_DESTROY],
1265 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1266 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1267 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1268 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1269 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1270 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1271 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1272 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1273 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1274 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1275 oti->oti_declare_ops[OSD_OT_WRITE],
1276 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1277 oti->oti_declare_ops_used[OSD_OT_WRITE],
1278 oti->oti_declare_ops[OSD_OT_PUNCH],
1279 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1280 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1281 oti->oti_declare_ops[OSD_OT_QUOTA],
1282 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1283 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1284 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1285 oti->oti_declare_ops[OSD_OT_INSERT],
1286 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1287 oti->oti_declare_ops_used[OSD_OT_INSERT],
1288 oti->oti_declare_ops[OSD_OT_DELETE],
1289 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1290 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1291 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1292 oti->oti_declare_ops[OSD_OT_REF_ADD],
1293 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1294 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1295 oti->oti_declare_ops[OSD_OT_REF_DEL],
1296 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1297 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1301 * Concurrency: shouldn't matter.
1303 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1306 struct osd_thread_info *oti = osd_oti_get(env);
1307 struct osd_device *dev = osd_dt_dev(d);
1309 struct osd_thandle *oh;
1314 LASSERT(current->journal_info == NULL);
1316 oh = container_of0(th, struct osd_thandle, ot_super);
1317 LASSERT(oh != NULL);
1318 LASSERT(oh->ot_handle == NULL);
1320 rc = dt_txn_hook_start(env, d, th);
1324 if (unlikely(osd_param_is_not_sane(dev, th))) {
1325 static unsigned long last_printed;
1326 static int last_credits;
1328 CWARN("%.16s: too many transaction credits (%d > %d)\n",
1329 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
1331 osd_journal(dev)->j_max_transaction_buffers);
1333 osd_trans_dump_creds(env, th);
1335 if (last_credits != oh->ot_credits &&
1336 time_after(jiffies, last_printed +
1337 msecs_to_jiffies(60 * MSEC_PER_SEC))) {
1338 libcfs_debug_dumpstack(NULL);
1339 last_credits = oh->ot_credits;
1340 last_printed = jiffies;
1342 /* XXX Limit the credits to 'max_transaction_buffers', and
1343 * let the underlying filesystem to catch the error if
1344 * we really need so many credits.
1346 * This should be removed when we can calculate the
1347 * credits precisely. */
1348 oh->ot_credits = osd_transaction_size(dev);
1352 * XXX temporary stuff. Some abstraction layer should
1355 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1359 LASSERT(oti->oti_txns == 0);
1360 lu_context_init(&th->th_ctx, th->th_tags);
1361 lu_context_enter(&th->th_ctx);
1363 lu_device_get(&d->dd_lu_dev);
1364 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1375 static int osd_seq_exists(const struct lu_env *env,
1376 struct osd_device *osd, u64 seq)
1378 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1379 struct seq_server_site *ss = osd_seq_site(osd);
1383 LASSERT(ss != NULL);
1384 LASSERT(ss->ss_server_fld != NULL);
1386 rc = osd_fld_lookup(env, osd, seq, range);
1389 CERROR("%s: can't lookup FLD sequence "LPX64
1390 ": rc = %d\n", osd_name(osd), seq, rc);
1394 RETURN(ss->ss_node_id == range->lsr_index);
1397 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1399 struct dt_txn_commit_cb *dcb;
1400 struct dt_txn_commit_cb *tmp;
1402 /* call per-transaction stop callbacks if any */
1403 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1405 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1406 "commit callback entry: magic=%x name='%s'\n",
1407 dcb->dcb_magic, dcb->dcb_name);
1408 list_del_init(&dcb->dcb_linkage);
1409 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1414 * Concurrency: shouldn't matter.
1416 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1419 int rc = 0, remove_agents = 0;
1420 struct osd_thandle *oh;
1421 struct osd_thread_info *oti = osd_oti_get(env);
1422 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1423 struct osd_device *osd = osd_dt_dev(th->th_dev);
1424 struct qsd_instance *qsd = osd->od_quota_slave;
1425 struct lquota_trans *qtrans;
1428 oh = container_of0(th, struct osd_thandle, ot_super);
1430 /* reset OI cache for safety */
1431 oti->oti_ins_cache_used = 0;
1433 remove_agents = oh->ot_remove_agents;
1435 qtrans = oh->ot_quota_trans;
1436 oh->ot_quota_trans = NULL;
1438 if (oh->ot_handle != NULL) {
1439 handle_t *hdl = oh->ot_handle;
1442 * add commit callback
1443 * notice we don't do this in osd_trans_start()
1444 * as underlying transaction can change during truncate
1446 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1449 LASSERT(oti->oti_txns == 1);
1452 rc = dt_txn_hook_stop(env, th);
1454 CERROR("%s: failed in transaction hook: rc = %d\n",
1457 osd_trans_stop_cb(oh, rc);
1458 /* hook functions might modify th_sync */
1459 hdl->h_sync = th->th_sync;
1461 oh->ot_handle = NULL;
1462 OSD_CHECK_SLOW_TH(oh, osd, rc = ldiskfs_journal_stop(hdl));
1464 CERROR("%s: failed to stop transaction: rc = %d\n",
1467 osd_trans_stop_cb(oh, th->th_result);
1471 /* inform the quota slave device that the transaction is stopping */
1472 qsd_op_end(env, qsd, qtrans);
1474 /* as we want IO to journal and data IO be concurrent, we don't block
1475 * awaiting data IO completion in osd_do_bio(), instead we wait here
1476 * once transaction is submitted to the journal. all reqular requests
1477 * don't do direct IO (except read/write), thus this wait_event becomes
1480 * IMPORTANT: we have to wait till any IO submited by the thread is
1481 * completed otherwise iobuf may be corrupted by different request
1483 wait_event(iobuf->dr_wait,
1484 atomic_read(&iobuf->dr_numreqs) == 0);
1485 osd_fini_iobuf(osd, iobuf);
1487 rc = iobuf->dr_error;
1489 if (unlikely(remove_agents != 0))
1490 osd_process_scheduled_agent_removals(env, osd);
1495 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1497 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1500 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1501 LASSERT(&dcb->dcb_func != NULL);
1502 if (dcb->dcb_flags & DCB_TRANS_STOP)
1503 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1505 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1511 * Called just before object is freed. Releases all resources except for
1512 * object itself (that is released by osd_object_free()).
1514 * Concurrency: no concurrent access is possible that late in object
1517 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1519 struct osd_object *obj = osd_obj(l);
1520 struct inode *inode = obj->oo_inode;
1522 LINVRNT(osd_invariant(obj));
1525 * If object is unlinked remove fid->ino mapping from object index.
1528 osd_index_fini(obj);
1529 if (inode != NULL) {
1530 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1531 qid_t uid = i_uid_read(inode);
1532 qid_t gid = i_gid_read(inode);
1535 obj->oo_inode = NULL;
1538 struct osd_thread_info *info = osd_oti_get(env);
1539 struct lquota_id_info *qi = &info->oti_qi;
1541 /* Release granted quota to master if necessary */
1542 qi->lqi_id.qid_uid = uid;
1543 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1545 qi->lqi_id.qid_uid = gid;
1546 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1552 * Concurrency: ->loo_object_release() is called under site spin-lock.
1554 static void osd_object_release(const struct lu_env *env,
1555 struct lu_object *l)
1557 struct osd_object *o = osd_obj(l);
1558 /* nobody should be releasing a non-destroyed object with nlink=0
1559 * the API allows this, but ldiskfs doesn't like and then report
1560 * this inode as deleted */
1561 if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
1566 * Concurrency: shouldn't matter.
1568 static int osd_object_print(const struct lu_env *env, void *cookie,
1569 lu_printer_t p, const struct lu_object *l)
1571 struct osd_object *o = osd_obj(l);
1572 struct iam_descr *d;
1574 if (o->oo_dir != NULL)
1575 d = o->oo_dir->od_container.ic_descr;
1578 return (*p)(env, cookie,
1579 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1581 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1582 o->oo_inode ? o->oo_inode->i_generation : 0,
1583 d ? d->id_ops->id_name : "plain");
1587 * Concurrency: shouldn't matter.
1589 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1590 struct obd_statfs *sfs)
1592 struct osd_device *osd = osd_dt_dev(d);
1593 struct super_block *sb = osd_sb(osd);
1594 struct kstatfs *ksfs;
1598 if (unlikely(osd->od_mnt == NULL))
1599 return -EINPROGRESS;
1601 /* osd_lproc.c call this without env, allocate ksfs for that case */
1602 if (unlikely(env == NULL)) {
1603 OBD_ALLOC_PTR(ksfs);
1607 ksfs = &osd_oti_get(env)->oti_ksfs;
1610 result = sb->s_op->statfs(sb->s_root, ksfs);
1614 statfs_pack(sfs, ksfs);
1615 if (unlikely(sb->s_flags & MS_RDONLY))
1616 sfs->os_state = OS_STATE_READONLY;
1617 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
1618 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1619 sfs->os_maxbytes = sb->s_maxbytes;
1621 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1624 * Reserve some space so to avoid fragmenting the filesystem too much.
1625 * Fragmentation not only impacts performance, but can also increase
1626 * metadata overhead significantly, causing grant calculation to be
1629 * Reserve 0.78% of total space, at least 8MB for small filesystems.
1631 CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
1632 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
1633 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
1634 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
1636 sfs->os_blocks -= reserved;
1637 sfs->os_bfree -= min(reserved, sfs->os_bfree);
1638 sfs->os_bavail -= min(reserved, sfs->os_bavail);
1641 if (unlikely(env == NULL))
1647 * Estimate space needed for file creations. We assume the largest filename
1648 * which is 2^64 - 1, hence a filename of 20 chars.
1649 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1651 #ifdef __LDISKFS_DIR_REC_LEN
1652 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1654 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1658 * Concurrency: doesn't access mutable data.
1660 static void osd_conf_get(const struct lu_env *env,
1661 const struct dt_device *dev,
1662 struct dt_device_param *param)
1664 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1668 * XXX should be taken from not-yet-existing fs abstraction layer.
1670 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1671 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1672 param->ddp_symlink_max = sb->s_blocksize;
1673 param->ddp_mount_type = LDD_MT_LDISKFS;
1674 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1675 param->ddp_maxbytes = sb->s_maxbytes;
1677 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1678 /* inode are statically allocated, so per-inode space consumption
1679 * is the space consumed by the directory entry */
1680 param->ddp_inodespace = PER_OBJ_USAGE;
1681 /* EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
1682 * = 128MB) which is unlikely to be hit in real life. Report a smaller
1683 * maximum length to not under count the actual number of extents
1684 * needed for writing a file. */
1685 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
1686 /* worst-case extent insertion metadata overhead */
1687 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
1688 param->ddp_mntopts = 0;
1689 if (test_opt(sb, XATTR_USER))
1690 param->ddp_mntopts |= MNTOPT_USERXATTR;
1691 if (test_opt(sb, POSIX_ACL))
1692 param->ddp_mntopts |= MNTOPT_ACL;
1694 /* LOD might calculate the max stripe count based on max_ea_size,
1695 * so we need take account in the overhead as well,
1696 * xattr_header + magic + xattr_entry_head */
1697 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
1698 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
1700 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1701 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1702 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
1706 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
1710 * Concurrency: shouldn't matter.
1712 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1716 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1718 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1720 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
1721 LUSTRE_OSD_LDISKFS_NAME, rc);
1727 * Start commit for OSD device.
1729 * An implementation of dt_commit_async method for OSD device.
1730 * Asychronously starts underlayng fs sync and thereby a transaction
1733 * \param env environment
1734 * \param d dt device
1736 * \see dt_device_operations
1738 static int osd_commit_async(const struct lu_env *env,
1739 struct dt_device *d)
1741 struct super_block *s = osd_sb(osd_dt_dev(d));
1744 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1745 RETURN(s->s_op->sync_fs(s, 0));
1749 * Concurrency: shouldn't matter.
1752 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1754 struct super_block *sb = osd_sb(osd_dt_dev(d));
1755 struct block_device *dev = sb->s_bdev;
1756 #ifdef HAVE_DEV_SET_RDONLY
1757 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1760 int rc = -EOPNOTSUPP;
1764 #ifdef HAVE_DEV_SET_RDONLY
1765 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1767 if (jdev && (jdev != dev)) {
1768 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1770 dev_set_rdonly(jdev);
1772 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1773 dev_set_rdonly(dev);
1775 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1776 osd_dt_dev(d)->od_svname, (long)dev, rc);
1782 * Note: we do not count into QUOTA here.
1783 * If we mount with --data_journal we may need more.
1785 const int osd_dto_credits_noquota[DTO_NR] = {
1788 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1789 * SINGLEDATA_TRANS_BLOCKS(8)
1790 * XXX Note: maybe iam need more, since iam have more level than
1793 [DTO_INDEX_INSERT] = 16,
1796 * just modify a single entry, probably merge few within a block
1798 [DTO_INDEX_DELETE] = 1,
1802 [DTO_INDEX_UPDATE] = 16,
1804 * 4(inode, inode bits, groups, GDT)
1805 * notice: OI updates are counted separately with DTO_INDEX_INSERT
1807 [DTO_OBJECT_CREATE] = 4,
1809 * 4(inode, inode bits, groups, GDT)
1810 * notice: OI updates are counted separately with DTO_INDEX_DELETE
1812 [DTO_OBJECT_DELETE] = 4,
1814 * Attr set credits (inode)
1816 [DTO_ATTR_SET_BASE] = 1,
1818 * Xattr set. The same as xattr of EXT3.
1819 * DATA_TRANS_BLOCKS(14)
1820 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1821 * are also counted in. Do not know why?
1823 [DTO_XATTR_SET] = 14,
1825 * credits for inode change during write.
1827 [DTO_WRITE_BASE] = 3,
1829 * credits for single block write.
1831 [DTO_WRITE_BLOCK] = 14,
1833 * Attr set credits for chown.
1834 * This is extra credits for setattr, and it is null without quota
1836 [DTO_ATTR_SET_CHOWN] = 0
1839 static const struct dt_device_operations osd_dt_ops = {
1840 .dt_root_get = osd_root_get,
1841 .dt_statfs = osd_statfs,
1842 .dt_trans_create = osd_trans_create,
1843 .dt_trans_start = osd_trans_start,
1844 .dt_trans_stop = osd_trans_stop,
1845 .dt_trans_cb_add = osd_trans_cb_add,
1846 .dt_conf_get = osd_conf_get,
1847 .dt_sync = osd_sync,
1849 .dt_commit_async = osd_commit_async,
1852 static void osd_object_read_lock(const struct lu_env *env,
1853 struct dt_object *dt, unsigned role)
1855 struct osd_object *obj = osd_dt_obj(dt);
1856 struct osd_thread_info *oti = osd_oti_get(env);
1858 LINVRNT(osd_invariant(obj));
1860 LASSERT(obj->oo_owner != env);
1861 down_read_nested(&obj->oo_sem, role);
1863 LASSERT(obj->oo_owner == NULL);
1867 static void osd_object_write_lock(const struct lu_env *env,
1868 struct dt_object *dt, unsigned role)
1870 struct osd_object *obj = osd_dt_obj(dt);
1871 struct osd_thread_info *oti = osd_oti_get(env);
1873 LINVRNT(osd_invariant(obj));
1875 LASSERT(obj->oo_owner != env);
1876 down_write_nested(&obj->oo_sem, role);
1878 LASSERT(obj->oo_owner == NULL);
1879 obj->oo_owner = env;
1883 static void osd_object_read_unlock(const struct lu_env *env,
1884 struct dt_object *dt)
1886 struct osd_object *obj = osd_dt_obj(dt);
1887 struct osd_thread_info *oti = osd_oti_get(env);
1889 LINVRNT(osd_invariant(obj));
1891 LASSERT(oti->oti_r_locks > 0);
1893 up_read(&obj->oo_sem);
1896 static void osd_object_write_unlock(const struct lu_env *env,
1897 struct dt_object *dt)
1899 struct osd_object *obj = osd_dt_obj(dt);
1900 struct osd_thread_info *oti = osd_oti_get(env);
1902 LINVRNT(osd_invariant(obj));
1904 LASSERT(obj->oo_owner == env);
1905 LASSERT(oti->oti_w_locks > 0);
1907 obj->oo_owner = NULL;
1908 up_write(&obj->oo_sem);
1911 static int osd_object_write_locked(const struct lu_env *env,
1912 struct dt_object *dt)
1914 struct osd_object *obj = osd_dt_obj(dt);
1916 LINVRNT(osd_invariant(obj));
1918 return obj->oo_owner == env;
1921 static struct timespec *osd_inode_time(const struct lu_env *env,
1922 struct inode *inode, __u64 seconds)
1924 struct osd_thread_info *oti = osd_oti_get(env);
1925 struct timespec *t = &oti->oti_time;
1927 t->tv_sec = seconds;
1929 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1933 static void osd_inode_getattr(const struct lu_env *env,
1934 struct inode *inode, struct lu_attr *attr)
1936 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1937 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1938 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1941 attr->la_atime = LTIME_S(inode->i_atime);
1942 attr->la_mtime = LTIME_S(inode->i_mtime);
1943 attr->la_ctime = LTIME_S(inode->i_ctime);
1944 attr->la_mode = inode->i_mode;
1945 attr->la_size = i_size_read(inode);
1946 attr->la_blocks = inode->i_blocks;
1947 attr->la_uid = i_uid_read(inode);
1948 attr->la_gid = i_gid_read(inode);
1949 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
1950 attr->la_nlink = inode->i_nlink;
1951 attr->la_rdev = inode->i_rdev;
1952 attr->la_blksize = 1 << inode->i_blkbits;
1953 attr->la_blkbits = inode->i_blkbits;
1956 static int osd_attr_get(const struct lu_env *env,
1957 struct dt_object *dt,
1958 struct lu_attr *attr)
1960 struct osd_object *obj = osd_dt_obj(dt);
1962 if (unlikely(!dt_object_exists(dt)))
1964 if (unlikely(obj->oo_destroyed))
1967 LASSERT(!dt_object_remote(dt));
1968 LINVRNT(osd_invariant(obj));
1970 spin_lock(&obj->oo_guard);
1971 osd_inode_getattr(env, obj->oo_inode, attr);
1972 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
1973 attr->la_flags |= LUSTRE_ORPHAN_FL;
1974 spin_unlock(&obj->oo_guard);
1979 static int osd_declare_attr_set(const struct lu_env *env,
1980 struct dt_object *dt,
1981 const struct lu_attr *attr,
1982 struct thandle *handle)
1984 struct osd_thandle *oh;
1985 struct osd_object *obj;
1986 struct osd_thread_info *info = osd_oti_get(env);
1987 struct lquota_id_info *qi = &info->oti_qi;
1995 LASSERT(dt != NULL);
1996 LASSERT(handle != NULL);
1998 obj = osd_dt_obj(dt);
1999 LASSERT(osd_invariant(obj));
2001 oh = container_of0(handle, struct osd_thandle, ot_super);
2002 LASSERT(oh->ot_handle == NULL);
2004 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2005 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2007 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2008 osd_dto_credits_noquota[DTO_XATTR_SET]);
2010 if (attr == NULL || obj->oo_inode == NULL)
2013 bspace = obj->oo_inode->i_blocks;
2014 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2015 bspace = toqb(bspace);
2017 /* Changing ownership is always preformed by super user, it should not
2020 * We still need to call the osd_declare_qid() to calculate the journal
2021 * credits for updating quota accounting files and to trigger quota
2022 * space adjustment once the operation is completed.*/
2023 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2025 uid = i_uid_read(obj->oo_inode);
2026 qi->lqi_type = USRQUOTA;
2027 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2028 /* inode accounting */
2029 qi->lqi_is_blk = false;
2031 /* one more inode for the new uid ... */
2032 qi->lqi_id.qid_uid = attr->la_uid;
2034 /* Reserve credits for the new uid */
2035 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2036 if (rc == -EDQUOT || rc == -EINPROGRESS)
2041 /* and one less inode for the current uid */
2042 qi->lqi_id.qid_uid = uid;
2044 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2045 if (rc == -EDQUOT || rc == -EINPROGRESS)
2050 /* block accounting */
2051 qi->lqi_is_blk = true;
2053 /* more blocks for the new uid ... */
2054 qi->lqi_id.qid_uid = attr->la_uid;
2055 qi->lqi_space = bspace;
2057 * Credits for the new uid has been reserved, re-use "obj"
2058 * to save credit reservation.
2060 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2061 if (rc == -EDQUOT || rc == -EINPROGRESS)
2066 /* and finally less blocks for the current uid */
2067 qi->lqi_id.qid_uid = uid;
2068 qi->lqi_space = -bspace;
2069 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2070 if (rc == -EDQUOT || rc == -EINPROGRESS)
2076 gid = i_gid_read(obj->oo_inode);
2077 qi->lqi_type = GRPQUOTA;
2078 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2080 /* inode accounting */
2081 qi->lqi_is_blk = false;
2083 /* one more inode for the new gid ... */
2084 qi->lqi_id.qid_gid = attr->la_gid;
2086 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2087 if (rc == -EDQUOT || rc == -EINPROGRESS)
2092 /* and one less inode for the current gid */
2093 qi->lqi_id.qid_gid = gid;
2095 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2096 if (rc == -EDQUOT || rc == -EINPROGRESS)
2101 /* block accounting */
2102 qi->lqi_is_blk = true;
2104 /* more blocks for the new gid ... */
2105 qi->lqi_id.qid_gid = attr->la_gid;
2106 qi->lqi_space = bspace;
2107 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2108 if (rc == -EDQUOT || rc == -EINPROGRESS)
2113 /* and finally less blocks for the current gid */
2114 qi->lqi_id.qid_gid = gid;
2115 qi->lqi_space = -bspace;
2116 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2117 if (rc == -EDQUOT || rc == -EINPROGRESS)
2126 static int osd_inode_setattr(const struct lu_env *env,
2127 struct inode *inode, const struct lu_attr *attr)
2129 __u64 bits = attr->la_valid;
2131 /* Only allow set size for regular file */
2132 if (!S_ISREG(inode->i_mode))
2133 bits &= ~(LA_SIZE | LA_BLOCKS);
2138 if (bits & LA_ATIME)
2139 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2140 if (bits & LA_CTIME)
2141 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2142 if (bits & LA_MTIME)
2143 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2144 if (bits & LA_SIZE) {
2145 LDISKFS_I(inode)->i_disksize = attr->la_size;
2146 i_size_write(inode, attr->la_size);
2150 /* OSD should not change "i_blocks" which is used by quota.
2151 * "i_blocks" should be changed by ldiskfs only. */
2152 if (bits & LA_BLOCKS)
2153 inode->i_blocks = attr->la_blocks;
2156 inode->i_mode = (inode->i_mode & S_IFMT) |
2157 (attr->la_mode & ~S_IFMT);
2159 i_uid_write(inode, attr->la_uid);
2161 i_gid_write(inode, attr->la_gid);
2162 if (bits & LA_NLINK)
2163 set_nlink(inode, attr->la_nlink);
2165 inode->i_rdev = attr->la_rdev;
2167 if (bits & LA_FLAGS) {
2168 /* always keep S_NOCMTIME */
2169 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2175 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2177 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2178 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2182 ll_vfs_dq_init(inode);
2184 if (attr->la_valid & LA_UID)
2185 iattr.ia_valid |= ATTR_UID;
2186 if (attr->la_valid & LA_GID)
2187 iattr.ia_valid |= ATTR_GID;
2188 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2189 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2191 rc = ll_vfs_dq_transfer(inode, &iattr);
2193 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2194 "enforcement enabled on the ldiskfs "
2195 "filesystem?\n", inode->i_sb->s_id, rc);
2202 static int osd_attr_set(const struct lu_env *env,
2203 struct dt_object *dt,
2204 const struct lu_attr *attr,
2205 struct thandle *handle)
2207 struct osd_object *obj = osd_dt_obj(dt);
2208 struct inode *inode;
2211 if (!dt_object_exists(dt))
2214 LASSERT(handle != NULL);
2215 LASSERT(!dt_object_remote(dt));
2216 LASSERT(osd_invariant(obj));
2218 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2220 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2221 struct osd_thread_info *oti = osd_oti_get(env);
2222 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2223 struct lu_fid *fid1 = &oti->oti_fid;
2224 struct osd_inode_id *id = &oti->oti_id;
2225 struct iam_path_descr *ipd;
2226 struct iam_container *bag;
2227 struct osd_thandle *oh;
2230 fid_cpu_to_be(fid1, fid0);
2231 memset(id, 1, sizeof(*id));
2232 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2233 fid0)->oi_dir.od_container;
2234 ipd = osd_idx_ipd_get(env, bag);
2235 if (unlikely(ipd == NULL))
2238 oh = container_of0(handle, struct osd_thandle, ot_super);
2239 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2240 (const struct iam_rec *)id, ipd);
2241 osd_ipd_put(env, bag, ipd);
2242 return(rc > 0 ? 0 : rc);
2245 inode = obj->oo_inode;
2247 rc = osd_quota_transfer(inode, attr);
2251 spin_lock(&obj->oo_guard);
2252 rc = osd_inode_setattr(env, inode, attr);
2253 spin_unlock(&obj->oo_guard);
2257 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2259 if (!(attr->la_valid & LA_FLAGS))
2262 /* Let's check if there are extra flags need to be set into LMA */
2263 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2264 struct osd_thread_info *info = osd_oti_get(env);
2265 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2267 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
2271 lma->lma_incompat |=
2272 lustre_to_lma_flags(attr->la_flags);
2273 lustre_lma_swab(lma);
2274 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2275 lma, sizeof(*lma), XATTR_REPLACE);
2277 struct osd_device *osd = osd_obj2dev(obj);
2279 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2280 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2281 lma->lma_incompat, rc);
2284 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2286 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2289 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2294 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2295 struct osd_object *obj,
2296 const char *name, const int namelen)
2298 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2301 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2302 umode_t mode, struct dt_allocation_hint *hint,
2306 struct osd_device *osd = osd_obj2dev(obj);
2307 struct osd_thandle *oth;
2308 struct dt_object *parent = NULL;
2309 struct inode *inode;
2311 LINVRNT(osd_invariant(obj));
2312 LASSERT(obj->oo_inode == NULL);
2313 LASSERT(obj->oo_hl_head == NULL);
2315 if (S_ISDIR(mode) && ldiskfs_pdo) {
2316 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2317 if (obj->oo_hl_head == NULL)
2321 oth = container_of(th, struct osd_thandle, ot_super);
2322 LASSERT(oth->ot_handle->h_transaction != NULL);
2324 if (hint != NULL && hint->dah_parent != NULL &&
2325 !dt_object_remote(hint->dah_parent))
2326 parent = hint->dah_parent;
2328 inode = ldiskfs_create_inode(oth->ot_handle,
2329 parent ? osd_dt_obj(parent)->oo_inode :
2330 osd_sb(osd)->s_root->d_inode,
2332 if (!IS_ERR(inode)) {
2333 /* Do not update file c/mtime in ldiskfs. */
2334 inode->i_flags |= S_NOCMTIME;
2336 /* For new created object, it must be consistent,
2337 * and it is unnecessary to scrub against it. */
2338 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2340 obj->oo_inode = inode;
2343 if (obj->oo_hl_head != NULL) {
2344 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2345 obj->oo_hl_head = NULL;
2347 result = PTR_ERR(inode);
2349 LINVRNT(osd_invariant(obj));
2357 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2358 struct lu_attr *attr,
2359 struct dt_allocation_hint *hint,
2360 struct dt_object_format *dof,
2364 struct osd_thandle *oth;
2365 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
2367 LASSERT(S_ISDIR(attr->la_mode));
2369 oth = container_of(th, struct osd_thandle, ot_super);
2370 LASSERT(oth->ot_handle->h_transaction != NULL);
2371 result = osd_mkfile(info, obj, mode, hint, th);
2376 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2377 struct lu_attr *attr,
2378 struct dt_allocation_hint *hint,
2379 struct dt_object_format *dof,
2383 struct osd_thandle *oth;
2384 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2386 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2388 LASSERT(S_ISREG(attr->la_mode));
2390 oth = container_of(th, struct osd_thandle, ot_super);
2391 LASSERT(oth->ot_handle->h_transaction != NULL);
2393 result = osd_mkfile(info, obj, mode, hint, th);
2395 LASSERT(obj->oo_inode != NULL);
2396 if (feat->dif_flags & DT_IND_VARKEY)
2397 result = iam_lvar_create(obj->oo_inode,
2398 feat->dif_keysize_max,
2400 feat->dif_recsize_max,
2403 result = iam_lfix_create(obj->oo_inode,
2404 feat->dif_keysize_max,
2406 feat->dif_recsize_max,
2413 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2414 struct lu_attr *attr,
2415 struct dt_allocation_hint *hint,
2416 struct dt_object_format *dof,
2419 LASSERT(S_ISREG(attr->la_mode));
2420 return osd_mkfile(info, obj, (attr->la_mode &
2421 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2424 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2425 struct lu_attr *attr,
2426 struct dt_allocation_hint *hint,
2427 struct dt_object_format *dof,
2430 LASSERT(S_ISLNK(attr->la_mode));
2431 return osd_mkfile(info, obj, (attr->la_mode &
2432 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2435 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2436 struct lu_attr *attr,
2437 struct dt_allocation_hint *hint,
2438 struct dt_object_format *dof,
2441 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2444 LINVRNT(osd_invariant(obj));
2445 LASSERT(obj->oo_inode == NULL);
2446 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2447 S_ISFIFO(mode) || S_ISSOCK(mode));
2449 result = osd_mkfile(info, obj, mode, hint, th);
2451 LASSERT(obj->oo_inode != NULL);
2453 * This inode should be marked dirty for i_rdev. Currently
2454 * that is done in the osd_attr_init().
2456 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2459 LINVRNT(osd_invariant(obj));
2463 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2465 struct dt_allocation_hint *hint,
2466 struct dt_object_format *dof,
2469 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2471 osd_obj_type_f result;
2487 result = osd_mk_index;
2498 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2499 struct dt_object *parent, struct dt_object *child,
2504 ah->dah_parent = parent;
2505 ah->dah_mode = child_mode;
2507 if (parent != NULL && !dt_object_remote(parent)) {
2508 /* will help to find FID->ino at dt_insert("..") */
2509 struct osd_object *pobj = osd_dt_obj(parent);
2510 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2514 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2515 struct lu_attr *attr, struct dt_object_format *dof)
2517 struct inode *inode = obj->oo_inode;
2518 __u64 valid = attr->la_valid;
2521 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2523 if (dof->dof_type != DFT_NODE)
2524 attr->la_valid &= ~LA_RDEV;
2525 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2526 attr->la_valid &= ~LA_ATIME;
2527 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2528 attr->la_valid &= ~LA_CTIME;
2529 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2530 attr->la_valid &= ~LA_MTIME;
2532 result = osd_quota_transfer(inode, attr);
2536 if (attr->la_valid != 0) {
2537 result = osd_inode_setattr(info->oti_env, inode, attr);
2539 * The osd_inode_setattr() should always succeed here. The
2540 * only error that could be returned is EDQUOT when we are
2541 * trying to change the UID or GID of the inode. However, this
2542 * should not happen since quota enforcement is no longer
2543 * enabled on ldiskfs (lquota takes care of it).
2545 LASSERTF(result == 0, "%d\n", result);
2546 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2549 attr->la_valid = valid;
2553 * Helper function for osd_object_create()
2555 * \retval 0, on success
2557 static int __osd_object_create(struct osd_thread_info *info,
2558 struct osd_object *obj, struct lu_attr *attr,
2559 struct dt_allocation_hint *hint,
2560 struct dt_object_format *dof,
2566 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2568 /* we drop umask so that permissions we pass are not affected */
2569 umask = current->fs->umask;
2570 current->fs->umask = 0;
2572 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2574 if (likely(obj->oo_inode != NULL)) {
2575 LASSERT(obj->oo_inode->i_state & I_NEW);
2577 /* Unlock the inode before attr initialization to avoid
2578 * unnecessary dqget operations. LU-6378 */
2579 unlock_new_inode(obj->oo_inode);
2582 if (likely(result == 0)) {
2583 osd_attr_init(info, obj, attr, dof);
2584 osd_object_init0(obj);
2587 /* restore previous umask value */
2588 current->fs->umask = umask;
2590 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
2596 * Helper function for osd_object_create()
2598 * \retval 0, on success
2600 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2601 const struct lu_fid *fid, struct thandle *th)
2603 struct osd_thread_info *info = osd_oti_get(env);
2604 struct osd_inode_id *id = &info->oti_id;
2605 struct osd_device *osd = osd_obj2dev(obj);
2606 struct osd_thandle *oh;
2609 LASSERT(obj->oo_inode != NULL);
2611 oh = container_of0(th, struct osd_thandle, ot_super);
2612 LASSERT(oh->ot_handle);
2613 osd_trans_exec_op(env, th, OSD_OT_INSERT);
2615 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2616 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle, OI_CHECK_FLD);
2617 osd_trans_exec_check(env, th, OSD_OT_INSERT);
2622 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2623 u64 seq, struct lu_seq_range *range)
2625 struct seq_server_site *ss = osd_seq_site(osd);
2627 if (fid_seq_is_idif(seq)) {
2628 fld_range_set_ost(range);
2629 range->lsr_index = idif_ost_idx(seq);
2633 if (!fid_seq_in_fldb(seq)) {
2634 fld_range_set_mdt(range);
2636 /* FIXME: If ss is NULL, it suppose not get lsr_index
2638 range->lsr_index = ss->ss_node_id;
2642 LASSERT(ss != NULL);
2643 fld_range_set_any(range);
2644 /* OSD will only do local fld lookup */
2645 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
2649 * Concurrency: no external locking is necessary.
2651 static int osd_declare_object_create(const struct lu_env *env,
2652 struct dt_object *dt,
2653 struct lu_attr *attr,
2654 struct dt_allocation_hint *hint,
2655 struct dt_object_format *dof,
2656 struct thandle *handle)
2658 struct osd_thandle *oh;
2662 LASSERT(handle != NULL);
2664 oh = container_of0(handle, struct osd_thandle, ot_super);
2665 LASSERT(oh->ot_handle == NULL);
2667 /* EA object consumes more credits than regular object: osd_mk_index
2668 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
2669 * leaf_node, could involves the block, block bitmap, groups, GDT
2670 * change for each block, so add 4 * 2 credits in that case. */
2671 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2672 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
2673 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
2674 /* Reuse idle OI block may cause additional one OI block
2676 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2677 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2682 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2683 osd_dt_obj(dt), false, NULL, false);
2687 /* will help to find FID->ino mapping at dt_insert() */
2688 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
2694 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2695 struct lu_attr *attr,
2696 struct dt_allocation_hint *hint,
2697 struct dt_object_format *dof, struct thandle *th)
2699 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2700 struct osd_object *obj = osd_dt_obj(dt);
2701 struct osd_thread_info *info = osd_oti_get(env);
2705 if (dt_object_exists(dt))
2708 LINVRNT(osd_invariant(obj));
2709 LASSERT(!dt_object_remote(dt));
2710 LASSERT(osd_write_locked(env, obj));
2711 LASSERT(th != NULL);
2713 if (unlikely(fid_is_acct(fid)))
2714 /* Quota files can't be created from the kernel any more,
2715 * 'tune2fs -O quota' will take care of creating them */
2718 result = __osd_object_create(info, obj, attr, hint, dof, th);
2720 result = __osd_oi_insert(env, obj, fid, th);
2721 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
2722 obj->oo_dt.do_body_ops = &osd_body_ops;
2724 LASSERT(ergo(result == 0,
2725 dt_object_exists(dt) && !dt_object_remote(dt)));
2727 LASSERT(osd_invariant(obj));
2732 * Called to destroy on-disk representation of the object
2734 * Concurrency: must be locked
2736 static int osd_declare_object_destroy(const struct lu_env *env,
2737 struct dt_object *dt,
2740 struct osd_object *obj = osd_dt_obj(dt);
2741 struct inode *inode = obj->oo_inode;
2742 struct osd_thandle *oh;
2749 oh = container_of0(th, struct osd_thandle, ot_super);
2750 LASSERT(oh->ot_handle == NULL);
2752 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2753 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2754 /* Recycle idle OI leaf may cause additional three OI blocks
2756 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2757 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2758 /* one less inode */
2759 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2760 -1, oh, obj, false, NULL, false);
2763 /* data to be truncated */
2764 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2765 0, oh, obj, true, NULL, false);
2769 /* will help to find FID->ino when this object is being
2770 * added to PENDING/ */
2771 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
2776 static int osd_object_destroy(const struct lu_env *env,
2777 struct dt_object *dt,
2780 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2781 struct osd_object *obj = osd_dt_obj(dt);
2782 struct inode *inode = obj->oo_inode;
2783 struct osd_device *osd = osd_obj2dev(obj);
2784 struct osd_thandle *oh;
2788 oh = container_of0(th, struct osd_thandle, ot_super);
2789 LASSERT(oh->ot_handle);
2791 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2793 if (unlikely(fid_is_acct(fid)))
2796 if (S_ISDIR(inode->i_mode)) {
2797 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
2798 inode->i_nlink == 2);
2799 /* it will check/delete the inode from remote parent,
2800 * how to optimize it? unlink performance impaction XXX */
2801 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2802 if (result != 0 && result != -ENOENT) {
2803 CERROR("%s: delete inode "DFID": rc = %d\n",
2804 osd_name(osd), PFID(fid), result);
2806 spin_lock(&obj->oo_guard);
2808 spin_unlock(&obj->oo_guard);
2809 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2812 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2814 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
2815 result = osd_oi_delete(osd_oti_get(env), osd, fid, oh->ot_handle,
2818 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
2819 /* XXX: add to ext3 orphan list */
2820 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2822 /* not needed in the cache anymore */
2823 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2824 obj->oo_destroyed = 1;
2830 * Put the fid into lustre_mdt_attrs, and then place the structure
2831 * inode's ea. This fid should not be altered during the life time
2834 * \retval +ve, on success
2835 * \retval -ve, on error
2837 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2839 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2840 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2842 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2846 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2849 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
2852 lustre_lma_init(lma, fid, compat, incompat);
2853 lustre_lma_swab(lma);
2855 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2857 /* LMA may already exist, but we need to check that all the
2858 * desired compat/incompat flags have been added. */
2859 if (unlikely(rc == -EEXIST)) {
2860 if (compat == 0 && incompat == 0)
2863 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2864 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2869 lustre_lma_swab(lma);
2870 if (!(~lma->lma_compat & compat) &&
2871 !(~lma->lma_incompat & incompat))
2874 lma->lma_compat |= compat;
2875 lma->lma_incompat |= incompat;
2876 lustre_lma_swab(lma);
2877 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2878 sizeof(*lma), XATTR_REPLACE);
2885 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2886 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2887 * To have compatilibility with 1.8 ldiskfs driver we need to have
2888 * magic number at start of fid data.
2889 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2892 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2893 const struct lu_fid *fid)
2895 if (!fid_is_namespace_visible(fid) ||
2896 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2897 param->edp_magic = 0;
2901 param->edp_magic = LDISKFS_LUFID_MAGIC;
2902 param->edp_len = sizeof(struct lu_fid) + 1;
2903 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2907 * Try to read the fid from inode ea into dt_rec.
2909 * \param fid object fid.
2911 * \retval 0 on success
2913 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2914 __u32 ino, struct lu_fid *fid,
2915 struct osd_inode_id *id)
2917 struct osd_thread_info *info = osd_oti_get(env);
2918 struct inode *inode;
2921 osd_id_gen(id, ino, OSD_OII_NOGEN);
2922 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2924 RETURN(PTR_ERR(inode));
2930 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2932 struct inode *parent_dir,
2933 const struct lu_fid *dot_fid,
2934 const struct lu_fid *dot_dot_fid,
2935 struct osd_thandle *oth)
2937 struct ldiskfs_dentry_param *dot_ldp;
2938 struct ldiskfs_dentry_param *dot_dot_ldp;
2939 __u32 saved_nlink = dir->i_nlink;
2942 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2943 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2945 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2946 dot_ldp->edp_magic = 0;
2948 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2949 dir, dot_ldp, dot_dot_ldp);
2950 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
2951 * the subseqent ref_add() will increase the dir->i_nlink
2952 * as 3. That is incorrect for new created directory.
2954 * It looks like hack, because we want to make the OSD API
2955 * to be order-independent for new created directory object
2956 * between dt_insert(..) and ref_add() operations.
2958 * Here, we only restore the in-RAM dir-inode's nlink attr,
2959 * becuase if the nlink attr is not 2, then there will be
2960 * ref_add() called following the dt_insert(..), such call
2961 * will make both the in-RAM and on-disk dir-inode's nlink
2962 * attr to be set as 2. LU-7447 */
2963 set_nlink(dir, saved_nlink);
2968 * Create an local agent inode for remote entry
2970 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2971 struct osd_device *osd,
2972 struct osd_object *pobj,
2973 const struct lu_fid *fid,
2977 struct osd_thread_info *info = osd_oti_get(env);
2978 struct inode *local;
2979 struct osd_thandle *oh;
2984 oh = container_of(th, struct osd_thandle, ot_super);
2985 LASSERT(oh->ot_handle->h_transaction != NULL);
2987 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
2988 if (IS_ERR(local)) {
2989 CERROR("%s: create local error %d\n", osd_name(osd),
2990 (int)PTR_ERR(local));
2994 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
2995 unlock_new_inode(local);
2997 /* Set special LMA flag for local agent inode */
2998 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3000 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3001 osd_name(osd), PFID(fid), rc);
3002 RETURN(ERR_PTR(rc));
3008 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3009 lu_object_fid(&pobj->oo_dt.do_lu),
3012 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3013 osd_name(osd), PFID(fid), rc);
3014 RETURN(ERR_PTR(rc));
3021 * when direntry is deleted, we have to take care of possible agent inode
3022 * referenced by that. unfortunately we can't do this at that point:
3023 * iget() within a running transaction leads to deadlock and we better do
3024 * not call that every delete declaration to save performance. so we put
3025 * a potention agent inode on a list and process that once the transaction
3026 * is over. Notice it's not any worse in terms of real orphans as regular
3027 * object destroy doesn't put inodes on the on-disk orphan list. this should
3028 * be addressed separately
3030 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3031 struct osd_thandle *oh,
3034 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3035 struct osd_obj_orphan *oor;
3042 oor->oor_env = (struct lu_env *)env;
3043 spin_lock(&osd->od_osfs_lock);
3044 list_add(&oor->oor_list, &osd->od_orphan_list);
3045 spin_unlock(&osd->od_osfs_lock);
3047 oh->ot_remove_agents = 1;
3053 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3054 struct osd_device *osd)
3056 struct osd_thread_info *info = osd_oti_get(env);
3057 struct osd_obj_orphan *oor, *tmp;
3058 struct osd_inode_id id;
3059 struct list_head list;
3060 struct inode *inode;
3065 INIT_LIST_HEAD(&list);
3067 spin_lock(&osd->od_osfs_lock);
3068 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3069 if (oor->oor_env == env) {
3070 list_del(&oor->oor_list);
3071 list_add(&oor->oor_list, &list);
3074 spin_unlock(&osd->od_osfs_lock);
3076 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3080 list_del(&oor->oor_list);
3083 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3084 inode = osd_iget_fid(info, osd, &id, &fid);
3088 if (!osd_remote_fid(env, osd, &fid)) {
3093 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3095 mark_inode_dirty(inode);
3096 ldiskfs_journal_stop(jh);
3104 * OSD layer object create function for interoperability mode (b11826).
3105 * This is mostly similar to osd_object_create(). Only difference being, fid is
3106 * inserted into inode ea here.
3108 * \retval 0, on success
3109 * \retval -ve, on error
3111 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
3112 struct lu_attr *attr,
3113 struct dt_allocation_hint *hint,
3114 struct dt_object_format *dof,
3117 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3118 struct osd_object *obj = osd_dt_obj(dt);
3119 struct osd_thread_info *info = osd_oti_get(env);
3120 int result, on_ost = 0;
3124 if (dt_object_exists(dt))
3127 LASSERT(osd_invariant(obj));
3128 LASSERT(!dt_object_remote(dt));
3129 LASSERT(osd_write_locked(env, obj));
3130 LASSERT(th != NULL);
3132 if (unlikely(fid_is_acct(fid)))
3133 /* Quota files can't be created from the kernel any more,
3134 * 'tune2fs -O quota' will take care of creating them */
3137 result = __osd_object_create(info, obj, attr, hint, dof, th);
3139 if (fid_is_idif(fid) &&
3140 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3141 struct lu_fid *tfid = &info->oti_fid;
3142 struct ost_id *oi = &info->oti_ostid;
3144 fid_to_ostid(fid, oi);
3145 ostid_to_fid(tfid, oi, 0);
3147 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3148 LMAC_FID_ON_OST, 0);
3150 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3152 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3153 on_ost ? LMAC_FID_ON_OST : 0,
3156 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3157 obj->oo_dt.do_body_ops = &osd_body_ops;
3161 result = __osd_oi_insert(env, obj, fid, th);
3163 /* a small optimization - dt_insert() isn't usually applied
3164 * to OST objects, so we don't need to cache OI mapping for
3166 if (result == 0 && on_ost == 0) {
3167 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3168 result = osd_idc_find_and_init(env, osd, obj);
3169 LASSERT(result == 0);
3172 LASSERT(ergo(result == 0,
3173 dt_object_exists(dt) && !dt_object_remote(dt)));
3174 LINVRNT(osd_invariant(obj));
3178 static int osd_declare_object_ref_add(const struct lu_env *env,
3179 struct dt_object *dt,
3180 struct thandle *handle)
3182 struct osd_thandle *oh;
3184 /* it's possible that object doesn't exist yet */
3185 LASSERT(handle != NULL);
3187 oh = container_of0(handle, struct osd_thandle, ot_super);
3188 LASSERT(oh->ot_handle == NULL);
3190 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3191 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3197 * Concurrency: @dt is write locked.
3199 static int osd_object_ref_add(const struct lu_env *env,
3200 struct dt_object *dt, struct thandle *th)
3202 struct osd_object *obj = osd_dt_obj(dt);
3203 struct inode *inode = obj->oo_inode;
3204 struct osd_thandle *oh;
3207 if (!dt_object_exists(dt) || obj->oo_destroyed)
3210 LINVRNT(osd_invariant(obj));
3211 LASSERT(!dt_object_remote(dt));
3212 LASSERT(osd_write_locked(env, obj));
3213 LASSERT(th != NULL);
3215 oh = container_of0(th, struct osd_thandle, ot_super);
3216 LASSERT(oh->ot_handle != NULL);
3218 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3220 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3221 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3223 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3224 * (65000) subdirectories by storing "1" in i_nlink if the link count
3225 * would otherwise overflow. Directory tranversal tools understand
3226 * that (st_nlink == 1) indicates that the filesystem dose not track
3227 * hard links count on the directory, and will not abort subdirectory
3228 * scanning early once (st_nlink - 2) subdirs have been found.
3230 * This also has to properly handle the case of inodes with nlink == 0
3231 * in case they are being linked into the PENDING directory
3233 spin_lock(&obj->oo_guard);
3234 if (unlikely(inode->i_nlink == 0))
3235 /* inc_nlink from 0 may cause WARN_ON */
3236 set_nlink(inode, 1);
3238 ldiskfs_inc_count(oh->ot_handle, inode);
3239 if (!S_ISDIR(inode->i_mode))
3240 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3242 spin_unlock(&obj->oo_guard);
3244 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3245 LINVRNT(osd_invariant(obj));
3247 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3252 static int osd_declare_object_ref_del(const struct lu_env *env,
3253 struct dt_object *dt,
3254 struct thandle *handle)
3256 struct osd_thandle *oh;
3258 if (!dt_object_exists(dt))
3261 LASSERT(!dt_object_remote(dt));
3262 LASSERT(handle != NULL);
3264 oh = container_of0(handle, struct osd_thandle, ot_super);
3265 LASSERT(oh->ot_handle == NULL);
3267 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3268 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3274 * Concurrency: @dt is write locked.
3276 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
3279 struct osd_object *obj = osd_dt_obj(dt);
3280 struct inode *inode = obj->oo_inode;
3281 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3282 struct osd_thandle *oh;
3284 if (!dt_object_exists(dt))
3287 LINVRNT(osd_invariant(obj));
3288 LASSERT(!dt_object_remote(dt));
3289 LASSERT(osd_write_locked(env, obj));
3290 LASSERT(th != NULL);
3292 oh = container_of0(th, struct osd_thandle, ot_super);
3293 LASSERT(oh->ot_handle != NULL);
3295 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3297 spin_lock(&obj->oo_guard);
3298 /* That can be result of upgrade from old Lustre version and
3299 * applied only to local files. Just skip this ref_del call.
3300 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3301 if (inode->i_nlink == 0) {
3302 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3303 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3304 ", maybe an upgraded file? (LU-3915)\n",
3305 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3306 spin_unlock(&obj->oo_guard);
3310 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3311 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3313 ldiskfs_dec_count(oh->ot_handle, inode);
3314 spin_unlock(&obj->oo_guard);
3316 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3317 LINVRNT(osd_invariant(obj));
3319 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3325 * Get the 64-bit version for an inode.
3327 static int osd_object_version_get(const struct lu_env *env,
3328 struct dt_object *dt, dt_obj_version_t *ver)
3330 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3332 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
3333 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3334 *ver = LDISKFS_I(inode)->i_fs_version;
3339 * Concurrency: @dt is read locked.
3341 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3342 struct lu_buf *buf, const char *name)
3344 struct osd_object *obj = osd_dt_obj(dt);
3345 struct inode *inode = obj->oo_inode;
3346 struct osd_thread_info *info = osd_oti_get(env);
3347 struct dentry *dentry = &info->oti_obj_dentry;
3349 /* version get is not real XATTR but uses xattr API */
3350 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3351 /* for version we are just using xattr API but change inode
3353 if (buf->lb_len == 0)
3354 return sizeof(dt_obj_version_t);
3356 if (buf->lb_len < sizeof(dt_obj_version_t))
3359 osd_object_version_get(env, dt, buf->lb_buf);
3361 return sizeof(dt_obj_version_t);
3364 if (!dt_object_exists(dt))
3367 LASSERT(!dt_object_remote(dt));
3368 LASSERT(inode->i_op != NULL);
3369 LASSERT(inode->i_op->getxattr != NULL);
3371 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3375 static int osd_declare_xattr_set(const struct lu_env *env,
3376 struct dt_object *dt,
3377 const struct lu_buf *buf, const char *name,
3378 int fl, struct thandle *handle)
3380 struct osd_thandle *oh;
3382 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3384 LASSERT(handle != NULL);
3386 oh = container_of0(handle, struct osd_thandle, ot_super);
3387 LASSERT(oh->ot_handle == NULL);
3389 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3390 /* For non-upgrading case, the LMA is set first and
3391 * usually fit inode. But for upgrade case, the LMA
3392 * may be in another separated EA block. */
3393 if (!dt_object_exists(dt))
3395 else if (fl == LU_XATTR_REPLACE)
3399 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3403 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3408 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3409 /* learn xattr size from osd_xattr_get if
3410 attribute has not been read yet */
3411 buflen = __osd_xattr_get(
3412 osd_dt_obj(dt)->oo_inode,
3413 &osd_oti_get(env)->oti_obj_dentry,
3418 buflen = buf->lb_len;
3421 if (buflen > sb->s_blocksize) {
3422 credits += osd_calc_bkmap_credits(
3424 (buflen + sb->s_blocksize - 1) >>
3425 sb->s_blocksize_bits);
3429 * xattr set may involve inode quota change, reserve credits for
3430 * dquot_initialize()
3432 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3435 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3441 * Set the 64-bit version for object
3443 static void osd_object_version_set(const struct lu_env *env,
3444 struct dt_object *dt,
3445 dt_obj_version_t *new_version)
3447 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3449 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
3450 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3452 LDISKFS_I(inode)->i_fs_version = *new_version;
3453 /** Version is set after all inode operations are finished,
3454 * so we should mark it dirty here */
3455 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3459 * Concurrency: @dt is write locked.
3461 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3462 const struct lu_buf *buf, const char *name, int fl,
3463 struct thandle *handle)
3465 struct osd_object *obj = osd_dt_obj(dt);
3466 struct inode *inode = obj->oo_inode;
3467 struct osd_thread_info *info = osd_oti_get(env);
3472 LASSERT(handle != NULL);
3474 /* version set is not real XATTR */
3475 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3476 /* for version we are just using xattr API but change inode
3478 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3479 osd_object_version_set(env, dt, buf->lb_buf);
3480 return sizeof(dt_obj_version_t);
3483 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3484 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3486 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3487 if (fl & LU_XATTR_REPLACE)
3488 fs_flags |= XATTR_REPLACE;
3490 if (fl & LU_XATTR_CREATE)
3491 fs_flags |= XATTR_CREATE;
3493 if (strcmp(name, XATTR_NAME_LMV) == 0) {
3494 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3497 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3501 lma->lma_incompat |= LMAI_STRIPED;
3502 lustre_lma_swab(lma);
3503 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3504 sizeof(*lma), XATTR_REPLACE);
3509 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
3510 strcmp(name, XATTR_NAME_LINK) == 0)
3513 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3515 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3521 * Concurrency: @dt is read locked.
3523 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3524 const struct lu_buf *buf)
3526 struct osd_object *obj = osd_dt_obj(dt);
3527 struct inode *inode = obj->oo_inode;
3528 struct osd_thread_info *info = osd_oti_get(env);
3529 struct dentry *dentry = &info->oti_obj_dentry;
3531 if (!dt_object_exists(dt))
3534 LASSERT(!dt_object_remote(dt));
3535 LASSERT(inode->i_op != NULL);
3536 LASSERT(inode->i_op->listxattr != NULL);
3538 dentry->d_inode = inode;
3539 dentry->d_sb = inode->i_sb;
3540 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3543 static int osd_declare_xattr_del(const struct lu_env *env,
3544 struct dt_object *dt, const char *name,
3545 struct thandle *handle)
3547 struct osd_thandle *oh;
3548 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3550 LASSERT(!dt_object_remote(dt));
3551 LASSERT(handle != NULL);
3553 oh = container_of0(handle, struct osd_thandle, ot_super);
3554 LASSERT(oh->ot_handle == NULL);
3556 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3557 osd_dto_credits_noquota[DTO_XATTR_SET]);
3559 * xattr del may involve inode quota change, reserve credits for
3560 * dquot_initialize()
3562 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3568 * Concurrency: @dt is write locked.
3570 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3571 const char *name, struct thandle *handle)
3573 struct osd_object *obj = osd_dt_obj(dt);
3574 struct inode *inode = obj->oo_inode;
3575 struct osd_thread_info *info = osd_oti_get(env);
3576 struct dentry *dentry = &info->oti_obj_dentry;
3579 if (!dt_object_exists(dt))
3582 LASSERT(!dt_object_remote(dt));
3583 LASSERT(inode->i_op != NULL);
3584 LASSERT(inode->i_op->removexattr != NULL);
3585 LASSERT(handle != NULL);
3587 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3589 ll_vfs_dq_init(inode);
3590 dentry->d_inode = inode;
3591 dentry->d_sb = inode->i_sb;
3592 rc = inode->i_op->removexattr(dentry, name);
3593 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3597 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
3598 __u64 start, __u64 end)
3600 struct osd_object *obj = osd_dt_obj(dt);
3601 struct inode *inode = obj->oo_inode;
3602 struct osd_thread_info *info = osd_oti_get(env);
3603 struct dentry *dentry = &info->oti_obj_dentry;
3604 struct file *file = &info->oti_file;
3609 dentry->d_inode = inode;
3610 dentry->d_sb = inode->i_sb;
3611 file->f_path.dentry = dentry;
3612 file->f_mapping = inode->i_mapping;
3613 file->f_op = inode->i_fop;
3614 set_file_inode(file, inode);
3616 rc = ll_vfs_fsync_range(file, start, end, 0);
3625 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3626 const struct dt_index_features *feat)
3628 struct iam_descr *descr;
3630 if (osd_object_is_root(o))
3631 return feat == &dt_directory_features;
3633 LASSERT(o->oo_dir != NULL);
3635 descr = o->oo_dir->od_container.ic_descr;
3636 if (feat == &dt_directory_features) {
3637 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3643 feat->dif_keysize_min <= descr->id_key_size &&
3644 descr->id_key_size <= feat->dif_keysize_max &&
3645 feat->dif_recsize_min <= descr->id_rec_size &&
3646 descr->id_rec_size <= feat->dif_recsize_max &&
3647 !(feat->dif_flags & (DT_IND_VARKEY |
3648 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3649 ergo(feat->dif_flags & DT_IND_UPDATE,
3650 1 /* XXX check that object (and file system) is
3655 static int osd_iam_container_init(const struct lu_env *env,
3656 struct osd_object *obj,
3657 struct osd_directory *dir)
3659 struct iam_container *bag = &dir->od_container;
3662 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3666 result = iam_container_setup(bag);
3668 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3670 iam_container_fini(bag);
3677 * Concurrency: no external locking is necessary.
3679 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3680 const struct dt_index_features *feat)
3684 struct osd_object *obj = osd_dt_obj(dt);
3686 LINVRNT(osd_invariant(obj));
3688 if (osd_object_is_root(obj)) {
3689 dt->do_index_ops = &osd_index_ea_ops;
3691 } else if (feat == &dt_directory_features) {
3692 dt->do_index_ops = &osd_index_ea_ops;
3693 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
3698 } else if (unlikely(feat == &dt_otable_features)) {
3699 dt->do_index_ops = &osd_otable_ops;
3701 } else if (unlikely(feat == &dt_acct_features)) {
3702 dt->do_index_ops = &osd_acct_index_ops;
3705 } else if (!osd_has_index(obj)) {
3706 struct osd_directory *dir;
3711 spin_lock(&obj->oo_guard);
3712 if (obj->oo_dir == NULL)
3716 * Concurrent thread allocated container data.
3719 spin_unlock(&obj->oo_guard);
3721 * Now, that we have container data, serialize its
3724 down_write(&obj->oo_ext_idx_sem);
3726 * recheck under lock.
3728 if (!osd_has_index(obj))
3729 result = osd_iam_container_init(env, obj,
3733 up_write(&obj->oo_ext_idx_sem);
3741 if (result == 0 && skip_iam == 0) {
3742 if (!osd_iam_index_probe(env, obj, feat))
3745 LINVRNT(osd_invariant(obj));
3747 if (result == 0 && feat == &dt_quota_glb_features &&
3748 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3749 result = osd_quota_migration(env, dt);
3754 static int osd_otable_it_attr_get(const struct lu_env *env,
3755 struct dt_object *dt,
3756 struct lu_attr *attr)
3762 static const struct dt_object_operations osd_obj_ops = {
3763 .do_read_lock = osd_object_read_lock,
3764 .do_write_lock = osd_object_write_lock,
3765 .do_read_unlock = osd_object_read_unlock,
3766 .do_write_unlock = osd_object_write_unlock,
3767 .do_write_locked = osd_object_write_locked,
3768 .do_attr_get = osd_attr_get,
3769 .do_declare_attr_set = osd_declare_attr_set,
3770 .do_attr_set = osd_attr_set,
3771 .do_ah_init = osd_ah_init,
3772 .do_declare_create = osd_declare_object_create,
3773 .do_create = osd_object_create,
3774 .do_declare_destroy = osd_declare_object_destroy,
3775 .do_destroy = osd_object_destroy,
3776 .do_index_try = osd_index_try,
3777 .do_declare_ref_add = osd_declare_object_ref_add,
3778 .do_ref_add = osd_object_ref_add,
3779 .do_declare_ref_del = osd_declare_object_ref_del,
3780 .do_ref_del = osd_object_ref_del,
3781 .do_xattr_get = osd_xattr_get,
3782 .do_declare_xattr_set = osd_declare_xattr_set,
3783 .do_xattr_set = osd_xattr_set,
3784 .do_declare_xattr_del = osd_declare_xattr_del,
3785 .do_xattr_del = osd_xattr_del,
3786 .do_xattr_list = osd_xattr_list,
3787 .do_object_sync = osd_object_sync,
3791 * dt_object_operations for interoperability mode
3792 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3794 static const struct dt_object_operations osd_obj_ea_ops = {
3795 .do_read_lock = osd_object_read_lock,
3796 .do_write_lock = osd_object_write_lock,
3797 .do_read_unlock = osd_object_read_unlock,
3798 .do_write_unlock = osd_object_write_unlock,
3799 .do_write_locked = osd_object_write_locked,
3800 .do_attr_get = osd_attr_get,
3801 .do_declare_attr_set = osd_declare_attr_set,
3802 .do_attr_set = osd_attr_set,
3803 .do_ah_init = osd_ah_init,
3804 .do_declare_create = osd_declare_object_create,
3805 .do_create = osd_object_ea_create,
3806 .do_declare_destroy = osd_declare_object_destroy,
3807 .do_destroy = osd_object_destroy,
3808 .do_index_try = osd_index_try,
3809 .do_declare_ref_add = osd_declare_object_ref_add,
3810 .do_ref_add = osd_object_ref_add,
3811 .do_declare_ref_del = osd_declare_object_ref_del,
3812 .do_ref_del = osd_object_ref_del,
3813 .do_xattr_get = osd_xattr_get,
3814 .do_declare_xattr_set = osd_declare_xattr_set,
3815 .do_xattr_set = osd_xattr_set,
3816 .do_declare_xattr_del = osd_declare_xattr_del,
3817 .do_xattr_del = osd_xattr_del,
3818 .do_xattr_list = osd_xattr_list,
3819 .do_object_sync = osd_object_sync,
3822 static const struct dt_object_operations osd_obj_otable_it_ops = {
3823 .do_attr_get = osd_otable_it_attr_get,
3824 .do_index_try = osd_index_try,
3827 static int osd_index_declare_iam_delete(const struct lu_env *env,
3828 struct dt_object *dt,
3829 const struct dt_key *key,
3830 struct thandle *handle)
3832 struct osd_thandle *oh;
3834 oh = container_of0(handle, struct osd_thandle, ot_super);
3835 LASSERT(oh->ot_handle == NULL);
3837 /* Recycle may cause additional three blocks to be changed. */
3838 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3839 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3845 * delete a (key, value) pair from index \a dt specified by \a key
3847 * \param dt osd index object
3848 * \param key key for index
3849 * \param rec record reference
3850 * \param handle transaction handler
3853 * \retval -ve failure
3855 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3856 const struct dt_key *key,
3857 struct thandle *handle)
3859 struct osd_thread_info *oti = osd_oti_get(env);
3860 struct osd_object *obj = osd_dt_obj(dt);
3861 struct osd_thandle *oh;
3862 struct iam_path_descr *ipd;
3863 struct iam_container *bag = &obj->oo_dir->od_container;
3867 if (!dt_object_exists(dt))
3870 LINVRNT(osd_invariant(obj));
3871 LASSERT(!dt_object_remote(dt));
3872 LASSERT(bag->ic_object == obj->oo_inode);
3873 LASSERT(handle != NULL);
3875 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3877 ipd = osd_idx_ipd_get(env, bag);
3878 if (unlikely(ipd == NULL))
3881 oh = container_of0(handle, struct osd_thandle, ot_super);
3882 LASSERT(oh->ot_handle != NULL);
3883 LASSERT(oh->ot_handle->h_transaction != NULL);
3885 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3886 /* swab quota uid/gid provided by caller */
3887 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3888 key = (const struct dt_key *)&oti->oti_quota_id;
3891 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3892 osd_ipd_put(env, bag, ipd);
3893 LINVRNT(osd_invariant(obj));
3894 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
3898 static int osd_index_declare_ea_delete(const struct lu_env *env,
3899 struct dt_object *dt,
3900 const struct dt_key *key,
3901 struct thandle *handle)
3903 struct osd_thandle *oh;
3904 struct inode *inode;
3908 LASSERT(!dt_object_remote(dt));
3909 LASSERT(handle != NULL);
3911 oh = container_of0(handle, struct osd_thandle, ot_super);
3912 LASSERT(oh->ot_handle == NULL);
3914 /* due to DNE we may need to remove an agent inode */
3915 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3916 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
3917 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3919 inode = osd_dt_obj(dt)->oo_inode;
3923 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3924 0, oh, osd_dt_obj(dt), true, NULL, false);
3928 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3931 struct osd_fid_pack *rec;
3934 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3935 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3936 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3937 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
3943 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3944 const struct lu_fid *fid)
3946 struct seq_server_site *ss = osd_seq_site(osd);
3949 /* FID seqs not in FLDB, must be local seq */
3950 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3953 /* If FLD is not being initialized yet, it only happens during the
3954 * initialization, likely during mgs initialization, and we assume
3955 * this is local FID. */
3956 if (ss == NULL || ss->ss_server_fld == NULL)
3959 /* Only check the local FLDB here */
3960 if (osd_seq_exists(env, osd, fid_seq(fid)))
3967 * Index delete function for interoperability mode (b11826).
3968 * It will remove the directory entry added by osd_index_ea_insert().
3969 * This entry is needed to maintain name->fid mapping.
3971 * \param key, key i.e. file entry to be deleted
3973 * \retval 0, on success
3974 * \retval -ve, on error
3976 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3977 const struct dt_key *key, struct thandle *handle)
3979 struct osd_object *obj = osd_dt_obj(dt);
3980 struct inode *dir = obj->oo_inode;
3981 struct dentry *dentry;
3982 struct osd_thandle *oh;
3983 struct ldiskfs_dir_entry_2 *de = NULL;
3984 struct buffer_head *bh;
3985 struct htree_lock *hlock = NULL;
3986 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3987 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3991 if (!dt_object_exists(dt))
3994 LINVRNT(osd_invariant(obj));
3995 LASSERT(!dt_object_remote(dt));
3996 LASSERT(handle != NULL);
3998 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4000 oh = container_of(handle, struct osd_thandle, ot_super);
4001 LASSERT(oh->ot_handle != NULL);
4002 LASSERT(oh->ot_handle->h_transaction != NULL);
4004 ll_vfs_dq_init(dir);
4005 dentry = osd_child_dentry_get(env, obj,
4006 (char *)key, strlen((char *)key));
4008 if (obj->oo_hl_head != NULL) {
4009 hlock = osd_oti_get(env)->oti_hlock;
4010 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4011 dir, LDISKFS_HLOCK_DEL);
4013 down_write(&obj->oo_ext_idx_sem);
4016 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4018 /* If this is not the ".." entry, it might be a remote DNE
4019 * entry and we need to check if the FID is for a remote
4020 * MDT. If the FID is not in the directory entry (e.g.
4021 * upgraded 1.8 filesystem without dirdata enabled) then
4022 * we need to get the FID from the LMA. For a remote directory
4023 * there HAS to be an LMA, it cannot be an IGIF inode in this
4026 * Delete the entry before the agent inode in order to
4027 * simplify error handling. At worst an error after deleting
4028 * the entry first might leak the agent inode afterward. The
4029 * reverse would need filesystem abort in case of error deleting
4030 * the entry after the agent had been removed, or leave a
4031 * dangling entry pointing at a random inode. */
4032 if (strcmp((char *)key, dotdot) != 0) {
4033 LASSERT(de != NULL);
4034 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4035 if (rc == -ENODATA) {
4036 /* can't get FID, postpone to the end of the
4037 * transaction when iget() is safe */
4038 osd_schedule_agent_inode_removal(env, oh,
4039 le32_to_cpu(de->inode));
4040 } else if (rc == 0 &&
4041 unlikely(osd_remote_fid(env, osd, fid))) {
4042 osd_schedule_agent_inode_removal(env, oh,
4043 le32_to_cpu(de->inode));
4046 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4052 ldiskfs_htree_unlock(hlock);
4054 up_write(&obj->oo_ext_idx_sem);
4059 /* For inode on the remote MDT, .. will point to
4060 * /Agent directory, Check whether it needs to delete
4061 * from agent directory */
4062 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4063 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
4065 if (rc != 0 && rc != -ENOENT) {
4066 CERROR("%s: delete agent inode "DFID": rc = %d\n",
4067 osd_name(osd), PFID(fid), rc);
4077 LASSERT(osd_invariant(obj));
4078 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4083 * Lookup index for \a key and copy record to \a rec.
4085 * \param dt osd index object
4086 * \param key key for index
4087 * \param rec record reference
4089 * \retval +ve success : exact mach
4090 * \retval 0 return record with key not greater than \a key
4091 * \retval -ve failure
4093 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4094 struct dt_rec *rec, const struct dt_key *key)
4096 struct osd_object *obj = osd_dt_obj(dt);
4097 struct iam_path_descr *ipd;
4098 struct iam_container *bag = &obj->oo_dir->od_container;
4099 struct osd_thread_info *oti = osd_oti_get(env);
4100 struct iam_iterator *it = &oti->oti_idx_it;
4101 struct iam_rec *iam_rec;
4105 if (!dt_object_exists(dt))
4108 LASSERT(osd_invariant(obj));
4109 LASSERT(!dt_object_remote(dt));
4110 LASSERT(bag->ic_object == obj->oo_inode);
4112 ipd = osd_idx_ipd_get(env, bag);
4116 /* got ipd now we can start iterator. */
4117 iam_it_init(it, bag, 0, ipd);
4119 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4120 /* swab quota uid/gid provided by caller */
4121 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4122 key = (const struct dt_key *)&oti->oti_quota_id;
4125 rc = iam_it_get(it, (struct iam_key *)key);
4127 if (S_ISDIR(obj->oo_inode->i_mode))
4128 iam_rec = (struct iam_rec *)oti->oti_ldp;
4130 iam_rec = (struct iam_rec *) rec;
4132 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4134 if (S_ISDIR(obj->oo_inode->i_mode))
4135 osd_fid_unpack((struct lu_fid *) rec,
4136 (struct osd_fid_pack *)iam_rec);
4137 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4138 osd_quota_unpack(obj, rec);
4143 osd_ipd_put(env, bag, ipd);
4145 LINVRNT(osd_invariant(obj));
4150 static int osd_index_declare_iam_insert(const struct lu_env *env,
4151 struct dt_object *dt,
4152 const struct dt_rec *rec,
4153 const struct dt_key *key,
4154 struct thandle *handle)
4156 struct osd_thandle *oh;
4158 LASSERT(handle != NULL);
4160 oh = container_of0(handle, struct osd_thandle, ot_super);
4161 LASSERT(oh->ot_handle == NULL);
4163 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4164 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4170 * Inserts (key, value) pair in \a dt index object.
4172 * \param dt osd index object
4173 * \param key key for index
4174 * \param rec record reference
4175 * \param th transaction handler
4178 * \retval -ve failure
4180 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4181 const struct dt_rec *rec,
4182 const struct dt_key *key, struct thandle *th,
4185 struct osd_object *obj = osd_dt_obj(dt);
4186 struct iam_path_descr *ipd;
4187 struct osd_thandle *oh;
4188 struct iam_container *bag;
4189 struct osd_thread_info *oti = osd_oti_get(env);
4190 struct iam_rec *iam_rec;
4194 if (!dt_object_exists(dt))
4197 LINVRNT(osd_invariant(obj));
4198 LASSERT(!dt_object_remote(dt));
4200 bag = &obj->oo_dir->od_container;
4201 LASSERT(bag->ic_object == obj->oo_inode);
4202 LASSERT(th != NULL);
4204 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4206 ipd = osd_idx_ipd_get(env, bag);
4207 if (unlikely(ipd == NULL))
4210 oh = container_of0(th, struct osd_thandle, ot_super);
4211 LASSERT(oh->ot_handle != NULL);
4212 LASSERT(oh->ot_handle->h_transaction != NULL);
4213 if (S_ISDIR(obj->oo_inode->i_mode)) {
4214 iam_rec = (struct iam_rec *)oti->oti_ldp;
4215 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4216 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4217 /* pack quota uid/gid */
4218 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4219 key = (const struct dt_key *)&oti->oti_quota_id;
4220 /* pack quota record */
4221 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4222 iam_rec = (struct iam_rec *)rec;
4224 iam_rec = (struct iam_rec *)rec;
4227 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4229 osd_ipd_put(env, bag, ipd);
4230 LINVRNT(osd_invariant(obj));
4231 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4236 * Calls ldiskfs_add_entry() to add directory entry
4237 * into the directory. This is required for
4238 * interoperability mode (b11826)
4240 * \retval 0, on success
4241 * \retval -ve, on error
4243 static int __osd_ea_add_rec(struct osd_thread_info *info,
4244 struct osd_object *pobj, struct inode *cinode,
4245 const char *name, const struct lu_fid *fid,
4246 struct htree_lock *hlock, struct thandle *th)
4248 struct ldiskfs_dentry_param *ldp;
4249 struct dentry *child;
4250 struct osd_thandle *oth;
4253 oth = container_of(th, struct osd_thandle, ot_super);
4254 LASSERT(oth->ot_handle != NULL);
4255 LASSERT(oth->ot_handle->h_transaction != NULL);
4256 LASSERT(pobj->oo_inode);
4258 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4259 if (unlikely(pobj->oo_inode ==
4260 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4263 osd_get_ldiskfs_dirent_param(ldp, fid);
4264 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4265 child->d_fsdata = (void *)ldp;
4266 ll_vfs_dq_init(pobj->oo_inode);
4267 rc = osd_ldiskfs_add_entry(info, oth->ot_handle, child,
4269 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4270 struct ldiskfs_dir_entry_2 *de;
4271 struct buffer_head *bh;
4274 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4277 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4280 if (S_ISDIR(cinode->i_mode))
4281 de->file_type = LDISKFS_DIRENT_LUFID |
4282 LDISKFS_FT_REG_FILE;
4284 de->file_type = LDISKFS_DIRENT_LUFID |
4286 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4297 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4298 * into the directory.Also sets flags into osd object to
4299 * indicate dot and dotdot are created. This is required for
4300 * interoperability mode (b11826)
4302 * \param dir directory for dot and dotdot fixup.
4303 * \param obj child object for linking
4305 * \retval 0, on success
4306 * \retval -ve, on error
4308 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4309 struct osd_object *dir,
4310 struct inode *parent_dir, const char *name,
4311 const struct lu_fid *dot_fid,
4312 const struct lu_fid *dot_dot_fid,
4315 struct inode *inode = dir->oo_inode;
4316 struct osd_thandle *oth;
4319 oth = container_of(th, struct osd_thandle, ot_super);
4320 LASSERT(oth->ot_handle->h_transaction != NULL);
4321 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4323 if (strcmp(name, dot) == 0) {
4324 if (dir->oo_compat_dot_created) {
4327 LASSERT(inode->i_ino == parent_dir->i_ino);
4328 dir->oo_compat_dot_created = 1;
4331 } else if (strcmp(name, dotdot) == 0) {
4332 if (!dir->oo_compat_dot_created)
4334 /* in case of rename, dotdot is already created */
4335 if (dir->oo_compat_dotdot_created) {
4336 return __osd_ea_add_rec(info, dir, parent_dir, name,
4337 dot_dot_fid, NULL, th);
4340 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4341 struct lu_fid tfid = *dot_dot_fid;
4344 result = osd_add_dot_dotdot_internal(info,
4345 dir->oo_inode, parent_dir, dot_fid,
4348 result = osd_add_dot_dotdot_internal(info,
4349 dir->oo_inode, parent_dir, dot_fid,
4354 dir->oo_compat_dotdot_created = 1;
4362 * It will call the appropriate osd_add* function and return the
4363 * value, return by respective functions.
4365 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4366 struct inode *cinode, const char *name,
4367 const struct lu_fid *fid, struct thandle *th)
4369 struct osd_thread_info *info = osd_oti_get(env);
4370 struct htree_lock *hlock;
4373 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4375 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4377 if (hlock != NULL) {
4378 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4381 down_write(&pobj->oo_ext_idx_sem);
4384 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4385 lu_object_fid(&pobj->oo_dt.do_lu),
4388 if (hlock != NULL) {
4389 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4390 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4392 down_write(&pobj->oo_ext_idx_sem);
4395 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4396 struct lu_fid *tfid = &info->oti_fid;
4400 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4403 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4408 ldiskfs_htree_unlock(hlock);
4410 up_write(&pobj->oo_ext_idx_sem);
4416 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4417 struct osd_idmap_cache *oic)
4419 struct osd_scrub *scrub = &dev->od_scrub;
4420 struct lu_fid *fid = &oic->oic_fid;
4421 struct osd_inode_id *id = &oti->oti_id;
4426 if (!fid_is_norm(fid) && !fid_is_igif(fid))
4429 if (scrub->os_pos_current > id->oii_ino)
4433 rc = osd_oi_lookup(oti, dev, fid, id, 0);
4434 if (rc == -ENOENT) {
4435 struct inode *inode;
4438 inode = osd_iget(oti, dev, &oic->oic_lid);
4440 /* The inode has been removed (by race maybe). */
4441 if (IS_ERR(inode)) {
4442 rc = PTR_ERR(inode);
4444 RETURN(rc == -ESTALE ? -ENOENT : rc);
4448 /* The OI mapping is lost. */
4449 if (id->oii_gen != OSD_OII_NOGEN)
4452 /* The inode may has been reused by others, we do not know,
4453 * leave it to be handled by subsequent osd_fid_lookup(). */
4455 } else if (rc != 0 || osd_id_eq(id, &oic->oic_lid)) {
4460 if (thread_is_running(&scrub->os_thread)) {
4461 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
4462 /* There is race condition between osd_oi_lookup and OI scrub.
4463 * The OI scrub finished just after osd_oi_lookup() failure.
4464 * Under such case, it is unnecessary to trigger OI scrub again,
4465 * but try to call osd_oi_lookup() again. */
4466 if (unlikely(rc == -EAGAIN))
4472 if (!dev->od_noscrub && ++once == 1) {
4473 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4475 CDEBUG(D_LFSCK | D_CONSOLE, "%.16s: trigger OI scrub by RPC "
4476 "for "DFID", rc = %d [2]\n",
4477 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4479 if (rc == 0 || rc == -EALREADY)
4486 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4487 struct osd_device *dev,
4488 struct osd_idmap_cache *oic,
4489 struct lu_fid *fid, __u32 ino)
4491 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
4492 struct inode *inode;
4495 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4496 inode = osd_iget(oti, dev, &oic->oic_lid);
4497 if (IS_ERR(inode)) {
4498 fid_zero(&oic->oic_fid);
4499 return PTR_ERR(inode);
4502 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4505 fid_zero(&oic->oic_fid);
4507 *fid = oic->oic_fid = lma->lma_self_fid;
4511 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4512 struct osd_inode_id *id, const struct lu_fid *fid)
4514 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4515 id->oii_ino, id->oii_gen, info);
4516 info->oti_cache.oic_lid = *id;
4517 info->oti_cache.oic_fid = *fid;
4518 info->oti_cache.oic_dev = osd;
4522 * Get parent FID from the linkEA.
4524 * For a directory which parent resides on remote MDT, to satisfy the
4525 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
4526 * the other hand, to make the lookup(..) on the directory can return
4527 * the real parent FID, we append the real parent FID after its ".."
4528 * name entry in the /REMOTE_PARENT_DIR.
4530 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4531 * backup. So after the restore, we cannot get the right parent FID from
4532 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4533 * we have stored the real parent FID in the directory object's linkEA,
4534 * we can parse the linkEA for the real parent FID.
4536 * \param[in] env pointer to the thread context
4537 * \param[in] obj pointer to the object to be handled
4538 * \param[out]fid pointer to the buffer to hold the parent FID
4540 * \retval 0 for getting the real parent FID successfully
4541 * \retval negative error number on failure
4543 static int osd_get_pfid_from_linkea(const struct lu_env *env,
4544 struct osd_object *obj,
4547 struct osd_thread_info *oti = osd_oti_get(env);
4548 struct lu_buf *buf = &oti->oti_big_buf;
4549 struct dentry *dentry = &oti->oti_obj_dentry;
4550 struct inode *inode = obj->oo_inode;
4551 struct linkea_data ldata = { NULL };
4556 if (!S_ISDIR(inode->i_mode))
4560 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4561 buf->lb_buf, buf->lb_len);
4562 if (rc == -ERANGE) {
4563 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4566 lu_buf_realloc(buf, rc);
4567 if (buf->lb_buf == NULL)
4574 if (unlikely(rc == 0))
4580 if (unlikely(buf->lb_buf == NULL)) {
4581 lu_buf_realloc(buf, rc);
4582 if (buf->lb_buf == NULL)
4589 rc = linkea_init(&ldata);
4591 linkea_first_entry(&ldata);
4592 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
4599 * Calls ->lookup() to find dentry. From dentry get inode and
4600 * read inode's ea to get fid. This is required for interoperability
4603 * \retval 0, on success
4604 * \retval -ve, on error
4606 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
4607 struct dt_rec *rec, const struct dt_key *key)
4609 struct inode *dir = obj->oo_inode;
4610 struct dentry *dentry;
4611 struct ldiskfs_dir_entry_2 *de;
4612 struct buffer_head *bh;
4613 struct lu_fid *fid = (struct lu_fid *) rec;
4614 struct htree_lock *hlock = NULL;
4619 LASSERT(dir->i_op != NULL);
4620 LASSERT(dir->i_op->lookup != NULL);
4622 dentry = osd_child_dentry_get(env, obj,
4623 (char *)key, strlen((char *)key));
4625 if (obj->oo_hl_head != NULL) {
4626 hlock = osd_oti_get(env)->oti_hlock;
4627 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4628 dir, LDISKFS_HLOCK_LOOKUP);
4630 down_read(&obj->oo_ext_idx_sem);
4633 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4635 struct osd_thread_info *oti = osd_oti_get(env);
4636 struct osd_inode_id *id = &oti->oti_id;
4637 struct osd_idmap_cache *oic = &oti->oti_cache;
4638 struct osd_device *dev = osd_obj2dev(obj);
4640 ino = le32_to_cpu(de->inode);
4641 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
4643 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
4647 rc = osd_get_fid_from_dentry(de, rec);
4649 /* done with de, release bh */
4652 if (unlikely(ino == osd_remote_parent_ino(dev))) {
4653 const char *name = (const char *)key;
4655 /* If the parent is on remote MDT, and there
4656 * is no FID-in-dirent, then we have to get
4657 * the parent FID from the linkEA. */
4658 if (likely(strlen(name) == 2 &&
4659 name[0] == '.' && name[1] == '.'))
4660 rc = osd_get_pfid_from_linkea(env, obj,
4663 rc = osd_ea_fid_get(env, obj, ino, fid, id);
4666 osd_id_gen(id, ino, OSD_OII_NOGEN);
4669 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
4670 fid_zero(&oic->oic_fid);
4675 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
4676 rc = osd_consistency_check(oti, dev, oic);
4678 fid_zero(&oic->oic_fid);
4687 ldiskfs_htree_unlock(hlock);
4689 up_read(&obj->oo_ext_idx_sem);
4694 * Put the osd object once done with it.
4696 * \param obj osd object that needs to be put
4698 static inline void osd_object_put(const struct lu_env *env,
4699 struct osd_object *obj)
4701 lu_object_put(env, &obj->oo_dt.do_lu);
4704 static int osd_index_declare_ea_insert(const struct lu_env *env,
4705 struct dt_object *dt,
4706 const struct dt_rec *rec,
4707 const struct dt_key *key,
4708 struct thandle *handle)
4710 struct osd_thandle *oh;
4711 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4712 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4713 const struct lu_fid *fid = rec1->rec_fid;
4714 int credits, rc = 0;
4715 struct osd_idmap_cache *idc;
4718 LASSERT(!dt_object_remote(dt));
4719 LASSERT(handle != NULL);
4720 LASSERT(fid != NULL);
4721 LASSERT(rec1->rec_type != 0);
4723 oh = container_of0(handle, struct osd_thandle, ot_super);
4724 LASSERT(oh->ot_handle == NULL);
4726 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
4728 /* we can't call iget() while a transactions is running
4729 * (this can lead to a deadlock), but we need to know
4730 * inum and object type. so we find this information at
4731 * declaration and cache in per-thread info */
4732 idc = osd_idc_find_or_init(env, osd, fid);
4734 RETURN(PTR_ERR(idc));
4735 if (idc->oic_remote) {
4736 /* a reference to remote inode is represented by an
4737 * agent inode which we have to create */
4738 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
4739 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4742 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
4744 if (osd_dt_obj(dt)->oo_inode != NULL) {
4745 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4747 /* We ignore block quota on meta pool (MDTs), so needn't
4748 * calculate how many blocks will be consumed by this index
4750 rc = osd_declare_inode_qid(env, i_uid_read(inode),
4751 i_gid_read(inode), 0, oh,
4752 osd_dt_obj(dt), true, NULL, false);
4759 * Index add function for interoperability mode (b11826).
4760 * It will add the directory entry.This entry is needed to
4761 * maintain name->fid mapping.
4763 * \param key it is key i.e. file entry to be inserted
4764 * \param rec it is value of given key i.e. fid
4766 * \retval 0, on success
4767 * \retval -ve, on error
4769 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4770 const struct dt_rec *rec,
4771 const struct dt_key *key, struct thandle *th,
4774 struct osd_object *obj = osd_dt_obj(dt);
4775 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4776 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4777 const struct lu_fid *fid = rec1->rec_fid;
4778 const char *name = (const char *)key;
4779 struct osd_thread_info *oti = osd_oti_get(env);
4780 struct inode *child_inode = NULL;
4781 struct osd_idmap_cache *idc;
4785 if (!dt_object_exists(dt))
4788 LASSERT(osd_invariant(obj));
4789 LASSERT(!dt_object_remote(dt));
4790 LASSERT(th != NULL);
4792 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4794 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
4796 idc = osd_idc_find(env, osd, fid);
4797 if (unlikely(idc == NULL)) {
4798 /* this dt_insert() wasn't declared properly, so
4799 * FID is missing in OI cache. we better do not
4800 * lookup FID in FLDB/OI and don't risk to deadlock,
4801 * but in some special cases (lfsck testing, etc)
4802 * it's much simpler than fixing a caller */
4803 CERROR("%s: "DFID" wasn't declared for insert\n",
4804 osd_name(osd), PFID(fid));
4806 idc = osd_idc_find_or_init(env, osd, fid);
4808 RETURN(PTR_ERR(idc));
4811 if (idc->oic_remote) {
4812 /* Insert remote entry */
4813 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4814 struct osd_mdobj_map *omm = osd->od_mdt_map;
4815 struct osd_thandle *oh;
4817 /* If parent on remote MDT, we need put this object
4819 oh = container_of(th, typeof(*oh), ot_super);
4820 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4822 CERROR("%s: add "DFID" error: rc = %d\n",
4824 PFID(lu_object_fid(&dt->do_lu)), rc);
4828 child_inode = igrab(omm->omm_remote_parent->d_inode);
4830 child_inode = osd_create_local_agent_inode(env, osd,
4831 obj, fid, rec1->rec_type & S_IFMT, th);
4832 if (IS_ERR(child_inode))
4833 RETURN(PTR_ERR(child_inode));
4836 /* Insert local entry */
4837 if (unlikely(idc->oic_lid.oii_ino == 0)) {
4838 /* for a reason OI cache wasn't filled properly */
4839 CERROR("%s: OIC for "DFID" isn't filled\n",
4840 osd_name(osd), PFID(fid));
4843 child_inode = oti->oti_inode;
4844 if (unlikely(child_inode == NULL)) {
4845 struct ldiskfs_inode_info *lii;
4849 child_inode = oti->oti_inode = &lii->vfs_inode;
4851 child_inode->i_sb = osd_sb(osd);
4852 child_inode->i_ino = idc->oic_lid.oii_ino;
4853 child_inode->i_mode = rec1->rec_type & S_IFMT;
4856 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
4858 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
4859 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
4861 if (child_inode && child_inode != oti->oti_inode)
4863 LASSERT(osd_invariant(obj));
4864 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4869 * Initialize osd Iterator for given osd index object.
4871 * \param dt osd index object
4874 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4875 struct dt_object *dt,
4878 struct osd_it_iam *it;
4879 struct osd_object *obj = osd_dt_obj(dt);
4880 struct lu_object *lo = &dt->do_lu;
4881 struct iam_path_descr *ipd;
4882 struct iam_container *bag = &obj->oo_dir->od_container;
4884 if (!dt_object_exists(dt))
4885 return ERR_PTR(-ENOENT);
4889 return ERR_PTR(-ENOMEM);
4891 ipd = osd_it_ipd_get(env, bag);
4892 if (likely(ipd != NULL)) {
4896 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4897 return (struct dt_it *)it;
4900 return ERR_PTR(-ENOMEM);
4905 * free given Iterator.
4908 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4910 struct osd_it_iam *it = (struct osd_it_iam *)di;
4911 struct osd_object *obj = it->oi_obj;
4913 iam_it_fini(&it->oi_it);
4914 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4915 lu_object_put(env, &obj->oo_dt.do_lu);
4920 * Move Iterator to record specified by \a key
4922 * \param di osd iterator
4923 * \param key key for index
4925 * \retval +ve di points to record with least key not larger than key
4926 * \retval 0 di points to exact matched key
4927 * \retval -ve failure
4930 static int osd_it_iam_get(const struct lu_env *env,
4931 struct dt_it *di, const struct dt_key *key)
4933 struct osd_thread_info *oti = osd_oti_get(env);
4934 struct osd_it_iam *it = (struct osd_it_iam *)di;
4936 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4937 /* swab quota uid/gid */
4938 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4939 key = (struct dt_key *)&oti->oti_quota_id;
4942 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4948 * \param di osd iterator
4950 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4952 struct osd_it_iam *it = (struct osd_it_iam *)di;
4954 iam_it_put(&it->oi_it);
4958 * Move iterator by one record
4960 * \param di osd iterator
4962 * \retval +1 end of container reached
4964 * \retval -ve failure
4967 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4969 struct osd_it_iam *it = (struct osd_it_iam *)di;
4971 return iam_it_next(&it->oi_it);
4975 * Return pointer to the key under iterator.
4978 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4979 const struct dt_it *di)
4981 struct osd_thread_info *oti = osd_oti_get(env);
4982 struct osd_it_iam *it = (struct osd_it_iam *)di;
4983 struct osd_object *obj = it->oi_obj;
4986 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4988 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4989 /* swab quota uid/gid */
4990 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4991 key = (struct dt_key *)&oti->oti_quota_id;
4998 * Return size of key under iterator (in bytes)
5001 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5003 struct osd_it_iam *it = (struct osd_it_iam *)di;
5005 return iam_it_key_size(&it->oi_it);
5009 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5011 /* check if file type is required */
5012 if (ent->lde_attrs & LUDA_TYPE) {
5013 struct luda_type *lt;
5014 int align = sizeof(*lt) - 1;
5016 len = (len + align) & ~align;
5017 lt = (struct luda_type *)(ent->lde_name + len);
5018 lt->lt_type = cpu_to_le16(DTTOIF(type));
5021 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5025 * build lu direct from backend fs dirent.
5029 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5030 char *name, __u16 namelen, __u16 type, __u32 attr)
5032 ent->lde_attrs = attr | LUDA_FID;
5033 fid_cpu_to_le(&ent->lde_fid, fid);
5035 ent->lde_hash = cpu_to_le64(offset);
5036 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5038 strncpy(ent->lde_name, name, namelen);
5039 ent->lde_name[namelen] = '\0';
5040 ent->lde_namelen = cpu_to_le16(namelen);
5042 /* append lustre attributes */
5043 osd_it_append_attrs(ent, namelen, type);
5047 * Return pointer to the record under iterator.
5049 static int osd_it_iam_rec(const struct lu_env *env,
5050 const struct dt_it *di,
5051 struct dt_rec *dtrec, __u32 attr)
5053 struct osd_it_iam *it = (struct osd_it_iam *)di;
5054 struct osd_thread_info *info = osd_oti_get(env);
5057 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5058 const struct osd_fid_pack *rec;
5059 struct lu_fid *fid = &info->oti_fid;
5060 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5066 name = (char *)iam_it_key_get(&it->oi_it);
5068 RETURN(PTR_ERR(name));
5070 namelen = iam_it_key_size(&it->oi_it);
5072 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5074 RETURN(PTR_ERR(rec));
5076 rc = osd_fid_unpack(fid, rec);
5080 hash = iam_it_store(&it->oi_it);
5082 /* IAM does not store object type in IAM index (dir) */
5083 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5085 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5086 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5087 (struct iam_rec *)dtrec);
5088 osd_quota_unpack(it->oi_obj, dtrec);
5090 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5091 (struct iam_rec *)dtrec);
5098 * Returns cookie for current Iterator position.
5100 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5102 struct osd_it_iam *it = (struct osd_it_iam *)di;
5104 return iam_it_store(&it->oi_it);
5108 * Restore iterator from cookie.
5110 * \param di osd iterator
5111 * \param hash Iterator location cookie
5113 * \retval +ve di points to record with least key not larger than key.
5114 * \retval 0 di points to exact matched key
5115 * \retval -ve failure
5118 static int osd_it_iam_load(const struct lu_env *env,
5119 const struct dt_it *di, __u64 hash)
5121 struct osd_it_iam *it = (struct osd_it_iam *)di;
5123 return iam_it_load(&it->oi_it, hash);
5126 static const struct dt_index_operations osd_index_iam_ops = {
5127 .dio_lookup = osd_index_iam_lookup,
5128 .dio_declare_insert = osd_index_declare_iam_insert,
5129 .dio_insert = osd_index_iam_insert,
5130 .dio_declare_delete = osd_index_declare_iam_delete,
5131 .dio_delete = osd_index_iam_delete,
5133 .init = osd_it_iam_init,
5134 .fini = osd_it_iam_fini,
5135 .get = osd_it_iam_get,
5136 .put = osd_it_iam_put,
5137 .next = osd_it_iam_next,
5138 .key = osd_it_iam_key,
5139 .key_size = osd_it_iam_key_size,
5140 .rec = osd_it_iam_rec,
5141 .store = osd_it_iam_store,
5142 .load = osd_it_iam_load
5148 * Creates or initializes iterator context.
5150 * \retval struct osd_it_ea, iterator structure on success
5153 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5154 struct dt_object *dt,
5157 struct osd_object *obj = osd_dt_obj(dt);
5158 struct osd_thread_info *info = osd_oti_get(env);
5159 struct osd_it_ea *oie;
5161 struct lu_object *lo = &dt->do_lu;
5162 struct dentry *obj_dentry;
5165 if (!dt_object_exists(dt) || obj->oo_destroyed)
5166 RETURN(ERR_PTR(-ENOENT));
5168 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5170 RETURN(ERR_PTR(-ENOMEM));
5171 obj_dentry = &oie->oie_dentry;
5173 obj_dentry->d_inode = obj->oo_inode;
5174 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5175 obj_dentry->d_name.hash = 0;
5177 oie->oie_rd_dirent = 0;
5178 oie->oie_it_dirent = 0;
5179 oie->oie_dirent = NULL;
5180 if (unlikely(!info->oti_it_ea_buf_used)) {
5181 oie->oie_buf = info->oti_it_ea_buf;
5182 info->oti_it_ea_buf_used = 1;
5184 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5185 if (oie->oie_buf == NULL)
5186 RETURN(ERR_PTR(-ENOMEM));
5190 file = &oie->oie_file;
5192 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5193 if (attr & LUDA_64BITHASH)
5194 file->f_mode = FMODE_64BITHASH;
5196 file->f_mode = FMODE_32BITHASH;
5197 file->f_path.dentry = obj_dentry;
5198 file->f_mapping = obj->oo_inode->i_mapping;
5199 file->f_op = obj->oo_inode->i_fop;
5200 set_file_inode(file, obj->oo_inode);
5203 RETURN((struct dt_it *) oie);
5207 * Destroy or finishes iterator context.
5209 * \param di iterator structure to be destroyed
5211 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5213 struct osd_thread_info *info = osd_oti_get(env);
5214 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5215 struct osd_object *obj = oie->oie_obj;
5216 struct inode *inode = obj->oo_inode;
5219 oie->oie_file.f_op->release(inode, &oie->oie_file);
5220 lu_object_put(env, &obj->oo_dt.do_lu);
5221 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5222 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5224 info->oti_it_ea_buf_used = 0;
5225 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5230 * It position the iterator at given key, so that next lookup continues from
5231 * that key Or it is similar to dio_it->load() but based on a key,
5232 * rather than file position.
5234 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5237 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5239 static int osd_it_ea_get(const struct lu_env *env,
5240 struct dt_it *di, const struct dt_key *key)
5242 struct osd_it_ea *it = (struct osd_it_ea *)di;
5245 LASSERT(((const char *)key)[0] == '\0');
5246 it->oie_file.f_pos = 0;
5247 it->oie_rd_dirent = 0;
5248 it->oie_it_dirent = 0;
5249 it->oie_dirent = NULL;
5257 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5261 struct osd_filldir_cbs {
5262 #ifdef HAVE_DIR_CONTEXT
5263 struct dir_context ctx;
5265 struct osd_it_ea *it;
5268 * It is called internally by ->readdir(). It fills the
5269 * iterator's in-memory data structure with required
5270 * information i.e. name, namelen, rec_size etc.
5272 * \param buf in which information to be filled in.
5273 * \param name name of the file in given dir
5275 * \retval 0 on success
5276 * \retval 1 on buffer full
5278 static int osd_ldiskfs_filldir(void *buf, const char *name, int namelen,
5279 loff_t offset, __u64 ino,
5282 struct osd_it_ea *it = ((struct osd_filldir_cbs *)buf)->it;
5283 struct osd_object *obj = it->oie_obj;
5284 struct osd_it_ea_dirent *ent = it->oie_dirent;
5285 struct lu_fid *fid = &ent->oied_fid;
5286 struct osd_fid_pack *rec;
5289 /* this should never happen */
5290 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5291 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5295 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5299 /* "." is just the object itself. */
5300 if (namelen == 1 && name[0] == '.') {
5301 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5302 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5303 rec = (struct osd_fid_pack*) (name + namelen + 1);
5304 if (osd_fid_unpack(fid, rec) != 0)
5309 d_type &= ~LDISKFS_DIRENT_LUFID;
5311 /* NOT export local root. */
5312 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5313 ino = obj->oo_inode->i_ino;
5314 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5317 ent->oied_ino = ino;
5318 ent->oied_off = offset;
5319 ent->oied_namelen = namelen;
5320 ent->oied_type = d_type;
5322 memcpy(ent->oied_name, name, namelen);
5324 it->oie_rd_dirent++;
5325 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5330 * Calls ->readdir() to load a directory entry at a time
5331 * and stored it in iterator's in-memory data structure.
5333 * \param di iterator's in memory structure
5335 * \retval 0 on success
5336 * \retval -ve on error
5337 * \retval +1 reach the end of entry
5339 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5340 const struct dt_it *di)
5342 struct osd_it_ea *it = (struct osd_it_ea *)di;
5343 struct osd_object *obj = it->oie_obj;
5344 struct inode *inode = obj->oo_inode;
5345 struct htree_lock *hlock = NULL;
5346 struct file *filp = &it->oie_file;
5348 struct osd_filldir_cbs buf = {
5349 #ifdef HAVE_DIR_CONTEXT
5350 .ctx.actor = osd_ldiskfs_filldir,
5356 it->oie_dirent = it->oie_buf;
5357 it->oie_rd_dirent = 0;
5359 if (obj->oo_hl_head != NULL) {
5360 hlock = osd_oti_get(env)->oti_hlock;
5361 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5362 inode, LDISKFS_HLOCK_READDIR);
5364 down_read(&obj->oo_ext_idx_sem);
5367 #ifdef HAVE_DIR_CONTEXT
5368 buf.ctx.pos = filp->f_pos;
5369 rc = inode->i_fop->iterate(filp, &buf.ctx);
5370 filp->f_pos = buf.ctx.pos;
5372 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5376 ldiskfs_htree_unlock(hlock);
5378 up_read(&obj->oo_ext_idx_sem);
5380 if (it->oie_rd_dirent == 0) {
5381 /*If it does not get any dirent, it means it has been reached
5382 *to the end of the dir */
5383 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5387 it->oie_dirent = it->oie_buf;
5388 it->oie_it_dirent = 1;
5395 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5396 * to load a directory entry at a time and stored it in
5397 * iterator's in-memory data structure.
5399 * \param di iterator's in memory structure
5401 * \retval +ve iterator reached to end
5402 * \retval 0 iterator not reached to end
5403 * \retval -ve on error
5405 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5407 struct osd_it_ea *it = (struct osd_it_ea *)di;
5412 if (it->oie_it_dirent < it->oie_rd_dirent) {
5414 (void *) it->oie_dirent +
5415 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5416 it->oie_dirent->oied_namelen);
5417 it->oie_it_dirent++;
5420 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5423 rc = osd_ldiskfs_it_fill(env, di);
5430 * Returns the key at current position from iterator's in memory structure.
5432 * \param di iterator's in memory structure
5434 * \retval key i.e. struct dt_key on success
5436 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5437 const struct dt_it *di)
5439 struct osd_it_ea *it = (struct osd_it_ea *)di;
5441 return (struct dt_key *)it->oie_dirent->oied_name;
5445 * Returns the key's size at current position from iterator's in memory structure.
5447 * \param di iterator's in memory structure
5449 * \retval key_size i.e. struct dt_key on success
5451 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5453 struct osd_it_ea *it = (struct osd_it_ea *)di;
5455 return it->oie_dirent->oied_namelen;
5459 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
5461 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
5462 "dot_dotdot = %d\n", dot_dotdot);
5464 if (LDISKFS_DIR_REC_LEN(de) >=
5465 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
5472 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5473 unsigned blocksize, int dot_dotdot)
5476 return osd_dot_dotdot_has_space(de, dot_dotdot);
5478 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5479 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5486 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
5487 struct dentry *dentry, const struct lu_fid *fid,
5488 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
5489 struct htree_lock *hlock, int dot_dotdot)
5491 struct inode *dir = dentry->d_parent->d_inode;
5492 struct inode *inode = dentry->d_inode;
5493 struct osd_fid_pack *rec;
5494 struct ldiskfs_dentry_param *ldp;
5495 int namelen = dentry->d_name.len;
5497 struct osd_thread_info *info = osd_oti_get(env);
5500 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
5501 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5504 /* There is enough space to hold the FID-in-dirent. */
5505 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize,
5507 rc = ldiskfs_journal_get_write_access(jh, bh);
5511 de->name[namelen] = 0;
5512 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
5513 rec->fp_len = sizeof(struct lu_fid) + 1;
5514 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
5515 de->file_type |= LDISKFS_DIRENT_LUFID;
5516 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
5521 LASSERTF(dot_dotdot == 0, "dot_dotdot = %d\n", dot_dotdot);
5523 rc = ldiskfs_delete_entry(jh, dir, de, bh);
5527 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
5528 osd_get_ldiskfs_dirent_param(ldp, fid);
5529 dentry->d_fsdata = (void *)ldp;
5530 ll_vfs_dq_init(dir);
5531 rc = osd_ldiskfs_add_entry(info, jh, dentry, inode, hlock);
5532 /* It is too bad, we cannot reinsert the name entry back.
5533 * That means we lose it! */
5535 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
5536 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
5537 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
5538 dir->i_ino, dir->i_generation, namelen,
5539 dentry->d_name.name, PFID(fid), rc);
5545 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
5546 struct osd_it_ea *it, struct lu_fid *fid,
5547 struct osd_inode_id *id, __u32 *attr)
5549 struct osd_thread_info *info = osd_oti_get(env);
5550 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
5551 struct osd_device *dev = osd_obj2dev(obj);
5552 struct super_block *sb = osd_sb(dev);
5553 const char *devname =
5554 LDISKFS_SB(sb)->s_es->s_volume_name;
5555 struct osd_it_ea_dirent *ent = it->oie_dirent;
5556 struct inode *dir = obj->oo_inode;
5557 struct htree_lock *hlock = NULL;
5558 struct buffer_head *bh = NULL;
5559 handle_t *jh = NULL;
5560 struct ldiskfs_dir_entry_2 *de;
5561 struct dentry *dentry;
5562 struct inode *inode;
5569 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5570 inode = osd_iget(info, dev, id);
5571 if (IS_ERR(inode)) {
5572 rc = PTR_ERR(inode);
5573 if (rc == -ENOENT || rc == -ESTALE) {
5574 *attr |= LUDA_UNKNOWN;
5577 CDEBUG(D_LFSCK, "%.16s: fail to iget for dirent "
5578 "check_repair, dir = %lu/%u, name = %.*s: "
5580 devname, dir->i_ino, dir->i_generation,
5581 ent->oied_namelen, ent->oied_name, rc);
5587 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
5589 rc = osd_get_lma(info, inode, dentry, lma);
5595 if (ent->oied_name[0] == '.') {
5596 if (ent->oied_namelen == 1)
5598 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
5602 /* We need to ensure that the name entry is still valid.
5603 * Because it may be removed or renamed by other already.
5605 * The unlink or rename operation will start journal before PDO lock,
5606 * so to avoid deadlock, here we need to start journal handle before
5607 * related PDO lock also. But because we do not know whether there
5608 * will be something to be repaired before PDO lock, we just start
5609 * journal without conditions.
5611 * We may need to remove the name entry firstly, then insert back.
5612 * One credit is for user quota file update.
5613 * One credit is for group quota file update.
5614 * Two credits are for dirty inode. */
5615 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
5616 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
5618 if (dev->od_dirent_journal != 0) {
5621 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
5624 CDEBUG(D_LFSCK, "%.16s: fail to start trans for dirent "
5625 "check_repair, dir = %lu/%u, credits = %d, "
5626 "name = %.*s: rc = %d\n",
5627 devname, dir->i_ino, dir->i_generation, credits,
5628 ent->oied_namelen, ent->oied_name, rc);
5630 GOTO(out_inode, rc);
5633 if (obj->oo_hl_head != NULL) {
5634 hlock = osd_oti_get(env)->oti_hlock;
5635 /* "0" means exclusive lock for the whole directory.
5636 * We need to prevent others access such name entry
5637 * during the delete + insert. Neither HLOCK_ADD nor
5638 * HLOCK_DEL cannot guarantee the atomicity. */
5639 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
5641 down_write(&obj->oo_ext_idx_sem);
5644 if (obj->oo_hl_head != NULL) {
5645 hlock = osd_oti_get(env)->oti_hlock;
5646 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
5647 LDISKFS_HLOCK_LOOKUP);
5649 down_read(&obj->oo_ext_idx_sem);
5653 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5654 /* For dot/dotdot entry, if there is not enough space to hold the
5655 * FID-in-dirent, just keep them there. It only happens when the
5656 * device upgraded from 1.8 or restored from MDT file-level backup.
5657 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
5658 * and needs to get FID from LMA when readdir, it will not affect the
5659 * performance much. */
5660 if ((bh == NULL) || (le32_to_cpu(de->inode) != inode->i_ino) ||
5661 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
5662 *attr |= LUDA_IGNORE;
5668 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
5669 struct lu_fid *tfid = &lma->lma_self_fid;
5671 *attr |= LUDA_IGNORE;
5672 /* It must be REMOTE_PARENT_DIR and as the
5673 * dotdot entry of remote directory */
5674 if (unlikely(dot_dotdot != 2 ||
5675 fid_seq(tfid) != FID_SEQ_LOCAL_FILE ||
5676 fid_oid(tfid) != REMOTE_PARENT_DIR_OID)) {
5677 CDEBUG(D_LFSCK, "%.16s: expect remote agent "
5678 "parent directory, but got %.*s under "
5679 "dir = %lu/%u with the FID "DFID"\n",
5680 devname, ent->oied_namelen,
5681 ent->oied_name, dir->i_ino,
5682 dir->i_generation, PFID(tfid));
5684 GOTO(out, rc = -EIO);
5690 if (fid_is_sane(fid)) {
5691 /* FID-in-dirent is valid. */
5692 if (lu_fid_eq(fid, &lma->lma_self_fid))
5695 /* Do not repair under dryrun mode. */
5696 if (*attr & LUDA_VERIFY_DRYRUN) {
5697 *attr |= LUDA_REPAIR;
5704 dev->od_dirent_journal = 1;
5705 if (hlock != NULL) {
5706 ldiskfs_htree_unlock(hlock);
5709 up_read(&obj->oo_ext_idx_sem);
5715 *fid = lma->lma_self_fid;
5717 /* Update the FID-in-dirent. */
5718 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5721 *attr |= LUDA_REPAIR;
5723 CDEBUG(D_LFSCK, "%.16s: fail to update FID "
5724 "in the dirent, dir = %lu/%u, "
5725 "name = %.*s, "DFID": rc = %d\n",
5726 devname, dir->i_ino, dir->i_generation,
5727 ent->oied_namelen, ent->oied_name,
5730 /* Do not repair under dryrun mode. */
5731 if (*attr & LUDA_VERIFY_DRYRUN) {
5732 *fid = lma->lma_self_fid;
5733 *attr |= LUDA_REPAIR;
5740 dev->od_dirent_journal = 1;
5741 if (hlock != NULL) {
5742 ldiskfs_htree_unlock(hlock);
5745 up_read(&obj->oo_ext_idx_sem);
5751 *fid = lma->lma_self_fid;
5753 /* Append the FID-in-dirent. */
5754 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5757 *attr |= LUDA_REPAIR;
5759 CDEBUG(D_LFSCK, "%.16s: fail to append FID "
5760 "after the dirent, dir = %lu/%u, "
5761 "name = %.*s, "DFID": rc = %d\n",
5762 devname, dir->i_ino, dir->i_generation,
5763 ent->oied_namelen, ent->oied_name,
5767 /* Do not repair under dryrun mode. */
5768 if (*attr & LUDA_VERIFY_DRYRUN) {
5769 if (fid_is_sane(fid)) {
5770 *attr |= LUDA_REPAIR;
5772 lu_igif_build(fid, inode->i_ino,
5773 inode->i_generation);
5774 *attr |= LUDA_UPGRADE;
5782 dev->od_dirent_journal = 1;
5783 if (hlock != NULL) {
5784 ldiskfs_htree_unlock(hlock);
5787 up_read(&obj->oo_ext_idx_sem);
5794 if (unlikely(fid_is_sane(fid))) {
5795 /* FID-in-dirent exists, but FID-in-LMA is lost.
5796 * Trust the FID-in-dirent, and add FID-in-LMA. */
5797 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5799 *attr |= LUDA_REPAIR;
5801 CDEBUG(D_LFSCK, "%.16s: fail to set LMA for "
5802 "update dirent, dir = %lu/%u, "
5803 "name = %.*s, "DFID": rc = %d\n",
5804 devname, dir->i_ino, dir->i_generation,
5805 ent->oied_namelen, ent->oied_name,
5808 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5809 /* It is probably IGIF object. Only aappend the
5810 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5811 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5814 *attr |= LUDA_UPGRADE;
5816 CDEBUG(D_LFSCK, "%.16s: fail to append IGIF "
5817 "after the dirent, dir = %lu/%u, "
5818 "name = %.*s, "DFID": rc = %d\n",
5819 devname, dir->i_ino, dir->i_generation,
5820 ent->oied_namelen, ent->oied_name,
5829 if (hlock != NULL) {
5830 ldiskfs_htree_unlock(hlock);
5832 if (dev->od_dirent_journal != 0)
5833 up_write(&obj->oo_ext_idx_sem);
5835 up_read(&obj->oo_ext_idx_sem);
5839 ldiskfs_journal_stop(jh);
5843 if (rc >= 0 && !dirty)
5844 dev->od_dirent_journal = 0;
5850 * Returns the value at current position from iterator's in memory structure.
5852 * \param di struct osd_it_ea, iterator's in memory structure
5853 * \param attr attr requested for dirent.
5854 * \param lde lustre dirent
5856 * \retval 0 no error and \param lde has correct lustre dirent.
5857 * \retval -ve on error
5859 static inline int osd_it_ea_rec(const struct lu_env *env,
5860 const struct dt_it *di,
5861 struct dt_rec *dtrec, __u32 attr)
5863 struct osd_it_ea *it = (struct osd_it_ea *)di;
5864 struct osd_object *obj = it->oie_obj;
5865 struct osd_device *dev = osd_obj2dev(obj);
5866 struct osd_thread_info *oti = osd_oti_get(env);
5867 struct osd_inode_id *id = &oti->oti_id;
5868 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5869 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5870 __u32 ino = it->oie_dirent->oied_ino;
5874 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
5876 if (attr & LUDA_VERIFY) {
5877 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5878 attr |= LUDA_IGNORE;
5879 /* If the parent is on remote MDT, and there
5880 * is no FID-in-dirent, then we have to get
5881 * the parent FID from the linkEA. */
5882 if (!fid_is_sane(fid) &&
5883 it->oie_dirent->oied_namelen == 2 &&
5884 it->oie_dirent->oied_name[0] == '.' &&
5885 it->oie_dirent->oied_name[1] == '.')
5886 osd_get_pfid_from_linkea(env, obj, fid);
5888 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5892 if (!fid_is_sane(fid)) {
5893 attr &= ~LUDA_IGNORE;
5894 attr |= LUDA_UNKNOWN;
5897 attr &= ~LU_DIRENT_ATTRS_MASK;
5898 if (!fid_is_sane(fid)) {
5899 bool is_dotdot = false;
5900 if (it->oie_dirent->oied_namelen == 2 &&
5901 it->oie_dirent->oied_name[0] == '.' &&
5902 it->oie_dirent->oied_name[1] == '.')
5904 /* If the parent is on remote MDT, and there
5905 * is no FID-in-dirent, then we have to get
5906 * the parent FID from the linkEA. */
5907 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
5908 rc = osd_get_pfid_from_linkea(env, obj, fid);
5910 if (is_dotdot == false &&
5911 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5914 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5917 osd_id_gen(id, ino, OSD_OII_NOGEN);
5921 /* Pack the entry anyway, at least the offset is right. */
5922 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5923 it->oie_dirent->oied_name,
5924 it->oie_dirent->oied_namelen,
5925 it->oie_dirent->oied_type, attr);
5930 if (osd_remote_fid(env, dev, fid))
5933 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
5934 osd_add_oi_cache(oti, dev, id, fid);
5936 RETURN(rc > 0 ? 0 : rc);
5940 * Returns the record size size at current position.
5942 * This function will return record(lu_dirent) size in bytes.
5944 * \param[in] env execution environment
5945 * \param[in] di iterator's in memory structure
5946 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
5947 * calculate the lu_dirent size.
5949 * \retval record size(in bytes & in memory) of the current lu_dirent
5952 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
5955 struct osd_it_ea *it = (struct osd_it_ea *)di;
5957 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
5961 * Returns a cookie for current position of the iterator head, so that
5962 * user can use this cookie to load/start the iterator next time.
5964 * \param di iterator's in memory structure
5966 * \retval cookie for current position, on success
5968 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5970 struct osd_it_ea *it = (struct osd_it_ea *)di;
5972 return it->oie_dirent->oied_off;
5976 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5977 * to load a directory entry at a time and stored it i inn,
5978 * in iterator's in-memory data structure.
5980 * \param di struct osd_it_ea, iterator's in memory structure
5982 * \retval +ve on success
5983 * \retval -ve on error
5985 static int osd_it_ea_load(const struct lu_env *env,
5986 const struct dt_it *di, __u64 hash)
5988 struct osd_it_ea *it = (struct osd_it_ea *)di;
5992 it->oie_file.f_pos = hash;
5994 rc = osd_ldiskfs_it_fill(env, di);
6005 * Index lookup function for interoperability mode (b11826).
6007 * \param key, key i.e. file name to be searched
6009 * \retval +ve, on success
6010 * \retval -ve, on error
6012 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6013 struct dt_rec *rec, const struct dt_key *key)
6015 struct osd_object *obj = osd_dt_obj(dt);
6020 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6021 LINVRNT(osd_invariant(obj));
6023 rc = osd_ea_lookup_rec(env, obj, rec, key);
6030 * Index and Iterator operations for interoperability
6031 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6033 static const struct dt_index_operations osd_index_ea_ops = {
6034 .dio_lookup = osd_index_ea_lookup,
6035 .dio_declare_insert = osd_index_declare_ea_insert,
6036 .dio_insert = osd_index_ea_insert,
6037 .dio_declare_delete = osd_index_declare_ea_delete,
6038 .dio_delete = osd_index_ea_delete,
6040 .init = osd_it_ea_init,
6041 .fini = osd_it_ea_fini,
6042 .get = osd_it_ea_get,
6043 .put = osd_it_ea_put,
6044 .next = osd_it_ea_next,
6045 .key = osd_it_ea_key,
6046 .key_size = osd_it_ea_key_size,
6047 .rec = osd_it_ea_rec,
6048 .rec_size = osd_it_ea_rec_size,
6049 .store = osd_it_ea_store,
6050 .load = osd_it_ea_load
6054 static void *osd_key_init(const struct lu_context *ctx,
6055 struct lu_context_key *key)
6057 struct osd_thread_info *info;
6059 OBD_ALLOC_PTR(info);
6061 return ERR_PTR(-ENOMEM);
6063 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6064 if (info->oti_it_ea_buf == NULL)
6067 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6069 info->oti_hlock = ldiskfs_htree_lock_alloc();
6070 if (info->oti_hlock == NULL)
6076 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6079 return ERR_PTR(-ENOMEM);
6082 static void osd_key_fini(const struct lu_context *ctx,
6083 struct lu_context_key *key, void* data)
6085 struct osd_thread_info *info = data;
6086 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6087 struct osd_idmap_cache *idc = info->oti_ins_cache;
6089 if (info->oti_inode != NULL)
6091 if (info->oti_hlock != NULL)
6092 ldiskfs_htree_lock_free(info->oti_hlock);
6093 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6094 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6095 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6096 lu_buf_free(&info->oti_big_buf);
6098 LASSERT(info->oti_ins_cache_size > 0);
6099 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6100 info->oti_ins_cache = NULL;
6101 info->oti_ins_cache_size = 0;
6106 static void osd_key_exit(const struct lu_context *ctx,
6107 struct lu_context_key *key, void *data)
6109 struct osd_thread_info *info = data;
6111 LASSERT(info->oti_r_locks == 0);
6112 LASSERT(info->oti_w_locks == 0);
6113 LASSERT(info->oti_txns == 0);
6116 /* type constructor/destructor: osd_type_init, osd_type_fini */
6117 LU_TYPE_INIT_FINI(osd, &osd_key);
6119 struct lu_context_key osd_key = {
6120 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6121 .lct_init = osd_key_init,
6122 .lct_fini = osd_key_fini,
6123 .lct_exit = osd_key_exit
6127 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6128 const char *name, struct lu_device *next)
6130 struct osd_device *osd = osd_dev(d);
6132 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6133 >= sizeof(osd->od_svname))
6135 return osd_procfs_init(osd, name);
6138 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6140 struct seq_server_site *ss = osd_seq_site(osd);
6144 if (osd->od_is_ost || osd->od_cl_seq != NULL)
6147 if (unlikely(ss == NULL))
6150 OBD_ALLOC_PTR(osd->od_cl_seq);
6151 if (osd->od_cl_seq == NULL)
6154 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6155 osd->od_svname, ss->ss_server_seq);
6157 OBD_FREE_PTR(osd->od_cl_seq);
6158 osd->od_cl_seq = NULL;
6162 if (ss->ss_node_id == 0) {
6163 /* If the OSD on the sequence controller(MDT0), then allocate
6164 * sequence here, otherwise allocate sequence after connected
6165 * to MDT0 (see mdt_register_lwp_callback()). */
6166 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6167 &osd->od_cl_seq->lcs_space, env);
6173 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6175 if (osd->od_cl_seq == NULL)
6178 seq_client_fini(osd->od_cl_seq);
6179 OBD_FREE_PTR(osd->od_cl_seq);
6180 osd->od_cl_seq = NULL;
6183 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6187 /* shutdown quota slave instance associated with the device */
6188 if (o->od_quota_slave != NULL) {
6189 qsd_fini(env, o->od_quota_slave);
6190 o->od_quota_slave = NULL;
6193 osd_fid_fini(env, o);
6198 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6202 if (o->od_mnt != NULL) {
6203 shrink_dcache_sb(osd_sb(o));
6204 osd_sync(env, &o->od_dt_dev);
6213 static int osd_mount(const struct lu_env *env,
6214 struct osd_device *o, struct lustre_cfg *cfg)
6216 const char *name = lustre_cfg_string(cfg, 0);
6217 const char *dev = lustre_cfg_string(cfg, 1);
6219 unsigned long page, s_flags, lmd_flags = 0;
6220 struct page *__page;
6221 struct file_system_type *type;
6222 char *options = NULL;
6224 struct osd_thread_info *info = osd_oti_get(env);
6225 struct lu_fid *fid = &info->oti_fid;
6226 struct inode *inode;
6227 int rc = 0, force_over_128tb = 0;
6230 if (o->od_mnt != NULL)
6233 if (strlen(dev) >= sizeof(o->od_mntdev))
6235 strcpy(o->od_mntdev, dev);
6237 str = lustre_cfg_string(cfg, 2);
6238 s_flags = simple_strtoul(str, NULL, 0);
6239 str = strstr(str, ":");
6241 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6242 opts = lustre_cfg_string(cfg, 3);
6244 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6245 CERROR("%s: device %s extents feature is not guaranteed to "
6246 "work on big-endian systems. Use \"bigendian_extents\" "
6247 "mount option to override.\n", name, dev);
6251 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL)
6252 force_over_128tb = 1;
6254 __page = alloc_page(GFP_IOFS);
6256 GOTO(out, rc = -ENOMEM);
6257 page = (unsigned long)page_address(__page);
6258 options = (char *)page;
6261 /* strip out the options for back compatiblity */
6262 static char *sout[] = {
6269 /* strip out option we processed in osd */
6270 "bigendian_extents",
6274 strcat(options, opts);
6275 for (rc = 0, str = options; sout[rc]; ) {
6276 char *op = strstr(str, sout[rc]);
6282 if (op == options || *(op - 1) == ',') {
6283 str = op + strlen(sout[rc]);
6284 if (*str == ',' || *str == '\0') {
6285 *str == ',' ? str++ : str;
6286 memmove(op, str, strlen(str) + 1);
6289 for (str = op; *str != ',' && *str != '\0'; str++)
6293 strncat(options, "user_xattr,acl", 14);
6296 /* Glom up mount options */
6297 if (*options != '\0')
6298 strcat(options, ",");
6299 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
6301 type = get_fs_type("ldiskfs");
6303 CERROR("%s: cannot find ldiskfs module\n", name);
6304 GOTO(out, rc = -ENODEV);
6307 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6308 module_put(type->owner);
6310 if (IS_ERR(o->od_mnt)) {
6311 rc = PTR_ERR(o->od_mnt);
6313 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6317 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) > (8ULL << 32) &&
6318 force_over_128tb == 0) {
6319 CERROR("%s: device %s LDISKFS does not support filesystems "
6320 "greater than 128TB and can cause data corruption. "
6321 "Use \"force_over_128tb\" mount option to override.\n",
6323 GOTO(out, rc = -EINVAL);
6326 #ifdef HAVE_DEV_SET_RDONLY
6327 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
6328 CERROR("%s: underlying device %s is marked as read-only. "
6329 "Setup failed\n", name, dev);
6330 GOTO(out_mnt, rc = -EROFS);
6334 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6335 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6336 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6337 GOTO(out_mnt, rc = -EINVAL);
6340 #ifdef LDISKFS_MOUNT_DIRDATA
6341 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6342 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6343 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6344 else if (!o->od_is_ost)
6345 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6346 "enabling the dirdata feature. If you do not want to "
6347 "downgrade to Lustre-1.x again, you can enable it via "
6348 "'tune2fs -O dirdata device'\n", name, dev);
6350 inode = osd_sb(o)->s_root->d_inode;
6351 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6352 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6354 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6358 if (lmd_flags & LMD_FLG_NOSCRUB)
6369 __free_page(__page);
6374 static struct lu_device *osd_device_fini(const struct lu_env *env,
6375 struct lu_device *d)
6377 struct osd_device *o = osd_dev(d);
6380 osd_shutdown(env, o);
6382 osd_scrub_cleanup(env, o);
6383 osd_obj_map_fini(o);
6389 static int osd_device_init0(const struct lu_env *env,
6390 struct osd_device *o,
6391 struct lustre_cfg *cfg)
6393 struct lu_device *l = osd2lu_dev(o);
6394 struct osd_thread_info *info;
6398 /* if the module was re-loaded, env can loose its keys */
6399 rc = lu_env_refill((struct lu_env *) env);
6402 info = osd_oti_get(env);
6405 l->ld_ops = &osd_lu_ops;
6406 o->od_dt_dev.dd_ops = &osd_dt_ops;
6408 spin_lock_init(&o->od_osfs_lock);
6409 mutex_init(&o->od_otable_mutex);
6410 INIT_LIST_HEAD(&o->od_orphan_list);
6412 o->od_read_cache = 1;
6413 o->od_writethrough_cache = 1;
6414 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6416 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6417 sizeof(o->od_svname));
6418 if (cplen >= sizeof(o->od_svname)) {
6423 o->od_index = -1; /* -1 means index is invalid */
6424 rc = server_name2index(o->od_svname, &o->od_index, NULL);
6425 if (rc == LDD_F_SV_TYPE_OST)
6428 o->od_full_scrub_ratio = OFSR_DEFAULT;
6429 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6430 rc = osd_mount(env, o, cfg);
6434 rc = osd_obj_map_init(env, o);
6438 rc = lu_site_init(&o->od_site, l);
6440 GOTO(out_compat, rc);
6441 o->od_site.ls_bottom_dev = l;
6443 rc = lu_site_init_finish(&o->od_site);
6447 INIT_LIST_HEAD(&o->od_ios_list);
6448 /* setup scrub, including OI files initialization */
6449 rc = osd_scrub_setup(env, o);
6453 rc = osd_procfs_init(o, o->od_svname);
6455 CERROR("%s: can't initialize procfs: rc = %d\n",
6457 GOTO(out_scrub, rc);
6460 LASSERT(l->ld_site->ls_linkage.next != NULL);
6461 LASSERT(l->ld_site->ls_linkage.prev != NULL);
6463 /* initialize quota slave instance */
6464 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
6466 if (IS_ERR(o->od_quota_slave)) {
6467 rc = PTR_ERR(o->od_quota_slave);
6468 o->od_quota_slave = NULL;
6469 GOTO(out_procfs, rc);
6477 osd_scrub_cleanup(env, o);
6479 lu_site_fini(&o->od_site);
6481 osd_obj_map_fini(o);
6488 static struct lu_device *osd_device_alloc(const struct lu_env *env,
6489 struct lu_device_type *t,
6490 struct lustre_cfg *cfg)
6492 struct osd_device *o;
6497 return ERR_PTR(-ENOMEM);
6499 rc = dt_device_init(&o->od_dt_dev, t);
6501 /* Because the ctx might be revived in dt_device_init,
6502 * refill the env here */
6503 lu_env_refill((struct lu_env *)env);
6504 rc = osd_device_init0(env, o, cfg);
6506 dt_device_fini(&o->od_dt_dev);
6509 if (unlikely(rc != 0))
6512 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
6515 static struct lu_device *osd_device_free(const struct lu_env *env,
6516 struct lu_device *d)
6518 struct osd_device *o = osd_dev(d);
6521 /* XXX: make osd top device in order to release reference */
6522 d->ld_site->ls_top_dev = d;
6523 lu_site_purge(env, d->ld_site, -1);
6524 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
6525 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
6526 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
6528 lu_site_fini(&o->od_site);
6529 dt_device_fini(&o->od_dt_dev);
6534 static int osd_process_config(const struct lu_env *env,
6535 struct lu_device *d, struct lustre_cfg *cfg)
6537 struct osd_device *o = osd_dev(d);
6541 switch (cfg->lcfg_command) {
6543 rc = osd_mount(env, o, cfg);
6546 lu_dev_del_linkage(d->ld_site, d);
6547 rc = osd_shutdown(env, o);
6550 LASSERT(&o->od_dt_dev);
6551 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
6552 cfg, &o->od_dt_dev);
6553 if (rc > 0 || rc == -ENOSYS)
6554 rc = class_process_proc_param(PARAM_OST,
6555 lprocfs_osd_obd_vars,
6556 cfg, &o->od_dt_dev);
6565 static int osd_recovery_complete(const struct lu_env *env,
6566 struct lu_device *d)
6568 struct osd_device *osd = osd_dev(d);
6572 if (osd->od_quota_slave == NULL)
6575 /* start qsd instance on recovery completion, this notifies the quota
6576 * slave code that we are about to process new requests now */
6577 rc = qsd_start(env, osd->od_quota_slave);
6582 * we use exports to track all osd users
6584 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
6585 struct obd_device *obd, struct obd_uuid *cluuid,
6586 struct obd_connect_data *data, void *localdata)
6588 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6589 struct lustre_handle conn;
6593 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
6595 rc = class_connect(&conn, obd, cluuid);
6599 *exp = class_conn2export(&conn);
6601 spin_lock(&osd->od_osfs_lock);
6603 spin_unlock(&osd->od_osfs_lock);
6609 * once last export (we don't count self-export) disappeared
6610 * osd can be released
6612 static int osd_obd_disconnect(struct obd_export *exp)
6614 struct obd_device *obd = exp->exp_obd;
6615 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6616 int rc, release = 0;
6619 /* Only disconnect the underlying layers on the final disconnect. */
6620 spin_lock(&osd->od_osfs_lock);
6622 if (osd->od_connects == 0)
6624 spin_unlock(&osd->od_osfs_lock);
6626 rc = class_disconnect(exp); /* bz 9811 */
6628 if (rc == 0 && release)
6629 class_manual_cleanup(obd);
6633 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
6634 struct lu_device *dev)
6636 struct osd_device *osd = osd_dev(dev);
6637 struct lr_server_data *lsd =
6638 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
6642 if (osd->od_quota_slave != NULL) {
6643 /* set up quota slave objects */
6644 result = qsd_prepare(env, osd->od_quota_slave);
6649 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
6650 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
6651 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
6652 osd->od_index_in_idif = 1;
6654 osd->od_index_in_idif = 0;
6655 result = osd_register_proc_index_in_idif(osd);
6660 osd->od_index_in_idif = 1;
6664 result = osd_fid_init(env, osd);
6669 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
6670 struct lu_fid *fid, struct md_op_data *op_data)
6672 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
6674 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
6677 static const struct lu_object_operations osd_lu_obj_ops = {
6678 .loo_object_init = osd_object_init,
6679 .loo_object_delete = osd_object_delete,
6680 .loo_object_release = osd_object_release,
6681 .loo_object_free = osd_object_free,
6682 .loo_object_print = osd_object_print,
6683 .loo_object_invariant = osd_object_invariant
6686 const struct lu_device_operations osd_lu_ops = {
6687 .ldo_object_alloc = osd_object_alloc,
6688 .ldo_process_config = osd_process_config,
6689 .ldo_recovery_complete = osd_recovery_complete,
6690 .ldo_prepare = osd_prepare,
6693 static const struct lu_device_type_operations osd_device_type_ops = {
6694 .ldto_init = osd_type_init,
6695 .ldto_fini = osd_type_fini,
6697 .ldto_start = osd_type_start,
6698 .ldto_stop = osd_type_stop,
6700 .ldto_device_alloc = osd_device_alloc,
6701 .ldto_device_free = osd_device_free,
6703 .ldto_device_init = osd_device_init,
6704 .ldto_device_fini = osd_device_fini
6707 static struct lu_device_type osd_device_type = {
6708 .ldt_tags = LU_DEVICE_DT,
6709 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
6710 .ldt_ops = &osd_device_type_ops,
6711 .ldt_ctx_tags = LCT_LOCAL,
6714 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
6716 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6717 struct super_block *sb = osd_sb(osd);
6719 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
6723 * lprocfs legacy support.
6725 static struct obd_ops osd_obd_device_ops = {
6726 .o_owner = THIS_MODULE,
6727 .o_connect = osd_obd_connect,
6728 .o_disconnect = osd_obd_disconnect,
6729 .o_fid_alloc = osd_fid_alloc,
6730 .o_health_check = osd_health_check,
6733 static int __init osd_init(void)
6737 LASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
6738 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
6739 /* please, try to keep osd_thread_info smaller than a page */
6740 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
6745 rc = lu_kmem_init(ldiskfs_caches);
6749 rc = class_register_type(&osd_obd_device_ops, NULL, true,
6750 lprocfs_osd_module_vars,
6751 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
6753 lu_kmem_fini(ldiskfs_caches);
6757 static void __exit osd_exit(void)
6759 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
6760 lu_kmem_fini(ldiskfs_caches);
6763 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6764 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
6765 MODULE_VERSION(LUSTRE_VERSION_STRING);
6766 MODULE_LICENSE("GPL");
6768 module_init(osd_init);
6769 module_exit(osd_exit);