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>
80 /* Maximum EA size is limited by LNET_MTU for remote objects */
81 #define OSD_MAX_EA_SIZE 1048364
84 module_param(ldiskfs_pdo, int, 0644);
85 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
87 int ldiskfs_track_declares_assert;
88 module_param(ldiskfs_track_declares_assert, int, 0644);
89 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
91 /* Slab to allocate dynlocks */
92 struct kmem_cache *dynlock_cachep;
94 /* Slab to allocate osd_it_ea */
95 struct kmem_cache *osd_itea_cachep;
97 static struct lu_kmem_descr ldiskfs_caches[] = {
99 .ckd_cache = &dynlock_cachep,
100 .ckd_name = "dynlock_cache",
101 .ckd_size = sizeof(struct dynlock_handle)
104 .ckd_cache = &osd_itea_cachep,
105 .ckd_name = "osd_itea_cache",
106 .ckd_size = sizeof(struct osd_it_ea)
113 static const char dot[] = ".";
114 static const char dotdot[] = "..";
116 static const struct lu_object_operations osd_lu_obj_ops;
117 static const struct dt_object_operations osd_obj_ops;
118 static const struct dt_object_operations osd_obj_otable_it_ops;
119 static const struct dt_index_operations osd_index_iam_ops;
120 static const struct dt_index_operations osd_index_ea_ops;
122 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
123 const struct lu_fid *fid);
124 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
125 struct osd_device *osd);
127 int osd_trans_declare_op2rb[] = {
128 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
129 [OSD_OT_PUNCH] = OSD_OT_MAX,
130 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
131 [OSD_OT_CREATE] = OSD_OT_DESTROY,
132 [OSD_OT_DESTROY] = OSD_OT_CREATE,
133 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
134 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
135 [OSD_OT_WRITE] = OSD_OT_WRITE,
136 [OSD_OT_INSERT] = OSD_OT_DELETE,
137 [OSD_OT_DELETE] = OSD_OT_INSERT,
138 [OSD_OT_QUOTA] = OSD_OT_MAX,
141 static int osd_has_index(const struct osd_object *obj)
143 return obj->oo_dt.do_index_ops != NULL;
146 static int osd_object_invariant(const struct lu_object *l)
148 return osd_invariant(osd_obj(l));
152 * Concurrency: doesn't matter
154 static int osd_is_write_locked(const struct lu_env *env, struct osd_object *o)
156 struct osd_thread_info *oti = osd_oti_get(env);
157 return oti->oti_w_locks > 0 && o->oo_owner == env;
161 * Concurrency: doesn't access mutable data
163 static int osd_root_get(const struct lu_env *env,
164 struct dt_device *dev, struct lu_fid *f)
166 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
171 * the following set of functions are used to maintain per-thread
172 * cache of FID->ino mapping. this mechanism is needed to resolve
173 * FID to inode at dt_insert() which in turn stores ino in the
174 * directory entries to keep ldiskfs compatible with ext[34].
175 * due to locking-originated restrictions we can't lookup ino
176 * using LU cache (deadlock is possible). lookup using OI is quite
177 * expensive. so instead we maintain this cache and methods like
178 * dt_create() fill it. so in the majority of cases dt_insert() is
179 * able to find needed mapping in lockless manner.
181 static struct osd_idmap_cache *
182 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
183 const struct lu_fid *fid)
185 struct osd_thread_info *oti = osd_oti_get(env);
186 struct osd_idmap_cache *idc = oti->oti_ins_cache;
188 for (i = 0; i < oti->oti_ins_cache_used; i++) {
189 if (!lu_fid_eq(&idc[i].oic_fid, fid))
191 if (idc[i].oic_dev != osd)
200 static struct osd_idmap_cache *
201 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
202 const struct lu_fid *fid)
204 struct osd_thread_info *oti = osd_oti_get(env);
205 struct osd_idmap_cache *idc;
208 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
209 i = oti->oti_ins_cache_size * 2;
211 i = OSD_INS_CACHE_SIZE;
212 OBD_ALLOC(idc, sizeof(*idc) * i);
214 return ERR_PTR(-ENOMEM);
215 if (oti->oti_ins_cache != NULL) {
216 memcpy(idc, oti->oti_ins_cache,
217 oti->oti_ins_cache_used * sizeof(*idc));
218 OBD_FREE(oti->oti_ins_cache,
219 oti->oti_ins_cache_used * sizeof(*idc));
221 oti->oti_ins_cache = idc;
222 oti->oti_ins_cache_size = i;
225 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
228 idc->oic_lid.oii_ino = 0;
229 idc->oic_lid.oii_gen = 0;
236 * lookup mapping for the given fid in the cache, initialize a
237 * new one if not found. the initialization checks whether the
238 * object is local or remote. for local objects, OI is used to
239 * learn ino/generation. the function is used when the caller
240 * has no information about the object, e.g. at dt_insert().
242 static struct osd_idmap_cache *
243 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
244 const struct lu_fid *fid)
246 struct osd_idmap_cache *idc;
249 idc = osd_idc_find(env, osd, fid);
250 LASSERT(!IS_ERR(idc));
254 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
255 osd->od_svname, PFID(fid));
257 /* new mapping is needed */
258 idc = osd_idc_add(env, osd, fid);
260 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
261 osd->od_svname, PFID(fid), PTR_ERR(idc));
266 rc = osd_remote_fid(env, osd, fid);
267 if (unlikely(rc < 0))
271 /* the object is local, lookup in OI */
272 /* XXX: probably cheaper to lookup in LU first? */
273 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
275 if (unlikely(rc < 0)) {
276 CERROR("can't lookup: rc = %d\n", rc);
280 /* the object is remote */
288 * lookup mapping for given FID and fill it from the given object.
289 * the object is lolcal by definition.
291 static int osd_idc_find_and_init(const struct lu_env *env,
292 struct osd_device *osd,
293 struct osd_object *obj)
295 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
296 struct osd_idmap_cache *idc;
298 idc = osd_idc_find(env, osd, fid);
299 LASSERT(!IS_ERR(idc));
301 if (obj->oo_inode == NULL)
303 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
304 LASSERT(idc->oic_lid.oii_ino == 0);
305 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
306 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
311 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
312 osd->od_svname, PFID(fid));
314 /* new mapping is needed */
315 idc = osd_idc_add(env, osd, fid);
317 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
318 osd->od_svname, PFID(fid), PTR_ERR(idc));
322 if (obj->oo_inode != NULL) {
323 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
324 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
330 * OSD object methods.
334 * Concurrency: no concurrent access is possible that early in object
337 static struct lu_object *osd_object_alloc(const struct lu_env *env,
338 const struct lu_object_header *hdr,
341 struct osd_object *mo;
346 struct lu_object_header *h;
347 struct osd_device *o = osd_dev(d);
349 l = &mo->oo_dt.do_lu;
350 if (unlikely(o->od_in_init)) {
357 lu_object_header_init(h);
358 lu_object_init(l, h, d);
359 lu_object_add_top(h, l);
362 dt_object_init(&mo->oo_dt, NULL, d);
363 mo->oo_header = NULL;
366 mo->oo_dt.do_ops = &osd_obj_ops;
367 l->lo_ops = &osd_lu_obj_ops;
368 init_rwsem(&mo->oo_sem);
369 init_rwsem(&mo->oo_ext_idx_sem);
370 spin_lock_init(&mo->oo_guard);
371 INIT_LIST_HEAD(&mo->oo_xattr_list);
378 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
379 struct dentry *dentry, struct lustre_ost_attrs *loa)
383 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
384 (void *)loa, sizeof(*loa));
386 struct lustre_mdt_attrs *lma = &loa->loa_lma;
388 if (rc < sizeof(*lma))
392 lustre_loa_swab(loa, true);
393 /* Check LMA compatibility */
394 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
395 CWARN("%s: unsupported incompat LMA feature(s) %#x "
396 "for fid = "DFID", ino = %lu\n",
398 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
399 PFID(&lma->lma_self_fid), inode->i_ino);
402 } else if (rc == 0) {
410 * retrieve object from backend ext fs.
412 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
413 struct osd_inode_id *id)
416 struct inode *inode = NULL;
418 /* if we look for an inode withing a running
419 * transaction, then we risk to deadlock */
420 /* osd_dirent_check_repair() breaks this */
421 /*LASSERT(current->journal_info == NULL);*/
423 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
425 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
426 id->oii_ino, PTR_ERR(inode));
427 } else if (id->oii_gen != OSD_OII_NOGEN &&
428 inode->i_generation != id->oii_gen) {
429 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
430 "i_generation = %u\n",
431 id->oii_ino, id->oii_gen, inode->i_generation);
433 inode = ERR_PTR(-ESTALE);
434 } else if (inode->i_nlink == 0) {
435 /* due to parallel readdir and unlink,
436 * we can have dead inode here. */
437 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
439 inode = ERR_PTR(-ESTALE);
440 } else if (is_bad_inode(inode)) {
441 CWARN("%s: bad inode: ino = %u\n",
442 osd_dev2name(dev), id->oii_ino);
444 inode = ERR_PTR(-ENOENT);
445 } else if ((rc = osd_attach_jinode(inode))) {
449 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
450 if (id->oii_gen == OSD_OII_NOGEN)
451 osd_id_gen(id, inode->i_ino, inode->i_generation);
453 /* Do not update file c/mtime in ldiskfs.
454 * NB: we don't have any lock to protect this because we don't
455 * have reference on osd_object now, but contention with
456 * another lookup + attr_set can't happen in the tiny window
457 * between if (...) and set S_NOCMTIME. */
458 if (!(inode->i_flags & S_NOCMTIME))
459 inode->i_flags |= S_NOCMTIME;
464 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
465 handle_t *handle, struct dentry *child,
466 struct inode *inode, struct htree_lock *hlock)
470 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
471 if (rc == -ENOBUFS || rc == -ENOSPC) {
472 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
473 struct inode *parent = child->d_parent->d_inode;
474 struct lu_fid *fid = NULL;
476 rc2 = osd_get_lma(info, parent, child->d_parent, loa);
478 fid = &loa->loa_lma.lma_self_fid;
479 } else if (rc2 == -ENODATA) {
480 if (unlikely(parent == inode->i_sb->s_root->d_inode)) {
481 fid = &info->oti_fid3;
482 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
483 } else if (!osd->od_is_ost && osd->od_index == 0) {
484 fid = &info->oti_fid3;
485 lu_igif_build(fid, parent->i_ino,
486 parent->i_generation);
491 CWARN("%s: directory (inode: %lu, FID: "DFID") %s "
492 "maximum entry limit\n",
493 osd_name(osd), parent->i_ino, PFID(fid),
494 rc == -ENOSPC ? "has reached" : "is approaching");
496 CWARN("%s: directory (inode: %lu, FID: unknown) %s "
497 "maximum entry limit\n",
498 osd_name(osd), parent->i_ino,
499 rc == -ENOSPC ? "has reached" : "is approaching");
501 /* ignore such error now */
511 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
512 struct osd_inode_id *id, struct lu_fid *fid)
514 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
518 inode = osd_iget(info, dev, id);
522 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
524 *fid = loa->loa_lma.lma_self_fid;
525 } else if (rc == -ENODATA) {
526 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
527 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
529 lu_igif_build(fid, inode->i_ino, inode->i_generation);
537 static struct inode *osd_iget_check(struct osd_thread_info *info,
538 struct osd_device *dev,
539 const struct lu_fid *fid,
540 struct osd_inode_id *id,
547 /* The cached OI mapping is trustable. If we cannot locate the inode
548 * via the cached OI mapping, then return the failure to the caller
549 * directly without further OI checking. */
552 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
555 if (!trusted && (rc == -ENOENT || rc == -ESTALE))
558 CDEBUG(D_INODE, "no inode for FID: "DFID", ino = %u, rc = %d\n",
559 PFID(fid), id->oii_ino, rc);
563 if (is_bad_inode(inode)) {
568 CDEBUG(D_INODE, "bad inode for FID: "DFID", ino = %u\n",
569 PFID(fid), id->oii_ino);
573 if (id->oii_gen != OSD_OII_NOGEN &&
574 inode->i_generation != id->oii_gen) {
579 CDEBUG(D_INODE, "unmatched inode for FID: "DFID", ino = %u, "
580 "oii_gen = %u, i_generation = %u\n", PFID(fid),
581 id->oii_ino, id->oii_gen, inode->i_generation);
585 if (inode->i_nlink == 0) {
590 CDEBUG(D_INODE, "stale inode for FID: "DFID", ino = %u\n",
591 PFID(fid), id->oii_ino);
595 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
599 __u32 saved_ino = id->oii_ino;
600 __u32 saved_gen = id->oii_gen;
603 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
605 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
606 /* XXX: There are four possible cases:
608 * Backup/restore caused the OI invalid.
610 * Someone unlinked the object but NOT removed
611 * the OI mapping, such as mount target device
612 * as ldiskfs, and modify something directly.
614 * Someone just removed the object between the
615 * former oi_lookup and the iget. It is normal.
616 * 4. Other failure cases.
618 * Generally, when the device is mounted, it will
619 * auto check whether the system is restored from
620 * file-level backup or not. We trust such detect
621 * to distinguish the 1st case from the 2nd case:
622 * if the OI files are consistent but may contain
623 * stale OI mappings because of case 2, if iget()
624 * returns -ENOENT or -ESTALE, then it should be
627 /* If the OI mapping was in OI file before the
628 * osd_iget_check(), but now, it is disappear,
629 * then it must be removed by race. That is a
630 * normal race case. */
633 /* It is the OI scrub updated the OI mapping by race.
634 * The new OI mapping must be valid. */
635 if (saved_ino != id->oii_ino ||
636 (saved_gen != id->oii_gen && saved_gen != OSD_OII_NOGEN)) {
645 if (dev->od_scrub.os_scrub.os_file.sf_flags &
647 /* It still can be the case 2, but we cannot
648 * distinguish it from the case 1. So return
649 * -EREMCHG to block current operation until
650 * OI scrub rebuilt the OI mappings. */
658 if (inode->i_generation == id->oii_gen)
663 if (id->oii_gen == OSD_OII_NOGEN)
664 osd_id_gen(id, inode->i_ino, inode->i_generation);
666 /* Do not update file c/mtime in ldiskfs.
667 * NB: we don't have any lock to protect this because we don't
668 * have reference on osd_object now, but contention with
669 * another lookup + attr_set can't happen in the tiny window
670 * between if (...) and set S_NOCMTIME. */
671 if (!(inode->i_flags & S_NOCMTIME))
672 inode->i_flags |= S_NOCMTIME;
689 * \retval +v: new filter_fid, does not contain self-fid
690 * \retval 0: filter_fid_old, contains self-fid
691 * \retval -v: other failure cases
693 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
694 struct dentry *dentry, struct lu_fid *fid)
696 struct filter_fid_old *ff = &info->oti_ff;
697 struct ost_id *ostid = &info->oti_ostid;
700 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
701 if (rc == sizeof(*ff)) {
703 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
704 rc = ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
706 * XXX: use 0 as the index for compatibility, the caller will
707 * handle index related issues when necessary.
710 ostid_to_fid(fid, ostid, 0);
711 } else if (rc == sizeof(struct filter_fid)) {
713 } else if (rc >= 0) {
720 static int osd_lma_self_repair(struct osd_thread_info *info,
721 struct osd_device *osd, struct inode *inode,
722 const struct lu_fid *fid, __u32 compat)
727 LASSERT(current->journal_info == NULL);
729 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
730 osd_dto_credits_noquota[DTO_XATTR_SET]);
733 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
738 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
740 CWARN("%s: cannot self repair the LMA: rc = %d\n",
742 ldiskfs_journal_stop(jh);
746 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
748 struct osd_thread_info *info = osd_oti_get(env);
749 struct osd_device *osd = osd_obj2dev(obj);
750 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
751 struct lustre_mdt_attrs *lma = &loa->loa_lma;
752 struct inode *inode = obj->oo_inode;
753 struct dentry *dentry = &info->oti_obj_dentry;
754 struct lu_fid *fid = NULL;
755 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
759 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
760 (void *)loa, sizeof(*loa));
761 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
762 fid = &lma->lma_self_fid;
763 rc = osd_get_idif(info, inode, dentry, fid);
764 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
765 /* For the given OST-object, if it has neither LMA nor
766 * FID in XATTR_NAME_FID, then the given FID (which is
767 * contained in the @obj, from client RPC for locating
768 * the OST-object) is trusted. We use it to generate
770 osd_lma_self_repair(info, osd, inode, rfid,
781 lustre_lma_swab(lma);
782 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
783 (CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT) &&
784 S_ISREG(inode->i_mode)))) {
785 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
786 "fid = "DFID", ino = %lu\n", osd_name(osd),
787 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
788 PFID(rfid), inode->i_ino);
791 fid = &lma->lma_self_fid;
792 if (lma->lma_compat & LMAC_STRIPE_INFO &&
794 obj->oo_pfid_in_lma = 1;
795 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
797 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
801 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
802 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
803 struct ost_id *oi = &info->oti_ostid;
804 struct lu_fid *fid1 = &info->oti_fid3;
805 __u32 idx = fid_idif_ost_idx(rfid);
807 /* For old IDIF, the OST index is not part of the IDIF,
808 * Means that different OSTs may have the same IDIFs.
809 * Under such case, we need to make some compatible
810 * check to make sure to trigger OI scrub properly. */
811 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
812 /* Given @rfid is new, LMA is old. */
813 fid_to_ostid(fid, oi);
814 ostid_to_fid(fid1, oi, idx);
815 if (lu_fid_eq(fid1, rfid)) {
816 if (osd->od_index_in_idif)
817 osd_lma_self_repair(info, osd,
831 struct osd_check_lmv_buf {
832 #ifdef HAVE_DIR_CONTEXT
833 /* please keep it as first member */
834 struct dir_context ctx;
836 struct osd_thread_info *oclb_info;
837 struct osd_device *oclb_dev;
838 struct osd_idmap_cache *oclb_oic;
844 * It is called internally by ->readdir() to filter out the
845 * local slave object's FID of the striped directory.
847 * \retval 1 found the local slave's FID
848 * \retval 0 continue to check next item
849 * \retval -ve for failure
851 #ifdef HAVE_FILLDIR_USE_CTX
852 static int osd_stripe_dir_filldir(struct dir_context *buf,
854 static int osd_stripe_dir_filldir(void *buf,
856 const char *name, int namelen,
857 loff_t offset, __u64 ino, unsigned d_type)
859 struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
860 struct osd_thread_info *oti = oclb->oclb_info;
861 struct lu_fid *fid = &oti->oti_fid3;
862 struct osd_inode_id *id = &oti->oti_id3;
863 struct osd_device *dev = oclb->oclb_dev;
864 struct osd_idmap_cache *oic = oclb->oclb_oic;
873 sscanf(name + 1, SFID, RFID(fid));
874 if (!fid_is_sane(fid))
877 if (osd_remote_fid(oti->oti_env, dev, fid))
880 osd_id_gen(id, ino, OSD_OII_NOGEN);
881 inode = osd_iget(oti, dev, id);
883 return PTR_ERR(inode);
886 osd_add_oi_cache(oti, dev, id, fid);
890 osd_oii_insert(dev, oic, true);
891 oclb->oclb_found = true;
896 /* When lookup item under striped directory, we need to locate the master
897 * MDT-object of the striped directory firstly, then the client will send
898 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
899 * and the item's name. If the system is restored from MDT file level backup,
900 * then before the OI scrub completely built the OI files, the OI mappings of
901 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
902 * not a problem for the master MDT-object. Because when locate the master
903 * MDT-object, we will do name based lookup (for the striped directory itself)
904 * firstly, during such process we can setup the correct OI mapping for the
905 * master MDT-object. But it will be trouble for the slave MDT-object. Because
906 * the client will not trigger name based lookup on the MDT to locate the slave
907 * MDT-object before locating item under the striped directory, then when
908 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
909 * is invalid and does not know what the right OI mapping is, then the MDT has
910 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
911 * the OI file, related OI mapping is unknown yet, please try again later. And
912 * then client will re-try the RPC again and again until related OI mapping has
913 * been updated. That is quite inefficient.
915 * To resolve above trouble, we will handle it as the following two cases:
917 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
918 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
919 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
920 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
921 * directly. Please check osd_fid_lookup().
923 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
924 * Under such case, during lookup the master MDT-object, we will lookup the
925 * slave MDT-object via readdir against the master MDT-object, because the
926 * slave MDT-objects information are stored as sub-directories with the name
927 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
928 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
929 * the correct OI mapping for the slave MDT-object. */
930 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
931 struct inode *inode, struct osd_idmap_cache *oic)
933 struct lu_buf *buf = &oti->oti_big_buf;
934 struct dentry *dentry = &oti->oti_obj_dentry;
935 struct file *filp = &oti->oti_file;
936 const struct file_operations *fops;
937 struct lmv_mds_md_v1 *lmv1;
938 struct osd_check_lmv_buf oclb = {
939 #ifdef HAVE_DIR_CONTEXT
940 .ctx.actor = osd_stripe_dir_filldir,
951 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, buf->lb_buf,
954 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, NULL, 0);
956 lu_buf_realloc(buf, rc);
957 if (buf->lb_buf == NULL)
958 GOTO(out, rc = -ENOMEM);
964 if (unlikely(rc == 0 || rc == -ENODATA))
970 if (unlikely(buf->lb_buf == NULL)) {
971 lu_buf_realloc(buf, rc);
972 if (buf->lb_buf == NULL)
973 GOTO(out, rc = -ENOMEM);
979 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
983 dentry->d_inode = inode;
984 dentry->d_sb = inode->i_sb;
986 filp->f_path.dentry = dentry;
987 filp->f_mode = FMODE_64BITHASH;
988 filp->f_mapping = inode->i_mapping;
990 filp->private_data = NULL;
991 set_file_inode(filp, inode);
995 #ifdef HAVE_DIR_CONTEXT
996 oclb.ctx.pos = filp->f_pos;
997 rc = fops->iterate(filp, &oclb.ctx);
998 filp->f_pos = oclb.ctx.pos;
1000 rc = fops->readdir(filp, &oclb, osd_stripe_dir_filldir);
1002 } while (rc >= 0 && oclb.oclb_items > 0 && !oclb.oclb_found &&
1003 filp->f_pos != LDISKFS_HTREE_EOF_64BIT);
1004 fops->release(inode, filp);
1008 CDEBUG(D_LFSCK, "%s: fail to check LMV EA, inode = %lu/%u,"
1009 DFID": rc = %d\n", osd_ino2name(inode),
1010 inode->i_ino, inode->i_generation,
1011 PFID(&oic->oic_fid), rc);
1018 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
1019 const struct lu_fid *fid,
1020 const struct lu_object_conf *conf)
1022 struct osd_thread_info *info;
1023 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1024 struct osd_device *dev;
1025 struct osd_idmap_cache *oic;
1026 struct osd_inode_id *id;
1027 struct inode *inode = NULL;
1028 struct lustre_scrub *scrub;
1029 struct scrub_file *sf;
1030 __u32 flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT | SS_AUTO_FULL;
1035 bool remote = false;
1036 bool trusted = true;
1037 bool updated = false;
1038 bool checked = false;
1041 LINVRNT(osd_invariant(obj));
1042 LASSERT(obj->oo_inode == NULL);
1043 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
1045 dev = osd_dev(ldev);
1046 scrub = &dev->od_scrub.os_scrub;
1047 sf = &scrub->os_file;
1048 info = osd_oti_get(env);
1050 oic = &info->oti_cache;
1052 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
1055 /* For the object is created as locking anchor, or for the object to
1056 * be created on disk. No need to osd_oi_lookup() at here because FID
1057 * shouldn't never be re-used, if it's really a duplicate FID from
1058 * unexpected reason, we should be able to detect it later by calling
1059 * do_create->osd_oi_insert(). */
1060 if (conf && conf->loc_flags & LOC_F_NEW)
1061 GOTO(out, result = 0);
1063 /* Search order: 1. per-thread cache. */
1064 if (lu_fid_eq(fid, &oic->oic_fid) && likely(oic->oic_dev == dev)) {
1070 if (!list_empty(&scrub->os_inconsistent_items)) {
1071 /* Search order: 2. OI scrub pending list. */
1072 result = osd_oii_lookup(dev, fid, id);
1077 /* The OI mapping in the OI file can be updated by the OI scrub
1078 * when we locate the inode via FID. So it may be not trustable. */
1081 /* Search order: 3. OI files. */
1082 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1083 if (result == -ENOENT) {
1084 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
1085 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1086 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
1088 GOTO(out, result = 0);
1097 obj->oo_inode = NULL;
1098 /* for later passes through checks, not true on first pass */
1099 if (!IS_ERR_OR_NULL(inode))
1102 inode = osd_iget_check(info, dev, fid, id, trusted);
1103 if (!IS_ERR(inode)) {
1104 obj->oo_inode = inode;
1112 result = PTR_ERR(inode);
1113 if (result == -ENOENT || result == -ESTALE)
1114 GOTO(out, result = 0);
1116 if (result != -EREMCHG)
1120 /* We still have chance to get the valid inode: for the
1121 * object which is referenced by remote name entry, the
1122 * object on the local MDT will be linked under the dir
1123 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1125 * We do not know whether the object for the given FID
1126 * is referenced by some remote name entry or not, and
1127 * especially for DNE II, a multiple-linked object may
1128 * have many name entries reside on many MDTs.
1130 * To simplify the operation, OSD will not distinguish
1131 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1132 * only happened for the RPC from other MDT during the
1133 * OI scrub, or for the client side RPC with FID only,
1134 * such as FID to path, or from old connected client. */
1136 rc1 = osd_lookup_in_remote_parent(info, dev, fid, id);
1140 flags |= SS_AUTO_PARTIAL;
1141 flags &= ~SS_AUTO_FULL;
1146 if (thread_is_running(&scrub->os_thread)) {
1147 if (scrub->os_partial_scan && !scrub->os_in_join)
1150 if (IS_ERR_OR_NULL(inode) || result)
1151 GOTO(out, result = -EINPROGRESS);
1154 LASSERT(obj->oo_inode == inode);
1156 osd_add_oi_cache(info, dev, id, fid);
1157 osd_oii_insert(dev, oic, true);
1161 if (dev->od_auto_scrub_interval == AS_NEVER) {
1163 GOTO(out, result = -EREMCHG);
1166 LASSERT(obj->oo_inode == inode);
1168 osd_add_oi_cache(info, dev, id, fid);
1173 rc1 = osd_scrub_start(env, dev, flags);
1174 LCONSOLE_WARN("%s: trigger OI scrub by RPC for the " DFID" with flags "
1175 "0x%x, rc = %d\n", osd_name(dev), PFID(fid), flags, rc1);
1176 if (rc1 && rc1 != -EALREADY)
1177 GOTO(out, result = -EREMCHG);
1179 if (IS_ERR_OR_NULL(inode) || result)
1180 GOTO(out, result = -EINPROGRESS);
1183 LASSERT(obj->oo_inode == inode);
1185 osd_add_oi_cache(info, dev, id, fid);
1186 osd_oii_insert(dev, oic, true);
1191 if (unlikely(obj->oo_header))
1194 result = osd_check_lma(env, obj);
1198 LASSERTF(id->oii_ino == inode->i_ino &&
1199 id->oii_gen == inode->i_generation,
1200 "locate wrong inode for FID: "DFID", %u/%u => %ld/%u\n",
1201 PFID(fid), id->oii_ino, id->oii_gen,
1202 inode->i_ino, inode->i_generation);
1204 saved_ino = inode->i_ino;
1205 saved_gen = inode->i_generation;
1207 if (unlikely(result == -ENODATA)) {
1208 /* If the OI scrub updated the OI mapping by race, it
1209 * must be valid. Trust the inode that has no LMA EA. */
1213 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1215 /* The OI mapping is still there, the inode is still
1216 * valid. It is just becaues the inode has no LMA EA. */
1217 if (saved_ino == id->oii_ino &&
1218 saved_gen == id->oii_gen)
1221 /* It is the OI scrub updated the OI mapping by race.
1222 * The new OI mapping must be valid. */
1228 /* "result == -ENOENT" means that the OI mappinghas been
1229 * removed by race, so the inode belongs to other object.
1231 * Others error can be returned directly. */
1232 if (result == -ENOENT) {
1235 obj->oo_inode = NULL;
1240 if (result != -EREMCHG)
1245 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1246 /* "result == -ENOENT" means the cached OI mapping has been removed
1247 * from the OI file by race, above inode belongs to other object. */
1248 if (result == -ENOENT) {
1251 obj->oo_inode = NULL;
1252 GOTO(out, result = 0);
1258 if (saved_ino == id->oii_ino && saved_gen == id->oii_gen) {
1263 /* It is the OI scrub updated the OI mapping by race.
1264 * The new OI mapping must be valid. */
1271 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
1272 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
1274 result = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
1276 if (lma->lma_compat & LMAC_STRIPE_INFO &&
1278 obj->oo_pfid_in_lma = 1;
1279 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
1281 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
1282 } else if (result != -ENODATA) {
1287 obj->oo_compat_dot_created = 1;
1288 obj->oo_compat_dotdot_created = 1;
1290 if (S_ISDIR(inode->i_mode) &&
1291 (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1292 osd_check_lmv(info, dev, inode, oic);
1294 result = osd_attach_jinode(inode);
1299 GOTO(out, result = 0);
1301 LASSERT(!obj->oo_hl_head);
1302 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1304 GOTO(out, result = (!obj->oo_hl_head ? -ENOMEM : 0));
1307 if (result || !obj->oo_inode) {
1308 if (!IS_ERR_OR_NULL(inode))
1311 obj->oo_inode = NULL;
1313 fid_zero(&oic->oic_fid);
1316 LINVRNT(osd_invariant(obj));
1321 * Concurrency: shouldn't matter.
1323 static void osd_object_init0(struct osd_object *obj)
1325 LASSERT(obj->oo_inode != NULL);
1326 obj->oo_dt.do_body_ops = &osd_body_ops;
1327 obj->oo_dt.do_lu.lo_header->loh_attr |=
1328 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1332 * Concurrency: no concurrent access is possible that early in object
1335 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1336 const struct lu_object_conf *conf)
1338 struct osd_object *obj = osd_obj(l);
1341 LINVRNT(osd_invariant(obj));
1343 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1344 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1345 l->lo_header->loh_attr |= LOHA_EXISTS;
1349 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1350 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1351 if (result == 0 && obj->oo_inode != NULL) {
1352 struct osd_thread_info *oti = osd_oti_get(env);
1353 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
1355 osd_object_init0(obj);
1356 if (unlikely(obj->oo_header))
1359 result = osd_get_lma(oti, obj->oo_inode,
1360 &oti->oti_obj_dentry, loa);
1362 /* Convert LMAI flags to lustre LMA flags
1363 * and cache it to oo_lma_flags */
1365 lma_to_lustre_flags(loa->loa_lma.lma_incompat);
1366 } else if (result == -ENODATA) {
1371 LINVRNT(osd_invariant(obj));
1375 /* The first part of oxe_buf is xattr name, and is '\0' terminated.
1376 * The left part is for value, binary mode. */
1377 struct osd_xattr_entry {
1378 struct list_head oxe_list;
1382 struct rcu_head oxe_rcu;
1386 static int osd_oxc_get(struct osd_object *obj, const char *name,
1389 struct osd_xattr_entry *tmp;
1390 struct osd_xattr_entry *oxe = NULL;
1391 size_t namelen = strlen(name);
1396 list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1397 if (namelen == tmp->oxe_namelen &&
1398 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1405 GOTO(out, rc = -ENOENT);
1407 if (!oxe->oxe_exist)
1408 GOTO(out, rc = -ENODATA);
1411 rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1414 if (buf->lb_buf == NULL)
1417 if (buf->lb_len < rc)
1418 GOTO(out, rc = -ERANGE);
1420 memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1428 static void osd_oxc_free(struct rcu_head *head)
1430 struct osd_xattr_entry *oxe;
1432 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1433 OBD_FREE(oxe, oxe->oxe_len);
1436 static void osd_oxc_add(struct osd_object *obj, const char *name,
1437 const char *buf, int buflen)
1439 struct osd_xattr_entry *oxe;
1440 struct osd_xattr_entry *old = NULL;
1441 struct osd_xattr_entry *tmp;
1442 size_t namelen = strlen(name);
1443 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1445 OBD_ALLOC(oxe, len);
1449 INIT_LIST_HEAD(&oxe->oxe_list);
1451 oxe->oxe_namelen = namelen;
1452 memcpy(oxe->oxe_buf, name, namelen);
1454 LASSERT(buf != NULL);
1455 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1456 oxe->oxe_exist = true;
1458 oxe->oxe_exist = false;
1461 /* this should be rarely called, just remove old and add new */
1462 spin_lock(&obj->oo_guard);
1463 list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1464 if (namelen == tmp->oxe_namelen &&
1465 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1471 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1472 call_rcu(&old->oxe_rcu, osd_oxc_free);
1474 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1476 spin_unlock(&obj->oo_guard);
1479 static void osd_oxc_del(struct osd_object *obj, const char *name)
1481 struct osd_xattr_entry *oxe;
1482 size_t namelen = strlen(name);
1484 spin_lock(&obj->oo_guard);
1485 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1486 if (namelen == oxe->oxe_namelen &&
1487 strncmp(name, oxe->oxe_buf, namelen) == 0) {
1488 list_del_rcu(&oxe->oxe_list);
1489 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1493 spin_unlock(&obj->oo_guard);
1496 static void osd_oxc_fini(struct osd_object *obj)
1498 struct osd_xattr_entry *oxe, *next;
1500 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1501 list_del(&oxe->oxe_list);
1502 OBD_FREE(oxe, oxe->oxe_len);
1507 * Concurrency: no concurrent access is possible that late in object
1510 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1512 struct osd_object *obj = osd_obj(l);
1513 struct lu_object_header *h = obj->oo_header;
1515 LINVRNT(osd_invariant(obj));
1518 dt_object_fini(&obj->oo_dt);
1519 if (obj->oo_hl_head != NULL)
1520 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1523 lu_object_header_fini(h);
1529 * Concurrency: no concurrent access is possible that late in object
1532 static void osd_index_fini(struct osd_object *o)
1534 struct iam_container *bag;
1536 if (o->oo_dir != NULL) {
1537 bag = &o->oo_dir->od_container;
1538 if (o->oo_inode != NULL) {
1539 if (bag->ic_object == o->oo_inode)
1540 iam_container_fini(bag);
1542 OBD_FREE_PTR(o->oo_dir);
1548 * Concurrency: no concurrent access is possible that late in object
1549 * life-cycle (for all existing callers, that is. New callers have to provide
1550 * their own locking.)
1552 static int osd_inode_unlinked(const struct inode *inode)
1554 return inode->i_nlink == 0;
1558 OSD_TXN_OI_DELETE_CREDITS = 20,
1559 OSD_TXN_INODE_DELETE_CREDITS = 20
1566 #if OSD_THANDLE_STATS
1568 * Set time when the handle is allocated
1570 static void osd_th_alloced(struct osd_thandle *oth)
1572 oth->oth_alloced = ktime_get();
1576 * Set time when the handle started
1578 static void osd_th_started(struct osd_thandle *oth)
1580 oth->oth_started = ktime_get();
1584 * Check whether the we deal with this handle for too long.
1586 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1587 ktime_t alloced, ktime_t started,
1590 ktime_t now = ktime_get();
1592 LASSERT(dev != NULL);
1594 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1595 ktime_us_delta(started, alloced));
1596 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1597 ktime_us_delta(closed, started));
1598 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1599 ktime_us_delta(now, closed));
1601 if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1602 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1603 oth, now, alloced, started, closed);
1604 libcfs_debug_dumpstack(NULL);
1608 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1610 ktime_t __closed = ktime_get(); \
1611 ktime_t __alloced = oth->oth_alloced; \
1612 ktime_t __started = oth->oth_started; \
1615 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1618 #else /* OSD_THANDLE_STATS */
1620 #define osd_th_alloced(h) do {} while(0)
1621 #define osd_th_started(h) do {} while(0)
1622 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1624 #endif /* OSD_THANDLE_STATS */
1627 * Concurrency: doesn't access mutable data.
1629 static int osd_param_is_not_sane(const struct osd_device *dev,
1630 const struct thandle *th)
1632 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1634 return oh->ot_credits > osd_transaction_size(dev);
1638 * Concurrency: shouldn't matter.
1640 static void osd_trans_commit_cb(struct super_block *sb,
1641 struct ldiskfs_journal_cb_entry *jcb, int error)
1643 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1644 struct thandle *th = &oh->ot_super;
1645 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1646 struct dt_txn_commit_cb *dcb, *tmp;
1648 LASSERT(oh->ot_handle == NULL);
1651 CERROR("transaction @0x%p commit error: %d\n", th, error);
1653 dt_txn_hook_commit(th);
1655 /* call per-transaction callbacks if any */
1656 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1658 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1659 "commit callback entry: magic=%x name='%s'\n",
1660 dcb->dcb_magic, dcb->dcb_name);
1661 list_del_init(&dcb->dcb_linkage);
1662 dcb->dcb_func(NULL, th, dcb, error);
1665 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1672 #ifndef HAVE_SB_START_WRITE
1673 # define sb_start_write(sb) do {} while (0)
1674 # define sb_end_write(sb) do {} while (0)
1677 static struct thandle *osd_trans_create(const struct lu_env *env,
1678 struct dt_device *d)
1680 struct osd_thread_info *oti = osd_oti_get(env);
1681 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1682 struct osd_thandle *oh;
1687 CERROR("%s: someone try to start transaction under "
1688 "readonly mode, should be disabled.\n",
1689 osd_name(osd_dt_dev(d)));
1691 RETURN(ERR_PTR(-EROFS));
1694 /* on pending IO in this thread should left from prev. request */
1695 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1697 sb_start_write(osd_sb(osd_dt_dev(d)));
1699 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1701 sb_end_write(osd_sb(osd_dt_dev(d)));
1702 RETURN(ERR_PTR(-ENOMEM));
1705 oh->ot_quota_trans = &oti->oti_quota_trans;
1706 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1711 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1712 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1715 memset(oti->oti_declare_ops, 0,
1716 sizeof(oti->oti_declare_ops));
1717 memset(oti->oti_declare_ops_cred, 0,
1718 sizeof(oti->oti_declare_ops_cred));
1719 memset(oti->oti_declare_ops_used, 0,
1720 sizeof(oti->oti_declare_ops_used));
1722 oti->oti_ins_cache_depth++;
1727 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1729 struct osd_thread_info *oti = osd_oti_get(env);
1730 struct osd_thandle *oh;
1732 oh = container_of0(th, struct osd_thandle, ot_super);
1733 LASSERT(oh != NULL);
1735 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1736 oti->oti_declare_ops[OSD_OT_CREATE],
1737 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1738 oti->oti_declare_ops_used[OSD_OT_CREATE],
1739 oti->oti_declare_ops[OSD_OT_DESTROY],
1740 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1741 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1742 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1743 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1744 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1745 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1746 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1747 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1748 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1749 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1750 oti->oti_declare_ops[OSD_OT_WRITE],
1751 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1752 oti->oti_declare_ops_used[OSD_OT_WRITE],
1753 oti->oti_declare_ops[OSD_OT_PUNCH],
1754 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1755 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1756 oti->oti_declare_ops[OSD_OT_QUOTA],
1757 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1758 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1759 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1760 oti->oti_declare_ops[OSD_OT_INSERT],
1761 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1762 oti->oti_declare_ops_used[OSD_OT_INSERT],
1763 oti->oti_declare_ops[OSD_OT_DELETE],
1764 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1765 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1766 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1767 oti->oti_declare_ops[OSD_OT_REF_ADD],
1768 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1769 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1770 oti->oti_declare_ops[OSD_OT_REF_DEL],
1771 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1772 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1776 * Concurrency: shouldn't matter.
1778 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1781 struct osd_thread_info *oti = osd_oti_get(env);
1782 struct osd_device *dev = osd_dt_dev(d);
1784 struct osd_thandle *oh;
1789 LASSERT(current->journal_info == NULL);
1791 oh = container_of0(th, struct osd_thandle, ot_super);
1792 LASSERT(oh != NULL);
1793 LASSERT(oh->ot_handle == NULL);
1795 rc = dt_txn_hook_start(env, d, th);
1799 if (unlikely(osd_param_is_not_sane(dev, th))) {
1800 static unsigned long last_printed;
1801 static int last_credits;
1803 /* don't make noise on a tiny testing systems
1804 * actual credits misuse will be caught anyway */
1805 if (last_credits != oh->ot_credits &&
1806 time_after(jiffies, last_printed +
1807 msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
1808 osd_transaction_size(dev) > 512) {
1809 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1810 oh->ot_credits, osd_transaction_size(dev));
1811 osd_trans_dump_creds(env, th);
1812 libcfs_debug_dumpstack(NULL);
1813 last_credits = oh->ot_credits;
1814 last_printed = jiffies;
1816 /* XXX Limit the credits to 'max_transaction_buffers', and
1817 * let the underlying filesystem to catch the error if
1818 * we really need so many credits.
1820 * This should be removed when we can calculate the
1821 * credits precisely. */
1822 oh->ot_credits = osd_transaction_size(dev);
1823 } else if (ldiskfs_track_declares_assert != 0) {
1824 /* reserve few credits to prevent an assertion in JBD
1825 * our debugging mechanism will be able to detected
1826 * overuse. this can help to debug single-update
1828 oh->ot_credits += 10;
1829 if (unlikely(osd_param_is_not_sane(dev, th)))
1830 oh->ot_credits = osd_transaction_size(dev);
1834 * XXX temporary stuff. Some abstraction layer should
1837 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1841 LASSERT(oti->oti_txns == 0);
1843 lu_device_get(&d->dd_lu_dev);
1844 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1855 static int osd_seq_exists(const struct lu_env *env,
1856 struct osd_device *osd, u64 seq)
1858 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1859 struct seq_server_site *ss = osd_seq_site(osd);
1863 LASSERT(ss != NULL);
1864 LASSERT(ss->ss_server_fld != NULL);
1866 rc = osd_fld_lookup(env, osd, seq, range);
1869 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
1870 osd_name(osd), seq, rc);
1874 RETURN(ss->ss_node_id == range->lsr_index);
1877 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1879 struct dt_txn_commit_cb *dcb;
1880 struct dt_txn_commit_cb *tmp;
1882 /* call per-transaction stop callbacks if any */
1883 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1885 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1886 "commit callback entry: magic=%x name='%s'\n",
1887 dcb->dcb_magic, dcb->dcb_name);
1888 list_del_init(&dcb->dcb_linkage);
1889 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1894 * Concurrency: shouldn't matter.
1896 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1899 int rc = 0, remove_agents = 0;
1900 struct osd_thandle *oh;
1901 struct osd_thread_info *oti = osd_oti_get(env);
1902 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1903 struct osd_device *osd = osd_dt_dev(th->th_dev);
1904 struct qsd_instance *qsd = osd->od_quota_slave;
1905 struct lquota_trans *qtrans;
1908 oh = container_of0(th, struct osd_thandle, ot_super);
1910 remove_agents = oh->ot_remove_agents;
1912 qtrans = oh->ot_quota_trans;
1913 oh->ot_quota_trans = NULL;
1915 if (oh->ot_handle != NULL) {
1917 handle_t *hdl = oh->ot_handle;
1920 * add commit callback
1921 * notice we don't do this in osd_trans_start()
1922 * as underlying transaction can change during truncate
1924 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1927 LASSERT(oti->oti_txns == 1);
1930 rc = dt_txn_hook_stop(env, th);
1932 CERROR("%s: failed in transaction hook: rc = %d\n",
1935 osd_trans_stop_cb(oh, rc);
1936 /* hook functions might modify th_sync */
1937 hdl->h_sync = th->th_sync;
1939 oh->ot_handle = NULL;
1940 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
1942 CERROR("%s: failed to stop transaction: rc = %d\n",
1943 osd_name(osd), rc2);
1947 osd_trans_stop_cb(oh, th->th_result);
1951 /* inform the quota slave device that the transaction is stopping */
1952 qsd_op_end(env, qsd, qtrans);
1954 /* as we want IO to journal and data IO be concurrent, we don't block
1955 * awaiting data IO completion in osd_do_bio(), instead we wait here
1956 * once transaction is submitted to the journal. all reqular requests
1957 * don't do direct IO (except read/write), thus this wait_event becomes
1960 * IMPORTANT: we have to wait till any IO submited by the thread is
1961 * completed otherwise iobuf may be corrupted by different request
1963 wait_event(iobuf->dr_wait,
1964 atomic_read(&iobuf->dr_numreqs) == 0);
1965 osd_fini_iobuf(osd, iobuf);
1967 rc = iobuf->dr_error;
1969 if (unlikely(remove_agents != 0))
1970 osd_process_scheduled_agent_removals(env, osd);
1972 oti->oti_ins_cache_depth--;
1973 /* reset OI cache for safety */
1974 if (oti->oti_ins_cache_depth == 0)
1975 oti->oti_ins_cache_used = 0;
1977 sb_end_write(osd_sb(osd));
1982 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1984 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1987 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1988 LASSERT(&dcb->dcb_func != NULL);
1989 if (dcb->dcb_flags & DCB_TRANS_STOP)
1990 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1992 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1998 * Called just before object is freed. Releases all resources except for
1999 * object itself (that is released by osd_object_free()).
2001 * Concurrency: no concurrent access is possible that late in object
2004 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
2006 struct osd_object *obj = osd_obj(l);
2007 struct inode *inode = obj->oo_inode;
2009 LINVRNT(osd_invariant(obj));
2012 * If object is unlinked remove fid->ino mapping from object index.
2015 osd_index_fini(obj);
2016 if (inode != NULL) {
2017 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
2018 qid_t uid = i_uid_read(inode);
2019 qid_t gid = i_gid_read(inode);
2021 obj->oo_inode = NULL;
2023 if (!obj->oo_header && qsd) {
2024 struct osd_thread_info *info = osd_oti_get(env);
2025 struct lquota_id_info *qi = &info->oti_qi;
2027 /* Release granted quota to master if necessary */
2028 qi->lqi_id.qid_uid = uid;
2029 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
2031 qi->lqi_id.qid_uid = gid;
2032 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
2034 qi->lqi_id.qid_uid = i_projid_read(inode);
2035 qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
2041 * Concurrency: ->loo_object_release() is called under site spin-lock.
2043 static void osd_object_release(const struct lu_env *env,
2044 struct lu_object *l)
2046 struct osd_object *o = osd_obj(l);
2047 /* nobody should be releasing a non-destroyed object with nlink=0
2048 * the API allows this, but ldiskfs doesn't like and then report
2049 * this inode as deleted */
2050 if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
2055 * Concurrency: shouldn't matter.
2057 static int osd_object_print(const struct lu_env *env, void *cookie,
2058 lu_printer_t p, const struct lu_object *l)
2060 struct osd_object *o = osd_obj(l);
2061 struct iam_descr *d;
2063 if (o->oo_dir != NULL)
2064 d = o->oo_dir->od_container.ic_descr;
2067 return (*p)(env, cookie,
2068 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
2070 o->oo_inode ? o->oo_inode->i_ino : 0UL,
2071 o->oo_inode ? o->oo_inode->i_generation : 0,
2072 d ? d->id_ops->id_name : "plain");
2076 * Concurrency: shouldn't matter.
2078 int osd_statfs(const struct lu_env *env, struct dt_device *d,
2079 struct obd_statfs *sfs)
2081 struct osd_device *osd = osd_dt_dev(d);
2082 struct super_block *sb = osd_sb(osd);
2083 struct kstatfs *ksfs;
2087 if (unlikely(osd->od_mnt == NULL))
2088 return -EINPROGRESS;
2090 /* osd_lproc.c call this without env, allocate ksfs for that case */
2091 if (unlikely(env == NULL)) {
2092 OBD_ALLOC_PTR(ksfs);
2096 ksfs = &osd_oti_get(env)->oti_ksfs;
2099 result = sb->s_op->statfs(sb->s_root, ksfs);
2103 statfs_pack(sfs, ksfs);
2104 if (unlikely(sb->s_flags & MS_RDONLY))
2105 sfs->os_state |= OS_STATE_READONLY;
2106 if (ldiskfs_has_feature_extents(sb))
2107 sfs->os_maxbytes = sb->s_maxbytes;
2109 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2112 * Reserve some space so to avoid fragmenting the filesystem too much.
2113 * Fragmentation not only impacts performance, but can also increase
2114 * metadata overhead significantly, causing grant calculation to be
2117 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2119 CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
2120 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2121 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2122 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2124 sfs->os_blocks -= reserved;
2125 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2126 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2129 if (unlikely(env == NULL))
2135 * Estimate space needed for file creations. We assume the largest filename
2136 * which is 2^64 - 1, hence a filename of 20 chars.
2137 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2139 #ifdef __LDISKFS_DIR_REC_LEN
2140 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2142 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2146 * Concurrency: doesn't access mutable data.
2148 static void osd_conf_get(const struct lu_env *env,
2149 const struct dt_device *dev,
2150 struct dt_device_param *param)
2152 struct super_block *sb = osd_sb(osd_dt_dev(dev));
2156 * XXX should be taken from not-yet-existing fs abstraction layer.
2158 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2159 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2160 param->ddp_symlink_max = sb->s_blocksize;
2161 param->ddp_mount_type = LDD_MT_LDISKFS;
2162 if (ldiskfs_has_feature_extents(sb))
2163 param->ddp_maxbytes = sb->s_maxbytes;
2165 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2166 /* inode are statically allocated, so per-inode space consumption
2167 * is the space consumed by the directory entry */
2168 param->ddp_inodespace = PER_OBJ_USAGE;
2169 /* EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2170 * = 128MB) which is unlikely to be hit in real life. Report a smaller
2171 * maximum length to not under count the actual number of extents
2172 * needed for writing a file. */
2173 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
2174 /* worst-case extent insertion metadata overhead */
2175 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2176 param->ddp_mntopts = 0;
2177 if (test_opt(sb, XATTR_USER))
2178 param->ddp_mntopts |= MNTOPT_USERXATTR;
2179 if (test_opt(sb, POSIX_ACL))
2180 param->ddp_mntopts |= MNTOPT_ACL;
2182 /* LOD might calculate the max stripe count based on max_ea_size,
2183 * so we need take account in the overhead as well,
2184 * xattr_header + magic + xattr_entry_head */
2185 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2186 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2188 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2189 if (ldiskfs_has_feature_ea_inode(sb))
2190 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2194 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2196 if (param->ddp_max_ea_size > OSD_MAX_EA_SIZE)
2197 param->ddp_max_ea_size = OSD_MAX_EA_SIZE;
2199 /* Preferred RPC size for efficient disk IO. 4MB shows good
2200 * all-around performance for ldiskfs, but use bigalloc chunk size
2201 * by default if larger. */
2202 #if defined(LDISKFS_CLUSTER_SIZE)
2203 if (LDISKFS_CLUSTER_SIZE(sb) > DT_DEF_BRW_SIZE)
2204 param->ddp_brw_size = LDISKFS_CLUSTER_SIZE(sb);
2207 param->ddp_brw_size = DT_DEF_BRW_SIZE;
2211 * Concurrency: shouldn't matter.
2213 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2217 CDEBUG(D_CACHE, "%s: syncing OSD\n", osd_dt_dev(d)->od_svname);
2219 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
2221 CDEBUG(D_CACHE, "%s: synced OSD: rc = %d\n", osd_dt_dev(d)->od_svname,
2228 * Start commit for OSD device.
2230 * An implementation of dt_commit_async method for OSD device.
2231 * Asychronously starts underlayng fs sync and thereby a transaction
2234 * \param env environment
2235 * \param d dt device
2237 * \see dt_device_operations
2239 static int osd_commit_async(const struct lu_env *env,
2240 struct dt_device *d)
2242 struct super_block *s = osd_sb(osd_dt_dev(d));
2245 CDEBUG(D_HA, "%s: async commit OSD\n", osd_dt_dev(d)->od_svname);
2246 RETURN(s->s_op->sync_fs(s, 0));
2249 /* Our own copy of the set readonly functions if present, or NU if not. */
2250 static int (*priv_dev_set_rdonly)(struct block_device *bdev);
2251 static int (*priv_dev_check_rdonly)(struct block_device *bdev);
2252 /* static int (*priv_dev_clear_rdonly)(struct block_device *bdev); */
2255 * Concurrency: shouldn't matter.
2257 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2259 struct super_block *sb = osd_sb(osd_dt_dev(d));
2260 struct block_device *dev = sb->s_bdev;
2261 int rc = -EOPNOTSUPP;
2264 if (priv_dev_set_rdonly) {
2265 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
2268 CERROR("*** setting %s read-only ***\n",
2269 osd_dt_dev(d)->od_svname);
2271 if (sb->s_op->freeze_fs) {
2272 rc = sb->s_op->freeze_fs(sb);
2277 if (jdev && (jdev != dev)) {
2278 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
2280 priv_dev_set_rdonly(jdev);
2282 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
2283 priv_dev_set_rdonly(dev);
2285 if (sb->s_op->unfreeze_fs)
2286 sb->s_op->unfreeze_fs(sb);
2291 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2292 osd_dt_dev(d)->od_svname, (long)dev, rc);
2298 * Note: we do not count into QUOTA here.
2299 * If we mount with --data_journal we may need more.
2301 const int osd_dto_credits_noquota[DTO_NR] = {
2304 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2305 * SINGLEDATA_TRANS_BLOCKS(8)
2306 * XXX Note: maybe iam need more, since iam have more level than
2309 [DTO_INDEX_INSERT] = 16,
2312 * just modify a single entry, probably merge few within a block
2314 [DTO_INDEX_DELETE] = 1,
2318 [DTO_INDEX_UPDATE] = 16,
2320 * 4(inode, inode bits, groups, GDT)
2321 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2323 [DTO_OBJECT_CREATE] = 4,
2325 * 4(inode, inode bits, groups, GDT)
2326 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2328 [DTO_OBJECT_DELETE] = 4,
2330 * Attr set credits (inode)
2332 [DTO_ATTR_SET_BASE] = 1,
2334 * Xattr set. The same as xattr of EXT3.
2335 * DATA_TRANS_BLOCKS(14)
2336 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2337 * are also counted in. Do not know why?
2339 [DTO_XATTR_SET] = 14,
2341 * credits for inode change during write.
2343 [DTO_WRITE_BASE] = 3,
2345 * credits for single block write.
2347 [DTO_WRITE_BLOCK] = 14,
2349 * Attr set credits for chown.
2350 * This is extra credits for setattr, and it is null without quota
2352 [DTO_ATTR_SET_CHOWN] = 0
2355 static const struct dt_device_operations osd_dt_ops = {
2356 .dt_root_get = osd_root_get,
2357 .dt_statfs = osd_statfs,
2358 .dt_trans_create = osd_trans_create,
2359 .dt_trans_start = osd_trans_start,
2360 .dt_trans_stop = osd_trans_stop,
2361 .dt_trans_cb_add = osd_trans_cb_add,
2362 .dt_conf_get = osd_conf_get,
2363 .dt_sync = osd_sync,
2365 .dt_commit_async = osd_commit_async,
2368 static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
2371 struct osd_object *obj = osd_dt_obj(dt);
2372 struct osd_thread_info *oti = osd_oti_get(env);
2374 LINVRNT(osd_invariant(obj));
2376 LASSERT(obj->oo_owner != env);
2377 down_read_nested(&obj->oo_sem, role);
2379 LASSERT(obj->oo_owner == NULL);
2383 static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
2386 struct osd_object *obj = osd_dt_obj(dt);
2387 struct osd_thread_info *oti = osd_oti_get(env);
2389 LINVRNT(osd_invariant(obj));
2391 LASSERT(obj->oo_owner != env);
2392 down_write_nested(&obj->oo_sem, role);
2394 LASSERT(obj->oo_owner == NULL);
2395 obj->oo_owner = env;
2399 static void osd_read_unlock(const struct lu_env *env, struct dt_object *dt)
2401 struct osd_object *obj = osd_dt_obj(dt);
2402 struct osd_thread_info *oti = osd_oti_get(env);
2404 LINVRNT(osd_invariant(obj));
2406 LASSERT(oti->oti_r_locks > 0);
2408 up_read(&obj->oo_sem);
2411 static void osd_write_unlock(const struct lu_env *env, struct dt_object *dt)
2413 struct osd_object *obj = osd_dt_obj(dt);
2414 struct osd_thread_info *oti = osd_oti_get(env);
2416 LINVRNT(osd_invariant(obj));
2418 LASSERT(obj->oo_owner == env);
2419 LASSERT(oti->oti_w_locks > 0);
2421 obj->oo_owner = NULL;
2422 up_write(&obj->oo_sem);
2425 static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
2427 struct osd_object *obj = osd_dt_obj(dt);
2429 LINVRNT(osd_invariant(obj));
2431 return obj->oo_owner == env;
2434 static struct timespec *osd_inode_time(const struct lu_env *env,
2435 struct inode *inode, __u64 seconds)
2437 struct osd_thread_info *oti = osd_oti_get(env);
2438 struct timespec *t = &oti->oti_time;
2440 t->tv_sec = seconds;
2442 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
2446 static void osd_inode_getattr(const struct lu_env *env,
2447 struct inode *inode, struct lu_attr *attr)
2449 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2450 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2451 LA_PROJID | LA_FLAGS | LA_NLINK | LA_RDEV |
2452 LA_BLKSIZE | LA_TYPE;
2454 attr->la_atime = LTIME_S(inode->i_atime);
2455 attr->la_mtime = LTIME_S(inode->i_mtime);
2456 attr->la_ctime = LTIME_S(inode->i_ctime);
2457 attr->la_mode = inode->i_mode;
2458 attr->la_size = i_size_read(inode);
2459 attr->la_blocks = inode->i_blocks;
2460 attr->la_uid = i_uid_read(inode);
2461 attr->la_gid = i_gid_read(inode);
2462 attr->la_projid = i_projid_read(inode);
2463 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2464 attr->la_nlink = inode->i_nlink;
2465 attr->la_rdev = inode->i_rdev;
2466 attr->la_blksize = 1 << inode->i_blkbits;
2467 attr->la_blkbits = inode->i_blkbits;
2469 * Ext4 did not transfer inherit flags from raw inode
2470 * to inode flags, and ext4 internally test raw inode
2471 * @i_flags directly. Instead of patching ext4, we do it here.
2473 if (LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL)
2474 attr->la_flags |= LUSTRE_PROJINHERIT_FL;
2477 static int osd_attr_get(const struct lu_env *env,
2478 struct dt_object *dt,
2479 struct lu_attr *attr)
2481 struct osd_object *obj = osd_dt_obj(dt);
2483 if (unlikely(!dt_object_exists(dt)))
2485 if (unlikely(obj->oo_destroyed))
2488 LASSERT(!dt_object_remote(dt));
2489 LINVRNT(osd_invariant(obj));
2491 spin_lock(&obj->oo_guard);
2492 osd_inode_getattr(env, obj->oo_inode, attr);
2493 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
2494 attr->la_flags |= LUSTRE_ORPHAN_FL;
2495 spin_unlock(&obj->oo_guard);
2500 static int osd_declare_attr_qid(const struct lu_env *env,
2501 struct osd_object *obj,
2502 struct osd_thandle *oh, long long bspace,
2503 qid_t old_id, qid_t new_id, bool enforce,
2504 unsigned type, bool ignore_edquot)
2507 struct osd_thread_info *info = osd_oti_get(env);
2508 struct lquota_id_info *qi = &info->oti_qi;
2510 qi->lqi_type = type;
2511 /* inode accounting */
2512 qi->lqi_is_blk = false;
2514 /* one more inode for the new id ... */
2515 qi->lqi_id.qid_uid = new_id;
2517 /* Reserve credits for the new id */
2518 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2519 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2524 /* and one less inode for the current id */
2525 qi->lqi_id.qid_uid = old_id;
2527 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2528 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2533 /* block accounting */
2534 qi->lqi_is_blk = true;
2536 /* more blocks for the new id ... */
2537 qi->lqi_id.qid_uid = new_id;
2538 qi->lqi_space = bspace;
2540 * Credits for the new uid has been reserved, re-use "obj"
2541 * to save credit reservation.
2543 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2544 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2549 /* and finally less blocks for the current uid */
2550 qi->lqi_id.qid_uid = old_id;
2551 qi->lqi_space = -bspace;
2552 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2553 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2559 static int osd_declare_attr_set(const struct lu_env *env,
2560 struct dt_object *dt,
2561 const struct lu_attr *attr,
2562 struct thandle *handle)
2564 struct osd_thandle *oh;
2565 struct osd_object *obj;
2573 LASSERT(dt != NULL);
2574 LASSERT(handle != NULL);
2576 obj = osd_dt_obj(dt);
2577 LASSERT(osd_invariant(obj));
2579 oh = container_of0(handle, struct osd_thandle, ot_super);
2580 LASSERT(oh->ot_handle == NULL);
2582 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2583 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2585 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2586 osd_dto_credits_noquota[DTO_XATTR_SET]);
2588 if (attr == NULL || obj->oo_inode == NULL)
2591 bspace = obj->oo_inode->i_blocks << 9;
2592 bspace = toqb(bspace);
2594 /* Changing ownership is always preformed by super user, it should not
2595 * fail with EDQUOT unless required explicitly.
2597 * We still need to call the osd_declare_qid() to calculate the journal
2598 * credits for updating quota accounting files and to trigger quota
2599 * space adjustment once the operation is completed.*/
2600 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2601 bool ignore_edquot = !(attr->la_flags & LUSTRE_SET_SYNC_FL);
2604 CDEBUG(D_QUOTA, "%s: enforce quota on UID %u, GID %u"
2605 "(the quota space is %lld)\n",
2606 obj->oo_inode->i_sb->s_id, attr->la_uid,
2607 attr->la_gid, bspace);
2610 uid = i_uid_read(obj->oo_inode);
2611 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2612 rc = osd_declare_attr_qid(env, obj, oh, bspace, uid,
2613 attr->la_uid, enforce, USRQUOTA,
2618 gid = i_gid_read(obj->oo_inode);
2619 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2620 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2621 i_gid_read(obj->oo_inode),
2622 attr->la_gid, enforce, GRPQUOTA,
2628 #ifdef HAVE_PROJECT_QUOTA
2629 if (attr->la_valid & LA_PROJID) {
2630 __u32 projid = i_projid_read(obj->oo_inode);
2631 enforce = (attr->la_valid & LA_PROJID) &&
2632 (attr->la_projid != projid);
2633 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2634 (qid_t)projid, (qid_t)attr->la_projid,
2635 enforce, PRJQUOTA, true);
2643 static int osd_inode_setattr(const struct lu_env *env,
2644 struct inode *inode, const struct lu_attr *attr)
2646 __u64 bits = attr->la_valid;
2648 /* Only allow set size for regular file */
2649 if (!S_ISREG(inode->i_mode))
2650 bits &= ~(LA_SIZE | LA_BLOCKS);
2655 if (bits & LA_ATIME)
2656 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2657 if (bits & LA_CTIME)
2658 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2659 if (bits & LA_MTIME)
2660 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2661 if (bits & LA_SIZE) {
2662 spin_lock(&inode->i_lock);
2663 LDISKFS_I(inode)->i_disksize = attr->la_size;
2664 i_size_write(inode, attr->la_size);
2665 spin_unlock(&inode->i_lock);
2668 /* OSD should not change "i_blocks" which is used by quota.
2669 * "i_blocks" should be changed by ldiskfs only. */
2671 inode->i_mode = (inode->i_mode & S_IFMT) |
2672 (attr->la_mode & ~S_IFMT);
2674 i_uid_write(inode, attr->la_uid);
2676 i_gid_write(inode, attr->la_gid);
2677 if (bits & LA_PROJID)
2678 i_projid_write(inode, attr->la_projid);
2679 if (bits & LA_NLINK)
2680 set_nlink(inode, attr->la_nlink);
2682 inode->i_rdev = attr->la_rdev;
2684 if (bits & LA_FLAGS) {
2685 /* always keep S_NOCMTIME */
2686 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2689 * Ext4 did not transfer inherit flags from
2690 * @inode->i_flags to raw inode i_flags when writing
2691 * flags, we do it explictly here.
2693 if (attr->la_flags & LUSTRE_PROJINHERIT_FL)
2694 LDISKFS_I(inode)->i_flags |= LUSTRE_PROJINHERIT_FL;
2696 LDISKFS_I(inode)->i_flags &= ~LUSTRE_PROJINHERIT_FL;
2701 #ifdef HAVE_PROJECT_QUOTA
2702 static int osd_transfer_project(struct inode *inode, __u32 projid)
2704 struct super_block *sb = inode->i_sb;
2705 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2708 struct ldiskfs_iloc iloc;
2709 struct ldiskfs_inode *raw_inode;
2710 struct dquot *transfer_to[LDISKFS_MAXQUOTAS] = { };
2712 if (!ldiskfs_has_feature_project(sb)) {
2713 LASSERT(__kprojid_val(LDISKFS_I(inode)->i_projid)
2714 == LDISKFS_DEF_PROJID);
2715 if (projid != LDISKFS_DEF_PROJID)
2721 if (LDISKFS_INODE_SIZE(sb) <= LDISKFS_GOOD_OLD_INODE_SIZE)
2724 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2725 if (projid_eq(kprojid, LDISKFS_I(inode)->i_projid))
2728 err = ldiskfs_get_inode_loc(inode, &iloc);
2732 raw_inode = ldiskfs_raw_inode(&iloc);
2733 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
2740 dquot_initialize(inode);
2741 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2742 if (transfer_to[PRJQUOTA]) {
2743 err = __dquot_transfer(inode, transfer_to);
2744 dqput(transfer_to[PRJQUOTA]);
2753 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2757 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2758 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2761 ll_vfs_dq_init(inode);
2763 if (attr->la_valid & LA_UID)
2764 iattr.ia_valid |= ATTR_UID;
2765 if (attr->la_valid & LA_GID)
2766 iattr.ia_valid |= ATTR_GID;
2767 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2768 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2770 rc = ll_vfs_dq_transfer(inode, &iattr);
2772 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2773 "enforcement enabled on the ldiskfs "
2774 "filesystem?\n", inode->i_sb->s_id, rc);
2779 /* Handle project id transfer here properly */
2780 if (attr->la_valid & LA_PROJID &&
2781 attr->la_projid != i_projid_read(inode)) {
2782 #ifdef HAVE_PROJECT_QUOTA
2783 rc = osd_transfer_project(inode, attr->la_projid);
2788 CERROR("%s: quota transfer failed: rc = %d. Is project "
2789 "enforcement enabled on the ldiskfs "
2790 "filesystem?\n", inode->i_sb->s_id, rc);
2797 static int osd_attr_set(const struct lu_env *env,
2798 struct dt_object *dt,
2799 const struct lu_attr *attr,
2800 struct thandle *handle)
2802 struct osd_object *obj = osd_dt_obj(dt);
2803 struct inode *inode;
2806 if (!dt_object_exists(dt))
2809 LASSERT(handle != NULL);
2810 LASSERT(!dt_object_remote(dt));
2811 LASSERT(osd_invariant(obj));
2813 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2815 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) &&
2816 !osd_obj2dev(obj)->od_is_ost) {
2817 struct osd_thread_info *oti = osd_oti_get(env);
2818 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2819 struct lu_fid *fid1 = &oti->oti_fid;
2820 struct osd_inode_id *id = &oti->oti_id;
2821 struct iam_path_descr *ipd;
2822 struct iam_container *bag;
2823 struct osd_thandle *oh;
2826 fid_cpu_to_be(fid1, fid0);
2827 memset(id, 1, sizeof(*id));
2828 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2829 fid0)->oi_dir.od_container;
2830 ipd = osd_idx_ipd_get(env, bag);
2831 if (unlikely(ipd == NULL))
2834 oh = container_of0(handle, struct osd_thandle, ot_super);
2835 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2836 (const struct iam_rec *)id, ipd);
2837 osd_ipd_put(env, bag, ipd);
2838 return(rc > 0 ? 0 : rc);
2841 inode = obj->oo_inode;
2843 rc = osd_quota_transfer(inode, attr);
2847 spin_lock(&obj->oo_guard);
2848 rc = osd_inode_setattr(env, inode, attr);
2849 spin_unlock(&obj->oo_guard);
2853 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2855 if (!(attr->la_valid & LA_FLAGS))
2858 /* Let's check if there are extra flags need to be set into LMA */
2859 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2860 struct osd_thread_info *info = osd_oti_get(env);
2861 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
2863 LASSERT(!obj->oo_pfid_in_lma);
2865 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
2866 &info->oti_ost_attrs);
2870 lma->lma_incompat |=
2871 lustre_to_lma_flags(attr->la_flags);
2872 lustre_lma_swab(lma);
2873 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2874 lma, sizeof(*lma), XATTR_REPLACE);
2876 struct osd_device *osd = osd_obj2dev(obj);
2878 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2879 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2880 lma->lma_incompat, rc);
2883 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2885 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2888 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2893 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2894 struct osd_object *obj,
2895 const char *name, const int namelen)
2897 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2900 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2901 umode_t mode, struct dt_allocation_hint *hint,
2905 struct osd_device *osd = osd_obj2dev(obj);
2906 struct osd_thandle *oth;
2907 struct dt_object *parent = NULL;
2908 struct inode *inode;
2910 LINVRNT(osd_invariant(obj));
2911 LASSERT(obj->oo_inode == NULL);
2912 LASSERT(obj->oo_hl_head == NULL);
2914 if (S_ISDIR(mode) && ldiskfs_pdo) {
2915 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2916 if (obj->oo_hl_head == NULL)
2920 oth = container_of(th, struct osd_thandle, ot_super);
2921 LASSERT(oth->ot_handle->h_transaction != NULL);
2923 if (hint != NULL && hint->dah_parent != NULL &&
2924 !dt_object_remote(hint->dah_parent))
2925 parent = hint->dah_parent;
2927 inode = ldiskfs_create_inode(oth->ot_handle,
2928 parent ? osd_dt_obj(parent)->oo_inode :
2929 osd_sb(osd)->s_root->d_inode,
2931 if (!IS_ERR(inode)) {
2932 /* Do not update file c/mtime in ldiskfs. */
2933 inode->i_flags |= S_NOCMTIME;
2935 /* For new created object, it must be consistent,
2936 * and it is unnecessary to scrub against it. */
2937 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2939 obj->oo_inode = inode;
2942 if (obj->oo_hl_head != NULL) {
2943 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2944 obj->oo_hl_head = NULL;
2946 result = PTR_ERR(inode);
2948 LINVRNT(osd_invariant(obj));
2956 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2957 struct lu_attr *attr,
2958 struct dt_allocation_hint *hint,
2959 struct dt_object_format *dof,
2963 struct osd_thandle *oth;
2964 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
2966 LASSERT(S_ISDIR(attr->la_mode));
2968 oth = container_of(th, struct osd_thandle, ot_super);
2969 LASSERT(oth->ot_handle->h_transaction != NULL);
2970 result = osd_mkfile(info, obj, mode, hint, th);
2975 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2976 struct lu_attr *attr,
2977 struct dt_allocation_hint *hint,
2978 struct dt_object_format *dof,
2982 struct osd_thandle *oth;
2983 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2985 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2987 LASSERT(S_ISREG(attr->la_mode));
2989 oth = container_of(th, struct osd_thandle, ot_super);
2990 LASSERT(oth->ot_handle->h_transaction != NULL);
2992 result = osd_mkfile(info, obj, mode, hint, th);
2994 LASSERT(obj->oo_inode != NULL);
2995 if (feat->dif_flags & DT_IND_VARKEY)
2996 result = iam_lvar_create(obj->oo_inode,
2997 feat->dif_keysize_max,
2999 feat->dif_recsize_max,
3002 result = iam_lfix_create(obj->oo_inode,
3003 feat->dif_keysize_max,
3005 feat->dif_recsize_max,
3012 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
3013 struct lu_attr *attr,
3014 struct dt_allocation_hint *hint,
3015 struct dt_object_format *dof,
3018 LASSERT(S_ISREG(attr->la_mode));
3019 return osd_mkfile(info, obj, (attr->la_mode &
3020 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
3023 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
3024 struct lu_attr *attr,
3025 struct dt_allocation_hint *hint,
3026 struct dt_object_format *dof,
3029 LASSERT(S_ISLNK(attr->la_mode));
3030 return osd_mkfile(info, obj, (attr->la_mode &
3031 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
3034 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
3035 struct lu_attr *attr,
3036 struct dt_allocation_hint *hint,
3037 struct dt_object_format *dof,
3040 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
3043 LINVRNT(osd_invariant(obj));
3044 LASSERT(obj->oo_inode == NULL);
3045 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
3046 S_ISFIFO(mode) || S_ISSOCK(mode));
3048 result = osd_mkfile(info, obj, mode, hint, th);
3050 LASSERT(obj->oo_inode != NULL);
3052 * This inode should be marked dirty for i_rdev. Currently
3053 * that is done in the osd_attr_init().
3055 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
3058 LINVRNT(osd_invariant(obj));
3062 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
3064 struct dt_allocation_hint *hint,
3065 struct dt_object_format *dof,
3068 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
3070 osd_obj_type_f result;
3086 result = osd_mk_index;
3097 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
3098 struct dt_object *parent, struct dt_object *child,
3103 ah->dah_parent = parent;
3104 ah->dah_mode = child_mode;
3106 if (parent != NULL && !dt_object_remote(parent)) {
3107 /* will help to find FID->ino at dt_insert("..") */
3108 struct osd_object *pobj = osd_dt_obj(parent);
3109 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
3113 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
3114 struct lu_attr *attr, struct dt_object_format *dof)
3116 struct inode *inode = obj->oo_inode;
3117 __u64 valid = attr->la_valid;
3120 attr->la_valid &= ~(LA_TYPE | LA_MODE);
3122 if (dof->dof_type != DFT_NODE)
3123 attr->la_valid &= ~LA_RDEV;
3124 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
3125 attr->la_valid &= ~LA_ATIME;
3126 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
3127 attr->la_valid &= ~LA_CTIME;
3128 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
3129 attr->la_valid &= ~LA_MTIME;
3131 result = osd_quota_transfer(inode, attr);
3135 if (attr->la_valid != 0) {
3136 result = osd_inode_setattr(info->oti_env, inode, attr);
3138 * The osd_inode_setattr() should always succeed here. The
3139 * only error that could be returned is EDQUOT when we are
3140 * trying to change the UID or GID of the inode. However, this
3141 * should not happen since quota enforcement is no longer
3142 * enabled on ldiskfs (lquota takes care of it).
3144 LASSERTF(result == 0, "%d\n", result);
3145 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3148 attr->la_valid = valid;
3152 * Helper function for osd_create()
3154 * \retval 0, on success
3156 static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
3157 struct lu_attr *attr, struct dt_allocation_hint *hint,
3158 struct dt_object_format *dof, struct thandle *th)
3163 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
3165 /* we drop umask so that permissions we pass are not affected */
3166 umask = current->fs->umask;
3167 current->fs->umask = 0;
3169 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
3171 if (likely(obj->oo_inode != NULL)) {
3172 LASSERT(obj->oo_inode->i_state & I_NEW);
3174 /* Unlock the inode before attr initialization to avoid
3175 * unnecessary dqget operations. LU-6378 */
3176 unlock_new_inode(obj->oo_inode);
3179 if (likely(result == 0)) {
3180 osd_attr_init(info, obj, attr, dof);
3181 osd_object_init0(obj);
3184 /* restore previous umask value */
3185 current->fs->umask = umask;
3187 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
3193 * Helper function for osd_create()
3195 * \retval 0, on success
3197 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
3198 const struct lu_fid *fid, struct thandle *th)
3200 struct osd_thread_info *info = osd_oti_get(env);
3201 struct osd_inode_id *id = &info->oti_id;
3202 struct osd_device *osd = osd_obj2dev(obj);
3203 struct osd_thandle *oh;
3206 LASSERT(obj->oo_inode != NULL);
3208 oh = container_of0(th, struct osd_thandle, ot_super);
3209 LASSERT(oh->ot_handle);
3210 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3212 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3213 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3214 OI_CHECK_FLD, NULL);
3215 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3220 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3221 u64 seq, struct lu_seq_range *range)
3223 struct seq_server_site *ss = osd_seq_site(osd);
3225 if (fid_seq_is_idif(seq)) {
3226 fld_range_set_ost(range);
3227 range->lsr_index = idif_ost_idx(seq);
3231 if (!fid_seq_in_fldb(seq)) {
3232 fld_range_set_mdt(range);
3234 /* FIXME: If ss is NULL, it suppose not get lsr_index
3236 range->lsr_index = ss->ss_node_id;
3240 LASSERT(ss != NULL);
3241 fld_range_set_any(range);
3242 /* OSD will only do local fld lookup */
3243 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3246 static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
3247 struct lu_attr *attr,
3248 struct dt_allocation_hint *hint,
3249 struct dt_object_format *dof,
3250 struct thandle *handle)
3252 struct osd_thandle *oh;
3256 LASSERT(handle != NULL);
3258 oh = container_of0(handle, struct osd_thandle, ot_super);
3259 LASSERT(oh->ot_handle == NULL);
3261 /* EA object consumes more credits than regular object: osd_mk_index
3262 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3263 * leaf_node, could involves the block, block bitmap, groups, GDT
3264 * change for each block, so add 4 * 2 credits in that case. */
3265 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3266 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3267 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3268 /* Reuse idle OI block may cause additional one OI block
3270 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3271 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3276 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid,
3277 attr->la_projid, 1, oh, osd_dt_obj(dt),
3278 NULL, OSD_QID_INODE);
3282 /* will help to find FID->ino mapping at dt_insert() */
3283 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3290 * Called to destroy on-disk representation of the object
3292 * Concurrency: must be locked
3294 static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
3297 struct osd_object *obj = osd_dt_obj(dt);
3298 struct inode *inode = obj->oo_inode;
3299 struct osd_thandle *oh;
3306 oh = container_of0(th, struct osd_thandle, ot_super);
3307 LASSERT(oh->ot_handle == NULL);
3309 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3310 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3312 /* For removing agent entry */
3313 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu))
3314 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3316 /* Recycle idle OI leaf may cause additional three OI blocks
3318 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3319 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3320 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3321 /* one less inode */
3322 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3323 i_projid_read(inode), -1, oh, obj, NULL,
3327 /* data to be truncated */
3328 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3329 i_projid_read(inode), 0, oh, obj, NULL,
3334 /* will help to find FID->ino when this object is being
3335 * added to PENDING/ */
3336 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3341 static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
3344 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3345 struct osd_object *obj = osd_dt_obj(dt);
3346 struct inode *inode = obj->oo_inode;
3347 struct osd_device *osd = osd_obj2dev(obj);
3348 struct osd_thandle *oh;
3352 oh = container_of0(th, struct osd_thandle, ot_super);
3353 LASSERT(oh->ot_handle);
3355 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3357 if (unlikely(fid_is_acct(fid)))
3360 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu)) {
3361 result = osd_delete_from_remote_parent(env, osd, obj, oh, true);
3363 CERROR("%s: remove agent entry "DFID": rc = %d\n",
3364 osd_name(osd), PFID(fid), result);
3367 if (S_ISDIR(inode->i_mode)) {
3368 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
3369 inode->i_nlink == 2);
3371 spin_lock(&obj->oo_guard);
3373 spin_unlock(&obj->oo_guard);
3374 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3377 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3379 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3381 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3382 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3383 oh->ot_handle, OI_CHECK_FLD);
3385 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3386 /* XXX: add to ext3 orphan list */
3387 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3389 /* not needed in the cache anymore */
3390 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3391 obj->oo_destroyed = 1;
3397 * Put the fid into lustre_mdt_attrs, and then place the structure
3398 * inode's ea. This fid should not be altered during the life time
3401 * \retval +ve, on success
3402 * \retval -ve, on error
3404 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3406 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3407 const struct lu_fid *fid, __u32 compat, __u32 incompat)
3409 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3410 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3414 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3417 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3420 lustre_loa_init(loa, fid, compat, incompat);
3421 lustre_loa_swab(loa, false);
3423 /* For the OST device with 256 bytes inode size by default,
3424 * the PFID EA will be stored together with LMA EA to avoid
3425 * performance trouble. Otherwise the PFID EA can be stored
3426 * independently. LU-8998 */
3427 if ((compat & LMAC_FID_ON_OST) &&
3428 LDISKFS_INODE_SIZE(inode->i_sb) <= 256)
3429 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3430 sizeof(*loa), XATTR_CREATE);
3432 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3433 sizeof(*lma), XATTR_CREATE);
3434 /* LMA may already exist, but we need to check that all the
3435 * desired compat/incompat flags have been added. */
3436 if (unlikely(rc == -EEXIST)) {
3437 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3438 XATTR_NAME_LMA, (void *)loa, sizeof(*loa));
3442 if (rc < sizeof(*lma))
3445 lustre_loa_swab(loa, true);
3446 if (lu_fid_eq(fid, &lma->lma_self_fid) &&
3447 ((compat == 0 && incompat == 0) ||
3448 (!(~lma->lma_compat & compat) &&
3449 !(~lma->lma_incompat & incompat))))
3452 lma->lma_self_fid = *fid;
3453 lma->lma_compat |= compat;
3454 lma->lma_incompat |= incompat;
3455 if (rc == sizeof(*lma)) {
3456 lustre_lma_swab(lma);
3457 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3458 sizeof(*lma), XATTR_REPLACE);
3460 lustre_loa_swab(loa, false);
3461 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3462 sizeof(*loa), XATTR_REPLACE);
3470 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3471 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3472 * To have compatilibility with 1.8 ldiskfs driver we need to have
3473 * magic number at start of fid data.
3474 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3477 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3478 const struct lu_fid *fid)
3480 if (!fid_is_namespace_visible(fid) ||
3481 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3482 param->edp_magic = 0;
3486 param->edp_magic = LDISKFS_LUFID_MAGIC;
3487 param->edp_len = sizeof(struct lu_fid) + 1;
3488 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3492 * Try to read the fid from inode ea into dt_rec.
3494 * \param fid object fid.
3496 * \retval 0 on success
3498 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3499 __u32 ino, struct lu_fid *fid,
3500 struct osd_inode_id *id)
3502 struct osd_thread_info *info = osd_oti_get(env);
3503 struct inode *inode;
3506 osd_id_gen(id, ino, OSD_OII_NOGEN);
3507 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3509 RETURN(PTR_ERR(inode));
3515 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3517 struct inode *parent_dir,
3518 const struct lu_fid *dot_fid,
3519 const struct lu_fid *dot_dot_fid,
3520 struct osd_thandle *oth)
3522 struct ldiskfs_dentry_param *dot_ldp;
3523 struct ldiskfs_dentry_param *dot_dot_ldp;
3524 __u32 saved_nlink = dir->i_nlink;
3527 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3528 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3530 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3531 dot_ldp->edp_magic = 0;
3533 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3534 dir, dot_ldp, dot_dot_ldp);
3535 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3536 * the subseqent ref_add() will increase the dir->i_nlink
3537 * as 3. That is incorrect for new created directory.
3539 * It looks like hack, because we want to make the OSD API
3540 * to be order-independent for new created directory object
3541 * between dt_insert(..) and ref_add() operations.
3543 * Here, we only restore the in-RAM dir-inode's nlink attr,
3544 * becuase if the nlink attr is not 2, then there will be
3545 * ref_add() called following the dt_insert(..), such call
3546 * will make both the in-RAM and on-disk dir-inode's nlink
3547 * attr to be set as 2. LU-7447 */
3548 set_nlink(dir, saved_nlink);
3553 * Create an local agent inode for remote entry
3555 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3556 struct osd_device *osd,
3557 struct osd_object *pobj,
3558 const struct lu_fid *fid,
3562 struct osd_thread_info *info = osd_oti_get(env);
3563 struct inode *local;
3564 struct osd_thandle *oh;
3569 oh = container_of(th, struct osd_thandle, ot_super);
3570 LASSERT(oh->ot_handle->h_transaction != NULL);
3572 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
3573 if (IS_ERR(local)) {
3574 CERROR("%s: create local error %d\n", osd_name(osd),
3575 (int)PTR_ERR(local));
3579 /* restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
3580 * correct gid on remote file, not agent here */
3581 local->i_gid = current_fsgid();
3582 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3583 unlock_new_inode(local);
3585 /* Agent inode should not have project ID */
3586 #ifdef HAVE_PROJECT_QUOTA
3587 if (LDISKFS_I(pobj->oo_inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
3588 i_projid_read(pobj->oo_inode) != 0) {
3589 rc = osd_transfer_project(local, 0);
3591 CERROR("%s: quota transfer failed: rc = %d. Is project "
3592 "quota enforcement enabled on the ldiskfs "
3593 "filesystem?\n", local->i_sb->s_id, rc);
3594 RETURN(ERR_PTR(rc));
3598 /* Set special LMA flag for local agent inode */
3599 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3601 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3602 osd_name(osd), PFID(fid), rc);
3603 RETURN(ERR_PTR(rc));
3609 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3610 lu_object_fid(&pobj->oo_dt.do_lu),
3613 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3614 osd_name(osd), PFID(fid), rc);
3615 RETURN(ERR_PTR(rc));
3622 * when direntry is deleted, we have to take care of possible agent inode
3623 * referenced by that. unfortunately we can't do this at that point:
3624 * iget() within a running transaction leads to deadlock and we better do
3625 * not call that every delete declaration to save performance. so we put
3626 * a potention agent inode on a list and process that once the transaction
3627 * is over. Notice it's not any worse in terms of real orphans as regular
3628 * object destroy doesn't put inodes on the on-disk orphan list. this should
3629 * be addressed separately
3631 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3632 struct osd_thandle *oh,
3635 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3636 struct osd_obj_orphan *oor;
3643 oor->oor_env = (struct lu_env *)env;
3644 spin_lock(&osd->od_osfs_lock);
3645 list_add(&oor->oor_list, &osd->od_orphan_list);
3646 spin_unlock(&osd->od_osfs_lock);
3648 oh->ot_remove_agents = 1;
3654 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3655 struct osd_device *osd)
3657 struct osd_thread_info *info = osd_oti_get(env);
3658 struct osd_obj_orphan *oor, *tmp;
3659 struct osd_inode_id id;
3660 struct list_head list;
3661 struct inode *inode;
3666 INIT_LIST_HEAD(&list);
3668 spin_lock(&osd->od_osfs_lock);
3669 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3670 if (oor->oor_env == env) {
3671 list_del(&oor->oor_list);
3672 list_add(&oor->oor_list, &list);
3675 spin_unlock(&osd->od_osfs_lock);
3677 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3681 list_del(&oor->oor_list);
3684 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3685 inode = osd_iget_fid(info, osd, &id, &fid);
3689 if (!osd_remote_fid(env, osd, &fid)) {
3694 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3696 mark_inode_dirty(inode);
3697 ldiskfs_journal_stop(jh);
3705 * OSD layer object create function for OST objects (b=11826).
3707 * The FID is inserted into inode xattr here.
3709 * \retval 0, on success
3710 * \retval -ve, on error
3712 static int osd_create(const struct lu_env *env, struct dt_object *dt,
3713 struct lu_attr *attr, struct dt_allocation_hint *hint,
3714 struct dt_object_format *dof, struct thandle *th)
3716 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3717 struct osd_object *obj = osd_dt_obj(dt);
3718 struct osd_thread_info *info = osd_oti_get(env);
3719 int result, on_ost = 0;
3723 if (dt_object_exists(dt))
3726 LINVRNT(osd_invariant(obj));
3727 LASSERT(!dt_object_remote(dt));
3728 LASSERT(osd_is_write_locked(env, obj));
3729 LASSERT(th != NULL);
3731 if (unlikely(fid_is_acct(fid)))
3732 /* Quota files can't be created from the kernel any more,
3733 * 'tune2fs -O quota' will take care of creating them */
3736 result = __osd_create(info, obj, attr, hint, dof, th);
3738 if (fid_is_idif(fid) &&
3739 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3740 struct lu_fid *tfid = &info->oti_fid;
3741 struct ost_id *oi = &info->oti_ostid;
3743 fid_to_ostid(fid, oi);
3744 ostid_to_fid(tfid, oi, 0);
3746 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3747 LMAC_FID_ON_OST, 0);
3749 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3751 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3752 on_ost ? LMAC_FID_ON_OST : 0,
3755 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3756 obj->oo_dt.do_body_ops = &osd_body_ops;
3759 if (!result && !CFS_FAIL_CHECK(OBD_FAIL_OSD_NO_OI_ENTRY))
3760 result = __osd_oi_insert(env, obj, fid, th);
3762 /* a small optimization - dt_insert() isn't usually applied
3763 * to OST objects, so we don't need to cache OI mapping for
3765 if (result == 0 && on_ost == 0) {
3766 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3767 result = osd_idc_find_and_init(env, osd, obj);
3768 LASSERT(result == 0);
3771 LASSERT(ergo(result == 0,
3772 dt_object_exists(dt) && !dt_object_remote(dt)));
3773 LINVRNT(osd_invariant(obj));
3777 static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
3778 struct thandle *handle)
3780 struct osd_thandle *oh;
3782 /* it's possible that object doesn't exist yet */
3783 LASSERT(handle != NULL);
3785 oh = container_of0(handle, struct osd_thandle, ot_super);
3786 LASSERT(oh->ot_handle == NULL);
3788 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3789 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3791 osd_idc_find_and_init(env, osd_dev(dt->do_lu.lo_dev), osd_dt_obj(dt));
3797 * Concurrency: @dt is write locked.
3799 static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
3802 struct osd_object *obj = osd_dt_obj(dt);
3803 struct inode *inode = obj->oo_inode;
3804 struct osd_thandle *oh;
3807 if (!dt_object_exists(dt) || obj->oo_destroyed)
3810 LINVRNT(osd_invariant(obj));
3811 LASSERT(!dt_object_remote(dt));
3812 LASSERT(osd_is_write_locked(env, obj));
3813 LASSERT(th != NULL);
3815 oh = container_of0(th, struct osd_thandle, ot_super);
3816 LASSERT(oh->ot_handle != NULL);
3818 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3820 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3821 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3823 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3824 * (65000) subdirectories by storing "1" in i_nlink if the link count
3825 * would otherwise overflow. Directory tranversal tools understand
3826 * that (st_nlink == 1) indicates that the filesystem dose not track
3827 * hard links count on the directory, and will not abort subdirectory
3828 * scanning early once (st_nlink - 2) subdirs have been found.
3830 * This also has to properly handle the case of inodes with nlink == 0
3831 * in case they are being linked into the PENDING directory
3833 spin_lock(&obj->oo_guard);
3834 if (unlikely(inode->i_nlink == 0))
3835 /* inc_nlink from 0 may cause WARN_ON */
3836 set_nlink(inode, 1);
3838 ldiskfs_inc_count(oh->ot_handle, inode);
3839 if (!S_ISDIR(inode->i_mode))
3840 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3842 spin_unlock(&obj->oo_guard);
3844 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3845 LINVRNT(osd_invariant(obj));
3847 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3852 static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
3853 struct thandle *handle)
3855 struct osd_thandle *oh;
3857 if (!dt_object_exists(dt))
3860 LASSERT(!dt_object_remote(dt));
3861 LASSERT(handle != NULL);
3863 oh = container_of0(handle, struct osd_thandle, ot_super);
3864 LASSERT(oh->ot_handle == NULL);
3866 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3867 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3873 * Concurrency: @dt is write locked.
3875 static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
3878 struct osd_object *obj = osd_dt_obj(dt);
3879 struct inode *inode = obj->oo_inode;
3880 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3881 struct osd_thandle *oh;
3883 if (!dt_object_exists(dt))
3886 LINVRNT(osd_invariant(obj));
3887 LASSERT(!dt_object_remote(dt));
3888 LASSERT(osd_is_write_locked(env, obj));
3889 LASSERT(th != NULL);
3891 oh = container_of0(th, struct osd_thandle, ot_super);
3892 LASSERT(oh->ot_handle != NULL);
3894 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3896 spin_lock(&obj->oo_guard);
3897 /* That can be result of upgrade from old Lustre version and
3898 * applied only to local files. Just skip this ref_del call.
3899 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3900 if (inode->i_nlink == 0) {
3901 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3902 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3903 ", maybe an upgraded file? (LU-3915)\n",
3904 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3905 spin_unlock(&obj->oo_guard);
3909 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3910 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3912 ldiskfs_dec_count(oh->ot_handle, inode);
3913 spin_unlock(&obj->oo_guard);
3915 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3916 LINVRNT(osd_invariant(obj));
3918 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3924 * Concurrency: @dt is read locked.
3926 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3927 struct lu_buf *buf, const char *name)
3929 struct osd_object *obj = osd_dt_obj(dt);
3930 struct inode *inode = obj->oo_inode;
3931 struct osd_thread_info *info = osd_oti_get(env);
3932 struct dentry *dentry = &info->oti_obj_dentry;
3933 bool cache_xattr = false;
3938 /* version get is not real XATTR but uses xattr API */
3939 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3940 dt_obj_version_t *ver = buf->lb_buf;
3942 /* for version we are just using xattr API but change inode
3944 if (buf->lb_len == 0)
3945 return sizeof(dt_obj_version_t);
3947 if (buf->lb_len < sizeof(dt_obj_version_t))
3950 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
3951 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3953 *ver = LDISKFS_I(inode)->i_fs_version;
3955 return sizeof(dt_obj_version_t);
3958 if (!dt_object_exists(dt))
3961 LASSERT(!dt_object_remote(dt));
3962 LASSERT(inode->i_op != NULL);
3963 #ifdef HAVE_IOP_XATTR
3964 LASSERT(inode->i_op->getxattr != NULL);
3967 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
3968 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
3972 rc = osd_oxc_get(obj, name, buf);
3977 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
3978 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3979 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3980 struct filter_fid *ff;
3981 struct ost_layout *ol;
3983 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
3987 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
3990 if (buf->lb_len == 0 || !buf->lb_buf)
3993 if (buf->lb_len < rc)
3997 ol = &ff->ff_layout;
3998 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
3999 PFID_STRIPE_IDX_BITS);
4000 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
4001 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
4002 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
4003 if (lma->lma_compat & LMAC_COMP_INFO) {
4004 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
4005 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
4006 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
4008 ol->ol_comp_start = 0;
4009 ol->ol_comp_end = 0;
4013 rc = __osd_xattr_get(inode, dentry, name,
4014 buf->lb_buf, buf->lb_len);
4018 if (rc == -ENOENT || rc == -ENODATA)
4019 osd_oxc_add(obj, name, NULL, 0);
4020 else if (rc > 0 && buf->lb_buf != NULL)
4021 osd_oxc_add(obj, name, buf->lb_buf, rc);
4027 static int osd_declare_xattr_set(const struct lu_env *env,
4028 struct dt_object *dt,
4029 const struct lu_buf *buf, const char *name,
4030 int fl, struct thandle *handle)
4032 struct osd_thandle *oh;
4034 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4036 LASSERT(handle != NULL);
4038 oh = container_of0(handle, struct osd_thandle, ot_super);
4039 LASSERT(oh->ot_handle == NULL);
4041 if (strcmp(name, XATTR_NAME_LMA) == 0) {
4042 /* For non-upgrading case, the LMA is set first and
4043 * usually fit inode. But for upgrade case, the LMA
4044 * may be in another separated EA block. */
4045 if (dt_object_exists(dt)) {
4046 if (fl == LU_XATTR_REPLACE)
4051 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4053 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4054 /* We may need to delete the old PFID EA. */
4055 credits = LDISKFS_MAXQUOTAS_DEL_BLOCKS(sb);
4056 if (fl == LU_XATTR_REPLACE)
4061 /* If some name entry resides on remote MDT, then will create
4062 * agent entry under remote parent. On the other hand, if the
4063 * remote entry will be removed, then related agent entry may
4064 * need to be removed from the remote parent. So there may be
4065 * kinds of cases, let's declare enough credits. The credits
4066 * for create agent entry is enough for remove case. */
4067 if (strcmp(name, XATTR_NAME_LINK) == 0) {
4068 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4069 if (dt_object_exists(dt))
4070 credits += 1; /* For updating LMA */
4074 credits += osd_dto_credits_noquota[DTO_XATTR_SET];
4079 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
4080 /* learn xattr size from osd_xattr_get if
4081 attribute has not been read yet */
4082 buflen = __osd_xattr_get(
4083 osd_dt_obj(dt)->oo_inode,
4084 &osd_oti_get(env)->oti_obj_dentry,
4089 buflen = buf->lb_len;
4092 if (buflen > sb->s_blocksize) {
4093 credits += osd_calc_bkmap_credits(
4095 (buflen + sb->s_blocksize - 1) >>
4096 sb->s_blocksize_bits);
4100 * xattr set may involve inode quota change, reserve credits for
4101 * dquot_initialize()
4103 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4106 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
4111 static int osd_xattr_set_pfid(const struct lu_env *env, struct osd_object *obj,
4112 const struct lu_buf *buf, int fl,
4113 struct thandle *handle)
4115 struct osd_thread_info *info = osd_oti_get(env);
4116 struct dentry *dentry = &info->oti_obj_dentry;
4117 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4118 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4119 struct inode *inode = obj->oo_inode;
4120 struct filter_fid *ff = buf->lb_buf;
4121 struct ost_layout *ol = &ff->ff_layout;
4122 int flags = XATTR_REPLACE;
4126 if (buf->lb_len != sizeof(*ff) && buf->lb_len != sizeof(struct lu_fid))
4129 rc = osd_get_lma(info, inode, dentry, loa);
4130 if (rc == -ENODATA) {
4131 /* Usually for upgarding from old device */
4132 lustre_loa_init(loa, lu_object_fid(&obj->oo_dt.do_lu),
4133 LMAC_FID_ON_OST, 0);
4134 flags = XATTR_CREATE;
4139 if (!rc && lma->lma_compat & LMAC_STRIPE_INFO) {
4140 if ((fl & LU_XATTR_CREATE) && !(fl & LU_XATTR_REPLACE))
4143 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256) {
4144 /* Separate PFID EA from LMA */
4145 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4146 lustre_lma_swab(lma);
4147 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4148 sizeof(*lma), XATTR_REPLACE);
4150 obj->oo_pfid_in_lma = 0;
4151 rc = LU_XATTR_CREATE;
4157 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256)
4160 /* Old client does not send stripe information,
4161 * then store the PFID EA on disk separatedly. */
4162 if (unlikely(buf->lb_len == sizeof(struct lu_fid) ||
4163 ol->ol_stripe_size == 0))
4166 /* Remove old PFID EA entry firstly. */
4167 ll_vfs_dq_init(inode);
4168 rc = osd_removexattr(dentry, inode, XATTR_NAME_FID);
4169 if (rc == -ENODATA) {
4170 if ((fl & LU_XATTR_REPLACE) && !(fl & LU_XATTR_CREATE))
4177 fid_le_to_cpu(&loa->loa_parent_fid, &ff->ff_parent);
4178 if (likely(ol->ol_stripe_size != 0)) {
4179 loa->loa_parent_fid.f_ver |= le32_to_cpu(ol->ol_stripe_count) <<
4180 PFID_STRIPE_IDX_BITS;
4181 loa->loa_stripe_size = le32_to_cpu(ol->ol_stripe_size);
4182 lma->lma_compat |= LMAC_STRIPE_INFO;
4183 if (ol->ol_comp_id != 0) {
4184 loa->loa_comp_id = le32_to_cpu(ol->ol_comp_id);
4185 loa->loa_comp_start = le64_to_cpu(ol->ol_comp_start);
4186 loa->loa_comp_end = le64_to_cpu(ol->ol_comp_end);
4187 lma->lma_compat |= LMAC_COMP_INFO;
4191 lustre_loa_swab(loa, false);
4193 /* Store the PFID EA inside LMA. */
4194 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa, sizeof(*loa),
4197 obj->oo_pfid_in_lma = 1;
4203 * In DNE environment, the object (in spite of regular file or directory)
4204 * and its name entry may reside on different MDTs. Under such case, we will
4205 * create an agent entry on the MDT where the object resides. The agent entry
4206 * references the object locally, that makes the object to be visible to the
4207 * userspace when mounted as 'ldiskfs' directly. Then the userspace tools,
4208 * such as 'tar' can handle the object properly.
4210 * We handle the agent entry during set linkEA that is the common interface
4211 * for both regular file and directroy, can handle kinds of cases, such as
4212 * create/link/unlink/rename, and so on.
4214 * NOTE: we can NOT do that when ea_{insert,delete} that is only for directory.
4216 * XXX: There are two known issues:
4217 * 1. For one object, we will create at most one agent entry even if there
4218 * may be more than one cross-MDTs hard links on the object. So the local
4219 * e2fsck may claim that the object's nlink is larger than the name entries
4220 * that reference such inode. And in further, the e2fsck will fix the nlink
4221 * attribute to match the local references. Then it will cause the object's
4222 * nlink attribute to be inconsistent with the global references. it is bad
4223 * but not fatal. The ref_del() can handle the zero-referenced case. On the
4224 * other hand, the global namespace LFSCK can repair the object's attribute
4225 * according to the linkEA.
4226 * 2. There may be too many hard links on the object as to its linkEA overflow,
4227 * then the linkEA entry for cross-MDTs reference may be discarded. If such
4228 * case happened, then at this point, we do not know whether there are some
4229 * cross-MDTs reference. But there are local references, it guarantees that
4230 * object is visible to userspace when mounted as 'ldiskfs'. That is enough.
4232 static int osd_xattr_handle_linkea(const struct lu_env *env,
4233 struct osd_device *osd,
4234 struct osd_object *obj,
4235 const struct lu_buf *buf,
4236 struct thandle *handle)
4238 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
4239 struct lu_fid *tfid = &osd_oti_get(env)->oti_fid3;
4240 struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
4241 struct lu_name tmpname;
4242 struct osd_thandle *oh;
4244 bool remote = false;
4247 oh = container_of0(handle, struct osd_thandle, ot_super);
4248 LASSERT(oh->ot_handle != NULL);
4250 rc = linkea_init_with_rec(&ldata);
4252 linkea_first_entry(&ldata);
4253 while (ldata.ld_lee != NULL && !remote) {
4254 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
4256 if (osd_remote_fid(env, osd, tfid) > 0)
4259 linkea_next_entry(&ldata);
4261 } else if (rc == -ENODATA) {
4267 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
4268 rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
4270 CERROR("%s: failed to remove agent entry for "DFID
4271 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4272 } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
4273 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4275 CERROR("%s: failed to create agent entry for "DFID
4276 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4283 * Concurrency: @dt is write locked.
4285 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
4286 const struct lu_buf *buf, const char *name, int fl,
4287 struct thandle *handle)
4289 struct osd_object *obj = osd_dt_obj(dt);
4290 struct osd_device *osd = osd_obj2dev(obj);
4291 struct inode *inode = obj->oo_inode;
4292 struct osd_thread_info *info = osd_oti_get(env);
4301 /* version set is not real XATTR */
4302 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4303 dt_obj_version_t *version = buf->lb_buf;
4305 /* for version we are just using xattr API but change inode
4307 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
4309 CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
4310 *version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4312 LDISKFS_I(inode)->i_fs_version = *version;
4313 /* Version is set after all inode operations are finished,
4314 * so we should mark it dirty here */
4315 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
4320 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
4321 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
4324 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4326 /* For the OST device with 256 bytes inode size by default,
4327 * the PFID EA will be stored together with LMA EA to avoid
4328 * performance trouble. Otherwise the PFID EA can be stored
4329 * independently. LU-8998 */
4330 if (strcmp(name, XATTR_NAME_FID) == 0 && osd->od_is_ost &&
4331 (LDISKFS_INODE_SIZE(inode->i_sb) <= 256 || obj->oo_pfid_in_lma)) {
4332 LASSERT(buf->lb_buf);
4334 fl = osd_xattr_set_pfid(env, obj, buf, fl, handle);
4337 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
4338 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4339 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4341 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4345 lma->lma_incompat |= LMAI_STRIPED;
4346 lustre_lma_swab(lma);
4347 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4348 sizeof(*lma), XATTR_REPLACE);
4351 } else if (strcmp(name, XATTR_NAME_LINK) == 0) {
4352 LASSERT(!osd->od_is_ost);
4354 rc = osd_xattr_handle_linkea(env, osd, obj, buf, handle);
4359 if (fl & LU_XATTR_REPLACE)
4360 fs_flags |= XATTR_REPLACE;
4362 if (fl & LU_XATTR_CREATE)
4363 fs_flags |= XATTR_CREATE;
4365 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, len, fs_flags);
4366 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4369 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4370 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4371 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
4377 * Concurrency: @dt is read locked.
4379 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
4380 const struct lu_buf *buf)
4382 struct osd_object *obj = osd_dt_obj(dt);
4383 struct inode *inode = obj->oo_inode;
4384 struct osd_thread_info *info = osd_oti_get(env);
4385 struct dentry *dentry = &info->oti_obj_dentry;
4387 if (!dt_object_exists(dt))
4390 LASSERT(!dt_object_remote(dt));
4391 LASSERT(inode->i_op != NULL);
4392 LASSERT(inode->i_op->listxattr != NULL);
4394 dentry->d_inode = inode;
4395 dentry->d_sb = inode->i_sb;
4396 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
4399 static int osd_declare_xattr_del(const struct lu_env *env,
4400 struct dt_object *dt, const char *name,
4401 struct thandle *handle)
4403 struct osd_thandle *oh;
4404 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4406 LASSERT(!dt_object_remote(dt));
4407 LASSERT(handle != NULL);
4409 oh = container_of0(handle, struct osd_thandle, ot_super);
4410 LASSERT(oh->ot_handle == NULL);
4412 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
4413 osd_dto_credits_noquota[DTO_XATTR_SET]);
4415 * xattr del may involve inode quota change, reserve credits for
4416 * dquot_initialize()
4418 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4424 * Concurrency: @dt is write locked.
4426 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
4427 const char *name, struct thandle *handle)
4429 struct osd_object *obj = osd_dt_obj(dt);
4430 struct inode *inode = obj->oo_inode;
4431 struct osd_thread_info *info = osd_oti_get(env);
4432 struct dentry *dentry = &info->oti_obj_dentry;
4435 if (!dt_object_exists(dt))
4438 LASSERT(!dt_object_remote(dt));
4439 LASSERT(inode->i_op != NULL);
4440 LASSERT(handle != NULL);
4441 #ifdef HAVE_IOP_XATTR
4442 LASSERT(inode->i_op->removexattr != NULL);
4445 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4447 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
4448 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
4450 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
4451 &info->oti_ost_attrs);
4453 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
4455 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4456 lustre_lma_swab(lma);
4457 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4458 sizeof(*lma), XATTR_REPLACE);
4460 obj->oo_pfid_in_lma = 0;
4463 ll_vfs_dq_init(inode);
4464 dentry->d_inode = inode;
4465 dentry->d_sb = inode->i_sb;
4466 rc = osd_removexattr(dentry, inode, name);
4469 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4472 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4473 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4474 osd_oxc_del(obj, name);
4479 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
4480 __u64 start, __u64 end)
4482 struct osd_object *obj = osd_dt_obj(dt);
4483 struct inode *inode = obj->oo_inode;
4484 struct osd_thread_info *info = osd_oti_get(env);
4485 struct dentry *dentry = &info->oti_obj_dentry;
4486 struct file *file = &info->oti_file;
4491 dentry->d_inode = inode;
4492 dentry->d_sb = inode->i_sb;
4493 file->f_path.dentry = dentry;
4494 file->f_mapping = inode->i_mapping;
4495 file->f_op = inode->i_fop;
4496 set_file_inode(file, inode);
4498 rc = ll_vfs_fsync_range(file, start, end, 0);
4503 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4512 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4513 const struct dt_index_features *feat)
4515 struct iam_descr *descr;
4517 if (osd_object_is_root(o))
4518 return feat == &dt_directory_features;
4520 LASSERT(o->oo_dir != NULL);
4522 descr = o->oo_dir->od_container.ic_descr;
4523 if (feat == &dt_directory_features) {
4524 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4530 feat->dif_keysize_min <= descr->id_key_size &&
4531 descr->id_key_size <= feat->dif_keysize_max &&
4532 feat->dif_recsize_min <= descr->id_rec_size &&
4533 descr->id_rec_size <= feat->dif_recsize_max &&
4534 !(feat->dif_flags & (DT_IND_VARKEY |
4535 DT_IND_VARREC | DT_IND_NONUNQ)) &&
4536 ergo(feat->dif_flags & DT_IND_UPDATE,
4537 1 /* XXX check that object (and file system) is
4542 static int osd_iam_container_init(const struct lu_env *env,
4543 struct osd_object *obj,
4544 struct osd_directory *dir)
4546 struct iam_container *bag = &dir->od_container;
4549 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
4553 result = iam_container_setup(bag);
4555 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
4557 iam_container_fini(bag);
4564 * Concurrency: no external locking is necessary.
4566 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
4567 const struct dt_index_features *feat)
4571 struct osd_object *obj = osd_dt_obj(dt);
4573 LINVRNT(osd_invariant(obj));
4575 if (osd_object_is_root(obj)) {
4576 dt->do_index_ops = &osd_index_ea_ops;
4578 } else if (feat == &dt_directory_features) {
4579 dt->do_index_ops = &osd_index_ea_ops;
4580 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
4585 } else if (unlikely(feat == &dt_otable_features)) {
4586 dt->do_index_ops = &osd_otable_ops;
4588 } else if (unlikely(feat == &dt_acct_features)) {
4589 dt->do_index_ops = &osd_acct_index_ops;
4592 } else if (!osd_has_index(obj)) {
4593 struct osd_directory *dir;
4594 struct osd_device *osd = osd_obj2dev(obj);
4595 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
4600 spin_lock(&obj->oo_guard);
4601 if (obj->oo_dir == NULL)
4605 * Concurrent thread allocated container data.
4608 spin_unlock(&obj->oo_guard);
4610 * Now, that we have container data, serialize its
4613 down_write(&obj->oo_ext_idx_sem);
4615 * recheck under lock.
4618 if (osd_has_index(obj)) {
4623 result = osd_iam_container_init(env, obj, obj->oo_dir);
4624 if (result || feat == &dt_lfsck_namespace_features ||
4625 feat == &dt_lfsck_layout_orphan_features ||
4626 feat == &dt_lfsck_layout_dangling_features)
4629 result = osd_index_register(osd, fid,
4630 feat->dif_keysize_max,
4631 feat->dif_recsize_max);
4633 CWARN("%s: failed to register index "
4635 osd_name(osd), PFID(fid), result);
4636 else if (result > 0)
4639 CDEBUG(D_LFSCK, "%s: index object "DFID
4640 " (%d/%d) registered\n",
4641 osd_name(osd), PFID(fid),
4642 (int)feat->dif_keysize_max,
4643 (int)feat->dif_recsize_max);
4646 up_write(&obj->oo_ext_idx_sem);
4654 if (result == 0 && skip_iam == 0) {
4655 if (!osd_iam_index_probe(env, obj, feat))
4658 LINVRNT(osd_invariant(obj));
4663 static int osd_otable_it_attr_get(const struct lu_env *env,
4664 struct dt_object *dt,
4665 struct lu_attr *attr)
4671 static const struct dt_object_operations osd_obj_ops = {
4672 .do_read_lock = osd_read_lock,
4673 .do_write_lock = osd_write_lock,
4674 .do_read_unlock = osd_read_unlock,
4675 .do_write_unlock = osd_write_unlock,
4676 .do_write_locked = osd_write_locked,
4677 .do_attr_get = osd_attr_get,
4678 .do_declare_attr_set = osd_declare_attr_set,
4679 .do_attr_set = osd_attr_set,
4680 .do_ah_init = osd_ah_init,
4681 .do_declare_create = osd_declare_create,
4682 .do_create = osd_create,
4683 .do_declare_destroy = osd_declare_destroy,
4684 .do_destroy = osd_destroy,
4685 .do_index_try = osd_index_try,
4686 .do_declare_ref_add = osd_declare_ref_add,
4687 .do_ref_add = osd_ref_add,
4688 .do_declare_ref_del = osd_declare_ref_del,
4689 .do_ref_del = osd_ref_del,
4690 .do_xattr_get = osd_xattr_get,
4691 .do_declare_xattr_set = osd_declare_xattr_set,
4692 .do_xattr_set = osd_xattr_set,
4693 .do_declare_xattr_del = osd_declare_xattr_del,
4694 .do_xattr_del = osd_xattr_del,
4695 .do_xattr_list = osd_xattr_list,
4696 .do_object_sync = osd_object_sync,
4697 .do_invalidate = osd_invalidate,
4700 static const struct dt_object_operations osd_obj_otable_it_ops = {
4701 .do_attr_get = osd_otable_it_attr_get,
4702 .do_index_try = osd_index_try,
4705 static int osd_index_declare_iam_delete(const struct lu_env *env,
4706 struct dt_object *dt,
4707 const struct dt_key *key,
4708 struct thandle *handle)
4710 struct osd_thandle *oh;
4712 oh = container_of0(handle, struct osd_thandle, ot_super);
4713 LASSERT(oh->ot_handle == NULL);
4715 /* Recycle may cause additional three blocks to be changed. */
4716 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
4717 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
4723 * delete a (key, value) pair from index \a dt specified by \a key
4725 * \param dt osd index object
4726 * \param key key for index
4727 * \param rec record reference
4728 * \param handle transaction handler
4731 * \retval -ve failure
4733 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
4734 const struct dt_key *key,
4735 struct thandle *handle)
4737 struct osd_thread_info *oti = osd_oti_get(env);
4738 struct osd_object *obj = osd_dt_obj(dt);
4739 struct osd_thandle *oh;
4740 struct iam_path_descr *ipd;
4741 struct iam_container *bag = &obj->oo_dir->od_container;
4745 if (!dt_object_exists(dt))
4748 LINVRNT(osd_invariant(obj));
4749 LASSERT(!dt_object_remote(dt));
4750 LASSERT(bag->ic_object == obj->oo_inode);
4751 LASSERT(handle != NULL);
4753 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4755 ipd = osd_idx_ipd_get(env, bag);
4756 if (unlikely(ipd == NULL))
4759 oh = container_of0(handle, struct osd_thandle, ot_super);
4760 LASSERT(oh->ot_handle != NULL);
4761 LASSERT(oh->ot_handle->h_transaction != NULL);
4763 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4764 /* swab quota uid/gid provided by caller */
4765 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4766 key = (const struct dt_key *)&oti->oti_quota_id;
4769 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
4770 osd_ipd_put(env, bag, ipd);
4771 LINVRNT(osd_invariant(obj));
4772 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4776 static int osd_index_declare_ea_delete(const struct lu_env *env,
4777 struct dt_object *dt,
4778 const struct dt_key *key,
4779 struct thandle *handle)
4781 struct osd_thandle *oh;
4782 struct inode *inode;
4786 LASSERT(!dt_object_remote(dt));
4787 LASSERT(handle != NULL);
4789 oh = container_of0(handle, struct osd_thandle, ot_super);
4790 LASSERT(oh->ot_handle == NULL);
4792 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
4793 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
4795 inode = osd_dt_obj(dt)->oo_inode;
4799 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
4800 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
4805 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
4808 struct osd_fid_pack *rec;
4811 if (de->file_type & LDISKFS_DIRENT_LUFID) {
4812 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
4813 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
4814 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
4820 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
4821 const struct lu_fid *fid)
4823 struct seq_server_site *ss = osd_seq_site(osd);
4826 /* FID seqs not in FLDB, must be local seq */
4827 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
4830 /* If FLD is not being initialized yet, it only happens during the
4831 * initialization, likely during mgs initialization, and we assume
4832 * this is local FID. */
4833 if (ss == NULL || ss->ss_server_fld == NULL)
4836 /* Only check the local FLDB here */
4837 if (osd_seq_exists(env, osd, fid_seq(fid)))
4843 static void osd_take_care_of_agent(const struct lu_env *env,
4844 struct osd_device *osd,
4845 struct osd_thandle *oh,
4846 struct ldiskfs_dir_entry_2 *de)
4848 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
4849 struct osd_idmap_cache *idc;
4850 int rc, schedule = 0;
4852 LASSERT(de != NULL);
4854 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4855 if (likely(rc == 0)) {
4856 idc = osd_idc_find_or_init(env, osd, fid);
4857 if (IS_ERR(idc) || idc->oic_remote)
4859 } else if (rc == -ENODATA) {
4860 /* can't get FID, postpone to the end of the
4861 * transaction when iget() is safe */
4864 CERROR("%s: can't get FID: rc = %d\n", osd_name(osd), rc);
4867 osd_schedule_agent_inode_removal(env, oh,
4868 le32_to_cpu(de->inode));
4872 * Index delete function for interoperability mode (b11826).
4873 * It will remove the directory entry added by osd_index_ea_insert().
4874 * This entry is needed to maintain name->fid mapping.
4876 * \param key, key i.e. file entry to be deleted
4878 * \retval 0, on success
4879 * \retval -ve, on error
4881 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
4882 const struct dt_key *key, struct thandle *handle)
4884 struct osd_object *obj = osd_dt_obj(dt);
4885 struct inode *dir = obj->oo_inode;
4886 struct dentry *dentry;
4887 struct osd_thandle *oh;
4888 struct ldiskfs_dir_entry_2 *de = NULL;
4889 struct buffer_head *bh;
4890 struct htree_lock *hlock = NULL;
4891 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4895 if (!dt_object_exists(dt))
4898 LINVRNT(osd_invariant(obj));
4899 LASSERT(!dt_object_remote(dt));
4900 LASSERT(handle != NULL);
4902 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4904 oh = container_of(handle, struct osd_thandle, ot_super);
4905 LASSERT(oh->ot_handle != NULL);
4906 LASSERT(oh->ot_handle->h_transaction != NULL);
4908 ll_vfs_dq_init(dir);
4909 dentry = osd_child_dentry_get(env, obj,
4910 (char *)key, strlen((char *)key));
4912 if (obj->oo_hl_head != NULL) {
4913 hlock = osd_oti_get(env)->oti_hlock;
4914 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4915 dir, LDISKFS_HLOCK_DEL);
4917 down_write(&obj->oo_ext_idx_sem);
4920 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4922 /* If this is not the ".." entry, it might be a remote DNE
4923 * entry and we need to check if the FID is for a remote
4924 * MDT. If the FID is not in the directory entry (e.g.
4925 * upgraded 1.8 filesystem without dirdata enabled) then
4926 * we need to get the FID from the LMA. For a remote directory
4927 * there HAS to be an LMA, it cannot be an IGIF inode in this
4930 * Delete the entry before the agent inode in order to
4931 * simplify error handling. At worst an error after deleting
4932 * the entry first might leak the agent inode afterward. The
4933 * reverse would need filesystem abort in case of error deleting
4934 * the entry after the agent had been removed, or leave a
4935 * dangling entry pointing at a random inode. */
4936 if (strcmp((char *)key, dotdot) != 0)
4937 osd_take_care_of_agent(env, osd, oh, de);
4938 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4944 ldiskfs_htree_unlock(hlock);
4946 up_write(&obj->oo_ext_idx_sem);
4951 LASSERT(osd_invariant(obj));
4952 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4957 * Lookup index for \a key and copy record to \a rec.
4959 * \param dt osd index object
4960 * \param key key for index
4961 * \param rec record reference
4963 * \retval +ve success : exact mach
4964 * \retval 0 return record with key not greater than \a key
4965 * \retval -ve failure
4967 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4968 struct dt_rec *rec, const struct dt_key *key)
4970 struct osd_object *obj = osd_dt_obj(dt);
4971 struct iam_path_descr *ipd;
4972 struct iam_container *bag = &obj->oo_dir->od_container;
4973 struct osd_thread_info *oti = osd_oti_get(env);
4974 struct iam_iterator *it = &oti->oti_idx_it;
4975 struct iam_rec *iam_rec;
4979 if (!dt_object_exists(dt))
4982 LASSERT(osd_invariant(obj));
4983 LASSERT(!dt_object_remote(dt));
4984 LASSERT(bag->ic_object == obj->oo_inode);
4986 ipd = osd_idx_ipd_get(env, bag);
4990 /* got ipd now we can start iterator. */
4991 iam_it_init(it, bag, 0, ipd);
4993 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4994 /* swab quota uid/gid provided by caller */
4995 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4996 key = (const struct dt_key *)&oti->oti_quota_id;
4999 rc = iam_it_get(it, (struct iam_key *)key);
5001 if (S_ISDIR(obj->oo_inode->i_mode))
5002 iam_rec = (struct iam_rec *)oti->oti_ldp;
5004 iam_rec = (struct iam_rec *) rec;
5006 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
5008 if (S_ISDIR(obj->oo_inode->i_mode))
5009 osd_fid_unpack((struct lu_fid *) rec,
5010 (struct osd_fid_pack *)iam_rec);
5011 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
5012 osd_quota_unpack(obj, rec);
5017 osd_ipd_put(env, bag, ipd);
5019 LINVRNT(osd_invariant(obj));
5024 static int osd_index_declare_iam_insert(const struct lu_env *env,
5025 struct dt_object *dt,
5026 const struct dt_rec *rec,
5027 const struct dt_key *key,
5028 struct thandle *handle)
5030 struct osd_thandle *oh;
5032 LASSERT(handle != NULL);
5034 oh = container_of0(handle, struct osd_thandle, ot_super);
5035 LASSERT(oh->ot_handle == NULL);
5037 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
5038 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
5044 * Inserts (key, value) pair in \a dt index object.
5046 * \param dt osd index object
5047 * \param key key for index
5048 * \param rec record reference
5049 * \param th transaction handler
5052 * \retval -ve failure
5054 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
5055 const struct dt_rec *rec,
5056 const struct dt_key *key, struct thandle *th,
5059 struct osd_object *obj = osd_dt_obj(dt);
5060 struct iam_path_descr *ipd;
5061 struct osd_thandle *oh;
5062 struct iam_container *bag;
5063 struct osd_thread_info *oti = osd_oti_get(env);
5064 struct iam_rec *iam_rec;
5068 if (!dt_object_exists(dt))
5071 LINVRNT(osd_invariant(obj));
5072 LASSERT(!dt_object_remote(dt));
5074 bag = &obj->oo_dir->od_container;
5075 LASSERT(bag->ic_object == obj->oo_inode);
5076 LASSERT(th != NULL);
5078 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5080 ipd = osd_idx_ipd_get(env, bag);
5081 if (unlikely(ipd == NULL))
5084 oh = container_of0(th, struct osd_thandle, ot_super);
5085 LASSERT(oh->ot_handle != NULL);
5086 LASSERT(oh->ot_handle->h_transaction != NULL);
5087 if (S_ISDIR(obj->oo_inode->i_mode)) {
5088 iam_rec = (struct iam_rec *)oti->oti_ldp;
5089 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
5090 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5091 /* pack quota uid/gid */
5092 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5093 key = (const struct dt_key *)&oti->oti_quota_id;
5094 /* pack quota record */
5095 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
5096 iam_rec = (struct iam_rec *)rec;
5098 iam_rec = (struct iam_rec *)rec;
5101 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
5103 osd_ipd_put(env, bag, ipd);
5104 LINVRNT(osd_invariant(obj));
5105 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5110 * Calls ldiskfs_add_entry() to add directory entry
5111 * into the directory. This is required for
5112 * interoperability mode (b11826)
5114 * \retval 0, on success
5115 * \retval -ve, on error
5117 static int __osd_ea_add_rec(struct osd_thread_info *info,
5118 struct osd_object *pobj, struct inode *cinode,
5119 const char *name, const struct lu_fid *fid,
5120 struct htree_lock *hlock, struct thandle *th)
5122 struct ldiskfs_dentry_param *ldp;
5123 struct dentry *child;
5124 struct osd_thandle *oth;
5127 oth = container_of(th, struct osd_thandle, ot_super);
5128 LASSERT(oth->ot_handle != NULL);
5129 LASSERT(oth->ot_handle->h_transaction != NULL);
5130 LASSERT(pobj->oo_inode);
5132 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
5133 if (unlikely(pobj->oo_inode ==
5134 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
5137 osd_get_ldiskfs_dirent_param(ldp, fid);
5138 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
5139 child->d_fsdata = (void *)ldp;
5140 ll_vfs_dq_init(pobj->oo_inode);
5141 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
5142 child, cinode, hlock);
5143 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
5144 struct ldiskfs_dir_entry_2 *de;
5145 struct buffer_head *bh;
5148 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
5151 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
5154 if (S_ISDIR(cinode->i_mode))
5155 de->file_type = LDISKFS_DIRENT_LUFID |
5156 LDISKFS_FT_REG_FILE;
5158 de->file_type = LDISKFS_DIRENT_LUFID |
5160 ldiskfs_handle_dirty_metadata(oth->ot_handle,
5171 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
5172 * into the directory.Also sets flags into osd object to
5173 * indicate dot and dotdot are created. This is required for
5174 * interoperability mode (b11826)
5176 * \param dir directory for dot and dotdot fixup.
5177 * \param obj child object for linking
5179 * \retval 0, on success
5180 * \retval -ve, on error
5182 static int osd_add_dot_dotdot(struct osd_thread_info *info,
5183 struct osd_object *dir,
5184 struct inode *parent_dir, const char *name,
5185 const struct lu_fid *dot_fid,
5186 const struct lu_fid *dot_dot_fid,
5189 struct inode *inode = dir->oo_inode;
5190 struct osd_thandle *oth;
5193 oth = container_of(th, struct osd_thandle, ot_super);
5194 LASSERT(oth->ot_handle->h_transaction != NULL);
5195 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
5197 if (strcmp(name, dot) == 0) {
5198 if (dir->oo_compat_dot_created) {
5201 LASSERT(inode->i_ino == parent_dir->i_ino);
5202 dir->oo_compat_dot_created = 1;
5205 } else if (strcmp(name, dotdot) == 0) {
5206 if (!dir->oo_compat_dot_created)
5208 /* in case of rename, dotdot is already created */
5209 if (dir->oo_compat_dotdot_created) {
5210 return __osd_ea_add_rec(info, dir, parent_dir, name,
5211 dot_dot_fid, NULL, th);
5214 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
5215 struct lu_fid tfid = *dot_dot_fid;
5218 result = osd_add_dot_dotdot_internal(info,
5219 dir->oo_inode, parent_dir, dot_fid,
5222 result = osd_add_dot_dotdot_internal(info,
5223 dir->oo_inode, parent_dir, dot_fid,
5228 dir->oo_compat_dotdot_created = 1;
5236 * It will call the appropriate osd_add* function and return the
5237 * value, return by respective functions.
5239 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
5240 struct inode *cinode, const char *name,
5241 const struct lu_fid *fid, struct thandle *th)
5243 struct osd_thread_info *info = osd_oti_get(env);
5244 struct htree_lock *hlock;
5247 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
5249 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
5251 if (hlock != NULL) {
5252 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5255 down_write(&pobj->oo_ext_idx_sem);
5258 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
5259 lu_object_fid(&pobj->oo_dt.do_lu),
5262 if (hlock != NULL) {
5263 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5264 pobj->oo_inode, LDISKFS_HLOCK_ADD);
5266 down_write(&pobj->oo_ext_idx_sem);
5269 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
5270 struct lu_fid *tfid = &info->oti_fid;
5274 rc = __osd_ea_add_rec(info, pobj, cinode, name,
5277 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
5282 ldiskfs_htree_unlock(hlock);
5284 up_write(&pobj->oo_ext_idx_sem);
5290 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
5291 struct osd_idmap_cache *oic)
5293 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
5294 struct lu_fid *fid = &oic->oic_fid;
5295 struct osd_inode_id *id = &oic->oic_lid;
5296 struct inode *inode = NULL;
5302 if (!fid_is_norm(fid) && !fid_is_igif(fid))
5305 if (thread_is_running(&scrub->os_thread) &&
5306 scrub->os_pos_current > id->oii_ino)
5309 if (dev->od_auto_scrub_interval == AS_NEVER ||
5310 ktime_get_real_seconds() <
5311 scrub->os_file.sf_time_last_complete + dev->od_auto_scrub_interval)
5315 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
5316 if (rc == -ENOENT) {
5317 __u32 gen = id->oii_gen;
5323 inode = osd_iget(oti, dev, id);
5324 /* The inode has been removed (by race maybe). */
5325 if (IS_ERR(inode)) {
5326 rc = PTR_ERR(inode);
5328 RETURN(rc == -ESTALE ? -ENOENT : rc);
5331 /* The OI mapping is lost. */
5332 if (gen != OSD_OII_NOGEN)
5335 /* The inode may has been reused by others, we do not know,
5336 * leave it to be handled by subsequent osd_fid_lookup(). */
5338 } else if (rc || osd_id_eq(id, &oti->oti_id)) {
5345 if (thread_is_running(&scrub->os_thread)) {
5346 if (inode == NULL) {
5347 inode = osd_iget(oti, dev, id);
5348 /* The inode has been removed (by race maybe). */
5349 if (IS_ERR(inode)) {
5350 rc = PTR_ERR(inode);
5352 RETURN(rc == -ESTALE ? -ENOENT : rc);
5356 rc = osd_oii_insert(dev, oic, insert);
5357 /* There is race condition between osd_oi_lookup and OI scrub.
5358 * The OI scrub finished just after osd_oi_lookup() failure.
5359 * Under such case, it is unnecessary to trigger OI scrub again,
5360 * but try to call osd_oi_lookup() again. */
5361 if (unlikely(rc == -EAGAIN))
5364 if (!S_ISDIR(inode->i_mode))
5367 rc = osd_check_lmv(oti, dev, inode, oic);
5372 if (dev->od_auto_scrub_interval != AS_NEVER && ++once == 1) {
5373 rc = osd_scrub_start(oti->oti_env, dev, SS_AUTO_PARTIAL |
5374 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
5375 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
5376 "%s: trigger partial OI scrub for RPC inconsistency "
5377 "checking FID "DFID": rc = %d\n",
5378 osd_dev2name(dev), PFID(fid), rc);
5379 if (rc == 0 || rc == -EALREADY)
5392 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
5393 struct osd_device *dev,
5394 struct osd_idmap_cache *oic,
5395 struct lu_fid *fid, __u32 ino)
5397 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
5398 struct inode *inode;
5401 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
5402 inode = osd_iget(oti, dev, &oic->oic_lid);
5403 if (IS_ERR(inode)) {
5404 fid_zero(&oic->oic_fid);
5405 return PTR_ERR(inode);
5408 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, loa);
5411 fid_zero(&oic->oic_fid);
5413 *fid = oic->oic_fid = loa->loa_lma.lma_self_fid;
5417 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
5418 struct osd_inode_id *id, const struct lu_fid *fid)
5420 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
5421 id->oii_ino, id->oii_gen, info);
5422 info->oti_cache.oic_lid = *id;
5423 info->oti_cache.oic_fid = *fid;
5424 info->oti_cache.oic_dev = osd;
5428 * Get parent FID from the linkEA.
5430 * For a directory which parent resides on remote MDT, to satisfy the
5431 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
5432 * the other hand, to make the lookup(..) on the directory can return
5433 * the real parent FID, we append the real parent FID after its ".."
5434 * name entry in the /REMOTE_PARENT_DIR.
5436 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
5437 * backup. So after the restore, we cannot get the right parent FID from
5438 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
5439 * we have stored the real parent FID in the directory object's linkEA,
5440 * we can parse the linkEA for the real parent FID.
5442 * \param[in] env pointer to the thread context
5443 * \param[in] obj pointer to the object to be handled
5444 * \param[out]fid pointer to the buffer to hold the parent FID
5446 * \retval 0 for getting the real parent FID successfully
5447 * \retval negative error number on failure
5449 static int osd_get_pfid_from_linkea(const struct lu_env *env,
5450 struct osd_object *obj,
5453 struct osd_thread_info *oti = osd_oti_get(env);
5454 struct lu_buf *buf = &oti->oti_big_buf;
5455 struct dentry *dentry = &oti->oti_obj_dentry;
5456 struct inode *inode = obj->oo_inode;
5457 struct linkea_data ldata = { NULL };
5462 if (!S_ISDIR(inode->i_mode))
5466 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5467 buf->lb_buf, buf->lb_len);
5468 if (rc == -ERANGE) {
5469 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5472 lu_buf_realloc(buf, rc);
5473 if (buf->lb_buf == NULL)
5480 if (unlikely(rc == 0))
5486 if (unlikely(buf->lb_buf == NULL)) {
5487 lu_buf_realloc(buf, rc);
5488 if (buf->lb_buf == NULL)
5495 rc = linkea_init_with_rec(&ldata);
5497 linkea_first_entry(&ldata);
5498 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5504 static int osd_verify_ent_by_linkea(const struct lu_env *env,
5505 struct inode *inode,
5506 const struct lu_fid *pfid,
5507 const char *name, const int namelen)
5509 struct osd_thread_info *oti = osd_oti_get(env);
5510 struct lu_buf *buf = &oti->oti_big_buf;
5511 struct dentry *dentry = &oti->oti_obj_dentry;
5512 struct linkea_data ldata = { NULL };
5513 struct lu_name cname = { .ln_name = name,
5514 .ln_namelen = namelen };
5519 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5520 buf->lb_buf, buf->lb_len);
5522 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
5527 if (unlikely(rc == 0))
5530 if (buf->lb_len < rc) {
5531 lu_buf_realloc(buf, rc);
5532 if (buf->lb_buf == NULL)
5539 rc = linkea_init_with_rec(&ldata);
5541 rc = linkea_links_find(&ldata, &cname, pfid);
5547 * Calls ->lookup() to find dentry. From dentry get inode and
5548 * read inode's ea to get fid. This is required for interoperability
5551 * \retval 0, on success
5552 * \retval -ve, on error
5554 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
5555 struct dt_rec *rec, const struct dt_key *key)
5557 struct inode *dir = obj->oo_inode;
5558 struct dentry *dentry;
5559 struct ldiskfs_dir_entry_2 *de;
5560 struct buffer_head *bh;
5561 struct lu_fid *fid = (struct lu_fid *) rec;
5562 struct htree_lock *hlock = NULL;
5567 LASSERT(dir->i_op != NULL);
5568 LASSERT(dir->i_op->lookup != NULL);
5570 dentry = osd_child_dentry_get(env, obj,
5571 (char *)key, strlen((char *)key));
5573 if (obj->oo_hl_head != NULL) {
5574 hlock = osd_oti_get(env)->oti_hlock;
5575 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5576 dir, LDISKFS_HLOCK_LOOKUP);
5578 down_read(&obj->oo_ext_idx_sem);
5581 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5583 struct osd_thread_info *oti = osd_oti_get(env);
5584 struct osd_inode_id *id = &oti->oti_id;
5585 struct osd_idmap_cache *oic = &oti->oti_cache;
5586 struct osd_device *dev = osd_obj2dev(obj);
5588 ino = le32_to_cpu(de->inode);
5589 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
5591 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
5595 rc = osd_get_fid_from_dentry(de, rec);
5597 /* done with de, release bh */
5600 if (unlikely(is_remote_parent_ino(dev, ino))) {
5601 const char *name = (const char *)key;
5603 /* If the parent is on remote MDT, and there
5604 * is no FID-in-dirent, then we have to get
5605 * the parent FID from the linkEA. */
5606 if (likely(strlen(name) == 2 &&
5607 name[0] == '.' && name[1] == '.'))
5608 rc = osd_get_pfid_from_linkea(env, obj,
5611 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5614 osd_id_gen(id, ino, OSD_OII_NOGEN);
5617 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
5618 fid_zero(&oic->oic_fid);
5623 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
5624 rc = osd_consistency_check(oti, dev, oic);
5626 fid_zero(&oic->oic_fid);
5628 /* Other error should not affect lookup result. */
5638 ldiskfs_htree_unlock(hlock);
5640 up_read(&obj->oo_ext_idx_sem);
5644 static int osd_index_declare_ea_insert(const struct lu_env *env,
5645 struct dt_object *dt,
5646 const struct dt_rec *rec,
5647 const struct dt_key *key,
5648 struct thandle *handle)
5650 struct osd_thandle *oh;
5651 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5652 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5653 const struct lu_fid *fid = rec1->rec_fid;
5654 int credits, rc = 0;
5655 struct osd_idmap_cache *idc;
5658 LASSERT(!dt_object_remote(dt));
5659 LASSERT(handle != NULL);
5660 LASSERT(fid != NULL);
5661 LASSERT(rec1->rec_type != 0);
5663 oh = container_of0(handle, struct osd_thandle, ot_super);
5664 LASSERT(oh->ot_handle == NULL);
5666 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
5668 /* we can't call iget() while a transactions is running
5669 * (this can lead to a deadlock), but we need to know
5670 * inum and object type. so we find this information at
5671 * declaration and cache in per-thread info */
5672 idc = osd_idc_find_or_init(env, osd, fid);
5674 RETURN(PTR_ERR(idc));
5675 if (idc->oic_remote) {
5676 /* a reference to remote inode is represented by an
5677 * agent inode which we have to create */
5678 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
5679 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
5682 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
5684 if (osd_dt_obj(dt)->oo_inode != NULL) {
5685 struct inode *inode = osd_dt_obj(dt)->oo_inode;
5687 /* We ignore block quota on meta pool (MDTs), so needn't
5688 * calculate how many blocks will be consumed by this index
5690 rc = osd_declare_inode_qid(env, i_uid_read(inode),
5692 i_projid_read(inode), 0,
5693 oh, osd_dt_obj(dt), NULL,
5698 #ifdef HAVE_PROJECT_QUOTA
5699 /* Reserve credits for local agent inode to transfer
5700 * to 0, quota enforcement is ignored in this case.
5702 if (idc->oic_remote &&
5703 LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
5704 i_projid_read(inode) != 0)
5705 rc = osd_declare_attr_qid(env, osd_dt_obj(dt), oh,
5706 0, i_projid_read(inode),
5707 0, false, PRJQUOTA, true);
5716 * Index add function for interoperability mode (b11826).
5717 * It will add the directory entry.This entry is needed to
5718 * maintain name->fid mapping.
5720 * \param key it is key i.e. file entry to be inserted
5721 * \param rec it is value of given key i.e. fid
5723 * \retval 0, on success
5724 * \retval -ve, on error
5726 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
5727 const struct dt_rec *rec,
5728 const struct dt_key *key, struct thandle *th,
5731 struct osd_object *obj = osd_dt_obj(dt);
5732 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5733 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5734 const struct lu_fid *fid = rec1->rec_fid;
5735 const char *name = (const char *)key;
5736 struct osd_thread_info *oti = osd_oti_get(env);
5737 struct inode *child_inode = NULL;
5738 struct osd_idmap_cache *idc;
5742 if (!dt_object_exists(dt))
5745 LASSERT(osd_invariant(obj));
5746 LASSERT(!dt_object_remote(dt));
5747 LASSERT(th != NULL);
5749 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5751 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
5753 idc = osd_idc_find(env, osd, fid);
5754 if (unlikely(idc == NULL)) {
5755 /* this dt_insert() wasn't declared properly, so
5756 * FID is missing in OI cache. we better do not
5757 * lookup FID in FLDB/OI and don't risk to deadlock,
5758 * but in some special cases (lfsck testing, etc)
5759 * it's much simpler than fixing a caller */
5760 CERROR("%s: "DFID" wasn't declared for insert\n",
5761 osd_name(osd), PFID(fid));
5763 idc = osd_idc_find_or_init(env, osd, fid);
5765 RETURN(PTR_ERR(idc));
5768 if (idc->oic_remote) {
5769 /* Insert remote entry */
5770 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
5772 igrab(osd->od_mdt_map->omm_remote_parent->d_inode);
5774 child_inode = osd_create_local_agent_inode(env, osd,
5775 obj, fid, rec1->rec_type & S_IFMT, th);
5776 if (IS_ERR(child_inode))
5777 RETURN(PTR_ERR(child_inode));
5780 /* Insert local entry */
5781 if (unlikely(idc->oic_lid.oii_ino == 0)) {
5782 /* for a reason OI cache wasn't filled properly */
5783 CERROR("%s: OIC for "DFID" isn't filled\n",
5784 osd_name(osd), PFID(fid));
5787 child_inode = oti->oti_inode;
5788 if (unlikely(child_inode == NULL)) {
5789 struct ldiskfs_inode_info *lii;
5793 child_inode = oti->oti_inode = &lii->vfs_inode;
5795 child_inode->i_sb = osd_sb(osd);
5796 child_inode->i_ino = idc->oic_lid.oii_ino;
5797 child_inode->i_mode = rec1->rec_type & S_IFMT;
5800 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
5802 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
5803 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
5805 if (child_inode && child_inode != oti->oti_inode)
5807 LASSERT(osd_invariant(obj));
5808 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5813 * Initialize osd Iterator for given osd index object.
5815 * \param dt osd index object
5818 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
5819 struct dt_object *dt,
5822 struct osd_it_iam *it;
5823 struct osd_object *obj = osd_dt_obj(dt);
5824 struct lu_object *lo = &dt->do_lu;
5825 struct iam_path_descr *ipd;
5826 struct iam_container *bag = &obj->oo_dir->od_container;
5828 if (!dt_object_exists(dt))
5829 return ERR_PTR(-ENOENT);
5833 return ERR_PTR(-ENOMEM);
5835 ipd = osd_it_ipd_get(env, bag);
5836 if (likely(ipd != NULL)) {
5840 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
5841 return (struct dt_it *)it;
5844 return ERR_PTR(-ENOMEM);
5849 * free given Iterator.
5851 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
5853 struct osd_it_iam *it = (struct osd_it_iam *)di;
5854 struct osd_object *obj = it->oi_obj;
5856 iam_it_fini(&it->oi_it);
5857 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
5858 osd_object_put(env, obj);
5863 * Move Iterator to record specified by \a key
5865 * \param di osd iterator
5866 * \param key key for index
5868 * \retval +ve di points to record with least key not larger than key
5869 * \retval 0 di points to exact matched key
5870 * \retval -ve failure
5873 static int osd_it_iam_get(const struct lu_env *env,
5874 struct dt_it *di, const struct dt_key *key)
5876 struct osd_thread_info *oti = osd_oti_get(env);
5877 struct osd_it_iam *it = (struct osd_it_iam *)di;
5879 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5880 /* swab quota uid/gid */
5881 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5882 key = (struct dt_key *)&oti->oti_quota_id;
5885 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
5891 * \param di osd iterator
5893 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
5895 struct osd_it_iam *it = (struct osd_it_iam *)di;
5897 iam_it_put(&it->oi_it);
5901 * Move iterator by one record
5903 * \param di osd iterator
5905 * \retval +1 end of container reached
5907 * \retval -ve failure
5910 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
5912 struct osd_it_iam *it = (struct osd_it_iam *)di;
5914 return iam_it_next(&it->oi_it);
5918 * Return pointer to the key under iterator.
5921 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
5922 const struct dt_it *di)
5924 struct osd_thread_info *oti = osd_oti_get(env);
5925 struct osd_it_iam *it = (struct osd_it_iam *)di;
5926 struct osd_object *obj = it->oi_obj;
5929 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
5931 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
5932 /* swab quota uid/gid */
5933 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
5934 key = (struct dt_key *)&oti->oti_quota_id;
5941 * Return size of key under iterator (in bytes)
5944 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5946 struct osd_it_iam *it = (struct osd_it_iam *)di;
5948 return iam_it_key_size(&it->oi_it);
5952 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5954 /* check if file type is required */
5955 if (ent->lde_attrs & LUDA_TYPE) {
5956 struct luda_type *lt;
5957 int align = sizeof(*lt) - 1;
5959 len = (len + align) & ~align;
5960 lt = (struct luda_type *)(ent->lde_name + len);
5961 lt->lt_type = cpu_to_le16(DTTOIF(type));
5964 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5968 * build lu direct from backend fs dirent.
5972 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5973 char *name, __u16 namelen, __u16 type, __u32 attr)
5975 ent->lde_attrs = attr | LUDA_FID;
5976 fid_cpu_to_le(&ent->lde_fid, fid);
5978 ent->lde_hash = cpu_to_le64(offset);
5979 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5981 strncpy(ent->lde_name, name, namelen);
5982 ent->lde_name[namelen] = '\0';
5983 ent->lde_namelen = cpu_to_le16(namelen);
5985 /* append lustre attributes */
5986 osd_it_append_attrs(ent, namelen, type);
5990 * Return pointer to the record under iterator.
5992 static int osd_it_iam_rec(const struct lu_env *env,
5993 const struct dt_it *di,
5994 struct dt_rec *dtrec, __u32 attr)
5996 struct osd_it_iam *it = (struct osd_it_iam *)di;
5997 struct osd_thread_info *info = osd_oti_get(env);
6000 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
6001 const struct osd_fid_pack *rec;
6002 struct lu_fid *fid = &info->oti_fid;
6003 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6009 name = (char *)iam_it_key_get(&it->oi_it);
6011 RETURN(PTR_ERR(name));
6013 namelen = iam_it_key_size(&it->oi_it);
6015 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
6017 RETURN(PTR_ERR(rec));
6019 rc = osd_fid_unpack(fid, rec);
6023 hash = iam_it_store(&it->oi_it);
6025 /* IAM does not store object type in IAM index (dir) */
6026 osd_it_pack_dirent(lde, fid, hash, name, namelen,
6028 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
6029 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6030 (struct iam_rec *)dtrec);
6031 osd_quota_unpack(it->oi_obj, dtrec);
6033 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6034 (struct iam_rec *)dtrec);
6041 * Returns cookie for current Iterator position.
6043 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
6045 struct osd_it_iam *it = (struct osd_it_iam *)di;
6047 return iam_it_store(&it->oi_it);
6051 * Restore iterator from cookie.
6053 * \param di osd iterator
6054 * \param hash Iterator location cookie
6056 * \retval +ve di points to record with least key not larger than key.
6057 * \retval 0 di points to exact matched key
6058 * \retval -ve failure
6061 static int osd_it_iam_load(const struct lu_env *env,
6062 const struct dt_it *di, __u64 hash)
6064 struct osd_it_iam *it = (struct osd_it_iam *)di;
6066 return iam_it_load(&it->oi_it, hash);
6069 static const struct dt_index_operations osd_index_iam_ops = {
6070 .dio_lookup = osd_index_iam_lookup,
6071 .dio_declare_insert = osd_index_declare_iam_insert,
6072 .dio_insert = osd_index_iam_insert,
6073 .dio_declare_delete = osd_index_declare_iam_delete,
6074 .dio_delete = osd_index_iam_delete,
6076 .init = osd_it_iam_init,
6077 .fini = osd_it_iam_fini,
6078 .get = osd_it_iam_get,
6079 .put = osd_it_iam_put,
6080 .next = osd_it_iam_next,
6081 .key = osd_it_iam_key,
6082 .key_size = osd_it_iam_key_size,
6083 .rec = osd_it_iam_rec,
6084 .store = osd_it_iam_store,
6085 .load = osd_it_iam_load
6091 * Creates or initializes iterator context.
6093 * \retval struct osd_it_ea, iterator structure on success
6096 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
6097 struct dt_object *dt,
6100 struct osd_object *obj = osd_dt_obj(dt);
6101 struct osd_thread_info *info = osd_oti_get(env);
6102 struct osd_it_ea *oie;
6104 struct lu_object *lo = &dt->do_lu;
6105 struct dentry *obj_dentry;
6108 if (!dt_object_exists(dt) || obj->oo_destroyed)
6109 RETURN(ERR_PTR(-ENOENT));
6111 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
6113 RETURN(ERR_PTR(-ENOMEM));
6114 obj_dentry = &oie->oie_dentry;
6116 obj_dentry->d_inode = obj->oo_inode;
6117 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
6118 obj_dentry->d_name.hash = 0;
6120 oie->oie_rd_dirent = 0;
6121 oie->oie_it_dirent = 0;
6122 oie->oie_dirent = NULL;
6123 if (unlikely(!info->oti_it_ea_buf_used)) {
6124 oie->oie_buf = info->oti_it_ea_buf;
6125 info->oti_it_ea_buf_used = 1;
6127 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6128 if (oie->oie_buf == NULL)
6129 RETURN(ERR_PTR(-ENOMEM));
6133 file = &oie->oie_file;
6135 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
6136 if (attr & LUDA_64BITHASH)
6137 file->f_mode = FMODE_64BITHASH;
6139 file->f_mode = FMODE_32BITHASH;
6140 file->f_path.dentry = obj_dentry;
6141 file->f_mapping = obj->oo_inode->i_mapping;
6142 file->f_op = obj->oo_inode->i_fop;
6143 set_file_inode(file, obj->oo_inode);
6146 RETURN((struct dt_it *) oie);
6150 * Destroy or finishes iterator context.
6152 * \param di iterator structure to be destroyed
6154 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
6156 struct osd_thread_info *info = osd_oti_get(env);
6157 struct osd_it_ea *oie = (struct osd_it_ea *)di;
6158 struct osd_object *obj = oie->oie_obj;
6159 struct inode *inode = obj->oo_inode;
6162 oie->oie_file.f_op->release(inode, &oie->oie_file);
6163 osd_object_put(env, obj);
6164 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
6165 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6167 info->oti_it_ea_buf_used = 0;
6168 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
6173 * It position the iterator at given key, so that next lookup continues from
6174 * that key Or it is similar to dio_it->load() but based on a key,
6175 * rather than file position.
6177 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
6180 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
6182 static int osd_it_ea_get(const struct lu_env *env,
6183 struct dt_it *di, const struct dt_key *key)
6185 struct osd_it_ea *it = (struct osd_it_ea *)di;
6188 LASSERT(((const char *)key)[0] == '\0');
6189 it->oie_file.f_pos = 0;
6190 it->oie_rd_dirent = 0;
6191 it->oie_it_dirent = 0;
6192 it->oie_dirent = NULL;
6200 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
6204 struct osd_filldir_cbs {
6205 #ifdef HAVE_DIR_CONTEXT
6206 struct dir_context ctx;
6208 struct osd_it_ea *it;
6211 * It is called internally by ->readdir(). It fills the
6212 * iterator's in-memory data structure with required
6213 * information i.e. name, namelen, rec_size etc.
6215 * \param buf in which information to be filled in.
6216 * \param name name of the file in given dir
6218 * \retval 0 on success
6219 * \retval 1 on buffer full
6221 #ifdef HAVE_FILLDIR_USE_CTX
6222 static int osd_ldiskfs_filldir(struct dir_context *buf,
6224 static int osd_ldiskfs_filldir(void *buf,
6226 const char *name, int namelen,
6227 loff_t offset, __u64 ino, unsigned d_type)
6229 struct osd_it_ea *it =
6230 ((struct osd_filldir_cbs *)buf)->it;
6231 struct osd_object *obj = it->oie_obj;
6232 struct osd_it_ea_dirent *ent = it->oie_dirent;
6233 struct lu_fid *fid = &ent->oied_fid;
6234 struct osd_fid_pack *rec;
6237 /* this should never happen */
6238 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
6239 CERROR("ldiskfs return invalid namelen %d\n", namelen);
6243 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
6247 /* "." is just the object itself. */
6248 if (namelen == 1 && name[0] == '.') {
6249 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6250 } else if (d_type & LDISKFS_DIRENT_LUFID) {
6251 rec = (struct osd_fid_pack*) (name + namelen + 1);
6252 if (osd_fid_unpack(fid, rec) != 0)
6257 d_type &= ~LDISKFS_DIRENT_LUFID;
6259 /* NOT export local root. */
6260 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
6261 ino = obj->oo_inode->i_ino;
6262 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6265 ent->oied_ino = ino;
6266 ent->oied_off = offset;
6267 ent->oied_namelen = namelen;
6268 ent->oied_type = d_type;
6270 memcpy(ent->oied_name, name, namelen);
6272 it->oie_rd_dirent++;
6273 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
6278 * Calls ->readdir() to load a directory entry at a time
6279 * and stored it in iterator's in-memory data structure.
6281 * \param di iterator's in memory structure
6283 * \retval 0 on success
6284 * \retval -ve on error
6285 * \retval +1 reach the end of entry
6287 static int osd_ldiskfs_it_fill(const struct lu_env *env,
6288 const struct dt_it *di)
6290 struct osd_it_ea *it = (struct osd_it_ea *)di;
6291 struct osd_object *obj = it->oie_obj;
6292 struct inode *inode = obj->oo_inode;
6293 struct htree_lock *hlock = NULL;
6294 struct file *filp = &it->oie_file;
6296 struct osd_filldir_cbs buf = {
6297 #ifdef HAVE_DIR_CONTEXT
6298 .ctx.actor = osd_ldiskfs_filldir,
6304 it->oie_dirent = it->oie_buf;
6305 it->oie_rd_dirent = 0;
6307 if (obj->oo_hl_head != NULL) {
6308 hlock = osd_oti_get(env)->oti_hlock;
6309 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
6310 inode, LDISKFS_HLOCK_READDIR);
6312 down_read(&obj->oo_ext_idx_sem);
6315 #ifdef HAVE_DIR_CONTEXT
6316 buf.ctx.pos = filp->f_pos;
6317 rc = inode->i_fop->iterate(filp, &buf.ctx);
6318 filp->f_pos = buf.ctx.pos;
6320 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
6324 ldiskfs_htree_unlock(hlock);
6326 up_read(&obj->oo_ext_idx_sem);
6328 if (it->oie_rd_dirent == 0) {
6329 /*If it does not get any dirent, it means it has been reached
6330 *to the end of the dir */
6331 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
6335 it->oie_dirent = it->oie_buf;
6336 it->oie_it_dirent = 1;
6343 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6344 * to load a directory entry at a time and stored it in
6345 * iterator's in-memory data structure.
6347 * \param di iterator's in memory structure
6349 * \retval +ve iterator reached to end
6350 * \retval 0 iterator not reached to end
6351 * \retval -ve on error
6353 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
6355 struct osd_it_ea *it = (struct osd_it_ea *)di;
6360 if (it->oie_it_dirent < it->oie_rd_dirent) {
6362 (void *) it->oie_dirent +
6363 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
6364 it->oie_dirent->oied_namelen);
6365 it->oie_it_dirent++;
6368 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
6371 rc = osd_ldiskfs_it_fill(env, di);
6378 * Returns the key at current position from iterator's in memory structure.
6380 * \param di iterator's in memory structure
6382 * \retval key i.e. struct dt_key on success
6384 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
6385 const struct dt_it *di)
6387 struct osd_it_ea *it = (struct osd_it_ea *)di;
6389 return (struct dt_key *)it->oie_dirent->oied_name;
6393 * Returns the key's size at current position from iterator's in memory structure.
6395 * \param di iterator's in memory structure
6397 * \retval key_size i.e. struct dt_key on success
6399 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
6401 struct osd_it_ea *it = (struct osd_it_ea *)di;
6403 return it->oie_dirent->oied_namelen;
6406 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
6408 if (LDISKFS_DIR_REC_LEN(de) >=
6409 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
6416 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
6417 unsigned blocksize, bool dotdot)
6420 return osd_dotdot_has_space(de);
6422 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
6423 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
6430 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
6431 handle_t *jh, struct dentry *dentry,
6432 const struct lu_fid *fid, struct buffer_head *bh,
6433 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
6436 struct inode *dir = dentry->d_parent->d_inode;
6437 struct inode *inode = dentry->d_inode;
6438 struct osd_fid_pack *rec;
6439 struct ldiskfs_dentry_param *ldp;
6440 int namelen = dentry->d_name.len;
6442 struct osd_thread_info *info = osd_oti_get(env);
6445 if (!ldiskfs_has_feature_dirdata(inode->i_sb))
6448 /* There is enough space to hold the FID-in-dirent. */
6449 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
6450 rc = ldiskfs_journal_get_write_access(jh, bh);
6454 de->name[namelen] = 0;
6455 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
6456 rec->fp_len = sizeof(struct lu_fid) + 1;
6457 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
6458 de->file_type |= LDISKFS_DIRENT_LUFID;
6459 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
6466 rc = ldiskfs_delete_entry(jh, dir, de, bh);
6470 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
6471 osd_get_ldiskfs_dirent_param(ldp, fid);
6472 dentry->d_fsdata = (void *)ldp;
6473 ll_vfs_dq_init(dir);
6474 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
6475 /* It is too bad, we cannot reinsert the name entry back.
6476 * That means we lose it! */
6478 CDEBUG(D_LFSCK, "%s: fail to reinsert the dirent, "
6479 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
6480 osd_ino2name(inode),
6481 dir->i_ino, dir->i_generation, namelen,
6482 dentry->d_name.name, PFID(fid), rc);
6488 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
6489 struct osd_it_ea *it, struct lu_fid *fid,
6490 struct osd_inode_id *id, __u32 *attr)
6492 struct osd_thread_info *info = osd_oti_get(env);
6493 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
6494 struct osd_device *dev = osd_obj2dev(obj);
6495 struct super_block *sb = osd_sb(dev);
6496 const char *devname = osd_name(dev);
6497 struct osd_it_ea_dirent *ent = it->oie_dirent;
6498 struct inode *dir = obj->oo_inode;
6499 struct htree_lock *hlock = NULL;
6500 struct buffer_head *bh = NULL;
6501 handle_t *jh = NULL;
6502 struct ldiskfs_dir_entry_2 *de;
6503 struct dentry *dentry;
6504 struct inode *inode;
6505 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
6508 bool dotdot = false;
6512 if (ent->oied_name[0] == '.') {
6513 if (ent->oied_namelen == 1)
6516 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
6520 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
6521 inode = osd_iget(info, dev, id);
6522 if (IS_ERR(inode)) {
6523 rc = PTR_ERR(inode);
6524 if (rc == -ENOENT || rc == -ESTALE) {
6525 /* Maybe dangling name entry, or
6526 * corrupted directory entry. */
6527 *attr |= LUDA_UNKNOWN;
6530 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
6531 "check_repair, dir = %lu/%u, name = %.*s, "
6532 "ino = %llu, rc = %d\n",
6533 devname, dir->i_ino, dir->i_generation,
6534 ent->oied_namelen, ent->oied_name,
6541 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
6543 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
6544 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
6549 /* We need to ensure that the name entry is still valid.
6550 * Because it may be removed or renamed by other already.
6552 * The unlink or rename operation will start journal before PDO lock,
6553 * so to avoid deadlock, here we need to start journal handle before
6554 * related PDO lock also. But because we do not know whether there
6555 * will be something to be repaired before PDO lock, we just start
6556 * journal without conditions.
6558 * We may need to remove the name entry firstly, then insert back.
6559 * One credit is for user quota file update.
6560 * One credit is for group quota file update.
6561 * Two credits are for dirty inode. */
6562 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
6563 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
6565 if (dev->od_dirent_journal != 0) {
6568 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
6571 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
6572 "check_repair, dir = %lu/%u, credits = %d, "
6573 "name = %.*s, ino = %llu: rc = %d\n",
6574 devname, dir->i_ino, dir->i_generation, credits,
6575 ent->oied_namelen, ent->oied_name,
6578 GOTO(out_inode, rc);
6581 if (obj->oo_hl_head != NULL) {
6582 hlock = osd_oti_get(env)->oti_hlock;
6583 /* "0" means exclusive lock for the whole directory.
6584 * We need to prevent others access such name entry
6585 * during the delete + insert. Neither HLOCK_ADD nor
6586 * HLOCK_DEL cannot guarantee the atomicity. */
6587 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
6589 down_write(&obj->oo_ext_idx_sem);
6592 if (obj->oo_hl_head != NULL) {
6593 hlock = osd_oti_get(env)->oti_hlock;
6594 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
6595 LDISKFS_HLOCK_LOOKUP);
6597 down_read(&obj->oo_ext_idx_sem);
6601 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6602 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
6603 *attr |= LUDA_IGNORE;
6608 /* For dotdot entry, if there is not enough space to hold the
6609 * FID-in-dirent, just keep them there. It only happens when the
6610 * device upgraded from 1.8 or restored from MDT file-level backup.
6611 * For the whole directory, only dotdot entry have no FID-in-dirent
6612 * and needs to get FID from LMA when readdir, it will not affect the
6613 * performance much. */
6614 if (dotdot && !osd_dotdot_has_space(de)) {
6615 *attr |= LUDA_UNKNOWN;
6621 if (lu_fid_eq(fid, &lma->lma_self_fid))
6624 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
6625 struct lu_fid *tfid = &lma->lma_self_fid;
6627 if (likely(dotdot &&
6628 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
6629 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
6630 /* It must be REMOTE_PARENT_DIR and as the
6631 * 'dotdot' entry of remote directory */
6632 *attr |= LUDA_IGNORE;
6634 CDEBUG(D_LFSCK, "%s: expect remote agent "
6635 "parent directory, but got %.*s under "
6636 "dir = %lu/%u with the FID "DFID"\n",
6637 devname, ent->oied_namelen,
6638 ent->oied_name, dir->i_ino,
6639 dir->i_generation, PFID(tfid));
6641 *attr |= LUDA_UNKNOWN;
6648 if (!fid_is_zero(fid)) {
6649 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
6651 if (rc == -ENOENT ||
6653 !(dev->od_scrub.os_scrub.os_file.sf_flags & SF_UPGRADE))) {
6654 /* linkEA does not recognize the dirent entry,
6655 * it may because the dirent entry corruption
6656 * and points to other's inode. */
6657 CDEBUG(D_LFSCK, "%s: the target inode does not "
6658 "recognize the dirent, dir = %lu/%u, "
6659 " name = %.*s, ino = %llu, "
6660 DFID": rc = %d\n", devname, dir->i_ino,
6661 dir->i_generation, ent->oied_namelen,
6662 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6663 *attr |= LUDA_UNKNOWN;
6668 if (rc && rc != -ENODATA) {
6669 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
6670 "dir = %lu/%u, name = %.*s, ino = %llu, "
6671 DFID": rc = %d\n", devname, dir->i_ino,
6672 dir->i_generation, ent->oied_namelen,
6673 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6674 *attr |= LUDA_UNKNOWN;
6681 /* linkEA recognizes the dirent entry, the FID-in-LMA is
6682 * valid, trusted, in spite of fid_is_sane(fid) or not. */
6683 if (*attr & LUDA_VERIFY_DRYRUN) {
6684 *fid = lma->lma_self_fid;
6685 *attr |= LUDA_REPAIR;
6692 dev->od_dirent_journal = 1;
6693 if (hlock != NULL) {
6694 ldiskfs_htree_unlock(hlock);
6697 up_read(&obj->oo_ext_idx_sem);
6703 *fid = lma->lma_self_fid;
6705 /* Update or append the FID-in-dirent. */
6706 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6707 bh, de, hlock, dotdot);
6709 *attr |= LUDA_REPAIR;
6711 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
6712 "the dirent, dir = %lu/%u, name = %.*s, "
6713 "ino = %llu, "DFID": rc = %d\n",
6714 devname, dir->i_ino, dir->i_generation,
6715 ent->oied_namelen, ent->oied_name,
6716 ent->oied_ino, PFID(fid), rc);
6718 /* lma is NULL, trust the FID-in-dirent if it is valid. */
6719 if (*attr & LUDA_VERIFY_DRYRUN) {
6720 if (fid_is_sane(fid)) {
6721 *attr |= LUDA_REPAIR;
6722 } else if (dev->od_index == 0) {
6723 lu_igif_build(fid, inode->i_ino,
6724 inode->i_generation);
6725 *attr |= LUDA_UPGRADE;
6733 dev->od_dirent_journal = 1;
6734 if (hlock != NULL) {
6735 ldiskfs_htree_unlock(hlock);
6738 up_read(&obj->oo_ext_idx_sem);
6745 if (unlikely(fid_is_sane(fid))) {
6746 /* FID-in-dirent exists, but FID-in-LMA is lost.
6747 * Trust the FID-in-dirent, and add FID-in-LMA. */
6748 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
6750 *attr |= LUDA_REPAIR;
6752 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
6753 "update dirent, dir = %lu/%u, "
6754 "name = %.*s, ino = %llu, "
6756 devname, dir->i_ino, dir->i_generation,
6757 ent->oied_namelen, ent->oied_name,
6758 ent->oied_ino, PFID(fid), rc);
6759 } else if (dev->od_index == 0) {
6760 lu_igif_build(fid, inode->i_ino, inode->i_generation);
6761 /* It is probably IGIF object. Only aappend the
6762 * FID-in-dirent. OI scrub will process FID-in-LMA. */
6763 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6764 bh, de, hlock, dotdot);
6766 *attr |= LUDA_UPGRADE;
6768 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
6769 "after the dirent, dir = %lu/%u, "
6770 "name = %.*s, ino = %llu, "
6772 devname, dir->i_ino, dir->i_generation,
6773 ent->oied_namelen, ent->oied_name,
6774 ent->oied_ino, PFID(fid), rc);
6783 if (hlock != NULL) {
6784 ldiskfs_htree_unlock(hlock);
6786 if (dev->od_dirent_journal != 0)
6787 up_write(&obj->oo_ext_idx_sem);
6789 up_read(&obj->oo_ext_idx_sem);
6793 ldiskfs_journal_stop(jh);
6797 if (rc >= 0 && !dirty)
6798 dev->od_dirent_journal = 0;
6804 * Returns the value at current position from iterator's in memory structure.
6806 * \param di struct osd_it_ea, iterator's in memory structure
6807 * \param attr attr requested for dirent.
6808 * \param lde lustre dirent
6810 * \retval 0 no error and \param lde has correct lustre dirent.
6811 * \retval -ve on error
6813 static inline int osd_it_ea_rec(const struct lu_env *env,
6814 const struct dt_it *di,
6815 struct dt_rec *dtrec, __u32 attr)
6817 struct osd_it_ea *it = (struct osd_it_ea *)di;
6818 struct osd_object *obj = it->oie_obj;
6819 struct osd_device *dev = osd_obj2dev(obj);
6820 struct osd_thread_info *oti = osd_oti_get(env);
6821 struct osd_inode_id *id = &oti->oti_id;
6822 struct lu_fid *fid = &it->oie_dirent->oied_fid;
6823 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6824 __u32 ino = it->oie_dirent->oied_ino;
6828 LASSERT(!is_remote_parent_ino(dev, obj->oo_inode->i_ino));
6830 if (attr & LUDA_VERIFY) {
6831 if (unlikely(is_remote_parent_ino(dev, ino))) {
6832 attr |= LUDA_IGNORE;
6833 /* If the parent is on remote MDT, and there
6834 * is no FID-in-dirent, then we have to get
6835 * the parent FID from the linkEA. */
6836 if (!fid_is_sane(fid) &&
6837 it->oie_dirent->oied_namelen == 2 &&
6838 it->oie_dirent->oied_name[0] == '.' &&
6839 it->oie_dirent->oied_name[1] == '.')
6840 osd_get_pfid_from_linkea(env, obj, fid);
6842 rc = osd_dirent_check_repair(env, obj, it, fid, id,
6846 if (!fid_is_sane(fid))
6847 attr |= LUDA_UNKNOWN;
6849 attr &= ~LU_DIRENT_ATTRS_MASK;
6850 if (!fid_is_sane(fid)) {
6851 bool is_dotdot = false;
6852 if (it->oie_dirent->oied_namelen == 2 &&
6853 it->oie_dirent->oied_name[0] == '.' &&
6854 it->oie_dirent->oied_name[1] == '.')
6856 /* If the parent is on remote MDT, and there
6857 * is no FID-in-dirent, then we have to get
6858 * the parent FID from the linkEA. */
6859 if (is_remote_parent_ino(dev, ino) && is_dotdot) {
6860 rc = osd_get_pfid_from_linkea(env, obj, fid);
6862 if (is_dotdot == false &&
6863 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
6866 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6869 osd_id_gen(id, ino, OSD_OII_NOGEN);
6873 /* Pack the entry anyway, at least the offset is right. */
6874 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
6875 it->oie_dirent->oied_name,
6876 it->oie_dirent->oied_namelen,
6877 it->oie_dirent->oied_type, attr);
6882 if (osd_remote_fid(env, dev, fid))
6885 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
6886 osd_add_oi_cache(oti, dev, id, fid);
6888 RETURN(rc > 0 ? 0 : rc);
6892 * Returns the record size size at current position.
6894 * This function will return record(lu_dirent) size in bytes.
6896 * \param[in] env execution environment
6897 * \param[in] di iterator's in memory structure
6898 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
6899 * calculate the lu_dirent size.
6901 * \retval record size(in bytes & in memory) of the current lu_dirent
6904 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
6907 struct osd_it_ea *it = (struct osd_it_ea *)di;
6909 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
6913 * Returns a cookie for current position of the iterator head, so that
6914 * user can use this cookie to load/start the iterator next time.
6916 * \param di iterator's in memory structure
6918 * \retval cookie for current position, on success
6920 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
6922 struct osd_it_ea *it = (struct osd_it_ea *)di;
6924 return it->oie_dirent->oied_off;
6928 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6929 * to load a directory entry at a time and stored it i inn,
6930 * in iterator's in-memory data structure.
6932 * \param di struct osd_it_ea, iterator's in memory structure
6934 * \retval +ve on success
6935 * \retval -ve on error
6937 static int osd_it_ea_load(const struct lu_env *env,
6938 const struct dt_it *di, __u64 hash)
6940 struct osd_it_ea *it = (struct osd_it_ea *)di;
6944 it->oie_file.f_pos = hash;
6946 rc = osd_ldiskfs_it_fill(env, di);
6957 * Index lookup function for interoperability mode (b11826).
6959 * \param key, key i.e. file name to be searched
6961 * \retval +ve, on success
6962 * \retval -ve, on error
6964 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6965 struct dt_rec *rec, const struct dt_key *key)
6967 struct osd_object *obj = osd_dt_obj(dt);
6972 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6973 LINVRNT(osd_invariant(obj));
6975 rc = osd_ea_lookup_rec(env, obj, rec, key);
6982 * Index and Iterator operations for interoperability
6983 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6985 static const struct dt_index_operations osd_index_ea_ops = {
6986 .dio_lookup = osd_index_ea_lookup,
6987 .dio_declare_insert = osd_index_declare_ea_insert,
6988 .dio_insert = osd_index_ea_insert,
6989 .dio_declare_delete = osd_index_declare_ea_delete,
6990 .dio_delete = osd_index_ea_delete,
6992 .init = osd_it_ea_init,
6993 .fini = osd_it_ea_fini,
6994 .get = osd_it_ea_get,
6995 .put = osd_it_ea_put,
6996 .next = osd_it_ea_next,
6997 .key = osd_it_ea_key,
6998 .key_size = osd_it_ea_key_size,
6999 .rec = osd_it_ea_rec,
7000 .rec_size = osd_it_ea_rec_size,
7001 .store = osd_it_ea_store,
7002 .load = osd_it_ea_load
7006 static void *osd_key_init(const struct lu_context *ctx,
7007 struct lu_context_key *key)
7009 struct osd_thread_info *info;
7011 OBD_ALLOC_PTR(info);
7013 return ERR_PTR(-ENOMEM);
7015 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7016 if (info->oti_it_ea_buf == NULL)
7019 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
7021 info->oti_hlock = ldiskfs_htree_lock_alloc();
7022 if (info->oti_hlock == NULL)
7028 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7031 return ERR_PTR(-ENOMEM);
7034 static void osd_key_fini(const struct lu_context *ctx,
7035 struct lu_context_key *key, void* data)
7037 struct osd_thread_info *info = data;
7038 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
7039 struct osd_idmap_cache *idc = info->oti_ins_cache;
7041 if (info->oti_inode != NULL)
7043 if (info->oti_hlock != NULL)
7044 ldiskfs_htree_lock_free(info->oti_hlock);
7045 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7046 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
7047 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
7048 lu_buf_free(&info->oti_big_buf);
7050 LASSERT(info->oti_ins_cache_size > 0);
7051 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
7052 info->oti_ins_cache = NULL;
7053 info->oti_ins_cache_size = 0;
7058 static void osd_key_exit(const struct lu_context *ctx,
7059 struct lu_context_key *key, void *data)
7061 struct osd_thread_info *info = data;
7063 LASSERT(info->oti_r_locks == 0);
7064 LASSERT(info->oti_w_locks == 0);
7065 LASSERT(info->oti_txns == 0);
7068 /* type constructor/destructor: osd_type_init, osd_type_fini */
7069 LU_TYPE_INIT_FINI(osd, &osd_key);
7071 struct lu_context_key osd_key = {
7072 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
7073 .lct_init = osd_key_init,
7074 .lct_fini = osd_key_fini,
7075 .lct_exit = osd_key_exit
7079 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
7080 const char *name, struct lu_device *next)
7082 struct osd_device *osd = osd_dev(d);
7084 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
7085 >= sizeof(osd->od_svname))
7087 return osd_procfs_init(osd, name);
7090 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
7092 struct seq_server_site *ss = osd_seq_site(osd);
7096 if (osd->od_is_ost || osd->od_cl_seq != NULL)
7099 if (unlikely(ss == NULL))
7102 OBD_ALLOC_PTR(osd->od_cl_seq);
7103 if (osd->od_cl_seq == NULL)
7106 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
7107 osd->od_svname, ss->ss_server_seq);
7109 OBD_FREE_PTR(osd->od_cl_seq);
7110 osd->od_cl_seq = NULL;
7114 if (ss->ss_node_id == 0) {
7115 /* If the OSD on the sequence controller(MDT0), then allocate
7116 * sequence here, otherwise allocate sequence after connected
7117 * to MDT0 (see mdt_register_lwp_callback()). */
7118 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
7119 &osd->od_cl_seq->lcs_space, env);
7125 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
7127 if (osd->od_cl_seq == NULL)
7130 seq_client_fini(osd->od_cl_seq);
7131 OBD_FREE_PTR(osd->od_cl_seq);
7132 osd->od_cl_seq = NULL;
7135 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
7139 /* shutdown quota slave instance associated with the device */
7140 if (o->od_quota_slave != NULL) {
7141 struct qsd_instance *qsd = o->od_quota_slave;
7143 o->od_quota_slave = NULL;
7147 osd_fid_fini(env, o);
7148 osd_scrub_cleanup(env, o);
7153 static void osd_umount(const struct lu_env *env, struct osd_device *o)
7157 if (o->od_mnt != NULL) {
7158 shrink_dcache_sb(osd_sb(o));
7159 osd_sync(env, &o->od_dt_dev);
7168 static int osd_mount(const struct lu_env *env,
7169 struct osd_device *o, struct lustre_cfg *cfg)
7171 const char *name = lustre_cfg_string(cfg, 0);
7172 const char *dev = lustre_cfg_string(cfg, 1);
7174 unsigned long page, s_flags, lmd_flags = 0;
7175 struct page *__page;
7176 struct file_system_type *type;
7177 char *options = NULL;
7179 struct osd_thread_info *info = osd_oti_get(env);
7180 struct lu_fid *fid = &info->oti_fid;
7181 struct inode *inode;
7182 int rc = 0, force_over_512tb = 0;
7185 if (o->od_mnt != NULL)
7188 if (strlen(dev) >= sizeof(o->od_mntdev))
7190 strcpy(o->od_mntdev, dev);
7192 str = lustre_cfg_string(cfg, 2);
7193 s_flags = simple_strtoul(str, NULL, 0);
7194 str = strstr(str, ":");
7196 lmd_flags = simple_strtoul(str + 1, NULL, 0);
7197 opts = lustre_cfg_string(cfg, 3);
7199 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
7200 CERROR("%s: device %s extents feature is not guaranteed to "
7201 "work on big-endian systems. Use \"bigendian_extents\" "
7202 "mount option to override.\n", name, dev);
7206 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7207 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
7208 CWARN("force_over_128tb option is deprecated. "
7209 "Filesystems less than 512TB can be created without any "
7210 "force options. Use force_over_512tb option for "
7211 "filesystems greater than 512TB.\n");
7214 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
7215 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
7216 CWARN("force_over_256tb option is deprecated. "
7217 "Filesystems less than 512TB can be created without any "
7218 "force options. Use force_over_512tb option for "
7219 "filesystems greater than 512TB.\n");
7223 if (opts != NULL && strstr(opts, "force_over_512tb") != NULL)
7224 force_over_512tb = 1;
7226 __page = alloc_page(GFP_KERNEL);
7228 GOTO(out, rc = -ENOMEM);
7229 page = (unsigned long)page_address(__page);
7230 options = (char *)page;
7233 /* strip out the options for back compatiblity */
7234 static char *sout[] = {
7241 /* strip out option we processed in osd */
7242 "bigendian_extents",
7248 strcat(options, opts);
7249 for (rc = 0, str = options; sout[rc]; ) {
7250 char *op = strstr(str, sout[rc]);
7256 if (op == options || *(op - 1) == ',') {
7257 str = op + strlen(sout[rc]);
7258 if (*str == ',' || *str == '\0') {
7259 *str == ',' ? str++ : str;
7260 memmove(op, str, strlen(str) + 1);
7263 for (str = op; *str != ',' && *str != '\0'; str++)
7267 strncat(options, "user_xattr,acl", 14);
7270 /* Glom up mount options */
7271 if (*options != '\0')
7272 strcat(options, ",");
7273 strlcat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
7275 type = get_fs_type("ldiskfs");
7277 CERROR("%s: cannot find ldiskfs module\n", name);
7278 GOTO(out, rc = -ENODEV);
7281 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
7282 module_put(type->owner);
7284 if (IS_ERR(o->od_mnt)) {
7285 rc = PTR_ERR(o->od_mnt);
7287 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
7291 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
7292 osd_sb(o)->s_blocksize_bits > 512ULL << 40 &&
7293 force_over_512tb == 0) {
7294 CERROR("%s: device %s LDISKFS does not support filesystems "
7295 "greater than 512TB and can cause data corruption. "
7296 "Use \"force_over_512tb\" mount option to override.\n",
7298 GOTO(out_mnt, rc = -EINVAL);
7301 if (lmd_flags & LMD_FLG_DEV_RDONLY) {
7302 if (priv_dev_set_rdonly) {
7303 priv_dev_set_rdonly(osd_sb(o)->s_bdev);
7304 o->od_dt_dev.dd_rdonly = 1;
7305 LCONSOLE_WARN("%s: set dev_rdonly on this device\n",
7308 LCONSOLE_WARN("%s: not support dev_rdonly on this device",
7311 GOTO(out_mnt, rc = -EOPNOTSUPP);
7313 } else if (priv_dev_check_rdonly &&
7314 priv_dev_check_rdonly(osd_sb(o)->s_bdev)) {
7315 CERROR("%s: underlying device %s is marked as "
7316 "read-only. Setup failed\n", name, dev);
7318 GOTO(out_mnt, rc = -EROFS);
7321 if (!ldiskfs_has_feature_journal(o->od_mnt->mnt_sb)) {
7322 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
7323 GOTO(out_mnt, rc = -EINVAL);
7326 #ifdef LDISKFS_MOUNT_DIRDATA
7327 if (ldiskfs_has_feature_dirdata(o->od_mnt->mnt_sb))
7328 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
7329 else if (strstr(name, "MDT")) /* don't complain for MGT or OSTs */
7330 CWARN("%s: device %s was upgraded from Lustre-1.x without "
7331 "enabling the dirdata feature. If you do not want to "
7332 "downgrade to Lustre-1.x again, you can enable it via "
7333 "'tune2fs -O dirdata device'\n", name, dev);
7335 inode = osd_sb(o)->s_root->d_inode;
7336 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
7337 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
7339 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
7343 if (lmd_flags & LMD_FLG_NOSCRUB)
7344 o->od_auto_scrub_interval = AS_NEVER;
7354 __free_page(__page);
7359 static struct lu_device *osd_device_fini(const struct lu_env *env,
7360 struct lu_device *d)
7362 struct osd_device *o = osd_dev(d);
7365 osd_index_backup(env, o, false);
7366 osd_shutdown(env, o);
7368 osd_obj_map_fini(o);
7374 static int osd_device_init0(const struct lu_env *env,
7375 struct osd_device *o,
7376 struct lustre_cfg *cfg)
7378 struct lu_device *l = osd2lu_dev(o);
7379 struct osd_thread_info *info;
7383 /* if the module was re-loaded, env can loose its keys */
7384 rc = lu_env_refill((struct lu_env *) env);
7387 info = osd_oti_get(env);
7390 l->ld_ops = &osd_lu_ops;
7391 o->od_dt_dev.dd_ops = &osd_dt_ops;
7393 spin_lock_init(&o->od_osfs_lock);
7394 mutex_init(&o->od_otable_mutex);
7395 INIT_LIST_HEAD(&o->od_orphan_list);
7396 INIT_LIST_HEAD(&o->od_index_backup_list);
7397 INIT_LIST_HEAD(&o->od_index_restore_list);
7398 spin_lock_init(&o->od_lock);
7399 o->od_index_backup_policy = LIBP_NONE;
7401 o->od_read_cache = 1;
7402 o->od_writethrough_cache = 1;
7403 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
7404 o->od_auto_scrub_interval = AS_DEFAULT;
7406 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
7407 sizeof(o->od_svname));
7408 if (cplen >= sizeof(o->od_svname)) {
7413 o->od_index_backup_stop = 0;
7414 o->od_index = -1; /* -1 means index is invalid */
7415 rc = server_name2index(o->od_svname, &o->od_index, NULL);
7416 if (rc == LDD_F_SV_TYPE_OST)
7419 o->od_full_scrub_ratio = OFSR_DEFAULT;
7420 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
7421 rc = osd_mount(env, o, cfg);
7425 rc = osd_obj_map_init(env, o);
7429 rc = lu_site_init(&o->od_site, l);
7431 GOTO(out_compat, rc);
7432 o->od_site.ls_bottom_dev = l;
7434 rc = lu_site_init_finish(&o->od_site);
7438 INIT_LIST_HEAD(&o->od_ios_list);
7439 /* setup scrub, including OI files initialization */
7441 rc = osd_scrub_setup(env, o);
7446 rc = osd_procfs_init(o, o->od_svname);
7448 CERROR("%s: can't initialize procfs: rc = %d\n",
7450 GOTO(out_scrub, rc);
7453 LASSERT(l->ld_site->ls_linkage.next != NULL);
7454 LASSERT(l->ld_site->ls_linkage.prev != NULL);
7456 /* initialize quota slave instance */
7457 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
7459 if (IS_ERR(o->od_quota_slave)) {
7460 rc = PTR_ERR(o->od_quota_slave);
7461 o->od_quota_slave = NULL;
7462 GOTO(out_procfs, rc);
7470 osd_scrub_cleanup(env, o);
7472 lu_site_fini(&o->od_site);
7474 osd_obj_map_fini(o);
7481 static struct lu_device *osd_device_alloc(const struct lu_env *env,
7482 struct lu_device_type *t,
7483 struct lustre_cfg *cfg)
7485 struct osd_device *o;
7490 return ERR_PTR(-ENOMEM);
7492 rc = dt_device_init(&o->od_dt_dev, t);
7494 /* Because the ctx might be revived in dt_device_init,
7495 * refill the env here */
7496 lu_env_refill((struct lu_env *)env);
7497 rc = osd_device_init0(env, o, cfg);
7499 dt_device_fini(&o->od_dt_dev);
7502 if (unlikely(rc != 0))
7505 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
7508 static struct lu_device *osd_device_free(const struct lu_env *env,
7509 struct lu_device *d)
7511 struct osd_device *o = osd_dev(d);
7514 /* XXX: make osd top device in order to release reference */
7515 d->ld_site->ls_top_dev = d;
7516 lu_site_purge(env, d->ld_site, -1);
7517 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
7518 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
7519 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
7521 lu_site_fini(&o->od_site);
7522 dt_device_fini(&o->od_dt_dev);
7527 static int osd_process_config(const struct lu_env *env,
7528 struct lu_device *d, struct lustre_cfg *cfg)
7530 struct osd_device *o = osd_dev(d);
7534 switch (cfg->lcfg_command) {
7536 rc = osd_mount(env, o, cfg);
7539 /* For the case LCFG_PRE_CLEANUP is not called in advance,
7540 * that may happend if hit failure during mount process. */
7541 osd_index_backup(env, o, false);
7542 lu_dev_del_linkage(d->ld_site, d);
7543 rc = osd_shutdown(env, o);
7546 LASSERT(&o->od_dt_dev);
7547 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
7548 cfg, &o->od_dt_dev);
7549 if (rc > 0 || rc == -ENOSYS) {
7550 rc = class_process_proc_param(PARAM_OST,
7551 lprocfs_osd_obd_vars,
7552 cfg, &o->od_dt_dev);
7557 case LCFG_PRE_CLEANUP:
7559 osd_index_backup(env, o,
7560 o->od_index_backup_policy != LIBP_NONE);
7570 static int osd_recovery_complete(const struct lu_env *env,
7571 struct lu_device *d)
7573 struct osd_device *osd = osd_dev(d);
7577 if (osd->od_quota_slave == NULL)
7580 /* start qsd instance on recovery completion, this notifies the quota
7581 * slave code that we are about to process new requests now */
7582 rc = qsd_start(env, osd->od_quota_slave);
7587 * we use exports to track all osd users
7589 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
7590 struct obd_device *obd, struct obd_uuid *cluuid,
7591 struct obd_connect_data *data, void *localdata)
7593 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7594 struct lustre_handle conn;
7598 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
7600 rc = class_connect(&conn, obd, cluuid);
7604 *exp = class_conn2export(&conn);
7606 spin_lock(&osd->od_osfs_lock);
7608 spin_unlock(&osd->od_osfs_lock);
7614 * once last export (we don't count self-export) disappeared
7615 * osd can be released
7617 static int osd_obd_disconnect(struct obd_export *exp)
7619 struct obd_device *obd = exp->exp_obd;
7620 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7621 int rc, release = 0;
7624 /* Only disconnect the underlying layers on the final disconnect. */
7625 spin_lock(&osd->od_osfs_lock);
7627 if (osd->od_connects == 0)
7629 spin_unlock(&osd->od_osfs_lock);
7631 rc = class_disconnect(exp); /* bz 9811 */
7633 if (rc == 0 && release)
7634 class_manual_cleanup(obd);
7638 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
7639 struct lu_device *dev)
7641 struct osd_device *osd = osd_dev(dev);
7642 struct lr_server_data *lsd =
7643 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
7647 if (osd->od_quota_slave != NULL) {
7648 /* set up quota slave objects */
7649 result = qsd_prepare(env, osd->od_quota_slave);
7654 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
7655 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
7656 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
7657 osd->od_index_in_idif = 1;
7659 osd->od_index_in_idif = 0;
7660 result = osd_register_proc_index_in_idif(osd);
7665 osd->od_index_in_idif = 1;
7669 result = osd_fid_init(env, osd);
7674 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
7675 struct lu_fid *fid, struct md_op_data *op_data)
7677 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
7679 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
7682 static const struct lu_object_operations osd_lu_obj_ops = {
7683 .loo_object_init = osd_object_init,
7684 .loo_object_delete = osd_object_delete,
7685 .loo_object_release = osd_object_release,
7686 .loo_object_free = osd_object_free,
7687 .loo_object_print = osd_object_print,
7688 .loo_object_invariant = osd_object_invariant
7691 const struct lu_device_operations osd_lu_ops = {
7692 .ldo_object_alloc = osd_object_alloc,
7693 .ldo_process_config = osd_process_config,
7694 .ldo_recovery_complete = osd_recovery_complete,
7695 .ldo_prepare = osd_prepare,
7698 static const struct lu_device_type_operations osd_device_type_ops = {
7699 .ldto_init = osd_type_init,
7700 .ldto_fini = osd_type_fini,
7702 .ldto_start = osd_type_start,
7703 .ldto_stop = osd_type_stop,
7705 .ldto_device_alloc = osd_device_alloc,
7706 .ldto_device_free = osd_device_free,
7708 .ldto_device_init = osd_device_init,
7709 .ldto_device_fini = osd_device_fini
7712 static struct lu_device_type osd_device_type = {
7713 .ldt_tags = LU_DEVICE_DT,
7714 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
7715 .ldt_ops = &osd_device_type_ops,
7716 .ldt_ctx_tags = LCT_LOCAL,
7719 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
7721 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7722 struct super_block *sb = osd_sb(osd);
7724 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
7728 * lprocfs legacy support.
7730 static struct obd_ops osd_obd_device_ops = {
7731 .o_owner = THIS_MODULE,
7732 .o_connect = osd_obd_connect,
7733 .o_disconnect = osd_obd_disconnect,
7734 .o_fid_alloc = osd_fid_alloc,
7735 .o_health_check = osd_health_check,
7738 static int __init osd_init(void)
7742 CLASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
7743 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
7744 /* please, try to keep osd_thread_info smaller than a page */
7745 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
7750 rc = lu_kmem_init(ldiskfs_caches);
7754 #ifdef CONFIG_KALLSYMS
7755 priv_dev_set_rdonly = (void *)kallsyms_lookup_name("dev_set_rdonly");
7756 priv_dev_check_rdonly = (void *)kallsyms_lookup_name("dev_check_rdonly");
7757 /* Clear readonly is unused at this time */
7758 /*priv_dev_clear_rdonly = (void *)kallsyms_lookup_name("dev_clear_rdonly");*/
7761 rc = class_register_type(&osd_obd_device_ops, NULL, true,
7762 lprocfs_osd_module_vars,
7763 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
7765 lu_kmem_fini(ldiskfs_caches);
7769 static void __exit osd_exit(void)
7771 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
7772 lu_kmem_fini(ldiskfs_caches);
7775 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7776 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
7777 MODULE_VERSION(LUSTRE_VERSION_STRING);
7778 MODULE_LICENSE("GPL");
7780 module_init(osd_init);
7781 module_exit(osd_exit);