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>
76 #ifdef HAVE_LDISKFS_PDO
78 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
79 "ldiskfs with parallel directory operations");
84 static const char dot[] = ".";
85 static const char dotdot[] = "..";
86 static const char remote_obj_dir[] = "REM_OBJ_DIR";
88 static const struct lu_object_operations osd_lu_obj_ops;
89 static const struct dt_object_operations osd_obj_ops;
90 static const struct dt_object_operations osd_obj_ea_ops;
91 static const struct dt_index_operations osd_index_iam_ops;
92 static const struct dt_index_operations osd_index_ea_ops;
94 static int osd_has_index(const struct osd_object *obj)
96 return obj->oo_dt.do_index_ops != NULL;
99 static int osd_object_invariant(const struct lu_object *l)
101 return osd_invariant(osd_obj(l));
104 #ifdef HAVE_QUOTA_SUPPORT
106 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
108 struct md_ucred *uc = md_ucred(env);
113 save->oc_uid = current_fsuid();
114 save->oc_gid = current_fsgid();
115 save->oc_cap = current_cap();
116 if ((tc = prepare_creds())) {
117 tc->fsuid = uc->mu_fsuid;
118 tc->fsgid = uc->mu_fsgid;
121 /* XXX not suboptimal */
122 cfs_curproc_cap_unpack(uc->mu_cap);
126 osd_pop_ctxt(struct osd_ctxt *save)
130 if ((tc = prepare_creds())) {
131 tc->fsuid = save->oc_uid;
132 tc->fsgid = save->oc_gid;
133 tc->cap_effective = save->oc_cap;
140 * Concurrency: doesn't matter
142 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
144 return osd_oti_get(env)->oti_r_locks > 0;
148 * Concurrency: doesn't matter
150 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
152 struct osd_thread_info *oti = osd_oti_get(env);
153 return oti->oti_w_locks > 0 && o->oo_owner == env;
157 * Concurrency: doesn't access mutable data
159 static int osd_root_get(const struct lu_env *env,
160 struct dt_device *dev, struct lu_fid *f)
162 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
166 static inline int osd_qid_type(struct osd_thandle *oh, int i)
168 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
171 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
173 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
176 void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
177 int type, uid_t id, struct inode *inode)
180 int i, allocated = 0;
181 struct osd_object *obj;
185 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
188 /* id entry is allocated in the quota file */
189 if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
192 for (i = 0; i < oh->ot_id_cnt; i++) {
193 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
197 if (unlikely(i >= OSD_MAX_UGID_CNT)) {
198 CERROR("more than %d uid/gids for a transaction?\n", i);
202 oh->ot_id_array[i] = id;
203 osd_qid_set_type(oh, i, type);
205 obj = osd_dt_obj(dt);
206 oh->ot_credits += (allocated || id == 0) ?
207 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
212 * OSD object methods.
216 * Concurrency: no concurrent access is possible that early in object
219 static struct lu_object *osd_object_alloc(const struct lu_env *env,
220 const struct lu_object_header *hdr,
223 struct osd_object *mo;
229 l = &mo->oo_dt.do_lu;
230 dt_object_init(&mo->oo_dt, NULL, d);
231 if (osd_dev(d)->od_iop_mode)
232 mo->oo_dt.do_ops = &osd_obj_ea_ops;
234 mo->oo_dt.do_ops = &osd_obj_ops;
236 l->lo_ops = &osd_lu_obj_ops;
237 cfs_init_rwsem(&mo->oo_sem);
238 cfs_init_rwsem(&mo->oo_ext_idx_sem);
239 cfs_spin_lock_init(&mo->oo_guard);
246 static int osd_get_lma(struct inode *inode, struct dentry *dentry,
247 struct lustre_mdt_attrs *lma)
251 dentry->d_inode = inode;
252 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
255 /* Check LMA compatibility */
256 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
257 CWARN("%.16s: unsupported incompat LMA feature(s) "
259 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
260 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
264 lustre_lma_swab(lma);
267 } else if (rc == 0) {
275 * retrieve object from backend ext fs.
277 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
278 struct osd_inode_id *id)
280 struct inode *inode = NULL;
282 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
284 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
285 id->oii_ino, PTR_ERR(inode));
286 } else if (id->oii_gen != OSD_OII_NOGEN &&
287 inode->i_generation != id->oii_gen) {
288 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
290 id->oii_ino, id->oii_gen, inode->i_generation);
292 inode = ERR_PTR(-ESTALE);
293 } else if (inode->i_nlink == 0) {
294 /* due to parallel readdir and unlink,
295 * we can have dead inode here. */
296 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
297 make_bad_inode(inode);
299 inode = ERR_PTR(-ESTALE);
300 } else if (is_bad_inode(inode)) {
301 CWARN("%s: bad inode: ino = %u\n",
302 dev->od_dt_dev.dd_lu_dev.ld_obd->obd_name, id->oii_ino);
304 inode = ERR_PTR(-ENOENT);
306 if (id->oii_gen == OSD_OII_NOGEN)
307 osd_id_gen(id, inode->i_ino, inode->i_generation);
309 /* Do not update file c/mtime in ldiskfs.
310 * NB: we don't have any lock to protect this because we don't
311 * have reference on osd_object now, but contention with
312 * another lookup + attr_set can't happen in the tiny window
313 * between if (...) and set S_NOCMTIME. */
314 if (!(inode->i_flags & S_NOCMTIME))
315 inode->i_flags |= S_NOCMTIME;
320 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
321 struct osd_inode_id *id, struct lu_fid *fid)
323 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
327 inode = osd_iget(info, dev, id);
331 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
333 *fid = lma->lma_self_fid;
334 } else if (rc == -ENODATA) {
335 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
343 static struct inode *
344 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
345 struct osd_inode_id *id, const struct lu_fid *fid)
347 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
351 inode = osd_iget(info, dev, id);
355 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
357 if (rc == -ENODATA) {
358 CDEBUG(D_LFSCK, "inconsistent obj: NULL, %lu, "DFID"\n",
359 inode->i_ino, PFID(fid));
366 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
367 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
368 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
370 return ERR_PTR(EREMCHG);
375 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
376 const struct lu_fid *fid,
377 const struct lu_object_conf *conf)
379 struct osd_thread_info *info;
380 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
381 struct osd_device *dev;
382 struct osd_idmap_cache *oic;
383 struct osd_inode_id *id;
385 struct osd_scrub *scrub;
386 struct scrub_file *sf;
391 LINVRNT(osd_invariant(obj));
392 LASSERT(obj->oo_inode == NULL);
393 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
396 scrub = &dev->od_scrub;
397 sf = &scrub->os_file;
398 info = osd_oti_get(env);
400 oic = &info->oti_cache;
403 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
406 if (fid_is_norm(fid)) {
407 /* Search order: 1. per-thread cache. */
408 if (lu_fid_eq(fid, &oic->oic_fid)) {
410 } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
411 /* Search order: 2. OI scrub pending list. */
412 result = osd_oii_lookup(dev, fid, id);
417 if (sf->sf_flags & SF_INCONSISTENT)
421 fid_zero(&oic->oic_fid);
422 /* Search order: 3. OI files. */
423 result = osd_oi_lookup(info, dev, fid, id);
424 if (result != 0 && result != -ENOENT)
427 /* If fid wasn't found in oi, inode-less object is created,
428 * for which lu_object_exists() returns false. This is used
429 * in a (frequent) case when objects are created as locking
430 * anchors or place holders for objects yet to be created. */
431 if (conf != NULL && conf->loc_flags & LOC_F_NEW) {
432 if (unlikely(result == 0))
433 GOTO(out, result = -EEXIST);
435 GOTO(out, result = 0);
438 if (result == -ENOENT) {
439 if (!fid_is_norm(fid) ||
440 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
442 GOTO(out, result = 0);
449 inode = osd_iget(info, dev, id);
451 inode = osd_iget_verify(info, dev, id, fid);
453 result = PTR_ERR(inode);
454 if (result == -ENOENT || result == -ESTALE) {
456 } else if (result == -EREMCHG) {
459 if (thread_is_running(&scrub->os_thread)) {
460 result = -EINPROGRESS;
461 } else if (!scrub->os_no_scrub) {
462 result = osd_scrub_start(dev);
463 LCONSOLE_ERROR("Trigger OI scrub by RPC for "
466 if (result == 0 || result == -EALREADY)
467 result = -EINPROGRESS;
476 obj->oo_inode = inode;
477 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
478 if (dev->od_iop_mode) {
479 obj->oo_compat_dot_created = 1;
480 obj->oo_compat_dotdot_created = 1;
483 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
484 GOTO(out, result = 0);
486 LASSERT(obj->oo_hl_head == NULL);
487 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
488 if (obj->oo_hl_head == NULL) {
489 obj->oo_inode = NULL;
491 GOTO(out, result = -ENOMEM);
493 GOTO(out, result = 0);
496 LINVRNT(osd_invariant(obj));
501 * Concurrency: shouldn't matter.
503 static void osd_object_init0(struct osd_object *obj)
505 LASSERT(obj->oo_inode != NULL);
506 obj->oo_dt.do_body_ops = &osd_body_ops;
507 obj->oo_dt.do_lu.lo_header->loh_attr |=
508 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
512 * Concurrency: no concurrent access is possible that early in object
515 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
516 const struct lu_object_conf *conf)
518 struct osd_object *obj = osd_obj(l);
521 LINVRNT(osd_invariant(obj));
523 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
524 obj->oo_dt.do_body_ops = &osd_body_ops_new;
526 if (obj->oo_inode != NULL)
527 osd_object_init0(obj);
529 LINVRNT(osd_invariant(obj));
534 * Concurrency: no concurrent access is possible that late in object
537 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
539 struct osd_object *obj = osd_obj(l);
541 LINVRNT(osd_invariant(obj));
543 dt_object_fini(&obj->oo_dt);
544 if (obj->oo_hl_head != NULL)
545 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
550 * Concurrency: no concurrent access is possible that late in object
553 static void osd_index_fini(struct osd_object *o)
555 struct iam_container *bag;
557 if (o->oo_dir != NULL) {
558 bag = &o->oo_dir->od_container;
559 if (o->oo_inode != NULL) {
560 if (bag->ic_object == o->oo_inode)
561 iam_container_fini(bag);
563 OBD_FREE_PTR(o->oo_dir);
569 * Concurrency: no concurrent access is possible that late in object
570 * life-cycle (for all existing callers, that is. New callers have to provide
571 * their own locking.)
573 static int osd_inode_unlinked(const struct inode *inode)
575 return inode->i_nlink == 0;
579 OSD_TXN_OI_DELETE_CREDITS = 20,
580 OSD_TXN_INODE_DELETE_CREDITS = 20
587 #if OSD_THANDLE_STATS
589 * Set time when the handle is allocated
591 static void osd_th_alloced(struct osd_thandle *oth)
593 oth->oth_alloced = cfs_time_current();
597 * Set time when the handle started
599 static void osd_th_started(struct osd_thandle *oth)
601 oth->oth_started = cfs_time_current();
605 * Helper function to convert time interval to microseconds packed in
606 * long int (default time units for the counter in "stats" initialized
607 * by lu_time_init() )
609 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
613 cfs_duration_usec(cfs_time_sub(end, start), &val);
614 return val.tv_sec * 1000000 + val.tv_usec;
618 * Check whether the we deal with this handle for too long.
620 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
621 cfs_time_t alloced, cfs_time_t started,
624 cfs_time_t now = cfs_time_current();
626 LASSERT(dev != NULL);
628 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
629 interval_to_usec(alloced, started));
630 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
631 interval_to_usec(started, closed));
632 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
633 interval_to_usec(closed, now));
635 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
636 CWARN("transaction handle %p was open for too long: "
638 "alloced "CFS_TIME_T" ,"
639 "started "CFS_TIME_T" ,"
640 "closed "CFS_TIME_T"\n",
641 oth, now, alloced, started, closed);
642 libcfs_debug_dumpstack(NULL);
646 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
648 cfs_time_t __closed = cfs_time_current(); \
649 cfs_time_t __alloced = oth->oth_alloced; \
650 cfs_time_t __started = oth->oth_started; \
653 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
656 #else /* OSD_THANDLE_STATS */
658 #define osd_th_alloced(h) do {} while(0)
659 #define osd_th_started(h) do {} while(0)
660 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
662 #endif /* OSD_THANDLE_STATS */
665 * Concurrency: doesn't access mutable data.
667 static int osd_param_is_sane(const struct osd_device *dev,
668 const struct thandle *th)
670 struct osd_thandle *oh;
671 oh = container_of0(th, struct osd_thandle, ot_super);
672 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
676 * Concurrency: shouldn't matter.
678 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
679 static void osd_trans_commit_cb(struct super_block *sb,
680 struct journal_callback *jcb, int error)
682 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
685 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
686 struct thandle *th = &oh->ot_super;
687 struct lu_device *lud = &th->th_dev->dd_lu_dev;
688 struct dt_txn_commit_cb *dcb, *tmp;
690 LASSERT(oh->ot_handle == NULL);
693 CERROR("transaction @0x%p commit error: %d\n", th, error);
695 dt_txn_hook_commit(th);
697 /* call per-transaction callbacks if any */
698 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
699 cfs_list_del_init(&dcb->dcb_linkage);
700 dcb->dcb_func(NULL, th, dcb, error);
703 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
707 lu_context_exit(&th->th_ctx);
708 lu_context_fini(&th->th_ctx);
712 static struct thandle *osd_trans_create(const struct lu_env *env,
715 struct osd_thread_info *oti = osd_oti_get(env);
716 struct osd_iobuf *iobuf = &oti->oti_iobuf;
717 struct osd_thandle *oh;
721 /* on pending IO in this thread should left from prev. request */
722 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
724 th = ERR_PTR(-ENOMEM);
725 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
730 th->th_tags = LCT_TX_HANDLE;
732 oti->oti_dev = osd_dt_dev(d);
733 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
740 * Concurrency: shouldn't matter.
742 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
745 struct osd_thread_info *oti = osd_oti_get(env);
746 struct osd_device *dev = osd_dt_dev(d);
748 struct osd_thandle *oh;
753 LASSERT(current->journal_info == NULL);
755 oh = container_of0(th, struct osd_thandle, ot_super);
757 LASSERT(oh->ot_handle == NULL);
759 rc = dt_txn_hook_start(env, d, th);
763 if (!osd_param_is_sane(dev, th)) {
764 CWARN("%s: too many transaction credits (%d > %d)\n",
765 d->dd_lu_dev.ld_obd->obd_name, oh->ot_credits,
766 osd_journal(dev)->j_max_transaction_buffers);
767 /* XXX Limit the credits to 'max_transaction_buffers', and
768 * let the underlying filesystem to catch the error if
769 * we really need so many credits.
771 * This should be removed when we can calculate the
772 * credits precisely. */
773 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
774 #ifdef OSD_TRACK_DECLARES
775 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
776 oh->ot_declare_attr_set, oh->ot_declare_punch,
777 oh->ot_declare_xattr_set);
778 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
779 oh->ot_declare_create, oh->ot_declare_ref_add,
780 oh->ot_declare_ref_del, oh->ot_declare_write);
781 CERROR(" insert: %d, delete: %d, destroy: %d\n",
782 oh->ot_declare_insert, oh->ot_declare_delete,
783 oh->ot_declare_destroy);
788 * XXX temporary stuff. Some abstraction layer should
791 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
795 LASSERT(oti->oti_txns == 0);
796 lu_context_init(&th->th_ctx, th->th_tags);
797 lu_context_enter(&th->th_ctx);
799 lu_device_get(&d->dd_lu_dev);
800 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
804 * XXX: current rule is that we first start tx,
805 * then lock object(s), but we can't use
806 * this rule for data (due to locking specifics
807 * in ldiskfs). also in long-term we'd like to
808 * use usually-used (locks;tx) ordering. so,
809 * UGLY thing is that we'll use one ordering for
810 * data (ofd) and reverse ordering for metadata
811 * (mdd). then at some point we'll fix the latter
813 if (lu_device_is_md(&d->dd_lu_dev)) {
814 LASSERT(oti->oti_r_locks == 0);
815 LASSERT(oti->oti_w_locks == 0);
828 * Concurrency: shouldn't matter.
830 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
833 struct osd_thandle *oh;
834 struct osd_thread_info *oti = osd_oti_get(env);
835 struct osd_iobuf *iobuf = &oti->oti_iobuf;
839 oh = container_of0(th, struct osd_thandle, ot_super);
841 if (oh->ot_handle != NULL) {
842 handle_t *hdl = oh->ot_handle;
844 hdl->h_sync = th->th_sync;
847 * add commit callback
848 * notice we don't do this in osd_trans_start()
849 * as underlying transaction can change during truncate
851 osd_journal_callback_set(hdl, osd_trans_commit_cb,
854 LASSERT(oti->oti_txns == 1);
857 * XXX: current rule is that we first start tx,
858 * then lock object(s), but we can't use
859 * this rule for data (due to locking specifics
860 * in ldiskfs). also in long-term we'd like to
861 * use usually-used (locks;tx) ordering. so,
862 * UGLY thing is that we'll use one ordering for
863 * data (ofd) and reverse ordering for metadata
864 * (mdd). then at some point we'll fix the latter
866 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
867 LASSERT(oti->oti_r_locks == 0);
868 LASSERT(oti->oti_w_locks == 0);
870 rc = dt_txn_hook_stop(env, th);
872 CERROR("Failure in transaction hook: %d\n", rc);
873 oh->ot_handle = NULL;
874 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
875 rc = ldiskfs_journal_stop(hdl));
877 CERROR("Failure to stop transaction: %d\n", rc);
882 /* as we want IO to journal and data IO be concurrent, we don't block
883 * awaiting data IO completion in osd_do_bio(), instead we wait here
884 * once transaction is submitted to the journal. all reqular requests
885 * don't do direct IO (except read/write), thus this wait_event becomes
888 * IMPORTANT: we have to wait till any IO submited by the thread is
889 * completed otherwise iobuf may be corrupted by different request
891 cfs_wait_event(iobuf->dr_wait,
892 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
894 rc = iobuf->dr_error;
899 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
901 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
904 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
910 * Called just before object is freed. Releases all resources except for
911 * object itself (that is released by osd_object_free()).
913 * Concurrency: no concurrent access is possible that late in object
916 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
918 struct osd_object *obj = osd_obj(l);
919 struct inode *inode = obj->oo_inode;
921 LINVRNT(osd_invariant(obj));
924 * If object is unlinked remove fid->ino mapping from object index.
930 obj->oo_inode = NULL;
935 * Concurrency: ->loo_object_release() is called under site spin-lock.
937 static void osd_object_release(const struct lu_env *env,
943 * Concurrency: shouldn't matter.
945 static int osd_object_print(const struct lu_env *env, void *cookie,
946 lu_printer_t p, const struct lu_object *l)
948 struct osd_object *o = osd_obj(l);
951 if (o->oo_dir != NULL)
952 d = o->oo_dir->od_container.ic_descr;
955 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
957 o->oo_inode ? o->oo_inode->i_ino : 0UL,
958 o->oo_inode ? o->oo_inode->i_generation : 0,
959 d ? d->id_ops->id_name : "plain");
963 * Concurrency: shouldn't matter.
965 int osd_statfs(const struct lu_env *env, struct dt_device *d,
966 struct obd_statfs *sfs)
968 struct osd_device *osd = osd_dt_dev(d);
969 struct super_block *sb = osd_sb(osd);
970 struct kstatfs *ksfs;
973 /* osd_lproc.c call this without env, allocate ksfs for that case */
974 if (unlikely(env == NULL)) {
979 ksfs = &osd_oti_get(env)->oti_ksfs;
982 cfs_spin_lock(&osd->od_osfs_lock);
984 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
985 result = ll_do_statfs(sb, ksfs);
986 if (likely(result == 0)) { /* N.B. statfs can't really fail */
987 osd->od_osfs_age = cfs_time_current_64();
988 statfs_pack(&osd->od_statfs, ksfs);
992 if (likely(result == 0))
993 *sfs = osd->od_statfs;
994 cfs_spin_unlock(&osd->od_osfs_lock);
996 if (unlikely(env == NULL))
1003 * Concurrency: doesn't access mutable data.
1005 static void osd_conf_get(const struct lu_env *env,
1006 const struct dt_device *dev,
1007 struct dt_device_param *param)
1009 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1012 * XXX should be taken from not-yet-existing fs abstraction layer.
1014 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1015 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1016 param->ddp_block_shift = sb->s_blocksize_bits;
1017 param->ddp_mntopts = 0;
1018 if (test_opt(sb, XATTR_USER))
1019 param->ddp_mntopts |= MNTOPT_USERXATTR;
1020 if (test_opt(sb, POSIX_ACL))
1021 param->ddp_mntopts |= MNTOPT_ACL;
1023 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1024 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1025 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1028 param->ddp_max_ea_size = sb->s_blocksize;
1033 * Helper function to get and fill the buffer with input values.
1035 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1039 buf = &osd_oti_get(env)->oti_buf;
1046 * Concurrency: shouldn't matter.
1048 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1050 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1051 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1055 * Start commit for OSD device.
1057 * An implementation of dt_commit_async method for OSD device.
1058 * Asychronously starts underlayng fs sync and thereby a transaction
1061 * \param env environment
1062 * \param d dt device
1064 * \see dt_device_operations
1066 static int osd_commit_async(const struct lu_env *env,
1067 struct dt_device *d)
1069 struct super_block *s = osd_sb(osd_dt_dev(d));
1072 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1073 RETURN(s->s_op->sync_fs(s, 0));
1077 * Concurrency: shouldn't matter.
1080 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1082 struct super_block *sb = osd_sb(osd_dt_dev(d));
1086 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1088 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1093 * Concurrency: serialization provided by callers.
1095 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1096 int mode, unsigned long timeout, __u32 alg,
1097 struct lustre_capa_key *keys)
1099 struct osd_device *dev = osd_dt_dev(d);
1102 dev->od_fl_capa = mode;
1103 dev->od_capa_timeout = timeout;
1104 dev->od_capa_alg = alg;
1105 dev->od_capa_keys = keys;
1110 * Concurrency: serialization provided by callers.
1112 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1113 struct dt_quota_ctxt *ctxt, void *data)
1115 struct obd_device *obd = (void *)ctxt;
1116 struct vfsmount *mnt = (struct vfsmount *)data;
1119 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1120 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1121 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1122 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1123 obd->obd_lvfs_ctxt.fs = get_ds();
1129 * Note: we do not count into QUOTA here.
1130 * If we mount with --data_journal we may need more.
1132 const int osd_dto_credits_noquota[DTO_NR] = {
1135 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1136 * SINGLEDATA_TRANS_BLOCKS(8)
1137 * XXX Note: maybe iam need more, since iam have more level than
1140 [DTO_INDEX_INSERT] = 16,
1141 [DTO_INDEX_DELETE] = 16,
1145 [DTO_INDEX_UPDATE] = 16,
1147 * Create a object. The same as create object in EXT3.
1148 * DATA_TRANS_BLOCKS(14) +
1149 * INDEX_EXTRA_BLOCKS(8) +
1150 * 3(inode bits, groups, GDT)
1152 [DTO_OBJECT_CREATE] = 25,
1154 * XXX: real credits to be fixed
1156 [DTO_OBJECT_DELETE] = 25,
1158 * Attr set credits (inode)
1160 [DTO_ATTR_SET_BASE] = 1,
1162 * Xattr set. The same as xattr of EXT3.
1163 * DATA_TRANS_BLOCKS(14)
1164 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1165 * are also counted in. Do not know why?
1167 [DTO_XATTR_SET] = 14,
1170 * credits for inode change during write.
1172 [DTO_WRITE_BASE] = 3,
1174 * credits for single block write.
1176 [DTO_WRITE_BLOCK] = 14,
1178 * Attr set credits for chown.
1179 * This is extra credits for setattr, and it is null without quota
1181 [DTO_ATTR_SET_CHOWN]= 0
1184 static const struct dt_device_operations osd_dt_ops = {
1185 .dt_root_get = osd_root_get,
1186 .dt_statfs = osd_statfs,
1187 .dt_trans_create = osd_trans_create,
1188 .dt_trans_start = osd_trans_start,
1189 .dt_trans_stop = osd_trans_stop,
1190 .dt_trans_cb_add = osd_trans_cb_add,
1191 .dt_conf_get = osd_conf_get,
1192 .dt_sync = osd_sync,
1194 .dt_commit_async = osd_commit_async,
1195 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1196 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1199 static void osd_object_read_lock(const struct lu_env *env,
1200 struct dt_object *dt, unsigned role)
1202 struct osd_object *obj = osd_dt_obj(dt);
1203 struct osd_thread_info *oti = osd_oti_get(env);
1205 LINVRNT(osd_invariant(obj));
1207 LASSERT(obj->oo_owner != env);
1208 cfs_down_read_nested(&obj->oo_sem, role);
1210 LASSERT(obj->oo_owner == NULL);
1214 static void osd_object_write_lock(const struct lu_env *env,
1215 struct dt_object *dt, unsigned role)
1217 struct osd_object *obj = osd_dt_obj(dt);
1218 struct osd_thread_info *oti = osd_oti_get(env);
1220 LINVRNT(osd_invariant(obj));
1222 LASSERT(obj->oo_owner != env);
1223 cfs_down_write_nested(&obj->oo_sem, role);
1225 LASSERT(obj->oo_owner == NULL);
1226 obj->oo_owner = env;
1230 static void osd_object_read_unlock(const struct lu_env *env,
1231 struct dt_object *dt)
1233 struct osd_object *obj = osd_dt_obj(dt);
1234 struct osd_thread_info *oti = osd_oti_get(env);
1236 LINVRNT(osd_invariant(obj));
1238 LASSERT(oti->oti_r_locks > 0);
1240 cfs_up_read(&obj->oo_sem);
1243 static void osd_object_write_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(obj->oo_owner == env);
1252 LASSERT(oti->oti_w_locks > 0);
1254 obj->oo_owner = NULL;
1255 cfs_up_write(&obj->oo_sem);
1258 static int osd_object_write_locked(const struct lu_env *env,
1259 struct dt_object *dt)
1261 struct osd_object *obj = osd_dt_obj(dt);
1263 LINVRNT(osd_invariant(obj));
1265 return obj->oo_owner == env;
1268 static int capa_is_sane(const struct lu_env *env,
1269 struct osd_device *dev,
1270 struct lustre_capa *capa,
1271 struct lustre_capa_key *keys)
1273 struct osd_thread_info *oti = osd_oti_get(env);
1274 struct lustre_capa *tcapa = &oti->oti_capa;
1275 struct obd_capa *oc;
1279 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1281 if (capa_is_expired(oc)) {
1282 DEBUG_CAPA(D_ERROR, capa, "expired");
1289 if (capa_is_expired_sec(capa)) {
1290 DEBUG_CAPA(D_ERROR, capa, "expired");
1294 cfs_spin_lock(&capa_lock);
1295 for (i = 0; i < 2; i++) {
1296 if (keys[i].lk_keyid == capa->lc_keyid) {
1297 oti->oti_capa_key = keys[i];
1301 cfs_spin_unlock(&capa_lock);
1304 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1308 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1312 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1313 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1317 oc = capa_add(dev->od_capa_hash, capa);
1323 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1324 struct lustre_capa *capa, __u64 opc)
1326 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1327 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1328 struct md_capainfo *ci;
1331 if (!dev->od_fl_capa)
1334 if (capa == BYPASS_CAPA)
1337 ci = md_capainfo(env);
1341 if (ci->mc_auth == LC_ID_NONE)
1345 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1349 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1350 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1355 if (!capa_opc_supported(capa, opc)) {
1356 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1360 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1361 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1368 static struct timespec *osd_inode_time(const struct lu_env *env,
1369 struct inode *inode, __u64 seconds)
1371 struct osd_thread_info *oti = osd_oti_get(env);
1372 struct timespec *t = &oti->oti_time;
1374 t->tv_sec = seconds;
1376 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1381 static void osd_inode_getattr(const struct lu_env *env,
1382 struct inode *inode, struct lu_attr *attr)
1384 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1385 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1386 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1388 attr->la_atime = LTIME_S(inode->i_atime);
1389 attr->la_mtime = LTIME_S(inode->i_mtime);
1390 attr->la_ctime = LTIME_S(inode->i_ctime);
1391 attr->la_mode = inode->i_mode;
1392 attr->la_size = i_size_read(inode);
1393 attr->la_blocks = inode->i_blocks;
1394 attr->la_uid = inode->i_uid;
1395 attr->la_gid = inode->i_gid;
1396 attr->la_flags = LDISKFS_I(inode)->i_flags;
1397 attr->la_nlink = inode->i_nlink;
1398 attr->la_rdev = inode->i_rdev;
1399 attr->la_blksize = ll_inode_blksize(inode);
1400 attr->la_blkbits = inode->i_blkbits;
1403 static int osd_attr_get(const struct lu_env *env,
1404 struct dt_object *dt,
1405 struct lu_attr *attr,
1406 struct lustre_capa *capa)
1408 struct osd_object *obj = osd_dt_obj(dt);
1410 LASSERT(dt_object_exists(dt));
1411 LINVRNT(osd_invariant(obj));
1413 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1416 cfs_spin_lock(&obj->oo_guard);
1417 osd_inode_getattr(env, obj->oo_inode, attr);
1418 cfs_spin_unlock(&obj->oo_guard);
1422 static int osd_declare_attr_set(const struct lu_env *env,
1423 struct dt_object *dt,
1424 const struct lu_attr *attr,
1425 struct thandle *handle)
1427 struct osd_thandle *oh;
1428 struct osd_object *obj;
1430 LASSERT(dt != NULL);
1431 LASSERT(handle != NULL);
1433 obj = osd_dt_obj(dt);
1434 LASSERT(osd_invariant(obj));
1436 oh = container_of0(handle, struct osd_thandle, ot_super);
1437 LASSERT(oh->ot_handle == NULL);
1439 OSD_DECLARE_OP(oh, attr_set);
1440 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1442 if (attr && attr->la_valid & LA_UID) {
1444 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1446 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1448 if (attr && attr->la_valid & LA_GID) {
1450 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1452 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1458 static int osd_inode_setattr(const struct lu_env *env,
1459 struct inode *inode, const struct lu_attr *attr)
1463 bits = attr->la_valid;
1465 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1467 if (bits & LA_ATIME)
1468 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1469 if (bits & LA_CTIME)
1470 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1471 if (bits & LA_MTIME)
1472 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1473 if (bits & LA_SIZE) {
1474 LDISKFS_I(inode)->i_disksize = attr->la_size;
1475 i_size_write(inode, attr->la_size);
1479 /* OSD should not change "i_blocks" which is used by quota.
1480 * "i_blocks" should be changed by ldiskfs only. */
1481 if (bits & LA_BLOCKS)
1482 inode->i_blocks = attr->la_blocks;
1485 inode->i_mode = (inode->i_mode & S_IFMT) |
1486 (attr->la_mode & ~S_IFMT);
1488 inode->i_uid = attr->la_uid;
1490 inode->i_gid = attr->la_gid;
1491 if (bits & LA_NLINK)
1492 inode->i_nlink = attr->la_nlink;
1494 inode->i_rdev = attr->la_rdev;
1496 if (bits & LA_FLAGS) {
1497 /* always keep S_NOCMTIME */
1498 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1504 static int osd_attr_set(const struct lu_env *env,
1505 struct dt_object *dt,
1506 const struct lu_attr *attr,
1507 struct thandle *handle,
1508 struct lustre_capa *capa)
1510 struct osd_object *obj = osd_dt_obj(dt);
1511 struct inode *inode;
1514 LASSERT(handle != NULL);
1515 LASSERT(dt_object_exists(dt));
1516 LASSERT(osd_invariant(obj));
1518 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1521 OSD_EXEC_OP(handle, attr_set);
1523 inode = obj->oo_inode;
1524 #ifdef HAVE_QUOTA_SUPPORT
1525 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1526 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1527 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1532 if (attr->la_valid & LA_UID)
1533 iattr.ia_valid |= ATTR_UID;
1534 if (attr->la_valid & LA_GID)
1535 iattr.ia_valid |= ATTR_GID;
1536 iattr.ia_uid = attr->la_uid;
1537 iattr.ia_gid = attr->la_gid;
1538 osd_push_ctxt(env, save);
1539 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1546 cfs_spin_lock(&obj->oo_guard);
1547 rc = osd_inode_setattr(env, inode, attr);
1548 cfs_spin_unlock(&obj->oo_guard);
1551 inode->i_sb->s_op->dirty_inode(inode);
1558 * XXX temporary solution.
1560 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1561 struct lu_attr *attr, struct thandle *th)
1566 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1567 struct lu_attr *attr, struct thandle *th)
1569 osd_object_init0(obj);
1570 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1571 unlock_new_inode(obj->oo_inode);
1575 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1576 struct osd_object *obj,
1577 const char *name, const int namelen)
1579 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1582 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1584 struct dt_allocation_hint *hint,
1588 struct osd_device *osd = osd_obj2dev(obj);
1589 struct osd_thandle *oth;
1590 struct dt_object *parent = NULL;
1591 struct inode *inode;
1592 #ifdef HAVE_QUOTA_SUPPORT
1593 struct osd_ctxt *save = &info->oti_ctxt;
1596 LINVRNT(osd_invariant(obj));
1597 LASSERT(obj->oo_inode == NULL);
1598 LASSERT(obj->oo_hl_head == NULL);
1600 if (S_ISDIR(mode) && ldiskfs_pdo) {
1601 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1602 if (obj->oo_hl_head == NULL)
1606 oth = container_of(th, struct osd_thandle, ot_super);
1607 LASSERT(oth->ot_handle->h_transaction != NULL);
1609 if (hint && hint->dah_parent)
1610 parent = hint->dah_parent;
1612 #ifdef HAVE_QUOTA_SUPPORT
1613 osd_push_ctxt(info->oti_env, save);
1615 inode = ldiskfs_create_inode(oth->ot_handle,
1616 parent ? osd_dt_obj(parent)->oo_inode :
1617 osd_sb(osd)->s_root->d_inode,
1619 #ifdef HAVE_QUOTA_SUPPORT
1622 if (!IS_ERR(inode)) {
1623 /* Do not update file c/mtime in ldiskfs.
1624 * NB: don't need any lock because no contention at this
1626 inode->i_flags |= S_NOCMTIME;
1627 inode->i_state |= I_LUSTRE_NOSCRUB;
1628 obj->oo_inode = inode;
1631 if (obj->oo_hl_head != NULL) {
1632 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1633 obj->oo_hl_head = NULL;
1635 result = PTR_ERR(inode);
1637 LINVRNT(osd_invariant(obj));
1645 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1646 struct lu_attr *attr,
1647 struct dt_allocation_hint *hint,
1648 struct dt_object_format *dof,
1652 struct osd_thandle *oth;
1653 struct osd_device *osd = osd_obj2dev(obj);
1654 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1656 LASSERT(S_ISDIR(attr->la_mode));
1658 oth = container_of(th, struct osd_thandle, ot_super);
1659 LASSERT(oth->ot_handle->h_transaction != NULL);
1660 result = osd_mkfile(info, obj, mode, hint, th);
1661 if (result == 0 && osd->od_iop_mode == 0) {
1662 LASSERT(obj->oo_inode != NULL);
1664 * XXX uh-oh... call low-level iam function directly.
1667 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1668 sizeof (struct osd_fid_pack),
1674 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1675 struct lu_attr *attr,
1676 struct dt_allocation_hint *hint,
1677 struct dt_object_format *dof,
1681 struct osd_thandle *oth;
1682 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1684 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1686 LASSERT(S_ISREG(attr->la_mode));
1688 oth = container_of(th, struct osd_thandle, ot_super);
1689 LASSERT(oth->ot_handle->h_transaction != NULL);
1691 result = osd_mkfile(info, obj, mode, hint, th);
1693 LASSERT(obj->oo_inode != NULL);
1694 if (feat->dif_flags & DT_IND_VARKEY)
1695 result = iam_lvar_create(obj->oo_inode,
1696 feat->dif_keysize_max,
1698 feat->dif_recsize_max,
1701 result = iam_lfix_create(obj->oo_inode,
1702 feat->dif_keysize_max,
1704 feat->dif_recsize_max,
1711 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1712 struct lu_attr *attr,
1713 struct dt_allocation_hint *hint,
1714 struct dt_object_format *dof,
1717 LASSERT(S_ISREG(attr->la_mode));
1718 return osd_mkfile(info, obj, (attr->la_mode &
1719 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1722 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1723 struct lu_attr *attr,
1724 struct dt_allocation_hint *hint,
1725 struct dt_object_format *dof,
1728 LASSERT(S_ISLNK(attr->la_mode));
1729 return osd_mkfile(info, obj, (attr->la_mode &
1730 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1733 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1734 struct lu_attr *attr,
1735 struct dt_allocation_hint *hint,
1736 struct dt_object_format *dof,
1739 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1742 LINVRNT(osd_invariant(obj));
1743 LASSERT(obj->oo_inode == NULL);
1744 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1745 S_ISFIFO(mode) || S_ISSOCK(mode));
1747 result = osd_mkfile(info, obj, mode, hint, th);
1749 LASSERT(obj->oo_inode != NULL);
1750 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1752 LINVRNT(osd_invariant(obj));
1756 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1758 struct dt_allocation_hint *hint,
1759 struct dt_object_format *dof,
1762 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1764 osd_obj_type_f result;
1780 result = osd_mk_index;
1791 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1792 struct dt_object *parent, cfs_umode_t child_mode)
1796 memset(ah, 0, sizeof(*ah));
1797 ah->dah_parent = parent;
1798 ah->dah_mode = child_mode;
1802 * Helper function for osd_object_create()
1804 * \retval 0, on success
1806 static int __osd_object_create(struct osd_thread_info *info,
1807 struct osd_object *obj, struct lu_attr *attr,
1808 struct dt_allocation_hint *hint,
1809 struct dt_object_format *dof,
1815 result = osd_create_pre(info, obj, attr, th);
1817 result = osd_create_type_f(dof->dof_type)(info, obj,
1818 attr, hint, dof, th);
1820 result = osd_create_post(info, obj, attr, th);
1826 * Helper function for osd_object_create()
1828 * \retval 0, on success
1830 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1831 const struct lu_fid *fid, struct thandle *th)
1833 struct osd_thread_info *info = osd_oti_get(env);
1834 struct osd_inode_id *id = &info->oti_id;
1835 struct osd_device *osd = osd_obj2dev(obj);
1836 struct md_ucred *uc = md_ucred(env);
1838 LASSERT(obj->oo_inode != NULL);
1839 LASSERT(uc != NULL);
1841 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1842 return osd_oi_insert(info, osd, fid, id, th);
1845 static int osd_declare_object_create(const struct lu_env *env,
1846 struct dt_object *dt,
1847 struct lu_attr *attr,
1848 struct dt_allocation_hint *hint,
1849 struct dt_object_format *dof,
1850 struct thandle *handle)
1852 struct osd_thandle *oh;
1854 LASSERT(handle != NULL);
1856 oh = container_of0(handle, struct osd_thandle, ot_super);
1857 LASSERT(oh->ot_handle == NULL);
1859 OSD_DECLARE_OP(oh, create);
1860 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1861 /* XXX: So far, only normal fid needs be inserted into the oi,
1862 * things could be changed later. Revise following code then. */
1863 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1864 OSD_DECLARE_OP(oh, insert);
1865 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1867 /* If this is directory, then we expect . and .. to be inserted as
1868 * well. The one directory block always needs to be created for the
1869 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1870 * block), there is no danger of needing a tree for the first block.
1872 if (attr && S_ISDIR(attr->la_mode)) {
1873 OSD_DECLARE_OP(oh, insert);
1874 OSD_DECLARE_OP(oh, insert);
1875 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1879 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1880 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1885 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1886 struct lu_attr *attr,
1887 struct dt_allocation_hint *hint,
1888 struct dt_object_format *dof,
1891 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1892 struct osd_object *obj = osd_dt_obj(dt);
1893 struct osd_thread_info *info = osd_oti_get(env);
1898 LINVRNT(osd_invariant(obj));
1899 LASSERT(!dt_object_exists(dt));
1900 LASSERT(osd_write_locked(env, obj));
1901 LASSERT(th != NULL);
1903 OSD_EXEC_OP(th, create);
1905 result = __osd_object_create(info, obj, attr, hint, dof, th);
1907 result = __osd_oi_insert(env, obj, fid, th);
1909 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1910 LASSERT(osd_invariant(obj));
1915 * Called to destroy on-disk representation of the object
1917 * Concurrency: must be locked
1919 static int osd_declare_object_destroy(const struct lu_env *env,
1920 struct dt_object *dt,
1923 struct osd_object *obj = osd_dt_obj(dt);
1924 struct inode *inode = obj->oo_inode;
1925 struct osd_thandle *oh;
1929 oh = container_of0(th, struct osd_thandle, ot_super);
1930 LASSERT(oh->ot_handle == NULL);
1933 OSD_DECLARE_OP(oh, destroy);
1934 OSD_DECLARE_OP(oh, delete);
1935 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
1936 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
1938 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
1939 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
1944 static int osd_object_destroy(const struct lu_env *env,
1945 struct dt_object *dt,
1948 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1949 struct osd_object *obj = osd_dt_obj(dt);
1950 struct inode *inode = obj->oo_inode;
1951 struct osd_device *osd = osd_obj2dev(obj);
1952 struct osd_thandle *oh;
1956 oh = container_of0(th, struct osd_thandle, ot_super);
1957 LASSERT(oh->ot_handle);
1959 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1961 /* Parallel control for OI scrub. For most of cases, there is no
1962 * lock contention. So it will not affect unlink performance. */
1963 cfs_mutex_lock(&inode->i_mutex);
1964 if (S_ISDIR(inode->i_mode)) {
1965 LASSERT(osd_inode_unlinked(inode) ||
1966 inode->i_nlink == 1);
1967 cfs_spin_lock(&obj->oo_guard);
1969 cfs_spin_unlock(&obj->oo_guard);
1970 inode->i_sb->s_op->dirty_inode(inode);
1972 LASSERT(osd_inode_unlinked(inode));
1975 OSD_EXEC_OP(th, destroy);
1977 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
1978 cfs_mutex_unlock(&inode->i_mutex);
1980 /* XXX: add to ext3 orphan list */
1981 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
1983 /* not needed in the cache anymore */
1984 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1990 * Helper function for osd_xattr_set()
1992 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1993 const struct lu_buf *buf, const char *name, int fl)
1995 struct osd_object *obj = osd_dt_obj(dt);
1996 struct inode *inode = obj->oo_inode;
1997 struct osd_thread_info *info = osd_oti_get(env);
1998 struct dentry *dentry = &info->oti_child_dentry;
2002 LASSERT(dt_object_exists(dt));
2003 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2005 if (fl & LU_XATTR_REPLACE)
2006 fs_flags |= XATTR_REPLACE;
2008 if (fl & LU_XATTR_CREATE)
2009 fs_flags |= XATTR_CREATE;
2011 dentry->d_inode = inode;
2012 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2013 buf->lb_len, fs_flags);
2018 * Put the fid into lustre_mdt_attrs, and then place the structure
2019 * inode's ea. This fid should not be altered during the life time
2022 * \retval +ve, on success
2023 * \retval -ve, on error
2025 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2027 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2028 const struct lu_fid *fid)
2030 struct osd_thread_info *info = osd_oti_get(env);
2031 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2033 lustre_lma_init(mdt_attrs, fid);
2034 lustre_lma_swab(mdt_attrs);
2035 return __osd_xattr_set(env, dt,
2036 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2037 XATTR_NAME_LMA, LU_XATTR_CREATE);
2042 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2043 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2044 * To have compatilibility with 1.8 ldiskfs driver we need to have
2045 * magic number at start of fid data.
2046 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2049 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2050 const struct dt_rec *fid)
2052 param->edp_magic = LDISKFS_LUFID_MAGIC;
2053 param->edp_len = sizeof(struct lu_fid) + 1;
2055 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2056 (struct lu_fid *)fid);
2060 * Try to read the fid from inode ea into dt_rec, if return value
2061 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2063 * \param fid object fid.
2065 * \retval 0 on success
2067 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2068 __u32 ino, struct lu_fid *fid,
2069 struct osd_inode_id *id)
2071 struct osd_thread_info *info = osd_oti_get(env);
2072 struct inode *inode;
2075 osd_id_gen(id, ino, OSD_OII_NOGEN);
2076 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2078 RETURN(PTR_ERR(inode));
2085 * OSD layer object create function for interoperability mode (b11826).
2086 * This is mostly similar to osd_object_create(). Only difference being, fid is
2087 * inserted into inode ea here.
2089 * \retval 0, on success
2090 * \retval -ve, on error
2092 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2093 struct lu_attr *attr,
2094 struct dt_allocation_hint *hint,
2095 struct dt_object_format *dof,
2098 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2099 struct osd_object *obj = osd_dt_obj(dt);
2100 struct osd_thread_info *info = osd_oti_get(env);
2105 LASSERT(osd_invariant(obj));
2106 LASSERT(!dt_object_exists(dt));
2107 LASSERT(osd_write_locked(env, obj));
2108 LASSERT(th != NULL);
2110 OSD_EXEC_OP(th, create);
2112 result = __osd_object_create(info, obj, attr, hint, dof, th);
2113 /* objects under osd root shld have igif fid, so dont add fid EA */
2114 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2115 result = osd_ea_fid_set(env, dt, fid);
2118 result = __osd_oi_insert(env, obj, fid, th);
2120 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2121 LINVRNT(osd_invariant(obj));
2125 static int osd_declare_object_ref_add(const struct lu_env *env,
2126 struct dt_object *dt,
2127 struct thandle *handle)
2129 struct osd_thandle *oh;
2131 /* it's possible that object doesn't exist yet */
2132 LASSERT(handle != NULL);
2134 oh = container_of0(handle, struct osd_thandle, ot_super);
2135 LASSERT(oh->ot_handle == NULL);
2137 OSD_DECLARE_OP(oh, ref_add);
2138 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2144 * Concurrency: @dt is write locked.
2146 static int osd_object_ref_add(const struct lu_env *env,
2147 struct dt_object *dt, struct thandle *th)
2149 struct osd_object *obj = osd_dt_obj(dt);
2150 struct inode *inode = obj->oo_inode;
2152 LINVRNT(osd_invariant(obj));
2153 LASSERT(dt_object_exists(dt));
2154 LASSERT(osd_write_locked(env, obj));
2155 LASSERT(th != NULL);
2157 OSD_EXEC_OP(th, ref_add);
2160 * DIR_NLINK feature is set for compatibility reasons if:
2161 * 1) nlinks > LDISKFS_LINK_MAX, or
2162 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2164 * It is easier to always set this flag (rather than check and set),
2165 * since it has less overhead, and the superblock will be dirtied
2166 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2167 * do not actually care whether this flag is set or not.
2169 cfs_spin_lock(&obj->oo_guard);
2171 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2172 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2173 inode->i_nlink == 2)
2176 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2177 cfs_spin_unlock(&obj->oo_guard);
2178 inode->i_sb->s_op->dirty_inode(inode);
2179 LINVRNT(osd_invariant(obj));
2184 static int osd_declare_object_ref_del(const struct lu_env *env,
2185 struct dt_object *dt,
2186 struct thandle *handle)
2188 struct osd_thandle *oh;
2190 LASSERT(dt_object_exists(dt));
2191 LASSERT(handle != NULL);
2193 oh = container_of0(handle, struct osd_thandle, ot_super);
2194 LASSERT(oh->ot_handle == NULL);
2196 OSD_DECLARE_OP(oh, ref_del);
2197 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2203 * Concurrency: @dt is write locked.
2205 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2208 struct osd_object *obj = osd_dt_obj(dt);
2209 struct inode *inode = obj->oo_inode;
2211 LINVRNT(osd_invariant(obj));
2212 LASSERT(dt_object_exists(dt));
2213 LASSERT(osd_write_locked(env, obj));
2214 LASSERT(th != NULL);
2216 OSD_EXEC_OP(th, ref_del);
2218 cfs_spin_lock(&obj->oo_guard);
2219 LASSERT(inode->i_nlink > 0);
2221 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2222 * then the nlink count is 1. Don't let it be set to 0 or the directory
2223 * inode will be deleted incorrectly. */
2224 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2226 cfs_spin_unlock(&obj->oo_guard);
2227 inode->i_sb->s_op->dirty_inode(inode);
2228 LINVRNT(osd_invariant(obj));
2234 * Get the 64-bit version for an inode.
2236 static int osd_object_version_get(const struct lu_env *env,
2237 struct dt_object *dt, dt_obj_version_t *ver)
2239 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2241 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2242 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2243 *ver = LDISKFS_I(inode)->i_fs_version;
2248 * Concurrency: @dt is read locked.
2250 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2251 struct lu_buf *buf, const char *name,
2252 struct lustre_capa *capa)
2254 struct osd_object *obj = osd_dt_obj(dt);
2255 struct inode *inode = obj->oo_inode;
2256 struct osd_thread_info *info = osd_oti_get(env);
2257 struct dentry *dentry = &info->oti_obj_dentry;
2259 /* version get is not real XATTR but uses xattr API */
2260 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2261 /* for version we are just using xattr API but change inode
2263 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2264 osd_object_version_get(env, dt, buf->lb_buf);
2265 return sizeof(dt_obj_version_t);
2268 LASSERT(dt_object_exists(dt));
2269 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2270 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2272 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2275 dentry->d_inode = inode;
2276 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2280 static int osd_declare_xattr_set(const struct lu_env *env,
2281 struct dt_object *dt,
2282 const struct lu_buf *buf, const char *name,
2283 int fl, struct thandle *handle)
2285 struct osd_thandle *oh;
2287 LASSERT(handle != NULL);
2289 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2290 /* no credits for version */
2294 oh = container_of0(handle, struct osd_thandle, ot_super);
2295 LASSERT(oh->ot_handle == NULL);
2297 OSD_DECLARE_OP(oh, xattr_set);
2298 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2304 * Set the 64-bit version for object
2306 static void osd_object_version_set(const struct lu_env *env,
2307 struct dt_object *dt,
2308 dt_obj_version_t *new_version)
2310 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2312 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2313 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2315 LDISKFS_I(inode)->i_fs_version = *new_version;
2316 /** Version is set after all inode operations are finished,
2317 * so we should mark it dirty here */
2318 inode->i_sb->s_op->dirty_inode(inode);
2322 * Concurrency: @dt is write locked.
2324 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2325 const struct lu_buf *buf, const char *name, int fl,
2326 struct thandle *handle, struct lustre_capa *capa)
2328 LASSERT(handle != NULL);
2330 /* version set is not real XATTR */
2331 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2332 /* for version we are just using xattr API but change inode
2334 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2335 osd_object_version_set(env, dt, buf->lb_buf);
2336 return sizeof(dt_obj_version_t);
2339 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2342 OSD_EXEC_OP(handle, xattr_set);
2343 return __osd_xattr_set(env, dt, buf, name, fl);
2347 * Concurrency: @dt is read locked.
2349 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2350 struct lu_buf *buf, struct lustre_capa *capa)
2352 struct osd_object *obj = osd_dt_obj(dt);
2353 struct inode *inode = obj->oo_inode;
2354 struct osd_thread_info *info = osd_oti_get(env);
2355 struct dentry *dentry = &info->oti_obj_dentry;
2357 LASSERT(dt_object_exists(dt));
2358 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2359 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2361 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2364 dentry->d_inode = inode;
2365 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2368 static int osd_declare_xattr_del(const struct lu_env *env,
2369 struct dt_object *dt, const char *name,
2370 struct thandle *handle)
2372 struct osd_thandle *oh;
2374 LASSERT(dt_object_exists(dt));
2375 LASSERT(handle != NULL);
2377 oh = container_of0(handle, struct osd_thandle, ot_super);
2378 LASSERT(oh->ot_handle == NULL);
2380 OSD_DECLARE_OP(oh, xattr_set);
2381 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2387 * Concurrency: @dt is write locked.
2389 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2390 const char *name, struct thandle *handle,
2391 struct lustre_capa *capa)
2393 struct osd_object *obj = osd_dt_obj(dt);
2394 struct inode *inode = obj->oo_inode;
2395 struct osd_thread_info *info = osd_oti_get(env);
2396 struct dentry *dentry = &info->oti_obj_dentry;
2399 LASSERT(dt_object_exists(dt));
2400 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2401 LASSERT(osd_write_locked(env, obj));
2402 LASSERT(handle != NULL);
2404 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2407 OSD_EXEC_OP(handle, xattr_set);
2409 dentry->d_inode = inode;
2410 rc = inode->i_op->removexattr(dentry, name);
2414 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2415 struct dt_object *dt,
2416 struct lustre_capa *old,
2419 struct osd_thread_info *info = osd_oti_get(env);
2420 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2421 struct osd_object *obj = osd_dt_obj(dt);
2422 struct osd_device *dev = osd_obj2dev(obj);
2423 struct lustre_capa_key *key = &info->oti_capa_key;
2424 struct lustre_capa *capa = &info->oti_capa;
2425 struct obd_capa *oc;
2426 struct md_capainfo *ci;
2430 if (!dev->od_fl_capa)
2431 RETURN(ERR_PTR(-ENOENT));
2433 LASSERT(dt_object_exists(dt));
2434 LINVRNT(osd_invariant(obj));
2436 /* renewal sanity check */
2437 if (old && osd_object_auth(env, dt, old, opc))
2438 RETURN(ERR_PTR(-EACCES));
2440 ci = md_capainfo(env);
2442 RETURN(ERR_PTR(-ENOENT));
2444 switch (ci->mc_auth) {
2448 capa->lc_uid = obj->oo_inode->i_uid;
2449 capa->lc_gid = obj->oo_inode->i_gid;
2450 capa->lc_flags = LC_ID_PLAIN;
2452 case LC_ID_CONVERT: {
2455 s[0] = obj->oo_inode->i_uid;
2456 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2457 s[2] = obj->oo_inode->i_gid;
2458 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2459 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2461 RETURN(ERR_PTR(rc));
2463 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2464 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2465 capa->lc_flags = LC_ID_CONVERT;
2469 RETURN(ERR_PTR(-EINVAL));
2472 capa->lc_fid = *fid;
2474 capa->lc_flags |= dev->od_capa_alg << 24;
2475 capa->lc_timeout = dev->od_capa_timeout;
2476 capa->lc_expiry = 0;
2478 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2480 LASSERT(!capa_is_expired(oc));
2484 cfs_spin_lock(&capa_lock);
2485 *key = dev->od_capa_keys[1];
2486 cfs_spin_unlock(&capa_lock);
2488 capa->lc_keyid = key->lk_keyid;
2489 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2491 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2493 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2494 RETURN(ERR_PTR(rc));
2497 oc = capa_add(dev->od_capa_hash, capa);
2501 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2503 struct osd_object *obj = osd_dt_obj(dt);
2504 struct inode *inode = obj->oo_inode;
2505 struct osd_thread_info *info = osd_oti_get(env);
2506 struct dentry *dentry = &info->oti_obj_dentry;
2507 struct file *file = &info->oti_file;
2512 dentry->d_inode = inode;
2513 file->f_dentry = dentry;
2514 file->f_mapping = inode->i_mapping;
2515 file->f_op = inode->i_fop;
2516 LOCK_INODE_MUTEX(inode);
2517 rc = file->f_op->fsync(file, dentry, 0);
2518 UNLOCK_INODE_MUTEX(inode);
2522 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2525 struct osd_object *obj = osd_dt_obj(dt);
2528 *data = (void *)obj->oo_inode;
2536 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2537 const struct dt_index_features *feat)
2539 struct iam_descr *descr;
2541 if (osd_object_is_root(o))
2542 return feat == &dt_directory_features;
2544 LASSERT(o->oo_dir != NULL);
2546 descr = o->oo_dir->od_container.ic_descr;
2547 if (feat == &dt_directory_features) {
2548 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2554 feat->dif_keysize_min <= descr->id_key_size &&
2555 descr->id_key_size <= feat->dif_keysize_max &&
2556 feat->dif_recsize_min <= descr->id_rec_size &&
2557 descr->id_rec_size <= feat->dif_recsize_max &&
2558 !(feat->dif_flags & (DT_IND_VARKEY |
2559 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2560 ergo(feat->dif_flags & DT_IND_UPDATE,
2561 1 /* XXX check that object (and file system) is
2566 static int osd_iam_container_init(const struct lu_env *env,
2567 struct osd_object *obj,
2568 struct osd_directory *dir)
2570 struct iam_container *bag = &dir->od_container;
2573 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2577 result = iam_container_setup(bag);
2581 if (osd_obj2dev(obj)->od_iop_mode) {
2582 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2584 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2590 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2592 iam_container_fini(bag);
2599 * Concurrency: no external locking is necessary.
2601 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2602 const struct dt_index_features *feat)
2606 struct osd_object *obj = osd_dt_obj(dt);
2607 struct osd_device *osd = osd_obj2dev(obj);
2609 LINVRNT(osd_invariant(obj));
2610 LASSERT(dt_object_exists(dt));
2612 if (osd_object_is_root(obj)) {
2613 dt->do_index_ops = &osd_index_ea_ops;
2615 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2616 dt->do_index_ops = &osd_index_ea_ops;
2617 if (S_ISDIR(obj->oo_inode->i_mode))
2622 } else if (unlikely(feat == &dt_otable_features)) {
2623 dt->do_index_ops = &osd_otable_ops;
2625 } else if (!osd_has_index(obj)) {
2626 struct osd_directory *dir;
2631 cfs_spin_lock(&obj->oo_guard);
2632 if (obj->oo_dir == NULL)
2636 * Concurrent thread allocated container data.
2639 cfs_spin_unlock(&obj->oo_guard);
2641 * Now, that we have container data, serialize its
2644 cfs_down_write(&obj->oo_ext_idx_sem);
2646 * recheck under lock.
2648 if (!osd_has_index(obj))
2649 result = osd_iam_container_init(env, obj, dir);
2652 cfs_up_write(&obj->oo_ext_idx_sem);
2660 if (result == 0 && ea_dir == 0) {
2661 if (!osd_iam_index_probe(env, obj, feat))
2664 LINVRNT(osd_invariant(obj));
2669 static const struct dt_object_operations osd_obj_ops = {
2670 .do_read_lock = osd_object_read_lock,
2671 .do_write_lock = osd_object_write_lock,
2672 .do_read_unlock = osd_object_read_unlock,
2673 .do_write_unlock = osd_object_write_unlock,
2674 .do_write_locked = osd_object_write_locked,
2675 .do_attr_get = osd_attr_get,
2676 .do_declare_attr_set = osd_declare_attr_set,
2677 .do_attr_set = osd_attr_set,
2678 .do_ah_init = osd_ah_init,
2679 .do_declare_create = osd_declare_object_create,
2680 .do_create = osd_object_create,
2681 .do_declare_destroy = osd_declare_object_destroy,
2682 .do_destroy = osd_object_destroy,
2683 .do_index_try = osd_index_try,
2684 .do_declare_ref_add = osd_declare_object_ref_add,
2685 .do_ref_add = osd_object_ref_add,
2686 .do_declare_ref_del = osd_declare_object_ref_del,
2687 .do_ref_del = osd_object_ref_del,
2688 .do_xattr_get = osd_xattr_get,
2689 .do_declare_xattr_set = osd_declare_xattr_set,
2690 .do_xattr_set = osd_xattr_set,
2691 .do_declare_xattr_del = osd_declare_xattr_del,
2692 .do_xattr_del = osd_xattr_del,
2693 .do_xattr_list = osd_xattr_list,
2694 .do_capa_get = osd_capa_get,
2695 .do_object_sync = osd_object_sync,
2696 .do_data_get = osd_data_get,
2700 * dt_object_operations for interoperability mode
2701 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2703 static const struct dt_object_operations osd_obj_ea_ops = {
2704 .do_read_lock = osd_object_read_lock,
2705 .do_write_lock = osd_object_write_lock,
2706 .do_read_unlock = osd_object_read_unlock,
2707 .do_write_unlock = osd_object_write_unlock,
2708 .do_write_locked = osd_object_write_locked,
2709 .do_attr_get = osd_attr_get,
2710 .do_declare_attr_set = osd_declare_attr_set,
2711 .do_attr_set = osd_attr_set,
2712 .do_ah_init = osd_ah_init,
2713 .do_declare_create = osd_declare_object_create,
2714 .do_create = osd_object_ea_create,
2715 .do_declare_destroy = osd_declare_object_destroy,
2716 .do_destroy = osd_object_destroy,
2717 .do_index_try = osd_index_try,
2718 .do_declare_ref_add = osd_declare_object_ref_add,
2719 .do_ref_add = osd_object_ref_add,
2720 .do_declare_ref_del = osd_declare_object_ref_del,
2721 .do_ref_del = osd_object_ref_del,
2722 .do_xattr_get = osd_xattr_get,
2723 .do_declare_xattr_set = osd_declare_xattr_set,
2724 .do_xattr_set = osd_xattr_set,
2725 .do_declare_xattr_del = osd_declare_xattr_del,
2726 .do_xattr_del = osd_xattr_del,
2727 .do_xattr_list = osd_xattr_list,
2728 .do_capa_get = osd_capa_get,
2729 .do_object_sync = osd_object_sync,
2730 .do_data_get = osd_data_get,
2733 static int osd_index_declare_iam_delete(const struct lu_env *env,
2734 struct dt_object *dt,
2735 const struct dt_key *key,
2736 struct thandle *handle)
2738 struct osd_thandle *oh;
2740 oh = container_of0(handle, struct osd_thandle, ot_super);
2741 LASSERT(oh->ot_handle == NULL);
2743 OSD_DECLARE_OP(oh, delete);
2744 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2750 * delete a (key, value) pair from index \a dt specified by \a key
2752 * \param dt osd index object
2753 * \param key key for index
2754 * \param rec record reference
2755 * \param handle transaction handler
2758 * \retval -ve failure
2761 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2762 const struct dt_key *key,
2763 struct thandle *handle,
2764 struct lustre_capa *capa)
2766 struct osd_object *obj = osd_dt_obj(dt);
2767 struct osd_thandle *oh;
2768 struct iam_path_descr *ipd;
2769 struct iam_container *bag = &obj->oo_dir->od_container;
2774 LINVRNT(osd_invariant(obj));
2775 LASSERT(dt_object_exists(dt));
2776 LASSERT(bag->ic_object == obj->oo_inode);
2777 LASSERT(handle != NULL);
2779 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2782 OSD_EXEC_OP(handle, delete);
2784 ipd = osd_idx_ipd_get(env, bag);
2785 if (unlikely(ipd == NULL))
2788 oh = container_of0(handle, struct osd_thandle, ot_super);
2789 LASSERT(oh->ot_handle != NULL);
2790 LASSERT(oh->ot_handle->h_transaction != NULL);
2792 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2793 osd_ipd_put(env, bag, ipd);
2794 LINVRNT(osd_invariant(obj));
2798 static int osd_index_declare_ea_delete(const struct lu_env *env,
2799 struct dt_object *dt,
2800 const struct dt_key *key,
2801 struct thandle *handle)
2803 struct osd_thandle *oh;
2805 LASSERT(dt_object_exists(dt));
2806 LASSERT(handle != NULL);
2808 oh = container_of0(handle, struct osd_thandle, ot_super);
2809 LASSERT(oh->ot_handle == NULL);
2811 OSD_DECLARE_OP(oh, delete);
2812 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2814 LASSERT(osd_dt_obj(dt)->oo_inode);
2815 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2816 osd_dt_obj(dt)->oo_inode);
2817 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2818 osd_dt_obj(dt)->oo_inode);
2823 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2826 struct osd_fid_pack *rec;
2829 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2830 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2831 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2837 * Index delete function for interoperability mode (b11826).
2838 * It will remove the directory entry added by osd_index_ea_insert().
2839 * This entry is needed to maintain name->fid mapping.
2841 * \param key, key i.e. file entry to be deleted
2843 * \retval 0, on success
2844 * \retval -ve, on error
2846 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2847 const struct dt_key *key,
2848 struct thandle *handle,
2849 struct lustre_capa *capa)
2851 struct osd_object *obj = osd_dt_obj(dt);
2852 struct inode *dir = obj->oo_inode;
2853 struct dentry *dentry;
2854 struct osd_thandle *oh;
2855 struct ldiskfs_dir_entry_2 *de;
2856 struct buffer_head *bh;
2857 struct htree_lock *hlock = NULL;
2862 LINVRNT(osd_invariant(obj));
2863 LASSERT(dt_object_exists(dt));
2864 LASSERT(handle != NULL);
2866 OSD_EXEC_OP(handle, delete);
2868 oh = container_of(handle, struct osd_thandle, ot_super);
2869 LASSERT(oh->ot_handle != NULL);
2870 LASSERT(oh->ot_handle->h_transaction != NULL);
2872 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2875 dentry = osd_child_dentry_get(env, obj,
2876 (char *)key, strlen((char *)key));
2878 if (obj->oo_hl_head != NULL) {
2879 hlock = osd_oti_get(env)->oti_hlock;
2880 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2881 dir, LDISKFS_HLOCK_DEL);
2883 cfs_down_write(&obj->oo_ext_idx_sem);
2886 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2888 rc = ldiskfs_delete_entry(oh->ot_handle,
2895 ldiskfs_htree_unlock(hlock);
2897 cfs_up_write(&obj->oo_ext_idx_sem);
2899 LASSERT(osd_invariant(obj));
2904 * Lookup index for \a key and copy record to \a rec.
2906 * \param dt osd index object
2907 * \param key key for index
2908 * \param rec record reference
2910 * \retval +ve success : exact mach
2911 * \retval 0 return record with key not greater than \a key
2912 * \retval -ve failure
2914 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2915 struct dt_rec *rec, const struct dt_key *key,
2916 struct lustre_capa *capa)
2918 struct osd_object *obj = osd_dt_obj(dt);
2919 struct iam_path_descr *ipd;
2920 struct iam_container *bag = &obj->oo_dir->od_container;
2921 struct osd_thread_info *oti = osd_oti_get(env);
2922 struct iam_iterator *it = &oti->oti_idx_it;
2923 struct iam_rec *iam_rec;
2928 LASSERT(osd_invariant(obj));
2929 LASSERT(dt_object_exists(dt));
2930 LASSERT(bag->ic_object == obj->oo_inode);
2932 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2935 ipd = osd_idx_ipd_get(env, bag);
2939 /* got ipd now we can start iterator. */
2940 iam_it_init(it, bag, 0, ipd);
2942 rc = iam_it_get(it, (struct iam_key *)key);
2944 if (S_ISDIR(obj->oo_inode->i_mode))
2945 iam_rec = (struct iam_rec *)oti->oti_ldp;
2947 iam_rec = (struct iam_rec *) rec;
2949 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2950 if (S_ISDIR(obj->oo_inode->i_mode))
2951 osd_fid_unpack((struct lu_fid *) rec,
2952 (struct osd_fid_pack *)iam_rec);
2956 osd_ipd_put(env, bag, ipd);
2958 LINVRNT(osd_invariant(obj));
2963 static int osd_index_declare_iam_insert(const struct lu_env *env,
2964 struct dt_object *dt,
2965 const struct dt_rec *rec,
2966 const struct dt_key *key,
2967 struct thandle *handle)
2969 struct osd_thandle *oh;
2971 LASSERT(dt_object_exists(dt));
2972 LASSERT(handle != NULL);
2974 oh = container_of0(handle, struct osd_thandle, ot_super);
2975 LASSERT(oh->ot_handle == NULL);
2977 OSD_DECLARE_OP(oh, insert);
2978 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
2984 * Inserts (key, value) pair in \a dt index object.
2986 * \param dt osd index object
2987 * \param key key for index
2988 * \param rec record reference
2989 * \param th transaction handler
2992 * \retval -ve failure
2994 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2995 const struct dt_rec *rec,
2996 const struct dt_key *key, struct thandle *th,
2997 struct lustre_capa *capa, int ignore_quota)
2999 struct osd_object *obj = osd_dt_obj(dt);
3000 struct iam_path_descr *ipd;
3001 struct osd_thandle *oh;
3002 struct iam_container *bag = &obj->oo_dir->od_container;
3003 #ifdef HAVE_QUOTA_SUPPORT
3004 cfs_cap_t save = cfs_curproc_cap_pack();
3006 struct osd_thread_info *oti = osd_oti_get(env);
3007 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3012 LINVRNT(osd_invariant(obj));
3013 LASSERT(dt_object_exists(dt));
3014 LASSERT(bag->ic_object == obj->oo_inode);
3015 LASSERT(th != NULL);
3017 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3020 OSD_EXEC_OP(th, insert);
3022 ipd = osd_idx_ipd_get(env, bag);
3023 if (unlikely(ipd == NULL))
3026 oh = container_of0(th, struct osd_thandle, ot_super);
3027 LASSERT(oh->ot_handle != NULL);
3028 LASSERT(oh->ot_handle->h_transaction != NULL);
3029 #ifdef HAVE_QUOTA_SUPPORT
3031 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3033 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3035 if (S_ISDIR(obj->oo_inode->i_mode))
3036 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3038 iam_rec = (struct iam_rec *) rec;
3039 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3041 #ifdef HAVE_QUOTA_SUPPORT
3042 cfs_curproc_cap_unpack(save);
3044 osd_ipd_put(env, bag, ipd);
3045 LINVRNT(osd_invariant(obj));
3050 * Calls ldiskfs_add_entry() to add directory entry
3051 * into the directory. This is required for
3052 * interoperability mode (b11826)
3054 * \retval 0, on success
3055 * \retval -ve, on error
3057 static int __osd_ea_add_rec(struct osd_thread_info *info,
3058 struct osd_object *pobj, struct inode *cinode,
3059 const char *name, const struct dt_rec *fid,
3060 struct htree_lock *hlock, struct thandle *th)
3062 struct ldiskfs_dentry_param *ldp;
3063 struct dentry *child;
3064 struct osd_thandle *oth;
3067 oth = container_of(th, struct osd_thandle, ot_super);
3068 LASSERT(oth->ot_handle != NULL);
3069 LASSERT(oth->ot_handle->h_transaction != NULL);
3071 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3073 /* XXX: remove fid_is_igif() check here.
3074 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3075 * it is IGIF now but needs FID in dir entry as well for readdir
3077 * LU-838 should fix that and remove fid_is_igif() check */
3078 if (fid_is_igif((struct lu_fid *)fid) ||
3079 fid_is_norm((struct lu_fid *)fid)) {
3080 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3081 osd_get_ldiskfs_dirent_param(ldp, fid);
3082 child->d_fsdata = (void *)ldp;
3084 child->d_fsdata = NULL;
3086 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3092 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3093 * into the directory.Also sets flags into osd object to
3094 * indicate dot and dotdot are created. This is required for
3095 * interoperability mode (b11826)
3097 * \param dir directory for dot and dotdot fixup.
3098 * \param obj child object for linking
3100 * \retval 0, on success
3101 * \retval -ve, on error
3103 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3104 struct osd_object *dir,
3105 struct inode *parent_dir, const char *name,
3106 const struct dt_rec *dot_fid,
3107 const struct dt_rec *dot_dot_fid,
3110 struct inode *inode = dir->oo_inode;
3111 struct ldiskfs_dentry_param *dot_ldp;
3112 struct ldiskfs_dentry_param *dot_dot_ldp;
3113 struct osd_thandle *oth;
3116 oth = container_of(th, struct osd_thandle, ot_super);
3117 LASSERT(oth->ot_handle->h_transaction != NULL);
3118 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3120 if (strcmp(name, dot) == 0) {
3121 if (dir->oo_compat_dot_created) {
3124 LASSERT(inode == parent_dir);
3125 dir->oo_compat_dot_created = 1;
3128 } else if(strcmp(name, dotdot) == 0) {
3129 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3130 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3132 if (!dir->oo_compat_dot_created)
3134 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3135 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3136 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3141 /* in case of rename, dotdot is already created */
3142 if (dir->oo_compat_dotdot_created) {
3143 return __osd_ea_add_rec(info, dir, parent_dir, name,
3144 dot_dot_fid, NULL, th);
3147 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3148 inode, dot_ldp, dot_dot_ldp);
3150 dir->oo_compat_dotdot_created = 1;
3158 * It will call the appropriate osd_add* function and return the
3159 * value, return by respective functions.
3161 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3162 struct inode *cinode, const char *name,
3163 const struct dt_rec *fid, struct thandle *th)
3165 struct osd_thread_info *info = osd_oti_get(env);
3166 struct htree_lock *hlock;
3169 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3171 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3173 if (hlock != NULL) {
3174 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3177 cfs_down_write(&pobj->oo_ext_idx_sem);
3179 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3180 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3183 if (hlock != NULL) {
3184 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3185 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3187 cfs_down_write(&pobj->oo_ext_idx_sem);
3190 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3194 ldiskfs_htree_unlock(hlock);
3196 cfs_up_write(&pobj->oo_ext_idx_sem);
3202 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3203 struct osd_idmap_cache *oic)
3205 struct osd_scrub *scrub = &dev->od_scrub;
3206 struct lu_fid *fid = &oic->oic_fid;
3207 struct osd_inode_id *id = &oti->oti_id;
3213 rc = osd_oi_lookup(oti, dev, fid, id);
3214 if (rc != 0 && rc != -ENOENT)
3217 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3220 if (thread_is_running(&scrub->os_thread)) {
3221 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3222 /* There is race condition between osd_oi_lookup and OI scrub.
3223 * The OI scrub finished just after osd_oi_lookup() failure.
3224 * Under such case, it is unnecessary to trigger OI scrub again,
3225 * but try to call osd_oi_lookup() again. */
3226 if (unlikely(rc == -EAGAIN))
3232 if (!scrub->os_no_scrub && ++once == 1) {
3233 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3235 rc = osd_scrub_start(dev);
3236 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID", rc = %d\n",
3242 RETURN(rc = -EREMCHG);
3246 * Calls ->lookup() to find dentry. From dentry get inode and
3247 * read inode's ea to get fid. This is required for interoperability
3250 * \retval 0, on success
3251 * \retval -ve, on error
3253 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3254 struct dt_rec *rec, const struct dt_key *key)
3256 struct inode *dir = obj->oo_inode;
3257 struct dentry *dentry;
3258 struct ldiskfs_dir_entry_2 *de;
3259 struct buffer_head *bh;
3260 struct lu_fid *fid = (struct lu_fid *) rec;
3261 struct htree_lock *hlock = NULL;
3265 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3267 dentry = osd_child_dentry_get(env, obj,
3268 (char *)key, strlen((char *)key));
3270 if (obj->oo_hl_head != NULL) {
3271 hlock = osd_oti_get(env)->oti_hlock;
3272 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3273 dir, LDISKFS_HLOCK_LOOKUP);
3275 cfs_down_read(&obj->oo_ext_idx_sem);
3278 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3280 struct osd_thread_info *oti = osd_oti_get(env);
3281 struct osd_idmap_cache *oic = &oti->oti_cache;
3282 struct osd_device *dev = osd_obj2dev(obj);
3283 struct osd_scrub *scrub = &dev->od_scrub;
3284 struct scrub_file *sf = &scrub->os_file;
3286 ino = le32_to_cpu(de->inode);
3287 rc = osd_get_fid_from_dentry(de, rec);
3289 /* done with de, release bh */
3292 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3294 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3296 if (rc != 0 || !fid_is_norm(fid))
3299 oic->oic_fid = *fid;
3300 if ((scrub->os_pos_current <= ino) &&
3301 (sf->sf_flags & SF_INCONSISTENT ||
3302 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3304 rc = osd_consistency_check(oti, dev, oic);
3313 ldiskfs_htree_unlock(hlock);
3315 cfs_up_read(&obj->oo_ext_idx_sem);
3320 * Find the osd object for given fid.
3322 * \param fid need to find the osd object having this fid
3324 * \retval osd_object on success
3325 * \retval -ve on error
3327 struct osd_object *osd_object_find(const struct lu_env *env,
3328 struct dt_object *dt,
3329 const struct lu_fid *fid)
3331 struct lu_device *ludev = dt->do_lu.lo_dev;
3332 struct osd_object *child = NULL;
3333 struct lu_object *luch;
3334 struct lu_object *lo;
3336 luch = lu_object_find(env, ludev, fid, NULL);
3337 if (!IS_ERR(luch)) {
3338 if (lu_object_exists(luch)) {
3339 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3341 child = osd_obj(lo);
3343 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3344 "lu_object can't be located"
3345 DFID"\n", PFID(fid));
3347 if (child == NULL) {
3348 lu_object_put(env, luch);
3349 CERROR("Unable to get osd_object\n");
3350 child = ERR_PTR(-ENOENT);
3353 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3354 "lu_object does not exists "DFID"\n",
3356 lu_object_put(env, luch);
3357 child = ERR_PTR(-ENOENT);
3360 child = (void *)luch;
3366 * Put the osd object once done with it.
3368 * \param obj osd object that needs to be put
3370 static inline void osd_object_put(const struct lu_env *env,
3371 struct osd_object *obj)
3373 lu_object_put(env, &obj->oo_dt.do_lu);
3376 static int osd_index_declare_ea_insert(const struct lu_env *env,
3377 struct dt_object *dt,
3378 const struct dt_rec *rec,
3379 const struct dt_key *key,
3380 struct thandle *handle)
3382 struct osd_thandle *oh;
3384 LASSERT(dt_object_exists(dt));
3385 LASSERT(handle != NULL);
3387 oh = container_of0(handle, struct osd_thandle, ot_super);
3388 LASSERT(oh->ot_handle == NULL);
3390 OSD_DECLARE_OP(oh, insert);
3391 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3393 LASSERT(osd_dt_obj(dt)->oo_inode);
3394 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3395 osd_dt_obj(dt)->oo_inode);
3396 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3397 osd_dt_obj(dt)->oo_inode);
3403 * Index add function for interoperability mode (b11826).
3404 * It will add the directory entry.This entry is needed to
3405 * maintain name->fid mapping.
3407 * \param key it is key i.e. file entry to be inserted
3408 * \param rec it is value of given key i.e. fid
3410 * \retval 0, on success
3411 * \retval -ve, on error
3413 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3414 const struct dt_rec *rec,
3415 const struct dt_key *key, struct thandle *th,
3416 struct lustre_capa *capa, int ignore_quota)
3418 struct osd_object *obj = osd_dt_obj(dt);
3419 struct lu_fid *fid = (struct lu_fid *) rec;
3420 const char *name = (const char *)key;
3421 struct osd_object *child;
3422 #ifdef HAVE_QUOTA_SUPPORT
3423 cfs_cap_t save = cfs_curproc_cap_pack();
3429 LASSERT(osd_invariant(obj));
3430 LASSERT(dt_object_exists(dt));
3431 LASSERT(th != NULL);
3433 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3436 child = osd_object_find(env, dt, fid);
3437 if (!IS_ERR(child)) {
3438 #ifdef HAVE_QUOTA_SUPPORT
3440 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3442 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3444 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3445 #ifdef HAVE_QUOTA_SUPPORT
3446 cfs_curproc_cap_unpack(save);
3448 osd_object_put(env, child);
3450 rc = PTR_ERR(child);
3453 LASSERT(osd_invariant(obj));
3458 * Initialize osd Iterator for given osd index object.
3460 * \param dt osd index object
3463 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3464 struct dt_object *dt,
3466 struct lustre_capa *capa)
3468 struct osd_it_iam *it;
3469 struct osd_thread_info *oti = osd_oti_get(env);
3470 struct osd_object *obj = osd_dt_obj(dt);
3471 struct lu_object *lo = &dt->do_lu;
3472 struct iam_path_descr *ipd;
3473 struct iam_container *bag = &obj->oo_dir->od_container;
3475 LASSERT(lu_object_exists(lo));
3477 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3478 return ERR_PTR(-EACCES);
3481 ipd = osd_it_ipd_get(env, bag);
3482 if (likely(ipd != NULL)) {
3486 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3487 return (struct dt_it *)it;
3489 return ERR_PTR(-ENOMEM);
3493 * free given Iterator.
3496 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3498 struct osd_it_iam *it = (struct osd_it_iam *)di;
3499 struct osd_object *obj = it->oi_obj;
3501 iam_it_fini(&it->oi_it);
3502 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3503 lu_object_put(env, &obj->oo_dt.do_lu);
3507 * Move Iterator to record specified by \a key
3509 * \param di osd iterator
3510 * \param key key for index
3512 * \retval +ve di points to record with least key not larger than key
3513 * \retval 0 di points to exact matched key
3514 * \retval -ve failure
3517 static int osd_it_iam_get(const struct lu_env *env,
3518 struct dt_it *di, const struct dt_key *key)
3520 struct osd_it_iam *it = (struct osd_it_iam *)di;
3522 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3528 * \param di osd iterator
3531 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3533 struct osd_it_iam *it = (struct osd_it_iam *)di;
3535 iam_it_put(&it->oi_it);
3539 * Move iterator by one record
3541 * \param di osd iterator
3543 * \retval +1 end of container reached
3545 * \retval -ve failure
3548 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3550 struct osd_it_iam *it = (struct osd_it_iam *)di;
3552 return iam_it_next(&it->oi_it);
3556 * Return pointer to the key under iterator.
3559 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3560 const struct dt_it *di)
3562 struct osd_it_iam *it = (struct osd_it_iam *)di;
3564 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3568 * Return size of key under iterator (in bytes)
3571 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3573 struct osd_it_iam *it = (struct osd_it_iam *)di;
3575 return iam_it_key_size(&it->oi_it);
3578 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3579 int len, __u16 type)
3581 struct luda_type *lt;
3582 const unsigned align = sizeof(struct luda_type) - 1;
3584 /* check if file type is required */
3585 if (attr & LUDA_TYPE) {
3586 len = (len + align) & ~align;
3588 lt = (void *) ent->lde_name + len;
3589 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3590 ent->lde_attrs |= LUDA_TYPE;
3593 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3597 * build lu direct from backend fs dirent.
3600 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3601 struct lu_fid *fid, __u64 offset,
3602 char *name, __u16 namelen,
3603 __u16 type, __u32 attr)
3605 fid_cpu_to_le(&ent->lde_fid, fid);
3606 ent->lde_attrs = LUDA_FID;
3608 ent->lde_hash = cpu_to_le64(offset);
3609 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3611 strncpy(ent->lde_name, name, namelen);
3612 ent->lde_namelen = cpu_to_le16(namelen);
3614 /* append lustre attributes */
3615 osd_it_append_attrs(ent, attr, namelen, type);
3619 * Return pointer to the record under iterator.
3621 static int osd_it_iam_rec(const struct lu_env *env,
3622 const struct dt_it *di,
3623 struct dt_rec *dtrec, __u32 attr)
3625 struct osd_it_iam *it = (struct osd_it_iam *)di;
3626 struct osd_thread_info *info = osd_oti_get(env);
3629 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3630 const struct osd_fid_pack *rec;
3631 struct lu_fid *fid = &info->oti_fid;
3632 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3638 name = (char *)iam_it_key_get(&it->oi_it);
3640 RETURN(PTR_ERR(name));
3642 namelen = iam_it_key_size(&it->oi_it);
3644 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3646 RETURN(PTR_ERR(rec));
3648 rc = osd_fid_unpack(fid, rec);
3652 hash = iam_it_store(&it->oi_it);
3654 /* IAM does not store object type in IAM index (dir) */
3655 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3658 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3659 (struct iam_rec *)dtrec);
3666 * Returns cookie for current Iterator position.
3668 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3670 struct osd_it_iam *it = (struct osd_it_iam *)di;
3672 return iam_it_store(&it->oi_it);
3676 * Restore iterator from cookie.
3678 * \param di osd iterator
3679 * \param hash Iterator location cookie
3681 * \retval +ve di points to record with least key not larger than key.
3682 * \retval 0 di points to exact matched key
3683 * \retval -ve failure
3686 static int osd_it_iam_load(const struct lu_env *env,
3687 const struct dt_it *di, __u64 hash)
3689 struct osd_it_iam *it = (struct osd_it_iam *)di;
3691 return iam_it_load(&it->oi_it, hash);
3694 static const struct dt_index_operations osd_index_iam_ops = {
3695 .dio_lookup = osd_index_iam_lookup,
3696 .dio_declare_insert = osd_index_declare_iam_insert,
3697 .dio_insert = osd_index_iam_insert,
3698 .dio_declare_delete = osd_index_declare_iam_delete,
3699 .dio_delete = osd_index_iam_delete,
3701 .init = osd_it_iam_init,
3702 .fini = osd_it_iam_fini,
3703 .get = osd_it_iam_get,
3704 .put = osd_it_iam_put,
3705 .next = osd_it_iam_next,
3706 .key = osd_it_iam_key,
3707 .key_size = osd_it_iam_key_size,
3708 .rec = osd_it_iam_rec,
3709 .store = osd_it_iam_store,
3710 .load = osd_it_iam_load
3715 * Creates or initializes iterator context.
3717 * \retval struct osd_it_ea, iterator structure on success
3720 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3721 struct dt_object *dt,
3723 struct lustre_capa *capa)
3725 struct osd_object *obj = osd_dt_obj(dt);
3726 struct osd_thread_info *info = osd_oti_get(env);
3727 struct osd_it_ea *it = &info->oti_it_ea;
3728 struct lu_object *lo = &dt->do_lu;
3729 struct dentry *obj_dentry = &info->oti_it_dentry;
3731 LASSERT(lu_object_exists(lo));
3733 obj_dentry->d_inode = obj->oo_inode;
3734 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3735 obj_dentry->d_name.hash = 0;
3737 it->oie_rd_dirent = 0;
3738 it->oie_it_dirent = 0;
3739 it->oie_dirent = NULL;
3740 it->oie_buf = info->oti_it_ea_buf;
3742 it->oie_file.f_pos = 0;
3743 it->oie_file.f_dentry = obj_dentry;
3744 if (attr & LUDA_64BITHASH)
3745 it->oie_file.f_flags = O_64BITHASH;
3747 it->oie_file.f_flags = O_32BITHASH;
3748 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3749 it->oie_file.f_op = obj->oo_inode->i_fop;
3750 it->oie_file.private_data = NULL;
3752 RETURN((struct dt_it *) it);
3756 * Destroy or finishes iterator context.
3758 * \param di iterator structure to be destroyed
3760 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3762 struct osd_it_ea *it = (struct osd_it_ea *)di;
3763 struct osd_object *obj = it->oie_obj;
3764 struct inode *inode = obj->oo_inode;
3767 it->oie_file.f_op->release(inode, &it->oie_file);
3768 lu_object_put(env, &obj->oo_dt.do_lu);
3773 * It position the iterator at given key, so that next lookup continues from
3774 * that key Or it is similar to dio_it->load() but based on a key,
3775 * rather than file position.
3777 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3780 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3782 static int osd_it_ea_get(const struct lu_env *env,
3783 struct dt_it *di, const struct dt_key *key)
3785 struct osd_it_ea *it = (struct osd_it_ea *)di;
3788 LASSERT(((const char *)key)[0] == '\0');
3789 it->oie_file.f_pos = 0;
3790 it->oie_rd_dirent = 0;
3791 it->oie_it_dirent = 0;
3792 it->oie_dirent = NULL;
3800 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3805 * It is called internally by ->readdir(). It fills the
3806 * iterator's in-memory data structure with required
3807 * information i.e. name, namelen, rec_size etc.
3809 * \param buf in which information to be filled in.
3810 * \param name name of the file in given dir
3812 * \retval 0 on success
3813 * \retval 1 on buffer full
3815 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3816 loff_t offset, __u64 ino,
3819 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3820 struct osd_it_ea_dirent *ent = it->oie_dirent;
3821 struct lu_fid *fid = &ent->oied_fid;
3822 struct osd_fid_pack *rec;
3825 /* this should never happen */
3826 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3827 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3831 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3835 if (d_type & LDISKFS_DIRENT_LUFID) {
3836 rec = (struct osd_fid_pack*) (name + namelen + 1);
3838 if (osd_fid_unpack(fid, rec) != 0)
3841 d_type &= ~LDISKFS_DIRENT_LUFID;
3846 ent->oied_ino = ino;
3847 ent->oied_off = offset;
3848 ent->oied_namelen = namelen;
3849 ent->oied_type = d_type;
3851 memcpy(ent->oied_name, name, namelen);
3853 it->oie_rd_dirent++;
3854 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3859 * Calls ->readdir() to load a directory entry at a time
3860 * and stored it in iterator's in-memory data structure.
3862 * \param di iterator's in memory structure
3864 * \retval 0 on success
3865 * \retval -ve on error
3867 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3868 const struct dt_it *di)
3870 struct osd_it_ea *it = (struct osd_it_ea *)di;
3871 struct osd_object *obj = it->oie_obj;
3872 struct inode *inode = obj->oo_inode;
3873 struct htree_lock *hlock = NULL;
3877 it->oie_dirent = it->oie_buf;
3878 it->oie_rd_dirent = 0;
3880 if (obj->oo_hl_head != NULL) {
3881 hlock = osd_oti_get(env)->oti_hlock;
3882 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3883 inode, LDISKFS_HLOCK_READDIR);
3885 cfs_down_read(&obj->oo_ext_idx_sem);
3888 result = inode->i_fop->readdir(&it->oie_file, it,
3889 (filldir_t) osd_ldiskfs_filldir);
3892 ldiskfs_htree_unlock(hlock);
3894 cfs_up_read(&obj->oo_ext_idx_sem);
3896 if (it->oie_rd_dirent == 0) {
3899 it->oie_dirent = it->oie_buf;
3900 it->oie_it_dirent = 1;
3907 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3908 * to load a directory entry at a time and stored it in
3909 * iterator's in-memory data structure.
3911 * \param di iterator's in memory structure
3913 * \retval +ve iterator reached to end
3914 * \retval 0 iterator not reached to end
3915 * \retval -ve on error
3917 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3919 struct osd_it_ea *it = (struct osd_it_ea *)di;
3924 if (it->oie_it_dirent < it->oie_rd_dirent) {
3926 (void *) it->oie_dirent +
3927 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3928 it->oie_dirent->oied_namelen);
3929 it->oie_it_dirent++;
3932 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3935 rc = osd_ldiskfs_it_fill(env, di);
3942 * Returns the key at current position from iterator's in memory structure.
3944 * \param di iterator's in memory structure
3946 * \retval key i.e. struct dt_key on success
3948 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3949 const struct dt_it *di)
3951 struct osd_it_ea *it = (struct osd_it_ea *)di;
3953 return (struct dt_key *)it->oie_dirent->oied_name;
3957 * Returns the key's size at current position from iterator's in memory structure.
3959 * \param di iterator's in memory structure
3961 * \retval key_size i.e. struct dt_key on success
3963 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3965 struct osd_it_ea *it = (struct osd_it_ea *)di;
3967 return it->oie_dirent->oied_namelen;
3972 * Returns the value (i.e. fid/igif) at current position from iterator's
3973 * in memory structure.
3975 * \param di struct osd_it_ea, iterator's in memory structure
3976 * \param attr attr requested for dirent.
3977 * \param lde lustre dirent
3979 * \retval 0 no error and \param lde has correct lustre dirent.
3980 * \retval -ve on error
3982 static inline int osd_it_ea_rec(const struct lu_env *env,
3983 const struct dt_it *di,
3984 struct dt_rec *dtrec, __u32 attr)
3986 struct osd_it_ea *it = (struct osd_it_ea *)di;
3987 struct osd_object *obj = it->oie_obj;
3988 struct osd_device *dev = osd_obj2dev(obj);
3989 struct osd_scrub *scrub = &dev->od_scrub;
3990 struct scrub_file *sf = &scrub->os_file;
3991 struct osd_thread_info *oti = osd_oti_get(env);
3992 struct osd_idmap_cache *oic = &oti->oti_cache;
3993 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3994 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3995 __u32 ino = it->oie_dirent->oied_ino;
3999 if (!fid_is_sane(fid)) {
4000 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4004 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4007 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4008 it->oie_dirent->oied_name,
4009 it->oie_dirent->oied_namelen,
4010 it->oie_dirent->oied_type, attr);
4012 if (!fid_is_norm(fid))
4015 oic->oic_fid = *fid;
4016 if ((scrub->os_pos_current <= ino) &&
4017 (sf->sf_flags & SF_INCONSISTENT ||
4018 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4019 rc = osd_consistency_check(oti, dev, oic);
4025 * Returns a cookie for current position of the iterator head, so that
4026 * user can use this cookie to load/start the iterator next time.
4028 * \param di iterator's in memory structure
4030 * \retval cookie for current position, on success
4032 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4034 struct osd_it_ea *it = (struct osd_it_ea *)di;
4036 return it->oie_dirent->oied_off;
4040 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4041 * to load a directory entry at a time and stored it i inn,
4042 * in iterator's in-memory data structure.
4044 * \param di struct osd_it_ea, iterator's in memory structure
4046 * \retval +ve on success
4047 * \retval -ve on error
4049 static int osd_it_ea_load(const struct lu_env *env,
4050 const struct dt_it *di, __u64 hash)
4052 struct osd_it_ea *it = (struct osd_it_ea *)di;
4056 it->oie_file.f_pos = hash;
4058 rc = osd_ldiskfs_it_fill(env, di);
4066 * Index lookup function for interoperability mode (b11826).
4068 * \param key, key i.e. file name to be searched
4070 * \retval +ve, on success
4071 * \retval -ve, on error
4073 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4074 struct dt_rec *rec, const struct dt_key *key,
4075 struct lustre_capa *capa)
4077 struct osd_object *obj = osd_dt_obj(dt);
4082 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4083 LINVRNT(osd_invariant(obj));
4085 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4088 rc = osd_ea_lookup_rec(env, obj, rec, key);
4096 * Index and Iterator operations for interoperability
4097 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4099 static const struct dt_index_operations osd_index_ea_ops = {
4100 .dio_lookup = osd_index_ea_lookup,
4101 .dio_declare_insert = osd_index_declare_ea_insert,
4102 .dio_insert = osd_index_ea_insert,
4103 .dio_declare_delete = osd_index_declare_ea_delete,
4104 .dio_delete = osd_index_ea_delete,
4106 .init = osd_it_ea_init,
4107 .fini = osd_it_ea_fini,
4108 .get = osd_it_ea_get,
4109 .put = osd_it_ea_put,
4110 .next = osd_it_ea_next,
4111 .key = osd_it_ea_key,
4112 .key_size = osd_it_ea_key_size,
4113 .rec = osd_it_ea_rec,
4114 .store = osd_it_ea_store,
4115 .load = osd_it_ea_load
4119 static void *osd_key_init(const struct lu_context *ctx,
4120 struct lu_context_key *key)
4122 struct osd_thread_info *info;
4124 OBD_ALLOC_PTR(info);
4126 return ERR_PTR(-ENOMEM);
4128 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4129 if (info->oti_it_ea_buf == NULL)
4132 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4134 info->oti_hlock = ldiskfs_htree_lock_alloc();
4135 if (info->oti_hlock == NULL)
4141 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4144 return ERR_PTR(-ENOMEM);
4147 static void osd_key_fini(const struct lu_context *ctx,
4148 struct lu_context_key *key, void* data)
4150 struct osd_thread_info *info = data;
4152 if (info->oti_hlock != NULL)
4153 ldiskfs_htree_lock_free(info->oti_hlock);
4154 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4158 static void osd_key_exit(const struct lu_context *ctx,
4159 struct lu_context_key *key, void *data)
4161 struct osd_thread_info *info = data;
4163 LASSERT(info->oti_r_locks == 0);
4164 LASSERT(info->oti_w_locks == 0);
4165 LASSERT(info->oti_txns == 0);
4168 /* type constructor/destructor: osd_type_init, osd_type_fini */
4169 LU_TYPE_INIT_FINI(osd, &osd_key);
4171 struct lu_context_key osd_key = {
4172 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4173 .lct_init = osd_key_init,
4174 .lct_fini = osd_key_fini,
4175 .lct_exit = osd_key_exit
4179 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4180 const char *name, struct lu_device *next)
4182 return osd_procfs_init(osd_dev(d), name);
4185 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4189 osd_scrub_cleanup(env, o);
4192 fsfilt_put_ops(o->od_fsops);
4199 static int osd_mount(const struct lu_env *env,
4200 struct osd_device *o, struct lustre_cfg *cfg)
4202 struct lustre_mount_info *lmi;
4203 const char *dev = lustre_cfg_string(cfg, 0);
4204 struct lustre_disk_data *ldd;
4205 struct lustre_sb_info *lsi;
4210 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4211 if (o->od_fsops == NULL) {
4212 CERROR("Can't find fsfilt_ldiskfs\n");
4216 if (o->od_mount != NULL) {
4217 CERROR("Already mounted (%s)\n", dev);
4222 lmi = server_get_mount(dev);
4224 CERROR("Cannot get mount info for %s!\n", dev);
4228 LASSERT(lmi != NULL);
4229 /* save lustre_mount_info in dt_device */
4231 o->od_mnt = lmi->lmi_mnt;
4233 lsi = s2lsi(lmi->lmi_sb);
4236 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4238 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", dev);
4242 if (ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
4243 rc = osd_compat_init(o);
4245 CERROR("%s: can't initialize compats: %d\n", dev, rc);
4251 static struct lu_device *osd_device_fini(const struct lu_env *env,
4252 struct lu_device *d)
4257 osd_compat_fini(osd_dev(d));
4259 shrink_dcache_sb(osd_sb(osd_dev(d)));
4260 osd_sync(env, lu2dt_dev(d));
4262 rc = osd_procfs_fini(osd_dev(d));
4264 CERROR("proc fini error %d \n", rc);
4265 RETURN (ERR_PTR(rc));
4268 if (osd_dev(d)->od_mount)
4269 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4270 osd_dev(d)->od_mount->lmi_mnt);
4271 osd_dev(d)->od_mount = NULL;
4276 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4277 struct lu_device_type *t,
4278 struct lustre_cfg *cfg)
4280 struct lu_device *l;
4281 struct osd_device *o;
4287 result = dt_device_init(&o->od_dt_dev, t);
4290 l->ld_ops = &osd_lu_ops;
4291 o->od_dt_dev.dd_ops = &osd_dt_ops;
4292 cfs_spin_lock_init(&o->od_osfs_lock);
4293 cfs_mutex_init(&o->od_otable_mutex);
4294 o->od_osfs_age = cfs_time_shift_64(-1000);
4295 o->od_capa_hash = init_capa_hash();
4296 if (o->od_capa_hash == NULL) {
4297 dt_device_fini(&o->od_dt_dev);
4298 l = ERR_PTR(-ENOMEM);
4301 l = ERR_PTR(result);
4306 l = ERR_PTR(-ENOMEM);
4310 static struct lu_device *osd_device_free(const struct lu_env *env,
4311 struct lu_device *d)
4313 struct osd_device *o = osd_dev(d);
4316 cleanup_capa_hash(o->od_capa_hash);
4317 dt_device_fini(&o->od_dt_dev);
4322 static int osd_process_config(const struct lu_env *env,
4323 struct lu_device *d, struct lustre_cfg *cfg)
4325 struct osd_device *o = osd_dev(d);
4329 switch(cfg->lcfg_command) {
4331 err = osd_mount(env, o, cfg);
4334 err = osd_shutdown(env, o);
4343 static int osd_recovery_complete(const struct lu_env *env,
4344 struct lu_device *d)
4349 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4350 struct lu_device *dev)
4352 struct osd_device *osd = osd_dev(dev);
4356 /* 1. setup scrub, including OI files initialization */
4357 result = osd_scrub_setup(env, osd);
4361 if (!lu_device_is_md(pdev))
4364 /* 2. setup local objects */
4365 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4369 static const struct lu_object_operations osd_lu_obj_ops = {
4370 .loo_object_init = osd_object_init,
4371 .loo_object_delete = osd_object_delete,
4372 .loo_object_release = osd_object_release,
4373 .loo_object_free = osd_object_free,
4374 .loo_object_print = osd_object_print,
4375 .loo_object_invariant = osd_object_invariant
4378 const struct lu_device_operations osd_lu_ops = {
4379 .ldo_object_alloc = osd_object_alloc,
4380 .ldo_process_config = osd_process_config,
4381 .ldo_recovery_complete = osd_recovery_complete,
4382 .ldo_prepare = osd_prepare,
4385 static const struct lu_device_type_operations osd_device_type_ops = {
4386 .ldto_init = osd_type_init,
4387 .ldto_fini = osd_type_fini,
4389 .ldto_start = osd_type_start,
4390 .ldto_stop = osd_type_stop,
4392 .ldto_device_alloc = osd_device_alloc,
4393 .ldto_device_free = osd_device_free,
4395 .ldto_device_init = osd_device_init,
4396 .ldto_device_fini = osd_device_fini
4399 static struct lu_device_type osd_device_type = {
4400 .ldt_tags = LU_DEVICE_DT,
4401 .ldt_name = LUSTRE_OSD_NAME,
4402 .ldt_ops = &osd_device_type_ops,
4403 .ldt_ctx_tags = LCT_LOCAL,
4407 * lprocfs legacy support.
4409 static struct obd_ops osd_obd_device_ops = {
4410 .o_owner = THIS_MODULE
4413 static int __init osd_mod_init(void)
4415 struct lprocfs_static_vars lvars;
4418 lprocfs_osd_init_vars(&lvars);
4419 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4420 LUSTRE_OSD_NAME, &osd_device_type);
4423 static void __exit osd_mod_exit(void)
4425 class_unregister_type(LUSTRE_OSD_NAME);
4428 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4429 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4430 MODULE_LICENSE("GPL");
4432 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);