4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
60 * struct OBD_{ALLOC,FREE}*()
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
68 #include <lustre_fid.h>
70 #include "osd_internal.h"
73 /* llo_* api support */
74 #include <md_object.h>
75 /* dt_acct_features */
78 #ifdef HAVE_LDISKFS_PDO
80 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
81 "ldiskfs with parallel directory operations");
86 static const char dot[] = ".";
87 static const char dotdot[] = "..";
88 static const char remote_obj_dir[] = "REM_OBJ_DIR";
90 static const struct lu_object_operations osd_lu_obj_ops;
91 static const struct dt_object_operations osd_obj_ops;
92 static const struct dt_object_operations osd_obj_ea_ops;
93 static const struct dt_object_operations osd_obj_otable_it_ops;
94 static const struct dt_index_operations osd_index_iam_ops;
95 static const struct dt_index_operations osd_index_ea_ops;
97 static int osd_has_index(const struct osd_object *obj)
99 return obj->oo_dt.do_index_ops != NULL;
102 static int osd_object_invariant(const struct lu_object *l)
104 return osd_invariant(osd_obj(l));
107 #ifdef HAVE_QUOTA_SUPPORT
109 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
111 struct md_ucred *uc = md_ucred(env);
116 save->oc_uid = current_fsuid();
117 save->oc_gid = current_fsgid();
118 save->oc_cap = current_cap();
119 if ((tc = prepare_creds())) {
120 tc->fsuid = uc->mu_fsuid;
121 tc->fsgid = uc->mu_fsgid;
124 /* XXX not suboptimal */
125 cfs_curproc_cap_unpack(uc->mu_cap);
129 osd_pop_ctxt(struct osd_ctxt *save)
133 if ((tc = prepare_creds())) {
134 tc->fsuid = save->oc_uid;
135 tc->fsgid = save->oc_gid;
136 tc->cap_effective = save->oc_cap;
143 * Concurrency: doesn't matter
145 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
147 return osd_oti_get(env)->oti_r_locks > 0;
151 * Concurrency: doesn't matter
153 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
155 struct osd_thread_info *oti = osd_oti_get(env);
156 return oti->oti_w_locks > 0 && o->oo_owner == env;
160 * Concurrency: doesn't access mutable data
162 static int osd_root_get(const struct lu_env *env,
163 struct dt_device *dev, struct lu_fid *f)
165 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
169 static inline int osd_qid_type(struct osd_thandle *oh, int i)
171 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
174 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
176 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
179 void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
180 int type, uid_t id, struct inode *inode)
183 int i, allocated = 0;
184 struct osd_object *obj;
188 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
191 /* id entry is allocated in the quota file */
192 if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
195 for (i = 0; i < oh->ot_id_cnt; i++) {
196 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
200 if (unlikely(i >= OSD_MAX_UGID_CNT)) {
201 CERROR("more than %d uid/gids for a transaction?\n", i);
205 oh->ot_id_array[i] = id;
206 osd_qid_set_type(oh, i, type);
208 obj = osd_dt_obj(dt);
209 oh->ot_credits += (allocated || id == 0) ?
210 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
215 * OSD object methods.
219 * Concurrency: no concurrent access is possible that early in object
222 static struct lu_object *osd_object_alloc(const struct lu_env *env,
223 const struct lu_object_header *hdr,
226 struct osd_object *mo;
232 l = &mo->oo_dt.do_lu;
233 dt_object_init(&mo->oo_dt, NULL, d);
234 if (osd_dev(d)->od_iop_mode)
235 mo->oo_dt.do_ops = &osd_obj_ea_ops;
237 mo->oo_dt.do_ops = &osd_obj_ops;
239 l->lo_ops = &osd_lu_obj_ops;
240 cfs_init_rwsem(&mo->oo_sem);
241 cfs_init_rwsem(&mo->oo_ext_idx_sem);
242 cfs_spin_lock_init(&mo->oo_guard);
249 static int osd_get_lma(struct inode *inode, struct dentry *dentry,
250 struct lustre_mdt_attrs *lma)
254 dentry->d_inode = inode;
255 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
258 /* Check LMA compatibility */
259 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
260 CWARN("%.16s: unsupported incompat LMA feature(s) "
262 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
263 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
267 lustre_lma_swab(lma);
270 } else if (rc == 0) {
278 * retrieve object from backend ext fs.
280 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
281 struct osd_inode_id *id)
283 struct inode *inode = NULL;
285 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
287 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
288 id->oii_ino, PTR_ERR(inode));
289 } else if (id->oii_gen != OSD_OII_NOGEN &&
290 inode->i_generation != id->oii_gen) {
291 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
293 id->oii_ino, id->oii_gen, inode->i_generation);
295 inode = ERR_PTR(-ESTALE);
296 } else if (inode->i_nlink == 0) {
297 /* due to parallel readdir and unlink,
298 * we can have dead inode here. */
299 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
300 make_bad_inode(inode);
302 inode = ERR_PTR(-ESTALE);
303 } else if (is_bad_inode(inode)) {
304 CWARN("%.16s: bad inode: ino = %u\n",
305 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
307 inode = ERR_PTR(-ENOENT);
309 if (id->oii_gen == OSD_OII_NOGEN)
310 osd_id_gen(id, inode->i_ino, inode->i_generation);
312 /* Do not update file c/mtime in ldiskfs.
313 * NB: we don't have any lock to protect this because we don't
314 * have reference on osd_object now, but contention with
315 * another lookup + attr_set can't happen in the tiny window
316 * between if (...) and set S_NOCMTIME. */
317 if (!(inode->i_flags & S_NOCMTIME))
318 inode->i_flags |= S_NOCMTIME;
323 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
324 struct osd_inode_id *id, struct lu_fid *fid)
326 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
330 inode = osd_iget(info, dev, id);
334 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
336 *fid = lma->lma_self_fid;
337 } else if (rc == -ENODATA) {
338 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
346 static struct inode *
347 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
348 struct osd_inode_id *id, const struct lu_fid *fid)
350 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
354 inode = osd_iget(info, dev, id);
358 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
360 if (rc == -ENODATA) {
361 CDEBUG(D_LFSCK, "inconsistent obj: NULL, %lu, "DFID"\n",
362 inode->i_ino, PFID(fid));
369 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
370 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
371 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
373 return ERR_PTR(EREMCHG);
378 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
379 const struct lu_fid *fid,
380 const struct lu_object_conf *conf)
382 struct osd_thread_info *info;
383 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
384 struct osd_device *dev;
385 struct osd_idmap_cache *oic;
386 struct osd_inode_id *id;
388 struct osd_scrub *scrub;
389 struct scrub_file *sf;
394 LINVRNT(osd_invariant(obj));
395 LASSERT(obj->oo_inode == NULL);
396 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
399 scrub = &dev->od_scrub;
400 sf = &scrub->os_file;
401 info = osd_oti_get(env);
403 oic = &info->oti_cache;
406 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
409 if (fid_is_norm(fid)) {
410 /* Search order: 1. per-thread cache. */
411 if (lu_fid_eq(fid, &oic->oic_fid)) {
413 } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
414 /* Search order: 2. OI scrub pending list. */
415 result = osd_oii_lookup(dev, fid, id);
420 if (sf->sf_flags & SF_INCONSISTENT)
424 fid_zero(&oic->oic_fid);
425 /* Search order: 3. OI files. */
426 result = osd_oi_lookup(info, dev, fid, id);
427 if (result != 0 && result != -ENOENT)
430 /* If fid wasn't found in oi, inode-less object is created,
431 * for which lu_object_exists() returns false. This is used
432 * in a (frequent) case when objects are created as locking
433 * anchors or place holders for objects yet to be created. */
434 if (conf != NULL && conf->loc_flags & LOC_F_NEW) {
435 if (unlikely(result == 0))
436 GOTO(out, result = -EEXIST);
438 GOTO(out, result = 0);
441 if (result == -ENOENT) {
442 if (!fid_is_norm(fid) ||
443 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
445 GOTO(out, result = 0);
452 inode = osd_iget(info, dev, id);
454 inode = osd_iget_verify(info, dev, id, fid);
456 result = PTR_ERR(inode);
457 if (result == -ENOENT || result == -ESTALE) {
459 } else if (result == -EREMCHG) {
462 if (thread_is_running(&scrub->os_thread)) {
463 result = -EINPROGRESS;
464 } else if (!scrub->os_no_scrub) {
465 result = osd_scrub_start(dev);
466 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
467 "for "DFID", rc = %d [1]\n",
468 LDISKFS_SB(osd_sb(dev))->s_es->\
469 s_volume_name,PFID(fid), result);
470 if (result == 0 || result == -EALREADY)
471 result = -EINPROGRESS;
480 obj->oo_inode = inode;
481 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
482 if (dev->od_iop_mode) {
483 obj->oo_compat_dot_created = 1;
484 obj->oo_compat_dotdot_created = 1;
487 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
488 GOTO(out, result = 0);
490 LASSERT(obj->oo_hl_head == NULL);
491 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
492 if (obj->oo_hl_head == NULL) {
493 obj->oo_inode = NULL;
495 GOTO(out, result = -ENOMEM);
497 GOTO(out, result = 0);
500 LINVRNT(osd_invariant(obj));
505 * Concurrency: shouldn't matter.
507 static void osd_object_init0(struct osd_object *obj)
509 LASSERT(obj->oo_inode != NULL);
510 obj->oo_dt.do_body_ops = &osd_body_ops;
511 obj->oo_dt.do_lu.lo_header->loh_attr |=
512 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
516 * Concurrency: no concurrent access is possible that early in object
519 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
520 const struct lu_object_conf *conf)
522 struct osd_object *obj = osd_obj(l);
525 LINVRNT(osd_invariant(obj));
527 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
528 obj->oo_dt.do_body_ops = &osd_body_ops_new;
530 if (obj->oo_inode != NULL) {
531 osd_object_init0(obj);
532 } else if (fid_is_otable_it(&l->lo_header->loh_fid)) {
533 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
534 /* LFSCK iterator object is special without inode */
535 l->lo_header->loh_attr |= LOHA_EXISTS;
538 LINVRNT(osd_invariant(obj));
543 * Concurrency: no concurrent access is possible that late in object
546 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
548 struct osd_object *obj = osd_obj(l);
550 LINVRNT(osd_invariant(obj));
552 dt_object_fini(&obj->oo_dt);
553 if (obj->oo_hl_head != NULL)
554 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
559 * Concurrency: no concurrent access is possible that late in object
562 static void osd_index_fini(struct osd_object *o)
564 struct iam_container *bag;
566 if (o->oo_dir != NULL) {
567 bag = &o->oo_dir->od_container;
568 if (o->oo_inode != NULL) {
569 if (bag->ic_object == o->oo_inode)
570 iam_container_fini(bag);
572 OBD_FREE_PTR(o->oo_dir);
578 * Concurrency: no concurrent access is possible that late in object
579 * life-cycle (for all existing callers, that is. New callers have to provide
580 * their own locking.)
582 static int osd_inode_unlinked(const struct inode *inode)
584 return inode->i_nlink == 0;
588 OSD_TXN_OI_DELETE_CREDITS = 20,
589 OSD_TXN_INODE_DELETE_CREDITS = 20
596 #if OSD_THANDLE_STATS
598 * Set time when the handle is allocated
600 static void osd_th_alloced(struct osd_thandle *oth)
602 oth->oth_alloced = cfs_time_current();
606 * Set time when the handle started
608 static void osd_th_started(struct osd_thandle *oth)
610 oth->oth_started = cfs_time_current();
614 * Helper function to convert time interval to microseconds packed in
615 * long int (default time units for the counter in "stats" initialized
616 * by lu_time_init() )
618 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
622 cfs_duration_usec(cfs_time_sub(end, start), &val);
623 return val.tv_sec * 1000000 + val.tv_usec;
627 * Check whether the we deal with this handle for too long.
629 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
630 cfs_time_t alloced, cfs_time_t started,
633 cfs_time_t now = cfs_time_current();
635 LASSERT(dev != NULL);
637 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
638 interval_to_usec(alloced, started));
639 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
640 interval_to_usec(started, closed));
641 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
642 interval_to_usec(closed, now));
644 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
645 CWARN("transaction handle %p was open for too long: "
647 "alloced "CFS_TIME_T" ,"
648 "started "CFS_TIME_T" ,"
649 "closed "CFS_TIME_T"\n",
650 oth, now, alloced, started, closed);
651 libcfs_debug_dumpstack(NULL);
655 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
657 cfs_time_t __closed = cfs_time_current(); \
658 cfs_time_t __alloced = oth->oth_alloced; \
659 cfs_time_t __started = oth->oth_started; \
662 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
665 #else /* OSD_THANDLE_STATS */
667 #define osd_th_alloced(h) do {} while(0)
668 #define osd_th_started(h) do {} while(0)
669 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
671 #endif /* OSD_THANDLE_STATS */
674 * Concurrency: doesn't access mutable data.
676 static int osd_param_is_sane(const struct osd_device *dev,
677 const struct thandle *th)
679 struct osd_thandle *oh;
680 oh = container_of0(th, struct osd_thandle, ot_super);
681 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
685 * Concurrency: shouldn't matter.
687 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
688 static void osd_trans_commit_cb(struct super_block *sb,
689 struct journal_callback *jcb, int error)
691 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
694 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
695 struct thandle *th = &oh->ot_super;
696 struct lu_device *lud = &th->th_dev->dd_lu_dev;
697 struct dt_txn_commit_cb *dcb, *tmp;
699 LASSERT(oh->ot_handle == NULL);
702 CERROR("transaction @0x%p commit error: %d\n", th, error);
704 dt_txn_hook_commit(th);
706 /* call per-transaction callbacks if any */
707 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
708 cfs_list_del_init(&dcb->dcb_linkage);
709 dcb->dcb_func(NULL, th, dcb, error);
712 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
716 lu_context_exit(&th->th_ctx);
717 lu_context_fini(&th->th_ctx);
721 static struct thandle *osd_trans_create(const struct lu_env *env,
724 struct osd_thread_info *oti = osd_oti_get(env);
725 struct osd_iobuf *iobuf = &oti->oti_iobuf;
726 struct osd_thandle *oh;
730 /* on pending IO in this thread should left from prev. request */
731 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
733 th = ERR_PTR(-ENOMEM);
734 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
739 th->th_tags = LCT_TX_HANDLE;
741 oti->oti_dev = osd_dt_dev(d);
742 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
749 * Concurrency: shouldn't matter.
751 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
754 struct osd_thread_info *oti = osd_oti_get(env);
755 struct osd_device *dev = osd_dt_dev(d);
757 struct osd_thandle *oh;
762 LASSERT(current->journal_info == NULL);
764 oh = container_of0(th, struct osd_thandle, ot_super);
766 LASSERT(oh->ot_handle == NULL);
768 rc = dt_txn_hook_start(env, d, th);
772 if (!osd_param_is_sane(dev, th)) {
773 CWARN("%.16s: too many transaction credits (%d > %d)\n",
774 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
776 osd_journal(dev)->j_max_transaction_buffers);
777 /* XXX Limit the credits to 'max_transaction_buffers', and
778 * let the underlying filesystem to catch the error if
779 * we really need so many credits.
781 * This should be removed when we can calculate the
782 * credits precisely. */
783 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
784 #ifdef OSD_TRACK_DECLARES
785 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
786 oh->ot_declare_attr_set, oh->ot_declare_punch,
787 oh->ot_declare_xattr_set);
788 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
789 oh->ot_declare_create, oh->ot_declare_ref_add,
790 oh->ot_declare_ref_del, oh->ot_declare_write);
791 CERROR(" insert: %d, delete: %d, destroy: %d\n",
792 oh->ot_declare_insert, oh->ot_declare_delete,
793 oh->ot_declare_destroy);
798 * XXX temporary stuff. Some abstraction layer should
801 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
805 LASSERT(oti->oti_txns == 0);
806 lu_context_init(&th->th_ctx, th->th_tags);
807 lu_context_enter(&th->th_ctx);
809 lu_device_get(&d->dd_lu_dev);
810 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
814 * XXX: current rule is that we first start tx,
815 * then lock object(s), but we can't use
816 * this rule for data (due to locking specifics
817 * in ldiskfs). also in long-term we'd like to
818 * use usually-used (locks;tx) ordering. so,
819 * UGLY thing is that we'll use one ordering for
820 * data (ofd) and reverse ordering for metadata
821 * (mdd). then at some point we'll fix the latter
823 if (lu_device_is_md(&d->dd_lu_dev)) {
824 LASSERT(oti->oti_r_locks == 0);
825 LASSERT(oti->oti_w_locks == 0);
838 * Concurrency: shouldn't matter.
840 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
843 struct osd_thandle *oh;
844 struct osd_thread_info *oti = osd_oti_get(env);
845 struct osd_iobuf *iobuf = &oti->oti_iobuf;
849 oh = container_of0(th, struct osd_thandle, ot_super);
851 if (oh->ot_handle != NULL) {
852 handle_t *hdl = oh->ot_handle;
854 hdl->h_sync = th->th_sync;
857 * add commit callback
858 * notice we don't do this in osd_trans_start()
859 * as underlying transaction can change during truncate
861 osd_journal_callback_set(hdl, osd_trans_commit_cb,
864 LASSERT(oti->oti_txns == 1);
867 * XXX: current rule is that we first start tx,
868 * then lock object(s), but we can't use
869 * this rule for data (due to locking specifics
870 * in ldiskfs). also in long-term we'd like to
871 * use usually-used (locks;tx) ordering. so,
872 * UGLY thing is that we'll use one ordering for
873 * data (ofd) and reverse ordering for metadata
874 * (mdd). then at some point we'll fix the latter
876 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
877 LASSERT(oti->oti_r_locks == 0);
878 LASSERT(oti->oti_w_locks == 0);
880 rc = dt_txn_hook_stop(env, th);
882 CERROR("Failure in transaction hook: %d\n", rc);
883 oh->ot_handle = NULL;
884 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
885 rc = ldiskfs_journal_stop(hdl));
887 CERROR("Failure to stop transaction: %d\n", rc);
892 /* as we want IO to journal and data IO be concurrent, we don't block
893 * awaiting data IO completion in osd_do_bio(), instead we wait here
894 * once transaction is submitted to the journal. all reqular requests
895 * don't do direct IO (except read/write), thus this wait_event becomes
898 * IMPORTANT: we have to wait till any IO submited by the thread is
899 * completed otherwise iobuf may be corrupted by different request
901 cfs_wait_event(iobuf->dr_wait,
902 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
904 rc = iobuf->dr_error;
909 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
911 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
914 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
920 * Called just before object is freed. Releases all resources except for
921 * object itself (that is released by osd_object_free()).
923 * Concurrency: no concurrent access is possible that late in object
926 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
928 struct osd_object *obj = osd_obj(l);
929 struct inode *inode = obj->oo_inode;
931 LINVRNT(osd_invariant(obj));
934 * If object is unlinked remove fid->ino mapping from object index.
940 obj->oo_inode = NULL;
945 * Concurrency: ->loo_object_release() is called under site spin-lock.
947 static void osd_object_release(const struct lu_env *env,
953 * Concurrency: shouldn't matter.
955 static int osd_object_print(const struct lu_env *env, void *cookie,
956 lu_printer_t p, const struct lu_object *l)
958 struct osd_object *o = osd_obj(l);
961 if (o->oo_dir != NULL)
962 d = o->oo_dir->od_container.ic_descr;
965 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
967 o->oo_inode ? o->oo_inode->i_ino : 0UL,
968 o->oo_inode ? o->oo_inode->i_generation : 0,
969 d ? d->id_ops->id_name : "plain");
973 * Concurrency: shouldn't matter.
975 int osd_statfs(const struct lu_env *env, struct dt_device *d,
976 struct obd_statfs *sfs)
978 struct osd_device *osd = osd_dt_dev(d);
979 struct super_block *sb = osd_sb(osd);
980 struct kstatfs *ksfs;
983 /* osd_lproc.c call this without env, allocate ksfs for that case */
984 if (unlikely(env == NULL)) {
989 ksfs = &osd_oti_get(env)->oti_ksfs;
992 cfs_spin_lock(&osd->od_osfs_lock);
994 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
995 result = ll_do_statfs(sb, ksfs);
996 if (likely(result == 0)) { /* N.B. statfs can't really fail */
997 osd->od_osfs_age = cfs_time_current_64();
998 statfs_pack(&osd->od_statfs, ksfs);
1002 if (likely(result == 0))
1003 *sfs = osd->od_statfs;
1004 cfs_spin_unlock(&osd->od_osfs_lock);
1006 if (unlikely(env == NULL))
1013 * Concurrency: doesn't access mutable data.
1015 static void osd_conf_get(const struct lu_env *env,
1016 const struct dt_device *dev,
1017 struct dt_device_param *param)
1019 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1022 * XXX should be taken from not-yet-existing fs abstraction layer.
1024 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1025 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1026 param->ddp_block_shift = sb->s_blocksize_bits;
1027 param->ddp_mntopts = 0;
1028 if (test_opt(sb, XATTR_USER))
1029 param->ddp_mntopts |= MNTOPT_USERXATTR;
1030 if (test_opt(sb, POSIX_ACL))
1031 param->ddp_mntopts |= MNTOPT_ACL;
1033 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1034 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1035 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1038 param->ddp_max_ea_size = sb->s_blocksize;
1043 * Helper function to get and fill the buffer with input values.
1045 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1049 buf = &osd_oti_get(env)->oti_buf;
1056 * Concurrency: shouldn't matter.
1058 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1060 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1061 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1065 * Start commit for OSD device.
1067 * An implementation of dt_commit_async method for OSD device.
1068 * Asychronously starts underlayng fs sync and thereby a transaction
1071 * \param env environment
1072 * \param d dt device
1074 * \see dt_device_operations
1076 static int osd_commit_async(const struct lu_env *env,
1077 struct dt_device *d)
1079 struct super_block *s = osd_sb(osd_dt_dev(d));
1082 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1083 RETURN(s->s_op->sync_fs(s, 0));
1087 * Concurrency: shouldn't matter.
1090 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1092 struct super_block *sb = osd_sb(osd_dt_dev(d));
1096 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1098 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1103 * Concurrency: serialization provided by callers.
1105 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1106 int mode, unsigned long timeout, __u32 alg,
1107 struct lustre_capa_key *keys)
1109 struct osd_device *dev = osd_dt_dev(d);
1112 dev->od_fl_capa = mode;
1113 dev->od_capa_timeout = timeout;
1114 dev->od_capa_alg = alg;
1115 dev->od_capa_keys = keys;
1120 * Concurrency: serialization provided by callers.
1122 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1123 struct dt_quota_ctxt *ctxt, void *data)
1125 struct obd_device *obd = (void *)ctxt;
1126 struct vfsmount *mnt = (struct vfsmount *)data;
1129 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1130 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1131 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1132 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1133 obd->obd_lvfs_ctxt.fs = get_ds();
1139 * Note: we do not count into QUOTA here.
1140 * If we mount with --data_journal we may need more.
1142 const int osd_dto_credits_noquota[DTO_NR] = {
1145 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1146 * SINGLEDATA_TRANS_BLOCKS(8)
1147 * XXX Note: maybe iam need more, since iam have more level than
1150 [DTO_INDEX_INSERT] = 16,
1151 [DTO_INDEX_DELETE] = 16,
1155 [DTO_INDEX_UPDATE] = 16,
1157 * Create a object. The same as create object in EXT3.
1158 * DATA_TRANS_BLOCKS(14) +
1159 * INDEX_EXTRA_BLOCKS(8) +
1160 * 3(inode bits, groups, GDT)
1162 [DTO_OBJECT_CREATE] = 25,
1164 * XXX: real credits to be fixed
1166 [DTO_OBJECT_DELETE] = 25,
1168 * Attr set credits (inode)
1170 [DTO_ATTR_SET_BASE] = 1,
1172 * Xattr set. The same as xattr of EXT3.
1173 * DATA_TRANS_BLOCKS(14)
1174 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1175 * are also counted in. Do not know why?
1177 [DTO_XATTR_SET] = 14,
1180 * credits for inode change during write.
1182 [DTO_WRITE_BASE] = 3,
1184 * credits for single block write.
1186 [DTO_WRITE_BLOCK] = 14,
1188 * Attr set credits for chown.
1189 * This is extra credits for setattr, and it is null without quota
1191 [DTO_ATTR_SET_CHOWN]= 0
1194 static const struct dt_device_operations osd_dt_ops = {
1195 .dt_root_get = osd_root_get,
1196 .dt_statfs = osd_statfs,
1197 .dt_trans_create = osd_trans_create,
1198 .dt_trans_start = osd_trans_start,
1199 .dt_trans_stop = osd_trans_stop,
1200 .dt_trans_cb_add = osd_trans_cb_add,
1201 .dt_conf_get = osd_conf_get,
1202 .dt_sync = osd_sync,
1204 .dt_commit_async = osd_commit_async,
1205 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1206 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1209 static void osd_object_read_lock(const struct lu_env *env,
1210 struct dt_object *dt, unsigned role)
1212 struct osd_object *obj = osd_dt_obj(dt);
1213 struct osd_thread_info *oti = osd_oti_get(env);
1215 LINVRNT(osd_invariant(obj));
1217 LASSERT(obj->oo_owner != env);
1218 cfs_down_read_nested(&obj->oo_sem, role);
1220 LASSERT(obj->oo_owner == NULL);
1224 static void osd_object_write_lock(const struct lu_env *env,
1225 struct dt_object *dt, unsigned role)
1227 struct osd_object *obj = osd_dt_obj(dt);
1228 struct osd_thread_info *oti = osd_oti_get(env);
1230 LINVRNT(osd_invariant(obj));
1232 LASSERT(obj->oo_owner != env);
1233 cfs_down_write_nested(&obj->oo_sem, role);
1235 LASSERT(obj->oo_owner == NULL);
1236 obj->oo_owner = env;
1240 static void osd_object_read_unlock(const struct lu_env *env,
1241 struct dt_object *dt)
1243 struct osd_object *obj = osd_dt_obj(dt);
1244 struct osd_thread_info *oti = osd_oti_get(env);
1246 LINVRNT(osd_invariant(obj));
1248 LASSERT(oti->oti_r_locks > 0);
1250 cfs_up_read(&obj->oo_sem);
1253 static void osd_object_write_unlock(const struct lu_env *env,
1254 struct dt_object *dt)
1256 struct osd_object *obj = osd_dt_obj(dt);
1257 struct osd_thread_info *oti = osd_oti_get(env);
1259 LINVRNT(osd_invariant(obj));
1261 LASSERT(obj->oo_owner == env);
1262 LASSERT(oti->oti_w_locks > 0);
1264 obj->oo_owner = NULL;
1265 cfs_up_write(&obj->oo_sem);
1268 static int osd_object_write_locked(const struct lu_env *env,
1269 struct dt_object *dt)
1271 struct osd_object *obj = osd_dt_obj(dt);
1273 LINVRNT(osd_invariant(obj));
1275 return obj->oo_owner == env;
1278 static int capa_is_sane(const struct lu_env *env,
1279 struct osd_device *dev,
1280 struct lustre_capa *capa,
1281 struct lustre_capa_key *keys)
1283 struct osd_thread_info *oti = osd_oti_get(env);
1284 struct lustre_capa *tcapa = &oti->oti_capa;
1285 struct obd_capa *oc;
1289 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1291 if (capa_is_expired(oc)) {
1292 DEBUG_CAPA(D_ERROR, capa, "expired");
1299 if (capa_is_expired_sec(capa)) {
1300 DEBUG_CAPA(D_ERROR, capa, "expired");
1304 cfs_spin_lock(&capa_lock);
1305 for (i = 0; i < 2; i++) {
1306 if (keys[i].lk_keyid == capa->lc_keyid) {
1307 oti->oti_capa_key = keys[i];
1311 cfs_spin_unlock(&capa_lock);
1314 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1318 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1322 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1323 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1327 oc = capa_add(dev->od_capa_hash, capa);
1333 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1334 struct lustre_capa *capa, __u64 opc)
1336 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1337 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1338 struct md_capainfo *ci;
1341 if (!dev->od_fl_capa)
1344 if (capa == BYPASS_CAPA)
1347 ci = md_capainfo(env);
1351 if (ci->mc_auth == LC_ID_NONE)
1355 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1359 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1360 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1365 if (!capa_opc_supported(capa, opc)) {
1366 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1370 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1371 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1378 static struct timespec *osd_inode_time(const struct lu_env *env,
1379 struct inode *inode, __u64 seconds)
1381 struct osd_thread_info *oti = osd_oti_get(env);
1382 struct timespec *t = &oti->oti_time;
1384 t->tv_sec = seconds;
1386 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1391 static void osd_inode_getattr(const struct lu_env *env,
1392 struct inode *inode, struct lu_attr *attr)
1394 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1395 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1396 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1398 attr->la_atime = LTIME_S(inode->i_atime);
1399 attr->la_mtime = LTIME_S(inode->i_mtime);
1400 attr->la_ctime = LTIME_S(inode->i_ctime);
1401 attr->la_mode = inode->i_mode;
1402 attr->la_size = i_size_read(inode);
1403 attr->la_blocks = inode->i_blocks;
1404 attr->la_uid = inode->i_uid;
1405 attr->la_gid = inode->i_gid;
1406 attr->la_flags = LDISKFS_I(inode)->i_flags;
1407 attr->la_nlink = inode->i_nlink;
1408 attr->la_rdev = inode->i_rdev;
1409 attr->la_blksize = ll_inode_blksize(inode);
1410 attr->la_blkbits = inode->i_blkbits;
1413 static int osd_attr_get(const struct lu_env *env,
1414 struct dt_object *dt,
1415 struct lu_attr *attr,
1416 struct lustre_capa *capa)
1418 struct osd_object *obj = osd_dt_obj(dt);
1420 LASSERT(dt_object_exists(dt));
1421 LINVRNT(osd_invariant(obj));
1423 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1426 cfs_spin_lock(&obj->oo_guard);
1427 osd_inode_getattr(env, obj->oo_inode, attr);
1428 cfs_spin_unlock(&obj->oo_guard);
1432 static int osd_declare_attr_set(const struct lu_env *env,
1433 struct dt_object *dt,
1434 const struct lu_attr *attr,
1435 struct thandle *handle)
1437 struct osd_thandle *oh;
1438 struct osd_object *obj;
1440 LASSERT(dt != NULL);
1441 LASSERT(handle != NULL);
1443 obj = osd_dt_obj(dt);
1444 LASSERT(osd_invariant(obj));
1446 oh = container_of0(handle, struct osd_thandle, ot_super);
1447 LASSERT(oh->ot_handle == NULL);
1449 OSD_DECLARE_OP(oh, attr_set);
1450 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1452 if (attr && attr->la_valid & LA_UID) {
1454 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1456 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1458 if (attr && attr->la_valid & LA_GID) {
1460 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1462 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1468 static int osd_inode_setattr(const struct lu_env *env,
1469 struct inode *inode, const struct lu_attr *attr)
1473 bits = attr->la_valid;
1475 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1477 if (bits & LA_ATIME)
1478 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1479 if (bits & LA_CTIME)
1480 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1481 if (bits & LA_MTIME)
1482 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1483 if (bits & LA_SIZE) {
1484 LDISKFS_I(inode)->i_disksize = attr->la_size;
1485 i_size_write(inode, attr->la_size);
1489 /* OSD should not change "i_blocks" which is used by quota.
1490 * "i_blocks" should be changed by ldiskfs only. */
1491 if (bits & LA_BLOCKS)
1492 inode->i_blocks = attr->la_blocks;
1495 inode->i_mode = (inode->i_mode & S_IFMT) |
1496 (attr->la_mode & ~S_IFMT);
1498 inode->i_uid = attr->la_uid;
1500 inode->i_gid = attr->la_gid;
1501 if (bits & LA_NLINK)
1502 inode->i_nlink = attr->la_nlink;
1504 inode->i_rdev = attr->la_rdev;
1506 if (bits & LA_FLAGS) {
1507 /* always keep S_NOCMTIME */
1508 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1514 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1516 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1517 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1522 if (attr->la_valid & LA_UID)
1523 iattr.ia_valid |= ATTR_UID;
1524 if (attr->la_valid & LA_GID)
1525 iattr.ia_valid |= ATTR_GID;
1526 iattr.ia_uid = attr->la_uid;
1527 iattr.ia_gid = attr->la_gid;
1529 rc = ll_vfs_dq_transfer(inode, &iattr);
1531 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1532 "enforcement enabled on the ldiskfs filesystem?",
1533 inode->i_sb->s_id, rc);
1540 static int osd_attr_set(const struct lu_env *env,
1541 struct dt_object *dt,
1542 const struct lu_attr *attr,
1543 struct thandle *handle,
1544 struct lustre_capa *capa)
1546 struct osd_object *obj = osd_dt_obj(dt);
1547 struct inode *inode;
1550 LASSERT(handle != NULL);
1551 LASSERT(dt_object_exists(dt));
1552 LASSERT(osd_invariant(obj));
1554 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1557 OSD_EXEC_OP(handle, attr_set);
1559 inode = obj->oo_inode;
1560 if (LDISKFS_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1561 LDISKFS_FEATURE_RO_COMPAT_QUOTA)) {
1562 rc = osd_quota_transfer(inode, attr);
1566 #ifdef HAVE_QUOTA_SUPPORT
1567 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1568 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1569 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1574 if (attr->la_valid & LA_UID)
1575 iattr.ia_valid |= ATTR_UID;
1576 if (attr->la_valid & LA_GID)
1577 iattr.ia_valid |= ATTR_GID;
1578 iattr.ia_uid = attr->la_uid;
1579 iattr.ia_gid = attr->la_gid;
1580 osd_push_ctxt(env, save);
1581 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1588 cfs_spin_lock(&obj->oo_guard);
1589 rc = osd_inode_setattr(env, inode, attr);
1590 cfs_spin_unlock(&obj->oo_guard);
1593 inode->i_sb->s_op->dirty_inode(inode);
1597 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1598 struct osd_object *obj,
1599 const char *name, const int namelen)
1601 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1604 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1606 struct dt_allocation_hint *hint,
1610 struct osd_device *osd = osd_obj2dev(obj);
1611 struct osd_thandle *oth;
1612 struct dt_object *parent = NULL;
1613 struct inode *inode;
1614 #ifdef HAVE_QUOTA_SUPPORT
1615 struct osd_ctxt *save = &info->oti_ctxt;
1618 LINVRNT(osd_invariant(obj));
1619 LASSERT(obj->oo_inode == NULL);
1620 LASSERT(obj->oo_hl_head == NULL);
1622 if (S_ISDIR(mode) && ldiskfs_pdo) {
1623 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1624 if (obj->oo_hl_head == NULL)
1628 oth = container_of(th, struct osd_thandle, ot_super);
1629 LASSERT(oth->ot_handle->h_transaction != NULL);
1631 if (hint && hint->dah_parent)
1632 parent = hint->dah_parent;
1634 #ifdef HAVE_QUOTA_SUPPORT
1635 osd_push_ctxt(info->oti_env, save);
1637 inode = ldiskfs_create_inode(oth->ot_handle,
1638 parent ? osd_dt_obj(parent)->oo_inode :
1639 osd_sb(osd)->s_root->d_inode,
1641 #ifdef HAVE_QUOTA_SUPPORT
1644 if (!IS_ERR(inode)) {
1645 /* Do not update file c/mtime in ldiskfs.
1646 * NB: don't need any lock because no contention at this
1648 inode->i_flags |= S_NOCMTIME;
1649 inode->i_state |= I_LUSTRE_NOSCRUB;
1650 obj->oo_inode = inode;
1653 if (obj->oo_hl_head != NULL) {
1654 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1655 obj->oo_hl_head = NULL;
1657 result = PTR_ERR(inode);
1659 LINVRNT(osd_invariant(obj));
1667 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1668 struct lu_attr *attr,
1669 struct dt_allocation_hint *hint,
1670 struct dt_object_format *dof,
1674 struct osd_thandle *oth;
1675 struct osd_device *osd = osd_obj2dev(obj);
1676 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1678 LASSERT(S_ISDIR(attr->la_mode));
1680 oth = container_of(th, struct osd_thandle, ot_super);
1681 LASSERT(oth->ot_handle->h_transaction != NULL);
1682 result = osd_mkfile(info, obj, mode, hint, th);
1683 if (result == 0 && osd->od_iop_mode == 0) {
1684 LASSERT(obj->oo_inode != NULL);
1686 * XXX uh-oh... call low-level iam function directly.
1689 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1690 sizeof (struct osd_fid_pack),
1696 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1697 struct lu_attr *attr,
1698 struct dt_allocation_hint *hint,
1699 struct dt_object_format *dof,
1703 struct osd_thandle *oth;
1704 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1706 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1708 LASSERT(S_ISREG(attr->la_mode));
1710 oth = container_of(th, struct osd_thandle, ot_super);
1711 LASSERT(oth->ot_handle->h_transaction != NULL);
1713 result = osd_mkfile(info, obj, mode, hint, th);
1715 LASSERT(obj->oo_inode != NULL);
1716 if (feat->dif_flags & DT_IND_VARKEY)
1717 result = iam_lvar_create(obj->oo_inode,
1718 feat->dif_keysize_max,
1720 feat->dif_recsize_max,
1723 result = iam_lfix_create(obj->oo_inode,
1724 feat->dif_keysize_max,
1726 feat->dif_recsize_max,
1733 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1734 struct lu_attr *attr,
1735 struct dt_allocation_hint *hint,
1736 struct dt_object_format *dof,
1739 LASSERT(S_ISREG(attr->la_mode));
1740 return osd_mkfile(info, obj, (attr->la_mode &
1741 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1744 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1745 struct lu_attr *attr,
1746 struct dt_allocation_hint *hint,
1747 struct dt_object_format *dof,
1750 LASSERT(S_ISLNK(attr->la_mode));
1751 return osd_mkfile(info, obj, (attr->la_mode &
1752 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1755 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1756 struct lu_attr *attr,
1757 struct dt_allocation_hint *hint,
1758 struct dt_object_format *dof,
1761 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1764 LINVRNT(osd_invariant(obj));
1765 LASSERT(obj->oo_inode == NULL);
1766 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1767 S_ISFIFO(mode) || S_ISSOCK(mode));
1769 result = osd_mkfile(info, obj, mode, hint, th);
1771 LASSERT(obj->oo_inode != NULL);
1773 * This inode should be marked dirty for i_rdev. Currently
1774 * that is done in the osd_attr_init().
1776 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1778 LINVRNT(osd_invariant(obj));
1782 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1784 struct dt_allocation_hint *hint,
1785 struct dt_object_format *dof,
1788 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1790 osd_obj_type_f result;
1806 result = osd_mk_index;
1817 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1818 struct dt_object *parent, cfs_umode_t child_mode)
1822 memset(ah, 0, sizeof(*ah));
1823 ah->dah_parent = parent;
1824 ah->dah_mode = child_mode;
1827 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1828 struct lu_attr *attr, struct dt_object_format *dof)
1830 struct inode *inode = obj->oo_inode;
1831 __u64 valid = attr->la_valid;
1834 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1836 if (dof->dof_type != DFT_NODE)
1837 attr->la_valid &= ~LA_RDEV;
1838 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1839 attr->la_valid &= ~LA_ATIME;
1840 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1841 attr->la_valid &= ~LA_CTIME;
1842 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1843 attr->la_valid &= ~LA_MTIME;
1845 if (LDISKFS_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1846 LDISKFS_FEATURE_RO_COMPAT_QUOTA)) {
1847 result = osd_quota_transfer(inode, attr);
1851 #ifdef HAVE_QUOTA_SUPPORT
1852 attr->la_valid &= ~(LA_UID | LA_GID);
1856 if (attr->la_valid != 0) {
1857 result = osd_inode_setattr(info->oti_env, inode, attr);
1859 * The osd_inode_setattr() should always succeed here. The
1860 * only error that could be returned is EDQUOT when we are
1861 * trying to change the UID or GID of the inode. However, this
1862 * should not happen since quota enforcement is no longer
1863 * enabled on ldiskfs (lquota takes care of it).
1865 LASSERTF(result == 0, "%d", result);
1866 inode->i_sb->s_op->dirty_inode(inode);
1869 attr->la_valid = valid;
1873 * Helper function for osd_object_create()
1875 * \retval 0, on success
1877 static int __osd_object_create(struct osd_thread_info *info,
1878 struct osd_object *obj, struct lu_attr *attr,
1879 struct dt_allocation_hint *hint,
1880 struct dt_object_format *dof,
1886 /* we drop umask so that permissions we pass are not affected */
1887 umask = current->fs->umask;
1888 current->fs->umask = 0;
1890 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1893 osd_attr_init(info, obj, attr, dof);
1894 osd_object_init0(obj);
1896 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1897 unlock_new_inode(obj->oo_inode);
1900 /* restore previous umask value */
1901 current->fs->umask = umask;
1907 * Helper function for osd_object_create()
1909 * \retval 0, on success
1911 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1912 const struct lu_fid *fid, struct thandle *th)
1914 struct osd_thread_info *info = osd_oti_get(env);
1915 struct osd_inode_id *id = &info->oti_id;
1916 struct osd_device *osd = osd_obj2dev(obj);
1917 struct md_ucred *uc = md_ucred(env);
1919 LASSERT(obj->oo_inode != NULL);
1920 LASSERT(uc != NULL);
1922 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1923 return osd_oi_insert(info, osd, fid, id, th);
1926 static int osd_declare_object_create(const struct lu_env *env,
1927 struct dt_object *dt,
1928 struct lu_attr *attr,
1929 struct dt_allocation_hint *hint,
1930 struct dt_object_format *dof,
1931 struct thandle *handle)
1933 struct osd_thandle *oh;
1935 LASSERT(handle != NULL);
1937 oh = container_of0(handle, struct osd_thandle, ot_super);
1938 LASSERT(oh->ot_handle == NULL);
1940 OSD_DECLARE_OP(oh, create);
1941 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1942 /* XXX: So far, only normal fid needs be inserted into the oi,
1943 * things could be changed later. Revise following code then. */
1944 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1945 OSD_DECLARE_OP(oh, insert);
1946 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1948 /* If this is directory, then we expect . and .. to be inserted as
1949 * well. The one directory block always needs to be created for the
1950 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1951 * block), there is no danger of needing a tree for the first block.
1953 if (attr && S_ISDIR(attr->la_mode)) {
1954 OSD_DECLARE_OP(oh, insert);
1955 OSD_DECLARE_OP(oh, insert);
1956 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1960 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1961 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1966 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1967 struct lu_attr *attr,
1968 struct dt_allocation_hint *hint,
1969 struct dt_object_format *dof,
1972 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1973 struct osd_object *obj = osd_dt_obj(dt);
1974 struct osd_thread_info *info = osd_oti_get(env);
1979 LINVRNT(osd_invariant(obj));
1980 LASSERT(!dt_object_exists(dt));
1981 LASSERT(osd_write_locked(env, obj));
1982 LASSERT(th != NULL);
1984 if (unlikely(fid_is_acct(fid)))
1985 /* Quota files can't be created from the kernel any more,
1986 * 'tune2fs -O quota' will take care of creating them */
1989 OSD_EXEC_OP(th, create);
1991 result = __osd_object_create(info, obj, attr, hint, dof, th);
1993 result = __osd_oi_insert(env, obj, fid, th);
1995 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1996 LASSERT(osd_invariant(obj));
2001 * Called to destroy on-disk representation of the object
2003 * Concurrency: must be locked
2005 static int osd_declare_object_destroy(const struct lu_env *env,
2006 struct dt_object *dt,
2009 struct osd_object *obj = osd_dt_obj(dt);
2010 struct inode *inode = obj->oo_inode;
2011 struct osd_thandle *oh;
2015 oh = container_of0(th, struct osd_thandle, ot_super);
2016 LASSERT(oh->ot_handle == NULL);
2019 OSD_DECLARE_OP(oh, destroy);
2020 OSD_DECLARE_OP(oh, delete);
2021 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2022 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2024 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
2025 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
2030 static int osd_object_destroy(const struct lu_env *env,
2031 struct dt_object *dt,
2034 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2035 struct osd_object *obj = osd_dt_obj(dt);
2036 struct inode *inode = obj->oo_inode;
2037 struct osd_device *osd = osd_obj2dev(obj);
2038 struct osd_thandle *oh;
2042 oh = container_of0(th, struct osd_thandle, ot_super);
2043 LASSERT(oh->ot_handle);
2045 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2047 if (unlikely(fid_is_acct(fid)))
2050 /* Parallel control for OI scrub. For most of cases, there is no
2051 * lock contention. So it will not affect unlink performance. */
2052 cfs_mutex_lock(&inode->i_mutex);
2053 if (S_ISDIR(inode->i_mode)) {
2054 LASSERT(osd_inode_unlinked(inode) ||
2055 inode->i_nlink == 1);
2056 cfs_spin_lock(&obj->oo_guard);
2058 cfs_spin_unlock(&obj->oo_guard);
2059 inode->i_sb->s_op->dirty_inode(inode);
2061 LASSERT(osd_inode_unlinked(inode));
2064 OSD_EXEC_OP(th, destroy);
2066 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2067 cfs_mutex_unlock(&inode->i_mutex);
2069 /* XXX: add to ext3 orphan list */
2070 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2072 /* not needed in the cache anymore */
2073 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2079 * Helper function for osd_xattr_set()
2081 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2082 const struct lu_buf *buf, const char *name, int fl)
2084 struct osd_object *obj = osd_dt_obj(dt);
2085 struct inode *inode = obj->oo_inode;
2086 struct osd_thread_info *info = osd_oti_get(env);
2087 struct dentry *dentry = &info->oti_child_dentry;
2091 LASSERT(dt_object_exists(dt));
2092 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2094 if (fl & LU_XATTR_REPLACE)
2095 fs_flags |= XATTR_REPLACE;
2097 if (fl & LU_XATTR_CREATE)
2098 fs_flags |= XATTR_CREATE;
2100 dentry->d_inode = inode;
2101 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2102 buf->lb_len, fs_flags);
2107 * Put the fid into lustre_mdt_attrs, and then place the structure
2108 * inode's ea. This fid should not be altered during the life time
2111 * \retval +ve, on success
2112 * \retval -ve, on error
2114 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2116 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2117 const struct lu_fid *fid)
2119 struct osd_thread_info *info = osd_oti_get(env);
2120 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2122 lustre_lma_init(mdt_attrs, fid);
2123 lustre_lma_swab(mdt_attrs);
2124 return __osd_xattr_set(env, dt,
2125 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2126 XATTR_NAME_LMA, LU_XATTR_CREATE);
2131 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2132 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2133 * To have compatilibility with 1.8 ldiskfs driver we need to have
2134 * magic number at start of fid data.
2135 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2138 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2139 const struct dt_rec *fid)
2141 param->edp_magic = LDISKFS_LUFID_MAGIC;
2142 param->edp_len = sizeof(struct lu_fid) + 1;
2144 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2145 (struct lu_fid *)fid);
2149 * Try to read the fid from inode ea into dt_rec, if return value
2150 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2152 * \param fid object fid.
2154 * \retval 0 on success
2156 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2157 __u32 ino, struct lu_fid *fid,
2158 struct osd_inode_id *id)
2160 struct osd_thread_info *info = osd_oti_get(env);
2161 struct inode *inode;
2164 osd_id_gen(id, ino, OSD_OII_NOGEN);
2165 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2167 RETURN(PTR_ERR(inode));
2174 * OSD layer object create function for interoperability mode (b11826).
2175 * This is mostly similar to osd_object_create(). Only difference being, fid is
2176 * inserted into inode ea here.
2178 * \retval 0, on success
2179 * \retval -ve, on error
2181 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2182 struct lu_attr *attr,
2183 struct dt_allocation_hint *hint,
2184 struct dt_object_format *dof,
2187 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2188 struct osd_object *obj = osd_dt_obj(dt);
2189 struct osd_thread_info *info = osd_oti_get(env);
2194 LASSERT(osd_invariant(obj));
2195 LASSERT(!dt_object_exists(dt));
2196 LASSERT(osd_write_locked(env, obj));
2197 LASSERT(th != NULL);
2199 if (unlikely(fid_is_acct(fid)))
2200 /* Quota files can't be created from the kernel any more,
2201 * 'tune2fs -O quota' will take care of creating them */
2204 OSD_EXEC_OP(th, create);
2206 result = __osd_object_create(info, obj, attr, hint, dof, th);
2207 /* objects under osd root shld have igif fid, so dont add fid EA */
2208 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2209 result = osd_ea_fid_set(env, dt, fid);
2212 result = __osd_oi_insert(env, obj, fid, th);
2214 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2215 LINVRNT(osd_invariant(obj));
2219 static int osd_declare_object_ref_add(const struct lu_env *env,
2220 struct dt_object *dt,
2221 struct thandle *handle)
2223 struct osd_thandle *oh;
2225 /* it's possible that object doesn't exist yet */
2226 LASSERT(handle != NULL);
2228 oh = container_of0(handle, struct osd_thandle, ot_super);
2229 LASSERT(oh->ot_handle == NULL);
2231 OSD_DECLARE_OP(oh, ref_add);
2232 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2238 * Concurrency: @dt is write locked.
2240 static int osd_object_ref_add(const struct lu_env *env,
2241 struct dt_object *dt, struct thandle *th)
2243 struct osd_object *obj = osd_dt_obj(dt);
2244 struct inode *inode = obj->oo_inode;
2246 LINVRNT(osd_invariant(obj));
2247 LASSERT(dt_object_exists(dt));
2248 LASSERT(osd_write_locked(env, obj));
2249 LASSERT(th != NULL);
2251 OSD_EXEC_OP(th, ref_add);
2254 * DIR_NLINK feature is set for compatibility reasons if:
2255 * 1) nlinks > LDISKFS_LINK_MAX, or
2256 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2258 * It is easier to always set this flag (rather than check and set),
2259 * since it has less overhead, and the superblock will be dirtied
2260 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2261 * do not actually care whether this flag is set or not.
2263 cfs_spin_lock(&obj->oo_guard);
2265 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2266 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2267 inode->i_nlink == 2)
2270 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2271 cfs_spin_unlock(&obj->oo_guard);
2272 inode->i_sb->s_op->dirty_inode(inode);
2273 LINVRNT(osd_invariant(obj));
2278 static int osd_declare_object_ref_del(const struct lu_env *env,
2279 struct dt_object *dt,
2280 struct thandle *handle)
2282 struct osd_thandle *oh;
2284 LASSERT(dt_object_exists(dt));
2285 LASSERT(handle != NULL);
2287 oh = container_of0(handle, struct osd_thandle, ot_super);
2288 LASSERT(oh->ot_handle == NULL);
2290 OSD_DECLARE_OP(oh, ref_del);
2291 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2297 * Concurrency: @dt is write locked.
2299 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2302 struct osd_object *obj = osd_dt_obj(dt);
2303 struct inode *inode = obj->oo_inode;
2305 LINVRNT(osd_invariant(obj));
2306 LASSERT(dt_object_exists(dt));
2307 LASSERT(osd_write_locked(env, obj));
2308 LASSERT(th != NULL);
2310 OSD_EXEC_OP(th, ref_del);
2312 cfs_spin_lock(&obj->oo_guard);
2313 LASSERT(inode->i_nlink > 0);
2315 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2316 * then the nlink count is 1. Don't let it be set to 0 or the directory
2317 * inode will be deleted incorrectly. */
2318 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2320 cfs_spin_unlock(&obj->oo_guard);
2321 inode->i_sb->s_op->dirty_inode(inode);
2322 LINVRNT(osd_invariant(obj));
2328 * Get the 64-bit version for an inode.
2330 static int osd_object_version_get(const struct lu_env *env,
2331 struct dt_object *dt, dt_obj_version_t *ver)
2333 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2335 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2336 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2337 *ver = LDISKFS_I(inode)->i_fs_version;
2342 * Concurrency: @dt is read locked.
2344 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2345 struct lu_buf *buf, const char *name,
2346 struct lustre_capa *capa)
2348 struct osd_object *obj = osd_dt_obj(dt);
2349 struct inode *inode = obj->oo_inode;
2350 struct osd_thread_info *info = osd_oti_get(env);
2351 struct dentry *dentry = &info->oti_obj_dentry;
2353 /* version get is not real XATTR but uses xattr API */
2354 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2355 /* for version we are just using xattr API but change inode
2357 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2358 osd_object_version_get(env, dt, buf->lb_buf);
2359 return sizeof(dt_obj_version_t);
2362 LASSERT(dt_object_exists(dt));
2363 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2364 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2366 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2369 dentry->d_inode = inode;
2370 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2374 static int osd_declare_xattr_set(const struct lu_env *env,
2375 struct dt_object *dt,
2376 const struct lu_buf *buf, const char *name,
2377 int fl, struct thandle *handle)
2379 struct osd_thandle *oh;
2381 LASSERT(handle != NULL);
2383 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2384 /* no credits for version */
2388 oh = container_of0(handle, struct osd_thandle, ot_super);
2389 LASSERT(oh->ot_handle == NULL);
2391 OSD_DECLARE_OP(oh, xattr_set);
2392 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2398 * Set the 64-bit version for object
2400 static void osd_object_version_set(const struct lu_env *env,
2401 struct dt_object *dt,
2402 dt_obj_version_t *new_version)
2404 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2406 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2407 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2409 LDISKFS_I(inode)->i_fs_version = *new_version;
2410 /** Version is set after all inode operations are finished,
2411 * so we should mark it dirty here */
2412 inode->i_sb->s_op->dirty_inode(inode);
2416 * Concurrency: @dt is write locked.
2418 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2419 const struct lu_buf *buf, const char *name, int fl,
2420 struct thandle *handle, struct lustre_capa *capa)
2422 LASSERT(handle != NULL);
2424 /* version set is not real XATTR */
2425 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2426 /* for version we are just using xattr API but change inode
2428 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2429 osd_object_version_set(env, dt, buf->lb_buf);
2430 return sizeof(dt_obj_version_t);
2433 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2436 OSD_EXEC_OP(handle, xattr_set);
2437 return __osd_xattr_set(env, dt, buf, name, fl);
2441 * Concurrency: @dt is read locked.
2443 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2444 struct lu_buf *buf, struct lustre_capa *capa)
2446 struct osd_object *obj = osd_dt_obj(dt);
2447 struct inode *inode = obj->oo_inode;
2448 struct osd_thread_info *info = osd_oti_get(env);
2449 struct dentry *dentry = &info->oti_obj_dentry;
2451 LASSERT(dt_object_exists(dt));
2452 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2453 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2455 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2458 dentry->d_inode = inode;
2459 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2462 static int osd_declare_xattr_del(const struct lu_env *env,
2463 struct dt_object *dt, const char *name,
2464 struct thandle *handle)
2466 struct osd_thandle *oh;
2468 LASSERT(dt_object_exists(dt));
2469 LASSERT(handle != NULL);
2471 oh = container_of0(handle, struct osd_thandle, ot_super);
2472 LASSERT(oh->ot_handle == NULL);
2474 OSD_DECLARE_OP(oh, xattr_set);
2475 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2481 * Concurrency: @dt is write locked.
2483 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2484 const char *name, struct thandle *handle,
2485 struct lustre_capa *capa)
2487 struct osd_object *obj = osd_dt_obj(dt);
2488 struct inode *inode = obj->oo_inode;
2489 struct osd_thread_info *info = osd_oti_get(env);
2490 struct dentry *dentry = &info->oti_obj_dentry;
2493 LASSERT(dt_object_exists(dt));
2494 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2495 LASSERT(osd_write_locked(env, obj));
2496 LASSERT(handle != NULL);
2498 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2501 OSD_EXEC_OP(handle, xattr_set);
2503 dentry->d_inode = inode;
2504 rc = inode->i_op->removexattr(dentry, name);
2508 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2509 struct dt_object *dt,
2510 struct lustre_capa *old,
2513 struct osd_thread_info *info = osd_oti_get(env);
2514 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2515 struct osd_object *obj = osd_dt_obj(dt);
2516 struct osd_device *dev = osd_obj2dev(obj);
2517 struct lustre_capa_key *key = &info->oti_capa_key;
2518 struct lustre_capa *capa = &info->oti_capa;
2519 struct obd_capa *oc;
2520 struct md_capainfo *ci;
2524 if (!dev->od_fl_capa)
2525 RETURN(ERR_PTR(-ENOENT));
2527 LASSERT(dt_object_exists(dt));
2528 LINVRNT(osd_invariant(obj));
2530 /* renewal sanity check */
2531 if (old && osd_object_auth(env, dt, old, opc))
2532 RETURN(ERR_PTR(-EACCES));
2534 ci = md_capainfo(env);
2536 RETURN(ERR_PTR(-ENOENT));
2538 switch (ci->mc_auth) {
2542 capa->lc_uid = obj->oo_inode->i_uid;
2543 capa->lc_gid = obj->oo_inode->i_gid;
2544 capa->lc_flags = LC_ID_PLAIN;
2546 case LC_ID_CONVERT: {
2549 s[0] = obj->oo_inode->i_uid;
2550 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2551 s[2] = obj->oo_inode->i_gid;
2552 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2553 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2555 RETURN(ERR_PTR(rc));
2557 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2558 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2559 capa->lc_flags = LC_ID_CONVERT;
2563 RETURN(ERR_PTR(-EINVAL));
2566 capa->lc_fid = *fid;
2568 capa->lc_flags |= dev->od_capa_alg << 24;
2569 capa->lc_timeout = dev->od_capa_timeout;
2570 capa->lc_expiry = 0;
2572 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2574 LASSERT(!capa_is_expired(oc));
2578 cfs_spin_lock(&capa_lock);
2579 *key = dev->od_capa_keys[1];
2580 cfs_spin_unlock(&capa_lock);
2582 capa->lc_keyid = key->lk_keyid;
2583 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2585 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2587 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2588 RETURN(ERR_PTR(rc));
2591 oc = capa_add(dev->od_capa_hash, capa);
2595 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2597 struct osd_object *obj = osd_dt_obj(dt);
2598 struct inode *inode = obj->oo_inode;
2599 struct osd_thread_info *info = osd_oti_get(env);
2600 struct dentry *dentry = &info->oti_obj_dentry;
2601 struct file *file = &info->oti_file;
2606 dentry->d_inode = inode;
2607 file->f_dentry = dentry;
2608 file->f_mapping = inode->i_mapping;
2609 file->f_op = inode->i_fop;
2610 LOCK_INODE_MUTEX(inode);
2611 rc = file->f_op->fsync(file, dentry, 0);
2612 UNLOCK_INODE_MUTEX(inode);
2616 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2619 struct osd_object *obj = osd_dt_obj(dt);
2622 *data = (void *)obj->oo_inode;
2630 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2631 const struct dt_index_features *feat)
2633 struct iam_descr *descr;
2635 if (osd_object_is_root(o))
2636 return feat == &dt_directory_features;
2638 LASSERT(o->oo_dir != NULL);
2640 descr = o->oo_dir->od_container.ic_descr;
2641 if (feat == &dt_directory_features) {
2642 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2648 feat->dif_keysize_min <= descr->id_key_size &&
2649 descr->id_key_size <= feat->dif_keysize_max &&
2650 feat->dif_recsize_min <= descr->id_rec_size &&
2651 descr->id_rec_size <= feat->dif_recsize_max &&
2652 !(feat->dif_flags & (DT_IND_VARKEY |
2653 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2654 ergo(feat->dif_flags & DT_IND_UPDATE,
2655 1 /* XXX check that object (and file system) is
2660 static int osd_iam_container_init(const struct lu_env *env,
2661 struct osd_object *obj,
2662 struct osd_directory *dir)
2664 struct iam_container *bag = &dir->od_container;
2667 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2671 result = iam_container_setup(bag);
2675 if (osd_obj2dev(obj)->od_iop_mode) {
2676 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2678 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2684 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2686 iam_container_fini(bag);
2693 * Concurrency: no external locking is necessary.
2695 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2696 const struct dt_index_features *feat)
2700 struct osd_object *obj = osd_dt_obj(dt);
2701 struct osd_device *osd = osd_obj2dev(obj);
2703 LINVRNT(osd_invariant(obj));
2704 LASSERT(dt_object_exists(dt));
2706 if (osd_object_is_root(obj)) {
2707 dt->do_index_ops = &osd_index_ea_ops;
2709 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2710 dt->do_index_ops = &osd_index_ea_ops;
2711 if (S_ISDIR(obj->oo_inode->i_mode))
2716 } else if (unlikely(feat == &dt_otable_features)) {
2717 dt->do_index_ops = &osd_otable_ops;
2719 } else if (feat == &dt_acct_features) {
2720 dt->do_index_ops = &osd_acct_index_ops;
2723 } else if (!osd_has_index(obj)) {
2724 struct osd_directory *dir;
2729 cfs_spin_lock(&obj->oo_guard);
2730 if (obj->oo_dir == NULL)
2734 * Concurrent thread allocated container data.
2737 cfs_spin_unlock(&obj->oo_guard);
2739 * Now, that we have container data, serialize its
2742 cfs_down_write(&obj->oo_ext_idx_sem);
2744 * recheck under lock.
2746 if (!osd_has_index(obj))
2747 result = osd_iam_container_init(env, obj, dir);
2750 cfs_up_write(&obj->oo_ext_idx_sem);
2758 if (result == 0 && skip_iam == 0) {
2759 if (!osd_iam_index_probe(env, obj, feat))
2762 LINVRNT(osd_invariant(obj));
2767 static int osd_otable_it_attr_get(const struct lu_env *env,
2768 struct dt_object *dt,
2769 struct lu_attr *attr,
2770 struct lustre_capa *capa)
2776 static const struct dt_object_operations osd_obj_ops = {
2777 .do_read_lock = osd_object_read_lock,
2778 .do_write_lock = osd_object_write_lock,
2779 .do_read_unlock = osd_object_read_unlock,
2780 .do_write_unlock = osd_object_write_unlock,
2781 .do_write_locked = osd_object_write_locked,
2782 .do_attr_get = osd_attr_get,
2783 .do_declare_attr_set = osd_declare_attr_set,
2784 .do_attr_set = osd_attr_set,
2785 .do_ah_init = osd_ah_init,
2786 .do_declare_create = osd_declare_object_create,
2787 .do_create = osd_object_create,
2788 .do_declare_destroy = osd_declare_object_destroy,
2789 .do_destroy = osd_object_destroy,
2790 .do_index_try = osd_index_try,
2791 .do_declare_ref_add = osd_declare_object_ref_add,
2792 .do_ref_add = osd_object_ref_add,
2793 .do_declare_ref_del = osd_declare_object_ref_del,
2794 .do_ref_del = osd_object_ref_del,
2795 .do_xattr_get = osd_xattr_get,
2796 .do_declare_xattr_set = osd_declare_xattr_set,
2797 .do_xattr_set = osd_xattr_set,
2798 .do_declare_xattr_del = osd_declare_xattr_del,
2799 .do_xattr_del = osd_xattr_del,
2800 .do_xattr_list = osd_xattr_list,
2801 .do_capa_get = osd_capa_get,
2802 .do_object_sync = osd_object_sync,
2803 .do_data_get = osd_data_get,
2807 * dt_object_operations for interoperability mode
2808 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2810 static const struct dt_object_operations osd_obj_ea_ops = {
2811 .do_read_lock = osd_object_read_lock,
2812 .do_write_lock = osd_object_write_lock,
2813 .do_read_unlock = osd_object_read_unlock,
2814 .do_write_unlock = osd_object_write_unlock,
2815 .do_write_locked = osd_object_write_locked,
2816 .do_attr_get = osd_attr_get,
2817 .do_declare_attr_set = osd_declare_attr_set,
2818 .do_attr_set = osd_attr_set,
2819 .do_ah_init = osd_ah_init,
2820 .do_declare_create = osd_declare_object_create,
2821 .do_create = osd_object_ea_create,
2822 .do_declare_destroy = osd_declare_object_destroy,
2823 .do_destroy = osd_object_destroy,
2824 .do_index_try = osd_index_try,
2825 .do_declare_ref_add = osd_declare_object_ref_add,
2826 .do_ref_add = osd_object_ref_add,
2827 .do_declare_ref_del = osd_declare_object_ref_del,
2828 .do_ref_del = osd_object_ref_del,
2829 .do_xattr_get = osd_xattr_get,
2830 .do_declare_xattr_set = osd_declare_xattr_set,
2831 .do_xattr_set = osd_xattr_set,
2832 .do_declare_xattr_del = osd_declare_xattr_del,
2833 .do_xattr_del = osd_xattr_del,
2834 .do_xattr_list = osd_xattr_list,
2835 .do_capa_get = osd_capa_get,
2836 .do_object_sync = osd_object_sync,
2837 .do_data_get = osd_data_get,
2840 static const struct dt_object_operations osd_obj_otable_it_ops = {
2841 .do_attr_get = osd_otable_it_attr_get,
2842 .do_index_try = osd_index_try,
2845 static int osd_index_declare_iam_delete(const struct lu_env *env,
2846 struct dt_object *dt,
2847 const struct dt_key *key,
2848 struct thandle *handle)
2850 struct osd_thandle *oh;
2852 oh = container_of0(handle, struct osd_thandle, ot_super);
2853 LASSERT(oh->ot_handle == NULL);
2855 OSD_DECLARE_OP(oh, delete);
2856 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2862 * delete a (key, value) pair from index \a dt specified by \a key
2864 * \param dt osd index object
2865 * \param key key for index
2866 * \param rec record reference
2867 * \param handle transaction handler
2870 * \retval -ve failure
2873 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2874 const struct dt_key *key,
2875 struct thandle *handle,
2876 struct lustre_capa *capa)
2878 struct osd_object *obj = osd_dt_obj(dt);
2879 struct osd_thandle *oh;
2880 struct iam_path_descr *ipd;
2881 struct iam_container *bag = &obj->oo_dir->od_container;
2886 LINVRNT(osd_invariant(obj));
2887 LASSERT(dt_object_exists(dt));
2888 LASSERT(bag->ic_object == obj->oo_inode);
2889 LASSERT(handle != NULL);
2891 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2894 OSD_EXEC_OP(handle, delete);
2896 ipd = osd_idx_ipd_get(env, bag);
2897 if (unlikely(ipd == NULL))
2900 oh = container_of0(handle, struct osd_thandle, ot_super);
2901 LASSERT(oh->ot_handle != NULL);
2902 LASSERT(oh->ot_handle->h_transaction != NULL);
2904 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2905 osd_ipd_put(env, bag, ipd);
2906 LINVRNT(osd_invariant(obj));
2910 static int osd_index_declare_ea_delete(const struct lu_env *env,
2911 struct dt_object *dt,
2912 const struct dt_key *key,
2913 struct thandle *handle)
2915 struct osd_thandle *oh;
2917 LASSERT(dt_object_exists(dt));
2918 LASSERT(handle != NULL);
2920 oh = container_of0(handle, struct osd_thandle, ot_super);
2921 LASSERT(oh->ot_handle == NULL);
2923 OSD_DECLARE_OP(oh, delete);
2924 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2926 LASSERT(osd_dt_obj(dt)->oo_inode);
2927 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2928 osd_dt_obj(dt)->oo_inode);
2929 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2930 osd_dt_obj(dt)->oo_inode);
2935 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2938 struct osd_fid_pack *rec;
2941 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2942 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2943 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2949 * Index delete function for interoperability mode (b11826).
2950 * It will remove the directory entry added by osd_index_ea_insert().
2951 * This entry is needed to maintain name->fid mapping.
2953 * \param key, key i.e. file entry to be deleted
2955 * \retval 0, on success
2956 * \retval -ve, on error
2958 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2959 const struct dt_key *key,
2960 struct thandle *handle,
2961 struct lustre_capa *capa)
2963 struct osd_object *obj = osd_dt_obj(dt);
2964 struct inode *dir = obj->oo_inode;
2965 struct dentry *dentry;
2966 struct osd_thandle *oh;
2967 struct ldiskfs_dir_entry_2 *de;
2968 struct buffer_head *bh;
2969 struct htree_lock *hlock = NULL;
2974 LINVRNT(osd_invariant(obj));
2975 LASSERT(dt_object_exists(dt));
2976 LASSERT(handle != NULL);
2978 OSD_EXEC_OP(handle, delete);
2980 oh = container_of(handle, struct osd_thandle, ot_super);
2981 LASSERT(oh->ot_handle != NULL);
2982 LASSERT(oh->ot_handle->h_transaction != NULL);
2984 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2987 dentry = osd_child_dentry_get(env, obj,
2988 (char *)key, strlen((char *)key));
2990 if (obj->oo_hl_head != NULL) {
2991 hlock = osd_oti_get(env)->oti_hlock;
2992 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2993 dir, LDISKFS_HLOCK_DEL);
2995 cfs_down_write(&obj->oo_ext_idx_sem);
2998 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3000 rc = ldiskfs_delete_entry(oh->ot_handle,
3007 ldiskfs_htree_unlock(hlock);
3009 cfs_up_write(&obj->oo_ext_idx_sem);
3011 LASSERT(osd_invariant(obj));
3016 * Lookup index for \a key and copy record to \a rec.
3018 * \param dt osd index object
3019 * \param key key for index
3020 * \param rec record reference
3022 * \retval +ve success : exact mach
3023 * \retval 0 return record with key not greater than \a key
3024 * \retval -ve failure
3026 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3027 struct dt_rec *rec, const struct dt_key *key,
3028 struct lustre_capa *capa)
3030 struct osd_object *obj = osd_dt_obj(dt);
3031 struct iam_path_descr *ipd;
3032 struct iam_container *bag = &obj->oo_dir->od_container;
3033 struct osd_thread_info *oti = osd_oti_get(env);
3034 struct iam_iterator *it = &oti->oti_idx_it;
3035 struct iam_rec *iam_rec;
3040 LASSERT(osd_invariant(obj));
3041 LASSERT(dt_object_exists(dt));
3042 LASSERT(bag->ic_object == obj->oo_inode);
3044 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3047 ipd = osd_idx_ipd_get(env, bag);
3051 /* got ipd now we can start iterator. */
3052 iam_it_init(it, bag, 0, ipd);
3054 rc = iam_it_get(it, (struct iam_key *)key);
3056 if (S_ISDIR(obj->oo_inode->i_mode))
3057 iam_rec = (struct iam_rec *)oti->oti_ldp;
3059 iam_rec = (struct iam_rec *) rec;
3061 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3062 if (S_ISDIR(obj->oo_inode->i_mode))
3063 osd_fid_unpack((struct lu_fid *) rec,
3064 (struct osd_fid_pack *)iam_rec);
3068 osd_ipd_put(env, bag, ipd);
3070 LINVRNT(osd_invariant(obj));
3075 static int osd_index_declare_iam_insert(const struct lu_env *env,
3076 struct dt_object *dt,
3077 const struct dt_rec *rec,
3078 const struct dt_key *key,
3079 struct thandle *handle)
3081 struct osd_thandle *oh;
3083 LASSERT(dt_object_exists(dt));
3084 LASSERT(handle != NULL);
3086 oh = container_of0(handle, struct osd_thandle, ot_super);
3087 LASSERT(oh->ot_handle == NULL);
3089 OSD_DECLARE_OP(oh, insert);
3090 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3096 * Inserts (key, value) pair in \a dt index object.
3098 * \param dt osd index object
3099 * \param key key for index
3100 * \param rec record reference
3101 * \param th transaction handler
3104 * \retval -ve failure
3106 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3107 const struct dt_rec *rec,
3108 const struct dt_key *key, struct thandle *th,
3109 struct lustre_capa *capa, int ignore_quota)
3111 struct osd_object *obj = osd_dt_obj(dt);
3112 struct iam_path_descr *ipd;
3113 struct osd_thandle *oh;
3114 struct iam_container *bag = &obj->oo_dir->od_container;
3115 #ifdef HAVE_QUOTA_SUPPORT
3116 cfs_cap_t save = cfs_curproc_cap_pack();
3118 struct osd_thread_info *oti = osd_oti_get(env);
3119 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3124 LINVRNT(osd_invariant(obj));
3125 LASSERT(dt_object_exists(dt));
3126 LASSERT(bag->ic_object == obj->oo_inode);
3127 LASSERT(th != NULL);
3129 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3132 OSD_EXEC_OP(th, insert);
3134 ipd = osd_idx_ipd_get(env, bag);
3135 if (unlikely(ipd == NULL))
3138 oh = container_of0(th, struct osd_thandle, ot_super);
3139 LASSERT(oh->ot_handle != NULL);
3140 LASSERT(oh->ot_handle->h_transaction != NULL);
3141 #ifdef HAVE_QUOTA_SUPPORT
3143 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3145 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3147 if (S_ISDIR(obj->oo_inode->i_mode))
3148 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3150 iam_rec = (struct iam_rec *) rec;
3151 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3153 #ifdef HAVE_QUOTA_SUPPORT
3154 cfs_curproc_cap_unpack(save);
3156 osd_ipd_put(env, bag, ipd);
3157 LINVRNT(osd_invariant(obj));
3162 * Calls ldiskfs_add_entry() to add directory entry
3163 * into the directory. This is required for
3164 * interoperability mode (b11826)
3166 * \retval 0, on success
3167 * \retval -ve, on error
3169 static int __osd_ea_add_rec(struct osd_thread_info *info,
3170 struct osd_object *pobj, struct inode *cinode,
3171 const char *name, const struct dt_rec *fid,
3172 struct htree_lock *hlock, struct thandle *th)
3174 struct ldiskfs_dentry_param *ldp;
3175 struct dentry *child;
3176 struct osd_thandle *oth;
3179 oth = container_of(th, struct osd_thandle, ot_super);
3180 LASSERT(oth->ot_handle != NULL);
3181 LASSERT(oth->ot_handle->h_transaction != NULL);
3183 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3185 /* XXX: remove fid_is_igif() check here.
3186 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3187 * it is IGIF now but needs FID in dir entry as well for readdir
3189 * LU-838 should fix that and remove fid_is_igif() check */
3190 if (fid_is_igif((struct lu_fid *)fid) ||
3191 fid_is_norm((struct lu_fid *)fid)) {
3192 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3193 osd_get_ldiskfs_dirent_param(ldp, fid);
3194 child->d_fsdata = (void *)ldp;
3196 child->d_fsdata = NULL;
3198 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3204 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3205 * into the directory.Also sets flags into osd object to
3206 * indicate dot and dotdot are created. This is required for
3207 * interoperability mode (b11826)
3209 * \param dir directory for dot and dotdot fixup.
3210 * \param obj child object for linking
3212 * \retval 0, on success
3213 * \retval -ve, on error
3215 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3216 struct osd_object *dir,
3217 struct inode *parent_dir, const char *name,
3218 const struct dt_rec *dot_fid,
3219 const struct dt_rec *dot_dot_fid,
3222 struct inode *inode = dir->oo_inode;
3223 struct ldiskfs_dentry_param *dot_ldp;
3224 struct ldiskfs_dentry_param *dot_dot_ldp;
3225 struct osd_thandle *oth;
3228 oth = container_of(th, struct osd_thandle, ot_super);
3229 LASSERT(oth->ot_handle->h_transaction != NULL);
3230 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3232 if (strcmp(name, dot) == 0) {
3233 if (dir->oo_compat_dot_created) {
3236 LASSERT(inode == parent_dir);
3237 dir->oo_compat_dot_created = 1;
3240 } else if(strcmp(name, dotdot) == 0) {
3241 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3242 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3244 if (!dir->oo_compat_dot_created)
3246 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3247 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3248 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3253 /* in case of rename, dotdot is already created */
3254 if (dir->oo_compat_dotdot_created) {
3255 return __osd_ea_add_rec(info, dir, parent_dir, name,
3256 dot_dot_fid, NULL, th);
3259 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3260 inode, dot_ldp, dot_dot_ldp);
3262 dir->oo_compat_dotdot_created = 1;
3270 * It will call the appropriate osd_add* function and return the
3271 * value, return by respective functions.
3273 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3274 struct inode *cinode, const char *name,
3275 const struct dt_rec *fid, struct thandle *th)
3277 struct osd_thread_info *info = osd_oti_get(env);
3278 struct htree_lock *hlock;
3281 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3283 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3285 if (hlock != NULL) {
3286 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3289 cfs_down_write(&pobj->oo_ext_idx_sem);
3291 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3292 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3295 if (hlock != NULL) {
3296 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3297 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3299 cfs_down_write(&pobj->oo_ext_idx_sem);
3302 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3306 ldiskfs_htree_unlock(hlock);
3308 cfs_up_write(&pobj->oo_ext_idx_sem);
3314 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3315 struct osd_idmap_cache *oic)
3317 struct osd_scrub *scrub = &dev->od_scrub;
3318 struct lu_fid *fid = &oic->oic_fid;
3319 struct osd_inode_id *id = &oti->oti_id;
3325 rc = osd_oi_lookup(oti, dev, fid, id);
3326 if (rc != 0 && rc != -ENOENT)
3329 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3332 if (thread_is_running(&scrub->os_thread)) {
3333 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3334 /* There is race condition between osd_oi_lookup and OI scrub.
3335 * The OI scrub finished just after osd_oi_lookup() failure.
3336 * Under such case, it is unnecessary to trigger OI scrub again,
3337 * but try to call osd_oi_lookup() again. */
3338 if (unlikely(rc == -EAGAIN))
3344 if (!scrub->os_no_scrub && ++once == 1) {
3345 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3347 rc = osd_scrub_start(dev);
3348 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3350 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3356 RETURN(rc = -EREMCHG);
3360 * Calls ->lookup() to find dentry. From dentry get inode and
3361 * read inode's ea to get fid. This is required for interoperability
3364 * \retval 0, on success
3365 * \retval -ve, on error
3367 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3368 struct dt_rec *rec, const struct dt_key *key)
3370 struct inode *dir = obj->oo_inode;
3371 struct dentry *dentry;
3372 struct ldiskfs_dir_entry_2 *de;
3373 struct buffer_head *bh;
3374 struct lu_fid *fid = (struct lu_fid *) rec;
3375 struct htree_lock *hlock = NULL;
3379 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3381 dentry = osd_child_dentry_get(env, obj,
3382 (char *)key, strlen((char *)key));
3384 if (obj->oo_hl_head != NULL) {
3385 hlock = osd_oti_get(env)->oti_hlock;
3386 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3387 dir, LDISKFS_HLOCK_LOOKUP);
3389 cfs_down_read(&obj->oo_ext_idx_sem);
3392 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3394 struct osd_thread_info *oti = osd_oti_get(env);
3395 struct osd_idmap_cache *oic = &oti->oti_cache;
3396 struct osd_device *dev = osd_obj2dev(obj);
3397 struct osd_scrub *scrub = &dev->od_scrub;
3398 struct scrub_file *sf = &scrub->os_file;
3400 ino = le32_to_cpu(de->inode);
3401 rc = osd_get_fid_from_dentry(de, rec);
3403 /* done with de, release bh */
3406 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3408 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3410 if (rc != 0 || !fid_is_norm(fid))
3413 oic->oic_fid = *fid;
3414 if ((scrub->os_pos_current <= ino) &&
3415 (sf->sf_flags & SF_INCONSISTENT ||
3416 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3418 rc = osd_consistency_check(oti, dev, oic);
3427 ldiskfs_htree_unlock(hlock);
3429 cfs_up_read(&obj->oo_ext_idx_sem);
3434 * Find the osd object for given fid.
3436 * \param fid need to find the osd object having this fid
3438 * \retval osd_object on success
3439 * \retval -ve on error
3441 struct osd_object *osd_object_find(const struct lu_env *env,
3442 struct dt_object *dt,
3443 const struct lu_fid *fid)
3445 struct lu_device *ludev = dt->do_lu.lo_dev;
3446 struct osd_object *child = NULL;
3447 struct lu_object *luch;
3448 struct lu_object *lo;
3450 luch = lu_object_find(env, ludev, fid, NULL);
3451 if (!IS_ERR(luch)) {
3452 if (lu_object_exists(luch)) {
3453 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3455 child = osd_obj(lo);
3457 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3458 "lu_object can't be located"
3459 DFID"\n", PFID(fid));
3461 if (child == NULL) {
3462 lu_object_put(env, luch);
3463 CERROR("Unable to get osd_object\n");
3464 child = ERR_PTR(-ENOENT);
3467 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3468 "lu_object does not exists "DFID"\n",
3470 lu_object_put(env, luch);
3471 child = ERR_PTR(-ENOENT);
3474 child = (void *)luch;
3480 * Put the osd object once done with it.
3482 * \param obj osd object that needs to be put
3484 static inline void osd_object_put(const struct lu_env *env,
3485 struct osd_object *obj)
3487 lu_object_put(env, &obj->oo_dt.do_lu);
3490 static int osd_index_declare_ea_insert(const struct lu_env *env,
3491 struct dt_object *dt,
3492 const struct dt_rec *rec,
3493 const struct dt_key *key,
3494 struct thandle *handle)
3496 struct osd_thandle *oh;
3498 LASSERT(dt_object_exists(dt));
3499 LASSERT(handle != NULL);
3501 oh = container_of0(handle, struct osd_thandle, ot_super);
3502 LASSERT(oh->ot_handle == NULL);
3504 OSD_DECLARE_OP(oh, insert);
3505 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3507 LASSERT(osd_dt_obj(dt)->oo_inode);
3508 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3509 osd_dt_obj(dt)->oo_inode);
3510 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3511 osd_dt_obj(dt)->oo_inode);
3517 * Index add function for interoperability mode (b11826).
3518 * It will add the directory entry.This entry is needed to
3519 * maintain name->fid mapping.
3521 * \param key it is key i.e. file entry to be inserted
3522 * \param rec it is value of given key i.e. fid
3524 * \retval 0, on success
3525 * \retval -ve, on error
3527 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3528 const struct dt_rec *rec,
3529 const struct dt_key *key, struct thandle *th,
3530 struct lustre_capa *capa, int ignore_quota)
3532 struct osd_object *obj = osd_dt_obj(dt);
3533 struct lu_fid *fid = (struct lu_fid *) rec;
3534 const char *name = (const char *)key;
3535 struct osd_object *child;
3536 #ifdef HAVE_QUOTA_SUPPORT
3537 cfs_cap_t save = cfs_curproc_cap_pack();
3543 LASSERT(osd_invariant(obj));
3544 LASSERT(dt_object_exists(dt));
3545 LASSERT(th != NULL);
3547 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3550 child = osd_object_find(env, dt, fid);
3551 if (!IS_ERR(child)) {
3552 #ifdef HAVE_QUOTA_SUPPORT
3554 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3556 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3558 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3559 #ifdef HAVE_QUOTA_SUPPORT
3560 cfs_curproc_cap_unpack(save);
3562 osd_object_put(env, child);
3564 rc = PTR_ERR(child);
3567 LASSERT(osd_invariant(obj));
3572 * Initialize osd Iterator for given osd index object.
3574 * \param dt osd index object
3577 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3578 struct dt_object *dt,
3580 struct lustre_capa *capa)
3582 struct osd_it_iam *it;
3583 struct osd_thread_info *oti = osd_oti_get(env);
3584 struct osd_object *obj = osd_dt_obj(dt);
3585 struct lu_object *lo = &dt->do_lu;
3586 struct iam_path_descr *ipd;
3587 struct iam_container *bag = &obj->oo_dir->od_container;
3589 LASSERT(lu_object_exists(lo));
3591 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3592 return ERR_PTR(-EACCES);
3595 ipd = osd_it_ipd_get(env, bag);
3596 if (likely(ipd != NULL)) {
3600 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3601 return (struct dt_it *)it;
3603 return ERR_PTR(-ENOMEM);
3607 * free given Iterator.
3610 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3612 struct osd_it_iam *it = (struct osd_it_iam *)di;
3613 struct osd_object *obj = it->oi_obj;
3615 iam_it_fini(&it->oi_it);
3616 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3617 lu_object_put(env, &obj->oo_dt.do_lu);
3621 * Move Iterator to record specified by \a key
3623 * \param di osd iterator
3624 * \param key key for index
3626 * \retval +ve di points to record with least key not larger than key
3627 * \retval 0 di points to exact matched key
3628 * \retval -ve failure
3631 static int osd_it_iam_get(const struct lu_env *env,
3632 struct dt_it *di, const struct dt_key *key)
3634 struct osd_it_iam *it = (struct osd_it_iam *)di;
3636 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3642 * \param di osd iterator
3645 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3647 struct osd_it_iam *it = (struct osd_it_iam *)di;
3649 iam_it_put(&it->oi_it);
3653 * Move iterator by one record
3655 * \param di osd iterator
3657 * \retval +1 end of container reached
3659 * \retval -ve failure
3662 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3664 struct osd_it_iam *it = (struct osd_it_iam *)di;
3666 return iam_it_next(&it->oi_it);
3670 * Return pointer to the key under iterator.
3673 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3674 const struct dt_it *di)
3676 struct osd_it_iam *it = (struct osd_it_iam *)di;
3678 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3682 * Return size of key under iterator (in bytes)
3685 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3687 struct osd_it_iam *it = (struct osd_it_iam *)di;
3689 return iam_it_key_size(&it->oi_it);
3692 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3693 int len, __u16 type)
3695 struct luda_type *lt;
3696 const unsigned align = sizeof(struct luda_type) - 1;
3698 /* check if file type is required */
3699 if (attr & LUDA_TYPE) {
3700 len = (len + align) & ~align;
3702 lt = (void *) ent->lde_name + len;
3703 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3704 ent->lde_attrs |= LUDA_TYPE;
3707 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3711 * build lu direct from backend fs dirent.
3714 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3715 struct lu_fid *fid, __u64 offset,
3716 char *name, __u16 namelen,
3717 __u16 type, __u32 attr)
3719 fid_cpu_to_le(&ent->lde_fid, fid);
3720 ent->lde_attrs = LUDA_FID;
3722 ent->lde_hash = cpu_to_le64(offset);
3723 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3725 strncpy(ent->lde_name, name, namelen);
3726 ent->lde_namelen = cpu_to_le16(namelen);
3728 /* append lustre attributes */
3729 osd_it_append_attrs(ent, attr, namelen, type);
3733 * Return pointer to the record under iterator.
3735 static int osd_it_iam_rec(const struct lu_env *env,
3736 const struct dt_it *di,
3737 struct dt_rec *dtrec, __u32 attr)
3739 struct osd_it_iam *it = (struct osd_it_iam *)di;
3740 struct osd_thread_info *info = osd_oti_get(env);
3743 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3744 const struct osd_fid_pack *rec;
3745 struct lu_fid *fid = &info->oti_fid;
3746 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3752 name = (char *)iam_it_key_get(&it->oi_it);
3754 RETURN(PTR_ERR(name));
3756 namelen = iam_it_key_size(&it->oi_it);
3758 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3760 RETURN(PTR_ERR(rec));
3762 rc = osd_fid_unpack(fid, rec);
3766 hash = iam_it_store(&it->oi_it);
3768 /* IAM does not store object type in IAM index (dir) */
3769 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3772 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3773 (struct iam_rec *)dtrec);
3780 * Returns cookie for current Iterator position.
3782 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3784 struct osd_it_iam *it = (struct osd_it_iam *)di;
3786 return iam_it_store(&it->oi_it);
3790 * Restore iterator from cookie.
3792 * \param di osd iterator
3793 * \param hash Iterator location cookie
3795 * \retval +ve di points to record with least key not larger than key.
3796 * \retval 0 di points to exact matched key
3797 * \retval -ve failure
3800 static int osd_it_iam_load(const struct lu_env *env,
3801 const struct dt_it *di, __u64 hash)
3803 struct osd_it_iam *it = (struct osd_it_iam *)di;
3805 return iam_it_load(&it->oi_it, hash);
3808 static const struct dt_index_operations osd_index_iam_ops = {
3809 .dio_lookup = osd_index_iam_lookup,
3810 .dio_declare_insert = osd_index_declare_iam_insert,
3811 .dio_insert = osd_index_iam_insert,
3812 .dio_declare_delete = osd_index_declare_iam_delete,
3813 .dio_delete = osd_index_iam_delete,
3815 .init = osd_it_iam_init,
3816 .fini = osd_it_iam_fini,
3817 .get = osd_it_iam_get,
3818 .put = osd_it_iam_put,
3819 .next = osd_it_iam_next,
3820 .key = osd_it_iam_key,
3821 .key_size = osd_it_iam_key_size,
3822 .rec = osd_it_iam_rec,
3823 .store = osd_it_iam_store,
3824 .load = osd_it_iam_load
3829 * Creates or initializes iterator context.
3831 * \retval struct osd_it_ea, iterator structure on success
3834 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3835 struct dt_object *dt,
3837 struct lustre_capa *capa)
3839 struct osd_object *obj = osd_dt_obj(dt);
3840 struct osd_thread_info *info = osd_oti_get(env);
3841 struct osd_it_ea *it = &info->oti_it_ea;
3842 struct lu_object *lo = &dt->do_lu;
3843 struct dentry *obj_dentry = &info->oti_it_dentry;
3845 LASSERT(lu_object_exists(lo));
3847 obj_dentry->d_inode = obj->oo_inode;
3848 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3849 obj_dentry->d_name.hash = 0;
3851 it->oie_rd_dirent = 0;
3852 it->oie_it_dirent = 0;
3853 it->oie_dirent = NULL;
3854 it->oie_buf = info->oti_it_ea_buf;
3856 it->oie_file.f_pos = 0;
3857 it->oie_file.f_dentry = obj_dentry;
3858 if (attr & LUDA_64BITHASH)
3859 it->oie_file.f_flags = O_64BITHASH;
3861 it->oie_file.f_flags = O_32BITHASH;
3862 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3863 it->oie_file.f_op = obj->oo_inode->i_fop;
3864 it->oie_file.private_data = NULL;
3866 RETURN((struct dt_it *) it);
3870 * Destroy or finishes iterator context.
3872 * \param di iterator structure to be destroyed
3874 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3876 struct osd_it_ea *it = (struct osd_it_ea *)di;
3877 struct osd_object *obj = it->oie_obj;
3878 struct inode *inode = obj->oo_inode;
3881 it->oie_file.f_op->release(inode, &it->oie_file);
3882 lu_object_put(env, &obj->oo_dt.do_lu);
3887 * It position the iterator at given key, so that next lookup continues from
3888 * that key Or it is similar to dio_it->load() but based on a key,
3889 * rather than file position.
3891 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3894 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3896 static int osd_it_ea_get(const struct lu_env *env,
3897 struct dt_it *di, const struct dt_key *key)
3899 struct osd_it_ea *it = (struct osd_it_ea *)di;
3902 LASSERT(((const char *)key)[0] == '\0');
3903 it->oie_file.f_pos = 0;
3904 it->oie_rd_dirent = 0;
3905 it->oie_it_dirent = 0;
3906 it->oie_dirent = NULL;
3914 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3919 * It is called internally by ->readdir(). It fills the
3920 * iterator's in-memory data structure with required
3921 * information i.e. name, namelen, rec_size etc.
3923 * \param buf in which information to be filled in.
3924 * \param name name of the file in given dir
3926 * \retval 0 on success
3927 * \retval 1 on buffer full
3929 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3930 loff_t offset, __u64 ino,
3933 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3934 struct osd_it_ea_dirent *ent = it->oie_dirent;
3935 struct lu_fid *fid = &ent->oied_fid;
3936 struct osd_fid_pack *rec;
3939 /* this should never happen */
3940 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3941 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3945 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3949 if (d_type & LDISKFS_DIRENT_LUFID) {
3950 rec = (struct osd_fid_pack*) (name + namelen + 1);
3952 if (osd_fid_unpack(fid, rec) != 0)
3955 d_type &= ~LDISKFS_DIRENT_LUFID;
3960 ent->oied_ino = ino;
3961 ent->oied_off = offset;
3962 ent->oied_namelen = namelen;
3963 ent->oied_type = d_type;
3965 memcpy(ent->oied_name, name, namelen);
3967 it->oie_rd_dirent++;
3968 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3973 * Calls ->readdir() to load a directory entry at a time
3974 * and stored it in iterator's in-memory data structure.
3976 * \param di iterator's in memory structure
3978 * \retval 0 on success
3979 * \retval -ve on error
3981 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3982 const struct dt_it *di)
3984 struct osd_it_ea *it = (struct osd_it_ea *)di;
3985 struct osd_object *obj = it->oie_obj;
3986 struct inode *inode = obj->oo_inode;
3987 struct htree_lock *hlock = NULL;
3991 it->oie_dirent = it->oie_buf;
3992 it->oie_rd_dirent = 0;
3994 if (obj->oo_hl_head != NULL) {
3995 hlock = osd_oti_get(env)->oti_hlock;
3996 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3997 inode, LDISKFS_HLOCK_READDIR);
3999 cfs_down_read(&obj->oo_ext_idx_sem);
4002 result = inode->i_fop->readdir(&it->oie_file, it,
4003 (filldir_t) osd_ldiskfs_filldir);
4006 ldiskfs_htree_unlock(hlock);
4008 cfs_up_read(&obj->oo_ext_idx_sem);
4010 if (it->oie_rd_dirent == 0) {
4013 it->oie_dirent = it->oie_buf;
4014 it->oie_it_dirent = 1;
4021 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4022 * to load a directory entry at a time and stored it in
4023 * iterator's in-memory data structure.
4025 * \param di iterator's in memory structure
4027 * \retval +ve iterator reached to end
4028 * \retval 0 iterator not reached to end
4029 * \retval -ve on error
4031 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4033 struct osd_it_ea *it = (struct osd_it_ea *)di;
4038 if (it->oie_it_dirent < it->oie_rd_dirent) {
4040 (void *) it->oie_dirent +
4041 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4042 it->oie_dirent->oied_namelen);
4043 it->oie_it_dirent++;
4046 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
4049 rc = osd_ldiskfs_it_fill(env, di);
4056 * Returns the key at current position from iterator's in memory structure.
4058 * \param di iterator's in memory structure
4060 * \retval key i.e. struct dt_key on success
4062 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4063 const struct dt_it *di)
4065 struct osd_it_ea *it = (struct osd_it_ea *)di;
4067 return (struct dt_key *)it->oie_dirent->oied_name;
4071 * Returns the key's size at current position from iterator's in memory structure.
4073 * \param di iterator's in memory structure
4075 * \retval key_size i.e. struct dt_key on success
4077 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4079 struct osd_it_ea *it = (struct osd_it_ea *)di;
4081 return it->oie_dirent->oied_namelen;
4086 * Returns the value (i.e. fid/igif) at current position from iterator's
4087 * in memory structure.
4089 * \param di struct osd_it_ea, iterator's in memory structure
4090 * \param attr attr requested for dirent.
4091 * \param lde lustre dirent
4093 * \retval 0 no error and \param lde has correct lustre dirent.
4094 * \retval -ve on error
4096 static inline int osd_it_ea_rec(const struct lu_env *env,
4097 const struct dt_it *di,
4098 struct dt_rec *dtrec, __u32 attr)
4100 struct osd_it_ea *it = (struct osd_it_ea *)di;
4101 struct osd_object *obj = it->oie_obj;
4102 struct osd_device *dev = osd_obj2dev(obj);
4103 struct osd_scrub *scrub = &dev->od_scrub;
4104 struct scrub_file *sf = &scrub->os_file;
4105 struct osd_thread_info *oti = osd_oti_get(env);
4106 struct osd_idmap_cache *oic = &oti->oti_cache;
4107 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4108 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4109 __u32 ino = it->oie_dirent->oied_ino;
4113 if (!fid_is_sane(fid)) {
4114 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4118 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4121 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4122 it->oie_dirent->oied_name,
4123 it->oie_dirent->oied_namelen,
4124 it->oie_dirent->oied_type, attr);
4126 if (!fid_is_norm(fid))
4129 oic->oic_fid = *fid;
4130 if ((scrub->os_pos_current <= ino) &&
4131 (sf->sf_flags & SF_INCONSISTENT ||
4132 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4133 rc = osd_consistency_check(oti, dev, oic);
4139 * Returns a cookie for current position of the iterator head, so that
4140 * user can use this cookie to load/start the iterator next time.
4142 * \param di iterator's in memory structure
4144 * \retval cookie for current position, on success
4146 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4148 struct osd_it_ea *it = (struct osd_it_ea *)di;
4150 return it->oie_dirent->oied_off;
4154 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4155 * to load a directory entry at a time and stored it i inn,
4156 * in iterator's in-memory data structure.
4158 * \param di struct osd_it_ea, iterator's in memory structure
4160 * \retval +ve on success
4161 * \retval -ve on error
4163 static int osd_it_ea_load(const struct lu_env *env,
4164 const struct dt_it *di, __u64 hash)
4166 struct osd_it_ea *it = (struct osd_it_ea *)di;
4170 it->oie_file.f_pos = hash;
4172 rc = osd_ldiskfs_it_fill(env, di);
4180 * Index lookup function for interoperability mode (b11826).
4182 * \param key, key i.e. file name to be searched
4184 * \retval +ve, on success
4185 * \retval -ve, on error
4187 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4188 struct dt_rec *rec, const struct dt_key *key,
4189 struct lustre_capa *capa)
4191 struct osd_object *obj = osd_dt_obj(dt);
4196 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4197 LINVRNT(osd_invariant(obj));
4199 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4202 rc = osd_ea_lookup_rec(env, obj, rec, key);
4210 * Index and Iterator operations for interoperability
4211 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4213 static const struct dt_index_operations osd_index_ea_ops = {
4214 .dio_lookup = osd_index_ea_lookup,
4215 .dio_declare_insert = osd_index_declare_ea_insert,
4216 .dio_insert = osd_index_ea_insert,
4217 .dio_declare_delete = osd_index_declare_ea_delete,
4218 .dio_delete = osd_index_ea_delete,
4220 .init = osd_it_ea_init,
4221 .fini = osd_it_ea_fini,
4222 .get = osd_it_ea_get,
4223 .put = osd_it_ea_put,
4224 .next = osd_it_ea_next,
4225 .key = osd_it_ea_key,
4226 .key_size = osd_it_ea_key_size,
4227 .rec = osd_it_ea_rec,
4228 .store = osd_it_ea_store,
4229 .load = osd_it_ea_load
4233 static void *osd_key_init(const struct lu_context *ctx,
4234 struct lu_context_key *key)
4236 struct osd_thread_info *info;
4238 OBD_ALLOC_PTR(info);
4240 return ERR_PTR(-ENOMEM);
4242 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4243 if (info->oti_it_ea_buf == NULL)
4246 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4248 info->oti_hlock = ldiskfs_htree_lock_alloc();
4249 if (info->oti_hlock == NULL)
4255 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4258 return ERR_PTR(-ENOMEM);
4261 static void osd_key_fini(const struct lu_context *ctx,
4262 struct lu_context_key *key, void* data)
4264 struct osd_thread_info *info = data;
4266 if (info->oti_hlock != NULL)
4267 ldiskfs_htree_lock_free(info->oti_hlock);
4268 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4272 static void osd_key_exit(const struct lu_context *ctx,
4273 struct lu_context_key *key, void *data)
4275 struct osd_thread_info *info = data;
4277 LASSERT(info->oti_r_locks == 0);
4278 LASSERT(info->oti_w_locks == 0);
4279 LASSERT(info->oti_txns == 0);
4282 /* type constructor/destructor: osd_type_init, osd_type_fini */
4283 LU_TYPE_INIT_FINI(osd, &osd_key);
4285 struct lu_context_key osd_key = {
4286 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4287 .lct_init = osd_key_init,
4288 .lct_fini = osd_key_fini,
4289 .lct_exit = osd_key_exit
4293 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4294 const char *name, struct lu_device *next)
4296 struct osd_device *osd = osd_dev(d);
4298 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4299 return osd_procfs_init(osd, name);
4302 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4306 osd_scrub_cleanup(env, o);
4309 fsfilt_put_ops(o->od_fsops);
4313 /* shutdown quota slave instance associated with the device */
4314 if (o->od_quota_slave != NULL) {
4315 qsd_fini(env, o->od_quota_slave);
4316 o->od_quota_slave = NULL;
4322 static int osd_mount(const struct lu_env *env,
4323 struct osd_device *o, struct lustre_cfg *cfg)
4325 struct lustre_mount_info *lmi;
4326 const char *dev = lustre_cfg_string(cfg, 0);
4327 struct lustre_disk_data *ldd;
4328 struct lustre_sb_info *lsi;
4333 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4334 if (o->od_fsops == NULL) {
4335 CERROR("Can't find fsfilt_ldiskfs\n");
4339 if (o->od_mount != NULL) {
4340 CERROR("Already mounted (%s)\n", dev);
4345 lmi = server_get_mount(dev);
4347 CERROR("Cannot get mount info for %s!\n", dev);
4351 LASSERT(lmi != NULL);
4352 /* save lustre_mount_info in dt_device */
4354 o->od_mnt = lmi->lmi_mnt;
4356 lsi = s2lsi(lmi->lmi_sb);
4359 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4361 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", dev);
4365 if (ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
4366 rc = osd_compat_init(o);
4368 CERROR("%s: can't initialize compats: %d\n", dev, rc);
4374 static struct lu_device *osd_device_fini(const struct lu_env *env,
4375 struct lu_device *d)
4380 osd_compat_fini(osd_dev(d));
4382 shrink_dcache_sb(osd_sb(osd_dev(d)));
4383 osd_sync(env, lu2dt_dev(d));
4385 rc = osd_procfs_fini(osd_dev(d));
4387 CERROR("proc fini error %d \n", rc);
4388 RETURN (ERR_PTR(rc));
4391 if (osd_dev(d)->od_mount)
4392 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4393 osd_dev(d)->od_mount->lmi_mnt);
4394 osd_dev(d)->od_mount = NULL;
4399 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4400 struct lu_device_type *t,
4401 struct lustre_cfg *cfg)
4403 struct lu_device *l;
4404 struct osd_device *o;
4410 result = dt_device_init(&o->od_dt_dev, t);
4413 l->ld_ops = &osd_lu_ops;
4414 o->od_dt_dev.dd_ops = &osd_dt_ops;
4415 cfs_spin_lock_init(&o->od_osfs_lock);
4416 cfs_mutex_init(&o->od_otable_mutex);
4417 o->od_osfs_age = cfs_time_shift_64(-1000);
4418 o->od_capa_hash = init_capa_hash();
4419 if (o->od_capa_hash == NULL) {
4420 dt_device_fini(&o->od_dt_dev);
4421 l = ERR_PTR(-ENOMEM);
4424 l = ERR_PTR(result);
4429 l = ERR_PTR(-ENOMEM);
4433 static struct lu_device *osd_device_free(const struct lu_env *env,
4434 struct lu_device *d)
4436 struct osd_device *o = osd_dev(d);
4439 cleanup_capa_hash(o->od_capa_hash);
4440 dt_device_fini(&o->od_dt_dev);
4445 static int osd_process_config(const struct lu_env *env,
4446 struct lu_device *d, struct lustre_cfg *cfg)
4448 struct osd_device *o = osd_dev(d);
4452 switch(cfg->lcfg_command) {
4454 err = osd_mount(env, o, cfg);
4457 err = osd_shutdown(env, o);
4466 static int osd_recovery_complete(const struct lu_env *env,
4467 struct lu_device *d)
4472 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4473 struct lu_device *dev)
4475 struct osd_device *osd = osd_dev(dev);
4479 /* 1. setup scrub, including OI files initialization */
4480 result = osd_scrub_setup(env, osd);
4484 /* 2. setup quota slave instance */
4485 osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
4486 osd->od_proc_entry);
4487 if (IS_ERR(osd->od_quota_slave)) {
4488 result = PTR_ERR(osd->od_quota_slave);
4489 osd->od_quota_slave = NULL;
4493 if (!lu_device_is_md(pdev))
4496 /* 3. setup local objects */
4497 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4501 static const struct lu_object_operations osd_lu_obj_ops = {
4502 .loo_object_init = osd_object_init,
4503 .loo_object_delete = osd_object_delete,
4504 .loo_object_release = osd_object_release,
4505 .loo_object_free = osd_object_free,
4506 .loo_object_print = osd_object_print,
4507 .loo_object_invariant = osd_object_invariant
4510 const struct lu_device_operations osd_lu_ops = {
4511 .ldo_object_alloc = osd_object_alloc,
4512 .ldo_process_config = osd_process_config,
4513 .ldo_recovery_complete = osd_recovery_complete,
4514 .ldo_prepare = osd_prepare,
4517 static const struct lu_device_type_operations osd_device_type_ops = {
4518 .ldto_init = osd_type_init,
4519 .ldto_fini = osd_type_fini,
4521 .ldto_start = osd_type_start,
4522 .ldto_stop = osd_type_stop,
4524 .ldto_device_alloc = osd_device_alloc,
4525 .ldto_device_free = osd_device_free,
4527 .ldto_device_init = osd_device_init,
4528 .ldto_device_fini = osd_device_fini
4531 static struct lu_device_type osd_device_type = {
4532 .ldt_tags = LU_DEVICE_DT,
4533 .ldt_name = LUSTRE_OSD_NAME,
4534 .ldt_ops = &osd_device_type_ops,
4535 .ldt_ctx_tags = LCT_LOCAL,
4539 * lprocfs legacy support.
4541 static struct obd_ops osd_obd_device_ops = {
4542 .o_owner = THIS_MODULE
4545 static int __init osd_mod_init(void)
4547 struct lprocfs_static_vars lvars;
4550 lprocfs_osd_init_vars(&lvars);
4551 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4552 LUSTRE_OSD_NAME, &osd_device_type);
4555 static void __exit osd_mod_exit(void)
4557 class_unregister_type(LUSTRE_OSD_NAME);
4560 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4561 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4562 MODULE_LICENSE("GPL");
4564 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);