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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/osd/osd_handler.c
34 * Top-level entry points into osd module
36 * Author: Nikita Danilov <nikita@clusterfs.com>
37 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
40 #define DEBUG_SUBSYSTEM S_OSD
42 #include <linux/kallsyms.h>
43 #include <linux/module.h>
44 #include <linux/user_namespace.h>
45 #ifdef HAVE_UIDGID_HEADER
46 # include <linux/uidgid.h>
49 /* prerequisite for linux/xattr.h */
50 #include <linux/types.h>
51 /* prerequisite for linux/xattr.h */
53 /* XATTR_{REPLACE,CREATE} */
54 #include <linux/xattr.h>
56 #include <ldiskfs/ldiskfs.h>
57 #include <ldiskfs/xattr.h>
58 #include <ldiskfs/ldiskfs_extents.h>
61 * struct OBD_{ALLOC,FREE}*()
64 #include <obd_support.h>
65 /* struct ptlrpc_thread */
66 #include <lustre_net.h>
67 #include <lustre_fid.h>
69 #include <uapi/linux/lustre/lustre_param.h>
71 #include "osd_internal.h"
72 #include "osd_dynlocks.h"
74 /* llo_* api support */
75 #include <md_object.h>
76 #include <lustre_quota.h>
78 #include <lustre_linkea.h>
81 module_param(ldiskfs_pdo, int, 0644);
82 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
84 int ldiskfs_track_declares_assert;
85 module_param(ldiskfs_track_declares_assert, int, 0644);
86 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
88 /* Slab to allocate dynlocks */
89 struct kmem_cache *dynlock_cachep;
91 /* Slab to allocate osd_it_ea */
92 struct kmem_cache *osd_itea_cachep;
94 static struct lu_kmem_descr ldiskfs_caches[] = {
96 .ckd_cache = &dynlock_cachep,
97 .ckd_name = "dynlock_cache",
98 .ckd_size = sizeof(struct dynlock_handle)
101 .ckd_cache = &osd_itea_cachep,
102 .ckd_name = "osd_itea_cache",
103 .ckd_size = sizeof(struct osd_it_ea)
110 static const char dot[] = ".";
111 static const char dotdot[] = "..";
113 static const struct lu_object_operations osd_lu_obj_ops;
114 static const struct dt_object_operations osd_obj_ops;
115 static const struct dt_object_operations osd_obj_otable_it_ops;
116 static const struct dt_index_operations osd_index_iam_ops;
117 static const struct dt_index_operations osd_index_ea_ops;
119 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
120 const struct lu_fid *fid);
121 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
122 struct osd_device *osd);
124 int osd_trans_declare_op2rb[] = {
125 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
126 [OSD_OT_PUNCH] = OSD_OT_MAX,
127 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
128 [OSD_OT_CREATE] = OSD_OT_DESTROY,
129 [OSD_OT_DESTROY] = OSD_OT_CREATE,
130 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
131 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
132 [OSD_OT_WRITE] = OSD_OT_WRITE,
133 [OSD_OT_INSERT] = OSD_OT_DELETE,
134 [OSD_OT_DELETE] = OSD_OT_INSERT,
135 [OSD_OT_QUOTA] = OSD_OT_MAX,
138 static int osd_has_index(const struct osd_object *obj)
140 return obj->oo_dt.do_index_ops != NULL;
143 static int osd_object_invariant(const struct lu_object *l)
145 return osd_invariant(osd_obj(l));
149 * Concurrency: doesn't matter
151 static int osd_is_write_locked(const struct lu_env *env, struct osd_object *o)
153 struct osd_thread_info *oti = osd_oti_get(env);
154 return oti->oti_w_locks > 0 && o->oo_owner == env;
158 * Concurrency: doesn't access mutable data
160 static int osd_root_get(const struct lu_env *env,
161 struct dt_device *dev, struct lu_fid *f)
163 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
168 * the following set of functions are used to maintain per-thread
169 * cache of FID->ino mapping. this mechanism is needed to resolve
170 * FID to inode at dt_insert() which in turn stores ino in the
171 * directory entries to keep ldiskfs compatible with ext[34].
172 * due to locking-originated restrictions we can't lookup ino
173 * using LU cache (deadlock is possible). lookup using OI is quite
174 * expensive. so instead we maintain this cache and methods like
175 * dt_create() fill it. so in the majority of cases dt_insert() is
176 * able to find needed mapping in lockless manner.
178 static struct osd_idmap_cache *
179 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
180 const struct lu_fid *fid)
182 struct osd_thread_info *oti = osd_oti_get(env);
183 struct osd_idmap_cache *idc = oti->oti_ins_cache;
185 for (i = 0; i < oti->oti_ins_cache_used; i++) {
186 if (!lu_fid_eq(&idc[i].oic_fid, fid))
188 if (idc[i].oic_dev != osd)
197 static struct osd_idmap_cache *
198 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
199 const struct lu_fid *fid)
201 struct osd_thread_info *oti = osd_oti_get(env);
202 struct osd_idmap_cache *idc;
205 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
206 i = oti->oti_ins_cache_size * 2;
208 i = OSD_INS_CACHE_SIZE;
209 OBD_ALLOC(idc, sizeof(*idc) * i);
211 return ERR_PTR(-ENOMEM);
212 if (oti->oti_ins_cache != NULL) {
213 memcpy(idc, oti->oti_ins_cache,
214 oti->oti_ins_cache_used * sizeof(*idc));
215 OBD_FREE(oti->oti_ins_cache,
216 oti->oti_ins_cache_used * sizeof(*idc));
218 oti->oti_ins_cache = idc;
219 oti->oti_ins_cache_size = i;
222 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
225 idc->oic_lid.oii_ino = 0;
226 idc->oic_lid.oii_gen = 0;
233 * lookup mapping for the given fid in the cache, initialize a
234 * new one if not found. the initialization checks whether the
235 * object is local or remote. for local objects, OI is used to
236 * learn ino/generation. the function is used when the caller
237 * has no information about the object, e.g. at dt_insert().
239 static struct osd_idmap_cache *
240 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
241 const struct lu_fid *fid)
243 struct osd_idmap_cache *idc;
246 idc = osd_idc_find(env, osd, fid);
247 LASSERT(!IS_ERR(idc));
251 /* new mapping is needed */
252 idc = osd_idc_add(env, osd, fid);
257 rc = osd_remote_fid(env, osd, fid);
258 if (unlikely(rc < 0))
262 /* the object is local, lookup in OI */
263 /* XXX: probably cheaper to lookup in LU first? */
264 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
266 if (unlikely(rc < 0)) {
267 CERROR("can't lookup: rc = %d\n", rc);
271 /* the object is remote */
279 * lookup mapping for given FID and fill it from the given object.
280 * the object is lolcal by definition.
282 static int osd_idc_find_and_init(const struct lu_env *env,
283 struct osd_device *osd,
284 struct osd_object *obj)
286 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
287 struct osd_idmap_cache *idc;
289 idc = osd_idc_find(env, osd, fid);
290 LASSERT(!IS_ERR(idc));
292 if (obj->oo_inode == NULL)
294 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
295 LASSERT(idc->oic_lid.oii_ino == 0);
296 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
297 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
302 /* new mapping is needed */
303 idc = osd_idc_add(env, osd, fid);
307 if (obj->oo_inode != NULL) {
308 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
309 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
315 * OSD object methods.
319 * Concurrency: no concurrent access is possible that early in object
322 static struct lu_object *osd_object_alloc(const struct lu_env *env,
323 const struct lu_object_header *hdr,
326 struct osd_object *mo;
331 struct lu_object_header *h;
332 struct osd_device *o = osd_dev(d);
334 l = &mo->oo_dt.do_lu;
335 if (unlikely(o->od_in_init)) {
342 lu_object_header_init(h);
343 lu_object_init(l, h, d);
344 lu_object_add_top(h, l);
347 dt_object_init(&mo->oo_dt, NULL, d);
348 mo->oo_header = NULL;
351 mo->oo_dt.do_ops = &osd_obj_ops;
352 l->lo_ops = &osd_lu_obj_ops;
353 init_rwsem(&mo->oo_sem);
354 init_rwsem(&mo->oo_ext_idx_sem);
355 spin_lock_init(&mo->oo_guard);
356 INIT_LIST_HEAD(&mo->oo_xattr_list);
363 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
364 struct dentry *dentry, struct lustre_ost_attrs *loa)
368 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
369 (void *)loa, sizeof(*loa));
371 struct lustre_mdt_attrs *lma = &loa->loa_lma;
373 if (rc < sizeof(*lma))
377 lustre_loa_swab(loa, true);
378 /* Check LMA compatibility */
379 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
380 CWARN("%s: unsupported incompat LMA feature(s) %#x "
381 "for fid = "DFID", ino = %lu\n",
383 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
384 PFID(&lma->lma_self_fid), inode->i_ino);
387 } else if (rc == 0) {
395 * retrieve object from backend ext fs.
397 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
398 struct osd_inode_id *id)
401 struct inode *inode = NULL;
403 /* if we look for an inode withing a running
404 * transaction, then we risk to deadlock */
405 /* osd_dirent_check_repair() breaks this */
406 /*LASSERT(current->journal_info == NULL);*/
408 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
410 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
411 id->oii_ino, PTR_ERR(inode));
412 } else if (id->oii_gen != OSD_OII_NOGEN &&
413 inode->i_generation != id->oii_gen) {
414 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
415 "i_generation = %u\n",
416 id->oii_ino, id->oii_gen, inode->i_generation);
418 inode = ERR_PTR(-ESTALE);
419 } else if (inode->i_nlink == 0) {
420 /* due to parallel readdir and unlink,
421 * we can have dead inode here. */
422 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
424 inode = ERR_PTR(-ESTALE);
425 } else if (is_bad_inode(inode)) {
426 CWARN("%s: bad inode: ino = %u\n",
427 osd_dev2name(dev), id->oii_ino);
429 inode = ERR_PTR(-ENOENT);
430 } else if ((rc = osd_attach_jinode(inode))) {
434 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
435 if (id->oii_gen == OSD_OII_NOGEN)
436 osd_id_gen(id, inode->i_ino, inode->i_generation);
438 /* Do not update file c/mtime in ldiskfs.
439 * NB: we don't have any lock to protect this because we don't
440 * have reference on osd_object now, but contention with
441 * another lookup + attr_set can't happen in the tiny window
442 * between if (...) and set S_NOCMTIME. */
443 if (!(inode->i_flags & S_NOCMTIME))
444 inode->i_flags |= S_NOCMTIME;
449 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
450 handle_t *handle, struct dentry *child,
451 struct inode *inode, struct htree_lock *hlock)
455 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
456 if (rc == -ENOBUFS || rc == -ENOSPC) {
457 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
458 struct inode *parent = child->d_parent->d_inode;
459 struct lu_fid *fid = NULL;
461 rc2 = osd_get_lma(info, parent, child->d_parent, loa);
463 fid = &loa->loa_lma.lma_self_fid;
464 } else if (rc2 == -ENODATA) {
465 if (unlikely(parent == inode->i_sb->s_root->d_inode)) {
466 fid = &info->oti_fid3;
467 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
468 } else if (!osd->od_is_ost && osd->od_index == 0) {
469 fid = &info->oti_fid3;
470 lu_igif_build(fid, parent->i_ino,
471 parent->i_generation);
476 CWARN("%s: directory (inode: %lu, FID: "DFID") %s "
477 "maximum entry limit\n",
478 osd_name(osd), parent->i_ino, PFID(fid),
479 rc == -ENOSPC ? "has reached" : "is approaching");
481 CWARN("%s: directory (inode: %lu, FID: unknown) %s "
482 "maximum entry limit\n",
483 osd_name(osd), parent->i_ino,
484 rc == -ENOSPC ? "has reached" : "is approaching");
486 /* ignore such error now */
496 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
497 struct osd_inode_id *id, struct lu_fid *fid)
499 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
503 inode = osd_iget(info, dev, id);
507 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
509 *fid = loa->loa_lma.lma_self_fid;
510 } else if (rc == -ENODATA) {
511 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
512 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
514 lu_igif_build(fid, inode->i_ino, inode->i_generation);
522 static struct inode *osd_iget_check(struct osd_thread_info *info,
523 struct osd_device *dev,
524 const struct lu_fid *fid,
525 struct osd_inode_id *id,
532 /* The cached OI mapping is trustable. If we cannot locate the inode
533 * via the cached OI mapping, then return the failure to the caller
534 * directly without further OI checking. */
537 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
540 if (!trusted && (rc == -ENOENT || rc == -ESTALE))
543 CDEBUG(D_INODE, "no inode for FID: "DFID", ino = %u, rc = %d\n",
544 PFID(fid), id->oii_ino, rc);
548 if (is_bad_inode(inode)) {
553 CDEBUG(D_INODE, "bad inode for FID: "DFID", ino = %u\n",
554 PFID(fid), id->oii_ino);
558 if (id->oii_gen != OSD_OII_NOGEN &&
559 inode->i_generation != id->oii_gen) {
564 CDEBUG(D_INODE, "unmatched inode for FID: "DFID", ino = %u, "
565 "oii_gen = %u, i_generation = %u\n", PFID(fid),
566 id->oii_ino, id->oii_gen, inode->i_generation);
570 if (inode->i_nlink == 0) {
575 CDEBUG(D_INODE, "stale inode for FID: "DFID", ino = %u\n",
576 PFID(fid), id->oii_ino);
580 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
584 __u32 saved_ino = id->oii_ino;
585 __u32 saved_gen = id->oii_gen;
588 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
590 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
591 /* XXX: There are four possible cases:
593 * Backup/restore caused the OI invalid.
595 * Someone unlinked the object but NOT removed
596 * the OI mapping, such as mount target device
597 * as ldiskfs, and modify something directly.
599 * Someone just removed the object between the
600 * former oi_lookup and the iget. It is normal.
601 * 4. Other failure cases.
603 * Generally, when the device is mounted, it will
604 * auto check whether the system is restored from
605 * file-level backup or not. We trust such detect
606 * to distinguish the 1st case from the 2nd case:
607 * if the OI files are consistent but may contain
608 * stale OI mappings because of case 2, if iget()
609 * returns -ENOENT or -ESTALE, then it should be
612 /* If the OI mapping was in OI file before the
613 * osd_iget_check(), but now, it is disappear,
614 * then it must be removed by race. That is a
615 * normal race case. */
618 /* It is the OI scrub updated the OI mapping by race.
619 * The new OI mapping must be valid. */
620 if (saved_ino != id->oii_ino ||
621 (saved_gen != id->oii_gen && saved_gen != OSD_OII_NOGEN)) {
630 if (dev->od_scrub.os_scrub.os_file.sf_flags &
632 /* It still can be the case 2, but we cannot
633 * distinguish it from the case 1. So return
634 * -EREMCHG to block current operation until
635 * OI scrub rebuilt the OI mappings. */
643 if (inode->i_generation == id->oii_gen)
648 if (id->oii_gen == OSD_OII_NOGEN)
649 osd_id_gen(id, inode->i_ino, inode->i_generation);
651 /* Do not update file c/mtime in ldiskfs.
652 * NB: we don't have any lock to protect this because we don't
653 * have reference on osd_object now, but contention with
654 * another lookup + attr_set can't happen in the tiny window
655 * between if (...) and set S_NOCMTIME. */
656 if (!(inode->i_flags & S_NOCMTIME))
657 inode->i_flags |= S_NOCMTIME;
674 * \retval +v: new filter_fid, does not contain self-fid
675 * \retval 0: filter_fid_old, contains self-fid
676 * \retval -v: other failure cases
678 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
679 struct dentry *dentry, struct lu_fid *fid)
681 struct filter_fid_old *ff = &info->oti_ff;
682 struct ost_id *ostid = &info->oti_ostid;
685 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
686 if (rc == sizeof(*ff)) {
688 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
689 rc = ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
691 * XXX: use 0 as the index for compatibility, the caller will
692 * handle index related issues when necessary.
695 ostid_to_fid(fid, ostid, 0);
696 } else if (rc == sizeof(struct filter_fid)) {
698 } else if (rc >= 0) {
705 static int osd_lma_self_repair(struct osd_thread_info *info,
706 struct osd_device *osd, struct inode *inode,
707 const struct lu_fid *fid, __u32 compat)
712 LASSERT(current->journal_info == NULL);
714 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
715 osd_dto_credits_noquota[DTO_XATTR_SET]);
718 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
723 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
725 CWARN("%s: cannot self repair the LMA: rc = %d\n",
727 ldiskfs_journal_stop(jh);
731 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
733 struct osd_thread_info *info = osd_oti_get(env);
734 struct osd_device *osd = osd_obj2dev(obj);
735 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
736 struct lustre_mdt_attrs *lma = &loa->loa_lma;
737 struct inode *inode = obj->oo_inode;
738 struct dentry *dentry = &info->oti_obj_dentry;
739 struct lu_fid *fid = NULL;
740 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
744 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
745 (void *)loa, sizeof(*loa));
746 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
747 fid = &lma->lma_self_fid;
748 rc = osd_get_idif(info, inode, dentry, fid);
749 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
750 /* For the given OST-object, if it has neither LMA nor
751 * FID in XATTR_NAME_FID, then the given FID (which is
752 * contained in the @obj, from client RPC for locating
753 * the OST-object) is trusted. We use it to generate
755 osd_lma_self_repair(info, osd, inode, rfid,
766 lustre_lma_swab(lma);
767 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
768 (CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT) &&
769 S_ISREG(inode->i_mode)))) {
770 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
771 "fid = "DFID", ino = %lu\n", osd_name(osd),
772 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
773 PFID(rfid), inode->i_ino);
776 fid = &lma->lma_self_fid;
777 if (lma->lma_compat & LMAC_STRIPE_INFO &&
779 obj->oo_pfid_in_lma = 1;
780 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
782 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
786 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
787 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
788 struct ost_id *oi = &info->oti_ostid;
789 struct lu_fid *fid1 = &info->oti_fid3;
790 __u32 idx = fid_idif_ost_idx(rfid);
792 /* For old IDIF, the OST index is not part of the IDIF,
793 * Means that different OSTs may have the same IDIFs.
794 * Under such case, we need to make some compatible
795 * check to make sure to trigger OI scrub properly. */
796 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
797 /* Given @rfid is new, LMA is old. */
798 fid_to_ostid(fid, oi);
799 ostid_to_fid(fid1, oi, idx);
800 if (lu_fid_eq(fid1, rfid)) {
801 if (osd->od_index_in_idif)
802 osd_lma_self_repair(info, osd,
816 struct osd_check_lmv_buf {
817 #ifdef HAVE_DIR_CONTEXT
818 /* please keep it as first member */
819 struct dir_context ctx;
821 struct osd_thread_info *oclb_info;
822 struct osd_device *oclb_dev;
823 struct osd_idmap_cache *oclb_oic;
829 * It is called internally by ->readdir() to filter out the
830 * local slave object's FID of the striped directory.
832 * \retval 1 found the local slave's FID
833 * \retval 0 continue to check next item
834 * \retval -ve for failure
836 #ifdef HAVE_FILLDIR_USE_CTX
837 static int osd_stripe_dir_filldir(struct dir_context *buf,
839 static int osd_stripe_dir_filldir(void *buf,
841 const char *name, int namelen,
842 loff_t offset, __u64 ino, unsigned d_type)
844 struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
845 struct osd_thread_info *oti = oclb->oclb_info;
846 struct lu_fid *fid = &oti->oti_fid3;
847 struct osd_inode_id *id = &oti->oti_id3;
848 struct osd_device *dev = oclb->oclb_dev;
849 struct osd_idmap_cache *oic = oclb->oclb_oic;
858 sscanf(name + 1, SFID, RFID(fid));
859 if (!fid_is_sane(fid))
862 if (osd_remote_fid(oti->oti_env, dev, fid))
865 osd_id_gen(id, ino, OSD_OII_NOGEN);
866 inode = osd_iget(oti, dev, id);
868 return PTR_ERR(inode);
871 osd_add_oi_cache(oti, dev, id, fid);
875 osd_oii_insert(dev, oic, true);
876 oclb->oclb_found = true;
881 /* When lookup item under striped directory, we need to locate the master
882 * MDT-object of the striped directory firstly, then the client will send
883 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
884 * and the item's name. If the system is restored from MDT file level backup,
885 * then before the OI scrub completely built the OI files, the OI mappings of
886 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
887 * not a problem for the master MDT-object. Because when locate the master
888 * MDT-object, we will do name based lookup (for the striped directory itself)
889 * firstly, during such process we can setup the correct OI mapping for the
890 * master MDT-object. But it will be trouble for the slave MDT-object. Because
891 * the client will not trigger name based lookup on the MDT to locate the slave
892 * MDT-object before locating item under the striped directory, then when
893 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
894 * is invalid and does not know what the right OI mapping is, then the MDT has
895 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
896 * the OI file, related OI mapping is unknown yet, please try again later. And
897 * then client will re-try the RPC again and again until related OI mapping has
898 * been updated. That is quite inefficient.
900 * To resolve above trouble, we will handle it as the following two cases:
902 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
903 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
904 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
905 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
906 * directly. Please check osd_fid_lookup().
908 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
909 * Under such case, during lookup the master MDT-object, we will lookup the
910 * slave MDT-object via readdir against the master MDT-object, because the
911 * slave MDT-objects information are stored as sub-directories with the name
912 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
913 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
914 * the correct OI mapping for the slave MDT-object. */
915 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
916 struct inode *inode, struct osd_idmap_cache *oic)
918 struct lu_buf *buf = &oti->oti_big_buf;
919 struct dentry *dentry = &oti->oti_obj_dentry;
920 struct file *filp = &oti->oti_file;
921 const struct file_operations *fops;
922 struct lmv_mds_md_v1 *lmv1;
923 struct osd_check_lmv_buf oclb = {
924 #ifdef HAVE_DIR_CONTEXT
925 .ctx.actor = osd_stripe_dir_filldir,
936 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, buf->lb_buf,
939 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, NULL, 0);
941 lu_buf_realloc(buf, rc);
942 if (buf->lb_buf == NULL)
943 GOTO(out, rc = -ENOMEM);
949 if (unlikely(rc == 0 || rc == -ENODATA))
955 if (unlikely(buf->lb_buf == NULL)) {
956 lu_buf_realloc(buf, rc);
957 if (buf->lb_buf == NULL)
958 GOTO(out, rc = -ENOMEM);
964 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
968 dentry->d_inode = inode;
969 dentry->d_sb = inode->i_sb;
971 filp->f_path.dentry = dentry;
972 filp->f_mode = FMODE_64BITHASH;
973 filp->f_mapping = inode->i_mapping;
975 filp->private_data = NULL;
976 set_file_inode(filp, inode);
980 #ifdef HAVE_DIR_CONTEXT
981 oclb.ctx.pos = filp->f_pos;
982 rc = fops->iterate(filp, &oclb.ctx);
983 filp->f_pos = oclb.ctx.pos;
985 rc = fops->readdir(filp, &oclb, osd_stripe_dir_filldir);
987 } while (rc >= 0 && oclb.oclb_items > 0 && !oclb.oclb_found &&
988 filp->f_pos != LDISKFS_HTREE_EOF_64BIT);
989 fops->release(inode, filp);
993 CDEBUG(D_LFSCK, "%s: fail to check LMV EA, inode = %lu/%u,"
994 DFID": rc = %d\n", osd_ino2name(inode),
995 inode->i_ino, inode->i_generation,
996 PFID(&oic->oic_fid), rc);
1003 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
1004 const struct lu_fid *fid,
1005 const struct lu_object_conf *conf)
1007 struct osd_thread_info *info;
1008 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1009 struct osd_device *dev;
1010 struct osd_idmap_cache *oic;
1011 struct osd_inode_id *id;
1012 struct inode *inode = NULL;
1013 struct lustre_scrub *scrub;
1014 struct scrub_file *sf;
1015 __u32 flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT | SS_AUTO_FULL;
1020 bool remote = false;
1021 bool trusted = true;
1022 bool updated = false;
1023 bool checked = false;
1026 LINVRNT(osd_invariant(obj));
1027 LASSERT(obj->oo_inode == NULL);
1028 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
1030 dev = osd_dev(ldev);
1031 scrub = &dev->od_scrub.os_scrub;
1032 sf = &scrub->os_file;
1033 info = osd_oti_get(env);
1035 oic = &info->oti_cache;
1037 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
1040 /* For the object is created as locking anchor, or for the object to
1041 * be created on disk. No need to osd_oi_lookup() at here because FID
1042 * shouldn't never be re-used, if it's really a duplicate FID from
1043 * unexpected reason, we should be able to detect it later by calling
1044 * do_create->osd_oi_insert(). */
1045 if (conf && conf->loc_flags & LOC_F_NEW)
1046 GOTO(out, result = 0);
1048 /* Search order: 1. per-thread cache. */
1049 if (lu_fid_eq(fid, &oic->oic_fid) && likely(oic->oic_dev == dev)) {
1055 if (!list_empty(&scrub->os_inconsistent_items)) {
1056 /* Search order: 2. OI scrub pending list. */
1057 result = osd_oii_lookup(dev, fid, id);
1062 /* The OI mapping in the OI file can be updated by the OI scrub
1063 * when we locate the inode via FID. So it may be not trustable. */
1066 /* Search order: 3. OI files. */
1067 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1068 if (result == -ENOENT) {
1069 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
1070 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1071 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
1073 GOTO(out, result = 0);
1082 obj->oo_inode = NULL;
1083 /* for later passes through checks, not true on first pass */
1084 if (!IS_ERR_OR_NULL(inode))
1087 inode = osd_iget_check(info, dev, fid, id, trusted);
1088 if (!IS_ERR(inode)) {
1089 obj->oo_inode = inode;
1097 result = PTR_ERR(inode);
1098 if (result == -ENOENT || result == -ESTALE)
1099 GOTO(out, result = 0);
1101 if (result != -EREMCHG)
1105 /* We still have chance to get the valid inode: for the
1106 * object which is referenced by remote name entry, the
1107 * object on the local MDT will be linked under the dir
1108 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1110 * We do not know whether the object for the given FID
1111 * is referenced by some remote name entry or not, and
1112 * especially for DNE II, a multiple-linked object may
1113 * have many name entries reside on many MDTs.
1115 * To simplify the operation, OSD will not distinguish
1116 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1117 * only happened for the RPC from other MDT during the
1118 * OI scrub, or for the client side RPC with FID only,
1119 * such as FID to path, or from old connected client. */
1120 if (!remote && !fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1121 rc1 = osd_lookup_in_remote_parent(info, dev, fid, id);
1125 flags |= SS_AUTO_PARTIAL;
1126 flags &= ~SS_AUTO_FULL;
1131 if (thread_is_running(&scrub->os_thread)) {
1132 if (scrub->os_partial_scan && !scrub->os_in_join)
1135 if (IS_ERR_OR_NULL(inode) || result)
1136 GOTO(out, result = -EINPROGRESS);
1139 LASSERT(obj->oo_inode == inode);
1141 osd_add_oi_cache(info, dev, id, fid);
1142 osd_oii_insert(dev, oic, true);
1146 if (dev->od_auto_scrub_interval == AS_NEVER) {
1148 GOTO(out, result = -EREMCHG);
1151 LASSERT(obj->oo_inode == inode);
1153 osd_add_oi_cache(info, dev, id, fid);
1158 rc1 = osd_scrub_start(env, dev, flags);
1159 LCONSOLE_WARN("%s: trigger OI scrub by RPC for the " DFID" with flags "
1160 "0x%x, rc = %d\n", osd_name(dev), PFID(fid), flags, rc1);
1161 if (rc1 && rc1 != -EALREADY)
1162 GOTO(out, result = -EREMCHG);
1164 if (IS_ERR_OR_NULL(inode) || result)
1165 GOTO(out, result = -EINPROGRESS);
1168 LASSERT(obj->oo_inode == inode);
1170 osd_add_oi_cache(info, dev, id, fid);
1171 osd_oii_insert(dev, oic, true);
1176 if (unlikely(obj->oo_header))
1179 result = osd_check_lma(env, obj);
1183 LASSERTF(id->oii_ino == inode->i_ino &&
1184 id->oii_gen == inode->i_generation,
1185 "locate wrong inode for FID: "DFID", %u/%u => %ld/%u\n",
1186 PFID(fid), id->oii_ino, id->oii_gen,
1187 inode->i_ino, inode->i_generation);
1189 saved_ino = inode->i_ino;
1190 saved_gen = inode->i_generation;
1192 if (unlikely(result == -ENODATA)) {
1193 /* If the OI scrub updated the OI mapping by race, it
1194 * must be valid. Trust the inode that has no LMA EA. */
1198 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1200 /* The OI mapping is still there, the inode is still
1201 * valid. It is just becaues the inode has no LMA EA. */
1202 if (saved_ino == id->oii_ino &&
1203 saved_gen == id->oii_gen)
1206 /* It is the OI scrub updated the OI mapping by race.
1207 * The new OI mapping must be valid. */
1213 /* "result == -ENOENT" means that the OI mappinghas been
1214 * removed by race, so the inode belongs to other object.
1216 * Others error can be returned directly. */
1217 if (result == -ENOENT) {
1220 obj->oo_inode = NULL;
1225 if (result != -EREMCHG)
1230 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1231 /* "result == -ENOENT" means the cached OI mapping has been removed
1232 * from the OI file by race, above inode belongs to other object. */
1233 if (result == -ENOENT) {
1236 obj->oo_inode = NULL;
1237 GOTO(out, result = 0);
1243 if (saved_ino == id->oii_ino && saved_gen == id->oii_gen) {
1248 /* It is the OI scrub updated the OI mapping by race.
1249 * The new OI mapping must be valid. */
1256 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
1257 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
1259 result = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
1261 if (lma->lma_compat & LMAC_STRIPE_INFO &&
1263 obj->oo_pfid_in_lma = 1;
1264 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
1266 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
1267 } else if (result != -ENODATA) {
1272 obj->oo_compat_dot_created = 1;
1273 obj->oo_compat_dotdot_created = 1;
1275 if (S_ISDIR(inode->i_mode) &&
1276 (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1277 osd_check_lmv(info, dev, inode, oic);
1279 result = osd_attach_jinode(inode);
1284 GOTO(out, result = 0);
1286 LASSERT(!obj->oo_hl_head);
1287 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1289 GOTO(out, result = (!obj->oo_hl_head ? -ENOMEM : 0));
1292 if (result || !obj->oo_inode) {
1293 if (!IS_ERR_OR_NULL(inode))
1296 obj->oo_inode = NULL;
1298 fid_zero(&oic->oic_fid);
1301 LINVRNT(osd_invariant(obj));
1306 * Concurrency: shouldn't matter.
1308 static void osd_object_init0(struct osd_object *obj)
1310 LASSERT(obj->oo_inode != NULL);
1311 obj->oo_dt.do_body_ops = &osd_body_ops;
1312 obj->oo_dt.do_lu.lo_header->loh_attr |=
1313 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1317 * Concurrency: no concurrent access is possible that early in object
1320 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1321 const struct lu_object_conf *conf)
1323 struct osd_object *obj = osd_obj(l);
1326 LINVRNT(osd_invariant(obj));
1328 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1329 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1330 l->lo_header->loh_attr |= LOHA_EXISTS;
1334 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1335 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1336 if (result == 0 && obj->oo_inode != NULL) {
1337 struct osd_thread_info *oti = osd_oti_get(env);
1338 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
1340 osd_object_init0(obj);
1341 if (unlikely(obj->oo_header))
1344 result = osd_get_lma(oti, obj->oo_inode,
1345 &oti->oti_obj_dentry, loa);
1347 /* Convert LMAI flags to lustre LMA flags
1348 * and cache it to oo_lma_flags */
1350 lma_to_lustre_flags(loa->loa_lma.lma_incompat);
1351 } else if (result == -ENODATA) {
1356 LINVRNT(osd_invariant(obj));
1360 /* The first part of oxe_buf is xattr name, and is '\0' terminated.
1361 * The left part is for value, binary mode. */
1362 struct osd_xattr_entry {
1363 struct list_head oxe_list;
1367 struct rcu_head oxe_rcu;
1371 static int osd_oxc_get(struct osd_object *obj, const char *name,
1374 struct osd_xattr_entry *tmp;
1375 struct osd_xattr_entry *oxe = NULL;
1376 size_t namelen = strlen(name);
1381 list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1382 if (namelen == tmp->oxe_namelen &&
1383 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1390 GOTO(out, rc = -ENOENT);
1392 if (!oxe->oxe_exist)
1393 GOTO(out, rc = -ENODATA);
1396 rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1399 if (buf->lb_buf == NULL)
1402 if (buf->lb_len < rc)
1403 GOTO(out, rc = -ERANGE);
1405 memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1413 static void osd_oxc_free(struct rcu_head *head)
1415 struct osd_xattr_entry *oxe;
1417 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1418 OBD_FREE(oxe, oxe->oxe_len);
1421 static void osd_oxc_add(struct osd_object *obj, const char *name,
1422 const char *buf, int buflen)
1424 struct osd_xattr_entry *oxe;
1425 struct osd_xattr_entry *old = NULL;
1426 struct osd_xattr_entry *tmp;
1427 size_t namelen = strlen(name);
1428 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1430 OBD_ALLOC(oxe, len);
1434 INIT_LIST_HEAD(&oxe->oxe_list);
1436 oxe->oxe_namelen = namelen;
1437 memcpy(oxe->oxe_buf, name, namelen);
1439 LASSERT(buf != NULL);
1440 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1441 oxe->oxe_exist = true;
1443 oxe->oxe_exist = false;
1446 /* this should be rarely called, just remove old and add new */
1447 spin_lock(&obj->oo_guard);
1448 list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1449 if (namelen == tmp->oxe_namelen &&
1450 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1456 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1457 call_rcu(&old->oxe_rcu, osd_oxc_free);
1459 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1461 spin_unlock(&obj->oo_guard);
1464 static void osd_oxc_del(struct osd_object *obj, const char *name)
1466 struct osd_xattr_entry *oxe;
1467 size_t namelen = strlen(name);
1469 spin_lock(&obj->oo_guard);
1470 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1471 if (namelen == oxe->oxe_namelen &&
1472 strncmp(name, oxe->oxe_buf, namelen) == 0) {
1473 list_del_rcu(&oxe->oxe_list);
1474 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1478 spin_unlock(&obj->oo_guard);
1481 static void osd_oxc_fini(struct osd_object *obj)
1483 struct osd_xattr_entry *oxe, *next;
1485 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1486 list_del(&oxe->oxe_list);
1487 OBD_FREE(oxe, oxe->oxe_len);
1492 * Concurrency: no concurrent access is possible that late in object
1495 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1497 struct osd_object *obj = osd_obj(l);
1498 struct lu_object_header *h = obj->oo_header;
1500 LINVRNT(osd_invariant(obj));
1503 dt_object_fini(&obj->oo_dt);
1504 if (obj->oo_hl_head != NULL)
1505 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1508 lu_object_header_fini(h);
1514 * Concurrency: no concurrent access is possible that late in object
1517 static void osd_index_fini(struct osd_object *o)
1519 struct iam_container *bag;
1521 if (o->oo_dir != NULL) {
1522 bag = &o->oo_dir->od_container;
1523 if (o->oo_inode != NULL) {
1524 if (bag->ic_object == o->oo_inode)
1525 iam_container_fini(bag);
1527 OBD_FREE_PTR(o->oo_dir);
1533 * Concurrency: no concurrent access is possible that late in object
1534 * life-cycle (for all existing callers, that is. New callers have to provide
1535 * their own locking.)
1537 static int osd_inode_unlinked(const struct inode *inode)
1539 return inode->i_nlink == 0;
1543 OSD_TXN_OI_DELETE_CREDITS = 20,
1544 OSD_TXN_INODE_DELETE_CREDITS = 20
1551 #if OSD_THANDLE_STATS
1553 * Set time when the handle is allocated
1555 static void osd_th_alloced(struct osd_thandle *oth)
1557 oth->oth_alloced = ktime_get();
1561 * Set time when the handle started
1563 static void osd_th_started(struct osd_thandle *oth)
1565 oth->oth_started = ktime_get();
1569 * Check whether the we deal with this handle for too long.
1571 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1572 ktime_t alloced, ktime_t started,
1575 ktime_t now = ktime_get();
1577 LASSERT(dev != NULL);
1579 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1580 ktime_us_delta(started, alloced));
1581 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1582 ktime_us_delta(closed, started));
1583 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1584 ktime_us_delta(now, closed));
1586 if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1587 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1588 oth, now, alloced, started, closed);
1589 libcfs_debug_dumpstack(NULL);
1593 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1595 ktime_t __closed = ktime_get(); \
1596 ktime_t __alloced = oth->oth_alloced; \
1597 ktime_t __started = oth->oth_started; \
1600 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1603 #else /* OSD_THANDLE_STATS */
1605 #define osd_th_alloced(h) do {} while(0)
1606 #define osd_th_started(h) do {} while(0)
1607 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1609 #endif /* OSD_THANDLE_STATS */
1612 * Concurrency: doesn't access mutable data.
1614 static int osd_param_is_not_sane(const struct osd_device *dev,
1615 const struct thandle *th)
1617 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1619 return oh->ot_credits > osd_transaction_size(dev);
1623 * Concurrency: shouldn't matter.
1625 static void osd_trans_commit_cb(struct super_block *sb,
1626 struct ldiskfs_journal_cb_entry *jcb, int error)
1628 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1629 struct thandle *th = &oh->ot_super;
1630 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1631 struct dt_txn_commit_cb *dcb, *tmp;
1633 LASSERT(oh->ot_handle == NULL);
1636 CERROR("transaction @0x%p commit error: %d\n", th, error);
1638 dt_txn_hook_commit(th);
1640 /* call per-transaction callbacks if any */
1641 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1643 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1644 "commit callback entry: magic=%x name='%s'\n",
1645 dcb->dcb_magic, dcb->dcb_name);
1646 list_del_init(&dcb->dcb_linkage);
1647 dcb->dcb_func(NULL, th, dcb, error);
1650 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1657 #ifndef HAVE_SB_START_WRITE
1658 # define sb_start_write(sb) do {} while (0)
1659 # define sb_end_write(sb) do {} while (0)
1662 static struct thandle *osd_trans_create(const struct lu_env *env,
1663 struct dt_device *d)
1665 struct osd_thread_info *oti = osd_oti_get(env);
1666 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1667 struct osd_thandle *oh;
1672 CERROR("%s: someone try to start transaction under "
1673 "readonly mode, should be disabled.\n",
1674 osd_name(osd_dt_dev(d)));
1676 RETURN(ERR_PTR(-EROFS));
1679 /* on pending IO in this thread should left from prev. request */
1680 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1682 sb_start_write(osd_sb(osd_dt_dev(d)));
1684 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1686 oh->ot_quota_trans = &oti->oti_quota_trans;
1687 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1692 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1693 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1696 memset(oti->oti_declare_ops, 0,
1697 sizeof(oti->oti_declare_ops));
1698 memset(oti->oti_declare_ops_cred, 0,
1699 sizeof(oti->oti_declare_ops_cred));
1700 memset(oti->oti_declare_ops_used, 0,
1701 sizeof(oti->oti_declare_ops_used));
1703 sb_end_write(osd_sb(osd_dt_dev(d)));
1704 th = ERR_PTR(-ENOMEM);
1709 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1711 struct osd_thread_info *oti = osd_oti_get(env);
1712 struct osd_thandle *oh;
1714 oh = container_of0(th, struct osd_thandle, ot_super);
1715 LASSERT(oh != NULL);
1717 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1718 oti->oti_declare_ops[OSD_OT_CREATE],
1719 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1720 oti->oti_declare_ops_used[OSD_OT_CREATE],
1721 oti->oti_declare_ops[OSD_OT_DESTROY],
1722 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1723 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1724 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1725 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1726 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1727 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1728 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1729 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1730 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1731 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1732 oti->oti_declare_ops[OSD_OT_WRITE],
1733 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1734 oti->oti_declare_ops_used[OSD_OT_WRITE],
1735 oti->oti_declare_ops[OSD_OT_PUNCH],
1736 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1737 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1738 oti->oti_declare_ops[OSD_OT_QUOTA],
1739 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1740 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1741 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1742 oti->oti_declare_ops[OSD_OT_INSERT],
1743 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1744 oti->oti_declare_ops_used[OSD_OT_INSERT],
1745 oti->oti_declare_ops[OSD_OT_DELETE],
1746 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1747 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1748 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1749 oti->oti_declare_ops[OSD_OT_REF_ADD],
1750 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1751 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1752 oti->oti_declare_ops[OSD_OT_REF_DEL],
1753 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1754 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1758 * Concurrency: shouldn't matter.
1760 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1763 struct osd_thread_info *oti = osd_oti_get(env);
1764 struct osd_device *dev = osd_dt_dev(d);
1766 struct osd_thandle *oh;
1771 LASSERT(current->journal_info == NULL);
1773 oh = container_of0(th, struct osd_thandle, ot_super);
1774 LASSERT(oh != NULL);
1775 LASSERT(oh->ot_handle == NULL);
1777 rc = dt_txn_hook_start(env, d, th);
1781 if (unlikely(osd_param_is_not_sane(dev, th))) {
1782 static unsigned long last_printed;
1783 static int last_credits;
1785 /* don't make noise on a tiny testing systems
1786 * actual credits misuse will be caught anyway */
1787 if (last_credits != oh->ot_credits &&
1788 time_after(jiffies, last_printed +
1789 msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
1790 osd_transaction_size(dev) > 512) {
1791 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1792 oh->ot_credits, osd_transaction_size(dev));
1793 osd_trans_dump_creds(env, th);
1794 libcfs_debug_dumpstack(NULL);
1795 last_credits = oh->ot_credits;
1796 last_printed = jiffies;
1798 /* XXX Limit the credits to 'max_transaction_buffers', and
1799 * let the underlying filesystem to catch the error if
1800 * we really need so many credits.
1802 * This should be removed when we can calculate the
1803 * credits precisely. */
1804 oh->ot_credits = osd_transaction_size(dev);
1805 } else if (ldiskfs_track_declares_assert != 0) {
1806 /* reserve few credits to prevent an assertion in JBD
1807 * our debugging mechanism will be able to detected
1808 * overuse. this can help to debug single-update
1810 oh->ot_credits += 10;
1811 if (unlikely(osd_param_is_not_sane(dev, th)))
1812 oh->ot_credits = osd_transaction_size(dev);
1816 * XXX temporary stuff. Some abstraction layer should
1819 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1823 LASSERT(oti->oti_txns == 0);
1825 lu_device_get(&d->dd_lu_dev);
1826 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1837 static int osd_seq_exists(const struct lu_env *env,
1838 struct osd_device *osd, u64 seq)
1840 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1841 struct seq_server_site *ss = osd_seq_site(osd);
1845 LASSERT(ss != NULL);
1846 LASSERT(ss->ss_server_fld != NULL);
1848 rc = osd_fld_lookup(env, osd, seq, range);
1851 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
1852 osd_name(osd), seq, rc);
1856 RETURN(ss->ss_node_id == range->lsr_index);
1859 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1861 struct dt_txn_commit_cb *dcb;
1862 struct dt_txn_commit_cb *tmp;
1864 /* call per-transaction stop callbacks if any */
1865 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1867 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1868 "commit callback entry: magic=%x name='%s'\n",
1869 dcb->dcb_magic, dcb->dcb_name);
1870 list_del_init(&dcb->dcb_linkage);
1871 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1876 * Concurrency: shouldn't matter.
1878 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1881 int rc = 0, remove_agents = 0;
1882 struct osd_thandle *oh;
1883 struct osd_thread_info *oti = osd_oti_get(env);
1884 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1885 struct osd_device *osd = osd_dt_dev(th->th_dev);
1886 struct qsd_instance *qsd = osd->od_quota_slave;
1887 struct lquota_trans *qtrans;
1890 oh = container_of0(th, struct osd_thandle, ot_super);
1892 remove_agents = oh->ot_remove_agents;
1894 qtrans = oh->ot_quota_trans;
1895 oh->ot_quota_trans = NULL;
1897 if (oh->ot_handle != NULL) {
1899 handle_t *hdl = oh->ot_handle;
1902 * add commit callback
1903 * notice we don't do this in osd_trans_start()
1904 * as underlying transaction can change during truncate
1906 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1909 LASSERT(oti->oti_txns == 1);
1912 rc = dt_txn_hook_stop(env, th);
1914 CERROR("%s: failed in transaction hook: rc = %d\n",
1917 osd_trans_stop_cb(oh, rc);
1918 /* hook functions might modify th_sync */
1919 hdl->h_sync = th->th_sync;
1921 oh->ot_handle = NULL;
1922 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
1924 CERROR("%s: failed to stop transaction: rc = %d\n",
1925 osd_name(osd), rc2);
1929 osd_trans_stop_cb(oh, th->th_result);
1933 /* inform the quota slave device that the transaction is stopping */
1934 qsd_op_end(env, qsd, qtrans);
1936 /* as we want IO to journal and data IO be concurrent, we don't block
1937 * awaiting data IO completion in osd_do_bio(), instead we wait here
1938 * once transaction is submitted to the journal. all reqular requests
1939 * don't do direct IO (except read/write), thus this wait_event becomes
1942 * IMPORTANT: we have to wait till any IO submited by the thread is
1943 * completed otherwise iobuf may be corrupted by different request
1945 wait_event(iobuf->dr_wait,
1946 atomic_read(&iobuf->dr_numreqs) == 0);
1947 osd_fini_iobuf(osd, iobuf);
1949 rc = iobuf->dr_error;
1951 if (unlikely(remove_agents != 0))
1952 osd_process_scheduled_agent_removals(env, osd);
1954 /* reset OI cache for safety */
1955 oti->oti_ins_cache_used = 0;
1957 sb_end_write(osd_sb(osd));
1962 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1964 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1967 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1968 LASSERT(&dcb->dcb_func != NULL);
1969 if (dcb->dcb_flags & DCB_TRANS_STOP)
1970 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1972 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1978 * Called just before object is freed. Releases all resources except for
1979 * object itself (that is released by osd_object_free()).
1981 * Concurrency: no concurrent access is possible that late in object
1984 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1986 struct osd_object *obj = osd_obj(l);
1987 struct inode *inode = obj->oo_inode;
1989 LINVRNT(osd_invariant(obj));
1992 * If object is unlinked remove fid->ino mapping from object index.
1995 osd_index_fini(obj);
1996 if (inode != NULL) {
1997 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1998 qid_t uid = i_uid_read(inode);
1999 qid_t gid = i_gid_read(inode);
2001 obj->oo_inode = NULL;
2003 if (!obj->oo_header && qsd) {
2004 struct osd_thread_info *info = osd_oti_get(env);
2005 struct lquota_id_info *qi = &info->oti_qi;
2007 /* Release granted quota to master if necessary */
2008 qi->lqi_id.qid_uid = uid;
2009 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
2011 qi->lqi_id.qid_uid = gid;
2012 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
2014 qi->lqi_id.qid_uid = i_projid_read(inode);
2015 qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
2021 * Concurrency: ->loo_object_release() is called under site spin-lock.
2023 static void osd_object_release(const struct lu_env *env,
2024 struct lu_object *l)
2026 struct osd_object *o = osd_obj(l);
2027 /* nobody should be releasing a non-destroyed object with nlink=0
2028 * the API allows this, but ldiskfs doesn't like and then report
2029 * this inode as deleted */
2030 if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
2035 * Concurrency: shouldn't matter.
2037 static int osd_object_print(const struct lu_env *env, void *cookie,
2038 lu_printer_t p, const struct lu_object *l)
2040 struct osd_object *o = osd_obj(l);
2041 struct iam_descr *d;
2043 if (o->oo_dir != NULL)
2044 d = o->oo_dir->od_container.ic_descr;
2047 return (*p)(env, cookie,
2048 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
2050 o->oo_inode ? o->oo_inode->i_ino : 0UL,
2051 o->oo_inode ? o->oo_inode->i_generation : 0,
2052 d ? d->id_ops->id_name : "plain");
2056 * Concurrency: shouldn't matter.
2058 int osd_statfs(const struct lu_env *env, struct dt_device *d,
2059 struct obd_statfs *sfs)
2061 struct osd_device *osd = osd_dt_dev(d);
2062 struct super_block *sb = osd_sb(osd);
2063 struct kstatfs *ksfs;
2067 if (unlikely(osd->od_mnt == NULL))
2068 return -EINPROGRESS;
2070 /* osd_lproc.c call this without env, allocate ksfs for that case */
2071 if (unlikely(env == NULL)) {
2072 OBD_ALLOC_PTR(ksfs);
2076 ksfs = &osd_oti_get(env)->oti_ksfs;
2079 result = sb->s_op->statfs(sb->s_root, ksfs);
2083 statfs_pack(sfs, ksfs);
2084 if (unlikely(sb->s_flags & MS_RDONLY))
2085 sfs->os_state |= OS_STATE_READONLY;
2086 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
2087 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2088 sfs->os_maxbytes = sb->s_maxbytes;
2090 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2093 * Reserve some space so to avoid fragmenting the filesystem too much.
2094 * Fragmentation not only impacts performance, but can also increase
2095 * metadata overhead significantly, causing grant calculation to be
2098 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2100 CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
2101 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2102 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2103 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2105 sfs->os_blocks -= reserved;
2106 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2107 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2110 if (unlikely(env == NULL))
2116 * Estimate space needed for file creations. We assume the largest filename
2117 * which is 2^64 - 1, hence a filename of 20 chars.
2118 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2120 #ifdef __LDISKFS_DIR_REC_LEN
2121 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2123 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2127 * Concurrency: doesn't access mutable data.
2129 static void osd_conf_get(const struct lu_env *env,
2130 const struct dt_device *dev,
2131 struct dt_device_param *param)
2133 struct super_block *sb = osd_sb(osd_dt_dev(dev));
2137 * XXX should be taken from not-yet-existing fs abstraction layer.
2139 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2140 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2141 param->ddp_symlink_max = sb->s_blocksize;
2142 param->ddp_mount_type = LDD_MT_LDISKFS;
2143 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2144 param->ddp_maxbytes = sb->s_maxbytes;
2146 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2147 /* inode are statically allocated, so per-inode space consumption
2148 * is the space consumed by the directory entry */
2149 param->ddp_inodespace = PER_OBJ_USAGE;
2150 /* EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2151 * = 128MB) which is unlikely to be hit in real life. Report a smaller
2152 * maximum length to not under count the actual number of extents
2153 * needed for writing a file. */
2154 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
2155 /* worst-case extent insertion metadata overhead */
2156 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2157 param->ddp_mntopts = 0;
2158 if (test_opt(sb, XATTR_USER))
2159 param->ddp_mntopts |= MNTOPT_USERXATTR;
2160 if (test_opt(sb, POSIX_ACL))
2161 param->ddp_mntopts |= MNTOPT_ACL;
2163 /* LOD might calculate the max stripe count based on max_ea_size,
2164 * so we need take account in the overhead as well,
2165 * xattr_header + magic + xattr_entry_head */
2166 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2167 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2169 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2170 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
2171 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2175 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2177 /* Preferred RPC size for efficient disk IO. 4MB shows good
2178 * all-around performance for ldiskfs, but use bigalloc chunk size
2179 * by default if larger. */
2180 #if defined(LDISKFS_CLUSTER_SIZE)
2181 if (LDISKFS_CLUSTER_SIZE(sb) > DT_DEF_BRW_SIZE)
2182 param->ddp_brw_size = LDISKFS_CLUSTER_SIZE(sb);
2185 param->ddp_brw_size = DT_DEF_BRW_SIZE;
2189 * Concurrency: shouldn't matter.
2191 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2195 CDEBUG(D_CACHE, "%s: syncing OSD\n", osd_dt_dev(d)->od_svname);
2197 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
2199 CDEBUG(D_CACHE, "%s: synced OSD: rc = %d\n", osd_dt_dev(d)->od_svname,
2206 * Start commit for OSD device.
2208 * An implementation of dt_commit_async method for OSD device.
2209 * Asychronously starts underlayng fs sync and thereby a transaction
2212 * \param env environment
2213 * \param d dt device
2215 * \see dt_device_operations
2217 static int osd_commit_async(const struct lu_env *env,
2218 struct dt_device *d)
2220 struct super_block *s = osd_sb(osd_dt_dev(d));
2223 CDEBUG(D_HA, "%s: async commit OSD\n", osd_dt_dev(d)->od_svname);
2224 RETURN(s->s_op->sync_fs(s, 0));
2227 /* Our own copy of the set readonly functions if present, or NU if not. */
2228 static int (*priv_dev_set_rdonly)(struct block_device *bdev);
2229 static int (*priv_dev_check_rdonly)(struct block_device *bdev);
2230 /* static int (*priv_dev_clear_rdonly)(struct block_device *bdev); */
2233 * Concurrency: shouldn't matter.
2235 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2237 struct super_block *sb = osd_sb(osd_dt_dev(d));
2238 struct block_device *dev = sb->s_bdev;
2239 int rc = -EOPNOTSUPP;
2242 if (priv_dev_set_rdonly) {
2243 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
2246 CERROR("*** setting %s read-only ***\n",
2247 osd_dt_dev(d)->od_svname);
2249 if (sb->s_op->freeze_fs) {
2250 rc = sb->s_op->freeze_fs(sb);
2255 if (jdev && (jdev != dev)) {
2256 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
2258 priv_dev_set_rdonly(jdev);
2260 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
2261 priv_dev_set_rdonly(dev);
2263 if (sb->s_op->unfreeze_fs)
2264 sb->s_op->unfreeze_fs(sb);
2269 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2270 osd_dt_dev(d)->od_svname, (long)dev, rc);
2276 * Note: we do not count into QUOTA here.
2277 * If we mount with --data_journal we may need more.
2279 const int osd_dto_credits_noquota[DTO_NR] = {
2282 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2283 * SINGLEDATA_TRANS_BLOCKS(8)
2284 * XXX Note: maybe iam need more, since iam have more level than
2287 [DTO_INDEX_INSERT] = 16,
2290 * just modify a single entry, probably merge few within a block
2292 [DTO_INDEX_DELETE] = 1,
2296 [DTO_INDEX_UPDATE] = 16,
2298 * 4(inode, inode bits, groups, GDT)
2299 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2301 [DTO_OBJECT_CREATE] = 4,
2303 * 4(inode, inode bits, groups, GDT)
2304 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2306 [DTO_OBJECT_DELETE] = 4,
2308 * Attr set credits (inode)
2310 [DTO_ATTR_SET_BASE] = 1,
2312 * Xattr set. The same as xattr of EXT3.
2313 * DATA_TRANS_BLOCKS(14)
2314 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2315 * are also counted in. Do not know why?
2317 [DTO_XATTR_SET] = 14,
2319 * credits for inode change during write.
2321 [DTO_WRITE_BASE] = 3,
2323 * credits for single block write.
2325 [DTO_WRITE_BLOCK] = 14,
2327 * Attr set credits for chown.
2328 * This is extra credits for setattr, and it is null without quota
2330 [DTO_ATTR_SET_CHOWN] = 0
2333 static const struct dt_device_operations osd_dt_ops = {
2334 .dt_root_get = osd_root_get,
2335 .dt_statfs = osd_statfs,
2336 .dt_trans_create = osd_trans_create,
2337 .dt_trans_start = osd_trans_start,
2338 .dt_trans_stop = osd_trans_stop,
2339 .dt_trans_cb_add = osd_trans_cb_add,
2340 .dt_conf_get = osd_conf_get,
2341 .dt_sync = osd_sync,
2343 .dt_commit_async = osd_commit_async,
2346 static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
2349 struct osd_object *obj = osd_dt_obj(dt);
2350 struct osd_thread_info *oti = osd_oti_get(env);
2352 LINVRNT(osd_invariant(obj));
2354 LASSERT(obj->oo_owner != env);
2355 down_read_nested(&obj->oo_sem, role);
2357 LASSERT(obj->oo_owner == NULL);
2361 static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
2364 struct osd_object *obj = osd_dt_obj(dt);
2365 struct osd_thread_info *oti = osd_oti_get(env);
2367 LINVRNT(osd_invariant(obj));
2369 LASSERT(obj->oo_owner != env);
2370 down_write_nested(&obj->oo_sem, role);
2372 LASSERT(obj->oo_owner == NULL);
2373 obj->oo_owner = env;
2377 static void osd_read_unlock(const struct lu_env *env, struct dt_object *dt)
2379 struct osd_object *obj = osd_dt_obj(dt);
2380 struct osd_thread_info *oti = osd_oti_get(env);
2382 LINVRNT(osd_invariant(obj));
2384 LASSERT(oti->oti_r_locks > 0);
2386 up_read(&obj->oo_sem);
2389 static void osd_write_unlock(const struct lu_env *env, struct dt_object *dt)
2391 struct osd_object *obj = osd_dt_obj(dt);
2392 struct osd_thread_info *oti = osd_oti_get(env);
2394 LINVRNT(osd_invariant(obj));
2396 LASSERT(obj->oo_owner == env);
2397 LASSERT(oti->oti_w_locks > 0);
2399 obj->oo_owner = NULL;
2400 up_write(&obj->oo_sem);
2403 static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
2405 struct osd_object *obj = osd_dt_obj(dt);
2407 LINVRNT(osd_invariant(obj));
2409 return obj->oo_owner == env;
2412 static struct timespec *osd_inode_time(const struct lu_env *env,
2413 struct inode *inode, __u64 seconds)
2415 struct osd_thread_info *oti = osd_oti_get(env);
2416 struct timespec *t = &oti->oti_time;
2418 t->tv_sec = seconds;
2420 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
2424 static void osd_inode_getattr(const struct lu_env *env,
2425 struct inode *inode, struct lu_attr *attr)
2427 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2428 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2429 LA_PROJID | LA_FLAGS | LA_NLINK | LA_RDEV |
2430 LA_BLKSIZE | LA_TYPE;
2432 attr->la_atime = LTIME_S(inode->i_atime);
2433 attr->la_mtime = LTIME_S(inode->i_mtime);
2434 attr->la_ctime = LTIME_S(inode->i_ctime);
2435 attr->la_mode = inode->i_mode;
2436 attr->la_size = i_size_read(inode);
2437 attr->la_blocks = inode->i_blocks;
2438 attr->la_uid = i_uid_read(inode);
2439 attr->la_gid = i_gid_read(inode);
2440 attr->la_projid = i_projid_read(inode);
2441 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2442 attr->la_nlink = inode->i_nlink;
2443 attr->la_rdev = inode->i_rdev;
2444 attr->la_blksize = 1 << inode->i_blkbits;
2445 attr->la_blkbits = inode->i_blkbits;
2447 * Ext4 did not transfer inherit flags from raw inode
2448 * to inode flags, and ext4 internally test raw inode
2449 * @i_flags directly. Instead of patching ext4, we do it here.
2451 if (LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL)
2452 attr->la_flags |= LUSTRE_PROJINHERIT_FL;
2455 static int osd_attr_get(const struct lu_env *env,
2456 struct dt_object *dt,
2457 struct lu_attr *attr)
2459 struct osd_object *obj = osd_dt_obj(dt);
2461 if (unlikely(!dt_object_exists(dt)))
2463 if (unlikely(obj->oo_destroyed))
2466 LASSERT(!dt_object_remote(dt));
2467 LINVRNT(osd_invariant(obj));
2469 spin_lock(&obj->oo_guard);
2470 osd_inode_getattr(env, obj->oo_inode, attr);
2471 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
2472 attr->la_flags |= LUSTRE_ORPHAN_FL;
2473 spin_unlock(&obj->oo_guard);
2478 static int osd_declare_attr_qid(const struct lu_env *env,
2479 struct osd_object *obj,
2480 struct osd_thandle *oh, long long bspace,
2481 qid_t old_id, qid_t new_id, bool enforce,
2482 unsigned type, bool ignore_edquot)
2485 struct osd_thread_info *info = osd_oti_get(env);
2486 struct lquota_id_info *qi = &info->oti_qi;
2488 qi->lqi_type = type;
2489 /* inode accounting */
2490 qi->lqi_is_blk = false;
2492 /* one more inode for the new id ... */
2493 qi->lqi_id.qid_uid = new_id;
2495 /* Reserve credits for the new id */
2496 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2497 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2502 /* and one less inode for the current id */
2503 qi->lqi_id.qid_uid = old_id;
2505 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2506 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2511 /* block accounting */
2512 qi->lqi_is_blk = true;
2514 /* more blocks for the new id ... */
2515 qi->lqi_id.qid_uid = new_id;
2516 qi->lqi_space = bspace;
2518 * Credits for the new uid has been reserved, re-use "obj"
2519 * to save credit reservation.
2521 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2522 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2527 /* and finally less blocks for the current uid */
2528 qi->lqi_id.qid_uid = old_id;
2529 qi->lqi_space = -bspace;
2530 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2531 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2537 static int osd_declare_attr_set(const struct lu_env *env,
2538 struct dt_object *dt,
2539 const struct lu_attr *attr,
2540 struct thandle *handle)
2542 struct osd_thandle *oh;
2543 struct osd_object *obj;
2551 LASSERT(dt != NULL);
2552 LASSERT(handle != NULL);
2554 obj = osd_dt_obj(dt);
2555 LASSERT(osd_invariant(obj));
2557 oh = container_of0(handle, struct osd_thandle, ot_super);
2558 LASSERT(oh->ot_handle == NULL);
2560 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2561 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2563 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2564 osd_dto_credits_noquota[DTO_XATTR_SET]);
2566 if (attr == NULL || obj->oo_inode == NULL)
2569 bspace = obj->oo_inode->i_blocks << 9;
2570 bspace = toqb(bspace);
2572 /* Changing ownership is always preformed by super user, it should not
2573 * fail with EDQUOT unless required explicitly.
2575 * We still need to call the osd_declare_qid() to calculate the journal
2576 * credits for updating quota accounting files and to trigger quota
2577 * space adjustment once the operation is completed.*/
2578 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2579 bool ignore_edquot = !(attr->la_flags & LUSTRE_SET_SYNC_FL);
2582 CDEBUG(D_QUOTA, "%s: enforce quota on UID %u, GID %u"
2583 "(the quota space is %lld)\n",
2584 obj->oo_inode->i_sb->s_id, attr->la_uid,
2585 attr->la_gid, bspace);
2588 uid = i_uid_read(obj->oo_inode);
2589 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2590 rc = osd_declare_attr_qid(env, obj, oh, bspace, uid,
2591 attr->la_uid, enforce, USRQUOTA,
2596 gid = i_gid_read(obj->oo_inode);
2597 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2598 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2599 i_gid_read(obj->oo_inode),
2600 attr->la_gid, enforce, GRPQUOTA,
2606 #ifdef HAVE_PROJECT_QUOTA
2607 if (attr->la_valid & LA_PROJID) {
2608 __u32 projid = i_projid_read(obj->oo_inode);
2609 enforce = (attr->la_valid & LA_PROJID) &&
2610 (attr->la_projid != projid);
2611 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2612 (qid_t)projid, (qid_t)attr->la_projid,
2613 enforce, PRJQUOTA, true);
2621 static int osd_inode_setattr(const struct lu_env *env,
2622 struct inode *inode, const struct lu_attr *attr)
2624 __u64 bits = attr->la_valid;
2626 /* Only allow set size for regular file */
2627 if (!S_ISREG(inode->i_mode))
2628 bits &= ~(LA_SIZE | LA_BLOCKS);
2633 if (bits & LA_ATIME)
2634 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2635 if (bits & LA_CTIME)
2636 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2637 if (bits & LA_MTIME)
2638 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2639 if (bits & LA_SIZE) {
2640 spin_lock(&inode->i_lock);
2641 LDISKFS_I(inode)->i_disksize = attr->la_size;
2642 i_size_write(inode, attr->la_size);
2643 spin_unlock(&inode->i_lock);
2646 /* OSD should not change "i_blocks" which is used by quota.
2647 * "i_blocks" should be changed by ldiskfs only. */
2649 inode->i_mode = (inode->i_mode & S_IFMT) |
2650 (attr->la_mode & ~S_IFMT);
2652 i_uid_write(inode, attr->la_uid);
2654 i_gid_write(inode, attr->la_gid);
2655 if (bits & LA_PROJID)
2656 i_projid_write(inode, attr->la_projid);
2657 if (bits & LA_NLINK)
2658 set_nlink(inode, attr->la_nlink);
2660 inode->i_rdev = attr->la_rdev;
2662 if (bits & LA_FLAGS) {
2663 /* always keep S_NOCMTIME */
2664 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2667 * Ext4 did not transfer inherit flags from
2668 * @inode->i_flags to raw inode i_flags when writing
2669 * flags, we do it explictly here.
2671 if (attr->la_flags & LUSTRE_PROJINHERIT_FL)
2672 LDISKFS_I(inode)->i_flags |= LUSTRE_PROJINHERIT_FL;
2674 LDISKFS_I(inode)->i_flags &= ~LUSTRE_PROJINHERIT_FL;
2679 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2683 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2684 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2687 ll_vfs_dq_init(inode);
2689 if (attr->la_valid & LA_UID)
2690 iattr.ia_valid |= ATTR_UID;
2691 if (attr->la_valid & LA_GID)
2692 iattr.ia_valid |= ATTR_GID;
2693 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2694 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2696 rc = ll_vfs_dq_transfer(inode, &iattr);
2698 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2699 "enforcement enabled on the ldiskfs "
2700 "filesystem?\n", inode->i_sb->s_id, rc);
2705 /* Handle project id transfer here properly */
2706 if (attr->la_valid & LA_PROJID &&
2707 attr->la_projid != i_projid_read(inode)) {
2708 #ifdef HAVE_PROJECT_QUOTA
2709 rc = ldiskfs_transfer_project(inode, attr->la_projid);
2714 CERROR("%s: quota transfer failed: rc = %d. Is project "
2715 "enforcement enabled on the ldiskfs "
2716 "filesystem?\n", inode->i_sb->s_id, rc);
2723 static int osd_attr_set(const struct lu_env *env,
2724 struct dt_object *dt,
2725 const struct lu_attr *attr,
2726 struct thandle *handle)
2728 struct osd_object *obj = osd_dt_obj(dt);
2729 struct inode *inode;
2732 if (!dt_object_exists(dt))
2735 LASSERT(handle != NULL);
2736 LASSERT(!dt_object_remote(dt));
2737 LASSERT(osd_invariant(obj));
2739 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2741 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) &&
2742 !osd_obj2dev(obj)->od_is_ost) {
2743 struct osd_thread_info *oti = osd_oti_get(env);
2744 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2745 struct lu_fid *fid1 = &oti->oti_fid;
2746 struct osd_inode_id *id = &oti->oti_id;
2747 struct iam_path_descr *ipd;
2748 struct iam_container *bag;
2749 struct osd_thandle *oh;
2752 fid_cpu_to_be(fid1, fid0);
2753 memset(id, 1, sizeof(*id));
2754 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2755 fid0)->oi_dir.od_container;
2756 ipd = osd_idx_ipd_get(env, bag);
2757 if (unlikely(ipd == NULL))
2760 oh = container_of0(handle, struct osd_thandle, ot_super);
2761 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2762 (const struct iam_rec *)id, ipd);
2763 osd_ipd_put(env, bag, ipd);
2764 return(rc > 0 ? 0 : rc);
2767 inode = obj->oo_inode;
2769 rc = osd_quota_transfer(inode, attr);
2773 spin_lock(&obj->oo_guard);
2774 rc = osd_inode_setattr(env, inode, attr);
2775 spin_unlock(&obj->oo_guard);
2779 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2781 if (!(attr->la_valid & LA_FLAGS))
2784 /* Let's check if there are extra flags need to be set into LMA */
2785 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2786 struct osd_thread_info *info = osd_oti_get(env);
2787 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
2789 LASSERT(!obj->oo_pfid_in_lma);
2791 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
2792 &info->oti_ost_attrs);
2796 lma->lma_incompat |=
2797 lustre_to_lma_flags(attr->la_flags);
2798 lustre_lma_swab(lma);
2799 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2800 lma, sizeof(*lma), XATTR_REPLACE);
2802 struct osd_device *osd = osd_obj2dev(obj);
2804 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2805 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2806 lma->lma_incompat, rc);
2809 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2811 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2814 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2819 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2820 struct osd_object *obj,
2821 const char *name, const int namelen)
2823 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2826 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2827 umode_t mode, struct dt_allocation_hint *hint,
2831 struct osd_device *osd = osd_obj2dev(obj);
2832 struct osd_thandle *oth;
2833 struct dt_object *parent = NULL;
2834 struct inode *inode;
2836 LINVRNT(osd_invariant(obj));
2837 LASSERT(obj->oo_inode == NULL);
2838 LASSERT(obj->oo_hl_head == NULL);
2840 if (S_ISDIR(mode) && ldiskfs_pdo) {
2841 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2842 if (obj->oo_hl_head == NULL)
2846 oth = container_of(th, struct osd_thandle, ot_super);
2847 LASSERT(oth->ot_handle->h_transaction != NULL);
2849 if (hint != NULL && hint->dah_parent != NULL &&
2850 !dt_object_remote(hint->dah_parent))
2851 parent = hint->dah_parent;
2853 inode = ldiskfs_create_inode(oth->ot_handle,
2854 parent ? osd_dt_obj(parent)->oo_inode :
2855 osd_sb(osd)->s_root->d_inode,
2857 if (!IS_ERR(inode)) {
2858 /* Do not update file c/mtime in ldiskfs. */
2859 inode->i_flags |= S_NOCMTIME;
2861 /* For new created object, it must be consistent,
2862 * and it is unnecessary to scrub against it. */
2863 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2865 obj->oo_inode = inode;
2868 if (obj->oo_hl_head != NULL) {
2869 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2870 obj->oo_hl_head = NULL;
2872 result = PTR_ERR(inode);
2874 LINVRNT(osd_invariant(obj));
2882 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2883 struct lu_attr *attr,
2884 struct dt_allocation_hint *hint,
2885 struct dt_object_format *dof,
2889 struct osd_thandle *oth;
2890 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
2892 LASSERT(S_ISDIR(attr->la_mode));
2894 oth = container_of(th, struct osd_thandle, ot_super);
2895 LASSERT(oth->ot_handle->h_transaction != NULL);
2896 result = osd_mkfile(info, obj, mode, hint, th);
2901 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2902 struct lu_attr *attr,
2903 struct dt_allocation_hint *hint,
2904 struct dt_object_format *dof,
2908 struct osd_thandle *oth;
2909 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2911 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2913 LASSERT(S_ISREG(attr->la_mode));
2915 oth = container_of(th, struct osd_thandle, ot_super);
2916 LASSERT(oth->ot_handle->h_transaction != NULL);
2918 result = osd_mkfile(info, obj, mode, hint, th);
2920 LASSERT(obj->oo_inode != NULL);
2921 if (feat->dif_flags & DT_IND_VARKEY)
2922 result = iam_lvar_create(obj->oo_inode,
2923 feat->dif_keysize_max,
2925 feat->dif_recsize_max,
2928 result = iam_lfix_create(obj->oo_inode,
2929 feat->dif_keysize_max,
2931 feat->dif_recsize_max,
2938 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2939 struct lu_attr *attr,
2940 struct dt_allocation_hint *hint,
2941 struct dt_object_format *dof,
2944 LASSERT(S_ISREG(attr->la_mode));
2945 return osd_mkfile(info, obj, (attr->la_mode &
2946 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2949 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2950 struct lu_attr *attr,
2951 struct dt_allocation_hint *hint,
2952 struct dt_object_format *dof,
2955 LASSERT(S_ISLNK(attr->la_mode));
2956 return osd_mkfile(info, obj, (attr->la_mode &
2957 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2960 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2961 struct lu_attr *attr,
2962 struct dt_allocation_hint *hint,
2963 struct dt_object_format *dof,
2966 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2969 LINVRNT(osd_invariant(obj));
2970 LASSERT(obj->oo_inode == NULL);
2971 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2972 S_ISFIFO(mode) || S_ISSOCK(mode));
2974 result = osd_mkfile(info, obj, mode, hint, th);
2976 LASSERT(obj->oo_inode != NULL);
2978 * This inode should be marked dirty for i_rdev. Currently
2979 * that is done in the osd_attr_init().
2981 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2984 LINVRNT(osd_invariant(obj));
2988 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2990 struct dt_allocation_hint *hint,
2991 struct dt_object_format *dof,
2994 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2996 osd_obj_type_f result;
3012 result = osd_mk_index;
3023 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
3024 struct dt_object *parent, struct dt_object *child,
3029 ah->dah_parent = parent;
3030 ah->dah_mode = child_mode;
3032 if (parent != NULL && !dt_object_remote(parent)) {
3033 /* will help to find FID->ino at dt_insert("..") */
3034 struct osd_object *pobj = osd_dt_obj(parent);
3035 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
3039 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
3040 struct lu_attr *attr, struct dt_object_format *dof)
3042 struct inode *inode = obj->oo_inode;
3043 __u64 valid = attr->la_valid;
3046 attr->la_valid &= ~(LA_TYPE | LA_MODE);
3048 if (dof->dof_type != DFT_NODE)
3049 attr->la_valid &= ~LA_RDEV;
3050 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
3051 attr->la_valid &= ~LA_ATIME;
3052 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
3053 attr->la_valid &= ~LA_CTIME;
3054 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
3055 attr->la_valid &= ~LA_MTIME;
3057 result = osd_quota_transfer(inode, attr);
3061 if (attr->la_valid != 0) {
3062 result = osd_inode_setattr(info->oti_env, inode, attr);
3064 * The osd_inode_setattr() should always succeed here. The
3065 * only error that could be returned is EDQUOT when we are
3066 * trying to change the UID or GID of the inode. However, this
3067 * should not happen since quota enforcement is no longer
3068 * enabled on ldiskfs (lquota takes care of it).
3070 LASSERTF(result == 0, "%d\n", result);
3071 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3074 attr->la_valid = valid;
3078 * Helper function for osd_create()
3080 * \retval 0, on success
3082 static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
3083 struct lu_attr *attr, struct dt_allocation_hint *hint,
3084 struct dt_object_format *dof, struct thandle *th)
3089 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
3091 /* we drop umask so that permissions we pass are not affected */
3092 umask = current->fs->umask;
3093 current->fs->umask = 0;
3095 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
3097 if (likely(obj->oo_inode != NULL)) {
3098 LASSERT(obj->oo_inode->i_state & I_NEW);
3100 /* Unlock the inode before attr initialization to avoid
3101 * unnecessary dqget operations. LU-6378 */
3102 unlock_new_inode(obj->oo_inode);
3105 if (likely(result == 0)) {
3106 osd_attr_init(info, obj, attr, dof);
3107 osd_object_init0(obj);
3110 /* restore previous umask value */
3111 current->fs->umask = umask;
3113 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
3119 * Helper function for osd_create()
3121 * \retval 0, on success
3123 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
3124 const struct lu_fid *fid, struct thandle *th)
3126 struct osd_thread_info *info = osd_oti_get(env);
3127 struct osd_inode_id *id = &info->oti_id;
3128 struct osd_device *osd = osd_obj2dev(obj);
3129 struct osd_thandle *oh;
3132 LASSERT(obj->oo_inode != NULL);
3134 oh = container_of0(th, struct osd_thandle, ot_super);
3135 LASSERT(oh->ot_handle);
3136 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3138 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3139 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3140 OI_CHECK_FLD, NULL);
3141 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3146 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3147 u64 seq, struct lu_seq_range *range)
3149 struct seq_server_site *ss = osd_seq_site(osd);
3151 if (fid_seq_is_idif(seq)) {
3152 fld_range_set_ost(range);
3153 range->lsr_index = idif_ost_idx(seq);
3157 if (!fid_seq_in_fldb(seq)) {
3158 fld_range_set_mdt(range);
3160 /* FIXME: If ss is NULL, it suppose not get lsr_index
3162 range->lsr_index = ss->ss_node_id;
3166 LASSERT(ss != NULL);
3167 fld_range_set_any(range);
3168 /* OSD will only do local fld lookup */
3169 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3172 static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
3173 struct lu_attr *attr,
3174 struct dt_allocation_hint *hint,
3175 struct dt_object_format *dof,
3176 struct thandle *handle)
3178 struct osd_thandle *oh;
3182 LASSERT(handle != NULL);
3184 oh = container_of0(handle, struct osd_thandle, ot_super);
3185 LASSERT(oh->ot_handle == NULL);
3187 /* EA object consumes more credits than regular object: osd_mk_index
3188 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3189 * leaf_node, could involves the block, block bitmap, groups, GDT
3190 * change for each block, so add 4 * 2 credits in that case. */
3191 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3192 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3193 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3194 /* Reuse idle OI block may cause additional one OI block
3196 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3197 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3202 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid,
3203 attr->la_projid, 1, oh, osd_dt_obj(dt),
3204 NULL, OSD_QID_INODE);
3208 /* will help to find FID->ino mapping at dt_insert() */
3209 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3216 * Called to destroy on-disk representation of the object
3218 * Concurrency: must be locked
3220 static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
3223 struct osd_object *obj = osd_dt_obj(dt);
3224 struct inode *inode = obj->oo_inode;
3225 struct osd_thandle *oh;
3232 oh = container_of0(th, struct osd_thandle, ot_super);
3233 LASSERT(oh->ot_handle == NULL);
3235 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3236 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3238 /* For removing agent entry */
3239 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu))
3240 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3242 /* Recycle idle OI leaf may cause additional three OI blocks
3244 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3245 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3246 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3247 /* one less inode */
3248 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3249 i_projid_read(inode), -1, oh, obj, NULL,
3253 /* data to be truncated */
3254 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3255 i_projid_read(inode), 0, oh, obj, NULL,
3260 /* will help to find FID->ino when this object is being
3261 * added to PENDING/ */
3262 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3267 static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
3270 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3271 struct osd_object *obj = osd_dt_obj(dt);
3272 struct inode *inode = obj->oo_inode;
3273 struct osd_device *osd = osd_obj2dev(obj);
3274 struct osd_thandle *oh;
3278 oh = container_of0(th, struct osd_thandle, ot_super);
3279 LASSERT(oh->ot_handle);
3281 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3283 if (unlikely(fid_is_acct(fid)))
3286 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu)) {
3287 result = osd_delete_from_remote_parent(env, osd, obj, oh, true);
3289 CERROR("%s: remove agent entry "DFID": rc = %d\n",
3290 osd_name(osd), PFID(fid), result);
3293 if (S_ISDIR(inode->i_mode)) {
3294 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
3295 inode->i_nlink == 2);
3297 spin_lock(&obj->oo_guard);
3299 spin_unlock(&obj->oo_guard);
3300 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3303 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3305 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3307 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3308 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3309 oh->ot_handle, OI_CHECK_FLD);
3311 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3312 /* XXX: add to ext3 orphan list */
3313 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3315 /* not needed in the cache anymore */
3316 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3317 obj->oo_destroyed = 1;
3323 * Put the fid into lustre_mdt_attrs, and then place the structure
3324 * inode's ea. This fid should not be altered during the life time
3327 * \retval +ve, on success
3328 * \retval -ve, on error
3330 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3332 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3333 const struct lu_fid *fid, __u32 compat, __u32 incompat)
3335 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3336 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3340 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3343 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3346 lustre_loa_init(loa, fid, compat, incompat);
3347 lustre_loa_swab(loa, false);
3349 /* For the OST device with 256 bytes inode size by default,
3350 * the PFID EA will be stored together with LMA EA to avoid
3351 * performance trouble. Otherwise the PFID EA can be stored
3352 * independently. LU-8998 */
3353 if ((compat & LMAC_FID_ON_OST) &&
3354 LDISKFS_INODE_SIZE(inode->i_sb) <= 256)
3355 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3356 sizeof(*loa), XATTR_CREATE);
3358 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3359 sizeof(*lma), XATTR_CREATE);
3360 /* LMA may already exist, but we need to check that all the
3361 * desired compat/incompat flags have been added. */
3362 if (unlikely(rc == -EEXIST)) {
3363 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3364 XATTR_NAME_LMA, (void *)loa, sizeof(*loa));
3368 if (rc < sizeof(*lma))
3371 lustre_loa_swab(loa, true);
3372 if (lu_fid_eq(fid, &lma->lma_self_fid) &&
3373 ((compat == 0 && incompat == 0) ||
3374 (!(~lma->lma_compat & compat) &&
3375 !(~lma->lma_incompat & incompat))))
3378 lma->lma_self_fid = *fid;
3379 lma->lma_compat |= compat;
3380 lma->lma_incompat |= incompat;
3381 if (rc == sizeof(*lma)) {
3382 lustre_lma_swab(lma);
3383 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3384 sizeof(*lma), XATTR_REPLACE);
3386 lustre_loa_swab(loa, false);
3387 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3388 sizeof(*loa), XATTR_REPLACE);
3396 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3397 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3398 * To have compatilibility with 1.8 ldiskfs driver we need to have
3399 * magic number at start of fid data.
3400 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3403 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3404 const struct lu_fid *fid)
3406 if (!fid_is_namespace_visible(fid) ||
3407 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3408 param->edp_magic = 0;
3412 param->edp_magic = LDISKFS_LUFID_MAGIC;
3413 param->edp_len = sizeof(struct lu_fid) + 1;
3414 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3418 * Try to read the fid from inode ea into dt_rec.
3420 * \param fid object fid.
3422 * \retval 0 on success
3424 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3425 __u32 ino, struct lu_fid *fid,
3426 struct osd_inode_id *id)
3428 struct osd_thread_info *info = osd_oti_get(env);
3429 struct inode *inode;
3432 osd_id_gen(id, ino, OSD_OII_NOGEN);
3433 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3435 RETURN(PTR_ERR(inode));
3441 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3443 struct inode *parent_dir,
3444 const struct lu_fid *dot_fid,
3445 const struct lu_fid *dot_dot_fid,
3446 struct osd_thandle *oth)
3448 struct ldiskfs_dentry_param *dot_ldp;
3449 struct ldiskfs_dentry_param *dot_dot_ldp;
3450 __u32 saved_nlink = dir->i_nlink;
3453 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3454 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3456 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3457 dot_ldp->edp_magic = 0;
3459 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3460 dir, dot_ldp, dot_dot_ldp);
3461 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3462 * the subseqent ref_add() will increase the dir->i_nlink
3463 * as 3. That is incorrect for new created directory.
3465 * It looks like hack, because we want to make the OSD API
3466 * to be order-independent for new created directory object
3467 * between dt_insert(..) and ref_add() operations.
3469 * Here, we only restore the in-RAM dir-inode's nlink attr,
3470 * becuase if the nlink attr is not 2, then there will be
3471 * ref_add() called following the dt_insert(..), such call
3472 * will make both the in-RAM and on-disk dir-inode's nlink
3473 * attr to be set as 2. LU-7447 */
3474 set_nlink(dir, saved_nlink);
3479 * Create an local agent inode for remote entry
3481 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3482 struct osd_device *osd,
3483 struct osd_object *pobj,
3484 const struct lu_fid *fid,
3488 struct osd_thread_info *info = osd_oti_get(env);
3489 struct inode *local;
3490 struct osd_thandle *oh;
3495 oh = container_of(th, struct osd_thandle, ot_super);
3496 LASSERT(oh->ot_handle->h_transaction != NULL);
3498 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
3499 if (IS_ERR(local)) {
3500 CERROR("%s: create local error %d\n", osd_name(osd),
3501 (int)PTR_ERR(local));
3505 /* restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
3506 * correct gid on remote file, not agent here */
3507 local->i_gid = current_fsgid();
3508 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3509 unlock_new_inode(local);
3511 /* Agent inode should not have project ID */
3512 #ifdef HAVE_PROJECT_QUOTA
3513 if (LDISKFS_I(pobj->oo_inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
3514 i_projid_read(pobj->oo_inode) != 0) {
3515 rc = ldiskfs_transfer_project(local, 0);
3517 CERROR("%s: quota transfer failed: rc = %d. Is project "
3518 "quota enforcement enabled on the ldiskfs "
3519 "filesystem?\n", local->i_sb->s_id, rc);
3520 RETURN(ERR_PTR(rc));
3524 /* Set special LMA flag for local agent inode */
3525 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3527 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3528 osd_name(osd), PFID(fid), rc);
3529 RETURN(ERR_PTR(rc));
3535 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3536 lu_object_fid(&pobj->oo_dt.do_lu),
3539 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3540 osd_name(osd), PFID(fid), rc);
3541 RETURN(ERR_PTR(rc));
3548 * when direntry is deleted, we have to take care of possible agent inode
3549 * referenced by that. unfortunately we can't do this at that point:
3550 * iget() within a running transaction leads to deadlock and we better do
3551 * not call that every delete declaration to save performance. so we put
3552 * a potention agent inode on a list and process that once the transaction
3553 * is over. Notice it's not any worse in terms of real orphans as regular
3554 * object destroy doesn't put inodes on the on-disk orphan list. this should
3555 * be addressed separately
3557 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3558 struct osd_thandle *oh,
3561 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3562 struct osd_obj_orphan *oor;
3569 oor->oor_env = (struct lu_env *)env;
3570 spin_lock(&osd->od_osfs_lock);
3571 list_add(&oor->oor_list, &osd->od_orphan_list);
3572 spin_unlock(&osd->od_osfs_lock);
3574 oh->ot_remove_agents = 1;
3580 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3581 struct osd_device *osd)
3583 struct osd_thread_info *info = osd_oti_get(env);
3584 struct osd_obj_orphan *oor, *tmp;
3585 struct osd_inode_id id;
3586 struct list_head list;
3587 struct inode *inode;
3592 INIT_LIST_HEAD(&list);
3594 spin_lock(&osd->od_osfs_lock);
3595 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3596 if (oor->oor_env == env) {
3597 list_del(&oor->oor_list);
3598 list_add(&oor->oor_list, &list);
3601 spin_unlock(&osd->od_osfs_lock);
3603 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3607 list_del(&oor->oor_list);
3610 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3611 inode = osd_iget_fid(info, osd, &id, &fid);
3615 if (!osd_remote_fid(env, osd, &fid)) {
3620 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3622 mark_inode_dirty(inode);
3623 ldiskfs_journal_stop(jh);
3631 * OSD layer object create function for OST objects (b=11826).
3633 * The FID is inserted into inode xattr here.
3635 * \retval 0, on success
3636 * \retval -ve, on error
3638 static int osd_create(const struct lu_env *env, struct dt_object *dt,
3639 struct lu_attr *attr, struct dt_allocation_hint *hint,
3640 struct dt_object_format *dof, struct thandle *th)
3642 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3643 struct osd_object *obj = osd_dt_obj(dt);
3644 struct osd_thread_info *info = osd_oti_get(env);
3645 int result, on_ost = 0;
3649 if (dt_object_exists(dt))
3652 LINVRNT(osd_invariant(obj));
3653 LASSERT(!dt_object_remote(dt));
3654 LASSERT(osd_is_write_locked(env, obj));
3655 LASSERT(th != NULL);
3657 if (unlikely(fid_is_acct(fid)))
3658 /* Quota files can't be created from the kernel any more,
3659 * 'tune2fs -O quota' will take care of creating them */
3662 result = __osd_create(info, obj, attr, hint, dof, th);
3664 if (fid_is_idif(fid) &&
3665 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3666 struct lu_fid *tfid = &info->oti_fid;
3667 struct ost_id *oi = &info->oti_ostid;
3669 fid_to_ostid(fid, oi);
3670 ostid_to_fid(tfid, oi, 0);
3672 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3673 LMAC_FID_ON_OST, 0);
3675 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3677 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3678 on_ost ? LMAC_FID_ON_OST : 0,
3681 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3682 obj->oo_dt.do_body_ops = &osd_body_ops;
3685 if (!result && !CFS_FAIL_CHECK(OBD_FAIL_OSD_NO_OI_ENTRY))
3686 result = __osd_oi_insert(env, obj, fid, th);
3688 /* a small optimization - dt_insert() isn't usually applied
3689 * to OST objects, so we don't need to cache OI mapping for
3691 if (result == 0 && on_ost == 0) {
3692 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3693 result = osd_idc_find_and_init(env, osd, obj);
3694 LASSERT(result == 0);
3697 LASSERT(ergo(result == 0,
3698 dt_object_exists(dt) && !dt_object_remote(dt)));
3699 LINVRNT(osd_invariant(obj));
3703 static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
3704 struct thandle *handle)
3706 struct osd_thandle *oh;
3708 /* it's possible that object doesn't exist yet */
3709 LASSERT(handle != NULL);
3711 oh = container_of0(handle, struct osd_thandle, ot_super);
3712 LASSERT(oh->ot_handle == NULL);
3714 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3715 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3717 osd_idc_find_and_init(env, osd_dev(dt->do_lu.lo_dev), osd_dt_obj(dt));
3723 * Concurrency: @dt is write locked.
3725 static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
3728 struct osd_object *obj = osd_dt_obj(dt);
3729 struct inode *inode = obj->oo_inode;
3730 struct osd_thandle *oh;
3733 if (!dt_object_exists(dt) || obj->oo_destroyed)
3736 LINVRNT(osd_invariant(obj));
3737 LASSERT(!dt_object_remote(dt));
3738 LASSERT(osd_is_write_locked(env, obj));
3739 LASSERT(th != NULL);
3741 oh = container_of0(th, struct osd_thandle, ot_super);
3742 LASSERT(oh->ot_handle != NULL);
3744 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3746 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3747 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3749 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3750 * (65000) subdirectories by storing "1" in i_nlink if the link count
3751 * would otherwise overflow. Directory tranversal tools understand
3752 * that (st_nlink == 1) indicates that the filesystem dose not track
3753 * hard links count on the directory, and will not abort subdirectory
3754 * scanning early once (st_nlink - 2) subdirs have been found.
3756 * This also has to properly handle the case of inodes with nlink == 0
3757 * in case they are being linked into the PENDING directory
3759 spin_lock(&obj->oo_guard);
3760 if (unlikely(inode->i_nlink == 0))
3761 /* inc_nlink from 0 may cause WARN_ON */
3762 set_nlink(inode, 1);
3764 ldiskfs_inc_count(oh->ot_handle, inode);
3765 if (!S_ISDIR(inode->i_mode))
3766 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3768 spin_unlock(&obj->oo_guard);
3770 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3771 LINVRNT(osd_invariant(obj));
3773 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3778 static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
3779 struct thandle *handle)
3781 struct osd_thandle *oh;
3783 if (!dt_object_exists(dt))
3786 LASSERT(!dt_object_remote(dt));
3787 LASSERT(handle != NULL);
3789 oh = container_of0(handle, struct osd_thandle, ot_super);
3790 LASSERT(oh->ot_handle == NULL);
3792 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3793 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3799 * Concurrency: @dt is write locked.
3801 static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
3804 struct osd_object *obj = osd_dt_obj(dt);
3805 struct inode *inode = obj->oo_inode;
3806 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3807 struct osd_thandle *oh;
3809 if (!dt_object_exists(dt))
3812 LINVRNT(osd_invariant(obj));
3813 LASSERT(!dt_object_remote(dt));
3814 LASSERT(osd_is_write_locked(env, obj));
3815 LASSERT(th != NULL);
3817 oh = container_of0(th, struct osd_thandle, ot_super);
3818 LASSERT(oh->ot_handle != NULL);
3820 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3822 spin_lock(&obj->oo_guard);
3823 /* That can be result of upgrade from old Lustre version and
3824 * applied only to local files. Just skip this ref_del call.
3825 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3826 if (inode->i_nlink == 0) {
3827 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3828 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3829 ", maybe an upgraded file? (LU-3915)\n",
3830 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3831 spin_unlock(&obj->oo_guard);
3835 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3836 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3838 ldiskfs_dec_count(oh->ot_handle, inode);
3839 spin_unlock(&obj->oo_guard);
3841 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3842 LINVRNT(osd_invariant(obj));
3844 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3850 * Concurrency: @dt is read locked.
3852 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3853 struct lu_buf *buf, const char *name)
3855 struct osd_object *obj = osd_dt_obj(dt);
3856 struct inode *inode = obj->oo_inode;
3857 struct osd_thread_info *info = osd_oti_get(env);
3858 struct dentry *dentry = &info->oti_obj_dentry;
3859 bool cache_xattr = false;
3864 /* version get is not real XATTR but uses xattr API */
3865 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3866 dt_obj_version_t *ver = buf->lb_buf;
3868 /* for version we are just using xattr API but change inode
3870 if (buf->lb_len == 0)
3871 return sizeof(dt_obj_version_t);
3873 if (buf->lb_len < sizeof(dt_obj_version_t))
3876 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
3877 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3879 *ver = LDISKFS_I(inode)->i_fs_version;
3881 return sizeof(dt_obj_version_t);
3884 if (!dt_object_exists(dt))
3887 LASSERT(!dt_object_remote(dt));
3888 LASSERT(inode->i_op != NULL);
3889 LASSERT(inode->i_op->getxattr != NULL);
3891 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
3892 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
3896 rc = osd_oxc_get(obj, name, buf);
3901 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
3902 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3903 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3904 struct filter_fid *ff;
3905 struct ost_layout *ol;
3907 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
3911 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
3914 if (buf->lb_len == 0 || !buf->lb_buf)
3917 if (buf->lb_len < rc)
3921 ol = &ff->ff_layout;
3922 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
3923 PFID_STRIPE_IDX_BITS);
3924 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
3925 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
3926 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
3927 if (lma->lma_compat & LMAC_COMP_INFO) {
3928 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
3929 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
3930 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
3932 ol->ol_comp_start = 0;
3933 ol->ol_comp_end = 0;
3937 rc = __osd_xattr_get(inode, dentry, name,
3938 buf->lb_buf, buf->lb_len);
3942 if (rc == -ENOENT || rc == -ENODATA)
3943 osd_oxc_add(obj, name, NULL, 0);
3944 else if (rc > 0 && buf->lb_buf != NULL)
3945 osd_oxc_add(obj, name, buf->lb_buf, rc);
3951 static int osd_declare_xattr_set(const struct lu_env *env,
3952 struct dt_object *dt,
3953 const struct lu_buf *buf, const char *name,
3954 int fl, struct thandle *handle)
3956 struct osd_thandle *oh;
3958 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3960 LASSERT(handle != NULL);
3962 oh = container_of0(handle, struct osd_thandle, ot_super);
3963 LASSERT(oh->ot_handle == NULL);
3965 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3966 /* For non-upgrading case, the LMA is set first and
3967 * usually fit inode. But for upgrade case, the LMA
3968 * may be in another separated EA block. */
3969 if (dt_object_exists(dt)) {
3970 if (fl == LU_XATTR_REPLACE)
3975 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3977 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3978 /* We may need to delete the old PFID EA. */
3979 credits = LDISKFS_MAXQUOTAS_DEL_BLOCKS(sb);
3980 if (fl == LU_XATTR_REPLACE)
3985 /* If some name entry resides on remote MDT, then will create
3986 * agent entry under remote parent. On the other hand, if the
3987 * remote entry will be removed, then related agent entry may
3988 * need to be removed from the remote parent. So there may be
3989 * kinds of cases, let's declare enough credits. The credits
3990 * for create agent entry is enough for remove case. */
3991 if (strcmp(name, XATTR_NAME_LINK) == 0) {
3992 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3993 if (dt_object_exists(dt))
3994 credits += 1; /* For updating LMA */
3998 credits += osd_dto_credits_noquota[DTO_XATTR_SET];
4003 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
4004 /* learn xattr size from osd_xattr_get if
4005 attribute has not been read yet */
4006 buflen = __osd_xattr_get(
4007 osd_dt_obj(dt)->oo_inode,
4008 &osd_oti_get(env)->oti_obj_dentry,
4013 buflen = buf->lb_len;
4016 if (buflen > sb->s_blocksize) {
4017 credits += osd_calc_bkmap_credits(
4019 (buflen + sb->s_blocksize - 1) >>
4020 sb->s_blocksize_bits);
4024 * xattr set may involve inode quota change, reserve credits for
4025 * dquot_initialize()
4027 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4030 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
4035 static int osd_xattr_set_pfid(const struct lu_env *env, struct osd_object *obj,
4036 const struct lu_buf *buf, int fl,
4037 struct thandle *handle)
4039 struct osd_thread_info *info = osd_oti_get(env);
4040 struct dentry *dentry = &info->oti_obj_dentry;
4041 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4042 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4043 struct inode *inode = obj->oo_inode;
4044 struct filter_fid *ff = buf->lb_buf;
4045 struct ost_layout *ol = &ff->ff_layout;
4046 int flags = XATTR_REPLACE;
4050 if (buf->lb_len != sizeof(*ff) && buf->lb_len != sizeof(struct lu_fid))
4053 rc = osd_get_lma(info, inode, dentry, loa);
4054 if (rc == -ENODATA) {
4055 /* Usually for upgarding from old device */
4056 lustre_loa_init(loa, lu_object_fid(&obj->oo_dt.do_lu),
4057 LMAC_FID_ON_OST, 0);
4058 flags = XATTR_CREATE;
4063 if (!rc && lma->lma_compat & LMAC_STRIPE_INFO) {
4064 if ((fl & LU_XATTR_CREATE) && !(fl & LU_XATTR_REPLACE))
4067 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256) {
4068 /* Separate PFID EA from LMA */
4069 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4070 lustre_lma_swab(lma);
4071 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4072 sizeof(*lma), XATTR_REPLACE);
4074 obj->oo_pfid_in_lma = 0;
4075 rc = LU_XATTR_CREATE;
4081 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256)
4084 /* Old client does not send stripe information,
4085 * then store the PFID EA on disk separatedly. */
4086 if (unlikely(buf->lb_len == sizeof(struct lu_fid) ||
4087 ol->ol_stripe_size == 0))
4090 /* Remove old PFID EA entry firstly. */
4091 ll_vfs_dq_init(inode);
4092 rc = inode->i_op->removexattr(dentry, XATTR_NAME_FID);
4093 if (rc == -ENODATA) {
4094 if ((fl & LU_XATTR_REPLACE) && !(fl & LU_XATTR_CREATE))
4101 fid_le_to_cpu(&loa->loa_parent_fid, &ff->ff_parent);
4102 if (likely(ol->ol_stripe_size != 0)) {
4103 loa->loa_parent_fid.f_ver |= le32_to_cpu(ol->ol_stripe_count) <<
4104 PFID_STRIPE_IDX_BITS;
4105 loa->loa_stripe_size = le32_to_cpu(ol->ol_stripe_size);
4106 lma->lma_compat |= LMAC_STRIPE_INFO;
4107 if (ol->ol_comp_id != 0) {
4108 loa->loa_comp_id = le32_to_cpu(ol->ol_comp_id);
4109 loa->loa_comp_start = le64_to_cpu(ol->ol_comp_start);
4110 loa->loa_comp_end = le64_to_cpu(ol->ol_comp_end);
4111 lma->lma_compat |= LMAC_COMP_INFO;
4115 lustre_loa_swab(loa, false);
4117 /* Store the PFID EA inside LMA. */
4118 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa, sizeof(*loa),
4121 obj->oo_pfid_in_lma = 1;
4127 * In DNE environment, the object (in spite of regular file or directory)
4128 * and its name entry may reside on different MDTs. Under such case, we will
4129 * create an agent entry on the MDT where the object resides. The agent entry
4130 * references the object locally, that makes the object to be visible to the
4131 * userspace when mounted as 'ldiskfs' directly. Then the userspace tools,
4132 * such as 'tar' can handle the object properly.
4134 * We handle the agent entry during set linkEA that is the common interface
4135 * for both regular file and directroy, can handle kinds of cases, such as
4136 * create/link/unlink/rename, and so on.
4138 * NOTE: we can NOT do that when ea_{insert,delete} that is only for directory.
4140 * XXX: There are two known issues:
4141 * 1. For one object, we will create at most one agent entry even if there
4142 * may be more than one cross-MDTs hard links on the object. So the local
4143 * e2fsck may claim that the object's nlink is larger than the name entries
4144 * that reference such inode. And in further, the e2fsck will fix the nlink
4145 * attribute to match the local references. Then it will cause the object's
4146 * nlink attribute to be inconsistent with the global references. it is bad
4147 * but not fatal. The ref_del() can handle the zero-referenced case. On the
4148 * other hand, the global namespace LFSCK can repair the object's attribute
4149 * according to the linkEA.
4150 * 2. There may be too many hard links on the object as to its linkEA overflow,
4151 * then the linkEA entry for cross-MDTs reference may be discarded. If such
4152 * case happened, then at this point, we do not know whether there are some
4153 * cross-MDTs reference. But there are local references, it guarantees that
4154 * object is visible to userspace when mounted as 'ldiskfs'. That is enough.
4156 static int osd_xattr_handle_linkea(const struct lu_env *env,
4157 struct osd_device *osd,
4158 struct osd_object *obj,
4159 const struct lu_buf *buf,
4160 struct thandle *handle)
4162 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
4163 struct lu_fid *tfid = &osd_oti_get(env)->oti_fid3;
4164 struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
4165 struct lu_name tmpname;
4166 struct osd_thandle *oh;
4168 bool remote = false;
4171 oh = container_of0(handle, struct osd_thandle, ot_super);
4172 LASSERT(oh->ot_handle != NULL);
4174 rc = linkea_init_with_rec(&ldata);
4176 linkea_first_entry(&ldata);
4177 while (ldata.ld_lee != NULL && !remote) {
4178 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
4180 if (osd_remote_fid(env, osd, tfid) > 0)
4183 linkea_next_entry(&ldata);
4185 } else if (rc == -ENODATA) {
4191 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
4192 rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
4194 CERROR("%s: failed to remove agent entry for "DFID
4195 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4196 } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
4197 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4199 CERROR("%s: failed to create agent entry for "DFID
4200 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4207 * Concurrency: @dt is write locked.
4209 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
4210 const struct lu_buf *buf, const char *name, int fl,
4211 struct thandle *handle)
4213 struct osd_object *obj = osd_dt_obj(dt);
4214 struct osd_device *osd = osd_obj2dev(obj);
4215 struct inode *inode = obj->oo_inode;
4216 struct osd_thread_info *info = osd_oti_get(env);
4225 /* version set is not real XATTR */
4226 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4227 dt_obj_version_t *version = buf->lb_buf;
4229 /* for version we are just using xattr API but change inode
4231 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
4233 CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
4234 *version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4236 LDISKFS_I(inode)->i_fs_version = *version;
4237 /* Version is set after all inode operations are finished,
4238 * so we should mark it dirty here */
4239 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
4244 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
4245 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
4248 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4250 /* For the OST device with 256 bytes inode size by default,
4251 * the PFID EA will be stored together with LMA EA to avoid
4252 * performance trouble. Otherwise the PFID EA can be stored
4253 * independently. LU-8998 */
4254 if (strcmp(name, XATTR_NAME_FID) == 0 && osd->od_is_ost &&
4255 (LDISKFS_INODE_SIZE(inode->i_sb) <= 256 || obj->oo_pfid_in_lma)) {
4256 LASSERT(buf->lb_buf);
4258 fl = osd_xattr_set_pfid(env, obj, buf, fl, handle);
4261 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
4262 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4263 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4265 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4269 lma->lma_incompat |= LMAI_STRIPED;
4270 lustre_lma_swab(lma);
4271 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4272 sizeof(*lma), XATTR_REPLACE);
4275 } else if (strcmp(name, XATTR_NAME_LINK) == 0) {
4276 LASSERT(!osd->od_is_ost);
4278 rc = osd_xattr_handle_linkea(env, osd, obj, buf, handle);
4283 if (fl & LU_XATTR_REPLACE)
4284 fs_flags |= XATTR_REPLACE;
4286 if (fl & LU_XATTR_CREATE)
4287 fs_flags |= XATTR_CREATE;
4289 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, len, fs_flags);
4290 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4293 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4294 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4295 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
4301 * Concurrency: @dt is read locked.
4303 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
4304 const struct lu_buf *buf)
4306 struct osd_object *obj = osd_dt_obj(dt);
4307 struct inode *inode = obj->oo_inode;
4308 struct osd_thread_info *info = osd_oti_get(env);
4309 struct dentry *dentry = &info->oti_obj_dentry;
4311 if (!dt_object_exists(dt))
4314 LASSERT(!dt_object_remote(dt));
4315 LASSERT(inode->i_op != NULL);
4316 LASSERT(inode->i_op->listxattr != NULL);
4318 dentry->d_inode = inode;
4319 dentry->d_sb = inode->i_sb;
4320 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
4323 static int osd_declare_xattr_del(const struct lu_env *env,
4324 struct dt_object *dt, const char *name,
4325 struct thandle *handle)
4327 struct osd_thandle *oh;
4328 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4330 LASSERT(!dt_object_remote(dt));
4331 LASSERT(handle != NULL);
4333 oh = container_of0(handle, struct osd_thandle, ot_super);
4334 LASSERT(oh->ot_handle == NULL);
4336 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
4337 osd_dto_credits_noquota[DTO_XATTR_SET]);
4339 * xattr del may involve inode quota change, reserve credits for
4340 * dquot_initialize()
4342 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4348 * Concurrency: @dt is write locked.
4350 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
4351 const char *name, struct thandle *handle)
4353 struct osd_object *obj = osd_dt_obj(dt);
4354 struct inode *inode = obj->oo_inode;
4355 struct osd_thread_info *info = osd_oti_get(env);
4356 struct dentry *dentry = &info->oti_obj_dentry;
4359 if (!dt_object_exists(dt))
4362 LASSERT(!dt_object_remote(dt));
4363 LASSERT(inode->i_op != NULL);
4364 LASSERT(inode->i_op->removexattr != NULL);
4365 LASSERT(handle != NULL);
4367 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4369 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
4370 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
4372 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
4373 &info->oti_ost_attrs);
4375 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
4377 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4378 lustre_lma_swab(lma);
4379 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4380 sizeof(*lma), XATTR_REPLACE);
4382 obj->oo_pfid_in_lma = 0;
4385 ll_vfs_dq_init(inode);
4386 dentry->d_inode = inode;
4387 dentry->d_sb = inode->i_sb;
4388 rc = inode->i_op->removexattr(dentry, name);
4391 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4394 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4395 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4396 osd_oxc_del(obj, name);
4401 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
4402 __u64 start, __u64 end)
4404 struct osd_object *obj = osd_dt_obj(dt);
4405 struct inode *inode = obj->oo_inode;
4406 struct osd_thread_info *info = osd_oti_get(env);
4407 struct dentry *dentry = &info->oti_obj_dentry;
4408 struct file *file = &info->oti_file;
4413 dentry->d_inode = inode;
4414 dentry->d_sb = inode->i_sb;
4415 file->f_path.dentry = dentry;
4416 file->f_mapping = inode->i_mapping;
4417 file->f_op = inode->i_fop;
4418 set_file_inode(file, inode);
4420 rc = ll_vfs_fsync_range(file, start, end, 0);
4425 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4434 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4435 const struct dt_index_features *feat)
4437 struct iam_descr *descr;
4439 if (osd_object_is_root(o))
4440 return feat == &dt_directory_features;
4442 LASSERT(o->oo_dir != NULL);
4444 descr = o->oo_dir->od_container.ic_descr;
4445 if (feat == &dt_directory_features) {
4446 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4452 feat->dif_keysize_min <= descr->id_key_size &&
4453 descr->id_key_size <= feat->dif_keysize_max &&
4454 feat->dif_recsize_min <= descr->id_rec_size &&
4455 descr->id_rec_size <= feat->dif_recsize_max &&
4456 !(feat->dif_flags & (DT_IND_VARKEY |
4457 DT_IND_VARREC | DT_IND_NONUNQ)) &&
4458 ergo(feat->dif_flags & DT_IND_UPDATE,
4459 1 /* XXX check that object (and file system) is
4464 static int osd_iam_container_init(const struct lu_env *env,
4465 struct osd_object *obj,
4466 struct osd_directory *dir)
4468 struct iam_container *bag = &dir->od_container;
4471 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
4475 result = iam_container_setup(bag);
4477 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
4479 iam_container_fini(bag);
4486 * Concurrency: no external locking is necessary.
4488 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
4489 const struct dt_index_features *feat)
4493 struct osd_object *obj = osd_dt_obj(dt);
4495 LINVRNT(osd_invariant(obj));
4497 if (osd_object_is_root(obj)) {
4498 dt->do_index_ops = &osd_index_ea_ops;
4500 } else if (feat == &dt_directory_features) {
4501 dt->do_index_ops = &osd_index_ea_ops;
4502 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
4507 } else if (unlikely(feat == &dt_otable_features)) {
4508 dt->do_index_ops = &osd_otable_ops;
4510 } else if (unlikely(feat == &dt_acct_features)) {
4511 dt->do_index_ops = &osd_acct_index_ops;
4514 } else if (!osd_has_index(obj)) {
4515 struct osd_directory *dir;
4516 struct osd_device *osd = osd_obj2dev(obj);
4517 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
4522 spin_lock(&obj->oo_guard);
4523 if (obj->oo_dir == NULL)
4527 * Concurrent thread allocated container data.
4530 spin_unlock(&obj->oo_guard);
4532 * Now, that we have container data, serialize its
4535 down_write(&obj->oo_ext_idx_sem);
4537 * recheck under lock.
4540 if (osd_has_index(obj)) {
4545 result = osd_iam_container_init(env, obj, obj->oo_dir);
4546 if (result || feat == &dt_lfsck_namespace_features ||
4547 feat == &dt_lfsck_layout_orphan_features ||
4548 feat == &dt_lfsck_layout_dangling_features)
4551 result = osd_index_register(osd, fid,
4552 feat->dif_keysize_max,
4553 feat->dif_recsize_max);
4555 CWARN("%s: failed to register index "
4557 osd_name(osd), PFID(fid), result);
4558 else if (result > 0)
4561 CDEBUG(D_LFSCK, "%s: index object "DFID
4562 " (%d/%d) registered\n",
4563 osd_name(osd), PFID(fid),
4564 (int)feat->dif_keysize_max,
4565 (int)feat->dif_recsize_max);
4568 up_write(&obj->oo_ext_idx_sem);
4576 if (result == 0 && skip_iam == 0) {
4577 if (!osd_iam_index_probe(env, obj, feat))
4580 LINVRNT(osd_invariant(obj));
4585 static int osd_otable_it_attr_get(const struct lu_env *env,
4586 struct dt_object *dt,
4587 struct lu_attr *attr)
4593 static const struct dt_object_operations osd_obj_ops = {
4594 .do_read_lock = osd_read_lock,
4595 .do_write_lock = osd_write_lock,
4596 .do_read_unlock = osd_read_unlock,
4597 .do_write_unlock = osd_write_unlock,
4598 .do_write_locked = osd_write_locked,
4599 .do_attr_get = osd_attr_get,
4600 .do_declare_attr_set = osd_declare_attr_set,
4601 .do_attr_set = osd_attr_set,
4602 .do_ah_init = osd_ah_init,
4603 .do_declare_create = osd_declare_create,
4604 .do_create = osd_create,
4605 .do_declare_destroy = osd_declare_destroy,
4606 .do_destroy = osd_destroy,
4607 .do_index_try = osd_index_try,
4608 .do_declare_ref_add = osd_declare_ref_add,
4609 .do_ref_add = osd_ref_add,
4610 .do_declare_ref_del = osd_declare_ref_del,
4611 .do_ref_del = osd_ref_del,
4612 .do_xattr_get = osd_xattr_get,
4613 .do_declare_xattr_set = osd_declare_xattr_set,
4614 .do_xattr_set = osd_xattr_set,
4615 .do_declare_xattr_del = osd_declare_xattr_del,
4616 .do_xattr_del = osd_xattr_del,
4617 .do_xattr_list = osd_xattr_list,
4618 .do_object_sync = osd_object_sync,
4619 .do_invalidate = osd_invalidate,
4622 static const struct dt_object_operations osd_obj_otable_it_ops = {
4623 .do_attr_get = osd_otable_it_attr_get,
4624 .do_index_try = osd_index_try,
4627 static int osd_index_declare_iam_delete(const struct lu_env *env,
4628 struct dt_object *dt,
4629 const struct dt_key *key,
4630 struct thandle *handle)
4632 struct osd_thandle *oh;
4634 oh = container_of0(handle, struct osd_thandle, ot_super);
4635 LASSERT(oh->ot_handle == NULL);
4637 /* Recycle may cause additional three blocks to be changed. */
4638 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
4639 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
4645 * delete a (key, value) pair from index \a dt specified by \a key
4647 * \param dt osd index object
4648 * \param key key for index
4649 * \param rec record reference
4650 * \param handle transaction handler
4653 * \retval -ve failure
4655 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
4656 const struct dt_key *key,
4657 struct thandle *handle)
4659 struct osd_thread_info *oti = osd_oti_get(env);
4660 struct osd_object *obj = osd_dt_obj(dt);
4661 struct osd_thandle *oh;
4662 struct iam_path_descr *ipd;
4663 struct iam_container *bag = &obj->oo_dir->od_container;
4667 if (!dt_object_exists(dt))
4670 LINVRNT(osd_invariant(obj));
4671 LASSERT(!dt_object_remote(dt));
4672 LASSERT(bag->ic_object == obj->oo_inode);
4673 LASSERT(handle != NULL);
4675 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4677 ipd = osd_idx_ipd_get(env, bag);
4678 if (unlikely(ipd == NULL))
4681 oh = container_of0(handle, struct osd_thandle, ot_super);
4682 LASSERT(oh->ot_handle != NULL);
4683 LASSERT(oh->ot_handle->h_transaction != NULL);
4685 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4686 /* swab quota uid/gid provided by caller */
4687 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4688 key = (const struct dt_key *)&oti->oti_quota_id;
4691 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
4692 osd_ipd_put(env, bag, ipd);
4693 LINVRNT(osd_invariant(obj));
4694 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4698 static int osd_index_declare_ea_delete(const struct lu_env *env,
4699 struct dt_object *dt,
4700 const struct dt_key *key,
4701 struct thandle *handle)
4703 struct osd_thandle *oh;
4704 struct inode *inode;
4708 LASSERT(!dt_object_remote(dt));
4709 LASSERT(handle != NULL);
4711 oh = container_of0(handle, struct osd_thandle, ot_super);
4712 LASSERT(oh->ot_handle == NULL);
4714 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
4715 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
4717 inode = osd_dt_obj(dt)->oo_inode;
4721 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
4722 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
4727 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
4730 struct osd_fid_pack *rec;
4733 if (de->file_type & LDISKFS_DIRENT_LUFID) {
4734 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
4735 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
4736 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
4742 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
4743 const struct lu_fid *fid)
4745 struct seq_server_site *ss = osd_seq_site(osd);
4748 /* FID seqs not in FLDB, must be local seq */
4749 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
4752 /* If FLD is not being initialized yet, it only happens during the
4753 * initialization, likely during mgs initialization, and we assume
4754 * this is local FID. */
4755 if (ss == NULL || ss->ss_server_fld == NULL)
4758 /* Only check the local FLDB here */
4759 if (osd_seq_exists(env, osd, fid_seq(fid)))
4765 static void osd_take_care_of_agent(const struct lu_env *env,
4766 struct osd_device *osd,
4767 struct osd_thandle *oh,
4768 struct ldiskfs_dir_entry_2 *de)
4770 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
4771 struct osd_idmap_cache *idc;
4772 int rc, schedule = 0;
4774 LASSERT(de != NULL);
4776 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4777 if (likely(rc == 0)) {
4778 idc = osd_idc_find_or_init(env, osd, fid);
4779 if (IS_ERR(idc) || idc->oic_remote)
4781 } else if (rc == -ENODATA) {
4782 /* can't get FID, postpone to the end of the
4783 * transaction when iget() is safe */
4786 CERROR("%s: can't get FID: rc = %d\n", osd_name(osd), rc);
4789 osd_schedule_agent_inode_removal(env, oh,
4790 le32_to_cpu(de->inode));
4794 * Index delete function for interoperability mode (b11826).
4795 * It will remove the directory entry added by osd_index_ea_insert().
4796 * This entry is needed to maintain name->fid mapping.
4798 * \param key, key i.e. file entry to be deleted
4800 * \retval 0, on success
4801 * \retval -ve, on error
4803 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
4804 const struct dt_key *key, struct thandle *handle)
4806 struct osd_object *obj = osd_dt_obj(dt);
4807 struct inode *dir = obj->oo_inode;
4808 struct dentry *dentry;
4809 struct osd_thandle *oh;
4810 struct ldiskfs_dir_entry_2 *de = NULL;
4811 struct buffer_head *bh;
4812 struct htree_lock *hlock = NULL;
4813 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4817 if (!dt_object_exists(dt))
4820 LINVRNT(osd_invariant(obj));
4821 LASSERT(!dt_object_remote(dt));
4822 LASSERT(handle != NULL);
4824 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4826 oh = container_of(handle, struct osd_thandle, ot_super);
4827 LASSERT(oh->ot_handle != NULL);
4828 LASSERT(oh->ot_handle->h_transaction != NULL);
4830 ll_vfs_dq_init(dir);
4831 dentry = osd_child_dentry_get(env, obj,
4832 (char *)key, strlen((char *)key));
4834 if (obj->oo_hl_head != NULL) {
4835 hlock = osd_oti_get(env)->oti_hlock;
4836 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4837 dir, LDISKFS_HLOCK_DEL);
4839 down_write(&obj->oo_ext_idx_sem);
4842 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4844 /* If this is not the ".." entry, it might be a remote DNE
4845 * entry and we need to check if the FID is for a remote
4846 * MDT. If the FID is not in the directory entry (e.g.
4847 * upgraded 1.8 filesystem without dirdata enabled) then
4848 * we need to get the FID from the LMA. For a remote directory
4849 * there HAS to be an LMA, it cannot be an IGIF inode in this
4852 * Delete the entry before the agent inode in order to
4853 * simplify error handling. At worst an error after deleting
4854 * the entry first might leak the agent inode afterward. The
4855 * reverse would need filesystem abort in case of error deleting
4856 * the entry after the agent had been removed, or leave a
4857 * dangling entry pointing at a random inode. */
4858 if (strcmp((char *)key, dotdot) != 0)
4859 osd_take_care_of_agent(env, osd, oh, de);
4860 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4866 ldiskfs_htree_unlock(hlock);
4868 up_write(&obj->oo_ext_idx_sem);
4873 LASSERT(osd_invariant(obj));
4874 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4879 * Lookup index for \a key and copy record to \a rec.
4881 * \param dt osd index object
4882 * \param key key for index
4883 * \param rec record reference
4885 * \retval +ve success : exact mach
4886 * \retval 0 return record with key not greater than \a key
4887 * \retval -ve failure
4889 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4890 struct dt_rec *rec, const struct dt_key *key)
4892 struct osd_object *obj = osd_dt_obj(dt);
4893 struct iam_path_descr *ipd;
4894 struct iam_container *bag = &obj->oo_dir->od_container;
4895 struct osd_thread_info *oti = osd_oti_get(env);
4896 struct iam_iterator *it = &oti->oti_idx_it;
4897 struct iam_rec *iam_rec;
4901 if (!dt_object_exists(dt))
4904 LASSERT(osd_invariant(obj));
4905 LASSERT(!dt_object_remote(dt));
4906 LASSERT(bag->ic_object == obj->oo_inode);
4908 ipd = osd_idx_ipd_get(env, bag);
4912 /* got ipd now we can start iterator. */
4913 iam_it_init(it, bag, 0, ipd);
4915 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4916 /* swab quota uid/gid provided by caller */
4917 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4918 key = (const struct dt_key *)&oti->oti_quota_id;
4921 rc = iam_it_get(it, (struct iam_key *)key);
4923 if (S_ISDIR(obj->oo_inode->i_mode))
4924 iam_rec = (struct iam_rec *)oti->oti_ldp;
4926 iam_rec = (struct iam_rec *) rec;
4928 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4930 if (S_ISDIR(obj->oo_inode->i_mode))
4931 osd_fid_unpack((struct lu_fid *) rec,
4932 (struct osd_fid_pack *)iam_rec);
4933 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4934 osd_quota_unpack(obj, rec);
4939 osd_ipd_put(env, bag, ipd);
4941 LINVRNT(osd_invariant(obj));
4946 static int osd_index_declare_iam_insert(const struct lu_env *env,
4947 struct dt_object *dt,
4948 const struct dt_rec *rec,
4949 const struct dt_key *key,
4950 struct thandle *handle)
4952 struct osd_thandle *oh;
4954 LASSERT(handle != NULL);
4956 oh = container_of0(handle, struct osd_thandle, ot_super);
4957 LASSERT(oh->ot_handle == NULL);
4959 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4960 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4966 * Inserts (key, value) pair in \a dt index object.
4968 * \param dt osd index object
4969 * \param key key for index
4970 * \param rec record reference
4971 * \param th transaction handler
4974 * \retval -ve failure
4976 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4977 const struct dt_rec *rec,
4978 const struct dt_key *key, struct thandle *th,
4981 struct osd_object *obj = osd_dt_obj(dt);
4982 struct iam_path_descr *ipd;
4983 struct osd_thandle *oh;
4984 struct iam_container *bag;
4985 struct osd_thread_info *oti = osd_oti_get(env);
4986 struct iam_rec *iam_rec;
4990 if (!dt_object_exists(dt))
4993 LINVRNT(osd_invariant(obj));
4994 LASSERT(!dt_object_remote(dt));
4996 bag = &obj->oo_dir->od_container;
4997 LASSERT(bag->ic_object == obj->oo_inode);
4998 LASSERT(th != NULL);
5000 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5002 ipd = osd_idx_ipd_get(env, bag);
5003 if (unlikely(ipd == NULL))
5006 oh = container_of0(th, struct osd_thandle, ot_super);
5007 LASSERT(oh->ot_handle != NULL);
5008 LASSERT(oh->ot_handle->h_transaction != NULL);
5009 if (S_ISDIR(obj->oo_inode->i_mode)) {
5010 iam_rec = (struct iam_rec *)oti->oti_ldp;
5011 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
5012 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5013 /* pack quota uid/gid */
5014 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5015 key = (const struct dt_key *)&oti->oti_quota_id;
5016 /* pack quota record */
5017 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
5018 iam_rec = (struct iam_rec *)rec;
5020 iam_rec = (struct iam_rec *)rec;
5023 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
5025 osd_ipd_put(env, bag, ipd);
5026 LINVRNT(osd_invariant(obj));
5027 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5032 * Calls ldiskfs_add_entry() to add directory entry
5033 * into the directory. This is required for
5034 * interoperability mode (b11826)
5036 * \retval 0, on success
5037 * \retval -ve, on error
5039 static int __osd_ea_add_rec(struct osd_thread_info *info,
5040 struct osd_object *pobj, struct inode *cinode,
5041 const char *name, const struct lu_fid *fid,
5042 struct htree_lock *hlock, struct thandle *th)
5044 struct ldiskfs_dentry_param *ldp;
5045 struct dentry *child;
5046 struct osd_thandle *oth;
5049 oth = container_of(th, struct osd_thandle, ot_super);
5050 LASSERT(oth->ot_handle != NULL);
5051 LASSERT(oth->ot_handle->h_transaction != NULL);
5052 LASSERT(pobj->oo_inode);
5054 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
5055 if (unlikely(pobj->oo_inode ==
5056 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
5059 osd_get_ldiskfs_dirent_param(ldp, fid);
5060 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
5061 child->d_fsdata = (void *)ldp;
5062 ll_vfs_dq_init(pobj->oo_inode);
5063 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
5064 child, cinode, hlock);
5065 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
5066 struct ldiskfs_dir_entry_2 *de;
5067 struct buffer_head *bh;
5070 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
5073 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
5076 if (S_ISDIR(cinode->i_mode))
5077 de->file_type = LDISKFS_DIRENT_LUFID |
5078 LDISKFS_FT_REG_FILE;
5080 de->file_type = LDISKFS_DIRENT_LUFID |
5082 ldiskfs_handle_dirty_metadata(oth->ot_handle,
5093 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
5094 * into the directory.Also sets flags into osd object to
5095 * indicate dot and dotdot are created. This is required for
5096 * interoperability mode (b11826)
5098 * \param dir directory for dot and dotdot fixup.
5099 * \param obj child object for linking
5101 * \retval 0, on success
5102 * \retval -ve, on error
5104 static int osd_add_dot_dotdot(struct osd_thread_info *info,
5105 struct osd_object *dir,
5106 struct inode *parent_dir, const char *name,
5107 const struct lu_fid *dot_fid,
5108 const struct lu_fid *dot_dot_fid,
5111 struct inode *inode = dir->oo_inode;
5112 struct osd_thandle *oth;
5115 oth = container_of(th, struct osd_thandle, ot_super);
5116 LASSERT(oth->ot_handle->h_transaction != NULL);
5117 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
5119 if (strcmp(name, dot) == 0) {
5120 if (dir->oo_compat_dot_created) {
5123 LASSERT(inode->i_ino == parent_dir->i_ino);
5124 dir->oo_compat_dot_created = 1;
5127 } else if (strcmp(name, dotdot) == 0) {
5128 if (!dir->oo_compat_dot_created)
5130 /* in case of rename, dotdot is already created */
5131 if (dir->oo_compat_dotdot_created) {
5132 return __osd_ea_add_rec(info, dir, parent_dir, name,
5133 dot_dot_fid, NULL, th);
5136 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
5137 struct lu_fid tfid = *dot_dot_fid;
5140 result = osd_add_dot_dotdot_internal(info,
5141 dir->oo_inode, parent_dir, dot_fid,
5144 result = osd_add_dot_dotdot_internal(info,
5145 dir->oo_inode, parent_dir, dot_fid,
5150 dir->oo_compat_dotdot_created = 1;
5158 * It will call the appropriate osd_add* function and return the
5159 * value, return by respective functions.
5161 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
5162 struct inode *cinode, const char *name,
5163 const struct lu_fid *fid, struct thandle *th)
5165 struct osd_thread_info *info = osd_oti_get(env);
5166 struct htree_lock *hlock;
5169 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
5171 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
5173 if (hlock != NULL) {
5174 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5177 down_write(&pobj->oo_ext_idx_sem);
5180 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
5181 lu_object_fid(&pobj->oo_dt.do_lu),
5184 if (hlock != NULL) {
5185 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5186 pobj->oo_inode, LDISKFS_HLOCK_ADD);
5188 down_write(&pobj->oo_ext_idx_sem);
5191 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
5192 struct lu_fid *tfid = &info->oti_fid;
5196 rc = __osd_ea_add_rec(info, pobj, cinode, name,
5199 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
5204 ldiskfs_htree_unlock(hlock);
5206 up_write(&pobj->oo_ext_idx_sem);
5212 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
5213 struct osd_idmap_cache *oic)
5215 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
5216 struct lu_fid *fid = &oic->oic_fid;
5217 struct osd_inode_id *id = &oic->oic_lid;
5218 struct inode *inode = NULL;
5224 if (!fid_is_norm(fid) && !fid_is_igif(fid))
5227 if (thread_is_running(&scrub->os_thread) &&
5228 scrub->os_pos_current > id->oii_ino)
5231 if (dev->od_auto_scrub_interval == AS_NEVER ||
5232 cfs_time_before(ktime_get_real_seconds(),
5233 scrub->os_file.sf_time_last_complete +
5234 dev->od_auto_scrub_interval))
5238 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
5239 if (rc == -ENOENT) {
5240 __u32 gen = id->oii_gen;
5246 inode = osd_iget(oti, dev, id);
5247 /* The inode has been removed (by race maybe). */
5248 if (IS_ERR(inode)) {
5249 rc = PTR_ERR(inode);
5251 RETURN(rc == -ESTALE ? -ENOENT : rc);
5254 /* The OI mapping is lost. */
5255 if (gen != OSD_OII_NOGEN)
5258 /* The inode may has been reused by others, we do not know,
5259 * leave it to be handled by subsequent osd_fid_lookup(). */
5261 } else if (rc || osd_id_eq(id, &oti->oti_id)) {
5268 if (thread_is_running(&scrub->os_thread)) {
5269 if (inode == NULL) {
5270 inode = osd_iget(oti, dev, id);
5271 /* The inode has been removed (by race maybe). */
5272 if (IS_ERR(inode)) {
5273 rc = PTR_ERR(inode);
5275 RETURN(rc == -ESTALE ? -ENOENT : rc);
5279 rc = osd_oii_insert(dev, oic, insert);
5280 /* There is race condition between osd_oi_lookup and OI scrub.
5281 * The OI scrub finished just after osd_oi_lookup() failure.
5282 * Under such case, it is unnecessary to trigger OI scrub again,
5283 * but try to call osd_oi_lookup() again. */
5284 if (unlikely(rc == -EAGAIN))
5287 if (!S_ISDIR(inode->i_mode))
5290 rc = osd_check_lmv(oti, dev, inode, oic);
5295 if (dev->od_auto_scrub_interval != AS_NEVER && ++once == 1) {
5296 rc = osd_scrub_start(oti->oti_env, dev, SS_AUTO_PARTIAL |
5297 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
5298 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
5299 "%s: trigger partial OI scrub for RPC inconsistency "
5300 "checking FID "DFID": rc = %d\n",
5301 osd_dev2name(dev), PFID(fid), rc);
5302 if (rc == 0 || rc == -EALREADY)
5315 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
5316 struct osd_device *dev,
5317 struct osd_idmap_cache *oic,
5318 struct lu_fid *fid, __u32 ino)
5320 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
5321 struct inode *inode;
5324 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
5325 inode = osd_iget(oti, dev, &oic->oic_lid);
5326 if (IS_ERR(inode)) {
5327 fid_zero(&oic->oic_fid);
5328 return PTR_ERR(inode);
5331 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, loa);
5334 fid_zero(&oic->oic_fid);
5336 *fid = oic->oic_fid = loa->loa_lma.lma_self_fid;
5340 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
5341 struct osd_inode_id *id, const struct lu_fid *fid)
5343 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
5344 id->oii_ino, id->oii_gen, info);
5345 info->oti_cache.oic_lid = *id;
5346 info->oti_cache.oic_fid = *fid;
5347 info->oti_cache.oic_dev = osd;
5351 * Get parent FID from the linkEA.
5353 * For a directory which parent resides on remote MDT, to satisfy the
5354 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
5355 * the other hand, to make the lookup(..) on the directory can return
5356 * the real parent FID, we append the real parent FID after its ".."
5357 * name entry in the /REMOTE_PARENT_DIR.
5359 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
5360 * backup. So after the restore, we cannot get the right parent FID from
5361 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
5362 * we have stored the real parent FID in the directory object's linkEA,
5363 * we can parse the linkEA for the real parent FID.
5365 * \param[in] env pointer to the thread context
5366 * \param[in] obj pointer to the object to be handled
5367 * \param[out]fid pointer to the buffer to hold the parent FID
5369 * \retval 0 for getting the real parent FID successfully
5370 * \retval negative error number on failure
5372 static int osd_get_pfid_from_linkea(const struct lu_env *env,
5373 struct osd_object *obj,
5376 struct osd_thread_info *oti = osd_oti_get(env);
5377 struct lu_buf *buf = &oti->oti_big_buf;
5378 struct dentry *dentry = &oti->oti_obj_dentry;
5379 struct inode *inode = obj->oo_inode;
5380 struct linkea_data ldata = { NULL };
5385 if (!S_ISDIR(inode->i_mode))
5389 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5390 buf->lb_buf, buf->lb_len);
5391 if (rc == -ERANGE) {
5392 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5395 lu_buf_realloc(buf, rc);
5396 if (buf->lb_buf == NULL)
5403 if (unlikely(rc == 0))
5409 if (unlikely(buf->lb_buf == NULL)) {
5410 lu_buf_realloc(buf, rc);
5411 if (buf->lb_buf == NULL)
5418 rc = linkea_init_with_rec(&ldata);
5420 linkea_first_entry(&ldata);
5421 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5427 static int osd_verify_ent_by_linkea(const struct lu_env *env,
5428 struct inode *inode,
5429 const struct lu_fid *pfid,
5430 const char *name, const int namelen)
5432 struct osd_thread_info *oti = osd_oti_get(env);
5433 struct lu_buf *buf = &oti->oti_big_buf;
5434 struct dentry *dentry = &oti->oti_obj_dentry;
5435 struct linkea_data ldata = { NULL };
5436 struct lu_name cname = { .ln_name = name,
5437 .ln_namelen = namelen };
5442 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5443 buf->lb_buf, buf->lb_len);
5445 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
5450 if (unlikely(rc == 0))
5453 if (buf->lb_len < rc) {
5454 lu_buf_realloc(buf, rc);
5455 if (buf->lb_buf == NULL)
5462 rc = linkea_init_with_rec(&ldata);
5464 rc = linkea_links_find(&ldata, &cname, pfid);
5470 * Calls ->lookup() to find dentry. From dentry get inode and
5471 * read inode's ea to get fid. This is required for interoperability
5474 * \retval 0, on success
5475 * \retval -ve, on error
5477 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
5478 struct dt_rec *rec, const struct dt_key *key)
5480 struct inode *dir = obj->oo_inode;
5481 struct dentry *dentry;
5482 struct ldiskfs_dir_entry_2 *de;
5483 struct buffer_head *bh;
5484 struct lu_fid *fid = (struct lu_fid *) rec;
5485 struct htree_lock *hlock = NULL;
5490 LASSERT(dir->i_op != NULL);
5491 LASSERT(dir->i_op->lookup != NULL);
5493 dentry = osd_child_dentry_get(env, obj,
5494 (char *)key, strlen((char *)key));
5496 if (obj->oo_hl_head != NULL) {
5497 hlock = osd_oti_get(env)->oti_hlock;
5498 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5499 dir, LDISKFS_HLOCK_LOOKUP);
5501 down_read(&obj->oo_ext_idx_sem);
5504 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5506 struct osd_thread_info *oti = osd_oti_get(env);
5507 struct osd_inode_id *id = &oti->oti_id;
5508 struct osd_idmap_cache *oic = &oti->oti_cache;
5509 struct osd_device *dev = osd_obj2dev(obj);
5511 ino = le32_to_cpu(de->inode);
5512 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
5514 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
5518 rc = osd_get_fid_from_dentry(de, rec);
5520 /* done with de, release bh */
5523 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5524 const char *name = (const char *)key;
5526 /* If the parent is on remote MDT, and there
5527 * is no FID-in-dirent, then we have to get
5528 * the parent FID from the linkEA. */
5529 if (likely(strlen(name) == 2 &&
5530 name[0] == '.' && name[1] == '.'))
5531 rc = osd_get_pfid_from_linkea(env, obj,
5534 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5537 osd_id_gen(id, ino, OSD_OII_NOGEN);
5540 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
5541 fid_zero(&oic->oic_fid);
5546 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
5547 rc = osd_consistency_check(oti, dev, oic);
5549 fid_zero(&oic->oic_fid);
5551 /* Other error should not affect lookup result. */
5561 ldiskfs_htree_unlock(hlock);
5563 up_read(&obj->oo_ext_idx_sem);
5567 static int osd_index_declare_ea_insert(const struct lu_env *env,
5568 struct dt_object *dt,
5569 const struct dt_rec *rec,
5570 const struct dt_key *key,
5571 struct thandle *handle)
5573 struct osd_thandle *oh;
5574 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5575 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5576 const struct lu_fid *fid = rec1->rec_fid;
5577 int credits, rc = 0;
5578 struct osd_idmap_cache *idc;
5581 LASSERT(!dt_object_remote(dt));
5582 LASSERT(handle != NULL);
5583 LASSERT(fid != NULL);
5584 LASSERT(rec1->rec_type != 0);
5586 oh = container_of0(handle, struct osd_thandle, ot_super);
5587 LASSERT(oh->ot_handle == NULL);
5589 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
5591 /* we can't call iget() while a transactions is running
5592 * (this can lead to a deadlock), but we need to know
5593 * inum and object type. so we find this information at
5594 * declaration and cache in per-thread info */
5595 idc = osd_idc_find_or_init(env, osd, fid);
5597 RETURN(PTR_ERR(idc));
5598 if (idc->oic_remote) {
5599 /* a reference to remote inode is represented by an
5600 * agent inode which we have to create */
5601 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
5602 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
5605 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
5607 if (osd_dt_obj(dt)->oo_inode != NULL) {
5608 struct inode *inode = osd_dt_obj(dt)->oo_inode;
5610 /* We ignore block quota on meta pool (MDTs), so needn't
5611 * calculate how many blocks will be consumed by this index
5613 rc = osd_declare_inode_qid(env, i_uid_read(inode),
5615 i_projid_read(inode), 0,
5616 oh, osd_dt_obj(dt), NULL,
5621 #ifdef HAVE_PROJECT_QUOTA
5622 /* Reserve credits for local agent inode to transfer
5623 * to 0, quota enforcement is ignored in this case.
5625 if (idc->oic_remote &&
5626 LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
5627 i_projid_read(inode) != 0)
5628 rc = osd_declare_attr_qid(env, osd_dt_obj(dt), oh,
5629 0, i_projid_read(inode),
5630 0, false, PRJQUOTA, true);
5639 * Index add function for interoperability mode (b11826).
5640 * It will add the directory entry.This entry is needed to
5641 * maintain name->fid mapping.
5643 * \param key it is key i.e. file entry to be inserted
5644 * \param rec it is value of given key i.e. fid
5646 * \retval 0, on success
5647 * \retval -ve, on error
5649 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
5650 const struct dt_rec *rec,
5651 const struct dt_key *key, struct thandle *th,
5654 struct osd_object *obj = osd_dt_obj(dt);
5655 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5656 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5657 const struct lu_fid *fid = rec1->rec_fid;
5658 const char *name = (const char *)key;
5659 struct osd_thread_info *oti = osd_oti_get(env);
5660 struct inode *child_inode = NULL;
5661 struct osd_idmap_cache *idc;
5665 if (!dt_object_exists(dt))
5668 LASSERT(osd_invariant(obj));
5669 LASSERT(!dt_object_remote(dt));
5670 LASSERT(th != NULL);
5672 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5674 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
5676 idc = osd_idc_find(env, osd, fid);
5677 if (unlikely(idc == NULL)) {
5678 /* this dt_insert() wasn't declared properly, so
5679 * FID is missing in OI cache. we better do not
5680 * lookup FID in FLDB/OI and don't risk to deadlock,
5681 * but in some special cases (lfsck testing, etc)
5682 * it's much simpler than fixing a caller */
5683 CERROR("%s: "DFID" wasn't declared for insert\n",
5684 osd_name(osd), PFID(fid));
5686 idc = osd_idc_find_or_init(env, osd, fid);
5688 RETURN(PTR_ERR(idc));
5691 if (idc->oic_remote) {
5692 /* Insert remote entry */
5693 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
5695 igrab(osd->od_mdt_map->omm_remote_parent->d_inode);
5697 child_inode = osd_create_local_agent_inode(env, osd,
5698 obj, fid, rec1->rec_type & S_IFMT, th);
5699 if (IS_ERR(child_inode))
5700 RETURN(PTR_ERR(child_inode));
5703 /* Insert local entry */
5704 if (unlikely(idc->oic_lid.oii_ino == 0)) {
5705 /* for a reason OI cache wasn't filled properly */
5706 CERROR("%s: OIC for "DFID" isn't filled\n",
5707 osd_name(osd), PFID(fid));
5710 child_inode = oti->oti_inode;
5711 if (unlikely(child_inode == NULL)) {
5712 struct ldiskfs_inode_info *lii;
5716 child_inode = oti->oti_inode = &lii->vfs_inode;
5718 child_inode->i_sb = osd_sb(osd);
5719 child_inode->i_ino = idc->oic_lid.oii_ino;
5720 child_inode->i_mode = rec1->rec_type & S_IFMT;
5723 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
5725 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
5726 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
5728 if (child_inode && child_inode != oti->oti_inode)
5730 LASSERT(osd_invariant(obj));
5731 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5736 * Initialize osd Iterator for given osd index object.
5738 * \param dt osd index object
5741 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
5742 struct dt_object *dt,
5745 struct osd_it_iam *it;
5746 struct osd_object *obj = osd_dt_obj(dt);
5747 struct lu_object *lo = &dt->do_lu;
5748 struct iam_path_descr *ipd;
5749 struct iam_container *bag = &obj->oo_dir->od_container;
5751 if (!dt_object_exists(dt))
5752 return ERR_PTR(-ENOENT);
5756 return ERR_PTR(-ENOMEM);
5758 ipd = osd_it_ipd_get(env, bag);
5759 if (likely(ipd != NULL)) {
5763 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
5764 return (struct dt_it *)it;
5767 return ERR_PTR(-ENOMEM);
5772 * free given Iterator.
5774 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
5776 struct osd_it_iam *it = (struct osd_it_iam *)di;
5777 struct osd_object *obj = it->oi_obj;
5779 iam_it_fini(&it->oi_it);
5780 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
5781 osd_object_put(env, obj);
5786 * Move Iterator to record specified by \a key
5788 * \param di osd iterator
5789 * \param key key for index
5791 * \retval +ve di points to record with least key not larger than key
5792 * \retval 0 di points to exact matched key
5793 * \retval -ve failure
5796 static int osd_it_iam_get(const struct lu_env *env,
5797 struct dt_it *di, const struct dt_key *key)
5799 struct osd_thread_info *oti = osd_oti_get(env);
5800 struct osd_it_iam *it = (struct osd_it_iam *)di;
5802 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5803 /* swab quota uid/gid */
5804 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5805 key = (struct dt_key *)&oti->oti_quota_id;
5808 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
5814 * \param di osd iterator
5816 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
5818 struct osd_it_iam *it = (struct osd_it_iam *)di;
5820 iam_it_put(&it->oi_it);
5824 * Move iterator by one record
5826 * \param di osd iterator
5828 * \retval +1 end of container reached
5830 * \retval -ve failure
5833 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
5835 struct osd_it_iam *it = (struct osd_it_iam *)di;
5837 return iam_it_next(&it->oi_it);
5841 * Return pointer to the key under iterator.
5844 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
5845 const struct dt_it *di)
5847 struct osd_thread_info *oti = osd_oti_get(env);
5848 struct osd_it_iam *it = (struct osd_it_iam *)di;
5849 struct osd_object *obj = it->oi_obj;
5852 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
5854 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
5855 /* swab quota uid/gid */
5856 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
5857 key = (struct dt_key *)&oti->oti_quota_id;
5864 * Return size of key under iterator (in bytes)
5867 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5869 struct osd_it_iam *it = (struct osd_it_iam *)di;
5871 return iam_it_key_size(&it->oi_it);
5875 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5877 /* check if file type is required */
5878 if (ent->lde_attrs & LUDA_TYPE) {
5879 struct luda_type *lt;
5880 int align = sizeof(*lt) - 1;
5882 len = (len + align) & ~align;
5883 lt = (struct luda_type *)(ent->lde_name + len);
5884 lt->lt_type = cpu_to_le16(DTTOIF(type));
5887 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5891 * build lu direct from backend fs dirent.
5895 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5896 char *name, __u16 namelen, __u16 type, __u32 attr)
5898 ent->lde_attrs = attr | LUDA_FID;
5899 fid_cpu_to_le(&ent->lde_fid, fid);
5901 ent->lde_hash = cpu_to_le64(offset);
5902 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5904 strncpy(ent->lde_name, name, namelen);
5905 ent->lde_name[namelen] = '\0';
5906 ent->lde_namelen = cpu_to_le16(namelen);
5908 /* append lustre attributes */
5909 osd_it_append_attrs(ent, namelen, type);
5913 * Return pointer to the record under iterator.
5915 static int osd_it_iam_rec(const struct lu_env *env,
5916 const struct dt_it *di,
5917 struct dt_rec *dtrec, __u32 attr)
5919 struct osd_it_iam *it = (struct osd_it_iam *)di;
5920 struct osd_thread_info *info = osd_oti_get(env);
5923 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5924 const struct osd_fid_pack *rec;
5925 struct lu_fid *fid = &info->oti_fid;
5926 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5932 name = (char *)iam_it_key_get(&it->oi_it);
5934 RETURN(PTR_ERR(name));
5936 namelen = iam_it_key_size(&it->oi_it);
5938 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5940 RETURN(PTR_ERR(rec));
5942 rc = osd_fid_unpack(fid, rec);
5946 hash = iam_it_store(&it->oi_it);
5948 /* IAM does not store object type in IAM index (dir) */
5949 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5951 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5952 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5953 (struct iam_rec *)dtrec);
5954 osd_quota_unpack(it->oi_obj, dtrec);
5956 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5957 (struct iam_rec *)dtrec);
5964 * Returns cookie for current Iterator position.
5966 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5968 struct osd_it_iam *it = (struct osd_it_iam *)di;
5970 return iam_it_store(&it->oi_it);
5974 * Restore iterator from cookie.
5976 * \param di osd iterator
5977 * \param hash Iterator location cookie
5979 * \retval +ve di points to record with least key not larger than key.
5980 * \retval 0 di points to exact matched key
5981 * \retval -ve failure
5984 static int osd_it_iam_load(const struct lu_env *env,
5985 const struct dt_it *di, __u64 hash)
5987 struct osd_it_iam *it = (struct osd_it_iam *)di;
5989 return iam_it_load(&it->oi_it, hash);
5992 static const struct dt_index_operations osd_index_iam_ops = {
5993 .dio_lookup = osd_index_iam_lookup,
5994 .dio_declare_insert = osd_index_declare_iam_insert,
5995 .dio_insert = osd_index_iam_insert,
5996 .dio_declare_delete = osd_index_declare_iam_delete,
5997 .dio_delete = osd_index_iam_delete,
5999 .init = osd_it_iam_init,
6000 .fini = osd_it_iam_fini,
6001 .get = osd_it_iam_get,
6002 .put = osd_it_iam_put,
6003 .next = osd_it_iam_next,
6004 .key = osd_it_iam_key,
6005 .key_size = osd_it_iam_key_size,
6006 .rec = osd_it_iam_rec,
6007 .store = osd_it_iam_store,
6008 .load = osd_it_iam_load
6014 * Creates or initializes iterator context.
6016 * \retval struct osd_it_ea, iterator structure on success
6019 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
6020 struct dt_object *dt,
6023 struct osd_object *obj = osd_dt_obj(dt);
6024 struct osd_thread_info *info = osd_oti_get(env);
6025 struct osd_it_ea *oie;
6027 struct lu_object *lo = &dt->do_lu;
6028 struct dentry *obj_dentry;
6031 if (!dt_object_exists(dt) || obj->oo_destroyed)
6032 RETURN(ERR_PTR(-ENOENT));
6034 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
6036 RETURN(ERR_PTR(-ENOMEM));
6037 obj_dentry = &oie->oie_dentry;
6039 obj_dentry->d_inode = obj->oo_inode;
6040 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
6041 obj_dentry->d_name.hash = 0;
6043 oie->oie_rd_dirent = 0;
6044 oie->oie_it_dirent = 0;
6045 oie->oie_dirent = NULL;
6046 if (unlikely(!info->oti_it_ea_buf_used)) {
6047 oie->oie_buf = info->oti_it_ea_buf;
6048 info->oti_it_ea_buf_used = 1;
6050 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6051 if (oie->oie_buf == NULL)
6052 RETURN(ERR_PTR(-ENOMEM));
6056 file = &oie->oie_file;
6058 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
6059 if (attr & LUDA_64BITHASH)
6060 file->f_mode = FMODE_64BITHASH;
6062 file->f_mode = FMODE_32BITHASH;
6063 file->f_path.dentry = obj_dentry;
6064 file->f_mapping = obj->oo_inode->i_mapping;
6065 file->f_op = obj->oo_inode->i_fop;
6066 set_file_inode(file, obj->oo_inode);
6069 RETURN((struct dt_it *) oie);
6073 * Destroy or finishes iterator context.
6075 * \param di iterator structure to be destroyed
6077 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
6079 struct osd_thread_info *info = osd_oti_get(env);
6080 struct osd_it_ea *oie = (struct osd_it_ea *)di;
6081 struct osd_object *obj = oie->oie_obj;
6082 struct inode *inode = obj->oo_inode;
6085 oie->oie_file.f_op->release(inode, &oie->oie_file);
6086 osd_object_put(env, obj);
6087 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
6088 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6090 info->oti_it_ea_buf_used = 0;
6091 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
6096 * It position the iterator at given key, so that next lookup continues from
6097 * that key Or it is similar to dio_it->load() but based on a key,
6098 * rather than file position.
6100 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
6103 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
6105 static int osd_it_ea_get(const struct lu_env *env,
6106 struct dt_it *di, const struct dt_key *key)
6108 struct osd_it_ea *it = (struct osd_it_ea *)di;
6111 LASSERT(((const char *)key)[0] == '\0');
6112 it->oie_file.f_pos = 0;
6113 it->oie_rd_dirent = 0;
6114 it->oie_it_dirent = 0;
6115 it->oie_dirent = NULL;
6123 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
6127 struct osd_filldir_cbs {
6128 #ifdef HAVE_DIR_CONTEXT
6129 struct dir_context ctx;
6131 struct osd_it_ea *it;
6134 * It is called internally by ->readdir(). It fills the
6135 * iterator's in-memory data structure with required
6136 * information i.e. name, namelen, rec_size etc.
6138 * \param buf in which information to be filled in.
6139 * \param name name of the file in given dir
6141 * \retval 0 on success
6142 * \retval 1 on buffer full
6144 #ifdef HAVE_FILLDIR_USE_CTX
6145 static int osd_ldiskfs_filldir(struct dir_context *buf,
6147 static int osd_ldiskfs_filldir(void *buf,
6149 const char *name, int namelen,
6150 loff_t offset, __u64 ino, unsigned d_type)
6152 struct osd_it_ea *it =
6153 ((struct osd_filldir_cbs *)buf)->it;
6154 struct osd_object *obj = it->oie_obj;
6155 struct osd_it_ea_dirent *ent = it->oie_dirent;
6156 struct lu_fid *fid = &ent->oied_fid;
6157 struct osd_fid_pack *rec;
6160 /* this should never happen */
6161 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
6162 CERROR("ldiskfs return invalid namelen %d\n", namelen);
6166 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
6170 /* "." is just the object itself. */
6171 if (namelen == 1 && name[0] == '.') {
6172 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6173 } else if (d_type & LDISKFS_DIRENT_LUFID) {
6174 rec = (struct osd_fid_pack*) (name + namelen + 1);
6175 if (osd_fid_unpack(fid, rec) != 0)
6180 d_type &= ~LDISKFS_DIRENT_LUFID;
6182 /* NOT export local root. */
6183 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
6184 ino = obj->oo_inode->i_ino;
6185 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6188 ent->oied_ino = ino;
6189 ent->oied_off = offset;
6190 ent->oied_namelen = namelen;
6191 ent->oied_type = d_type;
6193 memcpy(ent->oied_name, name, namelen);
6195 it->oie_rd_dirent++;
6196 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
6201 * Calls ->readdir() to load a directory entry at a time
6202 * and stored it in iterator's in-memory data structure.
6204 * \param di iterator's in memory structure
6206 * \retval 0 on success
6207 * \retval -ve on error
6208 * \retval +1 reach the end of entry
6210 static int osd_ldiskfs_it_fill(const struct lu_env *env,
6211 const struct dt_it *di)
6213 struct osd_it_ea *it = (struct osd_it_ea *)di;
6214 struct osd_object *obj = it->oie_obj;
6215 struct inode *inode = obj->oo_inode;
6216 struct htree_lock *hlock = NULL;
6217 struct file *filp = &it->oie_file;
6219 struct osd_filldir_cbs buf = {
6220 #ifdef HAVE_DIR_CONTEXT
6221 .ctx.actor = osd_ldiskfs_filldir,
6227 it->oie_dirent = it->oie_buf;
6228 it->oie_rd_dirent = 0;
6230 if (obj->oo_hl_head != NULL) {
6231 hlock = osd_oti_get(env)->oti_hlock;
6232 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
6233 inode, LDISKFS_HLOCK_READDIR);
6235 down_read(&obj->oo_ext_idx_sem);
6238 #ifdef HAVE_DIR_CONTEXT
6239 buf.ctx.pos = filp->f_pos;
6240 rc = inode->i_fop->iterate(filp, &buf.ctx);
6241 filp->f_pos = buf.ctx.pos;
6243 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
6247 ldiskfs_htree_unlock(hlock);
6249 up_read(&obj->oo_ext_idx_sem);
6251 if (it->oie_rd_dirent == 0) {
6252 /*If it does not get any dirent, it means it has been reached
6253 *to the end of the dir */
6254 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
6258 it->oie_dirent = it->oie_buf;
6259 it->oie_it_dirent = 1;
6266 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6267 * to load a directory entry at a time and stored it in
6268 * iterator's in-memory data structure.
6270 * \param di iterator's in memory structure
6272 * \retval +ve iterator reached to end
6273 * \retval 0 iterator not reached to end
6274 * \retval -ve on error
6276 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
6278 struct osd_it_ea *it = (struct osd_it_ea *)di;
6283 if (it->oie_it_dirent < it->oie_rd_dirent) {
6285 (void *) it->oie_dirent +
6286 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
6287 it->oie_dirent->oied_namelen);
6288 it->oie_it_dirent++;
6291 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
6294 rc = osd_ldiskfs_it_fill(env, di);
6301 * Returns the key at current position from iterator's in memory structure.
6303 * \param di iterator's in memory structure
6305 * \retval key i.e. struct dt_key on success
6307 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
6308 const struct dt_it *di)
6310 struct osd_it_ea *it = (struct osd_it_ea *)di;
6312 return (struct dt_key *)it->oie_dirent->oied_name;
6316 * Returns the key's size at current position from iterator's in memory structure.
6318 * \param di iterator's in memory structure
6320 * \retval key_size i.e. struct dt_key on success
6322 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
6324 struct osd_it_ea *it = (struct osd_it_ea *)di;
6326 return it->oie_dirent->oied_namelen;
6329 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
6331 if (LDISKFS_DIR_REC_LEN(de) >=
6332 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
6339 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
6340 unsigned blocksize, bool dotdot)
6343 return osd_dotdot_has_space(de);
6345 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
6346 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
6353 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
6354 handle_t *jh, struct dentry *dentry,
6355 const struct lu_fid *fid, struct buffer_head *bh,
6356 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
6359 struct inode *dir = dentry->d_parent->d_inode;
6360 struct inode *inode = dentry->d_inode;
6361 struct osd_fid_pack *rec;
6362 struct ldiskfs_dentry_param *ldp;
6363 int namelen = dentry->d_name.len;
6365 struct osd_thread_info *info = osd_oti_get(env);
6368 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
6369 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6372 /* There is enough space to hold the FID-in-dirent. */
6373 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
6374 rc = ldiskfs_journal_get_write_access(jh, bh);
6378 de->name[namelen] = 0;
6379 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
6380 rec->fp_len = sizeof(struct lu_fid) + 1;
6381 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
6382 de->file_type |= LDISKFS_DIRENT_LUFID;
6383 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
6390 rc = ldiskfs_delete_entry(jh, dir, de, bh);
6394 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
6395 osd_get_ldiskfs_dirent_param(ldp, fid);
6396 dentry->d_fsdata = (void *)ldp;
6397 ll_vfs_dq_init(dir);
6398 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
6399 /* It is too bad, we cannot reinsert the name entry back.
6400 * That means we lose it! */
6402 CDEBUG(D_LFSCK, "%s: fail to reinsert the dirent, "
6403 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
6404 osd_ino2name(inode),
6405 dir->i_ino, dir->i_generation, namelen,
6406 dentry->d_name.name, PFID(fid), rc);
6412 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
6413 struct osd_it_ea *it, struct lu_fid *fid,
6414 struct osd_inode_id *id, __u32 *attr)
6416 struct osd_thread_info *info = osd_oti_get(env);
6417 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
6418 struct osd_device *dev = osd_obj2dev(obj);
6419 struct super_block *sb = osd_sb(dev);
6420 const char *devname = osd_name(dev);
6421 struct osd_it_ea_dirent *ent = it->oie_dirent;
6422 struct inode *dir = obj->oo_inode;
6423 struct htree_lock *hlock = NULL;
6424 struct buffer_head *bh = NULL;
6425 handle_t *jh = NULL;
6426 struct ldiskfs_dir_entry_2 *de;
6427 struct dentry *dentry;
6428 struct inode *inode;
6429 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
6432 bool dotdot = false;
6436 if (ent->oied_name[0] == '.') {
6437 if (ent->oied_namelen == 1)
6440 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
6444 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
6445 inode = osd_iget(info, dev, id);
6446 if (IS_ERR(inode)) {
6447 rc = PTR_ERR(inode);
6448 if (rc == -ENOENT || rc == -ESTALE) {
6449 /* Maybe dangling name entry, or
6450 * corrupted directory entry. */
6451 *attr |= LUDA_UNKNOWN;
6454 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
6455 "check_repair, dir = %lu/%u, name = %.*s, "
6456 "ino = %llu, rc = %d\n",
6457 devname, dir->i_ino, dir->i_generation,
6458 ent->oied_namelen, ent->oied_name,
6465 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
6467 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
6468 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
6473 /* We need to ensure that the name entry is still valid.
6474 * Because it may be removed or renamed by other already.
6476 * The unlink or rename operation will start journal before PDO lock,
6477 * so to avoid deadlock, here we need to start journal handle before
6478 * related PDO lock also. But because we do not know whether there
6479 * will be something to be repaired before PDO lock, we just start
6480 * journal without conditions.
6482 * We may need to remove the name entry firstly, then insert back.
6483 * One credit is for user quota file update.
6484 * One credit is for group quota file update.
6485 * Two credits are for dirty inode. */
6486 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
6487 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
6489 if (dev->od_dirent_journal != 0) {
6492 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
6495 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
6496 "check_repair, dir = %lu/%u, credits = %d, "
6497 "name = %.*s, ino = %llu: rc = %d\n",
6498 devname, dir->i_ino, dir->i_generation, credits,
6499 ent->oied_namelen, ent->oied_name,
6502 GOTO(out_inode, rc);
6505 if (obj->oo_hl_head != NULL) {
6506 hlock = osd_oti_get(env)->oti_hlock;
6507 /* "0" means exclusive lock for the whole directory.
6508 * We need to prevent others access such name entry
6509 * during the delete + insert. Neither HLOCK_ADD nor
6510 * HLOCK_DEL cannot guarantee the atomicity. */
6511 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
6513 down_write(&obj->oo_ext_idx_sem);
6516 if (obj->oo_hl_head != NULL) {
6517 hlock = osd_oti_get(env)->oti_hlock;
6518 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
6519 LDISKFS_HLOCK_LOOKUP);
6521 down_read(&obj->oo_ext_idx_sem);
6525 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6526 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
6527 *attr |= LUDA_IGNORE;
6532 /* For dotdot entry, if there is not enough space to hold the
6533 * FID-in-dirent, just keep them there. It only happens when the
6534 * device upgraded from 1.8 or restored from MDT file-level backup.
6535 * For the whole directory, only dotdot entry have no FID-in-dirent
6536 * and needs to get FID from LMA when readdir, it will not affect the
6537 * performance much. */
6538 if (dotdot && !osd_dotdot_has_space(de)) {
6539 *attr |= LUDA_UNKNOWN;
6545 if (lu_fid_eq(fid, &lma->lma_self_fid))
6548 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
6549 struct lu_fid *tfid = &lma->lma_self_fid;
6551 if (likely(dotdot &&
6552 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
6553 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
6554 /* It must be REMOTE_PARENT_DIR and as the
6555 * 'dotdot' entry of remote directory */
6556 *attr |= LUDA_IGNORE;
6558 CDEBUG(D_LFSCK, "%s: expect remote agent "
6559 "parent directory, but got %.*s under "
6560 "dir = %lu/%u with the FID "DFID"\n",
6561 devname, ent->oied_namelen,
6562 ent->oied_name, dir->i_ino,
6563 dir->i_generation, PFID(tfid));
6565 *attr |= LUDA_UNKNOWN;
6572 if (!fid_is_zero(fid)) {
6573 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
6575 if (rc == -ENOENT ||
6577 !(dev->od_scrub.os_scrub.os_file.sf_flags & SF_UPGRADE))) {
6578 /* linkEA does not recognize the dirent entry,
6579 * it may because the dirent entry corruption
6580 * and points to other's inode. */
6581 CDEBUG(D_LFSCK, "%s: the target inode does not "
6582 "recognize the dirent, dir = %lu/%u, "
6583 " name = %.*s, ino = %llu, "
6584 DFID": rc = %d\n", devname, dir->i_ino,
6585 dir->i_generation, ent->oied_namelen,
6586 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6587 *attr |= LUDA_UNKNOWN;
6592 if (rc && rc != -ENODATA) {
6593 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
6594 "dir = %lu/%u, name = %.*s, ino = %llu, "
6595 DFID": rc = %d\n", devname, dir->i_ino,
6596 dir->i_generation, ent->oied_namelen,
6597 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6598 *attr |= LUDA_UNKNOWN;
6605 /* linkEA recognizes the dirent entry, the FID-in-LMA is
6606 * valid, trusted, in spite of fid_is_sane(fid) or not. */
6607 if (*attr & LUDA_VERIFY_DRYRUN) {
6608 *fid = lma->lma_self_fid;
6609 *attr |= LUDA_REPAIR;
6616 dev->od_dirent_journal = 1;
6617 if (hlock != NULL) {
6618 ldiskfs_htree_unlock(hlock);
6621 up_read(&obj->oo_ext_idx_sem);
6627 *fid = lma->lma_self_fid;
6629 /* Update or append the FID-in-dirent. */
6630 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6631 bh, de, hlock, dotdot);
6633 *attr |= LUDA_REPAIR;
6635 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
6636 "the dirent, dir = %lu/%u, name = %.*s, "
6637 "ino = %llu, "DFID": rc = %d\n",
6638 devname, dir->i_ino, dir->i_generation,
6639 ent->oied_namelen, ent->oied_name,
6640 ent->oied_ino, PFID(fid), rc);
6642 /* lma is NULL, trust the FID-in-dirent if it is valid. */
6643 if (*attr & LUDA_VERIFY_DRYRUN) {
6644 if (fid_is_sane(fid)) {
6645 *attr |= LUDA_REPAIR;
6646 } else if (dev->od_index == 0) {
6647 lu_igif_build(fid, inode->i_ino,
6648 inode->i_generation);
6649 *attr |= LUDA_UPGRADE;
6657 dev->od_dirent_journal = 1;
6658 if (hlock != NULL) {
6659 ldiskfs_htree_unlock(hlock);
6662 up_read(&obj->oo_ext_idx_sem);
6669 if (unlikely(fid_is_sane(fid))) {
6670 /* FID-in-dirent exists, but FID-in-LMA is lost.
6671 * Trust the FID-in-dirent, and add FID-in-LMA. */
6672 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
6674 *attr |= LUDA_REPAIR;
6676 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
6677 "update dirent, dir = %lu/%u, "
6678 "name = %.*s, ino = %llu, "
6680 devname, dir->i_ino, dir->i_generation,
6681 ent->oied_namelen, ent->oied_name,
6682 ent->oied_ino, PFID(fid), rc);
6683 } else if (dev->od_index == 0) {
6684 lu_igif_build(fid, inode->i_ino, inode->i_generation);
6685 /* It is probably IGIF object. Only aappend the
6686 * FID-in-dirent. OI scrub will process FID-in-LMA. */
6687 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6688 bh, de, hlock, dotdot);
6690 *attr |= LUDA_UPGRADE;
6692 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
6693 "after the dirent, dir = %lu/%u, "
6694 "name = %.*s, ino = %llu, "
6696 devname, dir->i_ino, dir->i_generation,
6697 ent->oied_namelen, ent->oied_name,
6698 ent->oied_ino, PFID(fid), rc);
6707 if (hlock != NULL) {
6708 ldiskfs_htree_unlock(hlock);
6710 if (dev->od_dirent_journal != 0)
6711 up_write(&obj->oo_ext_idx_sem);
6713 up_read(&obj->oo_ext_idx_sem);
6717 ldiskfs_journal_stop(jh);
6721 if (rc >= 0 && !dirty)
6722 dev->od_dirent_journal = 0;
6728 * Returns the value at current position from iterator's in memory structure.
6730 * \param di struct osd_it_ea, iterator's in memory structure
6731 * \param attr attr requested for dirent.
6732 * \param lde lustre dirent
6734 * \retval 0 no error and \param lde has correct lustre dirent.
6735 * \retval -ve on error
6737 static inline int osd_it_ea_rec(const struct lu_env *env,
6738 const struct dt_it *di,
6739 struct dt_rec *dtrec, __u32 attr)
6741 struct osd_it_ea *it = (struct osd_it_ea *)di;
6742 struct osd_object *obj = it->oie_obj;
6743 struct osd_device *dev = osd_obj2dev(obj);
6744 struct osd_thread_info *oti = osd_oti_get(env);
6745 struct osd_inode_id *id = &oti->oti_id;
6746 struct lu_fid *fid = &it->oie_dirent->oied_fid;
6747 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6748 __u32 ino = it->oie_dirent->oied_ino;
6752 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
6754 if (attr & LUDA_VERIFY) {
6755 if (unlikely(ino == osd_remote_parent_ino(dev))) {
6756 attr |= LUDA_IGNORE;
6757 /* If the parent is on remote MDT, and there
6758 * is no FID-in-dirent, then we have to get
6759 * the parent FID from the linkEA. */
6760 if (!fid_is_sane(fid) &&
6761 it->oie_dirent->oied_namelen == 2 &&
6762 it->oie_dirent->oied_name[0] == '.' &&
6763 it->oie_dirent->oied_name[1] == '.')
6764 osd_get_pfid_from_linkea(env, obj, fid);
6766 rc = osd_dirent_check_repair(env, obj, it, fid, id,
6770 if (!fid_is_sane(fid))
6771 attr |= LUDA_UNKNOWN;
6773 attr &= ~LU_DIRENT_ATTRS_MASK;
6774 if (!fid_is_sane(fid)) {
6775 bool is_dotdot = false;
6776 if (it->oie_dirent->oied_namelen == 2 &&
6777 it->oie_dirent->oied_name[0] == '.' &&
6778 it->oie_dirent->oied_name[1] == '.')
6780 /* If the parent is on remote MDT, and there
6781 * is no FID-in-dirent, then we have to get
6782 * the parent FID from the linkEA. */
6783 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
6784 rc = osd_get_pfid_from_linkea(env, obj, fid);
6786 if (is_dotdot == false &&
6787 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
6790 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6793 osd_id_gen(id, ino, OSD_OII_NOGEN);
6797 /* Pack the entry anyway, at least the offset is right. */
6798 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
6799 it->oie_dirent->oied_name,
6800 it->oie_dirent->oied_namelen,
6801 it->oie_dirent->oied_type, attr);
6806 if (osd_remote_fid(env, dev, fid))
6809 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
6810 osd_add_oi_cache(oti, dev, id, fid);
6812 RETURN(rc > 0 ? 0 : rc);
6816 * Returns the record size size at current position.
6818 * This function will return record(lu_dirent) size in bytes.
6820 * \param[in] env execution environment
6821 * \param[in] di iterator's in memory structure
6822 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
6823 * calculate the lu_dirent size.
6825 * \retval record size(in bytes & in memory) of the current lu_dirent
6828 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
6831 struct osd_it_ea *it = (struct osd_it_ea *)di;
6833 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
6837 * Returns a cookie for current position of the iterator head, so that
6838 * user can use this cookie to load/start the iterator next time.
6840 * \param di iterator's in memory structure
6842 * \retval cookie for current position, on success
6844 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
6846 struct osd_it_ea *it = (struct osd_it_ea *)di;
6848 return it->oie_dirent->oied_off;
6852 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6853 * to load a directory entry at a time and stored it i inn,
6854 * in iterator's in-memory data structure.
6856 * \param di struct osd_it_ea, iterator's in memory structure
6858 * \retval +ve on success
6859 * \retval -ve on error
6861 static int osd_it_ea_load(const struct lu_env *env,
6862 const struct dt_it *di, __u64 hash)
6864 struct osd_it_ea *it = (struct osd_it_ea *)di;
6868 it->oie_file.f_pos = hash;
6870 rc = osd_ldiskfs_it_fill(env, di);
6881 * Index lookup function for interoperability mode (b11826).
6883 * \param key, key i.e. file name to be searched
6885 * \retval +ve, on success
6886 * \retval -ve, on error
6888 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6889 struct dt_rec *rec, const struct dt_key *key)
6891 struct osd_object *obj = osd_dt_obj(dt);
6896 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6897 LINVRNT(osd_invariant(obj));
6899 rc = osd_ea_lookup_rec(env, obj, rec, key);
6906 * Index and Iterator operations for interoperability
6907 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6909 static const struct dt_index_operations osd_index_ea_ops = {
6910 .dio_lookup = osd_index_ea_lookup,
6911 .dio_declare_insert = osd_index_declare_ea_insert,
6912 .dio_insert = osd_index_ea_insert,
6913 .dio_declare_delete = osd_index_declare_ea_delete,
6914 .dio_delete = osd_index_ea_delete,
6916 .init = osd_it_ea_init,
6917 .fini = osd_it_ea_fini,
6918 .get = osd_it_ea_get,
6919 .put = osd_it_ea_put,
6920 .next = osd_it_ea_next,
6921 .key = osd_it_ea_key,
6922 .key_size = osd_it_ea_key_size,
6923 .rec = osd_it_ea_rec,
6924 .rec_size = osd_it_ea_rec_size,
6925 .store = osd_it_ea_store,
6926 .load = osd_it_ea_load
6930 static void *osd_key_init(const struct lu_context *ctx,
6931 struct lu_context_key *key)
6933 struct osd_thread_info *info;
6935 OBD_ALLOC_PTR(info);
6937 return ERR_PTR(-ENOMEM);
6939 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6940 if (info->oti_it_ea_buf == NULL)
6943 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6945 info->oti_hlock = ldiskfs_htree_lock_alloc();
6946 if (info->oti_hlock == NULL)
6952 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6955 return ERR_PTR(-ENOMEM);
6958 static void osd_key_fini(const struct lu_context *ctx,
6959 struct lu_context_key *key, void* data)
6961 struct osd_thread_info *info = data;
6962 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6963 struct osd_idmap_cache *idc = info->oti_ins_cache;
6965 if (info->oti_inode != NULL)
6967 if (info->oti_hlock != NULL)
6968 ldiskfs_htree_lock_free(info->oti_hlock);
6969 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6970 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6971 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6972 lu_buf_free(&info->oti_big_buf);
6974 LASSERT(info->oti_ins_cache_size > 0);
6975 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6976 info->oti_ins_cache = NULL;
6977 info->oti_ins_cache_size = 0;
6982 static void osd_key_exit(const struct lu_context *ctx,
6983 struct lu_context_key *key, void *data)
6985 struct osd_thread_info *info = data;
6987 LASSERT(info->oti_r_locks == 0);
6988 LASSERT(info->oti_w_locks == 0);
6989 LASSERT(info->oti_txns == 0);
6992 /* type constructor/destructor: osd_type_init, osd_type_fini */
6993 LU_TYPE_INIT_FINI(osd, &osd_key);
6995 struct lu_context_key osd_key = {
6996 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6997 .lct_init = osd_key_init,
6998 .lct_fini = osd_key_fini,
6999 .lct_exit = osd_key_exit
7003 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
7004 const char *name, struct lu_device *next)
7006 struct osd_device *osd = osd_dev(d);
7008 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
7009 >= sizeof(osd->od_svname))
7011 return osd_procfs_init(osd, name);
7014 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
7016 struct seq_server_site *ss = osd_seq_site(osd);
7020 if (osd->od_is_ost || osd->od_cl_seq != NULL)
7023 if (unlikely(ss == NULL))
7026 OBD_ALLOC_PTR(osd->od_cl_seq);
7027 if (osd->od_cl_seq == NULL)
7030 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
7031 osd->od_svname, ss->ss_server_seq);
7033 OBD_FREE_PTR(osd->od_cl_seq);
7034 osd->od_cl_seq = NULL;
7038 if (ss->ss_node_id == 0) {
7039 /* If the OSD on the sequence controller(MDT0), then allocate
7040 * sequence here, otherwise allocate sequence after connected
7041 * to MDT0 (see mdt_register_lwp_callback()). */
7042 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
7043 &osd->od_cl_seq->lcs_space, env);
7049 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
7051 if (osd->od_cl_seq == NULL)
7054 seq_client_fini(osd->od_cl_seq);
7055 OBD_FREE_PTR(osd->od_cl_seq);
7056 osd->od_cl_seq = NULL;
7059 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
7063 /* shutdown quota slave instance associated with the device */
7064 if (o->od_quota_slave != NULL) {
7065 struct qsd_instance *qsd = o->od_quota_slave;
7067 o->od_quota_slave = NULL;
7071 osd_fid_fini(env, o);
7072 osd_scrub_cleanup(env, o);
7077 static void osd_umount(const struct lu_env *env, struct osd_device *o)
7081 if (o->od_mnt != NULL) {
7082 shrink_dcache_sb(osd_sb(o));
7083 osd_sync(env, &o->od_dt_dev);
7092 static int osd_mount(const struct lu_env *env,
7093 struct osd_device *o, struct lustre_cfg *cfg)
7095 const char *name = lustre_cfg_string(cfg, 0);
7096 const char *dev = lustre_cfg_string(cfg, 1);
7098 unsigned long page, s_flags, lmd_flags = 0;
7099 struct page *__page;
7100 struct file_system_type *type;
7101 char *options = NULL;
7103 struct osd_thread_info *info = osd_oti_get(env);
7104 struct lu_fid *fid = &info->oti_fid;
7105 struct inode *inode;
7106 int rc = 0, force_over_512tb = 0;
7109 if (o->od_mnt != NULL)
7112 if (strlen(dev) >= sizeof(o->od_mntdev))
7114 strcpy(o->od_mntdev, dev);
7116 str = lustre_cfg_string(cfg, 2);
7117 s_flags = simple_strtoul(str, NULL, 0);
7118 str = strstr(str, ":");
7120 lmd_flags = simple_strtoul(str + 1, NULL, 0);
7121 opts = lustre_cfg_string(cfg, 3);
7123 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
7124 CERROR("%s: device %s extents feature is not guaranteed to "
7125 "work on big-endian systems. Use \"bigendian_extents\" "
7126 "mount option to override.\n", name, dev);
7130 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7131 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
7132 CWARN("force_over_128tb option is deprecated. "
7133 "Filesystems less than 512TB can be created without any "
7134 "force options. Use force_over_512tb option for "
7135 "filesystems greater than 512TB.\n");
7138 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
7139 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
7140 CWARN("force_over_256tb option is deprecated. "
7141 "Filesystems less than 512TB can be created without any "
7142 "force options. Use force_over_512tb option for "
7143 "filesystems greater than 512TB.\n");
7147 if (opts != NULL && strstr(opts, "force_over_512tb") != NULL)
7148 force_over_512tb = 1;
7150 __page = alloc_page(GFP_KERNEL);
7152 GOTO(out, rc = -ENOMEM);
7153 page = (unsigned long)page_address(__page);
7154 options = (char *)page;
7157 /* strip out the options for back compatiblity */
7158 static char *sout[] = {
7165 /* strip out option we processed in osd */
7166 "bigendian_extents",
7172 strcat(options, opts);
7173 for (rc = 0, str = options; sout[rc]; ) {
7174 char *op = strstr(str, sout[rc]);
7180 if (op == options || *(op - 1) == ',') {
7181 str = op + strlen(sout[rc]);
7182 if (*str == ',' || *str == '\0') {
7183 *str == ',' ? str++ : str;
7184 memmove(op, str, strlen(str) + 1);
7187 for (str = op; *str != ',' && *str != '\0'; str++)
7191 strncat(options, "user_xattr,acl", 14);
7194 /* Glom up mount options */
7195 if (*options != '\0')
7196 strcat(options, ",");
7197 strlcat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
7199 type = get_fs_type("ldiskfs");
7201 CERROR("%s: cannot find ldiskfs module\n", name);
7202 GOTO(out, rc = -ENODEV);
7205 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
7206 module_put(type->owner);
7208 if (IS_ERR(o->od_mnt)) {
7209 rc = PTR_ERR(o->od_mnt);
7211 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
7215 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
7216 osd_sb(o)->s_blocksize_bits > 512ULL << 40 &&
7217 force_over_512tb == 0) {
7218 CERROR("%s: device %s LDISKFS does not support filesystems "
7219 "greater than 512TB and can cause data corruption. "
7220 "Use \"force_over_512tb\" mount option to override.\n",
7222 GOTO(out_mnt, rc = -EINVAL);
7225 if (lmd_flags & LMD_FLG_DEV_RDONLY) {
7226 if (priv_dev_set_rdonly) {
7227 priv_dev_set_rdonly(osd_sb(o)->s_bdev);
7228 o->od_dt_dev.dd_rdonly = 1;
7229 LCONSOLE_WARN("%s: set dev_rdonly on this device\n",
7232 LCONSOLE_WARN("%s: not support dev_rdonly on this device",
7235 GOTO(out_mnt, rc = -EOPNOTSUPP);
7237 } else if (priv_dev_check_rdonly &&
7238 priv_dev_check_rdonly(osd_sb(o)->s_bdev)) {
7239 CERROR("%s: underlying device %s is marked as "
7240 "read-only. Setup failed\n", name, dev);
7242 GOTO(out_mnt, rc = -EROFS);
7245 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
7246 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
7247 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
7248 GOTO(out_mnt, rc = -EINVAL);
7251 #ifdef LDISKFS_MOUNT_DIRDATA
7252 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
7253 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
7254 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
7255 else if (strstr(name, "MDT")) /* don't complain for MGT or OSTs */
7256 CWARN("%s: device %s was upgraded from Lustre-1.x without "
7257 "enabling the dirdata feature. If you do not want to "
7258 "downgrade to Lustre-1.x again, you can enable it via "
7259 "'tune2fs -O dirdata device'\n", name, dev);
7261 inode = osd_sb(o)->s_root->d_inode;
7262 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
7263 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
7265 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
7269 if (lmd_flags & LMD_FLG_NOSCRUB)
7270 o->od_auto_scrub_interval = AS_NEVER;
7280 __free_page(__page);
7285 static struct lu_device *osd_device_fini(const struct lu_env *env,
7286 struct lu_device *d)
7288 struct osd_device *o = osd_dev(d);
7291 osd_shutdown(env, o);
7293 osd_obj_map_fini(o);
7299 static int osd_device_init0(const struct lu_env *env,
7300 struct osd_device *o,
7301 struct lustre_cfg *cfg)
7303 struct lu_device *l = osd2lu_dev(o);
7304 struct osd_thread_info *info;
7308 /* if the module was re-loaded, env can loose its keys */
7309 rc = lu_env_refill((struct lu_env *) env);
7312 info = osd_oti_get(env);
7315 l->ld_ops = &osd_lu_ops;
7316 o->od_dt_dev.dd_ops = &osd_dt_ops;
7318 spin_lock_init(&o->od_osfs_lock);
7319 mutex_init(&o->od_otable_mutex);
7320 INIT_LIST_HEAD(&o->od_orphan_list);
7321 INIT_LIST_HEAD(&o->od_index_backup_list);
7322 INIT_LIST_HEAD(&o->od_index_restore_list);
7323 spin_lock_init(&o->od_lock);
7324 o->od_index_backup_policy = LIBP_NONE;
7326 o->od_read_cache = 1;
7327 o->od_writethrough_cache = 1;
7328 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
7329 o->od_auto_scrub_interval = AS_DEFAULT;
7331 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
7332 sizeof(o->od_svname));
7333 if (cplen >= sizeof(o->od_svname)) {
7338 o->od_index_backup_stop = 0;
7339 o->od_index = -1; /* -1 means index is invalid */
7340 rc = server_name2index(o->od_svname, &o->od_index, NULL);
7341 if (rc == LDD_F_SV_TYPE_OST)
7344 o->od_full_scrub_ratio = OFSR_DEFAULT;
7345 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
7346 rc = osd_mount(env, o, cfg);
7350 rc = osd_obj_map_init(env, o);
7354 rc = lu_site_init(&o->od_site, l);
7356 GOTO(out_compat, rc);
7357 o->od_site.ls_bottom_dev = l;
7359 rc = lu_site_init_finish(&o->od_site);
7363 INIT_LIST_HEAD(&o->od_ios_list);
7364 /* setup scrub, including OI files initialization */
7366 rc = osd_scrub_setup(env, o);
7371 rc = osd_procfs_init(o, o->od_svname);
7373 CERROR("%s: can't initialize procfs: rc = %d\n",
7375 GOTO(out_scrub, rc);
7378 LASSERT(l->ld_site->ls_linkage.next != NULL);
7379 LASSERT(l->ld_site->ls_linkage.prev != NULL);
7381 /* initialize quota slave instance */
7382 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
7384 if (IS_ERR(o->od_quota_slave)) {
7385 rc = PTR_ERR(o->od_quota_slave);
7386 o->od_quota_slave = NULL;
7387 GOTO(out_procfs, rc);
7395 osd_scrub_cleanup(env, o);
7397 lu_site_fini(&o->od_site);
7399 osd_obj_map_fini(o);
7406 static struct lu_device *osd_device_alloc(const struct lu_env *env,
7407 struct lu_device_type *t,
7408 struct lustre_cfg *cfg)
7410 struct osd_device *o;
7415 return ERR_PTR(-ENOMEM);
7417 rc = dt_device_init(&o->od_dt_dev, t);
7419 /* Because the ctx might be revived in dt_device_init,
7420 * refill the env here */
7421 lu_env_refill((struct lu_env *)env);
7422 rc = osd_device_init0(env, o, cfg);
7424 dt_device_fini(&o->od_dt_dev);
7427 if (unlikely(rc != 0))
7430 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
7433 static struct lu_device *osd_device_free(const struct lu_env *env,
7434 struct lu_device *d)
7436 struct osd_device *o = osd_dev(d);
7439 /* XXX: make osd top device in order to release reference */
7440 d->ld_site->ls_top_dev = d;
7441 lu_site_purge(env, d->ld_site, -1);
7442 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
7443 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
7444 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
7446 lu_site_fini(&o->od_site);
7447 dt_device_fini(&o->od_dt_dev);
7452 static int osd_process_config(const struct lu_env *env,
7453 struct lu_device *d, struct lustre_cfg *cfg)
7455 struct osd_device *o = osd_dev(d);
7459 switch (cfg->lcfg_command) {
7461 rc = osd_mount(env, o, cfg);
7464 /* For the case LCFG_PRE_CLEANUP is not called in advance,
7465 * that may happend if hit failure during mount process. */
7466 osd_index_backup(env, o, false);
7467 lu_dev_del_linkage(d->ld_site, d);
7468 rc = osd_shutdown(env, o);
7471 LASSERT(&o->od_dt_dev);
7472 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
7473 cfg, &o->od_dt_dev);
7474 if (rc > 0 || rc == -ENOSYS) {
7475 rc = class_process_proc_param(PARAM_OST,
7476 lprocfs_osd_obd_vars,
7477 cfg, &o->od_dt_dev);
7482 case LCFG_PRE_CLEANUP:
7483 osd_index_backup(env, o,
7484 o->od_index_backup_policy != LIBP_NONE);
7494 static int osd_recovery_complete(const struct lu_env *env,
7495 struct lu_device *d)
7497 struct osd_device *osd = osd_dev(d);
7501 if (osd->od_quota_slave == NULL)
7504 /* start qsd instance on recovery completion, this notifies the quota
7505 * slave code that we are about to process new requests now */
7506 rc = qsd_start(env, osd->od_quota_slave);
7511 * we use exports to track all osd users
7513 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
7514 struct obd_device *obd, struct obd_uuid *cluuid,
7515 struct obd_connect_data *data, void *localdata)
7517 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7518 struct lustre_handle conn;
7522 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
7524 rc = class_connect(&conn, obd, cluuid);
7528 *exp = class_conn2export(&conn);
7530 spin_lock(&osd->od_osfs_lock);
7532 spin_unlock(&osd->od_osfs_lock);
7538 * once last export (we don't count self-export) disappeared
7539 * osd can be released
7541 static int osd_obd_disconnect(struct obd_export *exp)
7543 struct obd_device *obd = exp->exp_obd;
7544 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7545 int rc, release = 0;
7548 /* Only disconnect the underlying layers on the final disconnect. */
7549 spin_lock(&osd->od_osfs_lock);
7551 if (osd->od_connects == 0)
7553 spin_unlock(&osd->od_osfs_lock);
7555 rc = class_disconnect(exp); /* bz 9811 */
7557 if (rc == 0 && release)
7558 class_manual_cleanup(obd);
7562 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
7563 struct lu_device *dev)
7565 struct osd_device *osd = osd_dev(dev);
7566 struct lr_server_data *lsd =
7567 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
7571 if (osd->od_quota_slave != NULL) {
7572 /* set up quota slave objects */
7573 result = qsd_prepare(env, osd->od_quota_slave);
7578 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
7579 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
7580 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
7581 osd->od_index_in_idif = 1;
7583 osd->od_index_in_idif = 0;
7584 result = osd_register_proc_index_in_idif(osd);
7589 osd->od_index_in_idif = 1;
7593 result = osd_fid_init(env, osd);
7598 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
7599 struct lu_fid *fid, struct md_op_data *op_data)
7601 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
7603 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
7606 static const struct lu_object_operations osd_lu_obj_ops = {
7607 .loo_object_init = osd_object_init,
7608 .loo_object_delete = osd_object_delete,
7609 .loo_object_release = osd_object_release,
7610 .loo_object_free = osd_object_free,
7611 .loo_object_print = osd_object_print,
7612 .loo_object_invariant = osd_object_invariant
7615 const struct lu_device_operations osd_lu_ops = {
7616 .ldo_object_alloc = osd_object_alloc,
7617 .ldo_process_config = osd_process_config,
7618 .ldo_recovery_complete = osd_recovery_complete,
7619 .ldo_prepare = osd_prepare,
7622 static const struct lu_device_type_operations osd_device_type_ops = {
7623 .ldto_init = osd_type_init,
7624 .ldto_fini = osd_type_fini,
7626 .ldto_start = osd_type_start,
7627 .ldto_stop = osd_type_stop,
7629 .ldto_device_alloc = osd_device_alloc,
7630 .ldto_device_free = osd_device_free,
7632 .ldto_device_init = osd_device_init,
7633 .ldto_device_fini = osd_device_fini
7636 static struct lu_device_type osd_device_type = {
7637 .ldt_tags = LU_DEVICE_DT,
7638 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
7639 .ldt_ops = &osd_device_type_ops,
7640 .ldt_ctx_tags = LCT_LOCAL,
7643 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
7645 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7646 struct super_block *sb = osd_sb(osd);
7648 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
7652 * lprocfs legacy support.
7654 static struct obd_ops osd_obd_device_ops = {
7655 .o_owner = THIS_MODULE,
7656 .o_connect = osd_obd_connect,
7657 .o_disconnect = osd_obd_disconnect,
7658 .o_fid_alloc = osd_fid_alloc,
7659 .o_health_check = osd_health_check,
7662 static int __init osd_init(void)
7666 CLASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
7667 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
7668 /* please, try to keep osd_thread_info smaller than a page */
7669 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
7674 rc = lu_kmem_init(ldiskfs_caches);
7678 #ifdef CONFIG_KALLSYMS
7679 priv_dev_set_rdonly = (void *)kallsyms_lookup_name("dev_set_rdonly");
7680 priv_dev_check_rdonly = (void *)kallsyms_lookup_name("dev_check_rdonly");
7681 /* Clear readonly is unused at this time */
7682 /*priv_dev_clear_rdonly = (void *)kallsyms_lookup_name("dev_clear_rdonly");*/
7685 rc = class_register_type(&osd_obd_device_ops, NULL, true,
7686 lprocfs_osd_module_vars,
7687 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
7689 lu_kmem_fini(ldiskfs_caches);
7693 static void __exit osd_exit(void)
7695 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
7696 lu_kmem_fini(ldiskfs_caches);
7699 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7700 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
7701 MODULE_VERSION(LUSTRE_VERSION_STRING);
7702 MODULE_LICENSE("GPL");
7704 module_init(osd_init);
7705 module_exit(osd_exit);