4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
58 * struct OBD_{ALLOC,FREE}*()
61 #include <obd_support.h>
62 /* struct ptlrpc_thread */
63 #include <lustre_net.h>
64 #include <lustre_fid.h>
66 #include "osd_internal.h"
68 /* llo_* api support */
69 #include <md_object.h>
70 #include <lustre_quota.h>
73 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
74 "ldiskfs with parallel directory operations");
76 static const char dot[] = ".";
77 static const char dotdot[] = "..";
78 static const char remote_obj_dir[] = "REM_OBJ_DIR";
80 static const struct lu_object_operations osd_lu_obj_ops;
81 static const struct dt_object_operations osd_obj_ops;
82 static const struct dt_object_operations osd_obj_ea_ops;
83 static const struct dt_object_operations osd_obj_otable_it_ops;
84 static const struct dt_index_operations osd_index_iam_ops;
85 static const struct dt_index_operations osd_index_ea_ops;
87 #ifdef OSD_TRACK_DECLARES
88 int osd_trans_declare_op2rb[] = {
89 [OSD_OT_ATTR_SET] = OSD_OT_ATTR_SET,
90 [OSD_OT_PUNCH] = OSD_OT_MAX,
91 [OSD_OT_XATTR_SET] = OSD_OT_XATTR_SET,
92 [OSD_OT_CREATE] = OSD_OT_DESTROY,
93 [OSD_OT_DESTROY] = OSD_OT_CREATE,
94 [OSD_OT_REF_ADD] = OSD_OT_REF_DEL,
95 [OSD_OT_REF_DEL] = OSD_OT_REF_ADD,
96 [OSD_OT_WRITE] = OSD_OT_WRITE,
97 [OSD_OT_INSERT] = OSD_OT_DELETE,
98 [OSD_OT_DELETE] = OSD_OT_INSERT,
99 [OSD_OT_QUOTA] = OSD_OT_MAX,
103 static int osd_has_index(const struct osd_object *obj)
105 return obj->oo_dt.do_index_ops != NULL;
108 static int osd_object_invariant(const struct lu_object *l)
110 return osd_invariant(osd_obj(l));
114 * Concurrency: doesn't matter
116 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
118 return osd_oti_get(env)->oti_r_locks > 0;
122 * Concurrency: doesn't matter
124 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
126 struct osd_thread_info *oti = osd_oti_get(env);
127 return oti->oti_w_locks > 0 && o->oo_owner == env;
131 * Concurrency: doesn't access mutable data
133 static int osd_root_get(const struct lu_env *env,
134 struct dt_device *dev, struct lu_fid *f)
136 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
141 * OSD object methods.
145 * Concurrency: no concurrent access is possible that early in object
148 static struct lu_object *osd_object_alloc(const struct lu_env *env,
149 const struct lu_object_header *hdr,
152 struct osd_object *mo;
158 l = &mo->oo_dt.do_lu;
159 dt_object_init(&mo->oo_dt, NULL, d);
160 mo->oo_dt.do_ops = &osd_obj_ea_ops;
161 l->lo_ops = &osd_lu_obj_ops;
162 init_rwsem(&mo->oo_sem);
163 init_rwsem(&mo->oo_ext_idx_sem);
164 spin_lock_init(&mo->oo_guard);
171 static inline int __osd_xattr_get(struct inode *inode, struct dentry *dentry,
172 const char *name, void *buf, int len)
174 dentry->d_inode = inode;
175 dentry->d_sb = inode->i_sb;
176 return inode->i_op->getxattr(dentry, name, buf, len);
179 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
180 struct dentry *dentry, struct lustre_mdt_attrs *lma)
184 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA, (void *)lma,
187 /* try with old lma size */
188 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA,
189 info->oti_mdt_attrs_old,
192 memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
195 /* Check LMA compatibility */
196 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
197 CWARN("%.16s: unsupported incompat LMA feature(s) "
199 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
200 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
204 lustre_lma_swab(lma);
207 } else if (rc == 0) {
215 * retrieve object from backend ext fs.
217 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
218 struct osd_inode_id *id)
220 struct inode *inode = NULL;
222 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
224 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
225 id->oii_ino, PTR_ERR(inode));
226 } else if (id->oii_gen != OSD_OII_NOGEN &&
227 inode->i_generation != id->oii_gen) {
228 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
230 id->oii_ino, id->oii_gen, inode->i_generation);
232 inode = ERR_PTR(-ESTALE);
233 } else if (inode->i_nlink == 0) {
234 /* due to parallel readdir and unlink,
235 * we can have dead inode here. */
236 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
237 make_bad_inode(inode);
239 inode = ERR_PTR(-ESTALE);
240 } else if (is_bad_inode(inode)) {
241 CWARN("%.16s: bad inode: ino = %u\n",
242 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
244 inode = ERR_PTR(-ENOENT);
246 if (id->oii_gen == OSD_OII_NOGEN)
247 osd_id_gen(id, inode->i_ino, inode->i_generation);
249 /* Do not update file c/mtime in ldiskfs.
250 * NB: we don't have any lock to protect this because we don't
251 * have reference on osd_object now, but contention with
252 * another lookup + attr_set can't happen in the tiny window
253 * between if (...) and set S_NOCMTIME. */
254 if (!(inode->i_flags & S_NOCMTIME))
255 inode->i_flags |= S_NOCMTIME;
260 static struct inode *
261 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
262 struct osd_inode_id *id, struct lu_fid *fid)
264 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
268 inode = osd_iget(info, dev, id);
272 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
274 *fid = lma->lma_self_fid;
275 } else if (rc == -ENODATA) {
276 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
277 lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
279 lu_igif_build(fid, inode->i_ino, inode->i_generation);
287 static struct inode *
288 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
289 struct osd_inode_id *id, const struct lu_fid *fid)
291 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
295 inode = osd_iget(info, dev, id);
299 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
308 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
309 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
310 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
312 return ERR_PTR(-EREMCHG);
318 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
319 const struct lu_fid *fid,
320 const struct lu_object_conf *conf)
322 struct osd_thread_info *info;
323 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
324 struct osd_device *dev;
325 struct osd_idmap_cache *oic;
326 struct osd_inode_id *id;
328 struct osd_scrub *scrub;
329 struct scrub_file *sf;
334 LINVRNT(osd_invariant(obj));
335 LASSERT(obj->oo_inode == NULL);
336 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
339 scrub = &dev->od_scrub;
340 sf = &scrub->os_file;
341 info = osd_oti_get(env);
343 oic = &info->oti_cache;
345 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
348 /* Search order: 1. per-thread cache. */
349 if (lu_fid_eq(fid, &oic->oic_fid)) {
355 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
356 /* Search order: 2. OI scrub pending list. */
357 result = osd_oii_lookup(dev, fid, id);
362 if (sf->sf_flags & SF_INCONSISTENT)
366 * Objects are created as locking anchors or place holders for objects
367 * yet to be created. No need to osd_oi_lookup() at here because FID
368 * shouldn't never be re-used, if it's really a duplicate FID from
369 * unexpected reason, we should be able to detect it later by calling
370 * do_create->osd_oi_insert()
372 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
373 GOTO(out, result = 0);
375 /* Search order: 3. OI files. */
376 result = osd_oi_lookup(info, dev, fid, id, true);
377 if (result == -ENOENT) {
378 if (!fid_is_norm(fid) || fid_is_on_ost(info, dev, fid) ||
379 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
381 GOTO(out, result = 0);
391 inode = osd_iget(info, dev, id);
393 inode = osd_iget_verify(info, dev, id, fid);
395 result = PTR_ERR(inode);
396 if (result == -ENOENT || result == -ESTALE) {
397 fid_zero(&oic->oic_fid);
399 } else if (result == -EREMCHG) {
402 if (thread_is_running(&scrub->os_thread)) {
403 result = -EINPROGRESS;
404 } else if (!dev->od_noscrub) {
405 result = osd_scrub_start(dev);
406 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
407 "for "DFID", rc = %d [1]\n",
408 LDISKFS_SB(osd_sb(dev))->s_es->\
409 s_volume_name,PFID(fid), result);
410 if (result == 0 || result == -EALREADY)
411 result = -EINPROGRESS;
420 obj->oo_inode = inode;
421 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
423 obj->oo_compat_dot_created = 1;
424 obj->oo_compat_dotdot_created = 1;
426 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
427 GOTO(out, result = 0);
429 LASSERT(obj->oo_hl_head == NULL);
430 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
431 if (obj->oo_hl_head == NULL) {
432 obj->oo_inode = NULL;
434 GOTO(out, result = -ENOMEM);
436 GOTO(out, result = 0);
439 LINVRNT(osd_invariant(obj));
444 * Concurrency: shouldn't matter.
446 static void osd_object_init0(struct osd_object *obj)
448 LASSERT(obj->oo_inode != NULL);
449 obj->oo_dt.do_body_ops = &osd_body_ops;
450 obj->oo_dt.do_lu.lo_header->loh_attr |=
451 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
455 * Concurrency: no concurrent access is possible that early in object
458 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
459 const struct lu_object_conf *conf)
461 struct osd_object *obj = osd_obj(l);
464 LINVRNT(osd_invariant(obj));
466 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
467 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
468 l->lo_header->loh_attr |= LOHA_EXISTS;
472 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
473 obj->oo_dt.do_body_ops = &osd_body_ops_new;
474 if (result == 0 && obj->oo_inode != NULL)
475 osd_object_init0(obj);
477 LINVRNT(osd_invariant(obj));
482 * Concurrency: no concurrent access is possible that late in object
485 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
487 struct osd_object *obj = osd_obj(l);
489 LINVRNT(osd_invariant(obj));
491 dt_object_fini(&obj->oo_dt);
492 if (obj->oo_hl_head != NULL)
493 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
498 * Concurrency: no concurrent access is possible that late in object
501 static void osd_index_fini(struct osd_object *o)
503 struct iam_container *bag;
505 if (o->oo_dir != NULL) {
506 bag = &o->oo_dir->od_container;
507 if (o->oo_inode != NULL) {
508 if (bag->ic_object == o->oo_inode)
509 iam_container_fini(bag);
511 OBD_FREE_PTR(o->oo_dir);
517 * Concurrency: no concurrent access is possible that late in object
518 * life-cycle (for all existing callers, that is. New callers have to provide
519 * their own locking.)
521 static int osd_inode_unlinked(const struct inode *inode)
523 return inode->i_nlink == 0;
527 OSD_TXN_OI_DELETE_CREDITS = 20,
528 OSD_TXN_INODE_DELETE_CREDITS = 20
535 #if OSD_THANDLE_STATS
537 * Set time when the handle is allocated
539 static void osd_th_alloced(struct osd_thandle *oth)
541 oth->oth_alloced = cfs_time_current();
545 * Set time when the handle started
547 static void osd_th_started(struct osd_thandle *oth)
549 oth->oth_started = cfs_time_current();
553 * Helper function to convert time interval to microseconds packed in
556 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
560 cfs_duration_usec(cfs_time_sub(end, start), &val);
561 return val.tv_sec * 1000000 + val.tv_usec;
565 * Check whether the we deal with this handle for too long.
567 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
568 cfs_time_t alloced, cfs_time_t started,
571 cfs_time_t now = cfs_time_current();
573 LASSERT(dev != NULL);
575 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
576 interval_to_usec(alloced, started));
577 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
578 interval_to_usec(started, closed));
579 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
580 interval_to_usec(closed, now));
582 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
583 CWARN("transaction handle %p was open for too long: "
585 "alloced "CFS_TIME_T" ,"
586 "started "CFS_TIME_T" ,"
587 "closed "CFS_TIME_T"\n",
588 oth, now, alloced, started, closed);
589 libcfs_debug_dumpstack(NULL);
593 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
595 cfs_time_t __closed = cfs_time_current(); \
596 cfs_time_t __alloced = oth->oth_alloced; \
597 cfs_time_t __started = oth->oth_started; \
600 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
603 #else /* OSD_THANDLE_STATS */
605 #define osd_th_alloced(h) do {} while(0)
606 #define osd_th_started(h) do {} while(0)
607 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
609 #endif /* OSD_THANDLE_STATS */
612 * Concurrency: doesn't access mutable data.
614 static int osd_param_is_not_sane(const struct osd_device *dev,
615 const struct thandle *th)
617 struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
619 return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
623 * Concurrency: shouldn't matter.
625 static void osd_trans_commit_cb(struct super_block *sb,
626 struct ldiskfs_journal_cb_entry *jcb, int error)
628 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
629 struct thandle *th = &oh->ot_super;
630 struct lu_device *lud = &th->th_dev->dd_lu_dev;
631 struct dt_txn_commit_cb *dcb, *tmp;
633 LASSERT(oh->ot_handle == NULL);
636 CERROR("transaction @0x%p commit error: %d\n", th, error);
638 dt_txn_hook_commit(th);
640 /* call per-transaction callbacks if any */
641 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
642 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
643 "commit callback entry: magic=%x name='%s'\n",
644 dcb->dcb_magic, dcb->dcb_name);
645 cfs_list_del_init(&dcb->dcb_linkage);
646 dcb->dcb_func(NULL, th, dcb, error);
649 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
653 lu_context_exit(&th->th_ctx);
654 lu_context_fini(&th->th_ctx);
658 static struct thandle *osd_trans_create(const struct lu_env *env,
661 struct osd_thread_info *oti = osd_oti_get(env);
662 struct osd_iobuf *iobuf = &oti->oti_iobuf;
663 struct osd_thandle *oh;
667 /* on pending IO in this thread should left from prev. request */
668 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
670 th = ERR_PTR(-ENOMEM);
671 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
673 oh->ot_quota_trans = &oti->oti_quota_trans;
674 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
678 th->th_tags = LCT_TX_HANDLE;
680 oti->oti_dev = osd_dt_dev(d);
681 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
684 #ifdef OSD_TRACK_DECLARES
685 memset(oti->oti_declare_ops, 0,
686 sizeof(oti->oti_declare_ops));
687 memset(oti->oti_declare_ops_rb, 0,
688 sizeof(oti->oti_declare_ops_rb));
689 memset(oti->oti_declare_ops_cred, 0,
690 sizeof(oti->oti_declare_ops_cred));
691 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 #ifdef OSD_TRACK_DECLARES
723 static unsigned long last_printed;
724 static int last_credits;
727 CWARN("%.16s: too many transaction credits (%d > %d)\n",
728 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
730 osd_journal(dev)->j_max_transaction_buffers);
731 #ifdef OSD_TRACK_DECLARES
732 CWARN(" create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
733 oti->oti_declare_ops[OSD_OT_CREATE],
734 oti->oti_declare_ops_cred[OSD_OT_CREATE],
735 oti->oti_declare_ops[OSD_OT_DELETE],
736 oti->oti_declare_ops_cred[OSD_OT_DELETE],
737 oti->oti_declare_ops[OSD_OT_DESTROY],
738 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
739 CWARN(" attr_set: %u/%u, xattr_set: %u/%u\n",
740 oti->oti_declare_ops[OSD_OT_ATTR_SET],
741 oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
742 oti->oti_declare_ops[OSD_OT_XATTR_SET],
743 oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
744 CWARN(" write: %u/%u, punch: %u/%u, quota %u/%u\n",
745 oti->oti_declare_ops[OSD_OT_WRITE],
746 oti->oti_declare_ops_cred[OSD_OT_WRITE],
747 oti->oti_declare_ops[OSD_OT_PUNCH],
748 oti->oti_declare_ops_cred[OSD_OT_PUNCH],
749 oti->oti_declare_ops[OSD_OT_QUOTA],
750 oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
751 CWARN(" insert: %u/%u, delete: %u/%u\n",
752 oti->oti_declare_ops[OSD_OT_INSERT],
753 oti->oti_declare_ops_cred[OSD_OT_INSERT],
754 oti->oti_declare_ops[OSD_OT_DESTROY],
755 oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
756 CWARN(" ref_add: %u/%u, ref_del: %u/%u\n",
757 oti->oti_declare_ops[OSD_OT_REF_ADD],
758 oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
759 oti->oti_declare_ops[OSD_OT_REF_DEL],
760 oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
762 if (last_credits != oh->ot_credits &&
763 time_after(jiffies, last_printed + 60 * HZ)) {
764 libcfs_debug_dumpstack(NULL);
765 last_credits = oh->ot_credits;
766 last_printed = jiffies;
769 /* XXX Limit the credits to 'max_transaction_buffers', and
770 * let the underlying filesystem to catch the error if
771 * we really need so many credits.
773 * This should be removed when we can calculate the
774 * credits precisely. */
775 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
779 * XXX temporary stuff. Some abstraction layer should
782 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
786 LASSERT(oti->oti_txns == 0);
787 lu_context_init(&th->th_ctx, th->th_tags);
788 lu_context_enter(&th->th_ctx);
790 lu_device_get(&d->dd_lu_dev);
791 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
803 * Concurrency: shouldn't matter.
805 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
808 struct osd_thandle *oh;
809 struct osd_thread_info *oti = osd_oti_get(env);
810 struct osd_iobuf *iobuf = &oti->oti_iobuf;
811 struct qsd_instance *qsd = oti->oti_dev->od_quota_slave;
814 oh = container_of0(th, struct osd_thandle, ot_super);
817 /* inform the quota slave device that the transaction is
819 qsd_op_end(env, qsd, oh->ot_quota_trans);
820 oh->ot_quota_trans = NULL;
822 if (oh->ot_handle != NULL) {
823 handle_t *hdl = oh->ot_handle;
826 * add commit callback
827 * notice we don't do this in osd_trans_start()
828 * as underlying transaction can change during truncate
830 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
833 LASSERT(oti->oti_txns == 1);
835 rc = dt_txn_hook_stop(env, th);
837 CERROR("Failure in transaction hook: %d\n", rc);
839 /* hook functions might modify th_sync */
840 hdl->h_sync = th->th_sync;
842 oh->ot_handle = NULL;
843 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
844 rc = ldiskfs_journal_stop(hdl));
846 CERROR("Failure to stop transaction: %d\n", rc);
851 /* as we want IO to journal and data IO be concurrent, we don't block
852 * awaiting data IO completion in osd_do_bio(), instead we wait here
853 * once transaction is submitted to the journal. all reqular requests
854 * don't do direct IO (except read/write), thus this wait_event becomes
857 * IMPORTANT: we have to wait till any IO submited by the thread is
858 * completed otherwise iobuf may be corrupted by different request
860 cfs_wait_event(iobuf->dr_wait,
861 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
863 rc = iobuf->dr_error;
868 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
870 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
873 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
874 LASSERT(&dcb->dcb_func != NULL);
875 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
881 * Called just before object is freed. Releases all resources except for
882 * object itself (that is released by osd_object_free()).
884 * Concurrency: no concurrent access is possible that late in object
887 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
889 struct osd_object *obj = osd_obj(l);
890 struct inode *inode = obj->oo_inode;
892 LINVRNT(osd_invariant(obj));
895 * If object is unlinked remove fid->ino mapping from object index.
900 struct qsd_instance *qsd = osd_obj2dev(obj)->od_quota_slave;
901 qid_t uid = inode->i_uid;
902 qid_t gid = inode->i_gid;
905 obj->oo_inode = NULL;
908 struct osd_thread_info *info = osd_oti_get(env);
909 struct lquota_id_info *qi = &info->oti_qi;
911 /* Release granted quota to master if necessary */
912 qi->lqi_id.qid_uid = uid;
913 qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
915 qi->lqi_id.qid_uid = gid;
916 qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
922 * Concurrency: ->loo_object_release() is called under site spin-lock.
924 static void osd_object_release(const struct lu_env *env,
930 * Concurrency: shouldn't matter.
932 static int osd_object_print(const struct lu_env *env, void *cookie,
933 lu_printer_t p, const struct lu_object *l)
935 struct osd_object *o = osd_obj(l);
938 if (o->oo_dir != NULL)
939 d = o->oo_dir->od_container.ic_descr;
942 return (*p)(env, cookie,
943 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
945 o->oo_inode ? o->oo_inode->i_ino : 0UL,
946 o->oo_inode ? o->oo_inode->i_generation : 0,
947 d ? d->id_ops->id_name : "plain");
951 * Concurrency: shouldn't matter.
953 int osd_statfs(const struct lu_env *env, struct dt_device *d,
954 struct obd_statfs *sfs)
956 struct osd_device *osd = osd_dt_dev(d);
957 struct super_block *sb = osd_sb(osd);
958 struct kstatfs *ksfs;
961 if (unlikely(osd->od_mnt == NULL))
964 /* osd_lproc.c call this without env, allocate ksfs for that case */
965 if (unlikely(env == NULL)) {
970 ksfs = &osd_oti_get(env)->oti_ksfs;
973 spin_lock(&osd->od_osfs_lock);
975 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
976 result = sb->s_op->statfs(sb->s_root, ksfs);
977 if (likely(result == 0)) { /* N.B. statfs can't really fail */
978 osd->od_osfs_age = cfs_time_current_64();
979 statfs_pack(&osd->od_statfs, ksfs);
980 if (sb->s_flags & MS_RDONLY)
981 sfs->os_state = OS_STATE_READONLY;
985 if (likely(result == 0))
986 *sfs = osd->od_statfs;
987 spin_unlock(&osd->od_osfs_lock);
989 if (unlikely(env == NULL))
996 * Estimate space needed for file creations. We assume the largest filename
997 * which is 2^64 - 1, hence a filename of 20 chars.
998 * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1000 #ifdef __LDISKFS_DIR_REC_LEN
1001 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1003 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1007 * Concurrency: doesn't access mutable data.
1009 static void osd_conf_get(const struct lu_env *env,
1010 const struct dt_device *dev,
1011 struct dt_device_param *param)
1013 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1016 * XXX should be taken from not-yet-existing fs abstraction layer.
1018 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1019 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1020 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1021 param->ddp_block_shift = sb->s_blocksize_bits;
1022 param->ddp_mount_type = LDD_MT_LDISKFS;
1023 param->ddp_maxbytes = sb->s_maxbytes;
1024 /* Overhead estimate should be fairly accurate, so we really take a tiny
1025 * error margin which also avoids fragmenting the filesystem too much */
1026 param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1027 /* inode are statically allocated, so per-inode space consumption
1028 * is the space consumed by the directory entry */
1029 param->ddp_inodespace = PER_OBJ_USAGE;
1030 /* per-fragment overhead to be used by the client code */
1031 param->ddp_grant_frag = 6 * LDISKFS_BLOCK_SIZE(sb);
1032 param->ddp_mntopts = 0;
1033 if (test_opt(sb, XATTR_USER))
1034 param->ddp_mntopts |= MNTOPT_USERXATTR;
1035 if (test_opt(sb, POSIX_ACL))
1036 param->ddp_mntopts |= MNTOPT_ACL;
1038 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1039 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1040 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1043 param->ddp_max_ea_size = sb->s_blocksize;
1048 * Concurrency: shouldn't matter.
1050 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1052 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1053 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1057 * Start commit for OSD device.
1059 * An implementation of dt_commit_async method for OSD device.
1060 * Asychronously starts underlayng fs sync and thereby a transaction
1063 * \param env environment
1064 * \param d dt device
1066 * \see dt_device_operations
1068 static int osd_commit_async(const struct lu_env *env,
1069 struct dt_device *d)
1071 struct super_block *s = osd_sb(osd_dt_dev(d));
1074 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1075 RETURN(s->s_op->sync_fs(s, 0));
1079 * Concurrency: shouldn't matter.
1082 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1084 struct super_block *sb = osd_sb(osd_dt_dev(d));
1085 struct block_device *dev = sb->s_bdev;
1086 #ifdef HAVE_DEV_SET_RDONLY
1087 struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1090 int rc = -EOPNOTSUPP;
1094 #ifdef HAVE_DEV_SET_RDONLY
1095 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1097 if (jdev && (jdev != dev)) {
1098 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1100 dev_set_rdonly(jdev);
1102 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1103 dev_set_rdonly(dev);
1105 CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1106 osd_dt_dev(d)->od_svname, (long)dev, rc);
1112 * Concurrency: serialization provided by callers.
1114 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1115 int mode, unsigned long timeout, __u32 alg,
1116 struct lustre_capa_key *keys)
1118 struct osd_device *dev = osd_dt_dev(d);
1121 dev->od_fl_capa = mode;
1122 dev->od_capa_timeout = timeout;
1123 dev->od_capa_alg = alg;
1124 dev->od_capa_keys = keys;
1129 * Note: we do not count into QUOTA here.
1130 * If we mount with --data_journal we may need more.
1132 const int osd_dto_credits_noquota[DTO_NR] = {
1135 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1136 * SINGLEDATA_TRANS_BLOCKS(8)
1137 * XXX Note: maybe iam need more, since iam have more level than
1140 [DTO_INDEX_INSERT] = 16,
1141 [DTO_INDEX_DELETE] = 16,
1145 [DTO_INDEX_UPDATE] = 16,
1147 * Create a object. The same as create object in EXT3.
1148 * DATA_TRANS_BLOCKS(14) +
1149 * INDEX_EXTRA_BLOCKS(8) +
1150 * 3(inode bits, groups, GDT)
1152 [DTO_OBJECT_CREATE] = 25,
1154 * XXX: real credits to be fixed
1156 [DTO_OBJECT_DELETE] = 25,
1158 * Attr set credits (inode)
1160 [DTO_ATTR_SET_BASE] = 1,
1162 * Xattr set. The same as xattr of EXT3.
1163 * DATA_TRANS_BLOCKS(14)
1164 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1165 * are also counted in. Do not know why?
1167 [DTO_XATTR_SET] = 14,
1170 * credits for inode change during write.
1172 [DTO_WRITE_BASE] = 3,
1174 * credits for single block write.
1176 [DTO_WRITE_BLOCK] = 14,
1178 * Attr set credits for chown.
1179 * This is extra credits for setattr, and it is null without quota
1181 [DTO_ATTR_SET_CHOWN]= 0
1184 static const struct dt_device_operations osd_dt_ops = {
1185 .dt_root_get = osd_root_get,
1186 .dt_statfs = osd_statfs,
1187 .dt_trans_create = osd_trans_create,
1188 .dt_trans_start = osd_trans_start,
1189 .dt_trans_stop = osd_trans_stop,
1190 .dt_trans_cb_add = osd_trans_cb_add,
1191 .dt_conf_get = osd_conf_get,
1192 .dt_sync = osd_sync,
1194 .dt_commit_async = osd_commit_async,
1195 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1198 static void osd_object_read_lock(const struct lu_env *env,
1199 struct dt_object *dt, unsigned role)
1201 struct osd_object *obj = osd_dt_obj(dt);
1202 struct osd_thread_info *oti = osd_oti_get(env);
1204 LINVRNT(osd_invariant(obj));
1206 LASSERT(obj->oo_owner != env);
1207 down_read_nested(&obj->oo_sem, role);
1209 LASSERT(obj->oo_owner == NULL);
1213 static void osd_object_write_lock(const struct lu_env *env,
1214 struct dt_object *dt, unsigned role)
1216 struct osd_object *obj = osd_dt_obj(dt);
1217 struct osd_thread_info *oti = osd_oti_get(env);
1219 LINVRNT(osd_invariant(obj));
1221 LASSERT(obj->oo_owner != env);
1222 down_write_nested(&obj->oo_sem, role);
1224 LASSERT(obj->oo_owner == NULL);
1225 obj->oo_owner = env;
1229 static void osd_object_read_unlock(const struct lu_env *env,
1230 struct dt_object *dt)
1232 struct osd_object *obj = osd_dt_obj(dt);
1233 struct osd_thread_info *oti = osd_oti_get(env);
1235 LINVRNT(osd_invariant(obj));
1237 LASSERT(oti->oti_r_locks > 0);
1239 up_read(&obj->oo_sem);
1242 static void osd_object_write_unlock(const struct lu_env *env,
1243 struct dt_object *dt)
1245 struct osd_object *obj = osd_dt_obj(dt);
1246 struct osd_thread_info *oti = osd_oti_get(env);
1248 LINVRNT(osd_invariant(obj));
1250 LASSERT(obj->oo_owner == env);
1251 LASSERT(oti->oti_w_locks > 0);
1253 obj->oo_owner = NULL;
1254 up_write(&obj->oo_sem);
1257 static int osd_object_write_locked(const struct lu_env *env,
1258 struct dt_object *dt)
1260 struct osd_object *obj = osd_dt_obj(dt);
1262 LINVRNT(osd_invariant(obj));
1264 return obj->oo_owner == env;
1267 static int capa_is_sane(const struct lu_env *env,
1268 struct osd_device *dev,
1269 struct lustre_capa *capa,
1270 struct lustre_capa_key *keys)
1272 struct osd_thread_info *oti = osd_oti_get(env);
1273 struct lustre_capa *tcapa = &oti->oti_capa;
1274 struct obd_capa *oc;
1278 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1280 if (capa_is_expired(oc)) {
1281 DEBUG_CAPA(D_ERROR, capa, "expired");
1288 if (capa_is_expired_sec(capa)) {
1289 DEBUG_CAPA(D_ERROR, capa, "expired");
1293 spin_lock(&capa_lock);
1294 for (i = 0; i < 2; i++) {
1295 if (keys[i].lk_keyid == capa->lc_keyid) {
1296 oti->oti_capa_key = keys[i];
1300 spin_unlock(&capa_lock);
1303 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1307 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1311 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1312 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1316 oc = capa_add(dev->od_capa_hash, capa);
1322 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1323 struct lustre_capa *capa, __u64 opc)
1325 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1326 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1327 struct md_capainfo *ci;
1330 if (!dev->od_fl_capa)
1333 if (capa == BYPASS_CAPA)
1336 ci = md_capainfo(env);
1340 if (ci->mc_auth == LC_ID_NONE)
1344 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1348 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1349 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1354 if (!capa_opc_supported(capa, opc)) {
1355 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1359 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1360 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1367 static struct timespec *osd_inode_time(const struct lu_env *env,
1368 struct inode *inode, __u64 seconds)
1370 struct osd_thread_info *oti = osd_oti_get(env);
1371 struct timespec *t = &oti->oti_time;
1373 t->tv_sec = seconds;
1375 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1380 static void osd_inode_getattr(const struct lu_env *env,
1381 struct inode *inode, struct lu_attr *attr)
1383 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1384 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1385 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1388 attr->la_atime = LTIME_S(inode->i_atime);
1389 attr->la_mtime = LTIME_S(inode->i_mtime);
1390 attr->la_ctime = LTIME_S(inode->i_ctime);
1391 attr->la_mode = inode->i_mode;
1392 attr->la_size = i_size_read(inode);
1393 attr->la_blocks = inode->i_blocks;
1394 attr->la_uid = inode->i_uid;
1395 attr->la_gid = inode->i_gid;
1396 attr->la_flags = LDISKFS_I(inode)->i_flags;
1397 attr->la_nlink = inode->i_nlink;
1398 attr->la_rdev = inode->i_rdev;
1399 attr->la_blksize = 1 << inode->i_blkbits;
1400 attr->la_blkbits = inode->i_blkbits;
1403 static int osd_attr_get(const struct lu_env *env,
1404 struct dt_object *dt,
1405 struct lu_attr *attr,
1406 struct lustre_capa *capa)
1408 struct osd_object *obj = osd_dt_obj(dt);
1410 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1411 LINVRNT(osd_invariant(obj));
1413 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1416 spin_lock(&obj->oo_guard);
1417 osd_inode_getattr(env, obj->oo_inode, attr);
1418 spin_unlock(&obj->oo_guard);
1422 static int osd_declare_attr_set(const struct lu_env *env,
1423 struct dt_object *dt,
1424 const struct lu_attr *attr,
1425 struct thandle *handle)
1427 struct osd_thandle *oh;
1428 struct osd_object *obj;
1429 struct osd_thread_info *info = osd_oti_get(env);
1430 struct lquota_id_info *qi = &info->oti_qi;
1436 LASSERT(dt != NULL);
1437 LASSERT(handle != NULL);
1439 obj = osd_dt_obj(dt);
1440 LASSERT(osd_invariant(obj));
1442 oh = container_of0(handle, struct osd_thandle, ot_super);
1443 LASSERT(oh->ot_handle == NULL);
1445 osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1446 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1448 if (attr == NULL || obj->oo_inode == NULL)
1451 bspace = obj->oo_inode->i_blocks;
1452 bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1453 bspace = toqb(bspace);
1455 /* Changing ownership is always preformed by super user, it should not
1458 * We still need to call the osd_declare_qid() to calculate the journal
1459 * credits for updating quota accounting files and to trigger quota
1460 * space adjustment once the operation is completed.*/
1461 if ((attr->la_valid & LA_UID) != 0 &&
1462 attr->la_uid != obj->oo_inode->i_uid) {
1463 qi->lqi_type = USRQUOTA;
1465 /* inode accounting */
1466 qi->lqi_is_blk = false;
1468 /* one more inode for the new owner ... */
1469 qi->lqi_id.qid_uid = attr->la_uid;
1471 allocated = (attr->la_uid == 0) ? true : false;
1472 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1473 if (rc == -EDQUOT || rc == -EINPROGRESS)
1478 /* and one less inode for the current uid */
1479 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1481 rc = osd_declare_qid(env, oh, qi, true, NULL);
1482 if (rc == -EDQUOT || rc == -EINPROGRESS)
1487 /* block accounting */
1488 qi->lqi_is_blk = true;
1490 /* more blocks for the new owner ... */
1491 qi->lqi_id.qid_uid = attr->la_uid;
1492 qi->lqi_space = bspace;
1493 allocated = (attr->la_uid == 0) ? true : false;
1494 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1495 if (rc == -EDQUOT || rc == -EINPROGRESS)
1500 /* and finally less blocks for the current owner */
1501 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1502 qi->lqi_space = -bspace;
1503 rc = osd_declare_qid(env, oh, qi, true, NULL);
1504 if (rc == -EDQUOT || rc == -EINPROGRESS)
1510 if (attr->la_valid & LA_GID &&
1511 attr->la_gid != obj->oo_inode->i_gid) {
1512 qi->lqi_type = GRPQUOTA;
1514 /* inode accounting */
1515 qi->lqi_is_blk = false;
1517 /* one more inode for the new group owner ... */
1518 qi->lqi_id.qid_gid = attr->la_gid;
1520 allocated = (attr->la_gid == 0) ? true : false;
1521 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1522 if (rc == -EDQUOT || rc == -EINPROGRESS)
1527 /* and one less inode for the current gid */
1528 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1530 rc = osd_declare_qid(env, oh, qi, true, NULL);
1531 if (rc == -EDQUOT || rc == -EINPROGRESS)
1536 /* block accounting */
1537 qi->lqi_is_blk = true;
1539 /* more blocks for the new owner ... */
1540 qi->lqi_id.qid_gid = attr->la_gid;
1541 qi->lqi_space = bspace;
1542 allocated = (attr->la_gid == 0) ? true : false;
1543 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1544 if (rc == -EDQUOT || rc == -EINPROGRESS)
1549 /* and finally less blocks for the current owner */
1550 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1551 qi->lqi_space = -bspace;
1552 rc = osd_declare_qid(env, oh, qi, true, NULL);
1553 if (rc == -EDQUOT || rc == -EINPROGRESS)
1562 static int osd_inode_setattr(const struct lu_env *env,
1563 struct inode *inode, const struct lu_attr *attr)
1567 bits = attr->la_valid;
1569 if (bits & LA_ATIME)
1570 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1571 if (bits & LA_CTIME)
1572 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1573 if (bits & LA_MTIME)
1574 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1575 if (bits & LA_SIZE) {
1576 LDISKFS_I(inode)->i_disksize = attr->la_size;
1577 i_size_write(inode, attr->la_size);
1581 /* OSD should not change "i_blocks" which is used by quota.
1582 * "i_blocks" should be changed by ldiskfs only. */
1583 if (bits & LA_BLOCKS)
1584 inode->i_blocks = attr->la_blocks;
1587 inode->i_mode = (inode->i_mode & S_IFMT) |
1588 (attr->la_mode & ~S_IFMT);
1590 inode->i_uid = attr->la_uid;
1592 inode->i_gid = attr->la_gid;
1593 if (bits & LA_NLINK)
1594 set_nlink(inode, attr->la_nlink);
1596 inode->i_rdev = attr->la_rdev;
1598 if (bits & LA_FLAGS) {
1599 /* always keep S_NOCMTIME */
1600 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1606 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1608 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1609 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1614 if (attr->la_valid & LA_UID)
1615 iattr.ia_valid |= ATTR_UID;
1616 if (attr->la_valid & LA_GID)
1617 iattr.ia_valid |= ATTR_GID;
1618 iattr.ia_uid = attr->la_uid;
1619 iattr.ia_gid = attr->la_gid;
1621 rc = ll_vfs_dq_transfer(inode, &iattr);
1623 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1624 "enforcement enabled on the ldiskfs filesystem?",
1625 inode->i_sb->s_id, rc);
1632 static int osd_attr_set(const struct lu_env *env,
1633 struct dt_object *dt,
1634 const struct lu_attr *attr,
1635 struct thandle *handle,
1636 struct lustre_capa *capa)
1638 struct osd_object *obj = osd_dt_obj(dt);
1639 struct inode *inode;
1642 LASSERT(handle != NULL);
1643 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1644 LASSERT(osd_invariant(obj));
1646 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1649 osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1651 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1652 struct osd_thread_info *oti = osd_oti_get(env);
1653 const struct lu_fid *fid0 = lu_object_fid(&dt->do_lu);
1654 struct lu_fid *fid1 = &oti->oti_fid;
1655 struct osd_inode_id *id = &oti->oti_id;
1656 struct iam_path_descr *ipd;
1657 struct iam_container *bag;
1658 struct osd_thandle *oh;
1661 fid_cpu_to_be(fid1, fid0);
1662 memset(id, 1, sizeof(*id));
1663 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1664 fid0)->oi_dir.od_container;
1665 ipd = osd_idx_ipd_get(env, bag);
1666 if (unlikely(ipd == NULL))
1669 oh = container_of0(handle, struct osd_thandle, ot_super);
1670 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1671 (const struct iam_rec *)id, ipd);
1672 osd_ipd_put(env, bag, ipd);
1673 return(rc > 0 ? 0 : rc);
1676 inode = obj->oo_inode;
1677 ll_vfs_dq_init(inode);
1679 rc = osd_quota_transfer(inode, attr);
1683 spin_lock(&obj->oo_guard);
1684 rc = osd_inode_setattr(env, inode, attr);
1685 spin_unlock(&obj->oo_guard);
1688 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1692 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1693 struct osd_object *obj,
1694 const char *name, const int namelen)
1696 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1699 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1701 struct dt_allocation_hint *hint,
1705 struct osd_device *osd = osd_obj2dev(obj);
1706 struct osd_thandle *oth;
1707 struct dt_object *parent = NULL;
1708 struct inode *inode;
1710 LINVRNT(osd_invariant(obj));
1711 LASSERT(obj->oo_inode == NULL);
1712 LASSERT(obj->oo_hl_head == NULL);
1714 if (S_ISDIR(mode) && ldiskfs_pdo) {
1715 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1716 if (obj->oo_hl_head == NULL)
1720 oth = container_of(th, struct osd_thandle, ot_super);
1721 LASSERT(oth->ot_handle->h_transaction != NULL);
1723 if (hint && hint->dah_parent)
1724 parent = hint->dah_parent;
1726 inode = ldiskfs_create_inode(oth->ot_handle,
1727 parent ? osd_dt_obj(parent)->oo_inode :
1728 osd_sb(osd)->s_root->d_inode,
1730 if (!IS_ERR(inode)) {
1731 /* Do not update file c/mtime in ldiskfs.
1732 * NB: don't need any lock because no contention at this
1734 inode->i_flags |= S_NOCMTIME;
1736 /* For new created object, it must be consistent,
1737 * and it is unnecessary to scrub against it. */
1738 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1739 obj->oo_inode = inode;
1742 if (obj->oo_hl_head != NULL) {
1743 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1744 obj->oo_hl_head = NULL;
1746 result = PTR_ERR(inode);
1748 LINVRNT(osd_invariant(obj));
1756 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1757 struct lu_attr *attr,
1758 struct dt_allocation_hint *hint,
1759 struct dt_object_format *dof,
1763 struct osd_thandle *oth;
1764 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1766 LASSERT(S_ISDIR(attr->la_mode));
1768 oth = container_of(th, struct osd_thandle, ot_super);
1769 LASSERT(oth->ot_handle->h_transaction != NULL);
1770 result = osd_mkfile(info, obj, mode, hint, th);
1775 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1776 struct lu_attr *attr,
1777 struct dt_allocation_hint *hint,
1778 struct dt_object_format *dof,
1782 struct osd_thandle *oth;
1783 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1785 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1787 LASSERT(S_ISREG(attr->la_mode));
1789 oth = container_of(th, struct osd_thandle, ot_super);
1790 LASSERT(oth->ot_handle->h_transaction != NULL);
1792 result = osd_mkfile(info, obj, mode, hint, th);
1794 LASSERT(obj->oo_inode != NULL);
1795 if (feat->dif_flags & DT_IND_VARKEY)
1796 result = iam_lvar_create(obj->oo_inode,
1797 feat->dif_keysize_max,
1799 feat->dif_recsize_max,
1802 result = iam_lfix_create(obj->oo_inode,
1803 feat->dif_keysize_max,
1805 feat->dif_recsize_max,
1812 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1813 struct lu_attr *attr,
1814 struct dt_allocation_hint *hint,
1815 struct dt_object_format *dof,
1818 LASSERT(S_ISREG(attr->la_mode));
1819 return osd_mkfile(info, obj, (attr->la_mode &
1820 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1823 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1824 struct lu_attr *attr,
1825 struct dt_allocation_hint *hint,
1826 struct dt_object_format *dof,
1829 LASSERT(S_ISLNK(attr->la_mode));
1830 return osd_mkfile(info, obj, (attr->la_mode &
1831 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1834 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1835 struct lu_attr *attr,
1836 struct dt_allocation_hint *hint,
1837 struct dt_object_format *dof,
1840 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1843 LINVRNT(osd_invariant(obj));
1844 LASSERT(obj->oo_inode == NULL);
1845 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1846 S_ISFIFO(mode) || S_ISSOCK(mode));
1848 result = osd_mkfile(info, obj, mode, hint, th);
1850 LASSERT(obj->oo_inode != NULL);
1852 * This inode should be marked dirty for i_rdev. Currently
1853 * that is done in the osd_attr_init().
1855 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1858 LINVRNT(osd_invariant(obj));
1862 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1864 struct dt_allocation_hint *hint,
1865 struct dt_object_format *dof,
1868 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1870 osd_obj_type_f result;
1886 result = osd_mk_index;
1897 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1898 struct dt_object *parent, struct dt_object *child,
1899 cfs_umode_t child_mode)
1903 memset(ah, 0, sizeof(*ah));
1904 ah->dah_parent = parent;
1905 ah->dah_mode = child_mode;
1908 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1909 struct lu_attr *attr, struct dt_object_format *dof)
1911 struct inode *inode = obj->oo_inode;
1912 __u64 valid = attr->la_valid;
1915 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1917 if (dof->dof_type != DFT_NODE)
1918 attr->la_valid &= ~LA_RDEV;
1919 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1920 attr->la_valid &= ~LA_ATIME;
1921 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1922 attr->la_valid &= ~LA_CTIME;
1923 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1924 attr->la_valid &= ~LA_MTIME;
1926 result = osd_quota_transfer(inode, attr);
1930 if (attr->la_valid != 0) {
1931 result = osd_inode_setattr(info->oti_env, inode, attr);
1933 * The osd_inode_setattr() should always succeed here. The
1934 * only error that could be returned is EDQUOT when we are
1935 * trying to change the UID or GID of the inode. However, this
1936 * should not happen since quota enforcement is no longer
1937 * enabled on ldiskfs (lquota takes care of it).
1939 LASSERTF(result == 0, "%d", result);
1940 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1943 attr->la_valid = valid;
1947 * Helper function for osd_object_create()
1949 * \retval 0, on success
1951 static int __osd_object_create(struct osd_thread_info *info,
1952 struct osd_object *obj, struct lu_attr *attr,
1953 struct dt_allocation_hint *hint,
1954 struct dt_object_format *dof,
1960 /* we drop umask so that permissions we pass are not affected */
1961 umask = current->fs->umask;
1962 current->fs->umask = 0;
1964 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1967 osd_attr_init(info, obj, attr, dof);
1968 osd_object_init0(obj);
1970 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1971 unlock_new_inode(obj->oo_inode);
1974 /* restore previous umask value */
1975 current->fs->umask = umask;
1981 * Helper function for osd_object_create()
1983 * \retval 0, on success
1985 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1986 const struct lu_fid *fid, struct thandle *th)
1988 struct osd_thread_info *info = osd_oti_get(env);
1989 struct osd_inode_id *id = &info->oti_id;
1990 struct osd_device *osd = osd_obj2dev(obj);
1992 LASSERT(obj->oo_inode != NULL);
1994 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1995 return osd_oi_insert(info, osd, fid, id, th);
1998 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
1999 const struct lu_fid *fid, struct lu_seq_range *range)
2001 struct seq_server_site *ss = osd_seq_site(osd);
2004 if (fid_is_idif(fid)) {
2005 range->lsr_flags = LU_SEQ_RANGE_OST;
2006 range->lsr_index = fid_idif_ost_idx(fid);
2010 if (!fid_seq_in_fldb(fid_seq(fid))) {
2011 range->lsr_flags = LU_SEQ_RANGE_MDT;
2013 /* FIXME: If ss is NULL, it suppose not get lsr_index
2015 range->lsr_index = ss->ss_node_id;
2019 LASSERT(ss != NULL);
2020 range->lsr_flags = -1;
2021 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
2023 CERROR("%s can not find "DFID": rc = %d\n",
2024 osd_name(osd), PFID(fid), rc);
2030 * Concurrency: no external locking is necessary.
2032 static int osd_declare_object_create(const struct lu_env *env,
2033 struct dt_object *dt,
2034 struct lu_attr *attr,
2035 struct dt_allocation_hint *hint,
2036 struct dt_object_format *dof,
2037 struct thandle *handle)
2039 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
2040 struct osd_thandle *oh;
2044 LASSERT(handle != NULL);
2046 oh = container_of0(handle, struct osd_thandle, ot_super);
2047 LASSERT(oh->ot_handle == NULL);
2049 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2050 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2051 if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2052 lu_object_fid(&dt->do_lu)))
2053 /* Reuse idle OI block may cause additional one OI block
2055 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2056 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2058 /* If this is directory, then we expect . and .. to be inserted as
2059 * well. The one directory block always needs to be created for the
2060 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2061 * block), there is no danger of needing a tree for the first block.
2063 if (attr && S_ISDIR(attr->la_mode)) {
2064 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2065 osd_dto_credits_noquota[DTO_WRITE_BASE]);
2066 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2072 rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2073 false, false, NULL, false);
2077 /* It does fld look up inside declare, and the result will be
2078 * added to fld cache, so the following fld lookup inside insert
2079 * does not need send RPC anymore, so avoid send rpc with holding
2081 if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2082 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2083 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2084 lu_object_fid(&dt->do_lu), range);
2090 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2091 struct lu_attr *attr,
2092 struct dt_allocation_hint *hint,
2093 struct dt_object_format *dof,
2096 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2097 struct osd_object *obj = osd_dt_obj(dt);
2098 struct osd_thread_info *info = osd_oti_get(env);
2103 LINVRNT(osd_invariant(obj));
2104 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2105 LASSERT(osd_write_locked(env, obj));
2106 LASSERT(th != NULL);
2108 if (unlikely(fid_is_acct(fid)))
2109 /* Quota files can't be created from the kernel any more,
2110 * 'tune2fs -O quota' will take care of creating them */
2113 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2114 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2116 result = __osd_object_create(info, obj, attr, hint, dof, th);
2118 result = __osd_oi_insert(env, obj, fid, th);
2120 LASSERT(ergo(result == 0,
2121 dt_object_exists(dt) && !dt_object_remote(dt)));
2123 LASSERT(osd_invariant(obj));
2128 * Called to destroy on-disk representation of the object
2130 * Concurrency: must be locked
2132 static int osd_declare_object_destroy(const struct lu_env *env,
2133 struct dt_object *dt,
2136 struct osd_object *obj = osd_dt_obj(dt);
2137 struct inode *inode = obj->oo_inode;
2138 struct osd_thandle *oh;
2142 oh = container_of0(th, struct osd_thandle, ot_super);
2143 LASSERT(oh->ot_handle == NULL);
2146 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2147 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2148 /* Recycle idle OI leaf may cause additional three OI blocks
2150 osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2151 osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2153 /* one less inode */
2154 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2155 false, true, NULL, false);
2158 /* data to be truncated */
2159 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2160 true, true, NULL, false);
2164 static int osd_object_destroy(const struct lu_env *env,
2165 struct dt_object *dt,
2168 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2169 struct osd_object *obj = osd_dt_obj(dt);
2170 struct inode *inode = obj->oo_inode;
2171 struct osd_device *osd = osd_obj2dev(obj);
2172 struct osd_thandle *oh;
2176 oh = container_of0(th, struct osd_thandle, ot_super);
2177 LASSERT(oh->ot_handle);
2179 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2181 if (unlikely(fid_is_acct(fid)))
2184 /* Parallel control for OI scrub. For most of cases, there is no
2185 * lock contention. So it will not affect unlink performance. */
2186 mutex_lock(&inode->i_mutex);
2187 if (S_ISDIR(inode->i_mode)) {
2188 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2189 /* it will check/delete the inode from remote parent,
2190 * how to optimize it? unlink performance impaction XXX */
2191 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2192 if (result != 0 && result != -ENOENT) {
2193 CERROR("%s: delete inode "DFID": rc = %d\n",
2194 osd_name(osd), PFID(fid), result);
2196 spin_lock(&obj->oo_guard);
2198 spin_unlock(&obj->oo_guard);
2199 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2202 osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2204 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2205 mutex_unlock(&inode->i_mutex);
2207 /* XXX: add to ext3 orphan list */
2208 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2210 /* not needed in the cache anymore */
2211 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2217 * Put the fid into lustre_mdt_attrs, and then place the structure
2218 * inode's ea. This fid should not be altered during the life time
2221 * \retval +ve, on success
2222 * \retval -ve, on error
2224 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2226 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2227 const struct lu_fid *fid, __u64 flags)
2229 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2232 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2235 lustre_lma_init(lma, fid, flags);
2236 lustre_lma_swab(lma);
2238 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2240 /* Someone may created the EA by race. */
2241 if (unlikely(rc == -EEXIST))
2247 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2248 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2249 * To have compatilibility with 1.8 ldiskfs driver we need to have
2250 * magic number at start of fid data.
2251 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2254 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2255 const struct dt_rec *fid)
2257 if (!fid_is_namespace_visible((const struct lu_fid *)fid) ||
2258 OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2259 param->edp_magic = 0;
2263 param->edp_magic = LDISKFS_LUFID_MAGIC;
2264 param->edp_len = sizeof(struct lu_fid) + 1;
2265 fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2269 * Try to read the fid from inode ea into dt_rec.
2271 * \param fid object fid.
2273 * \retval 0 on success
2275 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2276 __u32 ino, struct lu_fid *fid,
2277 struct osd_inode_id *id)
2279 struct osd_thread_info *info = osd_oti_get(env);
2280 struct inode *inode;
2283 osd_id_gen(id, ino, OSD_OII_NOGEN);
2284 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2286 RETURN(PTR_ERR(inode));
2292 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2294 struct inode *parent_dir,
2295 const struct dt_rec *dot_fid,
2296 const struct dt_rec *dot_dot_fid,
2297 struct osd_thandle *oth)
2299 struct ldiskfs_dentry_param *dot_ldp;
2300 struct ldiskfs_dentry_param *dot_dot_ldp;
2302 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2303 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2305 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2306 dot_ldp->edp_magic = 0;
2307 return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2308 dir, dot_ldp, dot_dot_ldp);
2312 * Create an local agent inode for remote entry
2314 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2315 struct osd_device *osd,
2316 struct osd_object *pobj,
2317 const struct lu_fid *fid,
2320 struct osd_thread_info *info = osd_oti_get(env);
2321 struct inode *local;
2322 struct osd_thandle *oh;
2327 oh = container_of(th, struct osd_thandle, ot_super);
2328 LASSERT(oh->ot_handle->h_transaction != NULL);
2330 /* FIXME: Insert index api needs to know the mode of
2331 * the remote object. Just use S_IFDIR for now */
2332 local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2333 if (IS_ERR(local)) {
2334 CERROR("%s: create local error %d\n", osd_name(osd),
2335 (int)PTR_ERR(local));
2339 /* Set special LMA flag for local agent inode */
2340 rc = osd_ea_fid_set(info, local, fid, LMAI_AGENT);
2342 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2343 osd_name(osd), PFID(fid), rc);
2344 RETURN(ERR_PTR(rc));
2347 rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2348 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2349 (const struct dt_rec *)fid, oh);
2351 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2352 osd_name(osd), PFID(fid), rc);
2353 RETURN(ERR_PTR(rc));
2360 * Delete local agent inode for remote entry
2362 static int osd_delete_local_agent_inode(const struct lu_env *env,
2363 struct osd_device *osd,
2364 const struct lu_fid *fid,
2365 __u32 ino, struct osd_thandle *oh)
2367 struct osd_thread_info *oti = osd_oti_get(env);
2368 struct osd_inode_id *id = &oti->oti_id;
2369 struct inode *inode;
2372 id->oii_ino = le32_to_cpu(ino);
2373 id->oii_gen = OSD_OII_NOGEN;
2374 inode = osd_iget(oti, osd, id);
2375 if (IS_ERR(inode)) {
2376 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2377 PFID(fid), id->oii_ino, id->oii_gen);
2378 RETURN(PTR_ERR(inode));
2382 mark_inode_dirty(inode);
2383 CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2384 osd_name(osd), PFID(fid), inode->i_ino);
2390 * OSD layer object create function for interoperability mode (b11826).
2391 * This is mostly similar to osd_object_create(). Only difference being, fid is
2392 * inserted into inode ea here.
2394 * \retval 0, on success
2395 * \retval -ve, on error
2397 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2398 struct lu_attr *attr,
2399 struct dt_allocation_hint *hint,
2400 struct dt_object_format *dof,
2403 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2404 struct osd_object *obj = osd_dt_obj(dt);
2405 struct osd_thread_info *info = osd_oti_get(env);
2410 LASSERT(osd_invariant(obj));
2411 LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2412 LASSERT(osd_write_locked(env, obj));
2413 LASSERT(th != NULL);
2415 if (unlikely(fid_is_acct(fid)))
2416 /* Quota files can't be created from the kernel any more,
2417 * 'tune2fs -O quota' will take care of creating them */
2420 osd_trans_exec_op(env, th, OSD_OT_CREATE);
2421 osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2423 result = __osd_object_create(info, obj, attr, hint, dof, th);
2425 result = osd_ea_fid_set(info, obj->oo_inode, fid, 0);
2428 result = __osd_oi_insert(env, obj, fid, th);
2430 LASSERT(ergo(result == 0,
2431 dt_object_exists(dt) && !dt_object_remote(dt)));
2432 LINVRNT(osd_invariant(obj));
2436 static int osd_declare_object_ref_add(const struct lu_env *env,
2437 struct dt_object *dt,
2438 struct thandle *handle)
2440 struct osd_thandle *oh;
2442 /* it's possible that object doesn't exist yet */
2443 LASSERT(handle != NULL);
2445 oh = container_of0(handle, struct osd_thandle, ot_super);
2446 LASSERT(oh->ot_handle == NULL);
2448 osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2449 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2455 * Concurrency: @dt is write locked.
2457 static int osd_object_ref_add(const struct lu_env *env,
2458 struct dt_object *dt, struct thandle *th)
2460 struct osd_object *obj = osd_dt_obj(dt);
2461 struct inode *inode = obj->oo_inode;
2463 LINVRNT(osd_invariant(obj));
2464 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2465 LASSERT(osd_write_locked(env, obj));
2466 LASSERT(th != NULL);
2468 osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2471 * DIR_NLINK feature is set for compatibility reasons if:
2472 * 1) nlinks > LDISKFS_LINK_MAX, or
2473 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2475 * It is easier to always set this flag (rather than check and set),
2476 * since it has less overhead, and the superblock will be dirtied
2477 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2478 * do not actually care whether this flag is set or not.
2480 spin_lock(&obj->oo_guard);
2481 /* inc_nlink from 0 may cause WARN_ON */
2482 if(inode->i_nlink == 0)
2483 set_nlink(inode, 1);
2486 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2487 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2488 inode->i_nlink == 2)
2489 set_nlink(inode, 1);
2491 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2492 spin_unlock(&obj->oo_guard);
2493 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2494 LINVRNT(osd_invariant(obj));
2499 static int osd_declare_object_ref_del(const struct lu_env *env,
2500 struct dt_object *dt,
2501 struct thandle *handle)
2503 struct osd_thandle *oh;
2505 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2506 LASSERT(handle != NULL);
2508 oh = container_of0(handle, struct osd_thandle, ot_super);
2509 LASSERT(oh->ot_handle == NULL);
2511 osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2512 osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2518 * Concurrency: @dt is write locked.
2520 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2523 struct osd_object *obj = osd_dt_obj(dt);
2524 struct inode *inode = obj->oo_inode;
2526 LINVRNT(osd_invariant(obj));
2527 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2528 LASSERT(osd_write_locked(env, obj));
2529 LASSERT(th != NULL);
2531 osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2533 spin_lock(&obj->oo_guard);
2534 LASSERT(inode->i_nlink > 0);
2536 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2537 * then the nlink count is 1. Don't let it be set to 0 or the directory
2538 * inode will be deleted incorrectly. */
2539 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2540 set_nlink(inode, 1);
2541 spin_unlock(&obj->oo_guard);
2542 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2543 LINVRNT(osd_invariant(obj));
2549 * Get the 64-bit version for an inode.
2551 static int osd_object_version_get(const struct lu_env *env,
2552 struct dt_object *dt, dt_obj_version_t *ver)
2554 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2556 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2557 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2558 *ver = LDISKFS_I(inode)->i_fs_version;
2563 * Concurrency: @dt is read locked.
2565 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2566 struct lu_buf *buf, const char *name,
2567 struct lustre_capa *capa)
2569 struct osd_object *obj = osd_dt_obj(dt);
2570 struct inode *inode = obj->oo_inode;
2571 struct osd_thread_info *info = osd_oti_get(env);
2572 struct dentry *dentry = &info->oti_obj_dentry;
2574 /* version get is not real XATTR but uses xattr API */
2575 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2576 /* for version we are just using xattr API but change inode
2578 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2579 osd_object_version_get(env, dt, buf->lb_buf);
2580 return sizeof(dt_obj_version_t);
2583 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2584 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2586 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2589 return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2593 static int osd_declare_xattr_set(const struct lu_env *env,
2594 struct dt_object *dt,
2595 const struct lu_buf *buf, const char *name,
2596 int fl, struct thandle *handle)
2598 struct osd_thandle *oh;
2600 LASSERT(handle != NULL);
2602 oh = container_of0(handle, struct osd_thandle, ot_super);
2603 LASSERT(oh->ot_handle == NULL);
2605 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2606 strcmp(name, XATTR_NAME_VERSION) == 0 ?
2607 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2608 osd_dto_credits_noquota[DTO_XATTR_SET]);
2614 * Set the 64-bit version for object
2616 static void osd_object_version_set(const struct lu_env *env,
2617 struct dt_object *dt,
2618 dt_obj_version_t *new_version)
2620 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2622 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2623 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2625 LDISKFS_I(inode)->i_fs_version = *new_version;
2626 /** Version is set after all inode operations are finished,
2627 * so we should mark it dirty here */
2628 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2632 * Concurrency: @dt is write locked.
2634 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2635 const struct lu_buf *buf, const char *name, int fl,
2636 struct thandle *handle, struct lustre_capa *capa)
2638 struct osd_object *obj = osd_dt_obj(dt);
2639 struct inode *inode = obj->oo_inode;
2640 struct osd_thread_info *info = osd_oti_get(env);
2644 LASSERT(handle != NULL);
2646 /* version set is not real XATTR */
2647 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2648 /* for version we are just using xattr API but change inode
2650 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2651 osd_object_version_set(env, dt, buf->lb_buf);
2652 return sizeof(dt_obj_version_t);
2655 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2658 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2659 if (fl & LU_XATTR_REPLACE)
2660 fs_flags |= XATTR_REPLACE;
2662 if (fl & LU_XATTR_CREATE)
2663 fs_flags |= XATTR_CREATE;
2665 return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2670 * Concurrency: @dt is read locked.
2672 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2673 struct lu_buf *buf, struct lustre_capa *capa)
2675 struct osd_object *obj = osd_dt_obj(dt);
2676 struct inode *inode = obj->oo_inode;
2677 struct osd_thread_info *info = osd_oti_get(env);
2678 struct dentry *dentry = &info->oti_obj_dentry;
2680 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2681 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2682 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2684 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2687 dentry->d_inode = inode;
2688 dentry->d_sb = inode->i_sb;
2689 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2692 static int osd_declare_xattr_del(const struct lu_env *env,
2693 struct dt_object *dt, const char *name,
2694 struct thandle *handle)
2696 struct osd_thandle *oh;
2698 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2699 LASSERT(handle != NULL);
2701 oh = container_of0(handle, struct osd_thandle, ot_super);
2702 LASSERT(oh->ot_handle == NULL);
2704 osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2705 osd_dto_credits_noquota[DTO_XATTR_SET]);
2711 * Concurrency: @dt is write locked.
2713 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2714 const char *name, struct thandle *handle,
2715 struct lustre_capa *capa)
2717 struct osd_object *obj = osd_dt_obj(dt);
2718 struct inode *inode = obj->oo_inode;
2719 struct osd_thread_info *info = osd_oti_get(env);
2720 struct dentry *dentry = &info->oti_obj_dentry;
2723 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2724 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2725 LASSERT(handle != NULL);
2727 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2730 osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2732 ll_vfs_dq_init(inode);
2733 dentry->d_inode = inode;
2734 dentry->d_sb = inode->i_sb;
2735 rc = inode->i_op->removexattr(dentry, name);
2739 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2740 struct dt_object *dt,
2741 struct lustre_capa *old,
2744 struct osd_thread_info *info = osd_oti_get(env);
2745 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2746 struct osd_object *obj = osd_dt_obj(dt);
2747 struct osd_device *dev = osd_obj2dev(obj);
2748 struct lustre_capa_key *key = &info->oti_capa_key;
2749 struct lustre_capa *capa = &info->oti_capa;
2750 struct obd_capa *oc;
2751 struct md_capainfo *ci;
2755 if (!dev->od_fl_capa)
2756 RETURN(ERR_PTR(-ENOENT));
2758 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2759 LINVRNT(osd_invariant(obj));
2761 /* renewal sanity check */
2762 if (old && osd_object_auth(env, dt, old, opc))
2763 RETURN(ERR_PTR(-EACCES));
2765 ci = md_capainfo(env);
2767 RETURN(ERR_PTR(-ENOENT));
2769 switch (ci->mc_auth) {
2773 capa->lc_uid = obj->oo_inode->i_uid;
2774 capa->lc_gid = obj->oo_inode->i_gid;
2775 capa->lc_flags = LC_ID_PLAIN;
2777 case LC_ID_CONVERT: {
2780 s[0] = obj->oo_inode->i_uid;
2781 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2782 s[2] = obj->oo_inode->i_gid;
2783 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2784 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2786 RETURN(ERR_PTR(rc));
2788 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2789 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2790 capa->lc_flags = LC_ID_CONVERT;
2794 RETURN(ERR_PTR(-EINVAL));
2797 capa->lc_fid = *fid;
2799 capa->lc_flags |= dev->od_capa_alg << 24;
2800 capa->lc_timeout = dev->od_capa_timeout;
2801 capa->lc_expiry = 0;
2803 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2805 LASSERT(!capa_is_expired(oc));
2809 spin_lock(&capa_lock);
2810 *key = dev->od_capa_keys[1];
2811 spin_unlock(&capa_lock);
2813 capa->lc_keyid = key->lk_keyid;
2814 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2816 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2818 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2819 RETURN(ERR_PTR(rc));
2822 oc = capa_add(dev->od_capa_hash, capa);
2826 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2828 struct osd_object *obj = osd_dt_obj(dt);
2829 struct inode *inode = obj->oo_inode;
2830 struct osd_thread_info *info = osd_oti_get(env);
2831 struct dentry *dentry = &info->oti_obj_dentry;
2832 struct file *file = &info->oti_file;
2837 dentry->d_inode = inode;
2838 dentry->d_sb = inode->i_sb;
2839 file->f_dentry = dentry;
2840 file->f_mapping = inode->i_mapping;
2841 file->f_op = inode->i_fop;
2842 #ifndef HAVE_FILE_FSYNC_4ARGS
2843 mutex_lock(&inode->i_mutex);
2845 rc = do_fsync(file, 0);
2846 #ifndef HAVE_FILE_FSYNC_4ARGS
2847 mutex_unlock(&inode->i_mutex);
2852 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2855 struct osd_object *obj = osd_dt_obj(dt);
2858 *data = (void *)obj->oo_inode;
2866 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2867 const struct dt_index_features *feat)
2869 struct iam_descr *descr;
2871 if (osd_object_is_root(o))
2872 return feat == &dt_directory_features;
2874 LASSERT(o->oo_dir != NULL);
2876 descr = o->oo_dir->od_container.ic_descr;
2877 if (feat == &dt_directory_features) {
2878 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2884 feat->dif_keysize_min <= descr->id_key_size &&
2885 descr->id_key_size <= feat->dif_keysize_max &&
2886 feat->dif_recsize_min <= descr->id_rec_size &&
2887 descr->id_rec_size <= feat->dif_recsize_max &&
2888 !(feat->dif_flags & (DT_IND_VARKEY |
2889 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2890 ergo(feat->dif_flags & DT_IND_UPDATE,
2891 1 /* XXX check that object (and file system) is
2896 static int osd_iam_container_init(const struct lu_env *env,
2897 struct osd_object *obj,
2898 struct osd_directory *dir)
2900 struct iam_container *bag = &dir->od_container;
2903 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2907 result = iam_container_setup(bag);
2909 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2911 iam_container_fini(bag);
2918 * Concurrency: no external locking is necessary.
2920 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2921 const struct dt_index_features *feat)
2925 struct osd_object *obj = osd_dt_obj(dt);
2927 LINVRNT(osd_invariant(obj));
2929 if (osd_object_is_root(obj)) {
2930 dt->do_index_ops = &osd_index_ea_ops;
2932 } else if (feat == &dt_directory_features) {
2933 dt->do_index_ops = &osd_index_ea_ops;
2934 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
2939 } else if (unlikely(feat == &dt_otable_features)) {
2940 dt->do_index_ops = &osd_otable_ops;
2942 } else if (unlikely(feat == &dt_acct_features)) {
2943 dt->do_index_ops = &osd_acct_index_ops;
2946 } else if (!osd_has_index(obj)) {
2947 struct osd_directory *dir;
2952 spin_lock(&obj->oo_guard);
2953 if (obj->oo_dir == NULL)
2957 * Concurrent thread allocated container data.
2960 spin_unlock(&obj->oo_guard);
2962 * Now, that we have container data, serialize its
2965 down_write(&obj->oo_ext_idx_sem);
2967 * recheck under lock.
2969 if (!osd_has_index(obj))
2970 result = osd_iam_container_init(env, obj, dir);
2973 up_write(&obj->oo_ext_idx_sem);
2981 if (result == 0 && skip_iam == 0) {
2982 if (!osd_iam_index_probe(env, obj, feat))
2985 LINVRNT(osd_invariant(obj));
2987 if (result == 0 && is_quota_glb_feat(feat) &&
2988 fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
2989 result = osd_quota_migration(env, dt, feat);
2994 static int osd_otable_it_attr_get(const struct lu_env *env,
2995 struct dt_object *dt,
2996 struct lu_attr *attr,
2997 struct lustre_capa *capa)
3003 static const struct dt_object_operations osd_obj_ops = {
3004 .do_read_lock = osd_object_read_lock,
3005 .do_write_lock = osd_object_write_lock,
3006 .do_read_unlock = osd_object_read_unlock,
3007 .do_write_unlock = osd_object_write_unlock,
3008 .do_write_locked = osd_object_write_locked,
3009 .do_attr_get = osd_attr_get,
3010 .do_declare_attr_set = osd_declare_attr_set,
3011 .do_attr_set = osd_attr_set,
3012 .do_ah_init = osd_ah_init,
3013 .do_declare_create = osd_declare_object_create,
3014 .do_create = osd_object_create,
3015 .do_declare_destroy = osd_declare_object_destroy,
3016 .do_destroy = osd_object_destroy,
3017 .do_index_try = osd_index_try,
3018 .do_declare_ref_add = osd_declare_object_ref_add,
3019 .do_ref_add = osd_object_ref_add,
3020 .do_declare_ref_del = osd_declare_object_ref_del,
3021 .do_ref_del = osd_object_ref_del,
3022 .do_xattr_get = osd_xattr_get,
3023 .do_declare_xattr_set = osd_declare_xattr_set,
3024 .do_xattr_set = osd_xattr_set,
3025 .do_declare_xattr_del = osd_declare_xattr_del,
3026 .do_xattr_del = osd_xattr_del,
3027 .do_xattr_list = osd_xattr_list,
3028 .do_capa_get = osd_capa_get,
3029 .do_object_sync = osd_object_sync,
3030 .do_data_get = osd_data_get,
3034 * dt_object_operations for interoperability mode
3035 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3037 static const struct dt_object_operations osd_obj_ea_ops = {
3038 .do_read_lock = osd_object_read_lock,
3039 .do_write_lock = osd_object_write_lock,
3040 .do_read_unlock = osd_object_read_unlock,
3041 .do_write_unlock = osd_object_write_unlock,
3042 .do_write_locked = osd_object_write_locked,
3043 .do_attr_get = osd_attr_get,
3044 .do_declare_attr_set = osd_declare_attr_set,
3045 .do_attr_set = osd_attr_set,
3046 .do_ah_init = osd_ah_init,
3047 .do_declare_create = osd_declare_object_create,
3048 .do_create = osd_object_ea_create,
3049 .do_declare_destroy = osd_declare_object_destroy,
3050 .do_destroy = osd_object_destroy,
3051 .do_index_try = osd_index_try,
3052 .do_declare_ref_add = osd_declare_object_ref_add,
3053 .do_ref_add = osd_object_ref_add,
3054 .do_declare_ref_del = osd_declare_object_ref_del,
3055 .do_ref_del = osd_object_ref_del,
3056 .do_xattr_get = osd_xattr_get,
3057 .do_declare_xattr_set = osd_declare_xattr_set,
3058 .do_xattr_set = osd_xattr_set,
3059 .do_declare_xattr_del = osd_declare_xattr_del,
3060 .do_xattr_del = osd_xattr_del,
3061 .do_xattr_list = osd_xattr_list,
3062 .do_capa_get = osd_capa_get,
3063 .do_object_sync = osd_object_sync,
3064 .do_data_get = osd_data_get,
3067 static const struct dt_object_operations osd_obj_otable_it_ops = {
3068 .do_attr_get = osd_otable_it_attr_get,
3069 .do_index_try = osd_index_try,
3072 static int osd_index_declare_iam_delete(const struct lu_env *env,
3073 struct dt_object *dt,
3074 const struct dt_key *key,
3075 struct thandle *handle)
3077 struct osd_thandle *oh;
3079 oh = container_of0(handle, struct osd_thandle, ot_super);
3080 LASSERT(oh->ot_handle == NULL);
3082 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3083 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3089 * delete a (key, value) pair from index \a dt specified by \a key
3091 * \param dt osd index object
3092 * \param key key for index
3093 * \param rec record reference
3094 * \param handle transaction handler
3097 * \retval -ve failure
3100 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3101 const struct dt_key *key,
3102 struct thandle *handle,
3103 struct lustre_capa *capa)
3105 struct osd_thread_info *oti = osd_oti_get(env);
3106 struct osd_object *obj = osd_dt_obj(dt);
3107 struct osd_thandle *oh;
3108 struct iam_path_descr *ipd;
3109 struct iam_container *bag = &obj->oo_dir->od_container;
3114 LINVRNT(osd_invariant(obj));
3115 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3116 LASSERT(bag->ic_object == obj->oo_inode);
3117 LASSERT(handle != NULL);
3119 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3122 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3124 ipd = osd_idx_ipd_get(env, bag);
3125 if (unlikely(ipd == NULL))
3128 oh = container_of0(handle, struct osd_thandle, ot_super);
3129 LASSERT(oh->ot_handle != NULL);
3130 LASSERT(oh->ot_handle->h_transaction != NULL);
3132 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3133 /* swab quota uid/gid provided by caller */
3134 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3135 key = (const struct dt_key *)&oti->oti_quota_id;
3138 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3139 osd_ipd_put(env, bag, ipd);
3140 LINVRNT(osd_invariant(obj));
3144 static int osd_index_declare_ea_delete(const struct lu_env *env,
3145 struct dt_object *dt,
3146 const struct dt_key *key,
3147 struct thandle *handle)
3149 struct osd_thandle *oh;
3150 struct inode *inode;
3154 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3155 LASSERT(handle != NULL);
3157 oh = container_of0(handle, struct osd_thandle, ot_super);
3158 LASSERT(oh->ot_handle == NULL);
3160 osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3161 osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3163 inode = osd_dt_obj(dt)->oo_inode;
3166 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3167 true, true, NULL, false);
3171 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3174 struct osd_fid_pack *rec;
3177 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3178 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3179 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3184 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3187 struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
3188 struct seq_server_site *ss = osd_seq_site(osd);
3192 /* Those FID seqs, which are not in FLDB, must be local seq */
3193 if (unlikely(!fid_seq_in_fldb(fid_seq(fid)) || ss == NULL))
3196 rc = osd_fld_lookup(env, osd, fid, range);
3198 CERROR("%s: Can not lookup fld for "DFID"\n",
3199 osd_name(osd), PFID(fid));
3203 RETURN(ss->ss_node_id != range->lsr_index);
3207 * Index delete function for interoperability mode (b11826).
3208 * It will remove the directory entry added by osd_index_ea_insert().
3209 * This entry is needed to maintain name->fid mapping.
3211 * \param key, key i.e. file entry to be deleted
3213 * \retval 0, on success
3214 * \retval -ve, on error
3216 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3217 const struct dt_key *key,
3218 struct thandle *handle,
3219 struct lustre_capa *capa)
3221 struct osd_object *obj = osd_dt_obj(dt);
3222 struct inode *dir = obj->oo_inode;
3223 struct dentry *dentry;
3224 struct osd_thandle *oh;
3225 struct ldiskfs_dir_entry_2 *de = NULL;
3226 struct buffer_head *bh;
3227 struct htree_lock *hlock = NULL;
3228 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
3229 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3233 LINVRNT(osd_invariant(obj));
3234 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3235 LASSERT(handle != NULL);
3237 osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3239 oh = container_of(handle, struct osd_thandle, ot_super);
3240 LASSERT(oh->ot_handle != NULL);
3241 LASSERT(oh->ot_handle->h_transaction != NULL);
3243 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3246 ll_vfs_dq_init(dir);
3247 dentry = osd_child_dentry_get(env, obj,
3248 (char *)key, strlen((char *)key));
3250 if (obj->oo_hl_head != NULL) {
3251 hlock = osd_oti_get(env)->oti_hlock;
3252 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3253 dir, LDISKFS_HLOCK_DEL);
3255 down_write(&obj->oo_ext_idx_sem);
3258 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3262 /* Tried to delete local agent inode for the entries,
3263 * If it tries to delete .., which only happens during
3264 * rename or move the dir to /ORPHAN, even .. might
3265 * point to the remote parent, but it do not have
3266 * local agent inode, so we do not need check .. at all,
3267 * Note: we need delete entry first, then inode to keep
3269 if (strcmp((char *)key, dotdot) != 0) {
3270 LASSERT(de != NULL);
3271 rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3273 unlikely(osd_remote_fid(env, osd, fid)))
3274 /* Need to delete agent inode */
3275 ino = le32_to_cpu(de->inode);
3277 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3279 if (rc == 0 && unlikely(ino != 0)) {
3280 rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3283 CERROR("%s: del local inode "DFID": rc = %d\n",
3284 osd_name(osd), PFID(fid), rc);
3290 ldiskfs_htree_unlock(hlock);
3292 up_write(&obj->oo_ext_idx_sem);
3297 /* For inode on the remote MDT, .. will point to
3298 * /Agent directory, Check whether it needs to delete
3299 * from agent directory */
3300 if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3301 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3303 if (rc != 0 && rc != -ENOENT) {
3304 CERROR("%s: delete agent inode "DFID": rc = %d\n",
3305 osd_name(osd), PFID(fid), rc);
3315 LASSERT(osd_invariant(obj));
3320 * Lookup index for \a key and copy record to \a rec.
3322 * \param dt osd index object
3323 * \param key key for index
3324 * \param rec record reference
3326 * \retval +ve success : exact mach
3327 * \retval 0 return record with key not greater than \a key
3328 * \retval -ve failure
3330 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3331 struct dt_rec *rec, const struct dt_key *key,
3332 struct lustre_capa *capa)
3334 struct osd_object *obj = osd_dt_obj(dt);
3335 struct iam_path_descr *ipd;
3336 struct iam_container *bag = &obj->oo_dir->od_container;
3337 struct osd_thread_info *oti = osd_oti_get(env);
3338 struct iam_iterator *it = &oti->oti_idx_it;
3339 struct iam_rec *iam_rec;
3344 LASSERT(osd_invariant(obj));
3345 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3346 LASSERT(bag->ic_object == obj->oo_inode);
3348 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3351 ipd = osd_idx_ipd_get(env, bag);
3355 /* got ipd now we can start iterator. */
3356 iam_it_init(it, bag, 0, ipd);
3358 if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3359 /* swab quota uid/gid provided by caller */
3360 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3361 key = (const struct dt_key *)&oti->oti_quota_id;
3364 rc = iam_it_get(it, (struct iam_key *)key);
3366 if (S_ISDIR(obj->oo_inode->i_mode))
3367 iam_rec = (struct iam_rec *)oti->oti_ldp;
3369 iam_rec = (struct iam_rec *) rec;
3371 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3373 if (S_ISDIR(obj->oo_inode->i_mode))
3374 osd_fid_unpack((struct lu_fid *) rec,
3375 (struct osd_fid_pack *)iam_rec);
3376 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3377 osd_quota_unpack(obj, rec);
3382 osd_ipd_put(env, bag, ipd);
3384 LINVRNT(osd_invariant(obj));
3389 static int osd_index_declare_iam_insert(const struct lu_env *env,
3390 struct dt_object *dt,
3391 const struct dt_rec *rec,
3392 const struct dt_key *key,
3393 struct thandle *handle)
3395 struct osd_thandle *oh;
3397 LASSERT(handle != NULL);
3399 oh = container_of0(handle, struct osd_thandle, ot_super);
3400 LASSERT(oh->ot_handle == NULL);
3402 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3403 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3409 * Inserts (key, value) pair in \a dt index object.
3411 * \param dt osd index object
3412 * \param key key for index
3413 * \param rec record reference
3414 * \param th transaction handler
3417 * \retval -ve failure
3419 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3420 const struct dt_rec *rec,
3421 const struct dt_key *key, struct thandle *th,
3422 struct lustre_capa *capa, int ignore_quota)
3424 struct osd_object *obj = osd_dt_obj(dt);
3425 struct iam_path_descr *ipd;
3426 struct osd_thandle *oh;
3427 struct iam_container *bag = &obj->oo_dir->od_container;
3428 struct osd_thread_info *oti = osd_oti_get(env);
3429 struct iam_rec *iam_rec;
3434 LINVRNT(osd_invariant(obj));
3435 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3436 LASSERT(bag->ic_object == obj->oo_inode);
3437 LASSERT(th != NULL);
3439 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3442 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3444 ipd = osd_idx_ipd_get(env, bag);
3445 if (unlikely(ipd == NULL))
3448 oh = container_of0(th, struct osd_thandle, ot_super);
3449 LASSERT(oh->ot_handle != NULL);
3450 LASSERT(oh->ot_handle->h_transaction != NULL);
3451 if (S_ISDIR(obj->oo_inode->i_mode)) {
3452 iam_rec = (struct iam_rec *)oti->oti_ldp;
3453 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3454 } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3455 /* pack quota uid/gid */
3456 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3457 key = (const struct dt_key *)&oti->oti_quota_id;
3458 /* pack quota record */
3459 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3460 iam_rec = (struct iam_rec *)rec;
3462 iam_rec = (struct iam_rec *)rec;
3465 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3467 osd_ipd_put(env, bag, ipd);
3468 LINVRNT(osd_invariant(obj));
3473 * Calls ldiskfs_add_entry() to add directory entry
3474 * into the directory. This is required for
3475 * interoperability mode (b11826)
3477 * \retval 0, on success
3478 * \retval -ve, on error
3480 static int __osd_ea_add_rec(struct osd_thread_info *info,
3481 struct osd_object *pobj, struct inode *cinode,
3482 const char *name, const struct dt_rec *fid,
3483 struct htree_lock *hlock, struct thandle *th)
3485 struct ldiskfs_dentry_param *ldp;
3486 struct dentry *child;
3487 struct osd_thandle *oth;
3490 oth = container_of(th, struct osd_thandle, ot_super);
3491 LASSERT(oth->ot_handle != NULL);
3492 LASSERT(oth->ot_handle->h_transaction != NULL);
3493 LASSERT(pobj->oo_inode);
3495 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3496 if (unlikely(pobj->oo_inode ==
3497 osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3500 osd_get_ldiskfs_dirent_param(ldp, fid);
3501 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3502 child->d_fsdata = (void *)ldp;
3503 ll_vfs_dq_init(pobj->oo_inode);
3504 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3510 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3511 * into the directory.Also sets flags into osd object to
3512 * indicate dot and dotdot are created. This is required for
3513 * interoperability mode (b11826)
3515 * \param dir directory for dot and dotdot fixup.
3516 * \param obj child object for linking
3518 * \retval 0, on success
3519 * \retval -ve, on error
3521 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3522 struct osd_object *dir,
3523 struct inode *parent_dir, const char *name,
3524 const struct dt_rec *dot_fid,
3525 const struct dt_rec *dot_dot_fid,
3528 struct inode *inode = dir->oo_inode;
3529 struct osd_thandle *oth;
3532 oth = container_of(th, struct osd_thandle, ot_super);
3533 LASSERT(oth->ot_handle->h_transaction != NULL);
3534 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3536 if (strcmp(name, dot) == 0) {
3537 if (dir->oo_compat_dot_created) {
3540 LASSERT(inode == parent_dir);
3541 dir->oo_compat_dot_created = 1;
3544 } else if (strcmp(name, dotdot) == 0) {
3545 if (!dir->oo_compat_dot_created)
3547 /* in case of rename, dotdot is already created */
3548 if (dir->oo_compat_dotdot_created) {
3549 return __osd_ea_add_rec(info, dir, parent_dir, name,
3550 dot_dot_fid, NULL, th);
3553 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3554 parent_dir, dot_fid,
3557 dir->oo_compat_dotdot_created = 1;
3565 * It will call the appropriate osd_add* function and return the
3566 * value, return by respective functions.
3568 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3569 struct inode *cinode, const char *name,
3570 const struct dt_rec *fid, struct thandle *th)
3572 struct osd_thread_info *info = osd_oti_get(env);
3573 struct htree_lock *hlock;
3576 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3578 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3580 if (hlock != NULL) {
3581 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3584 down_write(&pobj->oo_ext_idx_sem);
3586 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3587 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3590 if (hlock != NULL) {
3591 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3592 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3594 down_write(&pobj->oo_ext_idx_sem);
3597 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3598 struct lu_fid *tfid = &info->oti_fid;
3600 *tfid = *(const struct lu_fid *)fid;
3602 rc = __osd_ea_add_rec(info, pobj, cinode, name,
3603 (const struct dt_rec *)tfid,
3606 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3611 ldiskfs_htree_unlock(hlock);
3613 up_write(&pobj->oo_ext_idx_sem);
3619 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3620 struct osd_idmap_cache *oic)
3622 struct osd_scrub *scrub = &dev->od_scrub;
3623 struct lu_fid *fid = &oic->oic_fid;
3624 struct osd_inode_id *id = &oti->oti_id;
3629 if (!fid_is_norm(fid) && !fid_is_igif(fid))
3633 rc = osd_oi_lookup(oti, dev, fid, id, true);
3634 if (rc != 0 && rc != -ENOENT)
3637 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3640 if (thread_is_running(&scrub->os_thread)) {
3641 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3642 /* There is race condition between osd_oi_lookup and OI scrub.
3643 * The OI scrub finished just after osd_oi_lookup() failure.
3644 * Under such case, it is unnecessary to trigger OI scrub again,
3645 * but try to call osd_oi_lookup() again. */
3646 if (unlikely(rc == -EAGAIN))
3652 if (!dev->od_noscrub && ++once == 1) {
3653 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3655 rc = osd_scrub_start(dev);
3656 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3658 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3667 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3668 struct osd_device *dev,
3669 struct osd_idmap_cache *oic,
3670 struct lu_fid *fid, __u32 ino)
3672 struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
3673 struct inode *inode;
3676 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3677 inode = osd_iget(oti, dev, &oic->oic_lid);
3678 if (IS_ERR(inode)) {
3679 fid_zero(&oic->oic_fid);
3680 return PTR_ERR(inode);
3683 rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3686 fid_zero(&oic->oic_fid);
3688 *fid = oic->oic_fid = lma->lma_self_fid;
3693 * Calls ->lookup() to find dentry. From dentry get inode and
3694 * read inode's ea to get fid. This is required for interoperability
3697 * \retval 0, on success
3698 * \retval -ve, on error
3700 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3701 struct dt_rec *rec, const struct dt_key *key)
3703 struct inode *dir = obj->oo_inode;
3704 struct dentry *dentry;
3705 struct ldiskfs_dir_entry_2 *de;
3706 struct buffer_head *bh;
3707 struct lu_fid *fid = (struct lu_fid *) rec;
3708 struct htree_lock *hlock = NULL;
3713 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3715 dentry = osd_child_dentry_get(env, obj,
3716 (char *)key, strlen((char *)key));
3718 if (obj->oo_hl_head != NULL) {
3719 hlock = osd_oti_get(env)->oti_hlock;
3720 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3721 dir, LDISKFS_HLOCK_LOOKUP);
3723 down_read(&obj->oo_ext_idx_sem);
3726 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3728 struct osd_thread_info *oti = osd_oti_get(env);
3729 struct osd_inode_id *id = &oti->oti_id;
3730 struct osd_idmap_cache *oic = &oti->oti_cache;
3731 struct osd_device *dev = osd_obj2dev(obj);
3732 struct osd_scrub *scrub = &dev->od_scrub;
3733 struct scrub_file *sf = &scrub->os_file;
3735 ino = le32_to_cpu(de->inode);
3736 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
3738 rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
3742 rc = osd_get_fid_from_dentry(de, rec);
3744 /* done with de, release bh */
3747 rc = osd_ea_fid_get(env, obj, ino, fid, id);
3749 osd_id_gen(id, ino, OSD_OII_NOGEN);
3750 if (rc != 0 || osd_remote_fid(env, dev, fid)) {
3751 fid_zero(&oic->oic_fid);
3756 oic->oic_fid = *fid;
3757 if ((scrub->os_pos_current <= ino) &&
3758 ((sf->sf_flags & SF_INCONSISTENT) ||
3759 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
3760 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3762 osd_consistency_check(oti, dev, oic);
3771 ldiskfs_htree_unlock(hlock);
3773 up_read(&obj->oo_ext_idx_sem);
3778 * Find the osd object for given fid.
3780 * \param fid need to find the osd object having this fid
3782 * \retval osd_object on success
3783 * \retval -ve on error
3785 struct osd_object *osd_object_find(const struct lu_env *env,
3786 struct dt_object *dt,
3787 const struct lu_fid *fid)
3789 struct lu_device *ludev = dt->do_lu.lo_dev;
3790 struct osd_object *child = NULL;
3791 struct lu_object *luch;
3792 struct lu_object *lo;
3795 * at this point topdev might not exist yet
3796 * (i.e. MGS is preparing profiles). so we can
3797 * not rely on topdev and instead lookup with
3798 * our device passed as topdev. this can't work
3799 * if the object isn't cached yet (as osd doesn't
3800 * allocate lu_header). IOW, the object must be
3801 * in the cache, otherwise lu_object_alloc() crashes
3804 luch = lu_object_find_at(env, ludev, fid, NULL);
3805 if (!IS_ERR(luch)) {
3806 if (lu_object_exists(luch)) {
3807 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3809 child = osd_obj(lo);
3811 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3812 "lu_object can't be located"
3813 DFID"\n", PFID(fid));
3815 if (child == NULL) {
3816 lu_object_put(env, luch);
3817 CERROR("Unable to get osd_object\n");
3818 child = ERR_PTR(-ENOENT);
3821 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3822 "lu_object does not exists "DFID"\n",
3824 lu_object_put(env, luch);
3825 child = ERR_PTR(-ENOENT);
3828 child = (void *)luch;
3834 * Put the osd object once done with it.
3836 * \param obj osd object that needs to be put
3838 static inline void osd_object_put(const struct lu_env *env,
3839 struct osd_object *obj)
3841 lu_object_put(env, &obj->oo_dt.do_lu);
3844 static int osd_index_declare_ea_insert(const struct lu_env *env,
3845 struct dt_object *dt,
3846 const struct dt_rec *rec,
3847 const struct dt_key *key,
3848 struct thandle *handle)
3850 struct osd_thandle *oh;
3851 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3852 struct lu_fid *fid = (struct lu_fid *)rec;
3856 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3857 LASSERT(handle != NULL);
3859 oh = container_of0(handle, struct osd_thandle, ot_super);
3860 LASSERT(oh->ot_handle == NULL);
3862 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3863 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3865 if (osd_dt_obj(dt)->oo_inode == NULL) {
3866 const char *name = (const char *)key;
3867 /* Object is not being created yet. Only happens when
3868 * 1. declare directory create
3869 * 2. declare insert .
3870 * 3. declare insert ..
3872 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
3874 struct inode *inode = osd_dt_obj(dt)->oo_inode;
3876 /* We ignore block quota on meta pool (MDTs), so needn't
3877 * calculate how many blocks will be consumed by this index
3879 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
3880 oh, true, true, NULL, false);
3886 rc = osd_remote_fid(env, osd, fid);
3892 osd_trans_declare_op(env, oh, OSD_OT_CREATE,
3893 osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
3894 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3895 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3896 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3897 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
3903 * Index add function for interoperability mode (b11826).
3904 * It will add the directory entry.This entry is needed to
3905 * maintain name->fid mapping.
3907 * \param key it is key i.e. file entry to be inserted
3908 * \param rec it is value of given key i.e. fid
3910 * \retval 0, on success
3911 * \retval -ve, on error
3913 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3914 const struct dt_rec *rec,
3915 const struct dt_key *key, struct thandle *th,
3916 struct lustre_capa *capa, int ignore_quota)
3918 struct osd_object *obj = osd_dt_obj(dt);
3919 struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
3920 struct lu_fid *fid = (struct lu_fid *) rec;
3921 const char *name = (const char *)key;
3922 struct osd_thread_info *oti = osd_oti_get(env);
3923 struct osd_inode_id *id = &oti->oti_id;
3924 struct inode *child_inode = NULL;
3925 struct osd_object *child = NULL;
3929 LASSERT(osd_invariant(obj));
3930 LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3931 LASSERT(th != NULL);
3933 osd_trans_exec_op(env, th, OSD_OT_INSERT);
3935 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3938 LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
3940 rc = osd_remote_fid(env, osd, fid);
3942 CERROR("%s: Can not find object "DFID" rc %d\n",
3943 osd_name(osd), PFID(fid), rc);
3948 /* Insert remote entry */
3949 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
3950 struct osd_mdobj_map *omm = osd->od_mdt_map;
3951 struct osd_thandle *oh;
3953 /* If parent on remote MDT, we need put this object
3955 oh = container_of(th, typeof(*oh), ot_super);
3956 rc = osd_add_to_remote_parent(env, osd, obj, oh);
3958 CERROR("%s: add "DFID" error: rc = %d\n",
3960 PFID(lu_object_fid(&dt->do_lu)), rc);
3964 child_inode = igrab(omm->omm_remote_parent->d_inode);
3966 child_inode = osd_create_local_agent_inode(env, osd,
3969 if (IS_ERR(child_inode))
3970 RETURN(PTR_ERR(child_inode));
3973 /* Insert local entry */
3974 child = osd_object_find(env, dt, fid);
3975 if (IS_ERR(child)) {
3976 CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
3977 osd_name(osd), PFID(fid),
3978 id->oii_ino, id->oii_gen,
3979 (int)PTR_ERR(child_inode));
3980 RETURN(PTR_ERR(child_inode));
3982 child_inode = igrab(child->oo_inode);
3985 rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
3989 osd_object_put(env, child);
3990 LASSERT(osd_invariant(obj));
3995 * Initialize osd Iterator for given osd index object.
3997 * \param dt osd index object
4000 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4001 struct dt_object *dt,
4003 struct lustre_capa *capa)
4005 struct osd_it_iam *it;
4006 struct osd_thread_info *oti = osd_oti_get(env);
4007 struct osd_object *obj = osd_dt_obj(dt);
4008 struct lu_object *lo = &dt->do_lu;
4009 struct iam_path_descr *ipd;
4010 struct iam_container *bag = &obj->oo_dir->od_container;
4012 LASSERT(lu_object_exists(lo));
4014 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4015 return ERR_PTR(-EACCES);
4018 ipd = osd_it_ipd_get(env, bag);
4019 if (likely(ipd != NULL)) {
4023 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4024 return (struct dt_it *)it;
4026 return ERR_PTR(-ENOMEM);
4030 * free given Iterator.
4033 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4035 struct osd_it_iam *it = (struct osd_it_iam *)di;
4036 struct osd_object *obj = it->oi_obj;
4038 iam_it_fini(&it->oi_it);
4039 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4040 lu_object_put(env, &obj->oo_dt.do_lu);
4044 * Move Iterator to record specified by \a key
4046 * \param di osd iterator
4047 * \param key key for index
4049 * \retval +ve di points to record with least key not larger than key
4050 * \retval 0 di points to exact matched key
4051 * \retval -ve failure
4054 static int osd_it_iam_get(const struct lu_env *env,
4055 struct dt_it *di, const struct dt_key *key)
4057 struct osd_thread_info *oti = osd_oti_get(env);
4058 struct osd_it_iam *it = (struct osd_it_iam *)di;
4060 if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4061 /* swab quota uid/gid */
4062 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4063 key = (struct dt_key *)&oti->oti_quota_id;
4066 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4072 * \param di osd iterator
4074 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4076 struct osd_it_iam *it = (struct osd_it_iam *)di;
4078 iam_it_put(&it->oi_it);
4082 * Move iterator by one record
4084 * \param di osd iterator
4086 * \retval +1 end of container reached
4088 * \retval -ve failure
4091 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4093 struct osd_it_iam *it = (struct osd_it_iam *)di;
4095 return iam_it_next(&it->oi_it);
4099 * Return pointer to the key under iterator.
4102 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4103 const struct dt_it *di)
4105 struct osd_thread_info *oti = osd_oti_get(env);
4106 struct osd_it_iam *it = (struct osd_it_iam *)di;
4107 struct osd_object *obj = it->oi_obj;
4110 key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4112 if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4113 /* swab quota uid/gid */
4114 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4115 key = (struct dt_key *)&oti->oti_quota_id;
4122 * Return size of key under iterator (in bytes)
4125 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4127 struct osd_it_iam *it = (struct osd_it_iam *)di;
4129 return iam_it_key_size(&it->oi_it);
4133 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4135 /* check if file type is required */
4136 if (ent->lde_attrs & LUDA_TYPE) {
4137 struct luda_type *lt;
4138 int align = sizeof(*lt) - 1;
4140 len = (len + align) & ~align;
4141 lt = (struct luda_type *)(ent->lde_name + len);
4142 lt->lt_type = cpu_to_le16(DTTOIF(type));
4145 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4149 * build lu direct from backend fs dirent.
4153 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4154 char *name, __u16 namelen, __u16 type, __u32 attr)
4156 ent->lde_attrs = attr | LUDA_FID;
4157 fid_cpu_to_le(&ent->lde_fid, fid);
4159 ent->lde_hash = cpu_to_le64(offset);
4160 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4162 strncpy(ent->lde_name, name, namelen);
4163 ent->lde_name[namelen] = '\0';
4164 ent->lde_namelen = cpu_to_le16(namelen);
4166 /* append lustre attributes */
4167 osd_it_append_attrs(ent, namelen, type);
4171 * Return pointer to the record under iterator.
4173 static int osd_it_iam_rec(const struct lu_env *env,
4174 const struct dt_it *di,
4175 struct dt_rec *dtrec, __u32 attr)
4177 struct osd_it_iam *it = (struct osd_it_iam *)di;
4178 struct osd_thread_info *info = osd_oti_get(env);
4181 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4182 const struct osd_fid_pack *rec;
4183 struct lu_fid *fid = &info->oti_fid;
4184 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4190 name = (char *)iam_it_key_get(&it->oi_it);
4192 RETURN(PTR_ERR(name));
4194 namelen = iam_it_key_size(&it->oi_it);
4196 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4198 RETURN(PTR_ERR(rec));
4200 rc = osd_fid_unpack(fid, rec);
4204 hash = iam_it_store(&it->oi_it);
4206 /* IAM does not store object type in IAM index (dir) */
4207 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4209 } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4210 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4211 (struct iam_rec *)dtrec);
4212 osd_quota_unpack(it->oi_obj, dtrec);
4214 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4215 (struct iam_rec *)dtrec);
4222 * Returns cookie for current Iterator position.
4224 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4226 struct osd_it_iam *it = (struct osd_it_iam *)di;
4228 return iam_it_store(&it->oi_it);
4232 * Restore iterator from cookie.
4234 * \param di osd iterator
4235 * \param hash Iterator location cookie
4237 * \retval +ve di points to record with least key not larger than key.
4238 * \retval 0 di points to exact matched key
4239 * \retval -ve failure
4242 static int osd_it_iam_load(const struct lu_env *env,
4243 const struct dt_it *di, __u64 hash)
4245 struct osd_it_iam *it = (struct osd_it_iam *)di;
4247 return iam_it_load(&it->oi_it, hash);
4250 static const struct dt_index_operations osd_index_iam_ops = {
4251 .dio_lookup = osd_index_iam_lookup,
4252 .dio_declare_insert = osd_index_declare_iam_insert,
4253 .dio_insert = osd_index_iam_insert,
4254 .dio_declare_delete = osd_index_declare_iam_delete,
4255 .dio_delete = osd_index_iam_delete,
4257 .init = osd_it_iam_init,
4258 .fini = osd_it_iam_fini,
4259 .get = osd_it_iam_get,
4260 .put = osd_it_iam_put,
4261 .next = osd_it_iam_next,
4262 .key = osd_it_iam_key,
4263 .key_size = osd_it_iam_key_size,
4264 .rec = osd_it_iam_rec,
4265 .store = osd_it_iam_store,
4266 .load = osd_it_iam_load
4272 * Creates or initializes iterator context.
4274 * \retval struct osd_it_ea, iterator structure on success
4277 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4278 struct dt_object *dt,
4280 struct lustre_capa *capa)
4282 struct osd_object *obj = osd_dt_obj(dt);
4283 struct osd_thread_info *info = osd_oti_get(env);
4284 struct osd_it_ea *it = &info->oti_it_ea;
4285 struct lu_object *lo = &dt->do_lu;
4286 struct dentry *obj_dentry = &info->oti_it_dentry;
4288 LASSERT(lu_object_exists(lo));
4290 obj_dentry->d_inode = obj->oo_inode;
4291 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4292 obj_dentry->d_name.hash = 0;
4294 it->oie_rd_dirent = 0;
4295 it->oie_it_dirent = 0;
4296 it->oie_dirent = NULL;
4297 it->oie_buf = info->oti_it_ea_buf;
4299 it->oie_file.f_pos = 0;
4300 it->oie_file.f_dentry = obj_dentry;
4301 if (attr & LUDA_64BITHASH)
4302 it->oie_file.f_mode |= FMODE_64BITHASH;
4304 it->oie_file.f_mode |= FMODE_32BITHASH;
4305 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
4306 it->oie_file.f_op = obj->oo_inode->i_fop;
4307 it->oie_file.private_data = NULL;
4309 RETURN((struct dt_it *) it);
4313 * Destroy or finishes iterator context.
4315 * \param di iterator structure to be destroyed
4317 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4319 struct osd_it_ea *it = (struct osd_it_ea *)di;
4320 struct osd_object *obj = it->oie_obj;
4321 struct inode *inode = obj->oo_inode;
4324 it->oie_file.f_op->release(inode, &it->oie_file);
4325 lu_object_put(env, &obj->oo_dt.do_lu);
4330 * It position the iterator at given key, so that next lookup continues from
4331 * that key Or it is similar to dio_it->load() but based on a key,
4332 * rather than file position.
4334 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4337 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4339 static int osd_it_ea_get(const struct lu_env *env,
4340 struct dt_it *di, const struct dt_key *key)
4342 struct osd_it_ea *it = (struct osd_it_ea *)di;
4345 LASSERT(((const char *)key)[0] == '\0');
4346 it->oie_file.f_pos = 0;
4347 it->oie_rd_dirent = 0;
4348 it->oie_it_dirent = 0;
4349 it->oie_dirent = NULL;
4357 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4362 * It is called internally by ->readdir(). It fills the
4363 * iterator's in-memory data structure with required
4364 * information i.e. name, namelen, rec_size etc.
4366 * \param buf in which information to be filled in.
4367 * \param name name of the file in given dir
4369 * \retval 0 on success
4370 * \retval 1 on buffer full
4372 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4373 loff_t offset, __u64 ino,
4376 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4377 struct osd_object *obj = it->oie_obj;
4378 struct osd_it_ea_dirent *ent = it->oie_dirent;
4379 struct lu_fid *fid = &ent->oied_fid;
4380 struct osd_fid_pack *rec;
4383 /* this should never happen */
4384 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4385 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4389 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4393 /* "." is just the object itself. */
4394 if (namelen == 1 && name[0] == '.') {
4395 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4396 } else if (d_type & LDISKFS_DIRENT_LUFID) {
4397 rec = (struct osd_fid_pack*) (name + namelen + 1);
4398 if (osd_fid_unpack(fid, rec) != 0)
4403 d_type &= ~LDISKFS_DIRENT_LUFID;
4405 /* NOT export local root. */
4406 if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4407 ino = obj->oo_inode->i_ino;
4408 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4411 ent->oied_ino = ino;
4412 ent->oied_off = offset;
4413 ent->oied_namelen = namelen;
4414 ent->oied_type = d_type;
4416 memcpy(ent->oied_name, name, namelen);
4418 it->oie_rd_dirent++;
4419 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4424 * Calls ->readdir() to load a directory entry at a time
4425 * and stored it in iterator's in-memory data structure.
4427 * \param di iterator's in memory structure
4429 * \retval 0 on success
4430 * \retval -ve on error
4432 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4433 const struct dt_it *di)
4435 struct osd_it_ea *it = (struct osd_it_ea *)di;
4436 struct osd_object *obj = it->oie_obj;
4437 struct inode *inode = obj->oo_inode;
4438 struct htree_lock *hlock = NULL;
4442 it->oie_dirent = it->oie_buf;
4443 it->oie_rd_dirent = 0;
4445 if (obj->oo_hl_head != NULL) {
4446 hlock = osd_oti_get(env)->oti_hlock;
4447 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4448 inode, LDISKFS_HLOCK_READDIR);
4450 down_read(&obj->oo_ext_idx_sem);
4453 result = inode->i_fop->readdir(&it->oie_file, it,
4454 (filldir_t) osd_ldiskfs_filldir);
4457 ldiskfs_htree_unlock(hlock);
4459 up_read(&obj->oo_ext_idx_sem);
4461 if (it->oie_rd_dirent == 0) {
4464 it->oie_dirent = it->oie_buf;
4465 it->oie_it_dirent = 1;
4472 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4473 * to load a directory entry at a time and stored it in
4474 * iterator's in-memory data structure.
4476 * \param di iterator's in memory structure
4478 * \retval +ve iterator reached to end
4479 * \retval 0 iterator not reached to end
4480 * \retval -ve on error
4482 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4484 struct osd_it_ea *it = (struct osd_it_ea *)di;
4489 if (it->oie_it_dirent < it->oie_rd_dirent) {
4491 (void *) it->oie_dirent +
4492 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4493 it->oie_dirent->oied_namelen);
4494 it->oie_it_dirent++;
4497 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4500 rc = osd_ldiskfs_it_fill(env, di);
4507 * Returns the key at current position from iterator's in memory structure.
4509 * \param di iterator's in memory structure
4511 * \retval key i.e. struct dt_key on success
4513 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4514 const struct dt_it *di)
4516 struct osd_it_ea *it = (struct osd_it_ea *)di;
4518 return (struct dt_key *)it->oie_dirent->oied_name;
4522 * Returns the key's size at current position from iterator's in memory structure.
4524 * \param di iterator's in memory structure
4526 * \retval key_size i.e. struct dt_key on success
4528 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4530 struct osd_it_ea *it = (struct osd_it_ea *)di;
4532 return it->oie_dirent->oied_namelen;
4536 osd_dirent_update(handle_t *jh, struct super_block *sb,
4537 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4538 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4540 struct osd_fid_pack *rec;
4544 LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4545 LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4547 rc = ldiskfs_journal_get_write_access(jh, bh);
4549 CERROR("%.16s: fail to write access for update dirent: "
4550 "name = %.*s, rc = %d\n",
4551 LDISKFS_SB(sb)->s_es->s_volume_name,
4552 ent->oied_namelen, ent->oied_name, rc);
4556 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4557 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4558 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4560 CERROR("%.16s: fail to dirty metadata for update dirent: "
4561 "name = %.*s, rc = %d\n",
4562 LDISKFS_SB(sb)->s_es->s_volume_name,
4563 ent->oied_namelen, ent->oied_name, rc);
4569 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4571 if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4572 __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4579 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
4581 LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
4582 "dot_dotdot = %d\n", dot_dotdot);
4584 if (LDISKFS_DIR_REC_LEN(de) >=
4585 __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
4592 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4593 struct inode *dir, struct inode *inode,
4594 struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4595 struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4596 struct htree_lock *hlock)
4598 struct dentry *dentry;
4599 struct osd_fid_pack *rec;
4600 struct ldiskfs_dentry_param *ldp;
4604 if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4605 LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4608 /* There is enough space to hold the FID-in-dirent. */
4609 if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4610 dir->i_sb->s_blocksize)) {
4611 rc = ldiskfs_journal_get_write_access(jh, bh);
4613 CERROR("%.16s: fail to write access for reinsert "
4614 "dirent: name = %.*s, rc = %d\n",
4615 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4616 ent->oied_namelen, ent->oied_name, rc);
4620 de->name[de->name_len] = 0;
4621 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4622 rec->fp_len = sizeof(struct lu_fid) + 1;
4623 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4624 de->file_type |= LDISKFS_DIRENT_LUFID;
4626 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4628 CERROR("%.16s: fail to dirty metadata for reinsert "
4629 "dirent: name = %.*s, rc = %d\n",
4630 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4631 ent->oied_namelen, ent->oied_name, rc);
4636 rc = ldiskfs_delete_entry(jh, dir, de, bh);
4638 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4639 "name = %.*s, rc = %d\n",
4640 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4641 ent->oied_namelen, ent->oied_name, rc);
4645 dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4647 ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4648 osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4649 dentry->d_fsdata = (void *)ldp;
4650 ll_vfs_dq_init(dir);
4651 rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4652 /* It is too bad, we cannot reinsert the name entry back.
4653 * That means we lose it! */
4655 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4656 "name = %.*s, rc = %d\n",
4657 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4658 ent->oied_namelen, ent->oied_name, rc);
4664 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4665 struct osd_it_ea *it, struct lu_fid *fid,
4666 struct osd_inode_id *id, __u32 *attr)
4668 struct osd_thread_info *info = osd_oti_get(env);
4669 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
4670 struct osd_device *dev = osd_obj2dev(obj);
4671 struct super_block *sb = osd_sb(dev);
4672 const char *devname =
4673 LDISKFS_SB(sb)->s_es->s_volume_name;
4674 struct osd_it_ea_dirent *ent = it->oie_dirent;
4675 struct inode *dir = obj->oo_inode;
4676 struct htree_lock *hlock = NULL;
4677 struct buffer_head *bh = NULL;
4678 handle_t *jh = NULL;
4679 struct ldiskfs_dir_entry_2 *de;
4680 struct dentry *dentry;
4681 struct inode *inode;
4688 if (ent->oied_name[0] == '.') {
4689 if (ent->oied_namelen == 1)
4691 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4695 dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4698 /* We need to ensure that the name entry is still valid.
4699 * Because it may be removed or renamed by other already.
4701 * The unlink or rename operation will start journal before PDO lock,
4702 * so to avoid deadlock, here we need to start journal handle before
4703 * related PDO lock also. But because we do not know whether there
4704 * will be something to be repaired before PDO lock, we just start
4705 * journal without conditions.
4707 * We may need to remove the name entry firstly, then insert back.
4708 * One credit is for user quota file update.
4709 * One credit is for group quota file update.
4710 * Two credits are for dirty inode. */
4711 credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4712 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4715 if (dev->od_dirent_journal) {
4716 jh = ldiskfs_journal_start_sb(sb, credits);
4719 CERROR("%.16s: fail to start trans for dirent "
4720 "check_repair: credits %d, name %.*s, rc %d\n",
4721 devname, credits, ent->oied_namelen,
4722 ent->oied_name, rc);
4726 if (obj->oo_hl_head != NULL) {
4727 hlock = osd_oti_get(env)->oti_hlock;
4728 /* "0" means exclusive lock for the whole directory.
4729 * We need to prevent others access such name entry
4730 * during the delete + insert. Neither HLOCK_ADD nor
4731 * HLOCK_DEL cannot guarantee the atomicity. */
4732 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
4734 down_write(&obj->oo_ext_idx_sem);
4737 if (obj->oo_hl_head != NULL) {
4738 hlock = osd_oti_get(env)->oti_hlock;
4739 ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
4740 LDISKFS_HLOCK_LOOKUP);
4742 down_read(&obj->oo_ext_idx_sem);
4746 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
4747 /* For dot/dotdot entry, if there is not enough space to hold the
4748 * FID-in-dirent, just keep them there. It only happens when the
4749 * device upgraded from 1.8 or restored from MDT file-level backup.
4750 * For the whole directory, only dot/dotdot entry have no FID-in-dirent
4751 * and needs to get FID from LMA when readdir, it will not affect the
4752 * performance much. */
4753 if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
4754 (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
4755 *attr |= LUDA_IGNORE;
4756 GOTO(out_journal, rc = 0);
4759 osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
4760 inode = osd_iget(info, dev, id);
4761 if (IS_ERR(inode)) {
4762 rc = PTR_ERR(inode);
4763 if (rc == -ENOENT || rc == -ESTALE) {
4764 *attr |= LUDA_IGNORE;
4768 GOTO(out_journal, rc);
4771 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
4773 if (fid_is_sane(fid)) {
4774 /* FID-in-dirent is valid. */
4775 if (lu_fid_eq(fid, &lma->lma_self_fid))
4776 GOTO(out_inode, rc = 0);
4778 /* Do not repair under dryrun mode. */
4779 if (*attr & LUDA_VERIFY_DRYRUN) {
4780 *attr |= LUDA_REPAIR;
4781 GOTO(out_inode, rc = 0);
4784 if (!dev->od_dirent_journal) {
4788 ldiskfs_htree_unlock(hlock);
4790 up_read(&obj->oo_ext_idx_sem);
4791 dev->od_dirent_journal = 1;
4795 *fid = lma->lma_self_fid;
4797 /* Update the FID-in-dirent. */
4798 rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
4800 *attr |= LUDA_REPAIR;
4802 /* Do not repair under dryrun mode. */
4803 if (*attr & LUDA_VERIFY_DRYRUN) {
4804 *fid = lma->lma_self_fid;
4805 *attr |= LUDA_REPAIR;
4806 GOTO(out_inode, rc = 0);
4809 if (!dev->od_dirent_journal) {
4813 ldiskfs_htree_unlock(hlock);
4815 up_read(&obj->oo_ext_idx_sem);
4816 dev->od_dirent_journal = 1;
4820 *fid = lma->lma_self_fid;
4822 /* Append the FID-in-dirent. */
4823 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4824 fid, bh, de, hlock);
4826 *attr |= LUDA_REPAIR;
4828 } else if (rc == -ENODATA) {
4829 /* Do not repair under dryrun mode. */
4830 if (*attr & LUDA_VERIFY_DRYRUN) {
4831 if (fid_is_sane(fid)) {
4832 *attr |= LUDA_REPAIR;
4834 lu_igif_build(fid, inode->i_ino,
4835 inode->i_generation);
4836 *attr |= LUDA_UPGRADE;
4838 GOTO(out_inode, rc = 0);
4841 if (!dev->od_dirent_journal) {
4845 ldiskfs_htree_unlock(hlock);
4847 up_read(&obj->oo_ext_idx_sem);
4848 dev->od_dirent_journal = 1;
4853 if (unlikely(fid_is_sane(fid))) {
4854 /* FID-in-dirent exists, but FID-in-LMA is lost.
4855 * Trust the FID-in-dirent, and add FID-in-LMA. */
4856 rc = osd_ea_fid_set(info, inode, fid, 0);
4858 *attr |= LUDA_REPAIR;
4860 lu_igif_build(fid, inode->i_ino, inode->i_generation);
4861 /* It is probably IGIF object. Only aappend the
4862 * FID-in-dirent. OI scrub will process FID-in-LMA. */
4863 rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
4864 fid, bh, de, hlock);
4866 *attr |= LUDA_UPGRADE;
4870 GOTO(out_inode, rc);
4877 if (hlock != NULL) {
4878 ldiskfs_htree_unlock(hlock);
4880 if (dev->od_dirent_journal)
4881 up_write(&obj->oo_ext_idx_sem);
4883 up_read(&obj->oo_ext_idx_sem);
4886 ldiskfs_journal_stop(jh);
4887 if (rc >= 0 && !dirty)
4888 dev->od_dirent_journal = 0;
4893 * Returns the value at current position from iterator's in memory structure.
4895 * \param di struct osd_it_ea, iterator's in memory structure
4896 * \param attr attr requested for dirent.
4897 * \param lde lustre dirent
4899 * \retval 0 no error and \param lde has correct lustre dirent.
4900 * \retval -ve on error
4902 static inline int osd_it_ea_rec(const struct lu_env *env,
4903 const struct dt_it *di,
4904 struct dt_rec *dtrec, __u32 attr)
4906 struct osd_it_ea *it = (struct osd_it_ea *)di;
4907 struct osd_object *obj = it->oie_obj;
4908 struct osd_device *dev = osd_obj2dev(obj);
4909 struct osd_scrub *scrub = &dev->od_scrub;
4910 struct scrub_file *sf = &scrub->os_file;
4911 struct osd_thread_info *oti = osd_oti_get(env);
4912 struct osd_inode_id *id = &oti->oti_id;
4913 struct osd_idmap_cache *oic = &oti->oti_cache;
4914 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4915 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4916 __u32 ino = it->oie_dirent->oied_ino;
4920 if (attr & LUDA_VERIFY) {
4922 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
4923 attr |= LUDA_IGNORE;
4928 rc = osd_dirent_check_repair(env, obj, it, fid, id, &attr);
4930 attr &= ~LU_DIRENT_ATTRS_MASK;
4931 if (!fid_is_sane(fid)) {
4932 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
4935 rc = osd_ea_fid_get(env, obj, ino, fid, id);
4937 osd_id_gen(id, ino, OSD_OII_NOGEN);
4945 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4946 it->oie_dirent->oied_name,
4947 it->oie_dirent->oied_namelen,
4948 it->oie_dirent->oied_type, attr);
4950 if (osd_remote_fid(env, dev, fid))
4953 if (likely(!(attr & LUDA_IGNORE))) {
4955 oic->oic_fid = *fid;
4958 if (!(attr & LUDA_VERIFY) &&
4959 (scrub->os_pos_current <= ino) &&
4960 ((sf->sf_flags & SF_INCONSISTENT) ||
4961 (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
4962 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4963 osd_consistency_check(oti, dev, oic);
4969 * Returns a cookie for current position of the iterator head, so that
4970 * user can use this cookie to load/start the iterator next time.
4972 * \param di iterator's in memory structure
4974 * \retval cookie for current position, on success
4976 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4978 struct osd_it_ea *it = (struct osd_it_ea *)di;
4980 return it->oie_dirent->oied_off;
4984 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4985 * to load a directory entry at a time and stored it i inn,
4986 * in iterator's in-memory data structure.
4988 * \param di struct osd_it_ea, iterator's in memory structure
4990 * \retval +ve on success
4991 * \retval -ve on error
4993 static int osd_it_ea_load(const struct lu_env *env,
4994 const struct dt_it *di, __u64 hash)
4996 struct osd_it_ea *it = (struct osd_it_ea *)di;
5000 it->oie_file.f_pos = hash;
5002 rc = osd_ldiskfs_it_fill(env, di);
5010 * Index lookup function for interoperability mode (b11826).
5012 * \param key, key i.e. file name to be searched
5014 * \retval +ve, on success
5015 * \retval -ve, on error
5017 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5018 struct dt_rec *rec, const struct dt_key *key,
5019 struct lustre_capa *capa)
5021 struct osd_object *obj = osd_dt_obj(dt);
5026 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5027 LINVRNT(osd_invariant(obj));
5029 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5032 rc = osd_ea_lookup_rec(env, obj, rec, key);
5039 * Index and Iterator operations for interoperability
5040 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5042 static const struct dt_index_operations osd_index_ea_ops = {
5043 .dio_lookup = osd_index_ea_lookup,
5044 .dio_declare_insert = osd_index_declare_ea_insert,
5045 .dio_insert = osd_index_ea_insert,
5046 .dio_declare_delete = osd_index_declare_ea_delete,
5047 .dio_delete = osd_index_ea_delete,
5049 .init = osd_it_ea_init,
5050 .fini = osd_it_ea_fini,
5051 .get = osd_it_ea_get,
5052 .put = osd_it_ea_put,
5053 .next = osd_it_ea_next,
5054 .key = osd_it_ea_key,
5055 .key_size = osd_it_ea_key_size,
5056 .rec = osd_it_ea_rec,
5057 .store = osd_it_ea_store,
5058 .load = osd_it_ea_load
5062 static void *osd_key_init(const struct lu_context *ctx,
5063 struct lu_context_key *key)
5065 struct osd_thread_info *info;
5067 OBD_ALLOC_PTR(info);
5069 return ERR_PTR(-ENOMEM);
5071 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5072 if (info->oti_it_ea_buf == NULL)
5075 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5077 info->oti_hlock = ldiskfs_htree_lock_alloc();
5078 if (info->oti_hlock == NULL)
5084 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5087 return ERR_PTR(-ENOMEM);
5090 static void osd_key_fini(const struct lu_context *ctx,
5091 struct lu_context_key *key, void* data)
5093 struct osd_thread_info *info = data;
5095 if (info->oti_hlock != NULL)
5096 ldiskfs_htree_lock_free(info->oti_hlock);
5097 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5098 lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5099 lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5103 static void osd_key_exit(const struct lu_context *ctx,
5104 struct lu_context_key *key, void *data)
5106 struct osd_thread_info *info = data;
5108 LASSERT(info->oti_r_locks == 0);
5109 LASSERT(info->oti_w_locks == 0);
5110 LASSERT(info->oti_txns == 0);
5113 /* type constructor/destructor: osd_type_init, osd_type_fini */
5114 LU_TYPE_INIT_FINI(osd, &osd_key);
5116 struct lu_context_key osd_key = {
5117 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5118 .lct_init = osd_key_init,
5119 .lct_fini = osd_key_fini,
5120 .lct_exit = osd_key_exit
5124 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5125 const char *name, struct lu_device *next)
5127 struct osd_device *osd = osd_dev(d);
5129 if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5130 >= sizeof(osd->od_svname))
5132 return osd_procfs_init(osd, name);
5135 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5139 osd_scrub_cleanup(env, o);
5142 fsfilt_put_ops(o->od_fsops);
5146 /* shutdown quota slave instance associated with the device */
5147 if (o->od_quota_slave != NULL) {
5148 qsd_fini(env, o->od_quota_slave);
5149 o->od_quota_slave = NULL;
5155 static int osd_mount(const struct lu_env *env,
5156 struct osd_device *o, struct lustre_cfg *cfg)
5158 const char *name = lustre_cfg_string(cfg, 0);
5159 const char *dev = lustre_cfg_string(cfg, 1);
5161 unsigned long page, s_flags, lmd_flags = 0;
5162 struct page *__page;
5163 struct file_system_type *type;
5164 char *options = NULL;
5169 if (o->od_mnt != NULL)
5172 if (strlen(dev) >= sizeof(o->od_mntdev))
5174 strcpy(o->od_mntdev, dev);
5176 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
5177 if (o->od_fsops == NULL) {
5178 CERROR("Can't find fsfilt_ldiskfs\n");
5182 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
5184 GOTO(out, rc = -ENOMEM);
5186 str = lustre_cfg_string(cfg, 2);
5187 s_flags = simple_strtoul(str, NULL, 0);
5188 str = strstr(str, ":");
5190 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5191 opts = lustre_cfg_string(cfg, 3);
5192 page = (unsigned long)cfs_page_address(__page);
5193 options = (char *)page;
5196 strcat(options, "user_xattr,acl");
5198 strcat(options, opts);
5200 /* Glom up mount options */
5201 if (*options != '\0')
5202 strcat(options, ",");
5203 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
5205 type = get_fs_type("ldiskfs");
5207 CERROR("%s: cannot find ldiskfs module\n", name);
5208 GOTO(out, rc = -ENODEV);
5211 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5212 cfs_module_put(type->owner);
5214 if (IS_ERR(o->od_mnt)) {
5215 rc = PTR_ERR(o->od_mnt);
5216 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5221 #ifdef HAVE_DEV_SET_RDONLY
5222 if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5223 CERROR("%s: underlying device %s is marked as read-only. "
5224 "Setup failed\n", name, dev);
5227 GOTO(out, rc = -EROFS);
5231 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5232 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5233 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5236 GOTO(out, rc = -EINVAL);
5239 ldiskfs_set_inode_state(osd_sb(o)->s_root->d_inode,
5240 LDISKFS_STATE_LUSTRE_NO_OI);
5241 if (lmd_flags & LMD_FLG_NOSCRUB)
5246 OBD_PAGE_FREE(__page);
5248 fsfilt_put_ops(o->od_fsops);
5253 static struct lu_device *osd_device_fini(const struct lu_env *env,
5254 struct lu_device *d)
5259 rc = osd_shutdown(env, osd_dev(d));
5261 osd_obj_map_fini(osd_dev(d));
5263 shrink_dcache_sb(osd_sb(osd_dev(d)));
5264 osd_sync(env, lu2dt_dev(d));
5266 rc = osd_procfs_fini(osd_dev(d));
5268 CERROR("proc fini error %d \n", rc);
5269 RETURN (ERR_PTR(rc));
5272 if (osd_dev(d)->od_mnt) {
5273 mntput(osd_dev(d)->od_mnt);
5274 osd_dev(d)->od_mnt = NULL;
5280 static int osd_device_init0(const struct lu_env *env,
5281 struct osd_device *o,
5282 struct lustre_cfg *cfg)
5284 struct lu_device *l = osd2lu_dev(o);
5285 struct osd_thread_info *info;
5289 /* if the module was re-loaded, env can loose its keys */
5290 rc = lu_env_refill((struct lu_env *) env);
5293 info = osd_oti_get(env);
5296 l->ld_ops = &osd_lu_ops;
5297 o->od_dt_dev.dd_ops = &osd_dt_ops;
5299 spin_lock_init(&o->od_osfs_lock);
5300 mutex_init(&o->od_otable_mutex);
5301 o->od_osfs_age = cfs_time_shift_64(-1000);
5303 o->od_capa_hash = init_capa_hash();
5304 if (o->od_capa_hash == NULL)
5305 GOTO(out, rc = -ENOMEM);
5307 o->od_read_cache = 1;
5308 o->od_writethrough_cache = 1;
5309 o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5311 rc = osd_mount(env, o, cfg);
5315 CFS_INIT_LIST_HEAD(&o->od_ios_list);
5316 /* setup scrub, including OI files initialization */
5317 rc = osd_scrub_setup(env, o);
5321 cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5322 sizeof(o->od_svname));
5323 if (cplen >= sizeof(o->od_svname)) {
5328 rc = osd_obj_map_init(env, o);
5330 GOTO(out_scrub, rc);
5332 rc = lu_site_init(&o->od_site, l);
5334 GOTO(out_compat, rc);
5335 o->od_site.ls_bottom_dev = l;
5337 rc = lu_site_init_finish(&o->od_site);
5341 rc = osd_procfs_init(o, o->od_svname);
5343 CERROR("%s: can't initialize procfs: rc = %d\n",
5348 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5350 /* initialize quota slave instance */
5351 o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5353 if (IS_ERR(o->od_quota_slave)) {
5354 rc = PTR_ERR(o->od_quota_slave);
5355 o->od_quota_slave = NULL;
5356 GOTO(out_procfs, rc);
5363 lu_site_fini(&o->od_site);
5365 osd_obj_map_fini(o);
5367 osd_scrub_cleanup(env, o);
5369 osd_oi_fini(info, o);
5370 osd_shutdown(env, o);
5374 cleanup_capa_hash(o->od_capa_hash);
5379 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5380 struct lu_device_type *t,
5381 struct lustre_cfg *cfg)
5383 struct osd_device *o;
5388 return ERR_PTR(-ENOMEM);
5390 rc = dt_device_init(&o->od_dt_dev, t);
5392 /* Because the ctx might be revived in dt_device_init,
5393 * refill the env here */
5394 lu_env_refill((struct lu_env *)env);
5395 rc = osd_device_init0(env, o, cfg);
5397 dt_device_fini(&o->od_dt_dev);
5400 if (unlikely(rc != 0))
5403 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5406 static struct lu_device *osd_device_free(const struct lu_env *env,
5407 struct lu_device *d)
5409 struct osd_device *o = osd_dev(d);
5412 cleanup_capa_hash(o->od_capa_hash);
5413 /* XXX: make osd top device in order to release reference */
5414 d->ld_site->ls_top_dev = d;
5415 lu_site_purge(env, d->ld_site, -1);
5416 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5417 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5418 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5420 lu_site_fini(&o->od_site);
5421 dt_device_fini(&o->od_dt_dev);
5426 static int osd_process_config(const struct lu_env *env,
5427 struct lu_device *d, struct lustre_cfg *cfg)
5429 struct osd_device *o = osd_dev(d);
5433 switch(cfg->lcfg_command) {
5435 err = osd_mount(env, o, cfg);
5438 lu_dev_del_linkage(d->ld_site, d);
5439 err = osd_shutdown(env, o);
5448 static int osd_recovery_complete(const struct lu_env *env,
5449 struct lu_device *d)
5451 struct osd_device *osd = osd_dev(d);
5455 if (osd->od_quota_slave == NULL)
5458 /* start qsd instance on recovery completion, this notifies the quota
5459 * slave code that we are about to process new requests now */
5460 rc = qsd_start(env, osd->od_quota_slave);
5465 * we use exports to track all osd users
5467 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5468 struct obd_device *obd, struct obd_uuid *cluuid,
5469 struct obd_connect_data *data, void *localdata)
5471 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5472 struct lustre_handle conn;
5476 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5478 rc = class_connect(&conn, obd, cluuid);
5482 *exp = class_conn2export(&conn);
5484 spin_lock(&osd->od_osfs_lock);
5486 spin_unlock(&osd->od_osfs_lock);
5492 * once last export (we don't count self-export) disappeared
5493 * osd can be released
5495 static int osd_obd_disconnect(struct obd_export *exp)
5497 struct obd_device *obd = exp->exp_obd;
5498 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5499 int rc, release = 0;
5502 /* Only disconnect the underlying layers on the final disconnect. */
5503 spin_lock(&osd->od_osfs_lock);
5505 if (osd->od_connects == 0)
5507 spin_unlock(&osd->od_osfs_lock);
5509 rc = class_disconnect(exp); /* bz 9811 */
5511 if (rc == 0 && release)
5512 class_manual_cleanup(obd);
5516 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5517 struct lu_device *dev)
5519 struct osd_device *osd = osd_dev(dev);
5523 if (dev->ld_site && lu_device_is_md(dev->ld_site->ls_top_dev)) {
5524 /* MDT/MDD still use old infrastructure to create
5526 result = llo_local_objects_setup(env, lu2md_dev(pdev),
5532 if (osd->od_quota_slave != NULL)
5533 /* set up quota slave objects */
5534 result = qsd_prepare(env, osd->od_quota_slave);
5539 static const struct lu_object_operations osd_lu_obj_ops = {
5540 .loo_object_init = osd_object_init,
5541 .loo_object_delete = osd_object_delete,
5542 .loo_object_release = osd_object_release,
5543 .loo_object_free = osd_object_free,
5544 .loo_object_print = osd_object_print,
5545 .loo_object_invariant = osd_object_invariant
5548 const struct lu_device_operations osd_lu_ops = {
5549 .ldo_object_alloc = osd_object_alloc,
5550 .ldo_process_config = osd_process_config,
5551 .ldo_recovery_complete = osd_recovery_complete,
5552 .ldo_prepare = osd_prepare,
5555 static const struct lu_device_type_operations osd_device_type_ops = {
5556 .ldto_init = osd_type_init,
5557 .ldto_fini = osd_type_fini,
5559 .ldto_start = osd_type_start,
5560 .ldto_stop = osd_type_stop,
5562 .ldto_device_alloc = osd_device_alloc,
5563 .ldto_device_free = osd_device_free,
5565 .ldto_device_init = osd_device_init,
5566 .ldto_device_fini = osd_device_fini
5569 struct lu_device_type osd_device_type = {
5570 .ldt_tags = LU_DEVICE_DT,
5571 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
5572 .ldt_ops = &osd_device_type_ops,
5573 .ldt_ctx_tags = LCT_LOCAL,
5577 * lprocfs legacy support.
5579 static struct obd_ops osd_obd_device_ops = {
5580 .o_owner = THIS_MODULE,
5581 .o_connect = osd_obd_connect,
5582 .o_disconnect = osd_obd_disconnect
5585 static int __init osd_mod_init(void)
5587 struct lprocfs_static_vars lvars;
5590 lprocfs_osd_init_vars(&lvars);
5591 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
5592 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5595 static void __exit osd_mod_exit(void)
5597 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5600 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5601 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5602 MODULE_LICENSE("GPL");
5604 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);