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, 2016, 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/module.h>
43 #include <linux/user_namespace.h>
44 #ifdef HAVE_UIDGID_HEADER
45 # include <linux/uidgid.h>
48 /* prerequisite for linux/xattr.h */
49 #include <linux/types.h>
50 /* prerequisite for linux/xattr.h */
52 /* XATTR_{REPLACE,CREATE} */
53 #include <linux/xattr.h>
55 #include <ldiskfs/ldiskfs.h>
56 #include <ldiskfs/xattr.h>
57 #include <ldiskfs/ldiskfs_extents.h>
60 * struct OBD_{ALLOC,FREE}*()
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
66 #include <lustre_fid.h>
68 #include <uapi/linux/lustre/lustre_param.h>
70 #include "osd_internal.h"
71 #include "osd_dynlocks.h"
73 /* llo_* api support */
74 #include <md_object.h>
75 #include <lustre_quota.h>
77 #include <lustre_linkea.h>
79 #define PFID_STRIPE_IDX_BITS 16
80 #define PFID_STRIPE_COUNT_MASK ((1 << PFID_STRIPE_IDX_BITS) - 1)
83 module_param(ldiskfs_pdo, int, 0644);
84 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
86 int ldiskfs_track_declares_assert;
87 module_param(ldiskfs_track_declares_assert, int, 0644);
88 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
90 /* Slab to allocate dynlocks */
91 struct kmem_cache *dynlock_cachep;
93 /* Slab to allocate osd_it_ea */
94 struct kmem_cache *osd_itea_cachep;
96 static struct lu_kmem_descr ldiskfs_caches[] = {
98 .ckd_cache = &dynlock_cachep,
99 .ckd_name = "dynlock_cache",
100 .ckd_size = sizeof(struct dynlock_handle)
103 .ckd_cache = &osd_itea_cachep,
104 .ckd_name = "osd_itea_cache",
105 .ckd_size = sizeof(struct osd_it_ea)
112 static const char dot[] = ".";
113 static const char dotdot[] = "..";
115 static const struct lu_object_operations osd_lu_obj_ops;
116 static const struct dt_object_operations osd_obj_ops;
117 static const struct dt_object_operations osd_obj_otable_it_ops;
118 static const struct dt_index_operations osd_index_iam_ops;
119 static const struct dt_index_operations osd_index_ea_ops;
121 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
122 const struct lu_fid *fid);
123 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
124 struct osd_device *osd);
126 int osd_trans_declare_op2rb[] = {
127 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
128 [OSD_OT_PUNCH] = OSD_OT_MAX,
129 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
130 [OSD_OT_CREATE] = OSD_OT_DESTROY,
131 [OSD_OT_DESTROY] = OSD_OT_CREATE,
132 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
133 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
134 [OSD_OT_WRITE] = OSD_OT_WRITE,
135 [OSD_OT_INSERT] = OSD_OT_DELETE,
136 [OSD_OT_DELETE] = OSD_OT_INSERT,
137 [OSD_OT_QUOTA] = OSD_OT_MAX,
140 static int osd_has_index(const struct osd_object *obj)
142 return obj->oo_dt.do_index_ops != NULL;
145 static int osd_object_invariant(const struct lu_object *l)
147 return osd_invariant(osd_obj(l));
151 * Concurrency: doesn't matter
153 static int osd_is_write_locked(const struct lu_env *env, struct osd_object *o)
155 struct osd_thread_info *oti = osd_oti_get(env);
156 return oti->oti_w_locks > 0 && o->oo_owner == env;
160 * Concurrency: doesn't access mutable data
162 static int osd_root_get(const struct lu_env *env,
163 struct dt_device *dev, struct lu_fid *f)
165 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
170 * the following set of functions are used to maintain per-thread
171 * cache of FID->ino mapping. this mechanism is needed to resolve
172 * FID to inode at dt_insert() which in turn stores ino in the
173 * directory entries to keep ldiskfs compatible with ext[34].
174 * due to locking-originated restrictions we can't lookup ino
175 * using LU cache (deadlock is possible). lookup using OI is quite
176 * expensive. so instead we maintain this cache and methods like
177 * dt_create() fill it. so in the majority of cases dt_insert() is
178 * able to find needed mapping in lockless manner.
180 static struct osd_idmap_cache *
181 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
182 const struct lu_fid *fid)
184 struct osd_thread_info *oti = osd_oti_get(env);
185 struct osd_idmap_cache *idc = oti->oti_ins_cache;
187 for (i = 0; i < oti->oti_ins_cache_used; i++) {
188 if (!lu_fid_eq(&idc[i].oic_fid, fid))
190 if (idc[i].oic_dev != osd)
199 static struct osd_idmap_cache *
200 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
201 const struct lu_fid *fid)
203 struct osd_thread_info *oti = osd_oti_get(env);
204 struct osd_idmap_cache *idc;
207 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
208 i = oti->oti_ins_cache_size * 2;
210 i = OSD_INS_CACHE_SIZE;
211 OBD_ALLOC(idc, sizeof(*idc) * i);
213 return ERR_PTR(-ENOMEM);
214 if (oti->oti_ins_cache != NULL) {
215 memcpy(idc, oti->oti_ins_cache,
216 oti->oti_ins_cache_used * sizeof(*idc));
217 OBD_FREE(oti->oti_ins_cache,
218 oti->oti_ins_cache_used * sizeof(*idc));
220 oti->oti_ins_cache = idc;
221 oti->oti_ins_cache_size = i;
224 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
227 idc->oic_lid.oii_ino = 0;
228 idc->oic_lid.oii_gen = 0;
235 * lookup mapping for the given fid in the cache, initialize a
236 * new one if not found. the initialization checks whether the
237 * object is local or remote. for local objects, OI is used to
238 * learn ino/generation. the function is used when the caller
239 * has no information about the object, e.g. at dt_insert().
241 static struct osd_idmap_cache *
242 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
243 const struct lu_fid *fid)
245 struct osd_idmap_cache *idc;
248 idc = osd_idc_find(env, osd, fid);
249 LASSERT(!IS_ERR(idc));
253 /* new mapping is needed */
254 idc = osd_idc_add(env, osd, fid);
259 rc = osd_remote_fid(env, osd, fid);
260 if (unlikely(rc < 0))
264 /* the object is local, lookup in OI */
265 /* XXX: probably cheaper to lookup in LU first? */
266 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
268 if (unlikely(rc < 0)) {
269 CERROR("can't lookup: rc = %d\n", rc);
273 /* the object is remote */
281 * lookup mapping for given FID and fill it from the given object.
282 * the object is lolcal by definition.
284 static int osd_idc_find_and_init(const struct lu_env *env,
285 struct osd_device *osd,
286 struct osd_object *obj)
288 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
289 struct osd_idmap_cache *idc;
291 idc = osd_idc_find(env, osd, fid);
292 LASSERT(!IS_ERR(idc));
294 if (obj->oo_inode == NULL)
296 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
297 LASSERT(idc->oic_lid.oii_ino == 0);
298 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
299 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
304 /* new mapping is needed */
305 idc = osd_idc_add(env, osd, fid);
309 if (obj->oo_inode != NULL) {
310 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
311 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
317 * OSD object methods.
321 * Concurrency: no concurrent access is possible that early in object
324 static struct lu_object *osd_object_alloc(const struct lu_env *env,
325 const struct lu_object_header *hdr,
328 struct osd_object *mo;
333 struct lu_object_header *h;
335 l = &mo->oo_dt.do_lu;
336 h = &osd_dev(d)->od_scrub.os_scrub.os_obj_header;
337 if (unlikely(fid_is_zero(&h->loh_fid))) {
338 /* For the OI_scrub object during OSD device init. */
339 lu_object_header_init(h);
340 lu_object_init(l, h, d);
341 lu_object_add_top(h, l);
343 dt_object_init(&mo->oo_dt, NULL, d);
345 mo->oo_dt.do_ops = &osd_obj_ops;
346 l->lo_ops = &osd_lu_obj_ops;
347 init_rwsem(&mo->oo_sem);
348 init_rwsem(&mo->oo_ext_idx_sem);
349 spin_lock_init(&mo->oo_guard);
350 INIT_LIST_HEAD(&mo->oo_xattr_list);
357 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
358 struct dentry *dentry, struct lustre_ost_attrs *loa)
362 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
363 (void *)loa, sizeof(*loa));
365 struct lustre_mdt_attrs *lma = &loa->loa_lma;
367 if (rc < sizeof(*lma))
371 lustre_loa_swab(loa, true);
372 /* Check LMA compatibility */
373 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
374 CWARN("%s: unsupported incompat LMA feature(s) %#x "
375 "for fid = "DFID", ino = %lu\n",
377 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
378 PFID(&lma->lma_self_fid), inode->i_ino);
381 } else if (rc == 0) {
389 * retrieve object from backend ext fs.
391 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
392 struct osd_inode_id *id)
395 struct inode *inode = NULL;
397 /* if we look for an inode withing a running
398 * transaction, then we risk to deadlock */
399 /* osd_dirent_check_repair() breaks this */
400 /*LASSERT(current->journal_info == NULL);*/
402 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
404 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
405 id->oii_ino, PTR_ERR(inode));
406 } else if (id->oii_gen != OSD_OII_NOGEN &&
407 inode->i_generation != id->oii_gen) {
408 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
409 "i_generation = %u\n",
410 id->oii_ino, id->oii_gen, inode->i_generation);
412 inode = ERR_PTR(-ESTALE);
413 } else if (inode->i_nlink == 0) {
414 /* due to parallel readdir and unlink,
415 * we can have dead inode here. */
416 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
418 inode = ERR_PTR(-ESTALE);
419 } else if (is_bad_inode(inode)) {
420 CWARN("%s: bad inode: ino = %u\n",
421 osd_dev2name(dev), id->oii_ino);
423 inode = ERR_PTR(-ENOENT);
424 } else if ((rc = osd_attach_jinode(inode))) {
428 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
429 if (id->oii_gen == OSD_OII_NOGEN)
430 osd_id_gen(id, inode->i_ino, inode->i_generation);
432 /* Do not update file c/mtime in ldiskfs.
433 * NB: we don't have any lock to protect this because we don't
434 * have reference on osd_object now, but contention with
435 * another lookup + attr_set can't happen in the tiny window
436 * between if (...) and set S_NOCMTIME. */
437 if (!(inode->i_flags & S_NOCMTIME))
438 inode->i_flags |= S_NOCMTIME;
443 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
444 handle_t *handle, struct dentry *child,
445 struct inode *inode, struct htree_lock *hlock)
449 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
450 if (rc == -ENOBUFS || rc == -ENOSPC) {
451 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
452 struct inode *parent = child->d_parent->d_inode;
453 struct lu_fid *fid = NULL;
455 rc2 = osd_get_lma(info, parent, child->d_parent, loa);
457 fid = &loa->loa_lma.lma_self_fid;
458 } else if (rc2 == -ENODATA) {
459 if (unlikely(parent == inode->i_sb->s_root->d_inode)) {
460 fid = &info->oti_fid3;
461 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
462 } else if (!osd->od_is_ost && osd->od_index == 0) {
463 fid = &info->oti_fid3;
464 lu_igif_build(fid, parent->i_ino,
465 parent->i_generation);
470 CWARN("%s: directory (inode: %lu, FID: "DFID") %s "
471 "maximum entry limit\n",
472 osd_name(osd), parent->i_ino, PFID(fid),
473 rc == -ENOSPC ? "has reached" : "is approaching");
475 CWARN("%s: directory (inode: %lu, FID: unknown) %s "
476 "maximum entry limit\n",
477 osd_name(osd), parent->i_ino,
478 rc == -ENOSPC ? "has reached" : "is approaching");
480 /* ignore such error now */
489 static struct inode *
490 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
491 struct osd_inode_id *id, struct lu_fid *fid)
493 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
497 inode = osd_iget(info, dev, id);
501 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
503 *fid = loa->loa_lma.lma_self_fid;
504 } else if (rc == -ENODATA) {
505 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
506 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
508 lu_igif_build(fid, inode->i_ino, inode->i_generation);
516 static struct inode *osd_iget_check(struct osd_thread_info *info,
517 struct osd_device *dev,
518 const struct lu_fid *fid,
519 struct osd_inode_id *id,
526 /* The cached OI mapping is trustable. If we cannot locate the inode
527 * via the cached OI mapping, then return the failure to the caller
528 * directly without further OI checking. */
531 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
534 if (!trusted && (rc == -ENOENT || rc == -ESTALE))
537 CDEBUG(D_INODE, "no inode for FID: "DFID", ino = %u, rc = %d\n",
538 PFID(fid), id->oii_ino, rc);
542 if (is_bad_inode(inode)) {
547 CDEBUG(D_INODE, "bad inode for FID: "DFID", ino = %u\n",
548 PFID(fid), id->oii_ino);
552 if (id->oii_gen != OSD_OII_NOGEN &&
553 inode->i_generation != id->oii_gen) {
558 CDEBUG(D_INODE, "unmatched inode for FID: "DFID", ino = %u, "
559 "oii_gen = %u, i_generation = %u\n", PFID(fid),
560 id->oii_ino, id->oii_gen, inode->i_generation);
564 if (inode->i_nlink == 0) {
569 CDEBUG(D_INODE, "stale inode for FID: "DFID", ino = %u\n",
570 PFID(fid), id->oii_ino);
574 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
578 __u32 saved_ino = id->oii_ino;
579 __u32 saved_gen = id->oii_gen;
582 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
584 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
585 /* XXX: There are four possible cases:
587 * Backup/restore caused the OI invalid.
589 * Someone unlinked the object but NOT removed
590 * the OI mapping, such as mount target device
591 * as ldiskfs, and modify something directly.
593 * Someone just removed the object between the
594 * former oi_lookup and the iget. It is normal.
595 * 4. Other failure cases.
597 * Generally, when the device is mounted, it will
598 * auto check whether the system is restored from
599 * file-level backup or not. We trust such detect
600 * to distinguish the 1st case from the 2nd case:
601 * if the OI files are consistent but may contain
602 * stale OI mappings because of case 2, if iget()
603 * returns -ENOENT or -ESTALE, then it should be
606 /* If the OI mapping was in OI file before the
607 * osd_iget_check(), but now, it is disappear,
608 * then it must be removed by race. That is a
609 * normal race case. */
612 /* It is the OI scrub updated the OI mapping by race.
613 * The new OI mapping must be valid. */
614 if (saved_ino != id->oii_ino ||
615 (saved_gen != id->oii_gen && saved_gen != OSD_OII_NOGEN)) {
624 if (dev->od_scrub.os_scrub.os_file.sf_flags &
626 /* It still can be the case 2, but we cannot
627 * distinguish it from the case 1. So return
628 * -EREMCHG to block current operation until
629 * OI scrub rebuilt the OI mappings. */
637 if (inode->i_generation == id->oii_gen)
642 if (id->oii_gen == OSD_OII_NOGEN)
643 osd_id_gen(id, inode->i_ino, inode->i_generation);
645 /* Do not update file c/mtime in ldiskfs.
646 * NB: we don't have any lock to protect this because we don't
647 * have reference on osd_object now, but contention with
648 * another lookup + attr_set can't happen in the tiny window
649 * between if (...) and set S_NOCMTIME. */
650 if (!(inode->i_flags & S_NOCMTIME))
651 inode->i_flags |= S_NOCMTIME;
668 * \retval +v: new filter_fid, does not contain self-fid
669 * \retval 0: filter_fid_old, contains self-fid
670 * \retval -v: other failure cases
672 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
673 struct dentry *dentry, struct lu_fid *fid)
675 struct filter_fid_old *ff = &info->oti_ff;
676 struct ost_id *ostid = &info->oti_ostid;
679 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
680 if (rc == sizeof(*ff)) {
682 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
683 rc = ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
685 * XXX: use 0 as the index for compatibility, the caller will
686 * handle index related issues when necessary.
689 ostid_to_fid(fid, ostid, 0);
690 } else if (rc == sizeof(struct filter_fid)) {
692 } else if (rc >= 0) {
699 static int osd_lma_self_repair(struct osd_thread_info *info,
700 struct osd_device *osd, struct inode *inode,
701 const struct lu_fid *fid, __u32 compat)
706 LASSERT(current->journal_info == NULL);
708 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
709 osd_dto_credits_noquota[DTO_XATTR_SET]);
712 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
717 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
719 CWARN("%s: cannot self repair the LMA: rc = %d\n",
721 ldiskfs_journal_stop(jh);
725 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
727 struct osd_thread_info *info = osd_oti_get(env);
728 struct osd_device *osd = osd_obj2dev(obj);
729 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
730 struct lustre_mdt_attrs *lma = &loa->loa_lma;
731 struct inode *inode = obj->oo_inode;
732 struct dentry *dentry = &info->oti_obj_dentry;
733 struct lu_fid *fid = NULL;
734 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
738 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
739 (void *)loa, sizeof(*loa));
740 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
741 fid = &lma->lma_self_fid;
742 rc = osd_get_idif(info, inode, dentry, fid);
743 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
744 /* For the given OST-object, if it has neither LMA nor
745 * FID in XATTR_NAME_FID, then the given FID (which is
746 * contained in the @obj, from client RPC for locating
747 * the OST-object) is trusted. We use it to generate
749 osd_lma_self_repair(info, osd, inode, rfid,
760 lustre_lma_swab(lma);
761 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
762 (CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT) &&
763 S_ISREG(inode->i_mode)))) {
764 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
765 "fid = "DFID", ino = %lu\n", osd_name(osd),
766 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
767 PFID(rfid), inode->i_ino);
770 fid = &lma->lma_self_fid;
774 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
775 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
776 struct ost_id *oi = &info->oti_ostid;
777 struct lu_fid *fid1 = &info->oti_fid3;
778 __u32 idx = fid_idif_ost_idx(rfid);
780 /* For old IDIF, the OST index is not part of the IDIF,
781 * Means that different OSTs may have the same IDIFs.
782 * Under such case, we need to make some compatible
783 * check to make sure to trigger OI scrub properly. */
784 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
785 /* Given @rfid is new, LMA is old. */
786 fid_to_ostid(fid, oi);
787 ostid_to_fid(fid1, oi, idx);
788 if (lu_fid_eq(fid1, rfid)) {
789 if (osd->od_index_in_idif)
790 osd_lma_self_repair(info, osd,
804 struct osd_check_lmv_buf {
805 #ifdef HAVE_DIR_CONTEXT
806 /* please keep it as first member */
807 struct dir_context ctx;
809 struct osd_thread_info *oclb_info;
810 struct osd_device *oclb_dev;
811 struct osd_idmap_cache *oclb_oic;
815 * It is called internally by ->readdir() to filter out the
816 * local slave object's FID of the striped directory.
818 * \retval 1 found the local slave's FID
819 * \retval 0 continue to check next item
820 * \retval -ve for failure
822 #ifdef HAVE_FILLDIR_USE_CTX
823 static int osd_stripe_dir_filldir(struct dir_context *buf,
825 static int osd_stripe_dir_filldir(void *buf,
827 const char *name, int namelen,
828 loff_t offset, __u64 ino, unsigned d_type)
830 struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
831 struct osd_thread_info *oti = oclb->oclb_info;
832 struct lu_fid *fid = &oti->oti_fid3;
833 struct osd_inode_id *id = &oti->oti_id3;
834 struct osd_device *dev = oclb->oclb_dev;
835 struct osd_idmap_cache *oic = oclb->oclb_oic;
843 sscanf(name + 1, SFID, RFID(fid));
844 if (!fid_is_sane(fid))
847 if (osd_remote_fid(oti->oti_env, dev, fid))
850 osd_id_gen(id, ino, OSD_OII_NOGEN);
851 inode = osd_iget(oti, dev, id);
853 return PTR_ERR(inode);
856 osd_add_oi_cache(oti, dev, id, fid);
860 rc = osd_oii_insert(dev, oic, true);
862 return rc == 0 ? 1 : rc;
865 /* When lookup item under striped directory, we need to locate the master
866 * MDT-object of the striped directory firstly, then the client will send
867 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
868 * and the item's name. If the system is restored from MDT file level backup,
869 * then before the OI scrub completely built the OI files, the OI mappings of
870 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
871 * not a problem for the master MDT-object. Because when locate the master
872 * MDT-object, we will do name based lookup (for the striped directory itself)
873 * firstly, during such process we can setup the correct OI mapping for the
874 * master MDT-object. But it will be trouble for the slave MDT-object. Because
875 * the client will not trigger name based lookup on the MDT to locate the slave
876 * MDT-object before locating item under the striped directory, then when
877 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
878 * is invalid and does not know what the right OI mapping is, then the MDT has
879 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
880 * the OI file, related OI mapping is unknown yet, please try again later. And
881 * then client will re-try the RPC again and again until related OI mapping has
882 * been updated. That is quite inefficient.
884 * To resolve above trouble, we will handle it as the following two cases:
886 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
887 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
888 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
889 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
890 * directly. Please check osd_fid_lookup().
892 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
893 * Under such case, during lookup the master MDT-object, we will lookup the
894 * slave MDT-object via readdir against the master MDT-object, because the
895 * slave MDT-objects information are stored as sub-directories with the name
896 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
897 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
898 * the correct OI mapping for the slave MDT-object. */
899 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
900 struct inode *inode, struct osd_idmap_cache *oic)
902 struct lu_buf *buf = &oti->oti_big_buf;
903 struct dentry *dentry = &oti->oti_obj_dentry;
904 struct file *filp = &oti->oti_file;
905 const struct file_operations *fops;
906 struct lmv_mds_md_v1 *lmv1;
907 struct osd_check_lmv_buf oclb = {
908 #ifdef HAVE_DIR_CONTEXT
909 .ctx.actor = osd_stripe_dir_filldir,
919 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, buf->lb_buf,
922 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, NULL, 0);
924 lu_buf_realloc(buf, rc);
925 if (buf->lb_buf == NULL)
926 GOTO(out, rc = -ENOMEM);
932 if (unlikely(rc == 0 || rc == -ENODATA))
938 if (unlikely(buf->lb_buf == NULL)) {
939 lu_buf_realloc(buf, rc);
940 if (buf->lb_buf == NULL)
941 GOTO(out, rc = -ENOMEM);
947 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
951 dentry->d_inode = inode;
952 dentry->d_sb = inode->i_sb;
954 filp->f_path.dentry = dentry;
955 filp->f_mode = FMODE_64BITHASH;
956 filp->f_mapping = inode->i_mapping;
958 filp->private_data = NULL;
959 set_file_inode(filp, inode);
961 #ifdef HAVE_DIR_CONTEXT
962 oclb.ctx.pos = filp->f_pos;
963 rc = fops->iterate(filp, &oclb.ctx);
964 filp->f_pos = oclb.ctx.pos;
966 rc = fops->readdir(filp, &oclb, osd_stripe_dir_filldir);
968 fops->release(inode, filp);
972 CDEBUG(D_LFSCK, "%s: fail to check LMV EA, inode = %lu/%u,"
973 DFID": rc = %d\n", osd_ino2name(inode),
974 inode->i_ino, inode->i_generation,
975 PFID(&oic->oic_fid), rc);
982 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
983 const struct lu_fid *fid,
984 const struct lu_object_conf *conf)
986 struct osd_thread_info *info;
987 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
988 struct osd_device *dev;
989 struct osd_idmap_cache *oic;
990 struct osd_inode_id *id;
991 struct inode *inode = NULL;
992 struct lustre_scrub *scrub;
993 struct scrub_file *sf;
994 __u32 flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT | SS_AUTO_FULL;
1000 bool trusted = true;
1001 bool updated = false;
1004 LINVRNT(osd_invariant(obj));
1005 LASSERT(obj->oo_inode == NULL);
1006 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
1008 dev = osd_dev(ldev);
1009 scrub = &dev->od_scrub.os_scrub;
1010 sf = &scrub->os_file;
1011 info = osd_oti_get(env);
1013 oic = &info->oti_cache;
1015 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
1018 /* For the object is created as locking anchor, or for the object to
1019 * be created on disk. No need to osd_oi_lookup() at here because FID
1020 * shouldn't never be re-used, if it's really a duplicate FID from
1021 * unexpected reason, we should be able to detect it later by calling
1022 * do_create->osd_oi_insert(). */
1023 if (conf && conf->loc_flags & LOC_F_NEW)
1024 GOTO(out, result = 0);
1026 /* Search order: 1. per-thread cache. */
1027 if (lu_fid_eq(fid, &oic->oic_fid) && likely(oic->oic_dev == dev)) {
1033 if (!list_empty(&scrub->os_inconsistent_items)) {
1034 /* Search order: 2. OI scrub pending list. */
1035 result = osd_oii_lookup(dev, fid, id);
1040 /* The OI mapping in the OI file can be updated by the OI scrub
1041 * when we locate the inode via FID. So it may be not trustable. */
1044 /* Search order: 3. OI files. */
1045 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1046 if (result == -ENOENT) {
1047 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
1048 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1049 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
1051 GOTO(out, result = 0);
1060 obj->oo_inode = NULL;
1061 /* for later passes through checks, not true on first pass */
1062 if (!IS_ERR_OR_NULL(inode))
1065 inode = osd_iget_check(info, dev, fid, id, trusted);
1066 if (!IS_ERR(inode)) {
1067 obj->oo_inode = inode;
1075 result = PTR_ERR(inode);
1076 if (result == -ENOENT || result == -ESTALE)
1077 GOTO(out, result = 0);
1079 if (result != -EREMCHG)
1083 /* We still have chance to get the valid inode: for the
1084 * object which is referenced by remote name entry, the
1085 * object on the local MDT will be linked under the dir
1086 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1088 * We do not know whether the object for the given FID
1089 * is referenced by some remote name entry or not, and
1090 * especially for DNE II, a multiple-linked object may
1091 * have many name entries reside on many MDTs.
1093 * To simplify the operation, OSD will not distinguish
1094 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1095 * only happened for the RPC from other MDT during the
1096 * OI scrub, or for the client side RPC with FID only,
1097 * such as FID to path, or from old connected client. */
1098 if (!remote && !fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1099 rc1 = osd_lookup_in_remote_parent(info, dev, fid, id);
1103 flags |= SS_AUTO_PARTIAL;
1104 flags &= ~SS_AUTO_FULL;
1109 if (thread_is_running(&scrub->os_thread)) {
1110 if (scrub->os_partial_scan && !scrub->os_in_join)
1113 if (IS_ERR_OR_NULL(inode) || result)
1114 GOTO(out, result = -EINPROGRESS);
1117 LASSERT(obj->oo_inode == inode);
1119 osd_add_oi_cache(info, dev, id, fid);
1120 osd_oii_insert(dev, oic, true);
1124 if (dev->od_noscrub) {
1126 GOTO(out, result = -EREMCHG);
1129 LASSERT(obj->oo_inode == inode);
1131 osd_add_oi_cache(info, dev, id, fid);
1136 rc1 = osd_scrub_start(env, dev, flags);
1137 LCONSOLE_WARN("%s: trigger OI scrub by RPC for the " DFID" with flags "
1138 "0x%x, rc = %d\n", osd_name(dev), PFID(fid), flags, rc1);
1139 if (rc1 && rc1 != -EALREADY)
1140 GOTO(out, result = -EREMCHG);
1142 if (IS_ERR_OR_NULL(inode) || result)
1143 GOTO(out, result = -EINPROGRESS);
1146 LASSERT(obj->oo_inode == inode);
1148 osd_add_oi_cache(info, dev, id, fid);
1149 osd_oii_insert(dev, oic, true);
1153 result = osd_check_lma(env, obj);
1157 LASSERTF(id->oii_ino == inode->i_ino &&
1158 id->oii_gen == inode->i_generation,
1159 "locate wrong inode for FID: "DFID", %u/%u => %ld/%u\n",
1160 PFID(fid), id->oii_ino, id->oii_gen,
1161 inode->i_ino, inode->i_generation);
1163 saved_ino = inode->i_ino;
1164 saved_gen = inode->i_generation;
1166 if (unlikely(result == -ENODATA)) {
1167 /* If the OI scrub updated the OI mapping by race, it
1168 * must be valid. Trust the inode that has no LMA EA. */
1172 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1174 /* The OI mapping is still there, the inode is still
1175 * valid. It is just becaues the inode has no LMA EA. */
1176 if (saved_ino == id->oii_ino &&
1177 saved_gen == id->oii_gen)
1180 /* It is the OI scrub updated the OI mapping by race.
1181 * The new OI mapping must be valid. */
1187 /* "result == -ENOENT" means that the OI mappinghas been
1188 * removed by race, so the inode belongs to other object.
1190 * Others error can be returned directly. */
1191 if (result == -ENOENT) {
1194 obj->oo_inode = NULL;
1199 if (result != -EREMCHG)
1204 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1205 /* "result == -ENOENT" means the cached OI mapping has been removed
1206 * from the OI file by race, above inode belongs to other object. */
1207 if (result == -ENOENT) {
1210 obj->oo_inode = NULL;
1211 GOTO(out, result = 0);
1217 if (saved_ino == id->oii_ino && saved_gen == id->oii_gen) {
1222 /* It is the OI scrub updated the OI mapping by race.
1223 * The new OI mapping must be valid. */
1229 obj->oo_compat_dot_created = 1;
1230 obj->oo_compat_dotdot_created = 1;
1232 if (S_ISDIR(inode->i_mode) &&
1233 (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1234 osd_check_lmv(info, dev, inode, oic);
1236 result = osd_attach_jinode(inode);
1241 GOTO(out, result = 0);
1243 LASSERT(!obj->oo_hl_head);
1244 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1246 GOTO(out, result = (!obj->oo_hl_head ? -ENOMEM : 0));
1249 if (result || !obj->oo_inode) {
1250 if (!IS_ERR_OR_NULL(inode))
1253 obj->oo_inode = NULL;
1255 fid_zero(&oic->oic_fid);
1258 LINVRNT(osd_invariant(obj));
1263 * Concurrency: shouldn't matter.
1265 static void osd_object_init0(struct osd_object *obj)
1267 LASSERT(obj->oo_inode != NULL);
1268 obj->oo_dt.do_body_ops = &osd_body_ops;
1269 obj->oo_dt.do_lu.lo_header->loh_attr |=
1270 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1274 * Concurrency: no concurrent access is possible that early in object
1277 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1278 const struct lu_object_conf *conf)
1280 struct osd_object *obj = osd_obj(l);
1283 LINVRNT(osd_invariant(obj));
1285 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1286 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1287 l->lo_header->loh_attr |= LOHA_EXISTS;
1291 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1292 if (unlikely(l->lo_header ==
1293 &osd_obj2dev(obj)->od_scrub.os_scrub.os_obj_header)) {
1294 /* For the OI_scrub object during OSD device init. */
1295 l->lo_header->loh_attr |= LOHA_EXISTS;
1296 obj->oo_dt.do_body_ops = &osd_body_ops;
1298 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1300 if (result == 0 && obj->oo_inode != NULL) {
1301 struct osd_thread_info *oti = osd_oti_get(env);
1302 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
1304 osd_object_init0(obj);
1305 result = osd_get_lma(oti, obj->oo_inode,
1306 &oti->oti_obj_dentry, loa);
1308 /* Convert LMAI flags to lustre LMA flags
1309 * and cache it to oo_lma_flags */
1311 lma_to_lustre_flags(loa->loa_lma.lma_incompat);
1312 } else if (result == -ENODATA) {
1317 LINVRNT(osd_invariant(obj));
1321 /* The first part of oxe_buf is xattr name, and is '\0' terminated.
1322 * The left part is for value, binary mode. */
1323 struct osd_xattr_entry {
1324 struct list_head oxe_list;
1328 struct rcu_head oxe_rcu;
1332 static int osd_oxc_get(struct osd_object *obj, const char *name,
1335 struct osd_xattr_entry *tmp;
1336 struct osd_xattr_entry *oxe = NULL;
1337 size_t namelen = strlen(name);
1342 list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1343 if (namelen == tmp->oxe_namelen &&
1344 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1351 GOTO(out, rc = -ENOENT);
1353 if (!oxe->oxe_exist)
1354 GOTO(out, rc = -ENODATA);
1357 rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1360 if (buf->lb_buf == NULL)
1363 if (buf->lb_len < rc)
1364 GOTO(out, rc = -ERANGE);
1366 memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1374 static void osd_oxc_free(struct rcu_head *head)
1376 struct osd_xattr_entry *oxe;
1378 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1379 OBD_FREE(oxe, oxe->oxe_len);
1382 static void osd_oxc_add(struct osd_object *obj, const char *name,
1383 const char *buf, int buflen)
1385 struct osd_xattr_entry *oxe;
1386 struct osd_xattr_entry *old = NULL;
1387 struct osd_xattr_entry *tmp;
1388 size_t namelen = strlen(name);
1389 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1391 OBD_ALLOC(oxe, len);
1395 INIT_LIST_HEAD(&oxe->oxe_list);
1397 oxe->oxe_namelen = namelen;
1398 memcpy(oxe->oxe_buf, name, namelen);
1400 LASSERT(buf != NULL);
1401 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1402 oxe->oxe_exist = true;
1404 oxe->oxe_exist = false;
1407 /* this should be rarely called, just remove old and add new */
1408 spin_lock(&obj->oo_guard);
1409 list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1410 if (namelen == tmp->oxe_namelen &&
1411 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1417 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1418 call_rcu(&old->oxe_rcu, osd_oxc_free);
1420 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1422 spin_unlock(&obj->oo_guard);
1425 static void osd_oxc_del(struct osd_object *obj, const char *name)
1427 struct osd_xattr_entry *oxe;
1428 size_t namelen = strlen(name);
1430 spin_lock(&obj->oo_guard);
1431 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1432 if (namelen == oxe->oxe_namelen &&
1433 strncmp(name, oxe->oxe_buf, namelen) == 0) {
1434 list_del_rcu(&oxe->oxe_list);
1435 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1439 spin_unlock(&obj->oo_guard);
1442 static void osd_oxc_fini(struct osd_object *obj)
1444 struct osd_xattr_entry *oxe, *next;
1446 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1447 list_del(&oxe->oxe_list);
1448 OBD_FREE(oxe, oxe->oxe_len);
1453 * Concurrency: no concurrent access is possible that late in object
1456 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1458 struct osd_object *obj = osd_obj(l);
1459 struct lu_object_header *h = NULL;
1461 LINVRNT(osd_invariant(obj));
1463 if (unlikely(l->lo_header ==
1464 &osd_obj2dev(obj)->od_scrub.os_scrub.os_obj_header))
1468 dt_object_fini(&obj->oo_dt);
1469 if (obj->oo_hl_head != NULL)
1470 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1473 lu_object_header_fini(h);
1477 * Concurrency: no concurrent access is possible that late in object
1480 static void osd_index_fini(struct osd_object *o)
1482 struct iam_container *bag;
1484 if (o->oo_dir != NULL) {
1485 bag = &o->oo_dir->od_container;
1486 if (o->oo_inode != NULL) {
1487 if (bag->ic_object == o->oo_inode)
1488 iam_container_fini(bag);
1490 OBD_FREE_PTR(o->oo_dir);
1496 * Concurrency: no concurrent access is possible that late in object
1497 * life-cycle (for all existing callers, that is. New callers have to provide
1498 * their own locking.)
1500 static int osd_inode_unlinked(const struct inode *inode)
1502 return inode->i_nlink == 0;
1506 OSD_TXN_OI_DELETE_CREDITS = 20,
1507 OSD_TXN_INODE_DELETE_CREDITS = 20
1514 #if OSD_THANDLE_STATS
1516 * Set time when the handle is allocated
1518 static void osd_th_alloced(struct osd_thandle *oth)
1520 oth->oth_alloced = ktime_get();
1524 * Set time when the handle started
1526 static void osd_th_started(struct osd_thandle *oth)
1528 oth->oth_started = ktime_get();
1532 * Check whether the we deal with this handle for too long.
1534 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1535 ktime_t alloced, ktime_t started,
1538 ktime_t now = ktime_get();
1540 LASSERT(dev != NULL);
1542 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1543 ktime_us_delta(started, alloced));
1544 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1545 ktime_us_delta(closed, started));
1546 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1547 ktime_us_delta(now, closed));
1549 if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1550 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1551 oth, now, alloced, started, closed);
1552 libcfs_debug_dumpstack(NULL);
1556 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1558 ktime_t __closed = ktime_get(); \
1559 ktime_t __alloced = oth->oth_alloced; \
1560 ktime_t __started = oth->oth_started; \
1563 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1566 #else /* OSD_THANDLE_STATS */
1568 #define osd_th_alloced(h) do {} while(0)
1569 #define osd_th_started(h) do {} while(0)
1570 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1572 #endif /* OSD_THANDLE_STATS */
1575 * Concurrency: doesn't access mutable data.
1577 static int osd_param_is_not_sane(const struct osd_device *dev,
1578 const struct thandle *th)
1580 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1582 return oh->ot_credits > osd_transaction_size(dev);
1586 * Concurrency: shouldn't matter.
1588 static void osd_trans_commit_cb(struct super_block *sb,
1589 struct ldiskfs_journal_cb_entry *jcb, int error)
1591 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1592 struct thandle *th = &oh->ot_super;
1593 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1594 struct dt_txn_commit_cb *dcb, *tmp;
1596 LASSERT(oh->ot_handle == NULL);
1599 CERROR("transaction @0x%p commit error: %d\n", th, error);
1601 dt_txn_hook_commit(th);
1603 /* call per-transaction callbacks if any */
1604 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1606 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1607 "commit callback entry: magic=%x name='%s'\n",
1608 dcb->dcb_magic, dcb->dcb_name);
1609 list_del_init(&dcb->dcb_linkage);
1610 dcb->dcb_func(NULL, th, dcb, error);
1613 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1620 #ifndef HAVE_SB_START_WRITE
1621 # define sb_start_write(sb) do {} while (0)
1622 # define sb_end_write(sb) do {} while (0)
1625 static struct thandle *osd_trans_create(const struct lu_env *env,
1626 struct dt_device *d)
1628 struct osd_thread_info *oti = osd_oti_get(env);
1629 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1630 struct osd_thandle *oh;
1635 CERROR("%s: someone try to start transaction under "
1636 "readonly mode, should be disabled.\n",
1637 osd_name(osd_dt_dev(d)));
1639 RETURN(ERR_PTR(-EROFS));
1642 /* on pending IO in this thread should left from prev. request */
1643 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1645 sb_start_write(osd_sb(osd_dt_dev(d)));
1647 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1649 oh->ot_quota_trans = &oti->oti_quota_trans;
1650 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1655 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1656 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1659 memset(oti->oti_declare_ops, 0,
1660 sizeof(oti->oti_declare_ops));
1661 memset(oti->oti_declare_ops_cred, 0,
1662 sizeof(oti->oti_declare_ops_cred));
1663 memset(oti->oti_declare_ops_used, 0,
1664 sizeof(oti->oti_declare_ops_used));
1666 sb_end_write(osd_sb(osd_dt_dev(d)));
1667 th = ERR_PTR(-ENOMEM);
1672 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1674 struct osd_thread_info *oti = osd_oti_get(env);
1675 struct osd_thandle *oh;
1677 oh = container_of0(th, struct osd_thandle, ot_super);
1678 LASSERT(oh != NULL);
1680 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1681 oti->oti_declare_ops[OSD_OT_CREATE],
1682 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1683 oti->oti_declare_ops_used[OSD_OT_CREATE],
1684 oti->oti_declare_ops[OSD_OT_DESTROY],
1685 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1686 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1687 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1688 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1689 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1690 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1691 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1692 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1693 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1694 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1695 oti->oti_declare_ops[OSD_OT_WRITE],
1696 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1697 oti->oti_declare_ops_used[OSD_OT_WRITE],
1698 oti->oti_declare_ops[OSD_OT_PUNCH],
1699 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1700 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1701 oti->oti_declare_ops[OSD_OT_QUOTA],
1702 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1703 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1704 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1705 oti->oti_declare_ops[OSD_OT_INSERT],
1706 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1707 oti->oti_declare_ops_used[OSD_OT_INSERT],
1708 oti->oti_declare_ops[OSD_OT_DELETE],
1709 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1710 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1711 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1712 oti->oti_declare_ops[OSD_OT_REF_ADD],
1713 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1714 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1715 oti->oti_declare_ops[OSD_OT_REF_DEL],
1716 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1717 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1721 * Concurrency: shouldn't matter.
1723 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1726 struct osd_thread_info *oti = osd_oti_get(env);
1727 struct osd_device *dev = osd_dt_dev(d);
1729 struct osd_thandle *oh;
1734 LASSERT(current->journal_info == NULL);
1736 oh = container_of0(th, struct osd_thandle, ot_super);
1737 LASSERT(oh != NULL);
1738 LASSERT(oh->ot_handle == NULL);
1740 rc = dt_txn_hook_start(env, d, th);
1744 if (unlikely(osd_param_is_not_sane(dev, th))) {
1745 static unsigned long last_printed;
1746 static int last_credits;
1748 /* don't make noise on a tiny testing systems
1749 * actual credits misuse will be caught anyway */
1750 if (last_credits != oh->ot_credits &&
1751 time_after(jiffies, last_printed +
1752 msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
1753 osd_transaction_size(dev) > 512) {
1754 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1755 oh->ot_credits, osd_transaction_size(dev));
1756 osd_trans_dump_creds(env, th);
1757 libcfs_debug_dumpstack(NULL);
1758 last_credits = oh->ot_credits;
1759 last_printed = jiffies;
1761 /* XXX Limit the credits to 'max_transaction_buffers', and
1762 * let the underlying filesystem to catch the error if
1763 * we really need so many credits.
1765 * This should be removed when we can calculate the
1766 * credits precisely. */
1767 oh->ot_credits = osd_transaction_size(dev);
1768 } else if (ldiskfs_track_declares_assert != 0) {
1769 /* reserve few credits to prevent an assertion in JBD
1770 * our debugging mechanism will be able to detected
1771 * overuse. this can help to debug single-update
1773 oh->ot_credits += 10;
1774 if (unlikely(osd_param_is_not_sane(dev, th)))
1775 oh->ot_credits = osd_transaction_size(dev);
1779 * XXX temporary stuff. Some abstraction layer should
1782 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1786 LASSERT(oti->oti_txns == 0);
1788 lu_device_get(&d->dd_lu_dev);
1789 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1800 static int osd_seq_exists(const struct lu_env *env,
1801 struct osd_device *osd, u64 seq)
1803 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1804 struct seq_server_site *ss = osd_seq_site(osd);
1808 LASSERT(ss != NULL);
1809 LASSERT(ss->ss_server_fld != NULL);
1811 rc = osd_fld_lookup(env, osd, seq, range);
1814 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
1815 osd_name(osd), seq, rc);
1819 RETURN(ss->ss_node_id == range->lsr_index);
1822 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1824 struct dt_txn_commit_cb *dcb;
1825 struct dt_txn_commit_cb *tmp;
1827 /* call per-transaction stop callbacks if any */
1828 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1830 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1831 "commit callback entry: magic=%x name='%s'\n",
1832 dcb->dcb_magic, dcb->dcb_name);
1833 list_del_init(&dcb->dcb_linkage);
1834 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1839 * Concurrency: shouldn't matter.
1841 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1844 int rc = 0, remove_agents = 0;
1845 struct osd_thandle *oh;
1846 struct osd_thread_info *oti = osd_oti_get(env);
1847 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1848 struct osd_device *osd = osd_dt_dev(th->th_dev);
1849 struct qsd_instance *qsd = osd->od_quota_slave;
1850 struct lquota_trans *qtrans;
1853 oh = container_of0(th, struct osd_thandle, ot_super);
1855 /* reset OI cache for safety */
1856 oti->oti_ins_cache_used = 0;
1858 remove_agents = oh->ot_remove_agents;
1860 qtrans = oh->ot_quota_trans;
1861 oh->ot_quota_trans = NULL;
1863 if (oh->ot_handle != NULL) {
1865 handle_t *hdl = oh->ot_handle;
1868 * add commit callback
1869 * notice we don't do this in osd_trans_start()
1870 * as underlying transaction can change during truncate
1872 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1875 LASSERT(oti->oti_txns == 1);
1878 rc = dt_txn_hook_stop(env, th);
1880 CERROR("%s: failed in transaction hook: rc = %d\n",
1883 osd_trans_stop_cb(oh, rc);
1884 /* hook functions might modify th_sync */
1885 hdl->h_sync = th->th_sync;
1887 oh->ot_handle = NULL;
1888 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
1890 CERROR("%s: failed to stop transaction: rc = %d\n",
1891 osd_name(osd), rc2);
1895 osd_trans_stop_cb(oh, th->th_result);
1899 /* inform the quota slave device that the transaction is stopping */
1900 qsd_op_end(env, qsd, qtrans);
1902 /* as we want IO to journal and data IO be concurrent, we don't block
1903 * awaiting data IO completion in osd_do_bio(), instead we wait here
1904 * once transaction is submitted to the journal. all reqular requests
1905 * don't do direct IO (except read/write), thus this wait_event becomes
1908 * IMPORTANT: we have to wait till any IO submited by the thread is
1909 * completed otherwise iobuf may be corrupted by different request
1911 wait_event(iobuf->dr_wait,
1912 atomic_read(&iobuf->dr_numreqs) == 0);
1913 osd_fini_iobuf(osd, iobuf);
1915 rc = iobuf->dr_error;
1917 if (unlikely(remove_agents != 0))
1918 osd_process_scheduled_agent_removals(env, osd);
1920 sb_end_write(osd_sb(osd));
1925 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1927 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1930 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1931 LASSERT(&dcb->dcb_func != NULL);
1932 if (dcb->dcb_flags & DCB_TRANS_STOP)
1933 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1935 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1941 * Called just before object is freed. Releases all resources except for
1942 * object itself (that is released by osd_object_free()).
1944 * Concurrency: no concurrent access is possible that late in object
1947 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1949 struct osd_object *obj = osd_obj(l);
1950 struct inode *inode = obj->oo_inode;
1952 LINVRNT(osd_invariant(obj));
1955 * If object is unlinked remove fid->ino mapping from object index.
1958 osd_index_fini(obj);
1959 if (inode != NULL) {
1960 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1961 qid_t uid = i_uid_read(inode);
1962 qid_t gid = i_gid_read(inode);
1965 obj->oo_inode = NULL;
1968 struct osd_thread_info *info = osd_oti_get(env);
1969 struct lquota_id_info *qi = &info->oti_qi;
1971 /* Release granted quota to master if necessary */
1972 qi->lqi_id.qid_uid = uid;
1973 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1975 qi->lqi_id.qid_uid = gid;
1976 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1978 qi->lqi_id.qid_uid = i_projid_read(inode);
1979 qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
1985 * Concurrency: ->loo_object_release() is called under site spin-lock.
1987 static void osd_object_release(const struct lu_env *env,
1988 struct lu_object *l)
1990 struct osd_object *o = osd_obj(l);
1991 /* nobody should be releasing a non-destroyed object with nlink=0
1992 * the API allows this, but ldiskfs doesn't like and then report
1993 * this inode as deleted */
1994 if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
1999 * Concurrency: shouldn't matter.
2001 static int osd_object_print(const struct lu_env *env, void *cookie,
2002 lu_printer_t p, const struct lu_object *l)
2004 struct osd_object *o = osd_obj(l);
2005 struct iam_descr *d;
2007 if (o->oo_dir != NULL)
2008 d = o->oo_dir->od_container.ic_descr;
2011 return (*p)(env, cookie,
2012 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
2014 o->oo_inode ? o->oo_inode->i_ino : 0UL,
2015 o->oo_inode ? o->oo_inode->i_generation : 0,
2016 d ? d->id_ops->id_name : "plain");
2020 * Concurrency: shouldn't matter.
2022 int osd_statfs(const struct lu_env *env, struct dt_device *d,
2023 struct obd_statfs *sfs)
2025 struct osd_device *osd = osd_dt_dev(d);
2026 struct super_block *sb = osd_sb(osd);
2027 struct kstatfs *ksfs;
2031 if (unlikely(osd->od_mnt == NULL))
2032 return -EINPROGRESS;
2034 /* osd_lproc.c call this without env, allocate ksfs for that case */
2035 if (unlikely(env == NULL)) {
2036 OBD_ALLOC_PTR(ksfs);
2040 ksfs = &osd_oti_get(env)->oti_ksfs;
2043 result = sb->s_op->statfs(sb->s_root, ksfs);
2047 statfs_pack(sfs, ksfs);
2048 if (unlikely(sb->s_flags & MS_RDONLY))
2049 sfs->os_state |= OS_STATE_READONLY;
2050 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
2051 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2052 sfs->os_maxbytes = sb->s_maxbytes;
2054 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2057 * Reserve some space so to avoid fragmenting the filesystem too much.
2058 * Fragmentation not only impacts performance, but can also increase
2059 * metadata overhead significantly, causing grant calculation to be
2062 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2064 CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
2065 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2066 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2067 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2069 sfs->os_blocks -= reserved;
2070 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2071 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2074 if (unlikely(env == NULL))
2080 * Estimate space needed for file creations. We assume the largest filename
2081 * which is 2^64 - 1, hence a filename of 20 chars.
2082 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2084 #ifdef __LDISKFS_DIR_REC_LEN
2085 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2087 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2091 * Concurrency: doesn't access mutable data.
2093 static void osd_conf_get(const struct lu_env *env,
2094 const struct dt_device *dev,
2095 struct dt_device_param *param)
2097 struct super_block *sb = osd_sb(osd_dt_dev(dev));
2101 * XXX should be taken from not-yet-existing fs abstraction layer.
2103 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2104 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2105 param->ddp_symlink_max = sb->s_blocksize;
2106 param->ddp_mount_type = LDD_MT_LDISKFS;
2107 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2108 param->ddp_maxbytes = sb->s_maxbytes;
2110 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2111 /* inode are statically allocated, so per-inode space consumption
2112 * is the space consumed by the directory entry */
2113 param->ddp_inodespace = PER_OBJ_USAGE;
2114 /* EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2115 * = 128MB) which is unlikely to be hit in real life. Report a smaller
2116 * maximum length to not under count the actual number of extents
2117 * needed for writing a file. */
2118 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
2119 /* worst-case extent insertion metadata overhead */
2120 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2121 param->ddp_mntopts = 0;
2122 if (test_opt(sb, XATTR_USER))
2123 param->ddp_mntopts |= MNTOPT_USERXATTR;
2124 if (test_opt(sb, POSIX_ACL))
2125 param->ddp_mntopts |= MNTOPT_ACL;
2127 /* LOD might calculate the max stripe count based on max_ea_size,
2128 * so we need take account in the overhead as well,
2129 * xattr_header + magic + xattr_entry_head */
2130 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2131 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2133 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2134 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
2135 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2139 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2143 * Concurrency: shouldn't matter.
2145 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2149 CDEBUG(D_CACHE, "%s: syncing OSD\n", osd_dt_dev(d)->od_svname);
2151 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
2153 CDEBUG(D_CACHE, "%s: synced OSD: rc = %d\n", osd_dt_dev(d)->od_svname,
2160 * Start commit for OSD device.
2162 * An implementation of dt_commit_async method for OSD device.
2163 * Asychronously starts underlayng fs sync and thereby a transaction
2166 * \param env environment
2167 * \param d dt device
2169 * \see dt_device_operations
2171 static int osd_commit_async(const struct lu_env *env,
2172 struct dt_device *d)
2174 struct super_block *s = osd_sb(osd_dt_dev(d));
2177 CDEBUG(D_HA, "%s: async commit OSD\n", osd_dt_dev(d)->od_svname);
2178 RETURN(s->s_op->sync_fs(s, 0));
2181 /* Our own copy of the set readonly functions if present, or NU if not. */
2182 static int (*priv_dev_set_rdonly)(struct block_device *bdev);
2183 static int (*priv_dev_check_rdonly)(struct block_device *bdev);
2184 /* static int (*priv_dev_clear_rdonly)(struct block_device *bdev); */
2187 * Concurrency: shouldn't matter.
2189 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2191 struct super_block *sb = osd_sb(osd_dt_dev(d));
2192 struct block_device *dev = sb->s_bdev;
2193 int rc = -EOPNOTSUPP;
2196 if (priv_dev_set_rdonly) {
2197 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
2200 CERROR("*** setting %s read-only ***\n",
2201 osd_dt_dev(d)->od_svname);
2203 if (sb->s_op->freeze_fs) {
2204 rc = sb->s_op->freeze_fs(sb);
2209 if (jdev && (jdev != dev)) {
2210 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
2212 priv_dev_set_rdonly(jdev);
2214 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
2215 priv_dev_set_rdonly(dev);
2217 if (sb->s_op->unfreeze_fs)
2218 sb->s_op->unfreeze_fs(sb);
2223 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2224 osd_dt_dev(d)->od_svname, (long)dev, rc);
2230 * Note: we do not count into QUOTA here.
2231 * If we mount with --data_journal we may need more.
2233 const int osd_dto_credits_noquota[DTO_NR] = {
2236 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2237 * SINGLEDATA_TRANS_BLOCKS(8)
2238 * XXX Note: maybe iam need more, since iam have more level than
2241 [DTO_INDEX_INSERT] = 16,
2244 * just modify a single entry, probably merge few within a block
2246 [DTO_INDEX_DELETE] = 1,
2250 [DTO_INDEX_UPDATE] = 16,
2252 * 4(inode, inode bits, groups, GDT)
2253 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2255 [DTO_OBJECT_CREATE] = 4,
2257 * 4(inode, inode bits, groups, GDT)
2258 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2260 [DTO_OBJECT_DELETE] = 4,
2262 * Attr set credits (inode)
2264 [DTO_ATTR_SET_BASE] = 1,
2266 * Xattr set. The same as xattr of EXT3.
2267 * DATA_TRANS_BLOCKS(14)
2268 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2269 * are also counted in. Do not know why?
2271 [DTO_XATTR_SET] = 14,
2273 * credits for inode change during write.
2275 [DTO_WRITE_BASE] = 3,
2277 * credits for single block write.
2279 [DTO_WRITE_BLOCK] = 14,
2281 * Attr set credits for chown.
2282 * This is extra credits for setattr, and it is null without quota
2284 [DTO_ATTR_SET_CHOWN] = 0
2287 static const struct dt_device_operations osd_dt_ops = {
2288 .dt_root_get = osd_root_get,
2289 .dt_statfs = osd_statfs,
2290 .dt_trans_create = osd_trans_create,
2291 .dt_trans_start = osd_trans_start,
2292 .dt_trans_stop = osd_trans_stop,
2293 .dt_trans_cb_add = osd_trans_cb_add,
2294 .dt_conf_get = osd_conf_get,
2295 .dt_sync = osd_sync,
2297 .dt_commit_async = osd_commit_async,
2300 static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
2303 struct osd_object *obj = osd_dt_obj(dt);
2304 struct osd_thread_info *oti = osd_oti_get(env);
2306 LINVRNT(osd_invariant(obj));
2308 LASSERT(obj->oo_owner != env);
2309 down_read_nested(&obj->oo_sem, role);
2311 LASSERT(obj->oo_owner == NULL);
2315 static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
2318 struct osd_object *obj = osd_dt_obj(dt);
2319 struct osd_thread_info *oti = osd_oti_get(env);
2321 LINVRNT(osd_invariant(obj));
2323 LASSERT(obj->oo_owner != env);
2324 down_write_nested(&obj->oo_sem, role);
2326 LASSERT(obj->oo_owner == NULL);
2327 obj->oo_owner = env;
2331 static void osd_read_unlock(const struct lu_env *env, struct dt_object *dt)
2333 struct osd_object *obj = osd_dt_obj(dt);
2334 struct osd_thread_info *oti = osd_oti_get(env);
2336 LINVRNT(osd_invariant(obj));
2338 LASSERT(oti->oti_r_locks > 0);
2340 up_read(&obj->oo_sem);
2343 static void osd_write_unlock(const struct lu_env *env, struct dt_object *dt)
2345 struct osd_object *obj = osd_dt_obj(dt);
2346 struct osd_thread_info *oti = osd_oti_get(env);
2348 LINVRNT(osd_invariant(obj));
2350 LASSERT(obj->oo_owner == env);
2351 LASSERT(oti->oti_w_locks > 0);
2353 obj->oo_owner = NULL;
2354 up_write(&obj->oo_sem);
2357 static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
2359 struct osd_object *obj = osd_dt_obj(dt);
2361 LINVRNT(osd_invariant(obj));
2363 return obj->oo_owner == env;
2366 static struct timespec *osd_inode_time(const struct lu_env *env,
2367 struct inode *inode, __u64 seconds)
2369 struct osd_thread_info *oti = osd_oti_get(env);
2370 struct timespec *t = &oti->oti_time;
2372 t->tv_sec = seconds;
2374 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
2378 static void osd_inode_getattr(const struct lu_env *env,
2379 struct inode *inode, struct lu_attr *attr)
2381 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2382 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2383 LA_PROJID | LA_FLAGS | LA_NLINK | LA_RDEV |
2384 LA_BLKSIZE | LA_TYPE;
2386 attr->la_atime = LTIME_S(inode->i_atime);
2387 attr->la_mtime = LTIME_S(inode->i_mtime);
2388 attr->la_ctime = LTIME_S(inode->i_ctime);
2389 attr->la_mode = inode->i_mode;
2390 attr->la_size = i_size_read(inode);
2391 attr->la_blocks = inode->i_blocks;
2392 attr->la_uid = i_uid_read(inode);
2393 attr->la_gid = i_gid_read(inode);
2394 attr->la_projid = i_projid_read(inode);
2395 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2396 attr->la_nlink = inode->i_nlink;
2397 attr->la_rdev = inode->i_rdev;
2398 attr->la_blksize = 1 << inode->i_blkbits;
2399 attr->la_blkbits = inode->i_blkbits;
2401 * Ext4 did not transfer inherit flags from raw inode
2402 * to inode flags, and ext4 internally test raw inode
2403 * @i_flags directly. Instead of patching ext4, we do it here.
2405 if (LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL)
2406 attr->la_flags |= LUSTRE_PROJINHERIT_FL;
2409 static int osd_attr_get(const struct lu_env *env,
2410 struct dt_object *dt,
2411 struct lu_attr *attr)
2413 struct osd_object *obj = osd_dt_obj(dt);
2415 if (unlikely(!dt_object_exists(dt)))
2417 if (unlikely(obj->oo_destroyed))
2420 LASSERT(!dt_object_remote(dt));
2421 LINVRNT(osd_invariant(obj));
2423 spin_lock(&obj->oo_guard);
2424 osd_inode_getattr(env, obj->oo_inode, attr);
2425 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
2426 attr->la_flags |= LUSTRE_ORPHAN_FL;
2427 spin_unlock(&obj->oo_guard);
2432 static int osd_declare_attr_qid(const struct lu_env *env,
2433 struct osd_object *obj,
2434 struct osd_thandle *oh, long long bspace,
2435 qid_t old_id, qid_t new_id, bool enforce,
2439 struct osd_thread_info *info = osd_oti_get(env);
2440 struct lquota_id_info *qi = &info->oti_qi;
2442 qi->lqi_type = type;
2443 /* inode accounting */
2444 qi->lqi_is_blk = false;
2446 /* one more inode for the new id ... */
2447 qi->lqi_id.qid_uid = new_id;
2449 /* Reserve credits for the new id */
2450 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2451 if (rc == -EDQUOT || rc == -EINPROGRESS)
2456 /* and one less inode for the current id */
2457 qi->lqi_id.qid_uid = old_id;
2459 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2460 if (rc == -EDQUOT || rc == -EINPROGRESS)
2465 /* block accounting */
2466 qi->lqi_is_blk = true;
2468 /* more blocks for the new id ... */
2469 qi->lqi_id.qid_uid = new_id;
2470 qi->lqi_space = bspace;
2472 * Credits for the new uid has been reserved, re-use "obj"
2473 * to save credit reservation.
2475 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2476 if (rc == -EDQUOT || rc == -EINPROGRESS)
2481 /* and finally less blocks for the current uid */
2482 qi->lqi_id.qid_uid = old_id;
2483 qi->lqi_space = -bspace;
2484 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2485 if (rc == -EDQUOT || rc == -EINPROGRESS)
2491 static int osd_declare_attr_set(const struct lu_env *env,
2492 struct dt_object *dt,
2493 const struct lu_attr *attr,
2494 struct thandle *handle)
2496 struct osd_thandle *oh;
2497 struct osd_object *obj;
2505 LASSERT(dt != NULL);
2506 LASSERT(handle != NULL);
2508 obj = osd_dt_obj(dt);
2509 LASSERT(osd_invariant(obj));
2511 oh = container_of0(handle, struct osd_thandle, ot_super);
2512 LASSERT(oh->ot_handle == NULL);
2514 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2515 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2517 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2518 osd_dto_credits_noquota[DTO_XATTR_SET]);
2520 if (attr == NULL || obj->oo_inode == NULL)
2523 bspace = obj->oo_inode->i_blocks;
2524 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2525 bspace = toqb(bspace);
2527 /* Changing ownership is always preformed by super user, it should not
2530 * We still need to call the osd_declare_qid() to calculate the journal
2531 * credits for updating quota accounting files and to trigger quota
2532 * space adjustment once the operation is completed.*/
2533 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2535 uid = i_uid_read(obj->oo_inode);
2536 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2537 rc = osd_declare_attr_qid(env, obj, oh, bspace, uid,
2538 attr->la_uid, enforce, USRQUOTA);
2542 gid = i_gid_read(obj->oo_inode);
2543 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2544 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2545 i_gid_read(obj->oo_inode),
2546 attr->la_gid, enforce, GRPQUOTA);
2551 #ifdef HAVE_PROJECT_QUOTA
2552 if (attr->la_valid & LA_PROJID) {
2553 __u32 projid = i_projid_read(obj->oo_inode);
2554 enforce = (attr->la_valid & LA_PROJID) &&
2555 (attr->la_projid != projid);
2556 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2557 (qid_t)projid, (qid_t)attr->la_projid,
2566 static int osd_inode_setattr(const struct lu_env *env,
2567 struct inode *inode, const struct lu_attr *attr)
2569 __u64 bits = attr->la_valid;
2571 /* Only allow set size for regular file */
2572 if (!S_ISREG(inode->i_mode))
2573 bits &= ~(LA_SIZE | LA_BLOCKS);
2578 if (bits & LA_ATIME)
2579 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2580 if (bits & LA_CTIME)
2581 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2582 if (bits & LA_MTIME)
2583 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2584 if (bits & LA_SIZE) {
2585 spin_lock(&inode->i_lock);
2586 LDISKFS_I(inode)->i_disksize = attr->la_size;
2587 i_size_write(inode, attr->la_size);
2588 spin_unlock(&inode->i_lock);
2591 /* OSD should not change "i_blocks" which is used by quota.
2592 * "i_blocks" should be changed by ldiskfs only. */
2594 inode->i_mode = (inode->i_mode & S_IFMT) |
2595 (attr->la_mode & ~S_IFMT);
2597 i_uid_write(inode, attr->la_uid);
2599 i_gid_write(inode, attr->la_gid);
2600 if (bits & LA_PROJID)
2601 i_projid_write(inode, attr->la_projid);
2602 if (bits & LA_NLINK)
2603 set_nlink(inode, attr->la_nlink);
2605 inode->i_rdev = attr->la_rdev;
2607 if (bits & LA_FLAGS) {
2608 /* always keep S_NOCMTIME */
2609 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2612 * Ext4 did not transfer inherit flags from
2613 * @inode->i_flags to raw inode i_flags when writing
2614 * flags, we do it explictly here.
2616 if (attr->la_flags & LUSTRE_PROJINHERIT_FL)
2617 LDISKFS_I(inode)->i_flags |= LUSTRE_PROJINHERIT_FL;
2619 LDISKFS_I(inode)->i_flags &= ~LUSTRE_PROJINHERIT_FL;
2624 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2628 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2629 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2632 ll_vfs_dq_init(inode);
2634 if (attr->la_valid & LA_UID)
2635 iattr.ia_valid |= ATTR_UID;
2636 if (attr->la_valid & LA_GID)
2637 iattr.ia_valid |= ATTR_GID;
2638 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2639 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2641 rc = ll_vfs_dq_transfer(inode, &iattr);
2643 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2644 "enforcement enabled on the ldiskfs "
2645 "filesystem?\n", inode->i_sb->s_id, rc);
2650 /* Handle project id transfer here properly */
2651 if (attr->la_valid & LA_PROJID &&
2652 attr->la_projid != i_projid_read(inode)) {
2653 #ifdef HAVE_PROJECT_QUOTA
2654 rc = ldiskfs_transfer_project(inode, attr->la_projid);
2659 CERROR("%s: quota transfer failed: rc = %d. Is project "
2660 "enforcement enabled on the ldiskfs "
2661 "filesystem?\n", inode->i_sb->s_id, rc);
2668 static int osd_attr_set(const struct lu_env *env,
2669 struct dt_object *dt,
2670 const struct lu_attr *attr,
2671 struct thandle *handle)
2673 struct osd_object *obj = osd_dt_obj(dt);
2674 struct inode *inode;
2677 if (!dt_object_exists(dt))
2680 LASSERT(handle != NULL);
2681 LASSERT(!dt_object_remote(dt));
2682 LASSERT(osd_invariant(obj));
2684 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2686 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) &&
2687 !osd_obj2dev(obj)->od_is_ost) {
2688 struct osd_thread_info *oti = osd_oti_get(env);
2689 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2690 struct lu_fid *fid1 = &oti->oti_fid;
2691 struct osd_inode_id *id = &oti->oti_id;
2692 struct iam_path_descr *ipd;
2693 struct iam_container *bag;
2694 struct osd_thandle *oh;
2697 fid_cpu_to_be(fid1, fid0);
2698 memset(id, 1, sizeof(*id));
2699 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2700 fid0)->oi_dir.od_container;
2701 ipd = osd_idx_ipd_get(env, bag);
2702 if (unlikely(ipd == NULL))
2705 oh = container_of0(handle, struct osd_thandle, ot_super);
2706 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2707 (const struct iam_rec *)id, ipd);
2708 osd_ipd_put(env, bag, ipd);
2709 return(rc > 0 ? 0 : rc);
2712 inode = obj->oo_inode;
2714 rc = osd_quota_transfer(inode, attr);
2718 spin_lock(&obj->oo_guard);
2719 rc = osd_inode_setattr(env, inode, attr);
2720 spin_unlock(&obj->oo_guard);
2724 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2726 if (!(attr->la_valid & LA_FLAGS))
2729 /* Let's check if there are extra flags need to be set into LMA */
2730 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2731 struct osd_thread_info *info = osd_oti_get(env);
2732 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
2734 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
2735 &info->oti_ost_attrs);
2739 lma->lma_incompat |=
2740 lustre_to_lma_flags(attr->la_flags);
2741 lustre_lma_swab(lma);
2742 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2743 lma, sizeof(*lma), XATTR_REPLACE);
2745 struct osd_device *osd = osd_obj2dev(obj);
2747 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2748 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2749 lma->lma_incompat, rc);
2752 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2754 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2757 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2762 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2763 struct osd_object *obj,
2764 const char *name, const int namelen)
2766 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2769 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2770 umode_t mode, struct dt_allocation_hint *hint,
2774 struct osd_device *osd = osd_obj2dev(obj);
2775 struct osd_thandle *oth;
2776 struct dt_object *parent = NULL;
2777 struct inode *inode;
2779 LINVRNT(osd_invariant(obj));
2780 LASSERT(obj->oo_inode == NULL);
2781 LASSERT(obj->oo_hl_head == NULL);
2783 if (S_ISDIR(mode) && ldiskfs_pdo) {
2784 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2785 if (obj->oo_hl_head == NULL)
2789 oth = container_of(th, struct osd_thandle, ot_super);
2790 LASSERT(oth->ot_handle->h_transaction != NULL);
2792 if (hint != NULL && hint->dah_parent != NULL &&
2793 !dt_object_remote(hint->dah_parent))
2794 parent = hint->dah_parent;
2796 inode = ldiskfs_create_inode(oth->ot_handle,
2797 parent ? osd_dt_obj(parent)->oo_inode :
2798 osd_sb(osd)->s_root->d_inode,
2800 if (!IS_ERR(inode)) {
2801 /* Do not update file c/mtime in ldiskfs. */
2802 inode->i_flags |= S_NOCMTIME;
2804 /* For new created object, it must be consistent,
2805 * and it is unnecessary to scrub against it. */
2806 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2808 obj->oo_inode = inode;
2811 if (obj->oo_hl_head != NULL) {
2812 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2813 obj->oo_hl_head = NULL;
2815 result = PTR_ERR(inode);
2817 LINVRNT(osd_invariant(obj));
2825 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2826 struct lu_attr *attr,
2827 struct dt_allocation_hint *hint,
2828 struct dt_object_format *dof,
2832 struct osd_thandle *oth;
2833 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
2835 LASSERT(S_ISDIR(attr->la_mode));
2837 oth = container_of(th, struct osd_thandle, ot_super);
2838 LASSERT(oth->ot_handle->h_transaction != NULL);
2839 result = osd_mkfile(info, obj, mode, hint, th);
2844 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2845 struct lu_attr *attr,
2846 struct dt_allocation_hint *hint,
2847 struct dt_object_format *dof,
2851 struct osd_thandle *oth;
2852 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2854 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2856 LASSERT(S_ISREG(attr->la_mode));
2858 oth = container_of(th, struct osd_thandle, ot_super);
2859 LASSERT(oth->ot_handle->h_transaction != NULL);
2861 result = osd_mkfile(info, obj, mode, hint, th);
2863 LASSERT(obj->oo_inode != NULL);
2864 if (feat->dif_flags & DT_IND_VARKEY)
2865 result = iam_lvar_create(obj->oo_inode,
2866 feat->dif_keysize_max,
2868 feat->dif_recsize_max,
2871 result = iam_lfix_create(obj->oo_inode,
2872 feat->dif_keysize_max,
2874 feat->dif_recsize_max,
2881 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2882 struct lu_attr *attr,
2883 struct dt_allocation_hint *hint,
2884 struct dt_object_format *dof,
2887 LASSERT(S_ISREG(attr->la_mode));
2888 return osd_mkfile(info, obj, (attr->la_mode &
2889 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2892 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2893 struct lu_attr *attr,
2894 struct dt_allocation_hint *hint,
2895 struct dt_object_format *dof,
2898 LASSERT(S_ISLNK(attr->la_mode));
2899 return osd_mkfile(info, obj, (attr->la_mode &
2900 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2903 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2904 struct lu_attr *attr,
2905 struct dt_allocation_hint *hint,
2906 struct dt_object_format *dof,
2909 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2912 LINVRNT(osd_invariant(obj));
2913 LASSERT(obj->oo_inode == NULL);
2914 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2915 S_ISFIFO(mode) || S_ISSOCK(mode));
2917 result = osd_mkfile(info, obj, mode, hint, th);
2919 LASSERT(obj->oo_inode != NULL);
2921 * This inode should be marked dirty for i_rdev. Currently
2922 * that is done in the osd_attr_init().
2924 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2927 LINVRNT(osd_invariant(obj));
2931 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2933 struct dt_allocation_hint *hint,
2934 struct dt_object_format *dof,
2937 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2939 osd_obj_type_f result;
2955 result = osd_mk_index;
2966 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2967 struct dt_object *parent, struct dt_object *child,
2972 ah->dah_parent = parent;
2973 ah->dah_mode = child_mode;
2975 if (parent != NULL && !dt_object_remote(parent)) {
2976 /* will help to find FID->ino at dt_insert("..") */
2977 struct osd_object *pobj = osd_dt_obj(parent);
2978 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2982 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2983 struct lu_attr *attr, struct dt_object_format *dof)
2985 struct inode *inode = obj->oo_inode;
2986 __u64 valid = attr->la_valid;
2989 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2991 if (dof->dof_type != DFT_NODE)
2992 attr->la_valid &= ~LA_RDEV;
2993 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2994 attr->la_valid &= ~LA_ATIME;
2995 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2996 attr->la_valid &= ~LA_CTIME;
2997 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2998 attr->la_valid &= ~LA_MTIME;
3000 result = osd_quota_transfer(inode, attr);
3004 if (attr->la_valid != 0) {
3005 result = osd_inode_setattr(info->oti_env, inode, attr);
3007 * The osd_inode_setattr() should always succeed here. The
3008 * only error that could be returned is EDQUOT when we are
3009 * trying to change the UID or GID of the inode. However, this
3010 * should not happen since quota enforcement is no longer
3011 * enabled on ldiskfs (lquota takes care of it).
3013 LASSERTF(result == 0, "%d\n", result);
3014 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3017 attr->la_valid = valid;
3021 * Helper function for osd_create()
3023 * \retval 0, on success
3025 static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
3026 struct lu_attr *attr, struct dt_allocation_hint *hint,
3027 struct dt_object_format *dof, struct thandle *th)
3032 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
3034 /* we drop umask so that permissions we pass are not affected */
3035 umask = current->fs->umask;
3036 current->fs->umask = 0;
3038 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
3040 if (likely(obj->oo_inode != NULL)) {
3041 LASSERT(obj->oo_inode->i_state & I_NEW);
3043 /* Unlock the inode before attr initialization to avoid
3044 * unnecessary dqget operations. LU-6378 */
3045 unlock_new_inode(obj->oo_inode);
3048 if (likely(result == 0)) {
3049 osd_attr_init(info, obj, attr, dof);
3050 osd_object_init0(obj);
3053 /* restore previous umask value */
3054 current->fs->umask = umask;
3056 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
3062 * Helper function for osd_create()
3064 * \retval 0, on success
3066 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
3067 const struct lu_fid *fid, struct thandle *th)
3069 struct osd_thread_info *info = osd_oti_get(env);
3070 struct osd_inode_id *id = &info->oti_id;
3071 struct osd_device *osd = osd_obj2dev(obj);
3072 struct osd_thandle *oh;
3075 LASSERT(obj->oo_inode != NULL);
3077 oh = container_of0(th, struct osd_thandle, ot_super);
3078 LASSERT(oh->ot_handle);
3079 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3081 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3082 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3083 OI_CHECK_FLD, NULL);
3084 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3089 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3090 u64 seq, struct lu_seq_range *range)
3092 struct seq_server_site *ss = osd_seq_site(osd);
3094 if (fid_seq_is_idif(seq)) {
3095 fld_range_set_ost(range);
3096 range->lsr_index = idif_ost_idx(seq);
3100 if (!fid_seq_in_fldb(seq)) {
3101 fld_range_set_mdt(range);
3103 /* FIXME: If ss is NULL, it suppose not get lsr_index
3105 range->lsr_index = ss->ss_node_id;
3109 LASSERT(ss != NULL);
3110 fld_range_set_any(range);
3111 /* OSD will only do local fld lookup */
3112 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3115 static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
3116 struct lu_attr *attr,
3117 struct dt_allocation_hint *hint,
3118 struct dt_object_format *dof,
3119 struct thandle *handle)
3121 struct osd_thandle *oh;
3125 LASSERT(handle != NULL);
3127 oh = container_of0(handle, struct osd_thandle, ot_super);
3128 LASSERT(oh->ot_handle == NULL);
3130 /* EA object consumes more credits than regular object: osd_mk_index
3131 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3132 * leaf_node, could involves the block, block bitmap, groups, GDT
3133 * change for each block, so add 4 * 2 credits in that case. */
3134 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3135 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3136 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3137 /* Reuse idle OI block may cause additional one OI block
3139 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3140 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3145 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid,
3146 attr->la_projid, 1, oh, osd_dt_obj(dt),
3147 NULL, OSD_QID_INODE);
3151 /* will help to find FID->ino mapping at dt_insert() */
3152 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3159 * Called to destroy on-disk representation of the object
3161 * Concurrency: must be locked
3163 static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
3166 struct osd_object *obj = osd_dt_obj(dt);
3167 struct inode *inode = obj->oo_inode;
3168 struct osd_thandle *oh;
3175 oh = container_of0(th, struct osd_thandle, ot_super);
3176 LASSERT(oh->ot_handle == NULL);
3178 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3179 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3180 /* Recycle idle OI leaf may cause additional three OI blocks
3182 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3183 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3184 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3185 /* one less inode */
3186 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3187 i_projid_read(inode), -1, oh, obj, NULL,
3191 /* data to be truncated */
3192 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3193 i_projid_read(inode), 0, oh, obj, NULL,
3198 /* will help to find FID->ino when this object is being
3199 * added to PENDING/ */
3200 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3205 static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
3208 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3209 struct osd_object *obj = osd_dt_obj(dt);
3210 struct inode *inode = obj->oo_inode;
3211 struct osd_device *osd = osd_obj2dev(obj);
3212 struct osd_thandle *oh;
3216 oh = container_of0(th, struct osd_thandle, ot_super);
3217 LASSERT(oh->ot_handle);
3219 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3221 if (unlikely(fid_is_acct(fid)))
3224 if (S_ISDIR(inode->i_mode)) {
3225 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
3226 inode->i_nlink == 2);
3227 /* it will check/delete the inode from remote parent,
3228 * how to optimize it? unlink performance impaction XXX */
3229 result = osd_delete_from_remote_parent(env, osd, obj, oh);
3231 CERROR("%s: delete inode "DFID": rc = %d\n",
3232 osd_name(osd), PFID(fid), result);
3234 spin_lock(&obj->oo_guard);
3236 spin_unlock(&obj->oo_guard);
3237 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3240 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3242 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3244 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3245 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3246 oh->ot_handle, OI_CHECK_FLD);
3248 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3249 /* XXX: add to ext3 orphan list */
3250 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3252 /* not needed in the cache anymore */
3253 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3254 obj->oo_destroyed = 1;
3260 * Put the fid into lustre_mdt_attrs, and then place the structure
3261 * inode's ea. This fid should not be altered during the life time
3264 * \retval +ve, on success
3265 * \retval -ve, on error
3267 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3269 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3270 const struct lu_fid *fid, __u32 compat, __u32 incompat)
3272 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3273 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3277 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3280 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3283 lustre_loa_init(loa, fid, compat, incompat);
3284 lustre_loa_swab(loa, false);
3286 /* For the OST device with 256 bytes inode size by default,
3287 * the PFID EA will be stored together with LMA EA to avoid
3288 * performance trouble. Otherwise the PFID EA can be stored
3289 * independently. LU-8998 */
3290 if ((compat & LMAC_FID_ON_OST) &&
3291 LDISKFS_INODE_SIZE(inode->i_sb) <= 256)
3292 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3293 sizeof(*loa), XATTR_CREATE);
3295 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3296 sizeof(*lma), XATTR_CREATE);
3297 /* LMA may already exist, but we need to check that all the
3298 * desired compat/incompat flags have been added. */
3299 if (unlikely(rc == -EEXIST)) {
3300 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3301 XATTR_NAME_LMA, (void *)loa, sizeof(*loa));
3305 if (rc < sizeof(*lma))
3308 lustre_loa_swab(loa, true);
3309 if (lu_fid_eq(fid, &lma->lma_self_fid) &&
3310 ((compat == 0 && incompat == 0) ||
3311 (!(~lma->lma_compat & compat) &&
3312 !(~lma->lma_incompat & incompat))))
3315 lma->lma_self_fid = *fid;
3316 lma->lma_compat |= compat;
3317 lma->lma_incompat |= incompat;
3318 if (rc == sizeof(*lma)) {
3319 lustre_lma_swab(lma);
3320 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3321 sizeof(*lma), XATTR_REPLACE);
3323 lustre_loa_swab(loa, false);
3324 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3325 sizeof(*loa), XATTR_REPLACE);
3333 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3334 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3335 * To have compatilibility with 1.8 ldiskfs driver we need to have
3336 * magic number at start of fid data.
3337 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3340 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3341 const struct lu_fid *fid)
3343 if (!fid_is_namespace_visible(fid) ||
3344 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3345 param->edp_magic = 0;
3349 param->edp_magic = LDISKFS_LUFID_MAGIC;
3350 param->edp_len = sizeof(struct lu_fid) + 1;
3351 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3355 * Try to read the fid from inode ea into dt_rec.
3357 * \param fid object fid.
3359 * \retval 0 on success
3361 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3362 __u32 ino, struct lu_fid *fid,
3363 struct osd_inode_id *id)
3365 struct osd_thread_info *info = osd_oti_get(env);
3366 struct inode *inode;
3369 osd_id_gen(id, ino, OSD_OII_NOGEN);
3370 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3372 RETURN(PTR_ERR(inode));
3378 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3380 struct inode *parent_dir,
3381 const struct lu_fid *dot_fid,
3382 const struct lu_fid *dot_dot_fid,
3383 struct osd_thandle *oth)
3385 struct ldiskfs_dentry_param *dot_ldp;
3386 struct ldiskfs_dentry_param *dot_dot_ldp;
3387 __u32 saved_nlink = dir->i_nlink;
3390 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3391 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3393 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3394 dot_ldp->edp_magic = 0;
3396 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3397 dir, dot_ldp, dot_dot_ldp);
3398 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3399 * the subseqent ref_add() will increase the dir->i_nlink
3400 * as 3. That is incorrect for new created directory.
3402 * It looks like hack, because we want to make the OSD API
3403 * to be order-independent for new created directory object
3404 * between dt_insert(..) and ref_add() operations.
3406 * Here, we only restore the in-RAM dir-inode's nlink attr,
3407 * becuase if the nlink attr is not 2, then there will be
3408 * ref_add() called following the dt_insert(..), such call
3409 * will make both the in-RAM and on-disk dir-inode's nlink
3410 * attr to be set as 2. LU-7447 */
3411 set_nlink(dir, saved_nlink);
3416 * Create an local agent inode for remote entry
3418 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3419 struct osd_device *osd,
3420 struct osd_object *pobj,
3421 const struct lu_fid *fid,
3425 struct osd_thread_info *info = osd_oti_get(env);
3426 struct inode *local;
3427 struct osd_thandle *oh;
3432 oh = container_of(th, struct osd_thandle, ot_super);
3433 LASSERT(oh->ot_handle->h_transaction != NULL);
3435 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
3436 if (IS_ERR(local)) {
3437 CERROR("%s: create local error %d\n", osd_name(osd),
3438 (int)PTR_ERR(local));
3442 /* restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
3443 * correct gid on remote file, not agent here */
3444 local->i_gid = current_fsgid();
3445 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3446 unlock_new_inode(local);
3448 /* Agent inode should not have project ID */
3449 #ifdef HAVE_PROJECT_QUOTA
3450 if (LDISKFS_I(pobj->oo_inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
3451 i_projid_read(pobj->oo_inode) != 0) {
3452 rc = ldiskfs_transfer_project(local, 0);
3454 CERROR("%s: quota transfer failed: rc = %d. Is project "
3455 "quota enforcement enabled on the ldiskfs "
3456 "filesystem?\n", local->i_sb->s_id, rc);
3457 RETURN(ERR_PTR(rc));
3461 /* Set special LMA flag for local agent inode */
3462 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3464 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3465 osd_name(osd), PFID(fid), rc);
3466 RETURN(ERR_PTR(rc));
3472 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3473 lu_object_fid(&pobj->oo_dt.do_lu),
3476 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3477 osd_name(osd), PFID(fid), rc);
3478 RETURN(ERR_PTR(rc));
3485 * when direntry is deleted, we have to take care of possible agent inode
3486 * referenced by that. unfortunately we can't do this at that point:
3487 * iget() within a running transaction leads to deadlock and we better do
3488 * not call that every delete declaration to save performance. so we put
3489 * a potention agent inode on a list and process that once the transaction
3490 * is over. Notice it's not any worse in terms of real orphans as regular
3491 * object destroy doesn't put inodes on the on-disk orphan list. this should
3492 * be addressed separately
3494 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3495 struct osd_thandle *oh,
3498 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3499 struct osd_obj_orphan *oor;
3506 oor->oor_env = (struct lu_env *)env;
3507 spin_lock(&osd->od_osfs_lock);
3508 list_add(&oor->oor_list, &osd->od_orphan_list);
3509 spin_unlock(&osd->od_osfs_lock);
3511 oh->ot_remove_agents = 1;
3517 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3518 struct osd_device *osd)
3520 struct osd_thread_info *info = osd_oti_get(env);
3521 struct osd_obj_orphan *oor, *tmp;
3522 struct osd_inode_id id;
3523 struct list_head list;
3524 struct inode *inode;
3529 INIT_LIST_HEAD(&list);
3531 spin_lock(&osd->od_osfs_lock);
3532 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3533 if (oor->oor_env == env) {
3534 list_del(&oor->oor_list);
3535 list_add(&oor->oor_list, &list);
3538 spin_unlock(&osd->od_osfs_lock);
3540 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3544 list_del(&oor->oor_list);
3547 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3548 inode = osd_iget_fid(info, osd, &id, &fid);
3552 if (!osd_remote_fid(env, osd, &fid)) {
3557 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3559 mark_inode_dirty(inode);
3560 ldiskfs_journal_stop(jh);
3568 * OSD layer object create function for OST objects (b=11826).
3570 * The FID is inserted into inode xattr here.
3572 * \retval 0, on success
3573 * \retval -ve, on error
3575 static int osd_create(const struct lu_env *env, struct dt_object *dt,
3576 struct lu_attr *attr, struct dt_allocation_hint *hint,
3577 struct dt_object_format *dof, struct thandle *th)
3579 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3580 struct osd_object *obj = osd_dt_obj(dt);
3581 struct osd_thread_info *info = osd_oti_get(env);
3582 int result, on_ost = 0;
3586 if (dt_object_exists(dt))
3589 LINVRNT(osd_invariant(obj));
3590 LASSERT(!dt_object_remote(dt));
3591 LASSERT(osd_is_write_locked(env, obj));
3592 LASSERT(th != NULL);
3594 if (unlikely(fid_is_acct(fid)))
3595 /* Quota files can't be created from the kernel any more,
3596 * 'tune2fs -O quota' will take care of creating them */
3599 result = __osd_create(info, obj, attr, hint, dof, th);
3601 if (fid_is_idif(fid) &&
3602 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3603 struct lu_fid *tfid = &info->oti_fid;
3604 struct ost_id *oi = &info->oti_ostid;
3606 fid_to_ostid(fid, oi);
3607 ostid_to_fid(tfid, oi, 0);
3609 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3610 LMAC_FID_ON_OST, 0);
3612 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3614 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3615 on_ost ? LMAC_FID_ON_OST : 0,
3618 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3619 obj->oo_dt.do_body_ops = &osd_body_ops;
3623 result = __osd_oi_insert(env, obj, fid, th);
3625 /* a small optimization - dt_insert() isn't usually applied
3626 * to OST objects, so we don't need to cache OI mapping for
3628 if (result == 0 && on_ost == 0) {
3629 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3630 result = osd_idc_find_and_init(env, osd, obj);
3631 LASSERT(result == 0);
3634 LASSERT(ergo(result == 0,
3635 dt_object_exists(dt) && !dt_object_remote(dt)));
3636 LINVRNT(osd_invariant(obj));
3640 static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
3641 struct thandle *handle)
3643 struct osd_thandle *oh;
3645 /* it's possible that object doesn't exist yet */
3646 LASSERT(handle != NULL);
3648 oh = container_of0(handle, struct osd_thandle, ot_super);
3649 LASSERT(oh->ot_handle == NULL);
3651 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3652 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3658 * Concurrency: @dt is write locked.
3660 static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
3663 struct osd_object *obj = osd_dt_obj(dt);
3664 struct inode *inode = obj->oo_inode;
3665 struct osd_thandle *oh;
3668 if (!dt_object_exists(dt) || obj->oo_destroyed)
3671 LINVRNT(osd_invariant(obj));
3672 LASSERT(!dt_object_remote(dt));
3673 LASSERT(osd_is_write_locked(env, obj));
3674 LASSERT(th != NULL);
3676 oh = container_of0(th, struct osd_thandle, ot_super);
3677 LASSERT(oh->ot_handle != NULL);
3679 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3681 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3682 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3684 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3685 * (65000) subdirectories by storing "1" in i_nlink if the link count
3686 * would otherwise overflow. Directory tranversal tools understand
3687 * that (st_nlink == 1) indicates that the filesystem dose not track
3688 * hard links count on the directory, and will not abort subdirectory
3689 * scanning early once (st_nlink - 2) subdirs have been found.
3691 * This also has to properly handle the case of inodes with nlink == 0
3692 * in case they are being linked into the PENDING directory
3694 spin_lock(&obj->oo_guard);
3695 if (unlikely(inode->i_nlink == 0))
3696 /* inc_nlink from 0 may cause WARN_ON */
3697 set_nlink(inode, 1);
3699 ldiskfs_inc_count(oh->ot_handle, inode);
3700 if (!S_ISDIR(inode->i_mode))
3701 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3703 spin_unlock(&obj->oo_guard);
3705 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3706 LINVRNT(osd_invariant(obj));
3708 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3713 static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
3714 struct thandle *handle)
3716 struct osd_thandle *oh;
3718 if (!dt_object_exists(dt))
3721 LASSERT(!dt_object_remote(dt));
3722 LASSERT(handle != NULL);
3724 oh = container_of0(handle, struct osd_thandle, ot_super);
3725 LASSERT(oh->ot_handle == NULL);
3727 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3728 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3734 * Concurrency: @dt is write locked.
3736 static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
3739 struct osd_object *obj = osd_dt_obj(dt);
3740 struct inode *inode = obj->oo_inode;
3741 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3742 struct osd_thandle *oh;
3744 if (!dt_object_exists(dt))
3747 LINVRNT(osd_invariant(obj));
3748 LASSERT(!dt_object_remote(dt));
3749 LASSERT(osd_is_write_locked(env, obj));
3750 LASSERT(th != NULL);
3752 oh = container_of0(th, struct osd_thandle, ot_super);
3753 LASSERT(oh->ot_handle != NULL);
3755 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3757 spin_lock(&obj->oo_guard);
3758 /* That can be result of upgrade from old Lustre version and
3759 * applied only to local files. Just skip this ref_del call.
3760 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3761 if (inode->i_nlink == 0) {
3762 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3763 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3764 ", maybe an upgraded file? (LU-3915)\n",
3765 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3766 spin_unlock(&obj->oo_guard);
3770 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3771 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3773 ldiskfs_dec_count(oh->ot_handle, inode);
3774 spin_unlock(&obj->oo_guard);
3776 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3777 LINVRNT(osd_invariant(obj));
3779 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3785 * Concurrency: @dt is read locked.
3787 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3788 struct lu_buf *buf, const char *name)
3790 struct osd_object *obj = osd_dt_obj(dt);
3791 struct inode *inode = obj->oo_inode;
3792 struct osd_thread_info *info = osd_oti_get(env);
3793 struct dentry *dentry = &info->oti_obj_dentry;
3794 bool cache_xattr = false;
3799 /* version get is not real XATTR but uses xattr API */
3800 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3801 dt_obj_version_t *ver = buf->lb_buf;
3803 /* for version we are just using xattr API but change inode
3805 if (buf->lb_len == 0)
3806 return sizeof(dt_obj_version_t);
3808 if (buf->lb_len < sizeof(dt_obj_version_t))
3811 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
3812 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3814 *ver = LDISKFS_I(inode)->i_fs_version;
3816 return sizeof(dt_obj_version_t);
3819 if (!dt_object_exists(dt))
3822 LASSERT(!dt_object_remote(dt));
3823 LASSERT(inode->i_op != NULL);
3824 LASSERT(inode->i_op->getxattr != NULL);
3826 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
3827 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
3831 rc = osd_oxc_get(obj, name, buf);
3836 rc = __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3837 if (rc == -ENODATA && strcmp(name, XATTR_NAME_FID) == 0) {
3838 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3839 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3840 struct filter_fid *ff;
3841 struct ost_layout *ol;
3843 if (!osd_dev(dt->do_lu.lo_dev)->od_is_ost)
3846 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
3850 if (!(lma->lma_compat & LMAC_STRIPE_INFO)) {
3856 if (buf->lb_len == 0 || !buf->lb_buf)
3859 if (buf->lb_len < rc)
3863 ol = &ff->ff_layout;
3864 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
3865 PFID_STRIPE_IDX_BITS);
3866 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
3867 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
3868 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
3869 if (lma->lma_compat & LMAC_COMP_INFO) {
3870 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
3871 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
3872 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
3874 ol->ol_comp_start = 0;
3875 ol->ol_comp_end = 0;
3882 if (rc == -ENOENT || rc == -ENODATA)
3883 osd_oxc_add(obj, name, NULL, 0);
3884 else if (rc > 0 && buf->lb_buf != NULL)
3885 osd_oxc_add(obj, name, buf->lb_buf, rc);
3891 static int osd_declare_xattr_set(const struct lu_env *env,
3892 struct dt_object *dt,
3893 const struct lu_buf *buf, const char *name,
3894 int fl, struct thandle *handle)
3896 struct osd_thandle *oh;
3898 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3900 LASSERT(handle != NULL);
3902 oh = container_of0(handle, struct osd_thandle, ot_super);
3903 LASSERT(oh->ot_handle == NULL);
3905 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3906 /* For non-upgrading case, the LMA is set first and
3907 * usually fit inode. But for upgrade case, the LMA
3908 * may be in another separated EA block. */
3909 if (dt_object_exists(dt)) {
3910 if (fl == LU_XATTR_REPLACE)
3915 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3917 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3918 /* We may need to delete the old PFID EA. */
3919 credits = LDISKFS_MAXQUOTAS_DEL_BLOCKS(sb);
3920 if (fl == LU_XATTR_REPLACE)
3927 credits += osd_dto_credits_noquota[DTO_XATTR_SET];
3932 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3933 /* learn xattr size from osd_xattr_get if
3934 attribute has not been read yet */
3935 buflen = __osd_xattr_get(
3936 osd_dt_obj(dt)->oo_inode,
3937 &osd_oti_get(env)->oti_obj_dentry,
3942 buflen = buf->lb_len;
3945 if (buflen > sb->s_blocksize) {
3946 credits += osd_calc_bkmap_credits(
3948 (buflen + sb->s_blocksize - 1) >>
3949 sb->s_blocksize_bits);
3953 * xattr set may involve inode quota change, reserve credits for
3954 * dquot_initialize()
3956 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3959 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3965 * Concurrency: @dt is write locked.
3967 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3968 const struct lu_buf *buf, const char *name, int fl,
3969 struct thandle *handle)
3971 struct osd_object *obj = osd_dt_obj(dt);
3972 struct inode *inode = obj->oo_inode;
3973 struct osd_thread_info *info = osd_oti_get(env);
3974 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3975 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3984 /* version set is not real XATTR */
3985 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3986 dt_obj_version_t *version = buf->lb_buf;
3988 /* for version we are just using xattr API but change inode
3990 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3992 CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
3993 *version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3995 LDISKFS_I(inode)->i_fs_version = *version;
3996 /* Version is set after all inode operations are finished,
3997 * so we should mark it dirty here */
3998 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
4003 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
4004 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
4007 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4008 if (fl & LU_XATTR_REPLACE)
4009 fs_flags |= XATTR_REPLACE;
4011 if (fl & LU_XATTR_CREATE)
4012 fs_flags |= XATTR_CREATE;
4014 /* For the OST device with 256 bytes inode size by default,
4015 * the PFID EA will be stored together with LMA EA to avoid
4016 * performance trouble. Otherwise the PFID EA can be stored
4017 * independently. LU-8998 */
4018 if (strcmp(name, XATTR_NAME_FID) == 0 &&
4019 LDISKFS_INODE_SIZE(inode->i_sb) <= 256) {
4020 struct dentry *dentry = &info->oti_obj_dentry;
4021 struct filter_fid *ff;
4022 struct ost_layout *ol;
4025 LASSERT(osd_dev(dt->do_lu.lo_dev)->od_is_ost);
4028 ol = &ff->ff_layout;
4029 /* Old client does not send stripe information, store
4030 * the PFID EA on disk directly. */
4031 if (buf->lb_len == sizeof(struct lu_fid) ||
4032 ol->ol_stripe_size == 0) {
4033 len = sizeof(struct lu_fid);
4037 if (buf->lb_len != sizeof(*ff))
4040 rc = osd_get_lma(info, inode, dentry, loa);
4041 if (unlikely(rc == -ENODATA)) {
4042 /* Usually for upgarding from old device */
4043 lustre_loa_init(loa, lu_object_fid(&dt->do_lu),
4044 LMAC_FID_ON_OST, 0);
4052 fid_le_to_cpu(&loa->loa_parent_fid, &ff->ff_parent);
4053 loa->loa_parent_fid.f_ver |= le32_to_cpu(ol->ol_stripe_count) <<
4054 PFID_STRIPE_IDX_BITS;
4055 loa->loa_stripe_size = le32_to_cpu(ol->ol_stripe_size);
4056 lma->lma_compat |= LMAC_STRIPE_INFO;
4057 if (ol->ol_comp_id != 0) {
4058 loa->loa_comp_id = le32_to_cpu(ol->ol_comp_id);
4059 loa->loa_comp_start = le64_to_cpu(ol->ol_comp_start);
4060 loa->loa_comp_end = le64_to_cpu(ol->ol_comp_end);
4061 lma->lma_compat |= LMAC_COMP_INFO;
4064 lustre_loa_swab(loa, false);
4066 /* Remove old PFID EA entry firstly. */
4067 ll_vfs_dq_init(inode);
4068 rc = inode->i_op->removexattr(dentry, name);
4069 if (rc && rc != -ENODATA)
4072 /* Store the PFID EA inside the LMA EA. */
4073 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
4077 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
4078 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4082 lma->lma_incompat |= LMAI_STRIPED;
4083 lustre_lma_swab(lma);
4084 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4085 sizeof(*lma), XATTR_REPLACE);
4091 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, len, fs_flags);
4092 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4095 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4096 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4097 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
4103 * Concurrency: @dt is read locked.
4105 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
4106 const struct lu_buf *buf)
4108 struct osd_object *obj = osd_dt_obj(dt);
4109 struct inode *inode = obj->oo_inode;
4110 struct osd_thread_info *info = osd_oti_get(env);
4111 struct dentry *dentry = &info->oti_obj_dentry;
4113 if (!dt_object_exists(dt))
4116 LASSERT(!dt_object_remote(dt));
4117 LASSERT(inode->i_op != NULL);
4118 LASSERT(inode->i_op->listxattr != NULL);
4120 dentry->d_inode = inode;
4121 dentry->d_sb = inode->i_sb;
4122 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
4125 static int osd_declare_xattr_del(const struct lu_env *env,
4126 struct dt_object *dt, const char *name,
4127 struct thandle *handle)
4129 struct osd_thandle *oh;
4130 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4132 LASSERT(!dt_object_remote(dt));
4133 LASSERT(handle != NULL);
4135 oh = container_of0(handle, struct osd_thandle, ot_super);
4136 LASSERT(oh->ot_handle == NULL);
4138 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
4139 osd_dto_credits_noquota[DTO_XATTR_SET]);
4141 * xattr del may involve inode quota change, reserve credits for
4142 * dquot_initialize()
4144 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4150 * Concurrency: @dt is write locked.
4152 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
4153 const char *name, struct thandle *handle)
4155 struct osd_object *obj = osd_dt_obj(dt);
4156 struct inode *inode = obj->oo_inode;
4157 struct osd_thread_info *info = osd_oti_get(env);
4158 struct dentry *dentry = &info->oti_obj_dentry;
4161 if (!dt_object_exists(dt))
4164 LASSERT(!dt_object_remote(dt));
4165 LASSERT(inode->i_op != NULL);
4166 LASSERT(inode->i_op->removexattr != NULL);
4167 LASSERT(handle != NULL);
4169 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4171 ll_vfs_dq_init(inode);
4172 dentry->d_inode = inode;
4173 dentry->d_sb = inode->i_sb;
4174 rc = inode->i_op->removexattr(dentry, name);
4175 if (rc == -ENODATA && strcmp(name, XATTR_NAME_FID) == 0) {
4176 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
4178 LASSERT(osd_dev(dt->do_lu.lo_dev)->od_is_ost);
4180 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
4181 &info->oti_ost_attrs);
4183 if (!(lma->lma_compat & LMAC_STRIPE_INFO)) {
4188 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4189 lustre_lma_swab(lma);
4190 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4191 sizeof(*lma), XATTR_REPLACE);
4196 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4199 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4200 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4201 osd_oxc_del(obj, name);
4206 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
4207 __u64 start, __u64 end)
4209 struct osd_object *obj = osd_dt_obj(dt);
4210 struct inode *inode = obj->oo_inode;
4211 struct osd_thread_info *info = osd_oti_get(env);
4212 struct dentry *dentry = &info->oti_obj_dentry;
4213 struct file *file = &info->oti_file;
4218 dentry->d_inode = inode;
4219 dentry->d_sb = inode->i_sb;
4220 file->f_path.dentry = dentry;
4221 file->f_mapping = inode->i_mapping;
4222 file->f_op = inode->i_fop;
4223 set_file_inode(file, inode);
4225 rc = ll_vfs_fsync_range(file, start, end, 0);
4230 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4239 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4240 const struct dt_index_features *feat)
4242 struct iam_descr *descr;
4244 if (osd_object_is_root(o))
4245 return feat == &dt_directory_features;
4247 LASSERT(o->oo_dir != NULL);
4249 descr = o->oo_dir->od_container.ic_descr;
4250 if (feat == &dt_directory_features) {
4251 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4257 feat->dif_keysize_min <= descr->id_key_size &&
4258 descr->id_key_size <= feat->dif_keysize_max &&
4259 feat->dif_recsize_min <= descr->id_rec_size &&
4260 descr->id_rec_size <= feat->dif_recsize_max &&
4261 !(feat->dif_flags & (DT_IND_VARKEY |
4262 DT_IND_VARREC | DT_IND_NONUNQ)) &&
4263 ergo(feat->dif_flags & DT_IND_UPDATE,
4264 1 /* XXX check that object (and file system) is
4269 static int osd_iam_container_init(const struct lu_env *env,
4270 struct osd_object *obj,
4271 struct osd_directory *dir)
4273 struct iam_container *bag = &dir->od_container;
4276 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
4280 result = iam_container_setup(bag);
4282 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
4284 iam_container_fini(bag);
4291 * Concurrency: no external locking is necessary.
4293 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
4294 const struct dt_index_features *feat)
4298 struct osd_object *obj = osd_dt_obj(dt);
4300 LINVRNT(osd_invariant(obj));
4302 if (osd_object_is_root(obj)) {
4303 dt->do_index_ops = &osd_index_ea_ops;
4305 } else if (feat == &dt_directory_features) {
4306 dt->do_index_ops = &osd_index_ea_ops;
4307 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
4312 } else if (unlikely(feat == &dt_otable_features)) {
4313 dt->do_index_ops = &osd_otable_ops;
4315 } else if (unlikely(feat == &dt_acct_features)) {
4316 dt->do_index_ops = &osd_acct_index_ops;
4319 } else if (!osd_has_index(obj)) {
4320 struct osd_directory *dir;
4325 spin_lock(&obj->oo_guard);
4326 if (obj->oo_dir == NULL)
4330 * Concurrent thread allocated container data.
4333 spin_unlock(&obj->oo_guard);
4335 * Now, that we have container data, serialize its
4338 down_write(&obj->oo_ext_idx_sem);
4340 * recheck under lock.
4342 if (!osd_has_index(obj))
4343 result = osd_iam_container_init(env, obj,
4347 up_write(&obj->oo_ext_idx_sem);
4355 if (result == 0 && skip_iam == 0) {
4356 if (!osd_iam_index_probe(env, obj, feat))
4359 LINVRNT(osd_invariant(obj));
4364 static int osd_otable_it_attr_get(const struct lu_env *env,
4365 struct dt_object *dt,
4366 struct lu_attr *attr)
4372 static const struct dt_object_operations osd_obj_ops = {
4373 .do_read_lock = osd_read_lock,
4374 .do_write_lock = osd_write_lock,
4375 .do_read_unlock = osd_read_unlock,
4376 .do_write_unlock = osd_write_unlock,
4377 .do_write_locked = osd_write_locked,
4378 .do_attr_get = osd_attr_get,
4379 .do_declare_attr_set = osd_declare_attr_set,
4380 .do_attr_set = osd_attr_set,
4381 .do_ah_init = osd_ah_init,
4382 .do_declare_create = osd_declare_create,
4383 .do_create = osd_create,
4384 .do_declare_destroy = osd_declare_destroy,
4385 .do_destroy = osd_destroy,
4386 .do_index_try = osd_index_try,
4387 .do_declare_ref_add = osd_declare_ref_add,
4388 .do_ref_add = osd_ref_add,
4389 .do_declare_ref_del = osd_declare_ref_del,
4390 .do_ref_del = osd_ref_del,
4391 .do_xattr_get = osd_xattr_get,
4392 .do_declare_xattr_set = osd_declare_xattr_set,
4393 .do_xattr_set = osd_xattr_set,
4394 .do_declare_xattr_del = osd_declare_xattr_del,
4395 .do_xattr_del = osd_xattr_del,
4396 .do_xattr_list = osd_xattr_list,
4397 .do_object_sync = osd_object_sync,
4398 .do_invalidate = osd_invalidate,
4401 static const struct dt_object_operations osd_obj_otable_it_ops = {
4402 .do_attr_get = osd_otable_it_attr_get,
4403 .do_index_try = osd_index_try,
4406 static int osd_index_declare_iam_delete(const struct lu_env *env,
4407 struct dt_object *dt,
4408 const struct dt_key *key,
4409 struct thandle *handle)
4411 struct osd_thandle *oh;
4413 oh = container_of0(handle, struct osd_thandle, ot_super);
4414 LASSERT(oh->ot_handle == NULL);
4416 /* Recycle may cause additional three blocks to be changed. */
4417 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
4418 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
4424 * delete a (key, value) pair from index \a dt specified by \a key
4426 * \param dt osd index object
4427 * \param key key for index
4428 * \param rec record reference
4429 * \param handle transaction handler
4432 * \retval -ve failure
4434 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
4435 const struct dt_key *key,
4436 struct thandle *handle)
4438 struct osd_thread_info *oti = osd_oti_get(env);
4439 struct osd_object *obj = osd_dt_obj(dt);
4440 struct osd_thandle *oh;
4441 struct iam_path_descr *ipd;
4442 struct iam_container *bag = &obj->oo_dir->od_container;
4446 if (!dt_object_exists(dt))
4449 LINVRNT(osd_invariant(obj));
4450 LASSERT(!dt_object_remote(dt));
4451 LASSERT(bag->ic_object == obj->oo_inode);
4452 LASSERT(handle != NULL);
4454 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4456 ipd = osd_idx_ipd_get(env, bag);
4457 if (unlikely(ipd == NULL))
4460 oh = container_of0(handle, struct osd_thandle, ot_super);
4461 LASSERT(oh->ot_handle != NULL);
4462 LASSERT(oh->ot_handle->h_transaction != NULL);
4464 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4465 /* swab quota uid/gid provided by caller */
4466 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4467 key = (const struct dt_key *)&oti->oti_quota_id;
4470 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
4471 osd_ipd_put(env, bag, ipd);
4472 LINVRNT(osd_invariant(obj));
4473 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4477 static int osd_index_declare_ea_delete(const struct lu_env *env,
4478 struct dt_object *dt,
4479 const struct dt_key *key,
4480 struct thandle *handle)
4482 struct osd_thandle *oh;
4483 struct inode *inode;
4487 LASSERT(!dt_object_remote(dt));
4488 LASSERT(handle != NULL);
4490 oh = container_of0(handle, struct osd_thandle, ot_super);
4491 LASSERT(oh->ot_handle == NULL);
4493 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
4494 if (key != NULL && unlikely(strcmp((char *)key, dotdot) == 0)) {
4495 /* '..' to a remote object has a local representative */
4496 credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
4497 /* to reset LMAI_REMOTE_PARENT */
4500 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
4502 inode = osd_dt_obj(dt)->oo_inode;
4506 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
4507 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
4512 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
4515 struct osd_fid_pack *rec;
4518 if (de->file_type & LDISKFS_DIRENT_LUFID) {
4519 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
4520 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
4521 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
4527 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
4528 const struct lu_fid *fid)
4530 struct seq_server_site *ss = osd_seq_site(osd);
4533 /* FID seqs not in FLDB, must be local seq */
4534 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
4537 /* If FLD is not being initialized yet, it only happens during the
4538 * initialization, likely during mgs initialization, and we assume
4539 * this is local FID. */
4540 if (ss == NULL || ss->ss_server_fld == NULL)
4543 /* Only check the local FLDB here */
4544 if (osd_seq_exists(env, osd, fid_seq(fid)))
4551 * Index delete function for interoperability mode (b11826).
4552 * It will remove the directory entry added by osd_index_ea_insert().
4553 * This entry is needed to maintain name->fid mapping.
4555 * \param key, key i.e. file entry to be deleted
4557 * \retval 0, on success
4558 * \retval -ve, on error
4560 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
4561 const struct dt_key *key, struct thandle *handle)
4563 struct osd_object *obj = osd_dt_obj(dt);
4564 struct inode *dir = obj->oo_inode;
4565 struct dentry *dentry;
4566 struct osd_thandle *oh;
4567 struct ldiskfs_dir_entry_2 *de = NULL;
4568 struct buffer_head *bh;
4569 struct htree_lock *hlock = NULL;
4570 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
4571 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4575 if (!dt_object_exists(dt))
4578 LINVRNT(osd_invariant(obj));
4579 LASSERT(!dt_object_remote(dt));
4580 LASSERT(handle != NULL);
4582 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4584 oh = container_of(handle, struct osd_thandle, ot_super);
4585 LASSERT(oh->ot_handle != NULL);
4586 LASSERT(oh->ot_handle->h_transaction != NULL);
4588 ll_vfs_dq_init(dir);
4589 dentry = osd_child_dentry_get(env, obj,
4590 (char *)key, strlen((char *)key));
4592 if (obj->oo_hl_head != NULL) {
4593 hlock = osd_oti_get(env)->oti_hlock;
4594 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4595 dir, LDISKFS_HLOCK_DEL);
4597 down_write(&obj->oo_ext_idx_sem);
4600 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4602 /* If this is not the ".." entry, it might be a remote DNE
4603 * entry and we need to check if the FID is for a remote
4604 * MDT. If the FID is not in the directory entry (e.g.
4605 * upgraded 1.8 filesystem without dirdata enabled) then
4606 * we need to get the FID from the LMA. For a remote directory
4607 * there HAS to be an LMA, it cannot be an IGIF inode in this
4610 * Delete the entry before the agent inode in order to
4611 * simplify error handling. At worst an error after deleting
4612 * the entry first might leak the agent inode afterward. The
4613 * reverse would need filesystem abort in case of error deleting
4614 * the entry after the agent had been removed, or leave a
4615 * dangling entry pointing at a random inode. */
4616 if (strcmp((char *)key, dotdot) != 0) {
4617 LASSERT(de != NULL);
4618 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4619 if (rc == -ENODATA) {
4620 /* can't get FID, postpone to the end of the
4621 * transaction when iget() is safe */
4622 osd_schedule_agent_inode_removal(env, oh,
4623 le32_to_cpu(de->inode));
4624 } else if (rc == 0 &&
4625 unlikely(osd_remote_fid(env, osd, fid))) {
4626 osd_schedule_agent_inode_removal(env, oh,
4627 le32_to_cpu(de->inode));
4630 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4636 ldiskfs_htree_unlock(hlock);
4638 up_write(&obj->oo_ext_idx_sem);
4643 /* For inode on the remote MDT, .. will point to
4644 * /Agent directory, Check whether it needs to delete
4645 * from agent directory */
4646 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4649 ret = osd_delete_from_remote_parent(env, osd_obj2dev(obj),
4652 /* Sigh, the entry has been deleted, and
4653 * it is not easy to revert it back, so
4654 * let's keep this error private, and let
4655 * LFSCK fix it. XXX */
4656 CERROR("%s: delete remote parent "DFID": rc = %d\n",
4657 osd_name(osd), PFID(fid), ret);
4660 LASSERT(osd_invariant(obj));
4661 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4666 * Lookup index for \a key and copy record to \a rec.
4668 * \param dt osd index object
4669 * \param key key for index
4670 * \param rec record reference
4672 * \retval +ve success : exact mach
4673 * \retval 0 return record with key not greater than \a key
4674 * \retval -ve failure
4676 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4677 struct dt_rec *rec, const struct dt_key *key)
4679 struct osd_object *obj = osd_dt_obj(dt);
4680 struct iam_path_descr *ipd;
4681 struct iam_container *bag = &obj->oo_dir->od_container;
4682 struct osd_thread_info *oti = osd_oti_get(env);
4683 struct iam_iterator *it = &oti->oti_idx_it;
4684 struct iam_rec *iam_rec;
4688 if (!dt_object_exists(dt))
4691 LASSERT(osd_invariant(obj));
4692 LASSERT(!dt_object_remote(dt));
4693 LASSERT(bag->ic_object == obj->oo_inode);
4695 ipd = osd_idx_ipd_get(env, bag);
4699 /* got ipd now we can start iterator. */
4700 iam_it_init(it, bag, 0, ipd);
4702 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4703 /* swab quota uid/gid provided by caller */
4704 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4705 key = (const struct dt_key *)&oti->oti_quota_id;
4708 rc = iam_it_get(it, (struct iam_key *)key);
4710 if (S_ISDIR(obj->oo_inode->i_mode))
4711 iam_rec = (struct iam_rec *)oti->oti_ldp;
4713 iam_rec = (struct iam_rec *) rec;
4715 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4717 if (S_ISDIR(obj->oo_inode->i_mode))
4718 osd_fid_unpack((struct lu_fid *) rec,
4719 (struct osd_fid_pack *)iam_rec);
4720 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4721 osd_quota_unpack(obj, rec);
4726 osd_ipd_put(env, bag, ipd);
4728 LINVRNT(osd_invariant(obj));
4733 static int osd_index_declare_iam_insert(const struct lu_env *env,
4734 struct dt_object *dt,
4735 const struct dt_rec *rec,
4736 const struct dt_key *key,
4737 struct thandle *handle)
4739 struct osd_thandle *oh;
4741 LASSERT(handle != NULL);
4743 oh = container_of0(handle, struct osd_thandle, ot_super);
4744 LASSERT(oh->ot_handle == NULL);
4746 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4747 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4753 * Inserts (key, value) pair in \a dt index object.
4755 * \param dt osd index object
4756 * \param key key for index
4757 * \param rec record reference
4758 * \param th transaction handler
4761 * \retval -ve failure
4763 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4764 const struct dt_rec *rec,
4765 const struct dt_key *key, struct thandle *th,
4768 struct osd_object *obj = osd_dt_obj(dt);
4769 struct iam_path_descr *ipd;
4770 struct osd_thandle *oh;
4771 struct iam_container *bag;
4772 struct osd_thread_info *oti = osd_oti_get(env);
4773 struct iam_rec *iam_rec;
4777 if (!dt_object_exists(dt))
4780 LINVRNT(osd_invariant(obj));
4781 LASSERT(!dt_object_remote(dt));
4783 bag = &obj->oo_dir->od_container;
4784 LASSERT(bag->ic_object == obj->oo_inode);
4785 LASSERT(th != NULL);
4787 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4789 ipd = osd_idx_ipd_get(env, bag);
4790 if (unlikely(ipd == NULL))
4793 oh = container_of0(th, struct osd_thandle, ot_super);
4794 LASSERT(oh->ot_handle != NULL);
4795 LASSERT(oh->ot_handle->h_transaction != NULL);
4796 if (S_ISDIR(obj->oo_inode->i_mode)) {
4797 iam_rec = (struct iam_rec *)oti->oti_ldp;
4798 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4799 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4800 /* pack quota uid/gid */
4801 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4802 key = (const struct dt_key *)&oti->oti_quota_id;
4803 /* pack quota record */
4804 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4805 iam_rec = (struct iam_rec *)rec;
4807 iam_rec = (struct iam_rec *)rec;
4810 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4812 osd_ipd_put(env, bag, ipd);
4813 LINVRNT(osd_invariant(obj));
4814 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4819 * Calls ldiskfs_add_entry() to add directory entry
4820 * into the directory. This is required for
4821 * interoperability mode (b11826)
4823 * \retval 0, on success
4824 * \retval -ve, on error
4826 static int __osd_ea_add_rec(struct osd_thread_info *info,
4827 struct osd_object *pobj, struct inode *cinode,
4828 const char *name, const struct lu_fid *fid,
4829 struct htree_lock *hlock, struct thandle *th)
4831 struct ldiskfs_dentry_param *ldp;
4832 struct dentry *child;
4833 struct osd_thandle *oth;
4836 oth = container_of(th, struct osd_thandle, ot_super);
4837 LASSERT(oth->ot_handle != NULL);
4838 LASSERT(oth->ot_handle->h_transaction != NULL);
4839 LASSERT(pobj->oo_inode);
4841 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4842 if (unlikely(pobj->oo_inode ==
4843 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4846 osd_get_ldiskfs_dirent_param(ldp, fid);
4847 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4848 child->d_fsdata = (void *)ldp;
4849 ll_vfs_dq_init(pobj->oo_inode);
4850 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
4851 child, cinode, hlock);
4852 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4853 struct ldiskfs_dir_entry_2 *de;
4854 struct buffer_head *bh;
4857 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4860 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4863 if (S_ISDIR(cinode->i_mode))
4864 de->file_type = LDISKFS_DIRENT_LUFID |
4865 LDISKFS_FT_REG_FILE;
4867 de->file_type = LDISKFS_DIRENT_LUFID |
4869 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4880 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4881 * into the directory.Also sets flags into osd object to
4882 * indicate dot and dotdot are created. This is required for
4883 * interoperability mode (b11826)
4885 * \param dir directory for dot and dotdot fixup.
4886 * \param obj child object for linking
4888 * \retval 0, on success
4889 * \retval -ve, on error
4891 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4892 struct osd_object *dir,
4893 struct inode *parent_dir, const char *name,
4894 const struct lu_fid *dot_fid,
4895 const struct lu_fid *dot_dot_fid,
4898 struct inode *inode = dir->oo_inode;
4899 struct osd_thandle *oth;
4902 oth = container_of(th, struct osd_thandle, ot_super);
4903 LASSERT(oth->ot_handle->h_transaction != NULL);
4904 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4906 if (strcmp(name, dot) == 0) {
4907 if (dir->oo_compat_dot_created) {
4910 LASSERT(inode->i_ino == parent_dir->i_ino);
4911 dir->oo_compat_dot_created = 1;
4914 } else if (strcmp(name, dotdot) == 0) {
4915 if (!dir->oo_compat_dot_created)
4917 /* in case of rename, dotdot is already created */
4918 if (dir->oo_compat_dotdot_created) {
4919 return __osd_ea_add_rec(info, dir, parent_dir, name,
4920 dot_dot_fid, NULL, th);
4923 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4924 struct lu_fid tfid = *dot_dot_fid;
4927 result = osd_add_dot_dotdot_internal(info,
4928 dir->oo_inode, parent_dir, dot_fid,
4931 result = osd_add_dot_dotdot_internal(info,
4932 dir->oo_inode, parent_dir, dot_fid,
4937 dir->oo_compat_dotdot_created = 1;
4945 * It will call the appropriate osd_add* function and return the
4946 * value, return by respective functions.
4948 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4949 struct inode *cinode, const char *name,
4950 const struct lu_fid *fid, struct thandle *th)
4952 struct osd_thread_info *info = osd_oti_get(env);
4953 struct htree_lock *hlock;
4956 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4958 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4960 if (hlock != NULL) {
4961 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4964 down_write(&pobj->oo_ext_idx_sem);
4967 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4968 lu_object_fid(&pobj->oo_dt.do_lu),
4971 if (hlock != NULL) {
4972 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4973 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4975 down_write(&pobj->oo_ext_idx_sem);
4978 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4979 struct lu_fid *tfid = &info->oti_fid;
4983 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4986 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4991 ldiskfs_htree_unlock(hlock);
4993 up_write(&pobj->oo_ext_idx_sem);
4999 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
5000 struct osd_idmap_cache *oic)
5002 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
5003 struct lu_fid *fid = &oic->oic_fid;
5004 struct osd_inode_id *id = &oic->oic_lid;
5005 struct inode *inode = NULL;
5011 if (!fid_is_norm(fid) && !fid_is_igif(fid))
5014 if (dev->od_noscrub && !thread_is_running(&scrub->os_thread))
5017 if (scrub->os_pos_current > id->oii_ino)
5021 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
5022 if (rc == -ENOENT) {
5023 __u32 gen = id->oii_gen;
5029 inode = osd_iget(oti, dev, id);
5030 /* The inode has been removed (by race maybe). */
5031 if (IS_ERR(inode)) {
5032 rc = PTR_ERR(inode);
5034 RETURN(rc == -ESTALE ? -ENOENT : rc);
5037 /* The OI mapping is lost. */
5038 if (gen != OSD_OII_NOGEN)
5041 /* The inode may has been reused by others, we do not know,
5042 * leave it to be handled by subsequent osd_fid_lookup(). */
5044 } else if (rc || osd_id_eq(id, &oti->oti_id)) {
5051 if (thread_is_running(&scrub->os_thread)) {
5052 if (inode == NULL) {
5053 inode = osd_iget(oti, dev, id);
5054 /* The inode has been removed (by race maybe). */
5055 if (IS_ERR(inode)) {
5056 rc = PTR_ERR(inode);
5058 RETURN(rc == -ESTALE ? -ENOENT : rc);
5062 rc = osd_oii_insert(dev, oic, insert);
5063 /* There is race condition between osd_oi_lookup and OI scrub.
5064 * The OI scrub finished just after osd_oi_lookup() failure.
5065 * Under such case, it is unnecessary to trigger OI scrub again,
5066 * but try to call osd_oi_lookup() again. */
5067 if (unlikely(rc == -EAGAIN))
5070 if (!S_ISDIR(inode->i_mode))
5073 rc = osd_check_lmv(oti, dev, inode, oic);
5078 if (!dev->od_noscrub && ++once == 1) {
5079 rc = osd_scrub_start(oti->oti_env, dev, SS_AUTO_PARTIAL |
5080 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
5081 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
5082 "%s: trigger partial OI scrub for RPC inconsistency "
5083 "checking FID "DFID": rc = %d\n",
5084 osd_dev2name(dev), PFID(fid), rc);
5085 if (rc == 0 || rc == -EALREADY)
5098 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
5099 struct osd_device *dev,
5100 struct osd_idmap_cache *oic,
5101 struct lu_fid *fid, __u32 ino)
5103 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
5104 struct inode *inode;
5107 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
5108 inode = osd_iget(oti, dev, &oic->oic_lid);
5109 if (IS_ERR(inode)) {
5110 fid_zero(&oic->oic_fid);
5111 return PTR_ERR(inode);
5114 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, loa);
5117 fid_zero(&oic->oic_fid);
5119 *fid = oic->oic_fid = loa->loa_lma.lma_self_fid;
5123 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
5124 struct osd_inode_id *id, const struct lu_fid *fid)
5126 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
5127 id->oii_ino, id->oii_gen, info);
5128 info->oti_cache.oic_lid = *id;
5129 info->oti_cache.oic_fid = *fid;
5130 info->oti_cache.oic_dev = osd;
5134 * Get parent FID from the linkEA.
5136 * For a directory which parent resides on remote MDT, to satisfy the
5137 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
5138 * the other hand, to make the lookup(..) on the directory can return
5139 * the real parent FID, we append the real parent FID after its ".."
5140 * name entry in the /REMOTE_PARENT_DIR.
5142 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
5143 * backup. So after the restore, we cannot get the right parent FID from
5144 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
5145 * we have stored the real parent FID in the directory object's linkEA,
5146 * we can parse the linkEA for the real parent FID.
5148 * \param[in] env pointer to the thread context
5149 * \param[in] obj pointer to the object to be handled
5150 * \param[out]fid pointer to the buffer to hold the parent FID
5152 * \retval 0 for getting the real parent FID successfully
5153 * \retval negative error number on failure
5155 static int osd_get_pfid_from_linkea(const struct lu_env *env,
5156 struct osd_object *obj,
5159 struct osd_thread_info *oti = osd_oti_get(env);
5160 struct lu_buf *buf = &oti->oti_big_buf;
5161 struct dentry *dentry = &oti->oti_obj_dentry;
5162 struct inode *inode = obj->oo_inode;
5163 struct linkea_data ldata = { NULL };
5168 if (!S_ISDIR(inode->i_mode))
5172 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5173 buf->lb_buf, buf->lb_len);
5174 if (rc == -ERANGE) {
5175 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5178 lu_buf_realloc(buf, rc);
5179 if (buf->lb_buf == NULL)
5186 if (unlikely(rc == 0))
5192 if (unlikely(buf->lb_buf == NULL)) {
5193 lu_buf_realloc(buf, rc);
5194 if (buf->lb_buf == NULL)
5201 rc = linkea_init_with_rec(&ldata);
5203 linkea_first_entry(&ldata);
5204 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5210 static int osd_verify_ent_by_linkea(const struct lu_env *env,
5211 struct inode *inode,
5212 const struct lu_fid *pfid,
5213 const char *name, const int namelen)
5215 struct osd_thread_info *oti = osd_oti_get(env);
5216 struct lu_buf *buf = &oti->oti_big_buf;
5217 struct dentry *dentry = &oti->oti_obj_dentry;
5218 struct linkea_data ldata = { NULL };
5219 struct lu_name cname = { .ln_name = name,
5220 .ln_namelen = namelen };
5225 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5226 buf->lb_buf, buf->lb_len);
5228 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
5233 if (unlikely(rc == 0))
5236 if (buf->lb_len < rc) {
5237 lu_buf_realloc(buf, rc);
5238 if (buf->lb_buf == NULL)
5245 rc = linkea_init_with_rec(&ldata);
5247 rc = linkea_links_find(&ldata, &cname, pfid);
5253 * Calls ->lookup() to find dentry. From dentry get inode and
5254 * read inode's ea to get fid. This is required for interoperability
5257 * \retval 0, on success
5258 * \retval -ve, on error
5260 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
5261 struct dt_rec *rec, const struct dt_key *key)
5263 struct inode *dir = obj->oo_inode;
5264 struct dentry *dentry;
5265 struct ldiskfs_dir_entry_2 *de;
5266 struct buffer_head *bh;
5267 struct lu_fid *fid = (struct lu_fid *) rec;
5268 struct htree_lock *hlock = NULL;
5273 LASSERT(dir->i_op != NULL);
5274 LASSERT(dir->i_op->lookup != NULL);
5276 dentry = osd_child_dentry_get(env, obj,
5277 (char *)key, strlen((char *)key));
5279 if (obj->oo_hl_head != NULL) {
5280 hlock = osd_oti_get(env)->oti_hlock;
5281 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5282 dir, LDISKFS_HLOCK_LOOKUP);
5284 down_read(&obj->oo_ext_idx_sem);
5287 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5289 struct osd_thread_info *oti = osd_oti_get(env);
5290 struct osd_inode_id *id = &oti->oti_id;
5291 struct osd_idmap_cache *oic = &oti->oti_cache;
5292 struct osd_device *dev = osd_obj2dev(obj);
5294 ino = le32_to_cpu(de->inode);
5295 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
5297 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
5301 rc = osd_get_fid_from_dentry(de, rec);
5303 /* done with de, release bh */
5306 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5307 const char *name = (const char *)key;
5309 /* If the parent is on remote MDT, and there
5310 * is no FID-in-dirent, then we have to get
5311 * the parent FID from the linkEA. */
5312 if (likely(strlen(name) == 2 &&
5313 name[0] == '.' && name[1] == '.'))
5314 rc = osd_get_pfid_from_linkea(env, obj,
5317 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5320 osd_id_gen(id, ino, OSD_OII_NOGEN);
5323 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
5324 fid_zero(&oic->oic_fid);
5329 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
5330 rc = osd_consistency_check(oti, dev, oic);
5332 fid_zero(&oic->oic_fid);
5334 /* Other error should not affect lookup result. */
5344 ldiskfs_htree_unlock(hlock);
5346 up_read(&obj->oo_ext_idx_sem);
5350 static int osd_index_declare_ea_insert(const struct lu_env *env,
5351 struct dt_object *dt,
5352 const struct dt_rec *rec,
5353 const struct dt_key *key,
5354 struct thandle *handle)
5356 struct osd_thandle *oh;
5357 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5358 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5359 const struct lu_fid *fid = rec1->rec_fid;
5360 int credits, rc = 0;
5361 struct osd_idmap_cache *idc;
5364 LASSERT(!dt_object_remote(dt));
5365 LASSERT(handle != NULL);
5366 LASSERT(fid != NULL);
5367 LASSERT(rec1->rec_type != 0);
5369 oh = container_of0(handle, struct osd_thandle, ot_super);
5370 LASSERT(oh->ot_handle == NULL);
5372 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
5374 /* we can't call iget() while a transactions is running
5375 * (this can lead to a deadlock), but we need to know
5376 * inum and object type. so we find this information at
5377 * declaration and cache in per-thread info */
5378 idc = osd_idc_find_or_init(env, osd, fid);
5380 RETURN(PTR_ERR(idc));
5381 if (idc->oic_remote) {
5382 /* a reference to remote inode is represented by an
5383 * agent inode which we have to create */
5384 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
5385 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
5388 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
5390 if (osd_dt_obj(dt)->oo_inode != NULL) {
5391 struct inode *inode = osd_dt_obj(dt)->oo_inode;
5393 /* We ignore block quota on meta pool (MDTs), so needn't
5394 * calculate how many blocks will be consumed by this index
5396 rc = osd_declare_inode_qid(env, i_uid_read(inode),
5398 i_projid_read(inode), 0,
5399 oh, osd_dt_obj(dt), NULL,
5404 #ifdef HAVE_PROJECT_QUOTA
5405 /* Reserve credits for local agent inode to transfer
5406 * to 0, quota enforcement is ignored in this case.
5408 if (idc->oic_remote &&
5409 LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
5410 i_projid_read(inode) != 0)
5411 rc = osd_declare_attr_qid(env, osd_dt_obj(dt), oh,
5412 0, i_projid_read(inode),
5413 0, false, PRJQUOTA);
5422 * Index add function for interoperability mode (b11826).
5423 * It will add the directory entry.This entry is needed to
5424 * maintain name->fid mapping.
5426 * \param key it is key i.e. file entry to be inserted
5427 * \param rec it is value of given key i.e. fid
5429 * \retval 0, on success
5430 * \retval -ve, on error
5432 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
5433 const struct dt_rec *rec,
5434 const struct dt_key *key, struct thandle *th,
5437 struct osd_object *obj = osd_dt_obj(dt);
5438 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5439 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5440 const struct lu_fid *fid = rec1->rec_fid;
5441 const char *name = (const char *)key;
5442 struct osd_thread_info *oti = osd_oti_get(env);
5443 struct inode *child_inode = NULL;
5444 struct osd_idmap_cache *idc;
5448 if (!dt_object_exists(dt))
5451 LASSERT(osd_invariant(obj));
5452 LASSERT(!dt_object_remote(dt));
5453 LASSERT(th != NULL);
5455 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5457 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
5459 idc = osd_idc_find(env, osd, fid);
5460 if (unlikely(idc == NULL)) {
5461 /* this dt_insert() wasn't declared properly, so
5462 * FID is missing in OI cache. we better do not
5463 * lookup FID in FLDB/OI and don't risk to deadlock,
5464 * but in some special cases (lfsck testing, etc)
5465 * it's much simpler than fixing a caller */
5466 CERROR("%s: "DFID" wasn't declared for insert\n",
5467 osd_name(osd), PFID(fid));
5469 idc = osd_idc_find_or_init(env, osd, fid);
5471 RETURN(PTR_ERR(idc));
5474 if (idc->oic_remote) {
5475 /* Insert remote entry */
5476 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
5477 struct osd_mdobj_map *omm = osd->od_mdt_map;
5478 struct osd_thandle *oh;
5480 /* If parent on remote MDT, we need put this object
5482 oh = container_of(th, typeof(*oh), ot_super);
5483 rc = osd_add_to_remote_parent(env, osd, obj, oh);
5485 CERROR("%s: add "DFID" error: rc = %d\n",
5487 PFID(lu_object_fid(&dt->do_lu)), rc);
5491 child_inode = igrab(omm->omm_remote_parent->d_inode);
5493 child_inode = osd_create_local_agent_inode(env, osd,
5494 obj, fid, rec1->rec_type & S_IFMT, th);
5495 if (IS_ERR(child_inode))
5496 RETURN(PTR_ERR(child_inode));
5499 /* Insert local entry */
5500 if (unlikely(idc->oic_lid.oii_ino == 0)) {
5501 /* for a reason OI cache wasn't filled properly */
5502 CERROR("%s: OIC for "DFID" isn't filled\n",
5503 osd_name(osd), PFID(fid));
5506 child_inode = oti->oti_inode;
5507 if (unlikely(child_inode == NULL)) {
5508 struct ldiskfs_inode_info *lii;
5512 child_inode = oti->oti_inode = &lii->vfs_inode;
5514 child_inode->i_sb = osd_sb(osd);
5515 child_inode->i_ino = idc->oic_lid.oii_ino;
5516 child_inode->i_mode = rec1->rec_type & S_IFMT;
5519 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
5521 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
5522 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
5524 if (child_inode && child_inode != oti->oti_inode)
5526 LASSERT(osd_invariant(obj));
5527 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5532 * Initialize osd Iterator for given osd index object.
5534 * \param dt osd index object
5537 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
5538 struct dt_object *dt,
5541 struct osd_it_iam *it;
5542 struct osd_object *obj = osd_dt_obj(dt);
5543 struct lu_object *lo = &dt->do_lu;
5544 struct iam_path_descr *ipd;
5545 struct iam_container *bag = &obj->oo_dir->od_container;
5547 if (!dt_object_exists(dt))
5548 return ERR_PTR(-ENOENT);
5552 return ERR_PTR(-ENOMEM);
5554 ipd = osd_it_ipd_get(env, bag);
5555 if (likely(ipd != NULL)) {
5559 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
5560 return (struct dt_it *)it;
5563 return ERR_PTR(-ENOMEM);
5568 * free given Iterator.
5570 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
5572 struct osd_it_iam *it = (struct osd_it_iam *)di;
5573 struct osd_object *obj = it->oi_obj;
5575 iam_it_fini(&it->oi_it);
5576 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
5577 osd_object_put(env, obj);
5582 * Move Iterator to record specified by \a key
5584 * \param di osd iterator
5585 * \param key key for index
5587 * \retval +ve di points to record with least key not larger than key
5588 * \retval 0 di points to exact matched key
5589 * \retval -ve failure
5592 static int osd_it_iam_get(const struct lu_env *env,
5593 struct dt_it *di, const struct dt_key *key)
5595 struct osd_thread_info *oti = osd_oti_get(env);
5596 struct osd_it_iam *it = (struct osd_it_iam *)di;
5598 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5599 /* swab quota uid/gid */
5600 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5601 key = (struct dt_key *)&oti->oti_quota_id;
5604 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
5610 * \param di osd iterator
5612 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
5614 struct osd_it_iam *it = (struct osd_it_iam *)di;
5616 iam_it_put(&it->oi_it);
5620 * Move iterator by one record
5622 * \param di osd iterator
5624 * \retval +1 end of container reached
5626 * \retval -ve failure
5629 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
5631 struct osd_it_iam *it = (struct osd_it_iam *)di;
5633 return iam_it_next(&it->oi_it);
5637 * Return pointer to the key under iterator.
5640 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
5641 const struct dt_it *di)
5643 struct osd_thread_info *oti = osd_oti_get(env);
5644 struct osd_it_iam *it = (struct osd_it_iam *)di;
5645 struct osd_object *obj = it->oi_obj;
5648 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
5650 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
5651 /* swab quota uid/gid */
5652 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
5653 key = (struct dt_key *)&oti->oti_quota_id;
5660 * Return size of key under iterator (in bytes)
5663 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5665 struct osd_it_iam *it = (struct osd_it_iam *)di;
5667 return iam_it_key_size(&it->oi_it);
5671 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5673 /* check if file type is required */
5674 if (ent->lde_attrs & LUDA_TYPE) {
5675 struct luda_type *lt;
5676 int align = sizeof(*lt) - 1;
5678 len = (len + align) & ~align;
5679 lt = (struct luda_type *)(ent->lde_name + len);
5680 lt->lt_type = cpu_to_le16(DTTOIF(type));
5683 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5687 * build lu direct from backend fs dirent.
5691 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5692 char *name, __u16 namelen, __u16 type, __u32 attr)
5694 ent->lde_attrs = attr | LUDA_FID;
5695 fid_cpu_to_le(&ent->lde_fid, fid);
5697 ent->lde_hash = cpu_to_le64(offset);
5698 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5700 strncpy(ent->lde_name, name, namelen);
5701 ent->lde_name[namelen] = '\0';
5702 ent->lde_namelen = cpu_to_le16(namelen);
5704 /* append lustre attributes */
5705 osd_it_append_attrs(ent, namelen, type);
5709 * Return pointer to the record under iterator.
5711 static int osd_it_iam_rec(const struct lu_env *env,
5712 const struct dt_it *di,
5713 struct dt_rec *dtrec, __u32 attr)
5715 struct osd_it_iam *it = (struct osd_it_iam *)di;
5716 struct osd_thread_info *info = osd_oti_get(env);
5719 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5720 const struct osd_fid_pack *rec;
5721 struct lu_fid *fid = &info->oti_fid;
5722 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5728 name = (char *)iam_it_key_get(&it->oi_it);
5730 RETURN(PTR_ERR(name));
5732 namelen = iam_it_key_size(&it->oi_it);
5734 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5736 RETURN(PTR_ERR(rec));
5738 rc = osd_fid_unpack(fid, rec);
5742 hash = iam_it_store(&it->oi_it);
5744 /* IAM does not store object type in IAM index (dir) */
5745 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5747 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5748 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5749 (struct iam_rec *)dtrec);
5750 osd_quota_unpack(it->oi_obj, dtrec);
5752 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5753 (struct iam_rec *)dtrec);
5760 * Returns cookie for current Iterator position.
5762 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5764 struct osd_it_iam *it = (struct osd_it_iam *)di;
5766 return iam_it_store(&it->oi_it);
5770 * Restore iterator from cookie.
5772 * \param di osd iterator
5773 * \param hash Iterator location cookie
5775 * \retval +ve di points to record with least key not larger than key.
5776 * \retval 0 di points to exact matched key
5777 * \retval -ve failure
5780 static int osd_it_iam_load(const struct lu_env *env,
5781 const struct dt_it *di, __u64 hash)
5783 struct osd_it_iam *it = (struct osd_it_iam *)di;
5785 return iam_it_load(&it->oi_it, hash);
5788 static const struct dt_index_operations osd_index_iam_ops = {
5789 .dio_lookup = osd_index_iam_lookup,
5790 .dio_declare_insert = osd_index_declare_iam_insert,
5791 .dio_insert = osd_index_iam_insert,
5792 .dio_declare_delete = osd_index_declare_iam_delete,
5793 .dio_delete = osd_index_iam_delete,
5795 .init = osd_it_iam_init,
5796 .fini = osd_it_iam_fini,
5797 .get = osd_it_iam_get,
5798 .put = osd_it_iam_put,
5799 .next = osd_it_iam_next,
5800 .key = osd_it_iam_key,
5801 .key_size = osd_it_iam_key_size,
5802 .rec = osd_it_iam_rec,
5803 .store = osd_it_iam_store,
5804 .load = osd_it_iam_load
5810 * Creates or initializes iterator context.
5812 * \retval struct osd_it_ea, iterator structure on success
5815 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5816 struct dt_object *dt,
5819 struct osd_object *obj = osd_dt_obj(dt);
5820 struct osd_thread_info *info = osd_oti_get(env);
5821 struct osd_it_ea *oie;
5823 struct lu_object *lo = &dt->do_lu;
5824 struct dentry *obj_dentry;
5827 if (!dt_object_exists(dt) || obj->oo_destroyed)
5828 RETURN(ERR_PTR(-ENOENT));
5830 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5832 RETURN(ERR_PTR(-ENOMEM));
5833 obj_dentry = &oie->oie_dentry;
5835 obj_dentry->d_inode = obj->oo_inode;
5836 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5837 obj_dentry->d_name.hash = 0;
5839 oie->oie_rd_dirent = 0;
5840 oie->oie_it_dirent = 0;
5841 oie->oie_dirent = NULL;
5842 if (unlikely(!info->oti_it_ea_buf_used)) {
5843 oie->oie_buf = info->oti_it_ea_buf;
5844 info->oti_it_ea_buf_used = 1;
5846 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5847 if (oie->oie_buf == NULL)
5848 RETURN(ERR_PTR(-ENOMEM));
5852 file = &oie->oie_file;
5854 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5855 if (attr & LUDA_64BITHASH)
5856 file->f_mode = FMODE_64BITHASH;
5858 file->f_mode = FMODE_32BITHASH;
5859 file->f_path.dentry = obj_dentry;
5860 file->f_mapping = obj->oo_inode->i_mapping;
5861 file->f_op = obj->oo_inode->i_fop;
5862 set_file_inode(file, obj->oo_inode);
5865 RETURN((struct dt_it *) oie);
5869 * Destroy or finishes iterator context.
5871 * \param di iterator structure to be destroyed
5873 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5875 struct osd_thread_info *info = osd_oti_get(env);
5876 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5877 struct osd_object *obj = oie->oie_obj;
5878 struct inode *inode = obj->oo_inode;
5881 oie->oie_file.f_op->release(inode, &oie->oie_file);
5882 osd_object_put(env, obj);
5883 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5884 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5886 info->oti_it_ea_buf_used = 0;
5887 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5892 * It position the iterator at given key, so that next lookup continues from
5893 * that key Or it is similar to dio_it->load() but based on a key,
5894 * rather than file position.
5896 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5899 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5901 static int osd_it_ea_get(const struct lu_env *env,
5902 struct dt_it *di, const struct dt_key *key)
5904 struct osd_it_ea *it = (struct osd_it_ea *)di;
5907 LASSERT(((const char *)key)[0] == '\0');
5908 it->oie_file.f_pos = 0;
5909 it->oie_rd_dirent = 0;
5910 it->oie_it_dirent = 0;
5911 it->oie_dirent = NULL;
5919 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5923 struct osd_filldir_cbs {
5924 #ifdef HAVE_DIR_CONTEXT
5925 struct dir_context ctx;
5927 struct osd_it_ea *it;
5930 * It is called internally by ->readdir(). It fills the
5931 * iterator's in-memory data structure with required
5932 * information i.e. name, namelen, rec_size etc.
5934 * \param buf in which information to be filled in.
5935 * \param name name of the file in given dir
5937 * \retval 0 on success
5938 * \retval 1 on buffer full
5940 #ifdef HAVE_FILLDIR_USE_CTX
5941 static int osd_ldiskfs_filldir(struct dir_context *buf,
5943 static int osd_ldiskfs_filldir(void *buf,
5945 const char *name, int namelen,
5946 loff_t offset, __u64 ino, unsigned d_type)
5948 struct osd_it_ea *it =
5949 ((struct osd_filldir_cbs *)buf)->it;
5950 struct osd_object *obj = it->oie_obj;
5951 struct osd_it_ea_dirent *ent = it->oie_dirent;
5952 struct lu_fid *fid = &ent->oied_fid;
5953 struct osd_fid_pack *rec;
5956 /* this should never happen */
5957 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5958 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5962 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5966 /* "." is just the object itself. */
5967 if (namelen == 1 && name[0] == '.') {
5968 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5969 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5970 rec = (struct osd_fid_pack*) (name + namelen + 1);
5971 if (osd_fid_unpack(fid, rec) != 0)
5976 d_type &= ~LDISKFS_DIRENT_LUFID;
5978 /* NOT export local root. */
5979 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5980 ino = obj->oo_inode->i_ino;
5981 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5984 ent->oied_ino = ino;
5985 ent->oied_off = offset;
5986 ent->oied_namelen = namelen;
5987 ent->oied_type = d_type;
5989 memcpy(ent->oied_name, name, namelen);
5991 it->oie_rd_dirent++;
5992 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5997 * Calls ->readdir() to load a directory entry at a time
5998 * and stored it in iterator's in-memory data structure.
6000 * \param di iterator's in memory structure
6002 * \retval 0 on success
6003 * \retval -ve on error
6004 * \retval +1 reach the end of entry
6006 static int osd_ldiskfs_it_fill(const struct lu_env *env,
6007 const struct dt_it *di)
6009 struct osd_it_ea *it = (struct osd_it_ea *)di;
6010 struct osd_object *obj = it->oie_obj;
6011 struct inode *inode = obj->oo_inode;
6012 struct htree_lock *hlock = NULL;
6013 struct file *filp = &it->oie_file;
6015 struct osd_filldir_cbs buf = {
6016 #ifdef HAVE_DIR_CONTEXT
6017 .ctx.actor = osd_ldiskfs_filldir,
6023 it->oie_dirent = it->oie_buf;
6024 it->oie_rd_dirent = 0;
6026 if (obj->oo_hl_head != NULL) {
6027 hlock = osd_oti_get(env)->oti_hlock;
6028 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
6029 inode, LDISKFS_HLOCK_READDIR);
6031 down_read(&obj->oo_ext_idx_sem);
6034 #ifdef HAVE_DIR_CONTEXT
6035 buf.ctx.pos = filp->f_pos;
6036 rc = inode->i_fop->iterate(filp, &buf.ctx);
6037 filp->f_pos = buf.ctx.pos;
6039 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
6043 ldiskfs_htree_unlock(hlock);
6045 up_read(&obj->oo_ext_idx_sem);
6047 if (it->oie_rd_dirent == 0) {
6048 /*If it does not get any dirent, it means it has been reached
6049 *to the end of the dir */
6050 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
6054 it->oie_dirent = it->oie_buf;
6055 it->oie_it_dirent = 1;
6062 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6063 * to load a directory entry at a time and stored it in
6064 * iterator's in-memory data structure.
6066 * \param di iterator's in memory structure
6068 * \retval +ve iterator reached to end
6069 * \retval 0 iterator not reached to end
6070 * \retval -ve on error
6072 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
6074 struct osd_it_ea *it = (struct osd_it_ea *)di;
6079 if (it->oie_it_dirent < it->oie_rd_dirent) {
6081 (void *) it->oie_dirent +
6082 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
6083 it->oie_dirent->oied_namelen);
6084 it->oie_it_dirent++;
6087 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
6090 rc = osd_ldiskfs_it_fill(env, di);
6097 * Returns the key at current position from iterator's in memory structure.
6099 * \param di iterator's in memory structure
6101 * \retval key i.e. struct dt_key on success
6103 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
6104 const struct dt_it *di)
6106 struct osd_it_ea *it = (struct osd_it_ea *)di;
6108 return (struct dt_key *)it->oie_dirent->oied_name;
6112 * Returns the key's size at current position from iterator's in memory structure.
6114 * \param di iterator's in memory structure
6116 * \retval key_size i.e. struct dt_key on success
6118 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
6120 struct osd_it_ea *it = (struct osd_it_ea *)di;
6122 return it->oie_dirent->oied_namelen;
6125 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
6127 if (LDISKFS_DIR_REC_LEN(de) >=
6128 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
6135 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
6136 unsigned blocksize, bool dotdot)
6139 return osd_dotdot_has_space(de);
6141 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
6142 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
6149 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
6150 handle_t *jh, struct dentry *dentry,
6151 const struct lu_fid *fid, struct buffer_head *bh,
6152 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
6155 struct inode *dir = dentry->d_parent->d_inode;
6156 struct inode *inode = dentry->d_inode;
6157 struct osd_fid_pack *rec;
6158 struct ldiskfs_dentry_param *ldp;
6159 int namelen = dentry->d_name.len;
6161 struct osd_thread_info *info = osd_oti_get(env);
6164 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
6165 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6168 /* There is enough space to hold the FID-in-dirent. */
6169 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
6170 rc = ldiskfs_journal_get_write_access(jh, bh);
6174 de->name[namelen] = 0;
6175 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
6176 rec->fp_len = sizeof(struct lu_fid) + 1;
6177 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
6178 de->file_type |= LDISKFS_DIRENT_LUFID;
6179 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
6186 rc = ldiskfs_delete_entry(jh, dir, de, bh);
6190 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
6191 osd_get_ldiskfs_dirent_param(ldp, fid);
6192 dentry->d_fsdata = (void *)ldp;
6193 ll_vfs_dq_init(dir);
6194 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
6195 /* It is too bad, we cannot reinsert the name entry back.
6196 * That means we lose it! */
6198 CDEBUG(D_LFSCK, "%s: fail to reinsert the dirent, "
6199 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
6200 osd_ino2name(inode),
6201 dir->i_ino, dir->i_generation, namelen,
6202 dentry->d_name.name, PFID(fid), rc);
6208 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
6209 struct osd_it_ea *it, struct lu_fid *fid,
6210 struct osd_inode_id *id, __u32 *attr)
6212 struct osd_thread_info *info = osd_oti_get(env);
6213 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
6214 struct osd_device *dev = osd_obj2dev(obj);
6215 struct super_block *sb = osd_sb(dev);
6216 const char *devname = osd_name(dev);
6217 struct osd_it_ea_dirent *ent = it->oie_dirent;
6218 struct inode *dir = obj->oo_inode;
6219 struct htree_lock *hlock = NULL;
6220 struct buffer_head *bh = NULL;
6221 handle_t *jh = NULL;
6222 struct ldiskfs_dir_entry_2 *de;
6223 struct dentry *dentry;
6224 struct inode *inode;
6225 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
6228 bool dotdot = false;
6232 if (ent->oied_name[0] == '.') {
6233 if (ent->oied_namelen == 1)
6236 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
6240 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
6241 inode = osd_iget(info, dev, id);
6242 if (IS_ERR(inode)) {
6243 rc = PTR_ERR(inode);
6244 if (rc == -ENOENT || rc == -ESTALE) {
6245 /* Maybe dangling name entry, or
6246 * corrupted directory entry. */
6247 *attr |= LUDA_UNKNOWN;
6250 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
6251 "check_repair, dir = %lu/%u, name = %.*s, "
6252 "ino = %llu, rc = %d\n",
6253 devname, dir->i_ino, dir->i_generation,
6254 ent->oied_namelen, ent->oied_name,
6261 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
6263 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
6264 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
6269 /* We need to ensure that the name entry is still valid.
6270 * Because it may be removed or renamed by other already.
6272 * The unlink or rename operation will start journal before PDO lock,
6273 * so to avoid deadlock, here we need to start journal handle before
6274 * related PDO lock also. But because we do not know whether there
6275 * will be something to be repaired before PDO lock, we just start
6276 * journal without conditions.
6278 * We may need to remove the name entry firstly, then insert back.
6279 * One credit is for user quota file update.
6280 * One credit is for group quota file update.
6281 * Two credits are for dirty inode. */
6282 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
6283 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
6285 if (dev->od_dirent_journal != 0) {
6288 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
6291 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
6292 "check_repair, dir = %lu/%u, credits = %d, "
6293 "name = %.*s, ino = %llu: rc = %d\n",
6294 devname, dir->i_ino, dir->i_generation, credits,
6295 ent->oied_namelen, ent->oied_name,
6298 GOTO(out_inode, rc);
6301 if (obj->oo_hl_head != NULL) {
6302 hlock = osd_oti_get(env)->oti_hlock;
6303 /* "0" means exclusive lock for the whole directory.
6304 * We need to prevent others access such name entry
6305 * during the delete + insert. Neither HLOCK_ADD nor
6306 * HLOCK_DEL cannot guarantee the atomicity. */
6307 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
6309 down_write(&obj->oo_ext_idx_sem);
6312 if (obj->oo_hl_head != NULL) {
6313 hlock = osd_oti_get(env)->oti_hlock;
6314 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
6315 LDISKFS_HLOCK_LOOKUP);
6317 down_read(&obj->oo_ext_idx_sem);
6321 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6322 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
6323 *attr |= LUDA_IGNORE;
6328 /* For dotdot entry, if there is not enough space to hold the
6329 * FID-in-dirent, just keep them there. It only happens when the
6330 * device upgraded from 1.8 or restored from MDT file-level backup.
6331 * For the whole directory, only dotdot entry have no FID-in-dirent
6332 * and needs to get FID from LMA when readdir, it will not affect the
6333 * performance much. */
6334 if (dotdot && !osd_dotdot_has_space(de)) {
6335 *attr |= LUDA_UNKNOWN;
6341 if (lu_fid_eq(fid, &lma->lma_self_fid))
6344 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
6345 struct lu_fid *tfid = &lma->lma_self_fid;
6347 if (likely(dotdot &&
6348 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
6349 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
6350 /* It must be REMOTE_PARENT_DIR and as the
6351 * 'dotdot' entry of remote directory */
6352 *attr |= LUDA_IGNORE;
6354 CDEBUG(D_LFSCK, "%s: expect remote agent "
6355 "parent directory, but got %.*s under "
6356 "dir = %lu/%u with the FID "DFID"\n",
6357 devname, ent->oied_namelen,
6358 ent->oied_name, dir->i_ino,
6359 dir->i_generation, PFID(tfid));
6361 *attr |= LUDA_UNKNOWN;
6368 if (!fid_is_zero(fid)) {
6369 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
6371 if (rc == -ENOENT ||
6373 !(dev->od_scrub.os_scrub.os_file.sf_flags & SF_UPGRADE))) {
6374 /* linkEA does not recognize the dirent entry,
6375 * it may because the dirent entry corruption
6376 * and points to other's inode. */
6377 CDEBUG(D_LFSCK, "%s: the target inode does not "
6378 "recognize the dirent, dir = %lu/%u, "
6379 " name = %.*s, ino = %llu, "
6380 DFID": rc = %d\n", devname, dir->i_ino,
6381 dir->i_generation, ent->oied_namelen,
6382 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6383 *attr |= LUDA_UNKNOWN;
6388 if (rc && rc != -ENODATA) {
6389 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
6390 "dir = %lu/%u, name = %.*s, ino = %llu, "
6391 DFID": rc = %d\n", devname, dir->i_ino,
6392 dir->i_generation, ent->oied_namelen,
6393 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6394 *attr |= LUDA_UNKNOWN;
6401 /* linkEA recognizes the dirent entry, the FID-in-LMA is
6402 * valid, trusted, in spite of fid_is_sane(fid) or not. */
6403 if (*attr & LUDA_VERIFY_DRYRUN) {
6404 *fid = lma->lma_self_fid;
6405 *attr |= LUDA_REPAIR;
6412 dev->od_dirent_journal = 1;
6413 if (hlock != NULL) {
6414 ldiskfs_htree_unlock(hlock);
6417 up_read(&obj->oo_ext_idx_sem);
6423 *fid = lma->lma_self_fid;
6425 /* Update or append the FID-in-dirent. */
6426 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6427 bh, de, hlock, dotdot);
6429 *attr |= LUDA_REPAIR;
6431 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
6432 "the dirent, dir = %lu/%u, name = %.*s, "
6433 "ino = %llu, "DFID": rc = %d\n",
6434 devname, dir->i_ino, dir->i_generation,
6435 ent->oied_namelen, ent->oied_name,
6436 ent->oied_ino, PFID(fid), rc);
6438 /* lma is NULL, trust the FID-in-dirent if it is valid. */
6439 if (*attr & LUDA_VERIFY_DRYRUN) {
6440 if (fid_is_sane(fid)) {
6441 *attr |= LUDA_REPAIR;
6442 } else if (dev->od_index == 0) {
6443 lu_igif_build(fid, inode->i_ino,
6444 inode->i_generation);
6445 *attr |= LUDA_UPGRADE;
6453 dev->od_dirent_journal = 1;
6454 if (hlock != NULL) {
6455 ldiskfs_htree_unlock(hlock);
6458 up_read(&obj->oo_ext_idx_sem);
6465 if (unlikely(fid_is_sane(fid))) {
6466 /* FID-in-dirent exists, but FID-in-LMA is lost.
6467 * Trust the FID-in-dirent, and add FID-in-LMA. */
6468 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
6470 *attr |= LUDA_REPAIR;
6472 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
6473 "update dirent, dir = %lu/%u, "
6474 "name = %.*s, ino = %llu, "
6476 devname, dir->i_ino, dir->i_generation,
6477 ent->oied_namelen, ent->oied_name,
6478 ent->oied_ino, PFID(fid), rc);
6479 } else if (dev->od_index == 0) {
6480 lu_igif_build(fid, inode->i_ino, inode->i_generation);
6481 /* It is probably IGIF object. Only aappend the
6482 * FID-in-dirent. OI scrub will process FID-in-LMA. */
6483 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6484 bh, de, hlock, dotdot);
6486 *attr |= LUDA_UPGRADE;
6488 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
6489 "after the dirent, dir = %lu/%u, "
6490 "name = %.*s, ino = %llu, "
6492 devname, dir->i_ino, dir->i_generation,
6493 ent->oied_namelen, ent->oied_name,
6494 ent->oied_ino, PFID(fid), rc);
6503 if (hlock != NULL) {
6504 ldiskfs_htree_unlock(hlock);
6506 if (dev->od_dirent_journal != 0)
6507 up_write(&obj->oo_ext_idx_sem);
6509 up_read(&obj->oo_ext_idx_sem);
6513 ldiskfs_journal_stop(jh);
6517 if (rc >= 0 && !dirty)
6518 dev->od_dirent_journal = 0;
6524 * Returns the value at current position from iterator's in memory structure.
6526 * \param di struct osd_it_ea, iterator's in memory structure
6527 * \param attr attr requested for dirent.
6528 * \param lde lustre dirent
6530 * \retval 0 no error and \param lde has correct lustre dirent.
6531 * \retval -ve on error
6533 static inline int osd_it_ea_rec(const struct lu_env *env,
6534 const struct dt_it *di,
6535 struct dt_rec *dtrec, __u32 attr)
6537 struct osd_it_ea *it = (struct osd_it_ea *)di;
6538 struct osd_object *obj = it->oie_obj;
6539 struct osd_device *dev = osd_obj2dev(obj);
6540 struct osd_thread_info *oti = osd_oti_get(env);
6541 struct osd_inode_id *id = &oti->oti_id;
6542 struct lu_fid *fid = &it->oie_dirent->oied_fid;
6543 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6544 __u32 ino = it->oie_dirent->oied_ino;
6548 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
6550 if (attr & LUDA_VERIFY) {
6551 if (unlikely(ino == osd_remote_parent_ino(dev))) {
6552 attr |= LUDA_IGNORE;
6553 /* If the parent is on remote MDT, and there
6554 * is no FID-in-dirent, then we have to get
6555 * the parent FID from the linkEA. */
6556 if (!fid_is_sane(fid) &&
6557 it->oie_dirent->oied_namelen == 2 &&
6558 it->oie_dirent->oied_name[0] == '.' &&
6559 it->oie_dirent->oied_name[1] == '.')
6560 osd_get_pfid_from_linkea(env, obj, fid);
6562 rc = osd_dirent_check_repair(env, obj, it, fid, id,
6566 if (!fid_is_sane(fid))
6567 attr |= LUDA_UNKNOWN;
6569 attr &= ~LU_DIRENT_ATTRS_MASK;
6570 if (!fid_is_sane(fid)) {
6571 bool is_dotdot = false;
6572 if (it->oie_dirent->oied_namelen == 2 &&
6573 it->oie_dirent->oied_name[0] == '.' &&
6574 it->oie_dirent->oied_name[1] == '.')
6576 /* If the parent is on remote MDT, and there
6577 * is no FID-in-dirent, then we have to get
6578 * the parent FID from the linkEA. */
6579 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
6580 rc = osd_get_pfid_from_linkea(env, obj, fid);
6582 if (is_dotdot == false &&
6583 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
6586 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6589 osd_id_gen(id, ino, OSD_OII_NOGEN);
6593 /* Pack the entry anyway, at least the offset is right. */
6594 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
6595 it->oie_dirent->oied_name,
6596 it->oie_dirent->oied_namelen,
6597 it->oie_dirent->oied_type, attr);
6602 if (osd_remote_fid(env, dev, fid))
6605 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
6606 osd_add_oi_cache(oti, dev, id, fid);
6608 RETURN(rc > 0 ? 0 : rc);
6612 * Returns the record size size at current position.
6614 * This function will return record(lu_dirent) size in bytes.
6616 * \param[in] env execution environment
6617 * \param[in] di iterator's in memory structure
6618 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
6619 * calculate the lu_dirent size.
6621 * \retval record size(in bytes & in memory) of the current lu_dirent
6624 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
6627 struct osd_it_ea *it = (struct osd_it_ea *)di;
6629 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
6633 * Returns a cookie for current position of the iterator head, so that
6634 * user can use this cookie to load/start the iterator next time.
6636 * \param di iterator's in memory structure
6638 * \retval cookie for current position, on success
6640 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
6642 struct osd_it_ea *it = (struct osd_it_ea *)di;
6644 return it->oie_dirent->oied_off;
6648 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6649 * to load a directory entry at a time and stored it i inn,
6650 * in iterator's in-memory data structure.
6652 * \param di struct osd_it_ea, iterator's in memory structure
6654 * \retval +ve on success
6655 * \retval -ve on error
6657 static int osd_it_ea_load(const struct lu_env *env,
6658 const struct dt_it *di, __u64 hash)
6660 struct osd_it_ea *it = (struct osd_it_ea *)di;
6664 it->oie_file.f_pos = hash;
6666 rc = osd_ldiskfs_it_fill(env, di);
6677 * Index lookup function for interoperability mode (b11826).
6679 * \param key, key i.e. file name to be searched
6681 * \retval +ve, on success
6682 * \retval -ve, on error
6684 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6685 struct dt_rec *rec, const struct dt_key *key)
6687 struct osd_object *obj = osd_dt_obj(dt);
6692 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6693 LINVRNT(osd_invariant(obj));
6695 rc = osd_ea_lookup_rec(env, obj, rec, key);
6702 * Index and Iterator operations for interoperability
6703 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6705 static const struct dt_index_operations osd_index_ea_ops = {
6706 .dio_lookup = osd_index_ea_lookup,
6707 .dio_declare_insert = osd_index_declare_ea_insert,
6708 .dio_insert = osd_index_ea_insert,
6709 .dio_declare_delete = osd_index_declare_ea_delete,
6710 .dio_delete = osd_index_ea_delete,
6712 .init = osd_it_ea_init,
6713 .fini = osd_it_ea_fini,
6714 .get = osd_it_ea_get,
6715 .put = osd_it_ea_put,
6716 .next = osd_it_ea_next,
6717 .key = osd_it_ea_key,
6718 .key_size = osd_it_ea_key_size,
6719 .rec = osd_it_ea_rec,
6720 .rec_size = osd_it_ea_rec_size,
6721 .store = osd_it_ea_store,
6722 .load = osd_it_ea_load
6726 static void *osd_key_init(const struct lu_context *ctx,
6727 struct lu_context_key *key)
6729 struct osd_thread_info *info;
6731 OBD_ALLOC_PTR(info);
6733 return ERR_PTR(-ENOMEM);
6735 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6736 if (info->oti_it_ea_buf == NULL)
6739 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6741 info->oti_hlock = ldiskfs_htree_lock_alloc();
6742 if (info->oti_hlock == NULL)
6748 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6751 return ERR_PTR(-ENOMEM);
6754 static void osd_key_fini(const struct lu_context *ctx,
6755 struct lu_context_key *key, void* data)
6757 struct osd_thread_info *info = data;
6758 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6759 struct osd_idmap_cache *idc = info->oti_ins_cache;
6761 if (info->oti_inode != NULL)
6763 if (info->oti_hlock != NULL)
6764 ldiskfs_htree_lock_free(info->oti_hlock);
6765 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6766 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6767 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6768 lu_buf_free(&info->oti_big_buf);
6770 LASSERT(info->oti_ins_cache_size > 0);
6771 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6772 info->oti_ins_cache = NULL;
6773 info->oti_ins_cache_size = 0;
6778 static void osd_key_exit(const struct lu_context *ctx,
6779 struct lu_context_key *key, void *data)
6781 struct osd_thread_info *info = data;
6783 LASSERT(info->oti_r_locks == 0);
6784 LASSERT(info->oti_w_locks == 0);
6785 LASSERT(info->oti_txns == 0);
6788 /* type constructor/destructor: osd_type_init, osd_type_fini */
6789 LU_TYPE_INIT_FINI(osd, &osd_key);
6791 struct lu_context_key osd_key = {
6792 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6793 .lct_init = osd_key_init,
6794 .lct_fini = osd_key_fini,
6795 .lct_exit = osd_key_exit
6799 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6800 const char *name, struct lu_device *next)
6802 struct osd_device *osd = osd_dev(d);
6804 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6805 >= sizeof(osd->od_svname))
6807 return osd_procfs_init(osd, name);
6810 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6812 struct seq_server_site *ss = osd_seq_site(osd);
6816 if (osd->od_is_ost || osd->od_cl_seq != NULL)
6819 if (unlikely(ss == NULL))
6822 OBD_ALLOC_PTR(osd->od_cl_seq);
6823 if (osd->od_cl_seq == NULL)
6826 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6827 osd->od_svname, ss->ss_server_seq);
6829 OBD_FREE_PTR(osd->od_cl_seq);
6830 osd->od_cl_seq = NULL;
6834 if (ss->ss_node_id == 0) {
6835 /* If the OSD on the sequence controller(MDT0), then allocate
6836 * sequence here, otherwise allocate sequence after connected
6837 * to MDT0 (see mdt_register_lwp_callback()). */
6838 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6839 &osd->od_cl_seq->lcs_space, env);
6845 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6847 if (osd->od_cl_seq == NULL)
6850 seq_client_fini(osd->od_cl_seq);
6851 OBD_FREE_PTR(osd->od_cl_seq);
6852 osd->od_cl_seq = NULL;
6855 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6859 /* shutdown quota slave instance associated with the device */
6860 if (o->od_quota_slave != NULL) {
6861 qsd_fini(env, o->od_quota_slave);
6862 o->od_quota_slave = NULL;
6865 osd_fid_fini(env, o);
6866 osd_scrub_cleanup(env, o);
6871 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6875 if (o->od_mnt != NULL) {
6876 shrink_dcache_sb(osd_sb(o));
6877 osd_sync(env, &o->od_dt_dev);
6886 static int osd_mount(const struct lu_env *env,
6887 struct osd_device *o, struct lustre_cfg *cfg)
6889 const char *name = lustre_cfg_string(cfg, 0);
6890 const char *dev = lustre_cfg_string(cfg, 1);
6892 unsigned long page, s_flags, lmd_flags = 0;
6893 struct page *__page;
6894 struct file_system_type *type;
6895 char *options = NULL;
6897 struct osd_thread_info *info = osd_oti_get(env);
6898 struct lu_fid *fid = &info->oti_fid;
6899 struct inode *inode;
6900 int rc = 0, force_over_512tb = 0;
6903 if (o->od_mnt != NULL)
6906 if (strlen(dev) >= sizeof(o->od_mntdev))
6908 strcpy(o->od_mntdev, dev);
6910 str = lustre_cfg_string(cfg, 2);
6911 s_flags = simple_strtoul(str, NULL, 0);
6912 str = strstr(str, ":");
6914 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6915 opts = lustre_cfg_string(cfg, 3);
6917 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6918 CERROR("%s: device %s extents feature is not guaranteed to "
6919 "work on big-endian systems. Use \"bigendian_extents\" "
6920 "mount option to override.\n", name, dev);
6924 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6925 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
6926 CWARN("force_over_128tb option is deprecated. "
6927 "Filesystems less than 512TB can be created without any "
6928 "force options. Use force_over_512tb option for "
6929 "filesystems greater than 512TB.\n");
6932 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
6933 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
6934 CWARN("force_over_256tb option is deprecated. "
6935 "Filesystems less than 512TB can be created without any "
6936 "force options. Use force_over_512tb option for "
6937 "filesystems greater than 512TB.\n");
6941 if (opts != NULL && strstr(opts, "force_over_512tb") != NULL)
6942 force_over_512tb = 1;
6944 __page = alloc_page(GFP_KERNEL);
6946 GOTO(out, rc = -ENOMEM);
6947 page = (unsigned long)page_address(__page);
6948 options = (char *)page;
6951 /* strip out the options for back compatiblity */
6952 static char *sout[] = {
6959 /* strip out option we processed in osd */
6960 "bigendian_extents",
6966 strcat(options, opts);
6967 for (rc = 0, str = options; sout[rc]; ) {
6968 char *op = strstr(str, sout[rc]);
6974 if (op == options || *(op - 1) == ',') {
6975 str = op + strlen(sout[rc]);
6976 if (*str == ',' || *str == '\0') {
6977 *str == ',' ? str++ : str;
6978 memmove(op, str, strlen(str) + 1);
6981 for (str = op; *str != ',' && *str != '\0'; str++)
6985 strncat(options, "user_xattr,acl", 14);
6988 /* Glom up mount options */
6989 if (*options != '\0')
6990 strcat(options, ",");
6991 strlcat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
6993 type = get_fs_type("ldiskfs");
6995 CERROR("%s: cannot find ldiskfs module\n", name);
6996 GOTO(out, rc = -ENODEV);
6999 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
7000 module_put(type->owner);
7002 if (IS_ERR(o->od_mnt)) {
7003 rc = PTR_ERR(o->od_mnt);
7005 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
7009 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
7010 osd_sb(o)->s_blocksize_bits > 512ULL << 40 &&
7011 force_over_512tb == 0) {
7012 CERROR("%s: device %s LDISKFS does not support filesystems "
7013 "greater than 512TB and can cause data corruption. "
7014 "Use \"force_over_512tb\" mount option to override.\n",
7016 GOTO(out_mnt, rc = -EINVAL);
7019 if (lmd_flags & LMD_FLG_DEV_RDONLY) {
7020 if (priv_dev_set_rdonly) {
7021 priv_dev_set_rdonly(osd_sb(o)->s_bdev);
7022 o->od_dt_dev.dd_rdonly = 1;
7023 LCONSOLE_WARN("%s: set dev_rdonly on this device\n",
7026 LCONSOLE_WARN("%s: not support dev_rdonly on this device",
7029 GOTO(out_mnt, rc = -EOPNOTSUPP);
7031 } else if (priv_dev_check_rdonly &&
7032 priv_dev_check_rdonly(osd_sb(o)->s_bdev)) {
7033 CERROR("%s: underlying device %s is marked as "
7034 "read-only. Setup failed\n", name, dev);
7036 GOTO(out_mnt, rc = -EROFS);
7039 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
7040 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
7041 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
7042 GOTO(out_mnt, rc = -EINVAL);
7045 #ifdef LDISKFS_MOUNT_DIRDATA
7046 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
7047 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
7048 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
7049 else if (strstr(name, "MDT")) /* don't complain for MGT or OSTs */
7050 CWARN("%s: device %s was upgraded from Lustre-1.x without "
7051 "enabling the dirdata feature. If you do not want to "
7052 "downgrade to Lustre-1.x again, you can enable it via "
7053 "'tune2fs -O dirdata device'\n", name, dev);
7055 inode = osd_sb(o)->s_root->d_inode;
7056 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
7057 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
7059 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
7063 if (lmd_flags & LMD_FLG_NOSCRUB)
7074 __free_page(__page);
7079 static struct lu_device *osd_device_fini(const struct lu_env *env,
7080 struct lu_device *d)
7082 struct osd_device *o = osd_dev(d);
7085 osd_shutdown(env, o);
7087 osd_obj_map_fini(o);
7093 static int osd_device_init0(const struct lu_env *env,
7094 struct osd_device *o,
7095 struct lustre_cfg *cfg)
7097 struct lu_device *l = osd2lu_dev(o);
7098 struct osd_thread_info *info;
7102 /* if the module was re-loaded, env can loose its keys */
7103 rc = lu_env_refill((struct lu_env *) env);
7106 info = osd_oti_get(env);
7109 l->ld_ops = &osd_lu_ops;
7110 o->od_dt_dev.dd_ops = &osd_dt_ops;
7112 spin_lock_init(&o->od_osfs_lock);
7113 mutex_init(&o->od_otable_mutex);
7114 INIT_LIST_HEAD(&o->od_orphan_list);
7116 o->od_read_cache = 1;
7117 o->od_writethrough_cache = 1;
7118 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
7120 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
7121 sizeof(o->od_svname));
7122 if (cplen >= sizeof(o->od_svname)) {
7127 o->od_index = -1; /* -1 means index is invalid */
7128 rc = server_name2index(o->od_svname, &o->od_index, NULL);
7129 if (rc == LDD_F_SV_TYPE_OST)
7132 o->od_full_scrub_ratio = OFSR_DEFAULT;
7133 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
7134 rc = osd_mount(env, o, cfg);
7138 rc = osd_obj_map_init(env, o);
7142 rc = lu_site_init(&o->od_site, l);
7144 GOTO(out_compat, rc);
7145 o->od_site.ls_bottom_dev = l;
7147 rc = lu_site_init_finish(&o->od_site);
7151 INIT_LIST_HEAD(&o->od_ios_list);
7152 /* setup scrub, including OI files initialization */
7153 rc = osd_scrub_setup(env, o);
7157 rc = osd_procfs_init(o, o->od_svname);
7159 CERROR("%s: can't initialize procfs: rc = %d\n",
7161 GOTO(out_scrub, rc);
7164 LASSERT(l->ld_site->ls_linkage.next != NULL);
7165 LASSERT(l->ld_site->ls_linkage.prev != NULL);
7167 /* initialize quota slave instance */
7168 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
7170 if (IS_ERR(o->od_quota_slave)) {
7171 rc = PTR_ERR(o->od_quota_slave);
7172 o->od_quota_slave = NULL;
7173 GOTO(out_procfs, rc);
7181 osd_scrub_cleanup(env, o);
7183 lu_site_fini(&o->od_site);
7185 osd_obj_map_fini(o);
7192 static struct lu_device *osd_device_alloc(const struct lu_env *env,
7193 struct lu_device_type *t,
7194 struct lustre_cfg *cfg)
7196 struct osd_device *o;
7201 return ERR_PTR(-ENOMEM);
7203 rc = dt_device_init(&o->od_dt_dev, t);
7205 /* Because the ctx might be revived in dt_device_init,
7206 * refill the env here */
7207 lu_env_refill((struct lu_env *)env);
7208 rc = osd_device_init0(env, o, cfg);
7210 dt_device_fini(&o->od_dt_dev);
7213 if (unlikely(rc != 0))
7216 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
7219 static struct lu_device *osd_device_free(const struct lu_env *env,
7220 struct lu_device *d)
7222 struct osd_device *o = osd_dev(d);
7225 /* XXX: make osd top device in order to release reference */
7226 d->ld_site->ls_top_dev = d;
7227 lu_site_purge(env, d->ld_site, -1);
7228 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
7229 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
7230 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
7232 lu_site_fini(&o->od_site);
7233 dt_device_fini(&o->od_dt_dev);
7238 static int osd_process_config(const struct lu_env *env,
7239 struct lu_device *d, struct lustre_cfg *cfg)
7241 struct osd_device *o = osd_dev(d);
7245 switch (cfg->lcfg_command) {
7247 rc = osd_mount(env, o, cfg);
7250 lu_dev_del_linkage(d->ld_site, d);
7251 rc = osd_shutdown(env, o);
7254 LASSERT(&o->od_dt_dev);
7255 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
7256 cfg, &o->od_dt_dev);
7257 if (rc > 0 || rc == -ENOSYS) {
7258 rc = class_process_proc_param(PARAM_OST,
7259 lprocfs_osd_obd_vars,
7260 cfg, &o->od_dt_dev);
7272 static int osd_recovery_complete(const struct lu_env *env,
7273 struct lu_device *d)
7275 struct osd_device *osd = osd_dev(d);
7279 if (osd->od_quota_slave == NULL)
7282 /* start qsd instance on recovery completion, this notifies the quota
7283 * slave code that we are about to process new requests now */
7284 rc = qsd_start(env, osd->od_quota_slave);
7289 * we use exports to track all osd users
7291 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
7292 struct obd_device *obd, struct obd_uuid *cluuid,
7293 struct obd_connect_data *data, void *localdata)
7295 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7296 struct lustre_handle conn;
7300 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
7302 rc = class_connect(&conn, obd, cluuid);
7306 *exp = class_conn2export(&conn);
7308 spin_lock(&osd->od_osfs_lock);
7310 spin_unlock(&osd->od_osfs_lock);
7316 * once last export (we don't count self-export) disappeared
7317 * osd can be released
7319 static int osd_obd_disconnect(struct obd_export *exp)
7321 struct obd_device *obd = exp->exp_obd;
7322 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7323 int rc, release = 0;
7326 /* Only disconnect the underlying layers on the final disconnect. */
7327 spin_lock(&osd->od_osfs_lock);
7329 if (osd->od_connects == 0)
7331 spin_unlock(&osd->od_osfs_lock);
7333 rc = class_disconnect(exp); /* bz 9811 */
7335 if (rc == 0 && release)
7336 class_manual_cleanup(obd);
7340 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
7341 struct lu_device *dev)
7343 struct osd_device *osd = osd_dev(dev);
7344 struct lr_server_data *lsd =
7345 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
7349 if (osd->od_quota_slave != NULL) {
7350 /* set up quota slave objects */
7351 result = qsd_prepare(env, osd->od_quota_slave);
7356 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
7357 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
7358 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
7359 osd->od_index_in_idif = 1;
7361 osd->od_index_in_idif = 0;
7362 result = osd_register_proc_index_in_idif(osd);
7367 osd->od_index_in_idif = 1;
7371 result = osd_fid_init(env, osd);
7376 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
7377 struct lu_fid *fid, struct md_op_data *op_data)
7379 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
7381 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
7384 static const struct lu_object_operations osd_lu_obj_ops = {
7385 .loo_object_init = osd_object_init,
7386 .loo_object_delete = osd_object_delete,
7387 .loo_object_release = osd_object_release,
7388 .loo_object_free = osd_object_free,
7389 .loo_object_print = osd_object_print,
7390 .loo_object_invariant = osd_object_invariant
7393 const struct lu_device_operations osd_lu_ops = {
7394 .ldo_object_alloc = osd_object_alloc,
7395 .ldo_process_config = osd_process_config,
7396 .ldo_recovery_complete = osd_recovery_complete,
7397 .ldo_prepare = osd_prepare,
7400 static const struct lu_device_type_operations osd_device_type_ops = {
7401 .ldto_init = osd_type_init,
7402 .ldto_fini = osd_type_fini,
7404 .ldto_start = osd_type_start,
7405 .ldto_stop = osd_type_stop,
7407 .ldto_device_alloc = osd_device_alloc,
7408 .ldto_device_free = osd_device_free,
7410 .ldto_device_init = osd_device_init,
7411 .ldto_device_fini = osd_device_fini
7414 static struct lu_device_type osd_device_type = {
7415 .ldt_tags = LU_DEVICE_DT,
7416 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
7417 .ldt_ops = &osd_device_type_ops,
7418 .ldt_ctx_tags = LCT_LOCAL,
7421 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
7423 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7424 struct super_block *sb = osd_sb(osd);
7426 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
7430 * lprocfs legacy support.
7432 static struct obd_ops osd_obd_device_ops = {
7433 .o_owner = THIS_MODULE,
7434 .o_connect = osd_obd_connect,
7435 .o_disconnect = osd_obd_disconnect,
7436 .o_fid_alloc = osd_fid_alloc,
7437 .o_health_check = osd_health_check,
7440 static int __init osd_init(void)
7444 CLASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
7445 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
7446 /* please, try to keep osd_thread_info smaller than a page */
7447 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
7452 rc = lu_kmem_init(ldiskfs_caches);
7456 #ifdef CONFIG_KALLSYMS
7457 priv_dev_set_rdonly = (void *)kallsyms_lookup_name("dev_set_rdonly");
7458 priv_dev_check_rdonly = (void *)kallsyms_lookup_name("dev_check_rdonly");
7459 /* Clear readonly is unused at this time */
7460 /*priv_dev_clear_rdonly = (void *)kallsyms_lookup_name("dev_clear_rdonly");*/
7463 rc = class_register_type(&osd_obd_device_ops, NULL, true,
7464 lprocfs_osd_module_vars,
7465 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
7467 lu_kmem_fini(ldiskfs_caches);
7471 static void __exit osd_exit(void)
7473 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
7474 lu_kmem_fini(ldiskfs_caches);
7477 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7478 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
7479 MODULE_VERSION(LUSTRE_VERSION_STRING);
7480 MODULE_LICENSE("GPL");
7482 module_init(osd_init);
7483 module_exit(osd_exit);