4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/osd/osd_handler.c
34 * Top-level entry points into osd module
36 * Author: Nikita Danilov <nikita@clusterfs.com>
37 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
40 #define DEBUG_SUBSYSTEM S_OSD
42 #include <linux/fs_struct.h>
43 #include <linux/kallsyms.h>
44 #include <linux/module.h>
45 #include <linux/user_namespace.h>
46 #include <linux/uidgid.h>
48 /* prerequisite for linux/xattr.h */
49 #include <linux/types.h>
50 /* prerequisite for linux/xattr.h */
52 /* XATTR_{REPLACE,CREATE} */
53 #include <linux/xattr.h>
55 #include <ldiskfs/ldiskfs.h>
56 #include <ldiskfs/xattr.h>
57 #include <ldiskfs/ldiskfs_extents.h>
60 * struct OBD_{ALLOC,FREE}*()
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
66 #include <lustre_fid.h>
68 #include <uapi/linux/lustre/lustre_param.h>
70 #include "osd_internal.h"
71 #include "osd_dynlocks.h"
73 /* llo_* api support */
74 #include <md_object.h>
75 #include <lustre_quota.h>
77 #include <lustre_linkea.h>
79 /* Maximum EA size is limited by LNET_MTU for remote objects */
80 #define OSD_MAX_EA_SIZE 1048364
83 module_param(ldiskfs_pdo, int, 0644);
84 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
86 int ldiskfs_track_declares_assert;
87 module_param(ldiskfs_track_declares_assert, int, 0644);
88 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
90 /* Slab to allocate dynlocks */
91 struct kmem_cache *dynlock_cachep;
93 /* Slab to allocate osd_it_ea */
94 struct kmem_cache *osd_itea_cachep;
96 static struct lu_kmem_descr ldiskfs_caches[] = {
98 .ckd_cache = &dynlock_cachep,
99 .ckd_name = "dynlock_cache",
100 .ckd_size = sizeof(struct dynlock_handle)
103 .ckd_cache = &osd_itea_cachep,
104 .ckd_name = "osd_itea_cache",
105 .ckd_size = sizeof(struct osd_it_ea)
112 static const char dot[] = ".";
113 static const char dotdot[] = "..";
115 static const struct lu_object_operations osd_lu_obj_ops;
116 static const struct dt_object_operations osd_obj_ops;
117 static const struct dt_object_operations osd_obj_otable_it_ops;
118 static const struct dt_index_operations osd_index_iam_ops;
119 static const struct dt_index_operations osd_index_ea_ops;
121 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
122 const struct lu_fid *fid);
123 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
124 struct osd_device *osd);
126 int osd_trans_declare_op2rb[] = {
127 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
128 [OSD_OT_PUNCH] = OSD_OT_MAX,
129 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
130 [OSD_OT_CREATE] = OSD_OT_DESTROY,
131 [OSD_OT_DESTROY] = OSD_OT_CREATE,
132 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
133 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
134 [OSD_OT_WRITE] = OSD_OT_WRITE,
135 [OSD_OT_INSERT] = OSD_OT_DELETE,
136 [OSD_OT_DELETE] = OSD_OT_INSERT,
137 [OSD_OT_QUOTA] = OSD_OT_MAX,
140 static int osd_has_index(const struct osd_object *obj)
142 return obj->oo_dt.do_index_ops != NULL;
145 static int osd_object_invariant(const struct lu_object *l)
147 return osd_invariant(osd_obj(l));
151 * Concurrency: doesn't matter
153 static int osd_is_write_locked(const struct lu_env *env, struct osd_object *o)
155 struct osd_thread_info *oti = osd_oti_get(env);
157 return oti->oti_w_locks > 0 && o->oo_owner == env;
161 * Concurrency: doesn't access mutable data
163 static int osd_root_get(const struct lu_env *env,
164 struct dt_device *dev, struct lu_fid *f)
166 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
171 * the following set of functions are used to maintain per-thread
172 * cache of FID->ino mapping. this mechanism is needed to resolve
173 * FID to inode at dt_insert() which in turn stores ino in the
174 * directory entries to keep ldiskfs compatible with ext[34].
175 * due to locking-originated restrictions we can't lookup ino
176 * using LU cache (deadlock is possible). lookup using OI is quite
177 * expensive. so instead we maintain this cache and methods like
178 * dt_create() fill it. so in the majority of cases dt_insert() is
179 * able to find needed mapping in lockless manner.
181 static struct osd_idmap_cache *
182 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
183 const struct lu_fid *fid)
185 struct osd_thread_info *oti = osd_oti_get(env);
186 struct osd_idmap_cache *idc = oti->oti_ins_cache;
189 for (i = 0; i < oti->oti_ins_cache_used; i++) {
190 if (!lu_fid_eq(&idc[i].oic_fid, fid))
192 if (idc[i].oic_dev != osd)
201 static struct osd_idmap_cache *
202 osd_idc_add(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;
209 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
210 i = oti->oti_ins_cache_size * 2;
212 i = OSD_INS_CACHE_SIZE;
213 OBD_ALLOC_PTR_ARRAY(idc, i);
215 return ERR_PTR(-ENOMEM);
216 if (oti->oti_ins_cache != NULL) {
217 memcpy(idc, oti->oti_ins_cache,
218 oti->oti_ins_cache_used * sizeof(*idc));
219 OBD_FREE_PTR_ARRAY(oti->oti_ins_cache,
220 oti->oti_ins_cache_used);
222 oti->oti_ins_cache = idc;
223 oti->oti_ins_cache_size = i;
226 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
229 idc->oic_lid.oii_ino = 0;
230 idc->oic_lid.oii_gen = 0;
237 * lookup mapping for the given fid in the cache, initialize a
238 * new one if not found. the initialization checks whether the
239 * object is local or remote. for local objects, OI is used to
240 * learn ino/generation. the function is used when the caller
241 * has no information about the object, e.g. at dt_insert().
243 static struct osd_idmap_cache *
244 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
245 const struct lu_fid *fid)
247 struct osd_idmap_cache *idc;
250 idc = osd_idc_find(env, osd, fid);
251 LASSERT(!IS_ERR(idc));
255 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
256 osd->od_svname, PFID(fid));
258 /* new mapping is needed */
259 idc = osd_idc_add(env, osd, fid);
261 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
262 osd->od_svname, PFID(fid), PTR_ERR(idc));
267 rc = osd_remote_fid(env, osd, fid);
268 if (unlikely(rc < 0))
272 /* the object is local, lookup in OI */
273 /* XXX: probably cheaper to lookup in LU first? */
274 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
276 if (unlikely(rc < 0)) {
277 CERROR("can't lookup: rc = %d\n", rc);
281 /* the object is remote */
288 static void osd_idc_dump_lma(const struct lu_env *env,
289 struct osd_device *osd,
293 struct osd_thread_info *info = osd_oti_get(env);
294 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
295 const struct lu_fid *fid;
296 struct osd_inode_id lid;
300 inode = osd_ldiskfs_iget(osd_sb(osd), ino);
302 CERROR("%s: can't get inode %lu: rc = %d\n",
303 osd->od_svname, ino, (int)PTR_ERR(inode));
306 if (is_bad_inode(inode)) {
307 CERROR("%s: bad inode %lu\n", osd->od_svname, ino);
310 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
312 CERROR("%s: can't get LMA for %lu: rc = %d\n",
313 osd->od_svname, ino, rc);
316 fid = &loa->loa_lma.lma_self_fid;
317 LCONSOLE(D_INFO, "%s: "DFID" in inode %lu/%u\n", osd->od_svname,
318 PFID(fid), ino, (unsigned)inode->i_generation);
321 rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
323 CERROR("%s: can't lookup "DFID": rc = %d\n",
324 osd->od_svname, PFID(fid), rc);
327 LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n", osd->od_svname,
328 PFID(fid), lid.oii_ino, lid.oii_gen);
333 static void osd_idc_dump_debug(const struct lu_env *env,
334 struct osd_device *osd,
335 const struct lu_fid *fid,
339 struct osd_inode_id lid;
343 rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
345 LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n",
346 osd->od_svname, PFID(fid), lid.oii_ino, lid.oii_gen);
347 osd_idc_dump_lma(env, osd, lid.oii_ino, false);
349 CERROR("%s: can't lookup "DFID": rc = %d\n",
350 osd->od_svname, PFID(fid), rc);
353 osd_idc_dump_lma(env, osd, ino1, true);
355 osd_idc_dump_lma(env, osd, ino2, true);
359 * lookup mapping for given FID and fill it from the given object.
360 * the object is lolcal by definition.
362 static int osd_idc_find_and_init(const struct lu_env *env,
363 struct osd_device *osd,
364 struct osd_object *obj)
366 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
367 struct osd_idmap_cache *idc;
369 idc = osd_idc_find(env, osd, fid);
370 LASSERT(!IS_ERR(idc));
372 if (obj->oo_inode == NULL)
374 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
375 if (idc->oic_lid.oii_ino) {
376 osd_idc_dump_debug(env, osd, fid,
377 idc->oic_lid.oii_ino,
378 obj->oo_inode->i_ino);
381 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
382 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
387 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
388 osd->od_svname, PFID(fid));
390 /* new mapping is needed */
391 idc = osd_idc_add(env, osd, fid);
393 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
394 osd->od_svname, PFID(fid), PTR_ERR(idc));
398 if (obj->oo_inode != NULL) {
399 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
400 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
406 * OSD object methods.
410 * Concurrency: no concurrent access is possible that early in object
413 static struct lu_object *osd_object_alloc(const struct lu_env *env,
414 const struct lu_object_header *hdr,
417 struct osd_object *mo;
422 struct lu_object_header *h;
423 struct osd_device *o = osd_dev(d);
425 l = &mo->oo_dt.do_lu;
426 if (unlikely(o->od_in_init)) {
433 lu_object_header_init(h);
434 lu_object_init(l, h, d);
435 lu_object_add_top(h, l);
438 dt_object_init(&mo->oo_dt, NULL, d);
439 mo->oo_header = NULL;
442 mo->oo_dt.do_ops = &osd_obj_ops;
443 l->lo_ops = &osd_lu_obj_ops;
444 init_rwsem(&mo->oo_sem);
445 init_rwsem(&mo->oo_ext_idx_sem);
446 spin_lock_init(&mo->oo_guard);
447 INIT_LIST_HEAD(&mo->oo_xattr_list);
453 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
454 struct dentry *dentry, struct lustre_ost_attrs *loa)
458 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
459 (void *)loa, sizeof(*loa));
461 struct lustre_mdt_attrs *lma = &loa->loa_lma;
463 if (rc < sizeof(*lma))
467 lustre_loa_swab(loa, true);
468 /* Check LMA compatibility */
469 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
471 CWARN("%s: unsupported incompat LMA feature(s) %#x for fid = "DFID", ino = %lu: rc = %d\n",
473 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
474 PFID(&lma->lma_self_fid), inode->i_ino, rc);
476 } else if (rc == 0) {
484 * retrieve object from backend ext fs.
486 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
487 struct osd_inode_id *id)
490 struct inode *inode = NULL;
493 * if we look for an inode withing a running
494 * transaction, then we risk to deadlock
495 * osd_dirent_check_repair() breaks this
497 /* LASSERT(current->journal_info == NULL); */
499 inode = osd_ldiskfs_iget(osd_sb(dev), id->oii_ino);
501 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
502 id->oii_ino, PTR_ERR(inode));
503 } else if (id->oii_gen != OSD_OII_NOGEN &&
504 inode->i_generation != id->oii_gen) {
505 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
506 "i_generation = %u\n",
507 id->oii_ino, id->oii_gen, inode->i_generation);
509 inode = ERR_PTR(-ESTALE);
510 } else if (inode->i_nlink == 0) {
512 * due to parallel readdir and unlink,
513 * we can have dead inode here.
515 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
517 inode = ERR_PTR(-ESTALE);
518 } else if (is_bad_inode(inode)) {
520 CWARN("%s: bad inode: ino = %u: rc = %d\n",
521 osd_dev2name(dev), id->oii_ino, rc);
524 } else if ((rc = osd_attach_jinode(inode))) {
528 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
529 if (id->oii_gen == OSD_OII_NOGEN)
530 osd_id_gen(id, inode->i_ino, inode->i_generation);
533 * Do not update file c/mtime in ldiskfs.
534 * NB: we don't have any lock to protect this because we don't
535 * have reference on osd_object now, but contention with
536 * another lookup + attr_set can't happen in the tiny window
537 * between if (...) and set S_NOCMTIME.
539 if (!(inode->i_flags & S_NOCMTIME))
540 inode->i_flags |= S_NOCMTIME;
545 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
546 handle_t *handle, struct dentry *child,
547 struct inode *inode, struct htree_lock *hlock)
551 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
552 if (rc == -ENOBUFS || rc == -ENOSPC) {
553 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
554 struct inode *parent = child->d_parent->d_inode;
555 struct lu_fid *fid = NULL;
556 char fidstr[FID_LEN + 1] = "unknown";
558 rc2 = osd_get_lma(info, parent, child->d_parent, loa);
560 fid = &loa->loa_lma.lma_self_fid;
561 } else if (rc2 == -ENODATA) {
562 if (unlikely(parent == inode->i_sb->s_root->d_inode)) {
563 fid = &info->oti_fid3;
564 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
565 } else if (!osd->od_is_ost && osd->od_index == 0) {
566 fid = &info->oti_fid3;
567 lu_igif_build(fid, parent->i_ino,
568 parent->i_generation);
573 snprintf(fidstr, sizeof(fidstr), DFID, PFID(fid));
575 /* below message is checked in sanity.sh test_129 */
577 CWARN("%s: directory (inode: %lu, FID: %s) has reached max size limit\n",
578 osd_name(osd), parent->i_ino, fidstr);
580 rc = 0; /* ignore such error now */
581 CWARN("%s: directory (inode: %lu, FID: %s) is approaching max size limit\n",
582 osd_name(osd), parent->i_ino, fidstr);
592 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
593 struct osd_inode_id *id, struct lu_fid *fid)
595 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
599 inode = osd_iget(info, dev, id);
603 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
605 *fid = loa->loa_lma.lma_self_fid;
606 } else if (rc == -ENODATA) {
607 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
608 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
610 lu_igif_build(fid, inode->i_ino, inode->i_generation);
618 static struct inode *osd_iget_check(struct osd_thread_info *info,
619 struct osd_device *dev,
620 const struct lu_fid *fid,
621 struct osd_inode_id *id,
630 * The cached OI mapping is trustable. If we cannot locate the inode
631 * via the cached OI mapping, then return the failure to the caller
632 * directly without further OI checking.
636 inode = osd_ldiskfs_iget(osd_sb(dev), id->oii_ino);
639 if (!trusted && (rc == -ENOENT || rc == -ESTALE))
642 CDEBUG(D_INODE, "no inode for FID: "DFID", ino = %u, rc = %d\n",
643 PFID(fid), id->oii_ino, rc);
647 if (is_bad_inode(inode)) {
652 CDEBUG(D_INODE, "bad inode for FID: "DFID", ino = %u\n",
653 PFID(fid), id->oii_ino);
657 if (id->oii_gen != OSD_OII_NOGEN &&
658 inode->i_generation != id->oii_gen) {
663 CDEBUG(D_INODE, "unmatched inode for FID: "DFID", ino = %u, "
664 "oii_gen = %u, i_generation = %u\n", PFID(fid),
665 id->oii_ino, id->oii_gen, inode->i_generation);
669 if (inode->i_nlink == 0) {
674 CDEBUG(D_INODE, "stale inode for FID: "DFID", ino = %u\n",
675 PFID(fid), id->oii_ino);
679 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
683 __u32 saved_ino = id->oii_ino;
684 __u32 saved_gen = id->oii_gen;
687 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
689 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
691 * XXX: There are four possible cases:
693 * Backup/restore caused the OI invalid.
695 * Someone unlinked the object but NOT removed
696 * the OI mapping, such as mount target device
697 * as ldiskfs, and modify something directly.
699 * Someone just removed the object between the
700 * former oi_lookup and the iget. It is normal.
701 * 4. Other failure cases.
703 * Generally, when the device is mounted, it will
704 * auto check whether the system is restored from
705 * file-level backup or not. We trust such detect
706 * to distinguish the 1st case from the 2nd case:
707 * if the OI files are consistent but may contain
708 * stale OI mappings because of case 2, if iget()
709 * returns -ENOENT or -ESTALE, then it should be
714 * If the OI mapping was in OI file before the
715 * osd_iget_check(), but now, it is disappear,
716 * then it must be removed by race. That is a
722 * It is the OI scrub updated the OI mapping by race.
723 * The new OI mapping must be valid.
725 if (saved_ino != id->oii_ino ||
726 (saved_gen != id->oii_gen && saved_gen != OSD_OII_NOGEN)) {
735 if (dev->od_scrub.os_scrub.os_file.sf_flags &
738 * It still can be the case 2, but we cannot
739 * distinguish it from the case 1. So return
740 * -EREMCHG to block current operation until
741 * OI scrub rebuilt the OI mappings.
750 if (inode->i_generation == id->oii_gen)
755 if (id->oii_gen == OSD_OII_NOGEN)
756 osd_id_gen(id, inode->i_ino, inode->i_generation);
759 * Do not update file c/mtime in ldiskfs.
760 * NB: we don't have any lock to protect this because we don't
761 * have reference on osd_object now, but contention with
762 * another lookup + attr_set can't happen in the tiny window
763 * between if (...) and set S_NOCMTIME.
765 if (!(inode->i_flags & S_NOCMTIME))
766 inode->i_flags |= S_NOCMTIME;
783 * \retval +v: new filter_fid does not contain self-fid
784 * \retval 0: filter_fid_18_23, contains self-fid
785 * \retval -v: other failure cases
787 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
788 struct dentry *dentry, struct lu_fid *fid)
790 struct filter_fid *ff = &info->oti_ff;
791 struct ost_id *ostid = &info->oti_ostid;
794 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
795 if (rc == sizeof(struct filter_fid_18_23)) {
796 struct filter_fid_18_23 *ff_old = (void *)ff;
798 ostid_set_seq(ostid, le64_to_cpu(ff_old->ff_seq));
799 rc = ostid_set_id(ostid, le64_to_cpu(ff_old->ff_objid));
801 * XXX: use 0 as the index for compatibility, the caller will
802 * handle index related issues when necessary.
805 ostid_to_fid(fid, ostid, 0);
806 } else if (rc >= (int)sizeof(struct filter_fid_24_29)) {
808 } else if (rc >= 0) {
815 static int osd_lma_self_repair(struct osd_thread_info *info,
816 struct osd_device *osd, struct inode *inode,
817 const struct lu_fid *fid, __u32 compat)
822 LASSERT(current->journal_info == NULL);
824 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
825 osd_dto_credits_noquota[DTO_XATTR_SET]);
828 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
833 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
835 CWARN("%s: cannot self repair the LMA: rc = %d\n",
837 ldiskfs_journal_stop(jh);
841 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
843 struct osd_thread_info *info = osd_oti_get(env);
844 struct osd_device *osd = osd_obj2dev(obj);
845 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
846 struct lustre_mdt_attrs *lma = &loa->loa_lma;
847 struct inode *inode = obj->oo_inode;
848 struct dentry *dentry = &info->oti_obj_dentry;
849 struct lu_fid *fid = NULL;
850 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
855 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
856 (void *)loa, sizeof(*loa));
857 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
858 fid = &lma->lma_self_fid;
859 rc = osd_get_idif(info, inode, dentry, fid);
860 if (rc > 0 || (rc == -ENODATA && osd->od_index_in_idif)) {
862 * For the given OST-object, if it has neither LMA nor
863 * FID in XATTR_NAME_FID, then the given FID (which is
864 * contained in the @obj, from client RPC for locating
865 * the OST-object) is trusted. We use it to generate
868 osd_lma_self_repair(info, osd, inode, rfid,
879 lustre_lma_swab(lma);
880 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
881 (CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT) &&
882 S_ISREG(inode->i_mode)))) {
883 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
884 "fid = "DFID", ino = %lu\n", osd_name(osd),
885 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
886 PFID(rfid), inode->i_ino);
889 fid = &lma->lma_self_fid;
890 if (lma->lma_compat & LMAC_STRIPE_INFO &&
892 obj->oo_pfid_in_lma = 1;
893 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
895 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
899 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
900 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
901 struct ost_id *oi = &info->oti_ostid;
902 struct lu_fid *fid1 = &info->oti_fid3;
903 __u32 idx = fid_idif_ost_idx(rfid);
906 * For old IDIF, the OST index is not part of the IDIF,
907 * Means that different OSTs may have the same IDIFs.
908 * Under such case, we need to make some compatible
909 * check to make sure to trigger OI scrub properly.
911 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
912 /* Given @rfid is new, LMA is old. */
913 fid_to_ostid(fid, oi);
914 ostid_to_fid(fid1, oi, idx);
915 if (lu_fid_eq(fid1, rfid)) {
916 if (osd->od_index_in_idif)
917 osd_lma_self_repair(info, osd,
931 struct osd_check_lmv_buf {
932 /* please keep it as first member */
933 struct dir_context ctx;
934 struct osd_thread_info *oclb_info;
935 struct osd_device *oclb_dev;
936 struct osd_idmap_cache *oclb_oic;
942 * It is called internally by ->iterate*() to filter out the
943 * local slave object's FID of the striped directory.
945 * \retval 1 found the local slave's FID
946 * \retval 0 continue to check next item
947 * \retval -ve for failure
949 #ifdef HAVE_FILLDIR_USE_CTX
950 static int osd_stripe_dir_filldir(struct dir_context *buf,
952 static int osd_stripe_dir_filldir(void *buf,
954 const char *name, int namelen,
955 loff_t offset, __u64 ino, unsigned int d_type)
957 struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
958 struct osd_thread_info *oti = oclb->oclb_info;
959 struct lu_fid *fid = &oti->oti_fid3;
960 struct osd_inode_id *id = &oti->oti_id3;
961 struct osd_device *dev = oclb->oclb_dev;
962 struct osd_idmap_cache *oic = oclb->oclb_oic;
971 sscanf(name + 1, SFID, RFID(fid));
972 if (!fid_is_sane(fid))
975 if (osd_remote_fid(oti->oti_env, dev, fid))
978 osd_id_gen(id, ino, OSD_OII_NOGEN);
979 inode = osd_iget(oti, dev, id);
981 return PTR_ERR(inode);
984 osd_add_oi_cache(oti, dev, id, fid);
988 osd_oii_insert(dev, oic, true);
989 oclb->oclb_found = true;
995 * When lookup item under striped directory, we need to locate the master
996 * MDT-object of the striped directory firstly, then the client will send
997 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
998 * and the item's name. If the system is restored from MDT file level backup,
999 * then before the OI scrub completely built the OI files, the OI mappings of
1000 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
1001 * not a problem for the master MDT-object. Because when locate the master
1002 * MDT-object, we will do name based lookup (for the striped directory itself)
1003 * firstly, during such process we can setup the correct OI mapping for the
1004 * master MDT-object. But it will be trouble for the slave MDT-object. Because
1005 * the client will not trigger name based lookup on the MDT to locate the slave
1006 * MDT-object before locating item under the striped directory, then when
1007 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
1008 * is invalid and does not know what the right OI mapping is, then the MDT has
1009 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
1010 * the OI file, related OI mapping is unknown yet, please try again later. And
1011 * then client will re-try the RPC again and again until related OI mapping has
1012 * been updated. That is quite inefficient.
1014 * To resolve above trouble, we will handle it as the following two cases:
1016 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
1017 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
1018 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
1019 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
1020 * directly. Please check osd_fid_lookup().
1022 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
1023 * Under such case, during lookup the master MDT-object, we will lookup the
1024 * slave MDT-object via readdir against the master MDT-object, because the
1025 * slave MDT-objects information are stored as sub-directories with the name
1026 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
1027 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
1028 * the correct OI mapping for the slave MDT-object.
1030 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
1031 struct inode *inode, struct osd_idmap_cache *oic)
1033 struct lu_buf *buf = &oti->oti_big_buf;
1034 struct dentry *dentry = &oti->oti_obj_dentry;
1036 struct lmv_mds_md_v1 *lmv1;
1037 struct osd_check_lmv_buf oclb = {
1038 .ctx.actor = osd_stripe_dir_filldir,
1042 .oclb_found = false,
1049 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, buf->lb_buf,
1051 if (rc == -ERANGE) {
1052 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, NULL, 0);
1054 lu_buf_realloc(buf, rc);
1055 if (buf->lb_buf == NULL)
1056 GOTO(out, rc = -ENOMEM);
1062 if (unlikely(rc == 0 || rc == -ENODATA))
1068 if (unlikely(buf->lb_buf == NULL)) {
1069 lu_buf_realloc(buf, rc);
1070 if (buf->lb_buf == NULL)
1071 GOTO(out, rc = -ENOMEM);
1077 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1080 filp = osd_quasi_file(oti->oti_env, inode);
1081 rc = osd_security_file_alloc(filp);
1086 oclb.oclb_items = 0;
1087 rc = iterate_dir(filp, &oclb.ctx);
1088 } while (rc >= 0 && oclb.oclb_items > 0 && !oclb.oclb_found &&
1089 filp->f_pos != LDISKFS_HTREE_EOF_64BIT);
1090 inode->i_fop->release(inode, filp);
1095 "%s: cannot check LMV, ino = %lu/%u "DFID": rc = %d\n",
1096 osd_ino2name(inode), inode->i_ino, inode->i_generation,
1097 PFID(&oic->oic_fid), rc);
1104 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
1105 const struct lu_fid *fid,
1106 const struct lu_object_conf *conf)
1108 struct osd_thread_info *info;
1109 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1110 struct osd_device *dev;
1111 struct osd_idmap_cache *oic;
1112 struct osd_inode_id *id;
1113 struct inode *inode = NULL;
1114 struct lustre_scrub *scrub;
1115 struct scrub_file *sf;
1116 __u32 flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT | SS_AUTO_FULL;
1121 bool remote = false;
1122 bool trusted = true;
1123 bool updated = false;
1124 bool checked = false;
1128 LINVRNT(osd_invariant(obj));
1129 LASSERT(obj->oo_inode == NULL);
1130 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
1132 dev = osd_dev(ldev);
1133 scrub = &dev->od_scrub.os_scrub;
1134 sf = &scrub->os_file;
1135 info = osd_oti_get(env);
1137 oic = &info->oti_cache;
1139 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
1143 * For the object is created as locking anchor, or for the object to
1144 * be created on disk. No need to osd_oi_lookup() at here because FID
1145 * shouldn't never be re-used, if it's really a duplicate FID from
1146 * unexpected reason, we should be able to detect it later by calling
1147 * do_create->osd_oi_insert().
1149 if (conf && conf->loc_flags & LOC_F_NEW)
1150 GOTO(out, result = 0);
1152 /* Search order: 1. per-thread cache. */
1153 if (lu_fid_eq(fid, &oic->oic_fid) && likely(oic->oic_dev == dev)) {
1159 if (!list_empty(&scrub->os_inconsistent_items)) {
1160 /* Search order: 2. OI scrub pending list. */
1161 result = osd_oii_lookup(dev, fid, id);
1167 * The OI mapping in the OI file can be updated by the OI scrub
1168 * when we locate the inode via FID. So it may be not trustable.
1172 /* Search order: 3. OI files. */
1173 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1174 if (result == -ENOENT) {
1175 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
1176 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1177 !ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
1179 GOTO(out, result = 0);
1184 /* -ESTALE is returned if inode of OST object doesn't exist */
1185 if (result == -ESTALE &&
1186 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1187 GOTO(out, result = 0);
1194 obj->oo_inode = NULL;
1195 /* for later passes through checks, not true on first pass */
1196 if (!IS_ERR_OR_NULL(inode))
1199 inode = osd_iget_check(info, dev, fid, id, trusted);
1200 if (!IS_ERR(inode)) {
1201 obj->oo_inode = inode;
1209 result = PTR_ERR(inode);
1210 if (result == -ENOENT || result == -ESTALE)
1211 GOTO(out, result = 0);
1213 if (result != -EREMCHG)
1218 * We still have chance to get the valid inode: for the
1219 * object which is referenced by remote name entry, the
1220 * object on the local MDT will be linked under the dir
1221 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1223 * We do not know whether the object for the given FID
1224 * is referenced by some remote name entry or not, and
1225 * especially for DNE II, a multiple-linked object may
1226 * have many name entries reside on many MDTs.
1228 * To simplify the operation, OSD will not distinguish
1229 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1230 * only happened for the RPC from other MDT during the
1231 * OI scrub, or for the client side RPC with FID only,
1232 * such as FID to path, or from old connected client.
1235 rc1 = osd_lookup_in_remote_parent(info, dev, fid, id);
1239 flags |= SS_AUTO_PARTIAL;
1240 flags &= ~SS_AUTO_FULL;
1245 if (scrub->os_running) {
1246 if (scrub->os_partial_scan && !scrub->os_in_join)
1249 osd_add_oi_cache(info, dev, id, fid);
1250 if (IS_ERR_OR_NULL(inode) || result) {
1251 osd_oii_insert(dev, oic, result == -ENOENT);
1252 GOTO(out, result = -EINPROGRESS);
1256 LASSERT(obj->oo_inode == inode);
1258 osd_oii_insert(dev, oic, true);
1262 if (dev->od_auto_scrub_interval == AS_NEVER) {
1264 GOTO(out, result = -EREMCHG);
1267 LASSERT(obj->oo_inode == inode);
1269 osd_add_oi_cache(info, dev, id, fid);
1274 rc1 = osd_scrub_start(env, dev, flags);
1275 LCONSOLE_WARN("%s: trigger OI scrub by RPC for the " DFID" with flags "
1276 "0x%x, rc = %d\n", osd_name(dev), PFID(fid), flags, rc1);
1277 if (rc1 && rc1 != -EALREADY)
1278 GOTO(out, result = -EREMCHG);
1280 osd_add_oi_cache(info, dev, id, fid);
1281 if (IS_ERR_OR_NULL(inode) || result) {
1282 osd_oii_insert(dev, oic, result == -ENOENT);
1283 GOTO(out, result = -EINPROGRESS);
1287 LASSERT(obj->oo_inode == inode);
1289 osd_oii_insert(dev, oic, true);
1294 if (unlikely(obj->oo_header))
1297 result = osd_check_lma(env, obj);
1301 LASSERTF(id->oii_ino == inode->i_ino &&
1302 id->oii_gen == inode->i_generation,
1303 "locate wrong inode for FID: "DFID", %u/%u => %ld/%u\n",
1304 PFID(fid), id->oii_ino, id->oii_gen,
1305 inode->i_ino, inode->i_generation);
1307 saved_ino = inode->i_ino;
1308 saved_gen = inode->i_generation;
1310 if (unlikely(result == -ENODATA)) {
1312 * If the OI scrub updated the OI mapping by race, it
1313 * must be valid. Trust the inode that has no LMA EA.
1318 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1321 * The OI mapping is still there, the inode is still
1322 * valid. It is just becaues the inode has no LMA EA.
1324 if (saved_ino == id->oii_ino &&
1325 saved_gen == id->oii_gen)
1329 * It is the OI scrub updated the OI mapping by race.
1330 * The new OI mapping must be valid.
1338 * "result == -ENOENT" means that the OI mappinghas been
1339 * removed by race, so the inode belongs to other object.
1341 * Others error can be returned directly.
1343 if (result == -ENOENT) {
1346 obj->oo_inode = NULL;
1351 if (result != -EREMCHG)
1357 * if two OST objects map to the same inode, and inode mode is
1358 * (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666), which means it's
1359 * reserved by precreate, and not written yet, in this case, don't
1360 * set inode for the object whose FID mismatch, so that it can create
1361 * inode and not block precreate.
1363 if (fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) &&
1364 inode->i_mode == (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666)) {
1365 obj->oo_inode = NULL;
1366 GOTO(out, result = 0);
1369 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1371 * "result == -ENOENT" means the cached OI mapping has been removed
1372 * from the OI file by race, above inode belongs to other object.
1374 if (result == -ENOENT) {
1377 obj->oo_inode = NULL;
1378 GOTO(out, result = 0);
1384 if (saved_ino == id->oii_ino && saved_gen == id->oii_gen) {
1390 * It is the OI scrub updated the OI mapping by race.
1391 * The new OI mapping must be valid.
1399 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
1400 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
1402 result = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
1404 if (lma->lma_compat & LMAC_STRIPE_INFO &&
1406 obj->oo_pfid_in_lma = 1;
1407 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
1409 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
1410 } else if (result != -ENODATA) {
1415 obj->oo_compat_dot_created = 1;
1416 obj->oo_compat_dotdot_created = 1;
1418 if (S_ISDIR(inode->i_mode) &&
1419 (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1420 osd_check_lmv(info, dev, inode, oic);
1422 result = osd_attach_jinode(inode);
1427 GOTO(out, result = 0);
1429 LASSERT(!obj->oo_hl_head);
1430 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1432 GOTO(out, result = (!obj->oo_hl_head ? -ENOMEM : 0));
1435 if (result || !obj->oo_inode) {
1436 if (!IS_ERR_OR_NULL(inode))
1439 obj->oo_inode = NULL;
1441 fid_zero(&oic->oic_fid);
1444 LINVRNT(osd_invariant(obj));
1449 * Concurrency: shouldn't matter.
1451 static void osd_object_init0(struct osd_object *obj)
1453 LASSERT(obj->oo_inode != NULL);
1454 obj->oo_dt.do_body_ops = &osd_body_ops;
1455 obj->oo_dt.do_lu.lo_header->loh_attr |=
1456 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1460 * Concurrency: no concurrent access is possible that early in object
1463 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1464 const struct lu_object_conf *conf)
1466 struct osd_object *obj = osd_obj(l);
1469 LINVRNT(osd_invariant(obj));
1471 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
1472 cfs_fail_val == 2) {
1473 struct osd_thread_info *info = osd_oti_get(env);
1474 struct osd_idmap_cache *oic = &info->oti_cache;
1475 /* invalidate thread cache */
1476 memset(&oic->oic_fid, 0, sizeof(oic->oic_fid));
1478 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1479 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1480 l->lo_header->loh_attr |= LOHA_EXISTS;
1484 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1485 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1486 if (result == 0 && obj->oo_inode != NULL) {
1487 struct osd_thread_info *oti = osd_oti_get(env);
1488 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
1490 osd_object_init0(obj);
1491 if (unlikely(obj->oo_header))
1494 result = osd_get_lma(oti, obj->oo_inode,
1495 &oti->oti_obj_dentry, loa);
1498 * Convert LMAI flags to lustre LMA flags
1499 * and cache it to oo_lma_flags
1502 lma_to_lustre_flags(loa->loa_lma.lma_incompat);
1503 } else if (result == -ENODATA) {
1507 obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
1509 LINVRNT(osd_invariant(obj));
1514 * The first part of oxe_buf is xattr name, and is '\0' terminated.
1515 * The left part is for value, binary mode.
1517 struct osd_xattr_entry {
1518 struct list_head oxe_list;
1522 struct rcu_head oxe_rcu;
1526 static int osd_oxc_get(struct osd_object *obj, const char *name,
1529 struct osd_xattr_entry *tmp;
1530 struct osd_xattr_entry *oxe = NULL;
1531 size_t namelen = strlen(name);
1535 list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1536 if (namelen == tmp->oxe_namelen &&
1537 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1544 GOTO(out, rc = -ENOENT);
1546 if (!oxe->oxe_exist)
1547 GOTO(out, rc = -ENODATA);
1550 rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1553 if (buf->lb_buf == NULL)
1556 if (buf->lb_len < rc)
1557 GOTO(out, rc = -ERANGE);
1559 memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1566 static void osd_oxc_free(struct rcu_head *head)
1568 struct osd_xattr_entry *oxe;
1570 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1571 OBD_FREE(oxe, oxe->oxe_len);
1574 static void osd_oxc_add(struct osd_object *obj, const char *name,
1575 const char *buf, int buflen)
1577 struct osd_xattr_entry *oxe;
1578 struct osd_xattr_entry *old = NULL;
1579 struct osd_xattr_entry *tmp;
1580 size_t namelen = strlen(name);
1581 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1583 OBD_ALLOC(oxe, len);
1587 INIT_LIST_HEAD(&oxe->oxe_list);
1589 oxe->oxe_namelen = namelen;
1590 memcpy(oxe->oxe_buf, name, namelen);
1592 LASSERT(buf != NULL);
1593 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1594 oxe->oxe_exist = true;
1596 oxe->oxe_exist = false;
1599 /* this should be rarely called, just remove old and add new */
1600 spin_lock(&obj->oo_guard);
1601 list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1602 if (namelen == tmp->oxe_namelen &&
1603 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1609 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1610 call_rcu(&old->oxe_rcu, osd_oxc_free);
1612 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1614 spin_unlock(&obj->oo_guard);
1617 static void osd_oxc_del(struct osd_object *obj, const char *name)
1619 struct osd_xattr_entry *oxe;
1620 size_t namelen = strlen(name);
1622 spin_lock(&obj->oo_guard);
1623 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1624 if (namelen == oxe->oxe_namelen &&
1625 strncmp(name, oxe->oxe_buf, namelen) == 0) {
1626 list_del_rcu(&oxe->oxe_list);
1627 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1631 spin_unlock(&obj->oo_guard);
1634 static void osd_oxc_fini(struct osd_object *obj)
1636 struct osd_xattr_entry *oxe, *next;
1638 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1639 list_del(&oxe->oxe_list);
1640 OBD_FREE(oxe, oxe->oxe_len);
1645 * Concurrency: no concurrent access is possible that late in object
1648 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1650 struct osd_object *obj = osd_obj(l);
1651 struct lu_object_header *h = obj->oo_header;
1653 LINVRNT(osd_invariant(obj));
1656 dt_object_fini(&obj->oo_dt);
1657 if (obj->oo_hl_head != NULL)
1658 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1659 /* obj doesn't contain an lu_object_header, so we don't need call_rcu */
1662 lu_object_header_free(h);
1666 * Concurrency: no concurrent access is possible that late in object
1669 static void osd_index_fini(struct osd_object *o)
1671 struct iam_container *bag;
1673 if (o->oo_dir != NULL) {
1674 bag = &o->oo_dir->od_container;
1675 if (o->oo_inode != NULL) {
1676 if (bag->ic_object == o->oo_inode)
1677 iam_container_fini(bag);
1679 OBD_FREE_PTR(o->oo_dir);
1685 OSD_TXN_OI_DELETE_CREDITS = 20,
1686 OSD_TXN_INODE_DELETE_CREDITS = 20
1693 #if OSD_THANDLE_STATS
1695 * Set time when the handle is allocated
1697 static void osd_th_alloced(struct osd_thandle *oth)
1699 oth->oth_alloced = ktime_get();
1703 * Set time when the handle started
1705 static void osd_th_started(struct osd_thandle *oth)
1707 oth->oth_started = ktime_get();
1711 * Check whether the we deal with this handle for too long.
1713 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1714 ktime_t alloced, ktime_t started,
1717 ktime_t now = ktime_get();
1719 LASSERT(dev != NULL);
1721 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1722 ktime_us_delta(started, alloced));
1723 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1724 ktime_us_delta(closed, started));
1725 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1726 ktime_us_delta(now, closed));
1728 if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1729 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1730 oth, now, alloced, started, closed);
1731 libcfs_debug_dumpstack(NULL);
1735 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1737 ktime_t __closed = ktime_get(); \
1738 ktime_t __alloced = oth->oth_alloced; \
1739 ktime_t __started = oth->oth_started; \
1742 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1745 #else /* OSD_THANDLE_STATS */
1747 #define osd_th_alloced(h) do {} while(0)
1748 #define osd_th_started(h) do {} while(0)
1749 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1751 #endif /* OSD_THANDLE_STATS */
1754 * Concurrency: doesn't access mutable data.
1756 static int osd_param_is_not_sane(const struct osd_device *dev,
1757 const struct thandle *th)
1759 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1761 return oh->ot_credits > osd_transaction_size(dev);
1765 * Concurrency: shouldn't matter.
1767 static void osd_trans_commit_cb(struct super_block *sb,
1768 struct ldiskfs_journal_cb_entry *jcb, int error)
1770 struct osd_thandle *oh = container_of(jcb, struct osd_thandle, ot_jcb);
1771 struct thandle *th = &oh->ot_super;
1772 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1773 struct osd_device *osd = osd_dev(lud);
1774 struct dt_txn_commit_cb *dcb, *tmp;
1776 LASSERT(oh->ot_handle == NULL);
1779 CERROR("transaction @0x%p commit error: %d\n", th, error);
1781 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_DELAY_TRANS, 40);
1782 /* call per-transaction callbacks if any */
1783 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1785 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1786 "commit callback entry: magic=%x name='%s'\n",
1787 dcb->dcb_magic, dcb->dcb_name);
1788 list_del_init(&dcb->dcb_linkage);
1789 dcb->dcb_func(NULL, th, dcb, error);
1792 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1793 if (atomic_dec_and_test(&osd->od_commit_cb_in_flight))
1794 wake_up(&osd->od_commit_cb_done);
1800 static struct thandle *osd_trans_create(const struct lu_env *env,
1801 struct dt_device *d)
1803 struct osd_thread_info *oti = osd_oti_get(env);
1804 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1805 struct osd_thandle *oh;
1811 CERROR("%s: someone try to start transaction under "
1812 "readonly mode, should be disabled.\n",
1813 osd_name(osd_dt_dev(d)));
1815 RETURN(ERR_PTR(-EROFS));
1818 /* on pending IO in this thread should left from prev. request */
1819 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1821 sb_start_write(osd_sb(osd_dt_dev(d)));
1823 OBD_ALLOC_GFP(oh, sizeof(*oh), GFP_NOFS);
1825 sb_end_write(osd_sb(osd_dt_dev(d)));
1826 RETURN(ERR_PTR(-ENOMEM));
1829 oh->ot_quota_trans = &oti->oti_quota_trans;
1830 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1835 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1836 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1837 INIT_LIST_HEAD(&oh->ot_trunc_locks);
1840 memset(oti->oti_declare_ops, 0,
1841 sizeof(oti->oti_declare_ops));
1842 memset(oti->oti_declare_ops_cred, 0,
1843 sizeof(oti->oti_declare_ops_cred));
1844 memset(oti->oti_declare_ops_used, 0,
1845 sizeof(oti->oti_declare_ops_used));
1847 oti->oti_ins_cache_depth++;
1852 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1854 struct osd_thread_info *oti = osd_oti_get(env);
1855 struct osd_thandle *oh;
1857 oh = container_of(th, struct osd_thandle, ot_super);
1858 LASSERT(oh != NULL);
1860 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1861 oti->oti_declare_ops[OSD_OT_CREATE],
1862 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1863 oti->oti_declare_ops_used[OSD_OT_CREATE],
1864 oti->oti_declare_ops[OSD_OT_DESTROY],
1865 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1866 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1867 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1868 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1869 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1870 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1871 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1872 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1873 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1874 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1875 oti->oti_declare_ops[OSD_OT_WRITE],
1876 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1877 oti->oti_declare_ops_used[OSD_OT_WRITE],
1878 oti->oti_declare_ops[OSD_OT_PUNCH],
1879 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1880 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1881 oti->oti_declare_ops[OSD_OT_QUOTA],
1882 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1883 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1884 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1885 oti->oti_declare_ops[OSD_OT_INSERT],
1886 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1887 oti->oti_declare_ops_used[OSD_OT_INSERT],
1888 oti->oti_declare_ops[OSD_OT_DELETE],
1889 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1890 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1891 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1892 oti->oti_declare_ops[OSD_OT_REF_ADD],
1893 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1894 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1895 oti->oti_declare_ops[OSD_OT_REF_DEL],
1896 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1897 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1901 * Concurrency: shouldn't matter.
1903 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1906 struct osd_thread_info *oti = osd_oti_get(env);
1907 struct osd_device *dev = osd_dt_dev(d);
1909 struct osd_thandle *oh;
1914 LASSERT(current->journal_info == NULL);
1916 oh = container_of(th, struct osd_thandle, ot_super);
1917 LASSERT(oh != NULL);
1918 LASSERT(oh->ot_handle == NULL);
1920 rc = dt_txn_hook_start(env, d, th);
1924 if (unlikely(osd_param_is_not_sane(dev, th))) {
1925 static unsigned long last_printed;
1926 static int last_credits;
1929 * don't make noise on a tiny testing systems
1930 * actual credits misuse will be caught anyway
1932 if (last_credits != oh->ot_credits &&
1933 time_after(jiffies, last_printed +
1934 cfs_time_seconds(60)) &&
1935 osd_transaction_size(dev) > 512) {
1936 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1937 oh->ot_credits, osd_transaction_size(dev));
1938 osd_trans_dump_creds(env, th);
1939 libcfs_debug_dumpstack(NULL);
1940 last_credits = oh->ot_credits;
1941 last_printed = jiffies;
1944 * XXX Limit the credits to 'max_transaction_buffers', and
1945 * let the underlying filesystem to catch the error if
1946 * we really need so many credits.
1948 * This should be removed when we can calculate the
1949 * credits precisely.
1951 oh->ot_credits = osd_transaction_size(dev);
1952 } else if (ldiskfs_track_declares_assert != 0) {
1954 * reserve few credits to prevent an assertion in JBD
1955 * our debugging mechanism will be able to detected
1956 * overuse. this can help to debug single-update
1959 oh->ot_credits += 10;
1960 if (unlikely(osd_param_is_not_sane(dev, th)))
1961 oh->ot_credits = osd_transaction_size(dev);
1964 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_TXN_START))
1965 GOTO(out, rc = -EIO);
1968 * XXX temporary stuff. Some abstraction layer should
1971 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1975 LASSERT(oti->oti_txns == 0);
1977 atomic_inc(&dev->od_commit_cb_in_flight);
1978 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1989 static int osd_seq_exists(const struct lu_env *env,
1990 struct osd_device *osd, u64 seq)
1992 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1993 struct seq_server_site *ss = osd_seq_site(osd);
1998 LASSERT(ss != NULL);
1999 LASSERT(ss->ss_server_fld != NULL);
2001 rc = osd_fld_lookup(env, osd, seq, range);
2004 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
2005 osd_name(osd), seq, rc);
2009 RETURN(ss->ss_node_id == range->lsr_index);
2012 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
2014 struct dt_txn_commit_cb *dcb;
2015 struct dt_txn_commit_cb *tmp;
2017 /* call per-transaction stop callbacks if any */
2018 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
2020 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
2021 "commit callback entry: magic=%x name='%s'\n",
2022 dcb->dcb_magic, dcb->dcb_name);
2023 list_del_init(&dcb->dcb_linkage);
2024 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
2029 * Concurrency: shouldn't matter.
2031 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
2034 struct osd_thread_info *oti = osd_oti_get(env);
2035 struct osd_thandle *oh;
2036 struct osd_iobuf *iobuf = &oti->oti_iobuf;
2037 struct osd_device *osd = osd_dt_dev(th->th_dev);
2038 struct qsd_instance *qsd = osd_def_qsd(osd);
2039 struct lquota_trans *qtrans;
2040 LIST_HEAD(truncates);
2041 int rc = 0, remove_agents = 0;
2045 oh = container_of(th, struct osd_thandle, ot_super);
2047 remove_agents = oh->ot_remove_agents;
2049 qtrans = oh->ot_quota_trans;
2050 oh->ot_quota_trans = NULL;
2052 /* move locks to local list, stop tx, execute truncates */
2053 list_splice(&oh->ot_trunc_locks, &truncates);
2055 if (oh->ot_handle != NULL) {
2058 handle_t *hdl = oh->ot_handle;
2061 * add commit callback
2062 * notice we don't do this in osd_trans_start()
2063 * as underlying transaction can change during truncate
2065 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
2068 LASSERT(oti->oti_txns == 1);
2071 rc = dt_txn_hook_stop(env, th);
2073 CERROR("%s: failed in transaction hook: rc = %d\n",
2076 osd_trans_stop_cb(oh, rc);
2077 /* hook functions might modify th_sync */
2078 hdl->h_sync = th->th_sync;
2080 oh->ot_handle = NULL;
2081 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
2083 CERROR("%s: failed to stop transaction: rc = %d\n",
2084 osd_name(osd), rc2);
2088 osd_process_truncates(&truncates);
2090 osd_trans_stop_cb(oh, th->th_result);
2094 osd_trunc_unlock_all(env, &truncates);
2096 /* inform the quota slave device that the transaction is stopping */
2097 qsd_op_end(env, qsd, qtrans);
2100 * as we want IO to journal and data IO be concurrent, we don't block
2101 * awaiting data IO completion in osd_do_bio(), instead we wait here
2102 * once transaction is submitted to the journal. all reqular requests
2103 * don't do direct IO (except read/write), thus this wait_event becomes
2106 * IMPORTANT: we have to wait till any IO submited by the thread is
2107 * completed otherwise iobuf may be corrupted by different request
2109 wait_event(iobuf->dr_wait,
2110 atomic_read(&iobuf->dr_numreqs) == 0);
2111 osd_fini_iobuf(osd, iobuf);
2113 rc = iobuf->dr_error;
2115 if (unlikely(remove_agents != 0))
2116 osd_process_scheduled_agent_removals(env, osd);
2118 LASSERT(oti->oti_ins_cache_depth > 0);
2119 oti->oti_ins_cache_depth--;
2120 /* reset OI cache for safety */
2121 if (oti->oti_ins_cache_depth == 0)
2122 oti->oti_ins_cache_used = 0;
2124 sb_end_write(osd_sb(osd));
2129 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
2131 struct osd_thandle *oh = container_of(th, struct osd_thandle,
2134 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
2135 LASSERT(&dcb->dcb_func != NULL);
2136 if (dcb->dcb_flags & DCB_TRANS_STOP)
2137 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
2139 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
2145 * Called just before object is freed. Releases all resources except for
2146 * object itself (that is released by osd_object_free()).
2148 * Concurrency: no concurrent access is possible that late in object
2151 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
2153 struct osd_object *obj = osd_obj(l);
2154 struct qsd_instance *qsd = osd_def_qsd(osd_obj2dev(obj));
2155 struct inode *inode = obj->oo_inode;
2160 LINVRNT(osd_invariant(obj));
2163 * If object is unlinked remove fid->ino mapping from object index.
2166 osd_index_fini(obj);
2171 if (osd_has_index(obj) && obj->oo_dt.do_index_ops == &osd_index_iam_ops)
2172 ldiskfs_set_inode_flag(inode, LDISKFS_INODE_JOURNAL_DATA);
2174 uid = i_uid_read(inode);
2175 gid = i_gid_read(inode);
2176 projid = i_projid_read(inode);
2178 obj->oo_inode = NULL;
2181 /* do not rebalance quota if the caller needs to release memory
2182 * otherwise qsd_refresh_usage() may went into a new ldiskfs
2183 * transaction and risk to deadlock - LU-12178 */
2184 if (current->flags & (PF_MEMALLOC | PF_KSWAPD))
2187 if (!obj->oo_header && qsd) {
2188 struct osd_thread_info *info = osd_oti_get(env);
2189 struct lquota_id_info *qi = &info->oti_qi;
2191 /* Release granted quota to master if necessary */
2192 qi->lqi_id.qid_uid = uid;
2193 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
2195 qi->lqi_id.qid_uid = gid;
2196 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
2198 qi->lqi_id.qid_uid = projid;
2199 qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
2204 * Concurrency: ->loo_object_release() is called under site spin-lock.
2206 static void osd_object_release(const struct lu_env *env,
2207 struct lu_object *l)
2209 struct osd_object *o = osd_obj(l);
2212 * nobody should be releasing a non-destroyed object with nlink=0
2213 * the API allows this, but ldiskfs doesn't like and then report
2214 * this inode as deleted
2216 LASSERT(!(o->oo_destroyed == 0 && o->oo_inode &&
2217 o->oo_inode->i_nlink == 0));
2221 * Concurrency: shouldn't matter.
2223 static int osd_object_print(const struct lu_env *env, void *cookie,
2224 lu_printer_t p, const struct lu_object *l)
2226 struct osd_object *o = osd_obj(l);
2227 struct iam_descr *d;
2229 if (o->oo_dir != NULL)
2230 d = o->oo_dir->od_container.ic_descr;
2233 return (*p)(env, cookie,
2234 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
2236 o->oo_inode ? o->oo_inode->i_ino : 0UL,
2237 o->oo_inode ? o->oo_inode->i_generation : 0,
2238 d ? d->id_ops->id_name : "plain");
2242 * Concurrency: shouldn't matter.
2244 int osd_statfs(const struct lu_env *env, struct dt_device *d,
2245 struct obd_statfs *sfs, struct obd_statfs_info *info)
2247 struct osd_device *osd = osd_dt_dev(d);
2248 struct super_block *sb = osd_sb(osd);
2249 struct kstatfs *ksfs;
2253 if (unlikely(osd->od_mnt == NULL))
2254 return -EINPROGRESS;
2256 /* osd_lproc.c call this without env, allocate ksfs for that case */
2257 if (unlikely(env == NULL)) {
2258 OBD_ALLOC_PTR(ksfs);
2262 ksfs = &osd_oti_get(env)->oti_ksfs;
2265 result = sb->s_op->statfs(sb->s_root, ksfs);
2269 statfs_pack(sfs, ksfs);
2270 if (unlikely(sb->s_flags & SB_RDONLY))
2271 sfs->os_state |= OS_STATFS_READONLY;
2273 sfs->os_state |= osd->od_nonrotational ? OS_STATFS_NONROT : 0;
2275 if (ldiskfs_has_feature_extents(sb))
2276 sfs->os_maxbytes = sb->s_maxbytes;
2278 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2281 * Reserve some space so to avoid fragmenting the filesystem too much.
2282 * Fragmentation not only impacts performance, but can also increase
2283 * metadata overhead significantly, causing grant calculation to be
2286 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2288 BUILD_BUG_ON(OSD_STATFS_RESERVED <= LDISKFS_MAX_BLOCK_SIZE);
2289 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2290 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2291 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2293 sfs->os_blocks -= reserved;
2294 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2295 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2298 if (unlikely(env == NULL))
2304 * Estimate space needed for file creations. We assume the largest filename
2305 * which is 2^64 - 1, hence a filename of 20 chars.
2306 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2308 #ifdef __LDISKFS_DIR_REC_LEN
2309 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2311 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2315 * Concurrency: doesn't access mutable data.
2317 static void osd_conf_get(const struct lu_env *env,
2318 const struct dt_device *dev,
2319 struct dt_device_param *param)
2321 struct osd_device *d = osd_dt_dev(dev);
2322 struct super_block *sb = osd_sb(d);
2323 struct blk_integrity *bi = bdev_get_integrity(sb->s_bdev);
2328 * XXX should be taken from not-yet-existing fs abstraction layer.
2330 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2331 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2332 param->ddp_symlink_max = sb->s_blocksize;
2333 param->ddp_mount_type = LDD_MT_LDISKFS;
2334 if (ldiskfs_has_feature_extents(sb))
2335 param->ddp_maxbytes = sb->s_maxbytes;
2337 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2339 * inode are statically allocated, so per-inode space consumption
2340 * is the space consumed by the directory entry
2342 param->ddp_inodespace = PER_OBJ_USAGE;
2344 * EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2345 * is 128MB) which is unlikely to be hit in real life. Report a smaller
2346 * maximum length to not under-count the actual number of extents
2347 * needed for writing a file if there are sub-optimal block allocations.
2349 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 1;
2350 /* worst-case extent insertion metadata overhead */
2351 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2352 param->ddp_mntopts = 0;
2353 if (test_opt(sb, XATTR_USER))
2354 param->ddp_mntopts |= MNTOPT_USERXATTR;
2355 if (test_opt(sb, POSIX_ACL))
2356 param->ddp_mntopts |= MNTOPT_ACL;
2359 * LOD might calculate the max stripe count based on max_ea_size,
2360 * so we need take account in the overhead as well,
2361 * xattr_header + magic + xattr_entry_head
2363 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2364 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2366 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2367 if (ldiskfs_has_feature_ea_inode(sb))
2368 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2372 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2374 if (param->ddp_max_ea_size > OBD_MAX_EA_SIZE)
2375 param->ddp_max_ea_size = OBD_MAX_EA_SIZE;
2378 * Preferred RPC size for efficient disk IO. 4MB shows good
2379 * all-around performance for ldiskfs, but use bigalloc chunk size
2380 * by default if larger.
2382 #if defined(LDISKFS_CLUSTER_SIZE)
2383 if (LDISKFS_CLUSTER_SIZE(sb) > DT_DEF_BRW_SIZE)
2384 param->ddp_brw_size = LDISKFS_CLUSTER_SIZE(sb);
2387 param->ddp_brw_size = DT_DEF_BRW_SIZE;
2389 param->ddp_t10_cksum_type = 0;
2391 unsigned short interval = blk_integrity_interval(bi);
2392 name = blk_integrity_name(bi);
2400 if (strncmp(name, "T10-DIF-TYPE",
2401 sizeof("T10-DIF-TYPE") - 1) == 0) {
2402 /* also skip "1/3-" at end */
2403 const int type_off = sizeof("T10-DIF-TYPE.");
2404 char type_number = name[type_off - 2];
2406 if (interval != 512 && interval != 4096) {
2407 CERROR("%s: unsupported T10PI sector size %u\n",
2408 d->od_svname, interval);
2409 } else if (type_number != '1' && type_number != '3') {
2410 CERROR("%s: unsupported T10PI type %s\n",
2411 d->od_svname, name);
2412 } else if (strcmp(name + type_off, "CRC") == 0) {
2413 d->od_t10_type = type_number == '1' ?
2414 OSD_T10_TYPE1_CRC : OSD_T10_TYPE3_CRC;
2415 param->ddp_t10_cksum_type = interval == 512 ?
2416 OBD_CKSUM_T10CRC512 :
2418 } else if (strcmp(name + type_off, "IP") == 0) {
2419 d->od_t10_type = type_number == '1' ?
2420 OSD_T10_TYPE1_IP : OSD_T10_TYPE3_IP;
2421 param->ddp_t10_cksum_type = interval == 512 ?
2422 OBD_CKSUM_T10IP512 :
2425 CERROR("%s: unsupported checksum type of "
2427 d->od_svname, name);
2431 CERROR("%s: unsupported T10PI type '%s'",
2432 d->od_svname, name);
2436 param->ddp_has_lseek_data_hole = true;
2439 static struct super_block *osd_mnt_sb_get(const struct dt_device *d)
2441 return osd_sb(osd_dt_dev(d));
2445 * Concurrency: shouldn't matter.
2447 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2450 struct super_block *s = osd_sb(osd_dt_dev(d));
2453 down_read(&s->s_umount);
2454 rc = s->s_op->sync_fs(s, 1);
2455 up_read(&s->s_umount);
2457 CDEBUG(D_CACHE, "%s: synced OSD: rc = %d\n", osd_dt_dev(d)->od_svname,
2464 * Start commit for OSD device.
2466 * An implementation of dt_commit_async method for OSD device.
2467 * Asychronously starts underlayng fs sync and thereby a transaction
2470 * \param env environment
2471 * \param d dt device
2473 * \see dt_device_operations
2475 static int osd_commit_async(const struct lu_env *env,
2476 struct dt_device *d)
2478 struct super_block *s = osd_sb(osd_dt_dev(d));
2483 CDEBUG(D_HA, "%s: async commit OSD\n", osd_dt_dev(d)->od_svname);
2484 down_read(&s->s_umount);
2485 rc = s->s_op->sync_fs(s, 0);
2486 up_read(&s->s_umount);
2491 static int (*priv_security_file_alloc)(struct file *file);
2493 int osd_security_file_alloc(struct file *file)
2495 if (priv_security_file_alloc)
2496 return priv_security_file_alloc(file);
2501 * Concurrency: shouldn't matter.
2503 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2505 struct super_block *sb = osd_sb(osd_dt_dev(d));
2506 struct block_device *dev = sb->s_bdev;
2507 int rc = -EOPNOTSUPP;
2511 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2512 osd_dt_dev(d)->od_svname, (long)dev, rc);
2518 * Note: we do not count into QUOTA here.
2519 * If we mount with --data_journal we may need more.
2521 const int osd_dto_credits_noquota[DTO_NR] = {
2524 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2525 * SINGLEDATA_TRANS_BLOCKS(8)
2526 * XXX Note: maybe iam need more, since iam have more level than
2529 [DTO_INDEX_INSERT] = 16,
2532 * just modify a single entry, probably merge few within a block
2534 [DTO_INDEX_DELETE] = 1,
2538 [DTO_INDEX_UPDATE] = 16,
2540 * 4(inode, inode bits, groups, GDT)
2541 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2543 [DTO_OBJECT_CREATE] = 4,
2545 * 4(inode, inode bits, groups, GDT)
2546 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2548 [DTO_OBJECT_DELETE] = 4,
2550 * Attr set credits (inode)
2552 [DTO_ATTR_SET_BASE] = 1,
2554 * Xattr set. The same as xattr of EXT3.
2555 * DATA_TRANS_BLOCKS(14)
2556 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2557 * are also counted in. Do not know why?
2559 [DTO_XATTR_SET] = 14,
2561 * credits for inode change during write.
2563 [DTO_WRITE_BASE] = 3,
2565 * credits for single block write.
2567 [DTO_WRITE_BLOCK] = 14,
2569 * Attr set credits for chown.
2570 * This is extra credits for setattr, and it is null without quota
2572 [DTO_ATTR_SET_CHOWN] = 0
2575 static const struct dt_device_operations osd_dt_ops = {
2576 .dt_root_get = osd_root_get,
2577 .dt_statfs = osd_statfs,
2578 .dt_trans_create = osd_trans_create,
2579 .dt_trans_start = osd_trans_start,
2580 .dt_trans_stop = osd_trans_stop,
2581 .dt_trans_cb_add = osd_trans_cb_add,
2582 .dt_conf_get = osd_conf_get,
2583 .dt_mnt_sb_get = osd_mnt_sb_get,
2584 .dt_sync = osd_sync,
2586 .dt_commit_async = osd_commit_async,
2589 static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
2592 struct osd_object *obj = osd_dt_obj(dt);
2593 struct osd_thread_info *oti = osd_oti_get(env);
2595 LINVRNT(osd_invariant(obj));
2597 LASSERT(obj->oo_owner != env);
2598 down_read_nested(&obj->oo_sem, role);
2600 LASSERT(obj->oo_owner == NULL);
2604 static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
2607 struct osd_object *obj = osd_dt_obj(dt);
2608 struct osd_thread_info *oti = osd_oti_get(env);
2610 LINVRNT(osd_invariant(obj));
2612 LASSERT(obj->oo_owner != env);
2613 down_write_nested(&obj->oo_sem, role);
2615 LASSERT(obj->oo_owner == NULL);
2616 obj->oo_owner = env;
2620 static void osd_read_unlock(const struct lu_env *env, struct dt_object *dt)
2622 struct osd_object *obj = osd_dt_obj(dt);
2623 struct osd_thread_info *oti = osd_oti_get(env);
2625 LINVRNT(osd_invariant(obj));
2627 LASSERT(oti->oti_r_locks > 0);
2629 up_read(&obj->oo_sem);
2632 static void osd_write_unlock(const struct lu_env *env, struct dt_object *dt)
2634 struct osd_object *obj = osd_dt_obj(dt);
2635 struct osd_thread_info *oti = osd_oti_get(env);
2637 LINVRNT(osd_invariant(obj));
2639 LASSERT(obj->oo_owner == env);
2640 LASSERT(oti->oti_w_locks > 0);
2642 obj->oo_owner = NULL;
2643 up_write(&obj->oo_sem);
2646 static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
2648 struct osd_object *obj = osd_dt_obj(dt);
2650 LINVRNT(osd_invariant(obj));
2652 return obj->oo_owner == env;
2655 static void osd_inode_getattr(const struct lu_env *env,
2656 struct inode *inode, struct lu_attr *attr)
2658 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2659 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2660 LA_PROJID | LA_FLAGS | LA_NLINK | LA_RDEV |
2661 LA_BLKSIZE | LA_TYPE | LA_BTIME;
2663 attr->la_atime = inode->i_atime.tv_sec;
2664 attr->la_mtime = inode->i_mtime.tv_sec;
2665 attr->la_ctime = inode->i_ctime.tv_sec;
2666 attr->la_btime = LDISKFS_I(inode)->i_crtime.tv_sec;
2667 attr->la_mode = inode->i_mode;
2668 attr->la_size = i_size_read(inode);
2669 attr->la_blocks = inode->i_blocks;
2670 attr->la_uid = i_uid_read(inode);
2671 attr->la_gid = i_gid_read(inode);
2672 attr->la_projid = i_projid_read(inode);
2673 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2674 attr->la_nlink = inode->i_nlink;
2675 attr->la_rdev = inode->i_rdev;
2676 attr->la_blksize = 1 << inode->i_blkbits;
2677 attr->la_blkbits = inode->i_blkbits;
2679 * Ext4 did not transfer inherit flags from raw inode
2680 * to inode flags, and ext4 internally test raw inode
2681 * @i_flags directly. Instead of patching ext4, we do it here.
2683 if (LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL)
2684 attr->la_flags |= LUSTRE_PROJINHERIT_FL;
2687 static int osd_dirent_count(const struct lu_env *env, struct dt_object *dt,
2690 struct osd_object *obj = osd_dt_obj(dt);
2691 const struct dt_it_ops *iops;
2697 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
2698 LASSERT(fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)));
2700 if (obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
2701 *count = obj->oo_dirent_count;
2705 /* directory not initialized yet */
2706 if (!dt->do_index_ops) {
2711 iops = &dt->do_index_ops->dio_it;
2712 it = iops->init(env, dt, LUDA_64BITHASH);
2714 RETURN(PTR_ERR(it));
2716 rc = iops->load(env, it, 0);
2718 if (rc == -ENODATA) {
2725 rc = iops->next(env, it);
2727 for (*count = 0; rc == 0 || rc == -ESTALE; rc = iops->next(env, it)) {
2731 if (iops->key_size(env, it) == 0)
2737 obj->oo_dirent_count = *count;
2742 iops->fini(env, it);
2747 static int osd_attr_get(const struct lu_env *env, struct dt_object *dt,
2748 struct lu_attr *attr)
2750 struct osd_object *obj = osd_dt_obj(dt);
2753 if (unlikely(!dt_object_exists(dt)))
2755 if (unlikely(obj->oo_destroyed))
2758 LASSERT(!dt_object_remote(dt));
2759 LINVRNT(osd_invariant(obj));
2761 spin_lock(&obj->oo_guard);
2762 osd_inode_getattr(env, obj->oo_inode, attr);
2763 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL) {
2764 attr->la_valid |= LA_FLAGS;
2765 attr->la_flags |= LUSTRE_ORPHAN_FL;
2767 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL) {
2768 attr->la_valid |= LA_FLAGS;
2769 attr->la_flags |= LUSTRE_ENCRYPT_FL;
2771 spin_unlock(&obj->oo_guard);
2773 if (S_ISDIR(obj->oo_inode->i_mode) &&
2774 fid_is_namespace_visible(lu_object_fid(&dt->do_lu)))
2775 rc = osd_dirent_count(env, dt, &attr->la_dirent_count);
2780 static int osd_declare_attr_qid(const struct lu_env *env,
2781 struct osd_object *obj,
2782 struct osd_thandle *oh, long long bspace,
2783 qid_t old_id, qid_t new_id, bool enforce,
2784 unsigned int type, bool ignore_edquot)
2787 struct osd_thread_info *info = osd_oti_get(env);
2788 struct lquota_id_info *qi = &info->oti_qi;
2790 qi->lqi_type = type;
2791 /* inode accounting */
2792 qi->lqi_is_blk = false;
2794 /* one more inode for the new id ... */
2795 qi->lqi_id.qid_uid = new_id;
2797 /* Reserve credits for the new id */
2798 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2799 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2804 /* and one less inode for the current id */
2805 qi->lqi_id.qid_uid = old_id;
2807 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2808 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2813 /* block accounting */
2814 qi->lqi_is_blk = true;
2816 /* more blocks for the new id ... */
2817 qi->lqi_id.qid_uid = new_id;
2818 qi->lqi_space = bspace;
2820 * Credits for the new uid has been reserved, re-use "obj"
2821 * to save credit reservation.
2823 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2824 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2829 /* and finally less blocks for the current uid */
2830 qi->lqi_id.qid_uid = old_id;
2831 qi->lqi_space = -bspace;
2832 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2833 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2839 static int osd_declare_attr_set(const struct lu_env *env,
2840 struct dt_object *dt,
2841 const struct lu_attr *attr,
2842 struct thandle *handle)
2844 struct osd_thandle *oh;
2845 struct osd_object *obj;
2854 LASSERT(dt != NULL);
2855 LASSERT(handle != NULL);
2857 obj = osd_dt_obj(dt);
2858 LASSERT(osd_invariant(obj));
2860 oh = container_of(handle, struct osd_thandle, ot_super);
2861 LASSERT(oh->ot_handle == NULL);
2863 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2864 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2866 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2867 osd_dto_credits_noquota[DTO_XATTR_SET]);
2869 if (attr == NULL || obj->oo_inode == NULL)
2872 bspace = obj->oo_inode->i_blocks << 9;
2873 bspace = toqb(bspace);
2876 * Changing ownership is always preformed by super user, it should not
2877 * fail with EDQUOT unless required explicitly.
2879 * We still need to call the osd_declare_qid() to calculate the journal
2880 * credits for updating quota accounting files and to trigger quota
2881 * space adjustment once the operation is completed.
2883 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2884 bool ignore_edquot = !(attr->la_flags & LUSTRE_SET_SYNC_FL);
2888 "%s: enforce quota on UID %u, GID %u (quota space is %lld)\n",
2889 osd_ino2name(obj->oo_inode), attr->la_uid,
2890 attr->la_gid, bspace);
2893 uid = i_uid_read(obj->oo_inode);
2894 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2895 rc = osd_declare_attr_qid(env, obj, oh, bspace, uid,
2896 attr->la_uid, enforce, USRQUOTA,
2901 gid = i_gid_read(obj->oo_inode);
2902 CDEBUG(D_QUOTA, "declare uid %d -> %d gid %d -> %d\n", uid,
2903 attr->la_uid, gid, attr->la_gid);
2904 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2905 rc = osd_declare_attr_qid(env, obj, oh, bspace, gid,
2906 attr->la_gid, enforce, GRPQUOTA,
2912 #ifdef HAVE_PROJECT_QUOTA
2913 if (attr->la_valid & LA_PROJID) {
2914 __u32 projid = i_projid_read(obj->oo_inode);
2916 enforce = (attr->la_valid & LA_PROJID) &&
2917 (attr->la_projid != projid);
2918 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2919 (qid_t)projid, (qid_t)attr->la_projid,
2920 enforce, PRJQUOTA, true);
2928 static int osd_inode_setattr(const struct lu_env *env,
2929 struct inode *inode, const struct lu_attr *attr)
2931 __u64 bits = attr->la_valid;
2933 /* Only allow set size for regular file */
2934 if (!S_ISREG(inode->i_mode))
2935 bits &= ~(LA_SIZE | LA_BLOCKS);
2940 if (bits & LA_ATIME)
2941 inode->i_atime = osd_inode_time(inode, attr->la_atime);
2942 if (bits & LA_CTIME)
2943 inode->i_ctime = osd_inode_time(inode, attr->la_ctime);
2944 if (bits & LA_MTIME)
2945 inode->i_mtime = osd_inode_time(inode, attr->la_mtime);
2946 if (bits & LA_SIZE) {
2947 spin_lock(&inode->i_lock);
2948 LDISKFS_I(inode)->i_disksize = attr->la_size;
2949 i_size_write(inode, attr->la_size);
2950 spin_unlock(&inode->i_lock);
2954 * OSD should not change "i_blocks" which is used by quota.
2955 * "i_blocks" should be changed by ldiskfs only.
2958 inode->i_mode = (inode->i_mode & S_IFMT) |
2959 (attr->la_mode & ~S_IFMT);
2961 i_uid_write(inode, attr->la_uid);
2963 i_gid_write(inode, attr->la_gid);
2964 if (bits & LA_PROJID)
2965 i_projid_write(inode, attr->la_projid);
2966 if (bits & LA_NLINK)
2967 set_nlink(inode, attr->la_nlink);
2969 inode->i_rdev = attr->la_rdev;
2971 if (bits & LA_FLAGS) {
2972 /* always keep S_NOCMTIME */
2973 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2975 #if defined(S_ENCRYPTED)
2976 /* Always remove S_ENCRYPTED, because ldiskfs must not be
2977 * aware of encryption status. It is just stored into LMA
2978 * so that it can be forwared to client side.
2980 inode->i_flags &= ~S_ENCRYPTED;
2983 * Ext4 did not transfer inherit flags from
2984 * @inode->i_flags to raw inode i_flags when writing
2985 * flags, we do it explictly here.
2987 if (attr->la_flags & LUSTRE_PROJINHERIT_FL)
2988 LDISKFS_I(inode)->i_flags |= LUSTRE_PROJINHERIT_FL;
2990 LDISKFS_I(inode)->i_flags &= ~LUSTRE_PROJINHERIT_FL;
2995 #ifdef HAVE_PROJECT_QUOTA
2996 static int osd_transfer_project(struct inode *inode, __u32 projid,
2997 struct thandle *handle)
2999 struct super_block *sb = inode->i_sb;
3000 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
3003 struct ldiskfs_iloc iloc;
3004 struct ldiskfs_inode *raw_inode;
3005 struct dquot *transfer_to[LDISKFS_MAXQUOTAS] = { };
3007 if (!ldiskfs_has_feature_project(sb)) {
3008 LASSERT(__kprojid_val(LDISKFS_I(inode)->i_projid)
3009 == LDISKFS_DEF_PROJID);
3010 if (projid != LDISKFS_DEF_PROJID)
3016 if (LDISKFS_INODE_SIZE(sb) <= LDISKFS_GOOD_OLD_INODE_SIZE)
3019 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3020 if (projid_eq(kprojid, LDISKFS_I(inode)->i_projid))
3023 err = ldiskfs_get_inode_loc(inode, &iloc);
3027 raw_inode = ldiskfs_raw_inode(&iloc);
3028 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3029 struct osd_thandle *oh = container_of(handle,
3033 * try to expand inode size automatically.
3035 ldiskfs_mark_inode_dirty(oh->ot_handle, inode);
3036 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3044 dquot_initialize(inode);
3045 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3046 if (transfer_to[PRJQUOTA]) {
3047 err = __dquot_transfer(inode, transfer_to);
3048 dqput(transfer_to[PRJQUOTA]);
3057 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr,
3058 struct thandle *handle)
3062 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
3063 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
3067 "executing dquot_transfer inode %ld uid %d -> %d gid %d -> %d\n",
3068 inode->i_ino, i_uid_read(inode), attr->la_uid,
3069 i_gid_read(inode), attr->la_gid);
3071 dquot_initialize(inode);
3073 if (attr->la_valid & LA_UID)
3074 iattr.ia_valid |= ATTR_UID;
3075 if (attr->la_valid & LA_GID)
3076 iattr.ia_valid |= ATTR_GID;
3077 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
3078 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
3080 rc = dquot_transfer(inode, &iattr);
3082 CERROR("%s: quota transfer failed. Is quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3083 osd_ino2name(inode), rc);
3088 /* Handle project id transfer here properly */
3089 if (attr->la_valid & LA_PROJID &&
3090 attr->la_projid != i_projid_read(inode)) {
3091 #ifdef HAVE_PROJECT_QUOTA
3092 rc = osd_transfer_project(inode, attr->la_projid, handle);
3097 CERROR("%s: quota transfer failed. Is project enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3098 osd_ino2name(inode), rc);
3105 static int osd_attr_set(const struct lu_env *env,
3106 struct dt_object *dt,
3107 const struct lu_attr *attr,
3108 struct thandle *handle)
3110 struct osd_object *obj = osd_dt_obj(dt);
3111 struct inode *inode;
3114 if (!dt_object_exists(dt))
3117 LASSERT(handle != NULL);
3118 LASSERT(!dt_object_remote(dt));
3119 LASSERT(osd_invariant(obj));
3121 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
3123 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) &&
3124 !osd_obj2dev(obj)->od_is_ost) {
3125 struct osd_thread_info *oti = osd_oti_get(env);
3126 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
3127 struct lu_fid *fid1 = &oti->oti_fid;
3128 struct osd_inode_id *id = &oti->oti_id;
3129 struct iam_path_descr *ipd;
3130 struct iam_container *bag;
3131 struct osd_thandle *oh;
3134 fid_cpu_to_be(fid1, fid0);
3135 memset(id, 1, sizeof(*id));
3136 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
3137 fid0)->oi_dir.od_container;
3138 ipd = osd_idx_ipd_get(env, bag);
3139 if (unlikely(ipd == NULL))
3142 oh = container_of(handle, struct osd_thandle, ot_super);
3143 rc = iam_update(oh->ot_handle, bag,
3144 (const struct iam_key *)fid1,
3145 (const struct iam_rec *)id, ipd);
3146 osd_ipd_put(env, bag, ipd);
3147 return(rc > 0 ? 0 : rc);
3150 inode = obj->oo_inode;
3152 rc = osd_quota_transfer(inode, attr, handle);
3156 spin_lock(&obj->oo_guard);
3157 rc = osd_inode_setattr(env, inode, attr);
3158 spin_unlock(&obj->oo_guard);
3162 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3164 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
3166 if (!(attr->la_valid & LA_FLAGS))
3169 /* Let's check if there are extra flags need to be set into LMA */
3170 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
3171 struct osd_thread_info *info = osd_oti_get(env);
3172 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
3174 LASSERT(!obj->oo_pfid_in_lma);
3176 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
3177 &info->oti_ost_attrs);
3181 lma->lma_incompat |=
3182 lustre_to_lma_flags(attr->la_flags);
3183 lustre_lma_swab(lma);
3185 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3187 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
3188 lma, sizeof(*lma), XATTR_REPLACE);
3190 struct osd_device *osd = osd_obj2dev(obj);
3192 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
3193 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
3194 lma->lma_incompat, rc);
3197 attr->la_flags & LUSTRE_LMA_FL_MASKS;
3199 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3206 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
3207 struct osd_object *obj,
3208 const char *name, const int namelen)
3210 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
3213 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
3214 umode_t mode, struct dt_allocation_hint *hint,
3215 struct thandle *th, struct lu_attr *attr)
3218 struct osd_device *osd = osd_obj2dev(obj);
3219 struct osd_thandle *oth;
3220 struct dt_object *parent = NULL;
3221 struct inode *inode;
3222 uid_t owner[2] = {0, 0};
3224 if (attr->la_valid & LA_UID)
3225 owner[0] = attr->la_uid;
3226 if (attr->la_valid & LA_GID)
3227 owner[1] = attr->la_gid;
3229 LINVRNT(osd_invariant(obj));
3230 LASSERT(obj->oo_inode == NULL);
3231 LASSERT(obj->oo_hl_head == NULL);
3233 if (S_ISDIR(mode) && ldiskfs_pdo) {
3235 ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
3236 if (obj->oo_hl_head == NULL)
3240 oth = container_of(th, struct osd_thandle, ot_super);
3241 LASSERT(oth->ot_handle->h_transaction != NULL);
3243 if (hint != NULL && hint->dah_parent != NULL &&
3244 !dt_object_remote(hint->dah_parent))
3245 parent = hint->dah_parent;
3247 inode = ldiskfs_create_inode(oth->ot_handle,
3248 parent ? osd_dt_obj(parent)->oo_inode :
3249 osd_sb(osd)->s_root->d_inode,
3251 if (!IS_ERR(inode)) {
3252 /* Do not update file c/mtime in ldiskfs. */
3253 inode->i_flags |= S_NOCMTIME;
3256 * For new created object, it must be consistent,
3257 * and it is unnecessary to scrub against it.
3259 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
3261 obj->oo_inode = inode;
3264 if (obj->oo_hl_head != NULL) {
3265 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
3266 obj->oo_hl_head = NULL;
3268 result = PTR_ERR(inode);
3270 LINVRNT(osd_invariant(obj));
3278 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
3279 struct lu_attr *attr,
3280 struct dt_allocation_hint *hint,
3281 struct dt_object_format *dof,
3285 struct osd_thandle *oth;
3286 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
3288 LASSERT(S_ISDIR(attr->la_mode));
3290 oth = container_of(th, struct osd_thandle, ot_super);
3291 LASSERT(oth->ot_handle->h_transaction != NULL);
3292 if (fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)))
3293 obj->oo_dirent_count = 0;
3294 result = osd_mkfile(info, obj, mode, hint, th, attr);
3299 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
3300 struct lu_attr *attr,
3301 struct dt_allocation_hint *hint,
3302 struct dt_object_format *dof,
3306 struct osd_thandle *oth;
3307 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
3309 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
3311 LASSERT(S_ISREG(attr->la_mode));
3313 oth = container_of(th, struct osd_thandle, ot_super);
3314 LASSERT(oth->ot_handle->h_transaction != NULL);
3316 result = osd_mkfile(info, obj, mode, hint, th, attr);
3318 LASSERT(obj->oo_inode != NULL);
3319 if (feat->dif_flags & DT_IND_VARKEY)
3320 result = iam_lvar_create(obj->oo_inode,
3321 feat->dif_keysize_max,
3323 feat->dif_recsize_max,
3326 result = iam_lfix_create(obj->oo_inode,
3327 feat->dif_keysize_max,
3329 feat->dif_recsize_max,
3335 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
3336 struct lu_attr *attr,
3337 struct dt_allocation_hint *hint,
3338 struct dt_object_format *dof,
3341 LASSERT(S_ISREG(attr->la_mode));
3342 return osd_mkfile(info, obj, (attr->la_mode &
3343 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
3347 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
3348 struct lu_attr *attr,
3349 struct dt_allocation_hint *hint,
3350 struct dt_object_format *dof,
3353 LASSERT(S_ISLNK(attr->la_mode));
3354 return osd_mkfile(info, obj, (attr->la_mode &
3355 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
3359 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
3360 struct lu_attr *attr,
3361 struct dt_allocation_hint *hint,
3362 struct dt_object_format *dof,
3365 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
3368 LINVRNT(osd_invariant(obj));
3369 LASSERT(obj->oo_inode == NULL);
3370 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
3371 S_ISFIFO(mode) || S_ISSOCK(mode));
3373 result = osd_mkfile(info, obj, mode, hint, th, attr);
3375 LASSERT(obj->oo_inode != NULL);
3377 * This inode should be marked dirty for i_rdev. Currently
3378 * that is done in the osd_attr_init().
3380 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
3383 LINVRNT(osd_invariant(obj));
3387 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
3389 struct dt_allocation_hint *hint,
3390 struct dt_object_format *dof,
3393 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
3395 osd_obj_type_f result;
3411 result = osd_mk_index;
3422 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
3423 struct dt_object *parent, struct dt_object *child,
3428 ah->dah_parent = parent;
3429 ah->dah_mode = child_mode;
3431 if (parent != NULL && !dt_object_remote(parent)) {
3432 /* will help to find FID->ino at dt_insert("..") */
3433 struct osd_object *pobj = osd_dt_obj(parent);
3435 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
3439 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
3440 struct lu_attr *attr, struct dt_object_format *dof,
3441 struct thandle *handle)
3443 struct inode *inode = obj->oo_inode;
3444 __u64 valid = attr->la_valid;
3447 attr->la_valid &= ~(LA_TYPE | LA_MODE);
3449 if (dof->dof_type != DFT_NODE)
3450 attr->la_valid &= ~LA_RDEV;
3451 if ((valid & LA_ATIME) && (attr->la_atime == inode->i_atime.tv_sec))
3452 attr->la_valid &= ~LA_ATIME;
3453 if ((valid & LA_CTIME) && (attr->la_ctime == inode->i_ctime.tv_sec))
3454 attr->la_valid &= ~LA_CTIME;
3455 if ((valid & LA_MTIME) && (attr->la_mtime == inode->i_mtime.tv_sec))
3456 attr->la_valid &= ~LA_MTIME;
3458 result = osd_quota_transfer(inode, attr, handle);
3462 if (attr->la_valid != 0) {
3463 result = osd_inode_setattr(info->oti_env, inode, attr);
3465 * The osd_inode_setattr() should always succeed here. The
3466 * only error that could be returned is EDQUOT when we are
3467 * trying to change the UID or GID of the inode. However, this
3468 * should not happen since quota enforcement is no longer
3469 * enabled on ldiskfs (lquota takes care of it).
3471 LASSERTF(result == 0, "%d\n", result);
3472 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3475 attr->la_valid = valid;
3479 * Helper function for osd_create()
3481 * \retval 0, on success
3483 static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
3484 struct lu_attr *attr, struct dt_allocation_hint *hint,
3485 struct dt_object_format *dof, struct thandle *th)
3490 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
3492 /* we drop umask so that permissions we pass are not affected */
3493 umask = current->fs->umask;
3494 current->fs->umask = 0;
3496 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
3498 if (likely(obj->oo_inode != NULL)) {
3499 LASSERT(obj->oo_inode->i_state & I_NEW);
3502 * Unlock the inode before attr initialization to avoid
3503 * unnecessary dqget operations. LU-6378
3505 unlock_new_inode(obj->oo_inode);
3508 if (likely(result == 0)) {
3509 osd_attr_init(info, obj, attr, dof, th);
3510 osd_object_init0(obj);
3513 /* restore previous umask value */
3514 current->fs->umask = umask;
3516 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
3522 * Helper function for osd_create()
3524 * \retval 0, on success
3526 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
3527 const struct lu_fid *fid, struct thandle *th)
3529 struct osd_thread_info *info = osd_oti_get(env);
3530 struct osd_inode_id *id = &info->oti_id;
3531 struct osd_device *osd = osd_obj2dev(obj);
3532 struct osd_thandle *oh;
3535 LASSERT(obj->oo_inode != NULL);
3537 oh = container_of(th, struct osd_thandle, ot_super);
3538 LASSERT(oh->ot_handle);
3539 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3541 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3542 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3543 OI_CHECK_FLD, NULL);
3544 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP) && osd->od_is_ost) {
3545 struct lu_fid next_fid = *fid;
3547 /* insert next object in advance, and map to the same inode */
3549 if (next_fid.f_oid != 0) {
3550 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3551 osd_oi_insert(info, osd, &next_fid, id, oh->ot_handle,
3552 OI_CHECK_FLD, NULL);
3553 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3557 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3562 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3563 u64 seq, struct lu_seq_range *range)
3565 struct seq_server_site *ss = osd_seq_site(osd);
3567 if (fid_seq_is_idif(seq)) {
3568 fld_range_set_ost(range);
3569 range->lsr_index = idif_ost_idx(seq);
3573 if (!fid_seq_in_fldb(seq)) {
3574 fld_range_set_mdt(range);
3577 * FIXME: If ss is NULL, it suppose not get lsr_index
3580 range->lsr_index = ss->ss_node_id;
3584 LASSERT(ss != NULL);
3585 fld_range_set_any(range);
3586 /* OSD will only do local fld lookup */
3587 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3590 static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
3591 struct lu_attr *attr,
3592 struct dt_allocation_hint *hint,
3593 struct dt_object_format *dof,
3594 struct thandle *handle)
3596 struct osd_thandle *oh;
3601 LASSERT(handle != NULL);
3603 oh = container_of(handle, struct osd_thandle, ot_super);
3604 LASSERT(oh->ot_handle == NULL);
3607 * EA object consumes more credits than regular object: osd_mk_index
3608 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3609 * leaf_node, could involves the block, block bitmap, groups, GDT
3610 * change for each block, so add 4 * 2 credits in that case.
3612 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3613 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3614 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3616 * Reuse idle OI block may cause additional one OI block
3619 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3620 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3621 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP))
3622 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3623 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3625 /* will help to find FID->ino mapping at dt_insert() */
3626 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3634 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid,
3635 attr->la_projid, 1, oh, osd_dt_obj(dt),
3636 NULL, OSD_QID_INODE);
3644 * Called to destroy on-disk representation of the object
3646 * Concurrency: must be locked
3648 static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
3651 struct osd_object *obj = osd_dt_obj(dt);
3652 struct inode *inode = obj->oo_inode;
3653 struct osd_thandle *oh;
3661 oh = container_of(th, struct osd_thandle, ot_super);
3662 LASSERT(oh->ot_handle == NULL);
3664 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3665 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3667 /* For removing agent entry */
3668 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu))
3669 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3672 * Recycle idle OI leaf may cause additional three OI blocks
3675 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3676 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3677 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3678 /* one less inode */
3679 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3680 i_projid_read(inode), -1, oh, obj, NULL,
3684 /* data to be truncated */
3685 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3686 i_projid_read(inode), 0, oh, obj, NULL,
3692 * will help to find FID->ino when this object is being
3695 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3700 static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
3703 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3704 struct osd_object *obj = osd_dt_obj(dt);
3705 struct inode *inode = obj->oo_inode;
3706 struct osd_device *osd = osd_obj2dev(obj);
3707 struct osd_thandle *oh;
3712 oh = container_of(th, struct osd_thandle, ot_super);
3713 LASSERT(oh->ot_handle);
3715 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3717 if (unlikely(fid_is_acct(fid)))
3720 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu)) {
3721 result = osd_delete_from_remote_parent(env, osd, obj, oh, true);
3723 CERROR("%s: remove agent entry "DFID": rc = %d\n",
3724 osd_name(osd), PFID(fid), result);
3727 if (S_ISDIR(inode->i_mode)) {
3728 if (inode->i_nlink > 2)
3729 CERROR("%s: directory "DFID" ino %lu link count is %u at unlink. run e2fsck to repair\n",
3730 osd_name(osd), PFID(fid), inode->i_ino,
3733 spin_lock(&obj->oo_guard);
3735 spin_unlock(&obj->oo_guard);
3736 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3739 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3741 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3743 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3744 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3745 oh->ot_handle, OI_CHECK_FLD);
3747 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3748 /* XXX: add to ext3 orphan list */
3749 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3751 /* not needed in the cache anymore */
3752 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3753 obj->oo_destroyed = 1;
3759 * Put the fid into lustre_mdt_attrs, and then place the structure
3760 * inode's ea. This fid should not be altered during the life time
3763 * \retval +ve, on success
3764 * \retval -ve, on error
3766 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3768 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3769 const struct lu_fid *fid, __u32 compat, __u32 incompat)
3771 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3772 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3777 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3780 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3783 lustre_loa_init(loa, fid, compat, incompat);
3784 lustre_loa_swab(loa, false);
3787 * For the OST device with 256 bytes inode size by default,
3788 * the PFID EA will be stored together with LMA EA to avoid
3789 * performance trouble. Otherwise the PFID EA can be stored
3790 * independently. LU-8998
3792 if ((compat & LMAC_FID_ON_OST) &&
3793 LDISKFS_INODE_SIZE(inode->i_sb) <= 256)
3794 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3795 sizeof(*loa), XATTR_CREATE);
3797 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3798 sizeof(*lma), XATTR_CREATE);
3800 * LMA may already exist, but we need to check that all the
3801 * desired compat/incompat flags have been added.
3803 if (unlikely(rc == -EEXIST)) {
3804 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3805 XATTR_NAME_LMA, (void *)loa, sizeof(*loa));
3809 if (rc < sizeof(*lma))
3812 lustre_loa_swab(loa, true);
3813 if (lu_fid_eq(fid, &lma->lma_self_fid) &&
3814 ((compat == 0 && incompat == 0) ||
3815 (!(~lma->lma_compat & compat) &&
3816 !(~lma->lma_incompat & incompat))))
3819 lma->lma_self_fid = *fid;
3820 lma->lma_compat |= compat;
3821 lma->lma_incompat |= incompat;
3822 if (rc == sizeof(*lma)) {
3823 lustre_lma_swab(lma);
3824 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3825 sizeof(*lma), XATTR_REPLACE);
3827 lustre_loa_swab(loa, false);
3828 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3829 sizeof(*loa), XATTR_REPLACE);
3837 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3838 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3839 * To have compatilibility with 1.8 ldiskfs driver we need to have
3840 * magic number at start of fid data.
3841 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3844 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3845 const struct lu_fid *fid)
3847 if (!fid_is_namespace_visible(fid) ||
3848 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3849 param->edp_magic = 0;
3853 param->edp_magic = LDISKFS_LUFID_MAGIC;
3854 param->edp_len = sizeof(struct lu_fid) + 1;
3855 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3859 * Try to read the fid from inode ea into dt_rec.
3861 * \param fid object fid.
3863 * \retval 0 on success
3865 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3866 __u32 ino, struct lu_fid *fid,
3867 struct osd_inode_id *id)
3869 struct osd_thread_info *info = osd_oti_get(env);
3870 struct inode *inode;
3874 osd_id_gen(id, ino, OSD_OII_NOGEN);
3875 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3877 RETURN(PTR_ERR(inode));
3883 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3885 struct inode *parent_dir,
3886 const struct lu_fid *dot_fid,
3887 const struct lu_fid *dot_dot_fid,
3888 struct osd_thandle *oth)
3890 struct ldiskfs_dentry_param *dot_ldp;
3891 struct ldiskfs_dentry_param *dot_dot_ldp;
3892 __u32 saved_nlink = dir->i_nlink;
3895 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3896 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3898 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3899 dot_ldp->edp_magic = 0;
3901 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3902 dir, dot_ldp, dot_dot_ldp);
3904 * The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3905 * the subseqent ref_add() will increase the dir->i_nlink
3906 * as 3. That is incorrect for new created directory.
3908 * It looks like hack, because we want to make the OSD API
3909 * to be order-independent for new created directory object
3910 * between dt_insert(..) and ref_add() operations.
3912 * Here, we only restore the in-RAM dir-inode's nlink attr,
3913 * becuase if the nlink attr is not 2, then there will be
3914 * ref_add() called following the dt_insert(..), such call
3915 * will make both the in-RAM and on-disk dir-inode's nlink
3916 * attr to be set as 2. LU-7447
3918 set_nlink(dir, saved_nlink);
3923 * Create an local agent inode for remote entry
3925 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3926 struct osd_device *osd,
3927 struct osd_object *pobj,
3928 const struct lu_fid *fid,
3932 struct osd_thread_info *info = osd_oti_get(env);
3933 struct inode *local;
3934 struct osd_thandle *oh;
3935 uid_t own[2] = {0, 0};
3941 oh = container_of(th, struct osd_thandle, ot_super);
3942 LASSERT(oh->ot_handle->h_transaction != NULL);
3944 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type, own);
3945 if (IS_ERR(local)) {
3946 CERROR("%s: create local error %d\n", osd_name(osd),
3947 (int)PTR_ERR(local));
3952 * restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
3953 * correct gid on remote file, not agent here
3955 local->i_gid = current_fsgid();
3956 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3958 /* e2fsck doesn't like empty symlinks. Store remote FID as symlink.
3959 * That gives e2fsck something to look at and be happy, and allows
3960 * debugging if we need to determine where this symlink came from.
3962 if (S_ISLNK(type)) {
3963 BUILD_BUG_ON(LDISKFS_N_BLOCKS * 4 < FID_LEN + 1);
3964 rc = snprintf((char *)LDISKFS_I(local)->i_data,
3965 LDISKFS_N_BLOCKS * 4, DFID, PFID(fid));
3967 i_size_write(local, rc);
3968 LDISKFS_I(local)->i_disksize = rc;
3970 unlock_new_inode(local);
3972 /* Agent inode should not have project ID */
3973 #ifdef HAVE_PROJECT_QUOTA
3974 if (LDISKFS_I(pobj->oo_inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
3975 i_projid_read(pobj->oo_inode) != 0) {
3976 rc = osd_transfer_project(local, 0, th);
3978 CERROR("%s: quota transfer failed:. Is project quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3979 osd_ino2name(local), rc);
3980 RETURN(ERR_PTR(rc));
3984 /* Set special LMA flag for local agent inode */
3985 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3987 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3988 osd_name(osd), PFID(fid), rc);
3989 RETURN(ERR_PTR(rc));
3995 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3996 lu_object_fid(&pobj->oo_dt.do_lu),
3999 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
4000 osd_name(osd), PFID(fid), rc);
4001 RETURN(ERR_PTR(rc));
4008 * when direntry is deleted, we have to take care of possible agent inode
4009 * referenced by that. unfortunately we can't do this at that point:
4010 * iget() within a running transaction leads to deadlock and we better do
4011 * not call that every delete declaration to save performance. so we put
4012 * a potention agent inode on a list and process that once the transaction
4013 * is over. Notice it's not any worse in terms of real orphans as regular
4014 * object destroy doesn't put inodes on the on-disk orphan list. this should
4015 * be addressed separately
4017 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
4018 struct osd_thandle *oh,
4021 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
4022 struct osd_obj_orphan *oor;
4029 oor->oor_env = (struct lu_env *)env;
4030 spin_lock(&osd->od_osfs_lock);
4031 list_add(&oor->oor_list, &osd->od_orphan_list);
4032 spin_unlock(&osd->od_osfs_lock);
4034 oh->ot_remove_agents = 1;
4040 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
4041 struct osd_device *osd)
4043 struct osd_thread_info *info = osd_oti_get(env);
4044 struct osd_obj_orphan *oor, *tmp;
4045 struct osd_inode_id id;
4047 struct inode *inode;
4052 spin_lock(&osd->od_osfs_lock);
4053 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
4054 if (oor->oor_env == env)
4055 list_move(&oor->oor_list, &list);
4057 spin_unlock(&osd->od_osfs_lock);
4059 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
4063 list_del(&oor->oor_list);
4066 osd_id_gen(&id, ino, OSD_OII_NOGEN);
4067 inode = osd_iget_fid(info, osd, &id, &fid);
4071 if (!osd_remote_fid(env, osd, &fid)) {
4076 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
4078 mark_inode_dirty(inode);
4079 ldiskfs_journal_stop(jh);
4087 * OSD layer object create function for OST objects (b=11826).
4089 * The FID is inserted into inode xattr here.
4091 * \retval 0, on success
4092 * \retval -ve, on error
4094 static int osd_create(const struct lu_env *env, struct dt_object *dt,
4095 struct lu_attr *attr, struct dt_allocation_hint *hint,
4096 struct dt_object_format *dof, struct thandle *th)
4098 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
4099 struct osd_object *obj = osd_dt_obj(dt);
4100 struct osd_thread_info *info = osd_oti_get(env);
4101 int result, on_ost = 0;
4105 if (dt_object_exists(dt))
4108 LINVRNT(osd_invariant(obj));
4109 LASSERT(!dt_object_remote(dt));
4110 LASSERT(osd_is_write_locked(env, obj));
4111 LASSERT(th != NULL);
4113 if (unlikely(fid_is_acct(fid)))
4115 * Quota files can't be created from the kernel any more,
4116 * 'tune2fs -O quota' will take care of creating them
4120 result = __osd_create(info, obj, attr, hint, dof, th);
4122 if (fid_is_idif(fid) &&
4123 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
4124 struct lu_fid *tfid = &info->oti_fid;
4125 struct ost_id *oi = &info->oti_ostid;
4127 fid_to_ostid(fid, oi);
4128 ostid_to_fid(tfid, oi, 0);
4130 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
4131 LMAC_FID_ON_OST, 0);
4133 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
4135 result = osd_ea_fid_set(info, obj->oo_inode, fid,
4136 on_ost ? LMAC_FID_ON_OST : 0,
4139 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
4140 obj->oo_dt.do_body_ops = &osd_body_ops;
4143 if (!result && !CFS_FAIL_CHECK(OBD_FAIL_OSD_NO_OI_ENTRY))
4144 result = __osd_oi_insert(env, obj, fid, th);
4147 * a small optimization - dt_insert() isn't usually applied
4148 * to OST objects, so we don't need to cache OI mapping for
4151 if (result == 0 && on_ost == 0) {
4152 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4154 result = osd_idc_find_and_init(env, osd, obj);
4155 LASSERT(result == 0);
4158 LASSERT(ergo(result == 0,
4159 dt_object_exists(dt) && !dt_object_remote(dt)));
4160 LINVRNT(osd_invariant(obj));
4164 static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
4165 struct thandle *handle)
4167 struct osd_thandle *oh;
4170 /* it's possible that object doesn't exist yet */
4171 LASSERT(handle != NULL);
4173 oh = container_of(handle, struct osd_thandle, ot_super);
4174 LASSERT(oh->ot_handle == NULL);
4176 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
4177 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
4179 rc = osd_idc_find_and_init(env, osd_dev(dt->do_lu.lo_dev),
4186 * Concurrency: @dt is write locked.
4188 static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
4191 struct osd_object *obj = osd_dt_obj(dt);
4192 struct inode *inode = obj->oo_inode;
4193 struct osd_thandle *oh;
4196 if (!dt_object_exists(dt) || obj->oo_destroyed)
4199 LINVRNT(osd_invariant(obj));
4200 LASSERT(!dt_object_remote(dt));
4201 LASSERT(osd_is_write_locked(env, obj));
4202 LASSERT(th != NULL);
4204 oh = container_of(th, struct osd_thandle, ot_super);
4205 LASSERT(oh->ot_handle != NULL);
4207 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
4209 CDEBUG(D_INODE, DFID" increase nlink %d\n",
4210 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
4212 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
4213 * (65000) subdirectories by storing "1" in i_nlink if the link count
4214 * would otherwise overflow. Directory tranversal tools understand
4215 * that (st_nlink == 1) indicates that the filesystem dose not track
4216 * hard links count on the directory, and will not abort subdirectory
4217 * scanning early once (st_nlink - 2) subdirs have been found.
4219 * This also has to properly handle the case of inodes with nlink == 0
4220 * in case they are being linked into the PENDING directory
4222 spin_lock(&obj->oo_guard);
4223 if (unlikely(inode->i_nlink == 0))
4224 /* inc_nlink from 0 may cause WARN_ON */
4225 set_nlink(inode, 1);
4227 ldiskfs_inc_count(oh->ot_handle, inode);
4228 if (!S_ISDIR(inode->i_mode))
4229 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
4231 spin_unlock(&obj->oo_guard);
4233 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4234 LINVRNT(osd_invariant(obj));
4236 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
4241 static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
4242 struct thandle *handle)
4244 struct osd_thandle *oh;
4246 if (!dt_object_exists(dt))
4249 LASSERT(!dt_object_remote(dt));
4250 LASSERT(handle != NULL);
4252 oh = container_of(handle, struct osd_thandle, ot_super);
4253 LASSERT(oh->ot_handle == NULL);
4255 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
4256 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
4262 * Concurrency: @dt is write locked.
4264 static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
4267 struct osd_object *obj = osd_dt_obj(dt);
4268 struct inode *inode = obj->oo_inode;
4269 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4270 struct osd_thandle *oh;
4272 if (!dt_object_exists(dt))
4275 LINVRNT(osd_invariant(obj));
4276 LASSERT(!dt_object_remote(dt));
4277 LASSERT(osd_is_write_locked(env, obj));
4278 LASSERT(th != NULL);
4280 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_REF_DEL))
4283 oh = container_of(th, struct osd_thandle, ot_super);
4284 LASSERT(oh->ot_handle != NULL);
4286 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
4288 spin_lock(&obj->oo_guard);
4290 * That can be result of upgrade from old Lustre version and
4291 * applied only to local files. Just skip this ref_del call.
4292 * ext4_unlink() only treats this as a warning, don't LASSERT here.
4294 if (inode->i_nlink == 0) {
4295 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
4296 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
4297 ", maybe an upgraded file? (LU-3915)\n",
4298 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
4299 spin_unlock(&obj->oo_guard);
4303 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
4304 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
4306 ldiskfs_dec_count(oh->ot_handle, inode);
4307 spin_unlock(&obj->oo_guard);
4309 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4310 LINVRNT(osd_invariant(obj));
4312 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
4318 * Concurrency: @dt is read locked.
4320 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
4321 struct lu_buf *buf, const char *name)
4323 struct osd_object *obj = osd_dt_obj(dt);
4324 struct inode *inode = obj->oo_inode;
4325 struct osd_thread_info *info = osd_oti_get(env);
4326 struct dentry *dentry = &info->oti_obj_dentry;
4327 bool cache_xattr = false;
4332 /* version get is not real XATTR but uses xattr API */
4333 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4334 dt_obj_version_t *ver = buf->lb_buf;
4337 * for version we are just using xattr API but change inode
4340 if (buf->lb_len == 0)
4341 return sizeof(dt_obj_version_t);
4343 if (buf->lb_len < sizeof(dt_obj_version_t))
4346 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
4347 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4349 *ver = LDISKFS_I(inode)->i_fs_version;
4351 return sizeof(dt_obj_version_t);
4354 if (!dt_object_exists(dt))
4357 LASSERT(!dt_object_remote(dt));
4358 LASSERT(inode->i_op != NULL);
4359 #ifdef HAVE_IOP_XATTR
4360 LASSERT(inode->i_op->getxattr != NULL);
4363 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
4364 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
4368 rc = osd_oxc_get(obj, name, buf);
4373 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
4374 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4375 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4376 struct filter_fid *ff;
4377 struct ost_layout *ol;
4379 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4383 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
4386 if (buf->lb_len == 0 || !buf->lb_buf)
4389 if (buf->lb_len < rc)
4393 ol = &ff->ff_layout;
4394 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
4395 PFID_STRIPE_IDX_BITS);
4396 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
4397 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
4398 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
4399 if (lma->lma_compat & LMAC_COMP_INFO) {
4400 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
4401 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
4402 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
4404 ol->ol_comp_start = 0;
4405 ol->ol_comp_end = 0;
4409 rc = __osd_xattr_get(inode, dentry, name,
4410 buf->lb_buf, buf->lb_len);
4414 if (rc == -ENOENT || rc == -ENODATA)
4415 osd_oxc_add(obj, name, NULL, 0);
4416 else if (rc > 0 && buf->lb_buf != NULL)
4417 osd_oxc_add(obj, name, buf->lb_buf, rc);
4423 static int osd_declare_xattr_set(const struct lu_env *env,
4424 struct dt_object *dt,
4425 const struct lu_buf *buf, const char *name,
4426 int fl, struct thandle *handle)
4428 struct osd_thandle *oh;
4430 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4432 LASSERT(handle != NULL);
4434 oh = container_of(handle, struct osd_thandle, ot_super);
4435 LASSERT(oh->ot_handle == NULL);
4437 if (strcmp(name, XATTR_NAME_LMA) == 0) {
4439 * For non-upgrading case, the LMA is set first and
4440 * usually fit inode. But for upgrade case, the LMA
4441 * may be in another separated EA block.
4443 if (dt_object_exists(dt)) {
4444 if (fl == LU_XATTR_REPLACE)
4449 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4451 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4452 /* We may need to delete the old PFID EA. */
4453 credits = LDISKFS_MAXQUOTAS_DEL_BLOCKS(sb);
4454 if (fl == LU_XATTR_REPLACE)
4460 * If some name entry resides on remote MDT, then will create
4461 * agent entry under remote parent. On the other hand, if the
4462 * remote entry will be removed, then related agent entry may
4463 * need to be removed from the remote parent. So there may be
4464 * kinds of cases, let's declare enough credits. The credits
4465 * for create agent entry is enough for remove case.
4467 if (strcmp(name, XATTR_NAME_LINK) == 0) {
4468 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4469 if (dt_object_exists(dt))
4470 credits += 1; /* For updating LMA */
4474 credits += osd_dto_credits_noquota[DTO_XATTR_SET];
4479 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
4481 * learn xattr size from osd_xattr_get if
4482 * attribute has not been read yet
4484 buflen = __osd_xattr_get(
4485 osd_dt_obj(dt)->oo_inode,
4486 &osd_oti_get(env)->oti_obj_dentry,
4491 buflen = buf->lb_len;
4494 if (buflen > sb->s_blocksize) {
4495 credits += osd_calc_bkmap_credits(
4497 (buflen + sb->s_blocksize - 1) >>
4498 sb->s_blocksize_bits);
4502 * xattr set may involve inode quota change, reserve credits for
4503 * dquot_initialize()
4505 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4508 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
4513 static int osd_xattr_set_pfid(const struct lu_env *env, struct osd_object *obj,
4514 const struct lu_buf *buf, int fl,
4515 struct thandle *handle)
4517 struct osd_thread_info *info = osd_oti_get(env);
4518 struct dentry *dentry = &info->oti_obj_dentry;
4519 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4520 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4521 struct inode *inode = obj->oo_inode;
4522 struct filter_fid *ff = buf->lb_buf;
4523 struct ost_layout *ol = &ff->ff_layout;
4524 int flags = XATTR_REPLACE;
4529 if (buf->lb_len != sizeof(*ff) && buf->lb_len != sizeof(struct lu_fid))
4532 rc = osd_get_lma(info, inode, dentry, loa);
4533 if (rc == -ENODATA) {
4534 /* Usually for upgarding from old device */
4535 lustre_loa_init(loa, lu_object_fid(&obj->oo_dt.do_lu),
4536 LMAC_FID_ON_OST, 0);
4537 flags = XATTR_CREATE;
4542 if (!rc && lma->lma_compat & LMAC_STRIPE_INFO) {
4543 if ((fl & LU_XATTR_CREATE) && !(fl & LU_XATTR_REPLACE))
4546 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256) {
4547 /* Separate PFID EA from LMA */
4548 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4549 lustre_lma_swab(lma);
4550 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4551 sizeof(*lma), XATTR_REPLACE);
4553 obj->oo_pfid_in_lma = 0;
4554 rc = LU_XATTR_CREATE;
4560 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256)
4564 * Old client does not send stripe information,
4565 * then store the PFID EA on disk separatedly.
4567 if (unlikely(buf->lb_len == sizeof(struct lu_fid) ||
4568 ol->ol_stripe_size == 0))
4571 /* Remove old PFID EA entry firstly. */
4572 dquot_initialize(inode);
4573 rc = ll_vfs_removexattr(dentry, inode, XATTR_NAME_FID);
4574 if (rc == -ENODATA) {
4575 if ((fl & LU_XATTR_REPLACE) && !(fl & LU_XATTR_CREATE))
4582 fid_le_to_cpu(&loa->loa_parent_fid, &ff->ff_parent);
4583 if (likely(ol->ol_stripe_size != 0)) {
4584 loa->loa_parent_fid.f_ver |= le32_to_cpu(ol->ol_stripe_count) <<
4585 PFID_STRIPE_IDX_BITS;
4586 loa->loa_stripe_size = le32_to_cpu(ol->ol_stripe_size);
4587 lma->lma_compat |= LMAC_STRIPE_INFO;
4588 if (ol->ol_comp_id != 0) {
4589 loa->loa_comp_id = le32_to_cpu(ol->ol_comp_id);
4590 loa->loa_comp_start = le64_to_cpu(ol->ol_comp_start);
4591 loa->loa_comp_end = le64_to_cpu(ol->ol_comp_end);
4592 lma->lma_compat |= LMAC_COMP_INFO;
4596 lustre_loa_swab(loa, false);
4598 /* Store the PFID EA inside LMA. */
4599 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa, sizeof(*loa),
4602 obj->oo_pfid_in_lma = 1;
4608 * In DNE environment, the object (in spite of regular file or directory)
4609 * and its name entry may reside on different MDTs. Under such case, we will
4610 * create an agent entry on the MDT where the object resides. The agent entry
4611 * references the object locally, that makes the object to be visible to the
4612 * userspace when mounted as 'ldiskfs' directly. Then the userspace tools,
4613 * such as 'tar' can handle the object properly.
4615 * We handle the agent entry during set linkEA that is the common interface
4616 * for both regular file and directroy, can handle kinds of cases, such as
4617 * create/link/unlink/rename, and so on.
4619 * NOTE: we can NOT do that when ea_{insert,delete} that is only for directory.
4621 * XXX: There are two known issues:
4622 * 1. For one object, we will create at most one agent entry even if there
4623 * may be more than one cross-MDTs hard links on the object. So the local
4624 * e2fsck may claim that the object's nlink is larger than the name entries
4625 * that reference such inode. And in further, the e2fsck will fix the nlink
4626 * attribute to match the local references. Then it will cause the object's
4627 * nlink attribute to be inconsistent with the global references. it is bad
4628 * but not fatal. The ref_del() can handle the zero-referenced case. On the
4629 * other hand, the global namespace LFSCK can repair the object's attribute
4630 * according to the linkEA.
4631 * 2. There may be too many hard links on the object as to its linkEA overflow,
4632 * then the linkEA entry for cross-MDTs reference may be discarded. If such
4633 * case happened, then at this point, we do not know whether there are some
4634 * cross-MDTs reference. But there are local references, it guarantees that
4635 * object is visible to userspace when mounted as 'ldiskfs'. That is enough.
4637 static int osd_xattr_handle_linkea(const struct lu_env *env,
4638 struct osd_device *osd,
4639 struct osd_object *obj,
4640 const struct lu_buf *buf,
4641 struct thandle *handle)
4643 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
4644 struct lu_fid *tfid = &osd_oti_get(env)->oti_fid3;
4645 struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
4646 struct lu_name tmpname;
4647 struct osd_thandle *oh;
4649 bool remote = false;
4653 oh = container_of(handle, struct osd_thandle, ot_super);
4654 LASSERT(oh->ot_handle != NULL);
4656 rc = linkea_init_with_rec(&ldata);
4658 linkea_first_entry(&ldata);
4659 while (ldata.ld_lee != NULL && !remote) {
4660 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
4662 if (osd_remote_fid(env, osd, tfid) > 0)
4665 linkea_next_entry(&ldata);
4667 } else if (rc == -ENODATA) {
4673 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
4674 rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
4676 CERROR("%s: failed to remove agent entry for "DFID
4677 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4678 } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
4679 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4681 CERROR("%s: failed to create agent entry for "DFID
4682 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4689 * Concurrency: @dt is write locked.
4691 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
4692 const struct lu_buf *buf, const char *name, int fl,
4693 struct thandle *handle)
4695 struct osd_object *obj = osd_dt_obj(dt);
4696 struct osd_device *osd = osd_obj2dev(obj);
4697 struct inode *inode = obj->oo_inode;
4698 struct osd_thread_info *info = osd_oti_get(env);
4708 /* version set is not real XATTR */
4709 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4710 dt_obj_version_t *version = buf->lb_buf;
4713 * for version we are just using xattr API but change inode
4716 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
4718 CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
4719 *version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4721 LDISKFS_I(inode)->i_fs_version = *version;
4723 * Version is set after all inode operations are finished,
4724 * so we should mark it dirty here
4726 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4731 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
4732 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
4735 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4738 * For the OST device with 256 bytes inode size by default,
4739 * the PFID EA will be stored together with LMA EA to avoid
4740 * performance trouble. Otherwise the PFID EA can be stored
4741 * independently. LU-8998
4743 if (strcmp(name, XATTR_NAME_FID) == 0 && osd->od_is_ost &&
4744 (LDISKFS_INODE_SIZE(inode->i_sb) <= 256 || obj->oo_pfid_in_lma)) {
4745 LASSERT(buf->lb_buf);
4747 fl = osd_xattr_set_pfid(env, obj, buf, fl, handle);
4750 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
4751 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4752 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4754 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4758 lma->lma_incompat |= LMAI_STRIPED;
4759 lustre_lma_swab(lma);
4760 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4761 sizeof(*lma), XATTR_REPLACE);
4764 } else if (strcmp(name, XATTR_NAME_LINK) == 0) {
4765 LASSERT(!osd->od_is_ost);
4767 rc = osd_xattr_handle_linkea(env, osd, obj, buf, handle);
4772 if (fl & LU_XATTR_REPLACE)
4773 fs_flags |= XATTR_REPLACE;
4775 if (fl & LU_XATTR_CREATE)
4776 fs_flags |= XATTR_CREATE;
4778 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, len, fs_flags);
4779 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4782 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4783 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4784 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
4790 * Concurrency: @dt is read locked.
4792 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
4793 const struct lu_buf *buf)
4795 struct osd_object *obj = osd_dt_obj(dt);
4796 struct inode *inode = obj->oo_inode;
4797 struct osd_thread_info *info = osd_oti_get(env);
4798 struct dentry *dentry = &info->oti_obj_dentry;
4800 if (!dt_object_exists(dt))
4803 LASSERT(!dt_object_remote(dt));
4804 LASSERT(inode->i_op != NULL);
4805 LASSERT(inode->i_op->listxattr != NULL);
4807 dentry->d_inode = inode;
4808 dentry->d_sb = inode->i_sb;
4809 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
4812 static int osd_declare_xattr_del(const struct lu_env *env,
4813 struct dt_object *dt, const char *name,
4814 struct thandle *handle)
4816 struct osd_thandle *oh;
4817 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4819 LASSERT(!dt_object_remote(dt));
4820 LASSERT(handle != NULL);
4822 oh = container_of(handle, struct osd_thandle, ot_super);
4823 LASSERT(oh->ot_handle == NULL);
4825 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
4826 osd_dto_credits_noquota[DTO_XATTR_SET]);
4828 * xattr del may involve inode quota change, reserve credits for
4829 * dquot_initialize()
4831 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4837 * Concurrency: @dt is write locked.
4839 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
4840 const char *name, struct thandle *handle)
4842 struct osd_object *obj = osd_dt_obj(dt);
4843 struct inode *inode = obj->oo_inode;
4844 struct osd_thread_info *info = osd_oti_get(env);
4845 struct dentry *dentry = &info->oti_obj_dentry;
4848 if (!dt_object_exists(dt))
4851 LASSERT(!dt_object_remote(dt));
4852 LASSERT(inode->i_op != NULL);
4853 LASSERT(handle != NULL);
4854 #ifdef HAVE_IOP_XATTR
4855 LASSERT(inode->i_op->removexattr != NULL);
4858 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4860 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
4861 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
4863 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
4864 &info->oti_ost_attrs);
4866 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
4868 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4869 lustre_lma_swab(lma);
4870 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4871 sizeof(*lma), XATTR_REPLACE);
4873 obj->oo_pfid_in_lma = 0;
4876 dquot_initialize(inode);
4877 dentry->d_inode = inode;
4878 dentry->d_sb = inode->i_sb;
4879 rc = ll_vfs_removexattr(dentry, inode, name);
4882 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4885 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4886 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4887 osd_oxc_del(obj, name);
4892 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
4893 __u64 start, __u64 end)
4895 struct osd_object *obj = osd_dt_obj(dt);
4896 struct inode *inode = obj->oo_inode;
4897 struct file *file = osd_quasi_file(env, inode);
4902 rc = vfs_fsync_range(file, start, end, 0);
4907 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4916 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4917 const struct dt_index_features *feat)
4919 struct iam_descr *descr;
4921 if (osd_object_is_root(o))
4922 return feat == &dt_directory_features;
4924 LASSERT(o->oo_dir != NULL);
4926 descr = o->oo_dir->od_container.ic_descr;
4927 if (feat == &dt_directory_features) {
4928 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4933 return feat->dif_keysize_min <= descr->id_key_size &&
4934 descr->id_key_size <= feat->dif_keysize_max &&
4935 feat->dif_recsize_min <= descr->id_rec_size &&
4936 descr->id_rec_size <= feat->dif_recsize_max &&
4937 !(feat->dif_flags & (DT_IND_VARKEY |
4938 DT_IND_VARREC | DT_IND_NONUNQ)) &&
4939 ergo(feat->dif_flags & DT_IND_UPDATE,
4940 1 /* XXX check that object (and fs) is writable */);
4944 static int osd_iam_container_init(const struct lu_env *env,
4945 struct osd_object *obj,
4946 struct osd_directory *dir)
4948 struct iam_container *bag = &dir->od_container;
4951 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
4955 result = iam_container_setup(bag);
4957 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
4959 iam_container_fini(bag);
4966 * Concurrency: no external locking is necessary.
4968 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
4969 const struct dt_index_features *feat)
4973 struct osd_object *obj = osd_dt_obj(dt);
4975 LINVRNT(osd_invariant(obj));
4977 if (osd_object_is_root(obj)) {
4978 dt->do_index_ops = &osd_index_ea_ops;
4980 } else if (feat == &dt_directory_features) {
4981 dt->do_index_ops = &osd_index_ea_ops;
4982 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
4987 } else if (unlikely(feat == &dt_otable_features)) {
4988 dt->do_index_ops = &osd_otable_ops;
4990 } else if (unlikely(feat == &dt_acct_features)) {
4991 dt->do_index_ops = &osd_acct_index_ops;
4994 } else if (!osd_has_index(obj)) {
4995 struct osd_directory *dir;
4996 struct osd_device *osd = osd_obj2dev(obj);
4997 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
5002 spin_lock(&obj->oo_guard);
5003 if (obj->oo_dir == NULL)
5007 * Concurrent thread allocated container data.
5010 spin_unlock(&obj->oo_guard);
5012 * Now, that we have container data, serialize its
5015 down_write(&obj->oo_ext_idx_sem);
5017 * recheck under lock.
5020 if (osd_has_index(obj)) {
5025 result = osd_iam_container_init(env, obj, obj->oo_dir);
5026 if (result || feat == &dt_lfsck_namespace_features ||
5027 feat == &dt_lfsck_layout_orphan_features ||
5028 feat == &dt_lfsck_layout_dangling_features)
5031 result = osd_index_register(osd, fid,
5032 feat->dif_keysize_max,
5033 feat->dif_recsize_max);
5035 CWARN("%s: failed to register index "
5037 osd_name(osd), PFID(fid), result);
5038 else if (result > 0)
5041 CDEBUG(D_LFSCK, "%s: index object "DFID
5042 " (%d/%d) registered\n",
5043 osd_name(osd), PFID(fid),
5044 (int)feat->dif_keysize_max,
5045 (int)feat->dif_recsize_max);
5048 up_write(&obj->oo_ext_idx_sem);
5056 if (result == 0 && skip_iam == 0) {
5057 if (!osd_iam_index_probe(env, obj, feat))
5060 LINVRNT(osd_invariant(obj));
5065 static int osd_otable_it_attr_get(const struct lu_env *env,
5066 struct dt_object *dt,
5067 struct lu_attr *attr)
5073 static const struct dt_object_operations osd_obj_ops = {
5074 .do_read_lock = osd_read_lock,
5075 .do_write_lock = osd_write_lock,
5076 .do_read_unlock = osd_read_unlock,
5077 .do_write_unlock = osd_write_unlock,
5078 .do_write_locked = osd_write_locked,
5079 .do_attr_get = osd_attr_get,
5080 .do_declare_attr_set = osd_declare_attr_set,
5081 .do_attr_set = osd_attr_set,
5082 .do_ah_init = osd_ah_init,
5083 .do_declare_create = osd_declare_create,
5084 .do_create = osd_create,
5085 .do_declare_destroy = osd_declare_destroy,
5086 .do_destroy = osd_destroy,
5087 .do_index_try = osd_index_try,
5088 .do_declare_ref_add = osd_declare_ref_add,
5089 .do_ref_add = osd_ref_add,
5090 .do_declare_ref_del = osd_declare_ref_del,
5091 .do_ref_del = osd_ref_del,
5092 .do_xattr_get = osd_xattr_get,
5093 .do_declare_xattr_set = osd_declare_xattr_set,
5094 .do_xattr_set = osd_xattr_set,
5095 .do_declare_xattr_del = osd_declare_xattr_del,
5096 .do_xattr_del = osd_xattr_del,
5097 .do_xattr_list = osd_xattr_list,
5098 .do_object_sync = osd_object_sync,
5099 .do_invalidate = osd_invalidate,
5102 static const struct dt_object_operations osd_obj_otable_it_ops = {
5103 .do_attr_get = osd_otable_it_attr_get,
5104 .do_index_try = osd_index_try,
5107 static int osd_index_declare_iam_delete(const struct lu_env *env,
5108 struct dt_object *dt,
5109 const struct dt_key *key,
5110 struct thandle *handle)
5112 struct osd_thandle *oh;
5114 oh = container_of(handle, struct osd_thandle, ot_super);
5115 LASSERT(oh->ot_handle == NULL);
5117 /* Recycle may cause additional three blocks to be changed. */
5118 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
5119 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
5125 * delete a (key, value) pair from index \a dt specified by \a key
5127 * \param dt osd index object
5128 * \param key key for index
5129 * \param rec record reference
5130 * \param handle transaction handler
5133 * \retval -ve failure
5135 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
5136 const struct dt_key *key,
5137 struct thandle *handle)
5139 struct osd_thread_info *oti = osd_oti_get(env);
5140 struct osd_object *obj = osd_dt_obj(dt);
5141 struct osd_thandle *oh;
5142 struct iam_path_descr *ipd;
5143 struct iam_container *bag = &obj->oo_dir->od_container;
5148 if (!dt_object_exists(dt))
5151 LINVRNT(osd_invariant(obj));
5152 LASSERT(!dt_object_remote(dt));
5153 LASSERT(bag->ic_object == obj->oo_inode);
5154 LASSERT(handle != NULL);
5156 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
5158 ipd = osd_idx_ipd_get(env, bag);
5159 if (unlikely(ipd == NULL))
5162 oh = container_of(handle, struct osd_thandle, ot_super);
5163 LASSERT(oh->ot_handle != NULL);
5164 LASSERT(oh->ot_handle->h_transaction != NULL);
5166 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5167 /* swab quota uid/gid provided by caller */
5168 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5169 key = (const struct dt_key *)&oti->oti_quota_id;
5172 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
5173 osd_ipd_put(env, bag, ipd);
5174 LINVRNT(osd_invariant(obj));
5175 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
5179 static int osd_index_declare_ea_delete(const struct lu_env *env,
5180 struct dt_object *dt,
5181 const struct dt_key *key,
5182 struct thandle *handle)
5184 struct osd_thandle *oh;
5185 struct inode *inode;
5190 LASSERT(!dt_object_remote(dt));
5191 LASSERT(handle != NULL);
5193 oh = container_of(handle, struct osd_thandle, ot_super);
5194 LASSERT(oh->ot_handle == NULL);
5196 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
5197 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
5199 inode = osd_dt_obj(dt)->oo_inode;
5203 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
5204 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
5209 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
5212 struct osd_fid_pack *rec;
5215 if (de->file_type & LDISKFS_DIRENT_LUFID) {
5216 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
5217 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
5218 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
5224 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
5225 const struct lu_fid *fid)
5227 struct seq_server_site *ss = osd_seq_site(osd);
5231 /* FID seqs not in FLDB, must be local seq */
5232 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
5236 * If FLD is not being initialized yet, it only happens during the
5237 * initialization, likely during mgs initialization, and we assume
5238 * this is local FID.
5240 if (ss == NULL || ss->ss_server_fld == NULL)
5243 /* Only check the local FLDB here */
5244 if (osd_seq_exists(env, osd, fid_seq(fid)))
5250 static void osd_take_care_of_agent(const struct lu_env *env,
5251 struct osd_device *osd,
5252 struct osd_thandle *oh,
5253 struct ldiskfs_dir_entry_2 *de)
5255 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
5256 struct osd_idmap_cache *idc;
5257 int rc, schedule = 0;
5259 LASSERT(de != NULL);
5261 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
5262 if (likely(rc == 0)) {
5263 idc = osd_idc_find_or_init(env, osd, fid);
5264 if (IS_ERR(idc) || idc->oic_remote)
5266 } else if (rc == -ENODATA) {
5268 * can't get FID, postpone to the end of the
5269 * transaction when iget() is safe
5273 CERROR("%s: can't get FID: rc = %d\n", osd_name(osd), rc);
5276 osd_schedule_agent_inode_removal(env, oh,
5277 le32_to_cpu(de->inode));
5281 * Index delete function for interoperability mode (b11826).
5282 * It will remove the directory entry added by osd_index_ea_insert().
5283 * This entry is needed to maintain name->fid mapping.
5285 * \param key, key i.e. file entry to be deleted
5287 * \retval 0, on success
5288 * \retval -ve, on error
5290 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
5291 const struct dt_key *key, struct thandle *handle)
5293 struct osd_object *obj = osd_dt_obj(dt);
5294 struct inode *dir = obj->oo_inode;
5295 struct dentry *dentry;
5296 struct osd_thandle *oh;
5297 struct ldiskfs_dir_entry_2 *de = NULL;
5298 struct buffer_head *bh;
5299 struct htree_lock *hlock = NULL;
5300 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5305 if (!dt_object_exists(dt))
5308 LINVRNT(osd_invariant(obj));
5309 LASSERT(!dt_object_remote(dt));
5310 LASSERT(handle != NULL);
5312 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
5314 oh = container_of(handle, struct osd_thandle, ot_super);
5315 LASSERT(oh->ot_handle != NULL);
5316 LASSERT(oh->ot_handle->h_transaction != NULL);
5318 dquot_initialize(dir);
5319 dentry = osd_child_dentry_get(env, obj,
5320 (char *)key, strlen((char *)key));
5322 if (obj->oo_hl_head != NULL) {
5323 hlock = osd_oti_get(env)->oti_hlock;
5324 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5325 dir, LDISKFS_HLOCK_DEL);
5327 down_write(&obj->oo_ext_idx_sem);
5330 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5333 * If this is not the ".." entry, it might be a remote DNE
5334 * entry and we need to check if the FID is for a remote
5335 * MDT. If the FID is not in the directory entry (e.g.
5336 * upgraded 1.8 filesystem without dirdata enabled) then
5337 * we need to get the FID from the LMA. For a remote directory
5338 * there HAS to be an LMA, it cannot be an IGIF inode in this
5341 * Delete the entry before the agent inode in order to
5342 * simplify error handling. At worst an error after deleting
5343 * the entry first might leak the agent inode afterward. The
5344 * reverse would need filesystem abort in case of error deleting
5345 * the entry after the agent had been removed, or leave a
5346 * dangling entry pointing at a random inode.
5348 if (strcmp((char *)key, dotdot) != 0)
5349 osd_take_care_of_agent(env, osd, oh, de);
5350 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
5356 if (!rc && fid_is_namespace_visible(lu_object_fid(&dt->do_lu)) &&
5357 obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
5358 /* NB, dirent count may not be accurate, because it's counted
5361 if (obj->oo_dirent_count)
5362 obj->oo_dirent_count--;
5364 obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
5367 ldiskfs_htree_unlock(hlock);
5369 up_write(&obj->oo_ext_idx_sem);
5372 LASSERT(osd_invariant(obj));
5373 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
5378 * Lookup index for \a key and copy record to \a rec.
5380 * \param dt osd index object
5381 * \param key key for index
5382 * \param rec record reference
5384 * \retval +ve success : exact mach
5385 * \retval 0 return record with key not greater than \a key
5386 * \retval -ve failure
5388 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
5389 struct dt_rec *rec, const struct dt_key *key)
5391 struct osd_object *obj = osd_dt_obj(dt);
5392 struct iam_path_descr *ipd;
5393 struct iam_container *bag = &obj->oo_dir->od_container;
5394 struct osd_thread_info *oti = osd_oti_get(env);
5395 struct iam_iterator *it = &oti->oti_idx_it;
5396 struct iam_rec *iam_rec;
5401 if (!dt_object_exists(dt))
5404 LASSERT(osd_invariant(obj));
5405 LASSERT(!dt_object_remote(dt));
5406 LASSERT(bag->ic_object == obj->oo_inode);
5408 ipd = osd_idx_ipd_get(env, bag);
5412 /* got ipd now we can start iterator. */
5413 iam_it_init(it, bag, 0, ipd);
5415 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5416 /* swab quota uid/gid provided by caller */
5417 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5418 key = (const struct dt_key *)&oti->oti_quota_id;
5421 rc = iam_it_get(it, (struct iam_key *)key);
5423 if (S_ISDIR(obj->oo_inode->i_mode))
5424 iam_rec = (struct iam_rec *)oti->oti_ldp;
5426 iam_rec = (struct iam_rec *)rec;
5428 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
5430 if (S_ISDIR(obj->oo_inode->i_mode))
5431 osd_fid_unpack((struct lu_fid *)rec,
5432 (struct osd_fid_pack *)iam_rec);
5433 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
5434 osd_quota_unpack(obj, rec);
5439 osd_ipd_put(env, bag, ipd);
5441 LINVRNT(osd_invariant(obj));
5446 static int osd_index_declare_iam_insert(const struct lu_env *env,
5447 struct dt_object *dt,
5448 const struct dt_rec *rec,
5449 const struct dt_key *key,
5450 struct thandle *handle)
5452 struct osd_thandle *oh;
5454 LASSERT(handle != NULL);
5456 oh = container_of(handle, struct osd_thandle, ot_super);
5457 LASSERT(oh->ot_handle == NULL);
5459 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
5460 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
5466 * Inserts (key, value) pair in \a dt index object.
5468 * \param dt osd index object
5469 * \param key key for index
5470 * \param rec record reference
5471 * \param th transaction handler
5474 * \retval -ve failure
5476 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
5477 const struct dt_rec *rec,
5478 const struct dt_key *key, struct thandle *th)
5480 struct osd_object *obj = osd_dt_obj(dt);
5481 struct iam_path_descr *ipd;
5482 struct osd_thandle *oh;
5483 struct iam_container *bag;
5484 struct osd_thread_info *oti = osd_oti_get(env);
5485 struct iam_rec *iam_rec;
5490 if (!dt_object_exists(dt))
5493 LINVRNT(osd_invariant(obj));
5494 LASSERT(!dt_object_remote(dt));
5496 bag = &obj->oo_dir->od_container;
5497 LASSERT(bag->ic_object == obj->oo_inode);
5498 LASSERT(th != NULL);
5500 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5502 ipd = osd_idx_ipd_get(env, bag);
5503 if (unlikely(ipd == NULL))
5506 oh = container_of(th, struct osd_thandle, ot_super);
5507 LASSERT(oh->ot_handle != NULL);
5508 LASSERT(oh->ot_handle->h_transaction != NULL);
5509 if (S_ISDIR(obj->oo_inode->i_mode)) {
5510 iam_rec = (struct iam_rec *)oti->oti_ldp;
5511 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
5513 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5514 /* pack quota uid/gid */
5515 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5516 key = (const struct dt_key *)&oti->oti_quota_id;
5517 /* pack quota record */
5518 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
5519 iam_rec = (struct iam_rec *)rec;
5521 iam_rec = (struct iam_rec *)rec;
5524 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
5526 osd_ipd_put(env, bag, ipd);
5527 LINVRNT(osd_invariant(obj));
5528 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5533 * Calls ldiskfs_add_entry() to add directory entry
5534 * into the directory. This is required for
5535 * interoperability mode (b11826)
5537 * \retval 0, on success
5538 * \retval -ve, on error
5540 static int __osd_ea_add_rec(struct osd_thread_info *info,
5541 struct osd_object *pobj, struct inode *cinode,
5542 const char *name, const struct lu_fid *fid,
5543 struct htree_lock *hlock, struct thandle *th)
5545 struct ldiskfs_dentry_param *ldp;
5546 struct dentry *child;
5547 struct osd_thandle *oth;
5550 oth = container_of(th, struct osd_thandle, ot_super);
5551 LASSERT(oth->ot_handle != NULL);
5552 LASSERT(oth->ot_handle->h_transaction != NULL);
5553 LASSERT(pobj->oo_inode);
5555 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
5556 if (unlikely(pobj->oo_inode ==
5557 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
5560 osd_get_ldiskfs_dirent_param(ldp, fid);
5561 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
5562 child->d_fsdata = (void *)ldp;
5563 dquot_initialize(pobj->oo_inode);
5564 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
5565 child, cinode, hlock);
5566 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
5567 struct ldiskfs_dir_entry_2 *de;
5568 struct buffer_head *bh;
5571 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
5574 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
5577 if (S_ISDIR(cinode->i_mode))
5578 de->file_type = LDISKFS_DIRENT_LUFID |
5579 LDISKFS_FT_REG_FILE;
5581 de->file_type = LDISKFS_DIRENT_LUFID |
5583 ldiskfs_handle_dirty_metadata(oth->ot_handle,
5594 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
5595 * into the directory.Also sets flags into osd object to
5596 * indicate dot and dotdot are created. This is required for
5597 * interoperability mode (b11826)
5599 * \param dir directory for dot and dotdot fixup.
5600 * \param obj child object for linking
5602 * \retval 0, on success
5603 * \retval -ve, on error
5605 static int osd_add_dot_dotdot(struct osd_thread_info *info,
5606 struct osd_object *dir,
5607 struct inode *parent_dir, const char *name,
5608 const struct lu_fid *dot_fid,
5609 const struct lu_fid *dot_dot_fid,
5612 struct inode *inode = dir->oo_inode;
5613 struct osd_thandle *oth;
5616 oth = container_of(th, struct osd_thandle, ot_super);
5617 LASSERT(oth->ot_handle->h_transaction != NULL);
5618 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
5620 if (strcmp(name, dot) == 0) {
5621 if (dir->oo_compat_dot_created) {
5624 LASSERT(inode->i_ino == parent_dir->i_ino);
5625 dir->oo_compat_dot_created = 1;
5628 } else if (strcmp(name, dotdot) == 0) {
5629 if (!dir->oo_compat_dot_created)
5631 /* in case of rename, dotdot is already created */
5632 if (dir->oo_compat_dotdot_created) {
5633 return __osd_ea_add_rec(info, dir, parent_dir, name,
5634 dot_dot_fid, NULL, th);
5637 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
5638 struct lu_fid tfid = *dot_dot_fid;
5641 result = osd_add_dot_dotdot_internal(info,
5642 dir->oo_inode, parent_dir, dot_fid,
5645 result = osd_add_dot_dotdot_internal(info,
5646 dir->oo_inode, parent_dir, dot_fid,
5651 dir->oo_compat_dotdot_created = 1;
5659 * It will call the appropriate osd_add* function and return the
5660 * value, return by respective functions.
5662 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
5663 struct inode *cinode, const char *name,
5664 const struct lu_fid *fid, struct thandle *th)
5666 struct osd_thread_info *info = osd_oti_get(env);
5667 struct htree_lock *hlock;
5670 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
5672 if (name[0] == '.' && (name[1] == '\0' ||
5673 (name[1] == '.' && name[2] == '\0'))) {
5674 if (hlock != NULL) {
5675 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5678 down_write(&pobj->oo_ext_idx_sem);
5681 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
5682 lu_object_fid(&pobj->oo_dt.do_lu),
5685 if (hlock != NULL) {
5686 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5687 pobj->oo_inode, LDISKFS_HLOCK_ADD);
5689 down_write(&pobj->oo_ext_idx_sem);
5692 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
5693 struct lu_fid *tfid = &info->oti_fid;
5697 rc = __osd_ea_add_rec(info, pobj, cinode, name,
5700 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
5704 if (!rc && fid_is_namespace_visible(lu_object_fid(&pobj->oo_dt.do_lu))
5705 && pobj->oo_dirent_count != LU_DIRENT_COUNT_UNSET)
5706 pobj->oo_dirent_count++;
5709 ldiskfs_htree_unlock(hlock);
5711 up_write(&pobj->oo_ext_idx_sem);
5717 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
5718 struct osd_idmap_cache *oic)
5720 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
5721 struct lu_fid *fid = &oic->oic_fid;
5722 struct osd_inode_id *id = &oic->oic_lid;
5723 struct inode *inode = NULL;
5730 if (!fid_is_norm(fid) && !fid_is_igif(fid))
5733 if (scrub->os_running && scrub->os_pos_current > id->oii_ino)
5736 if (dev->od_auto_scrub_interval == AS_NEVER ||
5737 ktime_get_real_seconds() <
5738 scrub->os_file.sf_time_last_complete + dev->od_auto_scrub_interval)
5742 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
5743 if (rc == -ENOENT) {
5744 __u32 gen = id->oii_gen;
5750 inode = osd_iget(oti, dev, id);
5751 /* The inode has been removed (by race maybe). */
5752 if (IS_ERR(inode)) {
5753 rc = PTR_ERR(inode);
5755 RETURN(rc == -ESTALE ? -ENOENT : rc);
5758 /* The OI mapping is lost. */
5759 if (gen != OSD_OII_NOGEN)
5763 * The inode may has been reused by others, we do not know,
5764 * leave it to be handled by subsequent osd_fid_lookup().
5767 } else if (rc || osd_id_eq(id, &oti->oti_id)) {
5774 if (scrub->os_running) {
5775 if (inode == NULL) {
5776 inode = osd_iget(oti, dev, id);
5777 /* The inode has been removed (by race maybe). */
5778 if (IS_ERR(inode)) {
5779 rc = PTR_ERR(inode);
5781 RETURN(rc == -ESTALE ? -ENOENT : rc);
5785 rc = osd_oii_insert(dev, oic, insert);
5787 * There is race condition between osd_oi_lookup and OI scrub.
5788 * The OI scrub finished just after osd_oi_lookup() failure.
5789 * Under such case, it is unnecessary to trigger OI scrub again,
5790 * but try to call osd_oi_lookup() again.
5792 if (unlikely(rc == -EAGAIN))
5795 if (!S_ISDIR(inode->i_mode))
5798 rc = osd_check_lmv(oti, dev, inode, oic);
5803 if (dev->od_auto_scrub_interval != AS_NEVER && ++once == 1) {
5804 rc = osd_scrub_start(oti->oti_env, dev, SS_AUTO_PARTIAL |
5805 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
5806 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
5807 "%s: trigger partial OI scrub for RPC inconsistency checking FID "DFID": rc = %d\n",
5808 osd_dev2name(dev), PFID(fid), rc);
5809 if (rc == 0 || rc == -EALREADY)
5822 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
5823 struct osd_device *dev,
5824 struct osd_idmap_cache *oic,
5825 struct lu_fid *fid, __u32 ino)
5827 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
5828 struct inode *inode;
5831 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
5832 inode = osd_iget(oti, dev, &oic->oic_lid);
5833 if (IS_ERR(inode)) {
5834 fid_zero(&oic->oic_fid);
5835 return PTR_ERR(inode);
5838 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, loa);
5841 fid_zero(&oic->oic_fid);
5843 *fid = oic->oic_fid = loa->loa_lma.lma_self_fid;
5847 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
5848 struct osd_inode_id *id, const struct lu_fid *fid)
5850 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
5851 id->oii_ino, id->oii_gen, info);
5852 info->oti_cache.oic_lid = *id;
5853 info->oti_cache.oic_fid = *fid;
5854 info->oti_cache.oic_dev = osd;
5858 * Get parent FID from the linkEA.
5860 * For a directory which parent resides on remote MDT, to satisfy the
5861 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
5862 * the other hand, to make the lookup(..) on the directory can return
5863 * the real parent FID, we append the real parent FID after its ".."
5864 * name entry in the /REMOTE_PARENT_DIR.
5866 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
5867 * backup. So after the restore, we cannot get the right parent FID from
5868 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
5869 * we have stored the real parent FID in the directory object's linkEA,
5870 * we can parse the linkEA for the real parent FID.
5872 * \param[in] env pointer to the thread context
5873 * \param[in] obj pointer to the object to be handled
5874 * \param[out]fid pointer to the buffer to hold the parent FID
5876 * \retval 0 for getting the real parent FID successfully
5877 * \retval negative error number on failure
5879 static int osd_get_pfid_from_linkea(const struct lu_env *env,
5880 struct osd_object *obj,
5883 struct osd_thread_info *oti = osd_oti_get(env);
5884 struct lu_buf *buf = &oti->oti_big_buf;
5885 struct dentry *dentry = &oti->oti_obj_dentry;
5886 struct inode *inode = obj->oo_inode;
5887 struct linkea_data ldata = { NULL };
5893 if (!S_ISDIR(inode->i_mode))
5897 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5898 buf->lb_buf, buf->lb_len);
5899 if (rc == -ERANGE) {
5900 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5903 lu_buf_realloc(buf, rc);
5904 if (buf->lb_buf == NULL)
5911 if (unlikely(rc == 0))
5917 if (unlikely(buf->lb_buf == NULL)) {
5918 lu_buf_realloc(buf, rc);
5919 if (buf->lb_buf == NULL)
5926 rc = linkea_init_with_rec(&ldata);
5928 linkea_first_entry(&ldata);
5929 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5935 static int osd_verify_ent_by_linkea(const struct lu_env *env,
5936 struct inode *inode,
5937 const struct lu_fid *pfid,
5938 const char *name, const int namelen)
5940 struct osd_thread_info *oti = osd_oti_get(env);
5941 struct lu_buf *buf = &oti->oti_big_buf;
5942 struct dentry *dentry = &oti->oti_obj_dentry;
5943 struct linkea_data ldata = { NULL };
5944 struct lu_name cname = { .ln_name = name,
5945 .ln_namelen = namelen };
5951 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5952 buf->lb_buf, buf->lb_len);
5954 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
5959 if (unlikely(rc == 0))
5962 if (buf->lb_len < rc) {
5963 lu_buf_realloc(buf, rc);
5964 if (buf->lb_buf == NULL)
5971 rc = linkea_init_with_rec(&ldata);
5973 rc = linkea_links_find(&ldata, &cname, pfid);
5979 * Calls ->lookup() to find dentry. From dentry get inode and
5980 * read inode's ea to get fid. This is required for interoperability
5983 * \retval 0, on success
5984 * \retval -ve, on error
5986 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
5987 struct dt_rec *rec, const struct dt_key *key)
5989 struct inode *dir = obj->oo_inode;
5990 struct dentry *dentry;
5991 struct ldiskfs_dir_entry_2 *de;
5992 struct buffer_head *bh;
5993 struct lu_fid *fid = (struct lu_fid *)rec;
5994 struct htree_lock *hlock = NULL;
6000 LASSERT(dir->i_op != NULL);
6001 LASSERT(dir->i_op->lookup != NULL);
6003 dentry = osd_child_dentry_get(env, obj,
6004 (char *)key, strlen((char *)key));
6006 if (obj->oo_hl_head != NULL) {
6007 hlock = osd_oti_get(env)->oti_hlock;
6008 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
6009 dir, LDISKFS_HLOCK_LOOKUP);
6011 down_read(&obj->oo_ext_idx_sem);
6014 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6016 struct osd_thread_info *oti = osd_oti_get(env);
6017 struct osd_inode_id *id = &oti->oti_id;
6018 struct osd_idmap_cache *oic = &oti->oti_cache;
6019 struct osd_device *dev = osd_obj2dev(obj);
6021 ino = le32_to_cpu(de->inode);
6022 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
6024 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
6028 rc = osd_get_fid_from_dentry(de, rec);
6030 /* done with de, release bh */
6033 if (unlikely(is_remote_parent_ino(dev, ino))) {
6034 const char *name = (const char *)key;
6037 * If the parent is on remote MDT, and there
6038 * is no FID-in-dirent, then we have to get
6039 * the parent FID from the linkEA.
6041 if (likely(strlen(name) == 2 &&
6042 name[0] == '.' && name[1] == '.'))
6043 rc = osd_get_pfid_from_linkea(env, obj,
6046 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6049 osd_id_gen(id, ino, OSD_OII_NOGEN);
6052 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
6053 fid_zero(&oic->oic_fid);
6058 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
6059 rc = osd_consistency_check(oti, dev, oic);
6061 fid_zero(&oic->oic_fid);
6063 /* Other error should not affect lookup result. */
6073 ldiskfs_htree_unlock(hlock);
6075 up_read(&obj->oo_ext_idx_sem);
6079 static int osd_index_declare_ea_insert(const struct lu_env *env,
6080 struct dt_object *dt,
6081 const struct dt_rec *rec,
6082 const struct dt_key *key,
6083 struct thandle *handle)
6085 struct osd_thandle *oh;
6086 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
6087 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
6088 const struct lu_fid *fid = rec1->rec_fid;
6089 int credits, rc = 0;
6090 struct osd_idmap_cache *idc;
6094 LASSERT(!dt_object_remote(dt));
6095 LASSERT(handle != NULL);
6096 LASSERT(fid != NULL);
6097 LASSERT(rec1->rec_type != 0);
6099 oh = container_of(handle, struct osd_thandle, ot_super);
6100 LASSERT(oh->ot_handle == NULL);
6102 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
6105 * we can't call iget() while a transactions is running
6106 * (this can lead to a deadlock), but we need to know
6107 * inum and object type. so we find this information at
6108 * declaration and cache in per-thread info
6110 idc = osd_idc_find_or_init(env, osd, fid);
6112 RETURN(PTR_ERR(idc));
6113 if (idc->oic_remote) {
6115 * a reference to remote inode is represented by an
6116 * agent inode which we have to create
6118 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
6119 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
6122 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
6124 if (osd_dt_obj(dt)->oo_inode != NULL) {
6125 struct inode *inode = osd_dt_obj(dt)->oo_inode;
6128 * We ignore block quota on meta pool (MDTs), so needn't
6129 * calculate how many blocks will be consumed by this index
6132 rc = osd_declare_inode_qid(env, i_uid_read(inode),
6134 i_projid_read(inode), 0,
6135 oh, osd_dt_obj(dt), NULL,
6140 #ifdef HAVE_PROJECT_QUOTA
6142 * Reserve credits for local agent inode to transfer
6143 * to 0, quota enforcement is ignored in this case.
6145 if (idc->oic_remote &&
6146 LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
6147 i_projid_read(inode) != 0)
6148 rc = osd_declare_attr_qid(env, osd_dt_obj(dt), oh,
6149 0, i_projid_read(inode),
6150 0, false, PRJQUOTA, true);
6158 * Index add function for interoperability mode (b11826).
6159 * It will add the directory entry.This entry is needed to
6160 * maintain name->fid mapping.
6162 * \param key it is key i.e. file entry to be inserted
6163 * \param rec it is value of given key i.e. fid
6165 * \retval 0, on success
6166 * \retval -ve, on error
6168 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
6169 const struct dt_rec *rec,
6170 const struct dt_key *key, struct thandle *th)
6172 struct osd_object *obj = osd_dt_obj(dt);
6173 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
6174 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
6175 const struct lu_fid *fid = rec1->rec_fid;
6176 const char *name = (const char *)key;
6177 struct osd_thread_info *oti = osd_oti_get(env);
6178 struct inode *child_inode = NULL;
6179 struct osd_idmap_cache *idc;
6184 if (!dt_object_exists(dt))
6187 LASSERT(osd_invariant(obj));
6188 LASSERT(!dt_object_remote(dt));
6189 LASSERT(th != NULL);
6191 osd_trans_exec_op(env, th, OSD_OT_INSERT);
6193 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
6195 idc = osd_idc_find(env, osd, fid);
6196 if (unlikely(idc == NULL)) {
6197 idc = osd_idc_find_or_init(env, osd, fid);
6200 * this dt_insert() wasn't declared properly, so
6201 * FID is missing in OI cache. we better do not
6202 * lookup FID in FLDB/OI and don't risk to deadlock,
6203 * but in some special cases (lfsck testing, etc)
6204 * it's much simpler than fixing a caller.
6206 * normally this error should be placed after the first
6207 * find, but migrate may attach source stripes to
6208 * target, which doesn't create stripes.
6210 CERROR("%s: "DFID" wasn't declared for insert\n",
6211 osd_name(osd), PFID(fid));
6213 RETURN(PTR_ERR(idc));
6217 if (idc->oic_remote) {
6218 /* Insert remote entry */
6219 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
6221 igrab(osd->od_mdt_map->omm_remote_parent->d_inode);
6223 child_inode = osd_create_local_agent_inode(env, osd,
6224 obj, fid, rec1->rec_type & S_IFMT, th);
6225 if (IS_ERR(child_inode))
6226 RETURN(PTR_ERR(child_inode));
6229 /* Insert local entry */
6230 if (unlikely(idc->oic_lid.oii_ino == 0)) {
6231 /* for a reason OI cache wasn't filled properly */
6232 CERROR("%s: OIC for "DFID" isn't filled\n",
6233 osd_name(osd), PFID(fid));
6236 child_inode = oti->oti_inode;
6237 if (unlikely(child_inode == NULL)) {
6238 struct ldiskfs_inode_info *lii;
6243 child_inode = oti->oti_inode = &lii->vfs_inode;
6245 child_inode->i_sb = osd_sb(osd);
6246 child_inode->i_ino = idc->oic_lid.oii_ino;
6247 child_inode->i_mode = rec1->rec_type & S_IFMT;
6250 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
6252 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
6253 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
6255 if (child_inode && child_inode != oti->oti_inode)
6257 LASSERT(osd_invariant(obj));
6258 osd_trans_exec_check(env, th, OSD_OT_INSERT);
6264 * Initialize osd Iterator for given osd index object.
6266 * \param dt osd index object
6269 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
6270 struct dt_object *dt,
6273 struct osd_it_iam *it;
6274 struct osd_object *obj = osd_dt_obj(dt);
6275 struct lu_object *lo = &dt->do_lu;
6276 struct iam_path_descr *ipd;
6277 struct iam_container *bag = &obj->oo_dir->od_container;
6279 if (!dt_object_exists(dt))
6280 return ERR_PTR(-ENOENT);
6284 return ERR_PTR(-ENOMEM);
6286 ipd = osd_it_ipd_get(env, bag);
6287 if (likely(ipd != NULL)) {
6291 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
6292 return (struct dt_it *)it;
6295 return ERR_PTR(-ENOMEM);
6300 * free given Iterator.
6302 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
6304 struct osd_it_iam *it = (struct osd_it_iam *)di;
6305 struct osd_object *obj = it->oi_obj;
6307 iam_it_fini(&it->oi_it);
6308 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
6309 osd_object_put(env, obj);
6314 * Move Iterator to record specified by \a key
6316 * \param di osd iterator
6317 * \param key key for index
6319 * \retval +ve di points to record with least key not larger than key
6320 * \retval 0 di points to exact matched key
6321 * \retval -ve failure
6324 static int osd_it_iam_get(const struct lu_env *env,
6325 struct dt_it *di, const struct dt_key *key)
6327 struct osd_thread_info *oti = osd_oti_get(env);
6328 struct osd_it_iam *it = (struct osd_it_iam *)di;
6330 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
6331 /* swab quota uid/gid */
6332 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
6333 key = (struct dt_key *)&oti->oti_quota_id;
6336 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
6342 * \param di osd iterator
6344 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
6346 struct osd_it_iam *it = (struct osd_it_iam *)di;
6348 iam_it_put(&it->oi_it);
6352 * Move iterator by one record
6354 * \param di osd iterator
6356 * \retval +1 end of container reached
6358 * \retval -ve failure
6361 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
6363 struct osd_it_iam *it = (struct osd_it_iam *)di;
6365 return iam_it_next(&it->oi_it);
6369 * Return pointer to the key under iterator.
6372 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
6373 const struct dt_it *di)
6375 struct osd_thread_info *oti = osd_oti_get(env);
6376 struct osd_it_iam *it = (struct osd_it_iam *)di;
6377 struct osd_object *obj = it->oi_obj;
6380 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
6382 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
6383 /* swab quota uid/gid */
6384 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
6385 key = (struct dt_key *)&oti->oti_quota_id;
6392 * Return size of key under iterator (in bytes)
6395 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
6397 struct osd_it_iam *it = (struct osd_it_iam *)di;
6399 return iam_it_key_size(&it->oi_it);
6403 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
6405 /* check if file type is required */
6406 if (ent->lde_attrs & LUDA_TYPE) {
6407 struct luda_type *lt;
6408 int align = sizeof(*lt) - 1;
6410 len = (len + align) & ~align;
6411 lt = (struct luda_type *)(ent->lde_name + len);
6412 lt->lt_type = cpu_to_le16(DTTOIF(type));
6415 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
6419 * build lu direct from backend fs dirent.
6423 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
6424 char *name, __u16 namelen, __u16 type, __u32 attr)
6426 ent->lde_attrs = attr | LUDA_FID;
6427 fid_cpu_to_le(&ent->lde_fid, fid);
6429 ent->lde_hash = cpu_to_le64(offset);
6430 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
6432 strncpy(ent->lde_name, name, namelen);
6433 ent->lde_name[namelen] = '\0';
6434 ent->lde_namelen = cpu_to_le16(namelen);
6436 /* append lustre attributes */
6437 osd_it_append_attrs(ent, namelen, type);
6441 * Return pointer to the record under iterator.
6443 static int osd_it_iam_rec(const struct lu_env *env,
6444 const struct dt_it *di,
6445 struct dt_rec *dtrec, __u32 attr)
6447 struct osd_it_iam *it = (struct osd_it_iam *)di;
6448 struct osd_thread_info *info = osd_oti_get(env);
6452 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
6453 const struct osd_fid_pack *rec;
6454 struct lu_fid *fid = &info->oti_fid;
6455 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6461 name = (char *)iam_it_key_get(&it->oi_it);
6463 RETURN(PTR_ERR(name));
6465 namelen = iam_it_key_size(&it->oi_it);
6467 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
6469 RETURN(PTR_ERR(rec));
6471 rc = osd_fid_unpack(fid, rec);
6475 hash = iam_it_store(&it->oi_it);
6477 /* IAM does not store object type in IAM index (dir) */
6478 osd_it_pack_dirent(lde, fid, hash, name, namelen,
6480 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
6481 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6482 (struct iam_rec *)dtrec);
6483 osd_quota_unpack(it->oi_obj, dtrec);
6485 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6486 (struct iam_rec *)dtrec);
6493 * Returns cookie for current Iterator position.
6495 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
6497 struct osd_it_iam *it = (struct osd_it_iam *)di;
6499 return iam_it_store(&it->oi_it);
6503 * Restore iterator from cookie.
6505 * \param di osd iterator
6506 * \param hash Iterator location cookie
6508 * \retval +ve di points to record with least key not larger than key.
6509 * \retval 0 di points to exact matched key
6510 * \retval -ve failure
6513 static int osd_it_iam_load(const struct lu_env *env,
6514 const struct dt_it *di, __u64 hash)
6516 struct osd_it_iam *it = (struct osd_it_iam *)di;
6518 return iam_it_load(&it->oi_it, hash);
6521 static const struct dt_index_operations osd_index_iam_ops = {
6522 .dio_lookup = osd_index_iam_lookup,
6523 .dio_declare_insert = osd_index_declare_iam_insert,
6524 .dio_insert = osd_index_iam_insert,
6525 .dio_declare_delete = osd_index_declare_iam_delete,
6526 .dio_delete = osd_index_iam_delete,
6528 .init = osd_it_iam_init,
6529 .fini = osd_it_iam_fini,
6530 .get = osd_it_iam_get,
6531 .put = osd_it_iam_put,
6532 .next = osd_it_iam_next,
6533 .key = osd_it_iam_key,
6534 .key_size = osd_it_iam_key_size,
6535 .rec = osd_it_iam_rec,
6536 .store = osd_it_iam_store,
6537 .load = osd_it_iam_load
6543 * Creates or initializes iterator context.
6545 * \retval struct osd_it_ea, iterator structure on success
6548 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
6549 struct dt_object *dt,
6552 struct osd_object *obj = osd_dt_obj(dt);
6553 struct osd_thread_info *info = osd_oti_get(env);
6554 struct osd_it_ea *oie;
6556 struct lu_object *lo = &dt->do_lu;
6557 struct dentry *obj_dentry;
6561 if (!dt_object_exists(dt) || obj->oo_destroyed)
6562 RETURN(ERR_PTR(-ENOENT));
6564 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
6566 RETURN(ERR_PTR(-ENOMEM));
6567 obj_dentry = &oie->oie_dentry;
6569 obj_dentry->d_inode = obj->oo_inode;
6570 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
6571 obj_dentry->d_name.hash = 0;
6573 oie->oie_rd_dirent = 0;
6574 oie->oie_it_dirent = 0;
6575 oie->oie_dirent = NULL;
6576 if (unlikely(!info->oti_it_ea_buf_used)) {
6577 oie->oie_buf = info->oti_it_ea_buf;
6578 info->oti_it_ea_buf_used = 1;
6580 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6581 if (oie->oie_buf == NULL)
6582 RETURN(ERR_PTR(-ENOMEM));
6586 file = &oie->oie_file;
6588 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
6589 if (attr & LUDA_64BITHASH)
6590 file->f_mode = FMODE_64BITHASH;
6592 file->f_mode = FMODE_32BITHASH;
6593 file->f_path.dentry = obj_dentry;
6594 file->f_mapping = obj->oo_inode->i_mapping;
6595 file->f_op = obj->oo_inode->i_fop;
6596 file->f_inode = obj->oo_inode;
6599 RETURN((struct dt_it *)oie);
6603 * Destroy or finishes iterator context.
6605 * \param di iterator structure to be destroyed
6607 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
6609 struct osd_thread_info *info = osd_oti_get(env);
6610 struct osd_it_ea *oie = (struct osd_it_ea *)di;
6611 struct osd_object *obj = oie->oie_obj;
6612 struct inode *inode = obj->oo_inode;
6615 oie->oie_file.f_op->release(inode, &oie->oie_file);
6616 osd_object_put(env, obj);
6617 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
6618 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6620 info->oti_it_ea_buf_used = 0;
6621 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
6626 * It position the iterator at given key, so that next lookup continues from
6627 * that key Or it is similar to dio_it->load() but based on a key,
6628 * rather than file position.
6630 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
6633 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
6635 static int osd_it_ea_get(const struct lu_env *env,
6636 struct dt_it *di, const struct dt_key *key)
6638 struct osd_it_ea *it = (struct osd_it_ea *)di;
6641 LASSERT(((const char *)key)[0] == '\0');
6642 it->oie_file.f_pos = 0;
6643 it->oie_rd_dirent = 0;
6644 it->oie_it_dirent = 0;
6645 it->oie_dirent = NULL;
6653 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
6657 struct osd_filldir_cbs {
6658 struct dir_context ctx;
6659 struct osd_it_ea *it;
6662 * It is called internally by ->iterate*(). It fills the
6663 * iterator's in-memory data structure with required
6664 * information i.e. name, namelen, rec_size etc.
6666 * \param buf in which information to be filled in.
6667 * \param name name of the file in given dir
6669 * \retval 0 on success
6670 * \retval 1 on buffer full
6672 #ifdef HAVE_FILLDIR_USE_CTX
6673 static int osd_ldiskfs_filldir(struct dir_context *buf,
6675 static int osd_ldiskfs_filldir(void *buf,
6677 const char *name, int namelen,
6678 loff_t offset, __u64 ino, unsigned int d_type)
6680 struct osd_it_ea *it = ((struct osd_filldir_cbs *)buf)->it;
6681 struct osd_object *obj = it->oie_obj;
6682 struct osd_it_ea_dirent *ent = it->oie_dirent;
6683 struct lu_fid *fid = &ent->oied_fid;
6684 struct osd_fid_pack *rec;
6688 /* this should never happen */
6689 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
6690 CERROR("ldiskfs return invalid namelen %d\n", namelen);
6694 if ((void *)ent - it->oie_buf + sizeof(*ent) + namelen >
6698 /* "." is just the object itself. */
6699 if (namelen == 1 && name[0] == '.') {
6700 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6701 } else if (d_type & LDISKFS_DIRENT_LUFID) {
6702 rec = (struct osd_fid_pack *)(name + namelen + 1);
6703 if (osd_fid_unpack(fid, rec) != 0)
6708 d_type &= ~LDISKFS_DIRENT_LUFID;
6710 /* NOT export local root. */
6711 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
6712 ino = obj->oo_inode->i_ino;
6713 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6716 ent->oied_ino = ino;
6717 ent->oied_off = offset;
6718 ent->oied_namelen = namelen;
6719 ent->oied_type = d_type;
6721 memcpy(ent->oied_name, name, namelen);
6723 it->oie_rd_dirent++;
6724 it->oie_dirent = (void *)ent + cfs_size_round(sizeof(*ent) + namelen);
6729 * Calls ->iterate*() to load a directory entry at a time
6730 * and stored it in iterator's in-memory data structure.
6732 * \param di iterator's in memory structure
6734 * \retval 0 on success
6735 * \retval -ve on error
6736 * \retval +1 reach the end of entry
6738 static int osd_ldiskfs_it_fill(const struct lu_env *env,
6739 const struct dt_it *di)
6741 struct osd_it_ea *it = (struct osd_it_ea *)di;
6742 struct osd_object *obj = it->oie_obj;
6743 struct inode *inode = obj->oo_inode;
6744 struct htree_lock *hlock = NULL;
6745 struct file *filp = &it->oie_file;
6747 struct osd_filldir_cbs buf = {
6748 .ctx.actor = osd_ldiskfs_filldir,
6753 it->oie_dirent = it->oie_buf;
6754 it->oie_rd_dirent = 0;
6756 if (obj->oo_hl_head != NULL) {
6757 hlock = osd_oti_get(env)->oti_hlock;
6758 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
6759 inode, LDISKFS_HLOCK_READDIR);
6761 down_read(&obj->oo_ext_idx_sem);
6764 filp->f_cred = current_cred();
6765 rc = osd_security_file_alloc(filp);
6769 filp->f_flags |= O_NOATIME;
6770 filp->f_mode |= FMODE_NONOTIFY;
6771 rc = iterate_dir(filp, &buf.ctx);
6775 if (it->oie_rd_dirent == 0) {
6777 * If it does not get any dirent, it means it has been reached
6778 * to the end of the dir
6780 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
6784 it->oie_dirent = it->oie_buf;
6785 it->oie_it_dirent = 1;
6789 ldiskfs_htree_unlock(hlock);
6791 up_read(&obj->oo_ext_idx_sem);
6797 * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
6798 * to load a directory entry at a time and stored it in
6799 * iterator's in-memory data structure.
6801 * \param di iterator's in memory structure
6803 * \retval +ve iterator reached to end
6804 * \retval 0 iterator not reached to end
6805 * \retval -ve on error
6807 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
6809 struct osd_it_ea *it = (struct osd_it_ea *)di;
6814 if (it->oie_it_dirent < it->oie_rd_dirent) {
6816 (void *)it->oie_dirent +
6817 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
6818 it->oie_dirent->oied_namelen);
6819 it->oie_it_dirent++;
6822 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
6825 rc = osd_ldiskfs_it_fill(env, di);
6832 * Returns the key at current position from iterator's in memory structure.
6834 * \param di iterator's in memory structure
6836 * \retval key i.e. struct dt_key on success
6838 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
6839 const struct dt_it *di)
6841 struct osd_it_ea *it = (struct osd_it_ea *)di;
6843 return (struct dt_key *)it->oie_dirent->oied_name;
6847 * Returns key's size at current position from iterator's in memory structure.
6849 * \param di iterator's in memory structure
6851 * \retval key_size i.e. struct dt_key on success
6853 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
6855 struct osd_it_ea *it = (struct osd_it_ea *)di;
6857 return it->oie_dirent->oied_namelen;
6860 #if defined LDISKFS_DIR_ENTRY_LEN && defined LDISKFS_DIR_ENTRY_LEN_
6861 #undef LDISKFS_DIR_REC_LEN
6862 #define LDISKFS_DIR_REC_LEN(de) LDISKFS_DIR_ENTRY_LEN_((de))
6865 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
6867 if (LDISKFS_DIR_REC_LEN(de) >=
6868 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
6875 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
6876 unsigned int blocksize, bool dotdot)
6879 return osd_dotdot_has_space(de);
6881 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
6882 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
6889 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
6890 handle_t *jh, struct dentry *dentry,
6891 const struct lu_fid *fid, struct buffer_head *bh,
6892 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
6895 struct inode *dir = dentry->d_parent->d_inode;
6896 struct inode *inode = dentry->d_inode;
6897 struct osd_fid_pack *rec;
6898 struct ldiskfs_dentry_param *ldp;
6899 int namelen = dentry->d_name.len;
6901 struct osd_thread_info *info = osd_oti_get(env);
6905 if (!ldiskfs_has_feature_dirdata(inode->i_sb))
6908 /* There is enough space to hold the FID-in-dirent. */
6909 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
6910 rc = ldiskfs_journal_get_write_access(jh, bh);
6914 de->name[namelen] = 0;
6915 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
6916 rec->fp_len = sizeof(struct lu_fid) + 1;
6917 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
6918 de->file_type |= LDISKFS_DIRENT_LUFID;
6919 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
6926 rc = ldiskfs_delete_entry(jh, dir, de, bh);
6930 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
6931 osd_get_ldiskfs_dirent_param(ldp, fid);
6932 dentry->d_fsdata = (void *)ldp;
6933 dquot_initialize(dir);
6934 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
6936 * It is too bad, we cannot reinsert the name entry back.
6937 * That means we lose it!
6941 "%s: fail to reinsert the dirent, dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
6942 osd_ino2name(inode), dir->i_ino, dir->i_generation,
6943 namelen, dentry->d_name.name, PFID(fid), rc);
6949 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
6950 struct osd_it_ea *it, struct lu_fid *fid,
6951 struct osd_inode_id *id, __u32 *attr)
6953 struct osd_thread_info *info = osd_oti_get(env);
6954 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
6955 struct osd_device *dev = osd_obj2dev(obj);
6956 struct super_block *sb = osd_sb(dev);
6957 const char *devname = osd_name(dev);
6958 struct osd_it_ea_dirent *ent = it->oie_dirent;
6959 struct inode *dir = obj->oo_inode;
6960 struct htree_lock *hlock = NULL;
6961 struct buffer_head *bh = NULL;
6962 handle_t *jh = NULL;
6963 struct ldiskfs_dir_entry_2 *de;
6964 struct dentry *dentry;
6965 struct inode *inode;
6966 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
6969 bool dotdot = false;
6974 if (ent->oied_name[0] == '.') {
6975 if (ent->oied_namelen == 1)
6978 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
6982 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
6983 inode = osd_iget(info, dev, id);
6984 if (IS_ERR(inode)) {
6985 rc = PTR_ERR(inode);
6986 if (rc == -ENOENT || rc == -ESTALE) {
6988 * Maybe dangling name entry, or
6989 * corrupted directory entry.
6991 *attr |= LUDA_UNKNOWN;
6994 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
6995 "check_repair, dir = %lu/%u, name = %.*s, "
6996 "ino = %llu, rc = %d\n",
6997 devname, dir->i_ino, dir->i_generation,
6998 ent->oied_namelen, ent->oied_name,
7005 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
7007 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
7008 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
7014 * We need to ensure that the name entry is still valid.
7015 * Because it may be removed or renamed by other already.
7017 * The unlink or rename operation will start journal before PDO lock,
7018 * so to avoid deadlock, here we need to start journal handle before
7019 * related PDO lock also. But because we do not know whether there
7020 * will be something to be repaired before PDO lock, we just start
7021 * journal without conditions.
7023 * We may need to remove the name entry firstly, then insert back.
7024 * One credit is for user quota file update.
7025 * One credit is for group quota file update.
7026 * Two credits are for dirty inode.
7028 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
7029 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
7031 if (dev->od_dirent_journal != 0) {
7034 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
7037 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
7038 "check_repair, dir = %lu/%u, credits = %d, "
7039 "name = %.*s, ino = %llu: rc = %d\n",
7040 devname, dir->i_ino, dir->i_generation, credits,
7041 ent->oied_namelen, ent->oied_name,
7044 GOTO(out_inode, rc);
7047 if (obj->oo_hl_head != NULL) {
7048 hlock = osd_oti_get(env)->oti_hlock;
7050 * "0" means exclusive lock for the whole directory.
7051 * We need to prevent others access such name entry
7052 * during the delete + insert. Neither HLOCK_ADD nor
7053 * HLOCK_DEL cannot guarantee the atomicity.
7055 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
7057 down_write(&obj->oo_ext_idx_sem);
7060 if (obj->oo_hl_head != NULL) {
7061 hlock = osd_oti_get(env)->oti_hlock;
7062 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
7063 LDISKFS_HLOCK_LOOKUP);
7065 down_read(&obj->oo_ext_idx_sem);
7069 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
7070 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
7071 *attr |= LUDA_IGNORE;
7077 * For dotdot entry, if there is not enough space to hold the
7078 * FID-in-dirent, just keep them there. It only happens when the
7079 * device upgraded from 1.8 or restored from MDT file-level backup.
7080 * For the whole directory, only dotdot entry have no FID-in-dirent
7081 * and needs to get FID from LMA when readdir, it will not affect the
7084 if (dotdot && !osd_dotdot_has_space(de)) {
7085 *attr |= LUDA_UNKNOWN;
7091 if (lu_fid_eq(fid, &lma->lma_self_fid))
7094 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
7095 struct lu_fid *tfid = &lma->lma_self_fid;
7097 if (likely(dotdot &&
7098 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
7099 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
7101 * It must be REMOTE_PARENT_DIR and as the
7102 * 'dotdot' entry of remote directory
7104 *attr |= LUDA_IGNORE;
7106 CDEBUG(D_LFSCK, "%s: expect remote agent "
7107 "parent directory, but got %.*s under "
7108 "dir = %lu/%u with the FID "DFID"\n",
7109 devname, ent->oied_namelen,
7110 ent->oied_name, dir->i_ino,
7111 dir->i_generation, PFID(tfid));
7113 *attr |= LUDA_UNKNOWN;
7120 if (!fid_is_zero(fid)) {
7121 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
7123 if (rc == -ENOENT ||
7125 !(dev->od_scrub.os_scrub.os_file.sf_flags & SF_UPGRADE))) {
7127 * linkEA does not recognize the dirent entry,
7128 * it may because the dirent entry corruption
7129 * and points to other's inode.
7131 CDEBUG(D_LFSCK, "%s: the target inode does not "
7132 "recognize the dirent, dir = %lu/%u, "
7133 " name = %.*s, ino = %llu, "
7134 DFID": rc = %d\n", devname, dir->i_ino,
7135 dir->i_generation, ent->oied_namelen,
7136 ent->oied_name, ent->oied_ino, PFID(fid), rc);
7137 *attr |= LUDA_UNKNOWN;
7142 if (rc && rc != -ENODATA) {
7143 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
7144 "dir = %lu/%u, name = %.*s, ino = %llu, "
7145 DFID": rc = %d\n", devname, dir->i_ino,
7146 dir->i_generation, ent->oied_namelen,
7147 ent->oied_name, ent->oied_ino, PFID(fid), rc);
7148 *attr |= LUDA_UNKNOWN;
7156 * linkEA recognizes the dirent entry, the FID-in-LMA is
7157 * valid, trusted, in spite of fid_is_sane(fid) or not.
7159 if (*attr & LUDA_VERIFY_DRYRUN) {
7160 *fid = lma->lma_self_fid;
7161 *attr |= LUDA_REPAIR;
7168 dev->od_dirent_journal = 1;
7169 if (hlock != NULL) {
7170 ldiskfs_htree_unlock(hlock);
7173 up_read(&obj->oo_ext_idx_sem);
7179 *fid = lma->lma_self_fid;
7181 /* Update or append the FID-in-dirent. */
7182 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
7183 bh, de, hlock, dotdot);
7185 *attr |= LUDA_REPAIR;
7187 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
7188 "the dirent, dir = %lu/%u, name = %.*s, "
7189 "ino = %llu, "DFID": rc = %d\n",
7190 devname, dir->i_ino, dir->i_generation,
7191 ent->oied_namelen, ent->oied_name,
7192 ent->oied_ino, PFID(fid), rc);
7194 /* lma is NULL, trust the FID-in-dirent if it is valid. */
7195 if (*attr & LUDA_VERIFY_DRYRUN) {
7196 if (fid_is_sane(fid)) {
7197 *attr |= LUDA_REPAIR;
7198 } else if (dev->od_index == 0) {
7199 lu_igif_build(fid, inode->i_ino,
7200 inode->i_generation);
7201 *attr |= LUDA_UPGRADE;
7209 dev->od_dirent_journal = 1;
7210 if (hlock != NULL) {
7211 ldiskfs_htree_unlock(hlock);
7214 up_read(&obj->oo_ext_idx_sem);
7221 if (unlikely(fid_is_sane(fid))) {
7223 * FID-in-dirent exists, but FID-in-LMA is lost.
7224 * Trust the FID-in-dirent, and add FID-in-LMA.
7226 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
7228 *attr |= LUDA_REPAIR;
7230 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
7231 "update dirent, dir = %lu/%u, "
7232 "name = %.*s, ino = %llu, "
7234 devname, dir->i_ino, dir->i_generation,
7235 ent->oied_namelen, ent->oied_name,
7236 ent->oied_ino, PFID(fid), rc);
7237 } else if (dev->od_index == 0) {
7238 lu_igif_build(fid, inode->i_ino, inode->i_generation);
7240 * It is probably IGIF object. Only aappend the
7241 * FID-in-dirent. OI scrub will process FID-in-LMA.
7243 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
7244 bh, de, hlock, dotdot);
7246 *attr |= LUDA_UPGRADE;
7248 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
7249 "after the dirent, dir = %lu/%u, "
7250 "name = %.*s, ino = %llu, "
7252 devname, dir->i_ino, dir->i_generation,
7253 ent->oied_namelen, ent->oied_name,
7254 ent->oied_ino, PFID(fid), rc);
7263 if (hlock != NULL) {
7264 ldiskfs_htree_unlock(hlock);
7266 if (dev->od_dirent_journal != 0)
7267 up_write(&obj->oo_ext_idx_sem);
7269 up_read(&obj->oo_ext_idx_sem);
7273 ldiskfs_journal_stop(jh);
7277 if (rc >= 0 && !dirty)
7278 dev->od_dirent_journal = 0;
7284 * Returns the value at current position from iterator's in memory structure.
7286 * \param di struct osd_it_ea, iterator's in memory structure
7287 * \param attr attr requested for dirent.
7288 * \param lde lustre dirent
7290 * \retval 0 no error and \param lde has correct lustre dirent.
7291 * \retval -ve on error
7293 static inline int osd_it_ea_rec(const struct lu_env *env,
7294 const struct dt_it *di,
7295 struct dt_rec *dtrec, __u32 attr)
7297 struct osd_it_ea *it = (struct osd_it_ea *)di;
7298 struct osd_object *obj = it->oie_obj;
7299 struct osd_device *dev = osd_obj2dev(obj);
7300 struct osd_thread_info *oti = osd_oti_get(env);
7301 struct osd_inode_id *id = &oti->oti_id;
7302 struct lu_fid *fid = &it->oie_dirent->oied_fid;
7303 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
7304 __u32 ino = it->oie_dirent->oied_ino;
7309 LASSERT(!is_remote_parent_ino(dev, obj->oo_inode->i_ino));
7311 if (attr & LUDA_VERIFY) {
7312 if (unlikely(is_remote_parent_ino(dev, ino))) {
7313 attr |= LUDA_IGNORE;
7315 * If the parent is on remote MDT, and there
7316 * is no FID-in-dirent, then we have to get
7317 * the parent FID from the linkEA.
7319 if (!fid_is_sane(fid) &&
7320 it->oie_dirent->oied_namelen == 2 &&
7321 it->oie_dirent->oied_name[0] == '.' &&
7322 it->oie_dirent->oied_name[1] == '.')
7323 osd_get_pfid_from_linkea(env, obj, fid);
7325 rc = osd_dirent_check_repair(env, obj, it, fid, id,
7329 if (!fid_is_sane(fid))
7330 attr |= LUDA_UNKNOWN;
7332 attr &= ~LU_DIRENT_ATTRS_MASK;
7333 if (!fid_is_sane(fid)) {
7334 bool is_dotdot = false;
7336 if (it->oie_dirent->oied_namelen == 2 &&
7337 it->oie_dirent->oied_name[0] == '.' &&
7338 it->oie_dirent->oied_name[1] == '.')
7341 * If the parent is on remote MDT, and there
7342 * is no FID-in-dirent, then we have to get
7343 * the parent FID from the linkEA.
7345 if (is_remote_parent_ino(dev, ino) && is_dotdot) {
7346 rc = osd_get_pfid_from_linkea(env, obj, fid);
7348 if (is_dotdot == false &&
7349 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
7352 rc = osd_ea_fid_get(env, obj, ino, fid, id);
7357 /* Pack the entry anyway, at least the offset is right. */
7358 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
7359 it->oie_dirent->oied_name,
7360 it->oie_dirent->oied_namelen,
7361 it->oie_dirent->oied_type, attr);
7363 RETURN(rc > 0 ? 0 : rc);
7367 * Returns the record size size at current position.
7369 * This function will return record(lu_dirent) size in bytes.
7371 * \param[in] env execution environment
7372 * \param[in] di iterator's in memory structure
7373 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
7374 * calculate the lu_dirent size.
7376 * \retval record size(in bytes & in memory) of the current lu_dirent
7379 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
7382 struct osd_it_ea *it = (struct osd_it_ea *)di;
7384 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
7388 * Returns a cookie for current position of the iterator head, so that
7389 * user can use this cookie to load/start the iterator next time.
7391 * \param di iterator's in memory structure
7393 * \retval cookie for current position, on success
7395 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
7397 struct osd_it_ea *it = (struct osd_it_ea *)di;
7399 return it->oie_dirent->oied_off;
7403 * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
7404 * to load a directory entry at a time and stored it i inn,
7405 * in iterator's in-memory data structure.
7407 * \param di struct osd_it_ea, iterator's in memory structure
7409 * \retval +ve on success
7410 * \retval -ve on error
7412 static int osd_it_ea_load(const struct lu_env *env,
7413 const struct dt_it *di, __u64 hash)
7415 struct osd_it_ea *it = (struct osd_it_ea *)di;
7419 it->oie_file.f_pos = hash;
7421 rc = osd_ldiskfs_it_fill(env, di);
7432 * Index lookup function for interoperability mode (b11826).
7434 * \param key, key i.e. file name to be searched
7436 * \retval +ve, on success
7437 * \retval -ve, on error
7439 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
7440 struct dt_rec *rec, const struct dt_key *key)
7442 struct osd_object *obj = osd_dt_obj(dt);
7447 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
7448 LINVRNT(osd_invariant(obj));
7450 rc = osd_ea_lookup_rec(env, obj, rec, key);
7457 * Index and Iterator operations for interoperability
7458 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
7460 static const struct dt_index_operations osd_index_ea_ops = {
7461 .dio_lookup = osd_index_ea_lookup,
7462 .dio_declare_insert = osd_index_declare_ea_insert,
7463 .dio_insert = osd_index_ea_insert,
7464 .dio_declare_delete = osd_index_declare_ea_delete,
7465 .dio_delete = osd_index_ea_delete,
7467 .init = osd_it_ea_init,
7468 .fini = osd_it_ea_fini,
7469 .get = osd_it_ea_get,
7470 .put = osd_it_ea_put,
7471 .next = osd_it_ea_next,
7472 .key = osd_it_ea_key,
7473 .key_size = osd_it_ea_key_size,
7474 .rec = osd_it_ea_rec,
7475 .rec_size = osd_it_ea_rec_size,
7476 .store = osd_it_ea_store,
7477 .load = osd_it_ea_load
7481 static void *osd_key_init(const struct lu_context *ctx,
7482 struct lu_context_key *key)
7484 struct osd_thread_info *info;
7486 OBD_ALLOC_PTR(info);
7488 return ERR_PTR(-ENOMEM);
7490 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7491 if (info->oti_it_ea_buf == NULL)
7494 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
7496 info->oti_hlock = ldiskfs_htree_lock_alloc();
7497 if (info->oti_hlock == NULL)
7503 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7506 return ERR_PTR(-ENOMEM);
7509 static void osd_key_fini(const struct lu_context *ctx,
7510 struct lu_context_key *key, void *data)
7512 struct osd_thread_info *info = data;
7513 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
7514 struct osd_idmap_cache *idc = info->oti_ins_cache;
7516 if (info->oti_dio_pages) {
7518 for (i = 0; i < PTLRPC_MAX_BRW_PAGES; i++) {
7519 struct page *page = info->oti_dio_pages[i];
7521 LASSERT(PagePrivate2(page));
7522 LASSERT(PageLocked(page));
7523 ClearPagePrivate2(page);
7528 OBD_FREE_PTR_ARRAY_LARGE(info->oti_dio_pages,
7529 PTLRPC_MAX_BRW_PAGES);
7532 if (info->oti_inode != NULL)
7534 if (info->oti_hlock != NULL)
7535 ldiskfs_htree_lock_free(info->oti_hlock);
7536 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7537 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
7538 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
7539 lu_buf_free(&info->oti_iobuf.dr_lnb_buf);
7540 lu_buf_free(&info->oti_big_buf);
7542 LASSERT(info->oti_ins_cache_size > 0);
7543 OBD_FREE_PTR_ARRAY(idc, info->oti_ins_cache_size);
7544 info->oti_ins_cache = NULL;
7545 info->oti_ins_cache_size = 0;
7550 static void osd_key_exit(const struct lu_context *ctx,
7551 struct lu_context_key *key, void *data)
7553 struct osd_thread_info *info = data;
7555 LASSERT(info->oti_r_locks == 0);
7556 LASSERT(info->oti_w_locks == 0);
7557 LASSERT(info->oti_txns == 0);
7560 /* type constructor/destructor: osd_type_init, osd_type_fini */
7561 LU_TYPE_INIT_FINI(osd, &osd_key);
7563 struct lu_context_key osd_key = {
7564 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
7565 .lct_init = osd_key_init,
7566 .lct_fini = osd_key_fini,
7567 .lct_exit = osd_key_exit
7571 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
7572 const char *name, struct lu_device *next)
7574 struct osd_device *osd = osd_dev(d);
7576 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname)) >=
7577 sizeof(osd->od_svname))
7579 return osd_procfs_init(osd, name);
7582 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
7584 struct seq_server_site *ss = osd_seq_site(osd);
7589 if (osd->od_is_ost || osd->od_cl_seq != NULL)
7592 if (unlikely(ss == NULL))
7595 OBD_ALLOC_PTR(osd->od_cl_seq);
7596 if (osd->od_cl_seq == NULL)
7599 seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
7600 osd->od_svname, ss->ss_server_seq);
7602 if (ss->ss_node_id == 0) {
7604 * If the OSD on the sequence controller(MDT0), then allocate
7605 * sequence here, otherwise allocate sequence after connected
7606 * to MDT0 (see mdt_register_lwp_callback()).
7608 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
7609 &osd->od_cl_seq->lcs_space, env);
7615 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
7617 if (osd->od_cl_seq == NULL)
7620 seq_client_fini(osd->od_cl_seq);
7621 OBD_FREE_PTR(osd->od_cl_seq);
7622 osd->od_cl_seq = NULL;
7625 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
7629 /* shutdown quota slave instance associated with the device */
7630 if (o->od_quota_slave_md != NULL) {
7631 struct qsd_instance *qsd = o->od_quota_slave_md;
7633 o->od_quota_slave_md = NULL;
7637 if (o->od_quota_slave_dt != NULL) {
7638 struct qsd_instance *qsd = o->od_quota_slave_dt;
7640 o->od_quota_slave_dt = NULL;
7644 osd_fid_fini(env, o);
7645 osd_scrub_cleanup(env, o);
7650 static void osd_umount(const struct lu_env *env, struct osd_device *o)
7654 if (o->od_mnt != NULL) {
7655 shrink_dcache_sb(osd_sb(o));
7656 osd_sync(env, &o->od_dt_dev);
7657 wait_event(o->od_commit_cb_done,
7658 !atomic_read(&o->od_commit_cb_in_flight));
7667 static int osd_mount(const struct lu_env *env,
7668 struct osd_device *o, struct lustre_cfg *cfg)
7670 const char *name = lustre_cfg_string(cfg, 0);
7671 const char *dev = lustre_cfg_string(cfg, 1);
7673 unsigned long page, s_flags, lmd_flags = 0;
7674 struct page *__page;
7675 struct file_system_type *type;
7676 char *options = NULL;
7678 struct osd_thread_info *info = osd_oti_get(env);
7679 struct lu_fid *fid = &info->oti_fid;
7680 struct inode *inode;
7681 int rc = 0, force_over_1024tb = 0;
7685 if (o->od_mnt != NULL)
7688 if (strlen(dev) >= sizeof(o->od_mntdev))
7690 strcpy(o->od_mntdev, dev);
7692 str = lustre_cfg_string(cfg, 2);
7693 s_flags = simple_strtoul(str, NULL, 0);
7694 str = strstr(str, ":");
7696 lmd_flags = simple_strtoul(str + 1, NULL, 0);
7697 opts = lustre_cfg_string(cfg, 3);
7699 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
7700 CERROR("%s: device %s extents feature is not guaranteed to "
7701 "work on big-endian systems. Use \"bigendian_extents\" "
7702 "mount option to override.\n", name, dev);
7706 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7707 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
7708 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");
7711 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
7712 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
7713 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");
7716 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
7717 if (opts != NULL && strstr(opts, "force_over_512tb") != NULL) {
7718 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");
7722 if (opts != NULL && strstr(opts, "force_over_1024tb") != NULL)
7723 force_over_1024tb = 1;
7725 __page = alloc_page(GFP_KERNEL);
7727 GOTO(out, rc = -ENOMEM);
7728 page = (unsigned long)page_address(__page);
7729 options = (char *)page;
7732 /* strip out the options for back compatiblity */
7733 static const char * const sout[] = {
7740 /* strip out option we processed in osd */
7741 "bigendian_extents",
7745 "force_over_1024tb",
7748 strncat(options, opts, PAGE_SIZE);
7749 for (rc = 0, str = options; sout[rc]; ) {
7750 char *op = strstr(str, sout[rc]);
7757 if (op == options || *(op - 1) == ',') {
7758 str = op + strlen(sout[rc]);
7759 if (*str == ',' || *str == '\0') {
7760 *str == ',' ? str++ : str;
7761 memmove(op, str, strlen(str) + 1);
7764 for (str = op; *str != ',' && *str != '\0'; str++)
7768 strncat(options, "user_xattr,acl", PAGE_SIZE);
7771 /* Glom up mount options */
7772 if (*options != '\0')
7773 strncat(options, ",", PAGE_SIZE);
7774 strncat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
7776 type = get_fs_type("ldiskfs");
7778 CERROR("%s: cannot find ldiskfs module\n", name);
7779 GOTO(out, rc = -ENODEV);
7782 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
7783 module_put(type->owner);
7785 if (IS_ERR(o->od_mnt)) {
7786 rc = PTR_ERR(o->od_mnt);
7788 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
7792 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
7793 osd_sb(o)->s_blocksize_bits > 1024ULL << 40 &&
7794 force_over_1024tb == 0) {
7795 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",
7797 GOTO(out_mnt, rc = -EINVAL);
7800 if (lmd_flags & LMD_FLG_DEV_RDONLY) {
7801 LCONSOLE_WARN("%s: not support dev_rdonly on this device",
7804 GOTO(out_mnt, rc = -EOPNOTSUPP);
7807 if (!ldiskfs_has_feature_journal(o->od_mnt->mnt_sb)) {
7808 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
7809 GOTO(out_mnt, rc = -EINVAL);
7812 #ifdef LDISKFS_MOUNT_DIRDATA
7813 if (ldiskfs_has_feature_dirdata(o->od_mnt->mnt_sb))
7814 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
7815 else if (strstr(name, "MDT")) /* don't complain for MGT or OSTs */
7816 CWARN("%s: device %s was upgraded from Lustre-1.x without "
7817 "enabling the dirdata feature. If you do not want to "
7818 "downgrade to Lustre-1.x again, you can enable it via "
7819 "'tune2fs -O dirdata device'\n", name, dev);
7821 inode = osd_sb(o)->s_root->d_inode;
7822 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
7823 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
7825 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
7829 if (lmd_flags & LMD_FLG_NOSCRUB)
7830 o->od_auto_scrub_interval = AS_NEVER;
7832 if (blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev))) {
7833 /* do not use pagecache with flash-backed storage */
7834 o->od_writethrough_cache = 0;
7835 o->od_read_cache = 0;
7846 __free_page(__page);
7851 static struct lu_device *osd_device_fini(const struct lu_env *env,
7852 struct lu_device *d)
7854 struct osd_device *o = osd_dev(d);
7858 osd_index_backup(env, o, false);
7859 osd_shutdown(env, o);
7861 if (o->od_oi_table != NULL)
7862 osd_oi_fini(osd_oti_get(env), o);
7863 osd_obj_map_fini(o);
7869 static int osd_device_init0(const struct lu_env *env,
7870 struct osd_device *o,
7871 struct lustre_cfg *cfg)
7873 struct lu_device *l = osd2lu_dev(o);
7874 struct osd_thread_info *info;
7878 /* if the module was re-loaded, env can loose its keys */
7879 rc = lu_env_refill((struct lu_env *)env);
7882 info = osd_oti_get(env);
7885 l->ld_ops = &osd_lu_ops;
7886 o->od_dt_dev.dd_ops = &osd_dt_ops;
7888 spin_lock_init(&o->od_osfs_lock);
7889 mutex_init(&o->od_otable_mutex);
7890 INIT_LIST_HEAD(&o->od_orphan_list);
7891 INIT_LIST_HEAD(&o->od_index_backup_list);
7892 INIT_LIST_HEAD(&o->od_index_restore_list);
7893 spin_lock_init(&o->od_lock);
7894 o->od_index_backup_policy = LIBP_NONE;
7896 init_waitqueue_head(&o->od_commit_cb_done);
7898 o->od_read_cache = 1;
7899 o->od_writethrough_cache = 1;
7900 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
7901 o->od_readcache_max_iosize = OSD_READCACHE_MAX_IO_MB << 20;
7902 o->od_writethrough_max_iosize = OSD_WRITECACHE_MAX_IO_MB << 20;
7903 o->od_auto_scrub_interval = AS_DEFAULT;
7905 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
7906 sizeof(o->od_svname));
7907 if (cplen >= sizeof(o->od_svname)) {
7912 o->od_index_backup_stop = 0;
7913 o->od_index = -1; /* -1 means index is invalid */
7914 rc = server_name2index(o->od_svname, &o->od_index, NULL);
7915 if (rc == LDD_F_SV_TYPE_OST)
7918 o->od_full_scrub_ratio = OFSR_DEFAULT;
7919 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
7920 rc = osd_mount(env, o, cfg);
7924 /* Can only check block device after mount */
7925 o->od_nonrotational =
7926 blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev));
7928 rc = osd_obj_map_init(env, o);
7932 rc = lu_site_init(&o->od_site, l);
7934 GOTO(out_compat, rc);
7935 o->od_site.ls_bottom_dev = l;
7937 rc = lu_site_init_finish(&o->od_site);
7941 INIT_LIST_HEAD(&o->od_ios_list);
7942 /* setup scrub, including OI files initialization */
7944 rc = osd_scrub_setup(env, o);
7949 rc = osd_procfs_init(o, o->od_svname);
7951 CERROR("%s: can't initialize procfs: rc = %d\n",
7953 GOTO(out_scrub, rc);
7956 LASSERT(l->ld_site->ls_linkage.next != NULL);
7957 LASSERT(l->ld_site->ls_linkage.prev != NULL);
7959 /* initialize quota slave instance */
7960 /* currently it's no need to prepare qsd_instance_md for OST */
7961 if (!o->od_is_ost) {
7962 o->od_quota_slave_md = qsd_init(env, o->od_svname,
7964 o->od_proc_entry, true);
7965 if (IS_ERR(o->od_quota_slave_md)) {
7966 rc = PTR_ERR(o->od_quota_slave_md);
7967 o->od_quota_slave_md = NULL;
7968 GOTO(out_procfs, rc);
7972 o->od_quota_slave_dt = qsd_init(env, o->od_svname, &o->od_dt_dev,
7973 o->od_proc_entry, false);
7975 if (IS_ERR(o->od_quota_slave_dt)) {
7976 if (o->od_quota_slave_md != NULL) {
7977 qsd_fini(env, o->od_quota_slave_md);
7978 o->od_quota_slave_md = NULL;
7981 rc = PTR_ERR(o->od_quota_slave_dt);
7982 o->od_quota_slave_dt = NULL;
7983 GOTO(out_procfs, rc);
7991 osd_scrub_cleanup(env, o);
7993 lu_site_fini(&o->od_site);
7995 osd_obj_map_fini(o);
8002 static struct lu_device *osd_device_alloc(const struct lu_env *env,
8003 struct lu_device_type *t,
8004 struct lustre_cfg *cfg)
8006 struct osd_device *o;
8011 return ERR_PTR(-ENOMEM);
8013 rc = dt_device_init(&o->od_dt_dev, t);
8016 * Because the ctx might be revived in dt_device_init,
8017 * refill the env here
8019 lu_env_refill((struct lu_env *)env);
8020 rc = osd_device_init0(env, o, cfg);
8022 dt_device_fini(&o->od_dt_dev);
8025 if (unlikely(rc != 0))
8028 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
8031 static struct lu_device *osd_device_free(const struct lu_env *env,
8032 struct lu_device *d)
8034 struct osd_device *o = osd_dev(d);
8038 /* XXX: make osd top device in order to release reference */
8039 d->ld_site->ls_top_dev = d;
8040 lu_site_purge(env, d->ld_site, -1);
8041 lu_site_print(env, d->ld_site, &d->ld_site->ls_obj_hash.nelems,
8042 D_ERROR, lu_cdebug_printer);
8043 lu_site_fini(&o->od_site);
8044 dt_device_fini(&o->od_dt_dev);
8049 static int osd_process_config(const struct lu_env *env,
8050 struct lu_device *d, struct lustre_cfg *cfg)
8052 struct osd_device *o = osd_dev(d);
8058 switch (cfg->lcfg_command) {
8060 rc = osd_mount(env, o, cfg);
8064 * For the case LCFG_PRE_CLEANUP is not called in advance,
8065 * that may happend if hit failure during mount process.
8067 osd_index_backup(env, o, false);
8068 lu_dev_del_linkage(d->ld_site, d);
8069 rc = osd_shutdown(env, o);
8072 LASSERT(&o->od_dt_dev);
8073 count = class_modify_config(cfg, PARAM_OSD,
8074 &o->od_dt_dev.dd_kobj);
8076 count = class_modify_config(cfg, PARAM_OST,
8077 &o->od_dt_dev.dd_kobj);
8078 rc = count > 0 ? 0 : count;
8080 case LCFG_PRE_CLEANUP:
8082 osd_index_backup(env, o,
8083 o->od_index_backup_policy != LIBP_NONE);
8093 static int osd_recovery_complete(const struct lu_env *env,
8094 struct lu_device *d)
8096 struct osd_device *osd = osd_dev(d);
8101 if (osd->od_quota_slave_md == NULL && osd->od_quota_slave_dt == NULL)
8105 * start qsd instance on recovery completion, this notifies the quota
8106 * slave code that we are about to process new requests now
8108 rc = qsd_start(env, osd->od_quota_slave_dt);
8109 if (rc == 0 && osd->od_quota_slave_md != NULL)
8110 rc = qsd_start(env, osd->od_quota_slave_md);
8116 * we use exports to track all osd users
8118 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
8119 struct obd_device *obd, struct obd_uuid *cluuid,
8120 struct obd_connect_data *data, void *localdata)
8122 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8123 struct lustre_handle conn;
8128 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
8130 rc = class_connect(&conn, obd, cluuid);
8134 *exp = class_conn2export(&conn);
8136 spin_lock(&osd->od_osfs_lock);
8138 spin_unlock(&osd->od_osfs_lock);
8144 * once last export (we don't count self-export) disappeared
8145 * osd can be released
8147 static int osd_obd_disconnect(struct obd_export *exp)
8149 struct obd_device *obd = exp->exp_obd;
8150 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8151 int rc, release = 0;
8155 /* Only disconnect the underlying layers on the final disconnect. */
8156 spin_lock(&osd->od_osfs_lock);
8158 if (osd->od_connects == 0)
8160 spin_unlock(&osd->od_osfs_lock);
8162 rc = class_disconnect(exp); /* bz 9811 */
8164 if (rc == 0 && release)
8165 class_manual_cleanup(obd);
8169 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
8170 struct lu_device *dev)
8172 struct osd_device *osd = osd_dev(dev);
8173 struct lr_server_data *lsd =
8174 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
8179 if (osd->od_quota_slave_md != NULL) {
8180 /* set up quota slave objects for inode */
8181 result = qsd_prepare(env, osd->od_quota_slave_md);
8186 if (osd->od_quota_slave_dt != NULL) {
8187 /* set up quota slave objects for block */
8188 result = qsd_prepare(env, osd->od_quota_slave_dt);
8194 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
8195 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
8196 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
8197 osd->od_index_in_idif = 1;
8199 osd->od_index_in_idif = 0;
8200 result = osd_register_proc_index_in_idif(osd);
8205 osd->od_index_in_idif = 1;
8209 result = osd_fid_init(env, osd);
8215 * Implementation of lu_device_operations::ldo_fid_alloc() for OSD
8219 * see include/lu_object.h for the details.
8221 static int osd_fid_alloc(const struct lu_env *env, struct lu_device *d,
8222 struct lu_fid *fid, struct lu_object *parent,
8223 const struct lu_name *name)
8225 struct osd_device *osd = osd_dev(d);
8227 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
8230 static const struct lu_object_operations osd_lu_obj_ops = {
8231 .loo_object_init = osd_object_init,
8232 .loo_object_delete = osd_object_delete,
8233 .loo_object_release = osd_object_release,
8234 .loo_object_free = osd_object_free,
8235 .loo_object_print = osd_object_print,
8236 .loo_object_invariant = osd_object_invariant
8239 const struct lu_device_operations osd_lu_ops = {
8240 .ldo_object_alloc = osd_object_alloc,
8241 .ldo_process_config = osd_process_config,
8242 .ldo_recovery_complete = osd_recovery_complete,
8243 .ldo_prepare = osd_prepare,
8244 .ldo_fid_alloc = osd_fid_alloc,
8247 static const struct lu_device_type_operations osd_device_type_ops = {
8248 .ldto_init = osd_type_init,
8249 .ldto_fini = osd_type_fini,
8251 .ldto_start = osd_type_start,
8252 .ldto_stop = osd_type_stop,
8254 .ldto_device_alloc = osd_device_alloc,
8255 .ldto_device_free = osd_device_free,
8257 .ldto_device_init = osd_device_init,
8258 .ldto_device_fini = osd_device_fini
8261 static struct lu_device_type osd_device_type = {
8262 .ldt_tags = LU_DEVICE_DT,
8263 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
8264 .ldt_ops = &osd_device_type_ops,
8265 .ldt_ctx_tags = LCT_LOCAL,
8268 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
8270 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8271 struct super_block *sb = osd_sb(osd);
8273 return (osd->od_mnt == NULL || sb->s_flags & SB_RDONLY);
8277 * lprocfs legacy support.
8279 static const struct obd_ops osd_obd_device_ops = {
8280 .o_owner = THIS_MODULE,
8281 .o_connect = osd_obd_connect,
8282 .o_disconnect = osd_obd_disconnect,
8283 .o_health_check = osd_health_check,
8286 static ssize_t track_declares_assert_show(struct kobject *kobj,
8287 struct attribute *attr,
8290 return sprintf(buf, "%d\n", ldiskfs_track_declares_assert);
8293 static ssize_t track_declares_assert_store(struct kobject *kobj,
8294 struct attribute *attr,
8295 const char *buffer, size_t count)
8297 bool track_declares_assert;
8300 rc = kstrtobool(buffer, &track_declares_assert);
8304 ldiskfs_track_declares_assert = track_declares_assert;
8308 LUSTRE_RW_ATTR(track_declares_assert);
8310 static int __init osd_init(void)
8312 struct kobject *kobj;
8315 BUILD_BUG_ON(BH_DXLock >=
8316 sizeof(((struct buffer_head *)0)->b_state) * 8);
8317 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
8318 /* please, try to keep osd_thread_info smaller than a page */
8319 BUILD_BUG_ON(sizeof(struct osd_thread_info) > PAGE_SIZE);
8324 rc = lu_kmem_init(ldiskfs_caches);
8328 #ifdef CONFIG_KALLSYMS
8329 priv_security_file_alloc =
8330 (void *)kallsyms_lookup_name("security_file_alloc");
8333 rc = class_register_type(&osd_obd_device_ops, NULL, true, NULL,
8334 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
8336 lu_kmem_fini(ldiskfs_caches);
8340 kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
8342 rc = sysfs_create_file(kobj,
8343 &lustre_attr_track_declares_assert.attr);
8346 CWARN("osd-ldiskfs: track_declares_assert failed to register with sysfs\n");
8353 static void __exit osd_exit(void)
8355 struct kobject *kobj;
8357 kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
8359 sysfs_remove_file(kobj,
8360 &lustre_attr_track_declares_assert.attr);
8363 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
8364 lu_kmem_fini(ldiskfs_caches);
8367 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
8368 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
8369 MODULE_VERSION(LUSTRE_VERSION_STRING);
8370 MODULE_LICENSE("GPL");
8372 module_init(osd_init);
8373 module_exit(osd_exit);