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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_OSD
46 #include <linux/module.h>
47 #include <linux/user_namespace.h>
48 #ifdef HAVE_UIDGID_HEADER
49 # include <linux/uidgid.h>
52 /* LUSTRE_VERSION_CODE */
53 #include <lustre_ver.h>
54 /* prerequisite for linux/xattr.h */
55 #include <linux/types.h>
56 /* prerequisite for linux/xattr.h */
58 /* XATTR_{REPLACE,CREATE} */
59 #include <linux/xattr.h>
61 #include <ldiskfs/ldiskfs.h>
62 #include <ldiskfs/xattr.h>
65 * struct OBD_{ALLOC,FREE}*()
68 #include <obd_support.h>
69 /* struct ptlrpc_thread */
70 #include <lustre_net.h>
71 #include <lustre_fid.h>
73 #include <lustre_param.h>
75 #include "osd_internal.h"
76 #include "osd_dynlocks.h"
78 /* llo_* api support */
79 #include <md_object.h>
80 #include <lustre_quota.h>
82 #include <lustre_linkea.h>
85 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
86 "ldiskfs with parallel directory operations");
88 int ldiskfs_track_declares_assert;
89 CFS_MODULE_PARM(ldiskfs_track_declares_assert, "i", int, 0644,
90 "LBUG during tracking of declares");
92 /* Slab to allocate dynlocks */
93 struct kmem_cache *dynlock_cachep;
95 /* Slab to allocate osd_it_ea */
96 struct kmem_cache *osd_itea_cachep;
98 static struct lu_kmem_descr ldiskfs_caches[] = {
100 .ckd_cache = &dynlock_cachep,
101 .ckd_name = "dynlock_cache",
102 .ckd_size = sizeof(struct dynlock_handle)
105 .ckd_cache = &osd_itea_cachep,
106 .ckd_name = "osd_itea_cache",
107 .ckd_size = sizeof(struct osd_it_ea)
114 static const char dot[] = ".";
115 static const char dotdot[] = "..";
116 static const char remote_obj_dir[] = "REM_OBJ_DIR";
118 static const struct lu_object_operations osd_lu_obj_ops;
119 static const struct dt_object_operations osd_obj_ops;
120 static const struct dt_object_operations osd_obj_ea_ops;
121 static const struct dt_object_operations osd_obj_otable_it_ops;
122 static const struct dt_index_operations osd_index_iam_ops;
123 static const struct dt_index_operations osd_index_ea_ops;
125 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
126 const struct lu_fid *fid);
127 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
128 struct osd_device *osd);
130 int osd_trans_declare_op2rb[] = {
131 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
132 [OSD_OT_PUNCH] = OSD_OT_MAX,
133 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
134 [OSD_OT_CREATE] = OSD_OT_DESTROY,
135 [OSD_OT_DESTROY] = OSD_OT_CREATE,
136 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
137 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
138 [OSD_OT_WRITE] = OSD_OT_WRITE,
139 [OSD_OT_INSERT] = OSD_OT_DELETE,
140 [OSD_OT_DELETE] = OSD_OT_INSERT,
141 [OSD_OT_QUOTA] = OSD_OT_MAX,
144 static int osd_has_index(const struct osd_object *obj)
146 return obj->oo_dt.do_index_ops != NULL;
149 static int osd_object_invariant(const struct lu_object *l)
151 return osd_invariant(osd_obj(l));
155 * Concurrency: doesn't matter
159 * Concurrency: doesn't matter
161 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
163 struct osd_thread_info *oti = osd_oti_get(env);
164 return oti->oti_w_locks > 0 && o->oo_owner == env;
168 * Concurrency: doesn't access mutable data
170 static int osd_root_get(const struct lu_env *env,
171 struct dt_device *dev, struct lu_fid *f)
173 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
178 * OSD object methods.
182 * Concurrency: no concurrent access is possible that early in object
185 static struct lu_object *osd_object_alloc(const struct lu_env *env,
186 const struct lu_object_header *hdr,
189 struct osd_object *mo;
195 l = &mo->oo_dt.do_lu;
196 dt_object_init(&mo->oo_dt, NULL, d);
197 mo->oo_dt.do_ops = &osd_obj_ea_ops;
198 l->lo_ops = &osd_lu_obj_ops;
199 init_rwsem(&mo->oo_sem);
200 init_rwsem(&mo->oo_ext_idx_sem);
201 spin_lock_init(&mo->oo_guard);
208 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
209 struct dentry *dentry, struct lustre_mdt_attrs *lma)
213 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
214 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
215 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
217 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
218 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
220 lustre_lma_swab(lma);
221 /* Check LMA compatibility */
222 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
223 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
224 "for fid = "DFID", ino = %lu\n",
225 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
226 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
227 PFID(&lma->lma_self_fid), inode->i_ino);
230 } else if (rc == 0) {
238 * retrieve object from backend ext fs.
240 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
241 struct osd_inode_id *id)
243 struct inode *inode = NULL;
245 /* if we look for an inode withing a running
246 * transaction, then we risk to deadlock */
247 /* osd_dirent_check_repair() breaks this */
248 /*LASSERT(current->journal_info == NULL);*/
250 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
252 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
253 id->oii_ino, PTR_ERR(inode));
254 } else if (id->oii_gen != OSD_OII_NOGEN &&
255 inode->i_generation != id->oii_gen) {
256 CDEBUG(D_INODE, "unmatched inode: ino = %u, oii_gen = %u, "
257 "i_generation = %u\n",
258 id->oii_ino, id->oii_gen, inode->i_generation);
260 inode = ERR_PTR(-ESTALE);
261 } else if (inode->i_nlink == 0) {
262 /* due to parallel readdir and unlink,
263 * we can have dead inode here. */
264 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
266 inode = ERR_PTR(-ESTALE);
267 } else if (is_bad_inode(inode)) {
268 CWARN("%.16s: bad inode: ino = %u\n",
269 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
271 inode = ERR_PTR(-ENOENT);
273 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
274 if (id->oii_gen == OSD_OII_NOGEN)
275 osd_id_gen(id, inode->i_ino, inode->i_generation);
277 /* Do not update file c/mtime in ldiskfs.
278 * NB: we don't have any lock to protect this because we don't
279 * have reference on osd_object now, but contention with
280 * another lookup + attr_set can't happen in the tiny window
281 * between if (...) and set S_NOCMTIME. */
282 if (!(inode->i_flags & S_NOCMTIME))
283 inode->i_flags |= S_NOCMTIME;
288 static struct inode *
289 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
290 struct osd_inode_id *id, struct lu_fid *fid)
292 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
296 inode = osd_iget(info, dev, id);
300 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
302 *fid = lma->lma_self_fid;
303 } else if (rc == -ENODATA) {
304 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
305 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
307 lu_igif_build(fid, inode->i_ino, inode->i_generation);
315 static struct inode *osd_iget_check(struct osd_thread_info *info,
316 struct osd_device *dev,
317 const struct lu_fid *fid,
318 struct osd_inode_id *id,
325 /* The cached OI mapping is trustable. If we cannot locate the inode
326 * via the cached OI mapping, then return the failure to the caller
327 * directly without further OI checking. */
329 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
332 if (cached || (rc != -ENOENT && rc != -ESTALE)) {
333 CDEBUG(D_INODE, "no inode: ino = %u, rc = %d\n",
342 if (is_bad_inode(inode)) {
345 CDEBUG(D_INODE, "bad inode: ino = %u\n", id->oii_ino);
353 if (id->oii_gen != OSD_OII_NOGEN &&
354 inode->i_generation != id->oii_gen) {
357 CDEBUG(D_INODE, "unmatched inode: ino = %u, "
358 "oii_gen = %u, i_generation = %u\n",
359 id->oii_ino, id->oii_gen, inode->i_generation);
367 if (inode->i_nlink == 0) {
370 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
378 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
382 LASSERTF(rc == -ESTALE || rc == -ENOENT, "rc = %d\n", rc);
384 rc = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
385 /* XXX: There are four possible cases:
387 * Backup/restore caused the OI invalid.
389 * Someone unlinked the object but NOT removed
390 * the OI mapping, such as mount target device
391 * as ldiskfs, and modify something directly.
393 * Someone just removed the object between the
394 * former oi_lookup and the iget. It is normal.
395 * 4. Other failure cases.
397 * Generally, when the device is mounted, it will
398 * auto check whether the system is restored from
399 * file-level backup or not. We trust such detect
400 * to distinguish the 1st case from the 2nd case. */
402 if (!IS_ERR(inode) && inode->i_generation != 0 &&
403 inode->i_generation == id->oii_gen)
404 /* "id->oii_gen != OSD_OII_NOGEN" is for
405 * "@cached == false" case. */
410 /* If the OI mapping was in OI file before the
411 * osd_iget_check(), but now, it is disappear,
412 * then it must be removed by race. That is a
413 * normal race case. */
416 if (id->oii_gen == OSD_OII_NOGEN)
417 osd_id_gen(id, inode->i_ino, inode->i_generation);
419 /* Do not update file c/mtime in ldiskfs.
420 * NB: we don't have any lock to protect this because we don't
421 * have reference on osd_object now, but contention with
422 * another lookup + attr_set can't happen in the tiny window
423 * between if (...) and set S_NOCMTIME. */
424 if (!(inode->i_flags & S_NOCMTIME))
425 inode->i_flags |= S_NOCMTIME;
442 * \retval +v: new filter_fid, does not contain self-fid
443 * \retval 0: filter_fid_old, contains self-fid
444 * \retval -v: other failure cases
446 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
447 struct dentry *dentry, struct lu_fid *fid)
449 struct filter_fid_old *ff = &info->oti_ff;
450 struct ost_id *ostid = &info->oti_ostid;
453 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
454 if (rc == sizeof(*ff)) {
456 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
457 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
458 /* XXX: use 0 as the index for compatibility, the caller will
459 * handle index related issues when necessarry. */
460 ostid_to_fid(fid, ostid, 0);
461 } else if (rc == sizeof(struct filter_fid)) {
463 } else if (rc >= 0) {
470 static int osd_lma_self_repair(struct osd_thread_info *info,
471 struct osd_device *osd, struct inode *inode,
472 const struct lu_fid *fid, __u32 compat)
477 LASSERT(current->journal_info == NULL);
479 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
480 osd_dto_credits_noquota[DTO_XATTR_SET]);
483 CWARN("%s: cannot start journal for lma_self_repair: rc = %d\n",
488 rc = osd_ea_fid_set(info, inode, fid, compat, 0);
490 CWARN("%s: cannot self repair the LMA: rc = %d\n",
492 ldiskfs_journal_stop(jh);
496 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
498 struct osd_thread_info *info = osd_oti_get(env);
499 struct osd_device *osd = osd_obj2dev(obj);
500 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
501 struct inode *inode = obj->oo_inode;
502 struct dentry *dentry = &info->oti_obj_dentry;
503 struct lu_fid *fid = NULL;
504 const struct lu_fid *rfid = lu_object_fid(&obj->oo_dt.do_lu);
508 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
509 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
510 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
511 if (rc == -ENODATA && !fid_is_igif(rfid) && osd->od_check_ff) {
512 fid = &lma->lma_self_fid;
513 rc = osd_get_idif(info, inode, dentry, fid);
514 if ((rc > 0) || (rc == -ENODATA && osd->od_index_in_idif)) {
515 /* For the given OST-object, if it has neither LMA nor
516 * FID in XATTR_NAME_FID, then the given FID (which is
517 * contained in the @obj, from client RPC for locating
518 * the OST-object) is trusted. We use it to generate
520 osd_lma_self_repair(info, osd, inode, rfid,
531 lustre_lma_swab(lma);
532 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
533 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
534 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
535 "fid = "DFID", ino = %lu\n", osd_name(osd),
536 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
537 PFID(rfid), inode->i_ino);
540 fid = &lma->lma_self_fid;
544 if (fid != NULL && unlikely(!lu_fid_eq(rfid, fid))) {
545 if (fid_is_idif(rfid) && fid_is_idif(fid)) {
546 struct ost_id *oi = &info->oti_ostid;
547 struct lu_fid *fid1 = &info->oti_fid3;
548 __u32 idx = fid_idif_ost_idx(rfid);
550 /* For old IDIF, the OST index is not part of the IDIF,
551 * Means that different OSTs may have the same IDIFs.
552 * Under such case, we need to make some compatible
553 * check to make sure to trigger OI scrub properly. */
554 if (idx != 0 && fid_idif_ost_idx(fid) == 0) {
555 /* Given @rfid is new, LMA is old. */
556 fid_to_ostid(fid, oi);
557 ostid_to_fid(fid1, oi, idx);
558 if (lu_fid_eq(fid1, rfid)) {
559 if (osd->od_index_in_idif)
560 osd_lma_self_repair(info, osd,
574 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
575 const struct lu_fid *fid,
576 const struct lu_object_conf *conf)
578 struct osd_thread_info *info;
579 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
580 struct osd_device *dev;
581 struct osd_idmap_cache *oic;
582 struct osd_inode_id *id;
584 struct osd_scrub *scrub;
585 struct scrub_file *sf;
589 bool triggered = false;
592 LINVRNT(osd_invariant(obj));
593 LASSERT(obj->oo_inode == NULL);
594 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID"\n", PFID(fid));
597 scrub = &dev->od_scrub;
598 sf = &scrub->os_file;
599 info = osd_oti_get(env);
601 oic = &info->oti_cache;
603 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
606 /* For the object is created as locking anchor, or for the object to
607 * be created on disk. No need to osd_oi_lookup() at here because FID
608 * shouldn't never be re-used, if it's really a duplicate FID from
609 * unexpected reason, we should be able to detect it later by calling
610 * do_create->osd_oi_insert(). */
611 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
612 GOTO(out, result = 0);
614 /* Search order: 1. per-thread cache. */
615 if (lu_fid_eq(fid, &oic->oic_fid) &&
616 likely(oic->oic_dev == dev)) {
622 if (!list_empty(&scrub->os_inconsistent_items)) {
623 /* Search order: 2. OI scrub pending list. */
624 result = osd_oii_lookup(dev, fid, id);
630 /* Search order: 3. OI files. */
631 result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
632 if (result == -ENOENT) {
633 if (!(fid_is_norm(fid) || fid_is_igif(fid)) ||
634 fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
635 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
637 GOTO(out, result = 0);
646 inode = osd_iget_check(info, dev, fid, id, cached);
648 result = PTR_ERR(inode);
649 if (result == -ENOENT || result == -ESTALE)
650 GOTO(out, result = -ENOENT);
652 if (result == -EREMCHG) {
655 if (unlikely(triggered))
656 GOTO(out, result = saved);
659 if (thread_is_running(&scrub->os_thread)) {
660 result = -EINPROGRESS;
661 } else if (!dev->od_noscrub) {
662 result = osd_scrub_start(dev, SS_AUTO_FULL |
663 SS_CLEAR_DRYRUN | SS_CLEAR_FAILOUT);
664 LCONSOLE_WARN("%.16s: trigger OI scrub by RPC "
665 "for "DFID", rc = %d [1]\n",
666 osd_name(dev), PFID(fid), result);
667 if (result == 0 || result == -EALREADY)
668 result = -EINPROGRESS;
673 if (fid_is_on_ost(info, dev, fid, OI_CHECK_FLD))
676 /* We still have chance to get the valid inode: for the
677 * object which is referenced by remote name entry, the
678 * object on the local MDT will be linked under the dir
679 * of "/REMOTE_PARENT_DIR" with its FID string as name.
681 * We do not know whether the object for the given FID
682 * is referenced by some remote name entry or not, and
683 * especially for DNE II, a multiple-linked object may
684 * have many name entries reside on many MDTs.
686 * To simplify the operation, OSD will not distinguish
687 * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
688 * only happened for the RPC from other MDT during the
689 * OI scrub, or for the client side RPC with FID only,
690 * such as FID to path, or from old connected client. */
692 result = osd_lookup_in_remote_parent(info, dev,
705 obj->oo_inode = inode;
706 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
708 result = osd_check_lma(env, obj);
710 if (result == -ENODATA) {
712 result = osd_oi_lookup(info, dev, fid, id,
715 /* result == -ENOENT means that the OI
716 * mapping has been removed by race,
717 * the target inode belongs to other
720 * Others error also can be returned
723 obj->oo_inode = NULL;
726 /* result == 0 means the cached OI
727 * mapping is still in the OI file,
728 * the target the inode is valid. */
731 /* The current OI mapping is from the OI file,
732 * since the inode has been found via
733 * osd_iget_check(), no need recheck OI. */
740 obj->oo_inode = NULL;
741 if (result != -EREMCHG)
745 result = osd_oi_lookup(info, dev, fid, id,
747 /* result == -ENOENT means the cached OI mapping
748 * has been removed from the OI file by race,
749 * above target inode belongs to other object.
751 * Others error also can be returned directly. */
755 /* result == 0, goto trigger */
757 /* The current OI mapping is from the OI file,
758 * since the inode has been found via
759 * osd_iget_check(), no need recheck OI. */
766 obj->oo_compat_dot_created = 1;
767 obj->oo_compat_dotdot_created = 1;
769 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
770 GOTO(out, result = 0);
772 LASSERT(obj->oo_hl_head == NULL);
773 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
774 if (obj->oo_hl_head == NULL) {
775 obj->oo_inode = NULL;
777 GOTO(out, result = -ENOMEM);
779 GOTO(out, result = 0);
782 if (result != 0 && cached)
783 fid_zero(&oic->oic_fid);
785 LINVRNT(osd_invariant(obj));
790 * Concurrency: shouldn't matter.
792 static void osd_object_init0(struct osd_object *obj)
794 LASSERT(obj->oo_inode != NULL);
795 obj->oo_dt.do_body_ops = &osd_body_ops;
796 obj->oo_dt.do_lu.lo_header->loh_attr |=
797 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
801 * Concurrency: no concurrent access is possible that early in object
804 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
805 const struct lu_object_conf *conf)
807 struct osd_object *obj = osd_obj(l);
810 LINVRNT(osd_invariant(obj));
812 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
813 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
814 l->lo_header->loh_attr |= LOHA_EXISTS;
818 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
819 obj->oo_dt.do_body_ops = &osd_body_ops_new;
820 if (result == 0 && obj->oo_inode != NULL)
821 osd_object_init0(obj);
823 LINVRNT(osd_invariant(obj));
828 * Concurrency: no concurrent access is possible that late in object
831 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
833 struct osd_object *obj = osd_obj(l);
835 LINVRNT(osd_invariant(obj));
837 dt_object_fini(&obj->oo_dt);
838 if (obj->oo_hl_head != NULL)
839 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
844 * Concurrency: no concurrent access is possible that late in object
847 static void osd_index_fini(struct osd_object *o)
849 struct iam_container *bag;
851 if (o->oo_dir != NULL) {
852 bag = &o->oo_dir->od_container;
853 if (o->oo_inode != NULL) {
854 if (bag->ic_object == o->oo_inode)
855 iam_container_fini(bag);
857 OBD_FREE_PTR(o->oo_dir);
863 * Concurrency: no concurrent access is possible that late in object
864 * life-cycle (for all existing callers, that is. New callers have to provide
865 * their own locking.)
867 static int osd_inode_unlinked(const struct inode *inode)
869 return inode->i_nlink == 0;
873 OSD_TXN_OI_DELETE_CREDITS = 20,
874 OSD_TXN_INODE_DELETE_CREDITS = 20
881 #if OSD_THANDLE_STATS
883 * Set time when the handle is allocated
885 static void osd_th_alloced(struct osd_thandle *oth)
887 oth->oth_alloced = cfs_time_current();
891 * Set time when the handle started
893 static void osd_th_started(struct osd_thandle *oth)
895 oth->oth_started = cfs_time_current();
899 * Helper function to convert time interval to microseconds packed in
902 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
906 cfs_duration_usec(cfs_time_sub(end, start), &val);
907 return val.tv_sec * 1000000 + val.tv_usec;
911 * Check whether the we deal with this handle for too long.
913 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
914 cfs_time_t alloced, cfs_time_t started,
917 cfs_time_t now = cfs_time_current();
919 LASSERT(dev != NULL);
921 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
922 interval_to_usec(alloced, started));
923 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
924 interval_to_usec(started, closed));
925 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
926 interval_to_usec(closed, now));
928 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
929 CWARN("transaction handle %p was open for too long: "
931 "alloced "CFS_TIME_T" ,"
932 "started "CFS_TIME_T" ,"
933 "closed "CFS_TIME_T"\n",
934 oth, now, alloced, started, closed);
935 libcfs_debug_dumpstack(NULL);
939 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
941 cfs_time_t __closed = cfs_time_current(); \
942 cfs_time_t __alloced = oth->oth_alloced; \
943 cfs_time_t __started = oth->oth_started; \
946 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
949 #else /* OSD_THANDLE_STATS */
951 #define osd_th_alloced(h) do {} while(0)
952 #define osd_th_started(h) do {} while(0)
953 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
955 #endif /* OSD_THANDLE_STATS */
958 * Concurrency: doesn't access mutable data.
960 static int osd_param_is_not_sane(const struct osd_device *dev,
961 const struct thandle *th)
963 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
965 return oh->ot_credits > osd_transaction_size(dev);
969 * Concurrency: shouldn't matter.
971 static void osd_trans_commit_cb(struct super_block *sb,
972 struct ldiskfs_journal_cb_entry *jcb, int error)
974 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
975 struct thandle *th = &oh->ot_super;
976 struct lu_device *lud = &th->th_dev->dd_lu_dev;
977 struct dt_txn_commit_cb *dcb, *tmp;
979 LASSERT(oh->ot_handle == NULL);
982 CERROR("transaction @0x%p commit error: %d\n", th, error);
984 dt_txn_hook_commit(th);
986 /* call per-transaction callbacks if any */
987 list_for_each_entry_safe(dcb, tmp, &oh->ot_commit_dcb_list,
989 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
990 "commit callback entry: magic=%x name='%s'\n",
991 dcb->dcb_magic, dcb->dcb_name);
992 list_del_init(&dcb->dcb_linkage);
993 dcb->dcb_func(NULL, th, dcb, error);
996 lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
1000 lu_context_exit(&th->th_ctx);
1001 lu_context_fini(&th->th_ctx);
1005 static struct thandle *osd_trans_create(const struct lu_env *env,
1006 struct dt_device *d)
1008 struct osd_thread_info *oti = osd_oti_get(env);
1009 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1010 struct osd_thandle *oh;
1014 /* on pending IO in this thread should left from prev. request */
1015 LASSERT(atomic_read(&iobuf->dr_numreqs) == 0);
1017 th = ERR_PTR(-ENOMEM);
1018 OBD_ALLOC_GFP(oh, sizeof *oh, GFP_NOFS);
1020 oh->ot_quota_trans = &oti->oti_quota_trans;
1021 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
1025 th->th_tags = LCT_TX_HANDLE;
1027 INIT_LIST_HEAD(&oh->ot_commit_dcb_list);
1028 INIT_LIST_HEAD(&oh->ot_stop_dcb_list);
1031 memset(oti->oti_declare_ops, 0,
1032 sizeof(oti->oti_declare_ops));
1033 memset(oti->oti_declare_ops_cred, 0,
1034 sizeof(oti->oti_declare_ops_cred));
1035 memset(oti->oti_declare_ops_used, 0,
1036 sizeof(oti->oti_declare_ops_used));
1041 void osd_trans_dump_creds(const struct lu_env *env, struct thandle *th)
1043 struct osd_thread_info *oti = osd_oti_get(env);
1044 struct osd_thandle *oh;
1046 oh = container_of0(th, struct osd_thandle, ot_super);
1047 LASSERT(oh != NULL);
1049 CWARN(" create: %u/%u/%u, destroy: %u/%u/%u\n",
1050 oti->oti_declare_ops[OSD_OT_CREATE],
1051 oti->oti_declare_ops_cred[OSD_OT_CREATE],
1052 oti->oti_declare_ops_used[OSD_OT_CREATE],
1053 oti->oti_declare_ops[OSD_OT_DESTROY],
1054 oti->oti_declare_ops_cred[OSD_OT_DESTROY],
1055 oti->oti_declare_ops_used[OSD_OT_DESTROY]);
1056 CWARN(" attr_set: %u/%u/%u, xattr_set: %u/%u/%u\n",
1057 oti->oti_declare_ops[OSD_OT_ATTR_SET],
1058 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
1059 oti->oti_declare_ops_used[OSD_OT_ATTR_SET],
1060 oti->oti_declare_ops[OSD_OT_XATTR_SET],
1061 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET],
1062 oti->oti_declare_ops_used[OSD_OT_XATTR_SET]);
1063 CWARN(" write: %u/%u/%u, punch: %u/%u/%u, quota %u/%u/%u\n",
1064 oti->oti_declare_ops[OSD_OT_WRITE],
1065 oti->oti_declare_ops_cred[OSD_OT_WRITE],
1066 oti->oti_declare_ops_used[OSD_OT_WRITE],
1067 oti->oti_declare_ops[OSD_OT_PUNCH],
1068 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
1069 oti->oti_declare_ops_used[OSD_OT_PUNCH],
1070 oti->oti_declare_ops[OSD_OT_QUOTA],
1071 oti->oti_declare_ops_cred[OSD_OT_QUOTA],
1072 oti->oti_declare_ops_used[OSD_OT_QUOTA]);
1073 CWARN(" insert: %u/%u/%u, delete: %u/%u/%u\n",
1074 oti->oti_declare_ops[OSD_OT_INSERT],
1075 oti->oti_declare_ops_cred[OSD_OT_INSERT],
1076 oti->oti_declare_ops_used[OSD_OT_INSERT],
1077 oti->oti_declare_ops[OSD_OT_DELETE],
1078 oti->oti_declare_ops_cred[OSD_OT_DELETE],
1079 oti->oti_declare_ops_used[OSD_OT_DELETE]);
1080 CWARN(" ref_add: %u/%u/%u, ref_del: %u/%u/%u\n",
1081 oti->oti_declare_ops[OSD_OT_REF_ADD],
1082 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
1083 oti->oti_declare_ops_used[OSD_OT_REF_ADD],
1084 oti->oti_declare_ops[OSD_OT_REF_DEL],
1085 oti->oti_declare_ops_cred[OSD_OT_REF_DEL],
1086 oti->oti_declare_ops_used[OSD_OT_REF_DEL]);
1090 * Concurrency: shouldn't matter.
1092 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
1095 struct osd_thread_info *oti = osd_oti_get(env);
1096 struct osd_device *dev = osd_dt_dev(d);
1098 struct osd_thandle *oh;
1103 LASSERT(current->journal_info == NULL);
1105 oh = container_of0(th, struct osd_thandle, ot_super);
1106 LASSERT(oh != NULL);
1107 LASSERT(oh->ot_handle == NULL);
1109 rc = dt_txn_hook_start(env, d, th);
1113 if (unlikely(osd_param_is_not_sane(dev, th))) {
1114 static unsigned long last_printed;
1115 static int last_credits;
1117 CWARN("%.16s: too many transaction credits (%d > %d)\n",
1118 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
1120 osd_journal(dev)->j_max_transaction_buffers);
1122 osd_trans_dump_creds(env, th);
1124 if (last_credits != oh->ot_credits &&
1125 time_after(jiffies, last_printed +
1126 msecs_to_jiffies(60 * MSEC_PER_SEC))) {
1127 libcfs_debug_dumpstack(NULL);
1128 last_credits = oh->ot_credits;
1129 last_printed = jiffies;
1131 /* XXX Limit the credits to 'max_transaction_buffers', and
1132 * let the underlying filesystem to catch the error if
1133 * we really need so many credits.
1135 * This should be removed when we can calculate the
1136 * credits precisely. */
1137 oh->ot_credits = osd_transaction_size(dev);
1141 * XXX temporary stuff. Some abstraction layer should
1144 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, oh->ot_credits);
1148 LASSERT(oti->oti_txns == 0);
1149 lu_context_init(&th->th_ctx, th->th_tags);
1150 lu_context_enter(&th->th_ctx);
1152 lu_device_get(&d->dd_lu_dev);
1153 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
1164 static int osd_seq_exists(const struct lu_env *env,
1165 struct osd_device *osd, u64 seq)
1167 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1168 struct seq_server_site *ss = osd_seq_site(osd);
1172 LASSERT(ss != NULL);
1173 LASSERT(ss->ss_server_fld != NULL);
1175 rc = osd_fld_lookup(env, osd, seq, range);
1178 CERROR("%s: can't lookup FLD sequence "LPX64
1179 ": rc = %d\n", osd_name(osd), seq, rc);
1183 RETURN(ss->ss_node_id == range->lsr_index);
1186 static void osd_trans_stop_cb(struct osd_thandle *oth, int result)
1188 struct dt_txn_commit_cb *dcb;
1189 struct dt_txn_commit_cb *tmp;
1191 /* call per-transaction stop callbacks if any */
1192 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
1194 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1195 "commit callback entry: magic=%x name='%s'\n",
1196 dcb->dcb_magic, dcb->dcb_name);
1197 list_del_init(&dcb->dcb_linkage);
1198 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1203 * Concurrency: shouldn't matter.
1205 static int osd_trans_stop(const struct lu_env *env, struct dt_device *dt,
1208 int rc = 0, remove_agents = 0;
1209 struct osd_thandle *oh;
1210 struct osd_thread_info *oti = osd_oti_get(env);
1211 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1212 struct osd_device *osd = osd_dt_dev(th->th_dev);
1213 struct qsd_instance *qsd = osd->od_quota_slave;
1214 struct lquota_trans *qtrans;
1217 oh = container_of0(th, struct osd_thandle, ot_super);
1219 remove_agents = oh->ot_remove_agents;
1221 qtrans = oh->ot_quota_trans;
1222 oh->ot_quota_trans = NULL;
1224 if (oh->ot_handle != NULL) {
1225 handle_t *hdl = oh->ot_handle;
1228 * add commit callback
1229 * notice we don't do this in osd_trans_start()
1230 * as underlying transaction can change during truncate
1232 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
1235 LASSERT(oti->oti_txns == 1);
1238 rc = dt_txn_hook_stop(env, th);
1240 CERROR("%s: failed in transaction hook: rc = %d\n",
1243 osd_trans_stop_cb(oh, rc);
1244 /* hook functions might modify th_sync */
1245 hdl->h_sync = th->th_sync;
1247 oh->ot_handle = NULL;
1248 OSD_CHECK_SLOW_TH(oh, osd, rc = ldiskfs_journal_stop(hdl));
1250 CERROR("%s: failed to stop transaction: rc = %d\n",
1253 osd_trans_stop_cb(oh, th->th_result);
1257 /* inform the quota slave device that the transaction is stopping */
1258 qsd_op_end(env, qsd, qtrans);
1260 /* as we want IO to journal and data IO be concurrent, we don't block
1261 * awaiting data IO completion in osd_do_bio(), instead we wait here
1262 * once transaction is submitted to the journal. all reqular requests
1263 * don't do direct IO (except read/write), thus this wait_event becomes
1266 * IMPORTANT: we have to wait till any IO submited by the thread is
1267 * completed otherwise iobuf may be corrupted by different request
1269 wait_event(iobuf->dr_wait,
1270 atomic_read(&iobuf->dr_numreqs) == 0);
1271 osd_fini_iobuf(osd, iobuf);
1273 rc = iobuf->dr_error;
1275 if (unlikely(remove_agents != 0))
1276 osd_process_scheduled_agent_removals(env, osd);
1281 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
1283 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
1286 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1287 LASSERT(&dcb->dcb_func != NULL);
1288 if (dcb->dcb_flags & DCB_TRANS_STOP)
1289 list_add(&dcb->dcb_linkage, &oh->ot_stop_dcb_list);
1291 list_add(&dcb->dcb_linkage, &oh->ot_commit_dcb_list);
1297 * Called just before object is freed. Releases all resources except for
1298 * object itself (that is released by osd_object_free()).
1300 * Concurrency: no concurrent access is possible that late in object
1303 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
1305 struct osd_object *obj = osd_obj(l);
1306 struct inode *inode = obj->oo_inode;
1308 LINVRNT(osd_invariant(obj));
1311 * If object is unlinked remove fid->ino mapping from object index.
1314 osd_index_fini(obj);
1315 if (inode != NULL) {
1316 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
1317 qid_t uid = i_uid_read(inode);
1318 qid_t gid = i_gid_read(inode);
1321 obj->oo_inode = NULL;
1324 struct osd_thread_info *info = osd_oti_get(env);
1325 struct lquota_id_info *qi = &info->oti_qi;
1327 /* Release granted quota to master if necessary */
1328 qi->lqi_id.qid_uid = uid;
1329 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1331 qi->lqi_id.qid_uid = gid;
1332 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1338 * Concurrency: ->loo_object_release() is called under site spin-lock.
1340 static void osd_object_release(const struct lu_env *env,
1341 struct lu_object *l)
1346 * Concurrency: shouldn't matter.
1348 static int osd_object_print(const struct lu_env *env, void *cookie,
1349 lu_printer_t p, const struct lu_object *l)
1351 struct osd_object *o = osd_obj(l);
1352 struct iam_descr *d;
1354 if (o->oo_dir != NULL)
1355 d = o->oo_dir->od_container.ic_descr;
1358 return (*p)(env, cookie,
1359 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1361 o->oo_inode ? o->oo_inode->i_ino : 0UL,
1362 o->oo_inode ? o->oo_inode->i_generation : 0,
1363 d ? d->id_ops->id_name : "plain");
1366 #define GRANT_FOR_LOCAL_OIDS 32 /* 128kB for last_rcvd, quota files, ... */
1369 * Concurrency: shouldn't matter.
1371 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1372 struct obd_statfs *sfs)
1374 struct osd_device *osd = osd_dt_dev(d);
1375 struct super_block *sb = osd_sb(osd);
1376 struct kstatfs *ksfs;
1379 if (unlikely(osd->od_mnt == NULL))
1380 return -EINPROGRESS;
1382 /* osd_lproc.c call this without env, allocate ksfs for that case */
1383 if (unlikely(env == NULL)) {
1384 OBD_ALLOC_PTR(ksfs);
1388 ksfs = &osd_oti_get(env)->oti_ksfs;
1391 spin_lock(&osd->od_osfs_lock);
1392 result = sb->s_op->statfs(sb->s_root, ksfs);
1393 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1394 statfs_pack(sfs, ksfs);
1395 if (unlikely(sb->s_flags & MS_RDONLY))
1396 sfs->os_state = OS_STATE_READONLY;
1397 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb,
1398 LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1399 sfs->os_maxbytes = sb->s_maxbytes;
1401 sfs->os_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1403 spin_unlock(&osd->od_osfs_lock);
1405 if (unlikely(env == NULL))
1408 /* Reserve a small amount of space for local objects like last_rcvd,
1409 * llog, quota files, ... */
1410 if (sfs->os_bavail <= GRANT_FOR_LOCAL_OIDS) {
1413 sfs->os_bavail -= GRANT_FOR_LOCAL_OIDS;
1414 /** Take out metadata overhead for indirect blocks */
1415 sfs->os_bavail -= sfs->os_bavail >> (sb->s_blocksize_bits - 3);
1422 * Estimate space needed for file creations. We assume the largest filename
1423 * which is 2^64 - 1, hence a filename of 20 chars.
1424 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1426 #ifdef __LDISKFS_DIR_REC_LEN
1427 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1429 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1433 * Concurrency: doesn't access mutable data.
1435 static void osd_conf_get(const struct lu_env *env,
1436 const struct dt_device *dev,
1437 struct dt_device_param *param)
1439 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1443 * XXX should be taken from not-yet-existing fs abstraction layer.
1445 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1446 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1447 param->ddp_block_shift = sb->s_blocksize_bits;
1448 param->ddp_mount_type = LDD_MT_LDISKFS;
1449 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EXTENTS))
1450 param->ddp_maxbytes = sb->s_maxbytes;
1452 param->ddp_maxbytes = LDISKFS_SB(sb)->s_bitmap_maxbytes;
1453 /* Overhead estimate should be fairly accurate, so we really take a tiny
1454 * error margin which also avoids fragmenting the filesystem too much */
1455 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1456 /* inode are statically allocated, so per-inode space consumption
1457 * is the space consumed by the directory entry */
1458 param->ddp_inodespace = PER_OBJ_USAGE;
1459 /* per-fragment overhead to be used by the client code */
1460 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1461 param->ddp_mntopts = 0;
1462 if (test_opt(sb, XATTR_USER))
1463 param->ddp_mntopts |= MNTOPT_USERXATTR;
1464 if (test_opt(sb, POSIX_ACL))
1465 param->ddp_mntopts |= MNTOPT_ACL;
1467 /* LOD might calculate the max stripe count based on max_ea_size,
1468 * so we need take account in the overhead as well,
1469 * xattr_header + magic + xattr_entry_head */
1470 ea_overhead = sizeof(struct ldiskfs_xattr_header) + sizeof(__u32) +
1471 LDISKFS_XATTR_LEN(XATTR_NAME_MAX_LEN);
1473 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1474 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1475 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE -
1479 param->ddp_max_ea_size = sb->s_blocksize - ea_overhead;
1483 * Concurrency: shouldn't matter.
1485 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1489 CDEBUG(D_CACHE, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1491 rc = ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1493 CDEBUG(D_CACHE, "synced OSD %s: rc = %d\n",
1494 LUSTRE_OSD_LDISKFS_NAME, rc);
1500 * Start commit for OSD device.
1502 * An implementation of dt_commit_async method for OSD device.
1503 * Asychronously starts underlayng fs sync and thereby a transaction
1506 * \param env environment
1507 * \param d dt device
1509 * \see dt_device_operations
1511 static int osd_commit_async(const struct lu_env *env,
1512 struct dt_device *d)
1514 struct super_block *s = osd_sb(osd_dt_dev(d));
1517 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1518 RETURN(s->s_op->sync_fs(s, 0));
1522 * Concurrency: shouldn't matter.
1525 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1527 struct super_block *sb = osd_sb(osd_dt_dev(d));
1528 struct block_device *dev = sb->s_bdev;
1529 #ifdef HAVE_DEV_SET_RDONLY
1530 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1533 int rc = -EOPNOTSUPP;
1537 #ifdef HAVE_DEV_SET_RDONLY
1538 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1540 if (jdev && (jdev != dev)) {
1541 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1543 dev_set_rdonly(jdev);
1545 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1546 dev_set_rdonly(dev);
1548 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1549 osd_dt_dev(d)->od_svname, (long)dev, rc);
1555 * Note: we do not count into QUOTA here.
1556 * If we mount with --data_journal we may need more.
1558 const int osd_dto_credits_noquota[DTO_NR] = {
1561 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1562 * SINGLEDATA_TRANS_BLOCKS(8)
1563 * XXX Note: maybe iam need more, since iam have more level than
1566 [DTO_INDEX_INSERT] = 16,
1569 * just modify a single entry, probably merge few within a block
1571 [DTO_INDEX_DELETE] = 1,
1575 [DTO_INDEX_UPDATE] = 16,
1577 * 4(inode, inode bits, groups, GDT)
1578 * notice: OI updates are counted separately with DTO_INDEX_INSERT
1580 [DTO_OBJECT_CREATE] = 4,
1582 * 4(inode, inode bits, groups, GDT)
1583 * notice: OI updates are counted separately with DTO_INDEX_DELETE
1585 [DTO_OBJECT_DELETE] = 4,
1587 * Attr set credits (inode)
1589 [DTO_ATTR_SET_BASE] = 1,
1591 * Xattr set. The same as xattr of EXT3.
1592 * DATA_TRANS_BLOCKS(14)
1593 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1594 * are also counted in. Do not know why?
1596 [DTO_XATTR_SET] = 14,
1598 * credits for inode change during write.
1600 [DTO_WRITE_BASE] = 3,
1602 * credits for single block write.
1604 [DTO_WRITE_BLOCK] = 14,
1606 * Attr set credits for chown.
1607 * This is extra credits for setattr, and it is null without quota
1609 [DTO_ATTR_SET_CHOWN] = 0
1612 static const struct dt_device_operations osd_dt_ops = {
1613 .dt_root_get = osd_root_get,
1614 .dt_statfs = osd_statfs,
1615 .dt_trans_create = osd_trans_create,
1616 .dt_trans_start = osd_trans_start,
1617 .dt_trans_stop = osd_trans_stop,
1618 .dt_trans_cb_add = osd_trans_cb_add,
1619 .dt_conf_get = osd_conf_get,
1620 .dt_sync = osd_sync,
1622 .dt_commit_async = osd_commit_async,
1625 static void osd_object_read_lock(const struct lu_env *env,
1626 struct dt_object *dt, unsigned role)
1628 struct osd_object *obj = osd_dt_obj(dt);
1629 struct osd_thread_info *oti = osd_oti_get(env);
1631 LINVRNT(osd_invariant(obj));
1633 LASSERT(obj->oo_owner != env);
1634 down_read_nested(&obj->oo_sem, role);
1636 LASSERT(obj->oo_owner == NULL);
1640 static void osd_object_write_lock(const struct lu_env *env,
1641 struct dt_object *dt, unsigned role)
1643 struct osd_object *obj = osd_dt_obj(dt);
1644 struct osd_thread_info *oti = osd_oti_get(env);
1646 LINVRNT(osd_invariant(obj));
1648 LASSERT(obj->oo_owner != env);
1649 down_write_nested(&obj->oo_sem, role);
1651 LASSERT(obj->oo_owner == NULL);
1652 obj->oo_owner = env;
1656 static void osd_object_read_unlock(const struct lu_env *env,
1657 struct dt_object *dt)
1659 struct osd_object *obj = osd_dt_obj(dt);
1660 struct osd_thread_info *oti = osd_oti_get(env);
1662 LINVRNT(osd_invariant(obj));
1664 LASSERT(oti->oti_r_locks > 0);
1666 up_read(&obj->oo_sem);
1669 static void osd_object_write_unlock(const struct lu_env *env,
1670 struct dt_object *dt)
1672 struct osd_object *obj = osd_dt_obj(dt);
1673 struct osd_thread_info *oti = osd_oti_get(env);
1675 LINVRNT(osd_invariant(obj));
1677 LASSERT(obj->oo_owner == env);
1678 LASSERT(oti->oti_w_locks > 0);
1680 obj->oo_owner = NULL;
1681 up_write(&obj->oo_sem);
1684 static int osd_object_write_locked(const struct lu_env *env,
1685 struct dt_object *dt)
1687 struct osd_object *obj = osd_dt_obj(dt);
1689 LINVRNT(osd_invariant(obj));
1691 return obj->oo_owner == env;
1694 static struct timespec *osd_inode_time(const struct lu_env *env,
1695 struct inode *inode, __u64 seconds)
1697 struct osd_thread_info *oti = osd_oti_get(env);
1698 struct timespec *t = &oti->oti_time;
1700 t->tv_sec = seconds;
1702 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1707 static void osd_inode_getattr(const struct lu_env *env,
1708 struct inode *inode, struct lu_attr *attr)
1710 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1711 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1712 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1715 attr->la_atime = LTIME_S(inode->i_atime);
1716 attr->la_mtime = LTIME_S(inode->i_mtime);
1717 attr->la_ctime = LTIME_S(inode->i_ctime);
1718 attr->la_mode = inode->i_mode;
1719 attr->la_size = i_size_read(inode);
1720 attr->la_blocks = inode->i_blocks;
1721 attr->la_uid = i_uid_read(inode);
1722 attr->la_gid = i_gid_read(inode);
1723 attr->la_flags = LDISKFS_I(inode)->i_flags;
1724 attr->la_nlink = inode->i_nlink;
1725 attr->la_rdev = inode->i_rdev;
1726 attr->la_blksize = 1 << inode->i_blkbits;
1727 attr->la_blkbits = inode->i_blkbits;
1730 static int osd_attr_get(const struct lu_env *env,
1731 struct dt_object *dt,
1732 struct lu_attr *attr)
1734 struct osd_object *obj = osd_dt_obj(dt);
1736 if (!dt_object_exists(dt))
1739 LASSERT(!dt_object_remote(dt));
1740 LINVRNT(osd_invariant(obj));
1742 spin_lock(&obj->oo_guard);
1743 osd_inode_getattr(env, obj->oo_inode, attr);
1744 spin_unlock(&obj->oo_guard);
1748 static int osd_declare_attr_set(const struct lu_env *env,
1749 struct dt_object *dt,
1750 const struct lu_attr *attr,
1751 struct thandle *handle)
1753 struct osd_thandle *oh;
1754 struct osd_object *obj;
1755 struct osd_thread_info *info = osd_oti_get(env);
1756 struct lquota_id_info *qi = &info->oti_qi;
1764 LASSERT(dt != NULL);
1765 LASSERT(handle != NULL);
1767 obj = osd_dt_obj(dt);
1768 LASSERT(osd_invariant(obj));
1770 oh = container_of0(handle, struct osd_thandle, ot_super);
1771 LASSERT(oh->ot_handle == NULL);
1773 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1774 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1776 if (attr == NULL || obj->oo_inode == NULL)
1779 bspace = obj->oo_inode->i_blocks;
1780 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1781 bspace = toqb(bspace);
1783 /* Changing ownership is always preformed by super user, it should not
1786 * We still need to call the osd_declare_qid() to calculate the journal
1787 * credits for updating quota accounting files and to trigger quota
1788 * space adjustment once the operation is completed.*/
1789 if (attr->la_valid & LA_UID || attr->la_valid & LA_GID) {
1791 uid = i_uid_read(obj->oo_inode);
1792 qi->lqi_type = USRQUOTA;
1793 enforce = (attr->la_valid & LA_UID) && (attr->la_uid != uid);
1794 /* inode accounting */
1795 qi->lqi_is_blk = false;
1797 /* one more inode for the new uid ... */
1798 qi->lqi_id.qid_uid = attr->la_uid;
1800 /* Reserve credits for the new uid */
1801 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
1802 if (rc == -EDQUOT || rc == -EINPROGRESS)
1807 /* and one less inode for the current uid */
1808 qi->lqi_id.qid_uid = uid;
1810 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1811 if (rc == -EDQUOT || rc == -EINPROGRESS)
1816 /* block accounting */
1817 qi->lqi_is_blk = true;
1819 /* more blocks for the new uid ... */
1820 qi->lqi_id.qid_uid = attr->la_uid;
1821 qi->lqi_space = bspace;
1823 * Credits for the new uid has been reserved, re-use "obj"
1824 * to save credit reservation.
1826 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1827 if (rc == -EDQUOT || rc == -EINPROGRESS)
1832 /* and finally less blocks for the current uid */
1833 qi->lqi_id.qid_uid = uid;
1834 qi->lqi_space = -bspace;
1835 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1836 if (rc == -EDQUOT || rc == -EINPROGRESS)
1842 gid = i_gid_read(obj->oo_inode);
1843 qi->lqi_type = GRPQUOTA;
1844 enforce = (attr->la_valid & LA_GID) && (attr->la_gid != gid);
1846 /* inode accounting */
1847 qi->lqi_is_blk = false;
1849 /* one more inode for the new gid ... */
1850 qi->lqi_id.qid_gid = attr->la_gid;
1852 rc = osd_declare_qid(env, oh, qi, NULL, enforce, NULL);
1853 if (rc == -EDQUOT || rc == -EINPROGRESS)
1858 /* and one less inode for the current gid */
1859 qi->lqi_id.qid_gid = gid;
1861 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1862 if (rc == -EDQUOT || rc == -EINPROGRESS)
1867 /* block accounting */
1868 qi->lqi_is_blk = true;
1870 /* more blocks for the new gid ... */
1871 qi->lqi_id.qid_gid = attr->la_gid;
1872 qi->lqi_space = bspace;
1873 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1874 if (rc == -EDQUOT || rc == -EINPROGRESS)
1879 /* and finally less blocks for the current gid */
1880 qi->lqi_id.qid_gid = gid;
1881 qi->lqi_space = -bspace;
1882 rc = osd_declare_qid(env, oh, qi, obj, enforce, NULL);
1883 if (rc == -EDQUOT || rc == -EINPROGRESS)
1892 static int osd_inode_setattr(const struct lu_env *env,
1893 struct inode *inode, const struct lu_attr *attr)
1895 __u64 bits = attr->la_valid;
1897 /* Only allow set size for regular file */
1898 if (!S_ISREG(inode->i_mode))
1899 bits &= ~(LA_SIZE | LA_BLOCKS);
1904 if (bits & LA_ATIME)
1905 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1906 if (bits & LA_CTIME)
1907 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1908 if (bits & LA_MTIME)
1909 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1910 if (bits & LA_SIZE) {
1911 LDISKFS_I(inode)->i_disksize = attr->la_size;
1912 i_size_write(inode, attr->la_size);
1916 /* OSD should not change "i_blocks" which is used by quota.
1917 * "i_blocks" should be changed by ldiskfs only. */
1918 if (bits & LA_BLOCKS)
1919 inode->i_blocks = attr->la_blocks;
1922 inode->i_mode = (inode->i_mode & S_IFMT) |
1923 (attr->la_mode & ~S_IFMT);
1925 i_uid_write(inode, attr->la_uid);
1927 i_gid_write(inode, attr->la_gid);
1928 if (bits & LA_NLINK)
1929 set_nlink(inode, attr->la_nlink);
1931 inode->i_rdev = attr->la_rdev;
1933 if (bits & LA_FLAGS) {
1934 /* always keep S_NOCMTIME */
1935 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1941 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1943 if ((attr->la_valid & LA_UID && attr->la_uid != i_uid_read(inode)) ||
1944 (attr->la_valid & LA_GID && attr->la_gid != i_gid_read(inode))) {
1948 ll_vfs_dq_init(inode);
1950 if (attr->la_valid & LA_UID)
1951 iattr.ia_valid |= ATTR_UID;
1952 if (attr->la_valid & LA_GID)
1953 iattr.ia_valid |= ATTR_GID;
1954 iattr.ia_uid = make_kuid(&init_user_ns, attr->la_uid);
1955 iattr.ia_gid = make_kgid(&init_user_ns, attr->la_gid);
1957 rc = ll_vfs_dq_transfer(inode, &iattr);
1959 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1960 "enforcement enabled on the ldiskfs "
1961 "filesystem?\n", inode->i_sb->s_id, rc);
1968 static int osd_attr_set(const struct lu_env *env,
1969 struct dt_object *dt,
1970 const struct lu_attr *attr,
1971 struct thandle *handle)
1973 struct osd_object *obj = osd_dt_obj(dt);
1974 struct inode *inode;
1977 if (!dt_object_exists(dt))
1980 LASSERT(handle != NULL);
1981 LASSERT(!dt_object_remote(dt));
1982 LASSERT(osd_invariant(obj));
1984 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1986 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1987 struct osd_thread_info *oti = osd_oti_get(env);
1988 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1989 struct lu_fid *fid1 = &oti->oti_fid;
1990 struct osd_inode_id *id = &oti->oti_id;
1991 struct iam_path_descr *ipd;
1992 struct iam_container *bag;
1993 struct osd_thandle *oh;
1996 fid_cpu_to_be(fid1, fid0);
1997 memset(id, 1, sizeof(*id));
1998 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1999 fid0)->oi_dir.od_container;
2000 ipd = osd_idx_ipd_get(env, bag);
2001 if (unlikely(ipd == NULL))
2004 oh = container_of0(handle, struct osd_thandle, ot_super);
2005 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
2006 (const struct iam_rec *)id, ipd);
2007 osd_ipd_put(env, bag, ipd);
2008 return(rc > 0 ? 0 : rc);
2011 inode = obj->oo_inode;
2013 rc = osd_quota_transfer(inode, attr);
2017 spin_lock(&obj->oo_guard);
2018 rc = osd_inode_setattr(env, inode, attr);
2019 spin_unlock(&obj->oo_guard);
2022 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2024 osd_trans_exec_check(env, handle, OSD_OT_ATTR_SET);
2029 static struct dentry *osd_child_dentry_get(const struct lu_env *env,
2030 struct osd_object *obj,
2031 const char *name, const int namelen)
2033 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
2036 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
2037 umode_t mode, struct dt_allocation_hint *hint,
2041 struct osd_device *osd = osd_obj2dev(obj);
2042 struct osd_thandle *oth;
2043 struct dt_object *parent = NULL;
2044 struct inode *inode;
2046 LINVRNT(osd_invariant(obj));
2047 LASSERT(obj->oo_inode == NULL);
2048 LASSERT(obj->oo_hl_head == NULL);
2050 if (S_ISDIR(mode) && ldiskfs_pdo) {
2051 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
2052 if (obj->oo_hl_head == NULL)
2056 oth = container_of(th, struct osd_thandle, ot_super);
2057 LASSERT(oth->ot_handle->h_transaction != NULL);
2059 if (hint != NULL && hint->dah_parent != NULL &&
2060 !dt_object_remote(hint->dah_parent))
2061 parent = hint->dah_parent;
2063 inode = ldiskfs_create_inode(oth->ot_handle,
2064 parent ? osd_dt_obj(parent)->oo_inode :
2065 osd_sb(osd)->s_root->d_inode,
2067 if (!IS_ERR(inode)) {
2068 /* Do not update file c/mtime in ldiskfs. */
2069 inode->i_flags |= S_NOCMTIME;
2071 /* For new created object, it must be consistent,
2072 * and it is unnecessary to scrub against it. */
2073 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
2075 obj->oo_inode = inode;
2078 if (obj->oo_hl_head != NULL) {
2079 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
2080 obj->oo_hl_head = NULL;
2082 result = PTR_ERR(inode);
2084 LINVRNT(osd_invariant(obj));
2092 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
2093 struct lu_attr *attr,
2094 struct dt_allocation_hint *hint,
2095 struct dt_object_format *dof,
2099 struct osd_thandle *oth;
2100 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
2102 LASSERT(S_ISDIR(attr->la_mode));
2104 oth = container_of(th, struct osd_thandle, ot_super);
2105 LASSERT(oth->ot_handle->h_transaction != NULL);
2106 result = osd_mkfile(info, obj, mode, hint, th);
2111 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
2112 struct lu_attr *attr,
2113 struct dt_allocation_hint *hint,
2114 struct dt_object_format *dof,
2118 struct osd_thandle *oth;
2119 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
2121 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
2123 LASSERT(S_ISREG(attr->la_mode));
2125 oth = container_of(th, struct osd_thandle, ot_super);
2126 LASSERT(oth->ot_handle->h_transaction != NULL);
2128 result = osd_mkfile(info, obj, mode, hint, th);
2130 LASSERT(obj->oo_inode != NULL);
2131 if (feat->dif_flags & DT_IND_VARKEY)
2132 result = iam_lvar_create(obj->oo_inode,
2133 feat->dif_keysize_max,
2135 feat->dif_recsize_max,
2138 result = iam_lfix_create(obj->oo_inode,
2139 feat->dif_keysize_max,
2141 feat->dif_recsize_max,
2148 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
2149 struct lu_attr *attr,
2150 struct dt_allocation_hint *hint,
2151 struct dt_object_format *dof,
2154 LASSERT(S_ISREG(attr->la_mode));
2155 return osd_mkfile(info, obj, (attr->la_mode &
2156 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2159 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
2160 struct lu_attr *attr,
2161 struct dt_allocation_hint *hint,
2162 struct dt_object_format *dof,
2165 LASSERT(S_ISLNK(attr->la_mode));
2166 return osd_mkfile(info, obj, (attr->la_mode &
2167 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
2170 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
2171 struct lu_attr *attr,
2172 struct dt_allocation_hint *hint,
2173 struct dt_object_format *dof,
2176 umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
2179 LINVRNT(osd_invariant(obj));
2180 LASSERT(obj->oo_inode == NULL);
2181 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
2182 S_ISFIFO(mode) || S_ISSOCK(mode));
2184 result = osd_mkfile(info, obj, mode, hint, th);
2186 LASSERT(obj->oo_inode != NULL);
2188 * This inode should be marked dirty for i_rdev. Currently
2189 * that is done in the osd_attr_init().
2191 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
2194 LINVRNT(osd_invariant(obj));
2198 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
2200 struct dt_allocation_hint *hint,
2201 struct dt_object_format *dof,
2204 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
2206 osd_obj_type_f result;
2222 result = osd_mk_index;
2233 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2234 struct dt_object *parent, struct dt_object *child,
2239 ah->dah_parent = parent;
2240 ah->dah_mode = child_mode;
2243 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2244 struct lu_attr *attr, struct dt_object_format *dof)
2246 struct inode *inode = obj->oo_inode;
2247 __u64 valid = attr->la_valid;
2250 attr->la_valid &= ~(LA_TYPE | LA_MODE);
2252 if (dof->dof_type != DFT_NODE)
2253 attr->la_valid &= ~LA_RDEV;
2254 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2255 attr->la_valid &= ~LA_ATIME;
2256 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2257 attr->la_valid &= ~LA_CTIME;
2258 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2259 attr->la_valid &= ~LA_MTIME;
2261 result = osd_quota_transfer(inode, attr);
2265 if (attr->la_valid != 0) {
2266 result = osd_inode_setattr(info->oti_env, inode, attr);
2268 * The osd_inode_setattr() should always succeed here. The
2269 * only error that could be returned is EDQUOT when we are
2270 * trying to change the UID or GID of the inode. However, this
2271 * should not happen since quota enforcement is no longer
2272 * enabled on ldiskfs (lquota takes care of it).
2274 LASSERTF(result == 0, "%d\n", result);
2275 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2278 attr->la_valid = valid;
2282 * Helper function for osd_object_create()
2284 * \retval 0, on success
2286 static int __osd_object_create(struct osd_thread_info *info,
2287 struct osd_object *obj, struct lu_attr *attr,
2288 struct dt_allocation_hint *hint,
2289 struct dt_object_format *dof,
2295 osd_trans_exec_op(info->oti_env, th, OSD_OT_CREATE);
2297 /* we drop umask so that permissions we pass are not affected */
2298 umask = current->fs->umask;
2299 current->fs->umask = 0;
2301 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2303 if (likely(obj->oo_inode != NULL)) {
2304 LASSERT(obj->oo_inode->i_state & I_NEW);
2306 /* Unlock the inode before attr initialization to avoid
2307 * unnecessary dqget operations. LU-6378 */
2308 unlock_new_inode(obj->oo_inode);
2311 if (likely(result == 0)) {
2312 osd_attr_init(info, obj, attr, dof);
2313 osd_object_init0(obj);
2316 /* restore previous umask value */
2317 current->fs->umask = umask;
2319 osd_trans_exec_check(info->oti_env, th, OSD_OT_CREATE);
2325 * Helper function for osd_object_create()
2327 * \retval 0, on success
2329 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2330 const struct lu_fid *fid, struct thandle *th)
2332 struct osd_thread_info *info = osd_oti_get(env);
2333 struct osd_inode_id *id = &info->oti_id;
2334 struct osd_device *osd = osd_obj2dev(obj);
2335 struct osd_thandle *oh;
2338 LASSERT(obj->oo_inode != NULL);
2340 oh = container_of0(th, struct osd_thandle, ot_super);
2341 LASSERT(oh->ot_handle);
2342 osd_trans_exec_op(env, th, OSD_OT_INSERT);
2344 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2345 rc = osd_oi_insert(info, osd, fid, id, oh->ot_handle, OI_CHECK_FLD);
2346 osd_trans_exec_check(env, th, OSD_OT_INSERT);
2351 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2352 u64 seq, struct lu_seq_range *range)
2354 struct seq_server_site *ss = osd_seq_site(osd);
2356 if (fid_seq_is_idif(seq)) {
2357 fld_range_set_ost(range);
2358 range->lsr_index = idif_ost_idx(seq);
2362 if (!fid_seq_in_fldb(seq)) {
2363 fld_range_set_mdt(range);
2365 /* FIXME: If ss is NULL, it suppose not get lsr_index
2367 range->lsr_index = ss->ss_node_id;
2371 LASSERT(ss != NULL);
2372 fld_range_set_any(range);
2373 /* OSD will only do local fld lookup */
2374 return fld_local_lookup(env, ss->ss_server_fld, seq, range);
2378 * Concurrency: no external locking is necessary.
2380 static int osd_declare_object_create(const struct lu_env *env,
2381 struct dt_object *dt,
2382 struct lu_attr *attr,
2383 struct dt_allocation_hint *hint,
2384 struct dt_object_format *dof,
2385 struct thandle *handle)
2387 struct osd_thandle *oh;
2391 LASSERT(handle != NULL);
2393 oh = container_of0(handle, struct osd_thandle, ot_super);
2394 LASSERT(oh->ot_handle == NULL);
2396 /* EA object consumes more credits than regular object: osd_mk_index
2397 * vs. osd_mkreg: osd_mk_index will create 2 blocks for root_node and
2398 * leaf_node, could involves the block, block bitmap, groups, GDT
2399 * change for each block, so add 4 * 2 credits in that case. */
2400 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2401 osd_dto_credits_noquota[DTO_OBJECT_CREATE] +
2402 (dof->dof_type == DFT_INDEX) ? 4 * 2 : 0);
2403 /* Reuse idle OI block may cause additional one OI block
2405 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2406 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2411 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2412 osd_dt_obj(dt), false, NULL, false);
2419 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2420 struct lu_attr *attr,
2421 struct dt_allocation_hint *hint,
2422 struct dt_object_format *dof, struct thandle *th)
2424 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2425 struct osd_object *obj = osd_dt_obj(dt);
2426 struct osd_thread_info *info = osd_oti_get(env);
2430 if (dt_object_exists(dt))
2433 LINVRNT(osd_invariant(obj));
2434 LASSERT(!dt_object_remote(dt));
2435 LASSERT(osd_write_locked(env, obj));
2436 LASSERT(th != NULL);
2438 if (unlikely(fid_is_acct(fid)))
2439 /* Quota files can't be created from the kernel any more,
2440 * 'tune2fs -O quota' will take care of creating them */
2443 result = __osd_object_create(info, obj, attr, hint, dof, th);
2445 result = __osd_oi_insert(env, obj, fid, th);
2446 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
2447 obj->oo_dt.do_body_ops = &osd_body_ops;
2449 LASSERT(ergo(result == 0,
2450 dt_object_exists(dt) && !dt_object_remote(dt)));
2452 LASSERT(osd_invariant(obj));
2457 * Called to destroy on-disk representation of the object
2459 * Concurrency: must be locked
2461 static int osd_declare_object_destroy(const struct lu_env *env,
2462 struct dt_object *dt,
2465 struct osd_object *obj = osd_dt_obj(dt);
2466 struct inode *inode = obj->oo_inode;
2467 struct osd_thandle *oh;
2474 oh = container_of0(th, struct osd_thandle, ot_super);
2475 LASSERT(oh->ot_handle == NULL);
2477 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2478 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2479 /* Recycle idle OI leaf may cause additional three OI blocks
2481 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2482 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2483 /* one less inode */
2484 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2485 -1, oh, obj, false, NULL, false);
2488 /* data to be truncated */
2489 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2490 0, oh, obj, true, NULL, false);
2494 static int osd_object_destroy(const struct lu_env *env,
2495 struct dt_object *dt,
2498 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2499 struct osd_object *obj = osd_dt_obj(dt);
2500 struct inode *inode = obj->oo_inode;
2501 struct osd_device *osd = osd_obj2dev(obj);
2502 struct osd_thandle *oh;
2506 oh = container_of0(th, struct osd_thandle, ot_super);
2507 LASSERT(oh->ot_handle);
2509 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2511 if (unlikely(fid_is_acct(fid)))
2514 if (S_ISDIR(inode->i_mode)) {
2515 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1 ||
2516 inode->i_nlink == 2);
2517 /* it will check/delete the inode from remote parent,
2518 * how to optimize it? unlink performance impaction XXX */
2519 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2520 if (result != 0 && result != -ENOENT) {
2521 CERROR("%s: delete inode "DFID": rc = %d\n",
2522 osd_name(osd), PFID(fid), result);
2524 spin_lock(&obj->oo_guard);
2526 spin_unlock(&obj->oo_guard);
2527 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2530 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2532 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_DESTROY);
2533 result = osd_oi_delete(osd_oti_get(env), osd, fid, oh->ot_handle,
2536 osd_trans_exec_check(env, th, OSD_OT_DESTROY);
2537 /* XXX: add to ext3 orphan list */
2538 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2540 /* not needed in the cache anymore */
2541 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2542 obj->oo_destroyed = 1;
2548 * Put the fid into lustre_mdt_attrs, and then place the structure
2549 * inode's ea. This fid should not be altered during the life time
2552 * \retval +ve, on success
2553 * \retval -ve, on error
2555 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2557 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2558 const struct lu_fid *fid, __u32 compat, __u32 incompat)
2560 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2564 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2567 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_OST_EA_FID_SET))
2570 lustre_lma_init(lma, fid, compat, incompat);
2571 lustre_lma_swab(lma);
2573 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2575 /* LMA may already exist, but we need to check that all the
2576 * desired compat/incompat flags have been added. */
2577 if (unlikely(rc == -EEXIST)) {
2578 if (compat == 0 && incompat == 0)
2581 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2582 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2587 lustre_lma_swab(lma);
2588 if (!(~lma->lma_compat & compat) &&
2589 !(~lma->lma_incompat & incompat))
2592 lma->lma_compat |= compat;
2593 lma->lma_incompat |= incompat;
2594 lustre_lma_swab(lma);
2595 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2596 sizeof(*lma), XATTR_REPLACE);
2603 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2604 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2605 * To have compatilibility with 1.8 ldiskfs driver we need to have
2606 * magic number at start of fid data.
2607 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2610 static void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2611 const struct lu_fid *fid)
2613 if (!fid_is_namespace_visible(fid) ||
2614 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2615 param->edp_magic = 0;
2619 param->edp_magic = LDISKFS_LUFID_MAGIC;
2620 param->edp_len = sizeof(struct lu_fid) + 1;
2621 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2625 * Try to read the fid from inode ea into dt_rec.
2627 * \param fid object fid.
2629 * \retval 0 on success
2631 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2632 __u32 ino, struct lu_fid *fid,
2633 struct osd_inode_id *id)
2635 struct osd_thread_info *info = osd_oti_get(env);
2636 struct inode *inode;
2639 osd_id_gen(id, ino, OSD_OII_NOGEN);
2640 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2642 RETURN(PTR_ERR(inode));
2648 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2650 struct inode *parent_dir,
2651 const struct lu_fid *dot_fid,
2652 const struct lu_fid *dot_dot_fid,
2653 struct osd_thandle *oth)
2655 struct ldiskfs_dentry_param *dot_ldp;
2656 struct ldiskfs_dentry_param *dot_dot_ldp;
2657 __u32 saved_nlink = dir->i_nlink;
2660 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2661 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2663 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2664 dot_ldp->edp_magic = 0;
2666 rc = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2667 dir, dot_ldp, dot_dot_ldp);
2668 /* The ldiskfs_add_dot_dotdot() may dir->i_nlink as 2, then
2669 * the subseqent ref_add() will increase the dir->i_nlink
2670 * as 3. That is incorrect for new created directory.
2672 * It looks like hack, because we want to make the OSD API
2673 * to be order-independent for new created directory object
2674 * between dt_insert(..) and ref_add() operations.
2676 * Here, we only restore the in-RAM dir-inode's nlink attr,
2677 * becuase if the nlink attr is not 2, then there will be
2678 * ref_add() called following the dt_insert(..), such call
2679 * will make both the in-RAM and on-disk dir-inode's nlink
2680 * attr to be set as 2. LU-7447 */
2681 set_nlink(dir, saved_nlink);
2686 * Create an local agent inode for remote entry
2688 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2689 struct osd_device *osd,
2690 struct osd_object *pobj,
2691 const struct lu_fid *fid,
2695 struct osd_thread_info *info = osd_oti_get(env);
2696 struct inode *local;
2697 struct osd_thandle *oh;
2702 oh = container_of(th, struct osd_thandle, ot_super);
2703 LASSERT(oh->ot_handle->h_transaction != NULL);
2705 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, type);
2706 if (IS_ERR(local)) {
2707 CERROR("%s: create local error %d\n", osd_name(osd),
2708 (int)PTR_ERR(local));
2712 ldiskfs_set_inode_state(local, LDISKFS_STATE_LUSTRE_NOSCRUB);
2713 unlock_new_inode(local);
2715 /* Set special LMA flag for local agent inode */
2716 rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2718 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2719 osd_name(osd), PFID(fid), rc);
2720 RETURN(ERR_PTR(rc));
2726 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2727 lu_object_fid(&pobj->oo_dt.do_lu),
2730 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2731 osd_name(osd), PFID(fid), rc);
2732 RETURN(ERR_PTR(rc));
2739 * when direntry is deleted, we have to take care of possible agent inode
2740 * referenced by that. unfortunately we can't do this at that point:
2741 * iget() within a running transaction leads to deadlock and we better do
2742 * not call that every delete declaration to save performance. so we put
2743 * a potention agent inode on a list and process that once the transaction
2744 * is over. Notice it's not any worse in terms of real orphans as regular
2745 * object destroy doesn't put inodes on the on-disk orphan list. this should
2746 * be addressed separately
2748 static int osd_schedule_agent_inode_removal(const struct lu_env *env,
2749 struct osd_thandle *oh,
2752 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
2753 struct osd_obj_orphan *oor;
2760 oor->oor_env = (struct lu_env *)env;
2761 spin_lock(&osd->od_osfs_lock);
2762 list_add(&oor->oor_list, &osd->od_orphan_list);
2763 spin_unlock(&osd->od_osfs_lock);
2765 oh->ot_remove_agents = 1;
2771 static int osd_process_scheduled_agent_removals(const struct lu_env *env,
2772 struct osd_device *osd)
2774 struct osd_thread_info *info = osd_oti_get(env);
2775 struct osd_obj_orphan *oor, *tmp;
2776 struct osd_inode_id id;
2777 struct list_head list;
2778 struct inode *inode;
2783 INIT_LIST_HEAD(&list);
2785 spin_lock(&osd->od_osfs_lock);
2786 list_for_each_entry_safe(oor, tmp, &osd->od_orphan_list, oor_list) {
2787 if (oor->oor_env == env) {
2788 list_del(&oor->oor_list);
2789 list_add(&oor->oor_list, &list);
2792 spin_unlock(&osd->od_osfs_lock);
2794 list_for_each_entry_safe(oor, tmp, &list, oor_list) {
2798 list_del(&oor->oor_list);
2801 osd_id_gen(&id, ino, OSD_OII_NOGEN);
2802 inode = osd_iget_fid(info, osd, &id, &fid);
2806 if (!osd_remote_fid(env, osd, &fid)) {
2811 jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC, 1);
2813 mark_inode_dirty(inode);
2814 ldiskfs_journal_stop(jh);
2822 * OSD layer object create function for interoperability mode (b11826).
2823 * This is mostly similar to osd_object_create(). Only difference being, fid is
2824 * inserted into inode ea here.
2826 * \retval 0, on success
2827 * \retval -ve, on error
2829 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2830 struct lu_attr *attr,
2831 struct dt_allocation_hint *hint,
2832 struct dt_object_format *dof,
2835 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2836 struct osd_object *obj = osd_dt_obj(dt);
2837 struct osd_thread_info *info = osd_oti_get(env);
2842 if (dt_object_exists(dt))
2845 LASSERT(osd_invariant(obj));
2846 LASSERT(!dt_object_remote(dt));
2847 LASSERT(osd_write_locked(env, obj));
2848 LASSERT(th != NULL);
2850 if (unlikely(fid_is_acct(fid)))
2851 /* Quota files can't be created from the kernel any more,
2852 * 'tune2fs -O quota' will take care of creating them */
2855 result = __osd_object_create(info, obj, attr, hint, dof, th);
2857 if (fid_is_idif(fid) &&
2858 !osd_dev(dt->do_lu.lo_dev)->od_index_in_idif) {
2859 struct lu_fid *tfid = &info->oti_fid;
2860 struct ost_id *oi = &info->oti_ostid;
2862 fid_to_ostid(fid, oi);
2863 ostid_to_fid(tfid, oi, 0);
2864 result = osd_ea_fid_set(info, obj->oo_inode, tfid,
2865 LMAC_FID_ON_OST, 0);
2867 result = osd_ea_fid_set(info, obj->oo_inode, fid,
2868 fid_is_on_ost(info, osd_obj2dev(obj),
2869 fid, OI_CHECK_FLD) ?
2870 LMAC_FID_ON_OST : 0, 0);
2872 if (obj->oo_dt.do_body_ops == &osd_body_ops_new)
2873 obj->oo_dt.do_body_ops = &osd_body_ops;
2877 result = __osd_oi_insert(env, obj, fid, th);
2879 LASSERT(ergo(result == 0,
2880 dt_object_exists(dt) && !dt_object_remote(dt)));
2881 LINVRNT(osd_invariant(obj));
2885 static int osd_declare_object_ref_add(const struct lu_env *env,
2886 struct dt_object *dt,
2887 struct thandle *handle)
2889 struct osd_thandle *oh;
2891 /* it's possible that object doesn't exist yet */
2892 LASSERT(handle != NULL);
2894 oh = container_of0(handle, struct osd_thandle, ot_super);
2895 LASSERT(oh->ot_handle == NULL);
2897 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2898 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2904 * Concurrency: @dt is write locked.
2906 static int osd_object_ref_add(const struct lu_env *env,
2907 struct dt_object *dt, struct thandle *th)
2909 struct osd_object *obj = osd_dt_obj(dt);
2910 struct inode *inode = obj->oo_inode;
2911 struct osd_thandle *oh;
2914 if (!dt_object_exists(dt))
2917 LINVRNT(osd_invariant(obj));
2918 LASSERT(!dt_object_remote(dt));
2919 LASSERT(osd_write_locked(env, obj));
2920 LASSERT(th != NULL);
2922 oh = container_of0(th, struct osd_thandle, ot_super);
2923 LASSERT(oh->ot_handle != NULL);
2925 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2927 CDEBUG(D_INODE, DFID" increase nlink %d\n",
2928 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
2930 * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
2931 * (65000) subdirectories by storing "1" in i_nlink if the link count
2932 * would otherwise overflow. Directory tranversal tools understand
2933 * that (st_nlink == 1) indicates that the filesystem dose not track
2934 * hard links count on the directory, and will not abort subdirectory
2935 * scanning early once (st_nlink - 2) subdirs have been found.
2937 * This also has to properly handle the case of inodes with nlink == 0
2938 * in case they are being linked into the PENDING directory
2940 spin_lock(&obj->oo_guard);
2941 if (unlikely(inode->i_nlink == 0))
2942 /* inc_nlink from 0 may cause WARN_ON */
2943 set_nlink(inode, 1);
2945 ldiskfs_inc_count(oh->ot_handle, inode);
2946 if (!S_ISDIR(inode->i_mode))
2947 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2949 spin_unlock(&obj->oo_guard);
2951 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2952 LINVRNT(osd_invariant(obj));
2954 osd_trans_exec_check(env, th, OSD_OT_REF_ADD);
2959 static int osd_declare_object_ref_del(const struct lu_env *env,
2960 struct dt_object *dt,
2961 struct thandle *handle)
2963 struct osd_thandle *oh;
2965 if (!dt_object_exists(dt))
2968 LASSERT(!dt_object_remote(dt));
2969 LASSERT(handle != NULL);
2971 oh = container_of0(handle, struct osd_thandle, ot_super);
2972 LASSERT(oh->ot_handle == NULL);
2974 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2975 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2981 * Concurrency: @dt is write locked.
2983 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2986 struct osd_object *obj = osd_dt_obj(dt);
2987 struct inode *inode = obj->oo_inode;
2988 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
2989 struct osd_thandle *oh;
2991 if (!dt_object_exists(dt))
2994 LINVRNT(osd_invariant(obj));
2995 LASSERT(!dt_object_remote(dt));
2996 LASSERT(osd_write_locked(env, obj));
2997 LASSERT(th != NULL);
2999 oh = container_of0(th, struct osd_thandle, ot_super);
3000 LASSERT(oh->ot_handle != NULL);
3002 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
3004 spin_lock(&obj->oo_guard);
3005 /* That can be result of upgrade from old Lustre version and
3006 * applied only to local files. Just skip this ref_del call.
3007 * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
3008 if (inode->i_nlink == 0) {
3009 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
3010 D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
3011 ", maybe an upgraded file? (LU-3915)\n",
3012 osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
3013 spin_unlock(&obj->oo_guard);
3017 CDEBUG(D_INODE, DFID" decrease nlink %d\n",
3018 PFID(lu_object_fid(&dt->do_lu)), inode->i_nlink);
3020 ldiskfs_dec_count(oh->ot_handle, inode);
3021 spin_unlock(&obj->oo_guard);
3023 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3024 LINVRNT(osd_invariant(obj));
3026 osd_trans_exec_check(env, th, OSD_OT_REF_DEL);
3032 * Get the 64-bit version for an inode.
3034 static int osd_object_version_get(const struct lu_env *env,
3035 struct dt_object *dt, dt_obj_version_t *ver)
3037 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3039 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
3040 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3041 *ver = LDISKFS_I(inode)->i_fs_version;
3046 * Concurrency: @dt is read locked.
3048 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
3049 struct lu_buf *buf, const char *name)
3051 struct osd_object *obj = osd_dt_obj(dt);
3052 struct inode *inode = obj->oo_inode;
3053 struct osd_thread_info *info = osd_oti_get(env);
3054 struct dentry *dentry = &info->oti_obj_dentry;
3056 /* version get is not real XATTR but uses xattr API */
3057 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3058 /* for version we are just using xattr API but change inode
3060 if (buf->lb_len == 0)
3061 return sizeof(dt_obj_version_t);
3063 if (buf->lb_len < sizeof(dt_obj_version_t))
3066 osd_object_version_get(env, dt, buf->lb_buf);
3068 return sizeof(dt_obj_version_t);
3071 if (!dt_object_exists(dt))
3074 LASSERT(!dt_object_remote(dt));
3075 LASSERT(inode->i_op != NULL);
3076 LASSERT(inode->i_op->getxattr != NULL);
3078 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
3082 static int osd_declare_xattr_set(const struct lu_env *env,
3083 struct dt_object *dt,
3084 const struct lu_buf *buf, const char *name,
3085 int fl, struct thandle *handle)
3087 struct osd_thandle *oh;
3089 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3091 LASSERT(handle != NULL);
3093 oh = container_of0(handle, struct osd_thandle, ot_super);
3094 LASSERT(oh->ot_handle == NULL);
3096 if (strcmp(name, XATTR_NAME_LMA) == 0) {
3097 /* For non-upgrading case, the LMA is set first and
3098 * usually fit inode. But for upgrade case, the LMA
3099 * may be in another separated EA block. */
3100 if (!dt_object_exists(dt))
3102 else if (fl == LU_XATTR_REPLACE)
3106 } else if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3110 credits = osd_dto_credits_noquota[DTO_XATTR_SET];
3115 if (buf->lb_buf == NULL && dt_object_exists(dt)) {
3116 /* learn xattr size from osd_xattr_get if
3117 attribute has not been read yet */
3118 buflen = __osd_xattr_get(
3119 osd_dt_obj(dt)->oo_inode,
3120 &osd_oti_get(env)->oti_obj_dentry,
3125 buflen = buf->lb_len;
3128 if (buflen > sb->s_blocksize) {
3129 credits += osd_calc_bkmap_credits(
3131 (buflen + sb->s_blocksize - 1) >>
3132 sb->s_blocksize_bits);
3136 * xattr set may involve inode quota change, reserve credits for
3137 * dquot_initialize()
3139 credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3142 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET, credits);
3148 * Set the 64-bit version for object
3150 static void osd_object_version_set(const struct lu_env *env,
3151 struct dt_object *dt,
3152 dt_obj_version_t *new_version)
3154 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3156 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
3157 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
3159 LDISKFS_I(inode)->i_fs_version = *new_version;
3160 /** Version is set after all inode operations are finished,
3161 * so we should mark it dirty here */
3162 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
3166 * Concurrency: @dt is write locked.
3168 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
3169 const struct lu_buf *buf, const char *name, int fl,
3170 struct thandle *handle)
3172 struct osd_object *obj = osd_dt_obj(dt);
3173 struct inode *inode = obj->oo_inode;
3174 struct osd_thread_info *info = osd_oti_get(env);
3179 LASSERT(handle != NULL);
3181 /* version set is not real XATTR */
3182 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
3183 /* for version we are just using xattr API but change inode
3185 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
3186 osd_object_version_set(env, dt, buf->lb_buf);
3187 return sizeof(dt_obj_version_t);
3190 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zu\n",
3191 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
3193 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3194 if (fl & LU_XATTR_REPLACE)
3195 fs_flags |= XATTR_REPLACE;
3197 if (fl & LU_XATTR_CREATE)
3198 fs_flags |= XATTR_CREATE;
3200 if (strcmp(name, XATTR_NAME_LMV) == 0) {
3201 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
3204 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
3208 lma->lma_incompat |= LMAI_STRIPED;
3209 lustre_lma_swab(lma);
3210 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
3211 sizeof(*lma), XATTR_REPLACE);
3216 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
3217 strcmp(name, XATTR_NAME_LINK) == 0)
3220 rc = __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
3222 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3228 * Concurrency: @dt is read locked.
3230 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
3231 const struct lu_buf *buf)
3233 struct osd_object *obj = osd_dt_obj(dt);
3234 struct inode *inode = obj->oo_inode;
3235 struct osd_thread_info *info = osd_oti_get(env);
3236 struct dentry *dentry = &info->oti_obj_dentry;
3238 if (!dt_object_exists(dt))
3241 LASSERT(!dt_object_remote(dt));
3242 LASSERT(inode->i_op != NULL);
3243 LASSERT(inode->i_op->listxattr != NULL);
3245 dentry->d_inode = inode;
3246 dentry->d_sb = inode->i_sb;
3247 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
3250 static int osd_declare_xattr_del(const struct lu_env *env,
3251 struct dt_object *dt, const char *name,
3252 struct thandle *handle)
3254 struct osd_thandle *oh;
3255 struct super_block *sb = osd_sb(osd_dev(dt->do_lu.lo_dev));
3257 LASSERT(!dt_object_remote(dt));
3258 LASSERT(handle != NULL);
3260 oh = container_of0(handle, struct osd_thandle, ot_super);
3261 LASSERT(oh->ot_handle == NULL);
3263 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
3264 osd_dto_credits_noquota[DTO_XATTR_SET]);
3266 * xattr del may involve inode quota change, reserve credits for
3267 * dquot_initialize()
3269 oh->ot_credits += LDISKFS_MAXQUOTAS_INIT_BLOCKS(sb);
3275 * Concurrency: @dt is write locked.
3277 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
3278 const char *name, struct thandle *handle)
3280 struct osd_object *obj = osd_dt_obj(dt);
3281 struct inode *inode = obj->oo_inode;
3282 struct osd_thread_info *info = osd_oti_get(env);
3283 struct dentry *dentry = &info->oti_obj_dentry;
3286 if (!dt_object_exists(dt))
3289 LASSERT(!dt_object_remote(dt));
3290 LASSERT(inode->i_op != NULL);
3291 LASSERT(inode->i_op->removexattr != NULL);
3292 LASSERT(handle != NULL);
3294 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
3296 ll_vfs_dq_init(inode);
3297 dentry->d_inode = inode;
3298 dentry->d_sb = inode->i_sb;
3299 rc = inode->i_op->removexattr(dentry, name);
3300 osd_trans_exec_check(env, handle, OSD_OT_XATTR_SET);
3304 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
3305 __u64 start, __u64 end)
3307 struct osd_object *obj = osd_dt_obj(dt);
3308 struct inode *inode = obj->oo_inode;
3309 struct osd_thread_info *info = osd_oti_get(env);
3310 struct dentry *dentry = &info->oti_obj_dentry;
3311 struct file *file = &info->oti_file;
3316 dentry->d_inode = inode;
3317 dentry->d_sb = inode->i_sb;
3318 file->f_path.dentry = dentry;
3319 file->f_mapping = inode->i_mapping;
3320 file->f_op = inode->i_fop;
3321 set_file_inode(file, inode);
3323 rc = ll_vfs_fsync_range(file, start, end, 0);
3332 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3333 const struct dt_index_features *feat)
3335 struct iam_descr *descr;
3337 if (osd_object_is_root(o))
3338 return feat == &dt_directory_features;
3340 LASSERT(o->oo_dir != NULL);
3342 descr = o->oo_dir->od_container.ic_descr;
3343 if (feat == &dt_directory_features) {
3344 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3350 feat->dif_keysize_min <= descr->id_key_size &&
3351 descr->id_key_size <= feat->dif_keysize_max &&
3352 feat->dif_recsize_min <= descr->id_rec_size &&
3353 descr->id_rec_size <= feat->dif_recsize_max &&
3354 !(feat->dif_flags & (DT_IND_VARKEY |
3355 DT_IND_VARREC | DT_IND_NONUNQ)) &&
3356 ergo(feat->dif_flags & DT_IND_UPDATE,
3357 1 /* XXX check that object (and file system) is
3362 static int osd_iam_container_init(const struct lu_env *env,
3363 struct osd_object *obj,
3364 struct osd_directory *dir)
3366 struct iam_container *bag = &dir->od_container;
3369 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3373 result = iam_container_setup(bag);
3375 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3377 iam_container_fini(bag);
3384 * Concurrency: no external locking is necessary.
3386 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3387 const struct dt_index_features *feat)
3391 struct osd_object *obj = osd_dt_obj(dt);
3393 LINVRNT(osd_invariant(obj));
3395 if (osd_object_is_root(obj)) {
3396 dt->do_index_ops = &osd_index_ea_ops;
3398 } else if (feat == &dt_directory_features) {
3399 dt->do_index_ops = &osd_index_ea_ops;
3400 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
3405 } else if (unlikely(feat == &dt_otable_features)) {
3406 dt->do_index_ops = &osd_otable_ops;
3408 } else if (unlikely(feat == &dt_acct_features)) {
3409 dt->do_index_ops = &osd_acct_index_ops;
3412 } else if (!osd_has_index(obj)) {
3413 struct osd_directory *dir;
3418 spin_lock(&obj->oo_guard);
3419 if (obj->oo_dir == NULL)
3423 * Concurrent thread allocated container data.
3426 spin_unlock(&obj->oo_guard);
3428 * Now, that we have container data, serialize its
3431 down_write(&obj->oo_ext_idx_sem);
3433 * recheck under lock.
3435 if (!osd_has_index(obj))
3436 result = osd_iam_container_init(env, obj,
3440 up_write(&obj->oo_ext_idx_sem);
3448 if (result == 0 && skip_iam == 0) {
3449 if (!osd_iam_index_probe(env, obj, feat))
3452 LINVRNT(osd_invariant(obj));
3454 if (result == 0 && feat == &dt_quota_glb_features &&
3455 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3456 result = osd_quota_migration(env, dt);
3461 static int osd_otable_it_attr_get(const struct lu_env *env,
3462 struct dt_object *dt,
3463 struct lu_attr *attr)
3469 static const struct dt_object_operations osd_obj_ops = {
3470 .do_read_lock = osd_object_read_lock,
3471 .do_write_lock = osd_object_write_lock,
3472 .do_read_unlock = osd_object_read_unlock,
3473 .do_write_unlock = osd_object_write_unlock,
3474 .do_write_locked = osd_object_write_locked,
3475 .do_attr_get = osd_attr_get,
3476 .do_declare_attr_set = osd_declare_attr_set,
3477 .do_attr_set = osd_attr_set,
3478 .do_ah_init = osd_ah_init,
3479 .do_declare_create = osd_declare_object_create,
3480 .do_create = osd_object_create,
3481 .do_declare_destroy = osd_declare_object_destroy,
3482 .do_destroy = osd_object_destroy,
3483 .do_index_try = osd_index_try,
3484 .do_declare_ref_add = osd_declare_object_ref_add,
3485 .do_ref_add = osd_object_ref_add,
3486 .do_declare_ref_del = osd_declare_object_ref_del,
3487 .do_ref_del = osd_object_ref_del,
3488 .do_xattr_get = osd_xattr_get,
3489 .do_declare_xattr_set = osd_declare_xattr_set,
3490 .do_xattr_set = osd_xattr_set,
3491 .do_declare_xattr_del = osd_declare_xattr_del,
3492 .do_xattr_del = osd_xattr_del,
3493 .do_xattr_list = osd_xattr_list,
3494 .do_object_sync = osd_object_sync,
3498 * dt_object_operations for interoperability mode
3499 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3501 static const struct dt_object_operations osd_obj_ea_ops = {
3502 .do_read_lock = osd_object_read_lock,
3503 .do_write_lock = osd_object_write_lock,
3504 .do_read_unlock = osd_object_read_unlock,
3505 .do_write_unlock = osd_object_write_unlock,
3506 .do_write_locked = osd_object_write_locked,
3507 .do_attr_get = osd_attr_get,
3508 .do_declare_attr_set = osd_declare_attr_set,
3509 .do_attr_set = osd_attr_set,
3510 .do_ah_init = osd_ah_init,
3511 .do_declare_create = osd_declare_object_create,
3512 .do_create = osd_object_ea_create,
3513 .do_declare_destroy = osd_declare_object_destroy,
3514 .do_destroy = osd_object_destroy,
3515 .do_index_try = osd_index_try,
3516 .do_declare_ref_add = osd_declare_object_ref_add,
3517 .do_ref_add = osd_object_ref_add,
3518 .do_declare_ref_del = osd_declare_object_ref_del,
3519 .do_ref_del = osd_object_ref_del,
3520 .do_xattr_get = osd_xattr_get,
3521 .do_declare_xattr_set = osd_declare_xattr_set,
3522 .do_xattr_set = osd_xattr_set,
3523 .do_declare_xattr_del = osd_declare_xattr_del,
3524 .do_xattr_del = osd_xattr_del,
3525 .do_xattr_list = osd_xattr_list,
3526 .do_object_sync = osd_object_sync,
3529 static const struct dt_object_operations osd_obj_otable_it_ops = {
3530 .do_attr_get = osd_otable_it_attr_get,
3531 .do_index_try = osd_index_try,
3534 static int osd_index_declare_iam_delete(const struct lu_env *env,
3535 struct dt_object *dt,
3536 const struct dt_key *key,
3537 struct thandle *handle)
3539 struct osd_thandle *oh;
3541 oh = container_of0(handle, struct osd_thandle, ot_super);
3542 LASSERT(oh->ot_handle == NULL);
3544 /* Recycle may cause additional three blocks to be changed. */
3545 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3546 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
3552 * delete a (key, value) pair from index \a dt specified by \a key
3554 * \param dt osd index object
3555 * \param key key for index
3556 * \param rec record reference
3557 * \param handle transaction handler
3560 * \retval -ve failure
3562 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3563 const struct dt_key *key,
3564 struct thandle *handle)
3566 struct osd_thread_info *oti = osd_oti_get(env);
3567 struct osd_object *obj = osd_dt_obj(dt);
3568 struct osd_thandle *oh;
3569 struct iam_path_descr *ipd;
3570 struct iam_container *bag = &obj->oo_dir->od_container;
3574 if (!dt_object_exists(dt))
3577 LINVRNT(osd_invariant(obj));
3578 LASSERT(!dt_object_remote(dt));
3579 LASSERT(bag->ic_object == obj->oo_inode);
3580 LASSERT(handle != NULL);
3582 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3584 ipd = osd_idx_ipd_get(env, bag);
3585 if (unlikely(ipd == NULL))
3588 oh = container_of0(handle, struct osd_thandle, ot_super);
3589 LASSERT(oh->ot_handle != NULL);
3590 LASSERT(oh->ot_handle->h_transaction != NULL);
3592 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3593 /* swab quota uid/gid provided by caller */
3594 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3595 key = (const struct dt_key *)&oti->oti_quota_id;
3598 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3599 osd_ipd_put(env, bag, ipd);
3600 LINVRNT(osd_invariant(obj));
3601 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
3605 static int osd_index_declare_ea_delete(const struct lu_env *env,
3606 struct dt_object *dt,
3607 const struct dt_key *key,
3608 struct thandle *handle)
3610 struct osd_thandle *oh;
3611 struct inode *inode;
3615 LASSERT(!dt_object_remote(dt));
3616 LASSERT(handle != NULL);
3618 oh = container_of0(handle, struct osd_thandle, ot_super);
3619 LASSERT(oh->ot_handle == NULL);
3621 /* due to DNE we may need to remove an agent inode */
3622 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3623 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
3624 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
3626 inode = osd_dt_obj(dt)->oo_inode;
3630 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
3631 0, oh, osd_dt_obj(dt), true, NULL, false);
3635 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3638 struct osd_fid_pack *rec;
3641 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3642 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3643 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3644 if (rc == 0 && unlikely(!fid_is_sane((struct lu_fid *)fid)))
3650 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3651 const struct lu_fid *fid)
3653 struct seq_server_site *ss = osd_seq_site(osd);
3656 /* FID seqs not in FLDB, must be local seq */
3657 if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3660 /* If FLD is not being initialized yet, it only happens during the
3661 * initialization, likely during mgs initialization, and we assume
3662 * this is local FID. */
3663 if (ss == NULL || ss->ss_server_fld == NULL)
3666 /* Only check the local FLDB here */
3667 if (osd_seq_exists(env, osd, fid_seq(fid)))
3674 * Index delete function for interoperability mode (b11826).
3675 * It will remove the directory entry added by osd_index_ea_insert().
3676 * This entry is needed to maintain name->fid mapping.
3678 * \param key, key i.e. file entry to be deleted
3680 * \retval 0, on success
3681 * \retval -ve, on error
3683 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3684 const struct dt_key *key, struct thandle *handle)
3686 struct osd_object *obj = osd_dt_obj(dt);
3687 struct inode *dir = obj->oo_inode;
3688 struct dentry *dentry;
3689 struct osd_thandle *oh;
3690 struct ldiskfs_dir_entry_2 *de = NULL;
3691 struct buffer_head *bh;
3692 struct htree_lock *hlock = NULL;
3693 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3694 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3698 if (!dt_object_exists(dt))
3701 LINVRNT(osd_invariant(obj));
3702 LASSERT(!dt_object_remote(dt));
3703 LASSERT(handle != NULL);
3705 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3707 oh = container_of(handle, struct osd_thandle, ot_super);
3708 LASSERT(oh->ot_handle != NULL);
3709 LASSERT(oh->ot_handle->h_transaction != NULL);
3711 ll_vfs_dq_init(dir);
3712 dentry = osd_child_dentry_get(env, obj,
3713 (char *)key, strlen((char *)key));
3715 if (obj->oo_hl_head != NULL) {
3716 hlock = osd_oti_get(env)->oti_hlock;
3717 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3718 dir, LDISKFS_HLOCK_DEL);
3720 down_write(&obj->oo_ext_idx_sem);
3723 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
3725 /* If this is not the ".." entry, it might be a remote DNE
3726 * entry and we need to check if the FID is for a remote
3727 * MDT. If the FID is not in the directory entry (e.g.
3728 * upgraded 1.8 filesystem without dirdata enabled) then
3729 * we need to get the FID from the LMA. For a remote directory
3730 * there HAS to be an LMA, it cannot be an IGIF inode in this
3733 * Delete the entry before the agent inode in order to
3734 * simplify error handling. At worst an error after deleting
3735 * the entry first might leak the agent inode afterward. The
3736 * reverse would need filesystem abort in case of error deleting
3737 * the entry after the agent had been removed, or leave a
3738 * dangling entry pointing at a random inode. */
3739 if (strcmp((char *)key, dotdot) != 0) {
3740 LASSERT(de != NULL);
3741 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3742 if (rc == -ENODATA) {
3743 /* can't get FID, postpone to the end of the
3744 * transaction when iget() is safe */
3745 osd_schedule_agent_inode_removal(env, oh,
3746 le32_to_cpu(de->inode));
3747 } else if (rc == 0 &&
3748 unlikely(osd_remote_fid(env, osd, fid))) {
3749 osd_schedule_agent_inode_removal(env, oh,
3750 le32_to_cpu(de->inode));
3753 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3759 ldiskfs_htree_unlock(hlock);
3761 up_write(&obj->oo_ext_idx_sem);
3766 /* For inode on the remote MDT, .. will point to
3767 * /Agent directory, Check whether it needs to delete
3768 * from agent directory */
3769 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3770 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3772 if (rc != 0 && rc != -ENOENT) {
3773 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3774 osd_name(osd), PFID(fid), rc);
3784 LASSERT(osd_invariant(obj));
3785 osd_trans_exec_check(env, handle, OSD_OT_DELETE);
3790 * Lookup index for \a key and copy record to \a rec.
3792 * \param dt osd index object
3793 * \param key key for index
3794 * \param rec record reference
3796 * \retval +ve success : exact mach
3797 * \retval 0 return record with key not greater than \a key
3798 * \retval -ve failure
3800 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3801 struct dt_rec *rec, const struct dt_key *key)
3803 struct osd_object *obj = osd_dt_obj(dt);
3804 struct iam_path_descr *ipd;
3805 struct iam_container *bag = &obj->oo_dir->od_container;
3806 struct osd_thread_info *oti = osd_oti_get(env);
3807 struct iam_iterator *it = &oti->oti_idx_it;
3808 struct iam_rec *iam_rec;
3812 if (!dt_object_exists(dt))
3815 LASSERT(osd_invariant(obj));
3816 LASSERT(!dt_object_remote(dt));
3817 LASSERT(bag->ic_object == obj->oo_inode);
3819 ipd = osd_idx_ipd_get(env, bag);
3823 /* got ipd now we can start iterator. */
3824 iam_it_init(it, bag, 0, ipd);
3826 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3827 /* swab quota uid/gid provided by caller */
3828 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3829 key = (const struct dt_key *)&oti->oti_quota_id;
3832 rc = iam_it_get(it, (struct iam_key *)key);
3834 if (S_ISDIR(obj->oo_inode->i_mode))
3835 iam_rec = (struct iam_rec *)oti->oti_ldp;
3837 iam_rec = (struct iam_rec *) rec;
3839 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3841 if (S_ISDIR(obj->oo_inode->i_mode))
3842 osd_fid_unpack((struct lu_fid *) rec,
3843 (struct osd_fid_pack *)iam_rec);
3844 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3845 osd_quota_unpack(obj, rec);
3850 osd_ipd_put(env, bag, ipd);
3852 LINVRNT(osd_invariant(obj));
3857 static int osd_index_declare_iam_insert(const struct lu_env *env,
3858 struct dt_object *dt,
3859 const struct dt_rec *rec,
3860 const struct dt_key *key,
3861 struct thandle *handle)
3863 struct osd_thandle *oh;
3865 LASSERT(handle != NULL);
3867 oh = container_of0(handle, struct osd_thandle, ot_super);
3868 LASSERT(oh->ot_handle == NULL);
3870 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3871 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3877 * Inserts (key, value) pair in \a dt index object.
3879 * \param dt osd index object
3880 * \param key key for index
3881 * \param rec record reference
3882 * \param th transaction handler
3885 * \retval -ve failure
3887 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3888 const struct dt_rec *rec,
3889 const struct dt_key *key, struct thandle *th,
3892 struct osd_object *obj = osd_dt_obj(dt);
3893 struct iam_path_descr *ipd;
3894 struct osd_thandle *oh;
3895 struct iam_container *bag;
3896 struct osd_thread_info *oti = osd_oti_get(env);
3897 struct iam_rec *iam_rec;
3901 if (!dt_object_exists(dt))
3904 LINVRNT(osd_invariant(obj));
3905 LASSERT(!dt_object_remote(dt));
3907 bag = &obj->oo_dir->od_container;
3908 LASSERT(bag->ic_object == obj->oo_inode);
3909 LASSERT(th != NULL);
3911 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3913 ipd = osd_idx_ipd_get(env, bag);
3914 if (unlikely(ipd == NULL))
3917 oh = container_of0(th, struct osd_thandle, ot_super);
3918 LASSERT(oh->ot_handle != NULL);
3919 LASSERT(oh->ot_handle->h_transaction != NULL);
3920 if (S_ISDIR(obj->oo_inode->i_mode)) {
3921 iam_rec = (struct iam_rec *)oti->oti_ldp;
3922 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3923 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3924 /* pack quota uid/gid */
3925 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3926 key = (const struct dt_key *)&oti->oti_quota_id;
3927 /* pack quota record */
3928 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3929 iam_rec = (struct iam_rec *)rec;
3931 iam_rec = (struct iam_rec *)rec;
3934 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3936 osd_ipd_put(env, bag, ipd);
3937 LINVRNT(osd_invariant(obj));
3938 osd_trans_exec_check(env, th, OSD_OT_INSERT);
3943 * Calls ldiskfs_add_entry() to add directory entry
3944 * into the directory. This is required for
3945 * interoperability mode (b11826)
3947 * \retval 0, on success
3948 * \retval -ve, on error
3950 static int __osd_ea_add_rec(struct osd_thread_info *info,
3951 struct osd_object *pobj, struct inode *cinode,
3952 const char *name, const struct lu_fid *fid,
3953 struct htree_lock *hlock, struct thandle *th)
3955 struct ldiskfs_dentry_param *ldp;
3956 struct dentry *child;
3957 struct osd_thandle *oth;
3960 oth = container_of(th, struct osd_thandle, ot_super);
3961 LASSERT(oth->ot_handle != NULL);
3962 LASSERT(oth->ot_handle->h_transaction != NULL);
3963 LASSERT(pobj->oo_inode);
3965 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3966 if (unlikely(pobj->oo_inode ==
3967 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3970 osd_get_ldiskfs_dirent_param(ldp, fid);
3971 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3972 child->d_fsdata = (void *)ldp;
3973 ll_vfs_dq_init(pobj->oo_inode);
3974 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3975 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_TYPE)) {
3976 struct ldiskfs_dir_entry_2 *de;
3977 struct buffer_head *bh;
3980 bh = osd_ldiskfs_find_entry(pobj->oo_inode, &child->d_name, &de,
3983 rc1 = ldiskfs_journal_get_write_access(oth->ot_handle,
3986 if (S_ISDIR(cinode->i_mode))
3987 de->file_type = LDISKFS_DIRENT_LUFID |
3988 LDISKFS_FT_REG_FILE;
3990 de->file_type = LDISKFS_DIRENT_LUFID |
3992 ldiskfs_handle_dirty_metadata(oth->ot_handle,
4003 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
4004 * into the directory.Also sets flags into osd object to
4005 * indicate dot and dotdot are created. This is required for
4006 * interoperability mode (b11826)
4008 * \param dir directory for dot and dotdot fixup.
4009 * \param obj child object for linking
4011 * \retval 0, on success
4012 * \retval -ve, on error
4014 static int osd_add_dot_dotdot(struct osd_thread_info *info,
4015 struct osd_object *dir,
4016 struct inode *parent_dir, const char *name,
4017 const struct lu_fid *dot_fid,
4018 const struct lu_fid *dot_dot_fid,
4021 struct inode *inode = dir->oo_inode;
4022 struct osd_thandle *oth;
4025 oth = container_of(th, struct osd_thandle, ot_super);
4026 LASSERT(oth->ot_handle->h_transaction != NULL);
4027 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
4029 if (strcmp(name, dot) == 0) {
4030 if (dir->oo_compat_dot_created) {
4033 LASSERT(inode == parent_dir);
4034 dir->oo_compat_dot_created = 1;
4037 } else if (strcmp(name, dotdot) == 0) {
4038 if (!dir->oo_compat_dot_created)
4040 /* in case of rename, dotdot is already created */
4041 if (dir->oo_compat_dotdot_created) {
4042 return __osd_ea_add_rec(info, dir, parent_dir, name,
4043 dot_dot_fid, NULL, th);
4046 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
4047 struct lu_fid tfid = *dot_dot_fid;
4050 result = osd_add_dot_dotdot_internal(info,
4051 dir->oo_inode, parent_dir, dot_fid,
4054 result = osd_add_dot_dotdot_internal(info,
4055 dir->oo_inode, parent_dir, dot_fid,
4060 dir->oo_compat_dotdot_created = 1;
4068 * It will call the appropriate osd_add* function and return the
4069 * value, return by respective functions.
4071 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
4072 struct inode *cinode, const char *name,
4073 const struct lu_fid *fid, struct thandle *th)
4075 struct osd_thread_info *info = osd_oti_get(env);
4076 struct htree_lock *hlock;
4079 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
4081 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
4083 if (hlock != NULL) {
4084 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4087 down_write(&pobj->oo_ext_idx_sem);
4090 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
4091 lu_object_fid(&pobj->oo_dt.do_lu),
4094 if (hlock != NULL) {
4095 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
4096 pobj->oo_inode, LDISKFS_HLOCK_ADD);
4098 down_write(&pobj->oo_ext_idx_sem);
4101 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
4102 struct lu_fid *tfid = &info->oti_fid;
4106 rc = __osd_ea_add_rec(info, pobj, cinode, name,
4109 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
4114 ldiskfs_htree_unlock(hlock);
4116 up_write(&pobj->oo_ext_idx_sem);
4122 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
4123 struct osd_idmap_cache *oic)
4125 struct osd_scrub *scrub = &dev->od_scrub;
4126 struct lu_fid *fid = &oic->oic_fid;
4127 struct osd_inode_id *id = &oti->oti_id;
4132 if (!fid_is_norm(fid) && !fid_is_igif(fid))
4135 if (scrub->os_pos_current > id->oii_ino)
4139 rc = osd_oi_lookup(oti, dev, fid, id, 0);
4140 if (rc == -ENOENT) {
4141 struct inode *inode;
4144 inode = osd_iget(oti, dev, &oic->oic_lid);
4146 /* The inode has been removed (by race maybe). */
4147 if (IS_ERR(inode)) {
4148 rc = PTR_ERR(inode);
4150 RETURN(rc == -ESTALE ? -ENOENT : rc);
4154 /* The OI mapping is lost. */
4155 if (id->oii_gen != OSD_OII_NOGEN)
4158 /* The inode may has been reused by others, we do not know,
4159 * leave it to be handled by subsequent osd_fid_lookup(). */
4161 } else if (rc != 0 || osd_id_eq(id, &oic->oic_lid)) {
4166 if (thread_is_running(&scrub->os_thread)) {
4167 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
4168 /* There is race condition between osd_oi_lookup and OI scrub.
4169 * The OI scrub finished just after osd_oi_lookup() failure.
4170 * Under such case, it is unnecessary to trigger OI scrub again,
4171 * but try to call osd_oi_lookup() again. */
4172 if (unlikely(rc == -EAGAIN))
4178 if (!dev->od_noscrub && ++once == 1) {
4179 rc = osd_scrub_start(dev, SS_AUTO_PARTIAL | SS_CLEAR_DRYRUN |
4181 CDEBUG(D_LFSCK | D_CONSOLE, "%.16s: trigger OI scrub by RPC "
4182 "for "DFID", rc = %d [2]\n",
4183 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
4185 if (rc == 0 || rc == -EALREADY)
4192 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
4193 struct osd_device *dev,
4194 struct osd_idmap_cache *oic,
4195 struct lu_fid *fid, __u32 ino)
4197 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
4198 struct inode *inode;
4201 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4202 inode = osd_iget(oti, dev, &oic->oic_lid);
4203 if (IS_ERR(inode)) {
4204 fid_zero(&oic->oic_fid);
4205 return PTR_ERR(inode);
4208 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
4211 fid_zero(&oic->oic_fid);
4213 *fid = oic->oic_fid = lma->lma_self_fid;
4217 void osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
4218 struct osd_inode_id *id, const struct lu_fid *fid)
4220 CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
4221 id->oii_ino, id->oii_gen, info);
4222 info->oti_cache.oic_lid = *id;
4223 info->oti_cache.oic_fid = *fid;
4224 info->oti_cache.oic_dev = osd;
4228 * Get parent FID from the linkEA.
4230 * For a directory which parent resides on remote MDT, to satisfy the
4231 * local e2fsck, we insert it into the /REMOTE_PARENT_DIR locally. On
4232 * the other hand, to make the lookup(..) on the directory can return
4233 * the real parent FID, we append the real parent FID after its ".."
4234 * name entry in the /REMOTE_PARENT_DIR.
4236 * Unfortunately, such PFID-in-dirent cannot be preserved via file-level
4237 * backup. So after the restore, we cannot get the right parent FID from
4238 * its ".." name entry in the /REMOTE_PARENT_DIR. Under such case, since
4239 * we have stored the real parent FID in the directory object's linkEA,
4240 * we can parse the linkEA for the real parent FID.
4242 * \param[in] env pointer to the thread context
4243 * \param[in] obj pointer to the object to be handled
4244 * \param[out]fid pointer to the buffer to hold the parent FID
4246 * \retval 0 for getting the real parent FID successfully
4247 * \retval negative error number on failure
4249 static int osd_get_pfid_from_linkea(const struct lu_env *env,
4250 struct osd_object *obj,
4253 struct osd_thread_info *oti = osd_oti_get(env);
4254 struct lu_buf *buf = &oti->oti_big_buf;
4255 struct dentry *dentry = &oti->oti_obj_dentry;
4256 struct inode *inode = obj->oo_inode;
4257 struct linkea_data ldata = { NULL };
4262 if (!S_ISDIR(inode->i_mode))
4266 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4267 buf->lb_buf, buf->lb_len);
4268 if (rc == -ERANGE) {
4269 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LINK,
4272 lu_buf_realloc(buf, rc);
4273 if (buf->lb_buf == NULL)
4280 if (unlikely(rc == 0))
4286 if (unlikely(buf->lb_buf == NULL)) {
4287 lu_buf_realloc(buf, rc);
4288 if (buf->lb_buf == NULL)
4295 rc = linkea_init(&ldata);
4297 linkea_first_entry(&ldata);
4298 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, NULL, fid);
4305 * Calls ->lookup() to find dentry. From dentry get inode and
4306 * read inode's ea to get fid. This is required for interoperability
4309 * \retval 0, on success
4310 * \retval -ve, on error
4312 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
4313 struct dt_rec *rec, const struct dt_key *key)
4315 struct inode *dir = obj->oo_inode;
4316 struct dentry *dentry;
4317 struct ldiskfs_dir_entry_2 *de;
4318 struct buffer_head *bh;
4319 struct lu_fid *fid = (struct lu_fid *) rec;
4320 struct htree_lock *hlock = NULL;
4325 LASSERT(dir->i_op != NULL);
4326 LASSERT(dir->i_op->lookup != NULL);
4328 dentry = osd_child_dentry_get(env, obj,
4329 (char *)key, strlen((char *)key));
4331 if (obj->oo_hl_head != NULL) {
4332 hlock = osd_oti_get(env)->oti_hlock;
4333 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4334 dir, LDISKFS_HLOCK_LOOKUP);
4336 down_read(&obj->oo_ext_idx_sem);
4339 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
4341 struct osd_thread_info *oti = osd_oti_get(env);
4342 struct osd_inode_id *id = &oti->oti_id;
4343 struct osd_idmap_cache *oic = &oti->oti_cache;
4344 struct osd_device *dev = osd_obj2dev(obj);
4346 ino = le32_to_cpu(de->inode);
4347 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
4349 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
4353 rc = osd_get_fid_from_dentry(de, rec);
4355 /* done with de, release bh */
4358 if (unlikely(ino == osd_remote_parent_ino(dev))) {
4359 const char *name = (const char *)key;
4361 /* If the parent is on remote MDT, and there
4362 * is no FID-in-dirent, then we have to get
4363 * the parent FID from the linkEA. */
4364 if (likely(strlen(name) == 2 &&
4365 name[0] == '.' && name[1] == '.'))
4366 rc = osd_get_pfid_from_linkea(env, obj,
4369 rc = osd_ea_fid_get(env, obj, ino, fid, id);
4372 osd_id_gen(id, ino, OSD_OII_NOGEN);
4375 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
4376 fid_zero(&oic->oic_fid);
4381 osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id, fid);
4382 rc = osd_consistency_check(oti, dev, oic);
4384 fid_zero(&oic->oic_fid);
4393 ldiskfs_htree_unlock(hlock);
4395 up_read(&obj->oo_ext_idx_sem);
4400 * Find the osd object for given fid.
4402 * \param fid need to find the osd object having this fid
4404 * \retval osd_object on success
4405 * \retval -ve on error
4407 static struct osd_object *osd_object_find(const struct lu_env *env,
4408 struct dt_object *dt,
4409 const struct lu_fid *fid)
4411 struct lu_device *ludev = dt->do_lu.lo_dev;
4412 struct osd_object *child = NULL;
4413 struct lu_object *luch;
4414 struct lu_object *lo;
4417 * at this point topdev might not exist yet
4418 * (i.e. MGS is preparing profiles). so we can
4419 * not rely on topdev and instead lookup with
4420 * our device passed as topdev. this can't work
4421 * if the object isn't cached yet (as osd doesn't
4422 * allocate lu_header). IOW, the object must be
4423 * in the cache, otherwise lu_object_alloc() crashes
4426 luch = lu_object_find_at(env, ludev->ld_site->ls_top_dev == NULL ?
4427 ludev : ludev->ld_site->ls_top_dev,
4429 if (!IS_ERR(luch)) {
4430 if (lu_object_exists(luch)) {
4431 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
4433 child = osd_obj(lo);
4435 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4436 "lu_object can't be located"
4437 DFID"\n", PFID(fid));
4439 if (child == NULL) {
4440 lu_object_put(env, luch);
4441 CERROR("Unable to get osd_object\n");
4442 child = ERR_PTR(-ENOENT);
4445 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4446 "lu_object does not exists "DFID"\n",
4448 lu_object_put(env, luch);
4449 child = ERR_PTR(-ENOENT);
4452 child = ERR_CAST(luch);
4459 * Put the osd object once done with it.
4461 * \param obj osd object that needs to be put
4463 static inline void osd_object_put(const struct lu_env *env,
4464 struct osd_object *obj)
4466 lu_object_put(env, &obj->oo_dt.do_lu);
4469 static int osd_index_declare_ea_insert(const struct lu_env *env,
4470 struct dt_object *dt,
4471 const struct dt_rec *rec,
4472 const struct dt_key *key,
4473 struct thandle *handle)
4475 struct osd_thandle *oh;
4476 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4477 struct lu_fid *fid = (struct lu_fid *)rec;
4478 int credits, rc = 0;
4481 LASSERT(!dt_object_remote(dt));
4482 LASSERT(handle != NULL);
4484 oh = container_of0(handle, struct osd_thandle, ot_super);
4485 LASSERT(oh->ot_handle == NULL);
4487 credits = osd_dto_credits_noquota[DTO_INDEX_INSERT];
4489 rc = osd_remote_fid(env, osd, fid);
4490 if (unlikely(rc < 0))
4493 /* a reference to remote inode is represented by an
4494 * agent inode which we have to create */
4495 credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
4496 credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
4501 osd_trans_declare_op(env, oh, OSD_OT_INSERT, credits);
4503 if (osd_dt_obj(dt)->oo_inode != NULL) {
4504 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4506 /* We ignore block quota on meta pool (MDTs), so needn't
4507 * calculate how many blocks will be consumed by this index
4509 rc = osd_declare_inode_qid(env, i_uid_read(inode),
4510 i_gid_read(inode), 0, oh,
4511 osd_dt_obj(dt), true, NULL, false);
4518 * Index add function for interoperability mode (b11826).
4519 * It will add the directory entry.This entry is needed to
4520 * maintain name->fid mapping.
4522 * \param key it is key i.e. file entry to be inserted
4523 * \param rec it is value of given key i.e. fid
4525 * \retval 0, on success
4526 * \retval -ve, on error
4528 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4529 const struct dt_rec *rec,
4530 const struct dt_key *key, struct thandle *th,
4533 struct osd_object *obj = osd_dt_obj(dt);
4534 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
4535 struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
4536 const struct lu_fid *fid = rec1->rec_fid;
4537 const char *name = (const char *)key;
4538 struct osd_thread_info *oti = osd_oti_get(env);
4539 struct osd_inode_id *id = &oti->oti_id;
4540 struct inode *child_inode = NULL;
4541 struct osd_object *child = NULL;
4545 if (!dt_object_exists(dt))
4548 LASSERT(osd_invariant(obj));
4549 LASSERT(!dt_object_remote(dt));
4550 LASSERT(th != NULL);
4552 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4554 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!\n", PFID(fid));
4556 rc = osd_remote_fid(env, osd, fid);
4558 CERROR("%s: Can not find object "DFID" rc %d\n",
4559 osd_name(osd), PFID(fid), rc);
4564 /* Insert remote entry */
4565 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4566 struct osd_mdobj_map *omm = osd->od_mdt_map;
4567 struct osd_thandle *oh;
4569 /* If parent on remote MDT, we need put this object
4571 oh = container_of(th, typeof(*oh), ot_super);
4572 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4574 CERROR("%s: add "DFID" error: rc = %d\n",
4576 PFID(lu_object_fid(&dt->do_lu)), rc);
4580 child_inode = igrab(omm->omm_remote_parent->d_inode);
4582 child_inode = osd_create_local_agent_inode(env, osd,
4583 obj, fid, rec1->rec_type & S_IFMT, th);
4584 if (IS_ERR(child_inode))
4585 RETURN(PTR_ERR(child_inode));
4588 /* Insert local entry */
4589 child = osd_object_find(env, dt, fid);
4590 if (IS_ERR(child)) {
4591 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4592 osd_name(osd), PFID(fid),
4593 id->oii_ino, id->oii_gen,
4594 (int)PTR_ERR(child));
4595 RETURN(PTR_ERR(child));
4597 child_inode = igrab(child->oo_inode);
4600 rc = osd_ea_add_rec(env, obj, child_inode, name, fid, th);
4602 CDEBUG(D_INODE, "parent %lu insert %s:%lu rc = %d\n",
4603 obj->oo_inode->i_ino, name, child_inode->i_ino, rc);
4607 osd_object_put(env, child);
4608 LASSERT(osd_invariant(obj));
4609 osd_trans_exec_check(env, th, OSD_OT_INSERT);
4614 * Initialize osd Iterator for given osd index object.
4616 * \param dt osd index object
4619 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4620 struct dt_object *dt,
4623 struct osd_it_iam *it;
4624 struct osd_object *obj = osd_dt_obj(dt);
4625 struct lu_object *lo = &dt->do_lu;
4626 struct iam_path_descr *ipd;
4627 struct iam_container *bag = &obj->oo_dir->od_container;
4629 if (!dt_object_exists(dt))
4630 return ERR_PTR(-ENOENT);
4634 return ERR_PTR(-ENOMEM);
4636 ipd = osd_it_ipd_get(env, bag);
4637 if (likely(ipd != NULL)) {
4641 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4642 return (struct dt_it *)it;
4645 return ERR_PTR(-ENOMEM);
4650 * free given Iterator.
4653 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4655 struct osd_it_iam *it = (struct osd_it_iam *)di;
4656 struct osd_object *obj = it->oi_obj;
4658 iam_it_fini(&it->oi_it);
4659 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4660 lu_object_put(env, &obj->oo_dt.do_lu);
4665 * Move Iterator to record specified by \a key
4667 * \param di osd iterator
4668 * \param key key for index
4670 * \retval +ve di points to record with least key not larger than key
4671 * \retval 0 di points to exact matched key
4672 * \retval -ve failure
4675 static int osd_it_iam_get(const struct lu_env *env,
4676 struct dt_it *di, const struct dt_key *key)
4678 struct osd_thread_info *oti = osd_oti_get(env);
4679 struct osd_it_iam *it = (struct osd_it_iam *)di;
4681 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4682 /* swab quota uid/gid */
4683 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4684 key = (struct dt_key *)&oti->oti_quota_id;
4687 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4693 * \param di osd iterator
4695 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4697 struct osd_it_iam *it = (struct osd_it_iam *)di;
4699 iam_it_put(&it->oi_it);
4703 * Move iterator by one record
4705 * \param di osd iterator
4707 * \retval +1 end of container reached
4709 * \retval -ve failure
4712 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4714 struct osd_it_iam *it = (struct osd_it_iam *)di;
4716 return iam_it_next(&it->oi_it);
4720 * Return pointer to the key under iterator.
4723 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4724 const struct dt_it *di)
4726 struct osd_thread_info *oti = osd_oti_get(env);
4727 struct osd_it_iam *it = (struct osd_it_iam *)di;
4728 struct osd_object *obj = it->oi_obj;
4731 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4733 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4734 /* swab quota uid/gid */
4735 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4736 key = (struct dt_key *)&oti->oti_quota_id;
4743 * Return size of key under iterator (in bytes)
4746 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4748 struct osd_it_iam *it = (struct osd_it_iam *)di;
4750 return iam_it_key_size(&it->oi_it);
4754 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4756 /* check if file type is required */
4757 if (ent->lde_attrs & LUDA_TYPE) {
4758 struct luda_type *lt;
4759 int align = sizeof(*lt) - 1;
4761 len = (len + align) & ~align;
4762 lt = (struct luda_type *)(ent->lde_name + len);
4763 lt->lt_type = cpu_to_le16(DTTOIF(type));
4766 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4770 * build lu direct from backend fs dirent.
4774 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4775 char *name, __u16 namelen, __u16 type, __u32 attr)
4777 ent->lde_attrs = attr | LUDA_FID;
4778 fid_cpu_to_le(&ent->lde_fid, fid);
4780 ent->lde_hash = cpu_to_le64(offset);
4781 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4783 strncpy(ent->lde_name, name, namelen);
4784 ent->lde_name[namelen] = '\0';
4785 ent->lde_namelen = cpu_to_le16(namelen);
4787 /* append lustre attributes */
4788 osd_it_append_attrs(ent, namelen, type);
4792 * Return pointer to the record under iterator.
4794 static int osd_it_iam_rec(const struct lu_env *env,
4795 const struct dt_it *di,
4796 struct dt_rec *dtrec, __u32 attr)
4798 struct osd_it_iam *it = (struct osd_it_iam *)di;
4799 struct osd_thread_info *info = osd_oti_get(env);
4802 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4803 const struct osd_fid_pack *rec;
4804 struct lu_fid *fid = &info->oti_fid;
4805 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4811 name = (char *)iam_it_key_get(&it->oi_it);
4813 RETURN(PTR_ERR(name));
4815 namelen = iam_it_key_size(&it->oi_it);
4817 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4819 RETURN(PTR_ERR(rec));
4821 rc = osd_fid_unpack(fid, rec);
4825 hash = iam_it_store(&it->oi_it);
4827 /* IAM does not store object type in IAM index (dir) */
4828 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4830 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4831 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4832 (struct iam_rec *)dtrec);
4833 osd_quota_unpack(it->oi_obj, dtrec);
4835 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4836 (struct iam_rec *)dtrec);
4843 * Returns cookie for current Iterator position.
4845 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4847 struct osd_it_iam *it = (struct osd_it_iam *)di;
4849 return iam_it_store(&it->oi_it);
4853 * Restore iterator from cookie.
4855 * \param di osd iterator
4856 * \param hash Iterator location cookie
4858 * \retval +ve di points to record with least key not larger than key.
4859 * \retval 0 di points to exact matched key
4860 * \retval -ve failure
4863 static int osd_it_iam_load(const struct lu_env *env,
4864 const struct dt_it *di, __u64 hash)
4866 struct osd_it_iam *it = (struct osd_it_iam *)di;
4868 return iam_it_load(&it->oi_it, hash);
4871 static const struct dt_index_operations osd_index_iam_ops = {
4872 .dio_lookup = osd_index_iam_lookup,
4873 .dio_declare_insert = osd_index_declare_iam_insert,
4874 .dio_insert = osd_index_iam_insert,
4875 .dio_declare_delete = osd_index_declare_iam_delete,
4876 .dio_delete = osd_index_iam_delete,
4878 .init = osd_it_iam_init,
4879 .fini = osd_it_iam_fini,
4880 .get = osd_it_iam_get,
4881 .put = osd_it_iam_put,
4882 .next = osd_it_iam_next,
4883 .key = osd_it_iam_key,
4884 .key_size = osd_it_iam_key_size,
4885 .rec = osd_it_iam_rec,
4886 .store = osd_it_iam_store,
4887 .load = osd_it_iam_load
4893 * Creates or initializes iterator context.
4895 * \retval struct osd_it_ea, iterator structure on success
4898 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4899 struct dt_object *dt,
4902 struct osd_object *obj = osd_dt_obj(dt);
4903 struct osd_thread_info *info = osd_oti_get(env);
4904 struct osd_it_ea *oie;
4906 struct lu_object *lo = &dt->do_lu;
4907 struct dentry *obj_dentry;
4910 if (!dt_object_exists(dt) || obj->oo_destroyed)
4911 RETURN(ERR_PTR(-ENOENT));
4913 OBD_SLAB_ALLOC_PTR_GFP(oie, osd_itea_cachep, GFP_NOFS);
4915 RETURN(ERR_PTR(-ENOMEM));
4916 obj_dentry = &oie->oie_dentry;
4918 obj_dentry->d_inode = obj->oo_inode;
4919 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4920 obj_dentry->d_name.hash = 0;
4922 oie->oie_rd_dirent = 0;
4923 oie->oie_it_dirent = 0;
4924 oie->oie_dirent = NULL;
4925 if (unlikely(!info->oti_it_ea_buf_used)) {
4926 oie->oie_buf = info->oti_it_ea_buf;
4927 info->oti_it_ea_buf_used = 1;
4929 OBD_ALLOC(oie->oie_buf, OSD_IT_EA_BUFSIZE);
4930 if (oie->oie_buf == NULL)
4931 RETURN(ERR_PTR(-ENOMEM));
4935 file = &oie->oie_file;
4937 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4938 if (attr & LUDA_64BITHASH)
4939 file->f_mode = FMODE_64BITHASH;
4941 file->f_mode = FMODE_32BITHASH;
4942 file->f_path.dentry = obj_dentry;
4943 file->f_mapping = obj->oo_inode->i_mapping;
4944 file->f_op = obj->oo_inode->i_fop;
4945 set_file_inode(file, obj->oo_inode);
4948 RETURN((struct dt_it *) oie);
4952 * Destroy or finishes iterator context.
4954 * \param di iterator structure to be destroyed
4956 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4958 struct osd_thread_info *info = osd_oti_get(env);
4959 struct osd_it_ea *oie = (struct osd_it_ea *)di;
4960 struct osd_object *obj = oie->oie_obj;
4961 struct inode *inode = obj->oo_inode;
4964 oie->oie_file.f_op->release(inode, &oie->oie_file);
4965 lu_object_put(env, &obj->oo_dt.do_lu);
4966 if (unlikely(oie->oie_buf != info->oti_it_ea_buf))
4967 OBD_FREE(oie->oie_buf, OSD_IT_EA_BUFSIZE);
4969 info->oti_it_ea_buf_used = 0;
4970 OBD_SLAB_FREE_PTR(oie, osd_itea_cachep);
4975 * It position the iterator at given key, so that next lookup continues from
4976 * that key Or it is similar to dio_it->load() but based on a key,
4977 * rather than file position.
4979 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4982 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4984 static int osd_it_ea_get(const struct lu_env *env,
4985 struct dt_it *di, const struct dt_key *key)
4987 struct osd_it_ea *it = (struct osd_it_ea *)di;
4990 LASSERT(((const char *)key)[0] == '\0');
4991 it->oie_file.f_pos = 0;
4992 it->oie_rd_dirent = 0;
4993 it->oie_it_dirent = 0;
4994 it->oie_dirent = NULL;
5002 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
5006 struct osd_filldir_cbs {
5007 #ifdef HAVE_DIR_CONTEXT
5008 struct dir_context ctx;
5010 struct osd_it_ea *it;
5013 * It is called internally by ->readdir(). It fills the
5014 * iterator's in-memory data structure with required
5015 * information i.e. name, namelen, rec_size etc.
5017 * \param buf in which information to be filled in.
5018 * \param name name of the file in given dir
5020 * \retval 0 on success
5021 * \retval 1 on buffer full
5023 static int osd_ldiskfs_filldir(void *buf, const char *name, int namelen,
5024 loff_t offset, __u64 ino,
5027 struct osd_it_ea *it = ((struct osd_filldir_cbs *)buf)->it;
5028 struct osd_object *obj = it->oie_obj;
5029 struct osd_it_ea_dirent *ent = it->oie_dirent;
5030 struct lu_fid *fid = &ent->oied_fid;
5031 struct osd_fid_pack *rec;
5034 /* this should never happen */
5035 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
5036 CERROR("ldiskfs return invalid namelen %d\n", namelen);
5040 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
5044 /* "." is just the object itself. */
5045 if (namelen == 1 && name[0] == '.') {
5046 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5047 } else if (d_type & LDISKFS_DIRENT_LUFID) {
5048 rec = (struct osd_fid_pack*) (name + namelen + 1);
5049 if (osd_fid_unpack(fid, rec) != 0)
5054 d_type &= ~LDISKFS_DIRENT_LUFID;
5056 /* NOT export local root. */
5057 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
5058 ino = obj->oo_inode->i_ino;
5059 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
5062 ent->oied_ino = ino;
5063 ent->oied_off = offset;
5064 ent->oied_namelen = namelen;
5065 ent->oied_type = d_type;
5067 memcpy(ent->oied_name, name, namelen);
5069 it->oie_rd_dirent++;
5070 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
5075 * Calls ->readdir() to load a directory entry at a time
5076 * and stored it in iterator's in-memory data structure.
5078 * \param di iterator's in memory structure
5080 * \retval 0 on success
5081 * \retval -ve on error
5082 * \retval +1 reach the end of entry
5084 static int osd_ldiskfs_it_fill(const struct lu_env *env,
5085 const struct dt_it *di)
5087 struct osd_it_ea *it = (struct osd_it_ea *)di;
5088 struct osd_object *obj = it->oie_obj;
5089 struct inode *inode = obj->oo_inode;
5090 struct htree_lock *hlock = NULL;
5091 struct file *filp = &it->oie_file;
5093 struct osd_filldir_cbs buf = {
5094 #ifdef HAVE_DIR_CONTEXT
5095 .ctx.actor = osd_ldiskfs_filldir,
5101 it->oie_dirent = it->oie_buf;
5102 it->oie_rd_dirent = 0;
5104 if (obj->oo_hl_head != NULL) {
5105 hlock = osd_oti_get(env)->oti_hlock;
5106 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
5107 inode, LDISKFS_HLOCK_READDIR);
5109 down_read(&obj->oo_ext_idx_sem);
5112 #ifdef HAVE_DIR_CONTEXT
5113 buf.ctx.pos = filp->f_pos;
5114 rc = inode->i_fop->iterate(filp, &buf.ctx);
5115 filp->f_pos = buf.ctx.pos;
5117 rc = inode->i_fop->readdir(filp, &buf, osd_ldiskfs_filldir);
5121 ldiskfs_htree_unlock(hlock);
5123 up_read(&obj->oo_ext_idx_sem);
5125 if (it->oie_rd_dirent == 0) {
5126 /*If it does not get any dirent, it means it has been reached
5127 *to the end of the dir */
5128 it->oie_file.f_pos = ldiskfs_get_htree_eof(&it->oie_file);
5132 it->oie_dirent = it->oie_buf;
5133 it->oie_it_dirent = 1;
5140 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5141 * to load a directory entry at a time and stored it in
5142 * iterator's in-memory data structure.
5144 * \param di iterator's in memory structure
5146 * \retval +ve iterator reached to end
5147 * \retval 0 iterator not reached to end
5148 * \retval -ve on error
5150 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
5152 struct osd_it_ea *it = (struct osd_it_ea *)di;
5157 if (it->oie_it_dirent < it->oie_rd_dirent) {
5159 (void *) it->oie_dirent +
5160 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
5161 it->oie_dirent->oied_namelen);
5162 it->oie_it_dirent++;
5165 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
5168 rc = osd_ldiskfs_it_fill(env, di);
5175 * Returns the key at current position from iterator's in memory structure.
5177 * \param di iterator's in memory structure
5179 * \retval key i.e. struct dt_key on success
5181 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
5182 const struct dt_it *di)
5184 struct osd_it_ea *it = (struct osd_it_ea *)di;
5186 return (struct dt_key *)it->oie_dirent->oied_name;
5190 * Returns the key's size at current position from iterator's in memory structure.
5192 * \param di iterator's in memory structure
5194 * \retval key_size i.e. struct dt_key on success
5196 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
5198 struct osd_it_ea *it = (struct osd_it_ea *)di;
5200 return it->oie_dirent->oied_namelen;
5204 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
5206 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
5207 "dot_dotdot = %d\n", dot_dotdot);
5209 if (LDISKFS_DIR_REC_LEN(de) >=
5210 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
5217 osd_dirent_has_space(struct ldiskfs_dir_entry_2 *de, __u16 namelen,
5218 unsigned blocksize, int dot_dotdot)
5221 return osd_dot_dotdot_has_space(de, dot_dotdot);
5223 if (ldiskfs_rec_len_from_disk(de->rec_len, blocksize) >=
5224 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
5231 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
5232 struct dentry *dentry, const struct lu_fid *fid,
5233 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
5234 struct htree_lock *hlock, int dot_dotdot)
5236 struct inode *dir = dentry->d_parent->d_inode;
5237 struct inode *inode = dentry->d_inode;
5238 struct osd_fid_pack *rec;
5239 struct ldiskfs_dentry_param *ldp;
5240 int namelen = dentry->d_name.len;
5244 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
5245 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
5248 /* There is enough space to hold the FID-in-dirent. */
5249 if (osd_dirent_has_space(de, namelen, dir->i_sb->s_blocksize,
5251 rc = ldiskfs_journal_get_write_access(jh, bh);
5255 de->name[namelen] = 0;
5256 rec = (struct osd_fid_pack *)(de->name + namelen + 1);
5257 rec->fp_len = sizeof(struct lu_fid) + 1;
5258 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
5259 de->file_type |= LDISKFS_DIRENT_LUFID;
5260 rc = ldiskfs_handle_dirty_metadata(jh, NULL, bh);
5265 LASSERTF(dot_dotdot == 0, "dot_dotdot = %d\n", dot_dotdot);
5267 rc = ldiskfs_delete_entry(jh, dir, de, bh);
5271 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
5272 osd_get_ldiskfs_dirent_param(ldp, fid);
5273 dentry->d_fsdata = (void *)ldp;
5274 ll_vfs_dq_init(dir);
5275 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
5276 /* It is too bad, we cannot reinsert the name entry back.
5277 * That means we lose it! */
5279 CDEBUG(D_LFSCK, "%.16s: fail to reinsert the dirent, "
5280 "dir = %lu/%u, name = %.*s, "DFID": rc = %d\n",
5281 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
5282 dir->i_ino, dir->i_generation, namelen,
5283 dentry->d_name.name, PFID(fid), rc);
5289 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
5290 struct osd_it_ea *it, struct lu_fid *fid,
5291 struct osd_inode_id *id, __u32 *attr)
5293 struct osd_thread_info *info = osd_oti_get(env);
5294 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
5295 struct osd_device *dev = osd_obj2dev(obj);
5296 struct super_block *sb = osd_sb(dev);
5297 const char *devname =
5298 LDISKFS_SB(sb)->s_es->s_volume_name;
5299 struct osd_it_ea_dirent *ent = it->oie_dirent;
5300 struct inode *dir = obj->oo_inode;
5301 struct htree_lock *hlock = NULL;
5302 struct buffer_head *bh = NULL;
5303 handle_t *jh = NULL;
5304 struct ldiskfs_dir_entry_2 *de;
5305 struct dentry *dentry;
5306 struct inode *inode;
5313 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5314 inode = osd_iget(info, dev, id);
5315 if (IS_ERR(inode)) {
5316 rc = PTR_ERR(inode);
5317 if (rc == -ENOENT || rc == -ESTALE) {
5318 *attr |= LUDA_UNKNOWN;
5321 CDEBUG(D_LFSCK, "%.16s: fail to iget for dirent "
5322 "check_repair, dir = %lu/%u, name = %.*s: "
5324 devname, dir->i_ino, dir->i_generation,
5325 ent->oied_namelen, ent->oied_name, rc);
5331 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
5333 rc = osd_get_lma(info, inode, dentry, lma);
5339 if (ent->oied_name[0] == '.') {
5340 if (ent->oied_namelen == 1)
5342 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
5346 /* We need to ensure that the name entry is still valid.
5347 * Because it may be removed or renamed by other already.
5349 * The unlink or rename operation will start journal before PDO lock,
5350 * so to avoid deadlock, here we need to start journal handle before
5351 * related PDO lock also. But because we do not know whether there
5352 * will be something to be repaired before PDO lock, we just start
5353 * journal without conditions.
5355 * We may need to remove the name entry firstly, then insert back.
5356 * One credit is for user quota file update.
5357 * One credit is for group quota file update.
5358 * Two credits are for dirty inode. */
5359 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
5360 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
5362 if (dev->od_dirent_journal != 0) {
5365 jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, credits);
5368 CDEBUG(D_LFSCK, "%.16s: fail to start trans for dirent "
5369 "check_repair, dir = %lu/%u, credits = %d, "
5370 "name = %.*s: rc = %d\n",
5371 devname, dir->i_ino, dir->i_generation, credits,
5372 ent->oied_namelen, ent->oied_name, rc);
5374 GOTO(out_inode, rc);
5377 if (obj->oo_hl_head != NULL) {
5378 hlock = osd_oti_get(env)->oti_hlock;
5379 /* "0" means exclusive lock for the whole directory.
5380 * We need to prevent others access such name entry
5381 * during the delete + insert. Neither HLOCK_ADD nor
5382 * HLOCK_DEL cannot guarantee the atomicity. */
5383 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
5385 down_write(&obj->oo_ext_idx_sem);
5388 if (obj->oo_hl_head != NULL) {
5389 hlock = osd_oti_get(env)->oti_hlock;
5390 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
5391 LDISKFS_HLOCK_LOOKUP);
5393 down_read(&obj->oo_ext_idx_sem);
5397 bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, hlock);
5398 /* For dot/dotdot entry, if there is not enough space to hold the
5399 * FID-in-dirent, just keep them there. It only happens when the
5400 * device upgraded from 1.8 or restored from MDT file-level backup.
5401 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
5402 * and needs to get FID from LMA when readdir, it will not affect the
5403 * performance much. */
5404 if ((bh == NULL) || (le32_to_cpu(de->inode) != inode->i_ino) ||
5405 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
5406 *attr |= LUDA_IGNORE;
5412 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI)) {
5413 struct lu_fid *tfid = &lma->lma_self_fid;
5415 *attr |= LUDA_IGNORE;
5416 /* It must be REMOTE_PARENT_DIR and as the
5417 * dotdot entry of remote directory */
5418 if (unlikely(dot_dotdot != 2 ||
5419 fid_seq(tfid) != FID_SEQ_LOCAL_FILE ||
5420 fid_oid(tfid) != REMOTE_PARENT_DIR_OID)) {
5421 CDEBUG(D_LFSCK, "%.16s: expect remote agent "
5422 "parent directory, but got %.*s under "
5423 "dir = %lu/%u with the FID "DFID"\n",
5424 devname, ent->oied_namelen,
5425 ent->oied_name, dir->i_ino,
5426 dir->i_generation, PFID(tfid));
5428 GOTO(out, rc = -EIO);
5434 if (fid_is_sane(fid)) {
5435 /* FID-in-dirent is valid. */
5436 if (lu_fid_eq(fid, &lma->lma_self_fid))
5439 /* Do not repair under dryrun mode. */
5440 if (*attr & LUDA_VERIFY_DRYRUN) {
5441 *attr |= LUDA_REPAIR;
5448 dev->od_dirent_journal = 1;
5449 if (hlock != NULL) {
5450 ldiskfs_htree_unlock(hlock);
5453 up_read(&obj->oo_ext_idx_sem);
5459 *fid = lma->lma_self_fid;
5461 /* Update the FID-in-dirent. */
5462 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5465 *attr |= LUDA_REPAIR;
5467 CDEBUG(D_LFSCK, "%.16s: fail to update FID "
5468 "in the dirent, dir = %lu/%u, "
5469 "name = %.*s, "DFID": rc = %d\n",
5470 devname, dir->i_ino, dir->i_generation,
5471 ent->oied_namelen, ent->oied_name,
5474 /* Do not repair under dryrun mode. */
5475 if (*attr & LUDA_VERIFY_DRYRUN) {
5476 *fid = lma->lma_self_fid;
5477 *attr |= LUDA_REPAIR;
5484 dev->od_dirent_journal = 1;
5485 if (hlock != NULL) {
5486 ldiskfs_htree_unlock(hlock);
5489 up_read(&obj->oo_ext_idx_sem);
5495 *fid = lma->lma_self_fid;
5497 /* Append the FID-in-dirent. */
5498 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5501 *attr |= LUDA_REPAIR;
5503 CDEBUG(D_LFSCK, "%.16s: fail to append FID "
5504 "after the dirent, dir = %lu/%u, "
5505 "name = %.*s, "DFID": rc = %d\n",
5506 devname, dir->i_ino, dir->i_generation,
5507 ent->oied_namelen, ent->oied_name,
5511 /* Do not repair under dryrun mode. */
5512 if (*attr & LUDA_VERIFY_DRYRUN) {
5513 if (fid_is_sane(fid)) {
5514 *attr |= LUDA_REPAIR;
5516 lu_igif_build(fid, inode->i_ino,
5517 inode->i_generation);
5518 *attr |= LUDA_UPGRADE;
5526 dev->od_dirent_journal = 1;
5527 if (hlock != NULL) {
5528 ldiskfs_htree_unlock(hlock);
5531 up_read(&obj->oo_ext_idx_sem);
5538 if (unlikely(fid_is_sane(fid))) {
5539 /* FID-in-dirent exists, but FID-in-LMA is lost.
5540 * Trust the FID-in-dirent, and add FID-in-LMA. */
5541 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5543 *attr |= LUDA_REPAIR;
5545 CDEBUG(D_LFSCK, "%.16s: fail to set LMA for "
5546 "update dirent, dir = %lu/%u, "
5547 "name = %.*s, "DFID": rc = %d\n",
5548 devname, dir->i_ino, dir->i_generation,
5549 ent->oied_namelen, ent->oied_name,
5552 lu_igif_build(fid, inode->i_ino, inode->i_generation);
5553 /* It is probably IGIF object. Only aappend the
5554 * FID-in-dirent. OI scrub will process FID-in-LMA. */
5555 rc = osd_dirent_reinsert(env, jh, dentry, fid, bh, de,
5558 *attr |= LUDA_UPGRADE;
5560 CDEBUG(D_LFSCK, "%.16s: fail to append IGIF "
5561 "after the dirent, dir = %lu/%u, "
5562 "name = %.*s, "DFID": rc = %d\n",
5563 devname, dir->i_ino, dir->i_generation,
5564 ent->oied_namelen, ent->oied_name,
5573 if (hlock != NULL) {
5574 ldiskfs_htree_unlock(hlock);
5576 if (dev->od_dirent_journal != 0)
5577 up_write(&obj->oo_ext_idx_sem);
5579 up_read(&obj->oo_ext_idx_sem);
5583 ldiskfs_journal_stop(jh);
5587 if (rc >= 0 && !dirty)
5588 dev->od_dirent_journal = 0;
5594 * Returns the value at current position from iterator's in memory structure.
5596 * \param di struct osd_it_ea, iterator's in memory structure
5597 * \param attr attr requested for dirent.
5598 * \param lde lustre dirent
5600 * \retval 0 no error and \param lde has correct lustre dirent.
5601 * \retval -ve on error
5603 static inline int osd_it_ea_rec(const struct lu_env *env,
5604 const struct dt_it *di,
5605 struct dt_rec *dtrec, __u32 attr)
5607 struct osd_it_ea *it = (struct osd_it_ea *)di;
5608 struct osd_object *obj = it->oie_obj;
5609 struct osd_device *dev = osd_obj2dev(obj);
5610 struct osd_thread_info *oti = osd_oti_get(env);
5611 struct osd_inode_id *id = &oti->oti_id;
5612 struct lu_fid *fid = &it->oie_dirent->oied_fid;
5613 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
5614 __u32 ino = it->oie_dirent->oied_ino;
5618 LASSERT(obj->oo_inode != dev->od_mdt_map->omm_remote_parent->d_inode);
5620 if (attr & LUDA_VERIFY) {
5621 if (unlikely(ino == osd_remote_parent_ino(dev))) {
5622 attr |= LUDA_IGNORE;
5623 /* If the parent is on remote MDT, and there
5624 * is no FID-in-dirent, then we have to get
5625 * the parent FID from the linkEA. */
5626 if (!fid_is_sane(fid) &&
5627 it->oie_dirent->oied_namelen == 2 &&
5628 it->oie_dirent->oied_name[0] == '.' &&
5629 it->oie_dirent->oied_name[1] == '.')
5630 osd_get_pfid_from_linkea(env, obj, fid);
5632 rc = osd_dirent_check_repair(env, obj, it, fid, id,
5636 if (!fid_is_sane(fid)) {
5637 attr &= ~LUDA_IGNORE;
5638 attr |= LUDA_UNKNOWN;
5641 attr &= ~LU_DIRENT_ATTRS_MASK;
5642 if (!fid_is_sane(fid)) {
5643 bool is_dotdot = false;
5644 if (it->oie_dirent->oied_namelen == 2 &&
5645 it->oie_dirent->oied_name[0] == '.' &&
5646 it->oie_dirent->oied_name[1] == '.')
5648 /* If the parent is on remote MDT, and there
5649 * is no FID-in-dirent, then we have to get
5650 * the parent FID from the linkEA. */
5651 if (ino == osd_remote_parent_ino(dev) && is_dotdot) {
5652 rc = osd_get_pfid_from_linkea(env, obj, fid);
5654 if (is_dotdot == false &&
5655 OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5658 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5661 osd_id_gen(id, ino, OSD_OII_NOGEN);
5665 /* Pack the entry anyway, at least the offset is right. */
5666 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5667 it->oie_dirent->oied_name,
5668 it->oie_dirent->oied_namelen,
5669 it->oie_dirent->oied_type, attr);
5674 if (osd_remote_fid(env, dev, fid))
5677 if (likely(!(attr & (LUDA_IGNORE | LUDA_UNKNOWN)) && rc == 0))
5678 osd_add_oi_cache(oti, dev, id, fid);
5680 RETURN(rc > 0 ? 0 : rc);
5684 * Returns the record size size at current position.
5686 * This function will return record(lu_dirent) size in bytes.
5688 * \param[in] env execution environment
5689 * \param[in] di iterator's in memory structure
5690 * \param[in] attr attribute of the entry, only requires LUDA_TYPE to
5691 * calculate the lu_dirent size.
5693 * \retval record size(in bytes & in memory) of the current lu_dirent
5696 static int osd_it_ea_rec_size(const struct lu_env *env, const struct dt_it *di,
5699 struct osd_it_ea *it = (struct osd_it_ea *)di;
5701 return lu_dirent_calc_size(it->oie_dirent->oied_namelen, attr);
5705 * Returns a cookie for current position of the iterator head, so that
5706 * user can use this cookie to load/start the iterator next time.
5708 * \param di iterator's in memory structure
5710 * \retval cookie for current position, on success
5712 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5714 struct osd_it_ea *it = (struct osd_it_ea *)di;
5716 return it->oie_dirent->oied_off;
5720 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5721 * to load a directory entry at a time and stored it i inn,
5722 * in iterator's in-memory data structure.
5724 * \param di struct osd_it_ea, iterator's in memory structure
5726 * \retval +ve on success
5727 * \retval -ve on error
5729 static int osd_it_ea_load(const struct lu_env *env,
5730 const struct dt_it *di, __u64 hash)
5732 struct osd_it_ea *it = (struct osd_it_ea *)di;
5736 it->oie_file.f_pos = hash;
5738 rc = osd_ldiskfs_it_fill(env, di);
5749 * Index lookup function for interoperability mode (b11826).
5751 * \param key, key i.e. file name to be searched
5753 * \retval +ve, on success
5754 * \retval -ve, on error
5756 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5757 struct dt_rec *rec, const struct dt_key *key)
5759 struct osd_object *obj = osd_dt_obj(dt);
5764 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5765 LINVRNT(osd_invariant(obj));
5767 rc = osd_ea_lookup_rec(env, obj, rec, key);
5774 * Index and Iterator operations for interoperability
5775 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5777 static const struct dt_index_operations osd_index_ea_ops = {
5778 .dio_lookup = osd_index_ea_lookup,
5779 .dio_declare_insert = osd_index_declare_ea_insert,
5780 .dio_insert = osd_index_ea_insert,
5781 .dio_declare_delete = osd_index_declare_ea_delete,
5782 .dio_delete = osd_index_ea_delete,
5784 .init = osd_it_ea_init,
5785 .fini = osd_it_ea_fini,
5786 .get = osd_it_ea_get,
5787 .put = osd_it_ea_put,
5788 .next = osd_it_ea_next,
5789 .key = osd_it_ea_key,
5790 .key_size = osd_it_ea_key_size,
5791 .rec = osd_it_ea_rec,
5792 .rec_size = osd_it_ea_rec_size,
5793 .store = osd_it_ea_store,
5794 .load = osd_it_ea_load
5798 static void *osd_key_init(const struct lu_context *ctx,
5799 struct lu_context_key *key)
5801 struct osd_thread_info *info;
5803 OBD_ALLOC_PTR(info);
5805 return ERR_PTR(-ENOMEM);
5807 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5808 if (info->oti_it_ea_buf == NULL)
5811 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5813 info->oti_hlock = ldiskfs_htree_lock_alloc();
5814 if (info->oti_hlock == NULL)
5820 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5823 return ERR_PTR(-ENOMEM);
5826 static void osd_key_fini(const struct lu_context *ctx,
5827 struct lu_context_key *key, void* data)
5829 struct osd_thread_info *info = data;
5830 struct ldiskfs_inode_info *lli = LDISKFS_I(info->oti_inode);
5832 if (info->oti_inode != NULL)
5834 if (info->oti_hlock != NULL)
5835 ldiskfs_htree_lock_free(info->oti_hlock);
5836 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5837 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5838 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5839 lu_buf_free(&info->oti_big_buf);
5843 static void osd_key_exit(const struct lu_context *ctx,
5844 struct lu_context_key *key, void *data)
5846 struct osd_thread_info *info = data;
5848 LASSERT(info->oti_r_locks == 0);
5849 LASSERT(info->oti_w_locks == 0);
5850 LASSERT(info->oti_txns == 0);
5853 /* type constructor/destructor: osd_type_init, osd_type_fini */
5854 LU_TYPE_INIT_FINI(osd, &osd_key);
5856 struct lu_context_key osd_key = {
5857 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5858 .lct_init = osd_key_init,
5859 .lct_fini = osd_key_fini,
5860 .lct_exit = osd_key_exit
5864 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5865 const char *name, struct lu_device *next)
5867 struct osd_device *osd = osd_dev(d);
5869 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5870 >= sizeof(osd->od_svname))
5872 return osd_procfs_init(osd, name);
5875 static int osd_fid_init(const struct lu_env *env, struct osd_device *osd)
5877 struct seq_server_site *ss = osd_seq_site(osd);
5881 if (osd->od_is_ost || osd->od_cl_seq != NULL)
5884 if (unlikely(ss == NULL))
5887 OBD_ALLOC_PTR(osd->od_cl_seq);
5888 if (osd->od_cl_seq == NULL)
5891 rc = seq_client_init(osd->od_cl_seq, NULL, LUSTRE_SEQ_METADATA,
5892 osd->od_svname, ss->ss_server_seq);
5894 OBD_FREE_PTR(osd->od_cl_seq);
5895 osd->od_cl_seq = NULL;
5899 if (ss->ss_node_id == 0) {
5900 /* If the OSD on the sequence controller(MDT0), then allocate
5901 * sequence here, otherwise allocate sequence after connected
5902 * to MDT0 (see mdt_register_lwp_callback()). */
5903 rc = seq_server_alloc_meta(osd->od_cl_seq->lcs_srv,
5904 &osd->od_cl_seq->lcs_space, env);
5910 static void osd_fid_fini(const struct lu_env *env, struct osd_device *osd)
5912 if (osd->od_cl_seq == NULL)
5915 seq_client_fini(osd->od_cl_seq);
5916 OBD_FREE_PTR(osd->od_cl_seq);
5917 osd->od_cl_seq = NULL;
5920 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5924 /* shutdown quota slave instance associated with the device */
5925 if (o->od_quota_slave != NULL) {
5926 qsd_fini(env, o->od_quota_slave);
5927 o->od_quota_slave = NULL;
5930 osd_fid_fini(env, o);
5935 static void osd_umount(const struct lu_env *env, struct osd_device *o)
5939 if (o->od_mnt != NULL) {
5940 shrink_dcache_sb(osd_sb(o));
5941 osd_sync(env, &o->od_dt_dev);
5950 static int osd_mount(const struct lu_env *env,
5951 struct osd_device *o, struct lustre_cfg *cfg)
5953 const char *name = lustre_cfg_string(cfg, 0);
5954 const char *dev = lustre_cfg_string(cfg, 1);
5956 unsigned long page, s_flags, lmd_flags = 0;
5957 struct page *__page;
5958 struct file_system_type *type;
5959 char *options = NULL;
5961 struct osd_thread_info *info = osd_oti_get(env);
5962 struct lu_fid *fid = &info->oti_fid;
5963 struct inode *inode;
5964 int rc = 0, force_over_128tb = 0;
5967 if (o->od_mnt != NULL)
5970 if (strlen(dev) >= sizeof(o->od_mntdev))
5972 strcpy(o->od_mntdev, dev);
5974 str = lustre_cfg_string(cfg, 2);
5975 s_flags = simple_strtoul(str, NULL, 0);
5976 str = strstr(str, ":");
5978 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5979 opts = lustre_cfg_string(cfg, 3);
5981 if (opts == NULL || strstr(opts, "bigendian_extents") == NULL) {
5982 CERROR("%s: device %s extents feature is not guaranteed to "
5983 "work on big-endian systems. Use \"bigendian_extents\" "
5984 "mount option to override.\n", name, dev);
5988 if (opts != NULL && strstr(opts, "force_over_128tb") != NULL)
5989 force_over_128tb = 1;
5991 __page = alloc_page(GFP_IOFS);
5993 GOTO(out, rc = -ENOMEM);
5994 page = (unsigned long)page_address(__page);
5995 options = (char *)page;
5998 /* strip out the options for back compatiblity */
5999 static char *sout[] = {
6006 /* strip out option we processed in osd */
6007 "bigendian_extents",
6011 strcat(options, opts);
6012 for (rc = 0, str = options; sout[rc]; ) {
6013 char *op = strstr(str, sout[rc]);
6019 if (op == options || *(op - 1) == ',') {
6020 str = op + strlen(sout[rc]);
6021 if (*str == ',' || *str == '\0') {
6022 *str == ',' ? str++ : str;
6023 memmove(op, str, strlen(str) + 1);
6026 for (str = op; *str != ',' && *str != '\0'; str++)
6030 strncat(options, "user_xattr,acl", 14);
6033 /* Glom up mount options */
6034 if (*options != '\0')
6035 strcat(options, ",");
6036 strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
6038 type = get_fs_type("ldiskfs");
6040 CERROR("%s: cannot find ldiskfs module\n", name);
6041 GOTO(out, rc = -ENODEV);
6044 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
6045 module_put(type->owner);
6047 if (IS_ERR(o->od_mnt)) {
6048 rc = PTR_ERR(o->od_mnt);
6050 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
6054 if (ldiskfs_blocks_count(LDISKFS_SB(osd_sb(o))->s_es) > (8ULL << 32) &&
6055 force_over_128tb == 0) {
6056 CERROR("%s: device %s LDISKFS does not support filesystems "
6057 "greater than 128TB and can cause data corruption. "
6058 "Use \"force_over_128tb\" mount option to override.\n",
6060 GOTO(out, rc = -EINVAL);
6063 #ifdef HAVE_DEV_SET_RDONLY
6064 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
6065 CERROR("%s: underlying device %s is marked as read-only. "
6066 "Setup failed\n", name, dev);
6067 GOTO(out_mnt, rc = -EROFS);
6071 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
6072 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
6073 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
6074 GOTO(out_mnt, rc = -EINVAL);
6077 #ifdef LDISKFS_MOUNT_DIRDATA
6078 if (LDISKFS_HAS_INCOMPAT_FEATURE(o->od_mnt->mnt_sb,
6079 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
6080 LDISKFS_SB(osd_sb(o))->s_mount_opt |= LDISKFS_MOUNT_DIRDATA;
6081 else if (!o->od_is_ost)
6082 CWARN("%s: device %s was upgraded from Lustre-1.x without "
6083 "enabling the dirdata feature. If you do not want to "
6084 "downgrade to Lustre-1.x again, you can enable it via "
6085 "'tune2fs -O dirdata device'\n", name, dev);
6087 inode = osd_sb(o)->s_root->d_inode;
6088 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
6089 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
6091 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
6095 if (lmd_flags & LMD_FLG_NOSCRUB)
6106 __free_page(__page);
6111 static struct lu_device *osd_device_fini(const struct lu_env *env,
6112 struct lu_device *d)
6114 struct osd_device *o = osd_dev(d);
6117 osd_shutdown(env, o);
6119 osd_scrub_cleanup(env, o);
6120 osd_obj_map_fini(o);
6126 static int osd_device_init0(const struct lu_env *env,
6127 struct osd_device *o,
6128 struct lustre_cfg *cfg)
6130 struct lu_device *l = osd2lu_dev(o);
6131 struct osd_thread_info *info;
6135 /* if the module was re-loaded, env can loose its keys */
6136 rc = lu_env_refill((struct lu_env *) env);
6139 info = osd_oti_get(env);
6142 l->ld_ops = &osd_lu_ops;
6143 o->od_dt_dev.dd_ops = &osd_dt_ops;
6145 spin_lock_init(&o->od_osfs_lock);
6146 mutex_init(&o->od_otable_mutex);
6147 INIT_LIST_HEAD(&o->od_orphan_list);
6149 o->od_read_cache = 1;
6150 o->od_writethrough_cache = 1;
6151 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
6153 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
6154 sizeof(o->od_svname));
6155 if (cplen >= sizeof(o->od_svname)) {
6160 o->od_index = -1; /* -1 means index is invalid */
6161 rc = server_name2index(o->od_svname, &o->od_index, NULL);
6162 if (rc == LDD_F_SV_TYPE_OST)
6165 o->od_full_scrub_ratio = OFSR_DEFAULT;
6166 o->od_full_scrub_threshold_rate = FULL_SCRUB_THRESHOLD_RATE_DEFAULT;
6167 rc = osd_mount(env, o, cfg);
6171 rc = osd_obj_map_init(env, o);
6175 rc = lu_site_init(&o->od_site, l);
6177 GOTO(out_compat, rc);
6178 o->od_site.ls_bottom_dev = l;
6180 rc = lu_site_init_finish(&o->od_site);
6184 INIT_LIST_HEAD(&o->od_ios_list);
6185 /* setup scrub, including OI files initialization */
6186 rc = osd_scrub_setup(env, o);
6190 rc = osd_procfs_init(o, o->od_svname);
6192 CERROR("%s: can't initialize procfs: rc = %d\n",
6194 GOTO(out_scrub, rc);
6197 LASSERT(l->ld_site->ls_linkage.next != NULL);
6198 LASSERT(l->ld_site->ls_linkage.prev != NULL);
6200 /* initialize quota slave instance */
6201 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
6203 if (IS_ERR(o->od_quota_slave)) {
6204 rc = PTR_ERR(o->od_quota_slave);
6205 o->od_quota_slave = NULL;
6206 GOTO(out_procfs, rc);
6214 osd_scrub_cleanup(env, o);
6216 lu_site_fini(&o->od_site);
6218 osd_obj_map_fini(o);
6225 static struct lu_device *osd_device_alloc(const struct lu_env *env,
6226 struct lu_device_type *t,
6227 struct lustre_cfg *cfg)
6229 struct osd_device *o;
6234 return ERR_PTR(-ENOMEM);
6236 rc = dt_device_init(&o->od_dt_dev, t);
6238 /* Because the ctx might be revived in dt_device_init,
6239 * refill the env here */
6240 lu_env_refill((struct lu_env *)env);
6241 rc = osd_device_init0(env, o, cfg);
6243 dt_device_fini(&o->od_dt_dev);
6246 if (unlikely(rc != 0))
6249 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
6252 static struct lu_device *osd_device_free(const struct lu_env *env,
6253 struct lu_device *d)
6255 struct osd_device *o = osd_dev(d);
6258 /* XXX: make osd top device in order to release reference */
6259 d->ld_site->ls_top_dev = d;
6260 lu_site_purge(env, d->ld_site, -1);
6261 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
6262 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
6263 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
6265 lu_site_fini(&o->od_site);
6266 dt_device_fini(&o->od_dt_dev);
6271 static int osd_process_config(const struct lu_env *env,
6272 struct lu_device *d, struct lustre_cfg *cfg)
6274 struct osd_device *o = osd_dev(d);
6278 switch (cfg->lcfg_command) {
6280 rc = osd_mount(env, o, cfg);
6283 lu_dev_del_linkage(d->ld_site, d);
6284 rc = osd_shutdown(env, o);
6287 LASSERT(&o->od_dt_dev);
6288 rc = class_process_proc_param(PARAM_OSD, lprocfs_osd_obd_vars,
6289 cfg, &o->od_dt_dev);
6290 if (rc > 0 || rc == -ENOSYS)
6291 rc = class_process_proc_param(PARAM_OST,
6292 lprocfs_osd_obd_vars,
6293 cfg, &o->od_dt_dev);
6302 static int osd_recovery_complete(const struct lu_env *env,
6303 struct lu_device *d)
6305 struct osd_device *osd = osd_dev(d);
6309 if (osd->od_quota_slave == NULL)
6312 /* start qsd instance on recovery completion, this notifies the quota
6313 * slave code that we are about to process new requests now */
6314 rc = qsd_start(env, osd->od_quota_slave);
6319 * we use exports to track all osd users
6321 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
6322 struct obd_device *obd, struct obd_uuid *cluuid,
6323 struct obd_connect_data *data, void *localdata)
6325 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6326 struct lustre_handle conn;
6330 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
6332 rc = class_connect(&conn, obd, cluuid);
6336 *exp = class_conn2export(&conn);
6338 spin_lock(&osd->od_osfs_lock);
6340 spin_unlock(&osd->od_osfs_lock);
6346 * once last export (we don't count self-export) disappeared
6347 * osd can be released
6349 static int osd_obd_disconnect(struct obd_export *exp)
6351 struct obd_device *obd = exp->exp_obd;
6352 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6353 int rc, release = 0;
6356 /* Only disconnect the underlying layers on the final disconnect. */
6357 spin_lock(&osd->od_osfs_lock);
6359 if (osd->od_connects == 0)
6361 spin_unlock(&osd->od_osfs_lock);
6363 rc = class_disconnect(exp); /* bz 9811 */
6365 if (rc == 0 && release)
6366 class_manual_cleanup(obd);
6370 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
6371 struct lu_device *dev)
6373 struct osd_device *osd = osd_dev(dev);
6374 struct lr_server_data *lsd =
6375 &osd->od_dt_dev.dd_lu_dev.ld_site->ls_tgt->lut_lsd;
6379 if (osd->od_quota_slave != NULL) {
6380 /* set up quota slave objects */
6381 result = qsd_prepare(env, osd->od_quota_slave);
6386 if (lsd->lsd_feature_incompat & OBD_COMPAT_OST) {
6387 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
6388 if (lsd->lsd_feature_rocompat & OBD_ROCOMPAT_IDX_IN_IDIF) {
6389 osd->od_index_in_idif = 1;
6391 osd->od_index_in_idif = 0;
6392 result = osd_register_proc_index_in_idif(osd);
6397 osd->od_index_in_idif = 1;
6401 result = osd_fid_init(env, osd);
6406 static int osd_fid_alloc(const struct lu_env *env, struct obd_export *exp,
6407 struct lu_fid *fid, struct md_op_data *op_data)
6409 struct osd_device *osd = osd_dev(exp->exp_obd->obd_lu_dev);
6411 return seq_client_alloc_fid(env, osd->od_cl_seq, fid);
6414 static const struct lu_object_operations osd_lu_obj_ops = {
6415 .loo_object_init = osd_object_init,
6416 .loo_object_delete = osd_object_delete,
6417 .loo_object_release = osd_object_release,
6418 .loo_object_free = osd_object_free,
6419 .loo_object_print = osd_object_print,
6420 .loo_object_invariant = osd_object_invariant
6423 const struct lu_device_operations osd_lu_ops = {
6424 .ldo_object_alloc = osd_object_alloc,
6425 .ldo_process_config = osd_process_config,
6426 .ldo_recovery_complete = osd_recovery_complete,
6427 .ldo_prepare = osd_prepare,
6430 static const struct lu_device_type_operations osd_device_type_ops = {
6431 .ldto_init = osd_type_init,
6432 .ldto_fini = osd_type_fini,
6434 .ldto_start = osd_type_start,
6435 .ldto_stop = osd_type_stop,
6437 .ldto_device_alloc = osd_device_alloc,
6438 .ldto_device_free = osd_device_free,
6440 .ldto_device_init = osd_device_init,
6441 .ldto_device_fini = osd_device_fini
6444 static struct lu_device_type osd_device_type = {
6445 .ldt_tags = LU_DEVICE_DT,
6446 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
6447 .ldt_ops = &osd_device_type_ops,
6448 .ldt_ctx_tags = LCT_LOCAL,
6451 static int osd_health_check(const struct lu_env *env, struct obd_device *obd)
6453 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
6454 struct super_block *sb = osd_sb(osd);
6456 return (osd->od_mnt == NULL || sb->s_flags & MS_RDONLY);
6460 * lprocfs legacy support.
6462 static struct obd_ops osd_obd_device_ops = {
6463 .o_owner = THIS_MODULE,
6464 .o_connect = osd_obd_connect,
6465 .o_disconnect = osd_obd_disconnect,
6466 .o_fid_alloc = osd_fid_alloc,
6467 .o_health_check = osd_health_check,
6470 static int __init osd_init(void)
6474 LASSERT(BH_DXLock < sizeof(((struct buffer_head *)0)->b_state) * 8);
6475 #if !defined(CONFIG_DEBUG_MUTEXES) && !defined(CONFIG_DEBUG_SPINLOCK)
6476 /* please, try to keep osd_thread_info smaller than a page */
6477 CLASSERT(sizeof(struct osd_thread_info) <= PAGE_SIZE);
6482 rc = lu_kmem_init(ldiskfs_caches);
6486 rc = class_register_type(&osd_obd_device_ops, NULL, true,
6487 lprocfs_osd_module_vars,
6488 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
6490 lu_kmem_fini(ldiskfs_caches);
6494 static void __exit osd_exit(void)
6496 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
6497 lu_kmem_fini(ldiskfs_caches);
6500 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6501 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
6502 MODULE_VERSION(LUSTRE_VERSION_STRING);
6503 MODULE_LICENSE("GPL");
6505 module_init(osd_init);
6506 module_exit(osd_exit);