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_index_operations osd_index_iam_ops;
94 static const struct dt_index_operations osd_index_ea_ops;
96 static int osd_has_index(const struct osd_object *obj)
98 return obj->oo_dt.do_index_ops != NULL;
101 static int osd_object_invariant(const struct lu_object *l)
103 return osd_invariant(osd_obj(l));
106 #ifdef HAVE_QUOTA_SUPPORT
108 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
110 struct md_ucred *uc = md_ucred(env);
115 save->oc_uid = current_fsuid();
116 save->oc_gid = current_fsgid();
117 save->oc_cap = current_cap();
118 if ((tc = prepare_creds())) {
119 tc->fsuid = uc->mu_fsuid;
120 tc->fsgid = uc->mu_fsgid;
123 /* XXX not suboptimal */
124 cfs_curproc_cap_unpack(uc->mu_cap);
128 osd_pop_ctxt(struct osd_ctxt *save)
132 if ((tc = prepare_creds())) {
133 tc->fsuid = save->oc_uid;
134 tc->fsgid = save->oc_gid;
135 tc->cap_effective = save->oc_cap;
142 * Concurrency: doesn't matter
144 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
146 return osd_oti_get(env)->oti_r_locks > 0;
150 * Concurrency: doesn't matter
152 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
154 struct osd_thread_info *oti = osd_oti_get(env);
155 return oti->oti_w_locks > 0 && o->oo_owner == env;
159 * Concurrency: doesn't access mutable data
161 static int osd_root_get(const struct lu_env *env,
162 struct dt_device *dev, struct lu_fid *f)
164 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
168 static inline int osd_qid_type(struct osd_thandle *oh, int i)
170 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
173 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
175 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
178 void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
179 int type, uid_t id, struct inode *inode)
182 int i, allocated = 0;
183 struct osd_object *obj;
187 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
190 /* id entry is allocated in the quota file */
191 if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
194 for (i = 0; i < oh->ot_id_cnt; i++) {
195 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
199 if (unlikely(i >= OSD_MAX_UGID_CNT)) {
200 CERROR("more than %d uid/gids for a transaction?\n", i);
204 oh->ot_id_array[i] = id;
205 osd_qid_set_type(oh, i, type);
207 obj = osd_dt_obj(dt);
208 oh->ot_credits += (allocated || id == 0) ?
209 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
214 * OSD object methods.
218 * Concurrency: no concurrent access is possible that early in object
221 static struct lu_object *osd_object_alloc(const struct lu_env *env,
222 const struct lu_object_header *hdr,
225 struct osd_object *mo;
231 l = &mo->oo_dt.do_lu;
232 dt_object_init(&mo->oo_dt, NULL, d);
233 if (osd_dev(d)->od_iop_mode)
234 mo->oo_dt.do_ops = &osd_obj_ea_ops;
236 mo->oo_dt.do_ops = &osd_obj_ops;
238 l->lo_ops = &osd_lu_obj_ops;
239 cfs_init_rwsem(&mo->oo_sem);
240 cfs_init_rwsem(&mo->oo_ext_idx_sem);
241 cfs_spin_lock_init(&mo->oo_guard);
248 static int osd_get_lma(struct inode *inode, struct dentry *dentry,
249 struct lustre_mdt_attrs *lma)
253 dentry->d_inode = inode;
254 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
257 /* Check LMA compatibility */
258 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
259 CWARN("%.16s: unsupported incompat LMA feature(s) "
261 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
262 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
266 lustre_lma_swab(lma);
269 } else if (rc == 0) {
277 * retrieve object from backend ext fs.
279 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
280 struct osd_inode_id *id)
282 struct inode *inode = NULL;
284 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
286 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
287 id->oii_ino, PTR_ERR(inode));
288 } else if (id->oii_gen != OSD_OII_NOGEN &&
289 inode->i_generation != id->oii_gen) {
290 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
292 id->oii_ino, id->oii_gen, inode->i_generation);
294 inode = ERR_PTR(-ESTALE);
295 } else if (inode->i_nlink == 0) {
296 /* due to parallel readdir and unlink,
297 * we can have dead inode here. */
298 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
299 make_bad_inode(inode);
301 inode = ERR_PTR(-ESTALE);
302 } else if (is_bad_inode(inode)) {
303 CWARN("%.16s: bad inode: ino = %u\n",
304 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
306 inode = ERR_PTR(-ENOENT);
308 if (id->oii_gen == OSD_OII_NOGEN)
309 osd_id_gen(id, inode->i_ino, inode->i_generation);
311 /* Do not update file c/mtime in ldiskfs.
312 * NB: we don't have any lock to protect this because we don't
313 * have reference on osd_object now, but contention with
314 * another lookup + attr_set can't happen in the tiny window
315 * between if (...) and set S_NOCMTIME. */
316 if (!(inode->i_flags & S_NOCMTIME))
317 inode->i_flags |= S_NOCMTIME;
322 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
323 struct osd_inode_id *id, struct lu_fid *fid)
325 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
329 inode = osd_iget(info, dev, id);
333 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
335 *fid = lma->lma_self_fid;
336 } else if (rc == -ENODATA) {
337 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
345 static struct inode *
346 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
347 struct osd_inode_id *id, const struct lu_fid *fid)
349 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
353 inode = osd_iget(info, dev, id);
357 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
359 if (rc == -ENODATA) {
360 CDEBUG(D_LFSCK, "inconsistent obj: NULL, %lu, "DFID"\n",
361 inode->i_ino, PFID(fid));
368 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
369 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
370 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
372 return ERR_PTR(EREMCHG);
377 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
378 const struct lu_fid *fid,
379 const struct lu_object_conf *conf)
381 struct osd_thread_info *info;
382 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
383 struct osd_device *dev;
384 struct osd_idmap_cache *oic;
385 struct osd_inode_id *id;
387 struct osd_scrub *scrub;
388 struct scrub_file *sf;
393 LINVRNT(osd_invariant(obj));
394 LASSERT(obj->oo_inode == NULL);
395 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
398 scrub = &dev->od_scrub;
399 sf = &scrub->os_file;
400 info = osd_oti_get(env);
402 oic = &info->oti_cache;
405 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
408 if (fid_is_norm(fid)) {
409 /* Search order: 1. per-thread cache. */
410 if (lu_fid_eq(fid, &oic->oic_fid)) {
412 } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
413 /* Search order: 2. OI scrub pending list. */
414 result = osd_oii_lookup(dev, fid, id);
419 if (sf->sf_flags & SF_INCONSISTENT)
423 fid_zero(&oic->oic_fid);
424 /* Search order: 3. OI files. */
425 result = osd_oi_lookup(info, dev, fid, id);
426 if (result != 0 && result != -ENOENT)
429 /* If fid wasn't found in oi, inode-less object is created,
430 * for which lu_object_exists() returns false. This is used
431 * in a (frequent) case when objects are created as locking
432 * anchors or place holders for objects yet to be created. */
433 if (conf != NULL && conf->loc_flags & LOC_F_NEW) {
434 if (unlikely(result == 0))
435 GOTO(out, result = -EEXIST);
437 GOTO(out, result = 0);
440 if (result == -ENOENT) {
441 if (!fid_is_norm(fid) ||
442 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
444 GOTO(out, result = 0);
451 inode = osd_iget(info, dev, id);
453 inode = osd_iget_verify(info, dev, id, fid);
455 result = PTR_ERR(inode);
456 if (result == -ENOENT || result == -ESTALE) {
458 } else if (result == -EREMCHG) {
461 if (thread_is_running(&scrub->os_thread)) {
462 result = -EINPROGRESS;
463 } else if (!scrub->os_no_scrub) {
464 result = osd_scrub_start(dev);
465 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
466 "for "DFID", rc = %d [1]\n",
467 LDISKFS_SB(osd_sb(dev))->s_es->\
468 s_volume_name,PFID(fid), result);
469 if (result == 0 || result == -EALREADY)
470 result = -EINPROGRESS;
479 obj->oo_inode = inode;
480 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
481 if (dev->od_iop_mode) {
482 obj->oo_compat_dot_created = 1;
483 obj->oo_compat_dotdot_created = 1;
486 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
487 GOTO(out, result = 0);
489 LASSERT(obj->oo_hl_head == NULL);
490 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
491 if (obj->oo_hl_head == NULL) {
492 obj->oo_inode = NULL;
494 GOTO(out, result = -ENOMEM);
496 GOTO(out, result = 0);
499 LINVRNT(osd_invariant(obj));
504 * Concurrency: shouldn't matter.
506 static void osd_object_init0(struct osd_object *obj)
508 LASSERT(obj->oo_inode != NULL);
509 obj->oo_dt.do_body_ops = &osd_body_ops;
510 obj->oo_dt.do_lu.lo_header->loh_attr |=
511 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
515 * Concurrency: no concurrent access is possible that early in object
518 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
519 const struct lu_object_conf *conf)
521 struct osd_object *obj = osd_obj(l);
524 LINVRNT(osd_invariant(obj));
526 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
527 obj->oo_dt.do_body_ops = &osd_body_ops_new;
529 if (obj->oo_inode != NULL)
530 osd_object_init0(obj);
532 LINVRNT(osd_invariant(obj));
537 * Concurrency: no concurrent access is possible that late in object
540 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
542 struct osd_object *obj = osd_obj(l);
544 LINVRNT(osd_invariant(obj));
546 dt_object_fini(&obj->oo_dt);
547 if (obj->oo_hl_head != NULL)
548 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
553 * Concurrency: no concurrent access is possible that late in object
556 static void osd_index_fini(struct osd_object *o)
558 struct iam_container *bag;
560 if (o->oo_dir != NULL) {
561 bag = &o->oo_dir->od_container;
562 if (o->oo_inode != NULL) {
563 if (bag->ic_object == o->oo_inode)
564 iam_container_fini(bag);
566 OBD_FREE_PTR(o->oo_dir);
572 * Concurrency: no concurrent access is possible that late in object
573 * life-cycle (for all existing callers, that is. New callers have to provide
574 * their own locking.)
576 static int osd_inode_unlinked(const struct inode *inode)
578 return inode->i_nlink == 0;
582 OSD_TXN_OI_DELETE_CREDITS = 20,
583 OSD_TXN_INODE_DELETE_CREDITS = 20
590 #if OSD_THANDLE_STATS
592 * Set time when the handle is allocated
594 static void osd_th_alloced(struct osd_thandle *oth)
596 oth->oth_alloced = cfs_time_current();
600 * Set time when the handle started
602 static void osd_th_started(struct osd_thandle *oth)
604 oth->oth_started = cfs_time_current();
608 * Helper function to convert time interval to microseconds packed in
609 * long int (default time units for the counter in "stats" initialized
610 * by lu_time_init() )
612 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
616 cfs_duration_usec(cfs_time_sub(end, start), &val);
617 return val.tv_sec * 1000000 + val.tv_usec;
621 * Check whether the we deal with this handle for too long.
623 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
624 cfs_time_t alloced, cfs_time_t started,
627 cfs_time_t now = cfs_time_current();
629 LASSERT(dev != NULL);
631 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
632 interval_to_usec(alloced, started));
633 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
634 interval_to_usec(started, closed));
635 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
636 interval_to_usec(closed, now));
638 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
639 CWARN("transaction handle %p was open for too long: "
641 "alloced "CFS_TIME_T" ,"
642 "started "CFS_TIME_T" ,"
643 "closed "CFS_TIME_T"\n",
644 oth, now, alloced, started, closed);
645 libcfs_debug_dumpstack(NULL);
649 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
651 cfs_time_t __closed = cfs_time_current(); \
652 cfs_time_t __alloced = oth->oth_alloced; \
653 cfs_time_t __started = oth->oth_started; \
656 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
659 #else /* OSD_THANDLE_STATS */
661 #define osd_th_alloced(h) do {} while(0)
662 #define osd_th_started(h) do {} while(0)
663 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
665 #endif /* OSD_THANDLE_STATS */
668 * Concurrency: doesn't access mutable data.
670 static int osd_param_is_sane(const struct osd_device *dev,
671 const struct thandle *th)
673 struct osd_thandle *oh;
674 oh = container_of0(th, struct osd_thandle, ot_super);
675 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
679 * Concurrency: shouldn't matter.
681 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
682 static void osd_trans_commit_cb(struct super_block *sb,
683 struct journal_callback *jcb, int error)
685 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
688 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
689 struct thandle *th = &oh->ot_super;
690 struct lu_device *lud = &th->th_dev->dd_lu_dev;
691 struct dt_txn_commit_cb *dcb, *tmp;
693 LASSERT(oh->ot_handle == NULL);
696 CERROR("transaction @0x%p commit error: %d\n", th, error);
698 dt_txn_hook_commit(th);
700 /* call per-transaction callbacks if any */
701 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
702 cfs_list_del_init(&dcb->dcb_linkage);
703 dcb->dcb_func(NULL, th, dcb, error);
706 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
710 lu_context_exit(&th->th_ctx);
711 lu_context_fini(&th->th_ctx);
715 static struct thandle *osd_trans_create(const struct lu_env *env,
718 struct osd_thread_info *oti = osd_oti_get(env);
719 struct osd_iobuf *iobuf = &oti->oti_iobuf;
720 struct osd_thandle *oh;
724 /* on pending IO in this thread should left from prev. request */
725 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
727 th = ERR_PTR(-ENOMEM);
728 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
733 th->th_tags = LCT_TX_HANDLE;
735 oti->oti_dev = osd_dt_dev(d);
736 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
743 * Concurrency: shouldn't matter.
745 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
748 struct osd_thread_info *oti = osd_oti_get(env);
749 struct osd_device *dev = osd_dt_dev(d);
751 struct osd_thandle *oh;
756 LASSERT(current->journal_info == NULL);
758 oh = container_of0(th, struct osd_thandle, ot_super);
760 LASSERT(oh->ot_handle == NULL);
762 rc = dt_txn_hook_start(env, d, th);
766 if (!osd_param_is_sane(dev, th)) {
767 CWARN("%.16s: too many transaction credits (%d > %d)\n",
768 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
770 osd_journal(dev)->j_max_transaction_buffers);
771 /* XXX Limit the credits to 'max_transaction_buffers', and
772 * let the underlying filesystem to catch the error if
773 * we really need so many credits.
775 * This should be removed when we can calculate the
776 * credits precisely. */
777 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
778 #ifdef OSD_TRACK_DECLARES
779 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
780 oh->ot_declare_attr_set, oh->ot_declare_punch,
781 oh->ot_declare_xattr_set);
782 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
783 oh->ot_declare_create, oh->ot_declare_ref_add,
784 oh->ot_declare_ref_del, oh->ot_declare_write);
785 CERROR(" insert: %d, delete: %d, destroy: %d\n",
786 oh->ot_declare_insert, oh->ot_declare_delete,
787 oh->ot_declare_destroy);
792 * XXX temporary stuff. Some abstraction layer should
795 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
799 LASSERT(oti->oti_txns == 0);
800 lu_context_init(&th->th_ctx, th->th_tags);
801 lu_context_enter(&th->th_ctx);
803 lu_device_get(&d->dd_lu_dev);
804 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
808 * XXX: current rule is that we first start tx,
809 * then lock object(s), but we can't use
810 * this rule for data (due to locking specifics
811 * in ldiskfs). also in long-term we'd like to
812 * use usually-used (locks;tx) ordering. so,
813 * UGLY thing is that we'll use one ordering for
814 * data (ofd) and reverse ordering for metadata
815 * (mdd). then at some point we'll fix the latter
817 if (lu_device_is_md(&d->dd_lu_dev)) {
818 LASSERT(oti->oti_r_locks == 0);
819 LASSERT(oti->oti_w_locks == 0);
832 * Concurrency: shouldn't matter.
834 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
837 struct osd_thandle *oh;
838 struct osd_thread_info *oti = osd_oti_get(env);
839 struct osd_iobuf *iobuf = &oti->oti_iobuf;
843 oh = container_of0(th, struct osd_thandle, ot_super);
845 if (oh->ot_handle != NULL) {
846 handle_t *hdl = oh->ot_handle;
848 hdl->h_sync = th->th_sync;
851 * add commit callback
852 * notice we don't do this in osd_trans_start()
853 * as underlying transaction can change during truncate
855 osd_journal_callback_set(hdl, osd_trans_commit_cb,
858 LASSERT(oti->oti_txns == 1);
861 * XXX: current rule is that we first start tx,
862 * then lock object(s), but we can't use
863 * this rule for data (due to locking specifics
864 * in ldiskfs). also in long-term we'd like to
865 * use usually-used (locks;tx) ordering. so,
866 * UGLY thing is that we'll use one ordering for
867 * data (ofd) and reverse ordering for metadata
868 * (mdd). then at some point we'll fix the latter
870 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
871 LASSERT(oti->oti_r_locks == 0);
872 LASSERT(oti->oti_w_locks == 0);
874 rc = dt_txn_hook_stop(env, th);
876 CERROR("Failure in transaction hook: %d\n", rc);
877 oh->ot_handle = NULL;
878 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
879 rc = ldiskfs_journal_stop(hdl));
881 CERROR("Failure to stop transaction: %d\n", rc);
886 /* as we want IO to journal and data IO be concurrent, we don't block
887 * awaiting data IO completion in osd_do_bio(), instead we wait here
888 * once transaction is submitted to the journal. all reqular requests
889 * don't do direct IO (except read/write), thus this wait_event becomes
892 * IMPORTANT: we have to wait till any IO submited by the thread is
893 * completed otherwise iobuf may be corrupted by different request
895 cfs_wait_event(iobuf->dr_wait,
896 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
898 rc = iobuf->dr_error;
903 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
905 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
908 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
914 * Called just before object is freed. Releases all resources except for
915 * object itself (that is released by osd_object_free()).
917 * Concurrency: no concurrent access is possible that late in object
920 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
922 struct osd_object *obj = osd_obj(l);
923 struct inode *inode = obj->oo_inode;
925 LINVRNT(osd_invariant(obj));
928 * If object is unlinked remove fid->ino mapping from object index.
934 obj->oo_inode = NULL;
939 * Concurrency: ->loo_object_release() is called under site spin-lock.
941 static void osd_object_release(const struct lu_env *env,
947 * Concurrency: shouldn't matter.
949 static int osd_object_print(const struct lu_env *env, void *cookie,
950 lu_printer_t p, const struct lu_object *l)
952 struct osd_object *o = osd_obj(l);
955 if (o->oo_dir != NULL)
956 d = o->oo_dir->od_container.ic_descr;
959 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
961 o->oo_inode ? o->oo_inode->i_ino : 0UL,
962 o->oo_inode ? o->oo_inode->i_generation : 0,
963 d ? d->id_ops->id_name : "plain");
967 * Concurrency: shouldn't matter.
969 int osd_statfs(const struct lu_env *env, struct dt_device *d,
970 struct obd_statfs *sfs)
972 struct osd_device *osd = osd_dt_dev(d);
973 struct super_block *sb = osd_sb(osd);
974 struct kstatfs *ksfs;
977 /* osd_lproc.c call this without env, allocate ksfs for that case */
978 if (unlikely(env == NULL)) {
983 ksfs = &osd_oti_get(env)->oti_ksfs;
986 cfs_spin_lock(&osd->od_osfs_lock);
988 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
989 result = ll_do_statfs(sb, ksfs);
990 if (likely(result == 0)) { /* N.B. statfs can't really fail */
991 osd->od_osfs_age = cfs_time_current_64();
992 statfs_pack(&osd->od_statfs, ksfs);
996 if (likely(result == 0))
997 *sfs = osd->od_statfs;
998 cfs_spin_unlock(&osd->od_osfs_lock);
1000 if (unlikely(env == NULL))
1007 * Concurrency: doesn't access mutable data.
1009 static void osd_conf_get(const struct lu_env *env,
1010 const struct dt_device *dev,
1011 struct dt_device_param *param)
1013 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1016 * XXX should be taken from not-yet-existing fs abstraction layer.
1018 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1019 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1020 param->ddp_block_shift = sb->s_blocksize_bits;
1021 param->ddp_mntopts = 0;
1022 if (test_opt(sb, XATTR_USER))
1023 param->ddp_mntopts |= MNTOPT_USERXATTR;
1024 if (test_opt(sb, POSIX_ACL))
1025 param->ddp_mntopts |= MNTOPT_ACL;
1027 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1028 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1029 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1032 param->ddp_max_ea_size = sb->s_blocksize;
1037 * Helper function to get and fill the buffer with input values.
1039 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1043 buf = &osd_oti_get(env)->oti_buf;
1050 * Concurrency: shouldn't matter.
1052 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1054 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1055 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1059 * Start commit for OSD device.
1061 * An implementation of dt_commit_async method for OSD device.
1062 * Asychronously starts underlayng fs sync and thereby a transaction
1065 * \param env environment
1066 * \param d dt device
1068 * \see dt_device_operations
1070 static int osd_commit_async(const struct lu_env *env,
1071 struct dt_device *d)
1073 struct super_block *s = osd_sb(osd_dt_dev(d));
1076 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1077 RETURN(s->s_op->sync_fs(s, 0));
1081 * Concurrency: shouldn't matter.
1084 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1086 struct super_block *sb = osd_sb(osd_dt_dev(d));
1090 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1092 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1097 * Concurrency: serialization provided by callers.
1099 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1100 int mode, unsigned long timeout, __u32 alg,
1101 struct lustre_capa_key *keys)
1103 struct osd_device *dev = osd_dt_dev(d);
1106 dev->od_fl_capa = mode;
1107 dev->od_capa_timeout = timeout;
1108 dev->od_capa_alg = alg;
1109 dev->od_capa_keys = keys;
1114 * Concurrency: serialization provided by callers.
1116 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1117 struct dt_quota_ctxt *ctxt, void *data)
1119 struct obd_device *obd = (void *)ctxt;
1120 struct vfsmount *mnt = (struct vfsmount *)data;
1123 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1124 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1125 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1126 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1127 obd->obd_lvfs_ctxt.fs = get_ds();
1133 * Note: we do not count into QUOTA here.
1134 * If we mount with --data_journal we may need more.
1136 const int osd_dto_credits_noquota[DTO_NR] = {
1139 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1140 * SINGLEDATA_TRANS_BLOCKS(8)
1141 * XXX Note: maybe iam need more, since iam have more level than
1144 [DTO_INDEX_INSERT] = 16,
1145 [DTO_INDEX_DELETE] = 16,
1149 [DTO_INDEX_UPDATE] = 16,
1151 * Create a object. The same as create object in EXT3.
1152 * DATA_TRANS_BLOCKS(14) +
1153 * INDEX_EXTRA_BLOCKS(8) +
1154 * 3(inode bits, groups, GDT)
1156 [DTO_OBJECT_CREATE] = 25,
1158 * XXX: real credits to be fixed
1160 [DTO_OBJECT_DELETE] = 25,
1162 * Attr set credits (inode)
1164 [DTO_ATTR_SET_BASE] = 1,
1166 * Xattr set. The same as xattr of EXT3.
1167 * DATA_TRANS_BLOCKS(14)
1168 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1169 * are also counted in. Do not know why?
1171 [DTO_XATTR_SET] = 14,
1174 * credits for inode change during write.
1176 [DTO_WRITE_BASE] = 3,
1178 * credits for single block write.
1180 [DTO_WRITE_BLOCK] = 14,
1182 * Attr set credits for chown.
1183 * This is extra credits for setattr, and it is null without quota
1185 [DTO_ATTR_SET_CHOWN]= 0
1188 static const struct dt_device_operations osd_dt_ops = {
1189 .dt_root_get = osd_root_get,
1190 .dt_statfs = osd_statfs,
1191 .dt_trans_create = osd_trans_create,
1192 .dt_trans_start = osd_trans_start,
1193 .dt_trans_stop = osd_trans_stop,
1194 .dt_trans_cb_add = osd_trans_cb_add,
1195 .dt_conf_get = osd_conf_get,
1196 .dt_sync = osd_sync,
1198 .dt_commit_async = osd_commit_async,
1199 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1200 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1203 static void osd_object_read_lock(const struct lu_env *env,
1204 struct dt_object *dt, unsigned role)
1206 struct osd_object *obj = osd_dt_obj(dt);
1207 struct osd_thread_info *oti = osd_oti_get(env);
1209 LINVRNT(osd_invariant(obj));
1211 LASSERT(obj->oo_owner != env);
1212 cfs_down_read_nested(&obj->oo_sem, role);
1214 LASSERT(obj->oo_owner == NULL);
1218 static void osd_object_write_lock(const struct lu_env *env,
1219 struct dt_object *dt, unsigned role)
1221 struct osd_object *obj = osd_dt_obj(dt);
1222 struct osd_thread_info *oti = osd_oti_get(env);
1224 LINVRNT(osd_invariant(obj));
1226 LASSERT(obj->oo_owner != env);
1227 cfs_down_write_nested(&obj->oo_sem, role);
1229 LASSERT(obj->oo_owner == NULL);
1230 obj->oo_owner = env;
1234 static void osd_object_read_unlock(const struct lu_env *env,
1235 struct dt_object *dt)
1237 struct osd_object *obj = osd_dt_obj(dt);
1238 struct osd_thread_info *oti = osd_oti_get(env);
1240 LINVRNT(osd_invariant(obj));
1242 LASSERT(oti->oti_r_locks > 0);
1244 cfs_up_read(&obj->oo_sem);
1247 static void osd_object_write_unlock(const struct lu_env *env,
1248 struct dt_object *dt)
1250 struct osd_object *obj = osd_dt_obj(dt);
1251 struct osd_thread_info *oti = osd_oti_get(env);
1253 LINVRNT(osd_invariant(obj));
1255 LASSERT(obj->oo_owner == env);
1256 LASSERT(oti->oti_w_locks > 0);
1258 obj->oo_owner = NULL;
1259 cfs_up_write(&obj->oo_sem);
1262 static int osd_object_write_locked(const struct lu_env *env,
1263 struct dt_object *dt)
1265 struct osd_object *obj = osd_dt_obj(dt);
1267 LINVRNT(osd_invariant(obj));
1269 return obj->oo_owner == env;
1272 static int capa_is_sane(const struct lu_env *env,
1273 struct osd_device *dev,
1274 struct lustre_capa *capa,
1275 struct lustre_capa_key *keys)
1277 struct osd_thread_info *oti = osd_oti_get(env);
1278 struct lustre_capa *tcapa = &oti->oti_capa;
1279 struct obd_capa *oc;
1283 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1285 if (capa_is_expired(oc)) {
1286 DEBUG_CAPA(D_ERROR, capa, "expired");
1293 if (capa_is_expired_sec(capa)) {
1294 DEBUG_CAPA(D_ERROR, capa, "expired");
1298 cfs_spin_lock(&capa_lock);
1299 for (i = 0; i < 2; i++) {
1300 if (keys[i].lk_keyid == capa->lc_keyid) {
1301 oti->oti_capa_key = keys[i];
1305 cfs_spin_unlock(&capa_lock);
1308 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1312 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1316 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1317 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1321 oc = capa_add(dev->od_capa_hash, capa);
1327 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1328 struct lustre_capa *capa, __u64 opc)
1330 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1331 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1332 struct md_capainfo *ci;
1335 if (!dev->od_fl_capa)
1338 if (capa == BYPASS_CAPA)
1341 ci = md_capainfo(env);
1345 if (ci->mc_auth == LC_ID_NONE)
1349 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1353 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1354 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1359 if (!capa_opc_supported(capa, opc)) {
1360 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1364 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1365 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1372 static struct timespec *osd_inode_time(const struct lu_env *env,
1373 struct inode *inode, __u64 seconds)
1375 struct osd_thread_info *oti = osd_oti_get(env);
1376 struct timespec *t = &oti->oti_time;
1378 t->tv_sec = seconds;
1380 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1385 static void osd_inode_getattr(const struct lu_env *env,
1386 struct inode *inode, struct lu_attr *attr)
1388 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1389 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1390 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1392 attr->la_atime = LTIME_S(inode->i_atime);
1393 attr->la_mtime = LTIME_S(inode->i_mtime);
1394 attr->la_ctime = LTIME_S(inode->i_ctime);
1395 attr->la_mode = inode->i_mode;
1396 attr->la_size = i_size_read(inode);
1397 attr->la_blocks = inode->i_blocks;
1398 attr->la_uid = inode->i_uid;
1399 attr->la_gid = inode->i_gid;
1400 attr->la_flags = LDISKFS_I(inode)->i_flags;
1401 attr->la_nlink = inode->i_nlink;
1402 attr->la_rdev = inode->i_rdev;
1403 attr->la_blksize = ll_inode_blksize(inode);
1404 attr->la_blkbits = inode->i_blkbits;
1407 static int osd_attr_get(const struct lu_env *env,
1408 struct dt_object *dt,
1409 struct lu_attr *attr,
1410 struct lustre_capa *capa)
1412 struct osd_object *obj = osd_dt_obj(dt);
1414 LASSERT(dt_object_exists(dt));
1415 LINVRNT(osd_invariant(obj));
1417 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1420 cfs_spin_lock(&obj->oo_guard);
1421 osd_inode_getattr(env, obj->oo_inode, attr);
1422 cfs_spin_unlock(&obj->oo_guard);
1426 static int osd_declare_attr_set(const struct lu_env *env,
1427 struct dt_object *dt,
1428 const struct lu_attr *attr,
1429 struct thandle *handle)
1431 struct osd_thandle *oh;
1432 struct osd_object *obj;
1434 LASSERT(dt != NULL);
1435 LASSERT(handle != NULL);
1437 obj = osd_dt_obj(dt);
1438 LASSERT(osd_invariant(obj));
1440 oh = container_of0(handle, struct osd_thandle, ot_super);
1441 LASSERT(oh->ot_handle == NULL);
1443 OSD_DECLARE_OP(oh, attr_set);
1444 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1446 if (attr && attr->la_valid & LA_UID) {
1448 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1450 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1452 if (attr && attr->la_valid & LA_GID) {
1454 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1456 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1462 static int osd_inode_setattr(const struct lu_env *env,
1463 struct inode *inode, const struct lu_attr *attr)
1467 bits = attr->la_valid;
1469 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1471 if (bits & LA_ATIME)
1472 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1473 if (bits & LA_CTIME)
1474 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1475 if (bits & LA_MTIME)
1476 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1477 if (bits & LA_SIZE) {
1478 LDISKFS_I(inode)->i_disksize = attr->la_size;
1479 i_size_write(inode, attr->la_size);
1483 /* OSD should not change "i_blocks" which is used by quota.
1484 * "i_blocks" should be changed by ldiskfs only. */
1485 if (bits & LA_BLOCKS)
1486 inode->i_blocks = attr->la_blocks;
1489 inode->i_mode = (inode->i_mode & S_IFMT) |
1490 (attr->la_mode & ~S_IFMT);
1492 inode->i_uid = attr->la_uid;
1494 inode->i_gid = attr->la_gid;
1495 if (bits & LA_NLINK)
1496 inode->i_nlink = attr->la_nlink;
1498 inode->i_rdev = attr->la_rdev;
1500 if (bits & LA_FLAGS) {
1501 /* always keep S_NOCMTIME */
1502 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1508 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1510 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1511 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1516 if (attr->la_valid & LA_UID)
1517 iattr.ia_valid |= ATTR_UID;
1518 if (attr->la_valid & LA_GID)
1519 iattr.ia_valid |= ATTR_GID;
1520 iattr.ia_uid = attr->la_uid;
1521 iattr.ia_gid = attr->la_gid;
1523 rc = ll_vfs_dq_transfer(inode, &iattr);
1525 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1526 "enforcement enabled on the ldiskfs filesystem?",
1527 inode->i_sb->s_id, rc);
1534 static int osd_attr_set(const struct lu_env *env,
1535 struct dt_object *dt,
1536 const struct lu_attr *attr,
1537 struct thandle *handle,
1538 struct lustre_capa *capa)
1540 struct osd_object *obj = osd_dt_obj(dt);
1541 struct inode *inode;
1544 LASSERT(handle != NULL);
1545 LASSERT(dt_object_exists(dt));
1546 LASSERT(osd_invariant(obj));
1548 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1551 OSD_EXEC_OP(handle, attr_set);
1553 inode = obj->oo_inode;
1554 if (LDISKFS_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1555 LDISKFS_FEATURE_RO_COMPAT_QUOTA)) {
1556 rc = osd_quota_transfer(inode, attr);
1560 #ifdef HAVE_QUOTA_SUPPORT
1561 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1562 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1563 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1568 if (attr->la_valid & LA_UID)
1569 iattr.ia_valid |= ATTR_UID;
1570 if (attr->la_valid & LA_GID)
1571 iattr.ia_valid |= ATTR_GID;
1572 iattr.ia_uid = attr->la_uid;
1573 iattr.ia_gid = attr->la_gid;
1574 osd_push_ctxt(env, save);
1575 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1582 cfs_spin_lock(&obj->oo_guard);
1583 rc = osd_inode_setattr(env, inode, attr);
1584 cfs_spin_unlock(&obj->oo_guard);
1587 inode->i_sb->s_op->dirty_inode(inode);
1591 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1592 struct osd_object *obj,
1593 const char *name, const int namelen)
1595 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1598 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1600 struct dt_allocation_hint *hint,
1604 struct osd_device *osd = osd_obj2dev(obj);
1605 struct osd_thandle *oth;
1606 struct dt_object *parent = NULL;
1607 struct inode *inode;
1608 #ifdef HAVE_QUOTA_SUPPORT
1609 struct osd_ctxt *save = &info->oti_ctxt;
1612 LINVRNT(osd_invariant(obj));
1613 LASSERT(obj->oo_inode == NULL);
1614 LASSERT(obj->oo_hl_head == NULL);
1616 if (S_ISDIR(mode) && ldiskfs_pdo) {
1617 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1618 if (obj->oo_hl_head == NULL)
1622 oth = container_of(th, struct osd_thandle, ot_super);
1623 LASSERT(oth->ot_handle->h_transaction != NULL);
1625 if (hint && hint->dah_parent)
1626 parent = hint->dah_parent;
1628 #ifdef HAVE_QUOTA_SUPPORT
1629 osd_push_ctxt(info->oti_env, save);
1631 inode = ldiskfs_create_inode(oth->ot_handle,
1632 parent ? osd_dt_obj(parent)->oo_inode :
1633 osd_sb(osd)->s_root->d_inode,
1635 #ifdef HAVE_QUOTA_SUPPORT
1638 if (!IS_ERR(inode)) {
1639 /* Do not update file c/mtime in ldiskfs.
1640 * NB: don't need any lock because no contention at this
1642 inode->i_flags |= S_NOCMTIME;
1643 inode->i_state |= I_LUSTRE_NOSCRUB;
1644 obj->oo_inode = inode;
1647 if (obj->oo_hl_head != NULL) {
1648 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1649 obj->oo_hl_head = NULL;
1651 result = PTR_ERR(inode);
1653 LINVRNT(osd_invariant(obj));
1661 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1662 struct lu_attr *attr,
1663 struct dt_allocation_hint *hint,
1664 struct dt_object_format *dof,
1668 struct osd_thandle *oth;
1669 struct osd_device *osd = osd_obj2dev(obj);
1670 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1672 LASSERT(S_ISDIR(attr->la_mode));
1674 oth = container_of(th, struct osd_thandle, ot_super);
1675 LASSERT(oth->ot_handle->h_transaction != NULL);
1676 result = osd_mkfile(info, obj, mode, hint, th);
1677 if (result == 0 && osd->od_iop_mode == 0) {
1678 LASSERT(obj->oo_inode != NULL);
1680 * XXX uh-oh... call low-level iam function directly.
1683 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1684 sizeof (struct osd_fid_pack),
1690 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1691 struct lu_attr *attr,
1692 struct dt_allocation_hint *hint,
1693 struct dt_object_format *dof,
1697 struct osd_thandle *oth;
1698 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1700 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1702 LASSERT(S_ISREG(attr->la_mode));
1704 oth = container_of(th, struct osd_thandle, ot_super);
1705 LASSERT(oth->ot_handle->h_transaction != NULL);
1707 result = osd_mkfile(info, obj, mode, hint, th);
1709 LASSERT(obj->oo_inode != NULL);
1710 if (feat->dif_flags & DT_IND_VARKEY)
1711 result = iam_lvar_create(obj->oo_inode,
1712 feat->dif_keysize_max,
1714 feat->dif_recsize_max,
1717 result = iam_lfix_create(obj->oo_inode,
1718 feat->dif_keysize_max,
1720 feat->dif_recsize_max,
1727 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1728 struct lu_attr *attr,
1729 struct dt_allocation_hint *hint,
1730 struct dt_object_format *dof,
1733 LASSERT(S_ISREG(attr->la_mode));
1734 return osd_mkfile(info, obj, (attr->la_mode &
1735 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1738 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1739 struct lu_attr *attr,
1740 struct dt_allocation_hint *hint,
1741 struct dt_object_format *dof,
1744 LASSERT(S_ISLNK(attr->la_mode));
1745 return osd_mkfile(info, obj, (attr->la_mode &
1746 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1749 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1750 struct lu_attr *attr,
1751 struct dt_allocation_hint *hint,
1752 struct dt_object_format *dof,
1755 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1758 LINVRNT(osd_invariant(obj));
1759 LASSERT(obj->oo_inode == NULL);
1760 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1761 S_ISFIFO(mode) || S_ISSOCK(mode));
1763 result = osd_mkfile(info, obj, mode, hint, th);
1765 LASSERT(obj->oo_inode != NULL);
1767 * This inode should be marked dirty for i_rdev. Currently
1768 * that is done in the osd_attr_init().
1770 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1772 LINVRNT(osd_invariant(obj));
1776 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1778 struct dt_allocation_hint *hint,
1779 struct dt_object_format *dof,
1782 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1784 osd_obj_type_f result;
1800 result = osd_mk_index;
1811 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1812 struct dt_object *parent, cfs_umode_t child_mode)
1816 memset(ah, 0, sizeof(*ah));
1817 ah->dah_parent = parent;
1818 ah->dah_mode = child_mode;
1821 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1822 struct lu_attr *attr, struct dt_object_format *dof)
1824 struct inode *inode = obj->oo_inode;
1825 __u64 valid = attr->la_valid;
1828 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1830 if (dof->dof_type != DFT_NODE)
1831 attr->la_valid &= ~LA_RDEV;
1832 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1833 attr->la_valid &= ~LA_ATIME;
1834 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1835 attr->la_valid &= ~LA_CTIME;
1836 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1837 attr->la_valid &= ~LA_MTIME;
1839 if (LDISKFS_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1840 LDISKFS_FEATURE_RO_COMPAT_QUOTA)) {
1841 result = osd_quota_transfer(inode, attr);
1845 #ifdef HAVE_QUOTA_SUPPORT
1846 attr->la_valid &= ~(LA_UID | LA_GID);
1850 if (attr->la_valid != 0) {
1851 result = osd_inode_setattr(info->oti_env, inode, attr);
1853 * The osd_inode_setattr() should always succeed here. The
1854 * only error that could be returned is EDQUOT when we are
1855 * trying to change the UID or GID of the inode. However, this
1856 * should not happen since quota enforcement is no longer
1857 * enabled on ldiskfs (lquota takes care of it).
1859 LASSERTF(result == 0, "%d", result);
1860 inode->i_sb->s_op->dirty_inode(inode);
1863 attr->la_valid = valid;
1867 * Helper function for osd_object_create()
1869 * \retval 0, on success
1871 static int __osd_object_create(struct osd_thread_info *info,
1872 struct osd_object *obj, struct lu_attr *attr,
1873 struct dt_allocation_hint *hint,
1874 struct dt_object_format *dof,
1880 /* we drop umask so that permissions we pass are not affected */
1881 umask = current->fs->umask;
1882 current->fs->umask = 0;
1884 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1887 osd_attr_init(info, obj, attr, dof);
1888 osd_object_init0(obj);
1890 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1891 unlock_new_inode(obj->oo_inode);
1894 /* restore previous umask value */
1895 current->fs->umask = umask;
1901 * Helper function for osd_object_create()
1903 * \retval 0, on success
1905 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1906 const struct lu_fid *fid, struct thandle *th)
1908 struct osd_thread_info *info = osd_oti_get(env);
1909 struct osd_inode_id *id = &info->oti_id;
1910 struct osd_device *osd = osd_obj2dev(obj);
1911 struct md_ucred *uc = md_ucred(env);
1913 LASSERT(obj->oo_inode != NULL);
1914 LASSERT(uc != NULL);
1916 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1917 return osd_oi_insert(info, osd, fid, id, th);
1920 static int osd_declare_object_create(const struct lu_env *env,
1921 struct dt_object *dt,
1922 struct lu_attr *attr,
1923 struct dt_allocation_hint *hint,
1924 struct dt_object_format *dof,
1925 struct thandle *handle)
1927 struct osd_thandle *oh;
1929 LASSERT(handle != NULL);
1931 oh = container_of0(handle, struct osd_thandle, ot_super);
1932 LASSERT(oh->ot_handle == NULL);
1934 OSD_DECLARE_OP(oh, create);
1935 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1936 /* XXX: So far, only normal fid needs be inserted into the oi,
1937 * things could be changed later. Revise following code then. */
1938 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1939 OSD_DECLARE_OP(oh, insert);
1940 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1942 /* If this is directory, then we expect . and .. to be inserted as
1943 * well. The one directory block always needs to be created for the
1944 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1945 * block), there is no danger of needing a tree for the first block.
1947 if (attr && S_ISDIR(attr->la_mode)) {
1948 OSD_DECLARE_OP(oh, insert);
1949 OSD_DECLARE_OP(oh, insert);
1950 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1954 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1955 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1960 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1961 struct lu_attr *attr,
1962 struct dt_allocation_hint *hint,
1963 struct dt_object_format *dof,
1966 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1967 struct osd_object *obj = osd_dt_obj(dt);
1968 struct osd_thread_info *info = osd_oti_get(env);
1973 LINVRNT(osd_invariant(obj));
1974 LASSERT(!dt_object_exists(dt));
1975 LASSERT(osd_write_locked(env, obj));
1976 LASSERT(th != NULL);
1978 if (unlikely(fid_is_acct(fid)))
1979 /* Quota files can't be created from the kernel any more,
1980 * 'tune2fs -O quota' will take care of creating them */
1983 OSD_EXEC_OP(th, create);
1985 result = __osd_object_create(info, obj, attr, hint, dof, th);
1987 result = __osd_oi_insert(env, obj, fid, th);
1989 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1990 LASSERT(osd_invariant(obj));
1995 * Called to destroy on-disk representation of the object
1997 * Concurrency: must be locked
1999 static int osd_declare_object_destroy(const struct lu_env *env,
2000 struct dt_object *dt,
2003 struct osd_object *obj = osd_dt_obj(dt);
2004 struct inode *inode = obj->oo_inode;
2005 struct osd_thandle *oh;
2009 oh = container_of0(th, struct osd_thandle, ot_super);
2010 LASSERT(oh->ot_handle == NULL);
2013 OSD_DECLARE_OP(oh, destroy);
2014 OSD_DECLARE_OP(oh, delete);
2015 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2016 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2018 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
2019 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
2024 static int osd_object_destroy(const struct lu_env *env,
2025 struct dt_object *dt,
2028 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2029 struct osd_object *obj = osd_dt_obj(dt);
2030 struct inode *inode = obj->oo_inode;
2031 struct osd_device *osd = osd_obj2dev(obj);
2032 struct osd_thandle *oh;
2036 oh = container_of0(th, struct osd_thandle, ot_super);
2037 LASSERT(oh->ot_handle);
2039 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2041 if (unlikely(fid_is_acct(fid)))
2044 /* Parallel control for OI scrub. For most of cases, there is no
2045 * lock contention. So it will not affect unlink performance. */
2046 cfs_mutex_lock(&inode->i_mutex);
2047 if (S_ISDIR(inode->i_mode)) {
2048 LASSERT(osd_inode_unlinked(inode) ||
2049 inode->i_nlink == 1);
2050 cfs_spin_lock(&obj->oo_guard);
2052 cfs_spin_unlock(&obj->oo_guard);
2053 inode->i_sb->s_op->dirty_inode(inode);
2055 LASSERT(osd_inode_unlinked(inode));
2058 OSD_EXEC_OP(th, destroy);
2060 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2061 cfs_mutex_unlock(&inode->i_mutex);
2063 /* XXX: add to ext3 orphan list */
2064 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2066 /* not needed in the cache anymore */
2067 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2073 * Helper function for osd_xattr_set()
2075 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2076 const struct lu_buf *buf, const char *name, int fl)
2078 struct osd_object *obj = osd_dt_obj(dt);
2079 struct inode *inode = obj->oo_inode;
2080 struct osd_thread_info *info = osd_oti_get(env);
2081 struct dentry *dentry = &info->oti_child_dentry;
2085 LASSERT(dt_object_exists(dt));
2086 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2088 if (fl & LU_XATTR_REPLACE)
2089 fs_flags |= XATTR_REPLACE;
2091 if (fl & LU_XATTR_CREATE)
2092 fs_flags |= XATTR_CREATE;
2094 dentry->d_inode = inode;
2095 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2096 buf->lb_len, fs_flags);
2101 * Put the fid into lustre_mdt_attrs, and then place the structure
2102 * inode's ea. This fid should not be altered during the life time
2105 * \retval +ve, on success
2106 * \retval -ve, on error
2108 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2110 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2111 const struct lu_fid *fid)
2113 struct osd_thread_info *info = osd_oti_get(env);
2114 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2116 lustre_lma_init(mdt_attrs, fid);
2117 lustre_lma_swab(mdt_attrs);
2118 return __osd_xattr_set(env, dt,
2119 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2120 XATTR_NAME_LMA, LU_XATTR_CREATE);
2125 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2126 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2127 * To have compatilibility with 1.8 ldiskfs driver we need to have
2128 * magic number at start of fid data.
2129 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2132 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2133 const struct dt_rec *fid)
2135 param->edp_magic = LDISKFS_LUFID_MAGIC;
2136 param->edp_len = sizeof(struct lu_fid) + 1;
2138 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2139 (struct lu_fid *)fid);
2143 * Try to read the fid from inode ea into dt_rec, if return value
2144 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2146 * \param fid object fid.
2148 * \retval 0 on success
2150 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2151 __u32 ino, struct lu_fid *fid,
2152 struct osd_inode_id *id)
2154 struct osd_thread_info *info = osd_oti_get(env);
2155 struct inode *inode;
2158 osd_id_gen(id, ino, OSD_OII_NOGEN);
2159 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2161 RETURN(PTR_ERR(inode));
2168 * OSD layer object create function for interoperability mode (b11826).
2169 * This is mostly similar to osd_object_create(). Only difference being, fid is
2170 * inserted into inode ea here.
2172 * \retval 0, on success
2173 * \retval -ve, on error
2175 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2176 struct lu_attr *attr,
2177 struct dt_allocation_hint *hint,
2178 struct dt_object_format *dof,
2181 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2182 struct osd_object *obj = osd_dt_obj(dt);
2183 struct osd_thread_info *info = osd_oti_get(env);
2188 LASSERT(osd_invariant(obj));
2189 LASSERT(!dt_object_exists(dt));
2190 LASSERT(osd_write_locked(env, obj));
2191 LASSERT(th != NULL);
2193 if (unlikely(fid_is_acct(fid)))
2194 /* Quota files can't be created from the kernel any more,
2195 * 'tune2fs -O quota' will take care of creating them */
2198 OSD_EXEC_OP(th, create);
2200 result = __osd_object_create(info, obj, attr, hint, dof, th);
2201 /* objects under osd root shld have igif fid, so dont add fid EA */
2202 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2203 result = osd_ea_fid_set(env, dt, fid);
2206 result = __osd_oi_insert(env, obj, fid, th);
2208 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2209 LINVRNT(osd_invariant(obj));
2213 static int osd_declare_object_ref_add(const struct lu_env *env,
2214 struct dt_object *dt,
2215 struct thandle *handle)
2217 struct osd_thandle *oh;
2219 /* it's possible that object doesn't exist yet */
2220 LASSERT(handle != NULL);
2222 oh = container_of0(handle, struct osd_thandle, ot_super);
2223 LASSERT(oh->ot_handle == NULL);
2225 OSD_DECLARE_OP(oh, ref_add);
2226 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2232 * Concurrency: @dt is write locked.
2234 static int osd_object_ref_add(const struct lu_env *env,
2235 struct dt_object *dt, struct thandle *th)
2237 struct osd_object *obj = osd_dt_obj(dt);
2238 struct inode *inode = obj->oo_inode;
2240 LINVRNT(osd_invariant(obj));
2241 LASSERT(dt_object_exists(dt));
2242 LASSERT(osd_write_locked(env, obj));
2243 LASSERT(th != NULL);
2245 OSD_EXEC_OP(th, ref_add);
2248 * DIR_NLINK feature is set for compatibility reasons if:
2249 * 1) nlinks > LDISKFS_LINK_MAX, or
2250 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2252 * It is easier to always set this flag (rather than check and set),
2253 * since it has less overhead, and the superblock will be dirtied
2254 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2255 * do not actually care whether this flag is set or not.
2257 cfs_spin_lock(&obj->oo_guard);
2259 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2260 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2261 inode->i_nlink == 2)
2264 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2265 cfs_spin_unlock(&obj->oo_guard);
2266 inode->i_sb->s_op->dirty_inode(inode);
2267 LINVRNT(osd_invariant(obj));
2272 static int osd_declare_object_ref_del(const struct lu_env *env,
2273 struct dt_object *dt,
2274 struct thandle *handle)
2276 struct osd_thandle *oh;
2278 LASSERT(dt_object_exists(dt));
2279 LASSERT(handle != NULL);
2281 oh = container_of0(handle, struct osd_thandle, ot_super);
2282 LASSERT(oh->ot_handle == NULL);
2284 OSD_DECLARE_OP(oh, ref_del);
2285 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2291 * Concurrency: @dt is write locked.
2293 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2296 struct osd_object *obj = osd_dt_obj(dt);
2297 struct inode *inode = obj->oo_inode;
2299 LINVRNT(osd_invariant(obj));
2300 LASSERT(dt_object_exists(dt));
2301 LASSERT(osd_write_locked(env, obj));
2302 LASSERT(th != NULL);
2304 OSD_EXEC_OP(th, ref_del);
2306 cfs_spin_lock(&obj->oo_guard);
2307 LASSERT(inode->i_nlink > 0);
2309 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2310 * then the nlink count is 1. Don't let it be set to 0 or the directory
2311 * inode will be deleted incorrectly. */
2312 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2314 cfs_spin_unlock(&obj->oo_guard);
2315 inode->i_sb->s_op->dirty_inode(inode);
2316 LINVRNT(osd_invariant(obj));
2322 * Get the 64-bit version for an inode.
2324 static int osd_object_version_get(const struct lu_env *env,
2325 struct dt_object *dt, dt_obj_version_t *ver)
2327 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2329 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2330 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2331 *ver = LDISKFS_I(inode)->i_fs_version;
2336 * Concurrency: @dt is read locked.
2338 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2339 struct lu_buf *buf, const char *name,
2340 struct lustre_capa *capa)
2342 struct osd_object *obj = osd_dt_obj(dt);
2343 struct inode *inode = obj->oo_inode;
2344 struct osd_thread_info *info = osd_oti_get(env);
2345 struct dentry *dentry = &info->oti_obj_dentry;
2347 /* version get is not real XATTR but uses xattr API */
2348 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2349 /* for version we are just using xattr API but change inode
2351 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2352 osd_object_version_get(env, dt, buf->lb_buf);
2353 return sizeof(dt_obj_version_t);
2356 LASSERT(dt_object_exists(dt));
2357 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2358 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2360 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2363 dentry->d_inode = inode;
2364 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2368 static int osd_declare_xattr_set(const struct lu_env *env,
2369 struct dt_object *dt,
2370 const struct lu_buf *buf, const char *name,
2371 int fl, struct thandle *handle)
2373 struct osd_thandle *oh;
2375 LASSERT(handle != NULL);
2377 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2378 /* no credits for version */
2382 oh = container_of0(handle, struct osd_thandle, ot_super);
2383 LASSERT(oh->ot_handle == NULL);
2385 OSD_DECLARE_OP(oh, xattr_set);
2386 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2392 * Set the 64-bit version for object
2394 static void osd_object_version_set(const struct lu_env *env,
2395 struct dt_object *dt,
2396 dt_obj_version_t *new_version)
2398 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2400 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2401 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2403 LDISKFS_I(inode)->i_fs_version = *new_version;
2404 /** Version is set after all inode operations are finished,
2405 * so we should mark it dirty here */
2406 inode->i_sb->s_op->dirty_inode(inode);
2410 * Concurrency: @dt is write locked.
2412 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2413 const struct lu_buf *buf, const char *name, int fl,
2414 struct thandle *handle, struct lustre_capa *capa)
2416 LASSERT(handle != NULL);
2418 /* version set is not real XATTR */
2419 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2420 /* for version we are just using xattr API but change inode
2422 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2423 osd_object_version_set(env, dt, buf->lb_buf);
2424 return sizeof(dt_obj_version_t);
2427 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2430 OSD_EXEC_OP(handle, xattr_set);
2431 return __osd_xattr_set(env, dt, buf, name, fl);
2435 * Concurrency: @dt is read locked.
2437 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2438 struct lu_buf *buf, struct lustre_capa *capa)
2440 struct osd_object *obj = osd_dt_obj(dt);
2441 struct inode *inode = obj->oo_inode;
2442 struct osd_thread_info *info = osd_oti_get(env);
2443 struct dentry *dentry = &info->oti_obj_dentry;
2445 LASSERT(dt_object_exists(dt));
2446 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2447 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2449 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2452 dentry->d_inode = inode;
2453 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2456 static int osd_declare_xattr_del(const struct lu_env *env,
2457 struct dt_object *dt, const char *name,
2458 struct thandle *handle)
2460 struct osd_thandle *oh;
2462 LASSERT(dt_object_exists(dt));
2463 LASSERT(handle != NULL);
2465 oh = container_of0(handle, struct osd_thandle, ot_super);
2466 LASSERT(oh->ot_handle == NULL);
2468 OSD_DECLARE_OP(oh, xattr_set);
2469 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2475 * Concurrency: @dt is write locked.
2477 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2478 const char *name, struct thandle *handle,
2479 struct lustre_capa *capa)
2481 struct osd_object *obj = osd_dt_obj(dt);
2482 struct inode *inode = obj->oo_inode;
2483 struct osd_thread_info *info = osd_oti_get(env);
2484 struct dentry *dentry = &info->oti_obj_dentry;
2487 LASSERT(dt_object_exists(dt));
2488 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2489 LASSERT(osd_write_locked(env, obj));
2490 LASSERT(handle != NULL);
2492 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2495 OSD_EXEC_OP(handle, xattr_set);
2497 dentry->d_inode = inode;
2498 rc = inode->i_op->removexattr(dentry, name);
2502 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2503 struct dt_object *dt,
2504 struct lustre_capa *old,
2507 struct osd_thread_info *info = osd_oti_get(env);
2508 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2509 struct osd_object *obj = osd_dt_obj(dt);
2510 struct osd_device *dev = osd_obj2dev(obj);
2511 struct lustre_capa_key *key = &info->oti_capa_key;
2512 struct lustre_capa *capa = &info->oti_capa;
2513 struct obd_capa *oc;
2514 struct md_capainfo *ci;
2518 if (!dev->od_fl_capa)
2519 RETURN(ERR_PTR(-ENOENT));
2521 LASSERT(dt_object_exists(dt));
2522 LINVRNT(osd_invariant(obj));
2524 /* renewal sanity check */
2525 if (old && osd_object_auth(env, dt, old, opc))
2526 RETURN(ERR_PTR(-EACCES));
2528 ci = md_capainfo(env);
2530 RETURN(ERR_PTR(-ENOENT));
2532 switch (ci->mc_auth) {
2536 capa->lc_uid = obj->oo_inode->i_uid;
2537 capa->lc_gid = obj->oo_inode->i_gid;
2538 capa->lc_flags = LC_ID_PLAIN;
2540 case LC_ID_CONVERT: {
2543 s[0] = obj->oo_inode->i_uid;
2544 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2545 s[2] = obj->oo_inode->i_gid;
2546 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2547 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2549 RETURN(ERR_PTR(rc));
2551 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2552 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2553 capa->lc_flags = LC_ID_CONVERT;
2557 RETURN(ERR_PTR(-EINVAL));
2560 capa->lc_fid = *fid;
2562 capa->lc_flags |= dev->od_capa_alg << 24;
2563 capa->lc_timeout = dev->od_capa_timeout;
2564 capa->lc_expiry = 0;
2566 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2568 LASSERT(!capa_is_expired(oc));
2572 cfs_spin_lock(&capa_lock);
2573 *key = dev->od_capa_keys[1];
2574 cfs_spin_unlock(&capa_lock);
2576 capa->lc_keyid = key->lk_keyid;
2577 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2579 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2581 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2582 RETURN(ERR_PTR(rc));
2585 oc = capa_add(dev->od_capa_hash, capa);
2589 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2591 struct osd_object *obj = osd_dt_obj(dt);
2592 struct inode *inode = obj->oo_inode;
2593 struct osd_thread_info *info = osd_oti_get(env);
2594 struct dentry *dentry = &info->oti_obj_dentry;
2595 struct file *file = &info->oti_file;
2600 dentry->d_inode = inode;
2601 file->f_dentry = dentry;
2602 file->f_mapping = inode->i_mapping;
2603 file->f_op = inode->i_fop;
2604 LOCK_INODE_MUTEX(inode);
2605 rc = file->f_op->fsync(file, dentry, 0);
2606 UNLOCK_INODE_MUTEX(inode);
2610 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2613 struct osd_object *obj = osd_dt_obj(dt);
2616 *data = (void *)obj->oo_inode;
2624 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2625 const struct dt_index_features *feat)
2627 struct iam_descr *descr;
2629 if (osd_object_is_root(o))
2630 return feat == &dt_directory_features;
2632 LASSERT(o->oo_dir != NULL);
2634 descr = o->oo_dir->od_container.ic_descr;
2635 if (feat == &dt_directory_features) {
2636 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2642 feat->dif_keysize_min <= descr->id_key_size &&
2643 descr->id_key_size <= feat->dif_keysize_max &&
2644 feat->dif_recsize_min <= descr->id_rec_size &&
2645 descr->id_rec_size <= feat->dif_recsize_max &&
2646 !(feat->dif_flags & (DT_IND_VARKEY |
2647 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2648 ergo(feat->dif_flags & DT_IND_UPDATE,
2649 1 /* XXX check that object (and file system) is
2654 static int osd_iam_container_init(const struct lu_env *env,
2655 struct osd_object *obj,
2656 struct osd_directory *dir)
2658 struct iam_container *bag = &dir->od_container;
2661 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2665 result = iam_container_setup(bag);
2669 if (osd_obj2dev(obj)->od_iop_mode) {
2670 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2672 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2678 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2680 iam_container_fini(bag);
2687 * Concurrency: no external locking is necessary.
2689 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2690 const struct dt_index_features *feat)
2694 struct osd_object *obj = osd_dt_obj(dt);
2695 struct osd_device *osd = osd_obj2dev(obj);
2697 LINVRNT(osd_invariant(obj));
2698 LASSERT(dt_object_exists(dt));
2700 if (osd_object_is_root(obj)) {
2701 dt->do_index_ops = &osd_index_ea_ops;
2703 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2704 dt->do_index_ops = &osd_index_ea_ops;
2705 if (S_ISDIR(obj->oo_inode->i_mode))
2710 } else if (unlikely(feat == &dt_otable_features)) {
2711 dt->do_index_ops = &osd_otable_ops;
2713 } else if (feat == &dt_acct_features) {
2714 dt->do_index_ops = &osd_acct_index_ops;
2717 } else if (!osd_has_index(obj)) {
2718 struct osd_directory *dir;
2723 cfs_spin_lock(&obj->oo_guard);
2724 if (obj->oo_dir == NULL)
2728 * Concurrent thread allocated container data.
2731 cfs_spin_unlock(&obj->oo_guard);
2733 * Now, that we have container data, serialize its
2736 cfs_down_write(&obj->oo_ext_idx_sem);
2738 * recheck under lock.
2740 if (!osd_has_index(obj))
2741 result = osd_iam_container_init(env, obj, dir);
2744 cfs_up_write(&obj->oo_ext_idx_sem);
2752 if (result == 0 && skip_iam == 0) {
2753 if (!osd_iam_index_probe(env, obj, feat))
2756 LINVRNT(osd_invariant(obj));
2761 static const struct dt_object_operations osd_obj_ops = {
2762 .do_read_lock = osd_object_read_lock,
2763 .do_write_lock = osd_object_write_lock,
2764 .do_read_unlock = osd_object_read_unlock,
2765 .do_write_unlock = osd_object_write_unlock,
2766 .do_write_locked = osd_object_write_locked,
2767 .do_attr_get = osd_attr_get,
2768 .do_declare_attr_set = osd_declare_attr_set,
2769 .do_attr_set = osd_attr_set,
2770 .do_ah_init = osd_ah_init,
2771 .do_declare_create = osd_declare_object_create,
2772 .do_create = osd_object_create,
2773 .do_declare_destroy = osd_declare_object_destroy,
2774 .do_destroy = osd_object_destroy,
2775 .do_index_try = osd_index_try,
2776 .do_declare_ref_add = osd_declare_object_ref_add,
2777 .do_ref_add = osd_object_ref_add,
2778 .do_declare_ref_del = osd_declare_object_ref_del,
2779 .do_ref_del = osd_object_ref_del,
2780 .do_xattr_get = osd_xattr_get,
2781 .do_declare_xattr_set = osd_declare_xattr_set,
2782 .do_xattr_set = osd_xattr_set,
2783 .do_declare_xattr_del = osd_declare_xattr_del,
2784 .do_xattr_del = osd_xattr_del,
2785 .do_xattr_list = osd_xattr_list,
2786 .do_capa_get = osd_capa_get,
2787 .do_object_sync = osd_object_sync,
2788 .do_data_get = osd_data_get,
2792 * dt_object_operations for interoperability mode
2793 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2795 static const struct dt_object_operations osd_obj_ea_ops = {
2796 .do_read_lock = osd_object_read_lock,
2797 .do_write_lock = osd_object_write_lock,
2798 .do_read_unlock = osd_object_read_unlock,
2799 .do_write_unlock = osd_object_write_unlock,
2800 .do_write_locked = osd_object_write_locked,
2801 .do_attr_get = osd_attr_get,
2802 .do_declare_attr_set = osd_declare_attr_set,
2803 .do_attr_set = osd_attr_set,
2804 .do_ah_init = osd_ah_init,
2805 .do_declare_create = osd_declare_object_create,
2806 .do_create = osd_object_ea_create,
2807 .do_declare_destroy = osd_declare_object_destroy,
2808 .do_destroy = osd_object_destroy,
2809 .do_index_try = osd_index_try,
2810 .do_declare_ref_add = osd_declare_object_ref_add,
2811 .do_ref_add = osd_object_ref_add,
2812 .do_declare_ref_del = osd_declare_object_ref_del,
2813 .do_ref_del = osd_object_ref_del,
2814 .do_xattr_get = osd_xattr_get,
2815 .do_declare_xattr_set = osd_declare_xattr_set,
2816 .do_xattr_set = osd_xattr_set,
2817 .do_declare_xattr_del = osd_declare_xattr_del,
2818 .do_xattr_del = osd_xattr_del,
2819 .do_xattr_list = osd_xattr_list,
2820 .do_capa_get = osd_capa_get,
2821 .do_object_sync = osd_object_sync,
2822 .do_data_get = osd_data_get,
2825 static int osd_index_declare_iam_delete(const struct lu_env *env,
2826 struct dt_object *dt,
2827 const struct dt_key *key,
2828 struct thandle *handle)
2830 struct osd_thandle *oh;
2832 oh = container_of0(handle, struct osd_thandle, ot_super);
2833 LASSERT(oh->ot_handle == NULL);
2835 OSD_DECLARE_OP(oh, delete);
2836 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2842 * delete a (key, value) pair from index \a dt specified by \a key
2844 * \param dt osd index object
2845 * \param key key for index
2846 * \param rec record reference
2847 * \param handle transaction handler
2850 * \retval -ve failure
2853 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2854 const struct dt_key *key,
2855 struct thandle *handle,
2856 struct lustre_capa *capa)
2858 struct osd_object *obj = osd_dt_obj(dt);
2859 struct osd_thandle *oh;
2860 struct iam_path_descr *ipd;
2861 struct iam_container *bag = &obj->oo_dir->od_container;
2866 LINVRNT(osd_invariant(obj));
2867 LASSERT(dt_object_exists(dt));
2868 LASSERT(bag->ic_object == obj->oo_inode);
2869 LASSERT(handle != NULL);
2871 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2874 OSD_EXEC_OP(handle, delete);
2876 ipd = osd_idx_ipd_get(env, bag);
2877 if (unlikely(ipd == NULL))
2880 oh = container_of0(handle, struct osd_thandle, ot_super);
2881 LASSERT(oh->ot_handle != NULL);
2882 LASSERT(oh->ot_handle->h_transaction != NULL);
2884 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2885 osd_ipd_put(env, bag, ipd);
2886 LINVRNT(osd_invariant(obj));
2890 static int osd_index_declare_ea_delete(const struct lu_env *env,
2891 struct dt_object *dt,
2892 const struct dt_key *key,
2893 struct thandle *handle)
2895 struct osd_thandle *oh;
2897 LASSERT(dt_object_exists(dt));
2898 LASSERT(handle != NULL);
2900 oh = container_of0(handle, struct osd_thandle, ot_super);
2901 LASSERT(oh->ot_handle == NULL);
2903 OSD_DECLARE_OP(oh, delete);
2904 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2906 LASSERT(osd_dt_obj(dt)->oo_inode);
2907 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2908 osd_dt_obj(dt)->oo_inode);
2909 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2910 osd_dt_obj(dt)->oo_inode);
2915 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2918 struct osd_fid_pack *rec;
2921 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2922 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2923 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2929 * Index delete function for interoperability mode (b11826).
2930 * It will remove the directory entry added by osd_index_ea_insert().
2931 * This entry is needed to maintain name->fid mapping.
2933 * \param key, key i.e. file entry to be deleted
2935 * \retval 0, on success
2936 * \retval -ve, on error
2938 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2939 const struct dt_key *key,
2940 struct thandle *handle,
2941 struct lustre_capa *capa)
2943 struct osd_object *obj = osd_dt_obj(dt);
2944 struct inode *dir = obj->oo_inode;
2945 struct dentry *dentry;
2946 struct osd_thandle *oh;
2947 struct ldiskfs_dir_entry_2 *de;
2948 struct buffer_head *bh;
2949 struct htree_lock *hlock = NULL;
2954 LINVRNT(osd_invariant(obj));
2955 LASSERT(dt_object_exists(dt));
2956 LASSERT(handle != NULL);
2958 OSD_EXEC_OP(handle, delete);
2960 oh = container_of(handle, struct osd_thandle, ot_super);
2961 LASSERT(oh->ot_handle != NULL);
2962 LASSERT(oh->ot_handle->h_transaction != NULL);
2964 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2967 dentry = osd_child_dentry_get(env, obj,
2968 (char *)key, strlen((char *)key));
2970 if (obj->oo_hl_head != NULL) {
2971 hlock = osd_oti_get(env)->oti_hlock;
2972 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2973 dir, LDISKFS_HLOCK_DEL);
2975 cfs_down_write(&obj->oo_ext_idx_sem);
2978 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2980 rc = ldiskfs_delete_entry(oh->ot_handle,
2987 ldiskfs_htree_unlock(hlock);
2989 cfs_up_write(&obj->oo_ext_idx_sem);
2991 LASSERT(osd_invariant(obj));
2996 * Lookup index for \a key and copy record to \a rec.
2998 * \param dt osd index object
2999 * \param key key for index
3000 * \param rec record reference
3002 * \retval +ve success : exact mach
3003 * \retval 0 return record with key not greater than \a key
3004 * \retval -ve failure
3006 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3007 struct dt_rec *rec, const struct dt_key *key,
3008 struct lustre_capa *capa)
3010 struct osd_object *obj = osd_dt_obj(dt);
3011 struct iam_path_descr *ipd;
3012 struct iam_container *bag = &obj->oo_dir->od_container;
3013 struct osd_thread_info *oti = osd_oti_get(env);
3014 struct iam_iterator *it = &oti->oti_idx_it;
3015 struct iam_rec *iam_rec;
3020 LASSERT(osd_invariant(obj));
3021 LASSERT(dt_object_exists(dt));
3022 LASSERT(bag->ic_object == obj->oo_inode);
3024 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3027 ipd = osd_idx_ipd_get(env, bag);
3031 /* got ipd now we can start iterator. */
3032 iam_it_init(it, bag, 0, ipd);
3034 rc = iam_it_get(it, (struct iam_key *)key);
3036 if (S_ISDIR(obj->oo_inode->i_mode))
3037 iam_rec = (struct iam_rec *)oti->oti_ldp;
3039 iam_rec = (struct iam_rec *) rec;
3041 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3042 if (S_ISDIR(obj->oo_inode->i_mode))
3043 osd_fid_unpack((struct lu_fid *) rec,
3044 (struct osd_fid_pack *)iam_rec);
3048 osd_ipd_put(env, bag, ipd);
3050 LINVRNT(osd_invariant(obj));
3055 static int osd_index_declare_iam_insert(const struct lu_env *env,
3056 struct dt_object *dt,
3057 const struct dt_rec *rec,
3058 const struct dt_key *key,
3059 struct thandle *handle)
3061 struct osd_thandle *oh;
3063 LASSERT(dt_object_exists(dt));
3064 LASSERT(handle != NULL);
3066 oh = container_of0(handle, struct osd_thandle, ot_super);
3067 LASSERT(oh->ot_handle == NULL);
3069 OSD_DECLARE_OP(oh, insert);
3070 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3076 * Inserts (key, value) pair in \a dt index object.
3078 * \param dt osd index object
3079 * \param key key for index
3080 * \param rec record reference
3081 * \param th transaction handler
3084 * \retval -ve failure
3086 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3087 const struct dt_rec *rec,
3088 const struct dt_key *key, struct thandle *th,
3089 struct lustre_capa *capa, int ignore_quota)
3091 struct osd_object *obj = osd_dt_obj(dt);
3092 struct iam_path_descr *ipd;
3093 struct osd_thandle *oh;
3094 struct iam_container *bag = &obj->oo_dir->od_container;
3095 #ifdef HAVE_QUOTA_SUPPORT
3096 cfs_cap_t save = cfs_curproc_cap_pack();
3098 struct osd_thread_info *oti = osd_oti_get(env);
3099 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3104 LINVRNT(osd_invariant(obj));
3105 LASSERT(dt_object_exists(dt));
3106 LASSERT(bag->ic_object == obj->oo_inode);
3107 LASSERT(th != NULL);
3109 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3112 OSD_EXEC_OP(th, insert);
3114 ipd = osd_idx_ipd_get(env, bag);
3115 if (unlikely(ipd == NULL))
3118 oh = container_of0(th, struct osd_thandle, ot_super);
3119 LASSERT(oh->ot_handle != NULL);
3120 LASSERT(oh->ot_handle->h_transaction != NULL);
3121 #ifdef HAVE_QUOTA_SUPPORT
3123 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3125 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3127 if (S_ISDIR(obj->oo_inode->i_mode))
3128 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3130 iam_rec = (struct iam_rec *) rec;
3131 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3133 #ifdef HAVE_QUOTA_SUPPORT
3134 cfs_curproc_cap_unpack(save);
3136 osd_ipd_put(env, bag, ipd);
3137 LINVRNT(osd_invariant(obj));
3142 * Calls ldiskfs_add_entry() to add directory entry
3143 * into the directory. This is required for
3144 * interoperability mode (b11826)
3146 * \retval 0, on success
3147 * \retval -ve, on error
3149 static int __osd_ea_add_rec(struct osd_thread_info *info,
3150 struct osd_object *pobj, struct inode *cinode,
3151 const char *name, const struct dt_rec *fid,
3152 struct htree_lock *hlock, struct thandle *th)
3154 struct ldiskfs_dentry_param *ldp;
3155 struct dentry *child;
3156 struct osd_thandle *oth;
3159 oth = container_of(th, struct osd_thandle, ot_super);
3160 LASSERT(oth->ot_handle != NULL);
3161 LASSERT(oth->ot_handle->h_transaction != NULL);
3163 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3165 /* XXX: remove fid_is_igif() check here.
3166 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3167 * it is IGIF now but needs FID in dir entry as well for readdir
3169 * LU-838 should fix that and remove fid_is_igif() check */
3170 if (fid_is_igif((struct lu_fid *)fid) ||
3171 fid_is_norm((struct lu_fid *)fid)) {
3172 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3173 osd_get_ldiskfs_dirent_param(ldp, fid);
3174 child->d_fsdata = (void *)ldp;
3176 child->d_fsdata = NULL;
3178 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3184 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3185 * into the directory.Also sets flags into osd object to
3186 * indicate dot and dotdot are created. This is required for
3187 * interoperability mode (b11826)
3189 * \param dir directory for dot and dotdot fixup.
3190 * \param obj child object for linking
3192 * \retval 0, on success
3193 * \retval -ve, on error
3195 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3196 struct osd_object *dir,
3197 struct inode *parent_dir, const char *name,
3198 const struct dt_rec *dot_fid,
3199 const struct dt_rec *dot_dot_fid,
3202 struct inode *inode = dir->oo_inode;
3203 struct ldiskfs_dentry_param *dot_ldp;
3204 struct ldiskfs_dentry_param *dot_dot_ldp;
3205 struct osd_thandle *oth;
3208 oth = container_of(th, struct osd_thandle, ot_super);
3209 LASSERT(oth->ot_handle->h_transaction != NULL);
3210 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3212 if (strcmp(name, dot) == 0) {
3213 if (dir->oo_compat_dot_created) {
3216 LASSERT(inode == parent_dir);
3217 dir->oo_compat_dot_created = 1;
3220 } else if(strcmp(name, dotdot) == 0) {
3221 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3222 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3224 if (!dir->oo_compat_dot_created)
3226 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3227 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3228 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3233 /* in case of rename, dotdot is already created */
3234 if (dir->oo_compat_dotdot_created) {
3235 return __osd_ea_add_rec(info, dir, parent_dir, name,
3236 dot_dot_fid, NULL, th);
3239 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3240 inode, dot_ldp, dot_dot_ldp);
3242 dir->oo_compat_dotdot_created = 1;
3250 * It will call the appropriate osd_add* function and return the
3251 * value, return by respective functions.
3253 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3254 struct inode *cinode, const char *name,
3255 const struct dt_rec *fid, struct thandle *th)
3257 struct osd_thread_info *info = osd_oti_get(env);
3258 struct htree_lock *hlock;
3261 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3263 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3265 if (hlock != NULL) {
3266 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3269 cfs_down_write(&pobj->oo_ext_idx_sem);
3271 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3272 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3275 if (hlock != NULL) {
3276 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3277 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3279 cfs_down_write(&pobj->oo_ext_idx_sem);
3282 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3286 ldiskfs_htree_unlock(hlock);
3288 cfs_up_write(&pobj->oo_ext_idx_sem);
3294 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3295 struct osd_idmap_cache *oic)
3297 struct osd_scrub *scrub = &dev->od_scrub;
3298 struct lu_fid *fid = &oic->oic_fid;
3299 struct osd_inode_id *id = &oti->oti_id;
3305 rc = osd_oi_lookup(oti, dev, fid, id);
3306 if (rc != 0 && rc != -ENOENT)
3309 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3312 if (thread_is_running(&scrub->os_thread)) {
3313 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3314 /* There is race condition between osd_oi_lookup and OI scrub.
3315 * The OI scrub finished just after osd_oi_lookup() failure.
3316 * Under such case, it is unnecessary to trigger OI scrub again,
3317 * but try to call osd_oi_lookup() again. */
3318 if (unlikely(rc == -EAGAIN))
3324 if (!scrub->os_no_scrub && ++once == 1) {
3325 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3327 rc = osd_scrub_start(dev);
3328 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3330 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3336 RETURN(rc = -EREMCHG);
3340 * Calls ->lookup() to find dentry. From dentry get inode and
3341 * read inode's ea to get fid. This is required for interoperability
3344 * \retval 0, on success
3345 * \retval -ve, on error
3347 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3348 struct dt_rec *rec, const struct dt_key *key)
3350 struct inode *dir = obj->oo_inode;
3351 struct dentry *dentry;
3352 struct ldiskfs_dir_entry_2 *de;
3353 struct buffer_head *bh;
3354 struct lu_fid *fid = (struct lu_fid *) rec;
3355 struct htree_lock *hlock = NULL;
3359 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3361 dentry = osd_child_dentry_get(env, obj,
3362 (char *)key, strlen((char *)key));
3364 if (obj->oo_hl_head != NULL) {
3365 hlock = osd_oti_get(env)->oti_hlock;
3366 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3367 dir, LDISKFS_HLOCK_LOOKUP);
3369 cfs_down_read(&obj->oo_ext_idx_sem);
3372 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3374 struct osd_thread_info *oti = osd_oti_get(env);
3375 struct osd_idmap_cache *oic = &oti->oti_cache;
3376 struct osd_device *dev = osd_obj2dev(obj);
3377 struct osd_scrub *scrub = &dev->od_scrub;
3378 struct scrub_file *sf = &scrub->os_file;
3380 ino = le32_to_cpu(de->inode);
3381 rc = osd_get_fid_from_dentry(de, rec);
3383 /* done with de, release bh */
3386 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3388 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3390 if (rc != 0 || !fid_is_norm(fid))
3393 oic->oic_fid = *fid;
3394 if ((scrub->os_pos_current <= ino) &&
3395 (sf->sf_flags & SF_INCONSISTENT ||
3396 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3398 rc = osd_consistency_check(oti, dev, oic);
3407 ldiskfs_htree_unlock(hlock);
3409 cfs_up_read(&obj->oo_ext_idx_sem);
3414 * Find the osd object for given fid.
3416 * \param fid need to find the osd object having this fid
3418 * \retval osd_object on success
3419 * \retval -ve on error
3421 struct osd_object *osd_object_find(const struct lu_env *env,
3422 struct dt_object *dt,
3423 const struct lu_fid *fid)
3425 struct lu_device *ludev = dt->do_lu.lo_dev;
3426 struct osd_object *child = NULL;
3427 struct lu_object *luch;
3428 struct lu_object *lo;
3430 luch = lu_object_find(env, ludev, fid, NULL);
3431 if (!IS_ERR(luch)) {
3432 if (lu_object_exists(luch)) {
3433 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3435 child = osd_obj(lo);
3437 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3438 "lu_object can't be located"
3439 DFID"\n", PFID(fid));
3441 if (child == NULL) {
3442 lu_object_put(env, luch);
3443 CERROR("Unable to get osd_object\n");
3444 child = ERR_PTR(-ENOENT);
3447 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3448 "lu_object does not exists "DFID"\n",
3450 lu_object_put(env, luch);
3451 child = ERR_PTR(-ENOENT);
3454 child = (void *)luch;
3460 * Put the osd object once done with it.
3462 * \param obj osd object that needs to be put
3464 static inline void osd_object_put(const struct lu_env *env,
3465 struct osd_object *obj)
3467 lu_object_put(env, &obj->oo_dt.do_lu);
3470 static int osd_index_declare_ea_insert(const struct lu_env *env,
3471 struct dt_object *dt,
3472 const struct dt_rec *rec,
3473 const struct dt_key *key,
3474 struct thandle *handle)
3476 struct osd_thandle *oh;
3478 LASSERT(dt_object_exists(dt));
3479 LASSERT(handle != NULL);
3481 oh = container_of0(handle, struct osd_thandle, ot_super);
3482 LASSERT(oh->ot_handle == NULL);
3484 OSD_DECLARE_OP(oh, insert);
3485 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3487 LASSERT(osd_dt_obj(dt)->oo_inode);
3488 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3489 osd_dt_obj(dt)->oo_inode);
3490 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3491 osd_dt_obj(dt)->oo_inode);
3497 * Index add function for interoperability mode (b11826).
3498 * It will add the directory entry.This entry is needed to
3499 * maintain name->fid mapping.
3501 * \param key it is key i.e. file entry to be inserted
3502 * \param rec it is value of given key i.e. fid
3504 * \retval 0, on success
3505 * \retval -ve, on error
3507 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3508 const struct dt_rec *rec,
3509 const struct dt_key *key, struct thandle *th,
3510 struct lustre_capa *capa, int ignore_quota)
3512 struct osd_object *obj = osd_dt_obj(dt);
3513 struct lu_fid *fid = (struct lu_fid *) rec;
3514 const char *name = (const char *)key;
3515 struct osd_object *child;
3516 #ifdef HAVE_QUOTA_SUPPORT
3517 cfs_cap_t save = cfs_curproc_cap_pack();
3523 LASSERT(osd_invariant(obj));
3524 LASSERT(dt_object_exists(dt));
3525 LASSERT(th != NULL);
3527 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3530 child = osd_object_find(env, dt, fid);
3531 if (!IS_ERR(child)) {
3532 #ifdef HAVE_QUOTA_SUPPORT
3534 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3536 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3538 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3539 #ifdef HAVE_QUOTA_SUPPORT
3540 cfs_curproc_cap_unpack(save);
3542 osd_object_put(env, child);
3544 rc = PTR_ERR(child);
3547 LASSERT(osd_invariant(obj));
3552 * Initialize osd Iterator for given osd index object.
3554 * \param dt osd index object
3557 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3558 struct dt_object *dt,
3560 struct lustre_capa *capa)
3562 struct osd_it_iam *it;
3563 struct osd_thread_info *oti = osd_oti_get(env);
3564 struct osd_object *obj = osd_dt_obj(dt);
3565 struct lu_object *lo = &dt->do_lu;
3566 struct iam_path_descr *ipd;
3567 struct iam_container *bag = &obj->oo_dir->od_container;
3569 LASSERT(lu_object_exists(lo));
3571 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3572 return ERR_PTR(-EACCES);
3575 ipd = osd_it_ipd_get(env, bag);
3576 if (likely(ipd != NULL)) {
3580 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3581 return (struct dt_it *)it;
3583 return ERR_PTR(-ENOMEM);
3587 * free given Iterator.
3590 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3592 struct osd_it_iam *it = (struct osd_it_iam *)di;
3593 struct osd_object *obj = it->oi_obj;
3595 iam_it_fini(&it->oi_it);
3596 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3597 lu_object_put(env, &obj->oo_dt.do_lu);
3601 * Move Iterator to record specified by \a key
3603 * \param di osd iterator
3604 * \param key key for index
3606 * \retval +ve di points to record with least key not larger than key
3607 * \retval 0 di points to exact matched key
3608 * \retval -ve failure
3611 static int osd_it_iam_get(const struct lu_env *env,
3612 struct dt_it *di, const struct dt_key *key)
3614 struct osd_it_iam *it = (struct osd_it_iam *)di;
3616 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3622 * \param di osd iterator
3625 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3627 struct osd_it_iam *it = (struct osd_it_iam *)di;
3629 iam_it_put(&it->oi_it);
3633 * Move iterator by one record
3635 * \param di osd iterator
3637 * \retval +1 end of container reached
3639 * \retval -ve failure
3642 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3644 struct osd_it_iam *it = (struct osd_it_iam *)di;
3646 return iam_it_next(&it->oi_it);
3650 * Return pointer to the key under iterator.
3653 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3654 const struct dt_it *di)
3656 struct osd_it_iam *it = (struct osd_it_iam *)di;
3658 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3662 * Return size of key under iterator (in bytes)
3665 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3667 struct osd_it_iam *it = (struct osd_it_iam *)di;
3669 return iam_it_key_size(&it->oi_it);
3672 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3673 int len, __u16 type)
3675 struct luda_type *lt;
3676 const unsigned align = sizeof(struct luda_type) - 1;
3678 /* check if file type is required */
3679 if (attr & LUDA_TYPE) {
3680 len = (len + align) & ~align;
3682 lt = (void *) ent->lde_name + len;
3683 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3684 ent->lde_attrs |= LUDA_TYPE;
3687 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3691 * build lu direct from backend fs dirent.
3694 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3695 struct lu_fid *fid, __u64 offset,
3696 char *name, __u16 namelen,
3697 __u16 type, __u32 attr)
3699 fid_cpu_to_le(&ent->lde_fid, fid);
3700 ent->lde_attrs = LUDA_FID;
3702 ent->lde_hash = cpu_to_le64(offset);
3703 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3705 strncpy(ent->lde_name, name, namelen);
3706 ent->lde_namelen = cpu_to_le16(namelen);
3708 /* append lustre attributes */
3709 osd_it_append_attrs(ent, attr, namelen, type);
3713 * Return pointer to the record under iterator.
3715 static int osd_it_iam_rec(const struct lu_env *env,
3716 const struct dt_it *di,
3717 struct dt_rec *dtrec, __u32 attr)
3719 struct osd_it_iam *it = (struct osd_it_iam *)di;
3720 struct osd_thread_info *info = osd_oti_get(env);
3723 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3724 const struct osd_fid_pack *rec;
3725 struct lu_fid *fid = &info->oti_fid;
3726 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3732 name = (char *)iam_it_key_get(&it->oi_it);
3734 RETURN(PTR_ERR(name));
3736 namelen = iam_it_key_size(&it->oi_it);
3738 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3740 RETURN(PTR_ERR(rec));
3742 rc = osd_fid_unpack(fid, rec);
3746 hash = iam_it_store(&it->oi_it);
3748 /* IAM does not store object type in IAM index (dir) */
3749 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3752 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3753 (struct iam_rec *)dtrec);
3760 * Returns cookie for current Iterator position.
3762 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3764 struct osd_it_iam *it = (struct osd_it_iam *)di;
3766 return iam_it_store(&it->oi_it);
3770 * Restore iterator from cookie.
3772 * \param di osd iterator
3773 * \param hash Iterator location cookie
3775 * \retval +ve di points to record with least key not larger than key.
3776 * \retval 0 di points to exact matched key
3777 * \retval -ve failure
3780 static int osd_it_iam_load(const struct lu_env *env,
3781 const struct dt_it *di, __u64 hash)
3783 struct osd_it_iam *it = (struct osd_it_iam *)di;
3785 return iam_it_load(&it->oi_it, hash);
3788 static const struct dt_index_operations osd_index_iam_ops = {
3789 .dio_lookup = osd_index_iam_lookup,
3790 .dio_declare_insert = osd_index_declare_iam_insert,
3791 .dio_insert = osd_index_iam_insert,
3792 .dio_declare_delete = osd_index_declare_iam_delete,
3793 .dio_delete = osd_index_iam_delete,
3795 .init = osd_it_iam_init,
3796 .fini = osd_it_iam_fini,
3797 .get = osd_it_iam_get,
3798 .put = osd_it_iam_put,
3799 .next = osd_it_iam_next,
3800 .key = osd_it_iam_key,
3801 .key_size = osd_it_iam_key_size,
3802 .rec = osd_it_iam_rec,
3803 .store = osd_it_iam_store,
3804 .load = osd_it_iam_load
3809 * Creates or initializes iterator context.
3811 * \retval struct osd_it_ea, iterator structure on success
3814 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3815 struct dt_object *dt,
3817 struct lustre_capa *capa)
3819 struct osd_object *obj = osd_dt_obj(dt);
3820 struct osd_thread_info *info = osd_oti_get(env);
3821 struct osd_it_ea *it = &info->oti_it_ea;
3822 struct lu_object *lo = &dt->do_lu;
3823 struct dentry *obj_dentry = &info->oti_it_dentry;
3825 LASSERT(lu_object_exists(lo));
3827 obj_dentry->d_inode = obj->oo_inode;
3828 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3829 obj_dentry->d_name.hash = 0;
3831 it->oie_rd_dirent = 0;
3832 it->oie_it_dirent = 0;
3833 it->oie_dirent = NULL;
3834 it->oie_buf = info->oti_it_ea_buf;
3836 it->oie_file.f_pos = 0;
3837 it->oie_file.f_dentry = obj_dentry;
3838 if (attr & LUDA_64BITHASH)
3839 it->oie_file.f_flags = O_64BITHASH;
3841 it->oie_file.f_flags = O_32BITHASH;
3842 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3843 it->oie_file.f_op = obj->oo_inode->i_fop;
3844 it->oie_file.private_data = NULL;
3846 RETURN((struct dt_it *) it);
3850 * Destroy or finishes iterator context.
3852 * \param di iterator structure to be destroyed
3854 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3856 struct osd_it_ea *it = (struct osd_it_ea *)di;
3857 struct osd_object *obj = it->oie_obj;
3858 struct inode *inode = obj->oo_inode;
3861 it->oie_file.f_op->release(inode, &it->oie_file);
3862 lu_object_put(env, &obj->oo_dt.do_lu);
3867 * It position the iterator at given key, so that next lookup continues from
3868 * that key Or it is similar to dio_it->load() but based on a key,
3869 * rather than file position.
3871 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3874 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3876 static int osd_it_ea_get(const struct lu_env *env,
3877 struct dt_it *di, const struct dt_key *key)
3879 struct osd_it_ea *it = (struct osd_it_ea *)di;
3882 LASSERT(((const char *)key)[0] == '\0');
3883 it->oie_file.f_pos = 0;
3884 it->oie_rd_dirent = 0;
3885 it->oie_it_dirent = 0;
3886 it->oie_dirent = NULL;
3894 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3899 * It is called internally by ->readdir(). It fills the
3900 * iterator's in-memory data structure with required
3901 * information i.e. name, namelen, rec_size etc.
3903 * \param buf in which information to be filled in.
3904 * \param name name of the file in given dir
3906 * \retval 0 on success
3907 * \retval 1 on buffer full
3909 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3910 loff_t offset, __u64 ino,
3913 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3914 struct osd_it_ea_dirent *ent = it->oie_dirent;
3915 struct lu_fid *fid = &ent->oied_fid;
3916 struct osd_fid_pack *rec;
3919 /* this should never happen */
3920 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3921 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3925 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3929 if (d_type & LDISKFS_DIRENT_LUFID) {
3930 rec = (struct osd_fid_pack*) (name + namelen + 1);
3932 if (osd_fid_unpack(fid, rec) != 0)
3935 d_type &= ~LDISKFS_DIRENT_LUFID;
3940 ent->oied_ino = ino;
3941 ent->oied_off = offset;
3942 ent->oied_namelen = namelen;
3943 ent->oied_type = d_type;
3945 memcpy(ent->oied_name, name, namelen);
3947 it->oie_rd_dirent++;
3948 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3953 * Calls ->readdir() to load a directory entry at a time
3954 * and stored it in iterator's in-memory data structure.
3956 * \param di iterator's in memory structure
3958 * \retval 0 on success
3959 * \retval -ve on error
3961 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3962 const struct dt_it *di)
3964 struct osd_it_ea *it = (struct osd_it_ea *)di;
3965 struct osd_object *obj = it->oie_obj;
3966 struct inode *inode = obj->oo_inode;
3967 struct htree_lock *hlock = NULL;
3971 it->oie_dirent = it->oie_buf;
3972 it->oie_rd_dirent = 0;
3974 if (obj->oo_hl_head != NULL) {
3975 hlock = osd_oti_get(env)->oti_hlock;
3976 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3977 inode, LDISKFS_HLOCK_READDIR);
3979 cfs_down_read(&obj->oo_ext_idx_sem);
3982 result = inode->i_fop->readdir(&it->oie_file, it,
3983 (filldir_t) osd_ldiskfs_filldir);
3986 ldiskfs_htree_unlock(hlock);
3988 cfs_up_read(&obj->oo_ext_idx_sem);
3990 if (it->oie_rd_dirent == 0) {
3993 it->oie_dirent = it->oie_buf;
3994 it->oie_it_dirent = 1;
4001 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4002 * to load a directory entry at a time and stored it in
4003 * iterator's in-memory data structure.
4005 * \param di iterator's in memory structure
4007 * \retval +ve iterator reached to end
4008 * \retval 0 iterator not reached to end
4009 * \retval -ve on error
4011 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4013 struct osd_it_ea *it = (struct osd_it_ea *)di;
4018 if (it->oie_it_dirent < it->oie_rd_dirent) {
4020 (void *) it->oie_dirent +
4021 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4022 it->oie_dirent->oied_namelen);
4023 it->oie_it_dirent++;
4026 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
4029 rc = osd_ldiskfs_it_fill(env, di);
4036 * Returns the key at current position from iterator's in memory structure.
4038 * \param di iterator's in memory structure
4040 * \retval key i.e. struct dt_key on success
4042 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4043 const struct dt_it *di)
4045 struct osd_it_ea *it = (struct osd_it_ea *)di;
4047 return (struct dt_key *)it->oie_dirent->oied_name;
4051 * Returns the key's size at current position from iterator's in memory structure.
4053 * \param di iterator's in memory structure
4055 * \retval key_size i.e. struct dt_key on success
4057 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4059 struct osd_it_ea *it = (struct osd_it_ea *)di;
4061 return it->oie_dirent->oied_namelen;
4066 * Returns the value (i.e. fid/igif) at current position from iterator's
4067 * in memory structure.
4069 * \param di struct osd_it_ea, iterator's in memory structure
4070 * \param attr attr requested for dirent.
4071 * \param lde lustre dirent
4073 * \retval 0 no error and \param lde has correct lustre dirent.
4074 * \retval -ve on error
4076 static inline int osd_it_ea_rec(const struct lu_env *env,
4077 const struct dt_it *di,
4078 struct dt_rec *dtrec, __u32 attr)
4080 struct osd_it_ea *it = (struct osd_it_ea *)di;
4081 struct osd_object *obj = it->oie_obj;
4082 struct osd_device *dev = osd_obj2dev(obj);
4083 struct osd_scrub *scrub = &dev->od_scrub;
4084 struct scrub_file *sf = &scrub->os_file;
4085 struct osd_thread_info *oti = osd_oti_get(env);
4086 struct osd_idmap_cache *oic = &oti->oti_cache;
4087 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4088 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4089 __u32 ino = it->oie_dirent->oied_ino;
4093 if (!fid_is_sane(fid)) {
4094 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4098 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4101 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4102 it->oie_dirent->oied_name,
4103 it->oie_dirent->oied_namelen,
4104 it->oie_dirent->oied_type, attr);
4106 if (!fid_is_norm(fid))
4109 oic->oic_fid = *fid;
4110 if ((scrub->os_pos_current <= ino) &&
4111 (sf->sf_flags & SF_INCONSISTENT ||
4112 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4113 rc = osd_consistency_check(oti, dev, oic);
4119 * Returns a cookie for current position of the iterator head, so that
4120 * user can use this cookie to load/start the iterator next time.
4122 * \param di iterator's in memory structure
4124 * \retval cookie for current position, on success
4126 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4128 struct osd_it_ea *it = (struct osd_it_ea *)di;
4130 return it->oie_dirent->oied_off;
4134 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4135 * to load a directory entry at a time and stored it i inn,
4136 * in iterator's in-memory data structure.
4138 * \param di struct osd_it_ea, iterator's in memory structure
4140 * \retval +ve on success
4141 * \retval -ve on error
4143 static int osd_it_ea_load(const struct lu_env *env,
4144 const struct dt_it *di, __u64 hash)
4146 struct osd_it_ea *it = (struct osd_it_ea *)di;
4150 it->oie_file.f_pos = hash;
4152 rc = osd_ldiskfs_it_fill(env, di);
4160 * Index lookup function for interoperability mode (b11826).
4162 * \param key, key i.e. file name to be searched
4164 * \retval +ve, on success
4165 * \retval -ve, on error
4167 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4168 struct dt_rec *rec, const struct dt_key *key,
4169 struct lustre_capa *capa)
4171 struct osd_object *obj = osd_dt_obj(dt);
4176 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4177 LINVRNT(osd_invariant(obj));
4179 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4182 rc = osd_ea_lookup_rec(env, obj, rec, key);
4190 * Index and Iterator operations for interoperability
4191 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4193 static const struct dt_index_operations osd_index_ea_ops = {
4194 .dio_lookup = osd_index_ea_lookup,
4195 .dio_declare_insert = osd_index_declare_ea_insert,
4196 .dio_insert = osd_index_ea_insert,
4197 .dio_declare_delete = osd_index_declare_ea_delete,
4198 .dio_delete = osd_index_ea_delete,
4200 .init = osd_it_ea_init,
4201 .fini = osd_it_ea_fini,
4202 .get = osd_it_ea_get,
4203 .put = osd_it_ea_put,
4204 .next = osd_it_ea_next,
4205 .key = osd_it_ea_key,
4206 .key_size = osd_it_ea_key_size,
4207 .rec = osd_it_ea_rec,
4208 .store = osd_it_ea_store,
4209 .load = osd_it_ea_load
4213 static void *osd_key_init(const struct lu_context *ctx,
4214 struct lu_context_key *key)
4216 struct osd_thread_info *info;
4218 OBD_ALLOC_PTR(info);
4220 return ERR_PTR(-ENOMEM);
4222 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4223 if (info->oti_it_ea_buf == NULL)
4226 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4228 info->oti_hlock = ldiskfs_htree_lock_alloc();
4229 if (info->oti_hlock == NULL)
4235 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4238 return ERR_PTR(-ENOMEM);
4241 static void osd_key_fini(const struct lu_context *ctx,
4242 struct lu_context_key *key, void* data)
4244 struct osd_thread_info *info = data;
4246 if (info->oti_hlock != NULL)
4247 ldiskfs_htree_lock_free(info->oti_hlock);
4248 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4252 static void osd_key_exit(const struct lu_context *ctx,
4253 struct lu_context_key *key, void *data)
4255 struct osd_thread_info *info = data;
4257 LASSERT(info->oti_r_locks == 0);
4258 LASSERT(info->oti_w_locks == 0);
4259 LASSERT(info->oti_txns == 0);
4262 /* type constructor/destructor: osd_type_init, osd_type_fini */
4263 LU_TYPE_INIT_FINI(osd, &osd_key);
4265 struct lu_context_key osd_key = {
4266 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4267 .lct_init = osd_key_init,
4268 .lct_fini = osd_key_fini,
4269 .lct_exit = osd_key_exit
4273 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4274 const char *name, struct lu_device *next)
4276 struct osd_device *osd = osd_dev(d);
4278 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4279 return osd_procfs_init(osd, name);
4282 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4286 osd_scrub_cleanup(env, o);
4289 fsfilt_put_ops(o->od_fsops);
4293 /* shutdown quota slave instance associated with the device */
4294 if (o->od_quota_slave != NULL) {
4295 qsd_fini(env, o->od_quota_slave);
4296 o->od_quota_slave = NULL;
4302 static int osd_mount(const struct lu_env *env,
4303 struct osd_device *o, struct lustre_cfg *cfg)
4305 struct lustre_mount_info *lmi;
4306 const char *dev = lustre_cfg_string(cfg, 0);
4307 struct lustre_disk_data *ldd;
4308 struct lustre_sb_info *lsi;
4313 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4314 if (o->od_fsops == NULL) {
4315 CERROR("Can't find fsfilt_ldiskfs\n");
4319 if (o->od_mount != NULL) {
4320 CERROR("Already mounted (%s)\n", dev);
4325 lmi = server_get_mount(dev);
4327 CERROR("Cannot get mount info for %s!\n", dev);
4331 LASSERT(lmi != NULL);
4332 /* save lustre_mount_info in dt_device */
4334 o->od_mnt = lmi->lmi_mnt;
4336 lsi = s2lsi(lmi->lmi_sb);
4339 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4341 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", dev);
4345 if (ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
4346 rc = osd_compat_init(o);
4348 CERROR("%s: can't initialize compats: %d\n", dev, rc);
4354 static struct lu_device *osd_device_fini(const struct lu_env *env,
4355 struct lu_device *d)
4360 osd_compat_fini(osd_dev(d));
4362 shrink_dcache_sb(osd_sb(osd_dev(d)));
4363 osd_sync(env, lu2dt_dev(d));
4365 rc = osd_procfs_fini(osd_dev(d));
4367 CERROR("proc fini error %d \n", rc);
4368 RETURN (ERR_PTR(rc));
4371 if (osd_dev(d)->od_mount)
4372 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4373 osd_dev(d)->od_mount->lmi_mnt);
4374 osd_dev(d)->od_mount = NULL;
4379 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4380 struct lu_device_type *t,
4381 struct lustre_cfg *cfg)
4383 struct lu_device *l;
4384 struct osd_device *o;
4390 result = dt_device_init(&o->od_dt_dev, t);
4393 l->ld_ops = &osd_lu_ops;
4394 o->od_dt_dev.dd_ops = &osd_dt_ops;
4395 cfs_spin_lock_init(&o->od_osfs_lock);
4396 cfs_mutex_init(&o->od_otable_mutex);
4397 o->od_osfs_age = cfs_time_shift_64(-1000);
4398 o->od_capa_hash = init_capa_hash();
4399 if (o->od_capa_hash == NULL) {
4400 dt_device_fini(&o->od_dt_dev);
4401 l = ERR_PTR(-ENOMEM);
4404 l = ERR_PTR(result);
4409 l = ERR_PTR(-ENOMEM);
4413 static struct lu_device *osd_device_free(const struct lu_env *env,
4414 struct lu_device *d)
4416 struct osd_device *o = osd_dev(d);
4419 cleanup_capa_hash(o->od_capa_hash);
4420 dt_device_fini(&o->od_dt_dev);
4425 static int osd_process_config(const struct lu_env *env,
4426 struct lu_device *d, struct lustre_cfg *cfg)
4428 struct osd_device *o = osd_dev(d);
4432 switch(cfg->lcfg_command) {
4434 err = osd_mount(env, o, cfg);
4437 err = osd_shutdown(env, o);
4446 static int osd_recovery_complete(const struct lu_env *env,
4447 struct lu_device *d)
4452 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4453 struct lu_device *dev)
4455 struct osd_device *osd = osd_dev(dev);
4459 /* 1. setup scrub, including OI files initialization */
4460 result = osd_scrub_setup(env, osd);
4464 /* 2. setup quota slave instance */
4465 osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
4466 osd->od_proc_entry);
4467 if (IS_ERR(osd->od_quota_slave)) {
4468 result = PTR_ERR(osd->od_quota_slave);
4469 osd->od_quota_slave = NULL;
4473 if (!lu_device_is_md(pdev))
4476 /* 3. setup local objects */
4477 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4481 static const struct lu_object_operations osd_lu_obj_ops = {
4482 .loo_object_init = osd_object_init,
4483 .loo_object_delete = osd_object_delete,
4484 .loo_object_release = osd_object_release,
4485 .loo_object_free = osd_object_free,
4486 .loo_object_print = osd_object_print,
4487 .loo_object_invariant = osd_object_invariant
4490 const struct lu_device_operations osd_lu_ops = {
4491 .ldo_object_alloc = osd_object_alloc,
4492 .ldo_process_config = osd_process_config,
4493 .ldo_recovery_complete = osd_recovery_complete,
4494 .ldo_prepare = osd_prepare,
4497 static const struct lu_device_type_operations osd_device_type_ops = {
4498 .ldto_init = osd_type_init,
4499 .ldto_fini = osd_type_fini,
4501 .ldto_start = osd_type_start,
4502 .ldto_stop = osd_type_stop,
4504 .ldto_device_alloc = osd_device_alloc,
4505 .ldto_device_free = osd_device_free,
4507 .ldto_device_init = osd_device_init,
4508 .ldto_device_fini = osd_device_fini
4511 static struct lu_device_type osd_device_type = {
4512 .ldt_tags = LU_DEVICE_DT,
4513 .ldt_name = LUSTRE_OSD_NAME,
4514 .ldt_ops = &osd_device_type_ops,
4515 .ldt_ctx_tags = LCT_LOCAL,
4519 * lprocfs legacy support.
4521 static struct obd_ops osd_obd_device_ops = {
4522 .o_owner = THIS_MODULE
4525 static int __init osd_mod_init(void)
4527 struct lprocfs_static_vars lvars;
4530 lprocfs_osd_init_vars(&lvars);
4531 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4532 LUSTRE_OSD_NAME, &osd_device_type);
4535 static void __exit osd_mod_exit(void)
4537 class_unregister_type(LUSTRE_OSD_NAME);
4540 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4541 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4542 MODULE_LICENSE("GPL");
4544 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);