4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/osd/osd_handler.c
33 * Top-level entry points into osd module
35 * Author: Nikita Danilov <nikita@clusterfs.com>
36 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
39 #define DEBUG_SUBSYSTEM S_OSD
41 #include <linux/fs_struct.h>
42 #include <linux/module.h>
43 #include <linux/user_namespace.h>
44 #include <linux/uidgid.h>
45 #ifdef HAVE_INODE_IVERSION
46 #include <linux/iversion.h>
48 #define inode_peek_iversion(__inode) ((__inode)->i_version)
51 /* prerequisite for linux/xattr.h */
52 #include <linux/types.h>
53 /* prerequisite for linux/xattr.h */
55 /* XATTR_{REPLACE,CREATE} */
56 #include <linux/xattr.h>
58 #include <ldiskfs/ldiskfs.h>
59 #include <ldiskfs/xattr.h>
60 #include <ldiskfs/ldiskfs_extents.h>
63 * struct OBD_{ALLOC,FREE}*()
65 #include <obd_support.h>
66 #include <libcfs/libcfs.h>
67 /* struct ptlrpc_thread */
68 #include <lustre_net.h>
69 #include <lustre_fid.h>
71 #include <uapi/linux/lustre/lustre_param.h>
72 #include <uapi/linux/lustre/lustre_disk.h>
74 #include "osd_internal.h"
75 #include "osd_dynlocks.h"
77 /* llo_* api support */
78 #include <md_object.h>
79 #include <lustre_quota.h>
81 #include <lustre_linkea.h>
83 /* encoding routines */
84 #include <lustre_crypto.h>
86 /* Maximum EA size is limited by LNET_MTU for remote objects */
87 #define OSD_MAX_EA_SIZE 1048364
90 module_param(ldiskfs_pdo, int, 0644);
91 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
93 int ldiskfs_track_declares_assert;
94 module_param(ldiskfs_track_declares_assert, int, 0644);
95 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
97 /* Slab to allocate dynlocks */
98 struct kmem_cache *dynlock_cachep;
100 /* Slab to allocate osd_it_ea */
101 struct kmem_cache *osd_itea_cachep;
103 static struct lu_kmem_descr ldiskfs_caches[] = {
105 .ckd_cache = &biop_cachep,
106 .ckd_name = "biop_cache",
107 .ckd_size = sizeof(struct osd_bio_private)
110 .ckd_cache = &dynlock_cachep,
111 .ckd_name = "dynlock_cache",
112 .ckd_size = sizeof(struct dynlock_handle)
115 .ckd_cache = &osd_itea_cachep,
116 .ckd_name = "osd_itea_cache",
117 .ckd_size = sizeof(struct osd_it_ea)
124 static atomic_t osd_mount_seq;
126 static const char dot[] = ".";
127 static const char dotdot[] = "..";
129 static const struct lu_object_operations osd_lu_obj_ops;
130 static const struct dt_object_operations osd_obj_ops;
131 static const struct dt_object_operations osd_obj_otable_it_ops;
132 static const struct dt_index_operations osd_index_iam_ops;
133 static const struct dt_index_operations osd_index_ea_ops;
135 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
136 const struct lu_fid *fid);
137 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
138 struct osd_device *osd);
139 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
140 const struct lu_buf *buf, const char *name, int fl,
141 struct thandle *handle);
143 int osd_trans_declare_op2rb[] = {
144 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
145 [OSD_OT_PUNCH] = OSD_OT_MAX,
146 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
147 [OSD_OT_CREATE] = OSD_OT_DESTROY,
148 [OSD_OT_DESTROY] = OSD_OT_CREATE,
149 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
150 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
151 [OSD_OT_WRITE] = OSD_OT_WRITE,
152 [OSD_OT_INSERT] = OSD_OT_DELETE,
153 [OSD_OT_DELETE] = OSD_OT_INSERT,
154 [OSD_OT_QUOTA] = OSD_OT_MAX,
157 static int osd_has_index(const struct osd_object *obj)
159 return obj->oo_dt.do_index_ops != NULL;
162 static int osd_object_invariant(const struct lu_object *l)
164 return osd_invariant(osd_obj(l));
168 * Concurrency: doesn't matter
170 static int osd_is_write_locked(const struct lu_env *env, struct osd_object *o)
172 struct osd_thread_info *oti = osd_oti_get(env);
174 return oti->oti_w_locks > 0 && o->oo_owner == env;
178 * Concurrency: doesn't access mutable data
180 static int osd_root_get(const struct lu_env *env,
181 struct dt_device *dev, struct lu_fid *f)
183 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
188 * the following set of functions are used to maintain per-thread
189 * cache of FID->ino mapping. this mechanism is needed to resolve
190 * FID to inode at dt_insert() which in turn stores ino in the
191 * directory entries to keep ldiskfs compatible with ext[34].
192 * due to locking-originated restrictions we can't lookup ino
193 * using LU cache (deadlock is possible). lookup using OI is quite
194 * expensive. so instead we maintain this cache and methods like
195 * dt_create() fill it. so in the majority of cases dt_insert() is
196 * able to find needed mapping in lockless manner.
198 static struct osd_idmap_cache *
199 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
200 const struct lu_fid *fid)
202 struct osd_thread_info *oti = osd_oti_get(env);
203 struct osd_idmap_cache *idc = oti->oti_ins_cache;
206 for (i = 0; i < oti->oti_ins_cache_used; i++) {
207 if (!lu_fid_eq(&idc[i].oic_fid, fid))
209 if (idc[i].oic_dev != osd)
218 static struct osd_idmap_cache *
219 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
220 const struct lu_fid *fid)
222 struct osd_thread_info *oti = osd_oti_get(env);
223 struct osd_idmap_cache *idc;
226 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
227 i = oti->oti_ins_cache_size * 2;
229 i = OSD_INS_CACHE_SIZE;
230 OBD_ALLOC_PTR_ARRAY_LARGE(idc, i);
232 return ERR_PTR(-ENOMEM);
233 if (oti->oti_ins_cache != NULL) {
234 memcpy(idc, oti->oti_ins_cache,
235 oti->oti_ins_cache_used * sizeof(*idc));
236 OBD_FREE_PTR_ARRAY_LARGE(oti->oti_ins_cache,
237 oti->oti_ins_cache_used);
239 oti->oti_ins_cache = idc;
240 oti->oti_ins_cache_size = i;
243 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
246 idc->oic_lid.oii_ino = 0;
247 idc->oic_lid.oii_gen = 0;
254 * lookup mapping for the given fid in the cache, initialize a
255 * new one if not found. the initialization checks whether the
256 * object is local or remote. for local objects, OI is used to
257 * learn ino/generation. the function is used when the caller
258 * has no information about the object, e.g. at dt_insert().
260 static struct osd_idmap_cache *
261 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
262 const struct lu_fid *fid)
264 struct osd_idmap_cache *idc;
267 idc = osd_idc_find(env, osd, fid);
268 LASSERT(!IS_ERR(idc));
272 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
273 osd->od_svname, PFID(fid));
275 /* new mapping is needed */
276 idc = osd_idc_add(env, osd, fid);
278 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
279 osd->od_svname, PFID(fid), PTR_ERR(idc));
284 rc = osd_remote_fid(env, osd, fid);
285 if (unlikely(rc < 0))
289 /* the object is local, lookup in OI */
290 /* XXX: probably cheaper to lookup in LU first? */
291 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
293 if (unlikely(rc < 0)) {
294 CERROR("can't lookup: rc = %d\n", rc);
298 /* the object is remote */
305 static void osd_idc_dump_lma(const struct lu_env *env,
306 struct osd_device *osd,
310 struct osd_thread_info *info = osd_oti_get(env);
311 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
312 const struct lu_fid *fid;
313 struct osd_inode_id lid;
317 inode = osd_ldiskfs_iget(osd_sb(osd), ino);
319 CERROR("%s: can't get inode %lu: rc = %d\n",
320 osd->od_svname, ino, (int)PTR_ERR(inode));
323 if (is_bad_inode(inode)) {
324 CERROR("%s: bad inode %lu\n", osd->od_svname, ino);
327 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
329 CERROR("%s: can't get LMA for %lu: rc = %d\n",
330 osd->od_svname, ino, rc);
333 fid = &loa->loa_lma.lma_self_fid;
334 LCONSOLE(D_INFO, "%s: "DFID" in inode %lu/%u\n", osd->od_svname,
335 PFID(fid), ino, (unsigned)inode->i_generation);
338 rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
340 CERROR("%s: can't lookup "DFID": rc = %d\n",
341 osd->od_svname, PFID(fid), rc);
344 LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n", osd->od_svname,
345 PFID(fid), lid.oii_ino, lid.oii_gen);
350 static void osd_idc_dump_debug(const struct lu_env *env,
351 struct osd_device *osd,
352 const struct lu_fid *fid,
356 struct osd_inode_id lid;
360 rc = osd_oi_lookup(osd_oti_get(env), osd, fid, &lid, 0);
362 LCONSOLE(D_INFO, "%s: "DFID" maps to %u/%u\n",
363 osd->od_svname, PFID(fid), lid.oii_ino, lid.oii_gen);
364 osd_idc_dump_lma(env, osd, lid.oii_ino, false);
366 CERROR("%s: can't lookup "DFID": rc = %d\n",
367 osd->od_svname, PFID(fid), rc);
370 osd_idc_dump_lma(env, osd, ino1, true);
372 osd_idc_dump_lma(env, osd, ino2, true);
376 * lookup mapping for given FID and fill it from the given object.
377 * the object is lolcal by definition.
379 static int osd_idc_find_and_init(const struct lu_env *env,
380 struct osd_device *osd,
381 struct osd_object *obj)
383 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
384 struct osd_idmap_cache *idc;
386 idc = osd_idc_find(env, osd, fid);
387 LASSERT(!IS_ERR(idc));
389 if (obj->oo_inode == NULL)
391 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
392 if (idc->oic_lid.oii_ino) {
393 osd_idc_dump_debug(env, osd, fid,
394 idc->oic_lid.oii_ino,
395 obj->oo_inode->i_ino);
398 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
399 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
404 CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
405 osd->od_svname, PFID(fid));
407 /* new mapping is needed */
408 idc = osd_idc_add(env, osd, fid);
410 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
411 osd->od_svname, PFID(fid), PTR_ERR(idc));
415 if (obj->oo_inode != NULL) {
416 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
417 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
423 * OSD object methods.
427 * Concurrency: no concurrent access is possible that early in object
430 static struct lu_object *osd_object_alloc(const struct lu_env *env,
431 const struct lu_object_header *hdr,
434 struct osd_object *mo;
439 struct lu_object_header *h;
440 struct osd_device *o = osd_dev(d);
442 l = &mo->oo_dt.do_lu;
443 if (unlikely(o->od_in_init)) {
450 lu_object_header_init(h);
451 lu_object_init(l, h, d);
452 lu_object_add_top(h, l);
455 dt_object_init(&mo->oo_dt, NULL, d);
456 mo->oo_header = NULL;
459 mo->oo_dt.do_ops = &osd_obj_ops;
460 l->lo_ops = &osd_lu_obj_ops;
461 init_rwsem(&mo->oo_sem);
462 init_rwsem(&mo->oo_ext_idx_sem);
463 spin_lock_init(&mo->oo_guard);
464 INIT_LIST_HEAD(&mo->oo_xattr_list);
470 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
471 struct dentry *dentry, struct lustre_ost_attrs *loa)
475 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
476 (void *)loa, sizeof(*loa));
478 struct lustre_mdt_attrs *lma = &loa->loa_lma;
480 if (rc < sizeof(*lma))
484 lustre_loa_swab(loa, true);
485 /* Check LMA compatibility */
486 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
488 CWARN("%s: unsupported incompat LMA feature(s) %#x for fid = "DFID", ino = %lu: rc = %d\n",
490 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
491 PFID(&lma->lma_self_fid), inode->i_ino, rc);
493 } else if (rc == 0) {
501 * retrieve object from backend ext fs.
503 static struct inode *osd_iget2(struct osd_thread_info *info,
504 struct osd_device *dev, struct osd_inode_id *id,
508 struct inode *inode = NULL;
511 * if we look for an inode withing a running
512 * transaction, then we risk to deadlock
513 * osd_dirent_check_repair() breaks this
515 /* LASSERT(current->journal_info == NULL); */
517 inode = osd_ldiskfs_iget(osd_sb(dev), id->oii_ino);
519 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
520 id->oii_ino, PTR_ERR(inode));
521 } else if (id->oii_gen != OSD_OII_NOGEN &&
522 inode->i_generation != id->oii_gen) {
523 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
524 "i_generation = %u\n",
525 id->oii_ino, id->oii_gen, inode->i_generation);
527 } else if (inode->i_nlink == 0) {
529 * due to parallel readdir and unlink,
530 * we can have dead inode here.
532 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
534 } else if (is_bad_inode(inode)) {
535 CWARN("%s: bad inode: ino = %u: rc = %d\n",
536 osd_dev2name(dev), id->oii_ino, -ENOENT);
538 } else if (osd_is_ea_inode(inode)) {
540 * EA inode is internal ldiskfs object, should don't visible
543 CDEBUG(D_INODE, "EA inode: ino = %u\n", id->oii_ino);
545 } else if ((rc = osd_attach_jinode(inode))) {
546 CDEBUG(D_INODE, "jbd: ino = %u rc = %d\n", id->oii_ino, rc);
548 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
549 if (id->oii_gen == OSD_OII_NOGEN)
550 osd_id_gen(id, inode->i_ino, inode->i_generation);
553 * Do not update file c/mtime in ldiskfs.
554 * NB: we don't have any lock to protect this because we don't
555 * have reference on osd_object now, but contention with
556 * another lookup + attr_set can't happen in the tiny window
557 * between if (...) and set S_NOCMTIME.
559 if (!(inode->i_flags & S_NOCMTIME))
560 inode->i_flags |= S_NOCMTIME;
568 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
569 struct osd_inode_id *id)
574 inode = osd_iget2(info, dev, id, &rc);
584 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
585 handle_t *handle, struct dentry *child,
586 struct inode *inode, struct htree_lock *hlock)
590 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
591 if (rc == -ENOBUFS || rc == -ENOSPC) {
592 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
593 struct inode *parent = child->d_parent->d_inode;
594 struct lu_fid *fid = NULL;
595 char fidstr[FID_LEN + 1] = "unknown";
597 rc2 = osd_get_lma(info, parent, child->d_parent, loa);
599 fid = &loa->loa_lma.lma_self_fid;
600 } else if (rc2 == -ENODATA) {
601 if (unlikely(is_root_inode(parent))) {
602 fid = &info->oti_fid3;
603 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
604 } else if (!osd->od_is_ost && osd->od_index == 0) {
605 fid = &info->oti_fid3;
606 lu_igif_build(fid, parent->i_ino,
607 parent->i_generation);
612 snprintf(fidstr, sizeof(fidstr), DFID, PFID(fid));
614 /* below message is checked in sanity.sh test_129 */
616 CWARN("%s: directory (inode: %lu, FID: %s) has reached max size limit\n",
617 osd_name(osd), parent->i_ino, fidstr);
619 rc = 0; /* ignore such error now */
620 CWARN("%s: directory (inode: %lu, FID: %s) is approaching max size limit\n",
621 osd_name(osd), parent->i_ino, fidstr);
631 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
632 struct osd_inode_id *id, struct lu_fid *fid)
634 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
638 inode = osd_iget(info, dev, id);
642 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
644 *fid = loa->loa_lma.lma_self_fid;
645 } else if (rc == -ENODATA) {
646 if (unlikely(is_root_inode(inode)))
647 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
649 lu_igif_build(fid, inode->i_ino, inode->i_generation);
657 static struct inode *osd_iget_check(struct osd_thread_info *info,
658 struct osd_device *dev,
659 const struct lu_fid *fid,
660 struct osd_inode_id *id,
669 * The cached OI mapping is trustable. If we cannot locate the inode
670 * via the cached OI mapping, then return the failure to the caller
671 * directly without further OI checking.
675 inode = osd_iget2(info, dev, id, &rc);
677 if (!trusted && (rc == -ENOENT || rc == -ESTALE))
680 CDEBUG(D_INODE, "no inode for FID: "DFID", ino = %u, rc = %d\n",
681 PFID(fid), id->oii_ino, rc);
687 __u32 saved_ino = id->oii_ino;
688 __u32 saved_gen = id->oii_gen;
691 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
693 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
695 * XXX: There are four possible cases:
697 * Backup/restore caused the OI invalid.
699 * Someone unlinked the object but NOT removed
700 * the OI mapping, such as mount target device
701 * as ldiskfs, and modify something directly.
703 * Someone just removed the object between the
704 * former oi_lookup and the iget. It is normal.
705 * 4. Other failure cases.
707 * Generally, when the device is mounted, it will
708 * auto check whether the system is restored from
709 * file-level backup or not. We trust such detect
710 * to distinguish the 1st case from the 2nd case:
711 * if the OI files are consistent but may contain
712 * stale OI mappings because of case 2, if iget()
713 * returns -ENOENT or -ESTALE, then it should be
718 * If the OI mapping was in OI file before the
719 * osd_iget_check(), but now, it is disappear,
720 * then it must be removed by race. That is a
726 * It is the OI scrub updated the OI mapping by race.
727 * The new OI mapping must be valid.
729 if (saved_ino != id->oii_ino ||
730 (saved_gen != id->oii_gen && saved_gen != OSD_OII_NOGEN)) {
739 if (dev->od_scrub.os_scrub.os_file.sf_flags &
742 * It still can be the case 2, but we cannot
743 * distinguish it from the case 1. So return
744 * -EREMCHG to block current operation until
745 * OI scrub rebuilt the OI mappings.
754 if (inode->i_generation == id->oii_gen)
774 * \retval +v: new filter_fid does not contain self-fid
775 * \retval 0: filter_fid_18_23, contains self-fid
776 * \retval -v: other failure cases
778 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
779 struct dentry *dentry, struct lu_fid *fid)
781 struct filter_fid *ff = &info->oti_ff;
782 struct ost_id *ostid = &info->oti_ostid;
785 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
786 if (rc == sizeof(struct filter_fid_18_23)) {
787 struct filter_fid_18_23 *ff_old = (void *)ff;
789 ostid_set_seq(ostid, le64_to_cpu(ff_old->ff_seq));
790 rc = ostid_set_id(ostid, le64_to_cpu(ff_old->ff_objid));
792 * XXX: use 0 as the index for compatibility, the caller will
793 * handle index related issues when necessary.
796 ostid_to_fid(fid, ostid, 0);
797 } else if (rc >= (int)sizeof(struct filter_fid_24_29)) {
799 } else if (rc >= 0) {
806 static int osd_lma_self_repair(struct osd_thread_info *info,
807 struct osd_device *osd, struct inode *inode,
808 const struct lu_fid *fid, __u32 compat)
813 LASSERT(current->journal_info == NULL);
815 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
816 osd_dto_credits_noquota[DTO_XATTR_SET]);
819 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
824 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
826 CWARN("%s: cannot self repair the LMA: rc = %d\n",
828 ldiskfs_journal_stop(jh);
832 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
834 struct osd_thread_info *info = osd_oti_get(env);
835 struct osd_device *osd = osd_obj2dev(obj);
836 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
837 struct lustre_mdt_attrs *lma = &loa->loa_lma;
838 struct inode *inode = obj->oo_inode;
839 struct dentry *dentry = &info->oti_obj_dentry;
840 struct lu_fid *fid = NULL;
841 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
846 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
847 (void *)loa, sizeof(*loa));
848 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
849 fid = &lma->lma_self_fid;
850 rc = osd_get_idif(info, inode, dentry, fid);
851 if (rc > 0 || (rc == -ENODATA && osd->od_index_in_idif)) {
853 * For the given OST-object, if it has neither LMA nor
854 * FID in XATTR_NAME_FID, then the given FID (which is
855 * contained in the @obj, from client RPC for locating
856 * the OST-object) is trusted. We use it to generate
859 osd_lma_self_repair(info, osd, inode, rfid,
870 lustre_lma_swab(lma);
871 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
872 (CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT) &&
873 S_ISREG(inode->i_mode)))) {
874 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
875 "fid = "DFID", ino = %lu\n", osd_name(osd),
876 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
877 PFID(rfid), inode->i_ino);
880 fid = &lma->lma_self_fid;
881 if (lma->lma_compat & LMAC_STRIPE_INFO &&
883 obj->oo_pfid_in_lma = 1;
884 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
886 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
890 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
891 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
892 struct ost_id *oi = &info->oti_ostid;
893 struct lu_fid *fid1 = &info->oti_fid3;
894 __u32 idx = fid_idif_ost_idx(rfid);
897 * For old IDIF, the OST index is not part of the IDIF,
898 * Means that different OSTs may have the same IDIFs.
899 * Under such case, we need to make some compatible
900 * check to make sure to trigger OI scrub properly.
902 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
903 /* Given @rfid is new, LMA is old. */
904 fid_to_ostid(fid, oi);
905 ostid_to_fid(fid1, oi, idx);
906 if (lu_fid_eq(fid1, rfid)) {
907 if (osd->od_index_in_idif)
908 osd_lma_self_repair(info, osd,
922 struct osd_check_lmv_buf {
923 /* please keep it as first member */
924 struct dir_context ctx;
925 struct osd_thread_info *oclb_info;
926 struct osd_device *oclb_dev;
932 * It is called internally by ->iterate*() to filter out the
933 * local slave object's FID of the striped directory.
935 * \retval 1 found the local slave's FID
936 * \retval 0 continue to check next item
937 * \retval -ve for failure
939 #ifdef HAVE_FILLDIR_USE_CTX
940 static FILLDIR_TYPE do_osd_stripe_dir_filldir(struct dir_context *buf,
942 static int osd_stripe_dir_filldir(void *buf,
944 const char *name, int namelen,
945 loff_t offset, __u64 ino, unsigned int d_type)
947 struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
948 struct osd_thread_info *oti = oclb->oclb_info;
949 struct lu_fid *fid = &oti->oti_fid3;
950 struct osd_inode_id *id = &oti->oti_id3;
951 struct osd_device *dev = oclb->oclb_dev;
960 sscanf(name + 1, SFID, RFID(fid));
961 if (!fid_is_sane(fid))
964 if (osd_remote_fid(oti->oti_env, dev, fid))
967 osd_id_gen(id, ino, OSD_OII_NOGEN);
968 inode = osd_iget(oti, dev, id);
970 return PTR_ERR(inode);
973 osd_add_oi_cache(oti, dev, id, fid);
974 osd_scrub_oi_insert(dev, fid, id, true);
975 oclb->oclb_found = true;
980 WRAP_FILLDIR_FN(do_, osd_stripe_dir_filldir)
983 * When lookup item under striped directory, we need to locate the master
984 * MDT-object of the striped directory firstly, then the client will send
985 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
986 * and the item's name. If the system is restored from MDT file level backup,
987 * then before the OI scrub completely built the OI files, the OI mappings of
988 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
989 * not a problem for the master MDT-object. Because when locate the master
990 * MDT-object, we will do name based lookup (for the striped directory itself)
991 * firstly, during such process we can setup the correct OI mapping for the
992 * master MDT-object. But it will be trouble for the slave MDT-object. Because
993 * the client will not trigger name based lookup on the MDT to locate the slave
994 * MDT-object before locating item under the striped directory, then when
995 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
996 * is invalid and does not know what the right OI mapping is, then the MDT has
997 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
998 * the OI file, related OI mapping is unknown yet, please try again later. And
999 * then client will re-try the RPC again and again until related OI mapping has
1000 * been updated. That is quite inefficient.
1002 * To resolve above trouble, we will handle it as the following two cases:
1004 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
1005 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
1006 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
1007 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
1008 * directly. Please check osd_fid_lookup().
1010 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
1011 * Under such case, during lookup the master MDT-object, we will lookup the
1012 * slave MDT-object via readdir against the master MDT-object, because the
1013 * slave MDT-objects information are stored as sub-directories with the name
1014 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
1015 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
1016 * the correct OI mapping for the slave MDT-object.
1018 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
1019 struct inode *inode)
1021 struct lu_buf *buf = &oti->oti_big_buf;
1023 struct lmv_mds_md_v1 *lmv1;
1024 struct osd_check_lmv_buf oclb = {
1025 .ctx.actor = osd_stripe_dir_filldir,
1028 .oclb_found = false,
1033 /* We should use the VFS layer to create a real dentry. */
1034 oti->oti_obj_dentry.d_inode = inode;
1035 oti->oti_obj_dentry.d_sb = inode->i_sb;
1037 filp = alloc_file_pseudo(inode, dev->od_mnt, "/", O_NOATIME,
1042 filp->f_mode |= FMODE_64BITHASH;
1046 rc = __osd_xattr_get(inode, filp->f_path.dentry, XATTR_NAME_LMV,
1047 buf->lb_buf, buf->lb_len);
1048 if (rc == -ERANGE) {
1049 rc = __osd_xattr_get(inode, filp->f_path.dentry,
1050 XATTR_NAME_LMV, NULL, 0);
1052 lu_buf_realloc(buf, rc);
1053 if (buf->lb_buf == NULL)
1054 GOTO(out, rc = -ENOMEM);
1060 if (unlikely(rc == 0 || rc == -ENODATA))
1066 if (unlikely(buf->lb_buf == NULL)) {
1067 lu_buf_realloc(buf, rc);
1068 if (buf->lb_buf == NULL)
1069 GOTO(out, rc = -ENOMEM);
1075 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1079 oclb.oclb_items = 0;
1080 rc = iterate_dir(filp, &oclb.ctx);
1081 } while (rc >= 0 && oclb.oclb_items > 0 && !oclb.oclb_found &&
1082 filp->f_pos != LDISKFS_HTREE_EOF_64BIT);
1087 "%s: cannot check LMV, ino = %lu/%u: rc = %d\n",
1088 osd_ino2name(inode), inode->i_ino, inode->i_generation,
1097 * Is object in scrub inconsistent/stale list.
1099 * \a scrub has two lists, os_inconsistent_items contains mappings to fix, while
1100 * os_stale_items contains mappings failed to fix.
1102 static bool fid_in_scrub_list(struct lustre_scrub *scrub,
1103 const struct list_head *list,
1104 const struct lu_fid *fid)
1106 struct osd_inconsistent_item *oii;
1108 if (list_empty(list))
1111 spin_lock(&scrub->os_lock);
1112 list_for_each_entry(oii, list, oii_list) {
1113 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
1114 spin_unlock(&scrub->os_lock);
1118 spin_unlock(&scrub->os_lock);
1123 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
1124 const struct lu_fid *fid,
1125 const struct lu_object_conf *conf)
1127 struct osd_thread_info *info;
1128 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1129 struct osd_device *dev;
1130 struct osd_idmap_cache *oic;
1131 struct osd_inode_id *id;
1132 struct inode *inode = NULL;
1133 struct lustre_scrub *scrub;
1134 struct scrub_file *sf;
1135 __u32 flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT | SS_AUTO_FULL;
1140 bool remote = false;
1141 bool trusted = true;
1142 bool updated = false;
1143 bool checked = false;
1148 LINVRNT(osd_invariant(obj));
1149 LASSERT(obj->oo_inode == NULL);
1151 if (fid_is_sane(fid) == 0) {
1152 CERROR("%s: invalid FID "DFID"\n", ldev->ld_obd->obd_name,
1158 dev = osd_dev(ldev);
1159 scrub = &dev->od_scrub.os_scrub;
1160 sf = &scrub->os_file;
1161 info = osd_oti_get(env);
1163 oic = &info->oti_cache;
1165 if (CFS_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
1169 * For the object is created as locking anchor, or for the object to
1170 * be created on disk. No need to osd_oi_lookup() at here because FID
1171 * shouldn't never be re-used, if it's really a duplicate FID from
1172 * unexpected reason, we should be able to detect it later by calling
1173 * do_create->osd_oi_insert().
1175 if (conf && conf->loc_flags & LOC_F_NEW)
1178 /* Search order: 1. per-thread cache. */
1179 if (lu_fid_eq(fid, &oic->oic_fid) && likely(oic->oic_dev == dev)) {
1184 /* Search order: 2. OI scrub pending list. */
1186 memset(id, 0, sizeof(struct osd_inode_id));
1187 if (fid_in_scrub_list(scrub, &scrub->os_inconsistent_items, fid) &&
1189 RETURN(-EINPROGRESS);
1191 stale = fid_in_scrub_list(scrub, &scrub->os_stale_items, fid);
1192 if (stale && CFS_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_STALE))
1196 * The OI mapping in the OI file can be updated by the OI scrub
1197 * when we locate the inode via FID. So it may be not trustable.
1201 /* Search order: 3. OI files. */
1202 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1203 if (result == -ENOENT) {
1204 if (!fid_is_norm(fid) ||
1205 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1206 !ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
1208 GOTO(out, result = 0);
1213 /* -ESTALE is returned if inode of OST object doesn't exist */
1214 if (result == -ESTALE &&
1215 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1216 GOTO(out, result = 0);
1223 obj->oo_inode = NULL;
1224 /* for later passes through checks, not true on first pass */
1225 if (!IS_ERR_OR_NULL(inode))
1228 inode = osd_iget_check(info, dev, fid, id, trusted);
1229 if (!IS_ERR(inode)) {
1230 obj->oo_inode = inode;
1238 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_STALE))
1241 result = PTR_ERR(inode);
1242 if (result == -ENOENT || result == -ESTALE)
1243 GOTO(out, result = 0);
1245 if (result != -EREMCHG)
1249 /* don't trigger repeatedly for stale mapping */
1251 GOTO(out, result = -ESTALE);
1254 * We still have chance to get the valid inode: for the
1255 * object which is referenced by remote name entry, the
1256 * object on the local MDT will be linked under the dir
1257 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1259 * We do not know whether the object for the given FID
1260 * is referenced by some remote name entry or not, and
1261 * especially for DNE II, a multiple-linked object may
1262 * have many name entries reside on many MDTs.
1264 * To simplify the operation, OSD will not distinguish
1265 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1266 * only happened for the RPC from other MDT during the
1267 * OI scrub, or for the client side RPC with FID only,
1268 * such as FID to path, or from old connected client.
1271 rc1 = osd_lookup_in_remote_parent(info, dev, fid, id);
1275 flags |= SS_AUTO_PARTIAL;
1276 flags &= ~SS_AUTO_FULL;
1281 if (scrub->os_running) {
1282 if (scrub->os_partial_scan && !scrub->os_in_join)
1285 if (IS_ERR_OR_NULL(inode) || result) {
1286 osd_scrub_oi_insert(dev, fid, id, result == -ENOENT);
1287 GOTO(out, result = -EINPROGRESS);
1291 LASSERT(obj->oo_inode == inode);
1293 osd_scrub_oi_insert(dev, fid, id, true);
1297 if (dev->od_scrub.os_scrub.os_auto_scrub_interval == AS_NEVER) {
1299 GOTO(out, result = -EREMCHG);
1302 LASSERT(obj->oo_inode == inode);
1304 osd_add_oi_cache(info, dev, id, fid);
1309 if (IS_ERR_OR_NULL(inode) || result)
1310 osd_scrub_oi_insert(dev, fid, id, result == -ENOENT);
1312 rc1 = osd_scrub_start(env, dev, flags);
1313 CDEBUG_LIMIT(D_LFSCK | D_CONSOLE | D_WARNING,
1314 "%s: trigger OI scrub by RPC for "DFID"/%u with flags %#x: rc = %d\n",
1315 osd_name(dev), PFID(fid), id->oii_ino, flags, rc1);
1316 if (rc1 && rc1 != -EALREADY)
1317 GOTO(out, result = -EREMCHG);
1319 if (IS_ERR_OR_NULL(inode) || result)
1320 GOTO(out, result = -EINPROGRESS);
1323 LASSERT(obj->oo_inode == inode);
1328 if (unlikely(obj->oo_header))
1331 result = osd_check_lma(env, obj);
1335 LASSERTF(id->oii_ino == inode->i_ino &&
1336 id->oii_gen == inode->i_generation,
1337 "locate wrong inode for FID: "DFID", %u/%u => %ld/%u\n",
1338 PFID(fid), id->oii_ino, id->oii_gen,
1339 inode->i_ino, inode->i_generation);
1341 saved_ino = inode->i_ino;
1342 saved_gen = inode->i_generation;
1344 if (unlikely(result == -ENODATA)) {
1346 * If the OI scrub updated the OI mapping by race, it
1347 * must be valid. Trust the inode that has no LMA EA.
1352 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1355 * The OI mapping is still there, the inode is still
1356 * valid. It is just becaues the inode has no LMA EA.
1358 if (saved_ino == id->oii_ino &&
1359 saved_gen == id->oii_gen)
1363 * It is the OI scrub updated the OI mapping by race.
1364 * The new OI mapping must be valid.
1372 * "result == -ENOENT" means that the OI mappinghas been
1373 * removed by race, so the inode belongs to other object.
1375 * Others error can be returned directly.
1377 if (result == -ENOENT) {
1378 obj->oo_inode = NULL;
1383 if (result != -EREMCHG)
1389 * if two OST objects map to the same inode, and inode mode is
1390 * (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666), which means it's
1391 * reserved by precreate, and not written yet, in this case, don't
1392 * set inode for the object whose FID mismatch, so that it can create
1393 * inode and not block precreate.
1395 if (fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) &&
1396 inode->i_mode == (S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666)) {
1397 obj->oo_inode = NULL;
1398 GOTO(out, result = 0);
1401 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1403 * "result == -ENOENT" means the cached OI mapping has been removed
1404 * from the OI file by race, above inode belongs to other object.
1406 if (result == -ENOENT) {
1407 obj->oo_inode = NULL;
1408 GOTO(out, result = 0);
1414 if (saved_ino == id->oii_ino && saved_gen == id->oii_gen) {
1416 osd_scrub_refresh_mapping(info, dev, fid, id, DTO_INDEX_DELETE,
1422 * It is the OI scrub updated the OI mapping by race.
1423 * The new OI mapping must be valid.
1431 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
1432 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
1434 result = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
1436 if (lma->lma_compat & LMAC_STRIPE_INFO &&
1438 obj->oo_pfid_in_lma = 1;
1439 if (unlikely(lma->lma_incompat & LMAI_REMOTE_PARENT) &&
1441 lu_object_set_agent_entry(&obj->oo_dt.do_lu);
1442 } else if (result != -ENODATA) {
1447 obj->oo_compat_dot_created = 1;
1448 obj->oo_compat_dotdot_created = 1;
1450 if (S_ISDIR(inode->i_mode) &&
1451 (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1452 osd_check_lmv(info, dev, inode);
1454 result = osd_attach_jinode(inode);
1459 GOTO(out, result = 0);
1461 LASSERT(!obj->oo_hl_head);
1462 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1464 GOTO(out, result = (!obj->oo_hl_head ? -ENOMEM : 0));
1467 if (!result && stale)
1468 osd_scrub_oi_resurrect(scrub, fid);
1470 if (result || !obj->oo_inode) {
1471 if (!IS_ERR_OR_NULL(inode))
1474 obj->oo_inode = NULL;
1476 fid_zero(&oic->oic_fid);
1479 LINVRNT(osd_invariant(obj));
1484 * Concurrency: shouldn't matter.
1486 static void osd_object_init0(struct osd_object *obj)
1488 LASSERT(obj->oo_inode != NULL);
1489 obj->oo_dt.do_body_ops = &osd_body_ops;
1490 obj->oo_dt.do_lu.lo_header->loh_attr |=
1491 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1495 * Concurrency: no concurrent access is possible that early in object
1498 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1499 const struct lu_object_conf *conf)
1501 struct osd_object *obj = osd_obj(l);
1504 LINVRNT(osd_invariant(obj));
1506 if (CFS_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
1507 cfs_fail_val == 2) {
1508 struct osd_thread_info *info = osd_oti_get(env);
1509 struct osd_idmap_cache *oic = &info->oti_cache;
1510 /* invalidate thread cache */
1511 memset(&oic->oic_fid, 0, sizeof(oic->oic_fid));
1513 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1514 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1515 l->lo_header->loh_attr |= LOHA_EXISTS;
1519 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1520 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1521 if (result == 0 && obj->oo_inode != NULL) {
1522 struct osd_thread_info *oti = osd_oti_get(env);
1523 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
1525 osd_object_init0(obj);
1526 if (unlikely(obj->oo_header))
1529 result = osd_get_lma(oti, obj->oo_inode,
1530 &oti->oti_obj_dentry, loa);
1533 * Convert LMAI flags to lustre LMA flags
1534 * and cache it to oo_lma_flags
1537 lma_to_lustre_flags(loa->loa_lma.lma_incompat);
1538 } else if (result == -ENODATA) {
1542 obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
1544 LINVRNT(osd_invariant(obj));
1549 * The first part of oxe_buf is xattr name, and is '\0' terminated.
1550 * The left part is for value, binary mode.
1552 struct osd_xattr_entry {
1553 struct list_head oxe_list;
1557 struct rcu_head oxe_rcu;
1561 static int osd_oxc_get(struct osd_object *obj, const char *name,
1564 struct osd_xattr_entry *tmp;
1565 struct osd_xattr_entry *oxe = NULL;
1566 size_t namelen = strlen(name);
1570 list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1571 if (namelen == tmp->oxe_namelen &&
1572 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1579 GOTO(out, rc = -ENOENT);
1581 if (!oxe->oxe_exist)
1582 GOTO(out, rc = -ENODATA);
1585 rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1588 if (buf->lb_buf == NULL)
1591 if (buf->lb_len < rc)
1592 GOTO(out, rc = -ERANGE);
1594 memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1601 static void osd_oxc_free(struct rcu_head *head)
1603 struct osd_xattr_entry *oxe;
1605 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1606 OBD_FREE(oxe, oxe->oxe_len);
1609 static void osd_oxc_add(struct osd_object *obj, const char *name,
1610 const char *buf, int buflen)
1612 struct osd_xattr_entry *oxe;
1613 struct osd_xattr_entry *old = NULL;
1614 struct osd_xattr_entry *tmp;
1615 size_t namelen = strlen(name);
1616 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1618 OBD_ALLOC(oxe, len);
1622 INIT_LIST_HEAD(&oxe->oxe_list);
1624 oxe->oxe_namelen = namelen;
1625 memcpy(oxe->oxe_buf, name, namelen);
1627 LASSERT(buf != NULL);
1628 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1629 oxe->oxe_exist = true;
1631 oxe->oxe_exist = false;
1634 /* this should be rarely called, just remove old and add new */
1635 spin_lock(&obj->oo_guard);
1636 list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1637 if (namelen == tmp->oxe_namelen &&
1638 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1644 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1645 call_rcu(&old->oxe_rcu, osd_oxc_free);
1647 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1649 spin_unlock(&obj->oo_guard);
1652 static void osd_oxc_del(struct osd_object *obj, const char *name)
1654 struct osd_xattr_entry *oxe;
1655 size_t namelen = strlen(name);
1657 spin_lock(&obj->oo_guard);
1658 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1659 if (namelen == oxe->oxe_namelen &&
1660 strncmp(name, oxe->oxe_buf, namelen) == 0) {
1661 list_del_rcu(&oxe->oxe_list);
1662 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1666 spin_unlock(&obj->oo_guard);
1669 static void osd_oxc_fini(struct osd_object *obj)
1671 struct osd_xattr_entry *oxe, *next;
1673 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1674 list_del(&oxe->oxe_list);
1675 OBD_FREE(oxe, oxe->oxe_len);
1680 * Concurrency: no concurrent access is possible that late in object
1683 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1685 struct osd_object *obj = osd_obj(l);
1686 struct lu_object_header *h = obj->oo_header;
1688 LINVRNT(osd_invariant(obj));
1691 dt_object_fini(&obj->oo_dt);
1692 if (obj->oo_hl_head != NULL)
1693 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1694 /* obj doesn't contain an lu_object_header, so we don't need call_rcu */
1697 lu_object_header_free(h);
1701 * Concurrency: no concurrent access is possible that late in object
1704 static void osd_index_fini(struct osd_object *o)
1706 struct iam_container *bag;
1708 if (o->oo_dir != NULL) {
1709 bag = &o->oo_dir->od_container;
1710 if (o->oo_inode != NULL) {
1711 if (bag->ic_object == o->oo_inode)
1712 iam_container_fini(bag);
1714 OBD_FREE_PTR(o->oo_dir);
1720 OSD_TXN_OI_DELETE_CREDITS = 20,
1721 OSD_TXN_INODE_DELETE_CREDITS = 20
1728 #if OSD_THANDLE_STATS
1730 * Set time when the handle is allocated
1732 static void osd_th_alloced(struct osd_thandle *oth)
1734 oth->oth_alloced = ktime_get();
1738 * Set time when the handle started
1740 static void osd_th_started(struct osd_thandle *oth)
1742 oth->oth_started = ktime_get();
1746 * Check whether the we deal with this handle for too long.
1748 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1749 ktime_t alloced, ktime_t started,
1752 ktime_t now = ktime_get();
1754 LASSERT(dev != NULL);
1756 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1757 ktime_us_delta(started, alloced));
1758 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1759 ktime_us_delta(closed, started));
1760 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1761 ktime_us_delta(now, closed));
1763 if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1764 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1765 oth, now, alloced, started, closed);
1766 libcfs_debug_dumpstack(NULL);
1770 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1772 ktime_t __closed = ktime_get(); \
1773 ktime_t __alloced = oth->oth_alloced; \
1774 ktime_t __started = oth->oth_started; \
1777 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1780 #else /* OSD_THANDLE_STATS */
1782 #define osd_th_alloced(h) do {} while(0)
1783 #define osd_th_started(h) do {} while(0)
1784 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1786 #endif /* OSD_THANDLE_STATS */
1789 * in some cases (like overstriped files) the same operations on the same
1790 * objects are declared many times and this may lead to huge number of
1791 * credits which can be a problem and/or cause performance degradation.
1792 * this function is to remember what declarations have been made within
1793 * a given thandle and then skip duplications.
1794 * limit it's scope so that regular small transactions don't need all
1795 * this overhead with allocations, lists.
1796 * also, limit scope to the specific objects like llogs, etc.
1798 static inline bool osd_check_special_fid(const struct lu_fid *f)
1800 if (fid_seq_is_llog(f->f_seq))
1802 if (f->f_seq == FID_SEQ_LOCAL_FILE &&
1803 f->f_oid == MDD_LOV_OBJ_OID)
1808 bool osd_tx_was_declared(const struct lu_env *env, struct osd_thandle *oth,
1809 struct dt_object *dt, enum dt_txn_op op, loff_t pos)
1811 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1812 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1813 struct osd_thread_info *oti = osd_oti_get(env);
1814 struct osd_obj_declare *old;
1819 /* small transactions don't need this overhead */
1820 if (oti->oti_declare_ops[DTO_OBJECT_CREATE] < 10 &&
1821 oti->oti_declare_ops[DTO_WRITE_BASE] < 10)
1824 if (osd_check_special_fid(fid) == 0)
1827 list_for_each_entry(old, &oth->ot_declare_list, old_list) {
1828 if (old->old_op == op && old->old_pos == pos &&
1829 lu_fid_eq(&old->old_fid, fid))
1833 if (unlikely(old == NULL))
1835 old->old_fid = *lu_object_fid(&dt->do_lu);
1838 list_add(&old->old_list, &oth->ot_declare_list);
1842 void osd_tx_declaration_free(struct osd_thandle *oth)
1844 struct osd_obj_declare *old, *tmp;
1846 list_for_each_entry_safe(old, tmp, &oth->ot_declare_list, old_list) {
1847 list_del_init(&old->old_list);
1853 * Concurrency: doesn't access mutable data.
1855 static int osd_param_is_not_sane(const struct osd_device *dev,
1856 const struct thandle *th)
1858 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1860 return oh->ot_credits > osd_transaction_size(dev);
1864 * Concurrency: shouldn't matter.
1866 static void osd_trans_commit_cb(struct super_block *sb,
1867 struct ldiskfs_journal_cb_entry *jcb, int error)
1869 struct osd_thandle *oh = container_of(jcb, struct osd_thandle, ot_jcb);
1870 struct thandle *th = &oh->ot_super;
1871 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1872 struct osd_device *osd = osd_dev(lud);
1873 struct dt_txn_commit_cb *dcb, *tmp;
1875 LASSERT(oh->ot_handle == NULL);
1878 CERROR("transaction @0x%p commit error: %d\n", th, error);
1880 /* call per-transaction callbacks if any */
1881 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1883 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1884 "commit callback entry: magic=%x name='%s'\n",
1885 dcb->dcb_magic, dcb->dcb_name);
1886 list_del_init(&dcb->dcb_linkage);
1887 dcb->dcb_func(NULL, th, dcb, error);
1890 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1891 if (atomic_dec_and_test(&osd->od_commit_cb_in_flight))
1892 wake_up(&osd->od_commit_cb_done);
1898 static struct thandle *osd_trans_create(const struct lu_env *env,
1899 struct dt_device *d)
1901 struct osd_thread_info *oti = osd_oti_get(env);
1902 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1903 struct osd_thandle *oh;
1909 CERROR("%s: someone try to start transaction under "
1910 "readonly mode, should be disabled.\n",
1911 osd_name(osd_dt_dev(d)));
1913 RETURN(ERR_PTR(-EROFS));
1916 /* on pending IO in this thread should left from prev. request */
1917 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1919 sb_start_write(osd_sb(osd_dt_dev(d)));
1921 OBD_ALLOC_GFP(oh, sizeof(*oh), GFP_NOFS);
1923 sb_end_write(osd_sb(osd_dt_dev(d)));
1924 RETURN(ERR_PTR(-ENOMEM));
1927 oh->ot_quota_trans = &oti->oti_quota_trans;
1928 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1933 oh->oh_declared_ext = 0;
1934 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1935 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1936 INIT_LIST_HEAD(&oh->ot_trunc_locks);
1937 INIT_LIST_HEAD(&oh->ot_declare_list);
1940 memset(oti->oti_declare_ops, 0,
1941 sizeof(oti->oti_declare_ops));
1942 memset(oti->oti_declare_ops_cred, 0,
1943 sizeof(oti->oti_declare_ops_cred));
1944 memset(oti->oti_declare_ops_used, 0,
1945 sizeof(oti->oti_declare_ops_used));
1947 oti->oti_ins_cache_depth++;
1952 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1954 struct osd_thread_info *oti = osd_oti_get(env);
1955 struct osd_thandle *oh;
1957 oh = container_of(th, struct osd_thandle, ot_super);
1958 LASSERT(oh != NULL);
1960 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1961 oti->oti_declare_ops[OSD_OT_CREATE],
1962 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1963 oti->oti_declare_ops_used[OSD_OT_CREATE],
1964 oti->oti_declare_ops[OSD_OT_DESTROY],
1965 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1966 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1967 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1968 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1969 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1970 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1971 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1972 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1973 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1974 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1975 oti->oti_declare_ops[OSD_OT_WRITE],
1976 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1977 oti->oti_declare_ops_used[OSD_OT_WRITE],
1978 oti->oti_declare_ops[OSD_OT_PUNCH],
1979 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1980 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1981 oti->oti_declare_ops[OSD_OT_QUOTA],
1982 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1983 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1984 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1985 oti->oti_declare_ops[OSD_OT_INSERT],
1986 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1987 oti->oti_declare_ops_used[OSD_OT_INSERT],
1988 oti->oti_declare_ops[OSD_OT_DELETE],
1989 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1990 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1991 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1992 oti->oti_declare_ops[OSD_OT_REF_ADD],
1993 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1994 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1995 oti->oti_declare_ops[OSD_OT_REF_DEL],
1996 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1997 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
2001 * Concurrency: shouldn't matter.
2003 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
2006 struct osd_thread_info *oti = osd_oti_get(env);
2007 struct osd_device *dev = osd_dt_dev(d);
2009 struct osd_thandle *oh;
2014 LASSERT(current->journal_info == NULL);
2016 oh = container_of(th, struct osd_thandle, ot_super);
2017 LASSERT(oh != NULL);
2018 LASSERT(oh->ot_handle == NULL);
2019 if (unlikely(ldiskfs_track_declares_assert != 0)) {
2020 LASSERT(oti->oti_r_locks == 0);
2021 LASSERT(oti->oti_w_locks == 0);
2024 rc = dt_txn_hook_start(env, d, th);
2028 if (unlikely(osd_param_is_not_sane(dev, th))) {
2029 static unsigned long last_printed;
2030 static int last_credits;
2032 lprocfs_counter_add(dev->od_stats,
2033 LPROC_OSD_TOO_MANY_CREDITS, 1);
2036 * don't make noise on a tiny testing systems
2037 * actual credits misuse will be caught anyway
2039 if (last_credits != oh->ot_credits &&
2040 time_after(jiffies, last_printed +
2041 cfs_time_seconds(60)) &&
2042 osd_transaction_size(dev) > 512) {
2043 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
2044 oh->ot_credits, osd_transaction_size(dev));
2045 osd_trans_dump_creds(env, th);
2046 libcfs_debug_dumpstack(NULL);
2047 last_credits = oh->ot_credits;
2048 last_printed = jiffies;
2051 * XXX Limit the credits to 'max_transaction_buffers', and
2052 * let the underlying filesystem to catch the error if
2053 * we really need so many credits.
2055 * This should be removed when we can calculate the
2056 * credits precisely.
2058 oh->ot_credits = osd_transaction_size(dev);
2059 } else if (ldiskfs_track_declares_assert != 0) {
2061 * reserve few credits to prevent an assertion in JBD
2062 * our debugging mechanism will be able to detected
2063 * overuse. this can help to debug single-update
2066 oh->ot_credits += 10;
2067 if (unlikely(osd_param_is_not_sane(dev, th)))
2068 oh->ot_credits = osd_transaction_size(dev);
2071 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_TXN_START))
2072 GOTO(out, rc = -EIO);
2075 * we ignore quota checks for system-owned files, but still
2076 * need to count blocks for uid/gid/projid
2078 osd_trans_declare_op(env, oh, OSD_OT_QUOTA, 3);
2081 * XXX temporary stuff. Some abstraction layer should
2084 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
2088 LASSERT(oti->oti_txns == 0);
2090 atomic_inc(&dev->od_commit_cb_in_flight);
2091 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
2102 static int osd_seq_exists(const struct lu_env *env,
2103 struct osd_device *osd, u64 seq)
2105 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2106 struct seq_server_site *ss = osd_seq_site(osd);
2111 LASSERT(ss != NULL);
2112 LASSERT(ss->ss_server_fld != NULL);
2114 rc = osd_fld_lookup(env, osd, seq, range);
2117 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
2118 osd_name(osd), seq, rc);
2122 RETURN(ss->ss_node_id == range->lsr_index);
2125 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
2127 struct dt_txn_commit_cb *dcb;
2128 struct dt_txn_commit_cb *tmp;
2130 /* call per-transaction stop callbacks if any */
2131 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
2133 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
2134 "commit callback entry: magic=%x name='%s'\n",
2135 dcb->dcb_magic, dcb->dcb_name);
2136 list_del_init(&dcb->dcb_linkage);
2137 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
2142 * Concurrency: shouldn't matter.
2144 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
2147 struct osd_thread_info *oti = osd_oti_get(env);
2148 struct osd_thandle *oh;
2149 struct osd_iobuf *iobuf = &oti->oti_iobuf;
2150 struct osd_device *osd = osd_dt_dev(th->th_dev);
2151 struct qsd_instance *qsd = osd_def_qsd(osd);
2152 struct lquota_trans *qtrans;
2153 LIST_HEAD(truncates);
2154 int rc = 0, remove_agents = 0;
2158 oh = container_of(th, struct osd_thandle, ot_super);
2160 remove_agents = oh->ot_remove_agents;
2162 qtrans = oh->ot_quota_trans;
2163 oh->ot_quota_trans = NULL;
2165 osd_tx_declaration_free(oh);
2167 /* move locks to local list, stop tx, execute truncates */
2168 list_splice(&oh->ot_trunc_locks, &truncates);
2170 if (oh->ot_handle != NULL) {
2173 handle_t *hdl = oh->ot_handle;
2176 * add commit callback
2177 * notice we don't do this in osd_trans_start()
2178 * as underlying transaction can change during truncate
2180 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
2183 LASSERT(oti->oti_txns == 1);
2186 rc = dt_txn_hook_stop(env, th);
2188 CERROR("%s: failed in transaction hook: rc = %d\n",
2191 osd_trans_stop_cb(oh, rc);
2192 /* hook functions might modify th_sync */
2193 hdl->h_sync = th->th_sync;
2195 oh->ot_handle = NULL;
2196 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
2198 CERROR("%s: failed to stop transaction: rc = %d\n",
2199 osd_name(osd), rc2);
2203 /* We preserve the origin behavior of ignoring any
2204 * failures with the underlying punch / truncate
2205 * operation. We do record for debugging if an error
2206 * does occur in the lctl dk logs.
2208 rc2 = osd_process_truncates(env, &truncates);
2210 CERROR("%s: failed truncate process: rc = %d\n",
2211 osd_name(osd), rc2);
2213 osd_trans_stop_cb(oh, th->th_result);
2217 osd_trunc_unlock_all(env, &truncates);
2219 /* inform the quota slave device that the transaction is stopping */
2220 qsd_op_end(env, qsd, qtrans);
2223 * as we want IO to journal and data IO be concurrent, we don't block
2224 * awaiting data IO completion in osd_do_bio(), instead we wait here
2225 * once transaction is submitted to the journal. all reqular requests
2226 * don't do direct IO (except read/write), thus this wait_event becomes
2229 * IMPORTANT: we have to wait till any IO submited by the thread is
2230 * completed otherwise iobuf may be corrupted by different request
2232 wait_event(iobuf->dr_wait,
2233 atomic_read(&iobuf->dr_numreqs) == 0);
2236 rc = iobuf->dr_error;
2238 osd_fini_iobuf(osd, iobuf);
2240 if (unlikely(remove_agents != 0))
2241 osd_process_scheduled_agent_removals(env, osd);
2243 LASSERT(oti->oti_ins_cache_depth > 0);
2244 oti->oti_ins_cache_depth--;
2245 /* reset OI cache for safety */
2246 if (oti->oti_ins_cache_depth == 0)
2247 oti->oti_ins_cache_used = 0;
2249 sb_end_write(osd_sb(osd));
2254 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
2256 struct osd_thandle *oh = container_of(th, struct osd_thandle,
2259 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
2260 LASSERT(&dcb->dcb_func != NULL);
2261 if (dcb->dcb_flags & DCB_TRANS_STOP)
2262 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
2264 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
2270 * Called just before object is freed. Releases all resources except for
2271 * object itself (that is released by osd_object_free()).
2273 * Concurrency: no concurrent access is possible that late in object
2276 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
2278 struct osd_object *obj = osd_obj(l);
2279 struct qsd_instance *qsd = osd_def_qsd(osd_obj2dev(obj));
2280 struct inode *inode = obj->oo_inode;
2285 LINVRNT(osd_invariant(obj));
2288 * If object is unlinked remove fid->ino mapping from object index.
2291 osd_index_fini(obj);
2296 if (osd_has_index(obj) && obj->oo_dt.do_index_ops == &osd_index_iam_ops)
2297 ldiskfs_set_inode_flag(inode, LDISKFS_INODE_JOURNAL_DATA);
2299 uid = i_uid_read(inode);
2300 gid = i_gid_read(inode);
2301 projid = i_projid_read(inode);
2303 obj->oo_inode = NULL;
2306 /* do not rebalance quota if the caller needs to release memory
2307 * otherwise qsd_refresh_usage() may went into a new ldiskfs
2308 * transaction and risk to deadlock - LU-12178 */
2309 if (current->flags & (PF_MEMALLOC | PF_KSWAPD))
2312 if (!obj->oo_header && qsd) {
2313 struct osd_thread_info *info = osd_oti_get(env);
2314 struct lquota_id_info *qi = &info->oti_qi;
2316 /* Release granted quota to master if necessary */
2317 qi->lqi_id.qid_uid = uid;
2318 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
2320 qi->lqi_id.qid_uid = gid;
2321 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
2323 qi->lqi_id.qid_uid = projid;
2324 qsd_op_adjust(env, qsd, &qi->lqi_id, PRJQUOTA);
2329 * Concurrency: ->loo_object_release() is called under site spin-lock.
2331 static void osd_object_release(const struct lu_env *env,
2332 struct lu_object *l)
2334 struct osd_object *o = osd_obj(l);
2337 * nobody should be releasing a non-destroyed object with nlink=0
2338 * the API allows this, but ldiskfs doesn't like and then report
2339 * this inode as deleted
2341 LASSERT(!(o->oo_destroyed == 0 && o->oo_inode &&
2342 o->oo_inode->i_nlink == 0));
2346 * Concurrency: shouldn't matter.
2348 static int osd_object_print(const struct lu_env *env, void *cookie,
2349 lu_printer_t p, const struct lu_object *l)
2351 struct osd_object *o = osd_obj(l);
2352 struct iam_descr *d;
2354 if (o->oo_dir != NULL)
2355 d = o->oo_dir->od_container.ic_descr;
2358 return (*p)(env, cookie,
2359 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
2361 o->oo_inode ? o->oo_inode->i_ino : 0UL,
2362 o->oo_inode ? o->oo_inode->i_generation : 0,
2363 d ? d->id_ops->id_name : "plain");
2367 * Concurrency: shouldn't matter.
2369 int osd_statfs(const struct lu_env *env, struct dt_device *d,
2370 struct obd_statfs *sfs, struct obd_statfs_info *info)
2372 struct osd_device *osd = osd_dt_dev(d);
2373 struct super_block *sb = osd_sb(osd);
2374 struct kstatfs *ksfs;
2378 if (unlikely(osd->od_mnt == NULL))
2379 return -EINPROGRESS;
2381 /* osd_lproc.c call this without env, allocate ksfs for that case */
2382 if (unlikely(env == NULL)) {
2383 OBD_ALLOC_PTR(ksfs);
2387 ksfs = &osd_oti_get(env)->oti_ksfs;
2390 result = sb->s_op->statfs(sb->s_root, ksfs);
2394 statfs_pack(sfs, ksfs);
2395 if (unlikely(sb->s_flags & SB_RDONLY))
2396 sfs->os_state |= OS_STATFS_READONLY;
2398 sfs->os_state |= osd->od_nonrotational ? OS_STATFS_NONROT : 0;
2400 if (ldiskfs_has_feature_extents(sb))
2401 sfs->os_maxbytes = sb->s_maxbytes;
2403 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2406 * Reserve some space so to avoid fragmenting the filesystem too much.
2407 * Fragmentation not only impacts performance, but can also increase
2408 * metadata overhead significantly, causing grant calculation to be
2411 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2413 BUILD_BUG_ON(OSD_STATFS_RESERVED <= LDISKFS_MAX_BLOCK_SIZE);
2414 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2415 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2416 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2418 sfs->os_blocks -= reserved;
2419 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2420 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2423 if (unlikely(env == NULL))
2429 * Estimate space needed for file creations. We assume the largest filename
2430 * which is 2^64 - 1, hence a filename of 20 chars.
2431 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2433 #ifdef __LDISKFS_DIR_REC_LEN
2434 # define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2435 #elif defined LDISKFS_DIR_REC_LEN_WITH_DIR
2436 # define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20, NULL)
2438 # define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2442 * Concurrency: doesn't access mutable data.
2444 static void osd_conf_get(const struct lu_env *env,
2445 const struct dt_device *dev,
2446 struct dt_device_param *param)
2448 struct osd_device *d = osd_dt_dev(dev);
2449 struct super_block *sb = osd_sb(d);
2450 struct blk_integrity *bi = bdev_get_integrity(sb->s_bdev);
2455 * XXX should be taken from not-yet-existing fs abstraction layer.
2457 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2458 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2459 param->ddp_symlink_max = sb->s_blocksize;
2460 param->ddp_mount_type = LDD_MT_LDISKFS;
2461 if (ldiskfs_has_feature_extents(sb))
2462 param->ddp_maxbytes = sb->s_maxbytes;
2464 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2466 * inode are statically allocated, so per-inode space consumption
2467 * is the space consumed by the directory entry
2469 param->ddp_inodespace = PER_OBJ_USAGE;
2471 * EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2472 * is 128MB) which is unlikely to be hit in real life. Report a smaller
2473 * maximum length to not under-count the actual number of extents
2474 * needed for writing a file if there are sub-optimal block allocations.
2476 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 1;
2477 /* worst-case extent insertion metadata overhead */
2478 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2479 param->ddp_mntopts = 0;
2480 if (test_opt(sb, XATTR_USER))
2481 param->ddp_mntopts |= MNTOPT_USERXATTR;
2482 if (test_opt(sb, POSIX_ACL))
2483 param->ddp_mntopts |= MNTOPT_ACL;
2486 * LOD might calculate the max stripe count based on max_ea_size,
2487 * so we need take account in the overhead as well,
2488 * xattr_header + magic + xattr_entry_head
2490 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2491 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2493 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2494 if (ldiskfs_has_feature_ea_inode(sb))
2495 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2499 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2501 if (param->ddp_max_ea_size > OBD_MAX_EA_SIZE)
2502 param->ddp_max_ea_size = OBD_MAX_EA_SIZE;
2505 * Preferred RPC size for efficient disk IO. 4MB shows good
2506 * all-around performance for ldiskfs, but use bigalloc chunk size
2507 * by default if larger.
2509 #if defined(LDISKFS_CLUSTER_SIZE)
2510 if (LDISKFS_CLUSTER_SIZE(sb) > DT_DEF_BRW_SIZE)
2511 param->ddp_brw_size = LDISKFS_CLUSTER_SIZE(sb);
2514 param->ddp_brw_size = DT_DEF_BRW_SIZE;
2516 param->ddp_t10_cksum_type = 0;
2518 unsigned short interval = blk_integrity_interval(bi);
2519 name = blk_integrity_name(bi);
2529 if (strncmp(name, "T10-DIF-TYPE",
2530 sizeof("T10-DIF-TYPE") - 1) == 0) {
2531 /* also skip "1/2/3-" at end */
2532 const int type_off = sizeof("T10-DIF-TYPE.");
2533 char type_number = name[type_off - 2];
2535 if (interval != 512 && interval != 4096) {
2536 CERROR("%s: unsupported T10PI sector size %u\n",
2537 d->od_svname, interval);
2540 switch (type_number) {
2542 d->od_t10_type = OSD_T10_TYPE1;
2545 d->od_t10_type = OSD_T10_TYPE2;
2548 d->od_t10_type = OSD_T10_TYPE3;
2551 CERROR("%s: unsupported T10PI type %s\n",
2552 d->od_svname, name);
2555 if (strcmp(name + type_off, "CRC") == 0) {
2556 d->od_t10_type |= OSD_T10_TYPE_CRC;
2557 param->ddp_t10_cksum_type = interval == 512 ?
2558 OBD_CKSUM_T10CRC512 :
2560 } else if (strcmp(name + type_off, "IP") == 0) {
2561 d->od_t10_type |= OSD_T10_TYPE_IP;
2562 param->ddp_t10_cksum_type = interval == 512 ?
2563 OBD_CKSUM_T10IP512 :
2566 CERROR("%s: unsupported checksum type of T10PI type '%s'\n",
2567 d->od_svname, name);
2572 CERROR("%s: unsupported T10PI type '%s'\n",
2573 d->od_svname, name);
2578 param->ddp_has_lseek_data_hole = true;
2581 static struct vfsmount *osd_mnt_get(const struct dt_device *d)
2583 return osd_dt_dev(d)->od_mnt;
2587 * Concurrency: shouldn't matter.
2589 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2592 struct super_block *s = osd_sb(osd_dt_dev(d));
2595 down_read(&s->s_umount);
2596 rc = s->s_op->sync_fs(s, 1);
2597 up_read(&s->s_umount);
2599 CDEBUG(D_CACHE, "%s: synced OSD: rc = %d\n", osd_dt_dev(d)->od_svname,
2606 * Start commit for OSD device.
2608 * An implementation of dt_commit_async method for OSD device.
2609 * Asychronously starts underlayng fs sync and thereby a transaction
2612 * \param env environment
2613 * \param d dt device
2615 * \see dt_device_operations
2617 static int osd_commit_async(const struct lu_env *env,
2618 struct dt_device *d)
2620 struct super_block *s = osd_sb(osd_dt_dev(d));
2625 CDEBUG(D_HA, "%s: async commit OSD\n", osd_dt_dev(d)->od_svname);
2626 down_read(&s->s_umount);
2627 rc = s->s_op->sync_fs(s, 0);
2628 up_read(&s->s_umount);
2634 * Concurrency: shouldn't matter.
2636 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2638 struct super_block *sb = osd_sb(osd_dt_dev(d));
2639 struct block_device *dev = sb->s_bdev;
2640 int rc = -EOPNOTSUPP;
2644 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2645 osd_dt_dev(d)->od_svname, (long)dev, rc);
2651 * Note: we do not count into QUOTA here.
2652 * If we mount with --data_journal we may need more.
2654 const int osd_dto_credits_noquota[DTO_NR] = {
2657 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2658 * SINGLEDATA_TRANS_BLOCKS(8)
2659 * XXX Note: maybe iam need more, since iam have more level than
2662 [DTO_INDEX_INSERT] = 16,
2665 * just modify a single entry, probably merge few within a block
2667 [DTO_INDEX_DELETE] = 1,
2671 [DTO_INDEX_UPDATE] = 16,
2673 * 4(inode, inode bits, groups, GDT)
2674 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2676 [DTO_OBJECT_CREATE] = 4,
2678 * 4(inode, inode bits, groups, GDT)
2679 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2681 [DTO_OBJECT_DELETE] = 4,
2683 * Attr set credits (inode)
2685 [DTO_ATTR_SET_BASE] = 1,
2687 * Xattr set. The same as xattr of EXT3.
2688 * DATA_TRANS_BLOCKS(14)
2689 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2690 * are also counted in. Do not know why?
2692 [DTO_XATTR_SET] = 14,
2694 * credits for inode change during write.
2696 [DTO_WRITE_BASE] = 3,
2698 * credits for single block write.
2700 [DTO_WRITE_BLOCK] = 14,
2702 * Attr set credits for chown.
2703 * This is extra credits for setattr, and it is null without quota
2705 [DTO_ATTR_SET_CHOWN] = 0
2708 /* reserve or free quota for some operation */
2709 static int osd_reserve_or_free_quota(const struct lu_env *env,
2710 struct dt_device *dev,
2711 struct lquota_id_info *qi)
2713 struct osd_device *osd = osd_dt_dev(dev);
2714 struct qsd_instance *qsd = NULL;
2720 qsd = osd->od_quota_slave_dt;
2722 qsd = osd->od_quota_slave_md;
2724 rc = qsd_reserve_or_free_quota(env, qsd, qi);
2728 static const struct dt_device_operations osd_dt_ops = {
2729 .dt_root_get = osd_root_get,
2730 .dt_statfs = osd_statfs,
2731 .dt_trans_create = osd_trans_create,
2732 .dt_trans_start = osd_trans_start,
2733 .dt_trans_stop = osd_trans_stop,
2734 .dt_trans_cb_add = osd_trans_cb_add,
2735 .dt_conf_get = osd_conf_get,
2736 .dt_mnt_get = osd_mnt_get,
2737 .dt_sync = osd_sync,
2739 .dt_commit_async = osd_commit_async,
2740 .dt_reserve_or_free_quota = osd_reserve_or_free_quota,
2743 static void osd_read_lock(const struct lu_env *env, struct dt_object *dt,
2746 struct osd_object *obj = osd_dt_obj(dt);
2747 struct osd_thread_info *oti = osd_oti_get(env);
2749 LINVRNT(osd_invariant(obj));
2751 LASSERT(obj->oo_owner != env);
2752 down_read_nested(&obj->oo_sem, role);
2754 LASSERT(obj->oo_owner == NULL);
2758 static void osd_write_lock(const struct lu_env *env, struct dt_object *dt,
2761 struct osd_object *obj = osd_dt_obj(dt);
2762 struct osd_thread_info *oti = osd_oti_get(env);
2764 LINVRNT(osd_invariant(obj));
2766 LASSERT(obj->oo_owner != env);
2767 down_write_nested(&obj->oo_sem, role);
2769 LASSERT(obj->oo_owner == NULL);
2770 obj->oo_owner = env;
2774 static void osd_read_unlock(const struct lu_env *env, struct dt_object *dt)
2776 struct osd_object *obj = osd_dt_obj(dt);
2777 struct osd_thread_info *oti = osd_oti_get(env);
2779 LINVRNT(osd_invariant(obj));
2781 LASSERT(oti->oti_r_locks > 0);
2783 up_read(&obj->oo_sem);
2786 static void osd_write_unlock(const struct lu_env *env, struct dt_object *dt)
2788 struct osd_object *obj = osd_dt_obj(dt);
2789 struct osd_thread_info *oti = osd_oti_get(env);
2791 LINVRNT(osd_invariant(obj));
2793 LASSERT(obj->oo_owner == env);
2794 LASSERT(oti->oti_w_locks > 0);
2796 obj->oo_owner = NULL;
2797 up_write(&obj->oo_sem);
2800 static int osd_write_locked(const struct lu_env *env, struct dt_object *dt)
2802 struct osd_object *obj = osd_dt_obj(dt);
2804 LINVRNT(osd_invariant(obj));
2806 return obj->oo_owner == env;
2809 static void osd_inode_getattr(const struct lu_env *env,
2810 struct inode *inode, struct lu_attr *attr)
2812 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2813 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2814 LA_PROJID | LA_FLAGS | LA_NLINK | LA_RDEV |
2815 LA_BLKSIZE | LA_TYPE | LA_BTIME;
2817 attr->la_atime = inode->i_atime.tv_sec;
2818 attr->la_mtime = inode->i_mtime.tv_sec;
2819 attr->la_ctime = inode->i_ctime.tv_sec;
2820 attr->la_btime = LDISKFS_I(inode)->i_crtime.tv_sec;
2821 attr->la_mode = inode->i_mode;
2822 attr->la_size = i_size_read(inode);
2823 attr->la_blocks = inode->i_blocks;
2824 attr->la_uid = i_uid_read(inode);
2825 attr->la_gid = i_gid_read(inode);
2826 attr->la_projid = i_projid_read(inode);
2827 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2828 attr->la_nlink = inode->i_nlink;
2829 attr->la_rdev = inode->i_rdev;
2830 attr->la_blksize = 1 << inode->i_blkbits;
2831 attr->la_blkbits = inode->i_blkbits;
2833 * Ext4 did not transfer inherit flags from raw inode
2834 * to inode flags, and ext4 internally test raw inode
2835 * @i_flags directly. Instead of patching ext4, we do it here.
2837 if (LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL)
2838 attr->la_flags |= LUSTRE_PROJINHERIT_FL;
2841 static int osd_dirent_count(const struct lu_env *env, struct dt_object *dt,
2844 struct osd_object *obj = osd_dt_obj(dt);
2845 const struct dt_it_ops *iops;
2851 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
2852 LASSERT(fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)));
2854 if (obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
2855 *count = obj->oo_dirent_count;
2859 /* directory not initialized yet */
2860 if (!dt->do_index_ops) {
2865 iops = &dt->do_index_ops->dio_it;
2866 it = iops->init(env, dt, LUDA_64BITHASH);
2868 RETURN(PTR_ERR(it));
2870 rc = iops->load(env, it, 0);
2872 if (rc == -ENODATA) {
2879 rc = iops->next(env, it);
2881 for (*count = 0; rc == 0 || rc == -ESTALE; rc = iops->next(env, it)) {
2885 if (iops->key_size(env, it) == 0)
2891 obj->oo_dirent_count = *count;
2896 iops->fini(env, it);
2901 static int osd_attr_get(const struct lu_env *env, struct dt_object *dt,
2902 struct lu_attr *attr)
2904 struct osd_object *obj = osd_dt_obj(dt);
2907 if (unlikely(!dt_object_exists(dt)))
2909 if (unlikely(obj->oo_destroyed))
2912 LASSERT(!dt_object_remote(dt));
2913 LINVRNT(osd_invariant(obj));
2915 spin_lock(&obj->oo_guard);
2916 osd_inode_getattr(env, obj->oo_inode, attr);
2917 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL) {
2918 attr->la_valid |= LA_FLAGS;
2919 attr->la_flags |= LUSTRE_ORPHAN_FL;
2921 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL) {
2922 attr->la_valid |= LA_FLAGS;
2923 attr->la_flags |= LUSTRE_ENCRYPT_FL;
2925 spin_unlock(&obj->oo_guard);
2927 if (S_ISDIR(obj->oo_inode->i_mode) &&
2928 fid_is_namespace_visible(lu_object_fid(&dt->do_lu)))
2929 rc = osd_dirent_count(env, dt, &attr->la_dirent_count);
2934 static int osd_declare_attr_qid(const struct lu_env *env,
2935 struct osd_object *obj,
2936 struct osd_thandle *oh, long long bspace,
2937 qid_t old_id, qid_t new_id, bool enforce,
2941 struct osd_thread_info *info = osd_oti_get(env);
2942 struct lquota_id_info *qi = &info->oti_qi;
2944 qi->lqi_type = type;
2945 /* inode accounting */
2946 qi->lqi_is_blk = false;
2948 /* one more inode for the new id ... */
2949 qi->lqi_id.qid_uid = new_id;
2951 /* Reserve credits for the new id */
2952 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2953 if (rc == -EDQUOT || rc == -EINPROGRESS)
2958 /* and one less inode for the current id */
2959 qi->lqi_id.qid_uid = old_id;
2961 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2962 if (rc == -EDQUOT || rc == -EINPROGRESS)
2967 /* block accounting */
2968 qi->lqi_is_blk = true;
2970 /* more blocks for the new id ... */
2971 qi->lqi_id.qid_uid = new_id;
2972 qi->lqi_space = bspace;
2974 * Credits for the new uid has been reserved, re-use "obj"
2975 * to save credit reservation.
2977 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2978 if (rc == -EDQUOT || rc == -EINPROGRESS)
2983 /* and finally less blocks for the current uid */
2984 qi->lqi_id.qid_uid = old_id;
2985 qi->lqi_space = -bspace;
2986 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2987 if (rc == -EDQUOT || rc == -EINPROGRESS)
2993 static int osd_declare_attr_set(const struct lu_env *env,
2994 struct dt_object *dt,
2995 const struct lu_attr *attr,
2996 struct thandle *handle)
2998 struct osd_thandle *oh;
2999 struct osd_object *obj;
3008 LASSERT(dt != NULL);
3009 LASSERT(handle != NULL);
3011 obj = osd_dt_obj(dt);
3012 LASSERT(osd_invariant(obj));
3014 oh = container_of(handle, struct osd_thandle, ot_super);
3015 LASSERT(oh->ot_handle == NULL);
3017 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
3018 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3020 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3021 osd_dto_credits_noquota[DTO_XATTR_SET]);
3023 if (attr == NULL || obj->oo_inode == NULL)
3026 bspace = obj->oo_inode->i_blocks << 9;
3027 bspace = toqb(bspace);
3030 * Changing ownership is always preformed by super user, it should not
3031 * fail with EDQUOT unless required explicitly.
3033 * We still need to call the osd_declare_qid() to calculate the journal
3034 * credits for updating quota accounting files and to trigger quota
3035 * space adjustment once the operation is completed.
3037 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
3039 uid = i_uid_read(obj->oo_inode);
3040 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
3041 rc = osd_declare_attr_qid(env, obj, oh, bspace, uid,
3042 attr->la_uid, enforce, USRQUOTA);
3046 gid = i_gid_read(obj->oo_inode);
3047 CDEBUG(D_QUOTA, "declare uid %d -> %d gid %d -> %d\n", uid,
3048 attr->la_uid, gid, attr->la_gid);
3049 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
3050 rc = osd_declare_attr_qid(env, obj, oh, bspace, gid,
3051 attr->la_gid, enforce, GRPQUOTA);
3056 #ifdef HAVE_PROJECT_QUOTA
3057 if (attr->la_valid & LA_PROJID) {
3058 __u32 projid = i_projid_read(obj->oo_inode);
3060 enforce = (attr->la_valid & LA_PROJID) &&
3061 (attr->la_projid != projid);
3062 rc = osd_declare_attr_qid(env, obj, oh, bspace,
3063 (qid_t)projid, (qid_t)attr->la_projid,
3069 /* punch must be aware we are dealing with an encrypted file */
3070 if (attr->la_valid & LA_FLAGS && attr->la_flags & LUSTRE_ENCRYPT_FL)
3071 obj->oo_lma_flags |= LUSTRE_ENCRYPT_FL;
3076 static int osd_inode_setattr(const struct lu_env *env,
3077 struct inode *inode, const struct lu_attr *attr)
3079 __u64 bits = attr->la_valid;
3081 /* Only allow set size for regular file */
3082 if (!S_ISREG(inode->i_mode))
3083 bits &= ~(LA_SIZE | LA_BLOCKS);
3088 if (bits & LA_ATIME)
3089 inode->i_atime = osd_inode_time(inode, attr->la_atime);
3090 if (bits & LA_CTIME)
3091 inode->i_ctime = osd_inode_time(inode, attr->la_ctime);
3092 if (bits & LA_MTIME)
3093 inode->i_mtime = osd_inode_time(inode, attr->la_mtime);
3094 if (bits & LA_SIZE) {
3095 spin_lock(&inode->i_lock);
3096 LDISKFS_I(inode)->i_disksize = attr->la_size;
3097 i_size_write(inode, attr->la_size);
3098 spin_unlock(&inode->i_lock);
3102 * OSD should not change "i_blocks" which is used by quota.
3103 * "i_blocks" should be changed by ldiskfs only.
3106 inode->i_mode = (inode->i_mode & S_IFMT) |
3107 (attr->la_mode & ~S_IFMT);
3109 i_uid_write(inode, attr->la_uid);
3111 i_gid_write(inode, attr->la_gid);
3112 if (bits & LA_PROJID)
3113 i_projid_write(inode, attr->la_projid);
3114 if (bits & LA_NLINK)
3115 set_nlink(inode, attr->la_nlink);
3117 inode->i_rdev = attr->la_rdev;
3119 if (bits & LA_FLAGS) {
3120 /* always keep S_NOCMTIME */
3121 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
3123 #if defined(S_ENCRYPTED)
3124 /* Always remove S_ENCRYPTED, because ldiskfs must not be
3125 * aware of encryption status. It is just stored into LMA
3126 * so that it can be forwared to client side.
3128 inode->i_flags &= ~S_ENCRYPTED;
3131 * Ext4 did not transfer inherit flags from
3132 * @inode->i_flags to raw inode i_flags when writing
3133 * flags, we do it explictly here.
3135 if (attr->la_flags & LUSTRE_PROJINHERIT_FL)
3136 LDISKFS_I(inode)->i_flags |= LUSTRE_PROJINHERIT_FL;
3138 LDISKFS_I(inode)->i_flags &= ~LUSTRE_PROJINHERIT_FL;
3143 #ifdef HAVE_PROJECT_QUOTA
3144 static int osd_transfer_project(struct inode *inode, __u32 projid,
3145 struct thandle *handle)
3147 struct super_block *sb = inode->i_sb;
3148 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
3151 struct ldiskfs_iloc iloc;
3152 struct ldiskfs_inode *raw_inode;
3153 struct dquot *transfer_to[LDISKFS_MAXQUOTAS] = { };
3155 if (!ldiskfs_has_feature_project(sb)) {
3156 LASSERT(__kprojid_val(LDISKFS_I(inode)->i_projid)
3157 == LDISKFS_DEF_PROJID);
3158 if (projid != LDISKFS_DEF_PROJID)
3164 if (LDISKFS_INODE_SIZE(sb) <= LDISKFS_GOOD_OLD_INODE_SIZE)
3167 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3168 if (projid_eq(kprojid, LDISKFS_I(inode)->i_projid))
3171 err = ldiskfs_get_inode_loc(inode, &iloc);
3175 raw_inode = ldiskfs_raw_inode(&iloc);
3176 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3177 struct osd_thandle *oh = container_of(handle,
3181 * try to expand inode size automatically.
3183 ldiskfs_mark_inode_dirty(oh->ot_handle, inode);
3184 if (!LDISKFS_FITS_IN_INODE(raw_inode, ei, i_projid)) {
3192 dquot_initialize(inode);
3193 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3194 if (transfer_to[PRJQUOTA]) {
3195 lock_dquot_transfer(inode);
3196 err = __dquot_transfer(inode, transfer_to);
3197 unlock_dquot_transfer(inode);
3198 dqput(transfer_to[PRJQUOTA]);
3207 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr,
3208 struct thandle *handle)
3212 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
3213 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
3217 "executing dquot_transfer inode %ld uid %d -> %d gid %d -> %d\n",
3218 inode->i_ino, i_uid_read(inode), attr->la_uid,
3219 i_gid_read(inode), attr->la_gid);
3221 dquot_initialize(inode);
3223 if (attr->la_valid & LA_UID)
3224 iattr.ia_valid |= ATTR_UID;
3225 if (attr->la_valid & LA_GID)
3226 iattr.ia_valid |= ATTR_GID;
3227 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
3228 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
3230 lock_dquot_transfer(inode);
3231 rc = osd_dquot_transfer(&nop_mnt_idmap, inode, &iattr);
3232 unlock_dquot_transfer(inode);
3234 CERROR("%s: quota transfer failed. Is quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3235 osd_ino2name(inode), rc);
3240 /* Handle project id transfer here properly */
3241 if (attr->la_valid & LA_PROJID &&
3242 attr->la_projid != i_projid_read(inode)) {
3243 if (!projid_valid(make_kprojid(&init_user_ns, attr->la_projid)))
3245 #ifdef HAVE_PROJECT_QUOTA
3246 rc = osd_transfer_project(inode, attr->la_projid, handle);
3251 CERROR("%s: quota transfer failed. Is project enforcement enabled on the ldiskfs filesystem? rc = %d\n",
3252 osd_ino2name(inode), rc);
3259 static int osd_attr_set(const struct lu_env *env,
3260 struct dt_object *dt,
3261 const struct lu_attr *attr,
3262 struct thandle *handle)
3264 struct osd_thread_info *info = osd_oti_get(env);
3265 struct osd_object *obj = osd_dt_obj(dt);
3266 struct osd_device *osd = osd_obj2dev(obj);
3267 struct inode *inode;
3270 if (!dt_object_exists(dt))
3273 LASSERT(handle != NULL);
3274 LASSERT(!dt_object_remote(dt));
3275 LASSERT(osd_invariant(obj));
3277 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
3279 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING) &&
3280 !osd_obj2dev(obj)->od_is_ost) {
3281 struct osd_thread_info *oti = osd_oti_get(env);
3282 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
3283 struct lu_fid *fid1 = &oti->oti_fid;
3284 struct osd_inode_id *id = &oti->oti_id;
3285 struct iam_path_descr *ipd;
3286 struct iam_container *bag;
3287 struct osd_thandle *oh;
3290 fid_cpu_to_be(fid1, fid0);
3291 memset(id, 1, sizeof(*id));
3292 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
3293 fid0)->oi_dir.od_container;
3294 ipd = osd_idx_ipd_get(env, bag);
3295 if (unlikely(ipd == NULL))
3298 oh = container_of(handle, struct osd_thandle, ot_super);
3299 rc = iam_update(oh->ot_handle, bag,
3300 (const struct iam_key *)fid1,
3301 (const struct iam_rec *)id, ipd);
3302 osd_ipd_put(env, bag, ipd);
3303 return(rc > 0 ? 0 : rc);
3306 inode = obj->oo_inode;
3308 rc = osd_quota_transfer(inode, attr, handle);
3312 spin_lock(&obj->oo_guard);
3313 rc = osd_inode_setattr(env, inode, attr);
3314 spin_unlock(&obj->oo_guard);
3318 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3320 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
3322 if (!(attr->la_valid & LA_FLAGS))
3325 /* If setting LUSTRE_ENCRYPT_FL on an OST object, also set a dummy
3326 * enc ctx xattr, with 2 benefits:
3327 * - setting the LL_XATTR_NAME_ENCRYPTION_CONTEXT xattr internally sets
3328 * the LDISKFS_ENCRYPT_FL flag on the on-disk inode;
3329 * - it makes e2fsprogs happy to see an enc ctx for an inode that has
3330 * the LDISKFS_ENCRYPT_FL flag
3331 * We do not need the actual encryption context on OST objects, it is
3332 * only stored on MDT inodes, at file creation time.
3334 if (!(LDISKFS_I(obj->oo_inode)->i_flags & LDISKFS_ENCRYPT_FL) &&
3335 attr->la_flags & LUSTRE_ENCRYPT_FL && osd->od_is_ost &&
3336 !CFS_FAIL_CHECK(OBD_FAIL_LFSCK_NO_ENCFLAG)) {
3339 /* use a dummy enc ctx, fine with e2fsprogs */
3340 buf.lb_buf = "\xFF";
3342 rc = osd_xattr_set(env, dt, &buf,
3343 LL_XATTR_NAME_ENCRYPTION_CONTEXT,
3346 CWARN("%s: set "DFID" enc ctx failed: rc = %d\n",
3347 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
3351 /* Let's check if there are extra flags need to be set into LMA */
3352 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
3353 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
3355 LASSERT(!obj->oo_pfid_in_lma);
3357 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
3358 &info->oti_ost_attrs);
3362 if ((lma->lma_incompat & lustre_to_lma_flags(attr->la_flags)) ==
3363 lustre_to_lma_flags(attr->la_flags))
3364 /* if lma incompat already has the flags,
3365 * save a useless call to xattr_set
3369 lma->lma_incompat |=
3370 lustre_to_lma_flags(attr->la_flags);
3371 lustre_lma_swab(lma);
3373 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3375 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
3376 lma, sizeof(*lma), XATTR_REPLACE);
3378 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
3379 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
3380 lma->lma_incompat, rc);
3383 attr->la_flags & LUSTRE_LMA_FL_MASKS;
3384 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3391 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
3392 struct osd_object *obj,
3393 const char *name, const int namelen)
3395 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
3398 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
3399 umode_t mode, struct dt_allocation_hint *hint,
3400 struct thandle *th, struct lu_attr *attr)
3403 struct osd_device *osd = osd_obj2dev(obj);
3404 struct osd_thandle *oth;
3405 struct dt_object *parent = NULL;
3406 struct inode *inode;
3407 struct iattr iattr = {
3408 .ia_valid = ATTR_UID | ATTR_GID |
3409 ATTR_CTIME | ATTR_MTIME | ATTR_ATIME,
3410 .ia_ctime.tv_sec = attr->la_ctime,
3411 .ia_mtime.tv_sec = attr->la_mtime,
3412 .ia_atime.tv_sec = attr->la_atime,
3413 .ia_uid = GLOBAL_ROOT_UID,
3414 .ia_gid = GLOBAL_ROOT_GID,
3416 const struct osd_timespec omit = { .tv_nsec = UTIME_OMIT };
3418 if (attr->la_valid & LA_UID)
3419 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
3420 if (attr->la_valid & LA_GID)
3421 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
3423 LINVRNT(osd_invariant(obj));
3424 LASSERT(obj->oo_inode == NULL);
3425 LASSERT(obj->oo_hl_head == NULL);
3427 if (S_ISDIR(mode) && ldiskfs_pdo) {
3429 ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
3430 if (obj->oo_hl_head == NULL)
3434 oth = container_of(th, struct osd_thandle, ot_super);
3435 LASSERT(oth->ot_handle->h_transaction != NULL);
3437 if (hint != NULL && hint->dah_parent != NULL &&
3438 !dt_object_remote(hint->dah_parent))
3439 parent = hint->dah_parent;
3441 /* if a time component is not valid set it to UTIME_OMIT */
3442 if (!(attr->la_valid & LA_CTIME))
3443 iattr.ia_ctime = omit;
3444 if (!(attr->la_valid & LA_MTIME))
3445 iattr.ia_mtime = omit;
3446 if (!(attr->la_valid & LA_ATIME))
3447 iattr.ia_atime = omit;
3449 inode = ldiskfs_create_inode(oth->ot_handle,
3450 parent ? osd_dt_obj(parent)->oo_inode :
3451 osd_sb(osd)->s_root->d_inode,
3453 if (!IS_ERR(inode)) {
3454 /* Do not update file c/mtime in ldiskfs. */
3455 inode->i_flags |= S_NOCMTIME;
3458 * For new created object, it must be consistent,
3459 * and it is unnecessary to scrub against it.
3461 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
3463 obj->oo_inode = inode;
3466 if (obj->oo_hl_head != NULL) {
3467 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
3468 obj->oo_hl_head = NULL;
3470 result = PTR_ERR(inode);
3472 LINVRNT(osd_invariant(obj));
3480 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
3481 struct lu_attr *attr,
3482 struct dt_allocation_hint *hint,
3483 struct dt_object_format *dof,
3487 struct osd_thandle *oth;
3488 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
3490 LASSERT(S_ISDIR(attr->la_mode));
3492 oth = container_of(th, struct osd_thandle, ot_super);
3493 LASSERT(oth->ot_handle->h_transaction != NULL);
3494 if (fid_is_namespace_visible(lu_object_fid(&obj->oo_dt.do_lu)))
3495 obj->oo_dirent_count = 0;
3496 result = osd_mkfile(info, obj, mode, hint, th, attr);
3501 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
3502 struct lu_attr *attr,
3503 struct dt_allocation_hint *hint,
3504 struct dt_object_format *dof,
3508 struct osd_thandle *oth;
3509 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
3511 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
3513 LASSERT(S_ISREG(attr->la_mode));
3515 oth = container_of(th, struct osd_thandle, ot_super);
3516 LASSERT(oth->ot_handle->h_transaction != NULL);
3518 result = osd_mkfile(info, obj, mode, hint, th, attr);
3520 LASSERT(obj->oo_inode != NULL);
3521 if (feat->dif_flags & DT_IND_VARKEY)
3522 result = iam_lvar_create(obj->oo_inode,
3523 feat->dif_keysize_max,
3525 feat->dif_recsize_max,
3528 result = iam_lfix_create(obj->oo_inode,
3529 feat->dif_keysize_max,
3531 feat->dif_recsize_max,
3537 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
3538 struct lu_attr *attr,
3539 struct dt_allocation_hint *hint,
3540 struct dt_object_format *dof,
3543 LASSERT(S_ISREG(attr->la_mode));
3544 return osd_mkfile(info, obj, (attr->la_mode &
3545 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
3549 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
3550 struct lu_attr *attr,
3551 struct dt_allocation_hint *hint,
3552 struct dt_object_format *dof,
3555 LASSERT(S_ISLNK(attr->la_mode));
3556 return osd_mkfile(info, obj, (attr->la_mode &
3557 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th,
3561 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
3562 struct lu_attr *attr,
3563 struct dt_allocation_hint *hint,
3564 struct dt_object_format *dof,
3567 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
3570 LINVRNT(osd_invariant(obj));
3571 LASSERT(obj->oo_inode == NULL);
3572 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
3573 S_ISFIFO(mode) || S_ISSOCK(mode));
3575 result = osd_mkfile(info, obj, mode, hint, th, attr);
3577 LASSERT(obj->oo_inode != NULL);
3579 * This inode should be marked dirty for i_rdev. Currently
3580 * that is done in the osd_attr_init().
3582 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
3585 LINVRNT(osd_invariant(obj));
3589 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
3591 struct dt_allocation_hint *hint,
3592 struct dt_object_format *dof,
3595 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
3597 osd_obj_type_f result;
3613 result = osd_mk_index;
3623 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
3624 struct dt_object *parent, struct dt_object *child,
3629 ah->dah_parent = parent;
3631 if (parent != NULL && !dt_object_remote(parent)) {
3632 /* will help to find FID->ino at dt_insert("..") */
3633 struct osd_object *pobj = osd_dt_obj(parent);
3635 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
3639 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
3640 struct lu_attr *attr, struct dt_object_format *dof,
3641 struct thandle *handle)
3643 struct inode *inode = obj->oo_inode;
3644 __u64 valid = attr->la_valid;
3647 attr->la_valid &= ~(LA_TYPE | LA_MODE);
3649 if (dof->dof_type != DFT_NODE)
3650 attr->la_valid &= ~LA_RDEV;
3651 if ((valid & LA_ATIME) && (attr->la_atime == inode->i_atime.tv_sec))
3652 attr->la_valid &= ~LA_ATIME;
3653 if ((valid & LA_CTIME) && (attr->la_ctime == inode->i_ctime.tv_sec))
3654 attr->la_valid &= ~LA_CTIME;
3655 if ((valid & LA_MTIME) && (attr->la_mtime == inode->i_mtime.tv_sec))
3656 attr->la_valid &= ~LA_MTIME;
3658 result = osd_quota_transfer(inode, attr, handle);
3662 if (attr->la_valid != 0) {
3663 result = osd_inode_setattr(info->oti_env, inode, attr);
3665 * The osd_inode_setattr() should always succeed here. The
3666 * only error that could be returned is EDQUOT when we are
3667 * trying to change the UID or GID of the inode. However, this
3668 * should not happen since quota enforcement is no longer
3669 * enabled on ldiskfs (lquota takes care of it).
3671 LASSERTF(result == 0, "%d\n", result);
3672 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3675 attr->la_valid = valid;
3679 * Helper function for osd_create()
3681 * \retval 0, on success
3683 static int __osd_create(struct osd_thread_info *info, struct osd_object *obj,
3684 struct lu_attr *attr, struct dt_allocation_hint *hint,
3685 struct dt_object_format *dof, struct thandle *th)
3690 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
3692 /* we drop umask so that permissions we pass are not affected */
3693 umask = current->fs->umask;
3694 current->fs->umask = 0;
3696 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
3698 if (likely(obj->oo_inode != NULL)) {
3699 LASSERT(obj->oo_inode->i_state & I_NEW);
3702 * Unlock the inode before attr initialization to avoid
3703 * unnecessary dqget operations. LU-6378
3705 unlock_new_inode(obj->oo_inode);
3708 if (likely(result == 0)) {
3709 osd_attr_init(info, obj, attr, dof, th);
3710 osd_object_init0(obj);
3713 /* restore previous umask value */
3714 current->fs->umask = umask;
3716 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
3722 * Helper function for osd_create()
3724 * \retval 0, on success
3726 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
3727 const struct lu_fid *fid, struct thandle *th)
3729 struct osd_thread_info *info = osd_oti_get(env);
3730 struct osd_inode_id *id = &info->oti_id;
3731 struct osd_device *osd = osd_obj2dev(obj);
3732 struct osd_thandle *oh;
3735 LASSERT(obj->oo_inode != NULL);
3737 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_OI_ENOSPC))
3740 oh = container_of(th, struct osd_thandle, ot_super);
3741 LASSERT(oh->ot_handle);
3742 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3744 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3745 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_FID_REUSE) && osd->od_is_ost &&
3747 struct lu_fid tfid = *fid;
3750 osd_oi_insert(info, osd, &tfid, id, oh->ot_handle,
3751 OI_CHECK_FLD, NULL);
3752 /* clear NOSCRUB flag so that it can be scrubbed immediately */
3753 ldiskfs_clear_inode_state(obj->oo_inode,
3754 LDISKFS_STATE_LUSTRE_NOSCRUB);
3757 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3758 OI_CHECK_FLD, NULL);
3760 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP) && osd->od_is_ost) {
3761 struct lu_fid next_fid = *fid;
3763 /* insert next object in advance, and map to the same inode */
3765 if (next_fid.f_oid != 0) {
3766 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3767 osd_oi_insert(info, osd, &next_fid, id, oh->ot_handle,
3768 OI_CHECK_FLD, NULL);
3769 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3773 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3778 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3779 u64 seq, struct lu_seq_range *range)
3781 struct seq_server_site *ss = osd_seq_site(osd);
3783 if (fid_seq_is_idif(seq)) {
3784 fld_range_set_ost(range);
3785 range->lsr_index = idif_ost_idx(seq);
3789 if (!fid_seq_in_fldb(seq)) {
3790 fld_range_set_mdt(range);
3793 * FIXME: If ss is NULL, it suppose not get lsr_index
3796 range->lsr_index = ss->ss_node_id;
3800 LASSERT(ss != NULL);
3801 fld_range_set_any(range);
3802 /* OSD will only do local fld lookup */
3803 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3806 static int osd_declare_create(const struct lu_env *env, struct dt_object *dt,
3807 struct lu_attr *attr,
3808 struct dt_allocation_hint *hint,
3809 struct dt_object_format *dof,
3810 struct thandle *handle)
3812 struct osd_thandle *oh;
3813 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3819 LASSERT(handle != NULL);
3821 oh = container_of(handle, struct osd_thandle, ot_super);
3822 LASSERT(oh->ot_handle == NULL);
3824 if (osd_tx_was_declared(env, oh, dt, DTO_OBJECT_CREATE, 0))
3828 * EA object consumes more credits than regular object: osd_mk_index
3829 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3830 * leaf_node, could involves the block, block bitmap, groups, GDT
3831 * change for each block, so add 4 * 2 credits in that case.
3833 * The default ACL initialization may consume an additional 16 blocks
3835 credits = osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3836 ((dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3839 * While ldiskfs_new_inode() calls ldiskfs_init_acl() we have to add
3840 * credits for possible default ACL creation in new inode
3842 if (hint && hint->dah_acl_len)
3843 credits += osd_calc_bkmap_credits(sb, NULL, 0, -1,
3844 (hint->dah_acl_len + sb->s_blocksize - 1) >>
3845 sb->s_blocksize_bits);
3847 osd_trans_declare_op(env, oh, OSD_OT_CREATE, credits);
3850 * Reuse idle OI block may cause additional one OI block
3853 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3854 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3855 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DUPLICATE_MAP))
3856 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3857 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3859 /* will help to find FID->ino mapping at dt_insert() */
3860 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3868 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid,
3869 attr->la_projid, 1, oh, osd_dt_obj(dt),
3870 NULL, OSD_QID_INODE);
3878 * Called to destroy on-disk representation of the object
3880 * Concurrency: must be locked
3882 static int osd_declare_destroy(const struct lu_env *env, struct dt_object *dt,
3885 struct osd_object *obj = osd_dt_obj(dt);
3886 struct inode *inode = obj->oo_inode;
3887 struct osd_thandle *oh;
3895 oh = container_of(th, struct osd_thandle, ot_super);
3896 LASSERT(oh->ot_handle == NULL);
3898 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3899 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3901 /* For removing agent entry */
3902 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu))
3903 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3906 * Recycle idle OI leaf may cause additional three OI blocks
3909 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3910 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3911 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3912 /* one less inode */
3913 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3914 i_projid_read(inode), -1, oh, obj, NULL,
3918 /* data to be truncated */
3919 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3920 i_projid_read(inode), 0, oh, obj, NULL,
3926 * will help to find FID->ino when this object is being
3929 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3934 static int osd_destroy(const struct lu_env *env, struct dt_object *dt,
3937 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3938 struct osd_object *obj = osd_dt_obj(dt);
3939 struct inode *inode = obj->oo_inode;
3940 struct osd_device *osd = osd_obj2dev(obj);
3941 struct osd_thandle *oh;
3946 oh = container_of(th, struct osd_thandle, ot_super);
3947 LASSERT(oh->ot_handle);
3949 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3951 if (unlikely(fid_is_acct(fid)))
3954 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu)) {
3955 result = osd_delete_from_remote_parent(env, osd, obj, oh, true);
3957 CERROR("%s: remove agent entry "DFID": rc = %d\n",
3958 osd_name(osd), PFID(fid), result);
3961 if (S_ISDIR(inode->i_mode)) {
3962 if (inode->i_nlink > 2)
3963 CERROR("%s: directory "DFID" ino %lu link count is %u at unlink. run e2fsck to repair\n",
3964 osd_name(osd), PFID(fid), inode->i_ino,
3967 spin_lock(&obj->oo_guard);
3969 spin_unlock(&obj->oo_guard);
3970 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
3973 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3975 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3977 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3978 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3979 oh->ot_handle, OI_CHECK_FLD);
3981 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3982 /* XXX: add to ext3 orphan list */
3983 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3985 /* not needed in the cache anymore */
3986 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3987 obj->oo_destroyed = 1;
3993 * Put the fid into lustre_mdt_attrs, and then place the structure
3994 * inode's ea. This fid should not be altered during the life time
3997 * \retval +ve, on success
3998 * \retval -ve, on error
4000 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
4002 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
4003 const struct lu_fid *fid, __u32 compat, __u32 incompat)
4005 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4006 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4011 if (CFS_FAIL_CHECK(OBD_FAIL_FID_INLMA))
4014 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
4017 lustre_loa_init(loa, fid, compat, incompat);
4018 lustre_loa_swab(loa, false);
4021 * For the OST device with 256 bytes inode size by default,
4022 * the PFID EA will be stored together with LMA EA to avoid
4023 * performance trouble. Otherwise the PFID EA can be stored
4024 * independently. LU-8998
4026 if ((compat & LMAC_FID_ON_OST) &&
4027 LDISKFS_INODE_SIZE(inode->i_sb) <= 256)
4028 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
4029 sizeof(*loa), XATTR_CREATE);
4031 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4032 sizeof(*lma), XATTR_CREATE);
4034 * LMA may already exist, but we need to check that all the
4035 * desired compat/incompat flags have been added.
4037 if (unlikely(rc == -EEXIST)) {
4038 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
4039 XATTR_NAME_LMA, (void *)loa, sizeof(*loa));
4043 if (rc < sizeof(*lma))
4046 lustre_loa_swab(loa, true);
4047 if (lu_fid_eq(fid, &lma->lma_self_fid) &&
4048 ((compat == 0 && incompat == 0) ||
4049 (!(~lma->lma_compat & compat) &&
4050 !(~lma->lma_incompat & incompat))))
4053 lma->lma_self_fid = *fid;
4054 lma->lma_compat |= compat;
4055 lma->lma_incompat |= incompat;
4056 if (rc == sizeof(*lma)) {
4057 lustre_lma_swab(lma);
4058 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4059 sizeof(*lma), XATTR_REPLACE);
4061 lustre_loa_swab(loa, false);
4062 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa,
4063 sizeof(*loa), XATTR_REPLACE);
4071 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
4072 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
4073 * To have compatilibility with 1.8 ldiskfs driver we need to have
4074 * magic number at start of fid data.
4075 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
4078 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
4079 const struct lu_fid *fid)
4081 if (!fid_is_namespace_visible(fid) ||
4082 CFS_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
4083 param->edp_magic = 0;
4087 param->edp_magic = LDISKFS_LUFID_MAGIC;
4088 param->edp_len = sizeof(struct lu_fid) + 1;
4089 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
4093 * Try to read the fid from inode ea into dt_rec.
4095 * \param fid object fid.
4097 * \retval 0 on success
4099 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
4100 __u32 ino, struct lu_fid *fid,
4101 struct osd_inode_id *id)
4103 struct osd_thread_info *info = osd_oti_get(env);
4104 struct inode *inode;
4108 osd_id_gen(id, ino, OSD_OII_NOGEN);
4109 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
4111 RETURN(PTR_ERR(inode));
4117 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
4119 struct inode *parent_dir,
4120 const struct lu_fid *dot_fid,
4121 const struct lu_fid *dot_dot_fid,
4122 struct osd_thandle *oth)
4124 struct ldiskfs_dentry_param *dot_ldp;
4125 struct ldiskfs_dentry_param *dot_dot_ldp;
4126 __u32 saved_nlink = dir->i_nlink;
4129 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_DOTDOT_ENOSPC))
4132 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
4133 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
4135 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4136 dot_ldp->edp_magic = 0;
4138 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
4139 dir, dot_ldp, dot_dot_ldp);
4141 * The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
4142 * the subseqent ref_add() will increase the dir->i_nlink
4143 * as 3. That is incorrect for new created directory.
4145 * It looks like hack, because we want to make the OSD API
4146 * to be order-independent for new created directory object
4147 * between dt_insert(..) and ref_add() operations.
4149 * Here, we only restore the in-RAM dir-inode's nlink attr,
4150 * becuase if the nlink attr is not 2, then there will be
4151 * ref_add() called following the dt_insert(..), such call
4152 * will make both the in-RAM and on-disk dir-inode's nlink
4153 * attr to be set as 2. LU-7447
4155 set_nlink(dir, saved_nlink);
4160 * Create an local agent inode for remote entry
4162 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
4163 struct osd_device *osd,
4164 struct osd_object *pobj,
4165 const struct lu_fid *fid,
4169 struct osd_thread_info *info = osd_oti_get(env);
4170 struct inode *local;
4171 struct osd_thandle *oh;
4172 struct iattr iattr = {
4173 .ia_valid = ATTR_UID | ATTR_GID |
4174 ATTR_CTIME | ATTR_MTIME | ATTR_ATIME,
4175 .ia_ctime.tv_nsec = UTIME_OMIT,
4176 .ia_mtime.tv_nsec = UTIME_OMIT,
4177 .ia_atime.tv_nsec = UTIME_OMIT,
4178 .ia_uid = GLOBAL_ROOT_UID,
4179 .ia_gid = GLOBAL_ROOT_GID,
4186 oh = container_of(th, struct osd_thandle, ot_super);
4187 LASSERT(oh->ot_handle->h_transaction != NULL);
4189 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode,
4191 if (IS_ERR(local)) {
4192 CERROR("%s: create local error %d\n", osd_name(osd),
4193 (int)PTR_ERR(local));
4198 * restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
4199 * correct gid on remote file, not agent here
4201 local->i_gid = current_fsgid();
4202 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
4204 /* e2fsck doesn't like empty symlinks. Store remote FID as symlink.
4205 * That gives e2fsck something to look at and be happy, and allows
4206 * debugging if we need to determine where this symlink came from.
4208 if (S_ISLNK(type)) {
4209 BUILD_BUG_ON(LDISKFS_N_BLOCKS * 4 < FID_LEN + 1);
4210 ldiskfs_clear_inode_flag(local, LDISKFS_INODE_EXTENTS);
4211 rc = scnprintf((char *)LDISKFS_I(local)->i_data,
4212 LDISKFS_N_BLOCKS * 4, DFID, PFID(fid));
4214 i_size_write(local, rc);
4215 LDISKFS_I(local)->i_disksize = rc;
4217 unlock_new_inode(local);
4219 /* Agent inode should not have project ID */
4220 #ifdef HAVE_PROJECT_QUOTA
4221 if (LDISKFS_I(pobj->oo_inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
4222 i_projid_read(pobj->oo_inode) != 0) {
4223 rc = osd_transfer_project(local, 0, th);
4225 CERROR("%s: quota transfer failed:. Is project quota enforcement enabled on the ldiskfs filesystem? rc = %d\n",
4226 osd_ino2name(local), rc);
4227 RETURN(ERR_PTR(rc));
4231 /* Set special LMA flag for local agent inode */
4232 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
4234 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
4235 osd_name(osd), PFID(fid), rc);
4236 RETURN(ERR_PTR(rc));
4242 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode, fid,
4243 lu_object_fid(&pobj->oo_dt.do_lu),
4246 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
4247 osd_name(osd), PFID(fid), rc);
4248 RETURN(ERR_PTR(rc));
4255 * when direntry is deleted, we have to take care of possible agent inode
4256 * referenced by that. unfortunately we can't do this at that point:
4257 * iget() within a running transaction leads to deadlock and we better do
4258 * not call that every delete declaration to save performance. so we put
4259 * a potention agent inode on a list and process that once the transaction
4260 * is over. Notice it's not any worse in terms of real orphans as regular
4261 * object destroy doesn't put inodes on the on-disk orphan list. this should
4262 * be addressed separately
4264 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
4265 struct osd_thandle *oh,
4268 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
4269 struct osd_obj_orphan *oor;
4276 oor->oor_env = (struct lu_env *)env;
4277 spin_lock(&osd->od_osfs_lock);
4278 list_add(&oor->oor_list, &osd->od_orphan_list);
4279 spin_unlock(&osd->od_osfs_lock);
4281 oh->ot_remove_agents = 1;
4287 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
4288 struct osd_device *osd)
4290 struct osd_thread_info *info = osd_oti_get(env);
4291 struct osd_obj_orphan *oor, *tmp;
4292 struct osd_inode_id id;
4294 struct inode *inode;
4299 spin_lock(&osd->od_osfs_lock);
4300 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
4301 if (oor->oor_env == env)
4302 list_move(&oor->oor_list, &list);
4304 spin_unlock(&osd->od_osfs_lock);
4306 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
4310 list_del(&oor->oor_list);
4313 osd_id_gen(&id, ino, OSD_OII_NOGEN);
4314 inode = osd_iget_fid(info, osd, &id, &fid);
4318 if (!osd_remote_fid(env, osd, &fid)) {
4323 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
4325 mark_inode_dirty(inode);
4326 ldiskfs_journal_stop(jh);
4334 * OSD layer object create function for OST objects (b=11826).
4336 * The FID is inserted into inode xattr here.
4338 * \retval 0, on success
4339 * \retval -ve, on error
4341 static int osd_create(const struct lu_env *env, struct dt_object *dt,
4342 struct lu_attr *attr, struct dt_allocation_hint *hint,
4343 struct dt_object_format *dof, struct thandle *th)
4345 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
4346 struct osd_object *obj = osd_dt_obj(dt);
4347 struct osd_thread_info *info = osd_oti_get(env);
4348 int result, on_ost = 0;
4352 if (dt_object_exists(dt))
4355 LINVRNT(osd_invariant(obj));
4356 LASSERT(!dt_object_remote(dt));
4357 LASSERT(osd_is_write_locked(env, obj));
4358 LASSERT(th != NULL);
4360 if (unlikely(fid_is_acct(fid)))
4362 * Quota files can't be created from the kernel any more,
4363 * 'tune2fs -O quota' will take care of creating them
4367 result = __osd_create(info, obj, attr, hint, dof, th);
4369 if (fid_is_idif(fid) &&
4370 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
4371 struct lu_fid *tfid = &info->oti_fid;
4372 struct ost_id *oi = &info->oti_ostid;
4374 fid_to_ostid(fid, oi);
4375 ostid_to_fid(tfid, oi, 0);
4377 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
4378 LMAC_FID_ON_OST, 0);
4380 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
4382 result = osd_ea_fid_set(info, obj->oo_inode, fid,
4383 on_ost ? LMAC_FID_ON_OST : 0,
4386 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
4387 obj->oo_dt.do_body_ops = &osd_body_ops;
4390 if (!result && !CFS_FAIL_CHECK(OBD_FAIL_OSD_NO_OI_ENTRY)) {
4391 struct inode *inode = obj->oo_inode;
4393 result = __osd_oi_insert(env, obj, fid, th);
4394 if (result && inode) {
4395 spin_lock(&obj->oo_guard);
4397 spin_unlock(&obj->oo_guard);
4398 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4399 ldiskfs_set_inode_state(inode,
4400 LDISKFS_STATE_LUSTRE_DESTROY);
4402 obj->oo_inode = NULL;
4407 * a small optimization - dt_insert() isn't usually applied
4408 * to OST objects, so we don't need to cache OI mapping for
4411 if (result == 0 && on_ost == 0) {
4412 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4414 result = osd_idc_find_and_init(env, osd, obj);
4415 LASSERT(result == 0);
4418 LASSERT(ergo(result == 0,
4419 dt_object_exists(dt) && !dt_object_remote(dt)));
4420 LINVRNT(osd_invariant(obj));
4424 static int osd_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
4425 struct thandle *handle)
4427 struct osd_thandle *oh;
4430 /* it's possible that object doesn't exist yet */
4431 LASSERT(handle != NULL);
4433 oh = container_of(handle, struct osd_thandle, ot_super);
4434 LASSERT(oh->ot_handle == NULL);
4436 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
4437 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
4439 rc = osd_idc_find_and_init(env, osd_dev(dt->do_lu.lo_dev),
4446 * Concurrency: @dt is write locked.
4448 static int osd_ref_add(const struct lu_env *env, struct dt_object *dt,
4451 struct osd_object *obj = osd_dt_obj(dt);
4452 struct inode *inode = obj->oo_inode;
4453 struct osd_thandle *oh;
4456 if (!dt_object_exists(dt) || obj->oo_destroyed)
4459 LINVRNT(osd_invariant(obj));
4460 LASSERT(!dt_object_remote(dt));
4461 LASSERT(osd_is_write_locked(env, obj));
4462 LASSERT(th != NULL);
4464 oh = container_of(th, struct osd_thandle, ot_super);
4465 LASSERT(oh->ot_handle != NULL);
4467 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
4469 CDEBUG(D_INODE, DFID" increase nlink %d\n",
4470 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
4472 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
4473 * (65000) subdirectories by storing "1" in i_nlink if the link count
4474 * would otherwise overflow. Directory tranversal tools understand
4475 * that (st_nlink == 1) indicates that the filesystem dose not track
4476 * hard links count on the directory, and will not abort subdirectory
4477 * scanning early once (st_nlink - 2) subdirs have been found.
4479 * This also has to properly handle the case of inodes with nlink == 0
4480 * in case they are being linked into the PENDING directory
4482 spin_lock(&obj->oo_guard);
4483 if (unlikely(inode->i_nlink == 0))
4484 /* inc_nlink from 0 may cause WARN_ON */
4485 set_nlink(inode, 1);
4487 osd_ldiskfs_inc_count(oh->ot_handle, inode);
4488 if (!S_ISDIR(inode->i_mode))
4489 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
4491 spin_unlock(&obj->oo_guard);
4493 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4494 LINVRNT(osd_invariant(obj));
4496 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
4501 static int osd_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
4502 struct thandle *handle)
4504 struct osd_thandle *oh;
4506 if (!dt_object_exists(dt))
4509 LASSERT(!dt_object_remote(dt));
4510 LASSERT(handle != NULL);
4512 oh = container_of(handle, struct osd_thandle, ot_super);
4513 LASSERT(oh->ot_handle == NULL);
4515 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
4516 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
4522 * Concurrency: @dt is write locked.
4524 static int osd_ref_del(const struct lu_env *env, struct dt_object *dt,
4527 struct osd_object *obj = osd_dt_obj(dt);
4528 struct inode *inode = obj->oo_inode;
4529 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4530 struct osd_thandle *oh;
4532 if (!dt_object_exists(dt))
4535 LINVRNT(osd_invariant(obj));
4536 LASSERT(!dt_object_remote(dt));
4537 LASSERT(osd_is_write_locked(env, obj));
4538 LASSERT(th != NULL);
4540 if (CFS_FAIL_CHECK(OBD_FAIL_OSD_REF_DEL))
4543 oh = container_of(th, struct osd_thandle, ot_super);
4544 LASSERT(oh->ot_handle != NULL);
4546 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
4548 spin_lock(&obj->oo_guard);
4550 * That can be result of upgrade from old Lustre version and
4551 * applied only to local files. Just skip this ref_del call.
4552 * ext4_unlink() only treats this as a warning, don't LASSERT here.
4554 if (inode->i_nlink == 0) {
4555 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
4556 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
4557 ", maybe an upgraded file? (LU-3915)\n",
4558 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
4559 spin_unlock(&obj->oo_guard);
4563 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
4564 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
4566 osd_ldiskfs_dec_count(oh->ot_handle, inode);
4567 spin_unlock(&obj->oo_guard);
4569 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
4570 LINVRNT(osd_invariant(obj));
4572 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
4578 * Concurrency: @dt is read locked.
4580 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
4581 struct lu_buf *buf, const char *name)
4583 struct osd_object *obj = osd_dt_obj(dt);
4584 struct inode *inode = obj->oo_inode;
4585 struct osd_thread_info *info = osd_oti_get(env);
4586 struct dentry *dentry = &info->oti_obj_dentry;
4587 bool cache_xattr = false;
4592 /* version get is not real XATTR but uses xattr API */
4593 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4594 dt_obj_version_t *ver = buf->lb_buf;
4597 * for version we are just using xattr API but change inode
4600 if (buf->lb_len == 0)
4601 return sizeof(dt_obj_version_t);
4603 if (buf->lb_len < sizeof(dt_obj_version_t))
4606 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
4607 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4609 *ver = LDISKFS_I(inode)->i_fs_version;
4611 return sizeof(dt_obj_version_t);
4614 if (!dt_object_exists(dt))
4617 LASSERT(!dt_object_remote(dt));
4618 LASSERT(inode->i_op != NULL);
4619 #ifdef HAVE_IOP_XATTR
4620 LASSERT(inode->i_op->getxattr != NULL);
4623 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
4624 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
4628 rc = osd_oxc_get(obj, name, buf);
4633 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
4634 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4635 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4636 struct filter_fid *ff;
4637 struct ost_layout *ol;
4639 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
4643 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
4646 if (buf->lb_len == 0 || !buf->lb_buf)
4649 if (buf->lb_len < rc)
4653 ol = &ff->ff_layout;
4654 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
4655 PFID_STRIPE_IDX_BITS);
4656 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
4657 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
4658 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
4659 if (lma->lma_compat & LMAC_COMP_INFO) {
4660 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
4661 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
4662 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
4664 ol->ol_comp_start = 0;
4665 ol->ol_comp_end = 0;
4669 /* Get enc context xattr directly from ldiskfs instead of going
4670 * through the VFS, as there is no xattr handler for
4673 if (strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
4674 rc = ldiskfs_xattr_get(inode,
4675 LDISKFS_XATTR_INDEX_ENCRYPTION,
4676 LDISKFS_XATTR_NAME_ENCRYPTION_CONTEXT,
4677 buf->lb_buf, buf->lb_len);
4679 rc = __osd_xattr_get(inode, dentry, name,
4680 buf->lb_buf, buf->lb_len);
4684 if (rc == -ENOENT || rc == -ENODATA)
4685 osd_oxc_add(obj, name, NULL, 0);
4686 else if (rc > 0 && buf->lb_buf != NULL)
4687 osd_oxc_add(obj, name, buf->lb_buf, rc);
4693 static int osd_declare_xattr_set(const struct lu_env *env,
4694 struct dt_object *dt,
4695 const struct lu_buf *buf, const char *name,
4696 int fl, struct thandle *handle)
4698 struct osd_thandle *oh;
4700 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
4702 LASSERT(handle != NULL);
4704 oh = container_of(handle, struct osd_thandle, ot_super);
4705 LASSERT(oh->ot_handle == NULL);
4707 if (strcmp(name, XATTR_NAME_LMA) == 0) {
4709 * For non-upgrading case, the LMA is set first and
4710 * usually fit inode. But for upgrade case, the LMA
4711 * may be in another separated EA block.
4713 if (dt_object_exists(dt)) {
4714 if (fl == LU_XATTR_REPLACE)
4719 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4721 } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4722 /* We may need to delete the old PFID EA. */
4723 credits = LDISKFS_MAXQUOTAS_DEL_BLOCKS(sb);
4724 if (fl == LU_XATTR_REPLACE)
4730 * If some name entry resides on remote MDT, then will create
4731 * agent entry under remote parent. On the other hand, if the
4732 * remote entry will be removed, then related agent entry may
4733 * need to be removed from the remote parent. So there may be
4734 * kinds of cases, let's declare enough credits. The credits
4735 * for create agent entry is enough for remove case.
4737 if (strcmp(name, XATTR_NAME_LINK) == 0) {
4738 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4739 if (dt_object_exists(dt))
4740 credits += 1; /* For updating LMA */
4744 credits += osd_dto_credits_noquota[DTO_XATTR_SET];
4749 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
4751 * learn xattr size from osd_xattr_get if
4752 * attribute has not been read yet
4754 buflen = __osd_xattr_get(
4755 osd_dt_obj(dt)->oo_inode,
4756 &osd_oti_get(env)->oti_obj_dentry,
4761 buflen = buf->lb_len;
4764 if (buflen > sb->s_blocksize) {
4765 credits += osd_calc_bkmap_credits(
4767 (buflen + sb->s_blocksize - 1) >>
4768 sb->s_blocksize_bits);
4772 * xattr set may involve inode quota change, reserve credits for
4773 * dquot_initialize()
4775 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4778 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
4783 static int osd_xattr_set_pfid(const struct lu_env *env, struct osd_object *obj,
4784 const struct lu_buf *buf, int fl,
4785 struct thandle *handle)
4787 struct osd_thread_info *info = osd_oti_get(env);
4788 struct dentry *dentry = &info->oti_obj_dentry;
4789 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
4790 struct lustre_mdt_attrs *lma = &loa->loa_lma;
4791 struct inode *inode = obj->oo_inode;
4792 struct filter_fid *ff = buf->lb_buf;
4793 struct ost_layout *ol = &ff->ff_layout;
4794 int flags = XATTR_REPLACE;
4799 if (buf->lb_len != sizeof(*ff) && buf->lb_len != sizeof(struct lu_fid))
4802 rc = osd_get_lma(info, inode, dentry, loa);
4803 if (rc == -ENODATA) {
4804 /* Usually for upgarding from old device */
4805 lustre_loa_init(loa, lu_object_fid(&obj->oo_dt.do_lu),
4806 LMAC_FID_ON_OST, 0);
4807 flags = XATTR_CREATE;
4812 if (!rc && lma->lma_compat & LMAC_STRIPE_INFO) {
4813 if ((fl & LU_XATTR_CREATE) && !(fl & LU_XATTR_REPLACE))
4816 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256) {
4817 /* Separate PFID EA from LMA */
4818 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
4819 lustre_lma_swab(lma);
4820 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
4821 sizeof(*lma), XATTR_REPLACE);
4823 obj->oo_pfid_in_lma = 0;
4824 rc = LU_XATTR_CREATE;
4830 if (LDISKFS_INODE_SIZE(inode->i_sb) > 256)
4834 * Old client does not send stripe information,
4835 * then store the PFID EA on disk separatedly.
4837 if (unlikely(buf->lb_len == sizeof(struct lu_fid) ||
4838 ol->ol_stripe_size == 0))
4841 /* Remove old PFID EA entry firstly. */
4842 dquot_initialize(inode);
4843 rc = ll_vfs_removexattr(dentry, inode, XATTR_NAME_FID);
4844 if (rc == -ENODATA) {
4845 /* XATTR_NAME_FID is already absent */
4852 fid_le_to_cpu(&loa->loa_parent_fid, &ff->ff_parent);
4853 if (likely(ol->ol_stripe_size != 0)) {
4854 loa->loa_parent_fid.f_ver |= le32_to_cpu(ol->ol_stripe_count) <<
4855 PFID_STRIPE_IDX_BITS;
4856 loa->loa_stripe_size = le32_to_cpu(ol->ol_stripe_size);
4857 lma->lma_compat |= LMAC_STRIPE_INFO;
4858 if (ol->ol_comp_id != 0) {
4859 loa->loa_comp_id = le32_to_cpu(ol->ol_comp_id);
4860 loa->loa_comp_start = le64_to_cpu(ol->ol_comp_start);
4861 loa->loa_comp_end = le64_to_cpu(ol->ol_comp_end);
4862 lma->lma_compat |= LMAC_COMP_INFO;
4866 lustre_loa_swab(loa, false);
4868 /* Store the PFID EA inside LMA. */
4869 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, loa, sizeof(*loa),
4872 obj->oo_pfid_in_lma = 1;
4878 * In DNE environment, the object (in spite of regular file or directory)
4879 * and its name entry may reside on different MDTs. Under such case, we will
4880 * create an agent entry on the MDT where the object resides. The agent entry
4881 * references the object locally, that makes the object to be visible to the
4882 * userspace when mounted as 'ldiskfs' directly. Then the userspace tools,
4883 * such as 'tar' can handle the object properly.
4885 * We handle the agent entry during set linkEA that is the common interface
4886 * for both regular file and directroy, can handle kinds of cases, such as
4887 * create/link/unlink/rename, and so on.
4889 * NOTE: we can NOT do that when ea_{insert,delete} that is only for directory.
4891 * XXX: There are two known issues:
4892 * 1. For one object, we will create at most one agent entry even if there
4893 * may be more than one cross-MDTs hard links on the object. So the local
4894 * e2fsck may claim that the object's nlink is larger than the name entries
4895 * that reference such inode. And in further, the e2fsck will fix the nlink
4896 * attribute to match the local references. Then it will cause the object's
4897 * nlink attribute to be inconsistent with the global references. it is bad
4898 * but not fatal. The ref_del() can handle the zero-referenced case. On the
4899 * other hand, the global namespace LFSCK can repair the object's attribute
4900 * according to the linkEA.
4901 * 2. There may be too many hard links on the object as to its linkEA overflow,
4902 * then the linkEA entry for cross-MDTs reference may be discarded. If such
4903 * case happened, then at this point, we do not know whether there are some
4904 * cross-MDTs reference. But there are local references, it guarantees that
4905 * object is visible to userspace when mounted as 'ldiskfs'. That is enough.
4907 static int osd_xattr_handle_linkea(const struct lu_env *env,
4908 struct osd_device *osd,
4909 struct osd_object *obj,
4910 const struct lu_buf *buf,
4911 struct thandle *handle)
4913 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
4914 struct lu_fid *tfid = &osd_oti_get(env)->oti_fid3;
4915 struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
4916 struct lu_name tmpname;
4917 struct osd_thandle *oh;
4919 bool remote = false;
4923 oh = container_of(handle, struct osd_thandle, ot_super);
4924 LASSERT(oh->ot_handle != NULL);
4926 rc = linkea_init_with_rec(&ldata);
4928 linkea_first_entry(&ldata);
4929 while (ldata.ld_lee != NULL && !remote) {
4930 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
4932 if (osd_remote_fid(env, osd, tfid) > 0)
4935 linkea_next_entry(&ldata);
4937 } else if (rc == -ENODATA) {
4943 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
4944 rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
4946 CERROR("%s: failed to remove agent entry for "DFID
4947 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4948 } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
4949 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4951 CERROR("%s: failed to create agent entry for "DFID
4952 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
4959 * Concurrency: @dt is write locked.
4961 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
4962 const struct lu_buf *buf, const char *name, int fl,
4963 struct thandle *handle)
4965 struct osd_object *obj = osd_dt_obj(dt);
4966 struct osd_device *osd = osd_obj2dev(obj);
4967 struct inode *inode = obj->oo_inode;
4968 struct osd_thread_info *info = osd_oti_get(env);
4978 /* version set is not real XATTR */
4979 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
4980 dt_obj_version_t *version = buf->lb_buf;
4983 * for version we are just using xattr API but change inode
4986 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
4989 DFID" set version %#llx (old %#llx) for inode %lu\n",
4990 PFID(lu_object_fid(&dt->do_lu)), *version,
4991 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
4993 LDISKFS_I(inode)->i_fs_version = *version;
4995 * Version is set after all inode operations are finished,
4996 * so we should mark it dirty here
4998 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
5003 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
5004 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
5007 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
5010 * For the OST device with 256 bytes inode size by default,
5011 * the PFID EA will be stored together with LMA EA to avoid
5012 * performance trouble. Otherwise the PFID EA can be stored
5013 * independently. LU-8998
5015 if (strcmp(name, XATTR_NAME_FID) == 0 && osd->od_is_ost &&
5016 (LDISKFS_INODE_SIZE(inode->i_sb) <= 256 || obj->oo_pfid_in_lma)) {
5017 LASSERT(buf->lb_buf);
5019 fl = osd_xattr_set_pfid(env, obj, buf, fl, handle);
5022 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
5023 struct lustre_ost_attrs *loa = &info->oti_ost_attrs;
5024 struct lustre_mdt_attrs *lma = &loa->loa_lma;
5026 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, loa);
5030 lma->lma_incompat |= LMAI_STRIPED;
5031 lustre_lma_swab(lma);
5032 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
5033 sizeof(*lma), XATTR_REPLACE);
5036 } else if (strcmp(name, XATTR_NAME_LINK) == 0) {
5037 LASSERT(!osd->od_is_ost);
5039 rc = osd_xattr_handle_linkea(env, osd, obj, buf, handle);
5044 if (fl & LU_XATTR_REPLACE)
5045 fs_flags |= XATTR_REPLACE;
5047 if (fl & LU_XATTR_CREATE)
5048 fs_flags |= XATTR_CREATE;
5050 if (strcmp(name, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0) {
5051 /* Set enc context xattr directly in ldiskfs instead of going
5052 * through the VFS, as there is no xattr handler for
5055 struct osd_thandle *oth = container_of(handle,
5059 if (!oth->ot_handle)
5060 /* this should be already part of a transaction */
5063 rc = ldiskfs_xattr_set_handle(oth->ot_handle, inode,
5064 LDISKFS_XATTR_INDEX_ENCRYPTION,
5065 LDISKFS_XATTR_NAME_ENCRYPTION_CONTEXT,
5066 buf->lb_buf, len, fs_flags);
5068 rc = __osd_xattr_set(info, inode, name,
5069 buf->lb_buf, len, fs_flags);
5071 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
5074 (strcmp(name, XATTR_NAME_LOV) == 0 ||
5075 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
5076 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
5082 * Concurrency: @dt is read locked.
5084 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
5085 const struct lu_buf *buf)
5087 struct osd_object *obj = osd_dt_obj(dt);
5088 struct osd_device *dev = osd_obj2dev(obj);
5089 struct inode *inode = obj->oo_inode;
5090 struct osd_thread_info *info = osd_oti_get(env);
5091 struct dentry *dentry = &info->oti_obj_dentry;
5094 if (!dt_object_exists(dt))
5097 LASSERT(!dt_object_remote(dt));
5098 LASSERT(inode->i_op != NULL);
5099 LASSERT(inode->i_op->listxattr != NULL);
5101 dentry->d_inode = inode;
5102 dentry->d_sb = inode->i_sb;
5103 rc = inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
5105 if (rc < 0 || buf->lb_buf == NULL)
5108 /* Hide virtual project ID xattr from list if disabled */
5109 if (!dev->od_enable_projid_xattr) {
5110 char *end = (char *)buf->lb_buf + rc;
5111 char *p = buf->lb_buf;
5114 char *next = p + strlen(p) + 1;
5116 if (strcmp(p, XATTR_NAME_PROJID) == 0) {
5118 memmove(p, next, end - next);
5130 static int osd_declare_xattr_del(const struct lu_env *env,
5131 struct dt_object *dt, const char *name,
5132 struct thandle *handle)
5134 struct osd_thandle *oh;
5135 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
5137 LASSERT(!dt_object_remote(dt));
5138 LASSERT(handle != NULL);
5140 oh = container_of(handle, struct osd_thandle, ot_super);
5141 LASSERT(oh->ot_handle == NULL);
5143 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
5144 osd_dto_credits_noquota[DTO_XATTR_SET]);
5146 * xattr del may involve inode quota change, reserve credits for
5147 * dquot_initialize()
5149 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
5155 * Concurrency: @dt is write locked.
5157 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
5158 const char *name, struct thandle *handle)
5160 struct osd_object *obj = osd_dt_obj(dt);
5161 struct inode *inode = obj->oo_inode;
5162 struct osd_thread_info *info = osd_oti_get(env);
5163 struct dentry *dentry = &info->oti_obj_dentry;
5166 if (!dt_object_exists(dt))
5169 LASSERT(!dt_object_remote(dt));
5170 LASSERT(inode->i_op != NULL);
5171 LASSERT(handle != NULL);
5172 #ifdef HAVE_IOP_XATTR
5173 LASSERT(inode->i_op->removexattr != NULL);
5176 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
5178 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma) {
5179 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
5181 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
5182 &info->oti_ost_attrs);
5184 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
5186 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
5187 lustre_lma_swab(lma);
5188 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
5189 sizeof(*lma), XATTR_REPLACE);
5191 obj->oo_pfid_in_lma = 0;
5194 dquot_initialize(inode);
5195 dentry->d_inode = inode;
5196 dentry->d_sb = inode->i_sb;
5197 rc = ll_vfs_removexattr(dentry, inode, name);
5200 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
5203 (strcmp(name, XATTR_NAME_LOV) == 0 ||
5204 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
5205 osd_oxc_del(obj, name);
5210 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
5211 __u64 start, __u64 end)
5213 struct osd_object *obj = osd_dt_obj(dt);
5214 struct osd_device *dev = osd_obj2dev(obj);
5215 struct inode *inode = obj->oo_inode;
5220 file = alloc_file_pseudo(inode, dev->od_mnt, "/", O_NOATIME,
5223 RETURN(PTR_ERR(file));
5225 file->f_mode |= FMODE_64BITHASH;
5226 rc = vfs_fsync_range(file, start, end, 0);
5233 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
5238 static bool osd_check_stale(struct dt_object *dt)
5246 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
5247 const struct dt_index_features *feat)
5249 struct iam_descr *descr;
5251 if (osd_object_is_root(o))
5252 return feat == &dt_directory_features;
5254 LASSERT(o->oo_dir != NULL);
5256 descr = o->oo_dir->od_container.ic_descr;
5257 if (feat == &dt_directory_features) {
5258 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
5263 return feat->dif_keysize_min <= descr->id_key_size &&
5264 descr->id_key_size <= feat->dif_keysize_max &&
5265 feat->dif_recsize_min <= descr->id_rec_size &&
5266 descr->id_rec_size <= feat->dif_recsize_max &&
5267 !(feat->dif_flags & (DT_IND_VARKEY |
5268 DT_IND_VARREC | DT_IND_NONUNQ)) &&
5269 ergo(feat->dif_flags & DT_IND_UPDATE,
5270 1 /* XXX check that object (and fs) is writable */);
5274 static int osd_iam_container_init(const struct lu_env *env,
5275 struct osd_object *obj,
5276 struct osd_directory *dir)
5278 struct iam_container *bag = &dir->od_container;
5281 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
5285 result = iam_container_setup(bag);
5287 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
5289 iam_container_fini(bag);
5296 * Concurrency: no external locking is necessary.
5298 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
5299 const struct dt_index_features *feat)
5303 struct osd_object *obj = osd_dt_obj(dt);
5305 LINVRNT(osd_invariant(obj));
5307 if (osd_object_is_root(obj)) {
5308 dt->do_index_ops = &osd_index_ea_ops;
5310 } else if (feat == &dt_directory_features) {
5311 dt->do_index_ops = &osd_index_ea_ops;
5312 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
5317 } else if (unlikely(feat == &dt_otable_features)) {
5318 dt->do_index_ops = &osd_otable_ops;
5320 } else if (unlikely(feat == &dt_acct_features)) {
5321 dt->do_index_ops = &osd_acct_index_ops;
5324 } else if (!osd_has_index(obj)) {
5325 struct osd_directory *dir;
5326 struct osd_device *osd = osd_obj2dev(obj);
5327 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
5332 spin_lock(&obj->oo_guard);
5333 if (obj->oo_dir == NULL)
5337 * Concurrent thread allocated container data.
5340 spin_unlock(&obj->oo_guard);
5342 * Now, that we have container data, serialize its
5345 down_write(&obj->oo_ext_idx_sem);
5347 * recheck under lock.
5350 if (osd_has_index(obj)) {
5355 result = osd_iam_container_init(env, obj, obj->oo_dir);
5356 if (result || feat == &dt_lfsck_namespace_features ||
5357 feat == &dt_lfsck_layout_orphan_features ||
5358 feat == &dt_lfsck_layout_dangling_features)
5361 result = osd_index_register(osd, fid,
5362 feat->dif_keysize_max,
5363 feat->dif_recsize_max);
5365 CWARN("%s: failed to register index "
5367 osd_name(osd), PFID(fid), result);
5368 else if (result > 0)
5371 CDEBUG(D_LFSCK, "%s: index object "DFID
5372 " (%d/%d) registered\n",
5373 osd_name(osd), PFID(fid),
5374 (int)feat->dif_keysize_max,
5375 (int)feat->dif_recsize_max);
5378 up_write(&obj->oo_ext_idx_sem);
5386 if (result == 0 && skip_iam == 0) {
5387 if (!osd_iam_index_probe(env, obj, feat))
5390 LINVRNT(osd_invariant(obj));
5395 static int osd_otable_it_attr_get(const struct lu_env *env,
5396 struct dt_object *dt,
5397 struct lu_attr *attr)
5403 static const struct dt_object_operations osd_obj_ops = {
5404 .do_read_lock = osd_read_lock,
5405 .do_write_lock = osd_write_lock,
5406 .do_read_unlock = osd_read_unlock,
5407 .do_write_unlock = osd_write_unlock,
5408 .do_write_locked = osd_write_locked,
5409 .do_attr_get = osd_attr_get,
5410 .do_declare_attr_set = osd_declare_attr_set,
5411 .do_attr_set = osd_attr_set,
5412 .do_ah_init = osd_ah_init,
5413 .do_declare_create = osd_declare_create,
5414 .do_create = osd_create,
5415 .do_declare_destroy = osd_declare_destroy,
5416 .do_destroy = osd_destroy,
5417 .do_index_try = osd_index_try,
5418 .do_declare_ref_add = osd_declare_ref_add,
5419 .do_ref_add = osd_ref_add,
5420 .do_declare_ref_del = osd_declare_ref_del,
5421 .do_ref_del = osd_ref_del,
5422 .do_xattr_get = osd_xattr_get,
5423 .do_declare_xattr_set = osd_declare_xattr_set,
5424 .do_xattr_set = osd_xattr_set,
5425 .do_declare_xattr_del = osd_declare_xattr_del,
5426 .do_xattr_del = osd_xattr_del,
5427 .do_xattr_list = osd_xattr_list,
5428 .do_object_sync = osd_object_sync,
5429 .do_invalidate = osd_invalidate,
5430 .do_check_stale = osd_check_stale,
5433 static const struct dt_object_operations osd_obj_otable_it_ops = {
5434 .do_attr_get = osd_otable_it_attr_get,
5435 .do_index_try = osd_index_try,
5438 static int osd_index_declare_iam_delete(const struct lu_env *env,
5439 struct dt_object *dt,
5440 const struct dt_key *key,
5441 struct thandle *handle)
5443 struct osd_thandle *oh;
5445 oh = container_of(handle, struct osd_thandle, ot_super);
5446 LASSERT(oh->ot_handle == NULL);
5448 /* Recycle may cause additional three blocks to be changed. */
5449 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
5450 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
5456 * delete a (key, value) pair from index \a dt specified by \a key
5458 * \param dt osd index object
5459 * \param key key for index
5460 * \param rec record reference
5461 * \param handle transaction handler
5464 * \retval -ve failure
5466 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
5467 const struct dt_key *key,
5468 struct thandle *handle)
5470 struct osd_thread_info *oti = osd_oti_get(env);
5471 struct osd_object *obj = osd_dt_obj(dt);
5472 struct osd_thandle *oh;
5473 struct iam_path_descr *ipd;
5474 struct iam_container *bag = &obj->oo_dir->od_container;
5479 if (!dt_object_exists(dt))
5482 LINVRNT(osd_invariant(obj));
5483 LASSERT(!dt_object_remote(dt));
5484 LASSERT(bag->ic_object == obj->oo_inode);
5485 LASSERT(handle != NULL);
5487 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
5489 ipd = osd_idx_ipd_get(env, bag);
5490 if (unlikely(ipd == NULL))
5493 oh = container_of(handle, struct osd_thandle, ot_super);
5494 LASSERT(oh->ot_handle != NULL);
5495 LASSERT(oh->ot_handle->h_transaction != NULL);
5497 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5498 /* swab quota uid/gid provided by caller */
5499 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5500 key = (const struct dt_key *)&oti->oti_quota_id;
5503 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
5504 osd_ipd_put(env, bag, ipd);
5505 LINVRNT(osd_invariant(obj));
5506 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
5510 static int osd_index_declare_ea_delete(const struct lu_env *env,
5511 struct dt_object *dt,
5512 const struct dt_key *key,
5513 struct thandle *handle)
5515 struct osd_thandle *oh;
5516 struct inode *inode;
5521 LASSERT(!dt_object_remote(dt));
5522 LASSERT(handle != NULL);
5524 oh = container_of(handle, struct osd_thandle, ot_super);
5525 LASSERT(oh->ot_handle == NULL);
5527 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
5528 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
5530 inode = osd_dt_obj(dt)->oo_inode;
5534 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
5535 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
5540 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
5543 struct osd_fid_pack *rec;
5546 if (de->file_type & LDISKFS_DIRENT_LUFID) {
5547 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
5548 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
5549 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
5555 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
5556 const struct lu_fid *fid)
5558 struct seq_server_site *ss = osd_seq_site(osd);
5562 /* FID seqs not in FLDB, must be local seq */
5563 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
5567 * If FLD is not being initialized yet, it only happens during the
5568 * initialization, likely during mgs initialization, and we assume
5569 * this is local FID.
5571 if (ss == NULL || ss->ss_server_fld == NULL)
5574 /* Only check the local FLDB here */
5575 if (osd_seq_exists(env, osd, fid_seq(fid)))
5581 static void osd_take_care_of_agent(const struct lu_env *env,
5582 struct osd_device *osd,
5583 struct osd_thandle *oh,
5584 struct ldiskfs_dir_entry_2 *de)
5586 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
5587 struct osd_idmap_cache *idc;
5588 int rc, schedule = 0;
5590 LASSERT(de != NULL);
5592 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
5593 if (likely(rc == 0)) {
5594 idc = osd_idc_find_or_init(env, osd, fid);
5595 if (IS_ERR(idc) || idc->oic_remote)
5597 } else if (rc == -ENODATA) {
5599 * can't get FID, postpone to the end of the
5600 * transaction when iget() is safe
5604 CERROR("%s: can't get FID: rc = %d\n", osd_name(osd), rc);
5607 osd_schedule_agent_inode_removal(env, oh,
5608 le32_to_cpu(de->inode));
5612 * Utility function to get real name from object name
5614 * \param[in] obj pointer to the object to be handled
5615 * \param[in] name object name
5616 * \param[in] len object name len
5617 * \param[out]ln pointer to the struct lu_name to hold the real name
5619 * If file is not encrypted, real name is just the object name.
5620 * If file is encrypted, object name needs to be decoded. In
5621 * this case a new buffer is allocated, and ln->ln_name needs to be freed by
5624 * \retval 0, on success
5625 * \retval -ve, on error
5627 static int obj_name2lu_name(struct osd_object *obj, const char *name,
5628 int len, struct lu_name *ln)
5630 if (!(obj->oo_lma_flags & LUSTRE_ENCRYPT_FL)) {
5632 ln->ln_namelen = len;
5634 char *buf = kmalloc(len, GFP_NOFS);
5639 len = critical_decode(name, len, buf);
5641 ln->ln_namelen = len;
5648 * Index delete function for interoperability mode (b11826).
5649 * It will remove the directory entry added by osd_index_ea_insert().
5650 * This entry is needed to maintain name->fid mapping.
5652 * \param key, key i.e. file entry to be deleted
5654 * \retval 0, on success
5655 * \retval -ve, on error
5657 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
5658 const struct dt_key *key, struct thandle *handle)
5660 struct osd_object *obj = osd_dt_obj(dt);
5661 struct inode *dir = obj->oo_inode;
5662 struct dentry *dentry;
5663 struct osd_thandle *oh;
5664 struct ldiskfs_dir_entry_2 *de = NULL;
5665 struct buffer_head *bh;
5666 struct htree_lock *hlock = NULL;
5667 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5673 if (!dt_object_exists(dt))
5676 LINVRNT(osd_invariant(obj));
5677 LASSERT(!dt_object_remote(dt));
5678 LASSERT(handle != NULL);
5680 rc = obj_name2lu_name(obj, (char *)key, strlen((char *)key), &ln);
5684 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
5686 oh = container_of(handle, struct osd_thandle, ot_super);
5687 LASSERT(oh->ot_handle != NULL);
5688 LASSERT(oh->ot_handle->h_transaction != NULL);
5690 dquot_initialize(dir);
5691 dentry = osd_child_dentry_get(env, obj, ln.ln_name, ln.ln_namelen);
5693 if (obj->oo_hl_head != NULL) {
5694 hlock = osd_oti_get(env)->oti_hlock;
5695 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5696 dir, LDISKFS_HLOCK_DEL);
5698 down_write(&obj->oo_ext_idx_sem);
5701 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5704 * If this is not the ".." entry, it might be a remote DNE
5705 * entry and we need to check if the FID is for a remote
5706 * MDT. If the FID is not in the directory entry (e.g.
5707 * upgraded 1.8 filesystem without dirdata enabled) then
5708 * we need to get the FID from the LMA. For a remote directory
5709 * there HAS to be an LMA, it cannot be an IGIF inode in this
5712 * Delete the entry before the agent inode in order to
5713 * simplify error handling. At worst an error after deleting
5714 * the entry first might leak the agent inode afterward. The
5715 * reverse would need filesystem abort in case of error deleting
5716 * the entry after the agent had been removed, or leave a
5717 * dangling entry pointing at a random inode.
5719 if (strcmp((char *)key, dotdot) != 0)
5720 osd_take_care_of_agent(env, osd, oh, de);
5721 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
5727 if (!rc && fid_is_namespace_visible(lu_object_fid(&dt->do_lu)) &&
5728 obj->oo_dirent_count != LU_DIRENT_COUNT_UNSET) {
5729 /* NB, dirent count may not be accurate, because it's counted
5732 if (obj->oo_dirent_count)
5733 obj->oo_dirent_count--;
5735 obj->oo_dirent_count = LU_DIRENT_COUNT_UNSET;
5738 ldiskfs_htree_unlock(hlock);
5740 up_write(&obj->oo_ext_idx_sem);
5743 LASSERT(osd_invariant(obj));
5744 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
5745 if (ln.ln_name != (char *)key)
5751 * Lookup index for \a key and copy record to \a rec.
5753 * \param dt osd index object
5754 * \param key key for index
5755 * \param rec record reference
5757 * \retval +ve success : exact mach
5758 * \retval 0 return record with key not greater than \a key
5759 * \retval -ve failure
5761 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
5762 struct dt_rec *rec, const struct dt_key *key)
5764 struct osd_object *obj = osd_dt_obj(dt);
5765 struct iam_path_descr *ipd;
5766 struct iam_container *bag = &obj->oo_dir->od_container;
5767 struct osd_thread_info *oti = osd_oti_get(env);
5768 struct iam_iterator *it = &oti->oti_idx_it;
5769 struct iam_rec *iam_rec;
5774 if (!dt_object_exists(dt))
5777 LASSERT(osd_invariant(obj));
5778 LASSERT(!dt_object_remote(dt));
5779 LASSERT(bag->ic_object == obj->oo_inode);
5781 ipd = osd_idx_ipd_get(env, bag);
5785 /* got ipd now we can start iterator. */
5786 iam_it_init(it, bag, 0, ipd);
5788 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5789 /* swab quota uid/gid provided by caller */
5790 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5791 key = (const struct dt_key *)&oti->oti_quota_id;
5794 rc = iam_it_get(it, (struct iam_key *)key);
5796 if (S_ISDIR(obj->oo_inode->i_mode))
5797 iam_rec = (struct iam_rec *)oti->oti_ldp;
5799 iam_rec = (struct iam_rec *)rec;
5801 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
5803 if (S_ISDIR(obj->oo_inode->i_mode))
5804 osd_fid_unpack((struct lu_fid *)rec,
5805 (struct osd_fid_pack *)iam_rec);
5806 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
5807 osd_quota_unpack(obj, rec);
5812 osd_ipd_put(env, bag, ipd);
5814 LINVRNT(osd_invariant(obj));
5819 static int osd_index_declare_iam_insert(const struct lu_env *env,
5820 struct dt_object *dt,
5821 const struct dt_rec *rec,
5822 const struct dt_key *key,
5823 struct thandle *handle)
5825 struct osd_thandle *oh;
5827 LASSERT(handle != NULL);
5829 oh = container_of(handle, struct osd_thandle, ot_super);
5830 LASSERT(oh->ot_handle == NULL);
5832 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
5833 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
5839 * Inserts (key, value) pair in \a dt index object.
5841 * \param dt osd index object
5842 * \param key key for index
5843 * \param rec record reference
5844 * \param th transaction handler
5847 * \retval -ve failure
5849 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
5850 const struct dt_rec *rec,
5851 const struct dt_key *key, struct thandle *th)
5853 struct osd_object *obj = osd_dt_obj(dt);
5854 struct iam_path_descr *ipd;
5855 struct osd_thandle *oh;
5856 struct iam_container *bag;
5857 struct osd_thread_info *oti = osd_oti_get(env);
5858 struct iam_rec *iam_rec;
5863 if (!dt_object_exists(dt))
5866 LINVRNT(osd_invariant(obj));
5867 LASSERT(!dt_object_remote(dt));
5869 bag = &obj->oo_dir->od_container;
5870 LASSERT(bag->ic_object == obj->oo_inode);
5871 LASSERT(th != NULL);
5873 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5875 ipd = osd_idx_ipd_get(env, bag);
5876 if (unlikely(ipd == NULL))
5879 oh = container_of(th, struct osd_thandle, ot_super);
5880 LASSERT(oh->ot_handle != NULL);
5881 LASSERT(oh->ot_handle->h_transaction != NULL);
5882 if (S_ISDIR(obj->oo_inode->i_mode)) {
5883 iam_rec = (struct iam_rec *)oti->oti_ldp;
5884 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
5886 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
5887 /* pack quota uid/gid */
5888 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5889 key = (const struct dt_key *)&oti->oti_quota_id;
5890 /* pack quota record */
5891 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
5892 iam_rec = (struct iam_rec *)rec;
5894 iam_rec = (struct iam_rec *)rec;
5897 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
5899 osd_ipd_put(env, bag, ipd);
5900 LINVRNT(osd_invariant(obj));
5901 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5906 * Calls ldiskfs_add_entry() to add directory entry
5907 * into the directory. This is required for
5908 * interoperability mode (b11826)
5910 * \retval 0, on success
5911 * \retval -ve, on error
5913 static int __osd_ea_add_rec(struct osd_thread_info *info,
5914 struct osd_object *pobj, struct inode *cinode,
5915 const char *name, const struct lu_fid *fid,
5916 struct htree_lock *hlock, struct thandle *th)
5918 struct ldiskfs_dentry_param *ldp;
5919 struct dentry *child;
5920 struct osd_thandle *oth;
5924 oth = container_of(th, struct osd_thandle, ot_super);
5925 LASSERT(oth->ot_handle != NULL);
5926 LASSERT(oth->ot_handle->h_transaction != NULL);
5927 LASSERT(pobj->oo_inode);
5929 rc = obj_name2lu_name(pobj, name, strlen(name), &ln);
5933 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
5934 if (unlikely(osd_object_is_root(pobj)))
5937 osd_get_ldiskfs_dirent_param(ldp, fid);
5938 child = osd_child_dentry_get(info->oti_env, pobj,
5939 ln.ln_name, ln.ln_namelen);
5940 child->d_fsdata = (void *)ldp;
5941 dquot_initialize(pobj->oo_inode);
5942 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
5943 child, cinode, hlock);
5944 if (rc == 0 && CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
5945 struct ldiskfs_dir_entry_2 *de;
5946 struct buffer_head *bh;
5949 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
5952 rc1 = osd_ldiskfs_journal_get_write_access(
5953 oth->ot_handle, pobj->oo_inode->i_sb, bh,
5956 if (S_ISDIR(cinode->i_mode))
5957 de->file_type = LDISKFS_DIRENT_LUFID |
5958 LDISKFS_FT_REG_FILE;
5960 de->file_type = LDISKFS_DIRENT_LUFID |
5962 ldiskfs_handle_dirty_metadata(oth->ot_handle,
5969 if (ln.ln_name != name)
5975 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
5976 * into the directory.Also sets flags into osd object to
5977 * indicate dot and dotdot are created. This is required for
5978 * interoperability mode (b11826)
5980 * \param dir directory for dot and dotdot fixup.
5981 * \param obj child object for linking
5983 * \retval 0, on success
5984 * \retval -ve, on error
5986 static int osd_add_dot_dotdot(struct osd_thread_info *info,
5987 struct osd_object *dir,
5988 struct inode *parent_dir, const char *name,
5989 const struct lu_fid *dot_fid,
5990 const struct lu_fid *dot_dot_fid,
5993 struct inode *inode = dir->oo_inode;
5994 struct osd_thandle *oth;
5997 oth = container_of(th, struct osd_thandle, ot_super);
5998 LASSERT(oth->ot_handle->h_transaction != NULL);
5999 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
6001 if (strcmp(name, dot) == 0) {
6002 if (dir->oo_compat_dot_created) {
6005 LASSERT(inode->i_ino == parent_dir->i_ino);
6006 dir->oo_compat_dot_created = 1;
6009 } else if (strcmp(name, dotdot) == 0) {
6010 if (!dir->oo_compat_dot_created)
6012 /* in case of rename, dotdot is already created */
6013 if (dir->oo_compat_dotdot_created) {
6014 return __osd_ea_add_rec(info, dir, parent_dir, name,
6015 dot_dot_fid, NULL, th);
6018 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
6019 struct lu_fid tfid = *dot_dot_fid;
6022 result = osd_add_dot_dotdot_internal(info,
6023 dir->oo_inode, parent_dir, dot_fid,
6026 result = osd_add_dot_dotdot_internal(info,
6027 dir->oo_inode, parent_dir, dot_fid,
6032 dir->oo_compat_dotdot_created = 1;
6040 * It will call the appropriate osd_add* function and return the
6041 * value, return by respective functions.
6043 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
6044 struct inode *cinode, const char *name,
6045 const struct lu_fid *fid, struct thandle *th)
6047 struct osd_thread_info *info = osd_oti_get(env);
6048 struct htree_lock *hlock;
6051 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
6053 if (name[0] == '.' && (name[1] == '\0' ||
6054 (name[1] == '.' && name[2] == '\0'))) {
6055 if (hlock != NULL) {
6056 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
6059 down_write(&pobj->oo_ext_idx_sem);
6062 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
6063 lu_object_fid(&pobj->oo_dt.do_lu),
6066 if (hlock != NULL) {
6067 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
6068 pobj->oo_inode, LDISKFS_HLOCK_ADD);
6070 down_write(&pobj->oo_ext_idx_sem);
6073 if (CFS_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
6074 struct lu_fid *tfid = &info->oti_fid;
6078 rc = __osd_ea_add_rec(info, pobj, cinode, name,
6081 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
6085 if (!rc && fid_is_namespace_visible(lu_object_fid(&pobj->oo_dt.do_lu))
6086 && pobj->oo_dirent_count != LU_DIRENT_COUNT_UNSET)
6087 pobj->oo_dirent_count++;
6090 ldiskfs_htree_unlock(hlock);
6092 up_write(&pobj->oo_ext_idx_sem);
6098 osd_ldiskfs_consistency_check(struct osd_thread_info *oti,
6099 struct osd_device *dev,
6100 const struct lu_fid *fid,
6101 struct osd_inode_id *id)
6103 struct lustre_scrub *scrub = &dev->od_scrub.os_scrub;
6104 struct inode *inode = NULL;
6110 if (!scrub_needs_check(scrub, fid, id->oii_ino))
6113 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
6114 if (rc == -ENOENT) {
6115 __u32 gen = id->oii_gen;
6121 inode = osd_iget(oti, dev, id);
6122 /* The inode has been removed (by race maybe). */
6123 if (IS_ERR(inode)) {
6124 rc = PTR_ERR(inode);
6126 RETURN(rc == -ESTALE ? -ENOENT : rc);
6129 /* The OI mapping is lost. */
6130 if (gen != OSD_OII_NOGEN)
6134 * The inode may has been reused by others, we do not know,
6135 * leave it to be handled by subsequent osd_fid_lookup().
6138 } else if (rc || osd_id_eq(id, &oti->oti_id)) {
6145 if (scrub->os_running) {
6146 if (inode == NULL) {
6147 inode = osd_iget(oti, dev, id);
6148 /* The inode has been removed (by race maybe). */
6149 if (IS_ERR(inode)) {
6150 rc = PTR_ERR(inode);
6152 RETURN(rc == -ESTALE ? -ENOENT : rc);
6156 rc = osd_scrub_oi_insert(dev, fid, id, insert);
6158 * There is race condition between osd_oi_lookup and OI scrub.
6159 * The OI scrub finished just after osd_oi_lookup() failure.
6160 * Under such case, it is unnecessary to trigger OI scrub again,
6161 * but try to call osd_oi_lookup() again.
6163 if (unlikely(rc == -EAGAIN))
6166 if (!S_ISDIR(inode->i_mode))
6169 rc = osd_check_lmv(oti, dev, inode);
6174 if (dev->od_scrub.os_scrub.os_auto_scrub_interval != AS_NEVER &&
6176 rc = osd_scrub_start(oti->oti_env, dev, SS_AUTO_PARTIAL |
6177 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
6178 CDEBUG_LIMIT(D_LFSCK | D_CONSOLE | D_WARNING,
6179 "%s: trigger partial OI scrub for RPC inconsistency, checking FID "DFID"/%u: rc = %d\n",
6180 osd_dev2name(dev), PFID(fid), id->oii_ino, rc);
6181 if (rc == 0 || rc == -EALREADY)
6193 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
6194 struct osd_device *dev,
6195 struct lu_fid *fid, __u32 ino)
6197 struct lustre_ost_attrs *loa = &oti->oti_ost_attrs;
6198 struct osd_idmap_cache *oic = &oti->oti_cache;
6199 struct inode *inode;
6202 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
6203 inode = osd_iget(oti, dev, &oic->oic_lid);
6204 if (IS_ERR(inode)) {
6205 fid_zero(&oic->oic_fid);
6206 return PTR_ERR(inode);
6209 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, loa);
6212 fid_zero(&oic->oic_fid);
6214 *fid = oic->oic_fid = loa->loa_lma.lma_self_fid;
6218 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
6219 struct osd_inode_id *id, const struct lu_fid *fid)
6221 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
6222 id->oii_ino, id->oii_gen, info);
6223 info->oti_cache.oic_lid = *id;
6224 info->oti_cache.oic_fid = *fid;
6225 info->oti_cache.oic_dev = osd;
6229 * Get parent FID from the linkEA.
6231 * For a directory which parent resides on remote MDT, to satisfy the
6232 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
6233 * the other hand, to make the lookup(..) on the directory can return
6234 * the real parent FID, we append the real parent FID after its ".."
6235 * name entry in the /REMOTE_PARENT_DIR.
6237 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
6238 * backup. So after the restore, we cannot get the right parent FID from
6239 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
6240 * we have stored the real parent FID in the directory object's linkEA,
6241 * we can parse the linkEA for the real parent FID.
6243 * \param[in] env pointer to the thread context
6244 * \param[in] obj pointer to the object to be handled
6245 * \param[out]fid pointer to the buffer to hold the parent FID
6247 * \retval 0 for getting the real parent FID successfully
6248 * \retval negative error number on failure
6250 static int osd_get_pfid_from_linkea(const struct lu_env *env,
6251 struct osd_object *obj,
6254 struct osd_thread_info *oti = osd_oti_get(env);
6255 struct lu_buf *buf = &oti->oti_big_buf;
6256 struct dentry *dentry = &oti->oti_obj_dentry;
6257 struct inode *inode = obj->oo_inode;
6258 struct linkea_data ldata = { NULL };
6264 if (!S_ISDIR(inode->i_mode))
6268 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
6269 buf->lb_buf, buf->lb_len);
6270 if (rc == -ERANGE) {
6271 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
6274 lu_buf_realloc(buf, rc);
6275 if (buf->lb_buf == NULL)
6282 if (unlikely(rc == 0))
6288 if (unlikely(buf->lb_buf == NULL)) {
6289 lu_buf_realloc(buf, rc);
6290 if (buf->lb_buf == NULL)
6297 rc = linkea_init_with_rec(&ldata);
6299 linkea_first_entry(&ldata);
6300 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
6306 static int osd_verify_ent_by_linkea(const struct lu_env *env,
6307 struct inode *inode,
6308 const struct lu_fid *pfid,
6309 const char *name, const int namelen)
6311 struct osd_thread_info *oti = osd_oti_get(env);
6312 struct lu_buf *buf = &oti->oti_big_buf;
6313 struct dentry *dentry = &oti->oti_obj_dentry;
6314 struct linkea_data ldata = { NULL };
6315 struct lu_name cname = { .ln_name = name,
6316 .ln_namelen = namelen };
6322 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
6323 buf->lb_buf, buf->lb_len);
6325 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
6330 if (unlikely(rc == 0))
6333 if (buf->lb_len < rc) {
6334 lu_buf_realloc(buf, rc);
6335 if (buf->lb_buf == NULL)
6342 rc = linkea_init_with_rec(&ldata);
6344 rc = linkea_links_find(&ldata, &cname, pfid);
6350 * Calls ->lookup() to find dentry. From dentry get inode and
6351 * read inode's ea to get fid. This is required for interoperability
6354 * \retval 0, on success
6355 * \retval -ve, on error
6357 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
6358 struct dt_rec *rec, const struct lu_name *ln)
6360 struct inode *dir = obj->oo_inode;
6361 struct dentry *dentry;
6362 struct ldiskfs_dir_entry_2 *de;
6363 struct buffer_head *bh;
6364 struct lu_fid *fid = (struct lu_fid *)rec;
6365 struct htree_lock *hlock = NULL;
6371 LASSERT(dir->i_op != NULL);
6372 LASSERT(dir->i_op->lookup != NULL);
6374 dentry = osd_child_dentry_get(env, obj, ln->ln_name, ln->ln_namelen);
6376 if (obj->oo_hl_head != NULL) {
6377 hlock = osd_oti_get(env)->oti_hlock;
6378 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
6379 dir, LDISKFS_HLOCK_LOOKUP);
6381 down_read(&obj->oo_ext_idx_sem);
6384 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6386 struct osd_thread_info *oti = osd_oti_get(env);
6387 struct osd_inode_id *id = &oti->oti_id;
6388 struct osd_device *dev = osd_obj2dev(obj);
6390 ino = le32_to_cpu(de->inode);
6391 if (CFS_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
6393 rc = osd_fail_fid_lookup(oti, dev, fid, ino);
6397 rc = osd_get_fid_from_dentry(de, rec);
6399 /* done with de, release bh */
6402 if (unlikely(is_remote_parent_ino(dev, ino))) {
6404 * If the parent is on remote MDT, and there
6405 * is no FID-in-dirent, then we have to get
6406 * the parent FID from the linkEA.
6408 if (likely(ln->ln_namelen == 2 &&
6409 ln->ln_name[0] == '.' && ln->ln_name[1] == '.'))
6410 rc = osd_get_pfid_from_linkea(env, obj,
6413 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6416 osd_id_gen(id, ino, OSD_OII_NOGEN);
6419 if (rc != 0 || osd_remote_fid(env, dev, fid))
6422 rc = osd_ldiskfs_consistency_check(oti, dev, fid, id);
6423 if (rc != -ENOENT) {
6424 /* Other error should not affect lookup result. */
6427 /* Normal file mapping should be added into OI cache
6428 * after FID in LMA check, but for local files like
6429 * hsm_actions, their FIDs are not stored in OI files,
6430 * see osd_initial_OI_scrub(), and here is the only
6431 * place to load mapping into OI cache.
6433 if (!fid_is_namespace_visible(fid))
6434 osd_add_oi_cache(osd_oti_get(env),
6435 osd_obj2dev(obj), id, fid);
6437 CDEBUG(D_INODE, DFID"/"DNAME" => "DFID"\n",
6438 PFID(lu_object_fid(&obj->oo_dt.do_lu)), PNAME(ln),
6448 ldiskfs_htree_unlock(hlock);
6450 up_read(&obj->oo_ext_idx_sem);
6454 static int osd_index_declare_ea_insert(const struct lu_env *env,
6455 struct dt_object *dt,
6456 const struct dt_rec *rec,
6457 const struct dt_key *key,
6458 struct thandle *handle)
6460 struct osd_thandle *oh;
6461 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
6462 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
6463 const struct lu_fid *fid = rec1->rec_fid;
6464 int credits, rc = 0;
6465 struct osd_idmap_cache *idc;
6469 LASSERT(!dt_object_remote(dt));
6470 LASSERT(handle != NULL);
6471 LASSERT(fid != NULL);
6472 LASSERT(rec1->rec_type != 0);
6474 oh = container_of(handle, struct osd_thandle, ot_super);
6475 LASSERT(oh->ot_handle == NULL);
6477 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
6480 * we can't call iget() while a transactions is running
6481 * (this can lead to a deadlock), but we need to know
6482 * inum and object type. so we find this information at
6483 * declaration and cache in per-thread info
6485 idc = osd_idc_find_or_init(env, osd, fid);
6487 RETURN(PTR_ERR(idc));
6488 if (idc->oic_remote) {
6490 * a reference to remote inode is represented by an
6491 * agent inode which we have to create
6493 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
6494 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
6497 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
6499 if (osd_dt_obj(dt)->oo_inode != NULL) {
6500 struct inode *inode = osd_dt_obj(dt)->oo_inode;
6503 * We ignore block quota on meta pool (MDTs), so needn't
6504 * calculate how many blocks will be consumed by this index
6507 rc = osd_declare_inode_qid(env, i_uid_read(inode),
6509 i_projid_read(inode), 0,
6510 oh, osd_dt_obj(dt), NULL,
6515 #ifdef HAVE_PROJECT_QUOTA
6517 * Reserve credits for local agent inode to transfer
6518 * to 0, quota enforcement is ignored in this case.
6520 if (idc->oic_remote &&
6521 LDISKFS_I(inode)->i_flags & LUSTRE_PROJINHERIT_FL &&
6522 i_projid_read(inode) != 0)
6523 rc = osd_declare_attr_qid(env, osd_dt_obj(dt), oh,
6524 0, i_projid_read(inode),
6525 0, false, PRJQUOTA);
6533 * Index add function for interoperability mode (b11826).
6534 * It will add the directory entry.This entry is needed to
6535 * maintain name->fid mapping.
6537 * \param key it is key i.e. file entry to be inserted
6538 * \param rec it is value of given key i.e. fid
6540 * \retval 0, on success
6541 * \retval -ve, on error
6543 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
6544 const struct dt_rec *rec,
6545 const struct dt_key *key, struct thandle *th)
6547 struct osd_object *obj = osd_dt_obj(dt);
6548 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
6549 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
6550 const struct lu_fid *fid = rec1->rec_fid;
6551 const char *name = (const char *)key;
6552 struct osd_thread_info *oti = osd_oti_get(env);
6553 struct inode *child_inode = NULL;
6554 struct osd_idmap_cache *idc;
6559 if (!dt_object_exists(dt))
6562 LASSERT(osd_invariant(obj));
6563 LASSERT(!dt_object_remote(dt));
6564 LASSERT(th != NULL);
6566 osd_trans_exec_op(env, th, OSD_OT_INSERT);
6568 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
6570 idc = osd_idc_find(env, osd, fid);
6571 if (unlikely(idc == NULL)) {
6572 idc = osd_idc_find_or_init(env, osd, fid);
6575 * this dt_insert() wasn't declared properly, so
6576 * FID is missing in OI cache. we better do not
6577 * lookup FID in FLDB/OI and don't risk to deadlock,
6578 * but in some special cases (lfsck testing, etc)
6579 * it's much simpler than fixing a caller.
6581 * normally this error should be placed after the first
6582 * find, but migrate may attach source stripes to
6583 * target, which doesn't create stripes.
6585 CERROR("%s: "DFID" wasn't declared for insert\n",
6586 osd_name(osd), PFID(fid));
6588 RETURN(PTR_ERR(idc));
6592 if (idc->oic_remote) {
6593 /* Insert remote entry */
6594 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
6596 igrab(osd->od_mdt_map->omm_remote_parent->d_inode);
6598 child_inode = osd_create_local_agent_inode(env, osd,
6599 obj, fid, rec1->rec_type & S_IFMT, th);
6600 if (IS_ERR(child_inode))
6601 RETURN(PTR_ERR(child_inode));
6604 /* Insert local entry */
6605 if (unlikely(idc->oic_lid.oii_ino == 0)) {
6606 /* for a reason OI cache wasn't filled properly */
6607 CERROR("%s: OIC for "DFID" isn't filled\n",
6608 osd_name(osd), PFID(fid));
6611 child_inode = oti->oti_inode;
6612 if (unlikely(child_inode == NULL)) {
6613 struct ldiskfs_inode_info *lii;
6618 child_inode = oti->oti_inode = &lii->vfs_inode;
6620 child_inode->i_sb = osd_sb(osd);
6621 child_inode->i_ino = idc->oic_lid.oii_ino;
6622 child_inode->i_mode = rec1->rec_type & S_IFMT;
6625 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
6627 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
6628 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
6630 if (child_inode && child_inode != oti->oti_inode)
6632 LASSERT(osd_invariant(obj));
6633 osd_trans_exec_check(env, th, OSD_OT_INSERT);
6639 * Initialize osd Iterator for given osd index object.
6641 * \param dt osd index object
6644 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
6645 struct dt_object *dt,
6648 struct osd_it_iam *it;
6649 struct osd_object *obj = osd_dt_obj(dt);
6650 struct lu_object *lo = &dt->do_lu;
6651 struct iam_path_descr *ipd;
6652 struct iam_container *bag = &obj->oo_dir->od_container;
6654 if (!dt_object_exists(dt))
6655 return ERR_PTR(-ENOENT);
6659 return ERR_PTR(-ENOMEM);
6661 ipd = osd_it_ipd_get(env, bag);
6662 if (likely(ipd != NULL)) {
6666 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
6667 return (struct dt_it *)it;
6670 return ERR_PTR(-ENOMEM);
6675 * free given Iterator.
6677 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
6679 struct osd_it_iam *it = (struct osd_it_iam *)di;
6680 struct osd_object *obj = it->oi_obj;
6682 iam_it_fini(&it->oi_it);
6683 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
6684 osd_object_put(env, obj);
6689 * Move Iterator to record specified by \a key
6691 * \param di osd iterator
6692 * \param key key for index
6694 * \retval +ve di points to record with least key not larger than key
6695 * \retval 0 di points to exact matched key
6696 * \retval -ve failure
6699 static int osd_it_iam_get(const struct lu_env *env,
6700 struct dt_it *di, const struct dt_key *key)
6702 struct osd_thread_info *oti = osd_oti_get(env);
6703 struct osd_it_iam *it = (struct osd_it_iam *)di;
6705 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
6706 /* swab quota uid/gid */
6707 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
6708 key = (struct dt_key *)&oti->oti_quota_id;
6711 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
6717 * \param di osd iterator
6719 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
6721 struct osd_it_iam *it = (struct osd_it_iam *)di;
6723 iam_it_put(&it->oi_it);
6727 * Move iterator by one record
6729 * \param di osd iterator
6731 * \retval +1 end of container reached
6733 * \retval -ve failure
6736 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
6738 struct osd_it_iam *it = (struct osd_it_iam *)di;
6740 return iam_it_next(&it->oi_it);
6744 * Return pointer to the key under iterator.
6747 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
6748 const struct dt_it *di)
6750 struct osd_thread_info *oti = osd_oti_get(env);
6751 struct osd_it_iam *it = (struct osd_it_iam *)di;
6752 struct osd_object *obj = it->oi_obj;
6755 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
6757 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
6758 /* swab quota uid/gid */
6759 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
6760 key = (struct dt_key *)&oti->oti_quota_id;
6767 * Return size of key under iterator (in bytes)
6770 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
6772 struct osd_it_iam *it = (struct osd_it_iam *)di;
6774 return iam_it_key_size(&it->oi_it);
6778 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
6780 /* check if file type is required */
6781 if (ent->lde_attrs & LUDA_TYPE) {
6782 struct luda_type *lt;
6783 int align = sizeof(*lt) - 1;
6785 len = (len + align) & ~align;
6786 lt = (struct luda_type *)(ent->lde_name + len);
6787 lt->lt_type = cpu_to_le16(DTTOIF(type));
6790 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
6794 * build lu direct from backend fs dirent.
6798 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
6799 char *name, __u16 namelen, __u16 type, __u32 attr)
6801 ent->lde_attrs = attr | LUDA_FID;
6802 fid_cpu_to_le(&ent->lde_fid, fid);
6804 ent->lde_hash = cpu_to_le64(offset);
6805 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
6807 strncpy(ent->lde_name, name, namelen);
6808 ent->lde_name[namelen] = '\0';
6809 ent->lde_namelen = cpu_to_le16(namelen);
6811 /* append lustre attributes */
6812 osd_it_append_attrs(ent, namelen, type);
6816 * Return pointer to the record under iterator.
6818 static int osd_it_iam_rec(const struct lu_env *env,
6819 const struct dt_it *di,
6820 struct dt_rec *dtrec, __u32 attr)
6822 struct osd_it_iam *it = (struct osd_it_iam *)di;
6823 struct osd_thread_info *info = osd_oti_get(env);
6827 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
6828 const struct osd_fid_pack *rec;
6829 struct lu_fid *fid = &info->oti_fid;
6830 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6836 name = (char *)iam_it_key_get(&it->oi_it);
6838 RETURN(PTR_ERR(name));
6840 namelen = iam_it_key_size(&it->oi_it);
6842 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
6844 RETURN(PTR_ERR(rec));
6846 rc = osd_fid_unpack(fid, rec);
6850 hash = iam_it_store(&it->oi_it);
6852 /* IAM does not store object type in IAM index (dir) */
6853 osd_it_pack_dirent(lde, fid, hash, name, namelen,
6855 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
6856 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6857 (struct iam_rec *)dtrec);
6858 osd_quota_unpack(it->oi_obj, dtrec);
6860 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
6861 (struct iam_rec *)dtrec);
6868 * Returns cookie for current Iterator position.
6870 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
6872 struct osd_it_iam *it = (struct osd_it_iam *)di;
6874 return iam_it_store(&it->oi_it);
6878 * Restore iterator from cookie.
6880 * \param di osd iterator
6881 * \param hash Iterator location cookie
6883 * \retval +ve di points to record with least key not larger than key.
6884 * \retval 0 di points to exact matched key
6885 * \retval -ve failure
6888 static int osd_it_iam_load(const struct lu_env *env,
6889 const struct dt_it *di, __u64 hash)
6891 struct osd_it_iam *it = (struct osd_it_iam *)di;
6893 return iam_it_load(&it->oi_it, hash);
6896 static const struct dt_index_operations osd_index_iam_ops = {
6897 .dio_lookup = osd_index_iam_lookup,
6898 .dio_declare_insert = osd_index_declare_iam_insert,
6899 .dio_insert = osd_index_iam_insert,
6900 .dio_declare_delete = osd_index_declare_iam_delete,
6901 .dio_delete = osd_index_iam_delete,
6903 .init = osd_it_iam_init,
6904 .fini = osd_it_iam_fini,
6905 .get = osd_it_iam_get,
6906 .put = osd_it_iam_put,
6907 .next = osd_it_iam_next,
6908 .key = osd_it_iam_key,
6909 .key_size = osd_it_iam_key_size,
6910 .rec = osd_it_iam_rec,
6911 .store = osd_it_iam_store,
6912 .load = osd_it_iam_load
6916 struct osd_it_ea *osd_it_dir_init(const struct lu_env *env,
6917 struct osd_device *dev,
6918 struct inode *inode, u32 attr)
6920 struct osd_thread_info *info = osd_oti_get(env);
6921 struct osd_it_ea *oie;
6925 file = alloc_file_pseudo(inode, dev->od_mnt, "/", O_NOATIME,
6928 RETURN(ERR_CAST(file));
6930 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
6931 if (attr & LUDA_64BITHASH)
6932 file->f_mode |= FMODE_64BITHASH;
6934 file->f_mode |= FMODE_32BITHASH;
6937 OBD_SLAB_ALLOC_PTR(oie, osd_itea_cachep);
6941 oie->oie_rd_dirent = 0;
6942 oie->oie_it_dirent = 0;
6943 oie->oie_dirent = NULL;
6944 if (unlikely(!info->oti_it_ea_buf_used)) {
6945 oie->oie_buf = info->oti_it_ea_buf;
6946 info->oti_it_ea_buf_used = 1;
6948 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
6952 oie->oie_obj = NULL;
6953 oie->oie_file = file;
6958 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
6962 return ERR_PTR(-ENOMEM);
6966 * Creates or initializes iterator context.
6968 * \retval struct osd_it_ea, iterator structure on success
6971 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
6972 struct dt_object *dt,
6975 struct osd_object *obj = osd_dt_obj(dt);
6976 struct osd_device *dev = osd_obj2dev(obj);
6977 struct lu_object *lo = &dt->do_lu;
6978 struct osd_it_ea *oie;
6982 if (!dt_object_exists(dt) || obj->oo_destroyed)
6983 RETURN(ERR_PTR(-ENOENT));
6985 oie = osd_it_dir_init(env, dev, obj->oo_inode, attr);
6987 RETURN(ERR_CAST(oie));
6991 RETURN((struct dt_it *)oie);
6994 void osd_it_dir_fini(const struct lu_env *env, struct osd_it_ea *oie,
6995 struct inode *inode)
6997 struct osd_thread_info *info = osd_oti_get(env);
7000 fput(oie->oie_file);
7001 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
7002 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
7004 info->oti_it_ea_buf_used = 0;
7005 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
7010 * Destroy or finishes iterator context.
7012 * \param di iterator structure to be destroyed
7014 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
7016 struct osd_it_ea *oie = (struct osd_it_ea *)di;
7017 struct osd_object *obj = oie->oie_obj;
7018 struct inode *inode = obj->oo_inode;
7021 osd_it_dir_fini(env, (struct osd_it_ea *)di, inode);
7022 osd_object_put(env, obj);
7027 * It position the iterator at given key, so that next lookup continues from
7028 * that key Or it is similar to dio_it->load() but based on a key,
7029 * rather than file position.
7031 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
7034 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
7036 static int osd_it_ea_get(const struct lu_env *env,
7037 struct dt_it *di, const struct dt_key *key)
7039 struct osd_it_ea *it = (struct osd_it_ea *)di;
7042 LASSERT(((const char *)key)[0] == '\0');
7043 it->oie_file->f_pos = 0;
7044 it->oie_rd_dirent = 0;
7045 it->oie_it_dirent = 0;
7046 it->oie_dirent = NULL;
7054 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
7058 struct osd_filldir_cbs {
7059 struct dir_context ctx;
7060 struct osd_it_ea *it;
7063 * It is called internally by ->iterate*(). It fills the
7064 * iterator's in-memory data structure with required
7065 * information i.e. name, namelen, rec_size etc.
7067 * \param buf in which information to be filled in.
7068 * \param name name of the file in given dir
7070 * \retval 0 on success
7071 * \retval 1 on buffer full
7073 #ifdef HAVE_FILLDIR_USE_CTX
7074 static FILLDIR_TYPE do_osd_ldiskfs_filldir(struct dir_context *ctx,
7076 static int osd_ldiskfs_filldir(void *ctx,
7078 const char *name, int namelen,
7079 loff_t offset, __u64 ino, unsigned int d_type)
7081 struct osd_it_ea *it = ((struct osd_filldir_cbs *)ctx)->it;
7082 struct osd_object *obj = it->oie_obj;
7083 struct osd_it_ea_dirent *ent = it->oie_dirent;
7084 struct lu_fid *fid = &ent->oied_fid;
7085 char *buf = it->oie_buf;
7086 struct osd_fid_pack *rec;
7089 /* this should never happen */
7090 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
7091 CERROR("ldiskfs return invalid namelen %d\n", namelen);
7095 /* Check for enough space. Note oied_name is not NUL terminated. */
7096 if (&ent->oied_name[namelen] > buf + OSD_IT_EA_BUFSIZE)
7099 /* "." is just the object itself. */
7100 if (namelen == 1 && name[0] == '.') {
7102 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
7103 } else if (d_type & LDISKFS_DIRENT_LUFID) {
7104 rec = (struct osd_fid_pack *)(name + namelen + 1);
7105 if (osd_fid_unpack(fid, rec) != 0)
7110 d_type &= ~LDISKFS_DIRENT_LUFID;
7112 /* NOT export local root. */
7114 unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
7115 ino = obj->oo_inode->i_ino;
7116 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
7119 if (obj == NULL || !(obj->oo_lma_flags & LUSTRE_ENCRYPT_FL)) {
7120 ent->oied_namelen = namelen;
7121 memcpy(ent->oied_name, name, namelen);
7123 int encoded_namelen = critical_chars(name, namelen);
7125 /* Check again for enough space. */
7126 if (&ent->oied_name[encoded_namelen] > buf + OSD_IT_EA_BUFSIZE)
7129 ent->oied_namelen = encoded_namelen;
7131 if (encoded_namelen == namelen)
7132 memcpy(ent->oied_name, name, namelen);
7134 critical_encode(name, namelen, ent->oied_name);
7137 ent->oied_ino = ino;
7138 ent->oied_off = offset;
7139 ent->oied_type = d_type;
7141 it->oie_rd_dirent++;
7142 it->oie_dirent = (void *)ent + round_up(sizeof(*ent) + ent->oied_namelen, 8);
7146 WRAP_FILLDIR_FN(do_, osd_ldiskfs_filldir)
7149 * Calls ->iterate*() to load a directory entry at a time
7150 * and stored it in iterator's in-memory data structure.
7152 * \param di iterator's in memory structure
7154 * \retval 0 on success
7155 * \retval -ve on error
7156 * \retval +1 reach the end of entry
7158 int osd_ldiskfs_it_fill(const struct lu_env *env, const struct dt_it *di)
7160 struct osd_it_ea *it = (struct osd_it_ea *)di;
7161 struct osd_object *obj = it->oie_obj;
7162 struct htree_lock *hlock = NULL;
7163 struct file *filp = it->oie_file;
7165 struct osd_filldir_cbs buf = {
7166 .ctx.actor = osd_ldiskfs_filldir,
7171 it->oie_dirent = it->oie_buf;
7172 it->oie_rd_dirent = 0;
7175 if (obj->oo_hl_head != NULL) {
7176 hlock = osd_oti_get(env)->oti_hlock;
7177 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
7179 LDISKFS_HLOCK_READDIR);
7181 down_read(&obj->oo_ext_idx_sem);
7185 rc = iterate_dir(filp, &buf.ctx);
7189 if (it->oie_rd_dirent == 0) {
7191 * If it does not get any dirent, it means it has been reached
7192 * to the end of the dir
7194 it->oie_file->f_pos = ldiskfs_get_htree_eof(it->oie_file);
7198 it->oie_dirent = it->oie_buf;
7199 it->oie_it_dirent = 1;
7204 ldiskfs_htree_unlock(hlock);
7206 up_read(&obj->oo_ext_idx_sem);
7213 * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
7214 * to load a directory entry at a time and stored it in
7215 * iterator's in-memory data structure.
7217 * \param di iterator's in memory structure
7219 * \retval +ve iterator reached to end
7220 * \retval 0 iterator not reached to end
7221 * \retval -ve on error
7223 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
7225 struct osd_it_ea *it = (struct osd_it_ea *)di;
7230 if (it->oie_it_dirent < it->oie_rd_dirent) {
7232 (void *)it->oie_dirent +
7233 round_up(sizeof(struct osd_it_ea_dirent) +
7234 it->oie_dirent->oied_namelen, 8);
7235 it->oie_it_dirent++;
7238 if (it->oie_file->f_pos == ldiskfs_get_htree_eof(it->oie_file))
7241 rc = osd_ldiskfs_it_fill(env, di);
7248 * Returns the key at current position from iterator's in memory structure.
7250 * \param di iterator's in memory structure
7252 * \retval key i.e. struct dt_key on success
7254 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
7255 const struct dt_it *di)
7257 struct osd_it_ea *it = (struct osd_it_ea *)di;
7259 return (struct dt_key *)it->oie_dirent->oied_name;
7263 * Returns key's size at current position from iterator's in memory structure.
7265 * \param di iterator's in memory structure
7267 * \retval key_size i.e. struct dt_key on success
7269 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
7271 struct osd_it_ea *it = (struct osd_it_ea *)di;
7273 return it->oie_dirent->oied_namelen;
7276 #if defined LDISKFS_DIR_ENTRY_LEN && defined LDISKFS_DIR_ENTRY_LEN_
7277 #undef LDISKFS_DIR_REC_LEN
7278 # if defined LDISKFS_DIR_REC_LEN_WITH_DIR
7279 # define LDISKFS_DIR_REC_LEN(de, dir) LDISKFS_DIR_ENTRY_LEN_((de), (dir))
7281 # define LDISKFS_DIR_REC_LEN(de) LDISKFS_DIR_ENTRY_LEN_((de))
7285 #if defined LDISKFS_DIR_REC_LEN_WITH_DIR
7286 # define LDISKFS_DIR_REC_LEN_DIR(de) LDISKFS_DIR_REC_LEN((de), NULL)
7288 # define LDISKFS_DIR_REC_LEN_DIR(de) LDISKFS_DIR_REC_LEN((de))
7291 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
7293 if (LDISKFS_DIR_REC_LEN_DIR(de) >=
7294 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
7301 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
7302 unsigned int blocksize, bool dotdot)
7305 return osd_dotdot_has_space(de);
7307 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
7308 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
7315 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
7316 handle_t *jh, struct dentry *dentry,
7317 const struct lu_fid *fid, struct buffer_head *bh,
7318 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
7321 struct inode *dir = dentry->d_parent->d_inode;
7322 struct inode *inode = dentry->d_inode;
7323 struct osd_fid_pack *rec;
7324 struct ldiskfs_dentry_param *ldp;
7325 int namelen = dentry->d_name.len;
7327 struct osd_thread_info *info = osd_oti_get(env);
7331 if (!ldiskfs_has_feature_dirdata(inode->i_sb))
7334 /* There is enough space to hold the FID-in-dirent. */
7335 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
7336 rc = osd_ldiskfs_journal_get_write_access(jh, dir->i_sb, bh,
7341 de->name[namelen] = 0;
7342 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
7343 rec->fp_len = sizeof(struct lu_fid) + 1;
7344 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
7345 de->file_type |= LDISKFS_DIRENT_LUFID;
7346 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
7353 rc = ldiskfs_delete_entry(jh, dir, de, bh);
7357 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
7358 osd_get_ldiskfs_dirent_param(ldp, fid);
7359 dentry->d_fsdata = (void *)ldp;
7360 dquot_initialize(dir);
7361 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
7363 * It is too bad, we cannot reinsert the name entry back.
7364 * That means we lose it!
7368 "%s: fail to reinsert the dirent, dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
7369 osd_ino2name(inode), dir->i_ino, dir->i_generation,
7370 namelen, dentry->d_name.name, PFID(fid), rc);
7376 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
7377 struct osd_it_ea *it, struct lu_fid *fid,
7378 struct osd_inode_id *id, __u32 *attr)
7380 struct osd_thread_info *info = osd_oti_get(env);
7381 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
7382 struct osd_device *dev = osd_obj2dev(obj);
7383 struct super_block *sb = osd_sb(dev);
7384 const char *devname = osd_name(dev);
7385 struct osd_it_ea_dirent *ent = it->oie_dirent;
7386 struct inode *dir = obj->oo_inode;
7387 struct htree_lock *hlock = NULL;
7388 struct buffer_head *bh = NULL;
7389 handle_t *jh = NULL;
7390 struct ldiskfs_dir_entry_2 *de;
7391 struct dentry *dentry;
7392 struct inode *inode;
7393 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
7396 bool dotdot = false;
7402 if (ent->oied_name[0] == '.') {
7403 if (ent->oied_namelen == 1)
7406 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
7410 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
7411 inode = osd_iget(info, dev, id);
7412 if (IS_ERR(inode)) {
7413 rc = PTR_ERR(inode);
7414 if (rc == -ENOENT || rc == -ESTALE) {
7416 * Maybe dangling name entry, or
7417 * corrupted directory entry.
7419 *attr |= LUDA_UNKNOWN;
7422 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
7423 "check_repair, dir = %lu/%u, name = %.*s, "
7424 "ino = %llu, rc = %d\n",
7425 devname, dir->i_ino, dir->i_generation,
7426 ent->oied_namelen, ent->oied_name,
7433 rc = obj_name2lu_name(obj, ent->oied_name, ent->oied_namelen, &ln);
7437 dentry = osd_child_dentry_by_inode(env, dir, ln.ln_name, ln.ln_namelen);
7438 rc = osd_get_lma(info, inode, dentry, &info->oti_ost_attrs);
7439 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
7445 * We need to ensure that the name entry is still valid.
7446 * Because it may be removed or renamed by other already.
7448 * The unlink or rename operation will start journal before PDO lock,
7449 * so to avoid deadlock, here we need to start journal handle before
7450 * related PDO lock also. But because we do not know whether there
7451 * will be something to be repaired before PDO lock, we just start
7452 * journal without conditions.
7454 * We may need to remove the name entry firstly, then insert back.
7455 * One credit is for user quota file update.
7456 * One credit is for group quota file update.
7457 * Two credits are for dirty inode.
7459 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
7460 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
7462 if (dev->od_dirent_journal != 0) {
7465 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
7468 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
7469 "check_repair, dir = %lu/%u, credits = %d, "
7470 "name = %.*s, ino = %llu: rc = %d\n",
7471 devname, dir->i_ino, dir->i_generation, credits,
7472 ent->oied_namelen, ent->oied_name,
7475 GOTO(out_inode, rc);
7478 if (obj->oo_hl_head != NULL) {
7479 hlock = osd_oti_get(env)->oti_hlock;
7481 * "0" means exclusive lock for the whole directory.
7482 * We need to prevent others access such name entry
7483 * during the delete + insert. Neither HLOCK_ADD nor
7484 * HLOCK_DEL cannot guarantee the atomicity.
7486 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
7488 down_write(&obj->oo_ext_idx_sem);
7491 if (obj->oo_hl_head != NULL) {
7492 hlock = osd_oti_get(env)->oti_hlock;
7493 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
7494 LDISKFS_HLOCK_LOOKUP);
7496 down_read(&obj->oo_ext_idx_sem);
7500 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
7501 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
7502 *attr |= LUDA_IGNORE;
7508 * For dotdot entry, if there is not enough space to hold the
7509 * FID-in-dirent, just keep them there. It only happens when the
7510 * device upgraded from 1.8 or restored from MDT file-level backup.
7511 * For the whole directory, only dotdot entry have no FID-in-dirent
7512 * and needs to get FID from LMA when readdir, it will not affect the
7515 if (dotdot && !osd_dotdot_has_space(de)) {
7516 *attr |= LUDA_UNKNOWN;
7522 if (lu_fid_eq(fid, &lma->lma_self_fid))
7525 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
7526 struct lu_fid *tfid = &lma->lma_self_fid;
7528 if (likely(dotdot &&
7529 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
7530 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
7532 * It must be REMOTE_PARENT_DIR and as the
7533 * 'dotdot' entry of remote directory
7535 *attr |= LUDA_IGNORE;
7537 CDEBUG(D_LFSCK, "%s: expect remote agent "
7538 "parent directory, but got %.*s under "
7539 "dir = %lu/%u with the FID "DFID"\n",
7540 devname, ent->oied_namelen,
7541 ent->oied_name, dir->i_ino,
7542 dir->i_generation, PFID(tfid));
7544 *attr |= LUDA_UNKNOWN;
7551 if (!fid_is_zero(fid)) {
7552 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
7554 if (rc == -ENOENT ||
7556 !(dev->od_scrub.os_scrub.os_file.sf_flags & SF_UPGRADE))) {
7558 * linkEA does not recognize the dirent entry,
7559 * it may because the dirent entry corruption
7560 * and points to other's inode.
7562 CDEBUG(D_LFSCK, "%s: the target inode does not "
7563 "recognize the dirent, dir = %lu/%u, "
7564 " name = %.*s, ino = %llu, "
7565 DFID": rc = %d\n", devname, dir->i_ino,
7566 dir->i_generation, ent->oied_namelen,
7567 ent->oied_name, ent->oied_ino, PFID(fid), rc);
7568 *attr |= LUDA_UNKNOWN;
7573 if (rc && rc != -ENODATA) {
7574 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
7575 "dir = %lu/%u, name = %.*s, ino = %llu, "
7576 DFID": rc = %d\n", devname, dir->i_ino,
7577 dir->i_generation, ent->oied_namelen,
7578 ent->oied_name, ent->oied_ino, PFID(fid), rc);
7579 *attr |= LUDA_UNKNOWN;
7587 * linkEA recognizes the dirent entry, the FID-in-LMA is
7588 * valid, trusted, in spite of fid_is_sane(fid) or not.
7590 if (*attr & LUDA_VERIFY_DRYRUN) {
7591 *fid = lma->lma_self_fid;
7592 *attr |= LUDA_REPAIR;
7599 dev->od_dirent_journal = 1;
7600 if (hlock != NULL) {
7601 ldiskfs_htree_unlock(hlock);
7604 up_read(&obj->oo_ext_idx_sem);
7610 *fid = lma->lma_self_fid;
7612 /* Update or append the FID-in-dirent. */
7613 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
7614 bh, de, hlock, dotdot);
7616 *attr |= LUDA_REPAIR;
7618 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
7619 "the dirent, dir = %lu/%u, name = %.*s, "
7620 "ino = %llu, "DFID": rc = %d\n",
7621 devname, dir->i_ino, dir->i_generation,
7622 ent->oied_namelen, ent->oied_name,
7623 ent->oied_ino, PFID(fid), rc);
7625 /* lma is NULL, trust the FID-in-dirent if it is valid. */
7626 if (*attr & LUDA_VERIFY_DRYRUN) {
7627 if (fid_is_sane(fid)) {
7628 *attr |= LUDA_REPAIR;
7629 } else if (dev->od_index == 0) {
7630 lu_igif_build(fid, inode->i_ino,
7631 inode->i_generation);
7632 *attr |= LUDA_UPGRADE;
7640 dev->od_dirent_journal = 1;
7641 if (hlock != NULL) {
7642 ldiskfs_htree_unlock(hlock);
7645 up_read(&obj->oo_ext_idx_sem);
7652 if (unlikely(fid_is_sane(fid))) {
7654 * FID-in-dirent exists, but FID-in-LMA is lost.
7655 * Trust the FID-in-dirent, and add FID-in-LMA.
7657 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
7659 *attr |= LUDA_REPAIR;
7661 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
7662 "update dirent, dir = %lu/%u, "
7663 "name = %.*s, ino = %llu, "
7665 devname, dir->i_ino, dir->i_generation,
7666 ent->oied_namelen, ent->oied_name,
7667 ent->oied_ino, PFID(fid), rc);
7668 } else if (dev->od_index == 0) {
7669 lu_igif_build(fid, inode->i_ino, inode->i_generation);
7671 * It is probably IGIF object. Only aappend the
7672 * FID-in-dirent. OI scrub will process FID-in-LMA.
7674 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
7675 bh, de, hlock, dotdot);
7677 *attr |= LUDA_UPGRADE;
7679 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
7680 "after the dirent, dir = %lu/%u, "
7681 "name = %.*s, ino = %llu, "
7683 devname, dir->i_ino, dir->i_generation,
7684 ent->oied_namelen, ent->oied_name,
7685 ent->oied_ino, PFID(fid), rc);
7694 if (hlock != NULL) {
7695 ldiskfs_htree_unlock(hlock);
7697 if (dev->od_dirent_journal != 0)
7698 up_write(&obj->oo_ext_idx_sem);
7700 up_read(&obj->oo_ext_idx_sem);
7704 ldiskfs_journal_stop(jh);
7708 if (rc >= 0 && !dirty)
7709 dev->od_dirent_journal = 0;
7710 if (ln.ln_name != ent->oied_name)
7717 * Returns the value at current position from iterator's in memory structure.
7719 * \param di struct osd_it_ea, iterator's in memory structure
7720 * \param attr attr requested for dirent.
7721 * \param lde lustre dirent
7723 * \retval 0 no error and \param lde has correct lustre dirent.
7724 * \retval -ve on error
7726 static inline int osd_it_ea_rec(const struct lu_env *env,
7727 const struct dt_it *di,
7728 struct dt_rec *dtrec, __u32 attr)
7730 struct osd_it_ea *it = (struct osd_it_ea *)di;
7731 struct osd_object *obj = it->oie_obj;
7732 struct osd_device *dev = osd_obj2dev(obj);
7733 struct osd_thread_info *oti = osd_oti_get(env);
7734 struct osd_inode_id *id = &oti->oti_id;
7735 struct lu_fid *fid = &it->oie_dirent->oied_fid;
7736 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
7737 __u32 ino = it->oie_dirent->oied_ino;
7742 LASSERT(!is_remote_parent_ino(dev, obj->oo_inode->i_ino));
7744 if (attr & LUDA_VERIFY) {
7745 if (unlikely(is_remote_parent_ino(dev, ino))) {
7746 attr |= LUDA_IGNORE;
7748 * If the parent is on remote MDT, and there
7749 * is no FID-in-dirent, then we have to get
7750 * the parent FID from the linkEA.
7752 if (!fid_is_sane(fid) &&
7753 it->oie_dirent->oied_namelen == 2 &&
7754 it->oie_dirent->oied_name[0] == '.' &&
7755 it->oie_dirent->oied_name[1] == '.')
7756 osd_get_pfid_from_linkea(env, obj, fid);
7758 rc = osd_dirent_check_repair(env, obj, it, fid, id,
7762 if (!fid_is_sane(fid))
7763 attr |= LUDA_UNKNOWN;
7765 attr &= ~LU_DIRENT_ATTRS_MASK;
7766 if (!fid_is_sane(fid)) {
7767 bool is_dotdot = false;
7769 if (it->oie_dirent->oied_namelen == 2 &&
7770 it->oie_dirent->oied_name[0] == '.' &&
7771 it->oie_dirent->oied_name[1] == '.')
7774 * If the parent is on remote MDT, and there
7775 * is no FID-in-dirent, then we have to get
7776 * the parent FID from the linkEA.
7778 if (is_remote_parent_ino(dev, ino) && is_dotdot) {
7779 rc = osd_get_pfid_from_linkea(env, obj, fid);
7781 if (is_dotdot == false &&
7782 CFS_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
7785 rc = osd_ea_fid_get(env, obj, ino, fid, id);
7790 /* Pack the entry anyway, at least the offset is right. */
7791 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
7792 it->oie_dirent->oied_name,
7793 it->oie_dirent->oied_namelen,
7794 it->oie_dirent->oied_type, attr);
7796 RETURN(rc > 0 ? 0 : rc);
7800 * Returns the record size size at current position.
7802 * This function will return record(lu_dirent) size in bytes.
7804 * \param[in] env execution environment
7805 * \param[in] di iterator's in memory structure
7806 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
7807 * calculate the lu_dirent size.
7809 * \retval record size(in bytes & in memory) of the current lu_dirent
7812 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
7815 struct osd_it_ea *it = (struct osd_it_ea *)di;
7817 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
7821 * Returns a cookie for current position of the iterator head, so that
7822 * user can use this cookie to load/start the iterator next time.
7824 * \param di iterator's in memory structure
7826 * \retval cookie for current position, on success
7828 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
7830 struct osd_it_ea *it = (struct osd_it_ea *)di;
7832 return it->oie_dirent->oied_off;
7836 * It calls osd_ldiskfs_it_fill() which will use ->iterate*()
7837 * to load a directory entry at a time and stored it i inn,
7838 * in iterator's in-memory data structure.
7840 * \param di struct osd_it_ea, iterator's in memory structure
7842 * \retval +ve on success
7843 * \retval -ve on error
7845 static int osd_it_ea_load(const struct lu_env *env,
7846 const struct dt_it *di, __u64 hash)
7848 struct osd_it_ea *it = (struct osd_it_ea *)di;
7852 it->oie_file->f_pos = hash;
7854 rc = osd_ldiskfs_it_fill(env, di);
7864 int osd_olc_lookup(const struct lu_env *env, struct osd_object *obj,
7865 u64 iversion, struct dt_rec *rec,
7866 const struct lu_name *ln, int *result)
7868 struct osd_thread_info *oti = osd_oti_get(env);
7869 struct osd_lookup_cache *olc = oti->oti_lookup_cache;
7870 struct osd_device *osd = osd_obj2dev(obj);
7871 struct osd_lookup_cache_object *cobj = &oti->oti_cobj;
7874 if (unlikely(olc == NULL))
7877 if (unlikely(atomic_read(&osd_mount_seq) != olc->olc_mount_seq)) {
7879 * umount has happened, a new OSD could land to the previous
7880 * address so we can't use it any more, invalidate our cache
7882 memset(olc, 0, sizeof(*olc));
7883 olc->olc_mount_seq = atomic_read(&osd_mount_seq);
7887 memset(cobj, 0, sizeof(*cobj));
7888 cobj->lco_osd = osd;
7889 cobj->lco_ino = obj->oo_inode->i_ino;
7890 cobj->lco_gen = obj->oo_inode->i_generation;
7891 cobj->lco_version = iversion;
7893 for (i = 0; i < OSD_LOOKUP_CACHE_MAX; i++) {
7894 struct osd_lookup_cache_entry *entry;
7896 entry = &olc->olc_entry[i];
7897 /* compare if osd/ino/generation/version match */
7898 if (memcmp(&entry->lce_obj, cobj, sizeof(*cobj)) != 0)
7900 if (entry->lce_namelen != ln->ln_namelen)
7902 if (memcmp(entry->lce_name, ln->ln_name, ln->ln_namelen) != 0)
7905 memcpy(rec, &entry->lce_fid, sizeof(entry->lce_fid));
7906 *result = entry->lce_rc;
7912 void osd_olc_save(const struct lu_env *env, struct osd_object *obj,
7913 struct dt_rec *rec, const struct lu_name *ln,
7914 const int result, u64 iversion)
7916 struct osd_thread_info *oti = osd_oti_get(env);
7917 struct osd_lookup_cache_entry *entry;
7918 struct osd_lookup_cache *olc;
7920 if (unlikely(oti->oti_lookup_cache == NULL)) {
7921 OBD_ALLOC_PTR(oti->oti_lookup_cache);
7922 if (oti->oti_lookup_cache == NULL)
7926 olc = oti->oti_lookup_cache;
7927 if (unlikely(atomic_read(&osd_mount_seq) != olc->olc_mount_seq)) {
7928 memset(olc, 0, sizeof(*olc));
7929 olc->olc_mount_seq = atomic_read(&osd_mount_seq);
7932 entry = &olc->olc_entry[olc->olc_cur];
7934 /* invaliate cache slot if needed */
7935 if (entry->lce_obj.lco_osd)
7936 memset(&entry->lce_obj, 0, sizeof(entry->lce_obj));
7938 /* XXX: some kind of LRU */
7939 entry->lce_obj.lco_osd = osd_obj2dev(obj);
7940 entry->lce_obj.lco_ino = obj->oo_inode->i_ino;
7941 entry->lce_obj.lco_gen = obj->oo_inode->i_generation;
7942 entry->lce_obj.lco_version = iversion;
7944 LASSERT(ln->ln_namelen <= LDISKFS_NAME_LEN + 1);
7945 entry->lce_namelen = ln->ln_namelen;
7946 memcpy(entry->lce_name, ln->ln_name, ln->ln_namelen);
7947 memcpy(&entry->lce_fid, rec, sizeof(entry->lce_fid));
7948 entry->lce_rc = result;
7950 if (++olc->olc_cur == OSD_LOOKUP_CACHE_MAX)
7955 * Index lookup function for interoperability mode (b11826).
7957 * \param key, key i.e. file name to be searched
7959 * \retval +ve, on success
7960 * \retval -ve, on error
7962 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
7963 struct dt_rec *rec, const struct dt_key *key)
7965 struct osd_object *obj = osd_dt_obj(dt);
7972 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
7973 LINVRNT(osd_invariant(obj));
7975 rc = obj_name2lu_name(obj, (char *)key, strlen((char *)key), &ln);
7980 * grab version before actual lookup, so that we recognize potential
7981 * insert between osd_ea_lookup_rec() and osd_olc_save()
7983 iversion = inode_peek_iversion(obj->oo_inode);
7985 if (osd_olc_lookup(env, obj, iversion, rec, &ln, &result))
7986 GOTO(out, rc = result);
7988 rc = osd_ea_lookup_rec(env, obj, rec, &ln);
7992 osd_olc_save(env, obj, rec, &ln, rc, iversion);
7995 if (ln.ln_name != (char *)key)
8001 * Index and Iterator operations for interoperability
8002 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
8004 static const struct dt_index_operations osd_index_ea_ops = {
8005 .dio_lookup = osd_index_ea_lookup,
8006 .dio_declare_insert = osd_index_declare_ea_insert,
8007 .dio_insert = osd_index_ea_insert,
8008 .dio_declare_delete = osd_index_declare_ea_delete,
8009 .dio_delete = osd_index_ea_delete,
8011 .init = osd_it_ea_init,
8012 .fini = osd_it_ea_fini,
8013 .get = osd_it_ea_get,
8014 .put = osd_it_ea_put,
8015 .next = osd_it_ea_next,
8016 .key = osd_it_ea_key,
8017 .key_size = osd_it_ea_key_size,
8018 .rec = osd_it_ea_rec,
8019 .rec_size = osd_it_ea_rec_size,
8020 .store = osd_it_ea_store,
8021 .load = osd_it_ea_load
8025 static void *osd_key_init(const struct lu_context *ctx,
8026 struct lu_context_key *key)
8028 struct osd_thread_info *info;
8030 OBD_ALLOC_PTR(info);
8032 return ERR_PTR(-ENOMEM);
8034 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
8035 if (info->oti_it_ea_buf == NULL)
8038 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
8040 info->oti_hlock = ldiskfs_htree_lock_alloc();
8041 if (info->oti_hlock == NULL)
8047 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
8050 return ERR_PTR(-ENOMEM);
8053 static void osd_key_fini(const struct lu_context *ctx,
8054 struct lu_context_key *key, void *data)
8056 struct osd_thread_info *info = data;
8057 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
8058 struct osd_idmap_cache *idc = info->oti_ins_cache;
8060 if (info->oti_dio_pages) {
8062 for (i = 0; i < PTLRPC_MAX_BRW_PAGES; i++) {
8063 struct page *page = info->oti_dio_pages[i];
8065 LASSERT(PagePrivate2(page));
8066 LASSERT(PageLocked(page));
8067 ClearPagePrivate2(page);
8072 OBD_FREE_PTR_ARRAY_LARGE(info->oti_dio_pages,
8073 PTLRPC_MAX_BRW_PAGES);
8076 if (info->oti_inode != NULL)
8078 if (info->oti_hlock != NULL)
8079 ldiskfs_htree_lock_free(info->oti_hlock);
8080 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
8081 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
8082 lu_buf_free(&info->oti_iobuf.dr_lnb_buf);
8083 lu_buf_free(&info->oti_big_buf);
8085 LASSERT(info->oti_ins_cache_size > 0);
8086 OBD_FREE_PTR_ARRAY_LARGE(idc, info->oti_ins_cache_size);
8087 info->oti_ins_cache = NULL;
8088 info->oti_ins_cache_size = 0;
8090 if (info->oti_lookup_cache)
8091 OBD_FREE_PTR(info->oti_lookup_cache);
8095 static void osd_key_exit(const struct lu_context *ctx,
8096 struct lu_context_key *key, void *data)
8098 struct osd_thread_info *info = data;
8099 struct osd_lookup_cache *olc = info->oti_lookup_cache;
8102 memset(olc, 0, sizeof(*olc));
8103 LASSERT(info->oti_r_locks == 0);
8104 LASSERT(info->oti_w_locks == 0);
8105 LASSERT(info->oti_txns == 0);
8106 LASSERTF(info->oti_dio_pages_used == 0, "%d\n",
8107 info->oti_dio_pages_used);
8110 /* type constructor/destructor: osd_type_init, osd_type_fini */
8111 LU_TYPE_INIT_FINI(osd, &osd_key);
8113 struct lu_context_key osd_key = {
8114 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
8115 .lct_init = osd_key_init,
8116 .lct_fini = osd_key_fini,
8117 .lct_exit = osd_key_exit
8121 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
8122 const char *name, struct lu_device *next)
8124 struct osd_device *osd = osd_dev(d);
8126 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname)) >=
8127 sizeof(osd->od_svname))
8129 return osd_procfs_init(osd, name);
8132 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
8134 struct seq_server_site *ss = osd_seq_site(osd);
8139 if (osd->od_is_ost || osd->od_cl_seq != NULL)
8142 if (unlikely(ss == NULL))
8145 OBD_ALLOC_PTR(osd->od_cl_seq);
8146 if (osd->od_cl_seq == NULL)
8149 seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
8150 osd->od_svname, ss->ss_server_seq);
8152 if (ss->ss_node_id == 0) {
8154 * If the OSD on the sequence controller(MDT0), then allocate
8155 * sequence here, otherwise allocate sequence after connected
8156 * to MDT0 (see mdt_register_lwp_callback()).
8158 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
8159 &osd->od_cl_seq->lcs_space, env);
8165 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
8167 if (osd->od_cl_seq == NULL)
8170 seq_client_fini(osd->od_cl_seq);
8171 OBD_FREE_PTR(osd->od_cl_seq);
8172 osd->od_cl_seq = NULL;
8175 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
8179 /* shutdown quota slave instance associated with the device */
8180 if (o->od_quota_slave_md != NULL) {
8181 struct qsd_instance *qsd = o->od_quota_slave_md;
8183 o->od_quota_slave_md = NULL;
8187 if (o->od_quota_slave_dt != NULL) {
8188 struct qsd_instance *qsd = o->od_quota_slave_dt;
8190 o->od_quota_slave_dt = NULL;
8194 osd_fid_fini(env, o);
8195 osd_scrub_cleanup(env, o);
8200 #ifdef HAVE_FLUSH_DELAYED_FPUT
8201 # define cfs_flush_delayed_fput() flush_delayed_fput()
8203 void (*cfs_flush_delayed_fput)(void);
8204 #endif /* HAVE_FLUSH_DELAYED_FPUT */
8206 static void osd_umount(const struct lu_env *env, struct osd_device *o)
8210 atomic_inc(&osd_mount_seq);
8212 if (o->od_mnt != NULL) {
8213 shrink_dcache_sb(osd_sb(o));
8214 osd_sync(env, &o->od_dt_dev);
8215 wait_event(o->od_commit_cb_done,
8216 !atomic_read(&o->od_commit_cb_in_flight));
8222 /* to be sure all delayed fput are finished */
8223 cfs_flush_delayed_fput();
8228 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
8229 # ifndef LDISKFS_HAS_INCOMPAT_FEATURE
8230 /* Newer kernels provide the ldiskfs_set_feature_largedir() wrapper already,
8231 * which calls ldiskfs_update_dynamic_rev() to update ancient filesystems.
8232 * All ldiskfs filesystems are already v2, so it is a no-op and unnecessary.
8233 * This avoids maintaining patches to export this otherwise-useless function.
8235 void ldiskfs_update_dynamic_rev(struct super_block *sb)
8242 static int osd_mount(const struct lu_env *env,
8243 struct osd_device *o, struct lustre_cfg *cfg)
8245 const char *name = lustre_cfg_string(cfg, 0);
8246 const char *dev = lustre_cfg_string(cfg, 1);
8248 unsigned long page, s_flags = 0, lmd_flags = 0;
8249 struct page *__page;
8250 struct file_system_type *type;
8251 char *options = NULL;
8253 struct osd_thread_info *info = osd_oti_get(env);
8254 struct lu_fid *fid = &info->oti_fid;
8255 struct inode *inode;
8256 int rc = 0, force_over_1024tb = 0;
8260 if (o->od_mnt != NULL)
8263 if (strlen(dev) >= sizeof(o->od_mntdev))
8265 strcpy(o->od_mntdev, dev);
8267 str = lustre_cfg_buf(cfg, 2);
8268 sscanf(str, "%lu:%lu", &s_flags, &lmd_flags);
8270 opts = lustre_cfg_string(cfg, 3);
8272 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
8273 CERROR("%s: device %s extents feature is not guaranteed to "
8274 "work on big-endian systems. Use \"bigendian_extents\" "
8275 "mount option to override.\n", name, dev);
8279 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
8280 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
8281 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");
8284 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
8285 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
8286 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");
8289 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
8290 if (opts != NULL && strstr(opts, "force_over_512tb") != NULL) {
8291 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");
8295 if (opts != NULL && strstr(opts, "force_over_1024tb") != NULL)
8296 force_over_1024tb = 1;
8298 __page = alloc_page(GFP_KERNEL);
8300 GOTO(out, rc = -ENOMEM);
8301 page = (unsigned long)page_address(__page);
8302 options = (char *)page;
8305 /* strip out the options for back compatiblity */
8306 static const char * const sout[] = {
8313 /* strip out option we processed in osd */
8314 "bigendian_extents",
8318 "force_over_1024tb",
8322 strncat(options, opts, PAGE_SIZE);
8323 for (rc = 0, str = options; sout[rc]; ) {
8324 char *op = strstr(str, sout[rc]);
8331 if (op == options || *(op - 1) == ',') {
8332 str = op + strlen(sout[rc]);
8333 if (*str == ',' || *str == '\0') {
8334 *str == ',' ? str++ : str;
8335 memmove(op, str, strlen(str) + 1);
8338 for (str = op; *str != ',' && *str != '\0'; str++)
8342 strncat(options, "user_xattr,acl", PAGE_SIZE);
8345 /* Glom up mount options */
8346 if (*options != '\0')
8347 strncat(options, ",", PAGE_SIZE);
8348 strncat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
8350 type = get_fs_type("ldiskfs");
8352 CERROR("%s: cannot find ldiskfs module\n", name);
8353 GOTO(out, rc = -ENODEV);
8356 s_flags |= SB_KERNMOUNT;
8357 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
8358 module_put(type->owner);
8360 if (IS_ERR(o->od_mnt)) {
8361 rc = PTR_ERR(o->od_mnt);
8363 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
8367 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
8368 osd_sb(o)->s_blocksize_bits > 1024ULL << 40 &&
8369 force_over_1024tb == 0) {
8370 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",
8372 GOTO(out_mnt, rc = -EINVAL);
8375 if (test_bit(LMD_FLG_DEV_RDONLY, &lmd_flags)) {
8376 LCONSOLE_WARN("%s: not support dev_rdonly on this device\n",
8379 GOTO(out_mnt, rc = -EOPNOTSUPP);
8382 if (!ldiskfs_has_feature_journal(o->od_mnt->mnt_sb)) {
8383 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
8384 GOTO(out_mnt, rc = -EINVAL);
8387 if (ldiskfs_has_feature_fast_commit(o->od_mnt->mnt_sb)) {
8388 CERROR("%s: device %s is mounted with fast_commit that breaks recovery\n",
8390 GOTO(out_mnt, rc = -EOPNOTSUPP);
8393 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 53, 0)
8394 #ifdef LDISKFS_MOUNT_DIRDATA
8395 if (ldiskfs_has_feature_dirdata(o->od_mnt->mnt_sb))
8396 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
8397 else if (strstr(name, "MDT")) /* don't complain for MGT or OSTs */
8398 CWARN("%s: device %s was upgraded from Lustre-1.x without "
8399 "enabling the dirdata feature. If you do not want to "
8400 "downgrade to Lustre-1.x again, you can enable it via "
8401 "'tune2fs -O dirdata device'\n", name, dev);
8403 /* enable large_dir on MDTs to avoid REMOTE_PARENT_DIR overflow,
8404 * and on very large OSTs to avoid object directory overflow */
8405 if (unlikely(!ldiskfs_has_feature_largedir(o->od_mnt->mnt_sb) &&
8406 !strstr(name, "MGS"))) {
8407 ldiskfs_set_feature_largedir(o->od_mnt->mnt_sb);
8408 LCONSOLE_INFO("%s: enabled 'large_dir' feature on device %s\n",
8412 inode = osd_sb(o)->s_root->d_inode;
8413 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
8414 if (!o->od_dt_dev.dd_rdonly) {
8415 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
8417 CERROR("%s: failed to set lma on %s root inode\n",
8423 if (test_bit(LMD_FLG_NOSCRUB, &lmd_flags))
8424 o->od_scrub.os_scrub.os_auto_scrub_interval = AS_NEVER;
8426 if (blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev))) {
8427 /* do not use pagecache with flash-backed storage */
8428 o->od_writethrough_cache = 0;
8429 o->od_read_cache = 0;
8440 __free_page(__page);
8445 static struct lu_device *osd_device_fini(const struct lu_env *env,
8446 struct lu_device *d)
8448 struct osd_device *o = osd_dev(d);
8452 osd_index_backup(env, o, false);
8453 osd_shutdown(env, o);
8455 if (o->od_oi_table != NULL)
8456 osd_oi_fini(osd_oti_get(env), o);
8457 if (o->od_extent_bytes_percpu)
8458 free_percpu(o->od_extent_bytes_percpu);
8459 osd_obj_map_fini(o);
8465 static int osd_device_init0(const struct lu_env *env,
8466 struct osd_device *o,
8467 struct lustre_cfg *cfg)
8469 struct lu_device *l = osd2lu_dev(o);
8470 struct osd_thread_info *info;
8473 bool restored = false;
8476 /* if the module was re-loaded, env can loose its keys */
8477 rc = lu_env_refill((struct lu_env *)env);
8480 info = osd_oti_get(env);
8483 l->ld_ops = &osd_lu_ops;
8484 o->od_dt_dev.dd_ops = &osd_dt_ops;
8486 spin_lock_init(&o->od_osfs_lock);
8487 mutex_init(&o->od_otable_mutex);
8488 INIT_LIST_HEAD(&o->od_orphan_list);
8489 INIT_LIST_HEAD(&o->od_index_backup_list);
8490 INIT_LIST_HEAD(&o->od_index_restore_list);
8491 spin_lock_init(&o->od_lock);
8492 o->od_index_backup_policy = LIBP_NONE;
8494 init_waitqueue_head(&o->od_commit_cb_done);
8496 o->od_read_cache = 1;
8497 o->od_writethrough_cache = 1;
8498 o->od_enable_projid_xattr = 0;
8499 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
8500 o->od_readcache_max_iosize = OSD_READCACHE_MAX_IO_MB << 20;
8501 o->od_writethrough_max_iosize = OSD_WRITECACHE_MAX_IO_MB << 20;
8502 o->od_scrub.os_scrub.os_auto_scrub_interval = AS_DEFAULT;
8503 /* default fallocate to unwritten extents: LU-14326/LU-14333 */
8504 o->od_fallocate_zero_blocks = 0;
8506 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
8507 sizeof(o->od_svname));
8508 if (cplen >= sizeof(o->od_svname)) {
8513 o->od_index_backup_stop = 0;
8514 o->od_index = -1; /* -1 means index is invalid */
8515 rc = server_name2index(o->od_svname, &o->od_index, NULL);
8516 if (rc == LDD_F_SV_TYPE_OST)
8519 o->od_full_scrub_ratio = OFSR_DEFAULT;
8520 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
8521 rc = osd_mount(env, o, cfg);
8525 /* Can only check block device after mount */
8526 o->od_nonrotational =
8527 blk_queue_nonrot(bdev_get_queue(osd_sb(o)->s_bdev));
8529 rc = osd_obj_map_init(env, o);
8533 rc = lu_site_init(&o->od_site, l);
8535 GOTO(out_compat, rc);
8536 o->od_site.ls_bottom_dev = l;
8538 rc = lu_site_init_finish(&o->od_site);
8542 opts = lustre_cfg_string(cfg, 3);
8543 if (opts && strstr(opts, "resetoi"))
8546 INIT_LIST_HEAD(&o->od_ios_list);
8548 rc = lprocfs_init_brw_stats(&o->od_brw_stats);
8550 GOTO(out_brw_stats, rc);
8552 /* setup scrub, including OI files initialization */
8554 rc = osd_scrub_setup(env, o, restored);
8557 GOTO(out_brw_stats, rc);
8559 rc = osd_procfs_init(o, o->od_svname);
8561 CERROR("%s: can't initialize procfs: rc = %d\n",
8563 GOTO(out_scrub, rc);
8566 LASSERT(l->ld_site->ls_linkage.next != NULL);
8567 LASSERT(l->ld_site->ls_linkage.prev != NULL);
8569 /* initialize quota slave instance */
8570 /* currently it's no need to prepare qsd_instance_md for OST */
8571 if (!o->od_is_ost) {
8572 o->od_quota_slave_md = qsd_init(env, o->od_svname,
8573 &o->od_dt_dev, o->od_proc_entry,
8575 if (IS_ERR(o->od_quota_slave_md)) {
8576 rc = PTR_ERR(o->od_quota_slave_md);
8577 o->od_quota_slave_md = NULL;
8578 GOTO(out_procfs, rc);
8582 o->od_quota_slave_dt = qsd_init(env, o->od_svname, &o->od_dt_dev,
8583 o->od_proc_entry, false, true);
8585 if (IS_ERR(o->od_quota_slave_dt)) {
8586 if (o->od_quota_slave_md != NULL) {
8587 qsd_fini(env, o->od_quota_slave_md);
8588 o->od_quota_slave_md = NULL;
8591 rc = PTR_ERR(o->od_quota_slave_dt);
8592 o->od_quota_slave_dt = NULL;
8593 GOTO(out_procfs, rc);
8596 o->od_extent_bytes_percpu = alloc_percpu(unsigned int);
8597 if (!o->od_extent_bytes_percpu) {
8599 GOTO(out_procfs, rc);
8607 osd_scrub_cleanup(env, o);
8609 lprocfs_fini_brw_stats(&o->od_brw_stats);
8611 lu_site_fini(&o->od_site);
8613 osd_obj_map_fini(o);
8620 static struct lu_device *osd_device_alloc(const struct lu_env *env,
8621 struct lu_device_type *t,
8622 struct lustre_cfg *cfg)
8624 struct osd_device *o;
8629 return ERR_PTR(-ENOMEM);
8631 rc = dt_device_init(&o->od_dt_dev, t);
8634 * Because the ctx might be revived in dt_device_init,
8635 * refill the env here
8637 lu_env_refill((struct lu_env *)env);
8638 rc = osd_device_init0(env, o, cfg);
8640 dt_device_fini(&o->od_dt_dev);
8643 if (unlikely(rc != 0))
8646 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
8649 static struct lu_device *osd_device_free(const struct lu_env *env,
8650 struct lu_device *d)
8652 struct osd_device *o = osd_dev(d);
8656 /* XXX: make osd top device in order to release reference */
8657 d->ld_site->ls_top_dev = d;
8658 lu_site_purge(env, d->ld_site, -1);
8659 lu_site_print(env, d->ld_site, &d->ld_site->ls_obj_hash.nelems,
8660 D_ERROR, lu_cdebug_printer);
8661 lu_site_fini(&o->od_site);
8662 dt_device_fini(&o->od_dt_dev);
8667 static int osd_process_config(const struct lu_env *env,
8668 struct lu_device *d, struct lustre_cfg *cfg)
8670 struct osd_device *o = osd_dev(d);
8676 switch (cfg->lcfg_command) {
8678 rc = osd_mount(env, o, cfg);
8682 * For the case LCFG_PRE_CLEANUP is not called in advance,
8683 * that may happend if hit failure during mount process.
8685 osd_index_backup(env, o, false);
8686 lu_dev_del_linkage(d->ld_site, d);
8687 rc = osd_shutdown(env, o);
8690 LASSERT(&o->od_dt_dev);
8691 count = class_modify_config(cfg, PARAM_OSD,
8692 &o->od_dt_dev.dd_kobj);
8694 count = class_modify_config(cfg, PARAM_OST,
8695 &o->od_dt_dev.dd_kobj);
8696 rc = count > 0 ? 0 : count;
8698 case LCFG_PRE_CLEANUP:
8700 osd_index_backup(env, o,
8701 o->od_index_backup_policy != LIBP_NONE);
8711 static int osd_recovery_complete(const struct lu_env *env,
8712 struct lu_device *d)
8714 struct osd_device *osd = osd_dev(d);
8719 if (osd->od_quota_slave_md == NULL && osd->od_quota_slave_dt == NULL)
8723 * start qsd instance on recovery completion, this notifies the quota
8724 * slave code that we are about to process new requests now
8726 rc = qsd_start(env, osd->od_quota_slave_dt);
8727 if (rc == 0 && osd->od_quota_slave_md != NULL)
8728 rc = qsd_start(env, osd->od_quota_slave_md);
8734 * we use exports to track all osd users
8736 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
8737 struct obd_device *obd, struct obd_uuid *cluuid,
8738 struct obd_connect_data *data, void *localdata)
8740 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8741 struct lustre_handle conn;
8746 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
8748 rc = class_connect(&conn, obd, cluuid);
8752 *exp = class_conn2export(&conn);
8754 spin_lock(&osd->od_osfs_lock);
8756 spin_unlock(&osd->od_osfs_lock);
8762 * once last export (we don't count self-export) disappeared
8763 * osd can be released
8765 static int osd_obd_disconnect(struct obd_export *exp)
8767 struct obd_device *obd = exp->exp_obd;
8768 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8769 int rc, release = 0;
8773 /* Only disconnect the underlying layers on the final disconnect. */
8774 spin_lock(&osd->od_osfs_lock);
8776 if (osd->od_connects == 0)
8778 spin_unlock(&osd->od_osfs_lock);
8780 rc = class_disconnect(exp); /* bz 9811 */
8782 if (rc == 0 && release)
8783 class_manual_cleanup(obd);
8787 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
8788 struct lu_device *dev)
8790 struct osd_device *osd = osd_dev(dev);
8791 struct lr_server_data *lsd =
8792 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
8797 if (osd->od_quota_slave_md != NULL) {
8798 /* set up quota slave objects for inode */
8799 result = qsd_prepare(env, osd->od_quota_slave_md);
8804 if (osd->od_quota_slave_dt != NULL) {
8805 /* set up quota slave objects for block */
8806 result = qsd_prepare(env, osd->od_quota_slave_dt);
8812 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
8813 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
8814 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
8815 osd->od_index_in_idif = 1;
8817 osd->od_index_in_idif = 0;
8818 result = osd_register_proc_index_in_idif(osd);
8823 osd->od_index_in_idif = 1;
8827 result = osd_fid_init(env, osd);
8833 * Implementation of lu_device_operations::ldo_fid_alloc() for OSD
8837 * see include/lu_object.h for the details.
8839 static int osd_fid_alloc(const struct lu_env *env, struct lu_device *d,
8840 struct lu_fid *fid, struct lu_object *parent,
8841 const struct lu_name *name)
8843 struct osd_device *osd = osd_dev(d);
8845 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
8848 static const struct lu_object_operations osd_lu_obj_ops = {
8849 .loo_object_init = osd_object_init,
8850 .loo_object_delete = osd_object_delete,
8851 .loo_object_release = osd_object_release,
8852 .loo_object_free = osd_object_free,
8853 .loo_object_print = osd_object_print,
8854 .loo_object_invariant = osd_object_invariant
8857 const struct lu_device_operations osd_lu_ops = {
8858 .ldo_object_alloc = osd_object_alloc,
8859 .ldo_process_config = osd_process_config,
8860 .ldo_recovery_complete = osd_recovery_complete,
8861 .ldo_prepare = osd_prepare,
8862 .ldo_fid_alloc = osd_fid_alloc,
8865 static const struct lu_device_type_operations osd_device_type_ops = {
8866 .ldto_init = osd_type_init,
8867 .ldto_fini = osd_type_fini,
8869 .ldto_start = osd_type_start,
8870 .ldto_stop = osd_type_stop,
8872 .ldto_device_alloc = osd_device_alloc,
8873 .ldto_device_free = osd_device_free,
8875 .ldto_device_init = osd_device_init,
8876 .ldto_device_fini = osd_device_fini
8879 static struct lu_device_type osd_device_type = {
8880 .ldt_tags = LU_DEVICE_DT,
8881 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
8882 .ldt_ops = &osd_device_type_ops,
8883 .ldt_ctx_tags = LCT_LOCAL,
8886 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
8888 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
8889 struct super_block *sb = osd_sb(osd);
8891 return (osd->od_mnt == NULL || sb->s_flags & SB_RDONLY);
8895 * lprocfs legacy support.
8897 static const struct obd_ops osd_obd_device_ops = {
8898 .o_owner = THIS_MODULE,
8899 .o_connect = osd_obd_connect,
8900 .o_disconnect = osd_obd_disconnect,
8901 .o_health_check = osd_health_check,
8904 static ssize_t track_declares_assert_show(struct kobject *kobj,
8905 struct attribute *attr,
8908 return sprintf(buf, "%d\n", ldiskfs_track_declares_assert);
8911 static ssize_t track_declares_assert_store(struct kobject *kobj,
8912 struct attribute *attr,
8913 const char *buffer, size_t count)
8915 bool track_declares_assert;
8918 rc = kstrtobool(buffer, &track_declares_assert);
8922 ldiskfs_track_declares_assert = track_declares_assert;
8926 LUSTRE_RW_ATTR(track_declares_assert);
8928 static int __init osd_init(void)
8930 struct kobject *kobj;
8933 BUILD_BUG_ON(BH_DXLock >=
8934 sizeof(((struct buffer_head *)0)->b_state) * 8);
8935 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
8936 /* please, try to keep osd_thread_info smaller than a page */
8937 BUILD_BUG_ON(sizeof(struct osd_thread_info) > PAGE_SIZE);
8942 rc = lu_kmem_init(ldiskfs_caches);
8946 rc = class_register_type(&osd_obd_device_ops, NULL, true,
8947 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
8949 lu_kmem_fini(ldiskfs_caches);
8953 kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
8955 rc = sysfs_create_file(kobj,
8956 &lustre_attr_track_declares_assert.attr);
8959 CWARN("osd-ldiskfs: track_declares_assert failed to register with sysfs\n");
8964 #ifndef HAVE_FLUSH_DELAYED_FPUT
8965 if (unlikely(cfs_flush_delayed_fput == NULL))
8966 cfs_flush_delayed_fput =
8967 cfs_kallsyms_lookup_name("flush_delayed_fput");
8973 static void __exit osd_exit(void)
8975 struct kobject *kobj;
8977 kobj = kset_find_obj(lustre_kset, LUSTRE_OSD_LDISKFS_NAME);
8979 sysfs_remove_file(kobj,
8980 &lustre_attr_track_declares_assert.attr);
8983 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
8984 lu_kmem_fini(ldiskfs_caches);
8987 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
8988 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
8989 MODULE_VERSION(LUSTRE_VERSION_STRING);
8990 MODULE_LICENSE("GPL");
8992 module_init(osd_init);
8993 module_exit(osd_exit);