4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/osd/osd_handler.c
34 * Top-level entry points into osd module
36 * Author: Nikita Danilov <nikita@clusterfs.com>
37 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
40 #define DEBUG_SUBSYSTEM S_OSD
42 #include <linux/module.h>
43 #include <linux/user_namespace.h>
44 #ifdef HAVE_UIDGID_HEADER
45 # include <linux/uidgid.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
57 #include <ldiskfs/ldiskfs.h>
58 #include <ldiskfs/xattr.h>
59 #include <ldiskfs/ldiskfs_extents.h>
62 * struct OBD_{ALLOC,FREE}*()
65 #include <obd_support.h>
66 /* struct ptlrpc_thread */
67 #include <lustre_net.h>
68 #include <lustre_fid.h>
70 #include <lustre_param.h>
72 #include "osd_internal.h"
73 #include "osd_dynlocks.h"
75 /* llo_* api support */
76 #include <md_object.h>
77 #include <lustre_quota.h>
79 #include <lustre_linkea.h>
82 module_param(ldiskfs_pdo, int, 0644);
83 MODULE_PARM_DESC(ldiskfs_pdo, "ldiskfs with parallel directory operations");
85 int ldiskfs_track_declares_assert;
86 module_param(ldiskfs_track_declares_assert, int, 0644);
87 MODULE_PARM_DESC(ldiskfs_track_declares_assert, "LBUG during tracking of declares");
89 /* Slab to allocate dynlocks */
90 struct kmem_cache *dynlock_cachep;
92 /* Slab to allocate osd_it_ea */
93 struct kmem_cache *osd_itea_cachep;
95 static struct lu_kmem_descr ldiskfs_caches[] = {
97 .ckd_cache = &dynlock_cachep,
98 .ckd_name = "dynlock_cache",
99 .ckd_size = sizeof(struct dynlock_handle)
102 .ckd_cache = &osd_itea_cachep,
103 .ckd_name = "osd_itea_cache",
104 .ckd_size = sizeof(struct osd_it_ea)
111 static const char dot[] = ".";
112 static const char dotdot[] = "..";
113 static const char remote_obj_dir[] = "REM_OBJ_DIR";
115 static const struct lu_object_operations osd_lu_obj_ops;
116 static const struct dt_object_operations osd_obj_ops;
117 static const struct dt_object_operations osd_obj_ea_ops;
118 static const struct dt_object_operations osd_obj_otable_it_ops;
119 static const struct dt_index_operations osd_index_iam_ops;
120 static const struct dt_index_operations osd_index_ea_ops;
122 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
123 const struct lu_fid *fid);
124 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
125 struct osd_device *osd);
127 int osd_trans_declare_op2rb[] = {
128 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
129 [OSD_OT_PUNCH] = OSD_OT_MAX,
130 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
131 [OSD_OT_CREATE] = OSD_OT_DESTROY,
132 [OSD_OT_DESTROY] = OSD_OT_CREATE,
133 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
134 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
135 [OSD_OT_WRITE] = OSD_OT_WRITE,
136 [OSD_OT_INSERT] = OSD_OT_DELETE,
137 [OSD_OT_DELETE] = OSD_OT_INSERT,
138 [OSD_OT_QUOTA] = OSD_OT_MAX,
141 static int osd_has_index(const struct osd_object *obj)
143 return obj->oo_dt.do_index_ops != NULL;
146 static int osd_object_invariant(const struct lu_object *l)
148 return osd_invariant(osd_obj(l));
152 * Concurrency: doesn't matter
156 * Concurrency: doesn't matter
158 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
160 struct osd_thread_info *oti = osd_oti_get(env);
161 return oti->oti_w_locks > 0 && o->oo_owner == env;
165 * Concurrency: doesn't access mutable data
167 static int osd_root_get(const struct lu_env *env,
168 struct dt_device *dev, struct lu_fid *f)
170 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
175 * the following set of functions are used to maintain per-thread
176 * cache of FID->ino mapping. this mechanism is needed to resolve
177 * FID to inode at dt_insert() which in turn stores ino in the
178 * directory entries to keep ldiskfs compatible with ext[34].
179 * due to locking-originated restrictions we can't lookup ino
180 * using LU cache (deadlock is possible). lookup using OI is quite
181 * expensive. so instead we maintain this cache and methods like
182 * dt_create() fill it. so in the majority of cases dt_insert() is
183 * able to find needed mapping in lockless manner.
185 static struct osd_idmap_cache *
186 osd_idc_find(const struct lu_env *env, struct osd_device *osd,
187 const struct lu_fid *fid)
189 struct osd_thread_info *oti = osd_oti_get(env);
190 struct osd_idmap_cache *idc = oti->oti_ins_cache;
192 for (i = 0; i < oti->oti_ins_cache_used; i++) {
193 if (!lu_fid_eq(&idc[i].oic_fid, fid))
195 if (idc[i].oic_dev != osd)
204 static struct osd_idmap_cache *
205 osd_idc_add(const struct lu_env *env, struct osd_device *osd,
206 const struct lu_fid *fid)
208 struct osd_thread_info *oti = osd_oti_get(env);
209 struct osd_idmap_cache *idc;
212 if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
213 i = oti->oti_ins_cache_size * 2;
215 i = OSD_INS_CACHE_SIZE;
216 OBD_ALLOC(idc, sizeof(*idc) * i);
218 return ERR_PTR(-ENOMEM);
219 if (oti->oti_ins_cache != NULL) {
220 memcpy(idc, oti->oti_ins_cache,
221 oti->oti_ins_cache_used * sizeof(*idc));
222 OBD_FREE(oti->oti_ins_cache,
223 oti->oti_ins_cache_used * sizeof(*idc));
225 oti->oti_ins_cache = idc;
226 oti->oti_ins_cache_size = i;
229 idc = oti->oti_ins_cache + oti->oti_ins_cache_used++;
232 idc->oic_lid.oii_ino = 0;
233 idc->oic_lid.oii_gen = 0;
240 * lookup mapping for the given fid in the cache, initialize a
241 * new one if not found. the initialization checks whether the
242 * object is local or remote. for local objects, OI is used to
243 * learn ino/generation. the function is used when the caller
244 * has no information about the object, e.g. at dt_insert().
246 static struct osd_idmap_cache *
247 osd_idc_find_or_init(const struct lu_env *env, struct osd_device *osd,
248 const struct lu_fid *fid)
250 struct osd_idmap_cache *idc;
253 idc = osd_idc_find(env, osd, fid);
254 LASSERT(!IS_ERR(idc));
258 /* new mapping is needed */
259 idc = osd_idc_add(env, osd, fid);
264 rc = osd_remote_fid(env, osd, fid);
265 if (unlikely(rc < 0))
269 /* the object is local, lookup in OI */
270 /* XXX: probably cheaper to lookup in LU first? */
271 rc = osd_oi_lookup(osd_oti_get(env), osd, fid,
273 if (unlikely(rc < 0)) {
274 CERROR("can't lookup: rc = %d\n", rc);
278 /* the object is remote */
286 * lookup mapping for given FID and fill it from the given object.
287 * the object is lolcal by definition.
289 static int osd_idc_find_and_init(const struct lu_env *env,
290 struct osd_device *osd,
291 struct osd_object *obj)
293 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
294 struct osd_idmap_cache *idc;
296 idc = osd_idc_find(env, osd, fid);
297 LASSERT(!IS_ERR(idc));
299 if (obj->oo_inode == NULL)
301 if (idc->oic_lid.oii_ino != obj->oo_inode->i_ino) {
302 LASSERT(idc->oic_lid.oii_ino == 0);
303 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
304 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
309 /* new mapping is needed */
310 idc = osd_idc_add(env, osd, fid);
314 if (obj->oo_inode != NULL) {
315 idc->oic_lid.oii_ino = obj->oo_inode->i_ino;
316 idc->oic_lid.oii_gen = obj->oo_inode->i_generation;
322 * OSD object methods.
326 * Concurrency: no concurrent access is possible that early in object
329 static struct lu_object *osd_object_alloc(const struct lu_env *env,
330 const struct lu_object_header *hdr,
333 struct osd_object *mo;
339 l = &mo->oo_dt.do_lu;
340 dt_object_init(&mo->oo_dt, NULL, d);
341 mo->oo_dt.do_ops = &osd_obj_ea_ops;
342 l->lo_ops = &osd_lu_obj_ops;
343 init_rwsem(&mo->oo_sem);
344 init_rwsem(&mo->oo_ext_idx_sem);
345 spin_lock_init(&mo->oo_guard);
346 INIT_LIST_HEAD(&mo->oo_xattr_list);
353 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
354 struct dentry *dentry, struct lustre_mdt_attrs *lma)
358 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
359 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
360 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
362 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
363 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
365 lustre_lma_swab(lma);
366 /* Check LMA compatibility */
367 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
368 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
369 "for fid = "DFID", ino = %lu\n",
370 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
371 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
372 PFID(&lma->lma_self_fid), inode->i_ino);
375 } else if (rc == 0) {
383 * retrieve object from backend ext fs.
385 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
386 struct osd_inode_id *id)
389 struct inode *inode = NULL;
391 /* if we look for an inode withing a running
392 * transaction, then we risk to deadlock */
393 /* osd_dirent_check_repair() breaks this */
394 /*LASSERT(current->journal_info == NULL);*/
396 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
398 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
399 id->oii_ino, PTR_ERR(inode));
400 } else if (id->oii_gen != OSD_OII_NOGEN &&
401 inode->i_generation != id->oii_gen) {
402 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
403 "i_generation = %u\n",
404 id->oii_ino, id->oii_gen, inode->i_generation);
406 inode = ERR_PTR(-ESTALE);
407 } else if (inode->i_nlink == 0) {
408 /* due to parallel readdir and unlink,
409 * we can have dead inode here. */
410 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
412 inode = ERR_PTR(-ESTALE);
413 } else if (is_bad_inode(inode)) {
414 CWARN("%.16s: bad inode: ino = %u\n",
415 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
417 inode = ERR_PTR(-ENOENT);
418 } else if ((rc = osd_attach_jinode(inode))) {
422 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
423 if (id->oii_gen == OSD_OII_NOGEN)
424 osd_id_gen(id, inode->i_ino, inode->i_generation);
426 /* Do not update file c/mtime in ldiskfs.
427 * NB: we don't have any lock to protect this because we don't
428 * have reference on osd_object now, but contention with
429 * another lookup + attr_set can't happen in the tiny window
430 * between if (...) and set S_NOCMTIME. */
431 if (!(inode->i_flags & S_NOCMTIME))
432 inode->i_flags |= S_NOCMTIME;
437 int osd_ldiskfs_add_entry(struct osd_thread_info *info, struct osd_device *osd,
438 handle_t *handle, struct dentry *child,
439 struct inode *inode, struct htree_lock *hlock)
443 rc = __ldiskfs_add_entry(handle, child, inode, hlock);
444 if (rc == -ENOBUFS || rc == -ENOSPC) {
445 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
446 struct inode *parent = child->d_parent->d_inode;
447 struct lu_fid *fid = NULL;
449 rc2 = osd_get_lma(info, parent, child->d_parent, lma);
451 fid = &lma->lma_self_fid;
452 } else if (rc2 == -ENODATA) {
453 if (unlikely(parent == inode->i_sb->s_root->d_inode)) {
454 fid = &info->oti_fid3;
455 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
456 } else if (!osd->od_is_ost && osd->od_index == 0) {
457 fid = &info->oti_fid3;
458 lu_igif_build(fid, parent->i_ino,
459 parent->i_generation);
464 CWARN("%s: directory (inode: %lu, FID: "DFID") %s "
465 "maximum entry limit\n",
466 osd_name(osd), parent->i_ino, PFID(fid),
467 rc == -ENOSPC ? "has reached" : "is approaching");
469 CWARN("%s: directory (inode: %lu, FID: unknown) %s "
470 "maximum entry limit\n",
471 osd_name(osd), parent->i_ino,
472 rc == -ENOSPC ? "has reached" : "is approaching");
474 /* ignore such error now */
483 static struct inode *
484 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
485 struct osd_inode_id *id, struct lu_fid *fid)
487 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
491 inode = osd_iget(info, dev, id);
495 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
497 *fid = lma->lma_self_fid;
498 } else if (rc == -ENODATA) {
499 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
500 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
502 lu_igif_build(fid, inode->i_ino, inode->i_generation);
510 static struct inode *osd_iget_check(struct osd_thread_info *info,
511 struct osd_device *dev,
512 const struct lu_fid *fid,
513 struct osd_inode_id *id,
520 /* The cached OI mapping is trustable. If we cannot locate the inode
521 * via the cached OI mapping, then return the failure to the caller
522 * directly without further OI checking. */
525 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
528 if (!trusted && (rc == -ENOENT || rc == -ESTALE))
531 CDEBUG(D_INODE, "no inode for FID: "DFID", ino = %u, rc = %d\n",
532 PFID(fid), id->oii_ino, rc);
536 if (is_bad_inode(inode)) {
541 CDEBUG(D_INODE, "bad inode for FID: "DFID", ino = %u\n",
542 PFID(fid), id->oii_ino);
546 if (id->oii_gen != OSD_OII_NOGEN &&
547 inode->i_generation != id->oii_gen) {
552 CDEBUG(D_INODE, "unmatched inode for FID: "DFID", ino = %u, "
553 "oii_gen = %u, i_generation = %u\n", PFID(fid),
554 id->oii_ino, id->oii_gen, inode->i_generation);
558 if (inode->i_nlink == 0) {
563 CDEBUG(D_INODE, "stale inode for FID: "DFID", ino = %u\n",
564 PFID(fid), id->oii_ino);
568 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
572 __u32 saved_ino = id->oii_ino;
573 __u32 saved_gen = id->oii_gen;
576 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
578 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
579 /* XXX: There are four possible cases:
581 * Backup/restore caused the OI invalid.
583 * Someone unlinked the object but NOT removed
584 * the OI mapping, such as mount target device
585 * as ldiskfs, and modify something directly.
587 * Someone just removed the object between the
588 * former oi_lookup and the iget. It is normal.
589 * 4. Other failure cases.
591 * Generally, when the device is mounted, it will
592 * auto check whether the system is restored from
593 * file-level backup or not. We trust such detect
594 * to distinguish the 1st case from the 2nd case:
595 * if the OI files are consistent but may contain
596 * stale OI mappings because of case 2, if iget()
597 * returns -ENOENT or -ESTALE, then it should be
600 /* If the OI mapping was in OI file before the
601 * osd_iget_check(), but now, it is disappear,
602 * then it must be removed by race. That is a
603 * normal race case. */
606 /* It is the OI scrub updated the OI mapping by race.
607 * The new OI mapping must be valid. */
608 if (saved_ino != id->oii_ino || saved_gen != id->oii_gen) {
614 if (dev->od_scrub.os_file.sf_flags & SF_INCONSISTENT)
615 /* It still can be the case 2, but we cannot
616 * distinguish it from the case 1. So return
617 * -EREMCHG to block current operation until
618 * OI scrub rebuilt the OI mappings. */
626 if (inode->i_generation == id->oii_gen)
631 if (id->oii_gen == OSD_OII_NOGEN)
632 osd_id_gen(id, inode->i_ino, inode->i_generation);
634 /* Do not update file c/mtime in ldiskfs.
635 * NB: we don't have any lock to protect this because we don't
636 * have reference on osd_object now, but contention with
637 * another lookup + attr_set can't happen in the tiny window
638 * between if (...) and set S_NOCMTIME. */
639 if (!(inode->i_flags & S_NOCMTIME))
640 inode->i_flags |= S_NOCMTIME;
657 * \retval +v: new filter_fid, does not contain self-fid
658 * \retval 0: filter_fid_old, contains self-fid
659 * \retval -v: other failure cases
661 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
662 struct dentry *dentry, struct lu_fid *fid)
664 struct filter_fid_old *ff = &info->oti_ff;
665 struct ost_id *ostid = &info->oti_ostid;
668 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
669 if (rc == sizeof(*ff)) {
671 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
672 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
673 /* XXX: use 0 as the index for compatibility, the caller will
674 * handle index related issues when necessarry. */
675 ostid_to_fid(fid, ostid, 0);
676 } else if (rc == sizeof(struct filter_fid)) {
678 } else if (rc >= 0) {
685 static int osd_lma_self_repair(struct osd_thread_info *info,
686 struct osd_device *osd, struct inode *inode,
687 const struct lu_fid *fid, __u32 compat)
692 LASSERT(current->journal_info == NULL);
694 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
695 osd_dto_credits_noquota[DTO_XATTR_SET]);
698 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
703 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
705 CWARN("%s: cannot self repair the LMA: rc = %d\n",
707 ldiskfs_journal_stop(jh);
711 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
713 struct osd_thread_info *info = osd_oti_get(env);
714 struct osd_device *osd = osd_obj2dev(obj);
715 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
716 struct inode *inode = obj->oo_inode;
717 struct dentry *dentry = &info->oti_obj_dentry;
718 struct lu_fid *fid = NULL;
719 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
723 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
724 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
725 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
726 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
727 fid = &lma->lma_self_fid;
728 rc = osd_get_idif(info, inode, dentry, fid);
729 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
730 /* For the given OST-object, if it has neither LMA nor
731 * FID in XATTR_NAME_FID, then the given FID (which is
732 * contained in the @obj, from client RPC for locating
733 * the OST-object) is trusted. We use it to generate
735 osd_lma_self_repair(info, osd, inode, rfid,
746 lustre_lma_swab(lma);
747 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
748 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
749 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
750 "fid = "DFID", ino = %lu\n", osd_name(osd),
751 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
752 PFID(rfid), inode->i_ino);
755 fid = &lma->lma_self_fid;
759 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
760 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
761 struct ost_id *oi = &info->oti_ostid;
762 struct lu_fid *fid1 = &info->oti_fid3;
763 __u32 idx = fid_idif_ost_idx(rfid);
765 /* For old IDIF, the OST index is not part of the IDIF,
766 * Means that different OSTs may have the same IDIFs.
767 * Under such case, we need to make some compatible
768 * check to make sure to trigger OI scrub properly. */
769 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
770 /* Given @rfid is new, LMA is old. */
771 fid_to_ostid(fid, oi);
772 ostid_to_fid(fid1, oi, idx);
773 if (lu_fid_eq(fid1, rfid)) {
774 if (osd->od_index_in_idif)
775 osd_lma_self_repair(info, osd,
789 struct osd_check_lmv_buf {
790 #ifdef HAVE_DIR_CONTEXT
791 /* please keep it as first member */
792 struct dir_context ctx;
794 struct osd_thread_info *oclb_info;
795 struct osd_device *oclb_dev;
796 struct osd_idmap_cache *oclb_oic;
800 * It is called internally by ->readdir() to filter out the
801 * local slave object's FID of the striped directory.
803 * \retval 1 found the local slave's FID
804 * \retval 0 continue to check next item
805 * \retval -ve for failure
807 #ifdef HAVE_FILLDIR_USE_CTX
808 static int osd_stripe_dir_filldir(struct dir_context *buf,
810 static int osd_stripe_dir_filldir(void *buf,
812 const char *name, int namelen,
813 loff_t offset, __u64 ino, unsigned d_type)
815 struct osd_check_lmv_buf *oclb = (struct osd_check_lmv_buf *)buf;
816 struct osd_thread_info *oti = oclb->oclb_info;
817 struct lu_fid *fid = &oti->oti_fid3;
818 struct osd_inode_id *id = &oti->oti_id3;
819 struct osd_device *dev = oclb->oclb_dev;
820 struct osd_idmap_cache *oic = oclb->oclb_oic;
828 sscanf(name + 1, SFID, RFID(fid));
829 if (!fid_is_sane(fid))
832 if (osd_remote_fid(oti->oti_env, dev, fid))
835 osd_id_gen(id, ino, OSD_OII_NOGEN);
836 inode = osd_iget(oti, dev, id);
838 return PTR_ERR(inode);
841 osd_add_oi_cache(oti, dev, id, fid);
845 rc = osd_oii_insert(dev, oic, true);
847 return rc == 0 ? 1 : rc;
850 /* When lookup item under striped directory, we need to locate the master
851 * MDT-object of the striped directory firstly, then the client will send
852 * lookup (getattr_by_name) RPC to the MDT with some slave MDT-object's FID
853 * and the item's name. If the system is restored from MDT file level backup,
854 * then before the OI scrub completely built the OI files, the OI mappings of
855 * the master MDT-object and slave MDT-object may be invalid. Usually, it is
856 * not a problem for the master MDT-object. Because when locate the master
857 * MDT-object, we will do name based lookup (for the striped directory itself)
858 * firstly, during such process we can setup the correct OI mapping for the
859 * master MDT-object. But it will be trouble for the slave MDT-object. Because
860 * the client will not trigger name based lookup on the MDT to locate the slave
861 * MDT-object before locating item under the striped directory, then when
862 * osd_fid_lookup(), it will find that the OI mapping for the slave MDT-object
863 * is invalid and does not know what the right OI mapping is, then the MDT has
864 * to return -EINPROGRESS to the client to notify that the OI scrub is rebuiding
865 * the OI file, related OI mapping is unknown yet, please try again later. And
866 * then client will re-try the RPC again and again until related OI mapping has
867 * been updated. That is quite inefficient.
869 * To resolve above trouble, we will handle it as the following two cases:
871 * 1) The slave MDT-object and the master MDT-object are on different MDTs.
872 * It is relative easy. Be as one of remote MDT-objects, the slave MDT-object
873 * is linked under /REMOTE_PARENT_DIR with the name of its FID string.
874 * We can locate the slave MDT-object via lookup the /REMOTE_PARENT_DIR
875 * directly. Please check osd_fid_lookup().
877 * 2) The slave MDT-object and the master MDT-object reside on the same MDT.
878 * Under such case, during lookup the master MDT-object, we will lookup the
879 * slave MDT-object via readdir against the master MDT-object, because the
880 * slave MDT-objects information are stored as sub-directories with the name
881 * "${FID}:${index}". Then when find the local slave MDT-object, its OI
882 * mapping will be recorded. Then subsequent osd_fid_lookup() will know
883 * the correct OI mapping for the slave MDT-object. */
884 static int osd_check_lmv(struct osd_thread_info *oti, struct osd_device *dev,
885 struct inode *inode, struct osd_idmap_cache *oic)
887 struct lu_buf *buf = &oti->oti_big_buf;
888 struct dentry *dentry = &oti->oti_obj_dentry;
889 struct file *filp = &oti->oti_file;
890 const struct file_operations *fops;
891 struct lmv_mds_md_v1 *lmv1;
892 struct osd_check_lmv_buf oclb = {
893 #ifdef HAVE_DIR_CONTEXT
894 .ctx.actor = osd_stripe_dir_filldir,
904 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, buf->lb_buf,
907 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMV, NULL, 0);
909 lu_buf_realloc(buf, rc);
910 if (buf->lb_buf == NULL)
911 GOTO(out, rc = -ENOMEM);
917 if (unlikely(rc == 0 || rc == -ENODATA))
923 if (unlikely(buf->lb_buf == NULL)) {
924 lu_buf_realloc(buf, rc);
925 if (buf->lb_buf == NULL)
926 GOTO(out, rc = -ENOMEM);
932 if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
936 dentry->d_inode = inode;
937 dentry->d_sb = inode->i_sb;
939 filp->f_path.dentry = dentry;
940 filp->f_mode = FMODE_64BITHASH;
941 filp->f_mapping = inode->i_mapping;
943 filp->private_data = NULL;
944 set_file_inode(filp, inode);
946 #ifdef HAVE_DIR_CONTEXT
947 oclb.ctx.pos = filp->f_pos;
948 rc = fops->iterate(filp, &oclb.ctx);
949 filp->f_pos = oclb.ctx.pos;
951 rc = fops->readdir(filp, &oclb, osd_stripe_dir_filldir);
953 fops->release(inode, filp);
957 CDEBUG(D_LFSCK, "%.16s: fail to check LMV EA, inode = %lu/%u,"
959 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
960 inode->i_ino, inode->i_generation,
961 PFID(&oic->oic_fid), rc);
968 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
969 const struct lu_fid *fid,
970 const struct lu_object_conf *conf)
972 struct osd_thread_info *info;
973 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
974 struct osd_device *dev;
975 struct osd_idmap_cache *oic;
976 struct osd_inode_id *id;
977 struct inode *inode = NULL;
978 struct osd_scrub *scrub;
979 struct scrub_file *sf;
980 __u32 flags = SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT | SS_AUTO_FULL;
987 bool updated = false;
990 LINVRNT(osd_invariant(obj));
991 LASSERT(obj->oo_inode == NULL);
992 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
995 scrub = &dev->od_scrub;
996 sf = &scrub->os_file;
997 info = osd_oti_get(env);
999 oic = &info->oti_cache;
1001 if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
1004 /* For the object is created as locking anchor, or for the object to
1005 * be created on disk. No need to osd_oi_lookup() at here because FID
1006 * shouldn't never be re-used, if it's really a duplicate FID from
1007 * unexpected reason, we should be able to detect it later by calling
1008 * do_create->osd_oi_insert(). */
1009 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
1010 GOTO(out, result = 0);
1012 /* Search order: 1. per-thread cache. */
1013 if (lu_fid_eq(fid, &oic->oic_fid) &&
1014 likely(oic->oic_dev == dev)) {
1020 if (!list_empty(&scrub->os_inconsistent_items)) {
1021 /* Search order: 2. OI scrub pending list. */
1022 result = osd_oii_lookup(dev, fid, id);
1027 /* The OI mapping in the OI file can be updated by the OI scrub
1028 * when we locate the inode via FID. So it may be not trustable. */
1031 /* Search order: 3. OI files. */
1032 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1033 if (result == -ENOENT) {
1034 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
1035 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
1036 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
1038 GOTO(out, result = 0);
1047 inode = osd_iget_check(info, dev, fid, id, trusted);
1048 if (IS_ERR(inode)) {
1049 result = PTR_ERR(inode);
1050 if (result == -ENOENT || result == -ESTALE)
1051 GOTO(out, result = 0);
1053 if (result == -EREMCHG) {
1056 /* We still have chance to get the valid inode: for the
1057 * object which is referenced by remote name entry, the
1058 * object on the local MDT will be linked under the dir
1059 * of "/REMOTE_PARENT_DIR" with its FID string as name.
1061 * We do not know whether the object for the given FID
1062 * is referenced by some remote name entry or not, and
1063 * especially for DNE II, a multiple-linked object may
1064 * have many name entries reside on many MDTs.
1066 * To simplify the operation, OSD will not distinguish
1067 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
1068 * only happened for the RPC from other MDT during the
1069 * OI scrub, or for the client side RPC with FID only,
1070 * such as FID to path, or from old connected client. */
1072 !fid_is_on_ost(info, dev, fid, OI_CHECK_FLD)) {
1073 rc1 = osd_lookup_in_remote_parent(info, dev,
1078 flags |= SS_AUTO_PARTIAL;
1079 flags &= ~SS_AUTO_FULL;
1084 if (thread_is_running(&scrub->os_thread)) {
1085 if (scrub->os_partial_scan &&
1086 !scrub->os_in_join) {
1089 if (inode != NULL && !IS_ERR(inode)) {
1092 osd_add_oi_cache(info, dev, id,
1094 osd_oii_insert(dev, oic, true);
1096 result = -EINPROGRESS;
1099 } else if (!dev->od_noscrub) {
1102 rc1 = osd_scrub_start(dev, flags);
1103 LCONSOLE_WARN("%.16s: trigger OI scrub by RPC "
1104 "for the "DFID" with flags 0x%x,"
1105 " rc = %d\n", osd_name(dev),
1106 PFID(fid), flags, rc1);
1107 if (rc1 == 0 || rc1 == -EALREADY) {
1108 if (inode != NULL && !IS_ERR(inode)) {
1111 osd_add_oi_cache(info, dev, id,
1113 osd_oii_insert(dev, oic, true);
1115 result = -EINPROGRESS;
1125 if (inode == NULL || IS_ERR(inode))
1127 } else if (remote) {
1131 obj->oo_inode = inode;
1132 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
1134 result = osd_check_lma(env, obj);
1138 LASSERTF(id->oii_ino == inode->i_ino &&
1139 id->oii_gen == inode->i_generation,
1140 "locate wrong inode for FID: "DFID", %u/%u => %ld/%u\n",
1141 PFID(fid), id->oii_ino, id->oii_gen,
1142 inode->i_ino, inode->i_generation);
1144 saved_ino = inode->i_ino;
1145 saved_gen = inode->i_generation;
1147 if (unlikely(result == -ENODATA)) {
1148 /* If the OI scrub updated the OI mapping by race, it
1149 * must be valid. Trust the inode that has no LMA EA. */
1153 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1155 /* The OI mapping is still there, the inode is still
1156 * valid. It is just becaues the inode has no LMA EA. */
1157 if (saved_ino == id->oii_ino &&
1158 saved_gen == id->oii_gen)
1161 /* It is the OI scrub updated the OI mapping by race.
1162 * The new OI mapping must be valid. */
1165 obj->oo_inode = NULL;
1171 /* "result == -ENOENT" means that the OI mappinghas been
1172 * removed by race, so the inode belongs to other object.
1174 * Others error can be returned directly. */
1175 if (result == -ENOENT) {
1184 obj->oo_inode = NULL;
1186 if (result != -EREMCHG)
1191 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
1192 /* "result == -ENOENT" means the cached OI mapping has been removed
1193 * from the OI file by race, above inode belongs to other object. */
1194 if (result == -ENOENT) {
1197 GOTO(out, result = 0);
1203 if (saved_ino == id->oii_ino && saved_gen == id->oii_gen)
1206 /* It is the OI scrub updated the OI mapping by race.
1207 * The new OI mapping must be valid. */
1213 obj->oo_compat_dot_created = 1;
1214 obj->oo_compat_dotdot_created = 1;
1216 if (S_ISDIR(inode->i_mode) &&
1217 (flags & SS_AUTO_PARTIAL || sf->sf_status == SS_SCANNING))
1218 osd_check_lmv(info, dev, inode, oic);
1220 result = osd_attach_jinode(inode);
1222 obj->oo_inode = NULL;
1228 GOTO(out, result = 0);
1230 LASSERT(obj->oo_hl_head == NULL);
1231 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1232 if (obj->oo_hl_head == NULL) {
1233 obj->oo_inode = NULL;
1235 GOTO(out, result = -ENOMEM);
1237 GOTO(out, result = 0);
1240 if (result != 0 && trusted)
1241 fid_zero(&oic->oic_fid);
1243 LINVRNT(osd_invariant(obj));
1248 * Concurrency: shouldn't matter.
1250 static void osd_object_init0(struct osd_object *obj)
1252 LASSERT(obj->oo_inode != NULL);
1253 obj->oo_dt.do_body_ops = &osd_body_ops;
1254 obj->oo_dt.do_lu.lo_header->loh_attr |=
1255 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
1259 * Concurrency: no concurrent access is possible that early in object
1262 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
1263 const struct lu_object_conf *conf)
1265 struct osd_object *obj = osd_obj(l);
1268 LINVRNT(osd_invariant(obj));
1270 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
1271 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
1272 l->lo_header->loh_attr |= LOHA_EXISTS;
1276 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
1277 obj->oo_dt.do_body_ops = &osd_body_ops_new;
1278 if (result == 0 && obj->oo_inode != NULL) {
1279 struct osd_thread_info *oti = osd_oti_get(env);
1280 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
1282 osd_object_init0(obj);
1283 result = osd_get_lma(oti, obj->oo_inode,
1284 &oti->oti_obj_dentry, lma);
1286 /* Convert LMAI flags to lustre LMA flags
1287 * and cache it to oo_lma_flags */
1289 lma_to_lustre_flags(lma->lma_incompat);
1290 } else if (result == -ENODATA) {
1295 LINVRNT(osd_invariant(obj));
1299 /* The first part of oxe_buf is xattr name, and is '\0' terminated.
1300 * The left part is for value, binary mode. */
1301 struct osd_xattr_entry {
1302 struct list_head oxe_list;
1306 struct rcu_head oxe_rcu;
1310 static int osd_oxc_get(struct osd_object *obj, const char *name,
1313 struct osd_xattr_entry *tmp;
1314 struct osd_xattr_entry *oxe = NULL;
1315 size_t namelen = strlen(name);
1320 list_for_each_entry_rcu(tmp, &obj->oo_xattr_list, oxe_list) {
1321 if (namelen == tmp->oxe_namelen &&
1322 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1329 GOTO(out, rc = -ENOENT);
1331 if (!oxe->oxe_exist)
1332 GOTO(out, rc = -ENODATA);
1335 rc = oxe->oxe_len - sizeof(*oxe) - oxe->oxe_namelen - 1;
1338 if (buf->lb_buf == NULL)
1341 if (buf->lb_len < rc)
1342 GOTO(out, rc = -ERANGE);
1344 memcpy(buf->lb_buf, &oxe->oxe_buf[namelen + 1], rc);
1352 static void osd_oxc_free(struct rcu_head *head)
1354 struct osd_xattr_entry *oxe;
1356 oxe = container_of(head, struct osd_xattr_entry, oxe_rcu);
1357 OBD_FREE(oxe, oxe->oxe_len);
1360 static void osd_oxc_add(struct osd_object *obj, const char *name,
1361 const char *buf, int buflen)
1363 struct osd_xattr_entry *oxe;
1364 struct osd_xattr_entry *old = NULL;
1365 struct osd_xattr_entry *tmp;
1366 size_t namelen = strlen(name);
1367 size_t len = sizeof(*oxe) + namelen + 1 + buflen;
1369 OBD_ALLOC(oxe, len);
1373 INIT_LIST_HEAD(&oxe->oxe_list);
1375 oxe->oxe_namelen = namelen;
1376 memcpy(oxe->oxe_buf, name, namelen);
1378 LASSERT(buf != NULL);
1379 memcpy(oxe->oxe_buf + namelen + 1, buf, buflen);
1380 oxe->oxe_exist = true;
1382 oxe->oxe_exist = false;
1385 /* this should be rarely called, just remove old and add new */
1386 spin_lock(&obj->oo_guard);
1387 list_for_each_entry(tmp, &obj->oo_xattr_list, oxe_list) {
1388 if (namelen == tmp->oxe_namelen &&
1389 strncmp(name, tmp->oxe_buf, namelen) == 0) {
1395 list_replace_rcu(&old->oxe_list, &oxe->oxe_list);
1396 call_rcu(&old->oxe_rcu, osd_oxc_free);
1398 list_add_tail_rcu(&oxe->oxe_list, &obj->oo_xattr_list);
1400 spin_unlock(&obj->oo_guard);
1403 static void osd_oxc_del(struct osd_object *obj, const char *name)
1405 struct osd_xattr_entry *oxe;
1406 size_t namelen = strlen(name);
1408 spin_lock(&obj->oo_guard);
1409 list_for_each_entry(oxe, &obj->oo_xattr_list, oxe_list) {
1410 if (namelen == oxe->oxe_namelen &&
1411 strncmp(name, oxe->oxe_buf, namelen) == 0) {
1412 list_del_rcu(&oxe->oxe_list);
1413 call_rcu(&oxe->oxe_rcu, osd_oxc_free);
1417 spin_unlock(&obj->oo_guard);
1420 static void osd_oxc_fini(struct osd_object *obj)
1422 struct osd_xattr_entry *oxe, *next;
1424 list_for_each_entry_safe(oxe, next, &obj->oo_xattr_list, oxe_list) {
1425 list_del(&oxe->oxe_list);
1426 OBD_FREE(oxe, oxe->oxe_len);
1431 * Concurrency: no concurrent access is possible that late in object
1434 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
1436 struct osd_object *obj = osd_obj(l);
1438 LINVRNT(osd_invariant(obj));
1441 dt_object_fini(&obj->oo_dt);
1442 if (obj->oo_hl_head != NULL)
1443 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1448 * Concurrency: no concurrent access is possible that late in object
1451 static void osd_index_fini(struct osd_object *o)
1453 struct iam_container *bag;
1455 if (o->oo_dir != NULL) {
1456 bag = &o->oo_dir->od_container;
1457 if (o->oo_inode != NULL) {
1458 if (bag->ic_object == o->oo_inode)
1459 iam_container_fini(bag);
1461 OBD_FREE_PTR(o->oo_dir);
1467 * Concurrency: no concurrent access is possible that late in object
1468 * life-cycle (for all existing callers, that is. New callers have to provide
1469 * their own locking.)
1471 static int osd_inode_unlinked(const struct inode *inode)
1473 return inode->i_nlink == 0;
1477 OSD_TXN_OI_DELETE_CREDITS = 20,
1478 OSD_TXN_INODE_DELETE_CREDITS = 20
1485 #if OSD_THANDLE_STATS
1487 * Set time when the handle is allocated
1489 static void osd_th_alloced(struct osd_thandle *oth)
1491 oth->oth_alloced = ktime_get();
1495 * Set time when the handle started
1497 static void osd_th_started(struct osd_thandle *oth)
1499 oth->oth_started = ktime_get();
1503 * Check whether the we deal with this handle for too long.
1505 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
1506 ktime_t alloced, ktime_t started,
1509 ktime_t now = ktime_get();
1511 LASSERT(dev != NULL);
1513 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
1514 ktime_us_delta(started, alloced));
1515 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
1516 ktime_us_delta(closed, started));
1517 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
1518 ktime_us_delta(now, closed));
1520 if (ktime_before(ktime_add_ns(alloced, 30 * NSEC_PER_SEC), now)) {
1521 CWARN("transaction handle %p was open for too long: now %lld, alloced %lld, started %lld, closed %lld\n",
1522 oth, now, alloced, started, closed);
1523 libcfs_debug_dumpstack(NULL);
1527 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
1529 ktime_t __closed = ktime_get(); \
1530 ktime_t __alloced = oth->oth_alloced; \
1531 ktime_t __started = oth->oth_started; \
1534 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
1537 #else /* OSD_THANDLE_STATS */
1539 #define osd_th_alloced(h) do {} while(0)
1540 #define osd_th_started(h) do {} while(0)
1541 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
1543 #endif /* OSD_THANDLE_STATS */
1546 * Concurrency: doesn't access mutable data.
1548 static int osd_param_is_not_sane(const struct osd_device *dev,
1549 const struct thandle *th)
1551 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
1553 return oh->ot_credits > osd_transaction_size(dev);
1557 * Concurrency: shouldn't matter.
1559 static void osd_trans_commit_cb(struct super_block *sb,
1560 struct ldiskfs_journal_cb_entry *jcb, int error)
1562 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
1563 struct thandle *th = &oh->ot_super;
1564 struct lu_device *lud = &th->th_dev->dd_lu_dev;
1565 struct dt_txn_commit_cb *dcb, *tmp;
1567 LASSERT(oh->ot_handle == NULL);
1570 CERROR("transaction @0x%p commit error: %d\n", th, error);
1572 dt_txn_hook_commit(th);
1574 /* call per-transaction callbacks if any */
1575 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
1577 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1578 "commit callback entry: magic=%x name='%s'\n",
1579 dcb->dcb_magic, dcb->dcb_name);
1580 list_del_init(&dcb->dcb_linkage);
1581 dcb->dcb_func(NULL, th, dcb, error);
1584 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1588 lu_context_exit(&th->th_ctx);
1589 lu_context_fini(&th->th_ctx);
1593 #ifndef HAVE_SB_START_WRITE
1594 # define sb_start_write(sb) do {} while (0)
1595 # define sb_end_write(sb) do {} while (0)
1598 static struct thandle *osd_trans_create(const struct lu_env *env,
1599 struct dt_device *d)
1601 struct osd_thread_info *oti = osd_oti_get(env);
1602 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1603 struct osd_thandle *oh;
1608 CERROR("%s: someone try to start transaction under "
1609 "readonly mode, should be disabled.\n",
1610 osd_name(osd_dt_dev(d)));
1612 RETURN(ERR_PTR(-EROFS));
1615 /* on pending IO in this thread should left from prev. request */
1616 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1618 sb_start_write(osd_sb(osd_dt_dev(d)));
1620 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1622 oh->ot_quota_trans = &oti->oti_quota_trans;
1623 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1627 th->th_tags = LCT_TX_HANDLE;
1629 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1630 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1633 memset(oti->oti_declare_ops, 0,
1634 sizeof(oti->oti_declare_ops));
1635 memset(oti->oti_declare_ops_cred, 0,
1636 sizeof(oti->oti_declare_ops_cred));
1637 memset(oti->oti_declare_ops_used, 0,
1638 sizeof(oti->oti_declare_ops_used));
1640 sb_end_write(osd_sb(osd_dt_dev(d)));
1641 th = ERR_PTR(-ENOMEM);
1646 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1648 struct osd_thread_info *oti = osd_oti_get(env);
1649 struct osd_thandle *oh;
1651 oh = container_of0(th, struct osd_thandle, ot_super);
1652 LASSERT(oh != NULL);
1654 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1655 oti->oti_declare_ops[OSD_OT_CREATE],
1656 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1657 oti->oti_declare_ops_used[OSD_OT_CREATE],
1658 oti->oti_declare_ops[OSD_OT_DESTROY],
1659 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1660 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1661 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1662 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1663 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1664 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1665 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1666 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1667 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1668 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1669 oti->oti_declare_ops[OSD_OT_WRITE],
1670 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1671 oti->oti_declare_ops_used[OSD_OT_WRITE],
1672 oti->oti_declare_ops[OSD_OT_PUNCH],
1673 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1674 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1675 oti->oti_declare_ops[OSD_OT_QUOTA],
1676 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1677 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1678 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1679 oti->oti_declare_ops[OSD_OT_INSERT],
1680 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1681 oti->oti_declare_ops_used[OSD_OT_INSERT],
1682 oti->oti_declare_ops[OSD_OT_DELETE],
1683 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1684 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1685 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1686 oti->oti_declare_ops[OSD_OT_REF_ADD],
1687 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1688 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1689 oti->oti_declare_ops[OSD_OT_REF_DEL],
1690 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1691 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1695 * Concurrency: shouldn't matter.
1697 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1700 struct osd_thread_info *oti = osd_oti_get(env);
1701 struct osd_device *dev = osd_dt_dev(d);
1703 struct osd_thandle *oh;
1708 LASSERT(current->journal_info == NULL);
1710 oh = container_of0(th, struct osd_thandle, ot_super);
1711 LASSERT(oh != NULL);
1712 LASSERT(oh->ot_handle == NULL);
1714 rc = dt_txn_hook_start(env, d, th);
1718 if (unlikely(osd_param_is_not_sane(dev, th))) {
1719 static unsigned long last_printed;
1720 static int last_credits;
1722 /* don't make noise on a tiny testing systems
1723 * actual credits misuse will be caught anyway */
1724 if (last_credits != oh->ot_credits &&
1725 time_after(jiffies, last_printed +
1726 msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
1727 osd_transaction_size(dev) > 512) {
1728 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1729 oh->ot_credits, osd_transaction_size(dev));
1730 osd_trans_dump_creds(env, th);
1731 libcfs_debug_dumpstack(NULL);
1732 last_credits = oh->ot_credits;
1733 last_printed = jiffies;
1735 /* XXX Limit the credits to 'max_transaction_buffers', and
1736 * let the underlying filesystem to catch the error if
1737 * we really need so many credits.
1739 * This should be removed when we can calculate the
1740 * credits precisely. */
1741 oh->ot_credits = osd_transaction_size(dev);
1742 } else if (ldiskfs_track_declares_assert != 0) {
1743 /* reserve few credits to prevent an assertion in JBD
1744 * our debugging mechanism will be able to detected
1745 * overuse. this can help to debug single-update
1747 oh->ot_credits += 10;
1748 if (unlikely(osd_param_is_not_sane(dev, th)))
1749 oh->ot_credits = osd_transaction_size(dev);
1753 * XXX temporary stuff. Some abstraction layer should
1756 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1760 LASSERT(oti->oti_txns == 0);
1761 lu_context_init(&th->th_ctx, th->th_tags);
1762 lu_context_enter(&th->th_ctx);
1764 lu_device_get(&d->dd_lu_dev);
1765 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1776 static int osd_seq_exists(const struct lu_env *env,
1777 struct osd_device *osd, u64 seq)
1779 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1780 struct seq_server_site *ss = osd_seq_site(osd);
1784 LASSERT(ss != NULL);
1785 LASSERT(ss->ss_server_fld != NULL);
1787 rc = osd_fld_lookup(env, osd, seq, range);
1790 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
1791 osd_name(osd), seq, rc);
1795 RETURN(ss->ss_node_id == range->lsr_index);
1798 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1800 struct dt_txn_commit_cb *dcb;
1801 struct dt_txn_commit_cb *tmp;
1803 /* call per-transaction stop callbacks if any */
1804 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1806 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1807 "commit callback entry: magic=%x name='%s'\n",
1808 dcb->dcb_magic, dcb->dcb_name);
1809 list_del_init(&dcb->dcb_linkage);
1810 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1815 * Concurrency: shouldn't matter.
1817 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1820 int rc = 0, remove_agents = 0;
1821 struct osd_thandle *oh;
1822 struct osd_thread_info *oti = osd_oti_get(env);
1823 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1824 struct osd_device *osd = osd_dt_dev(th->th_dev);
1825 struct qsd_instance *qsd = osd->od_quota_slave;
1826 struct lquota_trans *qtrans;
1829 oh = container_of0(th, struct osd_thandle, ot_super);
1831 /* reset OI cache for safety */
1832 oti->oti_ins_cache_used = 0;
1834 remove_agents = oh->ot_remove_agents;
1836 qtrans = oh->ot_quota_trans;
1837 oh->ot_quota_trans = NULL;
1839 if (oh->ot_handle != NULL) {
1841 handle_t *hdl = oh->ot_handle;
1844 * add commit callback
1845 * notice we don't do this in osd_trans_start()
1846 * as underlying transaction can change during truncate
1848 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1851 LASSERT(oti->oti_txns == 1);
1854 rc = dt_txn_hook_stop(env, th);
1856 CERROR("%s: failed in transaction hook: rc = %d\n",
1859 osd_trans_stop_cb(oh, rc);
1860 /* hook functions might modify th_sync */
1861 hdl->h_sync = th->th_sync;
1863 oh->ot_handle = NULL;
1864 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
1866 CERROR("%s: failed to stop transaction: rc = %d\n",
1867 osd_name(osd), rc2);
1871 osd_trans_stop_cb(oh, th->th_result);
1875 /* inform the quota slave device that the transaction is stopping */
1876 qsd_op_end(env, qsd, qtrans);
1878 /* as we want IO to journal and data IO be concurrent, we don't block
1879 * awaiting data IO completion in osd_do_bio(), instead we wait here
1880 * once transaction is submitted to the journal. all reqular requests
1881 * don't do direct IO (except read/write), thus this wait_event becomes
1884 * IMPORTANT: we have to wait till any IO submited by the thread is
1885 * completed otherwise iobuf may be corrupted by different request
1887 wait_event(iobuf->dr_wait,
1888 atomic_read(&iobuf->dr_numreqs) == 0);
1889 osd_fini_iobuf(osd, iobuf);
1891 rc = iobuf->dr_error;
1893 if (unlikely(remove_agents != 0))
1894 osd_process_scheduled_agent_removals(env, osd);
1896 sb_end_write(osd_sb(osd));
1901 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1903 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1906 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1907 LASSERT(&dcb->dcb_func != NULL);
1908 if (dcb->dcb_flags & DCB_TRANS_STOP)
1909 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1911 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1917 * Called just before object is freed. Releases all resources except for
1918 * object itself (that is released by osd_object_free()).
1920 * Concurrency: no concurrent access is possible that late in object
1923 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1925 struct osd_object *obj = osd_obj(l);
1926 struct inode *inode = obj->oo_inode;
1928 LINVRNT(osd_invariant(obj));
1931 * If object is unlinked remove fid->ino mapping from object index.
1934 osd_index_fini(obj);
1935 if (inode != NULL) {
1936 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1937 qid_t uid = i_uid_read(inode);
1938 qid_t gid = i_gid_read(inode);
1941 obj->oo_inode = NULL;
1944 struct osd_thread_info *info = osd_oti_get(env);
1945 struct lquota_id_info *qi = &info->oti_qi;
1947 /* Release granted quota to master if necessary */
1948 qi->lqi_id.qid_uid = uid;
1949 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1951 qi->lqi_id.qid_uid = gid;
1952 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1958 * Concurrency: ->loo_object_release() is called under site spin-lock.
1960 static void osd_object_release(const struct lu_env *env,
1961 struct lu_object *l)
1963 struct osd_object *o = osd_obj(l);
1964 /* nobody should be releasing a non-destroyed object with nlink=0
1965 * the API allows this, but ldiskfs doesn't like and then report
1966 * this inode as deleted */
1967 if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
1972 * Concurrency: shouldn't matter.
1974 static int osd_object_print(const struct lu_env *env, void *cookie,
1975 lu_printer_t p, const struct lu_object *l)
1977 struct osd_object *o = osd_obj(l);
1978 struct iam_descr *d;
1980 if (o->oo_dir != NULL)
1981 d = o->oo_dir->od_container.ic_descr;
1984 return (*p)(env, cookie,
1985 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1987 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1988 o->oo_inode ? o->oo_inode->i_generation : 0,
1989 d ? d->id_ops->id_name : "plain");
1993 * Concurrency: shouldn't matter.
1995 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1996 struct obd_statfs *sfs)
1998 struct osd_device *osd = osd_dt_dev(d);
1999 struct super_block *sb = osd_sb(osd);
2000 struct kstatfs *ksfs;
2004 if (unlikely(osd->od_mnt == NULL))
2005 return -EINPROGRESS;
2007 /* osd_lproc.c call this without env, allocate ksfs for that case */
2008 if (unlikely(env == NULL)) {
2009 OBD_ALLOC_PTR(ksfs);
2013 ksfs = &osd_oti_get(env)->oti_ksfs;
2016 result = sb->s_op->statfs(sb->s_root, ksfs);
2020 statfs_pack(sfs, ksfs);
2021 if (unlikely(sb->s_flags & MS_RDONLY))
2022 sfs->os_state |= OS_STATE_READONLY;
2023 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
2024 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2025 sfs->os_maxbytes = sb->s_maxbytes;
2027 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2030 * Reserve some space so to avoid fragmenting the filesystem too much.
2031 * Fragmentation not only impacts performance, but can also increase
2032 * metadata overhead significantly, causing grant calculation to be
2035 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2037 CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
2038 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2039 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2040 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2042 sfs->os_blocks -= reserved;
2043 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2044 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2047 if (unlikely(env == NULL))
2053 * Estimate space needed for file creations. We assume the largest filename
2054 * which is 2^64 - 1, hence a filename of 20 chars.
2055 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2057 #ifdef __LDISKFS_DIR_REC_LEN
2058 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2060 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2064 * Concurrency: doesn't access mutable data.
2066 static void osd_conf_get(const struct lu_env *env,
2067 const struct dt_device *dev,
2068 struct dt_device_param *param)
2070 struct super_block *sb = osd_sb(osd_dt_dev(dev));
2074 * XXX should be taken from not-yet-existing fs abstraction layer.
2076 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2077 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2078 param->ddp_symlink_max = sb->s_blocksize;
2079 param->ddp_mount_type = LDD_MT_LDISKFS;
2080 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2081 param->ddp_maxbytes = sb->s_maxbytes;
2083 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2084 /* inode are statically allocated, so per-inode space consumption
2085 * is the space consumed by the directory entry */
2086 param->ddp_inodespace = PER_OBJ_USAGE;
2087 /* EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2088 * = 128MB) which is unlikely to be hit in real life. Report a smaller
2089 * maximum length to not under count the actual number of extents
2090 * needed for writing a file. */
2091 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
2092 /* worst-case extent insertion metadata overhead */
2093 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2094 param->ddp_mntopts = 0;
2095 if (test_opt(sb, XATTR_USER))
2096 param->ddp_mntopts |= MNTOPT_USERXATTR;
2097 if (test_opt(sb, POSIX_ACL))
2098 param->ddp_mntopts |= MNTOPT_ACL;
2100 /* LOD might calculate the max stripe count based on max_ea_size,
2101 * so we need take account in the overhead as well,
2102 * xattr_header + magic + xattr_entry_head */
2103 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2104 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2106 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2107 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
2108 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2112 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2116 * Concurrency: shouldn't matter.
2118 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2122 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2124 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
2126 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
2127 LUSTRE_OSD_LDISKFS_NAME, rc);
2133 * Start commit for OSD device.
2135 * An implementation of dt_commit_async method for OSD device.
2136 * Asychronously starts underlayng fs sync and thereby a transaction
2139 * \param env environment
2140 * \param d dt device
2142 * \see dt_device_operations
2144 static int osd_commit_async(const struct lu_env *env,
2145 struct dt_device *d)
2147 struct super_block *s = osd_sb(osd_dt_dev(d));
2150 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2151 RETURN(s->s_op->sync_fs(s, 0));
2155 * Concurrency: shouldn't matter.
2158 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2160 struct super_block *sb = osd_sb(osd_dt_dev(d));
2161 struct block_device *dev = sb->s_bdev;
2162 #ifdef HAVE_DEV_SET_RDONLY
2163 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
2166 int rc = -EOPNOTSUPP;
2170 #ifdef HAVE_DEV_SET_RDONLY
2171 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
2173 if (sb->s_op->freeze_fs) {
2174 rc = sb->s_op->freeze_fs(sb);
2179 if (jdev && (jdev != dev)) {
2180 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
2182 dev_set_rdonly(jdev);
2184 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
2185 dev_set_rdonly(dev);
2187 if (sb->s_op->unfreeze_fs)
2188 sb->s_op->unfreeze_fs(sb);
2193 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2194 osd_dt_dev(d)->od_svname, (long)dev, rc);
2200 * Note: we do not count into QUOTA here.
2201 * If we mount with --data_journal we may need more.
2203 const int osd_dto_credits_noquota[DTO_NR] = {
2206 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2207 * SINGLEDATA_TRANS_BLOCKS(8)
2208 * XXX Note: maybe iam need more, since iam have more level than
2211 [DTO_INDEX_INSERT] = 16,
2214 * just modify a single entry, probably merge few within a block
2216 [DTO_INDEX_DELETE] = 1,
2220 [DTO_INDEX_UPDATE] = 16,
2222 * 4(inode, inode bits, groups, GDT)
2223 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2225 [DTO_OBJECT_CREATE] = 4,
2227 * 4(inode, inode bits, groups, GDT)
2228 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2230 [DTO_OBJECT_DELETE] = 4,
2232 * Attr set credits (inode)
2234 [DTO_ATTR_SET_BASE] = 1,
2236 * Xattr set. The same as xattr of EXT3.
2237 * DATA_TRANS_BLOCKS(14)
2238 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2239 * are also counted in. Do not know why?
2241 [DTO_XATTR_SET] = 14,
2243 * credits for inode change during write.
2245 [DTO_WRITE_BASE] = 3,
2247 * credits for single block write.
2249 [DTO_WRITE_BLOCK] = 14,
2251 * Attr set credits for chown.
2252 * This is extra credits for setattr, and it is null without quota
2254 [DTO_ATTR_SET_CHOWN] = 0
2257 static const struct dt_device_operations osd_dt_ops = {
2258 .dt_root_get = osd_root_get,
2259 .dt_statfs = osd_statfs,
2260 .dt_trans_create = osd_trans_create,
2261 .dt_trans_start = osd_trans_start,
2262 .dt_trans_stop = osd_trans_stop,
2263 .dt_trans_cb_add = osd_trans_cb_add,
2264 .dt_conf_get = osd_conf_get,
2265 .dt_sync = osd_sync,
2267 .dt_commit_async = osd_commit_async,
2270 static void osd_object_read_lock(const struct lu_env *env,
2271 struct dt_object *dt, unsigned role)
2273 struct osd_object *obj = osd_dt_obj(dt);
2274 struct osd_thread_info *oti = osd_oti_get(env);
2276 LINVRNT(osd_invariant(obj));
2278 LASSERT(obj->oo_owner != env);
2279 down_read_nested(&obj->oo_sem, role);
2281 LASSERT(obj->oo_owner == NULL);
2285 static void osd_object_write_lock(const struct lu_env *env,
2286 struct dt_object *dt, unsigned role)
2288 struct osd_object *obj = osd_dt_obj(dt);
2289 struct osd_thread_info *oti = osd_oti_get(env);
2291 LINVRNT(osd_invariant(obj));
2293 LASSERT(obj->oo_owner != env);
2294 down_write_nested(&obj->oo_sem, role);
2296 LASSERT(obj->oo_owner == NULL);
2297 obj->oo_owner = env;
2301 static void osd_object_read_unlock(const struct lu_env *env,
2302 struct dt_object *dt)
2304 struct osd_object *obj = osd_dt_obj(dt);
2305 struct osd_thread_info *oti = osd_oti_get(env);
2307 LINVRNT(osd_invariant(obj));
2309 LASSERT(oti->oti_r_locks > 0);
2311 up_read(&obj->oo_sem);
2314 static void osd_object_write_unlock(const struct lu_env *env,
2315 struct dt_object *dt)
2317 struct osd_object *obj = osd_dt_obj(dt);
2318 struct osd_thread_info *oti = osd_oti_get(env);
2320 LINVRNT(osd_invariant(obj));
2322 LASSERT(obj->oo_owner == env);
2323 LASSERT(oti->oti_w_locks > 0);
2325 obj->oo_owner = NULL;
2326 up_write(&obj->oo_sem);
2329 static int osd_object_write_locked(const struct lu_env *env,
2330 struct dt_object *dt)
2332 struct osd_object *obj = osd_dt_obj(dt);
2334 LINVRNT(osd_invariant(obj));
2336 return obj->oo_owner == env;
2339 static struct timespec *osd_inode_time(const struct lu_env *env,
2340 struct inode *inode, __u64 seconds)
2342 struct osd_thread_info *oti = osd_oti_get(env);
2343 struct timespec *t = &oti->oti_time;
2345 t->tv_sec = seconds;
2347 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
2351 static void osd_inode_getattr(const struct lu_env *env,
2352 struct inode *inode, struct lu_attr *attr)
2354 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2355 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2356 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
2359 attr->la_atime = LTIME_S(inode->i_atime);
2360 attr->la_mtime = LTIME_S(inode->i_mtime);
2361 attr->la_ctime = LTIME_S(inode->i_ctime);
2362 attr->la_mode = inode->i_mode;
2363 attr->la_size = i_size_read(inode);
2364 attr->la_blocks = inode->i_blocks;
2365 attr->la_uid = i_uid_read(inode);
2366 attr->la_gid = i_gid_read(inode);
2367 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2368 attr->la_nlink = inode->i_nlink;
2369 attr->la_rdev = inode->i_rdev;
2370 attr->la_blksize = 1 << inode->i_blkbits;
2371 attr->la_blkbits = inode->i_blkbits;
2374 static int osd_attr_get(const struct lu_env *env,
2375 struct dt_object *dt,
2376 struct lu_attr *attr)
2378 struct osd_object *obj = osd_dt_obj(dt);
2380 if (unlikely(!dt_object_exists(dt)))
2382 if (unlikely(obj->oo_destroyed))
2385 LASSERT(!dt_object_remote(dt));
2386 LINVRNT(osd_invariant(obj));
2388 spin_lock(&obj->oo_guard);
2389 osd_inode_getattr(env, obj->oo_inode, attr);
2390 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
2391 attr->la_flags |= LUSTRE_ORPHAN_FL;
2392 spin_unlock(&obj->oo_guard);
2397 static int osd_declare_attr_set(const struct lu_env *env,
2398 struct dt_object *dt,
2399 const struct lu_attr *attr,
2400 struct thandle *handle)
2402 struct osd_thandle *oh;
2403 struct osd_object *obj;
2404 struct osd_thread_info *info = osd_oti_get(env);
2405 struct lquota_id_info *qi = &info->oti_qi;
2413 LASSERT(dt != NULL);
2414 LASSERT(handle != NULL);
2416 obj = osd_dt_obj(dt);
2417 LASSERT(osd_invariant(obj));
2419 oh = container_of0(handle, struct osd_thandle, ot_super);
2420 LASSERT(oh->ot_handle == NULL);
2422 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2423 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2425 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2426 osd_dto_credits_noquota[DTO_XATTR_SET]);
2428 if (attr == NULL || obj->oo_inode == NULL)
2431 bspace = obj->oo_inode->i_blocks;
2432 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2433 bspace = toqb(bspace);
2435 /* Changing ownership is always preformed by super user, it should not
2438 * We still need to call the osd_declare_qid() to calculate the journal
2439 * credits for updating quota accounting files and to trigger quota
2440 * space adjustment once the operation is completed.*/
2441 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2443 uid = i_uid_read(obj->oo_inode);
2444 qi->lqi_type = USRQUOTA;
2445 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2446 /* inode accounting */
2447 qi->lqi_is_blk = false;
2449 /* one more inode for the new uid ... */
2450 qi->lqi_id.qid_uid = attr->la_uid;
2452 /* Reserve credits for the new uid */
2453 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2454 if (rc == -EDQUOT || rc == -EINPROGRESS)
2459 /* and one less inode for the current uid */
2460 qi->lqi_id.qid_uid = uid;
2462 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2463 if (rc == -EDQUOT || rc == -EINPROGRESS)
2468 /* block accounting */
2469 qi->lqi_is_blk = true;
2471 /* more blocks for the new uid ... */
2472 qi->lqi_id.qid_uid = attr->la_uid;
2473 qi->lqi_space = bspace;
2475 * Credits for the new uid has been reserved, re-use "obj"
2476 * to save credit reservation.
2478 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2479 if (rc == -EDQUOT || rc == -EINPROGRESS)
2484 /* and finally less blocks for the current uid */
2485 qi->lqi_id.qid_uid = uid;
2486 qi->lqi_space = -bspace;
2487 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2488 if (rc == -EDQUOT || rc == -EINPROGRESS)
2494 gid = i_gid_read(obj->oo_inode);
2495 qi->lqi_type = GRPQUOTA;
2496 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2498 /* inode accounting */
2499 qi->lqi_is_blk = false;
2501 /* one more inode for the new gid ... */
2502 qi->lqi_id.qid_gid = attr->la_gid;
2504 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2505 if (rc == -EDQUOT || rc == -EINPROGRESS)
2510 /* and one less inode for the current gid */
2511 qi->lqi_id.qid_gid = gid;
2513 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2514 if (rc == -EDQUOT || rc == -EINPROGRESS)
2519 /* block accounting */
2520 qi->lqi_is_blk = true;
2522 /* more blocks for the new gid ... */
2523 qi->lqi_id.qid_gid = attr->la_gid;
2524 qi->lqi_space = bspace;
2525 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2526 if (rc == -EDQUOT || rc == -EINPROGRESS)
2531 /* and finally less blocks for the current gid */
2532 qi->lqi_id.qid_gid = gid;
2533 qi->lqi_space = -bspace;
2534 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2535 if (rc == -EDQUOT || rc == -EINPROGRESS)
2544 static int osd_inode_setattr(const struct lu_env *env,
2545 struct inode *inode, const struct lu_attr *attr)
2547 __u64 bits = attr->la_valid;
2549 /* Only allow set size for regular file */
2550 if (!S_ISREG(inode->i_mode))
2551 bits &= ~(LA_SIZE | LA_BLOCKS);
2556 if (bits & LA_ATIME)
2557 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2558 if (bits & LA_CTIME)
2559 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2560 if (bits & LA_MTIME)
2561 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2562 if (bits & LA_SIZE) {
2563 spin_lock(&inode->i_lock);
2564 LDISKFS_I(inode)->i_disksize = attr->la_size;
2565 i_size_write(inode, attr->la_size);
2566 spin_unlock(&inode->i_lock);
2569 /* OSD should not change "i_blocks" which is used by quota.
2570 * "i_blocks" should be changed by ldiskfs only. */
2572 inode->i_mode = (inode->i_mode & S_IFMT) |
2573 (attr->la_mode & ~S_IFMT);
2575 i_uid_write(inode, attr->la_uid);
2577 i_gid_write(inode, attr->la_gid);
2578 if (bits & LA_NLINK)
2579 set_nlink(inode, attr->la_nlink);
2581 inode->i_rdev = attr->la_rdev;
2583 if (bits & LA_FLAGS) {
2584 /* always keep S_NOCMTIME */
2585 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2591 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2593 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2594 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2598 ll_vfs_dq_init(inode);
2600 if (attr->la_valid & LA_UID)
2601 iattr.ia_valid |= ATTR_UID;
2602 if (attr->la_valid & LA_GID)
2603 iattr.ia_valid |= ATTR_GID;
2604 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2605 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2607 rc = ll_vfs_dq_transfer(inode, &iattr);
2609 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2610 "enforcement enabled on the ldiskfs "
2611 "filesystem?\n", inode->i_sb->s_id, rc);
2618 static int osd_attr_set(const struct lu_env *env,
2619 struct dt_object *dt,
2620 const struct lu_attr *attr,
2621 struct thandle *handle)
2623 struct osd_object *obj = osd_dt_obj(dt);
2624 struct inode *inode;
2627 if (!dt_object_exists(dt))
2630 LASSERT(handle != NULL);
2631 LASSERT(!dt_object_remote(dt));
2632 LASSERT(osd_invariant(obj));
2634 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2636 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2637 struct osd_thread_info *oti = osd_oti_get(env);
2638 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2639 struct lu_fid *fid1 = &oti->oti_fid;
2640 struct osd_inode_id *id = &oti->oti_id;
2641 struct iam_path_descr *ipd;
2642 struct iam_container *bag;
2643 struct osd_thandle *oh;
2646 fid_cpu_to_be(fid1, fid0);
2647 memset(id, 1, sizeof(*id));
2648 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2649 fid0)->oi_dir.od_container;
2650 ipd = osd_idx_ipd_get(env, bag);
2651 if (unlikely(ipd == NULL))
2654 oh = container_of0(handle, struct osd_thandle, ot_super);
2655 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2656 (const struct iam_rec *)id, ipd);
2657 osd_ipd_put(env, bag, ipd);
2658 return(rc > 0 ? 0 : rc);
2661 inode = obj->oo_inode;
2663 rc = osd_quota_transfer(inode, attr);
2667 spin_lock(&obj->oo_guard);
2668 rc = osd_inode_setattr(env, inode, attr);
2669 spin_unlock(&obj->oo_guard);
2673 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2675 if (!(attr->la_valid & LA_FLAGS))
2678 /* Let's check if there are extra flags need to be set into LMA */
2679 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2680 struct osd_thread_info *info = osd_oti_get(env);
2681 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2683 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
2687 lma->lma_incompat |=
2688 lustre_to_lma_flags(attr->la_flags);
2689 lustre_lma_swab(lma);
2690 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2691 lma, sizeof(*lma), XATTR_REPLACE);
2693 struct osd_device *osd = osd_obj2dev(obj);
2695 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2696 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2697 lma->lma_incompat, rc);
2700 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2702 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2705 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2710 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2711 struct osd_object *obj,
2712 const char *name, const int namelen)
2714 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2717 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2718 umode_t mode, struct dt_allocation_hint *hint,
2722 struct osd_device *osd = osd_obj2dev(obj);
2723 struct osd_thandle *oth;
2724 struct dt_object *parent = NULL;
2725 struct inode *inode;
2727 LINVRNT(osd_invariant(obj));
2728 LASSERT(obj->oo_inode == NULL);
2729 LASSERT(obj->oo_hl_head == NULL);
2731 if (S_ISDIR(mode) && ldiskfs_pdo) {
2732 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2733 if (obj->oo_hl_head == NULL)
2737 oth = container_of(th, struct osd_thandle, ot_super);
2738 LASSERT(oth->ot_handle->h_transaction != NULL);
2740 if (hint != NULL && hint->dah_parent != NULL &&
2741 !dt_object_remote(hint->dah_parent))
2742 parent = hint->dah_parent;
2744 inode = ldiskfs_create_inode(oth->ot_handle,
2745 parent ? osd_dt_obj(parent)->oo_inode :
2746 osd_sb(osd)->s_root->d_inode,
2748 if (!IS_ERR(inode)) {
2749 /* Do not update file c/mtime in ldiskfs. */
2750 inode->i_flags |= S_NOCMTIME;
2752 /* For new created object, it must be consistent,
2753 * and it is unnecessary to scrub against it. */
2754 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2756 obj->oo_inode = inode;
2759 if (obj->oo_hl_head != NULL) {
2760 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2761 obj->oo_hl_head = NULL;
2763 result = PTR_ERR(inode);
2765 LINVRNT(osd_invariant(obj));
2773 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2774 struct lu_attr *attr,
2775 struct dt_allocation_hint *hint,
2776 struct dt_object_format *dof,
2780 struct osd_thandle *oth;
2781 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
2783 LASSERT(S_ISDIR(attr->la_mode));
2785 oth = container_of(th, struct osd_thandle, ot_super);
2786 LASSERT(oth->ot_handle->h_transaction != NULL);
2787 result = osd_mkfile(info, obj, mode, hint, th);
2792 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2793 struct lu_attr *attr,
2794 struct dt_allocation_hint *hint,
2795 struct dt_object_format *dof,
2799 struct osd_thandle *oth;
2800 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2802 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2804 LASSERT(S_ISREG(attr->la_mode));
2806 oth = container_of(th, struct osd_thandle, ot_super);
2807 LASSERT(oth->ot_handle->h_transaction != NULL);
2809 result = osd_mkfile(info, obj, mode, hint, th);
2811 LASSERT(obj->oo_inode != NULL);
2812 if (feat->dif_flags & DT_IND_VARKEY)
2813 result = iam_lvar_create(obj->oo_inode,
2814 feat->dif_keysize_max,
2816 feat->dif_recsize_max,
2819 result = iam_lfix_create(obj->oo_inode,
2820 feat->dif_keysize_max,
2822 feat->dif_recsize_max,
2829 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2830 struct lu_attr *attr,
2831 struct dt_allocation_hint *hint,
2832 struct dt_object_format *dof,
2835 LASSERT(S_ISREG(attr->la_mode));
2836 return osd_mkfile(info, obj, (attr->la_mode &
2837 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2840 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2841 struct lu_attr *attr,
2842 struct dt_allocation_hint *hint,
2843 struct dt_object_format *dof,
2846 LASSERT(S_ISLNK(attr->la_mode));
2847 return osd_mkfile(info, obj, (attr->la_mode &
2848 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2851 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2852 struct lu_attr *attr,
2853 struct dt_allocation_hint *hint,
2854 struct dt_object_format *dof,
2857 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2860 LINVRNT(osd_invariant(obj));
2861 LASSERT(obj->oo_inode == NULL);
2862 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2863 S_ISFIFO(mode) || S_ISSOCK(mode));
2865 result = osd_mkfile(info, obj, mode, hint, th);
2867 LASSERT(obj->oo_inode != NULL);
2869 * This inode should be marked dirty for i_rdev. Currently
2870 * that is done in the osd_attr_init().
2872 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2875 LINVRNT(osd_invariant(obj));
2879 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2881 struct dt_allocation_hint *hint,
2882 struct dt_object_format *dof,
2885 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2887 osd_obj_type_f result;
2903 result = osd_mk_index;
2914 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2915 struct dt_object *parent, struct dt_object *child,
2920 ah->dah_parent = parent;
2921 ah->dah_mode = child_mode;
2923 if (parent != NULL && !dt_object_remote(parent)) {
2924 /* will help to find FID->ino at dt_insert("..") */
2925 struct osd_object *pobj = osd_dt_obj(parent);
2926 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2930 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2931 struct lu_attr *attr, struct dt_object_format *dof)
2933 struct inode *inode = obj->oo_inode;
2934 __u64 valid = attr->la_valid;
2937 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2939 if (dof->dof_type != DFT_NODE)
2940 attr->la_valid &= ~LA_RDEV;
2941 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2942 attr->la_valid &= ~LA_ATIME;
2943 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2944 attr->la_valid &= ~LA_CTIME;
2945 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2946 attr->la_valid &= ~LA_MTIME;
2948 result = osd_quota_transfer(inode, attr);
2952 if (attr->la_valid != 0) {
2953 result = osd_inode_setattr(info->oti_env, inode, attr);
2955 * The osd_inode_setattr() should always succeed here. The
2956 * only error that could be returned is EDQUOT when we are
2957 * trying to change the UID or GID of the inode. However, this
2958 * should not happen since quota enforcement is no longer
2959 * enabled on ldiskfs (lquota takes care of it).
2961 LASSERTF(result == 0, "%d\n", result);
2962 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2965 attr->la_valid = valid;
2969 * Helper function for osd_object_create()
2971 * \retval 0, on success
2973 static int __osd_object_create(struct osd_thread_info *info,
2974 struct osd_object *obj, struct lu_attr *attr,
2975 struct dt_allocation_hint *hint,
2976 struct dt_object_format *dof,
2982 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2984 /* we drop umask so that permissions we pass are not affected */
2985 umask = current->fs->umask;
2986 current->fs->umask = 0;
2988 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2990 if (likely(obj->oo_inode != NULL)) {
2991 LASSERT(obj->oo_inode->i_state & I_NEW);
2993 /* Unlock the inode before attr initialization to avoid
2994 * unnecessary dqget operations. LU-6378 */
2995 unlock_new_inode(obj->oo_inode);
2998 if (likely(result == 0)) {
2999 osd_attr_init(info, obj, attr, dof);
3000 osd_object_init0(obj);
3003 /* restore previous umask value */
3004 current->fs->umask = umask;
3006 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
3012 * Helper function for osd_object_create()
3014 * \retval 0, on success
3016 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
3017 const struct lu_fid *fid, struct thandle *th)
3019 struct osd_thread_info *info = osd_oti_get(env);
3020 struct osd_inode_id *id = &info->oti_id;
3021 struct osd_device *osd = osd_obj2dev(obj);
3022 struct osd_thandle *oh;
3025 LASSERT(obj->oo_inode != NULL);
3027 oh = container_of0(th, struct osd_thandle, ot_super);
3028 LASSERT(oh->ot_handle);
3029 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3031 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3032 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3033 OI_CHECK_FLD, NULL);
3034 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3039 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3040 u64 seq, struct lu_seq_range *range)
3042 struct seq_server_site *ss = osd_seq_site(osd);
3044 if (fid_seq_is_idif(seq)) {
3045 fld_range_set_ost(range);
3046 range->lsr_index = idif_ost_idx(seq);
3050 if (!fid_seq_in_fldb(seq)) {
3051 fld_range_set_mdt(range);
3053 /* FIXME: If ss is NULL, it suppose not get lsr_index
3055 range->lsr_index = ss->ss_node_id;
3059 LASSERT(ss != NULL);
3060 fld_range_set_any(range);
3061 /* OSD will only do local fld lookup */
3062 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3066 * Concurrency: no external locking is necessary.
3068 static int osd_declare_object_create(const struct lu_env *env,
3069 struct dt_object *dt,
3070 struct lu_attr *attr,
3071 struct dt_allocation_hint *hint,
3072 struct dt_object_format *dof,
3073 struct thandle *handle)
3075 struct osd_thandle *oh;
3079 LASSERT(handle != NULL);
3081 oh = container_of0(handle, struct osd_thandle, ot_super);
3082 LASSERT(oh->ot_handle == NULL);
3084 /* EA object consumes more credits than regular object: osd_mk_index
3085 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3086 * leaf_node, could involves the block, block bitmap, groups, GDT
3087 * change for each block, so add 4 * 2 credits in that case. */
3088 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3089 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3090 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3091 /* Reuse idle OI block may cause additional one OI block
3093 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3094 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3099 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
3100 osd_dt_obj(dt), false, NULL, false);
3104 /* will help to find FID->ino mapping at dt_insert() */
3105 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3111 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
3112 struct lu_attr *attr,
3113 struct dt_allocation_hint *hint,
3114 struct dt_object_format *dof, struct thandle *th)
3116 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3117 struct osd_object *obj = osd_dt_obj(dt);
3118 struct osd_thread_info *info = osd_oti_get(env);
3122 if (dt_object_exists(dt))
3125 LINVRNT(osd_invariant(obj));
3126 LASSERT(!dt_object_remote(dt));
3127 LASSERT(osd_write_locked(env, obj));
3128 LASSERT(th != NULL);
3130 if (unlikely(fid_is_acct(fid)))
3131 /* Quota files can't be created from the kernel any more,
3132 * 'tune2fs -O quota' will take care of creating them */
3135 result = __osd_object_create(info, obj, attr, hint, dof, th);
3137 result = __osd_oi_insert(env, obj, fid, th);
3138 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3139 obj->oo_dt.do_body_ops = &osd_body_ops;
3141 LASSERT(ergo(result == 0,
3142 dt_object_exists(dt) && !dt_object_remote(dt)));
3144 LASSERT(osd_invariant(obj));
3149 * Called to destroy on-disk representation of the object
3151 * Concurrency: must be locked
3153 static int osd_declare_object_destroy(const struct lu_env *env,
3154 struct dt_object *dt,
3157 struct osd_object *obj = osd_dt_obj(dt);
3158 struct inode *inode = obj->oo_inode;
3159 struct osd_thandle *oh;
3166 oh = container_of0(th, struct osd_thandle, ot_super);
3167 LASSERT(oh->ot_handle == NULL);
3169 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3170 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3171 /* Recycle idle OI leaf may cause additional three OI blocks
3173 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3174 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3175 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3176 /* one less inode */
3177 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3178 -1, oh, obj, false, NULL, false);
3181 /* data to be truncated */
3182 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3183 0, oh, obj, true, NULL, false);
3187 /* will help to find FID->ino when this object is being
3188 * added to PENDING/ */
3189 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3194 static int osd_object_destroy(const struct lu_env *env,
3195 struct dt_object *dt,
3198 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3199 struct osd_object *obj = osd_dt_obj(dt);
3200 struct inode *inode = obj->oo_inode;
3201 struct osd_device *osd = osd_obj2dev(obj);
3202 struct osd_thandle *oh;
3206 oh = container_of0(th, struct osd_thandle, ot_super);
3207 LASSERT(oh->ot_handle);
3209 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3211 if (unlikely(fid_is_acct(fid)))
3214 if (S_ISDIR(inode->i_mode)) {
3215 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
3216 inode->i_nlink == 2);
3217 /* it will check/delete the inode from remote parent,
3218 * how to optimize it? unlink performance impaction XXX */
3219 result = osd_delete_from_remote_parent(env, osd, obj, oh);
3221 CERROR("%s: delete inode "DFID": rc = %d\n",
3222 osd_name(osd), PFID(fid), result);
3224 spin_lock(&obj->oo_guard);
3226 spin_unlock(&obj->oo_guard);
3227 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3230 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3232 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3234 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3235 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3236 oh->ot_handle, OI_CHECK_FLD);
3238 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3239 /* XXX: add to ext3 orphan list */
3240 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3242 /* not needed in the cache anymore */
3243 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3244 obj->oo_destroyed = 1;
3250 * Put the fid into lustre_mdt_attrs, and then place the structure
3251 * inode's ea. This fid should not be altered during the life time
3254 * \retval +ve, on success
3255 * \retval -ve, on error
3257 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3259 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3260 const struct lu_fid *fid, __u32 compat, __u32 incompat)
3262 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3266 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3269 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3272 lustre_lma_init(lma, fid, compat, incompat);
3273 lustre_lma_swab(lma);
3275 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
3277 /* LMA may already exist, but we need to check that all the
3278 * desired compat/incompat flags have been added. */
3279 if (unlikely(rc == -EEXIST)) {
3280 if (compat == 0 && incompat == 0)
3283 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3284 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
3289 lustre_lma_swab(lma);
3290 if (!(~lma->lma_compat & compat) &&
3291 !(~lma->lma_incompat & incompat))
3294 lma->lma_compat |= compat;
3295 lma->lma_incompat |= incompat;
3296 lustre_lma_swab(lma);
3297 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3298 sizeof(*lma), XATTR_REPLACE);
3305 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3306 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3307 * To have compatilibility with 1.8 ldiskfs driver we need to have
3308 * magic number at start of fid data.
3309 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3312 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3313 const struct lu_fid *fid)
3315 if (!fid_is_namespace_visible(fid) ||
3316 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3317 param->edp_magic = 0;
3321 param->edp_magic = LDISKFS_LUFID_MAGIC;
3322 param->edp_len = sizeof(struct lu_fid) + 1;
3323 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3327 * Try to read the fid from inode ea into dt_rec.
3329 * \param fid object fid.
3331 * \retval 0 on success
3333 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3334 __u32 ino, struct lu_fid *fid,
3335 struct osd_inode_id *id)
3337 struct osd_thread_info *info = osd_oti_get(env);
3338 struct inode *inode;
3341 osd_id_gen(id, ino, OSD_OII_NOGEN);
3342 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3344 RETURN(PTR_ERR(inode));
3350 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3352 struct inode *parent_dir,
3353 const struct lu_fid *dot_fid,
3354 const struct lu_fid *dot_dot_fid,
3355 struct osd_thandle *oth)
3357 struct ldiskfs_dentry_param *dot_ldp;
3358 struct ldiskfs_dentry_param *dot_dot_ldp;
3359 __u32 saved_nlink = dir->i_nlink;
3362 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3363 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3365 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3366 dot_ldp->edp_magic = 0;
3368 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3369 dir, dot_ldp, dot_dot_ldp);
3370 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3371 * the subseqent ref_add() will increase the dir->i_nlink
3372 * as 3. That is incorrect for new created directory.
3374 * It looks like hack, because we want to make the OSD API
3375 * to be order-independent for new created directory object
3376 * between dt_insert(..) and ref_add() operations.
3378 * Here, we only restore the in-RAM dir-inode's nlink attr,
3379 * becuase if the nlink attr is not 2, then there will be
3380 * ref_add() called following the dt_insert(..), such call
3381 * will make both the in-RAM and on-disk dir-inode's nlink
3382 * attr to be set as 2. LU-7447 */
3383 set_nlink(dir, saved_nlink);
3388 * Create an local agent inode for remote entry
3390 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3391 struct osd_device *osd,
3392 struct osd_object *pobj,
3393 const struct lu_fid *fid,
3397 struct osd_thread_info *info = osd_oti_get(env);
3398 struct inode *local;
3399 struct osd_thandle *oh;
3404 oh = container_of(th, struct osd_thandle, ot_super);
3405 LASSERT(oh->ot_handle->h_transaction != NULL);
3407 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
3408 if (IS_ERR(local)) {
3409 CERROR("%s: create local error %d\n", osd_name(osd),
3410 (int)PTR_ERR(local));
3414 /* restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
3415 * correct gid on remote file, not agent here */
3416 local->i_gid = current_fsgid();
3417 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3418 unlock_new_inode(local);
3420 /* Set special LMA flag for local agent inode */
3421 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3423 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3424 osd_name(osd), PFID(fid), rc);
3425 RETURN(ERR_PTR(rc));
3431 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3432 lu_object_fid(&pobj->oo_dt.do_lu),
3435 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3436 osd_name(osd), PFID(fid), rc);
3437 RETURN(ERR_PTR(rc));
3444 * when direntry is deleted, we have to take care of possible agent inode
3445 * referenced by that. unfortunately we can't do this at that point:
3446 * iget() within a running transaction leads to deadlock and we better do
3447 * not call that every delete declaration to save performance. so we put
3448 * a potention agent inode on a list and process that once the transaction
3449 * is over. Notice it's not any worse in terms of real orphans as regular
3450 * object destroy doesn't put inodes on the on-disk orphan list. this should
3451 * be addressed separately
3453 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3454 struct osd_thandle *oh,
3457 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3458 struct osd_obj_orphan *oor;
3465 oor->oor_env = (struct lu_env *)env;
3466 spin_lock(&osd->od_osfs_lock);
3467 list_add(&oor->oor_list, &osd->od_orphan_list);
3468 spin_unlock(&osd->od_osfs_lock);
3470 oh->ot_remove_agents = 1;
3476 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3477 struct osd_device *osd)
3479 struct osd_thread_info *info = osd_oti_get(env);
3480 struct osd_obj_orphan *oor, *tmp;
3481 struct osd_inode_id id;
3482 struct list_head list;
3483 struct inode *inode;
3488 INIT_LIST_HEAD(&list);
3490 spin_lock(&osd->od_osfs_lock);
3491 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3492 if (oor->oor_env == env) {
3493 list_del(&oor->oor_list);
3494 list_add(&oor->oor_list, &list);
3497 spin_unlock(&osd->od_osfs_lock);
3499 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3503 list_del(&oor->oor_list);
3506 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3507 inode = osd_iget_fid(info, osd, &id, &fid);
3511 if (!osd_remote_fid(env, osd, &fid)) {
3516 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3518 mark_inode_dirty(inode);
3519 ldiskfs_journal_stop(jh);
3527 * OSD layer object create function for interoperability mode (b11826).
3528 * This is mostly similar to osd_object_create(). Only difference being, fid is
3529 * inserted into inode ea here.
3531 * \retval 0, on success
3532 * \retval -ve, on error
3534 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
3535 struct lu_attr *attr,
3536 struct dt_allocation_hint *hint,
3537 struct dt_object_format *dof,
3540 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3541 struct osd_object *obj = osd_dt_obj(dt);
3542 struct osd_thread_info *info = osd_oti_get(env);
3543 int result, on_ost = 0;
3547 if (dt_object_exists(dt))
3550 LASSERT(osd_invariant(obj));
3551 LASSERT(!dt_object_remote(dt));
3552 LASSERT(osd_write_locked(env, obj));
3553 LASSERT(th != NULL);
3555 if (unlikely(fid_is_acct(fid)))
3556 /* Quota files can't be created from the kernel any more,
3557 * 'tune2fs -O quota' will take care of creating them */
3560 result = __osd_object_create(info, obj, attr, hint, dof, th);
3562 if (fid_is_idif(fid) &&
3563 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3564 struct lu_fid *tfid = &info->oti_fid;
3565 struct ost_id *oi = &info->oti_ostid;
3567 fid_to_ostid(fid, oi);
3568 ostid_to_fid(tfid, oi, 0);
3570 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3571 LMAC_FID_ON_OST, 0);
3573 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3575 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3576 on_ost ? LMAC_FID_ON_OST : 0,
3579 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3580 obj->oo_dt.do_body_ops = &osd_body_ops;
3584 result = __osd_oi_insert(env, obj, fid, th);
3586 /* a small optimization - dt_insert() isn't usually applied
3587 * to OST objects, so we don't need to cache OI mapping for
3589 if (result == 0 && on_ost == 0) {
3590 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3591 result = osd_idc_find_and_init(env, osd, obj);
3592 LASSERT(result == 0);
3595 LASSERT(ergo(result == 0,
3596 dt_object_exists(dt) && !dt_object_remote(dt)));
3597 LINVRNT(osd_invariant(obj));
3601 static int osd_declare_object_ref_add(const struct lu_env *env,
3602 struct dt_object *dt,
3603 struct thandle *handle)
3605 struct osd_thandle *oh;
3607 /* it's possible that object doesn't exist yet */
3608 LASSERT(handle != NULL);
3610 oh = container_of0(handle, struct osd_thandle, ot_super);
3611 LASSERT(oh->ot_handle == NULL);
3613 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3614 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3620 * Concurrency: @dt is write locked.
3622 static int osd_object_ref_add(const struct lu_env *env,
3623 struct dt_object *dt, struct thandle *th)
3625 struct osd_object *obj = osd_dt_obj(dt);
3626 struct inode *inode = obj->oo_inode;
3627 struct osd_thandle *oh;
3630 if (!dt_object_exists(dt) || obj->oo_destroyed)
3633 LINVRNT(osd_invariant(obj));
3634 LASSERT(!dt_object_remote(dt));
3635 LASSERT(osd_write_locked(env, obj));
3636 LASSERT(th != NULL);
3638 oh = container_of0(th, struct osd_thandle, ot_super);
3639 LASSERT(oh->ot_handle != NULL);
3641 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3643 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3644 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3646 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3647 * (65000) subdirectories by storing "1" in i_nlink if the link count
3648 * would otherwise overflow. Directory tranversal tools understand
3649 * that (st_nlink == 1) indicates that the filesystem dose not track
3650 * hard links count on the directory, and will not abort subdirectory
3651 * scanning early once (st_nlink - 2) subdirs have been found.
3653 * This also has to properly handle the case of inodes with nlink == 0
3654 * in case they are being linked into the PENDING directory
3656 spin_lock(&obj->oo_guard);
3657 if (unlikely(inode->i_nlink == 0))
3658 /* inc_nlink from 0 may cause WARN_ON */
3659 set_nlink(inode, 1);
3661 ldiskfs_inc_count(oh->ot_handle, inode);
3662 if (!S_ISDIR(inode->i_mode))
3663 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3665 spin_unlock(&obj->oo_guard);
3667 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3668 LINVRNT(osd_invariant(obj));
3670 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3675 static int osd_declare_object_ref_del(const struct lu_env *env,
3676 struct dt_object *dt,
3677 struct thandle *handle)
3679 struct osd_thandle *oh;
3681 if (!dt_object_exists(dt))
3684 LASSERT(!dt_object_remote(dt));
3685 LASSERT(handle != NULL);
3687 oh = container_of0(handle, struct osd_thandle, ot_super);
3688 LASSERT(oh->ot_handle == NULL);
3690 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3691 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3697 * Concurrency: @dt is write locked.
3699 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
3702 struct osd_object *obj = osd_dt_obj(dt);
3703 struct inode *inode = obj->oo_inode;
3704 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3705 struct osd_thandle *oh;
3707 if (!dt_object_exists(dt))
3710 LINVRNT(osd_invariant(obj));
3711 LASSERT(!dt_object_remote(dt));
3712 LASSERT(osd_write_locked(env, obj));
3713 LASSERT(th != NULL);
3715 oh = container_of0(th, struct osd_thandle, ot_super);
3716 LASSERT(oh->ot_handle != NULL);
3718 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3720 spin_lock(&obj->oo_guard);
3721 /* That can be result of upgrade from old Lustre version and
3722 * applied only to local files. Just skip this ref_del call.
3723 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3724 if (inode->i_nlink == 0) {
3725 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3726 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3727 ", maybe an upgraded file? (LU-3915)\n",
3728 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3729 spin_unlock(&obj->oo_guard);
3733 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3734 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3736 ldiskfs_dec_count(oh->ot_handle, inode);
3737 spin_unlock(&obj->oo_guard);
3739 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3740 LINVRNT(osd_invariant(obj));
3742 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3748 * Get the 64-bit version for an inode.
3750 static int osd_object_version_get(const struct lu_env *env,
3751 struct dt_object *dt, dt_obj_version_t *ver)
3753 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3755 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
3756 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3757 *ver = LDISKFS_I(inode)->i_fs_version;
3762 * Concurrency: @dt is read locked.
3764 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3765 struct lu_buf *buf, const char *name)
3767 struct osd_object *obj = osd_dt_obj(dt);
3768 struct inode *inode = obj->oo_inode;
3769 struct osd_thread_info *info = osd_oti_get(env);
3770 struct dentry *dentry = &info->oti_obj_dentry;
3771 bool cache_xattr = false;
3774 /* version get is not real XATTR but uses xattr API */
3775 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3776 /* for version we are just using xattr API but change inode
3778 if (buf->lb_len == 0)
3779 return sizeof(dt_obj_version_t);
3781 if (buf->lb_len < sizeof(dt_obj_version_t))
3784 osd_object_version_get(env, dt, buf->lb_buf);
3786 return sizeof(dt_obj_version_t);
3789 if (!dt_object_exists(dt))
3792 LASSERT(!dt_object_remote(dt));
3793 LASSERT(inode->i_op != NULL);
3794 LASSERT(inode->i_op->getxattr != NULL);
3796 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
3797 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
3801 rc = osd_oxc_get(obj, name, buf);
3806 rc = __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3808 if (rc == -ENOENT || rc == -ENODATA)
3809 osd_oxc_add(obj, name, NULL, 0);
3810 else if (rc > 0 && buf->lb_buf != NULL)
3811 osd_oxc_add(obj, name, buf->lb_buf, rc);
3817 static int osd_declare_xattr_set(const struct lu_env *env,
3818 struct dt_object *dt,
3819 const struct lu_buf *buf, const char *name,
3820 int fl, struct thandle *handle)
3822 struct osd_thandle *oh;
3824 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3826 LASSERT(handle != NULL);
3828 oh = container_of0(handle, struct osd_thandle, ot_super);
3829 LASSERT(oh->ot_handle == NULL);
3831 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3832 /* For non-upgrading case, the LMA is set first and
3833 * usually fit inode. But for upgrade case, the LMA
3834 * may be in another separated EA block. */
3835 if (!dt_object_exists(dt))
3837 else if (fl == LU_XATTR_REPLACE)
3841 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3845 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3850 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3851 /* learn xattr size from osd_xattr_get if
3852 attribute has not been read yet */
3853 buflen = __osd_xattr_get(
3854 osd_dt_obj(dt)->oo_inode,
3855 &osd_oti_get(env)->oti_obj_dentry,
3860 buflen = buf->lb_len;
3863 if (buflen > sb->s_blocksize) {
3864 credits += osd_calc_bkmap_credits(
3866 (buflen + sb->s_blocksize - 1) >>
3867 sb->s_blocksize_bits);
3871 * xattr set may involve inode quota change, reserve credits for
3872 * dquot_initialize()
3874 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3877 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3883 * Set the 64-bit version for object
3885 static void osd_object_version_set(const struct lu_env *env,
3886 struct dt_object *dt,
3887 dt_obj_version_t *new_version)
3889 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3891 CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
3892 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3894 LDISKFS_I(inode)->i_fs_version = *new_version;
3895 /** Version is set after all inode operations are finished,
3896 * so we should mark it dirty here */
3897 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3901 * Concurrency: @dt is write locked.
3903 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3904 const struct lu_buf *buf, const char *name, int fl,
3905 struct thandle *handle)
3907 struct osd_object *obj = osd_dt_obj(dt);
3908 struct inode *inode = obj->oo_inode;
3909 struct osd_thread_info *info = osd_oti_get(env);
3914 LASSERT(handle != NULL);
3916 /* version set is not real XATTR */
3917 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3918 /* for version we are just using xattr API but change inode
3920 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3921 osd_object_version_set(env, dt, buf->lb_buf);
3922 return sizeof(dt_obj_version_t);
3925 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3926 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3928 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3929 if (fl & LU_XATTR_REPLACE)
3930 fs_flags |= XATTR_REPLACE;
3932 if (fl & LU_XATTR_CREATE)
3933 fs_flags |= XATTR_CREATE;
3935 if (strcmp(name, XATTR_NAME_LMV) == 0) {
3936 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3938 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3942 lma->lma_incompat |= LMAI_STRIPED;
3943 lustre_lma_swab(lma);
3944 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3945 sizeof(*lma), XATTR_REPLACE);
3950 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3952 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3955 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3956 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
3957 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
3963 * Concurrency: @dt is read locked.
3965 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3966 const struct lu_buf *buf)
3968 struct osd_object *obj = osd_dt_obj(dt);
3969 struct inode *inode = obj->oo_inode;
3970 struct osd_thread_info *info = osd_oti_get(env);
3971 struct dentry *dentry = &info->oti_obj_dentry;
3973 if (!dt_object_exists(dt))
3976 LASSERT(!dt_object_remote(dt));
3977 LASSERT(inode->i_op != NULL);
3978 LASSERT(inode->i_op->listxattr != NULL);
3980 dentry->d_inode = inode;
3981 dentry->d_sb = inode->i_sb;
3982 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3985 static int osd_declare_xattr_del(const struct lu_env *env,
3986 struct dt_object *dt, const char *name,
3987 struct thandle *handle)
3989 struct osd_thandle *oh;
3990 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3992 LASSERT(!dt_object_remote(dt));
3993 LASSERT(handle != NULL);
3995 oh = container_of0(handle, struct osd_thandle, ot_super);
3996 LASSERT(oh->ot_handle == NULL);
3998 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3999 osd_dto_credits_noquota[DTO_XATTR_SET]);
4001 * xattr del may involve inode quota change, reserve credits for
4002 * dquot_initialize()
4004 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4010 * Concurrency: @dt is write locked.
4012 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
4013 const char *name, struct thandle *handle)
4015 struct osd_object *obj = osd_dt_obj(dt);
4016 struct inode *inode = obj->oo_inode;
4017 struct osd_thread_info *info = osd_oti_get(env);
4018 struct dentry *dentry = &info->oti_obj_dentry;
4021 if (!dt_object_exists(dt))
4024 LASSERT(!dt_object_remote(dt));
4025 LASSERT(inode->i_op != NULL);
4026 LASSERT(inode->i_op->removexattr != NULL);
4027 LASSERT(handle != NULL);
4029 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4031 ll_vfs_dq_init(inode);
4032 dentry->d_inode = inode;
4033 dentry->d_sb = inode->i_sb;
4034 rc = inode->i_op->removexattr(dentry, name);
4035 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4038 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4039 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4040 osd_oxc_del(obj, name);
4045 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
4046 __u64 start, __u64 end)
4048 struct osd_object *obj = osd_dt_obj(dt);
4049 struct inode *inode = obj->oo_inode;
4050 struct osd_thread_info *info = osd_oti_get(env);
4051 struct dentry *dentry = &info->oti_obj_dentry;
4052 struct file *file = &info->oti_file;
4057 dentry->d_inode = inode;
4058 dentry->d_sb = inode->i_sb;
4059 file->f_path.dentry = dentry;
4060 file->f_mapping = inode->i_mapping;
4061 file->f_op = inode->i_fop;
4062 set_file_inode(file, inode);
4064 rc = ll_vfs_fsync_range(file, start, end, 0);
4069 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4078 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4079 const struct dt_index_features *feat)
4081 struct iam_descr *descr;
4083 if (osd_object_is_root(o))
4084 return feat == &dt_directory_features;
4086 LASSERT(o->oo_dir != NULL);
4088 descr = o->oo_dir->od_container.ic_descr;
4089 if (feat == &dt_directory_features) {
4090 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4096 feat->dif_keysize_min <= descr->id_key_size &&
4097 descr->id_key_size <= feat->dif_keysize_max &&
4098 feat->dif_recsize_min <= descr->id_rec_size &&
4099 descr->id_rec_size <= feat->dif_recsize_max &&
4100 !(feat->dif_flags & (DT_IND_VARKEY |
4101 DT_IND_VARREC | DT_IND_NONUNQ)) &&
4102 ergo(feat->dif_flags & DT_IND_UPDATE,
4103 1 /* XXX check that object (and file system) is
4108 static int osd_iam_container_init(const struct lu_env *env,
4109 struct osd_object *obj,
4110 struct osd_directory *dir)
4112 struct iam_container *bag = &dir->od_container;
4115 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
4119 result = iam_container_setup(bag);
4121 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
4123 iam_container_fini(bag);
4130 * Concurrency: no external locking is necessary.
4132 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
4133 const struct dt_index_features *feat)
4137 struct osd_object *obj = osd_dt_obj(dt);
4139 LINVRNT(osd_invariant(obj));
4141 if (osd_object_is_root(obj)) {
4142 dt->do_index_ops = &osd_index_ea_ops;
4144 } else if (feat == &dt_directory_features) {
4145 dt->do_index_ops = &osd_index_ea_ops;
4146 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
4151 } else if (unlikely(feat == &dt_otable_features)) {
4152 dt->do_index_ops = &osd_otable_ops;
4154 } else if (unlikely(feat == &dt_acct_features)) {
4155 dt->do_index_ops = &osd_acct_index_ops;
4158 } else if (!osd_has_index(obj)) {
4159 struct osd_directory *dir;
4164 spin_lock(&obj->oo_guard);
4165 if (obj->oo_dir == NULL)
4169 * Concurrent thread allocated container data.
4172 spin_unlock(&obj->oo_guard);
4174 * Now, that we have container data, serialize its
4177 down_write(&obj->oo_ext_idx_sem);
4179 * recheck under lock.
4181 if (!osd_has_index(obj))
4182 result = osd_iam_container_init(env, obj,
4186 up_write(&obj->oo_ext_idx_sem);
4194 if (result == 0 && skip_iam == 0) {
4195 if (!osd_iam_index_probe(env, obj, feat))
4198 LINVRNT(osd_invariant(obj));
4200 if (result == 0 && feat == &dt_quota_glb_features &&
4201 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
4202 result = osd_quota_migration(env, dt);
4207 static int osd_otable_it_attr_get(const struct lu_env *env,
4208 struct dt_object *dt,
4209 struct lu_attr *attr)
4215 static const struct dt_object_operations osd_obj_ops = {
4216 .do_read_lock = osd_object_read_lock,
4217 .do_write_lock = osd_object_write_lock,
4218 .do_read_unlock = osd_object_read_unlock,
4219 .do_write_unlock = osd_object_write_unlock,
4220 .do_write_locked = osd_object_write_locked,
4221 .do_attr_get = osd_attr_get,
4222 .do_declare_attr_set = osd_declare_attr_set,
4223 .do_attr_set = osd_attr_set,
4224 .do_ah_init = osd_ah_init,
4225 .do_declare_create = osd_declare_object_create,
4226 .do_create = osd_object_create,
4227 .do_declare_destroy = osd_declare_object_destroy,
4228 .do_destroy = osd_object_destroy,
4229 .do_index_try = osd_index_try,
4230 .do_declare_ref_add = osd_declare_object_ref_add,
4231 .do_ref_add = osd_object_ref_add,
4232 .do_declare_ref_del = osd_declare_object_ref_del,
4233 .do_ref_del = osd_object_ref_del,
4234 .do_xattr_get = osd_xattr_get,
4235 .do_declare_xattr_set = osd_declare_xattr_set,
4236 .do_xattr_set = osd_xattr_set,
4237 .do_declare_xattr_del = osd_declare_xattr_del,
4238 .do_xattr_del = osd_xattr_del,
4239 .do_xattr_list = osd_xattr_list,
4240 .do_object_sync = osd_object_sync,
4241 .do_invalidate = osd_invalidate,
4245 * dt_object_operations for interoperability mode
4246 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4248 static const struct dt_object_operations osd_obj_ea_ops = {
4249 .do_read_lock = osd_object_read_lock,
4250 .do_write_lock = osd_object_write_lock,
4251 .do_read_unlock = osd_object_read_unlock,
4252 .do_write_unlock = osd_object_write_unlock,
4253 .do_write_locked = osd_object_write_locked,
4254 .do_attr_get = osd_attr_get,
4255 .do_declare_attr_set = osd_declare_attr_set,
4256 .do_attr_set = osd_attr_set,
4257 .do_ah_init = osd_ah_init,
4258 .do_declare_create = osd_declare_object_create,
4259 .do_create = osd_object_ea_create,
4260 .do_declare_destroy = osd_declare_object_destroy,
4261 .do_destroy = osd_object_destroy,
4262 .do_index_try = osd_index_try,
4263 .do_declare_ref_add = osd_declare_object_ref_add,
4264 .do_ref_add = osd_object_ref_add,
4265 .do_declare_ref_del = osd_declare_object_ref_del,
4266 .do_ref_del = osd_object_ref_del,
4267 .do_xattr_get = osd_xattr_get,
4268 .do_declare_xattr_set = osd_declare_xattr_set,
4269 .do_xattr_set = osd_xattr_set,
4270 .do_declare_xattr_del = osd_declare_xattr_del,
4271 .do_xattr_del = osd_xattr_del,
4272 .do_xattr_list = osd_xattr_list,
4273 .do_object_sync = osd_object_sync,
4274 .do_invalidate = osd_invalidate,
4277 static const struct dt_object_operations osd_obj_otable_it_ops = {
4278 .do_attr_get = osd_otable_it_attr_get,
4279 .do_index_try = osd_index_try,
4282 static int osd_index_declare_iam_delete(const struct lu_env *env,
4283 struct dt_object *dt,
4284 const struct dt_key *key,
4285 struct thandle *handle)
4287 struct osd_thandle *oh;
4289 oh = container_of0(handle, struct osd_thandle, ot_super);
4290 LASSERT(oh->ot_handle == NULL);
4292 /* Recycle may cause additional three blocks to be changed. */
4293 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
4294 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
4300 * delete a (key, value) pair from index \a dt specified by \a key
4302 * \param dt osd index object
4303 * \param key key for index
4304 * \param rec record reference
4305 * \param handle transaction handler
4308 * \retval -ve failure
4310 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
4311 const struct dt_key *key,
4312 struct thandle *handle)
4314 struct osd_thread_info *oti = osd_oti_get(env);
4315 struct osd_object *obj = osd_dt_obj(dt);
4316 struct osd_thandle *oh;
4317 struct iam_path_descr *ipd;
4318 struct iam_container *bag = &obj->oo_dir->od_container;
4322 if (!dt_object_exists(dt))
4325 LINVRNT(osd_invariant(obj));
4326 LASSERT(!dt_object_remote(dt));
4327 LASSERT(bag->ic_object == obj->oo_inode);
4328 LASSERT(handle != NULL);
4330 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4332 ipd = osd_idx_ipd_get(env, bag);
4333 if (unlikely(ipd == NULL))
4336 oh = container_of0(handle, struct osd_thandle, ot_super);
4337 LASSERT(oh->ot_handle != NULL);
4338 LASSERT(oh->ot_handle->h_transaction != NULL);
4340 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4341 /* swab quota uid/gid provided by caller */
4342 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4343 key = (const struct dt_key *)&oti->oti_quota_id;
4346 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
4347 osd_ipd_put(env, bag, ipd);
4348 LINVRNT(osd_invariant(obj));
4349 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4353 static int osd_index_declare_ea_delete(const struct lu_env *env,
4354 struct dt_object *dt,
4355 const struct dt_key *key,
4356 struct thandle *handle)
4358 struct osd_thandle *oh;
4359 struct inode *inode;
4363 LASSERT(!dt_object_remote(dt));
4364 LASSERT(handle != NULL);
4366 oh = container_of0(handle, struct osd_thandle, ot_super);
4367 LASSERT(oh->ot_handle == NULL);
4369 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
4370 if (key != NULL && unlikely(strcmp((char *)key, dotdot) == 0)) {
4371 /* '..' to a remote object has a local representative */
4372 credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
4373 /* to reset LMAI_REMOTE_PARENT */
4376 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
4378 inode = osd_dt_obj(dt)->oo_inode;
4382 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
4383 0, oh, osd_dt_obj(dt), true, NULL, false);
4387 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
4390 struct osd_fid_pack *rec;
4393 if (de->file_type & LDISKFS_DIRENT_LUFID) {
4394 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
4395 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
4396 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
4402 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
4403 const struct lu_fid *fid)
4405 struct seq_server_site *ss = osd_seq_site(osd);
4408 /* FID seqs not in FLDB, must be local seq */
4409 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
4412 /* If FLD is not being initialized yet, it only happens during the
4413 * initialization, likely during mgs initialization, and we assume
4414 * this is local FID. */
4415 if (ss == NULL || ss->ss_server_fld == NULL)
4418 /* Only check the local FLDB here */
4419 if (osd_seq_exists(env, osd, fid_seq(fid)))
4426 * Index delete function for interoperability mode (b11826).
4427 * It will remove the directory entry added by osd_index_ea_insert().
4428 * This entry is needed to maintain name->fid mapping.
4430 * \param key, key i.e. file entry to be deleted
4432 * \retval 0, on success
4433 * \retval -ve, on error
4435 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
4436 const struct dt_key *key, struct thandle *handle)
4438 struct osd_object *obj = osd_dt_obj(dt);
4439 struct inode *dir = obj->oo_inode;
4440 struct dentry *dentry;
4441 struct osd_thandle *oh;
4442 struct ldiskfs_dir_entry_2 *de = NULL;
4443 struct buffer_head *bh;
4444 struct htree_lock *hlock = NULL;
4445 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
4446 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4450 if (!dt_object_exists(dt))
4453 LINVRNT(osd_invariant(obj));
4454 LASSERT(!dt_object_remote(dt));
4455 LASSERT(handle != NULL);
4457 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4459 oh = container_of(handle, struct osd_thandle, ot_super);
4460 LASSERT(oh->ot_handle != NULL);
4461 LASSERT(oh->ot_handle->h_transaction != NULL);
4463 ll_vfs_dq_init(dir);
4464 dentry = osd_child_dentry_get(env, obj,
4465 (char *)key, strlen((char *)key));
4467 if (obj->oo_hl_head != NULL) {
4468 hlock = osd_oti_get(env)->oti_hlock;
4469 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4470 dir, LDISKFS_HLOCK_DEL);
4472 down_write(&obj->oo_ext_idx_sem);
4475 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4477 /* If this is not the ".." entry, it might be a remote DNE
4478 * entry and we need to check if the FID is for a remote
4479 * MDT. If the FID is not in the directory entry (e.g.
4480 * upgraded 1.8 filesystem without dirdata enabled) then
4481 * we need to get the FID from the LMA. For a remote directory
4482 * there HAS to be an LMA, it cannot be an IGIF inode in this
4485 * Delete the entry before the agent inode in order to
4486 * simplify error handling. At worst an error after deleting
4487 * the entry first might leak the agent inode afterward. The
4488 * reverse would need filesystem abort in case of error deleting
4489 * the entry after the agent had been removed, or leave a
4490 * dangling entry pointing at a random inode. */
4491 if (strcmp((char *)key, dotdot) != 0) {
4492 LASSERT(de != NULL);
4493 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4494 if (rc == -ENODATA) {
4495 /* can't get FID, postpone to the end of the
4496 * transaction when iget() is safe */
4497 osd_schedule_agent_inode_removal(env, oh,
4498 le32_to_cpu(de->inode));
4499 } else if (rc == 0 &&
4500 unlikely(osd_remote_fid(env, osd, fid))) {
4501 osd_schedule_agent_inode_removal(env, oh,
4502 le32_to_cpu(de->inode));
4505 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4511 ldiskfs_htree_unlock(hlock);
4513 up_write(&obj->oo_ext_idx_sem);
4518 /* For inode on the remote MDT, .. will point to
4519 * /Agent directory, Check whether it needs to delete
4520 * from agent directory */
4521 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4524 ret = osd_delete_from_remote_parent(env, osd_obj2dev(obj),
4527 /* Sigh, the entry has been deleted, and
4528 * it is not easy to revert it back, so
4529 * let's keep this error private, and let
4530 * LFSCK fix it. XXX */
4531 CERROR("%s: delete remote parent "DFID": rc = %d\n",
4532 osd_name(osd), PFID(fid), ret);
4535 LASSERT(osd_invariant(obj));
4536 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4541 * Lookup index for \a key and copy record to \a rec.
4543 * \param dt osd index object
4544 * \param key key for index
4545 * \param rec record reference
4547 * \retval +ve success : exact mach
4548 * \retval 0 return record with key not greater than \a key
4549 * \retval -ve failure
4551 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4552 struct dt_rec *rec, const struct dt_key *key)
4554 struct osd_object *obj = osd_dt_obj(dt);
4555 struct iam_path_descr *ipd;
4556 struct iam_container *bag = &obj->oo_dir->od_container;
4557 struct osd_thread_info *oti = osd_oti_get(env);
4558 struct iam_iterator *it = &oti->oti_idx_it;
4559 struct iam_rec *iam_rec;
4563 if (!dt_object_exists(dt))
4566 LASSERT(osd_invariant(obj));
4567 LASSERT(!dt_object_remote(dt));
4568 LASSERT(bag->ic_object == obj->oo_inode);
4570 ipd = osd_idx_ipd_get(env, bag);
4574 /* got ipd now we can start iterator. */
4575 iam_it_init(it, bag, 0, ipd);
4577 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4578 /* swab quota uid/gid provided by caller */
4579 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4580 key = (const struct dt_key *)&oti->oti_quota_id;
4583 rc = iam_it_get(it, (struct iam_key *)key);
4585 if (S_ISDIR(obj->oo_inode->i_mode))
4586 iam_rec = (struct iam_rec *)oti->oti_ldp;
4588 iam_rec = (struct iam_rec *) rec;
4590 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4592 if (S_ISDIR(obj->oo_inode->i_mode))
4593 osd_fid_unpack((struct lu_fid *) rec,
4594 (struct osd_fid_pack *)iam_rec);
4595 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4596 osd_quota_unpack(obj, rec);
4601 osd_ipd_put(env, bag, ipd);
4603 LINVRNT(osd_invariant(obj));
4608 static int osd_index_declare_iam_insert(const struct lu_env *env,
4609 struct dt_object *dt,
4610 const struct dt_rec *rec,
4611 const struct dt_key *key,
4612 struct thandle *handle)
4614 struct osd_thandle *oh;
4616 LASSERT(handle != NULL);
4618 oh = container_of0(handle, struct osd_thandle, ot_super);
4619 LASSERT(oh->ot_handle == NULL);
4621 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4622 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4628 * Inserts (key, value) pair in \a dt index object.
4630 * \param dt osd index object
4631 * \param key key for index
4632 * \param rec record reference
4633 * \param th transaction handler
4636 * \retval -ve failure
4638 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4639 const struct dt_rec *rec,
4640 const struct dt_key *key, struct thandle *th,
4643 struct osd_object *obj = osd_dt_obj(dt);
4644 struct iam_path_descr *ipd;
4645 struct osd_thandle *oh;
4646 struct iam_container *bag;
4647 struct osd_thread_info *oti = osd_oti_get(env);
4648 struct iam_rec *iam_rec;
4652 if (!dt_object_exists(dt))
4655 LINVRNT(osd_invariant(obj));
4656 LASSERT(!dt_object_remote(dt));
4658 bag = &obj->oo_dir->od_container;
4659 LASSERT(bag->ic_object == obj->oo_inode);
4660 LASSERT(th != NULL);
4662 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4664 ipd = osd_idx_ipd_get(env, bag);
4665 if (unlikely(ipd == NULL))
4668 oh = container_of0(th, struct osd_thandle, ot_super);
4669 LASSERT(oh->ot_handle != NULL);
4670 LASSERT(oh->ot_handle->h_transaction != NULL);
4671 if (S_ISDIR(obj->oo_inode->i_mode)) {
4672 iam_rec = (struct iam_rec *)oti->oti_ldp;
4673 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4674 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4675 /* pack quota uid/gid */
4676 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4677 key = (const struct dt_key *)&oti->oti_quota_id;
4678 /* pack quota record */
4679 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4680 iam_rec = (struct iam_rec *)rec;
4682 iam_rec = (struct iam_rec *)rec;
4685 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4687 osd_ipd_put(env, bag, ipd);
4688 LINVRNT(osd_invariant(obj));
4689 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4694 * Calls ldiskfs_add_entry() to add directory entry
4695 * into the directory. This is required for
4696 * interoperability mode (b11826)
4698 * \retval 0, on success
4699 * \retval -ve, on error
4701 static int __osd_ea_add_rec(struct osd_thread_info *info,
4702 struct osd_object *pobj, struct inode *cinode,
4703 const char *name, const struct lu_fid *fid,
4704 struct htree_lock *hlock, struct thandle *th)
4706 struct ldiskfs_dentry_param *ldp;
4707 struct dentry *child;
4708 struct osd_thandle *oth;
4711 oth = container_of(th, struct osd_thandle, ot_super);
4712 LASSERT(oth->ot_handle != NULL);
4713 LASSERT(oth->ot_handle->h_transaction != NULL);
4714 LASSERT(pobj->oo_inode);
4716 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4717 if (unlikely(pobj->oo_inode ==
4718 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4721 osd_get_ldiskfs_dirent_param(ldp, fid);
4722 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4723 child->d_fsdata = (void *)ldp;
4724 ll_vfs_dq_init(pobj->oo_inode);
4725 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
4726 child, cinode, hlock);
4727 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4728 struct ldiskfs_dir_entry_2 *de;
4729 struct buffer_head *bh;
4732 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4735 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4738 if (S_ISDIR(cinode->i_mode))
4739 de->file_type = LDISKFS_DIRENT_LUFID |
4740 LDISKFS_FT_REG_FILE;
4742 de->file_type = LDISKFS_DIRENT_LUFID |
4744 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4755 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4756 * into the directory.Also sets flags into osd object to
4757 * indicate dot and dotdot are created. This is required for
4758 * interoperability mode (b11826)
4760 * \param dir directory for dot and dotdot fixup.
4761 * \param obj child object for linking
4763 * \retval 0, on success
4764 * \retval -ve, on error
4766 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4767 struct osd_object *dir,
4768 struct inode *parent_dir, const char *name,
4769 const struct lu_fid *dot_fid,
4770 const struct lu_fid *dot_dot_fid,
4773 struct inode *inode = dir->oo_inode;
4774 struct osd_thandle *oth;
4777 oth = container_of(th, struct osd_thandle, ot_super);
4778 LASSERT(oth->ot_handle->h_transaction != NULL);
4779 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4781 if (strcmp(name, dot) == 0) {
4782 if (dir->oo_compat_dot_created) {
4785 LASSERT(inode->i_ino == parent_dir->i_ino);
4786 dir->oo_compat_dot_created = 1;
4789 } else if (strcmp(name, dotdot) == 0) {
4790 if (!dir->oo_compat_dot_created)
4792 /* in case of rename, dotdot is already created */
4793 if (dir->oo_compat_dotdot_created) {
4794 return __osd_ea_add_rec(info, dir, parent_dir, name,
4795 dot_dot_fid, NULL, th);
4798 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4799 struct lu_fid tfid = *dot_dot_fid;
4802 result = osd_add_dot_dotdot_internal(info,
4803 dir->oo_inode, parent_dir, dot_fid,
4806 result = osd_add_dot_dotdot_internal(info,
4807 dir->oo_inode, parent_dir, dot_fid,
4812 dir->oo_compat_dotdot_created = 1;
4820 * It will call the appropriate osd_add* function and return the
4821 * value, return by respective functions.
4823 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4824 struct inode *cinode, const char *name,
4825 const struct lu_fid *fid, struct thandle *th)
4827 struct osd_thread_info *info = osd_oti_get(env);
4828 struct htree_lock *hlock;
4831 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4833 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4835 if (hlock != NULL) {
4836 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4839 down_write(&pobj->oo_ext_idx_sem);
4842 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4843 lu_object_fid(&pobj->oo_dt.do_lu),
4846 if (hlock != NULL) {
4847 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4848 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4850 down_write(&pobj->oo_ext_idx_sem);
4853 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4854 struct lu_fid *tfid = &info->oti_fid;
4858 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4861 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4866 ldiskfs_htree_unlock(hlock);
4868 up_write(&pobj->oo_ext_idx_sem);
4874 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4875 struct osd_idmap_cache *oic)
4877 struct osd_scrub *scrub = &dev->od_scrub;
4878 struct lu_fid *fid = &oic->oic_fid;
4879 struct osd_inode_id *id = &oic->oic_lid;
4880 struct inode *inode = NULL;
4886 if (!fid_is_norm(fid) && !fid_is_igif(fid))
4889 if (scrub->os_pos_current > id->oii_ino)
4893 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
4894 if (rc == -ENOENT) {
4895 __u32 gen = id->oii_gen;
4901 inode = osd_iget(oti, dev, id);
4902 /* The inode has been removed (by race maybe). */
4903 if (IS_ERR(inode)) {
4904 rc = PTR_ERR(inode);
4906 RETURN(rc == -ESTALE ? -ENOENT : rc);
4909 /* The OI mapping is lost. */
4910 if (gen != OSD_OII_NOGEN)
4914 /* The inode may has been reused by others, we do not know,
4915 * leave it to be handled by subsequent osd_fid_lookup(). */
4917 } else if (rc != 0 || osd_id_eq(id, &oti->oti_id)) {
4924 if (thread_is_running(&scrub->os_thread)) {
4925 if (inode == NULL) {
4926 inode = osd_iget(oti, dev, id);
4927 /* The inode has been removed (by race maybe). */
4928 if (IS_ERR(inode)) {
4929 rc = PTR_ERR(inode);
4931 RETURN(rc == -ESTALE ? -ENOENT : rc);
4935 rc = osd_oii_insert(dev, oic, insert);
4936 /* There is race condition between osd_oi_lookup and OI scrub.
4937 * The OI scrub finished just after osd_oi_lookup() failure.
4938 * Under such case, it is unnecessary to trigger OI scrub again,
4939 * but try to call osd_oi_lookup() again. */
4940 if (unlikely(rc == -EAGAIN))
4943 if (!S_ISDIR(inode->i_mode))
4946 rc = osd_check_lmv(oti, dev, inode, oic);
4952 if (!dev->od_noscrub && ++once == 1) {
4953 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4955 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
4956 "%.16s: trigger partial OI scrub for RPC inconsistency "
4957 "checking FID "DFID": rc = %d\n",
4958 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4960 if (rc == 0 || rc == -EALREADY)
4970 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4971 struct osd_device *dev,
4972 struct osd_idmap_cache *oic,
4973 struct lu_fid *fid, __u32 ino)
4975 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
4976 struct inode *inode;
4979 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4980 inode = osd_iget(oti, dev, &oic->oic_lid);
4981 if (IS_ERR(inode)) {
4982 fid_zero(&oic->oic_fid);
4983 return PTR_ERR(inode);
4986 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4989 fid_zero(&oic->oic_fid);
4991 *fid = oic->oic_fid = lma->lma_self_fid;
4995 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4996 struct osd_inode_id *id, const struct lu_fid *fid)
4998 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4999 id->oii_ino, id->oii_gen, info);
5000 info->oti_cache.oic_lid = *id;
5001 info->oti_cache.oic_fid = *fid;
5002 info->oti_cache.oic_dev = osd;
5006 * Get parent FID from the linkEA.
5008 * For a directory which parent resides on remote MDT, to satisfy the
5009 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
5010 * the other hand, to make the lookup(..) on the directory can return
5011 * the real parent FID, we append the real parent FID after its ".."
5012 * name entry in the /REMOTE_PARENT_DIR.
5014 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
5015 * backup. So after the restore, we cannot get the right parent FID from
5016 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
5017 * we have stored the real parent FID in the directory object's linkEA,
5018 * we can parse the linkEA for the real parent FID.
5020 * \param[in] env pointer to the thread context
5021 * \param[in] obj pointer to the object to be handled
5022 * \param[out]fid pointer to the buffer to hold the parent FID
5024 * \retval 0 for getting the real parent FID successfully
5025 * \retval negative error number on failure
5027 static int osd_get_pfid_from_linkea(const struct lu_env *env,
5028 struct osd_object *obj,
5031 struct osd_thread_info *oti = osd_oti_get(env);
5032 struct lu_buf *buf = &oti->oti_big_buf;
5033 struct dentry *dentry = &oti->oti_obj_dentry;
5034 struct inode *inode = obj->oo_inode;
5035 struct linkea_data ldata = { NULL };
5040 if (!S_ISDIR(inode->i_mode))
5044 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5045 buf->lb_buf, buf->lb_len);
5046 if (rc == -ERANGE) {
5047 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5050 lu_buf_realloc(buf, rc);
5051 if (buf->lb_buf == NULL)
5058 if (unlikely(rc == 0))
5064 if (unlikely(buf->lb_buf == NULL)) {
5065 lu_buf_realloc(buf, rc);
5066 if (buf->lb_buf == NULL)
5073 rc = linkea_init_with_rec(&ldata);
5075 linkea_first_entry(&ldata);
5076 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5082 static int osd_verify_ent_by_linkea(const struct lu_env *env,
5083 struct inode *inode,
5084 const struct lu_fid *pfid,
5085 const char *name, const int namelen)
5087 struct osd_thread_info *oti = osd_oti_get(env);
5088 struct lu_buf *buf = &oti->oti_big_buf;
5089 struct dentry *dentry = &oti->oti_obj_dentry;
5090 struct linkea_data ldata = { NULL };
5091 struct lu_name cname = { .ln_name = name,
5092 .ln_namelen = namelen };
5097 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5098 buf->lb_buf, buf->lb_len);
5100 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
5105 if (unlikely(rc == 0))
5108 if (buf->lb_len < rc) {
5109 lu_buf_realloc(buf, rc);
5110 if (buf->lb_buf == NULL)
5117 rc = linkea_init_with_rec(&ldata);
5119 rc = linkea_links_find(&ldata, &cname, pfid);
5125 * Calls ->lookup() to find dentry. From dentry get inode and
5126 * read inode's ea to get fid. This is required for interoperability
5129 * \retval 0, on success
5130 * \retval -ve, on error
5132 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
5133 struct dt_rec *rec, const struct dt_key *key)
5135 struct inode *dir = obj->oo_inode;
5136 struct dentry *dentry;
5137 struct ldiskfs_dir_entry_2 *de;
5138 struct buffer_head *bh;
5139 struct lu_fid *fid = (struct lu_fid *) rec;
5140 struct htree_lock *hlock = NULL;
5145 LASSERT(dir->i_op != NULL);
5146 LASSERT(dir->i_op->lookup != NULL);
5148 dentry = osd_child_dentry_get(env, obj,
5149 (char *)key, strlen((char *)key));
5151 if (obj->oo_hl_head != NULL) {
5152 hlock = osd_oti_get(env)->oti_hlock;
5153 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5154 dir, LDISKFS_HLOCK_LOOKUP);
5156 down_read(&obj->oo_ext_idx_sem);
5159 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5161 struct osd_thread_info *oti = osd_oti_get(env);
5162 struct osd_inode_id *id = &oti->oti_id;
5163 struct osd_idmap_cache *oic = &oti->oti_cache;
5164 struct osd_device *dev = osd_obj2dev(obj);
5166 ino = le32_to_cpu(de->inode);
5167 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
5169 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
5173 rc = osd_get_fid_from_dentry(de, rec);
5175 /* done with de, release bh */
5178 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5179 const char *name = (const char *)key;
5181 /* If the parent is on remote MDT, and there
5182 * is no FID-in-dirent, then we have to get
5183 * the parent FID from the linkEA. */
5184 if (likely(strlen(name) == 2 &&
5185 name[0] == '.' && name[1] == '.'))
5186 rc = osd_get_pfid_from_linkea(env, obj,
5189 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5192 osd_id_gen(id, ino, OSD_OII_NOGEN);
5195 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
5196 fid_zero(&oic->oic_fid);
5201 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
5202 rc = osd_consistency_check(oti, dev, oic);
5204 fid_zero(&oic->oic_fid);
5213 ldiskfs_htree_unlock(hlock);
5215 up_read(&obj->oo_ext_idx_sem);
5219 static int osd_index_declare_ea_insert(const struct lu_env *env,
5220 struct dt_object *dt,
5221 const struct dt_rec *rec,
5222 const struct dt_key *key,
5223 struct thandle *handle)
5225 struct osd_thandle *oh;
5226 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5227 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5228 const struct lu_fid *fid = rec1->rec_fid;
5229 int credits, rc = 0;
5230 struct osd_idmap_cache *idc;
5233 LASSERT(!dt_object_remote(dt));
5234 LASSERT(handle != NULL);
5235 LASSERT(fid != NULL);
5236 LASSERT(rec1->rec_type != 0);
5238 oh = container_of0(handle, struct osd_thandle, ot_super);
5239 LASSERT(oh->ot_handle == NULL);
5241 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
5243 /* we can't call iget() while a transactions is running
5244 * (this can lead to a deadlock), but we need to know
5245 * inum and object type. so we find this information at
5246 * declaration and cache in per-thread info */
5247 idc = osd_idc_find_or_init(env, osd, fid);
5249 RETURN(PTR_ERR(idc));
5250 if (idc->oic_remote) {
5251 /* a reference to remote inode is represented by an
5252 * agent inode which we have to create */
5253 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
5254 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
5257 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
5259 if (osd_dt_obj(dt)->oo_inode != NULL) {
5260 struct inode *inode = osd_dt_obj(dt)->oo_inode;
5262 /* We ignore block quota on meta pool (MDTs), so needn't
5263 * calculate how many blocks will be consumed by this index
5265 rc = osd_declare_inode_qid(env, i_uid_read(inode),
5266 i_gid_read(inode), 0, oh,
5267 osd_dt_obj(dt), true, NULL, false);
5274 * Index add function for interoperability mode (b11826).
5275 * It will add the directory entry.This entry is needed to
5276 * maintain name->fid mapping.
5278 * \param key it is key i.e. file entry to be inserted
5279 * \param rec it is value of given key i.e. fid
5281 * \retval 0, on success
5282 * \retval -ve, on error
5284 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
5285 const struct dt_rec *rec,
5286 const struct dt_key *key, struct thandle *th,
5289 struct osd_object *obj = osd_dt_obj(dt);
5290 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5291 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5292 const struct lu_fid *fid = rec1->rec_fid;
5293 const char *name = (const char *)key;
5294 struct osd_thread_info *oti = osd_oti_get(env);
5295 struct inode *child_inode = NULL;
5296 struct osd_idmap_cache *idc;
5300 if (!dt_object_exists(dt))
5303 LASSERT(osd_invariant(obj));
5304 LASSERT(!dt_object_remote(dt));
5305 LASSERT(th != NULL);
5307 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5309 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
5311 idc = osd_idc_find(env, osd, fid);
5312 if (unlikely(idc == NULL)) {
5313 /* this dt_insert() wasn't declared properly, so
5314 * FID is missing in OI cache. we better do not
5315 * lookup FID in FLDB/OI and don't risk to deadlock,
5316 * but in some special cases (lfsck testing, etc)
5317 * it's much simpler than fixing a caller */
5318 CERROR("%s: "DFID" wasn't declared for insert\n",
5319 osd_name(osd), PFID(fid));
5321 idc = osd_idc_find_or_init(env, osd, fid);
5323 RETURN(PTR_ERR(idc));
5326 if (idc->oic_remote) {
5327 /* Insert remote entry */
5328 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
5329 struct osd_mdobj_map *omm = osd->od_mdt_map;
5330 struct osd_thandle *oh;
5332 /* If parent on remote MDT, we need put this object
5334 oh = container_of(th, typeof(*oh), ot_super);
5335 rc = osd_add_to_remote_parent(env, osd, obj, oh);
5337 CERROR("%s: add "DFID" error: rc = %d\n",
5339 PFID(lu_object_fid(&dt->do_lu)), rc);
5343 child_inode = igrab(omm->omm_remote_parent->d_inode);
5345 child_inode = osd_create_local_agent_inode(env, osd,
5346 obj, fid, rec1->rec_type & S_IFMT, th);
5347 if (IS_ERR(child_inode))
5348 RETURN(PTR_ERR(child_inode));
5351 /* Insert local entry */
5352 if (unlikely(idc->oic_lid.oii_ino == 0)) {
5353 /* for a reason OI cache wasn't filled properly */
5354 CERROR("%s: OIC for "DFID" isn't filled\n",
5355 osd_name(osd), PFID(fid));
5358 child_inode = oti->oti_inode;
5359 if (unlikely(child_inode == NULL)) {
5360 struct ldiskfs_inode_info *lii;
5364 child_inode = oti->oti_inode = &lii->vfs_inode;
5366 child_inode->i_sb = osd_sb(osd);
5367 child_inode->i_ino = idc->oic_lid.oii_ino;
5368 child_inode->i_mode = rec1->rec_type & S_IFMT;
5371 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
5373 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
5374 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
5376 if (child_inode && child_inode != oti->oti_inode)
5378 LASSERT(osd_invariant(obj));
5379 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5384 * Initialize osd Iterator for given osd index object.
5386 * \param dt osd index object
5389 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
5390 struct dt_object *dt,
5393 struct osd_it_iam *it;
5394 struct osd_object *obj = osd_dt_obj(dt);
5395 struct lu_object *lo = &dt->do_lu;
5396 struct iam_path_descr *ipd;
5397 struct iam_container *bag = &obj->oo_dir->od_container;
5399 if (!dt_object_exists(dt))
5400 return ERR_PTR(-ENOENT);
5404 return ERR_PTR(-ENOMEM);
5406 ipd = osd_it_ipd_get(env, bag);
5407 if (likely(ipd != NULL)) {
5411 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
5412 return (struct dt_it *)it;
5415 return ERR_PTR(-ENOMEM);
5420 * free given Iterator.
5422 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
5424 struct osd_it_iam *it = (struct osd_it_iam *)di;
5425 struct osd_object *obj = it->oi_obj;
5427 iam_it_fini(&it->oi_it);
5428 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
5429 osd_object_put(env, obj);
5434 * Move Iterator to record specified by \a key
5436 * \param di osd iterator
5437 * \param key key for index
5439 * \retval +ve di points to record with least key not larger than key
5440 * \retval 0 di points to exact matched key
5441 * \retval -ve failure
5444 static int osd_it_iam_get(const struct lu_env *env,
5445 struct dt_it *di, const struct dt_key *key)
5447 struct osd_thread_info *oti = osd_oti_get(env);
5448 struct osd_it_iam *it = (struct osd_it_iam *)di;
5450 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5451 /* swab quota uid/gid */
5452 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5453 key = (struct dt_key *)&oti->oti_quota_id;
5456 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
5462 * \param di osd iterator
5464 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
5466 struct osd_it_iam *it = (struct osd_it_iam *)di;
5468 iam_it_put(&it->oi_it);
5472 * Move iterator by one record
5474 * \param di osd iterator
5476 * \retval +1 end of container reached
5478 * \retval -ve failure
5481 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
5483 struct osd_it_iam *it = (struct osd_it_iam *)di;
5485 return iam_it_next(&it->oi_it);
5489 * Return pointer to the key under iterator.
5492 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
5493 const struct dt_it *di)
5495 struct osd_thread_info *oti = osd_oti_get(env);
5496 struct osd_it_iam *it = (struct osd_it_iam *)di;
5497 struct osd_object *obj = it->oi_obj;
5500 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
5502 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
5503 /* swab quota uid/gid */
5504 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
5505 key = (struct dt_key *)&oti->oti_quota_id;
5512 * Return size of key under iterator (in bytes)
5515 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5517 struct osd_it_iam *it = (struct osd_it_iam *)di;
5519 return iam_it_key_size(&it->oi_it);
5523 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5525 /* check if file type is required */
5526 if (ent->lde_attrs & LUDA_TYPE) {
5527 struct luda_type *lt;
5528 int align = sizeof(*lt) - 1;
5530 len = (len + align) & ~align;
5531 lt = (struct luda_type *)(ent->lde_name + len);
5532 lt->lt_type = cpu_to_le16(DTTOIF(type));
5535 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5539 * build lu direct from backend fs dirent.
5543 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5544 char *name, __u16 namelen, __u16 type, __u32 attr)
5546 ent->lde_attrs = attr | LUDA_FID;
5547 fid_cpu_to_le(&ent->lde_fid, fid);
5549 ent->lde_hash = cpu_to_le64(offset);
5550 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5552 strncpy(ent->lde_name, name, namelen);
5553 ent->lde_name[namelen] = '\0';
5554 ent->lde_namelen = cpu_to_le16(namelen);
5556 /* append lustre attributes */
5557 osd_it_append_attrs(ent, namelen, type);
5561 * Return pointer to the record under iterator.
5563 static int osd_it_iam_rec(const struct lu_env *env,
5564 const struct dt_it *di,
5565 struct dt_rec *dtrec, __u32 attr)
5567 struct osd_it_iam *it = (struct osd_it_iam *)di;
5568 struct osd_thread_info *info = osd_oti_get(env);
5571 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5572 const struct osd_fid_pack *rec;
5573 struct lu_fid *fid = &info->oti_fid;
5574 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5580 name = (char *)iam_it_key_get(&it->oi_it);
5582 RETURN(PTR_ERR(name));
5584 namelen = iam_it_key_size(&it->oi_it);
5586 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5588 RETURN(PTR_ERR(rec));
5590 rc = osd_fid_unpack(fid, rec);
5594 hash = iam_it_store(&it->oi_it);
5596 /* IAM does not store object type in IAM index (dir) */
5597 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5599 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5600 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5601 (struct iam_rec *)dtrec);
5602 osd_quota_unpack(it->oi_obj, dtrec);
5604 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5605 (struct iam_rec *)dtrec);
5612 * Returns cookie for current Iterator position.
5614 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5616 struct osd_it_iam *it = (struct osd_it_iam *)di;
5618 return iam_it_store(&it->oi_it);
5622 * Restore iterator from cookie.
5624 * \param di osd iterator
5625 * \param hash Iterator location cookie
5627 * \retval +ve di points to record with least key not larger than key.
5628 * \retval 0 di points to exact matched key
5629 * \retval -ve failure
5632 static int osd_it_iam_load(const struct lu_env *env,
5633 const struct dt_it *di, __u64 hash)
5635 struct osd_it_iam *it = (struct osd_it_iam *)di;
5637 return iam_it_load(&it->oi_it, hash);
5640 static const struct dt_index_operations osd_index_iam_ops = {
5641 .dio_lookup = osd_index_iam_lookup,
5642 .dio_declare_insert = osd_index_declare_iam_insert,
5643 .dio_insert = osd_index_iam_insert,
5644 .dio_declare_delete = osd_index_declare_iam_delete,
5645 .dio_delete = osd_index_iam_delete,
5647 .init = osd_it_iam_init,
5648 .fini = osd_it_iam_fini,
5649 .get = osd_it_iam_get,
5650 .put = osd_it_iam_put,
5651 .next = osd_it_iam_next,
5652 .key = osd_it_iam_key,
5653 .key_size = osd_it_iam_key_size,
5654 .rec = osd_it_iam_rec,
5655 .store = osd_it_iam_store,
5656 .load = osd_it_iam_load
5662 * Creates or initializes iterator context.
5664 * \retval struct osd_it_ea, iterator structure on success
5667 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5668 struct dt_object *dt,
5671 struct osd_object *obj = osd_dt_obj(dt);
5672 struct osd_thread_info *info = osd_oti_get(env);
5673 struct osd_it_ea *oie;
5675 struct lu_object *lo = &dt->do_lu;
5676 struct dentry *obj_dentry;
5679 if (!dt_object_exists(dt) || obj->oo_destroyed)
5680 RETURN(ERR_PTR(-ENOENT));
5682 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5684 RETURN(ERR_PTR(-ENOMEM));
5685 obj_dentry = &oie->oie_dentry;
5687 obj_dentry->d_inode = obj->oo_inode;
5688 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5689 obj_dentry->d_name.hash = 0;
5691 oie->oie_rd_dirent = 0;
5692 oie->oie_it_dirent = 0;
5693 oie->oie_dirent = NULL;
5694 if (unlikely(!info->oti_it_ea_buf_used)) {
5695 oie->oie_buf = info->oti_it_ea_buf;
5696 info->oti_it_ea_buf_used = 1;
5698 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5699 if (oie->oie_buf == NULL)
5700 RETURN(ERR_PTR(-ENOMEM));
5704 file = &oie->oie_file;
5706 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5707 if (attr & LUDA_64BITHASH)
5708 file->f_mode = FMODE_64BITHASH;
5710 file->f_mode = FMODE_32BITHASH;
5711 file->f_path.dentry = obj_dentry;
5712 file->f_mapping = obj->oo_inode->i_mapping;
5713 file->f_op = obj->oo_inode->i_fop;
5714 set_file_inode(file, obj->oo_inode);
5717 RETURN((struct dt_it *) oie);
5721 * Destroy or finishes iterator context.
5723 * \param di iterator structure to be destroyed
5725 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5727 struct osd_thread_info *info = osd_oti_get(env);
5728 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5729 struct osd_object *obj = oie->oie_obj;
5730 struct inode *inode = obj->oo_inode;
5733 oie->oie_file.f_op->release(inode, &oie->oie_file);
5734 osd_object_put(env, obj);
5735 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5736 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5738 info->oti_it_ea_buf_used = 0;
5739 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5744 * It position the iterator at given key, so that next lookup continues from
5745 * that key Or it is similar to dio_it->load() but based on a key,
5746 * rather than file position.
5748 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5751 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5753 static int osd_it_ea_get(const struct lu_env *env,
5754 struct dt_it *di, const struct dt_key *key)
5756 struct osd_it_ea *it = (struct osd_it_ea *)di;
5759 LASSERT(((const char *)key)[0] == '\0');
5760 it->oie_file.f_pos = 0;
5761 it->oie_rd_dirent = 0;
5762 it->oie_it_dirent = 0;
5763 it->oie_dirent = NULL;
5771 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5775 struct osd_filldir_cbs {
5776 #ifdef HAVE_DIR_CONTEXT
5777 struct dir_context ctx;
5779 struct osd_it_ea *it;
5782 * It is called internally by ->readdir(). It fills the
5783 * iterator's in-memory data structure with required
5784 * information i.e. name, namelen, rec_size etc.
5786 * \param buf in which information to be filled in.
5787 * \param name name of the file in given dir
5789 * \retval 0 on success
5790 * \retval 1 on buffer full
5792 #ifdef HAVE_FILLDIR_USE_CTX
5793 static int osd_ldiskfs_filldir(struct dir_context *buf,
5795 static int osd_ldiskfs_filldir(void *buf,
5797 const char *name, int namelen,
5798 loff_t offset, __u64 ino, unsigned d_type)
5800 struct osd_it_ea *it =
5801 ((struct osd_filldir_cbs *)buf)->it;
5802 struct osd_object *obj = it->oie_obj;
5803 struct osd_it_ea_dirent *ent = it->oie_dirent;
5804 struct lu_fid *fid = &ent->oied_fid;
5805 struct osd_fid_pack *rec;
5808 /* this should never happen */
5809 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5810 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5814 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5818 /* "." is just the object itself. */
5819 if (namelen == 1 && name[0] == '.') {
5820 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5821 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5822 rec = (struct osd_fid_pack*) (name + namelen + 1);
5823 if (osd_fid_unpack(fid, rec) != 0)
5828 d_type &= ~LDISKFS_DIRENT_LUFID;
5830 /* NOT export local root. */
5831 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5832 ino = obj->oo_inode->i_ino;
5833 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5836 ent->oied_ino = ino;
5837 ent->oied_off = offset;
5838 ent->oied_namelen = namelen;
5839 ent->oied_type = d_type;
5841 memcpy(ent->oied_name, name, namelen);
5843 it->oie_rd_dirent++;
5844 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5849 * Calls ->readdir() to load a directory entry at a time
5850 * and stored it in iterator's in-memory data structure.
5852 * \param di iterator's in memory structure
5854 * \retval 0 on success
5855 * \retval -ve on error
5856 * \retval +1 reach the end of entry
5858 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5859 const struct dt_it *di)
5861 struct osd_it_ea *it = (struct osd_it_ea *)di;
5862 struct osd_object *obj = it->oie_obj;
5863 struct inode *inode = obj->oo_inode;
5864 struct htree_lock *hlock = NULL;
5865 struct file *filp = &it->oie_file;
5867 struct osd_filldir_cbs buf = {
5868 #ifdef HAVE_DIR_CONTEXT
5869 .ctx.actor = osd_ldiskfs_filldir,
5875 it->oie_dirent = it->oie_buf;
5876 it->oie_rd_dirent = 0;
5878 if (obj->oo_hl_head != NULL) {
5879 hlock = osd_oti_get(env)->oti_hlock;
5880 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5881 inode, LDISKFS_HLOCK_READDIR);
5883 down_read(&obj->oo_ext_idx_sem);
5886 #ifdef HAVE_DIR_CONTEXT
5887 buf.ctx.pos = filp->f_pos;
5888 rc = inode->i_fop->iterate(filp, &buf.ctx);
5889 filp->f_pos = buf.ctx.pos;
5891 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5895 ldiskfs_htree_unlock(hlock);
5897 up_read(&obj->oo_ext_idx_sem);
5899 if (it->oie_rd_dirent == 0) {
5900 /*If it does not get any dirent, it means it has been reached
5901 *to the end of the dir */
5902 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5906 it->oie_dirent = it->oie_buf;
5907 it->oie_it_dirent = 1;
5914 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5915 * to load a directory entry at a time and stored it in
5916 * iterator's in-memory data structure.
5918 * \param di iterator's in memory structure
5920 * \retval +ve iterator reached to end
5921 * \retval 0 iterator not reached to end
5922 * \retval -ve on error
5924 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5926 struct osd_it_ea *it = (struct osd_it_ea *)di;
5931 if (it->oie_it_dirent < it->oie_rd_dirent) {
5933 (void *) it->oie_dirent +
5934 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5935 it->oie_dirent->oied_namelen);
5936 it->oie_it_dirent++;
5939 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5942 rc = osd_ldiskfs_it_fill(env, di);
5949 * Returns the key at current position from iterator's in memory structure.
5951 * \param di iterator's in memory structure
5953 * \retval key i.e. struct dt_key on success
5955 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5956 const struct dt_it *di)
5958 struct osd_it_ea *it = (struct osd_it_ea *)di;
5960 return (struct dt_key *)it->oie_dirent->oied_name;
5964 * Returns the key's size at current position from iterator's in memory structure.
5966 * \param di iterator's in memory structure
5968 * \retval key_size i.e. struct dt_key on success
5970 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5972 struct osd_it_ea *it = (struct osd_it_ea *)di;
5974 return it->oie_dirent->oied_namelen;
5977 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
5979 if (LDISKFS_DIR_REC_LEN(de) >=
5980 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
5987 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5988 unsigned blocksize, bool dotdot)
5991 return osd_dotdot_has_space(de);
5993 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5994 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
6001 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
6002 handle_t *jh, struct dentry *dentry,
6003 const struct lu_fid *fid, struct buffer_head *bh,
6004 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
6007 struct inode *dir = dentry->d_parent->d_inode;
6008 struct inode *inode = dentry->d_inode;
6009 struct osd_fid_pack *rec;
6010 struct ldiskfs_dentry_param *ldp;
6011 int namelen = dentry->d_name.len;
6013 struct osd_thread_info *info = osd_oti_get(env);
6016 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
6017 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6020 /* There is enough space to hold the FID-in-dirent. */
6021 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
6022 rc = ldiskfs_journal_get_write_access(jh, bh);
6026 de->name[namelen] = 0;
6027 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
6028 rec->fp_len = sizeof(struct lu_fid) + 1;
6029 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
6030 de->file_type |= LDISKFS_DIRENT_LUFID;
6031 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
6038 rc = ldiskfs_delete_entry(jh, dir, de, bh);
6042 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
6043 osd_get_ldiskfs_dirent_param(ldp, fid);
6044 dentry->d_fsdata = (void *)ldp;
6045 ll_vfs_dq_init(dir);
6046 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
6047 /* It is too bad, we cannot reinsert the name entry back.
6048 * That means we lose it! */
6050 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
6051 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
6052 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
6053 dir->i_ino, dir->i_generation, namelen,
6054 dentry->d_name.name, PFID(fid), rc);
6060 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
6061 struct osd_it_ea *it, struct lu_fid *fid,
6062 struct osd_inode_id *id, __u32 *attr)
6064 struct osd_thread_info *info = osd_oti_get(env);
6065 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
6066 struct osd_device *dev = osd_obj2dev(obj);
6067 struct super_block *sb = osd_sb(dev);
6068 const char *devname = osd_name(dev);
6069 struct osd_it_ea_dirent *ent = it->oie_dirent;
6070 struct inode *dir = obj->oo_inode;
6071 struct htree_lock *hlock = NULL;
6072 struct buffer_head *bh = NULL;
6073 handle_t *jh = NULL;
6074 struct ldiskfs_dir_entry_2 *de;
6075 struct dentry *dentry;
6076 struct inode *inode;
6077 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
6080 bool dotdot = false;
6084 if (ent->oied_name[0] == '.') {
6085 if (ent->oied_namelen == 1)
6088 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
6092 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
6093 inode = osd_iget(info, dev, id);
6094 if (IS_ERR(inode)) {
6095 rc = PTR_ERR(inode);
6096 if (rc == -ENOENT || rc == -ESTALE) {
6097 /* Maybe dangling name entry, or
6098 * corrupted directory entry. */
6099 *attr |= LUDA_UNKNOWN;
6102 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
6103 "check_repair, dir = %lu/%u, name = %.*s, "
6104 "ino = %llu, rc = %d\n",
6105 devname, dir->i_ino, dir->i_generation,
6106 ent->oied_namelen, ent->oied_name,
6113 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
6115 rc = osd_get_lma(info, inode, dentry, lma);
6116 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
6121 /* We need to ensure that the name entry is still valid.
6122 * Because it may be removed or renamed by other already.
6124 * The unlink or rename operation will start journal before PDO lock,
6125 * so to avoid deadlock, here we need to start journal handle before
6126 * related PDO lock also. But because we do not know whether there
6127 * will be something to be repaired before PDO lock, we just start
6128 * journal without conditions.
6130 * We may need to remove the name entry firstly, then insert back.
6131 * One credit is for user quota file update.
6132 * One credit is for group quota file update.
6133 * Two credits are for dirty inode. */
6134 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
6135 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
6137 if (dev->od_dirent_journal != 0) {
6140 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
6143 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
6144 "check_repair, dir = %lu/%u, credits = %d, "
6145 "name = %.*s, ino = %llu: rc = %d\n",
6146 devname, dir->i_ino, dir->i_generation, credits,
6147 ent->oied_namelen, ent->oied_name,
6150 GOTO(out_inode, rc);
6153 if (obj->oo_hl_head != NULL) {
6154 hlock = osd_oti_get(env)->oti_hlock;
6155 /* "0" means exclusive lock for the whole directory.
6156 * We need to prevent others access such name entry
6157 * during the delete + insert. Neither HLOCK_ADD nor
6158 * HLOCK_DEL cannot guarantee the atomicity. */
6159 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
6161 down_write(&obj->oo_ext_idx_sem);
6164 if (obj->oo_hl_head != NULL) {
6165 hlock = osd_oti_get(env)->oti_hlock;
6166 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
6167 LDISKFS_HLOCK_LOOKUP);
6169 down_read(&obj->oo_ext_idx_sem);
6173 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6174 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
6175 *attr |= LUDA_IGNORE;
6180 /* For dotdot entry, if there is not enough space to hold the
6181 * FID-in-dirent, just keep them there. It only happens when the
6182 * device upgraded from 1.8 or restored from MDT file-level backup.
6183 * For the whole directory, only dotdot entry have no FID-in-dirent
6184 * and needs to get FID from LMA when readdir, it will not affect the
6185 * performance much. */
6186 if (dotdot && !osd_dotdot_has_space(de)) {
6187 *attr |= LUDA_UNKNOWN;
6193 if (lu_fid_eq(fid, &lma->lma_self_fid))
6196 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
6197 struct lu_fid *tfid = &lma->lma_self_fid;
6199 if (likely(dotdot &&
6200 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
6201 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
6202 /* It must be REMOTE_PARENT_DIR and as the
6203 * 'dotdot' entry of remote directory */
6204 *attr |= LUDA_IGNORE;
6206 CDEBUG(D_LFSCK, "%s: expect remote agent "
6207 "parent directory, but got %.*s under "
6208 "dir = %lu/%u with the FID "DFID"\n",
6209 devname, ent->oied_namelen,
6210 ent->oied_name, dir->i_ino,
6211 dir->i_generation, PFID(tfid));
6213 *attr |= LUDA_UNKNOWN;
6220 if (!fid_is_zero(fid)) {
6221 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
6223 if (rc == -ENOENT ||
6225 !(dev->od_scrub.os_file.sf_flags & SF_UPGRADE))) {
6226 /* linkEA does not recognize the dirent entry,
6227 * it may because the dirent entry corruption
6228 * and points to other's inode. */
6229 CDEBUG(D_LFSCK, "%s: the target inode does not "
6230 "recognize the dirent, dir = %lu/%u, "
6231 " name = %.*s, ino = %llu, "
6232 DFID": rc = %d\n", devname, dir->i_ino,
6233 dir->i_generation, ent->oied_namelen,
6234 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6235 *attr |= LUDA_UNKNOWN;
6240 if (rc && rc != -ENODATA) {
6241 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
6242 "dir = %lu/%u, name = %.*s, ino = %llu, "
6243 DFID": rc = %d\n", devname, dir->i_ino,
6244 dir->i_generation, ent->oied_namelen,
6245 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6246 *attr |= LUDA_UNKNOWN;
6253 /* linkEA recognizes the dirent entry, the FID-in-LMA is
6254 * valid, trusted, in spite of fid_is_sane(fid) or not. */
6255 if (*attr & LUDA_VERIFY_DRYRUN) {
6256 *fid = lma->lma_self_fid;
6257 *attr |= LUDA_REPAIR;
6264 dev->od_dirent_journal = 1;
6265 if (hlock != NULL) {
6266 ldiskfs_htree_unlock(hlock);
6269 up_read(&obj->oo_ext_idx_sem);
6275 *fid = lma->lma_self_fid;
6277 /* Update or append the FID-in-dirent. */
6278 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6279 bh, de, hlock, dotdot);
6281 *attr |= LUDA_REPAIR;
6283 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
6284 "the dirent, dir = %lu/%u, name = %.*s, "
6285 "ino = %llu, "DFID": rc = %d\n",
6286 devname, dir->i_ino, dir->i_generation,
6287 ent->oied_namelen, ent->oied_name,
6288 ent->oied_ino, PFID(fid), rc);
6290 /* lma is NULL, trust the FID-in-dirent if it is valid. */
6291 if (*attr & LUDA_VERIFY_DRYRUN) {
6292 if (fid_is_sane(fid)) {
6293 *attr |= LUDA_REPAIR;
6294 } else if (dev->od_index == 0) {
6295 lu_igif_build(fid, inode->i_ino,
6296 inode->i_generation);
6297 *attr |= LUDA_UPGRADE;
6305 dev->od_dirent_journal = 1;
6306 if (hlock != NULL) {
6307 ldiskfs_htree_unlock(hlock);
6310 up_read(&obj->oo_ext_idx_sem);
6317 if (unlikely(fid_is_sane(fid))) {
6318 /* FID-in-dirent exists, but FID-in-LMA is lost.
6319 * Trust the FID-in-dirent, and add FID-in-LMA. */
6320 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
6322 *attr |= LUDA_REPAIR;
6324 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
6325 "update dirent, dir = %lu/%u, "
6326 "name = %.*s, ino = %llu, "
6328 devname, dir->i_ino, dir->i_generation,
6329 ent->oied_namelen, ent->oied_name,
6330 ent->oied_ino, PFID(fid), rc);
6331 } else if (dev->od_index == 0) {
6332 lu_igif_build(fid, inode->i_ino, inode->i_generation);
6333 /* It is probably IGIF object. Only aappend the
6334 * FID-in-dirent. OI scrub will process FID-in-LMA. */
6335 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6336 bh, de, hlock, dotdot);
6338 *attr |= LUDA_UPGRADE;
6340 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
6341 "after the dirent, dir = %lu/%u, "
6342 "name = %.*s, ino = %llu, "
6344 devname, dir->i_ino, dir->i_generation,
6345 ent->oied_namelen, ent->oied_name,
6346 ent->oied_ino, PFID(fid), rc);
6355 if (hlock != NULL) {
6356 ldiskfs_htree_unlock(hlock);
6358 if (dev->od_dirent_journal != 0)
6359 up_write(&obj->oo_ext_idx_sem);
6361 up_read(&obj->oo_ext_idx_sem);
6365 ldiskfs_journal_stop(jh);
6369 if (rc >= 0 && !dirty)
6370 dev->od_dirent_journal = 0;
6376 * Returns the value at current position from iterator's in memory structure.
6378 * \param di struct osd_it_ea, iterator's in memory structure
6379 * \param attr attr requested for dirent.
6380 * \param lde lustre dirent
6382 * \retval 0 no error and \param lde has correct lustre dirent.
6383 * \retval -ve on error
6385 static inline int osd_it_ea_rec(const struct lu_env *env,
6386 const struct dt_it *di,
6387 struct dt_rec *dtrec, __u32 attr)
6389 struct osd_it_ea *it = (struct osd_it_ea *)di;
6390 struct osd_object *obj = it->oie_obj;
6391 struct osd_device *dev = osd_obj2dev(obj);
6392 struct osd_thread_info *oti = osd_oti_get(env);
6393 struct osd_inode_id *id = &oti->oti_id;
6394 struct lu_fid *fid = &it->oie_dirent->oied_fid;
6395 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6396 __u32 ino = it->oie_dirent->oied_ino;
6400 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
6402 if (attr & LUDA_VERIFY) {
6403 if (unlikely(ino == osd_remote_parent_ino(dev))) {
6404 attr |= LUDA_IGNORE;
6405 /* If the parent is on remote MDT, and there
6406 * is no FID-in-dirent, then we have to get
6407 * the parent FID from the linkEA. */
6408 if (!fid_is_sane(fid) &&
6409 it->oie_dirent->oied_namelen == 2 &&
6410 it->oie_dirent->oied_name[0] == '.' &&
6411 it->oie_dirent->oied_name[1] == '.')
6412 osd_get_pfid_from_linkea(env, obj, fid);
6414 rc = osd_dirent_check_repair(env, obj, it, fid, id,
6418 if (!fid_is_sane(fid))
6419 attr |= LUDA_UNKNOWN;
6421 attr &= ~LU_DIRENT_ATTRS_MASK;
6422 if (!fid_is_sane(fid)) {
6423 bool is_dotdot = false;
6424 if (it->oie_dirent->oied_namelen == 2 &&
6425 it->oie_dirent->oied_name[0] == '.' &&
6426 it->oie_dirent->oied_name[1] == '.')
6428 /* If the parent is on remote MDT, and there
6429 * is no FID-in-dirent, then we have to get
6430 * the parent FID from the linkEA. */
6431 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
6432 rc = osd_get_pfid_from_linkea(env, obj, fid);
6434 if (is_dotdot == false &&
6435 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
6438 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6441 osd_id_gen(id, ino, OSD_OII_NOGEN);
6445 /* Pack the entry anyway, at least the offset is right. */
6446 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
6447 it->oie_dirent->oied_name,
6448 it->oie_dirent->oied_namelen,
6449 it->oie_dirent->oied_type, attr);
6454 if (osd_remote_fid(env, dev, fid))
6457 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
6458 osd_add_oi_cache(oti, dev, id, fid);
6460 RETURN(rc > 0 ? 0 : rc);
6464 * Returns the record size size at current position.
6466 * This function will return record(lu_dirent) size in bytes.
6468 * \param[in] env execution environment
6469 * \param[in] di iterator's in memory structure
6470 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
6471 * calculate the lu_dirent size.
6473 * \retval record size(in bytes & in memory) of the current lu_dirent
6476 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
6479 struct osd_it_ea *it = (struct osd_it_ea *)di;
6481 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
6485 * Returns a cookie for current position of the iterator head, so that
6486 * user can use this cookie to load/start the iterator next time.
6488 * \param di iterator's in memory structure
6490 * \retval cookie for current position, on success
6492 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
6494 struct osd_it_ea *it = (struct osd_it_ea *)di;
6496 return it->oie_dirent->oied_off;
6500 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6501 * to load a directory entry at a time and stored it i inn,
6502 * in iterator's in-memory data structure.
6504 * \param di struct osd_it_ea, iterator's in memory structure
6506 * \retval +ve on success
6507 * \retval -ve on error
6509 static int osd_it_ea_load(const struct lu_env *env,
6510 const struct dt_it *di, __u64 hash)
6512 struct osd_it_ea *it = (struct osd_it_ea *)di;
6516 it->oie_file.f_pos = hash;
6518 rc = osd_ldiskfs_it_fill(env, di);
6529 * Index lookup function for interoperability mode (b11826).
6531 * \param key, key i.e. file name to be searched
6533 * \retval +ve, on success
6534 * \retval -ve, on error
6536 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6537 struct dt_rec *rec, const struct dt_key *key)
6539 struct osd_object *obj = osd_dt_obj(dt);
6544 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6545 LINVRNT(osd_invariant(obj));
6547 rc = osd_ea_lookup_rec(env, obj, rec, key);
6554 * Index and Iterator operations for interoperability
6555 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6557 static const struct dt_index_operations osd_index_ea_ops = {
6558 .dio_lookup = osd_index_ea_lookup,
6559 .dio_declare_insert = osd_index_declare_ea_insert,
6560 .dio_insert = osd_index_ea_insert,
6561 .dio_declare_delete = osd_index_declare_ea_delete,
6562 .dio_delete = osd_index_ea_delete,
6564 .init = osd_it_ea_init,
6565 .fini = osd_it_ea_fini,
6566 .get = osd_it_ea_get,
6567 .put = osd_it_ea_put,
6568 .next = osd_it_ea_next,
6569 .key = osd_it_ea_key,
6570 .key_size = osd_it_ea_key_size,
6571 .rec = osd_it_ea_rec,
6572 .rec_size = osd_it_ea_rec_size,
6573 .store = osd_it_ea_store,
6574 .load = osd_it_ea_load
6578 static void *osd_key_init(const struct lu_context *ctx,
6579 struct lu_context_key *key)
6581 struct osd_thread_info *info;
6583 OBD_ALLOC_PTR(info);
6585 return ERR_PTR(-ENOMEM);
6587 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6588 if (info->oti_it_ea_buf == NULL)
6591 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6593 info->oti_hlock = ldiskfs_htree_lock_alloc();
6594 if (info->oti_hlock == NULL)
6600 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6603 return ERR_PTR(-ENOMEM);
6606 static void osd_key_fini(const struct lu_context *ctx,
6607 struct lu_context_key *key, void* data)
6609 struct osd_thread_info *info = data;
6610 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6611 struct osd_idmap_cache *idc = info->oti_ins_cache;
6613 if (info->oti_inode != NULL)
6615 if (info->oti_hlock != NULL)
6616 ldiskfs_htree_lock_free(info->oti_hlock);
6617 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6618 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6619 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6620 lu_buf_free(&info->oti_big_buf);
6622 LASSERT(info->oti_ins_cache_size > 0);
6623 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6624 info->oti_ins_cache = NULL;
6625 info->oti_ins_cache_size = 0;
6630 static void osd_key_exit(const struct lu_context *ctx,
6631 struct lu_context_key *key, void *data)
6633 struct osd_thread_info *info = data;
6635 LASSERT(info->oti_r_locks == 0);
6636 LASSERT(info->oti_w_locks == 0);
6637 LASSERT(info->oti_txns == 0);
6640 /* type constructor/destructor: osd_type_init, osd_type_fini */
6641 LU_TYPE_INIT_FINI(osd, &osd_key);
6643 struct lu_context_key osd_key = {
6644 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6645 .lct_init = osd_key_init,
6646 .lct_fini = osd_key_fini,
6647 .lct_exit = osd_key_exit
6651 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6652 const char *name, struct lu_device *next)
6654 struct osd_device *osd = osd_dev(d);
6656 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6657 >= sizeof(osd->od_svname))
6659 return osd_procfs_init(osd, name);
6662 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6664 struct seq_server_site *ss = osd_seq_site(osd);
6668 if (osd->od_is_ost || osd->od_cl_seq != NULL)
6671 if (unlikely(ss == NULL))
6674 OBD_ALLOC_PTR(osd->od_cl_seq);
6675 if (osd->od_cl_seq == NULL)
6678 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6679 osd->od_svname, ss->ss_server_seq);
6681 OBD_FREE_PTR(osd->od_cl_seq);
6682 osd->od_cl_seq = NULL;
6686 if (ss->ss_node_id == 0) {
6687 /* If the OSD on the sequence controller(MDT0), then allocate
6688 * sequence here, otherwise allocate sequence after connected
6689 * to MDT0 (see mdt_register_lwp_callback()). */
6690 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6691 &osd->od_cl_seq->lcs_space, env);
6697 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6699 if (osd->od_cl_seq == NULL)
6702 seq_client_fini(osd->od_cl_seq);
6703 OBD_FREE_PTR(osd->od_cl_seq);
6704 osd->od_cl_seq = NULL;
6707 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6711 /* shutdown quota slave instance associated with the device */
6712 if (o->od_quota_slave != NULL) {
6713 qsd_fini(env, o->od_quota_slave);
6714 o->od_quota_slave = NULL;
6717 osd_fid_fini(env, o);
6722 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6726 if (o->od_mnt != NULL) {
6727 shrink_dcache_sb(osd_sb(o));
6728 osd_sync(env, &o->od_dt_dev);
6737 static int osd_mount(const struct lu_env *env,
6738 struct osd_device *o, struct lustre_cfg *cfg)
6740 const char *name = lustre_cfg_string(cfg, 0);
6741 const char *dev = lustre_cfg_string(cfg, 1);
6743 unsigned long page, s_flags, lmd_flags = 0;
6744 struct page *__page;
6745 struct file_system_type *type;
6746 char *options = NULL;
6748 struct osd_thread_info *info = osd_oti_get(env);
6749 struct lu_fid *fid = &info->oti_fid;
6750 struct inode *inode;
6751 int rc = 0, force_over_512tb = 0;
6754 if (o->od_mnt != NULL)
6757 if (strlen(dev) >= sizeof(o->od_mntdev))
6759 strcpy(o->od_mntdev, dev);
6761 str = lustre_cfg_string(cfg, 2);
6762 s_flags = simple_strtoul(str, NULL, 0);
6763 str = strstr(str, ":");
6765 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6766 opts = lustre_cfg_string(cfg, 3);
6768 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6769 CERROR("%s: device %s extents feature is not guaranteed to "
6770 "work on big-endian systems. Use \"bigendian_extents\" "
6771 "mount option to override.\n", name, dev);
6775 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6776 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
6777 CWARN("force_over_128tb option is deprecated. "
6778 "Filesystems less than 512TB can be created without any "
6779 "force options. Use force_over_512tb option for "
6780 "filesystems greater than 512TB.\n");
6783 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
6784 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
6785 CWARN("force_over_256tb option is deprecated. "
6786 "Filesystems less than 512TB can be created without any "
6787 "force options. Use force_over_512tb option for "
6788 "filesystems greater than 512TB.\n");
6792 if (opts != NULL && strstr(opts, "force_over_512tb") != NULL)
6793 force_over_512tb = 1;
6795 __page = alloc_page(GFP_KERNEL);
6797 GOTO(out, rc = -ENOMEM);
6798 page = (unsigned long)page_address(__page);
6799 options = (char *)page;
6802 /* strip out the options for back compatiblity */
6803 static char *sout[] = {
6810 /* strip out option we processed in osd */
6811 "bigendian_extents",
6817 strcat(options, opts);
6818 for (rc = 0, str = options; sout[rc]; ) {
6819 char *op = strstr(str, sout[rc]);
6825 if (op == options || *(op - 1) == ',') {
6826 str = op + strlen(sout[rc]);
6827 if (*str == ',' || *str == '\0') {
6828 *str == ',' ? str++ : str;
6829 memmove(op, str, strlen(str) + 1);
6832 for (str = op; *str != ',' && *str != '\0'; str++)
6836 strncat(options, "user_xattr,acl", 14);
6839 /* Glom up mount options */
6840 if (*options != '\0')
6841 strcat(options, ",");
6842 strlcat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
6844 type = get_fs_type("ldiskfs");
6846 CERROR("%s: cannot find ldiskfs module\n", name);
6847 GOTO(out, rc = -ENODEV);
6850 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6851 module_put(type->owner);
6853 if (IS_ERR(o->od_mnt)) {
6854 rc = PTR_ERR(o->od_mnt);
6856 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6860 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
6861 osd_sb(o)->s_blocksize_bits > 512ULL << 40 &&
6862 force_over_512tb == 0) {
6863 CERROR("%s: device %s LDISKFS does not support filesystems "
6864 "greater than 512TB and can cause data corruption. "
6865 "Use \"force_over_512tb\" mount option to override.\n",
6867 GOTO(out_mnt, rc = -EINVAL);
6870 if (lmd_flags & LMD_FLG_DEV_RDONLY) {
6871 #ifdef HAVE_DEV_SET_RDONLY
6872 dev_set_rdonly(osd_sb(o)->s_bdev);
6873 o->od_dt_dev.dd_rdonly = 1;
6874 LCONSOLE_WARN("%s: set dev_rdonly on this device\n", name);
6876 LCONSOLE_WARN("%s: not support dev_rdonly on this device",
6879 GOTO(out_mnt, rc = -EOPNOTSUPP);
6882 #ifdef HAVE_DEV_SET_RDONLY
6883 if (dev_check_rdonly(osd_sb(o)->s_bdev)) {
6884 CERROR("%s: underlying device %s is marked as "
6885 "read-only. Setup failed\n", name, dev);
6887 GOTO(out_mnt, rc = -EROFS);
6892 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6893 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6894 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6895 GOTO(out_mnt, rc = -EINVAL);
6898 #ifdef LDISKFS_MOUNT_DIRDATA
6899 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6900 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6901 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6902 else if (!o->od_is_ost)
6903 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6904 "enabling the dirdata feature. If you do not want to "
6905 "downgrade to Lustre-1.x again, you can enable it via "
6906 "'tune2fs -O dirdata device'\n", name, dev);
6908 inode = osd_sb(o)->s_root->d_inode;
6909 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6910 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6912 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6916 if (lmd_flags & LMD_FLG_NOSCRUB)
6927 __free_page(__page);
6932 static struct lu_device *osd_device_fini(const struct lu_env *env,
6933 struct lu_device *d)
6935 struct osd_device *o = osd_dev(d);
6938 osd_shutdown(env, o);
6940 osd_scrub_cleanup(env, o);
6941 osd_obj_map_fini(o);
6947 static int osd_device_init0(const struct lu_env *env,
6948 struct osd_device *o,
6949 struct lustre_cfg *cfg)
6951 struct lu_device *l = osd2lu_dev(o);
6952 struct osd_thread_info *info;
6956 /* if the module was re-loaded, env can loose its keys */
6957 rc = lu_env_refill((struct lu_env *) env);
6960 info = osd_oti_get(env);
6963 l->ld_ops = &osd_lu_ops;
6964 o->od_dt_dev.dd_ops = &osd_dt_ops;
6966 spin_lock_init(&o->od_osfs_lock);
6967 mutex_init(&o->od_otable_mutex);
6968 INIT_LIST_HEAD(&o->od_orphan_list);
6970 o->od_read_cache = 1;
6971 o->od_writethrough_cache = 1;
6972 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6974 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6975 sizeof(o->od_svname));
6976 if (cplen >= sizeof(o->od_svname)) {
6981 o->od_index = -1; /* -1 means index is invalid */
6982 rc = server_name2index(o->od_svname, &o->od_index, NULL);
6983 if (rc == LDD_F_SV_TYPE_OST)
6986 o->od_full_scrub_ratio = OFSR_DEFAULT;
6987 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6988 rc = osd_mount(env, o, cfg);
6992 rc = osd_obj_map_init(env, o);
6996 rc = lu_site_init(&o->od_site, l);
6998 GOTO(out_compat, rc);
6999 o->od_site.ls_bottom_dev = l;
7001 rc = lu_site_init_finish(&o->od_site);
7005 INIT_LIST_HEAD(&o->od_ios_list);
7006 /* setup scrub, including OI files initialization */
7007 rc = osd_scrub_setup(env, o);
7011 rc = osd_procfs_init(o, o->od_svname);
7013 CERROR("%s: can't initialize procfs: rc = %d\n",
7015 GOTO(out_scrub, rc);
7018 LASSERT(l->ld_site->ls_linkage.next != NULL);
7019 LASSERT(l->ld_site->ls_linkage.prev != NULL);
7021 /* initialize quota slave instance */
7022 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
7024 if (IS_ERR(o->od_quota_slave)) {
7025 rc = PTR_ERR(o->od_quota_slave);
7026 o->od_quota_slave = NULL;
7027 GOTO(out_procfs, rc);
7035 osd_scrub_cleanup(env, o);
7037 lu_site_fini(&o->od_site);
7039 osd_obj_map_fini(o);
7046 static struct lu_device *osd_device_alloc(const struct lu_env *env,
7047 struct lu_device_type *t,
7048 struct lustre_cfg *cfg)
7050 struct osd_device *o;
7055 return ERR_PTR(-ENOMEM);
7057 rc = dt_device_init(&o->od_dt_dev, t);
7059 /* Because the ctx might be revived in dt_device_init,
7060 * refill the env here */
7061 lu_env_refill((struct lu_env *)env);
7062 rc = osd_device_init0(env, o, cfg);
7064 dt_device_fini(&o->od_dt_dev);
7067 if (unlikely(rc != 0))
7070 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
7073 static struct lu_device *osd_device_free(const struct lu_env *env,
7074 struct lu_device *d)
7076 struct osd_device *o = osd_dev(d);
7079 /* XXX: make osd top device in order to release reference */
7080 d->ld_site->ls_top_dev = d;
7081 lu_site_purge(env, d->ld_site, -1);
7082 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
7083 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
7084 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
7086 lu_site_fini(&o->od_site);
7087 dt_device_fini(&o->od_dt_dev);
7092 static int osd_process_config(const struct lu_env *env,
7093 struct lu_device *d, struct lustre_cfg *cfg)
7095 struct osd_device *o = osd_dev(d);
7099 switch (cfg->lcfg_command) {
7101 rc = osd_mount(env, o, cfg);
7104 lu_dev_del_linkage(d->ld_site, d);
7105 rc = osd_shutdown(env, o);
7108 LASSERT(&o->od_dt_dev);
7109 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
7110 cfg, &o->od_dt_dev);
7111 if (rc > 0 || rc == -ENOSYS)
7112 rc = class_process_proc_param(PARAM_OST,
7113 lprocfs_osd_obd_vars,
7114 cfg, &o->od_dt_dev);
7123 static int osd_recovery_complete(const struct lu_env *env,
7124 struct lu_device *d)
7126 struct osd_device *osd = osd_dev(d);
7130 if (osd->od_quota_slave == NULL)
7133 /* start qsd instance on recovery completion, this notifies the quota
7134 * slave code that we are about to process new requests now */
7135 rc = qsd_start(env, osd->od_quota_slave);
7140 * we use exports to track all osd users
7142 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
7143 struct obd_device *obd, struct obd_uuid *cluuid,
7144 struct obd_connect_data *data, void *localdata)
7146 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7147 struct lustre_handle conn;
7151 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
7153 rc = class_connect(&conn, obd, cluuid);
7157 *exp = class_conn2export(&conn);
7159 spin_lock(&osd->od_osfs_lock);
7161 spin_unlock(&osd->od_osfs_lock);
7167 * once last export (we don't count self-export) disappeared
7168 * osd can be released
7170 static int osd_obd_disconnect(struct obd_export *exp)
7172 struct obd_device *obd = exp->exp_obd;
7173 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7174 int rc, release = 0;
7177 /* Only disconnect the underlying layers on the final disconnect. */
7178 spin_lock(&osd->od_osfs_lock);
7180 if (osd->od_connects == 0)
7182 spin_unlock(&osd->od_osfs_lock);
7184 rc = class_disconnect(exp); /* bz 9811 */
7186 if (rc == 0 && release)
7187 class_manual_cleanup(obd);
7191 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
7192 struct lu_device *dev)
7194 struct osd_device *osd = osd_dev(dev);
7195 struct lr_server_data *lsd =
7196 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
7200 if (osd->od_quota_slave != NULL) {
7201 /* set up quota slave objects */
7202 result = qsd_prepare(env, osd->od_quota_slave);
7207 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
7208 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
7209 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
7210 osd->od_index_in_idif = 1;
7212 osd->od_index_in_idif = 0;
7213 result = osd_register_proc_index_in_idif(osd);
7218 osd->od_index_in_idif = 1;
7222 result = osd_fid_init(env, osd);
7227 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
7228 struct lu_fid *fid, struct md_op_data *op_data)
7230 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
7232 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
7235 static const struct lu_object_operations osd_lu_obj_ops = {
7236 .loo_object_init = osd_object_init,
7237 .loo_object_delete = osd_object_delete,
7238 .loo_object_release = osd_object_release,
7239 .loo_object_free = osd_object_free,
7240 .loo_object_print = osd_object_print,
7241 .loo_object_invariant = osd_object_invariant
7244 const struct lu_device_operations osd_lu_ops = {
7245 .ldo_object_alloc = osd_object_alloc,
7246 .ldo_process_config = osd_process_config,
7247 .ldo_recovery_complete = osd_recovery_complete,
7248 .ldo_prepare = osd_prepare,
7251 static const struct lu_device_type_operations osd_device_type_ops = {
7252 .ldto_init = osd_type_init,
7253 .ldto_fini = osd_type_fini,
7255 .ldto_start = osd_type_start,
7256 .ldto_stop = osd_type_stop,
7258 .ldto_device_alloc = osd_device_alloc,
7259 .ldto_device_free = osd_device_free,
7261 .ldto_device_init = osd_device_init,
7262 .ldto_device_fini = osd_device_fini
7265 static struct lu_device_type osd_device_type = {
7266 .ldt_tags = LU_DEVICE_DT,
7267 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
7268 .ldt_ops = &osd_device_type_ops,
7269 .ldt_ctx_tags = LCT_LOCAL,
7272 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
7274 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7275 struct super_block *sb = osd_sb(osd);
7277 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
7281 * lprocfs legacy support.
7283 static struct obd_ops osd_obd_device_ops = {
7284 .o_owner = THIS_MODULE,
7285 .o_connect = osd_obd_connect,
7286 .o_disconnect = osd_obd_disconnect,
7287 .o_fid_alloc = osd_fid_alloc,
7288 .o_health_check = osd_health_check,
7291 static int __init osd_init(void)
7295 LASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
7296 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
7297 /* please, try to keep osd_thread_info smaller than a page */
7298 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
7303 rc = lu_kmem_init(ldiskfs_caches);
7307 rc = class_register_type(&osd_obd_device_ops, NULL, true,
7308 lprocfs_osd_module_vars,
7309 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
7311 lu_kmem_fini(ldiskfs_caches);
7315 static void __exit osd_exit(void)
7317 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
7318 lu_kmem_fini(ldiskfs_caches);
7321 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7322 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
7323 MODULE_VERSION(LUSTRE_VERSION_STRING);
7324 MODULE_LICENSE("GPL");
7326 module_init(osd_init);
7327 module_exit(osd_exit);