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 return inode->i_op->getxattr(dentry, name, buf, len);
178 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
179 struct dentry *dentry, struct lustre_mdt_attrs *lma)
183 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA, (void *)lma,
186 /* try with old lma size */
187 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA,
188 info->oti_mdt_attrs_old,
191 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
194 /* Check LMA compatibility */
195 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
196 CWARN("%.16s: unsupported incompat LMA feature(s) "
198 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
199 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
203 lustre_lma_swab(lma);
206 } else if (rc == 0) {
214 * retrieve object from backend ext fs.
216 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
217 struct osd_inode_id *id)
219 struct inode *inode = NULL;
221 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
223 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
224 id->oii_ino, PTR_ERR(inode));
225 } else if (id->oii_gen != OSD_OII_NOGEN &&
226 inode->i_generation != id->oii_gen) {
227 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
229 id->oii_ino, id->oii_gen, inode->i_generation);
231 inode = ERR_PTR(-ESTALE);
232 } else if (inode->i_nlink == 0) {
233 /* due to parallel readdir and unlink,
234 * we can have dead inode here. */
235 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
236 make_bad_inode(inode);
238 inode = ERR_PTR(-ESTALE);
239 } else if (is_bad_inode(inode)) {
240 CWARN("%.16s: bad inode: ino = %u\n",
241 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
243 inode = ERR_PTR(-ENOENT);
245 if (id->oii_gen == OSD_OII_NOGEN)
246 osd_id_gen(id, inode->i_ino, inode->i_generation);
248 /* Do not update file c/mtime in ldiskfs.
249 * NB: we don't have any lock to protect this because we don't
250 * have reference on osd_object now, but contention with
251 * another lookup + attr_set can't happen in the tiny window
252 * between if (...) and set S_NOCMTIME. */
253 if (!(inode->i_flags & S_NOCMTIME))
254 inode->i_flags |= S_NOCMTIME;
259 static struct inode *
260 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
261 struct osd_inode_id *id, struct lu_fid *fid)
263 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
267 inode = osd_iget(info, dev, id);
271 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
273 *fid = lma->lma_self_fid;
274 } else if (rc == -ENODATA) {
275 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
276 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
278 lu_igif_build(fid, inode->i_ino, inode->i_generation);
286 static struct inode *
287 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
288 struct osd_inode_id *id, const struct lu_fid *fid)
290 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
294 inode = osd_iget(info, dev, id);
298 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
307 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
308 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
309 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
311 return ERR_PTR(-EREMCHG);
317 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
318 const struct lu_fid *fid,
319 const struct lu_object_conf *conf)
321 struct osd_thread_info *info;
322 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
323 struct osd_device *dev;
324 struct osd_idmap_cache *oic;
325 struct osd_inode_id *id;
327 struct osd_scrub *scrub;
328 struct scrub_file *sf;
333 LINVRNT(osd_invariant(obj));
334 LASSERT(obj->oo_inode == NULL);
335 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
338 scrub = &dev->od_scrub;
339 sf = &scrub->os_file;
340 info = osd_oti_get(env);
342 oic = &info->oti_cache;
344 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
347 /* Search order: 1. per-thread cache. */
348 if (lu_fid_eq(fid, &oic->oic_fid)) {
354 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
355 /* Search order: 2. OI scrub pending list. */
356 result = osd_oii_lookup(dev, fid, id);
361 if (sf->sf_flags & SF_INCONSISTENT)
365 * Objects are created as locking anchors or place holders for objects
366 * yet to be created. No need to osd_oi_lookup() at here because FID
367 * shouldn't never be re-used, if it's really a duplicate FID from
368 * unexpected reason, we should be able to detect it later by calling
369 * do_create->osd_oi_insert()
371 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
372 GOTO(out, result = 0);
374 /* Search order: 3. OI files. */
375 result = osd_oi_lookup(info, dev, fid, id, true);
376 if (result == -ENOENT) {
377 if (!fid_is_norm(fid) || fid_is_on_ost(info, dev, fid) ||
378 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
380 GOTO(out, result = 0);
390 inode = osd_iget(info, dev, id);
392 inode = osd_iget_verify(info, dev, id, fid);
394 result = PTR_ERR(inode);
395 if (result == -ENOENT || result == -ESTALE) {
396 fid_zero(&oic->oic_fid);
398 } else if (result == -EREMCHG) {
401 if (thread_is_running(&scrub->os_thread)) {
402 result = -EINPROGRESS;
403 } else if (!dev->od_noscrub) {
404 result = osd_scrub_start(dev);
405 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
406 "for "DFID", rc = %d [1]\n",
407 LDISKFS_SB(osd_sb(dev))->s_es->\
408 s_volume_name,PFID(fid), result);
409 if (result == 0 || result == -EALREADY)
410 result = -EINPROGRESS;
419 obj->oo_inode = inode;
420 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
422 obj->oo_compat_dot_created = 1;
423 obj->oo_compat_dotdot_created = 1;
425 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
426 GOTO(out, result = 0);
428 LASSERT(obj->oo_hl_head == NULL);
429 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
430 if (obj->oo_hl_head == NULL) {
431 obj->oo_inode = NULL;
433 GOTO(out, result = -ENOMEM);
435 GOTO(out, result = 0);
438 LINVRNT(osd_invariant(obj));
443 * Concurrency: shouldn't matter.
445 static void osd_object_init0(struct osd_object *obj)
447 LASSERT(obj->oo_inode != NULL);
448 obj->oo_dt.do_body_ops = &osd_body_ops;
449 obj->oo_dt.do_lu.lo_header->loh_attr |=
450 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
454 * Concurrency: no concurrent access is possible that early in object
457 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
458 const struct lu_object_conf *conf)
460 struct osd_object *obj = osd_obj(l);
463 LINVRNT(osd_invariant(obj));
465 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
466 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
467 l->lo_header->loh_attr |= LOHA_EXISTS;
471 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
472 obj->oo_dt.do_body_ops = &osd_body_ops_new;
473 if (result == 0 && obj->oo_inode != NULL)
474 osd_object_init0(obj);
476 LINVRNT(osd_invariant(obj));
481 * Concurrency: no concurrent access is possible that late in object
484 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
486 struct osd_object *obj = osd_obj(l);
488 LINVRNT(osd_invariant(obj));
490 dt_object_fini(&obj->oo_dt);
491 if (obj->oo_hl_head != NULL)
492 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
497 * Concurrency: no concurrent access is possible that late in object
500 static void osd_index_fini(struct osd_object *o)
502 struct iam_container *bag;
504 if (o->oo_dir != NULL) {
505 bag = &o->oo_dir->od_container;
506 if (o->oo_inode != NULL) {
507 if (bag->ic_object == o->oo_inode)
508 iam_container_fini(bag);
510 OBD_FREE_PTR(o->oo_dir);
516 * Concurrency: no concurrent access is possible that late in object
517 * life-cycle (for all existing callers, that is. New callers have to provide
518 * their own locking.)
520 static int osd_inode_unlinked(const struct inode *inode)
522 return inode->i_nlink == 0;
526 OSD_TXN_OI_DELETE_CREDITS = 20,
527 OSD_TXN_INODE_DELETE_CREDITS = 20
534 #if OSD_THANDLE_STATS
536 * Set time when the handle is allocated
538 static void osd_th_alloced(struct osd_thandle *oth)
540 oth->oth_alloced = cfs_time_current();
544 * Set time when the handle started
546 static void osd_th_started(struct osd_thandle *oth)
548 oth->oth_started = cfs_time_current();
552 * Helper function to convert time interval to microseconds packed in
555 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
559 cfs_duration_usec(cfs_time_sub(end, start), &val);
560 return val.tv_sec * 1000000 + val.tv_usec;
564 * Check whether the we deal with this handle for too long.
566 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
567 cfs_time_t alloced, cfs_time_t started,
570 cfs_time_t now = cfs_time_current();
572 LASSERT(dev != NULL);
574 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
575 interval_to_usec(alloced, started));
576 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
577 interval_to_usec(started, closed));
578 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
579 interval_to_usec(closed, now));
581 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
582 CWARN("transaction handle %p was open for too long: "
584 "alloced "CFS_TIME_T" ,"
585 "started "CFS_TIME_T" ,"
586 "closed "CFS_TIME_T"\n",
587 oth, now, alloced, started, closed);
588 libcfs_debug_dumpstack(NULL);
592 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
594 cfs_time_t __closed = cfs_time_current(); \
595 cfs_time_t __alloced = oth->oth_alloced; \
596 cfs_time_t __started = oth->oth_started; \
599 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
602 #else /* OSD_THANDLE_STATS */
604 #define osd_th_alloced(h) do {} while(0)
605 #define osd_th_started(h) do {} while(0)
606 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
608 #endif /* OSD_THANDLE_STATS */
611 * Concurrency: doesn't access mutable data.
613 static int osd_param_is_not_sane(const struct osd_device *dev,
614 const struct thandle *th)
616 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
618 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
622 * Concurrency: shouldn't matter.
624 static void osd_trans_commit_cb(struct super_block *sb,
625 struct ldiskfs_journal_cb_entry *jcb, int error)
627 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
628 struct thandle *th = &oh->ot_super;
629 struct lu_device *lud = &th->th_dev->dd_lu_dev;
630 struct dt_txn_commit_cb *dcb, *tmp;
632 LASSERT(oh->ot_handle == NULL);
635 CERROR("transaction @0x%p commit error: %d\n", th, error);
637 dt_txn_hook_commit(th);
639 /* call per-transaction callbacks if any */
640 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
641 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
642 "commit callback entry: magic=%x name='%s'\n",
643 dcb->dcb_magic, dcb->dcb_name);
644 cfs_list_del_init(&dcb->dcb_linkage);
645 dcb->dcb_func(NULL, th, dcb, error);
648 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
652 lu_context_exit(&th->th_ctx);
653 lu_context_fini(&th->th_ctx);
657 static struct thandle *osd_trans_create(const struct lu_env *env,
660 struct osd_thread_info *oti = osd_oti_get(env);
661 struct osd_iobuf *iobuf = &oti->oti_iobuf;
662 struct osd_thandle *oh;
666 /* on pending IO in this thread should left from prev. request */
667 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
669 th = ERR_PTR(-ENOMEM);
670 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
672 oh->ot_quota_trans = &oti->oti_quota_trans;
673 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
677 th->th_tags = LCT_TX_HANDLE;
679 oti->oti_dev = osd_dt_dev(d);
680 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
683 memset(oti->oti_declare_ops, 0, OSD_OT_MAX);
684 memset(oti->oti_declare_ops_rb, 0, OSD_OT_MAX);
685 memset(oti->oti_declare_ops_cred, 0, OSD_OT_MAX);
686 oti->oti_rollback = false;
692 * Concurrency: shouldn't matter.
694 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
697 struct osd_thread_info *oti = osd_oti_get(env);
698 struct osd_device *dev = osd_dt_dev(d);
700 struct osd_thandle *oh;
705 LASSERT(current->journal_info == NULL);
707 oh = container_of0(th, struct osd_thandle, ot_super);
709 LASSERT(oh->ot_handle == NULL);
711 rc = dt_txn_hook_start(env, d, th);
715 if (unlikely(osd_param_is_not_sane(dev, th))) {
716 static unsigned long last_printed;
717 static int last_credits;
719 CWARN("%.16s: too many transaction credits (%d > %d)\n",
720 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
722 osd_journal(dev)->j_max_transaction_buffers);
723 #ifdef OSD_TRACK_DECLARES
724 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
725 oti->oti_declare_ops[OSD_OT_CREATE],
726 oti->oti_declare_ops_cred[OSD_OT_CREATE],
727 oti->oti_declare_ops[OSD_OT_DELETE],
728 oti->oti_declare_ops_cred[OSD_OT_DELETE],
729 oti->oti_declare_ops[OSD_OT_DESTROY],
730 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
731 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
732 oti->oti_declare_ops[OSD_OT_ATTR_SET],
733 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
734 oti->oti_declare_ops[OSD_OT_XATTR_SET],
735 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
736 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
737 oti->oti_declare_ops[OSD_OT_WRITE],
738 oti->oti_declare_ops_cred[OSD_OT_WRITE],
739 oti->oti_declare_ops[OSD_OT_PUNCH],
740 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
741 oti->oti_declare_ops[OSD_OT_QUOTA],
742 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
743 CWARN(" insert: %u/%u, delete: %u/%u\n",
744 oti->oti_declare_ops[OSD_OT_INSERT],
745 oti->oti_declare_ops_cred[OSD_OT_INSERT],
746 oti->oti_declare_ops[OSD_OT_DESTROY],
747 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
748 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
749 oti->oti_declare_ops[OSD_OT_REF_ADD],
750 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
751 oti->oti_declare_ops[OSD_OT_REF_DEL],
752 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
754 if (last_credits != oh->ot_credits &&
755 time_after(jiffies, last_printed + 60 * HZ)) {
756 libcfs_debug_dumpstack(NULL);
757 last_credits = oh->ot_credits;
758 last_printed = jiffies;
761 /* XXX Limit the credits to 'max_transaction_buffers', and
762 * let the underlying filesystem to catch the error if
763 * we really need so many credits.
765 * This should be removed when we can calculate the
766 * credits precisely. */
767 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
771 * XXX temporary stuff. Some abstraction layer should
774 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
778 LASSERT(oti->oti_txns == 0);
779 lu_context_init(&th->th_ctx, th->th_tags);
780 lu_context_enter(&th->th_ctx);
782 lu_device_get(&d->dd_lu_dev);
783 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
795 * Concurrency: shouldn't matter.
797 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
800 struct osd_thandle *oh;
801 struct osd_thread_info *oti = osd_oti_get(env);
802 struct osd_iobuf *iobuf = &oti->oti_iobuf;
803 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
806 oh = container_of0(th, struct osd_thandle, ot_super);
809 /* inform the quota slave device that the transaction is
811 qsd_op_end(env, qsd, oh->ot_quota_trans);
812 oh->ot_quota_trans = NULL;
814 if (oh->ot_handle != NULL) {
815 handle_t *hdl = oh->ot_handle;
818 * add commit callback
819 * notice we don't do this in osd_trans_start()
820 * as underlying transaction can change during truncate
822 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
825 LASSERT(oti->oti_txns == 1);
827 rc = dt_txn_hook_stop(env, th);
829 CERROR("Failure in transaction hook: %d\n", rc);
831 /* hook functions might modify th_sync */
832 hdl->h_sync = th->th_sync;
834 oh->ot_handle = NULL;
835 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
836 rc = ldiskfs_journal_stop(hdl));
838 CERROR("Failure to stop transaction: %d\n", rc);
843 /* as we want IO to journal and data IO be concurrent, we don't block
844 * awaiting data IO completion in osd_do_bio(), instead we wait here
845 * once transaction is submitted to the journal. all reqular requests
846 * don't do direct IO (except read/write), thus this wait_event becomes
849 * IMPORTANT: we have to wait till any IO submited by the thread is
850 * completed otherwise iobuf may be corrupted by different request
852 cfs_wait_event(iobuf->dr_wait,
853 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
855 rc = iobuf->dr_error;
860 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
862 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
865 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
866 LASSERT(&dcb->dcb_func != NULL);
867 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
873 * Called just before object is freed. Releases all resources except for
874 * object itself (that is released by osd_object_free()).
876 * Concurrency: no concurrent access is possible that late in object
879 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
881 struct osd_object *obj = osd_obj(l);
882 struct inode *inode = obj->oo_inode;
884 LINVRNT(osd_invariant(obj));
887 * If object is unlinked remove fid->ino mapping from object index.
892 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
893 qid_t uid = inode->i_uid;
894 qid_t gid = inode->i_gid;
897 obj->oo_inode = NULL;
900 struct osd_thread_info *info = osd_oti_get(env);
901 struct lquota_id_info *qi = &info->oti_qi;
903 /* Release granted quota to master if necessary */
904 qi->lqi_id.qid_uid = uid;
905 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
907 qi->lqi_id.qid_uid = gid;
908 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
914 * Concurrency: ->loo_object_release() is called under site spin-lock.
916 static void osd_object_release(const struct lu_env *env,
922 * Concurrency: shouldn't matter.
924 static int osd_object_print(const struct lu_env *env, void *cookie,
925 lu_printer_t p, const struct lu_object *l)
927 struct osd_object *o = osd_obj(l);
930 if (o->oo_dir != NULL)
931 d = o->oo_dir->od_container.ic_descr;
934 return (*p)(env, cookie,
935 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
937 o->oo_inode ? o->oo_inode->i_ino : 0UL,
938 o->oo_inode ? o->oo_inode->i_generation : 0,
939 d ? d->id_ops->id_name : "plain");
943 * Concurrency: shouldn't matter.
945 int osd_statfs(const struct lu_env *env, struct dt_device *d,
946 struct obd_statfs *sfs)
948 struct osd_device *osd = osd_dt_dev(d);
949 struct super_block *sb = osd_sb(osd);
950 struct kstatfs *ksfs;
953 if (unlikely(osd->od_mnt == NULL))
956 /* osd_lproc.c call this without env, allocate ksfs for that case */
957 if (unlikely(env == NULL)) {
962 ksfs = &osd_oti_get(env)->oti_ksfs;
965 spin_lock(&osd->od_osfs_lock);
967 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
968 result = sb->s_op->statfs(sb->s_root, ksfs);
969 if (likely(result == 0)) { /* N.B. statfs can't really fail */
970 osd->od_osfs_age = cfs_time_current_64();
971 statfs_pack(&osd->od_statfs, ksfs);
972 if (sb->s_flags & MS_RDONLY)
973 sfs->os_state = OS_STATE_READONLY;
977 if (likely(result == 0))
978 *sfs = osd->od_statfs;
979 spin_unlock(&osd->od_osfs_lock);
981 if (unlikely(env == NULL))
988 * Estimate space needed for file creations. We assume the largest filename
989 * which is 2^64 - 1, hence a filename of 20 chars.
990 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
992 #ifdef __LDISKFS_DIR_REC_LEN
993 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
995 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
999 * Concurrency: doesn't access mutable data.
1001 static void osd_conf_get(const struct lu_env *env,
1002 const struct dt_device *dev,
1003 struct dt_device_param *param)
1005 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1008 * XXX should be taken from not-yet-existing fs abstraction layer.
1010 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1011 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1012 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1013 param->ddp_block_shift = sb->s_blocksize_bits;
1014 param->ddp_mount_type = LDD_MT_LDISKFS;
1015 param->ddp_maxbytes = sb->s_maxbytes;
1016 /* Overhead estimate should be fairly accurate, so we really take a tiny
1017 * error margin which also avoids fragmenting the filesystem too much */
1018 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1019 /* inode are statically allocated, so per-inode space consumption
1020 * is the space consumed by the directory entry */
1021 param->ddp_inodespace = PER_OBJ_USAGE;
1022 /* per-fragment overhead to be used by the client code */
1023 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1024 param->ddp_mntopts = 0;
1025 if (test_opt(sb, XATTR_USER))
1026 param->ddp_mntopts |= MNTOPT_USERXATTR;
1027 if (test_opt(sb, POSIX_ACL))
1028 param->ddp_mntopts |= MNTOPT_ACL;
1030 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1031 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1032 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1035 param->ddp_max_ea_size = sb->s_blocksize;
1040 * Concurrency: shouldn't matter.
1042 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1044 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1045 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1049 * Start commit for OSD device.
1051 * An implementation of dt_commit_async method for OSD device.
1052 * Asychronously starts underlayng fs sync and thereby a transaction
1055 * \param env environment
1056 * \param d dt device
1058 * \see dt_device_operations
1060 static int osd_commit_async(const struct lu_env *env,
1061 struct dt_device *d)
1063 struct super_block *s = osd_sb(osd_dt_dev(d));
1066 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1067 RETURN(s->s_op->sync_fs(s, 0));
1071 * Concurrency: shouldn't matter.
1074 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1076 struct super_block *sb = osd_sb(osd_dt_dev(d));
1077 struct block_device *dev = sb->s_bdev;
1078 #ifdef HAVE_DEV_SET_RDONLY
1079 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1082 int rc = -EOPNOTSUPP;
1086 #ifdef HAVE_DEV_SET_RDONLY
1087 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1089 if (jdev && (jdev != dev)) {
1090 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1092 dev_set_rdonly(jdev);
1094 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1095 dev_set_rdonly(dev);
1097 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1098 osd_dt_dev(d)->od_svname, (long)dev, rc);
1104 * Concurrency: serialization provided by callers.
1106 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1107 int mode, unsigned long timeout, __u32 alg,
1108 struct lustre_capa_key *keys)
1110 struct osd_device *dev = osd_dt_dev(d);
1113 dev->od_fl_capa = mode;
1114 dev->od_capa_timeout = timeout;
1115 dev->od_capa_alg = alg;
1116 dev->od_capa_keys = keys;
1121 * Note: we do not count into QUOTA here.
1122 * If we mount with --data_journal we may need more.
1124 const int osd_dto_credits_noquota[DTO_NR] = {
1127 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1128 * SINGLEDATA_TRANS_BLOCKS(8)
1129 * XXX Note: maybe iam need more, since iam have more level than
1132 [DTO_INDEX_INSERT] = 16,
1133 [DTO_INDEX_DELETE] = 16,
1137 [DTO_INDEX_UPDATE] = 16,
1139 * Create a object. The same as create object in EXT3.
1140 * DATA_TRANS_BLOCKS(14) +
1141 * INDEX_EXTRA_BLOCKS(8) +
1142 * 3(inode bits, groups, GDT)
1144 [DTO_OBJECT_CREATE] = 25,
1146 * XXX: real credits to be fixed
1148 [DTO_OBJECT_DELETE] = 25,
1150 * Attr set credits (inode)
1152 [DTO_ATTR_SET_BASE] = 1,
1154 * Xattr set. The same as xattr of EXT3.
1155 * DATA_TRANS_BLOCKS(14)
1156 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1157 * are also counted in. Do not know why?
1159 [DTO_XATTR_SET] = 14,
1162 * credits for inode change during write.
1164 [DTO_WRITE_BASE] = 3,
1166 * credits for single block write.
1168 [DTO_WRITE_BLOCK] = 14,
1170 * Attr set credits for chown.
1171 * This is extra credits for setattr, and it is null without quota
1173 [DTO_ATTR_SET_CHOWN]= 0
1176 static const struct dt_device_operations osd_dt_ops = {
1177 .dt_root_get = osd_root_get,
1178 .dt_statfs = osd_statfs,
1179 .dt_trans_create = osd_trans_create,
1180 .dt_trans_start = osd_trans_start,
1181 .dt_trans_stop = osd_trans_stop,
1182 .dt_trans_cb_add = osd_trans_cb_add,
1183 .dt_conf_get = osd_conf_get,
1184 .dt_sync = osd_sync,
1186 .dt_commit_async = osd_commit_async,
1187 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1190 static void osd_object_read_lock(const struct lu_env *env,
1191 struct dt_object *dt, unsigned role)
1193 struct osd_object *obj = osd_dt_obj(dt);
1194 struct osd_thread_info *oti = osd_oti_get(env);
1196 LINVRNT(osd_invariant(obj));
1198 LASSERT(obj->oo_owner != env);
1199 down_read_nested(&obj->oo_sem, role);
1201 LASSERT(obj->oo_owner == NULL);
1205 static void osd_object_write_lock(const struct lu_env *env,
1206 struct dt_object *dt, unsigned role)
1208 struct osd_object *obj = osd_dt_obj(dt);
1209 struct osd_thread_info *oti = osd_oti_get(env);
1211 LINVRNT(osd_invariant(obj));
1213 LASSERT(obj->oo_owner != env);
1214 down_write_nested(&obj->oo_sem, role);
1216 LASSERT(obj->oo_owner == NULL);
1217 obj->oo_owner = env;
1221 static void osd_object_read_unlock(const struct lu_env *env,
1222 struct dt_object *dt)
1224 struct osd_object *obj = osd_dt_obj(dt);
1225 struct osd_thread_info *oti = osd_oti_get(env);
1227 LINVRNT(osd_invariant(obj));
1229 LASSERT(oti->oti_r_locks > 0);
1231 up_read(&obj->oo_sem);
1234 static void osd_object_write_unlock(const struct lu_env *env,
1235 struct dt_object *dt)
1237 struct osd_object *obj = osd_dt_obj(dt);
1238 struct osd_thread_info *oti = osd_oti_get(env);
1240 LINVRNT(osd_invariant(obj));
1242 LASSERT(obj->oo_owner == env);
1243 LASSERT(oti->oti_w_locks > 0);
1245 obj->oo_owner = NULL;
1246 up_write(&obj->oo_sem);
1249 static int osd_object_write_locked(const struct lu_env *env,
1250 struct dt_object *dt)
1252 struct osd_object *obj = osd_dt_obj(dt);
1254 LINVRNT(osd_invariant(obj));
1256 return obj->oo_owner == env;
1259 static int capa_is_sane(const struct lu_env *env,
1260 struct osd_device *dev,
1261 struct lustre_capa *capa,
1262 struct lustre_capa_key *keys)
1264 struct osd_thread_info *oti = osd_oti_get(env);
1265 struct lustre_capa *tcapa = &oti->oti_capa;
1266 struct obd_capa *oc;
1270 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1272 if (capa_is_expired(oc)) {
1273 DEBUG_CAPA(D_ERROR, capa, "expired");
1280 if (capa_is_expired_sec(capa)) {
1281 DEBUG_CAPA(D_ERROR, capa, "expired");
1285 spin_lock(&capa_lock);
1286 for (i = 0; i < 2; i++) {
1287 if (keys[i].lk_keyid == capa->lc_keyid) {
1288 oti->oti_capa_key = keys[i];
1292 spin_unlock(&capa_lock);
1295 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1299 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1303 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1304 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1308 oc = capa_add(dev->od_capa_hash, capa);
1314 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1315 struct lustre_capa *capa, __u64 opc)
1317 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1318 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1319 struct md_capainfo *ci;
1322 if (!dev->od_fl_capa)
1325 if (capa == BYPASS_CAPA)
1328 ci = md_capainfo(env);
1332 if (ci->mc_auth == LC_ID_NONE)
1336 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1340 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1341 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1346 if (!capa_opc_supported(capa, opc)) {
1347 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1351 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1352 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1359 static struct timespec *osd_inode_time(const struct lu_env *env,
1360 struct inode *inode, __u64 seconds)
1362 struct osd_thread_info *oti = osd_oti_get(env);
1363 struct timespec *t = &oti->oti_time;
1365 t->tv_sec = seconds;
1367 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1372 static void osd_inode_getattr(const struct lu_env *env,
1373 struct inode *inode, struct lu_attr *attr)
1375 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1376 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1377 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1380 attr->la_atime = LTIME_S(inode->i_atime);
1381 attr->la_mtime = LTIME_S(inode->i_mtime);
1382 attr->la_ctime = LTIME_S(inode->i_ctime);
1383 attr->la_mode = inode->i_mode;
1384 attr->la_size = i_size_read(inode);
1385 attr->la_blocks = inode->i_blocks;
1386 attr->la_uid = inode->i_uid;
1387 attr->la_gid = inode->i_gid;
1388 attr->la_flags = LDISKFS_I(inode)->i_flags;
1389 attr->la_nlink = inode->i_nlink;
1390 attr->la_rdev = inode->i_rdev;
1391 attr->la_blksize = 1 << inode->i_blkbits;
1392 attr->la_blkbits = inode->i_blkbits;
1395 static int osd_attr_get(const struct lu_env *env,
1396 struct dt_object *dt,
1397 struct lu_attr *attr,
1398 struct lustre_capa *capa)
1400 struct osd_object *obj = osd_dt_obj(dt);
1402 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1403 LINVRNT(osd_invariant(obj));
1405 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1408 spin_lock(&obj->oo_guard);
1409 osd_inode_getattr(env, obj->oo_inode, attr);
1410 spin_unlock(&obj->oo_guard);
1414 static int osd_declare_attr_set(const struct lu_env *env,
1415 struct dt_object *dt,
1416 const struct lu_attr *attr,
1417 struct thandle *handle)
1419 struct osd_thandle *oh;
1420 struct osd_object *obj;
1421 struct osd_thread_info *info = osd_oti_get(env);
1422 struct lquota_id_info *qi = &info->oti_qi;
1428 LASSERT(dt != NULL);
1429 LASSERT(handle != NULL);
1431 obj = osd_dt_obj(dt);
1432 LASSERT(osd_invariant(obj));
1434 oh = container_of0(handle, struct osd_thandle, ot_super);
1435 LASSERT(oh->ot_handle == NULL);
1437 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1438 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1440 if (attr == NULL || obj->oo_inode == NULL)
1443 bspace = obj->oo_inode->i_blocks;
1444 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1445 bspace = toqb(bspace);
1447 /* Changing ownership is always preformed by super user, it should not
1450 * We still need to call the osd_declare_qid() to calculate the journal
1451 * credits for updating quota accounting files and to trigger quota
1452 * space adjustment once the operation is completed.*/
1453 if ((attr->la_valid & LA_UID) != 0 &&
1454 attr->la_uid != obj->oo_inode->i_uid) {
1455 qi->lqi_type = USRQUOTA;
1457 /* inode accounting */
1458 qi->lqi_is_blk = false;
1460 /* one more inode for the new owner ... */
1461 qi->lqi_id.qid_uid = attr->la_uid;
1463 allocated = (attr->la_uid == 0) ? true : false;
1464 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1465 if (rc == -EDQUOT || rc == -EINPROGRESS)
1470 /* and one less inode for the current uid */
1471 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1473 rc = osd_declare_qid(env, oh, qi, true, NULL);
1474 if (rc == -EDQUOT || rc == -EINPROGRESS)
1479 /* block accounting */
1480 qi->lqi_is_blk = true;
1482 /* more blocks for the new owner ... */
1483 qi->lqi_id.qid_uid = attr->la_uid;
1484 qi->lqi_space = bspace;
1485 allocated = (attr->la_uid == 0) ? true : false;
1486 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1487 if (rc == -EDQUOT || rc == -EINPROGRESS)
1492 /* and finally less blocks for the current owner */
1493 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1494 qi->lqi_space = -bspace;
1495 rc = osd_declare_qid(env, oh, qi, true, NULL);
1496 if (rc == -EDQUOT || rc == -EINPROGRESS)
1502 if (attr->la_valid & LA_GID &&
1503 attr->la_gid != obj->oo_inode->i_gid) {
1504 qi->lqi_type = GRPQUOTA;
1506 /* inode accounting */
1507 qi->lqi_is_blk = false;
1509 /* one more inode for the new group owner ... */
1510 qi->lqi_id.qid_gid = attr->la_gid;
1512 allocated = (attr->la_gid == 0) ? true : false;
1513 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1514 if (rc == -EDQUOT || rc == -EINPROGRESS)
1519 /* and one less inode for the current gid */
1520 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1522 rc = osd_declare_qid(env, oh, qi, true, NULL);
1523 if (rc == -EDQUOT || rc == -EINPROGRESS)
1528 /* block accounting */
1529 qi->lqi_is_blk = true;
1531 /* more blocks for the new owner ... */
1532 qi->lqi_id.qid_gid = attr->la_gid;
1533 qi->lqi_space = bspace;
1534 allocated = (attr->la_gid == 0) ? true : false;
1535 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1536 if (rc == -EDQUOT || rc == -EINPROGRESS)
1541 /* and finally less blocks for the current owner */
1542 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1543 qi->lqi_space = -bspace;
1544 rc = osd_declare_qid(env, oh, qi, true, NULL);
1545 if (rc == -EDQUOT || rc == -EINPROGRESS)
1554 static int osd_inode_setattr(const struct lu_env *env,
1555 struct inode *inode, const struct lu_attr *attr)
1559 bits = attr->la_valid;
1561 if (bits & LA_ATIME)
1562 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1563 if (bits & LA_CTIME)
1564 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1565 if (bits & LA_MTIME)
1566 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1567 if (bits & LA_SIZE) {
1568 LDISKFS_I(inode)->i_disksize = attr->la_size;
1569 i_size_write(inode, attr->la_size);
1573 /* OSD should not change "i_blocks" which is used by quota.
1574 * "i_blocks" should be changed by ldiskfs only. */
1575 if (bits & LA_BLOCKS)
1576 inode->i_blocks = attr->la_blocks;
1579 inode->i_mode = (inode->i_mode & S_IFMT) |
1580 (attr->la_mode & ~S_IFMT);
1582 inode->i_uid = attr->la_uid;
1584 inode->i_gid = attr->la_gid;
1585 if (bits & LA_NLINK)
1586 set_nlink(inode, attr->la_nlink);
1588 inode->i_rdev = attr->la_rdev;
1590 if (bits & LA_FLAGS) {
1591 /* always keep S_NOCMTIME */
1592 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1598 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1600 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1601 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1606 if (attr->la_valid & LA_UID)
1607 iattr.ia_valid |= ATTR_UID;
1608 if (attr->la_valid & LA_GID)
1609 iattr.ia_valid |= ATTR_GID;
1610 iattr.ia_uid = attr->la_uid;
1611 iattr.ia_gid = attr->la_gid;
1613 rc = ll_vfs_dq_transfer(inode, &iattr);
1615 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1616 "enforcement enabled on the ldiskfs filesystem?",
1617 inode->i_sb->s_id, rc);
1624 static int osd_attr_set(const struct lu_env *env,
1625 struct dt_object *dt,
1626 const struct lu_attr *attr,
1627 struct thandle *handle,
1628 struct lustre_capa *capa)
1630 struct osd_object *obj = osd_dt_obj(dt);
1631 struct inode *inode;
1634 LASSERT(handle != NULL);
1635 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1636 LASSERT(osd_invariant(obj));
1638 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1641 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1643 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1644 struct osd_thread_info *oti = osd_oti_get(env);
1645 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1646 struct lu_fid *fid1 = &oti->oti_fid;
1647 struct osd_inode_id *id = &oti->oti_id;
1648 struct iam_path_descr *ipd;
1649 struct iam_container *bag;
1650 struct osd_thandle *oh;
1653 fid_cpu_to_be(fid1, fid0);
1654 memset(id, 1, sizeof(*id));
1655 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1656 fid0)->oi_dir.od_container;
1657 ipd = osd_idx_ipd_get(env, bag);
1658 if (unlikely(ipd == NULL))
1661 oh = container_of0(handle, struct osd_thandle, ot_super);
1662 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1663 (const struct iam_rec *)id, ipd);
1664 osd_ipd_put(env, bag, ipd);
1665 return(rc > 0 ? 0 : rc);
1668 inode = obj->oo_inode;
1669 ll_vfs_dq_init(inode);
1671 rc = osd_quota_transfer(inode, attr);
1675 spin_lock(&obj->oo_guard);
1676 rc = osd_inode_setattr(env, inode, attr);
1677 spin_unlock(&obj->oo_guard);
1680 inode->i_sb->s_op->dirty_inode(inode);
1684 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1685 struct osd_object *obj,
1686 const char *name, const int namelen)
1688 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1691 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1693 struct dt_allocation_hint *hint,
1697 struct osd_device *osd = osd_obj2dev(obj);
1698 struct osd_thandle *oth;
1699 struct dt_object *parent = NULL;
1700 struct inode *inode;
1702 LINVRNT(osd_invariant(obj));
1703 LASSERT(obj->oo_inode == NULL);
1704 LASSERT(obj->oo_hl_head == NULL);
1706 if (S_ISDIR(mode) && ldiskfs_pdo) {
1707 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1708 if (obj->oo_hl_head == NULL)
1712 oth = container_of(th, struct osd_thandle, ot_super);
1713 LASSERT(oth->ot_handle->h_transaction != NULL);
1715 if (hint && hint->dah_parent)
1716 parent = hint->dah_parent;
1718 inode = ldiskfs_create_inode(oth->ot_handle,
1719 parent ? osd_dt_obj(parent)->oo_inode :
1720 osd_sb(osd)->s_root->d_inode,
1722 if (!IS_ERR(inode)) {
1723 /* Do not update file c/mtime in ldiskfs.
1724 * NB: don't need any lock because no contention at this
1726 inode->i_flags |= S_NOCMTIME;
1728 /* For new created object, it must be consistent,
1729 * and it is unnecessary to scrub against it. */
1730 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1731 obj->oo_inode = inode;
1734 if (obj->oo_hl_head != NULL) {
1735 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1736 obj->oo_hl_head = NULL;
1738 result = PTR_ERR(inode);
1740 LINVRNT(osd_invariant(obj));
1748 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1749 struct lu_attr *attr,
1750 struct dt_allocation_hint *hint,
1751 struct dt_object_format *dof,
1755 struct osd_thandle *oth;
1756 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1758 LASSERT(S_ISDIR(attr->la_mode));
1760 oth = container_of(th, struct osd_thandle, ot_super);
1761 LASSERT(oth->ot_handle->h_transaction != NULL);
1762 result = osd_mkfile(info, obj, mode, hint, th);
1767 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1768 struct lu_attr *attr,
1769 struct dt_allocation_hint *hint,
1770 struct dt_object_format *dof,
1774 struct osd_thandle *oth;
1775 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1777 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1779 LASSERT(S_ISREG(attr->la_mode));
1781 oth = container_of(th, struct osd_thandle, ot_super);
1782 LASSERT(oth->ot_handle->h_transaction != NULL);
1784 result = osd_mkfile(info, obj, mode, hint, th);
1786 LASSERT(obj->oo_inode != NULL);
1787 if (feat->dif_flags & DT_IND_VARKEY)
1788 result = iam_lvar_create(obj->oo_inode,
1789 feat->dif_keysize_max,
1791 feat->dif_recsize_max,
1794 result = iam_lfix_create(obj->oo_inode,
1795 feat->dif_keysize_max,
1797 feat->dif_recsize_max,
1804 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1805 struct lu_attr *attr,
1806 struct dt_allocation_hint *hint,
1807 struct dt_object_format *dof,
1810 LASSERT(S_ISREG(attr->la_mode));
1811 return osd_mkfile(info, obj, (attr->la_mode &
1812 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1815 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1816 struct lu_attr *attr,
1817 struct dt_allocation_hint *hint,
1818 struct dt_object_format *dof,
1821 LASSERT(S_ISLNK(attr->la_mode));
1822 return osd_mkfile(info, obj, (attr->la_mode &
1823 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1826 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1827 struct lu_attr *attr,
1828 struct dt_allocation_hint *hint,
1829 struct dt_object_format *dof,
1832 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1835 LINVRNT(osd_invariant(obj));
1836 LASSERT(obj->oo_inode == NULL);
1837 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1838 S_ISFIFO(mode) || S_ISSOCK(mode));
1840 result = osd_mkfile(info, obj, mode, hint, th);
1842 LASSERT(obj->oo_inode != NULL);
1844 * This inode should be marked dirty for i_rdev. Currently
1845 * that is done in the osd_attr_init().
1847 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1850 LINVRNT(osd_invariant(obj));
1854 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1856 struct dt_allocation_hint *hint,
1857 struct dt_object_format *dof,
1860 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1862 osd_obj_type_f result;
1878 result = osd_mk_index;
1889 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1890 struct dt_object *parent, struct dt_object *child,
1891 cfs_umode_t child_mode)
1895 memset(ah, 0, sizeof(*ah));
1896 ah->dah_parent = parent;
1897 ah->dah_mode = child_mode;
1900 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1901 struct lu_attr *attr, struct dt_object_format *dof)
1903 struct inode *inode = obj->oo_inode;
1904 __u64 valid = attr->la_valid;
1907 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1909 if (dof->dof_type != DFT_NODE)
1910 attr->la_valid &= ~LA_RDEV;
1911 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1912 attr->la_valid &= ~LA_ATIME;
1913 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1914 attr->la_valid &= ~LA_CTIME;
1915 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1916 attr->la_valid &= ~LA_MTIME;
1918 result = osd_quota_transfer(inode, attr);
1922 if (attr->la_valid != 0) {
1923 result = osd_inode_setattr(info->oti_env, inode, attr);
1925 * The osd_inode_setattr() should always succeed here. The
1926 * only error that could be returned is EDQUOT when we are
1927 * trying to change the UID or GID of the inode. However, this
1928 * should not happen since quota enforcement is no longer
1929 * enabled on ldiskfs (lquota takes care of it).
1931 LASSERTF(result == 0, "%d", result);
1932 inode->i_sb->s_op->dirty_inode(inode);
1935 attr->la_valid = valid;
1939 * Helper function for osd_object_create()
1941 * \retval 0, on success
1943 static int __osd_object_create(struct osd_thread_info *info,
1944 struct osd_object *obj, struct lu_attr *attr,
1945 struct dt_allocation_hint *hint,
1946 struct dt_object_format *dof,
1952 /* we drop umask so that permissions we pass are not affected */
1953 umask = current->fs->umask;
1954 current->fs->umask = 0;
1956 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1959 osd_attr_init(info, obj, attr, dof);
1960 osd_object_init0(obj);
1962 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1963 unlock_new_inode(obj->oo_inode);
1966 /* restore previous umask value */
1967 current->fs->umask = umask;
1973 * Helper function for osd_object_create()
1975 * \retval 0, on success
1977 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1978 const struct lu_fid *fid, struct thandle *th)
1980 struct osd_thread_info *info = osd_oti_get(env);
1981 struct osd_inode_id *id = &info->oti_id;
1982 struct osd_device *osd = osd_obj2dev(obj);
1984 LASSERT(obj->oo_inode != NULL);
1986 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1987 return osd_oi_insert(info, osd, fid, id, th);
1990 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
1991 const struct lu_fid *fid, struct lu_seq_range *range)
1993 struct seq_server_site *ss = osd_seq_site(osd);
1996 if (fid_is_idif(fid)) {
1997 range->lsr_flags = LU_SEQ_RANGE_OST;
1998 range->lsr_index = fid_idif_ost_idx(fid);
2002 if (!fid_seq_in_fldb(fid_seq(fid))) {
2003 range->lsr_flags = LU_SEQ_RANGE_MDT;
2005 /* FIXME: If ss is NULL, it suppose not get lsr_index
2007 range->lsr_index = ss->ss_node_id;
2011 LASSERT(ss != NULL);
2012 range->lsr_flags = -1;
2013 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
2015 CERROR("%s can not find "DFID": rc = %d\n",
2016 osd_name(osd), PFID(fid), rc);
2022 * Concurrency: no external locking is necessary.
2024 static int osd_declare_object_create(const struct lu_env *env,
2025 struct dt_object *dt,
2026 struct lu_attr *attr,
2027 struct dt_allocation_hint *hint,
2028 struct dt_object_format *dof,
2029 struct thandle *handle)
2031 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2032 struct osd_thandle *oh;
2036 LASSERT(handle != NULL);
2038 oh = container_of0(handle, struct osd_thandle, ot_super);
2039 LASSERT(oh->ot_handle == NULL);
2041 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2042 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2043 if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2044 lu_object_fid(&dt->do_lu)))
2045 /* Reuse idle OI block may cause additional one OI block
2047 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2048 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2050 /* If this is directory, then we expect . and .. to be inserted as
2051 * well. The one directory block always needs to be created for the
2052 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2053 * block), there is no danger of needing a tree for the first block.
2055 if (attr && S_ISDIR(attr->la_mode)) {
2056 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2057 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2058 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2064 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2065 false, false, NULL, false);
2069 /* It does fld look up inside declare, and the result will be
2070 * added to fld cache, so the following fld lookup inside insert
2071 * does not need send RPC anymore, so avoid send rpc with holding
2073 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2074 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2075 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2076 lu_object_fid(&dt->do_lu), range);
2082 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2083 struct lu_attr *attr,
2084 struct dt_allocation_hint *hint,
2085 struct dt_object_format *dof,
2088 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2089 struct osd_object *obj = osd_dt_obj(dt);
2090 struct osd_thread_info *info = osd_oti_get(env);
2095 LINVRNT(osd_invariant(obj));
2096 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2097 LASSERT(osd_write_locked(env, obj));
2098 LASSERT(th != NULL);
2100 if (unlikely(fid_is_acct(fid)))
2101 /* Quota files can't be created from the kernel any more,
2102 * 'tune2fs -O quota' will take care of creating them */
2105 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2106 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2108 result = __osd_object_create(info, obj, attr, hint, dof, th);
2110 result = __osd_oi_insert(env, obj, fid, th);
2112 LASSERT(ergo(result == 0,
2113 dt_object_exists(dt) && !dt_object_remote(dt)));
2115 LASSERT(osd_invariant(obj));
2120 * Called to destroy on-disk representation of the object
2122 * Concurrency: must be locked
2124 static int osd_declare_object_destroy(const struct lu_env *env,
2125 struct dt_object *dt,
2128 struct osd_object *obj = osd_dt_obj(dt);
2129 struct inode *inode = obj->oo_inode;
2130 struct osd_thandle *oh;
2134 oh = container_of0(th, struct osd_thandle, ot_super);
2135 LASSERT(oh->ot_handle == NULL);
2138 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2139 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2140 /* Recycle idle OI leaf may cause additional three OI blocks
2142 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2143 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2145 /* one less inode */
2146 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2147 false, true, NULL, false);
2150 /* data to be truncated */
2151 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2152 true, true, NULL, false);
2156 static int osd_object_destroy(const struct lu_env *env,
2157 struct dt_object *dt,
2160 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2161 struct osd_object *obj = osd_dt_obj(dt);
2162 struct inode *inode = obj->oo_inode;
2163 struct osd_device *osd = osd_obj2dev(obj);
2164 struct osd_thandle *oh;
2168 oh = container_of0(th, struct osd_thandle, ot_super);
2169 LASSERT(oh->ot_handle);
2171 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2173 if (unlikely(fid_is_acct(fid)))
2176 /* Parallel control for OI scrub. For most of cases, there is no
2177 * lock contention. So it will not affect unlink performance. */
2178 mutex_lock(&inode->i_mutex);
2179 if (S_ISDIR(inode->i_mode)) {
2180 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2181 /* it will check/delete the agent inode for every dir
2182 * destory, how to optimize it? unlink performance
2184 result = osd_delete_from_agent(env, osd, obj, oh);
2185 if (result != 0 && result != -ENOENT) {
2186 CERROR("%s: delete agent inode "DFID": rc = %d\n",
2187 osd_name(osd), PFID(fid), result);
2189 spin_lock(&obj->oo_guard);
2191 spin_unlock(&obj->oo_guard);
2192 inode->i_sb->s_op->dirty_inode(inode);
2195 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2197 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2198 mutex_unlock(&inode->i_mutex);
2200 /* XXX: add to ext3 orphan list */
2201 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2203 /* not needed in the cache anymore */
2204 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2209 static inline int __osd_xattr_set(struct osd_thread_info *info,
2210 struct inode *inode, const char *name,
2211 const void *buf, int buflen, int fl)
2213 struct dentry *dentry = &info->oti_child_dentry;
2215 ll_vfs_dq_init(inode);
2216 dentry->d_inode = inode;
2217 return inode->i_op->setxattr(dentry, name, buf, buflen, fl);
2221 * Put the fid into lustre_mdt_attrs, and then place the structure
2222 * inode's ea. This fid should not be altered during the life time
2225 * \retval +ve, on success
2226 * \retval -ve, on error
2228 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2230 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2231 const struct lu_fid *fid)
2233 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2236 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2239 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF) && fid_is_client_visible(fid))
2242 lustre_lma_init(lma, fid);
2243 lustre_lma_swab(lma);
2245 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2247 /* Someone may created the EA by race. */
2248 if (unlikely(rc == -EEXIST))
2254 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2255 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2256 * To have compatilibility with 1.8 ldiskfs driver we need to have
2257 * magic number at start of fid data.
2258 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2261 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2262 const struct dt_rec *fid)
2264 if (!fid_is_client_mdt_visible((const struct lu_fid *)fid)) {
2265 param->edp_magic = 0;
2269 param->edp_magic = LDISKFS_LUFID_MAGIC;
2270 param->edp_len = sizeof(struct lu_fid) + 1;
2271 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2275 * Try to read the fid from inode ea into dt_rec.
2277 * \param fid object fid.
2279 * \retval 0 on success
2281 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2282 __u32 ino, struct lu_fid *fid,
2283 struct osd_inode_id *id)
2285 struct osd_thread_info *info = osd_oti_get(env);
2286 struct inode *inode;
2289 osd_id_gen(id, ino, OSD_OII_NOGEN);
2290 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2292 RETURN(PTR_ERR(inode));
2298 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2300 struct inode *parent_dir,
2301 const struct dt_rec *dot_fid,
2302 const struct dt_rec *dot_dot_fid,
2303 struct osd_thandle *oth)
2305 struct ldiskfs_dentry_param *dot_ldp;
2306 struct ldiskfs_dentry_param *dot_dot_ldp;
2308 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2309 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2311 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2312 dot_ldp->edp_magic = 0;
2313 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2314 dir, dot_ldp, dot_dot_ldp);
2318 * Create an local inode for remote entry
2320 static struct inode *osd_create_remote_inode(const struct lu_env *env,
2321 struct osd_device *osd,
2322 struct osd_object *pobj,
2323 const struct lu_fid *fid,
2326 struct osd_thread_info *info = osd_oti_get(env);
2327 struct inode *local;
2328 struct osd_thandle *oh;
2333 oh = container_of(th, struct osd_thandle, ot_super);
2334 LASSERT(oh->ot_handle->h_transaction != NULL);
2336 /* FIXME: Insert index api needs to know the mode of
2337 * the remote object. Just use S_IFDIR for now */
2338 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2339 if (IS_ERR(local)) {
2340 CERROR("%s: create local error %d\n", osd_name(osd),
2341 (int)PTR_ERR(local));
2345 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2346 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2347 (const struct dt_rec *)fid, oh);
2349 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2350 osd_name(osd), PFID(fid), rc);
2351 RETURN(ERR_PTR(rc));
2358 * Delete local inode for remote entry
2360 static int osd_delete_remote_inode(const struct lu_env *env,
2361 struct osd_device *osd,
2362 const struct lu_fid *fid,
2363 __u32 ino, struct osd_thandle *oh)
2365 struct osd_thread_info *oti = osd_oti_get(env);
2366 struct osd_inode_id *id = &oti->oti_id;
2367 struct inode *inode;
2370 id->oii_ino = le32_to_cpu(ino);
2371 id->oii_gen = OSD_OII_NOGEN;
2372 inode = osd_iget(oti, osd, id);
2373 if (IS_ERR(inode)) {
2374 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2375 PFID(fid), id->oii_ino, id->oii_gen);
2376 RETURN(PTR_ERR(inode));
2380 mark_inode_dirty(inode);
2381 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2382 osd_name(osd), PFID(fid), inode->i_ino);
2388 * OSD layer object create function for interoperability mode (b11826).
2389 * This is mostly similar to osd_object_create(). Only difference being, fid is
2390 * inserted into inode ea here.
2392 * \retval 0, on success
2393 * \retval -ve, on error
2395 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2396 struct lu_attr *attr,
2397 struct dt_allocation_hint *hint,
2398 struct dt_object_format *dof,
2401 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2402 struct osd_object *obj = osd_dt_obj(dt);
2403 struct osd_thread_info *info = osd_oti_get(env);
2408 LASSERT(osd_invariant(obj));
2409 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2410 LASSERT(osd_write_locked(env, obj));
2411 LASSERT(th != NULL);
2413 if (unlikely(fid_is_acct(fid)))
2414 /* Quota files can't be created from the kernel any more,
2415 * 'tune2fs -O quota' will take care of creating them */
2418 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2419 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2421 result = __osd_object_create(info, obj, attr, hint, dof, th);
2422 if ((result == 0) &&
2423 (fid_is_last_id(fid) ||
2424 !fid_is_on_ost(info, osd_dt_dev(th->th_dev), fid)))
2425 result = osd_ea_fid_set(info, obj->oo_inode, fid);
2428 result = __osd_oi_insert(env, obj, fid, th);
2430 LASSERT(ergo(result == 0,
2431 dt_object_exists(dt) && !dt_object_remote(dt)));
2432 LINVRNT(osd_invariant(obj));
2436 static int osd_declare_object_ref_add(const struct lu_env *env,
2437 struct dt_object *dt,
2438 struct thandle *handle)
2440 struct osd_thandle *oh;
2442 /* it's possible that object doesn't exist yet */
2443 LASSERT(handle != NULL);
2445 oh = container_of0(handle, struct osd_thandle, ot_super);
2446 LASSERT(oh->ot_handle == NULL);
2448 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2449 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2455 * Concurrency: @dt is write locked.
2457 static int osd_object_ref_add(const struct lu_env *env,
2458 struct dt_object *dt, struct thandle *th)
2460 struct osd_object *obj = osd_dt_obj(dt);
2461 struct inode *inode = obj->oo_inode;
2463 LINVRNT(osd_invariant(obj));
2464 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2465 LASSERT(osd_write_locked(env, obj));
2466 LASSERT(th != NULL);
2468 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2471 * DIR_NLINK feature is set for compatibility reasons if:
2472 * 1) nlinks > LDISKFS_LINK_MAX, or
2473 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2475 * It is easier to always set this flag (rather than check and set),
2476 * since it has less overhead, and the superblock will be dirtied
2477 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2478 * do not actually care whether this flag is set or not.
2480 spin_lock(&obj->oo_guard);
2481 /* inc_nlink from 0 may cause WARN_ON */
2482 if(inode->i_nlink == 0)
2483 set_nlink(inode, 1);
2486 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2487 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2488 inode->i_nlink == 2)
2489 set_nlink(inode, 1);
2491 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2492 spin_unlock(&obj->oo_guard);
2493 inode->i_sb->s_op->dirty_inode(inode);
2494 LINVRNT(osd_invariant(obj));
2499 static int osd_declare_object_ref_del(const struct lu_env *env,
2500 struct dt_object *dt,
2501 struct thandle *handle)
2503 struct osd_thandle *oh;
2505 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2506 LASSERT(handle != NULL);
2508 oh = container_of0(handle, struct osd_thandle, ot_super);
2509 LASSERT(oh->ot_handle == NULL);
2511 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2512 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2518 * Concurrency: @dt is write locked.
2520 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2523 struct osd_object *obj = osd_dt_obj(dt);
2524 struct inode *inode = obj->oo_inode;
2526 LINVRNT(osd_invariant(obj));
2527 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2528 LASSERT(osd_write_locked(env, obj));
2529 LASSERT(th != NULL);
2531 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2533 spin_lock(&obj->oo_guard);
2534 LASSERT(inode->i_nlink > 0);
2536 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2537 * then the nlink count is 1. Don't let it be set to 0 or the directory
2538 * inode will be deleted incorrectly. */
2539 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2540 set_nlink(inode, 1);
2541 spin_unlock(&obj->oo_guard);
2542 inode->i_sb->s_op->dirty_inode(inode);
2543 LINVRNT(osd_invariant(obj));
2549 * Get the 64-bit version for an inode.
2551 static int osd_object_version_get(const struct lu_env *env,
2552 struct dt_object *dt, dt_obj_version_t *ver)
2554 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2556 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2557 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2558 *ver = LDISKFS_I(inode)->i_fs_version;
2563 * Concurrency: @dt is read locked.
2565 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2566 struct lu_buf *buf, const char *name,
2567 struct lustre_capa *capa)
2569 struct osd_object *obj = osd_dt_obj(dt);
2570 struct inode *inode = obj->oo_inode;
2571 struct osd_thread_info *info = osd_oti_get(env);
2572 struct dentry *dentry = &info->oti_obj_dentry;
2574 /* version get is not real XATTR but uses xattr API */
2575 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2576 /* for version we are just using xattr API but change inode
2578 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2579 osd_object_version_get(env, dt, buf->lb_buf);
2580 return sizeof(dt_obj_version_t);
2583 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2584 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2586 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2589 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2593 static int osd_declare_xattr_set(const struct lu_env *env,
2594 struct dt_object *dt,
2595 const struct lu_buf *buf, const char *name,
2596 int fl, struct thandle *handle)
2598 struct osd_thandle *oh;
2600 LASSERT(handle != NULL);
2602 oh = container_of0(handle, struct osd_thandle, ot_super);
2603 LASSERT(oh->ot_handle == NULL);
2605 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2606 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2607 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2608 osd_dto_credits_noquota[DTO_XATTR_SET]);
2614 * Set the 64-bit version for object
2616 static void osd_object_version_set(const struct lu_env *env,
2617 struct dt_object *dt,
2618 dt_obj_version_t *new_version)
2620 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2622 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2623 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2625 LDISKFS_I(inode)->i_fs_version = *new_version;
2626 /** Version is set after all inode operations are finished,
2627 * so we should mark it dirty here */
2628 inode->i_sb->s_op->dirty_inode(inode);
2632 * Concurrency: @dt is write locked.
2634 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2635 const struct lu_buf *buf, const char *name, int fl,
2636 struct thandle *handle, struct lustre_capa *capa)
2638 struct osd_object *obj = osd_dt_obj(dt);
2639 struct inode *inode = obj->oo_inode;
2640 struct osd_thread_info *info = osd_oti_get(env);
2644 LASSERT(handle != NULL);
2646 /* version set is not real XATTR */
2647 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2648 /* for version we are just using xattr API but change inode
2650 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2651 osd_object_version_set(env, dt, buf->lb_buf);
2652 return sizeof(dt_obj_version_t);
2655 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2658 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2659 if (fl & LU_XATTR_REPLACE)
2660 fs_flags |= XATTR_REPLACE;
2662 if (fl & LU_XATTR_CREATE)
2663 fs_flags |= XATTR_CREATE;
2665 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2670 * Concurrency: @dt is read locked.
2672 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2673 struct lu_buf *buf, struct lustre_capa *capa)
2675 struct osd_object *obj = osd_dt_obj(dt);
2676 struct inode *inode = obj->oo_inode;
2677 struct osd_thread_info *info = osd_oti_get(env);
2678 struct dentry *dentry = &info->oti_obj_dentry;
2680 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2681 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2682 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2684 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2687 dentry->d_inode = inode;
2688 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2691 static int osd_declare_xattr_del(const struct lu_env *env,
2692 struct dt_object *dt, const char *name,
2693 struct thandle *handle)
2695 struct osd_thandle *oh;
2697 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2698 LASSERT(handle != NULL);
2700 oh = container_of0(handle, struct osd_thandle, ot_super);
2701 LASSERT(oh->ot_handle == NULL);
2703 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2704 osd_dto_credits_noquota[DTO_XATTR_SET]);
2710 * Concurrency: @dt is write locked.
2712 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2713 const char *name, struct thandle *handle,
2714 struct lustre_capa *capa)
2716 struct osd_object *obj = osd_dt_obj(dt);
2717 struct inode *inode = obj->oo_inode;
2718 struct osd_thread_info *info = osd_oti_get(env);
2719 struct dentry *dentry = &info->oti_obj_dentry;
2722 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2723 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2724 LASSERT(handle != NULL);
2726 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2729 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2731 ll_vfs_dq_init(inode);
2732 dentry->d_inode = inode;
2733 rc = inode->i_op->removexattr(dentry, name);
2737 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2738 struct dt_object *dt,
2739 struct lustre_capa *old,
2742 struct osd_thread_info *info = osd_oti_get(env);
2743 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2744 struct osd_object *obj = osd_dt_obj(dt);
2745 struct osd_device *dev = osd_obj2dev(obj);
2746 struct lustre_capa_key *key = &info->oti_capa_key;
2747 struct lustre_capa *capa = &info->oti_capa;
2748 struct obd_capa *oc;
2749 struct md_capainfo *ci;
2753 if (!dev->od_fl_capa)
2754 RETURN(ERR_PTR(-ENOENT));
2756 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2757 LINVRNT(osd_invariant(obj));
2759 /* renewal sanity check */
2760 if (old && osd_object_auth(env, dt, old, opc))
2761 RETURN(ERR_PTR(-EACCES));
2763 ci = md_capainfo(env);
2765 RETURN(ERR_PTR(-ENOENT));
2767 switch (ci->mc_auth) {
2771 capa->lc_uid = obj->oo_inode->i_uid;
2772 capa->lc_gid = obj->oo_inode->i_gid;
2773 capa->lc_flags = LC_ID_PLAIN;
2775 case LC_ID_CONVERT: {
2778 s[0] = obj->oo_inode->i_uid;
2779 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2780 s[2] = obj->oo_inode->i_gid;
2781 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2782 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2784 RETURN(ERR_PTR(rc));
2786 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2787 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2788 capa->lc_flags = LC_ID_CONVERT;
2792 RETURN(ERR_PTR(-EINVAL));
2795 capa->lc_fid = *fid;
2797 capa->lc_flags |= dev->od_capa_alg << 24;
2798 capa->lc_timeout = dev->od_capa_timeout;
2799 capa->lc_expiry = 0;
2801 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2803 LASSERT(!capa_is_expired(oc));
2807 spin_lock(&capa_lock);
2808 *key = dev->od_capa_keys[1];
2809 spin_unlock(&capa_lock);
2811 capa->lc_keyid = key->lk_keyid;
2812 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2814 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2816 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2817 RETURN(ERR_PTR(rc));
2820 oc = capa_add(dev->od_capa_hash, capa);
2824 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2826 struct osd_object *obj = osd_dt_obj(dt);
2827 struct inode *inode = obj->oo_inode;
2828 struct osd_thread_info *info = osd_oti_get(env);
2829 struct dentry *dentry = &info->oti_obj_dentry;
2830 struct file *file = &info->oti_file;
2835 dentry->d_inode = inode;
2836 file->f_dentry = dentry;
2837 file->f_mapping = inode->i_mapping;
2838 file->f_op = inode->i_fop;
2839 #ifndef HAVE_FILE_FSYNC_4ARGS
2840 mutex_lock(&inode->i_mutex);
2842 rc = do_fsync(file, 0);
2843 #ifndef HAVE_FILE_FSYNC_4ARGS
2844 mutex_unlock(&inode->i_mutex);
2849 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2852 struct osd_object *obj = osd_dt_obj(dt);
2855 *data = (void *)obj->oo_inode;
2863 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2864 const struct dt_index_features *feat)
2866 struct iam_descr *descr;
2868 if (osd_object_is_root(o))
2869 return feat == &dt_directory_features;
2871 LASSERT(o->oo_dir != NULL);
2873 descr = o->oo_dir->od_container.ic_descr;
2874 if (feat == &dt_directory_features) {
2875 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2881 feat->dif_keysize_min <= descr->id_key_size &&
2882 descr->id_key_size <= feat->dif_keysize_max &&
2883 feat->dif_recsize_min <= descr->id_rec_size &&
2884 descr->id_rec_size <= feat->dif_recsize_max &&
2885 !(feat->dif_flags & (DT_IND_VARKEY |
2886 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2887 ergo(feat->dif_flags & DT_IND_UPDATE,
2888 1 /* XXX check that object (and file system) is
2893 static int osd_iam_container_init(const struct lu_env *env,
2894 struct osd_object *obj,
2895 struct osd_directory *dir)
2897 struct iam_container *bag = &dir->od_container;
2900 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2904 result = iam_container_setup(bag);
2906 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2908 iam_container_fini(bag);
2915 * Concurrency: no external locking is necessary.
2917 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2918 const struct dt_index_features *feat)
2922 struct osd_object *obj = osd_dt_obj(dt);
2924 LINVRNT(osd_invariant(obj));
2926 if (osd_object_is_root(obj)) {
2927 dt->do_index_ops = &osd_index_ea_ops;
2929 } else if (feat == &dt_directory_features) {
2930 dt->do_index_ops = &osd_index_ea_ops;
2931 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
2936 } else if (unlikely(feat == &dt_otable_features)) {
2937 dt->do_index_ops = &osd_otable_ops;
2939 } else if (unlikely(feat == &dt_acct_features)) {
2940 dt->do_index_ops = &osd_acct_index_ops;
2943 } else if (!osd_has_index(obj)) {
2944 struct osd_directory *dir;
2949 spin_lock(&obj->oo_guard);
2950 if (obj->oo_dir == NULL)
2954 * Concurrent thread allocated container data.
2957 spin_unlock(&obj->oo_guard);
2959 * Now, that we have container data, serialize its
2962 down_write(&obj->oo_ext_idx_sem);
2964 * recheck under lock.
2966 if (!osd_has_index(obj))
2967 result = osd_iam_container_init(env, obj, dir);
2970 up_write(&obj->oo_ext_idx_sem);
2978 if (result == 0 && skip_iam == 0) {
2979 if (!osd_iam_index_probe(env, obj, feat))
2982 LINVRNT(osd_invariant(obj));
2984 if (result == 0 && is_quota_glb_feat(feat) &&
2985 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
2986 result = osd_quota_migration(env, dt, feat);
2991 static int osd_otable_it_attr_get(const struct lu_env *env,
2992 struct dt_object *dt,
2993 struct lu_attr *attr,
2994 struct lustre_capa *capa)
3000 static const struct dt_object_operations osd_obj_ops = {
3001 .do_read_lock = osd_object_read_lock,
3002 .do_write_lock = osd_object_write_lock,
3003 .do_read_unlock = osd_object_read_unlock,
3004 .do_write_unlock = osd_object_write_unlock,
3005 .do_write_locked = osd_object_write_locked,
3006 .do_attr_get = osd_attr_get,
3007 .do_declare_attr_set = osd_declare_attr_set,
3008 .do_attr_set = osd_attr_set,
3009 .do_ah_init = osd_ah_init,
3010 .do_declare_create = osd_declare_object_create,
3011 .do_create = osd_object_create,
3012 .do_declare_destroy = osd_declare_object_destroy,
3013 .do_destroy = osd_object_destroy,
3014 .do_index_try = osd_index_try,
3015 .do_declare_ref_add = osd_declare_object_ref_add,
3016 .do_ref_add = osd_object_ref_add,
3017 .do_declare_ref_del = osd_declare_object_ref_del,
3018 .do_ref_del = osd_object_ref_del,
3019 .do_xattr_get = osd_xattr_get,
3020 .do_declare_xattr_set = osd_declare_xattr_set,
3021 .do_xattr_set = osd_xattr_set,
3022 .do_declare_xattr_del = osd_declare_xattr_del,
3023 .do_xattr_del = osd_xattr_del,
3024 .do_xattr_list = osd_xattr_list,
3025 .do_capa_get = osd_capa_get,
3026 .do_object_sync = osd_object_sync,
3027 .do_data_get = osd_data_get,
3031 * dt_object_operations for interoperability mode
3032 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3034 static const struct dt_object_operations osd_obj_ea_ops = {
3035 .do_read_lock = osd_object_read_lock,
3036 .do_write_lock = osd_object_write_lock,
3037 .do_read_unlock = osd_object_read_unlock,
3038 .do_write_unlock = osd_object_write_unlock,
3039 .do_write_locked = osd_object_write_locked,
3040 .do_attr_get = osd_attr_get,
3041 .do_declare_attr_set = osd_declare_attr_set,
3042 .do_attr_set = osd_attr_set,
3043 .do_ah_init = osd_ah_init,
3044 .do_declare_create = osd_declare_object_create,
3045 .do_create = osd_object_ea_create,
3046 .do_declare_destroy = osd_declare_object_destroy,
3047 .do_destroy = osd_object_destroy,
3048 .do_index_try = osd_index_try,
3049 .do_declare_ref_add = osd_declare_object_ref_add,
3050 .do_ref_add = osd_object_ref_add,
3051 .do_declare_ref_del = osd_declare_object_ref_del,
3052 .do_ref_del = osd_object_ref_del,
3053 .do_xattr_get = osd_xattr_get,
3054 .do_declare_xattr_set = osd_declare_xattr_set,
3055 .do_xattr_set = osd_xattr_set,
3056 .do_declare_xattr_del = osd_declare_xattr_del,
3057 .do_xattr_del = osd_xattr_del,
3058 .do_xattr_list = osd_xattr_list,
3059 .do_capa_get = osd_capa_get,
3060 .do_object_sync = osd_object_sync,
3061 .do_data_get = osd_data_get,
3064 static const struct dt_object_operations osd_obj_otable_it_ops = {
3065 .do_attr_get = osd_otable_it_attr_get,
3066 .do_index_try = osd_index_try,
3069 static int osd_index_declare_iam_delete(const struct lu_env *env,
3070 struct dt_object *dt,
3071 const struct dt_key *key,
3072 struct thandle *handle)
3074 struct osd_thandle *oh;
3076 oh = container_of0(handle, struct osd_thandle, ot_super);
3077 LASSERT(oh->ot_handle == NULL);
3079 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3080 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3086 * delete a (key, value) pair from index \a dt specified by \a key
3088 * \param dt osd index object
3089 * \param key key for index
3090 * \param rec record reference
3091 * \param handle transaction handler
3094 * \retval -ve failure
3097 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3098 const struct dt_key *key,
3099 struct thandle *handle,
3100 struct lustre_capa *capa)
3102 struct osd_thread_info *oti = osd_oti_get(env);
3103 struct osd_object *obj = osd_dt_obj(dt);
3104 struct osd_thandle *oh;
3105 struct iam_path_descr *ipd;
3106 struct iam_container *bag = &obj->oo_dir->od_container;
3111 LINVRNT(osd_invariant(obj));
3112 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3113 LASSERT(bag->ic_object == obj->oo_inode);
3114 LASSERT(handle != NULL);
3116 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3119 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3121 ipd = osd_idx_ipd_get(env, bag);
3122 if (unlikely(ipd == NULL))
3125 oh = container_of0(handle, struct osd_thandle, ot_super);
3126 LASSERT(oh->ot_handle != NULL);
3127 LASSERT(oh->ot_handle->h_transaction != NULL);
3129 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3130 /* swab quota uid/gid provided by caller */
3131 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3132 key = (const struct dt_key *)&oti->oti_quota_id;
3135 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3136 osd_ipd_put(env, bag, ipd);
3137 LINVRNT(osd_invariant(obj));
3141 static int osd_index_declare_ea_delete(const struct lu_env *env,
3142 struct dt_object *dt,
3143 const struct dt_key *key,
3144 struct thandle *handle)
3146 struct osd_thandle *oh;
3147 struct inode *inode;
3151 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3152 LASSERT(handle != NULL);
3154 oh = container_of0(handle, struct osd_thandle, ot_super);
3155 LASSERT(oh->ot_handle == NULL);
3157 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3158 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3160 inode = osd_dt_obj(dt)->oo_inode;
3163 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3164 true, true, NULL, false);
3168 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3171 struct osd_fid_pack *rec;
3174 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3175 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3176 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3181 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3184 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
3185 struct seq_server_site *ss = osd_seq_site(osd);
3189 /* Those FID seqs, which are not in FLDB, must be local seq */
3190 if (unlikely(!fid_seq_in_fldb(fid_seq(fid)) || ss == NULL))
3193 rc = osd_fld_lookup(env, osd, fid, range);
3195 CERROR("%s: Can not lookup fld for "DFID"\n",
3196 osd_name(osd), PFID(fid));
3200 RETURN(ss->ss_node_id != range->lsr_index);
3204 * Index delete function for interoperability mode (b11826).
3205 * It will remove the directory entry added by osd_index_ea_insert().
3206 * This entry is needed to maintain name->fid mapping.
3208 * \param key, key i.e. file entry to be deleted
3210 * \retval 0, on success
3211 * \retval -ve, on error
3213 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3214 const struct dt_key *key,
3215 struct thandle *handle,
3216 struct lustre_capa *capa)
3218 struct osd_object *obj = osd_dt_obj(dt);
3219 struct inode *dir = obj->oo_inode;
3220 struct dentry *dentry;
3221 struct osd_thandle *oh;
3222 struct ldiskfs_dir_entry_2 *de = NULL;
3223 struct buffer_head *bh;
3224 struct htree_lock *hlock = NULL;
3225 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3226 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3230 LINVRNT(osd_invariant(obj));
3231 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3232 LASSERT(handle != NULL);
3234 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3236 oh = container_of(handle, struct osd_thandle, ot_super);
3237 LASSERT(oh->ot_handle != NULL);
3238 LASSERT(oh->ot_handle->h_transaction != NULL);
3240 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3243 ll_vfs_dq_init(dir);
3244 dentry = osd_child_dentry_get(env, obj,
3245 (char *)key, strlen((char *)key));
3247 if (obj->oo_hl_head != NULL) {
3248 hlock = osd_oti_get(env)->oti_hlock;
3249 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3250 dir, LDISKFS_HLOCK_DEL);
3252 down_write(&obj->oo_ext_idx_sem);
3255 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3257 rc = ldiskfs_delete_entry(oh->ot_handle,
3264 ldiskfs_htree_unlock(hlock);
3266 up_write(&obj->oo_ext_idx_sem);
3271 /* For inode on the remote MDT, .. will point to
3272 * /Agent directory. So do not try to lookup/delete
3273 * remote inode for .. */
3274 if (strcmp((char *)key, dotdot) == 0)
3277 LASSERT(de != NULL);
3278 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3279 if (rc == 0 && osd_remote_fid(env, osd, fid)) {
3280 __u32 ino = le32_to_cpu(de->inode);
3282 rc = osd_delete_remote_inode(env, osd, fid, ino, oh);
3284 CERROR("%s: del local inode "DFID": rc = %d\n",
3285 osd_name(osd), PFID(fid), rc);
3292 LASSERT(osd_invariant(obj));
3297 * Lookup index for \a key and copy record to \a rec.
3299 * \param dt osd index object
3300 * \param key key for index
3301 * \param rec record reference
3303 * \retval +ve success : exact mach
3304 * \retval 0 return record with key not greater than \a key
3305 * \retval -ve failure
3307 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3308 struct dt_rec *rec, const struct dt_key *key,
3309 struct lustre_capa *capa)
3311 struct osd_object *obj = osd_dt_obj(dt);
3312 struct iam_path_descr *ipd;
3313 struct iam_container *bag = &obj->oo_dir->od_container;
3314 struct osd_thread_info *oti = osd_oti_get(env);
3315 struct iam_iterator *it = &oti->oti_idx_it;
3316 struct iam_rec *iam_rec;
3321 LASSERT(osd_invariant(obj));
3322 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3323 LASSERT(bag->ic_object == obj->oo_inode);
3325 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3328 ipd = osd_idx_ipd_get(env, bag);
3332 /* got ipd now we can start iterator. */
3333 iam_it_init(it, bag, 0, ipd);
3335 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3336 /* swab quota uid/gid provided by caller */
3337 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3338 key = (const struct dt_key *)&oti->oti_quota_id;
3341 rc = iam_it_get(it, (struct iam_key *)key);
3343 if (S_ISDIR(obj->oo_inode->i_mode))
3344 iam_rec = (struct iam_rec *)oti->oti_ldp;
3346 iam_rec = (struct iam_rec *) rec;
3348 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3350 if (S_ISDIR(obj->oo_inode->i_mode))
3351 osd_fid_unpack((struct lu_fid *) rec,
3352 (struct osd_fid_pack *)iam_rec);
3353 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3354 osd_quota_unpack(obj, rec);
3359 osd_ipd_put(env, bag, ipd);
3361 LINVRNT(osd_invariant(obj));
3366 static int osd_index_declare_iam_insert(const struct lu_env *env,
3367 struct dt_object *dt,
3368 const struct dt_rec *rec,
3369 const struct dt_key *key,
3370 struct thandle *handle)
3372 struct osd_thandle *oh;
3374 LASSERT(handle != NULL);
3376 oh = container_of0(handle, struct osd_thandle, ot_super);
3377 LASSERT(oh->ot_handle == NULL);
3379 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3380 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3386 * Inserts (key, value) pair in \a dt index object.
3388 * \param dt osd index object
3389 * \param key key for index
3390 * \param rec record reference
3391 * \param th transaction handler
3394 * \retval -ve failure
3396 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3397 const struct dt_rec *rec,
3398 const struct dt_key *key, struct thandle *th,
3399 struct lustre_capa *capa, int ignore_quota)
3401 struct osd_object *obj = osd_dt_obj(dt);
3402 struct iam_path_descr *ipd;
3403 struct osd_thandle *oh;
3404 struct iam_container *bag = &obj->oo_dir->od_container;
3405 struct osd_thread_info *oti = osd_oti_get(env);
3406 struct iam_rec *iam_rec;
3411 LINVRNT(osd_invariant(obj));
3412 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3413 LASSERT(bag->ic_object == obj->oo_inode);
3414 LASSERT(th != NULL);
3416 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3419 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3421 ipd = osd_idx_ipd_get(env, bag);
3422 if (unlikely(ipd == NULL))
3425 oh = container_of0(th, struct osd_thandle, ot_super);
3426 LASSERT(oh->ot_handle != NULL);
3427 LASSERT(oh->ot_handle->h_transaction != NULL);
3428 if (S_ISDIR(obj->oo_inode->i_mode)) {
3429 iam_rec = (struct iam_rec *)oti->oti_ldp;
3430 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3431 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3432 /* pack quota uid/gid */
3433 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3434 key = (const struct dt_key *)&oti->oti_quota_id;
3435 /* pack quota record */
3436 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3437 iam_rec = (struct iam_rec *)rec;
3439 iam_rec = (struct iam_rec *)rec;
3442 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3444 osd_ipd_put(env, bag, ipd);
3445 LINVRNT(osd_invariant(obj));
3450 * Calls ldiskfs_add_entry() to add directory entry
3451 * into the directory. This is required for
3452 * interoperability mode (b11826)
3454 * \retval 0, on success
3455 * \retval -ve, on error
3457 static int __osd_ea_add_rec(struct osd_thread_info *info,
3458 struct osd_object *pobj, struct inode *cinode,
3459 const char *name, const struct dt_rec *fid,
3460 struct htree_lock *hlock, struct thandle *th)
3462 struct ldiskfs_dentry_param *ldp;
3463 struct dentry *child;
3464 struct osd_thandle *oth;
3467 oth = container_of(th, struct osd_thandle, ot_super);
3468 LASSERT(oth->ot_handle != NULL);
3469 LASSERT(oth->ot_handle->h_transaction != NULL);
3470 LASSERT(pobj->oo_inode);
3472 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3473 if (unlikely(pobj->oo_inode ==
3474 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3477 osd_get_ldiskfs_dirent_param(ldp, fid);
3478 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3479 child->d_fsdata = (void *)ldp;
3480 ll_vfs_dq_init(pobj->oo_inode);
3481 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3487 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3488 * into the directory.Also sets flags into osd object to
3489 * indicate dot and dotdot are created. This is required for
3490 * interoperability mode (b11826)
3492 * \param dir directory for dot and dotdot fixup.
3493 * \param obj child object for linking
3495 * \retval 0, on success
3496 * \retval -ve, on error
3498 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3499 struct osd_object *dir,
3500 struct inode *parent_dir, const char *name,
3501 const struct dt_rec *dot_fid,
3502 const struct dt_rec *dot_dot_fid,
3505 struct inode *inode = dir->oo_inode;
3506 struct osd_thandle *oth;
3509 oth = container_of(th, struct osd_thandle, ot_super);
3510 LASSERT(oth->ot_handle->h_transaction != NULL);
3511 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3513 if (strcmp(name, dot) == 0) {
3514 if (dir->oo_compat_dot_created) {
3517 LASSERT(inode == parent_dir);
3518 dir->oo_compat_dot_created = 1;
3521 } else if (strcmp(name, dotdot) == 0) {
3522 if (!dir->oo_compat_dot_created)
3524 /* in case of rename, dotdot is already created */
3525 if (dir->oo_compat_dotdot_created) {
3526 return __osd_ea_add_rec(info, dir, parent_dir, name,
3527 dot_dot_fid, NULL, th);
3530 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3531 parent_dir, dot_fid,
3534 dir->oo_compat_dotdot_created = 1;
3542 * It will call the appropriate osd_add* function and return the
3543 * value, return by respective functions.
3545 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3546 struct inode *cinode, const char *name,
3547 const struct dt_rec *fid, struct thandle *th)
3549 struct osd_thread_info *info = osd_oti_get(env);
3550 struct htree_lock *hlock;
3553 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3555 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3557 if (hlock != NULL) {
3558 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3561 down_write(&pobj->oo_ext_idx_sem);
3563 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3564 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3567 if (hlock != NULL) {
3568 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3569 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3571 down_write(&pobj->oo_ext_idx_sem);
3574 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3575 struct lu_fid *tfid = &info->oti_fid;
3577 *tfid = *(const struct lu_fid *)fid;
3579 rc = __osd_ea_add_rec(info, pobj, cinode, name,
3580 (const struct dt_rec *)tfid,
3583 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3588 ldiskfs_htree_unlock(hlock);
3590 up_write(&pobj->oo_ext_idx_sem);
3596 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3597 struct osd_idmap_cache *oic)
3599 struct osd_scrub *scrub = &dev->od_scrub;
3600 struct lu_fid *fid = &oic->oic_fid;
3601 struct osd_inode_id *id = &oti->oti_id;
3606 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3610 rc = osd_oi_lookup(oti, dev, fid, id, true);
3611 if (rc != 0 && rc != -ENOENT)
3614 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3617 if (thread_is_running(&scrub->os_thread)) {
3618 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3619 /* There is race condition between osd_oi_lookup and OI scrub.
3620 * The OI scrub finished just after osd_oi_lookup() failure.
3621 * Under such case, it is unnecessary to trigger OI scrub again,
3622 * but try to call osd_oi_lookup() again. */
3623 if (unlikely(rc == -EAGAIN))
3629 if (!dev->od_noscrub && ++once == 1) {
3630 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3632 rc = osd_scrub_start(dev);
3633 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3635 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3644 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3645 struct osd_device *dev,
3646 struct osd_idmap_cache *oic,
3647 struct lu_fid *fid, __u32 ino)
3649 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
3650 struct inode *inode;
3653 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3654 inode = osd_iget(oti, dev, &oic->oic_lid);
3655 if (IS_ERR(inode)) {
3656 fid_zero(&oic->oic_fid);
3657 return PTR_ERR(inode);
3660 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3663 fid_zero(&oic->oic_fid);
3665 *fid = oic->oic_fid = lma->lma_self_fid;
3670 * Calls ->lookup() to find dentry. From dentry get inode and
3671 * read inode's ea to get fid. This is required for interoperability
3674 * \retval 0, on success
3675 * \retval -ve, on error
3677 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3678 struct dt_rec *rec, const struct dt_key *key)
3680 struct inode *dir = obj->oo_inode;
3681 struct dentry *dentry;
3682 struct ldiskfs_dir_entry_2 *de;
3683 struct buffer_head *bh;
3684 struct lu_fid *fid = (struct lu_fid *) rec;
3685 struct htree_lock *hlock = NULL;
3690 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3692 dentry = osd_child_dentry_get(env, obj,
3693 (char *)key, strlen((char *)key));
3695 if (obj->oo_hl_head != NULL) {
3696 hlock = osd_oti_get(env)->oti_hlock;
3697 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3698 dir, LDISKFS_HLOCK_LOOKUP);
3700 down_read(&obj->oo_ext_idx_sem);
3703 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3705 struct osd_thread_info *oti = osd_oti_get(env);
3706 struct osd_inode_id *id = &oti->oti_id;
3707 struct osd_idmap_cache *oic = &oti->oti_cache;
3708 struct osd_device *dev = osd_obj2dev(obj);
3709 struct osd_scrub *scrub = &dev->od_scrub;
3710 struct scrub_file *sf = &scrub->os_file;
3712 ino = le32_to_cpu(de->inode);
3713 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
3715 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
3719 rc = osd_get_fid_from_dentry(de, rec);
3721 /* done with de, release bh */
3724 rc = osd_ea_fid_get(env, obj, ino, fid, id);
3726 osd_id_gen(id, ino, OSD_OII_NOGEN);
3727 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
3728 fid_zero(&oic->oic_fid);
3733 oic->oic_fid = *fid;
3734 if ((scrub->os_pos_current <= ino) &&
3735 ((sf->sf_flags & SF_INCONSISTENT) ||
3736 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
3737 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3739 osd_consistency_check(oti, dev, oic);
3748 ldiskfs_htree_unlock(hlock);
3750 up_read(&obj->oo_ext_idx_sem);
3755 * Find the osd object for given fid.
3757 * \param fid need to find the osd object having this fid
3759 * \retval osd_object on success
3760 * \retval -ve on error
3762 struct osd_object *osd_object_find(const struct lu_env *env,
3763 struct dt_object *dt,
3764 const struct lu_fid *fid)
3766 struct lu_device *ludev = dt->do_lu.lo_dev;
3767 struct osd_object *child = NULL;
3768 struct lu_object *luch;
3769 struct lu_object *lo;
3772 * at this point topdev might not exist yet
3773 * (i.e. MGS is preparing profiles). so we can
3774 * not rely on topdev and instead lookup with
3775 * our device passed as topdev. this can't work
3776 * if the object isn't cached yet (as osd doesn't
3777 * allocate lu_header). IOW, the object must be
3778 * in the cache, otherwise lu_object_alloc() crashes
3781 luch = lu_object_find_at(env, ludev, fid, NULL);
3782 if (!IS_ERR(luch)) {
3783 if (lu_object_exists(luch)) {
3784 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3786 child = osd_obj(lo);
3788 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3789 "lu_object can't be located"
3790 DFID"\n", PFID(fid));
3792 if (child == NULL) {
3793 lu_object_put(env, luch);
3794 CERROR("Unable to get osd_object\n");
3795 child = ERR_PTR(-ENOENT);
3798 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3799 "lu_object does not exists "DFID"\n",
3801 lu_object_put(env, luch);
3802 child = ERR_PTR(-ENOENT);
3805 child = (void *)luch;
3811 * Put the osd object once done with it.
3813 * \param obj osd object that needs to be put
3815 static inline void osd_object_put(const struct lu_env *env,
3816 struct osd_object *obj)
3818 lu_object_put(env, &obj->oo_dt.do_lu);
3821 static int osd_index_declare_ea_insert(const struct lu_env *env,
3822 struct dt_object *dt,
3823 const struct dt_rec *rec,
3824 const struct dt_key *key,
3825 struct thandle *handle)
3827 struct osd_thandle *oh;
3828 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3829 struct lu_fid *fid = (struct lu_fid *)rec;
3833 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3834 LASSERT(handle != NULL);
3836 oh = container_of0(handle, struct osd_thandle, ot_super);
3837 LASSERT(oh->ot_handle == NULL);
3839 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3840 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3842 if (osd_dt_obj(dt)->oo_inode == NULL) {
3843 const char *name = (const char *)key;
3844 /* Object is not being created yet. Only happens when
3845 * 1. declare directory create
3846 * 2. declare insert .
3847 * 3. declare insert ..
3849 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
3851 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3853 /* We ignore block quota on meta pool (MDTs), so needn't
3854 * calculate how many blocks will be consumed by this index
3856 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
3857 oh, true, true, NULL, false);
3863 rc = osd_remote_fid(env, osd, fid);
3869 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3870 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
3871 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3872 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3873 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3874 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3880 * Index add function for interoperability mode (b11826).
3881 * It will add the directory entry.This entry is needed to
3882 * maintain name->fid mapping.
3884 * \param key it is key i.e. file entry to be inserted
3885 * \param rec it is value of given key i.e. fid
3887 * \retval 0, on success
3888 * \retval -ve, on error
3890 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3891 const struct dt_rec *rec,
3892 const struct dt_key *key, struct thandle *th,
3893 struct lustre_capa *capa, int ignore_quota)
3895 struct osd_object *obj = osd_dt_obj(dt);
3896 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3897 struct lu_fid *fid = (struct lu_fid *) rec;
3898 const char *name = (const char *)key;
3899 struct osd_thread_info *oti = osd_oti_get(env);
3900 struct osd_inode_id *id = &oti->oti_id;
3901 struct inode *child_inode = NULL;
3902 struct osd_object *child = NULL;
3906 LASSERT(osd_invariant(obj));
3907 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3908 LASSERT(th != NULL);
3910 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3912 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3915 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
3917 rc = osd_remote_fid(env, osd, fid);
3919 CERROR("%s: Can not find object "DFID" rc %d\n",
3920 osd_name(osd), PFID(fid), rc);
3925 /* Insert remote entry */
3926 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
3927 struct osd_mdobj_map *omm = osd->od_mdt_map;
3928 struct osd_thandle *oh;
3930 /* If parent on remote MDT, we need put this object
3932 oh = container_of(th, typeof(*oh), ot_super);
3933 rc = osd_add_to_agent(env, osd, obj, oh);
3935 CERROR("%s: add agent "DFID" error: rc = %d\n",
3937 PFID(lu_object_fid(&dt->do_lu)), rc);
3941 child_inode = igrab(omm->omm_agent_dentry->d_inode);
3943 child_inode = osd_create_remote_inode(env, osd, obj,
3945 if (IS_ERR(child_inode))
3946 RETURN(PTR_ERR(child_inode));
3949 /* Insert local entry */
3950 child = osd_object_find(env, dt, fid);
3951 if (IS_ERR(child)) {
3952 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
3953 osd_name(osd), PFID(fid),
3954 id->oii_ino, id->oii_gen,
3955 (int)PTR_ERR(child_inode));
3956 RETURN(PTR_ERR(child_inode));
3958 child_inode = igrab(child->oo_inode);
3961 rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
3965 osd_object_put(env, child);
3966 LASSERT(osd_invariant(obj));
3971 * Initialize osd Iterator for given osd index object.
3973 * \param dt osd index object
3976 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3977 struct dt_object *dt,
3979 struct lustre_capa *capa)
3981 struct osd_it_iam *it;
3982 struct osd_thread_info *oti = osd_oti_get(env);
3983 struct osd_object *obj = osd_dt_obj(dt);
3984 struct lu_object *lo = &dt->do_lu;
3985 struct iam_path_descr *ipd;
3986 struct iam_container *bag = &obj->oo_dir->od_container;
3988 LASSERT(lu_object_exists(lo));
3990 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3991 return ERR_PTR(-EACCES);
3994 ipd = osd_it_ipd_get(env, bag);
3995 if (likely(ipd != NULL)) {
3999 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4000 return (struct dt_it *)it;
4002 return ERR_PTR(-ENOMEM);
4006 * free given Iterator.
4009 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4011 struct osd_it_iam *it = (struct osd_it_iam *)di;
4012 struct osd_object *obj = it->oi_obj;
4014 iam_it_fini(&it->oi_it);
4015 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4016 lu_object_put(env, &obj->oo_dt.do_lu);
4020 * Move Iterator to record specified by \a key
4022 * \param di osd iterator
4023 * \param key key for index
4025 * \retval +ve di points to record with least key not larger than key
4026 * \retval 0 di points to exact matched key
4027 * \retval -ve failure
4030 static int osd_it_iam_get(const struct lu_env *env,
4031 struct dt_it *di, const struct dt_key *key)
4033 struct osd_thread_info *oti = osd_oti_get(env);
4034 struct osd_it_iam *it = (struct osd_it_iam *)di;
4036 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4037 /* swab quota uid/gid */
4038 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4039 key = (struct dt_key *)&oti->oti_quota_id;
4042 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4048 * \param di osd iterator
4050 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4052 struct osd_it_iam *it = (struct osd_it_iam *)di;
4054 iam_it_put(&it->oi_it);
4058 * Move iterator by one record
4060 * \param di osd iterator
4062 * \retval +1 end of container reached
4064 * \retval -ve failure
4067 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4069 struct osd_it_iam *it = (struct osd_it_iam *)di;
4071 return iam_it_next(&it->oi_it);
4075 * Return pointer to the key under iterator.
4078 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4079 const struct dt_it *di)
4081 struct osd_thread_info *oti = osd_oti_get(env);
4082 struct osd_it_iam *it = (struct osd_it_iam *)di;
4083 struct osd_object *obj = it->oi_obj;
4086 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4088 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4089 /* swab quota uid/gid */
4090 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4091 key = (struct dt_key *)&oti->oti_quota_id;
4098 * Return size of key under iterator (in bytes)
4101 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4103 struct osd_it_iam *it = (struct osd_it_iam *)di;
4105 return iam_it_key_size(&it->oi_it);
4109 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4111 /* check if file type is required */
4112 if (ent->lde_attrs & LUDA_TYPE) {
4113 struct luda_type *lt;
4114 int align = sizeof(*lt) - 1;
4116 len = (len + align) & ~align;
4117 lt = (struct luda_type *)(ent->lde_name + len);
4118 lt->lt_type = cpu_to_le16(DTTOIF(type));
4121 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4125 * build lu direct from backend fs dirent.
4129 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4130 char *name, __u16 namelen, __u16 type, __u32 attr)
4132 ent->lde_attrs = attr | LUDA_FID;
4133 fid_cpu_to_le(&ent->lde_fid, fid);
4135 ent->lde_hash = cpu_to_le64(offset);
4136 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4138 strncpy(ent->lde_name, name, namelen);
4139 ent->lde_name[namelen] = '\0';
4140 ent->lde_namelen = cpu_to_le16(namelen);
4142 /* append lustre attributes */
4143 osd_it_append_attrs(ent, namelen, type);
4147 * Return pointer to the record under iterator.
4149 static int osd_it_iam_rec(const struct lu_env *env,
4150 const struct dt_it *di,
4151 struct dt_rec *dtrec, __u32 attr)
4153 struct osd_it_iam *it = (struct osd_it_iam *)di;
4154 struct osd_thread_info *info = osd_oti_get(env);
4157 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4158 const struct osd_fid_pack *rec;
4159 struct lu_fid *fid = &info->oti_fid;
4160 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4166 name = (char *)iam_it_key_get(&it->oi_it);
4168 RETURN(PTR_ERR(name));
4170 namelen = iam_it_key_size(&it->oi_it);
4172 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4174 RETURN(PTR_ERR(rec));
4176 rc = osd_fid_unpack(fid, rec);
4180 hash = iam_it_store(&it->oi_it);
4182 /* IAM does not store object type in IAM index (dir) */
4183 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4185 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4186 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4187 (struct iam_rec *)dtrec);
4188 osd_quota_unpack(it->oi_obj, dtrec);
4190 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4191 (struct iam_rec *)dtrec);
4198 * Returns cookie for current Iterator position.
4200 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4202 struct osd_it_iam *it = (struct osd_it_iam *)di;
4204 return iam_it_store(&it->oi_it);
4208 * Restore iterator from cookie.
4210 * \param di osd iterator
4211 * \param hash Iterator location cookie
4213 * \retval +ve di points to record with least key not larger than key.
4214 * \retval 0 di points to exact matched key
4215 * \retval -ve failure
4218 static int osd_it_iam_load(const struct lu_env *env,
4219 const struct dt_it *di, __u64 hash)
4221 struct osd_it_iam *it = (struct osd_it_iam *)di;
4223 return iam_it_load(&it->oi_it, hash);
4226 static const struct dt_index_operations osd_index_iam_ops = {
4227 .dio_lookup = osd_index_iam_lookup,
4228 .dio_declare_insert = osd_index_declare_iam_insert,
4229 .dio_insert = osd_index_iam_insert,
4230 .dio_declare_delete = osd_index_declare_iam_delete,
4231 .dio_delete = osd_index_iam_delete,
4233 .init = osd_it_iam_init,
4234 .fini = osd_it_iam_fini,
4235 .get = osd_it_iam_get,
4236 .put = osd_it_iam_put,
4237 .next = osd_it_iam_next,
4238 .key = osd_it_iam_key,
4239 .key_size = osd_it_iam_key_size,
4240 .rec = osd_it_iam_rec,
4241 .store = osd_it_iam_store,
4242 .load = osd_it_iam_load
4248 * Creates or initializes iterator context.
4250 * \retval struct osd_it_ea, iterator structure on success
4253 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4254 struct dt_object *dt,
4256 struct lustre_capa *capa)
4258 struct osd_object *obj = osd_dt_obj(dt);
4259 struct osd_thread_info *info = osd_oti_get(env);
4260 struct osd_it_ea *it = &info->oti_it_ea;
4261 struct lu_object *lo = &dt->do_lu;
4262 struct dentry *obj_dentry = &info->oti_it_dentry;
4264 LASSERT(lu_object_exists(lo));
4266 obj_dentry->d_inode = obj->oo_inode;
4267 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4268 obj_dentry->d_name.hash = 0;
4270 it->oie_rd_dirent = 0;
4271 it->oie_it_dirent = 0;
4272 it->oie_dirent = NULL;
4273 it->oie_buf = info->oti_it_ea_buf;
4275 it->oie_file.f_pos = 0;
4276 it->oie_file.f_dentry = obj_dentry;
4277 if (attr & LUDA_64BITHASH)
4278 it->oie_file.f_mode |= FMODE_64BITHASH;
4280 it->oie_file.f_mode |= FMODE_32BITHASH;
4281 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
4282 it->oie_file.f_op = obj->oo_inode->i_fop;
4283 it->oie_file.private_data = NULL;
4285 RETURN((struct dt_it *) it);
4289 * Destroy or finishes iterator context.
4291 * \param di iterator structure to be destroyed
4293 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4295 struct osd_it_ea *it = (struct osd_it_ea *)di;
4296 struct osd_object *obj = it->oie_obj;
4297 struct inode *inode = obj->oo_inode;
4300 it->oie_file.f_op->release(inode, &it->oie_file);
4301 lu_object_put(env, &obj->oo_dt.do_lu);
4306 * It position the iterator at given key, so that next lookup continues from
4307 * that key Or it is similar to dio_it->load() but based on a key,
4308 * rather than file position.
4310 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4313 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4315 static int osd_it_ea_get(const struct lu_env *env,
4316 struct dt_it *di, const struct dt_key *key)
4318 struct osd_it_ea *it = (struct osd_it_ea *)di;
4321 LASSERT(((const char *)key)[0] == '\0');
4322 it->oie_file.f_pos = 0;
4323 it->oie_rd_dirent = 0;
4324 it->oie_it_dirent = 0;
4325 it->oie_dirent = NULL;
4333 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4338 * It is called internally by ->readdir(). It fills the
4339 * iterator's in-memory data structure with required
4340 * information i.e. name, namelen, rec_size etc.
4342 * \param buf in which information to be filled in.
4343 * \param name name of the file in given dir
4345 * \retval 0 on success
4346 * \retval 1 on buffer full
4348 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4349 loff_t offset, __u64 ino,
4352 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4353 struct osd_object *obj = it->oie_obj;
4354 struct osd_it_ea_dirent *ent = it->oie_dirent;
4355 struct lu_fid *fid = &ent->oied_fid;
4356 struct osd_fid_pack *rec;
4359 /* this should never happen */
4360 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4361 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4365 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4369 /* "." is just the object itself. */
4370 if (namelen == 1 && name[0] == '.') {
4371 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4372 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4373 rec = (struct osd_fid_pack*) (name + namelen + 1);
4374 if (osd_fid_unpack(fid, rec) != 0)
4379 d_type &= ~LDISKFS_DIRENT_LUFID;
4381 /* NOT export local root. */
4382 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4383 ino = obj->oo_inode->i_ino;
4384 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4387 ent->oied_ino = ino;
4388 ent->oied_off = offset;
4389 ent->oied_namelen = namelen;
4390 ent->oied_type = d_type;
4392 memcpy(ent->oied_name, name, namelen);
4394 it->oie_rd_dirent++;
4395 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4400 * Calls ->readdir() to load a directory entry at a time
4401 * and stored it in iterator's in-memory data structure.
4403 * \param di iterator's in memory structure
4405 * \retval 0 on success
4406 * \retval -ve on error
4408 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4409 const struct dt_it *di)
4411 struct osd_it_ea *it = (struct osd_it_ea *)di;
4412 struct osd_object *obj = it->oie_obj;
4413 struct inode *inode = obj->oo_inode;
4414 struct htree_lock *hlock = NULL;
4418 it->oie_dirent = it->oie_buf;
4419 it->oie_rd_dirent = 0;
4421 if (obj->oo_hl_head != NULL) {
4422 hlock = osd_oti_get(env)->oti_hlock;
4423 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4424 inode, LDISKFS_HLOCK_READDIR);
4426 down_read(&obj->oo_ext_idx_sem);
4429 result = inode->i_fop->readdir(&it->oie_file, it,
4430 (filldir_t) osd_ldiskfs_filldir);
4433 ldiskfs_htree_unlock(hlock);
4435 up_read(&obj->oo_ext_idx_sem);
4437 if (it->oie_rd_dirent == 0) {
4440 it->oie_dirent = it->oie_buf;
4441 it->oie_it_dirent = 1;
4448 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4449 * to load a directory entry at a time and stored it in
4450 * iterator's in-memory data structure.
4452 * \param di iterator's in memory structure
4454 * \retval +ve iterator reached to end
4455 * \retval 0 iterator not reached to end
4456 * \retval -ve on error
4458 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4460 struct osd_it_ea *it = (struct osd_it_ea *)di;
4465 if (it->oie_it_dirent < it->oie_rd_dirent) {
4467 (void *) it->oie_dirent +
4468 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4469 it->oie_dirent->oied_namelen);
4470 it->oie_it_dirent++;
4473 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4476 rc = osd_ldiskfs_it_fill(env, di);
4483 * Returns the key at current position from iterator's in memory structure.
4485 * \param di iterator's in memory structure
4487 * \retval key i.e. struct dt_key on success
4489 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4490 const struct dt_it *di)
4492 struct osd_it_ea *it = (struct osd_it_ea *)di;
4494 return (struct dt_key *)it->oie_dirent->oied_name;
4498 * Returns the key's size at current position from iterator's in memory structure.
4500 * \param di iterator's in memory structure
4502 * \retval key_size i.e. struct dt_key on success
4504 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4506 struct osd_it_ea *it = (struct osd_it_ea *)di;
4508 return it->oie_dirent->oied_namelen;
4512 osd_dirent_update(handle_t *jh, struct super_block *sb,
4513 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4514 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4516 struct osd_fid_pack *rec;
4520 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4521 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4523 rc = ldiskfs_journal_get_write_access(jh, bh);
4525 CERROR("%.16s: fail to write access for update dirent: "
4526 "name = %.*s, rc = %d\n",
4527 LDISKFS_SB(sb)->s_es->s_volume_name,
4528 ent->oied_namelen, ent->oied_name, rc);
4532 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4533 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4534 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4536 CERROR("%.16s: fail to dirty metadata for update dirent: "
4537 "name = %.*s, rc = %d\n",
4538 LDISKFS_SB(sb)->s_es->s_volume_name,
4539 ent->oied_namelen, ent->oied_name, rc);
4545 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4547 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4548 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4555 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4556 struct inode *dir, struct inode *inode,
4557 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4558 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4559 struct htree_lock *hlock)
4561 struct dentry *dentry;
4562 struct osd_fid_pack *rec;
4563 struct ldiskfs_dentry_param *ldp;
4567 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4568 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4571 /* There is enough space to hold the FID-in-dirent. */
4572 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4573 dir->i_sb->s_blocksize)) {
4574 rc = ldiskfs_journal_get_write_access(jh, bh);
4576 CERROR("%.16s: fail to write access for reinsert "
4577 "dirent: name = %.*s, rc = %d\n",
4578 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4579 ent->oied_namelen, ent->oied_name, rc);
4583 de->name[de->name_len] = 0;
4584 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4585 rec->fp_len = sizeof(struct lu_fid) + 1;
4586 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4587 de->file_type |= LDISKFS_DIRENT_LUFID;
4589 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4591 CERROR("%.16s: fail to dirty metadata for reinsert "
4592 "dirent: name = %.*s, rc = %d\n",
4593 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4594 ent->oied_namelen, ent->oied_name, rc);
4599 rc = ldiskfs_delete_entry(jh, dir, de, bh);
4601 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4602 "name = %.*s, rc = %d\n",
4603 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4604 ent->oied_namelen, ent->oied_name, rc);
4608 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4610 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4611 osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4612 dentry->d_fsdata = (void *)ldp;
4613 ll_vfs_dq_init(dir);
4614 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4615 /* It is too bad, we cannot reinsert the name entry back.
4616 * That means we lose it! */
4618 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4619 "name = %.*s, rc = %d\n",
4620 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4621 ent->oied_namelen, ent->oied_name, rc);
4627 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4628 struct osd_it_ea *it, struct lu_fid *fid,
4629 struct osd_inode_id *id, __u32 *attr)
4631 struct osd_thread_info *info = osd_oti_get(env);
4632 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
4633 struct osd_device *dev = osd_obj2dev(obj);
4634 struct super_block *sb = osd_sb(dev);
4635 const char *devname =
4636 LDISKFS_SB(sb)->s_es->s_volume_name;
4637 struct osd_it_ea_dirent *ent = it->oie_dirent;
4638 struct inode *dir = obj->oo_inode;
4639 struct htree_lock *hlock = NULL;
4640 struct buffer_head *bh = NULL;
4641 handle_t *jh = NULL;
4642 struct ldiskfs_dir_entry_2 *de;
4643 struct dentry *dentry;
4644 struct inode *inode;
4648 bool is_dotdot = false;
4651 if (ent->oied_name[0] == '.') {
4652 /* Skip dot entry, even if it has stale FID-in-dirent, because
4653 * we do not use such FID-in-dirent anymore, it is harmless. */
4654 if (ent->oied_namelen == 1)
4657 if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4661 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4664 /* We need to ensure that the name entry is still valid.
4665 * Because it may be removed or renamed by other already.
4667 * The unlink or rename operation will start journal before PDO lock,
4668 * so to avoid deadlock, here we need to start journal handle before
4669 * related PDO lock also. But because we do not know whether there
4670 * will be something to be repaired before PDO lock, we just start
4671 * journal without conditions.
4673 * We may need to remove the name entry firstly, then insert back.
4674 * One credit is for user quota file update.
4675 * One credit is for group quota file update.
4676 * Two credits are for dirty inode. */
4677 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4678 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4681 if (dev->od_dirent_journal) {
4682 jh = ldiskfs_journal_start_sb(sb, credits);
4685 CERROR("%.16s: fail to start trans for dirent "
4686 "check_repair: credits %d, name %.*s, rc %d\n",
4687 devname, credits, ent->oied_namelen,
4688 ent->oied_name, rc);
4693 if (obj->oo_hl_head != NULL) {
4694 hlock = osd_oti_get(env)->oti_hlock;
4695 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
4698 down_write(&obj->oo_ext_idx_sem);
4701 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
4702 /* For dotdot entry, if there is not enough space to hold FID-in-dirent,
4703 * just keep it there. It only happens when the device upgraded from 1.8
4704 * or restored from MDT file-level backup. For the whole directory, only
4705 * dotdot entry has no FID-in-dirent and needs to get FID from LMA when
4706 * readdir, it will not affect the performance much. */
4707 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
4708 (is_dotdot && !osd_dirent_has_space(de->rec_len,
4710 sb->s_blocksize))) {
4711 *attr |= LUDA_IGNORE;
4712 GOTO(out_journal, rc = 0);
4715 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
4716 inode = osd_iget(info, dev, id);
4717 if (IS_ERR(inode)) {
4718 rc = PTR_ERR(inode);
4719 if (rc == -ENOENT || rc == -ESTALE) {
4720 *attr |= LUDA_IGNORE;
4724 GOTO(out_journal, rc);
4727 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
4729 if (fid_is_sane(fid)) {
4730 /* FID-in-dirent is valid. */
4731 if (lu_fid_eq(fid, &lma->lma_self_fid))
4732 GOTO(out_inode, rc = 0);
4734 /* Do not repair under dryrun mode. */
4735 if (*attr & LUDA_VERIFY_DRYRUN) {
4736 *attr |= LUDA_REPAIR;
4737 GOTO(out_inode, rc = 0);
4740 if (!dev->od_dirent_journal) {
4744 ldiskfs_htree_unlock(hlock);
4746 up_write(&obj->oo_ext_idx_sem);
4747 dev->od_dirent_journal = 1;
4751 *fid = lma->lma_self_fid;
4753 /* Update the FID-in-dirent. */
4754 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
4756 *attr |= LUDA_REPAIR;
4758 /* Do not repair under dryrun mode. */
4759 if (*attr & LUDA_VERIFY_DRYRUN) {
4760 *attr |= LUDA_REPAIR;
4761 GOTO(out_inode, rc = 0);
4764 if (!dev->od_dirent_journal) {
4768 ldiskfs_htree_unlock(hlock);
4770 up_write(&obj->oo_ext_idx_sem);
4771 dev->od_dirent_journal = 1;
4775 *fid = lma->lma_self_fid;
4777 /* Append the FID-in-dirent. */
4778 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4779 fid, bh, de, hlock);
4781 *attr |= LUDA_REPAIR;
4783 } else if (rc == -ENODATA) {
4784 /* Do not repair under dryrun mode. */
4785 if (*attr & LUDA_VERIFY_DRYRUN) {
4786 if (fid_is_sane(fid))
4787 *attr |= LUDA_REPAIR;
4789 *attr |= LUDA_UPGRADE;
4790 GOTO(out_inode, rc = 0);
4793 if (!dev->od_dirent_journal) {
4797 ldiskfs_htree_unlock(hlock);
4799 up_write(&obj->oo_ext_idx_sem);
4800 dev->od_dirent_journal = 1;
4805 if (unlikely(fid_is_sane(fid))) {
4806 /* FID-in-dirent exists, but FID-in-LMA is lost.
4807 * Trust the FID-in-dirent, and add FID-in-LMA. */
4808 rc = osd_ea_fid_set(info, inode, fid);
4810 *attr |= LUDA_REPAIR;
4812 lu_igif_build(fid, inode->i_ino, inode->i_generation);
4813 /* It is probably IGIF object. Only aappend the
4814 * FID-in-dirent. OI scrub will process FID-in-LMA. */
4815 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4816 fid, bh, de, hlock);
4818 *attr |= LUDA_UPGRADE;
4822 GOTO(out_inode, rc);
4830 ldiskfs_htree_unlock(hlock);
4832 up_write(&obj->oo_ext_idx_sem);
4834 ldiskfs_journal_stop(jh);
4835 if (rc >= 0 && !dirty)
4836 dev->od_dirent_journal = 0;
4841 * Returns the value at current position from iterator's in memory structure.
4843 * \param di struct osd_it_ea, iterator's in memory structure
4844 * \param attr attr requested for dirent.
4845 * \param lde lustre dirent
4847 * \retval 0 no error and \param lde has correct lustre dirent.
4848 * \retval -ve on error
4850 static inline int osd_it_ea_rec(const struct lu_env *env,
4851 const struct dt_it *di,
4852 struct dt_rec *dtrec, __u32 attr)
4854 struct osd_it_ea *it = (struct osd_it_ea *)di;
4855 struct osd_object *obj = it->oie_obj;
4856 struct osd_device *dev = osd_obj2dev(obj);
4857 struct osd_scrub *scrub = &dev->od_scrub;
4858 struct scrub_file *sf = &scrub->os_file;
4859 struct osd_thread_info *oti = osd_oti_get(env);
4860 struct osd_inode_id *id = &oti->oti_id;
4861 struct osd_idmap_cache *oic = &oti->oti_cache;
4862 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4863 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4864 __u32 ino = it->oie_dirent->oied_ino;
4868 if (attr & LUDA_VERIFY) {
4870 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
4871 attr |= LUDA_IGNORE;
4876 rc = osd_dirent_check_repair(env, obj, it, fid, id, &attr);
4878 attr &= ~LU_DIRENT_ATTRS_MASK;
4879 if (!fid_is_sane(fid)) {
4880 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
4883 rc = osd_ea_fid_get(env, obj, ino, fid, id);
4885 osd_id_gen(id, ino, OSD_OII_NOGEN);
4893 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4894 it->oie_dirent->oied_name,
4895 it->oie_dirent->oied_namelen,
4896 it->oie_dirent->oied_type, attr);
4898 if (osd_remote_fid(env, dev, fid))
4901 if (likely(!(attr & LUDA_IGNORE))) {
4903 oic->oic_fid = *fid;
4906 if (!(attr & LUDA_VERIFY) &&
4907 (scrub->os_pos_current <= ino) &&
4908 ((sf->sf_flags & SF_INCONSISTENT) ||
4909 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
4910 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4911 osd_consistency_check(oti, dev, oic);
4917 * Returns a cookie for current position of the iterator head, so that
4918 * user can use this cookie to load/start the iterator next time.
4920 * \param di iterator's in memory structure
4922 * \retval cookie for current position, on success
4924 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4926 struct osd_it_ea *it = (struct osd_it_ea *)di;
4928 return it->oie_dirent->oied_off;
4932 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4933 * to load a directory entry at a time and stored it i inn,
4934 * in iterator's in-memory data structure.
4936 * \param di struct osd_it_ea, iterator's in memory structure
4938 * \retval +ve on success
4939 * \retval -ve on error
4941 static int osd_it_ea_load(const struct lu_env *env,
4942 const struct dt_it *di, __u64 hash)
4944 struct osd_it_ea *it = (struct osd_it_ea *)di;
4948 it->oie_file.f_pos = hash;
4950 rc = osd_ldiskfs_it_fill(env, di);
4958 * Index lookup function for interoperability mode (b11826).
4960 * \param key, key i.e. file name to be searched
4962 * \retval +ve, on success
4963 * \retval -ve, on error
4965 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4966 struct dt_rec *rec, const struct dt_key *key,
4967 struct lustre_capa *capa)
4969 struct osd_object *obj = osd_dt_obj(dt);
4974 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4975 LINVRNT(osd_invariant(obj));
4977 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4980 rc = osd_ea_lookup_rec(env, obj, rec, key);
4987 * Index and Iterator operations for interoperability
4988 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4990 static const struct dt_index_operations osd_index_ea_ops = {
4991 .dio_lookup = osd_index_ea_lookup,
4992 .dio_declare_insert = osd_index_declare_ea_insert,
4993 .dio_insert = osd_index_ea_insert,
4994 .dio_declare_delete = osd_index_declare_ea_delete,
4995 .dio_delete = osd_index_ea_delete,
4997 .init = osd_it_ea_init,
4998 .fini = osd_it_ea_fini,
4999 .get = osd_it_ea_get,
5000 .put = osd_it_ea_put,
5001 .next = osd_it_ea_next,
5002 .key = osd_it_ea_key,
5003 .key_size = osd_it_ea_key_size,
5004 .rec = osd_it_ea_rec,
5005 .store = osd_it_ea_store,
5006 .load = osd_it_ea_load
5010 static void *osd_key_init(const struct lu_context *ctx,
5011 struct lu_context_key *key)
5013 struct osd_thread_info *info;
5015 OBD_ALLOC_PTR(info);
5017 return ERR_PTR(-ENOMEM);
5019 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5020 if (info->oti_it_ea_buf == NULL)
5023 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5025 info->oti_hlock = ldiskfs_htree_lock_alloc();
5026 if (info->oti_hlock == NULL)
5032 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5035 return ERR_PTR(-ENOMEM);
5038 static void osd_key_fini(const struct lu_context *ctx,
5039 struct lu_context_key *key, void* data)
5041 struct osd_thread_info *info = data;
5043 if (info->oti_hlock != NULL)
5044 ldiskfs_htree_lock_free(info->oti_hlock);
5045 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5046 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5047 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5051 static void osd_key_exit(const struct lu_context *ctx,
5052 struct lu_context_key *key, void *data)
5054 struct osd_thread_info *info = data;
5056 LASSERT(info->oti_r_locks == 0);
5057 LASSERT(info->oti_w_locks == 0);
5058 LASSERT(info->oti_txns == 0);
5061 /* type constructor/destructor: osd_type_init, osd_type_fini */
5062 LU_TYPE_INIT_FINI(osd, &osd_key);
5064 struct lu_context_key osd_key = {
5065 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5066 .lct_init = osd_key_init,
5067 .lct_fini = osd_key_fini,
5068 .lct_exit = osd_key_exit
5072 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5073 const char *name, struct lu_device *next)
5075 struct osd_device *osd = osd_dev(d);
5077 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5078 >= sizeof(osd->od_svname))
5080 return osd_procfs_init(osd, name);
5083 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5087 osd_scrub_cleanup(env, o);
5090 fsfilt_put_ops(o->od_fsops);
5094 /* shutdown quota slave instance associated with the device */
5095 if (o->od_quota_slave != NULL) {
5096 qsd_fini(env, o->od_quota_slave);
5097 o->od_quota_slave = NULL;
5103 static int osd_mount(const struct lu_env *env,
5104 struct osd_device *o, struct lustre_cfg *cfg)
5106 const char *name = lustre_cfg_string(cfg, 0);
5107 const char *dev = lustre_cfg_string(cfg, 1);
5109 unsigned long page, s_flags, lmd_flags = 0;
5110 struct page *__page;
5111 struct file_system_type *type;
5112 char *options = NULL;
5117 if (o->od_mnt != NULL)
5120 if (strlen(dev) >= sizeof(o->od_mntdev))
5122 strcpy(o->od_mntdev, dev);
5124 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
5125 if (o->od_fsops == NULL) {
5126 CERROR("Can't find fsfilt_ldiskfs\n");
5130 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
5132 GOTO(out, rc = -ENOMEM);
5134 str = lustre_cfg_string(cfg, 2);
5135 s_flags = simple_strtoul(str, NULL, 0);
5136 str = strstr(str, ":");
5138 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5139 opts = lustre_cfg_string(cfg, 3);
5140 page = (unsigned long)cfs_page_address(__page);
5141 options = (char *)page;
5144 strcat(options, "user_xattr,acl");
5146 strcat(options, opts);
5148 /* Glom up mount options */
5149 if (*options != '\0')
5150 strcat(options, ",");
5151 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
5153 type = get_fs_type("ldiskfs");
5155 CERROR("%s: cannot find ldiskfs module\n", name);
5156 GOTO(out, rc = -ENODEV);
5159 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5160 cfs_module_put(type->owner);
5162 if (IS_ERR(o->od_mnt)) {
5163 rc = PTR_ERR(o->od_mnt);
5164 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5169 #ifdef HAVE_DEV_SET_RDONLY
5170 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5171 CERROR("%s: underlying device %s is marked as read-only. "
5172 "Setup failed\n", name, dev);
5175 GOTO(out, rc = -EROFS);
5179 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5180 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5181 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5184 GOTO(out, rc = -EINVAL);
5187 ldiskfs_set_inode_state(osd_sb(o)->s_root->d_inode,
5188 LDISKFS_STATE_LUSTRE_NO_OI);
5189 if (lmd_flags & LMD_FLG_NOSCRUB)
5194 OBD_PAGE_FREE(__page);
5196 fsfilt_put_ops(o->od_fsops);
5201 static struct lu_device *osd_device_fini(const struct lu_env *env,
5202 struct lu_device *d)
5207 rc = osd_shutdown(env, osd_dev(d));
5209 osd_obj_map_fini(osd_dev(d));
5211 shrink_dcache_sb(osd_sb(osd_dev(d)));
5212 osd_sync(env, lu2dt_dev(d));
5214 rc = osd_procfs_fini(osd_dev(d));
5216 CERROR("proc fini error %d \n", rc);
5217 RETURN (ERR_PTR(rc));
5220 if (osd_dev(d)->od_mnt) {
5221 mntput(osd_dev(d)->od_mnt);
5222 osd_dev(d)->od_mnt = NULL;
5228 static int osd_device_init0(const struct lu_env *env,
5229 struct osd_device *o,
5230 struct lustre_cfg *cfg)
5232 struct lu_device *l = osd2lu_dev(o);
5233 struct osd_thread_info *info;
5237 /* if the module was re-loaded, env can loose its keys */
5238 rc = lu_env_refill((struct lu_env *) env);
5241 info = osd_oti_get(env);
5244 l->ld_ops = &osd_lu_ops;
5245 o->od_dt_dev.dd_ops = &osd_dt_ops;
5247 spin_lock_init(&o->od_osfs_lock);
5248 mutex_init(&o->od_otable_mutex);
5249 o->od_osfs_age = cfs_time_shift_64(-1000);
5251 o->od_capa_hash = init_capa_hash();
5252 if (o->od_capa_hash == NULL)
5253 GOTO(out, rc = -ENOMEM);
5255 o->od_read_cache = 1;
5256 o->od_writethrough_cache = 1;
5257 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5259 rc = osd_mount(env, o, cfg);
5263 CFS_INIT_LIST_HEAD(&o->od_ios_list);
5264 /* setup scrub, including OI files initialization */
5265 rc = osd_scrub_setup(env, o);
5269 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5270 sizeof(o->od_svname));
5271 if (cplen >= sizeof(o->od_svname)) {
5276 rc = osd_obj_map_init(o);
5278 GOTO(out_scrub, rc);
5280 rc = lu_site_init(&o->od_site, l);
5282 GOTO(out_compat, rc);
5283 o->od_site.ls_bottom_dev = l;
5285 rc = lu_site_init_finish(&o->od_site);
5289 rc = osd_procfs_init(o, o->od_svname);
5291 CERROR("%s: can't initialize procfs: rc = %d\n",
5296 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5298 /* initialize quota slave instance */
5299 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5301 if (IS_ERR(o->od_quota_slave)) {
5302 rc = PTR_ERR(o->od_quota_slave);
5303 o->od_quota_slave = NULL;
5304 GOTO(out_procfs, rc);
5311 lu_site_fini(&o->od_site);
5313 osd_obj_map_fini(o);
5315 osd_scrub_cleanup(env, o);
5317 osd_oi_fini(info, o);
5318 osd_shutdown(env, o);
5322 cleanup_capa_hash(o->od_capa_hash);
5327 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5328 struct lu_device_type *t,
5329 struct lustre_cfg *cfg)
5331 struct osd_device *o;
5336 return ERR_PTR(-ENOMEM);
5338 rc = dt_device_init(&o->od_dt_dev, t);
5340 /* Because the ctx might be revived in dt_device_init,
5341 * refill the env here */
5342 lu_env_refill((struct lu_env *)env);
5343 rc = osd_device_init0(env, o, cfg);
5345 dt_device_fini(&o->od_dt_dev);
5348 if (unlikely(rc != 0))
5351 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5354 static struct lu_device *osd_device_free(const struct lu_env *env,
5355 struct lu_device *d)
5357 struct osd_device *o = osd_dev(d);
5360 cleanup_capa_hash(o->od_capa_hash);
5361 /* XXX: make osd top device in order to release reference */
5362 d->ld_site->ls_top_dev = d;
5363 lu_site_purge(env, d->ld_site, -1);
5364 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5365 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5366 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5368 lu_site_fini(&o->od_site);
5369 dt_device_fini(&o->od_dt_dev);
5374 static int osd_process_config(const struct lu_env *env,
5375 struct lu_device *d, struct lustre_cfg *cfg)
5377 struct osd_device *o = osd_dev(d);
5381 switch(cfg->lcfg_command) {
5383 err = osd_mount(env, o, cfg);
5386 lu_dev_del_linkage(d->ld_site, d);
5387 err = osd_shutdown(env, o);
5396 static int osd_recovery_complete(const struct lu_env *env,
5397 struct lu_device *d)
5399 struct osd_device *osd = osd_dev(d);
5403 if (osd->od_quota_slave == NULL)
5406 /* start qsd instance on recovery completion, this notifies the quota
5407 * slave code that we are about to process new requests now */
5408 rc = qsd_start(env, osd->od_quota_slave);
5413 * we use exports to track all osd users
5415 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5416 struct obd_device *obd, struct obd_uuid *cluuid,
5417 struct obd_connect_data *data, void *localdata)
5419 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5420 struct lustre_handle conn;
5424 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5426 rc = class_connect(&conn, obd, cluuid);
5430 *exp = class_conn2export(&conn);
5432 spin_lock(&osd->od_osfs_lock);
5434 spin_unlock(&osd->od_osfs_lock);
5440 * once last export (we don't count self-export) disappeared
5441 * osd can be released
5443 static int osd_obd_disconnect(struct obd_export *exp)
5445 struct obd_device *obd = exp->exp_obd;
5446 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5447 int rc, release = 0;
5450 /* Only disconnect the underlying layers on the final disconnect. */
5451 spin_lock(&osd->od_osfs_lock);
5453 if (osd->od_connects == 0)
5455 spin_unlock(&osd->od_osfs_lock);
5457 rc = class_disconnect(exp); /* bz 9811 */
5459 if (rc == 0 && release)
5460 class_manual_cleanup(obd);
5464 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5465 struct lu_device *dev)
5467 struct osd_device *osd = osd_dev(dev);
5471 if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
5472 /* MDT/MDD still use old infrastructure to create
5474 result = llo_local_objects_setup(env, lu2md_dev(pdev),
5480 if (osd->od_quota_slave != NULL)
5481 /* set up quota slave objects */
5482 result = qsd_prepare(env, osd->od_quota_slave);
5487 static const struct lu_object_operations osd_lu_obj_ops = {
5488 .loo_object_init = osd_object_init,
5489 .loo_object_delete = osd_object_delete,
5490 .loo_object_release = osd_object_release,
5491 .loo_object_free = osd_object_free,
5492 .loo_object_print = osd_object_print,
5493 .loo_object_invariant = osd_object_invariant
5496 const struct lu_device_operations osd_lu_ops = {
5497 .ldo_object_alloc = osd_object_alloc,
5498 .ldo_process_config = osd_process_config,
5499 .ldo_recovery_complete = osd_recovery_complete,
5500 .ldo_prepare = osd_prepare,
5503 static const struct lu_device_type_operations osd_device_type_ops = {
5504 .ldto_init = osd_type_init,
5505 .ldto_fini = osd_type_fini,
5507 .ldto_start = osd_type_start,
5508 .ldto_stop = osd_type_stop,
5510 .ldto_device_alloc = osd_device_alloc,
5511 .ldto_device_free = osd_device_free,
5513 .ldto_device_init = osd_device_init,
5514 .ldto_device_fini = osd_device_fini
5517 struct lu_device_type osd_device_type = {
5518 .ldt_tags = LU_DEVICE_DT,
5519 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
5520 .ldt_ops = &osd_device_type_ops,
5521 .ldt_ctx_tags = LCT_LOCAL,
5525 * lprocfs legacy support.
5527 static struct obd_ops osd_obd_device_ops = {
5528 .o_owner = THIS_MODULE,
5529 .o_connect = osd_obd_connect,
5530 .o_disconnect = osd_obd_disconnect
5533 static int __init osd_mod_init(void)
5535 struct lprocfs_static_vars lvars;
5538 lprocfs_osd_init_vars(&lvars);
5539 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
5540 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5543 static void __exit osd_mod_exit(void)
5545 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5548 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5549 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5550 MODULE_LICENSE("GPL");
5552 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);