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;
1607 /* on pending IO in this thread should left from prev. request */
1608 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1610 sb_start_write(osd_sb(osd_dt_dev(d)));
1612 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1614 oh->ot_quota_trans = &oti->oti_quota_trans;
1615 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1619 th->th_tags = LCT_TX_HANDLE;
1621 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1622 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1625 memset(oti->oti_declare_ops, 0,
1626 sizeof(oti->oti_declare_ops));
1627 memset(oti->oti_declare_ops_cred, 0,
1628 sizeof(oti->oti_declare_ops_cred));
1629 memset(oti->oti_declare_ops_used, 0,
1630 sizeof(oti->oti_declare_ops_used));
1632 sb_end_write(osd_sb(osd_dt_dev(d)));
1633 th = ERR_PTR(-ENOMEM);
1638 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1640 struct osd_thread_info *oti = osd_oti_get(env);
1641 struct osd_thandle *oh;
1643 oh = container_of0(th, struct osd_thandle, ot_super);
1644 LASSERT(oh != NULL);
1646 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1647 oti->oti_declare_ops[OSD_OT_CREATE],
1648 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1649 oti->oti_declare_ops_used[OSD_OT_CREATE],
1650 oti->oti_declare_ops[OSD_OT_DESTROY],
1651 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1652 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1653 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1654 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1655 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1656 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1657 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1658 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1659 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1660 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1661 oti->oti_declare_ops[OSD_OT_WRITE],
1662 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1663 oti->oti_declare_ops_used[OSD_OT_WRITE],
1664 oti->oti_declare_ops[OSD_OT_PUNCH],
1665 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1666 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1667 oti->oti_declare_ops[OSD_OT_QUOTA],
1668 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1669 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1670 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1671 oti->oti_declare_ops[OSD_OT_INSERT],
1672 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1673 oti->oti_declare_ops_used[OSD_OT_INSERT],
1674 oti->oti_declare_ops[OSD_OT_DELETE],
1675 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1676 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1677 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1678 oti->oti_declare_ops[OSD_OT_REF_ADD],
1679 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1680 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1681 oti->oti_declare_ops[OSD_OT_REF_DEL],
1682 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1683 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1687 * Concurrency: shouldn't matter.
1689 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1692 struct osd_thread_info *oti = osd_oti_get(env);
1693 struct osd_device *dev = osd_dt_dev(d);
1695 struct osd_thandle *oh;
1700 LASSERT(current->journal_info == NULL);
1702 oh = container_of0(th, struct osd_thandle, ot_super);
1703 LASSERT(oh != NULL);
1704 LASSERT(oh->ot_handle == NULL);
1706 rc = dt_txn_hook_start(env, d, th);
1710 if (unlikely(osd_param_is_not_sane(dev, th))) {
1711 static unsigned long last_printed;
1712 static int last_credits;
1714 /* don't make noise on a tiny testing systems
1715 * actual credits misuse will be caught anyway */
1716 if (last_credits != oh->ot_credits &&
1717 time_after(jiffies, last_printed +
1718 msecs_to_jiffies(60 * MSEC_PER_SEC)) &&
1719 osd_transaction_size(dev) > 512) {
1720 CWARN("%s: credits %u > trans_max %u\n", osd_name(dev),
1721 oh->ot_credits, osd_transaction_size(dev));
1722 osd_trans_dump_creds(env, th);
1723 libcfs_debug_dumpstack(NULL);
1724 last_credits = oh->ot_credits;
1725 last_printed = jiffies;
1727 /* XXX Limit the credits to 'max_transaction_buffers', and
1728 * let the underlying filesystem to catch the error if
1729 * we really need so many credits.
1731 * This should be removed when we can calculate the
1732 * credits precisely. */
1733 oh->ot_credits = osd_transaction_size(dev);
1734 } else if (ldiskfs_track_declares_assert != 0) {
1735 /* reserve few credits to prevent an assertion in JBD
1736 * our debugging mechanism will be able to detected
1737 * overuse. this can help to debug single-update
1739 oh->ot_credits += 10;
1740 if (unlikely(osd_param_is_not_sane(dev, th)))
1741 oh->ot_credits = osd_transaction_size(dev);
1745 * XXX temporary stuff. Some abstraction layer should
1748 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1752 LASSERT(oti->oti_txns == 0);
1753 lu_context_init(&th->th_ctx, th->th_tags);
1754 lu_context_enter(&th->th_ctx);
1756 lu_device_get(&d->dd_lu_dev);
1757 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1768 static int osd_seq_exists(const struct lu_env *env,
1769 struct osd_device *osd, u64 seq)
1771 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1772 struct seq_server_site *ss = osd_seq_site(osd);
1776 LASSERT(ss != NULL);
1777 LASSERT(ss->ss_server_fld != NULL);
1779 rc = osd_fld_lookup(env, osd, seq, range);
1782 CERROR("%s: can't lookup FLD sequence %#llx: rc = %d\n",
1783 osd_name(osd), seq, rc);
1787 RETURN(ss->ss_node_id == range->lsr_index);
1790 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1792 struct dt_txn_commit_cb *dcb;
1793 struct dt_txn_commit_cb *tmp;
1795 /* call per-transaction stop callbacks if any */
1796 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1798 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1799 "commit callback entry: magic=%x name='%s'\n",
1800 dcb->dcb_magic, dcb->dcb_name);
1801 list_del_init(&dcb->dcb_linkage);
1802 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1807 * Concurrency: shouldn't matter.
1809 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1812 int rc = 0, remove_agents = 0;
1813 struct osd_thandle *oh;
1814 struct osd_thread_info *oti = osd_oti_get(env);
1815 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1816 struct osd_device *osd = osd_dt_dev(th->th_dev);
1817 struct qsd_instance *qsd = osd->od_quota_slave;
1818 struct lquota_trans *qtrans;
1821 oh = container_of0(th, struct osd_thandle, ot_super);
1823 /* reset OI cache for safety */
1824 oti->oti_ins_cache_used = 0;
1826 remove_agents = oh->ot_remove_agents;
1828 qtrans = oh->ot_quota_trans;
1829 oh->ot_quota_trans = NULL;
1831 if (oh->ot_handle != NULL) {
1833 handle_t *hdl = oh->ot_handle;
1836 * add commit callback
1837 * notice we don't do this in osd_trans_start()
1838 * as underlying transaction can change during truncate
1840 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1843 LASSERT(oti->oti_txns == 1);
1846 rc = dt_txn_hook_stop(env, th);
1848 CERROR("%s: failed in transaction hook: rc = %d\n",
1851 osd_trans_stop_cb(oh, rc);
1852 /* hook functions might modify th_sync */
1853 hdl->h_sync = th->th_sync;
1855 oh->ot_handle = NULL;
1856 OSD_CHECK_SLOW_TH(oh, osd, rc2 = ldiskfs_journal_stop(hdl));
1858 CERROR("%s: failed to stop transaction: rc = %d\n",
1859 osd_name(osd), rc2);
1863 osd_trans_stop_cb(oh, th->th_result);
1867 /* inform the quota slave device that the transaction is stopping */
1868 qsd_op_end(env, qsd, qtrans);
1870 /* as we want IO to journal and data IO be concurrent, we don't block
1871 * awaiting data IO completion in osd_do_bio(), instead we wait here
1872 * once transaction is submitted to the journal. all reqular requests
1873 * don't do direct IO (except read/write), thus this wait_event becomes
1876 * IMPORTANT: we have to wait till any IO submited by the thread is
1877 * completed otherwise iobuf may be corrupted by different request
1879 wait_event(iobuf->dr_wait,
1880 atomic_read(&iobuf->dr_numreqs) == 0);
1881 osd_fini_iobuf(osd, iobuf);
1883 rc = iobuf->dr_error;
1885 if (unlikely(remove_agents != 0))
1886 osd_process_scheduled_agent_removals(env, osd);
1888 sb_end_write(osd_sb(osd));
1893 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1895 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1898 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1899 LASSERT(&dcb->dcb_func != NULL);
1900 if (dcb->dcb_flags & DCB_TRANS_STOP)
1901 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1903 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1909 * Called just before object is freed. Releases all resources except for
1910 * object itself (that is released by osd_object_free()).
1912 * Concurrency: no concurrent access is possible that late in object
1915 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1917 struct osd_object *obj = osd_obj(l);
1918 struct inode *inode = obj->oo_inode;
1920 LINVRNT(osd_invariant(obj));
1923 * If object is unlinked remove fid->ino mapping from object index.
1926 osd_index_fini(obj);
1927 if (inode != NULL) {
1928 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1929 qid_t uid = i_uid_read(inode);
1930 qid_t gid = i_gid_read(inode);
1933 obj->oo_inode = NULL;
1936 struct osd_thread_info *info = osd_oti_get(env);
1937 struct lquota_id_info *qi = &info->oti_qi;
1939 /* Release granted quota to master if necessary */
1940 qi->lqi_id.qid_uid = uid;
1941 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1943 qi->lqi_id.qid_uid = gid;
1944 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1950 * Concurrency: ->loo_object_release() is called under site spin-lock.
1952 static void osd_object_release(const struct lu_env *env,
1953 struct lu_object *l)
1955 struct osd_object *o = osd_obj(l);
1956 /* nobody should be releasing a non-destroyed object with nlink=0
1957 * the API allows this, but ldiskfs doesn't like and then report
1958 * this inode as deleted */
1959 if (unlikely(!o->oo_destroyed && o->oo_inode && o->oo_inode->i_nlink == 0))
1964 * Concurrency: shouldn't matter.
1966 static int osd_object_print(const struct lu_env *env, void *cookie,
1967 lu_printer_t p, const struct lu_object *l)
1969 struct osd_object *o = osd_obj(l);
1970 struct iam_descr *d;
1972 if (o->oo_dir != NULL)
1973 d = o->oo_dir->od_container.ic_descr;
1976 return (*p)(env, cookie,
1977 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1979 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1980 o->oo_inode ? o->oo_inode->i_generation : 0,
1981 d ? d->id_ops->id_name : "plain");
1985 * Concurrency: shouldn't matter.
1987 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1988 struct obd_statfs *sfs)
1990 struct osd_device *osd = osd_dt_dev(d);
1991 struct super_block *sb = osd_sb(osd);
1992 struct kstatfs *ksfs;
1996 if (unlikely(osd->od_mnt == NULL))
1997 return -EINPROGRESS;
1999 /* osd_lproc.c call this without env, allocate ksfs for that case */
2000 if (unlikely(env == NULL)) {
2001 OBD_ALLOC_PTR(ksfs);
2005 ksfs = &osd_oti_get(env)->oti_ksfs;
2008 result = sb->s_op->statfs(sb->s_root, ksfs);
2012 statfs_pack(sfs, ksfs);
2013 if (unlikely(sb->s_flags & MS_RDONLY))
2014 sfs->os_state |= OS_STATE_READONLY;
2015 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
2016 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2017 sfs->os_maxbytes = sb->s_maxbytes;
2019 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2022 * Reserve some space so to avoid fragmenting the filesystem too much.
2023 * Fragmentation not only impacts performance, but can also increase
2024 * metadata overhead significantly, causing grant calculation to be
2027 * Reserve 0.78% of total space, at least 8MB for small filesystems.
2029 CLASSERT(OSD_STATFS_RESERVED > LDISKFS_MAX_BLOCK_SIZE);
2030 reserved = OSD_STATFS_RESERVED >> sb->s_blocksize_bits;
2031 if (likely(sfs->os_blocks >= reserved << OSD_STATFS_RESERVED_SHIFT))
2032 reserved = sfs->os_blocks >> OSD_STATFS_RESERVED_SHIFT;
2034 sfs->os_blocks -= reserved;
2035 sfs->os_bfree -= min(reserved, sfs->os_bfree);
2036 sfs->os_bavail -= min(reserved, sfs->os_bavail);
2039 if (unlikely(env == NULL))
2045 * Estimate space needed for file creations. We assume the largest filename
2046 * which is 2^64 - 1, hence a filename of 20 chars.
2047 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
2049 #ifdef __LDISKFS_DIR_REC_LEN
2050 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
2052 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
2056 * Concurrency: doesn't access mutable data.
2058 static void osd_conf_get(const struct lu_env *env,
2059 const struct dt_device *dev,
2060 struct dt_device_param *param)
2062 struct super_block *sb = osd_sb(osd_dt_dev(dev));
2066 * XXX should be taken from not-yet-existing fs abstraction layer.
2068 param->ddp_max_name_len = LDISKFS_NAME_LEN;
2069 param->ddp_max_nlink = LDISKFS_LINK_MAX;
2070 param->ddp_symlink_max = sb->s_blocksize;
2071 param->ddp_mount_type = LDD_MT_LDISKFS;
2072 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
2073 param->ddp_maxbytes = sb->s_maxbytes;
2075 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
2076 /* inode are statically allocated, so per-inode space consumption
2077 * is the space consumed by the directory entry */
2078 param->ddp_inodespace = PER_OBJ_USAGE;
2079 /* EXT_INIT_MAX_LEN is the theoretical maximum extent size (32k blocks
2080 * = 128MB) which is unlikely to be hit in real life. Report a smaller
2081 * maximum length to not under count the actual number of extents
2082 * needed for writing a file. */
2083 param->ddp_max_extent_blks = EXT_INIT_MAX_LEN >> 2;
2084 /* worst-case extent insertion metadata overhead */
2085 param->ddp_extent_tax = 6 * LDISKFS_BLOCK_SIZE(sb);
2086 param->ddp_mntopts = 0;
2087 if (test_opt(sb, XATTR_USER))
2088 param->ddp_mntopts |= MNTOPT_USERXATTR;
2089 if (test_opt(sb, POSIX_ACL))
2090 param->ddp_mntopts |= MNTOPT_ACL;
2092 /* LOD might calculate the max stripe count based on max_ea_size,
2093 * so we need take account in the overhead as well,
2094 * xattr_header + magic + xattr_entry_head */
2095 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
2096 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
2098 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
2099 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
2100 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
2104 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
2108 * Concurrency: shouldn't matter.
2110 static int osd_sync(const struct lu_env *env, struct dt_device *d)
2114 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2116 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
2118 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
2119 LUSTRE_OSD_LDISKFS_NAME, rc);
2125 * Start commit for OSD device.
2127 * An implementation of dt_commit_async method for OSD device.
2128 * Asychronously starts underlayng fs sync and thereby a transaction
2131 * \param env environment
2132 * \param d dt device
2134 * \see dt_device_operations
2136 static int osd_commit_async(const struct lu_env *env,
2137 struct dt_device *d)
2139 struct super_block *s = osd_sb(osd_dt_dev(d));
2142 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
2143 RETURN(s->s_op->sync_fs(s, 0));
2147 * Concurrency: shouldn't matter.
2150 static int osd_ro(const struct lu_env *env, struct dt_device *d)
2152 struct super_block *sb = osd_sb(osd_dt_dev(d));
2153 struct block_device *dev = sb->s_bdev;
2154 #ifdef HAVE_DEV_SET_RDONLY
2155 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
2158 int rc = -EOPNOTSUPP;
2162 #ifdef HAVE_DEV_SET_RDONLY
2163 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
2165 if (sb->s_op->freeze_fs) {
2166 rc = sb->s_op->freeze_fs(sb);
2171 if (jdev && (jdev != dev)) {
2172 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
2174 dev_set_rdonly(jdev);
2176 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
2177 dev_set_rdonly(dev);
2179 if (sb->s_op->unfreeze_fs)
2180 sb->s_op->unfreeze_fs(sb);
2185 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
2186 osd_dt_dev(d)->od_svname, (long)dev, rc);
2192 * Note: we do not count into QUOTA here.
2193 * If we mount with --data_journal we may need more.
2195 const int osd_dto_credits_noquota[DTO_NR] = {
2198 * INDEX_EXTRA_TRANS_BLOCKS(8) +
2199 * SINGLEDATA_TRANS_BLOCKS(8)
2200 * XXX Note: maybe iam need more, since iam have more level than
2203 [DTO_INDEX_INSERT] = 16,
2206 * just modify a single entry, probably merge few within a block
2208 [DTO_INDEX_DELETE] = 1,
2212 [DTO_INDEX_UPDATE] = 16,
2214 * 4(inode, inode bits, groups, GDT)
2215 * notice: OI updates are counted separately with DTO_INDEX_INSERT
2217 [DTO_OBJECT_CREATE] = 4,
2219 * 4(inode, inode bits, groups, GDT)
2220 * notice: OI updates are counted separately with DTO_INDEX_DELETE
2222 [DTO_OBJECT_DELETE] = 4,
2224 * Attr set credits (inode)
2226 [DTO_ATTR_SET_BASE] = 1,
2228 * Xattr set. The same as xattr of EXT3.
2229 * DATA_TRANS_BLOCKS(14)
2230 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
2231 * are also counted in. Do not know why?
2233 [DTO_XATTR_SET] = 14,
2235 * credits for inode change during write.
2237 [DTO_WRITE_BASE] = 3,
2239 * credits for single block write.
2241 [DTO_WRITE_BLOCK] = 14,
2243 * Attr set credits for chown.
2244 * This is extra credits for setattr, and it is null without quota
2246 [DTO_ATTR_SET_CHOWN] = 0
2249 static const struct dt_device_operations osd_dt_ops = {
2250 .dt_root_get = osd_root_get,
2251 .dt_statfs = osd_statfs,
2252 .dt_trans_create = osd_trans_create,
2253 .dt_trans_start = osd_trans_start,
2254 .dt_trans_stop = osd_trans_stop,
2255 .dt_trans_cb_add = osd_trans_cb_add,
2256 .dt_conf_get = osd_conf_get,
2257 .dt_sync = osd_sync,
2259 .dt_commit_async = osd_commit_async,
2262 static void osd_object_read_lock(const struct lu_env *env,
2263 struct dt_object *dt, unsigned role)
2265 struct osd_object *obj = osd_dt_obj(dt);
2266 struct osd_thread_info *oti = osd_oti_get(env);
2268 LINVRNT(osd_invariant(obj));
2270 LASSERT(obj->oo_owner != env);
2271 down_read_nested(&obj->oo_sem, role);
2273 LASSERT(obj->oo_owner == NULL);
2277 static void osd_object_write_lock(const struct lu_env *env,
2278 struct dt_object *dt, unsigned role)
2280 struct osd_object *obj = osd_dt_obj(dt);
2281 struct osd_thread_info *oti = osd_oti_get(env);
2283 LINVRNT(osd_invariant(obj));
2285 LASSERT(obj->oo_owner != env);
2286 down_write_nested(&obj->oo_sem, role);
2288 LASSERT(obj->oo_owner == NULL);
2289 obj->oo_owner = env;
2293 static void osd_object_read_unlock(const struct lu_env *env,
2294 struct dt_object *dt)
2296 struct osd_object *obj = osd_dt_obj(dt);
2297 struct osd_thread_info *oti = osd_oti_get(env);
2299 LINVRNT(osd_invariant(obj));
2301 LASSERT(oti->oti_r_locks > 0);
2303 up_read(&obj->oo_sem);
2306 static void osd_object_write_unlock(const struct lu_env *env,
2307 struct dt_object *dt)
2309 struct osd_object *obj = osd_dt_obj(dt);
2310 struct osd_thread_info *oti = osd_oti_get(env);
2312 LINVRNT(osd_invariant(obj));
2314 LASSERT(obj->oo_owner == env);
2315 LASSERT(oti->oti_w_locks > 0);
2317 obj->oo_owner = NULL;
2318 up_write(&obj->oo_sem);
2321 static int osd_object_write_locked(const struct lu_env *env,
2322 struct dt_object *dt)
2324 struct osd_object *obj = osd_dt_obj(dt);
2326 LINVRNT(osd_invariant(obj));
2328 return obj->oo_owner == env;
2331 static struct timespec *osd_inode_time(const struct lu_env *env,
2332 struct inode *inode, __u64 seconds)
2334 struct osd_thread_info *oti = osd_oti_get(env);
2335 struct timespec *t = &oti->oti_time;
2337 t->tv_sec = seconds;
2339 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
2343 static void osd_inode_getattr(const struct lu_env *env,
2344 struct inode *inode, struct lu_attr *attr)
2346 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
2347 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
2348 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
2351 attr->la_atime = LTIME_S(inode->i_atime);
2352 attr->la_mtime = LTIME_S(inode->i_mtime);
2353 attr->la_ctime = LTIME_S(inode->i_ctime);
2354 attr->la_mode = inode->i_mode;
2355 attr->la_size = i_size_read(inode);
2356 attr->la_blocks = inode->i_blocks;
2357 attr->la_uid = i_uid_read(inode);
2358 attr->la_gid = i_gid_read(inode);
2359 attr->la_flags = ll_inode_to_ext_flags(inode->i_flags);
2360 attr->la_nlink = inode->i_nlink;
2361 attr->la_rdev = inode->i_rdev;
2362 attr->la_blksize = 1 << inode->i_blkbits;
2363 attr->la_blkbits = inode->i_blkbits;
2366 static int osd_attr_get(const struct lu_env *env,
2367 struct dt_object *dt,
2368 struct lu_attr *attr)
2370 struct osd_object *obj = osd_dt_obj(dt);
2372 if (unlikely(!dt_object_exists(dt)))
2374 if (unlikely(obj->oo_destroyed))
2377 LASSERT(!dt_object_remote(dt));
2378 LINVRNT(osd_invariant(obj));
2380 spin_lock(&obj->oo_guard);
2381 osd_inode_getattr(env, obj->oo_inode, attr);
2382 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
2383 attr->la_flags |= LUSTRE_ORPHAN_FL;
2384 spin_unlock(&obj->oo_guard);
2389 static int osd_declare_attr_set(const struct lu_env *env,
2390 struct dt_object *dt,
2391 const struct lu_attr *attr,
2392 struct thandle *handle)
2394 struct osd_thandle *oh;
2395 struct osd_object *obj;
2396 struct osd_thread_info *info = osd_oti_get(env);
2397 struct lquota_id_info *qi = &info->oti_qi;
2405 LASSERT(dt != NULL);
2406 LASSERT(handle != NULL);
2408 obj = osd_dt_obj(dt);
2409 LASSERT(osd_invariant(obj));
2411 oh = container_of0(handle, struct osd_thandle, ot_super);
2412 LASSERT(oh->ot_handle == NULL);
2414 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
2415 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2417 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2418 osd_dto_credits_noquota[DTO_XATTR_SET]);
2420 if (attr == NULL || obj->oo_inode == NULL)
2423 bspace = obj->oo_inode->i_blocks;
2424 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
2425 bspace = toqb(bspace);
2427 /* Changing ownership is always preformed by super user, it should not
2430 * We still need to call the osd_declare_qid() to calculate the journal
2431 * credits for updating quota accounting files and to trigger quota
2432 * space adjustment once the operation is completed.*/
2433 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
2435 uid = i_uid_read(obj->oo_inode);
2436 qi->lqi_type = USRQUOTA;
2437 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
2438 /* inode accounting */
2439 qi->lqi_is_blk = false;
2441 /* one more inode for the new uid ... */
2442 qi->lqi_id.qid_uid = attr->la_uid;
2444 /* Reserve credits for the new uid */
2445 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2446 if (rc == -EDQUOT || rc == -EINPROGRESS)
2451 /* and one less inode for the current uid */
2452 qi->lqi_id.qid_uid = uid;
2454 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2455 if (rc == -EDQUOT || rc == -EINPROGRESS)
2460 /* block accounting */
2461 qi->lqi_is_blk = true;
2463 /* more blocks for the new uid ... */
2464 qi->lqi_id.qid_uid = attr->la_uid;
2465 qi->lqi_space = bspace;
2467 * Credits for the new uid has been reserved, re-use "obj"
2468 * to save credit reservation.
2470 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2471 if (rc == -EDQUOT || rc == -EINPROGRESS)
2476 /* and finally less blocks for the current uid */
2477 qi->lqi_id.qid_uid = uid;
2478 qi->lqi_space = -bspace;
2479 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2480 if (rc == -EDQUOT || rc == -EINPROGRESS)
2486 gid = i_gid_read(obj->oo_inode);
2487 qi->lqi_type = GRPQUOTA;
2488 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
2490 /* inode accounting */
2491 qi->lqi_is_blk = false;
2493 /* one more inode for the new gid ... */
2494 qi->lqi_id.qid_gid = attr->la_gid;
2496 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
2497 if (rc == -EDQUOT || rc == -EINPROGRESS)
2502 /* and one less inode for the current gid */
2503 qi->lqi_id.qid_gid = gid;
2505 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2506 if (rc == -EDQUOT || rc == -EINPROGRESS)
2511 /* block accounting */
2512 qi->lqi_is_blk = true;
2514 /* more blocks for the new gid ... */
2515 qi->lqi_id.qid_gid = attr->la_gid;
2516 qi->lqi_space = bspace;
2517 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2518 if (rc == -EDQUOT || rc == -EINPROGRESS)
2523 /* and finally less blocks for the current gid */
2524 qi->lqi_id.qid_gid = gid;
2525 qi->lqi_space = -bspace;
2526 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
2527 if (rc == -EDQUOT || rc == -EINPROGRESS)
2536 static int osd_inode_setattr(const struct lu_env *env,
2537 struct inode *inode, const struct lu_attr *attr)
2539 __u64 bits = attr->la_valid;
2541 /* Only allow set size for regular file */
2542 if (!S_ISREG(inode->i_mode))
2543 bits &= ~(LA_SIZE | LA_BLOCKS);
2548 if (bits & LA_ATIME)
2549 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
2550 if (bits & LA_CTIME)
2551 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
2552 if (bits & LA_MTIME)
2553 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
2554 if (bits & LA_SIZE) {
2555 spin_lock(&inode->i_lock);
2556 LDISKFS_I(inode)->i_disksize = attr->la_size;
2557 i_size_write(inode, attr->la_size);
2558 spin_unlock(&inode->i_lock);
2561 /* OSD should not change "i_blocks" which is used by quota.
2562 * "i_blocks" should be changed by ldiskfs only. */
2564 inode->i_mode = (inode->i_mode & S_IFMT) |
2565 (attr->la_mode & ~S_IFMT);
2567 i_uid_write(inode, attr->la_uid);
2569 i_gid_write(inode, attr->la_gid);
2570 if (bits & LA_NLINK)
2571 set_nlink(inode, attr->la_nlink);
2573 inode->i_rdev = attr->la_rdev;
2575 if (bits & LA_FLAGS) {
2576 /* always keep S_NOCMTIME */
2577 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
2583 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
2585 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
2586 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
2590 ll_vfs_dq_init(inode);
2592 if (attr->la_valid & LA_UID)
2593 iattr.ia_valid |= ATTR_UID;
2594 if (attr->la_valid & LA_GID)
2595 iattr.ia_valid |= ATTR_GID;
2596 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
2597 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
2599 rc = ll_vfs_dq_transfer(inode, &iattr);
2601 CERROR("%s: quota transfer failed: rc = %d. Is quota "
2602 "enforcement enabled on the ldiskfs "
2603 "filesystem?\n", inode->i_sb->s_id, rc);
2610 static int osd_attr_set(const struct lu_env *env,
2611 struct dt_object *dt,
2612 const struct lu_attr *attr,
2613 struct thandle *handle)
2615 struct osd_object *obj = osd_dt_obj(dt);
2616 struct inode *inode;
2619 if (!dt_object_exists(dt))
2622 LASSERT(handle != NULL);
2623 LASSERT(!dt_object_remote(dt));
2624 LASSERT(osd_invariant(obj));
2626 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
2628 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
2629 struct osd_thread_info *oti = osd_oti_get(env);
2630 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
2631 struct lu_fid *fid1 = &oti->oti_fid;
2632 struct osd_inode_id *id = &oti->oti_id;
2633 struct iam_path_descr *ipd;
2634 struct iam_container *bag;
2635 struct osd_thandle *oh;
2638 fid_cpu_to_be(fid1, fid0);
2639 memset(id, 1, sizeof(*id));
2640 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
2641 fid0)->oi_dir.od_container;
2642 ipd = osd_idx_ipd_get(env, bag);
2643 if (unlikely(ipd == NULL))
2646 oh = container_of0(handle, struct osd_thandle, ot_super);
2647 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2648 (const struct iam_rec *)id, ipd);
2649 osd_ipd_put(env, bag, ipd);
2650 return(rc > 0 ? 0 : rc);
2653 inode = obj->oo_inode;
2655 rc = osd_quota_transfer(inode, attr);
2659 spin_lock(&obj->oo_guard);
2660 rc = osd_inode_setattr(env, inode, attr);
2661 spin_unlock(&obj->oo_guard);
2665 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2667 if (!(attr->la_valid & LA_FLAGS))
2670 /* Let's check if there are extra flags need to be set into LMA */
2671 if (attr->la_flags & LUSTRE_LMA_FL_MASKS) {
2672 struct osd_thread_info *info = osd_oti_get(env);
2673 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2675 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
2679 lma->lma_incompat |=
2680 lustre_to_lma_flags(attr->la_flags);
2681 lustre_lma_swab(lma);
2682 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA,
2683 lma, sizeof(*lma), XATTR_REPLACE);
2685 struct osd_device *osd = osd_obj2dev(obj);
2687 CWARN("%s: set "DFID" lma flags %u failed: rc = %d\n",
2688 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)),
2689 lma->lma_incompat, rc);
2692 attr->la_flags & LUSTRE_LMA_FL_MASKS;
2694 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
2697 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2702 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2703 struct osd_object *obj,
2704 const char *name, const int namelen)
2706 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2709 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2710 umode_t mode, struct dt_allocation_hint *hint,
2714 struct osd_device *osd = osd_obj2dev(obj);
2715 struct osd_thandle *oth;
2716 struct dt_object *parent = NULL;
2717 struct inode *inode;
2719 LINVRNT(osd_invariant(obj));
2720 LASSERT(obj->oo_inode == NULL);
2721 LASSERT(obj->oo_hl_head == NULL);
2723 if (S_ISDIR(mode) && ldiskfs_pdo) {
2724 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2725 if (obj->oo_hl_head == NULL)
2729 oth = container_of(th, struct osd_thandle, ot_super);
2730 LASSERT(oth->ot_handle->h_transaction != NULL);
2732 if (hint != NULL && hint->dah_parent != NULL &&
2733 !dt_object_remote(hint->dah_parent))
2734 parent = hint->dah_parent;
2736 inode = ldiskfs_create_inode(oth->ot_handle,
2737 parent ? osd_dt_obj(parent)->oo_inode :
2738 osd_sb(osd)->s_root->d_inode,
2740 if (!IS_ERR(inode)) {
2741 /* Do not update file c/mtime in ldiskfs. */
2742 inode->i_flags |= S_NOCMTIME;
2744 /* For new created object, it must be consistent,
2745 * and it is unnecessary to scrub against it. */
2746 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2748 obj->oo_inode = inode;
2751 if (obj->oo_hl_head != NULL) {
2752 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2753 obj->oo_hl_head = NULL;
2755 result = PTR_ERR(inode);
2757 LINVRNT(osd_invariant(obj));
2765 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2766 struct lu_attr *attr,
2767 struct dt_allocation_hint *hint,
2768 struct dt_object_format *dof,
2772 struct osd_thandle *oth;
2773 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX | S_ISGID));
2775 LASSERT(S_ISDIR(attr->la_mode));
2777 oth = container_of(th, struct osd_thandle, ot_super);
2778 LASSERT(oth->ot_handle->h_transaction != NULL);
2779 result = osd_mkfile(info, obj, mode, hint, th);
2784 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2785 struct lu_attr *attr,
2786 struct dt_allocation_hint *hint,
2787 struct dt_object_format *dof,
2791 struct osd_thandle *oth;
2792 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2794 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2796 LASSERT(S_ISREG(attr->la_mode));
2798 oth = container_of(th, struct osd_thandle, ot_super);
2799 LASSERT(oth->ot_handle->h_transaction != NULL);
2801 result = osd_mkfile(info, obj, mode, hint, th);
2803 LASSERT(obj->oo_inode != NULL);
2804 if (feat->dif_flags & DT_IND_VARKEY)
2805 result = iam_lvar_create(obj->oo_inode,
2806 feat->dif_keysize_max,
2808 feat->dif_recsize_max,
2811 result = iam_lfix_create(obj->oo_inode,
2812 feat->dif_keysize_max,
2814 feat->dif_recsize_max,
2821 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2822 struct lu_attr *attr,
2823 struct dt_allocation_hint *hint,
2824 struct dt_object_format *dof,
2827 LASSERT(S_ISREG(attr->la_mode));
2828 return osd_mkfile(info, obj, (attr->la_mode &
2829 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2832 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2833 struct lu_attr *attr,
2834 struct dt_allocation_hint *hint,
2835 struct dt_object_format *dof,
2838 LASSERT(S_ISLNK(attr->la_mode));
2839 return osd_mkfile(info, obj, (attr->la_mode &
2840 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2843 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2844 struct lu_attr *attr,
2845 struct dt_allocation_hint *hint,
2846 struct dt_object_format *dof,
2849 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2852 LINVRNT(osd_invariant(obj));
2853 LASSERT(obj->oo_inode == NULL);
2854 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2855 S_ISFIFO(mode) || S_ISSOCK(mode));
2857 result = osd_mkfile(info, obj, mode, hint, th);
2859 LASSERT(obj->oo_inode != NULL);
2861 * This inode should be marked dirty for i_rdev. Currently
2862 * that is done in the osd_attr_init().
2864 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2867 LINVRNT(osd_invariant(obj));
2871 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2873 struct dt_allocation_hint *hint,
2874 struct dt_object_format *dof,
2877 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2879 osd_obj_type_f result;
2895 result = osd_mk_index;
2906 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2907 struct dt_object *parent, struct dt_object *child,
2912 ah->dah_parent = parent;
2913 ah->dah_mode = child_mode;
2915 if (parent != NULL && !dt_object_remote(parent)) {
2916 /* will help to find FID->ino at dt_insert("..") */
2917 struct osd_object *pobj = osd_dt_obj(parent);
2918 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
2922 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2923 struct lu_attr *attr, struct dt_object_format *dof)
2925 struct inode *inode = obj->oo_inode;
2926 __u64 valid = attr->la_valid;
2929 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2931 if (dof->dof_type != DFT_NODE)
2932 attr->la_valid &= ~LA_RDEV;
2933 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2934 attr->la_valid &= ~LA_ATIME;
2935 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2936 attr->la_valid &= ~LA_CTIME;
2937 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2938 attr->la_valid &= ~LA_MTIME;
2940 result = osd_quota_transfer(inode, attr);
2944 if (attr->la_valid != 0) {
2945 result = osd_inode_setattr(info->oti_env, inode, attr);
2947 * The osd_inode_setattr() should always succeed here. The
2948 * only error that could be returned is EDQUOT when we are
2949 * trying to change the UID or GID of the inode. However, this
2950 * should not happen since quota enforcement is no longer
2951 * enabled on ldiskfs (lquota takes care of it).
2953 LASSERTF(result == 0, "%d\n", result);
2954 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2957 attr->la_valid = valid;
2961 * Helper function for osd_object_create()
2963 * \retval 0, on success
2965 static int __osd_object_create(struct osd_thread_info *info,
2966 struct osd_object *obj, struct lu_attr *attr,
2967 struct dt_allocation_hint *hint,
2968 struct dt_object_format *dof,
2974 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2976 /* we drop umask so that permissions we pass are not affected */
2977 umask = current->fs->umask;
2978 current->fs->umask = 0;
2980 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2982 if (likely(obj->oo_inode != NULL)) {
2983 LASSERT(obj->oo_inode->i_state & I_NEW);
2985 /* Unlock the inode before attr initialization to avoid
2986 * unnecessary dqget operations. LU-6378 */
2987 unlock_new_inode(obj->oo_inode);
2990 if (likely(result == 0)) {
2991 osd_attr_init(info, obj, attr, dof);
2992 osd_object_init0(obj);
2995 /* restore previous umask value */
2996 current->fs->umask = umask;
2998 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
3004 * Helper function for osd_object_create()
3006 * \retval 0, on success
3008 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
3009 const struct lu_fid *fid, struct thandle *th)
3011 struct osd_thread_info *info = osd_oti_get(env);
3012 struct osd_inode_id *id = &info->oti_id;
3013 struct osd_device *osd = osd_obj2dev(obj);
3014 struct osd_thandle *oh;
3017 LASSERT(obj->oo_inode != NULL);
3019 oh = container_of0(th, struct osd_thandle, ot_super);
3020 LASSERT(oh->ot_handle);
3021 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3023 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
3024 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle,
3025 OI_CHECK_FLD, NULL);
3026 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3031 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
3032 u64 seq, struct lu_seq_range *range)
3034 struct seq_server_site *ss = osd_seq_site(osd);
3036 if (fid_seq_is_idif(seq)) {
3037 fld_range_set_ost(range);
3038 range->lsr_index = idif_ost_idx(seq);
3042 if (!fid_seq_in_fldb(seq)) {
3043 fld_range_set_mdt(range);
3045 /* FIXME: If ss is NULL, it suppose not get lsr_index
3047 range->lsr_index = ss->ss_node_id;
3051 LASSERT(ss != NULL);
3052 fld_range_set_any(range);
3053 /* OSD will only do local fld lookup */
3054 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
3058 * Concurrency: no external locking is necessary.
3060 static int osd_declare_object_create(const struct lu_env *env,
3061 struct dt_object *dt,
3062 struct lu_attr *attr,
3063 struct dt_allocation_hint *hint,
3064 struct dt_object_format *dof,
3065 struct thandle *handle)
3067 struct osd_thandle *oh;
3071 LASSERT(handle != NULL);
3073 oh = container_of0(handle, struct osd_thandle, ot_super);
3074 LASSERT(oh->ot_handle == NULL);
3076 /* EA object consumes more credits than regular object: osd_mk_index
3077 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
3078 * leaf_node, could involves the block, block bitmap, groups, GDT
3079 * change for each block, so add 4 * 2 credits in that case. */
3080 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3081 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
3082 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
3083 /* Reuse idle OI block may cause additional one OI block
3085 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3086 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3091 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
3092 osd_dt_obj(dt), false, NULL, false);
3096 /* will help to find FID->ino mapping at dt_insert() */
3097 rc = osd_idc_find_and_init(env, osd_obj2dev(osd_dt_obj(dt)),
3103 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
3104 struct lu_attr *attr,
3105 struct dt_allocation_hint *hint,
3106 struct dt_object_format *dof, struct thandle *th)
3108 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3109 struct osd_object *obj = osd_dt_obj(dt);
3110 struct osd_thread_info *info = osd_oti_get(env);
3114 if (dt_object_exists(dt))
3117 LINVRNT(osd_invariant(obj));
3118 LASSERT(!dt_object_remote(dt));
3119 LASSERT(osd_write_locked(env, obj));
3120 LASSERT(th != NULL);
3122 if (unlikely(fid_is_acct(fid)))
3123 /* Quota files can't be created from the kernel any more,
3124 * 'tune2fs -O quota' will take care of creating them */
3127 result = __osd_object_create(info, obj, attr, hint, dof, th);
3129 result = __osd_oi_insert(env, obj, fid, th);
3130 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3131 obj->oo_dt.do_body_ops = &osd_body_ops;
3133 LASSERT(ergo(result == 0,
3134 dt_object_exists(dt) && !dt_object_remote(dt)));
3136 LASSERT(osd_invariant(obj));
3141 * Called to destroy on-disk representation of the object
3143 * Concurrency: must be locked
3145 static int osd_declare_object_destroy(const struct lu_env *env,
3146 struct dt_object *dt,
3149 struct osd_object *obj = osd_dt_obj(dt);
3150 struct inode *inode = obj->oo_inode;
3151 struct osd_thandle *oh;
3158 oh = container_of0(th, struct osd_thandle, ot_super);
3159 LASSERT(oh->ot_handle == NULL);
3161 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
3162 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3163 /* Recycle idle OI leaf may cause additional three OI blocks
3165 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3166 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3167 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3168 /* one less inode */
3169 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3170 -1, oh, obj, false, NULL, false);
3173 /* data to be truncated */
3174 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3175 0, oh, obj, true, NULL, false);
3179 /* will help to find FID->ino when this object is being
3180 * added to PENDING/ */
3181 rc = osd_idc_find_and_init(env, osd_obj2dev(obj), obj);
3186 static int osd_object_destroy(const struct lu_env *env,
3187 struct dt_object *dt,
3190 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3191 struct osd_object *obj = osd_dt_obj(dt);
3192 struct inode *inode = obj->oo_inode;
3193 struct osd_device *osd = osd_obj2dev(obj);
3194 struct osd_thandle *oh;
3198 oh = container_of0(th, struct osd_thandle, ot_super);
3199 LASSERT(oh->ot_handle);
3201 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
3203 if (unlikely(fid_is_acct(fid)))
3206 if (S_ISDIR(inode->i_mode)) {
3207 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
3208 inode->i_nlink == 2);
3209 /* it will check/delete the inode from remote parent,
3210 * how to optimize it? unlink performance impaction XXX */
3211 result = osd_delete_from_remote_parent(env, osd, obj, oh);
3213 CERROR("%s: delete inode "DFID": rc = %d\n",
3214 osd_name(osd), PFID(fid), result);
3216 spin_lock(&obj->oo_guard);
3218 spin_unlock(&obj->oo_guard);
3219 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3222 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
3224 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
3226 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3227 result = osd_oi_delete(osd_oti_get(env), osd, fid,
3228 oh->ot_handle, OI_CHECK_FLD);
3230 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
3231 /* XXX: add to ext3 orphan list */
3232 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
3234 /* not needed in the cache anymore */
3235 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
3236 obj->oo_destroyed = 1;
3242 * Put the fid into lustre_mdt_attrs, and then place the structure
3243 * inode's ea. This fid should not be altered during the life time
3246 * \retval +ve, on success
3247 * \retval -ve, on error
3249 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
3251 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
3252 const struct lu_fid *fid, __u32 compat, __u32 incompat)
3254 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3258 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
3261 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
3264 lustre_lma_init(lma, fid, compat, incompat);
3265 lustre_lma_swab(lma);
3267 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
3269 /* LMA may already exist, but we need to check that all the
3270 * desired compat/incompat flags have been added. */
3271 if (unlikely(rc == -EEXIST)) {
3272 if (compat == 0 && incompat == 0)
3275 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
3276 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
3281 lustre_lma_swab(lma);
3282 if (!(~lma->lma_compat & compat) &&
3283 !(~lma->lma_incompat & incompat))
3286 lma->lma_compat |= compat;
3287 lma->lma_incompat |= incompat;
3288 lustre_lma_swab(lma);
3289 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3290 sizeof(*lma), XATTR_REPLACE);
3297 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
3298 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
3299 * To have compatilibility with 1.8 ldiskfs driver we need to have
3300 * magic number at start of fid data.
3301 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
3304 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
3305 const struct lu_fid *fid)
3307 if (!fid_is_namespace_visible(fid) ||
3308 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
3309 param->edp_magic = 0;
3313 param->edp_magic = LDISKFS_LUFID_MAGIC;
3314 param->edp_len = sizeof(struct lu_fid) + 1;
3315 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
3319 * Try to read the fid from inode ea into dt_rec.
3321 * \param fid object fid.
3323 * \retval 0 on success
3325 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
3326 __u32 ino, struct lu_fid *fid,
3327 struct osd_inode_id *id)
3329 struct osd_thread_info *info = osd_oti_get(env);
3330 struct inode *inode;
3333 osd_id_gen(id, ino, OSD_OII_NOGEN);
3334 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
3336 RETURN(PTR_ERR(inode));
3342 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
3344 struct inode *parent_dir,
3345 const struct lu_fid *dot_fid,
3346 const struct lu_fid *dot_dot_fid,
3347 struct osd_thandle *oth)
3349 struct ldiskfs_dentry_param *dot_ldp;
3350 struct ldiskfs_dentry_param *dot_dot_ldp;
3351 __u32 saved_nlink = dir->i_nlink;
3354 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3355 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3357 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3358 dot_ldp->edp_magic = 0;
3360 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3361 dir, dot_ldp, dot_dot_ldp);
3362 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
3363 * the subseqent ref_add() will increase the dir->i_nlink
3364 * as 3. That is incorrect for new created directory.
3366 * It looks like hack, because we want to make the OSD API
3367 * to be order-independent for new created directory object
3368 * between dt_insert(..) and ref_add() operations.
3370 * Here, we only restore the in-RAM dir-inode's nlink attr,
3371 * becuase if the nlink attr is not 2, then there will be
3372 * ref_add() called following the dt_insert(..), such call
3373 * will make both the in-RAM and on-disk dir-inode's nlink
3374 * attr to be set as 2. LU-7447 */
3375 set_nlink(dir, saved_nlink);
3380 * Create an local agent inode for remote entry
3382 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
3383 struct osd_device *osd,
3384 struct osd_object *pobj,
3385 const struct lu_fid *fid,
3389 struct osd_thread_info *info = osd_oti_get(env);
3390 struct inode *local;
3391 struct osd_thandle *oh;
3396 oh = container_of(th, struct osd_thandle, ot_super);
3397 LASSERT(oh->ot_handle->h_transaction != NULL);
3399 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
3400 if (IS_ERR(local)) {
3401 CERROR("%s: create local error %d\n", osd_name(osd),
3402 (int)PTR_ERR(local));
3406 /* restore i_gid in case S_ISGID is set, we will inherit S_ISGID and set
3407 * correct gid on remote file, not agent here */
3408 local->i_gid = current_fsgid();
3409 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
3410 unlock_new_inode(local);
3412 /* Set special LMA flag for local agent inode */
3413 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
3415 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
3416 osd_name(osd), PFID(fid), rc);
3417 RETURN(ERR_PTR(rc));
3423 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
3424 lu_object_fid(&pobj->oo_dt.do_lu),
3427 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
3428 osd_name(osd), PFID(fid), rc);
3429 RETURN(ERR_PTR(rc));
3436 * when direntry is deleted, we have to take care of possible agent inode
3437 * referenced by that. unfortunately we can't do this at that point:
3438 * iget() within a running transaction leads to deadlock and we better do
3439 * not call that every delete declaration to save performance. so we put
3440 * a potention agent inode on a list and process that once the transaction
3441 * is over. Notice it's not any worse in terms of real orphans as regular
3442 * object destroy doesn't put inodes on the on-disk orphan list. this should
3443 * be addressed separately
3445 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
3446 struct osd_thandle *oh,
3449 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
3450 struct osd_obj_orphan *oor;
3457 oor->oor_env = (struct lu_env *)env;
3458 spin_lock(&osd->od_osfs_lock);
3459 list_add(&oor->oor_list, &osd->od_orphan_list);
3460 spin_unlock(&osd->od_osfs_lock);
3462 oh->ot_remove_agents = 1;
3468 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
3469 struct osd_device *osd)
3471 struct osd_thread_info *info = osd_oti_get(env);
3472 struct osd_obj_orphan *oor, *tmp;
3473 struct osd_inode_id id;
3474 struct list_head list;
3475 struct inode *inode;
3480 INIT_LIST_HEAD(&list);
3482 spin_lock(&osd->od_osfs_lock);
3483 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
3484 if (oor->oor_env == env) {
3485 list_del(&oor->oor_list);
3486 list_add(&oor->oor_list, &list);
3489 spin_unlock(&osd->od_osfs_lock);
3491 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
3495 list_del(&oor->oor_list);
3498 osd_id_gen(&id, ino, OSD_OII_NOGEN);
3499 inode = osd_iget_fid(info, osd, &id, &fid);
3503 if (!osd_remote_fid(env, osd, &fid)) {
3508 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
3510 mark_inode_dirty(inode);
3511 ldiskfs_journal_stop(jh);
3519 * OSD layer object create function for interoperability mode (b11826).
3520 * This is mostly similar to osd_object_create(). Only difference being, fid is
3521 * inserted into inode ea here.
3523 * \retval 0, on success
3524 * \retval -ve, on error
3526 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
3527 struct lu_attr *attr,
3528 struct dt_allocation_hint *hint,
3529 struct dt_object_format *dof,
3532 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
3533 struct osd_object *obj = osd_dt_obj(dt);
3534 struct osd_thread_info *info = osd_oti_get(env);
3535 int result, on_ost = 0;
3539 if (dt_object_exists(dt))
3542 LASSERT(osd_invariant(obj));
3543 LASSERT(!dt_object_remote(dt));
3544 LASSERT(osd_write_locked(env, obj));
3545 LASSERT(th != NULL);
3547 if (unlikely(fid_is_acct(fid)))
3548 /* Quota files can't be created from the kernel any more,
3549 * 'tune2fs -O quota' will take care of creating them */
3552 result = __osd_object_create(info, obj, attr, hint, dof, th);
3554 if (fid_is_idif(fid) &&
3555 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
3556 struct lu_fid *tfid = &info->oti_fid;
3557 struct ost_id *oi = &info->oti_ostid;
3559 fid_to_ostid(fid, oi);
3560 ostid_to_fid(tfid, oi, 0);
3562 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
3563 LMAC_FID_ON_OST, 0);
3565 on_ost = fid_is_on_ost(info, osd_obj2dev(obj),
3567 result = osd_ea_fid_set(info, obj->oo_inode, fid,
3568 on_ost ? LMAC_FID_ON_OST : 0,
3571 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
3572 obj->oo_dt.do_body_ops = &osd_body_ops;
3576 result = __osd_oi_insert(env, obj, fid, th);
3578 /* a small optimization - dt_insert() isn't usually applied
3579 * to OST objects, so we don't need to cache OI mapping for
3581 if (result == 0 && on_ost == 0) {
3582 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3583 result = osd_idc_find_and_init(env, osd, obj);
3584 LASSERT(result == 0);
3587 LASSERT(ergo(result == 0,
3588 dt_object_exists(dt) && !dt_object_remote(dt)));
3589 LINVRNT(osd_invariant(obj));
3593 static int osd_declare_object_ref_add(const struct lu_env *env,
3594 struct dt_object *dt,
3595 struct thandle *handle)
3597 struct osd_thandle *oh;
3599 /* it's possible that object doesn't exist yet */
3600 LASSERT(handle != NULL);
3602 oh = container_of0(handle, struct osd_thandle, ot_super);
3603 LASSERT(oh->ot_handle == NULL);
3605 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
3606 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3612 * Concurrency: @dt is write locked.
3614 static int osd_object_ref_add(const struct lu_env *env,
3615 struct dt_object *dt, struct thandle *th)
3617 struct osd_object *obj = osd_dt_obj(dt);
3618 struct inode *inode = obj->oo_inode;
3619 struct osd_thandle *oh;
3622 if (!dt_object_exists(dt) || obj->oo_destroyed)
3625 LINVRNT(osd_invariant(obj));
3626 LASSERT(!dt_object_remote(dt));
3627 LASSERT(osd_write_locked(env, obj));
3628 LASSERT(th != NULL);
3630 oh = container_of0(th, struct osd_thandle, ot_super);
3631 LASSERT(oh->ot_handle != NULL);
3633 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
3635 CDEBUG(D_INODE, DFID" increase nlink %d\n",
3636 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3638 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
3639 * (65000) subdirectories by storing "1" in i_nlink if the link count
3640 * would otherwise overflow. Directory tranversal tools understand
3641 * that (st_nlink == 1) indicates that the filesystem dose not track
3642 * hard links count on the directory, and will not abort subdirectory
3643 * scanning early once (st_nlink - 2) subdirs have been found.
3645 * This also has to properly handle the case of inodes with nlink == 0
3646 * in case they are being linked into the PENDING directory
3648 spin_lock(&obj->oo_guard);
3649 if (unlikely(inode->i_nlink == 0))
3650 /* inc_nlink from 0 may cause WARN_ON */
3651 set_nlink(inode, 1);
3653 ldiskfs_inc_count(oh->ot_handle, inode);
3654 if (!S_ISDIR(inode->i_mode))
3655 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
3657 spin_unlock(&obj->oo_guard);
3659 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3660 LINVRNT(osd_invariant(obj));
3662 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
3667 static int osd_declare_object_ref_del(const struct lu_env *env,
3668 struct dt_object *dt,
3669 struct thandle *handle)
3671 struct osd_thandle *oh;
3673 if (!dt_object_exists(dt))
3676 LASSERT(!dt_object_remote(dt));
3677 LASSERT(handle != NULL);
3679 oh = container_of0(handle, struct osd_thandle, ot_super);
3680 LASSERT(oh->ot_handle == NULL);
3682 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
3683 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
3689 * Concurrency: @dt is write locked.
3691 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
3694 struct osd_object *obj = osd_dt_obj(dt);
3695 struct inode *inode = obj->oo_inode;
3696 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3697 struct osd_thandle *oh;
3699 if (!dt_object_exists(dt))
3702 LINVRNT(osd_invariant(obj));
3703 LASSERT(!dt_object_remote(dt));
3704 LASSERT(osd_write_locked(env, obj));
3705 LASSERT(th != NULL);
3707 oh = container_of0(th, struct osd_thandle, ot_super);
3708 LASSERT(oh->ot_handle != NULL);
3710 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3712 spin_lock(&obj->oo_guard);
3713 /* That can be result of upgrade from old Lustre version and
3714 * applied only to local files. Just skip this ref_del call.
3715 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3716 if (inode->i_nlink == 0) {
3717 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3718 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3719 ", maybe an upgraded file? (LU-3915)\n",
3720 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3721 spin_unlock(&obj->oo_guard);
3725 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3726 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3728 ldiskfs_dec_count(oh->ot_handle, inode);
3729 spin_unlock(&obj->oo_guard);
3731 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3732 LINVRNT(osd_invariant(obj));
3734 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3740 * Get the 64-bit version for an inode.
3742 static int osd_object_version_get(const struct lu_env *env,
3743 struct dt_object *dt, dt_obj_version_t *ver)
3745 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3747 CDEBUG(D_INODE, "Get version %#llx for inode %lu\n",
3748 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3749 *ver = LDISKFS_I(inode)->i_fs_version;
3754 * Concurrency: @dt is read locked.
3756 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3757 struct lu_buf *buf, const char *name)
3759 struct osd_object *obj = osd_dt_obj(dt);
3760 struct inode *inode = obj->oo_inode;
3761 struct osd_thread_info *info = osd_oti_get(env);
3762 struct dentry *dentry = &info->oti_obj_dentry;
3763 bool cache_xattr = false;
3766 /* version get is not real XATTR but uses xattr API */
3767 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3768 /* for version we are just using xattr API but change inode
3770 if (buf->lb_len == 0)
3771 return sizeof(dt_obj_version_t);
3773 if (buf->lb_len < sizeof(dt_obj_version_t))
3776 osd_object_version_get(env, dt, buf->lb_buf);
3778 return sizeof(dt_obj_version_t);
3781 if (!dt_object_exists(dt))
3784 LASSERT(!dt_object_remote(dt));
3785 LASSERT(inode->i_op != NULL);
3786 LASSERT(inode->i_op->getxattr != NULL);
3788 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
3789 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
3793 rc = osd_oxc_get(obj, name, buf);
3798 rc = __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3800 if (rc == -ENOENT || rc == -ENODATA)
3801 osd_oxc_add(obj, name, NULL, 0);
3802 else if (rc > 0 && buf->lb_buf != NULL)
3803 osd_oxc_add(obj, name, buf->lb_buf, rc);
3809 static int osd_declare_xattr_set(const struct lu_env *env,
3810 struct dt_object *dt,
3811 const struct lu_buf *buf, const char *name,
3812 int fl, struct thandle *handle)
3814 struct osd_thandle *oh;
3816 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3818 LASSERT(handle != NULL);
3820 oh = container_of0(handle, struct osd_thandle, ot_super);
3821 LASSERT(oh->ot_handle == NULL);
3823 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3824 /* For non-upgrading case, the LMA is set first and
3825 * usually fit inode. But for upgrade case, the LMA
3826 * may be in another separated EA block. */
3827 if (!dt_object_exists(dt))
3829 else if (fl == LU_XATTR_REPLACE)
3833 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3837 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3842 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3843 /* learn xattr size from osd_xattr_get if
3844 attribute has not been read yet */
3845 buflen = __osd_xattr_get(
3846 osd_dt_obj(dt)->oo_inode,
3847 &osd_oti_get(env)->oti_obj_dentry,
3852 buflen = buf->lb_len;
3855 if (buflen > sb->s_blocksize) {
3856 credits += osd_calc_bkmap_credits(
3858 (buflen + sb->s_blocksize - 1) >>
3859 sb->s_blocksize_bits);
3863 * xattr set may involve inode quota change, reserve credits for
3864 * dquot_initialize()
3866 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3869 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3875 * Set the 64-bit version for object
3877 static void osd_object_version_set(const struct lu_env *env,
3878 struct dt_object *dt,
3879 dt_obj_version_t *new_version)
3881 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3883 CDEBUG(D_INODE, "Set version %#llx (old %#llx) for inode %lu\n",
3884 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3886 LDISKFS_I(inode)->i_fs_version = *new_version;
3887 /** Version is set after all inode operations are finished,
3888 * so we should mark it dirty here */
3889 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3893 * Concurrency: @dt is write locked.
3895 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3896 const struct lu_buf *buf, const char *name, int fl,
3897 struct thandle *handle)
3899 struct osd_object *obj = osd_dt_obj(dt);
3900 struct inode *inode = obj->oo_inode;
3901 struct osd_thread_info *info = osd_oti_get(env);
3906 LASSERT(handle != NULL);
3908 /* version set is not real XATTR */
3909 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3910 /* for version we are just using xattr API but change inode
3912 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3913 osd_object_version_set(env, dt, buf->lb_buf);
3914 return sizeof(dt_obj_version_t);
3917 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3918 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3920 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3921 if (fl & LU_XATTR_REPLACE)
3922 fs_flags |= XATTR_REPLACE;
3924 if (fl & LU_XATTR_CREATE)
3925 fs_flags |= XATTR_CREATE;
3927 if (strcmp(name, XATTR_NAME_LMV) == 0) {
3928 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3930 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3934 lma->lma_incompat |= LMAI_STRIPED;
3935 lustre_lma_swab(lma);
3936 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3937 sizeof(*lma), XATTR_REPLACE);
3942 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3944 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3947 (strcmp(name, XATTR_NAME_LOV) == 0 ||
3948 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
3949 osd_oxc_add(obj, name, buf->lb_buf, buf->lb_len);
3955 * Concurrency: @dt is read locked.
3957 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3958 const struct lu_buf *buf)
3960 struct osd_object *obj = osd_dt_obj(dt);
3961 struct inode *inode = obj->oo_inode;
3962 struct osd_thread_info *info = osd_oti_get(env);
3963 struct dentry *dentry = &info->oti_obj_dentry;
3965 if (!dt_object_exists(dt))
3968 LASSERT(!dt_object_remote(dt));
3969 LASSERT(inode->i_op != NULL);
3970 LASSERT(inode->i_op->listxattr != NULL);
3972 dentry->d_inode = inode;
3973 dentry->d_sb = inode->i_sb;
3974 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3977 static int osd_declare_xattr_del(const struct lu_env *env,
3978 struct dt_object *dt, const char *name,
3979 struct thandle *handle)
3981 struct osd_thandle *oh;
3982 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3984 LASSERT(!dt_object_remote(dt));
3985 LASSERT(handle != NULL);
3987 oh = container_of0(handle, struct osd_thandle, ot_super);
3988 LASSERT(oh->ot_handle == NULL);
3990 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3991 osd_dto_credits_noquota[DTO_XATTR_SET]);
3993 * xattr del may involve inode quota change, reserve credits for
3994 * dquot_initialize()
3996 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
4002 * Concurrency: @dt is write locked.
4004 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
4005 const char *name, struct thandle *handle)
4007 struct osd_object *obj = osd_dt_obj(dt);
4008 struct inode *inode = obj->oo_inode;
4009 struct osd_thread_info *info = osd_oti_get(env);
4010 struct dentry *dentry = &info->oti_obj_dentry;
4013 if (!dt_object_exists(dt))
4016 LASSERT(!dt_object_remote(dt));
4017 LASSERT(inode->i_op != NULL);
4018 LASSERT(inode->i_op->removexattr != NULL);
4019 LASSERT(handle != NULL);
4021 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
4023 ll_vfs_dq_init(inode);
4024 dentry->d_inode = inode;
4025 dentry->d_sb = inode->i_sb;
4026 rc = inode->i_op->removexattr(dentry, name);
4027 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
4030 (strcmp(name, XATTR_NAME_LOV) == 0 ||
4031 strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0))
4032 osd_oxc_del(obj, name);
4037 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
4038 __u64 start, __u64 end)
4040 struct osd_object *obj = osd_dt_obj(dt);
4041 struct inode *inode = obj->oo_inode;
4042 struct osd_thread_info *info = osd_oti_get(env);
4043 struct dentry *dentry = &info->oti_obj_dentry;
4044 struct file *file = &info->oti_file;
4049 dentry->d_inode = inode;
4050 dentry->d_sb = inode->i_sb;
4051 file->f_path.dentry = dentry;
4052 file->f_mapping = inode->i_mapping;
4053 file->f_op = inode->i_fop;
4054 set_file_inode(file, inode);
4056 rc = ll_vfs_fsync_range(file, start, end, 0);
4061 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
4070 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
4071 const struct dt_index_features *feat)
4073 struct iam_descr *descr;
4075 if (osd_object_is_root(o))
4076 return feat == &dt_directory_features;
4078 LASSERT(o->oo_dir != NULL);
4080 descr = o->oo_dir->od_container.ic_descr;
4081 if (feat == &dt_directory_features) {
4082 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
4088 feat->dif_keysize_min <= descr->id_key_size &&
4089 descr->id_key_size <= feat->dif_keysize_max &&
4090 feat->dif_recsize_min <= descr->id_rec_size &&
4091 descr->id_rec_size <= feat->dif_recsize_max &&
4092 !(feat->dif_flags & (DT_IND_VARKEY |
4093 DT_IND_VARREC | DT_IND_NONUNQ)) &&
4094 ergo(feat->dif_flags & DT_IND_UPDATE,
4095 1 /* XXX check that object (and file system) is
4100 static int osd_iam_container_init(const struct lu_env *env,
4101 struct osd_object *obj,
4102 struct osd_directory *dir)
4104 struct iam_container *bag = &dir->od_container;
4107 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
4111 result = iam_container_setup(bag);
4113 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
4115 iam_container_fini(bag);
4122 * Concurrency: no external locking is necessary.
4124 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
4125 const struct dt_index_features *feat)
4129 struct osd_object *obj = osd_dt_obj(dt);
4131 LINVRNT(osd_invariant(obj));
4133 if (osd_object_is_root(obj)) {
4134 dt->do_index_ops = &osd_index_ea_ops;
4136 } else if (feat == &dt_directory_features) {
4137 dt->do_index_ops = &osd_index_ea_ops;
4138 if (obj->oo_inode == NULL || S_ISDIR(obj->oo_inode->i_mode))
4143 } else if (unlikely(feat == &dt_otable_features)) {
4144 dt->do_index_ops = &osd_otable_ops;
4146 } else if (unlikely(feat == &dt_acct_features)) {
4147 dt->do_index_ops = &osd_acct_index_ops;
4150 } else if (!osd_has_index(obj)) {
4151 struct osd_directory *dir;
4156 spin_lock(&obj->oo_guard);
4157 if (obj->oo_dir == NULL)
4161 * Concurrent thread allocated container data.
4164 spin_unlock(&obj->oo_guard);
4166 * Now, that we have container data, serialize its
4169 down_write(&obj->oo_ext_idx_sem);
4171 * recheck under lock.
4173 if (!osd_has_index(obj))
4174 result = osd_iam_container_init(env, obj,
4178 up_write(&obj->oo_ext_idx_sem);
4186 if (result == 0 && skip_iam == 0) {
4187 if (!osd_iam_index_probe(env, obj, feat))
4190 LINVRNT(osd_invariant(obj));
4192 if (result == 0 && feat == &dt_quota_glb_features &&
4193 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
4194 result = osd_quota_migration(env, dt);
4199 static int osd_otable_it_attr_get(const struct lu_env *env,
4200 struct dt_object *dt,
4201 struct lu_attr *attr)
4207 static const struct dt_object_operations osd_obj_ops = {
4208 .do_read_lock = osd_object_read_lock,
4209 .do_write_lock = osd_object_write_lock,
4210 .do_read_unlock = osd_object_read_unlock,
4211 .do_write_unlock = osd_object_write_unlock,
4212 .do_write_locked = osd_object_write_locked,
4213 .do_attr_get = osd_attr_get,
4214 .do_declare_attr_set = osd_declare_attr_set,
4215 .do_attr_set = osd_attr_set,
4216 .do_ah_init = osd_ah_init,
4217 .do_declare_create = osd_declare_object_create,
4218 .do_create = osd_object_create,
4219 .do_declare_destroy = osd_declare_object_destroy,
4220 .do_destroy = osd_object_destroy,
4221 .do_index_try = osd_index_try,
4222 .do_declare_ref_add = osd_declare_object_ref_add,
4223 .do_ref_add = osd_object_ref_add,
4224 .do_declare_ref_del = osd_declare_object_ref_del,
4225 .do_ref_del = osd_object_ref_del,
4226 .do_xattr_get = osd_xattr_get,
4227 .do_declare_xattr_set = osd_declare_xattr_set,
4228 .do_xattr_set = osd_xattr_set,
4229 .do_declare_xattr_del = osd_declare_xattr_del,
4230 .do_xattr_del = osd_xattr_del,
4231 .do_xattr_list = osd_xattr_list,
4232 .do_object_sync = osd_object_sync,
4233 .do_invalidate = osd_invalidate,
4237 * dt_object_operations for interoperability mode
4238 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4240 static const struct dt_object_operations osd_obj_ea_ops = {
4241 .do_read_lock = osd_object_read_lock,
4242 .do_write_lock = osd_object_write_lock,
4243 .do_read_unlock = osd_object_read_unlock,
4244 .do_write_unlock = osd_object_write_unlock,
4245 .do_write_locked = osd_object_write_locked,
4246 .do_attr_get = osd_attr_get,
4247 .do_declare_attr_set = osd_declare_attr_set,
4248 .do_attr_set = osd_attr_set,
4249 .do_ah_init = osd_ah_init,
4250 .do_declare_create = osd_declare_object_create,
4251 .do_create = osd_object_ea_create,
4252 .do_declare_destroy = osd_declare_object_destroy,
4253 .do_destroy = osd_object_destroy,
4254 .do_index_try = osd_index_try,
4255 .do_declare_ref_add = osd_declare_object_ref_add,
4256 .do_ref_add = osd_object_ref_add,
4257 .do_declare_ref_del = osd_declare_object_ref_del,
4258 .do_ref_del = osd_object_ref_del,
4259 .do_xattr_get = osd_xattr_get,
4260 .do_declare_xattr_set = osd_declare_xattr_set,
4261 .do_xattr_set = osd_xattr_set,
4262 .do_declare_xattr_del = osd_declare_xattr_del,
4263 .do_xattr_del = osd_xattr_del,
4264 .do_xattr_list = osd_xattr_list,
4265 .do_object_sync = osd_object_sync,
4266 .do_invalidate = osd_invalidate,
4269 static const struct dt_object_operations osd_obj_otable_it_ops = {
4270 .do_attr_get = osd_otable_it_attr_get,
4271 .do_index_try = osd_index_try,
4274 static int osd_index_declare_iam_delete(const struct lu_env *env,
4275 struct dt_object *dt,
4276 const struct dt_key *key,
4277 struct thandle *handle)
4279 struct osd_thandle *oh;
4281 oh = container_of0(handle, struct osd_thandle, ot_super);
4282 LASSERT(oh->ot_handle == NULL);
4284 /* Recycle may cause additional three blocks to be changed. */
4285 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
4286 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
4292 * delete a (key, value) pair from index \a dt specified by \a key
4294 * \param dt osd index object
4295 * \param key key for index
4296 * \param rec record reference
4297 * \param handle transaction handler
4300 * \retval -ve failure
4302 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
4303 const struct dt_key *key,
4304 struct thandle *handle)
4306 struct osd_thread_info *oti = osd_oti_get(env);
4307 struct osd_object *obj = osd_dt_obj(dt);
4308 struct osd_thandle *oh;
4309 struct iam_path_descr *ipd;
4310 struct iam_container *bag = &obj->oo_dir->od_container;
4314 if (!dt_object_exists(dt))
4317 LINVRNT(osd_invariant(obj));
4318 LASSERT(!dt_object_remote(dt));
4319 LASSERT(bag->ic_object == obj->oo_inode);
4320 LASSERT(handle != NULL);
4322 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4324 ipd = osd_idx_ipd_get(env, bag);
4325 if (unlikely(ipd == NULL))
4328 oh = container_of0(handle, struct osd_thandle, ot_super);
4329 LASSERT(oh->ot_handle != NULL);
4330 LASSERT(oh->ot_handle->h_transaction != NULL);
4332 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4333 /* swab quota uid/gid provided by caller */
4334 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4335 key = (const struct dt_key *)&oti->oti_quota_id;
4338 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
4339 osd_ipd_put(env, bag, ipd);
4340 LINVRNT(osd_invariant(obj));
4341 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4345 static int osd_index_declare_ea_delete(const struct lu_env *env,
4346 struct dt_object *dt,
4347 const struct dt_key *key,
4348 struct thandle *handle)
4350 struct osd_thandle *oh;
4351 struct inode *inode;
4355 LASSERT(!dt_object_remote(dt));
4356 LASSERT(handle != NULL);
4358 oh = container_of0(handle, struct osd_thandle, ot_super);
4359 LASSERT(oh->ot_handle == NULL);
4361 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE];
4362 if (key != NULL && unlikely(strcmp((char *)key, dotdot) == 0)) {
4363 /* '..' to a remote object has a local representative */
4364 credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
4365 /* to reset LMAI_REMOTE_PARENT */
4368 osd_trans_declare_op(env, oh, OSD_OT_DELETE, credits);
4370 inode = osd_dt_obj(dt)->oo_inode;
4374 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
4375 0, oh, osd_dt_obj(dt), true, NULL, false);
4379 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
4382 struct osd_fid_pack *rec;
4385 if (de->file_type & LDISKFS_DIRENT_LUFID) {
4386 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
4387 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
4388 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
4394 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
4395 const struct lu_fid *fid)
4397 struct seq_server_site *ss = osd_seq_site(osd);
4400 /* FID seqs not in FLDB, must be local seq */
4401 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
4404 /* If FLD is not being initialized yet, it only happens during the
4405 * initialization, likely during mgs initialization, and we assume
4406 * this is local FID. */
4407 if (ss == NULL || ss->ss_server_fld == NULL)
4410 /* Only check the local FLDB here */
4411 if (osd_seq_exists(env, osd, fid_seq(fid)))
4418 * Index delete function for interoperability mode (b11826).
4419 * It will remove the directory entry added by osd_index_ea_insert().
4420 * This entry is needed to maintain name->fid mapping.
4422 * \param key, key i.e. file entry to be deleted
4424 * \retval 0, on success
4425 * \retval -ve, on error
4427 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
4428 const struct dt_key *key, struct thandle *handle)
4430 struct osd_object *obj = osd_dt_obj(dt);
4431 struct inode *dir = obj->oo_inode;
4432 struct dentry *dentry;
4433 struct osd_thandle *oh;
4434 struct ldiskfs_dir_entry_2 *de = NULL;
4435 struct buffer_head *bh;
4436 struct htree_lock *hlock = NULL;
4437 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
4438 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4442 if (!dt_object_exists(dt))
4445 LINVRNT(osd_invariant(obj));
4446 LASSERT(!dt_object_remote(dt));
4447 LASSERT(handle != NULL);
4449 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
4451 oh = container_of(handle, struct osd_thandle, ot_super);
4452 LASSERT(oh->ot_handle != NULL);
4453 LASSERT(oh->ot_handle->h_transaction != NULL);
4455 ll_vfs_dq_init(dir);
4456 dentry = osd_child_dentry_get(env, obj,
4457 (char *)key, strlen((char *)key));
4459 if (obj->oo_hl_head != NULL) {
4460 hlock = osd_oti_get(env)->oti_hlock;
4461 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4462 dir, LDISKFS_HLOCK_DEL);
4464 down_write(&obj->oo_ext_idx_sem);
4467 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4469 /* If this is not the ".." entry, it might be a remote DNE
4470 * entry and we need to check if the FID is for a remote
4471 * MDT. If the FID is not in the directory entry (e.g.
4472 * upgraded 1.8 filesystem without dirdata enabled) then
4473 * we need to get the FID from the LMA. For a remote directory
4474 * there HAS to be an LMA, it cannot be an IGIF inode in this
4477 * Delete the entry before the agent inode in order to
4478 * simplify error handling. At worst an error after deleting
4479 * the entry first might leak the agent inode afterward. The
4480 * reverse would need filesystem abort in case of error deleting
4481 * the entry after the agent had been removed, or leave a
4482 * dangling entry pointing at a random inode. */
4483 if (strcmp((char *)key, dotdot) != 0) {
4484 LASSERT(de != NULL);
4485 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
4486 if (rc == -ENODATA) {
4487 /* can't get FID, postpone to the end of the
4488 * transaction when iget() is safe */
4489 osd_schedule_agent_inode_removal(env, oh,
4490 le32_to_cpu(de->inode));
4491 } else if (rc == 0 &&
4492 unlikely(osd_remote_fid(env, osd, fid))) {
4493 osd_schedule_agent_inode_removal(env, oh,
4494 le32_to_cpu(de->inode));
4497 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
4503 ldiskfs_htree_unlock(hlock);
4505 up_write(&obj->oo_ext_idx_sem);
4510 /* For inode on the remote MDT, .. will point to
4511 * /Agent directory, Check whether it needs to delete
4512 * from agent directory */
4513 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
4516 ret = osd_delete_from_remote_parent(env, osd_obj2dev(obj),
4519 /* Sigh, the entry has been deleted, and
4520 * it is not easy to revert it back, so
4521 * let's keep this error private, and let
4522 * LFSCK fix it. XXX */
4523 CERROR("%s: delete remote parent "DFID": rc = %d\n",
4524 osd_name(osd), PFID(fid), ret);
4527 LASSERT(osd_invariant(obj));
4528 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
4533 * Lookup index for \a key and copy record to \a rec.
4535 * \param dt osd index object
4536 * \param key key for index
4537 * \param rec record reference
4539 * \retval +ve success : exact mach
4540 * \retval 0 return record with key not greater than \a key
4541 * \retval -ve failure
4543 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
4544 struct dt_rec *rec, const struct dt_key *key)
4546 struct osd_object *obj = osd_dt_obj(dt);
4547 struct iam_path_descr *ipd;
4548 struct iam_container *bag = &obj->oo_dir->od_container;
4549 struct osd_thread_info *oti = osd_oti_get(env);
4550 struct iam_iterator *it = &oti->oti_idx_it;
4551 struct iam_rec *iam_rec;
4555 if (!dt_object_exists(dt))
4558 LASSERT(osd_invariant(obj));
4559 LASSERT(!dt_object_remote(dt));
4560 LASSERT(bag->ic_object == obj->oo_inode);
4562 ipd = osd_idx_ipd_get(env, bag);
4566 /* got ipd now we can start iterator. */
4567 iam_it_init(it, bag, 0, ipd);
4569 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4570 /* swab quota uid/gid provided by caller */
4571 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4572 key = (const struct dt_key *)&oti->oti_quota_id;
4575 rc = iam_it_get(it, (struct iam_key *)key);
4577 if (S_ISDIR(obj->oo_inode->i_mode))
4578 iam_rec = (struct iam_rec *)oti->oti_ldp;
4580 iam_rec = (struct iam_rec *) rec;
4582 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
4584 if (S_ISDIR(obj->oo_inode->i_mode))
4585 osd_fid_unpack((struct lu_fid *) rec,
4586 (struct osd_fid_pack *)iam_rec);
4587 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
4588 osd_quota_unpack(obj, rec);
4593 osd_ipd_put(env, bag, ipd);
4595 LINVRNT(osd_invariant(obj));
4600 static int osd_index_declare_iam_insert(const struct lu_env *env,
4601 struct dt_object *dt,
4602 const struct dt_rec *rec,
4603 const struct dt_key *key,
4604 struct thandle *handle)
4606 struct osd_thandle *oh;
4608 LASSERT(handle != NULL);
4610 oh = container_of0(handle, struct osd_thandle, ot_super);
4611 LASSERT(oh->ot_handle == NULL);
4613 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4614 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4620 * Inserts (key, value) pair in \a dt index object.
4622 * \param dt osd index object
4623 * \param key key for index
4624 * \param rec record reference
4625 * \param th transaction handler
4628 * \retval -ve failure
4630 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
4631 const struct dt_rec *rec,
4632 const struct dt_key *key, struct thandle *th,
4635 struct osd_object *obj = osd_dt_obj(dt);
4636 struct iam_path_descr *ipd;
4637 struct osd_thandle *oh;
4638 struct iam_container *bag;
4639 struct osd_thread_info *oti = osd_oti_get(env);
4640 struct iam_rec *iam_rec;
4644 if (!dt_object_exists(dt))
4647 LINVRNT(osd_invariant(obj));
4648 LASSERT(!dt_object_remote(dt));
4650 bag = &obj->oo_dir->od_container;
4651 LASSERT(bag->ic_object == obj->oo_inode);
4652 LASSERT(th != NULL);
4654 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4656 ipd = osd_idx_ipd_get(env, bag);
4657 if (unlikely(ipd == NULL))
4660 oh = container_of0(th, struct osd_thandle, ot_super);
4661 LASSERT(oh->ot_handle != NULL);
4662 LASSERT(oh->ot_handle->h_transaction != NULL);
4663 if (S_ISDIR(obj->oo_inode->i_mode)) {
4664 iam_rec = (struct iam_rec *)oti->oti_ldp;
4665 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
4666 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
4667 /* pack quota uid/gid */
4668 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4669 key = (const struct dt_key *)&oti->oti_quota_id;
4670 /* pack quota record */
4671 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
4672 iam_rec = (struct iam_rec *)rec;
4674 iam_rec = (struct iam_rec *)rec;
4677 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
4679 osd_ipd_put(env, bag, ipd);
4680 LINVRNT(osd_invariant(obj));
4681 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4686 * Calls ldiskfs_add_entry() to add directory entry
4687 * into the directory. This is required for
4688 * interoperability mode (b11826)
4690 * \retval 0, on success
4691 * \retval -ve, on error
4693 static int __osd_ea_add_rec(struct osd_thread_info *info,
4694 struct osd_object *pobj, struct inode *cinode,
4695 const char *name, const struct lu_fid *fid,
4696 struct htree_lock *hlock, struct thandle *th)
4698 struct ldiskfs_dentry_param *ldp;
4699 struct dentry *child;
4700 struct osd_thandle *oth;
4703 oth = container_of(th, struct osd_thandle, ot_super);
4704 LASSERT(oth->ot_handle != NULL);
4705 LASSERT(oth->ot_handle->h_transaction != NULL);
4706 LASSERT(pobj->oo_inode);
4708 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
4709 if (unlikely(pobj->oo_inode ==
4710 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
4713 osd_get_ldiskfs_dirent_param(ldp, fid);
4714 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
4715 child->d_fsdata = (void *)ldp;
4716 ll_vfs_dq_init(pobj->oo_inode);
4717 rc = osd_ldiskfs_add_entry(info, osd_obj2dev(pobj), oth->ot_handle,
4718 child, cinode, hlock);
4719 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
4720 struct ldiskfs_dir_entry_2 *de;
4721 struct buffer_head *bh;
4724 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
4727 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
4730 if (S_ISDIR(cinode->i_mode))
4731 de->file_type = LDISKFS_DIRENT_LUFID |
4732 LDISKFS_FT_REG_FILE;
4734 de->file_type = LDISKFS_DIRENT_LUFID |
4736 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4747 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4748 * into the directory.Also sets flags into osd object to
4749 * indicate dot and dotdot are created. This is required for
4750 * interoperability mode (b11826)
4752 * \param dir directory for dot and dotdot fixup.
4753 * \param obj child object for linking
4755 * \retval 0, on success
4756 * \retval -ve, on error
4758 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4759 struct osd_object *dir,
4760 struct inode *parent_dir, const char *name,
4761 const struct lu_fid *dot_fid,
4762 const struct lu_fid *dot_dot_fid,
4765 struct inode *inode = dir->oo_inode;
4766 struct osd_thandle *oth;
4769 oth = container_of(th, struct osd_thandle, ot_super);
4770 LASSERT(oth->ot_handle->h_transaction != NULL);
4771 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4773 if (strcmp(name, dot) == 0) {
4774 if (dir->oo_compat_dot_created) {
4777 LASSERT(inode->i_ino == parent_dir->i_ino);
4778 dir->oo_compat_dot_created = 1;
4781 } else if (strcmp(name, dotdot) == 0) {
4782 if (!dir->oo_compat_dot_created)
4784 /* in case of rename, dotdot is already created */
4785 if (dir->oo_compat_dotdot_created) {
4786 return __osd_ea_add_rec(info, dir, parent_dir, name,
4787 dot_dot_fid, NULL, th);
4790 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4791 struct lu_fid tfid = *dot_dot_fid;
4794 result = osd_add_dot_dotdot_internal(info,
4795 dir->oo_inode, parent_dir, dot_fid,
4798 result = osd_add_dot_dotdot_internal(info,
4799 dir->oo_inode, parent_dir, dot_fid,
4804 dir->oo_compat_dotdot_created = 1;
4812 * It will call the appropriate osd_add* function and return the
4813 * value, return by respective functions.
4815 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4816 struct inode *cinode, const char *name,
4817 const struct lu_fid *fid, struct thandle *th)
4819 struct osd_thread_info *info = osd_oti_get(env);
4820 struct htree_lock *hlock;
4823 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4825 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4827 if (hlock != NULL) {
4828 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4831 down_write(&pobj->oo_ext_idx_sem);
4834 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4835 lu_object_fid(&pobj->oo_dt.do_lu),
4838 if (hlock != NULL) {
4839 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4840 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4842 down_write(&pobj->oo_ext_idx_sem);
4845 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4846 struct lu_fid *tfid = &info->oti_fid;
4850 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4853 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4858 ldiskfs_htree_unlock(hlock);
4860 up_write(&pobj->oo_ext_idx_sem);
4866 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4867 struct osd_idmap_cache *oic)
4869 struct osd_scrub *scrub = &dev->od_scrub;
4870 struct lu_fid *fid = &oic->oic_fid;
4871 struct osd_inode_id *id = &oic->oic_lid;
4872 struct inode *inode = NULL;
4878 if (!fid_is_norm(fid) && !fid_is_igif(fid))
4881 if (scrub->os_pos_current > id->oii_ino)
4885 rc = osd_oi_lookup(oti, dev, fid, &oti->oti_id, 0);
4886 if (rc == -ENOENT) {
4887 __u32 gen = id->oii_gen;
4893 inode = osd_iget(oti, dev, id);
4894 /* The inode has been removed (by race maybe). */
4895 if (IS_ERR(inode)) {
4896 rc = PTR_ERR(inode);
4898 RETURN(rc == -ESTALE ? -ENOENT : rc);
4901 /* The OI mapping is lost. */
4902 if (gen != OSD_OII_NOGEN)
4906 /* The inode may has been reused by others, we do not know,
4907 * leave it to be handled by subsequent osd_fid_lookup(). */
4909 } else if (rc != 0 || osd_id_eq(id, &oti->oti_id)) {
4916 if (thread_is_running(&scrub->os_thread)) {
4917 if (inode == NULL) {
4918 inode = osd_iget(oti, dev, id);
4919 /* The inode has been removed (by race maybe). */
4920 if (IS_ERR(inode)) {
4921 rc = PTR_ERR(inode);
4923 RETURN(rc == -ESTALE ? -ENOENT : rc);
4927 rc = osd_oii_insert(dev, oic, insert);
4928 /* There is race condition between osd_oi_lookup and OI scrub.
4929 * The OI scrub finished just after osd_oi_lookup() failure.
4930 * Under such case, it is unnecessary to trigger OI scrub again,
4931 * but try to call osd_oi_lookup() again. */
4932 if (unlikely(rc == -EAGAIN))
4935 if (!S_ISDIR(inode->i_mode))
4938 rc = osd_check_lmv(oti, dev, inode, oic);
4944 if (!dev->od_noscrub && ++once == 1) {
4945 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4947 CDEBUG(D_LFSCK | D_CONSOLE | D_WARNING,
4948 "%.16s: trigger partial OI scrub for RPC inconsistency "
4949 "checking FID "DFID": rc = %d\n",
4950 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4952 if (rc == 0 || rc == -EALREADY)
4962 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4963 struct osd_device *dev,
4964 struct osd_idmap_cache *oic,
4965 struct lu_fid *fid, __u32 ino)
4967 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
4968 struct inode *inode;
4971 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4972 inode = osd_iget(oti, dev, &oic->oic_lid);
4973 if (IS_ERR(inode)) {
4974 fid_zero(&oic->oic_fid);
4975 return PTR_ERR(inode);
4978 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4981 fid_zero(&oic->oic_fid);
4983 *fid = oic->oic_fid = lma->lma_self_fid;
4987 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4988 struct osd_inode_id *id, const struct lu_fid *fid)
4990 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4991 id->oii_ino, id->oii_gen, info);
4992 info->oti_cache.oic_lid = *id;
4993 info->oti_cache.oic_fid = *fid;
4994 info->oti_cache.oic_dev = osd;
4998 * Get parent FID from the linkEA.
5000 * For a directory which parent resides on remote MDT, to satisfy the
5001 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
5002 * the other hand, to make the lookup(..) on the directory can return
5003 * the real parent FID, we append the real parent FID after its ".."
5004 * name entry in the /REMOTE_PARENT_DIR.
5006 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
5007 * backup. So after the restore, we cannot get the right parent FID from
5008 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
5009 * we have stored the real parent FID in the directory object's linkEA,
5010 * we can parse the linkEA for the real parent FID.
5012 * \param[in] env pointer to the thread context
5013 * \param[in] obj pointer to the object to be handled
5014 * \param[out]fid pointer to the buffer to hold the parent FID
5016 * \retval 0 for getting the real parent FID successfully
5017 * \retval negative error number on failure
5019 static int osd_get_pfid_from_linkea(const struct lu_env *env,
5020 struct osd_object *obj,
5023 struct osd_thread_info *oti = osd_oti_get(env);
5024 struct lu_buf *buf = &oti->oti_big_buf;
5025 struct dentry *dentry = &oti->oti_obj_dentry;
5026 struct inode *inode = obj->oo_inode;
5027 struct linkea_data ldata = { NULL };
5032 if (!S_ISDIR(inode->i_mode))
5036 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5037 buf->lb_buf, buf->lb_len);
5038 if (rc == -ERANGE) {
5039 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5042 lu_buf_realloc(buf, rc);
5043 if (buf->lb_buf == NULL)
5050 if (unlikely(rc == 0))
5056 if (unlikely(buf->lb_buf == NULL)) {
5057 lu_buf_realloc(buf, rc);
5058 if (buf->lb_buf == NULL)
5065 rc = linkea_init_with_rec(&ldata);
5067 linkea_first_entry(&ldata);
5068 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
5074 static int osd_verify_ent_by_linkea(const struct lu_env *env,
5075 struct inode *inode,
5076 const struct lu_fid *pfid,
5077 const char *name, const int namelen)
5079 struct osd_thread_info *oti = osd_oti_get(env);
5080 struct lu_buf *buf = &oti->oti_big_buf;
5081 struct dentry *dentry = &oti->oti_obj_dentry;
5082 struct linkea_data ldata = { NULL };
5083 struct lu_name cname = { .ln_name = name,
5084 .ln_namelen = namelen };
5089 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
5090 buf->lb_buf, buf->lb_len);
5092 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK, NULL, 0);
5097 if (unlikely(rc == 0))
5100 if (buf->lb_len < rc) {
5101 lu_buf_realloc(buf, rc);
5102 if (buf->lb_buf == NULL)
5109 rc = linkea_init_with_rec(&ldata);
5111 rc = linkea_links_find(&ldata, &cname, pfid);
5117 * Calls ->lookup() to find dentry. From dentry get inode and
5118 * read inode's ea to get fid. This is required for interoperability
5121 * \retval 0, on success
5122 * \retval -ve, on error
5124 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
5125 struct dt_rec *rec, const struct dt_key *key)
5127 struct inode *dir = obj->oo_inode;
5128 struct dentry *dentry;
5129 struct ldiskfs_dir_entry_2 *de;
5130 struct buffer_head *bh;
5131 struct lu_fid *fid = (struct lu_fid *) rec;
5132 struct htree_lock *hlock = NULL;
5137 LASSERT(dir->i_op != NULL);
5138 LASSERT(dir->i_op->lookup != NULL);
5140 dentry = osd_child_dentry_get(env, obj,
5141 (char *)key, strlen((char *)key));
5143 if (obj->oo_hl_head != NULL) {
5144 hlock = osd_oti_get(env)->oti_hlock;
5145 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5146 dir, LDISKFS_HLOCK_LOOKUP);
5148 down_read(&obj->oo_ext_idx_sem);
5151 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5153 struct osd_thread_info *oti = osd_oti_get(env);
5154 struct osd_inode_id *id = &oti->oti_id;
5155 struct osd_idmap_cache *oic = &oti->oti_cache;
5156 struct osd_device *dev = osd_obj2dev(obj);
5158 ino = le32_to_cpu(de->inode);
5159 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
5161 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
5165 rc = osd_get_fid_from_dentry(de, rec);
5167 /* done with de, release bh */
5170 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5171 const char *name = (const char *)key;
5173 /* If the parent is on remote MDT, and there
5174 * is no FID-in-dirent, then we have to get
5175 * the parent FID from the linkEA. */
5176 if (likely(strlen(name) == 2 &&
5177 name[0] == '.' && name[1] == '.'))
5178 rc = osd_get_pfid_from_linkea(env, obj,
5181 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5184 osd_id_gen(id, ino, OSD_OII_NOGEN);
5187 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
5188 fid_zero(&oic->oic_fid);
5193 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
5194 rc = osd_consistency_check(oti, dev, oic);
5196 fid_zero(&oic->oic_fid);
5205 ldiskfs_htree_unlock(hlock);
5207 up_read(&obj->oo_ext_idx_sem);
5212 * Put the osd object once done with it.
5214 * \param obj osd object that needs to be put
5216 static inline void osd_object_put(const struct lu_env *env,
5217 struct osd_object *obj)
5219 lu_object_put(env, &obj->oo_dt.do_lu);
5222 static int osd_index_declare_ea_insert(const struct lu_env *env,
5223 struct dt_object *dt,
5224 const struct dt_rec *rec,
5225 const struct dt_key *key,
5226 struct thandle *handle)
5228 struct osd_thandle *oh;
5229 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5230 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5231 const struct lu_fid *fid = rec1->rec_fid;
5232 int credits, rc = 0;
5233 struct osd_idmap_cache *idc;
5236 LASSERT(!dt_object_remote(dt));
5237 LASSERT(handle != NULL);
5238 LASSERT(fid != NULL);
5239 LASSERT(rec1->rec_type != 0);
5241 oh = container_of0(handle, struct osd_thandle, ot_super);
5242 LASSERT(oh->ot_handle == NULL);
5244 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
5246 /* we can't call iget() while a transactions is running
5247 * (this can lead to a deadlock), but we need to know
5248 * inum and object type. so we find this information at
5249 * declaration and cache in per-thread info */
5250 idc = osd_idc_find_or_init(env, osd, fid);
5252 RETURN(PTR_ERR(idc));
5253 if (idc->oic_remote) {
5254 /* a reference to remote inode is represented by an
5255 * agent inode which we have to create */
5256 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
5257 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
5260 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
5262 if (osd_dt_obj(dt)->oo_inode != NULL) {
5263 struct inode *inode = osd_dt_obj(dt)->oo_inode;
5265 /* We ignore block quota on meta pool (MDTs), so needn't
5266 * calculate how many blocks will be consumed by this index
5268 rc = osd_declare_inode_qid(env, i_uid_read(inode),
5269 i_gid_read(inode), 0, oh,
5270 osd_dt_obj(dt), true, NULL, false);
5277 * Index add function for interoperability mode (b11826).
5278 * It will add the directory entry.This entry is needed to
5279 * maintain name->fid mapping.
5281 * \param key it is key i.e. file entry to be inserted
5282 * \param rec it is value of given key i.e. fid
5284 * \retval 0, on success
5285 * \retval -ve, on error
5287 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
5288 const struct dt_rec *rec,
5289 const struct dt_key *key, struct thandle *th,
5292 struct osd_object *obj = osd_dt_obj(dt);
5293 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
5294 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
5295 const struct lu_fid *fid = rec1->rec_fid;
5296 const char *name = (const char *)key;
5297 struct osd_thread_info *oti = osd_oti_get(env);
5298 struct inode *child_inode = NULL;
5299 struct osd_idmap_cache *idc;
5303 if (!dt_object_exists(dt))
5306 LASSERT(osd_invariant(obj));
5307 LASSERT(!dt_object_remote(dt));
5308 LASSERT(th != NULL);
5310 osd_trans_exec_op(env, th, OSD_OT_INSERT);
5312 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
5314 idc = osd_idc_find(env, osd, fid);
5315 if (unlikely(idc == NULL)) {
5316 /* this dt_insert() wasn't declared properly, so
5317 * FID is missing in OI cache. we better do not
5318 * lookup FID in FLDB/OI and don't risk to deadlock,
5319 * but in some special cases (lfsck testing, etc)
5320 * it's much simpler than fixing a caller */
5321 CERROR("%s: "DFID" wasn't declared for insert\n",
5322 osd_name(osd), PFID(fid));
5324 idc = osd_idc_find_or_init(env, osd, fid);
5326 RETURN(PTR_ERR(idc));
5329 if (idc->oic_remote) {
5330 /* Insert remote entry */
5331 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
5332 struct osd_mdobj_map *omm = osd->od_mdt_map;
5333 struct osd_thandle *oh;
5335 /* If parent on remote MDT, we need put this object
5337 oh = container_of(th, typeof(*oh), ot_super);
5338 rc = osd_add_to_remote_parent(env, osd, obj, oh);
5340 CERROR("%s: add "DFID" error: rc = %d\n",
5342 PFID(lu_object_fid(&dt->do_lu)), rc);
5346 child_inode = igrab(omm->omm_remote_parent->d_inode);
5348 child_inode = osd_create_local_agent_inode(env, osd,
5349 obj, fid, rec1->rec_type & S_IFMT, th);
5350 if (IS_ERR(child_inode))
5351 RETURN(PTR_ERR(child_inode));
5354 /* Insert local entry */
5355 if (unlikely(idc->oic_lid.oii_ino == 0)) {
5356 /* for a reason OI cache wasn't filled properly */
5357 CERROR("%s: OIC for "DFID" isn't filled\n",
5358 osd_name(osd), PFID(fid));
5361 child_inode = oti->oti_inode;
5362 if (unlikely(child_inode == NULL)) {
5363 struct ldiskfs_inode_info *lii;
5367 child_inode = oti->oti_inode = &lii->vfs_inode;
5369 child_inode->i_sb = osd_sb(osd);
5370 child_inode->i_ino = idc->oic_lid.oii_ino;
5371 child_inode->i_mode = rec1->rec_type & S_IFMT;
5374 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
5376 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
5377 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
5379 if (child_inode && child_inode != oti->oti_inode)
5381 LASSERT(osd_invariant(obj));
5382 osd_trans_exec_check(env, th, OSD_OT_INSERT);
5387 * Initialize osd Iterator for given osd index object.
5389 * \param dt osd index object
5392 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
5393 struct dt_object *dt,
5396 struct osd_it_iam *it;
5397 struct osd_object *obj = osd_dt_obj(dt);
5398 struct lu_object *lo = &dt->do_lu;
5399 struct iam_path_descr *ipd;
5400 struct iam_container *bag = &obj->oo_dir->od_container;
5402 if (!dt_object_exists(dt))
5403 return ERR_PTR(-ENOENT);
5407 return ERR_PTR(-ENOMEM);
5409 ipd = osd_it_ipd_get(env, bag);
5410 if (likely(ipd != NULL)) {
5414 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
5415 return (struct dt_it *)it;
5418 return ERR_PTR(-ENOMEM);
5423 * free given Iterator.
5426 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
5428 struct osd_it_iam *it = (struct osd_it_iam *)di;
5429 struct osd_object *obj = it->oi_obj;
5431 iam_it_fini(&it->oi_it);
5432 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
5433 lu_object_put(env, &obj->oo_dt.do_lu);
5438 * Move Iterator to record specified by \a key
5440 * \param di osd iterator
5441 * \param key key for index
5443 * \retval +ve di points to record with least key not larger than key
5444 * \retval 0 di points to exact matched key
5445 * \retval -ve failure
5448 static int osd_it_iam_get(const struct lu_env *env,
5449 struct dt_it *di, const struct dt_key *key)
5451 struct osd_thread_info *oti = osd_oti_get(env);
5452 struct osd_it_iam *it = (struct osd_it_iam *)di;
5454 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5455 /* swab quota uid/gid */
5456 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
5457 key = (struct dt_key *)&oti->oti_quota_id;
5460 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
5466 * \param di osd iterator
5468 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
5470 struct osd_it_iam *it = (struct osd_it_iam *)di;
5472 iam_it_put(&it->oi_it);
5476 * Move iterator by one record
5478 * \param di osd iterator
5480 * \retval +1 end of container reached
5482 * \retval -ve failure
5485 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
5487 struct osd_it_iam *it = (struct osd_it_iam *)di;
5489 return iam_it_next(&it->oi_it);
5493 * Return pointer to the key under iterator.
5496 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
5497 const struct dt_it *di)
5499 struct osd_thread_info *oti = osd_oti_get(env);
5500 struct osd_it_iam *it = (struct osd_it_iam *)di;
5501 struct osd_object *obj = it->oi_obj;
5504 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
5506 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
5507 /* swab quota uid/gid */
5508 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
5509 key = (struct dt_key *)&oti->oti_quota_id;
5516 * Return size of key under iterator (in bytes)
5519 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
5521 struct osd_it_iam *it = (struct osd_it_iam *)di;
5523 return iam_it_key_size(&it->oi_it);
5527 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
5529 /* check if file type is required */
5530 if (ent->lde_attrs & LUDA_TYPE) {
5531 struct luda_type *lt;
5532 int align = sizeof(*lt) - 1;
5534 len = (len + align) & ~align;
5535 lt = (struct luda_type *)(ent->lde_name + len);
5536 lt->lt_type = cpu_to_le16(DTTOIF(type));
5539 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
5543 * build lu direct from backend fs dirent.
5547 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
5548 char *name, __u16 namelen, __u16 type, __u32 attr)
5550 ent->lde_attrs = attr | LUDA_FID;
5551 fid_cpu_to_le(&ent->lde_fid, fid);
5553 ent->lde_hash = cpu_to_le64(offset);
5554 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
5556 strncpy(ent->lde_name, name, namelen);
5557 ent->lde_name[namelen] = '\0';
5558 ent->lde_namelen = cpu_to_le16(namelen);
5560 /* append lustre attributes */
5561 osd_it_append_attrs(ent, namelen, type);
5565 * Return pointer to the record under iterator.
5567 static int osd_it_iam_rec(const struct lu_env *env,
5568 const struct dt_it *di,
5569 struct dt_rec *dtrec, __u32 attr)
5571 struct osd_it_iam *it = (struct osd_it_iam *)di;
5572 struct osd_thread_info *info = osd_oti_get(env);
5575 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
5576 const struct osd_fid_pack *rec;
5577 struct lu_fid *fid = &info->oti_fid;
5578 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5584 name = (char *)iam_it_key_get(&it->oi_it);
5586 RETURN(PTR_ERR(name));
5588 namelen = iam_it_key_size(&it->oi_it);
5590 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
5592 RETURN(PTR_ERR(rec));
5594 rc = osd_fid_unpack(fid, rec);
5598 hash = iam_it_store(&it->oi_it);
5600 /* IAM does not store object type in IAM index (dir) */
5601 osd_it_pack_dirent(lde, fid, hash, name, namelen,
5603 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
5604 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5605 (struct iam_rec *)dtrec);
5606 osd_quota_unpack(it->oi_obj, dtrec);
5608 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
5609 (struct iam_rec *)dtrec);
5616 * Returns cookie for current Iterator position.
5618 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
5620 struct osd_it_iam *it = (struct osd_it_iam *)di;
5622 return iam_it_store(&it->oi_it);
5626 * Restore iterator from cookie.
5628 * \param di osd iterator
5629 * \param hash Iterator location cookie
5631 * \retval +ve di points to record with least key not larger than key.
5632 * \retval 0 di points to exact matched key
5633 * \retval -ve failure
5636 static int osd_it_iam_load(const struct lu_env *env,
5637 const struct dt_it *di, __u64 hash)
5639 struct osd_it_iam *it = (struct osd_it_iam *)di;
5641 return iam_it_load(&it->oi_it, hash);
5644 static const struct dt_index_operations osd_index_iam_ops = {
5645 .dio_lookup = osd_index_iam_lookup,
5646 .dio_declare_insert = osd_index_declare_iam_insert,
5647 .dio_insert = osd_index_iam_insert,
5648 .dio_declare_delete = osd_index_declare_iam_delete,
5649 .dio_delete = osd_index_iam_delete,
5651 .init = osd_it_iam_init,
5652 .fini = osd_it_iam_fini,
5653 .get = osd_it_iam_get,
5654 .put = osd_it_iam_put,
5655 .next = osd_it_iam_next,
5656 .key = osd_it_iam_key,
5657 .key_size = osd_it_iam_key_size,
5658 .rec = osd_it_iam_rec,
5659 .store = osd_it_iam_store,
5660 .load = osd_it_iam_load
5666 * Creates or initializes iterator context.
5668 * \retval struct osd_it_ea, iterator structure on success
5671 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
5672 struct dt_object *dt,
5675 struct osd_object *obj = osd_dt_obj(dt);
5676 struct osd_thread_info *info = osd_oti_get(env);
5677 struct osd_it_ea *oie;
5679 struct lu_object *lo = &dt->do_lu;
5680 struct dentry *obj_dentry;
5683 if (!dt_object_exists(dt) || obj->oo_destroyed)
5684 RETURN(ERR_PTR(-ENOENT));
5686 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
5688 RETURN(ERR_PTR(-ENOMEM));
5689 obj_dentry = &oie->oie_dentry;
5691 obj_dentry->d_inode = obj->oo_inode;
5692 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
5693 obj_dentry->d_name.hash = 0;
5695 oie->oie_rd_dirent = 0;
5696 oie->oie_it_dirent = 0;
5697 oie->oie_dirent = NULL;
5698 if (unlikely(!info->oti_it_ea_buf_used)) {
5699 oie->oie_buf = info->oti_it_ea_buf;
5700 info->oti_it_ea_buf_used = 1;
5702 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5703 if (oie->oie_buf == NULL)
5704 RETURN(ERR_PTR(-ENOMEM));
5708 file = &oie->oie_file;
5710 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
5711 if (attr & LUDA_64BITHASH)
5712 file->f_mode = FMODE_64BITHASH;
5714 file->f_mode = FMODE_32BITHASH;
5715 file->f_path.dentry = obj_dentry;
5716 file->f_mapping = obj->oo_inode->i_mapping;
5717 file->f_op = obj->oo_inode->i_fop;
5718 set_file_inode(file, obj->oo_inode);
5721 RETURN((struct dt_it *) oie);
5725 * Destroy or finishes iterator context.
5727 * \param di iterator structure to be destroyed
5729 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
5731 struct osd_thread_info *info = osd_oti_get(env);
5732 struct osd_it_ea *oie = (struct osd_it_ea *)di;
5733 struct osd_object *obj = oie->oie_obj;
5734 struct inode *inode = obj->oo_inode;
5737 oie->oie_file.f_op->release(inode, &oie->oie_file);
5738 lu_object_put(env, &obj->oo_dt.do_lu);
5739 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
5740 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
5742 info->oti_it_ea_buf_used = 0;
5743 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
5748 * It position the iterator at given key, so that next lookup continues from
5749 * that key Or it is similar to dio_it->load() but based on a key,
5750 * rather than file position.
5752 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
5755 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
5757 static int osd_it_ea_get(const struct lu_env *env,
5758 struct dt_it *di, const struct dt_key *key)
5760 struct osd_it_ea *it = (struct osd_it_ea *)di;
5763 LASSERT(((const char *)key)[0] == '\0');
5764 it->oie_file.f_pos = 0;
5765 it->oie_rd_dirent = 0;
5766 it->oie_it_dirent = 0;
5767 it->oie_dirent = NULL;
5775 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5779 struct osd_filldir_cbs {
5780 #ifdef HAVE_DIR_CONTEXT
5781 struct dir_context ctx;
5783 struct osd_it_ea *it;
5786 * It is called internally by ->readdir(). It fills the
5787 * iterator's in-memory data structure with required
5788 * information i.e. name, namelen, rec_size etc.
5790 * \param buf in which information to be filled in.
5791 * \param name name of the file in given dir
5793 * \retval 0 on success
5794 * \retval 1 on buffer full
5796 #ifdef HAVE_FILLDIR_USE_CTX
5797 static int osd_ldiskfs_filldir(struct dir_context *buf,
5799 static int osd_ldiskfs_filldir(void *buf,
5801 const char *name, int namelen,
5802 loff_t offset, __u64 ino, unsigned d_type)
5804 struct osd_it_ea *it =
5805 ((struct osd_filldir_cbs *)buf)->it;
5806 struct osd_object *obj = it->oie_obj;
5807 struct osd_it_ea_dirent *ent = it->oie_dirent;
5808 struct lu_fid *fid = &ent->oied_fid;
5809 struct osd_fid_pack *rec;
5812 /* this should never happen */
5813 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5814 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5818 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5822 /* "." is just the object itself. */
5823 if (namelen == 1 && name[0] == '.') {
5824 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5825 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5826 rec = (struct osd_fid_pack*) (name + namelen + 1);
5827 if (osd_fid_unpack(fid, rec) != 0)
5832 d_type &= ~LDISKFS_DIRENT_LUFID;
5834 /* NOT export local root. */
5835 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5836 ino = obj->oo_inode->i_ino;
5837 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5840 ent->oied_ino = ino;
5841 ent->oied_off = offset;
5842 ent->oied_namelen = namelen;
5843 ent->oied_type = d_type;
5845 memcpy(ent->oied_name, name, namelen);
5847 it->oie_rd_dirent++;
5848 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5853 * Calls ->readdir() to load a directory entry at a time
5854 * and stored it in iterator's in-memory data structure.
5856 * \param di iterator's in memory structure
5858 * \retval 0 on success
5859 * \retval -ve on error
5860 * \retval +1 reach the end of entry
5862 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5863 const struct dt_it *di)
5865 struct osd_it_ea *it = (struct osd_it_ea *)di;
5866 struct osd_object *obj = it->oie_obj;
5867 struct inode *inode = obj->oo_inode;
5868 struct htree_lock *hlock = NULL;
5869 struct file *filp = &it->oie_file;
5871 struct osd_filldir_cbs buf = {
5872 #ifdef HAVE_DIR_CONTEXT
5873 .ctx.actor = osd_ldiskfs_filldir,
5879 it->oie_dirent = it->oie_buf;
5880 it->oie_rd_dirent = 0;
5882 if (obj->oo_hl_head != NULL) {
5883 hlock = osd_oti_get(env)->oti_hlock;
5884 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5885 inode, LDISKFS_HLOCK_READDIR);
5887 down_read(&obj->oo_ext_idx_sem);
5890 #ifdef HAVE_DIR_CONTEXT
5891 buf.ctx.pos = filp->f_pos;
5892 rc = inode->i_fop->iterate(filp, &buf.ctx);
5893 filp->f_pos = buf.ctx.pos;
5895 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5899 ldiskfs_htree_unlock(hlock);
5901 up_read(&obj->oo_ext_idx_sem);
5903 if (it->oie_rd_dirent == 0) {
5904 /*If it does not get any dirent, it means it has been reached
5905 *to the end of the dir */
5906 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5910 it->oie_dirent = it->oie_buf;
5911 it->oie_it_dirent = 1;
5918 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5919 * to load a directory entry at a time and stored it in
5920 * iterator's in-memory data structure.
5922 * \param di iterator's in memory structure
5924 * \retval +ve iterator reached to end
5925 * \retval 0 iterator not reached to end
5926 * \retval -ve on error
5928 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5930 struct osd_it_ea *it = (struct osd_it_ea *)di;
5935 if (it->oie_it_dirent < it->oie_rd_dirent) {
5937 (void *) it->oie_dirent +
5938 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5939 it->oie_dirent->oied_namelen);
5940 it->oie_it_dirent++;
5943 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5946 rc = osd_ldiskfs_it_fill(env, di);
5953 * Returns the key at current position from iterator's in memory structure.
5955 * \param di iterator's in memory structure
5957 * \retval key i.e. struct dt_key on success
5959 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5960 const struct dt_it *di)
5962 struct osd_it_ea *it = (struct osd_it_ea *)di;
5964 return (struct dt_key *)it->oie_dirent->oied_name;
5968 * Returns the key's size at current position from iterator's in memory structure.
5970 * \param di iterator's in memory structure
5972 * \retval key_size i.e. struct dt_key on success
5974 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5976 struct osd_it_ea *it = (struct osd_it_ea *)di;
5978 return it->oie_dirent->oied_namelen;
5981 static inline bool osd_dotdot_has_space(struct ldiskfs_dir_entry_2 *de)
5983 if (LDISKFS_DIR_REC_LEN(de) >=
5984 __LDISKFS_DIR_REC_LEN(2 + 1 + sizeof(struct osd_fid_pack)))
5991 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5992 unsigned blocksize, bool dotdot)
5995 return osd_dotdot_has_space(de);
5997 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5998 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
6005 osd_dirent_reinsert(const struct lu_env *env, struct osd_device *dev,
6006 handle_t *jh, struct dentry *dentry,
6007 const struct lu_fid *fid, struct buffer_head *bh,
6008 struct ldiskfs_dir_entry_2 *de, struct htree_lock *hlock,
6011 struct inode *dir = dentry->d_parent->d_inode;
6012 struct inode *inode = dentry->d_inode;
6013 struct osd_fid_pack *rec;
6014 struct ldiskfs_dentry_param *ldp;
6015 int namelen = dentry->d_name.len;
6017 struct osd_thread_info *info = osd_oti_get(env);
6020 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
6021 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6024 /* There is enough space to hold the FID-in-dirent. */
6025 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize, dotdot)) {
6026 rc = ldiskfs_journal_get_write_access(jh, bh);
6030 de->name[namelen] = 0;
6031 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
6032 rec->fp_len = sizeof(struct lu_fid) + 1;
6033 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
6034 de->file_type |= LDISKFS_DIRENT_LUFID;
6035 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
6042 rc = ldiskfs_delete_entry(jh, dir, de, bh);
6046 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
6047 osd_get_ldiskfs_dirent_param(ldp, fid);
6048 dentry->d_fsdata = (void *)ldp;
6049 ll_vfs_dq_init(dir);
6050 rc = osd_ldiskfs_add_entry(info, dev, jh, dentry, inode, hlock);
6051 /* It is too bad, we cannot reinsert the name entry back.
6052 * That means we lose it! */
6054 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
6055 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
6056 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
6057 dir->i_ino, dir->i_generation, namelen,
6058 dentry->d_name.name, PFID(fid), rc);
6064 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
6065 struct osd_it_ea *it, struct lu_fid *fid,
6066 struct osd_inode_id *id, __u32 *attr)
6068 struct osd_thread_info *info = osd_oti_get(env);
6069 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
6070 struct osd_device *dev = osd_obj2dev(obj);
6071 struct super_block *sb = osd_sb(dev);
6072 const char *devname = osd_name(dev);
6073 struct osd_it_ea_dirent *ent = it->oie_dirent;
6074 struct inode *dir = obj->oo_inode;
6075 struct htree_lock *hlock = NULL;
6076 struct buffer_head *bh = NULL;
6077 handle_t *jh = NULL;
6078 struct ldiskfs_dir_entry_2 *de;
6079 struct dentry *dentry;
6080 struct inode *inode;
6081 const struct lu_fid *pfid = lu_object_fid(&obj->oo_dt.do_lu);
6084 bool dotdot = false;
6088 if (ent->oied_name[0] == '.') {
6089 if (ent->oied_namelen == 1)
6092 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
6096 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
6097 inode = osd_iget(info, dev, id);
6098 if (IS_ERR(inode)) {
6099 rc = PTR_ERR(inode);
6100 if (rc == -ENOENT || rc == -ESTALE) {
6101 /* Maybe dangling name entry, or
6102 * corrupted directory entry. */
6103 *attr |= LUDA_UNKNOWN;
6106 CDEBUG(D_LFSCK, "%s: fail to iget() for dirent "
6107 "check_repair, dir = %lu/%u, name = %.*s, "
6108 "ino = %llu, rc = %d\n",
6109 devname, dir->i_ino, dir->i_generation,
6110 ent->oied_namelen, ent->oied_name,
6117 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
6119 rc = osd_get_lma(info, inode, dentry, lma);
6120 if (rc == -ENODATA || !fid_is_sane(&lma->lma_self_fid))
6125 /* We need to ensure that the name entry is still valid.
6126 * Because it may be removed or renamed by other already.
6128 * The unlink or rename operation will start journal before PDO lock,
6129 * so to avoid deadlock, here we need to start journal handle before
6130 * related PDO lock also. But because we do not know whether there
6131 * will be something to be repaired before PDO lock, we just start
6132 * journal without conditions.
6134 * We may need to remove the name entry firstly, then insert back.
6135 * One credit is for user quota file update.
6136 * One credit is for group quota file update.
6137 * Two credits are for dirty inode. */
6138 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
6139 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
6141 if (dev->od_dirent_journal != 0) {
6144 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
6147 CDEBUG(D_LFSCK, "%s: fail to start trans for dirent "
6148 "check_repair, dir = %lu/%u, credits = %d, "
6149 "name = %.*s, ino = %llu: rc = %d\n",
6150 devname, dir->i_ino, dir->i_generation, credits,
6151 ent->oied_namelen, ent->oied_name,
6154 GOTO(out_inode, rc);
6157 if (obj->oo_hl_head != NULL) {
6158 hlock = osd_oti_get(env)->oti_hlock;
6159 /* "0" means exclusive lock for the whole directory.
6160 * We need to prevent others access such name entry
6161 * during the delete + insert. Neither HLOCK_ADD nor
6162 * HLOCK_DEL cannot guarantee the atomicity. */
6163 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
6165 down_write(&obj->oo_ext_idx_sem);
6168 if (obj->oo_hl_head != NULL) {
6169 hlock = osd_oti_get(env)->oti_hlock;
6170 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
6171 LDISKFS_HLOCK_LOOKUP);
6173 down_read(&obj->oo_ext_idx_sem);
6177 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
6178 if (IS_ERR(bh) || le32_to_cpu(de->inode) != inode->i_ino) {
6179 *attr |= LUDA_IGNORE;
6184 /* For dotdot entry, if there is not enough space to hold the
6185 * FID-in-dirent, just keep them there. It only happens when the
6186 * device upgraded from 1.8 or restored from MDT file-level backup.
6187 * For the whole directory, only dotdot entry have no FID-in-dirent
6188 * and needs to get FID from LMA when readdir, it will not affect the
6189 * performance much. */
6190 if (dotdot && !osd_dotdot_has_space(de)) {
6191 *attr |= LUDA_UNKNOWN;
6197 if (lu_fid_eq(fid, &lma->lma_self_fid))
6200 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
6201 struct lu_fid *tfid = &lma->lma_self_fid;
6203 if (likely(dotdot &&
6204 fid_seq(tfid) == FID_SEQ_LOCAL_FILE &&
6205 fid_oid(tfid) == REMOTE_PARENT_DIR_OID)) {
6206 /* It must be REMOTE_PARENT_DIR and as the
6207 * 'dotdot' entry of remote directory */
6208 *attr |= LUDA_IGNORE;
6210 CDEBUG(D_LFSCK, "%s: expect remote agent "
6211 "parent directory, but got %.*s under "
6212 "dir = %lu/%u with the FID "DFID"\n",
6213 devname, ent->oied_namelen,
6214 ent->oied_name, dir->i_ino,
6215 dir->i_generation, PFID(tfid));
6217 *attr |= LUDA_UNKNOWN;
6224 if (!fid_is_zero(fid)) {
6225 rc = osd_verify_ent_by_linkea(env, inode, pfid, ent->oied_name,
6227 if (rc == -ENOENT ||
6229 !(dev->od_scrub.os_file.sf_flags & SF_UPGRADE))) {
6230 /* linkEA does not recognize the dirent entry,
6231 * it may because the dirent entry corruption
6232 * and points to other's inode. */
6233 CDEBUG(D_LFSCK, "%s: the target inode does not "
6234 "recognize the dirent, dir = %lu/%u, "
6235 " name = %.*s, ino = %llu, "
6236 DFID": rc = %d\n", devname, dir->i_ino,
6237 dir->i_generation, ent->oied_namelen,
6238 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6239 *attr |= LUDA_UNKNOWN;
6244 if (rc && rc != -ENODATA) {
6245 CDEBUG(D_LFSCK, "%s: fail to verify FID in the dirent, "
6246 "dir = %lu/%u, name = %.*s, ino = %llu, "
6247 DFID": rc = %d\n", devname, dir->i_ino,
6248 dir->i_generation, ent->oied_namelen,
6249 ent->oied_name, ent->oied_ino, PFID(fid), rc);
6250 *attr |= LUDA_UNKNOWN;
6257 /* linkEA recognizes the dirent entry, the FID-in-LMA is
6258 * valid, trusted, in spite of fid_is_sane(fid) or not. */
6259 if (*attr & LUDA_VERIFY_DRYRUN) {
6260 *fid = lma->lma_self_fid;
6261 *attr |= LUDA_REPAIR;
6268 dev->od_dirent_journal = 1;
6269 if (hlock != NULL) {
6270 ldiskfs_htree_unlock(hlock);
6273 up_read(&obj->oo_ext_idx_sem);
6279 *fid = lma->lma_self_fid;
6281 /* Update or append the FID-in-dirent. */
6282 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6283 bh, de, hlock, dotdot);
6285 *attr |= LUDA_REPAIR;
6287 CDEBUG(D_LFSCK, "%s: fail to re-insert FID after "
6288 "the dirent, dir = %lu/%u, name = %.*s, "
6289 "ino = %llu, "DFID": rc = %d\n",
6290 devname, dir->i_ino, dir->i_generation,
6291 ent->oied_namelen, ent->oied_name,
6292 ent->oied_ino, PFID(fid), rc);
6294 /* lma is NULL, trust the FID-in-dirent if it is valid. */
6295 if (*attr & LUDA_VERIFY_DRYRUN) {
6296 if (fid_is_sane(fid)) {
6297 *attr |= LUDA_REPAIR;
6298 } else if (dev->od_index == 0) {
6299 lu_igif_build(fid, inode->i_ino,
6300 inode->i_generation);
6301 *attr |= LUDA_UPGRADE;
6309 dev->od_dirent_journal = 1;
6310 if (hlock != NULL) {
6311 ldiskfs_htree_unlock(hlock);
6314 up_read(&obj->oo_ext_idx_sem);
6321 if (unlikely(fid_is_sane(fid))) {
6322 /* FID-in-dirent exists, but FID-in-LMA is lost.
6323 * Trust the FID-in-dirent, and add FID-in-LMA. */
6324 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
6326 *attr |= LUDA_REPAIR;
6328 CDEBUG(D_LFSCK, "%s: fail to set LMA for "
6329 "update dirent, dir = %lu/%u, "
6330 "name = %.*s, ino = %llu, "
6332 devname, dir->i_ino, dir->i_generation,
6333 ent->oied_namelen, ent->oied_name,
6334 ent->oied_ino, PFID(fid), rc);
6335 } else if (dev->od_index == 0) {
6336 lu_igif_build(fid, inode->i_ino, inode->i_generation);
6337 /* It is probably IGIF object. Only aappend the
6338 * FID-in-dirent. OI scrub will process FID-in-LMA. */
6339 rc = osd_dirent_reinsert(env, dev, jh, dentry, fid,
6340 bh, de, hlock, dotdot);
6342 *attr |= LUDA_UPGRADE;
6344 CDEBUG(D_LFSCK, "%s: fail to append IGIF "
6345 "after the dirent, dir = %lu/%u, "
6346 "name = %.*s, ino = %llu, "
6348 devname, dir->i_ino, dir->i_generation,
6349 ent->oied_namelen, ent->oied_name,
6350 ent->oied_ino, PFID(fid), rc);
6359 if (hlock != NULL) {
6360 ldiskfs_htree_unlock(hlock);
6362 if (dev->od_dirent_journal != 0)
6363 up_write(&obj->oo_ext_idx_sem);
6365 up_read(&obj->oo_ext_idx_sem);
6369 ldiskfs_journal_stop(jh);
6373 if (rc >= 0 && !dirty)
6374 dev->od_dirent_journal = 0;
6380 * Returns the value at current position from iterator's in memory structure.
6382 * \param di struct osd_it_ea, iterator's in memory structure
6383 * \param attr attr requested for dirent.
6384 * \param lde lustre dirent
6386 * \retval 0 no error and \param lde has correct lustre dirent.
6387 * \retval -ve on error
6389 static inline int osd_it_ea_rec(const struct lu_env *env,
6390 const struct dt_it *di,
6391 struct dt_rec *dtrec, __u32 attr)
6393 struct osd_it_ea *it = (struct osd_it_ea *)di;
6394 struct osd_object *obj = it->oie_obj;
6395 struct osd_device *dev = osd_obj2dev(obj);
6396 struct osd_thread_info *oti = osd_oti_get(env);
6397 struct osd_inode_id *id = &oti->oti_id;
6398 struct lu_fid *fid = &it->oie_dirent->oied_fid;
6399 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
6400 __u32 ino = it->oie_dirent->oied_ino;
6404 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
6406 if (attr & LUDA_VERIFY) {
6407 if (unlikely(ino == osd_remote_parent_ino(dev))) {
6408 attr |= LUDA_IGNORE;
6409 /* If the parent is on remote MDT, and there
6410 * is no FID-in-dirent, then we have to get
6411 * the parent FID from the linkEA. */
6412 if (!fid_is_sane(fid) &&
6413 it->oie_dirent->oied_namelen == 2 &&
6414 it->oie_dirent->oied_name[0] == '.' &&
6415 it->oie_dirent->oied_name[1] == '.')
6416 osd_get_pfid_from_linkea(env, obj, fid);
6418 rc = osd_dirent_check_repair(env, obj, it, fid, id,
6422 if (!fid_is_sane(fid))
6423 attr |= LUDA_UNKNOWN;
6425 attr &= ~LU_DIRENT_ATTRS_MASK;
6426 if (!fid_is_sane(fid)) {
6427 bool is_dotdot = false;
6428 if (it->oie_dirent->oied_namelen == 2 &&
6429 it->oie_dirent->oied_name[0] == '.' &&
6430 it->oie_dirent->oied_name[1] == '.')
6432 /* If the parent is on remote MDT, and there
6433 * is no FID-in-dirent, then we have to get
6434 * the parent FID from the linkEA. */
6435 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
6436 rc = osd_get_pfid_from_linkea(env, obj, fid);
6438 if (is_dotdot == false &&
6439 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
6442 rc = osd_ea_fid_get(env, obj, ino, fid, id);
6445 osd_id_gen(id, ino, OSD_OII_NOGEN);
6449 /* Pack the entry anyway, at least the offset is right. */
6450 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
6451 it->oie_dirent->oied_name,
6452 it->oie_dirent->oied_namelen,
6453 it->oie_dirent->oied_type, attr);
6458 if (osd_remote_fid(env, dev, fid))
6461 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
6462 osd_add_oi_cache(oti, dev, id, fid);
6464 RETURN(rc > 0 ? 0 : rc);
6468 * Returns the record size size at current position.
6470 * This function will return record(lu_dirent) size in bytes.
6472 * \param[in] env execution environment
6473 * \param[in] di iterator's in memory structure
6474 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
6475 * calculate the lu_dirent size.
6477 * \retval record size(in bytes & in memory) of the current lu_dirent
6480 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
6483 struct osd_it_ea *it = (struct osd_it_ea *)di;
6485 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
6489 * Returns a cookie for current position of the iterator head, so that
6490 * user can use this cookie to load/start the iterator next time.
6492 * \param di iterator's in memory structure
6494 * \retval cookie for current position, on success
6496 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
6498 struct osd_it_ea *it = (struct osd_it_ea *)di;
6500 return it->oie_dirent->oied_off;
6504 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
6505 * to load a directory entry at a time and stored it i inn,
6506 * in iterator's in-memory data structure.
6508 * \param di struct osd_it_ea, iterator's in memory structure
6510 * \retval +ve on success
6511 * \retval -ve on error
6513 static int osd_it_ea_load(const struct lu_env *env,
6514 const struct dt_it *di, __u64 hash)
6516 struct osd_it_ea *it = (struct osd_it_ea *)di;
6520 it->oie_file.f_pos = hash;
6522 rc = osd_ldiskfs_it_fill(env, di);
6533 * Index lookup function for interoperability mode (b11826).
6535 * \param key, key i.e. file name to be searched
6537 * \retval +ve, on success
6538 * \retval -ve, on error
6540 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
6541 struct dt_rec *rec, const struct dt_key *key)
6543 struct osd_object *obj = osd_dt_obj(dt);
6548 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
6549 LINVRNT(osd_invariant(obj));
6551 rc = osd_ea_lookup_rec(env, obj, rec, key);
6558 * Index and Iterator operations for interoperability
6559 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
6561 static const struct dt_index_operations osd_index_ea_ops = {
6562 .dio_lookup = osd_index_ea_lookup,
6563 .dio_declare_insert = osd_index_declare_ea_insert,
6564 .dio_insert = osd_index_ea_insert,
6565 .dio_declare_delete = osd_index_declare_ea_delete,
6566 .dio_delete = osd_index_ea_delete,
6568 .init = osd_it_ea_init,
6569 .fini = osd_it_ea_fini,
6570 .get = osd_it_ea_get,
6571 .put = osd_it_ea_put,
6572 .next = osd_it_ea_next,
6573 .key = osd_it_ea_key,
6574 .key_size = osd_it_ea_key_size,
6575 .rec = osd_it_ea_rec,
6576 .rec_size = osd_it_ea_rec_size,
6577 .store = osd_it_ea_store,
6578 .load = osd_it_ea_load
6582 static void *osd_key_init(const struct lu_context *ctx,
6583 struct lu_context_key *key)
6585 struct osd_thread_info *info;
6587 OBD_ALLOC_PTR(info);
6589 return ERR_PTR(-ENOMEM);
6591 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6592 if (info->oti_it_ea_buf == NULL)
6595 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
6597 info->oti_hlock = ldiskfs_htree_lock_alloc();
6598 if (info->oti_hlock == NULL)
6604 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6607 return ERR_PTR(-ENOMEM);
6610 static void osd_key_fini(const struct lu_context *ctx,
6611 struct lu_context_key *key, void* data)
6613 struct osd_thread_info *info = data;
6614 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
6615 struct osd_idmap_cache *idc = info->oti_ins_cache;
6617 if (info->oti_inode != NULL)
6619 if (info->oti_hlock != NULL)
6620 ldiskfs_htree_lock_free(info->oti_hlock);
6621 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
6622 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
6623 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
6624 lu_buf_free(&info->oti_big_buf);
6626 LASSERT(info->oti_ins_cache_size > 0);
6627 OBD_FREE(idc, sizeof(*idc) * info->oti_ins_cache_size);
6628 info->oti_ins_cache = NULL;
6629 info->oti_ins_cache_size = 0;
6634 static void osd_key_exit(const struct lu_context *ctx,
6635 struct lu_context_key *key, void *data)
6637 struct osd_thread_info *info = data;
6639 LASSERT(info->oti_r_locks == 0);
6640 LASSERT(info->oti_w_locks == 0);
6641 LASSERT(info->oti_txns == 0);
6644 /* type constructor/destructor: osd_type_init, osd_type_fini */
6645 LU_TYPE_INIT_FINI(osd, &osd_key);
6647 struct lu_context_key osd_key = {
6648 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
6649 .lct_init = osd_key_init,
6650 .lct_fini = osd_key_fini,
6651 .lct_exit = osd_key_exit
6655 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
6656 const char *name, struct lu_device *next)
6658 struct osd_device *osd = osd_dev(d);
6660 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
6661 >= sizeof(osd->od_svname))
6663 return osd_procfs_init(osd, name);
6666 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
6668 struct seq_server_site *ss = osd_seq_site(osd);
6672 if (osd->od_is_ost || osd->od_cl_seq != NULL)
6675 if (unlikely(ss == NULL))
6678 OBD_ALLOC_PTR(osd->od_cl_seq);
6679 if (osd->od_cl_seq == NULL)
6682 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
6683 osd->od_svname, ss->ss_server_seq);
6685 OBD_FREE_PTR(osd->od_cl_seq);
6686 osd->od_cl_seq = NULL;
6690 if (ss->ss_node_id == 0) {
6691 /* If the OSD on the sequence controller(MDT0), then allocate
6692 * sequence here, otherwise allocate sequence after connected
6693 * to MDT0 (see mdt_register_lwp_callback()). */
6694 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
6695 &osd->od_cl_seq->lcs_space, env);
6701 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
6703 if (osd->od_cl_seq == NULL)
6706 seq_client_fini(osd->od_cl_seq);
6707 OBD_FREE_PTR(osd->od_cl_seq);
6708 osd->od_cl_seq = NULL;
6711 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
6715 /* shutdown quota slave instance associated with the device */
6716 if (o->od_quota_slave != NULL) {
6717 qsd_fini(env, o->od_quota_slave);
6718 o->od_quota_slave = NULL;
6721 osd_fid_fini(env, o);
6726 static void osd_umount(const struct lu_env *env, struct osd_device *o)
6730 if (o->od_mnt != NULL) {
6731 shrink_dcache_sb(osd_sb(o));
6732 osd_sync(env, &o->od_dt_dev);
6741 static int osd_mount(const struct lu_env *env,
6742 struct osd_device *o, struct lustre_cfg *cfg)
6744 const char *name = lustre_cfg_string(cfg, 0);
6745 const char *dev = lustre_cfg_string(cfg, 1);
6747 unsigned long page, s_flags, lmd_flags = 0;
6748 struct page *__page;
6749 struct file_system_type *type;
6750 char *options = NULL;
6752 struct osd_thread_info *info = osd_oti_get(env);
6753 struct lu_fid *fid = &info->oti_fid;
6754 struct inode *inode;
6755 int rc = 0, force_over_512tb = 0;
6758 if (o->od_mnt != NULL)
6761 if (strlen(dev) >= sizeof(o->od_mntdev))
6763 strcpy(o->od_mntdev, dev);
6765 str = lustre_cfg_string(cfg, 2);
6766 s_flags = simple_strtoul(str, NULL, 0);
6767 str = strstr(str, ":");
6769 lmd_flags = simple_strtoul(str + 1, NULL, 0);
6770 opts = lustre_cfg_string(cfg, 3);
6772 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
6773 CERROR("%s: device %s extents feature is not guaranteed to "
6774 "work on big-endian systems. Use \"bigendian_extents\" "
6775 "mount option to override.\n", name, dev);
6779 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6780 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL) {
6781 CWARN("force_over_128tb option is deprecated. "
6782 "Filesystems less than 512TB can be created without any "
6783 "force options. Use force_over_512tb option for "
6784 "filesystems greater than 512TB.\n");
6787 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
6788 if (opts != NULL && strstr(opts, "force_over_256tb") != NULL) {
6789 CWARN("force_over_256tb option is deprecated. "
6790 "Filesystems less than 512TB can be created without any "
6791 "force options. Use force_over_512tb option for "
6792 "filesystems greater than 512TB.\n");
6796 if (opts != NULL && strstr(opts, "force_over_512tb") != NULL)
6797 force_over_512tb = 1;
6799 __page = alloc_page(GFP_KERNEL);
6801 GOTO(out, rc = -ENOMEM);
6802 page = (unsigned long)page_address(__page);
6803 options = (char *)page;
6806 /* strip out the options for back compatiblity */
6807 static char *sout[] = {
6814 /* strip out option we processed in osd */
6815 "bigendian_extents",
6821 strcat(options, opts);
6822 for (rc = 0, str = options; sout[rc]; ) {
6823 char *op = strstr(str, sout[rc]);
6829 if (op == options || *(op - 1) == ',') {
6830 str = op + strlen(sout[rc]);
6831 if (*str == ',' || *str == '\0') {
6832 *str == ',' ? str++ : str;
6833 memmove(op, str, strlen(str) + 1);
6836 for (str = op; *str != ',' && *str != '\0'; str++)
6840 strncat(options, "user_xattr,acl", 14);
6843 /* Glom up mount options */
6844 if (*options != '\0')
6845 strcat(options, ",");
6846 strlcat(options, "no_mbcache,nodelalloc", PAGE_SIZE);
6848 type = get_fs_type("ldiskfs");
6850 CERROR("%s: cannot find ldiskfs module\n", name);
6851 GOTO(out, rc = -ENODEV);
6854 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6855 module_put(type->owner);
6857 if (IS_ERR(o->od_mnt)) {
6858 rc = PTR_ERR(o->od_mnt);
6860 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6864 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) <<
6865 osd_sb(o)->s_blocksize_bits > 512ULL << 40 &&
6866 force_over_512tb == 0) {
6867 CERROR("%s: device %s LDISKFS does not support filesystems "
6868 "greater than 512TB and can cause data corruption. "
6869 "Use \"force_over_512tb\" mount option to override.\n",
6871 GOTO(out, rc = -EINVAL);
6874 #ifdef HAVE_DEV_SET_RDONLY
6875 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
6876 CERROR("%s: underlying device %s is marked as read-only. "
6877 "Setup failed\n", name, dev);
6878 GOTO(out_mnt, rc = -EROFS);
6882 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6883 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6884 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6885 GOTO(out_mnt, rc = -EINVAL);
6888 #ifdef LDISKFS_MOUNT_DIRDATA
6889 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6890 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6891 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6892 else if (!o->od_is_ost)
6893 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6894 "enabling the dirdata feature. If you do not want to "
6895 "downgrade to Lustre-1.x again, you can enable it via "
6896 "'tune2fs -O dirdata device'\n", name, dev);
6898 inode = osd_sb(o)->s_root->d_inode;
6899 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6900 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6902 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6906 if (lmd_flags & LMD_FLG_NOSCRUB)
6917 __free_page(__page);
6922 static struct lu_device *osd_device_fini(const struct lu_env *env,
6923 struct lu_device *d)
6925 struct osd_device *o = osd_dev(d);
6928 osd_shutdown(env, o);
6930 osd_scrub_cleanup(env, o);
6931 osd_obj_map_fini(o);
6937 static int osd_device_init0(const struct lu_env *env,
6938 struct osd_device *o,
6939 struct lustre_cfg *cfg)
6941 struct lu_device *l = osd2lu_dev(o);
6942 struct osd_thread_info *info;
6946 /* if the module was re-loaded, env can loose its keys */
6947 rc = lu_env_refill((struct lu_env *) env);
6950 info = osd_oti_get(env);
6953 l->ld_ops = &osd_lu_ops;
6954 o->od_dt_dev.dd_ops = &osd_dt_ops;
6956 spin_lock_init(&o->od_osfs_lock);
6957 mutex_init(&o->od_otable_mutex);
6958 INIT_LIST_HEAD(&o->od_orphan_list);
6960 o->od_read_cache = 1;
6961 o->od_writethrough_cache = 1;
6962 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6964 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6965 sizeof(o->od_svname));
6966 if (cplen >= sizeof(o->od_svname)) {
6971 o->od_index = -1; /* -1 means index is invalid */
6972 rc = server_name2index(o->od_svname, &o->od_index, NULL);
6973 if (rc == LDD_F_SV_TYPE_OST)
6976 o->od_full_scrub_ratio = OFSR_DEFAULT;
6977 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6978 rc = osd_mount(env, o, cfg);
6982 rc = osd_obj_map_init(env, o);
6986 rc = lu_site_init(&o->od_site, l);
6988 GOTO(out_compat, rc);
6989 o->od_site.ls_bottom_dev = l;
6991 rc = lu_site_init_finish(&o->od_site);
6995 INIT_LIST_HEAD(&o->od_ios_list);
6996 /* setup scrub, including OI files initialization */
6997 rc = osd_scrub_setup(env, o);
7001 rc = osd_procfs_init(o, o->od_svname);
7003 CERROR("%s: can't initialize procfs: rc = %d\n",
7005 GOTO(out_scrub, rc);
7008 LASSERT(l->ld_site->ls_linkage.next != NULL);
7009 LASSERT(l->ld_site->ls_linkage.prev != NULL);
7011 /* initialize quota slave instance */
7012 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
7014 if (IS_ERR(o->od_quota_slave)) {
7015 rc = PTR_ERR(o->od_quota_slave);
7016 o->od_quota_slave = NULL;
7017 GOTO(out_procfs, rc);
7025 osd_scrub_cleanup(env, o);
7027 lu_site_fini(&o->od_site);
7029 osd_obj_map_fini(o);
7036 static struct lu_device *osd_device_alloc(const struct lu_env *env,
7037 struct lu_device_type *t,
7038 struct lustre_cfg *cfg)
7040 struct osd_device *o;
7045 return ERR_PTR(-ENOMEM);
7047 rc = dt_device_init(&o->od_dt_dev, t);
7049 /* Because the ctx might be revived in dt_device_init,
7050 * refill the env here */
7051 lu_env_refill((struct lu_env *)env);
7052 rc = osd_device_init0(env, o, cfg);
7054 dt_device_fini(&o->od_dt_dev);
7057 if (unlikely(rc != 0))
7060 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
7063 static struct lu_device *osd_device_free(const struct lu_env *env,
7064 struct lu_device *d)
7066 struct osd_device *o = osd_dev(d);
7069 /* XXX: make osd top device in order to release reference */
7070 d->ld_site->ls_top_dev = d;
7071 lu_site_purge(env, d->ld_site, -1);
7072 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
7073 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
7074 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
7076 lu_site_fini(&o->od_site);
7077 dt_device_fini(&o->od_dt_dev);
7082 static int osd_process_config(const struct lu_env *env,
7083 struct lu_device *d, struct lustre_cfg *cfg)
7085 struct osd_device *o = osd_dev(d);
7089 switch (cfg->lcfg_command) {
7091 rc = osd_mount(env, o, cfg);
7094 lu_dev_del_linkage(d->ld_site, d);
7095 rc = osd_shutdown(env, o);
7098 LASSERT(&o->od_dt_dev);
7099 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
7100 cfg, &o->od_dt_dev);
7101 if (rc > 0 || rc == -ENOSYS)
7102 rc = class_process_proc_param(PARAM_OST,
7103 lprocfs_osd_obd_vars,
7104 cfg, &o->od_dt_dev);
7113 static int osd_recovery_complete(const struct lu_env *env,
7114 struct lu_device *d)
7116 struct osd_device *osd = osd_dev(d);
7120 if (osd->od_quota_slave == NULL)
7123 /* start qsd instance on recovery completion, this notifies the quota
7124 * slave code that we are about to process new requests now */
7125 rc = qsd_start(env, osd->od_quota_slave);
7130 * we use exports to track all osd users
7132 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
7133 struct obd_device *obd, struct obd_uuid *cluuid,
7134 struct obd_connect_data *data, void *localdata)
7136 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7137 struct lustre_handle conn;
7141 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
7143 rc = class_connect(&conn, obd, cluuid);
7147 *exp = class_conn2export(&conn);
7149 spin_lock(&osd->od_osfs_lock);
7151 spin_unlock(&osd->od_osfs_lock);
7157 * once last export (we don't count self-export) disappeared
7158 * osd can be released
7160 static int osd_obd_disconnect(struct obd_export *exp)
7162 struct obd_device *obd = exp->exp_obd;
7163 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7164 int rc, release = 0;
7167 /* Only disconnect the underlying layers on the final disconnect. */
7168 spin_lock(&osd->od_osfs_lock);
7170 if (osd->od_connects == 0)
7172 spin_unlock(&osd->od_osfs_lock);
7174 rc = class_disconnect(exp); /* bz 9811 */
7176 if (rc == 0 && release)
7177 class_manual_cleanup(obd);
7181 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
7182 struct lu_device *dev)
7184 struct osd_device *osd = osd_dev(dev);
7185 struct lr_server_data *lsd =
7186 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
7190 if (osd->od_quota_slave != NULL) {
7191 /* set up quota slave objects */
7192 result = qsd_prepare(env, osd->od_quota_slave);
7197 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
7198 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
7199 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
7200 osd->od_index_in_idif = 1;
7202 osd->od_index_in_idif = 0;
7203 result = osd_register_proc_index_in_idif(osd);
7208 osd->od_index_in_idif = 1;
7212 result = osd_fid_init(env, osd);
7217 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
7218 struct lu_fid *fid, struct md_op_data *op_data)
7220 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
7222 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
7225 static const struct lu_object_operations osd_lu_obj_ops = {
7226 .loo_object_init = osd_object_init,
7227 .loo_object_delete = osd_object_delete,
7228 .loo_object_release = osd_object_release,
7229 .loo_object_free = osd_object_free,
7230 .loo_object_print = osd_object_print,
7231 .loo_object_invariant = osd_object_invariant
7234 const struct lu_device_operations osd_lu_ops = {
7235 .ldo_object_alloc = osd_object_alloc,
7236 .ldo_process_config = osd_process_config,
7237 .ldo_recovery_complete = osd_recovery_complete,
7238 .ldo_prepare = osd_prepare,
7241 static const struct lu_device_type_operations osd_device_type_ops = {
7242 .ldto_init = osd_type_init,
7243 .ldto_fini = osd_type_fini,
7245 .ldto_start = osd_type_start,
7246 .ldto_stop = osd_type_stop,
7248 .ldto_device_alloc = osd_device_alloc,
7249 .ldto_device_free = osd_device_free,
7251 .ldto_device_init = osd_device_init,
7252 .ldto_device_fini = osd_device_fini
7255 static struct lu_device_type osd_device_type = {
7256 .ldt_tags = LU_DEVICE_DT,
7257 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
7258 .ldt_ops = &osd_device_type_ops,
7259 .ldt_ctx_tags = LCT_LOCAL,
7262 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
7264 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
7265 struct super_block *sb = osd_sb(osd);
7267 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
7271 * lprocfs legacy support.
7273 static struct obd_ops osd_obd_device_ops = {
7274 .o_owner = THIS_MODULE,
7275 .o_connect = osd_obd_connect,
7276 .o_disconnect = osd_obd_disconnect,
7277 .o_fid_alloc = osd_fid_alloc,
7278 .o_health_check = osd_health_check,
7281 static int __init osd_init(void)
7285 LASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
7286 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
7287 /* please, try to keep osd_thread_info smaller than a page */
7288 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
7293 rc = lu_kmem_init(ldiskfs_caches);
7297 rc = class_register_type(&osd_obd_device_ops, NULL, true,
7298 lprocfs_osd_module_vars,
7299 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
7301 lu_kmem_fini(ldiskfs_caches);
7305 static void __exit osd_exit(void)
7307 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
7308 lu_kmem_fini(ldiskfs_caches);
7311 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7312 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
7313 MODULE_VERSION(LUSTRE_VERSION_STRING);
7314 MODULE_LICENSE("GPL");
7316 module_init(osd_init);
7317 module_exit(osd_exit);