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(is_root_inode(parent))) {
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(is_root_inode(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);
1131 if (fid_is_sane(fid) == 0) {
1132 CERROR("%s: invalid FID "DFID"\n", ldev->ld_obd->obd_name,
1138 dev = osd_dev(ldev);
1139 scrub = &dev->od_scrub.os_scrub;
1140 sf = &scrub->os_file;
1141 info = osd_oti_get(env);
1143 oic = &info->oti_cache;
1145 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
1149 * For the object is created as locking anchor, or for the object to
1150 * be created on disk. No need to osd_oi_lookup() at here because FID
1151 * shouldn't never be re-used, if it's really a duplicate FID from
1152 * unexpected reason, we should be able to detect it later by calling
1153 * do_create->osd_oi_insert().
1155 if (conf && conf->loc_flags & LOC_F_NEW)
1156 GOTO(out, result = 0);
1158 /* Search order: 1. per-thread cache. */
1159 if (lu_fid_eq(fid, &oic->oic_fid) && likely(oic->oic_dev == dev)) {
1165 if (!list_empty(&scrub->os_inconsistent_items)) {
1166 /* Search order: 2. OI scrub pending list. */
1167 result = osd_oii_lookup(dev, fid, id);
1173 * The OI mapping in the OI file can be updated by the OI scrub
1174 * when we locate the inode via FID. So it may be not trustable.
1178 /* Search order: 3. OI files. */
1179 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1180 if (result == -ENOENT) {
1181 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
1182 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1183 !ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
1185 GOTO(out, result = 0);
1190 /* -ESTALE is returned if inode of OST object doesn't exist */
1191 if (result == -ESTALE &&
1192 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1193 GOTO(out, result = 0);
1200 obj->oo_inode = NULL;
1201 /* for later passes through checks, not true on first pass */
1202 if (!IS_ERR_OR_NULL(inode))
1205 inode = osd_iget_check(info, dev, fid, id, trusted);
1206 if (!IS_ERR(inode)) {
1207 obj->oo_inode = inode;
1215 result = PTR_ERR(inode);
1216 if (result == -ENOENT || result == -ESTALE)
1217 GOTO(out, result = 0);
1219 if (result != -EREMCHG)
1224 * We still have chance to get the valid inode: for the
1225 * object which is referenced by remote name entry, the
1226 * object on the local MDT will be linked under the dir
1227 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1229 * We do not know whether the object for the given FID
1230 * is referenced by some remote name entry or not, and
1231 * especially for DNE II, a multiple-linked object may
1232 * have many name entries reside on many MDTs.
1234 * To simplify the operation, OSD will not distinguish
1235 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1236 * only happened for the RPC from other MDT during the
1237 * OI scrub, or for the client side RPC with FID only,
1238 * such as FID to path, or from old connected client.
1241 rc1 = osd_lookup_in_remote_parent(info, dev, fid, id);
1245 flags |= SS_AUTO_PARTIAL;
1246 flags &= ~SS_AUTO_FULL;
1251 if (scrub->os_running) {
1252 if (scrub->os_partial_scan && !scrub->os_in_join)
1255 osd_add_oi_cache(info, dev, id, fid);
1256 if (IS_ERR_OR_NULL(inode) || result) {
1257 osd_oii_insert(dev, oic, result == -ENOENT);
1258 GOTO(out, result = -EINPROGRESS);
1262 LASSERT(obj->oo_inode == inode);
1264 osd_oii_insert(dev, oic, true);
1268 if (dev->od_auto_scrub_interval == AS_NEVER) {
1270 GOTO(out, result = -EREMCHG);
1273 LASSERT(obj->oo_inode == inode);
1275 osd_add_oi_cache(info, dev, id, fid);
1280 rc1 = osd_scrub_start(env, dev, flags);
1281 LCONSOLE_WARN("%s: trigger OI scrub by RPC for the " DFID" with flags "
1282 "0x%x, rc = %d\n", osd_name(dev), PFID(fid), flags, rc1);
1283 if (rc1 && rc1 != -EALREADY)
1284 GOTO(out, result = -EREMCHG);
1286 osd_add_oi_cache(info, dev, id, fid);
1287 if (IS_ERR_OR_NULL(inode) || result) {
1288 osd_oii_insert(dev, oic, result == -ENOENT);
1289 GOTO(out, result = -EINPROGRESS);
1293 LASSERT(obj->oo_inode == inode);
1295 osd_oii_insert(dev, oic, true);
1300 if (unlikely(obj->oo_header))
1303 result = osd_check_lma(env, obj);
1307 LASSERTF(id->oii_ino == inode->i_ino &&
1308 id->oii_gen == inode->i_generation,
1309 "locate wrong inode for FID: "DFID", %u/%u => %ld/%u\n",
1310 PFID(fid), id->oii_ino, id->oii_gen,
1311 inode->i_ino, inode->i_generation);
1313 saved_ino = inode->i_ino;
1314 saved_gen = inode->i_generation;
1316 if (unlikely(result == -ENODATA)) {
1318 * If the OI scrub updated the OI mapping by race, it
1319 * must be valid. Trust the inode that has no LMA EA.
1324 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1327 * The OI mapping is still there, the inode is still
1328 * valid. It is just becaues the inode has no LMA EA.
1330 if (saved_ino == id->oii_ino &&
1331 saved_gen == id->oii_gen)
1335 * It is the OI scrub updated the OI mapping by race.
1336 * The new OI mapping must be valid.
1344 * "result == -ENOENT" means that the OI mappinghas been
1345 * removed by race, so the inode belongs to other object.
1347 * Others error can be returned directly.
1349 if (result == -ENOENT) {
1352 obj->oo_inode = NULL;
1357 if (result != -EREMCHG)
1363 * if two OST objects map to the same inode, and inode mode is
1364 * (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666), which means it's
1365 * reserved by precreate, and not written yet, in this case, don't
1366 * set inode for the object whose FID mismatch, so that it can create
1367 * inode and not block precreate.
1369 if (fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) &&
1370 inode->i_mode == (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666)) {
1371 obj->oo_inode = NULL;
1372 GOTO(out, result = 0);
1375 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1377 * "result == -ENOENT" means the cached OI mapping has been removed
1378 * from the OI file by race, above inode belongs to other object.
1380 if (result == -ENOENT) {
1383 obj->oo_inode = NULL;
1384 GOTO(out, result = 0);
1390 if (saved_ino == id->oii_ino && saved_gen == id->oii_gen) {
1396 * It is the OI scrub updated the OI mapping by race.
1397 * The new OI mapping must be valid.
1405 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
1406 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
1408 result = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
1410 if (lma->lma_compat & LMAC_STRIPE_INFO &&
1412 obj->oo_pfid_in_lma = 1;
1413 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
1415 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
1416 } else if (result != -ENODATA) {
1421 obj->oo_compat_dot_created = 1;
1422 obj->oo_compat_dotdot_created = 1;
1424 if (S_ISDIR(inode->i_mode) &&
1425 (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1426 osd_check_lmv(info, dev, inode, oic);
1428 result = osd_attach_jinode(inode);
1433 GOTO(out, result = 0);
1435 LASSERT(!obj->oo_hl_head);
1436 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1438 GOTO(out, result = (!obj->oo_hl_head ? -ENOMEM : 0));
1441 if (result || !obj->oo_inode) {
1442 if (!IS_ERR_OR_NULL(inode))
1445 obj->oo_inode = NULL;
1447 fid_zero(&oic->oic_fid);
1450 LINVRNT(osd_invariant(obj));
1455 * Concurrency: shouldn't matter.
1457 static void osd_object_init0(struct osd_object *obj)
1459 LASSERT(obj->oo_inode != NULL);
1460 obj->oo_dt.do_body_ops = &osd_body_ops;
1461 obj->oo_dt.do_lu.lo_header->loh_attr |=
1462 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1466 * Concurrency: no concurrent access is possible that early in object
1469 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1470 const struct lu_object_conf *conf)
1472 struct osd_object *obj = osd_obj(l);
1475 LINVRNT(osd_invariant(obj));
1477 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
1478 cfs_fail_val == 2) {
1479 struct osd_thread_info *info = osd_oti_get(env);
1480 struct osd_idmap_cache *oic = &info->oti_cache;
1481 /* invalidate thread cache */
1482 memset(&oic->oic_fid, 0, sizeof(oic->oic_fid));
1484 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1485 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1486 l->lo_header->loh_attr |= LOHA_EXISTS;
1490 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1491 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1492 if (result == 0 && obj->oo_inode != NULL) {
1493 struct osd_thread_info *oti = osd_oti_get(env);
1494 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
1496 osd_object_init0(obj);
1497 if (unlikely(obj->oo_header))
1500 result = osd_get_lma(oti, obj->oo_inode,
1501 &oti->oti_obj_dentry, loa);
1504 * Convert LMAI flags to lustre LMA flags
1505 * and cache it to oo_lma_flags
1508 lma_to_lustre_flags(loa->loa_lma.lma_incompat);
1509 } else if (result == -ENODATA) {
1513 obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
1515 LINVRNT(osd_invariant(obj));
1520 * The first part of oxe_buf is xattr name, and is '\0' terminated.
1521 * The left part is for value, binary mode.
1523 struct osd_xattr_entry {
1524 struct list_head oxe_list;
1528 struct rcu_head oxe_rcu;
1532 static int osd_oxc_get(struct osd_object *obj, const char *name,
1535 struct osd_xattr_entry *tmp;
1536 struct osd_xattr_entry *oxe = NULL;
1537 size_t namelen = strlen(name);
1541 list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1542 if (namelen == tmp->oxe_namelen &&
1543 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1550 GOTO(out, rc = -ENOENT);
1552 if (!oxe->oxe_exist)
1553 GOTO(out, rc = -ENODATA);
1556 rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1559 if (buf->lb_buf == NULL)
1562 if (buf->lb_len < rc)
1563 GOTO(out, rc = -ERANGE);
1565 memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1572 static void osd_oxc_free(struct rcu_head *head)
1574 struct osd_xattr_entry *oxe;
1576 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1577 OBD_FREE(oxe, oxe->oxe_len);
1580 static void osd_oxc_add(struct osd_object *obj, const char *name,
1581 const char *buf, int buflen)
1583 struct osd_xattr_entry *oxe;
1584 struct osd_xattr_entry *old = NULL;
1585 struct osd_xattr_entry *tmp;
1586 size_t namelen = strlen(name);
1587 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1589 OBD_ALLOC(oxe, len);
1593 INIT_LIST_HEAD(&oxe->oxe_list);
1595 oxe->oxe_namelen = namelen;
1596 memcpy(oxe->oxe_buf, name, namelen);
1598 LASSERT(buf != NULL);
1599 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1600 oxe->oxe_exist = true;
1602 oxe->oxe_exist = false;
1605 /* this should be rarely called, just remove old and add new */
1606 spin_lock(&obj->oo_guard);
1607 list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1608 if (namelen == tmp->oxe_namelen &&
1609 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1615 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1616 call_rcu(&old->oxe_rcu, osd_oxc_free);
1618 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1620 spin_unlock(&obj->oo_guard);
1623 static void osd_oxc_del(struct osd_object *obj, const char *name)
1625 struct osd_xattr_entry *oxe;
1626 size_t namelen = strlen(name);
1628 spin_lock(&obj->oo_guard);
1629 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1630 if (namelen == oxe->oxe_namelen &&
1631 strncmp(name, oxe->oxe_buf, namelen) == 0) {
1632 list_del_rcu(&oxe->oxe_list);
1633 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1637 spin_unlock(&obj->oo_guard);
1640 static void osd_oxc_fini(struct osd_object *obj)
1642 struct osd_xattr_entry *oxe, *next;
1644 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1645 list_del(&oxe->oxe_list);
1646 OBD_FREE(oxe, oxe->oxe_len);
1651 * Concurrency: no concurrent access is possible that late in object
1654 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1656 struct osd_object *obj = osd_obj(l);
1657 struct lu_object_header *h = obj->oo_header;
1659 LINVRNT(osd_invariant(obj));
1662 dt_object_fini(&obj->oo_dt);
1663 if (obj->oo_hl_head != NULL)
1664 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1665 /* obj doesn't contain an lu_object_header, so we don't need call_rcu */
1668 lu_object_header_free(h);
1672 * Concurrency: no concurrent access is possible that late in object
1675 static void osd_index_fini(struct osd_object *o)
1677 struct iam_container *bag;
1679 if (o->oo_dir != NULL) {
1680 bag = &o->oo_dir->od_container;
1681 if (o->oo_inode != NULL) {
1682 if (bag->ic_object == o->oo_inode)
1683 iam_container_fini(bag);
1685 OBD_FREE_PTR(o->oo_dir);
1691 OSD_TXN_OI_DELETE_CREDITS = 20,
1692 OSD_TXN_INODE_DELETE_CREDITS = 20
1699 #if OSD_THANDLE_STATS
1701 * Set time when the handle is allocated
1703 static void osd_th_alloced(struct osd_thandle *oth)
1705 oth->oth_alloced = ktime_get();
1709 * Set time when the handle started
1711 static void osd_th_started(struct osd_thandle *oth)
1713 oth->oth_started = ktime_get();
1717 * Check whether the we deal with this handle for too long.
1719 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1720 ktime_t alloced, ktime_t started,
1723 ktime_t now = ktime_get();
1725 LASSERT(dev != NULL);
1727 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1728 ktime_us_delta(started, alloced));
1729 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1730 ktime_us_delta(closed, started));
1731 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1732 ktime_us_delta(now, closed));
1734 if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1735 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1736 oth, now, alloced, started, closed);
1737 libcfs_debug_dumpstack(NULL);
1741 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1743 ktime_t __closed = ktime_get(); \
1744 ktime_t __alloced = oth->oth_alloced; \
1745 ktime_t __started = oth->oth_started; \
1748 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1751 #else /* OSD_THANDLE_STATS */
1753 #define osd_th_alloced(h) do {} while(0)
1754 #define osd_th_started(h) do {} while(0)
1755 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1757 #endif /* OSD_THANDLE_STATS */
1760 * Concurrency: doesn't access mutable data.
1762 static int osd_param_is_not_sane(const struct osd_device *dev,
1763 const struct thandle *th)
1765 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1767 return oh->ot_credits > osd_transaction_size(dev);
1771 * Concurrency: shouldn't matter.
1773 static void osd_trans_commit_cb(struct super_block *sb,
1774 struct ldiskfs_journal_cb_entry *jcb, int error)
1776 struct osd_thandle *oh = container_of(jcb, struct osd_thandle, ot_jcb);
1777 struct thandle *th = &oh->ot_super;
1778 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1779 struct osd_device *osd = osd_dev(lud);
1780 struct dt_txn_commit_cb *dcb, *tmp;
1782 LASSERT(oh->ot_handle == NULL);
1785 CERROR("transaction @0x%p commit error: %d\n", th, error);
1787 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_DELAY_TRANS, 40);
1788 /* call per-transaction callbacks if any */
1789 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1791 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1792 "commit callback entry: magic=%x name='%s'\n",
1793 dcb->dcb_magic, dcb->dcb_name);
1794 list_del_init(&dcb->dcb_linkage);
1795 dcb->dcb_func(NULL, th, dcb, error);
1798 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1799 if (atomic_dec_and_test(&osd->od_commit_cb_in_flight))
1800 wake_up(&osd->od_commit_cb_done);
1806 static struct thandle *osd_trans_create(const struct lu_env *env,
1807 struct dt_device *d)
1809 struct osd_thread_info *oti = osd_oti_get(env);
1810 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1811 struct osd_thandle *oh;
1817 CERROR("%s: someone try to start transaction under "
1818 "readonly mode, should be disabled.\n",
1819 osd_name(osd_dt_dev(d)));
1821 RETURN(ERR_PTR(-EROFS));
1824 /* on pending IO in this thread should left from prev. request */
1825 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1827 sb_start_write(osd_sb(osd_dt_dev(d)));
1829 OBD_ALLOC_GFP(oh, sizeof(*oh), GFP_NOFS);
1831 sb_end_write(osd_sb(osd_dt_dev(d)));
1832 RETURN(ERR_PTR(-ENOMEM));
1835 oh->ot_quota_trans = &oti->oti_quota_trans;
1836 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1841 oh->oh_declared_ext = 0;
1842 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1843 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1844 INIT_LIST_HEAD(&oh->ot_trunc_locks);
1847 memset(oti->oti_declare_ops, 0,
1848 sizeof(oti->oti_declare_ops));
1849 memset(oti->oti_declare_ops_cred, 0,
1850 sizeof(oti->oti_declare_ops_cred));
1851 memset(oti->oti_declare_ops_used, 0,
1852 sizeof(oti->oti_declare_ops_used));
1854 oti->oti_ins_cache_depth++;
1859 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1861 struct osd_thread_info *oti = osd_oti_get(env);
1862 struct osd_thandle *oh;
1864 oh = container_of(th, struct osd_thandle, ot_super);
1865 LASSERT(oh != NULL);
1867 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1868 oti->oti_declare_ops[OSD_OT_CREATE],
1869 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1870 oti->oti_declare_ops_used[OSD_OT_CREATE],
1871 oti->oti_declare_ops[OSD_OT_DESTROY],
1872 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1873 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1874 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1875 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1876 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1877 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1878 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1879 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1880 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1881 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1882 oti->oti_declare_ops[OSD_OT_WRITE],
1883 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1884 oti->oti_declare_ops_used[OSD_OT_WRITE],
1885 oti->oti_declare_ops[OSD_OT_PUNCH],
1886 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1887 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1888 oti->oti_declare_ops[OSD_OT_QUOTA],
1889 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1890 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1891 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1892 oti->oti_declare_ops[OSD_OT_INSERT],
1893 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1894 oti->oti_declare_ops_used[OSD_OT_INSERT],
1895 oti->oti_declare_ops[OSD_OT_DELETE],
1896 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1897 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1898 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1899 oti->oti_declare_ops[OSD_OT_REF_ADD],
1900 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1901 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1902 oti->oti_declare_ops[OSD_OT_REF_DEL],
1903 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1904 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1908 * Concurrency: shouldn't matter.
1910 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1913 struct osd_thread_info *oti = osd_oti_get(env);
1914 struct osd_device *dev = osd_dt_dev(d);
1916 struct osd_thandle *oh;
1921 LASSERT(current->journal_info == NULL);
1923 oh = container_of(th, struct osd_thandle, ot_super);
1924 LASSERT(oh != NULL);
1925 LASSERT(oh->ot_handle == NULL);
1927 rc = dt_txn_hook_start(env, d, th);
1931 if (unlikely(osd_param_is_not_sane(dev, th))) {
1932 static unsigned long last_printed;
1933 static int last_credits;
1936 * don't make noise on a tiny testing systems
1937 * actual credits misuse will be caught anyway
1939 if (last_credits != oh->ot_credits &&
1940 time_after(jiffies, last_printed +
1941 cfs_time_seconds(60)) &&
1942 osd_transaction_size(dev) > 512) {
1943 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1944 oh->ot_credits, osd_transaction_size(dev));
1945 osd_trans_dump_creds(env, th);
1946 libcfs_debug_dumpstack(NULL);
1947 last_credits = oh->ot_credits;
1948 last_printed = jiffies;
1951 * XXX Limit the credits to 'max_transaction_buffers', and
1952 * let the underlying filesystem to catch the error if
1953 * we really need so many credits.
1955 * This should be removed when we can calculate the
1956 * credits precisely.
1958 oh->ot_credits = osd_transaction_size(dev);
1959 } else if (ldiskfs_track_declares_assert != 0) {
1961 * reserve few credits to prevent an assertion in JBD
1962 * our debugging mechanism will be able to detected
1963 * overuse. this can help to debug single-update
1966 oh->ot_credits += 10;
1967 if (unlikely(osd_param_is_not_sane(dev, th)))
1968 oh->ot_credits = osd_transaction_size(dev);
1971 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_TXN_START))
1972 GOTO(out, rc = -EIO);
1975 * XXX temporary stuff. Some abstraction layer should
1978 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1982 LASSERT(oti->oti_txns == 0);
1984 atomic_inc(&dev->od_commit_cb_in_flight);
1985 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1996 static int osd_seq_exists(const struct lu_env *env,
1997 struct osd_device *osd, u64 seq)
1999 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2000 struct seq_server_site *ss = osd_seq_site(osd);
2005 LASSERT(ss != NULL);
2006 LASSERT(ss->ss_server_fld != NULL);
2008 rc = osd_fld_lookup(env, osd, seq, range);
2011 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
2012 osd_name(osd), seq, rc);
2016 RETURN(ss->ss_node_id == range->lsr_index);
2019 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
2021 struct dt_txn_commit_cb *dcb;
2022 struct dt_txn_commit_cb *tmp;
2024 /* call per-transaction stop callbacks if any */
2025 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
2027 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
2028 "commit callback entry: magic=%x name='%s'\n",
2029 dcb->dcb_magic, dcb->dcb_name);
2030 list_del_init(&dcb->dcb_linkage);
2031 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
2036 * Concurrency: shouldn't matter.
2038 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
2041 struct osd_thread_info *oti = osd_oti_get(env);
2042 struct osd_thandle *oh;
2043 struct osd_iobuf *iobuf = &oti->oti_iobuf;
2044 struct osd_device *osd = osd_dt_dev(th->th_dev);
2045 struct qsd_instance *qsd = osd_def_qsd(osd);
2046 struct lquota_trans *qtrans;
2047 LIST_HEAD(truncates);
2048 int rc = 0, remove_agents = 0;
2052 oh = container_of(th, struct osd_thandle, ot_super);
2054 remove_agents = oh->ot_remove_agents;
2056 qtrans = oh->ot_quota_trans;
2057 oh->ot_quota_trans = NULL;
2059 /* move locks to local list, stop tx, execute truncates */
2060 list_splice(&oh->ot_trunc_locks, &truncates);
2062 if (oh->ot_handle != NULL) {
2065 handle_t *hdl = oh->ot_handle;
2068 * add commit callback
2069 * notice we don't do this in osd_trans_start()
2070 * as underlying transaction can change during truncate
2072 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
2075 LASSERT(oti->oti_txns == 1);
2078 rc = dt_txn_hook_stop(env, th);
2080 CERROR("%s: failed in transaction hook: rc = %d\n",
2083 osd_trans_stop_cb(oh, rc);
2084 /* hook functions might modify th_sync */
2085 hdl->h_sync = th->th_sync;
2087 oh->ot_handle = NULL;
2088 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
2090 CERROR("%s: failed to stop transaction: rc = %d\n",
2091 osd_name(osd), rc2);
2095 osd_process_truncates(&truncates);
2097 osd_trans_stop_cb(oh, th->th_result);
2101 osd_trunc_unlock_all(env, &truncates);
2103 /* inform the quota slave device that the transaction is stopping */
2104 qsd_op_end(env, qsd, qtrans);
2107 * as we want IO to journal and data IO be concurrent, we don't block
2108 * awaiting data IO completion in osd_do_bio(), instead we wait here
2109 * once transaction is submitted to the journal. all reqular requests
2110 * don't do direct IO (except read/write), thus this wait_event becomes
2113 * IMPORTANT: we have to wait till any IO submited by the thread is
2114 * completed otherwise iobuf may be corrupted by different request
2116 wait_event(iobuf->dr_wait,
2117 atomic_read(&iobuf->dr_numreqs) == 0);
2118 osd_fini_iobuf(osd, iobuf);
2120 rc = iobuf->dr_error;
2122 if (unlikely(remove_agents != 0))
2123 osd_process_scheduled_agent_removals(env, osd);
2125 LASSERT(oti->oti_ins_cache_depth > 0);
2126 oti->oti_ins_cache_depth--;
2127 /* reset OI cache for safety */
2128 if (oti->oti_ins_cache_depth == 0)
2129 oti->oti_ins_cache_used = 0;
2131 sb_end_write(osd_sb(osd));
2136 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
2138 struct osd_thandle *oh = container_of(th, struct osd_thandle,
2141 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
2142 LASSERT(&dcb->dcb_func != NULL);
2143 if (dcb->dcb_flags & DCB_TRANS_STOP)
2144 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
2146 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
2152 * Called just before object is freed. Releases all resources except for
2153 * object itself (that is released by osd_object_free()).
2155 * Concurrency: no concurrent access is possible that late in object
2158 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
2160 struct osd_object *obj = osd_obj(l);
2161 struct qsd_instance *qsd = osd_def_qsd(osd_obj2dev(obj));
2162 struct inode *inode = obj->oo_inode;
2167 LINVRNT(osd_invariant(obj));
2170 * If object is unlinked remove fid->ino mapping from object index.
2173 osd_index_fini(obj);
2178 if (osd_has_index(obj) && obj->oo_dt.do_index_ops == &osd_index_iam_ops)
2179 ldiskfs_set_inode_flag(inode, LDISKFS_INODE_JOURNAL_DATA);
2181 uid = i_uid_read(inode);
2182 gid = i_gid_read(inode);
2183 projid = i_projid_read(inode);
2185 obj->oo_inode = NULL;
2188 /* do not rebalance quota if the caller needs to release memory
2189 * otherwise qsd_refresh_usage() may went into a new ldiskfs
2190 * transaction and risk to deadlock - LU-12178 */
2191 if (current->flags & (PF_MEMALLOC | PF_KSWAPD))
2194 if (!obj->oo_header && qsd) {
2195 struct osd_thread_info *info = osd_oti_get(env);
2196 struct lquota_id_info *qi = &info->oti_qi;
2198 /* Release granted quota to master if necessary */
2199 qi->lqi_id.qid_uid = uid;
2200 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
2202 qi->lqi_id.qid_uid = gid;
2203 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
2205 qi->lqi_id.qid_uid = projid;
2206 qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
2211 * Concurrency: ->loo_object_release() is called under site spin-lock.
2213 static void osd_object_release(const struct lu_env *env,
2214 struct lu_object *l)
2216 struct osd_object *o = osd_obj(l);
2219 * nobody should be releasing a non-destroyed object with nlink=0
2220 * the API allows this, but ldiskfs doesn't like and then report
2221 * this inode as deleted
2223 LASSERT(!(o->oo_destroyed == 0 && o->oo_inode &&
2224 o->oo_inode->i_nlink == 0));
2228 * Concurrency: shouldn't matter.
2230 static int osd_object_print(const struct lu_env *env, void *cookie,
2231 lu_printer_t p, const struct lu_object *l)
2233 struct osd_object *o = osd_obj(l);
2234 struct iam_descr *d;
2236 if (o->oo_dir != NULL)
2237 d = o->oo_dir->od_container.ic_descr;
2240 return (*p)(env, cookie,
2241 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
2243 o->oo_inode ? o->oo_inode->i_ino : 0UL,
2244 o->oo_inode ? o->oo_inode->i_generation : 0,
2245 d ? d->id_ops->id_name : "plain");
2249 * Concurrency: shouldn't matter.
2251 int osd_statfs(const struct lu_env *env, struct dt_device *d,
2252 struct obd_statfs *sfs, struct obd_statfs_info *info)
2254 struct osd_device *osd = osd_dt_dev(d);
2255 struct super_block *sb = osd_sb(osd);
2256 struct kstatfs *ksfs;
2260 if (unlikely(osd->od_mnt == NULL))
2261 return -EINPROGRESS;
2263 /* osd_lproc.c call this without env, allocate ksfs for that case */
2264 if (unlikely(env == NULL)) {
2265 OBD_ALLOC_PTR(ksfs);
2269 ksfs = &osd_oti_get(env)->oti_ksfs;
2272 result = sb->s_op->statfs(sb->s_root, ksfs);
2276 statfs_pack(sfs, ksfs);
2277 if (unlikely(sb->s_flags & SB_RDONLY))
2278 sfs->os_state |= OS_STATFS_READONLY;
2280 sfs->os_state |= osd->od_nonrotational ? OS_STATFS_NONROT : 0;
2282 if (ldiskfs_has_feature_extents(sb))
2283 sfs->os_maxbytes = sb->s_maxbytes;
2285 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2288 * Reserve some space so to avoid fragmenting the filesystem too much.
2289 * Fragmentation not only impacts performance, but can also increase
2290 * metadata overhead significantly, causing grant calculation to be
2293 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2295 BUILD_BUG_ON(OSD_STATFS_RESERVED <= LDISKFS_MAX_BLOCK_SIZE);
2296 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2297 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2298 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2300 sfs->os_blocks -= reserved;
2301 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2302 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2305 if (unlikely(env == NULL))
2311 * Estimate space needed for file creations. We assume the largest filename
2312 * which is 2^64 - 1, hence a filename of 20 chars.
2313 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2315 #ifdef __LDISKFS_DIR_REC_LEN
2316 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2318 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2322 * Concurrency: doesn't access mutable data.
2324 static void osd_conf_get(const struct lu_env *env,
2325 const struct dt_device *dev,
2326 struct dt_device_param *param)
2328 struct osd_device *d = osd_dt_dev(dev);
2329 struct super_block *sb = osd_sb(d);
2330 struct blk_integrity *bi = bdev_get_integrity(sb->s_bdev);
2335 * XXX should be taken from not-yet-existing fs abstraction layer.
2337 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2338 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2339 param->ddp_symlink_max = sb->s_blocksize;
2340 param->ddp_mount_type = LDD_MT_LDISKFS;
2341 if (ldiskfs_has_feature_extents(sb))
2342 param->ddp_maxbytes = sb->s_maxbytes;
2344 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2346 * inode are statically allocated, so per-inode space consumption
2347 * is the space consumed by the directory entry
2349 param->ddp_inodespace = PER_OBJ_USAGE;
2351 * EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2352 * is 128MB) which is unlikely to be hit in real life. Report a smaller
2353 * maximum length to not under-count the actual number of extents
2354 * needed for writing a file if there are sub-optimal block allocations.
2356 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 1;
2357 /* worst-case extent insertion metadata overhead */
2358 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2359 param->ddp_mntopts = 0;
2360 if (test_opt(sb, XATTR_USER))
2361 param->ddp_mntopts |= MNTOPT_USERXATTR;
2362 if (test_opt(sb, POSIX_ACL))
2363 param->ddp_mntopts |= MNTOPT_ACL;
2366 * LOD might calculate the max stripe count based on max_ea_size,
2367 * so we need take account in the overhead as well,
2368 * xattr_header + magic + xattr_entry_head
2370 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2371 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2373 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2374 if (ldiskfs_has_feature_ea_inode(sb))
2375 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2379 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2381 if (param->ddp_max_ea_size > OBD_MAX_EA_SIZE)
2382 param->ddp_max_ea_size = OBD_MAX_EA_SIZE;
2385 * Preferred RPC size for efficient disk IO. 4MB shows good
2386 * all-around performance for ldiskfs, but use bigalloc chunk size
2387 * by default if larger.
2389 #if defined(LDISKFS_CLUSTER_SIZE)
2390 if (LDISKFS_CLUSTER_SIZE(sb) > DT_DEF_BRW_SIZE)
2391 param->ddp_brw_size = LDISKFS_CLUSTER_SIZE(sb);
2394 param->ddp_brw_size = DT_DEF_BRW_SIZE;
2396 param->ddp_t10_cksum_type = 0;
2398 unsigned short interval = blk_integrity_interval(bi);
2399 name = blk_integrity_name(bi);
2407 if (strncmp(name, "T10-DIF-TYPE",
2408 sizeof("T10-DIF-TYPE") - 1) == 0) {
2409 /* also skip "1/3-" at end */
2410 const int type_off = sizeof("T10-DIF-TYPE.");
2411 char type_number = name[type_off - 2];
2413 if (interval != 512 && interval != 4096) {
2414 CERROR("%s: unsupported T10PI sector size %u\n",
2415 d->od_svname, interval);
2416 } else if (type_number != '1' && type_number != '3') {
2417 CERROR("%s: unsupported T10PI type %s\n",
2418 d->od_svname, name);
2419 } else if (strcmp(name + type_off, "CRC") == 0) {
2420 d->od_t10_type = type_number == '1' ?
2421 OSD_T10_TYPE1_CRC : OSD_T10_TYPE3_CRC;
2422 param->ddp_t10_cksum_type = interval == 512 ?
2423 OBD_CKSUM_T10CRC512 :
2425 } else if (strcmp(name + type_off, "IP") == 0) {
2426 d->od_t10_type = type_number == '1' ?
2427 OSD_T10_TYPE1_IP : OSD_T10_TYPE3_IP;
2428 param->ddp_t10_cksum_type = interval == 512 ?
2429 OBD_CKSUM_T10IP512 :
2432 CERROR("%s: unsupported checksum type of T10PI type '%s'\n",
2433 d->od_svname, name);
2437 CERROR("%s: unsupported T10PI type '%s'\n",
2438 d->od_svname, name);
2442 param->ddp_has_lseek_data_hole = true;
2445 static struct super_block *osd_mnt_sb_get(const struct dt_device *d)
2447 return osd_sb(osd_dt_dev(d));
2451 * Concurrency: shouldn't matter.
2453 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2456 struct super_block *s = osd_sb(osd_dt_dev(d));
2459 down_read(&s->s_umount);
2460 rc = s->s_op->sync_fs(s, 1);
2461 up_read(&s->s_umount);
2463 CDEBUG(D_CACHE, "%s: synced OSD: rc = %d\n", osd_dt_dev(d)->od_svname,
2470 * Start commit for OSD device.
2472 * An implementation of dt_commit_async method for OSD device.
2473 * Asychronously starts underlayng fs sync and thereby a transaction
2476 * \param env environment
2477 * \param d dt device
2479 * \see dt_device_operations
2481 static int osd_commit_async(const struct lu_env *env,
2482 struct dt_device *d)
2484 struct super_block *s = osd_sb(osd_dt_dev(d));
2489 CDEBUG(D_HA, "%s: async commit OSD\n", osd_dt_dev(d)->od_svname);
2490 down_read(&s->s_umount);
2491 rc = s->s_op->sync_fs(s, 0);
2492 up_read(&s->s_umount);
2497 static int (*priv_security_file_alloc)(struct file *file);
2499 int osd_security_file_alloc(struct file *file)
2501 if (priv_security_file_alloc)
2502 return priv_security_file_alloc(file);
2507 * Concurrency: shouldn't matter.
2509 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2511 struct super_block *sb = osd_sb(osd_dt_dev(d));
2512 struct block_device *dev = sb->s_bdev;
2513 int rc = -EOPNOTSUPP;
2517 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2518 osd_dt_dev(d)->od_svname, (long)dev, rc);
2524 * Note: we do not count into QUOTA here.
2525 * If we mount with --data_journal we may need more.
2527 const int osd_dto_credits_noquota[DTO_NR] = {
2530 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2531 * SINGLEDATA_TRANS_BLOCKS(8)
2532 * XXX Note: maybe iam need more, since iam have more level than
2535 [DTO_INDEX_INSERT] = 16,
2538 * just modify a single entry, probably merge few within a block
2540 [DTO_INDEX_DELETE] = 1,
2544 [DTO_INDEX_UPDATE] = 16,
2546 * 4(inode, inode bits, groups, GDT)
2547 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2549 [DTO_OBJECT_CREATE] = 4,
2551 * 4(inode, inode bits, groups, GDT)
2552 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2554 [DTO_OBJECT_DELETE] = 4,
2556 * Attr set credits (inode)
2558 [DTO_ATTR_SET_BASE] = 1,
2560 * Xattr set. The same as xattr of EXT3.
2561 * DATA_TRANS_BLOCKS(14)
2562 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2563 * are also counted in. Do not know why?
2565 [DTO_XATTR_SET] = 14,
2567 * credits for inode change during write.
2569 [DTO_WRITE_BASE] = 3,
2571 * credits for single block write.
2573 [DTO_WRITE_BLOCK] = 14,
2575 * Attr set credits for chown.
2576 * This is extra credits for setattr, and it is null without quota
2578 [DTO_ATTR_SET_CHOWN] = 0
2581 static const struct dt_device_operations osd_dt_ops = {
2582 .dt_root_get = osd_root_get,
2583 .dt_statfs = osd_statfs,
2584 .dt_trans_create = osd_trans_create,
2585 .dt_trans_start = osd_trans_start,
2586 .dt_trans_stop = osd_trans_stop,
2587 .dt_trans_cb_add = osd_trans_cb_add,
2588 .dt_conf_get = osd_conf_get,
2589 .dt_mnt_sb_get = osd_mnt_sb_get,
2590 .dt_sync = osd_sync,
2592 .dt_commit_async = osd_commit_async,
2595 static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
2598 struct osd_object *obj = osd_dt_obj(dt);
2599 struct osd_thread_info *oti = osd_oti_get(env);
2601 LINVRNT(osd_invariant(obj));
2603 LASSERT(obj->oo_owner != env);
2604 down_read_nested(&obj->oo_sem, role);
2606 LASSERT(obj->oo_owner == NULL);
2610 static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
2613 struct osd_object *obj = osd_dt_obj(dt);
2614 struct osd_thread_info *oti = osd_oti_get(env);
2616 LINVRNT(osd_invariant(obj));
2618 LASSERT(obj->oo_owner != env);
2619 down_write_nested(&obj->oo_sem, role);
2621 LASSERT(obj->oo_owner == NULL);
2622 obj->oo_owner = env;
2626 static void osd_read_unlock(const struct lu_env *env, struct dt_object *dt)
2628 struct osd_object *obj = osd_dt_obj(dt);
2629 struct osd_thread_info *oti = osd_oti_get(env);
2631 LINVRNT(osd_invariant(obj));
2633 LASSERT(oti->oti_r_locks > 0);
2635 up_read(&obj->oo_sem);
2638 static void osd_write_unlock(const struct lu_env *env, struct dt_object *dt)
2640 struct osd_object *obj = osd_dt_obj(dt);
2641 struct osd_thread_info *oti = osd_oti_get(env);
2643 LINVRNT(osd_invariant(obj));
2645 LASSERT(obj->oo_owner == env);
2646 LASSERT(oti->oti_w_locks > 0);
2648 obj->oo_owner = NULL;
2649 up_write(&obj->oo_sem);
2652 static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
2654 struct osd_object *obj = osd_dt_obj(dt);
2656 LINVRNT(osd_invariant(obj));
2658 return obj->oo_owner == env;
2661 static void osd_inode_getattr(const struct lu_env *env,
2662 struct inode *inode, struct lu_attr *attr)
2664 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2665 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2666 LA_PROJID | LA_FLAGS | LA_NLINK | LA_RDEV |
2667 LA_BLKSIZE | LA_TYPE | LA_BTIME;
2669 attr->la_atime = inode->i_atime.tv_sec;
2670 attr->la_mtime = inode->i_mtime.tv_sec;
2671 attr->la_ctime = inode->i_ctime.tv_sec;
2672 attr->la_btime = LDISKFS_I(inode)->i_crtime.tv_sec;
2673 attr->la_mode = inode->i_mode;
2674 attr->la_size = i_size_read(inode);
2675 attr->la_blocks = inode->i_blocks;
2676 attr->la_uid = i_uid_read(inode);
2677 attr->la_gid = i_gid_read(inode);
2678 attr->la_projid = i_projid_read(inode);
2679 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2680 attr->la_nlink = inode->i_nlink;
2681 attr->la_rdev = inode->i_rdev;
2682 attr->la_blksize = 1 << inode->i_blkbits;
2683 attr->la_blkbits = inode->i_blkbits;
2685 * Ext4 did not transfer inherit flags from raw inode
2686 * to inode flags, and ext4 internally test raw inode
2687 * @i_flags directly. Instead of patching ext4, we do it here.
2689 if (LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL)
2690 attr->la_flags |= LUSTRE_PROJINHERIT_FL;
2693 static int osd_dirent_count(const struct lu_env *env, struct dt_object *dt,
2696 struct osd_object *obj = osd_dt_obj(dt);
2697 const struct dt_it_ops *iops;
2703 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
2704 LASSERT(fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)));
2706 if (obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
2707 *count = obj->oo_dirent_count;
2711 /* directory not initialized yet */
2712 if (!dt->do_index_ops) {
2717 iops = &dt->do_index_ops->dio_it;
2718 it = iops->init(env, dt, LUDA_64BITHASH);
2720 RETURN(PTR_ERR(it));
2722 rc = iops->load(env, it, 0);
2724 if (rc == -ENODATA) {
2731 rc = iops->next(env, it);
2733 for (*count = 0; rc == 0 || rc == -ESTALE; rc = iops->next(env, it)) {
2737 if (iops->key_size(env, it) == 0)
2743 obj->oo_dirent_count = *count;
2748 iops->fini(env, it);
2753 static int osd_attr_get(const struct lu_env *env, struct dt_object *dt,
2754 struct lu_attr *attr)
2756 struct osd_object *obj = osd_dt_obj(dt);
2759 if (unlikely(!dt_object_exists(dt)))
2761 if (unlikely(obj->oo_destroyed))
2764 LASSERT(!dt_object_remote(dt));
2765 LINVRNT(osd_invariant(obj));
2767 spin_lock(&obj->oo_guard);
2768 osd_inode_getattr(env, obj->oo_inode, attr);
2769 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL) {
2770 attr->la_valid |= LA_FLAGS;
2771 attr->la_flags |= LUSTRE_ORPHAN_FL;
2773 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL) {
2774 attr->la_valid |= LA_FLAGS;
2775 attr->la_flags |= LUSTRE_ENCRYPT_FL;
2777 spin_unlock(&obj->oo_guard);
2779 if (S_ISDIR(obj->oo_inode->i_mode) &&
2780 fid_is_namespace_visible(lu_object_fid(&dt->do_lu)))
2781 rc = osd_dirent_count(env, dt, &attr->la_dirent_count);
2786 static int osd_declare_attr_qid(const struct lu_env *env,
2787 struct osd_object *obj,
2788 struct osd_thandle *oh, long long bspace,
2789 qid_t old_id, qid_t new_id, bool enforce,
2790 unsigned int type, bool ignore_edquot)
2793 struct osd_thread_info *info = osd_oti_get(env);
2794 struct lquota_id_info *qi = &info->oti_qi;
2796 qi->lqi_type = type;
2797 /* inode accounting */
2798 qi->lqi_is_blk = false;
2800 /* one more inode for the new id ... */
2801 qi->lqi_id.qid_uid = new_id;
2803 /* Reserve credits for the new id */
2804 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2805 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2810 /* and one less inode for the current id */
2811 qi->lqi_id.qid_uid = old_id;
2813 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2814 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2819 /* block accounting */
2820 qi->lqi_is_blk = true;
2822 /* more blocks for the new id ... */
2823 qi->lqi_id.qid_uid = new_id;
2824 qi->lqi_space = bspace;
2826 * Credits for the new uid has been reserved, re-use "obj"
2827 * to save credit reservation.
2829 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2830 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2835 /* and finally less blocks for the current uid */
2836 qi->lqi_id.qid_uid = old_id;
2837 qi->lqi_space = -bspace;
2838 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2839 if (ignore_edquot && (rc == -EDQUOT || rc == -EINPROGRESS))
2845 static int osd_declare_attr_set(const struct lu_env *env,
2846 struct dt_object *dt,
2847 const struct lu_attr *attr,
2848 struct thandle *handle)
2850 struct osd_thandle *oh;
2851 struct osd_object *obj;
2860 LASSERT(dt != NULL);
2861 LASSERT(handle != NULL);
2863 obj = osd_dt_obj(dt);
2864 LASSERT(osd_invariant(obj));
2866 oh = container_of(handle, struct osd_thandle, ot_super);
2867 LASSERT(oh->ot_handle == NULL);
2869 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2870 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2872 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2873 osd_dto_credits_noquota[DTO_XATTR_SET]);
2875 if (attr == NULL || obj->oo_inode == NULL)
2878 bspace = obj->oo_inode->i_blocks << 9;
2879 bspace = toqb(bspace);
2882 * Changing ownership is always preformed by super user, it should not
2883 * fail with EDQUOT unless required explicitly.
2885 * We still need to call the osd_declare_qid() to calculate the journal
2886 * credits for updating quota accounting files and to trigger quota
2887 * space adjustment once the operation is completed.
2889 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2890 bool ignore_edquot = !(attr->la_flags & LUSTRE_SET_SYNC_FL);
2894 "%s: enforce quota on UID %u, GID %u (quota space is %lld)\n",
2895 osd_ino2name(obj->oo_inode), attr->la_uid,
2896 attr->la_gid, bspace);
2899 uid = i_uid_read(obj->oo_inode);
2900 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2901 rc = osd_declare_attr_qid(env, obj, oh, bspace, uid,
2902 attr->la_uid, enforce, USRQUOTA,
2907 gid = i_gid_read(obj->oo_inode);
2908 CDEBUG(D_QUOTA, "declare uid %d -> %d gid %d -> %d\n", uid,
2909 attr->la_uid, gid, attr->la_gid);
2910 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2911 rc = osd_declare_attr_qid(env, obj, oh, bspace, gid,
2912 attr->la_gid, enforce, GRPQUOTA,
2918 #ifdef HAVE_PROJECT_QUOTA
2919 if (attr->la_valid & LA_PROJID) {
2920 __u32 projid = i_projid_read(obj->oo_inode);
2922 enforce = (attr->la_valid & LA_PROJID) &&
2923 (attr->la_projid != projid);
2924 rc = osd_declare_attr_qid(env, obj, oh, bspace,
2925 (qid_t)projid, (qid_t)attr->la_projid,
2926 enforce, PRJQUOTA, true);
2934 static int osd_inode_setattr(const struct lu_env *env,
2935 struct inode *inode, const struct lu_attr *attr)
2937 __u64 bits = attr->la_valid;
2939 /* Only allow set size for regular file */
2940 if (!S_ISREG(inode->i_mode))
2941 bits &= ~(LA_SIZE | LA_BLOCKS);
2946 if (bits & LA_ATIME)
2947 inode->i_atime = osd_inode_time(inode, attr->la_atime);
2948 if (bits & LA_CTIME)
2949 inode->i_ctime = osd_inode_time(inode, attr->la_ctime);
2950 if (bits & LA_MTIME)
2951 inode->i_mtime = osd_inode_time(inode, attr->la_mtime);
2952 if (bits & LA_SIZE) {
2953 spin_lock(&inode->i_lock);
2954 LDISKFS_I(inode)->i_disksize = attr->la_size;
2955 i_size_write(inode, attr->la_size);
2956 spin_unlock(&inode->i_lock);
2960 * OSD should not change "i_blocks" which is used by quota.
2961 * "i_blocks" should be changed by ldiskfs only.
2964 inode->i_mode = (inode->i_mode & S_IFMT) |
2965 (attr->la_mode & ~S_IFMT);
2967 i_uid_write(inode, attr->la_uid);
2969 i_gid_write(inode, attr->la_gid);
2970 if (bits & LA_PROJID)
2971 i_projid_write(inode, attr->la_projid);
2972 if (bits & LA_NLINK)
2973 set_nlink(inode, attr->la_nlink);
2975 inode->i_rdev = attr->la_rdev;
2977 if (bits & LA_FLAGS) {
2978 /* always keep S_NOCMTIME */
2979 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2981 #if defined(S_ENCRYPTED)
2982 /* Always remove S_ENCRYPTED, because ldiskfs must not be
2983 * aware of encryption status. It is just stored into LMA
2984 * so that it can be forwared to client side.
2986 inode->i_flags &= ~S_ENCRYPTED;
2989 * Ext4 did not transfer inherit flags from
2990 * @inode->i_flags to raw inode i_flags when writing
2991 * flags, we do it explictly here.
2993 if (attr->la_flags & LUSTRE_PROJINHERIT_FL)
2994 LDISKFS_I(inode)->i_flags |= LUSTRE_PROJINHERIT_FL;
2996 LDISKFS_I(inode)->i_flags &= ~LUSTRE_PROJINHERIT_FL;
3001 #ifdef HAVE_PROJECT_QUOTA
3002 static int osd_transfer_project(struct inode *inode, __u32 projid,
3003 struct thandle *handle)
3005 struct super_block *sb = inode->i_sb;
3006 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
3009 struct ldiskfs_iloc iloc;
3010 struct ldiskfs_inode *raw_inode;
3011 struct dquot *transfer_to[LDISKFS_MAXQUOTAS] = { };
3013 if (!ldiskfs_has_feature_project(sb)) {
3014 LASSERT(__kprojid_val(LDISKFS_I(inode)->i_projid)
3015 == LDISKFS_DEF_PROJID);
3016 if (projid != LDISKFS_DEF_PROJID)
3022 if (LDISKFS_INODE_SIZE(sb) <= LDISKFS_GOOD_OLD_INODE_SIZE)
3025 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3026 if (projid_eq(kprojid, LDISKFS_I(inode)->i_projid))
3029 err = ldiskfs_get_inode_loc(inode, &iloc);
3033 raw_inode = ldiskfs_raw_inode(&iloc);
3034 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3035 struct osd_thandle *oh = container_of(handle,
3039 * try to expand inode size automatically.
3041 ldiskfs_mark_inode_dirty(oh->ot_handle, inode);
3042 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3050 dquot_initialize(inode);
3051 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3052 if (transfer_to[PRJQUOTA]) {
3053 err = __dquot_transfer(inode, transfer_to);
3054 dqput(transfer_to[PRJQUOTA]);
3063 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr,
3064 struct thandle *handle)
3068 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
3069 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
3073 "executing dquot_transfer inode %ld uid %d -> %d gid %d -> %d\n",
3074 inode->i_ino, i_uid_read(inode), attr->la_uid,
3075 i_gid_read(inode), attr->la_gid);
3077 dquot_initialize(inode);
3079 if (attr->la_valid & LA_UID)
3080 iattr.ia_valid |= ATTR_UID;
3081 if (attr->la_valid & LA_GID)
3082 iattr.ia_valid |= ATTR_GID;
3083 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
3084 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
3086 rc = dquot_transfer(inode, &iattr);
3088 CERROR("%s: quota transfer failed. Is quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3089 osd_ino2name(inode), rc);
3094 /* Handle project id transfer here properly */
3095 if (attr->la_valid & LA_PROJID &&
3096 attr->la_projid != i_projid_read(inode)) {
3097 #ifdef HAVE_PROJECT_QUOTA
3098 rc = osd_transfer_project(inode, attr->la_projid, handle);
3103 CERROR("%s: quota transfer failed. Is project enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3104 osd_ino2name(inode), rc);
3111 static int osd_attr_set(const struct lu_env *env,
3112 struct dt_object *dt,
3113 const struct lu_attr *attr,
3114 struct thandle *handle)
3116 struct osd_object *obj = osd_dt_obj(dt);
3117 struct inode *inode;
3120 if (!dt_object_exists(dt))
3123 LASSERT(handle != NULL);
3124 LASSERT(!dt_object_remote(dt));
3125 LASSERT(osd_invariant(obj));
3127 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
3129 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) &&
3130 !osd_obj2dev(obj)->od_is_ost) {
3131 struct osd_thread_info *oti = osd_oti_get(env);
3132 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
3133 struct lu_fid *fid1 = &oti->oti_fid;
3134 struct osd_inode_id *id = &oti->oti_id;
3135 struct iam_path_descr *ipd;
3136 struct iam_container *bag;
3137 struct osd_thandle *oh;
3140 fid_cpu_to_be(fid1, fid0);
3141 memset(id, 1, sizeof(*id));
3142 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
3143 fid0)->oi_dir.od_container;
3144 ipd = osd_idx_ipd_get(env, bag);
3145 if (unlikely(ipd == NULL))
3148 oh = container_of(handle, struct osd_thandle, ot_super);
3149 rc = iam_update(oh->ot_handle, bag,
3150 (const struct iam_key *)fid1,
3151 (const struct iam_rec *)id, ipd);
3152 osd_ipd_put(env, bag, ipd);
3153 return(rc > 0 ? 0 : rc);
3156 inode = obj->oo_inode;
3158 rc = osd_quota_transfer(inode, attr, handle);
3162 spin_lock(&obj->oo_guard);
3163 rc = osd_inode_setattr(env, inode, attr);
3164 spin_unlock(&obj->oo_guard);
3168 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3170 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
3172 if (!(attr->la_valid & LA_FLAGS))
3175 /* Let's check if there are extra flags need to be set into LMA */
3176 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
3177 struct osd_thread_info *info = osd_oti_get(env);
3178 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
3180 LASSERT(!obj->oo_pfid_in_lma);
3182 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
3183 &info->oti_ost_attrs);
3187 lma->lma_incompat |=
3188 lustre_to_lma_flags(attr->la_flags);
3189 lustre_lma_swab(lma);
3191 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3193 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
3194 lma, sizeof(*lma), XATTR_REPLACE);
3196 struct osd_device *osd = osd_obj2dev(obj);
3198 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
3199 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
3200 lma->lma_incompat, rc);
3203 attr->la_flags & LUSTRE_LMA_FL_MASKS;
3205 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3212 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
3213 struct osd_object *obj,
3214 const char *name, const int namelen)
3216 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
3219 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
3220 umode_t mode, struct dt_allocation_hint *hint,
3221 struct thandle *th, struct lu_attr *attr)
3224 struct osd_device *osd = osd_obj2dev(obj);
3225 struct osd_thandle *oth;
3226 struct dt_object *parent = NULL;
3227 struct inode *inode;
3228 struct iattr iattr = {
3229 .ia_valid = ATTR_UID | ATTR_GID |
3230 ATTR_CTIME | ATTR_MTIME | ATTR_ATIME,
3231 .ia_ctime.tv_sec = attr->la_ctime,
3232 .ia_mtime.tv_sec = attr->la_mtime,
3233 .ia_atime.tv_sec = attr->la_atime,
3234 .ia_uid = GLOBAL_ROOT_UID,
3235 .ia_gid = GLOBAL_ROOT_GID,
3237 const struct osd_timespec omit = { .tv_nsec = UTIME_OMIT };
3239 if (attr->la_valid & LA_UID)
3240 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
3241 if (attr->la_valid & LA_GID)
3242 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
3244 LINVRNT(osd_invariant(obj));
3245 LASSERT(obj->oo_inode == NULL);
3246 LASSERT(obj->oo_hl_head == NULL);
3248 if (S_ISDIR(mode) && ldiskfs_pdo) {
3250 ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
3251 if (obj->oo_hl_head == NULL)
3255 oth = container_of(th, struct osd_thandle, ot_super);
3256 LASSERT(oth->ot_handle->h_transaction != NULL);
3258 if (hint != NULL && hint->dah_parent != NULL &&
3259 !dt_object_remote(hint->dah_parent))
3260 parent = hint->dah_parent;
3262 /* if a time component is not valid set it to UTIME_OMIT */
3263 if (!(attr->la_valid & LA_CTIME))
3264 iattr.ia_ctime = omit;
3265 if (!(attr->la_valid & LA_MTIME))
3266 iattr.ia_mtime = omit;
3267 if (!(attr->la_valid & LA_ATIME))
3268 iattr.ia_atime = omit;
3270 inode = ldiskfs_create_inode(oth->ot_handle,
3271 parent ? osd_dt_obj(parent)->oo_inode :
3272 osd_sb(osd)->s_root->d_inode,
3274 if (!IS_ERR(inode)) {
3275 /* Do not update file c/mtime in ldiskfs. */
3276 inode->i_flags |= S_NOCMTIME;
3279 * For new created object, it must be consistent,
3280 * and it is unnecessary to scrub against it.
3282 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
3284 obj->oo_inode = inode;
3287 if (obj->oo_hl_head != NULL) {
3288 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
3289 obj->oo_hl_head = NULL;
3291 result = PTR_ERR(inode);
3293 LINVRNT(osd_invariant(obj));
3301 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
3302 struct lu_attr *attr,
3303 struct dt_allocation_hint *hint,
3304 struct dt_object_format *dof,
3308 struct osd_thandle *oth;
3309 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
3311 LASSERT(S_ISDIR(attr->la_mode));
3313 oth = container_of(th, struct osd_thandle, ot_super);
3314 LASSERT(oth->ot_handle->h_transaction != NULL);
3315 if (fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)))
3316 obj->oo_dirent_count = 0;
3317 result = osd_mkfile(info, obj, mode, hint, th, attr);
3322 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
3323 struct lu_attr *attr,
3324 struct dt_allocation_hint *hint,
3325 struct dt_object_format *dof,
3329 struct osd_thandle *oth;
3330 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
3332 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
3334 LASSERT(S_ISREG(attr->la_mode));
3336 oth = container_of(th, struct osd_thandle, ot_super);
3337 LASSERT(oth->ot_handle->h_transaction != NULL);
3339 result = osd_mkfile(info, obj, mode, hint, th, attr);
3341 LASSERT(obj->oo_inode != NULL);
3342 if (feat->dif_flags & DT_IND_VARKEY)
3343 result = iam_lvar_create(obj->oo_inode,
3344 feat->dif_keysize_max,
3346 feat->dif_recsize_max,
3349 result = iam_lfix_create(obj->oo_inode,
3350 feat->dif_keysize_max,
3352 feat->dif_recsize_max,
3358 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
3359 struct lu_attr *attr,
3360 struct dt_allocation_hint *hint,
3361 struct dt_object_format *dof,
3364 LASSERT(S_ISREG(attr->la_mode));
3365 return osd_mkfile(info, obj, (attr->la_mode &
3366 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
3370 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
3371 struct lu_attr *attr,
3372 struct dt_allocation_hint *hint,
3373 struct dt_object_format *dof,
3376 LASSERT(S_ISLNK(attr->la_mode));
3377 return osd_mkfile(info, obj, (attr->la_mode &
3378 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
3382 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
3383 struct lu_attr *attr,
3384 struct dt_allocation_hint *hint,
3385 struct dt_object_format *dof,
3388 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
3391 LINVRNT(osd_invariant(obj));
3392 LASSERT(obj->oo_inode == NULL);
3393 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
3394 S_ISFIFO(mode) || S_ISSOCK(mode));
3396 result = osd_mkfile(info, obj, mode, hint, th, attr);
3398 LASSERT(obj->oo_inode != NULL);
3400 * This inode should be marked dirty for i_rdev. Currently
3401 * that is done in the osd_attr_init().
3403 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
3406 LINVRNT(osd_invariant(obj));
3410 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
3412 struct dt_allocation_hint *hint,
3413 struct dt_object_format *dof,
3416 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
3418 osd_obj_type_f result;
3434 result = osd_mk_index;
3445 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
3446 struct dt_object *parent, struct dt_object *child,
3451 ah->dah_parent = parent;
3452 ah->dah_mode = child_mode;
3454 if (parent != NULL && !dt_object_remote(parent)) {
3455 /* will help to find FID->ino at dt_insert("..") */
3456 struct osd_object *pobj = osd_dt_obj(parent);
3458 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
3462 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
3463 struct lu_attr *attr, struct dt_object_format *dof,
3464 struct thandle *handle)
3466 struct inode *inode = obj->oo_inode;
3467 __u64 valid = attr->la_valid;
3470 attr->la_valid &= ~(LA_TYPE | LA_MODE);
3472 if (dof->dof_type != DFT_NODE)
3473 attr->la_valid &= ~LA_RDEV;
3474 if ((valid & LA_ATIME) && (attr->la_atime == inode->i_atime.tv_sec))
3475 attr->la_valid &= ~LA_ATIME;
3476 if ((valid & LA_CTIME) && (attr->la_ctime == inode->i_ctime.tv_sec))
3477 attr->la_valid &= ~LA_CTIME;
3478 if ((valid & LA_MTIME) && (attr->la_mtime == inode->i_mtime.tv_sec))
3479 attr->la_valid &= ~LA_MTIME;
3481 result = osd_quota_transfer(inode, attr, handle);
3485 if (attr->la_valid != 0) {
3486 result = osd_inode_setattr(info->oti_env, inode, attr);
3488 * The osd_inode_setattr() should always succeed here. The
3489 * only error that could be returned is EDQUOT when we are
3490 * trying to change the UID or GID of the inode. However, this
3491 * should not happen since quota enforcement is no longer
3492 * enabled on ldiskfs (lquota takes care of it).
3494 LASSERTF(result == 0, "%d\n", result);
3495 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3498 attr->la_valid = valid;
3502 * Helper function for osd_create()
3504 * \retval 0, on success
3506 static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
3507 struct lu_attr *attr, struct dt_allocation_hint *hint,
3508 struct dt_object_format *dof, struct thandle *th)
3513 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
3515 /* we drop umask so that permissions we pass are not affected */
3516 umask = current->fs->umask;
3517 current->fs->umask = 0;
3519 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
3521 if (likely(obj->oo_inode != NULL)) {
3522 LASSERT(obj->oo_inode->i_state & I_NEW);
3525 * Unlock the inode before attr initialization to avoid
3526 * unnecessary dqget operations. LU-6378
3528 unlock_new_inode(obj->oo_inode);
3531 if (likely(result == 0)) {
3532 osd_attr_init(info, obj, attr, dof, th);
3533 osd_object_init0(obj);
3536 /* restore previous umask value */
3537 current->fs->umask = umask;
3539 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
3545 * Helper function for osd_create()
3547 * \retval 0, on success
3549 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
3550 const struct lu_fid *fid, struct thandle *th)
3552 struct osd_thread_info *info = osd_oti_get(env);
3553 struct osd_inode_id *id = &info->oti_id;
3554 struct osd_device *osd = osd_obj2dev(obj);
3555 struct osd_thandle *oh;
3558 LASSERT(obj->oo_inode != NULL);
3560 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_OI_ENOSPC))
3563 oh = container_of(th, struct osd_thandle, ot_super);
3564 LASSERT(oh->ot_handle);
3565 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3567 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3568 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3569 OI_CHECK_FLD, NULL);
3570 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP) && osd->od_is_ost) {
3571 struct lu_fid next_fid = *fid;
3573 /* insert next object in advance, and map to the same inode */
3575 if (next_fid.f_oid != 0) {
3576 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3577 osd_oi_insert(info, osd, &next_fid, id, oh->ot_handle,
3578 OI_CHECK_FLD, NULL);
3579 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3583 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3588 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3589 u64 seq, struct lu_seq_range *range)
3591 struct seq_server_site *ss = osd_seq_site(osd);
3593 if (fid_seq_is_idif(seq)) {
3594 fld_range_set_ost(range);
3595 range->lsr_index = idif_ost_idx(seq);
3599 if (!fid_seq_in_fldb(seq)) {
3600 fld_range_set_mdt(range);
3603 * FIXME: If ss is NULL, it suppose not get lsr_index
3606 range->lsr_index = ss->ss_node_id;
3610 LASSERT(ss != NULL);
3611 fld_range_set_any(range);
3612 /* OSD will only do local fld lookup */
3613 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3616 static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
3617 struct lu_attr *attr,
3618 struct dt_allocation_hint *hint,
3619 struct dt_object_format *dof,
3620 struct thandle *handle)
3622 struct osd_thandle *oh;
3627 LASSERT(handle != NULL);
3629 oh = container_of(handle, struct osd_thandle, ot_super);
3630 LASSERT(oh->ot_handle == NULL);
3633 * EA object consumes more credits than regular object: osd_mk_index
3634 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3635 * leaf_node, could involves the block, block bitmap, groups, GDT
3636 * change for each block, so add 4 * 2 credits in that case.
3638 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3639 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3640 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3642 * Reuse idle OI block may cause additional one OI block
3645 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3646 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3647 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP))
3648 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3649 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3651 /* will help to find FID->ino mapping at dt_insert() */
3652 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3660 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid,
3661 attr->la_projid, 1, oh, osd_dt_obj(dt),
3662 NULL, OSD_QID_INODE);
3670 * Called to destroy on-disk representation of the object
3672 * Concurrency: must be locked
3674 static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
3677 struct osd_object *obj = osd_dt_obj(dt);
3678 struct inode *inode = obj->oo_inode;
3679 struct osd_thandle *oh;
3687 oh = container_of(th, struct osd_thandle, ot_super);
3688 LASSERT(oh->ot_handle == NULL);
3690 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3691 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3693 /* For removing agent entry */
3694 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu))
3695 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3698 * Recycle idle OI leaf may cause additional three OI blocks
3701 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3702 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3703 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3704 /* one less inode */
3705 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3706 i_projid_read(inode), -1, oh, obj, NULL,
3710 /* data to be truncated */
3711 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3712 i_projid_read(inode), 0, oh, obj, NULL,
3718 * will help to find FID->ino when this object is being
3721 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3726 static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
3729 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3730 struct osd_object *obj = osd_dt_obj(dt);
3731 struct inode *inode = obj->oo_inode;
3732 struct osd_device *osd = osd_obj2dev(obj);
3733 struct osd_thandle *oh;
3738 oh = container_of(th, struct osd_thandle, ot_super);
3739 LASSERT(oh->ot_handle);
3741 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3743 if (unlikely(fid_is_acct(fid)))
3746 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu)) {
3747 result = osd_delete_from_remote_parent(env, osd, obj, oh, true);
3749 CERROR("%s: remove agent entry "DFID": rc = %d\n",
3750 osd_name(osd), PFID(fid), result);
3753 if (S_ISDIR(inode->i_mode)) {
3754 if (inode->i_nlink > 2)
3755 CERROR("%s: directory "DFID" ino %lu link count is %u at unlink. run e2fsck to repair\n",
3756 osd_name(osd), PFID(fid), inode->i_ino,
3759 spin_lock(&obj->oo_guard);
3761 spin_unlock(&obj->oo_guard);
3762 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3765 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3767 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3769 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3770 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3771 oh->ot_handle, OI_CHECK_FLD);
3773 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3774 /* XXX: add to ext3 orphan list */
3775 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3777 /* not needed in the cache anymore */
3778 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3779 obj->oo_destroyed = 1;
3785 * Put the fid into lustre_mdt_attrs, and then place the structure
3786 * inode's ea. This fid should not be altered during the life time
3789 * \retval +ve, on success
3790 * \retval -ve, on error
3792 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3794 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3795 const struct lu_fid *fid, __u32 compat, __u32 incompat)
3797 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
3798 struct lustre_mdt_attrs *lma = &loa->loa_lma;
3803 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3806 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3809 lustre_loa_init(loa, fid, compat, incompat);
3810 lustre_loa_swab(loa, false);
3813 * For the OST device with 256 bytes inode size by default,
3814 * the PFID EA will be stored together with LMA EA to avoid
3815 * performance trouble. Otherwise the PFID EA can be stored
3816 * independently. LU-8998
3818 if ((compat & LMAC_FID_ON_OST) &&
3819 LDISKFS_INODE_SIZE(inode->i_sb) <= 256)
3820 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3821 sizeof(*loa), XATTR_CREATE);
3823 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3824 sizeof(*lma), XATTR_CREATE);
3826 * LMA may already exist, but we need to check that all the
3827 * desired compat/incompat flags have been added.
3829 if (unlikely(rc == -EEXIST)) {
3830 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3831 XATTR_NAME_LMA, (void *)loa, sizeof(*loa));
3835 if (rc < sizeof(*lma))
3838 lustre_loa_swab(loa, true);
3839 if (lu_fid_eq(fid, &lma->lma_self_fid) &&
3840 ((compat == 0 && incompat == 0) ||
3841 (!(~lma->lma_compat & compat) &&
3842 !(~lma->lma_incompat & incompat))))
3845 lma->lma_self_fid = *fid;
3846 lma->lma_compat |= compat;
3847 lma->lma_incompat |= incompat;
3848 if (rc == sizeof(*lma)) {
3849 lustre_lma_swab(lma);
3850 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3851 sizeof(*lma), XATTR_REPLACE);
3853 lustre_loa_swab(loa, false);
3854 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
3855 sizeof(*loa), XATTR_REPLACE);
3863 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3864 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3865 * To have compatilibility with 1.8 ldiskfs driver we need to have
3866 * magic number at start of fid data.
3867 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3870 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3871 const struct lu_fid *fid)
3873 if (!fid_is_namespace_visible(fid) ||
3874 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3875 param->edp_magic = 0;
3879 param->edp_magic = LDISKFS_LUFID_MAGIC;
3880 param->edp_len = sizeof(struct lu_fid) + 1;
3881 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3885 * Try to read the fid from inode ea into dt_rec.
3887 * \param fid object fid.
3889 * \retval 0 on success
3891 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3892 __u32 ino, struct lu_fid *fid,
3893 struct osd_inode_id *id)
3895 struct osd_thread_info *info = osd_oti_get(env);
3896 struct inode *inode;
3900 osd_id_gen(id, ino, OSD_OII_NOGEN);
3901 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3903 RETURN(PTR_ERR(inode));
3909 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3911 struct inode *parent_dir,
3912 const struct lu_fid *dot_fid,
3913 const struct lu_fid *dot_dot_fid,
3914 struct osd_thandle *oth)
3916 struct ldiskfs_dentry_param *dot_ldp;
3917 struct ldiskfs_dentry_param *dot_dot_ldp;
3918 __u32 saved_nlink = dir->i_nlink;
3921 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_DOTDOT_ENOSPC))
3924 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3925 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3927 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3928 dot_ldp->edp_magic = 0;
3930 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3931 dir, dot_ldp, dot_dot_ldp);
3933 * The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3934 * the subseqent ref_add() will increase the dir->i_nlink
3935 * as 3. That is incorrect for new created directory.
3937 * It looks like hack, because we want to make the OSD API
3938 * to be order-independent for new created directory object
3939 * between dt_insert(..) and ref_add() operations.
3941 * Here, we only restore the in-RAM dir-inode's nlink attr,
3942 * becuase if the nlink attr is not 2, then there will be
3943 * ref_add() called following the dt_insert(..), such call
3944 * will make both the in-RAM and on-disk dir-inode's nlink
3945 * attr to be set as 2. LU-7447
3947 set_nlink(dir, saved_nlink);
3952 * Create an local agent inode for remote entry
3954 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3955 struct osd_device *osd,
3956 struct osd_object *pobj,
3957 const struct lu_fid *fid,
3961 struct osd_thread_info *info = osd_oti_get(env);
3962 struct inode *local;
3963 struct osd_thandle *oh;
3964 struct iattr iattr = {
3965 .ia_valid = ATTR_UID | ATTR_GID |
3966 ATTR_CTIME | ATTR_MTIME | ATTR_ATIME,
3967 .ia_ctime.tv_nsec = UTIME_OMIT,
3968 .ia_mtime.tv_nsec = UTIME_OMIT,
3969 .ia_atime.tv_nsec = UTIME_OMIT,
3970 .ia_uid = GLOBAL_ROOT_UID,
3971 .ia_gid = GLOBAL_ROOT_GID,
3978 oh = container_of(th, struct osd_thandle, ot_super);
3979 LASSERT(oh->ot_handle->h_transaction != NULL);
3981 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode,
3983 if (IS_ERR(local)) {
3984 CERROR("%s: create local error %d\n", osd_name(osd),
3985 (int)PTR_ERR(local));
3990 * restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
3991 * correct gid on remote file, not agent here
3993 local->i_gid = current_fsgid();
3994 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3996 /* e2fsck doesn't like empty symlinks. Store remote FID as symlink.
3997 * That gives e2fsck something to look at and be happy, and allows
3998 * debugging if we need to determine where this symlink came from.
4000 if (S_ISLNK(type)) {
4001 BUILD_BUG_ON(LDISKFS_N_BLOCKS * 4 < FID_LEN + 1);
4002 rc = scnprintf((char *)LDISKFS_I(local)->i_data,
4003 LDISKFS_N_BLOCKS * 4, DFID, PFID(fid));
4005 i_size_write(local, rc);
4006 LDISKFS_I(local)->i_disksize = rc;
4008 unlock_new_inode(local);
4010 /* Agent inode should not have project ID */
4011 #ifdef HAVE_PROJECT_QUOTA
4012 if (LDISKFS_I(pobj->oo_inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
4013 i_projid_read(pobj->oo_inode) != 0) {
4014 rc = osd_transfer_project(local, 0, th);
4016 CERROR("%s: quota transfer failed:. Is project quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
4017 osd_ino2name(local), rc);
4018 RETURN(ERR_PTR(rc));
4022 /* Set special LMA flag for local agent inode */
4023 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
4025 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
4026 osd_name(osd), PFID(fid), rc);
4027 RETURN(ERR_PTR(rc));
4033 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
4034 lu_object_fid(&pobj->oo_dt.do_lu),
4037 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
4038 osd_name(osd), PFID(fid), rc);
4039 RETURN(ERR_PTR(rc));
4046 * when direntry is deleted, we have to take care of possible agent inode
4047 * referenced by that. unfortunately we can't do this at that point:
4048 * iget() within a running transaction leads to deadlock and we better do
4049 * not call that every delete declaration to save performance. so we put
4050 * a potention agent inode on a list and process that once the transaction
4051 * is over. Notice it's not any worse in terms of real orphans as regular
4052 * object destroy doesn't put inodes on the on-disk orphan list. this should
4053 * be addressed separately
4055 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
4056 struct osd_thandle *oh,
4059 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
4060 struct osd_obj_orphan *oor;
4067 oor->oor_env = (struct lu_env *)env;
4068 spin_lock(&osd->od_osfs_lock);
4069 list_add(&oor->oor_list, &osd->od_orphan_list);
4070 spin_unlock(&osd->od_osfs_lock);
4072 oh->ot_remove_agents = 1;
4078 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
4079 struct osd_device *osd)
4081 struct osd_thread_info *info = osd_oti_get(env);
4082 struct osd_obj_orphan *oor, *tmp;
4083 struct osd_inode_id id;
4085 struct inode *inode;
4090 spin_lock(&osd->od_osfs_lock);
4091 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
4092 if (oor->oor_env == env)
4093 list_move(&oor->oor_list, &list);
4095 spin_unlock(&osd->od_osfs_lock);
4097 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
4101 list_del(&oor->oor_list);
4104 osd_id_gen(&id, ino, OSD_OII_NOGEN);
4105 inode = osd_iget_fid(info, osd, &id, &fid);
4109 if (!osd_remote_fid(env, osd, &fid)) {
4114 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
4116 mark_inode_dirty(inode);
4117 ldiskfs_journal_stop(jh);
4125 * OSD layer object create function for OST objects (b=11826).
4127 * The FID is inserted into inode xattr here.
4129 * \retval 0, on success
4130 * \retval -ve, on error
4132 static int osd_create(const struct lu_env *env, struct dt_object *dt,
4133 struct lu_attr *attr, struct dt_allocation_hint *hint,
4134 struct dt_object_format *dof, struct thandle *th)
4136 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
4137 struct osd_object *obj = osd_dt_obj(dt);
4138 struct osd_thread_info *info = osd_oti_get(env);
4139 int result, on_ost = 0;
4143 if (dt_object_exists(dt))
4146 LINVRNT(osd_invariant(obj));
4147 LASSERT(!dt_object_remote(dt));
4148 LASSERT(osd_is_write_locked(env, obj));
4149 LASSERT(th != NULL);
4151 if (unlikely(fid_is_acct(fid)))
4153 * Quota files can't be created from the kernel any more,
4154 * 'tune2fs -O quota' will take care of creating them
4158 result = __osd_create(info, obj, attr, hint, dof, th);
4160 if (fid_is_idif(fid) &&
4161 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
4162 struct lu_fid *tfid = &info->oti_fid;
4163 struct ost_id *oi = &info->oti_ostid;
4165 fid_to_ostid(fid, oi);
4166 ostid_to_fid(tfid, oi, 0);
4168 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
4169 LMAC_FID_ON_OST, 0);
4171 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
4173 result = osd_ea_fid_set(info, obj->oo_inode, fid,
4174 on_ost ? LMAC_FID_ON_OST : 0,
4177 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
4178 obj->oo_dt.do_body_ops = &osd_body_ops;
4181 if (!result && !CFS_FAIL_CHECK(OBD_FAIL_OSD_NO_OI_ENTRY)) {
4182 struct inode *inode = obj->oo_inode;
4184 result = __osd_oi_insert(env, obj, fid, th);
4185 if (result && inode) {
4186 spin_lock(&obj->oo_guard);
4188 spin_unlock(&obj->oo_guard);
4189 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4190 ldiskfs_set_inode_state(inode,
4191 LDISKFS_STATE_LUSTRE_DESTROY);
4193 obj->oo_inode = NULL;
4198 * a small optimization - dt_insert() isn't usually applied
4199 * to OST objects, so we don't need to cache OI mapping for
4202 if (result == 0 && on_ost == 0) {
4203 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4205 result = osd_idc_find_and_init(env, osd, obj);
4206 LASSERT(result == 0);
4209 LASSERT(ergo(result == 0,
4210 dt_object_exists(dt) && !dt_object_remote(dt)));
4211 LINVRNT(osd_invariant(obj));
4215 static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
4216 struct thandle *handle)
4218 struct osd_thandle *oh;
4221 /* it's possible that object doesn't exist yet */
4222 LASSERT(handle != NULL);
4224 oh = container_of(handle, struct osd_thandle, ot_super);
4225 LASSERT(oh->ot_handle == NULL);
4227 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
4228 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
4230 rc = osd_idc_find_and_init(env, osd_dev(dt->do_lu.lo_dev),
4237 * Concurrency: @dt is write locked.
4239 static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
4242 struct osd_object *obj = osd_dt_obj(dt);
4243 struct inode *inode = obj->oo_inode;
4244 struct osd_thandle *oh;
4247 if (!dt_object_exists(dt) || obj->oo_destroyed)
4250 LINVRNT(osd_invariant(obj));
4251 LASSERT(!dt_object_remote(dt));
4252 LASSERT(osd_is_write_locked(env, obj));
4253 LASSERT(th != NULL);
4255 oh = container_of(th, struct osd_thandle, ot_super);
4256 LASSERT(oh->ot_handle != NULL);
4258 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
4260 CDEBUG(D_INODE, DFID" increase nlink %d\n",
4261 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
4263 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
4264 * (65000) subdirectories by storing "1" in i_nlink if the link count
4265 * would otherwise overflow. Directory tranversal tools understand
4266 * that (st_nlink == 1) indicates that the filesystem dose not track
4267 * hard links count on the directory, and will not abort subdirectory
4268 * scanning early once (st_nlink - 2) subdirs have been found.
4270 * This also has to properly handle the case of inodes with nlink == 0
4271 * in case they are being linked into the PENDING directory
4273 spin_lock(&obj->oo_guard);
4274 if (unlikely(inode->i_nlink == 0))
4275 /* inc_nlink from 0 may cause WARN_ON */
4276 set_nlink(inode, 1);
4278 ldiskfs_inc_count(oh->ot_handle, inode);
4279 if (!S_ISDIR(inode->i_mode))
4280 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
4282 spin_unlock(&obj->oo_guard);
4284 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4285 LINVRNT(osd_invariant(obj));
4287 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
4292 static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
4293 struct thandle *handle)
4295 struct osd_thandle *oh;
4297 if (!dt_object_exists(dt))
4300 LASSERT(!dt_object_remote(dt));
4301 LASSERT(handle != NULL);
4303 oh = container_of(handle, struct osd_thandle, ot_super);
4304 LASSERT(oh->ot_handle == NULL);
4306 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
4307 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
4313 * Concurrency: @dt is write locked.
4315 static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
4318 struct osd_object *obj = osd_dt_obj(dt);
4319 struct inode *inode = obj->oo_inode;
4320 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4321 struct osd_thandle *oh;
4323 if (!dt_object_exists(dt))
4326 LINVRNT(osd_invariant(obj));
4327 LASSERT(!dt_object_remote(dt));
4328 LASSERT(osd_is_write_locked(env, obj));
4329 LASSERT(th != NULL);
4331 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_REF_DEL))
4334 oh = container_of(th, struct osd_thandle, ot_super);
4335 LASSERT(oh->ot_handle != NULL);
4337 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
4339 spin_lock(&obj->oo_guard);
4341 * That can be result of upgrade from old Lustre version and
4342 * applied only to local files. Just skip this ref_del call.
4343 * ext4_unlink() only treats this as a warning, don't LASSERT here.
4345 if (inode->i_nlink == 0) {
4346 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
4347 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
4348 ", maybe an upgraded file? (LU-3915)\n",
4349 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
4350 spin_unlock(&obj->oo_guard);
4354 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
4355 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
4357 ldiskfs_dec_count(oh->ot_handle, inode);
4358 spin_unlock(&obj->oo_guard);
4360 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4361 LINVRNT(osd_invariant(obj));
4363 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
4369 * Concurrency: @dt is read locked.
4371 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
4372 struct lu_buf *buf, const char *name)
4374 struct osd_object *obj = osd_dt_obj(dt);
4375 struct inode *inode = obj->oo_inode;
4376 struct osd_thread_info *info = osd_oti_get(env);
4377 struct dentry *dentry = &info->oti_obj_dentry;
4378 bool cache_xattr = false;
4383 /* version get is not real XATTR but uses xattr API */
4384 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4385 dt_obj_version_t *ver = buf->lb_buf;
4388 * for version we are just using xattr API but change inode
4391 if (buf->lb_len == 0)
4392 return sizeof(dt_obj_version_t);
4394 if (buf->lb_len < sizeof(dt_obj_version_t))
4397 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
4398 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4400 *ver = LDISKFS_I(inode)->i_fs_version;
4402 return sizeof(dt_obj_version_t);
4405 if (!dt_object_exists(dt))
4408 LASSERT(!dt_object_remote(dt));
4409 LASSERT(inode->i_op != NULL);
4410 #ifdef HAVE_IOP_XATTR
4411 LASSERT(inode->i_op->getxattr != NULL);
4414 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
4415 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
4419 rc = osd_oxc_get(obj, name, buf);
4424 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
4425 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4426 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4427 struct filter_fid *ff;
4428 struct ost_layout *ol;
4430 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4434 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
4437 if (buf->lb_len == 0 || !buf->lb_buf)
4440 if (buf->lb_len < rc)
4444 ol = &ff->ff_layout;
4445 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
4446 PFID_STRIPE_IDX_BITS);
4447 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
4448 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
4449 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
4450 if (lma->lma_compat & LMAC_COMP_INFO) {
4451 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
4452 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
4453 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
4455 ol->ol_comp_start = 0;
4456 ol->ol_comp_end = 0;
4460 rc = __osd_xattr_get(inode, dentry, name,
4461 buf->lb_buf, buf->lb_len);
4465 if (rc == -ENOENT || rc == -ENODATA)
4466 osd_oxc_add(obj, name, NULL, 0);
4467 else if (rc > 0 && buf->lb_buf != NULL)
4468 osd_oxc_add(obj, name, buf->lb_buf, rc);
4474 static int osd_declare_xattr_set(const struct lu_env *env,
4475 struct dt_object *dt,
4476 const struct lu_buf *buf, const char *name,
4477 int fl, struct thandle *handle)
4479 struct osd_thandle *oh;
4481 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4483 LASSERT(handle != NULL);
4485 oh = container_of(handle, struct osd_thandle, ot_super);
4486 LASSERT(oh->ot_handle == NULL);
4488 if (strcmp(name, XATTR_NAME_LMA) == 0) {
4490 * For non-upgrading case, the LMA is set first and
4491 * usually fit inode. But for upgrade case, the LMA
4492 * may be in another separated EA block.
4494 if (dt_object_exists(dt)) {
4495 if (fl == LU_XATTR_REPLACE)
4500 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4502 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4503 /* We may need to delete the old PFID EA. */
4504 credits = LDISKFS_MAXQUOTAS_DEL_BLOCKS(sb);
4505 if (fl == LU_XATTR_REPLACE)
4511 * If some name entry resides on remote MDT, then will create
4512 * agent entry under remote parent. On the other hand, if the
4513 * remote entry will be removed, then related agent entry may
4514 * need to be removed from the remote parent. So there may be
4515 * kinds of cases, let's declare enough credits. The credits
4516 * for create agent entry is enough for remove case.
4518 if (strcmp(name, XATTR_NAME_LINK) == 0) {
4519 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4520 if (dt_object_exists(dt))
4521 credits += 1; /* For updating LMA */
4525 credits += osd_dto_credits_noquota[DTO_XATTR_SET];
4530 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
4532 * learn xattr size from osd_xattr_get if
4533 * attribute has not been read yet
4535 buflen = __osd_xattr_get(
4536 osd_dt_obj(dt)->oo_inode,
4537 &osd_oti_get(env)->oti_obj_dentry,
4542 buflen = buf->lb_len;
4545 if (buflen > sb->s_blocksize) {
4546 credits += osd_calc_bkmap_credits(
4548 (buflen + sb->s_blocksize - 1) >>
4549 sb->s_blocksize_bits);
4553 * xattr set may involve inode quota change, reserve credits for
4554 * dquot_initialize()
4556 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4559 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
4564 static int osd_xattr_set_pfid(const struct lu_env *env, struct osd_object *obj,
4565 const struct lu_buf *buf, int fl,
4566 struct thandle *handle)
4568 struct osd_thread_info *info = osd_oti_get(env);
4569 struct dentry *dentry = &info->oti_obj_dentry;
4570 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4571 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4572 struct inode *inode = obj->oo_inode;
4573 struct filter_fid *ff = buf->lb_buf;
4574 struct ost_layout *ol = &ff->ff_layout;
4575 int flags = XATTR_REPLACE;
4580 if (buf->lb_len != sizeof(*ff) && buf->lb_len != sizeof(struct lu_fid))
4583 rc = osd_get_lma(info, inode, dentry, loa);
4584 if (rc == -ENODATA) {
4585 /* Usually for upgarding from old device */
4586 lustre_loa_init(loa, lu_object_fid(&obj->oo_dt.do_lu),
4587 LMAC_FID_ON_OST, 0);
4588 flags = XATTR_CREATE;
4593 if (!rc && lma->lma_compat & LMAC_STRIPE_INFO) {
4594 if ((fl & LU_XATTR_CREATE) && !(fl & LU_XATTR_REPLACE))
4597 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256) {
4598 /* Separate PFID EA from LMA */
4599 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4600 lustre_lma_swab(lma);
4601 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4602 sizeof(*lma), XATTR_REPLACE);
4604 obj->oo_pfid_in_lma = 0;
4605 rc = LU_XATTR_CREATE;
4611 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256)
4615 * Old client does not send stripe information,
4616 * then store the PFID EA on disk separatedly.
4618 if (unlikely(buf->lb_len == sizeof(struct lu_fid) ||
4619 ol->ol_stripe_size == 0))
4622 /* Remove old PFID EA entry firstly. */
4623 dquot_initialize(inode);
4624 rc = ll_vfs_removexattr(dentry, inode, XATTR_NAME_FID);
4625 if (rc == -ENODATA) {
4626 if ((fl & LU_XATTR_REPLACE) && !(fl & LU_XATTR_CREATE))
4633 fid_le_to_cpu(&loa->loa_parent_fid, &ff->ff_parent);
4634 if (likely(ol->ol_stripe_size != 0)) {
4635 loa->loa_parent_fid.f_ver |= le32_to_cpu(ol->ol_stripe_count) <<
4636 PFID_STRIPE_IDX_BITS;
4637 loa->loa_stripe_size = le32_to_cpu(ol->ol_stripe_size);
4638 lma->lma_compat |= LMAC_STRIPE_INFO;
4639 if (ol->ol_comp_id != 0) {
4640 loa->loa_comp_id = le32_to_cpu(ol->ol_comp_id);
4641 loa->loa_comp_start = le64_to_cpu(ol->ol_comp_start);
4642 loa->loa_comp_end = le64_to_cpu(ol->ol_comp_end);
4643 lma->lma_compat |= LMAC_COMP_INFO;
4647 lustre_loa_swab(loa, false);
4649 /* Store the PFID EA inside LMA. */
4650 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa, sizeof(*loa),
4653 obj->oo_pfid_in_lma = 1;
4659 * In DNE environment, the object (in spite of regular file or directory)
4660 * and its name entry may reside on different MDTs. Under such case, we will
4661 * create an agent entry on the MDT where the object resides. The agent entry
4662 * references the object locally, that makes the object to be visible to the
4663 * userspace when mounted as 'ldiskfs' directly. Then the userspace tools,
4664 * such as 'tar' can handle the object properly.
4666 * We handle the agent entry during set linkEA that is the common interface
4667 * for both regular file and directroy, can handle kinds of cases, such as
4668 * create/link/unlink/rename, and so on.
4670 * NOTE: we can NOT do that when ea_{insert,delete} that is only for directory.
4672 * XXX: There are two known issues:
4673 * 1. For one object, we will create at most one agent entry even if there
4674 * may be more than one cross-MDTs hard links on the object. So the local
4675 * e2fsck may claim that the object's nlink is larger than the name entries
4676 * that reference such inode. And in further, the e2fsck will fix the nlink
4677 * attribute to match the local references. Then it will cause the object's
4678 * nlink attribute to be inconsistent with the global references. it is bad
4679 * but not fatal. The ref_del() can handle the zero-referenced case. On the
4680 * other hand, the global namespace LFSCK can repair the object's attribute
4681 * according to the linkEA.
4682 * 2. There may be too many hard links on the object as to its linkEA overflow,
4683 * then the linkEA entry for cross-MDTs reference may be discarded. If such
4684 * case happened, then at this point, we do not know whether there are some
4685 * cross-MDTs reference. But there are local references, it guarantees that
4686 * object is visible to userspace when mounted as 'ldiskfs'. That is enough.
4688 static int osd_xattr_handle_linkea(const struct lu_env *env,
4689 struct osd_device *osd,
4690 struct osd_object *obj,
4691 const struct lu_buf *buf,
4692 struct thandle *handle)
4694 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
4695 struct lu_fid *tfid = &osd_oti_get(env)->oti_fid3;
4696 struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
4697 struct lu_name tmpname;
4698 struct osd_thandle *oh;
4700 bool remote = false;
4704 oh = container_of(handle, struct osd_thandle, ot_super);
4705 LASSERT(oh->ot_handle != NULL);
4707 rc = linkea_init_with_rec(&ldata);
4709 linkea_first_entry(&ldata);
4710 while (ldata.ld_lee != NULL && !remote) {
4711 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
4713 if (osd_remote_fid(env, osd, tfid) > 0)
4716 linkea_next_entry(&ldata);
4718 } else if (rc == -ENODATA) {
4724 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
4725 rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
4727 CERROR("%s: failed to remove agent entry for "DFID
4728 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4729 } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
4730 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4732 CERROR("%s: failed to create agent entry for "DFID
4733 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4740 * Concurrency: @dt is write locked.
4742 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
4743 const struct lu_buf *buf, const char *name, int fl,
4744 struct thandle *handle)
4746 struct osd_object *obj = osd_dt_obj(dt);
4747 struct osd_device *osd = osd_obj2dev(obj);
4748 struct inode *inode = obj->oo_inode;
4749 struct osd_thread_info *info = osd_oti_get(env);
4759 /* version set is not real XATTR */
4760 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4761 dt_obj_version_t *version = buf->lb_buf;
4764 * for version we are just using xattr API but change inode
4767 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
4769 CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
4770 *version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4772 LDISKFS_I(inode)->i_fs_version = *version;
4774 * Version is set after all inode operations are finished,
4775 * so we should mark it dirty here
4777 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4782 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
4783 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
4786 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4789 * For the OST device with 256 bytes inode size by default,
4790 * the PFID EA will be stored together with LMA EA to avoid
4791 * performance trouble. Otherwise the PFID EA can be stored
4792 * independently. LU-8998
4794 if (strcmp(name, XATTR_NAME_FID) == 0 && osd->od_is_ost &&
4795 (LDISKFS_INODE_SIZE(inode->i_sb) <= 256 || obj->oo_pfid_in_lma)) {
4796 LASSERT(buf->lb_buf);
4798 fl = osd_xattr_set_pfid(env, obj, buf, fl, handle);
4801 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
4802 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4803 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4805 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4809 lma->lma_incompat |= LMAI_STRIPED;
4810 lustre_lma_swab(lma);
4811 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4812 sizeof(*lma), XATTR_REPLACE);
4815 } else if (strcmp(name, XATTR_NAME_LINK) == 0) {
4816 LASSERT(!osd->od_is_ost);
4818 rc = osd_xattr_handle_linkea(env, osd, obj, buf, handle);
4823 if (fl & LU_XATTR_REPLACE)
4824 fs_flags |= XATTR_REPLACE;
4826 if (fl & LU_XATTR_CREATE)
4827 fs_flags |= XATTR_CREATE;
4829 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, len, fs_flags);
4830 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4833 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4834 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4835 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
4841 * Concurrency: @dt is read locked.
4843 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
4844 const struct lu_buf *buf)
4846 struct osd_object *obj = osd_dt_obj(dt);
4847 struct inode *inode = obj->oo_inode;
4848 struct osd_thread_info *info = osd_oti_get(env);
4849 struct dentry *dentry = &info->oti_obj_dentry;
4851 if (!dt_object_exists(dt))
4854 LASSERT(!dt_object_remote(dt));
4855 LASSERT(inode->i_op != NULL);
4856 LASSERT(inode->i_op->listxattr != NULL);
4858 dentry->d_inode = inode;
4859 dentry->d_sb = inode->i_sb;
4860 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
4863 static int osd_declare_xattr_del(const struct lu_env *env,
4864 struct dt_object *dt, const char *name,
4865 struct thandle *handle)
4867 struct osd_thandle *oh;
4868 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4870 LASSERT(!dt_object_remote(dt));
4871 LASSERT(handle != NULL);
4873 oh = container_of(handle, struct osd_thandle, ot_super);
4874 LASSERT(oh->ot_handle == NULL);
4876 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
4877 osd_dto_credits_noquota[DTO_XATTR_SET]);
4879 * xattr del may involve inode quota change, reserve credits for
4880 * dquot_initialize()
4882 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4888 * Concurrency: @dt is write locked.
4890 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
4891 const char *name, struct thandle *handle)
4893 struct osd_object *obj = osd_dt_obj(dt);
4894 struct inode *inode = obj->oo_inode;
4895 struct osd_thread_info *info = osd_oti_get(env);
4896 struct dentry *dentry = &info->oti_obj_dentry;
4899 if (!dt_object_exists(dt))
4902 LASSERT(!dt_object_remote(dt));
4903 LASSERT(inode->i_op != NULL);
4904 LASSERT(handle != NULL);
4905 #ifdef HAVE_IOP_XATTR
4906 LASSERT(inode->i_op->removexattr != NULL);
4909 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4911 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
4912 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
4914 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
4915 &info->oti_ost_attrs);
4917 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
4919 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4920 lustre_lma_swab(lma);
4921 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4922 sizeof(*lma), XATTR_REPLACE);
4924 obj->oo_pfid_in_lma = 0;
4927 dquot_initialize(inode);
4928 dentry->d_inode = inode;
4929 dentry->d_sb = inode->i_sb;
4930 rc = ll_vfs_removexattr(dentry, inode, name);
4933 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4936 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4937 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4938 osd_oxc_del(obj, name);
4943 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
4944 __u64 start, __u64 end)
4946 struct osd_object *obj = osd_dt_obj(dt);
4947 struct inode *inode = obj->oo_inode;
4948 struct file *file = osd_quasi_file(env, inode);
4953 rc = vfs_fsync_range(file, start, end, 0);
4958 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4963 static bool osd_check_stale(struct dt_object *dt)
4971 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4972 const struct dt_index_features *feat)
4974 struct iam_descr *descr;
4976 if (osd_object_is_root(o))
4977 return feat == &dt_directory_features;
4979 LASSERT(o->oo_dir != NULL);
4981 descr = o->oo_dir->od_container.ic_descr;
4982 if (feat == &dt_directory_features) {
4983 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4988 return feat->dif_keysize_min <= descr->id_key_size &&
4989 descr->id_key_size <= feat->dif_keysize_max &&
4990 feat->dif_recsize_min <= descr->id_rec_size &&
4991 descr->id_rec_size <= feat->dif_recsize_max &&
4992 !(feat->dif_flags & (DT_IND_VARKEY |
4993 DT_IND_VARREC | DT_IND_NONUNQ)) &&
4994 ergo(feat->dif_flags & DT_IND_UPDATE,
4995 1 /* XXX check that object (and fs) is writable */);
4999 static int osd_iam_container_init(const struct lu_env *env,
5000 struct osd_object *obj,
5001 struct osd_directory *dir)
5003 struct iam_container *bag = &dir->od_container;
5006 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
5010 result = iam_container_setup(bag);
5012 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
5014 iam_container_fini(bag);
5021 * Concurrency: no external locking is necessary.
5023 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
5024 const struct dt_index_features *feat)
5028 struct osd_object *obj = osd_dt_obj(dt);
5030 LINVRNT(osd_invariant(obj));
5032 if (osd_object_is_root(obj)) {
5033 dt->do_index_ops = &osd_index_ea_ops;
5035 } else if (feat == &dt_directory_features) {
5036 dt->do_index_ops = &osd_index_ea_ops;
5037 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
5042 } else if (unlikely(feat == &dt_otable_features)) {
5043 dt->do_index_ops = &osd_otable_ops;
5045 } else if (unlikely(feat == &dt_acct_features)) {
5046 dt->do_index_ops = &osd_acct_index_ops;
5049 } else if (!osd_has_index(obj)) {
5050 struct osd_directory *dir;
5051 struct osd_device *osd = osd_obj2dev(obj);
5052 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
5057 spin_lock(&obj->oo_guard);
5058 if (obj->oo_dir == NULL)
5062 * Concurrent thread allocated container data.
5065 spin_unlock(&obj->oo_guard);
5067 * Now, that we have container data, serialize its
5070 down_write(&obj->oo_ext_idx_sem);
5072 * recheck under lock.
5075 if (osd_has_index(obj)) {
5080 result = osd_iam_container_init(env, obj, obj->oo_dir);
5081 if (result || feat == &dt_lfsck_namespace_features ||
5082 feat == &dt_lfsck_layout_orphan_features ||
5083 feat == &dt_lfsck_layout_dangling_features)
5086 result = osd_index_register(osd, fid,
5087 feat->dif_keysize_max,
5088 feat->dif_recsize_max);
5090 CWARN("%s: failed to register index "
5092 osd_name(osd), PFID(fid), result);
5093 else if (result > 0)
5096 CDEBUG(D_LFSCK, "%s: index object "DFID
5097 " (%d/%d) registered\n",
5098 osd_name(osd), PFID(fid),
5099 (int)feat->dif_keysize_max,
5100 (int)feat->dif_recsize_max);
5103 up_write(&obj->oo_ext_idx_sem);
5111 if (result == 0 && skip_iam == 0) {
5112 if (!osd_iam_index_probe(env, obj, feat))
5115 LINVRNT(osd_invariant(obj));
5120 static int osd_otable_it_attr_get(const struct lu_env *env,
5121 struct dt_object *dt,
5122 struct lu_attr *attr)
5128 static const struct dt_object_operations osd_obj_ops = {
5129 .do_read_lock = osd_read_lock,
5130 .do_write_lock = osd_write_lock,
5131 .do_read_unlock = osd_read_unlock,
5132 .do_write_unlock = osd_write_unlock,
5133 .do_write_locked = osd_write_locked,
5134 .do_attr_get = osd_attr_get,
5135 .do_declare_attr_set = osd_declare_attr_set,
5136 .do_attr_set = osd_attr_set,
5137 .do_ah_init = osd_ah_init,
5138 .do_declare_create = osd_declare_create,
5139 .do_create = osd_create,
5140 .do_declare_destroy = osd_declare_destroy,
5141 .do_destroy = osd_destroy,
5142 .do_index_try = osd_index_try,
5143 .do_declare_ref_add = osd_declare_ref_add,
5144 .do_ref_add = osd_ref_add,
5145 .do_declare_ref_del = osd_declare_ref_del,
5146 .do_ref_del = osd_ref_del,
5147 .do_xattr_get = osd_xattr_get,
5148 .do_declare_xattr_set = osd_declare_xattr_set,
5149 .do_xattr_set = osd_xattr_set,
5150 .do_declare_xattr_del = osd_declare_xattr_del,
5151 .do_xattr_del = osd_xattr_del,
5152 .do_xattr_list = osd_xattr_list,
5153 .do_object_sync = osd_object_sync,
5154 .do_invalidate = osd_invalidate,
5155 .do_check_stale = osd_check_stale,
5158 static const struct dt_object_operations osd_obj_otable_it_ops = {
5159 .do_attr_get = osd_otable_it_attr_get,
5160 .do_index_try = osd_index_try,
5163 static int osd_index_declare_iam_delete(const struct lu_env *env,
5164 struct dt_object *dt,
5165 const struct dt_key *key,
5166 struct thandle *handle)
5168 struct osd_thandle *oh;
5170 oh = container_of(handle, struct osd_thandle, ot_super);
5171 LASSERT(oh->ot_handle == NULL);
5173 /* Recycle may cause additional three blocks to be changed. */
5174 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
5175 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
5181 * delete a (key, value) pair from index \a dt specified by \a key
5183 * \param dt osd index object
5184 * \param key key for index
5185 * \param rec record reference
5186 * \param handle transaction handler
5189 * \retval -ve failure
5191 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
5192 const struct dt_key *key,
5193 struct thandle *handle)
5195 struct osd_thread_info *oti = osd_oti_get(env);
5196 struct osd_object *obj = osd_dt_obj(dt);
5197 struct osd_thandle *oh;
5198 struct iam_path_descr *ipd;
5199 struct iam_container *bag = &obj->oo_dir->od_container;
5204 if (!dt_object_exists(dt))
5207 LINVRNT(osd_invariant(obj));
5208 LASSERT(!dt_object_remote(dt));
5209 LASSERT(bag->ic_object == obj->oo_inode);
5210 LASSERT(handle != NULL);
5212 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
5214 ipd = osd_idx_ipd_get(env, bag);
5215 if (unlikely(ipd == NULL))
5218 oh = container_of(handle, struct osd_thandle, ot_super);
5219 LASSERT(oh->ot_handle != NULL);
5220 LASSERT(oh->ot_handle->h_transaction != NULL);
5222 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5223 /* swab quota uid/gid provided by caller */
5224 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5225 key = (const struct dt_key *)&oti->oti_quota_id;
5228 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
5229 osd_ipd_put(env, bag, ipd);
5230 LINVRNT(osd_invariant(obj));
5231 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
5235 static int osd_index_declare_ea_delete(const struct lu_env *env,
5236 struct dt_object *dt,
5237 const struct dt_key *key,
5238 struct thandle *handle)
5240 struct osd_thandle *oh;
5241 struct inode *inode;
5246 LASSERT(!dt_object_remote(dt));
5247 LASSERT(handle != NULL);
5249 oh = container_of(handle, struct osd_thandle, ot_super);
5250 LASSERT(oh->ot_handle == NULL);
5252 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
5253 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
5255 inode = osd_dt_obj(dt)->oo_inode;
5259 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
5260 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
5265 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
5268 struct osd_fid_pack *rec;
5271 if (de->file_type & LDISKFS_DIRENT_LUFID) {
5272 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
5273 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
5274 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
5280 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
5281 const struct lu_fid *fid)
5283 struct seq_server_site *ss = osd_seq_site(osd);
5287 /* FID seqs not in FLDB, must be local seq */
5288 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
5292 * If FLD is not being initialized yet, it only happens during the
5293 * initialization, likely during mgs initialization, and we assume
5294 * this is local FID.
5296 if (ss == NULL || ss->ss_server_fld == NULL)
5299 /* Only check the local FLDB here */
5300 if (osd_seq_exists(env, osd, fid_seq(fid)))
5306 static void osd_take_care_of_agent(const struct lu_env *env,
5307 struct osd_device *osd,
5308 struct osd_thandle *oh,
5309 struct ldiskfs_dir_entry_2 *de)
5311 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
5312 struct osd_idmap_cache *idc;
5313 int rc, schedule = 0;
5315 LASSERT(de != NULL);
5317 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
5318 if (likely(rc == 0)) {
5319 idc = osd_idc_find_or_init(env, osd, fid);
5320 if (IS_ERR(idc) || idc->oic_remote)
5322 } else if (rc == -ENODATA) {
5324 * can't get FID, postpone to the end of the
5325 * transaction when iget() is safe
5329 CERROR("%s: can't get FID: rc = %d\n", osd_name(osd), rc);
5332 osd_schedule_agent_inode_removal(env, oh,
5333 le32_to_cpu(de->inode));
5337 * Index delete function for interoperability mode (b11826).
5338 * It will remove the directory entry added by osd_index_ea_insert().
5339 * This entry is needed to maintain name->fid mapping.
5341 * \param key, key i.e. file entry to be deleted
5343 * \retval 0, on success
5344 * \retval -ve, on error
5346 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
5347 const struct dt_key *key, struct thandle *handle)
5349 struct osd_object *obj = osd_dt_obj(dt);
5350 struct inode *dir = obj->oo_inode;
5351 struct dentry *dentry;
5352 struct osd_thandle *oh;
5353 struct ldiskfs_dir_entry_2 *de = NULL;
5354 struct buffer_head *bh;
5355 struct htree_lock *hlock = NULL;
5356 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5361 if (!dt_object_exists(dt))
5364 LINVRNT(osd_invariant(obj));
5365 LASSERT(!dt_object_remote(dt));
5366 LASSERT(handle != NULL);
5368 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
5370 oh = container_of(handle, struct osd_thandle, ot_super);
5371 LASSERT(oh->ot_handle != NULL);
5372 LASSERT(oh->ot_handle->h_transaction != NULL);
5374 dquot_initialize(dir);
5375 dentry = osd_child_dentry_get(env, obj,
5376 (char *)key, strlen((char *)key));
5378 if (obj->oo_hl_head != NULL) {
5379 hlock = osd_oti_get(env)->oti_hlock;
5380 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5381 dir, LDISKFS_HLOCK_DEL);
5383 down_write(&obj->oo_ext_idx_sem);
5386 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5389 * If this is not the ".." entry, it might be a remote DNE
5390 * entry and we need to check if the FID is for a remote
5391 * MDT. If the FID is not in the directory entry (e.g.
5392 * upgraded 1.8 filesystem without dirdata enabled) then
5393 * we need to get the FID from the LMA. For a remote directory
5394 * there HAS to be an LMA, it cannot be an IGIF inode in this
5397 * Delete the entry before the agent inode in order to
5398 * simplify error handling. At worst an error after deleting
5399 * the entry first might leak the agent inode afterward. The
5400 * reverse would need filesystem abort in case of error deleting
5401 * the entry after the agent had been removed, or leave a
5402 * dangling entry pointing at a random inode.
5404 if (strcmp((char *)key, dotdot) != 0)
5405 osd_take_care_of_agent(env, osd, oh, de);
5406 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
5412 if (!rc && fid_is_namespace_visible(lu_object_fid(&dt->do_lu)) &&
5413 obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
5414 /* NB, dirent count may not be accurate, because it's counted
5417 if (obj->oo_dirent_count)
5418 obj->oo_dirent_count--;
5420 obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
5423 ldiskfs_htree_unlock(hlock);
5425 up_write(&obj->oo_ext_idx_sem);
5428 LASSERT(osd_invariant(obj));
5429 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
5434 * Lookup index for \a key and copy record to \a rec.
5436 * \param dt osd index object
5437 * \param key key for index
5438 * \param rec record reference
5440 * \retval +ve success : exact mach
5441 * \retval 0 return record with key not greater than \a key
5442 * \retval -ve failure
5444 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
5445 struct dt_rec *rec, const struct dt_key *key)
5447 struct osd_object *obj = osd_dt_obj(dt);
5448 struct iam_path_descr *ipd;
5449 struct iam_container *bag = &obj->oo_dir->od_container;
5450 struct osd_thread_info *oti = osd_oti_get(env);
5451 struct iam_iterator *it = &oti->oti_idx_it;
5452 struct iam_rec *iam_rec;
5457 if (!dt_object_exists(dt))
5460 LASSERT(osd_invariant(obj));
5461 LASSERT(!dt_object_remote(dt));
5462 LASSERT(bag->ic_object == obj->oo_inode);
5464 ipd = osd_idx_ipd_get(env, bag);
5468 /* got ipd now we can start iterator. */
5469 iam_it_init(it, bag, 0, ipd);
5471 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5472 /* swab quota uid/gid provided by caller */
5473 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5474 key = (const struct dt_key *)&oti->oti_quota_id;
5477 rc = iam_it_get(it, (struct iam_key *)key);
5479 if (S_ISDIR(obj->oo_inode->i_mode))
5480 iam_rec = (struct iam_rec *)oti->oti_ldp;
5482 iam_rec = (struct iam_rec *)rec;
5484 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
5486 if (S_ISDIR(obj->oo_inode->i_mode))
5487 osd_fid_unpack((struct lu_fid *)rec,
5488 (struct osd_fid_pack *)iam_rec);
5489 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
5490 osd_quota_unpack(obj, rec);
5495 osd_ipd_put(env, bag, ipd);
5497 LINVRNT(osd_invariant(obj));
5502 static int osd_index_declare_iam_insert(const struct lu_env *env,
5503 struct dt_object *dt,
5504 const struct dt_rec *rec,
5505 const struct dt_key *key,
5506 struct thandle *handle)
5508 struct osd_thandle *oh;
5510 LASSERT(handle != NULL);
5512 oh = container_of(handle, struct osd_thandle, ot_super);
5513 LASSERT(oh->ot_handle == NULL);
5515 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
5516 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
5522 * Inserts (key, value) pair in \a dt index object.
5524 * \param dt osd index object
5525 * \param key key for index
5526 * \param rec record reference
5527 * \param th transaction handler
5530 * \retval -ve failure
5532 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
5533 const struct dt_rec *rec,
5534 const struct dt_key *key, struct thandle *th)
5536 struct osd_object *obj = osd_dt_obj(dt);
5537 struct iam_path_descr *ipd;
5538 struct osd_thandle *oh;
5539 struct iam_container *bag;
5540 struct osd_thread_info *oti = osd_oti_get(env);
5541 struct iam_rec *iam_rec;
5546 if (!dt_object_exists(dt))
5549 LINVRNT(osd_invariant(obj));
5550 LASSERT(!dt_object_remote(dt));
5552 bag = &obj->oo_dir->od_container;
5553 LASSERT(bag->ic_object == obj->oo_inode);
5554 LASSERT(th != NULL);
5556 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5558 ipd = osd_idx_ipd_get(env, bag);
5559 if (unlikely(ipd == NULL))
5562 oh = container_of(th, struct osd_thandle, ot_super);
5563 LASSERT(oh->ot_handle != NULL);
5564 LASSERT(oh->ot_handle->h_transaction != NULL);
5565 if (S_ISDIR(obj->oo_inode->i_mode)) {
5566 iam_rec = (struct iam_rec *)oti->oti_ldp;
5567 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
5569 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5570 /* pack quota uid/gid */
5571 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5572 key = (const struct dt_key *)&oti->oti_quota_id;
5573 /* pack quota record */
5574 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
5575 iam_rec = (struct iam_rec *)rec;
5577 iam_rec = (struct iam_rec *)rec;
5580 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
5582 osd_ipd_put(env, bag, ipd);
5583 LINVRNT(osd_invariant(obj));
5584 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5589 * Calls ldiskfs_add_entry() to add directory entry
5590 * into the directory. This is required for
5591 * interoperability mode (b11826)
5593 * \retval 0, on success
5594 * \retval -ve, on error
5596 static int __osd_ea_add_rec(struct osd_thread_info *info,
5597 struct osd_object *pobj, struct inode *cinode,
5598 const char *name, const struct lu_fid *fid,
5599 struct htree_lock *hlock, struct thandle *th)
5601 struct ldiskfs_dentry_param *ldp;
5602 struct dentry *child;
5603 struct osd_thandle *oth;
5606 oth = container_of(th, struct osd_thandle, ot_super);
5607 LASSERT(oth->ot_handle != NULL);
5608 LASSERT(oth->ot_handle->h_transaction != NULL);
5609 LASSERT(pobj->oo_inode);
5611 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
5612 if (unlikely(osd_object_is_root(pobj)))
5615 osd_get_ldiskfs_dirent_param(ldp, fid);
5616 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
5617 child->d_fsdata = (void *)ldp;
5618 dquot_initialize(pobj->oo_inode);
5619 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
5620 child, cinode, hlock);
5621 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
5622 struct ldiskfs_dir_entry_2 *de;
5623 struct buffer_head *bh;
5626 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
5629 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
5632 if (S_ISDIR(cinode->i_mode))
5633 de->file_type = LDISKFS_DIRENT_LUFID |
5634 LDISKFS_FT_REG_FILE;
5636 de->file_type = LDISKFS_DIRENT_LUFID |
5638 ldiskfs_handle_dirty_metadata(oth->ot_handle,
5649 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
5650 * into the directory.Also sets flags into osd object to
5651 * indicate dot and dotdot are created. This is required for
5652 * interoperability mode (b11826)
5654 * \param dir directory for dot and dotdot fixup.
5655 * \param obj child object for linking
5657 * \retval 0, on success
5658 * \retval -ve, on error
5660 static int osd_add_dot_dotdot(struct osd_thread_info *info,
5661 struct osd_object *dir,
5662 struct inode *parent_dir, const char *name,
5663 const struct lu_fid *dot_fid,
5664 const struct lu_fid *dot_dot_fid,
5667 struct inode *inode = dir->oo_inode;
5668 struct osd_thandle *oth;
5671 oth = container_of(th, struct osd_thandle, ot_super);
5672 LASSERT(oth->ot_handle->h_transaction != NULL);
5673 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
5675 if (strcmp(name, dot) == 0) {
5676 if (dir->oo_compat_dot_created) {
5679 LASSERT(inode->i_ino == parent_dir->i_ino);
5680 dir->oo_compat_dot_created = 1;
5683 } else if (strcmp(name, dotdot) == 0) {
5684 if (!dir->oo_compat_dot_created)
5686 /* in case of rename, dotdot is already created */
5687 if (dir->oo_compat_dotdot_created) {
5688 return __osd_ea_add_rec(info, dir, parent_dir, name,
5689 dot_dot_fid, NULL, th);
5692 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
5693 struct lu_fid tfid = *dot_dot_fid;
5696 result = osd_add_dot_dotdot_internal(info,
5697 dir->oo_inode, parent_dir, dot_fid,
5700 result = osd_add_dot_dotdot_internal(info,
5701 dir->oo_inode, parent_dir, dot_fid,
5706 dir->oo_compat_dotdot_created = 1;
5714 * It will call the appropriate osd_add* function and return the
5715 * value, return by respective functions.
5717 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
5718 struct inode *cinode, const char *name,
5719 const struct lu_fid *fid, struct thandle *th)
5721 struct osd_thread_info *info = osd_oti_get(env);
5722 struct htree_lock *hlock;
5725 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
5727 if (name[0] == '.' && (name[1] == '\0' ||
5728 (name[1] == '.' && name[2] == '\0'))) {
5729 if (hlock != NULL) {
5730 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5733 down_write(&pobj->oo_ext_idx_sem);
5736 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
5737 lu_object_fid(&pobj->oo_dt.do_lu),
5740 if (hlock != NULL) {
5741 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
5742 pobj->oo_inode, LDISKFS_HLOCK_ADD);
5744 down_write(&pobj->oo_ext_idx_sem);
5747 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
5748 struct lu_fid *tfid = &info->oti_fid;
5752 rc = __osd_ea_add_rec(info, pobj, cinode, name,
5755 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
5759 if (!rc && fid_is_namespace_visible(lu_object_fid(&pobj->oo_dt.do_lu))
5760 && pobj->oo_dirent_count != LU_DIRENT_COUNT_UNSET)
5761 pobj->oo_dirent_count++;
5764 ldiskfs_htree_unlock(hlock);
5766 up_write(&pobj->oo_ext_idx_sem);
5772 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
5773 struct osd_idmap_cache *oic)
5775 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
5776 struct lu_fid *fid = &oic->oic_fid;
5777 struct osd_inode_id *id = &oic->oic_lid;
5778 struct inode *inode = NULL;
5785 if (!fid_is_norm(fid) && !fid_is_igif(fid))
5788 if (scrub->os_running && scrub->os_pos_current > id->oii_ino)
5791 if (dev->od_auto_scrub_interval == AS_NEVER ||
5792 ktime_get_real_seconds() <
5793 scrub->os_file.sf_time_last_complete + dev->od_auto_scrub_interval)
5797 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
5798 if (rc == -ENOENT) {
5799 __u32 gen = id->oii_gen;
5805 inode = osd_iget(oti, dev, id);
5806 /* The inode has been removed (by race maybe). */
5807 if (IS_ERR(inode)) {
5808 rc = PTR_ERR(inode);
5810 RETURN(rc == -ESTALE ? -ENOENT : rc);
5813 /* The OI mapping is lost. */
5814 if (gen != OSD_OII_NOGEN)
5818 * The inode may has been reused by others, we do not know,
5819 * leave it to be handled by subsequent osd_fid_lookup().
5822 } else if (rc || osd_id_eq(id, &oti->oti_id)) {
5829 if (scrub->os_running) {
5830 if (inode == NULL) {
5831 inode = osd_iget(oti, dev, id);
5832 /* The inode has been removed (by race maybe). */
5833 if (IS_ERR(inode)) {
5834 rc = PTR_ERR(inode);
5836 RETURN(rc == -ESTALE ? -ENOENT : rc);
5840 rc = osd_oii_insert(dev, oic, insert);
5842 * There is race condition between osd_oi_lookup and OI scrub.
5843 * The OI scrub finished just after osd_oi_lookup() failure.
5844 * Under such case, it is unnecessary to trigger OI scrub again,
5845 * but try to call osd_oi_lookup() again.
5847 if (unlikely(rc == -EAGAIN))
5850 if (!S_ISDIR(inode->i_mode))
5853 rc = osd_check_lmv(oti, dev, inode, oic);
5858 if (dev->od_auto_scrub_interval != AS_NEVER && ++once == 1) {
5859 rc = osd_scrub_start(oti->oti_env, dev, SS_AUTO_PARTIAL |
5860 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
5861 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
5862 "%s: trigger partial OI scrub for RPC inconsistency checking FID "DFID": rc = %d\n",
5863 osd_dev2name(dev), PFID(fid), rc);
5864 if (rc == 0 || rc == -EALREADY)
5876 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
5877 struct osd_device *dev,
5878 struct osd_idmap_cache *oic,
5879 struct lu_fid *fid, __u32 ino)
5881 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
5882 struct inode *inode;
5885 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
5886 inode = osd_iget(oti, dev, &oic->oic_lid);
5887 if (IS_ERR(inode)) {
5888 fid_zero(&oic->oic_fid);
5889 return PTR_ERR(inode);
5892 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, loa);
5895 fid_zero(&oic->oic_fid);
5897 *fid = oic->oic_fid = loa->loa_lma.lma_self_fid;
5901 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
5902 struct osd_inode_id *id, const struct lu_fid *fid)
5904 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
5905 id->oii_ino, id->oii_gen, info);
5906 info->oti_cache.oic_lid = *id;
5907 info->oti_cache.oic_fid = *fid;
5908 info->oti_cache.oic_dev = osd;
5912 * Get parent FID from the linkEA.
5914 * For a directory which parent resides on remote MDT, to satisfy the
5915 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
5916 * the other hand, to make the lookup(..) on the directory can return
5917 * the real parent FID, we append the real parent FID after its ".."
5918 * name entry in the /REMOTE_PARENT_DIR.
5920 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
5921 * backup. So after the restore, we cannot get the right parent FID from
5922 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
5923 * we have stored the real parent FID in the directory object's linkEA,
5924 * we can parse the linkEA for the real parent FID.
5926 * \param[in] env pointer to the thread context
5927 * \param[in] obj pointer to the object to be handled
5928 * \param[out]fid pointer to the buffer to hold the parent FID
5930 * \retval 0 for getting the real parent FID successfully
5931 * \retval negative error number on failure
5933 static int osd_get_pfid_from_linkea(const struct lu_env *env,
5934 struct osd_object *obj,
5937 struct osd_thread_info *oti = osd_oti_get(env);
5938 struct lu_buf *buf = &oti->oti_big_buf;
5939 struct dentry *dentry = &oti->oti_obj_dentry;
5940 struct inode *inode = obj->oo_inode;
5941 struct linkea_data ldata = { NULL };
5947 if (!S_ISDIR(inode->i_mode))
5951 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5952 buf->lb_buf, buf->lb_len);
5953 if (rc == -ERANGE) {
5954 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5957 lu_buf_realloc(buf, rc);
5958 if (buf->lb_buf == NULL)
5965 if (unlikely(rc == 0))
5971 if (unlikely(buf->lb_buf == NULL)) {
5972 lu_buf_realloc(buf, rc);
5973 if (buf->lb_buf == NULL)
5980 rc = linkea_init_with_rec(&ldata);
5982 linkea_first_entry(&ldata);
5983 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5989 static int osd_verify_ent_by_linkea(const struct lu_env *env,
5990 struct inode *inode,
5991 const struct lu_fid *pfid,
5992 const char *name, const int namelen)
5994 struct osd_thread_info *oti = osd_oti_get(env);
5995 struct lu_buf *buf = &oti->oti_big_buf;
5996 struct dentry *dentry = &oti->oti_obj_dentry;
5997 struct linkea_data ldata = { NULL };
5998 struct lu_name cname = { .ln_name = name,
5999 .ln_namelen = namelen };
6005 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
6006 buf->lb_buf, buf->lb_len);
6008 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
6013 if (unlikely(rc == 0))
6016 if (buf->lb_len < rc) {
6017 lu_buf_realloc(buf, rc);
6018 if (buf->lb_buf == NULL)
6025 rc = linkea_init_with_rec(&ldata);
6027 rc = linkea_links_find(&ldata, &cname, pfid);
6033 * Calls ->lookup() to find dentry. From dentry get inode and
6034 * read inode's ea to get fid. This is required for interoperability
6037 * \retval 0, on success
6038 * \retval -ve, on error
6040 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
6041 struct dt_rec *rec, const struct dt_key *key)
6043 struct inode *dir = obj->oo_inode;
6044 struct dentry *dentry;
6045 struct ldiskfs_dir_entry_2 *de;
6046 struct buffer_head *bh;
6047 struct lu_fid *fid = (struct lu_fid *)rec;
6048 struct htree_lock *hlock = NULL;
6054 LASSERT(dir->i_op != NULL);
6055 LASSERT(dir->i_op->lookup != NULL);
6057 dentry = osd_child_dentry_get(env, obj,
6058 (char *)key, strlen((char *)key));
6060 if (obj->oo_hl_head != NULL) {
6061 hlock = osd_oti_get(env)->oti_hlock;
6062 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
6063 dir, LDISKFS_HLOCK_LOOKUP);
6065 down_read(&obj->oo_ext_idx_sem);
6068 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6070 struct osd_thread_info *oti = osd_oti_get(env);
6071 struct osd_inode_id *id = &oti->oti_id;
6072 struct osd_idmap_cache *oic = &oti->oti_cache;
6073 struct osd_device *dev = osd_obj2dev(obj);
6075 ino = le32_to_cpu(de->inode);
6076 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
6078 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
6082 rc = osd_get_fid_from_dentry(de, rec);
6084 /* done with de, release bh */
6087 if (unlikely(is_remote_parent_ino(dev, ino))) {
6088 const char *name = (const char *)key;
6091 * If the parent is on remote MDT, and there
6092 * is no FID-in-dirent, then we have to get
6093 * the parent FID from the linkEA.
6095 if (likely(strlen(name) == 2 &&
6096 name[0] == '.' && name[1] == '.'))
6097 rc = osd_get_pfid_from_linkea(env, obj,
6100 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6103 osd_id_gen(id, ino, OSD_OII_NOGEN);
6106 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
6107 fid_zero(&oic->oic_fid);
6112 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
6113 rc = osd_consistency_check(oti, dev, oic);
6115 fid_zero(&oic->oic_fid);
6117 /* Other error should not affect lookup result. */
6127 ldiskfs_htree_unlock(hlock);
6129 up_read(&obj->oo_ext_idx_sem);
6133 static int osd_index_declare_ea_insert(const struct lu_env *env,
6134 struct dt_object *dt,
6135 const struct dt_rec *rec,
6136 const struct dt_key *key,
6137 struct thandle *handle)
6139 struct osd_thandle *oh;
6140 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
6141 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
6142 const struct lu_fid *fid = rec1->rec_fid;
6143 int credits, rc = 0;
6144 struct osd_idmap_cache *idc;
6148 LASSERT(!dt_object_remote(dt));
6149 LASSERT(handle != NULL);
6150 LASSERT(fid != NULL);
6151 LASSERT(rec1->rec_type != 0);
6153 oh = container_of(handle, struct osd_thandle, ot_super);
6154 LASSERT(oh->ot_handle == NULL);
6156 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
6159 * we can't call iget() while a transactions is running
6160 * (this can lead to a deadlock), but we need to know
6161 * inum and object type. so we find this information at
6162 * declaration and cache in per-thread info
6164 idc = osd_idc_find_or_init(env, osd, fid);
6166 RETURN(PTR_ERR(idc));
6167 if (idc->oic_remote) {
6169 * a reference to remote inode is represented by an
6170 * agent inode which we have to create
6172 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
6173 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
6176 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
6178 if (osd_dt_obj(dt)->oo_inode != NULL) {
6179 struct inode *inode = osd_dt_obj(dt)->oo_inode;
6182 * We ignore block quota on meta pool (MDTs), so needn't
6183 * calculate how many blocks will be consumed by this index
6186 rc = osd_declare_inode_qid(env, i_uid_read(inode),
6188 i_projid_read(inode), 0,
6189 oh, osd_dt_obj(dt), NULL,
6194 #ifdef HAVE_PROJECT_QUOTA
6196 * Reserve credits for local agent inode to transfer
6197 * to 0, quota enforcement is ignored in this case.
6199 if (idc->oic_remote &&
6200 LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
6201 i_projid_read(inode) != 0)
6202 rc = osd_declare_attr_qid(env, osd_dt_obj(dt), oh,
6203 0, i_projid_read(inode),
6204 0, false, PRJQUOTA, true);
6212 * Index add function for interoperability mode (b11826).
6213 * It will add the directory entry.This entry is needed to
6214 * maintain name->fid mapping.
6216 * \param key it is key i.e. file entry to be inserted
6217 * \param rec it is value of given key i.e. fid
6219 * \retval 0, on success
6220 * \retval -ve, on error
6222 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
6223 const struct dt_rec *rec,
6224 const struct dt_key *key, struct thandle *th)
6226 struct osd_object *obj = osd_dt_obj(dt);
6227 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
6228 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
6229 const struct lu_fid *fid = rec1->rec_fid;
6230 const char *name = (const char *)key;
6231 struct osd_thread_info *oti = osd_oti_get(env);
6232 struct inode *child_inode = NULL;
6233 struct osd_idmap_cache *idc;
6238 if (!dt_object_exists(dt))
6241 LASSERT(osd_invariant(obj));
6242 LASSERT(!dt_object_remote(dt));
6243 LASSERT(th != NULL);
6245 osd_trans_exec_op(env, th, OSD_OT_INSERT);
6247 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
6249 idc = osd_idc_find(env, osd, fid);
6250 if (unlikely(idc == NULL)) {
6251 idc = osd_idc_find_or_init(env, osd, fid);
6254 * this dt_insert() wasn't declared properly, so
6255 * FID is missing in OI cache. we better do not
6256 * lookup FID in FLDB/OI and don't risk to deadlock,
6257 * but in some special cases (lfsck testing, etc)
6258 * it's much simpler than fixing a caller.
6260 * normally this error should be placed after the first
6261 * find, but migrate may attach source stripes to
6262 * target, which doesn't create stripes.
6264 CERROR("%s: "DFID" wasn't declared for insert\n",
6265 osd_name(osd), PFID(fid));
6267 RETURN(PTR_ERR(idc));
6271 if (idc->oic_remote) {
6272 /* Insert remote entry */
6273 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
6275 igrab(osd->od_mdt_map->omm_remote_parent->d_inode);
6277 child_inode = osd_create_local_agent_inode(env, osd,
6278 obj, fid, rec1->rec_type & S_IFMT, th);
6279 if (IS_ERR(child_inode))
6280 RETURN(PTR_ERR(child_inode));
6283 /* Insert local entry */
6284 if (unlikely(idc->oic_lid.oii_ino == 0)) {
6285 /* for a reason OI cache wasn't filled properly */
6286 CERROR("%s: OIC for "DFID" isn't filled\n",
6287 osd_name(osd), PFID(fid));
6290 child_inode = oti->oti_inode;
6291 if (unlikely(child_inode == NULL)) {
6292 struct ldiskfs_inode_info *lii;
6297 child_inode = oti->oti_inode = &lii->vfs_inode;
6299 child_inode->i_sb = osd_sb(osd);
6300 child_inode->i_ino = idc->oic_lid.oii_ino;
6301 child_inode->i_mode = rec1->rec_type & S_IFMT;
6304 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
6306 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
6307 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
6309 if (child_inode && child_inode != oti->oti_inode)
6311 LASSERT(osd_invariant(obj));
6312 osd_trans_exec_check(env, th, OSD_OT_INSERT);
6318 * Initialize osd Iterator for given osd index object.
6320 * \param dt osd index object
6323 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
6324 struct dt_object *dt,
6327 struct osd_it_iam *it;
6328 struct osd_object *obj = osd_dt_obj(dt);
6329 struct lu_object *lo = &dt->do_lu;
6330 struct iam_path_descr *ipd;
6331 struct iam_container *bag = &obj->oo_dir->od_container;
6333 if (!dt_object_exists(dt))
6334 return ERR_PTR(-ENOENT);
6338 return ERR_PTR(-ENOMEM);
6340 ipd = osd_it_ipd_get(env, bag);
6341 if (likely(ipd != NULL)) {
6345 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
6346 return (struct dt_it *)it;
6349 return ERR_PTR(-ENOMEM);
6354 * free given Iterator.
6356 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
6358 struct osd_it_iam *it = (struct osd_it_iam *)di;
6359 struct osd_object *obj = it->oi_obj;
6361 iam_it_fini(&it->oi_it);
6362 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
6363 osd_object_put(env, obj);
6368 * Move Iterator to record specified by \a key
6370 * \param di osd iterator
6371 * \param key key for index
6373 * \retval +ve di points to record with least key not larger than key
6374 * \retval 0 di points to exact matched key
6375 * \retval -ve failure
6378 static int osd_it_iam_get(const struct lu_env *env,
6379 struct dt_it *di, const struct dt_key *key)
6381 struct osd_thread_info *oti = osd_oti_get(env);
6382 struct osd_it_iam *it = (struct osd_it_iam *)di;
6384 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
6385 /* swab quota uid/gid */
6386 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
6387 key = (struct dt_key *)&oti->oti_quota_id;
6390 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
6396 * \param di osd iterator
6398 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
6400 struct osd_it_iam *it = (struct osd_it_iam *)di;
6402 iam_it_put(&it->oi_it);
6406 * Move iterator by one record
6408 * \param di osd iterator
6410 * \retval +1 end of container reached
6412 * \retval -ve failure
6415 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
6417 struct osd_it_iam *it = (struct osd_it_iam *)di;
6419 return iam_it_next(&it->oi_it);
6423 * Return pointer to the key under iterator.
6426 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
6427 const struct dt_it *di)
6429 struct osd_thread_info *oti = osd_oti_get(env);
6430 struct osd_it_iam *it = (struct osd_it_iam *)di;
6431 struct osd_object *obj = it->oi_obj;
6434 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
6436 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
6437 /* swab quota uid/gid */
6438 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
6439 key = (struct dt_key *)&oti->oti_quota_id;
6446 * Return size of key under iterator (in bytes)
6449 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
6451 struct osd_it_iam *it = (struct osd_it_iam *)di;
6453 return iam_it_key_size(&it->oi_it);
6457 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
6459 /* check if file type is required */
6460 if (ent->lde_attrs & LUDA_TYPE) {
6461 struct luda_type *lt;
6462 int align = sizeof(*lt) - 1;
6464 len = (len + align) & ~align;
6465 lt = (struct luda_type *)(ent->lde_name + len);
6466 lt->lt_type = cpu_to_le16(DTTOIF(type));
6469 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
6473 * build lu direct from backend fs dirent.
6477 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
6478 char *name, __u16 namelen, __u16 type, __u32 attr)
6480 ent->lde_attrs = attr | LUDA_FID;
6481 fid_cpu_to_le(&ent->lde_fid, fid);
6483 ent->lde_hash = cpu_to_le64(offset);
6484 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
6486 strncpy(ent->lde_name, name, namelen);
6487 ent->lde_name[namelen] = '\0';
6488 ent->lde_namelen = cpu_to_le16(namelen);
6490 /* append lustre attributes */
6491 osd_it_append_attrs(ent, namelen, type);
6495 * Return pointer to the record under iterator.
6497 static int osd_it_iam_rec(const struct lu_env *env,
6498 const struct dt_it *di,
6499 struct dt_rec *dtrec, __u32 attr)
6501 struct osd_it_iam *it = (struct osd_it_iam *)di;
6502 struct osd_thread_info *info = osd_oti_get(env);
6506 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
6507 const struct osd_fid_pack *rec;
6508 struct lu_fid *fid = &info->oti_fid;
6509 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6515 name = (char *)iam_it_key_get(&it->oi_it);
6517 RETURN(PTR_ERR(name));
6519 namelen = iam_it_key_size(&it->oi_it);
6521 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
6523 RETURN(PTR_ERR(rec));
6525 rc = osd_fid_unpack(fid, rec);
6529 hash = iam_it_store(&it->oi_it);
6531 /* IAM does not store object type in IAM index (dir) */
6532 osd_it_pack_dirent(lde, fid, hash, name, namelen,
6534 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
6535 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6536 (struct iam_rec *)dtrec);
6537 osd_quota_unpack(it->oi_obj, dtrec);
6539 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6540 (struct iam_rec *)dtrec);
6547 * Returns cookie for current Iterator position.
6549 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
6551 struct osd_it_iam *it = (struct osd_it_iam *)di;
6553 return iam_it_store(&it->oi_it);
6557 * Restore iterator from cookie.
6559 * \param di osd iterator
6560 * \param hash Iterator location cookie
6562 * \retval +ve di points to record with least key not larger than key.
6563 * \retval 0 di points to exact matched key
6564 * \retval -ve failure
6567 static int osd_it_iam_load(const struct lu_env *env,
6568 const struct dt_it *di, __u64 hash)
6570 struct osd_it_iam *it = (struct osd_it_iam *)di;
6572 return iam_it_load(&it->oi_it, hash);
6575 static const struct dt_index_operations osd_index_iam_ops = {
6576 .dio_lookup = osd_index_iam_lookup,
6577 .dio_declare_insert = osd_index_declare_iam_insert,
6578 .dio_insert = osd_index_iam_insert,
6579 .dio_declare_delete = osd_index_declare_iam_delete,
6580 .dio_delete = osd_index_iam_delete,
6582 .init = osd_it_iam_init,
6583 .fini = osd_it_iam_fini,
6584 .get = osd_it_iam_get,
6585 .put = osd_it_iam_put,
6586 .next = osd_it_iam_next,
6587 .key = osd_it_iam_key,
6588 .key_size = osd_it_iam_key_size,
6589 .rec = osd_it_iam_rec,
6590 .store = osd_it_iam_store,
6591 .load = osd_it_iam_load
6597 * Creates or initializes iterator context.
6599 * \retval struct osd_it_ea, iterator structure on success
6602 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
6603 struct dt_object *dt,
6606 struct osd_object *obj = osd_dt_obj(dt);
6607 struct osd_thread_info *info = osd_oti_get(env);
6608 struct osd_it_ea *oie;
6610 struct lu_object *lo = &dt->do_lu;
6611 struct dentry *obj_dentry;
6615 if (!dt_object_exists(dt) || obj->oo_destroyed)
6616 RETURN(ERR_PTR(-ENOENT));
6618 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
6620 RETURN(ERR_PTR(-ENOMEM));
6621 obj_dentry = &oie->oie_dentry;
6623 obj_dentry->d_inode = obj->oo_inode;
6624 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
6625 obj_dentry->d_name.hash = 0;
6627 oie->oie_rd_dirent = 0;
6628 oie->oie_it_dirent = 0;
6629 oie->oie_dirent = NULL;
6630 if (unlikely(!info->oti_it_ea_buf_used)) {
6631 oie->oie_buf = info->oti_it_ea_buf;
6632 info->oti_it_ea_buf_used = 1;
6634 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6635 if (oie->oie_buf == NULL)
6636 RETURN(ERR_PTR(-ENOMEM));
6640 file = &oie->oie_file;
6642 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
6643 if (attr & LUDA_64BITHASH)
6644 file->f_mode = FMODE_64BITHASH;
6646 file->f_mode = FMODE_32BITHASH;
6647 file->f_path.dentry = obj_dentry;
6648 file->f_mapping = obj->oo_inode->i_mapping;
6649 file->f_op = obj->oo_inode->i_fop;
6650 file->f_inode = obj->oo_inode;
6653 RETURN((struct dt_it *)oie);
6657 * Destroy or finishes iterator context.
6659 * \param di iterator structure to be destroyed
6661 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
6663 struct osd_thread_info *info = osd_oti_get(env);
6664 struct osd_it_ea *oie = (struct osd_it_ea *)di;
6665 struct osd_object *obj = oie->oie_obj;
6666 struct inode *inode = obj->oo_inode;
6669 oie->oie_file.f_op->release(inode, &oie->oie_file);
6670 osd_object_put(env, obj);
6671 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
6672 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6674 info->oti_it_ea_buf_used = 0;
6675 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
6680 * It position the iterator at given key, so that next lookup continues from
6681 * that key Or it is similar to dio_it->load() but based on a key,
6682 * rather than file position.
6684 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
6687 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
6689 static int osd_it_ea_get(const struct lu_env *env,
6690 struct dt_it *di, const struct dt_key *key)
6692 struct osd_it_ea *it = (struct osd_it_ea *)di;
6695 LASSERT(((const char *)key)[0] == '\0');
6696 it->oie_file.f_pos = 0;
6697 it->oie_rd_dirent = 0;
6698 it->oie_it_dirent = 0;
6699 it->oie_dirent = NULL;
6707 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
6711 struct osd_filldir_cbs {
6712 struct dir_context ctx;
6713 struct osd_it_ea *it;
6716 * It is called internally by ->iterate*(). It fills the
6717 * iterator's in-memory data structure with required
6718 * information i.e. name, namelen, rec_size etc.
6720 * \param buf in which information to be filled in.
6721 * \param name name of the file in given dir
6723 * \retval 0 on success
6724 * \retval 1 on buffer full
6726 #ifdef HAVE_FILLDIR_USE_CTX
6727 static int osd_ldiskfs_filldir(struct dir_context *buf,
6729 static int osd_ldiskfs_filldir(void *buf,
6731 const char *name, int namelen,
6732 loff_t offset, __u64 ino, unsigned int d_type)
6734 struct osd_it_ea *it = ((struct osd_filldir_cbs *)buf)->it;
6735 struct osd_object *obj = it->oie_obj;
6736 struct osd_it_ea_dirent *ent = it->oie_dirent;
6737 struct lu_fid *fid = &ent->oied_fid;
6738 struct osd_fid_pack *rec;
6742 /* this should never happen */
6743 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
6744 CERROR("ldiskfs return invalid namelen %d\n", namelen);
6748 if ((void *)ent - it->oie_buf + sizeof(*ent) + namelen >
6752 /* "." is just the object itself. */
6753 if (namelen == 1 && name[0] == '.') {
6754 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6755 } else if (d_type & LDISKFS_DIRENT_LUFID) {
6756 rec = (struct osd_fid_pack *)(name + namelen + 1);
6757 if (osd_fid_unpack(fid, rec) != 0)
6762 d_type &= ~LDISKFS_DIRENT_LUFID;
6764 /* NOT export local root. */
6765 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
6766 ino = obj->oo_inode->i_ino;
6767 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
6770 ent->oied_ino = ino;
6771 ent->oied_off = offset;
6772 ent->oied_namelen = namelen;
6773 ent->oied_type = d_type;
6775 memcpy(ent->oied_name, name, namelen);
6777 it->oie_rd_dirent++;
6778 it->oie_dirent = (void *)ent + cfs_size_round(sizeof(*ent) + namelen);
6783 * Calls ->iterate*() to load a directory entry at a time
6784 * and stored it in iterator's in-memory data structure.
6786 * \param di iterator's in memory structure
6788 * \retval 0 on success
6789 * \retval -ve on error
6790 * \retval +1 reach the end of entry
6792 static int osd_ldiskfs_it_fill(const struct lu_env *env,
6793 const struct dt_it *di)
6795 struct osd_it_ea *it = (struct osd_it_ea *)di;
6796 struct osd_object *obj = it->oie_obj;
6797 struct inode *inode = obj->oo_inode;
6798 struct htree_lock *hlock = NULL;
6799 struct file *filp = &it->oie_file;
6801 struct osd_filldir_cbs buf = {
6802 .ctx.actor = osd_ldiskfs_filldir,
6807 it->oie_dirent = it->oie_buf;
6808 it->oie_rd_dirent = 0;
6810 if (obj->oo_hl_head != NULL) {
6811 hlock = osd_oti_get(env)->oti_hlock;
6812 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
6813 inode, LDISKFS_HLOCK_READDIR);
6815 down_read(&obj->oo_ext_idx_sem);
6818 filp->f_cred = current_cred();
6819 rc = osd_security_file_alloc(filp);
6823 filp->f_flags |= O_NOATIME;
6824 filp->f_mode |= FMODE_NONOTIFY;
6825 rc = iterate_dir(filp, &buf.ctx);
6829 if (it->oie_rd_dirent == 0) {
6831 * If it does not get any dirent, it means it has been reached
6832 * to the end of the dir
6834 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
6838 it->oie_dirent = it->oie_buf;
6839 it->oie_it_dirent = 1;
6843 ldiskfs_htree_unlock(hlock);
6845 up_read(&obj->oo_ext_idx_sem);
6851 * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
6852 * to load a directory entry at a time and stored it in
6853 * iterator's in-memory data structure.
6855 * \param di iterator's in memory structure
6857 * \retval +ve iterator reached to end
6858 * \retval 0 iterator not reached to end
6859 * \retval -ve on error
6861 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
6863 struct osd_it_ea *it = (struct osd_it_ea *)di;
6868 if (it->oie_it_dirent < it->oie_rd_dirent) {
6870 (void *)it->oie_dirent +
6871 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
6872 it->oie_dirent->oied_namelen);
6873 it->oie_it_dirent++;
6876 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
6879 rc = osd_ldiskfs_it_fill(env, di);
6886 * Returns the key at current position from iterator's in memory structure.
6888 * \param di iterator's in memory structure
6890 * \retval key i.e. struct dt_key on success
6892 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
6893 const struct dt_it *di)
6895 struct osd_it_ea *it = (struct osd_it_ea *)di;
6897 return (struct dt_key *)it->oie_dirent->oied_name;
6901 * Returns key's size at current position from iterator's in memory structure.
6903 * \param di iterator's in memory structure
6905 * \retval key_size i.e. struct dt_key on success
6907 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
6909 struct osd_it_ea *it = (struct osd_it_ea *)di;
6911 return it->oie_dirent->oied_namelen;
6914 #if defined LDISKFS_DIR_ENTRY_LEN && defined LDISKFS_DIR_ENTRY_LEN_
6915 #undef LDISKFS_DIR_REC_LEN
6916 #define LDISKFS_DIR_REC_LEN(de) LDISKFS_DIR_ENTRY_LEN_((de))
6919 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
6921 if (LDISKFS_DIR_REC_LEN(de) >=
6922 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
6929 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
6930 unsigned int blocksize, bool dotdot)
6933 return osd_dotdot_has_space(de);
6935 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
6936 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
6943 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
6944 handle_t *jh, struct dentry *dentry,
6945 const struct lu_fid *fid, struct buffer_head *bh,
6946 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
6949 struct inode *dir = dentry->d_parent->d_inode;
6950 struct inode *inode = dentry->d_inode;
6951 struct osd_fid_pack *rec;
6952 struct ldiskfs_dentry_param *ldp;
6953 int namelen = dentry->d_name.len;
6955 struct osd_thread_info *info = osd_oti_get(env);
6959 if (!ldiskfs_has_feature_dirdata(inode->i_sb))
6962 /* There is enough space to hold the FID-in-dirent. */
6963 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
6964 rc = ldiskfs_journal_get_write_access(jh, bh);
6968 de->name[namelen] = 0;
6969 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
6970 rec->fp_len = sizeof(struct lu_fid) + 1;
6971 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
6972 de->file_type |= LDISKFS_DIRENT_LUFID;
6973 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
6980 rc = ldiskfs_delete_entry(jh, dir, de, bh);
6984 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
6985 osd_get_ldiskfs_dirent_param(ldp, fid);
6986 dentry->d_fsdata = (void *)ldp;
6987 dquot_initialize(dir);
6988 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
6990 * It is too bad, we cannot reinsert the name entry back.
6991 * That means we lose it!
6995 "%s: fail to reinsert the dirent, dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
6996 osd_ino2name(inode), dir->i_ino, dir->i_generation,
6997 namelen, dentry->d_name.name, PFID(fid), rc);
7003 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
7004 struct osd_it_ea *it, struct lu_fid *fid,
7005 struct osd_inode_id *id, __u32 *attr)
7007 struct osd_thread_info *info = osd_oti_get(env);
7008 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
7009 struct osd_device *dev = osd_obj2dev(obj);
7010 struct super_block *sb = osd_sb(dev);
7011 const char *devname = osd_name(dev);
7012 struct osd_it_ea_dirent *ent = it->oie_dirent;
7013 struct inode *dir = obj->oo_inode;
7014 struct htree_lock *hlock = NULL;
7015 struct buffer_head *bh = NULL;
7016 handle_t *jh = NULL;
7017 struct ldiskfs_dir_entry_2 *de;
7018 struct dentry *dentry;
7019 struct inode *inode;
7020 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
7023 bool dotdot = false;
7028 if (ent->oied_name[0] == '.') {
7029 if (ent->oied_namelen == 1)
7032 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
7036 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
7037 inode = osd_iget(info, dev, id);
7038 if (IS_ERR(inode)) {
7039 rc = PTR_ERR(inode);
7040 if (rc == -ENOENT || rc == -ESTALE) {
7042 * Maybe dangling name entry, or
7043 * corrupted directory entry.
7045 *attr |= LUDA_UNKNOWN;
7048 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
7049 "check_repair, dir = %lu/%u, name = %.*s, "
7050 "ino = %llu, rc = %d\n",
7051 devname, dir->i_ino, dir->i_generation,
7052 ent->oied_namelen, ent->oied_name,
7059 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
7061 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
7062 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
7068 * We need to ensure that the name entry is still valid.
7069 * Because it may be removed or renamed by other already.
7071 * The unlink or rename operation will start journal before PDO lock,
7072 * so to avoid deadlock, here we need to start journal handle before
7073 * related PDO lock also. But because we do not know whether there
7074 * will be something to be repaired before PDO lock, we just start
7075 * journal without conditions.
7077 * We may need to remove the name entry firstly, then insert back.
7078 * One credit is for user quota file update.
7079 * One credit is for group quota file update.
7080 * Two credits are for dirty inode.
7082 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
7083 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
7085 if (dev->od_dirent_journal != 0) {
7088 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
7091 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
7092 "check_repair, dir = %lu/%u, credits = %d, "
7093 "name = %.*s, ino = %llu: rc = %d\n",
7094 devname, dir->i_ino, dir->i_generation, credits,
7095 ent->oied_namelen, ent->oied_name,
7098 GOTO(out_inode, rc);
7101 if (obj->oo_hl_head != NULL) {
7102 hlock = osd_oti_get(env)->oti_hlock;
7104 * "0" means exclusive lock for the whole directory.
7105 * We need to prevent others access such name entry
7106 * during the delete + insert. Neither HLOCK_ADD nor
7107 * HLOCK_DEL cannot guarantee the atomicity.
7109 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
7111 down_write(&obj->oo_ext_idx_sem);
7114 if (obj->oo_hl_head != NULL) {
7115 hlock = osd_oti_get(env)->oti_hlock;
7116 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
7117 LDISKFS_HLOCK_LOOKUP);
7119 down_read(&obj->oo_ext_idx_sem);
7123 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
7124 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
7125 *attr |= LUDA_IGNORE;
7131 * For dotdot entry, if there is not enough space to hold the
7132 * FID-in-dirent, just keep them there. It only happens when the
7133 * device upgraded from 1.8 or restored from MDT file-level backup.
7134 * For the whole directory, only dotdot entry have no FID-in-dirent
7135 * and needs to get FID from LMA when readdir, it will not affect the
7138 if (dotdot && !osd_dotdot_has_space(de)) {
7139 *attr |= LUDA_UNKNOWN;
7145 if (lu_fid_eq(fid, &lma->lma_self_fid))
7148 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
7149 struct lu_fid *tfid = &lma->lma_self_fid;
7151 if (likely(dotdot &&
7152 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
7153 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
7155 * It must be REMOTE_PARENT_DIR and as the
7156 * 'dotdot' entry of remote directory
7158 *attr |= LUDA_IGNORE;
7160 CDEBUG(D_LFSCK, "%s: expect remote agent "
7161 "parent directory, but got %.*s under "
7162 "dir = %lu/%u with the FID "DFID"\n",
7163 devname, ent->oied_namelen,
7164 ent->oied_name, dir->i_ino,
7165 dir->i_generation, PFID(tfid));
7167 *attr |= LUDA_UNKNOWN;
7174 if (!fid_is_zero(fid)) {
7175 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
7177 if (rc == -ENOENT ||
7179 !(dev->od_scrub.os_scrub.os_file.sf_flags & SF_UPGRADE))) {
7181 * linkEA does not recognize the dirent entry,
7182 * it may because the dirent entry corruption
7183 * and points to other's inode.
7185 CDEBUG(D_LFSCK, "%s: the target inode does not "
7186 "recognize the dirent, dir = %lu/%u, "
7187 " name = %.*s, ino = %llu, "
7188 DFID": rc = %d\n", devname, dir->i_ino,
7189 dir->i_generation, ent->oied_namelen,
7190 ent->oied_name, ent->oied_ino, PFID(fid), rc);
7191 *attr |= LUDA_UNKNOWN;
7196 if (rc && rc != -ENODATA) {
7197 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
7198 "dir = %lu/%u, name = %.*s, ino = %llu, "
7199 DFID": rc = %d\n", devname, dir->i_ino,
7200 dir->i_generation, ent->oied_namelen,
7201 ent->oied_name, ent->oied_ino, PFID(fid), rc);
7202 *attr |= LUDA_UNKNOWN;
7210 * linkEA recognizes the dirent entry, the FID-in-LMA is
7211 * valid, trusted, in spite of fid_is_sane(fid) or not.
7213 if (*attr & LUDA_VERIFY_DRYRUN) {
7214 *fid = lma->lma_self_fid;
7215 *attr |= LUDA_REPAIR;
7222 dev->od_dirent_journal = 1;
7223 if (hlock != NULL) {
7224 ldiskfs_htree_unlock(hlock);
7227 up_read(&obj->oo_ext_idx_sem);
7233 *fid = lma->lma_self_fid;
7235 /* Update or append the FID-in-dirent. */
7236 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
7237 bh, de, hlock, dotdot);
7239 *attr |= LUDA_REPAIR;
7241 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
7242 "the dirent, dir = %lu/%u, name = %.*s, "
7243 "ino = %llu, "DFID": rc = %d\n",
7244 devname, dir->i_ino, dir->i_generation,
7245 ent->oied_namelen, ent->oied_name,
7246 ent->oied_ino, PFID(fid), rc);
7248 /* lma is NULL, trust the FID-in-dirent if it is valid. */
7249 if (*attr & LUDA_VERIFY_DRYRUN) {
7250 if (fid_is_sane(fid)) {
7251 *attr |= LUDA_REPAIR;
7252 } else if (dev->od_index == 0) {
7253 lu_igif_build(fid, inode->i_ino,
7254 inode->i_generation);
7255 *attr |= LUDA_UPGRADE;
7263 dev->od_dirent_journal = 1;
7264 if (hlock != NULL) {
7265 ldiskfs_htree_unlock(hlock);
7268 up_read(&obj->oo_ext_idx_sem);
7275 if (unlikely(fid_is_sane(fid))) {
7277 * FID-in-dirent exists, but FID-in-LMA is lost.
7278 * Trust the FID-in-dirent, and add FID-in-LMA.
7280 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
7282 *attr |= LUDA_REPAIR;
7284 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
7285 "update dirent, dir = %lu/%u, "
7286 "name = %.*s, ino = %llu, "
7288 devname, dir->i_ino, dir->i_generation,
7289 ent->oied_namelen, ent->oied_name,
7290 ent->oied_ino, PFID(fid), rc);
7291 } else if (dev->od_index == 0) {
7292 lu_igif_build(fid, inode->i_ino, inode->i_generation);
7294 * It is probably IGIF object. Only aappend the
7295 * FID-in-dirent. OI scrub will process FID-in-LMA.
7297 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
7298 bh, de, hlock, dotdot);
7300 *attr |= LUDA_UPGRADE;
7302 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
7303 "after the dirent, dir = %lu/%u, "
7304 "name = %.*s, ino = %llu, "
7306 devname, dir->i_ino, dir->i_generation,
7307 ent->oied_namelen, ent->oied_name,
7308 ent->oied_ino, PFID(fid), rc);
7317 if (hlock != NULL) {
7318 ldiskfs_htree_unlock(hlock);
7320 if (dev->od_dirent_journal != 0)
7321 up_write(&obj->oo_ext_idx_sem);
7323 up_read(&obj->oo_ext_idx_sem);
7327 ldiskfs_journal_stop(jh);
7331 if (rc >= 0 && !dirty)
7332 dev->od_dirent_journal = 0;
7338 * Returns the value at current position from iterator's in memory structure.
7340 * \param di struct osd_it_ea, iterator's in memory structure
7341 * \param attr attr requested for dirent.
7342 * \param lde lustre dirent
7344 * \retval 0 no error and \param lde has correct lustre dirent.
7345 * \retval -ve on error
7347 static inline int osd_it_ea_rec(const struct lu_env *env,
7348 const struct dt_it *di,
7349 struct dt_rec *dtrec, __u32 attr)
7351 struct osd_it_ea *it = (struct osd_it_ea *)di;
7352 struct osd_object *obj = it->oie_obj;
7353 struct osd_device *dev = osd_obj2dev(obj);
7354 struct osd_thread_info *oti = osd_oti_get(env);
7355 struct osd_inode_id *id = &oti->oti_id;
7356 struct lu_fid *fid = &it->oie_dirent->oied_fid;
7357 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
7358 __u32 ino = it->oie_dirent->oied_ino;
7363 LASSERT(!is_remote_parent_ino(dev, obj->oo_inode->i_ino));
7365 if (attr & LUDA_VERIFY) {
7366 if (unlikely(is_remote_parent_ino(dev, ino))) {
7367 attr |= LUDA_IGNORE;
7369 * If the parent is on remote MDT, and there
7370 * is no FID-in-dirent, then we have to get
7371 * the parent FID from the linkEA.
7373 if (!fid_is_sane(fid) &&
7374 it->oie_dirent->oied_namelen == 2 &&
7375 it->oie_dirent->oied_name[0] == '.' &&
7376 it->oie_dirent->oied_name[1] == '.')
7377 osd_get_pfid_from_linkea(env, obj, fid);
7379 rc = osd_dirent_check_repair(env, obj, it, fid, id,
7383 if (!fid_is_sane(fid))
7384 attr |= LUDA_UNKNOWN;
7386 attr &= ~LU_DIRENT_ATTRS_MASK;
7387 if (!fid_is_sane(fid)) {
7388 bool is_dotdot = false;
7390 if (it->oie_dirent->oied_namelen == 2 &&
7391 it->oie_dirent->oied_name[0] == '.' &&
7392 it->oie_dirent->oied_name[1] == '.')
7395 * If the parent is on remote MDT, and there
7396 * is no FID-in-dirent, then we have to get
7397 * the parent FID from the linkEA.
7399 if (is_remote_parent_ino(dev, ino) && is_dotdot) {
7400 rc = osd_get_pfid_from_linkea(env, obj, fid);
7402 if (is_dotdot == false &&
7403 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
7406 rc = osd_ea_fid_get(env, obj, ino, fid, id);
7411 /* Pack the entry anyway, at least the offset is right. */
7412 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
7413 it->oie_dirent->oied_name,
7414 it->oie_dirent->oied_namelen,
7415 it->oie_dirent->oied_type, attr);
7417 RETURN(rc > 0 ? 0 : rc);
7421 * Returns the record size size at current position.
7423 * This function will return record(lu_dirent) size in bytes.
7425 * \param[in] env execution environment
7426 * \param[in] di iterator's in memory structure
7427 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
7428 * calculate the lu_dirent size.
7430 * \retval record size(in bytes & in memory) of the current lu_dirent
7433 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
7436 struct osd_it_ea *it = (struct osd_it_ea *)di;
7438 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
7442 * Returns a cookie for current position of the iterator head, so that
7443 * user can use this cookie to load/start the iterator next time.
7445 * \param di iterator's in memory structure
7447 * \retval cookie for current position, on success
7449 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
7451 struct osd_it_ea *it = (struct osd_it_ea *)di;
7453 return it->oie_dirent->oied_off;
7457 * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
7458 * to load a directory entry at a time and stored it i inn,
7459 * in iterator's in-memory data structure.
7461 * \param di struct osd_it_ea, iterator's in memory structure
7463 * \retval +ve on success
7464 * \retval -ve on error
7466 static int osd_it_ea_load(const struct lu_env *env,
7467 const struct dt_it *di, __u64 hash)
7469 struct osd_it_ea *it = (struct osd_it_ea *)di;
7473 it->oie_file.f_pos = hash;
7475 rc = osd_ldiskfs_it_fill(env, di);
7486 * Index lookup function for interoperability mode (b11826).
7488 * \param key, key i.e. file name to be searched
7490 * \retval +ve, on success
7491 * \retval -ve, on error
7493 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
7494 struct dt_rec *rec, const struct dt_key *key)
7496 struct osd_object *obj = osd_dt_obj(dt);
7501 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
7502 LINVRNT(osd_invariant(obj));
7504 rc = osd_ea_lookup_rec(env, obj, rec, key);
7511 * Index and Iterator operations for interoperability
7512 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
7514 static const struct dt_index_operations osd_index_ea_ops = {
7515 .dio_lookup = osd_index_ea_lookup,
7516 .dio_declare_insert = osd_index_declare_ea_insert,
7517 .dio_insert = osd_index_ea_insert,
7518 .dio_declare_delete = osd_index_declare_ea_delete,
7519 .dio_delete = osd_index_ea_delete,
7521 .init = osd_it_ea_init,
7522 .fini = osd_it_ea_fini,
7523 .get = osd_it_ea_get,
7524 .put = osd_it_ea_put,
7525 .next = osd_it_ea_next,
7526 .key = osd_it_ea_key,
7527 .key_size = osd_it_ea_key_size,
7528 .rec = osd_it_ea_rec,
7529 .rec_size = osd_it_ea_rec_size,
7530 .store = osd_it_ea_store,
7531 .load = osd_it_ea_load
7535 static void *osd_key_init(const struct lu_context *ctx,
7536 struct lu_context_key *key)
7538 struct osd_thread_info *info;
7540 OBD_ALLOC_PTR(info);
7542 return ERR_PTR(-ENOMEM);
7544 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7545 if (info->oti_it_ea_buf == NULL)
7548 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
7550 info->oti_hlock = ldiskfs_htree_lock_alloc();
7551 if (info->oti_hlock == NULL)
7557 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7560 return ERR_PTR(-ENOMEM);
7563 static void osd_key_fini(const struct lu_context *ctx,
7564 struct lu_context_key *key, void *data)
7566 struct osd_thread_info *info = data;
7567 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
7568 struct osd_idmap_cache *idc = info->oti_ins_cache;
7570 if (info->oti_dio_pages) {
7572 for (i = 0; i < PTLRPC_MAX_BRW_PAGES; i++) {
7573 struct page *page = info->oti_dio_pages[i];
7575 LASSERT(PagePrivate2(page));
7576 LASSERT(PageLocked(page));
7577 ClearPagePrivate2(page);
7582 OBD_FREE_PTR_ARRAY_LARGE(info->oti_dio_pages,
7583 PTLRPC_MAX_BRW_PAGES);
7586 if (info->oti_inode != NULL)
7588 if (info->oti_hlock != NULL)
7589 ldiskfs_htree_lock_free(info->oti_hlock);
7590 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
7591 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
7592 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
7593 lu_buf_free(&info->oti_iobuf.dr_lnb_buf);
7594 lu_buf_free(&info->oti_big_buf);
7596 LASSERT(info->oti_ins_cache_size > 0);
7597 OBD_FREE_PTR_ARRAY(idc, info->oti_ins_cache_size);
7598 info->oti_ins_cache = NULL;
7599 info->oti_ins_cache_size = 0;
7604 static void osd_key_exit(const struct lu_context *ctx,
7605 struct lu_context_key *key, void *data)
7607 struct osd_thread_info *info = data;
7609 LASSERT(info->oti_r_locks == 0);
7610 LASSERT(info->oti_w_locks == 0);
7611 LASSERT(info->oti_txns == 0);
7614 /* type constructor/destructor: osd_type_init, osd_type_fini */
7615 LU_TYPE_INIT_FINI(osd, &osd_key);
7617 struct lu_context_key osd_key = {
7618 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
7619 .lct_init = osd_key_init,
7620 .lct_fini = osd_key_fini,
7621 .lct_exit = osd_key_exit
7625 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
7626 const char *name, struct lu_device *next)
7628 struct osd_device *osd = osd_dev(d);
7630 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname)) >=
7631 sizeof(osd->od_svname))
7633 return osd_procfs_init(osd, name);
7636 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
7638 struct seq_server_site *ss = osd_seq_site(osd);
7643 if (osd->od_is_ost || osd->od_cl_seq != NULL)
7646 if (unlikely(ss == NULL))
7649 OBD_ALLOC_PTR(osd->od_cl_seq);
7650 if (osd->od_cl_seq == NULL)
7653 seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
7654 osd->od_svname, ss->ss_server_seq);
7656 if (ss->ss_node_id == 0) {
7658 * If the OSD on the sequence controller(MDT0), then allocate
7659 * sequence here, otherwise allocate sequence after connected
7660 * to MDT0 (see mdt_register_lwp_callback()).
7662 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
7663 &osd->od_cl_seq->lcs_space, env);
7669 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
7671 if (osd->od_cl_seq == NULL)
7674 seq_client_fini(osd->od_cl_seq);
7675 OBD_FREE_PTR(osd->od_cl_seq);
7676 osd->od_cl_seq = NULL;
7679 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
7683 /* shutdown quota slave instance associated with the device */
7684 if (o->od_quota_slave_md != NULL) {
7685 struct qsd_instance *qsd = o->od_quota_slave_md;
7687 o->od_quota_slave_md = NULL;
7691 if (o->od_quota_slave_dt != NULL) {
7692 struct qsd_instance *qsd = o->od_quota_slave_dt;
7694 o->od_quota_slave_dt = NULL;
7698 osd_fid_fini(env, o);
7699 osd_scrub_cleanup(env, o);
7704 static void osd_umount(const struct lu_env *env, struct osd_device *o)
7708 if (o->od_mnt != NULL) {
7709 shrink_dcache_sb(osd_sb(o));
7710 osd_sync(env, &o->od_dt_dev);
7711 wait_event(o->od_commit_cb_done,
7712 !atomic_read(&o->od_commit_cb_in_flight));
7721 static int osd_mount(const struct lu_env *env,
7722 struct osd_device *o, struct lustre_cfg *cfg)
7724 const char *name = lustre_cfg_string(cfg, 0);
7725 const char *dev = lustre_cfg_string(cfg, 1);
7727 unsigned long page, s_flags = 0, lmd_flags = 0;
7728 struct page *__page;
7729 struct file_system_type *type;
7730 char *options = NULL;
7732 struct osd_thread_info *info = osd_oti_get(env);
7733 struct lu_fid *fid = &info->oti_fid;
7734 struct inode *inode;
7735 int rc = 0, force_over_1024tb = 0;
7739 if (o->od_mnt != NULL)
7742 if (strlen(dev) >= sizeof(o->od_mntdev))
7744 strcpy(o->od_mntdev, dev);
7746 str = lustre_cfg_buf(cfg, 2);
7747 sscanf(str, "%lu:%lu", &s_flags, &lmd_flags);
7749 opts = lustre_cfg_string(cfg, 3);
7751 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
7752 CERROR("%s: device %s extents feature is not guaranteed to "
7753 "work on big-endian systems. Use \"bigendian_extents\" "
7754 "mount option to override.\n", name, dev);
7758 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7759 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
7760 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");
7763 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
7764 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
7765 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");
7768 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
7769 if (opts != NULL && strstr(opts, "force_over_512tb") != NULL) {
7770 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");
7774 if (opts != NULL && strstr(opts, "force_over_1024tb") != NULL)
7775 force_over_1024tb = 1;
7777 __page = alloc_page(GFP_KERNEL);
7779 GOTO(out, rc = -ENOMEM);
7780 page = (unsigned long)page_address(__page);
7781 options = (char *)page;
7784 /* strip out the options for back compatiblity */
7785 static const char * const sout[] = {
7792 /* strip out option we processed in osd */
7793 "bigendian_extents",
7797 "force_over_1024tb",
7800 strncat(options, opts, PAGE_SIZE);
7801 for (rc = 0, str = options; sout[rc]; ) {
7802 char *op = strstr(str, sout[rc]);
7809 if (op == options || *(op - 1) == ',') {
7810 str = op + strlen(sout[rc]);
7811 if (*str == ',' || *str == '\0') {
7812 *str == ',' ? str++ : str;
7813 memmove(op, str, strlen(str) + 1);
7816 for (str = op; *str != ',' && *str != '\0'; str++)
7820 strncat(options, "user_xattr,acl", PAGE_SIZE);
7823 /* Glom up mount options */
7824 if (*options != '\0')
7825 strncat(options, ",", PAGE_SIZE);
7826 strncat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
7828 type = get_fs_type("ldiskfs");
7830 CERROR("%s: cannot find ldiskfs module\n", name);
7831 GOTO(out, rc = -ENODEV);
7834 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
7835 module_put(type->owner);
7837 if (IS_ERR(o->od_mnt)) {
7838 rc = PTR_ERR(o->od_mnt);
7840 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
7844 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
7845 osd_sb(o)->s_blocksize_bits > 1024ULL << 40 &&
7846 force_over_1024tb == 0) {
7847 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",
7849 GOTO(out_mnt, rc = -EINVAL);
7852 if (lmd_flags & LMD_FLG_DEV_RDONLY) {
7853 LCONSOLE_WARN("%s: not support dev_rdonly on this device\n",
7856 GOTO(out_mnt, rc = -EOPNOTSUPP);
7859 if (!ldiskfs_has_feature_journal(o->od_mnt->mnt_sb)) {
7860 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
7861 GOTO(out_mnt, rc = -EINVAL);
7864 #ifdef LDISKFS_MOUNT_DIRDATA
7865 if (ldiskfs_has_feature_dirdata(o->od_mnt->mnt_sb))
7866 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
7867 else if (strstr(name, "MDT")) /* don't complain for MGT or OSTs */
7868 CWARN("%s: device %s was upgraded from Lustre-1.x without "
7869 "enabling the dirdata feature. If you do not want to "
7870 "downgrade to Lustre-1.x again, you can enable it via "
7871 "'tune2fs -O dirdata device'\n", name, dev);
7873 inode = osd_sb(o)->s_root->d_inode;
7874 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
7875 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
7877 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
7881 if (lmd_flags & LMD_FLG_NOSCRUB)
7882 o->od_auto_scrub_interval = AS_NEVER;
7884 if (blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev))) {
7885 /* do not use pagecache with flash-backed storage */
7886 o->od_writethrough_cache = 0;
7887 o->od_read_cache = 0;
7898 __free_page(__page);
7903 static struct lu_device *osd_device_fini(const struct lu_env *env,
7904 struct lu_device *d)
7906 struct osd_device *o = osd_dev(d);
7910 osd_index_backup(env, o, false);
7911 osd_shutdown(env, o);
7913 if (o->od_oi_table != NULL)
7914 osd_oi_fini(osd_oti_get(env), o);
7915 if (o->od_extent_bytes_percpu)
7916 free_percpu(o->od_extent_bytes_percpu);
7917 osd_obj_map_fini(o);
7923 static int osd_device_init0(const struct lu_env *env,
7924 struct osd_device *o,
7925 struct lustre_cfg *cfg)
7927 struct lu_device *l = osd2lu_dev(o);
7928 struct osd_thread_info *info;
7932 /* if the module was re-loaded, env can loose its keys */
7933 rc = lu_env_refill((struct lu_env *)env);
7936 info = osd_oti_get(env);
7939 l->ld_ops = &osd_lu_ops;
7940 o->od_dt_dev.dd_ops = &osd_dt_ops;
7942 spin_lock_init(&o->od_osfs_lock);
7943 mutex_init(&o->od_otable_mutex);
7944 INIT_LIST_HEAD(&o->od_orphan_list);
7945 INIT_LIST_HEAD(&o->od_index_backup_list);
7946 INIT_LIST_HEAD(&o->od_index_restore_list);
7947 spin_lock_init(&o->od_lock);
7948 o->od_index_backup_policy = LIBP_NONE;
7950 init_waitqueue_head(&o->od_commit_cb_done);
7952 o->od_read_cache = 1;
7953 o->od_writethrough_cache = 1;
7954 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
7955 o->od_readcache_max_iosize = OSD_READCACHE_MAX_IO_MB << 20;
7956 o->od_writethrough_max_iosize = OSD_WRITECACHE_MAX_IO_MB << 20;
7957 o->od_auto_scrub_interval = AS_DEFAULT;
7958 /* default fallocate to unwritten extents: LU-14326/LU-14333 */
7959 o->od_fallocate_zero_blocks = 0;
7961 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
7962 sizeof(o->od_svname));
7963 if (cplen >= sizeof(o->od_svname)) {
7968 o->od_index_backup_stop = 0;
7969 o->od_index = -1; /* -1 means index is invalid */
7970 rc = server_name2index(o->od_svname, &o->od_index, NULL);
7971 if (rc == LDD_F_SV_TYPE_OST)
7974 o->od_full_scrub_ratio = OFSR_DEFAULT;
7975 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
7976 rc = osd_mount(env, o, cfg);
7980 /* Can only check block device after mount */
7981 o->od_nonrotational =
7982 blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev));
7984 rc = osd_obj_map_init(env, o);
7988 rc = lu_site_init(&o->od_site, l);
7990 GOTO(out_compat, rc);
7991 o->od_site.ls_bottom_dev = l;
7993 rc = lu_site_init_finish(&o->od_site);
7997 INIT_LIST_HEAD(&o->od_ios_list);
7998 /* setup scrub, including OI files initialization */
8000 rc = osd_scrub_setup(env, o);
8005 rc = osd_procfs_init(o, o->od_svname);
8007 CERROR("%s: can't initialize procfs: rc = %d\n",
8009 GOTO(out_scrub, rc);
8012 LASSERT(l->ld_site->ls_linkage.next != NULL);
8013 LASSERT(l->ld_site->ls_linkage.prev != NULL);
8015 /* initialize quota slave instance */
8016 /* currently it's no need to prepare qsd_instance_md for OST */
8017 if (!o->od_is_ost) {
8018 o->od_quota_slave_md = qsd_init(env, o->od_svname,
8020 o->od_proc_entry, true);
8021 if (IS_ERR(o->od_quota_slave_md)) {
8022 rc = PTR_ERR(o->od_quota_slave_md);
8023 o->od_quota_slave_md = NULL;
8024 GOTO(out_procfs, rc);
8028 o->od_quota_slave_dt = qsd_init(env, o->od_svname, &o->od_dt_dev,
8029 o->od_proc_entry, false);
8031 if (IS_ERR(o->od_quota_slave_dt)) {
8032 if (o->od_quota_slave_md != NULL) {
8033 qsd_fini(env, o->od_quota_slave_md);
8034 o->od_quota_slave_md = NULL;
8037 rc = PTR_ERR(o->od_quota_slave_dt);
8038 o->od_quota_slave_dt = NULL;
8039 GOTO(out_procfs, rc);
8042 o->od_extent_bytes_percpu = alloc_percpu(unsigned int);
8043 if (!o->od_extent_bytes_percpu) {
8045 GOTO(out_procfs, rc);
8053 osd_scrub_cleanup(env, o);
8055 lu_site_fini(&o->od_site);
8057 osd_obj_map_fini(o);
8064 static struct lu_device *osd_device_alloc(const struct lu_env *env,
8065 struct lu_device_type *t,
8066 struct lustre_cfg *cfg)
8068 struct osd_device *o;
8073 return ERR_PTR(-ENOMEM);
8075 rc = dt_device_init(&o->od_dt_dev, t);
8078 * Because the ctx might be revived in dt_device_init,
8079 * refill the env here
8081 lu_env_refill((struct lu_env *)env);
8082 rc = osd_device_init0(env, o, cfg);
8084 dt_device_fini(&o->od_dt_dev);
8087 if (unlikely(rc != 0))
8090 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
8093 static struct lu_device *osd_device_free(const struct lu_env *env,
8094 struct lu_device *d)
8096 struct osd_device *o = osd_dev(d);
8100 /* XXX: make osd top device in order to release reference */
8101 d->ld_site->ls_top_dev = d;
8102 lu_site_purge(env, d->ld_site, -1);
8103 lu_site_print(env, d->ld_site, &d->ld_site->ls_obj_hash.nelems,
8104 D_ERROR, lu_cdebug_printer);
8105 lu_site_fini(&o->od_site);
8106 dt_device_fini(&o->od_dt_dev);
8111 static int osd_process_config(const struct lu_env *env,
8112 struct lu_device *d, struct lustre_cfg *cfg)
8114 struct osd_device *o = osd_dev(d);
8120 switch (cfg->lcfg_command) {
8122 rc = osd_mount(env, o, cfg);
8126 * For the case LCFG_PRE_CLEANUP is not called in advance,
8127 * that may happend if hit failure during mount process.
8129 osd_index_backup(env, o, false);
8130 lu_dev_del_linkage(d->ld_site, d);
8131 rc = osd_shutdown(env, o);
8134 LASSERT(&o->od_dt_dev);
8135 count = class_modify_config(cfg, PARAM_OSD,
8136 &o->od_dt_dev.dd_kobj);
8138 count = class_modify_config(cfg, PARAM_OST,
8139 &o->od_dt_dev.dd_kobj);
8140 rc = count > 0 ? 0 : count;
8142 case LCFG_PRE_CLEANUP:
8144 osd_index_backup(env, o,
8145 o->od_index_backup_policy != LIBP_NONE);
8155 static int osd_recovery_complete(const struct lu_env *env,
8156 struct lu_device *d)
8158 struct osd_device *osd = osd_dev(d);
8163 if (osd->od_quota_slave_md == NULL && osd->od_quota_slave_dt == NULL)
8167 * start qsd instance on recovery completion, this notifies the quota
8168 * slave code that we are about to process new requests now
8170 rc = qsd_start(env, osd->od_quota_slave_dt);
8171 if (rc == 0 && osd->od_quota_slave_md != NULL)
8172 rc = qsd_start(env, osd->od_quota_slave_md);
8178 * we use exports to track all osd users
8180 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
8181 struct obd_device *obd, struct obd_uuid *cluuid,
8182 struct obd_connect_data *data, void *localdata)
8184 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8185 struct lustre_handle conn;
8190 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
8192 rc = class_connect(&conn, obd, cluuid);
8196 *exp = class_conn2export(&conn);
8198 spin_lock(&osd->od_osfs_lock);
8200 spin_unlock(&osd->od_osfs_lock);
8206 * once last export (we don't count self-export) disappeared
8207 * osd can be released
8209 static int osd_obd_disconnect(struct obd_export *exp)
8211 struct obd_device *obd = exp->exp_obd;
8212 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8213 int rc, release = 0;
8217 /* Only disconnect the underlying layers on the final disconnect. */
8218 spin_lock(&osd->od_osfs_lock);
8220 if (osd->od_connects == 0)
8222 spin_unlock(&osd->od_osfs_lock);
8224 rc = class_disconnect(exp); /* bz 9811 */
8226 if (rc == 0 && release)
8227 class_manual_cleanup(obd);
8231 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
8232 struct lu_device *dev)
8234 struct osd_device *osd = osd_dev(dev);
8235 struct lr_server_data *lsd =
8236 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
8241 if (osd->od_quota_slave_md != NULL) {
8242 /* set up quota slave objects for inode */
8243 result = qsd_prepare(env, osd->od_quota_slave_md);
8248 if (osd->od_quota_slave_dt != NULL) {
8249 /* set up quota slave objects for block */
8250 result = qsd_prepare(env, osd->od_quota_slave_dt);
8256 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
8257 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
8258 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
8259 osd->od_index_in_idif = 1;
8261 osd->od_index_in_idif = 0;
8262 result = osd_register_proc_index_in_idif(osd);
8267 osd->od_index_in_idif = 1;
8271 result = osd_fid_init(env, osd);
8277 * Implementation of lu_device_operations::ldo_fid_alloc() for OSD
8281 * see include/lu_object.h for the details.
8283 static int osd_fid_alloc(const struct lu_env *env, struct lu_device *d,
8284 struct lu_fid *fid, struct lu_object *parent,
8285 const struct lu_name *name)
8287 struct osd_device *osd = osd_dev(d);
8289 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
8292 static const struct lu_object_operations osd_lu_obj_ops = {
8293 .loo_object_init = osd_object_init,
8294 .loo_object_delete = osd_object_delete,
8295 .loo_object_release = osd_object_release,
8296 .loo_object_free = osd_object_free,
8297 .loo_object_print = osd_object_print,
8298 .loo_object_invariant = osd_object_invariant
8301 const struct lu_device_operations osd_lu_ops = {
8302 .ldo_object_alloc = osd_object_alloc,
8303 .ldo_process_config = osd_process_config,
8304 .ldo_recovery_complete = osd_recovery_complete,
8305 .ldo_prepare = osd_prepare,
8306 .ldo_fid_alloc = osd_fid_alloc,
8309 static const struct lu_device_type_operations osd_device_type_ops = {
8310 .ldto_init = osd_type_init,
8311 .ldto_fini = osd_type_fini,
8313 .ldto_start = osd_type_start,
8314 .ldto_stop = osd_type_stop,
8316 .ldto_device_alloc = osd_device_alloc,
8317 .ldto_device_free = osd_device_free,
8319 .ldto_device_init = osd_device_init,
8320 .ldto_device_fini = osd_device_fini
8323 static struct lu_device_type osd_device_type = {
8324 .ldt_tags = LU_DEVICE_DT,
8325 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
8326 .ldt_ops = &osd_device_type_ops,
8327 .ldt_ctx_tags = LCT_LOCAL,
8330 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
8332 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8333 struct super_block *sb = osd_sb(osd);
8335 return (osd->od_mnt == NULL || sb->s_flags & SB_RDONLY);
8339 * lprocfs legacy support.
8341 static const struct obd_ops osd_obd_device_ops = {
8342 .o_owner = THIS_MODULE,
8343 .o_connect = osd_obd_connect,
8344 .o_disconnect = osd_obd_disconnect,
8345 .o_health_check = osd_health_check,
8348 static ssize_t track_declares_assert_show(struct kobject *kobj,
8349 struct attribute *attr,
8352 return sprintf(buf, "%d\n", ldiskfs_track_declares_assert);
8355 static ssize_t track_declares_assert_store(struct kobject *kobj,
8356 struct attribute *attr,
8357 const char *buffer, size_t count)
8359 bool track_declares_assert;
8362 rc = kstrtobool(buffer, &track_declares_assert);
8366 ldiskfs_track_declares_assert = track_declares_assert;
8370 LUSTRE_RW_ATTR(track_declares_assert);
8372 static int __init osd_init(void)
8374 struct kobject *kobj;
8377 BUILD_BUG_ON(BH_DXLock >=
8378 sizeof(((struct buffer_head *)0)->b_state) * 8);
8379 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
8380 /* please, try to keep osd_thread_info smaller than a page */
8381 BUILD_BUG_ON(sizeof(struct osd_thread_info) > PAGE_SIZE);
8386 rc = lu_kmem_init(ldiskfs_caches);
8390 #ifdef CONFIG_KALLSYMS
8391 priv_security_file_alloc =
8392 (void *)kallsyms_lookup_name("security_file_alloc");
8395 rc = class_register_type(&osd_obd_device_ops, NULL, true,
8396 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
8398 lu_kmem_fini(ldiskfs_caches);
8402 kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
8404 rc = sysfs_create_file(kobj,
8405 &lustre_attr_track_declares_assert.attr);
8408 CWARN("osd-ldiskfs: track_declares_assert failed to register with sysfs\n");
8415 static void __exit osd_exit(void)
8417 struct kobject *kobj;
8419 kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
8421 sysfs_remove_file(kobj,
8422 &lustre_attr_track_declares_assert.attr);
8425 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
8426 lu_kmem_fini(ldiskfs_caches);
8429 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
8430 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
8431 MODULE_VERSION(LUSTRE_VERSION_STRING);
8432 MODULE_LICENSE("GPL");
8434 module_init(osd_init);
8435 module_exit(osd_exit);