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)
434 fid_zero(&oic->oic_fid);
437 * Objects are created as locking anchors or place holders for objects
438 * yet to be created. No need to osd_oi_lookup() at here because FID
439 * shouldn't never be re-used, if it's really a duplicate FID from
440 * unexpected reason, we should be able to detect it later by calling
441 * do_create->osd_oi_insert()
443 if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
444 GOTO(out, result = 0);
446 /* Search order: 3. OI files. */
447 result = osd_oi_lookup(info, dev, fid, id);
448 if (result == -ENOENT) {
449 if (!fid_is_norm(fid) ||
450 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
452 GOTO(out, result = 0);
462 inode = osd_iget(info, dev, id);
464 inode = osd_iget_verify(info, dev, id, fid);
466 result = PTR_ERR(inode);
467 if (result == -ENOENT || result == -ESTALE) {
469 } else if (result == -EREMCHG) {
472 if (thread_is_running(&scrub->os_thread)) {
473 result = -EINPROGRESS;
474 } else if (!scrub->os_no_scrub) {
475 result = osd_scrub_start(dev);
476 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
477 "for "DFID", rc = %d [1]\n",
478 LDISKFS_SB(osd_sb(dev))->s_es->\
479 s_volume_name,PFID(fid), result);
480 if (result == 0 || result == -EALREADY)
481 result = -EINPROGRESS;
490 obj->oo_inode = inode;
491 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
492 if (dev->od_iop_mode) {
493 obj->oo_compat_dot_created = 1;
494 obj->oo_compat_dotdot_created = 1;
497 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
498 GOTO(out, result = 0);
500 LASSERT(obj->oo_hl_head == NULL);
501 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
502 if (obj->oo_hl_head == NULL) {
503 obj->oo_inode = NULL;
505 GOTO(out, result = -ENOMEM);
507 GOTO(out, result = 0);
510 LINVRNT(osd_invariant(obj));
515 * Concurrency: shouldn't matter.
517 static void osd_object_init0(struct osd_object *obj)
519 LASSERT(obj->oo_inode != NULL);
520 obj->oo_dt.do_body_ops = &osd_body_ops;
521 obj->oo_dt.do_lu.lo_header->loh_attr |=
522 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
526 * Concurrency: no concurrent access is possible that early in object
529 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
530 const struct lu_object_conf *conf)
532 struct osd_object *obj = osd_obj(l);
535 LINVRNT(osd_invariant(obj));
537 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
538 obj->oo_dt.do_body_ops = &osd_body_ops_new;
540 if (obj->oo_inode != NULL) {
541 osd_object_init0(obj);
542 } else if (fid_is_otable_it(&l->lo_header->loh_fid)) {
543 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
544 /* LFSCK iterator object is special without inode */
545 l->lo_header->loh_attr |= LOHA_EXISTS;
548 LINVRNT(osd_invariant(obj));
553 * Concurrency: no concurrent access is possible that late in object
556 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
558 struct osd_object *obj = osd_obj(l);
560 LINVRNT(osd_invariant(obj));
562 dt_object_fini(&obj->oo_dt);
563 if (obj->oo_hl_head != NULL)
564 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
569 * Concurrency: no concurrent access is possible that late in object
572 static void osd_index_fini(struct osd_object *o)
574 struct iam_container *bag;
576 if (o->oo_dir != NULL) {
577 bag = &o->oo_dir->od_container;
578 if (o->oo_inode != NULL) {
579 if (bag->ic_object == o->oo_inode)
580 iam_container_fini(bag);
582 OBD_FREE_PTR(o->oo_dir);
588 * Concurrency: no concurrent access is possible that late in object
589 * life-cycle (for all existing callers, that is. New callers have to provide
590 * their own locking.)
592 static int osd_inode_unlinked(const struct inode *inode)
594 return inode->i_nlink == 0;
598 OSD_TXN_OI_DELETE_CREDITS = 20,
599 OSD_TXN_INODE_DELETE_CREDITS = 20
606 #if OSD_THANDLE_STATS
608 * Set time when the handle is allocated
610 static void osd_th_alloced(struct osd_thandle *oth)
612 oth->oth_alloced = cfs_time_current();
616 * Set time when the handle started
618 static void osd_th_started(struct osd_thandle *oth)
620 oth->oth_started = cfs_time_current();
624 * Helper function to convert time interval to microseconds packed in
625 * long int (default time units for the counter in "stats" initialized
626 * by lu_time_init() )
628 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
632 cfs_duration_usec(cfs_time_sub(end, start), &val);
633 return val.tv_sec * 1000000 + val.tv_usec;
637 * Check whether the we deal with this handle for too long.
639 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
640 cfs_time_t alloced, cfs_time_t started,
643 cfs_time_t now = cfs_time_current();
645 LASSERT(dev != NULL);
647 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
648 interval_to_usec(alloced, started));
649 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
650 interval_to_usec(started, closed));
651 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
652 interval_to_usec(closed, now));
654 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
655 CWARN("transaction handle %p was open for too long: "
657 "alloced "CFS_TIME_T" ,"
658 "started "CFS_TIME_T" ,"
659 "closed "CFS_TIME_T"\n",
660 oth, now, alloced, started, closed);
661 libcfs_debug_dumpstack(NULL);
665 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
667 cfs_time_t __closed = cfs_time_current(); \
668 cfs_time_t __alloced = oth->oth_alloced; \
669 cfs_time_t __started = oth->oth_started; \
672 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
675 #else /* OSD_THANDLE_STATS */
677 #define osd_th_alloced(h) do {} while(0)
678 #define osd_th_started(h) do {} while(0)
679 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
681 #endif /* OSD_THANDLE_STATS */
684 * Concurrency: doesn't access mutable data.
686 static int osd_param_is_sane(const struct osd_device *dev,
687 const struct thandle *th)
689 struct osd_thandle *oh;
690 oh = container_of0(th, struct osd_thandle, ot_super);
691 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
695 * Concurrency: shouldn't matter.
697 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
698 static void osd_trans_commit_cb(struct super_block *sb,
699 struct journal_callback *jcb, int error)
701 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
704 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
705 struct thandle *th = &oh->ot_super;
706 struct lu_device *lud = &th->th_dev->dd_lu_dev;
707 struct dt_txn_commit_cb *dcb, *tmp;
709 LASSERT(oh->ot_handle == NULL);
712 CERROR("transaction @0x%p commit error: %d\n", th, error);
714 dt_txn_hook_commit(th);
716 /* call per-transaction callbacks if any */
717 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
718 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
719 "commit callback entry: magic=%x name='%s'\n",
720 dcb->dcb_magic, dcb->dcb_name);
721 cfs_list_del_init(&dcb->dcb_linkage);
722 dcb->dcb_func(NULL, th, dcb, error);
725 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
729 lu_context_exit(&th->th_ctx);
730 lu_context_fini(&th->th_ctx);
734 static struct thandle *osd_trans_create(const struct lu_env *env,
737 struct osd_thread_info *oti = osd_oti_get(env);
738 struct osd_iobuf *iobuf = &oti->oti_iobuf;
739 struct osd_thandle *oh;
743 /* on pending IO in this thread should left from prev. request */
744 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
746 th = ERR_PTR(-ENOMEM);
747 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
752 th->th_tags = LCT_TX_HANDLE;
754 oti->oti_dev = osd_dt_dev(d);
755 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
762 * Concurrency: shouldn't matter.
764 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
767 struct osd_thread_info *oti = osd_oti_get(env);
768 struct osd_device *dev = osd_dt_dev(d);
770 struct osd_thandle *oh;
775 LASSERT(current->journal_info == NULL);
777 oh = container_of0(th, struct osd_thandle, ot_super);
779 LASSERT(oh->ot_handle == NULL);
781 rc = dt_txn_hook_start(env, d, th);
785 if (!osd_param_is_sane(dev, th)) {
786 CWARN("%.16s: too many transaction credits (%d > %d)\n",
787 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
789 osd_journal(dev)->j_max_transaction_buffers);
790 /* XXX Limit the credits to 'max_transaction_buffers', and
791 * let the underlying filesystem to catch the error if
792 * we really need so many credits.
794 * This should be removed when we can calculate the
795 * credits precisely. */
796 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
797 #ifdef OSD_TRACK_DECLARES
798 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
799 oh->ot_declare_attr_set, oh->ot_declare_punch,
800 oh->ot_declare_xattr_set);
801 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
802 oh->ot_declare_create, oh->ot_declare_ref_add,
803 oh->ot_declare_ref_del, oh->ot_declare_write);
804 CERROR(" insert: %d, delete: %d, destroy: %d\n",
805 oh->ot_declare_insert, oh->ot_declare_delete,
806 oh->ot_declare_destroy);
811 * XXX temporary stuff. Some abstraction layer should
814 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
818 LASSERT(oti->oti_txns == 0);
819 lu_context_init(&th->th_ctx, th->th_tags);
820 lu_context_enter(&th->th_ctx);
822 lu_device_get(&d->dd_lu_dev);
823 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
827 * XXX: current rule is that we first start tx,
828 * then lock object(s), but we can't use
829 * this rule for data (due to locking specifics
830 * in ldiskfs). also in long-term we'd like to
831 * use usually-used (locks;tx) ordering. so,
832 * UGLY thing is that we'll use one ordering for
833 * data (ofd) and reverse ordering for metadata
834 * (mdd). then at some point we'll fix the latter
837 LASSERT(oti->oti_r_locks == 0);
838 LASSERT(oti->oti_w_locks == 0);
851 * Concurrency: shouldn't matter.
853 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
856 struct osd_thandle *oh;
857 struct osd_thread_info *oti = osd_oti_get(env);
858 struct osd_iobuf *iobuf = &oti->oti_iobuf;
862 oh = container_of0(th, struct osd_thandle, ot_super);
864 if (oh->ot_handle != NULL) {
865 handle_t *hdl = oh->ot_handle;
867 hdl->h_sync = th->th_sync;
870 * add commit callback
871 * notice we don't do this in osd_trans_start()
872 * as underlying transaction can change during truncate
874 osd_journal_callback_set(hdl, osd_trans_commit_cb,
877 LASSERT(oti->oti_txns == 1);
880 * XXX: current rule is that we first start tx,
881 * then lock object(s), but we can't use
882 * this rule for data (due to locking specifics
883 * in ldiskfs). also in long-term we'd like to
884 * use usually-used (locks;tx) ordering. so,
885 * UGLY thing is that we'll use one ordering for
886 * data (ofd) and reverse ordering for metadata
887 * (mdd). then at some point we'll fix the latter
889 if (osd_dt_dev(th->th_dev)->od_is_md) {
890 LASSERT(oti->oti_r_locks == 0);
891 LASSERT(oti->oti_w_locks == 0);
893 rc = dt_txn_hook_stop(env, th);
895 CERROR("Failure in transaction hook: %d\n", rc);
896 oh->ot_handle = NULL;
897 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
898 rc = ldiskfs_journal_stop(hdl));
900 CERROR("Failure to stop transaction: %d\n", rc);
905 /* as we want IO to journal and data IO be concurrent, we don't block
906 * awaiting data IO completion in osd_do_bio(), instead we wait here
907 * once transaction is submitted to the journal. all reqular requests
908 * don't do direct IO (except read/write), thus this wait_event becomes
911 * IMPORTANT: we have to wait till any IO submited by the thread is
912 * completed otherwise iobuf may be corrupted by different request
914 cfs_wait_event(iobuf->dr_wait,
915 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
917 rc = iobuf->dr_error;
922 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
924 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
927 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
928 LASSERT(&dcb->dcb_func != NULL);
929 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
935 * Called just before object is freed. Releases all resources except for
936 * object itself (that is released by osd_object_free()).
938 * Concurrency: no concurrent access is possible that late in object
941 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
943 struct osd_object *obj = osd_obj(l);
944 struct inode *inode = obj->oo_inode;
946 LINVRNT(osd_invariant(obj));
949 * If object is unlinked remove fid->ino mapping from object index.
955 obj->oo_inode = NULL;
960 * Concurrency: ->loo_object_release() is called under site spin-lock.
962 static void osd_object_release(const struct lu_env *env,
968 * Concurrency: shouldn't matter.
970 static int osd_object_print(const struct lu_env *env, void *cookie,
971 lu_printer_t p, const struct lu_object *l)
973 struct osd_object *o = osd_obj(l);
976 if (o->oo_dir != NULL)
977 d = o->oo_dir->od_container.ic_descr;
980 return (*p)(env, cookie, LUSTRE_OSD_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_mount == 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_max_name_len = LDISKFS_NAME_LEN;
1043 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1044 param->ddp_block_shift = sb->s_blocksize_bits;
1045 param->ddp_mntopts = 0;
1046 if (test_opt(sb, XATTR_USER))
1047 param->ddp_mntopts |= MNTOPT_USERXATTR;
1048 if (test_opt(sb, POSIX_ACL))
1049 param->ddp_mntopts |= MNTOPT_ACL;
1051 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1052 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1053 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1056 param->ddp_max_ea_size = sb->s_blocksize;
1061 * Helper function to get and fill the buffer with input values.
1063 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1067 buf = &osd_oti_get(env)->oti_buf;
1074 * Concurrency: shouldn't matter.
1076 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1078 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1079 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1083 * Start commit for OSD device.
1085 * An implementation of dt_commit_async method for OSD device.
1086 * Asychronously starts underlayng fs sync and thereby a transaction
1089 * \param env environment
1090 * \param d dt device
1092 * \see dt_device_operations
1094 static int osd_commit_async(const struct lu_env *env,
1095 struct dt_device *d)
1097 struct super_block *s = osd_sb(osd_dt_dev(d));
1100 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1101 RETURN(s->s_op->sync_fs(s, 0));
1105 * Concurrency: shouldn't matter.
1108 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1110 struct super_block *sb = osd_sb(osd_dt_dev(d));
1114 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1116 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1121 * Concurrency: serialization provided by callers.
1123 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1124 int mode, unsigned long timeout, __u32 alg,
1125 struct lustre_capa_key *keys)
1127 struct osd_device *dev = osd_dt_dev(d);
1130 dev->od_fl_capa = mode;
1131 dev->od_capa_timeout = timeout;
1132 dev->od_capa_alg = alg;
1133 dev->od_capa_keys = keys;
1138 * Concurrency: serialization provided by callers.
1140 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1141 struct dt_quota_ctxt *ctxt, void *data)
1143 struct obd_device *obd = (void *)ctxt;
1144 struct vfsmount *mnt = (struct vfsmount *)data;
1147 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1148 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1149 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1150 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1151 obd->obd_lvfs_ctxt.fs = get_ds();
1157 * Note: we do not count into QUOTA here.
1158 * If we mount with --data_journal we may need more.
1160 const int osd_dto_credits_noquota[DTO_NR] = {
1163 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1164 * SINGLEDATA_TRANS_BLOCKS(8)
1165 * XXX Note: maybe iam need more, since iam have more level than
1168 [DTO_INDEX_INSERT] = 16,
1169 [DTO_INDEX_DELETE] = 16,
1173 [DTO_INDEX_UPDATE] = 16,
1175 * Create a object. The same as create object in EXT3.
1176 * DATA_TRANS_BLOCKS(14) +
1177 * INDEX_EXTRA_BLOCKS(8) +
1178 * 3(inode bits, groups, GDT)
1180 [DTO_OBJECT_CREATE] = 25,
1182 * XXX: real credits to be fixed
1184 [DTO_OBJECT_DELETE] = 25,
1186 * Attr set credits (inode)
1188 [DTO_ATTR_SET_BASE] = 1,
1190 * Xattr set. The same as xattr of EXT3.
1191 * DATA_TRANS_BLOCKS(14)
1192 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1193 * are also counted in. Do not know why?
1195 [DTO_XATTR_SET] = 14,
1198 * credits for inode change during write.
1200 [DTO_WRITE_BASE] = 3,
1202 * credits for single block write.
1204 [DTO_WRITE_BLOCK] = 14,
1206 * Attr set credits for chown.
1207 * This is extra credits for setattr, and it is null without quota
1209 [DTO_ATTR_SET_CHOWN]= 0
1212 static const struct dt_device_operations osd_dt_ops = {
1213 .dt_root_get = osd_root_get,
1214 .dt_statfs = osd_statfs,
1215 .dt_trans_create = osd_trans_create,
1216 .dt_trans_start = osd_trans_start,
1217 .dt_trans_stop = osd_trans_stop,
1218 .dt_trans_cb_add = osd_trans_cb_add,
1219 .dt_conf_get = osd_conf_get,
1220 .dt_sync = osd_sync,
1222 .dt_commit_async = osd_commit_async,
1223 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1224 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1227 static void osd_object_read_lock(const struct lu_env *env,
1228 struct dt_object *dt, unsigned role)
1230 struct osd_object *obj = osd_dt_obj(dt);
1231 struct osd_thread_info *oti = osd_oti_get(env);
1233 LINVRNT(osd_invariant(obj));
1235 LASSERT(obj->oo_owner != env);
1236 cfs_down_read_nested(&obj->oo_sem, role);
1238 LASSERT(obj->oo_owner == NULL);
1242 static void osd_object_write_lock(const struct lu_env *env,
1243 struct dt_object *dt, unsigned role)
1245 struct osd_object *obj = osd_dt_obj(dt);
1246 struct osd_thread_info *oti = osd_oti_get(env);
1248 LINVRNT(osd_invariant(obj));
1250 LASSERT(obj->oo_owner != env);
1251 cfs_down_write_nested(&obj->oo_sem, role);
1253 LASSERT(obj->oo_owner == NULL);
1254 obj->oo_owner = env;
1258 static void osd_object_read_unlock(const struct lu_env *env,
1259 struct dt_object *dt)
1261 struct osd_object *obj = osd_dt_obj(dt);
1262 struct osd_thread_info *oti = osd_oti_get(env);
1264 LINVRNT(osd_invariant(obj));
1266 LASSERT(oti->oti_r_locks > 0);
1268 cfs_up_read(&obj->oo_sem);
1271 static void osd_object_write_unlock(const struct lu_env *env,
1272 struct dt_object *dt)
1274 struct osd_object *obj = osd_dt_obj(dt);
1275 struct osd_thread_info *oti = osd_oti_get(env);
1277 LINVRNT(osd_invariant(obj));
1279 LASSERT(obj->oo_owner == env);
1280 LASSERT(oti->oti_w_locks > 0);
1282 obj->oo_owner = NULL;
1283 cfs_up_write(&obj->oo_sem);
1286 static int osd_object_write_locked(const struct lu_env *env,
1287 struct dt_object *dt)
1289 struct osd_object *obj = osd_dt_obj(dt);
1291 LINVRNT(osd_invariant(obj));
1293 return obj->oo_owner == env;
1296 static int capa_is_sane(const struct lu_env *env,
1297 struct osd_device *dev,
1298 struct lustre_capa *capa,
1299 struct lustre_capa_key *keys)
1301 struct osd_thread_info *oti = osd_oti_get(env);
1302 struct lustre_capa *tcapa = &oti->oti_capa;
1303 struct obd_capa *oc;
1307 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1309 if (capa_is_expired(oc)) {
1310 DEBUG_CAPA(D_ERROR, capa, "expired");
1317 if (capa_is_expired_sec(capa)) {
1318 DEBUG_CAPA(D_ERROR, capa, "expired");
1322 cfs_spin_lock(&capa_lock);
1323 for (i = 0; i < 2; i++) {
1324 if (keys[i].lk_keyid == capa->lc_keyid) {
1325 oti->oti_capa_key = keys[i];
1329 cfs_spin_unlock(&capa_lock);
1332 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1336 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1340 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1341 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1345 oc = capa_add(dev->od_capa_hash, capa);
1351 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1352 struct lustre_capa *capa, __u64 opc)
1354 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1355 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1356 struct md_capainfo *ci;
1359 if (!dev->od_fl_capa)
1362 if (capa == BYPASS_CAPA)
1365 ci = md_capainfo(env);
1369 if (ci->mc_auth == LC_ID_NONE)
1373 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1377 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1378 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1383 if (!capa_opc_supported(capa, opc)) {
1384 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1388 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1389 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1396 static struct timespec *osd_inode_time(const struct lu_env *env,
1397 struct inode *inode, __u64 seconds)
1399 struct osd_thread_info *oti = osd_oti_get(env);
1400 struct timespec *t = &oti->oti_time;
1402 t->tv_sec = seconds;
1404 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1409 static void osd_inode_getattr(const struct lu_env *env,
1410 struct inode *inode, struct lu_attr *attr)
1412 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1413 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1414 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1416 attr->la_atime = LTIME_S(inode->i_atime);
1417 attr->la_mtime = LTIME_S(inode->i_mtime);
1418 attr->la_ctime = LTIME_S(inode->i_ctime);
1419 attr->la_mode = inode->i_mode;
1420 attr->la_size = i_size_read(inode);
1421 attr->la_blocks = inode->i_blocks;
1422 attr->la_uid = inode->i_uid;
1423 attr->la_gid = inode->i_gid;
1424 attr->la_flags = LDISKFS_I(inode)->i_flags;
1425 attr->la_nlink = inode->i_nlink;
1426 attr->la_rdev = inode->i_rdev;
1427 attr->la_blksize = ll_inode_blksize(inode);
1428 attr->la_blkbits = inode->i_blkbits;
1431 static int osd_attr_get(const struct lu_env *env,
1432 struct dt_object *dt,
1433 struct lu_attr *attr,
1434 struct lustre_capa *capa)
1436 struct osd_object *obj = osd_dt_obj(dt);
1438 LASSERT(dt_object_exists(dt));
1439 LINVRNT(osd_invariant(obj));
1441 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1444 cfs_spin_lock(&obj->oo_guard);
1445 osd_inode_getattr(env, obj->oo_inode, attr);
1446 cfs_spin_unlock(&obj->oo_guard);
1450 static int osd_declare_attr_set(const struct lu_env *env,
1451 struct dt_object *dt,
1452 const struct lu_attr *attr,
1453 struct thandle *handle)
1455 struct osd_thandle *oh;
1456 struct osd_object *obj;
1458 LASSERT(dt != NULL);
1459 LASSERT(handle != NULL);
1461 obj = osd_dt_obj(dt);
1462 LASSERT(osd_invariant(obj));
1464 oh = container_of0(handle, struct osd_thandle, ot_super);
1465 LASSERT(oh->ot_handle == NULL);
1467 OSD_DECLARE_OP(oh, attr_set);
1468 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1470 if (attr && attr->la_valid & LA_UID) {
1472 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1474 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1476 if (attr && attr->la_valid & LA_GID) {
1478 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1480 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1486 static int osd_inode_setattr(const struct lu_env *env,
1487 struct inode *inode, const struct lu_attr *attr)
1491 bits = attr->la_valid;
1493 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1495 if (bits & LA_ATIME)
1496 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1497 if (bits & LA_CTIME)
1498 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1499 if (bits & LA_MTIME)
1500 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1501 if (bits & LA_SIZE) {
1502 LDISKFS_I(inode)->i_disksize = attr->la_size;
1503 i_size_write(inode, attr->la_size);
1507 /* OSD should not change "i_blocks" which is used by quota.
1508 * "i_blocks" should be changed by ldiskfs only. */
1509 if (bits & LA_BLOCKS)
1510 inode->i_blocks = attr->la_blocks;
1513 inode->i_mode = (inode->i_mode & S_IFMT) |
1514 (attr->la_mode & ~S_IFMT);
1516 inode->i_uid = attr->la_uid;
1518 inode->i_gid = attr->la_gid;
1519 if (bits & LA_NLINK)
1520 inode->i_nlink = attr->la_nlink;
1522 inode->i_rdev = attr->la_rdev;
1524 if (bits & LA_FLAGS) {
1525 /* always keep S_NOCMTIME */
1526 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1532 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1534 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1535 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1540 if (attr->la_valid & LA_UID)
1541 iattr.ia_valid |= ATTR_UID;
1542 if (attr->la_valid & LA_GID)
1543 iattr.ia_valid |= ATTR_GID;
1544 iattr.ia_uid = attr->la_uid;
1545 iattr.ia_gid = attr->la_gid;
1547 rc = ll_vfs_dq_transfer(inode, &iattr);
1549 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1550 "enforcement enabled on the ldiskfs filesystem?",
1551 inode->i_sb->s_id, rc);
1558 static int osd_attr_set(const struct lu_env *env,
1559 struct dt_object *dt,
1560 const struct lu_attr *attr,
1561 struct thandle *handle,
1562 struct lustre_capa *capa)
1564 struct osd_object *obj = osd_dt_obj(dt);
1565 struct inode *inode;
1568 LASSERT(handle != NULL);
1569 LASSERT(dt_object_exists(dt));
1570 LASSERT(osd_invariant(obj));
1572 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1575 OSD_EXEC_OP(handle, attr_set);
1577 inode = obj->oo_inode;
1578 if (!osd_dt_dev(handle->th_dev)->od_is_md) {
1580 rc = osd_quota_transfer(inode, attr);
1584 #ifdef HAVE_QUOTA_SUPPORT
1585 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1586 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1587 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1592 if (attr->la_valid & LA_UID)
1593 iattr.ia_valid |= ATTR_UID;
1594 if (attr->la_valid & LA_GID)
1595 iattr.ia_valid |= ATTR_GID;
1596 iattr.ia_uid = attr->la_uid;
1597 iattr.ia_gid = attr->la_gid;
1598 osd_push_ctxt(env, save, 1);
1599 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1600 osd_pop_ctxt(save, 1);
1606 cfs_spin_lock(&obj->oo_guard);
1607 rc = osd_inode_setattr(env, inode, attr);
1608 cfs_spin_unlock(&obj->oo_guard);
1611 inode->i_sb->s_op->dirty_inode(inode);
1615 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1616 struct osd_object *obj,
1617 const char *name, const int namelen)
1619 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1622 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1624 struct dt_allocation_hint *hint,
1628 struct osd_device *osd = osd_obj2dev(obj);
1629 struct osd_thandle *oth;
1630 struct dt_object *parent = NULL;
1631 struct inode *inode;
1632 #ifdef HAVE_QUOTA_SUPPORT
1633 struct osd_ctxt *save = &info->oti_ctxt;
1636 LINVRNT(osd_invariant(obj));
1637 LASSERT(obj->oo_inode == NULL);
1638 LASSERT(obj->oo_hl_head == NULL);
1640 if (S_ISDIR(mode) && ldiskfs_pdo) {
1641 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1642 if (obj->oo_hl_head == NULL)
1646 oth = container_of(th, struct osd_thandle, ot_super);
1647 LASSERT(oth->ot_handle->h_transaction != NULL);
1649 if (hint && hint->dah_parent)
1650 parent = hint->dah_parent;
1652 #ifdef HAVE_QUOTA_SUPPORT
1653 osd_push_ctxt(info->oti_env, save, osd_dt_dev(th->th_dev)->od_is_md);
1655 inode = ldiskfs_create_inode(oth->ot_handle,
1656 parent ? osd_dt_obj(parent)->oo_inode :
1657 osd_sb(osd)->s_root->d_inode,
1659 #ifdef HAVE_QUOTA_SUPPORT
1660 osd_pop_ctxt(save, osd_dt_dev(th->th_dev)->od_is_md);
1662 if (!IS_ERR(inode)) {
1663 /* Do not update file c/mtime in ldiskfs.
1664 * NB: don't need any lock because no contention at this
1666 inode->i_flags |= S_NOCMTIME;
1667 inode->i_state |= I_LUSTRE_NOSCRUB;
1668 obj->oo_inode = inode;
1671 if (obj->oo_hl_head != NULL) {
1672 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1673 obj->oo_hl_head = NULL;
1675 result = PTR_ERR(inode);
1677 LINVRNT(osd_invariant(obj));
1685 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1686 struct lu_attr *attr,
1687 struct dt_allocation_hint *hint,
1688 struct dt_object_format *dof,
1692 struct osd_thandle *oth;
1693 struct osd_device *osd = osd_obj2dev(obj);
1694 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1696 LASSERT(S_ISDIR(attr->la_mode));
1698 oth = container_of(th, struct osd_thandle, ot_super);
1699 LASSERT(oth->ot_handle->h_transaction != NULL);
1700 result = osd_mkfile(info, obj, mode, hint, th);
1701 if (result == 0 && osd->od_iop_mode == 0) {
1702 LASSERT(obj->oo_inode != NULL);
1704 * XXX uh-oh... call low-level iam function directly.
1707 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1708 sizeof (struct osd_fid_pack),
1714 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1715 struct lu_attr *attr,
1716 struct dt_allocation_hint *hint,
1717 struct dt_object_format *dof,
1721 struct osd_thandle *oth;
1722 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1724 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1726 LASSERT(S_ISREG(attr->la_mode));
1728 oth = container_of(th, struct osd_thandle, ot_super);
1729 LASSERT(oth->ot_handle->h_transaction != NULL);
1731 result = osd_mkfile(info, obj, mode, hint, th);
1733 LASSERT(obj->oo_inode != NULL);
1734 if (feat->dif_flags & DT_IND_VARKEY)
1735 result = iam_lvar_create(obj->oo_inode,
1736 feat->dif_keysize_max,
1738 feat->dif_recsize_max,
1741 result = iam_lfix_create(obj->oo_inode,
1742 feat->dif_keysize_max,
1744 feat->dif_recsize_max,
1751 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1752 struct lu_attr *attr,
1753 struct dt_allocation_hint *hint,
1754 struct dt_object_format *dof,
1757 LASSERT(S_ISREG(attr->la_mode));
1758 return osd_mkfile(info, obj, (attr->la_mode &
1759 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1762 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1763 struct lu_attr *attr,
1764 struct dt_allocation_hint *hint,
1765 struct dt_object_format *dof,
1768 LASSERT(S_ISLNK(attr->la_mode));
1769 return osd_mkfile(info, obj, (attr->la_mode &
1770 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1773 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1774 struct lu_attr *attr,
1775 struct dt_allocation_hint *hint,
1776 struct dt_object_format *dof,
1779 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1782 LINVRNT(osd_invariant(obj));
1783 LASSERT(obj->oo_inode == NULL);
1784 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1785 S_ISFIFO(mode) || S_ISSOCK(mode));
1787 result = osd_mkfile(info, obj, mode, hint, th);
1789 LASSERT(obj->oo_inode != NULL);
1791 * This inode should be marked dirty for i_rdev. Currently
1792 * that is done in the osd_attr_init().
1794 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1796 LINVRNT(osd_invariant(obj));
1800 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1802 struct dt_allocation_hint *hint,
1803 struct dt_object_format *dof,
1806 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1808 osd_obj_type_f result;
1824 result = osd_mk_index;
1835 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1836 struct dt_object *parent, cfs_umode_t child_mode)
1840 memset(ah, 0, sizeof(*ah));
1841 ah->dah_parent = parent;
1842 ah->dah_mode = child_mode;
1845 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1846 struct lu_attr *attr, struct dt_object_format *dof)
1848 struct inode *inode = obj->oo_inode;
1849 __u64 valid = attr->la_valid;
1852 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1854 if (dof->dof_type != DFT_NODE)
1855 attr->la_valid &= ~LA_RDEV;
1856 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1857 attr->la_valid &= ~LA_ATIME;
1858 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1859 attr->la_valid &= ~LA_CTIME;
1860 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1861 attr->la_valid &= ~LA_MTIME;
1863 if (!osd_obj2dev(obj)->od_is_md) {
1865 result = osd_quota_transfer(inode, attr);
1869 #ifdef HAVE_QUOTA_SUPPORT
1870 attr->la_valid &= ~(LA_UID | LA_GID);
1874 if (attr->la_valid != 0) {
1875 result = osd_inode_setattr(info->oti_env, inode, attr);
1877 * The osd_inode_setattr() should always succeed here. The
1878 * only error that could be returned is EDQUOT when we are
1879 * trying to change the UID or GID of the inode. However, this
1880 * should not happen since quota enforcement is no longer
1881 * enabled on ldiskfs (lquota takes care of it).
1883 LASSERTF(result == 0, "%d", result);
1884 inode->i_sb->s_op->dirty_inode(inode);
1887 attr->la_valid = valid;
1891 * Helper function for osd_object_create()
1893 * \retval 0, on success
1895 static int __osd_object_create(struct osd_thread_info *info,
1896 struct osd_object *obj, struct lu_attr *attr,
1897 struct dt_allocation_hint *hint,
1898 struct dt_object_format *dof,
1904 /* we drop umask so that permissions we pass are not affected */
1905 umask = current->fs->umask;
1906 current->fs->umask = 0;
1908 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1911 osd_attr_init(info, obj, attr, dof);
1912 osd_object_init0(obj);
1914 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1915 unlock_new_inode(obj->oo_inode);
1918 /* restore previous umask value */
1919 current->fs->umask = umask;
1925 * Helper function for osd_object_create()
1927 * \retval 0, on success
1929 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1930 const struct lu_fid *fid, struct thandle *th)
1932 struct osd_thread_info *info = osd_oti_get(env);
1933 struct osd_inode_id *id = &info->oti_id;
1934 struct osd_device *osd = osd_obj2dev(obj);
1936 LASSERT(obj->oo_inode != NULL);
1938 if (osd->od_is_md) {
1939 struct md_ucred *uc = md_ucred(env);
1940 LASSERT(uc != NULL);
1943 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1944 return osd_oi_insert(info, osd, fid, id, th);
1947 static int osd_declare_object_create(const struct lu_env *env,
1948 struct dt_object *dt,
1949 struct lu_attr *attr,
1950 struct dt_allocation_hint *hint,
1951 struct dt_object_format *dof,
1952 struct thandle *handle)
1954 struct osd_thandle *oh;
1956 LASSERT(handle != NULL);
1958 oh = container_of0(handle, struct osd_thandle, ot_super);
1959 LASSERT(oh->ot_handle == NULL);
1961 OSD_DECLARE_OP(oh, create);
1962 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1963 /* XXX: So far, only normal fid needs be inserted into the oi,
1964 * things could be changed later. Revise following code then. */
1965 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1966 OSD_DECLARE_OP(oh, insert);
1967 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1969 /* If this is directory, then we expect . and .. to be inserted as
1970 * well. The one directory block always needs to be created for the
1971 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1972 * block), there is no danger of needing a tree for the first block.
1974 if (attr && S_ISDIR(attr->la_mode)) {
1975 OSD_DECLARE_OP(oh, insert);
1976 OSD_DECLARE_OP(oh, insert);
1977 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1981 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1982 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1987 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1988 struct lu_attr *attr,
1989 struct dt_allocation_hint *hint,
1990 struct dt_object_format *dof,
1993 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1994 struct osd_object *obj = osd_dt_obj(dt);
1995 struct osd_thread_info *info = osd_oti_get(env);
2000 LINVRNT(osd_invariant(obj));
2001 LASSERT(!dt_object_exists(dt));
2002 LASSERT(osd_write_locked(env, obj));
2003 LASSERT(th != NULL);
2005 if (unlikely(fid_is_acct(fid)))
2006 /* Quota files can't be created from the kernel any more,
2007 * 'tune2fs -O quota' will take care of creating them */
2010 OSD_EXEC_OP(th, create);
2012 result = __osd_object_create(info, obj, attr, hint, dof, th);
2014 result = __osd_oi_insert(env, obj, fid, th);
2016 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2017 LASSERT(osd_invariant(obj));
2022 * Called to destroy on-disk representation of the object
2024 * Concurrency: must be locked
2026 static int osd_declare_object_destroy(const struct lu_env *env,
2027 struct dt_object *dt,
2030 struct osd_object *obj = osd_dt_obj(dt);
2031 struct inode *inode = obj->oo_inode;
2032 struct osd_thandle *oh;
2036 oh = container_of0(th, struct osd_thandle, ot_super);
2037 LASSERT(oh->ot_handle == NULL);
2040 OSD_DECLARE_OP(oh, destroy);
2041 OSD_DECLARE_OP(oh, delete);
2042 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2043 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2045 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
2046 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
2051 static int osd_object_destroy(const struct lu_env *env,
2052 struct dt_object *dt,
2055 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2056 struct osd_object *obj = osd_dt_obj(dt);
2057 struct inode *inode = obj->oo_inode;
2058 struct osd_device *osd = osd_obj2dev(obj);
2059 struct osd_thandle *oh;
2063 oh = container_of0(th, struct osd_thandle, ot_super);
2064 LASSERT(oh->ot_handle);
2066 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2068 if (unlikely(fid_is_acct(fid)))
2071 /* Parallel control for OI scrub. For most of cases, there is no
2072 * lock contention. So it will not affect unlink performance. */
2073 cfs_mutex_lock(&inode->i_mutex);
2074 if (S_ISDIR(inode->i_mode)) {
2075 LASSERT(osd_inode_unlinked(inode) ||
2076 inode->i_nlink == 1);
2077 cfs_spin_lock(&obj->oo_guard);
2079 cfs_spin_unlock(&obj->oo_guard);
2080 inode->i_sb->s_op->dirty_inode(inode);
2082 LASSERT(osd_inode_unlinked(inode));
2085 OSD_EXEC_OP(th, destroy);
2087 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2088 cfs_mutex_unlock(&inode->i_mutex);
2090 /* XXX: add to ext3 orphan list */
2091 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2093 /* not needed in the cache anymore */
2094 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2100 * Helper function for osd_xattr_set()
2102 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2103 const struct lu_buf *buf, const char *name, int fl)
2105 struct osd_object *obj = osd_dt_obj(dt);
2106 struct inode *inode = obj->oo_inode;
2107 struct osd_thread_info *info = osd_oti_get(env);
2108 struct dentry *dentry = &info->oti_child_dentry;
2112 LASSERT(dt_object_exists(dt));
2113 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2115 if (fl & LU_XATTR_REPLACE)
2116 fs_flags |= XATTR_REPLACE;
2118 if (fl & LU_XATTR_CREATE)
2119 fs_flags |= XATTR_CREATE;
2121 dentry->d_inode = inode;
2122 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2123 buf->lb_len, fs_flags);
2128 * Put the fid into lustre_mdt_attrs, and then place the structure
2129 * inode's ea. This fid should not be altered during the life time
2132 * \retval +ve, on success
2133 * \retval -ve, on error
2135 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2137 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2138 const struct lu_fid *fid)
2140 struct osd_thread_info *info = osd_oti_get(env);
2141 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2143 lustre_lma_init(mdt_attrs, fid);
2144 lustre_lma_swab(mdt_attrs);
2145 return __osd_xattr_set(env, dt,
2146 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2147 XATTR_NAME_LMA, LU_XATTR_CREATE);
2152 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2153 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2154 * To have compatilibility with 1.8 ldiskfs driver we need to have
2155 * magic number at start of fid data.
2156 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2159 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2160 const struct dt_rec *fid)
2162 param->edp_magic = LDISKFS_LUFID_MAGIC;
2163 param->edp_len = sizeof(struct lu_fid) + 1;
2165 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2166 (struct lu_fid *)fid);
2170 * Try to read the fid from inode ea into dt_rec, if return value
2171 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2173 * \param fid object fid.
2175 * \retval 0 on success
2177 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2178 __u32 ino, struct lu_fid *fid,
2179 struct osd_inode_id *id)
2181 struct osd_thread_info *info = osd_oti_get(env);
2182 struct inode *inode;
2185 osd_id_gen(id, ino, OSD_OII_NOGEN);
2186 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2188 RETURN(PTR_ERR(inode));
2195 * OSD layer object create function for interoperability mode (b11826).
2196 * This is mostly similar to osd_object_create(). Only difference being, fid is
2197 * inserted into inode ea here.
2199 * \retval 0, on success
2200 * \retval -ve, on error
2202 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2203 struct lu_attr *attr,
2204 struct dt_allocation_hint *hint,
2205 struct dt_object_format *dof,
2208 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2209 struct osd_object *obj = osd_dt_obj(dt);
2210 struct osd_thread_info *info = osd_oti_get(env);
2215 LASSERT(osd_invariant(obj));
2216 LASSERT(!dt_object_exists(dt));
2217 LASSERT(osd_write_locked(env, obj));
2218 LASSERT(th != NULL);
2220 if (unlikely(fid_is_acct(fid)))
2221 /* Quota files can't be created from the kernel any more,
2222 * 'tune2fs -O quota' will take care of creating them */
2225 OSD_EXEC_OP(th, create);
2227 result = __osd_object_create(info, obj, attr, hint, dof, th);
2228 /* objects under osd root shld have igif fid, so dont add fid EA */
2229 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2230 result = osd_ea_fid_set(env, dt, fid);
2233 result = __osd_oi_insert(env, obj, fid, th);
2235 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2236 LINVRNT(osd_invariant(obj));
2240 static int osd_declare_object_ref_add(const struct lu_env *env,
2241 struct dt_object *dt,
2242 struct thandle *handle)
2244 struct osd_thandle *oh;
2246 /* it's possible that object doesn't exist yet */
2247 LASSERT(handle != NULL);
2249 oh = container_of0(handle, struct osd_thandle, ot_super);
2250 LASSERT(oh->ot_handle == NULL);
2252 OSD_DECLARE_OP(oh, ref_add);
2253 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2259 * Concurrency: @dt is write locked.
2261 static int osd_object_ref_add(const struct lu_env *env,
2262 struct dt_object *dt, struct thandle *th)
2264 struct osd_object *obj = osd_dt_obj(dt);
2265 struct inode *inode = obj->oo_inode;
2267 LINVRNT(osd_invariant(obj));
2268 LASSERT(dt_object_exists(dt));
2269 LASSERT(osd_write_locked(env, obj));
2270 LASSERT(th != NULL);
2272 OSD_EXEC_OP(th, ref_add);
2275 * DIR_NLINK feature is set for compatibility reasons if:
2276 * 1) nlinks > LDISKFS_LINK_MAX, or
2277 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2279 * It is easier to always set this flag (rather than check and set),
2280 * since it has less overhead, and the superblock will be dirtied
2281 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2282 * do not actually care whether this flag is set or not.
2284 cfs_spin_lock(&obj->oo_guard);
2286 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2287 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2288 inode->i_nlink == 2)
2291 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2292 cfs_spin_unlock(&obj->oo_guard);
2293 inode->i_sb->s_op->dirty_inode(inode);
2294 LINVRNT(osd_invariant(obj));
2299 static int osd_declare_object_ref_del(const struct lu_env *env,
2300 struct dt_object *dt,
2301 struct thandle *handle)
2303 struct osd_thandle *oh;
2305 LASSERT(dt_object_exists(dt));
2306 LASSERT(handle != NULL);
2308 oh = container_of0(handle, struct osd_thandle, ot_super);
2309 LASSERT(oh->ot_handle == NULL);
2311 OSD_DECLARE_OP(oh, ref_del);
2312 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2318 * Concurrency: @dt is write locked.
2320 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2323 struct osd_object *obj = osd_dt_obj(dt);
2324 struct inode *inode = obj->oo_inode;
2326 LINVRNT(osd_invariant(obj));
2327 LASSERT(dt_object_exists(dt));
2328 LASSERT(osd_write_locked(env, obj));
2329 LASSERT(th != NULL);
2331 OSD_EXEC_OP(th, ref_del);
2333 cfs_spin_lock(&obj->oo_guard);
2334 LASSERT(inode->i_nlink > 0);
2336 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2337 * then the nlink count is 1. Don't let it be set to 0 or the directory
2338 * inode will be deleted incorrectly. */
2339 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2341 cfs_spin_unlock(&obj->oo_guard);
2342 inode->i_sb->s_op->dirty_inode(inode);
2343 LINVRNT(osd_invariant(obj));
2349 * Get the 64-bit version for an inode.
2351 static int osd_object_version_get(const struct lu_env *env,
2352 struct dt_object *dt, dt_obj_version_t *ver)
2354 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2356 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2357 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2358 *ver = LDISKFS_I(inode)->i_fs_version;
2363 * Concurrency: @dt is read locked.
2365 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2366 struct lu_buf *buf, const char *name,
2367 struct lustre_capa *capa)
2369 struct osd_object *obj = osd_dt_obj(dt);
2370 struct inode *inode = obj->oo_inode;
2371 struct osd_thread_info *info = osd_oti_get(env);
2372 struct dentry *dentry = &info->oti_obj_dentry;
2374 /* version get is not real XATTR but uses xattr API */
2375 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2376 /* for version we are just using xattr API but change inode
2378 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2379 osd_object_version_get(env, dt, buf->lb_buf);
2380 return sizeof(dt_obj_version_t);
2383 LASSERT(dt_object_exists(dt));
2384 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2385 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2387 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2390 dentry->d_inode = inode;
2391 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2395 static int osd_declare_xattr_set(const struct lu_env *env,
2396 struct dt_object *dt,
2397 const struct lu_buf *buf, const char *name,
2398 int fl, struct thandle *handle)
2400 struct osd_thandle *oh;
2402 LASSERT(handle != NULL);
2404 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2405 /* no credits for version */
2409 oh = container_of0(handle, struct osd_thandle, ot_super);
2410 LASSERT(oh->ot_handle == NULL);
2412 OSD_DECLARE_OP(oh, xattr_set);
2413 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2419 * Set the 64-bit version for object
2421 static void osd_object_version_set(const struct lu_env *env,
2422 struct dt_object *dt,
2423 dt_obj_version_t *new_version)
2425 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2427 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2428 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2430 LDISKFS_I(inode)->i_fs_version = *new_version;
2431 /** Version is set after all inode operations are finished,
2432 * so we should mark it dirty here */
2433 inode->i_sb->s_op->dirty_inode(inode);
2437 * Concurrency: @dt is write locked.
2439 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2440 const struct lu_buf *buf, const char *name, int fl,
2441 struct thandle *handle, struct lustre_capa *capa)
2443 LASSERT(handle != NULL);
2445 /* version set is not real XATTR */
2446 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2447 /* for version we are just using xattr API but change inode
2449 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2450 osd_object_version_set(env, dt, buf->lb_buf);
2451 return sizeof(dt_obj_version_t);
2454 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2457 OSD_EXEC_OP(handle, xattr_set);
2458 return __osd_xattr_set(env, dt, buf, name, fl);
2462 * Concurrency: @dt is read locked.
2464 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2465 struct lu_buf *buf, struct lustre_capa *capa)
2467 struct osd_object *obj = osd_dt_obj(dt);
2468 struct inode *inode = obj->oo_inode;
2469 struct osd_thread_info *info = osd_oti_get(env);
2470 struct dentry *dentry = &info->oti_obj_dentry;
2472 LASSERT(dt_object_exists(dt));
2473 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2474 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2476 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2479 dentry->d_inode = inode;
2480 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2483 static int osd_declare_xattr_del(const struct lu_env *env,
2484 struct dt_object *dt, const char *name,
2485 struct thandle *handle)
2487 struct osd_thandle *oh;
2489 LASSERT(dt_object_exists(dt));
2490 LASSERT(handle != NULL);
2492 oh = container_of0(handle, struct osd_thandle, ot_super);
2493 LASSERT(oh->ot_handle == NULL);
2495 OSD_DECLARE_OP(oh, xattr_set);
2496 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2502 * Concurrency: @dt is write locked.
2504 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2505 const char *name, struct thandle *handle,
2506 struct lustre_capa *capa)
2508 struct osd_object *obj = osd_dt_obj(dt);
2509 struct inode *inode = obj->oo_inode;
2510 struct osd_thread_info *info = osd_oti_get(env);
2511 struct dentry *dentry = &info->oti_obj_dentry;
2514 LASSERT(dt_object_exists(dt));
2515 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2516 LASSERT(osd_write_locked(env, obj));
2517 LASSERT(handle != NULL);
2519 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2522 OSD_EXEC_OP(handle, xattr_set);
2524 dentry->d_inode = inode;
2525 rc = inode->i_op->removexattr(dentry, name);
2529 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2530 struct dt_object *dt,
2531 struct lustre_capa *old,
2534 struct osd_thread_info *info = osd_oti_get(env);
2535 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2536 struct osd_object *obj = osd_dt_obj(dt);
2537 struct osd_device *dev = osd_obj2dev(obj);
2538 struct lustre_capa_key *key = &info->oti_capa_key;
2539 struct lustre_capa *capa = &info->oti_capa;
2540 struct obd_capa *oc;
2541 struct md_capainfo *ci;
2545 if (!dev->od_fl_capa)
2546 RETURN(ERR_PTR(-ENOENT));
2548 LASSERT(dt_object_exists(dt));
2549 LINVRNT(osd_invariant(obj));
2551 /* renewal sanity check */
2552 if (old && osd_object_auth(env, dt, old, opc))
2553 RETURN(ERR_PTR(-EACCES));
2555 ci = md_capainfo(env);
2557 RETURN(ERR_PTR(-ENOENT));
2559 switch (ci->mc_auth) {
2563 capa->lc_uid = obj->oo_inode->i_uid;
2564 capa->lc_gid = obj->oo_inode->i_gid;
2565 capa->lc_flags = LC_ID_PLAIN;
2567 case LC_ID_CONVERT: {
2570 s[0] = obj->oo_inode->i_uid;
2571 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2572 s[2] = obj->oo_inode->i_gid;
2573 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2574 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2576 RETURN(ERR_PTR(rc));
2578 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2579 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2580 capa->lc_flags = LC_ID_CONVERT;
2584 RETURN(ERR_PTR(-EINVAL));
2587 capa->lc_fid = *fid;
2589 capa->lc_flags |= dev->od_capa_alg << 24;
2590 capa->lc_timeout = dev->od_capa_timeout;
2591 capa->lc_expiry = 0;
2593 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2595 LASSERT(!capa_is_expired(oc));
2599 cfs_spin_lock(&capa_lock);
2600 *key = dev->od_capa_keys[1];
2601 cfs_spin_unlock(&capa_lock);
2603 capa->lc_keyid = key->lk_keyid;
2604 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2606 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2608 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2609 RETURN(ERR_PTR(rc));
2612 oc = capa_add(dev->od_capa_hash, capa);
2616 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2618 struct osd_object *obj = osd_dt_obj(dt);
2619 struct inode *inode = obj->oo_inode;
2620 struct osd_thread_info *info = osd_oti_get(env);
2621 struct dentry *dentry = &info->oti_obj_dentry;
2622 struct file *file = &info->oti_file;
2627 dentry->d_inode = inode;
2628 file->f_dentry = dentry;
2629 file->f_mapping = inode->i_mapping;
2630 file->f_op = inode->i_fop;
2631 mutex_lock(&inode->i_mutex);
2632 rc = file->f_op->fsync(file, dentry, 0);
2633 mutex_unlock(&inode->i_mutex);
2637 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2640 struct osd_object *obj = osd_dt_obj(dt);
2643 *data = (void *)obj->oo_inode;
2651 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2652 const struct dt_index_features *feat)
2654 struct iam_descr *descr;
2656 if (osd_object_is_root(o))
2657 return feat == &dt_directory_features;
2659 LASSERT(o->oo_dir != NULL);
2661 descr = o->oo_dir->od_container.ic_descr;
2662 if (feat == &dt_directory_features) {
2663 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2669 feat->dif_keysize_min <= descr->id_key_size &&
2670 descr->id_key_size <= feat->dif_keysize_max &&
2671 feat->dif_recsize_min <= descr->id_rec_size &&
2672 descr->id_rec_size <= feat->dif_recsize_max &&
2673 !(feat->dif_flags & (DT_IND_VARKEY |
2674 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2675 ergo(feat->dif_flags & DT_IND_UPDATE,
2676 1 /* XXX check that object (and file system) is
2681 static int osd_iam_container_init(const struct lu_env *env,
2682 struct osd_object *obj,
2683 struct osd_directory *dir)
2685 struct iam_container *bag = &dir->od_container;
2688 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2692 result = iam_container_setup(bag);
2696 if (osd_obj2dev(obj)->od_iop_mode) {
2697 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2699 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2705 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2707 iam_container_fini(bag);
2714 * Concurrency: no external locking is necessary.
2716 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2717 const struct dt_index_features *feat)
2721 struct osd_object *obj = osd_dt_obj(dt);
2722 struct osd_device *osd = osd_obj2dev(obj);
2724 LINVRNT(osd_invariant(obj));
2725 LASSERT(dt_object_exists(dt));
2727 if (osd_object_is_root(obj)) {
2728 dt->do_index_ops = &osd_index_ea_ops;
2730 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2731 dt->do_index_ops = &osd_index_ea_ops;
2732 if (S_ISDIR(obj->oo_inode->i_mode))
2737 } else if (unlikely(feat == &dt_otable_features)) {
2738 dt->do_index_ops = &osd_otable_ops;
2740 } else if (feat == &dt_acct_features) {
2741 dt->do_index_ops = &osd_acct_index_ops;
2744 } else if (!osd_has_index(obj)) {
2745 struct osd_directory *dir;
2750 cfs_spin_lock(&obj->oo_guard);
2751 if (obj->oo_dir == NULL)
2755 * Concurrent thread allocated container data.
2758 cfs_spin_unlock(&obj->oo_guard);
2760 * Now, that we have container data, serialize its
2763 cfs_down_write(&obj->oo_ext_idx_sem);
2765 * recheck under lock.
2767 if (!osd_has_index(obj))
2768 result = osd_iam_container_init(env, obj, dir);
2771 cfs_up_write(&obj->oo_ext_idx_sem);
2779 if (result == 0 && skip_iam == 0) {
2780 if (!osd_iam_index_probe(env, obj, feat))
2783 LINVRNT(osd_invariant(obj));
2788 static int osd_otable_it_attr_get(const struct lu_env *env,
2789 struct dt_object *dt,
2790 struct lu_attr *attr,
2791 struct lustre_capa *capa)
2797 static const struct dt_object_operations osd_obj_ops = {
2798 .do_read_lock = osd_object_read_lock,
2799 .do_write_lock = osd_object_write_lock,
2800 .do_read_unlock = osd_object_read_unlock,
2801 .do_write_unlock = osd_object_write_unlock,
2802 .do_write_locked = osd_object_write_locked,
2803 .do_attr_get = osd_attr_get,
2804 .do_declare_attr_set = osd_declare_attr_set,
2805 .do_attr_set = osd_attr_set,
2806 .do_ah_init = osd_ah_init,
2807 .do_declare_create = osd_declare_object_create,
2808 .do_create = osd_object_create,
2809 .do_declare_destroy = osd_declare_object_destroy,
2810 .do_destroy = osd_object_destroy,
2811 .do_index_try = osd_index_try,
2812 .do_declare_ref_add = osd_declare_object_ref_add,
2813 .do_ref_add = osd_object_ref_add,
2814 .do_declare_ref_del = osd_declare_object_ref_del,
2815 .do_ref_del = osd_object_ref_del,
2816 .do_xattr_get = osd_xattr_get,
2817 .do_declare_xattr_set = osd_declare_xattr_set,
2818 .do_xattr_set = osd_xattr_set,
2819 .do_declare_xattr_del = osd_declare_xattr_del,
2820 .do_xattr_del = osd_xattr_del,
2821 .do_xattr_list = osd_xattr_list,
2822 .do_capa_get = osd_capa_get,
2823 .do_object_sync = osd_object_sync,
2824 .do_data_get = osd_data_get,
2828 * dt_object_operations for interoperability mode
2829 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2831 static const struct dt_object_operations osd_obj_ea_ops = {
2832 .do_read_lock = osd_object_read_lock,
2833 .do_write_lock = osd_object_write_lock,
2834 .do_read_unlock = osd_object_read_unlock,
2835 .do_write_unlock = osd_object_write_unlock,
2836 .do_write_locked = osd_object_write_locked,
2837 .do_attr_get = osd_attr_get,
2838 .do_declare_attr_set = osd_declare_attr_set,
2839 .do_attr_set = osd_attr_set,
2840 .do_ah_init = osd_ah_init,
2841 .do_declare_create = osd_declare_object_create,
2842 .do_create = osd_object_ea_create,
2843 .do_declare_destroy = osd_declare_object_destroy,
2844 .do_destroy = osd_object_destroy,
2845 .do_index_try = osd_index_try,
2846 .do_declare_ref_add = osd_declare_object_ref_add,
2847 .do_ref_add = osd_object_ref_add,
2848 .do_declare_ref_del = osd_declare_object_ref_del,
2849 .do_ref_del = osd_object_ref_del,
2850 .do_xattr_get = osd_xattr_get,
2851 .do_declare_xattr_set = osd_declare_xattr_set,
2852 .do_xattr_set = osd_xattr_set,
2853 .do_declare_xattr_del = osd_declare_xattr_del,
2854 .do_xattr_del = osd_xattr_del,
2855 .do_xattr_list = osd_xattr_list,
2856 .do_capa_get = osd_capa_get,
2857 .do_object_sync = osd_object_sync,
2858 .do_data_get = osd_data_get,
2861 static const struct dt_object_operations osd_obj_otable_it_ops = {
2862 .do_attr_get = osd_otable_it_attr_get,
2863 .do_index_try = osd_index_try,
2866 static int osd_index_declare_iam_delete(const struct lu_env *env,
2867 struct dt_object *dt,
2868 const struct dt_key *key,
2869 struct thandle *handle)
2871 struct osd_thandle *oh;
2873 oh = container_of0(handle, struct osd_thandle, ot_super);
2874 LASSERT(oh->ot_handle == NULL);
2876 OSD_DECLARE_OP(oh, delete);
2877 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2883 * delete a (key, value) pair from index \a dt specified by \a key
2885 * \param dt osd index object
2886 * \param key key for index
2887 * \param rec record reference
2888 * \param handle transaction handler
2891 * \retval -ve failure
2894 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2895 const struct dt_key *key,
2896 struct thandle *handle,
2897 struct lustre_capa *capa)
2899 struct osd_object *obj = osd_dt_obj(dt);
2900 struct osd_thandle *oh;
2901 struct iam_path_descr *ipd;
2902 struct iam_container *bag = &obj->oo_dir->od_container;
2907 LINVRNT(osd_invariant(obj));
2908 LASSERT(dt_object_exists(dt));
2909 LASSERT(bag->ic_object == obj->oo_inode);
2910 LASSERT(handle != NULL);
2912 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2915 OSD_EXEC_OP(handle, delete);
2917 ipd = osd_idx_ipd_get(env, bag);
2918 if (unlikely(ipd == NULL))
2921 oh = container_of0(handle, struct osd_thandle, ot_super);
2922 LASSERT(oh->ot_handle != NULL);
2923 LASSERT(oh->ot_handle->h_transaction != NULL);
2925 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2926 osd_ipd_put(env, bag, ipd);
2927 LINVRNT(osd_invariant(obj));
2931 static int osd_index_declare_ea_delete(const struct lu_env *env,
2932 struct dt_object *dt,
2933 const struct dt_key *key,
2934 struct thandle *handle)
2936 struct osd_thandle *oh;
2938 LASSERT(dt_object_exists(dt));
2939 LASSERT(handle != NULL);
2941 oh = container_of0(handle, struct osd_thandle, ot_super);
2942 LASSERT(oh->ot_handle == NULL);
2944 OSD_DECLARE_OP(oh, delete);
2945 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2947 LASSERT(osd_dt_obj(dt)->oo_inode);
2948 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2949 osd_dt_obj(dt)->oo_inode);
2950 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2951 osd_dt_obj(dt)->oo_inode);
2956 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2959 struct osd_fid_pack *rec;
2962 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2963 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2964 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2970 * Index delete function for interoperability mode (b11826).
2971 * It will remove the directory entry added by osd_index_ea_insert().
2972 * This entry is needed to maintain name->fid mapping.
2974 * \param key, key i.e. file entry to be deleted
2976 * \retval 0, on success
2977 * \retval -ve, on error
2979 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2980 const struct dt_key *key,
2981 struct thandle *handle,
2982 struct lustre_capa *capa)
2984 struct osd_object *obj = osd_dt_obj(dt);
2985 struct inode *dir = obj->oo_inode;
2986 struct dentry *dentry;
2987 struct osd_thandle *oh;
2988 struct ldiskfs_dir_entry_2 *de;
2989 struct buffer_head *bh;
2990 struct htree_lock *hlock = NULL;
2995 LINVRNT(osd_invariant(obj));
2996 LASSERT(dt_object_exists(dt));
2997 LASSERT(handle != NULL);
2999 OSD_EXEC_OP(handle, delete);
3001 oh = container_of(handle, struct osd_thandle, ot_super);
3002 LASSERT(oh->ot_handle != NULL);
3003 LASSERT(oh->ot_handle->h_transaction != NULL);
3005 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3008 dentry = osd_child_dentry_get(env, obj,
3009 (char *)key, strlen((char *)key));
3011 if (obj->oo_hl_head != NULL) {
3012 hlock = osd_oti_get(env)->oti_hlock;
3013 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3014 dir, LDISKFS_HLOCK_DEL);
3016 cfs_down_write(&obj->oo_ext_idx_sem);
3019 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3021 rc = ldiskfs_delete_entry(oh->ot_handle,
3028 ldiskfs_htree_unlock(hlock);
3030 cfs_up_write(&obj->oo_ext_idx_sem);
3032 LASSERT(osd_invariant(obj));
3037 * Lookup index for \a key and copy record to \a rec.
3039 * \param dt osd index object
3040 * \param key key for index
3041 * \param rec record reference
3043 * \retval +ve success : exact mach
3044 * \retval 0 return record with key not greater than \a key
3045 * \retval -ve failure
3047 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3048 struct dt_rec *rec, const struct dt_key *key,
3049 struct lustre_capa *capa)
3051 struct osd_object *obj = osd_dt_obj(dt);
3052 struct iam_path_descr *ipd;
3053 struct iam_container *bag = &obj->oo_dir->od_container;
3054 struct osd_thread_info *oti = osd_oti_get(env);
3055 struct iam_iterator *it = &oti->oti_idx_it;
3056 struct iam_rec *iam_rec;
3061 LASSERT(osd_invariant(obj));
3062 LASSERT(dt_object_exists(dt));
3063 LASSERT(bag->ic_object == obj->oo_inode);
3065 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3068 ipd = osd_idx_ipd_get(env, bag);
3072 /* got ipd now we can start iterator. */
3073 iam_it_init(it, bag, 0, ipd);
3075 rc = iam_it_get(it, (struct iam_key *)key);
3077 if (S_ISDIR(obj->oo_inode->i_mode))
3078 iam_rec = (struct iam_rec *)oti->oti_ldp;
3080 iam_rec = (struct iam_rec *) rec;
3082 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3083 if (S_ISDIR(obj->oo_inode->i_mode))
3084 osd_fid_unpack((struct lu_fid *) rec,
3085 (struct osd_fid_pack *)iam_rec);
3089 osd_ipd_put(env, bag, ipd);
3091 LINVRNT(osd_invariant(obj));
3096 static int osd_index_declare_iam_insert(const struct lu_env *env,
3097 struct dt_object *dt,
3098 const struct dt_rec *rec,
3099 const struct dt_key *key,
3100 struct thandle *handle)
3102 struct osd_thandle *oh;
3104 LASSERT(dt_object_exists(dt));
3105 LASSERT(handle != NULL);
3107 oh = container_of0(handle, struct osd_thandle, ot_super);
3108 LASSERT(oh->ot_handle == NULL);
3110 OSD_DECLARE_OP(oh, insert);
3111 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3117 * Inserts (key, value) pair in \a dt index object.
3119 * \param dt osd index object
3120 * \param key key for index
3121 * \param rec record reference
3122 * \param th transaction handler
3125 * \retval -ve failure
3127 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3128 const struct dt_rec *rec,
3129 const struct dt_key *key, struct thandle *th,
3130 struct lustre_capa *capa, int ignore_quota)
3132 struct osd_object *obj = osd_dt_obj(dt);
3133 struct iam_path_descr *ipd;
3134 struct osd_thandle *oh;
3135 struct iam_container *bag = &obj->oo_dir->od_container;
3136 #ifdef HAVE_QUOTA_SUPPORT
3137 cfs_cap_t save = cfs_curproc_cap_pack();
3139 struct osd_thread_info *oti = osd_oti_get(env);
3140 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3145 LINVRNT(osd_invariant(obj));
3146 LASSERT(dt_object_exists(dt));
3147 LASSERT(bag->ic_object == obj->oo_inode);
3148 LASSERT(th != NULL);
3150 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3153 OSD_EXEC_OP(th, insert);
3155 ipd = osd_idx_ipd_get(env, bag);
3156 if (unlikely(ipd == NULL))
3159 oh = container_of0(th, struct osd_thandle, ot_super);
3160 LASSERT(oh->ot_handle != NULL);
3161 LASSERT(oh->ot_handle->h_transaction != NULL);
3162 #ifdef HAVE_QUOTA_SUPPORT
3164 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3166 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3168 if (S_ISDIR(obj->oo_inode->i_mode))
3169 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3171 iam_rec = (struct iam_rec *) rec;
3172 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3174 #ifdef HAVE_QUOTA_SUPPORT
3175 cfs_curproc_cap_unpack(save);
3177 osd_ipd_put(env, bag, ipd);
3178 LINVRNT(osd_invariant(obj));
3183 * Calls ldiskfs_add_entry() to add directory entry
3184 * into the directory. This is required for
3185 * interoperability mode (b11826)
3187 * \retval 0, on success
3188 * \retval -ve, on error
3190 static int __osd_ea_add_rec(struct osd_thread_info *info,
3191 struct osd_object *pobj, struct inode *cinode,
3192 const char *name, const struct dt_rec *fid,
3193 struct htree_lock *hlock, struct thandle *th)
3195 struct ldiskfs_dentry_param *ldp;
3196 struct dentry *child;
3197 struct osd_thandle *oth;
3200 oth = container_of(th, struct osd_thandle, ot_super);
3201 LASSERT(oth->ot_handle != NULL);
3202 LASSERT(oth->ot_handle->h_transaction != NULL);
3204 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3206 /* XXX: remove fid_is_igif() check here.
3207 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3208 * it is IGIF now but needs FID in dir entry as well for readdir
3210 * LU-838 should fix that and remove fid_is_igif() check */
3211 if (fid_is_igif((struct lu_fid *)fid) ||
3212 fid_is_norm((struct lu_fid *)fid)) {
3213 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3214 osd_get_ldiskfs_dirent_param(ldp, fid);
3215 child->d_fsdata = (void *)ldp;
3217 child->d_fsdata = NULL;
3219 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3225 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3226 * into the directory.Also sets flags into osd object to
3227 * indicate dot and dotdot are created. This is required for
3228 * interoperability mode (b11826)
3230 * \param dir directory for dot and dotdot fixup.
3231 * \param obj child object for linking
3233 * \retval 0, on success
3234 * \retval -ve, on error
3236 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3237 struct osd_object *dir,
3238 struct inode *parent_dir, const char *name,
3239 const struct dt_rec *dot_fid,
3240 const struct dt_rec *dot_dot_fid,
3243 struct inode *inode = dir->oo_inode;
3244 struct ldiskfs_dentry_param *dot_ldp;
3245 struct ldiskfs_dentry_param *dot_dot_ldp;
3246 struct osd_thandle *oth;
3249 oth = container_of(th, struct osd_thandle, ot_super);
3250 LASSERT(oth->ot_handle->h_transaction != NULL);
3251 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3253 if (strcmp(name, dot) == 0) {
3254 if (dir->oo_compat_dot_created) {
3257 LASSERT(inode == parent_dir);
3258 dir->oo_compat_dot_created = 1;
3261 } else if(strcmp(name, dotdot) == 0) {
3262 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3263 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3265 if (!dir->oo_compat_dot_created)
3267 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3268 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3269 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3274 /* in case of rename, dotdot is already created */
3275 if (dir->oo_compat_dotdot_created) {
3276 return __osd_ea_add_rec(info, dir, parent_dir, name,
3277 dot_dot_fid, NULL, th);
3280 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3281 inode, dot_ldp, dot_dot_ldp);
3283 dir->oo_compat_dotdot_created = 1;
3291 * It will call the appropriate osd_add* function and return the
3292 * value, return by respective functions.
3294 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3295 struct inode *cinode, const char *name,
3296 const struct dt_rec *fid, struct thandle *th)
3298 struct osd_thread_info *info = osd_oti_get(env);
3299 struct htree_lock *hlock;
3302 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3304 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3306 if (hlock != NULL) {
3307 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3310 cfs_down_write(&pobj->oo_ext_idx_sem);
3312 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3313 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3316 if (hlock != NULL) {
3317 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3318 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3320 cfs_down_write(&pobj->oo_ext_idx_sem);
3323 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3327 ldiskfs_htree_unlock(hlock);
3329 cfs_up_write(&pobj->oo_ext_idx_sem);
3335 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3336 struct osd_idmap_cache *oic)
3338 struct osd_scrub *scrub = &dev->od_scrub;
3339 struct lu_fid *fid = &oic->oic_fid;
3340 struct osd_inode_id *id = &oti->oti_id;
3346 rc = osd_oi_lookup(oti, dev, fid, id);
3347 if (rc != 0 && rc != -ENOENT)
3350 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3353 if (thread_is_running(&scrub->os_thread)) {
3354 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3355 /* There is race condition between osd_oi_lookup and OI scrub.
3356 * The OI scrub finished just after osd_oi_lookup() failure.
3357 * Under such case, it is unnecessary to trigger OI scrub again,
3358 * but try to call osd_oi_lookup() again. */
3359 if (unlikely(rc == -EAGAIN))
3365 if (!scrub->os_no_scrub && ++once == 1) {
3366 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3368 rc = osd_scrub_start(dev);
3369 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3371 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3377 RETURN(rc = -EREMCHG);
3381 * Calls ->lookup() to find dentry. From dentry get inode and
3382 * read inode's ea to get fid. This is required for interoperability
3385 * \retval 0, on success
3386 * \retval -ve, on error
3388 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3389 struct dt_rec *rec, const struct dt_key *key)
3391 struct inode *dir = obj->oo_inode;
3392 struct dentry *dentry;
3393 struct ldiskfs_dir_entry_2 *de;
3394 struct buffer_head *bh;
3395 struct lu_fid *fid = (struct lu_fid *) rec;
3396 struct htree_lock *hlock = NULL;
3400 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3402 dentry = osd_child_dentry_get(env, obj,
3403 (char *)key, strlen((char *)key));
3405 if (obj->oo_hl_head != NULL) {
3406 hlock = osd_oti_get(env)->oti_hlock;
3407 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3408 dir, LDISKFS_HLOCK_LOOKUP);
3410 cfs_down_read(&obj->oo_ext_idx_sem);
3413 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3415 struct osd_thread_info *oti = osd_oti_get(env);
3416 struct osd_idmap_cache *oic = &oti->oti_cache;
3417 struct osd_device *dev = osd_obj2dev(obj);
3418 struct osd_scrub *scrub = &dev->od_scrub;
3419 struct scrub_file *sf = &scrub->os_file;
3421 ino = le32_to_cpu(de->inode);
3422 rc = osd_get_fid_from_dentry(de, rec);
3424 /* done with de, release bh */
3427 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3429 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3431 if (rc != 0 || !fid_is_norm(fid))
3434 oic->oic_fid = *fid;
3435 if ((scrub->os_pos_current <= ino) &&
3436 (sf->sf_flags & SF_INCONSISTENT ||
3437 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3439 rc = osd_consistency_check(oti, dev, oic);
3448 ldiskfs_htree_unlock(hlock);
3450 cfs_up_read(&obj->oo_ext_idx_sem);
3455 * Find the osd object for given fid.
3457 * \param fid need to find the osd object having this fid
3459 * \retval osd_object on success
3460 * \retval -ve on error
3462 struct osd_object *osd_object_find(const struct lu_env *env,
3463 struct dt_object *dt,
3464 const struct lu_fid *fid)
3466 struct lu_device *ludev = dt->do_lu.lo_dev;
3467 struct osd_object *child = NULL;
3468 struct lu_object *luch;
3469 struct lu_object *lo;
3471 luch = lu_object_find(env, ludev, fid, NULL);
3472 if (!IS_ERR(luch)) {
3473 if (lu_object_exists(luch)) {
3474 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3476 child = osd_obj(lo);
3478 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3479 "lu_object can't be located"
3480 DFID"\n", PFID(fid));
3482 if (child == NULL) {
3483 lu_object_put(env, luch);
3484 CERROR("Unable to get osd_object\n");
3485 child = ERR_PTR(-ENOENT);
3488 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3489 "lu_object does not exists "DFID"\n",
3491 lu_object_put(env, luch);
3492 child = ERR_PTR(-ENOENT);
3495 child = (void *)luch;
3501 * Put the osd object once done with it.
3503 * \param obj osd object that needs to be put
3505 static inline void osd_object_put(const struct lu_env *env,
3506 struct osd_object *obj)
3508 lu_object_put(env, &obj->oo_dt.do_lu);
3511 static int osd_index_declare_ea_insert(const struct lu_env *env,
3512 struct dt_object *dt,
3513 const struct dt_rec *rec,
3514 const struct dt_key *key,
3515 struct thandle *handle)
3517 struct osd_thandle *oh;
3519 LASSERT(dt_object_exists(dt));
3520 LASSERT(handle != NULL);
3522 oh = container_of0(handle, struct osd_thandle, ot_super);
3523 LASSERT(oh->ot_handle == NULL);
3525 OSD_DECLARE_OP(oh, insert);
3526 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3528 LASSERT(osd_dt_obj(dt)->oo_inode);
3529 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3530 osd_dt_obj(dt)->oo_inode);
3531 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3532 osd_dt_obj(dt)->oo_inode);
3538 * Index add function for interoperability mode (b11826).
3539 * It will add the directory entry.This entry is needed to
3540 * maintain name->fid mapping.
3542 * \param key it is key i.e. file entry to be inserted
3543 * \param rec it is value of given key i.e. fid
3545 * \retval 0, on success
3546 * \retval -ve, on error
3548 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3549 const struct dt_rec *rec,
3550 const struct dt_key *key, struct thandle *th,
3551 struct lustre_capa *capa, int ignore_quota)
3553 struct osd_object *obj = osd_dt_obj(dt);
3554 struct lu_fid *fid = (struct lu_fid *) rec;
3555 const char *name = (const char *)key;
3556 struct osd_object *child;
3557 #ifdef HAVE_QUOTA_SUPPORT
3558 cfs_cap_t save = cfs_curproc_cap_pack();
3564 LASSERT(osd_invariant(obj));
3565 LASSERT(dt_object_exists(dt));
3566 LASSERT(th != NULL);
3568 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3571 child = osd_object_find(env, dt, fid);
3572 if (!IS_ERR(child)) {
3573 #ifdef HAVE_QUOTA_SUPPORT
3575 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3577 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3579 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3580 #ifdef HAVE_QUOTA_SUPPORT
3581 cfs_curproc_cap_unpack(save);
3583 osd_object_put(env, child);
3585 rc = PTR_ERR(child);
3588 LASSERT(osd_invariant(obj));
3593 * Initialize osd Iterator for given osd index object.
3595 * \param dt osd index object
3598 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3599 struct dt_object *dt,
3601 struct lustre_capa *capa)
3603 struct osd_it_iam *it;
3604 struct osd_thread_info *oti = osd_oti_get(env);
3605 struct osd_object *obj = osd_dt_obj(dt);
3606 struct lu_object *lo = &dt->do_lu;
3607 struct iam_path_descr *ipd;
3608 struct iam_container *bag = &obj->oo_dir->od_container;
3610 LASSERT(lu_object_exists(lo));
3612 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3613 return ERR_PTR(-EACCES);
3616 ipd = osd_it_ipd_get(env, bag);
3617 if (likely(ipd != NULL)) {
3621 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3622 return (struct dt_it *)it;
3624 return ERR_PTR(-ENOMEM);
3628 * free given Iterator.
3631 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3633 struct osd_it_iam *it = (struct osd_it_iam *)di;
3634 struct osd_object *obj = it->oi_obj;
3636 iam_it_fini(&it->oi_it);
3637 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3638 lu_object_put(env, &obj->oo_dt.do_lu);
3642 * Move Iterator to record specified by \a key
3644 * \param di osd iterator
3645 * \param key key for index
3647 * \retval +ve di points to record with least key not larger than key
3648 * \retval 0 di points to exact matched key
3649 * \retval -ve failure
3652 static int osd_it_iam_get(const struct lu_env *env,
3653 struct dt_it *di, const struct dt_key *key)
3655 struct osd_it_iam *it = (struct osd_it_iam *)di;
3657 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3663 * \param di osd iterator
3666 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3668 struct osd_it_iam *it = (struct osd_it_iam *)di;
3670 iam_it_put(&it->oi_it);
3674 * Move iterator by one record
3676 * \param di osd iterator
3678 * \retval +1 end of container reached
3680 * \retval -ve failure
3683 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3685 struct osd_it_iam *it = (struct osd_it_iam *)di;
3687 return iam_it_next(&it->oi_it);
3691 * Return pointer to the key under iterator.
3694 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3695 const struct dt_it *di)
3697 struct osd_it_iam *it = (struct osd_it_iam *)di;
3699 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3703 * Return size of key under iterator (in bytes)
3706 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3708 struct osd_it_iam *it = (struct osd_it_iam *)di;
3710 return iam_it_key_size(&it->oi_it);
3713 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3714 int len, __u16 type)
3716 struct luda_type *lt;
3717 const unsigned align = sizeof(struct luda_type) - 1;
3719 /* check if file type is required */
3720 if (attr & LUDA_TYPE) {
3721 len = (len + align) & ~align;
3723 lt = (void *) ent->lde_name + len;
3724 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3725 ent->lde_attrs |= LUDA_TYPE;
3728 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3732 * build lu direct from backend fs dirent.
3735 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3736 struct lu_fid *fid, __u64 offset,
3737 char *name, __u16 namelen,
3738 __u16 type, __u32 attr)
3740 fid_cpu_to_le(&ent->lde_fid, fid);
3741 ent->lde_attrs = LUDA_FID;
3743 ent->lde_hash = cpu_to_le64(offset);
3744 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3746 strncpy(ent->lde_name, name, namelen);
3747 ent->lde_namelen = cpu_to_le16(namelen);
3749 /* append lustre attributes */
3750 osd_it_append_attrs(ent, attr, namelen, type);
3754 * Return pointer to the record under iterator.
3756 static int osd_it_iam_rec(const struct lu_env *env,
3757 const struct dt_it *di,
3758 struct dt_rec *dtrec, __u32 attr)
3760 struct osd_it_iam *it = (struct osd_it_iam *)di;
3761 struct osd_thread_info *info = osd_oti_get(env);
3764 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3765 const struct osd_fid_pack *rec;
3766 struct lu_fid *fid = &info->oti_fid;
3767 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3773 name = (char *)iam_it_key_get(&it->oi_it);
3775 RETURN(PTR_ERR(name));
3777 namelen = iam_it_key_size(&it->oi_it);
3779 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3781 RETURN(PTR_ERR(rec));
3783 rc = osd_fid_unpack(fid, rec);
3787 hash = iam_it_store(&it->oi_it);
3789 /* IAM does not store object type in IAM index (dir) */
3790 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3793 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3794 (struct iam_rec *)dtrec);
3801 * Returns cookie for current Iterator position.
3803 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3805 struct osd_it_iam *it = (struct osd_it_iam *)di;
3807 return iam_it_store(&it->oi_it);
3811 * Restore iterator from cookie.
3813 * \param di osd iterator
3814 * \param hash Iterator location cookie
3816 * \retval +ve di points to record with least key not larger than key.
3817 * \retval 0 di points to exact matched key
3818 * \retval -ve failure
3821 static int osd_it_iam_load(const struct lu_env *env,
3822 const struct dt_it *di, __u64 hash)
3824 struct osd_it_iam *it = (struct osd_it_iam *)di;
3826 return iam_it_load(&it->oi_it, hash);
3829 static const struct dt_index_operations osd_index_iam_ops = {
3830 .dio_lookup = osd_index_iam_lookup,
3831 .dio_declare_insert = osd_index_declare_iam_insert,
3832 .dio_insert = osd_index_iam_insert,
3833 .dio_declare_delete = osd_index_declare_iam_delete,
3834 .dio_delete = osd_index_iam_delete,
3836 .init = osd_it_iam_init,
3837 .fini = osd_it_iam_fini,
3838 .get = osd_it_iam_get,
3839 .put = osd_it_iam_put,
3840 .next = osd_it_iam_next,
3841 .key = osd_it_iam_key,
3842 .key_size = osd_it_iam_key_size,
3843 .rec = osd_it_iam_rec,
3844 .store = osd_it_iam_store,
3845 .load = osd_it_iam_load
3850 * Creates or initializes iterator context.
3852 * \retval struct osd_it_ea, iterator structure on success
3855 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3856 struct dt_object *dt,
3858 struct lustre_capa *capa)
3860 struct osd_object *obj = osd_dt_obj(dt);
3861 struct osd_thread_info *info = osd_oti_get(env);
3862 struct osd_it_ea *it = &info->oti_it_ea;
3863 struct lu_object *lo = &dt->do_lu;
3864 struct dentry *obj_dentry = &info->oti_it_dentry;
3866 LASSERT(lu_object_exists(lo));
3868 obj_dentry->d_inode = obj->oo_inode;
3869 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3870 obj_dentry->d_name.hash = 0;
3872 it->oie_rd_dirent = 0;
3873 it->oie_it_dirent = 0;
3874 it->oie_dirent = NULL;
3875 it->oie_buf = info->oti_it_ea_buf;
3877 it->oie_file.f_pos = 0;
3878 it->oie_file.f_dentry = obj_dentry;
3879 if (attr & LUDA_64BITHASH)
3880 it->oie_file.f_flags = O_64BITHASH;
3882 it->oie_file.f_flags = O_32BITHASH;
3883 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3884 it->oie_file.f_op = obj->oo_inode->i_fop;
3885 it->oie_file.private_data = NULL;
3887 RETURN((struct dt_it *) it);
3891 * Destroy or finishes iterator context.
3893 * \param di iterator structure to be destroyed
3895 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3897 struct osd_it_ea *it = (struct osd_it_ea *)di;
3898 struct osd_object *obj = it->oie_obj;
3899 struct inode *inode = obj->oo_inode;
3902 it->oie_file.f_op->release(inode, &it->oie_file);
3903 lu_object_put(env, &obj->oo_dt.do_lu);
3908 * It position the iterator at given key, so that next lookup continues from
3909 * that key Or it is similar to dio_it->load() but based on a key,
3910 * rather than file position.
3912 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3915 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3917 static int osd_it_ea_get(const struct lu_env *env,
3918 struct dt_it *di, const struct dt_key *key)
3920 struct osd_it_ea *it = (struct osd_it_ea *)di;
3923 LASSERT(((const char *)key)[0] == '\0');
3924 it->oie_file.f_pos = 0;
3925 it->oie_rd_dirent = 0;
3926 it->oie_it_dirent = 0;
3927 it->oie_dirent = NULL;
3935 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3940 * It is called internally by ->readdir(). It fills the
3941 * iterator's in-memory data structure with required
3942 * information i.e. name, namelen, rec_size etc.
3944 * \param buf in which information to be filled in.
3945 * \param name name of the file in given dir
3947 * \retval 0 on success
3948 * \retval 1 on buffer full
3950 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3951 loff_t offset, __u64 ino,
3954 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3955 struct osd_it_ea_dirent *ent = it->oie_dirent;
3956 struct lu_fid *fid = &ent->oied_fid;
3957 struct osd_fid_pack *rec;
3960 /* this should never happen */
3961 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3962 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3966 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3970 if (d_type & LDISKFS_DIRENT_LUFID) {
3971 rec = (struct osd_fid_pack*) (name + namelen + 1);
3973 if (osd_fid_unpack(fid, rec) != 0)
3976 d_type &= ~LDISKFS_DIRENT_LUFID;
3981 ent->oied_ino = ino;
3982 ent->oied_off = offset;
3983 ent->oied_namelen = namelen;
3984 ent->oied_type = d_type;
3986 memcpy(ent->oied_name, name, namelen);
3988 it->oie_rd_dirent++;
3989 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3994 * Calls ->readdir() to load a directory entry at a time
3995 * and stored it in iterator's in-memory data structure.
3997 * \param di iterator's in memory structure
3999 * \retval 0 on success
4000 * \retval -ve on error
4002 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4003 const struct dt_it *di)
4005 struct osd_it_ea *it = (struct osd_it_ea *)di;
4006 struct osd_object *obj = it->oie_obj;
4007 struct inode *inode = obj->oo_inode;
4008 struct htree_lock *hlock = NULL;
4012 it->oie_dirent = it->oie_buf;
4013 it->oie_rd_dirent = 0;
4015 if (obj->oo_hl_head != NULL) {
4016 hlock = osd_oti_get(env)->oti_hlock;
4017 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4018 inode, LDISKFS_HLOCK_READDIR);
4020 cfs_down_read(&obj->oo_ext_idx_sem);
4023 result = inode->i_fop->readdir(&it->oie_file, it,
4024 (filldir_t) osd_ldiskfs_filldir);
4027 ldiskfs_htree_unlock(hlock);
4029 cfs_up_read(&obj->oo_ext_idx_sem);
4031 if (it->oie_rd_dirent == 0) {
4034 it->oie_dirent = it->oie_buf;
4035 it->oie_it_dirent = 1;
4042 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4043 * to load a directory entry at a time and stored it in
4044 * iterator's in-memory data structure.
4046 * \param di iterator's in memory structure
4048 * \retval +ve iterator reached to end
4049 * \retval 0 iterator not reached to end
4050 * \retval -ve on error
4052 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4054 struct osd_it_ea *it = (struct osd_it_ea *)di;
4059 if (it->oie_it_dirent < it->oie_rd_dirent) {
4061 (void *) it->oie_dirent +
4062 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4063 it->oie_dirent->oied_namelen);
4064 it->oie_it_dirent++;
4067 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
4070 rc = osd_ldiskfs_it_fill(env, di);
4077 * Returns the key at current position from iterator's in memory structure.
4079 * \param di iterator's in memory structure
4081 * \retval key i.e. struct dt_key on success
4083 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4084 const struct dt_it *di)
4086 struct osd_it_ea *it = (struct osd_it_ea *)di;
4088 return (struct dt_key *)it->oie_dirent->oied_name;
4092 * Returns the key's size at current position from iterator's in memory structure.
4094 * \param di iterator's in memory structure
4096 * \retval key_size i.e. struct dt_key on success
4098 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4100 struct osd_it_ea *it = (struct osd_it_ea *)di;
4102 return it->oie_dirent->oied_namelen;
4107 * Returns the value (i.e. fid/igif) at current position from iterator's
4108 * in memory structure.
4110 * \param di struct osd_it_ea, iterator's in memory structure
4111 * \param attr attr requested for dirent.
4112 * \param lde lustre dirent
4114 * \retval 0 no error and \param lde has correct lustre dirent.
4115 * \retval -ve on error
4117 static inline int osd_it_ea_rec(const struct lu_env *env,
4118 const struct dt_it *di,
4119 struct dt_rec *dtrec, __u32 attr)
4121 struct osd_it_ea *it = (struct osd_it_ea *)di;
4122 struct osd_object *obj = it->oie_obj;
4123 struct osd_device *dev = osd_obj2dev(obj);
4124 struct osd_scrub *scrub = &dev->od_scrub;
4125 struct scrub_file *sf = &scrub->os_file;
4126 struct osd_thread_info *oti = osd_oti_get(env);
4127 struct osd_idmap_cache *oic = &oti->oti_cache;
4128 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4129 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4130 __u32 ino = it->oie_dirent->oied_ino;
4134 if (!fid_is_sane(fid)) {
4135 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4139 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4142 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4143 it->oie_dirent->oied_name,
4144 it->oie_dirent->oied_namelen,
4145 it->oie_dirent->oied_type, attr);
4147 if (!fid_is_norm(fid))
4150 oic->oic_fid = *fid;
4151 if ((scrub->os_pos_current <= ino) &&
4152 (sf->sf_flags & SF_INCONSISTENT ||
4153 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4154 rc = osd_consistency_check(oti, dev, oic);
4160 * Returns a cookie for current position of the iterator head, so that
4161 * user can use this cookie to load/start the iterator next time.
4163 * \param di iterator's in memory structure
4165 * \retval cookie for current position, on success
4167 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4169 struct osd_it_ea *it = (struct osd_it_ea *)di;
4171 return it->oie_dirent->oied_off;
4175 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4176 * to load a directory entry at a time and stored it i inn,
4177 * in iterator's in-memory data structure.
4179 * \param di struct osd_it_ea, iterator's in memory structure
4181 * \retval +ve on success
4182 * \retval -ve on error
4184 static int osd_it_ea_load(const struct lu_env *env,
4185 const struct dt_it *di, __u64 hash)
4187 struct osd_it_ea *it = (struct osd_it_ea *)di;
4191 it->oie_file.f_pos = hash;
4193 rc = osd_ldiskfs_it_fill(env, di);
4201 * Index lookup function for interoperability mode (b11826).
4203 * \param key, key i.e. file name to be searched
4205 * \retval +ve, on success
4206 * \retval -ve, on error
4208 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4209 struct dt_rec *rec, const struct dt_key *key,
4210 struct lustre_capa *capa)
4212 struct osd_object *obj = osd_dt_obj(dt);
4217 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4218 LINVRNT(osd_invariant(obj));
4220 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4223 rc = osd_ea_lookup_rec(env, obj, rec, key);
4231 * Index and Iterator operations for interoperability
4232 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4234 static const struct dt_index_operations osd_index_ea_ops = {
4235 .dio_lookup = osd_index_ea_lookup,
4236 .dio_declare_insert = osd_index_declare_ea_insert,
4237 .dio_insert = osd_index_ea_insert,
4238 .dio_declare_delete = osd_index_declare_ea_delete,
4239 .dio_delete = osd_index_ea_delete,
4241 .init = osd_it_ea_init,
4242 .fini = osd_it_ea_fini,
4243 .get = osd_it_ea_get,
4244 .put = osd_it_ea_put,
4245 .next = osd_it_ea_next,
4246 .key = osd_it_ea_key,
4247 .key_size = osd_it_ea_key_size,
4248 .rec = osd_it_ea_rec,
4249 .store = osd_it_ea_store,
4250 .load = osd_it_ea_load
4254 static void *osd_key_init(const struct lu_context *ctx,
4255 struct lu_context_key *key)
4257 struct osd_thread_info *info;
4259 OBD_ALLOC_PTR(info);
4261 return ERR_PTR(-ENOMEM);
4263 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4264 if (info->oti_it_ea_buf == NULL)
4267 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4269 info->oti_hlock = ldiskfs_htree_lock_alloc();
4270 if (info->oti_hlock == NULL)
4276 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4279 return ERR_PTR(-ENOMEM);
4282 static void osd_key_fini(const struct lu_context *ctx,
4283 struct lu_context_key *key, void* data)
4285 struct osd_thread_info *info = data;
4287 if (info->oti_hlock != NULL)
4288 ldiskfs_htree_lock_free(info->oti_hlock);
4289 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4293 static void osd_key_exit(const struct lu_context *ctx,
4294 struct lu_context_key *key, void *data)
4296 struct osd_thread_info *info = data;
4298 LASSERT(info->oti_r_locks == 0);
4299 LASSERT(info->oti_w_locks == 0);
4300 LASSERT(info->oti_txns == 0);
4303 /* type constructor/destructor: osd_type_init, osd_type_fini */
4304 LU_TYPE_INIT_FINI(osd, &osd_key);
4306 struct lu_context_key osd_key = {
4307 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4308 .lct_init = osd_key_init,
4309 .lct_fini = osd_key_fini,
4310 .lct_exit = osd_key_exit
4314 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4315 const char *name, struct lu_device *next)
4317 struct osd_device *osd = osd_dev(d);
4319 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4320 return osd_procfs_init(osd, name);
4323 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4327 osd_scrub_cleanup(env, o);
4330 fsfilt_put_ops(o->od_fsops);
4334 /* shutdown quota slave instance associated with the device */
4335 if (o->od_quota_slave != NULL) {
4336 qsd_fini(env, o->od_quota_slave);
4337 o->od_quota_slave = NULL;
4343 static int osd_mount(const struct lu_env *env,
4344 struct osd_device *o, struct lustre_cfg *cfg)
4346 struct lustre_mount_info *lmi;
4347 const char *dev = lustre_cfg_string(cfg, 0);
4348 struct lustre_disk_data *ldd;
4349 struct lustre_sb_info *lsi;
4354 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4355 if (o->od_fsops == NULL) {
4356 CERROR("Can't find fsfilt_ldiskfs\n");
4360 if (o->od_mount != NULL) {
4361 CERROR("Already mounted (%s)\n", dev);
4366 lmi = server_get_mount(dev);
4368 CERROR("Cannot get mount info for %s!\n", dev);
4372 LASSERT(lmi != NULL);
4373 /* save lustre_mount_info in dt_device */
4375 o->od_mnt = lmi->lmi_mnt;
4377 lsi = s2lsi(lmi->lmi_sb);
4380 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4382 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", dev);
4386 if (ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
4387 rc = osd_compat_init(o);
4389 CERROR("%s: can't initialize compats: %d\n", dev, rc);
4395 static struct lu_device *osd_device_fini(const struct lu_env *env,
4396 struct lu_device *d)
4401 osd_compat_fini(osd_dev(d));
4403 shrink_dcache_sb(osd_sb(osd_dev(d)));
4404 osd_sync(env, lu2dt_dev(d));
4406 rc = osd_procfs_fini(osd_dev(d));
4408 CERROR("proc fini error %d \n", rc);
4409 RETURN (ERR_PTR(rc));
4412 if (osd_dev(d)->od_mount)
4413 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4414 osd_dev(d)->od_mount->lmi_mnt);
4415 osd_dev(d)->od_mount = NULL;
4420 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4421 struct lu_device_type *t,
4422 struct lustre_cfg *cfg)
4424 struct lu_device *l;
4425 struct osd_device *o;
4431 result = dt_device_init(&o->od_dt_dev, t);
4434 l->ld_ops = &osd_lu_ops;
4435 o->od_dt_dev.dd_ops = &osd_dt_ops;
4436 cfs_spin_lock_init(&o->od_osfs_lock);
4437 cfs_mutex_init(&o->od_otable_mutex);
4438 o->od_osfs_age = cfs_time_shift_64(-1000);
4439 o->od_capa_hash = init_capa_hash();
4440 if (o->od_capa_hash == NULL) {
4441 dt_device_fini(&o->od_dt_dev);
4442 l = ERR_PTR(-ENOMEM);
4445 l = ERR_PTR(result);
4450 l = ERR_PTR(-ENOMEM);
4454 static struct lu_device *osd_device_free(const struct lu_env *env,
4455 struct lu_device *d)
4457 struct osd_device *o = osd_dev(d);
4460 cleanup_capa_hash(o->od_capa_hash);
4461 dt_device_fini(&o->od_dt_dev);
4466 static int osd_process_config(const struct lu_env *env,
4467 struct lu_device *d, struct lustre_cfg *cfg)
4469 struct osd_device *o = osd_dev(d);
4473 switch(cfg->lcfg_command) {
4475 err = osd_mount(env, o, cfg);
4478 lu_dev_del_linkage(d->ld_site, d);
4479 err = osd_shutdown(env, o);
4488 static int osd_recovery_complete(const struct lu_env *env,
4489 struct lu_device *d)
4494 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4495 struct lu_device *dev)
4497 struct osd_device *osd = osd_dev(dev);
4501 /* 1. setup scrub, including OI files initialization */
4502 result = osd_scrub_setup(env, osd);
4506 /* 2. setup quota slave instance */
4507 osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
4508 osd->od_proc_entry);
4509 if (IS_ERR(osd->od_quota_slave)) {
4510 result = PTR_ERR(osd->od_quota_slave);
4511 osd->od_quota_slave = NULL;
4515 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,3,50,0)
4516 /* Unfortunately, the current MDD implementation relies on some specific
4517 * code to be executed in the OSD layer. Since OFD now also uses the OSD
4518 * module, we need a way to skip the metadata-specific code when running
4520 * The hack here is to check the type of the parent device which is
4521 * either MD (i.e. MDD device) with the current MDT stack or DT (i.e.
4522 * OFD device) on an OST. As a reminder, obdfilter does not use the OSD
4523 * layer and still relies on lvfs. This hack won't work any more when
4524 * LOD is landed since LOD is of DT type.
4525 * This code should be removed once the orion MDT changes (LOD/OSP, ...)
4526 * have been landed */
4527 osd->od_is_md = lu_device_is_md(pdev);
4529 #warning "all is_md checks must be removed from osd-ldiskfs"
4535 /* 3. setup local objects */
4536 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4540 static const struct lu_object_operations osd_lu_obj_ops = {
4541 .loo_object_init = osd_object_init,
4542 .loo_object_delete = osd_object_delete,
4543 .loo_object_release = osd_object_release,
4544 .loo_object_free = osd_object_free,
4545 .loo_object_print = osd_object_print,
4546 .loo_object_invariant = osd_object_invariant
4549 const struct lu_device_operations osd_lu_ops = {
4550 .ldo_object_alloc = osd_object_alloc,
4551 .ldo_process_config = osd_process_config,
4552 .ldo_recovery_complete = osd_recovery_complete,
4553 .ldo_prepare = osd_prepare,
4556 static const struct lu_device_type_operations osd_device_type_ops = {
4557 .ldto_init = osd_type_init,
4558 .ldto_fini = osd_type_fini,
4560 .ldto_start = osd_type_start,
4561 .ldto_stop = osd_type_stop,
4563 .ldto_device_alloc = osd_device_alloc,
4564 .ldto_device_free = osd_device_free,
4566 .ldto_device_init = osd_device_init,
4567 .ldto_device_fini = osd_device_fini
4570 static struct lu_device_type osd_device_type = {
4571 .ldt_tags = LU_DEVICE_DT,
4572 .ldt_name = LUSTRE_OSD_NAME,
4573 .ldt_ops = &osd_device_type_ops,
4574 .ldt_ctx_tags = LCT_LOCAL,
4578 * lprocfs legacy support.
4580 static struct obd_ops osd_obd_device_ops = {
4581 .o_owner = THIS_MODULE
4584 static int __init osd_mod_init(void)
4586 struct lprocfs_static_vars lvars;
4589 lprocfs_osd_init_vars(&lvars);
4590 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4591 LUSTRE_OSD_NAME, &osd_device_type);
4594 static void __exit osd_mod_exit(void)
4596 class_unregister_type(LUSTRE_OSD_NAME);
4599 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4600 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4601 MODULE_LICENSE("GPL");
4603 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);