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, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
60 * struct OBD_{ALLOC,FREE}*()
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
68 #include <lustre_fid.h>
70 #include "osd_internal.h"
73 /* llo_* api support */
74 #include <md_object.h>
75 /* dt_acct_features */
78 #ifdef HAVE_LDISKFS_PDO
80 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
81 "ldiskfs with parallel directory operations");
86 static const char dot[] = ".";
87 static const char dotdot[] = "..";
88 static const char remote_obj_dir[] = "REM_OBJ_DIR";
90 static const struct lu_object_operations osd_lu_obj_ops;
91 static const struct dt_object_operations osd_obj_ops;
92 static const struct dt_object_operations osd_obj_ea_ops;
93 static const struct dt_object_operations osd_obj_otable_it_ops;
94 static const struct dt_index_operations osd_index_iam_ops;
95 static const struct dt_index_operations osd_index_ea_ops;
97 static int osd_has_index(const struct osd_object *obj)
99 return obj->oo_dt.do_index_ops != NULL;
102 static int osd_object_invariant(const struct lu_object *l)
104 return osd_invariant(osd_obj(l));
107 #ifdef HAVE_QUOTA_SUPPORT
109 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save, bool is_md)
122 save->oc_uid = current_fsuid();
123 save->oc_gid = current_fsgid();
124 save->oc_cap = current_cap();
125 if ((tc = prepare_creds())) {
126 tc->fsuid = uc->mu_fsuid;
127 tc->fsgid = uc->mu_fsgid;
130 /* XXX not suboptimal */
131 cfs_curproc_cap_unpack(uc->mu_cap);
135 osd_pop_ctxt(struct osd_ctxt *save, bool is_md)
143 if ((tc = prepare_creds())) {
144 tc->fsuid = save->oc_uid;
145 tc->fsgid = save->oc_gid;
146 tc->cap_effective = save->oc_cap;
153 * Concurrency: doesn't matter
155 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
157 return osd_oti_get(env)->oti_r_locks > 0;
161 * Concurrency: doesn't matter
163 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
165 struct osd_thread_info *oti = osd_oti_get(env);
166 return oti->oti_w_locks > 0 && o->oo_owner == env;
170 * Concurrency: doesn't access mutable data
172 static int osd_root_get(const struct lu_env *env,
173 struct dt_device *dev, struct lu_fid *f)
175 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
179 static inline int osd_qid_type(struct osd_thandle *oh, int i)
181 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
184 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
186 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
189 void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
190 int type, uid_t id, struct inode *inode)
193 int i, allocated = 0;
194 struct osd_object *obj;
198 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
201 /* id entry is allocated in the quota file */
202 if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
205 for (i = 0; i < oh->ot_id_cnt; i++) {
206 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
210 if (unlikely(i >= OSD_MAX_UGID_CNT)) {
211 CERROR("more than %d uid/gids for a transaction?\n", i);
215 oh->ot_id_array[i] = id;
216 osd_qid_set_type(oh, i, type);
218 obj = osd_dt_obj(dt);
219 oh->ot_credits += (allocated || id == 0) ?
220 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
225 * OSD object methods.
229 * Concurrency: no concurrent access is possible that early in object
232 static struct lu_object *osd_object_alloc(const struct lu_env *env,
233 const struct lu_object_header *hdr,
236 struct osd_object *mo;
242 l = &mo->oo_dt.do_lu;
243 dt_object_init(&mo->oo_dt, NULL, d);
244 if (osd_dev(d)->od_iop_mode)
245 mo->oo_dt.do_ops = &osd_obj_ea_ops;
247 mo->oo_dt.do_ops = &osd_obj_ops;
249 l->lo_ops = &osd_lu_obj_ops;
250 cfs_init_rwsem(&mo->oo_sem);
251 cfs_init_rwsem(&mo->oo_ext_idx_sem);
252 cfs_spin_lock_init(&mo->oo_guard);
259 static int osd_get_lma(struct inode *inode, struct dentry *dentry,
260 struct lustre_mdt_attrs *lma)
264 dentry->d_inode = inode;
265 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
268 /* Check LMA compatibility */
269 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
270 CWARN("%.16s: unsupported incompat LMA feature(s) "
272 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
273 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
277 lustre_lma_swab(lma);
280 } else if (rc == 0) {
288 * retrieve object from backend ext fs.
290 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
291 struct osd_inode_id *id)
293 struct inode *inode = NULL;
295 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
297 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
298 id->oii_ino, PTR_ERR(inode));
299 } else if (id->oii_gen != OSD_OII_NOGEN &&
300 inode->i_generation != id->oii_gen) {
301 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
303 id->oii_ino, id->oii_gen, inode->i_generation);
305 inode = ERR_PTR(-ESTALE);
306 } else if (inode->i_nlink == 0) {
307 /* due to parallel readdir and unlink,
308 * we can have dead inode here. */
309 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
310 make_bad_inode(inode);
312 inode = ERR_PTR(-ESTALE);
313 } else if (is_bad_inode(inode)) {
314 CWARN("%.16s: bad inode: ino = %u\n",
315 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
317 inode = ERR_PTR(-ENOENT);
319 if (id->oii_gen == OSD_OII_NOGEN)
320 osd_id_gen(id, inode->i_ino, inode->i_generation);
322 /* Do not update file c/mtime in ldiskfs.
323 * NB: we don't have any lock to protect this because we don't
324 * have reference on osd_object now, but contention with
325 * another lookup + attr_set can't happen in the tiny window
326 * between if (...) and set S_NOCMTIME. */
327 if (!(inode->i_flags & S_NOCMTIME))
328 inode->i_flags |= S_NOCMTIME;
333 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
334 struct osd_inode_id *id, struct lu_fid *fid)
336 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
340 inode = osd_iget(info, dev, id);
344 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
346 *fid = lma->lma_self_fid;
347 } else if (rc == -ENODATA) {
348 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
356 static struct inode *
357 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
358 struct osd_inode_id *id, const struct lu_fid *fid)
360 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
364 inode = osd_iget(info, dev, id);
368 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
370 if (rc == -ENODATA) {
371 CDEBUG(D_LFSCK, "inconsistent obj: NULL, %lu, "DFID"\n",
372 inode->i_ino, PFID(fid));
379 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
380 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
381 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
383 return ERR_PTR(EREMCHG);
388 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
389 const struct lu_fid *fid,
390 const struct lu_object_conf *conf)
392 struct osd_thread_info *info;
393 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
394 struct osd_device *dev;
395 struct osd_idmap_cache *oic;
396 struct osd_inode_id *id;
398 struct osd_scrub *scrub;
399 struct scrub_file *sf;
404 LINVRNT(osd_invariant(obj));
405 LASSERT(obj->oo_inode == NULL);
406 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
409 scrub = &dev->od_scrub;
410 sf = &scrub->os_file;
411 info = osd_oti_get(env);
413 oic = &info->oti_cache;
416 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
419 if (fid_is_norm(fid)) {
420 /* Search order: 1. per-thread cache. */
421 if (lu_fid_eq(fid, &oic->oic_fid)) {
423 } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
424 /* Search order: 2. OI scrub pending list. */
425 result = osd_oii_lookup(dev, fid, id);
430 if (sf->sf_flags & SF_INCONSISTENT)
435 * Objects are created as locking anchors or place holders for objects
436 * yet to be created. No need to osd_oi_lookup() at here because FID
437 * shouldn't never be re-used, if it's really a duplicate FID from
438 * unexpected reason, we should be able to detect it later by calling
439 * do_create->osd_oi_insert()
441 if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
442 GOTO(out, result = 0);
444 /* Search order: 3. OI files. */
445 result = osd_oi_lookup(info, dev, fid, id);
446 if (result == -ENOENT) {
447 if (!fid_is_norm(fid) ||
448 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
450 GOTO(out, result = 0);
460 inode = osd_iget(info, dev, id);
462 inode = osd_iget_verify(info, dev, id, fid);
464 result = PTR_ERR(inode);
465 if (result == -ENOENT || result == -ESTALE) {
466 fid_zero(&oic->oic_fid);
468 } else if (result == -EREMCHG) {
471 if (thread_is_running(&scrub->os_thread)) {
472 result = -EINPROGRESS;
473 } else if (!scrub->os_no_scrub) {
474 result = osd_scrub_start(dev);
475 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
476 "for "DFID", rc = %d [1]\n",
477 LDISKFS_SB(osd_sb(dev))->s_es->\
478 s_volume_name,PFID(fid), result);
479 if (result == 0 || result == -EALREADY)
480 result = -EINPROGRESS;
489 obj->oo_inode = inode;
490 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
491 if (dev->od_iop_mode) {
492 obj->oo_compat_dot_created = 1;
493 obj->oo_compat_dotdot_created = 1;
496 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
497 GOTO(out, result = 0);
499 LASSERT(obj->oo_hl_head == NULL);
500 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
501 if (obj->oo_hl_head == NULL) {
502 obj->oo_inode = NULL;
504 GOTO(out, result = -ENOMEM);
506 GOTO(out, result = 0);
509 LINVRNT(osd_invariant(obj));
514 * Concurrency: shouldn't matter.
516 static void osd_object_init0(struct osd_object *obj)
518 LASSERT(obj->oo_inode != NULL);
519 obj->oo_dt.do_body_ops = &osd_body_ops;
520 obj->oo_dt.do_lu.lo_header->loh_attr |=
521 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
525 * Concurrency: no concurrent access is possible that early in object
528 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
529 const struct lu_object_conf *conf)
531 struct osd_object *obj = osd_obj(l);
534 LINVRNT(osd_invariant(obj));
536 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
537 obj->oo_dt.do_body_ops = &osd_body_ops_new;
539 if (obj->oo_inode != NULL) {
540 osd_object_init0(obj);
541 } else if (fid_is_otable_it(&l->lo_header->loh_fid)) {
542 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
543 /* LFSCK iterator object is special without inode */
544 l->lo_header->loh_attr |= LOHA_EXISTS;
547 LINVRNT(osd_invariant(obj));
552 * Concurrency: no concurrent access is possible that late in object
555 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
557 struct osd_object *obj = osd_obj(l);
559 LINVRNT(osd_invariant(obj));
561 dt_object_fini(&obj->oo_dt);
562 if (obj->oo_hl_head != NULL)
563 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
568 * Concurrency: no concurrent access is possible that late in object
571 static void osd_index_fini(struct osd_object *o)
573 struct iam_container *bag;
575 if (o->oo_dir != NULL) {
576 bag = &o->oo_dir->od_container;
577 if (o->oo_inode != NULL) {
578 if (bag->ic_object == o->oo_inode)
579 iam_container_fini(bag);
581 OBD_FREE_PTR(o->oo_dir);
587 * Concurrency: no concurrent access is possible that late in object
588 * life-cycle (for all existing callers, that is. New callers have to provide
589 * their own locking.)
591 static int osd_inode_unlinked(const struct inode *inode)
593 return inode->i_nlink == 0;
597 OSD_TXN_OI_DELETE_CREDITS = 20,
598 OSD_TXN_INODE_DELETE_CREDITS = 20
605 #if OSD_THANDLE_STATS
607 * Set time when the handle is allocated
609 static void osd_th_alloced(struct osd_thandle *oth)
611 oth->oth_alloced = cfs_time_current();
615 * Set time when the handle started
617 static void osd_th_started(struct osd_thandle *oth)
619 oth->oth_started = cfs_time_current();
623 * Helper function to convert time interval to microseconds packed in
624 * long int (default time units for the counter in "stats" initialized
625 * by lu_time_init() )
627 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
631 cfs_duration_usec(cfs_time_sub(end, start), &val);
632 return val.tv_sec * 1000000 + val.tv_usec;
636 * Check whether the we deal with this handle for too long.
638 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
639 cfs_time_t alloced, cfs_time_t started,
642 cfs_time_t now = cfs_time_current();
644 LASSERT(dev != NULL);
646 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
647 interval_to_usec(alloced, started));
648 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
649 interval_to_usec(started, closed));
650 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
651 interval_to_usec(closed, now));
653 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
654 CWARN("transaction handle %p was open for too long: "
656 "alloced "CFS_TIME_T" ,"
657 "started "CFS_TIME_T" ,"
658 "closed "CFS_TIME_T"\n",
659 oth, now, alloced, started, closed);
660 libcfs_debug_dumpstack(NULL);
664 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
666 cfs_time_t __closed = cfs_time_current(); \
667 cfs_time_t __alloced = oth->oth_alloced; \
668 cfs_time_t __started = oth->oth_started; \
671 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
674 #else /* OSD_THANDLE_STATS */
676 #define osd_th_alloced(h) do {} while(0)
677 #define osd_th_started(h) do {} while(0)
678 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
680 #endif /* OSD_THANDLE_STATS */
683 * Concurrency: doesn't access mutable data.
685 static int osd_param_is_sane(const struct osd_device *dev,
686 const struct thandle *th)
688 struct osd_thandle *oh;
689 oh = container_of0(th, struct osd_thandle, ot_super);
690 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
694 * Concurrency: shouldn't matter.
696 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
697 static void osd_trans_commit_cb(struct super_block *sb,
698 struct journal_callback *jcb, int error)
700 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
703 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
704 struct thandle *th = &oh->ot_super;
705 struct lu_device *lud = &th->th_dev->dd_lu_dev;
706 struct dt_txn_commit_cb *dcb, *tmp;
708 LASSERT(oh->ot_handle == NULL);
711 CERROR("transaction @0x%p commit error: %d\n", th, error);
713 dt_txn_hook_commit(th);
715 /* call per-transaction callbacks if any */
716 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
717 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
718 "commit callback entry: magic=%x name='%s'\n",
719 dcb->dcb_magic, dcb->dcb_name);
720 cfs_list_del_init(&dcb->dcb_linkage);
721 dcb->dcb_func(NULL, th, dcb, error);
724 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
728 lu_context_exit(&th->th_ctx);
729 lu_context_fini(&th->th_ctx);
733 static struct thandle *osd_trans_create(const struct lu_env *env,
736 struct osd_thread_info *oti = osd_oti_get(env);
737 struct osd_iobuf *iobuf = &oti->oti_iobuf;
738 struct osd_thandle *oh;
742 /* on pending IO in this thread should left from prev. request */
743 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
745 th = ERR_PTR(-ENOMEM);
746 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
751 th->th_tags = LCT_TX_HANDLE;
753 oti->oti_dev = osd_dt_dev(d);
754 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
761 * Concurrency: shouldn't matter.
763 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
766 struct osd_thread_info *oti = osd_oti_get(env);
767 struct osd_device *dev = osd_dt_dev(d);
769 struct osd_thandle *oh;
774 LASSERT(current->journal_info == NULL);
776 oh = container_of0(th, struct osd_thandle, ot_super);
778 LASSERT(oh->ot_handle == NULL);
780 rc = dt_txn_hook_start(env, d, th);
784 if (!osd_param_is_sane(dev, th)) {
785 CWARN("%.16s: too many transaction credits (%d > %d)\n",
786 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
788 osd_journal(dev)->j_max_transaction_buffers);
789 /* XXX Limit the credits to 'max_transaction_buffers', and
790 * let the underlying filesystem to catch the error if
791 * we really need so many credits.
793 * This should be removed when we can calculate the
794 * credits precisely. */
795 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
796 #ifdef OSD_TRACK_DECLARES
797 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
798 oh->ot_declare_attr_set, oh->ot_declare_punch,
799 oh->ot_declare_xattr_set);
800 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
801 oh->ot_declare_create, oh->ot_declare_ref_add,
802 oh->ot_declare_ref_del, oh->ot_declare_write);
803 CERROR(" insert: %d, delete: %d, destroy: %d\n",
804 oh->ot_declare_insert, oh->ot_declare_delete,
805 oh->ot_declare_destroy);
810 * XXX temporary stuff. Some abstraction layer should
813 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
817 LASSERT(oti->oti_txns == 0);
818 lu_context_init(&th->th_ctx, th->th_tags);
819 lu_context_enter(&th->th_ctx);
821 lu_device_get(&d->dd_lu_dev);
822 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
826 * XXX: current rule is that we first start tx,
827 * then lock object(s), but we can't use
828 * this rule for data (due to locking specifics
829 * in ldiskfs). also in long-term we'd like to
830 * use usually-used (locks;tx) ordering. so,
831 * UGLY thing is that we'll use one ordering for
832 * data (ofd) and reverse ordering for metadata
833 * (mdd). then at some point we'll fix the latter
836 LASSERT(oti->oti_r_locks == 0);
837 LASSERT(oti->oti_w_locks == 0);
850 * Concurrency: shouldn't matter.
852 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
855 struct osd_thandle *oh;
856 struct osd_thread_info *oti = osd_oti_get(env);
857 struct osd_iobuf *iobuf = &oti->oti_iobuf;
861 oh = container_of0(th, struct osd_thandle, ot_super);
863 if (oh->ot_handle != NULL) {
864 handle_t *hdl = oh->ot_handle;
866 hdl->h_sync = th->th_sync;
869 * add commit callback
870 * notice we don't do this in osd_trans_start()
871 * as underlying transaction can change during truncate
873 osd_journal_callback_set(hdl, osd_trans_commit_cb,
876 LASSERT(oti->oti_txns == 1);
879 * XXX: current rule is that we first start tx,
880 * then lock object(s), but we can't use
881 * this rule for data (due to locking specifics
882 * in ldiskfs). also in long-term we'd like to
883 * use usually-used (locks;tx) ordering. so,
884 * UGLY thing is that we'll use one ordering for
885 * data (ofd) and reverse ordering for metadata
886 * (mdd). then at some point we'll fix the latter
888 if (osd_dt_dev(th->th_dev)->od_is_md) {
889 LASSERT(oti->oti_r_locks == 0);
890 LASSERT(oti->oti_w_locks == 0);
892 rc = dt_txn_hook_stop(env, th);
894 CERROR("Failure in transaction hook: %d\n", rc);
895 oh->ot_handle = NULL;
896 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
897 rc = ldiskfs_journal_stop(hdl));
899 CERROR("Failure to stop transaction: %d\n", rc);
904 /* as we want IO to journal and data IO be concurrent, we don't block
905 * awaiting data IO completion in osd_do_bio(), instead we wait here
906 * once transaction is submitted to the journal. all reqular requests
907 * don't do direct IO (except read/write), thus this wait_event becomes
910 * IMPORTANT: we have to wait till any IO submited by the thread is
911 * completed otherwise iobuf may be corrupted by different request
913 cfs_wait_event(iobuf->dr_wait,
914 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
916 rc = iobuf->dr_error;
921 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
923 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
926 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
927 LASSERT(&dcb->dcb_func != NULL);
928 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
934 * Called just before object is freed. Releases all resources except for
935 * object itself (that is released by osd_object_free()).
937 * Concurrency: no concurrent access is possible that late in object
940 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
942 struct osd_object *obj = osd_obj(l);
943 struct inode *inode = obj->oo_inode;
945 LINVRNT(osd_invariant(obj));
948 * If object is unlinked remove fid->ino mapping from object index.
954 obj->oo_inode = NULL;
959 * Concurrency: ->loo_object_release() is called under site spin-lock.
961 static void osd_object_release(const struct lu_env *env,
967 * Concurrency: shouldn't matter.
969 static int osd_object_print(const struct lu_env *env, void *cookie,
970 lu_printer_t p, const struct lu_object *l)
972 struct osd_object *o = osd_obj(l);
975 if (o->oo_dir != NULL)
976 d = o->oo_dir->od_container.ic_descr;
979 return (*p)(env, cookie,
980 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
982 o->oo_inode ? o->oo_inode->i_ino : 0UL,
983 o->oo_inode ? o->oo_inode->i_generation : 0,
984 d ? d->id_ops->id_name : "plain");
988 * Concurrency: shouldn't matter.
990 int osd_statfs(const struct lu_env *env, struct dt_device *d,
991 struct obd_statfs *sfs)
993 struct osd_device *osd = osd_dt_dev(d);
994 struct super_block *sb = osd_sb(osd);
995 struct kstatfs *ksfs;
998 if (unlikely(osd->od_mnt == NULL))
1001 /* osd_lproc.c call this without env, allocate ksfs for that case */
1002 if (unlikely(env == NULL)) {
1003 OBD_ALLOC_PTR(ksfs);
1007 ksfs = &osd_oti_get(env)->oti_ksfs;
1010 cfs_spin_lock(&osd->od_osfs_lock);
1011 /* cache 1 second */
1012 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1013 result = sb->s_op->statfs(sb->s_root, ksfs);
1014 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1015 osd->od_osfs_age = cfs_time_current_64();
1016 statfs_pack(&osd->od_statfs, ksfs);
1020 if (likely(result == 0))
1021 *sfs = osd->od_statfs;
1022 cfs_spin_unlock(&osd->od_osfs_lock);
1024 if (unlikely(env == NULL))
1031 * Concurrency: doesn't access mutable data.
1033 static void osd_conf_get(const struct lu_env *env,
1034 const struct dt_device *dev,
1035 struct dt_device_param *param)
1037 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1040 * XXX should be taken from not-yet-existing fs abstraction layer.
1042 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1043 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1044 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1045 param->ddp_block_shift = sb->s_blocksize_bits;
1046 param->ddp_mntopts = 0;
1047 if (test_opt(sb, XATTR_USER))
1048 param->ddp_mntopts |= MNTOPT_USERXATTR;
1049 if (test_opt(sb, POSIX_ACL))
1050 param->ddp_mntopts |= MNTOPT_ACL;
1052 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1053 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1054 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1057 param->ddp_max_ea_size = sb->s_blocksize;
1062 * Helper function to get and fill the buffer with input values.
1064 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1068 buf = &osd_oti_get(env)->oti_buf;
1075 * Concurrency: shouldn't matter.
1077 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1079 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1080 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1084 * Start commit for OSD device.
1086 * An implementation of dt_commit_async method for OSD device.
1087 * Asychronously starts underlayng fs sync and thereby a transaction
1090 * \param env environment
1091 * \param d dt device
1093 * \see dt_device_operations
1095 static int osd_commit_async(const struct lu_env *env,
1096 struct dt_device *d)
1098 struct super_block *s = osd_sb(osd_dt_dev(d));
1101 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1102 RETURN(s->s_op->sync_fs(s, 0));
1106 * Concurrency: shouldn't matter.
1109 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1111 struct super_block *sb = osd_sb(osd_dt_dev(d));
1115 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1117 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1122 * Concurrency: serialization provided by callers.
1124 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1125 int mode, unsigned long timeout, __u32 alg,
1126 struct lustre_capa_key *keys)
1128 struct osd_device *dev = osd_dt_dev(d);
1131 dev->od_fl_capa = mode;
1132 dev->od_capa_timeout = timeout;
1133 dev->od_capa_alg = alg;
1134 dev->od_capa_keys = keys;
1139 * Concurrency: serialization provided by callers.
1141 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1142 struct dt_quota_ctxt *ctxt, void *data)
1144 struct obd_device *obd = (void *)ctxt;
1145 struct vfsmount *mnt = (struct vfsmount *)data;
1148 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1149 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1150 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1151 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1152 obd->obd_lvfs_ctxt.fs = get_ds();
1158 * Note: we do not count into QUOTA here.
1159 * If we mount with --data_journal we may need more.
1161 const int osd_dto_credits_noquota[DTO_NR] = {
1164 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1165 * SINGLEDATA_TRANS_BLOCKS(8)
1166 * XXX Note: maybe iam need more, since iam have more level than
1169 [DTO_INDEX_INSERT] = 16,
1170 [DTO_INDEX_DELETE] = 16,
1174 [DTO_INDEX_UPDATE] = 16,
1176 * Create a object. The same as create object in EXT3.
1177 * DATA_TRANS_BLOCKS(14) +
1178 * INDEX_EXTRA_BLOCKS(8) +
1179 * 3(inode bits, groups, GDT)
1181 [DTO_OBJECT_CREATE] = 25,
1183 * XXX: real credits to be fixed
1185 [DTO_OBJECT_DELETE] = 25,
1187 * Attr set credits (inode)
1189 [DTO_ATTR_SET_BASE] = 1,
1191 * Xattr set. The same as xattr of EXT3.
1192 * DATA_TRANS_BLOCKS(14)
1193 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1194 * are also counted in. Do not know why?
1196 [DTO_XATTR_SET] = 14,
1199 * credits for inode change during write.
1201 [DTO_WRITE_BASE] = 3,
1203 * credits for single block write.
1205 [DTO_WRITE_BLOCK] = 14,
1207 * Attr set credits for chown.
1208 * This is extra credits for setattr, and it is null without quota
1210 [DTO_ATTR_SET_CHOWN]= 0
1213 static const struct dt_device_operations osd_dt_ops = {
1214 .dt_root_get = osd_root_get,
1215 .dt_statfs = osd_statfs,
1216 .dt_trans_create = osd_trans_create,
1217 .dt_trans_start = osd_trans_start,
1218 .dt_trans_stop = osd_trans_stop,
1219 .dt_trans_cb_add = osd_trans_cb_add,
1220 .dt_conf_get = osd_conf_get,
1221 .dt_sync = osd_sync,
1223 .dt_commit_async = osd_commit_async,
1224 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1225 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1228 static void osd_object_read_lock(const struct lu_env *env,
1229 struct dt_object *dt, unsigned role)
1231 struct osd_object *obj = osd_dt_obj(dt);
1232 struct osd_thread_info *oti = osd_oti_get(env);
1234 LINVRNT(osd_invariant(obj));
1236 LASSERT(obj->oo_owner != env);
1237 cfs_down_read_nested(&obj->oo_sem, role);
1239 LASSERT(obj->oo_owner == NULL);
1243 static void osd_object_write_lock(const struct lu_env *env,
1244 struct dt_object *dt, unsigned role)
1246 struct osd_object *obj = osd_dt_obj(dt);
1247 struct osd_thread_info *oti = osd_oti_get(env);
1249 LINVRNT(osd_invariant(obj));
1251 LASSERT(obj->oo_owner != env);
1252 cfs_down_write_nested(&obj->oo_sem, role);
1254 LASSERT(obj->oo_owner == NULL);
1255 obj->oo_owner = env;
1259 static void osd_object_read_unlock(const struct lu_env *env,
1260 struct dt_object *dt)
1262 struct osd_object *obj = osd_dt_obj(dt);
1263 struct osd_thread_info *oti = osd_oti_get(env);
1265 LINVRNT(osd_invariant(obj));
1267 LASSERT(oti->oti_r_locks > 0);
1269 cfs_up_read(&obj->oo_sem);
1272 static void osd_object_write_unlock(const struct lu_env *env,
1273 struct dt_object *dt)
1275 struct osd_object *obj = osd_dt_obj(dt);
1276 struct osd_thread_info *oti = osd_oti_get(env);
1278 LINVRNT(osd_invariant(obj));
1280 LASSERT(obj->oo_owner == env);
1281 LASSERT(oti->oti_w_locks > 0);
1283 obj->oo_owner = NULL;
1284 cfs_up_write(&obj->oo_sem);
1287 static int osd_object_write_locked(const struct lu_env *env,
1288 struct dt_object *dt)
1290 struct osd_object *obj = osd_dt_obj(dt);
1292 LINVRNT(osd_invariant(obj));
1294 return obj->oo_owner == env;
1297 static int capa_is_sane(const struct lu_env *env,
1298 struct osd_device *dev,
1299 struct lustre_capa *capa,
1300 struct lustre_capa_key *keys)
1302 struct osd_thread_info *oti = osd_oti_get(env);
1303 struct lustre_capa *tcapa = &oti->oti_capa;
1304 struct obd_capa *oc;
1308 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1310 if (capa_is_expired(oc)) {
1311 DEBUG_CAPA(D_ERROR, capa, "expired");
1318 if (capa_is_expired_sec(capa)) {
1319 DEBUG_CAPA(D_ERROR, capa, "expired");
1323 cfs_spin_lock(&capa_lock);
1324 for (i = 0; i < 2; i++) {
1325 if (keys[i].lk_keyid == capa->lc_keyid) {
1326 oti->oti_capa_key = keys[i];
1330 cfs_spin_unlock(&capa_lock);
1333 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1337 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1341 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1342 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1346 oc = capa_add(dev->od_capa_hash, capa);
1352 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1353 struct lustre_capa *capa, __u64 opc)
1355 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1356 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1357 struct md_capainfo *ci;
1360 if (!dev->od_fl_capa)
1363 if (capa == BYPASS_CAPA)
1366 ci = md_capainfo(env);
1370 if (ci->mc_auth == LC_ID_NONE)
1374 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1378 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1379 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1384 if (!capa_opc_supported(capa, opc)) {
1385 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1389 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1390 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1397 static struct timespec *osd_inode_time(const struct lu_env *env,
1398 struct inode *inode, __u64 seconds)
1400 struct osd_thread_info *oti = osd_oti_get(env);
1401 struct timespec *t = &oti->oti_time;
1403 t->tv_sec = seconds;
1405 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1410 static void osd_inode_getattr(const struct lu_env *env,
1411 struct inode *inode, struct lu_attr *attr)
1413 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1414 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1415 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1417 attr->la_atime = LTIME_S(inode->i_atime);
1418 attr->la_mtime = LTIME_S(inode->i_mtime);
1419 attr->la_ctime = LTIME_S(inode->i_ctime);
1420 attr->la_mode = inode->i_mode;
1421 attr->la_size = i_size_read(inode);
1422 attr->la_blocks = inode->i_blocks;
1423 attr->la_uid = inode->i_uid;
1424 attr->la_gid = inode->i_gid;
1425 attr->la_flags = LDISKFS_I(inode)->i_flags;
1426 attr->la_nlink = inode->i_nlink;
1427 attr->la_rdev = inode->i_rdev;
1428 attr->la_blksize = 1 << inode->i_blkbits;
1429 attr->la_blkbits = inode->i_blkbits;
1432 static int osd_attr_get(const struct lu_env *env,
1433 struct dt_object *dt,
1434 struct lu_attr *attr,
1435 struct lustre_capa *capa)
1437 struct osd_object *obj = osd_dt_obj(dt);
1439 LASSERT(dt_object_exists(dt));
1440 LINVRNT(osd_invariant(obj));
1442 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1445 cfs_spin_lock(&obj->oo_guard);
1446 osd_inode_getattr(env, obj->oo_inode, attr);
1447 cfs_spin_unlock(&obj->oo_guard);
1451 static int osd_declare_attr_set(const struct lu_env *env,
1452 struct dt_object *dt,
1453 const struct lu_attr *attr,
1454 struct thandle *handle)
1456 struct osd_thandle *oh;
1457 struct osd_object *obj;
1459 LASSERT(dt != NULL);
1460 LASSERT(handle != NULL);
1462 obj = osd_dt_obj(dt);
1463 LASSERT(osd_invariant(obj));
1465 oh = container_of0(handle, struct osd_thandle, ot_super);
1466 LASSERT(oh->ot_handle == NULL);
1468 OSD_DECLARE_OP(oh, attr_set);
1469 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1471 if (attr && attr->la_valid & LA_UID) {
1473 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1475 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1477 if (attr && attr->la_valid & LA_GID) {
1479 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1481 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1487 static int osd_inode_setattr(const struct lu_env *env,
1488 struct inode *inode, const struct lu_attr *attr)
1492 bits = attr->la_valid;
1494 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1496 if (bits & LA_ATIME)
1497 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1498 if (bits & LA_CTIME)
1499 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1500 if (bits & LA_MTIME)
1501 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1502 if (bits & LA_SIZE) {
1503 LDISKFS_I(inode)->i_disksize = attr->la_size;
1504 i_size_write(inode, attr->la_size);
1508 /* OSD should not change "i_blocks" which is used by quota.
1509 * "i_blocks" should be changed by ldiskfs only. */
1510 if (bits & LA_BLOCKS)
1511 inode->i_blocks = attr->la_blocks;
1514 inode->i_mode = (inode->i_mode & S_IFMT) |
1515 (attr->la_mode & ~S_IFMT);
1517 inode->i_uid = attr->la_uid;
1519 inode->i_gid = attr->la_gid;
1520 if (bits & LA_NLINK)
1521 inode->i_nlink = attr->la_nlink;
1523 inode->i_rdev = attr->la_rdev;
1525 if (bits & LA_FLAGS) {
1526 /* always keep S_NOCMTIME */
1527 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1533 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1535 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1536 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1541 if (attr->la_valid & LA_UID)
1542 iattr.ia_valid |= ATTR_UID;
1543 if (attr->la_valid & LA_GID)
1544 iattr.ia_valid |= ATTR_GID;
1545 iattr.ia_uid = attr->la_uid;
1546 iattr.ia_gid = attr->la_gid;
1548 rc = ll_vfs_dq_transfer(inode, &iattr);
1550 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1551 "enforcement enabled on the ldiskfs filesystem?",
1552 inode->i_sb->s_id, rc);
1559 static int osd_attr_set(const struct lu_env *env,
1560 struct dt_object *dt,
1561 const struct lu_attr *attr,
1562 struct thandle *handle,
1563 struct lustre_capa *capa)
1565 struct osd_object *obj = osd_dt_obj(dt);
1566 struct inode *inode;
1569 LASSERT(handle != NULL);
1570 LASSERT(dt_object_exists(dt));
1571 LASSERT(osd_invariant(obj));
1573 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1576 OSD_EXEC_OP(handle, attr_set);
1578 inode = obj->oo_inode;
1579 if (!osd_dt_dev(handle->th_dev)->od_is_md) {
1581 rc = osd_quota_transfer(inode, attr);
1585 #ifdef HAVE_QUOTA_SUPPORT
1586 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1587 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1588 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1593 if (attr->la_valid & LA_UID)
1594 iattr.ia_valid |= ATTR_UID;
1595 if (attr->la_valid & LA_GID)
1596 iattr.ia_valid |= ATTR_GID;
1597 iattr.ia_uid = attr->la_uid;
1598 iattr.ia_gid = attr->la_gid;
1599 osd_push_ctxt(env, save, 1);
1600 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1601 osd_pop_ctxt(save, 1);
1607 cfs_spin_lock(&obj->oo_guard);
1608 rc = osd_inode_setattr(env, inode, attr);
1609 cfs_spin_unlock(&obj->oo_guard);
1612 inode->i_sb->s_op->dirty_inode(inode);
1616 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1617 struct osd_object *obj,
1618 const char *name, const int namelen)
1620 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1623 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1625 struct dt_allocation_hint *hint,
1629 struct osd_device *osd = osd_obj2dev(obj);
1630 struct osd_thandle *oth;
1631 struct dt_object *parent = NULL;
1632 struct inode *inode;
1633 #ifdef HAVE_QUOTA_SUPPORT
1634 struct osd_ctxt *save = &info->oti_ctxt;
1637 LINVRNT(osd_invariant(obj));
1638 LASSERT(obj->oo_inode == NULL);
1639 LASSERT(obj->oo_hl_head == NULL);
1641 if (S_ISDIR(mode) && ldiskfs_pdo) {
1642 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1643 if (obj->oo_hl_head == NULL)
1647 oth = container_of(th, struct osd_thandle, ot_super);
1648 LASSERT(oth->ot_handle->h_transaction != NULL);
1650 if (hint && hint->dah_parent)
1651 parent = hint->dah_parent;
1653 #ifdef HAVE_QUOTA_SUPPORT
1654 osd_push_ctxt(info->oti_env, save, osd_dt_dev(th->th_dev)->od_is_md);
1656 inode = ldiskfs_create_inode(oth->ot_handle,
1657 parent ? osd_dt_obj(parent)->oo_inode :
1658 osd_sb(osd)->s_root->d_inode,
1660 #ifdef HAVE_QUOTA_SUPPORT
1661 osd_pop_ctxt(save, osd_dt_dev(th->th_dev)->od_is_md);
1663 if (!IS_ERR(inode)) {
1664 /* Do not update file c/mtime in ldiskfs.
1665 * NB: don't need any lock because no contention at this
1667 inode->i_flags |= S_NOCMTIME;
1668 inode->i_state |= I_LUSTRE_NOSCRUB;
1669 obj->oo_inode = inode;
1672 if (obj->oo_hl_head != NULL) {
1673 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1674 obj->oo_hl_head = NULL;
1676 result = PTR_ERR(inode);
1678 LINVRNT(osd_invariant(obj));
1686 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1687 struct lu_attr *attr,
1688 struct dt_allocation_hint *hint,
1689 struct dt_object_format *dof,
1693 struct osd_thandle *oth;
1694 struct osd_device *osd = osd_obj2dev(obj);
1695 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1697 LASSERT(S_ISDIR(attr->la_mode));
1699 oth = container_of(th, struct osd_thandle, ot_super);
1700 LASSERT(oth->ot_handle->h_transaction != NULL);
1701 result = osd_mkfile(info, obj, mode, hint, th);
1702 if (result == 0 && osd->od_iop_mode == 0) {
1703 LASSERT(obj->oo_inode != NULL);
1705 * XXX uh-oh... call low-level iam function directly.
1708 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1709 sizeof (struct osd_fid_pack),
1715 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1716 struct lu_attr *attr,
1717 struct dt_allocation_hint *hint,
1718 struct dt_object_format *dof,
1722 struct osd_thandle *oth;
1723 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1725 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1727 LASSERT(S_ISREG(attr->la_mode));
1729 oth = container_of(th, struct osd_thandle, ot_super);
1730 LASSERT(oth->ot_handle->h_transaction != NULL);
1732 result = osd_mkfile(info, obj, mode, hint, th);
1734 LASSERT(obj->oo_inode != NULL);
1735 if (feat->dif_flags & DT_IND_VARKEY)
1736 result = iam_lvar_create(obj->oo_inode,
1737 feat->dif_keysize_max,
1739 feat->dif_recsize_max,
1742 result = iam_lfix_create(obj->oo_inode,
1743 feat->dif_keysize_max,
1745 feat->dif_recsize_max,
1752 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1753 struct lu_attr *attr,
1754 struct dt_allocation_hint *hint,
1755 struct dt_object_format *dof,
1758 LASSERT(S_ISREG(attr->la_mode));
1759 return osd_mkfile(info, obj, (attr->la_mode &
1760 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1763 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1764 struct lu_attr *attr,
1765 struct dt_allocation_hint *hint,
1766 struct dt_object_format *dof,
1769 LASSERT(S_ISLNK(attr->la_mode));
1770 return osd_mkfile(info, obj, (attr->la_mode &
1771 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1774 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1775 struct lu_attr *attr,
1776 struct dt_allocation_hint *hint,
1777 struct dt_object_format *dof,
1780 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1783 LINVRNT(osd_invariant(obj));
1784 LASSERT(obj->oo_inode == NULL);
1785 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1786 S_ISFIFO(mode) || S_ISSOCK(mode));
1788 result = osd_mkfile(info, obj, mode, hint, th);
1790 LASSERT(obj->oo_inode != NULL);
1792 * This inode should be marked dirty for i_rdev. Currently
1793 * that is done in the osd_attr_init().
1795 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1797 LINVRNT(osd_invariant(obj));
1801 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1803 struct dt_allocation_hint *hint,
1804 struct dt_object_format *dof,
1807 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1809 osd_obj_type_f result;
1825 result = osd_mk_index;
1836 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1837 struct dt_object *parent, cfs_umode_t child_mode)
1841 memset(ah, 0, sizeof(*ah));
1842 ah->dah_parent = parent;
1843 ah->dah_mode = child_mode;
1846 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1847 struct lu_attr *attr, struct dt_object_format *dof)
1849 struct inode *inode = obj->oo_inode;
1850 __u64 valid = attr->la_valid;
1853 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1855 if (dof->dof_type != DFT_NODE)
1856 attr->la_valid &= ~LA_RDEV;
1857 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1858 attr->la_valid &= ~LA_ATIME;
1859 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1860 attr->la_valid &= ~LA_CTIME;
1861 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1862 attr->la_valid &= ~LA_MTIME;
1864 if (!osd_obj2dev(obj)->od_is_md) {
1866 result = osd_quota_transfer(inode, attr);
1870 #ifdef HAVE_QUOTA_SUPPORT
1871 attr->la_valid &= ~(LA_UID | LA_GID);
1875 if (attr->la_valid != 0) {
1876 result = osd_inode_setattr(info->oti_env, inode, attr);
1878 * The osd_inode_setattr() should always succeed here. The
1879 * only error that could be returned is EDQUOT when we are
1880 * trying to change the UID or GID of the inode. However, this
1881 * should not happen since quota enforcement is no longer
1882 * enabled on ldiskfs (lquota takes care of it).
1884 LASSERTF(result == 0, "%d", result);
1885 inode->i_sb->s_op->dirty_inode(inode);
1888 attr->la_valid = valid;
1892 * Helper function for osd_object_create()
1894 * \retval 0, on success
1896 static int __osd_object_create(struct osd_thread_info *info,
1897 struct osd_object *obj, struct lu_attr *attr,
1898 struct dt_allocation_hint *hint,
1899 struct dt_object_format *dof,
1905 /* we drop umask so that permissions we pass are not affected */
1906 umask = current->fs->umask;
1907 current->fs->umask = 0;
1909 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1912 osd_attr_init(info, obj, attr, dof);
1913 osd_object_init0(obj);
1915 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1916 unlock_new_inode(obj->oo_inode);
1919 /* restore previous umask value */
1920 current->fs->umask = umask;
1926 * Helper function for osd_object_create()
1928 * \retval 0, on success
1930 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1931 const struct lu_fid *fid, struct thandle *th)
1933 struct osd_thread_info *info = osd_oti_get(env);
1934 struct osd_inode_id *id = &info->oti_id;
1935 struct osd_device *osd = osd_obj2dev(obj);
1937 LASSERT(obj->oo_inode != NULL);
1939 if (osd->od_is_md) {
1940 struct md_ucred *uc = md_ucred(env);
1941 LASSERT(uc != NULL);
1944 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1945 return osd_oi_insert(info, osd, fid, id, th);
1948 static int osd_declare_object_create(const struct lu_env *env,
1949 struct dt_object *dt,
1950 struct lu_attr *attr,
1951 struct dt_allocation_hint *hint,
1952 struct dt_object_format *dof,
1953 struct thandle *handle)
1955 struct osd_thandle *oh;
1957 LASSERT(handle != NULL);
1959 oh = container_of0(handle, struct osd_thandle, ot_super);
1960 LASSERT(oh->ot_handle == NULL);
1962 OSD_DECLARE_OP(oh, create);
1963 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1964 /* XXX: So far, only normal fid needs be inserted into the oi,
1965 * things could be changed later. Revise following code then. */
1966 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1967 OSD_DECLARE_OP(oh, insert);
1968 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1970 /* If this is directory, then we expect . and .. to be inserted as
1971 * well. The one directory block always needs to be created for the
1972 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1973 * block), there is no danger of needing a tree for the first block.
1975 if (attr && S_ISDIR(attr->la_mode)) {
1976 OSD_DECLARE_OP(oh, insert);
1977 OSD_DECLARE_OP(oh, insert);
1978 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1982 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1983 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1988 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1989 struct lu_attr *attr,
1990 struct dt_allocation_hint *hint,
1991 struct dt_object_format *dof,
1994 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1995 struct osd_object *obj = osd_dt_obj(dt);
1996 struct osd_thread_info *info = osd_oti_get(env);
2001 LINVRNT(osd_invariant(obj));
2002 LASSERT(!dt_object_exists(dt));
2003 LASSERT(osd_write_locked(env, obj));
2004 LASSERT(th != NULL);
2006 if (unlikely(fid_is_acct(fid)))
2007 /* Quota files can't be created from the kernel any more,
2008 * 'tune2fs -O quota' will take care of creating them */
2011 OSD_EXEC_OP(th, create);
2013 result = __osd_object_create(info, obj, attr, hint, dof, th);
2015 result = __osd_oi_insert(env, obj, fid, th);
2017 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2018 LASSERT(osd_invariant(obj));
2023 * Called to destroy on-disk representation of the object
2025 * Concurrency: must be locked
2027 static int osd_declare_object_destroy(const struct lu_env *env,
2028 struct dt_object *dt,
2031 struct osd_object *obj = osd_dt_obj(dt);
2032 struct inode *inode = obj->oo_inode;
2033 struct osd_thandle *oh;
2037 oh = container_of0(th, struct osd_thandle, ot_super);
2038 LASSERT(oh->ot_handle == NULL);
2041 OSD_DECLARE_OP(oh, destroy);
2042 OSD_DECLARE_OP(oh, delete);
2043 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2044 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2046 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
2047 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
2052 static int osd_object_destroy(const struct lu_env *env,
2053 struct dt_object *dt,
2056 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2057 struct osd_object *obj = osd_dt_obj(dt);
2058 struct inode *inode = obj->oo_inode;
2059 struct osd_device *osd = osd_obj2dev(obj);
2060 struct osd_thandle *oh;
2064 oh = container_of0(th, struct osd_thandle, ot_super);
2065 LASSERT(oh->ot_handle);
2067 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2069 if (unlikely(fid_is_acct(fid)))
2072 /* Parallel control for OI scrub. For most of cases, there is no
2073 * lock contention. So it will not affect unlink performance. */
2074 cfs_mutex_lock(&inode->i_mutex);
2075 if (S_ISDIR(inode->i_mode)) {
2076 LASSERT(osd_inode_unlinked(inode) ||
2077 inode->i_nlink == 1);
2078 cfs_spin_lock(&obj->oo_guard);
2080 cfs_spin_unlock(&obj->oo_guard);
2081 inode->i_sb->s_op->dirty_inode(inode);
2083 LASSERT(osd_inode_unlinked(inode));
2086 OSD_EXEC_OP(th, destroy);
2088 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2089 cfs_mutex_unlock(&inode->i_mutex);
2091 /* XXX: add to ext3 orphan list */
2092 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2094 /* not needed in the cache anymore */
2095 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2101 * Helper function for osd_xattr_set()
2103 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2104 const struct lu_buf *buf, const char *name, int fl)
2106 struct osd_object *obj = osd_dt_obj(dt);
2107 struct inode *inode = obj->oo_inode;
2108 struct osd_thread_info *info = osd_oti_get(env);
2109 struct dentry *dentry = &info->oti_child_dentry;
2113 LASSERT(dt_object_exists(dt));
2114 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2116 if (fl & LU_XATTR_REPLACE)
2117 fs_flags |= XATTR_REPLACE;
2119 if (fl & LU_XATTR_CREATE)
2120 fs_flags |= XATTR_CREATE;
2122 dentry->d_inode = inode;
2123 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2124 buf->lb_len, fs_flags);
2129 * Put the fid into lustre_mdt_attrs, and then place the structure
2130 * inode's ea. This fid should not be altered during the life time
2133 * \retval +ve, on success
2134 * \retval -ve, on error
2136 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2138 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2139 const struct lu_fid *fid)
2141 struct osd_thread_info *info = osd_oti_get(env);
2142 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2144 lustre_lma_init(mdt_attrs, fid);
2145 lustre_lma_swab(mdt_attrs);
2146 return __osd_xattr_set(env, dt,
2147 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2148 XATTR_NAME_LMA, LU_XATTR_CREATE);
2153 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2154 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2155 * To have compatilibility with 1.8 ldiskfs driver we need to have
2156 * magic number at start of fid data.
2157 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2160 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2161 const struct dt_rec *fid)
2163 param->edp_magic = LDISKFS_LUFID_MAGIC;
2164 param->edp_len = sizeof(struct lu_fid) + 1;
2166 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2167 (struct lu_fid *)fid);
2171 * Try to read the fid from inode ea into dt_rec, if return value
2172 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2174 * \param fid object fid.
2176 * \retval 0 on success
2178 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2179 __u32 ino, struct lu_fid *fid,
2180 struct osd_inode_id *id)
2182 struct osd_thread_info *info = osd_oti_get(env);
2183 struct inode *inode;
2186 osd_id_gen(id, ino, OSD_OII_NOGEN);
2187 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2189 RETURN(PTR_ERR(inode));
2196 * OSD layer object create function for interoperability mode (b11826).
2197 * This is mostly similar to osd_object_create(). Only difference being, fid is
2198 * inserted into inode ea here.
2200 * \retval 0, on success
2201 * \retval -ve, on error
2203 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2204 struct lu_attr *attr,
2205 struct dt_allocation_hint *hint,
2206 struct dt_object_format *dof,
2209 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2210 struct osd_object *obj = osd_dt_obj(dt);
2211 struct osd_thread_info *info = osd_oti_get(env);
2216 LASSERT(osd_invariant(obj));
2217 LASSERT(!dt_object_exists(dt));
2218 LASSERT(osd_write_locked(env, obj));
2219 LASSERT(th != NULL);
2221 if (unlikely(fid_is_acct(fid)))
2222 /* Quota files can't be created from the kernel any more,
2223 * 'tune2fs -O quota' will take care of creating them */
2226 OSD_EXEC_OP(th, create);
2228 result = __osd_object_create(info, obj, attr, hint, dof, th);
2229 /* objects under osd root shld have igif fid, so dont add fid EA */
2230 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2231 result = osd_ea_fid_set(env, dt, fid);
2234 result = __osd_oi_insert(env, obj, fid, th);
2236 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2237 LINVRNT(osd_invariant(obj));
2241 static int osd_declare_object_ref_add(const struct lu_env *env,
2242 struct dt_object *dt,
2243 struct thandle *handle)
2245 struct osd_thandle *oh;
2247 /* it's possible that object doesn't exist yet */
2248 LASSERT(handle != NULL);
2250 oh = container_of0(handle, struct osd_thandle, ot_super);
2251 LASSERT(oh->ot_handle == NULL);
2253 OSD_DECLARE_OP(oh, ref_add);
2254 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2260 * Concurrency: @dt is write locked.
2262 static int osd_object_ref_add(const struct lu_env *env,
2263 struct dt_object *dt, struct thandle *th)
2265 struct osd_object *obj = osd_dt_obj(dt);
2266 struct inode *inode = obj->oo_inode;
2268 LINVRNT(osd_invariant(obj));
2269 LASSERT(dt_object_exists(dt));
2270 LASSERT(osd_write_locked(env, obj));
2271 LASSERT(th != NULL);
2273 OSD_EXEC_OP(th, ref_add);
2276 * DIR_NLINK feature is set for compatibility reasons if:
2277 * 1) nlinks > LDISKFS_LINK_MAX, or
2278 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2280 * It is easier to always set this flag (rather than check and set),
2281 * since it has less overhead, and the superblock will be dirtied
2282 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2283 * do not actually care whether this flag is set or not.
2285 cfs_spin_lock(&obj->oo_guard);
2287 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2288 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2289 inode->i_nlink == 2)
2292 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2293 cfs_spin_unlock(&obj->oo_guard);
2294 inode->i_sb->s_op->dirty_inode(inode);
2295 LINVRNT(osd_invariant(obj));
2300 static int osd_declare_object_ref_del(const struct lu_env *env,
2301 struct dt_object *dt,
2302 struct thandle *handle)
2304 struct osd_thandle *oh;
2306 LASSERT(dt_object_exists(dt));
2307 LASSERT(handle != NULL);
2309 oh = container_of0(handle, struct osd_thandle, ot_super);
2310 LASSERT(oh->ot_handle == NULL);
2312 OSD_DECLARE_OP(oh, ref_del);
2313 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2319 * Concurrency: @dt is write locked.
2321 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2324 struct osd_object *obj = osd_dt_obj(dt);
2325 struct inode *inode = obj->oo_inode;
2327 LINVRNT(osd_invariant(obj));
2328 LASSERT(dt_object_exists(dt));
2329 LASSERT(osd_write_locked(env, obj));
2330 LASSERT(th != NULL);
2332 OSD_EXEC_OP(th, ref_del);
2334 cfs_spin_lock(&obj->oo_guard);
2335 LASSERT(inode->i_nlink > 0);
2337 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2338 * then the nlink count is 1. Don't let it be set to 0 or the directory
2339 * inode will be deleted incorrectly. */
2340 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2342 cfs_spin_unlock(&obj->oo_guard);
2343 inode->i_sb->s_op->dirty_inode(inode);
2344 LINVRNT(osd_invariant(obj));
2350 * Get the 64-bit version for an inode.
2352 static int osd_object_version_get(const struct lu_env *env,
2353 struct dt_object *dt, dt_obj_version_t *ver)
2355 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2357 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2358 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2359 *ver = LDISKFS_I(inode)->i_fs_version;
2364 * Concurrency: @dt is read locked.
2366 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2367 struct lu_buf *buf, const char *name,
2368 struct lustre_capa *capa)
2370 struct osd_object *obj = osd_dt_obj(dt);
2371 struct inode *inode = obj->oo_inode;
2372 struct osd_thread_info *info = osd_oti_get(env);
2373 struct dentry *dentry = &info->oti_obj_dentry;
2375 /* version get is not real XATTR but uses xattr API */
2376 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2377 /* for version we are just using xattr API but change inode
2379 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2380 osd_object_version_get(env, dt, buf->lb_buf);
2381 return sizeof(dt_obj_version_t);
2384 LASSERT(dt_object_exists(dt));
2385 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2386 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2388 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2391 dentry->d_inode = inode;
2392 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2396 static int osd_declare_xattr_set(const struct lu_env *env,
2397 struct dt_object *dt,
2398 const struct lu_buf *buf, const char *name,
2399 int fl, struct thandle *handle)
2401 struct osd_thandle *oh;
2403 LASSERT(handle != NULL);
2405 oh = container_of0(handle, struct osd_thandle, ot_super);
2406 LASSERT(oh->ot_handle == NULL);
2408 OSD_DECLARE_OP(oh, xattr_set);
2409 if (strcmp(name, XATTR_NAME_VERSION) == 0)
2410 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2412 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2418 * Set the 64-bit version for object
2420 static void osd_object_version_set(const struct lu_env *env,
2421 struct dt_object *dt,
2422 dt_obj_version_t *new_version)
2424 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2426 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2427 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2429 LDISKFS_I(inode)->i_fs_version = *new_version;
2430 /** Version is set after all inode operations are finished,
2431 * so we should mark it dirty here */
2432 inode->i_sb->s_op->dirty_inode(inode);
2436 * Concurrency: @dt is write locked.
2438 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2439 const struct lu_buf *buf, const char *name, int fl,
2440 struct thandle *handle, struct lustre_capa *capa)
2442 LASSERT(handle != NULL);
2444 /* version set is not real XATTR */
2445 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2446 /* for version we are just using xattr API but change inode
2448 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2449 osd_object_version_set(env, dt, buf->lb_buf);
2450 return sizeof(dt_obj_version_t);
2453 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2456 OSD_EXEC_OP(handle, xattr_set);
2457 return __osd_xattr_set(env, dt, buf, name, fl);
2461 * Concurrency: @dt is read locked.
2463 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2464 struct lu_buf *buf, struct lustre_capa *capa)
2466 struct osd_object *obj = osd_dt_obj(dt);
2467 struct inode *inode = obj->oo_inode;
2468 struct osd_thread_info *info = osd_oti_get(env);
2469 struct dentry *dentry = &info->oti_obj_dentry;
2471 LASSERT(dt_object_exists(dt));
2472 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2473 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2475 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2478 dentry->d_inode = inode;
2479 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2482 static int osd_declare_xattr_del(const struct lu_env *env,
2483 struct dt_object *dt, const char *name,
2484 struct thandle *handle)
2486 struct osd_thandle *oh;
2488 LASSERT(dt_object_exists(dt));
2489 LASSERT(handle != NULL);
2491 oh = container_of0(handle, struct osd_thandle, ot_super);
2492 LASSERT(oh->ot_handle == NULL);
2494 OSD_DECLARE_OP(oh, xattr_set);
2495 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2501 * Concurrency: @dt is write locked.
2503 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2504 const char *name, struct thandle *handle,
2505 struct lustre_capa *capa)
2507 struct osd_object *obj = osd_dt_obj(dt);
2508 struct inode *inode = obj->oo_inode;
2509 struct osd_thread_info *info = osd_oti_get(env);
2510 struct dentry *dentry = &info->oti_obj_dentry;
2513 LASSERT(dt_object_exists(dt));
2514 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2515 LASSERT(osd_write_locked(env, obj));
2516 LASSERT(handle != NULL);
2518 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2521 OSD_EXEC_OP(handle, xattr_set);
2523 dentry->d_inode = inode;
2524 rc = inode->i_op->removexattr(dentry, name);
2528 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2529 struct dt_object *dt,
2530 struct lustre_capa *old,
2533 struct osd_thread_info *info = osd_oti_get(env);
2534 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2535 struct osd_object *obj = osd_dt_obj(dt);
2536 struct osd_device *dev = osd_obj2dev(obj);
2537 struct lustre_capa_key *key = &info->oti_capa_key;
2538 struct lustre_capa *capa = &info->oti_capa;
2539 struct obd_capa *oc;
2540 struct md_capainfo *ci;
2544 if (!dev->od_fl_capa)
2545 RETURN(ERR_PTR(-ENOENT));
2547 LASSERT(dt_object_exists(dt));
2548 LINVRNT(osd_invariant(obj));
2550 /* renewal sanity check */
2551 if (old && osd_object_auth(env, dt, old, opc))
2552 RETURN(ERR_PTR(-EACCES));
2554 ci = md_capainfo(env);
2556 RETURN(ERR_PTR(-ENOENT));
2558 switch (ci->mc_auth) {
2562 capa->lc_uid = obj->oo_inode->i_uid;
2563 capa->lc_gid = obj->oo_inode->i_gid;
2564 capa->lc_flags = LC_ID_PLAIN;
2566 case LC_ID_CONVERT: {
2569 s[0] = obj->oo_inode->i_uid;
2570 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2571 s[2] = obj->oo_inode->i_gid;
2572 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2573 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2575 RETURN(ERR_PTR(rc));
2577 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2578 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2579 capa->lc_flags = LC_ID_CONVERT;
2583 RETURN(ERR_PTR(-EINVAL));
2586 capa->lc_fid = *fid;
2588 capa->lc_flags |= dev->od_capa_alg << 24;
2589 capa->lc_timeout = dev->od_capa_timeout;
2590 capa->lc_expiry = 0;
2592 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2594 LASSERT(!capa_is_expired(oc));
2598 cfs_spin_lock(&capa_lock);
2599 *key = dev->od_capa_keys[1];
2600 cfs_spin_unlock(&capa_lock);
2602 capa->lc_keyid = key->lk_keyid;
2603 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2605 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2607 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2608 RETURN(ERR_PTR(rc));
2611 oc = capa_add(dev->od_capa_hash, capa);
2615 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2617 struct osd_object *obj = osd_dt_obj(dt);
2618 struct inode *inode = obj->oo_inode;
2619 struct osd_thread_info *info = osd_oti_get(env);
2620 struct dentry *dentry = &info->oti_obj_dentry;
2621 struct file *file = &info->oti_file;
2626 dentry->d_inode = inode;
2627 file->f_dentry = dentry;
2628 file->f_mapping = inode->i_mapping;
2629 file->f_op = inode->i_fop;
2630 mutex_lock(&inode->i_mutex);
2631 rc = file->f_op->fsync(file, dentry, 0);
2632 mutex_unlock(&inode->i_mutex);
2636 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2639 struct osd_object *obj = osd_dt_obj(dt);
2642 *data = (void *)obj->oo_inode;
2650 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2651 const struct dt_index_features *feat)
2653 struct iam_descr *descr;
2655 if (osd_object_is_root(o))
2656 return feat == &dt_directory_features;
2658 LASSERT(o->oo_dir != NULL);
2660 descr = o->oo_dir->od_container.ic_descr;
2661 if (feat == &dt_directory_features) {
2662 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2668 feat->dif_keysize_min <= descr->id_key_size &&
2669 descr->id_key_size <= feat->dif_keysize_max &&
2670 feat->dif_recsize_min <= descr->id_rec_size &&
2671 descr->id_rec_size <= feat->dif_recsize_max &&
2672 !(feat->dif_flags & (DT_IND_VARKEY |
2673 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2674 ergo(feat->dif_flags & DT_IND_UPDATE,
2675 1 /* XXX check that object (and file system) is
2680 static int osd_iam_container_init(const struct lu_env *env,
2681 struct osd_object *obj,
2682 struct osd_directory *dir)
2684 struct iam_container *bag = &dir->od_container;
2687 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2691 result = iam_container_setup(bag);
2693 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2695 iam_container_fini(bag);
2702 * Concurrency: no external locking is necessary.
2704 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2705 const struct dt_index_features *feat)
2709 struct osd_object *obj = osd_dt_obj(dt);
2710 struct osd_device *osd = osd_obj2dev(obj);
2712 LINVRNT(osd_invariant(obj));
2713 LASSERT(dt_object_exists(dt));
2715 if (osd_object_is_root(obj)) {
2716 dt->do_index_ops = &osd_index_ea_ops;
2718 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2719 dt->do_index_ops = &osd_index_ea_ops;
2720 if (S_ISDIR(obj->oo_inode->i_mode))
2725 } else if (unlikely(feat == &dt_otable_features)) {
2726 dt->do_index_ops = &osd_otable_ops;
2728 } else if (feat == &dt_acct_features) {
2729 dt->do_index_ops = &osd_acct_index_ops;
2732 } else if (!osd_has_index(obj)) {
2733 struct osd_directory *dir;
2738 cfs_spin_lock(&obj->oo_guard);
2739 if (obj->oo_dir == NULL)
2743 * Concurrent thread allocated container data.
2746 cfs_spin_unlock(&obj->oo_guard);
2748 * Now, that we have container data, serialize its
2751 cfs_down_write(&obj->oo_ext_idx_sem);
2753 * recheck under lock.
2755 if (!osd_has_index(obj))
2756 result = osd_iam_container_init(env, obj, dir);
2759 cfs_up_write(&obj->oo_ext_idx_sem);
2767 if (result == 0 && skip_iam == 0) {
2768 if (!osd_iam_index_probe(env, obj, feat))
2771 LINVRNT(osd_invariant(obj));
2776 static int osd_otable_it_attr_get(const struct lu_env *env,
2777 struct dt_object *dt,
2778 struct lu_attr *attr,
2779 struct lustre_capa *capa)
2785 static const struct dt_object_operations osd_obj_ops = {
2786 .do_read_lock = osd_object_read_lock,
2787 .do_write_lock = osd_object_write_lock,
2788 .do_read_unlock = osd_object_read_unlock,
2789 .do_write_unlock = osd_object_write_unlock,
2790 .do_write_locked = osd_object_write_locked,
2791 .do_attr_get = osd_attr_get,
2792 .do_declare_attr_set = osd_declare_attr_set,
2793 .do_attr_set = osd_attr_set,
2794 .do_ah_init = osd_ah_init,
2795 .do_declare_create = osd_declare_object_create,
2796 .do_create = osd_object_create,
2797 .do_declare_destroy = osd_declare_object_destroy,
2798 .do_destroy = osd_object_destroy,
2799 .do_index_try = osd_index_try,
2800 .do_declare_ref_add = osd_declare_object_ref_add,
2801 .do_ref_add = osd_object_ref_add,
2802 .do_declare_ref_del = osd_declare_object_ref_del,
2803 .do_ref_del = osd_object_ref_del,
2804 .do_xattr_get = osd_xattr_get,
2805 .do_declare_xattr_set = osd_declare_xattr_set,
2806 .do_xattr_set = osd_xattr_set,
2807 .do_declare_xattr_del = osd_declare_xattr_del,
2808 .do_xattr_del = osd_xattr_del,
2809 .do_xattr_list = osd_xattr_list,
2810 .do_capa_get = osd_capa_get,
2811 .do_object_sync = osd_object_sync,
2812 .do_data_get = osd_data_get,
2816 * dt_object_operations for interoperability mode
2817 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2819 static const struct dt_object_operations osd_obj_ea_ops = {
2820 .do_read_lock = osd_object_read_lock,
2821 .do_write_lock = osd_object_write_lock,
2822 .do_read_unlock = osd_object_read_unlock,
2823 .do_write_unlock = osd_object_write_unlock,
2824 .do_write_locked = osd_object_write_locked,
2825 .do_attr_get = osd_attr_get,
2826 .do_declare_attr_set = osd_declare_attr_set,
2827 .do_attr_set = osd_attr_set,
2828 .do_ah_init = osd_ah_init,
2829 .do_declare_create = osd_declare_object_create,
2830 .do_create = osd_object_ea_create,
2831 .do_declare_destroy = osd_declare_object_destroy,
2832 .do_destroy = osd_object_destroy,
2833 .do_index_try = osd_index_try,
2834 .do_declare_ref_add = osd_declare_object_ref_add,
2835 .do_ref_add = osd_object_ref_add,
2836 .do_declare_ref_del = osd_declare_object_ref_del,
2837 .do_ref_del = osd_object_ref_del,
2838 .do_xattr_get = osd_xattr_get,
2839 .do_declare_xattr_set = osd_declare_xattr_set,
2840 .do_xattr_set = osd_xattr_set,
2841 .do_declare_xattr_del = osd_declare_xattr_del,
2842 .do_xattr_del = osd_xattr_del,
2843 .do_xattr_list = osd_xattr_list,
2844 .do_capa_get = osd_capa_get,
2845 .do_object_sync = osd_object_sync,
2846 .do_data_get = osd_data_get,
2849 static const struct dt_object_operations osd_obj_otable_it_ops = {
2850 .do_attr_get = osd_otable_it_attr_get,
2851 .do_index_try = osd_index_try,
2854 static int osd_index_declare_iam_delete(const struct lu_env *env,
2855 struct dt_object *dt,
2856 const struct dt_key *key,
2857 struct thandle *handle)
2859 struct osd_thandle *oh;
2861 oh = container_of0(handle, struct osd_thandle, ot_super);
2862 LASSERT(oh->ot_handle == NULL);
2864 OSD_DECLARE_OP(oh, delete);
2865 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2871 * delete a (key, value) pair from index \a dt specified by \a key
2873 * \param dt osd index object
2874 * \param key key for index
2875 * \param rec record reference
2876 * \param handle transaction handler
2879 * \retval -ve failure
2882 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2883 const struct dt_key *key,
2884 struct thandle *handle,
2885 struct lustre_capa *capa)
2887 struct osd_object *obj = osd_dt_obj(dt);
2888 struct osd_thandle *oh;
2889 struct iam_path_descr *ipd;
2890 struct iam_container *bag = &obj->oo_dir->od_container;
2895 LINVRNT(osd_invariant(obj));
2896 LASSERT(dt_object_exists(dt));
2897 LASSERT(bag->ic_object == obj->oo_inode);
2898 LASSERT(handle != NULL);
2900 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2903 OSD_EXEC_OP(handle, delete);
2905 ipd = osd_idx_ipd_get(env, bag);
2906 if (unlikely(ipd == NULL))
2909 oh = container_of0(handle, struct osd_thandle, ot_super);
2910 LASSERT(oh->ot_handle != NULL);
2911 LASSERT(oh->ot_handle->h_transaction != NULL);
2913 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2914 osd_ipd_put(env, bag, ipd);
2915 LINVRNT(osd_invariant(obj));
2919 static int osd_index_declare_ea_delete(const struct lu_env *env,
2920 struct dt_object *dt,
2921 const struct dt_key *key,
2922 struct thandle *handle)
2924 struct osd_thandle *oh;
2926 LASSERT(dt_object_exists(dt));
2927 LASSERT(handle != NULL);
2929 oh = container_of0(handle, struct osd_thandle, ot_super);
2930 LASSERT(oh->ot_handle == NULL);
2932 OSD_DECLARE_OP(oh, delete);
2933 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2935 LASSERT(osd_dt_obj(dt)->oo_inode);
2936 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2937 osd_dt_obj(dt)->oo_inode);
2938 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2939 osd_dt_obj(dt)->oo_inode);
2944 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2947 struct osd_fid_pack *rec;
2950 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2951 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2952 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2958 * Index delete function for interoperability mode (b11826).
2959 * It will remove the directory entry added by osd_index_ea_insert().
2960 * This entry is needed to maintain name->fid mapping.
2962 * \param key, key i.e. file entry to be deleted
2964 * \retval 0, on success
2965 * \retval -ve, on error
2967 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2968 const struct dt_key *key,
2969 struct thandle *handle,
2970 struct lustre_capa *capa)
2972 struct osd_object *obj = osd_dt_obj(dt);
2973 struct inode *dir = obj->oo_inode;
2974 struct dentry *dentry;
2975 struct osd_thandle *oh;
2976 struct ldiskfs_dir_entry_2 *de;
2977 struct buffer_head *bh;
2978 struct htree_lock *hlock = NULL;
2983 LINVRNT(osd_invariant(obj));
2984 LASSERT(dt_object_exists(dt));
2985 LASSERT(handle != NULL);
2987 OSD_EXEC_OP(handle, delete);
2989 oh = container_of(handle, struct osd_thandle, ot_super);
2990 LASSERT(oh->ot_handle != NULL);
2991 LASSERT(oh->ot_handle->h_transaction != NULL);
2993 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2996 dentry = osd_child_dentry_get(env, obj,
2997 (char *)key, strlen((char *)key));
2999 if (obj->oo_hl_head != NULL) {
3000 hlock = osd_oti_get(env)->oti_hlock;
3001 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3002 dir, LDISKFS_HLOCK_DEL);
3004 cfs_down_write(&obj->oo_ext_idx_sem);
3007 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3009 rc = ldiskfs_delete_entry(oh->ot_handle,
3016 ldiskfs_htree_unlock(hlock);
3018 cfs_up_write(&obj->oo_ext_idx_sem);
3020 LASSERT(osd_invariant(obj));
3025 * Lookup index for \a key and copy record to \a rec.
3027 * \param dt osd index object
3028 * \param key key for index
3029 * \param rec record reference
3031 * \retval +ve success : exact mach
3032 * \retval 0 return record with key not greater than \a key
3033 * \retval -ve failure
3035 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3036 struct dt_rec *rec, const struct dt_key *key,
3037 struct lustre_capa *capa)
3039 struct osd_object *obj = osd_dt_obj(dt);
3040 struct iam_path_descr *ipd;
3041 struct iam_container *bag = &obj->oo_dir->od_container;
3042 struct osd_thread_info *oti = osd_oti_get(env);
3043 struct iam_iterator *it = &oti->oti_idx_it;
3044 struct iam_rec *iam_rec;
3049 LASSERT(osd_invariant(obj));
3050 LASSERT(dt_object_exists(dt));
3051 LASSERT(bag->ic_object == obj->oo_inode);
3053 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3056 ipd = osd_idx_ipd_get(env, bag);
3060 /* got ipd now we can start iterator. */
3061 iam_it_init(it, bag, 0, ipd);
3063 rc = iam_it_get(it, (struct iam_key *)key);
3065 if (S_ISDIR(obj->oo_inode->i_mode))
3066 iam_rec = (struct iam_rec *)oti->oti_ldp;
3068 iam_rec = (struct iam_rec *) rec;
3070 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3071 if (S_ISDIR(obj->oo_inode->i_mode))
3072 osd_fid_unpack((struct lu_fid *) rec,
3073 (struct osd_fid_pack *)iam_rec);
3077 osd_ipd_put(env, bag, ipd);
3079 LINVRNT(osd_invariant(obj));
3084 static int osd_index_declare_iam_insert(const struct lu_env *env,
3085 struct dt_object *dt,
3086 const struct dt_rec *rec,
3087 const struct dt_key *key,
3088 struct thandle *handle)
3090 struct osd_thandle *oh;
3092 LASSERT(dt_object_exists(dt));
3093 LASSERT(handle != NULL);
3095 oh = container_of0(handle, struct osd_thandle, ot_super);
3096 LASSERT(oh->ot_handle == NULL);
3098 OSD_DECLARE_OP(oh, insert);
3099 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3105 * Inserts (key, value) pair in \a dt index object.
3107 * \param dt osd index object
3108 * \param key key for index
3109 * \param rec record reference
3110 * \param th transaction handler
3113 * \retval -ve failure
3115 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3116 const struct dt_rec *rec,
3117 const struct dt_key *key, struct thandle *th,
3118 struct lustre_capa *capa, int ignore_quota)
3120 struct osd_object *obj = osd_dt_obj(dt);
3121 struct iam_path_descr *ipd;
3122 struct osd_thandle *oh;
3123 struct iam_container *bag = &obj->oo_dir->od_container;
3124 #ifdef HAVE_QUOTA_SUPPORT
3125 cfs_cap_t save = cfs_curproc_cap_pack();
3127 struct osd_thread_info *oti = osd_oti_get(env);
3128 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3133 LINVRNT(osd_invariant(obj));
3134 LASSERT(dt_object_exists(dt));
3135 LASSERT(bag->ic_object == obj->oo_inode);
3136 LASSERT(th != NULL);
3138 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3141 OSD_EXEC_OP(th, insert);
3143 ipd = osd_idx_ipd_get(env, bag);
3144 if (unlikely(ipd == NULL))
3147 oh = container_of0(th, struct osd_thandle, ot_super);
3148 LASSERT(oh->ot_handle != NULL);
3149 LASSERT(oh->ot_handle->h_transaction != NULL);
3150 #ifdef HAVE_QUOTA_SUPPORT
3152 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3154 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3156 if (S_ISDIR(obj->oo_inode->i_mode))
3157 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3159 iam_rec = (struct iam_rec *) rec;
3160 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3162 #ifdef HAVE_QUOTA_SUPPORT
3163 cfs_curproc_cap_unpack(save);
3165 osd_ipd_put(env, bag, ipd);
3166 LINVRNT(osd_invariant(obj));
3171 * Calls ldiskfs_add_entry() to add directory entry
3172 * into the directory. This is required for
3173 * interoperability mode (b11826)
3175 * \retval 0, on success
3176 * \retval -ve, on error
3178 static int __osd_ea_add_rec(struct osd_thread_info *info,
3179 struct osd_object *pobj, struct inode *cinode,
3180 const char *name, const struct dt_rec *fid,
3181 struct htree_lock *hlock, struct thandle *th)
3183 struct ldiskfs_dentry_param *ldp;
3184 struct dentry *child;
3185 struct osd_thandle *oth;
3188 oth = container_of(th, struct osd_thandle, ot_super);
3189 LASSERT(oth->ot_handle != NULL);
3190 LASSERT(oth->ot_handle->h_transaction != NULL);
3192 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3194 /* XXX: remove fid_is_igif() check here.
3195 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3196 * it is IGIF now but needs FID in dir entry as well for readdir
3198 * LU-838 should fix that and remove fid_is_igif() check */
3199 if (fid_is_igif((struct lu_fid *)fid) ||
3200 fid_is_norm((struct lu_fid *)fid)) {
3201 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3202 osd_get_ldiskfs_dirent_param(ldp, fid);
3203 child->d_fsdata = (void *)ldp;
3205 child->d_fsdata = NULL;
3207 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3213 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3214 * into the directory.Also sets flags into osd object to
3215 * indicate dot and dotdot are created. This is required for
3216 * interoperability mode (b11826)
3218 * \param dir directory for dot and dotdot fixup.
3219 * \param obj child object for linking
3221 * \retval 0, on success
3222 * \retval -ve, on error
3224 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3225 struct osd_object *dir,
3226 struct inode *parent_dir, const char *name,
3227 const struct dt_rec *dot_fid,
3228 const struct dt_rec *dot_dot_fid,
3231 struct inode *inode = dir->oo_inode;
3232 struct ldiskfs_dentry_param *dot_ldp;
3233 struct ldiskfs_dentry_param *dot_dot_ldp;
3234 struct osd_thandle *oth;
3237 oth = container_of(th, struct osd_thandle, ot_super);
3238 LASSERT(oth->ot_handle->h_transaction != NULL);
3239 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3241 if (strcmp(name, dot) == 0) {
3242 if (dir->oo_compat_dot_created) {
3245 LASSERT(inode == parent_dir);
3246 dir->oo_compat_dot_created = 1;
3249 } else if(strcmp(name, dotdot) == 0) {
3250 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3251 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3253 if (!dir->oo_compat_dot_created)
3255 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3256 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3257 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3262 /* in case of rename, dotdot is already created */
3263 if (dir->oo_compat_dotdot_created) {
3264 return __osd_ea_add_rec(info, dir, parent_dir, name,
3265 dot_dot_fid, NULL, th);
3268 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3269 inode, dot_ldp, dot_dot_ldp);
3271 dir->oo_compat_dotdot_created = 1;
3279 * It will call the appropriate osd_add* function and return the
3280 * value, return by respective functions.
3282 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3283 struct inode *cinode, const char *name,
3284 const struct dt_rec *fid, struct thandle *th)
3286 struct osd_thread_info *info = osd_oti_get(env);
3287 struct htree_lock *hlock;
3290 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3292 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3294 if (hlock != NULL) {
3295 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3298 cfs_down_write(&pobj->oo_ext_idx_sem);
3300 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3301 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3304 if (hlock != NULL) {
3305 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3306 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3308 cfs_down_write(&pobj->oo_ext_idx_sem);
3311 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3315 ldiskfs_htree_unlock(hlock);
3317 cfs_up_write(&pobj->oo_ext_idx_sem);
3323 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3324 struct osd_idmap_cache *oic)
3326 struct osd_scrub *scrub = &dev->od_scrub;
3327 struct lu_fid *fid = &oic->oic_fid;
3328 struct osd_inode_id *id = &oti->oti_id;
3334 rc = osd_oi_lookup(oti, dev, fid, id);
3335 if (rc != 0 && rc != -ENOENT)
3338 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3341 if (thread_is_running(&scrub->os_thread)) {
3342 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3343 /* There is race condition between osd_oi_lookup and OI scrub.
3344 * The OI scrub finished just after osd_oi_lookup() failure.
3345 * Under such case, it is unnecessary to trigger OI scrub again,
3346 * but try to call osd_oi_lookup() again. */
3347 if (unlikely(rc == -EAGAIN))
3353 if (!scrub->os_no_scrub && ++once == 1) {
3354 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3356 rc = osd_scrub_start(dev);
3357 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3359 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3365 RETURN(rc = -EREMCHG);
3369 * Calls ->lookup() to find dentry. From dentry get inode and
3370 * read inode's ea to get fid. This is required for interoperability
3373 * \retval 0, on success
3374 * \retval -ve, on error
3376 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3377 struct dt_rec *rec, const struct dt_key *key)
3379 struct inode *dir = obj->oo_inode;
3380 struct dentry *dentry;
3381 struct ldiskfs_dir_entry_2 *de;
3382 struct buffer_head *bh;
3383 struct lu_fid *fid = (struct lu_fid *) rec;
3384 struct htree_lock *hlock = NULL;
3388 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3390 dentry = osd_child_dentry_get(env, obj,
3391 (char *)key, strlen((char *)key));
3393 if (obj->oo_hl_head != NULL) {
3394 hlock = osd_oti_get(env)->oti_hlock;
3395 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3396 dir, LDISKFS_HLOCK_LOOKUP);
3398 cfs_down_read(&obj->oo_ext_idx_sem);
3401 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3403 struct osd_thread_info *oti = osd_oti_get(env);
3404 struct osd_idmap_cache *oic = &oti->oti_cache;
3405 struct osd_device *dev = osd_obj2dev(obj);
3406 struct osd_scrub *scrub = &dev->od_scrub;
3407 struct scrub_file *sf = &scrub->os_file;
3409 ino = le32_to_cpu(de->inode);
3410 rc = osd_get_fid_from_dentry(de, rec);
3412 /* done with de, release bh */
3415 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3417 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3418 if (rc != 0 || !fid_is_norm(fid)) {
3419 fid_zero(&oic->oic_fid);
3423 oic->oic_fid = *fid;
3424 if ((scrub->os_pos_current <= ino) &&
3425 (sf->sf_flags & SF_INCONSISTENT ||
3426 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3428 rc = osd_consistency_check(oti, dev, oic);
3437 ldiskfs_htree_unlock(hlock);
3439 cfs_up_read(&obj->oo_ext_idx_sem);
3444 * Find the osd object for given fid.
3446 * \param fid need to find the osd object having this fid
3448 * \retval osd_object on success
3449 * \retval -ve on error
3451 struct osd_object *osd_object_find(const struct lu_env *env,
3452 struct dt_object *dt,
3453 const struct lu_fid *fid)
3455 struct lu_device *ludev = dt->do_lu.lo_dev;
3456 struct osd_object *child = NULL;
3457 struct lu_object *luch;
3458 struct lu_object *lo;
3460 luch = lu_object_find(env, ludev, fid, NULL);
3461 if (!IS_ERR(luch)) {
3462 if (lu_object_exists(luch)) {
3463 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3465 child = osd_obj(lo);
3467 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3468 "lu_object can't be located"
3469 DFID"\n", PFID(fid));
3471 if (child == NULL) {
3472 lu_object_put(env, luch);
3473 CERROR("Unable to get osd_object\n");
3474 child = ERR_PTR(-ENOENT);
3477 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3478 "lu_object does not exists "DFID"\n",
3480 lu_object_put(env, luch);
3481 child = ERR_PTR(-ENOENT);
3484 child = (void *)luch;
3490 * Put the osd object once done with it.
3492 * \param obj osd object that needs to be put
3494 static inline void osd_object_put(const struct lu_env *env,
3495 struct osd_object *obj)
3497 lu_object_put(env, &obj->oo_dt.do_lu);
3500 static int osd_index_declare_ea_insert(const struct lu_env *env,
3501 struct dt_object *dt,
3502 const struct dt_rec *rec,
3503 const struct dt_key *key,
3504 struct thandle *handle)
3506 struct osd_thandle *oh;
3508 LASSERT(dt_object_exists(dt));
3509 LASSERT(handle != NULL);
3511 oh = container_of0(handle, struct osd_thandle, ot_super);
3512 LASSERT(oh->ot_handle == NULL);
3514 OSD_DECLARE_OP(oh, insert);
3515 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3517 LASSERT(osd_dt_obj(dt)->oo_inode);
3518 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3519 osd_dt_obj(dt)->oo_inode);
3520 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3521 osd_dt_obj(dt)->oo_inode);
3527 * Index add function for interoperability mode (b11826).
3528 * It will add the directory entry.This entry is needed to
3529 * maintain name->fid mapping.
3531 * \param key it is key i.e. file entry to be inserted
3532 * \param rec it is value of given key i.e. fid
3534 * \retval 0, on success
3535 * \retval -ve, on error
3537 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3538 const struct dt_rec *rec,
3539 const struct dt_key *key, struct thandle *th,
3540 struct lustre_capa *capa, int ignore_quota)
3542 struct osd_object *obj = osd_dt_obj(dt);
3543 struct lu_fid *fid = (struct lu_fid *) rec;
3544 const char *name = (const char *)key;
3545 struct osd_object *child;
3546 #ifdef HAVE_QUOTA_SUPPORT
3547 cfs_cap_t save = cfs_curproc_cap_pack();
3553 LASSERT(osd_invariant(obj));
3554 LASSERT(dt_object_exists(dt));
3555 LASSERT(th != NULL);
3557 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3560 child = osd_object_find(env, dt, fid);
3561 if (!IS_ERR(child)) {
3562 #ifdef HAVE_QUOTA_SUPPORT
3564 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3566 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3568 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3569 #ifdef HAVE_QUOTA_SUPPORT
3570 cfs_curproc_cap_unpack(save);
3572 osd_object_put(env, child);
3574 rc = PTR_ERR(child);
3577 LASSERT(osd_invariant(obj));
3582 * Initialize osd Iterator for given osd index object.
3584 * \param dt osd index object
3587 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3588 struct dt_object *dt,
3590 struct lustre_capa *capa)
3592 struct osd_it_iam *it;
3593 struct osd_thread_info *oti = osd_oti_get(env);
3594 struct osd_object *obj = osd_dt_obj(dt);
3595 struct lu_object *lo = &dt->do_lu;
3596 struct iam_path_descr *ipd;
3597 struct iam_container *bag = &obj->oo_dir->od_container;
3599 LASSERT(lu_object_exists(lo));
3601 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3602 return ERR_PTR(-EACCES);
3605 ipd = osd_it_ipd_get(env, bag);
3606 if (likely(ipd != NULL)) {
3610 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3611 return (struct dt_it *)it;
3613 return ERR_PTR(-ENOMEM);
3617 * free given Iterator.
3620 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3622 struct osd_it_iam *it = (struct osd_it_iam *)di;
3623 struct osd_object *obj = it->oi_obj;
3625 iam_it_fini(&it->oi_it);
3626 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3627 lu_object_put(env, &obj->oo_dt.do_lu);
3631 * Move Iterator to record specified by \a key
3633 * \param di osd iterator
3634 * \param key key for index
3636 * \retval +ve di points to record with least key not larger than key
3637 * \retval 0 di points to exact matched key
3638 * \retval -ve failure
3641 static int osd_it_iam_get(const struct lu_env *env,
3642 struct dt_it *di, const struct dt_key *key)
3644 struct osd_it_iam *it = (struct osd_it_iam *)di;
3646 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3652 * \param di osd iterator
3655 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3657 struct osd_it_iam *it = (struct osd_it_iam *)di;
3659 iam_it_put(&it->oi_it);
3663 * Move iterator by one record
3665 * \param di osd iterator
3667 * \retval +1 end of container reached
3669 * \retval -ve failure
3672 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3674 struct osd_it_iam *it = (struct osd_it_iam *)di;
3676 return iam_it_next(&it->oi_it);
3680 * Return pointer to the key under iterator.
3683 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3684 const struct dt_it *di)
3686 struct osd_it_iam *it = (struct osd_it_iam *)di;
3688 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3692 * Return size of key under iterator (in bytes)
3695 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3697 struct osd_it_iam *it = (struct osd_it_iam *)di;
3699 return iam_it_key_size(&it->oi_it);
3702 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3703 int len, __u16 type)
3705 struct luda_type *lt;
3706 const unsigned align = sizeof(struct luda_type) - 1;
3708 /* check if file type is required */
3709 if (attr & LUDA_TYPE) {
3710 len = (len + align) & ~align;
3712 lt = (void *) ent->lde_name + len;
3713 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3714 ent->lde_attrs |= LUDA_TYPE;
3717 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3721 * build lu direct from backend fs dirent.
3724 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3725 struct lu_fid *fid, __u64 offset,
3726 char *name, __u16 namelen,
3727 __u16 type, __u32 attr)
3729 fid_cpu_to_le(&ent->lde_fid, fid);
3730 ent->lde_attrs = LUDA_FID;
3732 ent->lde_hash = cpu_to_le64(offset);
3733 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3735 strncpy(ent->lde_name, name, namelen);
3736 ent->lde_namelen = cpu_to_le16(namelen);
3738 /* append lustre attributes */
3739 osd_it_append_attrs(ent, attr, namelen, type);
3743 * Return pointer to the record under iterator.
3745 static int osd_it_iam_rec(const struct lu_env *env,
3746 const struct dt_it *di,
3747 struct dt_rec *dtrec, __u32 attr)
3749 struct osd_it_iam *it = (struct osd_it_iam *)di;
3750 struct osd_thread_info *info = osd_oti_get(env);
3753 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3754 const struct osd_fid_pack *rec;
3755 struct lu_fid *fid = &info->oti_fid;
3756 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3762 name = (char *)iam_it_key_get(&it->oi_it);
3764 RETURN(PTR_ERR(name));
3766 namelen = iam_it_key_size(&it->oi_it);
3768 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3770 RETURN(PTR_ERR(rec));
3772 rc = osd_fid_unpack(fid, rec);
3776 hash = iam_it_store(&it->oi_it);
3778 /* IAM does not store object type in IAM index (dir) */
3779 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3782 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3783 (struct iam_rec *)dtrec);
3790 * Returns cookie for current Iterator position.
3792 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3794 struct osd_it_iam *it = (struct osd_it_iam *)di;
3796 return iam_it_store(&it->oi_it);
3800 * Restore iterator from cookie.
3802 * \param di osd iterator
3803 * \param hash Iterator location cookie
3805 * \retval +ve di points to record with least key not larger than key.
3806 * \retval 0 di points to exact matched key
3807 * \retval -ve failure
3810 static int osd_it_iam_load(const struct lu_env *env,
3811 const struct dt_it *di, __u64 hash)
3813 struct osd_it_iam *it = (struct osd_it_iam *)di;
3815 return iam_it_load(&it->oi_it, hash);
3818 static const struct dt_index_operations osd_index_iam_ops = {
3819 .dio_lookup = osd_index_iam_lookup,
3820 .dio_declare_insert = osd_index_declare_iam_insert,
3821 .dio_insert = osd_index_iam_insert,
3822 .dio_declare_delete = osd_index_declare_iam_delete,
3823 .dio_delete = osd_index_iam_delete,
3825 .init = osd_it_iam_init,
3826 .fini = osd_it_iam_fini,
3827 .get = osd_it_iam_get,
3828 .put = osd_it_iam_put,
3829 .next = osd_it_iam_next,
3830 .key = osd_it_iam_key,
3831 .key_size = osd_it_iam_key_size,
3832 .rec = osd_it_iam_rec,
3833 .store = osd_it_iam_store,
3834 .load = osd_it_iam_load
3839 * Creates or initializes iterator context.
3841 * \retval struct osd_it_ea, iterator structure on success
3844 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3845 struct dt_object *dt,
3847 struct lustre_capa *capa)
3849 struct osd_object *obj = osd_dt_obj(dt);
3850 struct osd_thread_info *info = osd_oti_get(env);
3851 struct osd_it_ea *it = &info->oti_it_ea;
3852 struct lu_object *lo = &dt->do_lu;
3853 struct dentry *obj_dentry = &info->oti_it_dentry;
3855 LASSERT(lu_object_exists(lo));
3857 obj_dentry->d_inode = obj->oo_inode;
3858 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3859 obj_dentry->d_name.hash = 0;
3861 it->oie_rd_dirent = 0;
3862 it->oie_it_dirent = 0;
3863 it->oie_dirent = NULL;
3864 it->oie_buf = info->oti_it_ea_buf;
3866 it->oie_file.f_pos = 0;
3867 it->oie_file.f_dentry = obj_dentry;
3868 if (attr & LUDA_64BITHASH)
3869 it->oie_file.f_mode |= FMODE_64BITHASH;
3871 it->oie_file.f_mode |= FMODE_32BITHASH;
3872 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3873 it->oie_file.f_op = obj->oo_inode->i_fop;
3874 it->oie_file.private_data = NULL;
3876 RETURN((struct dt_it *) it);
3880 * Destroy or finishes iterator context.
3882 * \param di iterator structure to be destroyed
3884 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3886 struct osd_it_ea *it = (struct osd_it_ea *)di;
3887 struct osd_object *obj = it->oie_obj;
3888 struct inode *inode = obj->oo_inode;
3891 it->oie_file.f_op->release(inode, &it->oie_file);
3892 lu_object_put(env, &obj->oo_dt.do_lu);
3897 * It position the iterator at given key, so that next lookup continues from
3898 * that key Or it is similar to dio_it->load() but based on a key,
3899 * rather than file position.
3901 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3904 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3906 static int osd_it_ea_get(const struct lu_env *env,
3907 struct dt_it *di, const struct dt_key *key)
3909 struct osd_it_ea *it = (struct osd_it_ea *)di;
3912 LASSERT(((const char *)key)[0] == '\0');
3913 it->oie_file.f_pos = 0;
3914 it->oie_rd_dirent = 0;
3915 it->oie_it_dirent = 0;
3916 it->oie_dirent = NULL;
3924 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3929 * It is called internally by ->readdir(). It fills the
3930 * iterator's in-memory data structure with required
3931 * information i.e. name, namelen, rec_size etc.
3933 * \param buf in which information to be filled in.
3934 * \param name name of the file in given dir
3936 * \retval 0 on success
3937 * \retval 1 on buffer full
3939 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3940 loff_t offset, __u64 ino,
3943 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3944 struct osd_it_ea_dirent *ent = it->oie_dirent;
3945 struct lu_fid *fid = &ent->oied_fid;
3946 struct osd_fid_pack *rec;
3949 /* this should never happen */
3950 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3951 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3955 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3959 if (d_type & LDISKFS_DIRENT_LUFID) {
3960 rec = (struct osd_fid_pack*) (name + namelen + 1);
3962 if (osd_fid_unpack(fid, rec) != 0)
3965 d_type &= ~LDISKFS_DIRENT_LUFID;
3970 ent->oied_ino = ino;
3971 ent->oied_off = offset;
3972 ent->oied_namelen = namelen;
3973 ent->oied_type = d_type;
3975 memcpy(ent->oied_name, name, namelen);
3977 it->oie_rd_dirent++;
3978 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3983 * Calls ->readdir() to load a directory entry at a time
3984 * and stored it in iterator's in-memory data structure.
3986 * \param di iterator's in memory structure
3988 * \retval 0 on success
3989 * \retval -ve on error
3991 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3992 const struct dt_it *di)
3994 struct osd_it_ea *it = (struct osd_it_ea *)di;
3995 struct osd_object *obj = it->oie_obj;
3996 struct inode *inode = obj->oo_inode;
3997 struct htree_lock *hlock = NULL;
4001 it->oie_dirent = it->oie_buf;
4002 it->oie_rd_dirent = 0;
4004 if (obj->oo_hl_head != NULL) {
4005 hlock = osd_oti_get(env)->oti_hlock;
4006 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4007 inode, LDISKFS_HLOCK_READDIR);
4009 cfs_down_read(&obj->oo_ext_idx_sem);
4012 result = inode->i_fop->readdir(&it->oie_file, it,
4013 (filldir_t) osd_ldiskfs_filldir);
4016 ldiskfs_htree_unlock(hlock);
4018 cfs_up_read(&obj->oo_ext_idx_sem);
4020 if (it->oie_rd_dirent == 0) {
4023 it->oie_dirent = it->oie_buf;
4024 it->oie_it_dirent = 1;
4031 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4032 * to load a directory entry at a time and stored it in
4033 * iterator's in-memory data structure.
4035 * \param di iterator's in memory structure
4037 * \retval +ve iterator reached to end
4038 * \retval 0 iterator not reached to end
4039 * \retval -ve on error
4041 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4043 struct osd_it_ea *it = (struct osd_it_ea *)di;
4048 if (it->oie_it_dirent < it->oie_rd_dirent) {
4050 (void *) it->oie_dirent +
4051 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4052 it->oie_dirent->oied_namelen);
4053 it->oie_it_dirent++;
4056 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4059 rc = osd_ldiskfs_it_fill(env, di);
4066 * Returns the key at current position from iterator's in memory structure.
4068 * \param di iterator's in memory structure
4070 * \retval key i.e. struct dt_key on success
4072 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4073 const struct dt_it *di)
4075 struct osd_it_ea *it = (struct osd_it_ea *)di;
4077 return (struct dt_key *)it->oie_dirent->oied_name;
4081 * Returns the key's size at current position from iterator's in memory structure.
4083 * \param di iterator's in memory structure
4085 * \retval key_size i.e. struct dt_key on success
4087 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4089 struct osd_it_ea *it = (struct osd_it_ea *)di;
4091 return it->oie_dirent->oied_namelen;
4096 * Returns the value (i.e. fid/igif) at current position from iterator's
4097 * in memory structure.
4099 * \param di struct osd_it_ea, iterator's in memory structure
4100 * \param attr attr requested for dirent.
4101 * \param lde lustre dirent
4103 * \retval 0 no error and \param lde has correct lustre dirent.
4104 * \retval -ve on error
4106 static inline int osd_it_ea_rec(const struct lu_env *env,
4107 const struct dt_it *di,
4108 struct dt_rec *dtrec, __u32 attr)
4110 struct osd_it_ea *it = (struct osd_it_ea *)di;
4111 struct osd_object *obj = it->oie_obj;
4112 struct osd_device *dev = osd_obj2dev(obj);
4113 struct osd_scrub *scrub = &dev->od_scrub;
4114 struct scrub_file *sf = &scrub->os_file;
4115 struct osd_thread_info *oti = osd_oti_get(env);
4116 struct osd_idmap_cache *oic = &oti->oti_cache;
4117 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4118 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4119 __u32 ino = it->oie_dirent->oied_ino;
4123 if (!fid_is_sane(fid)) {
4124 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4128 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4131 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4132 it->oie_dirent->oied_name,
4133 it->oie_dirent->oied_namelen,
4134 it->oie_dirent->oied_type, attr);
4135 if (!fid_is_norm(fid)) {
4136 fid_zero(&oic->oic_fid);
4140 oic->oic_fid = *fid;
4141 if ((scrub->os_pos_current <= ino) &&
4142 (sf->sf_flags & SF_INCONSISTENT ||
4143 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4144 rc = osd_consistency_check(oti, dev, oic);
4150 * Returns a cookie for current position of the iterator head, so that
4151 * user can use this cookie to load/start the iterator next time.
4153 * \param di iterator's in memory structure
4155 * \retval cookie for current position, on success
4157 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4159 struct osd_it_ea *it = (struct osd_it_ea *)di;
4161 return it->oie_dirent->oied_off;
4165 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4166 * to load a directory entry at a time and stored it i inn,
4167 * in iterator's in-memory data structure.
4169 * \param di struct osd_it_ea, iterator's in memory structure
4171 * \retval +ve on success
4172 * \retval -ve on error
4174 static int osd_it_ea_load(const struct lu_env *env,
4175 const struct dt_it *di, __u64 hash)
4177 struct osd_it_ea *it = (struct osd_it_ea *)di;
4181 it->oie_file.f_pos = hash;
4183 rc = osd_ldiskfs_it_fill(env, di);
4191 * Index lookup function for interoperability mode (b11826).
4193 * \param key, key i.e. file name to be searched
4195 * \retval +ve, on success
4196 * \retval -ve, on error
4198 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4199 struct dt_rec *rec, const struct dt_key *key,
4200 struct lustre_capa *capa)
4202 struct osd_object *obj = osd_dt_obj(dt);
4207 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4208 LINVRNT(osd_invariant(obj));
4210 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4213 rc = osd_ea_lookup_rec(env, obj, rec, key);
4221 * Index and Iterator operations for interoperability
4222 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4224 static const struct dt_index_operations osd_index_ea_ops = {
4225 .dio_lookup = osd_index_ea_lookup,
4226 .dio_declare_insert = osd_index_declare_ea_insert,
4227 .dio_insert = osd_index_ea_insert,
4228 .dio_declare_delete = osd_index_declare_ea_delete,
4229 .dio_delete = osd_index_ea_delete,
4231 .init = osd_it_ea_init,
4232 .fini = osd_it_ea_fini,
4233 .get = osd_it_ea_get,
4234 .put = osd_it_ea_put,
4235 .next = osd_it_ea_next,
4236 .key = osd_it_ea_key,
4237 .key_size = osd_it_ea_key_size,
4238 .rec = osd_it_ea_rec,
4239 .store = osd_it_ea_store,
4240 .load = osd_it_ea_load
4244 static void *osd_key_init(const struct lu_context *ctx,
4245 struct lu_context_key *key)
4247 struct osd_thread_info *info;
4249 OBD_ALLOC_PTR(info);
4251 return ERR_PTR(-ENOMEM);
4253 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4254 if (info->oti_it_ea_buf == NULL)
4257 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4259 info->oti_hlock = ldiskfs_htree_lock_alloc();
4260 if (info->oti_hlock == NULL)
4266 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4269 return ERR_PTR(-ENOMEM);
4272 static void osd_key_fini(const struct lu_context *ctx,
4273 struct lu_context_key *key, void* data)
4275 struct osd_thread_info *info = data;
4277 if (info->oti_hlock != NULL)
4278 ldiskfs_htree_lock_free(info->oti_hlock);
4279 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4283 static void osd_key_exit(const struct lu_context *ctx,
4284 struct lu_context_key *key, void *data)
4286 struct osd_thread_info *info = data;
4288 LASSERT(info->oti_r_locks == 0);
4289 LASSERT(info->oti_w_locks == 0);
4290 LASSERT(info->oti_txns == 0);
4293 /* type constructor/destructor: osd_type_init, osd_type_fini */
4294 LU_TYPE_INIT_FINI(osd, &osd_key);
4296 struct lu_context_key osd_key = {
4297 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4298 .lct_init = osd_key_init,
4299 .lct_fini = osd_key_fini,
4300 .lct_exit = osd_key_exit
4304 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4305 const char *name, struct lu_device *next)
4307 struct osd_device *osd = osd_dev(d);
4309 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4310 return osd_procfs_init(osd, name);
4313 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4317 osd_scrub_cleanup(env, o);
4320 fsfilt_put_ops(o->od_fsops);
4324 /* shutdown quota slave instance associated with the device */
4325 if (o->od_quota_slave != NULL) {
4326 qsd_fini(env, o->od_quota_slave);
4327 o->od_quota_slave = NULL;
4333 static int osd_mount(const struct lu_env *env,
4334 struct osd_device *o, struct lustre_cfg *cfg)
4336 const char *name = lustre_cfg_string(cfg, 0);
4337 const char *dev = lustre_cfg_string(cfg, 1);
4339 unsigned long page, s_flags, lmd_flags = 0;
4340 struct page *__page;
4341 struct file_system_type *type;
4342 char *options = NULL;
4347 if (o->od_mnt != NULL)
4350 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4351 if (o->od_fsops == NULL) {
4352 CERROR("Can't find fsfilt_ldiskfs\n");
4356 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
4360 str = lustre_cfg_string(cfg, 2);
4361 s_flags = simple_strtoul(str, NULL, 0);
4362 str = strstr(str, ":");
4364 lmd_flags = simple_strtoul(str + 1, NULL, 0);
4365 opts = lustre_cfg_string(cfg, 3);
4366 page = (unsigned long)cfs_page_address(__page);
4367 options = (char *)page;
4370 strcat(options, "user_xattr,acl");
4372 strcat(options, opts);
4374 /* Glom up mount options */
4375 if (*options != '\0')
4376 strcat(options, ",");
4377 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
4379 type = get_fs_type("ldiskfs");
4381 CERROR("%s: cannot find ldiskfs module\n", name);
4382 GOTO(out, rc = -ENODEV);
4385 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
4386 cfs_module_put(type->owner);
4388 if (IS_ERR(o->od_mnt)) {
4389 rc = PTR_ERR(o->od_mnt);
4390 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
4395 if (lvfs_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
4396 CERROR("%s: underlying device %s is marked as read-only. "
4397 "Setup failed\n", name, dev);
4400 GOTO(out, rc = -EROFS);
4403 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
4404 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
4405 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
4408 GOTO(out, rc = -EINVAL);
4411 if (lmd_flags & LMD_FLG_IAM) {
4413 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", name);
4416 if (lmd_flags & LMD_FLG_NOSCRUB)
4417 o->od_scrub.os_no_scrub = 1;
4421 OBD_PAGE_FREE(__page);
4426 static struct lu_device *osd_device_fini(const struct lu_env *env,
4427 struct lu_device *d)
4432 rc = osd_shutdown(env, osd_dev(d));
4434 osd_compat_fini(osd_dev(d));
4436 shrink_dcache_sb(osd_sb(osd_dev(d)));
4437 osd_sync(env, lu2dt_dev(d));
4439 rc = osd_procfs_fini(osd_dev(d));
4441 CERROR("proc fini error %d \n", rc);
4442 RETURN (ERR_PTR(rc));
4445 if (osd_dev(d)->od_mnt) {
4446 mntput(osd_dev(d)->od_mnt);
4447 osd_dev(d)->od_mnt = NULL;
4453 static int osd_device_init0(const struct lu_env *env,
4454 struct osd_device *o,
4455 struct lustre_cfg *cfg)
4457 struct lu_device *l = osd2lu_dev(o);
4458 struct osd_thread_info *info;
4461 /* if the module was re-loaded, env can loose its keys */
4462 rc = lu_env_refill((struct lu_env *) env);
4465 info = osd_oti_get(env);
4468 l->ld_ops = &osd_lu_ops;
4469 o->od_dt_dev.dd_ops = &osd_dt_ops;
4471 cfs_spin_lock_init(&o->od_osfs_lock);
4472 cfs_mutex_init(&o->od_otable_mutex);
4473 o->od_osfs_age = cfs_time_shift_64(-1000);
4475 o->od_capa_hash = init_capa_hash();
4476 if (o->od_capa_hash == NULL)
4477 GOTO(out, rc = -ENOMEM);
4479 o->od_read_cache = 1;
4480 o->od_writethrough_cache = 1;
4482 rc = osd_mount(env, o, cfg);
4486 /* setup scrub, including OI files initialization */
4487 rc = osd_scrub_setup(env, o);
4491 strncpy(o->od_svname, lustre_cfg_string(cfg, 4),
4492 sizeof(o->od_svname) - 1);
4494 if (strstr(o->od_svname, "-OST")) {
4495 rc = osd_compat_init(o);
4500 rc = lu_site_init(&o->od_site, l);
4502 GOTO(out_compat, rc);
4504 rc = lu_site_init_finish(&o->od_site);
4506 GOTO(out_compat, rc);
4508 rc = osd_procfs_init(o, o->od_svname);
4510 CERROR("%s: can't initialize procfs: rc = %d\n",
4512 GOTO(out_compat, rc);
4515 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
4521 osd_oi_fini(info, o);
4522 osd_shutdown(env, o);
4526 cleanup_capa_hash(o->od_capa_hash);
4531 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4532 struct lu_device_type *t,
4533 struct lustre_cfg *cfg)
4535 struct osd_device *o;
4540 return ERR_PTR(-ENOMEM);
4542 rc = dt_device_init(&o->od_dt_dev, t);
4544 rc = osd_device_init0(env, o, cfg);
4546 dt_device_fini(&o->od_dt_dev);
4549 if (unlikely(rc != 0))
4552 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
4555 static struct lu_device *osd_device_free(const struct lu_env *env,
4556 struct lu_device *d)
4558 struct osd_device *o = osd_dev(d);
4561 cleanup_capa_hash(o->od_capa_hash);
4562 /* XXX: make osd top device in order to release reference */
4563 d->ld_site->ls_top_dev = d;
4564 lu_site_purge(env, d->ld_site, -1);
4565 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
4566 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
4567 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
4569 lu_site_fini(&o->od_site);
4570 dt_device_fini(&o->od_dt_dev);
4575 static int osd_process_config(const struct lu_env *env,
4576 struct lu_device *d, struct lustre_cfg *cfg)
4578 struct osd_device *o = osd_dev(d);
4582 switch(cfg->lcfg_command) {
4584 err = osd_mount(env, o, cfg);
4587 lu_dev_del_linkage(d->ld_site, d);
4597 static int osd_recovery_complete(const struct lu_env *env,
4598 struct lu_device *d)
4604 * we use exports to track all osd users
4606 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
4607 struct obd_device *obd, struct obd_uuid *cluuid,
4608 struct obd_connect_data *data, void *localdata)
4610 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4611 struct lustre_handle conn;
4615 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
4617 rc = class_connect(&conn, obd, cluuid);
4621 *exp = class_conn2export(&conn);
4623 cfs_spin_lock(&osd->od_osfs_lock);
4625 cfs_spin_unlock(&osd->od_osfs_lock);
4631 * once last export (we don't count self-export) disappeared
4632 * osd can be released
4634 static int osd_obd_disconnect(struct obd_export *exp)
4636 struct obd_device *obd = exp->exp_obd;
4637 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4638 int rc, release = 0;
4641 /* Only disconnect the underlying layers on the final disconnect. */
4642 cfs_spin_lock(&osd->od_osfs_lock);
4644 if (osd->od_connects == 0)
4646 cfs_spin_unlock(&osd->od_osfs_lock);
4648 rc = class_disconnect(exp); /* bz 9811 */
4650 if (rc == 0 && release)
4651 class_manual_cleanup(obd);
4655 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4656 struct lu_device *dev)
4658 struct osd_device *osd = osd_dev(dev);
4662 /* 2. setup quota slave instance */
4663 osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
4664 osd->od_proc_entry);
4665 if (IS_ERR(osd->od_quota_slave)) {
4666 result = PTR_ERR(osd->od_quota_slave);
4667 osd->od_quota_slave = NULL;
4671 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 55, 0)
4672 /* Unfortunately, the current MDD implementation relies on some specific
4673 * code to be executed in the OSD layer. Since OFD now also uses the OSD
4674 * module, we need a way to skip the metadata-specific code when running
4676 * The hack here is to check the type of the parent device which is
4677 * either MD (i.e. MDD device) with the current MDT stack or DT (i.e.
4678 * OFD device) on an OST. As a reminder, obdfilter does not use the OSD
4679 * layer and still relies on lvfs. This hack won't work any more when
4680 * LOD is landed since LOD is of DT type.
4681 * This code should be removed once the orion MDT changes (LOD/OSP, ...)
4682 * have been landed */
4683 osd->od_is_md = lu_device_is_md(pdev);
4685 #warning "all is_md checks must be removed from osd-ldiskfs"
4691 /* 3. setup local objects */
4692 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4696 static const struct lu_object_operations osd_lu_obj_ops = {
4697 .loo_object_init = osd_object_init,
4698 .loo_object_delete = osd_object_delete,
4699 .loo_object_release = osd_object_release,
4700 .loo_object_free = osd_object_free,
4701 .loo_object_print = osd_object_print,
4702 .loo_object_invariant = osd_object_invariant
4705 const struct lu_device_operations osd_lu_ops = {
4706 .ldo_object_alloc = osd_object_alloc,
4707 .ldo_process_config = osd_process_config,
4708 .ldo_recovery_complete = osd_recovery_complete,
4709 .ldo_prepare = osd_prepare,
4712 static const struct lu_device_type_operations osd_device_type_ops = {
4713 .ldto_init = osd_type_init,
4714 .ldto_fini = osd_type_fini,
4716 .ldto_start = osd_type_start,
4717 .ldto_stop = osd_type_stop,
4719 .ldto_device_alloc = osd_device_alloc,
4720 .ldto_device_free = osd_device_free,
4722 .ldto_device_init = osd_device_init,
4723 .ldto_device_fini = osd_device_fini
4726 struct lu_device_type osd_device_type = {
4727 .ldt_tags = LU_DEVICE_DT,
4728 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
4729 .ldt_ops = &osd_device_type_ops,
4730 .ldt_ctx_tags = LCT_LOCAL,
4734 * lprocfs legacy support.
4736 static struct obd_ops osd_obd_device_ops = {
4737 .o_owner = THIS_MODULE,
4738 .o_connect = osd_obd_connect,
4739 .o_disconnect = osd_obd_disconnect
4742 static int __init osd_mod_init(void)
4744 struct lprocfs_static_vars lvars;
4747 lprocfs_osd_init_vars(&lvars);
4748 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4749 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
4752 static void __exit osd_mod_exit(void)
4754 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
4757 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4758 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
4759 MODULE_LICENSE("GPL");
4761 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);