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, 2012, 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_MDS
46 #include <linux/module.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
58 * struct OBD_{ALLOC,FREE}*()
61 #include <obd_support.h>
62 /* struct ptlrpc_thread */
63 #include <lustre_net.h>
64 #include <lustre_fid.h>
66 #include "osd_internal.h"
68 /* llo_* api support */
69 #include <md_object.h>
70 #include <lustre_quota.h>
73 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
74 "ldiskfs with parallel directory operations");
76 static const char dot[] = ".";
77 static const char dotdot[] = "..";
78 static const char remote_obj_dir[] = "REM_OBJ_DIR";
80 static const struct lu_object_operations osd_lu_obj_ops;
81 static const struct dt_object_operations osd_obj_ops;
82 static const struct dt_object_operations osd_obj_ea_ops;
83 static const struct dt_object_operations osd_obj_otable_it_ops;
84 static const struct dt_index_operations osd_index_iam_ops;
85 static const struct dt_index_operations osd_index_ea_ops;
87 #ifdef OSD_TRACK_DECLARES
88 int osd_trans_declare_op2rb[] = {
89 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
90 [OSD_OT_PUNCH] = OSD_OT_MAX,
91 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
92 [OSD_OT_CREATE] = OSD_OT_DESTROY,
93 [OSD_OT_DESTROY] = OSD_OT_CREATE,
94 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
95 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
96 [OSD_OT_WRITE] = OSD_OT_WRITE,
97 [OSD_OT_INSERT] = OSD_OT_DELETE,
98 [OSD_OT_DELETE] = OSD_OT_INSERT,
99 [OSD_OT_QUOTA] = OSD_OT_MAX,
103 static int osd_has_index(const struct osd_object *obj)
105 return obj->oo_dt.do_index_ops != NULL;
108 static int osd_object_invariant(const struct lu_object *l)
110 return osd_invariant(osd_obj(l));
114 * Concurrency: doesn't matter
116 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
118 return osd_oti_get(env)->oti_r_locks > 0;
122 * Concurrency: doesn't matter
124 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
126 struct osd_thread_info *oti = osd_oti_get(env);
127 return oti->oti_w_locks > 0 && o->oo_owner == env;
131 * Concurrency: doesn't access mutable data
133 static int osd_root_get(const struct lu_env *env,
134 struct dt_device *dev, struct lu_fid *f)
136 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
141 * OSD object methods.
145 * Concurrency: no concurrent access is possible that early in object
148 static struct lu_object *osd_object_alloc(const struct lu_env *env,
149 const struct lu_object_header *hdr,
152 struct osd_object *mo;
158 l = &mo->oo_dt.do_lu;
159 dt_object_init(&mo->oo_dt, NULL, d);
160 mo->oo_dt.do_ops = &osd_obj_ea_ops;
161 l->lo_ops = &osd_lu_obj_ops;
162 init_rwsem(&mo->oo_sem);
163 init_rwsem(&mo->oo_ext_idx_sem);
164 spin_lock_init(&mo->oo_guard);
171 static inline int __osd_xattr_get(struct inode *inode, struct dentry *dentry,
172 const char *name, void *buf, int len)
174 dentry->d_inode = inode;
175 dentry->d_sb = inode->i_sb;
176 return inode->i_op->getxattr(dentry, name, buf, len);
179 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
180 struct dentry *dentry, struct lustre_mdt_attrs *lma)
184 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
185 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
186 info->oti_mdt_attrs_old, LMA_OLD_SIZE);
188 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
189 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
191 lustre_lma_swab(lma);
192 /* Check LMA compatibility */
193 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
194 CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
195 "for fid = "DFID", ino = %lu\n",
196 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
197 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
198 PFID(&lma->lma_self_fid), inode->i_ino);
201 } else if (rc == 0) {
209 * retrieve object from backend ext fs.
211 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
212 struct osd_inode_id *id)
214 struct inode *inode = NULL;
216 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
218 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
219 id->oii_ino, PTR_ERR(inode));
220 } else if (id->oii_gen != OSD_OII_NOGEN &&
221 inode->i_generation != id->oii_gen) {
222 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
224 id->oii_ino, id->oii_gen, inode->i_generation);
226 inode = ERR_PTR(-ESTALE);
227 } else if (inode->i_nlink == 0) {
228 /* due to parallel readdir and unlink,
229 * we can have dead inode here. */
230 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
231 make_bad_inode(inode);
233 inode = ERR_PTR(-ESTALE);
234 } else if (is_bad_inode(inode)) {
235 CWARN("%.16s: bad inode: ino = %u\n",
236 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
238 inode = ERR_PTR(-ENOENT);
240 if (id->oii_gen == OSD_OII_NOGEN)
241 osd_id_gen(id, inode->i_ino, inode->i_generation);
243 /* Do not update file c/mtime in ldiskfs.
244 * NB: we don't have any lock to protect this because we don't
245 * have reference on osd_object now, but contention with
246 * another lookup + attr_set can't happen in the tiny window
247 * between if (...) and set S_NOCMTIME. */
248 if (!(inode->i_flags & S_NOCMTIME))
249 inode->i_flags |= S_NOCMTIME;
254 static struct inode *
255 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
256 struct osd_inode_id *id, struct lu_fid *fid)
258 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
262 inode = osd_iget(info, dev, id);
266 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
268 *fid = lma->lma_self_fid;
269 } else if (rc == -ENODATA) {
270 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
271 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
273 lu_igif_build(fid, inode->i_ino, inode->i_generation);
281 static struct inode *
282 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
283 struct osd_inode_id *id, const struct lu_fid *fid)
285 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
289 inode = osd_iget(info, dev, id);
293 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
302 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
303 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
304 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
306 return ERR_PTR(-EREMCHG);
312 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
313 const struct lu_fid *fid,
314 const struct lu_object_conf *conf)
316 struct osd_thread_info *info;
317 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
318 struct osd_device *dev;
319 struct osd_idmap_cache *oic;
320 struct osd_inode_id *id;
322 struct osd_scrub *scrub;
323 struct scrub_file *sf;
328 LINVRNT(osd_invariant(obj));
329 LASSERT(obj->oo_inode == NULL);
330 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
333 scrub = &dev->od_scrub;
334 sf = &scrub->os_file;
335 info = osd_oti_get(env);
337 oic = &info->oti_cache;
339 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
342 /* Search order: 1. per-thread cache. */
343 if (lu_fid_eq(fid, &oic->oic_fid)) {
349 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
350 /* Search order: 2. OI scrub pending list. */
351 result = osd_oii_lookup(dev, fid, id);
356 if (sf->sf_flags & SF_INCONSISTENT)
360 * Objects are created as locking anchors or place holders for objects
361 * yet to be created. No need to osd_oi_lookup() at here because FID
362 * shouldn't never be re-used, if it's really a duplicate FID from
363 * unexpected reason, we should be able to detect it later by calling
364 * do_create->osd_oi_insert()
366 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
367 GOTO(out, result = 0);
369 /* Search order: 3. OI files. */
370 result = osd_oi_lookup(info, dev, fid, id, true);
371 if (result == -ENOENT) {
372 if (!fid_is_norm(fid) || fid_is_on_ost(info, dev, fid) ||
373 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
375 GOTO(out, result = 0);
385 inode = osd_iget(info, dev, id);
387 inode = osd_iget_verify(info, dev, id, fid);
389 result = PTR_ERR(inode);
390 if (result == -ENOENT || result == -ESTALE) {
391 fid_zero(&oic->oic_fid);
393 } else if (result == -EREMCHG) {
396 if (thread_is_running(&scrub->os_thread)) {
397 result = -EINPROGRESS;
398 } else if (!dev->od_noscrub) {
399 result = osd_scrub_start(dev);
400 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
401 "for "DFID", rc = %d [1]\n",
402 LDISKFS_SB(osd_sb(dev))->s_es->\
403 s_volume_name,PFID(fid), result);
404 if (result == 0 || result == -EALREADY)
405 result = -EINPROGRESS;
414 obj->oo_inode = inode;
415 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
417 obj->oo_compat_dot_created = 1;
418 obj->oo_compat_dotdot_created = 1;
420 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
421 GOTO(out, result = 0);
423 LASSERT(obj->oo_hl_head == NULL);
424 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
425 if (obj->oo_hl_head == NULL) {
426 obj->oo_inode = NULL;
428 GOTO(out, result = -ENOMEM);
430 GOTO(out, result = 0);
433 LINVRNT(osd_invariant(obj));
438 * Concurrency: shouldn't matter.
440 static void osd_object_init0(struct osd_object *obj)
442 LASSERT(obj->oo_inode != NULL);
443 obj->oo_dt.do_body_ops = &osd_body_ops;
444 obj->oo_dt.do_lu.lo_header->loh_attr |=
445 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
448 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
450 struct osd_thread_info *info = osd_oti_get(env);
451 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
455 CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
456 rc = __osd_xattr_get(obj->oo_inode, &info->oti_obj_dentry,
457 XATTR_NAME_LMA, info->oti_mdt_attrs_old,
461 lustre_lma_swab(lma);
462 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
463 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
464 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
465 "fid = "DFID", ino = %lu\n",
466 osd_obj2dev(obj)->od_svname,
467 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
468 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
469 obj->oo_inode->i_ino);
472 } else if (rc == -ENODATA) {
473 /* haven't initialize LMA xattr */
481 * Concurrency: no concurrent access is possible that early in object
484 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
485 const struct lu_object_conf *conf)
487 struct osd_object *obj = osd_obj(l);
490 LINVRNT(osd_invariant(obj));
492 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
493 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
494 l->lo_header->loh_attr |= LOHA_EXISTS;
498 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
499 obj->oo_dt.do_body_ops = &osd_body_ops_new;
500 if (result == 0 && obj->oo_inode != NULL) {
501 osd_object_init0(obj);
502 result = osd_check_lma(env, obj);
505 LINVRNT(osd_invariant(obj));
510 * Concurrency: no concurrent access is possible that late in object
513 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
515 struct osd_object *obj = osd_obj(l);
517 LINVRNT(osd_invariant(obj));
519 dt_object_fini(&obj->oo_dt);
520 if (obj->oo_hl_head != NULL)
521 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
526 * Concurrency: no concurrent access is possible that late in object
529 static void osd_index_fini(struct osd_object *o)
531 struct iam_container *bag;
533 if (o->oo_dir != NULL) {
534 bag = &o->oo_dir->od_container;
535 if (o->oo_inode != NULL) {
536 if (bag->ic_object == o->oo_inode)
537 iam_container_fini(bag);
539 OBD_FREE_PTR(o->oo_dir);
545 * Concurrency: no concurrent access is possible that late in object
546 * life-cycle (for all existing callers, that is. New callers have to provide
547 * their own locking.)
549 static int osd_inode_unlinked(const struct inode *inode)
551 return inode->i_nlink == 0;
555 OSD_TXN_OI_DELETE_CREDITS = 20,
556 OSD_TXN_INODE_DELETE_CREDITS = 20
563 #if OSD_THANDLE_STATS
565 * Set time when the handle is allocated
567 static void osd_th_alloced(struct osd_thandle *oth)
569 oth->oth_alloced = cfs_time_current();
573 * Set time when the handle started
575 static void osd_th_started(struct osd_thandle *oth)
577 oth->oth_started = cfs_time_current();
581 * Helper function to convert time interval to microseconds packed in
584 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
588 cfs_duration_usec(cfs_time_sub(end, start), &val);
589 return val.tv_sec * 1000000 + val.tv_usec;
593 * Check whether the we deal with this handle for too long.
595 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
596 cfs_time_t alloced, cfs_time_t started,
599 cfs_time_t now = cfs_time_current();
601 LASSERT(dev != NULL);
603 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
604 interval_to_usec(alloced, started));
605 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
606 interval_to_usec(started, closed));
607 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
608 interval_to_usec(closed, now));
610 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
611 CWARN("transaction handle %p was open for too long: "
613 "alloced "CFS_TIME_T" ,"
614 "started "CFS_TIME_T" ,"
615 "closed "CFS_TIME_T"\n",
616 oth, now, alloced, started, closed);
617 libcfs_debug_dumpstack(NULL);
621 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
623 cfs_time_t __closed = cfs_time_current(); \
624 cfs_time_t __alloced = oth->oth_alloced; \
625 cfs_time_t __started = oth->oth_started; \
628 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
631 #else /* OSD_THANDLE_STATS */
633 #define osd_th_alloced(h) do {} while(0)
634 #define osd_th_started(h) do {} while(0)
635 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
637 #endif /* OSD_THANDLE_STATS */
640 * Concurrency: doesn't access mutable data.
642 static int osd_param_is_not_sane(const struct osd_device *dev,
643 const struct thandle *th)
645 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
647 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
651 * Concurrency: shouldn't matter.
653 static void osd_trans_commit_cb(struct super_block *sb,
654 struct ldiskfs_journal_cb_entry *jcb, int error)
656 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
657 struct thandle *th = &oh->ot_super;
658 struct lu_device *lud = &th->th_dev->dd_lu_dev;
659 struct dt_txn_commit_cb *dcb, *tmp;
661 LASSERT(oh->ot_handle == NULL);
664 CERROR("transaction @0x%p commit error: %d\n", th, error);
666 dt_txn_hook_commit(th);
668 /* call per-transaction callbacks if any */
669 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
670 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
671 "commit callback entry: magic=%x name='%s'\n",
672 dcb->dcb_magic, dcb->dcb_name);
673 cfs_list_del_init(&dcb->dcb_linkage);
674 dcb->dcb_func(NULL, th, dcb, error);
677 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
681 lu_context_exit(&th->th_ctx);
682 lu_context_fini(&th->th_ctx);
686 static struct thandle *osd_trans_create(const struct lu_env *env,
689 struct osd_thread_info *oti = osd_oti_get(env);
690 struct osd_iobuf *iobuf = &oti->oti_iobuf;
691 struct osd_thandle *oh;
695 /* on pending IO in this thread should left from prev. request */
696 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
698 th = ERR_PTR(-ENOMEM);
699 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
701 oh->ot_quota_trans = &oti->oti_quota_trans;
702 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
706 th->th_tags = LCT_TX_HANDLE;
708 oti->oti_dev = osd_dt_dev(d);
709 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
712 #ifdef OSD_TRACK_DECLARES
713 memset(oti->oti_declare_ops, 0,
714 sizeof(oti->oti_declare_ops));
715 memset(oti->oti_declare_ops_rb, 0,
716 sizeof(oti->oti_declare_ops_rb));
717 memset(oti->oti_declare_ops_cred, 0,
718 sizeof(oti->oti_declare_ops_cred));
719 oti->oti_rollback = false;
726 * Concurrency: shouldn't matter.
728 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
731 struct osd_thread_info *oti = osd_oti_get(env);
732 struct osd_device *dev = osd_dt_dev(d);
734 struct osd_thandle *oh;
739 LASSERT(current->journal_info == NULL);
741 oh = container_of0(th, struct osd_thandle, ot_super);
743 LASSERT(oh->ot_handle == NULL);
745 rc = dt_txn_hook_start(env, d, th);
749 if (unlikely(osd_param_is_not_sane(dev, th))) {
750 #ifdef OSD_TRACK_DECLARES
751 static unsigned long last_printed;
752 static int last_credits;
755 CWARN("%.16s: too many transaction credits (%d > %d)\n",
756 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
758 osd_journal(dev)->j_max_transaction_buffers);
759 #ifdef OSD_TRACK_DECLARES
760 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
761 oti->oti_declare_ops[OSD_OT_CREATE],
762 oti->oti_declare_ops_cred[OSD_OT_CREATE],
763 oti->oti_declare_ops[OSD_OT_DELETE],
764 oti->oti_declare_ops_cred[OSD_OT_DELETE],
765 oti->oti_declare_ops[OSD_OT_DESTROY],
766 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
767 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
768 oti->oti_declare_ops[OSD_OT_ATTR_SET],
769 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
770 oti->oti_declare_ops[OSD_OT_XATTR_SET],
771 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
772 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
773 oti->oti_declare_ops[OSD_OT_WRITE],
774 oti->oti_declare_ops_cred[OSD_OT_WRITE],
775 oti->oti_declare_ops[OSD_OT_PUNCH],
776 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
777 oti->oti_declare_ops[OSD_OT_QUOTA],
778 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
779 CWARN(" insert: %u/%u, delete: %u/%u\n",
780 oti->oti_declare_ops[OSD_OT_INSERT],
781 oti->oti_declare_ops_cred[OSD_OT_INSERT],
782 oti->oti_declare_ops[OSD_OT_DESTROY],
783 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
784 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
785 oti->oti_declare_ops[OSD_OT_REF_ADD],
786 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
787 oti->oti_declare_ops[OSD_OT_REF_DEL],
788 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
790 if (last_credits != oh->ot_credits &&
791 time_after(jiffies, last_printed + 60 * HZ)) {
792 libcfs_debug_dumpstack(NULL);
793 last_credits = oh->ot_credits;
794 last_printed = jiffies;
797 /* XXX Limit the credits to 'max_transaction_buffers', and
798 * let the underlying filesystem to catch the error if
799 * we really need so many credits.
801 * This should be removed when we can calculate the
802 * credits precisely. */
803 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
807 * XXX temporary stuff. Some abstraction layer should
810 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
814 LASSERT(oti->oti_txns == 0);
815 lu_context_init(&th->th_ctx, th->th_tags);
816 lu_context_enter(&th->th_ctx);
818 lu_device_get(&d->dd_lu_dev);
819 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
831 * Concurrency: shouldn't matter.
833 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
836 struct osd_thandle *oh;
837 struct osd_thread_info *oti = osd_oti_get(env);
838 struct osd_iobuf *iobuf = &oti->oti_iobuf;
839 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
842 oh = container_of0(th, struct osd_thandle, ot_super);
845 /* inform the quota slave device that the transaction is
847 qsd_op_end(env, qsd, oh->ot_quota_trans);
848 oh->ot_quota_trans = NULL;
850 if (oh->ot_handle != NULL) {
851 handle_t *hdl = oh->ot_handle;
854 * add commit callback
855 * notice we don't do this in osd_trans_start()
856 * as underlying transaction can change during truncate
858 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
861 LASSERT(oti->oti_txns == 1);
863 rc = dt_txn_hook_stop(env, th);
865 CERROR("Failure in transaction hook: %d\n", rc);
867 /* hook functions might modify th_sync */
868 hdl->h_sync = th->th_sync;
870 oh->ot_handle = NULL;
871 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
872 rc = ldiskfs_journal_stop(hdl));
874 CERROR("Failure to stop transaction: %d\n", rc);
879 /* as we want IO to journal and data IO be concurrent, we don't block
880 * awaiting data IO completion in osd_do_bio(), instead we wait here
881 * once transaction is submitted to the journal. all reqular requests
882 * don't do direct IO (except read/write), thus this wait_event becomes
885 * IMPORTANT: we have to wait till any IO submited by the thread is
886 * completed otherwise iobuf may be corrupted by different request
888 cfs_wait_event(iobuf->dr_wait,
889 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
891 rc = iobuf->dr_error;
896 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
898 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
901 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
902 LASSERT(&dcb->dcb_func != NULL);
903 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
909 * Called just before object is freed. Releases all resources except for
910 * object itself (that is released by osd_object_free()).
912 * Concurrency: no concurrent access is possible that late in object
915 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
917 struct osd_object *obj = osd_obj(l);
918 struct inode *inode = obj->oo_inode;
920 LINVRNT(osd_invariant(obj));
923 * If object is unlinked remove fid->ino mapping from object index.
928 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
929 qid_t uid = inode->i_uid;
930 qid_t gid = inode->i_gid;
933 obj->oo_inode = NULL;
936 struct osd_thread_info *info = osd_oti_get(env);
937 struct lquota_id_info *qi = &info->oti_qi;
939 /* Release granted quota to master if necessary */
940 qi->lqi_id.qid_uid = uid;
941 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
943 qi->lqi_id.qid_uid = gid;
944 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
950 * Concurrency: ->loo_object_release() is called under site spin-lock.
952 static void osd_object_release(const struct lu_env *env,
958 * Concurrency: shouldn't matter.
960 static int osd_object_print(const struct lu_env *env, void *cookie,
961 lu_printer_t p, const struct lu_object *l)
963 struct osd_object *o = osd_obj(l);
966 if (o->oo_dir != NULL)
967 d = o->oo_dir->od_container.ic_descr;
970 return (*p)(env, cookie,
971 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
973 o->oo_inode ? o->oo_inode->i_ino : 0UL,
974 o->oo_inode ? o->oo_inode->i_generation : 0,
975 d ? d->id_ops->id_name : "plain");
979 * Concurrency: shouldn't matter.
981 int osd_statfs(const struct lu_env *env, struct dt_device *d,
982 struct obd_statfs *sfs)
984 struct osd_device *osd = osd_dt_dev(d);
985 struct super_block *sb = osd_sb(osd);
986 struct kstatfs *ksfs;
989 if (unlikely(osd->od_mnt == NULL))
992 /* osd_lproc.c call this without env, allocate ksfs for that case */
993 if (unlikely(env == NULL)) {
998 ksfs = &osd_oti_get(env)->oti_ksfs;
1001 spin_lock(&osd->od_osfs_lock);
1002 /* cache 1 second */
1003 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1004 result = sb->s_op->statfs(sb->s_root, ksfs);
1005 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1006 osd->od_osfs_age = cfs_time_current_64();
1007 statfs_pack(&osd->od_statfs, ksfs);
1008 if (sb->s_flags & MS_RDONLY)
1009 sfs->os_state = OS_STATE_READONLY;
1013 if (likely(result == 0))
1014 *sfs = osd->od_statfs;
1015 spin_unlock(&osd->od_osfs_lock);
1017 if (unlikely(env == NULL))
1024 * Estimate space needed for file creations. We assume the largest filename
1025 * which is 2^64 - 1, hence a filename of 20 chars.
1026 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1028 #ifdef __LDISKFS_DIR_REC_LEN
1029 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1031 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1035 * Concurrency: doesn't access mutable data.
1037 static void osd_conf_get(const struct lu_env *env,
1038 const struct dt_device *dev,
1039 struct dt_device_param *param)
1041 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1044 * XXX should be taken from not-yet-existing fs abstraction layer.
1046 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1047 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1048 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1049 param->ddp_block_shift = sb->s_blocksize_bits;
1050 param->ddp_mount_type = LDD_MT_LDISKFS;
1051 param->ddp_maxbytes = sb->s_maxbytes;
1052 /* Overhead estimate should be fairly accurate, so we really take a tiny
1053 * error margin which also avoids fragmenting the filesystem too much */
1054 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1055 /* inode are statically allocated, so per-inode space consumption
1056 * is the space consumed by the directory entry */
1057 param->ddp_inodespace = PER_OBJ_USAGE;
1058 /* per-fragment overhead to be used by the client code */
1059 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1060 param->ddp_mntopts = 0;
1061 if (test_opt(sb, XATTR_USER))
1062 param->ddp_mntopts |= MNTOPT_USERXATTR;
1063 if (test_opt(sb, POSIX_ACL))
1064 param->ddp_mntopts |= MNTOPT_ACL;
1066 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1067 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1068 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1071 param->ddp_max_ea_size = sb->s_blocksize;
1076 * Concurrency: shouldn't matter.
1078 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1080 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1081 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1085 * Start commit for OSD device.
1087 * An implementation of dt_commit_async method for OSD device.
1088 * Asychronously starts underlayng fs sync and thereby a transaction
1091 * \param env environment
1092 * \param d dt device
1094 * \see dt_device_operations
1096 static int osd_commit_async(const struct lu_env *env,
1097 struct dt_device *d)
1099 struct super_block *s = osd_sb(osd_dt_dev(d));
1102 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1103 RETURN(s->s_op->sync_fs(s, 0));
1107 * Concurrency: shouldn't matter.
1110 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1112 struct super_block *sb = osd_sb(osd_dt_dev(d));
1113 struct block_device *dev = sb->s_bdev;
1114 #ifdef HAVE_DEV_SET_RDONLY
1115 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1118 int rc = -EOPNOTSUPP;
1122 #ifdef HAVE_DEV_SET_RDONLY
1123 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1125 if (jdev && (jdev != dev)) {
1126 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1128 dev_set_rdonly(jdev);
1130 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1131 dev_set_rdonly(dev);
1133 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1134 osd_dt_dev(d)->od_svname, (long)dev, rc);
1140 * Concurrency: serialization provided by callers.
1142 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1143 int mode, unsigned long timeout, __u32 alg,
1144 struct lustre_capa_key *keys)
1146 struct osd_device *dev = osd_dt_dev(d);
1149 dev->od_fl_capa = mode;
1150 dev->od_capa_timeout = timeout;
1151 dev->od_capa_alg = alg;
1152 dev->od_capa_keys = keys;
1157 * Note: we do not count into QUOTA here.
1158 * If we mount with --data_journal we may need more.
1160 const int osd_dto_credits_noquota[DTO_NR] = {
1163 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1164 * SINGLEDATA_TRANS_BLOCKS(8)
1165 * XXX Note: maybe iam need more, since iam have more level than
1168 [DTO_INDEX_INSERT] = 16,
1169 [DTO_INDEX_DELETE] = 16,
1173 [DTO_INDEX_UPDATE] = 16,
1175 * Create a object. The same as create object in EXT3.
1176 * DATA_TRANS_BLOCKS(14) +
1177 * INDEX_EXTRA_BLOCKS(8) +
1178 * 3(inode bits, groups, GDT)
1180 [DTO_OBJECT_CREATE] = 25,
1182 * XXX: real credits to be fixed
1184 [DTO_OBJECT_DELETE] = 25,
1186 * Attr set credits (inode)
1188 [DTO_ATTR_SET_BASE] = 1,
1190 * Xattr set. The same as xattr of EXT3.
1191 * DATA_TRANS_BLOCKS(14)
1192 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1193 * are also counted in. Do not know why?
1195 [DTO_XATTR_SET] = 14,
1198 * credits for inode change during write.
1200 [DTO_WRITE_BASE] = 3,
1202 * credits for single block write.
1204 [DTO_WRITE_BLOCK] = 14,
1206 * Attr set credits for chown.
1207 * This is extra credits for setattr, and it is null without quota
1209 [DTO_ATTR_SET_CHOWN]= 0
1212 static const struct dt_device_operations osd_dt_ops = {
1213 .dt_root_get = osd_root_get,
1214 .dt_statfs = osd_statfs,
1215 .dt_trans_create = osd_trans_create,
1216 .dt_trans_start = osd_trans_start,
1217 .dt_trans_stop = osd_trans_stop,
1218 .dt_trans_cb_add = osd_trans_cb_add,
1219 .dt_conf_get = osd_conf_get,
1220 .dt_sync = osd_sync,
1222 .dt_commit_async = osd_commit_async,
1223 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1226 static void osd_object_read_lock(const struct lu_env *env,
1227 struct dt_object *dt, unsigned role)
1229 struct osd_object *obj = osd_dt_obj(dt);
1230 struct osd_thread_info *oti = osd_oti_get(env);
1232 LINVRNT(osd_invariant(obj));
1234 LASSERT(obj->oo_owner != env);
1235 down_read_nested(&obj->oo_sem, role);
1237 LASSERT(obj->oo_owner == NULL);
1241 static void osd_object_write_lock(const struct lu_env *env,
1242 struct dt_object *dt, unsigned role)
1244 struct osd_object *obj = osd_dt_obj(dt);
1245 struct osd_thread_info *oti = osd_oti_get(env);
1247 LINVRNT(osd_invariant(obj));
1249 LASSERT(obj->oo_owner != env);
1250 down_write_nested(&obj->oo_sem, role);
1252 LASSERT(obj->oo_owner == NULL);
1253 obj->oo_owner = env;
1257 static void osd_object_read_unlock(const struct lu_env *env,
1258 struct dt_object *dt)
1260 struct osd_object *obj = osd_dt_obj(dt);
1261 struct osd_thread_info *oti = osd_oti_get(env);
1263 LINVRNT(osd_invariant(obj));
1265 LASSERT(oti->oti_r_locks > 0);
1267 up_read(&obj->oo_sem);
1270 static void osd_object_write_unlock(const struct lu_env *env,
1271 struct dt_object *dt)
1273 struct osd_object *obj = osd_dt_obj(dt);
1274 struct osd_thread_info *oti = osd_oti_get(env);
1276 LINVRNT(osd_invariant(obj));
1278 LASSERT(obj->oo_owner == env);
1279 LASSERT(oti->oti_w_locks > 0);
1281 obj->oo_owner = NULL;
1282 up_write(&obj->oo_sem);
1285 static int osd_object_write_locked(const struct lu_env *env,
1286 struct dt_object *dt)
1288 struct osd_object *obj = osd_dt_obj(dt);
1290 LINVRNT(osd_invariant(obj));
1292 return obj->oo_owner == env;
1295 static int capa_is_sane(const struct lu_env *env,
1296 struct osd_device *dev,
1297 struct lustre_capa *capa,
1298 struct lustre_capa_key *keys)
1300 struct osd_thread_info *oti = osd_oti_get(env);
1301 struct lustre_capa *tcapa = &oti->oti_capa;
1302 struct obd_capa *oc;
1306 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1308 if (capa_is_expired(oc)) {
1309 DEBUG_CAPA(D_ERROR, capa, "expired");
1316 if (capa_is_expired_sec(capa)) {
1317 DEBUG_CAPA(D_ERROR, capa, "expired");
1321 spin_lock(&capa_lock);
1322 for (i = 0; i < 2; i++) {
1323 if (keys[i].lk_keyid == capa->lc_keyid) {
1324 oti->oti_capa_key = keys[i];
1328 spin_unlock(&capa_lock);
1331 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1335 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1339 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1340 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1344 oc = capa_add(dev->od_capa_hash, capa);
1350 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1351 struct lustre_capa *capa, __u64 opc)
1353 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1354 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1355 struct md_capainfo *ci;
1358 if (!dev->od_fl_capa)
1361 if (capa == BYPASS_CAPA)
1364 ci = md_capainfo(env);
1368 if (ci->mc_auth == LC_ID_NONE)
1372 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1376 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1377 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1382 if (!capa_opc_supported(capa, opc)) {
1383 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1387 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1388 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1395 static struct timespec *osd_inode_time(const struct lu_env *env,
1396 struct inode *inode, __u64 seconds)
1398 struct osd_thread_info *oti = osd_oti_get(env);
1399 struct timespec *t = &oti->oti_time;
1401 t->tv_sec = seconds;
1403 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1408 static void osd_inode_getattr(const struct lu_env *env,
1409 struct inode *inode, struct lu_attr *attr)
1411 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1412 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1413 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1416 attr->la_atime = LTIME_S(inode->i_atime);
1417 attr->la_mtime = LTIME_S(inode->i_mtime);
1418 attr->la_ctime = LTIME_S(inode->i_ctime);
1419 attr->la_mode = inode->i_mode;
1420 attr->la_size = i_size_read(inode);
1421 attr->la_blocks = inode->i_blocks;
1422 attr->la_uid = inode->i_uid;
1423 attr->la_gid = inode->i_gid;
1424 attr->la_flags = LDISKFS_I(inode)->i_flags;
1425 attr->la_nlink = inode->i_nlink;
1426 attr->la_rdev = inode->i_rdev;
1427 attr->la_blksize = 1 << inode->i_blkbits;
1428 attr->la_blkbits = inode->i_blkbits;
1431 static int osd_attr_get(const struct lu_env *env,
1432 struct dt_object *dt,
1433 struct lu_attr *attr,
1434 struct lustre_capa *capa)
1436 struct osd_object *obj = osd_dt_obj(dt);
1438 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1439 LINVRNT(osd_invariant(obj));
1441 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1444 spin_lock(&obj->oo_guard);
1445 osd_inode_getattr(env, obj->oo_inode, attr);
1446 spin_unlock(&obj->oo_guard);
1450 static int osd_declare_attr_set(const struct lu_env *env,
1451 struct dt_object *dt,
1452 const struct lu_attr *attr,
1453 struct thandle *handle)
1455 struct osd_thandle *oh;
1456 struct osd_object *obj;
1457 struct osd_thread_info *info = osd_oti_get(env);
1458 struct lquota_id_info *qi = &info->oti_qi;
1464 LASSERT(dt != NULL);
1465 LASSERT(handle != NULL);
1467 obj = osd_dt_obj(dt);
1468 LASSERT(osd_invariant(obj));
1470 oh = container_of0(handle, struct osd_thandle, ot_super);
1471 LASSERT(oh->ot_handle == NULL);
1473 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1474 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1476 if (attr == NULL || obj->oo_inode == NULL)
1479 bspace = obj->oo_inode->i_blocks;
1480 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1481 bspace = toqb(bspace);
1483 /* Changing ownership is always preformed by super user, it should not
1486 * We still need to call the osd_declare_qid() to calculate the journal
1487 * credits for updating quota accounting files and to trigger quota
1488 * space adjustment once the operation is completed.*/
1489 if ((attr->la_valid & LA_UID) != 0 &&
1490 attr->la_uid != obj->oo_inode->i_uid) {
1491 qi->lqi_type = USRQUOTA;
1493 /* inode accounting */
1494 qi->lqi_is_blk = false;
1496 /* one more inode for the new owner ... */
1497 qi->lqi_id.qid_uid = attr->la_uid;
1499 allocated = (attr->la_uid == 0) ? true : false;
1500 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1501 if (rc == -EDQUOT || rc == -EINPROGRESS)
1506 /* and one less inode for the current uid */
1507 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1509 rc = osd_declare_qid(env, oh, qi, true, NULL);
1510 if (rc == -EDQUOT || rc == -EINPROGRESS)
1515 /* block accounting */
1516 qi->lqi_is_blk = true;
1518 /* more blocks for the new owner ... */
1519 qi->lqi_id.qid_uid = attr->la_uid;
1520 qi->lqi_space = bspace;
1521 allocated = (attr->la_uid == 0) ? true : false;
1522 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1523 if (rc == -EDQUOT || rc == -EINPROGRESS)
1528 /* and finally less blocks for the current owner */
1529 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1530 qi->lqi_space = -bspace;
1531 rc = osd_declare_qid(env, oh, qi, true, NULL);
1532 if (rc == -EDQUOT || rc == -EINPROGRESS)
1538 if (attr->la_valid & LA_GID &&
1539 attr->la_gid != obj->oo_inode->i_gid) {
1540 qi->lqi_type = GRPQUOTA;
1542 /* inode accounting */
1543 qi->lqi_is_blk = false;
1545 /* one more inode for the new group owner ... */
1546 qi->lqi_id.qid_gid = attr->la_gid;
1548 allocated = (attr->la_gid == 0) ? true : false;
1549 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1550 if (rc == -EDQUOT || rc == -EINPROGRESS)
1555 /* and one less inode for the current gid */
1556 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1558 rc = osd_declare_qid(env, oh, qi, true, NULL);
1559 if (rc == -EDQUOT || rc == -EINPROGRESS)
1564 /* block accounting */
1565 qi->lqi_is_blk = true;
1567 /* more blocks for the new owner ... */
1568 qi->lqi_id.qid_gid = attr->la_gid;
1569 qi->lqi_space = bspace;
1570 allocated = (attr->la_gid == 0) ? true : false;
1571 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1572 if (rc == -EDQUOT || rc == -EINPROGRESS)
1577 /* and finally less blocks for the current owner */
1578 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1579 qi->lqi_space = -bspace;
1580 rc = osd_declare_qid(env, oh, qi, true, NULL);
1581 if (rc == -EDQUOT || rc == -EINPROGRESS)
1590 static int osd_inode_setattr(const struct lu_env *env,
1591 struct inode *inode, const struct lu_attr *attr)
1595 bits = attr->la_valid;
1597 if (bits & LA_ATIME)
1598 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1599 if (bits & LA_CTIME)
1600 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1601 if (bits & LA_MTIME)
1602 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1603 if (bits & LA_SIZE) {
1604 LDISKFS_I(inode)->i_disksize = attr->la_size;
1605 i_size_write(inode, attr->la_size);
1609 /* OSD should not change "i_blocks" which is used by quota.
1610 * "i_blocks" should be changed by ldiskfs only. */
1611 if (bits & LA_BLOCKS)
1612 inode->i_blocks = attr->la_blocks;
1615 inode->i_mode = (inode->i_mode & S_IFMT) |
1616 (attr->la_mode & ~S_IFMT);
1618 inode->i_uid = attr->la_uid;
1620 inode->i_gid = attr->la_gid;
1621 if (bits & LA_NLINK)
1622 set_nlink(inode, attr->la_nlink);
1624 inode->i_rdev = attr->la_rdev;
1626 if (bits & LA_FLAGS) {
1627 /* always keep S_NOCMTIME */
1628 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1634 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1636 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1637 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1642 if (attr->la_valid & LA_UID)
1643 iattr.ia_valid |= ATTR_UID;
1644 if (attr->la_valid & LA_GID)
1645 iattr.ia_valid |= ATTR_GID;
1646 iattr.ia_uid = attr->la_uid;
1647 iattr.ia_gid = attr->la_gid;
1649 rc = ll_vfs_dq_transfer(inode, &iattr);
1651 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1652 "enforcement enabled on the ldiskfs filesystem?",
1653 inode->i_sb->s_id, rc);
1660 static int osd_attr_set(const struct lu_env *env,
1661 struct dt_object *dt,
1662 const struct lu_attr *attr,
1663 struct thandle *handle,
1664 struct lustre_capa *capa)
1666 struct osd_object *obj = osd_dt_obj(dt);
1667 struct inode *inode;
1670 LASSERT(handle != NULL);
1671 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1672 LASSERT(osd_invariant(obj));
1674 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1677 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1679 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1680 struct osd_thread_info *oti = osd_oti_get(env);
1681 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1682 struct lu_fid *fid1 = &oti->oti_fid;
1683 struct osd_inode_id *id = &oti->oti_id;
1684 struct iam_path_descr *ipd;
1685 struct iam_container *bag;
1686 struct osd_thandle *oh;
1689 fid_cpu_to_be(fid1, fid0);
1690 memset(id, 1, sizeof(*id));
1691 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1692 fid0)->oi_dir.od_container;
1693 ipd = osd_idx_ipd_get(env, bag);
1694 if (unlikely(ipd == NULL))
1697 oh = container_of0(handle, struct osd_thandle, ot_super);
1698 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1699 (const struct iam_rec *)id, ipd);
1700 osd_ipd_put(env, bag, ipd);
1701 return(rc > 0 ? 0 : rc);
1704 inode = obj->oo_inode;
1705 ll_vfs_dq_init(inode);
1707 rc = osd_quota_transfer(inode, attr);
1711 spin_lock(&obj->oo_guard);
1712 rc = osd_inode_setattr(env, inode, attr);
1713 spin_unlock(&obj->oo_guard);
1716 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1720 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1721 struct osd_object *obj,
1722 const char *name, const int namelen)
1724 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1727 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1729 struct dt_allocation_hint *hint,
1733 struct osd_device *osd = osd_obj2dev(obj);
1734 struct osd_thandle *oth;
1735 struct dt_object *parent = NULL;
1736 struct inode *inode;
1738 LINVRNT(osd_invariant(obj));
1739 LASSERT(obj->oo_inode == NULL);
1740 LASSERT(obj->oo_hl_head == NULL);
1742 if (S_ISDIR(mode) && ldiskfs_pdo) {
1743 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1744 if (obj->oo_hl_head == NULL)
1748 oth = container_of(th, struct osd_thandle, ot_super);
1749 LASSERT(oth->ot_handle->h_transaction != NULL);
1751 if (hint && hint->dah_parent)
1752 parent = hint->dah_parent;
1754 inode = ldiskfs_create_inode(oth->ot_handle,
1755 parent ? osd_dt_obj(parent)->oo_inode :
1756 osd_sb(osd)->s_root->d_inode,
1758 if (!IS_ERR(inode)) {
1759 /* Do not update file c/mtime in ldiskfs.
1760 * NB: don't need any lock because no contention at this
1762 inode->i_flags |= S_NOCMTIME;
1764 /* For new created object, it must be consistent,
1765 * and it is unnecessary to scrub against it. */
1766 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1767 obj->oo_inode = inode;
1770 if (obj->oo_hl_head != NULL) {
1771 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1772 obj->oo_hl_head = NULL;
1774 result = PTR_ERR(inode);
1776 LINVRNT(osd_invariant(obj));
1784 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1785 struct lu_attr *attr,
1786 struct dt_allocation_hint *hint,
1787 struct dt_object_format *dof,
1791 struct osd_thandle *oth;
1792 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1794 LASSERT(S_ISDIR(attr->la_mode));
1796 oth = container_of(th, struct osd_thandle, ot_super);
1797 LASSERT(oth->ot_handle->h_transaction != NULL);
1798 result = osd_mkfile(info, obj, mode, hint, th);
1803 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1804 struct lu_attr *attr,
1805 struct dt_allocation_hint *hint,
1806 struct dt_object_format *dof,
1810 struct osd_thandle *oth;
1811 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1813 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1815 LASSERT(S_ISREG(attr->la_mode));
1817 oth = container_of(th, struct osd_thandle, ot_super);
1818 LASSERT(oth->ot_handle->h_transaction != NULL);
1820 result = osd_mkfile(info, obj, mode, hint, th);
1822 LASSERT(obj->oo_inode != NULL);
1823 if (feat->dif_flags & DT_IND_VARKEY)
1824 result = iam_lvar_create(obj->oo_inode,
1825 feat->dif_keysize_max,
1827 feat->dif_recsize_max,
1830 result = iam_lfix_create(obj->oo_inode,
1831 feat->dif_keysize_max,
1833 feat->dif_recsize_max,
1840 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1841 struct lu_attr *attr,
1842 struct dt_allocation_hint *hint,
1843 struct dt_object_format *dof,
1846 LASSERT(S_ISREG(attr->la_mode));
1847 return osd_mkfile(info, obj, (attr->la_mode &
1848 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1851 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1852 struct lu_attr *attr,
1853 struct dt_allocation_hint *hint,
1854 struct dt_object_format *dof,
1857 LASSERT(S_ISLNK(attr->la_mode));
1858 return osd_mkfile(info, obj, (attr->la_mode &
1859 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1862 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1863 struct lu_attr *attr,
1864 struct dt_allocation_hint *hint,
1865 struct dt_object_format *dof,
1868 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1871 LINVRNT(osd_invariant(obj));
1872 LASSERT(obj->oo_inode == NULL);
1873 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1874 S_ISFIFO(mode) || S_ISSOCK(mode));
1876 result = osd_mkfile(info, obj, mode, hint, th);
1878 LASSERT(obj->oo_inode != NULL);
1880 * This inode should be marked dirty for i_rdev. Currently
1881 * that is done in the osd_attr_init().
1883 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1886 LINVRNT(osd_invariant(obj));
1890 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1892 struct dt_allocation_hint *hint,
1893 struct dt_object_format *dof,
1896 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1898 osd_obj_type_f result;
1914 result = osd_mk_index;
1925 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1926 struct dt_object *parent, struct dt_object *child,
1927 cfs_umode_t child_mode)
1931 memset(ah, 0, sizeof(*ah));
1932 ah->dah_parent = parent;
1933 ah->dah_mode = child_mode;
1936 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1937 struct lu_attr *attr, struct dt_object_format *dof)
1939 struct inode *inode = obj->oo_inode;
1940 __u64 valid = attr->la_valid;
1943 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1945 if (dof->dof_type != DFT_NODE)
1946 attr->la_valid &= ~LA_RDEV;
1947 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1948 attr->la_valid &= ~LA_ATIME;
1949 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1950 attr->la_valid &= ~LA_CTIME;
1951 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1952 attr->la_valid &= ~LA_MTIME;
1954 result = osd_quota_transfer(inode, attr);
1958 if (attr->la_valid != 0) {
1959 result = osd_inode_setattr(info->oti_env, inode, attr);
1961 * The osd_inode_setattr() should always succeed here. The
1962 * only error that could be returned is EDQUOT when we are
1963 * trying to change the UID or GID of the inode. However, this
1964 * should not happen since quota enforcement is no longer
1965 * enabled on ldiskfs (lquota takes care of it).
1967 LASSERTF(result == 0, "%d", result);
1968 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1971 attr->la_valid = valid;
1975 * Helper function for osd_object_create()
1977 * \retval 0, on success
1979 static int __osd_object_create(struct osd_thread_info *info,
1980 struct osd_object *obj, struct lu_attr *attr,
1981 struct dt_allocation_hint *hint,
1982 struct dt_object_format *dof,
1988 /* we drop umask so that permissions we pass are not affected */
1989 umask = current->fs->umask;
1990 current->fs->umask = 0;
1992 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1995 osd_attr_init(info, obj, attr, dof);
1996 osd_object_init0(obj);
1998 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1999 unlock_new_inode(obj->oo_inode);
2002 /* restore previous umask value */
2003 current->fs->umask = umask;
2009 * Helper function for osd_object_create()
2011 * \retval 0, on success
2013 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2014 const struct lu_fid *fid, struct thandle *th)
2016 struct osd_thread_info *info = osd_oti_get(env);
2017 struct osd_inode_id *id = &info->oti_id;
2018 struct osd_device *osd = osd_obj2dev(obj);
2020 LASSERT(obj->oo_inode != NULL);
2022 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2023 return osd_oi_insert(info, osd, fid, id, th);
2026 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2027 const struct lu_fid *fid, struct lu_seq_range *range)
2029 struct seq_server_site *ss = osd_seq_site(osd);
2032 if (fid_is_idif(fid)) {
2033 fld_range_set_ost(range);
2034 range->lsr_index = fid_idif_ost_idx(fid);
2038 if (!fid_seq_in_fldb(fid_seq(fid))) {
2039 fld_range_set_mdt(range);
2041 /* FIXME: If ss is NULL, it suppose not get lsr_index
2043 range->lsr_index = ss->ss_node_id;
2047 LASSERT(ss != NULL);
2048 fld_range_set_any(range);
2049 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
2051 CERROR("%s: cannot find FLD range for "DFID": rc = %d\n",
2052 osd_name(osd), PFID(fid), rc);
2058 * Concurrency: no external locking is necessary.
2060 static int osd_declare_object_create(const struct lu_env *env,
2061 struct dt_object *dt,
2062 struct lu_attr *attr,
2063 struct dt_allocation_hint *hint,
2064 struct dt_object_format *dof,
2065 struct thandle *handle)
2067 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2068 struct osd_thandle *oh;
2072 LASSERT(handle != NULL);
2074 oh = container_of0(handle, struct osd_thandle, ot_super);
2075 LASSERT(oh->ot_handle == NULL);
2077 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2078 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2079 if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2080 lu_object_fid(&dt->do_lu)))
2081 /* Reuse idle OI block may cause additional one OI block
2083 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2084 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2086 /* If this is directory, then we expect . and .. to be inserted as
2087 * well. The one directory block always needs to be created for the
2088 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2089 * block), there is no danger of needing a tree for the first block.
2091 if (attr && S_ISDIR(attr->la_mode)) {
2092 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2093 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2094 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2100 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2101 false, false, NULL, false);
2105 /* It does fld look up inside declare, and the result will be
2106 * added to fld cache, so the following fld lookup inside insert
2107 * does not need send RPC anymore, so avoid send rpc with holding
2109 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2110 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2111 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2112 lu_object_fid(&dt->do_lu), range);
2118 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2119 struct lu_attr *attr,
2120 struct dt_allocation_hint *hint,
2121 struct dt_object_format *dof,
2124 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2125 struct osd_object *obj = osd_dt_obj(dt);
2126 struct osd_thread_info *info = osd_oti_get(env);
2131 LINVRNT(osd_invariant(obj));
2132 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2133 LASSERT(osd_write_locked(env, obj));
2134 LASSERT(th != NULL);
2136 if (unlikely(fid_is_acct(fid)))
2137 /* Quota files can't be created from the kernel any more,
2138 * 'tune2fs -O quota' will take care of creating them */
2141 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2142 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2144 result = __osd_object_create(info, obj, attr, hint, dof, th);
2146 result = __osd_oi_insert(env, obj, fid, th);
2148 LASSERT(ergo(result == 0,
2149 dt_object_exists(dt) && !dt_object_remote(dt)));
2151 LASSERT(osd_invariant(obj));
2156 * Called to destroy on-disk representation of the object
2158 * Concurrency: must be locked
2160 static int osd_declare_object_destroy(const struct lu_env *env,
2161 struct dt_object *dt,
2164 struct osd_object *obj = osd_dt_obj(dt);
2165 struct inode *inode = obj->oo_inode;
2166 struct osd_thandle *oh;
2170 oh = container_of0(th, struct osd_thandle, ot_super);
2171 LASSERT(oh->ot_handle == NULL);
2174 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2175 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2176 /* Recycle idle OI leaf may cause additional three OI blocks
2178 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2179 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2180 /* one less inode */
2181 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2182 false, true, NULL, false);
2185 /* data to be truncated */
2186 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2187 true, true, NULL, false);
2191 static int osd_object_destroy(const struct lu_env *env,
2192 struct dt_object *dt,
2195 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2196 struct osd_object *obj = osd_dt_obj(dt);
2197 struct inode *inode = obj->oo_inode;
2198 struct osd_device *osd = osd_obj2dev(obj);
2199 struct osd_thandle *oh;
2203 oh = container_of0(th, struct osd_thandle, ot_super);
2204 LASSERT(oh->ot_handle);
2206 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2208 if (unlikely(fid_is_acct(fid)))
2211 if (S_ISDIR(inode->i_mode)) {
2212 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2213 /* it will check/delete the inode from remote parent,
2214 * how to optimize it? unlink performance impaction XXX */
2215 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2216 if (result != 0 && result != -ENOENT) {
2217 CERROR("%s: delete inode "DFID": rc = %d\n",
2218 osd_name(osd), PFID(fid), result);
2220 spin_lock(&obj->oo_guard);
2222 spin_unlock(&obj->oo_guard);
2223 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2226 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2228 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2230 /* XXX: add to ext3 orphan list */
2231 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2233 /* not needed in the cache anymore */
2234 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2240 * Put the fid into lustre_mdt_attrs, and then place the structure
2241 * inode's ea. This fid should not be altered during the life time
2244 * \retval +ve, on success
2245 * \retval -ve, on error
2247 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2249 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2250 const struct lu_fid *fid, __u64 flags)
2252 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2255 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2258 lustre_lma_init(lma, fid, flags);
2259 lustre_lma_swab(lma);
2261 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2263 /* Someone may created the EA by race. */
2264 if (unlikely(rc == -EEXIST))
2270 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2271 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2272 * To have compatilibility with 1.8 ldiskfs driver we need to have
2273 * magic number at start of fid data.
2274 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2277 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2278 const struct dt_rec *fid)
2280 if (!fid_is_namespace_visible((const struct lu_fid *)fid) ||
2281 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2282 param->edp_magic = 0;
2286 param->edp_magic = LDISKFS_LUFID_MAGIC;
2287 param->edp_len = sizeof(struct lu_fid) + 1;
2288 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2292 * Try to read the fid from inode ea into dt_rec.
2294 * \param fid object fid.
2296 * \retval 0 on success
2298 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2299 __u32 ino, struct lu_fid *fid,
2300 struct osd_inode_id *id)
2302 struct osd_thread_info *info = osd_oti_get(env);
2303 struct inode *inode;
2306 osd_id_gen(id, ino, OSD_OII_NOGEN);
2307 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2309 RETURN(PTR_ERR(inode));
2315 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2317 struct inode *parent_dir,
2318 const struct dt_rec *dot_fid,
2319 const struct dt_rec *dot_dot_fid,
2320 struct osd_thandle *oth)
2322 struct ldiskfs_dentry_param *dot_ldp;
2323 struct ldiskfs_dentry_param *dot_dot_ldp;
2325 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2326 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2328 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2329 dot_ldp->edp_magic = 0;
2330 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2331 dir, dot_ldp, dot_dot_ldp);
2335 * Create an local agent inode for remote entry
2337 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2338 struct osd_device *osd,
2339 struct osd_object *pobj,
2340 const struct lu_fid *fid,
2343 struct osd_thread_info *info = osd_oti_get(env);
2344 struct inode *local;
2345 struct osd_thandle *oh;
2350 oh = container_of(th, struct osd_thandle, ot_super);
2351 LASSERT(oh->ot_handle->h_transaction != NULL);
2353 /* FIXME: Insert index api needs to know the mode of
2354 * the remote object. Just use S_IFDIR for now */
2355 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2356 if (IS_ERR(local)) {
2357 CERROR("%s: create local error %d\n", osd_name(osd),
2358 (int)PTR_ERR(local));
2362 /* Set special LMA flag for local agent inode */
2363 rc = osd_ea_fid_set(info, local, fid, LMAI_AGENT);
2365 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2366 osd_name(osd), PFID(fid), rc);
2367 RETURN(ERR_PTR(rc));
2370 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2371 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2372 (const struct dt_rec *)fid, oh);
2374 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2375 osd_name(osd), PFID(fid), rc);
2376 RETURN(ERR_PTR(rc));
2383 * Delete local agent inode for remote entry
2385 static int osd_delete_local_agent_inode(const struct lu_env *env,
2386 struct osd_device *osd,
2387 const struct lu_fid *fid,
2388 __u32 ino, struct osd_thandle *oh)
2390 struct osd_thread_info *oti = osd_oti_get(env);
2391 struct osd_inode_id *id = &oti->oti_id;
2392 struct inode *inode;
2395 id->oii_ino = le32_to_cpu(ino);
2396 id->oii_gen = OSD_OII_NOGEN;
2397 inode = osd_iget(oti, osd, id);
2398 if (IS_ERR(inode)) {
2399 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2400 PFID(fid), id->oii_ino, id->oii_gen);
2401 RETURN(PTR_ERR(inode));
2405 mark_inode_dirty(inode);
2406 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2407 osd_name(osd), PFID(fid), inode->i_ino);
2413 * OSD layer object create function for interoperability mode (b11826).
2414 * This is mostly similar to osd_object_create(). Only difference being, fid is
2415 * inserted into inode ea here.
2417 * \retval 0, on success
2418 * \retval -ve, on error
2420 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2421 struct lu_attr *attr,
2422 struct dt_allocation_hint *hint,
2423 struct dt_object_format *dof,
2426 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2427 struct osd_object *obj = osd_dt_obj(dt);
2428 struct osd_thread_info *info = osd_oti_get(env);
2433 LASSERT(osd_invariant(obj));
2434 LASSERT(!dt_object_exists(dt) && !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 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2444 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2446 result = __osd_object_create(info, obj, attr, hint, dof, th);
2448 result = osd_ea_fid_set(info, obj->oo_inode, fid, 0);
2451 result = __osd_oi_insert(env, obj, fid, th);
2453 LASSERT(ergo(result == 0,
2454 dt_object_exists(dt) && !dt_object_remote(dt)));
2455 LINVRNT(osd_invariant(obj));
2459 static int osd_declare_object_ref_add(const struct lu_env *env,
2460 struct dt_object *dt,
2461 struct thandle *handle)
2463 struct osd_thandle *oh;
2465 /* it's possible that object doesn't exist yet */
2466 LASSERT(handle != NULL);
2468 oh = container_of0(handle, struct osd_thandle, ot_super);
2469 LASSERT(oh->ot_handle == NULL);
2471 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2472 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2478 * Concurrency: @dt is write locked.
2480 static int osd_object_ref_add(const struct lu_env *env,
2481 struct dt_object *dt, struct thandle *th)
2483 struct osd_object *obj = osd_dt_obj(dt);
2484 struct inode *inode = obj->oo_inode;
2486 LINVRNT(osd_invariant(obj));
2487 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2488 LASSERT(osd_write_locked(env, obj));
2489 LASSERT(th != NULL);
2491 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2494 * DIR_NLINK feature is set for compatibility reasons if:
2495 * 1) nlinks > LDISKFS_LINK_MAX, or
2496 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2498 * It is easier to always set this flag (rather than check and set),
2499 * since it has less overhead, and the superblock will be dirtied
2500 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2501 * do not actually care whether this flag is set or not.
2503 spin_lock(&obj->oo_guard);
2504 /* inc_nlink from 0 may cause WARN_ON */
2505 if(inode->i_nlink == 0)
2506 set_nlink(inode, 1);
2509 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2510 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2511 inode->i_nlink == 2)
2512 set_nlink(inode, 1);
2514 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2515 spin_unlock(&obj->oo_guard);
2516 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2517 LINVRNT(osd_invariant(obj));
2522 static int osd_declare_object_ref_del(const struct lu_env *env,
2523 struct dt_object *dt,
2524 struct thandle *handle)
2526 struct osd_thandle *oh;
2528 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2529 LASSERT(handle != NULL);
2531 oh = container_of0(handle, struct osd_thandle, ot_super);
2532 LASSERT(oh->ot_handle == NULL);
2534 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2535 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2541 * Concurrency: @dt is write locked.
2543 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2546 struct osd_object *obj = osd_dt_obj(dt);
2547 struct inode *inode = obj->oo_inode;
2549 LINVRNT(osd_invariant(obj));
2550 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2551 LASSERT(osd_write_locked(env, obj));
2552 LASSERT(th != NULL);
2554 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2556 spin_lock(&obj->oo_guard);
2557 LASSERT(inode->i_nlink > 0);
2559 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2560 * then the nlink count is 1. Don't let it be set to 0 or the directory
2561 * inode will be deleted incorrectly. */
2562 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2563 set_nlink(inode, 1);
2564 spin_unlock(&obj->oo_guard);
2565 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2566 LINVRNT(osd_invariant(obj));
2572 * Get the 64-bit version for an inode.
2574 static int osd_object_version_get(const struct lu_env *env,
2575 struct dt_object *dt, dt_obj_version_t *ver)
2577 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2579 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2580 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2581 *ver = LDISKFS_I(inode)->i_fs_version;
2586 * Concurrency: @dt is read locked.
2588 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2589 struct lu_buf *buf, const char *name,
2590 struct lustre_capa *capa)
2592 struct osd_object *obj = osd_dt_obj(dt);
2593 struct inode *inode = obj->oo_inode;
2594 struct osd_thread_info *info = osd_oti_get(env);
2595 struct dentry *dentry = &info->oti_obj_dentry;
2597 /* version get is not real XATTR but uses xattr API */
2598 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2599 /* for version we are just using xattr API but change inode
2601 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2602 osd_object_version_get(env, dt, buf->lb_buf);
2603 return sizeof(dt_obj_version_t);
2606 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2607 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2609 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2612 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2616 static int osd_declare_xattr_set(const struct lu_env *env,
2617 struct dt_object *dt,
2618 const struct lu_buf *buf, const char *name,
2619 int fl, struct thandle *handle)
2621 struct osd_thandle *oh;
2623 LASSERT(handle != NULL);
2625 oh = container_of0(handle, struct osd_thandle, ot_super);
2626 LASSERT(oh->ot_handle == NULL);
2628 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2629 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2630 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2631 osd_dto_credits_noquota[DTO_XATTR_SET]);
2637 * Set the 64-bit version for object
2639 static void osd_object_version_set(const struct lu_env *env,
2640 struct dt_object *dt,
2641 dt_obj_version_t *new_version)
2643 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2645 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2646 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2648 LDISKFS_I(inode)->i_fs_version = *new_version;
2649 /** Version is set after all inode operations are finished,
2650 * so we should mark it dirty here */
2651 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2655 * Concurrency: @dt is write locked.
2657 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2658 const struct lu_buf *buf, const char *name, int fl,
2659 struct thandle *handle, struct lustre_capa *capa)
2661 struct osd_object *obj = osd_dt_obj(dt);
2662 struct inode *inode = obj->oo_inode;
2663 struct osd_thread_info *info = osd_oti_get(env);
2667 LASSERT(handle != NULL);
2669 /* version set is not real XATTR */
2670 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2671 /* for version we are just using xattr API but change inode
2673 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2674 osd_object_version_set(env, dt, buf->lb_buf);
2675 return sizeof(dt_obj_version_t);
2678 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2681 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2682 if (fl & LU_XATTR_REPLACE)
2683 fs_flags |= XATTR_REPLACE;
2685 if (fl & LU_XATTR_CREATE)
2686 fs_flags |= XATTR_CREATE;
2688 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2693 * Concurrency: @dt is read locked.
2695 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2696 struct lu_buf *buf, struct lustre_capa *capa)
2698 struct osd_object *obj = osd_dt_obj(dt);
2699 struct inode *inode = obj->oo_inode;
2700 struct osd_thread_info *info = osd_oti_get(env);
2701 struct dentry *dentry = &info->oti_obj_dentry;
2703 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2704 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2705 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2707 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2710 dentry->d_inode = inode;
2711 dentry->d_sb = inode->i_sb;
2712 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2715 static int osd_declare_xattr_del(const struct lu_env *env,
2716 struct dt_object *dt, const char *name,
2717 struct thandle *handle)
2719 struct osd_thandle *oh;
2721 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2722 LASSERT(handle != NULL);
2724 oh = container_of0(handle, struct osd_thandle, ot_super);
2725 LASSERT(oh->ot_handle == NULL);
2727 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2728 osd_dto_credits_noquota[DTO_XATTR_SET]);
2734 * Concurrency: @dt is write locked.
2736 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2737 const char *name, struct thandle *handle,
2738 struct lustre_capa *capa)
2740 struct osd_object *obj = osd_dt_obj(dt);
2741 struct inode *inode = obj->oo_inode;
2742 struct osd_thread_info *info = osd_oti_get(env);
2743 struct dentry *dentry = &info->oti_obj_dentry;
2746 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2747 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2748 LASSERT(handle != NULL);
2750 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2753 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2755 ll_vfs_dq_init(inode);
2756 dentry->d_inode = inode;
2757 dentry->d_sb = inode->i_sb;
2758 rc = inode->i_op->removexattr(dentry, name);
2762 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2763 struct dt_object *dt,
2764 struct lustre_capa *old,
2767 struct osd_thread_info *info = osd_oti_get(env);
2768 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2769 struct osd_object *obj = osd_dt_obj(dt);
2770 struct osd_device *dev = osd_obj2dev(obj);
2771 struct lustre_capa_key *key = &info->oti_capa_key;
2772 struct lustre_capa *capa = &info->oti_capa;
2773 struct obd_capa *oc;
2774 struct md_capainfo *ci;
2778 if (!dev->od_fl_capa)
2779 RETURN(ERR_PTR(-ENOENT));
2781 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2782 LINVRNT(osd_invariant(obj));
2784 /* renewal sanity check */
2785 if (old && osd_object_auth(env, dt, old, opc))
2786 RETURN(ERR_PTR(-EACCES));
2788 ci = md_capainfo(env);
2790 RETURN(ERR_PTR(-ENOENT));
2792 switch (ci->mc_auth) {
2796 capa->lc_uid = obj->oo_inode->i_uid;
2797 capa->lc_gid = obj->oo_inode->i_gid;
2798 capa->lc_flags = LC_ID_PLAIN;
2800 case LC_ID_CONVERT: {
2803 s[0] = obj->oo_inode->i_uid;
2804 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2805 s[2] = obj->oo_inode->i_gid;
2806 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2807 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2809 RETURN(ERR_PTR(rc));
2811 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2812 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2813 capa->lc_flags = LC_ID_CONVERT;
2817 RETURN(ERR_PTR(-EINVAL));
2820 capa->lc_fid = *fid;
2822 capa->lc_flags |= dev->od_capa_alg << 24;
2823 capa->lc_timeout = dev->od_capa_timeout;
2824 capa->lc_expiry = 0;
2826 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2828 LASSERT(!capa_is_expired(oc));
2832 spin_lock(&capa_lock);
2833 *key = dev->od_capa_keys[1];
2834 spin_unlock(&capa_lock);
2836 capa->lc_keyid = key->lk_keyid;
2837 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2839 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2841 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2842 RETURN(ERR_PTR(rc));
2845 oc = capa_add(dev->od_capa_hash, capa);
2849 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2851 struct osd_object *obj = osd_dt_obj(dt);
2852 struct inode *inode = obj->oo_inode;
2853 struct osd_thread_info *info = osd_oti_get(env);
2854 struct dentry *dentry = &info->oti_obj_dentry;
2855 struct file *file = &info->oti_file;
2860 dentry->d_inode = inode;
2861 dentry->d_sb = inode->i_sb;
2862 file->f_dentry = dentry;
2863 file->f_mapping = inode->i_mapping;
2864 file->f_op = inode->i_fop;
2865 #ifndef HAVE_FILE_FSYNC_4ARGS
2866 mutex_lock(&inode->i_mutex);
2868 rc = do_fsync(file, 0);
2869 #ifndef HAVE_FILE_FSYNC_4ARGS
2870 mutex_unlock(&inode->i_mutex);
2875 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2878 struct osd_object *obj = osd_dt_obj(dt);
2881 *data = (void *)obj->oo_inode;
2889 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2890 const struct dt_index_features *feat)
2892 struct iam_descr *descr;
2894 if (osd_object_is_root(o))
2895 return feat == &dt_directory_features;
2897 LASSERT(o->oo_dir != NULL);
2899 descr = o->oo_dir->od_container.ic_descr;
2900 if (feat == &dt_directory_features) {
2901 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2907 feat->dif_keysize_min <= descr->id_key_size &&
2908 descr->id_key_size <= feat->dif_keysize_max &&
2909 feat->dif_recsize_min <= descr->id_rec_size &&
2910 descr->id_rec_size <= feat->dif_recsize_max &&
2911 !(feat->dif_flags & (DT_IND_VARKEY |
2912 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2913 ergo(feat->dif_flags & DT_IND_UPDATE,
2914 1 /* XXX check that object (and file system) is
2919 static int osd_iam_container_init(const struct lu_env *env,
2920 struct osd_object *obj,
2921 struct osd_directory *dir)
2923 struct iam_container *bag = &dir->od_container;
2926 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2930 result = iam_container_setup(bag);
2932 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2934 iam_container_fini(bag);
2941 * Concurrency: no external locking is necessary.
2943 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2944 const struct dt_index_features *feat)
2948 struct osd_object *obj = osd_dt_obj(dt);
2950 LINVRNT(osd_invariant(obj));
2952 if (osd_object_is_root(obj)) {
2953 dt->do_index_ops = &osd_index_ea_ops;
2955 } else if (feat == &dt_directory_features) {
2956 dt->do_index_ops = &osd_index_ea_ops;
2957 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
2962 } else if (unlikely(feat == &dt_otable_features)) {
2963 dt->do_index_ops = &osd_otable_ops;
2965 } else if (unlikely(feat == &dt_acct_features)) {
2966 dt->do_index_ops = &osd_acct_index_ops;
2969 } else if (!osd_has_index(obj)) {
2970 struct osd_directory *dir;
2975 spin_lock(&obj->oo_guard);
2976 if (obj->oo_dir == NULL)
2980 * Concurrent thread allocated container data.
2983 spin_unlock(&obj->oo_guard);
2985 * Now, that we have container data, serialize its
2988 down_write(&obj->oo_ext_idx_sem);
2990 * recheck under lock.
2992 if (!osd_has_index(obj))
2993 result = osd_iam_container_init(env, obj, dir);
2996 up_write(&obj->oo_ext_idx_sem);
3004 if (result == 0 && skip_iam == 0) {
3005 if (!osd_iam_index_probe(env, obj, feat))
3008 LINVRNT(osd_invariant(obj));
3010 if (result == 0 && is_quota_glb_feat(feat) &&
3011 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3012 result = osd_quota_migration(env, dt, feat);
3017 static int osd_otable_it_attr_get(const struct lu_env *env,
3018 struct dt_object *dt,
3019 struct lu_attr *attr,
3020 struct lustre_capa *capa)
3026 static const struct dt_object_operations osd_obj_ops = {
3027 .do_read_lock = osd_object_read_lock,
3028 .do_write_lock = osd_object_write_lock,
3029 .do_read_unlock = osd_object_read_unlock,
3030 .do_write_unlock = osd_object_write_unlock,
3031 .do_write_locked = osd_object_write_locked,
3032 .do_attr_get = osd_attr_get,
3033 .do_declare_attr_set = osd_declare_attr_set,
3034 .do_attr_set = osd_attr_set,
3035 .do_ah_init = osd_ah_init,
3036 .do_declare_create = osd_declare_object_create,
3037 .do_create = osd_object_create,
3038 .do_declare_destroy = osd_declare_object_destroy,
3039 .do_destroy = osd_object_destroy,
3040 .do_index_try = osd_index_try,
3041 .do_declare_ref_add = osd_declare_object_ref_add,
3042 .do_ref_add = osd_object_ref_add,
3043 .do_declare_ref_del = osd_declare_object_ref_del,
3044 .do_ref_del = osd_object_ref_del,
3045 .do_xattr_get = osd_xattr_get,
3046 .do_declare_xattr_set = osd_declare_xattr_set,
3047 .do_xattr_set = osd_xattr_set,
3048 .do_declare_xattr_del = osd_declare_xattr_del,
3049 .do_xattr_del = osd_xattr_del,
3050 .do_xattr_list = osd_xattr_list,
3051 .do_capa_get = osd_capa_get,
3052 .do_object_sync = osd_object_sync,
3053 .do_data_get = osd_data_get,
3057 * dt_object_operations for interoperability mode
3058 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3060 static const struct dt_object_operations osd_obj_ea_ops = {
3061 .do_read_lock = osd_object_read_lock,
3062 .do_write_lock = osd_object_write_lock,
3063 .do_read_unlock = osd_object_read_unlock,
3064 .do_write_unlock = osd_object_write_unlock,
3065 .do_write_locked = osd_object_write_locked,
3066 .do_attr_get = osd_attr_get,
3067 .do_declare_attr_set = osd_declare_attr_set,
3068 .do_attr_set = osd_attr_set,
3069 .do_ah_init = osd_ah_init,
3070 .do_declare_create = osd_declare_object_create,
3071 .do_create = osd_object_ea_create,
3072 .do_declare_destroy = osd_declare_object_destroy,
3073 .do_destroy = osd_object_destroy,
3074 .do_index_try = osd_index_try,
3075 .do_declare_ref_add = osd_declare_object_ref_add,
3076 .do_ref_add = osd_object_ref_add,
3077 .do_declare_ref_del = osd_declare_object_ref_del,
3078 .do_ref_del = osd_object_ref_del,
3079 .do_xattr_get = osd_xattr_get,
3080 .do_declare_xattr_set = osd_declare_xattr_set,
3081 .do_xattr_set = osd_xattr_set,
3082 .do_declare_xattr_del = osd_declare_xattr_del,
3083 .do_xattr_del = osd_xattr_del,
3084 .do_xattr_list = osd_xattr_list,
3085 .do_capa_get = osd_capa_get,
3086 .do_object_sync = osd_object_sync,
3087 .do_data_get = osd_data_get,
3090 static const struct dt_object_operations osd_obj_otable_it_ops = {
3091 .do_attr_get = osd_otable_it_attr_get,
3092 .do_index_try = osd_index_try,
3095 static int osd_index_declare_iam_delete(const struct lu_env *env,
3096 struct dt_object *dt,
3097 const struct dt_key *key,
3098 struct thandle *handle)
3100 struct osd_thandle *oh;
3102 oh = container_of0(handle, struct osd_thandle, ot_super);
3103 LASSERT(oh->ot_handle == NULL);
3105 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3106 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3112 * delete a (key, value) pair from index \a dt specified by \a key
3114 * \param dt osd index object
3115 * \param key key for index
3116 * \param rec record reference
3117 * \param handle transaction handler
3120 * \retval -ve failure
3123 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3124 const struct dt_key *key,
3125 struct thandle *handle,
3126 struct lustre_capa *capa)
3128 struct osd_thread_info *oti = osd_oti_get(env);
3129 struct osd_object *obj = osd_dt_obj(dt);
3130 struct osd_thandle *oh;
3131 struct iam_path_descr *ipd;
3132 struct iam_container *bag = &obj->oo_dir->od_container;
3137 LINVRNT(osd_invariant(obj));
3138 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3139 LASSERT(bag->ic_object == obj->oo_inode);
3140 LASSERT(handle != NULL);
3142 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3145 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3147 ipd = osd_idx_ipd_get(env, bag);
3148 if (unlikely(ipd == NULL))
3151 oh = container_of0(handle, struct osd_thandle, ot_super);
3152 LASSERT(oh->ot_handle != NULL);
3153 LASSERT(oh->ot_handle->h_transaction != NULL);
3155 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3156 /* swab quota uid/gid provided by caller */
3157 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3158 key = (const struct dt_key *)&oti->oti_quota_id;
3161 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3162 osd_ipd_put(env, bag, ipd);
3163 LINVRNT(osd_invariant(obj));
3167 static int osd_index_declare_ea_delete(const struct lu_env *env,
3168 struct dt_object *dt,
3169 const struct dt_key *key,
3170 struct thandle *handle)
3172 struct osd_thandle *oh;
3173 struct inode *inode;
3177 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3178 LASSERT(handle != NULL);
3180 oh = container_of0(handle, struct osd_thandle, ot_super);
3181 LASSERT(oh->ot_handle == NULL);
3183 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3184 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3186 inode = osd_dt_obj(dt)->oo_inode;
3189 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3190 true, true, NULL, false);
3194 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3197 struct osd_fid_pack *rec;
3200 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3201 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3202 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3207 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3210 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
3211 struct seq_server_site *ss = osd_seq_site(osd);
3215 /* Those FID seqs, which are not in FLDB, must be local seq */
3216 if (unlikely(!fid_seq_in_fldb(fid_seq(fid)) || ss == NULL))
3219 rc = osd_fld_lookup(env, osd, fid, range);
3221 CERROR("%s: Can not lookup fld for "DFID"\n",
3222 osd_name(osd), PFID(fid));
3226 RETURN(ss->ss_node_id != range->lsr_index);
3230 * Index delete function for interoperability mode (b11826).
3231 * It will remove the directory entry added by osd_index_ea_insert().
3232 * This entry is needed to maintain name->fid mapping.
3234 * \param key, key i.e. file entry to be deleted
3236 * \retval 0, on success
3237 * \retval -ve, on error
3239 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3240 const struct dt_key *key,
3241 struct thandle *handle,
3242 struct lustre_capa *capa)
3244 struct osd_object *obj = osd_dt_obj(dt);
3245 struct inode *dir = obj->oo_inode;
3246 struct dentry *dentry;
3247 struct osd_thandle *oh;
3248 struct ldiskfs_dir_entry_2 *de = NULL;
3249 struct buffer_head *bh;
3250 struct htree_lock *hlock = NULL;
3251 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3252 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3256 LINVRNT(osd_invariant(obj));
3257 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3258 LASSERT(handle != NULL);
3260 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3262 oh = container_of(handle, struct osd_thandle, ot_super);
3263 LASSERT(oh->ot_handle != NULL);
3264 LASSERT(oh->ot_handle->h_transaction != NULL);
3266 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3269 ll_vfs_dq_init(dir);
3270 dentry = osd_child_dentry_get(env, obj,
3271 (char *)key, strlen((char *)key));
3273 if (obj->oo_hl_head != NULL) {
3274 hlock = osd_oti_get(env)->oti_hlock;
3275 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3276 dir, LDISKFS_HLOCK_DEL);
3278 down_write(&obj->oo_ext_idx_sem);
3281 bh = ldiskfs_find_entry(dir, &dentry->d_name, &de, hlock);
3285 /* If this is not the ".." entry, it might be a remote DNE
3286 * entry and we need to check if the FID is for a remote
3287 * MDT. If the FID is not in the directory entry (e.g.
3288 * upgraded 1.8 filesystem without dirdata enabled) then
3289 * we need to get the FID from the LMA. For a remote directory
3290 * there HAS to be an LMA, it cannot be an IGIF inode in this
3293 * Delete the entry before the agent inode in order to
3294 * simplify error handling. At worst an error after deleting
3295 * the entry first might leak the agent inode afterward. The
3296 * reverse would need filesystem abort in case of error deleting
3297 * the entry after the agent had been removed, or leave a
3298 * dangling entry pointing at a random inode. */
3299 if (strcmp((char *)key, dotdot) != 0) {
3300 LASSERT(de != NULL);
3301 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3302 /* If Fid is not in dentry, try to get it from LMA */
3303 if (rc == -ENODATA) {
3304 struct osd_inode_id *id;
3305 struct inode *inode;
3307 /* Before trying to get fid from the inode,
3308 * check whether the inode is valid.
3310 * If the inode has been deleted, do not go
3311 * ahead to do osd_ea_fid_get, which will set
3312 * the inode to bad inode, which might cause
3313 * the inode to be deleted uncorrectly */
3314 inode = ldiskfs_iget(osd_sb(osd),
3315 le32_to_cpu(de->inode));
3316 if (IS_ERR(inode)) {
3317 CDEBUG(D_INODE, "%s: "DFID"get inode"
3318 "error.\n", osd_name(osd),
3320 rc = PTR_ERR(inode);
3322 if (likely(inode->i_nlink != 0)) {
3323 id = &osd_oti_get(env)->oti_id;
3324 rc = osd_ea_fid_get(env, obj,
3325 le32_to_cpu(de->inode),
3328 CDEBUG(D_INFO, "%s: %u "DFID
3331 le32_to_cpu(de->inode),
3339 unlikely(osd_remote_fid(env, osd, fid)))
3340 /* Need to delete agent inode */
3341 ino = le32_to_cpu(de->inode);
3343 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3345 if (rc == 0 && unlikely(ino != 0)) {
3346 rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3349 CERROR("%s: del local inode "DFID": rc = %d\n",
3350 osd_name(osd), PFID(fid), rc);
3356 ldiskfs_htree_unlock(hlock);
3358 up_write(&obj->oo_ext_idx_sem);
3363 /* For inode on the remote MDT, .. will point to
3364 * /Agent directory, Check whether it needs to delete
3365 * from agent directory */
3366 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3367 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3369 if (rc != 0 && rc != -ENOENT) {
3370 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3371 osd_name(osd), PFID(fid), rc);
3381 LASSERT(osd_invariant(obj));
3386 * Lookup index for \a key and copy record to \a rec.
3388 * \param dt osd index object
3389 * \param key key for index
3390 * \param rec record reference
3392 * \retval +ve success : exact mach
3393 * \retval 0 return record with key not greater than \a key
3394 * \retval -ve failure
3396 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3397 struct dt_rec *rec, const struct dt_key *key,
3398 struct lustre_capa *capa)
3400 struct osd_object *obj = osd_dt_obj(dt);
3401 struct iam_path_descr *ipd;
3402 struct iam_container *bag = &obj->oo_dir->od_container;
3403 struct osd_thread_info *oti = osd_oti_get(env);
3404 struct iam_iterator *it = &oti->oti_idx_it;
3405 struct iam_rec *iam_rec;
3410 LASSERT(osd_invariant(obj));
3411 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3412 LASSERT(bag->ic_object == obj->oo_inode);
3414 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3417 ipd = osd_idx_ipd_get(env, bag);
3421 /* got ipd now we can start iterator. */
3422 iam_it_init(it, bag, 0, ipd);
3424 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3425 /* swab quota uid/gid provided by caller */
3426 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3427 key = (const struct dt_key *)&oti->oti_quota_id;
3430 rc = iam_it_get(it, (struct iam_key *)key);
3432 if (S_ISDIR(obj->oo_inode->i_mode))
3433 iam_rec = (struct iam_rec *)oti->oti_ldp;
3435 iam_rec = (struct iam_rec *) rec;
3437 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3439 if (S_ISDIR(obj->oo_inode->i_mode))
3440 osd_fid_unpack((struct lu_fid *) rec,
3441 (struct osd_fid_pack *)iam_rec);
3442 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3443 osd_quota_unpack(obj, rec);
3448 osd_ipd_put(env, bag, ipd);
3450 LINVRNT(osd_invariant(obj));
3455 static int osd_index_declare_iam_insert(const struct lu_env *env,
3456 struct dt_object *dt,
3457 const struct dt_rec *rec,
3458 const struct dt_key *key,
3459 struct thandle *handle)
3461 struct osd_thandle *oh;
3463 LASSERT(handle != NULL);
3465 oh = container_of0(handle, struct osd_thandle, ot_super);
3466 LASSERT(oh->ot_handle == NULL);
3468 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3469 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3475 * Inserts (key, value) pair in \a dt index object.
3477 * \param dt osd index object
3478 * \param key key for index
3479 * \param rec record reference
3480 * \param th transaction handler
3483 * \retval -ve failure
3485 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3486 const struct dt_rec *rec,
3487 const struct dt_key *key, struct thandle *th,
3488 struct lustre_capa *capa, int ignore_quota)
3490 struct osd_object *obj = osd_dt_obj(dt);
3491 struct iam_path_descr *ipd;
3492 struct osd_thandle *oh;
3493 struct iam_container *bag = &obj->oo_dir->od_container;
3494 struct osd_thread_info *oti = osd_oti_get(env);
3495 struct iam_rec *iam_rec;
3500 LINVRNT(osd_invariant(obj));
3501 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3502 LASSERT(bag->ic_object == obj->oo_inode);
3503 LASSERT(th != NULL);
3505 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3508 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3510 ipd = osd_idx_ipd_get(env, bag);
3511 if (unlikely(ipd == NULL))
3514 oh = container_of0(th, struct osd_thandle, ot_super);
3515 LASSERT(oh->ot_handle != NULL);
3516 LASSERT(oh->ot_handle->h_transaction != NULL);
3517 if (S_ISDIR(obj->oo_inode->i_mode)) {
3518 iam_rec = (struct iam_rec *)oti->oti_ldp;
3519 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3520 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3521 /* pack quota uid/gid */
3522 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3523 key = (const struct dt_key *)&oti->oti_quota_id;
3524 /* pack quota record */
3525 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3526 iam_rec = (struct iam_rec *)rec;
3528 iam_rec = (struct iam_rec *)rec;
3531 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3533 osd_ipd_put(env, bag, ipd);
3534 LINVRNT(osd_invariant(obj));
3539 * Calls ldiskfs_add_entry() to add directory entry
3540 * into the directory. This is required for
3541 * interoperability mode (b11826)
3543 * \retval 0, on success
3544 * \retval -ve, on error
3546 static int __osd_ea_add_rec(struct osd_thread_info *info,
3547 struct osd_object *pobj, struct inode *cinode,
3548 const char *name, const struct dt_rec *fid,
3549 struct htree_lock *hlock, struct thandle *th)
3551 struct ldiskfs_dentry_param *ldp;
3552 struct dentry *child;
3553 struct osd_thandle *oth;
3556 oth = container_of(th, struct osd_thandle, ot_super);
3557 LASSERT(oth->ot_handle != NULL);
3558 LASSERT(oth->ot_handle->h_transaction != NULL);
3559 LASSERT(pobj->oo_inode);
3561 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3562 if (unlikely(pobj->oo_inode ==
3563 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3566 osd_get_ldiskfs_dirent_param(ldp, fid);
3567 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3568 child->d_fsdata = (void *)ldp;
3569 ll_vfs_dq_init(pobj->oo_inode);
3570 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3576 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3577 * into the directory.Also sets flags into osd object to
3578 * indicate dot and dotdot are created. This is required for
3579 * interoperability mode (b11826)
3581 * \param dir directory for dot and dotdot fixup.
3582 * \param obj child object for linking
3584 * \retval 0, on success
3585 * \retval -ve, on error
3587 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3588 struct osd_object *dir,
3589 struct inode *parent_dir, const char *name,
3590 const struct dt_rec *dot_fid,
3591 const struct dt_rec *dot_dot_fid,
3594 struct inode *inode = dir->oo_inode;
3595 struct osd_thandle *oth;
3598 oth = container_of(th, struct osd_thandle, ot_super);
3599 LASSERT(oth->ot_handle->h_transaction != NULL);
3600 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3602 if (strcmp(name, dot) == 0) {
3603 if (dir->oo_compat_dot_created) {
3606 LASSERT(inode == parent_dir);
3607 dir->oo_compat_dot_created = 1;
3610 } else if (strcmp(name, dotdot) == 0) {
3611 if (!dir->oo_compat_dot_created)
3613 /* in case of rename, dotdot is already created */
3614 if (dir->oo_compat_dotdot_created) {
3615 return __osd_ea_add_rec(info, dir, parent_dir, name,
3616 dot_dot_fid, NULL, th);
3619 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3620 parent_dir, dot_fid,
3623 dir->oo_compat_dotdot_created = 1;
3631 * It will call the appropriate osd_add* function and return the
3632 * value, return by respective functions.
3634 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3635 struct inode *cinode, const char *name,
3636 const struct dt_rec *fid, struct thandle *th)
3638 struct osd_thread_info *info = osd_oti_get(env);
3639 struct htree_lock *hlock;
3642 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3644 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3646 if (hlock != NULL) {
3647 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3650 down_write(&pobj->oo_ext_idx_sem);
3652 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3653 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3656 if (hlock != NULL) {
3657 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3658 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3660 down_write(&pobj->oo_ext_idx_sem);
3663 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3664 struct lu_fid *tfid = &info->oti_fid;
3666 *tfid = *(const struct lu_fid *)fid;
3668 rc = __osd_ea_add_rec(info, pobj, cinode, name,
3669 (const struct dt_rec *)tfid,
3672 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3677 ldiskfs_htree_unlock(hlock);
3679 up_write(&pobj->oo_ext_idx_sem);
3685 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3686 struct osd_idmap_cache *oic)
3688 struct osd_scrub *scrub = &dev->od_scrub;
3689 struct lu_fid *fid = &oic->oic_fid;
3690 struct osd_inode_id *id = &oti->oti_id;
3695 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3699 rc = osd_oi_lookup(oti, dev, fid, id, true);
3700 if (rc != 0 && rc != -ENOENT)
3703 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3706 if (thread_is_running(&scrub->os_thread)) {
3707 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3708 /* There is race condition between osd_oi_lookup and OI scrub.
3709 * The OI scrub finished just after osd_oi_lookup() failure.
3710 * Under such case, it is unnecessary to trigger OI scrub again,
3711 * but try to call osd_oi_lookup() again. */
3712 if (unlikely(rc == -EAGAIN))
3718 if (!dev->od_noscrub && ++once == 1) {
3719 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3721 rc = osd_scrub_start(dev);
3722 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3724 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3733 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3734 struct osd_device *dev,
3735 struct osd_idmap_cache *oic,
3736 struct lu_fid *fid, __u32 ino)
3738 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
3739 struct inode *inode;
3742 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3743 inode = osd_iget(oti, dev, &oic->oic_lid);
3744 if (IS_ERR(inode)) {
3745 fid_zero(&oic->oic_fid);
3746 return PTR_ERR(inode);
3749 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3752 fid_zero(&oic->oic_fid);
3754 *fid = oic->oic_fid = lma->lma_self_fid;
3759 * Calls ->lookup() to find dentry. From dentry get inode and
3760 * read inode's ea to get fid. This is required for interoperability
3763 * \retval 0, on success
3764 * \retval -ve, on error
3766 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3767 struct dt_rec *rec, const struct dt_key *key)
3769 struct inode *dir = obj->oo_inode;
3770 struct dentry *dentry;
3771 struct ldiskfs_dir_entry_2 *de;
3772 struct buffer_head *bh;
3773 struct lu_fid *fid = (struct lu_fid *) rec;
3774 struct htree_lock *hlock = NULL;
3779 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3781 dentry = osd_child_dentry_get(env, obj,
3782 (char *)key, strlen((char *)key));
3784 if (obj->oo_hl_head != NULL) {
3785 hlock = osd_oti_get(env)->oti_hlock;
3786 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3787 dir, LDISKFS_HLOCK_LOOKUP);
3789 down_read(&obj->oo_ext_idx_sem);
3792 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3794 struct osd_thread_info *oti = osd_oti_get(env);
3795 struct osd_inode_id *id = &oti->oti_id;
3796 struct osd_idmap_cache *oic = &oti->oti_cache;
3797 struct osd_device *dev = osd_obj2dev(obj);
3798 struct osd_scrub *scrub = &dev->od_scrub;
3799 struct scrub_file *sf = &scrub->os_file;
3801 ino = le32_to_cpu(de->inode);
3802 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
3804 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
3808 rc = osd_get_fid_from_dentry(de, rec);
3810 /* done with de, release bh */
3813 rc = osd_ea_fid_get(env, obj, ino, fid, id);
3815 osd_id_gen(id, ino, OSD_OII_NOGEN);
3816 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
3817 fid_zero(&oic->oic_fid);
3822 oic->oic_fid = *fid;
3823 if ((scrub->os_pos_current <= ino) &&
3824 ((sf->sf_flags & SF_INCONSISTENT) ||
3825 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
3826 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3828 osd_consistency_check(oti, dev, oic);
3837 ldiskfs_htree_unlock(hlock);
3839 up_read(&obj->oo_ext_idx_sem);
3844 * Find the osd object for given fid.
3846 * \param fid need to find the osd object having this fid
3848 * \retval osd_object on success
3849 * \retval -ve on error
3851 struct osd_object *osd_object_find(const struct lu_env *env,
3852 struct dt_object *dt,
3853 const struct lu_fid *fid)
3855 struct lu_device *ludev = dt->do_lu.lo_dev;
3856 struct osd_object *child = NULL;
3857 struct lu_object *luch;
3858 struct lu_object *lo;
3861 * at this point topdev might not exist yet
3862 * (i.e. MGS is preparing profiles). so we can
3863 * not rely on topdev and instead lookup with
3864 * our device passed as topdev. this can't work
3865 * if the object isn't cached yet (as osd doesn't
3866 * allocate lu_header). IOW, the object must be
3867 * in the cache, otherwise lu_object_alloc() crashes
3870 luch = lu_object_find_at(env, ludev, fid, NULL);
3871 if (!IS_ERR(luch)) {
3872 if (lu_object_exists(luch)) {
3873 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3875 child = osd_obj(lo);
3877 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3878 "lu_object can't be located"
3879 DFID"\n", PFID(fid));
3881 if (child == NULL) {
3882 lu_object_put(env, luch);
3883 CERROR("Unable to get osd_object\n");
3884 child = ERR_PTR(-ENOENT);
3887 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3888 "lu_object does not exists "DFID"\n",
3890 lu_object_put(env, luch);
3891 child = ERR_PTR(-ENOENT);
3894 child = (void *)luch;
3900 * Put the osd object once done with it.
3902 * \param obj osd object that needs to be put
3904 static inline void osd_object_put(const struct lu_env *env,
3905 struct osd_object *obj)
3907 lu_object_put(env, &obj->oo_dt.do_lu);
3910 static int osd_index_declare_ea_insert(const struct lu_env *env,
3911 struct dt_object *dt,
3912 const struct dt_rec *rec,
3913 const struct dt_key *key,
3914 struct thandle *handle)
3916 struct osd_thandle *oh;
3917 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3918 struct lu_fid *fid = (struct lu_fid *)rec;
3922 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3923 LASSERT(handle != NULL);
3925 oh = container_of0(handle, struct osd_thandle, ot_super);
3926 LASSERT(oh->ot_handle == NULL);
3928 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3929 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3931 if (osd_dt_obj(dt)->oo_inode == NULL) {
3932 const char *name = (const char *)key;
3933 /* Object is not being created yet. Only happens when
3934 * 1. declare directory create
3935 * 2. declare insert .
3936 * 3. declare insert ..
3938 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
3940 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3942 /* We ignore block quota on meta pool (MDTs), so needn't
3943 * calculate how many blocks will be consumed by this index
3945 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
3946 oh, true, true, NULL, false);
3952 rc = osd_remote_fid(env, osd, fid);
3958 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3959 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
3960 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3961 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3962 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3963 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3969 * Index add function for interoperability mode (b11826).
3970 * It will add the directory entry.This entry is needed to
3971 * maintain name->fid mapping.
3973 * \param key it is key i.e. file entry to be inserted
3974 * \param rec it is value of given key i.e. fid
3976 * \retval 0, on success
3977 * \retval -ve, on error
3979 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3980 const struct dt_rec *rec,
3981 const struct dt_key *key, struct thandle *th,
3982 struct lustre_capa *capa, int ignore_quota)
3984 struct osd_object *obj = osd_dt_obj(dt);
3985 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3986 struct lu_fid *fid = (struct lu_fid *) rec;
3987 const char *name = (const char *)key;
3988 struct osd_thread_info *oti = osd_oti_get(env);
3989 struct osd_inode_id *id = &oti->oti_id;
3990 struct inode *child_inode = NULL;
3991 struct osd_object *child = NULL;
3995 LASSERT(osd_invariant(obj));
3996 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3997 LASSERT(th != NULL);
3999 osd_trans_exec_op(env, th, OSD_OT_INSERT);
4001 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
4004 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
4006 rc = osd_remote_fid(env, osd, fid);
4008 CERROR("%s: Can not find object "DFID" rc %d\n",
4009 osd_name(osd), PFID(fid), rc);
4014 /* Insert remote entry */
4015 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4016 struct osd_mdobj_map *omm = osd->od_mdt_map;
4017 struct osd_thandle *oh;
4019 /* If parent on remote MDT, we need put this object
4021 oh = container_of(th, typeof(*oh), ot_super);
4022 rc = osd_add_to_remote_parent(env, osd, obj, oh);
4024 CERROR("%s: add "DFID" error: rc = %d\n",
4026 PFID(lu_object_fid(&dt->do_lu)), rc);
4030 child_inode = igrab(omm->omm_remote_parent->d_inode);
4032 child_inode = osd_create_local_agent_inode(env, osd,
4035 if (IS_ERR(child_inode))
4036 RETURN(PTR_ERR(child_inode));
4039 /* Insert local entry */
4040 child = osd_object_find(env, dt, fid);
4041 if (IS_ERR(child)) {
4042 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4043 osd_name(osd), PFID(fid),
4044 id->oii_ino, id->oii_gen,
4045 (int)PTR_ERR(child_inode));
4046 RETURN(PTR_ERR(child_inode));
4048 child_inode = igrab(child->oo_inode);
4051 rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
4055 osd_object_put(env, child);
4056 LASSERT(osd_invariant(obj));
4061 * Initialize osd Iterator for given osd index object.
4063 * \param dt osd index object
4066 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4067 struct dt_object *dt,
4069 struct lustre_capa *capa)
4071 struct osd_it_iam *it;
4072 struct osd_thread_info *oti = osd_oti_get(env);
4073 struct osd_object *obj = osd_dt_obj(dt);
4074 struct lu_object *lo = &dt->do_lu;
4075 struct iam_path_descr *ipd;
4076 struct iam_container *bag = &obj->oo_dir->od_container;
4078 LASSERT(lu_object_exists(lo));
4080 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4081 return ERR_PTR(-EACCES);
4084 ipd = osd_it_ipd_get(env, bag);
4085 if (likely(ipd != NULL)) {
4089 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4090 return (struct dt_it *)it;
4092 return ERR_PTR(-ENOMEM);
4096 * free given Iterator.
4099 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4101 struct osd_it_iam *it = (struct osd_it_iam *)di;
4102 struct osd_object *obj = it->oi_obj;
4104 iam_it_fini(&it->oi_it);
4105 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4106 lu_object_put(env, &obj->oo_dt.do_lu);
4110 * Move Iterator to record specified by \a key
4112 * \param di osd iterator
4113 * \param key key for index
4115 * \retval +ve di points to record with least key not larger than key
4116 * \retval 0 di points to exact matched key
4117 * \retval -ve failure
4120 static int osd_it_iam_get(const struct lu_env *env,
4121 struct dt_it *di, const struct dt_key *key)
4123 struct osd_thread_info *oti = osd_oti_get(env);
4124 struct osd_it_iam *it = (struct osd_it_iam *)di;
4126 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4127 /* swab quota uid/gid */
4128 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4129 key = (struct dt_key *)&oti->oti_quota_id;
4132 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4138 * \param di osd iterator
4140 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4142 struct osd_it_iam *it = (struct osd_it_iam *)di;
4144 iam_it_put(&it->oi_it);
4148 * Move iterator by one record
4150 * \param di osd iterator
4152 * \retval +1 end of container reached
4154 * \retval -ve failure
4157 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4159 struct osd_it_iam *it = (struct osd_it_iam *)di;
4161 return iam_it_next(&it->oi_it);
4165 * Return pointer to the key under iterator.
4168 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4169 const struct dt_it *di)
4171 struct osd_thread_info *oti = osd_oti_get(env);
4172 struct osd_it_iam *it = (struct osd_it_iam *)di;
4173 struct osd_object *obj = it->oi_obj;
4176 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4178 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4179 /* swab quota uid/gid */
4180 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4181 key = (struct dt_key *)&oti->oti_quota_id;
4188 * Return size of key under iterator (in bytes)
4191 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4193 struct osd_it_iam *it = (struct osd_it_iam *)di;
4195 return iam_it_key_size(&it->oi_it);
4199 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4201 /* check if file type is required */
4202 if (ent->lde_attrs & LUDA_TYPE) {
4203 struct luda_type *lt;
4204 int align = sizeof(*lt) - 1;
4206 len = (len + align) & ~align;
4207 lt = (struct luda_type *)(ent->lde_name + len);
4208 lt->lt_type = cpu_to_le16(DTTOIF(type));
4211 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4215 * build lu direct from backend fs dirent.
4219 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4220 char *name, __u16 namelen, __u16 type, __u32 attr)
4222 ent->lde_attrs = attr | LUDA_FID;
4223 fid_cpu_to_le(&ent->lde_fid, fid);
4225 ent->lde_hash = cpu_to_le64(offset);
4226 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4228 strncpy(ent->lde_name, name, namelen);
4229 ent->lde_name[namelen] = '\0';
4230 ent->lde_namelen = cpu_to_le16(namelen);
4232 /* append lustre attributes */
4233 osd_it_append_attrs(ent, namelen, type);
4237 * Return pointer to the record under iterator.
4239 static int osd_it_iam_rec(const struct lu_env *env,
4240 const struct dt_it *di,
4241 struct dt_rec *dtrec, __u32 attr)
4243 struct osd_it_iam *it = (struct osd_it_iam *)di;
4244 struct osd_thread_info *info = osd_oti_get(env);
4247 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4248 const struct osd_fid_pack *rec;
4249 struct lu_fid *fid = &info->oti_fid;
4250 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4256 name = (char *)iam_it_key_get(&it->oi_it);
4258 RETURN(PTR_ERR(name));
4260 namelen = iam_it_key_size(&it->oi_it);
4262 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4264 RETURN(PTR_ERR(rec));
4266 rc = osd_fid_unpack(fid, rec);
4270 hash = iam_it_store(&it->oi_it);
4272 /* IAM does not store object type in IAM index (dir) */
4273 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4275 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4276 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4277 (struct iam_rec *)dtrec);
4278 osd_quota_unpack(it->oi_obj, dtrec);
4280 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4281 (struct iam_rec *)dtrec);
4288 * Returns cookie for current Iterator position.
4290 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4292 struct osd_it_iam *it = (struct osd_it_iam *)di;
4294 return iam_it_store(&it->oi_it);
4298 * Restore iterator from cookie.
4300 * \param di osd iterator
4301 * \param hash Iterator location cookie
4303 * \retval +ve di points to record with least key not larger than key.
4304 * \retval 0 di points to exact matched key
4305 * \retval -ve failure
4308 static int osd_it_iam_load(const struct lu_env *env,
4309 const struct dt_it *di, __u64 hash)
4311 struct osd_it_iam *it = (struct osd_it_iam *)di;
4313 return iam_it_load(&it->oi_it, hash);
4316 static const struct dt_index_operations osd_index_iam_ops = {
4317 .dio_lookup = osd_index_iam_lookup,
4318 .dio_declare_insert = osd_index_declare_iam_insert,
4319 .dio_insert = osd_index_iam_insert,
4320 .dio_declare_delete = osd_index_declare_iam_delete,
4321 .dio_delete = osd_index_iam_delete,
4323 .init = osd_it_iam_init,
4324 .fini = osd_it_iam_fini,
4325 .get = osd_it_iam_get,
4326 .put = osd_it_iam_put,
4327 .next = osd_it_iam_next,
4328 .key = osd_it_iam_key,
4329 .key_size = osd_it_iam_key_size,
4330 .rec = osd_it_iam_rec,
4331 .store = osd_it_iam_store,
4332 .load = osd_it_iam_load
4338 * Creates or initializes iterator context.
4340 * \retval struct osd_it_ea, iterator structure on success
4343 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4344 struct dt_object *dt,
4346 struct lustre_capa *capa)
4348 struct osd_object *obj = osd_dt_obj(dt);
4349 struct osd_thread_info *info = osd_oti_get(env);
4350 struct osd_it_ea *it = &info->oti_it_ea;
4351 struct file *file = &it->oie_file;
4352 struct lu_object *lo = &dt->do_lu;
4353 struct dentry *obj_dentry = &info->oti_it_dentry;
4355 LASSERT(lu_object_exists(lo));
4357 obj_dentry->d_inode = obj->oo_inode;
4358 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4359 obj_dentry->d_name.hash = 0;
4361 it->oie_rd_dirent = 0;
4362 it->oie_it_dirent = 0;
4363 it->oie_dirent = NULL;
4364 it->oie_buf = info->oti_it_ea_buf;
4367 /* Reset the "file" totally to avoid to reuse any old value from
4368 * former readdir handling, the "file->f_pos" should be zero. */
4369 memset(file, 0, sizeof(*file));
4370 /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4371 if (attr & LUDA_64BITHASH)
4372 file->f_mode = FMODE_64BITHASH;
4374 file->f_mode = FMODE_32BITHASH;
4375 file->f_dentry = obj_dentry;
4376 file->f_mapping = obj->oo_inode->i_mapping;
4377 file->f_op = obj->oo_inode->i_fop;
4379 RETURN((struct dt_it *) it);
4383 * Destroy or finishes iterator context.
4385 * \param di iterator structure to be destroyed
4387 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4389 struct osd_it_ea *it = (struct osd_it_ea *)di;
4390 struct osd_object *obj = it->oie_obj;
4391 struct inode *inode = obj->oo_inode;
4394 it->oie_file.f_op->release(inode, &it->oie_file);
4395 lu_object_put(env, &obj->oo_dt.do_lu);
4400 * It position the iterator at given key, so that next lookup continues from
4401 * that key Or it is similar to dio_it->load() but based on a key,
4402 * rather than file position.
4404 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4407 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4409 static int osd_it_ea_get(const struct lu_env *env,
4410 struct dt_it *di, const struct dt_key *key)
4412 struct osd_it_ea *it = (struct osd_it_ea *)di;
4415 LASSERT(((const char *)key)[0] == '\0');
4416 it->oie_file.f_pos = 0;
4417 it->oie_rd_dirent = 0;
4418 it->oie_it_dirent = 0;
4419 it->oie_dirent = NULL;
4427 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4432 * It is called internally by ->readdir(). It fills the
4433 * iterator's in-memory data structure with required
4434 * information i.e. name, namelen, rec_size etc.
4436 * \param buf in which information to be filled in.
4437 * \param name name of the file in given dir
4439 * \retval 0 on success
4440 * \retval 1 on buffer full
4442 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4443 loff_t offset, __u64 ino,
4446 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4447 struct osd_object *obj = it->oie_obj;
4448 struct osd_it_ea_dirent *ent = it->oie_dirent;
4449 struct lu_fid *fid = &ent->oied_fid;
4450 struct osd_fid_pack *rec;
4453 /* this should never happen */
4454 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4455 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4459 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4463 /* "." is just the object itself. */
4464 if (namelen == 1 && name[0] == '.') {
4465 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4466 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4467 rec = (struct osd_fid_pack*) (name + namelen + 1);
4468 if (osd_fid_unpack(fid, rec) != 0)
4473 d_type &= ~LDISKFS_DIRENT_LUFID;
4475 /* NOT export local root. */
4476 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4477 ino = obj->oo_inode->i_ino;
4478 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4481 ent->oied_ino = ino;
4482 ent->oied_off = offset;
4483 ent->oied_namelen = namelen;
4484 ent->oied_type = d_type;
4486 memcpy(ent->oied_name, name, namelen);
4488 it->oie_rd_dirent++;
4489 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4494 * Calls ->readdir() to load a directory entry at a time
4495 * and stored it in iterator's in-memory data structure.
4497 * \param di iterator's in memory structure
4499 * \retval 0 on success
4500 * \retval -ve on error
4502 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4503 const struct dt_it *di)
4505 struct osd_it_ea *it = (struct osd_it_ea *)di;
4506 struct osd_object *obj = it->oie_obj;
4507 struct inode *inode = obj->oo_inode;
4508 struct htree_lock *hlock = NULL;
4512 it->oie_dirent = it->oie_buf;
4513 it->oie_rd_dirent = 0;
4515 if (obj->oo_hl_head != NULL) {
4516 hlock = osd_oti_get(env)->oti_hlock;
4517 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4518 inode, LDISKFS_HLOCK_READDIR);
4520 down_read(&obj->oo_ext_idx_sem);
4523 result = inode->i_fop->readdir(&it->oie_file, it,
4524 (filldir_t) osd_ldiskfs_filldir);
4527 ldiskfs_htree_unlock(hlock);
4529 up_read(&obj->oo_ext_idx_sem);
4531 if (it->oie_rd_dirent == 0) {
4534 it->oie_dirent = it->oie_buf;
4535 it->oie_it_dirent = 1;
4542 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4543 * to load a directory entry at a time and stored it in
4544 * iterator's in-memory data structure.
4546 * \param di iterator's in memory structure
4548 * \retval +ve iterator reached to end
4549 * \retval 0 iterator not reached to end
4550 * \retval -ve on error
4552 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4554 struct osd_it_ea *it = (struct osd_it_ea *)di;
4559 if (it->oie_it_dirent < it->oie_rd_dirent) {
4561 (void *) it->oie_dirent +
4562 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4563 it->oie_dirent->oied_namelen);
4564 it->oie_it_dirent++;
4567 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4570 rc = osd_ldiskfs_it_fill(env, di);
4577 * Returns the key at current position from iterator's in memory structure.
4579 * \param di iterator's in memory structure
4581 * \retval key i.e. struct dt_key on success
4583 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4584 const struct dt_it *di)
4586 struct osd_it_ea *it = (struct osd_it_ea *)di;
4588 return (struct dt_key *)it->oie_dirent->oied_name;
4592 * Returns the key's size at current position from iterator's in memory structure.
4594 * \param di iterator's in memory structure
4596 * \retval key_size i.e. struct dt_key on success
4598 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4600 struct osd_it_ea *it = (struct osd_it_ea *)di;
4602 return it->oie_dirent->oied_namelen;
4606 osd_dirent_update(handle_t *jh, struct super_block *sb,
4607 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4608 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4610 struct osd_fid_pack *rec;
4614 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4615 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4617 rc = ldiskfs_journal_get_write_access(jh, bh);
4619 CERROR("%.16s: fail to write access for update dirent: "
4620 "name = %.*s, rc = %d\n",
4621 LDISKFS_SB(sb)->s_es->s_volume_name,
4622 ent->oied_namelen, ent->oied_name, rc);
4626 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4627 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4628 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4630 CERROR("%.16s: fail to dirty metadata for update dirent: "
4631 "name = %.*s, rc = %d\n",
4632 LDISKFS_SB(sb)->s_es->s_volume_name,
4633 ent->oied_namelen, ent->oied_name, rc);
4639 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4641 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4642 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4649 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
4651 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
4652 "dot_dotdot = %d\n", dot_dotdot);
4654 if (LDISKFS_DIR_REC_LEN(de) >=
4655 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
4662 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4663 struct inode *dir, struct inode *inode,
4664 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4665 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4666 struct htree_lock *hlock)
4668 struct dentry *dentry;
4669 struct osd_fid_pack *rec;
4670 struct ldiskfs_dentry_param *ldp;
4674 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4675 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4678 /* There is enough space to hold the FID-in-dirent. */
4679 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4680 dir->i_sb->s_blocksize)) {
4681 rc = ldiskfs_journal_get_write_access(jh, bh);
4683 CERROR("%.16s: fail to write access for reinsert "
4684 "dirent: name = %.*s, rc = %d\n",
4685 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4686 ent->oied_namelen, ent->oied_name, rc);
4690 de->name[de->name_len] = 0;
4691 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4692 rec->fp_len = sizeof(struct lu_fid) + 1;
4693 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4694 de->file_type |= LDISKFS_DIRENT_LUFID;
4696 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4698 CERROR("%.16s: fail to dirty metadata for reinsert "
4699 "dirent: name = %.*s, rc = %d\n",
4700 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4701 ent->oied_namelen, ent->oied_name, rc);
4706 rc = ldiskfs_delete_entry(jh, dir, de, bh);
4708 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4709 "name = %.*s, rc = %d\n",
4710 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4711 ent->oied_namelen, ent->oied_name, rc);
4715 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4717 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4718 osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4719 dentry->d_fsdata = (void *)ldp;
4720 ll_vfs_dq_init(dir);
4721 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4722 /* It is too bad, we cannot reinsert the name entry back.
4723 * That means we lose it! */
4725 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4726 "name = %.*s, rc = %d\n",
4727 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4728 ent->oied_namelen, ent->oied_name, rc);
4734 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4735 struct osd_it_ea *it, struct lu_fid *fid,
4736 struct osd_inode_id *id, __u32 *attr)
4738 struct osd_thread_info *info = osd_oti_get(env);
4739 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
4740 struct osd_device *dev = osd_obj2dev(obj);
4741 struct super_block *sb = osd_sb(dev);
4742 const char *devname =
4743 LDISKFS_SB(sb)->s_es->s_volume_name;
4744 struct osd_it_ea_dirent *ent = it->oie_dirent;
4745 struct inode *dir = obj->oo_inode;
4746 struct htree_lock *hlock = NULL;
4747 struct buffer_head *bh = NULL;
4748 handle_t *jh = NULL;
4749 struct ldiskfs_dir_entry_2 *de;
4750 struct dentry *dentry;
4751 struct inode *inode;
4758 if (ent->oied_name[0] == '.') {
4759 if (ent->oied_namelen == 1)
4761 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4765 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4768 /* We need to ensure that the name entry is still valid.
4769 * Because it may be removed or renamed by other already.
4771 * The unlink or rename operation will start journal before PDO lock,
4772 * so to avoid deadlock, here we need to start journal handle before
4773 * related PDO lock also. But because we do not know whether there
4774 * will be something to be repaired before PDO lock, we just start
4775 * journal without conditions.
4777 * We may need to remove the name entry firstly, then insert back.
4778 * One credit is for user quota file update.
4779 * One credit is for group quota file update.
4780 * Two credits are for dirty inode. */
4781 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4782 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4785 if (dev->od_dirent_journal) {
4786 jh = ldiskfs_journal_start_sb(sb, credits);
4789 CERROR("%.16s: fail to start trans for dirent "
4790 "check_repair: credits %d, name %.*s, rc %d\n",
4791 devname, credits, ent->oied_namelen,
4792 ent->oied_name, rc);
4796 if (obj->oo_hl_head != NULL) {
4797 hlock = osd_oti_get(env)->oti_hlock;
4798 /* "0" means exclusive lock for the whole directory.
4799 * We need to prevent others access such name entry
4800 * during the delete + insert. Neither HLOCK_ADD nor
4801 * HLOCK_DEL cannot guarantee the atomicity. */
4802 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
4804 down_write(&obj->oo_ext_idx_sem);
4807 if (obj->oo_hl_head != NULL) {
4808 hlock = osd_oti_get(env)->oti_hlock;
4809 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
4810 LDISKFS_HLOCK_LOOKUP);
4812 down_read(&obj->oo_ext_idx_sem);
4816 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
4817 /* For dot/dotdot entry, if there is not enough space to hold the
4818 * FID-in-dirent, just keep them there. It only happens when the
4819 * device upgraded from 1.8 or restored from MDT file-level backup.
4820 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
4821 * and needs to get FID from LMA when readdir, it will not affect the
4822 * performance much. */
4823 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
4824 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
4825 *attr |= LUDA_IGNORE;
4826 GOTO(out_journal, rc = 0);
4829 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
4830 inode = osd_iget(info, dev, id);
4831 if (IS_ERR(inode)) {
4832 rc = PTR_ERR(inode);
4833 if (rc == -ENOENT || rc == -ESTALE) {
4834 *attr |= LUDA_IGNORE;
4838 GOTO(out_journal, rc);
4841 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
4843 if (fid_is_sane(fid)) {
4844 /* FID-in-dirent is valid. */
4845 if (lu_fid_eq(fid, &lma->lma_self_fid))
4846 GOTO(out_inode, rc = 0);
4848 /* Do not repair under dryrun mode. */
4849 if (*attr & LUDA_VERIFY_DRYRUN) {
4850 *attr |= LUDA_REPAIR;
4851 GOTO(out_inode, rc = 0);
4854 if (!dev->od_dirent_journal) {
4858 ldiskfs_htree_unlock(hlock);
4860 up_read(&obj->oo_ext_idx_sem);
4861 dev->od_dirent_journal = 1;
4865 *fid = lma->lma_self_fid;
4867 /* Update the FID-in-dirent. */
4868 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
4870 *attr |= LUDA_REPAIR;
4872 /* Do not repair under dryrun mode. */
4873 if (*attr & LUDA_VERIFY_DRYRUN) {
4874 *fid = lma->lma_self_fid;
4875 *attr |= LUDA_REPAIR;
4876 GOTO(out_inode, rc = 0);
4879 if (!dev->od_dirent_journal) {
4883 ldiskfs_htree_unlock(hlock);
4885 up_read(&obj->oo_ext_idx_sem);
4886 dev->od_dirent_journal = 1;
4890 *fid = lma->lma_self_fid;
4892 /* Append the FID-in-dirent. */
4893 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4894 fid, bh, de, hlock);
4896 *attr |= LUDA_REPAIR;
4898 } else if (rc == -ENODATA) {
4899 /* Do not repair under dryrun mode. */
4900 if (*attr & LUDA_VERIFY_DRYRUN) {
4901 if (fid_is_sane(fid)) {
4902 *attr |= LUDA_REPAIR;
4904 lu_igif_build(fid, inode->i_ino,
4905 inode->i_generation);
4906 *attr |= LUDA_UPGRADE;
4908 GOTO(out_inode, rc = 0);
4911 if (!dev->od_dirent_journal) {
4915 ldiskfs_htree_unlock(hlock);
4917 up_read(&obj->oo_ext_idx_sem);
4918 dev->od_dirent_journal = 1;
4923 if (unlikely(fid_is_sane(fid))) {
4924 /* FID-in-dirent exists, but FID-in-LMA is lost.
4925 * Trust the FID-in-dirent, and add FID-in-LMA. */
4926 rc = osd_ea_fid_set(info, inode, fid, 0);
4928 *attr |= LUDA_REPAIR;
4930 lu_igif_build(fid, inode->i_ino, inode->i_generation);
4931 /* It is probably IGIF object. Only aappend the
4932 * FID-in-dirent. OI scrub will process FID-in-LMA. */
4933 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4934 fid, bh, de, hlock);
4936 *attr |= LUDA_UPGRADE;
4940 GOTO(out_inode, rc);
4947 if (hlock != NULL) {
4948 ldiskfs_htree_unlock(hlock);
4950 if (dev->od_dirent_journal)
4951 up_write(&obj->oo_ext_idx_sem);
4953 up_read(&obj->oo_ext_idx_sem);
4956 ldiskfs_journal_stop(jh);
4957 if (rc >= 0 && !dirty)
4958 dev->od_dirent_journal = 0;
4963 * Returns the value at current position from iterator's in memory structure.
4965 * \param di struct osd_it_ea, iterator's in memory structure
4966 * \param attr attr requested for dirent.
4967 * \param lde lustre dirent
4969 * \retval 0 no error and \param lde has correct lustre dirent.
4970 * \retval -ve on error
4972 static inline int osd_it_ea_rec(const struct lu_env *env,
4973 const struct dt_it *di,
4974 struct dt_rec *dtrec, __u32 attr)
4976 struct osd_it_ea *it = (struct osd_it_ea *)di;
4977 struct osd_object *obj = it->oie_obj;
4978 struct osd_device *dev = osd_obj2dev(obj);
4979 struct osd_scrub *scrub = &dev->od_scrub;
4980 struct scrub_file *sf = &scrub->os_file;
4981 struct osd_thread_info *oti = osd_oti_get(env);
4982 struct osd_inode_id *id = &oti->oti_id;
4983 struct osd_idmap_cache *oic = &oti->oti_cache;
4984 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4985 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4986 __u32 ino = it->oie_dirent->oied_ino;
4990 if (attr & LUDA_VERIFY) {
4992 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
4993 attr |= LUDA_IGNORE;
4998 rc = osd_dirent_check_repair(env, obj, it, fid, id, &attr);
5000 attr &= ~LU_DIRENT_ATTRS_MASK;
5001 if (!fid_is_sane(fid)) {
5002 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5005 rc = osd_ea_fid_get(env, obj, ino, fid, id);
5007 osd_id_gen(id, ino, OSD_OII_NOGEN);
5015 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5016 it->oie_dirent->oied_name,
5017 it->oie_dirent->oied_namelen,
5018 it->oie_dirent->oied_type, attr);
5020 if (osd_remote_fid(env, dev, fid))
5023 if (likely(!(attr & LUDA_IGNORE))) {
5025 oic->oic_fid = *fid;
5028 if (!(attr & LUDA_VERIFY) &&
5029 (scrub->os_pos_current <= ino) &&
5030 ((sf->sf_flags & SF_INCONSISTENT) ||
5031 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
5032 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
5033 osd_consistency_check(oti, dev, oic);
5039 * Returns a cookie for current position of the iterator head, so that
5040 * user can use this cookie to load/start the iterator next time.
5042 * \param di iterator's in memory structure
5044 * \retval cookie for current position, on success
5046 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5048 struct osd_it_ea *it = (struct osd_it_ea *)di;
5050 return it->oie_dirent->oied_off;
5054 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5055 * to load a directory entry at a time and stored it i inn,
5056 * in iterator's in-memory data structure.
5058 * \param di struct osd_it_ea, iterator's in memory structure
5060 * \retval +ve on success
5061 * \retval -ve on error
5063 static int osd_it_ea_load(const struct lu_env *env,
5064 const struct dt_it *di, __u64 hash)
5066 struct osd_it_ea *it = (struct osd_it_ea *)di;
5070 it->oie_file.f_pos = hash;
5072 rc = osd_ldiskfs_it_fill(env, di);
5080 * Index lookup function for interoperability mode (b11826).
5082 * \param key, key i.e. file name to be searched
5084 * \retval +ve, on success
5085 * \retval -ve, on error
5087 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5088 struct dt_rec *rec, const struct dt_key *key,
5089 struct lustre_capa *capa)
5091 struct osd_object *obj = osd_dt_obj(dt);
5096 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5097 LINVRNT(osd_invariant(obj));
5099 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5102 rc = osd_ea_lookup_rec(env, obj, rec, key);
5109 * Index and Iterator operations for interoperability
5110 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5112 static const struct dt_index_operations osd_index_ea_ops = {
5113 .dio_lookup = osd_index_ea_lookup,
5114 .dio_declare_insert = osd_index_declare_ea_insert,
5115 .dio_insert = osd_index_ea_insert,
5116 .dio_declare_delete = osd_index_declare_ea_delete,
5117 .dio_delete = osd_index_ea_delete,
5119 .init = osd_it_ea_init,
5120 .fini = osd_it_ea_fini,
5121 .get = osd_it_ea_get,
5122 .put = osd_it_ea_put,
5123 .next = osd_it_ea_next,
5124 .key = osd_it_ea_key,
5125 .key_size = osd_it_ea_key_size,
5126 .rec = osd_it_ea_rec,
5127 .store = osd_it_ea_store,
5128 .load = osd_it_ea_load
5132 static void *osd_key_init(const struct lu_context *ctx,
5133 struct lu_context_key *key)
5135 struct osd_thread_info *info;
5137 OBD_ALLOC_PTR(info);
5139 return ERR_PTR(-ENOMEM);
5141 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5142 if (info->oti_it_ea_buf == NULL)
5145 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5147 info->oti_hlock = ldiskfs_htree_lock_alloc();
5148 if (info->oti_hlock == NULL)
5154 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5157 return ERR_PTR(-ENOMEM);
5160 static void osd_key_fini(const struct lu_context *ctx,
5161 struct lu_context_key *key, void* data)
5163 struct osd_thread_info *info = data;
5165 if (info->oti_hlock != NULL)
5166 ldiskfs_htree_lock_free(info->oti_hlock);
5167 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5168 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5169 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5173 static void osd_key_exit(const struct lu_context *ctx,
5174 struct lu_context_key *key, void *data)
5176 struct osd_thread_info *info = data;
5178 LASSERT(info->oti_r_locks == 0);
5179 LASSERT(info->oti_w_locks == 0);
5180 LASSERT(info->oti_txns == 0);
5183 /* type constructor/destructor: osd_type_init, osd_type_fini */
5184 LU_TYPE_INIT_FINI(osd, &osd_key);
5186 struct lu_context_key osd_key = {
5187 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5188 .lct_init = osd_key_init,
5189 .lct_fini = osd_key_fini,
5190 .lct_exit = osd_key_exit
5194 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5195 const char *name, struct lu_device *next)
5197 struct osd_device *osd = osd_dev(d);
5199 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5200 >= sizeof(osd->od_svname))
5202 return osd_procfs_init(osd, name);
5205 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5209 osd_scrub_cleanup(env, o);
5212 fsfilt_put_ops(o->od_fsops);
5216 /* shutdown quota slave instance associated with the device */
5217 if (o->od_quota_slave != NULL) {
5218 qsd_fini(env, o->od_quota_slave);
5219 o->od_quota_slave = NULL;
5225 static int osd_mount(const struct lu_env *env,
5226 struct osd_device *o, struct lustre_cfg *cfg)
5228 const char *name = lustre_cfg_string(cfg, 0);
5229 const char *dev = lustre_cfg_string(cfg, 1);
5231 unsigned long page, s_flags, lmd_flags = 0;
5232 struct page *__page;
5233 struct file_system_type *type;
5234 char *options = NULL;
5239 if (o->od_mnt != NULL)
5242 if (strlen(dev) >= sizeof(o->od_mntdev))
5244 strcpy(o->od_mntdev, dev);
5246 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
5247 if (o->od_fsops == NULL) {
5248 CERROR("Can't find fsfilt_ldiskfs\n");
5252 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
5254 GOTO(out, rc = -ENOMEM);
5256 str = lustre_cfg_string(cfg, 2);
5257 s_flags = simple_strtoul(str, NULL, 0);
5258 str = strstr(str, ":");
5260 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5261 opts = lustre_cfg_string(cfg, 3);
5262 page = (unsigned long)cfs_page_address(__page);
5263 options = (char *)page;
5266 strcat(options, "user_xattr,acl");
5268 strcat(options, opts);
5270 /* Glom up mount options */
5271 if (*options != '\0')
5272 strcat(options, ",");
5273 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
5275 type = get_fs_type("ldiskfs");
5277 CERROR("%s: cannot find ldiskfs module\n", name);
5278 GOTO(out, rc = -ENODEV);
5281 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5282 cfs_module_put(type->owner);
5284 if (IS_ERR(o->od_mnt)) {
5285 rc = PTR_ERR(o->od_mnt);
5286 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5291 #ifdef HAVE_DEV_SET_RDONLY
5292 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5293 CERROR("%s: underlying device %s is marked as read-only. "
5294 "Setup failed\n", name, dev);
5297 GOTO(out, rc = -EROFS);
5301 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5302 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5303 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5306 GOTO(out, rc = -EINVAL);
5309 ldiskfs_set_inode_state(osd_sb(o)->s_root->d_inode,
5310 LDISKFS_STATE_LUSTRE_NO_OI);
5311 if (lmd_flags & LMD_FLG_NOSCRUB)
5316 OBD_PAGE_FREE(__page);
5318 fsfilt_put_ops(o->od_fsops);
5323 static struct lu_device *osd_device_fini(const struct lu_env *env,
5324 struct lu_device *d)
5329 rc = osd_shutdown(env, osd_dev(d));
5331 osd_obj_map_fini(osd_dev(d));
5333 shrink_dcache_sb(osd_sb(osd_dev(d)));
5334 osd_sync(env, lu2dt_dev(d));
5336 rc = osd_procfs_fini(osd_dev(d));
5338 CERROR("proc fini error %d \n", rc);
5339 RETURN (ERR_PTR(rc));
5342 if (osd_dev(d)->od_mnt) {
5343 mntput(osd_dev(d)->od_mnt);
5344 osd_dev(d)->od_mnt = NULL;
5350 static int osd_device_init0(const struct lu_env *env,
5351 struct osd_device *o,
5352 struct lustre_cfg *cfg)
5354 struct lu_device *l = osd2lu_dev(o);
5355 struct osd_thread_info *info;
5359 /* if the module was re-loaded, env can loose its keys */
5360 rc = lu_env_refill((struct lu_env *) env);
5363 info = osd_oti_get(env);
5366 l->ld_ops = &osd_lu_ops;
5367 o->od_dt_dev.dd_ops = &osd_dt_ops;
5369 spin_lock_init(&o->od_osfs_lock);
5370 mutex_init(&o->od_otable_mutex);
5371 o->od_osfs_age = cfs_time_shift_64(-1000);
5373 o->od_capa_hash = init_capa_hash();
5374 if (o->od_capa_hash == NULL)
5375 GOTO(out, rc = -ENOMEM);
5377 o->od_read_cache = 1;
5378 o->od_writethrough_cache = 1;
5379 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5381 rc = osd_mount(env, o, cfg);
5385 CFS_INIT_LIST_HEAD(&o->od_ios_list);
5386 /* setup scrub, including OI files initialization */
5387 rc = osd_scrub_setup(env, o);
5391 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5392 sizeof(o->od_svname));
5393 if (cplen >= sizeof(o->od_svname)) {
5398 rc = osd_obj_map_init(env, o);
5400 GOTO(out_scrub, rc);
5402 rc = lu_site_init(&o->od_site, l);
5404 GOTO(out_compat, rc);
5405 o->od_site.ls_bottom_dev = l;
5407 rc = lu_site_init_finish(&o->od_site);
5411 rc = osd_procfs_init(o, o->od_svname);
5413 CERROR("%s: can't initialize procfs: rc = %d\n",
5418 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5420 /* initialize quota slave instance */
5421 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5423 if (IS_ERR(o->od_quota_slave)) {
5424 rc = PTR_ERR(o->od_quota_slave);
5425 o->od_quota_slave = NULL;
5426 GOTO(out_procfs, rc);
5433 lu_site_fini(&o->od_site);
5435 osd_obj_map_fini(o);
5437 osd_scrub_cleanup(env, o);
5439 osd_oi_fini(info, o);
5440 osd_shutdown(env, o);
5444 cleanup_capa_hash(o->od_capa_hash);
5449 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5450 struct lu_device_type *t,
5451 struct lustre_cfg *cfg)
5453 struct osd_device *o;
5458 return ERR_PTR(-ENOMEM);
5460 rc = dt_device_init(&o->od_dt_dev, t);
5462 /* Because the ctx might be revived in dt_device_init,
5463 * refill the env here */
5464 lu_env_refill((struct lu_env *)env);
5465 rc = osd_device_init0(env, o, cfg);
5467 dt_device_fini(&o->od_dt_dev);
5470 if (unlikely(rc != 0))
5473 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5476 static struct lu_device *osd_device_free(const struct lu_env *env,
5477 struct lu_device *d)
5479 struct osd_device *o = osd_dev(d);
5482 cleanup_capa_hash(o->od_capa_hash);
5483 /* XXX: make osd top device in order to release reference */
5484 d->ld_site->ls_top_dev = d;
5485 lu_site_purge(env, d->ld_site, -1);
5486 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5487 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5488 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5490 lu_site_fini(&o->od_site);
5491 dt_device_fini(&o->od_dt_dev);
5496 static int osd_process_config(const struct lu_env *env,
5497 struct lu_device *d, struct lustre_cfg *cfg)
5499 struct osd_device *o = osd_dev(d);
5503 switch(cfg->lcfg_command) {
5505 err = osd_mount(env, o, cfg);
5508 lu_dev_del_linkage(d->ld_site, d);
5509 err = osd_shutdown(env, o);
5518 static int osd_recovery_complete(const struct lu_env *env,
5519 struct lu_device *d)
5521 struct osd_device *osd = osd_dev(d);
5525 if (osd->od_quota_slave == NULL)
5528 /* start qsd instance on recovery completion, this notifies the quota
5529 * slave code that we are about to process new requests now */
5530 rc = qsd_start(env, osd->od_quota_slave);
5535 * we use exports to track all osd users
5537 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5538 struct obd_device *obd, struct obd_uuid *cluuid,
5539 struct obd_connect_data *data, void *localdata)
5541 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5542 struct lustre_handle conn;
5546 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5548 rc = class_connect(&conn, obd, cluuid);
5552 *exp = class_conn2export(&conn);
5554 spin_lock(&osd->od_osfs_lock);
5556 spin_unlock(&osd->od_osfs_lock);
5562 * once last export (we don't count self-export) disappeared
5563 * osd can be released
5565 static int osd_obd_disconnect(struct obd_export *exp)
5567 struct obd_device *obd = exp->exp_obd;
5568 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5569 int rc, release = 0;
5572 /* Only disconnect the underlying layers on the final disconnect. */
5573 spin_lock(&osd->od_osfs_lock);
5575 if (osd->od_connects == 0)
5577 spin_unlock(&osd->od_osfs_lock);
5579 rc = class_disconnect(exp); /* bz 9811 */
5581 if (rc == 0 && release)
5582 class_manual_cleanup(obd);
5586 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5587 struct lu_device *dev)
5589 struct osd_device *osd = osd_dev(dev);
5593 if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
5594 /* MDT/MDD still use old infrastructure to create
5596 result = llo_local_objects_setup(env, lu2md_dev(pdev),
5602 if (osd->od_quota_slave != NULL)
5603 /* set up quota slave objects */
5604 result = qsd_prepare(env, osd->od_quota_slave);
5609 static const struct lu_object_operations osd_lu_obj_ops = {
5610 .loo_object_init = osd_object_init,
5611 .loo_object_delete = osd_object_delete,
5612 .loo_object_release = osd_object_release,
5613 .loo_object_free = osd_object_free,
5614 .loo_object_print = osd_object_print,
5615 .loo_object_invariant = osd_object_invariant
5618 const struct lu_device_operations osd_lu_ops = {
5619 .ldo_object_alloc = osd_object_alloc,
5620 .ldo_process_config = osd_process_config,
5621 .ldo_recovery_complete = osd_recovery_complete,
5622 .ldo_prepare = osd_prepare,
5625 static const struct lu_device_type_operations osd_device_type_ops = {
5626 .ldto_init = osd_type_init,
5627 .ldto_fini = osd_type_fini,
5629 .ldto_start = osd_type_start,
5630 .ldto_stop = osd_type_stop,
5632 .ldto_device_alloc = osd_device_alloc,
5633 .ldto_device_free = osd_device_free,
5635 .ldto_device_init = osd_device_init,
5636 .ldto_device_fini = osd_device_fini
5639 struct lu_device_type osd_device_type = {
5640 .ldt_tags = LU_DEVICE_DT,
5641 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
5642 .ldt_ops = &osd_device_type_ops,
5643 .ldt_ctx_tags = LCT_LOCAL,
5647 * lprocfs legacy support.
5649 static struct obd_ops osd_obd_device_ops = {
5650 .o_owner = THIS_MODULE,
5651 .o_connect = osd_obd_connect,
5652 .o_disconnect = osd_obd_disconnect
5655 static int __init osd_mod_init(void)
5657 struct lprocfs_static_vars lvars;
5660 lprocfs_osd_init_vars(&lvars);
5661 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
5662 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5665 static void __exit osd_mod_exit(void)
5667 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5670 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5671 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5672 MODULE_LICENSE("GPL");
5674 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);