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 module_param(ldiskfs_pdo, int, 0644);
87 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
89 int ldiskfs_track_declares_assert;
90 module_param(ldiskfs_track_declares_assert, int, 0644);
91 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "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);
350 INIT_LIST_HEAD(&mo->oo_xattr_list);
357 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
358 struct dentry *dentry, struct lustre_mdt_attrs *lma)
362 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
363 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
364 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
366 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
367 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
369 lustre_lma_swab(lma);
370 /* Check LMA compatibility */
371 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
372 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
373 "for fid = "DFID", ino = %lu\n",
374 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
375 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
376 PFID(&lma->lma_self_fid), inode->i_ino);
379 } else if (rc == 0) {
387 * retrieve object from backend ext fs.
389 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
390 struct osd_inode_id *id)
392 struct inode *inode = NULL;
394 /* if we look for an inode withing a running
395 * transaction, then we risk to deadlock */
396 /* osd_dirent_check_repair() breaks this */
397 /*LASSERT(current->journal_info == NULL);*/
399 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
401 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
402 id->oii_ino, PTR_ERR(inode));
403 } else if (id->oii_gen != OSD_OII_NOGEN &&
404 inode->i_generation != id->oii_gen) {
405 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
406 "i_generation = %u\n",
407 id->oii_ino, id->oii_gen, inode->i_generation);
409 inode = ERR_PTR(-ESTALE);
410 } else if (inode->i_nlink == 0) {
411 /* due to parallel readdir and unlink,
412 * we can have dead inode here. */
413 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
415 inode = ERR_PTR(-ESTALE);
416 } else if (is_bad_inode(inode)) {
417 CWARN("%.16s: bad inode: ino = %u\n",
418 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
420 inode = ERR_PTR(-ENOENT);
422 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
423 if (id->oii_gen == OSD_OII_NOGEN)
424 osd_id_gen(id, inode->i_ino, inode->i_generation);
426 /* Do not update file c/mtime in ldiskfs.
427 * NB: we don't have any lock to protect this because we don't
428 * have reference on osd_object now, but contention with
429 * another lookup + attr_set can't happen in the tiny window
430 * between if (...) and set S_NOCMTIME. */
431 if (!(inode->i_flags & S_NOCMTIME))
432 inode->i_flags |= S_NOCMTIME;
437 int osd_ldiskfs_add_entry(struct osd_thread_info *info,
438 handle_t *handle, struct dentry *child,
439 struct inode *inode, struct htree_lock *hlock)
443 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
444 if (rc == -ENOBUFS || rc == -ENOSPC) {
445 char fidbuf[FID_LEN + 1];
446 struct lustre_mdt_attrs lma;
447 struct lu_fid fid = { };
449 struct dentry *p_dentry = child->d_parent;
451 rc2 = osd_get_lma(info, p_dentry->d_inode, p_dentry,
454 fid = lma.lma_self_fid;
455 snprintf(fidbuf, sizeof(fidbuf), DFID, PFID(&fid));
456 } else if (rc2 == -ENODATA) {
457 if (unlikely(p_dentry->d_inode ==
458 inode->i_sb->s_root->d_inode))
459 lu_local_obj_fid(&fid, OSD_FS_ROOT_OID);
460 else if (info->oti_dev && !info->oti_dev->od_is_ost &&
461 fid_seq_is_mdt0(fid_seq(&fid)))
462 lu_igif_build(&fid, p_dentry->d_inode->i_ino,
463 p_dentry->d_inode->i_generation);
464 snprintf(fidbuf, sizeof(fidbuf), DFID, PFID(&fid));
466 snprintf(fidbuf, FID_LEN, "%s", "unknown");
470 errstr = "has reached";
472 errstr = "is approaching";
473 CWARN("%.16s: directory (inode: %lu FID: %s) %s maximum entry limit\n",
474 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
475 p_dentry->d_inode->i_ino, fidbuf, errstr);
476 /* ignore such error now */
484 static struct inode *
485 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
486 struct osd_inode_id *id, struct lu_fid *fid)
488 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
492 inode = osd_iget(info, dev, id);
496 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
498 *fid = lma->lma_self_fid;
499 } else if (rc == -ENODATA) {
500 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
501 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
503 lu_igif_build(fid, inode->i_ino, inode->i_generation);
511 static struct inode *osd_iget_check(struct osd_thread_info *info,
512 struct osd_device *dev,
513 const struct lu_fid *fid,
514 struct osd_inode_id *id,
521 /* The cached OI mapping is trustable. If we cannot locate the inode
522 * via the cached OI mapping, then return the failure to the caller
523 * directly without further OI checking. */
525 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
528 if (cached || (rc != -ENOENT && rc != -ESTALE)) {
529 CDEBUG(D_INODE, "no inode: ino = %u, rc = %d\n",
538 if (is_bad_inode(inode)) {
541 CDEBUG(D_INODE, "bad inode: ino = %u\n", id->oii_ino);
549 if (id->oii_gen != OSD_OII_NOGEN &&
550 inode->i_generation != id->oii_gen) {
553 CDEBUG(D_INODE, "unmatched inode: ino = %u, "
554 "oii_gen = %u, i_generation = %u\n",
555 id->oii_ino, id->oii_gen, inode->i_generation);
563 if (inode->i_nlink == 0) {
566 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
574 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
578 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
580 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
581 /* XXX: There are four possible cases:
583 * Backup/restore caused the OI invalid.
585 * Someone unlinked the object but NOT removed
586 * the OI mapping, such as mount target device
587 * as ldiskfs, and modify something directly.
589 * Someone just removed the object between the
590 * former oi_lookup and the iget. It is normal.
591 * 4. Other failure cases.
593 * Generally, when the device is mounted, it will
594 * auto check whether the system is restored from
595 * file-level backup or not. We trust such detect
596 * to distinguish the 1st case from the 2nd case. */
598 if (!IS_ERR(inode) && inode->i_generation != 0 &&
599 inode->i_generation == id->oii_gen)
600 /* "id->oii_gen != OSD_OII_NOGEN" is for
601 * "@cached == false" case. */
606 /* If the OI mapping was in OI file before the
607 * osd_iget_check(), but now, it is disappear,
608 * then it must be removed by race. That is a
609 * normal race case. */
612 if (id->oii_gen == OSD_OII_NOGEN)
613 osd_id_gen(id, inode->i_ino, inode->i_generation);
615 /* Do not update file c/mtime in ldiskfs.
616 * NB: we don't have any lock to protect this because we don't
617 * have reference on osd_object now, but contention with
618 * another lookup + attr_set can't happen in the tiny window
619 * between if (...) and set S_NOCMTIME. */
620 if (!(inode->i_flags & S_NOCMTIME))
621 inode->i_flags |= S_NOCMTIME;
638 * \retval +v: new filter_fid, does not contain self-fid
639 * \retval 0: filter_fid_old, contains self-fid
640 * \retval -v: other failure cases
642 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
643 struct dentry *dentry, struct lu_fid *fid)
645 struct filter_fid_old *ff = &info->oti_ff;
646 struct ost_id *ostid = &info->oti_ostid;
649 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
650 if (rc == sizeof(*ff)) {
652 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
653 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
654 /* XXX: use 0 as the index for compatibility, the caller will
655 * handle index related issues when necessarry. */
656 ostid_to_fid(fid, ostid, 0);
657 } else if (rc == sizeof(struct filter_fid)) {
659 } else if (rc >= 0) {
666 static int osd_lma_self_repair(struct osd_thread_info *info,
667 struct osd_device *osd, struct inode *inode,
668 const struct lu_fid *fid, __u32 compat)
673 LASSERT(current->journal_info == NULL);
675 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
676 osd_dto_credits_noquota[DTO_XATTR_SET]);
679 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
684 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
686 CWARN("%s: cannot self repair the LMA: rc = %d\n",
688 ldiskfs_journal_stop(jh);
692 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
694 struct osd_thread_info *info = osd_oti_get(env);
695 struct osd_device *osd = osd_obj2dev(obj);
696 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
697 struct inode *inode = obj->oo_inode;
698 struct dentry *dentry = &info->oti_obj_dentry;
699 struct lu_fid *fid = NULL;
700 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
704 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
705 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
706 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
707 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
708 fid = &lma->lma_self_fid;
709 rc = osd_get_idif(info, inode, dentry, fid);
710 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
711 /* For the given OST-object, if it has neither LMA nor
712 * FID in XATTR_NAME_FID, then the given FID (which is
713 * contained in the @obj, from client RPC for locating
714 * the OST-object) is trusted. We use it to generate
716 osd_lma_self_repair(info, osd, inode, rfid,
727 lustre_lma_swab(lma);
728 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
729 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
730 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
731 "fid = "DFID", ino = %lu\n", osd_name(osd),
732 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
733 PFID(rfid), inode->i_ino);
736 fid = &lma->lma_self_fid;
740 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
741 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
742 struct ost_id *oi = &info->oti_ostid;
743 struct lu_fid *fid1 = &info->oti_fid3;
744 __u32 idx = fid_idif_ost_idx(rfid);
746 /* For old IDIF, the OST index is not part of the IDIF,
747 * Means that different OSTs may have the same IDIFs.
748 * Under such case, we need to make some compatible
749 * check to make sure to trigger OI scrub properly. */
750 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
751 /* Given @rfid is new, LMA is old. */
752 fid_to_ostid(fid, oi);
753 ostid_to_fid(fid1, oi, idx);
754 if (lu_fid_eq(fid1, rfid)) {
755 if (osd->od_index_in_idif)
756 osd_lma_self_repair(info, osd,
770 struct osd_check_lmv_buf {
771 #ifdef HAVE_DIR_CONTEXT
772 /* please keep it as first member */
773 struct dir_context ctx;
775 struct osd_thread_info *oclb_info;
776 struct osd_device *oclb_dev;
777 struct osd_idmap_cache *oclb_oic;
781 * It is called internally by ->readdir() to filter out the
782 * local slave object's FID of the striped directory.
784 * \retval 1 found the local slave's FID
785 * \retval 0 continue to check next item
786 * \retval -ve for failure
788 #ifdef HAVE_FILLDIR_USE_CTX
789 static int osd_stripe_dir_filldir(struct dir_context *buf,
791 static int osd_stripe_dir_filldir(void *buf,
793 const char *name, int namelen,
794 loff_t offset, __u64 ino, unsigned d_type)
796 struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
797 struct osd_thread_info *oti = oclb->oclb_info;
798 struct lu_fid *fid = &oti->oti_fid3;
799 struct osd_inode_id *id = &oti->oti_id3;
800 struct osd_device *dev = oclb->oclb_dev;
801 struct osd_idmap_cache *oic = oclb->oclb_oic;
809 sscanf(name + 1, SFID, RFID(fid));
810 if (!fid_is_sane(fid))
813 if (osd_remote_fid(oti->oti_env, dev, fid))
816 osd_id_gen(id, ino, OSD_OII_NOGEN);
817 inode = osd_iget(oti, dev, id);
819 return PTR_ERR(inode);
822 osd_add_oi_cache(oti, dev, id, fid);
826 rc = osd_oii_insert(dev, oic, true);
828 return rc == 0 ? 1 : rc;
831 /* When lookup item under striped directory, we need to locate the master
832 * MDT-object of the striped directory firstly, then the client will send
833 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
834 * and the item's name. If the system is restored from MDT file level backup,
835 * then before the OI scrub completely built the OI files, the OI mappings of
836 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
837 * not a problem for the master MDT-object. Because when locate the master
838 * MDT-object, we will do name based lookup (for the striped directory itself)
839 * firstly, during such process we can setup the correct OI mapping for the
840 * master MDT-object. But it will be trouble for the slave MDT-object. Because
841 * the client will not trigger name based lookup on the MDT to locate the slave
842 * MDT-object before locating item under the striped directory, then when
843 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
844 * is invalid and does not know what the right OI mapping is, then the MDT has
845 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
846 * the OI file, related OI mapping is unknown yet, please try again later. And
847 * then client will re-try the RPC again and again until related OI mapping has
848 * been updated. That is quite inefficient.
850 * To resolve above trouble, we will handle it as the following two cases:
852 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
853 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
854 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
855 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
856 * directly. Please check osd_fid_lookup().
858 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
859 * Under such case, during lookup the master MDT-object, we will lookup the
860 * slave MDT-object via readdir against the master MDT-object, because the
861 * slave MDT-objects information are stored as sub-directories with the name
862 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
863 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
864 * the correct OI mapping for the slave MDT-object. */
865 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
866 struct inode *inode, struct osd_idmap_cache *oic)
868 struct lu_buf *buf = &oti->oti_big_buf;
869 struct dentry *dentry = &oti->oti_obj_dentry;
870 struct file *filp = &oti->oti_file;
871 const struct file_operations *fops;
872 struct lmv_mds_md_v1 *lmv1;
873 struct osd_check_lmv_buf oclb = {
874 #ifdef HAVE_DIR_CONTEXT
875 .ctx.actor = osd_stripe_dir_filldir,
885 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, buf->lb_buf,
888 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, NULL, 0);
890 lu_buf_realloc(buf, rc);
891 if (buf->lb_buf == NULL)
892 GOTO(out, rc = -ENOMEM);
898 if (unlikely(rc == 0 || rc == -ENODATA))
904 if (unlikely(buf->lb_buf == NULL)) {
905 lu_buf_realloc(buf, rc);
906 if (buf->lb_buf == NULL)
907 GOTO(out, rc = -ENOMEM);
913 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
917 dentry->d_inode = inode;
918 dentry->d_sb = inode->i_sb;
920 filp->f_path.dentry = dentry;
921 filp->f_mode = FMODE_64BITHASH;
922 filp->f_mapping = inode->i_mapping;
924 filp->private_data = NULL;
925 set_file_inode(filp, inode);
927 #ifdef HAVE_DIR_CONTEXT
928 oclb.ctx.pos = filp->f_pos;
929 rc = fops->iterate(filp, &oclb.ctx);
930 filp->f_pos = oclb.ctx.pos;
932 rc = fops->readdir(filp, &oclb, osd_stripe_dir_filldir);
934 fops->release(inode, filp);
937 RETURN(rc >= 0 ? 0 : rc);
940 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
941 const struct lu_fid *fid,
942 const struct lu_object_conf *conf)
944 struct osd_thread_info *info;
945 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
946 struct osd_device *dev;
947 struct osd_idmap_cache *oic;
948 struct osd_inode_id *id;
949 struct inode *inode = NULL;
950 struct osd_scrub *scrub;
951 struct scrub_file *sf;
952 __u32 flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT |
960 LINVRNT(osd_invariant(obj));
961 LASSERT(obj->oo_inode == NULL);
962 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
965 scrub = &dev->od_scrub;
966 sf = &scrub->os_file;
967 info = osd_oti_get(env);
969 oic = &info->oti_cache;
971 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
974 /* For the object is created as locking anchor, or for the object to
975 * be created on disk. No need to osd_oi_lookup() at here because FID
976 * shouldn't never be re-used, if it's really a duplicate FID from
977 * unexpected reason, we should be able to detect it later by calling
978 * do_create->osd_oi_insert(). */
979 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
980 GOTO(out, result = 0);
982 /* Search order: 1. per-thread cache. */
983 if (lu_fid_eq(fid, &oic->oic_fid) &&
984 likely(oic->oic_dev == dev)) {
990 if (!list_empty(&scrub->os_inconsistent_items)) {
991 /* Search order: 2. OI scrub pending list. */
992 result = osd_oii_lookup(dev, fid, id);
998 /* Search order: 3. OI files. */
999 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1000 if (result == -ENOENT) {
1001 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
1002 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1003 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
1005 GOTO(out, result = 0);
1014 inode = osd_iget_check(info, dev, fid, id, cached);
1015 if (IS_ERR(inode)) {
1016 result = PTR_ERR(inode);
1017 if (result == -ENOENT || result == -ESTALE)
1018 GOTO(out, result = -ENOENT);
1020 if (result == -EREMCHG) {
1023 /* We still have chance to get the valid inode: for the
1024 * object which is referenced by remote name entry, the
1025 * object on the local MDT will be linked under the dir
1026 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1028 * We do not know whether the object for the given FID
1029 * is referenced by some remote name entry or not, and
1030 * especially for DNE II, a multiple-linked object may
1031 * have many name entries reside on many MDTs.
1033 * To simplify the operation, OSD will not distinguish
1034 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1035 * only happened for the RPC from other MDT during the
1036 * OI scrub, or for the client side RPC with FID only,
1037 * such as FID to path, or from old connected client. */
1039 !fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1040 rc1 = osd_lookup_in_remote_parent(info, dev,
1045 flags |= SS_AUTO_PARTIAL;
1046 flags &= ~SS_AUTO_FULL;
1051 if (thread_is_running(&scrub->os_thread)) {
1052 if (scrub->os_partial_scan &&
1053 !scrub->os_in_join) {
1056 if (inode != NULL && !IS_ERR(inode)) {
1059 osd_add_oi_cache(info, dev, id,
1061 osd_oii_insert(dev, oic, true);
1063 result = -EINPROGRESS;
1066 } else if (!dev->od_noscrub) {
1069 rc1 = osd_scrub_start(dev, flags);
1070 LCONSOLE_WARN("%.16s: trigger OI scrub by RPC "
1071 "for the "DFID" with flags 0x%x,"
1072 " rc = %d\n", osd_name(dev),
1073 PFID(fid), flags, rc1);
1074 if (rc1 == 0 || rc1 == -EALREADY) {
1075 if (inode != NULL && !IS_ERR(inode)) {
1078 osd_add_oi_cache(info, dev, id,
1080 osd_oii_insert(dev, oic, true);
1082 result = -EINPROGRESS;
1092 if (inode == NULL || IS_ERR(inode))
1094 } else if (remote) {
1098 obj->oo_inode = inode;
1099 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
1101 result = osd_check_lma(env, obj);
1103 if (result == -ENODATA) {
1105 result = osd_oi_lookup(info, dev, fid, id,
1108 /* result == -ENOENT means that the OI
1109 * mapping has been removed by race,
1110 * the target inode belongs to other
1113 * Others error also can be returned
1116 obj->oo_inode = NULL;
1119 /* result == 0 means the cached OI
1120 * mapping is still in the OI file,
1121 * the target the inode is valid. */
1124 /* The current OI mapping is from the OI file,
1125 * since the inode has been found via
1126 * osd_iget_check(), no need recheck OI. */
1134 obj->oo_inode = NULL;
1135 if (result != -EREMCHG)
1139 result = osd_oi_lookup(info, dev, fid, id,
1141 /* result == -ENOENT means the cached OI mapping
1142 * has been removed from the OI file by race,
1143 * above target inode belongs to other object.
1145 * Others error also can be returned directly. */
1149 /* result == 0, goto trigger */
1151 /* The current OI mapping is from the OI file,
1152 * since the inode has been found via
1153 * osd_iget_check(), no need recheck OI. */
1160 obj->oo_compat_dot_created = 1;
1161 obj->oo_compat_dotdot_created = 1;
1163 if (!S_ISDIR(inode->i_mode))
1164 GOTO(out, result = 0);
1166 if (flags & SS_AUTO_PARTIAL)
1167 osd_check_lmv(info, dev, inode, oic);
1170 GOTO(out, result = 0);
1172 LASSERT(obj->oo_hl_head == NULL);
1173 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1174 if (obj->oo_hl_head == NULL) {
1175 obj->oo_inode = NULL;
1177 GOTO(out, result = -ENOMEM);
1179 GOTO(out, result = 0);
1182 if (result != 0 && cached)
1183 fid_zero(&oic->oic_fid);
1185 LINVRNT(osd_invariant(obj));
1190 * Concurrency: shouldn't matter.
1192 static void osd_object_init0(struct osd_object *obj)
1194 LASSERT(obj->oo_inode != NULL);
1195 obj->oo_dt.do_body_ops = &osd_body_ops;
1196 obj->oo_dt.do_lu.lo_header->loh_attr |=
1197 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1201 * Concurrency: no concurrent access is possible that early in object
1204 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1205 const struct lu_object_conf *conf)
1207 struct osd_object *obj = osd_obj(l);
1210 LINVRNT(osd_invariant(obj));
1212 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1213 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1214 l->lo_header->loh_attr |= LOHA_EXISTS;
1218 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1219 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1220 if (result == 0 && obj->oo_inode != NULL) {
1221 struct osd_thread_info *oti = osd_oti_get(env);
1222 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
1224 osd_object_init0(obj);
1225 result = osd_get_lma(oti, obj->oo_inode,
1226 &oti->oti_obj_dentry, lma);
1228 /* Convert LMAI flags to lustre LMA flags
1229 * and cache it to oo_lma_flags */
1231 lma_to_lustre_flags(lma->lma_incompat);
1232 } else if (result == -ENODATA) {
1237 LINVRNT(osd_invariant(obj));
1241 /* The first part of oxe_buf is xattr name, and is '\0' terminated.
1242 * The left part is for value, binary mode. */
1243 struct osd_xattr_entry {
1244 struct list_head oxe_list;
1248 struct rcu_head oxe_rcu;
1252 static struct osd_xattr_entry *osd_oxc_lookup(struct osd_object *obj,
1256 struct osd_xattr_entry *oxe;
1258 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1259 if (namelen == oxe->oxe_namelen &&
1260 strncmp(name, oxe->oxe_buf, namelen) == 0)
1267 static int osd_oxc_get(struct osd_object *obj, const char *name,
1270 struct osd_xattr_entry *oxe;
1275 oxe = osd_oxc_lookup(obj, name, strlen(name));
1281 if (!oxe->oxe_exist) {
1286 vallen = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1287 LASSERT(vallen > 0);
1289 if (buf->lb_buf == NULL) {
1294 if (buf->lb_len < vallen) {
1299 memcpy(buf->lb_buf, oxe->oxe_buf + oxe->oxe_namelen + 1, vallen);
1305 static void osd_oxc_free(struct rcu_head *head)
1307 struct osd_xattr_entry *oxe;
1309 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1310 OBD_FREE(oxe, oxe->oxe_len);
1313 static inline void __osd_oxc_del(struct osd_object *obj, const char *name)
1315 struct osd_xattr_entry *oxe;
1317 oxe = osd_oxc_lookup(obj, name, strlen(name));
1319 list_del(&oxe->oxe_list);
1320 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1324 static void osd_oxc_add(struct osd_object *obj, const char *name,
1325 const char *buf, int buflen)
1327 struct osd_xattr_entry *oxe;
1328 size_t namelen = strlen(name);
1329 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1331 OBD_ALLOC(oxe, len);
1335 INIT_LIST_HEAD(&oxe->oxe_list);
1337 oxe->oxe_namelen = namelen;
1338 memcpy(oxe->oxe_buf, name, namelen);
1340 LASSERT(buf != NULL);
1341 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1342 oxe->oxe_exist = true;
1344 oxe->oxe_exist = false;
1347 /* this should be rarely called, just remove old and add new */
1348 spin_lock(&obj->oo_guard);
1349 __osd_oxc_del(obj, name);
1350 list_add_tail(&oxe->oxe_list, &obj->oo_xattr_list);
1351 spin_unlock(&obj->oo_guard);
1354 static void osd_oxc_del(struct osd_object *obj, const char *name)
1356 spin_lock(&obj->oo_guard);
1357 __osd_oxc_del(obj, name);
1358 spin_unlock(&obj->oo_guard);
1361 static void osd_oxc_fini(struct osd_object *obj)
1363 struct osd_xattr_entry *oxe, *next;
1365 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1366 list_del(&oxe->oxe_list);
1367 OBD_FREE(oxe, oxe->oxe_len);
1372 * Concurrency: no concurrent access is possible that late in object
1375 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1377 struct osd_object *obj = osd_obj(l);
1379 LINVRNT(osd_invariant(obj));
1382 dt_object_fini(&obj->oo_dt);
1383 if (obj->oo_hl_head != NULL)
1384 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1389 * Concurrency: no concurrent access is possible that late in object
1392 static void osd_index_fini(struct osd_object *o)
1394 struct iam_container *bag;
1396 if (o->oo_dir != NULL) {
1397 bag = &o->oo_dir->od_container;
1398 if (o->oo_inode != NULL) {
1399 if (bag->ic_object == o->oo_inode)
1400 iam_container_fini(bag);
1402 OBD_FREE_PTR(o->oo_dir);
1408 * Concurrency: no concurrent access is possible that late in object
1409 * life-cycle (for all existing callers, that is. New callers have to provide
1410 * their own locking.)
1412 static int osd_inode_unlinked(const struct inode *inode)
1414 return inode->i_nlink == 0;
1418 OSD_TXN_OI_DELETE_CREDITS = 20,
1419 OSD_TXN_INODE_DELETE_CREDITS = 20
1426 #if OSD_THANDLE_STATS
1428 * Set time when the handle is allocated
1430 static void osd_th_alloced(struct osd_thandle *oth)
1432 oth->oth_alloced = cfs_time_current();
1436 * Set time when the handle started
1438 static void osd_th_started(struct osd_thandle *oth)
1440 oth->oth_started = cfs_time_current();
1444 * Helper function to convert time interval to microseconds packed in
1447 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
1451 cfs_duration_usec(cfs_time_sub(end, start), &val);
1452 return val.tv_sec * 1000000 + val.tv_usec;
1456 * Check whether the we deal with this handle for too long.
1458 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1459 cfs_time_t alloced, cfs_time_t started,
1462 cfs_time_t now = cfs_time_current();
1464 LASSERT(dev != NULL);
1466 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1467 interval_to_usec(alloced, started));
1468 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1469 interval_to_usec(started, closed));
1470 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1471 interval_to_usec(closed, now));
1473 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
1474 CWARN("transaction handle %p was open for too long: "
1475 "now "CFS_TIME_T" ,"
1476 "alloced "CFS_TIME_T" ,"
1477 "started "CFS_TIME_T" ,"
1478 "closed "CFS_TIME_T"\n",
1479 oth, now, alloced, started, closed);
1480 libcfs_debug_dumpstack(NULL);
1484 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1486 cfs_time_t __closed = cfs_time_current(); \
1487 cfs_time_t __alloced = oth->oth_alloced; \
1488 cfs_time_t __started = oth->oth_started; \
1491 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1494 #else /* OSD_THANDLE_STATS */
1496 #define osd_th_alloced(h) do {} while(0)
1497 #define osd_th_started(h) do {} while(0)
1498 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1500 #endif /* OSD_THANDLE_STATS */
1503 * Concurrency: doesn't access mutable data.
1505 static int osd_param_is_not_sane(const struct osd_device *dev,
1506 const struct thandle *th)
1508 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1510 return oh->ot_credits > osd_transaction_size(dev);
1514 * Concurrency: shouldn't matter.
1516 static void osd_trans_commit_cb(struct super_block *sb,
1517 struct ldiskfs_journal_cb_entry *jcb, int error)
1519 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1520 struct thandle *th = &oh->ot_super;
1521 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1522 struct dt_txn_commit_cb *dcb, *tmp;
1524 LASSERT(oh->ot_handle == NULL);
1527 CERROR("transaction @0x%p commit error: %d\n", th, error);
1529 dt_txn_hook_commit(th);
1531 /* call per-transaction callbacks if any */
1532 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1534 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1535 "commit callback entry: magic=%x name='%s'\n",
1536 dcb->dcb_magic, dcb->dcb_name);
1537 list_del_init(&dcb->dcb_linkage);
1538 dcb->dcb_func(NULL, th, dcb, error);
1541 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1545 lu_context_exit(&th->th_ctx);
1546 lu_context_fini(&th->th_ctx);
1550 #ifndef HAVE_SB_START_WRITE
1551 # define sb_start_write(sb) do {} while (0)
1552 # define sb_end_write(sb) do {} while (0)
1555 static struct thandle *osd_trans_create(const struct lu_env *env,
1556 struct dt_device *d)
1558 struct osd_thread_info *oti = osd_oti_get(env);
1559 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1560 struct osd_thandle *oh;
1564 /* on pending IO in this thread should left from prev. request */
1565 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1567 sb_start_write(osd_sb(osd_dt_dev(d)));
1569 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1571 oh->ot_quota_trans = &oti->oti_quota_trans;
1572 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1576 th->th_tags = LCT_TX_HANDLE;
1578 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1579 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1582 memset(oti->oti_declare_ops, 0,
1583 sizeof(oti->oti_declare_ops));
1584 memset(oti->oti_declare_ops_cred, 0,
1585 sizeof(oti->oti_declare_ops_cred));
1586 memset(oti->oti_declare_ops_used, 0,
1587 sizeof(oti->oti_declare_ops_used));
1589 sb_end_write(osd_sb(osd_dt_dev(d)));
1590 th = ERR_PTR(-ENOMEM);
1595 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1597 struct osd_thread_info *oti = osd_oti_get(env);
1598 struct osd_thandle *oh;
1600 oh = container_of0(th, struct osd_thandle, ot_super);
1601 LASSERT(oh != NULL);
1603 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1604 oti->oti_declare_ops[OSD_OT_CREATE],
1605 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1606 oti->oti_declare_ops_used[OSD_OT_CREATE],
1607 oti->oti_declare_ops[OSD_OT_DESTROY],
1608 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1609 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1610 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1611 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1612 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1613 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1614 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1615 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1616 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1617 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1618 oti->oti_declare_ops[OSD_OT_WRITE],
1619 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1620 oti->oti_declare_ops_used[OSD_OT_WRITE],
1621 oti->oti_declare_ops[OSD_OT_PUNCH],
1622 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1623 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1624 oti->oti_declare_ops[OSD_OT_QUOTA],
1625 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1626 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1627 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1628 oti->oti_declare_ops[OSD_OT_INSERT],
1629 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1630 oti->oti_declare_ops_used[OSD_OT_INSERT],
1631 oti->oti_declare_ops[OSD_OT_DELETE],
1632 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1633 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1634 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1635 oti->oti_declare_ops[OSD_OT_REF_ADD],
1636 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1637 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1638 oti->oti_declare_ops[OSD_OT_REF_DEL],
1639 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1640 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1644 * Concurrency: shouldn't matter.
1646 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1649 struct osd_thread_info *oti = osd_oti_get(env);
1650 struct osd_device *dev = osd_dt_dev(d);
1652 struct osd_thandle *oh;
1657 LASSERT(current->journal_info == NULL);
1659 oh = container_of0(th, struct osd_thandle, ot_super);
1660 LASSERT(oh != NULL);
1661 LASSERT(oh->ot_handle == NULL);
1663 rc = dt_txn_hook_start(env, d, th);
1667 if (unlikely(osd_param_is_not_sane(dev, th))) {
1668 static unsigned long last_printed;
1669 static int last_credits;
1671 /* don't make noise on a tiny testing systems
1672 * actual credits misuse will be caught anyway */
1673 if (last_credits != oh->ot_credits &&
1674 time_after(jiffies, last_printed +
1675 msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
1676 osd_transaction_size(dev) > 512) {
1677 osd_trans_dump_creds(env, th);
1678 libcfs_debug_dumpstack(NULL);
1679 last_credits = oh->ot_credits;
1680 last_printed = jiffies;
1682 /* XXX Limit the credits to 'max_transaction_buffers', and
1683 * let the underlying filesystem to catch the error if
1684 * we really need so many credits.
1686 * This should be removed when we can calculate the
1687 * credits precisely. */
1688 oh->ot_credits = osd_transaction_size(dev);
1692 * XXX temporary stuff. Some abstraction layer should
1695 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1699 LASSERT(oti->oti_txns == 0);
1700 lu_context_init(&th->th_ctx, th->th_tags);
1701 lu_context_enter(&th->th_ctx);
1703 lu_device_get(&d->dd_lu_dev);
1704 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1715 static int osd_seq_exists(const struct lu_env *env,
1716 struct osd_device *osd, u64 seq)
1718 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1719 struct seq_server_site *ss = osd_seq_site(osd);
1723 LASSERT(ss != NULL);
1724 LASSERT(ss->ss_server_fld != NULL);
1726 rc = osd_fld_lookup(env, osd, seq, range);
1729 CERROR("%s: can't lookup FLD sequence "LPX64
1730 ": rc = %d\n", osd_name(osd), seq, rc);
1734 RETURN(ss->ss_node_id == range->lsr_index);
1737 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1739 struct dt_txn_commit_cb *dcb;
1740 struct dt_txn_commit_cb *tmp;
1742 /* call per-transaction stop callbacks if any */
1743 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1745 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1746 "commit callback entry: magic=%x name='%s'\n",
1747 dcb->dcb_magic, dcb->dcb_name);
1748 list_del_init(&dcb->dcb_linkage);
1749 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1754 * Concurrency: shouldn't matter.
1756 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1759 int rc = 0, remove_agents = 0;
1760 struct osd_thandle *oh;
1761 struct osd_thread_info *oti = osd_oti_get(env);
1762 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1763 struct osd_device *osd = osd_dt_dev(th->th_dev);
1764 struct qsd_instance *qsd = osd->od_quota_slave;
1765 struct lquota_trans *qtrans;
1768 oh = container_of0(th, struct osd_thandle, ot_super);
1770 /* reset OI cache for safety */
1771 oti->oti_ins_cache_used = 0;
1773 remove_agents = oh->ot_remove_agents;
1775 qtrans = oh->ot_quota_trans;
1776 oh->ot_quota_trans = NULL;
1778 if (oh->ot_handle != NULL) {
1779 handle_t *hdl = oh->ot_handle;
1782 * add commit callback
1783 * notice we don't do this in osd_trans_start()
1784 * as underlying transaction can change during truncate
1786 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1789 LASSERT(oti->oti_txns == 1);
1792 rc = dt_txn_hook_stop(env, th);
1794 CERROR("%s: failed in transaction hook: rc = %d\n",
1797 osd_trans_stop_cb(oh, rc);
1798 /* hook functions might modify th_sync */
1799 hdl->h_sync = th->th_sync;
1801 oh->ot_handle = NULL;
1802 OSD_CHECK_SLOW_TH(oh, osd, rc = ldiskfs_journal_stop(hdl));
1804 CERROR("%s: failed to stop transaction: rc = %d\n",
1807 osd_trans_stop_cb(oh, th->th_result);
1811 /* inform the quota slave device that the transaction is stopping */
1812 qsd_op_end(env, qsd, qtrans);
1814 /* as we want IO to journal and data IO be concurrent, we don't block
1815 * awaiting data IO completion in osd_do_bio(), instead we wait here
1816 * once transaction is submitted to the journal. all reqular requests
1817 * don't do direct IO (except read/write), thus this wait_event becomes
1820 * IMPORTANT: we have to wait till any IO submited by the thread is
1821 * completed otherwise iobuf may be corrupted by different request
1823 wait_event(iobuf->dr_wait,
1824 atomic_read(&iobuf->dr_numreqs) == 0);
1825 osd_fini_iobuf(osd, iobuf);
1827 rc = iobuf->dr_error;
1829 if (unlikely(remove_agents != 0))
1830 osd_process_scheduled_agent_removals(env, osd);
1832 sb_end_write(osd_sb(osd));
1837 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1839 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1842 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1843 LASSERT(&dcb->dcb_func != NULL);
1844 if (dcb->dcb_flags & DCB_TRANS_STOP)
1845 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1847 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1853 * Called just before object is freed. Releases all resources except for
1854 * object itself (that is released by osd_object_free()).
1856 * Concurrency: no concurrent access is possible that late in object
1859 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1861 struct osd_object *obj = osd_obj(l);
1862 struct inode *inode = obj->oo_inode;
1864 LINVRNT(osd_invariant(obj));
1867 * If object is unlinked remove fid->ino mapping from object index.
1870 osd_index_fini(obj);
1871 if (inode != NULL) {
1872 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1873 qid_t uid = i_uid_read(inode);
1874 qid_t gid = i_gid_read(inode);
1877 obj->oo_inode = NULL;
1880 struct osd_thread_info *info = osd_oti_get(env);
1881 struct lquota_id_info *qi = &info->oti_qi;
1883 /* Release granted quota to master if necessary */
1884 qi->lqi_id.qid_uid = uid;
1885 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1887 qi->lqi_id.qid_uid = gid;
1888 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1894 * Concurrency: ->loo_object_release() is called under site spin-lock.
1896 static void osd_object_release(const struct lu_env *env,
1897 struct lu_object *l)
1899 struct osd_object *o = osd_obj(l);
1900 /* nobody should be releasing a non-destroyed object with nlink=0
1901 * the API allows this, but ldiskfs doesn't like and then report
1902 * this inode as deleted */
1903 if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
1908 * Concurrency: shouldn't matter.
1910 static int osd_object_print(const struct lu_env *env, void *cookie,
1911 lu_printer_t p, const struct lu_object *l)
1913 struct osd_object *o = osd_obj(l);
1914 struct iam_descr *d;
1916 if (o->oo_dir != NULL)
1917 d = o->oo_dir->od_container.ic_descr;
1920 return (*p)(env, cookie,
1921 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1923 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1924 o->oo_inode ? o->oo_inode->i_generation : 0,
1925 d ? d->id_ops->id_name : "plain");
1929 * Concurrency: shouldn't matter.
1931 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1932 struct obd_statfs *sfs)
1934 struct osd_device *osd = osd_dt_dev(d);
1935 struct super_block *sb = osd_sb(osd);
1936 struct kstatfs *ksfs;
1940 if (unlikely(osd->od_mnt == NULL))
1941 return -EINPROGRESS;
1943 /* osd_lproc.c call this without env, allocate ksfs for that case */
1944 if (unlikely(env == NULL)) {
1945 OBD_ALLOC_PTR(ksfs);
1949 ksfs = &osd_oti_get(env)->oti_ksfs;
1952 result = sb->s_op->statfs(sb->s_root, ksfs);
1956 statfs_pack(sfs, ksfs);
1957 if (unlikely(sb->s_flags & MS_RDONLY))
1958 sfs->os_state = OS_STATE_READONLY;
1959 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
1960 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1961 sfs->os_maxbytes = sb->s_maxbytes;
1963 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1966 * Reserve some space so to avoid fragmenting the filesystem too much.
1967 * Fragmentation not only impacts performance, but can also increase
1968 * metadata overhead significantly, causing grant calculation to be
1971 * Reserve 0.78% of total space, at least 8MB for small filesystems.
1973 CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
1974 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
1975 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
1976 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
1978 sfs->os_blocks -= reserved;
1979 sfs->os_bfree -= min(reserved, sfs->os_bfree);
1980 sfs->os_bavail -= min(reserved, sfs->os_bavail);
1983 if (unlikely(env == NULL))
1989 * Estimate space needed for file creations. We assume the largest filename
1990 * which is 2^64 - 1, hence a filename of 20 chars.
1991 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1993 #ifdef __LDISKFS_DIR_REC_LEN
1994 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1996 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2000 * Concurrency: doesn't access mutable data.
2002 static void osd_conf_get(const struct lu_env *env,
2003 const struct dt_device *dev,
2004 struct dt_device_param *param)
2006 struct super_block *sb = osd_sb(osd_dt_dev(dev));
2010 * XXX should be taken from not-yet-existing fs abstraction layer.
2012 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2013 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2014 param->ddp_symlink_max = sb->s_blocksize;
2015 param->ddp_mount_type = LDD_MT_LDISKFS;
2016 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2017 param->ddp_maxbytes = sb->s_maxbytes;
2019 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2020 /* inode are statically allocated, so per-inode space consumption
2021 * is the space consumed by the directory entry */
2022 param->ddp_inodespace = PER_OBJ_USAGE;
2023 /* EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2024 * = 128MB) which is unlikely to be hit in real life. Report a smaller
2025 * maximum length to not under count the actual number of extents
2026 * needed for writing a file. */
2027 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
2028 /* worst-case extent insertion metadata overhead */
2029 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2030 param->ddp_mntopts = 0;
2031 if (test_opt(sb, XATTR_USER))
2032 param->ddp_mntopts |= MNTOPT_USERXATTR;
2033 if (test_opt(sb, POSIX_ACL))
2034 param->ddp_mntopts |= MNTOPT_ACL;
2036 /* LOD might calculate the max stripe count based on max_ea_size,
2037 * so we need take account in the overhead as well,
2038 * xattr_header + magic + xattr_entry_head */
2039 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2040 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2042 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2043 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
2044 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2048 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2052 * Concurrency: shouldn't matter.
2054 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2058 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2060 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
2062 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
2063 LUSTRE_OSD_LDISKFS_NAME, rc);
2069 * Start commit for OSD device.
2071 * An implementation of dt_commit_async method for OSD device.
2072 * Asychronously starts underlayng fs sync and thereby a transaction
2075 * \param env environment
2076 * \param d dt device
2078 * \see dt_device_operations
2080 static int osd_commit_async(const struct lu_env *env,
2081 struct dt_device *d)
2083 struct super_block *s = osd_sb(osd_dt_dev(d));
2086 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2087 RETURN(s->s_op->sync_fs(s, 0));
2091 * Concurrency: shouldn't matter.
2094 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2096 struct super_block *sb = osd_sb(osd_dt_dev(d));
2097 struct block_device *dev = sb->s_bdev;
2098 #ifdef HAVE_DEV_SET_RDONLY
2099 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
2102 int rc = -EOPNOTSUPP;
2106 #ifdef HAVE_DEV_SET_RDONLY
2107 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
2109 if (sb->s_op->freeze_fs) {
2110 rc = sb->s_op->freeze_fs(sb);
2115 if (jdev && (jdev != dev)) {
2116 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
2118 dev_set_rdonly(jdev);
2120 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
2121 dev_set_rdonly(dev);
2123 if (sb->s_op->unfreeze_fs)
2124 sb->s_op->unfreeze_fs(sb);
2129 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2130 osd_dt_dev(d)->od_svname, (long)dev, rc);
2136 * Note: we do not count into QUOTA here.
2137 * If we mount with --data_journal we may need more.
2139 const int osd_dto_credits_noquota[DTO_NR] = {
2142 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2143 * SINGLEDATA_TRANS_BLOCKS(8)
2144 * XXX Note: maybe iam need more, since iam have more level than
2147 [DTO_INDEX_INSERT] = 16,
2150 * just modify a single entry, probably merge few within a block
2152 [DTO_INDEX_DELETE] = 1,
2156 [DTO_INDEX_UPDATE] = 16,
2158 * 4(inode, inode bits, groups, GDT)
2159 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2161 [DTO_OBJECT_CREATE] = 4,
2163 * 4(inode, inode bits, groups, GDT)
2164 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2166 [DTO_OBJECT_DELETE] = 4,
2168 * Attr set credits (inode)
2170 [DTO_ATTR_SET_BASE] = 1,
2172 * Xattr set. The same as xattr of EXT3.
2173 * DATA_TRANS_BLOCKS(14)
2174 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2175 * are also counted in. Do not know why?
2177 [DTO_XATTR_SET] = 14,
2179 * credits for inode change during write.
2181 [DTO_WRITE_BASE] = 3,
2183 * credits for single block write.
2185 [DTO_WRITE_BLOCK] = 14,
2187 * Attr set credits for chown.
2188 * This is extra credits for setattr, and it is null without quota
2190 [DTO_ATTR_SET_CHOWN] = 0
2193 static const struct dt_device_operations osd_dt_ops = {
2194 .dt_root_get = osd_root_get,
2195 .dt_statfs = osd_statfs,
2196 .dt_trans_create = osd_trans_create,
2197 .dt_trans_start = osd_trans_start,
2198 .dt_trans_stop = osd_trans_stop,
2199 .dt_trans_cb_add = osd_trans_cb_add,
2200 .dt_conf_get = osd_conf_get,
2201 .dt_sync = osd_sync,
2203 .dt_commit_async = osd_commit_async,
2206 static void osd_object_read_lock(const struct lu_env *env,
2207 struct dt_object *dt, unsigned role)
2209 struct osd_object *obj = osd_dt_obj(dt);
2210 struct osd_thread_info *oti = osd_oti_get(env);
2212 LINVRNT(osd_invariant(obj));
2214 LASSERT(obj->oo_owner != env);
2215 down_read_nested(&obj->oo_sem, role);
2217 LASSERT(obj->oo_owner == NULL);
2221 static void osd_object_write_lock(const struct lu_env *env,
2222 struct dt_object *dt, unsigned role)
2224 struct osd_object *obj = osd_dt_obj(dt);
2225 struct osd_thread_info *oti = osd_oti_get(env);
2227 LINVRNT(osd_invariant(obj));
2229 LASSERT(obj->oo_owner != env);
2230 down_write_nested(&obj->oo_sem, role);
2232 LASSERT(obj->oo_owner == NULL);
2233 obj->oo_owner = env;
2237 static void osd_object_read_unlock(const struct lu_env *env,
2238 struct dt_object *dt)
2240 struct osd_object *obj = osd_dt_obj(dt);
2241 struct osd_thread_info *oti = osd_oti_get(env);
2243 LINVRNT(osd_invariant(obj));
2245 LASSERT(oti->oti_r_locks > 0);
2247 up_read(&obj->oo_sem);
2250 static void osd_object_write_unlock(const struct lu_env *env,
2251 struct dt_object *dt)
2253 struct osd_object *obj = osd_dt_obj(dt);
2254 struct osd_thread_info *oti = osd_oti_get(env);
2256 LINVRNT(osd_invariant(obj));
2258 LASSERT(obj->oo_owner == env);
2259 LASSERT(oti->oti_w_locks > 0);
2261 obj->oo_owner = NULL;
2262 up_write(&obj->oo_sem);
2265 static int osd_object_write_locked(const struct lu_env *env,
2266 struct dt_object *dt)
2268 struct osd_object *obj = osd_dt_obj(dt);
2270 LINVRNT(osd_invariant(obj));
2272 return obj->oo_owner == env;
2275 static struct timespec *osd_inode_time(const struct lu_env *env,
2276 struct inode *inode, __u64 seconds)
2278 struct osd_thread_info *oti = osd_oti_get(env);
2279 struct timespec *t = &oti->oti_time;
2281 t->tv_sec = seconds;
2283 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
2287 static void osd_inode_getattr(const struct lu_env *env,
2288 struct inode *inode, struct lu_attr *attr)
2290 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2291 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2292 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
2295 attr->la_atime = LTIME_S(inode->i_atime);
2296 attr->la_mtime = LTIME_S(inode->i_mtime);
2297 attr->la_ctime = LTIME_S(inode->i_ctime);
2298 attr->la_mode = inode->i_mode;
2299 attr->la_size = i_size_read(inode);
2300 attr->la_blocks = inode->i_blocks;
2301 attr->la_uid = i_uid_read(inode);
2302 attr->la_gid = i_gid_read(inode);
2303 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2304 attr->la_nlink = inode->i_nlink;
2305 attr->la_rdev = inode->i_rdev;
2306 attr->la_blksize = 1 << inode->i_blkbits;
2307 attr->la_blkbits = inode->i_blkbits;
2310 static int osd_attr_get(const struct lu_env *env,
2311 struct dt_object *dt,
2312 struct lu_attr *attr)
2314 struct osd_object *obj = osd_dt_obj(dt);
2316 if (unlikely(!dt_object_exists(dt)))
2318 if (unlikely(obj->oo_destroyed))
2321 LASSERT(!dt_object_remote(dt));
2322 LINVRNT(osd_invariant(obj));
2324 spin_lock(&obj->oo_guard);
2325 osd_inode_getattr(env, obj->oo_inode, attr);
2326 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
2327 attr->la_flags |= LUSTRE_ORPHAN_FL;
2328 spin_unlock(&obj->oo_guard);
2333 static int osd_declare_attr_set(const struct lu_env *env,
2334 struct dt_object *dt,
2335 const struct lu_attr *attr,
2336 struct thandle *handle)
2338 struct osd_thandle *oh;
2339 struct osd_object *obj;
2340 struct osd_thread_info *info = osd_oti_get(env);
2341 struct lquota_id_info *qi = &info->oti_qi;
2349 LASSERT(dt != NULL);
2350 LASSERT(handle != NULL);
2352 obj = osd_dt_obj(dt);
2353 LASSERT(osd_invariant(obj));
2355 oh = container_of0(handle, struct osd_thandle, ot_super);
2356 LASSERT(oh->ot_handle == NULL);
2358 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2359 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2361 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2362 osd_dto_credits_noquota[DTO_XATTR_SET]);
2364 if (attr == NULL || obj->oo_inode == NULL)
2367 bspace = obj->oo_inode->i_blocks;
2368 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2369 bspace = toqb(bspace);
2371 /* Changing ownership is always preformed by super user, it should not
2374 * We still need to call the osd_declare_qid() to calculate the journal
2375 * credits for updating quota accounting files and to trigger quota
2376 * space adjustment once the operation is completed.*/
2377 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2379 uid = i_uid_read(obj->oo_inode);
2380 qi->lqi_type = USRQUOTA;
2381 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2382 /* inode accounting */
2383 qi->lqi_is_blk = false;
2385 /* one more inode for the new uid ... */
2386 qi->lqi_id.qid_uid = attr->la_uid;
2388 /* Reserve credits for the new uid */
2389 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2390 if (rc == -EDQUOT || rc == -EINPROGRESS)
2395 /* and one less inode for the current uid */
2396 qi->lqi_id.qid_uid = uid;
2398 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2399 if (rc == -EDQUOT || rc == -EINPROGRESS)
2404 /* block accounting */
2405 qi->lqi_is_blk = true;
2407 /* more blocks for the new uid ... */
2408 qi->lqi_id.qid_uid = attr->la_uid;
2409 qi->lqi_space = bspace;
2411 * Credits for the new uid has been reserved, re-use "obj"
2412 * to save credit reservation.
2414 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2415 if (rc == -EDQUOT || rc == -EINPROGRESS)
2420 /* and finally less blocks for the current uid */
2421 qi->lqi_id.qid_uid = uid;
2422 qi->lqi_space = -bspace;
2423 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2424 if (rc == -EDQUOT || rc == -EINPROGRESS)
2430 gid = i_gid_read(obj->oo_inode);
2431 qi->lqi_type = GRPQUOTA;
2432 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2434 /* inode accounting */
2435 qi->lqi_is_blk = false;
2437 /* one more inode for the new gid ... */
2438 qi->lqi_id.qid_gid = attr->la_gid;
2440 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2441 if (rc == -EDQUOT || rc == -EINPROGRESS)
2446 /* and one less inode for the current gid */
2447 qi->lqi_id.qid_gid = gid;
2449 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2450 if (rc == -EDQUOT || rc == -EINPROGRESS)
2455 /* block accounting */
2456 qi->lqi_is_blk = true;
2458 /* more blocks for the new gid ... */
2459 qi->lqi_id.qid_gid = attr->la_gid;
2460 qi->lqi_space = bspace;
2461 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2462 if (rc == -EDQUOT || rc == -EINPROGRESS)
2467 /* and finally less blocks for the current gid */
2468 qi->lqi_id.qid_gid = gid;
2469 qi->lqi_space = -bspace;
2470 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2471 if (rc == -EDQUOT || rc == -EINPROGRESS)
2480 static int osd_inode_setattr(const struct lu_env *env,
2481 struct inode *inode, const struct lu_attr *attr)
2483 __u64 bits = attr->la_valid;
2485 /* Only allow set size for regular file */
2486 if (!S_ISREG(inode->i_mode))
2487 bits &= ~(LA_SIZE | LA_BLOCKS);
2492 if (bits & LA_ATIME)
2493 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2494 if (bits & LA_CTIME)
2495 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2496 if (bits & LA_MTIME)
2497 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2498 if (bits & LA_SIZE) {
2499 LDISKFS_I(inode)->i_disksize = attr->la_size;
2500 i_size_write(inode, attr->la_size);
2503 /* OSD should not change "i_blocks" which is used by quota.
2504 * "i_blocks" should be changed by ldiskfs only. */
2506 inode->i_mode = (inode->i_mode & S_IFMT) |
2507 (attr->la_mode & ~S_IFMT);
2509 i_uid_write(inode, attr->la_uid);
2511 i_gid_write(inode, attr->la_gid);
2512 if (bits & LA_NLINK)
2513 set_nlink(inode, attr->la_nlink);
2515 inode->i_rdev = attr->la_rdev;
2517 if (bits & LA_FLAGS) {
2518 /* always keep S_NOCMTIME */
2519 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2525 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2527 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2528 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2532 ll_vfs_dq_init(inode);
2534 if (attr->la_valid & LA_UID)
2535 iattr.ia_valid |= ATTR_UID;
2536 if (attr->la_valid & LA_GID)
2537 iattr.ia_valid |= ATTR_GID;
2538 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2539 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2541 rc = ll_vfs_dq_transfer(inode, &iattr);
2543 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2544 "enforcement enabled on the ldiskfs "
2545 "filesystem?\n", inode->i_sb->s_id, rc);
2552 static int osd_attr_set(const struct lu_env *env,
2553 struct dt_object *dt,
2554 const struct lu_attr *attr,
2555 struct thandle *handle)
2557 struct osd_object *obj = osd_dt_obj(dt);
2558 struct inode *inode;
2561 if (!dt_object_exists(dt))
2564 LASSERT(handle != NULL);
2565 LASSERT(!dt_object_remote(dt));
2566 LASSERT(osd_invariant(obj));
2568 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2570 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2571 struct osd_thread_info *oti = osd_oti_get(env);
2572 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2573 struct lu_fid *fid1 = &oti->oti_fid;
2574 struct osd_inode_id *id = &oti->oti_id;
2575 struct iam_path_descr *ipd;
2576 struct iam_container *bag;
2577 struct osd_thandle *oh;
2580 fid_cpu_to_be(fid1, fid0);
2581 memset(id, 1, sizeof(*id));
2582 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2583 fid0)->oi_dir.od_container;
2584 ipd = osd_idx_ipd_get(env, bag);
2585 if (unlikely(ipd == NULL))
2588 oh = container_of0(handle, struct osd_thandle, ot_super);
2589 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2590 (const struct iam_rec *)id, ipd);
2591 osd_ipd_put(env, bag, ipd);
2592 return(rc > 0 ? 0 : rc);
2595 inode = obj->oo_inode;
2597 rc = osd_quota_transfer(inode, attr);
2601 spin_lock(&obj->oo_guard);
2602 rc = osd_inode_setattr(env, inode, attr);
2603 spin_unlock(&obj->oo_guard);
2607 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2609 if (!(attr->la_valid & LA_FLAGS))
2612 /* Let's check if there are extra flags need to be set into LMA */
2613 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2614 struct osd_thread_info *info = osd_oti_get(env);
2615 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2617 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
2621 lma->lma_incompat |=
2622 lustre_to_lma_flags(attr->la_flags);
2623 lustre_lma_swab(lma);
2624 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2625 lma, sizeof(*lma), XATTR_REPLACE);
2627 struct osd_device *osd = osd_obj2dev(obj);
2629 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2630 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2631 lma->lma_incompat, rc);
2634 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2636 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2639 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2644 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2645 struct osd_object *obj,
2646 const char *name, const int namelen)
2648 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2651 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2652 umode_t mode, struct dt_allocation_hint *hint,
2656 struct osd_device *osd = osd_obj2dev(obj);
2657 struct osd_thandle *oth;
2658 struct dt_object *parent = NULL;
2659 struct inode *inode;
2661 LINVRNT(osd_invariant(obj));
2662 LASSERT(obj->oo_inode == NULL);
2663 LASSERT(obj->oo_hl_head == NULL);
2665 if (S_ISDIR(mode) && ldiskfs_pdo) {
2666 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2667 if (obj->oo_hl_head == NULL)
2671 oth = container_of(th, struct osd_thandle, ot_super);
2672 LASSERT(oth->ot_handle->h_transaction != NULL);
2674 if (hint != NULL && hint->dah_parent != NULL &&
2675 !dt_object_remote(hint->dah_parent))
2676 parent = hint->dah_parent;
2678 inode = ldiskfs_create_inode(oth->ot_handle,
2679 parent ? osd_dt_obj(parent)->oo_inode :
2680 osd_sb(osd)->s_root->d_inode,
2682 if (!IS_ERR(inode)) {
2683 /* Do not update file c/mtime in ldiskfs. */
2684 inode->i_flags |= S_NOCMTIME;
2686 /* For new created object, it must be consistent,
2687 * and it is unnecessary to scrub against it. */
2688 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2690 obj->oo_inode = inode;
2693 if (obj->oo_hl_head != NULL) {
2694 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2695 obj->oo_hl_head = NULL;
2697 result = PTR_ERR(inode);
2699 LINVRNT(osd_invariant(obj));
2707 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2708 struct lu_attr *attr,
2709 struct dt_allocation_hint *hint,
2710 struct dt_object_format *dof,
2714 struct osd_thandle *oth;
2715 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
2717 LASSERT(S_ISDIR(attr->la_mode));
2719 oth = container_of(th, struct osd_thandle, ot_super);
2720 LASSERT(oth->ot_handle->h_transaction != NULL);
2721 result = osd_mkfile(info, obj, mode, hint, th);
2726 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2727 struct lu_attr *attr,
2728 struct dt_allocation_hint *hint,
2729 struct dt_object_format *dof,
2733 struct osd_thandle *oth;
2734 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2736 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2738 LASSERT(S_ISREG(attr->la_mode));
2740 oth = container_of(th, struct osd_thandle, ot_super);
2741 LASSERT(oth->ot_handle->h_transaction != NULL);
2743 result = osd_mkfile(info, obj, mode, hint, th);
2745 LASSERT(obj->oo_inode != NULL);
2746 if (feat->dif_flags & DT_IND_VARKEY)
2747 result = iam_lvar_create(obj->oo_inode,
2748 feat->dif_keysize_max,
2750 feat->dif_recsize_max,
2753 result = iam_lfix_create(obj->oo_inode,
2754 feat->dif_keysize_max,
2756 feat->dif_recsize_max,
2763 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2764 struct lu_attr *attr,
2765 struct dt_allocation_hint *hint,
2766 struct dt_object_format *dof,
2769 LASSERT(S_ISREG(attr->la_mode));
2770 return osd_mkfile(info, obj, (attr->la_mode &
2771 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2774 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2775 struct lu_attr *attr,
2776 struct dt_allocation_hint *hint,
2777 struct dt_object_format *dof,
2780 LASSERT(S_ISLNK(attr->la_mode));
2781 return osd_mkfile(info, obj, (attr->la_mode &
2782 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2785 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2786 struct lu_attr *attr,
2787 struct dt_allocation_hint *hint,
2788 struct dt_object_format *dof,
2791 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2794 LINVRNT(osd_invariant(obj));
2795 LASSERT(obj->oo_inode == NULL);
2796 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2797 S_ISFIFO(mode) || S_ISSOCK(mode));
2799 result = osd_mkfile(info, obj, mode, hint, th);
2801 LASSERT(obj->oo_inode != NULL);
2803 * This inode should be marked dirty for i_rdev. Currently
2804 * that is done in the osd_attr_init().
2806 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2809 LINVRNT(osd_invariant(obj));
2813 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2815 struct dt_allocation_hint *hint,
2816 struct dt_object_format *dof,
2819 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2821 osd_obj_type_f result;
2837 result = osd_mk_index;
2848 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2849 struct dt_object *parent, struct dt_object *child,
2854 ah->dah_parent = parent;
2855 ah->dah_mode = child_mode;
2857 if (parent != NULL && !dt_object_remote(parent)) {
2858 /* will help to find FID->ino at dt_insert("..") */
2859 struct osd_object *pobj = osd_dt_obj(parent);
2860 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2864 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2865 struct lu_attr *attr, struct dt_object_format *dof)
2867 struct inode *inode = obj->oo_inode;
2868 __u64 valid = attr->la_valid;
2871 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2873 if (dof->dof_type != DFT_NODE)
2874 attr->la_valid &= ~LA_RDEV;
2875 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2876 attr->la_valid &= ~LA_ATIME;
2877 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2878 attr->la_valid &= ~LA_CTIME;
2879 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2880 attr->la_valid &= ~LA_MTIME;
2882 result = osd_quota_transfer(inode, attr);
2886 if (attr->la_valid != 0) {
2887 result = osd_inode_setattr(info->oti_env, inode, attr);
2889 * The osd_inode_setattr() should always succeed here. The
2890 * only error that could be returned is EDQUOT when we are
2891 * trying to change the UID or GID of the inode. However, this
2892 * should not happen since quota enforcement is no longer
2893 * enabled on ldiskfs (lquota takes care of it).
2895 LASSERTF(result == 0, "%d\n", result);
2896 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2899 attr->la_valid = valid;
2903 * Helper function for osd_object_create()
2905 * \retval 0, on success
2907 static int __osd_object_create(struct osd_thread_info *info,
2908 struct osd_object *obj, struct lu_attr *attr,
2909 struct dt_allocation_hint *hint,
2910 struct dt_object_format *dof,
2916 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2918 /* we drop umask so that permissions we pass are not affected */
2919 umask = current->fs->umask;
2920 current->fs->umask = 0;
2922 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2924 if (likely(obj->oo_inode != NULL)) {
2925 LASSERT(obj->oo_inode->i_state & I_NEW);
2927 /* Unlock the inode before attr initialization to avoid
2928 * unnecessary dqget operations. LU-6378 */
2929 unlock_new_inode(obj->oo_inode);
2932 if (likely(result == 0)) {
2933 osd_attr_init(info, obj, attr, dof);
2934 osd_object_init0(obj);
2937 /* restore previous umask value */
2938 current->fs->umask = umask;
2940 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
2946 * Helper function for osd_object_create()
2948 * \retval 0, on success
2950 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2951 const struct lu_fid *fid, struct thandle *th)
2953 struct osd_thread_info *info = osd_oti_get(env);
2954 struct osd_inode_id *id = &info->oti_id;
2955 struct osd_device *osd = osd_obj2dev(obj);
2956 struct osd_thandle *oh;
2959 LASSERT(obj->oo_inode != NULL);
2961 oh = container_of0(th, struct osd_thandle, ot_super);
2962 LASSERT(oh->ot_handle);
2963 osd_trans_exec_op(env, th, OSD_OT_INSERT);
2965 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2966 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
2967 OI_CHECK_FLD, NULL);
2968 osd_trans_exec_check(env, th, OSD_OT_INSERT);
2973 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2974 u64 seq, struct lu_seq_range *range)
2976 struct seq_server_site *ss = osd_seq_site(osd);
2978 if (fid_seq_is_idif(seq)) {
2979 fld_range_set_ost(range);
2980 range->lsr_index = idif_ost_idx(seq);
2984 if (!fid_seq_in_fldb(seq)) {
2985 fld_range_set_mdt(range);
2987 /* FIXME: If ss is NULL, it suppose not get lsr_index
2989 range->lsr_index = ss->ss_node_id;
2993 LASSERT(ss != NULL);
2994 fld_range_set_any(range);
2995 /* OSD will only do local fld lookup */
2996 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3000 * Concurrency: no external locking is necessary.
3002 static int osd_declare_object_create(const struct lu_env *env,
3003 struct dt_object *dt,
3004 struct lu_attr *attr,
3005 struct dt_allocation_hint *hint,
3006 struct dt_object_format *dof,
3007 struct thandle *handle)
3009 struct osd_thandle *oh;
3013 LASSERT(handle != NULL);
3015 oh = container_of0(handle, struct osd_thandle, ot_super);
3016 LASSERT(oh->ot_handle == NULL);
3018 /* EA object consumes more credits than regular object: osd_mk_index
3019 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3020 * leaf_node, could involves the block, block bitmap, groups, GDT
3021 * change for each block, so add 4 * 2 credits in that case. */
3022 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3023 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3024 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3025 /* Reuse idle OI block may cause additional one OI block
3027 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3028 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3033 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
3034 osd_dt_obj(dt), false, NULL, false);
3038 /* will help to find FID->ino mapping at dt_insert() */
3039 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3045 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
3046 struct lu_attr *attr,
3047 struct dt_allocation_hint *hint,
3048 struct dt_object_format *dof, struct thandle *th)
3050 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3051 struct osd_object *obj = osd_dt_obj(dt);
3052 struct osd_thread_info *info = osd_oti_get(env);
3056 if (dt_object_exists(dt))
3059 LINVRNT(osd_invariant(obj));
3060 LASSERT(!dt_object_remote(dt));
3061 LASSERT(osd_write_locked(env, obj));
3062 LASSERT(th != NULL);
3064 if (unlikely(fid_is_acct(fid)))
3065 /* Quota files can't be created from the kernel any more,
3066 * 'tune2fs -O quota' will take care of creating them */
3069 result = __osd_object_create(info, obj, attr, hint, dof, th);
3071 result = __osd_oi_insert(env, obj, fid, th);
3072 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3073 obj->oo_dt.do_body_ops = &osd_body_ops;
3075 LASSERT(ergo(result == 0,
3076 dt_object_exists(dt) && !dt_object_remote(dt)));
3078 LASSERT(osd_invariant(obj));
3083 * Called to destroy on-disk representation of the object
3085 * Concurrency: must be locked
3087 static int osd_declare_object_destroy(const struct lu_env *env,
3088 struct dt_object *dt,
3091 struct osd_object *obj = osd_dt_obj(dt);
3092 struct inode *inode = obj->oo_inode;
3093 struct osd_thandle *oh;
3100 oh = container_of0(th, struct osd_thandle, ot_super);
3101 LASSERT(oh->ot_handle == NULL);
3103 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3104 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3105 /* Recycle idle OI leaf may cause additional three OI blocks
3107 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3108 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3109 /* one less inode */
3110 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3111 -1, oh, obj, false, NULL, false);
3114 /* data to be truncated */
3115 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3116 0, oh, obj, true, NULL, false);
3120 /* will help to find FID->ino when this object is being
3121 * added to PENDING/ */
3122 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3127 static int osd_object_destroy(const struct lu_env *env,
3128 struct dt_object *dt,
3131 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3132 struct osd_object *obj = osd_dt_obj(dt);
3133 struct inode *inode = obj->oo_inode;
3134 struct osd_device *osd = osd_obj2dev(obj);
3135 struct osd_thandle *oh;
3139 oh = container_of0(th, struct osd_thandle, ot_super);
3140 LASSERT(oh->ot_handle);
3142 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3144 if (unlikely(fid_is_acct(fid)))
3147 if (S_ISDIR(inode->i_mode)) {
3148 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
3149 inode->i_nlink == 2);
3150 /* it will check/delete the inode from remote parent,
3151 * how to optimize it? unlink performance impaction XXX */
3152 result = osd_delete_from_remote_parent(env, osd, obj, oh);
3153 if (result != 0 && result != -ENOENT) {
3154 CERROR("%s: delete inode "DFID": rc = %d\n",
3155 osd_name(osd), PFID(fid), result);
3157 spin_lock(&obj->oo_guard);
3159 spin_unlock(&obj->oo_guard);
3160 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3163 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3165 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3166 result = osd_oi_delete(osd_oti_get(env), osd, fid, oh->ot_handle,
3169 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3170 /* XXX: add to ext3 orphan list */
3171 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3173 /* not needed in the cache anymore */
3174 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3175 obj->oo_destroyed = 1;
3181 * Put the fid into lustre_mdt_attrs, and then place the structure
3182 * inode's ea. This fid should not be altered during the life time
3185 * \retval +ve, on success
3186 * \retval -ve, on error
3188 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3190 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3191 const struct lu_fid *fid, __u32 compat, __u32 incompat)
3193 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3197 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3200 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3203 lustre_lma_init(lma, fid, compat, incompat);
3204 lustre_lma_swab(lma);
3206 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
3208 /* LMA may already exist, but we need to check that all the
3209 * desired compat/incompat flags have been added. */
3210 if (unlikely(rc == -EEXIST)) {
3211 if (compat == 0 && incompat == 0)
3214 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3215 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
3220 lustre_lma_swab(lma);
3221 if (!(~lma->lma_compat & compat) &&
3222 !(~lma->lma_incompat & incompat))
3225 lma->lma_compat |= compat;
3226 lma->lma_incompat |= incompat;
3227 lustre_lma_swab(lma);
3228 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3229 sizeof(*lma), XATTR_REPLACE);
3236 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3237 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3238 * To have compatilibility with 1.8 ldiskfs driver we need to have
3239 * magic number at start of fid data.
3240 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3243 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3244 const struct lu_fid *fid)
3246 if (!fid_is_namespace_visible(fid) ||
3247 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3248 param->edp_magic = 0;
3252 param->edp_magic = LDISKFS_LUFID_MAGIC;
3253 param->edp_len = sizeof(struct lu_fid) + 1;
3254 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3258 * Try to read the fid from inode ea into dt_rec.
3260 * \param fid object fid.
3262 * \retval 0 on success
3264 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3265 __u32 ino, struct lu_fid *fid,
3266 struct osd_inode_id *id)
3268 struct osd_thread_info *info = osd_oti_get(env);
3269 struct inode *inode;
3272 osd_id_gen(id, ino, OSD_OII_NOGEN);
3273 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3275 RETURN(PTR_ERR(inode));
3281 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3283 struct inode *parent_dir,
3284 const struct lu_fid *dot_fid,
3285 const struct lu_fid *dot_dot_fid,
3286 struct osd_thandle *oth)
3288 struct ldiskfs_dentry_param *dot_ldp;
3289 struct ldiskfs_dentry_param *dot_dot_ldp;
3290 __u32 saved_nlink = dir->i_nlink;
3293 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3294 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3296 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3297 dot_ldp->edp_magic = 0;
3299 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3300 dir, dot_ldp, dot_dot_ldp);
3301 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3302 * the subseqent ref_add() will increase the dir->i_nlink
3303 * as 3. That is incorrect for new created directory.
3305 * It looks like hack, because we want to make the OSD API
3306 * to be order-independent for new created directory object
3307 * between dt_insert(..) and ref_add() operations.
3309 * Here, we only restore the in-RAM dir-inode's nlink attr,
3310 * becuase if the nlink attr is not 2, then there will be
3311 * ref_add() called following the dt_insert(..), such call
3312 * will make both the in-RAM and on-disk dir-inode's nlink
3313 * attr to be set as 2. LU-7447 */
3314 set_nlink(dir, saved_nlink);
3319 * Create an local agent inode for remote entry
3321 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3322 struct osd_device *osd,
3323 struct osd_object *pobj,
3324 const struct lu_fid *fid,
3328 struct osd_thread_info *info = osd_oti_get(env);
3329 struct inode *local;
3330 struct osd_thandle *oh;
3335 oh = container_of(th, struct osd_thandle, ot_super);
3336 LASSERT(oh->ot_handle->h_transaction != NULL);
3338 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
3339 if (IS_ERR(local)) {
3340 CERROR("%s: create local error %d\n", osd_name(osd),
3341 (int)PTR_ERR(local));
3345 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3346 unlock_new_inode(local);
3348 /* Set special LMA flag for local agent inode */
3349 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3351 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3352 osd_name(osd), PFID(fid), rc);
3353 RETURN(ERR_PTR(rc));
3359 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3360 lu_object_fid(&pobj->oo_dt.do_lu),
3363 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3364 osd_name(osd), PFID(fid), rc);
3365 RETURN(ERR_PTR(rc));
3372 * when direntry is deleted, we have to take care of possible agent inode
3373 * referenced by that. unfortunately we can't do this at that point:
3374 * iget() within a running transaction leads to deadlock and we better do
3375 * not call that every delete declaration to save performance. so we put
3376 * a potention agent inode on a list and process that once the transaction
3377 * is over. Notice it's not any worse in terms of real orphans as regular
3378 * object destroy doesn't put inodes on the on-disk orphan list. this should
3379 * be addressed separately
3381 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3382 struct osd_thandle *oh,
3385 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3386 struct osd_obj_orphan *oor;
3393 oor->oor_env = (struct lu_env *)env;
3394 spin_lock(&osd->od_osfs_lock);
3395 list_add(&oor->oor_list, &osd->od_orphan_list);
3396 spin_unlock(&osd->od_osfs_lock);
3398 oh->ot_remove_agents = 1;
3404 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3405 struct osd_device *osd)
3407 struct osd_thread_info *info = osd_oti_get(env);
3408 struct osd_obj_orphan *oor, *tmp;
3409 struct osd_inode_id id;
3410 struct list_head list;
3411 struct inode *inode;
3416 INIT_LIST_HEAD(&list);
3418 spin_lock(&osd->od_osfs_lock);
3419 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3420 if (oor->oor_env == env) {
3421 list_del(&oor->oor_list);
3422 list_add(&oor->oor_list, &list);
3425 spin_unlock(&osd->od_osfs_lock);
3427 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3431 list_del(&oor->oor_list);
3434 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3435 inode = osd_iget_fid(info, osd, &id, &fid);
3439 if (!osd_remote_fid(env, osd, &fid)) {
3444 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3446 mark_inode_dirty(inode);
3447 ldiskfs_journal_stop(jh);
3455 * OSD layer object create function for interoperability mode (b11826).
3456 * This is mostly similar to osd_object_create(). Only difference being, fid is
3457 * inserted into inode ea here.
3459 * \retval 0, on success
3460 * \retval -ve, on error
3462 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
3463 struct lu_attr *attr,
3464 struct dt_allocation_hint *hint,
3465 struct dt_object_format *dof,
3468 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3469 struct osd_object *obj = osd_dt_obj(dt);
3470 struct osd_thread_info *info = osd_oti_get(env);
3471 int result, on_ost = 0;
3475 if (dt_object_exists(dt))
3478 LASSERT(osd_invariant(obj));
3479 LASSERT(!dt_object_remote(dt));
3480 LASSERT(osd_write_locked(env, obj));
3481 LASSERT(th != NULL);
3483 if (unlikely(fid_is_acct(fid)))
3484 /* Quota files can't be created from the kernel any more,
3485 * 'tune2fs -O quota' will take care of creating them */
3488 result = __osd_object_create(info, obj, attr, hint, dof, th);
3490 if (fid_is_idif(fid) &&
3491 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3492 struct lu_fid *tfid = &info->oti_fid;
3493 struct ost_id *oi = &info->oti_ostid;
3495 fid_to_ostid(fid, oi);
3496 ostid_to_fid(tfid, oi, 0);
3498 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3499 LMAC_FID_ON_OST, 0);
3501 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3503 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3504 on_ost ? LMAC_FID_ON_OST : 0,
3507 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3508 obj->oo_dt.do_body_ops = &osd_body_ops;
3512 result = __osd_oi_insert(env, obj, fid, th);
3514 /* a small optimization - dt_insert() isn't usually applied
3515 * to OST objects, so we don't need to cache OI mapping for
3517 if (result == 0 && on_ost == 0) {
3518 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3519 result = osd_idc_find_and_init(env, osd, obj);
3520 LASSERT(result == 0);
3523 LASSERT(ergo(result == 0,
3524 dt_object_exists(dt) && !dt_object_remote(dt)));
3525 LINVRNT(osd_invariant(obj));
3529 static int osd_declare_object_ref_add(const struct lu_env *env,
3530 struct dt_object *dt,
3531 struct thandle *handle)
3533 struct osd_thandle *oh;
3535 /* it's possible that object doesn't exist yet */
3536 LASSERT(handle != NULL);
3538 oh = container_of0(handle, struct osd_thandle, ot_super);
3539 LASSERT(oh->ot_handle == NULL);
3541 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3542 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3548 * Concurrency: @dt is write locked.
3550 static int osd_object_ref_add(const struct lu_env *env,
3551 struct dt_object *dt, struct thandle *th)
3553 struct osd_object *obj = osd_dt_obj(dt);
3554 struct inode *inode = obj->oo_inode;
3555 struct osd_thandle *oh;
3558 if (!dt_object_exists(dt) || obj->oo_destroyed)
3561 LINVRNT(osd_invariant(obj));
3562 LASSERT(!dt_object_remote(dt));
3563 LASSERT(osd_write_locked(env, obj));
3564 LASSERT(th != NULL);
3566 oh = container_of0(th, struct osd_thandle, ot_super);
3567 LASSERT(oh->ot_handle != NULL);
3569 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3571 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3572 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3574 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3575 * (65000) subdirectories by storing "1" in i_nlink if the link count
3576 * would otherwise overflow. Directory tranversal tools understand
3577 * that (st_nlink == 1) indicates that the filesystem dose not track
3578 * hard links count on the directory, and will not abort subdirectory
3579 * scanning early once (st_nlink - 2) subdirs have been found.
3581 * This also has to properly handle the case of inodes with nlink == 0
3582 * in case they are being linked into the PENDING directory
3584 spin_lock(&obj->oo_guard);
3585 if (unlikely(inode->i_nlink == 0))
3586 /* inc_nlink from 0 may cause WARN_ON */
3587 set_nlink(inode, 1);
3589 ldiskfs_inc_count(oh->ot_handle, inode);
3590 if (!S_ISDIR(inode->i_mode))
3591 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3593 spin_unlock(&obj->oo_guard);
3595 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3596 LINVRNT(osd_invariant(obj));
3598 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3603 static int osd_declare_object_ref_del(const struct lu_env *env,
3604 struct dt_object *dt,
3605 struct thandle *handle)
3607 struct osd_thandle *oh;
3609 if (!dt_object_exists(dt))
3612 LASSERT(!dt_object_remote(dt));
3613 LASSERT(handle != NULL);
3615 oh = container_of0(handle, struct osd_thandle, ot_super);
3616 LASSERT(oh->ot_handle == NULL);
3618 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3619 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3625 * Concurrency: @dt is write locked.
3627 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
3630 struct osd_object *obj = osd_dt_obj(dt);
3631 struct inode *inode = obj->oo_inode;
3632 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3633 struct osd_thandle *oh;
3635 if (!dt_object_exists(dt))
3638 LINVRNT(osd_invariant(obj));
3639 LASSERT(!dt_object_remote(dt));
3640 LASSERT(osd_write_locked(env, obj));
3641 LASSERT(th != NULL);
3643 oh = container_of0(th, struct osd_thandle, ot_super);
3644 LASSERT(oh->ot_handle != NULL);
3646 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3648 spin_lock(&obj->oo_guard);
3649 /* That can be result of upgrade from old Lustre version and
3650 * applied only to local files. Just skip this ref_del call.
3651 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3652 if (inode->i_nlink == 0) {
3653 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3654 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3655 ", maybe an upgraded file? (LU-3915)\n",
3656 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3657 spin_unlock(&obj->oo_guard);
3661 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3662 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3664 ldiskfs_dec_count(oh->ot_handle, inode);
3665 spin_unlock(&obj->oo_guard);
3667 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3668 LINVRNT(osd_invariant(obj));
3670 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3676 * Get the 64-bit version for an inode.
3678 static int osd_object_version_get(const struct lu_env *env,
3679 struct dt_object *dt, dt_obj_version_t *ver)
3681 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3683 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
3684 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3685 *ver = LDISKFS_I(inode)->i_fs_version;
3690 * Concurrency: @dt is read locked.
3692 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3693 struct lu_buf *buf, const char *name)
3695 struct osd_object *obj = osd_dt_obj(dt);
3696 struct inode *inode = obj->oo_inode;
3697 struct osd_thread_info *info = osd_oti_get(env);
3698 struct dentry *dentry = &info->oti_obj_dentry;
3699 bool cache_xattr = false;
3702 /* version get is not real XATTR but uses xattr API */
3703 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3704 /* for version we are just using xattr API but change inode
3706 if (buf->lb_len == 0)
3707 return sizeof(dt_obj_version_t);
3709 if (buf->lb_len < sizeof(dt_obj_version_t))
3712 osd_object_version_get(env, dt, buf->lb_buf);
3714 return sizeof(dt_obj_version_t);
3717 if (!dt_object_exists(dt))
3720 LASSERT(!dt_object_remote(dt));
3721 LASSERT(inode->i_op != NULL);
3722 LASSERT(inode->i_op->getxattr != NULL);
3724 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
3725 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
3729 rc = osd_oxc_get(obj, name, buf);
3734 rc = __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3736 if (rc == -ENOENT || rc == -ENODATA)
3737 osd_oxc_add(obj, name, NULL, 0);
3738 else if (rc > 0 && buf->lb_buf != NULL)
3739 osd_oxc_add(obj, name, buf->lb_buf, rc);
3745 static int osd_declare_xattr_set(const struct lu_env *env,
3746 struct dt_object *dt,
3747 const struct lu_buf *buf, const char *name,
3748 int fl, struct thandle *handle)
3750 struct osd_thandle *oh;
3752 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3754 LASSERT(handle != NULL);
3756 oh = container_of0(handle, struct osd_thandle, ot_super);
3757 LASSERT(oh->ot_handle == NULL);
3759 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3760 /* For non-upgrading case, the LMA is set first and
3761 * usually fit inode. But for upgrade case, the LMA
3762 * may be in another separated EA block. */
3763 if (!dt_object_exists(dt))
3765 else if (fl == LU_XATTR_REPLACE)
3769 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3773 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3778 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3779 /* learn xattr size from osd_xattr_get if
3780 attribute has not been read yet */
3781 buflen = __osd_xattr_get(
3782 osd_dt_obj(dt)->oo_inode,
3783 &osd_oti_get(env)->oti_obj_dentry,
3788 buflen = buf->lb_len;
3791 if (buflen > sb->s_blocksize) {
3792 credits += osd_calc_bkmap_credits(
3794 (buflen + sb->s_blocksize - 1) >>
3795 sb->s_blocksize_bits);
3799 * xattr set may involve inode quota change, reserve credits for
3800 * dquot_initialize()
3802 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3805 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3811 * Set the 64-bit version for object
3813 static void osd_object_version_set(const struct lu_env *env,
3814 struct dt_object *dt,
3815 dt_obj_version_t *new_version)
3817 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3819 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
3820 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3822 LDISKFS_I(inode)->i_fs_version = *new_version;
3823 /** Version is set after all inode operations are finished,
3824 * so we should mark it dirty here */
3825 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3829 * Concurrency: @dt is write locked.
3831 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3832 const struct lu_buf *buf, const char *name, int fl,
3833 struct thandle *handle)
3835 struct osd_object *obj = osd_dt_obj(dt);
3836 struct inode *inode = obj->oo_inode;
3837 struct osd_thread_info *info = osd_oti_get(env);
3842 LASSERT(handle != NULL);
3844 /* version set is not real XATTR */
3845 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3846 /* for version we are just using xattr API but change inode
3848 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3849 osd_object_version_set(env, dt, buf->lb_buf);
3850 return sizeof(dt_obj_version_t);
3853 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3854 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3856 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3857 if (fl & LU_XATTR_REPLACE)
3858 fs_flags |= XATTR_REPLACE;
3860 if (fl & LU_XATTR_CREATE)
3861 fs_flags |= XATTR_CREATE;
3863 if (strcmp(name, XATTR_NAME_LMV) == 0) {
3864 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3867 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3871 lma->lma_incompat |= LMAI_STRIPED;
3872 lustre_lma_swab(lma);
3873 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3874 sizeof(*lma), XATTR_REPLACE);
3879 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
3880 strcmp(name, XATTR_NAME_LINK) == 0)
3883 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3885 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3888 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3889 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
3890 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
3896 * Concurrency: @dt is read locked.
3898 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3899 const struct lu_buf *buf)
3901 struct osd_object *obj = osd_dt_obj(dt);
3902 struct inode *inode = obj->oo_inode;
3903 struct osd_thread_info *info = osd_oti_get(env);
3904 struct dentry *dentry = &info->oti_obj_dentry;
3906 if (!dt_object_exists(dt))
3909 LASSERT(!dt_object_remote(dt));
3910 LASSERT(inode->i_op != NULL);
3911 LASSERT(inode->i_op->listxattr != NULL);
3913 dentry->d_inode = inode;
3914 dentry->d_sb = inode->i_sb;
3915 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3918 static int osd_declare_xattr_del(const struct lu_env *env,
3919 struct dt_object *dt, const char *name,
3920 struct thandle *handle)
3922 struct osd_thandle *oh;
3923 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3925 LASSERT(!dt_object_remote(dt));
3926 LASSERT(handle != NULL);
3928 oh = container_of0(handle, struct osd_thandle, ot_super);
3929 LASSERT(oh->ot_handle == NULL);
3931 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3932 osd_dto_credits_noquota[DTO_XATTR_SET]);
3934 * xattr del may involve inode quota change, reserve credits for
3935 * dquot_initialize()
3937 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3943 * Concurrency: @dt is write locked.
3945 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3946 const char *name, struct thandle *handle)
3948 struct osd_object *obj = osd_dt_obj(dt);
3949 struct inode *inode = obj->oo_inode;
3950 struct osd_thread_info *info = osd_oti_get(env);
3951 struct dentry *dentry = &info->oti_obj_dentry;
3954 if (!dt_object_exists(dt))
3957 LASSERT(!dt_object_remote(dt));
3958 LASSERT(inode->i_op != NULL);
3959 LASSERT(inode->i_op->removexattr != NULL);
3960 LASSERT(handle != NULL);
3962 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3964 ll_vfs_dq_init(inode);
3965 dentry->d_inode = inode;
3966 dentry->d_sb = inode->i_sb;
3967 rc = inode->i_op->removexattr(dentry, name);
3968 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3971 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3972 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
3973 osd_oxc_del(obj, name);
3978 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
3979 __u64 start, __u64 end)
3981 struct osd_object *obj = osd_dt_obj(dt);
3982 struct inode *inode = obj->oo_inode;
3983 struct osd_thread_info *info = osd_oti_get(env);
3984 struct dentry *dentry = &info->oti_obj_dentry;
3985 struct file *file = &info->oti_file;
3990 dentry->d_inode = inode;
3991 dentry->d_sb = inode->i_sb;
3992 file->f_path.dentry = dentry;
3993 file->f_mapping = inode->i_mapping;
3994 file->f_op = inode->i_fop;
3995 set_file_inode(file, inode);
3997 rc = ll_vfs_fsync_range(file, start, end, 0);
4002 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4011 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4012 const struct dt_index_features *feat)
4014 struct iam_descr *descr;
4016 if (osd_object_is_root(o))
4017 return feat == &dt_directory_features;
4019 LASSERT(o->oo_dir != NULL);
4021 descr = o->oo_dir->od_container.ic_descr;
4022 if (feat == &dt_directory_features) {
4023 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4029 feat->dif_keysize_min <= descr->id_key_size &&
4030 descr->id_key_size <= feat->dif_keysize_max &&
4031 feat->dif_recsize_min <= descr->id_rec_size &&
4032 descr->id_rec_size <= feat->dif_recsize_max &&
4033 !(feat->dif_flags & (DT_IND_VARKEY |
4034 DT_IND_VARREC | DT_IND_NONUNQ)) &&
4035 ergo(feat->dif_flags & DT_IND_UPDATE,
4036 1 /* XXX check that object (and file system) is
4041 static int osd_iam_container_init(const struct lu_env *env,
4042 struct osd_object *obj,
4043 struct osd_directory *dir)
4045 struct iam_container *bag = &dir->od_container;
4048 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
4052 result = iam_container_setup(bag);
4054 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
4056 iam_container_fini(bag);
4063 * Concurrency: no external locking is necessary.
4065 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
4066 const struct dt_index_features *feat)
4070 struct osd_object *obj = osd_dt_obj(dt);
4072 LINVRNT(osd_invariant(obj));
4074 if (osd_object_is_root(obj)) {
4075 dt->do_index_ops = &osd_index_ea_ops;
4077 } else if (feat == &dt_directory_features) {
4078 dt->do_index_ops = &osd_index_ea_ops;
4079 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
4084 } else if (unlikely(feat == &dt_otable_features)) {
4085 dt->do_index_ops = &osd_otable_ops;
4087 } else if (unlikely(feat == &dt_acct_features)) {
4088 dt->do_index_ops = &osd_acct_index_ops;
4091 } else if (!osd_has_index(obj)) {
4092 struct osd_directory *dir;
4097 spin_lock(&obj->oo_guard);
4098 if (obj->oo_dir == NULL)
4102 * Concurrent thread allocated container data.
4105 spin_unlock(&obj->oo_guard);
4107 * Now, that we have container data, serialize its
4110 down_write(&obj->oo_ext_idx_sem);
4112 * recheck under lock.
4114 if (!osd_has_index(obj))
4115 result = osd_iam_container_init(env, obj,
4119 up_write(&obj->oo_ext_idx_sem);
4127 if (result == 0 && skip_iam == 0) {
4128 if (!osd_iam_index_probe(env, obj, feat))
4131 LINVRNT(osd_invariant(obj));
4133 if (result == 0 && feat == &dt_quota_glb_features &&
4134 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
4135 result = osd_quota_migration(env, dt);
4140 static int osd_otable_it_attr_get(const struct lu_env *env,
4141 struct dt_object *dt,
4142 struct lu_attr *attr)
4148 static const struct dt_object_operations osd_obj_ops = {
4149 .do_read_lock = osd_object_read_lock,
4150 .do_write_lock = osd_object_write_lock,
4151 .do_read_unlock = osd_object_read_unlock,
4152 .do_write_unlock = osd_object_write_unlock,
4153 .do_write_locked = osd_object_write_locked,
4154 .do_attr_get = osd_attr_get,
4155 .do_declare_attr_set = osd_declare_attr_set,
4156 .do_attr_set = osd_attr_set,
4157 .do_ah_init = osd_ah_init,
4158 .do_declare_create = osd_declare_object_create,
4159 .do_create = osd_object_create,
4160 .do_declare_destroy = osd_declare_object_destroy,
4161 .do_destroy = osd_object_destroy,
4162 .do_index_try = osd_index_try,
4163 .do_declare_ref_add = osd_declare_object_ref_add,
4164 .do_ref_add = osd_object_ref_add,
4165 .do_declare_ref_del = osd_declare_object_ref_del,
4166 .do_ref_del = osd_object_ref_del,
4167 .do_xattr_get = osd_xattr_get,
4168 .do_declare_xattr_set = osd_declare_xattr_set,
4169 .do_xattr_set = osd_xattr_set,
4170 .do_declare_xattr_del = osd_declare_xattr_del,
4171 .do_xattr_del = osd_xattr_del,
4172 .do_xattr_list = osd_xattr_list,
4173 .do_object_sync = osd_object_sync,
4174 .do_invalidate = osd_invalidate,
4178 * dt_object_operations for interoperability mode
4179 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4181 static const struct dt_object_operations osd_obj_ea_ops = {
4182 .do_read_lock = osd_object_read_lock,
4183 .do_write_lock = osd_object_write_lock,
4184 .do_read_unlock = osd_object_read_unlock,
4185 .do_write_unlock = osd_object_write_unlock,
4186 .do_write_locked = osd_object_write_locked,
4187 .do_attr_get = osd_attr_get,
4188 .do_declare_attr_set = osd_declare_attr_set,
4189 .do_attr_set = osd_attr_set,
4190 .do_ah_init = osd_ah_init,
4191 .do_declare_create = osd_declare_object_create,
4192 .do_create = osd_object_ea_create,
4193 .do_declare_destroy = osd_declare_object_destroy,
4194 .do_destroy = osd_object_destroy,
4195 .do_index_try = osd_index_try,
4196 .do_declare_ref_add = osd_declare_object_ref_add,
4197 .do_ref_add = osd_object_ref_add,
4198 .do_declare_ref_del = osd_declare_object_ref_del,
4199 .do_ref_del = osd_object_ref_del,
4200 .do_xattr_get = osd_xattr_get,
4201 .do_declare_xattr_set = osd_declare_xattr_set,
4202 .do_xattr_set = osd_xattr_set,
4203 .do_declare_xattr_del = osd_declare_xattr_del,
4204 .do_xattr_del = osd_xattr_del,
4205 .do_xattr_list = osd_xattr_list,
4206 .do_object_sync = osd_object_sync,
4209 static const struct dt_object_operations osd_obj_otable_it_ops = {
4210 .do_attr_get = osd_otable_it_attr_get,
4211 .do_index_try = osd_index_try,
4214 static int osd_index_declare_iam_delete(const struct lu_env *env,
4215 struct dt_object *dt,
4216 const struct dt_key *key,
4217 struct thandle *handle)
4219 struct osd_thandle *oh;
4221 oh = container_of0(handle, struct osd_thandle, ot_super);
4222 LASSERT(oh->ot_handle == NULL);
4224 /* Recycle may cause additional three blocks to be changed. */
4225 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
4226 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
4232 * delete a (key, value) pair from index \a dt specified by \a key
4234 * \param dt osd index object
4235 * \param key key for index
4236 * \param rec record reference
4237 * \param handle transaction handler
4240 * \retval -ve failure
4242 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
4243 const struct dt_key *key,
4244 struct thandle *handle)
4246 struct osd_thread_info *oti = osd_oti_get(env);
4247 struct osd_object *obj = osd_dt_obj(dt);
4248 struct osd_thandle *oh;
4249 struct iam_path_descr *ipd;
4250 struct iam_container *bag = &obj->oo_dir->od_container;
4254 if (!dt_object_exists(dt))
4257 LINVRNT(osd_invariant(obj));
4258 LASSERT(!dt_object_remote(dt));
4259 LASSERT(bag->ic_object == obj->oo_inode);
4260 LASSERT(handle != NULL);
4262 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4264 ipd = osd_idx_ipd_get(env, bag);
4265 if (unlikely(ipd == NULL))
4268 oh = container_of0(handle, struct osd_thandle, ot_super);
4269 LASSERT(oh->ot_handle != NULL);
4270 LASSERT(oh->ot_handle->h_transaction != NULL);
4272 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4273 /* swab quota uid/gid provided by caller */
4274 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4275 key = (const struct dt_key *)&oti->oti_quota_id;
4278 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
4279 osd_ipd_put(env, bag, ipd);
4280 LINVRNT(osd_invariant(obj));
4281 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4285 static int osd_index_declare_ea_delete(const struct lu_env *env,
4286 struct dt_object *dt,
4287 const struct dt_key *key,
4288 struct thandle *handle)
4290 struct osd_thandle *oh;
4291 struct inode *inode;
4295 LASSERT(!dt_object_remote(dt));
4296 LASSERT(handle != NULL);
4298 oh = container_of0(handle, struct osd_thandle, ot_super);
4299 LASSERT(oh->ot_handle == NULL);
4301 /* due to DNE we may need to remove an agent inode */
4302 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
4303 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4304 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
4306 inode = osd_dt_obj(dt)->oo_inode;
4310 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
4311 0, oh, osd_dt_obj(dt), true, NULL, false);
4315 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
4318 struct osd_fid_pack *rec;
4321 if (de->file_type & LDISKFS_DIRENT_LUFID) {
4322 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
4323 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
4324 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
4330 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
4331 const struct lu_fid *fid)
4333 struct seq_server_site *ss = osd_seq_site(osd);
4336 /* FID seqs not in FLDB, must be local seq */
4337 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
4340 /* If FLD is not being initialized yet, it only happens during the
4341 * initialization, likely during mgs initialization, and we assume
4342 * this is local FID. */
4343 if (ss == NULL || ss->ss_server_fld == NULL)
4346 /* Only check the local FLDB here */
4347 if (osd_seq_exists(env, osd, fid_seq(fid)))
4354 * Index delete function for interoperability mode (b11826).
4355 * It will remove the directory entry added by osd_index_ea_insert().
4356 * This entry is needed to maintain name->fid mapping.
4358 * \param key, key i.e. file entry to be deleted
4360 * \retval 0, on success
4361 * \retval -ve, on error
4363 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
4364 const struct dt_key *key, struct thandle *handle)
4366 struct osd_object *obj = osd_dt_obj(dt);
4367 struct inode *dir = obj->oo_inode;
4368 struct dentry *dentry;
4369 struct osd_thandle *oh;
4370 struct ldiskfs_dir_entry_2 *de = NULL;
4371 struct buffer_head *bh;
4372 struct htree_lock *hlock = NULL;
4373 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
4374 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4378 if (!dt_object_exists(dt))
4381 LINVRNT(osd_invariant(obj));
4382 LASSERT(!dt_object_remote(dt));
4383 LASSERT(handle != NULL);
4385 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4387 oh = container_of(handle, struct osd_thandle, ot_super);
4388 LASSERT(oh->ot_handle != NULL);
4389 LASSERT(oh->ot_handle->h_transaction != NULL);
4391 ll_vfs_dq_init(dir);
4392 dentry = osd_child_dentry_get(env, obj,
4393 (char *)key, strlen((char *)key));
4395 if (obj->oo_hl_head != NULL) {
4396 hlock = osd_oti_get(env)->oti_hlock;
4397 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4398 dir, LDISKFS_HLOCK_DEL);
4400 down_write(&obj->oo_ext_idx_sem);
4403 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4405 /* If this is not the ".." entry, it might be a remote DNE
4406 * entry and we need to check if the FID is for a remote
4407 * MDT. If the FID is not in the directory entry (e.g.
4408 * upgraded 1.8 filesystem without dirdata enabled) then
4409 * we need to get the FID from the LMA. For a remote directory
4410 * there HAS to be an LMA, it cannot be an IGIF inode in this
4413 * Delete the entry before the agent inode in order to
4414 * simplify error handling. At worst an error after deleting
4415 * the entry first might leak the agent inode afterward. The
4416 * reverse would need filesystem abort in case of error deleting
4417 * the entry after the agent had been removed, or leave a
4418 * dangling entry pointing at a random inode. */
4419 if (strcmp((char *)key, dotdot) != 0) {
4420 LASSERT(de != NULL);
4421 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4422 if (rc == -ENODATA) {
4423 /* can't get FID, postpone to the end of the
4424 * transaction when iget() is safe */
4425 osd_schedule_agent_inode_removal(env, oh,
4426 le32_to_cpu(de->inode));
4427 } else if (rc == 0 &&
4428 unlikely(osd_remote_fid(env, osd, fid))) {
4429 osd_schedule_agent_inode_removal(env, oh,
4430 le32_to_cpu(de->inode));
4433 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4439 ldiskfs_htree_unlock(hlock);
4441 up_write(&obj->oo_ext_idx_sem);
4446 /* For inode on the remote MDT, .. will point to
4447 * /Agent directory, Check whether it needs to delete
4448 * from agent directory */
4449 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4450 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
4452 if (rc != 0 && rc != -ENOENT) {
4453 CERROR("%s: delete agent inode "DFID": rc = %d\n",
4454 osd_name(osd), PFID(fid), rc);
4464 LASSERT(osd_invariant(obj));
4465 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4470 * Lookup index for \a key and copy record to \a rec.
4472 * \param dt osd index object
4473 * \param key key for index
4474 * \param rec record reference
4476 * \retval +ve success : exact mach
4477 * \retval 0 return record with key not greater than \a key
4478 * \retval -ve failure
4480 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4481 struct dt_rec *rec, const struct dt_key *key)
4483 struct osd_object *obj = osd_dt_obj(dt);
4484 struct iam_path_descr *ipd;
4485 struct iam_container *bag = &obj->oo_dir->od_container;
4486 struct osd_thread_info *oti = osd_oti_get(env);
4487 struct iam_iterator *it = &oti->oti_idx_it;
4488 struct iam_rec *iam_rec;
4492 if (!dt_object_exists(dt))
4495 LASSERT(osd_invariant(obj));
4496 LASSERT(!dt_object_remote(dt));
4497 LASSERT(bag->ic_object == obj->oo_inode);
4499 ipd = osd_idx_ipd_get(env, bag);
4503 /* got ipd now we can start iterator. */
4504 iam_it_init(it, bag, 0, ipd);
4506 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4507 /* swab quota uid/gid provided by caller */
4508 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4509 key = (const struct dt_key *)&oti->oti_quota_id;
4512 rc = iam_it_get(it, (struct iam_key *)key);
4514 if (S_ISDIR(obj->oo_inode->i_mode))
4515 iam_rec = (struct iam_rec *)oti->oti_ldp;
4517 iam_rec = (struct iam_rec *) rec;
4519 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4521 if (S_ISDIR(obj->oo_inode->i_mode))
4522 osd_fid_unpack((struct lu_fid *) rec,
4523 (struct osd_fid_pack *)iam_rec);
4524 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4525 osd_quota_unpack(obj, rec);
4530 osd_ipd_put(env, bag, ipd);
4532 LINVRNT(osd_invariant(obj));
4537 static int osd_index_declare_iam_insert(const struct lu_env *env,
4538 struct dt_object *dt,
4539 const struct dt_rec *rec,
4540 const struct dt_key *key,
4541 struct thandle *handle)
4543 struct osd_thandle *oh;
4545 LASSERT(handle != NULL);
4547 oh = container_of0(handle, struct osd_thandle, ot_super);
4548 LASSERT(oh->ot_handle == NULL);
4550 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4551 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4557 * Inserts (key, value) pair in \a dt index object.
4559 * \param dt osd index object
4560 * \param key key for index
4561 * \param rec record reference
4562 * \param th transaction handler
4565 * \retval -ve failure
4567 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4568 const struct dt_rec *rec,
4569 const struct dt_key *key, struct thandle *th,
4572 struct osd_object *obj = osd_dt_obj(dt);
4573 struct iam_path_descr *ipd;
4574 struct osd_thandle *oh;
4575 struct iam_container *bag;
4576 struct osd_thread_info *oti = osd_oti_get(env);
4577 struct iam_rec *iam_rec;
4581 if (!dt_object_exists(dt))
4584 LINVRNT(osd_invariant(obj));
4585 LASSERT(!dt_object_remote(dt));
4587 bag = &obj->oo_dir->od_container;
4588 LASSERT(bag->ic_object == obj->oo_inode);
4589 LASSERT(th != NULL);
4591 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4593 ipd = osd_idx_ipd_get(env, bag);
4594 if (unlikely(ipd == NULL))
4597 oh = container_of0(th, struct osd_thandle, ot_super);
4598 LASSERT(oh->ot_handle != NULL);
4599 LASSERT(oh->ot_handle->h_transaction != NULL);
4600 if (S_ISDIR(obj->oo_inode->i_mode)) {
4601 iam_rec = (struct iam_rec *)oti->oti_ldp;
4602 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4603 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4604 /* pack quota uid/gid */
4605 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4606 key = (const struct dt_key *)&oti->oti_quota_id;
4607 /* pack quota record */
4608 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4609 iam_rec = (struct iam_rec *)rec;
4611 iam_rec = (struct iam_rec *)rec;
4614 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4616 osd_ipd_put(env, bag, ipd);
4617 LINVRNT(osd_invariant(obj));
4618 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4623 * Calls ldiskfs_add_entry() to add directory entry
4624 * into the directory. This is required for
4625 * interoperability mode (b11826)
4627 * \retval 0, on success
4628 * \retval -ve, on error
4630 static int __osd_ea_add_rec(struct osd_thread_info *info,
4631 struct osd_object *pobj, struct inode *cinode,
4632 const char *name, const struct lu_fid *fid,
4633 struct htree_lock *hlock, struct thandle *th)
4635 struct ldiskfs_dentry_param *ldp;
4636 struct dentry *child;
4637 struct osd_thandle *oth;
4640 oth = container_of(th, struct osd_thandle, ot_super);
4641 LASSERT(oth->ot_handle != NULL);
4642 LASSERT(oth->ot_handle->h_transaction != NULL);
4643 LASSERT(pobj->oo_inode);
4645 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4646 if (unlikely(pobj->oo_inode ==
4647 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4650 osd_get_ldiskfs_dirent_param(ldp, fid);
4651 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4652 child->d_fsdata = (void *)ldp;
4653 ll_vfs_dq_init(pobj->oo_inode);
4654 rc = osd_ldiskfs_add_entry(info, oth->ot_handle, child,
4656 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4657 struct ldiskfs_dir_entry_2 *de;
4658 struct buffer_head *bh;
4661 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4664 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4667 if (S_ISDIR(cinode->i_mode))
4668 de->file_type = LDISKFS_DIRENT_LUFID |
4669 LDISKFS_FT_REG_FILE;
4671 de->file_type = LDISKFS_DIRENT_LUFID |
4673 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4684 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4685 * into the directory.Also sets flags into osd object to
4686 * indicate dot and dotdot are created. This is required for
4687 * interoperability mode (b11826)
4689 * \param dir directory for dot and dotdot fixup.
4690 * \param obj child object for linking
4692 * \retval 0, on success
4693 * \retval -ve, on error
4695 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4696 struct osd_object *dir,
4697 struct inode *parent_dir, const char *name,
4698 const struct lu_fid *dot_fid,
4699 const struct lu_fid *dot_dot_fid,
4702 struct inode *inode = dir->oo_inode;
4703 struct osd_thandle *oth;
4706 oth = container_of(th, struct osd_thandle, ot_super);
4707 LASSERT(oth->ot_handle->h_transaction != NULL);
4708 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4710 if (strcmp(name, dot) == 0) {
4711 if (dir->oo_compat_dot_created) {
4714 LASSERT(inode->i_ino == parent_dir->i_ino);
4715 dir->oo_compat_dot_created = 1;
4718 } else if (strcmp(name, dotdot) == 0) {
4719 if (!dir->oo_compat_dot_created)
4721 /* in case of rename, dotdot is already created */
4722 if (dir->oo_compat_dotdot_created) {
4723 return __osd_ea_add_rec(info, dir, parent_dir, name,
4724 dot_dot_fid, NULL, th);
4727 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4728 struct lu_fid tfid = *dot_dot_fid;
4731 result = osd_add_dot_dotdot_internal(info,
4732 dir->oo_inode, parent_dir, dot_fid,
4735 result = osd_add_dot_dotdot_internal(info,
4736 dir->oo_inode, parent_dir, dot_fid,
4741 dir->oo_compat_dotdot_created = 1;
4749 * It will call the appropriate osd_add* function and return the
4750 * value, return by respective functions.
4752 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4753 struct inode *cinode, const char *name,
4754 const struct lu_fid *fid, struct thandle *th)
4756 struct osd_thread_info *info = osd_oti_get(env);
4757 struct htree_lock *hlock;
4760 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4762 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4764 if (hlock != NULL) {
4765 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4768 down_write(&pobj->oo_ext_idx_sem);
4771 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4772 lu_object_fid(&pobj->oo_dt.do_lu),
4775 if (hlock != NULL) {
4776 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4777 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4779 down_write(&pobj->oo_ext_idx_sem);
4782 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4783 struct lu_fid *tfid = &info->oti_fid;
4787 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4790 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4795 ldiskfs_htree_unlock(hlock);
4797 up_write(&pobj->oo_ext_idx_sem);
4803 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4804 struct osd_idmap_cache *oic)
4806 struct osd_scrub *scrub = &dev->od_scrub;
4807 struct lu_fid *fid = &oic->oic_fid;
4808 struct osd_inode_id *id = &oic->oic_lid;
4809 struct inode *inode = NULL;
4814 if (!fid_is_norm(fid) && !fid_is_igif(fid))
4817 if (scrub->os_pos_current > id->oii_ino)
4821 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
4822 if (rc == -ENOENT) {
4823 __u32 gen = id->oii_gen;
4828 inode = osd_iget(oti, dev, id);
4829 /* The inode has been removed (by race maybe). */
4830 if (IS_ERR(inode)) {
4831 rc = PTR_ERR(inode);
4833 RETURN(rc == -ESTALE ? -ENOENT : rc);
4836 /* The OI mapping is lost. */
4837 if (gen != OSD_OII_NOGEN)
4841 /* The inode may has been reused by others, we do not know,
4842 * leave it to be handled by subsequent osd_fid_lookup(). */
4844 } else if (rc != 0 || osd_id_eq(id, &oti->oti_id)) {
4849 if (thread_is_running(&scrub->os_thread)) {
4850 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
4851 /* There is race condition between osd_oi_lookup and OI scrub.
4852 * The OI scrub finished just after osd_oi_lookup() failure.
4853 * Under such case, it is unnecessary to trigger OI scrub again,
4854 * but try to call osd_oi_lookup() again. */
4855 if (unlikely(rc == -EAGAIN))
4858 if (inode == NULL) {
4859 inode = osd_iget(oti, dev, id);
4860 /* The inode has been removed (by race maybe). */
4861 if (IS_ERR(inode)) {
4862 rc = PTR_ERR(inode);
4864 RETURN(rc == -ESTALE ? -ENOENT : rc);
4868 if (!S_ISDIR(inode->i_mode))
4871 rc = osd_check_lmv(oti, dev, inode, oic);
4877 if (!dev->od_noscrub && ++once == 1) {
4878 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4880 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
4881 "%.16s: trigger partial OI scrub for RPC inconsistency "
4882 "checking FID "DFID": rc = %d\n",
4883 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4885 if (rc == 0 || rc == -EALREADY)
4895 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4896 struct osd_device *dev,
4897 struct osd_idmap_cache *oic,
4898 struct lu_fid *fid, __u32 ino)
4900 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
4901 struct inode *inode;
4904 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4905 inode = osd_iget(oti, dev, &oic->oic_lid);
4906 if (IS_ERR(inode)) {
4907 fid_zero(&oic->oic_fid);
4908 return PTR_ERR(inode);
4911 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4914 fid_zero(&oic->oic_fid);
4916 *fid = oic->oic_fid = lma->lma_self_fid;
4920 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4921 struct osd_inode_id *id, const struct lu_fid *fid)
4923 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4924 id->oii_ino, id->oii_gen, info);
4925 info->oti_cache.oic_lid = *id;
4926 info->oti_cache.oic_fid = *fid;
4927 info->oti_cache.oic_dev = osd;
4931 * Get parent FID from the linkEA.
4933 * For a directory which parent resides on remote MDT, to satisfy the
4934 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
4935 * the other hand, to make the lookup(..) on the directory can return
4936 * the real parent FID, we append the real parent FID after its ".."
4937 * name entry in the /REMOTE_PARENT_DIR.
4939 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4940 * backup. So after the restore, we cannot get the right parent FID from
4941 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4942 * we have stored the real parent FID in the directory object's linkEA,
4943 * we can parse the linkEA for the real parent FID.
4945 * \param[in] env pointer to the thread context
4946 * \param[in] obj pointer to the object to be handled
4947 * \param[out]fid pointer to the buffer to hold the parent FID
4949 * \retval 0 for getting the real parent FID successfully
4950 * \retval negative error number on failure
4952 static int osd_get_pfid_from_linkea(const struct lu_env *env,
4953 struct osd_object *obj,
4956 struct osd_thread_info *oti = osd_oti_get(env);
4957 struct lu_buf *buf = &oti->oti_big_buf;
4958 struct dentry *dentry = &oti->oti_obj_dentry;
4959 struct inode *inode = obj->oo_inode;
4960 struct linkea_data ldata = { NULL };
4965 if (!S_ISDIR(inode->i_mode))
4969 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4970 buf->lb_buf, buf->lb_len);
4971 if (rc == -ERANGE) {
4972 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4975 lu_buf_realloc(buf, rc);
4976 if (buf->lb_buf == NULL)
4983 if (unlikely(rc == 0))
4989 if (unlikely(buf->lb_buf == NULL)) {
4990 lu_buf_realloc(buf, rc);
4991 if (buf->lb_buf == NULL)
4998 rc = linkea_init(&ldata);
5000 linkea_first_entry(&ldata);
5001 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5008 * Calls ->lookup() to find dentry. From dentry get inode and
5009 * read inode's ea to get fid. This is required for interoperability
5012 * \retval 0, on success
5013 * \retval -ve, on error
5015 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
5016 struct dt_rec *rec, const struct dt_key *key)
5018 struct inode *dir = obj->oo_inode;
5019 struct dentry *dentry;
5020 struct ldiskfs_dir_entry_2 *de;
5021 struct buffer_head *bh;
5022 struct lu_fid *fid = (struct lu_fid *) rec;
5023 struct htree_lock *hlock = NULL;
5028 LASSERT(dir->i_op != NULL);
5029 LASSERT(dir->i_op->lookup != NULL);
5031 dentry = osd_child_dentry_get(env, obj,
5032 (char *)key, strlen((char *)key));
5034 if (obj->oo_hl_head != NULL) {
5035 hlock = osd_oti_get(env)->oti_hlock;
5036 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5037 dir, LDISKFS_HLOCK_LOOKUP);
5039 down_read(&obj->oo_ext_idx_sem);
5042 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5044 struct osd_thread_info *oti = osd_oti_get(env);
5045 struct osd_inode_id *id = &oti->oti_id;
5046 struct osd_idmap_cache *oic = &oti->oti_cache;
5047 struct osd_device *dev = osd_obj2dev(obj);
5049 ino = le32_to_cpu(de->inode);
5050 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
5052 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
5056 rc = osd_get_fid_from_dentry(de, rec);
5058 /* done with de, release bh */
5061 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5062 const char *name = (const char *)key;
5064 /* If the parent is on remote MDT, and there
5065 * is no FID-in-dirent, then we have to get
5066 * the parent FID from the linkEA. */
5067 if (likely(strlen(name) == 2 &&
5068 name[0] == '.' && name[1] == '.'))
5069 rc = osd_get_pfid_from_linkea(env, obj,
5072 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5075 osd_id_gen(id, ino, OSD_OII_NOGEN);
5078 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
5079 fid_zero(&oic->oic_fid);
5084 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
5085 rc = osd_consistency_check(oti, dev, oic);
5087 fid_zero(&oic->oic_fid);
5096 ldiskfs_htree_unlock(hlock);
5098 up_read(&obj->oo_ext_idx_sem);
5103 * Put the osd object once done with it.
5105 * \param obj osd object that needs to be put
5107 static inline void osd_object_put(const struct lu_env *env,
5108 struct osd_object *obj)
5110 lu_object_put(env, &obj->oo_dt.do_lu);
5113 static int osd_index_declare_ea_insert(const struct lu_env *env,
5114 struct dt_object *dt,
5115 const struct dt_rec *rec,
5116 const struct dt_key *key,
5117 struct thandle *handle)
5119 struct osd_thandle *oh;
5120 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5121 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5122 const struct lu_fid *fid = rec1->rec_fid;
5123 int credits, rc = 0;
5124 struct osd_idmap_cache *idc;
5127 LASSERT(!dt_object_remote(dt));
5128 LASSERT(handle != NULL);
5129 LASSERT(fid != NULL);
5130 LASSERT(rec1->rec_type != 0);
5132 oh = container_of0(handle, struct osd_thandle, ot_super);
5133 LASSERT(oh->ot_handle == NULL);
5135 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
5137 /* we can't call iget() while a transactions is running
5138 * (this can lead to a deadlock), but we need to know
5139 * inum and object type. so we find this information at
5140 * declaration and cache in per-thread info */
5141 idc = osd_idc_find_or_init(env, osd, fid);
5143 RETURN(PTR_ERR(idc));
5144 if (idc->oic_remote) {
5145 /* a reference to remote inode is represented by an
5146 * agent inode which we have to create */
5147 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
5148 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
5151 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
5153 if (osd_dt_obj(dt)->oo_inode != NULL) {
5154 struct inode *inode = osd_dt_obj(dt)->oo_inode;
5156 /* We ignore block quota on meta pool (MDTs), so needn't
5157 * calculate how many blocks will be consumed by this index
5159 rc = osd_declare_inode_qid(env, i_uid_read(inode),
5160 i_gid_read(inode), 0, oh,
5161 osd_dt_obj(dt), true, NULL, false);
5168 * Index add function for interoperability mode (b11826).
5169 * It will add the directory entry.This entry is needed to
5170 * maintain name->fid mapping.
5172 * \param key it is key i.e. file entry to be inserted
5173 * \param rec it is value of given key i.e. fid
5175 * \retval 0, on success
5176 * \retval -ve, on error
5178 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
5179 const struct dt_rec *rec,
5180 const struct dt_key *key, struct thandle *th,
5183 struct osd_object *obj = osd_dt_obj(dt);
5184 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5185 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5186 const struct lu_fid *fid = rec1->rec_fid;
5187 const char *name = (const char *)key;
5188 struct osd_thread_info *oti = osd_oti_get(env);
5189 struct inode *child_inode = NULL;
5190 struct osd_idmap_cache *idc;
5194 if (!dt_object_exists(dt))
5197 LASSERT(osd_invariant(obj));
5198 LASSERT(!dt_object_remote(dt));
5199 LASSERT(th != NULL);
5201 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5203 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
5205 idc = osd_idc_find(env, osd, fid);
5206 if (unlikely(idc == NULL)) {
5207 /* this dt_insert() wasn't declared properly, so
5208 * FID is missing in OI cache. we better do not
5209 * lookup FID in FLDB/OI and don't risk to deadlock,
5210 * but in some special cases (lfsck testing, etc)
5211 * it's much simpler than fixing a caller */
5212 CERROR("%s: "DFID" wasn't declared for insert\n",
5213 osd_name(osd), PFID(fid));
5215 idc = osd_idc_find_or_init(env, osd, fid);
5217 RETURN(PTR_ERR(idc));
5220 if (idc->oic_remote) {
5221 /* Insert remote entry */
5222 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
5223 struct osd_mdobj_map *omm = osd->od_mdt_map;
5224 struct osd_thandle *oh;
5226 /* If parent on remote MDT, we need put this object
5228 oh = container_of(th, typeof(*oh), ot_super);
5229 rc = osd_add_to_remote_parent(env, osd, obj, oh);
5231 CERROR("%s: add "DFID" error: rc = %d\n",
5233 PFID(lu_object_fid(&dt->do_lu)), rc);
5237 child_inode = igrab(omm->omm_remote_parent->d_inode);
5239 child_inode = osd_create_local_agent_inode(env, osd,
5240 obj, fid, rec1->rec_type & S_IFMT, th);
5241 if (IS_ERR(child_inode))
5242 RETURN(PTR_ERR(child_inode));
5245 /* Insert local entry */
5246 if (unlikely(idc->oic_lid.oii_ino == 0)) {
5247 /* for a reason OI cache wasn't filled properly */
5248 CERROR("%s: OIC for "DFID" isn't filled\n",
5249 osd_name(osd), PFID(fid));
5252 child_inode = oti->oti_inode;
5253 if (unlikely(child_inode == NULL)) {
5254 struct ldiskfs_inode_info *lii;
5258 child_inode = oti->oti_inode = &lii->vfs_inode;
5260 child_inode->i_sb = osd_sb(osd);
5261 child_inode->i_ino = idc->oic_lid.oii_ino;
5262 child_inode->i_mode = rec1->rec_type & S_IFMT;
5265 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
5267 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
5268 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
5270 if (child_inode && child_inode != oti->oti_inode)
5272 LASSERT(osd_invariant(obj));
5273 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5278 * Initialize osd Iterator for given osd index object.
5280 * \param dt osd index object
5283 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
5284 struct dt_object *dt,
5287 struct osd_it_iam *it;
5288 struct osd_object *obj = osd_dt_obj(dt);
5289 struct lu_object *lo = &dt->do_lu;
5290 struct iam_path_descr *ipd;
5291 struct iam_container *bag = &obj->oo_dir->od_container;
5293 if (!dt_object_exists(dt))
5294 return ERR_PTR(-ENOENT);
5298 return ERR_PTR(-ENOMEM);
5300 ipd = osd_it_ipd_get(env, bag);
5301 if (likely(ipd != NULL)) {
5305 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
5306 return (struct dt_it *)it;
5309 return ERR_PTR(-ENOMEM);
5314 * free given Iterator.
5317 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
5319 struct osd_it_iam *it = (struct osd_it_iam *)di;
5320 struct osd_object *obj = it->oi_obj;
5322 iam_it_fini(&it->oi_it);
5323 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
5324 lu_object_put(env, &obj->oo_dt.do_lu);
5329 * Move Iterator to record specified by \a key
5331 * \param di osd iterator
5332 * \param key key for index
5334 * \retval +ve di points to record with least key not larger than key
5335 * \retval 0 di points to exact matched key
5336 * \retval -ve failure
5339 static int osd_it_iam_get(const struct lu_env *env,
5340 struct dt_it *di, const struct dt_key *key)
5342 struct osd_thread_info *oti = osd_oti_get(env);
5343 struct osd_it_iam *it = (struct osd_it_iam *)di;
5345 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5346 /* swab quota uid/gid */
5347 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5348 key = (struct dt_key *)&oti->oti_quota_id;
5351 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
5357 * \param di osd iterator
5359 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
5361 struct osd_it_iam *it = (struct osd_it_iam *)di;
5363 iam_it_put(&it->oi_it);
5367 * Move iterator by one record
5369 * \param di osd iterator
5371 * \retval +1 end of container reached
5373 * \retval -ve failure
5376 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
5378 struct osd_it_iam *it = (struct osd_it_iam *)di;
5380 return iam_it_next(&it->oi_it);
5384 * Return pointer to the key under iterator.
5387 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
5388 const struct dt_it *di)
5390 struct osd_thread_info *oti = osd_oti_get(env);
5391 struct osd_it_iam *it = (struct osd_it_iam *)di;
5392 struct osd_object *obj = it->oi_obj;
5395 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
5397 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
5398 /* swab quota uid/gid */
5399 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
5400 key = (struct dt_key *)&oti->oti_quota_id;
5407 * Return size of key under iterator (in bytes)
5410 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5412 struct osd_it_iam *it = (struct osd_it_iam *)di;
5414 return iam_it_key_size(&it->oi_it);
5418 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5420 /* check if file type is required */
5421 if (ent->lde_attrs & LUDA_TYPE) {
5422 struct luda_type *lt;
5423 int align = sizeof(*lt) - 1;
5425 len = (len + align) & ~align;
5426 lt = (struct luda_type *)(ent->lde_name + len);
5427 lt->lt_type = cpu_to_le16(DTTOIF(type));
5430 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5434 * build lu direct from backend fs dirent.
5438 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5439 char *name, __u16 namelen, __u16 type, __u32 attr)
5441 ent->lde_attrs = attr | LUDA_FID;
5442 fid_cpu_to_le(&ent->lde_fid, fid);
5444 ent->lde_hash = cpu_to_le64(offset);
5445 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5447 strncpy(ent->lde_name, name, namelen);
5448 ent->lde_name[namelen] = '\0';
5449 ent->lde_namelen = cpu_to_le16(namelen);
5451 /* append lustre attributes */
5452 osd_it_append_attrs(ent, namelen, type);
5456 * Return pointer to the record under iterator.
5458 static int osd_it_iam_rec(const struct lu_env *env,
5459 const struct dt_it *di,
5460 struct dt_rec *dtrec, __u32 attr)
5462 struct osd_it_iam *it = (struct osd_it_iam *)di;
5463 struct osd_thread_info *info = osd_oti_get(env);
5466 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5467 const struct osd_fid_pack *rec;
5468 struct lu_fid *fid = &info->oti_fid;
5469 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5475 name = (char *)iam_it_key_get(&it->oi_it);
5477 RETURN(PTR_ERR(name));
5479 namelen = iam_it_key_size(&it->oi_it);
5481 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5483 RETURN(PTR_ERR(rec));
5485 rc = osd_fid_unpack(fid, rec);
5489 hash = iam_it_store(&it->oi_it);
5491 /* IAM does not store object type in IAM index (dir) */
5492 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5494 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5495 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5496 (struct iam_rec *)dtrec);
5497 osd_quota_unpack(it->oi_obj, dtrec);
5499 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5500 (struct iam_rec *)dtrec);
5507 * Returns cookie for current Iterator position.
5509 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5511 struct osd_it_iam *it = (struct osd_it_iam *)di;
5513 return iam_it_store(&it->oi_it);
5517 * Restore iterator from cookie.
5519 * \param di osd iterator
5520 * \param hash Iterator location cookie
5522 * \retval +ve di points to record with least key not larger than key.
5523 * \retval 0 di points to exact matched key
5524 * \retval -ve failure
5527 static int osd_it_iam_load(const struct lu_env *env,
5528 const struct dt_it *di, __u64 hash)
5530 struct osd_it_iam *it = (struct osd_it_iam *)di;
5532 return iam_it_load(&it->oi_it, hash);
5535 static const struct dt_index_operations osd_index_iam_ops = {
5536 .dio_lookup = osd_index_iam_lookup,
5537 .dio_declare_insert = osd_index_declare_iam_insert,
5538 .dio_insert = osd_index_iam_insert,
5539 .dio_declare_delete = osd_index_declare_iam_delete,
5540 .dio_delete = osd_index_iam_delete,
5542 .init = osd_it_iam_init,
5543 .fini = osd_it_iam_fini,
5544 .get = osd_it_iam_get,
5545 .put = osd_it_iam_put,
5546 .next = osd_it_iam_next,
5547 .key = osd_it_iam_key,
5548 .key_size = osd_it_iam_key_size,
5549 .rec = osd_it_iam_rec,
5550 .store = osd_it_iam_store,
5551 .load = osd_it_iam_load
5557 * Creates or initializes iterator context.
5559 * \retval struct osd_it_ea, iterator structure on success
5562 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5563 struct dt_object *dt,
5566 struct osd_object *obj = osd_dt_obj(dt);
5567 struct osd_thread_info *info = osd_oti_get(env);
5568 struct osd_it_ea *oie;
5570 struct lu_object *lo = &dt->do_lu;
5571 struct dentry *obj_dentry;
5574 if (!dt_object_exists(dt) || obj->oo_destroyed)
5575 RETURN(ERR_PTR(-ENOENT));
5577 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5579 RETURN(ERR_PTR(-ENOMEM));
5580 obj_dentry = &oie->oie_dentry;
5582 obj_dentry->d_inode = obj->oo_inode;
5583 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5584 obj_dentry->d_name.hash = 0;
5586 oie->oie_rd_dirent = 0;
5587 oie->oie_it_dirent = 0;
5588 oie->oie_dirent = NULL;
5589 if (unlikely(!info->oti_it_ea_buf_used)) {
5590 oie->oie_buf = info->oti_it_ea_buf;
5591 info->oti_it_ea_buf_used = 1;
5593 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5594 if (oie->oie_buf == NULL)
5595 RETURN(ERR_PTR(-ENOMEM));
5599 file = &oie->oie_file;
5601 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5602 if (attr & LUDA_64BITHASH)
5603 file->f_mode = FMODE_64BITHASH;
5605 file->f_mode = FMODE_32BITHASH;
5606 file->f_path.dentry = obj_dentry;
5607 file->f_mapping = obj->oo_inode->i_mapping;
5608 file->f_op = obj->oo_inode->i_fop;
5609 set_file_inode(file, obj->oo_inode);
5612 RETURN((struct dt_it *) oie);
5616 * Destroy or finishes iterator context.
5618 * \param di iterator structure to be destroyed
5620 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5622 struct osd_thread_info *info = osd_oti_get(env);
5623 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5624 struct osd_object *obj = oie->oie_obj;
5625 struct inode *inode = obj->oo_inode;
5628 oie->oie_file.f_op->release(inode, &oie->oie_file);
5629 lu_object_put(env, &obj->oo_dt.do_lu);
5630 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5631 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5633 info->oti_it_ea_buf_used = 0;
5634 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5639 * It position the iterator at given key, so that next lookup continues from
5640 * that key Or it is similar to dio_it->load() but based on a key,
5641 * rather than file position.
5643 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5646 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5648 static int osd_it_ea_get(const struct lu_env *env,
5649 struct dt_it *di, const struct dt_key *key)
5651 struct osd_it_ea *it = (struct osd_it_ea *)di;
5654 LASSERT(((const char *)key)[0] == '\0');
5655 it->oie_file.f_pos = 0;
5656 it->oie_rd_dirent = 0;
5657 it->oie_it_dirent = 0;
5658 it->oie_dirent = NULL;
5666 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5670 struct osd_filldir_cbs {
5671 #ifdef HAVE_DIR_CONTEXT
5672 struct dir_context ctx;
5674 struct osd_it_ea *it;
5677 * It is called internally by ->readdir(). It fills the
5678 * iterator's in-memory data structure with required
5679 * information i.e. name, namelen, rec_size etc.
5681 * \param buf in which information to be filled in.
5682 * \param name name of the file in given dir
5684 * \retval 0 on success
5685 * \retval 1 on buffer full
5687 #ifdef HAVE_FILLDIR_USE_CTX
5688 static int osd_ldiskfs_filldir(struct dir_context *buf,
5690 static int osd_ldiskfs_filldir(void *buf,
5692 const char *name, int namelen,
5693 loff_t offset, __u64 ino, unsigned d_type)
5695 struct osd_it_ea *it =
5696 ((struct osd_filldir_cbs *)buf)->it;
5697 struct osd_object *obj = it->oie_obj;
5698 struct osd_it_ea_dirent *ent = it->oie_dirent;
5699 struct lu_fid *fid = &ent->oied_fid;
5700 struct osd_fid_pack *rec;
5703 /* this should never happen */
5704 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5705 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5709 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5713 /* "." is just the object itself. */
5714 if (namelen == 1 && name[0] == '.') {
5715 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5716 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5717 rec = (struct osd_fid_pack*) (name + namelen + 1);
5718 if (osd_fid_unpack(fid, rec) != 0)
5723 d_type &= ~LDISKFS_DIRENT_LUFID;
5725 /* NOT export local root. */
5726 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5727 ino = obj->oo_inode->i_ino;
5728 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5731 ent->oied_ino = ino;
5732 ent->oied_off = offset;
5733 ent->oied_namelen = namelen;
5734 ent->oied_type = d_type;
5736 memcpy(ent->oied_name, name, namelen);
5738 it->oie_rd_dirent++;
5739 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5744 * Calls ->readdir() to load a directory entry at a time
5745 * and stored it in iterator's in-memory data structure.
5747 * \param di iterator's in memory structure
5749 * \retval 0 on success
5750 * \retval -ve on error
5751 * \retval +1 reach the end of entry
5753 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5754 const struct dt_it *di)
5756 struct osd_it_ea *it = (struct osd_it_ea *)di;
5757 struct osd_object *obj = it->oie_obj;
5758 struct inode *inode = obj->oo_inode;
5759 struct htree_lock *hlock = NULL;
5760 struct file *filp = &it->oie_file;
5762 struct osd_filldir_cbs buf = {
5763 #ifdef HAVE_DIR_CONTEXT
5764 .ctx.actor = osd_ldiskfs_filldir,
5770 it->oie_dirent = it->oie_buf;
5771 it->oie_rd_dirent = 0;
5773 if (obj->oo_hl_head != NULL) {
5774 hlock = osd_oti_get(env)->oti_hlock;
5775 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5776 inode, LDISKFS_HLOCK_READDIR);
5778 down_read(&obj->oo_ext_idx_sem);
5781 #ifdef HAVE_DIR_CONTEXT
5782 buf.ctx.pos = filp->f_pos;
5783 rc = inode->i_fop->iterate(filp, &buf.ctx);
5784 filp->f_pos = buf.ctx.pos;
5786 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5790 ldiskfs_htree_unlock(hlock);
5792 up_read(&obj->oo_ext_idx_sem);
5794 if (it->oie_rd_dirent == 0) {
5795 /*If it does not get any dirent, it means it has been reached
5796 *to the end of the dir */
5797 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5801 it->oie_dirent = it->oie_buf;
5802 it->oie_it_dirent = 1;
5809 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5810 * to load a directory entry at a time and stored it in
5811 * iterator's in-memory data structure.
5813 * \param di iterator's in memory structure
5815 * \retval +ve iterator reached to end
5816 * \retval 0 iterator not reached to end
5817 * \retval -ve on error
5819 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5821 struct osd_it_ea *it = (struct osd_it_ea *)di;
5826 if (it->oie_it_dirent < it->oie_rd_dirent) {
5828 (void *) it->oie_dirent +
5829 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5830 it->oie_dirent->oied_namelen);
5831 it->oie_it_dirent++;
5834 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5837 rc = osd_ldiskfs_it_fill(env, di);
5844 * Returns the key at current position from iterator's in memory structure.
5846 * \param di iterator's in memory structure
5848 * \retval key i.e. struct dt_key on success
5850 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5851 const struct dt_it *di)
5853 struct osd_it_ea *it = (struct osd_it_ea *)di;
5855 return (struct dt_key *)it->oie_dirent->oied_name;
5859 * Returns the key's size at current position from iterator's in memory structure.
5861 * \param di iterator's in memory structure
5863 * \retval key_size i.e. struct dt_key on success
5865 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5867 struct osd_it_ea *it = (struct osd_it_ea *)di;
5869 return it->oie_dirent->oied_namelen;
5873 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
5875 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
5876 "dot_dotdot = %d\n", dot_dotdot);
5878 if (LDISKFS_DIR_REC_LEN(de) >=
5879 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
5886 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5887 unsigned blocksize, int dot_dotdot)
5890 return osd_dot_dotdot_has_space(de, dot_dotdot);
5892 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5893 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5900 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
5901 struct dentry *dentry, const struct lu_fid *fid,
5902 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
5903 struct htree_lock *hlock, int dot_dotdot)
5905 struct inode *dir = dentry->d_parent->d_inode;
5906 struct inode *inode = dentry->d_inode;
5907 struct osd_fid_pack *rec;
5908 struct ldiskfs_dentry_param *ldp;
5909 int namelen = dentry->d_name.len;
5911 struct osd_thread_info *info = osd_oti_get(env);
5914 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
5915 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5918 /* There is enough space to hold the FID-in-dirent. */
5919 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize,
5921 rc = ldiskfs_journal_get_write_access(jh, bh);
5925 de->name[namelen] = 0;
5926 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
5927 rec->fp_len = sizeof(struct lu_fid) + 1;
5928 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
5929 de->file_type |= LDISKFS_DIRENT_LUFID;
5930 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
5935 LASSERTF(dot_dotdot == 0, "dot_dotdot = %d\n", dot_dotdot);
5937 rc = ldiskfs_delete_entry(jh, dir, de, bh);
5941 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
5942 osd_get_ldiskfs_dirent_param(ldp, fid);
5943 dentry->d_fsdata = (void *)ldp;
5944 ll_vfs_dq_init(dir);
5945 rc = osd_ldiskfs_add_entry(info, jh, dentry, inode, hlock);
5946 /* It is too bad, we cannot reinsert the name entry back.
5947 * That means we lose it! */
5949 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
5950 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
5951 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
5952 dir->i_ino, dir->i_generation, namelen,
5953 dentry->d_name.name, PFID(fid), rc);
5959 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
5960 struct osd_it_ea *it, struct lu_fid *fid,
5961 struct osd_inode_id *id, __u32 *attr)
5963 struct osd_thread_info *info = osd_oti_get(env);
5964 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
5965 struct osd_device *dev = osd_obj2dev(obj);
5966 struct super_block *sb = osd_sb(dev);
5967 const char *devname =
5968 LDISKFS_SB(sb)->s_es->s_volume_name;
5969 struct osd_it_ea_dirent *ent = it->oie_dirent;
5970 struct inode *dir = obj->oo_inode;
5971 struct htree_lock *hlock = NULL;
5972 struct buffer_head *bh = NULL;
5973 handle_t *jh = NULL;
5974 struct ldiskfs_dir_entry_2 *de;
5975 struct dentry *dentry;
5976 struct inode *inode;
5983 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5984 inode = osd_iget(info, dev, id);
5985 if (IS_ERR(inode)) {
5986 rc = PTR_ERR(inode);
5987 if (rc == -ENOENT || rc == -ESTALE) {
5988 *attr |= LUDA_UNKNOWN;
5991 CDEBUG(D_LFSCK, "%.16s: fail to iget for dirent "
5992 "check_repair, dir = %lu/%u, name = %.*s: "
5994 devname, dir->i_ino, dir->i_generation,
5995 ent->oied_namelen, ent->oied_name, rc);
6001 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
6003 rc = osd_get_lma(info, inode, dentry, lma);
6009 if (ent->oied_name[0] == '.') {
6010 if (ent->oied_namelen == 1)
6012 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
6016 /* We need to ensure that the name entry is still valid.
6017 * Because it may be removed or renamed by other already.
6019 * The unlink or rename operation will start journal before PDO lock,
6020 * so to avoid deadlock, here we need to start journal handle before
6021 * related PDO lock also. But because we do not know whether there
6022 * will be something to be repaired before PDO lock, we just start
6023 * journal without conditions.
6025 * We may need to remove the name entry firstly, then insert back.
6026 * One credit is for user quota file update.
6027 * One credit is for group quota file update.
6028 * Two credits are for dirty inode. */
6029 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
6030 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
6032 if (dev->od_dirent_journal != 0) {
6035 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
6038 CDEBUG(D_LFSCK, "%.16s: fail to start trans for dirent "
6039 "check_repair, dir = %lu/%u, credits = %d, "
6040 "name = %.*s: rc = %d\n",
6041 devname, dir->i_ino, dir->i_generation, credits,
6042 ent->oied_namelen, ent->oied_name, rc);
6044 GOTO(out_inode, rc);
6047 if (obj->oo_hl_head != NULL) {
6048 hlock = osd_oti_get(env)->oti_hlock;
6049 /* "0" means exclusive lock for the whole directory.
6050 * We need to prevent others access such name entry
6051 * during the delete + insert. Neither HLOCK_ADD nor
6052 * HLOCK_DEL cannot guarantee the atomicity. */
6053 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
6055 down_write(&obj->oo_ext_idx_sem);
6058 if (obj->oo_hl_head != NULL) {
6059 hlock = osd_oti_get(env)->oti_hlock;
6060 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
6061 LDISKFS_HLOCK_LOOKUP);
6063 down_read(&obj->oo_ext_idx_sem);
6067 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6068 /* For dot/dotdot entry, if there is not enough space to hold the
6069 * FID-in-dirent, just keep them there. It only happens when the
6070 * device upgraded from 1.8 or restored from MDT file-level backup.
6071 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
6072 * and needs to get FID from LMA when readdir, it will not affect the
6073 * performance much. */
6074 if ((bh == NULL) || (le32_to_cpu(de->inode) != inode->i_ino) ||
6075 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
6076 *attr |= LUDA_IGNORE;
6082 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
6083 struct lu_fid *tfid = &lma->lma_self_fid;
6085 *attr |= LUDA_IGNORE;
6086 /* It must be REMOTE_PARENT_DIR and as the
6087 * dotdot entry of remote directory */
6088 if (unlikely(dot_dotdot != 2 ||
6089 fid_seq(tfid) != FID_SEQ_LOCAL_FILE ||
6090 fid_oid(tfid) != REMOTE_PARENT_DIR_OID)) {
6091 CDEBUG(D_LFSCK, "%.16s: expect remote agent "
6092 "parent directory, but got %.*s under "
6093 "dir = %lu/%u with the FID "DFID"\n",
6094 devname, ent->oied_namelen,
6095 ent->oied_name, dir->i_ino,
6096 dir->i_generation, PFID(tfid));
6098 GOTO(out, rc = -EIO);
6104 if (fid_is_sane(fid)) {
6105 /* FID-in-dirent is valid. */
6106 if (lu_fid_eq(fid, &lma->lma_self_fid))
6109 /* Do not repair under dryrun mode. */
6110 if (*attr & LUDA_VERIFY_DRYRUN) {
6111 *attr |= LUDA_REPAIR;
6118 dev->od_dirent_journal = 1;
6119 if (hlock != NULL) {
6120 ldiskfs_htree_unlock(hlock);
6123 up_read(&obj->oo_ext_idx_sem);
6129 *fid = lma->lma_self_fid;
6131 /* Update the FID-in-dirent. */
6132 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
6135 *attr |= LUDA_REPAIR;
6137 CDEBUG(D_LFSCK, "%.16s: fail to update FID "
6138 "in the dirent, dir = %lu/%u, "
6139 "name = %.*s, "DFID": rc = %d\n",
6140 devname, dir->i_ino, dir->i_generation,
6141 ent->oied_namelen, ent->oied_name,
6144 /* Do not repair under dryrun mode. */
6145 if (*attr & LUDA_VERIFY_DRYRUN) {
6146 *fid = lma->lma_self_fid;
6147 *attr |= LUDA_REPAIR;
6154 dev->od_dirent_journal = 1;
6155 if (hlock != NULL) {
6156 ldiskfs_htree_unlock(hlock);
6159 up_read(&obj->oo_ext_idx_sem);
6165 *fid = lma->lma_self_fid;
6167 /* Append the FID-in-dirent. */
6168 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
6171 *attr |= LUDA_REPAIR;
6173 CDEBUG(D_LFSCK, "%.16s: fail to append FID "
6174 "after the dirent, dir = %lu/%u, "
6175 "name = %.*s, "DFID": rc = %d\n",
6176 devname, dir->i_ino, dir->i_generation,
6177 ent->oied_namelen, ent->oied_name,
6181 /* Do not repair under dryrun mode. */
6182 if (*attr & LUDA_VERIFY_DRYRUN) {
6183 if (fid_is_sane(fid)) {
6184 *attr |= LUDA_REPAIR;
6186 lu_igif_build(fid, inode->i_ino,
6187 inode->i_generation);
6188 *attr |= LUDA_UPGRADE;
6196 dev->od_dirent_journal = 1;
6197 if (hlock != NULL) {
6198 ldiskfs_htree_unlock(hlock);
6201 up_read(&obj->oo_ext_idx_sem);
6208 if (unlikely(fid_is_sane(fid))) {
6209 /* FID-in-dirent exists, but FID-in-LMA is lost.
6210 * Trust the FID-in-dirent, and add FID-in-LMA. */
6211 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
6213 *attr |= LUDA_REPAIR;
6215 CDEBUG(D_LFSCK, "%.16s: fail to set LMA for "
6216 "update dirent, dir = %lu/%u, "
6217 "name = %.*s, "DFID": rc = %d\n",
6218 devname, dir->i_ino, dir->i_generation,
6219 ent->oied_namelen, ent->oied_name,
6222 lu_igif_build(fid, inode->i_ino, inode->i_generation);
6223 /* It is probably IGIF object. Only aappend the
6224 * FID-in-dirent. OI scrub will process FID-in-LMA. */
6225 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
6228 *attr |= LUDA_UPGRADE;
6230 CDEBUG(D_LFSCK, "%.16s: fail to append IGIF "
6231 "after the dirent, dir = %lu/%u, "
6232 "name = %.*s, "DFID": rc = %d\n",
6233 devname, dir->i_ino, dir->i_generation,
6234 ent->oied_namelen, ent->oied_name,
6243 if (hlock != NULL) {
6244 ldiskfs_htree_unlock(hlock);
6246 if (dev->od_dirent_journal != 0)
6247 up_write(&obj->oo_ext_idx_sem);
6249 up_read(&obj->oo_ext_idx_sem);
6253 ldiskfs_journal_stop(jh);
6257 if (rc >= 0 && !dirty)
6258 dev->od_dirent_journal = 0;
6264 * Returns the value at current position from iterator's in memory structure.
6266 * \param di struct osd_it_ea, iterator's in memory structure
6267 * \param attr attr requested for dirent.
6268 * \param lde lustre dirent
6270 * \retval 0 no error and \param lde has correct lustre dirent.
6271 * \retval -ve on error
6273 static inline int osd_it_ea_rec(const struct lu_env *env,
6274 const struct dt_it *di,
6275 struct dt_rec *dtrec, __u32 attr)
6277 struct osd_it_ea *it = (struct osd_it_ea *)di;
6278 struct osd_object *obj = it->oie_obj;
6279 struct osd_device *dev = osd_obj2dev(obj);
6280 struct osd_thread_info *oti = osd_oti_get(env);
6281 struct osd_inode_id *id = &oti->oti_id;
6282 struct lu_fid *fid = &it->oie_dirent->oied_fid;
6283 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6284 __u32 ino = it->oie_dirent->oied_ino;
6288 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
6290 if (attr & LUDA_VERIFY) {
6291 if (unlikely(ino == osd_remote_parent_ino(dev))) {
6292 attr |= LUDA_IGNORE;
6293 /* If the parent is on remote MDT, and there
6294 * is no FID-in-dirent, then we have to get
6295 * the parent FID from the linkEA. */
6296 if (!fid_is_sane(fid) &&
6297 it->oie_dirent->oied_namelen == 2 &&
6298 it->oie_dirent->oied_name[0] == '.' &&
6299 it->oie_dirent->oied_name[1] == '.')
6300 osd_get_pfid_from_linkea(env, obj, fid);
6302 rc = osd_dirent_check_repair(env, obj, it, fid, id,
6306 if (!fid_is_sane(fid)) {
6307 attr &= ~LUDA_IGNORE;
6308 attr |= LUDA_UNKNOWN;
6311 attr &= ~LU_DIRENT_ATTRS_MASK;
6312 if (!fid_is_sane(fid)) {
6313 bool is_dotdot = false;
6314 if (it->oie_dirent->oied_namelen == 2 &&
6315 it->oie_dirent->oied_name[0] == '.' &&
6316 it->oie_dirent->oied_name[1] == '.')
6318 /* If the parent is on remote MDT, and there
6319 * is no FID-in-dirent, then we have to get
6320 * the parent FID from the linkEA. */
6321 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
6322 rc = osd_get_pfid_from_linkea(env, obj, fid);
6324 if (is_dotdot == false &&
6325 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
6328 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6331 osd_id_gen(id, ino, OSD_OII_NOGEN);
6335 /* Pack the entry anyway, at least the offset is right. */
6336 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
6337 it->oie_dirent->oied_name,
6338 it->oie_dirent->oied_namelen,
6339 it->oie_dirent->oied_type, attr);
6344 if (osd_remote_fid(env, dev, fid))
6347 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
6348 osd_add_oi_cache(oti, dev, id, fid);
6350 RETURN(rc > 0 ? 0 : rc);
6354 * Returns the record size size at current position.
6356 * This function will return record(lu_dirent) size in bytes.
6358 * \param[in] env execution environment
6359 * \param[in] di iterator's in memory structure
6360 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
6361 * calculate the lu_dirent size.
6363 * \retval record size(in bytes & in memory) of the current lu_dirent
6366 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
6369 struct osd_it_ea *it = (struct osd_it_ea *)di;
6371 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
6375 * Returns a cookie for current position of the iterator head, so that
6376 * user can use this cookie to load/start the iterator next time.
6378 * \param di iterator's in memory structure
6380 * \retval cookie for current position, on success
6382 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
6384 struct osd_it_ea *it = (struct osd_it_ea *)di;
6386 return it->oie_dirent->oied_off;
6390 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6391 * to load a directory entry at a time and stored it i inn,
6392 * in iterator's in-memory data structure.
6394 * \param di struct osd_it_ea, iterator's in memory structure
6396 * \retval +ve on success
6397 * \retval -ve on error
6399 static int osd_it_ea_load(const struct lu_env *env,
6400 const struct dt_it *di, __u64 hash)
6402 struct osd_it_ea *it = (struct osd_it_ea *)di;
6406 it->oie_file.f_pos = hash;
6408 rc = osd_ldiskfs_it_fill(env, di);
6419 * Index lookup function for interoperability mode (b11826).
6421 * \param key, key i.e. file name to be searched
6423 * \retval +ve, on success
6424 * \retval -ve, on error
6426 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6427 struct dt_rec *rec, const struct dt_key *key)
6429 struct osd_object *obj = osd_dt_obj(dt);
6434 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6435 LINVRNT(osd_invariant(obj));
6437 rc = osd_ea_lookup_rec(env, obj, rec, key);
6444 * Index and Iterator operations for interoperability
6445 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6447 static const struct dt_index_operations osd_index_ea_ops = {
6448 .dio_lookup = osd_index_ea_lookup,
6449 .dio_declare_insert = osd_index_declare_ea_insert,
6450 .dio_insert = osd_index_ea_insert,
6451 .dio_declare_delete = osd_index_declare_ea_delete,
6452 .dio_delete = osd_index_ea_delete,
6454 .init = osd_it_ea_init,
6455 .fini = osd_it_ea_fini,
6456 .get = osd_it_ea_get,
6457 .put = osd_it_ea_put,
6458 .next = osd_it_ea_next,
6459 .key = osd_it_ea_key,
6460 .key_size = osd_it_ea_key_size,
6461 .rec = osd_it_ea_rec,
6462 .rec_size = osd_it_ea_rec_size,
6463 .store = osd_it_ea_store,
6464 .load = osd_it_ea_load
6468 static void *osd_key_init(const struct lu_context *ctx,
6469 struct lu_context_key *key)
6471 struct osd_thread_info *info;
6473 OBD_ALLOC_PTR(info);
6475 return ERR_PTR(-ENOMEM);
6477 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6478 if (info->oti_it_ea_buf == NULL)
6481 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6483 info->oti_hlock = ldiskfs_htree_lock_alloc();
6484 if (info->oti_hlock == NULL)
6490 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6493 return ERR_PTR(-ENOMEM);
6496 static void osd_key_fini(const struct lu_context *ctx,
6497 struct lu_context_key *key, void* data)
6499 struct osd_thread_info *info = data;
6500 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6501 struct osd_idmap_cache *idc = info->oti_ins_cache;
6503 if (info->oti_inode != NULL)
6505 if (info->oti_hlock != NULL)
6506 ldiskfs_htree_lock_free(info->oti_hlock);
6507 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6508 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6509 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6510 lu_buf_free(&info->oti_big_buf);
6512 LASSERT(info->oti_ins_cache_size > 0);
6513 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6514 info->oti_ins_cache = NULL;
6515 info->oti_ins_cache_size = 0;
6520 static void osd_key_exit(const struct lu_context *ctx,
6521 struct lu_context_key *key, void *data)
6523 struct osd_thread_info *info = data;
6525 LASSERT(info->oti_r_locks == 0);
6526 LASSERT(info->oti_w_locks == 0);
6527 LASSERT(info->oti_txns == 0);
6530 /* type constructor/destructor: osd_type_init, osd_type_fini */
6531 LU_TYPE_INIT_FINI(osd, &osd_key);
6533 struct lu_context_key osd_key = {
6534 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6535 .lct_init = osd_key_init,
6536 .lct_fini = osd_key_fini,
6537 .lct_exit = osd_key_exit
6541 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6542 const char *name, struct lu_device *next)
6544 struct osd_device *osd = osd_dev(d);
6546 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6547 >= sizeof(osd->od_svname))
6549 return osd_procfs_init(osd, name);
6552 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6554 struct seq_server_site *ss = osd_seq_site(osd);
6558 if (osd->od_is_ost || osd->od_cl_seq != NULL)
6561 if (unlikely(ss == NULL))
6564 OBD_ALLOC_PTR(osd->od_cl_seq);
6565 if (osd->od_cl_seq == NULL)
6568 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6569 osd->od_svname, ss->ss_server_seq);
6571 OBD_FREE_PTR(osd->od_cl_seq);
6572 osd->od_cl_seq = NULL;
6576 if (ss->ss_node_id == 0) {
6577 /* If the OSD on the sequence controller(MDT0), then allocate
6578 * sequence here, otherwise allocate sequence after connected
6579 * to MDT0 (see mdt_register_lwp_callback()). */
6580 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6581 &osd->od_cl_seq->lcs_space, env);
6587 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6589 if (osd->od_cl_seq == NULL)
6592 seq_client_fini(osd->od_cl_seq);
6593 OBD_FREE_PTR(osd->od_cl_seq);
6594 osd->od_cl_seq = NULL;
6597 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6601 /* shutdown quota slave instance associated with the device */
6602 if (o->od_quota_slave != NULL) {
6603 qsd_fini(env, o->od_quota_slave);
6604 o->od_quota_slave = NULL;
6607 osd_fid_fini(env, o);
6612 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6616 if (o->od_mnt != NULL) {
6617 shrink_dcache_sb(osd_sb(o));
6618 osd_sync(env, &o->od_dt_dev);
6627 static int osd_mount(const struct lu_env *env,
6628 struct osd_device *o, struct lustre_cfg *cfg)
6630 const char *name = lustre_cfg_string(cfg, 0);
6631 const char *dev = lustre_cfg_string(cfg, 1);
6633 unsigned long page, s_flags, lmd_flags = 0;
6634 struct page *__page;
6635 struct file_system_type *type;
6636 char *options = NULL;
6638 struct osd_thread_info *info = osd_oti_get(env);
6639 struct lu_fid *fid = &info->oti_fid;
6640 struct inode *inode;
6641 int rc = 0, force_over_256tb = 0;
6644 if (o->od_mnt != NULL)
6647 if (strlen(dev) >= sizeof(o->od_mntdev))
6649 strcpy(o->od_mntdev, dev);
6651 str = lustre_cfg_string(cfg, 2);
6652 s_flags = simple_strtoul(str, NULL, 0);
6653 str = strstr(str, ":");
6655 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6656 opts = lustre_cfg_string(cfg, 3);
6658 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6659 CERROR("%s: device %s extents feature is not guaranteed to "
6660 "work on big-endian systems. Use \"bigendian_extents\" "
6661 "mount option to override.\n", name, dev);
6665 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
6666 CWARN("force_over_128tb option is depricated."
6667 "Filesystems less then 256TB can be created without any"
6668 "force options. Use force_over_256tb option for"
6669 "filesystems greather then 256TB.\n");
6672 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL)
6673 force_over_256tb = 1;
6675 __page = alloc_page(GFP_KERNEL);
6677 GOTO(out, rc = -ENOMEM);
6678 page = (unsigned long)page_address(__page);
6679 options = (char *)page;
6682 /* strip out the options for back compatiblity */
6683 static char *sout[] = {
6690 /* strip out option we processed in osd */
6691 "bigendian_extents",
6692 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(3,0,53,0)
6693 #warning "remove force_over_128 option"
6695 "force_over_128tb (deprecated)",
6700 strcat(options, opts);
6701 for (rc = 0, str = options; sout[rc]; ) {
6702 char *op = strstr(str, sout[rc]);
6708 if (op == options || *(op - 1) == ',') {
6709 str = op + strlen(sout[rc]);
6710 if (*str == ',' || *str == '\0') {
6711 *str == ',' ? str++ : str;
6712 memmove(op, str, strlen(str) + 1);
6715 for (str = op; *str != ',' && *str != '\0'; str++)
6719 strncat(options, "user_xattr,acl", 14);
6722 /* Glom up mount options */
6723 if (*options != '\0')
6724 strcat(options, ",");
6725 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
6727 type = get_fs_type("ldiskfs");
6729 CERROR("%s: cannot find ldiskfs module\n", name);
6730 GOTO(out, rc = -ENODEV);
6733 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6734 module_put(type->owner);
6736 if (IS_ERR(o->od_mnt)) {
6737 rc = PTR_ERR(o->od_mnt);
6739 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6743 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) > (64ULL << 30) &&
6744 force_over_256tb == 0) {
6745 CERROR("%s: device %s LDISKFS does not support filesystems "
6746 "greater than 256TB and can cause data corruption. "
6747 "Use \"force_over_256tb\" mount option to override.\n",
6749 GOTO(out, rc = -EINVAL);
6752 #ifdef HAVE_DEV_SET_RDONLY
6753 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
6754 CERROR("%s: underlying device %s is marked as read-only. "
6755 "Setup failed\n", name, dev);
6756 GOTO(out_mnt, rc = -EROFS);
6760 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6761 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6762 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6763 GOTO(out_mnt, rc = -EINVAL);
6766 #ifdef LDISKFS_MOUNT_DIRDATA
6767 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6768 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6769 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6770 else if (!o->od_is_ost)
6771 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6772 "enabling the dirdata feature. If you do not want to "
6773 "downgrade to Lustre-1.x again, you can enable it via "
6774 "'tune2fs -O dirdata device'\n", name, dev);
6776 inode = osd_sb(o)->s_root->d_inode;
6777 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6778 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6780 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6784 if (lmd_flags & LMD_FLG_NOSCRUB)
6795 __free_page(__page);
6800 static struct lu_device *osd_device_fini(const struct lu_env *env,
6801 struct lu_device *d)
6803 struct osd_device *o = osd_dev(d);
6806 osd_shutdown(env, o);
6808 osd_scrub_cleanup(env, o);
6809 osd_obj_map_fini(o);
6815 static int osd_device_init0(const struct lu_env *env,
6816 struct osd_device *o,
6817 struct lustre_cfg *cfg)
6819 struct lu_device *l = osd2lu_dev(o);
6820 struct osd_thread_info *info;
6824 /* if the module was re-loaded, env can loose its keys */
6825 rc = lu_env_refill((struct lu_env *) env);
6828 info = osd_oti_get(env);
6831 l->ld_ops = &osd_lu_ops;
6832 o->od_dt_dev.dd_ops = &osd_dt_ops;
6834 spin_lock_init(&o->od_osfs_lock);
6835 mutex_init(&o->od_otable_mutex);
6836 INIT_LIST_HEAD(&o->od_orphan_list);
6838 o->od_read_cache = 1;
6839 o->od_writethrough_cache = 1;
6840 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6842 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6843 sizeof(o->od_svname));
6844 if (cplen >= sizeof(o->od_svname)) {
6849 o->od_index = -1; /* -1 means index is invalid */
6850 rc = server_name2index(o->od_svname, &o->od_index, NULL);
6851 if (rc == LDD_F_SV_TYPE_OST)
6854 o->od_full_scrub_ratio = OFSR_DEFAULT;
6855 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6856 rc = osd_mount(env, o, cfg);
6860 rc = osd_obj_map_init(env, o);
6864 rc = lu_site_init(&o->od_site, l);
6866 GOTO(out_compat, rc);
6867 o->od_site.ls_bottom_dev = l;
6869 rc = lu_site_init_finish(&o->od_site);
6873 INIT_LIST_HEAD(&o->od_ios_list);
6874 /* setup scrub, including OI files initialization */
6875 rc = osd_scrub_setup(env, o);
6879 rc = osd_procfs_init(o, o->od_svname);
6881 CERROR("%s: can't initialize procfs: rc = %d\n",
6883 GOTO(out_scrub, rc);
6886 LASSERT(l->ld_site->ls_linkage.next != NULL);
6887 LASSERT(l->ld_site->ls_linkage.prev != NULL);
6889 /* initialize quota slave instance */
6890 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
6892 if (IS_ERR(o->od_quota_slave)) {
6893 rc = PTR_ERR(o->od_quota_slave);
6894 o->od_quota_slave = NULL;
6895 GOTO(out_procfs, rc);
6903 osd_scrub_cleanup(env, o);
6905 lu_site_fini(&o->od_site);
6907 osd_obj_map_fini(o);
6914 static struct lu_device *osd_device_alloc(const struct lu_env *env,
6915 struct lu_device_type *t,
6916 struct lustre_cfg *cfg)
6918 struct osd_device *o;
6923 return ERR_PTR(-ENOMEM);
6925 rc = dt_device_init(&o->od_dt_dev, t);
6927 /* Because the ctx might be revived in dt_device_init,
6928 * refill the env here */
6929 lu_env_refill((struct lu_env *)env);
6930 rc = osd_device_init0(env, o, cfg);
6932 dt_device_fini(&o->od_dt_dev);
6935 if (unlikely(rc != 0))
6938 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
6941 static struct lu_device *osd_device_free(const struct lu_env *env,
6942 struct lu_device *d)
6944 struct osd_device *o = osd_dev(d);
6947 /* XXX: make osd top device in order to release reference */
6948 d->ld_site->ls_top_dev = d;
6949 lu_site_purge(env, d->ld_site, -1);
6950 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
6951 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
6952 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
6954 lu_site_fini(&o->od_site);
6955 dt_device_fini(&o->od_dt_dev);
6960 static int osd_process_config(const struct lu_env *env,
6961 struct lu_device *d, struct lustre_cfg *cfg)
6963 struct osd_device *o = osd_dev(d);
6967 switch (cfg->lcfg_command) {
6969 rc = osd_mount(env, o, cfg);
6972 lu_dev_del_linkage(d->ld_site, d);
6973 rc = osd_shutdown(env, o);
6976 LASSERT(&o->od_dt_dev);
6977 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
6978 cfg, &o->od_dt_dev);
6979 if (rc > 0 || rc == -ENOSYS)
6980 rc = class_process_proc_param(PARAM_OST,
6981 lprocfs_osd_obd_vars,
6982 cfg, &o->od_dt_dev);
6991 static int osd_recovery_complete(const struct lu_env *env,
6992 struct lu_device *d)
6994 struct osd_device *osd = osd_dev(d);
6998 if (osd->od_quota_slave == NULL)
7001 /* start qsd instance on recovery completion, this notifies the quota
7002 * slave code that we are about to process new requests now */
7003 rc = qsd_start(env, osd->od_quota_slave);
7008 * we use exports to track all osd users
7010 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
7011 struct obd_device *obd, struct obd_uuid *cluuid,
7012 struct obd_connect_data *data, void *localdata)
7014 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7015 struct lustre_handle conn;
7019 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
7021 rc = class_connect(&conn, obd, cluuid);
7025 *exp = class_conn2export(&conn);
7027 spin_lock(&osd->od_osfs_lock);
7029 spin_unlock(&osd->od_osfs_lock);
7035 * once last export (we don't count self-export) disappeared
7036 * osd can be released
7038 static int osd_obd_disconnect(struct obd_export *exp)
7040 struct obd_device *obd = exp->exp_obd;
7041 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7042 int rc, release = 0;
7045 /* Only disconnect the underlying layers on the final disconnect. */
7046 spin_lock(&osd->od_osfs_lock);
7048 if (osd->od_connects == 0)
7050 spin_unlock(&osd->od_osfs_lock);
7052 rc = class_disconnect(exp); /* bz 9811 */
7054 if (rc == 0 && release)
7055 class_manual_cleanup(obd);
7059 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
7060 struct lu_device *dev)
7062 struct osd_device *osd = osd_dev(dev);
7063 struct lr_server_data *lsd =
7064 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
7068 if (osd->od_quota_slave != NULL) {
7069 /* set up quota slave objects */
7070 result = qsd_prepare(env, osd->od_quota_slave);
7075 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
7076 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
7077 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
7078 osd->od_index_in_idif = 1;
7080 osd->od_index_in_idif = 0;
7081 result = osd_register_proc_index_in_idif(osd);
7086 osd->od_index_in_idif = 1;
7090 result = osd_fid_init(env, osd);
7095 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
7096 struct lu_fid *fid, struct md_op_data *op_data)
7098 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
7100 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
7103 static const struct lu_object_operations osd_lu_obj_ops = {
7104 .loo_object_init = osd_object_init,
7105 .loo_object_delete = osd_object_delete,
7106 .loo_object_release = osd_object_release,
7107 .loo_object_free = osd_object_free,
7108 .loo_object_print = osd_object_print,
7109 .loo_object_invariant = osd_object_invariant
7112 const struct lu_device_operations osd_lu_ops = {
7113 .ldo_object_alloc = osd_object_alloc,
7114 .ldo_process_config = osd_process_config,
7115 .ldo_recovery_complete = osd_recovery_complete,
7116 .ldo_prepare = osd_prepare,
7119 static const struct lu_device_type_operations osd_device_type_ops = {
7120 .ldto_init = osd_type_init,
7121 .ldto_fini = osd_type_fini,
7123 .ldto_start = osd_type_start,
7124 .ldto_stop = osd_type_stop,
7126 .ldto_device_alloc = osd_device_alloc,
7127 .ldto_device_free = osd_device_free,
7129 .ldto_device_init = osd_device_init,
7130 .ldto_device_fini = osd_device_fini
7133 static struct lu_device_type osd_device_type = {
7134 .ldt_tags = LU_DEVICE_DT,
7135 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
7136 .ldt_ops = &osd_device_type_ops,
7137 .ldt_ctx_tags = LCT_LOCAL,
7140 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
7142 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7143 struct super_block *sb = osd_sb(osd);
7145 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
7149 * lprocfs legacy support.
7151 static struct obd_ops osd_obd_device_ops = {
7152 .o_owner = THIS_MODULE,
7153 .o_connect = osd_obd_connect,
7154 .o_disconnect = osd_obd_disconnect,
7155 .o_fid_alloc = osd_fid_alloc,
7156 .o_health_check = osd_health_check,
7159 static int __init osd_init(void)
7163 LASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
7164 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
7165 /* please, try to keep osd_thread_info smaller than a page */
7166 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
7171 rc = lu_kmem_init(ldiskfs_caches);
7175 rc = class_register_type(&osd_obd_device_ops, NULL, true,
7176 lprocfs_osd_module_vars,
7177 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
7179 lu_kmem_fini(ldiskfs_caches);
7183 static void __exit osd_exit(void)
7185 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
7186 lu_kmem_fini(ldiskfs_caches);
7189 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7190 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
7191 MODULE_VERSION(LUSTRE_VERSION_STRING);
7192 MODULE_LICENSE("GPL");
7194 module_init(osd_init);
7195 module_exit(osd_exit);