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>
60 * struct OBD_{ALLOC,FREE}*()
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
66 #include <lustre_fid.h>
68 #include "osd_internal.h"
70 /* llo_* api support */
71 #include <md_object.h>
72 #include <lustre_quota.h>
75 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
76 "ldiskfs with parallel directory operations");
78 static const char dot[] = ".";
79 static const char dotdot[] = "..";
80 static const char remote_obj_dir[] = "REM_OBJ_DIR";
82 static const struct lu_object_operations osd_lu_obj_ops;
83 static const struct dt_object_operations osd_obj_ops;
84 static const struct dt_object_operations osd_obj_ea_ops;
85 static const struct dt_object_operations osd_obj_otable_it_ops;
86 static const struct dt_index_operations osd_index_iam_ops;
87 static const struct dt_index_operations osd_index_ea_ops;
89 #ifdef OSD_TRACK_DECLARES
90 int osd_trans_declare_op2rb[] = {
91 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
92 [OSD_OT_PUNCH] = OSD_OT_MAX,
93 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
94 [OSD_OT_CREATE] = OSD_OT_DESTROY,
95 [OSD_OT_DESTROY] = OSD_OT_CREATE,
96 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
97 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
98 [OSD_OT_WRITE] = OSD_OT_WRITE,
99 [OSD_OT_INSERT] = OSD_OT_DELETE,
100 [OSD_OT_DELETE] = OSD_OT_INSERT,
101 [OSD_OT_QUOTA] = OSD_OT_MAX,
105 static int osd_has_index(const struct osd_object *obj)
107 return obj->oo_dt.do_index_ops != NULL;
110 static int osd_object_invariant(const struct lu_object *l)
112 return osd_invariant(osd_obj(l));
116 * Concurrency: doesn't matter
118 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
120 return osd_oti_get(env)->oti_r_locks > 0;
124 * Concurrency: doesn't matter
126 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
128 struct osd_thread_info *oti = osd_oti_get(env);
129 return oti->oti_w_locks > 0 && o->oo_owner == env;
133 * Concurrency: doesn't access mutable data
135 static int osd_root_get(const struct lu_env *env,
136 struct dt_device *dev, struct lu_fid *f)
138 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
143 * OSD object methods.
147 * Concurrency: no concurrent access is possible that early in object
150 static struct lu_object *osd_object_alloc(const struct lu_env *env,
151 const struct lu_object_header *hdr,
154 struct osd_object *mo;
160 l = &mo->oo_dt.do_lu;
161 dt_object_init(&mo->oo_dt, NULL, d);
162 mo->oo_dt.do_ops = &osd_obj_ea_ops;
163 l->lo_ops = &osd_lu_obj_ops;
164 init_rwsem(&mo->oo_sem);
165 init_rwsem(&mo->oo_ext_idx_sem);
166 spin_lock_init(&mo->oo_guard);
173 static inline int __osd_xattr_get(struct inode *inode, struct dentry *dentry,
174 const char *name, void *buf, int len)
176 dentry->d_inode = inode;
177 return inode->i_op->getxattr(dentry, name, buf, len);
180 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
181 struct dentry *dentry, struct lustre_mdt_attrs *lma)
185 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA, (void *)lma,
188 /* try with old lma size */
189 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA,
190 info->oti_mdt_attrs_old,
193 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
196 /* Check LMA compatibility */
197 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
198 CWARN("%.16s: unsupported incompat LMA feature(s) "
200 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
201 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
205 lustre_lma_swab(lma);
208 } else if (rc == 0) {
216 * retrieve object from backend ext fs.
218 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
219 struct osd_inode_id *id)
221 struct inode *inode = NULL;
223 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
225 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
226 id->oii_ino, PTR_ERR(inode));
227 } else if (id->oii_gen != OSD_OII_NOGEN &&
228 inode->i_generation != id->oii_gen) {
229 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
231 id->oii_ino, id->oii_gen, inode->i_generation);
233 inode = ERR_PTR(-ESTALE);
234 } else if (inode->i_nlink == 0) {
235 /* due to parallel readdir and unlink,
236 * we can have dead inode here. */
237 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
238 make_bad_inode(inode);
240 inode = ERR_PTR(-ESTALE);
241 } else if (is_bad_inode(inode)) {
242 CWARN("%.16s: bad inode: ino = %u\n",
243 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
245 inode = ERR_PTR(-ENOENT);
247 if (id->oii_gen == OSD_OII_NOGEN)
248 osd_id_gen(id, inode->i_ino, inode->i_generation);
250 /* Do not update file c/mtime in ldiskfs.
251 * NB: we don't have any lock to protect this because we don't
252 * have reference on osd_object now, but contention with
253 * another lookup + attr_set can't happen in the tiny window
254 * between if (...) and set S_NOCMTIME. */
255 if (!(inode->i_flags & S_NOCMTIME))
256 inode->i_flags |= S_NOCMTIME;
261 static struct inode *
262 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
263 struct osd_inode_id *id, struct lu_fid *fid)
265 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
269 inode = osd_iget(info, dev, id);
273 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
275 *fid = lma->lma_self_fid;
276 } else if (rc == -ENODATA) {
277 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
278 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
280 lu_igif_build(fid, inode->i_ino, inode->i_generation);
288 static struct inode *
289 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
290 struct osd_inode_id *id, const struct lu_fid *fid)
292 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
296 inode = osd_iget(info, dev, id);
300 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
309 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
310 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
311 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
313 return ERR_PTR(-EREMCHG);
319 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
320 const struct lu_fid *fid,
321 const struct lu_object_conf *conf)
323 struct osd_thread_info *info;
324 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
325 struct osd_device *dev;
326 struct osd_idmap_cache *oic;
327 struct osd_inode_id *id;
329 struct osd_scrub *scrub;
330 struct scrub_file *sf;
335 LINVRNT(osd_invariant(obj));
336 LASSERT(obj->oo_inode == NULL);
337 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
340 scrub = &dev->od_scrub;
341 sf = &scrub->os_file;
342 info = osd_oti_get(env);
344 oic = &info->oti_cache;
346 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
349 /* Search order: 1. per-thread cache. */
350 if (lu_fid_eq(fid, &oic->oic_fid)) {
356 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
357 /* Search order: 2. OI scrub pending list. */
358 result = osd_oii_lookup(dev, fid, id);
363 if (sf->sf_flags & SF_INCONSISTENT)
367 * Objects are created as locking anchors or place holders for objects
368 * yet to be created. No need to osd_oi_lookup() at here because FID
369 * shouldn't never be re-used, if it's really a duplicate FID from
370 * unexpected reason, we should be able to detect it later by calling
371 * do_create->osd_oi_insert()
373 if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
374 GOTO(out, result = 0);
376 /* Search order: 3. OI files. */
377 result = osd_oi_lookup(info, dev, fid, id, true);
378 if (result == -ENOENT) {
379 if (!fid_is_norm(fid) ||
380 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
382 GOTO(out, result = 0);
392 inode = osd_iget(info, dev, id);
394 inode = osd_iget_verify(info, dev, id, fid);
396 result = PTR_ERR(inode);
397 if (result == -ENOENT || result == -ESTALE) {
398 fid_zero(&oic->oic_fid);
400 } else if (result == -EREMCHG) {
403 if (thread_is_running(&scrub->os_thread)) {
404 result = -EINPROGRESS;
405 } else if (!dev->od_noscrub) {
406 result = osd_scrub_start(dev);
407 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
408 "for "DFID", rc = %d [1]\n",
409 LDISKFS_SB(osd_sb(dev))->s_es->\
410 s_volume_name,PFID(fid), result);
411 if (result == 0 || result == -EALREADY)
412 result = -EINPROGRESS;
421 obj->oo_inode = inode;
422 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
424 obj->oo_compat_dot_created = 1;
425 obj->oo_compat_dotdot_created = 1;
427 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
428 GOTO(out, result = 0);
430 LASSERT(obj->oo_hl_head == NULL);
431 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
432 if (obj->oo_hl_head == NULL) {
433 obj->oo_inode = NULL;
435 GOTO(out, result = -ENOMEM);
437 GOTO(out, result = 0);
440 LINVRNT(osd_invariant(obj));
445 * Concurrency: shouldn't matter.
447 static void osd_object_init0(struct osd_object *obj)
449 LASSERT(obj->oo_inode != NULL);
450 obj->oo_dt.do_body_ops = &osd_body_ops;
451 obj->oo_dt.do_lu.lo_header->loh_attr |=
452 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
456 * Concurrency: no concurrent access is possible that early in object
459 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
460 const struct lu_object_conf *conf)
462 struct osd_object *obj = osd_obj(l);
465 LINVRNT(osd_invariant(obj));
467 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
468 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
469 l->lo_header->loh_attr |= LOHA_EXISTS;
473 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
474 obj->oo_dt.do_body_ops = &osd_body_ops_new;
475 if (result == 0 && obj->oo_inode != NULL)
476 osd_object_init0(obj);
478 LINVRNT(osd_invariant(obj));
483 * Concurrency: no concurrent access is possible that late in object
486 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
488 struct osd_object *obj = osd_obj(l);
490 LINVRNT(osd_invariant(obj));
492 dt_object_fini(&obj->oo_dt);
493 if (obj->oo_hl_head != NULL)
494 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
499 * Concurrency: no concurrent access is possible that late in object
502 static void osd_index_fini(struct osd_object *o)
504 struct iam_container *bag;
506 if (o->oo_dir != NULL) {
507 bag = &o->oo_dir->od_container;
508 if (o->oo_inode != NULL) {
509 if (bag->ic_object == o->oo_inode)
510 iam_container_fini(bag);
512 OBD_FREE_PTR(o->oo_dir);
518 * Concurrency: no concurrent access is possible that late in object
519 * life-cycle (for all existing callers, that is. New callers have to provide
520 * their own locking.)
522 static int osd_inode_unlinked(const struct inode *inode)
524 return inode->i_nlink == 0;
528 OSD_TXN_OI_DELETE_CREDITS = 20,
529 OSD_TXN_INODE_DELETE_CREDITS = 20
536 #if OSD_THANDLE_STATS
538 * Set time when the handle is allocated
540 static void osd_th_alloced(struct osd_thandle *oth)
542 oth->oth_alloced = cfs_time_current();
546 * Set time when the handle started
548 static void osd_th_started(struct osd_thandle *oth)
550 oth->oth_started = cfs_time_current();
554 * Helper function to convert time interval to microseconds packed in
557 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
561 cfs_duration_usec(cfs_time_sub(end, start), &val);
562 return val.tv_sec * 1000000 + val.tv_usec;
566 * Check whether the we deal with this handle for too long.
568 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
569 cfs_time_t alloced, cfs_time_t started,
572 cfs_time_t now = cfs_time_current();
574 LASSERT(dev != NULL);
576 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
577 interval_to_usec(alloced, started));
578 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
579 interval_to_usec(started, closed));
580 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
581 interval_to_usec(closed, now));
583 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
584 CWARN("transaction handle %p was open for too long: "
586 "alloced "CFS_TIME_T" ,"
587 "started "CFS_TIME_T" ,"
588 "closed "CFS_TIME_T"\n",
589 oth, now, alloced, started, closed);
590 libcfs_debug_dumpstack(NULL);
594 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
596 cfs_time_t __closed = cfs_time_current(); \
597 cfs_time_t __alloced = oth->oth_alloced; \
598 cfs_time_t __started = oth->oth_started; \
601 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
604 #else /* OSD_THANDLE_STATS */
606 #define osd_th_alloced(h) do {} while(0)
607 #define osd_th_started(h) do {} while(0)
608 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
610 #endif /* OSD_THANDLE_STATS */
613 * Concurrency: doesn't access mutable data.
615 static int osd_param_is_not_sane(const struct osd_device *dev,
616 const struct thandle *th)
618 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
620 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
624 * Concurrency: shouldn't matter.
626 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
627 static void osd_trans_commit_cb(struct super_block *sb,
628 struct journal_callback *jcb, int error)
630 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
633 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
634 struct thandle *th = &oh->ot_super;
635 struct lu_device *lud = &th->th_dev->dd_lu_dev;
636 struct dt_txn_commit_cb *dcb, *tmp;
638 LASSERT(oh->ot_handle == NULL);
641 CERROR("transaction @0x%p commit error: %d\n", th, error);
643 dt_txn_hook_commit(th);
645 /* call per-transaction callbacks if any */
646 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
647 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
648 "commit callback entry: magic=%x name='%s'\n",
649 dcb->dcb_magic, dcb->dcb_name);
650 cfs_list_del_init(&dcb->dcb_linkage);
651 dcb->dcb_func(NULL, th, dcb, error);
654 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
658 lu_context_exit(&th->th_ctx);
659 lu_context_fini(&th->th_ctx);
663 static struct thandle *osd_trans_create(const struct lu_env *env,
666 struct osd_thread_info *oti = osd_oti_get(env);
667 struct osd_iobuf *iobuf = &oti->oti_iobuf;
668 struct osd_thandle *oh;
672 /* on pending IO in this thread should left from prev. request */
673 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
675 th = ERR_PTR(-ENOMEM);
676 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
678 oh->ot_quota_trans = &oti->oti_quota_trans;
679 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
683 th->th_tags = LCT_TX_HANDLE;
685 oti->oti_dev = osd_dt_dev(d);
686 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
689 memset(oti->oti_declare_ops, 0, OSD_OT_MAX);
690 memset(oti->oti_declare_ops_rb, 0, OSD_OT_MAX);
691 memset(oti->oti_declare_ops_cred, 0, OSD_OT_MAX);
692 oti->oti_rollback = false;
698 * Concurrency: shouldn't matter.
700 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
703 struct osd_thread_info *oti = osd_oti_get(env);
704 struct osd_device *dev = osd_dt_dev(d);
706 struct osd_thandle *oh;
711 LASSERT(current->journal_info == NULL);
713 oh = container_of0(th, struct osd_thandle, ot_super);
715 LASSERT(oh->ot_handle == NULL);
717 rc = dt_txn_hook_start(env, d, th);
721 if (unlikely(osd_param_is_not_sane(dev, th))) {
722 static unsigned long last_printed;
723 static int last_credits;
725 CWARN("%.16s: too many transaction credits (%d > %d)\n",
726 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
728 osd_journal(dev)->j_max_transaction_buffers);
729 #ifdef OSD_TRACK_DECLARES
730 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
731 oti->oti_declare_ops[OSD_OT_CREATE],
732 oti->oti_declare_ops_cred[OSD_OT_CREATE],
733 oti->oti_declare_ops[OSD_OT_DELETE],
734 oti->oti_declare_ops_cred[OSD_OT_DELETE],
735 oti->oti_declare_ops[OSD_OT_DESTROY],
736 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
737 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
738 oti->oti_declare_ops[OSD_OT_ATTR_SET],
739 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
740 oti->oti_declare_ops[OSD_OT_XATTR_SET],
741 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
742 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
743 oti->oti_declare_ops[OSD_OT_WRITE],
744 oti->oti_declare_ops_cred[OSD_OT_WRITE],
745 oti->oti_declare_ops[OSD_OT_PUNCH],
746 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
747 oti->oti_declare_ops[OSD_OT_QUOTA],
748 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
749 CWARN(" insert: %u/%u, delete: %u/%u\n",
750 oti->oti_declare_ops[OSD_OT_INSERT],
751 oti->oti_declare_ops_cred[OSD_OT_INSERT],
752 oti->oti_declare_ops[OSD_OT_DESTROY],
753 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
754 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
755 oti->oti_declare_ops[OSD_OT_REF_ADD],
756 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
757 oti->oti_declare_ops[OSD_OT_REF_DEL],
758 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
760 if (last_credits != oh->ot_credits &&
761 time_after(jiffies, last_printed + 60 * HZ)) {
762 libcfs_debug_dumpstack(NULL);
763 last_credits = oh->ot_credits;
764 last_printed = jiffies;
767 /* XXX Limit the credits to 'max_transaction_buffers', and
768 * let the underlying filesystem to catch the error if
769 * we really need so many credits.
771 * This should be removed when we can calculate the
772 * credits precisely. */
773 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
777 * XXX temporary stuff. Some abstraction layer should
780 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
784 LASSERT(oti->oti_txns == 0);
785 lu_context_init(&th->th_ctx, th->th_tags);
786 lu_context_enter(&th->th_ctx);
788 lu_device_get(&d->dd_lu_dev);
789 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
801 * Concurrency: shouldn't matter.
803 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
806 struct osd_thandle *oh;
807 struct osd_thread_info *oti = osd_oti_get(env);
808 struct osd_iobuf *iobuf = &oti->oti_iobuf;
809 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
812 oh = container_of0(th, struct osd_thandle, ot_super);
815 /* inform the quota slave device that the transaction is
817 qsd_op_end(env, qsd, oh->ot_quota_trans);
818 oh->ot_quota_trans = NULL;
820 if (oh->ot_handle != NULL) {
821 handle_t *hdl = oh->ot_handle;
824 * add commit callback
825 * notice we don't do this in osd_trans_start()
826 * as underlying transaction can change during truncate
828 osd_journal_callback_set(hdl, osd_trans_commit_cb,
831 LASSERT(oti->oti_txns == 1);
833 rc = dt_txn_hook_stop(env, th);
835 CERROR("Failure in transaction hook: %d\n", rc);
837 /* hook functions might modify th_sync */
838 hdl->h_sync = th->th_sync;
840 oh->ot_handle = NULL;
841 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
842 rc = ldiskfs_journal_stop(hdl));
844 CERROR("Failure to stop transaction: %d\n", rc);
849 /* as we want IO to journal and data IO be concurrent, we don't block
850 * awaiting data IO completion in osd_do_bio(), instead we wait here
851 * once transaction is submitted to the journal. all reqular requests
852 * don't do direct IO (except read/write), thus this wait_event becomes
855 * IMPORTANT: we have to wait till any IO submited by the thread is
856 * completed otherwise iobuf may be corrupted by different request
858 cfs_wait_event(iobuf->dr_wait,
859 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
861 rc = iobuf->dr_error;
866 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
868 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
871 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
872 LASSERT(&dcb->dcb_func != NULL);
873 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
879 * Called just before object is freed. Releases all resources except for
880 * object itself (that is released by osd_object_free()).
882 * Concurrency: no concurrent access is possible that late in object
885 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
887 struct osd_object *obj = osd_obj(l);
888 struct inode *inode = obj->oo_inode;
890 LINVRNT(osd_invariant(obj));
893 * If object is unlinked remove fid->ino mapping from object index.
898 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
899 qid_t uid = inode->i_uid;
900 qid_t gid = inode->i_gid;
903 obj->oo_inode = NULL;
906 struct osd_thread_info *info = osd_oti_get(env);
907 struct lquota_id_info *qi = &info->oti_qi;
909 /* Release granted quota to master if necessary */
910 qi->lqi_id.qid_uid = uid;
911 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
913 qi->lqi_id.qid_uid = gid;
914 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
920 * Concurrency: ->loo_object_release() is called under site spin-lock.
922 static void osd_object_release(const struct lu_env *env,
928 * Concurrency: shouldn't matter.
930 static int osd_object_print(const struct lu_env *env, void *cookie,
931 lu_printer_t p, const struct lu_object *l)
933 struct osd_object *o = osd_obj(l);
936 if (o->oo_dir != NULL)
937 d = o->oo_dir->od_container.ic_descr;
940 return (*p)(env, cookie,
941 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
943 o->oo_inode ? o->oo_inode->i_ino : 0UL,
944 o->oo_inode ? o->oo_inode->i_generation : 0,
945 d ? d->id_ops->id_name : "plain");
949 * Concurrency: shouldn't matter.
951 int osd_statfs(const struct lu_env *env, struct dt_device *d,
952 struct obd_statfs *sfs)
954 struct osd_device *osd = osd_dt_dev(d);
955 struct super_block *sb = osd_sb(osd);
956 struct kstatfs *ksfs;
959 if (unlikely(osd->od_mnt == NULL))
962 /* osd_lproc.c call this without env, allocate ksfs for that case */
963 if (unlikely(env == NULL)) {
968 ksfs = &osd_oti_get(env)->oti_ksfs;
971 spin_lock(&osd->od_osfs_lock);
973 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
974 result = sb->s_op->statfs(sb->s_root, ksfs);
975 if (likely(result == 0)) { /* N.B. statfs can't really fail */
976 osd->od_osfs_age = cfs_time_current_64();
977 statfs_pack(&osd->od_statfs, ksfs);
978 if (sb->s_flags & MS_RDONLY)
979 sfs->os_state = OS_STATE_READONLY;
983 if (likely(result == 0))
984 *sfs = osd->od_statfs;
985 spin_unlock(&osd->od_osfs_lock);
987 if (unlikely(env == NULL))
994 * Estimate space needed for file creations. We assume the largest filename
995 * which is 2^64 - 1, hence a filename of 20 chars.
996 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
998 #ifdef __LDISKFS_DIR_REC_LEN
999 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1001 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1005 * Concurrency: doesn't access mutable data.
1007 static void osd_conf_get(const struct lu_env *env,
1008 const struct dt_device *dev,
1009 struct dt_device_param *param)
1011 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1014 * XXX should be taken from not-yet-existing fs abstraction layer.
1016 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1017 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1018 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1019 param->ddp_block_shift = sb->s_blocksize_bits;
1020 param->ddp_mount_type = LDD_MT_LDISKFS;
1021 param->ddp_maxbytes = sb->s_maxbytes;
1022 /* Overhead estimate should be fairly accurate, so we really take a tiny
1023 * error margin which also avoids fragmenting the filesystem too much */
1024 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1025 /* inode are statically allocated, so per-inode space consumption
1026 * is the space consumed by the directory entry */
1027 param->ddp_inodespace = PER_OBJ_USAGE;
1028 /* per-fragment overhead to be used by the client code */
1029 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1030 param->ddp_mntopts = 0;
1031 if (test_opt(sb, XATTR_USER))
1032 param->ddp_mntopts |= MNTOPT_USERXATTR;
1033 if (test_opt(sb, POSIX_ACL))
1034 param->ddp_mntopts |= MNTOPT_ACL;
1036 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1037 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1038 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1041 param->ddp_max_ea_size = sb->s_blocksize;
1046 * Concurrency: shouldn't matter.
1048 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1050 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1051 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1055 * Start commit for OSD device.
1057 * An implementation of dt_commit_async method for OSD device.
1058 * Asychronously starts underlayng fs sync and thereby a transaction
1061 * \param env environment
1062 * \param d dt device
1064 * \see dt_device_operations
1066 static int osd_commit_async(const struct lu_env *env,
1067 struct dt_device *d)
1069 struct super_block *s = osd_sb(osd_dt_dev(d));
1072 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1073 RETURN(s->s_op->sync_fs(s, 0));
1077 * Concurrency: shouldn't matter.
1080 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1082 struct super_block *sb = osd_sb(osd_dt_dev(d));
1086 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1088 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1093 * Concurrency: serialization provided by callers.
1095 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1096 int mode, unsigned long timeout, __u32 alg,
1097 struct lustre_capa_key *keys)
1099 struct osd_device *dev = osd_dt_dev(d);
1102 dev->od_fl_capa = mode;
1103 dev->od_capa_timeout = timeout;
1104 dev->od_capa_alg = alg;
1105 dev->od_capa_keys = keys;
1110 * Note: we do not count into QUOTA here.
1111 * If we mount with --data_journal we may need more.
1113 const int osd_dto_credits_noquota[DTO_NR] = {
1116 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1117 * SINGLEDATA_TRANS_BLOCKS(8)
1118 * XXX Note: maybe iam need more, since iam have more level than
1121 [DTO_INDEX_INSERT] = 16,
1122 [DTO_INDEX_DELETE] = 16,
1126 [DTO_INDEX_UPDATE] = 16,
1128 * Create a object. The same as create object in EXT3.
1129 * DATA_TRANS_BLOCKS(14) +
1130 * INDEX_EXTRA_BLOCKS(8) +
1131 * 3(inode bits, groups, GDT)
1133 [DTO_OBJECT_CREATE] = 25,
1135 * XXX: real credits to be fixed
1137 [DTO_OBJECT_DELETE] = 25,
1139 * Attr set credits (inode)
1141 [DTO_ATTR_SET_BASE] = 1,
1143 * Xattr set. The same as xattr of EXT3.
1144 * DATA_TRANS_BLOCKS(14)
1145 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1146 * are also counted in. Do not know why?
1148 [DTO_XATTR_SET] = 14,
1151 * credits for inode change during write.
1153 [DTO_WRITE_BASE] = 3,
1155 * credits for single block write.
1157 [DTO_WRITE_BLOCK] = 14,
1159 * Attr set credits for chown.
1160 * This is extra credits for setattr, and it is null without quota
1162 [DTO_ATTR_SET_CHOWN]= 0
1165 static const struct dt_device_operations osd_dt_ops = {
1166 .dt_root_get = osd_root_get,
1167 .dt_statfs = osd_statfs,
1168 .dt_trans_create = osd_trans_create,
1169 .dt_trans_start = osd_trans_start,
1170 .dt_trans_stop = osd_trans_stop,
1171 .dt_trans_cb_add = osd_trans_cb_add,
1172 .dt_conf_get = osd_conf_get,
1173 .dt_sync = osd_sync,
1175 .dt_commit_async = osd_commit_async,
1176 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1179 static void osd_object_read_lock(const struct lu_env *env,
1180 struct dt_object *dt, unsigned role)
1182 struct osd_object *obj = osd_dt_obj(dt);
1183 struct osd_thread_info *oti = osd_oti_get(env);
1185 LINVRNT(osd_invariant(obj));
1187 LASSERT(obj->oo_owner != env);
1188 down_read_nested(&obj->oo_sem, role);
1190 LASSERT(obj->oo_owner == NULL);
1194 static void osd_object_write_lock(const struct lu_env *env,
1195 struct dt_object *dt, unsigned role)
1197 struct osd_object *obj = osd_dt_obj(dt);
1198 struct osd_thread_info *oti = osd_oti_get(env);
1200 LINVRNT(osd_invariant(obj));
1202 LASSERT(obj->oo_owner != env);
1203 down_write_nested(&obj->oo_sem, role);
1205 LASSERT(obj->oo_owner == NULL);
1206 obj->oo_owner = env;
1210 static void osd_object_read_unlock(const struct lu_env *env,
1211 struct dt_object *dt)
1213 struct osd_object *obj = osd_dt_obj(dt);
1214 struct osd_thread_info *oti = osd_oti_get(env);
1216 LINVRNT(osd_invariant(obj));
1218 LASSERT(oti->oti_r_locks > 0);
1220 up_read(&obj->oo_sem);
1223 static void osd_object_write_unlock(const struct lu_env *env,
1224 struct dt_object *dt)
1226 struct osd_object *obj = osd_dt_obj(dt);
1227 struct osd_thread_info *oti = osd_oti_get(env);
1229 LINVRNT(osd_invariant(obj));
1231 LASSERT(obj->oo_owner == env);
1232 LASSERT(oti->oti_w_locks > 0);
1234 obj->oo_owner = NULL;
1235 up_write(&obj->oo_sem);
1238 static int osd_object_write_locked(const struct lu_env *env,
1239 struct dt_object *dt)
1241 struct osd_object *obj = osd_dt_obj(dt);
1243 LINVRNT(osd_invariant(obj));
1245 return obj->oo_owner == env;
1248 static int capa_is_sane(const struct lu_env *env,
1249 struct osd_device *dev,
1250 struct lustre_capa *capa,
1251 struct lustre_capa_key *keys)
1253 struct osd_thread_info *oti = osd_oti_get(env);
1254 struct lustre_capa *tcapa = &oti->oti_capa;
1255 struct obd_capa *oc;
1259 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1261 if (capa_is_expired(oc)) {
1262 DEBUG_CAPA(D_ERROR, capa, "expired");
1269 if (capa_is_expired_sec(capa)) {
1270 DEBUG_CAPA(D_ERROR, capa, "expired");
1274 spin_lock(&capa_lock);
1275 for (i = 0; i < 2; i++) {
1276 if (keys[i].lk_keyid == capa->lc_keyid) {
1277 oti->oti_capa_key = keys[i];
1281 spin_unlock(&capa_lock);
1284 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1288 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1292 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1293 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1297 oc = capa_add(dev->od_capa_hash, capa);
1303 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1304 struct lustre_capa *capa, __u64 opc)
1306 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1307 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1308 struct md_capainfo *ci;
1311 if (!dev->od_fl_capa)
1314 if (capa == BYPASS_CAPA)
1317 ci = md_capainfo(env);
1321 if (ci->mc_auth == LC_ID_NONE)
1325 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1329 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1330 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1335 if (!capa_opc_supported(capa, opc)) {
1336 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1340 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1341 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1348 static struct timespec *osd_inode_time(const struct lu_env *env,
1349 struct inode *inode, __u64 seconds)
1351 struct osd_thread_info *oti = osd_oti_get(env);
1352 struct timespec *t = &oti->oti_time;
1354 t->tv_sec = seconds;
1356 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1361 static void osd_inode_getattr(const struct lu_env *env,
1362 struct inode *inode, struct lu_attr *attr)
1364 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1365 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1366 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1368 attr->la_atime = LTIME_S(inode->i_atime);
1369 attr->la_mtime = LTIME_S(inode->i_mtime);
1370 attr->la_ctime = LTIME_S(inode->i_ctime);
1371 attr->la_mode = inode->i_mode;
1372 attr->la_size = i_size_read(inode);
1373 attr->la_blocks = inode->i_blocks;
1374 attr->la_uid = inode->i_uid;
1375 attr->la_gid = inode->i_gid;
1376 attr->la_flags = LDISKFS_I(inode)->i_flags;
1377 attr->la_nlink = inode->i_nlink;
1378 attr->la_rdev = inode->i_rdev;
1379 attr->la_blksize = 1 << inode->i_blkbits;
1380 attr->la_blkbits = inode->i_blkbits;
1383 static int osd_attr_get(const struct lu_env *env,
1384 struct dt_object *dt,
1385 struct lu_attr *attr,
1386 struct lustre_capa *capa)
1388 struct osd_object *obj = osd_dt_obj(dt);
1390 LASSERT(dt_object_exists(dt));
1391 LINVRNT(osd_invariant(obj));
1393 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1396 spin_lock(&obj->oo_guard);
1397 osd_inode_getattr(env, obj->oo_inode, attr);
1398 spin_unlock(&obj->oo_guard);
1402 static int osd_declare_attr_set(const struct lu_env *env,
1403 struct dt_object *dt,
1404 const struct lu_attr *attr,
1405 struct thandle *handle)
1407 struct osd_thandle *oh;
1408 struct osd_object *obj;
1409 struct osd_thread_info *info = osd_oti_get(env);
1410 struct lquota_id_info *qi = &info->oti_qi;
1416 LASSERT(dt != NULL);
1417 LASSERT(handle != NULL);
1419 obj = osd_dt_obj(dt);
1420 LASSERT(osd_invariant(obj));
1422 oh = container_of0(handle, struct osd_thandle, ot_super);
1423 LASSERT(oh->ot_handle == NULL);
1425 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1426 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1428 if (attr == NULL || obj->oo_inode == NULL)
1431 bspace = obj->oo_inode->i_blocks;
1432 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1433 bspace = toqb(bspace);
1435 /* Changing ownership is always preformed by super user, it should not
1438 * We still need to call the osd_declare_qid() to calculate the journal
1439 * credits for updating quota accounting files and to trigger quota
1440 * space adjustment once the operation is completed.*/
1441 if ((attr->la_valid & LA_UID) != 0 &&
1442 attr->la_uid != obj->oo_inode->i_uid) {
1443 qi->lqi_type = USRQUOTA;
1445 /* inode accounting */
1446 qi->lqi_is_blk = false;
1448 /* one more inode for the new owner ... */
1449 qi->lqi_id.qid_uid = attr->la_uid;
1451 allocated = (attr->la_uid == 0) ? true : false;
1452 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1453 if (rc == -EDQUOT || rc == -EINPROGRESS)
1458 /* and one less inode for the current uid */
1459 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1461 rc = osd_declare_qid(env, oh, qi, true, NULL);
1462 if (rc == -EDQUOT || rc == -EINPROGRESS)
1467 /* block accounting */
1468 qi->lqi_is_blk = true;
1470 /* more blocks for the new owner ... */
1471 qi->lqi_id.qid_uid = attr->la_uid;
1472 qi->lqi_space = bspace;
1473 allocated = (attr->la_uid == 0) ? true : false;
1474 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1475 if (rc == -EDQUOT || rc == -EINPROGRESS)
1480 /* and finally less blocks for the current owner */
1481 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1482 qi->lqi_space = -bspace;
1483 rc = osd_declare_qid(env, oh, qi, true, NULL);
1484 if (rc == -EDQUOT || rc == -EINPROGRESS)
1490 if (attr->la_valid & LA_GID &&
1491 attr->la_gid != obj->oo_inode->i_gid) {
1492 qi->lqi_type = GRPQUOTA;
1494 /* inode accounting */
1495 qi->lqi_is_blk = false;
1497 /* one more inode for the new group owner ... */
1498 qi->lqi_id.qid_gid = attr->la_gid;
1500 allocated = (attr->la_gid == 0) ? true : false;
1501 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1502 if (rc == -EDQUOT || rc == -EINPROGRESS)
1507 /* and one less inode for the current gid */
1508 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1510 rc = osd_declare_qid(env, oh, qi, true, NULL);
1511 if (rc == -EDQUOT || rc == -EINPROGRESS)
1516 /* block accounting */
1517 qi->lqi_is_blk = true;
1519 /* more blocks for the new owner ... */
1520 qi->lqi_id.qid_gid = attr->la_gid;
1521 qi->lqi_space = bspace;
1522 allocated = (attr->la_gid == 0) ? true : false;
1523 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1524 if (rc == -EDQUOT || rc == -EINPROGRESS)
1529 /* and finally less blocks for the current owner */
1530 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1531 qi->lqi_space = -bspace;
1532 rc = osd_declare_qid(env, oh, qi, true, NULL);
1533 if (rc == -EDQUOT || rc == -EINPROGRESS)
1542 static int osd_inode_setattr(const struct lu_env *env,
1543 struct inode *inode, const struct lu_attr *attr)
1547 bits = attr->la_valid;
1549 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1551 if (bits & LA_ATIME)
1552 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1553 if (bits & LA_CTIME)
1554 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1555 if (bits & LA_MTIME)
1556 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1557 if (bits & LA_SIZE) {
1558 LDISKFS_I(inode)->i_disksize = attr->la_size;
1559 i_size_write(inode, attr->la_size);
1563 /* OSD should not change "i_blocks" which is used by quota.
1564 * "i_blocks" should be changed by ldiskfs only. */
1565 if (bits & LA_BLOCKS)
1566 inode->i_blocks = attr->la_blocks;
1569 inode->i_mode = (inode->i_mode & S_IFMT) |
1570 (attr->la_mode & ~S_IFMT);
1572 inode->i_uid = attr->la_uid;
1574 inode->i_gid = attr->la_gid;
1575 if (bits & LA_NLINK)
1576 set_nlink(inode, attr->la_nlink);
1578 inode->i_rdev = attr->la_rdev;
1580 if (bits & LA_FLAGS) {
1581 /* always keep S_NOCMTIME */
1582 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1588 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1590 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1591 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1596 if (attr->la_valid & LA_UID)
1597 iattr.ia_valid |= ATTR_UID;
1598 if (attr->la_valid & LA_GID)
1599 iattr.ia_valid |= ATTR_GID;
1600 iattr.ia_uid = attr->la_uid;
1601 iattr.ia_gid = attr->la_gid;
1603 rc = ll_vfs_dq_transfer(inode, &iattr);
1605 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1606 "enforcement enabled on the ldiskfs filesystem?",
1607 inode->i_sb->s_id, rc);
1614 static int osd_attr_set(const struct lu_env *env,
1615 struct dt_object *dt,
1616 const struct lu_attr *attr,
1617 struct thandle *handle,
1618 struct lustre_capa *capa)
1620 struct osd_object *obj = osd_dt_obj(dt);
1621 struct inode *inode;
1624 LASSERT(handle != NULL);
1625 LASSERT(dt_object_exists(dt));
1626 LASSERT(osd_invariant(obj));
1628 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1631 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1633 inode = obj->oo_inode;
1634 ll_vfs_dq_init(inode);
1636 rc = osd_quota_transfer(inode, attr);
1640 spin_lock(&obj->oo_guard);
1641 rc = osd_inode_setattr(env, inode, attr);
1642 spin_unlock(&obj->oo_guard);
1645 inode->i_sb->s_op->dirty_inode(inode);
1649 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1650 struct osd_object *obj,
1651 const char *name, const int namelen)
1653 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1656 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1658 struct dt_allocation_hint *hint,
1662 struct osd_device *osd = osd_obj2dev(obj);
1663 struct osd_thandle *oth;
1664 struct dt_object *parent = NULL;
1665 struct inode *inode;
1667 LINVRNT(osd_invariant(obj));
1668 LASSERT(obj->oo_inode == NULL);
1669 LASSERT(obj->oo_hl_head == NULL);
1671 if (S_ISDIR(mode) && ldiskfs_pdo) {
1672 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1673 if (obj->oo_hl_head == NULL)
1677 oth = container_of(th, struct osd_thandle, ot_super);
1678 LASSERT(oth->ot_handle->h_transaction != NULL);
1680 if (hint && hint->dah_parent)
1681 parent = hint->dah_parent;
1683 inode = ldiskfs_create_inode(oth->ot_handle,
1684 parent ? osd_dt_obj(parent)->oo_inode :
1685 osd_sb(osd)->s_root->d_inode,
1687 if (!IS_ERR(inode)) {
1688 /* Do not update file c/mtime in ldiskfs.
1689 * NB: don't need any lock because no contention at this
1691 inode->i_flags |= S_NOCMTIME;
1693 /* For new created object, it must be consistent,
1694 * and it is unnecessary to scrub against it. */
1695 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1696 obj->oo_inode = inode;
1699 if (obj->oo_hl_head != NULL) {
1700 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1701 obj->oo_hl_head = NULL;
1703 result = PTR_ERR(inode);
1705 LINVRNT(osd_invariant(obj));
1713 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1714 struct lu_attr *attr,
1715 struct dt_allocation_hint *hint,
1716 struct dt_object_format *dof,
1720 struct osd_thandle *oth;
1721 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1723 LASSERT(S_ISDIR(attr->la_mode));
1725 oth = container_of(th, struct osd_thandle, ot_super);
1726 LASSERT(oth->ot_handle->h_transaction != NULL);
1727 result = osd_mkfile(info, obj, mode, hint, th);
1732 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1733 struct lu_attr *attr,
1734 struct dt_allocation_hint *hint,
1735 struct dt_object_format *dof,
1739 struct osd_thandle *oth;
1740 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1742 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1744 LASSERT(S_ISREG(attr->la_mode));
1746 oth = container_of(th, struct osd_thandle, ot_super);
1747 LASSERT(oth->ot_handle->h_transaction != NULL);
1749 result = osd_mkfile(info, obj, mode, hint, th);
1751 LASSERT(obj->oo_inode != NULL);
1752 if (feat->dif_flags & DT_IND_VARKEY)
1753 result = iam_lvar_create(obj->oo_inode,
1754 feat->dif_keysize_max,
1756 feat->dif_recsize_max,
1759 result = iam_lfix_create(obj->oo_inode,
1760 feat->dif_keysize_max,
1762 feat->dif_recsize_max,
1769 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1770 struct lu_attr *attr,
1771 struct dt_allocation_hint *hint,
1772 struct dt_object_format *dof,
1775 LASSERT(S_ISREG(attr->la_mode));
1776 return osd_mkfile(info, obj, (attr->la_mode &
1777 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1780 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1781 struct lu_attr *attr,
1782 struct dt_allocation_hint *hint,
1783 struct dt_object_format *dof,
1786 LASSERT(S_ISLNK(attr->la_mode));
1787 return osd_mkfile(info, obj, (attr->la_mode &
1788 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1791 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1792 struct lu_attr *attr,
1793 struct dt_allocation_hint *hint,
1794 struct dt_object_format *dof,
1797 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1800 LINVRNT(osd_invariant(obj));
1801 LASSERT(obj->oo_inode == NULL);
1802 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1803 S_ISFIFO(mode) || S_ISSOCK(mode));
1805 result = osd_mkfile(info, obj, mode, hint, th);
1807 LASSERT(obj->oo_inode != NULL);
1809 * This inode should be marked dirty for i_rdev. Currently
1810 * that is done in the osd_attr_init().
1812 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1815 LINVRNT(osd_invariant(obj));
1819 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1821 struct dt_allocation_hint *hint,
1822 struct dt_object_format *dof,
1825 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1827 osd_obj_type_f result;
1843 result = osd_mk_index;
1854 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1855 struct dt_object *parent, struct dt_object *child,
1856 cfs_umode_t child_mode)
1860 memset(ah, 0, sizeof(*ah));
1861 ah->dah_parent = parent;
1862 ah->dah_mode = child_mode;
1865 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1866 struct lu_attr *attr, struct dt_object_format *dof)
1868 struct inode *inode = obj->oo_inode;
1869 __u64 valid = attr->la_valid;
1872 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1874 if (dof->dof_type != DFT_NODE)
1875 attr->la_valid &= ~LA_RDEV;
1876 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1877 attr->la_valid &= ~LA_ATIME;
1878 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1879 attr->la_valid &= ~LA_CTIME;
1880 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1881 attr->la_valid &= ~LA_MTIME;
1883 result = osd_quota_transfer(inode, attr);
1887 if (attr->la_valid != 0) {
1888 result = osd_inode_setattr(info->oti_env, inode, attr);
1890 * The osd_inode_setattr() should always succeed here. The
1891 * only error that could be returned is EDQUOT when we are
1892 * trying to change the UID or GID of the inode. However, this
1893 * should not happen since quota enforcement is no longer
1894 * enabled on ldiskfs (lquota takes care of it).
1896 LASSERTF(result == 0, "%d", result);
1897 inode->i_sb->s_op->dirty_inode(inode);
1900 attr->la_valid = valid;
1904 * Helper function for osd_object_create()
1906 * \retval 0, on success
1908 static int __osd_object_create(struct osd_thread_info *info,
1909 struct osd_object *obj, struct lu_attr *attr,
1910 struct dt_allocation_hint *hint,
1911 struct dt_object_format *dof,
1916 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1919 osd_attr_init(info, obj, attr, dof);
1920 osd_object_init0(obj);
1922 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1923 unlock_new_inode(obj->oo_inode);
1930 * Helper function for osd_object_create()
1932 * \retval 0, on success
1934 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1935 const struct lu_fid *fid, struct thandle *th)
1937 struct osd_thread_info *info = osd_oti_get(env);
1938 struct osd_inode_id *id = &info->oti_id;
1939 struct osd_device *osd = osd_obj2dev(obj);
1941 LASSERT(obj->oo_inode != NULL);
1943 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1944 return osd_oi_insert(info, osd, fid, id, th);
1947 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
1948 const struct lu_fid *fid, struct lu_seq_range *range)
1950 struct seq_server_site *ss = osd_seq_site(osd);
1953 if (fid_is_igif(fid)) {
1954 range->lsr_flags = LU_SEQ_RANGE_MDT;
1955 range->lsr_index = 0;
1959 if (fid_is_idif(fid)) {
1960 range->lsr_flags = LU_SEQ_RANGE_OST;
1961 range->lsr_index = fid_idif_ost_idx(fid);
1965 if (!fid_is_norm(fid)) {
1966 range->lsr_flags = LU_SEQ_RANGE_MDT;
1968 /* FIXME: If ss is NULL, it suppose not get lsr_index
1970 range->lsr_index = ss->ss_node_id;
1974 LASSERT(ss != NULL);
1975 range->lsr_flags = -1;
1976 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1978 CERROR("%s can not find "DFID": rc = %d\n",
1979 osd2lu_dev(osd)->ld_obd->obd_name, PFID(fid), rc);
1985 static int osd_declare_object_create(const struct lu_env *env,
1986 struct dt_object *dt,
1987 struct lu_attr *attr,
1988 struct dt_allocation_hint *hint,
1989 struct dt_object_format *dof,
1990 struct thandle *handle)
1992 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
1993 struct osd_thandle *oh;
1997 LASSERT(handle != NULL);
1999 oh = container_of0(handle, struct osd_thandle, ot_super);
2000 LASSERT(oh->ot_handle == NULL);
2002 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2003 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2004 /* XXX: So far, only normal fid needs be inserted into the oi,
2005 * things could be changed later. Revise following code then. */
2006 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2007 !fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2008 lu_object_fid(&dt->do_lu))) {
2009 /* Reuse idle OI block may cause additional one OI block
2011 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2012 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2014 /* If this is directory, then we expect . and .. to be inserted as
2015 * well. The one directory block always needs to be created for the
2016 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2017 * block), there is no danger of needing a tree for the first block.
2019 if (attr && S_ISDIR(attr->la_mode)) {
2020 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2021 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2022 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2028 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2029 false, false, NULL, false);
2033 /* It does fld look up inside declare, and the result will be
2034 * added to fld cache, so the following fld lookup inside insert
2035 * does not need send RPC anymore, so avoid send rpc with holding
2037 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2038 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2039 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2040 lu_object_fid(&dt->do_lu), range);
2046 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2047 struct lu_attr *attr,
2048 struct dt_allocation_hint *hint,
2049 struct dt_object_format *dof,
2052 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2053 struct osd_object *obj = osd_dt_obj(dt);
2054 struct osd_thread_info *info = osd_oti_get(env);
2059 LINVRNT(osd_invariant(obj));
2060 LASSERT(!dt_object_exists(dt));
2061 LASSERT(osd_write_locked(env, obj));
2062 LASSERT(th != NULL);
2064 if (unlikely(fid_is_acct(fid)))
2065 /* Quota files can't be created from the kernel any more,
2066 * 'tune2fs -O quota' will take care of creating them */
2069 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2070 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2072 result = __osd_object_create(info, obj, attr, hint, dof, th);
2074 result = __osd_oi_insert(env, obj, fid, th);
2076 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2077 LASSERT(osd_invariant(obj));
2082 * Called to destroy on-disk representation of the object
2084 * Concurrency: must be locked
2086 static int osd_declare_object_destroy(const struct lu_env *env,
2087 struct dt_object *dt,
2090 struct osd_object *obj = osd_dt_obj(dt);
2091 struct inode *inode = obj->oo_inode;
2092 struct osd_thandle *oh;
2096 oh = container_of0(th, struct osd_thandle, ot_super);
2097 LASSERT(oh->ot_handle == NULL);
2100 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2101 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2102 /* XXX: So far, only normal fid needs to be inserted into the OI,
2103 * so only normal fid needs to be removed from the OI also.
2104 * Recycle idle OI leaf may cause additional three OI blocks
2106 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2107 fid_is_norm(lu_object_fid(&dt->do_lu)) ?
2108 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3 : 0);
2110 /* one less inode */
2111 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2112 false, true, NULL, false);
2115 /* data to be truncated */
2116 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2117 true, true, NULL, false);
2121 static int osd_object_destroy(const struct lu_env *env,
2122 struct dt_object *dt,
2125 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2126 struct osd_object *obj = osd_dt_obj(dt);
2127 struct inode *inode = obj->oo_inode;
2128 struct osd_device *osd = osd_obj2dev(obj);
2129 struct osd_thandle *oh;
2133 oh = container_of0(th, struct osd_thandle, ot_super);
2134 LASSERT(oh->ot_handle);
2136 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2138 if (unlikely(fid_is_acct(fid)))
2141 /* Parallel control for OI scrub. For most of cases, there is no
2142 * lock contention. So it will not affect unlink performance. */
2143 mutex_lock(&inode->i_mutex);
2144 if (S_ISDIR(inode->i_mode)) {
2145 LASSERT(osd_inode_unlinked(inode) ||
2146 inode->i_nlink == 1);
2147 spin_lock(&obj->oo_guard);
2149 spin_unlock(&obj->oo_guard);
2150 inode->i_sb->s_op->dirty_inode(inode);
2153 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2155 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2156 mutex_unlock(&inode->i_mutex);
2158 /* XXX: add to ext3 orphan list */
2159 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2161 /* not needed in the cache anymore */
2162 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2167 static inline int __osd_xattr_set(struct osd_thread_info *info,
2168 struct inode *inode, const char *name,
2169 const void *buf, int buflen, int fl)
2171 struct dentry *dentry = &info->oti_child_dentry;
2173 ll_vfs_dq_init(inode);
2174 dentry->d_inode = inode;
2175 return inode->i_op->setxattr(dentry, name, buf, buflen, fl);
2179 * Put the fid into lustre_mdt_attrs, and then place the structure
2180 * inode's ea. This fid should not be altered during the life time
2183 * \retval +ve, on success
2184 * \retval -ve, on error
2186 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2188 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2189 const struct lu_fid *fid)
2191 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2194 lustre_lma_init(lma, fid);
2195 lustre_lma_swab(lma);
2197 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2199 /* Someone may created the EA by race. */
2200 if (unlikely(rc == -EEXIST))
2206 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2207 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2208 * To have compatilibility with 1.8 ldiskfs driver we need to have
2209 * magic number at start of fid data.
2210 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2213 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2214 const struct dt_rec *fid)
2216 /* XXX: replace the check with "!fid_is_client_mdt_visible()"
2217 * when FID in OI file introduced for local object. */
2218 if (!fid_is_norm((const struct lu_fid *)fid) &&
2219 !fid_is_igif((const struct lu_fid *)fid)) {
2220 param->edp_magic = 0;
2224 param->edp_magic = LDISKFS_LUFID_MAGIC;
2225 param->edp_len = sizeof(struct lu_fid) + 1;
2226 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2230 * Try to read the fid from inode ea into dt_rec, if return value
2231 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2233 * \param fid object fid.
2235 * \retval 0 on success
2237 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2238 __u32 ino, struct lu_fid *fid,
2239 struct osd_inode_id *id)
2241 struct osd_thread_info *info = osd_oti_get(env);
2242 struct inode *inode;
2245 osd_id_gen(id, ino, OSD_OII_NOGEN);
2246 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2248 RETURN(PTR_ERR(inode));
2255 * OSD layer object create function for interoperability mode (b11826).
2256 * This is mostly similar to osd_object_create(). Only difference being, fid is
2257 * inserted into inode ea here.
2259 * \retval 0, on success
2260 * \retval -ve, on error
2262 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2263 struct lu_attr *attr,
2264 struct dt_allocation_hint *hint,
2265 struct dt_object_format *dof,
2268 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2269 struct osd_object *obj = osd_dt_obj(dt);
2270 struct osd_thread_info *info = osd_oti_get(env);
2275 LASSERT(osd_invariant(obj));
2276 LASSERT(!dt_object_exists(dt));
2277 LASSERT(osd_write_locked(env, obj));
2278 LASSERT(th != NULL);
2280 if (unlikely(fid_is_acct(fid)))
2281 /* Quota files can't be created from the kernel any more,
2282 * 'tune2fs -O quota' will take care of creating them */
2285 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2286 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2288 result = __osd_object_create(info, obj, attr, hint, dof, th);
2289 /* objects under osd root shld have igif fid, so dont add fid EA */
2290 /* For ost object, the fid will be stored during first write */
2291 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL &&
2292 !fid_is_on_ost(info, osd_dt_dev(th->th_dev), fid))
2293 result = osd_ea_fid_set(info, obj->oo_inode, fid);
2296 result = __osd_oi_insert(env, obj, fid, th);
2298 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2299 LINVRNT(osd_invariant(obj));
2303 static int osd_declare_object_ref_add(const struct lu_env *env,
2304 struct dt_object *dt,
2305 struct thandle *handle)
2307 struct osd_thandle *oh;
2309 /* it's possible that object doesn't exist yet */
2310 LASSERT(handle != NULL);
2312 oh = container_of0(handle, struct osd_thandle, ot_super);
2313 LASSERT(oh->ot_handle == NULL);
2315 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2316 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2322 * Concurrency: @dt is write locked.
2324 static int osd_object_ref_add(const struct lu_env *env,
2325 struct dt_object *dt, struct thandle *th)
2327 struct osd_object *obj = osd_dt_obj(dt);
2328 struct inode *inode = obj->oo_inode;
2330 LINVRNT(osd_invariant(obj));
2331 LASSERT(dt_object_exists(dt));
2332 LASSERT(osd_write_locked(env, obj));
2333 LASSERT(th != NULL);
2335 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2338 * DIR_NLINK feature is set for compatibility reasons if:
2339 * 1) nlinks > LDISKFS_LINK_MAX, or
2340 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2342 * It is easier to always set this flag (rather than check and set),
2343 * since it has less overhead, and the superblock will be dirtied
2344 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2345 * do not actually care whether this flag is set or not.
2347 spin_lock(&obj->oo_guard);
2348 /* inc_nlink from 0 may cause WARN_ON */
2349 if(inode->i_nlink == 0)
2350 set_nlink(inode, 1);
2353 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2354 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2355 inode->i_nlink == 2)
2356 set_nlink(inode, 1);
2358 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2359 spin_unlock(&obj->oo_guard);
2360 inode->i_sb->s_op->dirty_inode(inode);
2361 LINVRNT(osd_invariant(obj));
2366 static int osd_declare_object_ref_del(const struct lu_env *env,
2367 struct dt_object *dt,
2368 struct thandle *handle)
2370 struct osd_thandle *oh;
2372 LASSERT(dt_object_exists(dt));
2373 LASSERT(handle != NULL);
2375 oh = container_of0(handle, struct osd_thandle, ot_super);
2376 LASSERT(oh->ot_handle == NULL);
2378 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2379 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2385 * Concurrency: @dt is write locked.
2387 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2390 struct osd_object *obj = osd_dt_obj(dt);
2391 struct inode *inode = obj->oo_inode;
2393 LINVRNT(osd_invariant(obj));
2394 LASSERT(dt_object_exists(dt));
2395 LASSERT(osd_write_locked(env, obj));
2396 LASSERT(th != NULL);
2398 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2400 spin_lock(&obj->oo_guard);
2401 LASSERT(inode->i_nlink > 0);
2403 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2404 * then the nlink count is 1. Don't let it be set to 0 or the directory
2405 * inode will be deleted incorrectly. */
2406 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2407 set_nlink(inode, 1);
2408 spin_unlock(&obj->oo_guard);
2409 inode->i_sb->s_op->dirty_inode(inode);
2410 LINVRNT(osd_invariant(obj));
2416 * Get the 64-bit version for an inode.
2418 static int osd_object_version_get(const struct lu_env *env,
2419 struct dt_object *dt, dt_obj_version_t *ver)
2421 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2423 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2424 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2425 *ver = LDISKFS_I(inode)->i_fs_version;
2430 * Concurrency: @dt is read locked.
2432 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2433 struct lu_buf *buf, const char *name,
2434 struct lustre_capa *capa)
2436 struct osd_object *obj = osd_dt_obj(dt);
2437 struct inode *inode = obj->oo_inode;
2438 struct osd_thread_info *info = osd_oti_get(env);
2439 struct dentry *dentry = &info->oti_obj_dentry;
2441 /* version get is not real XATTR but uses xattr API */
2442 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2443 /* for version we are just using xattr API but change inode
2445 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2446 osd_object_version_get(env, dt, buf->lb_buf);
2447 return sizeof(dt_obj_version_t);
2450 LASSERT(dt_object_exists(dt));
2451 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2453 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2456 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2460 static int osd_declare_xattr_set(const struct lu_env *env,
2461 struct dt_object *dt,
2462 const struct lu_buf *buf, const char *name,
2463 int fl, struct thandle *handle)
2465 struct osd_thandle *oh;
2467 LASSERT(handle != NULL);
2469 oh = container_of0(handle, struct osd_thandle, ot_super);
2470 LASSERT(oh->ot_handle == NULL);
2472 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2473 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2474 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2475 osd_dto_credits_noquota[DTO_XATTR_SET]);
2481 * Set the 64-bit version for object
2483 static void osd_object_version_set(const struct lu_env *env,
2484 struct dt_object *dt,
2485 dt_obj_version_t *new_version)
2487 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2489 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2490 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2492 LDISKFS_I(inode)->i_fs_version = *new_version;
2493 /** Version is set after all inode operations are finished,
2494 * so we should mark it dirty here */
2495 inode->i_sb->s_op->dirty_inode(inode);
2499 * Concurrency: @dt is write locked.
2501 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2502 const struct lu_buf *buf, const char *name, int fl,
2503 struct thandle *handle, struct lustre_capa *capa)
2505 struct osd_object *obj = osd_dt_obj(dt);
2506 struct inode *inode = obj->oo_inode;
2507 struct osd_thread_info *info = osd_oti_get(env);
2510 LASSERT(handle != NULL);
2512 /* version set is not real XATTR */
2513 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2514 /* for version we are just using xattr API but change inode
2516 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2517 osd_object_version_set(env, dt, buf->lb_buf);
2518 return sizeof(dt_obj_version_t);
2521 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2524 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2525 if (fl & LU_XATTR_REPLACE)
2526 fs_flags |= XATTR_REPLACE;
2528 if (fl & LU_XATTR_CREATE)
2529 fs_flags |= XATTR_CREATE;
2531 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2536 * Concurrency: @dt is read locked.
2538 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2539 struct lu_buf *buf, struct lustre_capa *capa)
2541 struct osd_object *obj = osd_dt_obj(dt);
2542 struct inode *inode = obj->oo_inode;
2543 struct osd_thread_info *info = osd_oti_get(env);
2544 struct dentry *dentry = &info->oti_obj_dentry;
2546 LASSERT(dt_object_exists(dt));
2547 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2548 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2550 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2553 dentry->d_inode = inode;
2554 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2557 static int osd_declare_xattr_del(const struct lu_env *env,
2558 struct dt_object *dt, const char *name,
2559 struct thandle *handle)
2561 struct osd_thandle *oh;
2563 LASSERT(dt_object_exists(dt));
2564 LASSERT(handle != NULL);
2566 oh = container_of0(handle, struct osd_thandle, ot_super);
2567 LASSERT(oh->ot_handle == NULL);
2569 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2570 osd_dto_credits_noquota[DTO_XATTR_SET]);
2576 * Concurrency: @dt is write locked.
2578 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2579 const char *name, struct thandle *handle,
2580 struct lustre_capa *capa)
2582 struct osd_object *obj = osd_dt_obj(dt);
2583 struct inode *inode = obj->oo_inode;
2584 struct osd_thread_info *info = osd_oti_get(env);
2585 struct dentry *dentry = &info->oti_obj_dentry;
2588 LASSERT(dt_object_exists(dt));
2589 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2590 LASSERT(handle != NULL);
2592 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2595 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2597 ll_vfs_dq_init(inode);
2598 dentry->d_inode = inode;
2599 rc = inode->i_op->removexattr(dentry, name);
2603 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2604 struct dt_object *dt,
2605 struct lustre_capa *old,
2608 struct osd_thread_info *info = osd_oti_get(env);
2609 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2610 struct osd_object *obj = osd_dt_obj(dt);
2611 struct osd_device *dev = osd_obj2dev(obj);
2612 struct lustre_capa_key *key = &info->oti_capa_key;
2613 struct lustre_capa *capa = &info->oti_capa;
2614 struct obd_capa *oc;
2615 struct md_capainfo *ci;
2619 if (!dev->od_fl_capa)
2620 RETURN(ERR_PTR(-ENOENT));
2622 LASSERT(dt_object_exists(dt));
2623 LINVRNT(osd_invariant(obj));
2625 /* renewal sanity check */
2626 if (old && osd_object_auth(env, dt, old, opc))
2627 RETURN(ERR_PTR(-EACCES));
2629 ci = md_capainfo(env);
2631 RETURN(ERR_PTR(-ENOENT));
2633 switch (ci->mc_auth) {
2637 capa->lc_uid = obj->oo_inode->i_uid;
2638 capa->lc_gid = obj->oo_inode->i_gid;
2639 capa->lc_flags = LC_ID_PLAIN;
2641 case LC_ID_CONVERT: {
2644 s[0] = obj->oo_inode->i_uid;
2645 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2646 s[2] = obj->oo_inode->i_gid;
2647 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2648 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2650 RETURN(ERR_PTR(rc));
2652 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2653 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2654 capa->lc_flags = LC_ID_CONVERT;
2658 RETURN(ERR_PTR(-EINVAL));
2661 capa->lc_fid = *fid;
2663 capa->lc_flags |= dev->od_capa_alg << 24;
2664 capa->lc_timeout = dev->od_capa_timeout;
2665 capa->lc_expiry = 0;
2667 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2669 LASSERT(!capa_is_expired(oc));
2673 spin_lock(&capa_lock);
2674 *key = dev->od_capa_keys[1];
2675 spin_unlock(&capa_lock);
2677 capa->lc_keyid = key->lk_keyid;
2678 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2680 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2682 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2683 RETURN(ERR_PTR(rc));
2686 oc = capa_add(dev->od_capa_hash, capa);
2690 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2692 struct osd_object *obj = osd_dt_obj(dt);
2693 struct inode *inode = obj->oo_inode;
2694 struct osd_thread_info *info = osd_oti_get(env);
2695 struct dentry *dentry = &info->oti_obj_dentry;
2696 struct file *file = &info->oti_file;
2701 dentry->d_inode = inode;
2702 file->f_dentry = dentry;
2703 file->f_mapping = inode->i_mapping;
2704 file->f_op = inode->i_fop;
2705 mutex_lock(&inode->i_mutex);
2706 rc = file->f_op->fsync(file, dentry, 0);
2707 mutex_unlock(&inode->i_mutex);
2711 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2714 struct osd_object *obj = osd_dt_obj(dt);
2717 *data = (void *)obj->oo_inode;
2725 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2726 const struct dt_index_features *feat)
2728 struct iam_descr *descr;
2730 if (osd_object_is_root(o))
2731 return feat == &dt_directory_features;
2733 LASSERT(o->oo_dir != NULL);
2735 descr = o->oo_dir->od_container.ic_descr;
2736 if (feat == &dt_directory_features) {
2737 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2743 feat->dif_keysize_min <= descr->id_key_size &&
2744 descr->id_key_size <= feat->dif_keysize_max &&
2745 feat->dif_recsize_min <= descr->id_rec_size &&
2746 descr->id_rec_size <= feat->dif_recsize_max &&
2747 !(feat->dif_flags & (DT_IND_VARKEY |
2748 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2749 ergo(feat->dif_flags & DT_IND_UPDATE,
2750 1 /* XXX check that object (and file system) is
2755 static int osd_iam_container_init(const struct lu_env *env,
2756 struct osd_object *obj,
2757 struct osd_directory *dir)
2759 struct iam_container *bag = &dir->od_container;
2762 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2766 result = iam_container_setup(bag);
2768 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2770 iam_container_fini(bag);
2777 * Concurrency: no external locking is necessary.
2779 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2780 const struct dt_index_features *feat)
2784 struct osd_object *obj = osd_dt_obj(dt);
2786 LINVRNT(osd_invariant(obj));
2787 LASSERT(dt_object_exists(dt));
2789 if (osd_object_is_root(obj)) {
2790 dt->do_index_ops = &osd_index_ea_ops;
2792 } else if (feat == &dt_directory_features) {
2793 dt->do_index_ops = &osd_index_ea_ops;
2794 if (S_ISDIR(obj->oo_inode->i_mode))
2799 } else if (unlikely(feat == &dt_otable_features)) {
2800 dt->do_index_ops = &osd_otable_ops;
2802 } else if (unlikely(feat == &dt_acct_features)) {
2803 dt->do_index_ops = &osd_acct_index_ops;
2806 } else if (!osd_has_index(obj)) {
2807 struct osd_directory *dir;
2812 spin_lock(&obj->oo_guard);
2813 if (obj->oo_dir == NULL)
2817 * Concurrent thread allocated container data.
2820 spin_unlock(&obj->oo_guard);
2822 * Now, that we have container data, serialize its
2825 down_write(&obj->oo_ext_idx_sem);
2827 * recheck under lock.
2829 if (!osd_has_index(obj))
2830 result = osd_iam_container_init(env, obj, dir);
2833 up_write(&obj->oo_ext_idx_sem);
2841 if (result == 0 && skip_iam == 0) {
2842 if (!osd_iam_index_probe(env, obj, feat))
2845 LINVRNT(osd_invariant(obj));
2847 if (is_quota_glb_feat(feat))
2848 result = osd_quota_migration(env, dt, feat);
2853 static int osd_otable_it_attr_get(const struct lu_env *env,
2854 struct dt_object *dt,
2855 struct lu_attr *attr,
2856 struct lustre_capa *capa)
2862 static const struct dt_object_operations osd_obj_ops = {
2863 .do_read_lock = osd_object_read_lock,
2864 .do_write_lock = osd_object_write_lock,
2865 .do_read_unlock = osd_object_read_unlock,
2866 .do_write_unlock = osd_object_write_unlock,
2867 .do_write_locked = osd_object_write_locked,
2868 .do_attr_get = osd_attr_get,
2869 .do_declare_attr_set = osd_declare_attr_set,
2870 .do_attr_set = osd_attr_set,
2871 .do_ah_init = osd_ah_init,
2872 .do_declare_create = osd_declare_object_create,
2873 .do_create = osd_object_create,
2874 .do_declare_destroy = osd_declare_object_destroy,
2875 .do_destroy = osd_object_destroy,
2876 .do_index_try = osd_index_try,
2877 .do_declare_ref_add = osd_declare_object_ref_add,
2878 .do_ref_add = osd_object_ref_add,
2879 .do_declare_ref_del = osd_declare_object_ref_del,
2880 .do_ref_del = osd_object_ref_del,
2881 .do_xattr_get = osd_xattr_get,
2882 .do_declare_xattr_set = osd_declare_xattr_set,
2883 .do_xattr_set = osd_xattr_set,
2884 .do_declare_xattr_del = osd_declare_xattr_del,
2885 .do_xattr_del = osd_xattr_del,
2886 .do_xattr_list = osd_xattr_list,
2887 .do_capa_get = osd_capa_get,
2888 .do_object_sync = osd_object_sync,
2889 .do_data_get = osd_data_get,
2893 * dt_object_operations for interoperability mode
2894 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2896 static const struct dt_object_operations osd_obj_ea_ops = {
2897 .do_read_lock = osd_object_read_lock,
2898 .do_write_lock = osd_object_write_lock,
2899 .do_read_unlock = osd_object_read_unlock,
2900 .do_write_unlock = osd_object_write_unlock,
2901 .do_write_locked = osd_object_write_locked,
2902 .do_attr_get = osd_attr_get,
2903 .do_declare_attr_set = osd_declare_attr_set,
2904 .do_attr_set = osd_attr_set,
2905 .do_ah_init = osd_ah_init,
2906 .do_declare_create = osd_declare_object_create,
2907 .do_create = osd_object_ea_create,
2908 .do_declare_destroy = osd_declare_object_destroy,
2909 .do_destroy = osd_object_destroy,
2910 .do_index_try = osd_index_try,
2911 .do_declare_ref_add = osd_declare_object_ref_add,
2912 .do_ref_add = osd_object_ref_add,
2913 .do_declare_ref_del = osd_declare_object_ref_del,
2914 .do_ref_del = osd_object_ref_del,
2915 .do_xattr_get = osd_xattr_get,
2916 .do_declare_xattr_set = osd_declare_xattr_set,
2917 .do_xattr_set = osd_xattr_set,
2918 .do_declare_xattr_del = osd_declare_xattr_del,
2919 .do_xattr_del = osd_xattr_del,
2920 .do_xattr_list = osd_xattr_list,
2921 .do_capa_get = osd_capa_get,
2922 .do_object_sync = osd_object_sync,
2923 .do_data_get = osd_data_get,
2926 static const struct dt_object_operations osd_obj_otable_it_ops = {
2927 .do_attr_get = osd_otable_it_attr_get,
2928 .do_index_try = osd_index_try,
2931 static int osd_index_declare_iam_delete(const struct lu_env *env,
2932 struct dt_object *dt,
2933 const struct dt_key *key,
2934 struct thandle *handle)
2936 struct osd_thandle *oh;
2938 oh = container_of0(handle, struct osd_thandle, ot_super);
2939 LASSERT(oh->ot_handle == NULL);
2941 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2942 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
2948 * delete a (key, value) pair from index \a dt specified by \a key
2950 * \param dt osd index object
2951 * \param key key for index
2952 * \param rec record reference
2953 * \param handle transaction handler
2956 * \retval -ve failure
2959 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2960 const struct dt_key *key,
2961 struct thandle *handle,
2962 struct lustre_capa *capa)
2964 struct osd_thread_info *oti = osd_oti_get(env);
2965 struct osd_object *obj = osd_dt_obj(dt);
2966 struct osd_thandle *oh;
2967 struct iam_path_descr *ipd;
2968 struct iam_container *bag = &obj->oo_dir->od_container;
2973 LINVRNT(osd_invariant(obj));
2974 LASSERT(dt_object_exists(dt));
2975 LASSERT(bag->ic_object == obj->oo_inode);
2976 LASSERT(handle != NULL);
2978 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2981 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
2983 ipd = osd_idx_ipd_get(env, bag);
2984 if (unlikely(ipd == NULL))
2987 oh = container_of0(handle, struct osd_thandle, ot_super);
2988 LASSERT(oh->ot_handle != NULL);
2989 LASSERT(oh->ot_handle->h_transaction != NULL);
2991 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
2992 /* swab quota uid/gid provided by caller */
2993 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
2994 key = (const struct dt_key *)&oti->oti_quota_id;
2997 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2998 osd_ipd_put(env, bag, ipd);
2999 LINVRNT(osd_invariant(obj));
3003 static int osd_index_declare_ea_delete(const struct lu_env *env,
3004 struct dt_object *dt,
3005 const struct dt_key *key,
3006 struct thandle *handle)
3008 struct osd_thandle *oh;
3009 struct inode *inode;
3013 LASSERT(dt_object_exists(dt));
3014 LASSERT(handle != NULL);
3016 oh = container_of0(handle, struct osd_thandle, ot_super);
3017 LASSERT(oh->ot_handle == NULL);
3019 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3020 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3022 inode = osd_dt_obj(dt)->oo_inode;
3025 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3026 true, true, NULL, false);
3030 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3033 struct osd_fid_pack *rec;
3036 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3037 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3038 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3044 * Index delete function for interoperability mode (b11826).
3045 * It will remove the directory entry added by osd_index_ea_insert().
3046 * This entry is needed to maintain name->fid mapping.
3048 * \param key, key i.e. file entry to be deleted
3050 * \retval 0, on success
3051 * \retval -ve, on error
3053 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3054 const struct dt_key *key,
3055 struct thandle *handle,
3056 struct lustre_capa *capa)
3058 struct osd_object *obj = osd_dt_obj(dt);
3059 struct inode *dir = obj->oo_inode;
3060 struct dentry *dentry;
3061 struct osd_thandle *oh;
3062 struct ldiskfs_dir_entry_2 *de;
3063 struct buffer_head *bh;
3064 struct htree_lock *hlock = NULL;
3069 LINVRNT(osd_invariant(obj));
3070 LASSERT(dt_object_exists(dt));
3071 LASSERT(handle != NULL);
3073 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3075 oh = container_of(handle, struct osd_thandle, ot_super);
3076 LASSERT(oh->ot_handle != NULL);
3077 LASSERT(oh->ot_handle->h_transaction != NULL);
3079 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3082 ll_vfs_dq_init(dir);
3083 dentry = osd_child_dentry_get(env, obj,
3084 (char *)key, strlen((char *)key));
3086 if (obj->oo_hl_head != NULL) {
3087 hlock = osd_oti_get(env)->oti_hlock;
3088 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3089 dir, LDISKFS_HLOCK_DEL);
3091 down_write(&obj->oo_ext_idx_sem);
3094 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3096 rc = ldiskfs_delete_entry(oh->ot_handle,
3103 ldiskfs_htree_unlock(hlock);
3105 up_write(&obj->oo_ext_idx_sem);
3107 LASSERT(osd_invariant(obj));
3112 * Lookup index for \a key and copy record to \a rec.
3114 * \param dt osd index object
3115 * \param key key for index
3116 * \param rec record reference
3118 * \retval +ve success : exact mach
3119 * \retval 0 return record with key not greater than \a key
3120 * \retval -ve failure
3122 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3123 struct dt_rec *rec, const struct dt_key *key,
3124 struct lustre_capa *capa)
3126 struct osd_object *obj = osd_dt_obj(dt);
3127 struct iam_path_descr *ipd;
3128 struct iam_container *bag = &obj->oo_dir->od_container;
3129 struct osd_thread_info *oti = osd_oti_get(env);
3130 struct iam_iterator *it = &oti->oti_idx_it;
3131 struct iam_rec *iam_rec;
3136 LASSERT(osd_invariant(obj));
3137 LASSERT(dt_object_exists(dt));
3138 LASSERT(bag->ic_object == obj->oo_inode);
3140 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3143 ipd = osd_idx_ipd_get(env, bag);
3147 /* got ipd now we can start iterator. */
3148 iam_it_init(it, bag, 0, ipd);
3150 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3151 /* swab quota uid/gid provided by caller */
3152 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3153 key = (const struct dt_key *)&oti->oti_quota_id;
3156 rc = iam_it_get(it, (struct iam_key *)key);
3158 if (S_ISDIR(obj->oo_inode->i_mode))
3159 iam_rec = (struct iam_rec *)oti->oti_ldp;
3161 iam_rec = (struct iam_rec *) rec;
3163 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3165 if (S_ISDIR(obj->oo_inode->i_mode))
3166 osd_fid_unpack((struct lu_fid *) rec,
3167 (struct osd_fid_pack *)iam_rec);
3168 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3169 osd_quota_unpack(obj, rec);
3174 osd_ipd_put(env, bag, ipd);
3176 LINVRNT(osd_invariant(obj));
3181 static int osd_index_declare_iam_insert(const struct lu_env *env,
3182 struct dt_object *dt,
3183 const struct dt_rec *rec,
3184 const struct dt_key *key,
3185 struct thandle *handle)
3187 struct osd_thandle *oh;
3189 LASSERT(dt_object_exists(dt));
3190 LASSERT(handle != NULL);
3192 oh = container_of0(handle, struct osd_thandle, ot_super);
3193 LASSERT(oh->ot_handle == NULL);
3195 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3196 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3202 * Inserts (key, value) pair in \a dt index object.
3204 * \param dt osd index object
3205 * \param key key for index
3206 * \param rec record reference
3207 * \param th transaction handler
3210 * \retval -ve failure
3212 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3213 const struct dt_rec *rec,
3214 const struct dt_key *key, struct thandle *th,
3215 struct lustre_capa *capa, int ignore_quota)
3217 struct osd_object *obj = osd_dt_obj(dt);
3218 struct iam_path_descr *ipd;
3219 struct osd_thandle *oh;
3220 struct iam_container *bag = &obj->oo_dir->od_container;
3221 struct osd_thread_info *oti = osd_oti_get(env);
3222 struct iam_rec *iam_rec;
3227 LINVRNT(osd_invariant(obj));
3228 LASSERT(dt_object_exists(dt));
3229 LASSERT(bag->ic_object == obj->oo_inode);
3230 LASSERT(th != NULL);
3232 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3235 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3237 ipd = osd_idx_ipd_get(env, bag);
3238 if (unlikely(ipd == NULL))
3241 oh = container_of0(th, struct osd_thandle, ot_super);
3242 LASSERT(oh->ot_handle != NULL);
3243 LASSERT(oh->ot_handle->h_transaction != NULL);
3244 if (S_ISDIR(obj->oo_inode->i_mode)) {
3245 iam_rec = (struct iam_rec *)oti->oti_ldp;
3246 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3247 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3248 /* pack quota uid/gid */
3249 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3250 key = (const struct dt_key *)&oti->oti_quota_id;
3251 /* pack quota record */
3252 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3253 iam_rec = (struct iam_rec *)rec;
3255 iam_rec = (struct iam_rec *)rec;
3258 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3260 osd_ipd_put(env, bag, ipd);
3261 LINVRNT(osd_invariant(obj));
3266 * Calls ldiskfs_add_entry() to add directory entry
3267 * into the directory. This is required for
3268 * interoperability mode (b11826)
3270 * \retval 0, on success
3271 * \retval -ve, on error
3273 static int __osd_ea_add_rec(struct osd_thread_info *info,
3274 struct osd_object *pobj, struct inode *cinode,
3275 const char *name, const struct dt_rec *fid,
3276 struct htree_lock *hlock, struct thandle *th)
3278 struct ldiskfs_dentry_param *ldp;
3279 struct dentry *child;
3280 struct osd_thandle *oth;
3283 oth = container_of(th, struct osd_thandle, ot_super);
3284 LASSERT(oth->ot_handle != NULL);
3285 LASSERT(oth->ot_handle->h_transaction != NULL);
3286 LASSERT(pobj->oo_inode);
3288 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3289 if (unlikely(pobj->oo_inode ==
3290 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3293 osd_get_ldiskfs_dirent_param(ldp, fid);
3294 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3295 child->d_fsdata = (void *)ldp;
3296 ll_vfs_dq_init(pobj->oo_inode);
3297 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3303 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3304 * into the directory.Also sets flags into osd object to
3305 * indicate dot and dotdot are created. This is required for
3306 * interoperability mode (b11826)
3308 * \param dir directory for dot and dotdot fixup.
3309 * \param obj child object for linking
3311 * \retval 0, on success
3312 * \retval -ve, on error
3314 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3315 struct osd_object *dir,
3316 struct inode *parent_dir, const char *name,
3317 const struct dt_rec *dot_fid,
3318 const struct dt_rec *dot_dot_fid,
3321 struct inode *inode = dir->oo_inode;
3322 struct ldiskfs_dentry_param *dot_ldp;
3323 struct ldiskfs_dentry_param *dot_dot_ldp;
3324 struct osd_thandle *oth;
3327 oth = container_of(th, struct osd_thandle, ot_super);
3328 LASSERT(oth->ot_handle->h_transaction != NULL);
3329 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3331 if (strcmp(name, dot) == 0) {
3332 if (dir->oo_compat_dot_created) {
3335 LASSERT(inode == parent_dir);
3336 dir->oo_compat_dot_created = 1;
3339 } else if(strcmp(name, dotdot) == 0) {
3340 if (!dir->oo_compat_dot_created)
3343 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3344 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3345 /* in case of rename, dotdot is already created */
3346 if (dir->oo_compat_dotdot_created)
3347 return __osd_ea_add_rec(info, dir, parent_dir, name,
3348 dot_dot_fid, NULL, th);
3350 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3351 dot_ldp->edp_magic = 0;
3352 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3353 inode, dot_ldp, dot_dot_ldp);
3355 dir->oo_compat_dotdot_created = 1;
3363 * It will call the appropriate osd_add* function and return the
3364 * value, return by respective functions.
3366 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3367 struct inode *cinode, const char *name,
3368 const struct dt_rec *fid, struct thandle *th)
3370 struct osd_thread_info *info = osd_oti_get(env);
3371 struct htree_lock *hlock;
3374 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3376 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3378 if (hlock != NULL) {
3379 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3382 down_write(&pobj->oo_ext_idx_sem);
3384 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3385 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3388 if (hlock != NULL) {
3389 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3390 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3392 down_write(&pobj->oo_ext_idx_sem);
3395 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3399 ldiskfs_htree_unlock(hlock);
3401 up_write(&pobj->oo_ext_idx_sem);
3407 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3408 struct osd_idmap_cache *oic)
3410 struct osd_scrub *scrub = &dev->od_scrub;
3411 struct lu_fid *fid = &oic->oic_fid;
3412 struct osd_inode_id *id = &oti->oti_id;
3417 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3421 rc = osd_oi_lookup(oti, dev, fid, id, true);
3422 if (rc != 0 && rc != -ENOENT)
3425 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3428 if (thread_is_running(&scrub->os_thread)) {
3429 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3430 /* There is race condition between osd_oi_lookup and OI scrub.
3431 * The OI scrub finished just after osd_oi_lookup() failure.
3432 * Under such case, it is unnecessary to trigger OI scrub again,
3433 * but try to call osd_oi_lookup() again. */
3434 if (unlikely(rc == -EAGAIN))
3440 if (!dev->od_noscrub && ++once == 1) {
3441 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3443 rc = osd_scrub_start(dev);
3444 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3446 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3456 * Calls ->lookup() to find dentry. From dentry get inode and
3457 * read inode's ea to get fid. This is required for interoperability
3460 * \retval 0, on success
3461 * \retval -ve, on error
3463 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3464 struct dt_rec *rec, const struct dt_key *key)
3466 struct inode *dir = obj->oo_inode;
3467 struct dentry *dentry;
3468 struct ldiskfs_dir_entry_2 *de;
3469 struct buffer_head *bh;
3470 struct lu_fid *fid = (struct lu_fid *) rec;
3471 struct htree_lock *hlock = NULL;
3476 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3478 dentry = osd_child_dentry_get(env, obj,
3479 (char *)key, strlen((char *)key));
3481 if (obj->oo_hl_head != NULL) {
3482 hlock = osd_oti_get(env)->oti_hlock;
3483 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3484 dir, LDISKFS_HLOCK_LOOKUP);
3486 down_read(&obj->oo_ext_idx_sem);
3489 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3491 struct osd_thread_info *oti = osd_oti_get(env);
3492 struct osd_idmap_cache *oic = &oti->oti_cache;
3493 struct osd_device *dev = osd_obj2dev(obj);
3494 struct osd_scrub *scrub = &dev->od_scrub;
3495 struct scrub_file *sf = &scrub->os_file;
3497 ino = le32_to_cpu(de->inode);
3498 rc = osd_get_fid_from_dentry(de, rec);
3500 /* done with de, release bh */
3503 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3505 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3507 fid_zero(&oic->oic_fid);
3511 oic->oic_fid = *fid;
3512 if ((scrub->os_pos_current <= ino) &&
3513 (sf->sf_flags & SF_INCONSISTENT ||
3514 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3516 osd_consistency_check(oti, dev, oic);
3525 ldiskfs_htree_unlock(hlock);
3527 up_read(&obj->oo_ext_idx_sem);
3532 * Find the osd object for given fid.
3534 * \param fid need to find the osd object having this fid
3536 * \retval osd_object on success
3537 * \retval -ve on error
3539 struct osd_object *osd_object_find(const struct lu_env *env,
3540 struct dt_object *dt,
3541 const struct lu_fid *fid)
3543 struct lu_device *ludev = dt->do_lu.lo_dev;
3544 struct osd_object *child = NULL;
3545 struct lu_object *luch;
3546 struct lu_object *lo;
3549 * at this point topdev might not exist yet
3550 * (i.e. MGS is preparing profiles). so we can
3551 * not rely on topdev and instead lookup with
3552 * our device passed as topdev. this can't work
3553 * if the object isn't cached yet (as osd doesn't
3554 * allocate lu_header). IOW, the object must be
3555 * in the cache, otherwise lu_object_alloc() crashes
3558 luch = lu_object_find_at(env, ludev, fid, NULL);
3559 if (!IS_ERR(luch)) {
3560 if (lu_object_exists(luch)) {
3561 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3563 child = osd_obj(lo);
3565 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3566 "lu_object can't be located"
3567 DFID"\n", PFID(fid));
3569 if (child == NULL) {
3570 lu_object_put(env, luch);
3571 CERROR("Unable to get osd_object\n");
3572 child = ERR_PTR(-ENOENT);
3575 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3576 "lu_object does not exists "DFID"\n",
3578 lu_object_put(env, luch);
3579 child = ERR_PTR(-ENOENT);
3582 child = (void *)luch;
3588 * Put the osd object once done with it.
3590 * \param obj osd object that needs to be put
3592 static inline void osd_object_put(const struct lu_env *env,
3593 struct osd_object *obj)
3595 lu_object_put(env, &obj->oo_dt.do_lu);
3598 static int osd_index_declare_ea_insert(const struct lu_env *env,
3599 struct dt_object *dt,
3600 const struct dt_rec *rec,
3601 const struct dt_key *key,
3602 struct thandle *handle)
3604 struct osd_thandle *oh;
3605 struct inode *inode;
3606 struct lu_fid *fid = (struct lu_fid *)rec;
3610 LASSERT(dt_object_exists(dt));
3611 LASSERT(handle != NULL);
3613 oh = container_of0(handle, struct osd_thandle, ot_super);
3614 LASSERT(oh->ot_handle == NULL);
3616 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3617 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3619 inode = osd_dt_obj(dt)->oo_inode;
3622 /* We ignore block quota on meta pool (MDTs), so needn't
3623 * calculate how many blocks will be consumed by this index
3625 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3626 true, true, NULL, false);
3630 /* It does fld look up inside declare, and the result will be
3631 * added to fld cache, so the following fld lookup inside insert
3632 * does not need send RPC anymore, so avoid send rpc with holding
3634 LASSERTF(fid_is_sane(fid), "fid is insane"DFID"\n", PFID(fid));
3635 osd_fld_lookup(env, osd_dt_dev(handle->th_dev), fid,
3636 &osd_oti_get(env)->oti_seq_range);
3642 * Index add function for interoperability mode (b11826).
3643 * It will add the directory entry.This entry is needed to
3644 * maintain name->fid mapping.
3646 * \param key it is key i.e. file entry to be inserted
3647 * \param rec it is value of given key i.e. fid
3649 * \retval 0, on success
3650 * \retval -ve, on error
3652 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3653 const struct dt_rec *rec,
3654 const struct dt_key *key, struct thandle *th,
3655 struct lustre_capa *capa, int ignore_quota)
3657 struct osd_object *obj = osd_dt_obj(dt);
3658 struct lu_fid *fid = (struct lu_fid *) rec;
3659 const char *name = (const char *)key;
3660 struct osd_object *child;
3665 LASSERT(osd_invariant(obj));
3666 LASSERT(dt_object_exists(dt));
3667 LASSERT(th != NULL);
3669 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3671 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3674 child = osd_object_find(env, dt, fid);
3675 if (!IS_ERR(child)) {
3676 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3677 osd_object_put(env, child);
3679 rc = PTR_ERR(child);
3682 LASSERT(osd_invariant(obj));
3687 * Initialize osd Iterator for given osd index object.
3689 * \param dt osd index object
3692 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3693 struct dt_object *dt,
3695 struct lustre_capa *capa)
3697 struct osd_it_iam *it;
3698 struct osd_thread_info *oti = osd_oti_get(env);
3699 struct osd_object *obj = osd_dt_obj(dt);
3700 struct lu_object *lo = &dt->do_lu;
3701 struct iam_path_descr *ipd;
3702 struct iam_container *bag = &obj->oo_dir->od_container;
3704 LASSERT(lu_object_exists(lo));
3706 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3707 return ERR_PTR(-EACCES);
3710 ipd = osd_it_ipd_get(env, bag);
3711 if (likely(ipd != NULL)) {
3715 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3716 return (struct dt_it *)it;
3718 return ERR_PTR(-ENOMEM);
3722 * free given Iterator.
3725 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3727 struct osd_it_iam *it = (struct osd_it_iam *)di;
3728 struct osd_object *obj = it->oi_obj;
3730 iam_it_fini(&it->oi_it);
3731 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3732 lu_object_put(env, &obj->oo_dt.do_lu);
3736 * Move Iterator to record specified by \a key
3738 * \param di osd iterator
3739 * \param key key for index
3741 * \retval +ve di points to record with least key not larger than key
3742 * \retval 0 di points to exact matched key
3743 * \retval -ve failure
3746 static int osd_it_iam_get(const struct lu_env *env,
3747 struct dt_it *di, const struct dt_key *key)
3749 struct osd_thread_info *oti = osd_oti_get(env);
3750 struct osd_it_iam *it = (struct osd_it_iam *)di;
3752 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3753 /* swab quota uid/gid */
3754 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3755 key = (struct dt_key *)&oti->oti_quota_id;
3758 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3764 * \param di osd iterator
3767 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3769 struct osd_it_iam *it = (struct osd_it_iam *)di;
3771 iam_it_put(&it->oi_it);
3775 * Move iterator by one record
3777 * \param di osd iterator
3779 * \retval +1 end of container reached
3781 * \retval -ve failure
3784 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3786 struct osd_it_iam *it = (struct osd_it_iam *)di;
3788 return iam_it_next(&it->oi_it);
3792 * Return pointer to the key under iterator.
3795 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3796 const struct dt_it *di)
3798 struct osd_thread_info *oti = osd_oti_get(env);
3799 struct osd_it_iam *it = (struct osd_it_iam *)di;
3800 struct osd_object *obj = it->oi_obj;
3803 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
3805 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
3806 /* swab quota uid/gid */
3807 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
3808 key = (struct dt_key *)&oti->oti_quota_id;
3815 * Return size of key under iterator (in bytes)
3818 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3820 struct osd_it_iam *it = (struct osd_it_iam *)di;
3822 return iam_it_key_size(&it->oi_it);
3825 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3826 int len, __u16 type)
3828 struct luda_type *lt;
3829 const unsigned align = sizeof(struct luda_type) - 1;
3831 /* check if file type is required */
3832 if (attr & LUDA_TYPE) {
3833 len = (len + align) & ~align;
3835 lt = (void *) ent->lde_name + len;
3836 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3837 ent->lde_attrs |= LUDA_TYPE;
3840 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3844 * build lu direct from backend fs dirent.
3847 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3848 struct lu_fid *fid, __u64 offset,
3849 char *name, __u16 namelen,
3850 __u16 type, __u32 attr)
3852 fid_cpu_to_le(&ent->lde_fid, fid);
3853 ent->lde_attrs = LUDA_FID;
3855 ent->lde_hash = cpu_to_le64(offset);
3856 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3858 strncpy(ent->lde_name, name, namelen);
3859 ent->lde_namelen = cpu_to_le16(namelen);
3861 /* append lustre attributes */
3862 osd_it_append_attrs(ent, attr, namelen, type);
3866 * Return pointer to the record under iterator.
3868 static int osd_it_iam_rec(const struct lu_env *env,
3869 const struct dt_it *di,
3870 struct dt_rec *dtrec, __u32 attr)
3872 struct osd_it_iam *it = (struct osd_it_iam *)di;
3873 struct osd_thread_info *info = osd_oti_get(env);
3876 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3877 const struct osd_fid_pack *rec;
3878 struct lu_fid *fid = &info->oti_fid;
3879 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3885 name = (char *)iam_it_key_get(&it->oi_it);
3887 RETURN(PTR_ERR(name));
3889 namelen = iam_it_key_size(&it->oi_it);
3891 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3893 RETURN(PTR_ERR(rec));
3895 rc = osd_fid_unpack(fid, rec);
3899 hash = iam_it_store(&it->oi_it);
3901 /* IAM does not store object type in IAM index (dir) */
3902 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3904 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
3905 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3906 (struct iam_rec *)dtrec);
3907 osd_quota_unpack(it->oi_obj, dtrec);
3909 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3910 (struct iam_rec *)dtrec);
3917 * Returns cookie for current Iterator position.
3919 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3921 struct osd_it_iam *it = (struct osd_it_iam *)di;
3923 return iam_it_store(&it->oi_it);
3927 * Restore iterator from cookie.
3929 * \param di osd iterator
3930 * \param hash Iterator location cookie
3932 * \retval +ve di points to record with least key not larger than key.
3933 * \retval 0 di points to exact matched key
3934 * \retval -ve failure
3937 static int osd_it_iam_load(const struct lu_env *env,
3938 const struct dt_it *di, __u64 hash)
3940 struct osd_it_iam *it = (struct osd_it_iam *)di;
3942 return iam_it_load(&it->oi_it, hash);
3945 static const struct dt_index_operations osd_index_iam_ops = {
3946 .dio_lookup = osd_index_iam_lookup,
3947 .dio_declare_insert = osd_index_declare_iam_insert,
3948 .dio_insert = osd_index_iam_insert,
3949 .dio_declare_delete = osd_index_declare_iam_delete,
3950 .dio_delete = osd_index_iam_delete,
3952 .init = osd_it_iam_init,
3953 .fini = osd_it_iam_fini,
3954 .get = osd_it_iam_get,
3955 .put = osd_it_iam_put,
3956 .next = osd_it_iam_next,
3957 .key = osd_it_iam_key,
3958 .key_size = osd_it_iam_key_size,
3959 .rec = osd_it_iam_rec,
3960 .store = osd_it_iam_store,
3961 .load = osd_it_iam_load
3967 * Creates or initializes iterator context.
3969 * \retval struct osd_it_ea, iterator structure on success
3972 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3973 struct dt_object *dt,
3975 struct lustre_capa *capa)
3977 struct osd_object *obj = osd_dt_obj(dt);
3978 struct osd_thread_info *info = osd_oti_get(env);
3979 struct osd_it_ea *it = &info->oti_it_ea;
3980 struct lu_object *lo = &dt->do_lu;
3981 struct dentry *obj_dentry = &info->oti_it_dentry;
3983 LASSERT(lu_object_exists(lo));
3985 obj_dentry->d_inode = obj->oo_inode;
3986 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3987 obj_dentry->d_name.hash = 0;
3989 it->oie_rd_dirent = 0;
3990 it->oie_it_dirent = 0;
3991 it->oie_dirent = NULL;
3992 it->oie_buf = info->oti_it_ea_buf;
3994 it->oie_file.f_pos = 0;
3995 it->oie_file.f_dentry = obj_dentry;
3996 if (attr & LUDA_64BITHASH)
3997 it->oie_file.f_mode |= FMODE_64BITHASH;
3999 it->oie_file.f_mode |= FMODE_32BITHASH;
4000 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
4001 it->oie_file.f_op = obj->oo_inode->i_fop;
4002 it->oie_file.private_data = NULL;
4004 RETURN((struct dt_it *) it);
4008 * Destroy or finishes iterator context.
4010 * \param di iterator structure to be destroyed
4012 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4014 struct osd_it_ea *it = (struct osd_it_ea *)di;
4015 struct osd_object *obj = it->oie_obj;
4016 struct inode *inode = obj->oo_inode;
4019 it->oie_file.f_op->release(inode, &it->oie_file);
4020 lu_object_put(env, &obj->oo_dt.do_lu);
4025 * It position the iterator at given key, so that next lookup continues from
4026 * that key Or it is similar to dio_it->load() but based on a key,
4027 * rather than file position.
4029 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4032 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4034 static int osd_it_ea_get(const struct lu_env *env,
4035 struct dt_it *di, const struct dt_key *key)
4037 struct osd_it_ea *it = (struct osd_it_ea *)di;
4040 LASSERT(((const char *)key)[0] == '\0');
4041 it->oie_file.f_pos = 0;
4042 it->oie_rd_dirent = 0;
4043 it->oie_it_dirent = 0;
4044 it->oie_dirent = NULL;
4052 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4057 * It is called internally by ->readdir(). It fills the
4058 * iterator's in-memory data structure with required
4059 * information i.e. name, namelen, rec_size etc.
4061 * \param buf in which information to be filled in.
4062 * \param name name of the file in given dir
4064 * \retval 0 on success
4065 * \retval 1 on buffer full
4067 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4068 loff_t offset, __u64 ino,
4071 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4072 struct osd_object *obj = it->oie_obj;
4073 struct osd_it_ea_dirent *ent = it->oie_dirent;
4074 struct lu_fid *fid = &ent->oied_fid;
4075 struct osd_fid_pack *rec;
4078 /* this should never happen */
4079 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4080 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4084 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4088 /* "." is just the object itself. */
4089 if (namelen == 1 && name[0] == '.') {
4090 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4091 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4092 rec = (struct osd_fid_pack*) (name + namelen + 1);
4093 if (osd_fid_unpack(fid, rec) != 0)
4098 d_type &= ~LDISKFS_DIRENT_LUFID;
4100 /* NOT export local root. */
4101 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4102 ino = obj->oo_inode->i_ino;
4103 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4106 ent->oied_ino = ino;
4107 ent->oied_off = offset;
4108 ent->oied_namelen = namelen;
4109 ent->oied_type = d_type;
4111 memcpy(ent->oied_name, name, namelen);
4113 it->oie_rd_dirent++;
4114 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4119 * Calls ->readdir() to load a directory entry at a time
4120 * and stored it in iterator's in-memory data structure.
4122 * \param di iterator's in memory structure
4124 * \retval 0 on success
4125 * \retval -ve on error
4127 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4128 const struct dt_it *di)
4130 struct osd_it_ea *it = (struct osd_it_ea *)di;
4131 struct osd_object *obj = it->oie_obj;
4132 struct inode *inode = obj->oo_inode;
4133 struct htree_lock *hlock = NULL;
4137 it->oie_dirent = it->oie_buf;
4138 it->oie_rd_dirent = 0;
4140 if (obj->oo_hl_head != NULL) {
4141 hlock = osd_oti_get(env)->oti_hlock;
4142 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4143 inode, LDISKFS_HLOCK_READDIR);
4145 down_read(&obj->oo_ext_idx_sem);
4148 result = inode->i_fop->readdir(&it->oie_file, it,
4149 (filldir_t) osd_ldiskfs_filldir);
4152 ldiskfs_htree_unlock(hlock);
4154 up_read(&obj->oo_ext_idx_sem);
4156 if (it->oie_rd_dirent == 0) {
4159 it->oie_dirent = it->oie_buf;
4160 it->oie_it_dirent = 1;
4167 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4168 * to load a directory entry at a time and stored it in
4169 * iterator's in-memory data structure.
4171 * \param di iterator's in memory structure
4173 * \retval +ve iterator reached to end
4174 * \retval 0 iterator not reached to end
4175 * \retval -ve on error
4177 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4179 struct osd_it_ea *it = (struct osd_it_ea *)di;
4184 if (it->oie_it_dirent < it->oie_rd_dirent) {
4186 (void *) it->oie_dirent +
4187 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4188 it->oie_dirent->oied_namelen);
4189 it->oie_it_dirent++;
4192 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4195 rc = osd_ldiskfs_it_fill(env, di);
4202 * Returns the key at current position from iterator's in memory structure.
4204 * \param di iterator's in memory structure
4206 * \retval key i.e. struct dt_key on success
4208 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4209 const struct dt_it *di)
4211 struct osd_it_ea *it = (struct osd_it_ea *)di;
4213 return (struct dt_key *)it->oie_dirent->oied_name;
4217 * Returns the key's size at current position from iterator's in memory structure.
4219 * \param di iterator's in memory structure
4221 * \retval key_size i.e. struct dt_key on success
4223 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4225 struct osd_it_ea *it = (struct osd_it_ea *)di;
4227 return it->oie_dirent->oied_namelen;
4232 * Returns the value (i.e. fid/igif) at current position from iterator's
4233 * in memory structure.
4235 * \param di struct osd_it_ea, iterator's in memory structure
4236 * \param attr attr requested for dirent.
4237 * \param lde lustre dirent
4239 * \retval 0 no error and \param lde has correct lustre dirent.
4240 * \retval -ve on error
4242 static inline int osd_it_ea_rec(const struct lu_env *env,
4243 const struct dt_it *di,
4244 struct dt_rec *dtrec, __u32 attr)
4246 struct osd_it_ea *it = (struct osd_it_ea *)di;
4247 struct osd_object *obj = it->oie_obj;
4248 struct osd_device *dev = osd_obj2dev(obj);
4249 struct osd_scrub *scrub = &dev->od_scrub;
4250 struct scrub_file *sf = &scrub->os_file;
4251 struct osd_thread_info *oti = osd_oti_get(env);
4252 struct osd_idmap_cache *oic = &oti->oti_cache;
4253 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4254 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4255 __u32 ino = it->oie_dirent->oied_ino;
4259 if (!fid_is_sane(fid)) {
4260 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4262 fid_zero(&oic->oic_fid);
4266 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4269 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4270 it->oie_dirent->oied_name,
4271 it->oie_dirent->oied_namelen,
4272 it->oie_dirent->oied_type, attr);
4273 oic->oic_fid = *fid;
4274 if ((scrub->os_pos_current <= ino) &&
4275 (sf->sf_flags & SF_INCONSISTENT ||
4276 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4277 osd_consistency_check(oti, dev, oic);
4283 * Returns a cookie for current position of the iterator head, so that
4284 * user can use this cookie to load/start the iterator next time.
4286 * \param di iterator's in memory structure
4288 * \retval cookie for current position, on success
4290 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4292 struct osd_it_ea *it = (struct osd_it_ea *)di;
4294 return it->oie_dirent->oied_off;
4298 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4299 * to load a directory entry at a time and stored it i inn,
4300 * in iterator's in-memory data structure.
4302 * \param di struct osd_it_ea, iterator's in memory structure
4304 * \retval +ve on success
4305 * \retval -ve on error
4307 static int osd_it_ea_load(const struct lu_env *env,
4308 const struct dt_it *di, __u64 hash)
4310 struct osd_it_ea *it = (struct osd_it_ea *)di;
4314 it->oie_file.f_pos = hash;
4316 rc = osd_ldiskfs_it_fill(env, di);
4324 * Index lookup function for interoperability mode (b11826).
4326 * \param key, key i.e. file name to be searched
4328 * \retval +ve, on success
4329 * \retval -ve, on error
4331 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4332 struct dt_rec *rec, const struct dt_key *key,
4333 struct lustre_capa *capa)
4335 struct osd_object *obj = osd_dt_obj(dt);
4340 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4341 LINVRNT(osd_invariant(obj));
4343 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4346 rc = osd_ea_lookup_rec(env, obj, rec, key);
4353 * Index and Iterator operations for interoperability
4354 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4356 static const struct dt_index_operations osd_index_ea_ops = {
4357 .dio_lookup = osd_index_ea_lookup,
4358 .dio_declare_insert = osd_index_declare_ea_insert,
4359 .dio_insert = osd_index_ea_insert,
4360 .dio_declare_delete = osd_index_declare_ea_delete,
4361 .dio_delete = osd_index_ea_delete,
4363 .init = osd_it_ea_init,
4364 .fini = osd_it_ea_fini,
4365 .get = osd_it_ea_get,
4366 .put = osd_it_ea_put,
4367 .next = osd_it_ea_next,
4368 .key = osd_it_ea_key,
4369 .key_size = osd_it_ea_key_size,
4370 .rec = osd_it_ea_rec,
4371 .store = osd_it_ea_store,
4372 .load = osd_it_ea_load
4376 static void *osd_key_init(const struct lu_context *ctx,
4377 struct lu_context_key *key)
4379 struct osd_thread_info *info;
4381 OBD_ALLOC_PTR(info);
4383 return ERR_PTR(-ENOMEM);
4385 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4386 if (info->oti_it_ea_buf == NULL)
4389 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4391 info->oti_hlock = ldiskfs_htree_lock_alloc();
4392 if (info->oti_hlock == NULL)
4398 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4401 return ERR_PTR(-ENOMEM);
4404 static void osd_key_fini(const struct lu_context *ctx,
4405 struct lu_context_key *key, void* data)
4407 struct osd_thread_info *info = data;
4409 if (info->oti_hlock != NULL)
4410 ldiskfs_htree_lock_free(info->oti_hlock);
4411 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4415 static void osd_key_exit(const struct lu_context *ctx,
4416 struct lu_context_key *key, void *data)
4418 struct osd_thread_info *info = data;
4420 LASSERT(info->oti_r_locks == 0);
4421 LASSERT(info->oti_w_locks == 0);
4422 LASSERT(info->oti_txns == 0);
4425 /* type constructor/destructor: osd_type_init, osd_type_fini */
4426 LU_TYPE_INIT_FINI(osd, &osd_key);
4428 struct lu_context_key osd_key = {
4429 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4430 .lct_init = osd_key_init,
4431 .lct_fini = osd_key_fini,
4432 .lct_exit = osd_key_exit
4436 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4437 const char *name, struct lu_device *next)
4439 struct osd_device *osd = osd_dev(d);
4441 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4442 return osd_procfs_init(osd, name);
4445 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4449 osd_scrub_cleanup(env, o);
4452 fsfilt_put_ops(o->od_fsops);
4456 /* shutdown quota slave instance associated with the device */
4457 if (o->od_quota_slave != NULL) {
4458 qsd_fini(env, o->od_quota_slave);
4459 o->od_quota_slave = NULL;
4465 static int osd_mount(const struct lu_env *env,
4466 struct osd_device *o, struct lustre_cfg *cfg)
4468 const char *name = lustre_cfg_string(cfg, 0);
4469 const char *dev = lustre_cfg_string(cfg, 1);
4471 unsigned long page, s_flags, lmd_flags = 0;
4472 struct page *__page;
4473 struct file_system_type *type;
4474 char *options = NULL;
4479 if (o->od_mnt != NULL)
4482 if (strlen(dev) >= sizeof(o->od_mntdev))
4484 strcpy(o->od_mntdev, dev);
4486 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4487 if (o->od_fsops == NULL) {
4488 CERROR("Can't find fsfilt_ldiskfs\n");
4492 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
4494 GOTO(out, rc = -ENOMEM);
4496 str = lustre_cfg_string(cfg, 2);
4497 s_flags = simple_strtoul(str, NULL, 0);
4498 str = strstr(str, ":");
4500 lmd_flags = simple_strtoul(str + 1, NULL, 0);
4501 opts = lustre_cfg_string(cfg, 3);
4502 page = (unsigned long)cfs_page_address(__page);
4503 options = (char *)page;
4506 strcat(options, "user_xattr,acl");
4508 strcat(options, opts);
4510 /* Glom up mount options */
4511 if (*options != '\0')
4512 strcat(options, ",");
4513 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
4515 type = get_fs_type("ldiskfs");
4517 CERROR("%s: cannot find ldiskfs module\n", name);
4518 GOTO(out, rc = -ENODEV);
4521 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
4522 cfs_module_put(type->owner);
4524 if (IS_ERR(o->od_mnt)) {
4525 rc = PTR_ERR(o->od_mnt);
4526 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
4531 if (lvfs_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
4532 CERROR("%s: underlying device %s is marked as read-only. "
4533 "Setup failed\n", name, dev);
4536 GOTO(out, rc = -EROFS);
4539 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
4540 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
4541 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
4544 GOTO(out, rc = -EINVAL);
4547 ldiskfs_set_inode_state(osd_sb(o)->s_root->d_inode,
4548 LDISKFS_STATE_LUSTRE_NO_OI);
4549 if (lmd_flags & LMD_FLG_NOSCRUB)
4554 OBD_PAGE_FREE(__page);
4556 fsfilt_put_ops(o->od_fsops);
4561 static struct lu_device *osd_device_fini(const struct lu_env *env,
4562 struct lu_device *d)
4567 rc = osd_shutdown(env, osd_dev(d));
4569 osd_obj_map_fini(osd_dev(d));
4571 shrink_dcache_sb(osd_sb(osd_dev(d)));
4572 osd_sync(env, lu2dt_dev(d));
4574 rc = osd_procfs_fini(osd_dev(d));
4576 CERROR("proc fini error %d \n", rc);
4577 RETURN (ERR_PTR(rc));
4580 if (osd_dev(d)->od_mnt) {
4581 mntput(osd_dev(d)->od_mnt);
4582 osd_dev(d)->od_mnt = NULL;
4588 static int osd_device_init0(const struct lu_env *env,
4589 struct osd_device *o,
4590 struct lustre_cfg *cfg)
4592 struct lu_device *l = osd2lu_dev(o);
4593 struct osd_thread_info *info;
4596 /* if the module was re-loaded, env can loose its keys */
4597 rc = lu_env_refill((struct lu_env *) env);
4600 info = osd_oti_get(env);
4603 l->ld_ops = &osd_lu_ops;
4604 o->od_dt_dev.dd_ops = &osd_dt_ops;
4606 spin_lock_init(&o->od_osfs_lock);
4607 mutex_init(&o->od_otable_mutex);
4608 o->od_osfs_age = cfs_time_shift_64(-1000);
4610 o->od_capa_hash = init_capa_hash();
4611 if (o->od_capa_hash == NULL)
4612 GOTO(out, rc = -ENOMEM);
4614 o->od_read_cache = 1;
4615 o->od_writethrough_cache = 1;
4616 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
4618 rc = osd_mount(env, o, cfg);
4622 CFS_INIT_LIST_HEAD(&o->od_ios_list);
4623 /* setup scrub, including OI files initialization */
4624 rc = osd_scrub_setup(env, o);
4628 strncpy(o->od_svname, lustre_cfg_string(cfg, 4),
4629 sizeof(o->od_svname) - 1);
4631 rc = osd_obj_map_init(o);
4633 GOTO(out_scrub, rc);
4635 rc = lu_site_init(&o->od_site, l);
4637 GOTO(out_compat, rc);
4638 o->od_site.ls_bottom_dev = l;
4640 rc = lu_site_init_finish(&o->od_site);
4644 rc = osd_procfs_init(o, o->od_svname);
4646 CERROR("%s: can't initialize procfs: rc = %d\n",
4651 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
4653 /* initialize quota slave instance */
4654 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
4656 if (IS_ERR(o->od_quota_slave)) {
4657 rc = PTR_ERR(o->od_quota_slave);
4658 o->od_quota_slave = NULL;
4659 GOTO(out_procfs, rc);
4666 lu_site_fini(&o->od_site);
4668 osd_obj_map_fini(o);
4670 osd_scrub_cleanup(env, o);
4672 osd_oi_fini(info, o);
4673 osd_shutdown(env, o);
4677 cleanup_capa_hash(o->od_capa_hash);
4682 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4683 struct lu_device_type *t,
4684 struct lustre_cfg *cfg)
4686 struct osd_device *o;
4691 return ERR_PTR(-ENOMEM);
4693 rc = dt_device_init(&o->od_dt_dev, t);
4695 /* Because the ctx might be revived in dt_device_init,
4696 * refill the env here */
4697 lu_env_refill((struct lu_env *)env);
4698 rc = osd_device_init0(env, o, cfg);
4700 dt_device_fini(&o->od_dt_dev);
4703 if (unlikely(rc != 0))
4706 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
4709 static struct lu_device *osd_device_free(const struct lu_env *env,
4710 struct lu_device *d)
4712 struct osd_device *o = osd_dev(d);
4715 cleanup_capa_hash(o->od_capa_hash);
4716 /* XXX: make osd top device in order to release reference */
4717 d->ld_site->ls_top_dev = d;
4718 lu_site_purge(env, d->ld_site, -1);
4719 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
4720 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
4721 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
4723 lu_site_fini(&o->od_site);
4724 dt_device_fini(&o->od_dt_dev);
4729 static int osd_process_config(const struct lu_env *env,
4730 struct lu_device *d, struct lustre_cfg *cfg)
4732 struct osd_device *o = osd_dev(d);
4736 switch(cfg->lcfg_command) {
4738 err = osd_mount(env, o, cfg);
4741 lu_dev_del_linkage(d->ld_site, d);
4742 err = osd_shutdown(env, o);
4751 static int osd_recovery_complete(const struct lu_env *env,
4752 struct lu_device *d)
4754 struct osd_device *osd = osd_dev(d);
4758 if (osd->od_quota_slave == NULL)
4761 /* start qsd instance on recovery completion, this notifies the quota
4762 * slave code that we are about to process new requests now */
4763 rc = qsd_start(env, osd->od_quota_slave);
4768 * we use exports to track all osd users
4770 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
4771 struct obd_device *obd, struct obd_uuid *cluuid,
4772 struct obd_connect_data *data, void *localdata)
4774 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4775 struct lustre_handle conn;
4779 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
4781 rc = class_connect(&conn, obd, cluuid);
4785 *exp = class_conn2export(&conn);
4787 spin_lock(&osd->od_osfs_lock);
4789 spin_unlock(&osd->od_osfs_lock);
4795 * once last export (we don't count self-export) disappeared
4796 * osd can be released
4798 static int osd_obd_disconnect(struct obd_export *exp)
4800 struct obd_device *obd = exp->exp_obd;
4801 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4802 int rc, release = 0;
4805 /* Only disconnect the underlying layers on the final disconnect. */
4806 spin_lock(&osd->od_osfs_lock);
4808 if (osd->od_connects == 0)
4810 spin_unlock(&osd->od_osfs_lock);
4812 rc = class_disconnect(exp); /* bz 9811 */
4814 if (rc == 0 && release)
4815 class_manual_cleanup(obd);
4819 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4820 struct lu_device *dev)
4822 struct osd_device *osd = osd_dev(dev);
4826 if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
4827 /* MDT/MDD still use old infrastructure to create
4829 result = llo_local_objects_setup(env, lu2md_dev(pdev),
4835 if (osd->od_quota_slave != NULL)
4836 /* set up quota slave objects */
4837 result = qsd_prepare(env, osd->od_quota_slave);
4842 static const struct lu_object_operations osd_lu_obj_ops = {
4843 .loo_object_init = osd_object_init,
4844 .loo_object_delete = osd_object_delete,
4845 .loo_object_release = osd_object_release,
4846 .loo_object_free = osd_object_free,
4847 .loo_object_print = osd_object_print,
4848 .loo_object_invariant = osd_object_invariant
4851 const struct lu_device_operations osd_lu_ops = {
4852 .ldo_object_alloc = osd_object_alloc,
4853 .ldo_process_config = osd_process_config,
4854 .ldo_recovery_complete = osd_recovery_complete,
4855 .ldo_prepare = osd_prepare,
4858 static const struct lu_device_type_operations osd_device_type_ops = {
4859 .ldto_init = osd_type_init,
4860 .ldto_fini = osd_type_fini,
4862 .ldto_start = osd_type_start,
4863 .ldto_stop = osd_type_stop,
4865 .ldto_device_alloc = osd_device_alloc,
4866 .ldto_device_free = osd_device_free,
4868 .ldto_device_init = osd_device_init,
4869 .ldto_device_fini = osd_device_fini
4872 struct lu_device_type osd_device_type = {
4873 .ldt_tags = LU_DEVICE_DT,
4874 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
4875 .ldt_ops = &osd_device_type_ops,
4876 .ldt_ctx_tags = LCT_LOCAL,
4880 * lprocfs legacy support.
4882 static struct obd_ops osd_obd_device_ops = {
4883 .o_owner = THIS_MODULE,
4884 .o_connect = osd_obd_connect,
4885 .o_disconnect = osd_obd_disconnect
4888 static int __init osd_mod_init(void)
4890 struct lprocfs_static_vars lvars;
4893 lprocfs_osd_init_vars(&lvars);
4894 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4895 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
4898 static void __exit osd_mod_exit(void)
4900 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
4903 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4904 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
4905 MODULE_LICENSE("GPL");
4907 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);