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, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
981 o->oo_inode ? o->oo_inode->i_ino : 0UL,
982 o->oo_inode ? o->oo_inode->i_generation : 0,
983 d ? d->id_ops->id_name : "plain");
987 * Concurrency: shouldn't matter.
989 int osd_statfs(const struct lu_env *env, struct dt_device *d,
990 struct obd_statfs *sfs)
992 struct osd_device *osd = osd_dt_dev(d);
993 struct super_block *sb = osd_sb(osd);
994 struct kstatfs *ksfs;
997 if (unlikely(osd->od_mount == NULL))
1000 /* osd_lproc.c call this without env, allocate ksfs for that case */
1001 if (unlikely(env == NULL)) {
1002 OBD_ALLOC_PTR(ksfs);
1006 ksfs = &osd_oti_get(env)->oti_ksfs;
1009 cfs_spin_lock(&osd->od_osfs_lock);
1010 /* cache 1 second */
1011 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1012 result = sb->s_op->statfs(sb->s_root, ksfs);
1013 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1014 osd->od_osfs_age = cfs_time_current_64();
1015 statfs_pack(&osd->od_statfs, ksfs);
1019 if (likely(result == 0))
1020 *sfs = osd->od_statfs;
1021 cfs_spin_unlock(&osd->od_osfs_lock);
1023 if (unlikely(env == NULL))
1030 * Concurrency: doesn't access mutable data.
1032 static void osd_conf_get(const struct lu_env *env,
1033 const struct dt_device *dev,
1034 struct dt_device_param *param)
1036 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1039 * XXX should be taken from not-yet-existing fs abstraction layer.
1041 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1042 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1043 param->ddp_block_shift = sb->s_blocksize_bits;
1044 param->ddp_mntopts = 0;
1045 if (test_opt(sb, XATTR_USER))
1046 param->ddp_mntopts |= MNTOPT_USERXATTR;
1047 if (test_opt(sb, POSIX_ACL))
1048 param->ddp_mntopts |= MNTOPT_ACL;
1050 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1051 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1052 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1055 param->ddp_max_ea_size = sb->s_blocksize;
1060 * Helper function to get and fill the buffer with input values.
1062 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1066 buf = &osd_oti_get(env)->oti_buf;
1073 * Concurrency: shouldn't matter.
1075 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1077 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1078 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1082 * Start commit for OSD device.
1084 * An implementation of dt_commit_async method for OSD device.
1085 * Asychronously starts underlayng fs sync and thereby a transaction
1088 * \param env environment
1089 * \param d dt device
1091 * \see dt_device_operations
1093 static int osd_commit_async(const struct lu_env *env,
1094 struct dt_device *d)
1096 struct super_block *s = osd_sb(osd_dt_dev(d));
1099 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1100 RETURN(s->s_op->sync_fs(s, 0));
1104 * Concurrency: shouldn't matter.
1107 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1109 struct super_block *sb = osd_sb(osd_dt_dev(d));
1113 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1115 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1120 * Concurrency: serialization provided by callers.
1122 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1123 int mode, unsigned long timeout, __u32 alg,
1124 struct lustre_capa_key *keys)
1126 struct osd_device *dev = osd_dt_dev(d);
1129 dev->od_fl_capa = mode;
1130 dev->od_capa_timeout = timeout;
1131 dev->od_capa_alg = alg;
1132 dev->od_capa_keys = keys;
1137 * Concurrency: serialization provided by callers.
1139 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1140 struct dt_quota_ctxt *ctxt, void *data)
1142 struct obd_device *obd = (void *)ctxt;
1143 struct vfsmount *mnt = (struct vfsmount *)data;
1146 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1147 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1148 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1149 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1150 obd->obd_lvfs_ctxt.fs = get_ds();
1156 * Note: we do not count into QUOTA here.
1157 * If we mount with --data_journal we may need more.
1159 const int osd_dto_credits_noquota[DTO_NR] = {
1162 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1163 * SINGLEDATA_TRANS_BLOCKS(8)
1164 * XXX Note: maybe iam need more, since iam have more level than
1167 [DTO_INDEX_INSERT] = 16,
1168 [DTO_INDEX_DELETE] = 16,
1172 [DTO_INDEX_UPDATE] = 16,
1174 * Create a object. The same as create object in EXT3.
1175 * DATA_TRANS_BLOCKS(14) +
1176 * INDEX_EXTRA_BLOCKS(8) +
1177 * 3(inode bits, groups, GDT)
1179 [DTO_OBJECT_CREATE] = 25,
1181 * XXX: real credits to be fixed
1183 [DTO_OBJECT_DELETE] = 25,
1185 * Attr set credits (inode)
1187 [DTO_ATTR_SET_BASE] = 1,
1189 * Xattr set. The same as xattr of EXT3.
1190 * DATA_TRANS_BLOCKS(14)
1191 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1192 * are also counted in. Do not know why?
1194 [DTO_XATTR_SET] = 14,
1197 * credits for inode change during write.
1199 [DTO_WRITE_BASE] = 3,
1201 * credits for single block write.
1203 [DTO_WRITE_BLOCK] = 14,
1205 * Attr set credits for chown.
1206 * This is extra credits for setattr, and it is null without quota
1208 [DTO_ATTR_SET_CHOWN]= 0
1211 static const struct dt_device_operations osd_dt_ops = {
1212 .dt_root_get = osd_root_get,
1213 .dt_statfs = osd_statfs,
1214 .dt_trans_create = osd_trans_create,
1215 .dt_trans_start = osd_trans_start,
1216 .dt_trans_stop = osd_trans_stop,
1217 .dt_trans_cb_add = osd_trans_cb_add,
1218 .dt_conf_get = osd_conf_get,
1219 .dt_sync = osd_sync,
1221 .dt_commit_async = osd_commit_async,
1222 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1223 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1226 static void osd_object_read_lock(const struct lu_env *env,
1227 struct dt_object *dt, unsigned role)
1229 struct osd_object *obj = osd_dt_obj(dt);
1230 struct osd_thread_info *oti = osd_oti_get(env);
1232 LINVRNT(osd_invariant(obj));
1234 LASSERT(obj->oo_owner != env);
1235 cfs_down_read_nested(&obj->oo_sem, role);
1237 LASSERT(obj->oo_owner == NULL);
1241 static void osd_object_write_lock(const struct lu_env *env,
1242 struct dt_object *dt, unsigned role)
1244 struct osd_object *obj = osd_dt_obj(dt);
1245 struct osd_thread_info *oti = osd_oti_get(env);
1247 LINVRNT(osd_invariant(obj));
1249 LASSERT(obj->oo_owner != env);
1250 cfs_down_write_nested(&obj->oo_sem, role);
1252 LASSERT(obj->oo_owner == NULL);
1253 obj->oo_owner = env;
1257 static void osd_object_read_unlock(const struct lu_env *env,
1258 struct dt_object *dt)
1260 struct osd_object *obj = osd_dt_obj(dt);
1261 struct osd_thread_info *oti = osd_oti_get(env);
1263 LINVRNT(osd_invariant(obj));
1265 LASSERT(oti->oti_r_locks > 0);
1267 cfs_up_read(&obj->oo_sem);
1270 static void osd_object_write_unlock(const struct lu_env *env,
1271 struct dt_object *dt)
1273 struct osd_object *obj = osd_dt_obj(dt);
1274 struct osd_thread_info *oti = osd_oti_get(env);
1276 LINVRNT(osd_invariant(obj));
1278 LASSERT(obj->oo_owner == env);
1279 LASSERT(oti->oti_w_locks > 0);
1281 obj->oo_owner = NULL;
1282 cfs_up_write(&obj->oo_sem);
1285 static int osd_object_write_locked(const struct lu_env *env,
1286 struct dt_object *dt)
1288 struct osd_object *obj = osd_dt_obj(dt);
1290 LINVRNT(osd_invariant(obj));
1292 return obj->oo_owner == env;
1295 static int capa_is_sane(const struct lu_env *env,
1296 struct osd_device *dev,
1297 struct lustre_capa *capa,
1298 struct lustre_capa_key *keys)
1300 struct osd_thread_info *oti = osd_oti_get(env);
1301 struct lustre_capa *tcapa = &oti->oti_capa;
1302 struct obd_capa *oc;
1306 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1308 if (capa_is_expired(oc)) {
1309 DEBUG_CAPA(D_ERROR, capa, "expired");
1316 if (capa_is_expired_sec(capa)) {
1317 DEBUG_CAPA(D_ERROR, capa, "expired");
1321 cfs_spin_lock(&capa_lock);
1322 for (i = 0; i < 2; i++) {
1323 if (keys[i].lk_keyid == capa->lc_keyid) {
1324 oti->oti_capa_key = keys[i];
1328 cfs_spin_unlock(&capa_lock);
1331 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1335 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1339 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1340 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1344 oc = capa_add(dev->od_capa_hash, capa);
1350 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1351 struct lustre_capa *capa, __u64 opc)
1353 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1354 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1355 struct md_capainfo *ci;
1358 if (!dev->od_fl_capa)
1361 if (capa == BYPASS_CAPA)
1364 ci = md_capainfo(env);
1368 if (ci->mc_auth == LC_ID_NONE)
1372 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1376 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1377 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1382 if (!capa_opc_supported(capa, opc)) {
1383 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1387 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1388 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1395 static struct timespec *osd_inode_time(const struct lu_env *env,
1396 struct inode *inode, __u64 seconds)
1398 struct osd_thread_info *oti = osd_oti_get(env);
1399 struct timespec *t = &oti->oti_time;
1401 t->tv_sec = seconds;
1403 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1408 static void osd_inode_getattr(const struct lu_env *env,
1409 struct inode *inode, struct lu_attr *attr)
1411 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1412 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1413 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1415 attr->la_atime = LTIME_S(inode->i_atime);
1416 attr->la_mtime = LTIME_S(inode->i_mtime);
1417 attr->la_ctime = LTIME_S(inode->i_ctime);
1418 attr->la_mode = inode->i_mode;
1419 attr->la_size = i_size_read(inode);
1420 attr->la_blocks = inode->i_blocks;
1421 attr->la_uid = inode->i_uid;
1422 attr->la_gid = inode->i_gid;
1423 attr->la_flags = LDISKFS_I(inode)->i_flags;
1424 attr->la_nlink = inode->i_nlink;
1425 attr->la_rdev = inode->i_rdev;
1426 attr->la_blksize = 1 << inode->i_blkbits;
1427 attr->la_blkbits = inode->i_blkbits;
1430 static int osd_attr_get(const struct lu_env *env,
1431 struct dt_object *dt,
1432 struct lu_attr *attr,
1433 struct lustre_capa *capa)
1435 struct osd_object *obj = osd_dt_obj(dt);
1437 LASSERT(dt_object_exists(dt));
1438 LINVRNT(osd_invariant(obj));
1440 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1443 cfs_spin_lock(&obj->oo_guard);
1444 osd_inode_getattr(env, obj->oo_inode, attr);
1445 cfs_spin_unlock(&obj->oo_guard);
1449 static int osd_declare_attr_set(const struct lu_env *env,
1450 struct dt_object *dt,
1451 const struct lu_attr *attr,
1452 struct thandle *handle)
1454 struct osd_thandle *oh;
1455 struct osd_object *obj;
1457 LASSERT(dt != NULL);
1458 LASSERT(handle != NULL);
1460 obj = osd_dt_obj(dt);
1461 LASSERT(osd_invariant(obj));
1463 oh = container_of0(handle, struct osd_thandle, ot_super);
1464 LASSERT(oh->ot_handle == NULL);
1466 OSD_DECLARE_OP(oh, attr_set);
1467 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1469 if (attr && attr->la_valid & LA_UID) {
1471 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1473 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1475 if (attr && attr->la_valid & LA_GID) {
1477 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1479 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1485 static int osd_inode_setattr(const struct lu_env *env,
1486 struct inode *inode, const struct lu_attr *attr)
1490 bits = attr->la_valid;
1492 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1494 if (bits & LA_ATIME)
1495 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1496 if (bits & LA_CTIME)
1497 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1498 if (bits & LA_MTIME)
1499 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1500 if (bits & LA_SIZE) {
1501 LDISKFS_I(inode)->i_disksize = attr->la_size;
1502 i_size_write(inode, attr->la_size);
1506 /* OSD should not change "i_blocks" which is used by quota.
1507 * "i_blocks" should be changed by ldiskfs only. */
1508 if (bits & LA_BLOCKS)
1509 inode->i_blocks = attr->la_blocks;
1512 inode->i_mode = (inode->i_mode & S_IFMT) |
1513 (attr->la_mode & ~S_IFMT);
1515 inode->i_uid = attr->la_uid;
1517 inode->i_gid = attr->la_gid;
1518 if (bits & LA_NLINK)
1519 inode->i_nlink = attr->la_nlink;
1521 inode->i_rdev = attr->la_rdev;
1523 if (bits & LA_FLAGS) {
1524 /* always keep S_NOCMTIME */
1525 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1531 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1533 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1534 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1539 if (attr->la_valid & LA_UID)
1540 iattr.ia_valid |= ATTR_UID;
1541 if (attr->la_valid & LA_GID)
1542 iattr.ia_valid |= ATTR_GID;
1543 iattr.ia_uid = attr->la_uid;
1544 iattr.ia_gid = attr->la_gid;
1546 rc = ll_vfs_dq_transfer(inode, &iattr);
1548 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1549 "enforcement enabled on the ldiskfs filesystem?",
1550 inode->i_sb->s_id, rc);
1557 static int osd_attr_set(const struct lu_env *env,
1558 struct dt_object *dt,
1559 const struct lu_attr *attr,
1560 struct thandle *handle,
1561 struct lustre_capa *capa)
1563 struct osd_object *obj = osd_dt_obj(dt);
1564 struct inode *inode;
1567 LASSERT(handle != NULL);
1568 LASSERT(dt_object_exists(dt));
1569 LASSERT(osd_invariant(obj));
1571 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1574 OSD_EXEC_OP(handle, attr_set);
1576 inode = obj->oo_inode;
1577 if (!osd_dt_dev(handle->th_dev)->od_is_md) {
1579 rc = osd_quota_transfer(inode, attr);
1583 #ifdef HAVE_QUOTA_SUPPORT
1584 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1585 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1586 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1591 if (attr->la_valid & LA_UID)
1592 iattr.ia_valid |= ATTR_UID;
1593 if (attr->la_valid & LA_GID)
1594 iattr.ia_valid |= ATTR_GID;
1595 iattr.ia_uid = attr->la_uid;
1596 iattr.ia_gid = attr->la_gid;
1597 osd_push_ctxt(env, save, 1);
1598 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1599 osd_pop_ctxt(save, 1);
1605 cfs_spin_lock(&obj->oo_guard);
1606 rc = osd_inode_setattr(env, inode, attr);
1607 cfs_spin_unlock(&obj->oo_guard);
1610 inode->i_sb->s_op->dirty_inode(inode);
1614 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1615 struct osd_object *obj,
1616 const char *name, const int namelen)
1618 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1621 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1623 struct dt_allocation_hint *hint,
1627 struct osd_device *osd = osd_obj2dev(obj);
1628 struct osd_thandle *oth;
1629 struct dt_object *parent = NULL;
1630 struct inode *inode;
1631 #ifdef HAVE_QUOTA_SUPPORT
1632 struct osd_ctxt *save = &info->oti_ctxt;
1635 LINVRNT(osd_invariant(obj));
1636 LASSERT(obj->oo_inode == NULL);
1637 LASSERT(obj->oo_hl_head == NULL);
1639 if (S_ISDIR(mode) && ldiskfs_pdo) {
1640 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1641 if (obj->oo_hl_head == NULL)
1645 oth = container_of(th, struct osd_thandle, ot_super);
1646 LASSERT(oth->ot_handle->h_transaction != NULL);
1648 if (hint && hint->dah_parent)
1649 parent = hint->dah_parent;
1651 #ifdef HAVE_QUOTA_SUPPORT
1652 osd_push_ctxt(info->oti_env, save, osd_dt_dev(th->th_dev)->od_is_md);
1654 inode = ldiskfs_create_inode(oth->ot_handle,
1655 parent ? osd_dt_obj(parent)->oo_inode :
1656 osd_sb(osd)->s_root->d_inode,
1658 #ifdef HAVE_QUOTA_SUPPORT
1659 osd_pop_ctxt(save, osd_dt_dev(th->th_dev)->od_is_md);
1661 if (!IS_ERR(inode)) {
1662 /* Do not update file c/mtime in ldiskfs.
1663 * NB: don't need any lock because no contention at this
1665 inode->i_flags |= S_NOCMTIME;
1666 inode->i_state |= I_LUSTRE_NOSCRUB;
1667 obj->oo_inode = inode;
1670 if (obj->oo_hl_head != NULL) {
1671 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1672 obj->oo_hl_head = NULL;
1674 result = PTR_ERR(inode);
1676 LINVRNT(osd_invariant(obj));
1684 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1685 struct lu_attr *attr,
1686 struct dt_allocation_hint *hint,
1687 struct dt_object_format *dof,
1691 struct osd_thandle *oth;
1692 struct osd_device *osd = osd_obj2dev(obj);
1693 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1695 LASSERT(S_ISDIR(attr->la_mode));
1697 oth = container_of(th, struct osd_thandle, ot_super);
1698 LASSERT(oth->ot_handle->h_transaction != NULL);
1699 result = osd_mkfile(info, obj, mode, hint, th);
1700 if (result == 0 && osd->od_iop_mode == 0) {
1701 LASSERT(obj->oo_inode != NULL);
1703 * XXX uh-oh... call low-level iam function directly.
1706 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1707 sizeof (struct osd_fid_pack),
1713 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1714 struct lu_attr *attr,
1715 struct dt_allocation_hint *hint,
1716 struct dt_object_format *dof,
1720 struct osd_thandle *oth;
1721 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1723 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1725 LASSERT(S_ISREG(attr->la_mode));
1727 oth = container_of(th, struct osd_thandle, ot_super);
1728 LASSERT(oth->ot_handle->h_transaction != NULL);
1730 result = osd_mkfile(info, obj, mode, hint, th);
1732 LASSERT(obj->oo_inode != NULL);
1733 if (feat->dif_flags & DT_IND_VARKEY)
1734 result = iam_lvar_create(obj->oo_inode,
1735 feat->dif_keysize_max,
1737 feat->dif_recsize_max,
1740 result = iam_lfix_create(obj->oo_inode,
1741 feat->dif_keysize_max,
1743 feat->dif_recsize_max,
1750 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1751 struct lu_attr *attr,
1752 struct dt_allocation_hint *hint,
1753 struct dt_object_format *dof,
1756 LASSERT(S_ISREG(attr->la_mode));
1757 return osd_mkfile(info, obj, (attr->la_mode &
1758 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1761 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1762 struct lu_attr *attr,
1763 struct dt_allocation_hint *hint,
1764 struct dt_object_format *dof,
1767 LASSERT(S_ISLNK(attr->la_mode));
1768 return osd_mkfile(info, obj, (attr->la_mode &
1769 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1772 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1773 struct lu_attr *attr,
1774 struct dt_allocation_hint *hint,
1775 struct dt_object_format *dof,
1778 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1781 LINVRNT(osd_invariant(obj));
1782 LASSERT(obj->oo_inode == NULL);
1783 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1784 S_ISFIFO(mode) || S_ISSOCK(mode));
1786 result = osd_mkfile(info, obj, mode, hint, th);
1788 LASSERT(obj->oo_inode != NULL);
1790 * This inode should be marked dirty for i_rdev. Currently
1791 * that is done in the osd_attr_init().
1793 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1795 LINVRNT(osd_invariant(obj));
1799 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1801 struct dt_allocation_hint *hint,
1802 struct dt_object_format *dof,
1805 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1807 osd_obj_type_f result;
1823 result = osd_mk_index;
1834 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1835 struct dt_object *parent, cfs_umode_t child_mode)
1839 memset(ah, 0, sizeof(*ah));
1840 ah->dah_parent = parent;
1841 ah->dah_mode = child_mode;
1844 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1845 struct lu_attr *attr, struct dt_object_format *dof)
1847 struct inode *inode = obj->oo_inode;
1848 __u64 valid = attr->la_valid;
1851 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1853 if (dof->dof_type != DFT_NODE)
1854 attr->la_valid &= ~LA_RDEV;
1855 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1856 attr->la_valid &= ~LA_ATIME;
1857 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1858 attr->la_valid &= ~LA_CTIME;
1859 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1860 attr->la_valid &= ~LA_MTIME;
1862 if (!osd_obj2dev(obj)->od_is_md) {
1864 result = osd_quota_transfer(inode, attr);
1868 #ifdef HAVE_QUOTA_SUPPORT
1869 attr->la_valid &= ~(LA_UID | LA_GID);
1873 if (attr->la_valid != 0) {
1874 result = osd_inode_setattr(info->oti_env, inode, attr);
1876 * The osd_inode_setattr() should always succeed here. The
1877 * only error that could be returned is EDQUOT when we are
1878 * trying to change the UID or GID of the inode. However, this
1879 * should not happen since quota enforcement is no longer
1880 * enabled on ldiskfs (lquota takes care of it).
1882 LASSERTF(result == 0, "%d", result);
1883 inode->i_sb->s_op->dirty_inode(inode);
1886 attr->la_valid = valid;
1890 * Helper function for osd_object_create()
1892 * \retval 0, on success
1894 static int __osd_object_create(struct osd_thread_info *info,
1895 struct osd_object *obj, struct lu_attr *attr,
1896 struct dt_allocation_hint *hint,
1897 struct dt_object_format *dof,
1903 /* we drop umask so that permissions we pass are not affected */
1904 umask = current->fs->umask;
1905 current->fs->umask = 0;
1907 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1910 osd_attr_init(info, obj, attr, dof);
1911 osd_object_init0(obj);
1913 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1914 unlock_new_inode(obj->oo_inode);
1917 /* restore previous umask value */
1918 current->fs->umask = umask;
1924 * Helper function for osd_object_create()
1926 * \retval 0, on success
1928 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1929 const struct lu_fid *fid, struct thandle *th)
1931 struct osd_thread_info *info = osd_oti_get(env);
1932 struct osd_inode_id *id = &info->oti_id;
1933 struct osd_device *osd = osd_obj2dev(obj);
1935 LASSERT(obj->oo_inode != NULL);
1937 if (osd->od_is_md) {
1938 struct md_ucred *uc = md_ucred(env);
1939 LASSERT(uc != NULL);
1942 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1943 return osd_oi_insert(info, osd, fid, id, th);
1946 static int osd_declare_object_create(const struct lu_env *env,
1947 struct dt_object *dt,
1948 struct lu_attr *attr,
1949 struct dt_allocation_hint *hint,
1950 struct dt_object_format *dof,
1951 struct thandle *handle)
1953 struct osd_thandle *oh;
1955 LASSERT(handle != NULL);
1957 oh = container_of0(handle, struct osd_thandle, ot_super);
1958 LASSERT(oh->ot_handle == NULL);
1960 OSD_DECLARE_OP(oh, create);
1961 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1962 /* XXX: So far, only normal fid needs be inserted into the oi,
1963 * things could be changed later. Revise following code then. */
1964 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1965 OSD_DECLARE_OP(oh, insert);
1966 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1968 /* If this is directory, then we expect . and .. to be inserted as
1969 * well. The one directory block always needs to be created for the
1970 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1971 * block), there is no danger of needing a tree for the first block.
1973 if (attr && S_ISDIR(attr->la_mode)) {
1974 OSD_DECLARE_OP(oh, insert);
1975 OSD_DECLARE_OP(oh, insert);
1976 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1980 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1981 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1986 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1987 struct lu_attr *attr,
1988 struct dt_allocation_hint *hint,
1989 struct dt_object_format *dof,
1992 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1993 struct osd_object *obj = osd_dt_obj(dt);
1994 struct osd_thread_info *info = osd_oti_get(env);
1999 LINVRNT(osd_invariant(obj));
2000 LASSERT(!dt_object_exists(dt));
2001 LASSERT(osd_write_locked(env, obj));
2002 LASSERT(th != NULL);
2004 if (unlikely(fid_is_acct(fid)))
2005 /* Quota files can't be created from the kernel any more,
2006 * 'tune2fs -O quota' will take care of creating them */
2009 OSD_EXEC_OP(th, create);
2011 result = __osd_object_create(info, obj, attr, hint, dof, th);
2013 result = __osd_oi_insert(env, obj, fid, th);
2015 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2016 LASSERT(osd_invariant(obj));
2021 * Called to destroy on-disk representation of the object
2023 * Concurrency: must be locked
2025 static int osd_declare_object_destroy(const struct lu_env *env,
2026 struct dt_object *dt,
2029 struct osd_object *obj = osd_dt_obj(dt);
2030 struct inode *inode = obj->oo_inode;
2031 struct osd_thandle *oh;
2035 oh = container_of0(th, struct osd_thandle, ot_super);
2036 LASSERT(oh->ot_handle == NULL);
2039 OSD_DECLARE_OP(oh, destroy);
2040 OSD_DECLARE_OP(oh, delete);
2041 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2042 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2044 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
2045 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
2050 static int osd_object_destroy(const struct lu_env *env,
2051 struct dt_object *dt,
2054 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2055 struct osd_object *obj = osd_dt_obj(dt);
2056 struct inode *inode = obj->oo_inode;
2057 struct osd_device *osd = osd_obj2dev(obj);
2058 struct osd_thandle *oh;
2062 oh = container_of0(th, struct osd_thandle, ot_super);
2063 LASSERT(oh->ot_handle);
2065 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2067 if (unlikely(fid_is_acct(fid)))
2070 /* Parallel control for OI scrub. For most of cases, there is no
2071 * lock contention. So it will not affect unlink performance. */
2072 cfs_mutex_lock(&inode->i_mutex);
2073 if (S_ISDIR(inode->i_mode)) {
2074 LASSERT(osd_inode_unlinked(inode) ||
2075 inode->i_nlink == 1);
2076 cfs_spin_lock(&obj->oo_guard);
2078 cfs_spin_unlock(&obj->oo_guard);
2079 inode->i_sb->s_op->dirty_inode(inode);
2081 LASSERT(osd_inode_unlinked(inode));
2084 OSD_EXEC_OP(th, destroy);
2086 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2087 cfs_mutex_unlock(&inode->i_mutex);
2089 /* XXX: add to ext3 orphan list */
2090 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2092 /* not needed in the cache anymore */
2093 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2099 * Helper function for osd_xattr_set()
2101 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2102 const struct lu_buf *buf, const char *name, int fl)
2104 struct osd_object *obj = osd_dt_obj(dt);
2105 struct inode *inode = obj->oo_inode;
2106 struct osd_thread_info *info = osd_oti_get(env);
2107 struct dentry *dentry = &info->oti_child_dentry;
2111 LASSERT(dt_object_exists(dt));
2112 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2114 if (fl & LU_XATTR_REPLACE)
2115 fs_flags |= XATTR_REPLACE;
2117 if (fl & LU_XATTR_CREATE)
2118 fs_flags |= XATTR_CREATE;
2120 dentry->d_inode = inode;
2121 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2122 buf->lb_len, fs_flags);
2127 * Put the fid into lustre_mdt_attrs, and then place the structure
2128 * inode's ea. This fid should not be altered during the life time
2131 * \retval +ve, on success
2132 * \retval -ve, on error
2134 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2136 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2137 const struct lu_fid *fid)
2139 struct osd_thread_info *info = osd_oti_get(env);
2140 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2142 lustre_lma_init(mdt_attrs, fid);
2143 lustre_lma_swab(mdt_attrs);
2144 return __osd_xattr_set(env, dt,
2145 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2146 XATTR_NAME_LMA, LU_XATTR_CREATE);
2151 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2152 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2153 * To have compatilibility with 1.8 ldiskfs driver we need to have
2154 * magic number at start of fid data.
2155 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2158 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2159 const struct dt_rec *fid)
2161 param->edp_magic = LDISKFS_LUFID_MAGIC;
2162 param->edp_len = sizeof(struct lu_fid) + 1;
2164 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2165 (struct lu_fid *)fid);
2169 * Try to read the fid from inode ea into dt_rec, if return value
2170 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2172 * \param fid object fid.
2174 * \retval 0 on success
2176 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2177 __u32 ino, struct lu_fid *fid,
2178 struct osd_inode_id *id)
2180 struct osd_thread_info *info = osd_oti_get(env);
2181 struct inode *inode;
2184 osd_id_gen(id, ino, OSD_OII_NOGEN);
2185 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2187 RETURN(PTR_ERR(inode));
2194 * OSD layer object create function for interoperability mode (b11826).
2195 * This is mostly similar to osd_object_create(). Only difference being, fid is
2196 * inserted into inode ea here.
2198 * \retval 0, on success
2199 * \retval -ve, on error
2201 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2202 struct lu_attr *attr,
2203 struct dt_allocation_hint *hint,
2204 struct dt_object_format *dof,
2207 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2208 struct osd_object *obj = osd_dt_obj(dt);
2209 struct osd_thread_info *info = osd_oti_get(env);
2214 LASSERT(osd_invariant(obj));
2215 LASSERT(!dt_object_exists(dt));
2216 LASSERT(osd_write_locked(env, obj));
2217 LASSERT(th != NULL);
2219 if (unlikely(fid_is_acct(fid)))
2220 /* Quota files can't be created from the kernel any more,
2221 * 'tune2fs -O quota' will take care of creating them */
2224 OSD_EXEC_OP(th, create);
2226 result = __osd_object_create(info, obj, attr, hint, dof, th);
2227 /* objects under osd root shld have igif fid, so dont add fid EA */
2228 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2229 result = osd_ea_fid_set(env, dt, fid);
2232 result = __osd_oi_insert(env, obj, fid, th);
2234 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2235 LINVRNT(osd_invariant(obj));
2239 static int osd_declare_object_ref_add(const struct lu_env *env,
2240 struct dt_object *dt,
2241 struct thandle *handle)
2243 struct osd_thandle *oh;
2245 /* it's possible that object doesn't exist yet */
2246 LASSERT(handle != NULL);
2248 oh = container_of0(handle, struct osd_thandle, ot_super);
2249 LASSERT(oh->ot_handle == NULL);
2251 OSD_DECLARE_OP(oh, ref_add);
2252 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2258 * Concurrency: @dt is write locked.
2260 static int osd_object_ref_add(const struct lu_env *env,
2261 struct dt_object *dt, struct thandle *th)
2263 struct osd_object *obj = osd_dt_obj(dt);
2264 struct inode *inode = obj->oo_inode;
2266 LINVRNT(osd_invariant(obj));
2267 LASSERT(dt_object_exists(dt));
2268 LASSERT(osd_write_locked(env, obj));
2269 LASSERT(th != NULL);
2271 OSD_EXEC_OP(th, ref_add);
2274 * DIR_NLINK feature is set for compatibility reasons if:
2275 * 1) nlinks > LDISKFS_LINK_MAX, or
2276 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2278 * It is easier to always set this flag (rather than check and set),
2279 * since it has less overhead, and the superblock will be dirtied
2280 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2281 * do not actually care whether this flag is set or not.
2283 cfs_spin_lock(&obj->oo_guard);
2285 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2286 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2287 inode->i_nlink == 2)
2290 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2291 cfs_spin_unlock(&obj->oo_guard);
2292 inode->i_sb->s_op->dirty_inode(inode);
2293 LINVRNT(osd_invariant(obj));
2298 static int osd_declare_object_ref_del(const struct lu_env *env,
2299 struct dt_object *dt,
2300 struct thandle *handle)
2302 struct osd_thandle *oh;
2304 LASSERT(dt_object_exists(dt));
2305 LASSERT(handle != NULL);
2307 oh = container_of0(handle, struct osd_thandle, ot_super);
2308 LASSERT(oh->ot_handle == NULL);
2310 OSD_DECLARE_OP(oh, ref_del);
2311 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2317 * Concurrency: @dt is write locked.
2319 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2322 struct osd_object *obj = osd_dt_obj(dt);
2323 struct inode *inode = obj->oo_inode;
2325 LINVRNT(osd_invariant(obj));
2326 LASSERT(dt_object_exists(dt));
2327 LASSERT(osd_write_locked(env, obj));
2328 LASSERT(th != NULL);
2330 OSD_EXEC_OP(th, ref_del);
2332 cfs_spin_lock(&obj->oo_guard);
2333 LASSERT(inode->i_nlink > 0);
2335 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2336 * then the nlink count is 1. Don't let it be set to 0 or the directory
2337 * inode will be deleted incorrectly. */
2338 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2340 cfs_spin_unlock(&obj->oo_guard);
2341 inode->i_sb->s_op->dirty_inode(inode);
2342 LINVRNT(osd_invariant(obj));
2348 * Get the 64-bit version for an inode.
2350 static int osd_object_version_get(const struct lu_env *env,
2351 struct dt_object *dt, dt_obj_version_t *ver)
2353 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2355 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2356 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2357 *ver = LDISKFS_I(inode)->i_fs_version;
2362 * Concurrency: @dt is read locked.
2364 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2365 struct lu_buf *buf, const char *name,
2366 struct lustre_capa *capa)
2368 struct osd_object *obj = osd_dt_obj(dt);
2369 struct inode *inode = obj->oo_inode;
2370 struct osd_thread_info *info = osd_oti_get(env);
2371 struct dentry *dentry = &info->oti_obj_dentry;
2373 /* version get is not real XATTR but uses xattr API */
2374 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2375 /* for version we are just using xattr API but change inode
2377 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2378 osd_object_version_get(env, dt, buf->lb_buf);
2379 return sizeof(dt_obj_version_t);
2382 LASSERT(dt_object_exists(dt));
2383 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2384 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2386 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2389 dentry->d_inode = inode;
2390 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2394 static int osd_declare_xattr_set(const struct lu_env *env,
2395 struct dt_object *dt,
2396 const struct lu_buf *buf, const char *name,
2397 int fl, struct thandle *handle)
2399 struct osd_thandle *oh;
2401 LASSERT(handle != NULL);
2403 oh = container_of0(handle, struct osd_thandle, ot_super);
2404 LASSERT(oh->ot_handle == NULL);
2406 OSD_DECLARE_OP(oh, xattr_set);
2407 if (strcmp(name, XATTR_NAME_VERSION) == 0)
2408 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2410 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2416 * Set the 64-bit version for object
2418 static void osd_object_version_set(const struct lu_env *env,
2419 struct dt_object *dt,
2420 dt_obj_version_t *new_version)
2422 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2424 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2425 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2427 LDISKFS_I(inode)->i_fs_version = *new_version;
2428 /** Version is set after all inode operations are finished,
2429 * so we should mark it dirty here */
2430 inode->i_sb->s_op->dirty_inode(inode);
2434 * Concurrency: @dt is write locked.
2436 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2437 const struct lu_buf *buf, const char *name, int fl,
2438 struct thandle *handle, struct lustre_capa *capa)
2440 LASSERT(handle != NULL);
2442 /* version set is not real XATTR */
2443 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2444 /* for version we are just using xattr API but change inode
2446 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2447 osd_object_version_set(env, dt, buf->lb_buf);
2448 return sizeof(dt_obj_version_t);
2451 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2454 OSD_EXEC_OP(handle, xattr_set);
2455 return __osd_xattr_set(env, dt, buf, name, fl);
2459 * Concurrency: @dt is read locked.
2461 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2462 struct lu_buf *buf, struct lustre_capa *capa)
2464 struct osd_object *obj = osd_dt_obj(dt);
2465 struct inode *inode = obj->oo_inode;
2466 struct osd_thread_info *info = osd_oti_get(env);
2467 struct dentry *dentry = &info->oti_obj_dentry;
2469 LASSERT(dt_object_exists(dt));
2470 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2471 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2473 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2476 dentry->d_inode = inode;
2477 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2480 static int osd_declare_xattr_del(const struct lu_env *env,
2481 struct dt_object *dt, const char *name,
2482 struct thandle *handle)
2484 struct osd_thandle *oh;
2486 LASSERT(dt_object_exists(dt));
2487 LASSERT(handle != NULL);
2489 oh = container_of0(handle, struct osd_thandle, ot_super);
2490 LASSERT(oh->ot_handle == NULL);
2492 OSD_DECLARE_OP(oh, xattr_set);
2493 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2499 * Concurrency: @dt is write locked.
2501 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2502 const char *name, struct thandle *handle,
2503 struct lustre_capa *capa)
2505 struct osd_object *obj = osd_dt_obj(dt);
2506 struct inode *inode = obj->oo_inode;
2507 struct osd_thread_info *info = osd_oti_get(env);
2508 struct dentry *dentry = &info->oti_obj_dentry;
2511 LASSERT(dt_object_exists(dt));
2512 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2513 LASSERT(osd_write_locked(env, obj));
2514 LASSERT(handle != NULL);
2516 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2519 OSD_EXEC_OP(handle, xattr_set);
2521 dentry->d_inode = inode;
2522 rc = inode->i_op->removexattr(dentry, name);
2526 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2527 struct dt_object *dt,
2528 struct lustre_capa *old,
2531 struct osd_thread_info *info = osd_oti_get(env);
2532 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2533 struct osd_object *obj = osd_dt_obj(dt);
2534 struct osd_device *dev = osd_obj2dev(obj);
2535 struct lustre_capa_key *key = &info->oti_capa_key;
2536 struct lustre_capa *capa = &info->oti_capa;
2537 struct obd_capa *oc;
2538 struct md_capainfo *ci;
2542 if (!dev->od_fl_capa)
2543 RETURN(ERR_PTR(-ENOENT));
2545 LASSERT(dt_object_exists(dt));
2546 LINVRNT(osd_invariant(obj));
2548 /* renewal sanity check */
2549 if (old && osd_object_auth(env, dt, old, opc))
2550 RETURN(ERR_PTR(-EACCES));
2552 ci = md_capainfo(env);
2554 RETURN(ERR_PTR(-ENOENT));
2556 switch (ci->mc_auth) {
2560 capa->lc_uid = obj->oo_inode->i_uid;
2561 capa->lc_gid = obj->oo_inode->i_gid;
2562 capa->lc_flags = LC_ID_PLAIN;
2564 case LC_ID_CONVERT: {
2567 s[0] = obj->oo_inode->i_uid;
2568 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2569 s[2] = obj->oo_inode->i_gid;
2570 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2571 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2573 RETURN(ERR_PTR(rc));
2575 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2576 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2577 capa->lc_flags = LC_ID_CONVERT;
2581 RETURN(ERR_PTR(-EINVAL));
2584 capa->lc_fid = *fid;
2586 capa->lc_flags |= dev->od_capa_alg << 24;
2587 capa->lc_timeout = dev->od_capa_timeout;
2588 capa->lc_expiry = 0;
2590 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2592 LASSERT(!capa_is_expired(oc));
2596 cfs_spin_lock(&capa_lock);
2597 *key = dev->od_capa_keys[1];
2598 cfs_spin_unlock(&capa_lock);
2600 capa->lc_keyid = key->lk_keyid;
2601 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2603 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2605 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2606 RETURN(ERR_PTR(rc));
2609 oc = capa_add(dev->od_capa_hash, capa);
2613 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2615 struct osd_object *obj = osd_dt_obj(dt);
2616 struct inode *inode = obj->oo_inode;
2617 struct osd_thread_info *info = osd_oti_get(env);
2618 struct dentry *dentry = &info->oti_obj_dentry;
2619 struct file *file = &info->oti_file;
2624 dentry->d_inode = inode;
2625 file->f_dentry = dentry;
2626 file->f_mapping = inode->i_mapping;
2627 file->f_op = inode->i_fop;
2628 mutex_lock(&inode->i_mutex);
2629 rc = file->f_op->fsync(file, dentry, 0);
2630 mutex_unlock(&inode->i_mutex);
2634 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2637 struct osd_object *obj = osd_dt_obj(dt);
2640 *data = (void *)obj->oo_inode;
2648 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2649 const struct dt_index_features *feat)
2651 struct iam_descr *descr;
2653 if (osd_object_is_root(o))
2654 return feat == &dt_directory_features;
2656 LASSERT(o->oo_dir != NULL);
2658 descr = o->oo_dir->od_container.ic_descr;
2659 if (feat == &dt_directory_features) {
2660 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2666 feat->dif_keysize_min <= descr->id_key_size &&
2667 descr->id_key_size <= feat->dif_keysize_max &&
2668 feat->dif_recsize_min <= descr->id_rec_size &&
2669 descr->id_rec_size <= feat->dif_recsize_max &&
2670 !(feat->dif_flags & (DT_IND_VARKEY |
2671 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2672 ergo(feat->dif_flags & DT_IND_UPDATE,
2673 1 /* XXX check that object (and file system) is
2678 static int osd_iam_container_init(const struct lu_env *env,
2679 struct osd_object *obj,
2680 struct osd_directory *dir)
2682 struct iam_container *bag = &dir->od_container;
2685 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2689 result = iam_container_setup(bag);
2691 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2693 iam_container_fini(bag);
2700 * Concurrency: no external locking is necessary.
2702 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2703 const struct dt_index_features *feat)
2707 struct osd_object *obj = osd_dt_obj(dt);
2708 struct osd_device *osd = osd_obj2dev(obj);
2710 LINVRNT(osd_invariant(obj));
2711 LASSERT(dt_object_exists(dt));
2713 if (osd_object_is_root(obj)) {
2714 dt->do_index_ops = &osd_index_ea_ops;
2716 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2717 dt->do_index_ops = &osd_index_ea_ops;
2718 if (S_ISDIR(obj->oo_inode->i_mode))
2723 } else if (unlikely(feat == &dt_otable_features)) {
2724 dt->do_index_ops = &osd_otable_ops;
2726 } else if (feat == &dt_acct_features) {
2727 dt->do_index_ops = &osd_acct_index_ops;
2730 } else if (!osd_has_index(obj)) {
2731 struct osd_directory *dir;
2736 cfs_spin_lock(&obj->oo_guard);
2737 if (obj->oo_dir == NULL)
2741 * Concurrent thread allocated container data.
2744 cfs_spin_unlock(&obj->oo_guard);
2746 * Now, that we have container data, serialize its
2749 cfs_down_write(&obj->oo_ext_idx_sem);
2751 * recheck under lock.
2753 if (!osd_has_index(obj))
2754 result = osd_iam_container_init(env, obj, dir);
2757 cfs_up_write(&obj->oo_ext_idx_sem);
2765 if (result == 0 && skip_iam == 0) {
2766 if (!osd_iam_index_probe(env, obj, feat))
2769 LINVRNT(osd_invariant(obj));
2774 static int osd_otable_it_attr_get(const struct lu_env *env,
2775 struct dt_object *dt,
2776 struct lu_attr *attr,
2777 struct lustre_capa *capa)
2783 static const struct dt_object_operations osd_obj_ops = {
2784 .do_read_lock = osd_object_read_lock,
2785 .do_write_lock = osd_object_write_lock,
2786 .do_read_unlock = osd_object_read_unlock,
2787 .do_write_unlock = osd_object_write_unlock,
2788 .do_write_locked = osd_object_write_locked,
2789 .do_attr_get = osd_attr_get,
2790 .do_declare_attr_set = osd_declare_attr_set,
2791 .do_attr_set = osd_attr_set,
2792 .do_ah_init = osd_ah_init,
2793 .do_declare_create = osd_declare_object_create,
2794 .do_create = osd_object_create,
2795 .do_declare_destroy = osd_declare_object_destroy,
2796 .do_destroy = osd_object_destroy,
2797 .do_index_try = osd_index_try,
2798 .do_declare_ref_add = osd_declare_object_ref_add,
2799 .do_ref_add = osd_object_ref_add,
2800 .do_declare_ref_del = osd_declare_object_ref_del,
2801 .do_ref_del = osd_object_ref_del,
2802 .do_xattr_get = osd_xattr_get,
2803 .do_declare_xattr_set = osd_declare_xattr_set,
2804 .do_xattr_set = osd_xattr_set,
2805 .do_declare_xattr_del = osd_declare_xattr_del,
2806 .do_xattr_del = osd_xattr_del,
2807 .do_xattr_list = osd_xattr_list,
2808 .do_capa_get = osd_capa_get,
2809 .do_object_sync = osd_object_sync,
2810 .do_data_get = osd_data_get,
2814 * dt_object_operations for interoperability mode
2815 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2817 static const struct dt_object_operations osd_obj_ea_ops = {
2818 .do_read_lock = osd_object_read_lock,
2819 .do_write_lock = osd_object_write_lock,
2820 .do_read_unlock = osd_object_read_unlock,
2821 .do_write_unlock = osd_object_write_unlock,
2822 .do_write_locked = osd_object_write_locked,
2823 .do_attr_get = osd_attr_get,
2824 .do_declare_attr_set = osd_declare_attr_set,
2825 .do_attr_set = osd_attr_set,
2826 .do_ah_init = osd_ah_init,
2827 .do_declare_create = osd_declare_object_create,
2828 .do_create = osd_object_ea_create,
2829 .do_declare_destroy = osd_declare_object_destroy,
2830 .do_destroy = osd_object_destroy,
2831 .do_index_try = osd_index_try,
2832 .do_declare_ref_add = osd_declare_object_ref_add,
2833 .do_ref_add = osd_object_ref_add,
2834 .do_declare_ref_del = osd_declare_object_ref_del,
2835 .do_ref_del = osd_object_ref_del,
2836 .do_xattr_get = osd_xattr_get,
2837 .do_declare_xattr_set = osd_declare_xattr_set,
2838 .do_xattr_set = osd_xattr_set,
2839 .do_declare_xattr_del = osd_declare_xattr_del,
2840 .do_xattr_del = osd_xattr_del,
2841 .do_xattr_list = osd_xattr_list,
2842 .do_capa_get = osd_capa_get,
2843 .do_object_sync = osd_object_sync,
2844 .do_data_get = osd_data_get,
2847 static const struct dt_object_operations osd_obj_otable_it_ops = {
2848 .do_attr_get = osd_otable_it_attr_get,
2849 .do_index_try = osd_index_try,
2852 static int osd_index_declare_iam_delete(const struct lu_env *env,
2853 struct dt_object *dt,
2854 const struct dt_key *key,
2855 struct thandle *handle)
2857 struct osd_thandle *oh;
2859 oh = container_of0(handle, struct osd_thandle, ot_super);
2860 LASSERT(oh->ot_handle == NULL);
2862 OSD_DECLARE_OP(oh, delete);
2863 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2869 * delete a (key, value) pair from index \a dt specified by \a key
2871 * \param dt osd index object
2872 * \param key key for index
2873 * \param rec record reference
2874 * \param handle transaction handler
2877 * \retval -ve failure
2880 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2881 const struct dt_key *key,
2882 struct thandle *handle,
2883 struct lustre_capa *capa)
2885 struct osd_object *obj = osd_dt_obj(dt);
2886 struct osd_thandle *oh;
2887 struct iam_path_descr *ipd;
2888 struct iam_container *bag = &obj->oo_dir->od_container;
2893 LINVRNT(osd_invariant(obj));
2894 LASSERT(dt_object_exists(dt));
2895 LASSERT(bag->ic_object == obj->oo_inode);
2896 LASSERT(handle != NULL);
2898 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2901 OSD_EXEC_OP(handle, delete);
2903 ipd = osd_idx_ipd_get(env, bag);
2904 if (unlikely(ipd == NULL))
2907 oh = container_of0(handle, struct osd_thandle, ot_super);
2908 LASSERT(oh->ot_handle != NULL);
2909 LASSERT(oh->ot_handle->h_transaction != NULL);
2911 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2912 osd_ipd_put(env, bag, ipd);
2913 LINVRNT(osd_invariant(obj));
2917 static int osd_index_declare_ea_delete(const struct lu_env *env,
2918 struct dt_object *dt,
2919 const struct dt_key *key,
2920 struct thandle *handle)
2922 struct osd_thandle *oh;
2924 LASSERT(dt_object_exists(dt));
2925 LASSERT(handle != NULL);
2927 oh = container_of0(handle, struct osd_thandle, ot_super);
2928 LASSERT(oh->ot_handle == NULL);
2930 OSD_DECLARE_OP(oh, delete);
2931 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2933 LASSERT(osd_dt_obj(dt)->oo_inode);
2934 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2935 osd_dt_obj(dt)->oo_inode);
2936 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2937 osd_dt_obj(dt)->oo_inode);
2942 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2945 struct osd_fid_pack *rec;
2948 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2949 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2950 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2956 * Index delete function for interoperability mode (b11826).
2957 * It will remove the directory entry added by osd_index_ea_insert().
2958 * This entry is needed to maintain name->fid mapping.
2960 * \param key, key i.e. file entry to be deleted
2962 * \retval 0, on success
2963 * \retval -ve, on error
2965 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2966 const struct dt_key *key,
2967 struct thandle *handle,
2968 struct lustre_capa *capa)
2970 struct osd_object *obj = osd_dt_obj(dt);
2971 struct inode *dir = obj->oo_inode;
2972 struct dentry *dentry;
2973 struct osd_thandle *oh;
2974 struct ldiskfs_dir_entry_2 *de;
2975 struct buffer_head *bh;
2976 struct htree_lock *hlock = NULL;
2981 LINVRNT(osd_invariant(obj));
2982 LASSERT(dt_object_exists(dt));
2983 LASSERT(handle != NULL);
2985 OSD_EXEC_OP(handle, delete);
2987 oh = container_of(handle, struct osd_thandle, ot_super);
2988 LASSERT(oh->ot_handle != NULL);
2989 LASSERT(oh->ot_handle->h_transaction != NULL);
2991 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2994 dentry = osd_child_dentry_get(env, obj,
2995 (char *)key, strlen((char *)key));
2997 if (obj->oo_hl_head != NULL) {
2998 hlock = osd_oti_get(env)->oti_hlock;
2999 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3000 dir, LDISKFS_HLOCK_DEL);
3002 cfs_down_write(&obj->oo_ext_idx_sem);
3005 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3007 rc = ldiskfs_delete_entry(oh->ot_handle,
3014 ldiskfs_htree_unlock(hlock);
3016 cfs_up_write(&obj->oo_ext_idx_sem);
3018 LASSERT(osd_invariant(obj));
3023 * Lookup index for \a key and copy record to \a rec.
3025 * \param dt osd index object
3026 * \param key key for index
3027 * \param rec record reference
3029 * \retval +ve success : exact mach
3030 * \retval 0 return record with key not greater than \a key
3031 * \retval -ve failure
3033 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3034 struct dt_rec *rec, const struct dt_key *key,
3035 struct lustre_capa *capa)
3037 struct osd_object *obj = osd_dt_obj(dt);
3038 struct iam_path_descr *ipd;
3039 struct iam_container *bag = &obj->oo_dir->od_container;
3040 struct osd_thread_info *oti = osd_oti_get(env);
3041 struct iam_iterator *it = &oti->oti_idx_it;
3042 struct iam_rec *iam_rec;
3047 LASSERT(osd_invariant(obj));
3048 LASSERT(dt_object_exists(dt));
3049 LASSERT(bag->ic_object == obj->oo_inode);
3051 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3054 ipd = osd_idx_ipd_get(env, bag);
3058 /* got ipd now we can start iterator. */
3059 iam_it_init(it, bag, 0, ipd);
3061 rc = iam_it_get(it, (struct iam_key *)key);
3063 if (S_ISDIR(obj->oo_inode->i_mode))
3064 iam_rec = (struct iam_rec *)oti->oti_ldp;
3066 iam_rec = (struct iam_rec *) rec;
3068 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3069 if (S_ISDIR(obj->oo_inode->i_mode))
3070 osd_fid_unpack((struct lu_fid *) rec,
3071 (struct osd_fid_pack *)iam_rec);
3075 osd_ipd_put(env, bag, ipd);
3077 LINVRNT(osd_invariant(obj));
3082 static int osd_index_declare_iam_insert(const struct lu_env *env,
3083 struct dt_object *dt,
3084 const struct dt_rec *rec,
3085 const struct dt_key *key,
3086 struct thandle *handle)
3088 struct osd_thandle *oh;
3090 LASSERT(dt_object_exists(dt));
3091 LASSERT(handle != NULL);
3093 oh = container_of0(handle, struct osd_thandle, ot_super);
3094 LASSERT(oh->ot_handle == NULL);
3096 OSD_DECLARE_OP(oh, insert);
3097 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3103 * Inserts (key, value) pair in \a dt index object.
3105 * \param dt osd index object
3106 * \param key key for index
3107 * \param rec record reference
3108 * \param th transaction handler
3111 * \retval -ve failure
3113 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3114 const struct dt_rec *rec,
3115 const struct dt_key *key, struct thandle *th,
3116 struct lustre_capa *capa, int ignore_quota)
3118 struct osd_object *obj = osd_dt_obj(dt);
3119 struct iam_path_descr *ipd;
3120 struct osd_thandle *oh;
3121 struct iam_container *bag = &obj->oo_dir->od_container;
3122 #ifdef HAVE_QUOTA_SUPPORT
3123 cfs_cap_t save = cfs_curproc_cap_pack();
3125 struct osd_thread_info *oti = osd_oti_get(env);
3126 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3131 LINVRNT(osd_invariant(obj));
3132 LASSERT(dt_object_exists(dt));
3133 LASSERT(bag->ic_object == obj->oo_inode);
3134 LASSERT(th != NULL);
3136 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3139 OSD_EXEC_OP(th, insert);
3141 ipd = osd_idx_ipd_get(env, bag);
3142 if (unlikely(ipd == NULL))
3145 oh = container_of0(th, struct osd_thandle, ot_super);
3146 LASSERT(oh->ot_handle != NULL);
3147 LASSERT(oh->ot_handle->h_transaction != NULL);
3148 #ifdef HAVE_QUOTA_SUPPORT
3150 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3152 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3154 if (S_ISDIR(obj->oo_inode->i_mode))
3155 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3157 iam_rec = (struct iam_rec *) rec;
3158 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3160 #ifdef HAVE_QUOTA_SUPPORT
3161 cfs_curproc_cap_unpack(save);
3163 osd_ipd_put(env, bag, ipd);
3164 LINVRNT(osd_invariant(obj));
3169 * Calls ldiskfs_add_entry() to add directory entry
3170 * into the directory. This is required for
3171 * interoperability mode (b11826)
3173 * \retval 0, on success
3174 * \retval -ve, on error
3176 static int __osd_ea_add_rec(struct osd_thread_info *info,
3177 struct osd_object *pobj, struct inode *cinode,
3178 const char *name, const struct dt_rec *fid,
3179 struct htree_lock *hlock, struct thandle *th)
3181 struct ldiskfs_dentry_param *ldp;
3182 struct dentry *child;
3183 struct osd_thandle *oth;
3186 oth = container_of(th, struct osd_thandle, ot_super);
3187 LASSERT(oth->ot_handle != NULL);
3188 LASSERT(oth->ot_handle->h_transaction != NULL);
3190 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3192 /* XXX: remove fid_is_igif() check here.
3193 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3194 * it is IGIF now but needs FID in dir entry as well for readdir
3196 * LU-838 should fix that and remove fid_is_igif() check */
3197 if (fid_is_igif((struct lu_fid *)fid) ||
3198 fid_is_norm((struct lu_fid *)fid)) {
3199 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3200 osd_get_ldiskfs_dirent_param(ldp, fid);
3201 child->d_fsdata = (void *)ldp;
3203 child->d_fsdata = NULL;
3205 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3211 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3212 * into the directory.Also sets flags into osd object to
3213 * indicate dot and dotdot are created. This is required for
3214 * interoperability mode (b11826)
3216 * \param dir directory for dot and dotdot fixup.
3217 * \param obj child object for linking
3219 * \retval 0, on success
3220 * \retval -ve, on error
3222 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3223 struct osd_object *dir,
3224 struct inode *parent_dir, const char *name,
3225 const struct dt_rec *dot_fid,
3226 const struct dt_rec *dot_dot_fid,
3229 struct inode *inode = dir->oo_inode;
3230 struct ldiskfs_dentry_param *dot_ldp;
3231 struct ldiskfs_dentry_param *dot_dot_ldp;
3232 struct osd_thandle *oth;
3235 oth = container_of(th, struct osd_thandle, ot_super);
3236 LASSERT(oth->ot_handle->h_transaction != NULL);
3237 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3239 if (strcmp(name, dot) == 0) {
3240 if (dir->oo_compat_dot_created) {
3243 LASSERT(inode == parent_dir);
3244 dir->oo_compat_dot_created = 1;
3247 } else if(strcmp(name, dotdot) == 0) {
3248 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3249 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3251 if (!dir->oo_compat_dot_created)
3253 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3254 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3255 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3260 /* in case of rename, dotdot is already created */
3261 if (dir->oo_compat_dotdot_created) {
3262 return __osd_ea_add_rec(info, dir, parent_dir, name,
3263 dot_dot_fid, NULL, th);
3266 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3267 inode, dot_ldp, dot_dot_ldp);
3269 dir->oo_compat_dotdot_created = 1;
3277 * It will call the appropriate osd_add* function and return the
3278 * value, return by respective functions.
3280 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3281 struct inode *cinode, const char *name,
3282 const struct dt_rec *fid, struct thandle *th)
3284 struct osd_thread_info *info = osd_oti_get(env);
3285 struct htree_lock *hlock;
3288 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3290 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3292 if (hlock != NULL) {
3293 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3296 cfs_down_write(&pobj->oo_ext_idx_sem);
3298 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3299 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3302 if (hlock != NULL) {
3303 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3304 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3306 cfs_down_write(&pobj->oo_ext_idx_sem);
3309 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3313 ldiskfs_htree_unlock(hlock);
3315 cfs_up_write(&pobj->oo_ext_idx_sem);
3321 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3322 struct osd_idmap_cache *oic)
3324 struct osd_scrub *scrub = &dev->od_scrub;
3325 struct lu_fid *fid = &oic->oic_fid;
3326 struct osd_inode_id *id = &oti->oti_id;
3332 rc = osd_oi_lookup(oti, dev, fid, id);
3333 if (rc != 0 && rc != -ENOENT)
3336 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3339 if (thread_is_running(&scrub->os_thread)) {
3340 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3341 /* There is race condition between osd_oi_lookup and OI scrub.
3342 * The OI scrub finished just after osd_oi_lookup() failure.
3343 * Under such case, it is unnecessary to trigger OI scrub again,
3344 * but try to call osd_oi_lookup() again. */
3345 if (unlikely(rc == -EAGAIN))
3351 if (!scrub->os_no_scrub && ++once == 1) {
3352 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3354 rc = osd_scrub_start(dev);
3355 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3357 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3363 RETURN(rc = -EREMCHG);
3367 * Calls ->lookup() to find dentry. From dentry get inode and
3368 * read inode's ea to get fid. This is required for interoperability
3371 * \retval 0, on success
3372 * \retval -ve, on error
3374 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3375 struct dt_rec *rec, const struct dt_key *key)
3377 struct inode *dir = obj->oo_inode;
3378 struct dentry *dentry;
3379 struct ldiskfs_dir_entry_2 *de;
3380 struct buffer_head *bh;
3381 struct lu_fid *fid = (struct lu_fid *) rec;
3382 struct htree_lock *hlock = NULL;
3386 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3388 dentry = osd_child_dentry_get(env, obj,
3389 (char *)key, strlen((char *)key));
3391 if (obj->oo_hl_head != NULL) {
3392 hlock = osd_oti_get(env)->oti_hlock;
3393 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3394 dir, LDISKFS_HLOCK_LOOKUP);
3396 cfs_down_read(&obj->oo_ext_idx_sem);
3399 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3401 struct osd_thread_info *oti = osd_oti_get(env);
3402 struct osd_idmap_cache *oic = &oti->oti_cache;
3403 struct osd_device *dev = osd_obj2dev(obj);
3404 struct osd_scrub *scrub = &dev->od_scrub;
3405 struct scrub_file *sf = &scrub->os_file;
3407 ino = le32_to_cpu(de->inode);
3408 rc = osd_get_fid_from_dentry(de, rec);
3410 /* done with de, release bh */
3413 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3415 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3416 if (rc != 0 || !fid_is_norm(fid)) {
3417 fid_zero(&oic->oic_fid);
3421 oic->oic_fid = *fid;
3422 if ((scrub->os_pos_current <= ino) &&
3423 (sf->sf_flags & SF_INCONSISTENT ||
3424 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3426 rc = osd_consistency_check(oti, dev, oic);
3435 ldiskfs_htree_unlock(hlock);
3437 cfs_up_read(&obj->oo_ext_idx_sem);
3442 * Find the osd object for given fid.
3444 * \param fid need to find the osd object having this fid
3446 * \retval osd_object on success
3447 * \retval -ve on error
3449 struct osd_object *osd_object_find(const struct lu_env *env,
3450 struct dt_object *dt,
3451 const struct lu_fid *fid)
3453 struct lu_device *ludev = dt->do_lu.lo_dev;
3454 struct osd_object *child = NULL;
3455 struct lu_object *luch;
3456 struct lu_object *lo;
3458 luch = lu_object_find(env, ludev, fid, NULL);
3459 if (!IS_ERR(luch)) {
3460 if (lu_object_exists(luch)) {
3461 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3463 child = osd_obj(lo);
3465 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3466 "lu_object can't be located"
3467 DFID"\n", PFID(fid));
3469 if (child == NULL) {
3470 lu_object_put(env, luch);
3471 CERROR("Unable to get osd_object\n");
3472 child = ERR_PTR(-ENOENT);
3475 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3476 "lu_object does not exists "DFID"\n",
3478 lu_object_put(env, luch);
3479 child = ERR_PTR(-ENOENT);
3482 child = (void *)luch;
3488 * Put the osd object once done with it.
3490 * \param obj osd object that needs to be put
3492 static inline void osd_object_put(const struct lu_env *env,
3493 struct osd_object *obj)
3495 lu_object_put(env, &obj->oo_dt.do_lu);
3498 static int osd_index_declare_ea_insert(const struct lu_env *env,
3499 struct dt_object *dt,
3500 const struct dt_rec *rec,
3501 const struct dt_key *key,
3502 struct thandle *handle)
3504 struct osd_thandle *oh;
3506 LASSERT(dt_object_exists(dt));
3507 LASSERT(handle != NULL);
3509 oh = container_of0(handle, struct osd_thandle, ot_super);
3510 LASSERT(oh->ot_handle == NULL);
3512 OSD_DECLARE_OP(oh, insert);
3513 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3515 LASSERT(osd_dt_obj(dt)->oo_inode);
3516 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3517 osd_dt_obj(dt)->oo_inode);
3518 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3519 osd_dt_obj(dt)->oo_inode);
3525 * Index add function for interoperability mode (b11826).
3526 * It will add the directory entry.This entry is needed to
3527 * maintain name->fid mapping.
3529 * \param key it is key i.e. file entry to be inserted
3530 * \param rec it is value of given key i.e. fid
3532 * \retval 0, on success
3533 * \retval -ve, on error
3535 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3536 const struct dt_rec *rec,
3537 const struct dt_key *key, struct thandle *th,
3538 struct lustre_capa *capa, int ignore_quota)
3540 struct osd_object *obj = osd_dt_obj(dt);
3541 struct lu_fid *fid = (struct lu_fid *) rec;
3542 const char *name = (const char *)key;
3543 struct osd_object *child;
3544 #ifdef HAVE_QUOTA_SUPPORT
3545 cfs_cap_t save = cfs_curproc_cap_pack();
3551 LASSERT(osd_invariant(obj));
3552 LASSERT(dt_object_exists(dt));
3553 LASSERT(th != NULL);
3555 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3558 child = osd_object_find(env, dt, fid);
3559 if (!IS_ERR(child)) {
3560 #ifdef HAVE_QUOTA_SUPPORT
3562 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3564 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3566 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3567 #ifdef HAVE_QUOTA_SUPPORT
3568 cfs_curproc_cap_unpack(save);
3570 osd_object_put(env, child);
3572 rc = PTR_ERR(child);
3575 LASSERT(osd_invariant(obj));
3580 * Initialize osd Iterator for given osd index object.
3582 * \param dt osd index object
3585 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3586 struct dt_object *dt,
3588 struct lustre_capa *capa)
3590 struct osd_it_iam *it;
3591 struct osd_thread_info *oti = osd_oti_get(env);
3592 struct osd_object *obj = osd_dt_obj(dt);
3593 struct lu_object *lo = &dt->do_lu;
3594 struct iam_path_descr *ipd;
3595 struct iam_container *bag = &obj->oo_dir->od_container;
3597 LASSERT(lu_object_exists(lo));
3599 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3600 return ERR_PTR(-EACCES);
3603 ipd = osd_it_ipd_get(env, bag);
3604 if (likely(ipd != NULL)) {
3608 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3609 return (struct dt_it *)it;
3611 return ERR_PTR(-ENOMEM);
3615 * free given Iterator.
3618 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3620 struct osd_it_iam *it = (struct osd_it_iam *)di;
3621 struct osd_object *obj = it->oi_obj;
3623 iam_it_fini(&it->oi_it);
3624 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3625 lu_object_put(env, &obj->oo_dt.do_lu);
3629 * Move Iterator to record specified by \a key
3631 * \param di osd iterator
3632 * \param key key for index
3634 * \retval +ve di points to record with least key not larger than key
3635 * \retval 0 di points to exact matched key
3636 * \retval -ve failure
3639 static int osd_it_iam_get(const struct lu_env *env,
3640 struct dt_it *di, const struct dt_key *key)
3642 struct osd_it_iam *it = (struct osd_it_iam *)di;
3644 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3650 * \param di osd iterator
3653 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3655 struct osd_it_iam *it = (struct osd_it_iam *)di;
3657 iam_it_put(&it->oi_it);
3661 * Move iterator by one record
3663 * \param di osd iterator
3665 * \retval +1 end of container reached
3667 * \retval -ve failure
3670 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3672 struct osd_it_iam *it = (struct osd_it_iam *)di;
3674 return iam_it_next(&it->oi_it);
3678 * Return pointer to the key under iterator.
3681 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3682 const struct dt_it *di)
3684 struct osd_it_iam *it = (struct osd_it_iam *)di;
3686 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3690 * Return size of key under iterator (in bytes)
3693 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3695 struct osd_it_iam *it = (struct osd_it_iam *)di;
3697 return iam_it_key_size(&it->oi_it);
3700 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3701 int len, __u16 type)
3703 struct luda_type *lt;
3704 const unsigned align = sizeof(struct luda_type) - 1;
3706 /* check if file type is required */
3707 if (attr & LUDA_TYPE) {
3708 len = (len + align) & ~align;
3710 lt = (void *) ent->lde_name + len;
3711 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3712 ent->lde_attrs |= LUDA_TYPE;
3715 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3719 * build lu direct from backend fs dirent.
3722 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3723 struct lu_fid *fid, __u64 offset,
3724 char *name, __u16 namelen,
3725 __u16 type, __u32 attr)
3727 fid_cpu_to_le(&ent->lde_fid, fid);
3728 ent->lde_attrs = LUDA_FID;
3730 ent->lde_hash = cpu_to_le64(offset);
3731 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3733 strncpy(ent->lde_name, name, namelen);
3734 ent->lde_namelen = cpu_to_le16(namelen);
3736 /* append lustre attributes */
3737 osd_it_append_attrs(ent, attr, namelen, type);
3741 * Return pointer to the record under iterator.
3743 static int osd_it_iam_rec(const struct lu_env *env,
3744 const struct dt_it *di,
3745 struct dt_rec *dtrec, __u32 attr)
3747 struct osd_it_iam *it = (struct osd_it_iam *)di;
3748 struct osd_thread_info *info = osd_oti_get(env);
3751 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3752 const struct osd_fid_pack *rec;
3753 struct lu_fid *fid = &info->oti_fid;
3754 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3760 name = (char *)iam_it_key_get(&it->oi_it);
3762 RETURN(PTR_ERR(name));
3764 namelen = iam_it_key_size(&it->oi_it);
3766 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3768 RETURN(PTR_ERR(rec));
3770 rc = osd_fid_unpack(fid, rec);
3774 hash = iam_it_store(&it->oi_it);
3776 /* IAM does not store object type in IAM index (dir) */
3777 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3780 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3781 (struct iam_rec *)dtrec);
3788 * Returns cookie for current Iterator position.
3790 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3792 struct osd_it_iam *it = (struct osd_it_iam *)di;
3794 return iam_it_store(&it->oi_it);
3798 * Restore iterator from cookie.
3800 * \param di osd iterator
3801 * \param hash Iterator location cookie
3803 * \retval +ve di points to record with least key not larger than key.
3804 * \retval 0 di points to exact matched key
3805 * \retval -ve failure
3808 static int osd_it_iam_load(const struct lu_env *env,
3809 const struct dt_it *di, __u64 hash)
3811 struct osd_it_iam *it = (struct osd_it_iam *)di;
3813 return iam_it_load(&it->oi_it, hash);
3816 static const struct dt_index_operations osd_index_iam_ops = {
3817 .dio_lookup = osd_index_iam_lookup,
3818 .dio_declare_insert = osd_index_declare_iam_insert,
3819 .dio_insert = osd_index_iam_insert,
3820 .dio_declare_delete = osd_index_declare_iam_delete,
3821 .dio_delete = osd_index_iam_delete,
3823 .init = osd_it_iam_init,
3824 .fini = osd_it_iam_fini,
3825 .get = osd_it_iam_get,
3826 .put = osd_it_iam_put,
3827 .next = osd_it_iam_next,
3828 .key = osd_it_iam_key,
3829 .key_size = osd_it_iam_key_size,
3830 .rec = osd_it_iam_rec,
3831 .store = osd_it_iam_store,
3832 .load = osd_it_iam_load
3837 * Creates or initializes iterator context.
3839 * \retval struct osd_it_ea, iterator structure on success
3842 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3843 struct dt_object *dt,
3845 struct lustre_capa *capa)
3847 struct osd_object *obj = osd_dt_obj(dt);
3848 struct osd_thread_info *info = osd_oti_get(env);
3849 struct osd_it_ea *it = &info->oti_it_ea;
3850 struct lu_object *lo = &dt->do_lu;
3851 struct dentry *obj_dentry = &info->oti_it_dentry;
3853 LASSERT(lu_object_exists(lo));
3855 obj_dentry->d_inode = obj->oo_inode;
3856 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3857 obj_dentry->d_name.hash = 0;
3859 it->oie_rd_dirent = 0;
3860 it->oie_it_dirent = 0;
3861 it->oie_dirent = NULL;
3862 it->oie_buf = info->oti_it_ea_buf;
3864 it->oie_file.f_pos = 0;
3865 it->oie_file.f_dentry = obj_dentry;
3866 if (attr & LUDA_64BITHASH)
3867 it->oie_file.f_mode |= FMODE_64BITHASH;
3869 it->oie_file.f_mode |= FMODE_32BITHASH;
3870 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3871 it->oie_file.f_op = obj->oo_inode->i_fop;
3872 it->oie_file.private_data = NULL;
3874 RETURN((struct dt_it *) it);
3878 * Destroy or finishes iterator context.
3880 * \param di iterator structure to be destroyed
3882 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3884 struct osd_it_ea *it = (struct osd_it_ea *)di;
3885 struct osd_object *obj = it->oie_obj;
3886 struct inode *inode = obj->oo_inode;
3889 it->oie_file.f_op->release(inode, &it->oie_file);
3890 lu_object_put(env, &obj->oo_dt.do_lu);
3895 * It position the iterator at given key, so that next lookup continues from
3896 * that key Or it is similar to dio_it->load() but based on a key,
3897 * rather than file position.
3899 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3902 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3904 static int osd_it_ea_get(const struct lu_env *env,
3905 struct dt_it *di, const struct dt_key *key)
3907 struct osd_it_ea *it = (struct osd_it_ea *)di;
3910 LASSERT(((const char *)key)[0] == '\0');
3911 it->oie_file.f_pos = 0;
3912 it->oie_rd_dirent = 0;
3913 it->oie_it_dirent = 0;
3914 it->oie_dirent = NULL;
3922 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3927 * It is called internally by ->readdir(). It fills the
3928 * iterator's in-memory data structure with required
3929 * information i.e. name, namelen, rec_size etc.
3931 * \param buf in which information to be filled in.
3932 * \param name name of the file in given dir
3934 * \retval 0 on success
3935 * \retval 1 on buffer full
3937 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3938 loff_t offset, __u64 ino,
3941 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3942 struct osd_it_ea_dirent *ent = it->oie_dirent;
3943 struct lu_fid *fid = &ent->oied_fid;
3944 struct osd_fid_pack *rec;
3947 /* this should never happen */
3948 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3949 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3953 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3957 if (d_type & LDISKFS_DIRENT_LUFID) {
3958 rec = (struct osd_fid_pack*) (name + namelen + 1);
3960 if (osd_fid_unpack(fid, rec) != 0)
3963 d_type &= ~LDISKFS_DIRENT_LUFID;
3968 ent->oied_ino = ino;
3969 ent->oied_off = offset;
3970 ent->oied_namelen = namelen;
3971 ent->oied_type = d_type;
3973 memcpy(ent->oied_name, name, namelen);
3975 it->oie_rd_dirent++;
3976 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3981 * Calls ->readdir() to load a directory entry at a time
3982 * and stored it in iterator's in-memory data structure.
3984 * \param di iterator's in memory structure
3986 * \retval 0 on success
3987 * \retval -ve on error
3989 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3990 const struct dt_it *di)
3992 struct osd_it_ea *it = (struct osd_it_ea *)di;
3993 struct osd_object *obj = it->oie_obj;
3994 struct inode *inode = obj->oo_inode;
3995 struct htree_lock *hlock = NULL;
3999 it->oie_dirent = it->oie_buf;
4000 it->oie_rd_dirent = 0;
4002 if (obj->oo_hl_head != NULL) {
4003 hlock = osd_oti_get(env)->oti_hlock;
4004 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4005 inode, LDISKFS_HLOCK_READDIR);
4007 cfs_down_read(&obj->oo_ext_idx_sem);
4010 result = inode->i_fop->readdir(&it->oie_file, it,
4011 (filldir_t) osd_ldiskfs_filldir);
4014 ldiskfs_htree_unlock(hlock);
4016 cfs_up_read(&obj->oo_ext_idx_sem);
4018 if (it->oie_rd_dirent == 0) {
4021 it->oie_dirent = it->oie_buf;
4022 it->oie_it_dirent = 1;
4029 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4030 * to load a directory entry at a time and stored it in
4031 * iterator's in-memory data structure.
4033 * \param di iterator's in memory structure
4035 * \retval +ve iterator reached to end
4036 * \retval 0 iterator not reached to end
4037 * \retval -ve on error
4039 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4041 struct osd_it_ea *it = (struct osd_it_ea *)di;
4046 if (it->oie_it_dirent < it->oie_rd_dirent) {
4048 (void *) it->oie_dirent +
4049 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4050 it->oie_dirent->oied_namelen);
4051 it->oie_it_dirent++;
4054 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4057 rc = osd_ldiskfs_it_fill(env, di);
4064 * Returns the key at current position from iterator's in memory structure.
4066 * \param di iterator's in memory structure
4068 * \retval key i.e. struct dt_key on success
4070 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4071 const struct dt_it *di)
4073 struct osd_it_ea *it = (struct osd_it_ea *)di;
4075 return (struct dt_key *)it->oie_dirent->oied_name;
4079 * Returns the key's size at current position from iterator's in memory structure.
4081 * \param di iterator's in memory structure
4083 * \retval key_size i.e. struct dt_key on success
4085 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4087 struct osd_it_ea *it = (struct osd_it_ea *)di;
4089 return it->oie_dirent->oied_namelen;
4094 * Returns the value (i.e. fid/igif) at current position from iterator's
4095 * in memory structure.
4097 * \param di struct osd_it_ea, iterator's in memory structure
4098 * \param attr attr requested for dirent.
4099 * \param lde lustre dirent
4101 * \retval 0 no error and \param lde has correct lustre dirent.
4102 * \retval -ve on error
4104 static inline int osd_it_ea_rec(const struct lu_env *env,
4105 const struct dt_it *di,
4106 struct dt_rec *dtrec, __u32 attr)
4108 struct osd_it_ea *it = (struct osd_it_ea *)di;
4109 struct osd_object *obj = it->oie_obj;
4110 struct osd_device *dev = osd_obj2dev(obj);
4111 struct osd_scrub *scrub = &dev->od_scrub;
4112 struct scrub_file *sf = &scrub->os_file;
4113 struct osd_thread_info *oti = osd_oti_get(env);
4114 struct osd_idmap_cache *oic = &oti->oti_cache;
4115 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4116 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4117 __u32 ino = it->oie_dirent->oied_ino;
4121 if (!fid_is_sane(fid)) {
4122 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4126 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4129 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4130 it->oie_dirent->oied_name,
4131 it->oie_dirent->oied_namelen,
4132 it->oie_dirent->oied_type, attr);
4133 if (!fid_is_norm(fid)) {
4134 fid_zero(&oic->oic_fid);
4138 oic->oic_fid = *fid;
4139 if ((scrub->os_pos_current <= ino) &&
4140 (sf->sf_flags & SF_INCONSISTENT ||
4141 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4142 rc = osd_consistency_check(oti, dev, oic);
4148 * Returns a cookie for current position of the iterator head, so that
4149 * user can use this cookie to load/start the iterator next time.
4151 * \param di iterator's in memory structure
4153 * \retval cookie for current position, on success
4155 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4157 struct osd_it_ea *it = (struct osd_it_ea *)di;
4159 return it->oie_dirent->oied_off;
4163 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4164 * to load a directory entry at a time and stored it i inn,
4165 * in iterator's in-memory data structure.
4167 * \param di struct osd_it_ea, iterator's in memory structure
4169 * \retval +ve on success
4170 * \retval -ve on error
4172 static int osd_it_ea_load(const struct lu_env *env,
4173 const struct dt_it *di, __u64 hash)
4175 struct osd_it_ea *it = (struct osd_it_ea *)di;
4179 it->oie_file.f_pos = hash;
4181 rc = osd_ldiskfs_it_fill(env, di);
4189 * Index lookup function for interoperability mode (b11826).
4191 * \param key, key i.e. file name to be searched
4193 * \retval +ve, on success
4194 * \retval -ve, on error
4196 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4197 struct dt_rec *rec, const struct dt_key *key,
4198 struct lustre_capa *capa)
4200 struct osd_object *obj = osd_dt_obj(dt);
4205 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4206 LINVRNT(osd_invariant(obj));
4208 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4211 rc = osd_ea_lookup_rec(env, obj, rec, key);
4219 * Index and Iterator operations for interoperability
4220 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4222 static const struct dt_index_operations osd_index_ea_ops = {
4223 .dio_lookup = osd_index_ea_lookup,
4224 .dio_declare_insert = osd_index_declare_ea_insert,
4225 .dio_insert = osd_index_ea_insert,
4226 .dio_declare_delete = osd_index_declare_ea_delete,
4227 .dio_delete = osd_index_ea_delete,
4229 .init = osd_it_ea_init,
4230 .fini = osd_it_ea_fini,
4231 .get = osd_it_ea_get,
4232 .put = osd_it_ea_put,
4233 .next = osd_it_ea_next,
4234 .key = osd_it_ea_key,
4235 .key_size = osd_it_ea_key_size,
4236 .rec = osd_it_ea_rec,
4237 .store = osd_it_ea_store,
4238 .load = osd_it_ea_load
4242 static void *osd_key_init(const struct lu_context *ctx,
4243 struct lu_context_key *key)
4245 struct osd_thread_info *info;
4247 OBD_ALLOC_PTR(info);
4249 return ERR_PTR(-ENOMEM);
4251 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4252 if (info->oti_it_ea_buf == NULL)
4255 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4257 info->oti_hlock = ldiskfs_htree_lock_alloc();
4258 if (info->oti_hlock == NULL)
4264 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4267 return ERR_PTR(-ENOMEM);
4270 static void osd_key_fini(const struct lu_context *ctx,
4271 struct lu_context_key *key, void* data)
4273 struct osd_thread_info *info = data;
4275 if (info->oti_hlock != NULL)
4276 ldiskfs_htree_lock_free(info->oti_hlock);
4277 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4281 static void osd_key_exit(const struct lu_context *ctx,
4282 struct lu_context_key *key, void *data)
4284 struct osd_thread_info *info = data;
4286 LASSERT(info->oti_r_locks == 0);
4287 LASSERT(info->oti_w_locks == 0);
4288 LASSERT(info->oti_txns == 0);
4291 /* type constructor/destructor: osd_type_init, osd_type_fini */
4292 LU_TYPE_INIT_FINI(osd, &osd_key);
4294 struct lu_context_key osd_key = {
4295 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4296 .lct_init = osd_key_init,
4297 .lct_fini = osd_key_fini,
4298 .lct_exit = osd_key_exit
4302 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4303 const char *name, struct lu_device *next)
4305 struct osd_device *osd = osd_dev(d);
4307 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4308 return osd_procfs_init(osd, name);
4311 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4315 osd_scrub_cleanup(env, o);
4318 fsfilt_put_ops(o->od_fsops);
4322 /* shutdown quota slave instance associated with the device */
4323 if (o->od_quota_slave != NULL) {
4324 qsd_fini(env, o->od_quota_slave);
4325 o->od_quota_slave = NULL;
4331 static int osd_mount(const struct lu_env *env,
4332 struct osd_device *o, struct lustre_cfg *cfg)
4334 struct lustre_mount_info *lmi;
4335 const char *dev = lustre_cfg_string(cfg, 0);
4336 struct lustre_sb_info *lsi;
4341 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4342 if (o->od_fsops == NULL) {
4343 CERROR("Can't find fsfilt_ldiskfs\n");
4347 if (o->od_mount != NULL) {
4348 CERROR("Already mounted (%s)\n", dev);
4353 lmi = server_get_mount(dev);
4355 CERROR("Cannot get mount info for %s!\n", dev);
4359 LASSERT(lmi != NULL);
4360 /* save lustre_mount_info in dt_device */
4362 o->od_mnt = lmi->lmi_mnt;
4364 lsi = s2lsi(lmi->lmi_sb);
4366 if (lsi->lsi_flags & LDD_F_IAM_DIR) {
4368 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", dev);
4372 if (lsi->lsi_flags & LDD_F_SV_TYPE_OST) {
4373 rc = osd_compat_init(o);
4375 CERROR("%s: can't initialize compats: %d\n", dev, rc);
4381 static struct lu_device *osd_device_fini(const struct lu_env *env,
4382 struct lu_device *d)
4387 osd_compat_fini(osd_dev(d));
4389 shrink_dcache_sb(osd_sb(osd_dev(d)));
4390 osd_sync(env, lu2dt_dev(d));
4392 rc = osd_procfs_fini(osd_dev(d));
4394 CERROR("proc fini error %d \n", rc);
4395 RETURN (ERR_PTR(rc));
4398 if (osd_dev(d)->od_mount)
4399 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4400 osd_dev(d)->od_mount->lmi_mnt);
4401 osd_dev(d)->od_mount = NULL;
4406 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4407 struct lu_device_type *t,
4408 struct lustre_cfg *cfg)
4410 struct lu_device *l;
4411 struct osd_device *o;
4417 result = dt_device_init(&o->od_dt_dev, t);
4420 l->ld_ops = &osd_lu_ops;
4421 o->od_dt_dev.dd_ops = &osd_dt_ops;
4422 cfs_spin_lock_init(&o->od_osfs_lock);
4423 cfs_mutex_init(&o->od_otable_mutex);
4424 o->od_osfs_age = cfs_time_shift_64(-1000);
4425 o->od_capa_hash = init_capa_hash();
4426 if (o->od_capa_hash == NULL) {
4427 dt_device_fini(&o->od_dt_dev);
4428 l = ERR_PTR(-ENOMEM);
4431 l = ERR_PTR(result);
4436 l = ERR_PTR(-ENOMEM);
4440 static struct lu_device *osd_device_free(const struct lu_env *env,
4441 struct lu_device *d)
4443 struct osd_device *o = osd_dev(d);
4446 cleanup_capa_hash(o->od_capa_hash);
4447 dt_device_fini(&o->od_dt_dev);
4452 static int osd_process_config(const struct lu_env *env,
4453 struct lu_device *d, struct lustre_cfg *cfg)
4455 struct osd_device *o = osd_dev(d);
4459 switch(cfg->lcfg_command) {
4461 err = osd_mount(env, o, cfg);
4464 lu_dev_del_linkage(d->ld_site, d);
4465 err = osd_shutdown(env, o);
4474 static int osd_recovery_complete(const struct lu_env *env,
4475 struct lu_device *d)
4480 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4481 struct lu_device *dev)
4483 struct osd_device *osd = osd_dev(dev);
4487 /* 1. setup scrub, including OI files initialization */
4488 result = osd_scrub_setup(env, osd);
4492 /* 2. setup quota slave instance */
4493 osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
4494 osd->od_proc_entry);
4495 if (IS_ERR(osd->od_quota_slave)) {
4496 result = PTR_ERR(osd->od_quota_slave);
4497 osd->od_quota_slave = NULL;
4501 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 55, 0)
4502 /* Unfortunately, the current MDD implementation relies on some specific
4503 * code to be executed in the OSD layer. Since OFD now also uses the OSD
4504 * module, we need a way to skip the metadata-specific code when running
4506 * The hack here is to check the type of the parent device which is
4507 * either MD (i.e. MDD device) with the current MDT stack or DT (i.e.
4508 * OFD device) on an OST. As a reminder, obdfilter does not use the OSD
4509 * layer and still relies on lvfs. This hack won't work any more when
4510 * LOD is landed since LOD is of DT type.
4511 * This code should be removed once the orion MDT changes (LOD/OSP, ...)
4512 * have been landed */
4513 osd->od_is_md = lu_device_is_md(pdev);
4515 #warning "all is_md checks must be removed from osd-ldiskfs"
4521 /* 3. setup local objects */
4522 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4526 static const struct lu_object_operations osd_lu_obj_ops = {
4527 .loo_object_init = osd_object_init,
4528 .loo_object_delete = osd_object_delete,
4529 .loo_object_release = osd_object_release,
4530 .loo_object_free = osd_object_free,
4531 .loo_object_print = osd_object_print,
4532 .loo_object_invariant = osd_object_invariant
4535 const struct lu_device_operations osd_lu_ops = {
4536 .ldo_object_alloc = osd_object_alloc,
4537 .ldo_process_config = osd_process_config,
4538 .ldo_recovery_complete = osd_recovery_complete,
4539 .ldo_prepare = osd_prepare,
4542 static const struct lu_device_type_operations osd_device_type_ops = {
4543 .ldto_init = osd_type_init,
4544 .ldto_fini = osd_type_fini,
4546 .ldto_start = osd_type_start,
4547 .ldto_stop = osd_type_stop,
4549 .ldto_device_alloc = osd_device_alloc,
4550 .ldto_device_free = osd_device_free,
4552 .ldto_device_init = osd_device_init,
4553 .ldto_device_fini = osd_device_fini
4556 static struct lu_device_type osd_device_type = {
4557 .ldt_tags = LU_DEVICE_DT,
4558 .ldt_name = LUSTRE_OSD_NAME,
4559 .ldt_ops = &osd_device_type_ops,
4560 .ldt_ctx_tags = LCT_LOCAL,
4564 * lprocfs legacy support.
4566 static struct obd_ops osd_obd_device_ops = {
4567 .o_owner = THIS_MODULE
4570 static int __init osd_mod_init(void)
4572 struct lprocfs_static_vars lvars;
4575 lprocfs_osd_init_vars(&lvars);
4576 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4577 LUSTRE_OSD_NAME, &osd_device_type);
4580 static void __exit osd_mod_exit(void)
4582 class_unregister_type(LUSTRE_OSD_NAME);
4585 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4586 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4587 MODULE_LICENSE("GPL");
4589 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);