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 cfs_list_del_init(&dcb->dcb_linkage);
719 dcb->dcb_func(NULL, th, dcb, error);
722 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
726 lu_context_exit(&th->th_ctx);
727 lu_context_fini(&th->th_ctx);
731 static struct thandle *osd_trans_create(const struct lu_env *env,
734 struct osd_thread_info *oti = osd_oti_get(env);
735 struct osd_iobuf *iobuf = &oti->oti_iobuf;
736 struct osd_thandle *oh;
740 /* on pending IO in this thread should left from prev. request */
741 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
743 th = ERR_PTR(-ENOMEM);
744 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
749 th->th_tags = LCT_TX_HANDLE;
751 oti->oti_dev = osd_dt_dev(d);
752 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
759 * Concurrency: shouldn't matter.
761 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
764 struct osd_thread_info *oti = osd_oti_get(env);
765 struct osd_device *dev = osd_dt_dev(d);
767 struct osd_thandle *oh;
772 LASSERT(current->journal_info == NULL);
774 oh = container_of0(th, struct osd_thandle, ot_super);
776 LASSERT(oh->ot_handle == NULL);
778 rc = dt_txn_hook_start(env, d, th);
782 if (!osd_param_is_sane(dev, th)) {
783 CWARN("%.16s: too many transaction credits (%d > %d)\n",
784 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
786 osd_journal(dev)->j_max_transaction_buffers);
787 /* XXX Limit the credits to 'max_transaction_buffers', and
788 * let the underlying filesystem to catch the error if
789 * we really need so many credits.
791 * This should be removed when we can calculate the
792 * credits precisely. */
793 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
794 #ifdef OSD_TRACK_DECLARES
795 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
796 oh->ot_declare_attr_set, oh->ot_declare_punch,
797 oh->ot_declare_xattr_set);
798 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
799 oh->ot_declare_create, oh->ot_declare_ref_add,
800 oh->ot_declare_ref_del, oh->ot_declare_write);
801 CERROR(" insert: %d, delete: %d, destroy: %d\n",
802 oh->ot_declare_insert, oh->ot_declare_delete,
803 oh->ot_declare_destroy);
808 * XXX temporary stuff. Some abstraction layer should
811 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
815 LASSERT(oti->oti_txns == 0);
816 lu_context_init(&th->th_ctx, th->th_tags);
817 lu_context_enter(&th->th_ctx);
819 lu_device_get(&d->dd_lu_dev);
820 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
824 * XXX: current rule is that we first start tx,
825 * then lock object(s), but we can't use
826 * this rule for data (due to locking specifics
827 * in ldiskfs). also in long-term we'd like to
828 * use usually-used (locks;tx) ordering. so,
829 * UGLY thing is that we'll use one ordering for
830 * data (ofd) and reverse ordering for metadata
831 * (mdd). then at some point we'll fix the latter
834 LASSERT(oti->oti_r_locks == 0);
835 LASSERT(oti->oti_w_locks == 0);
848 * Concurrency: shouldn't matter.
850 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
853 struct osd_thandle *oh;
854 struct osd_thread_info *oti = osd_oti_get(env);
855 struct osd_iobuf *iobuf = &oti->oti_iobuf;
859 oh = container_of0(th, struct osd_thandle, ot_super);
861 if (oh->ot_handle != NULL) {
862 handle_t *hdl = oh->ot_handle;
864 hdl->h_sync = th->th_sync;
867 * add commit callback
868 * notice we don't do this in osd_trans_start()
869 * as underlying transaction can change during truncate
871 osd_journal_callback_set(hdl, osd_trans_commit_cb,
874 LASSERT(oti->oti_txns == 1);
877 * XXX: current rule is that we first start tx,
878 * then lock object(s), but we can't use
879 * this rule for data (due to locking specifics
880 * in ldiskfs). also in long-term we'd like to
881 * use usually-used (locks;tx) ordering. so,
882 * UGLY thing is that we'll use one ordering for
883 * data (ofd) and reverse ordering for metadata
884 * (mdd). then at some point we'll fix the latter
886 if (osd_dt_dev(th->th_dev)->od_is_md) {
887 LASSERT(oti->oti_r_locks == 0);
888 LASSERT(oti->oti_w_locks == 0);
890 rc = dt_txn_hook_stop(env, th);
892 CERROR("Failure in transaction hook: %d\n", rc);
893 oh->ot_handle = NULL;
894 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
895 rc = ldiskfs_journal_stop(hdl));
897 CERROR("Failure to stop transaction: %d\n", rc);
902 /* as we want IO to journal and data IO be concurrent, we don't block
903 * awaiting data IO completion in osd_do_bio(), instead we wait here
904 * once transaction is submitted to the journal. all reqular requests
905 * don't do direct IO (except read/write), thus this wait_event becomes
908 * IMPORTANT: we have to wait till any IO submited by the thread is
909 * completed otherwise iobuf may be corrupted by different request
911 cfs_wait_event(iobuf->dr_wait,
912 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
914 rc = iobuf->dr_error;
919 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
921 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
924 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
930 * Called just before object is freed. Releases all resources except for
931 * object itself (that is released by osd_object_free()).
933 * Concurrency: no concurrent access is possible that late in object
936 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
938 struct osd_object *obj = osd_obj(l);
939 struct inode *inode = obj->oo_inode;
941 LINVRNT(osd_invariant(obj));
944 * If object is unlinked remove fid->ino mapping from object index.
950 obj->oo_inode = NULL;
955 * Concurrency: ->loo_object_release() is called under site spin-lock.
957 static void osd_object_release(const struct lu_env *env,
963 * Concurrency: shouldn't matter.
965 static int osd_object_print(const struct lu_env *env, void *cookie,
966 lu_printer_t p, const struct lu_object *l)
968 struct osd_object *o = osd_obj(l);
971 if (o->oo_dir != NULL)
972 d = o->oo_dir->od_container.ic_descr;
975 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
977 o->oo_inode ? o->oo_inode->i_ino : 0UL,
978 o->oo_inode ? o->oo_inode->i_generation : 0,
979 d ? d->id_ops->id_name : "plain");
983 * Concurrency: shouldn't matter.
985 int osd_statfs(const struct lu_env *env, struct dt_device *d,
986 struct obd_statfs *sfs)
988 struct osd_device *osd = osd_dt_dev(d);
989 struct super_block *sb = osd_sb(osd);
990 struct kstatfs *ksfs;
993 if (unlikely(osd->od_mount == NULL))
996 /* osd_lproc.c call this without env, allocate ksfs for that case */
997 if (unlikely(env == NULL)) {
1002 ksfs = &osd_oti_get(env)->oti_ksfs;
1005 cfs_spin_lock(&osd->od_osfs_lock);
1006 /* cache 1 second */
1007 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1008 result = ll_do_statfs(sb, ksfs);
1009 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1010 osd->od_osfs_age = cfs_time_current_64();
1011 statfs_pack(&osd->od_statfs, ksfs);
1015 if (likely(result == 0))
1016 *sfs = osd->od_statfs;
1017 cfs_spin_unlock(&osd->od_osfs_lock);
1019 if (unlikely(env == NULL))
1026 * Concurrency: doesn't access mutable data.
1028 static void osd_conf_get(const struct lu_env *env,
1029 const struct dt_device *dev,
1030 struct dt_device_param *param)
1032 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1035 * XXX should be taken from not-yet-existing fs abstraction layer.
1037 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1038 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1039 param->ddp_block_shift = sb->s_blocksize_bits;
1040 param->ddp_mntopts = 0;
1041 if (test_opt(sb, XATTR_USER))
1042 param->ddp_mntopts |= MNTOPT_USERXATTR;
1043 if (test_opt(sb, POSIX_ACL))
1044 param->ddp_mntopts |= MNTOPT_ACL;
1046 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1047 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1048 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1051 param->ddp_max_ea_size = sb->s_blocksize;
1056 * Helper function to get and fill the buffer with input values.
1058 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1062 buf = &osd_oti_get(env)->oti_buf;
1069 * Concurrency: shouldn't matter.
1071 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1073 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1074 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1078 * Start commit for OSD device.
1080 * An implementation of dt_commit_async method for OSD device.
1081 * Asychronously starts underlayng fs sync and thereby a transaction
1084 * \param env environment
1085 * \param d dt device
1087 * \see dt_device_operations
1089 static int osd_commit_async(const struct lu_env *env,
1090 struct dt_device *d)
1092 struct super_block *s = osd_sb(osd_dt_dev(d));
1095 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1096 RETURN(s->s_op->sync_fs(s, 0));
1100 * Concurrency: shouldn't matter.
1103 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1105 struct super_block *sb = osd_sb(osd_dt_dev(d));
1109 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1111 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1116 * Concurrency: serialization provided by callers.
1118 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1119 int mode, unsigned long timeout, __u32 alg,
1120 struct lustre_capa_key *keys)
1122 struct osd_device *dev = osd_dt_dev(d);
1125 dev->od_fl_capa = mode;
1126 dev->od_capa_timeout = timeout;
1127 dev->od_capa_alg = alg;
1128 dev->od_capa_keys = keys;
1133 * Concurrency: serialization provided by callers.
1135 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1136 struct dt_quota_ctxt *ctxt, void *data)
1138 struct obd_device *obd = (void *)ctxt;
1139 struct vfsmount *mnt = (struct vfsmount *)data;
1142 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1143 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1144 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1145 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1146 obd->obd_lvfs_ctxt.fs = get_ds();
1152 * Note: we do not count into QUOTA here.
1153 * If we mount with --data_journal we may need more.
1155 const int osd_dto_credits_noquota[DTO_NR] = {
1158 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1159 * SINGLEDATA_TRANS_BLOCKS(8)
1160 * XXX Note: maybe iam need more, since iam have more level than
1163 [DTO_INDEX_INSERT] = 16,
1164 [DTO_INDEX_DELETE] = 16,
1168 [DTO_INDEX_UPDATE] = 16,
1170 * Create a object. The same as create object in EXT3.
1171 * DATA_TRANS_BLOCKS(14) +
1172 * INDEX_EXTRA_BLOCKS(8) +
1173 * 3(inode bits, groups, GDT)
1175 [DTO_OBJECT_CREATE] = 25,
1177 * XXX: real credits to be fixed
1179 [DTO_OBJECT_DELETE] = 25,
1181 * Attr set credits (inode)
1183 [DTO_ATTR_SET_BASE] = 1,
1185 * Xattr set. The same as xattr of EXT3.
1186 * DATA_TRANS_BLOCKS(14)
1187 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1188 * are also counted in. Do not know why?
1190 [DTO_XATTR_SET] = 14,
1193 * credits for inode change during write.
1195 [DTO_WRITE_BASE] = 3,
1197 * credits for single block write.
1199 [DTO_WRITE_BLOCK] = 14,
1201 * Attr set credits for chown.
1202 * This is extra credits for setattr, and it is null without quota
1204 [DTO_ATTR_SET_CHOWN]= 0
1207 static const struct dt_device_operations osd_dt_ops = {
1208 .dt_root_get = osd_root_get,
1209 .dt_statfs = osd_statfs,
1210 .dt_trans_create = osd_trans_create,
1211 .dt_trans_start = osd_trans_start,
1212 .dt_trans_stop = osd_trans_stop,
1213 .dt_trans_cb_add = osd_trans_cb_add,
1214 .dt_conf_get = osd_conf_get,
1215 .dt_sync = osd_sync,
1217 .dt_commit_async = osd_commit_async,
1218 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1219 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1222 static void osd_object_read_lock(const struct lu_env *env,
1223 struct dt_object *dt, unsigned role)
1225 struct osd_object *obj = osd_dt_obj(dt);
1226 struct osd_thread_info *oti = osd_oti_get(env);
1228 LINVRNT(osd_invariant(obj));
1230 LASSERT(obj->oo_owner != env);
1231 cfs_down_read_nested(&obj->oo_sem, role);
1233 LASSERT(obj->oo_owner == NULL);
1237 static void osd_object_write_lock(const struct lu_env *env,
1238 struct dt_object *dt, unsigned role)
1240 struct osd_object *obj = osd_dt_obj(dt);
1241 struct osd_thread_info *oti = osd_oti_get(env);
1243 LINVRNT(osd_invariant(obj));
1245 LASSERT(obj->oo_owner != env);
1246 cfs_down_write_nested(&obj->oo_sem, role);
1248 LASSERT(obj->oo_owner == NULL);
1249 obj->oo_owner = env;
1253 static void osd_object_read_unlock(const struct lu_env *env,
1254 struct dt_object *dt)
1256 struct osd_object *obj = osd_dt_obj(dt);
1257 struct osd_thread_info *oti = osd_oti_get(env);
1259 LINVRNT(osd_invariant(obj));
1261 LASSERT(oti->oti_r_locks > 0);
1263 cfs_up_read(&obj->oo_sem);
1266 static void osd_object_write_unlock(const struct lu_env *env,
1267 struct dt_object *dt)
1269 struct osd_object *obj = osd_dt_obj(dt);
1270 struct osd_thread_info *oti = osd_oti_get(env);
1272 LINVRNT(osd_invariant(obj));
1274 LASSERT(obj->oo_owner == env);
1275 LASSERT(oti->oti_w_locks > 0);
1277 obj->oo_owner = NULL;
1278 cfs_up_write(&obj->oo_sem);
1281 static int osd_object_write_locked(const struct lu_env *env,
1282 struct dt_object *dt)
1284 struct osd_object *obj = osd_dt_obj(dt);
1286 LINVRNT(osd_invariant(obj));
1288 return obj->oo_owner == env;
1291 static int capa_is_sane(const struct lu_env *env,
1292 struct osd_device *dev,
1293 struct lustre_capa *capa,
1294 struct lustre_capa_key *keys)
1296 struct osd_thread_info *oti = osd_oti_get(env);
1297 struct lustre_capa *tcapa = &oti->oti_capa;
1298 struct obd_capa *oc;
1302 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1304 if (capa_is_expired(oc)) {
1305 DEBUG_CAPA(D_ERROR, capa, "expired");
1312 if (capa_is_expired_sec(capa)) {
1313 DEBUG_CAPA(D_ERROR, capa, "expired");
1317 cfs_spin_lock(&capa_lock);
1318 for (i = 0; i < 2; i++) {
1319 if (keys[i].lk_keyid == capa->lc_keyid) {
1320 oti->oti_capa_key = keys[i];
1324 cfs_spin_unlock(&capa_lock);
1327 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1331 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1335 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1336 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1340 oc = capa_add(dev->od_capa_hash, capa);
1346 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1347 struct lustre_capa *capa, __u64 opc)
1349 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1350 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1351 struct md_capainfo *ci;
1354 if (!dev->od_fl_capa)
1357 if (capa == BYPASS_CAPA)
1360 ci = md_capainfo(env);
1364 if (ci->mc_auth == LC_ID_NONE)
1368 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1372 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1373 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1378 if (!capa_opc_supported(capa, opc)) {
1379 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1383 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1384 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1391 static struct timespec *osd_inode_time(const struct lu_env *env,
1392 struct inode *inode, __u64 seconds)
1394 struct osd_thread_info *oti = osd_oti_get(env);
1395 struct timespec *t = &oti->oti_time;
1397 t->tv_sec = seconds;
1399 *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1404 static void osd_inode_getattr(const struct lu_env *env,
1405 struct inode *inode, struct lu_attr *attr)
1407 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1408 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1409 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1411 attr->la_atime = LTIME_S(inode->i_atime);
1412 attr->la_mtime = LTIME_S(inode->i_mtime);
1413 attr->la_ctime = LTIME_S(inode->i_ctime);
1414 attr->la_mode = inode->i_mode;
1415 attr->la_size = i_size_read(inode);
1416 attr->la_blocks = inode->i_blocks;
1417 attr->la_uid = inode->i_uid;
1418 attr->la_gid = inode->i_gid;
1419 attr->la_flags = LDISKFS_I(inode)->i_flags;
1420 attr->la_nlink = inode->i_nlink;
1421 attr->la_rdev = inode->i_rdev;
1422 attr->la_blksize = ll_inode_blksize(inode);
1423 attr->la_blkbits = inode->i_blkbits;
1426 static int osd_attr_get(const struct lu_env *env,
1427 struct dt_object *dt,
1428 struct lu_attr *attr,
1429 struct lustre_capa *capa)
1431 struct osd_object *obj = osd_dt_obj(dt);
1433 LASSERT(dt_object_exists(dt));
1434 LINVRNT(osd_invariant(obj));
1436 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1439 cfs_spin_lock(&obj->oo_guard);
1440 osd_inode_getattr(env, obj->oo_inode, attr);
1441 cfs_spin_unlock(&obj->oo_guard);
1445 static int osd_declare_attr_set(const struct lu_env *env,
1446 struct dt_object *dt,
1447 const struct lu_attr *attr,
1448 struct thandle *handle)
1450 struct osd_thandle *oh;
1451 struct osd_object *obj;
1453 LASSERT(dt != NULL);
1454 LASSERT(handle != NULL);
1456 obj = osd_dt_obj(dt);
1457 LASSERT(osd_invariant(obj));
1459 oh = container_of0(handle, struct osd_thandle, ot_super);
1460 LASSERT(oh->ot_handle == NULL);
1462 OSD_DECLARE_OP(oh, attr_set);
1463 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1465 if (attr && attr->la_valid & LA_UID) {
1467 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1469 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1471 if (attr && attr->la_valid & LA_GID) {
1473 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1475 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1481 static int osd_inode_setattr(const struct lu_env *env,
1482 struct inode *inode, const struct lu_attr *attr)
1486 bits = attr->la_valid;
1488 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1490 if (bits & LA_ATIME)
1491 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1492 if (bits & LA_CTIME)
1493 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1494 if (bits & LA_MTIME)
1495 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1496 if (bits & LA_SIZE) {
1497 LDISKFS_I(inode)->i_disksize = attr->la_size;
1498 i_size_write(inode, attr->la_size);
1502 /* OSD should not change "i_blocks" which is used by quota.
1503 * "i_blocks" should be changed by ldiskfs only. */
1504 if (bits & LA_BLOCKS)
1505 inode->i_blocks = attr->la_blocks;
1508 inode->i_mode = (inode->i_mode & S_IFMT) |
1509 (attr->la_mode & ~S_IFMT);
1511 inode->i_uid = attr->la_uid;
1513 inode->i_gid = attr->la_gid;
1514 if (bits & LA_NLINK)
1515 inode->i_nlink = attr->la_nlink;
1517 inode->i_rdev = attr->la_rdev;
1519 if (bits & LA_FLAGS) {
1520 /* always keep S_NOCMTIME */
1521 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1527 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1529 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1530 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1535 if (attr->la_valid & LA_UID)
1536 iattr.ia_valid |= ATTR_UID;
1537 if (attr->la_valid & LA_GID)
1538 iattr.ia_valid |= ATTR_GID;
1539 iattr.ia_uid = attr->la_uid;
1540 iattr.ia_gid = attr->la_gid;
1542 rc = ll_vfs_dq_transfer(inode, &iattr);
1544 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1545 "enforcement enabled on the ldiskfs filesystem?",
1546 inode->i_sb->s_id, rc);
1553 static int osd_attr_set(const struct lu_env *env,
1554 struct dt_object *dt,
1555 const struct lu_attr *attr,
1556 struct thandle *handle,
1557 struct lustre_capa *capa)
1559 struct osd_object *obj = osd_dt_obj(dt);
1560 struct inode *inode;
1563 LASSERT(handle != NULL);
1564 LASSERT(dt_object_exists(dt));
1565 LASSERT(osd_invariant(obj));
1567 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1570 OSD_EXEC_OP(handle, attr_set);
1572 inode = obj->oo_inode;
1573 if (!osd_dt_dev(handle->th_dev)->od_is_md) {
1575 rc = osd_quota_transfer(inode, attr);
1579 #ifdef HAVE_QUOTA_SUPPORT
1580 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1581 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1582 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1587 if (attr->la_valid & LA_UID)
1588 iattr.ia_valid |= ATTR_UID;
1589 if (attr->la_valid & LA_GID)
1590 iattr.ia_valid |= ATTR_GID;
1591 iattr.ia_uid = attr->la_uid;
1592 iattr.ia_gid = attr->la_gid;
1593 osd_push_ctxt(env, save, 1);
1594 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1595 osd_pop_ctxt(save, 1);
1601 cfs_spin_lock(&obj->oo_guard);
1602 rc = osd_inode_setattr(env, inode, attr);
1603 cfs_spin_unlock(&obj->oo_guard);
1606 inode->i_sb->s_op->dirty_inode(inode);
1610 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1611 struct osd_object *obj,
1612 const char *name, const int namelen)
1614 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1617 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1619 struct dt_allocation_hint *hint,
1623 struct osd_device *osd = osd_obj2dev(obj);
1624 struct osd_thandle *oth;
1625 struct dt_object *parent = NULL;
1626 struct inode *inode;
1627 #ifdef HAVE_QUOTA_SUPPORT
1628 struct osd_ctxt *save = &info->oti_ctxt;
1631 LINVRNT(osd_invariant(obj));
1632 LASSERT(obj->oo_inode == NULL);
1633 LASSERT(obj->oo_hl_head == NULL);
1635 if (S_ISDIR(mode) && ldiskfs_pdo) {
1636 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1637 if (obj->oo_hl_head == NULL)
1641 oth = container_of(th, struct osd_thandle, ot_super);
1642 LASSERT(oth->ot_handle->h_transaction != NULL);
1644 if (hint && hint->dah_parent)
1645 parent = hint->dah_parent;
1647 #ifdef HAVE_QUOTA_SUPPORT
1648 osd_push_ctxt(info->oti_env, save, osd_dt_dev(th->th_dev)->od_is_md);
1650 inode = ldiskfs_create_inode(oth->ot_handle,
1651 parent ? osd_dt_obj(parent)->oo_inode :
1652 osd_sb(osd)->s_root->d_inode,
1654 #ifdef HAVE_QUOTA_SUPPORT
1655 osd_pop_ctxt(save, osd_dt_dev(th->th_dev)->od_is_md);
1657 if (!IS_ERR(inode)) {
1658 /* Do not update file c/mtime in ldiskfs.
1659 * NB: don't need any lock because no contention at this
1661 inode->i_flags |= S_NOCMTIME;
1662 inode->i_state |= I_LUSTRE_NOSCRUB;
1663 obj->oo_inode = inode;
1666 if (obj->oo_hl_head != NULL) {
1667 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1668 obj->oo_hl_head = NULL;
1670 result = PTR_ERR(inode);
1672 LINVRNT(osd_invariant(obj));
1680 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1681 struct lu_attr *attr,
1682 struct dt_allocation_hint *hint,
1683 struct dt_object_format *dof,
1687 struct osd_thandle *oth;
1688 struct osd_device *osd = osd_obj2dev(obj);
1689 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1691 LASSERT(S_ISDIR(attr->la_mode));
1693 oth = container_of(th, struct osd_thandle, ot_super);
1694 LASSERT(oth->ot_handle->h_transaction != NULL);
1695 result = osd_mkfile(info, obj, mode, hint, th);
1696 if (result == 0 && osd->od_iop_mode == 0) {
1697 LASSERT(obj->oo_inode != NULL);
1699 * XXX uh-oh... call low-level iam function directly.
1702 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1703 sizeof (struct osd_fid_pack),
1709 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1710 struct lu_attr *attr,
1711 struct dt_allocation_hint *hint,
1712 struct dt_object_format *dof,
1716 struct osd_thandle *oth;
1717 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1719 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1721 LASSERT(S_ISREG(attr->la_mode));
1723 oth = container_of(th, struct osd_thandle, ot_super);
1724 LASSERT(oth->ot_handle->h_transaction != NULL);
1726 result = osd_mkfile(info, obj, mode, hint, th);
1728 LASSERT(obj->oo_inode != NULL);
1729 if (feat->dif_flags & DT_IND_VARKEY)
1730 result = iam_lvar_create(obj->oo_inode,
1731 feat->dif_keysize_max,
1733 feat->dif_recsize_max,
1736 result = iam_lfix_create(obj->oo_inode,
1737 feat->dif_keysize_max,
1739 feat->dif_recsize_max,
1746 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1747 struct lu_attr *attr,
1748 struct dt_allocation_hint *hint,
1749 struct dt_object_format *dof,
1752 LASSERT(S_ISREG(attr->la_mode));
1753 return osd_mkfile(info, obj, (attr->la_mode &
1754 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1757 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1758 struct lu_attr *attr,
1759 struct dt_allocation_hint *hint,
1760 struct dt_object_format *dof,
1763 LASSERT(S_ISLNK(attr->la_mode));
1764 return osd_mkfile(info, obj, (attr->la_mode &
1765 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1768 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1769 struct lu_attr *attr,
1770 struct dt_allocation_hint *hint,
1771 struct dt_object_format *dof,
1774 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1777 LINVRNT(osd_invariant(obj));
1778 LASSERT(obj->oo_inode == NULL);
1779 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1780 S_ISFIFO(mode) || S_ISSOCK(mode));
1782 result = osd_mkfile(info, obj, mode, hint, th);
1784 LASSERT(obj->oo_inode != NULL);
1786 * This inode should be marked dirty for i_rdev. Currently
1787 * that is done in the osd_attr_init().
1789 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1791 LINVRNT(osd_invariant(obj));
1795 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1797 struct dt_allocation_hint *hint,
1798 struct dt_object_format *dof,
1801 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1803 osd_obj_type_f result;
1819 result = osd_mk_index;
1830 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1831 struct dt_object *parent, cfs_umode_t child_mode)
1835 memset(ah, 0, sizeof(*ah));
1836 ah->dah_parent = parent;
1837 ah->dah_mode = child_mode;
1840 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1841 struct lu_attr *attr, struct dt_object_format *dof)
1843 struct inode *inode = obj->oo_inode;
1844 __u64 valid = attr->la_valid;
1847 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1849 if (dof->dof_type != DFT_NODE)
1850 attr->la_valid &= ~LA_RDEV;
1851 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1852 attr->la_valid &= ~LA_ATIME;
1853 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1854 attr->la_valid &= ~LA_CTIME;
1855 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1856 attr->la_valid &= ~LA_MTIME;
1858 if (!osd_obj2dev(obj)->od_is_md) {
1860 result = osd_quota_transfer(inode, attr);
1864 #ifdef HAVE_QUOTA_SUPPORT
1865 attr->la_valid &= ~(LA_UID | LA_GID);
1869 if (attr->la_valid != 0) {
1870 result = osd_inode_setattr(info->oti_env, inode, attr);
1872 * The osd_inode_setattr() should always succeed here. The
1873 * only error that could be returned is EDQUOT when we are
1874 * trying to change the UID or GID of the inode. However, this
1875 * should not happen since quota enforcement is no longer
1876 * enabled on ldiskfs (lquota takes care of it).
1878 LASSERTF(result == 0, "%d", result);
1879 inode->i_sb->s_op->dirty_inode(inode);
1882 attr->la_valid = valid;
1886 * Helper function for osd_object_create()
1888 * \retval 0, on success
1890 static int __osd_object_create(struct osd_thread_info *info,
1891 struct osd_object *obj, struct lu_attr *attr,
1892 struct dt_allocation_hint *hint,
1893 struct dt_object_format *dof,
1899 /* we drop umask so that permissions we pass are not affected */
1900 umask = current->fs->umask;
1901 current->fs->umask = 0;
1903 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1906 osd_attr_init(info, obj, attr, dof);
1907 osd_object_init0(obj);
1909 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1910 unlock_new_inode(obj->oo_inode);
1913 /* restore previous umask value */
1914 current->fs->umask = umask;
1920 * Helper function for osd_object_create()
1922 * \retval 0, on success
1924 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1925 const struct lu_fid *fid, struct thandle *th)
1927 struct osd_thread_info *info = osd_oti_get(env);
1928 struct osd_inode_id *id = &info->oti_id;
1929 struct osd_device *osd = osd_obj2dev(obj);
1931 LASSERT(obj->oo_inode != NULL);
1933 if (osd->od_is_md) {
1934 struct md_ucred *uc = md_ucred(env);
1935 LASSERT(uc != NULL);
1938 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1939 return osd_oi_insert(info, osd, fid, id, th);
1942 static int osd_declare_object_create(const struct lu_env *env,
1943 struct dt_object *dt,
1944 struct lu_attr *attr,
1945 struct dt_allocation_hint *hint,
1946 struct dt_object_format *dof,
1947 struct thandle *handle)
1949 struct osd_thandle *oh;
1951 LASSERT(handle != NULL);
1953 oh = container_of0(handle, struct osd_thandle, ot_super);
1954 LASSERT(oh->ot_handle == NULL);
1956 OSD_DECLARE_OP(oh, create);
1957 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1958 /* XXX: So far, only normal fid needs be inserted into the oi,
1959 * things could be changed later. Revise following code then. */
1960 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1961 OSD_DECLARE_OP(oh, insert);
1962 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1964 /* If this is directory, then we expect . and .. to be inserted as
1965 * well. The one directory block always needs to be created for the
1966 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1967 * block), there is no danger of needing a tree for the first block.
1969 if (attr && S_ISDIR(attr->la_mode)) {
1970 OSD_DECLARE_OP(oh, insert);
1971 OSD_DECLARE_OP(oh, insert);
1972 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1976 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1977 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1982 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1983 struct lu_attr *attr,
1984 struct dt_allocation_hint *hint,
1985 struct dt_object_format *dof,
1988 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1989 struct osd_object *obj = osd_dt_obj(dt);
1990 struct osd_thread_info *info = osd_oti_get(env);
1995 LINVRNT(osd_invariant(obj));
1996 LASSERT(!dt_object_exists(dt));
1997 LASSERT(osd_write_locked(env, obj));
1998 LASSERT(th != NULL);
2000 if (unlikely(fid_is_acct(fid)))
2001 /* Quota files can't be created from the kernel any more,
2002 * 'tune2fs -O quota' will take care of creating them */
2005 OSD_EXEC_OP(th, create);
2007 result = __osd_object_create(info, obj, attr, hint, dof, th);
2009 result = __osd_oi_insert(env, obj, fid, th);
2011 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2012 LASSERT(osd_invariant(obj));
2017 * Called to destroy on-disk representation of the object
2019 * Concurrency: must be locked
2021 static int osd_declare_object_destroy(const struct lu_env *env,
2022 struct dt_object *dt,
2025 struct osd_object *obj = osd_dt_obj(dt);
2026 struct inode *inode = obj->oo_inode;
2027 struct osd_thandle *oh;
2031 oh = container_of0(th, struct osd_thandle, ot_super);
2032 LASSERT(oh->ot_handle == NULL);
2035 OSD_DECLARE_OP(oh, destroy);
2036 OSD_DECLARE_OP(oh, delete);
2037 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2038 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2040 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
2041 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
2046 static int osd_object_destroy(const struct lu_env *env,
2047 struct dt_object *dt,
2050 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2051 struct osd_object *obj = osd_dt_obj(dt);
2052 struct inode *inode = obj->oo_inode;
2053 struct osd_device *osd = osd_obj2dev(obj);
2054 struct osd_thandle *oh;
2058 oh = container_of0(th, struct osd_thandle, ot_super);
2059 LASSERT(oh->ot_handle);
2061 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2063 if (unlikely(fid_is_acct(fid)))
2066 /* Parallel control for OI scrub. For most of cases, there is no
2067 * lock contention. So it will not affect unlink performance. */
2068 cfs_mutex_lock(&inode->i_mutex);
2069 if (S_ISDIR(inode->i_mode)) {
2070 LASSERT(osd_inode_unlinked(inode) ||
2071 inode->i_nlink == 1);
2072 cfs_spin_lock(&obj->oo_guard);
2074 cfs_spin_unlock(&obj->oo_guard);
2075 inode->i_sb->s_op->dirty_inode(inode);
2077 LASSERT(osd_inode_unlinked(inode));
2080 OSD_EXEC_OP(th, destroy);
2082 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2083 cfs_mutex_unlock(&inode->i_mutex);
2085 /* XXX: add to ext3 orphan list */
2086 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2088 /* not needed in the cache anymore */
2089 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2095 * Helper function for osd_xattr_set()
2097 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2098 const struct lu_buf *buf, const char *name, int fl)
2100 struct osd_object *obj = osd_dt_obj(dt);
2101 struct inode *inode = obj->oo_inode;
2102 struct osd_thread_info *info = osd_oti_get(env);
2103 struct dentry *dentry = &info->oti_child_dentry;
2107 LASSERT(dt_object_exists(dt));
2108 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2110 if (fl & LU_XATTR_REPLACE)
2111 fs_flags |= XATTR_REPLACE;
2113 if (fl & LU_XATTR_CREATE)
2114 fs_flags |= XATTR_CREATE;
2116 dentry->d_inode = inode;
2117 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2118 buf->lb_len, fs_flags);
2123 * Put the fid into lustre_mdt_attrs, and then place the structure
2124 * inode's ea. This fid should not be altered during the life time
2127 * \retval +ve, on success
2128 * \retval -ve, on error
2130 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2132 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2133 const struct lu_fid *fid)
2135 struct osd_thread_info *info = osd_oti_get(env);
2136 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2138 lustre_lma_init(mdt_attrs, fid);
2139 lustre_lma_swab(mdt_attrs);
2140 return __osd_xattr_set(env, dt,
2141 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2142 XATTR_NAME_LMA, LU_XATTR_CREATE);
2147 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2148 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2149 * To have compatilibility with 1.8 ldiskfs driver we need to have
2150 * magic number at start of fid data.
2151 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2154 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2155 const struct dt_rec *fid)
2157 param->edp_magic = LDISKFS_LUFID_MAGIC;
2158 param->edp_len = sizeof(struct lu_fid) + 1;
2160 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2161 (struct lu_fid *)fid);
2165 * Try to read the fid from inode ea into dt_rec, if return value
2166 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2168 * \param fid object fid.
2170 * \retval 0 on success
2172 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2173 __u32 ino, struct lu_fid *fid,
2174 struct osd_inode_id *id)
2176 struct osd_thread_info *info = osd_oti_get(env);
2177 struct inode *inode;
2180 osd_id_gen(id, ino, OSD_OII_NOGEN);
2181 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2183 RETURN(PTR_ERR(inode));
2190 * OSD layer object create function for interoperability mode (b11826).
2191 * This is mostly similar to osd_object_create(). Only difference being, fid is
2192 * inserted into inode ea here.
2194 * \retval 0, on success
2195 * \retval -ve, on error
2197 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2198 struct lu_attr *attr,
2199 struct dt_allocation_hint *hint,
2200 struct dt_object_format *dof,
2203 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2204 struct osd_object *obj = osd_dt_obj(dt);
2205 struct osd_thread_info *info = osd_oti_get(env);
2210 LASSERT(osd_invariant(obj));
2211 LASSERT(!dt_object_exists(dt));
2212 LASSERT(osd_write_locked(env, obj));
2213 LASSERT(th != NULL);
2215 if (unlikely(fid_is_acct(fid)))
2216 /* Quota files can't be created from the kernel any more,
2217 * 'tune2fs -O quota' will take care of creating them */
2220 OSD_EXEC_OP(th, create);
2222 result = __osd_object_create(info, obj, attr, hint, dof, th);
2223 /* objects under osd root shld have igif fid, so dont add fid EA */
2224 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2225 result = osd_ea_fid_set(env, dt, fid);
2228 result = __osd_oi_insert(env, obj, fid, th);
2230 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2231 LINVRNT(osd_invariant(obj));
2235 static int osd_declare_object_ref_add(const struct lu_env *env,
2236 struct dt_object *dt,
2237 struct thandle *handle)
2239 struct osd_thandle *oh;
2241 /* it's possible that object doesn't exist yet */
2242 LASSERT(handle != NULL);
2244 oh = container_of0(handle, struct osd_thandle, ot_super);
2245 LASSERT(oh->ot_handle == NULL);
2247 OSD_DECLARE_OP(oh, ref_add);
2248 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2254 * Concurrency: @dt is write locked.
2256 static int osd_object_ref_add(const struct lu_env *env,
2257 struct dt_object *dt, struct thandle *th)
2259 struct osd_object *obj = osd_dt_obj(dt);
2260 struct inode *inode = obj->oo_inode;
2262 LINVRNT(osd_invariant(obj));
2263 LASSERT(dt_object_exists(dt));
2264 LASSERT(osd_write_locked(env, obj));
2265 LASSERT(th != NULL);
2267 OSD_EXEC_OP(th, ref_add);
2270 * DIR_NLINK feature is set for compatibility reasons if:
2271 * 1) nlinks > LDISKFS_LINK_MAX, or
2272 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2274 * It is easier to always set this flag (rather than check and set),
2275 * since it has less overhead, and the superblock will be dirtied
2276 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2277 * do not actually care whether this flag is set or not.
2279 cfs_spin_lock(&obj->oo_guard);
2281 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2282 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2283 inode->i_nlink == 2)
2286 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2287 cfs_spin_unlock(&obj->oo_guard);
2288 inode->i_sb->s_op->dirty_inode(inode);
2289 LINVRNT(osd_invariant(obj));
2294 static int osd_declare_object_ref_del(const struct lu_env *env,
2295 struct dt_object *dt,
2296 struct thandle *handle)
2298 struct osd_thandle *oh;
2300 LASSERT(dt_object_exists(dt));
2301 LASSERT(handle != NULL);
2303 oh = container_of0(handle, struct osd_thandle, ot_super);
2304 LASSERT(oh->ot_handle == NULL);
2306 OSD_DECLARE_OP(oh, ref_del);
2307 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2313 * Concurrency: @dt is write locked.
2315 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2318 struct osd_object *obj = osd_dt_obj(dt);
2319 struct inode *inode = obj->oo_inode;
2321 LINVRNT(osd_invariant(obj));
2322 LASSERT(dt_object_exists(dt));
2323 LASSERT(osd_write_locked(env, obj));
2324 LASSERT(th != NULL);
2326 OSD_EXEC_OP(th, ref_del);
2328 cfs_spin_lock(&obj->oo_guard);
2329 LASSERT(inode->i_nlink > 0);
2331 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2332 * then the nlink count is 1. Don't let it be set to 0 or the directory
2333 * inode will be deleted incorrectly. */
2334 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2336 cfs_spin_unlock(&obj->oo_guard);
2337 inode->i_sb->s_op->dirty_inode(inode);
2338 LINVRNT(osd_invariant(obj));
2344 * Get the 64-bit version for an inode.
2346 static int osd_object_version_get(const struct lu_env *env,
2347 struct dt_object *dt, dt_obj_version_t *ver)
2349 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2351 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2352 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2353 *ver = LDISKFS_I(inode)->i_fs_version;
2358 * Concurrency: @dt is read locked.
2360 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2361 struct lu_buf *buf, const char *name,
2362 struct lustre_capa *capa)
2364 struct osd_object *obj = osd_dt_obj(dt);
2365 struct inode *inode = obj->oo_inode;
2366 struct osd_thread_info *info = osd_oti_get(env);
2367 struct dentry *dentry = &info->oti_obj_dentry;
2369 /* version get is not real XATTR but uses xattr API */
2370 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2371 /* for version we are just using xattr API but change inode
2373 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2374 osd_object_version_get(env, dt, buf->lb_buf);
2375 return sizeof(dt_obj_version_t);
2378 LASSERT(dt_object_exists(dt));
2379 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2380 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2382 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2385 dentry->d_inode = inode;
2386 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2390 static int osd_declare_xattr_set(const struct lu_env *env,
2391 struct dt_object *dt,
2392 const struct lu_buf *buf, const char *name,
2393 int fl, struct thandle *handle)
2395 struct osd_thandle *oh;
2397 LASSERT(handle != NULL);
2399 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2400 /* no credits for version */
2404 oh = container_of0(handle, struct osd_thandle, ot_super);
2405 LASSERT(oh->ot_handle == NULL);
2407 OSD_DECLARE_OP(oh, xattr_set);
2408 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2414 * Set the 64-bit version for object
2416 static void osd_object_version_set(const struct lu_env *env,
2417 struct dt_object *dt,
2418 dt_obj_version_t *new_version)
2420 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2422 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2423 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2425 LDISKFS_I(inode)->i_fs_version = *new_version;
2426 /** Version is set after all inode operations are finished,
2427 * so we should mark it dirty here */
2428 inode->i_sb->s_op->dirty_inode(inode);
2432 * Concurrency: @dt is write locked.
2434 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2435 const struct lu_buf *buf, const char *name, int fl,
2436 struct thandle *handle, struct lustre_capa *capa)
2438 LASSERT(handle != NULL);
2440 /* version set is not real XATTR */
2441 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2442 /* for version we are just using xattr API but change inode
2444 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2445 osd_object_version_set(env, dt, buf->lb_buf);
2446 return sizeof(dt_obj_version_t);
2449 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2452 OSD_EXEC_OP(handle, xattr_set);
2453 return __osd_xattr_set(env, dt, buf, name, fl);
2457 * Concurrency: @dt is read locked.
2459 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2460 struct lu_buf *buf, struct lustre_capa *capa)
2462 struct osd_object *obj = osd_dt_obj(dt);
2463 struct inode *inode = obj->oo_inode;
2464 struct osd_thread_info *info = osd_oti_get(env);
2465 struct dentry *dentry = &info->oti_obj_dentry;
2467 LASSERT(dt_object_exists(dt));
2468 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2469 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2471 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2474 dentry->d_inode = inode;
2475 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2478 static int osd_declare_xattr_del(const struct lu_env *env,
2479 struct dt_object *dt, const char *name,
2480 struct thandle *handle)
2482 struct osd_thandle *oh;
2484 LASSERT(dt_object_exists(dt));
2485 LASSERT(handle != NULL);
2487 oh = container_of0(handle, struct osd_thandle, ot_super);
2488 LASSERT(oh->ot_handle == NULL);
2490 OSD_DECLARE_OP(oh, xattr_set);
2491 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2497 * Concurrency: @dt is write locked.
2499 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2500 const char *name, struct thandle *handle,
2501 struct lustre_capa *capa)
2503 struct osd_object *obj = osd_dt_obj(dt);
2504 struct inode *inode = obj->oo_inode;
2505 struct osd_thread_info *info = osd_oti_get(env);
2506 struct dentry *dentry = &info->oti_obj_dentry;
2509 LASSERT(dt_object_exists(dt));
2510 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2511 LASSERT(osd_write_locked(env, obj));
2512 LASSERT(handle != NULL);
2514 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2517 OSD_EXEC_OP(handle, xattr_set);
2519 dentry->d_inode = inode;
2520 rc = inode->i_op->removexattr(dentry, name);
2524 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2525 struct dt_object *dt,
2526 struct lustre_capa *old,
2529 struct osd_thread_info *info = osd_oti_get(env);
2530 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2531 struct osd_object *obj = osd_dt_obj(dt);
2532 struct osd_device *dev = osd_obj2dev(obj);
2533 struct lustre_capa_key *key = &info->oti_capa_key;
2534 struct lustre_capa *capa = &info->oti_capa;
2535 struct obd_capa *oc;
2536 struct md_capainfo *ci;
2540 if (!dev->od_fl_capa)
2541 RETURN(ERR_PTR(-ENOENT));
2543 LASSERT(dt_object_exists(dt));
2544 LINVRNT(osd_invariant(obj));
2546 /* renewal sanity check */
2547 if (old && osd_object_auth(env, dt, old, opc))
2548 RETURN(ERR_PTR(-EACCES));
2550 ci = md_capainfo(env);
2552 RETURN(ERR_PTR(-ENOENT));
2554 switch (ci->mc_auth) {
2558 capa->lc_uid = obj->oo_inode->i_uid;
2559 capa->lc_gid = obj->oo_inode->i_gid;
2560 capa->lc_flags = LC_ID_PLAIN;
2562 case LC_ID_CONVERT: {
2565 s[0] = obj->oo_inode->i_uid;
2566 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2567 s[2] = obj->oo_inode->i_gid;
2568 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2569 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2571 RETURN(ERR_PTR(rc));
2573 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2574 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2575 capa->lc_flags = LC_ID_CONVERT;
2579 RETURN(ERR_PTR(-EINVAL));
2582 capa->lc_fid = *fid;
2584 capa->lc_flags |= dev->od_capa_alg << 24;
2585 capa->lc_timeout = dev->od_capa_timeout;
2586 capa->lc_expiry = 0;
2588 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2590 LASSERT(!capa_is_expired(oc));
2594 cfs_spin_lock(&capa_lock);
2595 *key = dev->od_capa_keys[1];
2596 cfs_spin_unlock(&capa_lock);
2598 capa->lc_keyid = key->lk_keyid;
2599 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2601 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2603 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2604 RETURN(ERR_PTR(rc));
2607 oc = capa_add(dev->od_capa_hash, capa);
2611 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2613 struct osd_object *obj = osd_dt_obj(dt);
2614 struct inode *inode = obj->oo_inode;
2615 struct osd_thread_info *info = osd_oti_get(env);
2616 struct dentry *dentry = &info->oti_obj_dentry;
2617 struct file *file = &info->oti_file;
2622 dentry->d_inode = inode;
2623 file->f_dentry = dentry;
2624 file->f_mapping = inode->i_mapping;
2625 file->f_op = inode->i_fop;
2626 LOCK_INODE_MUTEX(inode);
2627 rc = file->f_op->fsync(file, dentry, 0);
2628 UNLOCK_INODE_MUTEX(inode);
2632 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2635 struct osd_object *obj = osd_dt_obj(dt);
2638 *data = (void *)obj->oo_inode;
2646 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2647 const struct dt_index_features *feat)
2649 struct iam_descr *descr;
2651 if (osd_object_is_root(o))
2652 return feat == &dt_directory_features;
2654 LASSERT(o->oo_dir != NULL);
2656 descr = o->oo_dir->od_container.ic_descr;
2657 if (feat == &dt_directory_features) {
2658 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2664 feat->dif_keysize_min <= descr->id_key_size &&
2665 descr->id_key_size <= feat->dif_keysize_max &&
2666 feat->dif_recsize_min <= descr->id_rec_size &&
2667 descr->id_rec_size <= feat->dif_recsize_max &&
2668 !(feat->dif_flags & (DT_IND_VARKEY |
2669 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2670 ergo(feat->dif_flags & DT_IND_UPDATE,
2671 1 /* XXX check that object (and file system) is
2676 static int osd_iam_container_init(const struct lu_env *env,
2677 struct osd_object *obj,
2678 struct osd_directory *dir)
2680 struct iam_container *bag = &dir->od_container;
2683 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2687 result = iam_container_setup(bag);
2691 if (osd_obj2dev(obj)->od_iop_mode) {
2692 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2694 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2700 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2702 iam_container_fini(bag);
2709 * Concurrency: no external locking is necessary.
2711 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2712 const struct dt_index_features *feat)
2716 struct osd_object *obj = osd_dt_obj(dt);
2717 struct osd_device *osd = osd_obj2dev(obj);
2719 LINVRNT(osd_invariant(obj));
2720 LASSERT(dt_object_exists(dt));
2722 if (osd_object_is_root(obj)) {
2723 dt->do_index_ops = &osd_index_ea_ops;
2725 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2726 dt->do_index_ops = &osd_index_ea_ops;
2727 if (S_ISDIR(obj->oo_inode->i_mode))
2732 } else if (unlikely(feat == &dt_otable_features)) {
2733 dt->do_index_ops = &osd_otable_ops;
2735 } else if (feat == &dt_acct_features) {
2736 dt->do_index_ops = &osd_acct_index_ops;
2739 } else if (!osd_has_index(obj)) {
2740 struct osd_directory *dir;
2745 cfs_spin_lock(&obj->oo_guard);
2746 if (obj->oo_dir == NULL)
2750 * Concurrent thread allocated container data.
2753 cfs_spin_unlock(&obj->oo_guard);
2755 * Now, that we have container data, serialize its
2758 cfs_down_write(&obj->oo_ext_idx_sem);
2760 * recheck under lock.
2762 if (!osd_has_index(obj))
2763 result = osd_iam_container_init(env, obj, dir);
2766 cfs_up_write(&obj->oo_ext_idx_sem);
2774 if (result == 0 && skip_iam == 0) {
2775 if (!osd_iam_index_probe(env, obj, feat))
2778 LINVRNT(osd_invariant(obj));
2783 static int osd_otable_it_attr_get(const struct lu_env *env,
2784 struct dt_object *dt,
2785 struct lu_attr *attr,
2786 struct lustre_capa *capa)
2792 static const struct dt_object_operations osd_obj_ops = {
2793 .do_read_lock = osd_object_read_lock,
2794 .do_write_lock = osd_object_write_lock,
2795 .do_read_unlock = osd_object_read_unlock,
2796 .do_write_unlock = osd_object_write_unlock,
2797 .do_write_locked = osd_object_write_locked,
2798 .do_attr_get = osd_attr_get,
2799 .do_declare_attr_set = osd_declare_attr_set,
2800 .do_attr_set = osd_attr_set,
2801 .do_ah_init = osd_ah_init,
2802 .do_declare_create = osd_declare_object_create,
2803 .do_create = osd_object_create,
2804 .do_declare_destroy = osd_declare_object_destroy,
2805 .do_destroy = osd_object_destroy,
2806 .do_index_try = osd_index_try,
2807 .do_declare_ref_add = osd_declare_object_ref_add,
2808 .do_ref_add = osd_object_ref_add,
2809 .do_declare_ref_del = osd_declare_object_ref_del,
2810 .do_ref_del = osd_object_ref_del,
2811 .do_xattr_get = osd_xattr_get,
2812 .do_declare_xattr_set = osd_declare_xattr_set,
2813 .do_xattr_set = osd_xattr_set,
2814 .do_declare_xattr_del = osd_declare_xattr_del,
2815 .do_xattr_del = osd_xattr_del,
2816 .do_xattr_list = osd_xattr_list,
2817 .do_capa_get = osd_capa_get,
2818 .do_object_sync = osd_object_sync,
2819 .do_data_get = osd_data_get,
2823 * dt_object_operations for interoperability mode
2824 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2826 static const struct dt_object_operations osd_obj_ea_ops = {
2827 .do_read_lock = osd_object_read_lock,
2828 .do_write_lock = osd_object_write_lock,
2829 .do_read_unlock = osd_object_read_unlock,
2830 .do_write_unlock = osd_object_write_unlock,
2831 .do_write_locked = osd_object_write_locked,
2832 .do_attr_get = osd_attr_get,
2833 .do_declare_attr_set = osd_declare_attr_set,
2834 .do_attr_set = osd_attr_set,
2835 .do_ah_init = osd_ah_init,
2836 .do_declare_create = osd_declare_object_create,
2837 .do_create = osd_object_ea_create,
2838 .do_declare_destroy = osd_declare_object_destroy,
2839 .do_destroy = osd_object_destroy,
2840 .do_index_try = osd_index_try,
2841 .do_declare_ref_add = osd_declare_object_ref_add,
2842 .do_ref_add = osd_object_ref_add,
2843 .do_declare_ref_del = osd_declare_object_ref_del,
2844 .do_ref_del = osd_object_ref_del,
2845 .do_xattr_get = osd_xattr_get,
2846 .do_declare_xattr_set = osd_declare_xattr_set,
2847 .do_xattr_set = osd_xattr_set,
2848 .do_declare_xattr_del = osd_declare_xattr_del,
2849 .do_xattr_del = osd_xattr_del,
2850 .do_xattr_list = osd_xattr_list,
2851 .do_capa_get = osd_capa_get,
2852 .do_object_sync = osd_object_sync,
2853 .do_data_get = osd_data_get,
2856 static const struct dt_object_operations osd_obj_otable_it_ops = {
2857 .do_attr_get = osd_otable_it_attr_get,
2858 .do_index_try = osd_index_try,
2861 static int osd_index_declare_iam_delete(const struct lu_env *env,
2862 struct dt_object *dt,
2863 const struct dt_key *key,
2864 struct thandle *handle)
2866 struct osd_thandle *oh;
2868 oh = container_of0(handle, struct osd_thandle, ot_super);
2869 LASSERT(oh->ot_handle == NULL);
2871 OSD_DECLARE_OP(oh, delete);
2872 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2878 * delete a (key, value) pair from index \a dt specified by \a key
2880 * \param dt osd index object
2881 * \param key key for index
2882 * \param rec record reference
2883 * \param handle transaction handler
2886 * \retval -ve failure
2889 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2890 const struct dt_key *key,
2891 struct thandle *handle,
2892 struct lustre_capa *capa)
2894 struct osd_object *obj = osd_dt_obj(dt);
2895 struct osd_thandle *oh;
2896 struct iam_path_descr *ipd;
2897 struct iam_container *bag = &obj->oo_dir->od_container;
2902 LINVRNT(osd_invariant(obj));
2903 LASSERT(dt_object_exists(dt));
2904 LASSERT(bag->ic_object == obj->oo_inode);
2905 LASSERT(handle != NULL);
2907 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2910 OSD_EXEC_OP(handle, delete);
2912 ipd = osd_idx_ipd_get(env, bag);
2913 if (unlikely(ipd == NULL))
2916 oh = container_of0(handle, struct osd_thandle, ot_super);
2917 LASSERT(oh->ot_handle != NULL);
2918 LASSERT(oh->ot_handle->h_transaction != NULL);
2920 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2921 osd_ipd_put(env, bag, ipd);
2922 LINVRNT(osd_invariant(obj));
2926 static int osd_index_declare_ea_delete(const struct lu_env *env,
2927 struct dt_object *dt,
2928 const struct dt_key *key,
2929 struct thandle *handle)
2931 struct osd_thandle *oh;
2933 LASSERT(dt_object_exists(dt));
2934 LASSERT(handle != NULL);
2936 oh = container_of0(handle, struct osd_thandle, ot_super);
2937 LASSERT(oh->ot_handle == NULL);
2939 OSD_DECLARE_OP(oh, delete);
2940 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2942 LASSERT(osd_dt_obj(dt)->oo_inode);
2943 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2944 osd_dt_obj(dt)->oo_inode);
2945 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2946 osd_dt_obj(dt)->oo_inode);
2951 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2954 struct osd_fid_pack *rec;
2957 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2958 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2959 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2965 * Index delete function for interoperability mode (b11826).
2966 * It will remove the directory entry added by osd_index_ea_insert().
2967 * This entry is needed to maintain name->fid mapping.
2969 * \param key, key i.e. file entry to be deleted
2971 * \retval 0, on success
2972 * \retval -ve, on error
2974 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2975 const struct dt_key *key,
2976 struct thandle *handle,
2977 struct lustre_capa *capa)
2979 struct osd_object *obj = osd_dt_obj(dt);
2980 struct inode *dir = obj->oo_inode;
2981 struct dentry *dentry;
2982 struct osd_thandle *oh;
2983 struct ldiskfs_dir_entry_2 *de;
2984 struct buffer_head *bh;
2985 struct htree_lock *hlock = NULL;
2990 LINVRNT(osd_invariant(obj));
2991 LASSERT(dt_object_exists(dt));
2992 LASSERT(handle != NULL);
2994 OSD_EXEC_OP(handle, delete);
2996 oh = container_of(handle, struct osd_thandle, ot_super);
2997 LASSERT(oh->ot_handle != NULL);
2998 LASSERT(oh->ot_handle->h_transaction != NULL);
3000 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3003 dentry = osd_child_dentry_get(env, obj,
3004 (char *)key, strlen((char *)key));
3006 if (obj->oo_hl_head != NULL) {
3007 hlock = osd_oti_get(env)->oti_hlock;
3008 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3009 dir, LDISKFS_HLOCK_DEL);
3011 cfs_down_write(&obj->oo_ext_idx_sem);
3014 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3016 rc = ldiskfs_delete_entry(oh->ot_handle,
3023 ldiskfs_htree_unlock(hlock);
3025 cfs_up_write(&obj->oo_ext_idx_sem);
3027 LASSERT(osd_invariant(obj));
3032 * Lookup index for \a key and copy record to \a rec.
3034 * \param dt osd index object
3035 * \param key key for index
3036 * \param rec record reference
3038 * \retval +ve success : exact mach
3039 * \retval 0 return record with key not greater than \a key
3040 * \retval -ve failure
3042 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3043 struct dt_rec *rec, const struct dt_key *key,
3044 struct lustre_capa *capa)
3046 struct osd_object *obj = osd_dt_obj(dt);
3047 struct iam_path_descr *ipd;
3048 struct iam_container *bag = &obj->oo_dir->od_container;
3049 struct osd_thread_info *oti = osd_oti_get(env);
3050 struct iam_iterator *it = &oti->oti_idx_it;
3051 struct iam_rec *iam_rec;
3056 LASSERT(osd_invariant(obj));
3057 LASSERT(dt_object_exists(dt));
3058 LASSERT(bag->ic_object == obj->oo_inode);
3060 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3063 ipd = osd_idx_ipd_get(env, bag);
3067 /* got ipd now we can start iterator. */
3068 iam_it_init(it, bag, 0, ipd);
3070 rc = iam_it_get(it, (struct iam_key *)key);
3072 if (S_ISDIR(obj->oo_inode->i_mode))
3073 iam_rec = (struct iam_rec *)oti->oti_ldp;
3075 iam_rec = (struct iam_rec *) rec;
3077 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3078 if (S_ISDIR(obj->oo_inode->i_mode))
3079 osd_fid_unpack((struct lu_fid *) rec,
3080 (struct osd_fid_pack *)iam_rec);
3084 osd_ipd_put(env, bag, ipd);
3086 LINVRNT(osd_invariant(obj));
3091 static int osd_index_declare_iam_insert(const struct lu_env *env,
3092 struct dt_object *dt,
3093 const struct dt_rec *rec,
3094 const struct dt_key *key,
3095 struct thandle *handle)
3097 struct osd_thandle *oh;
3099 LASSERT(dt_object_exists(dt));
3100 LASSERT(handle != NULL);
3102 oh = container_of0(handle, struct osd_thandle, ot_super);
3103 LASSERT(oh->ot_handle == NULL);
3105 OSD_DECLARE_OP(oh, insert);
3106 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3112 * Inserts (key, value) pair in \a dt index object.
3114 * \param dt osd index object
3115 * \param key key for index
3116 * \param rec record reference
3117 * \param th transaction handler
3120 * \retval -ve failure
3122 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3123 const struct dt_rec *rec,
3124 const struct dt_key *key, struct thandle *th,
3125 struct lustre_capa *capa, int ignore_quota)
3127 struct osd_object *obj = osd_dt_obj(dt);
3128 struct iam_path_descr *ipd;
3129 struct osd_thandle *oh;
3130 struct iam_container *bag = &obj->oo_dir->od_container;
3131 #ifdef HAVE_QUOTA_SUPPORT
3132 cfs_cap_t save = cfs_curproc_cap_pack();
3134 struct osd_thread_info *oti = osd_oti_get(env);
3135 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3140 LINVRNT(osd_invariant(obj));
3141 LASSERT(dt_object_exists(dt));
3142 LASSERT(bag->ic_object == obj->oo_inode);
3143 LASSERT(th != NULL);
3145 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3148 OSD_EXEC_OP(th, insert);
3150 ipd = osd_idx_ipd_get(env, bag);
3151 if (unlikely(ipd == NULL))
3154 oh = container_of0(th, struct osd_thandle, ot_super);
3155 LASSERT(oh->ot_handle != NULL);
3156 LASSERT(oh->ot_handle->h_transaction != NULL);
3157 #ifdef HAVE_QUOTA_SUPPORT
3159 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3161 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3163 if (S_ISDIR(obj->oo_inode->i_mode))
3164 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3166 iam_rec = (struct iam_rec *) rec;
3167 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3169 #ifdef HAVE_QUOTA_SUPPORT
3170 cfs_curproc_cap_unpack(save);
3172 osd_ipd_put(env, bag, ipd);
3173 LINVRNT(osd_invariant(obj));
3178 * Calls ldiskfs_add_entry() to add directory entry
3179 * into the directory. This is required for
3180 * interoperability mode (b11826)
3182 * \retval 0, on success
3183 * \retval -ve, on error
3185 static int __osd_ea_add_rec(struct osd_thread_info *info,
3186 struct osd_object *pobj, struct inode *cinode,
3187 const char *name, const struct dt_rec *fid,
3188 struct htree_lock *hlock, struct thandle *th)
3190 struct ldiskfs_dentry_param *ldp;
3191 struct dentry *child;
3192 struct osd_thandle *oth;
3195 oth = container_of(th, struct osd_thandle, ot_super);
3196 LASSERT(oth->ot_handle != NULL);
3197 LASSERT(oth->ot_handle->h_transaction != NULL);
3199 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3201 /* XXX: remove fid_is_igif() check here.
3202 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3203 * it is IGIF now but needs FID in dir entry as well for readdir
3205 * LU-838 should fix that and remove fid_is_igif() check */
3206 if (fid_is_igif((struct lu_fid *)fid) ||
3207 fid_is_norm((struct lu_fid *)fid)) {
3208 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3209 osd_get_ldiskfs_dirent_param(ldp, fid);
3210 child->d_fsdata = (void *)ldp;
3212 child->d_fsdata = NULL;
3214 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3220 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3221 * into the directory.Also sets flags into osd object to
3222 * indicate dot and dotdot are created. This is required for
3223 * interoperability mode (b11826)
3225 * \param dir directory for dot and dotdot fixup.
3226 * \param obj child object for linking
3228 * \retval 0, on success
3229 * \retval -ve, on error
3231 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3232 struct osd_object *dir,
3233 struct inode *parent_dir, const char *name,
3234 const struct dt_rec *dot_fid,
3235 const struct dt_rec *dot_dot_fid,
3238 struct inode *inode = dir->oo_inode;
3239 struct ldiskfs_dentry_param *dot_ldp;
3240 struct ldiskfs_dentry_param *dot_dot_ldp;
3241 struct osd_thandle *oth;
3244 oth = container_of(th, struct osd_thandle, ot_super);
3245 LASSERT(oth->ot_handle->h_transaction != NULL);
3246 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3248 if (strcmp(name, dot) == 0) {
3249 if (dir->oo_compat_dot_created) {
3252 LASSERT(inode == parent_dir);
3253 dir->oo_compat_dot_created = 1;
3256 } else if(strcmp(name, dotdot) == 0) {
3257 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3258 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3260 if (!dir->oo_compat_dot_created)
3262 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3263 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3264 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3269 /* in case of rename, dotdot is already created */
3270 if (dir->oo_compat_dotdot_created) {
3271 return __osd_ea_add_rec(info, dir, parent_dir, name,
3272 dot_dot_fid, NULL, th);
3275 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3276 inode, dot_ldp, dot_dot_ldp);
3278 dir->oo_compat_dotdot_created = 1;
3286 * It will call the appropriate osd_add* function and return the
3287 * value, return by respective functions.
3289 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3290 struct inode *cinode, const char *name,
3291 const struct dt_rec *fid, struct thandle *th)
3293 struct osd_thread_info *info = osd_oti_get(env);
3294 struct htree_lock *hlock;
3297 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3299 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3301 if (hlock != NULL) {
3302 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3305 cfs_down_write(&pobj->oo_ext_idx_sem);
3307 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3308 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3311 if (hlock != NULL) {
3312 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3313 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3315 cfs_down_write(&pobj->oo_ext_idx_sem);
3318 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3322 ldiskfs_htree_unlock(hlock);
3324 cfs_up_write(&pobj->oo_ext_idx_sem);
3330 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3331 struct osd_idmap_cache *oic)
3333 struct osd_scrub *scrub = &dev->od_scrub;
3334 struct lu_fid *fid = &oic->oic_fid;
3335 struct osd_inode_id *id = &oti->oti_id;
3341 rc = osd_oi_lookup(oti, dev, fid, id);
3342 if (rc != 0 && rc != -ENOENT)
3345 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3348 if (thread_is_running(&scrub->os_thread)) {
3349 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3350 /* There is race condition between osd_oi_lookup and OI scrub.
3351 * The OI scrub finished just after osd_oi_lookup() failure.
3352 * Under such case, it is unnecessary to trigger OI scrub again,
3353 * but try to call osd_oi_lookup() again. */
3354 if (unlikely(rc == -EAGAIN))
3360 if (!scrub->os_no_scrub && ++once == 1) {
3361 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3363 rc = osd_scrub_start(dev);
3364 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3366 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3372 RETURN(rc = -EREMCHG);
3376 * Calls ->lookup() to find dentry. From dentry get inode and
3377 * read inode's ea to get fid. This is required for interoperability
3380 * \retval 0, on success
3381 * \retval -ve, on error
3383 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3384 struct dt_rec *rec, const struct dt_key *key)
3386 struct inode *dir = obj->oo_inode;
3387 struct dentry *dentry;
3388 struct ldiskfs_dir_entry_2 *de;
3389 struct buffer_head *bh;
3390 struct lu_fid *fid = (struct lu_fid *) rec;
3391 struct htree_lock *hlock = NULL;
3395 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3397 dentry = osd_child_dentry_get(env, obj,
3398 (char *)key, strlen((char *)key));
3400 if (obj->oo_hl_head != NULL) {
3401 hlock = osd_oti_get(env)->oti_hlock;
3402 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3403 dir, LDISKFS_HLOCK_LOOKUP);
3405 cfs_down_read(&obj->oo_ext_idx_sem);
3408 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3410 struct osd_thread_info *oti = osd_oti_get(env);
3411 struct osd_idmap_cache *oic = &oti->oti_cache;
3412 struct osd_device *dev = osd_obj2dev(obj);
3413 struct osd_scrub *scrub = &dev->od_scrub;
3414 struct scrub_file *sf = &scrub->os_file;
3416 ino = le32_to_cpu(de->inode);
3417 rc = osd_get_fid_from_dentry(de, rec);
3419 /* done with de, release bh */
3422 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3424 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3426 if (rc != 0 || !fid_is_norm(fid))
3429 oic->oic_fid = *fid;
3430 if ((scrub->os_pos_current <= ino) &&
3431 (sf->sf_flags & SF_INCONSISTENT ||
3432 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3434 rc = osd_consistency_check(oti, dev, oic);
3443 ldiskfs_htree_unlock(hlock);
3445 cfs_up_read(&obj->oo_ext_idx_sem);
3450 * Find the osd object for given fid.
3452 * \param fid need to find the osd object having this fid
3454 * \retval osd_object on success
3455 * \retval -ve on error
3457 struct osd_object *osd_object_find(const struct lu_env *env,
3458 struct dt_object *dt,
3459 const struct lu_fid *fid)
3461 struct lu_device *ludev = dt->do_lu.lo_dev;
3462 struct osd_object *child = NULL;
3463 struct lu_object *luch;
3464 struct lu_object *lo;
3466 luch = lu_object_find(env, ludev, fid, NULL);
3467 if (!IS_ERR(luch)) {
3468 if (lu_object_exists(luch)) {
3469 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3471 child = osd_obj(lo);
3473 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3474 "lu_object can't be located"
3475 DFID"\n", PFID(fid));
3477 if (child == NULL) {
3478 lu_object_put(env, luch);
3479 CERROR("Unable to get osd_object\n");
3480 child = ERR_PTR(-ENOENT);
3483 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3484 "lu_object does not exists "DFID"\n",
3486 lu_object_put(env, luch);
3487 child = ERR_PTR(-ENOENT);
3490 child = (void *)luch;
3496 * Put the osd object once done with it.
3498 * \param obj osd object that needs to be put
3500 static inline void osd_object_put(const struct lu_env *env,
3501 struct osd_object *obj)
3503 lu_object_put(env, &obj->oo_dt.do_lu);
3506 static int osd_index_declare_ea_insert(const struct lu_env *env,
3507 struct dt_object *dt,
3508 const struct dt_rec *rec,
3509 const struct dt_key *key,
3510 struct thandle *handle)
3512 struct osd_thandle *oh;
3514 LASSERT(dt_object_exists(dt));
3515 LASSERT(handle != NULL);
3517 oh = container_of0(handle, struct osd_thandle, ot_super);
3518 LASSERT(oh->ot_handle == NULL);
3520 OSD_DECLARE_OP(oh, insert);
3521 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3523 LASSERT(osd_dt_obj(dt)->oo_inode);
3524 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3525 osd_dt_obj(dt)->oo_inode);
3526 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3527 osd_dt_obj(dt)->oo_inode);
3533 * Index add function for interoperability mode (b11826).
3534 * It will add the directory entry.This entry is needed to
3535 * maintain name->fid mapping.
3537 * \param key it is key i.e. file entry to be inserted
3538 * \param rec it is value of given key i.e. fid
3540 * \retval 0, on success
3541 * \retval -ve, on error
3543 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3544 const struct dt_rec *rec,
3545 const struct dt_key *key, struct thandle *th,
3546 struct lustre_capa *capa, int ignore_quota)
3548 struct osd_object *obj = osd_dt_obj(dt);
3549 struct lu_fid *fid = (struct lu_fid *) rec;
3550 const char *name = (const char *)key;
3551 struct osd_object *child;
3552 #ifdef HAVE_QUOTA_SUPPORT
3553 cfs_cap_t save = cfs_curproc_cap_pack();
3559 LASSERT(osd_invariant(obj));
3560 LASSERT(dt_object_exists(dt));
3561 LASSERT(th != NULL);
3563 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3566 child = osd_object_find(env, dt, fid);
3567 if (!IS_ERR(child)) {
3568 #ifdef HAVE_QUOTA_SUPPORT
3570 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3572 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3574 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3575 #ifdef HAVE_QUOTA_SUPPORT
3576 cfs_curproc_cap_unpack(save);
3578 osd_object_put(env, child);
3580 rc = PTR_ERR(child);
3583 LASSERT(osd_invariant(obj));
3588 * Initialize osd Iterator for given osd index object.
3590 * \param dt osd index object
3593 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3594 struct dt_object *dt,
3596 struct lustre_capa *capa)
3598 struct osd_it_iam *it;
3599 struct osd_thread_info *oti = osd_oti_get(env);
3600 struct osd_object *obj = osd_dt_obj(dt);
3601 struct lu_object *lo = &dt->do_lu;
3602 struct iam_path_descr *ipd;
3603 struct iam_container *bag = &obj->oo_dir->od_container;
3605 LASSERT(lu_object_exists(lo));
3607 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3608 return ERR_PTR(-EACCES);
3611 ipd = osd_it_ipd_get(env, bag);
3612 if (likely(ipd != NULL)) {
3616 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3617 return (struct dt_it *)it;
3619 return ERR_PTR(-ENOMEM);
3623 * free given Iterator.
3626 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3628 struct osd_it_iam *it = (struct osd_it_iam *)di;
3629 struct osd_object *obj = it->oi_obj;
3631 iam_it_fini(&it->oi_it);
3632 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3633 lu_object_put(env, &obj->oo_dt.do_lu);
3637 * Move Iterator to record specified by \a key
3639 * \param di osd iterator
3640 * \param key key for index
3642 * \retval +ve di points to record with least key not larger than key
3643 * \retval 0 di points to exact matched key
3644 * \retval -ve failure
3647 static int osd_it_iam_get(const struct lu_env *env,
3648 struct dt_it *di, const struct dt_key *key)
3650 struct osd_it_iam *it = (struct osd_it_iam *)di;
3652 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3658 * \param di osd iterator
3661 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3663 struct osd_it_iam *it = (struct osd_it_iam *)di;
3665 iam_it_put(&it->oi_it);
3669 * Move iterator by one record
3671 * \param di osd iterator
3673 * \retval +1 end of container reached
3675 * \retval -ve failure
3678 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3680 struct osd_it_iam *it = (struct osd_it_iam *)di;
3682 return iam_it_next(&it->oi_it);
3686 * Return pointer to the key under iterator.
3689 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3690 const struct dt_it *di)
3692 struct osd_it_iam *it = (struct osd_it_iam *)di;
3694 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3698 * Return size of key under iterator (in bytes)
3701 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3703 struct osd_it_iam *it = (struct osd_it_iam *)di;
3705 return iam_it_key_size(&it->oi_it);
3708 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3709 int len, __u16 type)
3711 struct luda_type *lt;
3712 const unsigned align = sizeof(struct luda_type) - 1;
3714 /* check if file type is required */
3715 if (attr & LUDA_TYPE) {
3716 len = (len + align) & ~align;
3718 lt = (void *) ent->lde_name + len;
3719 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3720 ent->lde_attrs |= LUDA_TYPE;
3723 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3727 * build lu direct from backend fs dirent.
3730 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3731 struct lu_fid *fid, __u64 offset,
3732 char *name, __u16 namelen,
3733 __u16 type, __u32 attr)
3735 fid_cpu_to_le(&ent->lde_fid, fid);
3736 ent->lde_attrs = LUDA_FID;
3738 ent->lde_hash = cpu_to_le64(offset);
3739 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3741 strncpy(ent->lde_name, name, namelen);
3742 ent->lde_namelen = cpu_to_le16(namelen);
3744 /* append lustre attributes */
3745 osd_it_append_attrs(ent, attr, namelen, type);
3749 * Return pointer to the record under iterator.
3751 static int osd_it_iam_rec(const struct lu_env *env,
3752 const struct dt_it *di,
3753 struct dt_rec *dtrec, __u32 attr)
3755 struct osd_it_iam *it = (struct osd_it_iam *)di;
3756 struct osd_thread_info *info = osd_oti_get(env);
3759 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3760 const struct osd_fid_pack *rec;
3761 struct lu_fid *fid = &info->oti_fid;
3762 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3768 name = (char *)iam_it_key_get(&it->oi_it);
3770 RETURN(PTR_ERR(name));
3772 namelen = iam_it_key_size(&it->oi_it);
3774 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3776 RETURN(PTR_ERR(rec));
3778 rc = osd_fid_unpack(fid, rec);
3782 hash = iam_it_store(&it->oi_it);
3784 /* IAM does not store object type in IAM index (dir) */
3785 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3788 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3789 (struct iam_rec *)dtrec);
3796 * Returns cookie for current Iterator position.
3798 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3800 struct osd_it_iam *it = (struct osd_it_iam *)di;
3802 return iam_it_store(&it->oi_it);
3806 * Restore iterator from cookie.
3808 * \param di osd iterator
3809 * \param hash Iterator location cookie
3811 * \retval +ve di points to record with least key not larger than key.
3812 * \retval 0 di points to exact matched key
3813 * \retval -ve failure
3816 static int osd_it_iam_load(const struct lu_env *env,
3817 const struct dt_it *di, __u64 hash)
3819 struct osd_it_iam *it = (struct osd_it_iam *)di;
3821 return iam_it_load(&it->oi_it, hash);
3824 static const struct dt_index_operations osd_index_iam_ops = {
3825 .dio_lookup = osd_index_iam_lookup,
3826 .dio_declare_insert = osd_index_declare_iam_insert,
3827 .dio_insert = osd_index_iam_insert,
3828 .dio_declare_delete = osd_index_declare_iam_delete,
3829 .dio_delete = osd_index_iam_delete,
3831 .init = osd_it_iam_init,
3832 .fini = osd_it_iam_fini,
3833 .get = osd_it_iam_get,
3834 .put = osd_it_iam_put,
3835 .next = osd_it_iam_next,
3836 .key = osd_it_iam_key,
3837 .key_size = osd_it_iam_key_size,
3838 .rec = osd_it_iam_rec,
3839 .store = osd_it_iam_store,
3840 .load = osd_it_iam_load
3845 * Creates or initializes iterator context.
3847 * \retval struct osd_it_ea, iterator structure on success
3850 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3851 struct dt_object *dt,
3853 struct lustre_capa *capa)
3855 struct osd_object *obj = osd_dt_obj(dt);
3856 struct osd_thread_info *info = osd_oti_get(env);
3857 struct osd_it_ea *it = &info->oti_it_ea;
3858 struct lu_object *lo = &dt->do_lu;
3859 struct dentry *obj_dentry = &info->oti_it_dentry;
3861 LASSERT(lu_object_exists(lo));
3863 obj_dentry->d_inode = obj->oo_inode;
3864 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3865 obj_dentry->d_name.hash = 0;
3867 it->oie_rd_dirent = 0;
3868 it->oie_it_dirent = 0;
3869 it->oie_dirent = NULL;
3870 it->oie_buf = info->oti_it_ea_buf;
3872 it->oie_file.f_pos = 0;
3873 it->oie_file.f_dentry = obj_dentry;
3874 if (attr & LUDA_64BITHASH)
3875 it->oie_file.f_flags = O_64BITHASH;
3877 it->oie_file.f_flags = O_32BITHASH;
3878 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3879 it->oie_file.f_op = obj->oo_inode->i_fop;
3880 it->oie_file.private_data = NULL;
3882 RETURN((struct dt_it *) it);
3886 * Destroy or finishes iterator context.
3888 * \param di iterator structure to be destroyed
3890 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3892 struct osd_it_ea *it = (struct osd_it_ea *)di;
3893 struct osd_object *obj = it->oie_obj;
3894 struct inode *inode = obj->oo_inode;
3897 it->oie_file.f_op->release(inode, &it->oie_file);
3898 lu_object_put(env, &obj->oo_dt.do_lu);
3903 * It position the iterator at given key, so that next lookup continues from
3904 * that key Or it is similar to dio_it->load() but based on a key,
3905 * rather than file position.
3907 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3910 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3912 static int osd_it_ea_get(const struct lu_env *env,
3913 struct dt_it *di, const struct dt_key *key)
3915 struct osd_it_ea *it = (struct osd_it_ea *)di;
3918 LASSERT(((const char *)key)[0] == '\0');
3919 it->oie_file.f_pos = 0;
3920 it->oie_rd_dirent = 0;
3921 it->oie_it_dirent = 0;
3922 it->oie_dirent = NULL;
3930 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3935 * It is called internally by ->readdir(). It fills the
3936 * iterator's in-memory data structure with required
3937 * information i.e. name, namelen, rec_size etc.
3939 * \param buf in which information to be filled in.
3940 * \param name name of the file in given dir
3942 * \retval 0 on success
3943 * \retval 1 on buffer full
3945 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3946 loff_t offset, __u64 ino,
3949 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3950 struct osd_it_ea_dirent *ent = it->oie_dirent;
3951 struct lu_fid *fid = &ent->oied_fid;
3952 struct osd_fid_pack *rec;
3955 /* this should never happen */
3956 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3957 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3961 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3965 if (d_type & LDISKFS_DIRENT_LUFID) {
3966 rec = (struct osd_fid_pack*) (name + namelen + 1);
3968 if (osd_fid_unpack(fid, rec) != 0)
3971 d_type &= ~LDISKFS_DIRENT_LUFID;
3976 ent->oied_ino = ino;
3977 ent->oied_off = offset;
3978 ent->oied_namelen = namelen;
3979 ent->oied_type = d_type;
3981 memcpy(ent->oied_name, name, namelen);
3983 it->oie_rd_dirent++;
3984 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3989 * Calls ->readdir() to load a directory entry at a time
3990 * and stored it in iterator's in-memory data structure.
3992 * \param di iterator's in memory structure
3994 * \retval 0 on success
3995 * \retval -ve on error
3997 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3998 const struct dt_it *di)
4000 struct osd_it_ea *it = (struct osd_it_ea *)di;
4001 struct osd_object *obj = it->oie_obj;
4002 struct inode *inode = obj->oo_inode;
4003 struct htree_lock *hlock = NULL;
4007 it->oie_dirent = it->oie_buf;
4008 it->oie_rd_dirent = 0;
4010 if (obj->oo_hl_head != NULL) {
4011 hlock = osd_oti_get(env)->oti_hlock;
4012 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4013 inode, LDISKFS_HLOCK_READDIR);
4015 cfs_down_read(&obj->oo_ext_idx_sem);
4018 result = inode->i_fop->readdir(&it->oie_file, it,
4019 (filldir_t) osd_ldiskfs_filldir);
4022 ldiskfs_htree_unlock(hlock);
4024 cfs_up_read(&obj->oo_ext_idx_sem);
4026 if (it->oie_rd_dirent == 0) {
4029 it->oie_dirent = it->oie_buf;
4030 it->oie_it_dirent = 1;
4037 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4038 * to load a directory entry at a time and stored it in
4039 * iterator's in-memory data structure.
4041 * \param di iterator's in memory structure
4043 * \retval +ve iterator reached to end
4044 * \retval 0 iterator not reached to end
4045 * \retval -ve on error
4047 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4049 struct osd_it_ea *it = (struct osd_it_ea *)di;
4054 if (it->oie_it_dirent < it->oie_rd_dirent) {
4056 (void *) it->oie_dirent +
4057 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4058 it->oie_dirent->oied_namelen);
4059 it->oie_it_dirent++;
4062 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
4065 rc = osd_ldiskfs_it_fill(env, di);
4072 * Returns the key at current position from iterator's in memory structure.
4074 * \param di iterator's in memory structure
4076 * \retval key i.e. struct dt_key on success
4078 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4079 const struct dt_it *di)
4081 struct osd_it_ea *it = (struct osd_it_ea *)di;
4083 return (struct dt_key *)it->oie_dirent->oied_name;
4087 * Returns the key's size at current position from iterator's in memory structure.
4089 * \param di iterator's in memory structure
4091 * \retval key_size i.e. struct dt_key on success
4093 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4095 struct osd_it_ea *it = (struct osd_it_ea *)di;
4097 return it->oie_dirent->oied_namelen;
4102 * Returns the value (i.e. fid/igif) at current position from iterator's
4103 * in memory structure.
4105 * \param di struct osd_it_ea, iterator's in memory structure
4106 * \param attr attr requested for dirent.
4107 * \param lde lustre dirent
4109 * \retval 0 no error and \param lde has correct lustre dirent.
4110 * \retval -ve on error
4112 static inline int osd_it_ea_rec(const struct lu_env *env,
4113 const struct dt_it *di,
4114 struct dt_rec *dtrec, __u32 attr)
4116 struct osd_it_ea *it = (struct osd_it_ea *)di;
4117 struct osd_object *obj = it->oie_obj;
4118 struct osd_device *dev = osd_obj2dev(obj);
4119 struct osd_scrub *scrub = &dev->od_scrub;
4120 struct scrub_file *sf = &scrub->os_file;
4121 struct osd_thread_info *oti = osd_oti_get(env);
4122 struct osd_idmap_cache *oic = &oti->oti_cache;
4123 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4124 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4125 __u32 ino = it->oie_dirent->oied_ino;
4129 if (!fid_is_sane(fid)) {
4130 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4134 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4137 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4138 it->oie_dirent->oied_name,
4139 it->oie_dirent->oied_namelen,
4140 it->oie_dirent->oied_type, attr);
4142 if (!fid_is_norm(fid))
4145 oic->oic_fid = *fid;
4146 if ((scrub->os_pos_current <= ino) &&
4147 (sf->sf_flags & SF_INCONSISTENT ||
4148 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4149 rc = osd_consistency_check(oti, dev, oic);
4155 * Returns a cookie for current position of the iterator head, so that
4156 * user can use this cookie to load/start the iterator next time.
4158 * \param di iterator's in memory structure
4160 * \retval cookie for current position, on success
4162 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4164 struct osd_it_ea *it = (struct osd_it_ea *)di;
4166 return it->oie_dirent->oied_off;
4170 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4171 * to load a directory entry at a time and stored it i inn,
4172 * in iterator's in-memory data structure.
4174 * \param di struct osd_it_ea, iterator's in memory structure
4176 * \retval +ve on success
4177 * \retval -ve on error
4179 static int osd_it_ea_load(const struct lu_env *env,
4180 const struct dt_it *di, __u64 hash)
4182 struct osd_it_ea *it = (struct osd_it_ea *)di;
4186 it->oie_file.f_pos = hash;
4188 rc = osd_ldiskfs_it_fill(env, di);
4196 * Index lookup function for interoperability mode (b11826).
4198 * \param key, key i.e. file name to be searched
4200 * \retval +ve, on success
4201 * \retval -ve, on error
4203 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4204 struct dt_rec *rec, const struct dt_key *key,
4205 struct lustre_capa *capa)
4207 struct osd_object *obj = osd_dt_obj(dt);
4212 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4213 LINVRNT(osd_invariant(obj));
4215 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4218 rc = osd_ea_lookup_rec(env, obj, rec, key);
4226 * Index and Iterator operations for interoperability
4227 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4229 static const struct dt_index_operations osd_index_ea_ops = {
4230 .dio_lookup = osd_index_ea_lookup,
4231 .dio_declare_insert = osd_index_declare_ea_insert,
4232 .dio_insert = osd_index_ea_insert,
4233 .dio_declare_delete = osd_index_declare_ea_delete,
4234 .dio_delete = osd_index_ea_delete,
4236 .init = osd_it_ea_init,
4237 .fini = osd_it_ea_fini,
4238 .get = osd_it_ea_get,
4239 .put = osd_it_ea_put,
4240 .next = osd_it_ea_next,
4241 .key = osd_it_ea_key,
4242 .key_size = osd_it_ea_key_size,
4243 .rec = osd_it_ea_rec,
4244 .store = osd_it_ea_store,
4245 .load = osd_it_ea_load
4249 static void *osd_key_init(const struct lu_context *ctx,
4250 struct lu_context_key *key)
4252 struct osd_thread_info *info;
4254 OBD_ALLOC_PTR(info);
4256 return ERR_PTR(-ENOMEM);
4258 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4259 if (info->oti_it_ea_buf == NULL)
4262 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4264 info->oti_hlock = ldiskfs_htree_lock_alloc();
4265 if (info->oti_hlock == NULL)
4271 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4274 return ERR_PTR(-ENOMEM);
4277 static void osd_key_fini(const struct lu_context *ctx,
4278 struct lu_context_key *key, void* data)
4280 struct osd_thread_info *info = data;
4282 if (info->oti_hlock != NULL)
4283 ldiskfs_htree_lock_free(info->oti_hlock);
4284 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4288 static void osd_key_exit(const struct lu_context *ctx,
4289 struct lu_context_key *key, void *data)
4291 struct osd_thread_info *info = data;
4293 LASSERT(info->oti_r_locks == 0);
4294 LASSERT(info->oti_w_locks == 0);
4295 LASSERT(info->oti_txns == 0);
4298 /* type constructor/destructor: osd_type_init, osd_type_fini */
4299 LU_TYPE_INIT_FINI(osd, &osd_key);
4301 struct lu_context_key osd_key = {
4302 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4303 .lct_init = osd_key_init,
4304 .lct_fini = osd_key_fini,
4305 .lct_exit = osd_key_exit
4309 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4310 const char *name, struct lu_device *next)
4312 struct osd_device *osd = osd_dev(d);
4314 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4315 return osd_procfs_init(osd, name);
4318 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4322 osd_scrub_cleanup(env, o);
4325 fsfilt_put_ops(o->od_fsops);
4329 /* shutdown quota slave instance associated with the device */
4330 if (o->od_quota_slave != NULL) {
4331 qsd_fini(env, o->od_quota_slave);
4332 o->od_quota_slave = NULL;
4338 static int osd_mount(const struct lu_env *env,
4339 struct osd_device *o, struct lustre_cfg *cfg)
4341 struct lustre_mount_info *lmi;
4342 const char *dev = lustre_cfg_string(cfg, 0);
4343 struct lustre_disk_data *ldd;
4344 struct lustre_sb_info *lsi;
4349 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4350 if (o->od_fsops == NULL) {
4351 CERROR("Can't find fsfilt_ldiskfs\n");
4355 if (o->od_mount != NULL) {
4356 CERROR("Already mounted (%s)\n", dev);
4361 lmi = server_get_mount(dev);
4363 CERROR("Cannot get mount info for %s!\n", dev);
4367 LASSERT(lmi != NULL);
4368 /* save lustre_mount_info in dt_device */
4370 o->od_mnt = lmi->lmi_mnt;
4372 lsi = s2lsi(lmi->lmi_sb);
4375 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4377 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", dev);
4381 if (ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
4382 rc = osd_compat_init(o);
4384 CERROR("%s: can't initialize compats: %d\n", dev, rc);
4390 static struct lu_device *osd_device_fini(const struct lu_env *env,
4391 struct lu_device *d)
4396 osd_compat_fini(osd_dev(d));
4398 shrink_dcache_sb(osd_sb(osd_dev(d)));
4399 osd_sync(env, lu2dt_dev(d));
4401 rc = osd_procfs_fini(osd_dev(d));
4403 CERROR("proc fini error %d \n", rc);
4404 RETURN (ERR_PTR(rc));
4407 if (osd_dev(d)->od_mount)
4408 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4409 osd_dev(d)->od_mount->lmi_mnt);
4410 osd_dev(d)->od_mount = NULL;
4415 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4416 struct lu_device_type *t,
4417 struct lustre_cfg *cfg)
4419 struct lu_device *l;
4420 struct osd_device *o;
4426 result = dt_device_init(&o->od_dt_dev, t);
4429 l->ld_ops = &osd_lu_ops;
4430 o->od_dt_dev.dd_ops = &osd_dt_ops;
4431 cfs_spin_lock_init(&o->od_osfs_lock);
4432 cfs_mutex_init(&o->od_otable_mutex);
4433 o->od_osfs_age = cfs_time_shift_64(-1000);
4434 o->od_capa_hash = init_capa_hash();
4435 if (o->od_capa_hash == NULL) {
4436 dt_device_fini(&o->od_dt_dev);
4437 l = ERR_PTR(-ENOMEM);
4440 l = ERR_PTR(result);
4445 l = ERR_PTR(-ENOMEM);
4449 static struct lu_device *osd_device_free(const struct lu_env *env,
4450 struct lu_device *d)
4452 struct osd_device *o = osd_dev(d);
4455 cleanup_capa_hash(o->od_capa_hash);
4456 dt_device_fini(&o->od_dt_dev);
4461 static int osd_process_config(const struct lu_env *env,
4462 struct lu_device *d, struct lustre_cfg *cfg)
4464 struct osd_device *o = osd_dev(d);
4468 switch(cfg->lcfg_command) {
4470 err = osd_mount(env, o, cfg);
4473 lu_dev_del_linkage(d->ld_site, d);
4474 err = osd_shutdown(env, o);
4483 static int osd_recovery_complete(const struct lu_env *env,
4484 struct lu_device *d)
4489 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4490 struct lu_device *dev)
4492 struct osd_device *osd = osd_dev(dev);
4496 /* 1. setup scrub, including OI files initialization */
4497 result = osd_scrub_setup(env, osd);
4501 /* 2. setup quota slave instance */
4502 osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
4503 osd->od_proc_entry);
4504 if (IS_ERR(osd->od_quota_slave)) {
4505 result = PTR_ERR(osd->od_quota_slave);
4506 osd->od_quota_slave = NULL;
4510 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,3,50,0)
4511 /* Unfortunately, the current MDD implementation relies on some specific
4512 * code to be executed in the OSD layer. Since OFD now also uses the OSD
4513 * module, we need a way to skip the metadata-specific code when running
4515 * The hack here is to check the type of the parent device which is
4516 * either MD (i.e. MDD device) with the current MDT stack or DT (i.e.
4517 * OFD device) on an OST. As a reminder, obdfilter does not use the OSD
4518 * layer and still relies on lvfs. This hack won't work any more when
4519 * LOD is landed since LOD is of DT type.
4520 * This code should be removed once the orion MDT changes (LOD/OSP, ...)
4521 * have been landed */
4522 osd->od_is_md = lu_device_is_md(pdev);
4524 #warning "all is_md checks must be removed from osd-ldiskfs"
4530 /* 3. setup local objects */
4531 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4535 static const struct lu_object_operations osd_lu_obj_ops = {
4536 .loo_object_init = osd_object_init,
4537 .loo_object_delete = osd_object_delete,
4538 .loo_object_release = osd_object_release,
4539 .loo_object_free = osd_object_free,
4540 .loo_object_print = osd_object_print,
4541 .loo_object_invariant = osd_object_invariant
4544 const struct lu_device_operations osd_lu_ops = {
4545 .ldo_object_alloc = osd_object_alloc,
4546 .ldo_process_config = osd_process_config,
4547 .ldo_recovery_complete = osd_recovery_complete,
4548 .ldo_prepare = osd_prepare,
4551 static const struct lu_device_type_operations osd_device_type_ops = {
4552 .ldto_init = osd_type_init,
4553 .ldto_fini = osd_type_fini,
4555 .ldto_start = osd_type_start,
4556 .ldto_stop = osd_type_stop,
4558 .ldto_device_alloc = osd_device_alloc,
4559 .ldto_device_free = osd_device_free,
4561 .ldto_device_init = osd_device_init,
4562 .ldto_device_fini = osd_device_fini
4565 static struct lu_device_type osd_device_type = {
4566 .ldt_tags = LU_DEVICE_DT,
4567 .ldt_name = LUSTRE_OSD_NAME,
4568 .ldt_ops = &osd_device_type_ops,
4569 .ldt_ctx_tags = LCT_LOCAL,
4573 * lprocfs legacy support.
4575 static struct obd_ops osd_obd_device_ops = {
4576 .o_owner = THIS_MODULE
4579 static int __init osd_mod_init(void)
4581 struct lprocfs_static_vars lvars;
4584 lprocfs_osd_init_vars(&lvars);
4585 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4586 LUSTRE_OSD_NAME, &osd_device_type);
4589 static void __exit osd_mod_exit(void)
4591 class_unregister_type(LUSTRE_OSD_NAME);
4594 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4595 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4596 MODULE_LICENSE("GPL");
4598 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);