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>
80 module_param(ldiskfs_pdo, int, 0644);
81 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
83 int ldiskfs_track_declares_assert;
84 module_param(ldiskfs_track_declares_assert, int, 0644);
85 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
87 /* Slab to allocate dynlocks */
88 struct kmem_cache *dynlock_cachep;
90 /* Slab to allocate osd_it_ea */
91 struct kmem_cache *osd_itea_cachep;
93 static struct lu_kmem_descr ldiskfs_caches[] = {
95 .ckd_cache = &dynlock_cachep,
96 .ckd_name = "dynlock_cache",
97 .ckd_size = sizeof(struct dynlock_handle)
100 .ckd_cache = &osd_itea_cachep,
101 .ckd_name = "osd_itea_cache",
102 .ckd_size = sizeof(struct osd_it_ea)
109 static const char dot[] = ".";
110 static const char dotdot[] = "..";
112 static const struct lu_object_operations osd_lu_obj_ops;
113 static const struct dt_object_operations osd_obj_ops;
114 static const struct dt_object_operations osd_obj_otable_it_ops;
115 static const struct dt_index_operations osd_index_iam_ops;
116 static const struct dt_index_operations osd_index_ea_ops;
118 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
119 const struct lu_fid *fid);
120 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
121 struct osd_device *osd);
123 int osd_trans_declare_op2rb[] = {
124 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
125 [OSD_OT_PUNCH] = OSD_OT_MAX,
126 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
127 [OSD_OT_CREATE] = OSD_OT_DESTROY,
128 [OSD_OT_DESTROY] = OSD_OT_CREATE,
129 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
130 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
131 [OSD_OT_WRITE] = OSD_OT_WRITE,
132 [OSD_OT_INSERT] = OSD_OT_DELETE,
133 [OSD_OT_DELETE] = OSD_OT_INSERT,
134 [OSD_OT_QUOTA] = OSD_OT_MAX,
137 static int osd_has_index(const struct osd_object *obj)
139 return obj->oo_dt.do_index_ops != NULL;
142 static int osd_object_invariant(const struct lu_object *l)
144 return osd_invariant(osd_obj(l));
148 * Concurrency: doesn't matter
150 static int osd_is_write_locked(const struct lu_env *env, struct osd_object *o)
152 struct osd_thread_info *oti = osd_oti_get(env);
153 return oti->oti_w_locks > 0 && o->oo_owner == env;
157 * Concurrency: doesn't access mutable data
159 static int osd_root_get(const struct lu_env *env,
160 struct dt_device *dev, struct lu_fid *f)
162 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
167 * the following set of functions are used to maintain per-thread
168 * cache of FID->ino mapping. this mechanism is needed to resolve
169 * FID to inode at dt_insert() which in turn stores ino in the
170 * directory entries to keep ldiskfs compatible with ext[34].
171 * due to locking-originated restrictions we can't lookup ino
172 * using LU cache (deadlock is possible). lookup using OI is quite
173 * expensive. so instead we maintain this cache and methods like
174 * dt_create() fill it. so in the majority of cases dt_insert() is
175 * able to find needed mapping in lockless manner.
177 static struct osd_idmap_cache *
178 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
179 const struct lu_fid *fid)
181 struct osd_thread_info *oti = osd_oti_get(env);
182 struct osd_idmap_cache *idc = oti->oti_ins_cache;
184 for (i = 0; i < oti->oti_ins_cache_used; i++) {
185 if (!lu_fid_eq(&idc[i].oic_fid, fid))
187 if (idc[i].oic_dev != osd)
196 static struct osd_idmap_cache *
197 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
198 const struct lu_fid *fid)
200 struct osd_thread_info *oti = osd_oti_get(env);
201 struct osd_idmap_cache *idc;
204 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
205 i = oti->oti_ins_cache_size * 2;
207 i = OSD_INS_CACHE_SIZE;
208 OBD_ALLOC(idc, sizeof(*idc) * i);
210 return ERR_PTR(-ENOMEM);
211 if (oti->oti_ins_cache != NULL) {
212 memcpy(idc, oti->oti_ins_cache,
213 oti->oti_ins_cache_used * sizeof(*idc));
214 OBD_FREE(oti->oti_ins_cache,
215 oti->oti_ins_cache_used * sizeof(*idc));
217 oti->oti_ins_cache = idc;
218 oti->oti_ins_cache_size = i;
221 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
224 idc->oic_lid.oii_ino = 0;
225 idc->oic_lid.oii_gen = 0;
232 * lookup mapping for the given fid in the cache, initialize a
233 * new one if not found. the initialization checks whether the
234 * object is local or remote. for local objects, OI is used to
235 * learn ino/generation. the function is used when the caller
236 * has no information about the object, e.g. at dt_insert().
238 static struct osd_idmap_cache *
239 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
240 const struct lu_fid *fid)
242 struct osd_idmap_cache *idc;
245 idc = osd_idc_find(env, osd, fid);
246 LASSERT(!IS_ERR(idc));
250 /* new mapping is needed */
251 idc = osd_idc_add(env, osd, fid);
256 rc = osd_remote_fid(env, osd, fid);
257 if (unlikely(rc < 0))
261 /* the object is local, lookup in OI */
262 /* XXX: probably cheaper to lookup in LU first? */
263 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
265 if (unlikely(rc < 0)) {
266 CERROR("can't lookup: rc = %d\n", rc);
270 /* the object is remote */
278 * lookup mapping for given FID and fill it from the given object.
279 * the object is lolcal by definition.
281 static int osd_idc_find_and_init(const struct lu_env *env,
282 struct osd_device *osd,
283 struct osd_object *obj)
285 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
286 struct osd_idmap_cache *idc;
288 idc = osd_idc_find(env, osd, fid);
289 LASSERT(!IS_ERR(idc));
291 if (obj->oo_inode == NULL)
293 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
294 LASSERT(idc->oic_lid.oii_ino == 0);
295 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
296 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
301 /* new mapping is needed */
302 idc = osd_idc_add(env, osd, fid);
306 if (obj->oo_inode != NULL) {
307 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
308 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
314 * OSD object methods.
318 * Concurrency: no concurrent access is possible that early in object
321 static struct lu_object *osd_object_alloc(const struct lu_env *env,
322 const struct lu_object_header *hdr,
325 struct osd_object *mo;
330 struct lu_object_header *h;
332 l = &mo->oo_dt.do_lu;
333 h = &osd_dev(d)->od_scrub.os_scrub.os_obj_header;
334 if (unlikely(fid_is_zero(&h->loh_fid))) {
335 /* For the OI_scrub object during OSD device init. */
336 lu_object_header_init(h);
337 lu_object_init(l, h, d);
338 lu_object_add_top(h, l);
340 dt_object_init(&mo->oo_dt, NULL, d);
342 mo->oo_dt.do_ops = &osd_obj_ops;
343 l->lo_ops = &osd_lu_obj_ops;
344 init_rwsem(&mo->oo_sem);
345 init_rwsem(&mo->oo_ext_idx_sem);
346 spin_lock_init(&mo->oo_guard);
347 INIT_LIST_HEAD(&mo->oo_xattr_list);
354 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
355 struct dentry *dentry, struct lustre_ost_attrs *loa)
359 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
360 (void *)loa, sizeof(*loa));
362 struct lustre_mdt_attrs *lma = &loa->loa_lma;
364 if (rc < sizeof(*lma))
368 lustre_loa_swab(loa, true);
369 /* Check LMA compatibility */
370 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
371 CWARN("%s: unsupported incompat LMA feature(s) %#x "
372 "for fid = "DFID", ino = %lu\n",
374 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
375 PFID(&lma->lma_self_fid), inode->i_ino);
378 } else if (rc == 0) {
386 * retrieve object from backend ext fs.
388 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
389 struct osd_inode_id *id)
392 struct inode *inode = NULL;
394 /* if we look for an inode withing a running
395 * transaction, then we risk to deadlock */
396 /* osd_dirent_check_repair() breaks this */
397 /*LASSERT(current->journal_info == NULL);*/
399 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
401 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
402 id->oii_ino, PTR_ERR(inode));
403 } else if (id->oii_gen != OSD_OII_NOGEN &&
404 inode->i_generation != id->oii_gen) {
405 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
406 "i_generation = %u\n",
407 id->oii_ino, id->oii_gen, inode->i_generation);
409 inode = ERR_PTR(-ESTALE);
410 } else if (inode->i_nlink == 0) {
411 /* due to parallel readdir and unlink,
412 * we can have dead inode here. */
413 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
415 inode = ERR_PTR(-ESTALE);
416 } else if (is_bad_inode(inode)) {
417 CWARN("%s: bad inode: ino = %u\n",
418 osd_dev2name(dev), id->oii_ino);
420 inode = ERR_PTR(-ENOENT);
421 } else if ((rc = osd_attach_jinode(inode))) {
425 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
426 if (id->oii_gen == OSD_OII_NOGEN)
427 osd_id_gen(id, inode->i_ino, inode->i_generation);
429 /* Do not update file c/mtime in ldiskfs.
430 * NB: we don't have any lock to protect this because we don't
431 * have reference on osd_object now, but contention with
432 * another lookup + attr_set can't happen in the tiny window
433 * between if (...) and set S_NOCMTIME. */
434 if (!(inode->i_flags & S_NOCMTIME))
435 inode->i_flags |= S_NOCMTIME;
440 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
441 handle_t *handle, struct dentry *child,
442 struct inode *inode, struct htree_lock *hlock)
446 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
447 if (rc == -ENOBUFS || rc == -ENOSPC) {
448 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
449 struct inode *parent = child->d_parent->d_inode;
450 struct lu_fid *fid = NULL;
452 rc2 = osd_get_lma(info, parent, child->d_parent, loa);
454 fid = &loa->loa_lma.lma_self_fid;
455 } else if (rc2 == -ENODATA) {
456 if (unlikely(parent == inode->i_sb->s_root->d_inode)) {
457 fid = &info->oti_fid3;
458 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
459 } else if (!osd->od_is_ost && osd->od_index == 0) {
460 fid = &info->oti_fid3;
461 lu_igif_build(fid, parent->i_ino,
462 parent->i_generation);
467 CWARN("%s: directory (inode: %lu, FID: "DFID") %s "
468 "maximum entry limit\n",
469 osd_name(osd), parent->i_ino, PFID(fid),
470 rc == -ENOSPC ? "has reached" : "is approaching");
472 CWARN("%s: directory (inode: %lu, FID: unknown) %s "
473 "maximum entry limit\n",
474 osd_name(osd), parent->i_ino,
475 rc == -ENOSPC ? "has reached" : "is approaching");
477 /* ignore such error now */
486 static struct inode *
487 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
488 struct osd_inode_id *id, struct lu_fid *fid)
490 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
494 inode = osd_iget(info, dev, id);
498 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
500 *fid = loa->loa_lma.lma_self_fid;
501 } else if (rc == -ENODATA) {
502 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
503 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
505 lu_igif_build(fid, inode->i_ino, inode->i_generation);
513 static struct inode *osd_iget_check(struct osd_thread_info *info,
514 struct osd_device *dev,
515 const struct lu_fid *fid,
516 struct osd_inode_id *id,
523 /* The cached OI mapping is trustable. If we cannot locate the inode
524 * via the cached OI mapping, then return the failure to the caller
525 * directly without further OI checking. */
528 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
531 if (!trusted && (rc == -ENOENT || rc == -ESTALE))
534 CDEBUG(D_INODE, "no inode for FID: "DFID", ino = %u, rc = %d\n",
535 PFID(fid), id->oii_ino, rc);
539 if (is_bad_inode(inode)) {
544 CDEBUG(D_INODE, "bad inode for FID: "DFID", ino = %u\n",
545 PFID(fid), id->oii_ino);
549 if (id->oii_gen != OSD_OII_NOGEN &&
550 inode->i_generation != id->oii_gen) {
555 CDEBUG(D_INODE, "unmatched inode for FID: "DFID", ino = %u, "
556 "oii_gen = %u, i_generation = %u\n", PFID(fid),
557 id->oii_ino, id->oii_gen, inode->i_generation);
561 if (inode->i_nlink == 0) {
566 CDEBUG(D_INODE, "stale inode for FID: "DFID", ino = %u\n",
567 PFID(fid), id->oii_ino);
571 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
575 __u32 saved_ino = id->oii_ino;
576 __u32 saved_gen = id->oii_gen;
579 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
581 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
582 /* XXX: There are four possible cases:
584 * Backup/restore caused the OI invalid.
586 * Someone unlinked the object but NOT removed
587 * the OI mapping, such as mount target device
588 * as ldiskfs, and modify something directly.
590 * Someone just removed the object between the
591 * former oi_lookup and the iget. It is normal.
592 * 4. Other failure cases.
594 * Generally, when the device is mounted, it will
595 * auto check whether the system is restored from
596 * file-level backup or not. We trust such detect
597 * to distinguish the 1st case from the 2nd case:
598 * if the OI files are consistent but may contain
599 * stale OI mappings because of case 2, if iget()
600 * returns -ENOENT or -ESTALE, then it should be
603 /* If the OI mapping was in OI file before the
604 * osd_iget_check(), but now, it is disappear,
605 * then it must be removed by race. That is a
606 * normal race case. */
609 /* It is the OI scrub updated the OI mapping by race.
610 * The new OI mapping must be valid. */
611 if (saved_ino != id->oii_ino ||
612 (saved_gen != id->oii_gen && saved_gen != OSD_OII_NOGEN)) {
621 if (dev->od_scrub.os_scrub.os_file.sf_flags &
623 /* It still can be the case 2, but we cannot
624 * distinguish it from the case 1. So return
625 * -EREMCHG to block current operation until
626 * OI scrub rebuilt the OI mappings. */
634 if (inode->i_generation == id->oii_gen)
639 if (id->oii_gen == OSD_OII_NOGEN)
640 osd_id_gen(id, inode->i_ino, inode->i_generation);
642 /* Do not update file c/mtime in ldiskfs.
643 * NB: we don't have any lock to protect this because we don't
644 * have reference on osd_object now, but contention with
645 * another lookup + attr_set can't happen in the tiny window
646 * between if (...) and set S_NOCMTIME. */
647 if (!(inode->i_flags & S_NOCMTIME))
648 inode->i_flags |= S_NOCMTIME;
665 * \retval +v: new filter_fid, does not contain self-fid
666 * \retval 0: filter_fid_old, contains self-fid
667 * \retval -v: other failure cases
669 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
670 struct dentry *dentry, struct lu_fid *fid)
672 struct filter_fid_old *ff = &info->oti_ff;
673 struct ost_id *ostid = &info->oti_ostid;
676 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
677 if (rc == sizeof(*ff)) {
679 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
680 rc = ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
682 * XXX: use 0 as the index for compatibility, the caller will
683 * handle index related issues when necessary.
686 ostid_to_fid(fid, ostid, 0);
687 } else if (rc == sizeof(struct filter_fid)) {
689 } else if (rc >= 0) {
696 static int osd_lma_self_repair(struct osd_thread_info *info,
697 struct osd_device *osd, struct inode *inode,
698 const struct lu_fid *fid, __u32 compat)
703 LASSERT(current->journal_info == NULL);
705 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
706 osd_dto_credits_noquota[DTO_XATTR_SET]);
709 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
714 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
716 CWARN("%s: cannot self repair the LMA: rc = %d\n",
718 ldiskfs_journal_stop(jh);
722 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
724 struct osd_thread_info *info = osd_oti_get(env);
725 struct osd_device *osd = osd_obj2dev(obj);
726 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
727 struct lustre_mdt_attrs *lma = &loa->loa_lma;
728 struct inode *inode = obj->oo_inode;
729 struct dentry *dentry = &info->oti_obj_dentry;
730 struct lu_fid *fid = NULL;
731 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
735 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
736 (void *)loa, sizeof(*loa));
737 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
738 fid = &lma->lma_self_fid;
739 rc = osd_get_idif(info, inode, dentry, fid);
740 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
741 /* For the given OST-object, if it has neither LMA nor
742 * FID in XATTR_NAME_FID, then the given FID (which is
743 * contained in the @obj, from client RPC for locating
744 * the OST-object) is trusted. We use it to generate
746 osd_lma_self_repair(info, osd, inode, rfid,
757 lustre_lma_swab(lma);
758 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
759 (CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT) &&
760 S_ISREG(inode->i_mode)))) {
761 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
762 "fid = "DFID", ino = %lu\n", osd_name(osd),
763 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
764 PFID(rfid), inode->i_ino);
767 fid = &lma->lma_self_fid;
768 if (lma->lma_compat & LMAC_STRIPE_INFO &&
770 obj->oo_pfid_in_lma = 1;
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;
1002 bool checked = false;
1005 LINVRNT(osd_invariant(obj));
1006 LASSERT(obj->oo_inode == NULL);
1007 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
1009 dev = osd_dev(ldev);
1010 scrub = &dev->od_scrub.os_scrub;
1011 sf = &scrub->os_file;
1012 info = osd_oti_get(env);
1014 oic = &info->oti_cache;
1016 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
1019 /* For the object is created as locking anchor, or for the object to
1020 * be created on disk. No need to osd_oi_lookup() at here because FID
1021 * shouldn't never be re-used, if it's really a duplicate FID from
1022 * unexpected reason, we should be able to detect it later by calling
1023 * do_create->osd_oi_insert(). */
1024 if (conf && conf->loc_flags & LOC_F_NEW)
1025 GOTO(out, result = 0);
1027 /* Search order: 1. per-thread cache. */
1028 if (lu_fid_eq(fid, &oic->oic_fid) && likely(oic->oic_dev == dev)) {
1034 if (!list_empty(&scrub->os_inconsistent_items)) {
1035 /* Search order: 2. OI scrub pending list. */
1036 result = osd_oii_lookup(dev, fid, id);
1041 /* The OI mapping in the OI file can be updated by the OI scrub
1042 * when we locate the inode via FID. So it may be not trustable. */
1045 /* Search order: 3. OI files. */
1046 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1047 if (result == -ENOENT) {
1048 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
1049 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1050 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
1052 GOTO(out, result = 0);
1061 obj->oo_inode = NULL;
1062 /* for later passes through checks, not true on first pass */
1063 if (!IS_ERR_OR_NULL(inode))
1066 inode = osd_iget_check(info, dev, fid, id, trusted);
1067 if (!IS_ERR(inode)) {
1068 obj->oo_inode = inode;
1076 result = PTR_ERR(inode);
1077 if (result == -ENOENT || result == -ESTALE)
1078 GOTO(out, result = 0);
1080 if (result != -EREMCHG)
1084 /* We still have chance to get the valid inode: for the
1085 * object which is referenced by remote name entry, the
1086 * object on the local MDT will be linked under the dir
1087 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1089 * We do not know whether the object for the given FID
1090 * is referenced by some remote name entry or not, and
1091 * especially for DNE II, a multiple-linked object may
1092 * have many name entries reside on many MDTs.
1094 * To simplify the operation, OSD will not distinguish
1095 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1096 * only happened for the RPC from other MDT during the
1097 * OI scrub, or for the client side RPC with FID only,
1098 * such as FID to path, or from old connected client. */
1099 if (!remote && !fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1100 rc1 = osd_lookup_in_remote_parent(info, dev, fid, id);
1104 flags |= SS_AUTO_PARTIAL;
1105 flags &= ~SS_AUTO_FULL;
1110 if (thread_is_running(&scrub->os_thread)) {
1111 if (scrub->os_partial_scan && !scrub->os_in_join)
1114 if (IS_ERR_OR_NULL(inode) || result)
1115 GOTO(out, result = -EINPROGRESS);
1118 LASSERT(obj->oo_inode == inode);
1120 osd_add_oi_cache(info, dev, id, fid);
1121 osd_oii_insert(dev, oic, true);
1125 if (dev->od_auto_scrub_interval == AS_NEVER) {
1127 GOTO(out, result = -EREMCHG);
1130 LASSERT(obj->oo_inode == inode);
1132 osd_add_oi_cache(info, dev, id, fid);
1137 rc1 = osd_scrub_start(env, dev, flags);
1138 LCONSOLE_WARN("%s: trigger OI scrub by RPC for the " DFID" with flags "
1139 "0x%x, rc = %d\n", osd_name(dev), PFID(fid), flags, rc1);
1140 if (rc1 && rc1 != -EALREADY)
1141 GOTO(out, result = -EREMCHG);
1143 if (IS_ERR_OR_NULL(inode) || result)
1144 GOTO(out, result = -EINPROGRESS);
1147 LASSERT(obj->oo_inode == inode);
1149 osd_add_oi_cache(info, dev, id, fid);
1150 osd_oii_insert(dev, oic, true);
1154 result = osd_check_lma(env, obj);
1159 LASSERTF(id->oii_ino == inode->i_ino &&
1160 id->oii_gen == inode->i_generation,
1161 "locate wrong inode for FID: "DFID", %u/%u => %ld/%u\n",
1162 PFID(fid), id->oii_ino, id->oii_gen,
1163 inode->i_ino, inode->i_generation);
1165 saved_ino = inode->i_ino;
1166 saved_gen = inode->i_generation;
1168 if (unlikely(result == -ENODATA)) {
1169 /* If the OI scrub updated the OI mapping by race, it
1170 * must be valid. Trust the inode that has no LMA EA. */
1174 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1176 /* The OI mapping is still there, the inode is still
1177 * valid. It is just becaues the inode has no LMA EA. */
1178 if (saved_ino == id->oii_ino &&
1179 saved_gen == id->oii_gen)
1182 /* It is the OI scrub updated the OI mapping by race.
1183 * The new OI mapping must be valid. */
1189 /* "result == -ENOENT" means that the OI mappinghas been
1190 * removed by race, so the inode belongs to other object.
1192 * Others error can be returned directly. */
1193 if (result == -ENOENT) {
1196 obj->oo_inode = NULL;
1201 if (result != -EREMCHG)
1206 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1207 /* "result == -ENOENT" means the cached OI mapping has been removed
1208 * from the OI file by race, above inode belongs to other object. */
1209 if (result == -ENOENT) {
1212 obj->oo_inode = NULL;
1213 GOTO(out, result = 0);
1219 if (saved_ino == id->oii_ino && saved_gen == id->oii_gen) {
1224 /* It is the OI scrub updated the OI mapping by race.
1225 * The new OI mapping must be valid. */
1232 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
1233 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
1235 result = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
1237 if (lma->lma_compat & LMAC_STRIPE_INFO &&
1239 obj->oo_pfid_in_lma = 1;
1240 } else if (result != -ENODATA) {
1245 obj->oo_compat_dot_created = 1;
1246 obj->oo_compat_dotdot_created = 1;
1248 if (S_ISDIR(inode->i_mode) &&
1249 (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1250 osd_check_lmv(info, dev, inode, oic);
1252 result = osd_attach_jinode(inode);
1257 GOTO(out, result = 0);
1259 LASSERT(!obj->oo_hl_head);
1260 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1262 GOTO(out, result = (!obj->oo_hl_head ? -ENOMEM : 0));
1265 if (result || !obj->oo_inode) {
1266 if (!IS_ERR_OR_NULL(inode))
1269 obj->oo_inode = NULL;
1271 fid_zero(&oic->oic_fid);
1274 LINVRNT(osd_invariant(obj));
1279 * Concurrency: shouldn't matter.
1281 static void osd_object_init0(struct osd_object *obj)
1283 LASSERT(obj->oo_inode != NULL);
1284 obj->oo_dt.do_body_ops = &osd_body_ops;
1285 obj->oo_dt.do_lu.lo_header->loh_attr |=
1286 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1290 * Concurrency: no concurrent access is possible that early in object
1293 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1294 const struct lu_object_conf *conf)
1296 struct osd_object *obj = osd_obj(l);
1299 LINVRNT(osd_invariant(obj));
1301 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1302 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1303 l->lo_header->loh_attr |= LOHA_EXISTS;
1307 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1308 if (unlikely(l->lo_header ==
1309 &osd_obj2dev(obj)->od_scrub.os_scrub.os_obj_header)) {
1310 /* For the OI_scrub object during OSD device init. */
1311 l->lo_header->loh_attr |= LOHA_EXISTS;
1312 obj->oo_dt.do_body_ops = &osd_body_ops;
1314 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1316 if (result == 0 && obj->oo_inode != NULL) {
1317 struct osd_thread_info *oti = osd_oti_get(env);
1318 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
1320 osd_object_init0(obj);
1321 result = osd_get_lma(oti, obj->oo_inode,
1322 &oti->oti_obj_dentry, loa);
1324 /* Convert LMAI flags to lustre LMA flags
1325 * and cache it to oo_lma_flags */
1327 lma_to_lustre_flags(loa->loa_lma.lma_incompat);
1328 } else if (result == -ENODATA) {
1333 LINVRNT(osd_invariant(obj));
1337 /* The first part of oxe_buf is xattr name, and is '\0' terminated.
1338 * The left part is for value, binary mode. */
1339 struct osd_xattr_entry {
1340 struct list_head oxe_list;
1344 struct rcu_head oxe_rcu;
1348 static int osd_oxc_get(struct osd_object *obj, const char *name,
1351 struct osd_xattr_entry *tmp;
1352 struct osd_xattr_entry *oxe = NULL;
1353 size_t namelen = strlen(name);
1358 list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1359 if (namelen == tmp->oxe_namelen &&
1360 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1367 GOTO(out, rc = -ENOENT);
1369 if (!oxe->oxe_exist)
1370 GOTO(out, rc = -ENODATA);
1373 rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1376 if (buf->lb_buf == NULL)
1379 if (buf->lb_len < rc)
1380 GOTO(out, rc = -ERANGE);
1382 memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1390 static void osd_oxc_free(struct rcu_head *head)
1392 struct osd_xattr_entry *oxe;
1394 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1395 OBD_FREE(oxe, oxe->oxe_len);
1398 static void osd_oxc_add(struct osd_object *obj, const char *name,
1399 const char *buf, int buflen)
1401 struct osd_xattr_entry *oxe;
1402 struct osd_xattr_entry *old = NULL;
1403 struct osd_xattr_entry *tmp;
1404 size_t namelen = strlen(name);
1405 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1407 OBD_ALLOC(oxe, len);
1411 INIT_LIST_HEAD(&oxe->oxe_list);
1413 oxe->oxe_namelen = namelen;
1414 memcpy(oxe->oxe_buf, name, namelen);
1416 LASSERT(buf != NULL);
1417 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1418 oxe->oxe_exist = true;
1420 oxe->oxe_exist = false;
1423 /* this should be rarely called, just remove old and add new */
1424 spin_lock(&obj->oo_guard);
1425 list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1426 if (namelen == tmp->oxe_namelen &&
1427 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1433 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1434 call_rcu(&old->oxe_rcu, osd_oxc_free);
1436 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1438 spin_unlock(&obj->oo_guard);
1441 static void osd_oxc_del(struct osd_object *obj, const char *name)
1443 struct osd_xattr_entry *oxe;
1444 size_t namelen = strlen(name);
1446 spin_lock(&obj->oo_guard);
1447 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1448 if (namelen == oxe->oxe_namelen &&
1449 strncmp(name, oxe->oxe_buf, namelen) == 0) {
1450 list_del_rcu(&oxe->oxe_list);
1451 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1455 spin_unlock(&obj->oo_guard);
1458 static void osd_oxc_fini(struct osd_object *obj)
1460 struct osd_xattr_entry *oxe, *next;
1462 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1463 list_del(&oxe->oxe_list);
1464 OBD_FREE(oxe, oxe->oxe_len);
1469 * Concurrency: no concurrent access is possible that late in object
1472 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1474 struct osd_object *obj = osd_obj(l);
1475 struct lu_object_header *h = NULL;
1477 LINVRNT(osd_invariant(obj));
1479 if (unlikely(l->lo_header ==
1480 &osd_obj2dev(obj)->od_scrub.os_scrub.os_obj_header))
1484 dt_object_fini(&obj->oo_dt);
1485 if (obj->oo_hl_head != NULL)
1486 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1489 lu_object_header_fini(h);
1493 * Concurrency: no concurrent access is possible that late in object
1496 static void osd_index_fini(struct osd_object *o)
1498 struct iam_container *bag;
1500 if (o->oo_dir != NULL) {
1501 bag = &o->oo_dir->od_container;
1502 if (o->oo_inode != NULL) {
1503 if (bag->ic_object == o->oo_inode)
1504 iam_container_fini(bag);
1506 OBD_FREE_PTR(o->oo_dir);
1512 * Concurrency: no concurrent access is possible that late in object
1513 * life-cycle (for all existing callers, that is. New callers have to provide
1514 * their own locking.)
1516 static int osd_inode_unlinked(const struct inode *inode)
1518 return inode->i_nlink == 0;
1522 OSD_TXN_OI_DELETE_CREDITS = 20,
1523 OSD_TXN_INODE_DELETE_CREDITS = 20
1530 #if OSD_THANDLE_STATS
1532 * Set time when the handle is allocated
1534 static void osd_th_alloced(struct osd_thandle *oth)
1536 oth->oth_alloced = ktime_get();
1540 * Set time when the handle started
1542 static void osd_th_started(struct osd_thandle *oth)
1544 oth->oth_started = ktime_get();
1548 * Check whether the we deal with this handle for too long.
1550 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1551 ktime_t alloced, ktime_t started,
1554 ktime_t now = ktime_get();
1556 LASSERT(dev != NULL);
1558 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1559 ktime_us_delta(started, alloced));
1560 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1561 ktime_us_delta(closed, started));
1562 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1563 ktime_us_delta(now, closed));
1565 if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1566 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1567 oth, now, alloced, started, closed);
1568 libcfs_debug_dumpstack(NULL);
1572 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1574 ktime_t __closed = ktime_get(); \
1575 ktime_t __alloced = oth->oth_alloced; \
1576 ktime_t __started = oth->oth_started; \
1579 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1582 #else /* OSD_THANDLE_STATS */
1584 #define osd_th_alloced(h) do {} while(0)
1585 #define osd_th_started(h) do {} while(0)
1586 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1588 #endif /* OSD_THANDLE_STATS */
1591 * Concurrency: doesn't access mutable data.
1593 static int osd_param_is_not_sane(const struct osd_device *dev,
1594 const struct thandle *th)
1596 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1598 return oh->ot_credits > osd_transaction_size(dev);
1602 * Concurrency: shouldn't matter.
1604 static void osd_trans_commit_cb(struct super_block *sb,
1605 struct ldiskfs_journal_cb_entry *jcb, int error)
1607 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1608 struct thandle *th = &oh->ot_super;
1609 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1610 struct dt_txn_commit_cb *dcb, *tmp;
1612 LASSERT(oh->ot_handle == NULL);
1615 CERROR("transaction @0x%p commit error: %d\n", th, error);
1617 dt_txn_hook_commit(th);
1619 /* call per-transaction callbacks if any */
1620 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1622 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1623 "commit callback entry: magic=%x name='%s'\n",
1624 dcb->dcb_magic, dcb->dcb_name);
1625 list_del_init(&dcb->dcb_linkage);
1626 dcb->dcb_func(NULL, th, dcb, error);
1629 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1636 #ifndef HAVE_SB_START_WRITE
1637 # define sb_start_write(sb) do {} while (0)
1638 # define sb_end_write(sb) do {} while (0)
1641 static struct thandle *osd_trans_create(const struct lu_env *env,
1642 struct dt_device *d)
1644 struct osd_thread_info *oti = osd_oti_get(env);
1645 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1646 struct osd_thandle *oh;
1651 CERROR("%s: someone try to start transaction under "
1652 "readonly mode, should be disabled.\n",
1653 osd_name(osd_dt_dev(d)));
1655 RETURN(ERR_PTR(-EROFS));
1658 /* on pending IO in this thread should left from prev. request */
1659 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1661 sb_start_write(osd_sb(osd_dt_dev(d)));
1663 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1665 oh->ot_quota_trans = &oti->oti_quota_trans;
1666 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1671 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1672 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1675 memset(oti->oti_declare_ops, 0,
1676 sizeof(oti->oti_declare_ops));
1677 memset(oti->oti_declare_ops_cred, 0,
1678 sizeof(oti->oti_declare_ops_cred));
1679 memset(oti->oti_declare_ops_used, 0,
1680 sizeof(oti->oti_declare_ops_used));
1682 sb_end_write(osd_sb(osd_dt_dev(d)));
1683 th = ERR_PTR(-ENOMEM);
1688 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1690 struct osd_thread_info *oti = osd_oti_get(env);
1691 struct osd_thandle *oh;
1693 oh = container_of0(th, struct osd_thandle, ot_super);
1694 LASSERT(oh != NULL);
1696 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1697 oti->oti_declare_ops[OSD_OT_CREATE],
1698 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1699 oti->oti_declare_ops_used[OSD_OT_CREATE],
1700 oti->oti_declare_ops[OSD_OT_DESTROY],
1701 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1702 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1703 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1704 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1705 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1706 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1707 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1708 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1709 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1710 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1711 oti->oti_declare_ops[OSD_OT_WRITE],
1712 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1713 oti->oti_declare_ops_used[OSD_OT_WRITE],
1714 oti->oti_declare_ops[OSD_OT_PUNCH],
1715 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1716 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1717 oti->oti_declare_ops[OSD_OT_QUOTA],
1718 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1719 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1720 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1721 oti->oti_declare_ops[OSD_OT_INSERT],
1722 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1723 oti->oti_declare_ops_used[OSD_OT_INSERT],
1724 oti->oti_declare_ops[OSD_OT_DELETE],
1725 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1726 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1727 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1728 oti->oti_declare_ops[OSD_OT_REF_ADD],
1729 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1730 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1731 oti->oti_declare_ops[OSD_OT_REF_DEL],
1732 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1733 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1737 * Concurrency: shouldn't matter.
1739 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1742 struct osd_thread_info *oti = osd_oti_get(env);
1743 struct osd_device *dev = osd_dt_dev(d);
1745 struct osd_thandle *oh;
1750 LASSERT(current->journal_info == NULL);
1752 oh = container_of0(th, struct osd_thandle, ot_super);
1753 LASSERT(oh != NULL);
1754 LASSERT(oh->ot_handle == NULL);
1756 rc = dt_txn_hook_start(env, d, th);
1760 if (unlikely(osd_param_is_not_sane(dev, th))) {
1761 static unsigned long last_printed;
1762 static int last_credits;
1764 /* don't make noise on a tiny testing systems
1765 * actual credits misuse will be caught anyway */
1766 if (last_credits != oh->ot_credits &&
1767 time_after(jiffies, last_printed +
1768 msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
1769 osd_transaction_size(dev) > 512) {
1770 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1771 oh->ot_credits, osd_transaction_size(dev));
1772 osd_trans_dump_creds(env, th);
1773 libcfs_debug_dumpstack(NULL);
1774 last_credits = oh->ot_credits;
1775 last_printed = jiffies;
1777 /* XXX Limit the credits to 'max_transaction_buffers', and
1778 * let the underlying filesystem to catch the error if
1779 * we really need so many credits.
1781 * This should be removed when we can calculate the
1782 * credits precisely. */
1783 oh->ot_credits = osd_transaction_size(dev);
1784 } else if (ldiskfs_track_declares_assert != 0) {
1785 /* reserve few credits to prevent an assertion in JBD
1786 * our debugging mechanism will be able to detected
1787 * overuse. this can help to debug single-update
1789 oh->ot_credits += 10;
1790 if (unlikely(osd_param_is_not_sane(dev, th)))
1791 oh->ot_credits = osd_transaction_size(dev);
1795 * XXX temporary stuff. Some abstraction layer should
1798 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1802 LASSERT(oti->oti_txns == 0);
1804 lu_device_get(&d->dd_lu_dev);
1805 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1816 static int osd_seq_exists(const struct lu_env *env,
1817 struct osd_device *osd, u64 seq)
1819 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1820 struct seq_server_site *ss = osd_seq_site(osd);
1824 LASSERT(ss != NULL);
1825 LASSERT(ss->ss_server_fld != NULL);
1827 rc = osd_fld_lookup(env, osd, seq, range);
1830 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
1831 osd_name(osd), seq, rc);
1835 RETURN(ss->ss_node_id == range->lsr_index);
1838 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1840 struct dt_txn_commit_cb *dcb;
1841 struct dt_txn_commit_cb *tmp;
1843 /* call per-transaction stop callbacks if any */
1844 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1846 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1847 "commit callback entry: magic=%x name='%s'\n",
1848 dcb->dcb_magic, dcb->dcb_name);
1849 list_del_init(&dcb->dcb_linkage);
1850 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1855 * Concurrency: shouldn't matter.
1857 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1860 int rc = 0, remove_agents = 0;
1861 struct osd_thandle *oh;
1862 struct osd_thread_info *oti = osd_oti_get(env);
1863 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1864 struct osd_device *osd = osd_dt_dev(th->th_dev);
1865 struct qsd_instance *qsd = osd->od_quota_slave;
1866 struct lquota_trans *qtrans;
1869 oh = container_of0(th, struct osd_thandle, ot_super);
1871 /* reset OI cache for safety */
1872 oti->oti_ins_cache_used = 0;
1874 remove_agents = oh->ot_remove_agents;
1876 qtrans = oh->ot_quota_trans;
1877 oh->ot_quota_trans = NULL;
1879 if (oh->ot_handle != NULL) {
1881 handle_t *hdl = oh->ot_handle;
1884 * add commit callback
1885 * notice we don't do this in osd_trans_start()
1886 * as underlying transaction can change during truncate
1888 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1891 LASSERT(oti->oti_txns == 1);
1894 rc = dt_txn_hook_stop(env, th);
1896 CERROR("%s: failed in transaction hook: rc = %d\n",
1899 osd_trans_stop_cb(oh, rc);
1900 /* hook functions might modify th_sync */
1901 hdl->h_sync = th->th_sync;
1903 oh->ot_handle = NULL;
1904 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
1906 CERROR("%s: failed to stop transaction: rc = %d\n",
1907 osd_name(osd), rc2);
1911 osd_trans_stop_cb(oh, th->th_result);
1915 /* inform the quota slave device that the transaction is stopping */
1916 qsd_op_end(env, qsd, qtrans);
1918 /* as we want IO to journal and data IO be concurrent, we don't block
1919 * awaiting data IO completion in osd_do_bio(), instead we wait here
1920 * once transaction is submitted to the journal. all reqular requests
1921 * don't do direct IO (except read/write), thus this wait_event becomes
1924 * IMPORTANT: we have to wait till any IO submited by the thread is
1925 * completed otherwise iobuf may be corrupted by different request
1927 wait_event(iobuf->dr_wait,
1928 atomic_read(&iobuf->dr_numreqs) == 0);
1929 osd_fini_iobuf(osd, iobuf);
1931 rc = iobuf->dr_error;
1933 if (unlikely(remove_agents != 0))
1934 osd_process_scheduled_agent_removals(env, osd);
1936 sb_end_write(osd_sb(osd));
1941 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1943 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1946 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1947 LASSERT(&dcb->dcb_func != NULL);
1948 if (dcb->dcb_flags & DCB_TRANS_STOP)
1949 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1951 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1957 * Called just before object is freed. Releases all resources except for
1958 * object itself (that is released by osd_object_free()).
1960 * Concurrency: no concurrent access is possible that late in object
1963 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1965 struct osd_object *obj = osd_obj(l);
1966 struct inode *inode = obj->oo_inode;
1968 LINVRNT(osd_invariant(obj));
1971 * If object is unlinked remove fid->ino mapping from object index.
1974 osd_index_fini(obj);
1975 if (inode != NULL) {
1976 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1977 qid_t uid = i_uid_read(inode);
1978 qid_t gid = i_gid_read(inode);
1981 obj->oo_inode = NULL;
1984 struct osd_thread_info *info = osd_oti_get(env);
1985 struct lquota_id_info *qi = &info->oti_qi;
1987 /* Release granted quota to master if necessary */
1988 qi->lqi_id.qid_uid = uid;
1989 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1991 qi->lqi_id.qid_uid = gid;
1992 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1994 qi->lqi_id.qid_uid = i_projid_read(inode);
1995 qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
2001 * Concurrency: ->loo_object_release() is called under site spin-lock.
2003 static void osd_object_release(const struct lu_env *env,
2004 struct lu_object *l)
2006 struct osd_object *o = osd_obj(l);
2007 /* nobody should be releasing a non-destroyed object with nlink=0
2008 * the API allows this, but ldiskfs doesn't like and then report
2009 * this inode as deleted */
2010 if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
2015 * Concurrency: shouldn't matter.
2017 static int osd_object_print(const struct lu_env *env, void *cookie,
2018 lu_printer_t p, const struct lu_object *l)
2020 struct osd_object *o = osd_obj(l);
2021 struct iam_descr *d;
2023 if (o->oo_dir != NULL)
2024 d = o->oo_dir->od_container.ic_descr;
2027 return (*p)(env, cookie,
2028 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
2030 o->oo_inode ? o->oo_inode->i_ino : 0UL,
2031 o->oo_inode ? o->oo_inode->i_generation : 0,
2032 d ? d->id_ops->id_name : "plain");
2036 * Concurrency: shouldn't matter.
2038 int osd_statfs(const struct lu_env *env, struct dt_device *d,
2039 struct obd_statfs *sfs)
2041 struct osd_device *osd = osd_dt_dev(d);
2042 struct super_block *sb = osd_sb(osd);
2043 struct kstatfs *ksfs;
2047 if (unlikely(osd->od_mnt == NULL))
2048 return -EINPROGRESS;
2050 /* osd_lproc.c call this without env, allocate ksfs for that case */
2051 if (unlikely(env == NULL)) {
2052 OBD_ALLOC_PTR(ksfs);
2056 ksfs = &osd_oti_get(env)->oti_ksfs;
2059 result = sb->s_op->statfs(sb->s_root, ksfs);
2063 statfs_pack(sfs, ksfs);
2064 if (unlikely(sb->s_flags & MS_RDONLY))
2065 sfs->os_state |= OS_STATE_READONLY;
2066 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
2067 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2068 sfs->os_maxbytes = sb->s_maxbytes;
2070 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2073 * Reserve some space so to avoid fragmenting the filesystem too much.
2074 * Fragmentation not only impacts performance, but can also increase
2075 * metadata overhead significantly, causing grant calculation to be
2078 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2080 CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
2081 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2082 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2083 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2085 sfs->os_blocks -= reserved;
2086 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2087 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2090 if (unlikely(env == NULL))
2096 * Estimate space needed for file creations. We assume the largest filename
2097 * which is 2^64 - 1, hence a filename of 20 chars.
2098 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2100 #ifdef __LDISKFS_DIR_REC_LEN
2101 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2103 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2107 * Concurrency: doesn't access mutable data.
2109 static void osd_conf_get(const struct lu_env *env,
2110 const struct dt_device *dev,
2111 struct dt_device_param *param)
2113 struct super_block *sb = osd_sb(osd_dt_dev(dev));
2117 * XXX should be taken from not-yet-existing fs abstraction layer.
2119 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2120 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2121 param->ddp_symlink_max = sb->s_blocksize;
2122 param->ddp_mount_type = LDD_MT_LDISKFS;
2123 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2124 param->ddp_maxbytes = sb->s_maxbytes;
2126 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2127 /* inode are statically allocated, so per-inode space consumption
2128 * is the space consumed by the directory entry */
2129 param->ddp_inodespace = PER_OBJ_USAGE;
2130 /* EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2131 * = 128MB) which is unlikely to be hit in real life. Report a smaller
2132 * maximum length to not under count the actual number of extents
2133 * needed for writing a file. */
2134 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
2135 /* worst-case extent insertion metadata overhead */
2136 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2137 param->ddp_mntopts = 0;
2138 if (test_opt(sb, XATTR_USER))
2139 param->ddp_mntopts |= MNTOPT_USERXATTR;
2140 if (test_opt(sb, POSIX_ACL))
2141 param->ddp_mntopts |= MNTOPT_ACL;
2143 /* LOD might calculate the max stripe count based on max_ea_size,
2144 * so we need take account in the overhead as well,
2145 * xattr_header + magic + xattr_entry_head */
2146 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2147 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2149 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2150 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
2151 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2155 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2159 * Concurrency: shouldn't matter.
2161 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2165 CDEBUG(D_CACHE, "%s: syncing OSD\n", osd_dt_dev(d)->od_svname);
2167 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
2169 CDEBUG(D_CACHE, "%s: synced OSD: rc = %d\n", osd_dt_dev(d)->od_svname,
2176 * Start commit for OSD device.
2178 * An implementation of dt_commit_async method for OSD device.
2179 * Asychronously starts underlayng fs sync and thereby a transaction
2182 * \param env environment
2183 * \param d dt device
2185 * \see dt_device_operations
2187 static int osd_commit_async(const struct lu_env *env,
2188 struct dt_device *d)
2190 struct super_block *s = osd_sb(osd_dt_dev(d));
2193 CDEBUG(D_HA, "%s: async commit OSD\n", osd_dt_dev(d)->od_svname);
2194 RETURN(s->s_op->sync_fs(s, 0));
2197 /* Our own copy of the set readonly functions if present, or NU if not. */
2198 static int (*priv_dev_set_rdonly)(struct block_device *bdev);
2199 static int (*priv_dev_check_rdonly)(struct block_device *bdev);
2200 /* static int (*priv_dev_clear_rdonly)(struct block_device *bdev); */
2203 * Concurrency: shouldn't matter.
2205 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2207 struct super_block *sb = osd_sb(osd_dt_dev(d));
2208 struct block_device *dev = sb->s_bdev;
2209 int rc = -EOPNOTSUPP;
2212 if (priv_dev_set_rdonly) {
2213 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
2216 CERROR("*** setting %s read-only ***\n",
2217 osd_dt_dev(d)->od_svname);
2219 if (sb->s_op->freeze_fs) {
2220 rc = sb->s_op->freeze_fs(sb);
2225 if (jdev && (jdev != dev)) {
2226 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
2228 priv_dev_set_rdonly(jdev);
2230 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
2231 priv_dev_set_rdonly(dev);
2233 if (sb->s_op->unfreeze_fs)
2234 sb->s_op->unfreeze_fs(sb);
2239 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2240 osd_dt_dev(d)->od_svname, (long)dev, rc);
2246 * Note: we do not count into QUOTA here.
2247 * If we mount with --data_journal we may need more.
2249 const int osd_dto_credits_noquota[DTO_NR] = {
2252 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2253 * SINGLEDATA_TRANS_BLOCKS(8)
2254 * XXX Note: maybe iam need more, since iam have more level than
2257 [DTO_INDEX_INSERT] = 16,
2260 * just modify a single entry, probably merge few within a block
2262 [DTO_INDEX_DELETE] = 1,
2266 [DTO_INDEX_UPDATE] = 16,
2268 * 4(inode, inode bits, groups, GDT)
2269 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2271 [DTO_OBJECT_CREATE] = 4,
2273 * 4(inode, inode bits, groups, GDT)
2274 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2276 [DTO_OBJECT_DELETE] = 4,
2278 * Attr set credits (inode)
2280 [DTO_ATTR_SET_BASE] = 1,
2282 * Xattr set. The same as xattr of EXT3.
2283 * DATA_TRANS_BLOCKS(14)
2284 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2285 * are also counted in. Do not know why?
2287 [DTO_XATTR_SET] = 14,
2289 * credits for inode change during write.
2291 [DTO_WRITE_BASE] = 3,
2293 * credits for single block write.
2295 [DTO_WRITE_BLOCK] = 14,
2297 * Attr set credits for chown.
2298 * This is extra credits for setattr, and it is null without quota
2300 [DTO_ATTR_SET_CHOWN] = 0
2303 static const struct dt_device_operations osd_dt_ops = {
2304 .dt_root_get = osd_root_get,
2305 .dt_statfs = osd_statfs,
2306 .dt_trans_create = osd_trans_create,
2307 .dt_trans_start = osd_trans_start,
2308 .dt_trans_stop = osd_trans_stop,
2309 .dt_trans_cb_add = osd_trans_cb_add,
2310 .dt_conf_get = osd_conf_get,
2311 .dt_sync = osd_sync,
2313 .dt_commit_async = osd_commit_async,
2316 static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
2319 struct osd_object *obj = osd_dt_obj(dt);
2320 struct osd_thread_info *oti = osd_oti_get(env);
2322 LINVRNT(osd_invariant(obj));
2324 LASSERT(obj->oo_owner != env);
2325 down_read_nested(&obj->oo_sem, role);
2327 LASSERT(obj->oo_owner == NULL);
2331 static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
2334 struct osd_object *obj = osd_dt_obj(dt);
2335 struct osd_thread_info *oti = osd_oti_get(env);
2337 LINVRNT(osd_invariant(obj));
2339 LASSERT(obj->oo_owner != env);
2340 down_write_nested(&obj->oo_sem, role);
2342 LASSERT(obj->oo_owner == NULL);
2343 obj->oo_owner = env;
2347 static void osd_read_unlock(const struct lu_env *env, struct dt_object *dt)
2349 struct osd_object *obj = osd_dt_obj(dt);
2350 struct osd_thread_info *oti = osd_oti_get(env);
2352 LINVRNT(osd_invariant(obj));
2354 LASSERT(oti->oti_r_locks > 0);
2356 up_read(&obj->oo_sem);
2359 static void osd_write_unlock(const struct lu_env *env, struct dt_object *dt)
2361 struct osd_object *obj = osd_dt_obj(dt);
2362 struct osd_thread_info *oti = osd_oti_get(env);
2364 LINVRNT(osd_invariant(obj));
2366 LASSERT(obj->oo_owner == env);
2367 LASSERT(oti->oti_w_locks > 0);
2369 obj->oo_owner = NULL;
2370 up_write(&obj->oo_sem);
2373 static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
2375 struct osd_object *obj = osd_dt_obj(dt);
2377 LINVRNT(osd_invariant(obj));
2379 return obj->oo_owner == env;
2382 static struct timespec *osd_inode_time(const struct lu_env *env,
2383 struct inode *inode, __u64 seconds)
2385 struct osd_thread_info *oti = osd_oti_get(env);
2386 struct timespec *t = &oti->oti_time;
2388 t->tv_sec = seconds;
2390 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
2394 static void osd_inode_getattr(const struct lu_env *env,
2395 struct inode *inode, struct lu_attr *attr)
2397 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2398 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2399 LA_PROJID | LA_FLAGS | LA_NLINK | LA_RDEV |
2400 LA_BLKSIZE | LA_TYPE;
2402 attr->la_atime = LTIME_S(inode->i_atime);
2403 attr->la_mtime = LTIME_S(inode->i_mtime);
2404 attr->la_ctime = LTIME_S(inode->i_ctime);
2405 attr->la_mode = inode->i_mode;
2406 attr->la_size = i_size_read(inode);
2407 attr->la_blocks = inode->i_blocks;
2408 attr->la_uid = i_uid_read(inode);
2409 attr->la_gid = i_gid_read(inode);
2410 attr->la_projid = i_projid_read(inode);
2411 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2412 attr->la_nlink = inode->i_nlink;
2413 attr->la_rdev = inode->i_rdev;
2414 attr->la_blksize = 1 << inode->i_blkbits;
2415 attr->la_blkbits = inode->i_blkbits;
2417 * Ext4 did not transfer inherit flags from raw inode
2418 * to inode flags, and ext4 internally test raw inode
2419 * @i_flags directly. Instead of patching ext4, we do it here.
2421 if (LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL)
2422 attr->la_flags |= LUSTRE_PROJINHERIT_FL;
2425 static int osd_attr_get(const struct lu_env *env,
2426 struct dt_object *dt,
2427 struct lu_attr *attr)
2429 struct osd_object *obj = osd_dt_obj(dt);
2431 if (unlikely(!dt_object_exists(dt)))
2433 if (unlikely(obj->oo_destroyed))
2436 LASSERT(!dt_object_remote(dt));
2437 LINVRNT(osd_invariant(obj));
2439 spin_lock(&obj->oo_guard);
2440 osd_inode_getattr(env, obj->oo_inode, attr);
2441 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
2442 attr->la_flags |= LUSTRE_ORPHAN_FL;
2443 spin_unlock(&obj->oo_guard);
2448 static int osd_declare_attr_qid(const struct lu_env *env,
2449 struct osd_object *obj,
2450 struct osd_thandle *oh, long long bspace,
2451 qid_t old_id, qid_t new_id, bool enforce,
2455 struct osd_thread_info *info = osd_oti_get(env);
2456 struct lquota_id_info *qi = &info->oti_qi;
2458 qi->lqi_type = type;
2459 /* inode accounting */
2460 qi->lqi_is_blk = false;
2462 /* one more inode for the new id ... */
2463 qi->lqi_id.qid_uid = new_id;
2465 /* Reserve credits for the new id */
2466 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2467 if (rc == -EDQUOT || rc == -EINPROGRESS)
2472 /* and one less inode for the current id */
2473 qi->lqi_id.qid_uid = old_id;
2475 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2476 if (rc == -EDQUOT || rc == -EINPROGRESS)
2481 /* block accounting */
2482 qi->lqi_is_blk = true;
2484 /* more blocks for the new id ... */
2485 qi->lqi_id.qid_uid = new_id;
2486 qi->lqi_space = bspace;
2488 * Credits for the new uid has been reserved, re-use "obj"
2489 * to save credit reservation.
2491 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2492 if (rc == -EDQUOT || rc == -EINPROGRESS)
2497 /* and finally less blocks for the current uid */
2498 qi->lqi_id.qid_uid = old_id;
2499 qi->lqi_space = -bspace;
2500 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2501 if (rc == -EDQUOT || rc == -EINPROGRESS)
2507 static int osd_declare_attr_set(const struct lu_env *env,
2508 struct dt_object *dt,
2509 const struct lu_attr *attr,
2510 struct thandle *handle)
2512 struct osd_thandle *oh;
2513 struct osd_object *obj;
2521 LASSERT(dt != NULL);
2522 LASSERT(handle != NULL);
2524 obj = osd_dt_obj(dt);
2525 LASSERT(osd_invariant(obj));
2527 oh = container_of0(handle, struct osd_thandle, ot_super);
2528 LASSERT(oh->ot_handle == NULL);
2530 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2531 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2533 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2534 osd_dto_credits_noquota[DTO_XATTR_SET]);
2536 if (attr == NULL || obj->oo_inode == NULL)
2539 bspace = obj->oo_inode->i_blocks;
2540 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2541 bspace = toqb(bspace);
2543 /* Changing ownership is always preformed by super user, it should not
2546 * We still need to call the osd_declare_qid() to calculate the journal
2547 * credits for updating quota accounting files and to trigger quota
2548 * space adjustment once the operation is completed.*/
2549 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2551 uid = i_uid_read(obj->oo_inode);
2552 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2553 rc = osd_declare_attr_qid(env, obj, oh, bspace, uid,
2554 attr->la_uid, enforce, USRQUOTA);
2558 gid = i_gid_read(obj->oo_inode);
2559 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2560 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2561 i_gid_read(obj->oo_inode),
2562 attr->la_gid, enforce, GRPQUOTA);
2567 #ifdef HAVE_PROJECT_QUOTA
2568 if (attr->la_valid & LA_PROJID) {
2569 __u32 projid = i_projid_read(obj->oo_inode);
2570 enforce = (attr->la_valid & LA_PROJID) &&
2571 (attr->la_projid != projid);
2572 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2573 (qid_t)projid, (qid_t)attr->la_projid,
2582 static int osd_inode_setattr(const struct lu_env *env,
2583 struct inode *inode, const struct lu_attr *attr)
2585 __u64 bits = attr->la_valid;
2587 /* Only allow set size for regular file */
2588 if (!S_ISREG(inode->i_mode))
2589 bits &= ~(LA_SIZE | LA_BLOCKS);
2594 if (bits & LA_ATIME)
2595 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2596 if (bits & LA_CTIME)
2597 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2598 if (bits & LA_MTIME)
2599 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2600 if (bits & LA_SIZE) {
2601 spin_lock(&inode->i_lock);
2602 LDISKFS_I(inode)->i_disksize = attr->la_size;
2603 i_size_write(inode, attr->la_size);
2604 spin_unlock(&inode->i_lock);
2607 /* OSD should not change "i_blocks" which is used by quota.
2608 * "i_blocks" should be changed by ldiskfs only. */
2610 inode->i_mode = (inode->i_mode & S_IFMT) |
2611 (attr->la_mode & ~S_IFMT);
2613 i_uid_write(inode, attr->la_uid);
2615 i_gid_write(inode, attr->la_gid);
2616 if (bits & LA_PROJID)
2617 i_projid_write(inode, attr->la_projid);
2618 if (bits & LA_NLINK)
2619 set_nlink(inode, attr->la_nlink);
2621 inode->i_rdev = attr->la_rdev;
2623 if (bits & LA_FLAGS) {
2624 /* always keep S_NOCMTIME */
2625 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2628 * Ext4 did not transfer inherit flags from
2629 * @inode->i_flags to raw inode i_flags when writing
2630 * flags, we do it explictly here.
2632 if (attr->la_flags & LUSTRE_PROJINHERIT_FL)
2633 LDISKFS_I(inode)->i_flags |= LUSTRE_PROJINHERIT_FL;
2635 LDISKFS_I(inode)->i_flags &= ~LUSTRE_PROJINHERIT_FL;
2640 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2644 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2645 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2648 ll_vfs_dq_init(inode);
2650 if (attr->la_valid & LA_UID)
2651 iattr.ia_valid |= ATTR_UID;
2652 if (attr->la_valid & LA_GID)
2653 iattr.ia_valid |= ATTR_GID;
2654 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2655 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2657 rc = ll_vfs_dq_transfer(inode, &iattr);
2659 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2660 "enforcement enabled on the ldiskfs "
2661 "filesystem?\n", inode->i_sb->s_id, rc);
2666 /* Handle project id transfer here properly */
2667 if (attr->la_valid & LA_PROJID &&
2668 attr->la_projid != i_projid_read(inode)) {
2669 #ifdef HAVE_PROJECT_QUOTA
2670 rc = ldiskfs_transfer_project(inode, attr->la_projid);
2675 CERROR("%s: quota transfer failed: rc = %d. Is project "
2676 "enforcement enabled on the ldiskfs "
2677 "filesystem?\n", inode->i_sb->s_id, rc);
2684 static int osd_attr_set(const struct lu_env *env,
2685 struct dt_object *dt,
2686 const struct lu_attr *attr,
2687 struct thandle *handle)
2689 struct osd_object *obj = osd_dt_obj(dt);
2690 struct inode *inode;
2693 if (!dt_object_exists(dt))
2696 LASSERT(handle != NULL);
2697 LASSERT(!dt_object_remote(dt));
2698 LASSERT(osd_invariant(obj));
2700 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2702 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) &&
2703 !osd_obj2dev(obj)->od_is_ost) {
2704 struct osd_thread_info *oti = osd_oti_get(env);
2705 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2706 struct lu_fid *fid1 = &oti->oti_fid;
2707 struct osd_inode_id *id = &oti->oti_id;
2708 struct iam_path_descr *ipd;
2709 struct iam_container *bag;
2710 struct osd_thandle *oh;
2713 fid_cpu_to_be(fid1, fid0);
2714 memset(id, 1, sizeof(*id));
2715 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2716 fid0)->oi_dir.od_container;
2717 ipd = osd_idx_ipd_get(env, bag);
2718 if (unlikely(ipd == NULL))
2721 oh = container_of0(handle, struct osd_thandle, ot_super);
2722 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2723 (const struct iam_rec *)id, ipd);
2724 osd_ipd_put(env, bag, ipd);
2725 return(rc > 0 ? 0 : rc);
2728 inode = obj->oo_inode;
2730 rc = osd_quota_transfer(inode, attr);
2734 spin_lock(&obj->oo_guard);
2735 rc = osd_inode_setattr(env, inode, attr);
2736 spin_unlock(&obj->oo_guard);
2740 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2742 if (!(attr->la_valid & LA_FLAGS))
2745 /* Let's check if there are extra flags need to be set into LMA */
2746 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2747 struct osd_thread_info *info = osd_oti_get(env);
2748 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
2750 LASSERT(!obj->oo_pfid_in_lma);
2752 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
2753 &info->oti_ost_attrs);
2757 lma->lma_incompat |=
2758 lustre_to_lma_flags(attr->la_flags);
2759 lustre_lma_swab(lma);
2760 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2761 lma, sizeof(*lma), XATTR_REPLACE);
2763 struct osd_device *osd = osd_obj2dev(obj);
2765 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2766 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2767 lma->lma_incompat, rc);
2770 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2772 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2775 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2780 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2781 struct osd_object *obj,
2782 const char *name, const int namelen)
2784 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2787 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2788 umode_t mode, struct dt_allocation_hint *hint,
2792 struct osd_device *osd = osd_obj2dev(obj);
2793 struct osd_thandle *oth;
2794 struct dt_object *parent = NULL;
2795 struct inode *inode;
2797 LINVRNT(osd_invariant(obj));
2798 LASSERT(obj->oo_inode == NULL);
2799 LASSERT(obj->oo_hl_head == NULL);
2801 if (S_ISDIR(mode) && ldiskfs_pdo) {
2802 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2803 if (obj->oo_hl_head == NULL)
2807 oth = container_of(th, struct osd_thandle, ot_super);
2808 LASSERT(oth->ot_handle->h_transaction != NULL);
2810 if (hint != NULL && hint->dah_parent != NULL &&
2811 !dt_object_remote(hint->dah_parent))
2812 parent = hint->dah_parent;
2814 inode = ldiskfs_create_inode(oth->ot_handle,
2815 parent ? osd_dt_obj(parent)->oo_inode :
2816 osd_sb(osd)->s_root->d_inode,
2818 if (!IS_ERR(inode)) {
2819 /* Do not update file c/mtime in ldiskfs. */
2820 inode->i_flags |= S_NOCMTIME;
2822 /* For new created object, it must be consistent,
2823 * and it is unnecessary to scrub against it. */
2824 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2826 obj->oo_inode = inode;
2829 if (obj->oo_hl_head != NULL) {
2830 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2831 obj->oo_hl_head = NULL;
2833 result = PTR_ERR(inode);
2835 LINVRNT(osd_invariant(obj));
2843 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2844 struct lu_attr *attr,
2845 struct dt_allocation_hint *hint,
2846 struct dt_object_format *dof,
2850 struct osd_thandle *oth;
2851 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
2853 LASSERT(S_ISDIR(attr->la_mode));
2855 oth = container_of(th, struct osd_thandle, ot_super);
2856 LASSERT(oth->ot_handle->h_transaction != NULL);
2857 result = osd_mkfile(info, obj, mode, hint, th);
2862 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2863 struct lu_attr *attr,
2864 struct dt_allocation_hint *hint,
2865 struct dt_object_format *dof,
2869 struct osd_thandle *oth;
2870 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2872 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2874 LASSERT(S_ISREG(attr->la_mode));
2876 oth = container_of(th, struct osd_thandle, ot_super);
2877 LASSERT(oth->ot_handle->h_transaction != NULL);
2879 result = osd_mkfile(info, obj, mode, hint, th);
2881 LASSERT(obj->oo_inode != NULL);
2882 if (feat->dif_flags & DT_IND_VARKEY)
2883 result = iam_lvar_create(obj->oo_inode,
2884 feat->dif_keysize_max,
2886 feat->dif_recsize_max,
2889 result = iam_lfix_create(obj->oo_inode,
2890 feat->dif_keysize_max,
2892 feat->dif_recsize_max,
2899 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2900 struct lu_attr *attr,
2901 struct dt_allocation_hint *hint,
2902 struct dt_object_format *dof,
2905 LASSERT(S_ISREG(attr->la_mode));
2906 return osd_mkfile(info, obj, (attr->la_mode &
2907 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2910 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2911 struct lu_attr *attr,
2912 struct dt_allocation_hint *hint,
2913 struct dt_object_format *dof,
2916 LASSERT(S_ISLNK(attr->la_mode));
2917 return osd_mkfile(info, obj, (attr->la_mode &
2918 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2921 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2922 struct lu_attr *attr,
2923 struct dt_allocation_hint *hint,
2924 struct dt_object_format *dof,
2927 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2930 LINVRNT(osd_invariant(obj));
2931 LASSERT(obj->oo_inode == NULL);
2932 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2933 S_ISFIFO(mode) || S_ISSOCK(mode));
2935 result = osd_mkfile(info, obj, mode, hint, th);
2937 LASSERT(obj->oo_inode != NULL);
2939 * This inode should be marked dirty for i_rdev. Currently
2940 * that is done in the osd_attr_init().
2942 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2945 LINVRNT(osd_invariant(obj));
2949 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2951 struct dt_allocation_hint *hint,
2952 struct dt_object_format *dof,
2955 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2957 osd_obj_type_f result;
2973 result = osd_mk_index;
2984 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2985 struct dt_object *parent, struct dt_object *child,
2990 ah->dah_parent = parent;
2991 ah->dah_mode = child_mode;
2993 if (parent != NULL && !dt_object_remote(parent)) {
2994 /* will help to find FID->ino at dt_insert("..") */
2995 struct osd_object *pobj = osd_dt_obj(parent);
2996 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
3000 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
3001 struct lu_attr *attr, struct dt_object_format *dof)
3003 struct inode *inode = obj->oo_inode;
3004 __u64 valid = attr->la_valid;
3007 attr->la_valid &= ~(LA_TYPE | LA_MODE);
3009 if (dof->dof_type != DFT_NODE)
3010 attr->la_valid &= ~LA_RDEV;
3011 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
3012 attr->la_valid &= ~LA_ATIME;
3013 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
3014 attr->la_valid &= ~LA_CTIME;
3015 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
3016 attr->la_valid &= ~LA_MTIME;
3018 result = osd_quota_transfer(inode, attr);
3022 if (attr->la_valid != 0) {
3023 result = osd_inode_setattr(info->oti_env, inode, attr);
3025 * The osd_inode_setattr() should always succeed here. The
3026 * only error that could be returned is EDQUOT when we are
3027 * trying to change the UID or GID of the inode. However, this
3028 * should not happen since quota enforcement is no longer
3029 * enabled on ldiskfs (lquota takes care of it).
3031 LASSERTF(result == 0, "%d\n", result);
3032 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3035 attr->la_valid = valid;
3039 * Helper function for osd_create()
3041 * \retval 0, on success
3043 static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
3044 struct lu_attr *attr, struct dt_allocation_hint *hint,
3045 struct dt_object_format *dof, struct thandle *th)
3050 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
3052 /* we drop umask so that permissions we pass are not affected */
3053 umask = current->fs->umask;
3054 current->fs->umask = 0;
3056 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
3058 if (likely(obj->oo_inode != NULL)) {
3059 LASSERT(obj->oo_inode->i_state & I_NEW);
3061 /* Unlock the inode before attr initialization to avoid
3062 * unnecessary dqget operations. LU-6378 */
3063 unlock_new_inode(obj->oo_inode);
3066 if (likely(result == 0)) {
3067 osd_attr_init(info, obj, attr, dof);
3068 osd_object_init0(obj);
3071 /* restore previous umask value */
3072 current->fs->umask = umask;
3074 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
3080 * Helper function for osd_create()
3082 * \retval 0, on success
3084 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
3085 const struct lu_fid *fid, struct thandle *th)
3087 struct osd_thread_info *info = osd_oti_get(env);
3088 struct osd_inode_id *id = &info->oti_id;
3089 struct osd_device *osd = osd_obj2dev(obj);
3090 struct osd_thandle *oh;
3093 LASSERT(obj->oo_inode != NULL);
3095 oh = container_of0(th, struct osd_thandle, ot_super);
3096 LASSERT(oh->ot_handle);
3097 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3099 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3100 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3101 OI_CHECK_FLD, NULL);
3102 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3107 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3108 u64 seq, struct lu_seq_range *range)
3110 struct seq_server_site *ss = osd_seq_site(osd);
3112 if (fid_seq_is_idif(seq)) {
3113 fld_range_set_ost(range);
3114 range->lsr_index = idif_ost_idx(seq);
3118 if (!fid_seq_in_fldb(seq)) {
3119 fld_range_set_mdt(range);
3121 /* FIXME: If ss is NULL, it suppose not get lsr_index
3123 range->lsr_index = ss->ss_node_id;
3127 LASSERT(ss != NULL);
3128 fld_range_set_any(range);
3129 /* OSD will only do local fld lookup */
3130 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3133 static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
3134 struct lu_attr *attr,
3135 struct dt_allocation_hint *hint,
3136 struct dt_object_format *dof,
3137 struct thandle *handle)
3139 struct osd_thandle *oh;
3143 LASSERT(handle != NULL);
3145 oh = container_of0(handle, struct osd_thandle, ot_super);
3146 LASSERT(oh->ot_handle == NULL);
3148 /* EA object consumes more credits than regular object: osd_mk_index
3149 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3150 * leaf_node, could involves the block, block bitmap, groups, GDT
3151 * change for each block, so add 4 * 2 credits in that case. */
3152 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3153 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3154 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3155 /* Reuse idle OI block may cause additional one OI block
3157 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3158 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3163 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid,
3164 attr->la_projid, 1, oh, osd_dt_obj(dt),
3165 NULL, OSD_QID_INODE);
3169 /* will help to find FID->ino mapping at dt_insert() */
3170 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3177 * Called to destroy on-disk representation of the object
3179 * Concurrency: must be locked
3181 static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
3184 struct osd_object *obj = osd_dt_obj(dt);
3185 struct inode *inode = obj->oo_inode;
3186 struct osd_thandle *oh;
3193 oh = container_of0(th, struct osd_thandle, ot_super);
3194 LASSERT(oh->ot_handle == NULL);
3196 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3197 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3198 /* Recycle idle OI leaf may cause additional three OI blocks
3200 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3201 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3202 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3203 /* one less inode */
3204 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3205 i_projid_read(inode), -1, oh, obj, NULL,
3209 /* data to be truncated */
3210 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3211 i_projid_read(inode), 0, oh, obj, NULL,
3216 /* will help to find FID->ino when this object is being
3217 * added to PENDING/ */
3218 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3223 static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
3226 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3227 struct osd_object *obj = osd_dt_obj(dt);
3228 struct inode *inode = obj->oo_inode;
3229 struct osd_device *osd = osd_obj2dev(obj);
3230 struct osd_thandle *oh;
3234 oh = container_of0(th, struct osd_thandle, ot_super);
3235 LASSERT(oh->ot_handle);
3237 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3239 if (unlikely(fid_is_acct(fid)))
3242 if (S_ISDIR(inode->i_mode)) {
3243 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
3244 inode->i_nlink == 2);
3245 /* it will check/delete the inode from remote parent,
3246 * how to optimize it? unlink performance impaction XXX */
3247 result = osd_delete_from_remote_parent(env, osd, obj, oh);
3249 CERROR("%s: delete inode "DFID": rc = %d\n",
3250 osd_name(osd), PFID(fid), result);
3252 spin_lock(&obj->oo_guard);
3254 spin_unlock(&obj->oo_guard);
3255 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3258 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3260 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3262 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3263 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3264 oh->ot_handle, OI_CHECK_FLD);
3266 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3267 /* XXX: add to ext3 orphan list */
3268 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3270 /* not needed in the cache anymore */
3271 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3272 obj->oo_destroyed = 1;
3278 * Put the fid into lustre_mdt_attrs, and then place the structure
3279 * inode's ea. This fid should not be altered during the life time
3282 * \retval +ve, on success
3283 * \retval -ve, on error
3285 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3287 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3288 const struct lu_fid *fid, __u32 compat, __u32 incompat)
3290 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3291 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3295 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3298 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3301 lustre_loa_init(loa, fid, compat, incompat);
3302 lustre_loa_swab(loa, false);
3304 /* For the OST device with 256 bytes inode size by default,
3305 * the PFID EA will be stored together with LMA EA to avoid
3306 * performance trouble. Otherwise the PFID EA can be stored
3307 * independently. LU-8998 */
3308 if ((compat & LMAC_FID_ON_OST) &&
3309 LDISKFS_INODE_SIZE(inode->i_sb) <= 256)
3310 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3311 sizeof(*loa), XATTR_CREATE);
3313 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3314 sizeof(*lma), XATTR_CREATE);
3315 /* LMA may already exist, but we need to check that all the
3316 * desired compat/incompat flags have been added. */
3317 if (unlikely(rc == -EEXIST)) {
3318 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3319 XATTR_NAME_LMA, (void *)loa, sizeof(*loa));
3323 if (rc < sizeof(*lma))
3326 lustre_loa_swab(loa, true);
3327 if (lu_fid_eq(fid, &lma->lma_self_fid) &&
3328 ((compat == 0 && incompat == 0) ||
3329 (!(~lma->lma_compat & compat) &&
3330 !(~lma->lma_incompat & incompat))))
3333 lma->lma_self_fid = *fid;
3334 lma->lma_compat |= compat;
3335 lma->lma_incompat |= incompat;
3336 if (rc == sizeof(*lma)) {
3337 lustre_lma_swab(lma);
3338 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3339 sizeof(*lma), XATTR_REPLACE);
3341 lustre_loa_swab(loa, false);
3342 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3343 sizeof(*loa), XATTR_REPLACE);
3351 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3352 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3353 * To have compatilibility with 1.8 ldiskfs driver we need to have
3354 * magic number at start of fid data.
3355 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3358 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3359 const struct lu_fid *fid)
3361 if (!fid_is_namespace_visible(fid) ||
3362 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3363 param->edp_magic = 0;
3367 param->edp_magic = LDISKFS_LUFID_MAGIC;
3368 param->edp_len = sizeof(struct lu_fid) + 1;
3369 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3373 * Try to read the fid from inode ea into dt_rec.
3375 * \param fid object fid.
3377 * \retval 0 on success
3379 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3380 __u32 ino, struct lu_fid *fid,
3381 struct osd_inode_id *id)
3383 struct osd_thread_info *info = osd_oti_get(env);
3384 struct inode *inode;
3387 osd_id_gen(id, ino, OSD_OII_NOGEN);
3388 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3390 RETURN(PTR_ERR(inode));
3396 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3398 struct inode *parent_dir,
3399 const struct lu_fid *dot_fid,
3400 const struct lu_fid *dot_dot_fid,
3401 struct osd_thandle *oth)
3403 struct ldiskfs_dentry_param *dot_ldp;
3404 struct ldiskfs_dentry_param *dot_dot_ldp;
3405 __u32 saved_nlink = dir->i_nlink;
3408 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3409 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3411 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3412 dot_ldp->edp_magic = 0;
3414 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3415 dir, dot_ldp, dot_dot_ldp);
3416 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3417 * the subseqent ref_add() will increase the dir->i_nlink
3418 * as 3. That is incorrect for new created directory.
3420 * It looks like hack, because we want to make the OSD API
3421 * to be order-independent for new created directory object
3422 * between dt_insert(..) and ref_add() operations.
3424 * Here, we only restore the in-RAM dir-inode's nlink attr,
3425 * becuase if the nlink attr is not 2, then there will be
3426 * ref_add() called following the dt_insert(..), such call
3427 * will make both the in-RAM and on-disk dir-inode's nlink
3428 * attr to be set as 2. LU-7447 */
3429 set_nlink(dir, saved_nlink);
3434 * Create an local agent inode for remote entry
3436 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3437 struct osd_device *osd,
3438 struct osd_object *pobj,
3439 const struct lu_fid *fid,
3443 struct osd_thread_info *info = osd_oti_get(env);
3444 struct inode *local;
3445 struct osd_thandle *oh;
3450 oh = container_of(th, struct osd_thandle, ot_super);
3451 LASSERT(oh->ot_handle->h_transaction != NULL);
3453 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
3454 if (IS_ERR(local)) {
3455 CERROR("%s: create local error %d\n", osd_name(osd),
3456 (int)PTR_ERR(local));
3460 /* restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
3461 * correct gid on remote file, not agent here */
3462 local->i_gid = current_fsgid();
3463 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3464 unlock_new_inode(local);
3466 /* Agent inode should not have project ID */
3467 #ifdef HAVE_PROJECT_QUOTA
3468 if (LDISKFS_I(pobj->oo_inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
3469 i_projid_read(pobj->oo_inode) != 0) {
3470 rc = ldiskfs_transfer_project(local, 0);
3472 CERROR("%s: quota transfer failed: rc = %d. Is project "
3473 "quota enforcement enabled on the ldiskfs "
3474 "filesystem?\n", local->i_sb->s_id, rc);
3475 RETURN(ERR_PTR(rc));
3479 /* Set special LMA flag for local agent inode */
3480 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3482 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3483 osd_name(osd), PFID(fid), rc);
3484 RETURN(ERR_PTR(rc));
3490 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3491 lu_object_fid(&pobj->oo_dt.do_lu),
3494 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3495 osd_name(osd), PFID(fid), rc);
3496 RETURN(ERR_PTR(rc));
3503 * when direntry is deleted, we have to take care of possible agent inode
3504 * referenced by that. unfortunately we can't do this at that point:
3505 * iget() within a running transaction leads to deadlock and we better do
3506 * not call that every delete declaration to save performance. so we put
3507 * a potention agent inode on a list and process that once the transaction
3508 * is over. Notice it's not any worse in terms of real orphans as regular
3509 * object destroy doesn't put inodes on the on-disk orphan list. this should
3510 * be addressed separately
3512 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3513 struct osd_thandle *oh,
3516 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3517 struct osd_obj_orphan *oor;
3524 oor->oor_env = (struct lu_env *)env;
3525 spin_lock(&osd->od_osfs_lock);
3526 list_add(&oor->oor_list, &osd->od_orphan_list);
3527 spin_unlock(&osd->od_osfs_lock);
3529 oh->ot_remove_agents = 1;
3535 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3536 struct osd_device *osd)
3538 struct osd_thread_info *info = osd_oti_get(env);
3539 struct osd_obj_orphan *oor, *tmp;
3540 struct osd_inode_id id;
3541 struct list_head list;
3542 struct inode *inode;
3547 INIT_LIST_HEAD(&list);
3549 spin_lock(&osd->od_osfs_lock);
3550 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3551 if (oor->oor_env == env) {
3552 list_del(&oor->oor_list);
3553 list_add(&oor->oor_list, &list);
3556 spin_unlock(&osd->od_osfs_lock);
3558 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3562 list_del(&oor->oor_list);
3565 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3566 inode = osd_iget_fid(info, osd, &id, &fid);
3570 if (!osd_remote_fid(env, osd, &fid)) {
3575 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3577 mark_inode_dirty(inode);
3578 ldiskfs_journal_stop(jh);
3586 * OSD layer object create function for OST objects (b=11826).
3588 * The FID is inserted into inode xattr here.
3590 * \retval 0, on success
3591 * \retval -ve, on error
3593 static int osd_create(const struct lu_env *env, struct dt_object *dt,
3594 struct lu_attr *attr, struct dt_allocation_hint *hint,
3595 struct dt_object_format *dof, struct thandle *th)
3597 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3598 struct osd_object *obj = osd_dt_obj(dt);
3599 struct osd_thread_info *info = osd_oti_get(env);
3600 int result, on_ost = 0;
3604 if (dt_object_exists(dt))
3607 LINVRNT(osd_invariant(obj));
3608 LASSERT(!dt_object_remote(dt));
3609 LASSERT(osd_is_write_locked(env, obj));
3610 LASSERT(th != NULL);
3612 if (unlikely(fid_is_acct(fid)))
3613 /* Quota files can't be created from the kernel any more,
3614 * 'tune2fs -O quota' will take care of creating them */
3617 result = __osd_create(info, obj, attr, hint, dof, th);
3619 if (fid_is_idif(fid) &&
3620 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3621 struct lu_fid *tfid = &info->oti_fid;
3622 struct ost_id *oi = &info->oti_ostid;
3624 fid_to_ostid(fid, oi);
3625 ostid_to_fid(tfid, oi, 0);
3627 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3628 LMAC_FID_ON_OST, 0);
3630 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3632 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3633 on_ost ? LMAC_FID_ON_OST : 0,
3636 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3637 obj->oo_dt.do_body_ops = &osd_body_ops;
3641 result = __osd_oi_insert(env, obj, fid, th);
3643 /* a small optimization - dt_insert() isn't usually applied
3644 * to OST objects, so we don't need to cache OI mapping for
3646 if (result == 0 && on_ost == 0) {
3647 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3648 result = osd_idc_find_and_init(env, osd, obj);
3649 LASSERT(result == 0);
3652 LASSERT(ergo(result == 0,
3653 dt_object_exists(dt) && !dt_object_remote(dt)));
3654 LINVRNT(osd_invariant(obj));
3658 static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
3659 struct thandle *handle)
3661 struct osd_thandle *oh;
3663 /* it's possible that object doesn't exist yet */
3664 LASSERT(handle != NULL);
3666 oh = container_of0(handle, struct osd_thandle, ot_super);
3667 LASSERT(oh->ot_handle == NULL);
3669 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3670 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3676 * Concurrency: @dt is write locked.
3678 static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
3681 struct osd_object *obj = osd_dt_obj(dt);
3682 struct inode *inode = obj->oo_inode;
3683 struct osd_thandle *oh;
3686 if (!dt_object_exists(dt) || obj->oo_destroyed)
3689 LINVRNT(osd_invariant(obj));
3690 LASSERT(!dt_object_remote(dt));
3691 LASSERT(osd_is_write_locked(env, obj));
3692 LASSERT(th != NULL);
3694 oh = container_of0(th, struct osd_thandle, ot_super);
3695 LASSERT(oh->ot_handle != NULL);
3697 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3699 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3700 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3702 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3703 * (65000) subdirectories by storing "1" in i_nlink if the link count
3704 * would otherwise overflow. Directory tranversal tools understand
3705 * that (st_nlink == 1) indicates that the filesystem dose not track
3706 * hard links count on the directory, and will not abort subdirectory
3707 * scanning early once (st_nlink - 2) subdirs have been found.
3709 * This also has to properly handle the case of inodes with nlink == 0
3710 * in case they are being linked into the PENDING directory
3712 spin_lock(&obj->oo_guard);
3713 if (unlikely(inode->i_nlink == 0))
3714 /* inc_nlink from 0 may cause WARN_ON */
3715 set_nlink(inode, 1);
3717 ldiskfs_inc_count(oh->ot_handle, inode);
3718 if (!S_ISDIR(inode->i_mode))
3719 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3721 spin_unlock(&obj->oo_guard);
3723 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3724 LINVRNT(osd_invariant(obj));
3726 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3731 static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
3732 struct thandle *handle)
3734 struct osd_thandle *oh;
3736 if (!dt_object_exists(dt))
3739 LASSERT(!dt_object_remote(dt));
3740 LASSERT(handle != NULL);
3742 oh = container_of0(handle, struct osd_thandle, ot_super);
3743 LASSERT(oh->ot_handle == NULL);
3745 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3746 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3752 * Concurrency: @dt is write locked.
3754 static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
3757 struct osd_object *obj = osd_dt_obj(dt);
3758 struct inode *inode = obj->oo_inode;
3759 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3760 struct osd_thandle *oh;
3762 if (!dt_object_exists(dt))
3765 LINVRNT(osd_invariant(obj));
3766 LASSERT(!dt_object_remote(dt));
3767 LASSERT(osd_is_write_locked(env, obj));
3768 LASSERT(th != NULL);
3770 oh = container_of0(th, struct osd_thandle, ot_super);
3771 LASSERT(oh->ot_handle != NULL);
3773 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3775 spin_lock(&obj->oo_guard);
3776 /* That can be result of upgrade from old Lustre version and
3777 * applied only to local files. Just skip this ref_del call.
3778 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3779 if (inode->i_nlink == 0) {
3780 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3781 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3782 ", maybe an upgraded file? (LU-3915)\n",
3783 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3784 spin_unlock(&obj->oo_guard);
3788 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3789 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3791 ldiskfs_dec_count(oh->ot_handle, inode);
3792 spin_unlock(&obj->oo_guard);
3794 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3795 LINVRNT(osd_invariant(obj));
3797 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3803 * Concurrency: @dt is read locked.
3805 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3806 struct lu_buf *buf, const char *name)
3808 struct osd_object *obj = osd_dt_obj(dt);
3809 struct inode *inode = obj->oo_inode;
3810 struct osd_thread_info *info = osd_oti_get(env);
3811 struct dentry *dentry = &info->oti_obj_dentry;
3812 bool cache_xattr = false;
3817 /* version get is not real XATTR but uses xattr API */
3818 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3819 dt_obj_version_t *ver = buf->lb_buf;
3821 /* for version we are just using xattr API but change inode
3823 if (buf->lb_len == 0)
3824 return sizeof(dt_obj_version_t);
3826 if (buf->lb_len < sizeof(dt_obj_version_t))
3829 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
3830 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3832 *ver = LDISKFS_I(inode)->i_fs_version;
3834 return sizeof(dt_obj_version_t);
3837 if (!dt_object_exists(dt))
3840 LASSERT(!dt_object_remote(dt));
3841 LASSERT(inode->i_op != NULL);
3842 LASSERT(inode->i_op->getxattr != NULL);
3844 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
3845 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
3849 rc = osd_oxc_get(obj, name, buf);
3854 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
3855 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3856 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3857 struct filter_fid *ff;
3858 struct ost_layout *ol;
3860 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
3864 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
3867 if (buf->lb_len == 0 || !buf->lb_buf)
3870 if (buf->lb_len < rc)
3874 ol = &ff->ff_layout;
3875 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
3876 PFID_STRIPE_IDX_BITS);
3877 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
3878 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
3879 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
3880 if (lma->lma_compat & LMAC_COMP_INFO) {
3881 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
3882 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
3883 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
3885 ol->ol_comp_start = 0;
3886 ol->ol_comp_end = 0;
3890 rc = __osd_xattr_get(inode, dentry, name,
3891 buf->lb_buf, buf->lb_len);
3895 if (rc == -ENOENT || rc == -ENODATA)
3896 osd_oxc_add(obj, name, NULL, 0);
3897 else if (rc > 0 && buf->lb_buf != NULL)
3898 osd_oxc_add(obj, name, buf->lb_buf, rc);
3904 static int osd_declare_xattr_set(const struct lu_env *env,
3905 struct dt_object *dt,
3906 const struct lu_buf *buf, const char *name,
3907 int fl, struct thandle *handle)
3909 struct osd_thandle *oh;
3911 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3913 LASSERT(handle != NULL);
3915 oh = container_of0(handle, struct osd_thandle, ot_super);
3916 LASSERT(oh->ot_handle == NULL);
3918 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3919 /* For non-upgrading case, the LMA is set first and
3920 * usually fit inode. But for upgrade case, the LMA
3921 * may be in another separated EA block. */
3922 if (dt_object_exists(dt)) {
3923 if (fl == LU_XATTR_REPLACE)
3928 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3930 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3931 /* We may need to delete the old PFID EA. */
3932 credits = LDISKFS_MAXQUOTAS_DEL_BLOCKS(sb);
3933 if (fl == LU_XATTR_REPLACE)
3940 credits += osd_dto_credits_noquota[DTO_XATTR_SET];
3945 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3946 /* learn xattr size from osd_xattr_get if
3947 attribute has not been read yet */
3948 buflen = __osd_xattr_get(
3949 osd_dt_obj(dt)->oo_inode,
3950 &osd_oti_get(env)->oti_obj_dentry,
3955 buflen = buf->lb_len;
3958 if (buflen > sb->s_blocksize) {
3959 credits += osd_calc_bkmap_credits(
3961 (buflen + sb->s_blocksize - 1) >>
3962 sb->s_blocksize_bits);
3966 * xattr set may involve inode quota change, reserve credits for
3967 * dquot_initialize()
3969 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3972 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3977 static int osd_xattr_set_pfid(const struct lu_env *env, struct osd_object *obj,
3978 const struct lu_buf *buf, int fl,
3979 struct thandle *handle)
3981 struct osd_thread_info *info = osd_oti_get(env);
3982 struct dentry *dentry = &info->oti_obj_dentry;
3983 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3984 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3985 struct inode *inode = obj->oo_inode;
3986 struct filter_fid *ff = buf->lb_buf;
3987 struct ost_layout *ol = &ff->ff_layout;
3988 int flags = XATTR_REPLACE;
3992 if (buf->lb_len != sizeof(*ff) && buf->lb_len != sizeof(struct lu_fid))
3995 rc = osd_get_lma(info, inode, dentry, loa);
3996 if (rc == -ENODATA) {
3997 /* Usually for upgarding from old device */
3998 lustre_loa_init(loa, lu_object_fid(&obj->oo_dt.do_lu),
3999 LMAC_FID_ON_OST, 0);
4000 flags = XATTR_CREATE;
4005 if (!rc && lma->lma_compat & LMAC_STRIPE_INFO) {
4006 if ((fl & LU_XATTR_CREATE) && !(fl & LU_XATTR_REPLACE))
4009 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256) {
4010 /* Separate PFID EA from LMA */
4011 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4012 lustre_lma_swab(lma);
4013 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4014 sizeof(*lma), XATTR_REPLACE);
4016 obj->oo_pfid_in_lma = 0;
4017 rc = LU_XATTR_CREATE;
4023 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256)
4026 /* Old client does not send stripe information,
4027 * then store the PFID EA on disk separatedly. */
4028 if (unlikely(buf->lb_len == sizeof(struct lu_fid) ||
4029 ol->ol_stripe_size == 0))
4032 /* Remove old PFID EA entry firstly. */
4033 ll_vfs_dq_init(inode);
4034 rc = inode->i_op->removexattr(dentry, XATTR_NAME_FID);
4035 if (rc == -ENODATA) {
4036 if ((fl & LU_XATTR_REPLACE) && !(fl & LU_XATTR_CREATE))
4043 fid_le_to_cpu(&loa->loa_parent_fid, &ff->ff_parent);
4044 if (likely(ol->ol_stripe_size != 0)) {
4045 loa->loa_parent_fid.f_ver |= le32_to_cpu(ol->ol_stripe_count) <<
4046 PFID_STRIPE_IDX_BITS;
4047 loa->loa_stripe_size = le32_to_cpu(ol->ol_stripe_size);
4048 lma->lma_compat |= LMAC_STRIPE_INFO;
4049 if (ol->ol_comp_id != 0) {
4050 loa->loa_comp_id = le32_to_cpu(ol->ol_comp_id);
4051 loa->loa_comp_start = le64_to_cpu(ol->ol_comp_start);
4052 loa->loa_comp_end = le64_to_cpu(ol->ol_comp_end);
4053 lma->lma_compat |= LMAC_COMP_INFO;
4057 lustre_loa_swab(loa, false);
4059 /* Store the PFID EA inside LMA. */
4060 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa, sizeof(*loa),
4063 obj->oo_pfid_in_lma = 1;
4069 * Concurrency: @dt is write locked.
4071 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
4072 const struct lu_buf *buf, const char *name, int fl,
4073 struct thandle *handle)
4075 struct osd_object *obj = osd_dt_obj(dt);
4076 struct osd_device *osd = osd_obj2dev(obj);
4077 struct inode *inode = obj->oo_inode;
4078 struct osd_thread_info *info = osd_oti_get(env);
4087 /* version set is not real XATTR */
4088 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4089 dt_obj_version_t *version = buf->lb_buf;
4091 /* for version we are just using xattr API but change inode
4093 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
4095 CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
4096 *version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4098 LDISKFS_I(inode)->i_fs_version = *version;
4099 /* Version is set after all inode operations are finished,
4100 * so we should mark it dirty here */
4101 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
4106 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
4107 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
4110 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4112 /* For the OST device with 256 bytes inode size by default,
4113 * the PFID EA will be stored together with LMA EA to avoid
4114 * performance trouble. Otherwise the PFID EA can be stored
4115 * independently. LU-8998 */
4116 if (strcmp(name, XATTR_NAME_FID) == 0 && osd->od_is_ost &&
4117 (LDISKFS_INODE_SIZE(inode->i_sb) <= 256 || obj->oo_pfid_in_lma)) {
4118 LASSERT(buf->lb_buf);
4120 fl = osd_xattr_set_pfid(env, obj, buf, fl, handle);
4123 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
4124 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4125 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4127 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4131 lma->lma_incompat |= LMAI_STRIPED;
4132 lustre_lma_swab(lma);
4133 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4134 sizeof(*lma), XATTR_REPLACE);
4139 if (fl & LU_XATTR_REPLACE)
4140 fs_flags |= XATTR_REPLACE;
4142 if (fl & LU_XATTR_CREATE)
4143 fs_flags |= XATTR_CREATE;
4145 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, len, fs_flags);
4146 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4149 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4150 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4151 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
4157 * Concurrency: @dt is read locked.
4159 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
4160 const struct lu_buf *buf)
4162 struct osd_object *obj = osd_dt_obj(dt);
4163 struct inode *inode = obj->oo_inode;
4164 struct osd_thread_info *info = osd_oti_get(env);
4165 struct dentry *dentry = &info->oti_obj_dentry;
4167 if (!dt_object_exists(dt))
4170 LASSERT(!dt_object_remote(dt));
4171 LASSERT(inode->i_op != NULL);
4172 LASSERT(inode->i_op->listxattr != NULL);
4174 dentry->d_inode = inode;
4175 dentry->d_sb = inode->i_sb;
4176 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
4179 static int osd_declare_xattr_del(const struct lu_env *env,
4180 struct dt_object *dt, const char *name,
4181 struct thandle *handle)
4183 struct osd_thandle *oh;
4184 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4186 LASSERT(!dt_object_remote(dt));
4187 LASSERT(handle != NULL);
4189 oh = container_of0(handle, struct osd_thandle, ot_super);
4190 LASSERT(oh->ot_handle == NULL);
4192 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
4193 osd_dto_credits_noquota[DTO_XATTR_SET]);
4195 * xattr del may involve inode quota change, reserve credits for
4196 * dquot_initialize()
4198 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4204 * Concurrency: @dt is write locked.
4206 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
4207 const char *name, struct thandle *handle)
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;
4215 if (!dt_object_exists(dt))
4218 LASSERT(!dt_object_remote(dt));
4219 LASSERT(inode->i_op != NULL);
4220 LASSERT(inode->i_op->removexattr != NULL);
4221 LASSERT(handle != NULL);
4223 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4225 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
4226 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
4228 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
4229 &info->oti_ost_attrs);
4231 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
4233 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4234 lustre_lma_swab(lma);
4235 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4236 sizeof(*lma), XATTR_REPLACE);
4238 obj->oo_pfid_in_lma = 0;
4241 ll_vfs_dq_init(inode);
4242 dentry->d_inode = inode;
4243 dentry->d_sb = inode->i_sb;
4244 rc = inode->i_op->removexattr(dentry, name);
4247 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4250 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4251 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4252 osd_oxc_del(obj, name);
4257 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
4258 __u64 start, __u64 end)
4260 struct osd_object *obj = osd_dt_obj(dt);
4261 struct inode *inode = obj->oo_inode;
4262 struct osd_thread_info *info = osd_oti_get(env);
4263 struct dentry *dentry = &info->oti_obj_dentry;
4264 struct file *file = &info->oti_file;
4269 dentry->d_inode = inode;
4270 dentry->d_sb = inode->i_sb;
4271 file->f_path.dentry = dentry;
4272 file->f_mapping = inode->i_mapping;
4273 file->f_op = inode->i_fop;
4274 set_file_inode(file, inode);
4276 rc = ll_vfs_fsync_range(file, start, end, 0);
4281 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4290 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4291 const struct dt_index_features *feat)
4293 struct iam_descr *descr;
4295 if (osd_object_is_root(o))
4296 return feat == &dt_directory_features;
4298 LASSERT(o->oo_dir != NULL);
4300 descr = o->oo_dir->od_container.ic_descr;
4301 if (feat == &dt_directory_features) {
4302 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4308 feat->dif_keysize_min <= descr->id_key_size &&
4309 descr->id_key_size <= feat->dif_keysize_max &&
4310 feat->dif_recsize_min <= descr->id_rec_size &&
4311 descr->id_rec_size <= feat->dif_recsize_max &&
4312 !(feat->dif_flags & (DT_IND_VARKEY |
4313 DT_IND_VARREC | DT_IND_NONUNQ)) &&
4314 ergo(feat->dif_flags & DT_IND_UPDATE,
4315 1 /* XXX check that object (and file system) is
4320 static int osd_iam_container_init(const struct lu_env *env,
4321 struct osd_object *obj,
4322 struct osd_directory *dir)
4324 struct iam_container *bag = &dir->od_container;
4327 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
4331 result = iam_container_setup(bag);
4333 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
4335 iam_container_fini(bag);
4342 * Concurrency: no external locking is necessary.
4344 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
4345 const struct dt_index_features *feat)
4349 struct osd_object *obj = osd_dt_obj(dt);
4351 LINVRNT(osd_invariant(obj));
4353 if (osd_object_is_root(obj)) {
4354 dt->do_index_ops = &osd_index_ea_ops;
4356 } else if (feat == &dt_directory_features) {
4357 dt->do_index_ops = &osd_index_ea_ops;
4358 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
4363 } else if (unlikely(feat == &dt_otable_features)) {
4364 dt->do_index_ops = &osd_otable_ops;
4366 } else if (unlikely(feat == &dt_acct_features)) {
4367 dt->do_index_ops = &osd_acct_index_ops;
4370 } else if (!osd_has_index(obj)) {
4371 struct osd_directory *dir;
4376 spin_lock(&obj->oo_guard);
4377 if (obj->oo_dir == NULL)
4381 * Concurrent thread allocated container data.
4384 spin_unlock(&obj->oo_guard);
4386 * Now, that we have container data, serialize its
4389 down_write(&obj->oo_ext_idx_sem);
4391 * recheck under lock.
4393 if (!osd_has_index(obj))
4394 result = osd_iam_container_init(env, obj,
4398 up_write(&obj->oo_ext_idx_sem);
4406 if (result == 0 && skip_iam == 0) {
4407 if (!osd_iam_index_probe(env, obj, feat))
4410 LINVRNT(osd_invariant(obj));
4415 static int osd_otable_it_attr_get(const struct lu_env *env,
4416 struct dt_object *dt,
4417 struct lu_attr *attr)
4423 static const struct dt_object_operations osd_obj_ops = {
4424 .do_read_lock = osd_read_lock,
4425 .do_write_lock = osd_write_lock,
4426 .do_read_unlock = osd_read_unlock,
4427 .do_write_unlock = osd_write_unlock,
4428 .do_write_locked = osd_write_locked,
4429 .do_attr_get = osd_attr_get,
4430 .do_declare_attr_set = osd_declare_attr_set,
4431 .do_attr_set = osd_attr_set,
4432 .do_ah_init = osd_ah_init,
4433 .do_declare_create = osd_declare_create,
4434 .do_create = osd_create,
4435 .do_declare_destroy = osd_declare_destroy,
4436 .do_destroy = osd_destroy,
4437 .do_index_try = osd_index_try,
4438 .do_declare_ref_add = osd_declare_ref_add,
4439 .do_ref_add = osd_ref_add,
4440 .do_declare_ref_del = osd_declare_ref_del,
4441 .do_ref_del = osd_ref_del,
4442 .do_xattr_get = osd_xattr_get,
4443 .do_declare_xattr_set = osd_declare_xattr_set,
4444 .do_xattr_set = osd_xattr_set,
4445 .do_declare_xattr_del = osd_declare_xattr_del,
4446 .do_xattr_del = osd_xattr_del,
4447 .do_xattr_list = osd_xattr_list,
4448 .do_object_sync = osd_object_sync,
4449 .do_invalidate = osd_invalidate,
4452 static const struct dt_object_operations osd_obj_otable_it_ops = {
4453 .do_attr_get = osd_otable_it_attr_get,
4454 .do_index_try = osd_index_try,
4457 static int osd_index_declare_iam_delete(const struct lu_env *env,
4458 struct dt_object *dt,
4459 const struct dt_key *key,
4460 struct thandle *handle)
4462 struct osd_thandle *oh;
4464 oh = container_of0(handle, struct osd_thandle, ot_super);
4465 LASSERT(oh->ot_handle == NULL);
4467 /* Recycle may cause additional three blocks to be changed. */
4468 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
4469 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
4475 * delete a (key, value) pair from index \a dt specified by \a key
4477 * \param dt osd index object
4478 * \param key key for index
4479 * \param rec record reference
4480 * \param handle transaction handler
4483 * \retval -ve failure
4485 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
4486 const struct dt_key *key,
4487 struct thandle *handle)
4489 struct osd_thread_info *oti = osd_oti_get(env);
4490 struct osd_object *obj = osd_dt_obj(dt);
4491 struct osd_thandle *oh;
4492 struct iam_path_descr *ipd;
4493 struct iam_container *bag = &obj->oo_dir->od_container;
4497 if (!dt_object_exists(dt))
4500 LINVRNT(osd_invariant(obj));
4501 LASSERT(!dt_object_remote(dt));
4502 LASSERT(bag->ic_object == obj->oo_inode);
4503 LASSERT(handle != NULL);
4505 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4507 ipd = osd_idx_ipd_get(env, bag);
4508 if (unlikely(ipd == NULL))
4511 oh = container_of0(handle, struct osd_thandle, ot_super);
4512 LASSERT(oh->ot_handle != NULL);
4513 LASSERT(oh->ot_handle->h_transaction != NULL);
4515 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4516 /* swab quota uid/gid provided by caller */
4517 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4518 key = (const struct dt_key *)&oti->oti_quota_id;
4521 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
4522 osd_ipd_put(env, bag, ipd);
4523 LINVRNT(osd_invariant(obj));
4524 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4528 static int osd_index_declare_ea_delete(const struct lu_env *env,
4529 struct dt_object *dt,
4530 const struct dt_key *key,
4531 struct thandle *handle)
4533 struct osd_thandle *oh;
4534 struct inode *inode;
4538 LASSERT(!dt_object_remote(dt));
4539 LASSERT(handle != NULL);
4541 oh = container_of0(handle, struct osd_thandle, ot_super);
4542 LASSERT(oh->ot_handle == NULL);
4544 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
4545 if (key != NULL && unlikely(strcmp((char *)key, dotdot) == 0)) {
4546 /* '..' to a remote object has a local representative */
4547 credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
4548 /* to reset LMAI_REMOTE_PARENT */
4551 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
4553 inode = osd_dt_obj(dt)->oo_inode;
4557 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
4558 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
4563 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
4566 struct osd_fid_pack *rec;
4569 if (de->file_type & LDISKFS_DIRENT_LUFID) {
4570 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
4571 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
4572 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
4578 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
4579 const struct lu_fid *fid)
4581 struct seq_server_site *ss = osd_seq_site(osd);
4584 /* FID seqs not in FLDB, must be local seq */
4585 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
4588 /* If FLD is not being initialized yet, it only happens during the
4589 * initialization, likely during mgs initialization, and we assume
4590 * this is local FID. */
4591 if (ss == NULL || ss->ss_server_fld == NULL)
4594 /* Only check the local FLDB here */
4595 if (osd_seq_exists(env, osd, fid_seq(fid)))
4602 * Index delete function for interoperability mode (b11826).
4603 * It will remove the directory entry added by osd_index_ea_insert().
4604 * This entry is needed to maintain name->fid mapping.
4606 * \param key, key i.e. file entry to be deleted
4608 * \retval 0, on success
4609 * \retval -ve, on error
4611 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
4612 const struct dt_key *key, struct thandle *handle)
4614 struct osd_object *obj = osd_dt_obj(dt);
4615 struct inode *dir = obj->oo_inode;
4616 struct dentry *dentry;
4617 struct osd_thandle *oh;
4618 struct ldiskfs_dir_entry_2 *de = NULL;
4619 struct buffer_head *bh;
4620 struct htree_lock *hlock = NULL;
4621 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
4622 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4626 if (!dt_object_exists(dt))
4629 LINVRNT(osd_invariant(obj));
4630 LASSERT(!dt_object_remote(dt));
4631 LASSERT(handle != NULL);
4633 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4635 oh = container_of(handle, struct osd_thandle, ot_super);
4636 LASSERT(oh->ot_handle != NULL);
4637 LASSERT(oh->ot_handle->h_transaction != NULL);
4639 ll_vfs_dq_init(dir);
4640 dentry = osd_child_dentry_get(env, obj,
4641 (char *)key, strlen((char *)key));
4643 if (obj->oo_hl_head != NULL) {
4644 hlock = osd_oti_get(env)->oti_hlock;
4645 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4646 dir, LDISKFS_HLOCK_DEL);
4648 down_write(&obj->oo_ext_idx_sem);
4651 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4653 /* If this is not the ".." entry, it might be a remote DNE
4654 * entry and we need to check if the FID is for a remote
4655 * MDT. If the FID is not in the directory entry (e.g.
4656 * upgraded 1.8 filesystem without dirdata enabled) then
4657 * we need to get the FID from the LMA. For a remote directory
4658 * there HAS to be an LMA, it cannot be an IGIF inode in this
4661 * Delete the entry before the agent inode in order to
4662 * simplify error handling. At worst an error after deleting
4663 * the entry first might leak the agent inode afterward. The
4664 * reverse would need filesystem abort in case of error deleting
4665 * the entry after the agent had been removed, or leave a
4666 * dangling entry pointing at a random inode. */
4667 if (strcmp((char *)key, dotdot) != 0) {
4668 LASSERT(de != NULL);
4669 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4670 if (rc == -ENODATA) {
4671 /* can't get FID, postpone to the end of the
4672 * transaction when iget() is safe */
4673 osd_schedule_agent_inode_removal(env, oh,
4674 le32_to_cpu(de->inode));
4675 } else if (rc == 0 &&
4676 unlikely(osd_remote_fid(env, osd, fid))) {
4677 osd_schedule_agent_inode_removal(env, oh,
4678 le32_to_cpu(de->inode));
4681 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4687 ldiskfs_htree_unlock(hlock);
4689 up_write(&obj->oo_ext_idx_sem);
4694 /* For inode on the remote MDT, .. will point to
4695 * /Agent directory, Check whether it needs to delete
4696 * from agent directory */
4697 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4700 ret = osd_delete_from_remote_parent(env, osd_obj2dev(obj),
4703 /* Sigh, the entry has been deleted, and
4704 * it is not easy to revert it back, so
4705 * let's keep this error private, and let
4706 * LFSCK fix it. XXX */
4707 CERROR("%s: delete remote parent "DFID": rc = %d\n",
4708 osd_name(osd), PFID(fid), ret);
4711 LASSERT(osd_invariant(obj));
4712 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4717 * Lookup index for \a key and copy record to \a rec.
4719 * \param dt osd index object
4720 * \param key key for index
4721 * \param rec record reference
4723 * \retval +ve success : exact mach
4724 * \retval 0 return record with key not greater than \a key
4725 * \retval -ve failure
4727 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4728 struct dt_rec *rec, const struct dt_key *key)
4730 struct osd_object *obj = osd_dt_obj(dt);
4731 struct iam_path_descr *ipd;
4732 struct iam_container *bag = &obj->oo_dir->od_container;
4733 struct osd_thread_info *oti = osd_oti_get(env);
4734 struct iam_iterator *it = &oti->oti_idx_it;
4735 struct iam_rec *iam_rec;
4739 if (!dt_object_exists(dt))
4742 LASSERT(osd_invariant(obj));
4743 LASSERT(!dt_object_remote(dt));
4744 LASSERT(bag->ic_object == obj->oo_inode);
4746 ipd = osd_idx_ipd_get(env, bag);
4750 /* got ipd now we can start iterator. */
4751 iam_it_init(it, bag, 0, ipd);
4753 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4754 /* swab quota uid/gid provided by caller */
4755 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4756 key = (const struct dt_key *)&oti->oti_quota_id;
4759 rc = iam_it_get(it, (struct iam_key *)key);
4761 if (S_ISDIR(obj->oo_inode->i_mode))
4762 iam_rec = (struct iam_rec *)oti->oti_ldp;
4764 iam_rec = (struct iam_rec *) rec;
4766 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4768 if (S_ISDIR(obj->oo_inode->i_mode))
4769 osd_fid_unpack((struct lu_fid *) rec,
4770 (struct osd_fid_pack *)iam_rec);
4771 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4772 osd_quota_unpack(obj, rec);
4777 osd_ipd_put(env, bag, ipd);
4779 LINVRNT(osd_invariant(obj));
4784 static int osd_index_declare_iam_insert(const struct lu_env *env,
4785 struct dt_object *dt,
4786 const struct dt_rec *rec,
4787 const struct dt_key *key,
4788 struct thandle *handle)
4790 struct osd_thandle *oh;
4792 LASSERT(handle != NULL);
4794 oh = container_of0(handle, struct osd_thandle, ot_super);
4795 LASSERT(oh->ot_handle == NULL);
4797 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4798 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4804 * Inserts (key, value) pair in \a dt index object.
4806 * \param dt osd index object
4807 * \param key key for index
4808 * \param rec record reference
4809 * \param th transaction handler
4812 * \retval -ve failure
4814 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4815 const struct dt_rec *rec,
4816 const struct dt_key *key, struct thandle *th,
4819 struct osd_object *obj = osd_dt_obj(dt);
4820 struct iam_path_descr *ipd;
4821 struct osd_thandle *oh;
4822 struct iam_container *bag;
4823 struct osd_thread_info *oti = osd_oti_get(env);
4824 struct iam_rec *iam_rec;
4828 if (!dt_object_exists(dt))
4831 LINVRNT(osd_invariant(obj));
4832 LASSERT(!dt_object_remote(dt));
4834 bag = &obj->oo_dir->od_container;
4835 LASSERT(bag->ic_object == obj->oo_inode);
4836 LASSERT(th != NULL);
4838 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4840 ipd = osd_idx_ipd_get(env, bag);
4841 if (unlikely(ipd == NULL))
4844 oh = container_of0(th, struct osd_thandle, ot_super);
4845 LASSERT(oh->ot_handle != NULL);
4846 LASSERT(oh->ot_handle->h_transaction != NULL);
4847 if (S_ISDIR(obj->oo_inode->i_mode)) {
4848 iam_rec = (struct iam_rec *)oti->oti_ldp;
4849 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4850 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4851 /* pack quota uid/gid */
4852 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4853 key = (const struct dt_key *)&oti->oti_quota_id;
4854 /* pack quota record */
4855 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4856 iam_rec = (struct iam_rec *)rec;
4858 iam_rec = (struct iam_rec *)rec;
4861 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4863 osd_ipd_put(env, bag, ipd);
4864 LINVRNT(osd_invariant(obj));
4865 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4870 * Calls ldiskfs_add_entry() to add directory entry
4871 * into the directory. This is required for
4872 * interoperability mode (b11826)
4874 * \retval 0, on success
4875 * \retval -ve, on error
4877 static int __osd_ea_add_rec(struct osd_thread_info *info,
4878 struct osd_object *pobj, struct inode *cinode,
4879 const char *name, const struct lu_fid *fid,
4880 struct htree_lock *hlock, struct thandle *th)
4882 struct ldiskfs_dentry_param *ldp;
4883 struct dentry *child;
4884 struct osd_thandle *oth;
4887 oth = container_of(th, struct osd_thandle, ot_super);
4888 LASSERT(oth->ot_handle != NULL);
4889 LASSERT(oth->ot_handle->h_transaction != NULL);
4890 LASSERT(pobj->oo_inode);
4892 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4893 if (unlikely(pobj->oo_inode ==
4894 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4897 osd_get_ldiskfs_dirent_param(ldp, fid);
4898 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4899 child->d_fsdata = (void *)ldp;
4900 ll_vfs_dq_init(pobj->oo_inode);
4901 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
4902 child, cinode, hlock);
4903 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4904 struct ldiskfs_dir_entry_2 *de;
4905 struct buffer_head *bh;
4908 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4911 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4914 if (S_ISDIR(cinode->i_mode))
4915 de->file_type = LDISKFS_DIRENT_LUFID |
4916 LDISKFS_FT_REG_FILE;
4918 de->file_type = LDISKFS_DIRENT_LUFID |
4920 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4931 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4932 * into the directory.Also sets flags into osd object to
4933 * indicate dot and dotdot are created. This is required for
4934 * interoperability mode (b11826)
4936 * \param dir directory for dot and dotdot fixup.
4937 * \param obj child object for linking
4939 * \retval 0, on success
4940 * \retval -ve, on error
4942 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4943 struct osd_object *dir,
4944 struct inode *parent_dir, const char *name,
4945 const struct lu_fid *dot_fid,
4946 const struct lu_fid *dot_dot_fid,
4949 struct inode *inode = dir->oo_inode;
4950 struct osd_thandle *oth;
4953 oth = container_of(th, struct osd_thandle, ot_super);
4954 LASSERT(oth->ot_handle->h_transaction != NULL);
4955 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4957 if (strcmp(name, dot) == 0) {
4958 if (dir->oo_compat_dot_created) {
4961 LASSERT(inode->i_ino == parent_dir->i_ino);
4962 dir->oo_compat_dot_created = 1;
4965 } else if (strcmp(name, dotdot) == 0) {
4966 if (!dir->oo_compat_dot_created)
4968 /* in case of rename, dotdot is already created */
4969 if (dir->oo_compat_dotdot_created) {
4970 return __osd_ea_add_rec(info, dir, parent_dir, name,
4971 dot_dot_fid, NULL, th);
4974 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4975 struct lu_fid tfid = *dot_dot_fid;
4978 result = osd_add_dot_dotdot_internal(info,
4979 dir->oo_inode, parent_dir, dot_fid,
4982 result = osd_add_dot_dotdot_internal(info,
4983 dir->oo_inode, parent_dir, dot_fid,
4988 dir->oo_compat_dotdot_created = 1;
4996 * It will call the appropriate osd_add* function and return the
4997 * value, return by respective functions.
4999 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
5000 struct inode *cinode, const char *name,
5001 const struct lu_fid *fid, struct thandle *th)
5003 struct osd_thread_info *info = osd_oti_get(env);
5004 struct htree_lock *hlock;
5007 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
5009 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
5011 if (hlock != NULL) {
5012 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5015 down_write(&pobj->oo_ext_idx_sem);
5018 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
5019 lu_object_fid(&pobj->oo_dt.do_lu),
5022 if (hlock != NULL) {
5023 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5024 pobj->oo_inode, LDISKFS_HLOCK_ADD);
5026 down_write(&pobj->oo_ext_idx_sem);
5029 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
5030 struct lu_fid *tfid = &info->oti_fid;
5034 rc = __osd_ea_add_rec(info, pobj, cinode, name,
5037 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
5042 ldiskfs_htree_unlock(hlock);
5044 up_write(&pobj->oo_ext_idx_sem);
5050 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
5051 struct osd_idmap_cache *oic)
5053 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
5054 struct lu_fid *fid = &oic->oic_fid;
5055 struct osd_inode_id *id = &oic->oic_lid;
5056 struct inode *inode = NULL;
5062 if (!fid_is_norm(fid) && !fid_is_igif(fid))
5065 if (thread_is_running(&scrub->os_thread) &&
5066 scrub->os_pos_current > id->oii_ino)
5069 if (dev->od_auto_scrub_interval == AS_NEVER ||
5070 cfs_time_before(ktime_get_real_seconds(),
5071 scrub->os_file.sf_time_last_complete +
5072 dev->od_auto_scrub_interval))
5076 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
5077 if (rc == -ENOENT) {
5078 __u32 gen = id->oii_gen;
5084 inode = osd_iget(oti, dev, id);
5085 /* The inode has been removed (by race maybe). */
5086 if (IS_ERR(inode)) {
5087 rc = PTR_ERR(inode);
5089 RETURN(rc == -ESTALE ? -ENOENT : rc);
5092 /* The OI mapping is lost. */
5093 if (gen != OSD_OII_NOGEN)
5096 /* The inode may has been reused by others, we do not know,
5097 * leave it to be handled by subsequent osd_fid_lookup(). */
5099 } else if (rc || osd_id_eq(id, &oti->oti_id)) {
5106 if (thread_is_running(&scrub->os_thread)) {
5107 if (inode == NULL) {
5108 inode = osd_iget(oti, dev, id);
5109 /* The inode has been removed (by race maybe). */
5110 if (IS_ERR(inode)) {
5111 rc = PTR_ERR(inode);
5113 RETURN(rc == -ESTALE ? -ENOENT : rc);
5117 rc = osd_oii_insert(dev, oic, insert);
5118 /* There is race condition between osd_oi_lookup and OI scrub.
5119 * The OI scrub finished just after osd_oi_lookup() failure.
5120 * Under such case, it is unnecessary to trigger OI scrub again,
5121 * but try to call osd_oi_lookup() again. */
5122 if (unlikely(rc == -EAGAIN))
5125 if (!S_ISDIR(inode->i_mode))
5128 rc = osd_check_lmv(oti, dev, inode, oic);
5133 if (dev->od_auto_scrub_interval != AS_NEVER && ++once == 1) {
5134 rc = osd_scrub_start(oti->oti_env, dev, SS_AUTO_PARTIAL |
5135 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
5136 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
5137 "%s: trigger partial OI scrub for RPC inconsistency "
5138 "checking FID "DFID": rc = %d\n",
5139 osd_dev2name(dev), PFID(fid), rc);
5140 if (rc == 0 || rc == -EALREADY)
5153 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
5154 struct osd_device *dev,
5155 struct osd_idmap_cache *oic,
5156 struct lu_fid *fid, __u32 ino)
5158 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
5159 struct inode *inode;
5162 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
5163 inode = osd_iget(oti, dev, &oic->oic_lid);
5164 if (IS_ERR(inode)) {
5165 fid_zero(&oic->oic_fid);
5166 return PTR_ERR(inode);
5169 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, loa);
5172 fid_zero(&oic->oic_fid);
5174 *fid = oic->oic_fid = loa->loa_lma.lma_self_fid;
5178 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
5179 struct osd_inode_id *id, const struct lu_fid *fid)
5181 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
5182 id->oii_ino, id->oii_gen, info);
5183 info->oti_cache.oic_lid = *id;
5184 info->oti_cache.oic_fid = *fid;
5185 info->oti_cache.oic_dev = osd;
5189 * Get parent FID from the linkEA.
5191 * For a directory which parent resides on remote MDT, to satisfy the
5192 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
5193 * the other hand, to make the lookup(..) on the directory can return
5194 * the real parent FID, we append the real parent FID after its ".."
5195 * name entry in the /REMOTE_PARENT_DIR.
5197 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
5198 * backup. So after the restore, we cannot get the right parent FID from
5199 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
5200 * we have stored the real parent FID in the directory object's linkEA,
5201 * we can parse the linkEA for the real parent FID.
5203 * \param[in] env pointer to the thread context
5204 * \param[in] obj pointer to the object to be handled
5205 * \param[out]fid pointer to the buffer to hold the parent FID
5207 * \retval 0 for getting the real parent FID successfully
5208 * \retval negative error number on failure
5210 static int osd_get_pfid_from_linkea(const struct lu_env *env,
5211 struct osd_object *obj,
5214 struct osd_thread_info *oti = osd_oti_get(env);
5215 struct lu_buf *buf = &oti->oti_big_buf;
5216 struct dentry *dentry = &oti->oti_obj_dentry;
5217 struct inode *inode = obj->oo_inode;
5218 struct linkea_data ldata = { NULL };
5223 if (!S_ISDIR(inode->i_mode))
5227 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5228 buf->lb_buf, buf->lb_len);
5229 if (rc == -ERANGE) {
5230 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5233 lu_buf_realloc(buf, rc);
5234 if (buf->lb_buf == NULL)
5241 if (unlikely(rc == 0))
5247 if (unlikely(buf->lb_buf == NULL)) {
5248 lu_buf_realloc(buf, rc);
5249 if (buf->lb_buf == NULL)
5256 rc = linkea_init_with_rec(&ldata);
5258 linkea_first_entry(&ldata);
5259 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5265 static int osd_verify_ent_by_linkea(const struct lu_env *env,
5266 struct inode *inode,
5267 const struct lu_fid *pfid,
5268 const char *name, const int namelen)
5270 struct osd_thread_info *oti = osd_oti_get(env);
5271 struct lu_buf *buf = &oti->oti_big_buf;
5272 struct dentry *dentry = &oti->oti_obj_dentry;
5273 struct linkea_data ldata = { NULL };
5274 struct lu_name cname = { .ln_name = name,
5275 .ln_namelen = namelen };
5280 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5281 buf->lb_buf, buf->lb_len);
5283 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
5288 if (unlikely(rc == 0))
5291 if (buf->lb_len < rc) {
5292 lu_buf_realloc(buf, rc);
5293 if (buf->lb_buf == NULL)
5300 rc = linkea_init_with_rec(&ldata);
5302 rc = linkea_links_find(&ldata, &cname, pfid);
5308 * Calls ->lookup() to find dentry. From dentry get inode and
5309 * read inode's ea to get fid. This is required for interoperability
5312 * \retval 0, on success
5313 * \retval -ve, on error
5315 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
5316 struct dt_rec *rec, const struct dt_key *key)
5318 struct inode *dir = obj->oo_inode;
5319 struct dentry *dentry;
5320 struct ldiskfs_dir_entry_2 *de;
5321 struct buffer_head *bh;
5322 struct lu_fid *fid = (struct lu_fid *) rec;
5323 struct htree_lock *hlock = NULL;
5328 LASSERT(dir->i_op != NULL);
5329 LASSERT(dir->i_op->lookup != NULL);
5331 dentry = osd_child_dentry_get(env, obj,
5332 (char *)key, strlen((char *)key));
5334 if (obj->oo_hl_head != NULL) {
5335 hlock = osd_oti_get(env)->oti_hlock;
5336 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5337 dir, LDISKFS_HLOCK_LOOKUP);
5339 down_read(&obj->oo_ext_idx_sem);
5342 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5344 struct osd_thread_info *oti = osd_oti_get(env);
5345 struct osd_inode_id *id = &oti->oti_id;
5346 struct osd_idmap_cache *oic = &oti->oti_cache;
5347 struct osd_device *dev = osd_obj2dev(obj);
5349 ino = le32_to_cpu(de->inode);
5350 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
5352 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
5356 rc = osd_get_fid_from_dentry(de, rec);
5358 /* done with de, release bh */
5361 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5362 const char *name = (const char *)key;
5364 /* If the parent is on remote MDT, and there
5365 * is no FID-in-dirent, then we have to get
5366 * the parent FID from the linkEA. */
5367 if (likely(strlen(name) == 2 &&
5368 name[0] == '.' && name[1] == '.'))
5369 rc = osd_get_pfid_from_linkea(env, obj,
5372 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5375 osd_id_gen(id, ino, OSD_OII_NOGEN);
5378 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
5379 fid_zero(&oic->oic_fid);
5384 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
5385 rc = osd_consistency_check(oti, dev, oic);
5387 fid_zero(&oic->oic_fid);
5389 /* Other error should not affect lookup result. */
5399 ldiskfs_htree_unlock(hlock);
5401 up_read(&obj->oo_ext_idx_sem);
5405 static int osd_index_declare_ea_insert(const struct lu_env *env,
5406 struct dt_object *dt,
5407 const struct dt_rec *rec,
5408 const struct dt_key *key,
5409 struct thandle *handle)
5411 struct osd_thandle *oh;
5412 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5413 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5414 const struct lu_fid *fid = rec1->rec_fid;
5415 int credits, rc = 0;
5416 struct osd_idmap_cache *idc;
5419 LASSERT(!dt_object_remote(dt));
5420 LASSERT(handle != NULL);
5421 LASSERT(fid != NULL);
5422 LASSERT(rec1->rec_type != 0);
5424 oh = container_of0(handle, struct osd_thandle, ot_super);
5425 LASSERT(oh->ot_handle == NULL);
5427 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
5429 /* we can't call iget() while a transactions is running
5430 * (this can lead to a deadlock), but we need to know
5431 * inum and object type. so we find this information at
5432 * declaration and cache in per-thread info */
5433 idc = osd_idc_find_or_init(env, osd, fid);
5435 RETURN(PTR_ERR(idc));
5436 if (idc->oic_remote) {
5437 /* a reference to remote inode is represented by an
5438 * agent inode which we have to create */
5439 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
5440 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
5443 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
5445 if (osd_dt_obj(dt)->oo_inode != NULL) {
5446 struct inode *inode = osd_dt_obj(dt)->oo_inode;
5448 /* We ignore block quota on meta pool (MDTs), so needn't
5449 * calculate how many blocks will be consumed by this index
5451 rc = osd_declare_inode_qid(env, i_uid_read(inode),
5453 i_projid_read(inode), 0,
5454 oh, osd_dt_obj(dt), NULL,
5459 #ifdef HAVE_PROJECT_QUOTA
5460 /* Reserve credits for local agent inode to transfer
5461 * to 0, quota enforcement is ignored in this case.
5463 if (idc->oic_remote &&
5464 LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
5465 i_projid_read(inode) != 0)
5466 rc = osd_declare_attr_qid(env, osd_dt_obj(dt), oh,
5467 0, i_projid_read(inode),
5468 0, false, PRJQUOTA);
5477 * Index add function for interoperability mode (b11826).
5478 * It will add the directory entry.This entry is needed to
5479 * maintain name->fid mapping.
5481 * \param key it is key i.e. file entry to be inserted
5482 * \param rec it is value of given key i.e. fid
5484 * \retval 0, on success
5485 * \retval -ve, on error
5487 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
5488 const struct dt_rec *rec,
5489 const struct dt_key *key, struct thandle *th,
5492 struct osd_object *obj = osd_dt_obj(dt);
5493 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5494 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5495 const struct lu_fid *fid = rec1->rec_fid;
5496 const char *name = (const char *)key;
5497 struct osd_thread_info *oti = osd_oti_get(env);
5498 struct inode *child_inode = NULL;
5499 struct osd_idmap_cache *idc;
5503 if (!dt_object_exists(dt))
5506 LASSERT(osd_invariant(obj));
5507 LASSERT(!dt_object_remote(dt));
5508 LASSERT(th != NULL);
5510 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5512 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
5514 idc = osd_idc_find(env, osd, fid);
5515 if (unlikely(idc == NULL)) {
5516 /* this dt_insert() wasn't declared properly, so
5517 * FID is missing in OI cache. we better do not
5518 * lookup FID in FLDB/OI and don't risk to deadlock,
5519 * but in some special cases (lfsck testing, etc)
5520 * it's much simpler than fixing a caller */
5521 CERROR("%s: "DFID" wasn't declared for insert\n",
5522 osd_name(osd), PFID(fid));
5524 idc = osd_idc_find_or_init(env, osd, fid);
5526 RETURN(PTR_ERR(idc));
5529 if (idc->oic_remote) {
5530 /* Insert remote entry */
5531 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
5532 struct osd_mdobj_map *omm = osd->od_mdt_map;
5533 struct osd_thandle *oh;
5535 /* If parent on remote MDT, we need put this object
5537 oh = container_of(th, typeof(*oh), ot_super);
5538 rc = osd_add_to_remote_parent(env, osd, obj, oh);
5540 CERROR("%s: add "DFID" error: rc = %d\n",
5542 PFID(lu_object_fid(&dt->do_lu)), rc);
5546 child_inode = igrab(omm->omm_remote_parent->d_inode);
5548 child_inode = osd_create_local_agent_inode(env, osd,
5549 obj, fid, rec1->rec_type & S_IFMT, th);
5550 if (IS_ERR(child_inode))
5551 RETURN(PTR_ERR(child_inode));
5554 /* Insert local entry */
5555 if (unlikely(idc->oic_lid.oii_ino == 0)) {
5556 /* for a reason OI cache wasn't filled properly */
5557 CERROR("%s: OIC for "DFID" isn't filled\n",
5558 osd_name(osd), PFID(fid));
5561 child_inode = oti->oti_inode;
5562 if (unlikely(child_inode == NULL)) {
5563 struct ldiskfs_inode_info *lii;
5567 child_inode = oti->oti_inode = &lii->vfs_inode;
5569 child_inode->i_sb = osd_sb(osd);
5570 child_inode->i_ino = idc->oic_lid.oii_ino;
5571 child_inode->i_mode = rec1->rec_type & S_IFMT;
5574 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
5576 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
5577 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
5579 if (child_inode && child_inode != oti->oti_inode)
5581 LASSERT(osd_invariant(obj));
5582 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5587 * Initialize osd Iterator for given osd index object.
5589 * \param dt osd index object
5592 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
5593 struct dt_object *dt,
5596 struct osd_it_iam *it;
5597 struct osd_object *obj = osd_dt_obj(dt);
5598 struct lu_object *lo = &dt->do_lu;
5599 struct iam_path_descr *ipd;
5600 struct iam_container *bag = &obj->oo_dir->od_container;
5602 if (!dt_object_exists(dt))
5603 return ERR_PTR(-ENOENT);
5607 return ERR_PTR(-ENOMEM);
5609 ipd = osd_it_ipd_get(env, bag);
5610 if (likely(ipd != NULL)) {
5614 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
5615 return (struct dt_it *)it;
5618 return ERR_PTR(-ENOMEM);
5623 * free given Iterator.
5625 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
5627 struct osd_it_iam *it = (struct osd_it_iam *)di;
5628 struct osd_object *obj = it->oi_obj;
5630 iam_it_fini(&it->oi_it);
5631 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
5632 osd_object_put(env, obj);
5637 * Move Iterator to record specified by \a key
5639 * \param di osd iterator
5640 * \param key key for index
5642 * \retval +ve di points to record with least key not larger than key
5643 * \retval 0 di points to exact matched key
5644 * \retval -ve failure
5647 static int osd_it_iam_get(const struct lu_env *env,
5648 struct dt_it *di, const struct dt_key *key)
5650 struct osd_thread_info *oti = osd_oti_get(env);
5651 struct osd_it_iam *it = (struct osd_it_iam *)di;
5653 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5654 /* swab quota uid/gid */
5655 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5656 key = (struct dt_key *)&oti->oti_quota_id;
5659 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
5665 * \param di osd iterator
5667 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
5669 struct osd_it_iam *it = (struct osd_it_iam *)di;
5671 iam_it_put(&it->oi_it);
5675 * Move iterator by one record
5677 * \param di osd iterator
5679 * \retval +1 end of container reached
5681 * \retval -ve failure
5684 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
5686 struct osd_it_iam *it = (struct osd_it_iam *)di;
5688 return iam_it_next(&it->oi_it);
5692 * Return pointer to the key under iterator.
5695 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
5696 const struct dt_it *di)
5698 struct osd_thread_info *oti = osd_oti_get(env);
5699 struct osd_it_iam *it = (struct osd_it_iam *)di;
5700 struct osd_object *obj = it->oi_obj;
5703 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
5705 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
5706 /* swab quota uid/gid */
5707 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
5708 key = (struct dt_key *)&oti->oti_quota_id;
5715 * Return size of key under iterator (in bytes)
5718 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5720 struct osd_it_iam *it = (struct osd_it_iam *)di;
5722 return iam_it_key_size(&it->oi_it);
5726 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5728 /* check if file type is required */
5729 if (ent->lde_attrs & LUDA_TYPE) {
5730 struct luda_type *lt;
5731 int align = sizeof(*lt) - 1;
5733 len = (len + align) & ~align;
5734 lt = (struct luda_type *)(ent->lde_name + len);
5735 lt->lt_type = cpu_to_le16(DTTOIF(type));
5738 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5742 * build lu direct from backend fs dirent.
5746 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5747 char *name, __u16 namelen, __u16 type, __u32 attr)
5749 ent->lde_attrs = attr | LUDA_FID;
5750 fid_cpu_to_le(&ent->lde_fid, fid);
5752 ent->lde_hash = cpu_to_le64(offset);
5753 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5755 strncpy(ent->lde_name, name, namelen);
5756 ent->lde_name[namelen] = '\0';
5757 ent->lde_namelen = cpu_to_le16(namelen);
5759 /* append lustre attributes */
5760 osd_it_append_attrs(ent, namelen, type);
5764 * Return pointer to the record under iterator.
5766 static int osd_it_iam_rec(const struct lu_env *env,
5767 const struct dt_it *di,
5768 struct dt_rec *dtrec, __u32 attr)
5770 struct osd_it_iam *it = (struct osd_it_iam *)di;
5771 struct osd_thread_info *info = osd_oti_get(env);
5774 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5775 const struct osd_fid_pack *rec;
5776 struct lu_fid *fid = &info->oti_fid;
5777 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5783 name = (char *)iam_it_key_get(&it->oi_it);
5785 RETURN(PTR_ERR(name));
5787 namelen = iam_it_key_size(&it->oi_it);
5789 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5791 RETURN(PTR_ERR(rec));
5793 rc = osd_fid_unpack(fid, rec);
5797 hash = iam_it_store(&it->oi_it);
5799 /* IAM does not store object type in IAM index (dir) */
5800 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5802 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5803 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5804 (struct iam_rec *)dtrec);
5805 osd_quota_unpack(it->oi_obj, dtrec);
5807 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5808 (struct iam_rec *)dtrec);
5815 * Returns cookie for current Iterator position.
5817 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5819 struct osd_it_iam *it = (struct osd_it_iam *)di;
5821 return iam_it_store(&it->oi_it);
5825 * Restore iterator from cookie.
5827 * \param di osd iterator
5828 * \param hash Iterator location cookie
5830 * \retval +ve di points to record with least key not larger than key.
5831 * \retval 0 di points to exact matched key
5832 * \retval -ve failure
5835 static int osd_it_iam_load(const struct lu_env *env,
5836 const struct dt_it *di, __u64 hash)
5838 struct osd_it_iam *it = (struct osd_it_iam *)di;
5840 return iam_it_load(&it->oi_it, hash);
5843 static const struct dt_index_operations osd_index_iam_ops = {
5844 .dio_lookup = osd_index_iam_lookup,
5845 .dio_declare_insert = osd_index_declare_iam_insert,
5846 .dio_insert = osd_index_iam_insert,
5847 .dio_declare_delete = osd_index_declare_iam_delete,
5848 .dio_delete = osd_index_iam_delete,
5850 .init = osd_it_iam_init,
5851 .fini = osd_it_iam_fini,
5852 .get = osd_it_iam_get,
5853 .put = osd_it_iam_put,
5854 .next = osd_it_iam_next,
5855 .key = osd_it_iam_key,
5856 .key_size = osd_it_iam_key_size,
5857 .rec = osd_it_iam_rec,
5858 .store = osd_it_iam_store,
5859 .load = osd_it_iam_load
5865 * Creates or initializes iterator context.
5867 * \retval struct osd_it_ea, iterator structure on success
5870 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5871 struct dt_object *dt,
5874 struct osd_object *obj = osd_dt_obj(dt);
5875 struct osd_thread_info *info = osd_oti_get(env);
5876 struct osd_it_ea *oie;
5878 struct lu_object *lo = &dt->do_lu;
5879 struct dentry *obj_dentry;
5882 if (!dt_object_exists(dt) || obj->oo_destroyed)
5883 RETURN(ERR_PTR(-ENOENT));
5885 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5887 RETURN(ERR_PTR(-ENOMEM));
5888 obj_dentry = &oie->oie_dentry;
5890 obj_dentry->d_inode = obj->oo_inode;
5891 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5892 obj_dentry->d_name.hash = 0;
5894 oie->oie_rd_dirent = 0;
5895 oie->oie_it_dirent = 0;
5896 oie->oie_dirent = NULL;
5897 if (unlikely(!info->oti_it_ea_buf_used)) {
5898 oie->oie_buf = info->oti_it_ea_buf;
5899 info->oti_it_ea_buf_used = 1;
5901 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5902 if (oie->oie_buf == NULL)
5903 RETURN(ERR_PTR(-ENOMEM));
5907 file = &oie->oie_file;
5909 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5910 if (attr & LUDA_64BITHASH)
5911 file->f_mode = FMODE_64BITHASH;
5913 file->f_mode = FMODE_32BITHASH;
5914 file->f_path.dentry = obj_dentry;
5915 file->f_mapping = obj->oo_inode->i_mapping;
5916 file->f_op = obj->oo_inode->i_fop;
5917 set_file_inode(file, obj->oo_inode);
5920 RETURN((struct dt_it *) oie);
5924 * Destroy or finishes iterator context.
5926 * \param di iterator structure to be destroyed
5928 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5930 struct osd_thread_info *info = osd_oti_get(env);
5931 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5932 struct osd_object *obj = oie->oie_obj;
5933 struct inode *inode = obj->oo_inode;
5936 oie->oie_file.f_op->release(inode, &oie->oie_file);
5937 osd_object_put(env, obj);
5938 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5939 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5941 info->oti_it_ea_buf_used = 0;
5942 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5947 * It position the iterator at given key, so that next lookup continues from
5948 * that key Or it is similar to dio_it->load() but based on a key,
5949 * rather than file position.
5951 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5954 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5956 static int osd_it_ea_get(const struct lu_env *env,
5957 struct dt_it *di, const struct dt_key *key)
5959 struct osd_it_ea *it = (struct osd_it_ea *)di;
5962 LASSERT(((const char *)key)[0] == '\0');
5963 it->oie_file.f_pos = 0;
5964 it->oie_rd_dirent = 0;
5965 it->oie_it_dirent = 0;
5966 it->oie_dirent = NULL;
5974 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5978 struct osd_filldir_cbs {
5979 #ifdef HAVE_DIR_CONTEXT
5980 struct dir_context ctx;
5982 struct osd_it_ea *it;
5985 * It is called internally by ->readdir(). It fills the
5986 * iterator's in-memory data structure with required
5987 * information i.e. name, namelen, rec_size etc.
5989 * \param buf in which information to be filled in.
5990 * \param name name of the file in given dir
5992 * \retval 0 on success
5993 * \retval 1 on buffer full
5995 #ifdef HAVE_FILLDIR_USE_CTX
5996 static int osd_ldiskfs_filldir(struct dir_context *buf,
5998 static int osd_ldiskfs_filldir(void *buf,
6000 const char *name, int namelen,
6001 loff_t offset, __u64 ino, unsigned d_type)
6003 struct osd_it_ea *it =
6004 ((struct osd_filldir_cbs *)buf)->it;
6005 struct osd_object *obj = it->oie_obj;
6006 struct osd_it_ea_dirent *ent = it->oie_dirent;
6007 struct lu_fid *fid = &ent->oied_fid;
6008 struct osd_fid_pack *rec;
6011 /* this should never happen */
6012 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
6013 CERROR("ldiskfs return invalid namelen %d\n", namelen);
6017 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
6021 /* "." is just the object itself. */
6022 if (namelen == 1 && name[0] == '.') {
6023 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6024 } else if (d_type & LDISKFS_DIRENT_LUFID) {
6025 rec = (struct osd_fid_pack*) (name + namelen + 1);
6026 if (osd_fid_unpack(fid, rec) != 0)
6031 d_type &= ~LDISKFS_DIRENT_LUFID;
6033 /* NOT export local root. */
6034 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
6035 ino = obj->oo_inode->i_ino;
6036 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6039 ent->oied_ino = ino;
6040 ent->oied_off = offset;
6041 ent->oied_namelen = namelen;
6042 ent->oied_type = d_type;
6044 memcpy(ent->oied_name, name, namelen);
6046 it->oie_rd_dirent++;
6047 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
6052 * Calls ->readdir() to load a directory entry at a time
6053 * and stored it in iterator's in-memory data structure.
6055 * \param di iterator's in memory structure
6057 * \retval 0 on success
6058 * \retval -ve on error
6059 * \retval +1 reach the end of entry
6061 static int osd_ldiskfs_it_fill(const struct lu_env *env,
6062 const struct dt_it *di)
6064 struct osd_it_ea *it = (struct osd_it_ea *)di;
6065 struct osd_object *obj = it->oie_obj;
6066 struct inode *inode = obj->oo_inode;
6067 struct htree_lock *hlock = NULL;
6068 struct file *filp = &it->oie_file;
6070 struct osd_filldir_cbs buf = {
6071 #ifdef HAVE_DIR_CONTEXT
6072 .ctx.actor = osd_ldiskfs_filldir,
6078 it->oie_dirent = it->oie_buf;
6079 it->oie_rd_dirent = 0;
6081 if (obj->oo_hl_head != NULL) {
6082 hlock = osd_oti_get(env)->oti_hlock;
6083 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
6084 inode, LDISKFS_HLOCK_READDIR);
6086 down_read(&obj->oo_ext_idx_sem);
6089 #ifdef HAVE_DIR_CONTEXT
6090 buf.ctx.pos = filp->f_pos;
6091 rc = inode->i_fop->iterate(filp, &buf.ctx);
6092 filp->f_pos = buf.ctx.pos;
6094 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
6098 ldiskfs_htree_unlock(hlock);
6100 up_read(&obj->oo_ext_idx_sem);
6102 if (it->oie_rd_dirent == 0) {
6103 /*If it does not get any dirent, it means it has been reached
6104 *to the end of the dir */
6105 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
6109 it->oie_dirent = it->oie_buf;
6110 it->oie_it_dirent = 1;
6117 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6118 * to load a directory entry at a time and stored it in
6119 * iterator's in-memory data structure.
6121 * \param di iterator's in memory structure
6123 * \retval +ve iterator reached to end
6124 * \retval 0 iterator not reached to end
6125 * \retval -ve on error
6127 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
6129 struct osd_it_ea *it = (struct osd_it_ea *)di;
6134 if (it->oie_it_dirent < it->oie_rd_dirent) {
6136 (void *) it->oie_dirent +
6137 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
6138 it->oie_dirent->oied_namelen);
6139 it->oie_it_dirent++;
6142 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
6145 rc = osd_ldiskfs_it_fill(env, di);
6152 * Returns the key at current position from iterator's in memory structure.
6154 * \param di iterator's in memory structure
6156 * \retval key i.e. struct dt_key on success
6158 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
6159 const struct dt_it *di)
6161 struct osd_it_ea *it = (struct osd_it_ea *)di;
6163 return (struct dt_key *)it->oie_dirent->oied_name;
6167 * Returns the key's size at current position from iterator's in memory structure.
6169 * \param di iterator's in memory structure
6171 * \retval key_size i.e. struct dt_key on success
6173 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
6175 struct osd_it_ea *it = (struct osd_it_ea *)di;
6177 return it->oie_dirent->oied_namelen;
6180 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
6182 if (LDISKFS_DIR_REC_LEN(de) >=
6183 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
6190 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
6191 unsigned blocksize, bool dotdot)
6194 return osd_dotdot_has_space(de);
6196 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
6197 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
6204 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
6205 handle_t *jh, struct dentry *dentry,
6206 const struct lu_fid *fid, struct buffer_head *bh,
6207 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
6210 struct inode *dir = dentry->d_parent->d_inode;
6211 struct inode *inode = dentry->d_inode;
6212 struct osd_fid_pack *rec;
6213 struct ldiskfs_dentry_param *ldp;
6214 int namelen = dentry->d_name.len;
6216 struct osd_thread_info *info = osd_oti_get(env);
6219 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
6220 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6223 /* There is enough space to hold the FID-in-dirent. */
6224 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
6225 rc = ldiskfs_journal_get_write_access(jh, bh);
6229 de->name[namelen] = 0;
6230 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
6231 rec->fp_len = sizeof(struct lu_fid) + 1;
6232 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
6233 de->file_type |= LDISKFS_DIRENT_LUFID;
6234 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
6241 rc = ldiskfs_delete_entry(jh, dir, de, bh);
6245 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
6246 osd_get_ldiskfs_dirent_param(ldp, fid);
6247 dentry->d_fsdata = (void *)ldp;
6248 ll_vfs_dq_init(dir);
6249 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
6250 /* It is too bad, we cannot reinsert the name entry back.
6251 * That means we lose it! */
6253 CDEBUG(D_LFSCK, "%s: fail to reinsert the dirent, "
6254 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
6255 osd_ino2name(inode),
6256 dir->i_ino, dir->i_generation, namelen,
6257 dentry->d_name.name, PFID(fid), rc);
6263 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
6264 struct osd_it_ea *it, struct lu_fid *fid,
6265 struct osd_inode_id *id, __u32 *attr)
6267 struct osd_thread_info *info = osd_oti_get(env);
6268 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
6269 struct osd_device *dev = osd_obj2dev(obj);
6270 struct super_block *sb = osd_sb(dev);
6271 const char *devname = osd_name(dev);
6272 struct osd_it_ea_dirent *ent = it->oie_dirent;
6273 struct inode *dir = obj->oo_inode;
6274 struct htree_lock *hlock = NULL;
6275 struct buffer_head *bh = NULL;
6276 handle_t *jh = NULL;
6277 struct ldiskfs_dir_entry_2 *de;
6278 struct dentry *dentry;
6279 struct inode *inode;
6280 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
6283 bool dotdot = false;
6287 if (ent->oied_name[0] == '.') {
6288 if (ent->oied_namelen == 1)
6291 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
6295 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
6296 inode = osd_iget(info, dev, id);
6297 if (IS_ERR(inode)) {
6298 rc = PTR_ERR(inode);
6299 if (rc == -ENOENT || rc == -ESTALE) {
6300 /* Maybe dangling name entry, or
6301 * corrupted directory entry. */
6302 *attr |= LUDA_UNKNOWN;
6305 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
6306 "check_repair, dir = %lu/%u, name = %.*s, "
6307 "ino = %llu, rc = %d\n",
6308 devname, dir->i_ino, dir->i_generation,
6309 ent->oied_namelen, ent->oied_name,
6316 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
6318 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
6319 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
6324 /* We need to ensure that the name entry is still valid.
6325 * Because it may be removed or renamed by other already.
6327 * The unlink or rename operation will start journal before PDO lock,
6328 * so to avoid deadlock, here we need to start journal handle before
6329 * related PDO lock also. But because we do not know whether there
6330 * will be something to be repaired before PDO lock, we just start
6331 * journal without conditions.
6333 * We may need to remove the name entry firstly, then insert back.
6334 * One credit is for user quota file update.
6335 * One credit is for group quota file update.
6336 * Two credits are for dirty inode. */
6337 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
6338 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
6340 if (dev->od_dirent_journal != 0) {
6343 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
6346 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
6347 "check_repair, dir = %lu/%u, credits = %d, "
6348 "name = %.*s, ino = %llu: rc = %d\n",
6349 devname, dir->i_ino, dir->i_generation, credits,
6350 ent->oied_namelen, ent->oied_name,
6353 GOTO(out_inode, rc);
6356 if (obj->oo_hl_head != NULL) {
6357 hlock = osd_oti_get(env)->oti_hlock;
6358 /* "0" means exclusive lock for the whole directory.
6359 * We need to prevent others access such name entry
6360 * during the delete + insert. Neither HLOCK_ADD nor
6361 * HLOCK_DEL cannot guarantee the atomicity. */
6362 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
6364 down_write(&obj->oo_ext_idx_sem);
6367 if (obj->oo_hl_head != NULL) {
6368 hlock = osd_oti_get(env)->oti_hlock;
6369 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
6370 LDISKFS_HLOCK_LOOKUP);
6372 down_read(&obj->oo_ext_idx_sem);
6376 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6377 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
6378 *attr |= LUDA_IGNORE;
6383 /* For dotdot entry, if there is not enough space to hold the
6384 * FID-in-dirent, just keep them there. It only happens when the
6385 * device upgraded from 1.8 or restored from MDT file-level backup.
6386 * For the whole directory, only dotdot entry have no FID-in-dirent
6387 * and needs to get FID from LMA when readdir, it will not affect the
6388 * performance much. */
6389 if (dotdot && !osd_dotdot_has_space(de)) {
6390 *attr |= LUDA_UNKNOWN;
6396 if (lu_fid_eq(fid, &lma->lma_self_fid))
6399 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
6400 struct lu_fid *tfid = &lma->lma_self_fid;
6402 if (likely(dotdot &&
6403 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
6404 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
6405 /* It must be REMOTE_PARENT_DIR and as the
6406 * 'dotdot' entry of remote directory */
6407 *attr |= LUDA_IGNORE;
6409 CDEBUG(D_LFSCK, "%s: expect remote agent "
6410 "parent directory, but got %.*s under "
6411 "dir = %lu/%u with the FID "DFID"\n",
6412 devname, ent->oied_namelen,
6413 ent->oied_name, dir->i_ino,
6414 dir->i_generation, PFID(tfid));
6416 *attr |= LUDA_UNKNOWN;
6423 if (!fid_is_zero(fid)) {
6424 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
6426 if (rc == -ENOENT ||
6428 !(dev->od_scrub.os_scrub.os_file.sf_flags & SF_UPGRADE))) {
6429 /* linkEA does not recognize the dirent entry,
6430 * it may because the dirent entry corruption
6431 * and points to other's inode. */
6432 CDEBUG(D_LFSCK, "%s: the target inode does not "
6433 "recognize the dirent, dir = %lu/%u, "
6434 " name = %.*s, ino = %llu, "
6435 DFID": rc = %d\n", devname, dir->i_ino,
6436 dir->i_generation, ent->oied_namelen,
6437 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6438 *attr |= LUDA_UNKNOWN;
6443 if (rc && rc != -ENODATA) {
6444 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
6445 "dir = %lu/%u, name = %.*s, ino = %llu, "
6446 DFID": rc = %d\n", devname, dir->i_ino,
6447 dir->i_generation, ent->oied_namelen,
6448 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6449 *attr |= LUDA_UNKNOWN;
6456 /* linkEA recognizes the dirent entry, the FID-in-LMA is
6457 * valid, trusted, in spite of fid_is_sane(fid) or not. */
6458 if (*attr & LUDA_VERIFY_DRYRUN) {
6459 *fid = lma->lma_self_fid;
6460 *attr |= LUDA_REPAIR;
6467 dev->od_dirent_journal = 1;
6468 if (hlock != NULL) {
6469 ldiskfs_htree_unlock(hlock);
6472 up_read(&obj->oo_ext_idx_sem);
6478 *fid = lma->lma_self_fid;
6480 /* Update or append the FID-in-dirent. */
6481 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6482 bh, de, hlock, dotdot);
6484 *attr |= LUDA_REPAIR;
6486 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
6487 "the dirent, dir = %lu/%u, name = %.*s, "
6488 "ino = %llu, "DFID": rc = %d\n",
6489 devname, dir->i_ino, dir->i_generation,
6490 ent->oied_namelen, ent->oied_name,
6491 ent->oied_ino, PFID(fid), rc);
6493 /* lma is NULL, trust the FID-in-dirent if it is valid. */
6494 if (*attr & LUDA_VERIFY_DRYRUN) {
6495 if (fid_is_sane(fid)) {
6496 *attr |= LUDA_REPAIR;
6497 } else if (dev->od_index == 0) {
6498 lu_igif_build(fid, inode->i_ino,
6499 inode->i_generation);
6500 *attr |= LUDA_UPGRADE;
6508 dev->od_dirent_journal = 1;
6509 if (hlock != NULL) {
6510 ldiskfs_htree_unlock(hlock);
6513 up_read(&obj->oo_ext_idx_sem);
6520 if (unlikely(fid_is_sane(fid))) {
6521 /* FID-in-dirent exists, but FID-in-LMA is lost.
6522 * Trust the FID-in-dirent, and add FID-in-LMA. */
6523 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
6525 *attr |= LUDA_REPAIR;
6527 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
6528 "update dirent, dir = %lu/%u, "
6529 "name = %.*s, ino = %llu, "
6531 devname, dir->i_ino, dir->i_generation,
6532 ent->oied_namelen, ent->oied_name,
6533 ent->oied_ino, PFID(fid), rc);
6534 } else if (dev->od_index == 0) {
6535 lu_igif_build(fid, inode->i_ino, inode->i_generation);
6536 /* It is probably IGIF object. Only aappend the
6537 * FID-in-dirent. OI scrub will process FID-in-LMA. */
6538 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6539 bh, de, hlock, dotdot);
6541 *attr |= LUDA_UPGRADE;
6543 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
6544 "after the dirent, dir = %lu/%u, "
6545 "name = %.*s, ino = %llu, "
6547 devname, dir->i_ino, dir->i_generation,
6548 ent->oied_namelen, ent->oied_name,
6549 ent->oied_ino, PFID(fid), rc);
6558 if (hlock != NULL) {
6559 ldiskfs_htree_unlock(hlock);
6561 if (dev->od_dirent_journal != 0)
6562 up_write(&obj->oo_ext_idx_sem);
6564 up_read(&obj->oo_ext_idx_sem);
6568 ldiskfs_journal_stop(jh);
6572 if (rc >= 0 && !dirty)
6573 dev->od_dirent_journal = 0;
6579 * Returns the value at current position from iterator's in memory structure.
6581 * \param di struct osd_it_ea, iterator's in memory structure
6582 * \param attr attr requested for dirent.
6583 * \param lde lustre dirent
6585 * \retval 0 no error and \param lde has correct lustre dirent.
6586 * \retval -ve on error
6588 static inline int osd_it_ea_rec(const struct lu_env *env,
6589 const struct dt_it *di,
6590 struct dt_rec *dtrec, __u32 attr)
6592 struct osd_it_ea *it = (struct osd_it_ea *)di;
6593 struct osd_object *obj = it->oie_obj;
6594 struct osd_device *dev = osd_obj2dev(obj);
6595 struct osd_thread_info *oti = osd_oti_get(env);
6596 struct osd_inode_id *id = &oti->oti_id;
6597 struct lu_fid *fid = &it->oie_dirent->oied_fid;
6598 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6599 __u32 ino = it->oie_dirent->oied_ino;
6603 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
6605 if (attr & LUDA_VERIFY) {
6606 if (unlikely(ino == osd_remote_parent_ino(dev))) {
6607 attr |= LUDA_IGNORE;
6608 /* If the parent is on remote MDT, and there
6609 * is no FID-in-dirent, then we have to get
6610 * the parent FID from the linkEA. */
6611 if (!fid_is_sane(fid) &&
6612 it->oie_dirent->oied_namelen == 2 &&
6613 it->oie_dirent->oied_name[0] == '.' &&
6614 it->oie_dirent->oied_name[1] == '.')
6615 osd_get_pfid_from_linkea(env, obj, fid);
6617 rc = osd_dirent_check_repair(env, obj, it, fid, id,
6621 if (!fid_is_sane(fid))
6622 attr |= LUDA_UNKNOWN;
6624 attr &= ~LU_DIRENT_ATTRS_MASK;
6625 if (!fid_is_sane(fid)) {
6626 bool is_dotdot = false;
6627 if (it->oie_dirent->oied_namelen == 2 &&
6628 it->oie_dirent->oied_name[0] == '.' &&
6629 it->oie_dirent->oied_name[1] == '.')
6631 /* If the parent is on remote MDT, and there
6632 * is no FID-in-dirent, then we have to get
6633 * the parent FID from the linkEA. */
6634 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
6635 rc = osd_get_pfid_from_linkea(env, obj, fid);
6637 if (is_dotdot == false &&
6638 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
6641 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6644 osd_id_gen(id, ino, OSD_OII_NOGEN);
6648 /* Pack the entry anyway, at least the offset is right. */
6649 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
6650 it->oie_dirent->oied_name,
6651 it->oie_dirent->oied_namelen,
6652 it->oie_dirent->oied_type, attr);
6657 if (osd_remote_fid(env, dev, fid))
6660 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
6661 osd_add_oi_cache(oti, dev, id, fid);
6663 RETURN(rc > 0 ? 0 : rc);
6667 * Returns the record size size at current position.
6669 * This function will return record(lu_dirent) size in bytes.
6671 * \param[in] env execution environment
6672 * \param[in] di iterator's in memory structure
6673 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
6674 * calculate the lu_dirent size.
6676 * \retval record size(in bytes & in memory) of the current lu_dirent
6679 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
6682 struct osd_it_ea *it = (struct osd_it_ea *)di;
6684 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
6688 * Returns a cookie for current position of the iterator head, so that
6689 * user can use this cookie to load/start the iterator next time.
6691 * \param di iterator's in memory structure
6693 * \retval cookie for current position, on success
6695 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
6697 struct osd_it_ea *it = (struct osd_it_ea *)di;
6699 return it->oie_dirent->oied_off;
6703 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6704 * to load a directory entry at a time and stored it i inn,
6705 * in iterator's in-memory data structure.
6707 * \param di struct osd_it_ea, iterator's in memory structure
6709 * \retval +ve on success
6710 * \retval -ve on error
6712 static int osd_it_ea_load(const struct lu_env *env,
6713 const struct dt_it *di, __u64 hash)
6715 struct osd_it_ea *it = (struct osd_it_ea *)di;
6719 it->oie_file.f_pos = hash;
6721 rc = osd_ldiskfs_it_fill(env, di);
6732 * Index lookup function for interoperability mode (b11826).
6734 * \param key, key i.e. file name to be searched
6736 * \retval +ve, on success
6737 * \retval -ve, on error
6739 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6740 struct dt_rec *rec, const struct dt_key *key)
6742 struct osd_object *obj = osd_dt_obj(dt);
6747 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6748 LINVRNT(osd_invariant(obj));
6750 rc = osd_ea_lookup_rec(env, obj, rec, key);
6757 * Index and Iterator operations for interoperability
6758 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6760 static const struct dt_index_operations osd_index_ea_ops = {
6761 .dio_lookup = osd_index_ea_lookup,
6762 .dio_declare_insert = osd_index_declare_ea_insert,
6763 .dio_insert = osd_index_ea_insert,
6764 .dio_declare_delete = osd_index_declare_ea_delete,
6765 .dio_delete = osd_index_ea_delete,
6767 .init = osd_it_ea_init,
6768 .fini = osd_it_ea_fini,
6769 .get = osd_it_ea_get,
6770 .put = osd_it_ea_put,
6771 .next = osd_it_ea_next,
6772 .key = osd_it_ea_key,
6773 .key_size = osd_it_ea_key_size,
6774 .rec = osd_it_ea_rec,
6775 .rec_size = osd_it_ea_rec_size,
6776 .store = osd_it_ea_store,
6777 .load = osd_it_ea_load
6781 static void *osd_key_init(const struct lu_context *ctx,
6782 struct lu_context_key *key)
6784 struct osd_thread_info *info;
6786 OBD_ALLOC_PTR(info);
6788 return ERR_PTR(-ENOMEM);
6790 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6791 if (info->oti_it_ea_buf == NULL)
6794 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6796 info->oti_hlock = ldiskfs_htree_lock_alloc();
6797 if (info->oti_hlock == NULL)
6803 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6806 return ERR_PTR(-ENOMEM);
6809 static void osd_key_fini(const struct lu_context *ctx,
6810 struct lu_context_key *key, void* data)
6812 struct osd_thread_info *info = data;
6813 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6814 struct osd_idmap_cache *idc = info->oti_ins_cache;
6816 if (info->oti_inode != NULL)
6818 if (info->oti_hlock != NULL)
6819 ldiskfs_htree_lock_free(info->oti_hlock);
6820 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6821 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6822 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6823 lu_buf_free(&info->oti_big_buf);
6825 LASSERT(info->oti_ins_cache_size > 0);
6826 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6827 info->oti_ins_cache = NULL;
6828 info->oti_ins_cache_size = 0;
6833 static void osd_key_exit(const struct lu_context *ctx,
6834 struct lu_context_key *key, void *data)
6836 struct osd_thread_info *info = data;
6838 LASSERT(info->oti_r_locks == 0);
6839 LASSERT(info->oti_w_locks == 0);
6840 LASSERT(info->oti_txns == 0);
6843 /* type constructor/destructor: osd_type_init, osd_type_fini */
6844 LU_TYPE_INIT_FINI(osd, &osd_key);
6846 struct lu_context_key osd_key = {
6847 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6848 .lct_init = osd_key_init,
6849 .lct_fini = osd_key_fini,
6850 .lct_exit = osd_key_exit
6854 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6855 const char *name, struct lu_device *next)
6857 struct osd_device *osd = osd_dev(d);
6859 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6860 >= sizeof(osd->od_svname))
6862 return osd_procfs_init(osd, name);
6865 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6867 struct seq_server_site *ss = osd_seq_site(osd);
6871 if (osd->od_is_ost || osd->od_cl_seq != NULL)
6874 if (unlikely(ss == NULL))
6877 OBD_ALLOC_PTR(osd->od_cl_seq);
6878 if (osd->od_cl_seq == NULL)
6881 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6882 osd->od_svname, ss->ss_server_seq);
6884 OBD_FREE_PTR(osd->od_cl_seq);
6885 osd->od_cl_seq = NULL;
6889 if (ss->ss_node_id == 0) {
6890 /* If the OSD on the sequence controller(MDT0), then allocate
6891 * sequence here, otherwise allocate sequence after connected
6892 * to MDT0 (see mdt_register_lwp_callback()). */
6893 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6894 &osd->od_cl_seq->lcs_space, env);
6900 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6902 if (osd->od_cl_seq == NULL)
6905 seq_client_fini(osd->od_cl_seq);
6906 OBD_FREE_PTR(osd->od_cl_seq);
6907 osd->od_cl_seq = NULL;
6910 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6914 /* shutdown quota slave instance associated with the device */
6915 if (o->od_quota_slave != NULL) {
6916 qsd_fini(env, o->od_quota_slave);
6917 o->od_quota_slave = NULL;
6920 osd_fid_fini(env, o);
6921 osd_scrub_cleanup(env, o);
6926 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6930 if (o->od_mnt != NULL) {
6931 shrink_dcache_sb(osd_sb(o));
6932 osd_sync(env, &o->od_dt_dev);
6941 static int osd_mount(const struct lu_env *env,
6942 struct osd_device *o, struct lustre_cfg *cfg)
6944 const char *name = lustre_cfg_string(cfg, 0);
6945 const char *dev = lustre_cfg_string(cfg, 1);
6947 unsigned long page, s_flags, lmd_flags = 0;
6948 struct page *__page;
6949 struct file_system_type *type;
6950 char *options = NULL;
6952 struct osd_thread_info *info = osd_oti_get(env);
6953 struct lu_fid *fid = &info->oti_fid;
6954 struct inode *inode;
6955 int rc = 0, force_over_512tb = 0;
6958 if (o->od_mnt != NULL)
6961 if (strlen(dev) >= sizeof(o->od_mntdev))
6963 strcpy(o->od_mntdev, dev);
6965 str = lustre_cfg_string(cfg, 2);
6966 s_flags = simple_strtoul(str, NULL, 0);
6967 str = strstr(str, ":");
6969 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6970 opts = lustre_cfg_string(cfg, 3);
6972 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6973 CERROR("%s: device %s extents feature is not guaranteed to "
6974 "work on big-endian systems. Use \"bigendian_extents\" "
6975 "mount option to override.\n", name, dev);
6979 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6980 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
6981 CWARN("force_over_128tb option is deprecated. "
6982 "Filesystems less than 512TB can be created without any "
6983 "force options. Use force_over_512tb option for "
6984 "filesystems greater than 512TB.\n");
6987 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
6988 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
6989 CWARN("force_over_256tb option is deprecated. "
6990 "Filesystems less than 512TB can be created without any "
6991 "force options. Use force_over_512tb option for "
6992 "filesystems greater than 512TB.\n");
6996 if (opts != NULL && strstr(opts, "force_over_512tb") != NULL)
6997 force_over_512tb = 1;
6999 __page = alloc_page(GFP_KERNEL);
7001 GOTO(out, rc = -ENOMEM);
7002 page = (unsigned long)page_address(__page);
7003 options = (char *)page;
7006 /* strip out the options for back compatiblity */
7007 static char *sout[] = {
7014 /* strip out option we processed in osd */
7015 "bigendian_extents",
7021 strcat(options, opts);
7022 for (rc = 0, str = options; sout[rc]; ) {
7023 char *op = strstr(str, sout[rc]);
7029 if (op == options || *(op - 1) == ',') {
7030 str = op + strlen(sout[rc]);
7031 if (*str == ',' || *str == '\0') {
7032 *str == ',' ? str++ : str;
7033 memmove(op, str, strlen(str) + 1);
7036 for (str = op; *str != ',' && *str != '\0'; str++)
7040 strncat(options, "user_xattr,acl", 14);
7043 /* Glom up mount options */
7044 if (*options != '\0')
7045 strcat(options, ",");
7046 strlcat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
7048 type = get_fs_type("ldiskfs");
7050 CERROR("%s: cannot find ldiskfs module\n", name);
7051 GOTO(out, rc = -ENODEV);
7054 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
7055 module_put(type->owner);
7057 if (IS_ERR(o->od_mnt)) {
7058 rc = PTR_ERR(o->od_mnt);
7060 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
7064 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
7065 osd_sb(o)->s_blocksize_bits > 512ULL << 40 &&
7066 force_over_512tb == 0) {
7067 CERROR("%s: device %s LDISKFS does not support filesystems "
7068 "greater than 512TB and can cause data corruption. "
7069 "Use \"force_over_512tb\" mount option to override.\n",
7071 GOTO(out_mnt, rc = -EINVAL);
7074 if (lmd_flags & LMD_FLG_DEV_RDONLY) {
7075 if (priv_dev_set_rdonly) {
7076 priv_dev_set_rdonly(osd_sb(o)->s_bdev);
7077 o->od_dt_dev.dd_rdonly = 1;
7078 LCONSOLE_WARN("%s: set dev_rdonly on this device\n",
7081 LCONSOLE_WARN("%s: not support dev_rdonly on this device",
7084 GOTO(out_mnt, rc = -EOPNOTSUPP);
7086 } else if (priv_dev_check_rdonly &&
7087 priv_dev_check_rdonly(osd_sb(o)->s_bdev)) {
7088 CERROR("%s: underlying device %s is marked as "
7089 "read-only. Setup failed\n", name, dev);
7091 GOTO(out_mnt, rc = -EROFS);
7094 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
7095 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
7096 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
7097 GOTO(out_mnt, rc = -EINVAL);
7100 #ifdef LDISKFS_MOUNT_DIRDATA
7101 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
7102 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
7103 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
7104 else if (strstr(name, "MDT")) /* don't complain for MGT or OSTs */
7105 CWARN("%s: device %s was upgraded from Lustre-1.x without "
7106 "enabling the dirdata feature. If you do not want to "
7107 "downgrade to Lustre-1.x again, you can enable it via "
7108 "'tune2fs -O dirdata device'\n", name, dev);
7110 inode = osd_sb(o)->s_root->d_inode;
7111 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
7112 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
7114 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
7118 if (lmd_flags & LMD_FLG_NOSCRUB)
7119 o->od_auto_scrub_interval = AS_NEVER;
7129 __free_page(__page);
7134 static struct lu_device *osd_device_fini(const struct lu_env *env,
7135 struct lu_device *d)
7137 struct osd_device *o = osd_dev(d);
7140 osd_shutdown(env, o);
7142 osd_obj_map_fini(o);
7148 static int osd_device_init0(const struct lu_env *env,
7149 struct osd_device *o,
7150 struct lustre_cfg *cfg)
7152 struct lu_device *l = osd2lu_dev(o);
7153 struct osd_thread_info *info;
7157 /* if the module was re-loaded, env can loose its keys */
7158 rc = lu_env_refill((struct lu_env *) env);
7161 info = osd_oti_get(env);
7164 l->ld_ops = &osd_lu_ops;
7165 o->od_dt_dev.dd_ops = &osd_dt_ops;
7167 spin_lock_init(&o->od_osfs_lock);
7168 mutex_init(&o->od_otable_mutex);
7169 INIT_LIST_HEAD(&o->od_orphan_list);
7171 o->od_read_cache = 1;
7172 o->od_writethrough_cache = 1;
7173 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
7174 o->od_auto_scrub_interval = AS_DEFAULT;
7176 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
7177 sizeof(o->od_svname));
7178 if (cplen >= sizeof(o->od_svname)) {
7183 o->od_index = -1; /* -1 means index is invalid */
7184 rc = server_name2index(o->od_svname, &o->od_index, NULL);
7185 if (rc == LDD_F_SV_TYPE_OST)
7188 o->od_full_scrub_ratio = OFSR_DEFAULT;
7189 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
7190 rc = osd_mount(env, o, cfg);
7194 rc = osd_obj_map_init(env, o);
7198 rc = lu_site_init(&o->od_site, l);
7200 GOTO(out_compat, rc);
7201 o->od_site.ls_bottom_dev = l;
7203 rc = lu_site_init_finish(&o->od_site);
7207 INIT_LIST_HEAD(&o->od_ios_list);
7208 /* setup scrub, including OI files initialization */
7209 rc = osd_scrub_setup(env, o);
7213 rc = osd_procfs_init(o, o->od_svname);
7215 CERROR("%s: can't initialize procfs: rc = %d\n",
7217 GOTO(out_scrub, rc);
7220 LASSERT(l->ld_site->ls_linkage.next != NULL);
7221 LASSERT(l->ld_site->ls_linkage.prev != NULL);
7223 /* initialize quota slave instance */
7224 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
7226 if (IS_ERR(o->od_quota_slave)) {
7227 rc = PTR_ERR(o->od_quota_slave);
7228 o->od_quota_slave = NULL;
7229 GOTO(out_procfs, rc);
7237 osd_scrub_cleanup(env, o);
7239 lu_site_fini(&o->od_site);
7241 osd_obj_map_fini(o);
7248 static struct lu_device *osd_device_alloc(const struct lu_env *env,
7249 struct lu_device_type *t,
7250 struct lustre_cfg *cfg)
7252 struct osd_device *o;
7257 return ERR_PTR(-ENOMEM);
7259 rc = dt_device_init(&o->od_dt_dev, t);
7261 /* Because the ctx might be revived in dt_device_init,
7262 * refill the env here */
7263 lu_env_refill((struct lu_env *)env);
7264 rc = osd_device_init0(env, o, cfg);
7266 dt_device_fini(&o->od_dt_dev);
7269 if (unlikely(rc != 0))
7272 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
7275 static struct lu_device *osd_device_free(const struct lu_env *env,
7276 struct lu_device *d)
7278 struct osd_device *o = osd_dev(d);
7281 /* XXX: make osd top device in order to release reference */
7282 d->ld_site->ls_top_dev = d;
7283 lu_site_purge(env, d->ld_site, -1);
7284 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
7285 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
7286 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
7288 lu_site_fini(&o->od_site);
7289 dt_device_fini(&o->od_dt_dev);
7294 static int osd_process_config(const struct lu_env *env,
7295 struct lu_device *d, struct lustre_cfg *cfg)
7297 struct osd_device *o = osd_dev(d);
7301 switch (cfg->lcfg_command) {
7303 rc = osd_mount(env, o, cfg);
7306 lu_dev_del_linkage(d->ld_site, d);
7307 rc = osd_shutdown(env, o);
7310 LASSERT(&o->od_dt_dev);
7311 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
7312 cfg, &o->od_dt_dev);
7313 if (rc > 0 || rc == -ENOSYS) {
7314 rc = class_process_proc_param(PARAM_OST,
7315 lprocfs_osd_obd_vars,
7316 cfg, &o->od_dt_dev);
7328 static int osd_recovery_complete(const struct lu_env *env,
7329 struct lu_device *d)
7331 struct osd_device *osd = osd_dev(d);
7335 if (osd->od_quota_slave == NULL)
7338 /* start qsd instance on recovery completion, this notifies the quota
7339 * slave code that we are about to process new requests now */
7340 rc = qsd_start(env, osd->od_quota_slave);
7345 * we use exports to track all osd users
7347 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
7348 struct obd_device *obd, struct obd_uuid *cluuid,
7349 struct obd_connect_data *data, void *localdata)
7351 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7352 struct lustre_handle conn;
7356 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
7358 rc = class_connect(&conn, obd, cluuid);
7362 *exp = class_conn2export(&conn);
7364 spin_lock(&osd->od_osfs_lock);
7366 spin_unlock(&osd->od_osfs_lock);
7372 * once last export (we don't count self-export) disappeared
7373 * osd can be released
7375 static int osd_obd_disconnect(struct obd_export *exp)
7377 struct obd_device *obd = exp->exp_obd;
7378 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7379 int rc, release = 0;
7382 /* Only disconnect the underlying layers on the final disconnect. */
7383 spin_lock(&osd->od_osfs_lock);
7385 if (osd->od_connects == 0)
7387 spin_unlock(&osd->od_osfs_lock);
7389 rc = class_disconnect(exp); /* bz 9811 */
7391 if (rc == 0 && release)
7392 class_manual_cleanup(obd);
7396 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
7397 struct lu_device *dev)
7399 struct osd_device *osd = osd_dev(dev);
7400 struct lr_server_data *lsd =
7401 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
7405 if (osd->od_quota_slave != NULL) {
7406 /* set up quota slave objects */
7407 result = qsd_prepare(env, osd->od_quota_slave);
7412 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
7413 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
7414 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
7415 osd->od_index_in_idif = 1;
7417 osd->od_index_in_idif = 0;
7418 result = osd_register_proc_index_in_idif(osd);
7423 osd->od_index_in_idif = 1;
7427 result = osd_fid_init(env, osd);
7432 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
7433 struct lu_fid *fid, struct md_op_data *op_data)
7435 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
7437 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
7440 static const struct lu_object_operations osd_lu_obj_ops = {
7441 .loo_object_init = osd_object_init,
7442 .loo_object_delete = osd_object_delete,
7443 .loo_object_release = osd_object_release,
7444 .loo_object_free = osd_object_free,
7445 .loo_object_print = osd_object_print,
7446 .loo_object_invariant = osd_object_invariant
7449 const struct lu_device_operations osd_lu_ops = {
7450 .ldo_object_alloc = osd_object_alloc,
7451 .ldo_process_config = osd_process_config,
7452 .ldo_recovery_complete = osd_recovery_complete,
7453 .ldo_prepare = osd_prepare,
7456 static const struct lu_device_type_operations osd_device_type_ops = {
7457 .ldto_init = osd_type_init,
7458 .ldto_fini = osd_type_fini,
7460 .ldto_start = osd_type_start,
7461 .ldto_stop = osd_type_stop,
7463 .ldto_device_alloc = osd_device_alloc,
7464 .ldto_device_free = osd_device_free,
7466 .ldto_device_init = osd_device_init,
7467 .ldto_device_fini = osd_device_fini
7470 static struct lu_device_type osd_device_type = {
7471 .ldt_tags = LU_DEVICE_DT,
7472 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
7473 .ldt_ops = &osd_device_type_ops,
7474 .ldt_ctx_tags = LCT_LOCAL,
7477 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
7479 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7480 struct super_block *sb = osd_sb(osd);
7482 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
7486 * lprocfs legacy support.
7488 static struct obd_ops osd_obd_device_ops = {
7489 .o_owner = THIS_MODULE,
7490 .o_connect = osd_obd_connect,
7491 .o_disconnect = osd_obd_disconnect,
7492 .o_fid_alloc = osd_fid_alloc,
7493 .o_health_check = osd_health_check,
7496 static int __init osd_init(void)
7500 CLASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
7501 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
7502 /* please, try to keep osd_thread_info smaller than a page */
7503 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
7508 rc = lu_kmem_init(ldiskfs_caches);
7512 #ifdef CONFIG_KALLSYMS
7513 priv_dev_set_rdonly = (void *)kallsyms_lookup_name("dev_set_rdonly");
7514 priv_dev_check_rdonly = (void *)kallsyms_lookup_name("dev_check_rdonly");
7515 /* Clear readonly is unused at this time */
7516 /*priv_dev_clear_rdonly = (void *)kallsyms_lookup_name("dev_clear_rdonly");*/
7519 rc = class_register_type(&osd_obd_device_ops, NULL, true,
7520 lprocfs_osd_module_vars,
7521 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
7523 lu_kmem_fini(ldiskfs_caches);
7527 static void __exit osd_exit(void)
7529 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
7530 lu_kmem_fini(ldiskfs_caches);
7533 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7534 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
7535 MODULE_VERSION(LUSTRE_VERSION_STRING);
7536 MODULE_LICENSE("GPL");
7538 module_init(osd_init);
7539 module_exit(osd_exit);