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);
427 * Objects are created as locking anchors or place holders for objects
428 * yet to be created. No need to osd_oi_lookup() at here because FID
429 * shouldn't never be re-used, if it's really a duplicate FID from
430 * unexpected reason, we should be able to detect it later by calling
431 * do_create->osd_oi_insert()
433 if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
434 GOTO(out, result = 0);
436 /* Search order: 3. OI files. */
437 result = osd_oi_lookup(info, dev, fid, id);
438 if (result == -ENOENT) {
439 if (!fid_is_norm(fid) ||
440 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
442 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 if (unlikely(osd->od_mount == NULL))
986 /* osd_lproc.c call this without env, allocate ksfs for that case */
987 if (unlikely(env == NULL)) {
992 ksfs = &osd_oti_get(env)->oti_ksfs;
995 cfs_spin_lock(&osd->od_osfs_lock);
997 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
998 result = ll_do_statfs(sb, ksfs);
999 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1000 osd->od_osfs_age = cfs_time_current_64();
1001 statfs_pack(&osd->od_statfs, ksfs);
1005 if (likely(result == 0))
1006 *sfs = osd->od_statfs;
1007 cfs_spin_unlock(&osd->od_osfs_lock);
1009 if (unlikely(env == NULL))
1016 * Concurrency: doesn't access mutable data.
1018 static void osd_conf_get(const struct lu_env *env,
1019 const struct dt_device *dev,
1020 struct dt_device_param *param)
1022 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1025 * XXX should be taken from not-yet-existing fs abstraction layer.
1027 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1028 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1029 param->ddp_block_shift = sb->s_blocksize_bits;
1030 param->ddp_mntopts = 0;
1031 if (test_opt(sb, XATTR_USER))
1032 param->ddp_mntopts |= MNTOPT_USERXATTR;
1033 if (test_opt(sb, POSIX_ACL))
1034 param->ddp_mntopts |= MNTOPT_ACL;
1036 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1037 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1038 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1041 param->ddp_max_ea_size = sb->s_blocksize;
1046 * Helper function to get and fill the buffer with input values.
1048 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1052 buf = &osd_oti_get(env)->oti_buf;
1059 * Concurrency: shouldn't matter.
1061 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1063 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1064 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1068 * Start commit for OSD device.
1070 * An implementation of dt_commit_async method for OSD device.
1071 * Asychronously starts underlayng fs sync and thereby a transaction
1074 * \param env environment
1075 * \param d dt device
1077 * \see dt_device_operations
1079 static int osd_commit_async(const struct lu_env *env,
1080 struct dt_device *d)
1082 struct super_block *s = osd_sb(osd_dt_dev(d));
1085 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1086 RETURN(s->s_op->sync_fs(s, 0));
1090 * Concurrency: shouldn't matter.
1093 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1095 struct super_block *sb = osd_sb(osd_dt_dev(d));
1099 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1101 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1106 * Concurrency: serialization provided by callers.
1108 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1109 int mode, unsigned long timeout, __u32 alg,
1110 struct lustre_capa_key *keys)
1112 struct osd_device *dev = osd_dt_dev(d);
1115 dev->od_fl_capa = mode;
1116 dev->od_capa_timeout = timeout;
1117 dev->od_capa_alg = alg;
1118 dev->od_capa_keys = keys;
1123 * Concurrency: serialization provided by callers.
1125 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1126 struct dt_quota_ctxt *ctxt, void *data)
1128 struct obd_device *obd = (void *)ctxt;
1129 struct vfsmount *mnt = (struct vfsmount *)data;
1132 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1133 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1134 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1135 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1136 obd->obd_lvfs_ctxt.fs = get_ds();
1142 * Note: we do not count into QUOTA here.
1143 * If we mount with --data_journal we may need more.
1145 const int osd_dto_credits_noquota[DTO_NR] = {
1148 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1149 * SINGLEDATA_TRANS_BLOCKS(8)
1150 * XXX Note: maybe iam need more, since iam have more level than
1153 [DTO_INDEX_INSERT] = 16,
1154 [DTO_INDEX_DELETE] = 16,
1158 [DTO_INDEX_UPDATE] = 16,
1160 * Create a object. The same as create object in EXT3.
1161 * DATA_TRANS_BLOCKS(14) +
1162 * INDEX_EXTRA_BLOCKS(8) +
1163 * 3(inode bits, groups, GDT)
1165 [DTO_OBJECT_CREATE] = 25,
1167 * XXX: real credits to be fixed
1169 [DTO_OBJECT_DELETE] = 25,
1171 * Attr set credits (inode)
1173 [DTO_ATTR_SET_BASE] = 1,
1175 * Xattr set. The same as xattr of EXT3.
1176 * DATA_TRANS_BLOCKS(14)
1177 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1178 * are also counted in. Do not know why?
1180 [DTO_XATTR_SET] = 14,
1183 * credits for inode change during write.
1185 [DTO_WRITE_BASE] = 3,
1187 * credits for single block write.
1189 [DTO_WRITE_BLOCK] = 14,
1191 * Attr set credits for chown.
1192 * This is extra credits for setattr, and it is null without quota
1194 [DTO_ATTR_SET_CHOWN]= 0
1197 static const struct dt_device_operations osd_dt_ops = {
1198 .dt_root_get = osd_root_get,
1199 .dt_statfs = osd_statfs,
1200 .dt_trans_create = osd_trans_create,
1201 .dt_trans_start = osd_trans_start,
1202 .dt_trans_stop = osd_trans_stop,
1203 .dt_trans_cb_add = osd_trans_cb_add,
1204 .dt_conf_get = osd_conf_get,
1205 .dt_sync = osd_sync,
1207 .dt_commit_async = osd_commit_async,
1208 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1209 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1212 static void osd_object_read_lock(const struct lu_env *env,
1213 struct dt_object *dt, unsigned role)
1215 struct osd_object *obj = osd_dt_obj(dt);
1216 struct osd_thread_info *oti = osd_oti_get(env);
1218 LINVRNT(osd_invariant(obj));
1220 LASSERT(obj->oo_owner != env);
1221 cfs_down_read_nested(&obj->oo_sem, role);
1223 LASSERT(obj->oo_owner == NULL);
1227 static void osd_object_write_lock(const struct lu_env *env,
1228 struct dt_object *dt, unsigned role)
1230 struct osd_object *obj = osd_dt_obj(dt);
1231 struct osd_thread_info *oti = osd_oti_get(env);
1233 LINVRNT(osd_invariant(obj));
1235 LASSERT(obj->oo_owner != env);
1236 cfs_down_write_nested(&obj->oo_sem, role);
1238 LASSERT(obj->oo_owner == NULL);
1239 obj->oo_owner = env;
1243 static void osd_object_read_unlock(const struct lu_env *env,
1244 struct dt_object *dt)
1246 struct osd_object *obj = osd_dt_obj(dt);
1247 struct osd_thread_info *oti = osd_oti_get(env);
1249 LINVRNT(osd_invariant(obj));
1251 LASSERT(oti->oti_r_locks > 0);
1253 cfs_up_read(&obj->oo_sem);
1256 static void osd_object_write_unlock(const struct lu_env *env,
1257 struct dt_object *dt)
1259 struct osd_object *obj = osd_dt_obj(dt);
1260 struct osd_thread_info *oti = osd_oti_get(env);
1262 LINVRNT(osd_invariant(obj));
1264 LASSERT(obj->oo_owner == env);
1265 LASSERT(oti->oti_w_locks > 0);
1267 obj->oo_owner = NULL;
1268 cfs_up_write(&obj->oo_sem);
1271 static int osd_object_write_locked(const struct lu_env *env,
1272 struct dt_object *dt)
1274 struct osd_object *obj = osd_dt_obj(dt);
1276 LINVRNT(osd_invariant(obj));
1278 return obj->oo_owner == env;
1281 static int capa_is_sane(const struct lu_env *env,
1282 struct osd_device *dev,
1283 struct lustre_capa *capa,
1284 struct lustre_capa_key *keys)
1286 struct osd_thread_info *oti = osd_oti_get(env);
1287 struct lustre_capa *tcapa = &oti->oti_capa;
1288 struct obd_capa *oc;
1292 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1294 if (capa_is_expired(oc)) {
1295 DEBUG_CAPA(D_ERROR, capa, "expired");
1302 if (capa_is_expired_sec(capa)) {
1303 DEBUG_CAPA(D_ERROR, capa, "expired");
1307 cfs_spin_lock(&capa_lock);
1308 for (i = 0; i < 2; i++) {
1309 if (keys[i].lk_keyid == capa->lc_keyid) {
1310 oti->oti_capa_key = keys[i];
1314 cfs_spin_unlock(&capa_lock);
1317 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1321 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1325 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1326 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1330 oc = capa_add(dev->od_capa_hash, capa);
1336 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1337 struct lustre_capa *capa, __u64 opc)
1339 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1340 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1341 struct md_capainfo *ci;
1344 if (!dev->od_fl_capa)
1347 if (capa == BYPASS_CAPA)
1350 ci = md_capainfo(env);
1354 if (ci->mc_auth == LC_ID_NONE)
1358 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1362 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1363 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1368 if (!capa_opc_supported(capa, opc)) {
1369 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1373 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1374 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1381 static struct timespec *osd_inode_time(const struct lu_env *env,
1382 struct inode *inode, __u64 seconds)
1384 struct osd_thread_info *oti = osd_oti_get(env);
1385 struct timespec *t = &oti->oti_time;
1387 t->tv_sec = seconds;
1389 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1394 static void osd_inode_getattr(const struct lu_env *env,
1395 struct inode *inode, struct lu_attr *attr)
1397 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1398 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1399 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1401 attr->la_atime = LTIME_S(inode->i_atime);
1402 attr->la_mtime = LTIME_S(inode->i_mtime);
1403 attr->la_ctime = LTIME_S(inode->i_ctime);
1404 attr->la_mode = inode->i_mode;
1405 attr->la_size = i_size_read(inode);
1406 attr->la_blocks = inode->i_blocks;
1407 attr->la_uid = inode->i_uid;
1408 attr->la_gid = inode->i_gid;
1409 attr->la_flags = LDISKFS_I(inode)->i_flags;
1410 attr->la_nlink = inode->i_nlink;
1411 attr->la_rdev = inode->i_rdev;
1412 attr->la_blksize = ll_inode_blksize(inode);
1413 attr->la_blkbits = inode->i_blkbits;
1416 static int osd_attr_get(const struct lu_env *env,
1417 struct dt_object *dt,
1418 struct lu_attr *attr,
1419 struct lustre_capa *capa)
1421 struct osd_object *obj = osd_dt_obj(dt);
1423 LASSERT(dt_object_exists(dt));
1424 LINVRNT(osd_invariant(obj));
1426 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1429 cfs_spin_lock(&obj->oo_guard);
1430 osd_inode_getattr(env, obj->oo_inode, attr);
1431 cfs_spin_unlock(&obj->oo_guard);
1435 static int osd_declare_attr_set(const struct lu_env *env,
1436 struct dt_object *dt,
1437 const struct lu_attr *attr,
1438 struct thandle *handle)
1440 struct osd_thandle *oh;
1441 struct osd_object *obj;
1443 LASSERT(dt != NULL);
1444 LASSERT(handle != NULL);
1446 obj = osd_dt_obj(dt);
1447 LASSERT(osd_invariant(obj));
1449 oh = container_of0(handle, struct osd_thandle, ot_super);
1450 LASSERT(oh->ot_handle == NULL);
1452 OSD_DECLARE_OP(oh, attr_set);
1453 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1455 if (attr && attr->la_valid & LA_UID) {
1457 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1459 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1461 if (attr && attr->la_valid & LA_GID) {
1463 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1465 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1471 static int osd_inode_setattr(const struct lu_env *env,
1472 struct inode *inode, const struct lu_attr *attr)
1476 bits = attr->la_valid;
1478 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1480 if (bits & LA_ATIME)
1481 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1482 if (bits & LA_CTIME)
1483 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1484 if (bits & LA_MTIME)
1485 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1486 if (bits & LA_SIZE) {
1487 LDISKFS_I(inode)->i_disksize = attr->la_size;
1488 i_size_write(inode, attr->la_size);
1492 /* OSD should not change "i_blocks" which is used by quota.
1493 * "i_blocks" should be changed by ldiskfs only. */
1494 if (bits & LA_BLOCKS)
1495 inode->i_blocks = attr->la_blocks;
1498 inode->i_mode = (inode->i_mode & S_IFMT) |
1499 (attr->la_mode & ~S_IFMT);
1501 inode->i_uid = attr->la_uid;
1503 inode->i_gid = attr->la_gid;
1504 if (bits & LA_NLINK)
1505 inode->i_nlink = attr->la_nlink;
1507 inode->i_rdev = attr->la_rdev;
1509 if (bits & LA_FLAGS) {
1510 /* always keep S_NOCMTIME */
1511 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1517 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1519 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1520 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1525 if (attr->la_valid & LA_UID)
1526 iattr.ia_valid |= ATTR_UID;
1527 if (attr->la_valid & LA_GID)
1528 iattr.ia_valid |= ATTR_GID;
1529 iattr.ia_uid = attr->la_uid;
1530 iattr.ia_gid = attr->la_gid;
1532 rc = ll_vfs_dq_transfer(inode, &iattr);
1534 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1535 "enforcement enabled on the ldiskfs filesystem?",
1536 inode->i_sb->s_id, rc);
1543 static int osd_attr_set(const struct lu_env *env,
1544 struct dt_object *dt,
1545 const struct lu_attr *attr,
1546 struct thandle *handle,
1547 struct lustre_capa *capa)
1549 struct osd_object *obj = osd_dt_obj(dt);
1550 struct inode *inode;
1553 LASSERT(handle != NULL);
1554 LASSERT(dt_object_exists(dt));
1555 LASSERT(osd_invariant(obj));
1557 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1560 OSD_EXEC_OP(handle, attr_set);
1562 inode = obj->oo_inode;
1563 if (LDISKFS_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1564 LDISKFS_FEATURE_RO_COMPAT_QUOTA)) {
1565 rc = osd_quota_transfer(inode, attr);
1569 #ifdef HAVE_QUOTA_SUPPORT
1570 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1571 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1572 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1577 if (attr->la_valid & LA_UID)
1578 iattr.ia_valid |= ATTR_UID;
1579 if (attr->la_valid & LA_GID)
1580 iattr.ia_valid |= ATTR_GID;
1581 iattr.ia_uid = attr->la_uid;
1582 iattr.ia_gid = attr->la_gid;
1583 osd_push_ctxt(env, save);
1584 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1591 cfs_spin_lock(&obj->oo_guard);
1592 rc = osd_inode_setattr(env, inode, attr);
1593 cfs_spin_unlock(&obj->oo_guard);
1596 inode->i_sb->s_op->dirty_inode(inode);
1600 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1601 struct osd_object *obj,
1602 const char *name, const int namelen)
1604 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1607 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1609 struct dt_allocation_hint *hint,
1613 struct osd_device *osd = osd_obj2dev(obj);
1614 struct osd_thandle *oth;
1615 struct dt_object *parent = NULL;
1616 struct inode *inode;
1617 #ifdef HAVE_QUOTA_SUPPORT
1618 struct osd_ctxt *save = &info->oti_ctxt;
1621 LINVRNT(osd_invariant(obj));
1622 LASSERT(obj->oo_inode == NULL);
1623 LASSERT(obj->oo_hl_head == NULL);
1625 if (S_ISDIR(mode) && ldiskfs_pdo) {
1626 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1627 if (obj->oo_hl_head == NULL)
1631 oth = container_of(th, struct osd_thandle, ot_super);
1632 LASSERT(oth->ot_handle->h_transaction != NULL);
1634 if (hint && hint->dah_parent)
1635 parent = hint->dah_parent;
1637 #ifdef HAVE_QUOTA_SUPPORT
1638 osd_push_ctxt(info->oti_env, save);
1640 inode = ldiskfs_create_inode(oth->ot_handle,
1641 parent ? osd_dt_obj(parent)->oo_inode :
1642 osd_sb(osd)->s_root->d_inode,
1644 #ifdef HAVE_QUOTA_SUPPORT
1647 if (!IS_ERR(inode)) {
1648 /* Do not update file c/mtime in ldiskfs.
1649 * NB: don't need any lock because no contention at this
1651 inode->i_flags |= S_NOCMTIME;
1652 inode->i_state |= I_LUSTRE_NOSCRUB;
1653 obj->oo_inode = inode;
1656 if (obj->oo_hl_head != NULL) {
1657 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1658 obj->oo_hl_head = NULL;
1660 result = PTR_ERR(inode);
1662 LINVRNT(osd_invariant(obj));
1670 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1671 struct lu_attr *attr,
1672 struct dt_allocation_hint *hint,
1673 struct dt_object_format *dof,
1677 struct osd_thandle *oth;
1678 struct osd_device *osd = osd_obj2dev(obj);
1679 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1681 LASSERT(S_ISDIR(attr->la_mode));
1683 oth = container_of(th, struct osd_thandle, ot_super);
1684 LASSERT(oth->ot_handle->h_transaction != NULL);
1685 result = osd_mkfile(info, obj, mode, hint, th);
1686 if (result == 0 && osd->od_iop_mode == 0) {
1687 LASSERT(obj->oo_inode != NULL);
1689 * XXX uh-oh... call low-level iam function directly.
1692 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1693 sizeof (struct osd_fid_pack),
1699 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1700 struct lu_attr *attr,
1701 struct dt_allocation_hint *hint,
1702 struct dt_object_format *dof,
1706 struct osd_thandle *oth;
1707 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1709 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1711 LASSERT(S_ISREG(attr->la_mode));
1713 oth = container_of(th, struct osd_thandle, ot_super);
1714 LASSERT(oth->ot_handle->h_transaction != NULL);
1716 result = osd_mkfile(info, obj, mode, hint, th);
1718 LASSERT(obj->oo_inode != NULL);
1719 if (feat->dif_flags & DT_IND_VARKEY)
1720 result = iam_lvar_create(obj->oo_inode,
1721 feat->dif_keysize_max,
1723 feat->dif_recsize_max,
1726 result = iam_lfix_create(obj->oo_inode,
1727 feat->dif_keysize_max,
1729 feat->dif_recsize_max,
1736 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1737 struct lu_attr *attr,
1738 struct dt_allocation_hint *hint,
1739 struct dt_object_format *dof,
1742 LASSERT(S_ISREG(attr->la_mode));
1743 return osd_mkfile(info, obj, (attr->la_mode &
1744 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1747 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1748 struct lu_attr *attr,
1749 struct dt_allocation_hint *hint,
1750 struct dt_object_format *dof,
1753 LASSERT(S_ISLNK(attr->la_mode));
1754 return osd_mkfile(info, obj, (attr->la_mode &
1755 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1758 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1759 struct lu_attr *attr,
1760 struct dt_allocation_hint *hint,
1761 struct dt_object_format *dof,
1764 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1767 LINVRNT(osd_invariant(obj));
1768 LASSERT(obj->oo_inode == NULL);
1769 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1770 S_ISFIFO(mode) || S_ISSOCK(mode));
1772 result = osd_mkfile(info, obj, mode, hint, th);
1774 LASSERT(obj->oo_inode != NULL);
1776 * This inode should be marked dirty for i_rdev. Currently
1777 * that is done in the osd_attr_init().
1779 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1781 LINVRNT(osd_invariant(obj));
1785 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1787 struct dt_allocation_hint *hint,
1788 struct dt_object_format *dof,
1791 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1793 osd_obj_type_f result;
1809 result = osd_mk_index;
1820 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1821 struct dt_object *parent, cfs_umode_t child_mode)
1825 memset(ah, 0, sizeof(*ah));
1826 ah->dah_parent = parent;
1827 ah->dah_mode = child_mode;
1830 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1831 struct lu_attr *attr, struct dt_object_format *dof)
1833 struct inode *inode = obj->oo_inode;
1834 __u64 valid = attr->la_valid;
1837 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1839 if (dof->dof_type != DFT_NODE)
1840 attr->la_valid &= ~LA_RDEV;
1841 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1842 attr->la_valid &= ~LA_ATIME;
1843 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1844 attr->la_valid &= ~LA_CTIME;
1845 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1846 attr->la_valid &= ~LA_MTIME;
1848 if (LDISKFS_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1849 LDISKFS_FEATURE_RO_COMPAT_QUOTA)) {
1850 result = osd_quota_transfer(inode, attr);
1854 #ifdef HAVE_QUOTA_SUPPORT
1855 attr->la_valid &= ~(LA_UID | LA_GID);
1859 if (attr->la_valid != 0) {
1860 result = osd_inode_setattr(info->oti_env, inode, attr);
1862 * The osd_inode_setattr() should always succeed here. The
1863 * only error that could be returned is EDQUOT when we are
1864 * trying to change the UID or GID of the inode. However, this
1865 * should not happen since quota enforcement is no longer
1866 * enabled on ldiskfs (lquota takes care of it).
1868 LASSERTF(result == 0, "%d", result);
1869 inode->i_sb->s_op->dirty_inode(inode);
1872 attr->la_valid = valid;
1876 * Helper function for osd_object_create()
1878 * \retval 0, on success
1880 static int __osd_object_create(struct osd_thread_info *info,
1881 struct osd_object *obj, struct lu_attr *attr,
1882 struct dt_allocation_hint *hint,
1883 struct dt_object_format *dof,
1889 /* we drop umask so that permissions we pass are not affected */
1890 umask = current->fs->umask;
1891 current->fs->umask = 0;
1893 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1896 osd_attr_init(info, obj, attr, dof);
1897 osd_object_init0(obj);
1899 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1900 unlock_new_inode(obj->oo_inode);
1903 /* restore previous umask value */
1904 current->fs->umask = umask;
1910 * Helper function for osd_object_create()
1912 * \retval 0, on success
1914 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1915 const struct lu_fid *fid, struct thandle *th)
1917 struct osd_thread_info *info = osd_oti_get(env);
1918 struct osd_inode_id *id = &info->oti_id;
1919 struct osd_device *osd = osd_obj2dev(obj);
1920 struct md_ucred *uc = md_ucred(env);
1922 LASSERT(obj->oo_inode != NULL);
1923 LASSERT(uc != NULL);
1925 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1926 return osd_oi_insert(info, osd, fid, id, th);
1929 static int osd_declare_object_create(const struct lu_env *env,
1930 struct dt_object *dt,
1931 struct lu_attr *attr,
1932 struct dt_allocation_hint *hint,
1933 struct dt_object_format *dof,
1934 struct thandle *handle)
1936 struct osd_thandle *oh;
1938 LASSERT(handle != NULL);
1940 oh = container_of0(handle, struct osd_thandle, ot_super);
1941 LASSERT(oh->ot_handle == NULL);
1943 OSD_DECLARE_OP(oh, create);
1944 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1945 /* XXX: So far, only normal fid needs be inserted into the oi,
1946 * things could be changed later. Revise following code then. */
1947 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1948 OSD_DECLARE_OP(oh, insert);
1949 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1951 /* If this is directory, then we expect . and .. to be inserted as
1952 * well. The one directory block always needs to be created for the
1953 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1954 * block), there is no danger of needing a tree for the first block.
1956 if (attr && S_ISDIR(attr->la_mode)) {
1957 OSD_DECLARE_OP(oh, insert);
1958 OSD_DECLARE_OP(oh, insert);
1959 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1963 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1964 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1969 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1970 struct lu_attr *attr,
1971 struct dt_allocation_hint *hint,
1972 struct dt_object_format *dof,
1975 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1976 struct osd_object *obj = osd_dt_obj(dt);
1977 struct osd_thread_info *info = osd_oti_get(env);
1982 LINVRNT(osd_invariant(obj));
1983 LASSERT(!dt_object_exists(dt));
1984 LASSERT(osd_write_locked(env, obj));
1985 LASSERT(th != NULL);
1987 if (unlikely(fid_is_acct(fid)))
1988 /* Quota files can't be created from the kernel any more,
1989 * 'tune2fs -O quota' will take care of creating them */
1992 OSD_EXEC_OP(th, create);
1994 result = __osd_object_create(info, obj, attr, hint, dof, th);
1996 result = __osd_oi_insert(env, obj, fid, th);
1998 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1999 LASSERT(osd_invariant(obj));
2004 * Called to destroy on-disk representation of the object
2006 * Concurrency: must be locked
2008 static int osd_declare_object_destroy(const struct lu_env *env,
2009 struct dt_object *dt,
2012 struct osd_object *obj = osd_dt_obj(dt);
2013 struct inode *inode = obj->oo_inode;
2014 struct osd_thandle *oh;
2018 oh = container_of0(th, struct osd_thandle, ot_super);
2019 LASSERT(oh->ot_handle == NULL);
2022 OSD_DECLARE_OP(oh, destroy);
2023 OSD_DECLARE_OP(oh, delete);
2024 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2025 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2027 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
2028 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
2033 static int osd_object_destroy(const struct lu_env *env,
2034 struct dt_object *dt,
2037 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2038 struct osd_object *obj = osd_dt_obj(dt);
2039 struct inode *inode = obj->oo_inode;
2040 struct osd_device *osd = osd_obj2dev(obj);
2041 struct osd_thandle *oh;
2045 oh = container_of0(th, struct osd_thandle, ot_super);
2046 LASSERT(oh->ot_handle);
2048 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2050 if (unlikely(fid_is_acct(fid)))
2053 /* Parallel control for OI scrub. For most of cases, there is no
2054 * lock contention. So it will not affect unlink performance. */
2055 cfs_mutex_lock(&inode->i_mutex);
2056 if (S_ISDIR(inode->i_mode)) {
2057 LASSERT(osd_inode_unlinked(inode) ||
2058 inode->i_nlink == 1);
2059 cfs_spin_lock(&obj->oo_guard);
2061 cfs_spin_unlock(&obj->oo_guard);
2062 inode->i_sb->s_op->dirty_inode(inode);
2064 LASSERT(osd_inode_unlinked(inode));
2067 OSD_EXEC_OP(th, destroy);
2069 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2070 cfs_mutex_unlock(&inode->i_mutex);
2072 /* XXX: add to ext3 orphan list */
2073 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2075 /* not needed in the cache anymore */
2076 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2082 * Helper function for osd_xattr_set()
2084 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2085 const struct lu_buf *buf, const char *name, int fl)
2087 struct osd_object *obj = osd_dt_obj(dt);
2088 struct inode *inode = obj->oo_inode;
2089 struct osd_thread_info *info = osd_oti_get(env);
2090 struct dentry *dentry = &info->oti_child_dentry;
2094 LASSERT(dt_object_exists(dt));
2095 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2097 if (fl & LU_XATTR_REPLACE)
2098 fs_flags |= XATTR_REPLACE;
2100 if (fl & LU_XATTR_CREATE)
2101 fs_flags |= XATTR_CREATE;
2103 dentry->d_inode = inode;
2104 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2105 buf->lb_len, fs_flags);
2110 * Put the fid into lustre_mdt_attrs, and then place the structure
2111 * inode's ea. This fid should not be altered during the life time
2114 * \retval +ve, on success
2115 * \retval -ve, on error
2117 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2119 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2120 const struct lu_fid *fid)
2122 struct osd_thread_info *info = osd_oti_get(env);
2123 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2125 lustre_lma_init(mdt_attrs, fid);
2126 lustre_lma_swab(mdt_attrs);
2127 return __osd_xattr_set(env, dt,
2128 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2129 XATTR_NAME_LMA, LU_XATTR_CREATE);
2134 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2135 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2136 * To have compatilibility with 1.8 ldiskfs driver we need to have
2137 * magic number at start of fid data.
2138 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2141 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2142 const struct dt_rec *fid)
2144 param->edp_magic = LDISKFS_LUFID_MAGIC;
2145 param->edp_len = sizeof(struct lu_fid) + 1;
2147 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2148 (struct lu_fid *)fid);
2152 * Try to read the fid from inode ea into dt_rec, if return value
2153 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2155 * \param fid object fid.
2157 * \retval 0 on success
2159 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2160 __u32 ino, struct lu_fid *fid,
2161 struct osd_inode_id *id)
2163 struct osd_thread_info *info = osd_oti_get(env);
2164 struct inode *inode;
2167 osd_id_gen(id, ino, OSD_OII_NOGEN);
2168 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2170 RETURN(PTR_ERR(inode));
2177 * OSD layer object create function for interoperability mode (b11826).
2178 * This is mostly similar to osd_object_create(). Only difference being, fid is
2179 * inserted into inode ea here.
2181 * \retval 0, on success
2182 * \retval -ve, on error
2184 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2185 struct lu_attr *attr,
2186 struct dt_allocation_hint *hint,
2187 struct dt_object_format *dof,
2190 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2191 struct osd_object *obj = osd_dt_obj(dt);
2192 struct osd_thread_info *info = osd_oti_get(env);
2197 LASSERT(osd_invariant(obj));
2198 LASSERT(!dt_object_exists(dt));
2199 LASSERT(osd_write_locked(env, obj));
2200 LASSERT(th != NULL);
2202 if (unlikely(fid_is_acct(fid)))
2203 /* Quota files can't be created from the kernel any more,
2204 * 'tune2fs -O quota' will take care of creating them */
2207 OSD_EXEC_OP(th, create);
2209 result = __osd_object_create(info, obj, attr, hint, dof, th);
2210 /* objects under osd root shld have igif fid, so dont add fid EA */
2211 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2212 result = osd_ea_fid_set(env, dt, fid);
2215 result = __osd_oi_insert(env, obj, fid, th);
2217 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2218 LINVRNT(osd_invariant(obj));
2222 static int osd_declare_object_ref_add(const struct lu_env *env,
2223 struct dt_object *dt,
2224 struct thandle *handle)
2226 struct osd_thandle *oh;
2228 /* it's possible that object doesn't exist yet */
2229 LASSERT(handle != NULL);
2231 oh = container_of0(handle, struct osd_thandle, ot_super);
2232 LASSERT(oh->ot_handle == NULL);
2234 OSD_DECLARE_OP(oh, ref_add);
2235 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2241 * Concurrency: @dt is write locked.
2243 static int osd_object_ref_add(const struct lu_env *env,
2244 struct dt_object *dt, struct thandle *th)
2246 struct osd_object *obj = osd_dt_obj(dt);
2247 struct inode *inode = obj->oo_inode;
2249 LINVRNT(osd_invariant(obj));
2250 LASSERT(dt_object_exists(dt));
2251 LASSERT(osd_write_locked(env, obj));
2252 LASSERT(th != NULL);
2254 OSD_EXEC_OP(th, ref_add);
2257 * DIR_NLINK feature is set for compatibility reasons if:
2258 * 1) nlinks > LDISKFS_LINK_MAX, or
2259 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2261 * It is easier to always set this flag (rather than check and set),
2262 * since it has less overhead, and the superblock will be dirtied
2263 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2264 * do not actually care whether this flag is set or not.
2266 cfs_spin_lock(&obj->oo_guard);
2268 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2269 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2270 inode->i_nlink == 2)
2273 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2274 cfs_spin_unlock(&obj->oo_guard);
2275 inode->i_sb->s_op->dirty_inode(inode);
2276 LINVRNT(osd_invariant(obj));
2281 static int osd_declare_object_ref_del(const struct lu_env *env,
2282 struct dt_object *dt,
2283 struct thandle *handle)
2285 struct osd_thandle *oh;
2287 LASSERT(dt_object_exists(dt));
2288 LASSERT(handle != NULL);
2290 oh = container_of0(handle, struct osd_thandle, ot_super);
2291 LASSERT(oh->ot_handle == NULL);
2293 OSD_DECLARE_OP(oh, ref_del);
2294 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2300 * Concurrency: @dt is write locked.
2302 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2305 struct osd_object *obj = osd_dt_obj(dt);
2306 struct inode *inode = obj->oo_inode;
2308 LINVRNT(osd_invariant(obj));
2309 LASSERT(dt_object_exists(dt));
2310 LASSERT(osd_write_locked(env, obj));
2311 LASSERT(th != NULL);
2313 OSD_EXEC_OP(th, ref_del);
2315 cfs_spin_lock(&obj->oo_guard);
2316 LASSERT(inode->i_nlink > 0);
2318 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2319 * then the nlink count is 1. Don't let it be set to 0 or the directory
2320 * inode will be deleted incorrectly. */
2321 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2323 cfs_spin_unlock(&obj->oo_guard);
2324 inode->i_sb->s_op->dirty_inode(inode);
2325 LINVRNT(osd_invariant(obj));
2331 * Get the 64-bit version for an inode.
2333 static int osd_object_version_get(const struct lu_env *env,
2334 struct dt_object *dt, dt_obj_version_t *ver)
2336 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2338 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2339 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2340 *ver = LDISKFS_I(inode)->i_fs_version;
2345 * Concurrency: @dt is read locked.
2347 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2348 struct lu_buf *buf, const char *name,
2349 struct lustre_capa *capa)
2351 struct osd_object *obj = osd_dt_obj(dt);
2352 struct inode *inode = obj->oo_inode;
2353 struct osd_thread_info *info = osd_oti_get(env);
2354 struct dentry *dentry = &info->oti_obj_dentry;
2356 /* version get is not real XATTR but uses xattr API */
2357 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2358 /* for version we are just using xattr API but change inode
2360 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2361 osd_object_version_get(env, dt, buf->lb_buf);
2362 return sizeof(dt_obj_version_t);
2365 LASSERT(dt_object_exists(dt));
2366 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2367 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2369 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2372 dentry->d_inode = inode;
2373 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2377 static int osd_declare_xattr_set(const struct lu_env *env,
2378 struct dt_object *dt,
2379 const struct lu_buf *buf, const char *name,
2380 int fl, struct thandle *handle)
2382 struct osd_thandle *oh;
2384 LASSERT(handle != NULL);
2386 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2387 /* no credits for version */
2391 oh = container_of0(handle, struct osd_thandle, ot_super);
2392 LASSERT(oh->ot_handle == NULL);
2394 OSD_DECLARE_OP(oh, xattr_set);
2395 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2401 * Set the 64-bit version for object
2403 static void osd_object_version_set(const struct lu_env *env,
2404 struct dt_object *dt,
2405 dt_obj_version_t *new_version)
2407 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2409 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2410 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2412 LDISKFS_I(inode)->i_fs_version = *new_version;
2413 /** Version is set after all inode operations are finished,
2414 * so we should mark it dirty here */
2415 inode->i_sb->s_op->dirty_inode(inode);
2419 * Concurrency: @dt is write locked.
2421 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2422 const struct lu_buf *buf, const char *name, int fl,
2423 struct thandle *handle, struct lustre_capa *capa)
2425 LASSERT(handle != NULL);
2427 /* version set is not real XATTR */
2428 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2429 /* for version we are just using xattr API but change inode
2431 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2432 osd_object_version_set(env, dt, buf->lb_buf);
2433 return sizeof(dt_obj_version_t);
2436 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2439 OSD_EXEC_OP(handle, xattr_set);
2440 return __osd_xattr_set(env, dt, buf, name, fl);
2444 * Concurrency: @dt is read locked.
2446 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2447 struct lu_buf *buf, struct lustre_capa *capa)
2449 struct osd_object *obj = osd_dt_obj(dt);
2450 struct inode *inode = obj->oo_inode;
2451 struct osd_thread_info *info = osd_oti_get(env);
2452 struct dentry *dentry = &info->oti_obj_dentry;
2454 LASSERT(dt_object_exists(dt));
2455 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2456 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2458 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2461 dentry->d_inode = inode;
2462 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2465 static int osd_declare_xattr_del(const struct lu_env *env,
2466 struct dt_object *dt, const char *name,
2467 struct thandle *handle)
2469 struct osd_thandle *oh;
2471 LASSERT(dt_object_exists(dt));
2472 LASSERT(handle != NULL);
2474 oh = container_of0(handle, struct osd_thandle, ot_super);
2475 LASSERT(oh->ot_handle == NULL);
2477 OSD_DECLARE_OP(oh, xattr_set);
2478 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2484 * Concurrency: @dt is write locked.
2486 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2487 const char *name, struct thandle *handle,
2488 struct lustre_capa *capa)
2490 struct osd_object *obj = osd_dt_obj(dt);
2491 struct inode *inode = obj->oo_inode;
2492 struct osd_thread_info *info = osd_oti_get(env);
2493 struct dentry *dentry = &info->oti_obj_dentry;
2496 LASSERT(dt_object_exists(dt));
2497 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2498 LASSERT(osd_write_locked(env, obj));
2499 LASSERT(handle != NULL);
2501 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2504 OSD_EXEC_OP(handle, xattr_set);
2506 dentry->d_inode = inode;
2507 rc = inode->i_op->removexattr(dentry, name);
2511 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2512 struct dt_object *dt,
2513 struct lustre_capa *old,
2516 struct osd_thread_info *info = osd_oti_get(env);
2517 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2518 struct osd_object *obj = osd_dt_obj(dt);
2519 struct osd_device *dev = osd_obj2dev(obj);
2520 struct lustre_capa_key *key = &info->oti_capa_key;
2521 struct lustre_capa *capa = &info->oti_capa;
2522 struct obd_capa *oc;
2523 struct md_capainfo *ci;
2527 if (!dev->od_fl_capa)
2528 RETURN(ERR_PTR(-ENOENT));
2530 LASSERT(dt_object_exists(dt));
2531 LINVRNT(osd_invariant(obj));
2533 /* renewal sanity check */
2534 if (old && osd_object_auth(env, dt, old, opc))
2535 RETURN(ERR_PTR(-EACCES));
2537 ci = md_capainfo(env);
2539 RETURN(ERR_PTR(-ENOENT));
2541 switch (ci->mc_auth) {
2545 capa->lc_uid = obj->oo_inode->i_uid;
2546 capa->lc_gid = obj->oo_inode->i_gid;
2547 capa->lc_flags = LC_ID_PLAIN;
2549 case LC_ID_CONVERT: {
2552 s[0] = obj->oo_inode->i_uid;
2553 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2554 s[2] = obj->oo_inode->i_gid;
2555 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2556 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2558 RETURN(ERR_PTR(rc));
2560 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2561 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2562 capa->lc_flags = LC_ID_CONVERT;
2566 RETURN(ERR_PTR(-EINVAL));
2569 capa->lc_fid = *fid;
2571 capa->lc_flags |= dev->od_capa_alg << 24;
2572 capa->lc_timeout = dev->od_capa_timeout;
2573 capa->lc_expiry = 0;
2575 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2577 LASSERT(!capa_is_expired(oc));
2581 cfs_spin_lock(&capa_lock);
2582 *key = dev->od_capa_keys[1];
2583 cfs_spin_unlock(&capa_lock);
2585 capa->lc_keyid = key->lk_keyid;
2586 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2588 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2590 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2591 RETURN(ERR_PTR(rc));
2594 oc = capa_add(dev->od_capa_hash, capa);
2598 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2600 struct osd_object *obj = osd_dt_obj(dt);
2601 struct inode *inode = obj->oo_inode;
2602 struct osd_thread_info *info = osd_oti_get(env);
2603 struct dentry *dentry = &info->oti_obj_dentry;
2604 struct file *file = &info->oti_file;
2609 dentry->d_inode = inode;
2610 file->f_dentry = dentry;
2611 file->f_mapping = inode->i_mapping;
2612 file->f_op = inode->i_fop;
2613 LOCK_INODE_MUTEX(inode);
2614 rc = file->f_op->fsync(file, dentry, 0);
2615 UNLOCK_INODE_MUTEX(inode);
2619 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2622 struct osd_object *obj = osd_dt_obj(dt);
2625 *data = (void *)obj->oo_inode;
2633 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2634 const struct dt_index_features *feat)
2636 struct iam_descr *descr;
2638 if (osd_object_is_root(o))
2639 return feat == &dt_directory_features;
2641 LASSERT(o->oo_dir != NULL);
2643 descr = o->oo_dir->od_container.ic_descr;
2644 if (feat == &dt_directory_features) {
2645 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2651 feat->dif_keysize_min <= descr->id_key_size &&
2652 descr->id_key_size <= feat->dif_keysize_max &&
2653 feat->dif_recsize_min <= descr->id_rec_size &&
2654 descr->id_rec_size <= feat->dif_recsize_max &&
2655 !(feat->dif_flags & (DT_IND_VARKEY |
2656 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2657 ergo(feat->dif_flags & DT_IND_UPDATE,
2658 1 /* XXX check that object (and file system) is
2663 static int osd_iam_container_init(const struct lu_env *env,
2664 struct osd_object *obj,
2665 struct osd_directory *dir)
2667 struct iam_container *bag = &dir->od_container;
2670 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2674 result = iam_container_setup(bag);
2678 if (osd_obj2dev(obj)->od_iop_mode) {
2679 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2681 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2687 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2689 iam_container_fini(bag);
2696 * Concurrency: no external locking is necessary.
2698 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2699 const struct dt_index_features *feat)
2703 struct osd_object *obj = osd_dt_obj(dt);
2704 struct osd_device *osd = osd_obj2dev(obj);
2706 LINVRNT(osd_invariant(obj));
2707 LASSERT(dt_object_exists(dt));
2709 if (osd_object_is_root(obj)) {
2710 dt->do_index_ops = &osd_index_ea_ops;
2712 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2713 dt->do_index_ops = &osd_index_ea_ops;
2714 if (S_ISDIR(obj->oo_inode->i_mode))
2719 } else if (unlikely(feat == &dt_otable_features)) {
2720 dt->do_index_ops = &osd_otable_ops;
2722 } else if (feat == &dt_acct_features) {
2723 dt->do_index_ops = &osd_acct_index_ops;
2726 } else if (!osd_has_index(obj)) {
2727 struct osd_directory *dir;
2732 cfs_spin_lock(&obj->oo_guard);
2733 if (obj->oo_dir == NULL)
2737 * Concurrent thread allocated container data.
2740 cfs_spin_unlock(&obj->oo_guard);
2742 * Now, that we have container data, serialize its
2745 cfs_down_write(&obj->oo_ext_idx_sem);
2747 * recheck under lock.
2749 if (!osd_has_index(obj))
2750 result = osd_iam_container_init(env, obj, dir);
2753 cfs_up_write(&obj->oo_ext_idx_sem);
2761 if (result == 0 && skip_iam == 0) {
2762 if (!osd_iam_index_probe(env, obj, feat))
2765 LINVRNT(osd_invariant(obj));
2770 static int osd_otable_it_attr_get(const struct lu_env *env,
2771 struct dt_object *dt,
2772 struct lu_attr *attr,
2773 struct lustre_capa *capa)
2779 static const struct dt_object_operations osd_obj_ops = {
2780 .do_read_lock = osd_object_read_lock,
2781 .do_write_lock = osd_object_write_lock,
2782 .do_read_unlock = osd_object_read_unlock,
2783 .do_write_unlock = osd_object_write_unlock,
2784 .do_write_locked = osd_object_write_locked,
2785 .do_attr_get = osd_attr_get,
2786 .do_declare_attr_set = osd_declare_attr_set,
2787 .do_attr_set = osd_attr_set,
2788 .do_ah_init = osd_ah_init,
2789 .do_declare_create = osd_declare_object_create,
2790 .do_create = osd_object_create,
2791 .do_declare_destroy = osd_declare_object_destroy,
2792 .do_destroy = osd_object_destroy,
2793 .do_index_try = osd_index_try,
2794 .do_declare_ref_add = osd_declare_object_ref_add,
2795 .do_ref_add = osd_object_ref_add,
2796 .do_declare_ref_del = osd_declare_object_ref_del,
2797 .do_ref_del = osd_object_ref_del,
2798 .do_xattr_get = osd_xattr_get,
2799 .do_declare_xattr_set = osd_declare_xattr_set,
2800 .do_xattr_set = osd_xattr_set,
2801 .do_declare_xattr_del = osd_declare_xattr_del,
2802 .do_xattr_del = osd_xattr_del,
2803 .do_xattr_list = osd_xattr_list,
2804 .do_capa_get = osd_capa_get,
2805 .do_object_sync = osd_object_sync,
2806 .do_data_get = osd_data_get,
2810 * dt_object_operations for interoperability mode
2811 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2813 static const struct dt_object_operations osd_obj_ea_ops = {
2814 .do_read_lock = osd_object_read_lock,
2815 .do_write_lock = osd_object_write_lock,
2816 .do_read_unlock = osd_object_read_unlock,
2817 .do_write_unlock = osd_object_write_unlock,
2818 .do_write_locked = osd_object_write_locked,
2819 .do_attr_get = osd_attr_get,
2820 .do_declare_attr_set = osd_declare_attr_set,
2821 .do_attr_set = osd_attr_set,
2822 .do_ah_init = osd_ah_init,
2823 .do_declare_create = osd_declare_object_create,
2824 .do_create = osd_object_ea_create,
2825 .do_declare_destroy = osd_declare_object_destroy,
2826 .do_destroy = osd_object_destroy,
2827 .do_index_try = osd_index_try,
2828 .do_declare_ref_add = osd_declare_object_ref_add,
2829 .do_ref_add = osd_object_ref_add,
2830 .do_declare_ref_del = osd_declare_object_ref_del,
2831 .do_ref_del = osd_object_ref_del,
2832 .do_xattr_get = osd_xattr_get,
2833 .do_declare_xattr_set = osd_declare_xattr_set,
2834 .do_xattr_set = osd_xattr_set,
2835 .do_declare_xattr_del = osd_declare_xattr_del,
2836 .do_xattr_del = osd_xattr_del,
2837 .do_xattr_list = osd_xattr_list,
2838 .do_capa_get = osd_capa_get,
2839 .do_object_sync = osd_object_sync,
2840 .do_data_get = osd_data_get,
2843 static const struct dt_object_operations osd_obj_otable_it_ops = {
2844 .do_attr_get = osd_otable_it_attr_get,
2845 .do_index_try = osd_index_try,
2848 static int osd_index_declare_iam_delete(const struct lu_env *env,
2849 struct dt_object *dt,
2850 const struct dt_key *key,
2851 struct thandle *handle)
2853 struct osd_thandle *oh;
2855 oh = container_of0(handle, struct osd_thandle, ot_super);
2856 LASSERT(oh->ot_handle == NULL);
2858 OSD_DECLARE_OP(oh, delete);
2859 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2865 * delete a (key, value) pair from index \a dt specified by \a key
2867 * \param dt osd index object
2868 * \param key key for index
2869 * \param rec record reference
2870 * \param handle transaction handler
2873 * \retval -ve failure
2876 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2877 const struct dt_key *key,
2878 struct thandle *handle,
2879 struct lustre_capa *capa)
2881 struct osd_object *obj = osd_dt_obj(dt);
2882 struct osd_thandle *oh;
2883 struct iam_path_descr *ipd;
2884 struct iam_container *bag = &obj->oo_dir->od_container;
2889 LINVRNT(osd_invariant(obj));
2890 LASSERT(dt_object_exists(dt));
2891 LASSERT(bag->ic_object == obj->oo_inode);
2892 LASSERT(handle != NULL);
2894 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2897 OSD_EXEC_OP(handle, delete);
2899 ipd = osd_idx_ipd_get(env, bag);
2900 if (unlikely(ipd == NULL))
2903 oh = container_of0(handle, struct osd_thandle, ot_super);
2904 LASSERT(oh->ot_handle != NULL);
2905 LASSERT(oh->ot_handle->h_transaction != NULL);
2907 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2908 osd_ipd_put(env, bag, ipd);
2909 LINVRNT(osd_invariant(obj));
2913 static int osd_index_declare_ea_delete(const struct lu_env *env,
2914 struct dt_object *dt,
2915 const struct dt_key *key,
2916 struct thandle *handle)
2918 struct osd_thandle *oh;
2920 LASSERT(dt_object_exists(dt));
2921 LASSERT(handle != NULL);
2923 oh = container_of0(handle, struct osd_thandle, ot_super);
2924 LASSERT(oh->ot_handle == NULL);
2926 OSD_DECLARE_OP(oh, delete);
2927 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2929 LASSERT(osd_dt_obj(dt)->oo_inode);
2930 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2931 osd_dt_obj(dt)->oo_inode);
2932 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2933 osd_dt_obj(dt)->oo_inode);
2938 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2941 struct osd_fid_pack *rec;
2944 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2945 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2946 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2952 * Index delete function for interoperability mode (b11826).
2953 * It will remove the directory entry added by osd_index_ea_insert().
2954 * This entry is needed to maintain name->fid mapping.
2956 * \param key, key i.e. file entry to be deleted
2958 * \retval 0, on success
2959 * \retval -ve, on error
2961 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2962 const struct dt_key *key,
2963 struct thandle *handle,
2964 struct lustre_capa *capa)
2966 struct osd_object *obj = osd_dt_obj(dt);
2967 struct inode *dir = obj->oo_inode;
2968 struct dentry *dentry;
2969 struct osd_thandle *oh;
2970 struct ldiskfs_dir_entry_2 *de;
2971 struct buffer_head *bh;
2972 struct htree_lock *hlock = NULL;
2977 LINVRNT(osd_invariant(obj));
2978 LASSERT(dt_object_exists(dt));
2979 LASSERT(handle != NULL);
2981 OSD_EXEC_OP(handle, delete);
2983 oh = container_of(handle, struct osd_thandle, ot_super);
2984 LASSERT(oh->ot_handle != NULL);
2985 LASSERT(oh->ot_handle->h_transaction != NULL);
2987 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2990 dentry = osd_child_dentry_get(env, obj,
2991 (char *)key, strlen((char *)key));
2993 if (obj->oo_hl_head != NULL) {
2994 hlock = osd_oti_get(env)->oti_hlock;
2995 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2996 dir, LDISKFS_HLOCK_DEL);
2998 cfs_down_write(&obj->oo_ext_idx_sem);
3001 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3003 rc = ldiskfs_delete_entry(oh->ot_handle,
3010 ldiskfs_htree_unlock(hlock);
3012 cfs_up_write(&obj->oo_ext_idx_sem);
3014 LASSERT(osd_invariant(obj));
3019 * Lookup index for \a key and copy record to \a rec.
3021 * \param dt osd index object
3022 * \param key key for index
3023 * \param rec record reference
3025 * \retval +ve success : exact mach
3026 * \retval 0 return record with key not greater than \a key
3027 * \retval -ve failure
3029 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3030 struct dt_rec *rec, const struct dt_key *key,
3031 struct lustre_capa *capa)
3033 struct osd_object *obj = osd_dt_obj(dt);
3034 struct iam_path_descr *ipd;
3035 struct iam_container *bag = &obj->oo_dir->od_container;
3036 struct osd_thread_info *oti = osd_oti_get(env);
3037 struct iam_iterator *it = &oti->oti_idx_it;
3038 struct iam_rec *iam_rec;
3043 LASSERT(osd_invariant(obj));
3044 LASSERT(dt_object_exists(dt));
3045 LASSERT(bag->ic_object == obj->oo_inode);
3047 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3050 ipd = osd_idx_ipd_get(env, bag);
3054 /* got ipd now we can start iterator. */
3055 iam_it_init(it, bag, 0, ipd);
3057 rc = iam_it_get(it, (struct iam_key *)key);
3059 if (S_ISDIR(obj->oo_inode->i_mode))
3060 iam_rec = (struct iam_rec *)oti->oti_ldp;
3062 iam_rec = (struct iam_rec *) rec;
3064 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3065 if (S_ISDIR(obj->oo_inode->i_mode))
3066 osd_fid_unpack((struct lu_fid *) rec,
3067 (struct osd_fid_pack *)iam_rec);
3071 osd_ipd_put(env, bag, ipd);
3073 LINVRNT(osd_invariant(obj));
3078 static int osd_index_declare_iam_insert(const struct lu_env *env,
3079 struct dt_object *dt,
3080 const struct dt_rec *rec,
3081 const struct dt_key *key,
3082 struct thandle *handle)
3084 struct osd_thandle *oh;
3086 LASSERT(dt_object_exists(dt));
3087 LASSERT(handle != NULL);
3089 oh = container_of0(handle, struct osd_thandle, ot_super);
3090 LASSERT(oh->ot_handle == NULL);
3092 OSD_DECLARE_OP(oh, insert);
3093 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3099 * Inserts (key, value) pair in \a dt index object.
3101 * \param dt osd index object
3102 * \param key key for index
3103 * \param rec record reference
3104 * \param th transaction handler
3107 * \retval -ve failure
3109 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3110 const struct dt_rec *rec,
3111 const struct dt_key *key, struct thandle *th,
3112 struct lustre_capa *capa, int ignore_quota)
3114 struct osd_object *obj = osd_dt_obj(dt);
3115 struct iam_path_descr *ipd;
3116 struct osd_thandle *oh;
3117 struct iam_container *bag = &obj->oo_dir->od_container;
3118 #ifdef HAVE_QUOTA_SUPPORT
3119 cfs_cap_t save = cfs_curproc_cap_pack();
3121 struct osd_thread_info *oti = osd_oti_get(env);
3122 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3127 LINVRNT(osd_invariant(obj));
3128 LASSERT(dt_object_exists(dt));
3129 LASSERT(bag->ic_object == obj->oo_inode);
3130 LASSERT(th != NULL);
3132 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3135 OSD_EXEC_OP(th, insert);
3137 ipd = osd_idx_ipd_get(env, bag);
3138 if (unlikely(ipd == NULL))
3141 oh = container_of0(th, struct osd_thandle, ot_super);
3142 LASSERT(oh->ot_handle != NULL);
3143 LASSERT(oh->ot_handle->h_transaction != NULL);
3144 #ifdef HAVE_QUOTA_SUPPORT
3146 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3148 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3150 if (S_ISDIR(obj->oo_inode->i_mode))
3151 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3153 iam_rec = (struct iam_rec *) rec;
3154 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3156 #ifdef HAVE_QUOTA_SUPPORT
3157 cfs_curproc_cap_unpack(save);
3159 osd_ipd_put(env, bag, ipd);
3160 LINVRNT(osd_invariant(obj));
3165 * Calls ldiskfs_add_entry() to add directory entry
3166 * into the directory. This is required for
3167 * interoperability mode (b11826)
3169 * \retval 0, on success
3170 * \retval -ve, on error
3172 static int __osd_ea_add_rec(struct osd_thread_info *info,
3173 struct osd_object *pobj, struct inode *cinode,
3174 const char *name, const struct dt_rec *fid,
3175 struct htree_lock *hlock, struct thandle *th)
3177 struct ldiskfs_dentry_param *ldp;
3178 struct dentry *child;
3179 struct osd_thandle *oth;
3182 oth = container_of(th, struct osd_thandle, ot_super);
3183 LASSERT(oth->ot_handle != NULL);
3184 LASSERT(oth->ot_handle->h_transaction != NULL);
3186 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3188 /* XXX: remove fid_is_igif() check here.
3189 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3190 * it is IGIF now but needs FID in dir entry as well for readdir
3192 * LU-838 should fix that and remove fid_is_igif() check */
3193 if (fid_is_igif((struct lu_fid *)fid) ||
3194 fid_is_norm((struct lu_fid *)fid)) {
3195 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3196 osd_get_ldiskfs_dirent_param(ldp, fid);
3197 child->d_fsdata = (void *)ldp;
3199 child->d_fsdata = NULL;
3201 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3207 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3208 * into the directory.Also sets flags into osd object to
3209 * indicate dot and dotdot are created. This is required for
3210 * interoperability mode (b11826)
3212 * \param dir directory for dot and dotdot fixup.
3213 * \param obj child object for linking
3215 * \retval 0, on success
3216 * \retval -ve, on error
3218 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3219 struct osd_object *dir,
3220 struct inode *parent_dir, const char *name,
3221 const struct dt_rec *dot_fid,
3222 const struct dt_rec *dot_dot_fid,
3225 struct inode *inode = dir->oo_inode;
3226 struct ldiskfs_dentry_param *dot_ldp;
3227 struct ldiskfs_dentry_param *dot_dot_ldp;
3228 struct osd_thandle *oth;
3231 oth = container_of(th, struct osd_thandle, ot_super);
3232 LASSERT(oth->ot_handle->h_transaction != NULL);
3233 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3235 if (strcmp(name, dot) == 0) {
3236 if (dir->oo_compat_dot_created) {
3239 LASSERT(inode == parent_dir);
3240 dir->oo_compat_dot_created = 1;
3243 } else if(strcmp(name, dotdot) == 0) {
3244 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3245 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3247 if (!dir->oo_compat_dot_created)
3249 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3250 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3251 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3256 /* in case of rename, dotdot is already created */
3257 if (dir->oo_compat_dotdot_created) {
3258 return __osd_ea_add_rec(info, dir, parent_dir, name,
3259 dot_dot_fid, NULL, th);
3262 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3263 inode, dot_ldp, dot_dot_ldp);
3265 dir->oo_compat_dotdot_created = 1;
3273 * It will call the appropriate osd_add* function and return the
3274 * value, return by respective functions.
3276 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3277 struct inode *cinode, const char *name,
3278 const struct dt_rec *fid, struct thandle *th)
3280 struct osd_thread_info *info = osd_oti_get(env);
3281 struct htree_lock *hlock;
3284 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3286 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3288 if (hlock != NULL) {
3289 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3292 cfs_down_write(&pobj->oo_ext_idx_sem);
3294 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3295 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3298 if (hlock != NULL) {
3299 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3300 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3302 cfs_down_write(&pobj->oo_ext_idx_sem);
3305 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3309 ldiskfs_htree_unlock(hlock);
3311 cfs_up_write(&pobj->oo_ext_idx_sem);
3317 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3318 struct osd_idmap_cache *oic)
3320 struct osd_scrub *scrub = &dev->od_scrub;
3321 struct lu_fid *fid = &oic->oic_fid;
3322 struct osd_inode_id *id = &oti->oti_id;
3328 rc = osd_oi_lookup(oti, dev, fid, id);
3329 if (rc != 0 && rc != -ENOENT)
3332 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3335 if (thread_is_running(&scrub->os_thread)) {
3336 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3337 /* There is race condition between osd_oi_lookup and OI scrub.
3338 * The OI scrub finished just after osd_oi_lookup() failure.
3339 * Under such case, it is unnecessary to trigger OI scrub again,
3340 * but try to call osd_oi_lookup() again. */
3341 if (unlikely(rc == -EAGAIN))
3347 if (!scrub->os_no_scrub && ++once == 1) {
3348 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3350 rc = osd_scrub_start(dev);
3351 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3353 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3359 RETURN(rc = -EREMCHG);
3363 * Calls ->lookup() to find dentry. From dentry get inode and
3364 * read inode's ea to get fid. This is required for interoperability
3367 * \retval 0, on success
3368 * \retval -ve, on error
3370 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3371 struct dt_rec *rec, const struct dt_key *key)
3373 struct inode *dir = obj->oo_inode;
3374 struct dentry *dentry;
3375 struct ldiskfs_dir_entry_2 *de;
3376 struct buffer_head *bh;
3377 struct lu_fid *fid = (struct lu_fid *) rec;
3378 struct htree_lock *hlock = NULL;
3382 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3384 dentry = osd_child_dentry_get(env, obj,
3385 (char *)key, strlen((char *)key));
3387 if (obj->oo_hl_head != NULL) {
3388 hlock = osd_oti_get(env)->oti_hlock;
3389 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3390 dir, LDISKFS_HLOCK_LOOKUP);
3392 cfs_down_read(&obj->oo_ext_idx_sem);
3395 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3397 struct osd_thread_info *oti = osd_oti_get(env);
3398 struct osd_idmap_cache *oic = &oti->oti_cache;
3399 struct osd_device *dev = osd_obj2dev(obj);
3400 struct osd_scrub *scrub = &dev->od_scrub;
3401 struct scrub_file *sf = &scrub->os_file;
3403 ino = le32_to_cpu(de->inode);
3404 rc = osd_get_fid_from_dentry(de, rec);
3406 /* done with de, release bh */
3409 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3411 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3413 if (rc != 0 || !fid_is_norm(fid))
3416 oic->oic_fid = *fid;
3417 if ((scrub->os_pos_current <= ino) &&
3418 (sf->sf_flags & SF_INCONSISTENT ||
3419 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3421 rc = osd_consistency_check(oti, dev, oic);
3430 ldiskfs_htree_unlock(hlock);
3432 cfs_up_read(&obj->oo_ext_idx_sem);
3437 * Find the osd object for given fid.
3439 * \param fid need to find the osd object having this fid
3441 * \retval osd_object on success
3442 * \retval -ve on error
3444 struct osd_object *osd_object_find(const struct lu_env *env,
3445 struct dt_object *dt,
3446 const struct lu_fid *fid)
3448 struct lu_device *ludev = dt->do_lu.lo_dev;
3449 struct osd_object *child = NULL;
3450 struct lu_object *luch;
3451 struct lu_object *lo;
3453 luch = lu_object_find(env, ludev, fid, NULL);
3454 if (!IS_ERR(luch)) {
3455 if (lu_object_exists(luch)) {
3456 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3458 child = osd_obj(lo);
3460 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3461 "lu_object can't be located"
3462 DFID"\n", PFID(fid));
3464 if (child == NULL) {
3465 lu_object_put(env, luch);
3466 CERROR("Unable to get osd_object\n");
3467 child = ERR_PTR(-ENOENT);
3470 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3471 "lu_object does not exists "DFID"\n",
3473 lu_object_put(env, luch);
3474 child = ERR_PTR(-ENOENT);
3477 child = (void *)luch;
3483 * Put the osd object once done with it.
3485 * \param obj osd object that needs to be put
3487 static inline void osd_object_put(const struct lu_env *env,
3488 struct osd_object *obj)
3490 lu_object_put(env, &obj->oo_dt.do_lu);
3493 static int osd_index_declare_ea_insert(const struct lu_env *env,
3494 struct dt_object *dt,
3495 const struct dt_rec *rec,
3496 const struct dt_key *key,
3497 struct thandle *handle)
3499 struct osd_thandle *oh;
3501 LASSERT(dt_object_exists(dt));
3502 LASSERT(handle != NULL);
3504 oh = container_of0(handle, struct osd_thandle, ot_super);
3505 LASSERT(oh->ot_handle == NULL);
3507 OSD_DECLARE_OP(oh, insert);
3508 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3510 LASSERT(osd_dt_obj(dt)->oo_inode);
3511 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3512 osd_dt_obj(dt)->oo_inode);
3513 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3514 osd_dt_obj(dt)->oo_inode);
3520 * Index add function for interoperability mode (b11826).
3521 * It will add the directory entry.This entry is needed to
3522 * maintain name->fid mapping.
3524 * \param key it is key i.e. file entry to be inserted
3525 * \param rec it is value of given key i.e. fid
3527 * \retval 0, on success
3528 * \retval -ve, on error
3530 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3531 const struct dt_rec *rec,
3532 const struct dt_key *key, struct thandle *th,
3533 struct lustre_capa *capa, int ignore_quota)
3535 struct osd_object *obj = osd_dt_obj(dt);
3536 struct lu_fid *fid = (struct lu_fid *) rec;
3537 const char *name = (const char *)key;
3538 struct osd_object *child;
3539 #ifdef HAVE_QUOTA_SUPPORT
3540 cfs_cap_t save = cfs_curproc_cap_pack();
3546 LASSERT(osd_invariant(obj));
3547 LASSERT(dt_object_exists(dt));
3548 LASSERT(th != NULL);
3550 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3553 child = osd_object_find(env, dt, fid);
3554 if (!IS_ERR(child)) {
3555 #ifdef HAVE_QUOTA_SUPPORT
3557 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3559 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3561 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3562 #ifdef HAVE_QUOTA_SUPPORT
3563 cfs_curproc_cap_unpack(save);
3565 osd_object_put(env, child);
3567 rc = PTR_ERR(child);
3570 LASSERT(osd_invariant(obj));
3575 * Initialize osd Iterator for given osd index object.
3577 * \param dt osd index object
3580 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3581 struct dt_object *dt,
3583 struct lustre_capa *capa)
3585 struct osd_it_iam *it;
3586 struct osd_thread_info *oti = osd_oti_get(env);
3587 struct osd_object *obj = osd_dt_obj(dt);
3588 struct lu_object *lo = &dt->do_lu;
3589 struct iam_path_descr *ipd;
3590 struct iam_container *bag = &obj->oo_dir->od_container;
3592 LASSERT(lu_object_exists(lo));
3594 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3595 return ERR_PTR(-EACCES);
3598 ipd = osd_it_ipd_get(env, bag);
3599 if (likely(ipd != NULL)) {
3603 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3604 return (struct dt_it *)it;
3606 return ERR_PTR(-ENOMEM);
3610 * free given Iterator.
3613 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3615 struct osd_it_iam *it = (struct osd_it_iam *)di;
3616 struct osd_object *obj = it->oi_obj;
3618 iam_it_fini(&it->oi_it);
3619 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3620 lu_object_put(env, &obj->oo_dt.do_lu);
3624 * Move Iterator to record specified by \a key
3626 * \param di osd iterator
3627 * \param key key for index
3629 * \retval +ve di points to record with least key not larger than key
3630 * \retval 0 di points to exact matched key
3631 * \retval -ve failure
3634 static int osd_it_iam_get(const struct lu_env *env,
3635 struct dt_it *di, const struct dt_key *key)
3637 struct osd_it_iam *it = (struct osd_it_iam *)di;
3639 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3645 * \param di osd iterator
3648 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3650 struct osd_it_iam *it = (struct osd_it_iam *)di;
3652 iam_it_put(&it->oi_it);
3656 * Move iterator by one record
3658 * \param di osd iterator
3660 * \retval +1 end of container reached
3662 * \retval -ve failure
3665 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3667 struct osd_it_iam *it = (struct osd_it_iam *)di;
3669 return iam_it_next(&it->oi_it);
3673 * Return pointer to the key under iterator.
3676 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3677 const struct dt_it *di)
3679 struct osd_it_iam *it = (struct osd_it_iam *)di;
3681 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3685 * Return size of key under iterator (in bytes)
3688 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3690 struct osd_it_iam *it = (struct osd_it_iam *)di;
3692 return iam_it_key_size(&it->oi_it);
3695 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3696 int len, __u16 type)
3698 struct luda_type *lt;
3699 const unsigned align = sizeof(struct luda_type) - 1;
3701 /* check if file type is required */
3702 if (attr & LUDA_TYPE) {
3703 len = (len + align) & ~align;
3705 lt = (void *) ent->lde_name + len;
3706 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3707 ent->lde_attrs |= LUDA_TYPE;
3710 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3714 * build lu direct from backend fs dirent.
3717 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3718 struct lu_fid *fid, __u64 offset,
3719 char *name, __u16 namelen,
3720 __u16 type, __u32 attr)
3722 fid_cpu_to_le(&ent->lde_fid, fid);
3723 ent->lde_attrs = LUDA_FID;
3725 ent->lde_hash = cpu_to_le64(offset);
3726 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3728 strncpy(ent->lde_name, name, namelen);
3729 ent->lde_namelen = cpu_to_le16(namelen);
3731 /* append lustre attributes */
3732 osd_it_append_attrs(ent, attr, namelen, type);
3736 * Return pointer to the record under iterator.
3738 static int osd_it_iam_rec(const struct lu_env *env,
3739 const struct dt_it *di,
3740 struct dt_rec *dtrec, __u32 attr)
3742 struct osd_it_iam *it = (struct osd_it_iam *)di;
3743 struct osd_thread_info *info = osd_oti_get(env);
3746 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3747 const struct osd_fid_pack *rec;
3748 struct lu_fid *fid = &info->oti_fid;
3749 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3755 name = (char *)iam_it_key_get(&it->oi_it);
3757 RETURN(PTR_ERR(name));
3759 namelen = iam_it_key_size(&it->oi_it);
3761 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3763 RETURN(PTR_ERR(rec));
3765 rc = osd_fid_unpack(fid, rec);
3769 hash = iam_it_store(&it->oi_it);
3771 /* IAM does not store object type in IAM index (dir) */
3772 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3775 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3776 (struct iam_rec *)dtrec);
3783 * Returns cookie for current Iterator position.
3785 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3787 struct osd_it_iam *it = (struct osd_it_iam *)di;
3789 return iam_it_store(&it->oi_it);
3793 * Restore iterator from cookie.
3795 * \param di osd iterator
3796 * \param hash Iterator location cookie
3798 * \retval +ve di points to record with least key not larger than key.
3799 * \retval 0 di points to exact matched key
3800 * \retval -ve failure
3803 static int osd_it_iam_load(const struct lu_env *env,
3804 const struct dt_it *di, __u64 hash)
3806 struct osd_it_iam *it = (struct osd_it_iam *)di;
3808 return iam_it_load(&it->oi_it, hash);
3811 static const struct dt_index_operations osd_index_iam_ops = {
3812 .dio_lookup = osd_index_iam_lookup,
3813 .dio_declare_insert = osd_index_declare_iam_insert,
3814 .dio_insert = osd_index_iam_insert,
3815 .dio_declare_delete = osd_index_declare_iam_delete,
3816 .dio_delete = osd_index_iam_delete,
3818 .init = osd_it_iam_init,
3819 .fini = osd_it_iam_fini,
3820 .get = osd_it_iam_get,
3821 .put = osd_it_iam_put,
3822 .next = osd_it_iam_next,
3823 .key = osd_it_iam_key,
3824 .key_size = osd_it_iam_key_size,
3825 .rec = osd_it_iam_rec,
3826 .store = osd_it_iam_store,
3827 .load = osd_it_iam_load
3832 * Creates or initializes iterator context.
3834 * \retval struct osd_it_ea, iterator structure on success
3837 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3838 struct dt_object *dt,
3840 struct lustre_capa *capa)
3842 struct osd_object *obj = osd_dt_obj(dt);
3843 struct osd_thread_info *info = osd_oti_get(env);
3844 struct osd_it_ea *it = &info->oti_it_ea;
3845 struct lu_object *lo = &dt->do_lu;
3846 struct dentry *obj_dentry = &info->oti_it_dentry;
3848 LASSERT(lu_object_exists(lo));
3850 obj_dentry->d_inode = obj->oo_inode;
3851 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3852 obj_dentry->d_name.hash = 0;
3854 it->oie_rd_dirent = 0;
3855 it->oie_it_dirent = 0;
3856 it->oie_dirent = NULL;
3857 it->oie_buf = info->oti_it_ea_buf;
3859 it->oie_file.f_pos = 0;
3860 it->oie_file.f_dentry = obj_dentry;
3861 if (attr & LUDA_64BITHASH)
3862 it->oie_file.f_flags = O_64BITHASH;
3864 it->oie_file.f_flags = O_32BITHASH;
3865 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3866 it->oie_file.f_op = obj->oo_inode->i_fop;
3867 it->oie_file.private_data = NULL;
3869 RETURN((struct dt_it *) it);
3873 * Destroy or finishes iterator context.
3875 * \param di iterator structure to be destroyed
3877 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3879 struct osd_it_ea *it = (struct osd_it_ea *)di;
3880 struct osd_object *obj = it->oie_obj;
3881 struct inode *inode = obj->oo_inode;
3884 it->oie_file.f_op->release(inode, &it->oie_file);
3885 lu_object_put(env, &obj->oo_dt.do_lu);
3890 * It position the iterator at given key, so that next lookup continues from
3891 * that key Or it is similar to dio_it->load() but based on a key,
3892 * rather than file position.
3894 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3897 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3899 static int osd_it_ea_get(const struct lu_env *env,
3900 struct dt_it *di, const struct dt_key *key)
3902 struct osd_it_ea *it = (struct osd_it_ea *)di;
3905 LASSERT(((const char *)key)[0] == '\0');
3906 it->oie_file.f_pos = 0;
3907 it->oie_rd_dirent = 0;
3908 it->oie_it_dirent = 0;
3909 it->oie_dirent = NULL;
3917 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3922 * It is called internally by ->readdir(). It fills the
3923 * iterator's in-memory data structure with required
3924 * information i.e. name, namelen, rec_size etc.
3926 * \param buf in which information to be filled in.
3927 * \param name name of the file in given dir
3929 * \retval 0 on success
3930 * \retval 1 on buffer full
3932 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3933 loff_t offset, __u64 ino,
3936 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3937 struct osd_it_ea_dirent *ent = it->oie_dirent;
3938 struct lu_fid *fid = &ent->oied_fid;
3939 struct osd_fid_pack *rec;
3942 /* this should never happen */
3943 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3944 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3948 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3952 if (d_type & LDISKFS_DIRENT_LUFID) {
3953 rec = (struct osd_fid_pack*) (name + namelen + 1);
3955 if (osd_fid_unpack(fid, rec) != 0)
3958 d_type &= ~LDISKFS_DIRENT_LUFID;
3963 ent->oied_ino = ino;
3964 ent->oied_off = offset;
3965 ent->oied_namelen = namelen;
3966 ent->oied_type = d_type;
3968 memcpy(ent->oied_name, name, namelen);
3970 it->oie_rd_dirent++;
3971 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3976 * Calls ->readdir() to load a directory entry at a time
3977 * and stored it in iterator's in-memory data structure.
3979 * \param di iterator's in memory structure
3981 * \retval 0 on success
3982 * \retval -ve on error
3984 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3985 const struct dt_it *di)
3987 struct osd_it_ea *it = (struct osd_it_ea *)di;
3988 struct osd_object *obj = it->oie_obj;
3989 struct inode *inode = obj->oo_inode;
3990 struct htree_lock *hlock = NULL;
3994 it->oie_dirent = it->oie_buf;
3995 it->oie_rd_dirent = 0;
3997 if (obj->oo_hl_head != NULL) {
3998 hlock = osd_oti_get(env)->oti_hlock;
3999 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4000 inode, LDISKFS_HLOCK_READDIR);
4002 cfs_down_read(&obj->oo_ext_idx_sem);
4005 result = inode->i_fop->readdir(&it->oie_file, it,
4006 (filldir_t) osd_ldiskfs_filldir);
4009 ldiskfs_htree_unlock(hlock);
4011 cfs_up_read(&obj->oo_ext_idx_sem);
4013 if (it->oie_rd_dirent == 0) {
4016 it->oie_dirent = it->oie_buf;
4017 it->oie_it_dirent = 1;
4024 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4025 * to load a directory entry at a time and stored it in
4026 * iterator's in-memory data structure.
4028 * \param di iterator's in memory structure
4030 * \retval +ve iterator reached to end
4031 * \retval 0 iterator not reached to end
4032 * \retval -ve on error
4034 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4036 struct osd_it_ea *it = (struct osd_it_ea *)di;
4041 if (it->oie_it_dirent < it->oie_rd_dirent) {
4043 (void *) it->oie_dirent +
4044 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4045 it->oie_dirent->oied_namelen);
4046 it->oie_it_dirent++;
4049 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
4052 rc = osd_ldiskfs_it_fill(env, di);
4059 * Returns the key at current position from iterator's in memory structure.
4061 * \param di iterator's in memory structure
4063 * \retval key i.e. struct dt_key on success
4065 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4066 const struct dt_it *di)
4068 struct osd_it_ea *it = (struct osd_it_ea *)di;
4070 return (struct dt_key *)it->oie_dirent->oied_name;
4074 * Returns the key's size at current position from iterator's in memory structure.
4076 * \param di iterator's in memory structure
4078 * \retval key_size i.e. struct dt_key on success
4080 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4082 struct osd_it_ea *it = (struct osd_it_ea *)di;
4084 return it->oie_dirent->oied_namelen;
4089 * Returns the value (i.e. fid/igif) at current position from iterator's
4090 * in memory structure.
4092 * \param di struct osd_it_ea, iterator's in memory structure
4093 * \param attr attr requested for dirent.
4094 * \param lde lustre dirent
4096 * \retval 0 no error and \param lde has correct lustre dirent.
4097 * \retval -ve on error
4099 static inline int osd_it_ea_rec(const struct lu_env *env,
4100 const struct dt_it *di,
4101 struct dt_rec *dtrec, __u32 attr)
4103 struct osd_it_ea *it = (struct osd_it_ea *)di;
4104 struct osd_object *obj = it->oie_obj;
4105 struct osd_device *dev = osd_obj2dev(obj);
4106 struct osd_scrub *scrub = &dev->od_scrub;
4107 struct scrub_file *sf = &scrub->os_file;
4108 struct osd_thread_info *oti = osd_oti_get(env);
4109 struct osd_idmap_cache *oic = &oti->oti_cache;
4110 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4111 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4112 __u32 ino = it->oie_dirent->oied_ino;
4116 if (!fid_is_sane(fid)) {
4117 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4121 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4124 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4125 it->oie_dirent->oied_name,
4126 it->oie_dirent->oied_namelen,
4127 it->oie_dirent->oied_type, attr);
4129 if (!fid_is_norm(fid))
4132 oic->oic_fid = *fid;
4133 if ((scrub->os_pos_current <= ino) &&
4134 (sf->sf_flags & SF_INCONSISTENT ||
4135 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4136 rc = osd_consistency_check(oti, dev, oic);
4142 * Returns a cookie for current position of the iterator head, so that
4143 * user can use this cookie to load/start the iterator next time.
4145 * \param di iterator's in memory structure
4147 * \retval cookie for current position, on success
4149 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4151 struct osd_it_ea *it = (struct osd_it_ea *)di;
4153 return it->oie_dirent->oied_off;
4157 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4158 * to load a directory entry at a time and stored it i inn,
4159 * in iterator's in-memory data structure.
4161 * \param di struct osd_it_ea, iterator's in memory structure
4163 * \retval +ve on success
4164 * \retval -ve on error
4166 static int osd_it_ea_load(const struct lu_env *env,
4167 const struct dt_it *di, __u64 hash)
4169 struct osd_it_ea *it = (struct osd_it_ea *)di;
4173 it->oie_file.f_pos = hash;
4175 rc = osd_ldiskfs_it_fill(env, di);
4183 * Index lookup function for interoperability mode (b11826).
4185 * \param key, key i.e. file name to be searched
4187 * \retval +ve, on success
4188 * \retval -ve, on error
4190 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4191 struct dt_rec *rec, const struct dt_key *key,
4192 struct lustre_capa *capa)
4194 struct osd_object *obj = osd_dt_obj(dt);
4199 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4200 LINVRNT(osd_invariant(obj));
4202 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4205 rc = osd_ea_lookup_rec(env, obj, rec, key);
4213 * Index and Iterator operations for interoperability
4214 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4216 static const struct dt_index_operations osd_index_ea_ops = {
4217 .dio_lookup = osd_index_ea_lookup,
4218 .dio_declare_insert = osd_index_declare_ea_insert,
4219 .dio_insert = osd_index_ea_insert,
4220 .dio_declare_delete = osd_index_declare_ea_delete,
4221 .dio_delete = osd_index_ea_delete,
4223 .init = osd_it_ea_init,
4224 .fini = osd_it_ea_fini,
4225 .get = osd_it_ea_get,
4226 .put = osd_it_ea_put,
4227 .next = osd_it_ea_next,
4228 .key = osd_it_ea_key,
4229 .key_size = osd_it_ea_key_size,
4230 .rec = osd_it_ea_rec,
4231 .store = osd_it_ea_store,
4232 .load = osd_it_ea_load
4236 static void *osd_key_init(const struct lu_context *ctx,
4237 struct lu_context_key *key)
4239 struct osd_thread_info *info;
4241 OBD_ALLOC_PTR(info);
4243 return ERR_PTR(-ENOMEM);
4245 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4246 if (info->oti_it_ea_buf == NULL)
4249 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4251 info->oti_hlock = ldiskfs_htree_lock_alloc();
4252 if (info->oti_hlock == NULL)
4258 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4261 return ERR_PTR(-ENOMEM);
4264 static void osd_key_fini(const struct lu_context *ctx,
4265 struct lu_context_key *key, void* data)
4267 struct osd_thread_info *info = data;
4269 if (info->oti_hlock != NULL)
4270 ldiskfs_htree_lock_free(info->oti_hlock);
4271 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4275 static void osd_key_exit(const struct lu_context *ctx,
4276 struct lu_context_key *key, void *data)
4278 struct osd_thread_info *info = data;
4280 LASSERT(info->oti_r_locks == 0);
4281 LASSERT(info->oti_w_locks == 0);
4282 LASSERT(info->oti_txns == 0);
4285 /* type constructor/destructor: osd_type_init, osd_type_fini */
4286 LU_TYPE_INIT_FINI(osd, &osd_key);
4288 struct lu_context_key osd_key = {
4289 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4290 .lct_init = osd_key_init,
4291 .lct_fini = osd_key_fini,
4292 .lct_exit = osd_key_exit
4296 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4297 const char *name, struct lu_device *next)
4299 struct osd_device *osd = osd_dev(d);
4301 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4302 return osd_procfs_init(osd, name);
4305 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4309 osd_scrub_cleanup(env, o);
4312 fsfilt_put_ops(o->od_fsops);
4316 /* shutdown quota slave instance associated with the device */
4317 if (o->od_quota_slave != NULL) {
4318 qsd_fini(env, o->od_quota_slave);
4319 o->od_quota_slave = NULL;
4325 static int osd_mount(const struct lu_env *env,
4326 struct osd_device *o, struct lustre_cfg *cfg)
4328 struct lustre_mount_info *lmi;
4329 const char *dev = lustre_cfg_string(cfg, 0);
4330 struct lustre_disk_data *ldd;
4331 struct lustre_sb_info *lsi;
4336 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4337 if (o->od_fsops == NULL) {
4338 CERROR("Can't find fsfilt_ldiskfs\n");
4342 if (o->od_mount != NULL) {
4343 CERROR("Already mounted (%s)\n", dev);
4348 lmi = server_get_mount(dev);
4350 CERROR("Cannot get mount info for %s!\n", dev);
4354 LASSERT(lmi != NULL);
4355 /* save lustre_mount_info in dt_device */
4357 o->od_mnt = lmi->lmi_mnt;
4359 lsi = s2lsi(lmi->lmi_sb);
4362 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4364 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", dev);
4368 if (ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
4369 rc = osd_compat_init(o);
4371 CERROR("%s: can't initialize compats: %d\n", dev, rc);
4377 static struct lu_device *osd_device_fini(const struct lu_env *env,
4378 struct lu_device *d)
4383 osd_compat_fini(osd_dev(d));
4385 shrink_dcache_sb(osd_sb(osd_dev(d)));
4386 osd_sync(env, lu2dt_dev(d));
4388 rc = osd_procfs_fini(osd_dev(d));
4390 CERROR("proc fini error %d \n", rc);
4391 RETURN (ERR_PTR(rc));
4394 if (osd_dev(d)->od_mount)
4395 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4396 osd_dev(d)->od_mount->lmi_mnt);
4397 osd_dev(d)->od_mount = NULL;
4402 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4403 struct lu_device_type *t,
4404 struct lustre_cfg *cfg)
4406 struct lu_device *l;
4407 struct osd_device *o;
4413 result = dt_device_init(&o->od_dt_dev, t);
4416 l->ld_ops = &osd_lu_ops;
4417 o->od_dt_dev.dd_ops = &osd_dt_ops;
4418 cfs_spin_lock_init(&o->od_osfs_lock);
4419 cfs_mutex_init(&o->od_otable_mutex);
4420 o->od_osfs_age = cfs_time_shift_64(-1000);
4421 o->od_capa_hash = init_capa_hash();
4422 if (o->od_capa_hash == NULL) {
4423 dt_device_fini(&o->od_dt_dev);
4424 l = ERR_PTR(-ENOMEM);
4427 l = ERR_PTR(result);
4432 l = ERR_PTR(-ENOMEM);
4436 static struct lu_device *osd_device_free(const struct lu_env *env,
4437 struct lu_device *d)
4439 struct osd_device *o = osd_dev(d);
4442 cleanup_capa_hash(o->od_capa_hash);
4443 dt_device_fini(&o->od_dt_dev);
4448 static int osd_process_config(const struct lu_env *env,
4449 struct lu_device *d, struct lustre_cfg *cfg)
4451 struct osd_device *o = osd_dev(d);
4455 switch(cfg->lcfg_command) {
4457 err = osd_mount(env, o, cfg);
4460 err = osd_shutdown(env, o);
4469 static int osd_recovery_complete(const struct lu_env *env,
4470 struct lu_device *d)
4475 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4476 struct lu_device *dev)
4478 struct osd_device *osd = osd_dev(dev);
4482 /* 1. setup scrub, including OI files initialization */
4483 result = osd_scrub_setup(env, osd);
4487 /* 2. setup quota slave instance */
4488 osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
4489 osd->od_proc_entry);
4490 if (IS_ERR(osd->od_quota_slave)) {
4491 result = PTR_ERR(osd->od_quota_slave);
4492 osd->od_quota_slave = NULL;
4496 if (!lu_device_is_md(pdev))
4499 /* 3. setup local objects */
4500 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4504 static const struct lu_object_operations osd_lu_obj_ops = {
4505 .loo_object_init = osd_object_init,
4506 .loo_object_delete = osd_object_delete,
4507 .loo_object_release = osd_object_release,
4508 .loo_object_free = osd_object_free,
4509 .loo_object_print = osd_object_print,
4510 .loo_object_invariant = osd_object_invariant
4513 const struct lu_device_operations osd_lu_ops = {
4514 .ldo_object_alloc = osd_object_alloc,
4515 .ldo_process_config = osd_process_config,
4516 .ldo_recovery_complete = osd_recovery_complete,
4517 .ldo_prepare = osd_prepare,
4520 static const struct lu_device_type_operations osd_device_type_ops = {
4521 .ldto_init = osd_type_init,
4522 .ldto_fini = osd_type_fini,
4524 .ldto_start = osd_type_start,
4525 .ldto_stop = osd_type_stop,
4527 .ldto_device_alloc = osd_device_alloc,
4528 .ldto_device_free = osd_device_free,
4530 .ldto_device_init = osd_device_init,
4531 .ldto_device_fini = osd_device_fini
4534 static struct lu_device_type osd_device_type = {
4535 .ldt_tags = LU_DEVICE_DT,
4536 .ldt_name = LUSTRE_OSD_NAME,
4537 .ldt_ops = &osd_device_type_ops,
4538 .ldt_ctx_tags = LCT_LOCAL,
4542 * lprocfs legacy support.
4544 static struct obd_ops osd_obd_device_ops = {
4545 .o_owner = THIS_MODULE
4548 static int __init osd_mod_init(void)
4550 struct lprocfs_static_vars lvars;
4553 lprocfs_osd_init_vars(&lvars);
4554 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4555 LUSTRE_OSD_NAME, &osd_device_type);
4558 static void __exit osd_mod_exit(void)
4560 class_unregister_type(LUSTRE_OSD_NAME);
4563 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4564 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4565 MODULE_LICENSE("GPL");
4567 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);