4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/osd/osd_handler.c
33 * Top-level entry points into osd module
35 * Author: Nikita Danilov <nikita@clusterfs.com>
36 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
39 #define DEBUG_SUBSYSTEM S_OSD
41 #include <linux/fs_struct.h>
42 #include <linux/module.h>
43 #include <linux/user_namespace.h>
44 #include <linux/uidgid.h>
45 #ifdef HAVE_INODE_IVERSION
46 #include <linux/iversion.h>
48 #define inode_peek_iversion(__inode) ((__inode)->i_version)
51 /* prerequisite for linux/xattr.h */
52 #include <linux/types.h>
53 /* prerequisite for linux/xattr.h */
55 /* XATTR_{REPLACE,CREATE} */
56 #include <linux/xattr.h>
58 #include <ldiskfs/ldiskfs.h>
59 #include <ldiskfs/xattr.h>
60 #include <ldiskfs/ldiskfs_extents.h>
63 * struct OBD_{ALLOC,FREE}*()
65 #include <obd_support.h>
66 #include <libcfs/libcfs.h>
67 /* struct ptlrpc_thread */
68 #include <lustre_net.h>
69 #include <lustre_fid.h>
71 #include <uapi/linux/lustre/lustre_param.h>
72 #include <uapi/linux/lustre/lustre_disk.h>
74 #include "osd_internal.h"
75 #include "osd_dynlocks.h"
77 /* llo_* api support */
78 #include <md_object.h>
79 #include <lustre_quota.h>
81 #include <lustre_linkea.h>
83 /* encoding routines */
84 #include <lustre_crypto.h>
86 /* Maximum EA size is limited by LNET_MTU for remote objects */
87 #define OSD_MAX_EA_SIZE 1048364
90 module_param(ldiskfs_pdo, int, 0644);
91 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
93 int ldiskfs_track_declares_assert;
94 module_param(ldiskfs_track_declares_assert, int, 0644);
95 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
97 /* 1 GiB in 512-byte sectors */
98 int ldiskfs_delayed_unlink_blocks = (1 << (30 - 9));
100 /* Slab to allocate dynlocks */
101 struct kmem_cache *dynlock_cachep;
103 /* Slab to allocate osd_it_ea */
104 struct kmem_cache *osd_itea_cachep;
106 static struct lu_kmem_descr ldiskfs_caches[] = {
108 .ckd_cache = &biop_cachep,
109 .ckd_name = "biop_cache",
110 .ckd_size = sizeof(struct osd_bio_private)
113 .ckd_cache = &dynlock_cachep,
114 .ckd_name = "dynlock_cache",
115 .ckd_size = sizeof(struct dynlock_handle)
118 .ckd_cache = &osd_itea_cachep,
119 .ckd_name = "osd_itea_cache",
120 .ckd_size = sizeof(struct osd_it_ea)
127 static atomic_t osd_mount_seq;
129 static const char dot[] = ".";
130 static const char dotdot[] = "..";
132 static const struct lu_object_operations osd_lu_obj_ops;
133 static const struct dt_object_operations osd_obj_ops;
134 static const struct dt_object_operations osd_obj_otable_it_ops;
135 static const struct dt_index_operations osd_index_iam_ops;
136 static const struct dt_index_operations osd_index_ea_ops;
138 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
139 const struct lu_fid *fid);
140 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
141 struct osd_device *osd);
142 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
143 const struct lu_buf *buf, const char *name, int fl,
144 struct thandle *handle);
146 int osd_trans_declare_op2rb[] = {
147 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
148 [OSD_OT_PUNCH] = OSD_OT_MAX,
149 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
150 [OSD_OT_CREATE] = OSD_OT_DESTROY,
151 [OSD_OT_DESTROY] = OSD_OT_CREATE,
152 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
153 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
154 [OSD_OT_WRITE] = OSD_OT_WRITE,
155 [OSD_OT_INSERT] = OSD_OT_DELETE,
156 [OSD_OT_DELETE] = OSD_OT_INSERT,
157 [OSD_OT_QUOTA] = OSD_OT_MAX,
160 static int osd_has_index(const struct osd_object *obj)
162 return obj->oo_dt.do_index_ops != NULL;
165 static int osd_object_invariant(const struct lu_object *l)
167 return osd_invariant(osd_obj(l));
171 * Concurrency: doesn't matter
173 static int osd_is_write_locked(const struct lu_env *env, struct osd_object *o)
175 struct osd_thread_info *oti = osd_oti_get(env);
177 return oti->oti_w_locks > 0 && o->oo_owner == env;
181 * Concurrency: doesn't access mutable data
183 static int osd_root_get(const struct lu_env *env,
184 struct dt_device *dev, struct lu_fid *f)
186 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
191 * the following set of functions are used to maintain per-thread
192 * cache of FID->ino mapping. this mechanism is needed to resolve
193 * FID to inode at dt_insert() which in turn stores ino in the
194 * directory entries to keep ldiskfs compatible with ext[34].
195 * due to locking-originated restrictions we can't lookup ino
196 * using LU cache (deadlock is possible). lookup using OI is quite
197 * expensive. so instead we maintain this cache and methods like
198 * dt_create() fill it. so in the majority of cases dt_insert() is
199 * able to find needed mapping in lockless manner.
201 static struct osd_idmap_cache *
202 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
203 const struct lu_fid *fid)
205 struct osd_thread_info *oti = osd_oti_get(env);
206 struct osd_idmap_cache *idc = oti->oti_ins_cache;
209 for (i = 0; i < oti->oti_ins_cache_used; i++) {
210 if (!lu_fid_eq(&idc[i].oic_fid, fid))
212 if (idc[i].oic_dev != osd)
221 static struct osd_idmap_cache *
222 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
223 const struct lu_fid *fid)
225 struct osd_thread_info *oti = osd_oti_get(env);
226 struct osd_idmap_cache *idc;
229 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
230 i = oti->oti_ins_cache_size * 2;
232 i = OSD_INS_CACHE_SIZE;
233 OBD_ALLOC_PTR_ARRAY_LARGE(idc, i);
235 return ERR_PTR(-ENOMEM);
236 if (oti->oti_ins_cache != NULL) {
237 memcpy(idc, oti->oti_ins_cache,
238 oti->oti_ins_cache_used * sizeof(*idc));
239 OBD_FREE_PTR_ARRAY_LARGE(oti->oti_ins_cache,
240 oti->oti_ins_cache_used);
242 oti->oti_ins_cache = idc;
243 oti->oti_ins_cache_size = i;
246 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
249 idc->oic_lid.oii_ino = 0;
250 idc->oic_lid.oii_gen = 0;
257 * lookup mapping for the given fid in the cache, initialize a
258 * new one if not found. the initialization checks whether the
259 * object is local or remote. for local objects, OI is used to
260 * learn ino/generation. the function is used when the caller
261 * has no information about the object, e.g. at dt_insert().
263 static struct osd_idmap_cache *
264 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
265 const struct lu_fid *fid)
267 struct osd_idmap_cache *idc;
270 idc = osd_idc_find(env, osd, fid);
271 LASSERT(!IS_ERR(idc));
275 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
276 osd->od_svname, PFID(fid));
278 /* new mapping is needed */
279 idc = osd_idc_add(env, osd, fid);
281 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
282 osd->od_svname, PFID(fid), PTR_ERR(idc));
287 rc = osd_remote_fid(env, osd, fid);
288 if (unlikely(rc < 0))
292 /* the object is local, lookup in OI */
293 /* XXX: probably cheaper to lookup in LU first? */
294 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
296 if (unlikely(rc < 0)) {
297 CERROR("can't lookup: rc = %d\n", rc);
301 /* the object is remote */
308 static void osd_idc_dump_lma(const struct lu_env *env,
309 struct osd_device *osd,
313 struct osd_thread_info *info = osd_oti_get(env);
314 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
315 const struct lu_fid *fid;
316 struct osd_inode_id lid;
320 inode = osd_ldiskfs_iget(osd_sb(osd), ino);
322 CERROR("%s: can't get inode %lu: rc = %d\n",
323 osd->od_svname, ino, (int)PTR_ERR(inode));
326 if (is_bad_inode(inode)) {
327 CERROR("%s: bad inode %lu\n", osd->od_svname, ino);
330 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
332 CERROR("%s: can't get LMA for %lu: rc = %d\n",
333 osd->od_svname, ino, rc);
336 fid = &loa->loa_lma.lma_self_fid;
337 LCONSOLE(D_INFO, "%s: "DFID" in inode %lu/%u\n", osd->od_svname,
338 PFID(fid), ino, (unsigned)inode->i_generation);
341 rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
343 CERROR("%s: can't lookup "DFID": rc = %d\n",
344 osd->od_svname, PFID(fid), rc);
347 LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n", osd->od_svname,
348 PFID(fid), lid.oii_ino, lid.oii_gen);
353 static void osd_idc_dump_debug(const struct lu_env *env,
354 struct osd_device *osd,
355 const struct lu_fid *fid,
359 struct osd_inode_id lid;
363 rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
365 LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n",
366 osd->od_svname, PFID(fid), lid.oii_ino, lid.oii_gen);
367 osd_idc_dump_lma(env, osd, lid.oii_ino, false);
369 CERROR("%s: can't lookup "DFID": rc = %d\n",
370 osd->od_svname, PFID(fid), rc);
373 osd_idc_dump_lma(env, osd, ino1, true);
375 osd_idc_dump_lma(env, osd, ino2, true);
379 * lookup mapping for given FID and fill it from the given object.
380 * the object is lolcal by definition.
382 static int osd_idc_find_and_init(const struct lu_env *env,
383 struct osd_device *osd,
384 struct osd_object *obj)
386 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
387 struct osd_idmap_cache *idc;
389 idc = osd_idc_find(env, osd, fid);
390 LASSERT(!IS_ERR(idc));
392 if (obj->oo_inode == NULL)
394 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
395 if (idc->oic_lid.oii_ino) {
396 osd_idc_dump_debug(env, osd, fid,
397 idc->oic_lid.oii_ino,
398 obj->oo_inode->i_ino);
401 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
402 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
407 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
408 osd->od_svname, PFID(fid));
410 /* new mapping is needed */
411 idc = osd_idc_add(env, osd, fid);
413 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
414 osd->od_svname, PFID(fid), PTR_ERR(idc));
418 if (obj->oo_inode != NULL) {
419 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
420 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
426 * OSD object methods.
430 * Concurrency: no concurrent access is possible that early in object
433 static struct lu_object *osd_object_alloc(const struct lu_env *env,
434 const struct lu_object_header *hdr,
437 struct osd_object *mo;
442 struct lu_object_header *h;
443 struct osd_device *o = osd_dev(d);
445 l = &mo->oo_dt.do_lu;
446 if (unlikely(o->od_in_init)) {
453 lu_object_header_init(h);
454 lu_object_init(l, h, d);
455 lu_object_add_top(h, l);
458 dt_object_init(&mo->oo_dt, NULL, d);
459 mo->oo_header = NULL;
462 mo->oo_dt.do_ops = &osd_obj_ops;
463 l->lo_ops = &osd_lu_obj_ops;
464 init_rwsem(&mo->oo_sem);
465 init_rwsem(&mo->oo_ext_idx_sem);
466 spin_lock_init(&mo->oo_guard);
467 INIT_LIST_HEAD(&mo->oo_xattr_list);
473 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
474 struct dentry *dentry, struct lustre_ost_attrs *loa)
478 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
479 (void *)loa, sizeof(*loa));
481 struct lustre_mdt_attrs *lma = &loa->loa_lma;
483 if (rc < sizeof(*lma))
487 lustre_loa_swab(loa, true);
488 /* Check LMA compatibility */
489 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
491 CWARN("%s: unsupported incompat LMA feature(s) %#x for fid = "DFID", ino = %lu: rc = %d\n",
493 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
494 PFID(&lma->lma_self_fid), inode->i_ino, rc);
496 } else if (rc == 0) {
504 * retrieve object from backend ext fs.
506 static struct inode *osd_iget2(struct osd_thread_info *info,
507 struct osd_device *dev, struct osd_inode_id *id,
511 struct inode *inode = NULL;
514 * if we look for an inode withing a running
515 * transaction, then we risk to deadlock
516 * osd_dirent_check_repair() breaks this
518 /* LASSERT(current->journal_info == NULL); */
520 inode = osd_ldiskfs_iget(osd_sb(dev), id->oii_ino);
522 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
523 id->oii_ino, PTR_ERR(inode));
524 } else if (id->oii_gen != OSD_OII_NOGEN &&
525 inode->i_generation != id->oii_gen) {
526 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
527 "i_generation = %u\n",
528 id->oii_ino, id->oii_gen, inode->i_generation);
530 } else if (inode->i_nlink == 0) {
532 * due to parallel readdir and unlink,
533 * we can have dead inode here.
535 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
537 } else if (is_bad_inode(inode)) {
538 CWARN("%s: bad inode: ino = %u: rc = %d\n",
539 osd_dev2name(dev), id->oii_ino, -ENOENT);
541 } else if (osd_is_ea_inode(inode)) {
543 * EA inode is internal ldiskfs object, should don't visible
546 CDEBUG(D_INODE, "EA inode: ino = %u\n", id->oii_ino);
548 } else if ((rc = osd_attach_jinode(inode))) {
549 CDEBUG(D_INODE, "jbd: ino = %u rc = %d\n", id->oii_ino, rc);
551 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
552 if (id->oii_gen == OSD_OII_NOGEN)
553 osd_id_gen(id, inode->i_ino, inode->i_generation);
556 * Do not update file c/mtime in ldiskfs.
557 * NB: we don't have any lock to protect this because we don't
558 * have reference on osd_object now, but contention with
559 * another lookup + attr_set can't happen in the tiny window
560 * between if (...) and set S_NOCMTIME.
562 if (!(inode->i_flags & S_NOCMTIME))
563 inode->i_flags |= S_NOCMTIME;
571 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
572 struct osd_inode_id *id)
577 inode = osd_iget2(info, dev, id, &rc);
587 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
588 handle_t *handle, struct dentry *child,
589 struct inode *inode, struct htree_lock *hlock)
593 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
594 if (rc == -ENOBUFS || rc == -ENOSPC) {
595 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
596 struct inode *parent = child->d_parent->d_inode;
597 struct lu_fid *fid = NULL;
598 char fidstr[FID_LEN + 1] = "unknown";
600 rc2 = osd_get_lma(info, parent, child->d_parent, loa);
602 fid = &loa->loa_lma.lma_self_fid;
603 } else if (rc2 == -ENODATA) {
604 if (unlikely(is_root_inode(parent))) {
605 fid = &info->oti_fid3;
606 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
607 } else if (!osd->od_is_ost && osd->od_index == 0) {
608 fid = &info->oti_fid3;
609 lu_igif_build(fid, parent->i_ino,
610 parent->i_generation);
615 snprintf(fidstr, sizeof(fidstr), DFID, PFID(fid));
617 /* below message is checked in sanity.sh test_129 */
619 CWARN("%s: directory (inode: %lu, FID: %s) has reached max size limit\n",
620 osd_name(osd), parent->i_ino, fidstr);
622 rc = 0; /* ignore such error now */
623 CWARN("%s: directory (inode: %lu, FID: %s) is approaching max size limit\n",
624 osd_name(osd), parent->i_ino, fidstr);
634 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
635 struct osd_inode_id *id, struct lu_fid *fid)
637 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
641 inode = osd_iget(info, dev, id);
645 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
647 *fid = loa->loa_lma.lma_self_fid;
648 } else if (rc == -ENODATA) {
649 if (unlikely(is_root_inode(inode)))
650 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
652 lu_igif_build(fid, inode->i_ino, inode->i_generation);
660 static struct inode *osd_iget_check(struct osd_thread_info *info,
661 struct osd_device *dev,
662 const struct lu_fid *fid,
663 struct osd_inode_id *id,
672 * The cached OI mapping is trustable. If we cannot locate the inode
673 * via the cached OI mapping, then return the failure to the caller
674 * directly without further OI checking.
678 inode = osd_iget2(info, dev, id, &rc);
680 if (!trusted && (rc == -ENOENT || rc == -ESTALE))
683 CDEBUG(D_INODE, "no inode for FID: "DFID", ino = %u, rc = %d\n",
684 PFID(fid), id->oii_ino, rc);
690 __u32 saved_ino = id->oii_ino;
691 __u32 saved_gen = id->oii_gen;
694 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
696 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
698 * XXX: There are four possible cases:
700 * Backup/restore caused the OI invalid.
702 * Someone unlinked the object but NOT removed
703 * the OI mapping, such as mount target device
704 * as ldiskfs, and modify something directly.
706 * Someone just removed the object between the
707 * former oi_lookup and the iget. It is normal.
708 * 4. Other failure cases.
710 * Generally, when the device is mounted, it will
711 * auto check whether the system is restored from
712 * file-level backup or not. We trust such detect
713 * to distinguish the 1st case from the 2nd case:
714 * if the OI files are consistent but may contain
715 * stale OI mappings because of case 2, if iget()
716 * returns -ENOENT or -ESTALE, then it should be
721 * If the OI mapping was in OI file before the
722 * osd_iget_check(), but now, it is disappear,
723 * then it must be removed by race. That is a
729 * It is the OI scrub updated the OI mapping by race.
730 * The new OI mapping must be valid.
732 if (saved_ino != id->oii_ino ||
733 (saved_gen != id->oii_gen && saved_gen != OSD_OII_NOGEN)) {
742 if (dev->od_scrub.os_scrub.os_file.sf_flags &
745 * It still can be the case 2, but we cannot
746 * distinguish it from the case 1. So return
747 * -EREMCHG to block current operation until
748 * OI scrub rebuilt the OI mappings.
757 if (inode->i_generation == id->oii_gen)
777 * \retval +v: new filter_fid does not contain self-fid
778 * \retval 0: filter_fid_18_23, contains self-fid
779 * \retval -v: other failure cases
781 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
782 struct dentry *dentry, struct lu_fid *fid)
784 struct filter_fid *ff = &info->oti_ff;
785 struct ost_id *ostid = &info->oti_ostid;
788 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
789 if (rc == sizeof(struct filter_fid_18_23)) {
790 struct filter_fid_18_23 *ff_old = (void *)ff;
792 ostid_set_seq(ostid, le64_to_cpu(ff_old->ff_seq));
793 rc = ostid_set_id(ostid, le64_to_cpu(ff_old->ff_objid));
795 * XXX: use 0 as the index for compatibility, the caller will
796 * handle index related issues when necessary.
799 ostid_to_fid(fid, ostid, 0);
800 } else if (rc >= (int)sizeof(struct filter_fid_24_29)) {
802 } else if (rc >= 0) {
809 static int osd_lma_self_repair(struct osd_thread_info *info,
810 struct osd_device *osd, struct inode *inode,
811 const struct lu_fid *fid, __u32 compat)
816 LASSERT(current->journal_info == NULL);
818 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
819 osd_dto_credits_noquota[DTO_XATTR_SET]);
822 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
827 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
829 CWARN("%s: cannot self repair the LMA: rc = %d\n",
831 ldiskfs_journal_stop(jh);
835 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
837 struct osd_thread_info *info = osd_oti_get(env);
838 struct osd_device *osd = osd_obj2dev(obj);
839 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
840 struct lustre_mdt_attrs *lma = &loa->loa_lma;
841 struct inode *inode = obj->oo_inode;
842 struct dentry *dentry = &info->oti_obj_dentry;
843 struct lu_fid *fid = NULL;
844 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
849 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
850 (void *)loa, sizeof(*loa));
851 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
852 fid = &lma->lma_self_fid;
853 rc = osd_get_idif(info, inode, dentry, fid);
854 if (rc > 0 || (rc == -ENODATA && osd->od_index_in_idif)) {
856 * For the given OST-object, if it has neither LMA nor
857 * FID in XATTR_NAME_FID, then the given FID (which is
858 * contained in the @obj, from client RPC for locating
859 * the OST-object) is trusted. We use it to generate
862 osd_lma_self_repair(info, osd, inode, rfid,
873 lustre_lma_swab(lma);
874 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
875 (CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT) &&
876 S_ISREG(inode->i_mode)))) {
877 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
878 "fid = "DFID", ino = %lu\n", osd_name(osd),
879 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
880 PFID(rfid), inode->i_ino);
883 fid = &lma->lma_self_fid;
884 if (lma->lma_compat & LMAC_STRIPE_INFO &&
886 obj->oo_pfid_in_lma = 1;
887 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
889 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
893 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
894 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
895 struct ost_id *oi = &info->oti_ostid;
896 struct lu_fid *fid1 = &info->oti_fid3;
897 __u32 idx = fid_idif_ost_idx(rfid);
900 * For old IDIF, the OST index is not part of the IDIF,
901 * Means that different OSTs may have the same IDIFs.
902 * Under such case, we need to make some compatible
903 * check to make sure to trigger OI scrub properly.
905 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
906 /* Given @rfid is new, LMA is old. */
907 fid_to_ostid(fid, oi);
908 ostid_to_fid(fid1, oi, idx);
909 if (lu_fid_eq(fid1, rfid)) {
910 if (osd->od_index_in_idif)
911 osd_lma_self_repair(info, osd,
925 struct osd_check_lmv_buf {
926 /* please keep it as first member */
927 struct dir_context ctx;
928 struct osd_thread_info *oclb_info;
929 struct osd_device *oclb_dev;
935 * It is called internally by ->iterate*() to filter out the
936 * local slave object's FID of the striped directory.
938 * \retval 1 found the local slave's FID
939 * \retval 0 continue to check next item
940 * \retval -ve for failure
942 #ifdef HAVE_FILLDIR_USE_CTX
943 static FILLDIR_TYPE do_osd_stripe_dir_filldir(struct dir_context *buf,
945 static int osd_stripe_dir_filldir(void *buf,
947 const char *name, int namelen,
948 loff_t offset, __u64 ino, unsigned int d_type)
950 struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
951 struct osd_thread_info *oti = oclb->oclb_info;
952 struct lu_fid *fid = &oti->oti_fid3;
953 struct osd_inode_id *id = &oti->oti_id3;
954 struct osd_inode_id id2;
955 struct osd_device *dev = oclb->oclb_dev;
964 sscanf(name + 1, SFID, RFID(fid));
965 if (!fid_is_sane(fid))
968 if (osd_remote_fid(oti->oti_env, dev, fid))
971 osd_id_gen(id, ino, OSD_OII_NOGEN);
972 inode = osd_iget(oti, dev, id);
974 return PTR_ERR(inode);
977 osd_add_oi_cache(oti, dev, id, fid);
978 /* Check shard by scrub only if it has a problem with OI */
979 if (osd_oi_lookup(oti, dev, fid, &id2, 0) || !osd_id_eq(id, &id2))
980 osd_scrub_oi_insert(dev, fid, id, true);
981 oclb->oclb_found = true;
986 WRAP_FILLDIR_FN(do_, osd_stripe_dir_filldir)
989 * When lookup item under striped directory, we need to locate the master
990 * MDT-object of the striped directory firstly, then the client will send
991 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
992 * and the item's name. If the system is restored from MDT file level backup,
993 * then before the OI scrub completely built the OI files, the OI mappings of
994 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
995 * not a problem for the master MDT-object. Because when locate the master
996 * MDT-object, we will do name based lookup (for the striped directory itself)
997 * firstly, during such process we can setup the correct OI mapping for the
998 * master MDT-object. But it will be trouble for the slave MDT-object. Because
999 * the client will not trigger name based lookup on the MDT to locate the slave
1000 * MDT-object before locating item under the striped directory, then when
1001 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
1002 * is invalid and does not know what the right OI mapping is, then the MDT has
1003 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
1004 * the OI file, related OI mapping is unknown yet, please try again later. And
1005 * then client will re-try the RPC again and again until related OI mapping has
1006 * been updated. That is quite inefficient.
1008 * To resolve above trouble, we will handle it as the following two cases:
1010 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
1011 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
1012 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
1013 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
1014 * directly. Please check osd_fid_lookup().
1016 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
1017 * Under such case, during lookup the master MDT-object, we will lookup the
1018 * slave MDT-object via readdir against the master MDT-object, because the
1019 * slave MDT-objects information are stored as sub-directories with the name
1020 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
1021 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
1022 * the correct OI mapping for the slave MDT-object.
1024 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
1025 struct inode *inode)
1027 struct lu_buf *buf = &oti->oti_big_buf;
1029 struct lmv_mds_md_v1 *lmv1;
1030 struct osd_check_lmv_buf oclb = {
1031 .ctx.actor = osd_stripe_dir_filldir,
1034 .oclb_found = false,
1039 /* We should use the VFS layer to create a real dentry. */
1040 oti->oti_obj_dentry.d_inode = inode;
1041 oti->oti_obj_dentry.d_sb = inode->i_sb;
1043 filp = alloc_file_pseudo(inode, dev->od_mnt, "/", O_NOATIME,
1048 filp->f_mode |= FMODE_64BITHASH;
1052 rc = __osd_xattr_get(inode, filp->f_path.dentry, XATTR_NAME_LMV,
1053 buf->lb_buf, buf->lb_len);
1054 if (rc == -ERANGE) {
1055 rc = __osd_xattr_get(inode, filp->f_path.dentry,
1056 XATTR_NAME_LMV, NULL, 0);
1058 lu_buf_realloc(buf, rc);
1059 if (buf->lb_buf == NULL)
1060 GOTO(out, rc = -ENOMEM);
1066 if (unlikely(rc == 0 || rc == -ENODATA))
1072 if (unlikely(buf->lb_buf == NULL)) {
1073 lu_buf_realloc(buf, rc);
1074 if (buf->lb_buf == NULL)
1075 GOTO(out, rc = -ENOMEM);
1081 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1085 oclb.oclb_items = 0;
1086 rc = iterate_dir(filp, &oclb.ctx);
1087 } while (rc >= 0 && oclb.oclb_items > 0 && !oclb.oclb_found &&
1088 filp->f_pos != LDISKFS_HTREE_EOF_64BIT);
1093 "%s: cannot check LMV, ino = %lu/%u: rc = %d\n",
1094 osd_ino2name(inode), inode->i_ino, inode->i_generation,
1103 * Is object in scrub inconsistent/stale list.
1105 * \a scrub has two lists, os_inconsistent_items contains mappings to fix, while
1106 * os_stale_items contains mappings failed to fix.
1108 static bool fid_in_scrub_list(struct lustre_scrub *scrub,
1109 const struct list_head *list,
1110 const struct lu_fid *fid)
1112 struct osd_inconsistent_item *oii;
1114 if (list_empty(list))
1117 spin_lock(&scrub->os_lock);
1118 list_for_each_entry(oii, list, oii_list) {
1119 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
1120 spin_unlock(&scrub->os_lock);
1124 spin_unlock(&scrub->os_lock);
1129 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
1130 const struct lu_fid *fid,
1131 const struct lu_object_conf *conf)
1133 struct osd_thread_info *info;
1134 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1135 struct osd_device *dev;
1136 struct osd_idmap_cache *oic;
1137 struct osd_inode_id *id;
1138 struct inode *inode = NULL;
1139 struct lustre_scrub *scrub;
1140 struct scrub_file *sf;
1141 __u32 flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT | SS_AUTO_FULL;
1146 bool remote = false;
1147 bool trusted = true;
1148 bool updated = false;
1149 bool checked = false;
1154 LINVRNT(osd_invariant(obj));
1155 LASSERT(obj->oo_inode == NULL);
1157 if (fid_is_sane(fid) == 0) {
1158 CERROR("%s: invalid FID "DFID"\n", ldev->ld_obd->obd_name,
1164 dev = osd_dev(ldev);
1165 scrub = &dev->od_scrub.os_scrub;
1166 sf = &scrub->os_file;
1167 info = osd_oti_get(env);
1169 oic = &info->oti_cache;
1171 if (CFS_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
1175 * For the object is created as locking anchor, or for the object to
1176 * be created on disk. No need to osd_oi_lookup() at here because FID
1177 * shouldn't never be re-used, if it's really a duplicate FID from
1178 * unexpected reason, we should be able to detect it later by calling
1179 * do_create->osd_oi_insert().
1181 if (conf && conf->loc_flags & LOC_F_NEW)
1184 /* Search order: 1. per-thread cache. */
1185 if (lu_fid_eq(fid, &oic->oic_fid) && likely(oic->oic_dev == dev)) {
1190 /* Search order: 2. OI scrub pending list. */
1192 memset(id, 0, sizeof(struct osd_inode_id));
1193 if (fid_in_scrub_list(scrub, &scrub->os_inconsistent_items, fid) &&
1195 RETURN(-EINPROGRESS);
1197 stale = fid_in_scrub_list(scrub, &scrub->os_stale_items, fid);
1198 if (stale && CFS_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_STALE))
1202 * The OI mapping in the OI file can be updated by the OI scrub
1203 * when we locate the inode via FID. So it may be not trustable.
1207 /* Search order: 3. OI files. */
1208 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1209 if (result == -ENOENT) {
1210 if (!fid_is_norm(fid) ||
1211 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1212 !ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
1214 GOTO(out, result = 0);
1219 /* -ESTALE is returned if inode of OST object doesn't exist */
1220 if (result == -ESTALE &&
1221 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1222 GOTO(out, result = 0);
1229 obj->oo_inode = NULL;
1230 /* for later passes through checks, not true on first pass */
1231 if (!IS_ERR_OR_NULL(inode))
1234 inode = osd_iget_check(info, dev, fid, id, trusted);
1235 if (!IS_ERR(inode)) {
1236 obj->oo_inode = inode;
1244 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_STALE))
1247 result = PTR_ERR(inode);
1248 if (result == -ENOENT || result == -ESTALE)
1249 GOTO(out, result = 0);
1251 if (result != -EREMCHG)
1255 /* don't trigger repeatedly for stale mapping */
1257 GOTO(out, result = -ESTALE);
1260 * We still have chance to get the valid inode: for the
1261 * object which is referenced by remote name entry, the
1262 * object on the local MDT will be linked under the dir
1263 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1265 * We do not know whether the object for the given FID
1266 * is referenced by some remote name entry or not, and
1267 * especially for DNE II, a multiple-linked object may
1268 * have many name entries reside on many MDTs.
1270 * To simplify the operation, OSD will not distinguish
1271 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1272 * only happened for the RPC from other MDT during the
1273 * OI scrub, or for the client side RPC with FID only,
1274 * such as FID to path, or from old connected client.
1277 rc1 = osd_lookup_in_remote_parent(info, dev, fid, id);
1281 flags |= SS_AUTO_PARTIAL;
1282 flags &= ~SS_AUTO_FULL;
1287 if (scrub->os_running) {
1288 if (scrub->os_partial_scan && !scrub->os_in_join)
1291 if (IS_ERR_OR_NULL(inode) || result) {
1292 osd_scrub_oi_insert(dev, fid, id, result == -ENOENT);
1293 GOTO(out, result = -EINPROGRESS);
1297 LASSERT(obj->oo_inode == inode);
1299 osd_scrub_oi_insert(dev, fid, id, true);
1303 if (dev->od_scrub.os_scrub.os_auto_scrub_interval == AS_NEVER) {
1305 GOTO(out, result = -EREMCHG);
1308 LASSERT(obj->oo_inode == inode);
1310 osd_add_oi_cache(info, dev, id, fid);
1315 if (IS_ERR_OR_NULL(inode) || result)
1316 osd_scrub_oi_insert(dev, fid, id, result == -ENOENT);
1318 rc1 = osd_scrub_start(env, dev, flags);
1319 CDEBUG_LIMIT(D_LFSCK | D_CONSOLE | D_WARNING,
1320 "%s: trigger OI scrub by RPC for "DFID"/%u with flags %#x: rc = %d\n",
1321 osd_name(dev), PFID(fid), id->oii_ino, flags, rc1);
1322 if (rc1 && rc1 != -EALREADY)
1323 GOTO(out, result = -EREMCHG);
1325 if (IS_ERR_OR_NULL(inode) || result)
1326 GOTO(out, result = -EINPROGRESS);
1329 LASSERT(obj->oo_inode == inode);
1334 if (unlikely(obj->oo_header))
1337 result = osd_check_lma(env, obj);
1341 LASSERTF(id->oii_ino == inode->i_ino &&
1342 id->oii_gen == inode->i_generation,
1343 "locate wrong inode for FID: "DFID", %u/%u => %ld/%u\n",
1344 PFID(fid), id->oii_ino, id->oii_gen,
1345 inode->i_ino, inode->i_generation);
1347 saved_ino = inode->i_ino;
1348 saved_gen = inode->i_generation;
1350 if (unlikely(result == -ENODATA)) {
1352 * If the OI scrub updated the OI mapping by race, it
1353 * must be valid. Trust the inode that has no LMA EA.
1358 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1361 * The OI mapping is still there, the inode is still
1362 * valid. It is just becaues the inode has no LMA EA.
1364 if (saved_ino == id->oii_ino &&
1365 saved_gen == id->oii_gen)
1369 * It is the OI scrub updated the OI mapping by race.
1370 * The new OI mapping must be valid.
1378 * "result == -ENOENT" means that the OI mappinghas been
1379 * removed by race, so the inode belongs to other object.
1381 * Others error can be returned directly.
1383 if (result == -ENOENT) {
1384 obj->oo_inode = NULL;
1389 if (result != -EREMCHG)
1395 * if two OST objects map to the same inode, and inode mode is
1396 * (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666), which means it's
1397 * reserved by precreate, and not written yet, in this case, don't
1398 * set inode for the object whose FID mismatch, so that it can create
1399 * inode and not block precreate.
1401 if (fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) &&
1402 inode->i_mode == (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666)) {
1403 obj->oo_inode = NULL;
1404 GOTO(out, result = 0);
1407 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1409 * "result == -ENOENT" means the cached OI mapping has been removed
1410 * from the OI file by race, above inode belongs to other object.
1412 if (result == -ENOENT) {
1413 obj->oo_inode = NULL;
1414 GOTO(out, result = 0);
1420 if (saved_ino == id->oii_ino && saved_gen == id->oii_gen) {
1422 osd_scrub_refresh_mapping(info, dev, fid, id, DTO_INDEX_DELETE,
1428 * It is the OI scrub updated the OI mapping by race.
1429 * The new OI mapping must be valid.
1437 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
1438 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
1440 result = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
1442 if (lma->lma_compat & LMAC_STRIPE_INFO &&
1444 obj->oo_pfid_in_lma = 1;
1445 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
1447 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
1448 } else if (result != -ENODATA) {
1453 obj->oo_compat_dot_created = 1;
1454 obj->oo_compat_dotdot_created = 1;
1456 if (S_ISDIR(inode->i_mode) &&
1457 (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1458 osd_check_lmv(info, dev, inode);
1460 result = osd_attach_jinode(inode);
1465 GOTO(out, result = 0);
1467 LASSERT(!obj->oo_hl_head);
1468 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1470 GOTO(out, result = (!obj->oo_hl_head ? -ENOMEM : 0));
1473 if (!result && stale)
1474 osd_scrub_oi_resurrect(scrub, fid);
1476 if (result || !obj->oo_inode) {
1477 if (!IS_ERR_OR_NULL(inode))
1480 obj->oo_inode = NULL;
1482 fid_zero(&oic->oic_fid);
1485 LINVRNT(osd_invariant(obj));
1490 * Concurrency: shouldn't matter.
1492 static void osd_object_init0(struct osd_object *obj)
1494 LASSERT(obj->oo_inode != NULL);
1495 obj->oo_dt.do_body_ops = &osd_body_ops;
1496 obj->oo_dt.do_lu.lo_header->loh_attr |=
1497 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1501 * Concurrency: no concurrent access is possible that early in object
1504 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1505 const struct lu_object_conf *conf)
1507 struct osd_object *obj = osd_obj(l);
1510 LINVRNT(osd_invariant(obj));
1512 if (CFS_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
1513 cfs_fail_val == 2) {
1514 struct osd_thread_info *info = osd_oti_get(env);
1515 struct osd_idmap_cache *oic = &info->oti_cache;
1516 /* invalidate thread cache */
1517 memset(&oic->oic_fid, 0, sizeof(oic->oic_fid));
1519 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1520 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1521 l->lo_header->loh_attr |= LOHA_EXISTS;
1525 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1526 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1527 if (result == 0 && obj->oo_inode != NULL) {
1528 struct osd_thread_info *oti = osd_oti_get(env);
1529 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
1531 osd_object_init0(obj);
1532 if (unlikely(obj->oo_header))
1535 result = osd_get_lma(oti, obj->oo_inode,
1536 &oti->oti_obj_dentry, loa);
1539 * Convert LMAI flags to lustre LMA flags
1540 * and cache it to oo_lma_flags
1543 lma_to_lustre_flags(loa->loa_lma.lma_incompat);
1544 } else if (result == -ENODATA) {
1548 obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
1550 LINVRNT(osd_invariant(obj));
1555 * The first part of oxe_buf is xattr name, and is '\0' terminated.
1556 * The left part is for value, binary mode.
1558 struct osd_xattr_entry {
1559 struct list_head oxe_list;
1563 struct rcu_head oxe_rcu;
1567 static int osd_oxc_get(struct osd_object *obj, const char *name,
1570 struct osd_xattr_entry *tmp;
1571 struct osd_xattr_entry *oxe = NULL;
1572 size_t namelen = strlen(name);
1576 list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1577 if (namelen == tmp->oxe_namelen &&
1578 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1585 GOTO(out, rc = -ENOENT);
1587 if (!oxe->oxe_exist)
1588 GOTO(out, rc = -ENODATA);
1591 rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1594 if (buf->lb_buf == NULL)
1597 if (buf->lb_len < rc)
1598 GOTO(out, rc = -ERANGE);
1600 memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1607 static void osd_oxc_free(struct rcu_head *head)
1609 struct osd_xattr_entry *oxe;
1611 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1612 OBD_FREE(oxe, oxe->oxe_len);
1615 static void osd_oxc_add(struct osd_object *obj, const char *name,
1616 const char *buf, int buflen)
1618 struct osd_xattr_entry *oxe;
1619 struct osd_xattr_entry *old = NULL;
1620 struct osd_xattr_entry *tmp;
1621 size_t namelen = strlen(name);
1622 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1624 OBD_ALLOC(oxe, len);
1628 INIT_LIST_HEAD(&oxe->oxe_list);
1630 oxe->oxe_namelen = namelen;
1631 memcpy(oxe->oxe_buf, name, namelen);
1633 LASSERT(buf != NULL);
1634 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1635 oxe->oxe_exist = true;
1637 oxe->oxe_exist = false;
1640 /* this should be rarely called, just remove old and add new */
1641 spin_lock(&obj->oo_guard);
1642 list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1643 if (namelen == tmp->oxe_namelen &&
1644 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1650 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1651 call_rcu(&old->oxe_rcu, osd_oxc_free);
1653 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1655 spin_unlock(&obj->oo_guard);
1658 static void osd_oxc_del(struct osd_object *obj, const char *name)
1660 struct osd_xattr_entry *oxe;
1661 size_t namelen = strlen(name);
1663 spin_lock(&obj->oo_guard);
1664 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1665 if (namelen == oxe->oxe_namelen &&
1666 strncmp(name, oxe->oxe_buf, namelen) == 0) {
1667 list_del_rcu(&oxe->oxe_list);
1668 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1672 spin_unlock(&obj->oo_guard);
1675 static void osd_oxc_fini(struct osd_object *obj)
1677 struct osd_xattr_entry *oxe, *next;
1679 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1680 list_del(&oxe->oxe_list);
1681 OBD_FREE(oxe, oxe->oxe_len);
1686 * Concurrency: no concurrent access is possible that late in object
1689 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1691 struct osd_object *obj = osd_obj(l);
1692 struct lu_object_header *h = obj->oo_header;
1694 LINVRNT(osd_invariant(obj));
1697 dt_object_fini(&obj->oo_dt);
1698 if (obj->oo_hl_head != NULL)
1699 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1700 /* obj doesn't contain an lu_object_header, so we don't need call_rcu */
1703 lu_object_header_free(h);
1707 * Concurrency: no concurrent access is possible that late in object
1710 static void osd_index_fini(struct osd_object *o)
1712 struct iam_container *bag;
1714 if (o->oo_dir != NULL) {
1715 bag = &o->oo_dir->od_container;
1716 if (o->oo_inode != NULL) {
1717 if (bag->ic_object == o->oo_inode)
1718 iam_container_fini(bag);
1720 OBD_FREE_PTR(o->oo_dir);
1726 OSD_TXN_OI_DELETE_CREDITS = 20,
1727 OSD_TXN_INODE_DELETE_CREDITS = 20
1734 #if OSD_THANDLE_STATS
1736 * Set time when the handle is allocated
1738 static void osd_th_alloced(struct osd_thandle *oth)
1740 oth->oth_alloced = ktime_get();
1744 * Set time when the handle started
1746 static void osd_th_started(struct osd_thandle *oth)
1748 oth->oth_started = ktime_get();
1752 * Check whether the we deal with this handle for too long.
1754 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1755 ktime_t alloced, ktime_t started,
1758 ktime_t now = ktime_get();
1760 LASSERT(dev != NULL);
1762 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1763 ktime_us_delta(started, alloced));
1764 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1765 ktime_us_delta(closed, started));
1766 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1767 ktime_us_delta(now, closed));
1769 if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1770 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1771 oth, now, alloced, started, closed);
1772 libcfs_debug_dumpstack(NULL);
1776 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1778 ktime_t __closed = ktime_get(); \
1779 ktime_t __alloced = oth->oth_alloced; \
1780 ktime_t __started = oth->oth_started; \
1783 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1786 #else /* OSD_THANDLE_STATS */
1788 #define osd_th_alloced(h) do {} while(0)
1789 #define osd_th_started(h) do {} while(0)
1790 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1792 #endif /* OSD_THANDLE_STATS */
1795 * in some cases (like overstriped files) the same operations on the same
1796 * objects are declared many times and this may lead to huge number of
1797 * credits which can be a problem and/or cause performance degradation.
1798 * this function is to remember what declarations have been made within
1799 * a given thandle and then skip duplications.
1800 * limit it's scope so that regular small transactions don't need all
1801 * this overhead with allocations, lists.
1802 * also, limit scope to the specific objects like llogs, etc.
1804 static inline bool osd_check_special_fid(const struct lu_fid *f)
1806 if (fid_seq_is_llog(f->f_seq))
1808 if (f->f_seq == FID_SEQ_LOCAL_FILE &&
1809 f->f_oid == MDD_LOV_OBJ_OID)
1814 bool osd_tx_was_declared(const struct lu_env *env, struct osd_thandle *oth,
1815 struct dt_object *dt, enum dt_txn_op op, loff_t pos)
1817 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1818 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1819 struct osd_thread_info *oti = osd_oti_get(env);
1820 struct osd_obj_declare *old;
1825 /* small transactions don't need this overhead */
1826 if (oti->oti_declare_ops[DTO_OBJECT_CREATE] < 10 &&
1827 oti->oti_declare_ops[DTO_WRITE_BASE] < 10)
1830 if (osd_check_special_fid(fid) == 0)
1833 list_for_each_entry(old, &oth->ot_declare_list, old_list) {
1834 if (old->old_op == op && old->old_pos == pos &&
1835 lu_fid_eq(&old->old_fid, fid))
1839 if (unlikely(old == NULL))
1841 old->old_fid = *lu_object_fid(&dt->do_lu);
1844 list_add(&old->old_list, &oth->ot_declare_list);
1848 void osd_tx_declaration_free(struct osd_thandle *oth)
1850 struct osd_obj_declare *old, *tmp;
1852 list_for_each_entry_safe(old, tmp, &oth->ot_declare_list, old_list) {
1853 list_del_init(&old->old_list);
1859 * Concurrency: doesn't access mutable data.
1861 static int osd_param_is_not_sane(const struct osd_device *dev,
1862 const struct thandle *th)
1864 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1866 return oh->ot_credits > osd_transaction_size(dev);
1870 * Concurrency: shouldn't matter.
1872 static void osd_trans_commit_cb(struct super_block *sb,
1873 struct ldiskfs_journal_cb_entry *jcb, int error)
1875 struct osd_thandle *oh = container_of(jcb, struct osd_thandle, ot_jcb);
1876 struct thandle *th = &oh->ot_super;
1877 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1878 struct osd_device *osd = osd_dev(lud);
1879 struct dt_txn_commit_cb *dcb, *tmp;
1881 LASSERT(oh->ot_handle == NULL);
1884 CERROR("transaction @0x%p commit error: %d\n", th, error);
1886 /* call per-transaction callbacks if any */
1887 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1889 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1890 "commit callback entry: magic=%x name='%s'\n",
1891 dcb->dcb_magic, dcb->dcb_name);
1892 list_del_init(&dcb->dcb_linkage);
1893 dcb->dcb_func(NULL, th, dcb, error);
1896 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1897 if (atomic_dec_and_test(&osd->od_commit_cb_in_flight))
1898 wake_up(&osd->od_commit_cb_done);
1904 static struct thandle *osd_trans_create(const struct lu_env *env,
1905 struct dt_device *d)
1907 struct osd_thread_info *oti = osd_oti_get(env);
1908 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1909 struct osd_thandle *oh;
1915 CERROR("%s: someone try to start transaction under "
1916 "readonly mode, should be disabled.\n",
1917 osd_name(osd_dt_dev(d)));
1919 RETURN(ERR_PTR(-EROFS));
1922 /* on pending IO in this thread should left from prev. request */
1923 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1925 sb_start_write(osd_sb(osd_dt_dev(d)));
1927 OBD_ALLOC_GFP(oh, sizeof(*oh), GFP_NOFS);
1929 sb_end_write(osd_sb(osd_dt_dev(d)));
1930 RETURN(ERR_PTR(-ENOMEM));
1933 oh->ot_quota_trans = &oti->oti_quota_trans;
1934 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1939 oh->oh_declared_ext = 0;
1940 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1941 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1942 INIT_LIST_HEAD(&oh->ot_trunc_locks);
1943 INIT_LIST_HEAD(&oh->ot_declare_list);
1946 memset(oti->oti_declare_ops, 0,
1947 sizeof(oti->oti_declare_ops));
1948 memset(oti->oti_declare_ops_cred, 0,
1949 sizeof(oti->oti_declare_ops_cred));
1950 memset(oti->oti_declare_ops_used, 0,
1951 sizeof(oti->oti_declare_ops_used));
1953 oti->oti_ins_cache_depth++;
1958 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1960 struct osd_thread_info *oti = osd_oti_get(env);
1961 struct osd_thandle *oh;
1963 oh = container_of(th, struct osd_thandle, ot_super);
1964 LASSERT(oh != NULL);
1966 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1967 oti->oti_declare_ops[OSD_OT_CREATE],
1968 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1969 oti->oti_declare_ops_used[OSD_OT_CREATE],
1970 oti->oti_declare_ops[OSD_OT_DESTROY],
1971 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1972 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1973 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1974 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1975 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1976 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1977 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1978 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1979 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1980 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1981 oti->oti_declare_ops[OSD_OT_WRITE],
1982 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1983 oti->oti_declare_ops_used[OSD_OT_WRITE],
1984 oti->oti_declare_ops[OSD_OT_PUNCH],
1985 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1986 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1987 oti->oti_declare_ops[OSD_OT_QUOTA],
1988 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1989 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1990 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1991 oti->oti_declare_ops[OSD_OT_INSERT],
1992 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1993 oti->oti_declare_ops_used[OSD_OT_INSERT],
1994 oti->oti_declare_ops[OSD_OT_DELETE],
1995 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1996 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1997 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1998 oti->oti_declare_ops[OSD_OT_REF_ADD],
1999 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
2000 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
2001 oti->oti_declare_ops[OSD_OT_REF_DEL],
2002 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
2003 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
2007 * Concurrency: shouldn't matter.
2009 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
2012 struct osd_thread_info *oti = osd_oti_get(env);
2013 struct osd_device *dev = osd_dt_dev(d);
2015 struct osd_thandle *oh;
2020 LASSERT(current->journal_info == NULL);
2022 oh = container_of(th, struct osd_thandle, ot_super);
2023 LASSERT(oh != NULL);
2024 LASSERT(oh->ot_handle == NULL);
2025 if (unlikely(ldiskfs_track_declares_assert != 0)) {
2026 LASSERT(oti->oti_r_locks == 0);
2027 LASSERT(oti->oti_w_locks == 0);
2030 rc = dt_txn_hook_start(env, d, th);
2034 if (unlikely(osd_param_is_not_sane(dev, th))) {
2035 static unsigned long last_printed;
2036 static int last_credits;
2038 lprocfs_counter_add(dev->od_stats,
2039 LPROC_OSD_TOO_MANY_CREDITS, 1);
2042 * don't make noise on a tiny testing systems
2043 * actual credits misuse will be caught anyway
2045 if (last_credits != oh->ot_credits &&
2046 time_after(jiffies, last_printed +
2047 cfs_time_seconds(60)) &&
2048 osd_transaction_size(dev) > 512) {
2049 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
2050 oh->ot_credits, osd_transaction_size(dev));
2051 osd_trans_dump_creds(env, th);
2052 libcfs_debug_dumpstack(NULL);
2053 last_credits = oh->ot_credits;
2054 last_printed = jiffies;
2057 * XXX Limit the credits to 'max_transaction_buffers', and
2058 * let the underlying filesystem to catch the error if
2059 * we really need so many credits.
2061 * This should be removed when we can calculate the
2062 * credits precisely.
2064 oh->ot_credits = osd_transaction_size(dev);
2065 } else if (ldiskfs_track_declares_assert != 0) {
2067 * reserve few credits to prevent an assertion in JBD
2068 * our debugging mechanism will be able to detected
2069 * overuse. this can help to debug single-update
2072 oh->ot_credits += 10;
2073 if (unlikely(osd_param_is_not_sane(dev, th)))
2074 oh->ot_credits = osd_transaction_size(dev);
2077 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_TXN_START))
2078 GOTO(out, rc = -EIO);
2081 * we ignore quota checks for system-owned files, but still
2082 * need to count blocks for uid/gid/projid
2084 osd_trans_declare_op(env, oh, OSD_OT_QUOTA, 3);
2087 * XXX temporary stuff. Some abstraction layer should
2090 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
2094 LASSERT(oti->oti_txns == 0);
2096 atomic_inc(&dev->od_commit_cb_in_flight);
2097 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
2108 static int osd_seq_exists(const struct lu_env *env,
2109 struct osd_device *osd, u64 seq)
2111 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2112 struct seq_server_site *ss = osd_seq_site(osd);
2117 LASSERT(ss != NULL);
2118 LASSERT(ss->ss_server_fld != NULL);
2120 rc = osd_fld_lookup(env, osd, seq, range);
2123 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
2124 osd_name(osd), seq, rc);
2128 RETURN(ss->ss_node_id == range->lsr_index);
2131 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
2133 struct dt_txn_commit_cb *dcb;
2134 struct dt_txn_commit_cb *tmp;
2136 /* call per-transaction stop callbacks if any */
2137 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
2139 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
2140 "commit callback entry: magic=%x name='%s'\n",
2141 dcb->dcb_magic, dcb->dcb_name);
2142 list_del_init(&dcb->dcb_linkage);
2143 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
2148 * Concurrency: shouldn't matter.
2150 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
2153 struct osd_thread_info *oti = osd_oti_get(env);
2154 struct osd_thandle *oh;
2155 struct osd_iobuf *iobuf = &oti->oti_iobuf;
2156 struct osd_device *osd = osd_dt_dev(th->th_dev);
2157 struct qsd_instance *qsd = osd_def_qsd(osd);
2158 struct lquota_trans *qtrans;
2159 LIST_HEAD(truncates);
2160 int rc = 0, remove_agents = 0;
2164 oh = container_of(th, struct osd_thandle, ot_super);
2166 remove_agents = oh->ot_remove_agents;
2168 qtrans = oh->ot_quota_trans;
2169 oh->ot_quota_trans = NULL;
2171 osd_tx_declaration_free(oh);
2173 /* move locks to local list, stop tx, execute truncates */
2174 list_splice(&oh->ot_trunc_locks, &truncates);
2176 if (oh->ot_handle != NULL) {
2179 handle_t *hdl = oh->ot_handle;
2182 * add commit callback
2183 * notice we don't do this in osd_trans_start()
2184 * as underlying transaction can change during truncate
2186 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
2189 LASSERT(oti->oti_txns == 1);
2192 rc = dt_txn_hook_stop(env, th);
2194 CERROR("%s: failed in transaction hook: rc = %d\n",
2197 osd_trans_stop_cb(oh, rc);
2198 /* hook functions might modify th_sync */
2199 hdl->h_sync = th->th_sync;
2201 oh->ot_handle = NULL;
2202 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
2204 CERROR("%s: failed to stop transaction: rc = %d\n",
2205 osd_name(osd), rc2);
2209 /* We preserve the origin behavior of ignoring any
2210 * failures with the underlying punch / truncate
2211 * operation. We do record for debugging if an error
2212 * does occur in the lctl dk logs.
2214 rc2 = osd_process_truncates(env, &truncates);
2216 CERROR("%s: failed truncate process: rc = %d\n",
2217 osd_name(osd), rc2);
2219 osd_trans_stop_cb(oh, th->th_result);
2223 osd_trunc_unlock_all(env, &truncates);
2225 /* inform the quota slave device that the transaction is stopping */
2226 qsd_op_end(env, qsd, qtrans);
2229 * as we want IO to journal and data IO be concurrent, we don't block
2230 * awaiting data IO completion in osd_do_bio(), instead we wait here
2231 * once transaction is submitted to the journal. all reqular requests
2232 * don't do direct IO (except read/write), thus this wait_event becomes
2235 * IMPORTANT: we have to wait till any IO submited by the thread is
2236 * completed otherwise iobuf may be corrupted by different request
2238 wait_event(iobuf->dr_wait,
2239 atomic_read(&iobuf->dr_numreqs) == 0);
2242 rc = iobuf->dr_error;
2244 osd_fini_iobuf(osd, iobuf);
2246 if (unlikely(remove_agents != 0))
2247 osd_process_scheduled_agent_removals(env, osd);
2249 LASSERT(oti->oti_ins_cache_depth > 0);
2250 oti->oti_ins_cache_depth--;
2251 /* reset OI cache for safety */
2252 if (oti->oti_ins_cache_depth == 0)
2253 oti->oti_ins_cache_used = 0;
2255 sb_end_write(osd_sb(osd));
2260 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
2262 struct osd_thandle *oh = container_of(th, struct osd_thandle,
2265 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
2266 LASSERT(&dcb->dcb_func != NULL);
2267 if (dcb->dcb_flags & DCB_TRANS_STOP)
2268 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
2270 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
2275 struct osd_delayed_iput_work {
2276 struct work_struct diw_work;
2277 struct inode *diw_inode;
2280 static void osd_delayed_iput_fn(struct work_struct *work)
2282 struct osd_delayed_iput_work *diwork;
2283 struct inode *inode;
2285 diwork = container_of(work, struct osd_delayed_iput_work, diw_work);
2286 inode = diwork->diw_inode;
2287 CDEBUG(D_INODE, "%s: delayed iput (ino=%lu)\n",
2288 inode->i_sb->s_id, inode->i_ino);
2290 OBD_FREE_PTR(diwork);
2293 noinline void osd_delayed_iput(struct inode *inode,
2294 struct osd_delayed_iput_work *diwork)
2299 INIT_WORK(&diwork->diw_work, osd_delayed_iput_fn);
2300 diwork->diw_inode = inode;
2301 queue_work(LDISKFS_SB(inode->i_sb)->s_misc_wq,
2307 * Called just before object is freed. Releases all resources except for
2308 * object itself (that is released by osd_object_free()).
2310 * Concurrency: no concurrent access is possible that late in object
2313 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
2315 struct osd_object *obj = osd_obj(l);
2316 struct qsd_instance *qsd = osd_def_qsd(osd_obj2dev(obj));
2317 struct inode *inode = obj->oo_inode;
2318 struct osd_delayed_iput_work *diwork = NULL;
2323 LINVRNT(osd_invariant(obj));
2326 * If object is unlinked remove fid->ino mapping from object index.
2329 osd_index_fini(obj);
2334 if (inode->i_blocks > ldiskfs_delayed_unlink_blocks)
2335 OBD_ALLOC(diwork, sizeof(*diwork));
2337 if (osd_has_index(obj) && obj->oo_dt.do_index_ops == &osd_index_iam_ops)
2338 ldiskfs_set_inode_flag(inode, LDISKFS_INODE_JOURNAL_DATA);
2340 uid = i_uid_read(inode);
2341 gid = i_gid_read(inode);
2342 projid = i_projid_read(inode);
2344 obj->oo_inode = NULL;
2345 osd_delayed_iput(inode, diwork);
2347 /* do not rebalance quota if the caller needs to release memory
2348 * otherwise qsd_refresh_usage() may went into a new ldiskfs
2349 * transaction and risk to deadlock - LU-12178 */
2350 if (current->flags & (PF_MEMALLOC | PF_KSWAPD))
2353 if (!obj->oo_header && qsd) {
2354 struct osd_thread_info *info = osd_oti_get(env);
2355 struct lquota_id_info *qi = &info->oti_qi;
2357 /* Release granted quota to master if necessary */
2358 qi->lqi_id.qid_uid = uid;
2359 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
2361 qi->lqi_id.qid_uid = gid;
2362 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
2364 qi->lqi_id.qid_uid = projid;
2365 qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
2370 * Concurrency: ->loo_object_release() is called under site spin-lock.
2372 static void osd_object_release(const struct lu_env *env,
2373 struct lu_object *l)
2375 struct osd_object *o = osd_obj(l);
2378 * nobody should be releasing a non-destroyed object with nlink=0
2379 * the API allows this, but ldiskfs doesn't like and then report
2380 * this inode as deleted
2382 LASSERT(!(o->oo_destroyed == 0 && o->oo_inode &&
2383 o->oo_inode->i_nlink == 0));
2387 * Concurrency: shouldn't matter.
2389 static int osd_object_print(const struct lu_env *env, void *cookie,
2390 lu_printer_t p, const struct lu_object *l)
2392 struct osd_object *o = osd_obj(l);
2393 struct iam_descr *d;
2395 if (o->oo_dir != NULL)
2396 d = o->oo_dir->od_container.ic_descr;
2399 return (*p)(env, cookie,
2400 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
2402 o->oo_inode ? o->oo_inode->i_ino : 0UL,
2403 o->oo_inode ? o->oo_inode->i_generation : 0,
2404 d ? d->id_ops->id_name : "plain");
2408 * Concurrency: shouldn't matter.
2410 int osd_statfs(const struct lu_env *env, struct dt_device *d,
2411 struct obd_statfs *sfs, struct obd_statfs_info *info)
2413 struct osd_device *osd = osd_dt_dev(d);
2414 struct super_block *sb = osd_sb(osd);
2415 struct kstatfs *ksfs;
2419 if (unlikely(osd->od_mnt == NULL))
2420 return -EINPROGRESS;
2422 /* osd_lproc.c call this without env, allocate ksfs for that case */
2423 if (unlikely(env == NULL)) {
2424 OBD_ALLOC_PTR(ksfs);
2428 ksfs = &osd_oti_get(env)->oti_ksfs;
2431 result = sb->s_op->statfs(sb->s_root, ksfs);
2435 statfs_pack(sfs, ksfs);
2436 if (unlikely(sb->s_flags & SB_RDONLY))
2437 sfs->os_state |= OS_STATFS_READONLY;
2439 sfs->os_state |= osd->od_nonrotational ? OS_STATFS_NONROT : 0;
2441 if (ldiskfs_has_feature_extents(sb))
2442 sfs->os_maxbytes = sb->s_maxbytes;
2444 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2447 * Reserve some space so to avoid fragmenting the filesystem too much.
2448 * Fragmentation not only impacts performance, but can also increase
2449 * metadata overhead significantly, causing grant calculation to be
2452 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2454 BUILD_BUG_ON(OSD_STATFS_RESERVED <= LDISKFS_MAX_BLOCK_SIZE);
2455 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2456 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2457 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2459 sfs->os_blocks -= reserved;
2460 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2461 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2464 if (unlikely(env == NULL))
2470 * Estimate space needed for file creations. We assume the largest filename
2471 * which is 2^64 - 1, hence a filename of 20 chars.
2472 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2474 #ifdef __LDISKFS_DIR_REC_LEN
2475 # define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2476 #elif defined LDISKFS_DIR_REC_LEN_WITH_DIR
2477 # define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20, NULL)
2479 # define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2483 * Concurrency: doesn't access mutable data.
2485 static void osd_conf_get(const struct lu_env *env,
2486 const struct dt_device *dev,
2487 struct dt_device_param *param)
2489 struct osd_device *d = osd_dt_dev(dev);
2490 struct super_block *sb = osd_sb(d);
2491 struct blk_integrity *bi = bdev_get_integrity(sb->s_bdev);
2496 * XXX should be taken from not-yet-existing fs abstraction layer.
2498 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2499 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2500 param->ddp_symlink_max = sb->s_blocksize;
2501 param->ddp_mount_type = LDD_MT_LDISKFS;
2502 if (ldiskfs_has_feature_extents(sb))
2503 param->ddp_maxbytes = sb->s_maxbytes;
2505 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2507 * inode are statically allocated, so per-inode space consumption
2508 * is the space consumed by the directory entry
2510 param->ddp_inodespace = PER_OBJ_USAGE;
2512 * EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2513 * is 128MB) which is unlikely to be hit in real life. Report a smaller
2514 * maximum length to not under-count the actual number of extents
2515 * needed for writing a file if there are sub-optimal block allocations.
2517 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 1;
2518 /* worst-case extent insertion metadata overhead */
2519 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2520 param->ddp_mntopts = 0;
2521 if (test_opt(sb, XATTR_USER))
2522 param->ddp_mntopts |= MNTOPT_USERXATTR;
2523 if (test_opt(sb, POSIX_ACL))
2524 param->ddp_mntopts |= MNTOPT_ACL;
2527 * LOD might calculate the max stripe count based on max_ea_size,
2528 * so we need take account in the overhead as well,
2529 * xattr_header + magic + xattr_entry_head
2531 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2532 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2534 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2535 if (ldiskfs_has_feature_ea_inode(sb))
2536 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2540 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2542 if (param->ddp_max_ea_size > OBD_MAX_EA_SIZE)
2543 param->ddp_max_ea_size = OBD_MAX_EA_SIZE;
2546 * Preferred RPC size for efficient disk IO. 4MB shows good
2547 * all-around performance for ldiskfs, but use bigalloc chunk size
2548 * by default if larger.
2550 #if defined(LDISKFS_CLUSTER_SIZE)
2551 if (LDISKFS_CLUSTER_SIZE(sb) > DT_DEF_BRW_SIZE)
2552 param->ddp_brw_size = LDISKFS_CLUSTER_SIZE(sb);
2555 param->ddp_brw_size = DT_DEF_BRW_SIZE;
2557 param->ddp_t10_cksum_type = 0;
2559 unsigned short interval = blk_integrity_interval(bi);
2560 name = blk_integrity_name(bi);
2570 if (strncmp(name, "T10-DIF-TYPE",
2571 sizeof("T10-DIF-TYPE") - 1) == 0) {
2572 /* also skip "1/2/3-" at end */
2573 const int type_off = sizeof("T10-DIF-TYPE.");
2574 char type_number = name[type_off - 2];
2576 if (interval != 512 && interval != 4096) {
2577 CERROR("%s: unsupported T10PI sector size %u\n",
2578 d->od_svname, interval);
2581 switch (type_number) {
2583 d->od_t10_type = OSD_T10_TYPE1;
2586 d->od_t10_type = OSD_T10_TYPE2;
2589 d->od_t10_type = OSD_T10_TYPE3;
2592 CERROR("%s: unsupported T10PI type %s\n",
2593 d->od_svname, name);
2596 if (strcmp(name + type_off, "CRC") == 0) {
2597 d->od_t10_type |= OSD_T10_TYPE_CRC;
2598 param->ddp_t10_cksum_type = interval == 512 ?
2599 OBD_CKSUM_T10CRC512 :
2601 } else if (strcmp(name + type_off, "IP") == 0) {
2602 d->od_t10_type |= OSD_T10_TYPE_IP;
2603 param->ddp_t10_cksum_type = interval == 512 ?
2604 OBD_CKSUM_T10IP512 :
2607 CERROR("%s: unsupported checksum type of T10PI type '%s'\n",
2608 d->od_svname, name);
2613 CERROR("%s: unsupported T10PI type '%s'\n",
2614 d->od_svname, name);
2619 param->ddp_has_lseek_data_hole = true;
2622 static struct vfsmount *osd_mnt_get(const struct dt_device *d)
2624 return osd_dt_dev(d)->od_mnt;
2628 * Concurrency: shouldn't matter.
2630 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2633 struct super_block *s = osd_sb(osd_dt_dev(d));
2636 down_read(&s->s_umount);
2637 rc = s->s_op->sync_fs(s, 1);
2638 up_read(&s->s_umount);
2640 CDEBUG(D_CACHE, "%s: synced OSD: rc = %d\n", osd_dt_dev(d)->od_svname,
2647 * Start commit for OSD device.
2649 * An implementation of dt_commit_async method for OSD device.
2650 * Asychronously starts underlayng fs sync and thereby a transaction
2653 * \param env environment
2654 * \param d dt device
2656 * \see dt_device_operations
2658 static int osd_commit_async(const struct lu_env *env,
2659 struct dt_device *d)
2661 struct super_block *s = osd_sb(osd_dt_dev(d));
2666 CDEBUG(D_HA, "%s: async commit OSD\n", osd_dt_dev(d)->od_svname);
2667 down_read(&s->s_umount);
2668 rc = s->s_op->sync_fs(s, 0);
2669 up_read(&s->s_umount);
2675 * Concurrency: shouldn't matter.
2677 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2679 struct super_block *sb = osd_sb(osd_dt_dev(d));
2680 struct block_device *dev = sb->s_bdev;
2681 int rc = -EOPNOTSUPP;
2685 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2686 osd_dt_dev(d)->od_svname, (long)dev, rc);
2692 * Note: we do not count into QUOTA here.
2693 * If we mount with --data_journal we may need more.
2695 const int osd_dto_credits_noquota[DTO_NR] = {
2698 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2699 * SINGLEDATA_TRANS_BLOCKS(8)
2700 * XXX Note: maybe iam need more, since iam have more level than
2703 [DTO_INDEX_INSERT] = 16,
2706 * just modify a single entry, probably merge few within a block
2708 [DTO_INDEX_DELETE] = 1,
2712 [DTO_INDEX_UPDATE] = 16,
2714 * 4(inode, inode bits, groups, GDT)
2715 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2717 [DTO_OBJECT_CREATE] = 4,
2719 * 4(inode, inode bits, groups, GDT)
2720 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2722 [DTO_OBJECT_DELETE] = 4,
2724 * Attr set credits (inode)
2726 [DTO_ATTR_SET_BASE] = 1,
2728 * Xattr set. The same as xattr of EXT3.
2729 * DATA_TRANS_BLOCKS(14)
2730 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2731 * are also counted in. Do not know why?
2733 [DTO_XATTR_SET] = 14,
2735 * credits for inode change during write.
2737 [DTO_WRITE_BASE] = 3,
2739 * credits for single block write.
2741 [DTO_WRITE_BLOCK] = 14,
2743 * Attr set credits for chown.
2744 * This is extra credits for setattr, and it is null without quota
2746 [DTO_ATTR_SET_CHOWN] = 0
2749 /* reserve or free quota for some operation */
2750 static int osd_reserve_or_free_quota(const struct lu_env *env,
2751 struct dt_device *dev,
2752 struct lquota_id_info *qi)
2754 struct osd_device *osd = osd_dt_dev(dev);
2755 struct qsd_instance *qsd = NULL;
2761 qsd = osd->od_quota_slave_dt;
2763 qsd = osd->od_quota_slave_md;
2765 rc = qsd_reserve_or_free_quota(env, qsd, qi);
2769 static const struct dt_device_operations osd_dt_ops = {
2770 .dt_root_get = osd_root_get,
2771 .dt_statfs = osd_statfs,
2772 .dt_trans_create = osd_trans_create,
2773 .dt_trans_start = osd_trans_start,
2774 .dt_trans_stop = osd_trans_stop,
2775 .dt_trans_cb_add = osd_trans_cb_add,
2776 .dt_conf_get = osd_conf_get,
2777 .dt_mnt_get = osd_mnt_get,
2778 .dt_sync = osd_sync,
2780 .dt_commit_async = osd_commit_async,
2781 .dt_reserve_or_free_quota = osd_reserve_or_free_quota,
2784 static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
2787 struct osd_object *obj = osd_dt_obj(dt);
2788 struct osd_thread_info *oti = osd_oti_get(env);
2790 LINVRNT(osd_invariant(obj));
2792 LASSERT(obj->oo_owner != env);
2793 down_read_nested(&obj->oo_sem, role);
2795 LASSERT(obj->oo_owner == NULL);
2799 static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
2802 struct osd_object *obj = osd_dt_obj(dt);
2803 struct osd_thread_info *oti = osd_oti_get(env);
2805 LINVRNT(osd_invariant(obj));
2807 LASSERT(obj->oo_owner != env);
2808 down_write_nested(&obj->oo_sem, role);
2810 LASSERT(obj->oo_owner == NULL);
2811 obj->oo_owner = env;
2815 static void osd_read_unlock(const struct lu_env *env, struct dt_object *dt)
2817 struct osd_object *obj = osd_dt_obj(dt);
2818 struct osd_thread_info *oti = osd_oti_get(env);
2820 LINVRNT(osd_invariant(obj));
2822 LASSERT(oti->oti_r_locks > 0);
2824 up_read(&obj->oo_sem);
2827 static void osd_write_unlock(const struct lu_env *env, struct dt_object *dt)
2829 struct osd_object *obj = osd_dt_obj(dt);
2830 struct osd_thread_info *oti = osd_oti_get(env);
2832 LINVRNT(osd_invariant(obj));
2834 LASSERT(obj->oo_owner == env);
2835 LASSERT(oti->oti_w_locks > 0);
2837 obj->oo_owner = NULL;
2838 up_write(&obj->oo_sem);
2841 static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
2843 struct osd_object *obj = osd_dt_obj(dt);
2845 LINVRNT(osd_invariant(obj));
2847 return obj->oo_owner == env;
2850 static void osd_inode_getattr(const struct lu_env *env,
2851 struct inode *inode, struct lu_attr *attr)
2853 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2854 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2855 LA_PROJID | LA_FLAGS | LA_NLINK | LA_RDEV |
2856 LA_BLKSIZE | LA_TYPE | LA_BTIME;
2858 attr->la_atime = inode->i_atime.tv_sec;
2859 attr->la_mtime = inode->i_mtime.tv_sec;
2860 attr->la_ctime = inode->i_ctime.tv_sec;
2861 attr->la_btime = LDISKFS_I(inode)->i_crtime.tv_sec;
2862 attr->la_mode = inode->i_mode;
2863 attr->la_size = i_size_read(inode);
2864 attr->la_blocks = inode->i_blocks;
2865 attr->la_uid = i_uid_read(inode);
2866 attr->la_gid = i_gid_read(inode);
2867 attr->la_projid = i_projid_read(inode);
2868 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2869 attr->la_nlink = inode->i_nlink;
2870 attr->la_rdev = inode->i_rdev;
2871 attr->la_blksize = 1 << inode->i_blkbits;
2872 attr->la_blkbits = inode->i_blkbits;
2874 * Ext4 did not transfer inherit flags from raw inode
2875 * to inode flags, and ext4 internally test raw inode
2876 * @i_flags directly. Instead of patching ext4, we do it here.
2878 if (LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL)
2879 attr->la_flags |= LUSTRE_PROJINHERIT_FL;
2882 static int osd_dirent_count(const struct lu_env *env, struct dt_object *dt,
2885 struct osd_object *obj = osd_dt_obj(dt);
2886 const struct dt_it_ops *iops;
2892 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
2893 LASSERT(fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)));
2895 if (obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
2896 *count = obj->oo_dirent_count;
2900 /* directory not initialized yet */
2901 if (!dt->do_index_ops) {
2906 iops = &dt->do_index_ops->dio_it;
2907 it = iops->init(env, dt, LUDA_64BITHASH);
2909 RETURN(PTR_ERR(it));
2911 rc = iops->load(env, it, 0);
2913 if (rc == -ENODATA) {
2920 rc = iops->next(env, it);
2922 for (*count = 0; rc == 0 || rc == -ESTALE; rc = iops->next(env, it)) {
2926 if (iops->key_size(env, it) == 0)
2932 obj->oo_dirent_count = *count;
2937 iops->fini(env, it);
2942 static int osd_attr_get(const struct lu_env *env, struct dt_object *dt,
2943 struct lu_attr *attr)
2945 struct osd_object *obj = osd_dt_obj(dt);
2948 if (unlikely(!dt_object_exists(dt)))
2950 if (unlikely(obj->oo_destroyed))
2953 LASSERT(!dt_object_remote(dt));
2954 LINVRNT(osd_invariant(obj));
2956 spin_lock(&obj->oo_guard);
2957 osd_inode_getattr(env, obj->oo_inode, attr);
2958 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL) {
2959 attr->la_valid |= LA_FLAGS;
2960 attr->la_flags |= LUSTRE_ORPHAN_FL;
2962 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL) {
2963 attr->la_valid |= LA_FLAGS;
2964 attr->la_flags |= LUSTRE_ENCRYPT_FL;
2966 spin_unlock(&obj->oo_guard);
2968 if (S_ISDIR(obj->oo_inode->i_mode) &&
2969 fid_is_namespace_visible(lu_object_fid(&dt->do_lu)))
2970 rc = osd_dirent_count(env, dt, &attr->la_dirent_count);
2975 static int osd_declare_attr_qid(const struct lu_env *env,
2976 struct osd_object *obj,
2977 struct osd_thandle *oh, long long bspace,
2978 qid_t old_id, qid_t new_id, bool enforce,
2982 struct osd_thread_info *info = osd_oti_get(env);
2983 struct lquota_id_info *qi = &info->oti_qi;
2985 qi->lqi_type = type;
2986 /* inode accounting */
2987 qi->lqi_is_blk = false;
2989 /* one more inode for the new id ... */
2990 qi->lqi_id.qid_uid = new_id;
2992 /* Reserve credits for the new id */
2993 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2994 if (rc == -EDQUOT || rc == -EINPROGRESS)
2999 /* and one less inode for the current id */
3000 qi->lqi_id.qid_uid = old_id;
3002 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
3003 if (rc == -EDQUOT || rc == -EINPROGRESS)
3008 /* block accounting */
3009 qi->lqi_is_blk = true;
3011 /* more blocks for the new id ... */
3012 qi->lqi_id.qid_uid = new_id;
3013 qi->lqi_space = bspace;
3015 * Credits for the new uid has been reserved, re-use "obj"
3016 * to save credit reservation.
3018 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
3019 if (rc == -EDQUOT || rc == -EINPROGRESS)
3024 /* and finally less blocks for the current uid */
3025 qi->lqi_id.qid_uid = old_id;
3026 qi->lqi_space = -bspace;
3027 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
3028 if (rc == -EDQUOT || rc == -EINPROGRESS)
3034 static int osd_declare_attr_set(const struct lu_env *env,
3035 struct dt_object *dt,
3036 const struct lu_attr *attr,
3037 struct thandle *handle)
3039 struct osd_thandle *oh;
3040 struct osd_object *obj;
3049 LASSERT(dt != NULL);
3050 LASSERT(handle != NULL);
3052 obj = osd_dt_obj(dt);
3053 LASSERT(osd_invariant(obj));
3055 oh = container_of(handle, struct osd_thandle, ot_super);
3056 LASSERT(oh->ot_handle == NULL);
3058 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
3059 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3061 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3062 osd_dto_credits_noquota[DTO_XATTR_SET]);
3064 if (attr == NULL || obj->oo_inode == NULL)
3067 bspace = obj->oo_inode->i_blocks << 9;
3068 bspace = toqb(bspace);
3071 * Changing ownership is always preformed by super user, it should not
3072 * fail with EDQUOT unless required explicitly.
3074 * We still need to call the osd_declare_qid() to calculate the journal
3075 * credits for updating quota accounting files and to trigger quota
3076 * space adjustment once the operation is completed.
3078 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
3080 uid = i_uid_read(obj->oo_inode);
3081 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
3082 rc = osd_declare_attr_qid(env, obj, oh, bspace, uid,
3083 attr->la_uid, enforce, USRQUOTA);
3087 gid = i_gid_read(obj->oo_inode);
3088 CDEBUG(D_QUOTA, "declare uid %d -> %d gid %d -> %d\n", uid,
3089 attr->la_uid, gid, attr->la_gid);
3090 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
3091 rc = osd_declare_attr_qid(env, obj, oh, bspace, gid,
3092 attr->la_gid, enforce, GRPQUOTA);
3097 #ifdef HAVE_PROJECT_QUOTA
3098 if (attr->la_valid & LA_PROJID) {
3099 __u32 projid = i_projid_read(obj->oo_inode);
3101 enforce = (attr->la_valid & LA_PROJID) &&
3102 (attr->la_projid != projid);
3103 rc = osd_declare_attr_qid(env, obj, oh, bspace,
3104 (qid_t)projid, (qid_t)attr->la_projid,
3110 /* punch must be aware we are dealing with an encrypted file */
3111 if (attr->la_valid & LA_FLAGS && attr->la_flags & LUSTRE_ENCRYPT_FL)
3112 obj->oo_lma_flags |= LUSTRE_ENCRYPT_FL;
3117 static int osd_inode_setattr(const struct lu_env *env,
3118 struct inode *inode, const struct lu_attr *attr)
3120 __u64 bits = attr->la_valid;
3122 /* Only allow set size for regular file */
3123 if (!S_ISREG(inode->i_mode))
3124 bits &= ~(LA_SIZE | LA_BLOCKS);
3129 if (bits & LA_ATIME)
3130 inode->i_atime = osd_inode_time(inode, attr->la_atime);
3131 if (bits & LA_CTIME)
3132 inode->i_ctime = osd_inode_time(inode, attr->la_ctime);
3133 if (bits & LA_MTIME)
3134 inode->i_mtime = osd_inode_time(inode, attr->la_mtime);
3135 if (bits & LA_SIZE) {
3136 spin_lock(&inode->i_lock);
3137 LDISKFS_I(inode)->i_disksize = attr->la_size;
3138 i_size_write(inode, attr->la_size);
3139 spin_unlock(&inode->i_lock);
3143 * OSD should not change "i_blocks" which is used by quota.
3144 * "i_blocks" should be changed by ldiskfs only.
3147 inode->i_mode = (inode->i_mode & S_IFMT) |
3148 (attr->la_mode & ~S_IFMT);
3150 i_uid_write(inode, attr->la_uid);
3152 i_gid_write(inode, attr->la_gid);
3153 if (bits & LA_PROJID)
3154 i_projid_write(inode, attr->la_projid);
3155 if (bits & LA_NLINK)
3156 set_nlink(inode, attr->la_nlink);
3158 inode->i_rdev = attr->la_rdev;
3160 if (bits & LA_FLAGS) {
3161 /* always keep S_NOCMTIME */
3162 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
3164 #if defined(S_ENCRYPTED)
3165 /* Always remove S_ENCRYPTED, because ldiskfs must not be
3166 * aware of encryption status. It is just stored into LMA
3167 * so that it can be forwared to client side.
3169 inode->i_flags &= ~S_ENCRYPTED;
3172 * Ext4 did not transfer inherit flags from
3173 * @inode->i_flags to raw inode i_flags when writing
3174 * flags, we do it explictly here.
3176 if (attr->la_flags & LUSTRE_PROJINHERIT_FL)
3177 LDISKFS_I(inode)->i_flags |= LUSTRE_PROJINHERIT_FL;
3179 LDISKFS_I(inode)->i_flags &= ~LUSTRE_PROJINHERIT_FL;
3184 #ifdef HAVE_PROJECT_QUOTA
3185 static int osd_transfer_project(struct inode *inode, __u32 projid,
3186 struct thandle *handle)
3188 struct super_block *sb = inode->i_sb;
3189 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
3192 struct ldiskfs_iloc iloc;
3193 struct ldiskfs_inode *raw_inode;
3194 struct dquot *transfer_to[LDISKFS_MAXQUOTAS] = { };
3196 if (!ldiskfs_has_feature_project(sb)) {
3197 LASSERT(__kprojid_val(LDISKFS_I(inode)->i_projid)
3198 == LDISKFS_DEF_PROJID);
3199 if (projid != LDISKFS_DEF_PROJID)
3205 if (LDISKFS_INODE_SIZE(sb) <= LDISKFS_GOOD_OLD_INODE_SIZE)
3208 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3209 if (projid_eq(kprojid, LDISKFS_I(inode)->i_projid))
3212 err = ldiskfs_get_inode_loc(inode, &iloc);
3216 raw_inode = ldiskfs_raw_inode(&iloc);
3217 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3218 struct osd_thandle *oh = container_of(handle,
3222 * try to expand inode size automatically.
3224 ldiskfs_mark_inode_dirty(oh->ot_handle, inode);
3225 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3233 dquot_initialize(inode);
3234 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3235 if (transfer_to[PRJQUOTA]) {
3236 lock_dquot_transfer(inode);
3237 err = __dquot_transfer(inode, transfer_to);
3238 unlock_dquot_transfer(inode);
3239 dqput(transfer_to[PRJQUOTA]);
3248 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr,
3249 struct thandle *handle)
3253 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
3254 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
3258 "executing dquot_transfer inode %ld uid %d -> %d gid %d -> %d\n",
3259 inode->i_ino, i_uid_read(inode), attr->la_uid,
3260 i_gid_read(inode), attr->la_gid);
3262 dquot_initialize(inode);
3264 if (attr->la_valid & LA_UID)
3265 iattr.ia_valid |= ATTR_UID;
3266 if (attr->la_valid & LA_GID)
3267 iattr.ia_valid |= ATTR_GID;
3268 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
3269 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
3271 lock_dquot_transfer(inode);
3272 rc = osd_dquot_transfer(&nop_mnt_idmap, inode, &iattr);
3273 unlock_dquot_transfer(inode);
3275 CERROR("%s: quota transfer failed. Is quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3276 osd_ino2name(inode), rc);
3281 /* Handle project id transfer here properly */
3282 if (attr->la_valid & LA_PROJID &&
3283 attr->la_projid != i_projid_read(inode)) {
3284 if (!projid_valid(make_kprojid(&init_user_ns, attr->la_projid)))
3286 #ifdef HAVE_PROJECT_QUOTA
3287 rc = osd_transfer_project(inode, attr->la_projid, handle);
3292 CERROR("%s: quota transfer failed. Is project enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3293 osd_ino2name(inode), rc);
3300 static int osd_attr_set(const struct lu_env *env,
3301 struct dt_object *dt,
3302 const struct lu_attr *attr,
3303 struct thandle *handle)
3305 struct osd_thread_info *info = osd_oti_get(env);
3306 struct osd_object *obj = osd_dt_obj(dt);
3307 struct osd_device *osd = osd_obj2dev(obj);
3308 struct inode *inode;
3311 if (!dt_object_exists(dt))
3314 LASSERT(handle != NULL);
3315 LASSERT(!dt_object_remote(dt));
3316 LASSERT(osd_invariant(obj));
3318 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
3320 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) &&
3321 !osd_obj2dev(obj)->od_is_ost) {
3322 struct osd_thread_info *oti = osd_oti_get(env);
3323 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
3324 struct lu_fid *fid1 = &oti->oti_fid;
3325 struct osd_inode_id *id = &oti->oti_id;
3326 struct iam_path_descr *ipd;
3327 struct iam_container *bag;
3328 struct osd_thandle *oh;
3331 fid_cpu_to_be(fid1, fid0);
3332 memset(id, 1, sizeof(*id));
3333 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
3334 fid0)->oi_dir.od_container;
3335 ipd = osd_idx_ipd_get(env, bag);
3336 if (unlikely(ipd == NULL))
3339 oh = container_of(handle, struct osd_thandle, ot_super);
3340 rc = iam_update(oh->ot_handle, bag,
3341 (const struct iam_key *)fid1,
3342 (const struct iam_rec *)id, ipd);
3343 osd_ipd_put(env, bag, ipd);
3344 return(rc > 0 ? 0 : rc);
3347 inode = obj->oo_inode;
3349 rc = osd_quota_transfer(inode, attr, handle);
3353 spin_lock(&obj->oo_guard);
3354 rc = osd_inode_setattr(env, inode, attr);
3355 spin_unlock(&obj->oo_guard);
3359 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3361 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
3363 if (!(attr->la_valid & LA_FLAGS))
3366 /* If setting LUSTRE_ENCRYPT_FL on an OST object, also set a dummy
3367 * enc ctx xattr, with 2 benefits:
3368 * - setting the LL_XATTR_NAME_ENCRYPTION_CONTEXT xattr internally sets
3369 * the LDISKFS_ENCRYPT_FL flag on the on-disk inode;
3370 * - it makes e2fsprogs happy to see an enc ctx for an inode that has
3371 * the LDISKFS_ENCRYPT_FL flag
3372 * We do not need the actual encryption context on OST objects, it is
3373 * only stored on MDT inodes, at file creation time.
3375 if (!(LDISKFS_I(obj->oo_inode)->i_flags & LDISKFS_ENCRYPT_FL) &&
3376 attr->la_flags & LUSTRE_ENCRYPT_FL && osd->od_is_ost &&
3377 !CFS_FAIL_CHECK(OBD_FAIL_LFSCK_NO_ENCFLAG)) {
3380 /* use a dummy enc ctx, fine with e2fsprogs */
3381 buf.lb_buf = "\xFF";
3383 rc = osd_xattr_set(env, dt, &buf,
3384 LL_XATTR_NAME_ENCRYPTION_CONTEXT,
3387 CWARN("%s: set "DFID" enc ctx failed: rc = %d\n",
3388 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
3392 /* Let's check if there are extra flags need to be set into LMA */
3393 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
3394 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
3396 LASSERT(!obj->oo_pfid_in_lma);
3398 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
3399 &info->oti_ost_attrs);
3403 if ((lma->lma_incompat & lustre_to_lma_flags(attr->la_flags)) ==
3404 lustre_to_lma_flags(attr->la_flags))
3405 /* if lma incompat already has the flags,
3406 * save a useless call to xattr_set
3410 lma->lma_incompat |=
3411 lustre_to_lma_flags(attr->la_flags);
3412 lustre_lma_swab(lma);
3414 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3416 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
3417 lma, sizeof(*lma), XATTR_REPLACE);
3419 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
3420 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
3421 lma->lma_incompat, rc);
3424 attr->la_flags & LUSTRE_LMA_FL_MASKS;
3425 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3432 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
3433 struct osd_object *obj,
3434 const char *name, const int namelen)
3436 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
3439 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
3440 umode_t mode, struct dt_allocation_hint *hint,
3441 struct thandle *th, struct lu_attr *attr)
3444 struct osd_device *osd = osd_obj2dev(obj);
3445 struct osd_thandle *oth;
3446 struct dt_object *parent = NULL;
3447 struct inode *inode;
3448 struct iattr iattr = {
3449 .ia_valid = ATTR_UID | ATTR_GID |
3450 ATTR_CTIME | ATTR_MTIME | ATTR_ATIME,
3451 .ia_ctime.tv_sec = attr->la_ctime,
3452 .ia_mtime.tv_sec = attr->la_mtime,
3453 .ia_atime.tv_sec = attr->la_atime,
3454 .ia_uid = GLOBAL_ROOT_UID,
3455 .ia_gid = GLOBAL_ROOT_GID,
3457 const struct osd_timespec omit = { .tv_nsec = UTIME_OMIT };
3459 if (attr->la_valid & LA_UID)
3460 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
3461 if (attr->la_valid & LA_GID)
3462 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
3464 LINVRNT(osd_invariant(obj));
3465 LASSERT(obj->oo_inode == NULL);
3466 LASSERT(obj->oo_hl_head == NULL);
3468 if (S_ISDIR(mode) && ldiskfs_pdo) {
3470 ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
3471 if (obj->oo_hl_head == NULL)
3475 oth = container_of(th, struct osd_thandle, ot_super);
3476 LASSERT(oth->ot_handle->h_transaction != NULL);
3478 if (hint != NULL && hint->dah_parent != NULL &&
3479 !dt_object_remote(hint->dah_parent))
3480 parent = hint->dah_parent;
3482 /* if a time component is not valid set it to UTIME_OMIT */
3483 if (!(attr->la_valid & LA_CTIME))
3484 iattr.ia_ctime = omit;
3485 if (!(attr->la_valid & LA_MTIME))
3486 iattr.ia_mtime = omit;
3487 if (!(attr->la_valid & LA_ATIME))
3488 iattr.ia_atime = omit;
3490 inode = ldiskfs_create_inode(oth->ot_handle,
3491 parent ? osd_dt_obj(parent)->oo_inode :
3492 osd_sb(osd)->s_root->d_inode,
3494 if (!IS_ERR(inode)) {
3495 /* Do not update file c/mtime in ldiskfs. */
3496 inode->i_flags |= S_NOCMTIME;
3499 * For new created object, it must be consistent,
3500 * and it is unnecessary to scrub against it.
3502 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
3504 obj->oo_inode = inode;
3507 if (obj->oo_hl_head != NULL) {
3508 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
3509 obj->oo_hl_head = NULL;
3511 result = PTR_ERR(inode);
3513 LINVRNT(osd_invariant(obj));
3521 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
3522 struct lu_attr *attr,
3523 struct dt_allocation_hint *hint,
3524 struct dt_object_format *dof,
3528 struct osd_thandle *oth;
3529 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
3531 LASSERT(S_ISDIR(attr->la_mode));
3533 oth = container_of(th, struct osd_thandle, ot_super);
3534 LASSERT(oth->ot_handle->h_transaction != NULL);
3535 if (fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)))
3536 obj->oo_dirent_count = 0;
3537 result = osd_mkfile(info, obj, mode, hint, th, attr);
3542 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
3543 struct lu_attr *attr,
3544 struct dt_allocation_hint *hint,
3545 struct dt_object_format *dof,
3549 struct osd_thandle *oth;
3550 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
3552 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
3554 LASSERT(S_ISREG(attr->la_mode));
3556 oth = container_of(th, struct osd_thandle, ot_super);
3557 LASSERT(oth->ot_handle->h_transaction != NULL);
3559 result = osd_mkfile(info, obj, mode, hint, th, attr);
3561 LASSERT(obj->oo_inode != NULL);
3562 if (feat->dif_flags & DT_IND_VARKEY)
3563 result = iam_lvar_create(obj->oo_inode,
3564 feat->dif_keysize_max,
3566 feat->dif_recsize_max,
3569 result = iam_lfix_create(obj->oo_inode,
3570 feat->dif_keysize_max,
3572 feat->dif_recsize_max,
3578 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
3579 struct lu_attr *attr,
3580 struct dt_allocation_hint *hint,
3581 struct dt_object_format *dof,
3584 LASSERT(S_ISREG(attr->la_mode));
3585 return osd_mkfile(info, obj, (attr->la_mode &
3586 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
3590 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
3591 struct lu_attr *attr,
3592 struct dt_allocation_hint *hint,
3593 struct dt_object_format *dof,
3596 LASSERT(S_ISLNK(attr->la_mode));
3597 return osd_mkfile(info, obj, (attr->la_mode &
3598 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
3602 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
3603 struct lu_attr *attr,
3604 struct dt_allocation_hint *hint,
3605 struct dt_object_format *dof,
3608 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
3611 LINVRNT(osd_invariant(obj));
3612 LASSERT(obj->oo_inode == NULL);
3613 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
3614 S_ISFIFO(mode) || S_ISSOCK(mode));
3616 result = osd_mkfile(info, obj, mode, hint, th, attr);
3618 LASSERT(obj->oo_inode != NULL);
3620 * This inode should be marked dirty for i_rdev. Currently
3621 * that is done in the osd_attr_init().
3623 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
3626 LINVRNT(osd_invariant(obj));
3630 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
3632 struct dt_allocation_hint *hint,
3633 struct dt_object_format *dof,
3636 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
3638 osd_obj_type_f result;
3654 result = osd_mk_index;
3664 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
3665 struct dt_object *parent, struct dt_object *child,
3670 ah->dah_parent = parent;
3672 if (parent != NULL && !dt_object_remote(parent)) {
3673 /* will help to find FID->ino at dt_insert("..") */
3674 struct osd_object *pobj = osd_dt_obj(parent);
3676 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
3680 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
3681 struct lu_attr *attr, struct dt_object_format *dof,
3682 struct thandle *handle)
3684 struct inode *inode = obj->oo_inode;
3685 __u64 valid = attr->la_valid;
3688 attr->la_valid &= ~(LA_TYPE | LA_MODE);
3690 if (dof->dof_type != DFT_NODE)
3691 attr->la_valid &= ~LA_RDEV;
3692 if ((valid & LA_ATIME) && (attr->la_atime == inode->i_atime.tv_sec))
3693 attr->la_valid &= ~LA_ATIME;
3694 if ((valid & LA_CTIME) && (attr->la_ctime == inode->i_ctime.tv_sec))
3695 attr->la_valid &= ~LA_CTIME;
3696 if ((valid & LA_MTIME) && (attr->la_mtime == inode->i_mtime.tv_sec))
3697 attr->la_valid &= ~LA_MTIME;
3699 result = osd_quota_transfer(inode, attr, handle);
3703 if (attr->la_valid != 0) {
3704 result = osd_inode_setattr(info->oti_env, inode, attr);
3706 * The osd_inode_setattr() should always succeed here. The
3707 * only error that could be returned is EDQUOT when we are
3708 * trying to change the UID or GID of the inode. However, this
3709 * should not happen since quota enforcement is no longer
3710 * enabled on ldiskfs (lquota takes care of it).
3712 LASSERTF(result == 0, "%d\n", result);
3713 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3716 attr->la_valid = valid;
3720 * Helper function for osd_create()
3722 * \retval 0, on success
3724 static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
3725 struct lu_attr *attr, struct dt_allocation_hint *hint,
3726 struct dt_object_format *dof, struct thandle *th)
3731 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
3733 /* we drop umask so that permissions we pass are not affected */
3734 umask = current->fs->umask;
3735 current->fs->umask = 0;
3737 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
3739 if (likely(obj->oo_inode != NULL)) {
3740 LASSERT(obj->oo_inode->i_state & I_NEW);
3743 * Unlock the inode before attr initialization to avoid
3744 * unnecessary dqget operations. LU-6378
3746 unlock_new_inode(obj->oo_inode);
3749 if (likely(result == 0)) {
3750 osd_attr_init(info, obj, attr, dof, th);
3751 osd_object_init0(obj);
3754 /* restore previous umask value */
3755 current->fs->umask = umask;
3757 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
3763 * Helper function for osd_create()
3765 * \retval 0, on success
3767 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
3768 const struct lu_fid *fid, struct thandle *th)
3770 struct osd_thread_info *info = osd_oti_get(env);
3771 struct osd_inode_id *id = &info->oti_id;
3772 struct osd_device *osd = osd_obj2dev(obj);
3773 struct osd_thandle *oh;
3776 LASSERT(obj->oo_inode != NULL);
3778 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_OI_ENOSPC))
3781 oh = container_of(th, struct osd_thandle, ot_super);
3782 LASSERT(oh->ot_handle);
3783 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3785 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3786 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_FID_REUSE) && osd->od_is_ost &&
3788 struct lu_fid tfid = *fid;
3791 osd_oi_insert(info, osd, &tfid, id, oh->ot_handle,
3792 OI_CHECK_FLD, NULL);
3793 /* clear NOSCRUB flag so that it can be scrubbed immediately */
3794 ldiskfs_clear_inode_state(obj->oo_inode,
3795 LDISKFS_STATE_LUSTRE_NOSCRUB);
3798 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3799 OI_CHECK_FLD, NULL);
3801 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP) && osd->od_is_ost) {
3802 struct lu_fid next_fid = *fid;
3804 /* insert next object in advance, and map to the same inode */
3806 if (next_fid.f_oid != 0) {
3807 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3808 osd_oi_insert(info, osd, &next_fid, id, oh->ot_handle,
3809 OI_CHECK_FLD, NULL);
3810 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3814 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3819 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3820 u64 seq, struct lu_seq_range *range)
3822 struct seq_server_site *ss = osd_seq_site(osd);
3824 if (fid_seq_is_idif(seq)) {
3825 fld_range_set_ost(range);
3826 range->lsr_index = idif_ost_idx(seq);
3830 if (!fid_seq_in_fldb(seq)) {
3831 fld_range_set_mdt(range);
3834 * FIXME: If ss is NULL, it suppose not get lsr_index
3837 range->lsr_index = ss->ss_node_id;
3841 LASSERT(ss != NULL);
3842 fld_range_set_any(range);
3843 /* OSD will only do local fld lookup */
3844 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3847 static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
3848 struct lu_attr *attr,
3849 struct dt_allocation_hint *hint,
3850 struct dt_object_format *dof,
3851 struct thandle *handle)
3853 struct osd_thandle *oh;
3854 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3860 LASSERT(handle != NULL);
3862 oh = container_of(handle, struct osd_thandle, ot_super);
3863 LASSERT(oh->ot_handle == NULL);
3865 if (osd_tx_was_declared(env, oh, dt, DTO_OBJECT_CREATE, 0))
3869 * EA object consumes more credits than regular object: osd_mk_index
3870 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3871 * leaf_node, could involves the block, block bitmap, groups, GDT
3872 * change for each block, so add 4 * 2 credits in that case.
3874 * The default ACL initialization may consume an additional 16 blocks
3876 credits = osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3877 ((dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3880 * While ldiskfs_new_inode() calls ldiskfs_init_acl() we have to add
3881 * credits for possible default ACL creation in new inode
3883 if (hint && hint->dah_acl_len)
3884 credits += osd_calc_bkmap_credits(sb, NULL, 0, -1,
3885 (hint->dah_acl_len + sb->s_blocksize - 1) >>
3886 sb->s_blocksize_bits);
3888 osd_trans_declare_op(env, oh, OSD_OT_CREATE, credits);
3891 * Reuse idle OI block may cause additional one OI block
3894 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3895 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3896 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP))
3897 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3898 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3900 /* will help to find FID->ino mapping at dt_insert() */
3901 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3909 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid,
3910 attr->la_projid, 1, oh, osd_dt_obj(dt),
3911 NULL, OSD_QID_INODE);
3919 * Called to destroy on-disk representation of the object
3921 * Concurrency: must be locked
3923 static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
3926 struct osd_object *obj = osd_dt_obj(dt);
3927 struct inode *inode = obj->oo_inode;
3928 struct osd_thandle *oh;
3936 oh = container_of(th, struct osd_thandle, ot_super);
3937 LASSERT(oh->ot_handle == NULL);
3939 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3940 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3942 /* For removing agent entry */
3943 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu))
3944 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3947 * Recycle idle OI leaf may cause additional three OI blocks
3950 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3951 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3952 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3953 /* one less inode */
3954 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3955 i_projid_read(inode), -1, oh, obj, NULL,
3959 /* data to be truncated */
3960 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3961 i_projid_read(inode), 0, oh, obj, NULL,
3967 * will help to find FID->ino when this object is being
3970 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3975 static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
3978 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3979 struct osd_object *obj = osd_dt_obj(dt);
3980 struct inode *inode = obj->oo_inode;
3981 struct osd_device *osd = osd_obj2dev(obj);
3982 struct osd_thandle *oh;
3987 oh = container_of(th, struct osd_thandle, ot_super);
3988 LASSERT(oh->ot_handle);
3990 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3992 if (unlikely(fid_is_acct(fid)))
3995 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu)) {
3996 result = osd_delete_from_remote_parent(env, osd, obj, oh, true);
3998 CERROR("%s: remove agent entry "DFID": rc = %d\n",
3999 osd_name(osd), PFID(fid), result);
4002 if (S_ISDIR(inode->i_mode)) {
4003 if (inode->i_nlink > 2)
4004 CERROR("%s: directory "DFID" ino %lu link count is %u at unlink. run e2fsck to repair\n",
4005 osd_name(osd), PFID(fid), inode->i_ino,
4008 spin_lock(&obj->oo_guard);
4010 spin_unlock(&obj->oo_guard);
4011 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4014 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
4016 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
4018 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4019 result = osd_oi_delete(osd_oti_get(env), osd, fid,
4020 oh->ot_handle, OI_CHECK_FLD);
4022 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
4023 /* XXX: add to ext3 orphan list */
4024 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
4026 /* not needed in the cache anymore */
4027 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
4028 obj->oo_destroyed = 1;
4034 * Put the fid into lustre_mdt_attrs, and then place the structure
4035 * inode's ea. This fid should not be altered during the life time
4038 * \retval +ve, on success
4039 * \retval -ve, on error
4041 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
4043 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
4044 const struct lu_fid *fid, __u32 compat, __u32 incompat)
4046 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4047 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4052 if (CFS_FAIL_CHECK(OBD_FAIL_FID_INLMA))
4055 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
4058 lustre_loa_init(loa, fid, compat, incompat);
4059 lustre_loa_swab(loa, false);
4062 * For the OST device with 256 bytes inode size by default,
4063 * the PFID EA will be stored together with LMA EA to avoid
4064 * performance trouble. Otherwise the PFID EA can be stored
4065 * independently. LU-8998
4067 if ((compat & LMAC_FID_ON_OST) &&
4068 LDISKFS_INODE_SIZE(inode->i_sb) <= 256)
4069 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
4070 sizeof(*loa), XATTR_CREATE);
4072 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4073 sizeof(*lma), XATTR_CREATE);
4075 * LMA may already exist, but we need to check that all the
4076 * desired compat/incompat flags have been added.
4078 if (unlikely(rc == -EEXIST)) {
4079 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
4080 XATTR_NAME_LMA, (void *)loa, sizeof(*loa));
4084 if (rc < sizeof(*lma))
4087 lustre_loa_swab(loa, true);
4088 if (lu_fid_eq(fid, &lma->lma_self_fid) &&
4089 ((compat == 0 && incompat == 0) ||
4090 (!(~lma->lma_compat & compat) &&
4091 !(~lma->lma_incompat & incompat))))
4094 lma->lma_self_fid = *fid;
4095 lma->lma_compat |= compat;
4096 lma->lma_incompat |= incompat;
4097 if (rc == sizeof(*lma)) {
4098 lustre_lma_swab(lma);
4099 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4100 sizeof(*lma), XATTR_REPLACE);
4102 lustre_loa_swab(loa, false);
4103 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
4104 sizeof(*loa), XATTR_REPLACE);
4112 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
4113 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
4114 * To have compatilibility with 1.8 ldiskfs driver we need to have
4115 * magic number at start of fid data.
4116 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
4119 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
4120 const struct lu_fid *fid)
4122 if (!fid_is_namespace_visible(fid) ||
4123 CFS_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
4124 param->edp_magic = 0;
4128 param->edp_magic = LDISKFS_LUFID_MAGIC;
4129 param->edp_len = sizeof(struct lu_fid) + 1;
4130 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
4134 * Try to read the fid from inode ea into dt_rec.
4136 * \param fid object fid.
4138 * \retval 0 on success
4140 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
4141 __u32 ino, struct lu_fid *fid,
4142 struct osd_inode_id *id)
4144 struct osd_thread_info *info = osd_oti_get(env);
4145 struct inode *inode;
4149 osd_id_gen(id, ino, OSD_OII_NOGEN);
4150 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
4152 RETURN(PTR_ERR(inode));
4158 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
4160 struct inode *parent_dir,
4161 const struct lu_fid *dot_fid,
4162 const struct lu_fid *dot_dot_fid,
4163 struct osd_thandle *oth)
4165 struct ldiskfs_dentry_param *dot_ldp;
4166 struct ldiskfs_dentry_param *dot_dot_ldp;
4167 __u32 saved_nlink = dir->i_nlink;
4170 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DOTDOT_ENOSPC))
4173 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
4174 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
4176 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4177 dot_ldp->edp_magic = 0;
4179 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
4180 dir, dot_ldp, dot_dot_ldp);
4182 * The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
4183 * the subseqent ref_add() will increase the dir->i_nlink
4184 * as 3. That is incorrect for new created directory.
4186 * It looks like hack, because we want to make the OSD API
4187 * to be order-independent for new created directory object
4188 * between dt_insert(..) and ref_add() operations.
4190 * Here, we only restore the in-RAM dir-inode's nlink attr,
4191 * becuase if the nlink attr is not 2, then there will be
4192 * ref_add() called following the dt_insert(..), such call
4193 * will make both the in-RAM and on-disk dir-inode's nlink
4194 * attr to be set as 2. LU-7447
4196 set_nlink(dir, saved_nlink);
4201 * Create an local agent inode for remote entry
4203 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
4204 struct osd_device *osd,
4205 struct osd_object *pobj,
4206 const struct lu_fid *fid,
4210 struct osd_thread_info *info = osd_oti_get(env);
4211 struct inode *local;
4212 struct osd_thandle *oh;
4213 struct iattr iattr = {
4214 .ia_valid = ATTR_UID | ATTR_GID |
4215 ATTR_CTIME | ATTR_MTIME | ATTR_ATIME,
4216 .ia_ctime.tv_nsec = UTIME_OMIT,
4217 .ia_mtime.tv_nsec = UTIME_OMIT,
4218 .ia_atime.tv_nsec = UTIME_OMIT,
4219 .ia_uid = GLOBAL_ROOT_UID,
4220 .ia_gid = GLOBAL_ROOT_GID,
4227 oh = container_of(th, struct osd_thandle, ot_super);
4228 LASSERT(oh->ot_handle->h_transaction != NULL);
4230 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode,
4232 if (IS_ERR(local)) {
4233 CERROR("%s: create local error %d\n", osd_name(osd),
4234 (int)PTR_ERR(local));
4239 * restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
4240 * correct gid on remote file, not agent here
4242 local->i_gid = current_fsgid();
4243 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
4245 /* e2fsck doesn't like empty symlinks. Store remote FID as symlink.
4246 * That gives e2fsck something to look at and be happy, and allows
4247 * debugging if we need to determine where this symlink came from.
4249 if (S_ISLNK(type)) {
4250 BUILD_BUG_ON(LDISKFS_N_BLOCKS * 4 < FID_LEN + 1);
4251 ldiskfs_clear_inode_flag(local, LDISKFS_INODE_EXTENTS);
4252 rc = scnprintf((char *)LDISKFS_I(local)->i_data,
4253 LDISKFS_N_BLOCKS * 4, DFID, PFID(fid));
4255 i_size_write(local, rc);
4256 LDISKFS_I(local)->i_disksize = rc;
4258 unlock_new_inode(local);
4260 /* Agent inode should not have project ID */
4261 #ifdef HAVE_PROJECT_QUOTA
4262 if (LDISKFS_I(pobj->oo_inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
4263 i_projid_read(pobj->oo_inode) != 0) {
4264 rc = osd_transfer_project(local, 0, th);
4266 CERROR("%s: quota transfer failed:. Is project quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
4267 osd_ino2name(local), rc);
4268 RETURN(ERR_PTR(rc));
4272 /* Set special LMA flag for local agent inode */
4273 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
4275 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
4276 osd_name(osd), PFID(fid), rc);
4277 RETURN(ERR_PTR(rc));
4283 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode, fid,
4284 lu_object_fid(&pobj->oo_dt.do_lu),
4287 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
4288 osd_name(osd), PFID(fid), rc);
4289 RETURN(ERR_PTR(rc));
4296 * when direntry is deleted, we have to take care of possible agent inode
4297 * referenced by that. unfortunately we can't do this at that point:
4298 * iget() within a running transaction leads to deadlock and we better do
4299 * not call that every delete declaration to save performance. so we put
4300 * a potention agent inode on a list and process that once the transaction
4301 * is over. Notice it's not any worse in terms of real orphans as regular
4302 * object destroy doesn't put inodes on the on-disk orphan list. this should
4303 * be addressed separately
4305 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
4306 struct osd_thandle *oh,
4309 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
4310 struct osd_obj_orphan *oor;
4317 oor->oor_env = (struct lu_env *)env;
4318 spin_lock(&osd->od_osfs_lock);
4319 list_add(&oor->oor_list, &osd->od_orphan_list);
4320 spin_unlock(&osd->od_osfs_lock);
4322 oh->ot_remove_agents = 1;
4328 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
4329 struct osd_device *osd)
4331 struct osd_thread_info *info = osd_oti_get(env);
4332 struct osd_obj_orphan *oor, *tmp;
4333 struct osd_inode_id id;
4335 struct inode *inode;
4340 spin_lock(&osd->od_osfs_lock);
4341 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
4342 if (oor->oor_env == env)
4343 list_move(&oor->oor_list, &list);
4345 spin_unlock(&osd->od_osfs_lock);
4347 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
4351 list_del(&oor->oor_list);
4354 osd_id_gen(&id, ino, OSD_OII_NOGEN);
4355 inode = osd_iget_fid(info, osd, &id, &fid);
4359 if (!osd_remote_fid(env, osd, &fid)) {
4364 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
4366 mark_inode_dirty(inode);
4367 ldiskfs_journal_stop(jh);
4375 * OSD layer object create function for OST objects (b=11826).
4377 * The FID is inserted into inode xattr here.
4379 * \retval 0, on success
4380 * \retval -ve, on error
4382 static int osd_create(const struct lu_env *env, struct dt_object *dt,
4383 struct lu_attr *attr, struct dt_allocation_hint *hint,
4384 struct dt_object_format *dof, struct thandle *th)
4386 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
4387 struct osd_object *obj = osd_dt_obj(dt);
4388 struct osd_thread_info *info = osd_oti_get(env);
4389 int result, on_ost = 0;
4393 if (dt_object_exists(dt))
4396 LINVRNT(osd_invariant(obj));
4397 LASSERT(!dt_object_remote(dt));
4398 LASSERT(osd_is_write_locked(env, obj));
4399 LASSERT(th != NULL);
4401 if (unlikely(fid_is_acct(fid)))
4403 * Quota files can't be created from the kernel any more,
4404 * 'tune2fs -O quota' will take care of creating them
4408 result = __osd_create(info, obj, attr, hint, dof, th);
4410 if (fid_is_idif(fid) &&
4411 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
4412 struct lu_fid *tfid = &info->oti_fid;
4413 struct ost_id *oi = &info->oti_ostid;
4415 fid_to_ostid(fid, oi);
4416 ostid_to_fid(tfid, oi, 0);
4418 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
4419 LMAC_FID_ON_OST, 0);
4421 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
4423 result = osd_ea_fid_set(info, obj->oo_inode, fid,
4424 on_ost ? LMAC_FID_ON_OST : 0,
4427 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
4428 obj->oo_dt.do_body_ops = &osd_body_ops;
4431 if (!result && !CFS_FAIL_CHECK(OBD_FAIL_OSD_NO_OI_ENTRY)) {
4432 struct inode *inode = obj->oo_inode;
4434 result = __osd_oi_insert(env, obj, fid, th);
4435 if (result && inode) {
4436 spin_lock(&obj->oo_guard);
4438 spin_unlock(&obj->oo_guard);
4439 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4440 ldiskfs_set_inode_state(inode,
4441 LDISKFS_STATE_LUSTRE_DESTROY);
4443 obj->oo_inode = NULL;
4448 * a small optimization - dt_insert() isn't usually applied
4449 * to OST objects, so we don't need to cache OI mapping for
4452 if (result == 0 && on_ost == 0) {
4453 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4455 result = osd_idc_find_and_init(env, osd, obj);
4456 LASSERT(result == 0);
4459 LASSERT(ergo(result == 0,
4460 dt_object_exists(dt) && !dt_object_remote(dt)));
4461 LINVRNT(osd_invariant(obj));
4465 static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
4466 struct thandle *handle)
4468 struct osd_thandle *oh;
4471 /* it's possible that object doesn't exist yet */
4472 LASSERT(handle != NULL);
4474 oh = container_of(handle, struct osd_thandle, ot_super);
4475 LASSERT(oh->ot_handle == NULL);
4477 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
4478 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
4480 rc = osd_idc_find_and_init(env, osd_dev(dt->do_lu.lo_dev),
4487 * Concurrency: @dt is write locked.
4489 static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
4492 struct osd_object *obj = osd_dt_obj(dt);
4493 struct inode *inode = obj->oo_inode;
4494 struct osd_thandle *oh;
4497 if (!dt_object_exists(dt) || obj->oo_destroyed)
4500 LINVRNT(osd_invariant(obj));
4501 LASSERT(!dt_object_remote(dt));
4502 LASSERT(osd_is_write_locked(env, obj));
4503 LASSERT(th != NULL);
4505 oh = container_of(th, struct osd_thandle, ot_super);
4506 LASSERT(oh->ot_handle != NULL);
4508 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
4510 CDEBUG(D_INODE, DFID" increase nlink %d\n",
4511 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
4513 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
4514 * (65000) subdirectories by storing "1" in i_nlink if the link count
4515 * would otherwise overflow. Directory tranversal tools understand
4516 * that (st_nlink == 1) indicates that the filesystem dose not track
4517 * hard links count on the directory, and will not abort subdirectory
4518 * scanning early once (st_nlink - 2) subdirs have been found.
4520 * This also has to properly handle the case of inodes with nlink == 0
4521 * in case they are being linked into the PENDING directory
4523 spin_lock(&obj->oo_guard);
4524 if (unlikely(inode->i_nlink == 0))
4525 /* inc_nlink from 0 may cause WARN_ON */
4526 set_nlink(inode, 1);
4528 osd_ldiskfs_inc_count(oh->ot_handle, inode);
4529 if (!S_ISDIR(inode->i_mode))
4530 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
4532 spin_unlock(&obj->oo_guard);
4534 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4535 LINVRNT(osd_invariant(obj));
4537 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
4542 static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
4543 struct thandle *handle)
4545 struct osd_thandle *oh;
4547 if (!dt_object_exists(dt))
4550 LASSERT(!dt_object_remote(dt));
4551 LASSERT(handle != NULL);
4553 oh = container_of(handle, struct osd_thandle, ot_super);
4554 LASSERT(oh->ot_handle == NULL);
4556 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
4557 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
4563 * Concurrency: @dt is write locked.
4565 static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
4568 struct osd_object *obj = osd_dt_obj(dt);
4569 struct inode *inode = obj->oo_inode;
4570 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4571 struct osd_thandle *oh;
4573 if (!dt_object_exists(dt))
4576 LINVRNT(osd_invariant(obj));
4577 LASSERT(!dt_object_remote(dt));
4578 LASSERT(osd_is_write_locked(env, obj));
4579 LASSERT(th != NULL);
4581 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_REF_DEL))
4584 oh = container_of(th, struct osd_thandle, ot_super);
4585 LASSERT(oh->ot_handle != NULL);
4587 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
4589 spin_lock(&obj->oo_guard);
4591 * That can be result of upgrade from old Lustre version and
4592 * applied only to local files. Just skip this ref_del call.
4593 * ext4_unlink() only treats this as a warning, don't LASSERT here.
4595 if (inode->i_nlink == 0) {
4596 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
4597 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
4598 ", maybe an upgraded file? (LU-3915)\n",
4599 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
4600 spin_unlock(&obj->oo_guard);
4604 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
4605 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
4607 osd_ldiskfs_dec_count(oh->ot_handle, inode);
4608 spin_unlock(&obj->oo_guard);
4610 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4611 LINVRNT(osd_invariant(obj));
4613 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
4619 * Concurrency: @dt is read locked.
4621 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
4622 struct lu_buf *buf, const char *name)
4624 struct osd_object *obj = osd_dt_obj(dt);
4625 struct inode *inode = obj->oo_inode;
4626 struct osd_thread_info *info = osd_oti_get(env);
4627 struct dentry *dentry = &info->oti_obj_dentry;
4628 bool cache_xattr = false;
4633 /* version get is not real XATTR but uses xattr API */
4634 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4635 dt_obj_version_t *ver = buf->lb_buf;
4638 * for version we are just using xattr API but change inode
4641 if (buf->lb_len == 0)
4642 return sizeof(dt_obj_version_t);
4644 if (buf->lb_len < sizeof(dt_obj_version_t))
4647 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
4648 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4650 *ver = LDISKFS_I(inode)->i_fs_version;
4652 return sizeof(dt_obj_version_t);
4655 if (!dt_object_exists(dt))
4658 LASSERT(!dt_object_remote(dt));
4659 LASSERT(inode->i_op != NULL);
4660 #ifdef HAVE_IOP_XATTR
4661 LASSERT(inode->i_op->getxattr != NULL);
4664 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
4665 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
4669 rc = osd_oxc_get(obj, name, buf);
4674 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
4675 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4676 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4677 struct filter_fid *ff;
4678 struct ost_layout *ol;
4680 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4684 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
4687 if (buf->lb_len == 0 || !buf->lb_buf)
4690 if (buf->lb_len < rc)
4694 ol = &ff->ff_layout;
4695 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
4696 PFID_STRIPE_IDX_BITS);
4697 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
4698 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
4699 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
4700 if (lma->lma_compat & LMAC_COMP_INFO) {
4701 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
4702 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
4703 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
4705 ol->ol_comp_start = 0;
4706 ol->ol_comp_end = 0;
4710 /* Get enc context xattr directly from ldiskfs instead of going
4711 * through the VFS, as there is no xattr handler for
4714 if (strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
4715 rc = ldiskfs_xattr_get(inode,
4716 LDISKFS_XATTR_INDEX_ENCRYPTION,
4717 LDISKFS_XATTR_NAME_ENCRYPTION_CONTEXT,
4718 buf->lb_buf, buf->lb_len);
4720 rc = __osd_xattr_get(inode, dentry, name,
4721 buf->lb_buf, buf->lb_len);
4725 if (rc == -ENOENT || rc == -ENODATA)
4726 osd_oxc_add(obj, name, NULL, 0);
4727 else if (rc > 0 && buf->lb_buf != NULL)
4728 osd_oxc_add(obj, name, buf->lb_buf, rc);
4734 static int osd_declare_xattr_set(const struct lu_env *env,
4735 struct dt_object *dt,
4736 const struct lu_buf *buf, const char *name,
4737 int fl, struct thandle *handle)
4739 struct osd_thandle *oh;
4741 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4743 LASSERT(handle != NULL);
4745 oh = container_of(handle, struct osd_thandle, ot_super);
4746 LASSERT(oh->ot_handle == NULL);
4748 if (strcmp(name, XATTR_NAME_LMA) == 0) {
4750 * For non-upgrading case, the LMA is set first and
4751 * usually fit inode. But for upgrade case, the LMA
4752 * may be in another separated EA block.
4754 if (dt_object_exists(dt)) {
4755 if (fl == LU_XATTR_REPLACE)
4760 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4762 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4763 /* We may need to delete the old PFID EA. */
4764 credits = LDISKFS_MAXQUOTAS_DEL_BLOCKS(sb);
4765 if (fl == LU_XATTR_REPLACE)
4771 * If some name entry resides on remote MDT, then will create
4772 * agent entry under remote parent. On the other hand, if the
4773 * remote entry will be removed, then related agent entry may
4774 * need to be removed from the remote parent. So there may be
4775 * kinds of cases, let's declare enough credits. The credits
4776 * for create agent entry is enough for remove case.
4778 if (strcmp(name, XATTR_NAME_LINK) == 0) {
4779 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4780 if (dt_object_exists(dt))
4781 credits += 1; /* For updating LMA */
4785 credits += osd_dto_credits_noquota[DTO_XATTR_SET];
4790 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
4792 * learn xattr size from osd_xattr_get if
4793 * attribute has not been read yet
4795 buflen = __osd_xattr_get(
4796 osd_dt_obj(dt)->oo_inode,
4797 &osd_oti_get(env)->oti_obj_dentry,
4802 buflen = buf->lb_len;
4805 if (buflen > sb->s_blocksize) {
4806 credits += osd_calc_bkmap_credits(
4808 (buflen + sb->s_blocksize - 1) >>
4809 sb->s_blocksize_bits);
4813 * xattr set may involve inode quota change, reserve credits for
4814 * dquot_initialize()
4816 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4819 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
4824 static int osd_xattr_set_pfid(const struct lu_env *env, struct osd_object *obj,
4825 const struct lu_buf *buf, int fl,
4826 struct thandle *handle)
4828 struct osd_thread_info *info = osd_oti_get(env);
4829 struct dentry *dentry = &info->oti_obj_dentry;
4830 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4831 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4832 struct inode *inode = obj->oo_inode;
4833 struct filter_fid *ff = buf->lb_buf;
4834 struct ost_layout *ol = &ff->ff_layout;
4835 int flags = XATTR_REPLACE;
4840 if (buf->lb_len != sizeof(*ff) && buf->lb_len != sizeof(struct lu_fid))
4843 rc = osd_get_lma(info, inode, dentry, loa);
4844 if (rc == -ENODATA) {
4845 /* Usually for upgarding from old device */
4846 lustre_loa_init(loa, lu_object_fid(&obj->oo_dt.do_lu),
4847 LMAC_FID_ON_OST, 0);
4848 flags = XATTR_CREATE;
4853 if (!rc && lma->lma_compat & LMAC_STRIPE_INFO) {
4854 if ((fl & LU_XATTR_CREATE) && !(fl & LU_XATTR_REPLACE))
4857 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256) {
4858 /* Separate PFID EA from LMA */
4859 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4860 lustre_lma_swab(lma);
4861 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4862 sizeof(*lma), XATTR_REPLACE);
4864 obj->oo_pfid_in_lma = 0;
4865 rc = LU_XATTR_CREATE;
4871 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256)
4875 * Old client does not send stripe information,
4876 * then store the PFID EA on disk separatedly.
4878 if (unlikely(buf->lb_len == sizeof(struct lu_fid) ||
4879 ol->ol_stripe_size == 0))
4882 /* Remove old PFID EA entry firstly. */
4883 dquot_initialize(inode);
4884 rc = ll_vfs_removexattr(dentry, inode, XATTR_NAME_FID);
4885 if (rc == -ENODATA) {
4886 /* XATTR_NAME_FID is already absent */
4893 fid_le_to_cpu(&loa->loa_parent_fid, &ff->ff_parent);
4894 if (likely(ol->ol_stripe_size != 0)) {
4895 loa->loa_parent_fid.f_ver |= le32_to_cpu(ol->ol_stripe_count) <<
4896 PFID_STRIPE_IDX_BITS;
4897 loa->loa_stripe_size = le32_to_cpu(ol->ol_stripe_size);
4898 lma->lma_compat |= LMAC_STRIPE_INFO;
4899 if (ol->ol_comp_id != 0) {
4900 loa->loa_comp_id = le32_to_cpu(ol->ol_comp_id);
4901 loa->loa_comp_start = le64_to_cpu(ol->ol_comp_start);
4902 loa->loa_comp_end = le64_to_cpu(ol->ol_comp_end);
4903 lma->lma_compat |= LMAC_COMP_INFO;
4907 lustre_loa_swab(loa, false);
4909 /* Store the PFID EA inside LMA. */
4910 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa, sizeof(*loa),
4913 obj->oo_pfid_in_lma = 1;
4919 * In DNE environment, the object (in spite of regular file or directory)
4920 * and its name entry may reside on different MDTs. Under such case, we will
4921 * create an agent entry on the MDT where the object resides. The agent entry
4922 * references the object locally, that makes the object to be visible to the
4923 * userspace when mounted as 'ldiskfs' directly. Then the userspace tools,
4924 * such as 'tar' can handle the object properly.
4926 * We handle the agent entry during set linkEA that is the common interface
4927 * for both regular file and directroy, can handle kinds of cases, such as
4928 * create/link/unlink/rename, and so on.
4930 * NOTE: we can NOT do that when ea_{insert,delete} that is only for directory.
4932 * XXX: There are two known issues:
4933 * 1. For one object, we will create at most one agent entry even if there
4934 * may be more than one cross-MDTs hard links on the object. So the local
4935 * e2fsck may claim that the object's nlink is larger than the name entries
4936 * that reference such inode. And in further, the e2fsck will fix the nlink
4937 * attribute to match the local references. Then it will cause the object's
4938 * nlink attribute to be inconsistent with the global references. it is bad
4939 * but not fatal. The ref_del() can handle the zero-referenced case. On the
4940 * other hand, the global namespace LFSCK can repair the object's attribute
4941 * according to the linkEA.
4942 * 2. There may be too many hard links on the object as to its linkEA overflow,
4943 * then the linkEA entry for cross-MDTs reference may be discarded. If such
4944 * case happened, then at this point, we do not know whether there are some
4945 * cross-MDTs reference. But there are local references, it guarantees that
4946 * object is visible to userspace when mounted as 'ldiskfs'. That is enough.
4948 static int osd_xattr_handle_linkea(const struct lu_env *env,
4949 struct osd_device *osd,
4950 struct osd_object *obj,
4951 const struct lu_buf *buf,
4952 struct thandle *handle)
4954 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
4955 struct lu_fid *tfid = &osd_oti_get(env)->oti_fid3;
4956 struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
4957 struct lu_name tmpname;
4958 struct osd_thandle *oh;
4960 bool remote = false;
4964 oh = container_of(handle, struct osd_thandle, ot_super);
4965 LASSERT(oh->ot_handle != NULL);
4967 rc = linkea_init_with_rec(&ldata);
4969 linkea_first_entry(&ldata);
4970 while (ldata.ld_lee != NULL && !remote) {
4971 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
4973 if (osd_remote_fid(env, osd, tfid) > 0)
4976 linkea_next_entry(&ldata);
4978 } else if (rc == -ENODATA) {
4984 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
4985 rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
4987 CERROR("%s: failed to remove agent entry for "DFID
4988 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4989 } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
4990 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4992 CERROR("%s: failed to create agent entry for "DFID
4993 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
5000 * Concurrency: @dt is write locked.
5002 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
5003 const struct lu_buf *buf, const char *name, int fl,
5004 struct thandle *handle)
5006 struct osd_object *obj = osd_dt_obj(dt);
5007 struct osd_device *osd = osd_obj2dev(obj);
5008 struct inode *inode = obj->oo_inode;
5009 struct osd_thread_info *info = osd_oti_get(env);
5019 /* version set is not real XATTR */
5020 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
5021 dt_obj_version_t *version = buf->lb_buf;
5024 * for version we are just using xattr API but change inode
5027 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
5030 DFID" set version %#llx (old %#llx) for inode %lu\n",
5031 PFID(lu_object_fid(&dt->do_lu)), *version,
5032 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
5034 LDISKFS_I(inode)->i_fs_version = *version;
5036 * Version is set after all inode operations are finished,
5037 * so we should mark it dirty here
5039 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
5044 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
5045 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
5048 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
5051 * For the OST device with 256 bytes inode size by default,
5052 * the PFID EA will be stored together with LMA EA to avoid
5053 * performance trouble. Otherwise the PFID EA can be stored
5054 * independently. LU-8998
5056 if (strcmp(name, XATTR_NAME_FID) == 0 && osd->od_is_ost &&
5057 (LDISKFS_INODE_SIZE(inode->i_sb) <= 256 || obj->oo_pfid_in_lma)) {
5058 LASSERT(buf->lb_buf);
5060 fl = osd_xattr_set_pfid(env, obj, buf, fl, handle);
5063 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
5064 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
5065 struct lustre_mdt_attrs *lma = &loa->loa_lma;
5067 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
5071 lma->lma_incompat |= LMAI_STRIPED;
5072 lustre_lma_swab(lma);
5073 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
5074 sizeof(*lma), XATTR_REPLACE);
5077 } else if (strcmp(name, XATTR_NAME_LINK) == 0) {
5078 LASSERT(!osd->od_is_ost);
5080 rc = osd_xattr_handle_linkea(env, osd, obj, buf, handle);
5085 if (fl & LU_XATTR_REPLACE)
5086 fs_flags |= XATTR_REPLACE;
5088 if (fl & LU_XATTR_CREATE)
5089 fs_flags |= XATTR_CREATE;
5091 if (strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0) {
5092 /* Set enc context xattr directly in ldiskfs instead of going
5093 * through the VFS, as there is no xattr handler for
5096 struct osd_thandle *oth = container_of(handle,
5100 if (!oth->ot_handle)
5101 /* this should be already part of a transaction */
5104 rc = ldiskfs_xattr_set_handle(oth->ot_handle, inode,
5105 LDISKFS_XATTR_INDEX_ENCRYPTION,
5106 LDISKFS_XATTR_NAME_ENCRYPTION_CONTEXT,
5107 buf->lb_buf, len, fs_flags);
5109 rc = __osd_xattr_set(info, inode, name,
5110 buf->lb_buf, len, fs_flags);
5112 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
5115 (strcmp(name, XATTR_NAME_LOV) == 0 ||
5116 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
5117 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
5123 * Concurrency: @dt is read locked.
5125 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
5126 const struct lu_buf *buf)
5128 struct osd_object *obj = osd_dt_obj(dt);
5129 struct osd_device *dev = osd_obj2dev(obj);
5130 struct inode *inode = obj->oo_inode;
5131 struct osd_thread_info *info = osd_oti_get(env);
5132 struct dentry *dentry = &info->oti_obj_dentry;
5135 if (!dt_object_exists(dt))
5138 LASSERT(!dt_object_remote(dt));
5139 LASSERT(inode->i_op != NULL);
5140 LASSERT(inode->i_op->listxattr != NULL);
5142 dentry->d_inode = inode;
5143 dentry->d_sb = inode->i_sb;
5144 rc = inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
5146 if (rc < 0 || buf->lb_buf == NULL)
5149 /* Hide virtual project ID xattr from list if disabled */
5150 if (!dev->od_enable_projid_xattr) {
5151 char *end = (char *)buf->lb_buf + rc;
5152 char *p = buf->lb_buf;
5155 char *next = p + strlen(p) + 1;
5157 if (strcmp(p, XATTR_NAME_PROJID) == 0) {
5159 memmove(p, next, end - next);
5171 static int osd_declare_xattr_del(const struct lu_env *env,
5172 struct dt_object *dt, const char *name,
5173 struct thandle *handle)
5175 struct osd_thandle *oh;
5176 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
5178 LASSERT(!dt_object_remote(dt));
5179 LASSERT(handle != NULL);
5181 oh = container_of(handle, struct osd_thandle, ot_super);
5182 LASSERT(oh->ot_handle == NULL);
5184 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
5185 osd_dto_credits_noquota[DTO_XATTR_SET]);
5187 * xattr del may involve inode quota change, reserve credits for
5188 * dquot_initialize()
5190 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
5196 * Concurrency: @dt is write locked.
5198 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
5199 const char *name, struct thandle *handle)
5201 struct osd_object *obj = osd_dt_obj(dt);
5202 struct inode *inode = obj->oo_inode;
5203 struct osd_thread_info *info = osd_oti_get(env);
5204 struct dentry *dentry = &info->oti_obj_dentry;
5207 if (!dt_object_exists(dt))
5210 LASSERT(!dt_object_remote(dt));
5211 LASSERT(inode->i_op != NULL);
5212 LASSERT(handle != NULL);
5213 #ifdef HAVE_IOP_XATTR
5214 LASSERT(inode->i_op->removexattr != NULL);
5217 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
5219 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
5220 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
5222 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
5223 &info->oti_ost_attrs);
5225 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
5227 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
5228 lustre_lma_swab(lma);
5229 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
5230 sizeof(*lma), XATTR_REPLACE);
5232 obj->oo_pfid_in_lma = 0;
5235 dquot_initialize(inode);
5236 dentry->d_inode = inode;
5237 dentry->d_sb = inode->i_sb;
5238 rc = ll_vfs_removexattr(dentry, inode, name);
5241 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
5244 (strcmp(name, XATTR_NAME_LOV) == 0 ||
5245 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
5246 osd_oxc_del(obj, name);
5251 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
5252 __u64 start, __u64 end)
5254 struct osd_object *obj = osd_dt_obj(dt);
5255 struct osd_device *dev = osd_obj2dev(obj);
5256 struct inode *inode = obj->oo_inode;
5261 file = alloc_file_pseudo(inode, dev->od_mnt, "/", O_NOATIME,
5264 RETURN(PTR_ERR(file));
5266 file->f_mode |= FMODE_64BITHASH;
5267 rc = vfs_fsync_range(file, start, end, 0);
5274 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
5279 static bool osd_check_stale(struct dt_object *dt)
5287 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
5288 const struct dt_index_features *feat)
5290 struct iam_descr *descr;
5292 if (osd_object_is_root(o))
5293 return feat == &dt_directory_features;
5295 LASSERT(o->oo_dir != NULL);
5297 descr = o->oo_dir->od_container.ic_descr;
5298 if (feat == &dt_directory_features) {
5299 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
5304 return feat->dif_keysize_min <= descr->id_key_size &&
5305 descr->id_key_size <= feat->dif_keysize_max &&
5306 feat->dif_recsize_min <= descr->id_rec_size &&
5307 descr->id_rec_size <= feat->dif_recsize_max &&
5308 !(feat->dif_flags & (DT_IND_VARKEY |
5309 DT_IND_VARREC | DT_IND_NONUNQ)) &&
5310 ergo(feat->dif_flags & DT_IND_UPDATE,
5311 1 /* XXX check that object (and fs) is writable */);
5315 static int osd_iam_container_init(const struct lu_env *env,
5316 struct osd_object *obj,
5317 struct osd_directory *dir)
5319 struct iam_container *bag = &dir->od_container;
5322 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
5326 result = iam_container_setup(bag);
5328 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
5330 iam_container_fini(bag);
5337 * Concurrency: no external locking is necessary.
5339 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
5340 const struct dt_index_features *feat)
5344 struct osd_object *obj = osd_dt_obj(dt);
5346 LINVRNT(osd_invariant(obj));
5348 if (osd_object_is_root(obj)) {
5349 dt->do_index_ops = &osd_index_ea_ops;
5351 } else if (feat == &dt_directory_features) {
5352 dt->do_index_ops = &osd_index_ea_ops;
5353 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
5358 } else if (unlikely(feat == &dt_otable_features)) {
5359 dt->do_index_ops = &osd_otable_ops;
5361 } else if (unlikely(feat == &dt_acct_features)) {
5362 dt->do_index_ops = &osd_acct_index_ops;
5365 } else if (!osd_has_index(obj)) {
5366 struct osd_directory *dir;
5367 struct osd_device *osd = osd_obj2dev(obj);
5368 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
5373 spin_lock(&obj->oo_guard);
5374 if (obj->oo_dir == NULL)
5378 * Concurrent thread allocated container data.
5381 spin_unlock(&obj->oo_guard);
5383 * Now, that we have container data, serialize its
5386 down_write(&obj->oo_ext_idx_sem);
5388 * recheck under lock.
5391 if (osd_has_index(obj)) {
5396 result = osd_iam_container_init(env, obj, obj->oo_dir);
5397 if (result || feat == &dt_lfsck_namespace_features ||
5398 feat == &dt_lfsck_layout_orphan_features ||
5399 feat == &dt_lfsck_layout_dangling_features)
5402 result = osd_index_register(osd, fid,
5403 feat->dif_keysize_max,
5404 feat->dif_recsize_max);
5406 CWARN("%s: failed to register index "
5408 osd_name(osd), PFID(fid), result);
5409 else if (result > 0)
5412 CDEBUG(D_LFSCK, "%s: index object "DFID
5413 " (%d/%d) registered\n",
5414 osd_name(osd), PFID(fid),
5415 (int)feat->dif_keysize_max,
5416 (int)feat->dif_recsize_max);
5419 up_write(&obj->oo_ext_idx_sem);
5427 if (result == 0 && skip_iam == 0) {
5428 if (!osd_iam_index_probe(env, obj, feat))
5431 LINVRNT(osd_invariant(obj));
5436 static int osd_otable_it_attr_get(const struct lu_env *env,
5437 struct dt_object *dt,
5438 struct lu_attr *attr)
5444 static const struct dt_object_operations osd_obj_ops = {
5445 .do_read_lock = osd_read_lock,
5446 .do_write_lock = osd_write_lock,
5447 .do_read_unlock = osd_read_unlock,
5448 .do_write_unlock = osd_write_unlock,
5449 .do_write_locked = osd_write_locked,
5450 .do_attr_get = osd_attr_get,
5451 .do_declare_attr_set = osd_declare_attr_set,
5452 .do_attr_set = osd_attr_set,
5453 .do_ah_init = osd_ah_init,
5454 .do_declare_create = osd_declare_create,
5455 .do_create = osd_create,
5456 .do_declare_destroy = osd_declare_destroy,
5457 .do_destroy = osd_destroy,
5458 .do_index_try = osd_index_try,
5459 .do_declare_ref_add = osd_declare_ref_add,
5460 .do_ref_add = osd_ref_add,
5461 .do_declare_ref_del = osd_declare_ref_del,
5462 .do_ref_del = osd_ref_del,
5463 .do_xattr_get = osd_xattr_get,
5464 .do_declare_xattr_set = osd_declare_xattr_set,
5465 .do_xattr_set = osd_xattr_set,
5466 .do_declare_xattr_del = osd_declare_xattr_del,
5467 .do_xattr_del = osd_xattr_del,
5468 .do_xattr_list = osd_xattr_list,
5469 .do_object_sync = osd_object_sync,
5470 .do_invalidate = osd_invalidate,
5471 .do_check_stale = osd_check_stale,
5474 static const struct dt_object_operations osd_obj_otable_it_ops = {
5475 .do_attr_get = osd_otable_it_attr_get,
5476 .do_index_try = osd_index_try,
5479 static int osd_index_declare_iam_delete(const struct lu_env *env,
5480 struct dt_object *dt,
5481 const struct dt_key *key,
5482 struct thandle *handle)
5484 struct osd_thandle *oh;
5486 oh = container_of(handle, struct osd_thandle, ot_super);
5487 LASSERT(oh->ot_handle == NULL);
5489 /* Recycle may cause additional three blocks to be changed. */
5490 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
5491 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
5497 * delete a (key, value) pair from index \a dt specified by \a key
5499 * \param dt osd index object
5500 * \param key key for index
5501 * \param rec record reference
5502 * \param handle transaction handler
5505 * \retval -ve failure
5507 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
5508 const struct dt_key *key,
5509 struct thandle *handle)
5511 struct osd_thread_info *oti = osd_oti_get(env);
5512 struct osd_object *obj = osd_dt_obj(dt);
5513 struct osd_thandle *oh;
5514 struct iam_path_descr *ipd;
5515 struct iam_container *bag = &obj->oo_dir->od_container;
5520 if (!dt_object_exists(dt))
5523 LINVRNT(osd_invariant(obj));
5524 LASSERT(!dt_object_remote(dt));
5525 LASSERT(bag->ic_object == obj->oo_inode);
5526 LASSERT(handle != NULL);
5528 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
5530 ipd = osd_idx_ipd_get(env, bag);
5531 if (unlikely(ipd == NULL))
5534 oh = container_of(handle, struct osd_thandle, ot_super);
5535 LASSERT(oh->ot_handle != NULL);
5536 LASSERT(oh->ot_handle->h_transaction != NULL);
5538 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5539 /* swab quota uid/gid provided by caller */
5540 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5541 key = (const struct dt_key *)&oti->oti_quota_id;
5544 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
5545 osd_ipd_put(env, bag, ipd);
5546 LINVRNT(osd_invariant(obj));
5547 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
5551 static int osd_index_declare_ea_delete(const struct lu_env *env,
5552 struct dt_object *dt,
5553 const struct dt_key *key,
5554 struct thandle *handle)
5556 struct osd_thandle *oh;
5557 struct inode *inode;
5562 LASSERT(!dt_object_remote(dt));
5563 LASSERT(handle != NULL);
5565 oh = container_of(handle, struct osd_thandle, ot_super);
5566 LASSERT(oh->ot_handle == NULL);
5568 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
5569 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
5571 inode = osd_dt_obj(dt)->oo_inode;
5575 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
5576 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
5581 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
5584 struct osd_fid_pack *rec;
5587 if (de->file_type & LDISKFS_DIRENT_LUFID) {
5588 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
5589 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
5590 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
5596 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
5597 const struct lu_fid *fid)
5599 struct seq_server_site *ss = osd_seq_site(osd);
5603 /* FID seqs not in FLDB, must be local seq */
5604 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
5608 * If FLD is not being initialized yet, it only happens during the
5609 * initialization, likely during mgs initialization, and we assume
5610 * this is local FID.
5612 if (ss == NULL || ss->ss_server_fld == NULL)
5615 /* Only check the local FLDB here */
5616 if (osd_seq_exists(env, osd, fid_seq(fid)))
5622 static void osd_take_care_of_agent(const struct lu_env *env,
5623 struct osd_device *osd,
5624 struct osd_thandle *oh,
5625 struct ldiskfs_dir_entry_2 *de)
5627 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
5628 struct osd_idmap_cache *idc;
5629 int rc, schedule = 0;
5631 LASSERT(de != NULL);
5633 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
5634 if (likely(rc == 0)) {
5635 idc = osd_idc_find_or_init(env, osd, fid);
5636 if (IS_ERR(idc) || idc->oic_remote)
5638 } else if (rc == -ENODATA) {
5640 * can't get FID, postpone to the end of the
5641 * transaction when iget() is safe
5645 CERROR("%s: can't get FID: rc = %d\n", osd_name(osd), rc);
5648 osd_schedule_agent_inode_removal(env, oh,
5649 le32_to_cpu(de->inode));
5653 * Utility function to get real name from object name
5655 * \param[in] obj pointer to the object to be handled
5656 * \param[in] name object name
5657 * \param[in] len object name len
5658 * \param[out]ln pointer to the struct lu_name to hold the real name
5660 * If file is not encrypted, real name is just the object name.
5661 * If file is encrypted, object name needs to be decoded. In
5662 * this case a new buffer is allocated, and ln->ln_name needs to be freed by
5665 * \retval 0, on success
5666 * \retval -ve, on error
5668 static int obj_name2lu_name(struct osd_object *obj, const char *name,
5669 int len, struct lu_name *ln)
5671 if (!(obj->oo_lma_flags & LUSTRE_ENCRYPT_FL)) {
5673 ln->ln_namelen = len;
5675 char *buf = kmalloc(len, GFP_NOFS);
5680 len = critical_decode(name, len, buf);
5682 ln->ln_namelen = len;
5689 * Index delete function for interoperability mode (b11826).
5690 * It will remove the directory entry added by osd_index_ea_insert().
5691 * This entry is needed to maintain name->fid mapping.
5693 * \param key, key i.e. file entry to be deleted
5695 * \retval 0, on success
5696 * \retval -ve, on error
5698 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
5699 const struct dt_key *key, struct thandle *handle)
5701 struct osd_object *obj = osd_dt_obj(dt);
5702 struct inode *dir = obj->oo_inode;
5703 struct dentry *dentry;
5704 struct osd_thandle *oh;
5705 struct ldiskfs_dir_entry_2 *de = NULL;
5706 struct buffer_head *bh;
5707 struct htree_lock *hlock = NULL;
5708 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5714 if (!dt_object_exists(dt))
5717 LINVRNT(osd_invariant(obj));
5718 LASSERT(!dt_object_remote(dt));
5719 LASSERT(handle != NULL);
5721 rc = obj_name2lu_name(obj, (char *)key, strlen((char *)key), &ln);
5725 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
5727 oh = container_of(handle, struct osd_thandle, ot_super);
5728 LASSERT(oh->ot_handle != NULL);
5729 LASSERT(oh->ot_handle->h_transaction != NULL);
5731 dquot_initialize(dir);
5732 dentry = osd_child_dentry_get(env, obj, ln.ln_name, ln.ln_namelen);
5734 if (obj->oo_hl_head != NULL) {
5735 hlock = osd_oti_get(env)->oti_hlock;
5736 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5737 dir, LDISKFS_HLOCK_DEL);
5739 down_write(&obj->oo_ext_idx_sem);
5742 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5745 * If this is not the ".." entry, it might be a remote DNE
5746 * entry and we need to check if the FID is for a remote
5747 * MDT. If the FID is not in the directory entry (e.g.
5748 * upgraded 1.8 filesystem without dirdata enabled) then
5749 * we need to get the FID from the LMA. For a remote directory
5750 * there HAS to be an LMA, it cannot be an IGIF inode in this
5753 * Delete the entry before the agent inode in order to
5754 * simplify error handling. At worst an error after deleting
5755 * the entry first might leak the agent inode afterward. The
5756 * reverse would need filesystem abort in case of error deleting
5757 * the entry after the agent had been removed, or leave a
5758 * dangling entry pointing at a random inode.
5760 if (strcmp((char *)key, dotdot) != 0)
5761 osd_take_care_of_agent(env, osd, oh, de);
5762 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
5768 if (!rc && fid_is_namespace_visible(lu_object_fid(&dt->do_lu)) &&
5769 obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
5770 /* NB, dirent count may not be accurate, because it's counted
5773 if (obj->oo_dirent_count)
5774 obj->oo_dirent_count--;
5776 obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
5779 ldiskfs_htree_unlock(hlock);
5781 up_write(&obj->oo_ext_idx_sem);
5784 LASSERT(osd_invariant(obj));
5785 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
5786 if (ln.ln_name != (char *)key)
5792 * Lookup index for \a key and copy record to \a rec.
5794 * \param dt osd index object
5795 * \param key key for index
5796 * \param rec record reference
5798 * \retval +ve success : exact mach
5799 * \retval 0 return record with key not greater than \a key
5800 * \retval -ve failure
5802 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
5803 struct dt_rec *rec, const struct dt_key *key)
5805 struct osd_object *obj = osd_dt_obj(dt);
5806 struct iam_path_descr *ipd;
5807 struct iam_container *bag = &obj->oo_dir->od_container;
5808 struct osd_thread_info *oti = osd_oti_get(env);
5809 struct iam_iterator *it = &oti->oti_idx_it;
5810 struct iam_rec *iam_rec;
5815 if (!dt_object_exists(dt))
5818 LASSERT(osd_invariant(obj));
5819 LASSERT(!dt_object_remote(dt));
5820 LASSERT(bag->ic_object == obj->oo_inode);
5822 ipd = osd_idx_ipd_get(env, bag);
5826 /* got ipd now we can start iterator. */
5827 iam_it_init(it, bag, 0, ipd);
5829 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5830 /* swab quota uid/gid provided by caller */
5831 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5832 key = (const struct dt_key *)&oti->oti_quota_id;
5835 rc = iam_it_get(it, (struct iam_key *)key);
5837 if (S_ISDIR(obj->oo_inode->i_mode))
5838 iam_rec = (struct iam_rec *)oti->oti_ldp;
5840 iam_rec = (struct iam_rec *)rec;
5842 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
5844 if (S_ISDIR(obj->oo_inode->i_mode))
5845 osd_fid_unpack((struct lu_fid *)rec,
5846 (struct osd_fid_pack *)iam_rec);
5847 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
5848 osd_quota_unpack(obj, rec);
5853 osd_ipd_put(env, bag, ipd);
5855 LINVRNT(osd_invariant(obj));
5860 static int osd_index_declare_iam_insert(const struct lu_env *env,
5861 struct dt_object *dt,
5862 const struct dt_rec *rec,
5863 const struct dt_key *key,
5864 struct thandle *handle)
5866 struct osd_thandle *oh;
5868 LASSERT(handle != NULL);
5870 oh = container_of(handle, struct osd_thandle, ot_super);
5871 LASSERT(oh->ot_handle == NULL);
5873 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
5874 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
5880 * Inserts (key, value) pair in \a dt index object.
5882 * \param dt osd index object
5883 * \param key key for index
5884 * \param rec record reference
5885 * \param th transaction handler
5888 * \retval -ve failure
5890 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
5891 const struct dt_rec *rec,
5892 const struct dt_key *key, struct thandle *th)
5894 struct osd_object *obj = osd_dt_obj(dt);
5895 struct iam_path_descr *ipd;
5896 struct osd_thandle *oh;
5897 struct iam_container *bag;
5898 struct osd_thread_info *oti = osd_oti_get(env);
5899 struct iam_rec *iam_rec;
5904 if (!dt_object_exists(dt))
5907 LINVRNT(osd_invariant(obj));
5908 LASSERT(!dt_object_remote(dt));
5910 bag = &obj->oo_dir->od_container;
5911 LASSERT(bag->ic_object == obj->oo_inode);
5912 LASSERT(th != NULL);
5914 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5916 ipd = osd_idx_ipd_get(env, bag);
5917 if (unlikely(ipd == NULL))
5920 oh = container_of(th, struct osd_thandle, ot_super);
5921 LASSERT(oh->ot_handle != NULL);
5922 LASSERT(oh->ot_handle->h_transaction != NULL);
5923 if (S_ISDIR(obj->oo_inode->i_mode)) {
5924 iam_rec = (struct iam_rec *)oti->oti_ldp;
5925 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
5927 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5928 /* pack quota uid/gid */
5929 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5930 key = (const struct dt_key *)&oti->oti_quota_id;
5931 /* pack quota record */
5932 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
5933 iam_rec = (struct iam_rec *)rec;
5935 iam_rec = (struct iam_rec *)rec;
5938 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
5940 osd_ipd_put(env, bag, ipd);
5941 LINVRNT(osd_invariant(obj));
5942 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5947 * Calls ldiskfs_add_entry() to add directory entry
5948 * into the directory. This is required for
5949 * interoperability mode (b11826)
5951 * \retval 0, on success
5952 * \retval -ve, on error
5954 static int __osd_ea_add_rec(struct osd_thread_info *info,
5955 struct osd_object *pobj, struct inode *cinode,
5956 const char *name, const struct lu_fid *fid,
5957 struct htree_lock *hlock, struct thandle *th)
5959 struct ldiskfs_dentry_param *ldp;
5960 struct dentry *child;
5961 struct osd_thandle *oth;
5965 oth = container_of(th, struct osd_thandle, ot_super);
5966 LASSERT(oth->ot_handle != NULL);
5967 LASSERT(oth->ot_handle->h_transaction != NULL);
5968 LASSERT(pobj->oo_inode);
5970 rc = obj_name2lu_name(pobj, name, strlen(name), &ln);
5974 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
5975 if (unlikely(osd_object_is_root(pobj)))
5978 osd_get_ldiskfs_dirent_param(ldp, fid);
5979 child = osd_child_dentry_get(info->oti_env, pobj,
5980 ln.ln_name, ln.ln_namelen);
5981 child->d_fsdata = (void *)ldp;
5982 dquot_initialize(pobj->oo_inode);
5983 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
5984 child, cinode, hlock);
5985 if (rc == 0 && CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
5986 struct ldiskfs_dir_entry_2 *de;
5987 struct buffer_head *bh;
5990 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
5993 rc1 = osd_ldiskfs_journal_get_write_access(
5994 oth->ot_handle, pobj->oo_inode->i_sb, bh,
5997 if (S_ISDIR(cinode->i_mode))
5998 de->file_type = LDISKFS_DIRENT_LUFID |
5999 LDISKFS_FT_REG_FILE;
6001 de->file_type = LDISKFS_DIRENT_LUFID |
6003 ldiskfs_handle_dirty_metadata(oth->ot_handle,
6010 if (ln.ln_name != name)
6016 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
6017 * into the directory.Also sets flags into osd object to
6018 * indicate dot and dotdot are created. This is required for
6019 * interoperability mode (b11826)
6021 * \param dir directory for dot and dotdot fixup.
6022 * \param obj child object for linking
6024 * \retval 0, on success
6025 * \retval -ve, on error
6027 static int osd_add_dot_dotdot(struct osd_thread_info *info,
6028 struct osd_object *dir,
6029 struct inode *parent_dir, const char *name,
6030 const struct lu_fid *dot_fid,
6031 const struct lu_fid *dot_dot_fid,
6034 struct inode *inode = dir->oo_inode;
6035 struct osd_thandle *oth;
6038 oth = container_of(th, struct osd_thandle, ot_super);
6039 LASSERT(oth->ot_handle->h_transaction != NULL);
6040 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
6042 if (strcmp(name, dot) == 0) {
6043 if (dir->oo_compat_dot_created) {
6046 LASSERT(inode->i_ino == parent_dir->i_ino);
6047 dir->oo_compat_dot_created = 1;
6050 } else if (strcmp(name, dotdot) == 0) {
6051 if (!dir->oo_compat_dot_created)
6053 /* in case of rename, dotdot is already created */
6054 if (dir->oo_compat_dotdot_created) {
6055 return __osd_ea_add_rec(info, dir, parent_dir, name,
6056 dot_dot_fid, NULL, th);
6059 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
6060 struct lu_fid tfid = *dot_dot_fid;
6063 result = osd_add_dot_dotdot_internal(info,
6064 dir->oo_inode, parent_dir, dot_fid,
6067 result = osd_add_dot_dotdot_internal(info,
6068 dir->oo_inode, parent_dir, dot_fid,
6073 dir->oo_compat_dotdot_created = 1;
6081 * It will call the appropriate osd_add* function and return the
6082 * value, return by respective functions.
6084 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
6085 struct inode *cinode, const char *name,
6086 const struct lu_fid *fid, struct thandle *th)
6088 struct osd_thread_info *info = osd_oti_get(env);
6089 struct htree_lock *hlock;
6092 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
6094 if (name[0] == '.' && (name[1] == '\0' ||
6095 (name[1] == '.' && name[2] == '\0'))) {
6096 if (hlock != NULL) {
6097 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
6100 down_write(&pobj->oo_ext_idx_sem);
6103 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
6104 lu_object_fid(&pobj->oo_dt.do_lu),
6107 if (hlock != NULL) {
6108 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
6109 pobj->oo_inode, LDISKFS_HLOCK_ADD);
6111 down_write(&pobj->oo_ext_idx_sem);
6114 if (CFS_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
6115 struct lu_fid *tfid = &info->oti_fid;
6119 rc = __osd_ea_add_rec(info, pobj, cinode, name,
6122 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
6126 if (!rc && fid_is_namespace_visible(lu_object_fid(&pobj->oo_dt.do_lu))
6127 && pobj->oo_dirent_count != LU_DIRENT_COUNT_UNSET)
6128 pobj->oo_dirent_count++;
6131 ldiskfs_htree_unlock(hlock);
6133 up_write(&pobj->oo_ext_idx_sem);
6139 osd_ldiskfs_consistency_check(struct osd_thread_info *oti,
6140 struct osd_device *dev,
6141 const struct lu_fid *fid,
6142 struct osd_inode_id *id)
6144 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
6145 struct inode *inode = NULL;
6151 if (!scrub_needs_check(scrub, fid, id->oii_ino))
6154 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
6155 if (rc == -ENOENT) {
6156 __u32 gen = id->oii_gen;
6162 inode = osd_iget(oti, dev, id);
6163 /* The inode has been removed (by race maybe). */
6164 if (IS_ERR(inode)) {
6165 rc = PTR_ERR(inode);
6167 RETURN(rc == -ESTALE ? -ENOENT : rc);
6170 /* The OI mapping is lost. */
6171 if (gen != OSD_OII_NOGEN)
6175 * The inode may has been reused by others, we do not know,
6176 * leave it to be handled by subsequent osd_fid_lookup().
6179 } else if (rc || osd_id_eq(id, &oti->oti_id)) {
6186 if (scrub->os_running) {
6187 if (inode == NULL) {
6188 inode = osd_iget(oti, dev, id);
6189 /* The inode has been removed (by race maybe). */
6190 if (IS_ERR(inode)) {
6191 rc = PTR_ERR(inode);
6193 RETURN(rc == -ESTALE ? -ENOENT : rc);
6197 rc = osd_scrub_oi_insert(dev, fid, id, insert);
6199 * There is race condition between osd_oi_lookup and OI scrub.
6200 * The OI scrub finished just after osd_oi_lookup() failure.
6201 * Under such case, it is unnecessary to trigger OI scrub again,
6202 * but try to call osd_oi_lookup() again.
6204 if (unlikely(rc == -EAGAIN))
6207 if (!S_ISDIR(inode->i_mode))
6210 rc = osd_check_lmv(oti, dev, inode);
6215 if (dev->od_scrub.os_scrub.os_auto_scrub_interval != AS_NEVER &&
6217 rc = osd_scrub_start(oti->oti_env, dev, SS_AUTO_PARTIAL |
6218 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
6219 CDEBUG_LIMIT(D_LFSCK | D_CONSOLE | D_WARNING,
6220 "%s: trigger partial OI scrub for RPC inconsistency, checking FID "DFID"/%u: rc = %d\n",
6221 osd_dev2name(dev), PFID(fid), id->oii_ino, rc);
6222 if (rc == 0 || rc == -EALREADY)
6234 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
6235 struct osd_device *dev,
6236 struct lu_fid *fid, __u32 ino)
6238 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
6239 struct osd_idmap_cache *oic = &oti->oti_cache;
6240 struct inode *inode;
6243 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
6244 inode = osd_iget(oti, dev, &oic->oic_lid);
6245 if (IS_ERR(inode)) {
6246 fid_zero(&oic->oic_fid);
6247 return PTR_ERR(inode);
6250 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, loa);
6253 fid_zero(&oic->oic_fid);
6255 *fid = oic->oic_fid = loa->loa_lma.lma_self_fid;
6259 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
6260 struct osd_inode_id *id, const struct lu_fid *fid)
6262 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
6263 id->oii_ino, id->oii_gen, info);
6264 info->oti_cache.oic_lid = *id;
6265 info->oti_cache.oic_fid = *fid;
6266 info->oti_cache.oic_dev = osd;
6270 * Get parent FID from the linkEA.
6272 * For a directory which parent resides on remote MDT, to satisfy the
6273 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
6274 * the other hand, to make the lookup(..) on the directory can return
6275 * the real parent FID, we append the real parent FID after its ".."
6276 * name entry in the /REMOTE_PARENT_DIR.
6278 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
6279 * backup. So after the restore, we cannot get the right parent FID from
6280 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
6281 * we have stored the real parent FID in the directory object's linkEA,
6282 * we can parse the linkEA for the real parent FID.
6284 * \param[in] env pointer to the thread context
6285 * \param[in] obj pointer to the object to be handled
6286 * \param[out]fid pointer to the buffer to hold the parent FID
6288 * \retval 0 for getting the real parent FID successfully
6289 * \retval negative error number on failure
6291 static int osd_get_pfid_from_linkea(const struct lu_env *env,
6292 struct osd_object *obj,
6295 struct osd_thread_info *oti = osd_oti_get(env);
6296 struct lu_buf *buf = &oti->oti_big_buf;
6297 struct dentry *dentry = &oti->oti_obj_dentry;
6298 struct inode *inode = obj->oo_inode;
6299 struct linkea_data ldata = { NULL };
6305 if (!S_ISDIR(inode->i_mode))
6309 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
6310 buf->lb_buf, buf->lb_len);
6311 if (rc == -ERANGE) {
6312 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
6315 lu_buf_realloc(buf, rc);
6316 if (buf->lb_buf == NULL)
6323 if (unlikely(rc == 0))
6329 if (unlikely(buf->lb_buf == NULL)) {
6330 lu_buf_realloc(buf, rc);
6331 if (buf->lb_buf == NULL)
6338 rc = linkea_init_with_rec(&ldata);
6340 linkea_first_entry(&ldata);
6341 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
6347 static int osd_verify_ent_by_linkea(const struct lu_env *env,
6348 struct inode *inode,
6349 const struct lu_fid *pfid,
6350 const char *name, const int namelen)
6352 struct osd_thread_info *oti = osd_oti_get(env);
6353 struct lu_buf *buf = &oti->oti_big_buf;
6354 struct dentry *dentry = &oti->oti_obj_dentry;
6355 struct linkea_data ldata = { NULL };
6356 struct lu_name cname = { .ln_name = name,
6357 .ln_namelen = namelen };
6363 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
6364 buf->lb_buf, buf->lb_len);
6366 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
6371 if (unlikely(rc == 0))
6374 if (buf->lb_len < rc) {
6375 lu_buf_realloc(buf, rc);
6376 if (buf->lb_buf == NULL)
6383 rc = linkea_init_with_rec(&ldata);
6385 rc = linkea_links_find(&ldata, &cname, pfid);
6391 * Calls ->lookup() to find dentry. From dentry get inode and
6392 * read inode's ea to get fid. This is required for interoperability
6395 * \retval 0, on success
6396 * \retval -ve, on error
6398 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
6399 struct dt_rec *rec, const struct lu_name *ln)
6401 struct inode *dir = obj->oo_inode;
6402 struct dentry *dentry;
6403 struct ldiskfs_dir_entry_2 *de;
6404 struct buffer_head *bh;
6405 struct lu_fid *fid = (struct lu_fid *)rec;
6406 struct htree_lock *hlock = NULL;
6412 LASSERT(dir->i_op != NULL);
6413 LASSERT(dir->i_op->lookup != NULL);
6415 dentry = osd_child_dentry_get(env, obj, ln->ln_name, ln->ln_namelen);
6417 if (obj->oo_hl_head != NULL) {
6418 hlock = osd_oti_get(env)->oti_hlock;
6419 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
6420 dir, LDISKFS_HLOCK_LOOKUP);
6422 down_read(&obj->oo_ext_idx_sem);
6425 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6427 struct osd_thread_info *oti = osd_oti_get(env);
6428 struct osd_inode_id *id = &oti->oti_id;
6429 struct osd_device *dev = osd_obj2dev(obj);
6431 ino = le32_to_cpu(de->inode);
6432 if (CFS_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
6434 rc = osd_fail_fid_lookup(oti, dev, fid, ino);
6438 rc = osd_get_fid_from_dentry(de, rec);
6440 /* done with de, release bh */
6443 if (unlikely(is_remote_parent_ino(dev, ino))) {
6445 * If the parent is on remote MDT, and there
6446 * is no FID-in-dirent, then we have to get
6447 * the parent FID from the linkEA.
6449 if (likely(ln->ln_namelen == 2 &&
6450 ln->ln_name[0] == '.' && ln->ln_name[1] == '.'))
6451 rc = osd_get_pfid_from_linkea(env, obj,
6454 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6457 osd_id_gen(id, ino, OSD_OII_NOGEN);
6460 if (rc != 0 || osd_remote_fid(env, dev, fid))
6463 rc = osd_ldiskfs_consistency_check(oti, dev, fid, id);
6464 if (rc != -ENOENT) {
6465 /* Other error should not affect lookup result. */
6468 /* Normal file mapping should be added into OI cache
6469 * after FID in LMA check, but for local files like
6470 * hsm_actions, their FIDs are not stored in OI files,
6471 * see osd_initial_OI_scrub(), and here is the only
6472 * place to load mapping into OI cache.
6474 if (!fid_is_namespace_visible(fid))
6475 osd_add_oi_cache(osd_oti_get(env),
6476 osd_obj2dev(obj), id, fid);
6478 CDEBUG(D_INODE, DFID"/"DNAME" => "DFID"\n",
6479 PFID(lu_object_fid(&obj->oo_dt.do_lu)), PNAME(ln),
6489 ldiskfs_htree_unlock(hlock);
6491 up_read(&obj->oo_ext_idx_sem);
6495 static int osd_index_declare_ea_insert(const struct lu_env *env,
6496 struct dt_object *dt,
6497 const struct dt_rec *rec,
6498 const struct dt_key *key,
6499 struct thandle *handle)
6501 struct osd_thandle *oh;
6502 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
6503 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
6504 const struct lu_fid *fid = rec1->rec_fid;
6505 int credits, rc = 0;
6506 struct osd_idmap_cache *idc;
6510 LASSERT(!dt_object_remote(dt));
6511 LASSERT(handle != NULL);
6512 LASSERT(fid != NULL);
6513 LASSERT(rec1->rec_type != 0);
6515 oh = container_of(handle, struct osd_thandle, ot_super);
6516 LASSERT(oh->ot_handle == NULL);
6518 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
6521 * we can't call iget() while a transactions is running
6522 * (this can lead to a deadlock), but we need to know
6523 * inum and object type. so we find this information at
6524 * declaration and cache in per-thread info
6526 idc = osd_idc_find_or_init(env, osd, fid);
6528 RETURN(PTR_ERR(idc));
6529 if (idc->oic_remote) {
6531 * a reference to remote inode is represented by an
6532 * agent inode which we have to create
6534 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
6535 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
6538 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
6540 if (osd_dt_obj(dt)->oo_inode != NULL) {
6541 struct inode *inode = osd_dt_obj(dt)->oo_inode;
6544 * We ignore block quota on meta pool (MDTs), so needn't
6545 * calculate how many blocks will be consumed by this index
6548 rc = osd_declare_inode_qid(env, i_uid_read(inode),
6550 i_projid_read(inode), 0,
6551 oh, osd_dt_obj(dt), NULL,
6556 #ifdef HAVE_PROJECT_QUOTA
6558 * Reserve credits for local agent inode to transfer
6559 * to 0, quota enforcement is ignored in this case.
6561 if (idc->oic_remote &&
6562 LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
6563 i_projid_read(inode) != 0)
6564 rc = osd_declare_attr_qid(env, osd_dt_obj(dt), oh,
6565 0, i_projid_read(inode),
6566 0, false, PRJQUOTA);
6574 * Index add function for interoperability mode (b11826).
6575 * It will add the directory entry.This entry is needed to
6576 * maintain name->fid mapping.
6578 * \param key it is key i.e. file entry to be inserted
6579 * \param rec it is value of given key i.e. fid
6581 * \retval 0, on success
6582 * \retval -ve, on error
6584 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
6585 const struct dt_rec *rec,
6586 const struct dt_key *key, struct thandle *th)
6588 struct osd_object *obj = osd_dt_obj(dt);
6589 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
6590 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
6591 const struct lu_fid *fid = rec1->rec_fid;
6592 const char *name = (const char *)key;
6593 struct osd_thread_info *oti = osd_oti_get(env);
6594 struct inode *child_inode = NULL;
6595 struct osd_idmap_cache *idc;
6600 if (!dt_object_exists(dt))
6603 LASSERT(osd_invariant(obj));
6604 LASSERT(!dt_object_remote(dt));
6605 LASSERT(th != NULL);
6607 osd_trans_exec_op(env, th, OSD_OT_INSERT);
6609 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
6611 idc = osd_idc_find(env, osd, fid);
6612 if (unlikely(idc == NULL)) {
6613 idc = osd_idc_find_or_init(env, osd, fid);
6616 * this dt_insert() wasn't declared properly, so
6617 * FID is missing in OI cache. we better do not
6618 * lookup FID in FLDB/OI and don't risk to deadlock,
6619 * but in some special cases (lfsck testing, etc)
6620 * it's much simpler than fixing a caller.
6622 * normally this error should be placed after the first
6623 * find, but migrate may attach source stripes to
6624 * target, which doesn't create stripes.
6626 CERROR("%s: "DFID" wasn't declared for insert\n",
6627 osd_name(osd), PFID(fid));
6629 RETURN(PTR_ERR(idc));
6633 if (idc->oic_remote) {
6634 /* Insert remote entry */
6635 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
6637 igrab(osd->od_mdt_map->omm_remote_parent->d_inode);
6639 child_inode = osd_create_local_agent_inode(env, osd,
6640 obj, fid, rec1->rec_type & S_IFMT, th);
6641 if (IS_ERR(child_inode))
6642 RETURN(PTR_ERR(child_inode));
6645 /* Insert local entry */
6646 if (unlikely(idc->oic_lid.oii_ino == 0)) {
6647 /* for a reason OI cache wasn't filled properly */
6648 CERROR("%s: OIC for "DFID" isn't filled\n",
6649 osd_name(osd), PFID(fid));
6652 child_inode = oti->oti_inode;
6653 if (unlikely(child_inode == NULL)) {
6654 struct ldiskfs_inode_info *lii;
6659 child_inode = oti->oti_inode = &lii->vfs_inode;
6661 child_inode->i_sb = osd_sb(osd);
6662 child_inode->i_ino = idc->oic_lid.oii_ino;
6663 child_inode->i_mode = rec1->rec_type & S_IFMT;
6666 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
6668 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
6669 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
6671 if (child_inode && child_inode != oti->oti_inode)
6673 LASSERT(osd_invariant(obj));
6674 osd_trans_exec_check(env, th, OSD_OT_INSERT);
6680 * Initialize osd Iterator for given osd index object.
6682 * \param dt osd index object
6685 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
6686 struct dt_object *dt,
6689 struct osd_it_iam *it;
6690 struct osd_object *obj = osd_dt_obj(dt);
6691 struct lu_object *lo = &dt->do_lu;
6692 struct iam_path_descr *ipd;
6693 struct iam_container *bag = &obj->oo_dir->od_container;
6695 if (!dt_object_exists(dt))
6696 return ERR_PTR(-ENOENT);
6700 return ERR_PTR(-ENOMEM);
6702 ipd = osd_it_ipd_get(env, bag);
6703 if (likely(ipd != NULL)) {
6707 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
6708 return (struct dt_it *)it;
6711 return ERR_PTR(-ENOMEM);
6716 * free given Iterator.
6718 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
6720 struct osd_it_iam *it = (struct osd_it_iam *)di;
6721 struct osd_object *obj = it->oi_obj;
6723 iam_it_fini(&it->oi_it);
6724 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
6725 osd_object_put(env, obj);
6730 * Move Iterator to record specified by \a key
6732 * \param di osd iterator
6733 * \param key key for index
6735 * \retval +ve di points to record with least key not larger than key
6736 * \retval 0 di points to exact matched key
6737 * \retval -ve failure
6740 static int osd_it_iam_get(const struct lu_env *env,
6741 struct dt_it *di, const struct dt_key *key)
6743 struct osd_thread_info *oti = osd_oti_get(env);
6744 struct osd_it_iam *it = (struct osd_it_iam *)di;
6746 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
6747 /* swab quota uid/gid */
6748 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
6749 key = (struct dt_key *)&oti->oti_quota_id;
6752 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
6758 * \param di osd iterator
6760 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
6762 struct osd_it_iam *it = (struct osd_it_iam *)di;
6764 iam_it_put(&it->oi_it);
6768 * Move iterator by one record
6770 * \param di osd iterator
6772 * \retval +1 end of container reached
6774 * \retval -ve failure
6777 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
6779 struct osd_it_iam *it = (struct osd_it_iam *)di;
6781 return iam_it_next(&it->oi_it);
6785 * Return pointer to the key under iterator.
6788 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
6789 const struct dt_it *di)
6791 struct osd_thread_info *oti = osd_oti_get(env);
6792 struct osd_it_iam *it = (struct osd_it_iam *)di;
6793 struct osd_object *obj = it->oi_obj;
6796 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
6798 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
6799 /* swab quota uid/gid */
6800 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
6801 key = (struct dt_key *)&oti->oti_quota_id;
6808 * Return size of key under iterator (in bytes)
6811 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
6813 struct osd_it_iam *it = (struct osd_it_iam *)di;
6815 return iam_it_key_size(&it->oi_it);
6819 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
6821 /* check if file type is required */
6822 if (ent->lde_attrs & LUDA_TYPE) {
6823 struct luda_type *lt;
6824 int align = sizeof(*lt) - 1;
6826 len = (len + align) & ~align;
6827 lt = (struct luda_type *)(ent->lde_name + len);
6828 lt->lt_type = cpu_to_le16(DTTOIF(type));
6831 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
6835 * build lu direct from backend fs dirent.
6839 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
6840 char *name, __u16 namelen, __u16 type, __u32 attr)
6842 ent->lde_attrs = attr | LUDA_FID;
6843 fid_cpu_to_le(&ent->lde_fid, fid);
6845 ent->lde_hash = cpu_to_le64(offset);
6846 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
6848 strncpy(ent->lde_name, name, namelen);
6849 ent->lde_name[namelen] = '\0';
6850 ent->lde_namelen = cpu_to_le16(namelen);
6852 /* append lustre attributes */
6853 osd_it_append_attrs(ent, namelen, type);
6857 * Return pointer to the record under iterator.
6859 static int osd_it_iam_rec(const struct lu_env *env,
6860 const struct dt_it *di,
6861 struct dt_rec *dtrec, __u32 attr)
6863 struct osd_it_iam *it = (struct osd_it_iam *)di;
6864 struct osd_thread_info *info = osd_oti_get(env);
6868 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
6869 const struct osd_fid_pack *rec;
6870 struct lu_fid *fid = &info->oti_fid;
6871 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6877 name = (char *)iam_it_key_get(&it->oi_it);
6879 RETURN(PTR_ERR(name));
6881 namelen = iam_it_key_size(&it->oi_it);
6883 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
6885 RETURN(PTR_ERR(rec));
6887 rc = osd_fid_unpack(fid, rec);
6891 hash = iam_it_store(&it->oi_it);
6893 /* IAM does not store object type in IAM index (dir) */
6894 osd_it_pack_dirent(lde, fid, hash, name, namelen,
6896 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
6897 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6898 (struct iam_rec *)dtrec);
6899 osd_quota_unpack(it->oi_obj, dtrec);
6901 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6902 (struct iam_rec *)dtrec);
6909 * Returns cookie for current Iterator position.
6911 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
6913 struct osd_it_iam *it = (struct osd_it_iam *)di;
6915 return iam_it_store(&it->oi_it);
6919 * Restore iterator from cookie.
6921 * \param di osd iterator
6922 * \param hash Iterator location cookie
6924 * \retval +ve di points to record with least key not larger than key.
6925 * \retval 0 di points to exact matched key
6926 * \retval -ve failure
6929 static int osd_it_iam_load(const struct lu_env *env,
6930 const struct dt_it *di, __u64 hash)
6932 struct osd_it_iam *it = (struct osd_it_iam *)di;
6934 return iam_it_load(&it->oi_it, hash);
6937 static const struct dt_index_operations osd_index_iam_ops = {
6938 .dio_lookup = osd_index_iam_lookup,
6939 .dio_declare_insert = osd_index_declare_iam_insert,
6940 .dio_insert = osd_index_iam_insert,
6941 .dio_declare_delete = osd_index_declare_iam_delete,
6942 .dio_delete = osd_index_iam_delete,
6944 .init = osd_it_iam_init,
6945 .fini = osd_it_iam_fini,
6946 .get = osd_it_iam_get,
6947 .put = osd_it_iam_put,
6948 .next = osd_it_iam_next,
6949 .key = osd_it_iam_key,
6950 .key_size = osd_it_iam_key_size,
6951 .rec = osd_it_iam_rec,
6952 .store = osd_it_iam_store,
6953 .load = osd_it_iam_load
6957 struct osd_it_ea *osd_it_dir_init(const struct lu_env *env,
6958 struct osd_device *dev,
6959 struct inode *inode, u32 attr)
6961 struct osd_thread_info *info = osd_oti_get(env);
6962 struct osd_it_ea *oie;
6966 file = alloc_file_pseudo(inode, dev->od_mnt, "/", O_NOATIME,
6969 RETURN(ERR_CAST(file));
6971 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
6972 if (attr & LUDA_64BITHASH)
6973 file->f_mode |= FMODE_64BITHASH;
6975 file->f_mode |= FMODE_32BITHASH;
6978 OBD_SLAB_ALLOC_PTR(oie, osd_itea_cachep);
6982 oie->oie_rd_dirent = 0;
6983 oie->oie_it_dirent = 0;
6984 oie->oie_dirent = NULL;
6985 if (unlikely(!info->oti_it_ea_buf_used)) {
6986 oie->oie_buf = info->oti_it_ea_buf;
6987 info->oti_it_ea_buf_used = 1;
6989 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6993 oie->oie_obj = NULL;
6994 oie->oie_file = file;
6999 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
7003 return ERR_PTR(-ENOMEM);
7007 * Creates or initializes iterator context.
7009 * \retval struct osd_it_ea, iterator structure on success
7012 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
7013 struct dt_object *dt,
7016 struct osd_object *obj = osd_dt_obj(dt);
7017 struct osd_device *dev = osd_obj2dev(obj);
7018 struct lu_object *lo = &dt->do_lu;
7019 struct osd_it_ea *oie;
7023 if (!dt_object_exists(dt) || obj->oo_destroyed)
7024 RETURN(ERR_PTR(-ENOENT));
7026 oie = osd_it_dir_init(env, dev, obj->oo_inode, attr);
7028 RETURN(ERR_CAST(oie));
7032 RETURN((struct dt_it *)oie);
7035 void osd_it_dir_fini(const struct lu_env *env, struct osd_it_ea *oie,
7036 struct inode *inode)
7038 struct osd_thread_info *info = osd_oti_get(env);
7041 fput(oie->oie_file);
7042 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
7043 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
7045 info->oti_it_ea_buf_used = 0;
7046 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
7051 * Destroy or finishes iterator context.
7053 * \param di iterator structure to be destroyed
7055 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
7057 struct osd_it_ea *oie = (struct osd_it_ea *)di;
7058 struct osd_object *obj = oie->oie_obj;
7059 struct inode *inode = obj->oo_inode;
7062 osd_it_dir_fini(env, (struct osd_it_ea *)di, inode);
7063 osd_object_put(env, obj);
7068 * It position the iterator at given key, so that next lookup continues from
7069 * that key Or it is similar to dio_it->load() but based on a key,
7070 * rather than file position.
7072 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
7075 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
7077 static int osd_it_ea_get(const struct lu_env *env,
7078 struct dt_it *di, const struct dt_key *key)
7080 struct osd_it_ea *it = (struct osd_it_ea *)di;
7083 LASSERT(((const char *)key)[0] == '\0');
7084 it->oie_file->f_pos = 0;
7085 it->oie_rd_dirent = 0;
7086 it->oie_it_dirent = 0;
7087 it->oie_dirent = NULL;
7095 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
7099 struct osd_filldir_cbs {
7100 struct dir_context ctx;
7101 struct osd_it_ea *it;
7104 * It is called internally by ->iterate*(). It fills the
7105 * iterator's in-memory data structure with required
7106 * information i.e. name, namelen, rec_size etc.
7108 * \param buf in which information to be filled in.
7109 * \param name name of the file in given dir
7111 * \retval 0 on success
7112 * \retval 1 on buffer full
7114 #ifdef HAVE_FILLDIR_USE_CTX
7115 static FILLDIR_TYPE do_osd_ldiskfs_filldir(struct dir_context *ctx,
7117 static int osd_ldiskfs_filldir(void *ctx,
7119 const char *name, int namelen,
7120 loff_t offset, __u64 ino, unsigned int d_type)
7122 struct osd_it_ea *it = ((struct osd_filldir_cbs *)ctx)->it;
7123 struct osd_object *obj = it->oie_obj;
7124 struct osd_it_ea_dirent *ent = it->oie_dirent;
7125 struct lu_fid *fid = &ent->oied_fid;
7126 char *buf = it->oie_buf;
7127 struct osd_fid_pack *rec;
7130 /* this should never happen */
7131 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
7132 CERROR("ldiskfs return invalid namelen %d\n", namelen);
7136 /* Check for enough space. Note oied_name is not NUL terminated. */
7137 if (&ent->oied_name[namelen] > buf + OSD_IT_EA_BUFSIZE)
7140 /* "." is just the object itself. */
7141 if (namelen == 1 && name[0] == '.') {
7143 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
7144 } else if (d_type & LDISKFS_DIRENT_LUFID) {
7145 rec = (struct osd_fid_pack *)(name + namelen + 1);
7146 if (osd_fid_unpack(fid, rec) != 0)
7151 d_type &= ~LDISKFS_DIRENT_LUFID;
7153 /* NOT export local root. */
7155 unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
7156 ino = obj->oo_inode->i_ino;
7157 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
7160 if (obj == NULL || !(obj->oo_lma_flags & LUSTRE_ENCRYPT_FL)) {
7161 ent->oied_namelen = namelen;
7162 memcpy(ent->oied_name, name, namelen);
7164 int encoded_namelen = critical_chars(name, namelen);
7166 /* Check again for enough space. */
7167 if (&ent->oied_name[encoded_namelen] > buf + OSD_IT_EA_BUFSIZE)
7170 ent->oied_namelen = encoded_namelen;
7172 if (encoded_namelen == namelen)
7173 memcpy(ent->oied_name, name, namelen);
7175 critical_encode(name, namelen, ent->oied_name);
7178 ent->oied_ino = ino;
7179 ent->oied_off = offset;
7180 ent->oied_type = d_type;
7182 it->oie_rd_dirent++;
7183 it->oie_dirent = (void *)ent + round_up(sizeof(*ent) + ent->oied_namelen, 8);
7187 WRAP_FILLDIR_FN(do_, osd_ldiskfs_filldir)
7190 * Calls ->iterate*() to load a directory entry at a time
7191 * and stored it in iterator's in-memory data structure.
7193 * \param di iterator's in memory structure
7195 * \retval 0 on success
7196 * \retval -ve on error
7197 * \retval +1 reach the end of entry
7199 int osd_ldiskfs_it_fill(const struct lu_env *env, const struct dt_it *di)
7201 struct osd_it_ea *it = (struct osd_it_ea *)di;
7202 struct osd_object *obj = it->oie_obj;
7203 struct htree_lock *hlock = NULL;
7204 struct file *filp = it->oie_file;
7206 struct osd_filldir_cbs buf = {
7207 .ctx.actor = osd_ldiskfs_filldir,
7212 it->oie_dirent = it->oie_buf;
7213 it->oie_rd_dirent = 0;
7216 if (obj->oo_hl_head != NULL) {
7217 hlock = osd_oti_get(env)->oti_hlock;
7218 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
7220 LDISKFS_HLOCK_READDIR);
7222 down_read(&obj->oo_ext_idx_sem);
7226 rc = iterate_dir(filp, &buf.ctx);
7230 if (it->oie_rd_dirent == 0) {
7232 * If it does not get any dirent, it means it has been reached
7233 * to the end of the dir
7235 it->oie_file->f_pos = ldiskfs_get_htree_eof(it->oie_file);
7239 it->oie_dirent = it->oie_buf;
7240 it->oie_it_dirent = 1;
7245 ldiskfs_htree_unlock(hlock);
7247 up_read(&obj->oo_ext_idx_sem);
7254 * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
7255 * to load a directory entry at a time and stored it in
7256 * iterator's in-memory data structure.
7258 * \param di iterator's in memory structure
7260 * \retval +ve iterator reached to end
7261 * \retval 0 iterator not reached to end
7262 * \retval -ve on error
7264 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
7266 struct osd_it_ea *it = (struct osd_it_ea *)di;
7271 if (it->oie_it_dirent < it->oie_rd_dirent) {
7273 (void *)it->oie_dirent +
7274 round_up(sizeof(struct osd_it_ea_dirent) +
7275 it->oie_dirent->oied_namelen, 8);
7276 it->oie_it_dirent++;
7279 if (it->oie_file->f_pos == ldiskfs_get_htree_eof(it->oie_file))
7282 rc = osd_ldiskfs_it_fill(env, di);
7289 * Returns the key at current position from iterator's in memory structure.
7291 * \param di iterator's in memory structure
7293 * \retval key i.e. struct dt_key on success
7295 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
7296 const struct dt_it *di)
7298 struct osd_it_ea *it = (struct osd_it_ea *)di;
7300 return (struct dt_key *)it->oie_dirent->oied_name;
7304 * Returns key's size at current position from iterator's in memory structure.
7306 * \param di iterator's in memory structure
7308 * \retval key_size i.e. struct dt_key on success
7310 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
7312 struct osd_it_ea *it = (struct osd_it_ea *)di;
7314 return it->oie_dirent->oied_namelen;
7317 #if defined LDISKFS_DIR_ENTRY_LEN && defined LDISKFS_DIR_ENTRY_LEN_
7318 #undef LDISKFS_DIR_REC_LEN
7319 # if defined LDISKFS_DIR_REC_LEN_WITH_DIR
7320 # define LDISKFS_DIR_REC_LEN(de, dir) LDISKFS_DIR_ENTRY_LEN_((de), (dir))
7322 # define LDISKFS_DIR_REC_LEN(de) LDISKFS_DIR_ENTRY_LEN_((de))
7326 #if defined LDISKFS_DIR_REC_LEN_WITH_DIR
7327 # define LDISKFS_DIR_REC_LEN_DIR(de) LDISKFS_DIR_REC_LEN((de), NULL)
7329 # define LDISKFS_DIR_REC_LEN_DIR(de) LDISKFS_DIR_REC_LEN((de))
7332 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
7334 if (LDISKFS_DIR_REC_LEN_DIR(de) >=
7335 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
7342 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
7343 unsigned int blocksize, bool dotdot)
7346 return osd_dotdot_has_space(de);
7348 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
7349 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
7356 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
7357 handle_t *jh, struct dentry *dentry,
7358 const struct lu_fid *fid, struct buffer_head *bh,
7359 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
7362 struct inode *dir = dentry->d_parent->d_inode;
7363 struct inode *inode = dentry->d_inode;
7364 struct osd_fid_pack *rec;
7365 struct ldiskfs_dentry_param *ldp;
7366 int namelen = dentry->d_name.len;
7368 struct osd_thread_info *info = osd_oti_get(env);
7372 if (!ldiskfs_has_feature_dirdata(inode->i_sb))
7375 /* There is enough space to hold the FID-in-dirent. */
7376 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
7377 rc = osd_ldiskfs_journal_get_write_access(jh, dir->i_sb, bh,
7382 de->name[namelen] = 0;
7383 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
7384 rec->fp_len = sizeof(struct lu_fid) + 1;
7385 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
7386 de->file_type |= LDISKFS_DIRENT_LUFID;
7387 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
7394 rc = ldiskfs_delete_entry(jh, dir, de, bh);
7398 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
7399 osd_get_ldiskfs_dirent_param(ldp, fid);
7400 dentry->d_fsdata = (void *)ldp;
7401 dquot_initialize(dir);
7402 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
7404 * It is too bad, we cannot reinsert the name entry back.
7405 * That means we lose it!
7409 "%s: fail to reinsert the dirent, dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
7410 osd_ino2name(inode), dir->i_ino, dir->i_generation,
7411 namelen, dentry->d_name.name, PFID(fid), rc);
7417 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
7418 struct osd_it_ea *it, struct lu_fid *fid,
7419 struct osd_inode_id *id, __u32 *attr)
7421 struct osd_thread_info *info = osd_oti_get(env);
7422 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
7423 struct osd_device *dev = osd_obj2dev(obj);
7424 struct super_block *sb = osd_sb(dev);
7425 const char *devname = osd_name(dev);
7426 struct osd_it_ea_dirent *ent = it->oie_dirent;
7427 struct inode *dir = obj->oo_inode;
7428 struct htree_lock *hlock = NULL;
7429 struct buffer_head *bh = NULL;
7430 handle_t *jh = NULL;
7431 struct ldiskfs_dir_entry_2 *de;
7432 struct dentry *dentry;
7433 struct inode *inode;
7434 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
7437 bool dotdot = false;
7443 if (ent->oied_name[0] == '.') {
7444 if (ent->oied_namelen == 1)
7447 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
7451 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
7452 inode = osd_iget(info, dev, id);
7453 if (IS_ERR(inode)) {
7454 rc = PTR_ERR(inode);
7455 if (rc == -ENOENT || rc == -ESTALE) {
7457 * Maybe dangling name entry, or
7458 * corrupted directory entry.
7460 *attr |= LUDA_UNKNOWN;
7463 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
7464 "check_repair, dir = %lu/%u, name = %.*s, "
7465 "ino = %llu, rc = %d\n",
7466 devname, dir->i_ino, dir->i_generation,
7467 ent->oied_namelen, ent->oied_name,
7474 rc = obj_name2lu_name(obj, ent->oied_name, ent->oied_namelen, &ln);
7478 dentry = osd_child_dentry_by_inode(env, dir, ln.ln_name, ln.ln_namelen);
7479 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
7480 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
7486 * We need to ensure that the name entry is still valid.
7487 * Because it may be removed or renamed by other already.
7489 * The unlink or rename operation will start journal before PDO lock,
7490 * so to avoid deadlock, here we need to start journal handle before
7491 * related PDO lock also. But because we do not know whether there
7492 * will be something to be repaired before PDO lock, we just start
7493 * journal without conditions.
7495 * We may need to remove the name entry firstly, then insert back.
7496 * One credit is for user quota file update.
7497 * One credit is for group quota file update.
7498 * Two credits are for dirty inode.
7500 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
7501 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
7503 if (dev->od_dirent_journal != 0) {
7506 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
7509 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
7510 "check_repair, dir = %lu/%u, credits = %d, "
7511 "name = %.*s, ino = %llu: rc = %d\n",
7512 devname, dir->i_ino, dir->i_generation, credits,
7513 ent->oied_namelen, ent->oied_name,
7516 GOTO(out_inode, rc);
7519 if (obj->oo_hl_head != NULL) {
7520 hlock = osd_oti_get(env)->oti_hlock;
7522 * "0" means exclusive lock for the whole directory.
7523 * We need to prevent others access such name entry
7524 * during the delete + insert. Neither HLOCK_ADD nor
7525 * HLOCK_DEL cannot guarantee the atomicity.
7527 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
7529 down_write(&obj->oo_ext_idx_sem);
7532 if (obj->oo_hl_head != NULL) {
7533 hlock = osd_oti_get(env)->oti_hlock;
7534 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
7535 LDISKFS_HLOCK_LOOKUP);
7537 down_read(&obj->oo_ext_idx_sem);
7541 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
7542 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
7543 *attr |= LUDA_IGNORE;
7549 * For dotdot entry, if there is not enough space to hold the
7550 * FID-in-dirent, just keep them there. It only happens when the
7551 * device upgraded from 1.8 or restored from MDT file-level backup.
7552 * For the whole directory, only dotdot entry have no FID-in-dirent
7553 * and needs to get FID from LMA when readdir, it will not affect the
7556 if (dotdot && !osd_dotdot_has_space(de)) {
7557 *attr |= LUDA_UNKNOWN;
7563 if (lu_fid_eq(fid, &lma->lma_self_fid))
7566 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
7567 struct lu_fid *tfid = &lma->lma_self_fid;
7569 if (likely(dotdot &&
7570 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
7571 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
7573 * It must be REMOTE_PARENT_DIR and as the
7574 * 'dotdot' entry of remote directory
7576 *attr |= LUDA_IGNORE;
7578 CDEBUG(D_LFSCK, "%s: expect remote agent "
7579 "parent directory, but got %.*s under "
7580 "dir = %lu/%u with the FID "DFID"\n",
7581 devname, ent->oied_namelen,
7582 ent->oied_name, dir->i_ino,
7583 dir->i_generation, PFID(tfid));
7585 *attr |= LUDA_UNKNOWN;
7592 if (!fid_is_zero(fid)) {
7593 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
7595 if (rc == -ENOENT ||
7597 !(dev->od_scrub.os_scrub.os_file.sf_flags & SF_UPGRADE))) {
7599 * linkEA does not recognize the dirent entry,
7600 * it may because the dirent entry corruption
7601 * and points to other's inode.
7603 CDEBUG(D_LFSCK, "%s: the target inode does not "
7604 "recognize the dirent, dir = %lu/%u, "
7605 " name = %.*s, ino = %llu, "
7606 DFID": rc = %d\n", devname, dir->i_ino,
7607 dir->i_generation, ent->oied_namelen,
7608 ent->oied_name, ent->oied_ino, PFID(fid), rc);
7609 *attr |= LUDA_UNKNOWN;
7614 if (rc && rc != -ENODATA) {
7615 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
7616 "dir = %lu/%u, name = %.*s, ino = %llu, "
7617 DFID": rc = %d\n", devname, dir->i_ino,
7618 dir->i_generation, ent->oied_namelen,
7619 ent->oied_name, ent->oied_ino, PFID(fid), rc);
7620 *attr |= LUDA_UNKNOWN;
7628 * linkEA recognizes the dirent entry, the FID-in-LMA is
7629 * valid, trusted, in spite of fid_is_sane(fid) or not.
7631 if (*attr & LUDA_VERIFY_DRYRUN) {
7632 *fid = lma->lma_self_fid;
7633 *attr |= LUDA_REPAIR;
7640 dev->od_dirent_journal = 1;
7641 if (hlock != NULL) {
7642 ldiskfs_htree_unlock(hlock);
7645 up_read(&obj->oo_ext_idx_sem);
7651 *fid = lma->lma_self_fid;
7653 /* Update or append the FID-in-dirent. */
7654 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
7655 bh, de, hlock, dotdot);
7657 *attr |= LUDA_REPAIR;
7659 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
7660 "the dirent, dir = %lu/%u, name = %.*s, "
7661 "ino = %llu, "DFID": rc = %d\n",
7662 devname, dir->i_ino, dir->i_generation,
7663 ent->oied_namelen, ent->oied_name,
7664 ent->oied_ino, PFID(fid), rc);
7666 /* lma is NULL, trust the FID-in-dirent if it is valid. */
7667 if (*attr & LUDA_VERIFY_DRYRUN) {
7668 if (fid_is_sane(fid)) {
7669 *attr |= LUDA_REPAIR;
7670 } else if (dev->od_index == 0) {
7671 lu_igif_build(fid, inode->i_ino,
7672 inode->i_generation);
7673 *attr |= LUDA_UPGRADE;
7681 dev->od_dirent_journal = 1;
7682 if (hlock != NULL) {
7683 ldiskfs_htree_unlock(hlock);
7686 up_read(&obj->oo_ext_idx_sem);
7693 if (unlikely(fid_is_sane(fid))) {
7695 * FID-in-dirent exists, but FID-in-LMA is lost.
7696 * Trust the FID-in-dirent, and add FID-in-LMA.
7698 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
7700 *attr |= LUDA_REPAIR;
7702 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
7703 "update dirent, dir = %lu/%u, "
7704 "name = %.*s, ino = %llu, "
7706 devname, dir->i_ino, dir->i_generation,
7707 ent->oied_namelen, ent->oied_name,
7708 ent->oied_ino, PFID(fid), rc);
7709 } else if (dev->od_index == 0) {
7710 lu_igif_build(fid, inode->i_ino, inode->i_generation);
7712 * It is probably IGIF object. Only aappend the
7713 * FID-in-dirent. OI scrub will process FID-in-LMA.
7715 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
7716 bh, de, hlock, dotdot);
7718 *attr |= LUDA_UPGRADE;
7720 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
7721 "after the dirent, dir = %lu/%u, "
7722 "name = %.*s, ino = %llu, "
7724 devname, dir->i_ino, dir->i_generation,
7725 ent->oied_namelen, ent->oied_name,
7726 ent->oied_ino, PFID(fid), rc);
7735 if (hlock != NULL) {
7736 ldiskfs_htree_unlock(hlock);
7738 if (dev->od_dirent_journal != 0)
7739 up_write(&obj->oo_ext_idx_sem);
7741 up_read(&obj->oo_ext_idx_sem);
7745 ldiskfs_journal_stop(jh);
7749 if (rc >= 0 && !dirty)
7750 dev->od_dirent_journal = 0;
7751 if (ln.ln_name != ent->oied_name)
7758 * Returns the value at current position from iterator's in memory structure.
7760 * \param di struct osd_it_ea, iterator's in memory structure
7761 * \param attr attr requested for dirent.
7762 * \param lde lustre dirent
7764 * \retval 0 no error and \param lde has correct lustre dirent.
7765 * \retval -ve on error
7767 static inline int osd_it_ea_rec(const struct lu_env *env,
7768 const struct dt_it *di,
7769 struct dt_rec *dtrec, __u32 attr)
7771 struct osd_it_ea *it = (struct osd_it_ea *)di;
7772 struct osd_object *obj = it->oie_obj;
7773 struct osd_device *dev = osd_obj2dev(obj);
7774 struct osd_thread_info *oti = osd_oti_get(env);
7775 struct osd_inode_id *id = &oti->oti_id;
7776 struct lu_fid *fid = &it->oie_dirent->oied_fid;
7777 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
7778 __u32 ino = it->oie_dirent->oied_ino;
7783 LASSERT(!is_remote_parent_ino(dev, obj->oo_inode->i_ino));
7785 if (attr & LUDA_VERIFY) {
7786 if (unlikely(is_remote_parent_ino(dev, ino))) {
7787 attr |= LUDA_IGNORE;
7789 * If the parent is on remote MDT, and there
7790 * is no FID-in-dirent, then we have to get
7791 * the parent FID from the linkEA.
7793 if (!fid_is_sane(fid) &&
7794 it->oie_dirent->oied_namelen == 2 &&
7795 it->oie_dirent->oied_name[0] == '.' &&
7796 it->oie_dirent->oied_name[1] == '.')
7797 osd_get_pfid_from_linkea(env, obj, fid);
7799 rc = osd_dirent_check_repair(env, obj, it, fid, id,
7803 if (!fid_is_sane(fid))
7804 attr |= LUDA_UNKNOWN;
7806 attr &= ~LU_DIRENT_ATTRS_MASK;
7807 if (!fid_is_sane(fid)) {
7808 bool is_dotdot = false;
7810 if (it->oie_dirent->oied_namelen == 2 &&
7811 it->oie_dirent->oied_name[0] == '.' &&
7812 it->oie_dirent->oied_name[1] == '.')
7815 * If the parent is on remote MDT, and there
7816 * is no FID-in-dirent, then we have to get
7817 * the parent FID from the linkEA.
7819 if (is_remote_parent_ino(dev, ino) && is_dotdot) {
7820 rc = osd_get_pfid_from_linkea(env, obj, fid);
7822 if (is_dotdot == false &&
7823 CFS_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
7826 rc = osd_ea_fid_get(env, obj, ino, fid, id);
7831 /* Pack the entry anyway, at least the offset is right. */
7832 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
7833 it->oie_dirent->oied_name,
7834 it->oie_dirent->oied_namelen,
7835 it->oie_dirent->oied_type, attr);
7837 RETURN(rc > 0 ? 0 : rc);
7841 * Returns the record size size at current position.
7843 * This function will return record(lu_dirent) size in bytes.
7845 * \param[in] env execution environment
7846 * \param[in] di iterator's in memory structure
7847 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
7848 * calculate the lu_dirent size.
7850 * \retval record size(in bytes & in memory) of the current lu_dirent
7853 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
7856 struct osd_it_ea *it = (struct osd_it_ea *)di;
7858 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
7862 * Returns a cookie for current position of the iterator head, so that
7863 * user can use this cookie to load/start the iterator next time.
7865 * \param di iterator's in memory structure
7867 * \retval cookie for current position, on success
7869 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
7871 struct osd_it_ea *it = (struct osd_it_ea *)di;
7873 return it->oie_dirent->oied_off;
7877 * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
7878 * to load a directory entry at a time and stored it i inn,
7879 * in iterator's in-memory data structure.
7881 * \param di struct osd_it_ea, iterator's in memory structure
7883 * \retval +ve on success
7884 * \retval -ve on error
7886 static int osd_it_ea_load(const struct lu_env *env,
7887 const struct dt_it *di, __u64 hash)
7889 struct osd_it_ea *it = (struct osd_it_ea *)di;
7893 it->oie_file->f_pos = hash;
7895 rc = osd_ldiskfs_it_fill(env, di);
7905 int osd_olc_lookup(const struct lu_env *env, struct osd_object *obj,
7906 u64 iversion, struct dt_rec *rec,
7907 const struct lu_name *ln, int *result)
7909 struct osd_thread_info *oti = osd_oti_get(env);
7910 struct osd_lookup_cache *olc = oti->oti_lookup_cache;
7911 struct osd_device *osd = osd_obj2dev(obj);
7912 struct osd_lookup_cache_object *cobj = &oti->oti_cobj;
7915 if (unlikely(olc == NULL))
7918 if (unlikely(atomic_read(&osd_mount_seq) != olc->olc_mount_seq)) {
7920 * umount has happened, a new OSD could land to the previous
7921 * address so we can't use it any more, invalidate our cache
7923 memset(olc, 0, sizeof(*olc));
7924 olc->olc_mount_seq = atomic_read(&osd_mount_seq);
7928 memset(cobj, 0, sizeof(*cobj));
7929 cobj->lco_osd = osd;
7930 cobj->lco_ino = obj->oo_inode->i_ino;
7931 cobj->lco_gen = obj->oo_inode->i_generation;
7932 cobj->lco_version = iversion;
7934 for (i = 0; i < OSD_LOOKUP_CACHE_MAX; i++) {
7935 struct osd_lookup_cache_entry *entry;
7937 entry = &olc->olc_entry[i];
7938 /* compare if osd/ino/generation/version match */
7939 if (memcmp(&entry->lce_obj, cobj, sizeof(*cobj)) != 0)
7941 if (entry->lce_namelen != ln->ln_namelen)
7943 if (memcmp(entry->lce_name, ln->ln_name, ln->ln_namelen) != 0)
7946 memcpy(rec, &entry->lce_fid, sizeof(entry->lce_fid));
7947 *result = entry->lce_rc;
7953 void osd_olc_save(const struct lu_env *env, struct osd_object *obj,
7954 struct dt_rec *rec, const struct lu_name *ln,
7955 const int result, u64 iversion)
7957 struct osd_thread_info *oti = osd_oti_get(env);
7958 struct osd_lookup_cache_entry *entry;
7959 struct osd_lookup_cache *olc;
7961 if (unlikely(oti->oti_lookup_cache == NULL)) {
7962 OBD_ALLOC_PTR(oti->oti_lookup_cache);
7963 if (oti->oti_lookup_cache == NULL)
7967 olc = oti->oti_lookup_cache;
7968 if (unlikely(atomic_read(&osd_mount_seq) != olc->olc_mount_seq)) {
7969 memset(olc, 0, sizeof(*olc));
7970 olc->olc_mount_seq = atomic_read(&osd_mount_seq);
7973 entry = &olc->olc_entry[olc->olc_cur];
7975 /* invaliate cache slot if needed */
7976 if (entry->lce_obj.lco_osd)
7977 memset(&entry->lce_obj, 0, sizeof(entry->lce_obj));
7979 /* XXX: some kind of LRU */
7980 entry->lce_obj.lco_osd = osd_obj2dev(obj);
7981 entry->lce_obj.lco_ino = obj->oo_inode->i_ino;
7982 entry->lce_obj.lco_gen = obj->oo_inode->i_generation;
7983 entry->lce_obj.lco_version = iversion;
7985 LASSERT(ln->ln_namelen <= LDISKFS_NAME_LEN + 1);
7986 entry->lce_namelen = ln->ln_namelen;
7987 memcpy(entry->lce_name, ln->ln_name, ln->ln_namelen);
7988 memcpy(&entry->lce_fid, rec, sizeof(entry->lce_fid));
7989 entry->lce_rc = result;
7991 if (++olc->olc_cur == OSD_LOOKUP_CACHE_MAX)
7996 * Index lookup function for interoperability mode (b11826).
7998 * \param key, key i.e. file name to be searched
8000 * \retval +ve, on success
8001 * \retval -ve, on error
8003 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
8004 struct dt_rec *rec, const struct dt_key *key)
8006 struct osd_object *obj = osd_dt_obj(dt);
8013 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
8014 LINVRNT(osd_invariant(obj));
8016 rc = obj_name2lu_name(obj, (char *)key, strlen((char *)key), &ln);
8021 * grab version before actual lookup, so that we recognize potential
8022 * insert between osd_ea_lookup_rec() and osd_olc_save()
8024 iversion = inode_peek_iversion(obj->oo_inode);
8026 if (osd_olc_lookup(env, obj, iversion, rec, &ln, &result))
8027 GOTO(out, rc = result);
8029 rc = osd_ea_lookup_rec(env, obj, rec, &ln);
8033 osd_olc_save(env, obj, rec, &ln, rc, iversion);
8036 if (ln.ln_name != (char *)key)
8042 * Index and Iterator operations for interoperability
8043 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
8045 static const struct dt_index_operations osd_index_ea_ops = {
8046 .dio_lookup = osd_index_ea_lookup,
8047 .dio_declare_insert = osd_index_declare_ea_insert,
8048 .dio_insert = osd_index_ea_insert,
8049 .dio_declare_delete = osd_index_declare_ea_delete,
8050 .dio_delete = osd_index_ea_delete,
8052 .init = osd_it_ea_init,
8053 .fini = osd_it_ea_fini,
8054 .get = osd_it_ea_get,
8055 .put = osd_it_ea_put,
8056 .next = osd_it_ea_next,
8057 .key = osd_it_ea_key,
8058 .key_size = osd_it_ea_key_size,
8059 .rec = osd_it_ea_rec,
8060 .rec_size = osd_it_ea_rec_size,
8061 .store = osd_it_ea_store,
8062 .load = osd_it_ea_load
8066 static void *osd_key_init(const struct lu_context *ctx,
8067 struct lu_context_key *key)
8069 struct osd_thread_info *info;
8071 OBD_ALLOC_PTR(info);
8073 return ERR_PTR(-ENOMEM);
8075 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
8076 if (info->oti_it_ea_buf == NULL)
8079 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
8081 info->oti_hlock = ldiskfs_htree_lock_alloc();
8082 if (info->oti_hlock == NULL)
8088 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
8091 return ERR_PTR(-ENOMEM);
8094 static void osd_key_fini(const struct lu_context *ctx,
8095 struct lu_context_key *key, void *data)
8097 struct osd_thread_info *info = data;
8098 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
8099 struct osd_idmap_cache *idc = info->oti_ins_cache;
8101 if (info->oti_dio_pages) {
8103 for (i = 0; i < PTLRPC_MAX_BRW_PAGES; i++) {
8104 struct page *page = info->oti_dio_pages[i];
8106 LASSERT(PagePrivate2(page));
8107 LASSERT(PageLocked(page));
8108 ClearPagePrivate2(page);
8113 OBD_FREE_PTR_ARRAY_LARGE(info->oti_dio_pages,
8114 PTLRPC_MAX_BRW_PAGES);
8117 if (info->oti_inode != NULL)
8119 if (info->oti_hlock != NULL)
8120 ldiskfs_htree_lock_free(info->oti_hlock);
8121 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
8122 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
8123 lu_buf_free(&info->oti_iobuf.dr_lnb_buf);
8124 lu_buf_free(&info->oti_big_buf);
8126 LASSERT(info->oti_ins_cache_size > 0);
8127 OBD_FREE_PTR_ARRAY_LARGE(idc, info->oti_ins_cache_size);
8128 info->oti_ins_cache = NULL;
8129 info->oti_ins_cache_size = 0;
8131 if (info->oti_lookup_cache)
8132 OBD_FREE_PTR(info->oti_lookup_cache);
8136 static void osd_key_exit(const struct lu_context *ctx,
8137 struct lu_context_key *key, void *data)
8139 struct osd_thread_info *info = data;
8140 struct osd_lookup_cache *olc = info->oti_lookup_cache;
8143 memset(olc, 0, sizeof(*olc));
8144 LASSERT(info->oti_r_locks == 0);
8145 LASSERT(info->oti_w_locks == 0);
8146 LASSERT(info->oti_txns == 0);
8147 LASSERTF(info->oti_dio_pages_used == 0, "%d\n",
8148 info->oti_dio_pages_used);
8151 /* type constructor/destructor: osd_type_init, osd_type_fini */
8152 LU_TYPE_INIT_FINI(osd, &osd_key);
8154 struct lu_context_key osd_key = {
8155 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
8156 .lct_init = osd_key_init,
8157 .lct_fini = osd_key_fini,
8158 .lct_exit = osd_key_exit
8162 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
8163 const char *name, struct lu_device *next)
8165 struct osd_device *osd = osd_dev(d);
8167 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname)) >=
8168 sizeof(osd->od_svname))
8170 return osd_procfs_init(osd, name);
8173 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
8175 struct seq_server_site *ss = osd_seq_site(osd);
8180 if (osd->od_is_ost || osd->od_cl_seq != NULL)
8183 if (unlikely(ss == NULL))
8186 OBD_ALLOC_PTR(osd->od_cl_seq);
8187 if (osd->od_cl_seq == NULL)
8190 seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
8191 osd->od_svname, ss->ss_server_seq);
8193 if (ss->ss_node_id == 0) {
8195 * If the OSD on the sequence controller(MDT0), then allocate
8196 * sequence here, otherwise allocate sequence after connected
8197 * to MDT0 (see mdt_register_lwp_callback()).
8199 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
8200 &osd->od_cl_seq->lcs_space, env);
8206 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
8208 if (osd->od_cl_seq == NULL)
8211 seq_client_fini(osd->od_cl_seq);
8212 OBD_FREE_PTR(osd->od_cl_seq);
8213 osd->od_cl_seq = NULL;
8216 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
8220 /* shutdown quota slave instance associated with the device */
8221 if (o->od_quota_slave_md != NULL) {
8222 struct qsd_instance *qsd = o->od_quota_slave_md;
8224 o->od_quota_slave_md = NULL;
8228 if (o->od_quota_slave_dt != NULL) {
8229 struct qsd_instance *qsd = o->od_quota_slave_dt;
8231 o->od_quota_slave_dt = NULL;
8235 osd_fid_fini(env, o);
8236 osd_scrub_cleanup(env, o);
8241 #ifdef HAVE_FLUSH_DELAYED_FPUT
8242 # define cfs_flush_delayed_fput() flush_delayed_fput()
8244 void (*cfs_flush_delayed_fput)(void);
8245 #endif /* HAVE_FLUSH_DELAYED_FPUT */
8247 static void osd_umount(const struct lu_env *env, struct osd_device *o)
8251 atomic_inc(&osd_mount_seq);
8253 if (o->od_mnt != NULL) {
8254 shrink_dcache_sb(osd_sb(o));
8255 osd_sync(env, &o->od_dt_dev);
8256 wait_event(o->od_commit_cb_done,
8257 !atomic_read(&o->od_commit_cb_in_flight));
8263 /* to be sure all delayed fput are finished */
8264 cfs_flush_delayed_fput();
8269 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
8270 # ifndef LDISKFS_HAS_INCOMPAT_FEATURE
8271 /* Newer kernels provide the ldiskfs_set_feature_largedir() wrapper already,
8272 * which calls ldiskfs_update_dynamic_rev() to update ancient filesystems.
8273 * All ldiskfs filesystems are already v2, so it is a no-op and unnecessary.
8274 * This avoids maintaining patches to export this otherwise-useless function.
8276 void ldiskfs_update_dynamic_rev(struct super_block *sb)
8283 static int osd_mount(const struct lu_env *env,
8284 struct osd_device *o, struct lustre_cfg *cfg)
8286 const char *name = lustre_cfg_string(cfg, 0);
8287 const char *dev = lustre_cfg_string(cfg, 1);
8289 unsigned long page, s_flags = 0, lmd_flags = 0;
8290 struct page *__page;
8291 struct file_system_type *type;
8292 char *options = NULL;
8294 struct osd_thread_info *info = osd_oti_get(env);
8295 struct lu_fid *fid = &info->oti_fid;
8296 struct inode *inode;
8297 int rc = 0, force_over_1024tb = 0;
8301 if (o->od_mnt != NULL)
8304 if (strlen(dev) >= sizeof(o->od_mntdev))
8306 strcpy(o->od_mntdev, dev);
8308 str = lustre_cfg_buf(cfg, 2);
8309 sscanf(str, "%lu:%lu", &s_flags, &lmd_flags);
8311 opts = lustre_cfg_string(cfg, 3);
8313 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
8314 CERROR("%s: device %s extents feature is not guaranteed to "
8315 "work on big-endian systems. Use \"bigendian_extents\" "
8316 "mount option to override.\n", name, dev);
8320 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
8321 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
8322 CWARN("force_over_128tb option is deprecated. Filesystems smaller than 1024TB can be created without any force option. Use force_over_1024tb option for filesystems larger than 1024TB.\n");
8325 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
8326 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
8327 CWARN("force_over_256tb option is deprecated. Filesystems smaller than 1024TB can be created without any force options. Use force_over_1024tb option for filesystems larger than 1024TB.\n");
8330 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
8331 if (opts != NULL && strstr(opts, "force_over_512tb") != NULL) {
8332 CWARN("force_over_512tb option is deprecated. Filesystems smaller than 1024TB can be created without any force options. Use force_over_1024tb option for filesystems larger than 1024TB.\n");
8336 if (opts != NULL && strstr(opts, "force_over_1024tb") != NULL)
8337 force_over_1024tb = 1;
8339 __page = alloc_page(GFP_KERNEL);
8341 GOTO(out, rc = -ENOMEM);
8342 page = (unsigned long)page_address(__page);
8343 options = (char *)page;
8346 /* strip out the options for back compatiblity */
8347 static const char * const sout[] = {
8354 /* strip out option we processed in osd */
8355 "bigendian_extents",
8359 "force_over_1024tb",
8363 strncat(options, opts, PAGE_SIZE);
8364 for (rc = 0, str = options; sout[rc]; ) {
8365 char *op = strstr(str, sout[rc]);
8372 if (op == options || *(op - 1) == ',') {
8373 str = op + strlen(sout[rc]);
8374 if (*str == ',' || *str == '\0') {
8375 *str == ',' ? str++ : str;
8376 memmove(op, str, strlen(str) + 1);
8379 for (str = op; *str != ',' && *str != '\0'; str++)
8383 strncat(options, "user_xattr,acl", PAGE_SIZE);
8386 /* Glom up mount options */
8387 if (*options != '\0')
8388 strncat(options, ",", PAGE_SIZE);
8389 strncat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
8391 type = get_fs_type("ldiskfs");
8393 CERROR("%s: cannot find ldiskfs module\n", name);
8394 GOTO(out, rc = -ENODEV);
8397 s_flags |= SB_KERNMOUNT;
8398 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
8399 module_put(type->owner);
8401 if (IS_ERR(o->od_mnt)) {
8402 rc = PTR_ERR(o->od_mnt);
8404 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
8408 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
8409 osd_sb(o)->s_blocksize_bits > 1024ULL << 40 &&
8410 force_over_1024tb == 0) {
8411 CERROR("%s: device %s LDISKFS has not been tested on filesystems larger than 1024TB and may cause data corruption. Use 'force_over_1024tb' mount option to override.\n",
8413 GOTO(out_mnt, rc = -EINVAL);
8416 if (test_bit(LMD_FLG_DEV_RDONLY, &lmd_flags)) {
8417 LCONSOLE_WARN("%s: not support dev_rdonly on this device\n",
8420 GOTO(out_mnt, rc = -EOPNOTSUPP);
8423 if (!ldiskfs_has_feature_journal(o->od_mnt->mnt_sb)) {
8424 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
8425 GOTO(out_mnt, rc = -EINVAL);
8428 if (ldiskfs_has_feature_fast_commit(o->od_mnt->mnt_sb)) {
8429 CERROR("%s: device %s is mounted with fast_commit that breaks recovery\n",
8431 GOTO(out_mnt, rc = -EOPNOTSUPP);
8434 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
8435 #ifdef LDISKFS_MOUNT_DIRDATA
8436 if (ldiskfs_has_feature_dirdata(o->od_mnt->mnt_sb))
8437 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
8438 else if (strstr(name, "MDT")) /* don't complain for MGT or OSTs */
8439 CWARN("%s: device %s was upgraded from Lustre-1.x without "
8440 "enabling the dirdata feature. If you do not want to "
8441 "downgrade to Lustre-1.x again, you can enable it via "
8442 "'tune2fs -O dirdata device'\n", name, dev);
8444 /* enable large_dir on MDTs to avoid REMOTE_PARENT_DIR overflow,
8445 * and on very large OSTs to avoid object directory overflow */
8446 if (unlikely(!ldiskfs_has_feature_largedir(o->od_mnt->mnt_sb) &&
8447 !strstr(name, "MGS"))) {
8448 ldiskfs_set_feature_largedir(o->od_mnt->mnt_sb);
8449 LCONSOLE_INFO("%s: enabled 'large_dir' feature on device %s\n",
8453 inode = osd_sb(o)->s_root->d_inode;
8454 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
8455 if (!o->od_dt_dev.dd_rdonly) {
8456 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
8458 CERROR("%s: failed to set lma on %s root inode\n",
8464 if (test_bit(LMD_FLG_NOSCRUB, &lmd_flags))
8465 o->od_scrub.os_scrub.os_auto_scrub_interval = AS_NEVER;
8467 if (blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev))) {
8468 /* do not use pagecache with flash-backed storage */
8469 o->od_writethrough_cache = 0;
8470 o->od_read_cache = 0;
8481 __free_page(__page);
8486 static struct lu_device *osd_device_fini(const struct lu_env *env,
8487 struct lu_device *d)
8489 struct osd_device *o = osd_dev(d);
8493 osd_index_backup(env, o, false);
8494 osd_shutdown(env, o);
8496 if (o->od_oi_table != NULL)
8497 osd_oi_fini(osd_oti_get(env), o);
8498 if (o->od_extent_bytes_percpu)
8499 free_percpu(o->od_extent_bytes_percpu);
8500 osd_obj_map_fini(o);
8506 static int osd_device_init0(const struct lu_env *env,
8507 struct osd_device *o,
8508 struct lustre_cfg *cfg)
8510 struct lu_device *l = osd2lu_dev(o);
8511 struct osd_thread_info *info;
8514 bool restored = false;
8517 /* if the module was re-loaded, env can loose its keys */
8518 rc = lu_env_refill((struct lu_env *)env);
8521 info = osd_oti_get(env);
8524 l->ld_ops = &osd_lu_ops;
8525 o->od_dt_dev.dd_ops = &osd_dt_ops;
8527 spin_lock_init(&o->od_osfs_lock);
8528 mutex_init(&o->od_otable_mutex);
8529 INIT_LIST_HEAD(&o->od_orphan_list);
8530 INIT_LIST_HEAD(&o->od_index_backup_list);
8531 INIT_LIST_HEAD(&o->od_index_restore_list);
8532 spin_lock_init(&o->od_lock);
8533 o->od_index_backup_policy = LIBP_NONE;
8535 init_waitqueue_head(&o->od_commit_cb_done);
8537 o->od_read_cache = 1;
8538 o->od_writethrough_cache = 1;
8539 o->od_enable_projid_xattr = 0;
8540 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
8541 o->od_readcache_max_iosize = OSD_READCACHE_MAX_IO_MB << 20;
8542 o->od_writethrough_max_iosize = OSD_WRITECACHE_MAX_IO_MB << 20;
8543 o->od_scrub.os_scrub.os_auto_scrub_interval = AS_DEFAULT;
8544 /* default fallocate to unwritten extents: LU-14326/LU-14333 */
8545 o->od_fallocate_zero_blocks = 0;
8547 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
8548 sizeof(o->od_svname));
8549 if (cplen >= sizeof(o->od_svname)) {
8554 o->od_index_backup_stop = 0;
8555 o->od_index = -1; /* -1 means index is invalid */
8556 rc = server_name2index(o->od_svname, &o->od_index, NULL);
8557 if (rc == LDD_F_SV_TYPE_OST)
8560 o->od_full_scrub_ratio = OFSR_DEFAULT;
8561 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
8562 rc = osd_mount(env, o, cfg);
8566 /* Can only check block device after mount */
8567 o->od_nonrotational =
8568 blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev));
8570 rc = osd_obj_map_init(env, o);
8574 rc = lu_site_init(&o->od_site, l);
8576 GOTO(out_compat, rc);
8577 o->od_site.ls_bottom_dev = l;
8579 rc = lu_site_init_finish(&o->od_site);
8583 opts = lustre_cfg_string(cfg, 3);
8584 if (opts && strstr(opts, "resetoi"))
8587 INIT_LIST_HEAD(&o->od_ios_list);
8589 rc = lprocfs_init_brw_stats(&o->od_brw_stats);
8591 GOTO(out_brw_stats, rc);
8593 /* setup scrub, including OI files initialization */
8595 rc = osd_scrub_setup(env, o, restored);
8598 GOTO(out_brw_stats, rc);
8600 rc = osd_procfs_init(o, o->od_svname);
8602 CERROR("%s: can't initialize procfs: rc = %d\n",
8604 GOTO(out_scrub, rc);
8607 LASSERT(l->ld_site->ls_linkage.next != NULL);
8608 LASSERT(l->ld_site->ls_linkage.prev != NULL);
8610 /* initialize quota slave instance */
8611 /* currently it's no need to prepare qsd_instance_md for OST */
8612 if (!o->od_is_ost) {
8613 o->od_quota_slave_md = qsd_init(env, o->od_svname,
8614 &o->od_dt_dev, o->od_proc_entry,
8616 if (IS_ERR(o->od_quota_slave_md)) {
8617 rc = PTR_ERR(o->od_quota_slave_md);
8618 o->od_quota_slave_md = NULL;
8619 GOTO(out_procfs, rc);
8623 o->od_quota_slave_dt = qsd_init(env, o->od_svname, &o->od_dt_dev,
8624 o->od_proc_entry, false, true);
8626 if (IS_ERR(o->od_quota_slave_dt)) {
8627 if (o->od_quota_slave_md != NULL) {
8628 qsd_fini(env, o->od_quota_slave_md);
8629 o->od_quota_slave_md = NULL;
8632 rc = PTR_ERR(o->od_quota_slave_dt);
8633 o->od_quota_slave_dt = NULL;
8634 GOTO(out_procfs, rc);
8637 o->od_extent_bytes_percpu = alloc_percpu(unsigned int);
8638 if (!o->od_extent_bytes_percpu) {
8640 GOTO(out_procfs, rc);
8648 osd_scrub_cleanup(env, o);
8650 lprocfs_fini_brw_stats(&o->od_brw_stats);
8652 lu_site_fini(&o->od_site);
8654 osd_obj_map_fini(o);
8661 static struct lu_device *osd_device_alloc(const struct lu_env *env,
8662 struct lu_device_type *t,
8663 struct lustre_cfg *cfg)
8665 struct osd_device *o;
8670 return ERR_PTR(-ENOMEM);
8672 rc = dt_device_init(&o->od_dt_dev, t);
8675 * Because the ctx might be revived in dt_device_init,
8676 * refill the env here
8678 lu_env_refill((struct lu_env *)env);
8679 rc = osd_device_init0(env, o, cfg);
8681 dt_device_fini(&o->od_dt_dev);
8684 if (unlikely(rc != 0))
8687 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
8690 static struct lu_device *osd_device_free(const struct lu_env *env,
8691 struct lu_device *d)
8693 struct osd_device *o = osd_dev(d);
8697 /* XXX: make osd top device in order to release reference */
8698 d->ld_site->ls_top_dev = d;
8699 lu_site_purge(env, d->ld_site, -1);
8700 lu_site_print(env, d->ld_site, &d->ld_site->ls_obj_hash.nelems,
8701 D_ERROR, lu_cdebug_printer);
8702 lu_site_fini(&o->od_site);
8703 dt_device_fini(&o->od_dt_dev);
8708 static int osd_process_config(const struct lu_env *env,
8709 struct lu_device *d, struct lustre_cfg *cfg)
8711 struct osd_device *o = osd_dev(d);
8717 switch (cfg->lcfg_command) {
8719 rc = osd_mount(env, o, cfg);
8723 * For the case LCFG_PRE_CLEANUP is not called in advance,
8724 * that may happend if hit failure during mount process.
8726 osd_index_backup(env, o, false);
8727 lu_dev_del_linkage(d->ld_site, d);
8728 rc = osd_shutdown(env, o);
8731 LASSERT(&o->od_dt_dev);
8732 count = class_modify_config(cfg, PARAM_OSD,
8733 &o->od_dt_dev.dd_kobj);
8735 count = class_modify_config(cfg, PARAM_OST,
8736 &o->od_dt_dev.dd_kobj);
8737 rc = count > 0 ? 0 : count;
8739 case LCFG_PRE_CLEANUP:
8741 osd_index_backup(env, o,
8742 o->od_index_backup_policy != LIBP_NONE);
8752 static int osd_recovery_complete(const struct lu_env *env,
8753 struct lu_device *d)
8755 struct osd_device *osd = osd_dev(d);
8760 if (osd->od_quota_slave_md == NULL && osd->od_quota_slave_dt == NULL)
8764 * start qsd instance on recovery completion, this notifies the quota
8765 * slave code that we are about to process new requests now
8767 rc = qsd_start(env, osd->od_quota_slave_dt);
8768 if (rc == 0 && osd->od_quota_slave_md != NULL)
8769 rc = qsd_start(env, osd->od_quota_slave_md);
8775 * we use exports to track all osd users
8777 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
8778 struct obd_device *obd, struct obd_uuid *cluuid,
8779 struct obd_connect_data *data, void *localdata)
8781 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8782 struct lustre_handle conn;
8787 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
8789 rc = class_connect(&conn, obd, cluuid);
8793 *exp = class_conn2export(&conn);
8795 spin_lock(&osd->od_osfs_lock);
8797 spin_unlock(&osd->od_osfs_lock);
8803 * once last export (we don't count self-export) disappeared
8804 * osd can be released
8806 static int osd_obd_disconnect(struct obd_export *exp)
8808 struct obd_device *obd = exp->exp_obd;
8809 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8810 int rc, release = 0;
8814 /* Only disconnect the underlying layers on the final disconnect. */
8815 spin_lock(&osd->od_osfs_lock);
8817 if (osd->od_connects == 0)
8819 spin_unlock(&osd->od_osfs_lock);
8821 rc = class_disconnect(exp); /* bz 9811 */
8823 if (rc == 0 && release)
8824 class_manual_cleanup(obd);
8828 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
8829 struct lu_device *dev)
8831 struct osd_device *osd = osd_dev(dev);
8832 struct lr_server_data *lsd =
8833 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
8838 if (osd->od_quota_slave_md != NULL) {
8839 /* set up quota slave objects for inode */
8840 result = qsd_prepare(env, osd->od_quota_slave_md);
8845 if (osd->od_quota_slave_dt != NULL) {
8846 /* set up quota slave objects for block */
8847 result = qsd_prepare(env, osd->od_quota_slave_dt);
8853 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
8854 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
8855 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
8856 osd->od_index_in_idif = 1;
8858 osd->od_index_in_idif = 0;
8859 result = osd_register_proc_index_in_idif(osd);
8864 osd->od_index_in_idif = 1;
8868 result = osd_fid_init(env, osd);
8874 * Implementation of lu_device_operations::ldo_fid_alloc() for OSD
8878 * see include/lu_object.h for the details.
8880 static int osd_fid_alloc(const struct lu_env *env, struct lu_device *d,
8881 struct lu_fid *fid, struct lu_object *parent,
8882 const struct lu_name *name)
8884 struct osd_device *osd = osd_dev(d);
8886 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
8889 static const struct lu_object_operations osd_lu_obj_ops = {
8890 .loo_object_init = osd_object_init,
8891 .loo_object_delete = osd_object_delete,
8892 .loo_object_release = osd_object_release,
8893 .loo_object_free = osd_object_free,
8894 .loo_object_print = osd_object_print,
8895 .loo_object_invariant = osd_object_invariant
8898 const struct lu_device_operations osd_lu_ops = {
8899 .ldo_object_alloc = osd_object_alloc,
8900 .ldo_process_config = osd_process_config,
8901 .ldo_recovery_complete = osd_recovery_complete,
8902 .ldo_prepare = osd_prepare,
8903 .ldo_fid_alloc = osd_fid_alloc,
8906 static const struct lu_device_type_operations osd_device_type_ops = {
8907 .ldto_init = osd_type_init,
8908 .ldto_fini = osd_type_fini,
8910 .ldto_start = osd_type_start,
8911 .ldto_stop = osd_type_stop,
8913 .ldto_device_alloc = osd_device_alloc,
8914 .ldto_device_free = osd_device_free,
8916 .ldto_device_init = osd_device_init,
8917 .ldto_device_fini = osd_device_fini
8920 static struct lu_device_type osd_device_type = {
8921 .ldt_tags = LU_DEVICE_DT,
8922 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
8923 .ldt_ops = &osd_device_type_ops,
8924 .ldt_ctx_tags = LCT_LOCAL,
8927 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
8929 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8930 struct super_block *sb = osd_sb(osd);
8932 return (osd->od_mnt == NULL || sb->s_flags & SB_RDONLY);
8936 * lprocfs legacy support.
8938 static const struct obd_ops osd_obd_device_ops = {
8939 .o_owner = THIS_MODULE,
8940 .o_connect = osd_obd_connect,
8941 .o_disconnect = osd_obd_disconnect,
8942 .o_health_check = osd_health_check,
8945 static ssize_t delayed_unlink_mb_show(struct kobject *kobj,
8946 struct attribute *attr, char *buf)
8948 return snprintf(buf, PAGE_SIZE, "%d\n",
8949 ldiskfs_delayed_unlink_blocks >> 11);
8952 static ssize_t delayed_unlink_mb_store(struct kobject *kobj,
8953 struct attribute *attr,
8954 const char *buffer, size_t count)
8956 u64 delayed_unlink_bytes;
8959 rc = sysfs_memparse(buffer, count, &delayed_unlink_bytes, "MiB");
8963 ldiskfs_delayed_unlink_blocks = delayed_unlink_bytes >> 9;
8967 LUSTRE_RW_ATTR(delayed_unlink_mb);
8970 static ssize_t track_declares_assert_show(struct kobject *kobj,
8971 struct attribute *attr,
8974 return sprintf(buf, "%d\n", ldiskfs_track_declares_assert);
8977 static ssize_t track_declares_assert_store(struct kobject *kobj,
8978 struct attribute *attr,
8979 const char *buffer, size_t count)
8981 bool track_declares_assert;
8984 rc = kstrtobool(buffer, &track_declares_assert);
8988 ldiskfs_track_declares_assert = track_declares_assert;
8992 LUSTRE_RW_ATTR(track_declares_assert);
8994 static int __init osd_init(void)
8996 struct kobject *kobj;
8999 BUILD_BUG_ON(BH_DXLock >=
9000 sizeof(((struct buffer_head *)0)->b_state) * 8);
9001 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
9002 /* please, try to keep osd_thread_info smaller than a page */
9003 BUILD_BUG_ON(sizeof(struct osd_thread_info) > PAGE_SIZE);
9006 rc = libcfs_setup();
9012 rc = lu_kmem_init(ldiskfs_caches);
9016 rc = class_register_type(&osd_obd_device_ops, NULL, true,
9017 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
9019 lu_kmem_fini(ldiskfs_caches);
9023 kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
9025 rc = sysfs_create_file(kobj,
9026 &lustre_attr_track_declares_assert.attr);
9028 CWARN("%s: track_declares_assert sysfs registration failed: rc = %d\n",
9033 rc = sysfs_create_file(kobj,
9034 &lustre_attr_delayed_unlink_mb.attr);
9036 CWARN("%s: delayed_unlink_mb registration failed: rc = %d\n",
9044 #ifndef HAVE_FLUSH_DELAYED_FPUT
9045 if (unlikely(cfs_flush_delayed_fput == NULL))
9046 cfs_flush_delayed_fput =
9047 cfs_kallsyms_lookup_name("flush_delayed_fput");
9053 static void __exit osd_exit(void)
9055 struct kobject *kobj;
9057 kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
9059 sysfs_remove_file(kobj,
9060 &lustre_attr_track_declares_assert.attr);
9063 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
9064 lu_kmem_fini(ldiskfs_caches);
9067 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
9068 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
9069 MODULE_VERSION(LUSTRE_VERSION_STRING);
9070 MODULE_LICENSE("GPL");
9072 module_init(osd_init);
9073 module_exit(osd_exit);