4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
60 * struct OBD_{ALLOC,FREE}*()
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
68 #include <lustre_fid.h>
70 #include "osd_internal.h"
73 /* llo_* api support */
74 #include <md_object.h>
75 /* dt_acct_features */
78 #ifdef HAVE_LDISKFS_PDO
80 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
81 "ldiskfs with parallel directory operations");
86 static const char dot[] = ".";
87 static const char dotdot[] = "..";
88 static const char remote_obj_dir[] = "REM_OBJ_DIR";
90 static const struct lu_object_operations osd_lu_obj_ops;
91 static const struct dt_object_operations osd_obj_ops;
92 static const struct dt_object_operations osd_obj_ea_ops;
93 static const struct dt_object_operations osd_obj_otable_it_ops;
94 static const struct dt_index_operations osd_index_iam_ops;
95 static const struct dt_index_operations osd_index_ea_ops;
97 static int osd_has_index(const struct osd_object *obj)
99 return obj->oo_dt.do_index_ops != NULL;
102 static int osd_object_invariant(const struct lu_object *l)
104 return osd_invariant(osd_obj(l));
107 #ifdef HAVE_QUOTA_SUPPORT
109 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save, bool is_md)
122 save->oc_uid = current_fsuid();
123 save->oc_gid = current_fsgid();
124 save->oc_cap = current_cap();
125 if ((tc = prepare_creds())) {
126 tc->fsuid = uc->mu_fsuid;
127 tc->fsgid = uc->mu_fsgid;
130 /* XXX not suboptimal */
131 cfs_curproc_cap_unpack(uc->mu_cap);
135 osd_pop_ctxt(struct osd_ctxt *save, bool is_md)
143 if ((tc = prepare_creds())) {
144 tc->fsuid = save->oc_uid;
145 tc->fsgid = save->oc_gid;
146 tc->cap_effective = save->oc_cap;
153 * Concurrency: doesn't matter
155 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
157 return osd_oti_get(env)->oti_r_locks > 0;
161 * Concurrency: doesn't matter
163 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
165 struct osd_thread_info *oti = osd_oti_get(env);
166 return oti->oti_w_locks > 0 && o->oo_owner == env;
170 * Concurrency: doesn't access mutable data
172 static int osd_root_get(const struct lu_env *env,
173 struct dt_device *dev, struct lu_fid *f)
175 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
179 static inline int osd_qid_type(struct osd_thandle *oh, int i)
181 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
184 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
186 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
189 void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
190 int type, uid_t id, struct inode *inode)
193 int i, allocated = 0;
194 struct osd_object *obj;
198 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
201 /* id entry is allocated in the quota file */
202 if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
205 for (i = 0; i < oh->ot_id_cnt; i++) {
206 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
210 if (unlikely(i >= OSD_MAX_UGID_CNT)) {
211 CERROR("more than %d uid/gids for a transaction?\n", i);
215 oh->ot_id_array[i] = id;
216 osd_qid_set_type(oh, i, type);
218 obj = osd_dt_obj(dt);
219 oh->ot_credits += (allocated || id == 0) ?
220 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
225 * OSD object methods.
229 * Concurrency: no concurrent access is possible that early in object
232 static struct lu_object *osd_object_alloc(const struct lu_env *env,
233 const struct lu_object_header *hdr,
236 struct osd_object *mo;
242 l = &mo->oo_dt.do_lu;
243 dt_object_init(&mo->oo_dt, NULL, d);
244 if (osd_dev(d)->od_iop_mode)
245 mo->oo_dt.do_ops = &osd_obj_ea_ops;
247 mo->oo_dt.do_ops = &osd_obj_ops;
249 l->lo_ops = &osd_lu_obj_ops;
250 cfs_init_rwsem(&mo->oo_sem);
251 cfs_init_rwsem(&mo->oo_ext_idx_sem);
252 cfs_spin_lock_init(&mo->oo_guard);
259 static int osd_get_lma(struct inode *inode, struct dentry *dentry,
260 struct lustre_mdt_attrs *lma)
264 dentry->d_inode = inode;
265 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
268 /* Check LMA compatibility */
269 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
270 CWARN("%.16s: unsupported incompat LMA feature(s) "
272 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
273 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
277 lustre_lma_swab(lma);
280 } else if (rc == 0) {
288 * retrieve object from backend ext fs.
290 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
291 struct osd_inode_id *id)
293 struct inode *inode = NULL;
295 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
297 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
298 id->oii_ino, PTR_ERR(inode));
299 } else if (id->oii_gen != OSD_OII_NOGEN &&
300 inode->i_generation != id->oii_gen) {
301 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
303 id->oii_ino, id->oii_gen, inode->i_generation);
305 inode = ERR_PTR(-ESTALE);
306 } else if (inode->i_nlink == 0) {
307 /* due to parallel readdir and unlink,
308 * we can have dead inode here. */
309 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
310 make_bad_inode(inode);
312 inode = ERR_PTR(-ESTALE);
313 } else if (is_bad_inode(inode)) {
314 CWARN("%.16s: bad inode: ino = %u\n",
315 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
317 inode = ERR_PTR(-ENOENT);
319 if (id->oii_gen == OSD_OII_NOGEN)
320 osd_id_gen(id, inode->i_ino, inode->i_generation);
322 /* Do not update file c/mtime in ldiskfs.
323 * NB: we don't have any lock to protect this because we don't
324 * have reference on osd_object now, but contention with
325 * another lookup + attr_set can't happen in the tiny window
326 * between if (...) and set S_NOCMTIME. */
327 if (!(inode->i_flags & S_NOCMTIME))
328 inode->i_flags |= S_NOCMTIME;
333 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
334 struct osd_inode_id *id, struct lu_fid *fid)
336 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
340 inode = osd_iget(info, dev, id);
344 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
346 *fid = lma->lma_self_fid;
347 } else if (rc == -ENODATA) {
348 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
356 static struct inode *
357 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
358 struct osd_inode_id *id, const struct lu_fid *fid)
360 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
364 inode = osd_iget(info, dev, id);
368 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
370 if (rc == -ENODATA) {
371 CDEBUG(D_LFSCK, "inconsistent obj: NULL, %lu, "DFID"\n",
372 inode->i_ino, PFID(fid));
379 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
380 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
381 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
383 return ERR_PTR(EREMCHG);
388 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
389 const struct lu_fid *fid,
390 const struct lu_object_conf *conf)
392 struct osd_thread_info *info;
393 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
394 struct osd_device *dev;
395 struct osd_idmap_cache *oic;
396 struct osd_inode_id *id;
398 struct osd_scrub *scrub;
399 struct scrub_file *sf;
404 LINVRNT(osd_invariant(obj));
405 LASSERT(obj->oo_inode == NULL);
406 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
409 scrub = &dev->od_scrub;
410 sf = &scrub->os_file;
411 info = osd_oti_get(env);
413 oic = &info->oti_cache;
416 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
419 if (fid_is_norm(fid)) {
420 /* Search order: 1. per-thread cache. */
421 if (lu_fid_eq(fid, &oic->oic_fid)) {
423 } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
424 /* Search order: 2. OI scrub pending list. */
425 result = osd_oii_lookup(dev, fid, id);
430 if (sf->sf_flags & SF_INCONSISTENT)
435 * Objects are created as locking anchors or place holders for objects
436 * yet to be created. No need to osd_oi_lookup() at here because FID
437 * shouldn't never be re-used, if it's really a duplicate FID from
438 * unexpected reason, we should be able to detect it later by calling
439 * do_create->osd_oi_insert()
441 if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
442 GOTO(out, result = 0);
444 /* Search order: 3. OI files. */
445 result = osd_oi_lookup(info, dev, fid, id);
446 if (result == -ENOENT) {
447 if (!fid_is_norm(fid) ||
448 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
450 GOTO(out, result = 0);
460 inode = osd_iget(info, dev, id);
462 inode = osd_iget_verify(info, dev, id, fid);
464 result = PTR_ERR(inode);
465 if (result == -ENOENT || result == -ESTALE) {
466 fid_zero(&oic->oic_fid);
468 } else if (result == -EREMCHG) {
471 if (thread_is_running(&scrub->os_thread)) {
472 result = -EINPROGRESS;
473 } else if (!scrub->os_no_scrub) {
474 result = osd_scrub_start(dev);
475 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
476 "for "DFID", rc = %d [1]\n",
477 LDISKFS_SB(osd_sb(dev))->s_es->\
478 s_volume_name,PFID(fid), result);
479 if (result == 0 || result == -EALREADY)
480 result = -EINPROGRESS;
489 obj->oo_inode = inode;
490 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
491 if (dev->od_iop_mode) {
492 obj->oo_compat_dot_created = 1;
493 obj->oo_compat_dotdot_created = 1;
496 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
497 GOTO(out, result = 0);
499 LASSERT(obj->oo_hl_head == NULL);
500 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
501 if (obj->oo_hl_head == NULL) {
502 obj->oo_inode = NULL;
504 GOTO(out, result = -ENOMEM);
506 GOTO(out, result = 0);
509 LINVRNT(osd_invariant(obj));
514 * Concurrency: shouldn't matter.
516 static void osd_object_init0(struct osd_object *obj)
518 LASSERT(obj->oo_inode != NULL);
519 obj->oo_dt.do_body_ops = &osd_body_ops;
520 obj->oo_dt.do_lu.lo_header->loh_attr |=
521 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
525 * Concurrency: no concurrent access is possible that early in object
528 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
529 const struct lu_object_conf *conf)
531 struct osd_object *obj = osd_obj(l);
534 LINVRNT(osd_invariant(obj));
536 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
537 obj->oo_dt.do_body_ops = &osd_body_ops_new;
539 if (obj->oo_inode != NULL) {
540 osd_object_init0(obj);
541 } else if (fid_is_otable_it(&l->lo_header->loh_fid)) {
542 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
543 /* LFSCK iterator object is special without inode */
544 l->lo_header->loh_attr |= LOHA_EXISTS;
547 LINVRNT(osd_invariant(obj));
552 * Concurrency: no concurrent access is possible that late in object
555 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
557 struct osd_object *obj = osd_obj(l);
559 LINVRNT(osd_invariant(obj));
561 dt_object_fini(&obj->oo_dt);
562 if (obj->oo_hl_head != NULL)
563 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
568 * Concurrency: no concurrent access is possible that late in object
571 static void osd_index_fini(struct osd_object *o)
573 struct iam_container *bag;
575 if (o->oo_dir != NULL) {
576 bag = &o->oo_dir->od_container;
577 if (o->oo_inode != NULL) {
578 if (bag->ic_object == o->oo_inode)
579 iam_container_fini(bag);
581 OBD_FREE_PTR(o->oo_dir);
587 * Concurrency: no concurrent access is possible that late in object
588 * life-cycle (for all existing callers, that is. New callers have to provide
589 * their own locking.)
591 static int osd_inode_unlinked(const struct inode *inode)
593 return inode->i_nlink == 0;
597 OSD_TXN_OI_DELETE_CREDITS = 20,
598 OSD_TXN_INODE_DELETE_CREDITS = 20
605 #if OSD_THANDLE_STATS
607 * Set time when the handle is allocated
609 static void osd_th_alloced(struct osd_thandle *oth)
611 oth->oth_alloced = cfs_time_current();
615 * Set time when the handle started
617 static void osd_th_started(struct osd_thandle *oth)
619 oth->oth_started = cfs_time_current();
623 * Helper function to convert time interval to microseconds packed in
624 * long int (default time units for the counter in "stats" initialized
625 * by lu_time_init() )
627 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
631 cfs_duration_usec(cfs_time_sub(end, start), &val);
632 return val.tv_sec * 1000000 + val.tv_usec;
636 * Check whether the we deal with this handle for too long.
638 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
639 cfs_time_t alloced, cfs_time_t started,
642 cfs_time_t now = cfs_time_current();
644 LASSERT(dev != NULL);
646 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
647 interval_to_usec(alloced, started));
648 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
649 interval_to_usec(started, closed));
650 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
651 interval_to_usec(closed, now));
653 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
654 CWARN("transaction handle %p was open for too long: "
656 "alloced "CFS_TIME_T" ,"
657 "started "CFS_TIME_T" ,"
658 "closed "CFS_TIME_T"\n",
659 oth, now, alloced, started, closed);
660 libcfs_debug_dumpstack(NULL);
664 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
666 cfs_time_t __closed = cfs_time_current(); \
667 cfs_time_t __alloced = oth->oth_alloced; \
668 cfs_time_t __started = oth->oth_started; \
671 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
674 #else /* OSD_THANDLE_STATS */
676 #define osd_th_alloced(h) do {} while(0)
677 #define osd_th_started(h) do {} while(0)
678 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
680 #endif /* OSD_THANDLE_STATS */
683 * Concurrency: doesn't access mutable data.
685 static int osd_param_is_sane(const struct osd_device *dev,
686 const struct thandle *th)
688 struct osd_thandle *oh;
689 oh = container_of0(th, struct osd_thandle, ot_super);
690 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
694 * Concurrency: shouldn't matter.
696 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
697 static void osd_trans_commit_cb(struct super_block *sb,
698 struct journal_callback *jcb, int error)
700 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
703 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
704 struct thandle *th = &oh->ot_super;
705 struct lu_device *lud = &th->th_dev->dd_lu_dev;
706 struct dt_txn_commit_cb *dcb, *tmp;
708 LASSERT(oh->ot_handle == NULL);
711 CERROR("transaction @0x%p commit error: %d\n", th, error);
713 dt_txn_hook_commit(th);
715 /* call per-transaction callbacks if any */
716 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
717 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
718 "commit callback entry: magic=%x name='%s'\n",
719 dcb->dcb_magic, dcb->dcb_name);
720 cfs_list_del_init(&dcb->dcb_linkage);
721 dcb->dcb_func(NULL, th, dcb, error);
724 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
728 lu_context_exit(&th->th_ctx);
729 lu_context_fini(&th->th_ctx);
733 static struct thandle *osd_trans_create(const struct lu_env *env,
736 struct osd_thread_info *oti = osd_oti_get(env);
737 struct osd_iobuf *iobuf = &oti->oti_iobuf;
738 struct osd_thandle *oh;
742 /* on pending IO in this thread should left from prev. request */
743 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
745 th = ERR_PTR(-ENOMEM);
746 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
751 th->th_tags = LCT_TX_HANDLE;
753 oti->oti_dev = osd_dt_dev(d);
754 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
761 * Concurrency: shouldn't matter.
763 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
766 struct osd_thread_info *oti = osd_oti_get(env);
767 struct osd_device *dev = osd_dt_dev(d);
769 struct osd_thandle *oh;
774 LASSERT(current->journal_info == NULL);
776 oh = container_of0(th, struct osd_thandle, ot_super);
778 LASSERT(oh->ot_handle == NULL);
780 rc = dt_txn_hook_start(env, d, th);
784 if (!osd_param_is_sane(dev, th)) {
785 CWARN("%.16s: too many transaction credits (%d > %d)\n",
786 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
788 osd_journal(dev)->j_max_transaction_buffers);
789 /* XXX Limit the credits to 'max_transaction_buffers', and
790 * let the underlying filesystem to catch the error if
791 * we really need so many credits.
793 * This should be removed when we can calculate the
794 * credits precisely. */
795 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
796 #ifdef OSD_TRACK_DECLARES
797 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
798 oh->ot_declare_attr_set, oh->ot_declare_punch,
799 oh->ot_declare_xattr_set);
800 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
801 oh->ot_declare_create, oh->ot_declare_ref_add,
802 oh->ot_declare_ref_del, oh->ot_declare_write);
803 CERROR(" insert: %d, delete: %d, destroy: %d\n",
804 oh->ot_declare_insert, oh->ot_declare_delete,
805 oh->ot_declare_destroy);
810 * XXX temporary stuff. Some abstraction layer should
813 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
817 LASSERT(oti->oti_txns == 0);
818 lu_context_init(&th->th_ctx, th->th_tags);
819 lu_context_enter(&th->th_ctx);
821 lu_device_get(&d->dd_lu_dev);
822 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
826 * XXX: current rule is that we first start tx,
827 * then lock object(s), but we can't use
828 * this rule for data (due to locking specifics
829 * in ldiskfs). also in long-term we'd like to
830 * use usually-used (locks;tx) ordering. so,
831 * UGLY thing is that we'll use one ordering for
832 * data (ofd) and reverse ordering for metadata
833 * (mdd). then at some point we'll fix the latter
836 LASSERT(oti->oti_r_locks == 0);
837 LASSERT(oti->oti_w_locks == 0);
850 * Concurrency: shouldn't matter.
852 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
855 struct osd_thandle *oh;
856 struct osd_thread_info *oti = osd_oti_get(env);
857 struct osd_iobuf *iobuf = &oti->oti_iobuf;
861 oh = container_of0(th, struct osd_thandle, ot_super);
863 if (oh->ot_handle != NULL) {
864 handle_t *hdl = oh->ot_handle;
866 hdl->h_sync = th->th_sync;
869 * add commit callback
870 * notice we don't do this in osd_trans_start()
871 * as underlying transaction can change during truncate
873 osd_journal_callback_set(hdl, osd_trans_commit_cb,
876 LASSERT(oti->oti_txns == 1);
879 * XXX: current rule is that we first start tx,
880 * then lock object(s), but we can't use
881 * this rule for data (due to locking specifics
882 * in ldiskfs). also in long-term we'd like to
883 * use usually-used (locks;tx) ordering. so,
884 * UGLY thing is that we'll use one ordering for
885 * data (ofd) and reverse ordering for metadata
886 * (mdd). then at some point we'll fix the latter
888 if (osd_dt_dev(th->th_dev)->od_is_md) {
889 LASSERT(oti->oti_r_locks == 0);
890 LASSERT(oti->oti_w_locks == 0);
892 rc = dt_txn_hook_stop(env, th);
894 CERROR("Failure in transaction hook: %d\n", rc);
895 oh->ot_handle = NULL;
896 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
897 rc = ldiskfs_journal_stop(hdl));
899 CERROR("Failure to stop transaction: %d\n", rc);
904 /* as we want IO to journal and data IO be concurrent, we don't block
905 * awaiting data IO completion in osd_do_bio(), instead we wait here
906 * once transaction is submitted to the journal. all reqular requests
907 * don't do direct IO (except read/write), thus this wait_event becomes
910 * IMPORTANT: we have to wait till any IO submited by the thread is
911 * completed otherwise iobuf may be corrupted by different request
913 cfs_wait_event(iobuf->dr_wait,
914 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
916 rc = iobuf->dr_error;
921 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
923 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
926 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
927 LASSERT(&dcb->dcb_func != NULL);
928 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
934 * Called just before object is freed. Releases all resources except for
935 * object itself (that is released by osd_object_free()).
937 * Concurrency: no concurrent access is possible that late in object
940 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
942 struct osd_object *obj = osd_obj(l);
943 struct inode *inode = obj->oo_inode;
945 LINVRNT(osd_invariant(obj));
948 * If object is unlinked remove fid->ino mapping from object index.
954 obj->oo_inode = NULL;
959 * Concurrency: ->loo_object_release() is called under site spin-lock.
961 static void osd_object_release(const struct lu_env *env,
967 * Concurrency: shouldn't matter.
969 static int osd_object_print(const struct lu_env *env, void *cookie,
970 lu_printer_t p, const struct lu_object *l)
972 struct osd_object *o = osd_obj(l);
975 if (o->oo_dir != NULL)
976 d = o->oo_dir->od_container.ic_descr;
979 return (*p)(env, cookie,
980 LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
982 o->oo_inode ? o->oo_inode->i_ino : 0UL,
983 o->oo_inode ? o->oo_inode->i_generation : 0,
984 d ? d->id_ops->id_name : "plain");
988 * Concurrency: shouldn't matter.
990 int osd_statfs(const struct lu_env *env, struct dt_device *d,
991 struct obd_statfs *sfs)
993 struct osd_device *osd = osd_dt_dev(d);
994 struct super_block *sb = osd_sb(osd);
995 struct kstatfs *ksfs;
998 if (unlikely(osd->od_mnt == NULL))
1001 /* osd_lproc.c call this without env, allocate ksfs for that case */
1002 if (unlikely(env == NULL)) {
1003 OBD_ALLOC_PTR(ksfs);
1007 ksfs = &osd_oti_get(env)->oti_ksfs;
1010 cfs_spin_lock(&osd->od_osfs_lock);
1011 /* cache 1 second */
1012 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1013 result = sb->s_op->statfs(sb->s_root, ksfs);
1014 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1015 osd->od_osfs_age = cfs_time_current_64();
1016 statfs_pack(&osd->od_statfs, ksfs);
1020 if (likely(result == 0))
1021 *sfs = osd->od_statfs;
1022 cfs_spin_unlock(&osd->od_osfs_lock);
1024 if (unlikely(env == NULL))
1031 * Concurrency: doesn't access mutable data.
1033 static void osd_conf_get(const struct lu_env *env,
1034 const struct dt_device *dev,
1035 struct dt_device_param *param)
1037 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1040 * XXX should be taken from not-yet-existing fs abstraction layer.
1042 param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1043 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1044 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1045 param->ddp_block_shift = sb->s_blocksize_bits;
1046 param->ddp_mntopts = 0;
1047 if (test_opt(sb, XATTR_USER))
1048 param->ddp_mntopts |= MNTOPT_USERXATTR;
1049 if (test_opt(sb, POSIX_ACL))
1050 param->ddp_mntopts |= MNTOPT_ACL;
1052 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1053 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1054 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1057 param->ddp_max_ea_size = sb->s_blocksize;
1062 * Helper function to get and fill the buffer with input values.
1064 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1068 buf = &osd_oti_get(env)->oti_buf;
1075 * Concurrency: shouldn't matter.
1077 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1079 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1080 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1084 * Start commit for OSD device.
1086 * An implementation of dt_commit_async method for OSD device.
1087 * Asychronously starts underlayng fs sync and thereby a transaction
1090 * \param env environment
1091 * \param d dt device
1093 * \see dt_device_operations
1095 static int osd_commit_async(const struct lu_env *env,
1096 struct dt_device *d)
1098 struct super_block *s = osd_sb(osd_dt_dev(d));
1101 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1102 RETURN(s->s_op->sync_fs(s, 0));
1106 * Concurrency: shouldn't matter.
1109 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1111 struct super_block *sb = osd_sb(osd_dt_dev(d));
1115 CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1117 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1122 * Concurrency: serialization provided by callers.
1124 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1125 int mode, unsigned long timeout, __u32 alg,
1126 struct lustre_capa_key *keys)
1128 struct osd_device *dev = osd_dt_dev(d);
1131 dev->od_fl_capa = mode;
1132 dev->od_capa_timeout = timeout;
1133 dev->od_capa_alg = alg;
1134 dev->od_capa_keys = keys;
1139 * Concurrency: serialization provided by callers.
1141 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1142 struct dt_quota_ctxt *ctxt, void *data)
1144 struct obd_device *obd = (void *)ctxt;
1145 struct vfsmount *mnt = (struct vfsmount *)data;
1148 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1149 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1150 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1151 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1152 obd->obd_lvfs_ctxt.fs = get_ds();
1158 * Note: we do not count into QUOTA here.
1159 * If we mount with --data_journal we may need more.
1161 const int osd_dto_credits_noquota[DTO_NR] = {
1164 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1165 * SINGLEDATA_TRANS_BLOCKS(8)
1166 * XXX Note: maybe iam need more, since iam have more level than
1169 [DTO_INDEX_INSERT] = 16,
1170 [DTO_INDEX_DELETE] = 16,
1174 [DTO_INDEX_UPDATE] = 16,
1176 * Create a object. The same as create object in EXT3.
1177 * DATA_TRANS_BLOCKS(14) +
1178 * INDEX_EXTRA_BLOCKS(8) +
1179 * 3(inode bits, groups, GDT)
1181 [DTO_OBJECT_CREATE] = 25,
1183 * XXX: real credits to be fixed
1185 [DTO_OBJECT_DELETE] = 25,
1187 * Attr set credits (inode)
1189 [DTO_ATTR_SET_BASE] = 1,
1191 * Xattr set. The same as xattr of EXT3.
1192 * DATA_TRANS_BLOCKS(14)
1193 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1194 * are also counted in. Do not know why?
1196 [DTO_XATTR_SET] = 14,
1199 * credits for inode change during write.
1201 [DTO_WRITE_BASE] = 3,
1203 * credits for single block write.
1205 [DTO_WRITE_BLOCK] = 14,
1207 * Attr set credits for chown.
1208 * This is extra credits for setattr, and it is null without quota
1210 [DTO_ATTR_SET_CHOWN]= 0
1213 static const struct dt_device_operations osd_dt_ops = {
1214 .dt_root_get = osd_root_get,
1215 .dt_statfs = osd_statfs,
1216 .dt_trans_create = osd_trans_create,
1217 .dt_trans_start = osd_trans_start,
1218 .dt_trans_stop = osd_trans_stop,
1219 .dt_trans_cb_add = osd_trans_cb_add,
1220 .dt_conf_get = osd_conf_get,
1221 .dt_sync = osd_sync,
1223 .dt_commit_async = osd_commit_async,
1224 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1225 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1228 static void osd_object_read_lock(const struct lu_env *env,
1229 struct dt_object *dt, unsigned role)
1231 struct osd_object *obj = osd_dt_obj(dt);
1232 struct osd_thread_info *oti = osd_oti_get(env);
1234 LINVRNT(osd_invariant(obj));
1236 LASSERT(obj->oo_owner != env);
1237 cfs_down_read_nested(&obj->oo_sem, role);
1239 LASSERT(obj->oo_owner == NULL);
1243 static void osd_object_write_lock(const struct lu_env *env,
1244 struct dt_object *dt, unsigned role)
1246 struct osd_object *obj = osd_dt_obj(dt);
1247 struct osd_thread_info *oti = osd_oti_get(env);
1249 LINVRNT(osd_invariant(obj));
1251 LASSERT(obj->oo_owner != env);
1252 cfs_down_write_nested(&obj->oo_sem, role);
1254 LASSERT(obj->oo_owner == NULL);
1255 obj->oo_owner = env;
1259 static void osd_object_read_unlock(const struct lu_env *env,
1260 struct dt_object *dt)
1262 struct osd_object *obj = osd_dt_obj(dt);
1263 struct osd_thread_info *oti = osd_oti_get(env);
1265 LINVRNT(osd_invariant(obj));
1267 LASSERT(oti->oti_r_locks > 0);
1269 cfs_up_read(&obj->oo_sem);
1272 static void osd_object_write_unlock(const struct lu_env *env,
1273 struct dt_object *dt)
1275 struct osd_object *obj = osd_dt_obj(dt);
1276 struct osd_thread_info *oti = osd_oti_get(env);
1278 LINVRNT(osd_invariant(obj));
1280 LASSERT(obj->oo_owner == env);
1281 LASSERT(oti->oti_w_locks > 0);
1283 obj->oo_owner = NULL;
1284 cfs_up_write(&obj->oo_sem);
1287 static int osd_object_write_locked(const struct lu_env *env,
1288 struct dt_object *dt)
1290 struct osd_object *obj = osd_dt_obj(dt);
1292 LINVRNT(osd_invariant(obj));
1294 return obj->oo_owner == env;
1297 static int capa_is_sane(const struct lu_env *env,
1298 struct osd_device *dev,
1299 struct lustre_capa *capa,
1300 struct lustre_capa_key *keys)
1302 struct osd_thread_info *oti = osd_oti_get(env);
1303 struct lustre_capa *tcapa = &oti->oti_capa;
1304 struct obd_capa *oc;
1308 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1310 if (capa_is_expired(oc)) {
1311 DEBUG_CAPA(D_ERROR, capa, "expired");
1318 if (capa_is_expired_sec(capa)) {
1319 DEBUG_CAPA(D_ERROR, capa, "expired");
1323 cfs_spin_lock(&capa_lock);
1324 for (i = 0; i < 2; i++) {
1325 if (keys[i].lk_keyid == capa->lc_keyid) {
1326 oti->oti_capa_key = keys[i];
1330 cfs_spin_unlock(&capa_lock);
1333 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1337 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1341 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1342 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1346 oc = capa_add(dev->od_capa_hash, capa);
1352 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1353 struct lustre_capa *capa, __u64 opc)
1355 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1356 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1357 struct md_capainfo *ci;
1360 if (!dev->od_fl_capa)
1363 if (capa == BYPASS_CAPA)
1366 ci = md_capainfo(env);
1370 if (ci->mc_auth == LC_ID_NONE)
1374 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1378 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1379 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1384 if (!capa_opc_supported(capa, opc)) {
1385 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1389 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1390 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1397 static struct timespec *osd_inode_time(const struct lu_env *env,
1398 struct inode *inode, __u64 seconds)
1400 struct osd_thread_info *oti = osd_oti_get(env);
1401 struct timespec *t = &oti->oti_time;
1403 t->tv_sec = seconds;
1405 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1410 static void osd_inode_getattr(const struct lu_env *env,
1411 struct inode *inode, struct lu_attr *attr)
1413 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1414 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1415 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1417 attr->la_atime = LTIME_S(inode->i_atime);
1418 attr->la_mtime = LTIME_S(inode->i_mtime);
1419 attr->la_ctime = LTIME_S(inode->i_ctime);
1420 attr->la_mode = inode->i_mode;
1421 attr->la_size = i_size_read(inode);
1422 attr->la_blocks = inode->i_blocks;
1423 attr->la_uid = inode->i_uid;
1424 attr->la_gid = inode->i_gid;
1425 attr->la_flags = LDISKFS_I(inode)->i_flags;
1426 attr->la_nlink = inode->i_nlink;
1427 attr->la_rdev = inode->i_rdev;
1428 attr->la_blksize = 1 << inode->i_blkbits;
1429 attr->la_blkbits = inode->i_blkbits;
1432 static int osd_attr_get(const struct lu_env *env,
1433 struct dt_object *dt,
1434 struct lu_attr *attr,
1435 struct lustre_capa *capa)
1437 struct osd_object *obj = osd_dt_obj(dt);
1439 LASSERT(dt_object_exists(dt));
1440 LINVRNT(osd_invariant(obj));
1442 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1445 cfs_spin_lock(&obj->oo_guard);
1446 osd_inode_getattr(env, obj->oo_inode, attr);
1447 cfs_spin_unlock(&obj->oo_guard);
1451 static int osd_declare_attr_set(const struct lu_env *env,
1452 struct dt_object *dt,
1453 const struct lu_attr *attr,
1454 struct thandle *handle)
1456 struct osd_thandle *oh;
1457 struct osd_object *obj;
1459 LASSERT(dt != NULL);
1460 LASSERT(handle != NULL);
1462 obj = osd_dt_obj(dt);
1463 LASSERT(osd_invariant(obj));
1465 oh = container_of0(handle, struct osd_thandle, ot_super);
1466 LASSERT(oh->ot_handle == NULL);
1468 OSD_DECLARE_OP(oh, attr_set);
1469 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1471 if (attr && attr->la_valid & LA_UID) {
1473 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1475 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1477 if (attr && attr->la_valid & LA_GID) {
1479 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1481 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1487 static int osd_inode_setattr(const struct lu_env *env,
1488 struct inode *inode, const struct lu_attr *attr)
1492 bits = attr->la_valid;
1494 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1496 if (bits & LA_ATIME)
1497 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1498 if (bits & LA_CTIME)
1499 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1500 if (bits & LA_MTIME)
1501 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1502 if (bits & LA_SIZE) {
1503 LDISKFS_I(inode)->i_disksize = attr->la_size;
1504 i_size_write(inode, attr->la_size);
1508 /* OSD should not change "i_blocks" which is used by quota.
1509 * "i_blocks" should be changed by ldiskfs only. */
1510 if (bits & LA_BLOCKS)
1511 inode->i_blocks = attr->la_blocks;
1514 inode->i_mode = (inode->i_mode & S_IFMT) |
1515 (attr->la_mode & ~S_IFMT);
1517 inode->i_uid = attr->la_uid;
1519 inode->i_gid = attr->la_gid;
1520 if (bits & LA_NLINK)
1521 inode->i_nlink = attr->la_nlink;
1523 inode->i_rdev = attr->la_rdev;
1525 if (bits & LA_FLAGS) {
1526 /* always keep S_NOCMTIME */
1527 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1533 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1535 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1536 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1541 if (attr->la_valid & LA_UID)
1542 iattr.ia_valid |= ATTR_UID;
1543 if (attr->la_valid & LA_GID)
1544 iattr.ia_valid |= ATTR_GID;
1545 iattr.ia_uid = attr->la_uid;
1546 iattr.ia_gid = attr->la_gid;
1548 rc = ll_vfs_dq_transfer(inode, &iattr);
1550 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1551 "enforcement enabled on the ldiskfs filesystem?",
1552 inode->i_sb->s_id, rc);
1559 static int osd_attr_set(const struct lu_env *env,
1560 struct dt_object *dt,
1561 const struct lu_attr *attr,
1562 struct thandle *handle,
1563 struct lustre_capa *capa)
1565 struct osd_object *obj = osd_dt_obj(dt);
1566 struct inode *inode;
1569 LASSERT(handle != NULL);
1570 LASSERT(dt_object_exists(dt));
1571 LASSERT(osd_invariant(obj));
1573 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1576 OSD_EXEC_OP(handle, attr_set);
1578 inode = obj->oo_inode;
1579 if (!osd_dt_dev(handle->th_dev)->od_is_md) {
1581 rc = osd_quota_transfer(inode, attr);
1585 #ifdef HAVE_QUOTA_SUPPORT
1586 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1587 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1588 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1593 if (attr->la_valid & LA_UID)
1594 iattr.ia_valid |= ATTR_UID;
1595 if (attr->la_valid & LA_GID)
1596 iattr.ia_valid |= ATTR_GID;
1597 iattr.ia_uid = attr->la_uid;
1598 iattr.ia_gid = attr->la_gid;
1599 osd_push_ctxt(env, save, 1);
1600 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1601 osd_pop_ctxt(save, 1);
1607 cfs_spin_lock(&obj->oo_guard);
1608 rc = osd_inode_setattr(env, inode, attr);
1609 cfs_spin_unlock(&obj->oo_guard);
1612 inode->i_sb->s_op->dirty_inode(inode);
1616 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1617 struct osd_object *obj,
1618 const char *name, const int namelen)
1620 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1623 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1625 struct dt_allocation_hint *hint,
1629 struct osd_device *osd = osd_obj2dev(obj);
1630 struct osd_thandle *oth;
1631 struct dt_object *parent = NULL;
1632 struct inode *inode;
1633 #ifdef HAVE_QUOTA_SUPPORT
1634 struct osd_ctxt *save = &info->oti_ctxt;
1637 LINVRNT(osd_invariant(obj));
1638 LASSERT(obj->oo_inode == NULL);
1639 LASSERT(obj->oo_hl_head == NULL);
1641 if (S_ISDIR(mode) && ldiskfs_pdo) {
1642 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1643 if (obj->oo_hl_head == NULL)
1647 oth = container_of(th, struct osd_thandle, ot_super);
1648 LASSERT(oth->ot_handle->h_transaction != NULL);
1650 if (hint && hint->dah_parent)
1651 parent = hint->dah_parent;
1653 #ifdef HAVE_QUOTA_SUPPORT
1654 osd_push_ctxt(info->oti_env, save, osd_dt_dev(th->th_dev)->od_is_md);
1656 inode = ldiskfs_create_inode(oth->ot_handle,
1657 parent ? osd_dt_obj(parent)->oo_inode :
1658 osd_sb(osd)->s_root->d_inode,
1660 #ifdef HAVE_QUOTA_SUPPORT
1661 osd_pop_ctxt(save, osd_dt_dev(th->th_dev)->od_is_md);
1663 if (!IS_ERR(inode)) {
1664 /* Do not update file c/mtime in ldiskfs.
1665 * NB: don't need any lock because no contention at this
1667 inode->i_flags |= S_NOCMTIME;
1668 inode->i_state |= I_LUSTRE_NOSCRUB;
1669 obj->oo_inode = inode;
1672 if (obj->oo_hl_head != NULL) {
1673 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1674 obj->oo_hl_head = NULL;
1676 result = PTR_ERR(inode);
1678 LINVRNT(osd_invariant(obj));
1686 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1687 struct lu_attr *attr,
1688 struct dt_allocation_hint *hint,
1689 struct dt_object_format *dof,
1693 struct osd_thandle *oth;
1694 struct osd_device *osd = osd_obj2dev(obj);
1695 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1697 LASSERT(S_ISDIR(attr->la_mode));
1699 oth = container_of(th, struct osd_thandle, ot_super);
1700 LASSERT(oth->ot_handle->h_transaction != NULL);
1701 result = osd_mkfile(info, obj, mode, hint, th);
1702 if (result == 0 && osd->od_iop_mode == 0) {
1703 LASSERT(obj->oo_inode != NULL);
1705 * XXX uh-oh... call low-level iam function directly.
1708 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1709 sizeof (struct osd_fid_pack),
1715 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1716 struct lu_attr *attr,
1717 struct dt_allocation_hint *hint,
1718 struct dt_object_format *dof,
1722 struct osd_thandle *oth;
1723 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1725 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1727 LASSERT(S_ISREG(attr->la_mode));
1729 oth = container_of(th, struct osd_thandle, ot_super);
1730 LASSERT(oth->ot_handle->h_transaction != NULL);
1732 result = osd_mkfile(info, obj, mode, hint, th);
1734 LASSERT(obj->oo_inode != NULL);
1735 if (feat->dif_flags & DT_IND_VARKEY)
1736 result = iam_lvar_create(obj->oo_inode,
1737 feat->dif_keysize_max,
1739 feat->dif_recsize_max,
1742 result = iam_lfix_create(obj->oo_inode,
1743 feat->dif_keysize_max,
1745 feat->dif_recsize_max,
1752 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1753 struct lu_attr *attr,
1754 struct dt_allocation_hint *hint,
1755 struct dt_object_format *dof,
1758 LASSERT(S_ISREG(attr->la_mode));
1759 return osd_mkfile(info, obj, (attr->la_mode &
1760 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1763 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1764 struct lu_attr *attr,
1765 struct dt_allocation_hint *hint,
1766 struct dt_object_format *dof,
1769 LASSERT(S_ISLNK(attr->la_mode));
1770 return osd_mkfile(info, obj, (attr->la_mode &
1771 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1774 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1775 struct lu_attr *attr,
1776 struct dt_allocation_hint *hint,
1777 struct dt_object_format *dof,
1780 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1783 LINVRNT(osd_invariant(obj));
1784 LASSERT(obj->oo_inode == NULL);
1785 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1786 S_ISFIFO(mode) || S_ISSOCK(mode));
1788 result = osd_mkfile(info, obj, mode, hint, th);
1790 LASSERT(obj->oo_inode != NULL);
1792 * This inode should be marked dirty for i_rdev. Currently
1793 * that is done in the osd_attr_init().
1795 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1797 LINVRNT(osd_invariant(obj));
1801 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1803 struct dt_allocation_hint *hint,
1804 struct dt_object_format *dof,
1807 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1809 osd_obj_type_f result;
1825 result = osd_mk_index;
1836 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1837 struct dt_object *parent, cfs_umode_t child_mode)
1841 memset(ah, 0, sizeof(*ah));
1842 ah->dah_parent = parent;
1843 ah->dah_mode = child_mode;
1846 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1847 struct lu_attr *attr, struct dt_object_format *dof)
1849 struct inode *inode = obj->oo_inode;
1850 __u64 valid = attr->la_valid;
1853 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1855 if (dof->dof_type != DFT_NODE)
1856 attr->la_valid &= ~LA_RDEV;
1857 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1858 attr->la_valid &= ~LA_ATIME;
1859 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1860 attr->la_valid &= ~LA_CTIME;
1861 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1862 attr->la_valid &= ~LA_MTIME;
1864 if (!osd_obj2dev(obj)->od_is_md) {
1866 result = osd_quota_transfer(inode, attr);
1870 #ifdef HAVE_QUOTA_SUPPORT
1871 attr->la_valid &= ~(LA_UID | LA_GID);
1875 if (attr->la_valid != 0) {
1876 result = osd_inode_setattr(info->oti_env, inode, attr);
1878 * The osd_inode_setattr() should always succeed here. The
1879 * only error that could be returned is EDQUOT when we are
1880 * trying to change the UID or GID of the inode. However, this
1881 * should not happen since quota enforcement is no longer
1882 * enabled on ldiskfs (lquota takes care of it).
1884 LASSERTF(result == 0, "%d", result);
1885 inode->i_sb->s_op->dirty_inode(inode);
1888 attr->la_valid = valid;
1892 * Helper function for osd_object_create()
1894 * \retval 0, on success
1896 static int __osd_object_create(struct osd_thread_info *info,
1897 struct osd_object *obj, struct lu_attr *attr,
1898 struct dt_allocation_hint *hint,
1899 struct dt_object_format *dof,
1905 /* we drop umask so that permissions we pass are not affected */
1906 umask = current->fs->umask;
1907 current->fs->umask = 0;
1909 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1912 osd_attr_init(info, obj, attr, dof);
1913 osd_object_init0(obj);
1915 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1916 unlock_new_inode(obj->oo_inode);
1919 /* restore previous umask value */
1920 current->fs->umask = umask;
1926 * Helper function for osd_object_create()
1928 * \retval 0, on success
1930 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1931 const struct lu_fid *fid, struct thandle *th)
1933 struct osd_thread_info *info = osd_oti_get(env);
1934 struct osd_inode_id *id = &info->oti_id;
1935 struct osd_device *osd = osd_obj2dev(obj);
1937 LASSERT(obj->oo_inode != NULL);
1939 if (osd->od_is_md) {
1940 struct md_ucred *uc = md_ucred(env);
1941 LASSERT(uc != NULL);
1944 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1945 return osd_oi_insert(info, osd, fid, id, th);
1948 static int osd_declare_object_create(const struct lu_env *env,
1949 struct dt_object *dt,
1950 struct lu_attr *attr,
1951 struct dt_allocation_hint *hint,
1952 struct dt_object_format *dof,
1953 struct thandle *handle)
1955 struct osd_thandle *oh;
1957 LASSERT(handle != NULL);
1959 oh = container_of0(handle, struct osd_thandle, ot_super);
1960 LASSERT(oh->ot_handle == NULL);
1962 OSD_DECLARE_OP(oh, create);
1963 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1964 /* XXX: So far, only normal fid needs be inserted into the oi,
1965 * things could be changed later. Revise following code then. */
1966 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1967 OSD_DECLARE_OP(oh, insert);
1968 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1969 /* Reuse idle OI block may cause additional one OI block
1971 oh->ot_credits += 1;
1973 /* If this is directory, then we expect . and .. to be inserted as
1974 * well. The one directory block always needs to be created for the
1975 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1976 * block), there is no danger of needing a tree for the first block.
1978 if (attr && S_ISDIR(attr->la_mode)) {
1979 OSD_DECLARE_OP(oh, insert);
1980 OSD_DECLARE_OP(oh, insert);
1981 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1985 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1986 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1991 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1992 struct lu_attr *attr,
1993 struct dt_allocation_hint *hint,
1994 struct dt_object_format *dof,
1997 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1998 struct osd_object *obj = osd_dt_obj(dt);
1999 struct osd_thread_info *info = osd_oti_get(env);
2004 LINVRNT(osd_invariant(obj));
2005 LASSERT(!dt_object_exists(dt));
2006 LASSERT(osd_write_locked(env, obj));
2007 LASSERT(th != NULL);
2009 if (unlikely(fid_is_acct(fid)))
2010 /* Quota files can't be created from the kernel any more,
2011 * 'tune2fs -O quota' will take care of creating them */
2014 OSD_EXEC_OP(th, create);
2016 result = __osd_object_create(info, obj, attr, hint, dof, th);
2018 result = __osd_oi_insert(env, obj, fid, th);
2020 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2021 LASSERT(osd_invariant(obj));
2026 * Called to destroy on-disk representation of the object
2028 * Concurrency: must be locked
2030 static int osd_declare_object_destroy(const struct lu_env *env,
2031 struct dt_object *dt,
2034 struct osd_object *obj = osd_dt_obj(dt);
2035 struct inode *inode = obj->oo_inode;
2036 struct osd_thandle *oh;
2040 oh = container_of0(th, struct osd_thandle, ot_super);
2041 LASSERT(oh->ot_handle == NULL);
2044 OSD_DECLARE_OP(oh, destroy);
2045 OSD_DECLARE_OP(oh, delete);
2046 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2047 /* XXX: So far, only normal fid needs to be inserted into the OI,
2048 * so only normal fid needs to be removed from the OI also. */
2049 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
2050 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2051 /* Recycle idle OI leaf may cause additional three OI blocks
2053 oh->ot_credits += 3;
2056 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
2057 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
2062 static int osd_object_destroy(const struct lu_env *env,
2063 struct dt_object *dt,
2066 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2067 struct osd_object *obj = osd_dt_obj(dt);
2068 struct inode *inode = obj->oo_inode;
2069 struct osd_device *osd = osd_obj2dev(obj);
2070 struct osd_thandle *oh;
2074 oh = container_of0(th, struct osd_thandle, ot_super);
2075 LASSERT(oh->ot_handle);
2077 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2079 if (unlikely(fid_is_acct(fid)))
2082 /* Parallel control for OI scrub. For most of cases, there is no
2083 * lock contention. So it will not affect unlink performance. */
2084 cfs_mutex_lock(&inode->i_mutex);
2085 if (S_ISDIR(inode->i_mode)) {
2086 LASSERT(osd_inode_unlinked(inode) ||
2087 inode->i_nlink == 1);
2088 cfs_spin_lock(&obj->oo_guard);
2090 cfs_spin_unlock(&obj->oo_guard);
2091 inode->i_sb->s_op->dirty_inode(inode);
2093 LASSERT(osd_inode_unlinked(inode));
2096 OSD_EXEC_OP(th, destroy);
2098 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2099 cfs_mutex_unlock(&inode->i_mutex);
2101 /* XXX: add to ext3 orphan list */
2102 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2104 /* not needed in the cache anymore */
2105 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2111 * Helper function for osd_xattr_set()
2113 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2114 const struct lu_buf *buf, const char *name, int fl)
2116 struct osd_object *obj = osd_dt_obj(dt);
2117 struct inode *inode = obj->oo_inode;
2118 struct osd_thread_info *info = osd_oti_get(env);
2119 struct dentry *dentry = &info->oti_child_dentry;
2123 LASSERT(dt_object_exists(dt));
2124 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2126 if (fl & LU_XATTR_REPLACE)
2127 fs_flags |= XATTR_REPLACE;
2129 if (fl & LU_XATTR_CREATE)
2130 fs_flags |= XATTR_CREATE;
2132 dentry->d_inode = inode;
2133 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2134 buf->lb_len, fs_flags);
2139 * Put the fid into lustre_mdt_attrs, and then place the structure
2140 * inode's ea. This fid should not be altered during the life time
2143 * \retval +ve, on success
2144 * \retval -ve, on error
2146 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2148 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2149 const struct lu_fid *fid)
2151 struct osd_thread_info *info = osd_oti_get(env);
2152 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2154 lustre_lma_init(mdt_attrs, fid);
2155 lustre_lma_swab(mdt_attrs);
2156 return __osd_xattr_set(env, dt,
2157 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2158 XATTR_NAME_LMA, LU_XATTR_CREATE);
2163 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2164 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2165 * To have compatilibility with 1.8 ldiskfs driver we need to have
2166 * magic number at start of fid data.
2167 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2170 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2171 const struct dt_rec *fid)
2173 param->edp_magic = LDISKFS_LUFID_MAGIC;
2174 param->edp_len = sizeof(struct lu_fid) + 1;
2176 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2177 (struct lu_fid *)fid);
2181 * Try to read the fid from inode ea into dt_rec, if return value
2182 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2184 * \param fid object fid.
2186 * \retval 0 on success
2188 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2189 __u32 ino, struct lu_fid *fid,
2190 struct osd_inode_id *id)
2192 struct osd_thread_info *info = osd_oti_get(env);
2193 struct inode *inode;
2196 osd_id_gen(id, ino, OSD_OII_NOGEN);
2197 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2199 RETURN(PTR_ERR(inode));
2206 * OSD layer object create function for interoperability mode (b11826).
2207 * This is mostly similar to osd_object_create(). Only difference being, fid is
2208 * inserted into inode ea here.
2210 * \retval 0, on success
2211 * \retval -ve, on error
2213 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2214 struct lu_attr *attr,
2215 struct dt_allocation_hint *hint,
2216 struct dt_object_format *dof,
2219 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2220 struct osd_object *obj = osd_dt_obj(dt);
2221 struct osd_thread_info *info = osd_oti_get(env);
2226 LASSERT(osd_invariant(obj));
2227 LASSERT(!dt_object_exists(dt));
2228 LASSERT(osd_write_locked(env, obj));
2229 LASSERT(th != NULL);
2231 if (unlikely(fid_is_acct(fid)))
2232 /* Quota files can't be created from the kernel any more,
2233 * 'tune2fs -O quota' will take care of creating them */
2236 OSD_EXEC_OP(th, create);
2238 result = __osd_object_create(info, obj, attr, hint, dof, th);
2239 /* objects under osd root shld have igif fid, so dont add fid EA */
2240 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2241 result = osd_ea_fid_set(env, dt, fid);
2244 result = __osd_oi_insert(env, obj, fid, th);
2246 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2247 LINVRNT(osd_invariant(obj));
2251 static int osd_declare_object_ref_add(const struct lu_env *env,
2252 struct dt_object *dt,
2253 struct thandle *handle)
2255 struct osd_thandle *oh;
2257 /* it's possible that object doesn't exist yet */
2258 LASSERT(handle != NULL);
2260 oh = container_of0(handle, struct osd_thandle, ot_super);
2261 LASSERT(oh->ot_handle == NULL);
2263 OSD_DECLARE_OP(oh, ref_add);
2264 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2270 * Concurrency: @dt is write locked.
2272 static int osd_object_ref_add(const struct lu_env *env,
2273 struct dt_object *dt, struct thandle *th)
2275 struct osd_object *obj = osd_dt_obj(dt);
2276 struct inode *inode = obj->oo_inode;
2278 LINVRNT(osd_invariant(obj));
2279 LASSERT(dt_object_exists(dt));
2280 LASSERT(osd_write_locked(env, obj));
2281 LASSERT(th != NULL);
2283 OSD_EXEC_OP(th, ref_add);
2286 * DIR_NLINK feature is set for compatibility reasons if:
2287 * 1) nlinks > LDISKFS_LINK_MAX, or
2288 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2290 * It is easier to always set this flag (rather than check and set),
2291 * since it has less overhead, and the superblock will be dirtied
2292 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2293 * do not actually care whether this flag is set or not.
2295 cfs_spin_lock(&obj->oo_guard);
2297 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2298 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2299 inode->i_nlink == 2)
2302 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2303 cfs_spin_unlock(&obj->oo_guard);
2304 inode->i_sb->s_op->dirty_inode(inode);
2305 LINVRNT(osd_invariant(obj));
2310 static int osd_declare_object_ref_del(const struct lu_env *env,
2311 struct dt_object *dt,
2312 struct thandle *handle)
2314 struct osd_thandle *oh;
2316 LASSERT(dt_object_exists(dt));
2317 LASSERT(handle != NULL);
2319 oh = container_of0(handle, struct osd_thandle, ot_super);
2320 LASSERT(oh->ot_handle == NULL);
2322 OSD_DECLARE_OP(oh, ref_del);
2323 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2329 * Concurrency: @dt is write locked.
2331 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2334 struct osd_object *obj = osd_dt_obj(dt);
2335 struct inode *inode = obj->oo_inode;
2337 LINVRNT(osd_invariant(obj));
2338 LASSERT(dt_object_exists(dt));
2339 LASSERT(osd_write_locked(env, obj));
2340 LASSERT(th != NULL);
2342 OSD_EXEC_OP(th, ref_del);
2344 cfs_spin_lock(&obj->oo_guard);
2345 LASSERT(inode->i_nlink > 0);
2347 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2348 * then the nlink count is 1. Don't let it be set to 0 or the directory
2349 * inode will be deleted incorrectly. */
2350 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2352 cfs_spin_unlock(&obj->oo_guard);
2353 inode->i_sb->s_op->dirty_inode(inode);
2354 LINVRNT(osd_invariant(obj));
2360 * Get the 64-bit version for an inode.
2362 static int osd_object_version_get(const struct lu_env *env,
2363 struct dt_object *dt, dt_obj_version_t *ver)
2365 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2367 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2368 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2369 *ver = LDISKFS_I(inode)->i_fs_version;
2374 * Concurrency: @dt is read locked.
2376 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2377 struct lu_buf *buf, const char *name,
2378 struct lustre_capa *capa)
2380 struct osd_object *obj = osd_dt_obj(dt);
2381 struct inode *inode = obj->oo_inode;
2382 struct osd_thread_info *info = osd_oti_get(env);
2383 struct dentry *dentry = &info->oti_obj_dentry;
2385 /* version get is not real XATTR but uses xattr API */
2386 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2387 /* for version we are just using xattr API but change inode
2389 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2390 osd_object_version_get(env, dt, buf->lb_buf);
2391 return sizeof(dt_obj_version_t);
2394 LASSERT(dt_object_exists(dt));
2395 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2396 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2398 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2401 dentry->d_inode = inode;
2402 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2406 static int osd_declare_xattr_set(const struct lu_env *env,
2407 struct dt_object *dt,
2408 const struct lu_buf *buf, const char *name,
2409 int fl, struct thandle *handle)
2411 struct osd_thandle *oh;
2413 LASSERT(handle != NULL);
2415 oh = container_of0(handle, struct osd_thandle, ot_super);
2416 LASSERT(oh->ot_handle == NULL);
2418 OSD_DECLARE_OP(oh, xattr_set);
2419 if (strcmp(name, XATTR_NAME_VERSION) == 0)
2420 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2422 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2428 * Set the 64-bit version for object
2430 static void osd_object_version_set(const struct lu_env *env,
2431 struct dt_object *dt,
2432 dt_obj_version_t *new_version)
2434 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2436 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2437 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2439 LDISKFS_I(inode)->i_fs_version = *new_version;
2440 /** Version is set after all inode operations are finished,
2441 * so we should mark it dirty here */
2442 inode->i_sb->s_op->dirty_inode(inode);
2446 * Concurrency: @dt is write locked.
2448 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2449 const struct lu_buf *buf, const char *name, int fl,
2450 struct thandle *handle, struct lustre_capa *capa)
2452 LASSERT(handle != NULL);
2454 /* version set is not real XATTR */
2455 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2456 /* for version we are just using xattr API but change inode
2458 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2459 osd_object_version_set(env, dt, buf->lb_buf);
2460 return sizeof(dt_obj_version_t);
2463 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2466 OSD_EXEC_OP(handle, xattr_set);
2467 return __osd_xattr_set(env, dt, buf, name, fl);
2471 * Concurrency: @dt is read locked.
2473 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2474 struct lu_buf *buf, struct lustre_capa *capa)
2476 struct osd_object *obj = osd_dt_obj(dt);
2477 struct inode *inode = obj->oo_inode;
2478 struct osd_thread_info *info = osd_oti_get(env);
2479 struct dentry *dentry = &info->oti_obj_dentry;
2481 LASSERT(dt_object_exists(dt));
2482 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2483 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2485 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2488 dentry->d_inode = inode;
2489 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2492 static int osd_declare_xattr_del(const struct lu_env *env,
2493 struct dt_object *dt, const char *name,
2494 struct thandle *handle)
2496 struct osd_thandle *oh;
2498 LASSERT(dt_object_exists(dt));
2499 LASSERT(handle != NULL);
2501 oh = container_of0(handle, struct osd_thandle, ot_super);
2502 LASSERT(oh->ot_handle == NULL);
2504 OSD_DECLARE_OP(oh, xattr_set);
2505 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2511 * Concurrency: @dt is write locked.
2513 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2514 const char *name, struct thandle *handle,
2515 struct lustre_capa *capa)
2517 struct osd_object *obj = osd_dt_obj(dt);
2518 struct inode *inode = obj->oo_inode;
2519 struct osd_thread_info *info = osd_oti_get(env);
2520 struct dentry *dentry = &info->oti_obj_dentry;
2523 LASSERT(dt_object_exists(dt));
2524 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2525 LASSERT(osd_write_locked(env, obj));
2526 LASSERT(handle != NULL);
2528 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2531 OSD_EXEC_OP(handle, xattr_set);
2533 dentry->d_inode = inode;
2534 rc = inode->i_op->removexattr(dentry, name);
2538 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2539 struct dt_object *dt,
2540 struct lustre_capa *old,
2543 struct osd_thread_info *info = osd_oti_get(env);
2544 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2545 struct osd_object *obj = osd_dt_obj(dt);
2546 struct osd_device *dev = osd_obj2dev(obj);
2547 struct lustre_capa_key *key = &info->oti_capa_key;
2548 struct lustre_capa *capa = &info->oti_capa;
2549 struct obd_capa *oc;
2550 struct md_capainfo *ci;
2554 if (!dev->od_fl_capa)
2555 RETURN(ERR_PTR(-ENOENT));
2557 LASSERT(dt_object_exists(dt));
2558 LINVRNT(osd_invariant(obj));
2560 /* renewal sanity check */
2561 if (old && osd_object_auth(env, dt, old, opc))
2562 RETURN(ERR_PTR(-EACCES));
2564 ci = md_capainfo(env);
2566 RETURN(ERR_PTR(-ENOENT));
2568 switch (ci->mc_auth) {
2572 capa->lc_uid = obj->oo_inode->i_uid;
2573 capa->lc_gid = obj->oo_inode->i_gid;
2574 capa->lc_flags = LC_ID_PLAIN;
2576 case LC_ID_CONVERT: {
2579 s[0] = obj->oo_inode->i_uid;
2580 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2581 s[2] = obj->oo_inode->i_gid;
2582 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2583 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2585 RETURN(ERR_PTR(rc));
2587 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2588 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2589 capa->lc_flags = LC_ID_CONVERT;
2593 RETURN(ERR_PTR(-EINVAL));
2596 capa->lc_fid = *fid;
2598 capa->lc_flags |= dev->od_capa_alg << 24;
2599 capa->lc_timeout = dev->od_capa_timeout;
2600 capa->lc_expiry = 0;
2602 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2604 LASSERT(!capa_is_expired(oc));
2608 cfs_spin_lock(&capa_lock);
2609 *key = dev->od_capa_keys[1];
2610 cfs_spin_unlock(&capa_lock);
2612 capa->lc_keyid = key->lk_keyid;
2613 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2615 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2617 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2618 RETURN(ERR_PTR(rc));
2621 oc = capa_add(dev->od_capa_hash, capa);
2625 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2627 struct osd_object *obj = osd_dt_obj(dt);
2628 struct inode *inode = obj->oo_inode;
2629 struct osd_thread_info *info = osd_oti_get(env);
2630 struct dentry *dentry = &info->oti_obj_dentry;
2631 struct file *file = &info->oti_file;
2636 dentry->d_inode = inode;
2637 file->f_dentry = dentry;
2638 file->f_mapping = inode->i_mapping;
2639 file->f_op = inode->i_fop;
2640 mutex_lock(&inode->i_mutex);
2641 rc = file->f_op->fsync(file, dentry, 0);
2642 mutex_unlock(&inode->i_mutex);
2646 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2649 struct osd_object *obj = osd_dt_obj(dt);
2652 *data = (void *)obj->oo_inode;
2660 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2661 const struct dt_index_features *feat)
2663 struct iam_descr *descr;
2665 if (osd_object_is_root(o))
2666 return feat == &dt_directory_features;
2668 LASSERT(o->oo_dir != NULL);
2670 descr = o->oo_dir->od_container.ic_descr;
2671 if (feat == &dt_directory_features) {
2672 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2678 feat->dif_keysize_min <= descr->id_key_size &&
2679 descr->id_key_size <= feat->dif_keysize_max &&
2680 feat->dif_recsize_min <= descr->id_rec_size &&
2681 descr->id_rec_size <= feat->dif_recsize_max &&
2682 !(feat->dif_flags & (DT_IND_VARKEY |
2683 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2684 ergo(feat->dif_flags & DT_IND_UPDATE,
2685 1 /* XXX check that object (and file system) is
2690 static int osd_iam_container_init(const struct lu_env *env,
2691 struct osd_object *obj,
2692 struct osd_directory *dir)
2694 struct iam_container *bag = &dir->od_container;
2697 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2701 result = iam_container_setup(bag);
2703 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2705 iam_container_fini(bag);
2712 * Concurrency: no external locking is necessary.
2714 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2715 const struct dt_index_features *feat)
2719 struct osd_object *obj = osd_dt_obj(dt);
2720 struct osd_device *osd = osd_obj2dev(obj);
2722 LINVRNT(osd_invariant(obj));
2723 LASSERT(dt_object_exists(dt));
2725 if (osd_object_is_root(obj)) {
2726 dt->do_index_ops = &osd_index_ea_ops;
2728 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2729 dt->do_index_ops = &osd_index_ea_ops;
2730 if (S_ISDIR(obj->oo_inode->i_mode))
2735 } else if (unlikely(feat == &dt_otable_features)) {
2736 dt->do_index_ops = &osd_otable_ops;
2738 } else if (feat == &dt_acct_features) {
2739 dt->do_index_ops = &osd_acct_index_ops;
2742 } else if (!osd_has_index(obj)) {
2743 struct osd_directory *dir;
2748 cfs_spin_lock(&obj->oo_guard);
2749 if (obj->oo_dir == NULL)
2753 * Concurrent thread allocated container data.
2756 cfs_spin_unlock(&obj->oo_guard);
2758 * Now, that we have container data, serialize its
2761 cfs_down_write(&obj->oo_ext_idx_sem);
2763 * recheck under lock.
2765 if (!osd_has_index(obj))
2766 result = osd_iam_container_init(env, obj, dir);
2769 cfs_up_write(&obj->oo_ext_idx_sem);
2777 if (result == 0 && skip_iam == 0) {
2778 if (!osd_iam_index_probe(env, obj, feat))
2781 LINVRNT(osd_invariant(obj));
2786 static int osd_otable_it_attr_get(const struct lu_env *env,
2787 struct dt_object *dt,
2788 struct lu_attr *attr,
2789 struct lustre_capa *capa)
2795 static const struct dt_object_operations osd_obj_ops = {
2796 .do_read_lock = osd_object_read_lock,
2797 .do_write_lock = osd_object_write_lock,
2798 .do_read_unlock = osd_object_read_unlock,
2799 .do_write_unlock = osd_object_write_unlock,
2800 .do_write_locked = osd_object_write_locked,
2801 .do_attr_get = osd_attr_get,
2802 .do_declare_attr_set = osd_declare_attr_set,
2803 .do_attr_set = osd_attr_set,
2804 .do_ah_init = osd_ah_init,
2805 .do_declare_create = osd_declare_object_create,
2806 .do_create = osd_object_create,
2807 .do_declare_destroy = osd_declare_object_destroy,
2808 .do_destroy = osd_object_destroy,
2809 .do_index_try = osd_index_try,
2810 .do_declare_ref_add = osd_declare_object_ref_add,
2811 .do_ref_add = osd_object_ref_add,
2812 .do_declare_ref_del = osd_declare_object_ref_del,
2813 .do_ref_del = osd_object_ref_del,
2814 .do_xattr_get = osd_xattr_get,
2815 .do_declare_xattr_set = osd_declare_xattr_set,
2816 .do_xattr_set = osd_xattr_set,
2817 .do_declare_xattr_del = osd_declare_xattr_del,
2818 .do_xattr_del = osd_xattr_del,
2819 .do_xattr_list = osd_xattr_list,
2820 .do_capa_get = osd_capa_get,
2821 .do_object_sync = osd_object_sync,
2822 .do_data_get = osd_data_get,
2826 * dt_object_operations for interoperability mode
2827 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2829 static const struct dt_object_operations osd_obj_ea_ops = {
2830 .do_read_lock = osd_object_read_lock,
2831 .do_write_lock = osd_object_write_lock,
2832 .do_read_unlock = osd_object_read_unlock,
2833 .do_write_unlock = osd_object_write_unlock,
2834 .do_write_locked = osd_object_write_locked,
2835 .do_attr_get = osd_attr_get,
2836 .do_declare_attr_set = osd_declare_attr_set,
2837 .do_attr_set = osd_attr_set,
2838 .do_ah_init = osd_ah_init,
2839 .do_declare_create = osd_declare_object_create,
2840 .do_create = osd_object_ea_create,
2841 .do_declare_destroy = osd_declare_object_destroy,
2842 .do_destroy = osd_object_destroy,
2843 .do_index_try = osd_index_try,
2844 .do_declare_ref_add = osd_declare_object_ref_add,
2845 .do_ref_add = osd_object_ref_add,
2846 .do_declare_ref_del = osd_declare_object_ref_del,
2847 .do_ref_del = osd_object_ref_del,
2848 .do_xattr_get = osd_xattr_get,
2849 .do_declare_xattr_set = osd_declare_xattr_set,
2850 .do_xattr_set = osd_xattr_set,
2851 .do_declare_xattr_del = osd_declare_xattr_del,
2852 .do_xattr_del = osd_xattr_del,
2853 .do_xattr_list = osd_xattr_list,
2854 .do_capa_get = osd_capa_get,
2855 .do_object_sync = osd_object_sync,
2856 .do_data_get = osd_data_get,
2859 static const struct dt_object_operations osd_obj_otable_it_ops = {
2860 .do_attr_get = osd_otable_it_attr_get,
2861 .do_index_try = osd_index_try,
2864 static int osd_index_declare_iam_delete(const struct lu_env *env,
2865 struct dt_object *dt,
2866 const struct dt_key *key,
2867 struct thandle *handle)
2869 struct osd_thandle *oh;
2871 oh = container_of0(handle, struct osd_thandle, ot_super);
2872 LASSERT(oh->ot_handle == NULL);
2874 OSD_DECLARE_OP(oh, delete);
2875 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2881 * delete a (key, value) pair from index \a dt specified by \a key
2883 * \param dt osd index object
2884 * \param key key for index
2885 * \param rec record reference
2886 * \param handle transaction handler
2889 * \retval -ve failure
2892 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2893 const struct dt_key *key,
2894 struct thandle *handle,
2895 struct lustre_capa *capa)
2897 struct osd_object *obj = osd_dt_obj(dt);
2898 struct osd_thandle *oh;
2899 struct iam_path_descr *ipd;
2900 struct iam_container *bag = &obj->oo_dir->od_container;
2905 LINVRNT(osd_invariant(obj));
2906 LASSERT(dt_object_exists(dt));
2907 LASSERT(bag->ic_object == obj->oo_inode);
2908 LASSERT(handle != NULL);
2910 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2913 OSD_EXEC_OP(handle, delete);
2915 ipd = osd_idx_ipd_get(env, bag);
2916 if (unlikely(ipd == NULL))
2919 oh = container_of0(handle, struct osd_thandle, ot_super);
2920 LASSERT(oh->ot_handle != NULL);
2921 LASSERT(oh->ot_handle->h_transaction != NULL);
2923 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2924 osd_ipd_put(env, bag, ipd);
2925 LINVRNT(osd_invariant(obj));
2929 static int osd_index_declare_ea_delete(const struct lu_env *env,
2930 struct dt_object *dt,
2931 const struct dt_key *key,
2932 struct thandle *handle)
2934 struct osd_thandle *oh;
2936 LASSERT(dt_object_exists(dt));
2937 LASSERT(handle != NULL);
2939 oh = container_of0(handle, struct osd_thandle, ot_super);
2940 LASSERT(oh->ot_handle == NULL);
2942 OSD_DECLARE_OP(oh, delete);
2943 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2945 LASSERT(osd_dt_obj(dt)->oo_inode);
2946 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2947 osd_dt_obj(dt)->oo_inode);
2948 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2949 osd_dt_obj(dt)->oo_inode);
2954 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2957 struct osd_fid_pack *rec;
2960 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2961 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2962 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2968 * Index delete function for interoperability mode (b11826).
2969 * It will remove the directory entry added by osd_index_ea_insert().
2970 * This entry is needed to maintain name->fid mapping.
2972 * \param key, key i.e. file entry to be deleted
2974 * \retval 0, on success
2975 * \retval -ve, on error
2977 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2978 const struct dt_key *key,
2979 struct thandle *handle,
2980 struct lustre_capa *capa)
2982 struct osd_object *obj = osd_dt_obj(dt);
2983 struct inode *dir = obj->oo_inode;
2984 struct dentry *dentry;
2985 struct osd_thandle *oh;
2986 struct ldiskfs_dir_entry_2 *de;
2987 struct buffer_head *bh;
2988 struct htree_lock *hlock = NULL;
2993 LINVRNT(osd_invariant(obj));
2994 LASSERT(dt_object_exists(dt));
2995 LASSERT(handle != NULL);
2997 OSD_EXEC_OP(handle, delete);
2999 oh = container_of(handle, struct osd_thandle, ot_super);
3000 LASSERT(oh->ot_handle != NULL);
3001 LASSERT(oh->ot_handle->h_transaction != NULL);
3003 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3006 dentry = osd_child_dentry_get(env, obj,
3007 (char *)key, strlen((char *)key));
3009 if (obj->oo_hl_head != NULL) {
3010 hlock = osd_oti_get(env)->oti_hlock;
3011 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3012 dir, LDISKFS_HLOCK_DEL);
3014 cfs_down_write(&obj->oo_ext_idx_sem);
3017 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3019 rc = ldiskfs_delete_entry(oh->ot_handle,
3026 ldiskfs_htree_unlock(hlock);
3028 cfs_up_write(&obj->oo_ext_idx_sem);
3030 LASSERT(osd_invariant(obj));
3035 * Lookup index for \a key and copy record to \a rec.
3037 * \param dt osd index object
3038 * \param key key for index
3039 * \param rec record reference
3041 * \retval +ve success : exact mach
3042 * \retval 0 return record with key not greater than \a key
3043 * \retval -ve failure
3045 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3046 struct dt_rec *rec, const struct dt_key *key,
3047 struct lustre_capa *capa)
3049 struct osd_object *obj = osd_dt_obj(dt);
3050 struct iam_path_descr *ipd;
3051 struct iam_container *bag = &obj->oo_dir->od_container;
3052 struct osd_thread_info *oti = osd_oti_get(env);
3053 struct iam_iterator *it = &oti->oti_idx_it;
3054 struct iam_rec *iam_rec;
3059 LASSERT(osd_invariant(obj));
3060 LASSERT(dt_object_exists(dt));
3061 LASSERT(bag->ic_object == obj->oo_inode);
3063 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3066 ipd = osd_idx_ipd_get(env, bag);
3070 /* got ipd now we can start iterator. */
3071 iam_it_init(it, bag, 0, ipd);
3073 rc = iam_it_get(it, (struct iam_key *)key);
3075 if (S_ISDIR(obj->oo_inode->i_mode))
3076 iam_rec = (struct iam_rec *)oti->oti_ldp;
3078 iam_rec = (struct iam_rec *) rec;
3080 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3081 if (S_ISDIR(obj->oo_inode->i_mode))
3082 osd_fid_unpack((struct lu_fid *) rec,
3083 (struct osd_fid_pack *)iam_rec);
3087 osd_ipd_put(env, bag, ipd);
3089 LINVRNT(osd_invariant(obj));
3094 static int osd_index_declare_iam_insert(const struct lu_env *env,
3095 struct dt_object *dt,
3096 const struct dt_rec *rec,
3097 const struct dt_key *key,
3098 struct thandle *handle)
3100 struct osd_thandle *oh;
3102 LASSERT(dt_object_exists(dt));
3103 LASSERT(handle != NULL);
3105 oh = container_of0(handle, struct osd_thandle, ot_super);
3106 LASSERT(oh->ot_handle == NULL);
3108 OSD_DECLARE_OP(oh, insert);
3109 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3115 * Inserts (key, value) pair in \a dt index object.
3117 * \param dt osd index object
3118 * \param key key for index
3119 * \param rec record reference
3120 * \param th transaction handler
3123 * \retval -ve failure
3125 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3126 const struct dt_rec *rec,
3127 const struct dt_key *key, struct thandle *th,
3128 struct lustre_capa *capa, int ignore_quota)
3130 struct osd_object *obj = osd_dt_obj(dt);
3131 struct iam_path_descr *ipd;
3132 struct osd_thandle *oh;
3133 struct iam_container *bag = &obj->oo_dir->od_container;
3134 #ifdef HAVE_QUOTA_SUPPORT
3135 cfs_cap_t save = cfs_curproc_cap_pack();
3137 struct osd_thread_info *oti = osd_oti_get(env);
3138 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3143 LINVRNT(osd_invariant(obj));
3144 LASSERT(dt_object_exists(dt));
3145 LASSERT(bag->ic_object == obj->oo_inode);
3146 LASSERT(th != NULL);
3148 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3151 OSD_EXEC_OP(th, insert);
3153 ipd = osd_idx_ipd_get(env, bag);
3154 if (unlikely(ipd == NULL))
3157 oh = container_of0(th, struct osd_thandle, ot_super);
3158 LASSERT(oh->ot_handle != NULL);
3159 LASSERT(oh->ot_handle->h_transaction != NULL);
3160 #ifdef HAVE_QUOTA_SUPPORT
3162 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3164 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3166 if (S_ISDIR(obj->oo_inode->i_mode))
3167 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3169 iam_rec = (struct iam_rec *) rec;
3170 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3172 #ifdef HAVE_QUOTA_SUPPORT
3173 cfs_curproc_cap_unpack(save);
3175 osd_ipd_put(env, bag, ipd);
3176 LINVRNT(osd_invariant(obj));
3181 * Calls ldiskfs_add_entry() to add directory entry
3182 * into the directory. This is required for
3183 * interoperability mode (b11826)
3185 * \retval 0, on success
3186 * \retval -ve, on error
3188 static int __osd_ea_add_rec(struct osd_thread_info *info,
3189 struct osd_object *pobj, struct inode *cinode,
3190 const char *name, const struct dt_rec *fid,
3191 struct htree_lock *hlock, struct thandle *th)
3193 struct ldiskfs_dentry_param *ldp;
3194 struct dentry *child;
3195 struct osd_thandle *oth;
3198 oth = container_of(th, struct osd_thandle, ot_super);
3199 LASSERT(oth->ot_handle != NULL);
3200 LASSERT(oth->ot_handle->h_transaction != NULL);
3202 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3204 /* XXX: remove fid_is_igif() check here.
3205 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3206 * it is IGIF now but needs FID in dir entry as well for readdir
3208 * LU-838 should fix that and remove fid_is_igif() check */
3209 if (fid_is_igif((struct lu_fid *)fid) ||
3210 fid_is_norm((struct lu_fid *)fid)) {
3211 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3212 osd_get_ldiskfs_dirent_param(ldp, fid);
3213 child->d_fsdata = (void *)ldp;
3215 child->d_fsdata = NULL;
3217 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3223 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3224 * into the directory.Also sets flags into osd object to
3225 * indicate dot and dotdot are created. This is required for
3226 * interoperability mode (b11826)
3228 * \param dir directory for dot and dotdot fixup.
3229 * \param obj child object for linking
3231 * \retval 0, on success
3232 * \retval -ve, on error
3234 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3235 struct osd_object *dir,
3236 struct inode *parent_dir, const char *name,
3237 const struct dt_rec *dot_fid,
3238 const struct dt_rec *dot_dot_fid,
3241 struct inode *inode = dir->oo_inode;
3242 struct ldiskfs_dentry_param *dot_ldp;
3243 struct ldiskfs_dentry_param *dot_dot_ldp;
3244 struct osd_thandle *oth;
3247 oth = container_of(th, struct osd_thandle, ot_super);
3248 LASSERT(oth->ot_handle->h_transaction != NULL);
3249 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3251 if (strcmp(name, dot) == 0) {
3252 if (dir->oo_compat_dot_created) {
3255 LASSERT(inode == parent_dir);
3256 dir->oo_compat_dot_created = 1;
3259 } else if(strcmp(name, dotdot) == 0) {
3260 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3261 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3263 if (!dir->oo_compat_dot_created)
3265 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3266 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3267 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3272 /* in case of rename, dotdot is already created */
3273 if (dir->oo_compat_dotdot_created) {
3274 return __osd_ea_add_rec(info, dir, parent_dir, name,
3275 dot_dot_fid, NULL, th);
3278 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3279 inode, dot_ldp, dot_dot_ldp);
3281 dir->oo_compat_dotdot_created = 1;
3289 * It will call the appropriate osd_add* function and return the
3290 * value, return by respective functions.
3292 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3293 struct inode *cinode, const char *name,
3294 const struct dt_rec *fid, struct thandle *th)
3296 struct osd_thread_info *info = osd_oti_get(env);
3297 struct htree_lock *hlock;
3300 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3302 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3304 if (hlock != NULL) {
3305 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3308 cfs_down_write(&pobj->oo_ext_idx_sem);
3310 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3311 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3314 if (hlock != NULL) {
3315 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3316 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3318 cfs_down_write(&pobj->oo_ext_idx_sem);
3321 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3325 ldiskfs_htree_unlock(hlock);
3327 cfs_up_write(&pobj->oo_ext_idx_sem);
3333 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3334 struct osd_idmap_cache *oic)
3336 struct osd_scrub *scrub = &dev->od_scrub;
3337 struct lu_fid *fid = &oic->oic_fid;
3338 struct osd_inode_id *id = &oti->oti_id;
3344 rc = osd_oi_lookup(oti, dev, fid, id);
3345 if (rc != 0 && rc != -ENOENT)
3348 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3351 if (thread_is_running(&scrub->os_thread)) {
3352 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3353 /* There is race condition between osd_oi_lookup and OI scrub.
3354 * The OI scrub finished just after osd_oi_lookup() failure.
3355 * Under such case, it is unnecessary to trigger OI scrub again,
3356 * but try to call osd_oi_lookup() again. */
3357 if (unlikely(rc == -EAGAIN))
3363 if (!scrub->os_no_scrub && ++once == 1) {
3364 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3366 rc = osd_scrub_start(dev);
3367 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3369 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3375 RETURN(rc = -EREMCHG);
3379 * Calls ->lookup() to find dentry. From dentry get inode and
3380 * read inode's ea to get fid. This is required for interoperability
3383 * \retval 0, on success
3384 * \retval -ve, on error
3386 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3387 struct dt_rec *rec, const struct dt_key *key)
3389 struct inode *dir = obj->oo_inode;
3390 struct dentry *dentry;
3391 struct ldiskfs_dir_entry_2 *de;
3392 struct buffer_head *bh;
3393 struct lu_fid *fid = (struct lu_fid *) rec;
3394 struct htree_lock *hlock = NULL;
3398 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3400 dentry = osd_child_dentry_get(env, obj,
3401 (char *)key, strlen((char *)key));
3403 if (obj->oo_hl_head != NULL) {
3404 hlock = osd_oti_get(env)->oti_hlock;
3405 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3406 dir, LDISKFS_HLOCK_LOOKUP);
3408 cfs_down_read(&obj->oo_ext_idx_sem);
3411 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3413 struct osd_thread_info *oti = osd_oti_get(env);
3414 struct osd_idmap_cache *oic = &oti->oti_cache;
3415 struct osd_device *dev = osd_obj2dev(obj);
3416 struct osd_scrub *scrub = &dev->od_scrub;
3417 struct scrub_file *sf = &scrub->os_file;
3419 ino = le32_to_cpu(de->inode);
3420 rc = osd_get_fid_from_dentry(de, rec);
3422 /* done with de, release bh */
3425 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3427 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3428 if (rc != 0 || !fid_is_norm(fid)) {
3429 fid_zero(&oic->oic_fid);
3433 oic->oic_fid = *fid;
3434 if ((scrub->os_pos_current <= ino) &&
3435 (sf->sf_flags & SF_INCONSISTENT ||
3436 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3438 rc = osd_consistency_check(oti, dev, oic);
3447 ldiskfs_htree_unlock(hlock);
3449 cfs_up_read(&obj->oo_ext_idx_sem);
3454 * Find the osd object for given fid.
3456 * \param fid need to find the osd object having this fid
3458 * \retval osd_object on success
3459 * \retval -ve on error
3461 struct osd_object *osd_object_find(const struct lu_env *env,
3462 struct dt_object *dt,
3463 const struct lu_fid *fid)
3465 struct lu_device *ludev = dt->do_lu.lo_dev;
3466 struct osd_object *child = NULL;
3467 struct lu_object *luch;
3468 struct lu_object *lo;
3471 * at this point topdev might not exist yet
3472 * (i.e. MGS is preparing profiles). so we can
3473 * not rely on topdev and instead lookup with
3474 * our device passed as topdev. this can't work
3475 * if the object isn't cached yet (as osd doesn't
3476 * allocate lu_header). IOW, the object must be
3477 * in the cache, otherwise lu_object_alloc() crashes
3480 luch = lu_object_find_at(env, ludev, fid, NULL);
3481 if (!IS_ERR(luch)) {
3482 if (lu_object_exists(luch)) {
3483 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3485 child = osd_obj(lo);
3487 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3488 "lu_object can't be located"
3489 DFID"\n", PFID(fid));
3491 if (child == NULL) {
3492 lu_object_put(env, luch);
3493 CERROR("Unable to get osd_object\n");
3494 child = ERR_PTR(-ENOENT);
3497 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3498 "lu_object does not exists "DFID"\n",
3500 lu_object_put(env, luch);
3501 child = ERR_PTR(-ENOENT);
3504 child = (void *)luch;
3510 * Put the osd object once done with it.
3512 * \param obj osd object that needs to be put
3514 static inline void osd_object_put(const struct lu_env *env,
3515 struct osd_object *obj)
3517 lu_object_put(env, &obj->oo_dt.do_lu);
3520 static int osd_index_declare_ea_insert(const struct lu_env *env,
3521 struct dt_object *dt,
3522 const struct dt_rec *rec,
3523 const struct dt_key *key,
3524 struct thandle *handle)
3526 struct osd_thandle *oh;
3528 LASSERT(dt_object_exists(dt));
3529 LASSERT(handle != NULL);
3531 oh = container_of0(handle, struct osd_thandle, ot_super);
3532 LASSERT(oh->ot_handle == NULL);
3534 OSD_DECLARE_OP(oh, insert);
3535 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3537 LASSERT(osd_dt_obj(dt)->oo_inode);
3538 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3539 osd_dt_obj(dt)->oo_inode);
3540 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3541 osd_dt_obj(dt)->oo_inode);
3547 * Index add function for interoperability mode (b11826).
3548 * It will add the directory entry.This entry is needed to
3549 * maintain name->fid mapping.
3551 * \param key it is key i.e. file entry to be inserted
3552 * \param rec it is value of given key i.e. fid
3554 * \retval 0, on success
3555 * \retval -ve, on error
3557 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3558 const struct dt_rec *rec,
3559 const struct dt_key *key, struct thandle *th,
3560 struct lustre_capa *capa, int ignore_quota)
3562 struct osd_object *obj = osd_dt_obj(dt);
3563 struct lu_fid *fid = (struct lu_fid *) rec;
3564 const char *name = (const char *)key;
3565 struct osd_object *child;
3566 #ifdef HAVE_QUOTA_SUPPORT
3567 cfs_cap_t save = cfs_curproc_cap_pack();
3573 LASSERT(osd_invariant(obj));
3574 LASSERT(dt_object_exists(dt));
3575 LASSERT(th != NULL);
3577 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3580 child = osd_object_find(env, dt, fid);
3581 if (!IS_ERR(child)) {
3582 #ifdef HAVE_QUOTA_SUPPORT
3584 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3586 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3588 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3589 #ifdef HAVE_QUOTA_SUPPORT
3590 cfs_curproc_cap_unpack(save);
3592 osd_object_put(env, child);
3594 rc = PTR_ERR(child);
3597 LASSERT(osd_invariant(obj));
3602 * Initialize osd Iterator for given osd index object.
3604 * \param dt osd index object
3607 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3608 struct dt_object *dt,
3610 struct lustre_capa *capa)
3612 struct osd_it_iam *it;
3613 struct osd_thread_info *oti = osd_oti_get(env);
3614 struct osd_object *obj = osd_dt_obj(dt);
3615 struct lu_object *lo = &dt->do_lu;
3616 struct iam_path_descr *ipd;
3617 struct iam_container *bag = &obj->oo_dir->od_container;
3619 LASSERT(lu_object_exists(lo));
3621 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3622 return ERR_PTR(-EACCES);
3625 ipd = osd_it_ipd_get(env, bag);
3626 if (likely(ipd != NULL)) {
3630 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3631 return (struct dt_it *)it;
3633 return ERR_PTR(-ENOMEM);
3637 * free given Iterator.
3640 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3642 struct osd_it_iam *it = (struct osd_it_iam *)di;
3643 struct osd_object *obj = it->oi_obj;
3645 iam_it_fini(&it->oi_it);
3646 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3647 lu_object_put(env, &obj->oo_dt.do_lu);
3651 * Move Iterator to record specified by \a key
3653 * \param di osd iterator
3654 * \param key key for index
3656 * \retval +ve di points to record with least key not larger than key
3657 * \retval 0 di points to exact matched key
3658 * \retval -ve failure
3661 static int osd_it_iam_get(const struct lu_env *env,
3662 struct dt_it *di, const struct dt_key *key)
3664 struct osd_it_iam *it = (struct osd_it_iam *)di;
3666 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3672 * \param di osd iterator
3675 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3677 struct osd_it_iam *it = (struct osd_it_iam *)di;
3679 iam_it_put(&it->oi_it);
3683 * Move iterator by one record
3685 * \param di osd iterator
3687 * \retval +1 end of container reached
3689 * \retval -ve failure
3692 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3694 struct osd_it_iam *it = (struct osd_it_iam *)di;
3696 return iam_it_next(&it->oi_it);
3700 * Return pointer to the key under iterator.
3703 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3704 const struct dt_it *di)
3706 struct osd_it_iam *it = (struct osd_it_iam *)di;
3708 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3712 * Return size of key under iterator (in bytes)
3715 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3717 struct osd_it_iam *it = (struct osd_it_iam *)di;
3719 return iam_it_key_size(&it->oi_it);
3722 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3723 int len, __u16 type)
3725 struct luda_type *lt;
3726 const unsigned align = sizeof(struct luda_type) - 1;
3728 /* check if file type is required */
3729 if (attr & LUDA_TYPE) {
3730 len = (len + align) & ~align;
3732 lt = (void *) ent->lde_name + len;
3733 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3734 ent->lde_attrs |= LUDA_TYPE;
3737 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3741 * build lu direct from backend fs dirent.
3744 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3745 struct lu_fid *fid, __u64 offset,
3746 char *name, __u16 namelen,
3747 __u16 type, __u32 attr)
3749 fid_cpu_to_le(&ent->lde_fid, fid);
3750 ent->lde_attrs = LUDA_FID;
3752 ent->lde_hash = cpu_to_le64(offset);
3753 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3755 strncpy(ent->lde_name, name, namelen);
3756 ent->lde_namelen = cpu_to_le16(namelen);
3758 /* append lustre attributes */
3759 osd_it_append_attrs(ent, attr, namelen, type);
3763 * Return pointer to the record under iterator.
3765 static int osd_it_iam_rec(const struct lu_env *env,
3766 const struct dt_it *di,
3767 struct dt_rec *dtrec, __u32 attr)
3769 struct osd_it_iam *it = (struct osd_it_iam *)di;
3770 struct osd_thread_info *info = osd_oti_get(env);
3773 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3774 const struct osd_fid_pack *rec;
3775 struct lu_fid *fid = &info->oti_fid;
3776 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3782 name = (char *)iam_it_key_get(&it->oi_it);
3784 RETURN(PTR_ERR(name));
3786 namelen = iam_it_key_size(&it->oi_it);
3788 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3790 RETURN(PTR_ERR(rec));
3792 rc = osd_fid_unpack(fid, rec);
3796 hash = iam_it_store(&it->oi_it);
3798 /* IAM does not store object type in IAM index (dir) */
3799 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3802 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3803 (struct iam_rec *)dtrec);
3810 * Returns cookie for current Iterator position.
3812 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3814 struct osd_it_iam *it = (struct osd_it_iam *)di;
3816 return iam_it_store(&it->oi_it);
3820 * Restore iterator from cookie.
3822 * \param di osd iterator
3823 * \param hash Iterator location cookie
3825 * \retval +ve di points to record with least key not larger than key.
3826 * \retval 0 di points to exact matched key
3827 * \retval -ve failure
3830 static int osd_it_iam_load(const struct lu_env *env,
3831 const struct dt_it *di, __u64 hash)
3833 struct osd_it_iam *it = (struct osd_it_iam *)di;
3835 return iam_it_load(&it->oi_it, hash);
3838 static const struct dt_index_operations osd_index_iam_ops = {
3839 .dio_lookup = osd_index_iam_lookup,
3840 .dio_declare_insert = osd_index_declare_iam_insert,
3841 .dio_insert = osd_index_iam_insert,
3842 .dio_declare_delete = osd_index_declare_iam_delete,
3843 .dio_delete = osd_index_iam_delete,
3845 .init = osd_it_iam_init,
3846 .fini = osd_it_iam_fini,
3847 .get = osd_it_iam_get,
3848 .put = osd_it_iam_put,
3849 .next = osd_it_iam_next,
3850 .key = osd_it_iam_key,
3851 .key_size = osd_it_iam_key_size,
3852 .rec = osd_it_iam_rec,
3853 .store = osd_it_iam_store,
3854 .load = osd_it_iam_load
3859 * Creates or initializes iterator context.
3861 * \retval struct osd_it_ea, iterator structure on success
3864 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3865 struct dt_object *dt,
3867 struct lustre_capa *capa)
3869 struct osd_object *obj = osd_dt_obj(dt);
3870 struct osd_thread_info *info = osd_oti_get(env);
3871 struct osd_it_ea *it = &info->oti_it_ea;
3872 struct lu_object *lo = &dt->do_lu;
3873 struct dentry *obj_dentry = &info->oti_it_dentry;
3875 LASSERT(lu_object_exists(lo));
3877 obj_dentry->d_inode = obj->oo_inode;
3878 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3879 obj_dentry->d_name.hash = 0;
3881 it->oie_rd_dirent = 0;
3882 it->oie_it_dirent = 0;
3883 it->oie_dirent = NULL;
3884 it->oie_buf = info->oti_it_ea_buf;
3886 it->oie_file.f_pos = 0;
3887 it->oie_file.f_dentry = obj_dentry;
3888 if (attr & LUDA_64BITHASH)
3889 it->oie_file.f_mode |= FMODE_64BITHASH;
3891 it->oie_file.f_mode |= FMODE_32BITHASH;
3892 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3893 it->oie_file.f_op = obj->oo_inode->i_fop;
3894 it->oie_file.private_data = NULL;
3896 RETURN((struct dt_it *) it);
3900 * Destroy or finishes iterator context.
3902 * \param di iterator structure to be destroyed
3904 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3906 struct osd_it_ea *it = (struct osd_it_ea *)di;
3907 struct osd_object *obj = it->oie_obj;
3908 struct inode *inode = obj->oo_inode;
3911 it->oie_file.f_op->release(inode, &it->oie_file);
3912 lu_object_put(env, &obj->oo_dt.do_lu);
3917 * It position the iterator at given key, so that next lookup continues from
3918 * that key Or it is similar to dio_it->load() but based on a key,
3919 * rather than file position.
3921 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3924 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3926 static int osd_it_ea_get(const struct lu_env *env,
3927 struct dt_it *di, const struct dt_key *key)
3929 struct osd_it_ea *it = (struct osd_it_ea *)di;
3932 LASSERT(((const char *)key)[0] == '\0');
3933 it->oie_file.f_pos = 0;
3934 it->oie_rd_dirent = 0;
3935 it->oie_it_dirent = 0;
3936 it->oie_dirent = NULL;
3944 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3949 * It is called internally by ->readdir(). It fills the
3950 * iterator's in-memory data structure with required
3951 * information i.e. name, namelen, rec_size etc.
3953 * \param buf in which information to be filled in.
3954 * \param name name of the file in given dir
3956 * \retval 0 on success
3957 * \retval 1 on buffer full
3959 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3960 loff_t offset, __u64 ino,
3963 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3964 struct osd_it_ea_dirent *ent = it->oie_dirent;
3965 struct lu_fid *fid = &ent->oied_fid;
3966 struct osd_fid_pack *rec;
3969 /* this should never happen */
3970 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3971 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3975 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3979 if (d_type & LDISKFS_DIRENT_LUFID) {
3980 rec = (struct osd_fid_pack*) (name + namelen + 1);
3982 if (osd_fid_unpack(fid, rec) != 0)
3985 d_type &= ~LDISKFS_DIRENT_LUFID;
3990 ent->oied_ino = ino;
3991 ent->oied_off = offset;
3992 ent->oied_namelen = namelen;
3993 ent->oied_type = d_type;
3995 memcpy(ent->oied_name, name, namelen);
3997 it->oie_rd_dirent++;
3998 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4003 * Calls ->readdir() to load a directory entry at a time
4004 * and stored it in iterator's in-memory data structure.
4006 * \param di iterator's in memory structure
4008 * \retval 0 on success
4009 * \retval -ve on error
4011 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4012 const struct dt_it *di)
4014 struct osd_it_ea *it = (struct osd_it_ea *)di;
4015 struct osd_object *obj = it->oie_obj;
4016 struct inode *inode = obj->oo_inode;
4017 struct htree_lock *hlock = NULL;
4021 it->oie_dirent = it->oie_buf;
4022 it->oie_rd_dirent = 0;
4024 if (obj->oo_hl_head != NULL) {
4025 hlock = osd_oti_get(env)->oti_hlock;
4026 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4027 inode, LDISKFS_HLOCK_READDIR);
4029 cfs_down_read(&obj->oo_ext_idx_sem);
4032 result = inode->i_fop->readdir(&it->oie_file, it,
4033 (filldir_t) osd_ldiskfs_filldir);
4036 ldiskfs_htree_unlock(hlock);
4038 cfs_up_read(&obj->oo_ext_idx_sem);
4040 if (it->oie_rd_dirent == 0) {
4043 it->oie_dirent = it->oie_buf;
4044 it->oie_it_dirent = 1;
4051 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4052 * to load a directory entry at a time and stored it in
4053 * iterator's in-memory data structure.
4055 * \param di iterator's in memory structure
4057 * \retval +ve iterator reached to end
4058 * \retval 0 iterator not reached to end
4059 * \retval -ve on error
4061 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4063 struct osd_it_ea *it = (struct osd_it_ea *)di;
4068 if (it->oie_it_dirent < it->oie_rd_dirent) {
4070 (void *) it->oie_dirent +
4071 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4072 it->oie_dirent->oied_namelen);
4073 it->oie_it_dirent++;
4076 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4079 rc = osd_ldiskfs_it_fill(env, di);
4086 * Returns the key at current position from iterator's in memory structure.
4088 * \param di iterator's in memory structure
4090 * \retval key i.e. struct dt_key on success
4092 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4093 const struct dt_it *di)
4095 struct osd_it_ea *it = (struct osd_it_ea *)di;
4097 return (struct dt_key *)it->oie_dirent->oied_name;
4101 * Returns the key's size at current position from iterator's in memory structure.
4103 * \param di iterator's in memory structure
4105 * \retval key_size i.e. struct dt_key on success
4107 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4109 struct osd_it_ea *it = (struct osd_it_ea *)di;
4111 return it->oie_dirent->oied_namelen;
4116 * Returns the value (i.e. fid/igif) at current position from iterator's
4117 * in memory structure.
4119 * \param di struct osd_it_ea, iterator's in memory structure
4120 * \param attr attr requested for dirent.
4121 * \param lde lustre dirent
4123 * \retval 0 no error and \param lde has correct lustre dirent.
4124 * \retval -ve on error
4126 static inline int osd_it_ea_rec(const struct lu_env *env,
4127 const struct dt_it *di,
4128 struct dt_rec *dtrec, __u32 attr)
4130 struct osd_it_ea *it = (struct osd_it_ea *)di;
4131 struct osd_object *obj = it->oie_obj;
4132 struct osd_device *dev = osd_obj2dev(obj);
4133 struct osd_scrub *scrub = &dev->od_scrub;
4134 struct scrub_file *sf = &scrub->os_file;
4135 struct osd_thread_info *oti = osd_oti_get(env);
4136 struct osd_idmap_cache *oic = &oti->oti_cache;
4137 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4138 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4139 __u32 ino = it->oie_dirent->oied_ino;
4143 if (!fid_is_sane(fid)) {
4144 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4148 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4151 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4152 it->oie_dirent->oied_name,
4153 it->oie_dirent->oied_namelen,
4154 it->oie_dirent->oied_type, attr);
4155 if (!fid_is_norm(fid)) {
4156 fid_zero(&oic->oic_fid);
4160 oic->oic_fid = *fid;
4161 if ((scrub->os_pos_current <= ino) &&
4162 (sf->sf_flags & SF_INCONSISTENT ||
4163 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4164 rc = osd_consistency_check(oti, dev, oic);
4170 * Returns a cookie for current position of the iterator head, so that
4171 * user can use this cookie to load/start the iterator next time.
4173 * \param di iterator's in memory structure
4175 * \retval cookie for current position, on success
4177 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4179 struct osd_it_ea *it = (struct osd_it_ea *)di;
4181 return it->oie_dirent->oied_off;
4185 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4186 * to load a directory entry at a time and stored it i inn,
4187 * in iterator's in-memory data structure.
4189 * \param di struct osd_it_ea, iterator's in memory structure
4191 * \retval +ve on success
4192 * \retval -ve on error
4194 static int osd_it_ea_load(const struct lu_env *env,
4195 const struct dt_it *di, __u64 hash)
4197 struct osd_it_ea *it = (struct osd_it_ea *)di;
4201 it->oie_file.f_pos = hash;
4203 rc = osd_ldiskfs_it_fill(env, di);
4211 * Index lookup function for interoperability mode (b11826).
4213 * \param key, key i.e. file name to be searched
4215 * \retval +ve, on success
4216 * \retval -ve, on error
4218 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4219 struct dt_rec *rec, const struct dt_key *key,
4220 struct lustre_capa *capa)
4222 struct osd_object *obj = osd_dt_obj(dt);
4227 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4228 LINVRNT(osd_invariant(obj));
4230 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4233 rc = osd_ea_lookup_rec(env, obj, rec, key);
4241 * Index and Iterator operations for interoperability
4242 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4244 static const struct dt_index_operations osd_index_ea_ops = {
4245 .dio_lookup = osd_index_ea_lookup,
4246 .dio_declare_insert = osd_index_declare_ea_insert,
4247 .dio_insert = osd_index_ea_insert,
4248 .dio_declare_delete = osd_index_declare_ea_delete,
4249 .dio_delete = osd_index_ea_delete,
4251 .init = osd_it_ea_init,
4252 .fini = osd_it_ea_fini,
4253 .get = osd_it_ea_get,
4254 .put = osd_it_ea_put,
4255 .next = osd_it_ea_next,
4256 .key = osd_it_ea_key,
4257 .key_size = osd_it_ea_key_size,
4258 .rec = osd_it_ea_rec,
4259 .store = osd_it_ea_store,
4260 .load = osd_it_ea_load
4264 static void *osd_key_init(const struct lu_context *ctx,
4265 struct lu_context_key *key)
4267 struct osd_thread_info *info;
4269 OBD_ALLOC_PTR(info);
4271 return ERR_PTR(-ENOMEM);
4273 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4274 if (info->oti_it_ea_buf == NULL)
4277 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4279 info->oti_hlock = ldiskfs_htree_lock_alloc();
4280 if (info->oti_hlock == NULL)
4286 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4289 return ERR_PTR(-ENOMEM);
4292 static void osd_key_fini(const struct lu_context *ctx,
4293 struct lu_context_key *key, void* data)
4295 struct osd_thread_info *info = data;
4297 if (info->oti_hlock != NULL)
4298 ldiskfs_htree_lock_free(info->oti_hlock);
4299 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4303 static void osd_key_exit(const struct lu_context *ctx,
4304 struct lu_context_key *key, void *data)
4306 struct osd_thread_info *info = data;
4308 LASSERT(info->oti_r_locks == 0);
4309 LASSERT(info->oti_w_locks == 0);
4310 LASSERT(info->oti_txns == 0);
4313 /* type constructor/destructor: osd_type_init, osd_type_fini */
4314 LU_TYPE_INIT_FINI(osd, &osd_key);
4316 struct lu_context_key osd_key = {
4317 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4318 .lct_init = osd_key_init,
4319 .lct_fini = osd_key_fini,
4320 .lct_exit = osd_key_exit
4324 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4325 const char *name, struct lu_device *next)
4327 struct osd_device *osd = osd_dev(d);
4329 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4330 return osd_procfs_init(osd, name);
4333 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4337 osd_scrub_cleanup(env, o);
4340 fsfilt_put_ops(o->od_fsops);
4344 /* shutdown quota slave instance associated with the device */
4345 if (o->od_quota_slave != NULL) {
4346 qsd_fini(env, o->od_quota_slave);
4347 o->od_quota_slave = NULL;
4353 static int osd_mount(const struct lu_env *env,
4354 struct osd_device *o, struct lustre_cfg *cfg)
4356 const char *name = lustre_cfg_string(cfg, 0);
4357 const char *dev = lustre_cfg_string(cfg, 1);
4359 unsigned long page, s_flags, lmd_flags = 0;
4360 struct page *__page;
4361 struct file_system_type *type;
4362 char *options = NULL;
4367 if (o->od_mnt != NULL)
4370 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4371 if (o->od_fsops == NULL) {
4372 CERROR("Can't find fsfilt_ldiskfs\n");
4376 OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
4380 str = lustre_cfg_string(cfg, 2);
4381 s_flags = simple_strtoul(str, NULL, 0);
4382 str = strstr(str, ":");
4384 lmd_flags = simple_strtoul(str + 1, NULL, 0);
4385 opts = lustre_cfg_string(cfg, 3);
4386 page = (unsigned long)cfs_page_address(__page);
4387 options = (char *)page;
4390 strcat(options, "user_xattr,acl");
4392 strcat(options, opts);
4394 /* Glom up mount options */
4395 if (*options != '\0')
4396 strcat(options, ",");
4397 strlcat(options, "no_mbcache", CFS_PAGE_SIZE);
4399 type = get_fs_type("ldiskfs");
4401 CERROR("%s: cannot find ldiskfs module\n", name);
4402 GOTO(out, rc = -ENODEV);
4405 o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
4406 cfs_module_put(type->owner);
4408 if (IS_ERR(o->od_mnt)) {
4409 rc = PTR_ERR(o->od_mnt);
4410 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
4415 if (lvfs_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
4416 CERROR("%s: underlying device %s is marked as read-only. "
4417 "Setup failed\n", name, dev);
4420 GOTO(out, rc = -EROFS);
4423 if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
4424 LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
4425 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
4428 GOTO(out, rc = -EINVAL);
4431 if (lmd_flags & LMD_FLG_IAM) {
4433 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", name);
4436 if (lmd_flags & LMD_FLG_NOSCRUB)
4437 o->od_scrub.os_no_scrub = 1;
4441 OBD_PAGE_FREE(__page);
4446 static struct lu_device *osd_device_fini(const struct lu_env *env,
4447 struct lu_device *d)
4452 rc = osd_shutdown(env, osd_dev(d));
4454 osd_compat_fini(osd_dev(d));
4456 shrink_dcache_sb(osd_sb(osd_dev(d)));
4457 osd_sync(env, lu2dt_dev(d));
4459 rc = osd_procfs_fini(osd_dev(d));
4461 CERROR("proc fini error %d \n", rc);
4462 RETURN (ERR_PTR(rc));
4465 if (osd_dev(d)->od_mnt) {
4466 mntput(osd_dev(d)->od_mnt);
4467 osd_dev(d)->od_mnt = NULL;
4473 static int osd_device_init0(const struct lu_env *env,
4474 struct osd_device *o,
4475 struct lustre_cfg *cfg)
4477 struct lu_device *l = osd2lu_dev(o);
4478 struct osd_thread_info *info;
4481 /* if the module was re-loaded, env can loose its keys */
4482 rc = lu_env_refill((struct lu_env *) env);
4485 info = osd_oti_get(env);
4488 l->ld_ops = &osd_lu_ops;
4489 o->od_dt_dev.dd_ops = &osd_dt_ops;
4491 cfs_spin_lock_init(&o->od_osfs_lock);
4492 cfs_mutex_init(&o->od_otable_mutex);
4493 o->od_osfs_age = cfs_time_shift_64(-1000);
4495 o->od_capa_hash = init_capa_hash();
4496 if (o->od_capa_hash == NULL)
4497 GOTO(out, rc = -ENOMEM);
4499 o->od_read_cache = 1;
4500 o->od_writethrough_cache = 1;
4502 rc = osd_mount(env, o, cfg);
4506 /* setup scrub, including OI files initialization */
4507 rc = osd_scrub_setup(env, o);
4511 strncpy(o->od_svname, lustre_cfg_string(cfg, 4),
4512 sizeof(o->od_svname) - 1);
4514 if (strstr(o->od_svname, "-OST")) {
4515 rc = osd_compat_init(o);
4520 rc = lu_site_init(&o->od_site, l);
4522 GOTO(out_compat, rc);
4524 rc = lu_site_init_finish(&o->od_site);
4526 GOTO(out_compat, rc);
4528 rc = osd_procfs_init(o, o->od_svname);
4530 CERROR("%s: can't initialize procfs: rc = %d\n",
4532 GOTO(out_compat, rc);
4535 LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
4541 osd_oi_fini(info, o);
4542 osd_shutdown(env, o);
4546 cleanup_capa_hash(o->od_capa_hash);
4551 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4552 struct lu_device_type *t,
4553 struct lustre_cfg *cfg)
4555 struct osd_device *o;
4560 return ERR_PTR(-ENOMEM);
4562 rc = dt_device_init(&o->od_dt_dev, t);
4564 rc = osd_device_init0(env, o, cfg);
4566 dt_device_fini(&o->od_dt_dev);
4569 if (unlikely(rc != 0))
4572 return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
4575 static struct lu_device *osd_device_free(const struct lu_env *env,
4576 struct lu_device *d)
4578 struct osd_device *o = osd_dev(d);
4581 cleanup_capa_hash(o->od_capa_hash);
4582 /* XXX: make osd top device in order to release reference */
4583 d->ld_site->ls_top_dev = d;
4584 lu_site_purge(env, d->ld_site, -1);
4585 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
4586 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
4587 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
4589 lu_site_fini(&o->od_site);
4590 dt_device_fini(&o->od_dt_dev);
4595 static int osd_process_config(const struct lu_env *env,
4596 struct lu_device *d, struct lustre_cfg *cfg)
4598 struct osd_device *o = osd_dev(d);
4602 switch(cfg->lcfg_command) {
4604 err = osd_mount(env, o, cfg);
4607 lu_dev_del_linkage(d->ld_site, d);
4617 static int osd_recovery_complete(const struct lu_env *env,
4618 struct lu_device *d)
4624 * we use exports to track all osd users
4626 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
4627 struct obd_device *obd, struct obd_uuid *cluuid,
4628 struct obd_connect_data *data, void *localdata)
4630 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4631 struct lustre_handle conn;
4635 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
4637 rc = class_connect(&conn, obd, cluuid);
4641 *exp = class_conn2export(&conn);
4643 cfs_spin_lock(&osd->od_osfs_lock);
4645 cfs_spin_unlock(&osd->od_osfs_lock);
4651 * once last export (we don't count self-export) disappeared
4652 * osd can be released
4654 static int osd_obd_disconnect(struct obd_export *exp)
4656 struct obd_device *obd = exp->exp_obd;
4657 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
4658 int rc, release = 0;
4661 /* Only disconnect the underlying layers on the final disconnect. */
4662 cfs_spin_lock(&osd->od_osfs_lock);
4664 if (osd->od_connects == 0)
4666 cfs_spin_unlock(&osd->od_osfs_lock);
4668 rc = class_disconnect(exp); /* bz 9811 */
4670 if (rc == 0 && release)
4671 class_manual_cleanup(obd);
4675 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4676 struct lu_device *dev)
4678 struct osd_device *osd = osd_dev(dev);
4682 /* 2. setup quota slave instance */
4683 osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
4684 osd->od_proc_entry);
4685 if (IS_ERR(osd->od_quota_slave)) {
4686 result = PTR_ERR(osd->od_quota_slave);
4687 osd->od_quota_slave = NULL;
4691 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 55, 0)
4692 /* Unfortunately, the current MDD implementation relies on some specific
4693 * code to be executed in the OSD layer. Since OFD now also uses the OSD
4694 * module, we need a way to skip the metadata-specific code when running
4696 * The hack here is to check the type of the parent device which is
4697 * either MD (i.e. MDD device) with the current MDT stack or DT (i.e.
4698 * OFD device) on an OST. As a reminder, obdfilter does not use the OSD
4699 * layer and still relies on lvfs. This hack won't work any more when
4700 * LOD is landed since LOD is of DT type.
4701 * This code should be removed once the orion MDT changes (LOD/OSP, ...)
4702 * have been landed */
4703 osd->od_is_md = lu_device_is_md(pdev);
4705 #warning "all is_md checks must be removed from osd-ldiskfs"
4711 /* 3. setup local objects */
4712 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4716 static const struct lu_object_operations osd_lu_obj_ops = {
4717 .loo_object_init = osd_object_init,
4718 .loo_object_delete = osd_object_delete,
4719 .loo_object_release = osd_object_release,
4720 .loo_object_free = osd_object_free,
4721 .loo_object_print = osd_object_print,
4722 .loo_object_invariant = osd_object_invariant
4725 const struct lu_device_operations osd_lu_ops = {
4726 .ldo_object_alloc = osd_object_alloc,
4727 .ldo_process_config = osd_process_config,
4728 .ldo_recovery_complete = osd_recovery_complete,
4729 .ldo_prepare = osd_prepare,
4732 static const struct lu_device_type_operations osd_device_type_ops = {
4733 .ldto_init = osd_type_init,
4734 .ldto_fini = osd_type_fini,
4736 .ldto_start = osd_type_start,
4737 .ldto_stop = osd_type_stop,
4739 .ldto_device_alloc = osd_device_alloc,
4740 .ldto_device_free = osd_device_free,
4742 .ldto_device_init = osd_device_init,
4743 .ldto_device_fini = osd_device_fini
4746 struct lu_device_type osd_device_type = {
4747 .ldt_tags = LU_DEVICE_DT,
4748 .ldt_name = LUSTRE_OSD_LDISKFS_NAME,
4749 .ldt_ops = &osd_device_type_ops,
4750 .ldt_ctx_tags = LCT_LOCAL,
4754 * lprocfs legacy support.
4756 static struct obd_ops osd_obd_device_ops = {
4757 .o_owner = THIS_MODULE,
4758 .o_connect = osd_obd_connect,
4759 .o_disconnect = osd_obd_disconnect
4762 static int __init osd_mod_init(void)
4764 struct lprocfs_static_vars lvars;
4767 lprocfs_osd_init_vars(&lvars);
4768 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4769 LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
4772 static void __exit osd_mod_exit(void)
4774 class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
4777 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4778 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
4779 MODULE_LICENSE("GPL");
4781 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);