4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
60 * struct OBD_{ALLOC,FREE}*()
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
68 #include <lustre_fid.h>
70 #include "osd_internal.h"
73 /* llo_* api support */
74 #include <md_object.h>
75 /* dt_acct_features */
78 #ifdef HAVE_LDISKFS_PDO
80 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
81 "ldiskfs with parallel directory operations");
86 static const char dot[] = ".";
87 static const char dotdot[] = "..";
88 static const char remote_obj_dir[] = "REM_OBJ_DIR";
90 static const struct lu_object_operations osd_lu_obj_ops;
91 static const struct dt_object_operations osd_obj_ops;
92 static const struct dt_object_operations osd_obj_ea_ops;
93 static const struct dt_index_operations osd_index_iam_ops;
94 static const struct dt_index_operations osd_index_ea_ops;
96 static int osd_has_index(const struct osd_object *obj)
98 return obj->oo_dt.do_index_ops != NULL;
101 static int osd_object_invariant(const struct lu_object *l)
103 return osd_invariant(osd_obj(l));
106 #ifdef HAVE_QUOTA_SUPPORT
108 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
110 struct md_ucred *uc = md_ucred(env);
115 save->oc_uid = current_fsuid();
116 save->oc_gid = current_fsgid();
117 save->oc_cap = current_cap();
118 if ((tc = prepare_creds())) {
119 tc->fsuid = uc->mu_fsuid;
120 tc->fsgid = uc->mu_fsgid;
123 /* XXX not suboptimal */
124 cfs_curproc_cap_unpack(uc->mu_cap);
128 osd_pop_ctxt(struct osd_ctxt *save)
132 if ((tc = prepare_creds())) {
133 tc->fsuid = save->oc_uid;
134 tc->fsgid = save->oc_gid;
135 tc->cap_effective = save->oc_cap;
142 * Concurrency: doesn't matter
144 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
146 return osd_oti_get(env)->oti_r_locks > 0;
150 * Concurrency: doesn't matter
152 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
154 struct osd_thread_info *oti = osd_oti_get(env);
155 return oti->oti_w_locks > 0 && o->oo_owner == env;
159 * Concurrency: doesn't access mutable data
161 static int osd_root_get(const struct lu_env *env,
162 struct dt_device *dev, struct lu_fid *f)
164 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
168 static inline int osd_qid_type(struct osd_thandle *oh, int i)
170 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
173 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
175 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
178 void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
179 int type, uid_t id, struct inode *inode)
182 int i, allocated = 0;
183 struct osd_object *obj;
187 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
190 /* id entry is allocated in the quota file */
191 if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
194 for (i = 0; i < oh->ot_id_cnt; i++) {
195 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
199 if (unlikely(i >= OSD_MAX_UGID_CNT)) {
200 CERROR("more than %d uid/gids for a transaction?\n", i);
204 oh->ot_id_array[i] = id;
205 osd_qid_set_type(oh, i, type);
207 obj = osd_dt_obj(dt);
208 oh->ot_credits += (allocated || id == 0) ?
209 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
214 * OSD object methods.
218 * Concurrency: no concurrent access is possible that early in object
221 static struct lu_object *osd_object_alloc(const struct lu_env *env,
222 const struct lu_object_header *hdr,
225 struct osd_object *mo;
231 l = &mo->oo_dt.do_lu;
232 dt_object_init(&mo->oo_dt, NULL, d);
233 if (osd_dev(d)->od_iop_mode)
234 mo->oo_dt.do_ops = &osd_obj_ea_ops;
236 mo->oo_dt.do_ops = &osd_obj_ops;
238 l->lo_ops = &osd_lu_obj_ops;
239 cfs_init_rwsem(&mo->oo_sem);
240 cfs_init_rwsem(&mo->oo_ext_idx_sem);
241 cfs_spin_lock_init(&mo->oo_guard);
248 static int osd_get_lma(struct inode *inode, struct dentry *dentry,
249 struct lustre_mdt_attrs *lma)
253 dentry->d_inode = inode;
254 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
257 /* Check LMA compatibility */
258 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
259 CWARN("%.16s: unsupported incompat LMA feature(s) "
261 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
262 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
266 lustre_lma_swab(lma);
269 } else if (rc == 0) {
277 * retrieve object from backend ext fs.
279 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
280 struct osd_inode_id *id)
282 struct inode *inode = NULL;
284 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
286 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
287 id->oii_ino, PTR_ERR(inode));
288 } else if (id->oii_gen != OSD_OII_NOGEN &&
289 inode->i_generation != id->oii_gen) {
290 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
292 id->oii_ino, id->oii_gen, inode->i_generation);
294 inode = ERR_PTR(-ESTALE);
295 } else if (inode->i_nlink == 0) {
296 /* due to parallel readdir and unlink,
297 * we can have dead inode here. */
298 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
299 make_bad_inode(inode);
301 inode = ERR_PTR(-ESTALE);
302 } else if (is_bad_inode(inode)) {
303 CWARN("%s: bad inode: ino = %u\n",
304 dev->od_dt_dev.dd_lu_dev.ld_obd->obd_name, id->oii_ino);
306 inode = ERR_PTR(-ENOENT);
308 if (id->oii_gen == OSD_OII_NOGEN)
309 osd_id_gen(id, inode->i_ino, inode->i_generation);
311 /* Do not update file c/mtime in ldiskfs.
312 * NB: we don't have any lock to protect this because we don't
313 * have reference on osd_object now, but contention with
314 * another lookup + attr_set can't happen in the tiny window
315 * between if (...) and set S_NOCMTIME. */
316 if (!(inode->i_flags & S_NOCMTIME))
317 inode->i_flags |= S_NOCMTIME;
322 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
323 struct osd_inode_id *id, struct lu_fid *fid)
325 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
329 inode = osd_iget(info, dev, id);
333 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
335 *fid = lma->lma_self_fid;
336 } else if (rc == -ENODATA) {
337 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
345 static struct inode *
346 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
347 struct osd_inode_id *id, const struct lu_fid *fid)
349 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
353 inode = osd_iget(info, dev, id);
357 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
359 if (rc == -ENODATA) {
360 CDEBUG(D_LFSCK, "inconsistent obj: NULL, %lu, "DFID"\n",
361 inode->i_ino, PFID(fid));
368 if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
369 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
370 PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
372 return ERR_PTR(EREMCHG);
377 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
378 const struct lu_fid *fid,
379 const struct lu_object_conf *conf)
381 struct osd_thread_info *info;
382 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
383 struct osd_device *dev;
384 struct osd_idmap_cache *oic;
385 struct osd_inode_id *id;
387 struct osd_scrub *scrub;
388 struct scrub_file *sf;
393 LINVRNT(osd_invariant(obj));
394 LASSERT(obj->oo_inode == NULL);
395 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
398 scrub = &dev->od_scrub;
399 sf = &scrub->os_file;
400 info = osd_oti_get(env);
402 oic = &info->oti_cache;
405 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
408 if (fid_is_norm(fid)) {
409 /* Search order: 1. per-thread cache. */
410 if (lu_fid_eq(fid, &oic->oic_fid)) {
412 } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
413 /* Search order: 2. OI scrub pending list. */
414 result = osd_oii_lookup(dev, fid, id);
419 if (sf->sf_flags & SF_INCONSISTENT)
423 fid_zero(&oic->oic_fid);
424 /* Search order: 3. OI files. */
425 result = osd_oi_lookup(info, dev, fid, id);
426 if (result != 0 && result != -ENOENT)
429 /* If fid wasn't found in oi, inode-less object is created,
430 * for which lu_object_exists() returns false. This is used
431 * in a (frequent) case when objects are created as locking
432 * anchors or place holders for objects yet to be created. */
433 if (conf != NULL && conf->loc_flags & LOC_F_NEW) {
434 if (unlikely(result == 0))
435 GOTO(out, result = -EEXIST);
437 GOTO(out, result = 0);
440 if (result == -ENOENT) {
441 if (!fid_is_norm(fid) ||
442 !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
444 GOTO(out, result = 0);
451 inode = osd_iget(info, dev, id);
453 inode = osd_iget_verify(info, dev, id, fid);
455 result = PTR_ERR(inode);
456 if (result == -ENOENT || result == -ESTALE) {
458 } else if (result == -EREMCHG) {
461 if (thread_is_running(&scrub->os_thread)) {
462 result = -EINPROGRESS;
463 } else if (!scrub->os_no_scrub) {
464 result = osd_scrub_start(dev);
465 LCONSOLE_ERROR("Trigger OI scrub by RPC for "
468 if (result == 0 || result == -EALREADY)
469 result = -EINPROGRESS;
478 obj->oo_inode = inode;
479 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
480 if (dev->od_iop_mode) {
481 obj->oo_compat_dot_created = 1;
482 obj->oo_compat_dotdot_created = 1;
485 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
486 GOTO(out, result = 0);
488 LASSERT(obj->oo_hl_head == NULL);
489 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
490 if (obj->oo_hl_head == NULL) {
491 obj->oo_inode = NULL;
493 GOTO(out, result = -ENOMEM);
495 GOTO(out, result = 0);
498 LINVRNT(osd_invariant(obj));
503 * Concurrency: shouldn't matter.
505 static void osd_object_init0(struct osd_object *obj)
507 LASSERT(obj->oo_inode != NULL);
508 obj->oo_dt.do_body_ops = &osd_body_ops;
509 obj->oo_dt.do_lu.lo_header->loh_attr |=
510 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
514 * Concurrency: no concurrent access is possible that early in object
517 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
518 const struct lu_object_conf *conf)
520 struct osd_object *obj = osd_obj(l);
523 LINVRNT(osd_invariant(obj));
525 result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
526 obj->oo_dt.do_body_ops = &osd_body_ops_new;
528 if (obj->oo_inode != NULL)
529 osd_object_init0(obj);
531 LINVRNT(osd_invariant(obj));
536 * Concurrency: no concurrent access is possible that late in object
539 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
541 struct osd_object *obj = osd_obj(l);
543 LINVRNT(osd_invariant(obj));
545 dt_object_fini(&obj->oo_dt);
546 if (obj->oo_hl_head != NULL)
547 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
552 * Concurrency: no concurrent access is possible that late in object
555 static void osd_index_fini(struct osd_object *o)
557 struct iam_container *bag;
559 if (o->oo_dir != NULL) {
560 bag = &o->oo_dir->od_container;
561 if (o->oo_inode != NULL) {
562 if (bag->ic_object == o->oo_inode)
563 iam_container_fini(bag);
565 OBD_FREE_PTR(o->oo_dir);
571 * Concurrency: no concurrent access is possible that late in object
572 * life-cycle (for all existing callers, that is. New callers have to provide
573 * their own locking.)
575 static int osd_inode_unlinked(const struct inode *inode)
577 return inode->i_nlink == 0;
581 OSD_TXN_OI_DELETE_CREDITS = 20,
582 OSD_TXN_INODE_DELETE_CREDITS = 20
589 #if OSD_THANDLE_STATS
591 * Set time when the handle is allocated
593 static void osd_th_alloced(struct osd_thandle *oth)
595 oth->oth_alloced = cfs_time_current();
599 * Set time when the handle started
601 static void osd_th_started(struct osd_thandle *oth)
603 oth->oth_started = cfs_time_current();
607 * Helper function to convert time interval to microseconds packed in
608 * long int (default time units for the counter in "stats" initialized
609 * by lu_time_init() )
611 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
615 cfs_duration_usec(cfs_time_sub(end, start), &val);
616 return val.tv_sec * 1000000 + val.tv_usec;
620 * Check whether the we deal with this handle for too long.
622 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
623 cfs_time_t alloced, cfs_time_t started,
626 cfs_time_t now = cfs_time_current();
628 LASSERT(dev != NULL);
630 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
631 interval_to_usec(alloced, started));
632 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
633 interval_to_usec(started, closed));
634 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
635 interval_to_usec(closed, now));
637 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
638 CWARN("transaction handle %p was open for too long: "
640 "alloced "CFS_TIME_T" ,"
641 "started "CFS_TIME_T" ,"
642 "closed "CFS_TIME_T"\n",
643 oth, now, alloced, started, closed);
644 libcfs_debug_dumpstack(NULL);
648 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
650 cfs_time_t __closed = cfs_time_current(); \
651 cfs_time_t __alloced = oth->oth_alloced; \
652 cfs_time_t __started = oth->oth_started; \
655 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
658 #else /* OSD_THANDLE_STATS */
660 #define osd_th_alloced(h) do {} while(0)
661 #define osd_th_started(h) do {} while(0)
662 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
664 #endif /* OSD_THANDLE_STATS */
667 * Concurrency: doesn't access mutable data.
669 static int osd_param_is_sane(const struct osd_device *dev,
670 const struct thandle *th)
672 struct osd_thandle *oh;
673 oh = container_of0(th, struct osd_thandle, ot_super);
674 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
678 * Concurrency: shouldn't matter.
680 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
681 static void osd_trans_commit_cb(struct super_block *sb,
682 struct journal_callback *jcb, int error)
684 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
687 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
688 struct thandle *th = &oh->ot_super;
689 struct lu_device *lud = &th->th_dev->dd_lu_dev;
690 struct dt_txn_commit_cb *dcb, *tmp;
692 LASSERT(oh->ot_handle == NULL);
695 CERROR("transaction @0x%p commit error: %d\n", th, error);
697 dt_txn_hook_commit(th);
699 /* call per-transaction callbacks if any */
700 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
701 cfs_list_del_init(&dcb->dcb_linkage);
702 dcb->dcb_func(NULL, th, dcb, error);
705 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
709 lu_context_exit(&th->th_ctx);
710 lu_context_fini(&th->th_ctx);
714 static struct thandle *osd_trans_create(const struct lu_env *env,
717 struct osd_thread_info *oti = osd_oti_get(env);
718 struct osd_iobuf *iobuf = &oti->oti_iobuf;
719 struct osd_thandle *oh;
723 /* on pending IO in this thread should left from prev. request */
724 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
726 th = ERR_PTR(-ENOMEM);
727 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
732 th->th_tags = LCT_TX_HANDLE;
734 oti->oti_dev = osd_dt_dev(d);
735 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
742 * Concurrency: shouldn't matter.
744 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
747 struct osd_thread_info *oti = osd_oti_get(env);
748 struct osd_device *dev = osd_dt_dev(d);
750 struct osd_thandle *oh;
755 LASSERT(current->journal_info == NULL);
757 oh = container_of0(th, struct osd_thandle, ot_super);
759 LASSERT(oh->ot_handle == NULL);
761 rc = dt_txn_hook_start(env, d, th);
765 if (!osd_param_is_sane(dev, th)) {
766 CWARN("%s: too many transaction credits (%d > %d)\n",
767 d->dd_lu_dev.ld_obd->obd_name, oh->ot_credits,
768 osd_journal(dev)->j_max_transaction_buffers);
769 /* XXX Limit the credits to 'max_transaction_buffers', and
770 * let the underlying filesystem to catch the error if
771 * we really need so many credits.
773 * This should be removed when we can calculate the
774 * credits precisely. */
775 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
776 #ifdef OSD_TRACK_DECLARES
777 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
778 oh->ot_declare_attr_set, oh->ot_declare_punch,
779 oh->ot_declare_xattr_set);
780 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
781 oh->ot_declare_create, oh->ot_declare_ref_add,
782 oh->ot_declare_ref_del, oh->ot_declare_write);
783 CERROR(" insert: %d, delete: %d, destroy: %d\n",
784 oh->ot_declare_insert, oh->ot_declare_delete,
785 oh->ot_declare_destroy);
790 * XXX temporary stuff. Some abstraction layer should
793 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
797 LASSERT(oti->oti_txns == 0);
798 lu_context_init(&th->th_ctx, th->th_tags);
799 lu_context_enter(&th->th_ctx);
801 lu_device_get(&d->dd_lu_dev);
802 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
806 * XXX: current rule is that we first start tx,
807 * then lock object(s), but we can't use
808 * this rule for data (due to locking specifics
809 * in ldiskfs). also in long-term we'd like to
810 * use usually-used (locks;tx) ordering. so,
811 * UGLY thing is that we'll use one ordering for
812 * data (ofd) and reverse ordering for metadata
813 * (mdd). then at some point we'll fix the latter
815 if (lu_device_is_md(&d->dd_lu_dev)) {
816 LASSERT(oti->oti_r_locks == 0);
817 LASSERT(oti->oti_w_locks == 0);
830 * Concurrency: shouldn't matter.
832 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
835 struct osd_thandle *oh;
836 struct osd_thread_info *oti = osd_oti_get(env);
837 struct osd_iobuf *iobuf = &oti->oti_iobuf;
841 oh = container_of0(th, struct osd_thandle, ot_super);
843 if (oh->ot_handle != NULL) {
844 handle_t *hdl = oh->ot_handle;
846 hdl->h_sync = th->th_sync;
849 * add commit callback
850 * notice we don't do this in osd_trans_start()
851 * as underlying transaction can change during truncate
853 osd_journal_callback_set(hdl, osd_trans_commit_cb,
856 LASSERT(oti->oti_txns == 1);
859 * XXX: current rule is that we first start tx,
860 * then lock object(s), but we can't use
861 * this rule for data (due to locking specifics
862 * in ldiskfs). also in long-term we'd like to
863 * use usually-used (locks;tx) ordering. so,
864 * UGLY thing is that we'll use one ordering for
865 * data (ofd) and reverse ordering for metadata
866 * (mdd). then at some point we'll fix the latter
868 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
869 LASSERT(oti->oti_r_locks == 0);
870 LASSERT(oti->oti_w_locks == 0);
872 rc = dt_txn_hook_stop(env, th);
874 CERROR("Failure in transaction hook: %d\n", rc);
875 oh->ot_handle = NULL;
876 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
877 rc = ldiskfs_journal_stop(hdl));
879 CERROR("Failure to stop transaction: %d\n", rc);
884 /* as we want IO to journal and data IO be concurrent, we don't block
885 * awaiting data IO completion in osd_do_bio(), instead we wait here
886 * once transaction is submitted to the journal. all reqular requests
887 * don't do direct IO (except read/write), thus this wait_event becomes
890 * IMPORTANT: we have to wait till any IO submited by the thread is
891 * completed otherwise iobuf may be corrupted by different request
893 cfs_wait_event(iobuf->dr_wait,
894 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
896 rc = iobuf->dr_error;
901 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
903 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
906 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
912 * Called just before object is freed. Releases all resources except for
913 * object itself (that is released by osd_object_free()).
915 * Concurrency: no concurrent access is possible that late in object
918 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
920 struct osd_object *obj = osd_obj(l);
921 struct inode *inode = obj->oo_inode;
923 LINVRNT(osd_invariant(obj));
926 * If object is unlinked remove fid->ino mapping from object index.
932 obj->oo_inode = NULL;
937 * Concurrency: ->loo_object_release() is called under site spin-lock.
939 static void osd_object_release(const struct lu_env *env,
945 * Concurrency: shouldn't matter.
947 static int osd_object_print(const struct lu_env *env, void *cookie,
948 lu_printer_t p, const struct lu_object *l)
950 struct osd_object *o = osd_obj(l);
953 if (o->oo_dir != NULL)
954 d = o->oo_dir->od_container.ic_descr;
957 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
959 o->oo_inode ? o->oo_inode->i_ino : 0UL,
960 o->oo_inode ? o->oo_inode->i_generation : 0,
961 d ? d->id_ops->id_name : "plain");
965 * Concurrency: shouldn't matter.
967 int osd_statfs(const struct lu_env *env, struct dt_device *d,
968 struct obd_statfs *sfs)
970 struct osd_device *osd = osd_dt_dev(d);
971 struct super_block *sb = osd_sb(osd);
972 struct kstatfs *ksfs;
975 /* osd_lproc.c call this without env, allocate ksfs for that case */
976 if (unlikely(env == NULL)) {
981 ksfs = &osd_oti_get(env)->oti_ksfs;
984 cfs_spin_lock(&osd->od_osfs_lock);
986 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
987 result = ll_do_statfs(sb, ksfs);
988 if (likely(result == 0)) { /* N.B. statfs can't really fail */
989 osd->od_osfs_age = cfs_time_current_64();
990 statfs_pack(&osd->od_statfs, ksfs);
994 if (likely(result == 0))
995 *sfs = osd->od_statfs;
996 cfs_spin_unlock(&osd->od_osfs_lock);
998 if (unlikely(env == NULL))
1005 * Concurrency: doesn't access mutable data.
1007 static void osd_conf_get(const struct lu_env *env,
1008 const struct dt_device *dev,
1009 struct dt_device_param *param)
1011 struct super_block *sb = osd_sb(osd_dt_dev(dev));
1014 * XXX should be taken from not-yet-existing fs abstraction layer.
1016 param->ddp_max_name_len = LDISKFS_NAME_LEN;
1017 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1018 param->ddp_block_shift = sb->s_blocksize_bits;
1019 param->ddp_mntopts = 0;
1020 if (test_opt(sb, XATTR_USER))
1021 param->ddp_mntopts |= MNTOPT_USERXATTR;
1022 if (test_opt(sb, POSIX_ACL))
1023 param->ddp_mntopts |= MNTOPT_ACL;
1025 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1026 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1027 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1030 param->ddp_max_ea_size = sb->s_blocksize;
1035 * Helper function to get and fill the buffer with input values.
1037 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1041 buf = &osd_oti_get(env)->oti_buf;
1048 * Concurrency: shouldn't matter.
1050 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1052 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1053 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1057 * Start commit for OSD device.
1059 * An implementation of dt_commit_async method for OSD device.
1060 * Asychronously starts underlayng fs sync and thereby a transaction
1063 * \param env environment
1064 * \param d dt device
1066 * \see dt_device_operations
1068 static int osd_commit_async(const struct lu_env *env,
1069 struct dt_device *d)
1071 struct super_block *s = osd_sb(osd_dt_dev(d));
1074 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1075 RETURN(s->s_op->sync_fs(s, 0));
1079 * Concurrency: shouldn't matter.
1082 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1084 struct super_block *sb = osd_sb(osd_dt_dev(d));
1088 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1090 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1095 * Concurrency: serialization provided by callers.
1097 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1098 int mode, unsigned long timeout, __u32 alg,
1099 struct lustre_capa_key *keys)
1101 struct osd_device *dev = osd_dt_dev(d);
1104 dev->od_fl_capa = mode;
1105 dev->od_capa_timeout = timeout;
1106 dev->od_capa_alg = alg;
1107 dev->od_capa_keys = keys;
1112 * Concurrency: serialization provided by callers.
1114 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1115 struct dt_quota_ctxt *ctxt, void *data)
1117 struct obd_device *obd = (void *)ctxt;
1118 struct vfsmount *mnt = (struct vfsmount *)data;
1121 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1122 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1123 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1124 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1125 obd->obd_lvfs_ctxt.fs = get_ds();
1131 * Note: we do not count into QUOTA here.
1132 * If we mount with --data_journal we may need more.
1134 const int osd_dto_credits_noquota[DTO_NR] = {
1137 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1138 * SINGLEDATA_TRANS_BLOCKS(8)
1139 * XXX Note: maybe iam need more, since iam have more level than
1142 [DTO_INDEX_INSERT] = 16,
1143 [DTO_INDEX_DELETE] = 16,
1147 [DTO_INDEX_UPDATE] = 16,
1149 * Create a object. The same as create object in EXT3.
1150 * DATA_TRANS_BLOCKS(14) +
1151 * INDEX_EXTRA_BLOCKS(8) +
1152 * 3(inode bits, groups, GDT)
1154 [DTO_OBJECT_CREATE] = 25,
1156 * XXX: real credits to be fixed
1158 [DTO_OBJECT_DELETE] = 25,
1160 * Attr set credits (inode)
1162 [DTO_ATTR_SET_BASE] = 1,
1164 * Xattr set. The same as xattr of EXT3.
1165 * DATA_TRANS_BLOCKS(14)
1166 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1167 * are also counted in. Do not know why?
1169 [DTO_XATTR_SET] = 14,
1172 * credits for inode change during write.
1174 [DTO_WRITE_BASE] = 3,
1176 * credits for single block write.
1178 [DTO_WRITE_BLOCK] = 14,
1180 * Attr set credits for chown.
1181 * This is extra credits for setattr, and it is null without quota
1183 [DTO_ATTR_SET_CHOWN]= 0
1186 static const struct dt_device_operations osd_dt_ops = {
1187 .dt_root_get = osd_root_get,
1188 .dt_statfs = osd_statfs,
1189 .dt_trans_create = osd_trans_create,
1190 .dt_trans_start = osd_trans_start,
1191 .dt_trans_stop = osd_trans_stop,
1192 .dt_trans_cb_add = osd_trans_cb_add,
1193 .dt_conf_get = osd_conf_get,
1194 .dt_sync = osd_sync,
1196 .dt_commit_async = osd_commit_async,
1197 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1198 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1201 static void osd_object_read_lock(const struct lu_env *env,
1202 struct dt_object *dt, unsigned role)
1204 struct osd_object *obj = osd_dt_obj(dt);
1205 struct osd_thread_info *oti = osd_oti_get(env);
1207 LINVRNT(osd_invariant(obj));
1209 LASSERT(obj->oo_owner != env);
1210 cfs_down_read_nested(&obj->oo_sem, role);
1212 LASSERT(obj->oo_owner == NULL);
1216 static void osd_object_write_lock(const struct lu_env *env,
1217 struct dt_object *dt, unsigned role)
1219 struct osd_object *obj = osd_dt_obj(dt);
1220 struct osd_thread_info *oti = osd_oti_get(env);
1222 LINVRNT(osd_invariant(obj));
1224 LASSERT(obj->oo_owner != env);
1225 cfs_down_write_nested(&obj->oo_sem, role);
1227 LASSERT(obj->oo_owner == NULL);
1228 obj->oo_owner = env;
1232 static void osd_object_read_unlock(const struct lu_env *env,
1233 struct dt_object *dt)
1235 struct osd_object *obj = osd_dt_obj(dt);
1236 struct osd_thread_info *oti = osd_oti_get(env);
1238 LINVRNT(osd_invariant(obj));
1240 LASSERT(oti->oti_r_locks > 0);
1242 cfs_up_read(&obj->oo_sem);
1245 static void osd_object_write_unlock(const struct lu_env *env,
1246 struct dt_object *dt)
1248 struct osd_object *obj = osd_dt_obj(dt);
1249 struct osd_thread_info *oti = osd_oti_get(env);
1251 LINVRNT(osd_invariant(obj));
1253 LASSERT(obj->oo_owner == env);
1254 LASSERT(oti->oti_w_locks > 0);
1256 obj->oo_owner = NULL;
1257 cfs_up_write(&obj->oo_sem);
1260 static int osd_object_write_locked(const struct lu_env *env,
1261 struct dt_object *dt)
1263 struct osd_object *obj = osd_dt_obj(dt);
1265 LINVRNT(osd_invariant(obj));
1267 return obj->oo_owner == env;
1270 static int capa_is_sane(const struct lu_env *env,
1271 struct osd_device *dev,
1272 struct lustre_capa *capa,
1273 struct lustre_capa_key *keys)
1275 struct osd_thread_info *oti = osd_oti_get(env);
1276 struct lustre_capa *tcapa = &oti->oti_capa;
1277 struct obd_capa *oc;
1281 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1283 if (capa_is_expired(oc)) {
1284 DEBUG_CAPA(D_ERROR, capa, "expired");
1291 if (capa_is_expired_sec(capa)) {
1292 DEBUG_CAPA(D_ERROR, capa, "expired");
1296 cfs_spin_lock(&capa_lock);
1297 for (i = 0; i < 2; i++) {
1298 if (keys[i].lk_keyid == capa->lc_keyid) {
1299 oti->oti_capa_key = keys[i];
1303 cfs_spin_unlock(&capa_lock);
1306 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1310 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1314 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1315 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1319 oc = capa_add(dev->od_capa_hash, capa);
1325 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1326 struct lustre_capa *capa, __u64 opc)
1328 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1329 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1330 struct md_capainfo *ci;
1333 if (!dev->od_fl_capa)
1336 if (capa == BYPASS_CAPA)
1339 ci = md_capainfo(env);
1343 if (ci->mc_auth == LC_ID_NONE)
1347 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1351 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1352 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1357 if (!capa_opc_supported(capa, opc)) {
1358 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1362 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1363 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1370 static struct timespec *osd_inode_time(const struct lu_env *env,
1371 struct inode *inode, __u64 seconds)
1373 struct osd_thread_info *oti = osd_oti_get(env);
1374 struct timespec *t = &oti->oti_time;
1376 t->tv_sec = seconds;
1378 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1383 static void osd_inode_getattr(const struct lu_env *env,
1384 struct inode *inode, struct lu_attr *attr)
1386 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1387 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1388 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1390 attr->la_atime = LTIME_S(inode->i_atime);
1391 attr->la_mtime = LTIME_S(inode->i_mtime);
1392 attr->la_ctime = LTIME_S(inode->i_ctime);
1393 attr->la_mode = inode->i_mode;
1394 attr->la_size = i_size_read(inode);
1395 attr->la_blocks = inode->i_blocks;
1396 attr->la_uid = inode->i_uid;
1397 attr->la_gid = inode->i_gid;
1398 attr->la_flags = LDISKFS_I(inode)->i_flags;
1399 attr->la_nlink = inode->i_nlink;
1400 attr->la_rdev = inode->i_rdev;
1401 attr->la_blksize = ll_inode_blksize(inode);
1402 attr->la_blkbits = inode->i_blkbits;
1405 static int osd_attr_get(const struct lu_env *env,
1406 struct dt_object *dt,
1407 struct lu_attr *attr,
1408 struct lustre_capa *capa)
1410 struct osd_object *obj = osd_dt_obj(dt);
1412 LASSERT(dt_object_exists(dt));
1413 LINVRNT(osd_invariant(obj));
1415 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1418 cfs_spin_lock(&obj->oo_guard);
1419 osd_inode_getattr(env, obj->oo_inode, attr);
1420 cfs_spin_unlock(&obj->oo_guard);
1424 static int osd_declare_attr_set(const struct lu_env *env,
1425 struct dt_object *dt,
1426 const struct lu_attr *attr,
1427 struct thandle *handle)
1429 struct osd_thandle *oh;
1430 struct osd_object *obj;
1432 LASSERT(dt != NULL);
1433 LASSERT(handle != NULL);
1435 obj = osd_dt_obj(dt);
1436 LASSERT(osd_invariant(obj));
1438 oh = container_of0(handle, struct osd_thandle, ot_super);
1439 LASSERT(oh->ot_handle == NULL);
1441 OSD_DECLARE_OP(oh, attr_set);
1442 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1444 if (attr && attr->la_valid & LA_UID) {
1446 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1448 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1450 if (attr && attr->la_valid & LA_GID) {
1452 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1454 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1460 static int osd_inode_setattr(const struct lu_env *env,
1461 struct inode *inode, const struct lu_attr *attr)
1465 bits = attr->la_valid;
1467 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1469 if (bits & LA_ATIME)
1470 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1471 if (bits & LA_CTIME)
1472 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1473 if (bits & LA_MTIME)
1474 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1475 if (bits & LA_SIZE) {
1476 LDISKFS_I(inode)->i_disksize = attr->la_size;
1477 i_size_write(inode, attr->la_size);
1481 /* OSD should not change "i_blocks" which is used by quota.
1482 * "i_blocks" should be changed by ldiskfs only. */
1483 if (bits & LA_BLOCKS)
1484 inode->i_blocks = attr->la_blocks;
1487 inode->i_mode = (inode->i_mode & S_IFMT) |
1488 (attr->la_mode & ~S_IFMT);
1490 inode->i_uid = attr->la_uid;
1492 inode->i_gid = attr->la_gid;
1493 if (bits & LA_NLINK)
1494 inode->i_nlink = attr->la_nlink;
1496 inode->i_rdev = attr->la_rdev;
1498 if (bits & LA_FLAGS) {
1499 /* always keep S_NOCMTIME */
1500 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1506 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1508 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1509 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1514 if (attr->la_valid & LA_UID)
1515 iattr.ia_valid |= ATTR_UID;
1516 if (attr->la_valid & LA_GID)
1517 iattr.ia_valid |= ATTR_GID;
1518 iattr.ia_uid = attr->la_uid;
1519 iattr.ia_gid = attr->la_gid;
1521 rc = ll_vfs_dq_transfer(inode, &iattr);
1523 CERROR("%s: quota transfer failed: rc = %d. Is quota "
1524 "enforcement enabled on the ldiskfs filesystem?",
1525 inode->i_sb->s_id, rc);
1532 static int osd_attr_set(const struct lu_env *env,
1533 struct dt_object *dt,
1534 const struct lu_attr *attr,
1535 struct thandle *handle,
1536 struct lustre_capa *capa)
1538 struct osd_object *obj = osd_dt_obj(dt);
1539 struct inode *inode;
1542 LASSERT(handle != NULL);
1543 LASSERT(dt_object_exists(dt));
1544 LASSERT(osd_invariant(obj));
1546 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1549 OSD_EXEC_OP(handle, attr_set);
1551 inode = obj->oo_inode;
1552 if (LDISKFS_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1553 LDISKFS_FEATURE_RO_COMPAT_QUOTA)) {
1554 rc = osd_quota_transfer(inode, attr);
1558 #ifdef HAVE_QUOTA_SUPPORT
1559 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1560 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1561 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1566 if (attr->la_valid & LA_UID)
1567 iattr.ia_valid |= ATTR_UID;
1568 if (attr->la_valid & LA_GID)
1569 iattr.ia_valid |= ATTR_GID;
1570 iattr.ia_uid = attr->la_uid;
1571 iattr.ia_gid = attr->la_gid;
1572 osd_push_ctxt(env, save);
1573 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1580 cfs_spin_lock(&obj->oo_guard);
1581 rc = osd_inode_setattr(env, inode, attr);
1582 cfs_spin_unlock(&obj->oo_guard);
1585 inode->i_sb->s_op->dirty_inode(inode);
1589 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1590 struct osd_object *obj,
1591 const char *name, const int namelen)
1593 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1596 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1598 struct dt_allocation_hint *hint,
1602 struct osd_device *osd = osd_obj2dev(obj);
1603 struct osd_thandle *oth;
1604 struct dt_object *parent = NULL;
1605 struct inode *inode;
1606 #ifdef HAVE_QUOTA_SUPPORT
1607 struct osd_ctxt *save = &info->oti_ctxt;
1610 LINVRNT(osd_invariant(obj));
1611 LASSERT(obj->oo_inode == NULL);
1612 LASSERT(obj->oo_hl_head == NULL);
1614 if (S_ISDIR(mode) && ldiskfs_pdo) {
1615 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1616 if (obj->oo_hl_head == NULL)
1620 oth = container_of(th, struct osd_thandle, ot_super);
1621 LASSERT(oth->ot_handle->h_transaction != NULL);
1623 if (hint && hint->dah_parent)
1624 parent = hint->dah_parent;
1626 #ifdef HAVE_QUOTA_SUPPORT
1627 osd_push_ctxt(info->oti_env, save);
1629 inode = ldiskfs_create_inode(oth->ot_handle,
1630 parent ? osd_dt_obj(parent)->oo_inode :
1631 osd_sb(osd)->s_root->d_inode,
1633 #ifdef HAVE_QUOTA_SUPPORT
1636 if (!IS_ERR(inode)) {
1637 /* Do not update file c/mtime in ldiskfs.
1638 * NB: don't need any lock because no contention at this
1640 inode->i_flags |= S_NOCMTIME;
1641 inode->i_state |= I_LUSTRE_NOSCRUB;
1642 obj->oo_inode = inode;
1645 if (obj->oo_hl_head != NULL) {
1646 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1647 obj->oo_hl_head = NULL;
1649 result = PTR_ERR(inode);
1651 LINVRNT(osd_invariant(obj));
1659 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1660 struct lu_attr *attr,
1661 struct dt_allocation_hint *hint,
1662 struct dt_object_format *dof,
1666 struct osd_thandle *oth;
1667 struct osd_device *osd = osd_obj2dev(obj);
1668 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1670 LASSERT(S_ISDIR(attr->la_mode));
1672 oth = container_of(th, struct osd_thandle, ot_super);
1673 LASSERT(oth->ot_handle->h_transaction != NULL);
1674 result = osd_mkfile(info, obj, mode, hint, th);
1675 if (result == 0 && osd->od_iop_mode == 0) {
1676 LASSERT(obj->oo_inode != NULL);
1678 * XXX uh-oh... call low-level iam function directly.
1681 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1682 sizeof (struct osd_fid_pack),
1688 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1689 struct lu_attr *attr,
1690 struct dt_allocation_hint *hint,
1691 struct dt_object_format *dof,
1695 struct osd_thandle *oth;
1696 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1698 __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1700 LASSERT(S_ISREG(attr->la_mode));
1702 oth = container_of(th, struct osd_thandle, ot_super);
1703 LASSERT(oth->ot_handle->h_transaction != NULL);
1705 result = osd_mkfile(info, obj, mode, hint, th);
1707 LASSERT(obj->oo_inode != NULL);
1708 if (feat->dif_flags & DT_IND_VARKEY)
1709 result = iam_lvar_create(obj->oo_inode,
1710 feat->dif_keysize_max,
1712 feat->dif_recsize_max,
1715 result = iam_lfix_create(obj->oo_inode,
1716 feat->dif_keysize_max,
1718 feat->dif_recsize_max,
1725 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1726 struct lu_attr *attr,
1727 struct dt_allocation_hint *hint,
1728 struct dt_object_format *dof,
1731 LASSERT(S_ISREG(attr->la_mode));
1732 return osd_mkfile(info, obj, (attr->la_mode &
1733 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1736 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1737 struct lu_attr *attr,
1738 struct dt_allocation_hint *hint,
1739 struct dt_object_format *dof,
1742 LASSERT(S_ISLNK(attr->la_mode));
1743 return osd_mkfile(info, obj, (attr->la_mode &
1744 (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1747 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1748 struct lu_attr *attr,
1749 struct dt_allocation_hint *hint,
1750 struct dt_object_format *dof,
1753 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1756 LINVRNT(osd_invariant(obj));
1757 LASSERT(obj->oo_inode == NULL);
1758 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1759 S_ISFIFO(mode) || S_ISSOCK(mode));
1761 result = osd_mkfile(info, obj, mode, hint, th);
1763 LASSERT(obj->oo_inode != NULL);
1765 * This inode should be marked dirty for i_rdev. Currently
1766 * that is done in the osd_attr_init().
1768 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1770 LINVRNT(osd_invariant(obj));
1774 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1776 struct dt_allocation_hint *hint,
1777 struct dt_object_format *dof,
1780 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1782 osd_obj_type_f result;
1798 result = osd_mk_index;
1809 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1810 struct dt_object *parent, cfs_umode_t child_mode)
1814 memset(ah, 0, sizeof(*ah));
1815 ah->dah_parent = parent;
1816 ah->dah_mode = child_mode;
1819 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1820 struct lu_attr *attr, struct dt_object_format *dof)
1822 struct inode *inode = obj->oo_inode;
1823 __u64 valid = attr->la_valid;
1826 attr->la_valid &= ~(LA_TYPE | LA_MODE);
1828 if (dof->dof_type != DFT_NODE)
1829 attr->la_valid &= ~LA_RDEV;
1830 if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1831 attr->la_valid &= ~LA_ATIME;
1832 if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1833 attr->la_valid &= ~LA_CTIME;
1834 if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1835 attr->la_valid &= ~LA_MTIME;
1837 if (LDISKFS_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1838 LDISKFS_FEATURE_RO_COMPAT_QUOTA)) {
1839 result = osd_quota_transfer(inode, attr);
1843 #ifdef HAVE_QUOTA_SUPPORT
1844 attr->la_valid &= ~(LA_UID | LA_GID);
1848 if (attr->la_valid != 0) {
1849 result = osd_inode_setattr(info->oti_env, inode, attr);
1851 * The osd_inode_setattr() should always succeed here. The
1852 * only error that could be returned is EDQUOT when we are
1853 * trying to change the UID or GID of the inode. However, this
1854 * should not happen since quota enforcement is no longer
1855 * enabled on ldiskfs (lquota takes care of it).
1857 LASSERTF(result == 0, "%d", result);
1858 inode->i_sb->s_op->dirty_inode(inode);
1861 attr->la_valid = valid;
1865 * Helper function for osd_object_create()
1867 * \retval 0, on success
1869 static int __osd_object_create(struct osd_thread_info *info,
1870 struct osd_object *obj, struct lu_attr *attr,
1871 struct dt_allocation_hint *hint,
1872 struct dt_object_format *dof,
1878 /* we drop umask so that permissions we pass are not affected */
1879 umask = current->fs->umask;
1880 current->fs->umask = 0;
1882 result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1885 osd_attr_init(info, obj, attr, dof);
1886 osd_object_init0(obj);
1888 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1889 unlock_new_inode(obj->oo_inode);
1892 /* restore previous umask value */
1893 current->fs->umask = umask;
1899 * Helper function for osd_object_create()
1901 * \retval 0, on success
1903 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1904 const struct lu_fid *fid, struct thandle *th)
1906 struct osd_thread_info *info = osd_oti_get(env);
1907 struct osd_inode_id *id = &info->oti_id;
1908 struct osd_device *osd = osd_obj2dev(obj);
1909 struct md_ucred *uc = md_ucred(env);
1911 LASSERT(obj->oo_inode != NULL);
1912 LASSERT(uc != NULL);
1914 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1915 return osd_oi_insert(info, osd, fid, id, th);
1918 static int osd_declare_object_create(const struct lu_env *env,
1919 struct dt_object *dt,
1920 struct lu_attr *attr,
1921 struct dt_allocation_hint *hint,
1922 struct dt_object_format *dof,
1923 struct thandle *handle)
1925 struct osd_thandle *oh;
1927 LASSERT(handle != NULL);
1929 oh = container_of0(handle, struct osd_thandle, ot_super);
1930 LASSERT(oh->ot_handle == NULL);
1932 OSD_DECLARE_OP(oh, create);
1933 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1934 /* XXX: So far, only normal fid needs be inserted into the oi,
1935 * things could be changed later. Revise following code then. */
1936 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1937 OSD_DECLARE_OP(oh, insert);
1938 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1940 /* If this is directory, then we expect . and .. to be inserted as
1941 * well. The one directory block always needs to be created for the
1942 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1943 * block), there is no danger of needing a tree for the first block.
1945 if (attr && S_ISDIR(attr->la_mode)) {
1946 OSD_DECLARE_OP(oh, insert);
1947 OSD_DECLARE_OP(oh, insert);
1948 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1952 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1953 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1958 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1959 struct lu_attr *attr,
1960 struct dt_allocation_hint *hint,
1961 struct dt_object_format *dof,
1964 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1965 struct osd_object *obj = osd_dt_obj(dt);
1966 struct osd_thread_info *info = osd_oti_get(env);
1971 LINVRNT(osd_invariant(obj));
1972 LASSERT(!dt_object_exists(dt));
1973 LASSERT(osd_write_locked(env, obj));
1974 LASSERT(th != NULL);
1976 if (unlikely(fid_is_acct(fid)))
1977 /* Quota files can't be created from the kernel any more,
1978 * 'tune2fs -O quota' will take care of creating them */
1981 OSD_EXEC_OP(th, create);
1983 result = __osd_object_create(info, obj, attr, hint, dof, th);
1985 result = __osd_oi_insert(env, obj, fid, th);
1987 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1988 LASSERT(osd_invariant(obj));
1993 * Called to destroy on-disk representation of the object
1995 * Concurrency: must be locked
1997 static int osd_declare_object_destroy(const struct lu_env *env,
1998 struct dt_object *dt,
2001 struct osd_object *obj = osd_dt_obj(dt);
2002 struct inode *inode = obj->oo_inode;
2003 struct osd_thandle *oh;
2007 oh = container_of0(th, struct osd_thandle, ot_super);
2008 LASSERT(oh->ot_handle == NULL);
2011 OSD_DECLARE_OP(oh, destroy);
2012 OSD_DECLARE_OP(oh, delete);
2013 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2014 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2016 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
2017 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
2022 static int osd_object_destroy(const struct lu_env *env,
2023 struct dt_object *dt,
2026 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2027 struct osd_object *obj = osd_dt_obj(dt);
2028 struct inode *inode = obj->oo_inode;
2029 struct osd_device *osd = osd_obj2dev(obj);
2030 struct osd_thandle *oh;
2034 oh = container_of0(th, struct osd_thandle, ot_super);
2035 LASSERT(oh->ot_handle);
2037 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2039 if (unlikely(fid_is_acct(fid)))
2042 /* Parallel control for OI scrub. For most of cases, there is no
2043 * lock contention. So it will not affect unlink performance. */
2044 cfs_mutex_lock(&inode->i_mutex);
2045 if (S_ISDIR(inode->i_mode)) {
2046 LASSERT(osd_inode_unlinked(inode) ||
2047 inode->i_nlink == 1);
2048 cfs_spin_lock(&obj->oo_guard);
2050 cfs_spin_unlock(&obj->oo_guard);
2051 inode->i_sb->s_op->dirty_inode(inode);
2053 LASSERT(osd_inode_unlinked(inode));
2056 OSD_EXEC_OP(th, destroy);
2058 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2059 cfs_mutex_unlock(&inode->i_mutex);
2061 /* XXX: add to ext3 orphan list */
2062 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2064 /* not needed in the cache anymore */
2065 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2071 * Helper function for osd_xattr_set()
2073 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2074 const struct lu_buf *buf, const char *name, int fl)
2076 struct osd_object *obj = osd_dt_obj(dt);
2077 struct inode *inode = obj->oo_inode;
2078 struct osd_thread_info *info = osd_oti_get(env);
2079 struct dentry *dentry = &info->oti_child_dentry;
2083 LASSERT(dt_object_exists(dt));
2084 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2086 if (fl & LU_XATTR_REPLACE)
2087 fs_flags |= XATTR_REPLACE;
2089 if (fl & LU_XATTR_CREATE)
2090 fs_flags |= XATTR_CREATE;
2092 dentry->d_inode = inode;
2093 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2094 buf->lb_len, fs_flags);
2099 * Put the fid into lustre_mdt_attrs, and then place the structure
2100 * inode's ea. This fid should not be altered during the life time
2103 * \retval +ve, on success
2104 * \retval -ve, on error
2106 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2108 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2109 const struct lu_fid *fid)
2111 struct osd_thread_info *info = osd_oti_get(env);
2112 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2114 lustre_lma_init(mdt_attrs, fid);
2115 lustre_lma_swab(mdt_attrs);
2116 return __osd_xattr_set(env, dt,
2117 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2118 XATTR_NAME_LMA, LU_XATTR_CREATE);
2123 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2124 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2125 * To have compatilibility with 1.8 ldiskfs driver we need to have
2126 * magic number at start of fid data.
2127 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2130 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2131 const struct dt_rec *fid)
2133 param->edp_magic = LDISKFS_LUFID_MAGIC;
2134 param->edp_len = sizeof(struct lu_fid) + 1;
2136 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2137 (struct lu_fid *)fid);
2141 * Try to read the fid from inode ea into dt_rec, if return value
2142 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2144 * \param fid object fid.
2146 * \retval 0 on success
2148 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2149 __u32 ino, struct lu_fid *fid,
2150 struct osd_inode_id *id)
2152 struct osd_thread_info *info = osd_oti_get(env);
2153 struct inode *inode;
2156 osd_id_gen(id, ino, OSD_OII_NOGEN);
2157 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2159 RETURN(PTR_ERR(inode));
2166 * OSD layer object create function for interoperability mode (b11826).
2167 * This is mostly similar to osd_object_create(). Only difference being, fid is
2168 * inserted into inode ea here.
2170 * \retval 0, on success
2171 * \retval -ve, on error
2173 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2174 struct lu_attr *attr,
2175 struct dt_allocation_hint *hint,
2176 struct dt_object_format *dof,
2179 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2180 struct osd_object *obj = osd_dt_obj(dt);
2181 struct osd_thread_info *info = osd_oti_get(env);
2186 LASSERT(osd_invariant(obj));
2187 LASSERT(!dt_object_exists(dt));
2188 LASSERT(osd_write_locked(env, obj));
2189 LASSERT(th != NULL);
2191 if (unlikely(fid_is_acct(fid)))
2192 /* Quota files can't be created from the kernel any more,
2193 * 'tune2fs -O quota' will take care of creating them */
2196 OSD_EXEC_OP(th, create);
2198 result = __osd_object_create(info, obj, attr, hint, dof, th);
2199 /* objects under osd root shld have igif fid, so dont add fid EA */
2200 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2201 result = osd_ea_fid_set(env, dt, fid);
2204 result = __osd_oi_insert(env, obj, fid, th);
2206 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2207 LINVRNT(osd_invariant(obj));
2211 static int osd_declare_object_ref_add(const struct lu_env *env,
2212 struct dt_object *dt,
2213 struct thandle *handle)
2215 struct osd_thandle *oh;
2217 /* it's possible that object doesn't exist yet */
2218 LASSERT(handle != NULL);
2220 oh = container_of0(handle, struct osd_thandle, ot_super);
2221 LASSERT(oh->ot_handle == NULL);
2223 OSD_DECLARE_OP(oh, ref_add);
2224 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2230 * Concurrency: @dt is write locked.
2232 static int osd_object_ref_add(const struct lu_env *env,
2233 struct dt_object *dt, struct thandle *th)
2235 struct osd_object *obj = osd_dt_obj(dt);
2236 struct inode *inode = obj->oo_inode;
2238 LINVRNT(osd_invariant(obj));
2239 LASSERT(dt_object_exists(dt));
2240 LASSERT(osd_write_locked(env, obj));
2241 LASSERT(th != NULL);
2243 OSD_EXEC_OP(th, ref_add);
2246 * DIR_NLINK feature is set for compatibility reasons if:
2247 * 1) nlinks > LDISKFS_LINK_MAX, or
2248 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2250 * It is easier to always set this flag (rather than check and set),
2251 * since it has less overhead, and the superblock will be dirtied
2252 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2253 * do not actually care whether this flag is set or not.
2255 cfs_spin_lock(&obj->oo_guard);
2257 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2258 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2259 inode->i_nlink == 2)
2262 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2263 cfs_spin_unlock(&obj->oo_guard);
2264 inode->i_sb->s_op->dirty_inode(inode);
2265 LINVRNT(osd_invariant(obj));
2270 static int osd_declare_object_ref_del(const struct lu_env *env,
2271 struct dt_object *dt,
2272 struct thandle *handle)
2274 struct osd_thandle *oh;
2276 LASSERT(dt_object_exists(dt));
2277 LASSERT(handle != NULL);
2279 oh = container_of0(handle, struct osd_thandle, ot_super);
2280 LASSERT(oh->ot_handle == NULL);
2282 OSD_DECLARE_OP(oh, ref_del);
2283 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2289 * Concurrency: @dt is write locked.
2291 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2294 struct osd_object *obj = osd_dt_obj(dt);
2295 struct inode *inode = obj->oo_inode;
2297 LINVRNT(osd_invariant(obj));
2298 LASSERT(dt_object_exists(dt));
2299 LASSERT(osd_write_locked(env, obj));
2300 LASSERT(th != NULL);
2302 OSD_EXEC_OP(th, ref_del);
2304 cfs_spin_lock(&obj->oo_guard);
2305 LASSERT(inode->i_nlink > 0);
2307 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2308 * then the nlink count is 1. Don't let it be set to 0 or the directory
2309 * inode will be deleted incorrectly. */
2310 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2312 cfs_spin_unlock(&obj->oo_guard);
2313 inode->i_sb->s_op->dirty_inode(inode);
2314 LINVRNT(osd_invariant(obj));
2320 * Get the 64-bit version for an inode.
2322 static int osd_object_version_get(const struct lu_env *env,
2323 struct dt_object *dt, dt_obj_version_t *ver)
2325 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2327 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2328 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2329 *ver = LDISKFS_I(inode)->i_fs_version;
2334 * Concurrency: @dt is read locked.
2336 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2337 struct lu_buf *buf, const char *name,
2338 struct lustre_capa *capa)
2340 struct osd_object *obj = osd_dt_obj(dt);
2341 struct inode *inode = obj->oo_inode;
2342 struct osd_thread_info *info = osd_oti_get(env);
2343 struct dentry *dentry = &info->oti_obj_dentry;
2345 /* version get is not real XATTR but uses xattr API */
2346 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2347 /* for version we are just using xattr API but change inode
2349 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2350 osd_object_version_get(env, dt, buf->lb_buf);
2351 return sizeof(dt_obj_version_t);
2354 LASSERT(dt_object_exists(dt));
2355 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2356 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2358 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2361 dentry->d_inode = inode;
2362 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2366 static int osd_declare_xattr_set(const struct lu_env *env,
2367 struct dt_object *dt,
2368 const struct lu_buf *buf, const char *name,
2369 int fl, struct thandle *handle)
2371 struct osd_thandle *oh;
2373 LASSERT(handle != NULL);
2375 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2376 /* no credits for version */
2380 oh = container_of0(handle, struct osd_thandle, ot_super);
2381 LASSERT(oh->ot_handle == NULL);
2383 OSD_DECLARE_OP(oh, xattr_set);
2384 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2390 * Set the 64-bit version for object
2392 static void osd_object_version_set(const struct lu_env *env,
2393 struct dt_object *dt,
2394 dt_obj_version_t *new_version)
2396 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2398 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2399 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2401 LDISKFS_I(inode)->i_fs_version = *new_version;
2402 /** Version is set after all inode operations are finished,
2403 * so we should mark it dirty here */
2404 inode->i_sb->s_op->dirty_inode(inode);
2408 * Concurrency: @dt is write locked.
2410 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2411 const struct lu_buf *buf, const char *name, int fl,
2412 struct thandle *handle, struct lustre_capa *capa)
2414 LASSERT(handle != NULL);
2416 /* version set is not real XATTR */
2417 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2418 /* for version we are just using xattr API but change inode
2420 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2421 osd_object_version_set(env, dt, buf->lb_buf);
2422 return sizeof(dt_obj_version_t);
2425 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2428 OSD_EXEC_OP(handle, xattr_set);
2429 return __osd_xattr_set(env, dt, buf, name, fl);
2433 * Concurrency: @dt is read locked.
2435 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2436 struct lu_buf *buf, struct lustre_capa *capa)
2438 struct osd_object *obj = osd_dt_obj(dt);
2439 struct inode *inode = obj->oo_inode;
2440 struct osd_thread_info *info = osd_oti_get(env);
2441 struct dentry *dentry = &info->oti_obj_dentry;
2443 LASSERT(dt_object_exists(dt));
2444 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2445 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2447 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2450 dentry->d_inode = inode;
2451 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2454 static int osd_declare_xattr_del(const struct lu_env *env,
2455 struct dt_object *dt, const char *name,
2456 struct thandle *handle)
2458 struct osd_thandle *oh;
2460 LASSERT(dt_object_exists(dt));
2461 LASSERT(handle != NULL);
2463 oh = container_of0(handle, struct osd_thandle, ot_super);
2464 LASSERT(oh->ot_handle == NULL);
2466 OSD_DECLARE_OP(oh, xattr_set);
2467 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2473 * Concurrency: @dt is write locked.
2475 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2476 const char *name, struct thandle *handle,
2477 struct lustre_capa *capa)
2479 struct osd_object *obj = osd_dt_obj(dt);
2480 struct inode *inode = obj->oo_inode;
2481 struct osd_thread_info *info = osd_oti_get(env);
2482 struct dentry *dentry = &info->oti_obj_dentry;
2485 LASSERT(dt_object_exists(dt));
2486 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2487 LASSERT(osd_write_locked(env, obj));
2488 LASSERT(handle != NULL);
2490 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2493 OSD_EXEC_OP(handle, xattr_set);
2495 dentry->d_inode = inode;
2496 rc = inode->i_op->removexattr(dentry, name);
2500 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2501 struct dt_object *dt,
2502 struct lustre_capa *old,
2505 struct osd_thread_info *info = osd_oti_get(env);
2506 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2507 struct osd_object *obj = osd_dt_obj(dt);
2508 struct osd_device *dev = osd_obj2dev(obj);
2509 struct lustre_capa_key *key = &info->oti_capa_key;
2510 struct lustre_capa *capa = &info->oti_capa;
2511 struct obd_capa *oc;
2512 struct md_capainfo *ci;
2516 if (!dev->od_fl_capa)
2517 RETURN(ERR_PTR(-ENOENT));
2519 LASSERT(dt_object_exists(dt));
2520 LINVRNT(osd_invariant(obj));
2522 /* renewal sanity check */
2523 if (old && osd_object_auth(env, dt, old, opc))
2524 RETURN(ERR_PTR(-EACCES));
2526 ci = md_capainfo(env);
2528 RETURN(ERR_PTR(-ENOENT));
2530 switch (ci->mc_auth) {
2534 capa->lc_uid = obj->oo_inode->i_uid;
2535 capa->lc_gid = obj->oo_inode->i_gid;
2536 capa->lc_flags = LC_ID_PLAIN;
2538 case LC_ID_CONVERT: {
2541 s[0] = obj->oo_inode->i_uid;
2542 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2543 s[2] = obj->oo_inode->i_gid;
2544 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2545 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2547 RETURN(ERR_PTR(rc));
2549 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2550 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2551 capa->lc_flags = LC_ID_CONVERT;
2555 RETURN(ERR_PTR(-EINVAL));
2558 capa->lc_fid = *fid;
2560 capa->lc_flags |= dev->od_capa_alg << 24;
2561 capa->lc_timeout = dev->od_capa_timeout;
2562 capa->lc_expiry = 0;
2564 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2566 LASSERT(!capa_is_expired(oc));
2570 cfs_spin_lock(&capa_lock);
2571 *key = dev->od_capa_keys[1];
2572 cfs_spin_unlock(&capa_lock);
2574 capa->lc_keyid = key->lk_keyid;
2575 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2577 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2579 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2580 RETURN(ERR_PTR(rc));
2583 oc = capa_add(dev->od_capa_hash, capa);
2587 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2589 struct osd_object *obj = osd_dt_obj(dt);
2590 struct inode *inode = obj->oo_inode;
2591 struct osd_thread_info *info = osd_oti_get(env);
2592 struct dentry *dentry = &info->oti_obj_dentry;
2593 struct file *file = &info->oti_file;
2598 dentry->d_inode = inode;
2599 file->f_dentry = dentry;
2600 file->f_mapping = inode->i_mapping;
2601 file->f_op = inode->i_fop;
2602 LOCK_INODE_MUTEX(inode);
2603 rc = file->f_op->fsync(file, dentry, 0);
2604 UNLOCK_INODE_MUTEX(inode);
2608 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2611 struct osd_object *obj = osd_dt_obj(dt);
2614 *data = (void *)obj->oo_inode;
2622 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2623 const struct dt_index_features *feat)
2625 struct iam_descr *descr;
2627 if (osd_object_is_root(o))
2628 return feat == &dt_directory_features;
2630 LASSERT(o->oo_dir != NULL);
2632 descr = o->oo_dir->od_container.ic_descr;
2633 if (feat == &dt_directory_features) {
2634 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2640 feat->dif_keysize_min <= descr->id_key_size &&
2641 descr->id_key_size <= feat->dif_keysize_max &&
2642 feat->dif_recsize_min <= descr->id_rec_size &&
2643 descr->id_rec_size <= feat->dif_recsize_max &&
2644 !(feat->dif_flags & (DT_IND_VARKEY |
2645 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2646 ergo(feat->dif_flags & DT_IND_UPDATE,
2647 1 /* XXX check that object (and file system) is
2652 static int osd_iam_container_init(const struct lu_env *env,
2653 struct osd_object *obj,
2654 struct osd_directory *dir)
2656 struct iam_container *bag = &dir->od_container;
2659 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2663 result = iam_container_setup(bag);
2667 if (osd_obj2dev(obj)->od_iop_mode) {
2668 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2670 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2676 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2678 iam_container_fini(bag);
2685 * Concurrency: no external locking is necessary.
2687 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2688 const struct dt_index_features *feat)
2692 struct osd_object *obj = osd_dt_obj(dt);
2693 struct osd_device *osd = osd_obj2dev(obj);
2695 LINVRNT(osd_invariant(obj));
2696 LASSERT(dt_object_exists(dt));
2698 if (osd_object_is_root(obj)) {
2699 dt->do_index_ops = &osd_index_ea_ops;
2701 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2702 dt->do_index_ops = &osd_index_ea_ops;
2703 if (S_ISDIR(obj->oo_inode->i_mode))
2708 } else if (unlikely(feat == &dt_otable_features)) {
2709 dt->do_index_ops = &osd_otable_ops;
2711 } else if (feat == &dt_acct_features) {
2712 dt->do_index_ops = &osd_acct_index_ops;
2715 } else if (!osd_has_index(obj)) {
2716 struct osd_directory *dir;
2721 cfs_spin_lock(&obj->oo_guard);
2722 if (obj->oo_dir == NULL)
2726 * Concurrent thread allocated container data.
2729 cfs_spin_unlock(&obj->oo_guard);
2731 * Now, that we have container data, serialize its
2734 cfs_down_write(&obj->oo_ext_idx_sem);
2736 * recheck under lock.
2738 if (!osd_has_index(obj))
2739 result = osd_iam_container_init(env, obj, dir);
2742 cfs_up_write(&obj->oo_ext_idx_sem);
2750 if (result == 0 && skip_iam == 0) {
2751 if (!osd_iam_index_probe(env, obj, feat))
2754 LINVRNT(osd_invariant(obj));
2759 static const struct dt_object_operations osd_obj_ops = {
2760 .do_read_lock = osd_object_read_lock,
2761 .do_write_lock = osd_object_write_lock,
2762 .do_read_unlock = osd_object_read_unlock,
2763 .do_write_unlock = osd_object_write_unlock,
2764 .do_write_locked = osd_object_write_locked,
2765 .do_attr_get = osd_attr_get,
2766 .do_declare_attr_set = osd_declare_attr_set,
2767 .do_attr_set = osd_attr_set,
2768 .do_ah_init = osd_ah_init,
2769 .do_declare_create = osd_declare_object_create,
2770 .do_create = osd_object_create,
2771 .do_declare_destroy = osd_declare_object_destroy,
2772 .do_destroy = osd_object_destroy,
2773 .do_index_try = osd_index_try,
2774 .do_declare_ref_add = osd_declare_object_ref_add,
2775 .do_ref_add = osd_object_ref_add,
2776 .do_declare_ref_del = osd_declare_object_ref_del,
2777 .do_ref_del = osd_object_ref_del,
2778 .do_xattr_get = osd_xattr_get,
2779 .do_declare_xattr_set = osd_declare_xattr_set,
2780 .do_xattr_set = osd_xattr_set,
2781 .do_declare_xattr_del = osd_declare_xattr_del,
2782 .do_xattr_del = osd_xattr_del,
2783 .do_xattr_list = osd_xattr_list,
2784 .do_capa_get = osd_capa_get,
2785 .do_object_sync = osd_object_sync,
2786 .do_data_get = osd_data_get,
2790 * dt_object_operations for interoperability mode
2791 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2793 static const struct dt_object_operations osd_obj_ea_ops = {
2794 .do_read_lock = osd_object_read_lock,
2795 .do_write_lock = osd_object_write_lock,
2796 .do_read_unlock = osd_object_read_unlock,
2797 .do_write_unlock = osd_object_write_unlock,
2798 .do_write_locked = osd_object_write_locked,
2799 .do_attr_get = osd_attr_get,
2800 .do_declare_attr_set = osd_declare_attr_set,
2801 .do_attr_set = osd_attr_set,
2802 .do_ah_init = osd_ah_init,
2803 .do_declare_create = osd_declare_object_create,
2804 .do_create = osd_object_ea_create,
2805 .do_declare_destroy = osd_declare_object_destroy,
2806 .do_destroy = osd_object_destroy,
2807 .do_index_try = osd_index_try,
2808 .do_declare_ref_add = osd_declare_object_ref_add,
2809 .do_ref_add = osd_object_ref_add,
2810 .do_declare_ref_del = osd_declare_object_ref_del,
2811 .do_ref_del = osd_object_ref_del,
2812 .do_xattr_get = osd_xattr_get,
2813 .do_declare_xattr_set = osd_declare_xattr_set,
2814 .do_xattr_set = osd_xattr_set,
2815 .do_declare_xattr_del = osd_declare_xattr_del,
2816 .do_xattr_del = osd_xattr_del,
2817 .do_xattr_list = osd_xattr_list,
2818 .do_capa_get = osd_capa_get,
2819 .do_object_sync = osd_object_sync,
2820 .do_data_get = osd_data_get,
2823 static int osd_index_declare_iam_delete(const struct lu_env *env,
2824 struct dt_object *dt,
2825 const struct dt_key *key,
2826 struct thandle *handle)
2828 struct osd_thandle *oh;
2830 oh = container_of0(handle, struct osd_thandle, ot_super);
2831 LASSERT(oh->ot_handle == NULL);
2833 OSD_DECLARE_OP(oh, delete);
2834 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2840 * delete a (key, value) pair from index \a dt specified by \a key
2842 * \param dt osd index object
2843 * \param key key for index
2844 * \param rec record reference
2845 * \param handle transaction handler
2848 * \retval -ve failure
2851 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2852 const struct dt_key *key,
2853 struct thandle *handle,
2854 struct lustre_capa *capa)
2856 struct osd_object *obj = osd_dt_obj(dt);
2857 struct osd_thandle *oh;
2858 struct iam_path_descr *ipd;
2859 struct iam_container *bag = &obj->oo_dir->od_container;
2864 LINVRNT(osd_invariant(obj));
2865 LASSERT(dt_object_exists(dt));
2866 LASSERT(bag->ic_object == obj->oo_inode);
2867 LASSERT(handle != NULL);
2869 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2872 OSD_EXEC_OP(handle, delete);
2874 ipd = osd_idx_ipd_get(env, bag);
2875 if (unlikely(ipd == NULL))
2878 oh = container_of0(handle, struct osd_thandle, ot_super);
2879 LASSERT(oh->ot_handle != NULL);
2880 LASSERT(oh->ot_handle->h_transaction != NULL);
2882 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2883 osd_ipd_put(env, bag, ipd);
2884 LINVRNT(osd_invariant(obj));
2888 static int osd_index_declare_ea_delete(const struct lu_env *env,
2889 struct dt_object *dt,
2890 const struct dt_key *key,
2891 struct thandle *handle)
2893 struct osd_thandle *oh;
2895 LASSERT(dt_object_exists(dt));
2896 LASSERT(handle != NULL);
2898 oh = container_of0(handle, struct osd_thandle, ot_super);
2899 LASSERT(oh->ot_handle == NULL);
2901 OSD_DECLARE_OP(oh, delete);
2902 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2904 LASSERT(osd_dt_obj(dt)->oo_inode);
2905 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2906 osd_dt_obj(dt)->oo_inode);
2907 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2908 osd_dt_obj(dt)->oo_inode);
2913 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2916 struct osd_fid_pack *rec;
2919 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2920 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2921 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2927 * Index delete function for interoperability mode (b11826).
2928 * It will remove the directory entry added by osd_index_ea_insert().
2929 * This entry is needed to maintain name->fid mapping.
2931 * \param key, key i.e. file entry to be deleted
2933 * \retval 0, on success
2934 * \retval -ve, on error
2936 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2937 const struct dt_key *key,
2938 struct thandle *handle,
2939 struct lustre_capa *capa)
2941 struct osd_object *obj = osd_dt_obj(dt);
2942 struct inode *dir = obj->oo_inode;
2943 struct dentry *dentry;
2944 struct osd_thandle *oh;
2945 struct ldiskfs_dir_entry_2 *de;
2946 struct buffer_head *bh;
2947 struct htree_lock *hlock = NULL;
2952 LINVRNT(osd_invariant(obj));
2953 LASSERT(dt_object_exists(dt));
2954 LASSERT(handle != NULL);
2956 OSD_EXEC_OP(handle, delete);
2958 oh = container_of(handle, struct osd_thandle, ot_super);
2959 LASSERT(oh->ot_handle != NULL);
2960 LASSERT(oh->ot_handle->h_transaction != NULL);
2962 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2965 dentry = osd_child_dentry_get(env, obj,
2966 (char *)key, strlen((char *)key));
2968 if (obj->oo_hl_head != NULL) {
2969 hlock = osd_oti_get(env)->oti_hlock;
2970 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2971 dir, LDISKFS_HLOCK_DEL);
2973 cfs_down_write(&obj->oo_ext_idx_sem);
2976 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2978 rc = ldiskfs_delete_entry(oh->ot_handle,
2985 ldiskfs_htree_unlock(hlock);
2987 cfs_up_write(&obj->oo_ext_idx_sem);
2989 LASSERT(osd_invariant(obj));
2994 * Lookup index for \a key and copy record to \a rec.
2996 * \param dt osd index object
2997 * \param key key for index
2998 * \param rec record reference
3000 * \retval +ve success : exact mach
3001 * \retval 0 return record with key not greater than \a key
3002 * \retval -ve failure
3004 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3005 struct dt_rec *rec, const struct dt_key *key,
3006 struct lustre_capa *capa)
3008 struct osd_object *obj = osd_dt_obj(dt);
3009 struct iam_path_descr *ipd;
3010 struct iam_container *bag = &obj->oo_dir->od_container;
3011 struct osd_thread_info *oti = osd_oti_get(env);
3012 struct iam_iterator *it = &oti->oti_idx_it;
3013 struct iam_rec *iam_rec;
3018 LASSERT(osd_invariant(obj));
3019 LASSERT(dt_object_exists(dt));
3020 LASSERT(bag->ic_object == obj->oo_inode);
3022 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3025 ipd = osd_idx_ipd_get(env, bag);
3029 /* got ipd now we can start iterator. */
3030 iam_it_init(it, bag, 0, ipd);
3032 rc = iam_it_get(it, (struct iam_key *)key);
3034 if (S_ISDIR(obj->oo_inode->i_mode))
3035 iam_rec = (struct iam_rec *)oti->oti_ldp;
3037 iam_rec = (struct iam_rec *) rec;
3039 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3040 if (S_ISDIR(obj->oo_inode->i_mode))
3041 osd_fid_unpack((struct lu_fid *) rec,
3042 (struct osd_fid_pack *)iam_rec);
3046 osd_ipd_put(env, bag, ipd);
3048 LINVRNT(osd_invariant(obj));
3053 static int osd_index_declare_iam_insert(const struct lu_env *env,
3054 struct dt_object *dt,
3055 const struct dt_rec *rec,
3056 const struct dt_key *key,
3057 struct thandle *handle)
3059 struct osd_thandle *oh;
3061 LASSERT(dt_object_exists(dt));
3062 LASSERT(handle != NULL);
3064 oh = container_of0(handle, struct osd_thandle, ot_super);
3065 LASSERT(oh->ot_handle == NULL);
3067 OSD_DECLARE_OP(oh, insert);
3068 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3074 * Inserts (key, value) pair in \a dt index object.
3076 * \param dt osd index object
3077 * \param key key for index
3078 * \param rec record reference
3079 * \param th transaction handler
3082 * \retval -ve failure
3084 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3085 const struct dt_rec *rec,
3086 const struct dt_key *key, struct thandle *th,
3087 struct lustre_capa *capa, int ignore_quota)
3089 struct osd_object *obj = osd_dt_obj(dt);
3090 struct iam_path_descr *ipd;
3091 struct osd_thandle *oh;
3092 struct iam_container *bag = &obj->oo_dir->od_container;
3093 #ifdef HAVE_QUOTA_SUPPORT
3094 cfs_cap_t save = cfs_curproc_cap_pack();
3096 struct osd_thread_info *oti = osd_oti_get(env);
3097 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3102 LINVRNT(osd_invariant(obj));
3103 LASSERT(dt_object_exists(dt));
3104 LASSERT(bag->ic_object == obj->oo_inode);
3105 LASSERT(th != NULL);
3107 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3110 OSD_EXEC_OP(th, insert);
3112 ipd = osd_idx_ipd_get(env, bag);
3113 if (unlikely(ipd == NULL))
3116 oh = container_of0(th, struct osd_thandle, ot_super);
3117 LASSERT(oh->ot_handle != NULL);
3118 LASSERT(oh->ot_handle->h_transaction != NULL);
3119 #ifdef HAVE_QUOTA_SUPPORT
3121 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3123 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3125 if (S_ISDIR(obj->oo_inode->i_mode))
3126 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3128 iam_rec = (struct iam_rec *) rec;
3129 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3131 #ifdef HAVE_QUOTA_SUPPORT
3132 cfs_curproc_cap_unpack(save);
3134 osd_ipd_put(env, bag, ipd);
3135 LINVRNT(osd_invariant(obj));
3140 * Calls ldiskfs_add_entry() to add directory entry
3141 * into the directory. This is required for
3142 * interoperability mode (b11826)
3144 * \retval 0, on success
3145 * \retval -ve, on error
3147 static int __osd_ea_add_rec(struct osd_thread_info *info,
3148 struct osd_object *pobj, struct inode *cinode,
3149 const char *name, const struct dt_rec *fid,
3150 struct htree_lock *hlock, struct thandle *th)
3152 struct ldiskfs_dentry_param *ldp;
3153 struct dentry *child;
3154 struct osd_thandle *oth;
3157 oth = container_of(th, struct osd_thandle, ot_super);
3158 LASSERT(oth->ot_handle != NULL);
3159 LASSERT(oth->ot_handle->h_transaction != NULL);
3161 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3163 /* XXX: remove fid_is_igif() check here.
3164 * IGIF check is just to handle insertion of .. when it is 'ROOT',
3165 * it is IGIF now but needs FID in dir entry as well for readdir
3167 * LU-838 should fix that and remove fid_is_igif() check */
3168 if (fid_is_igif((struct lu_fid *)fid) ||
3169 fid_is_norm((struct lu_fid *)fid)) {
3170 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3171 osd_get_ldiskfs_dirent_param(ldp, fid);
3172 child->d_fsdata = (void *)ldp;
3174 child->d_fsdata = NULL;
3176 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3182 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3183 * into the directory.Also sets flags into osd object to
3184 * indicate dot and dotdot are created. This is required for
3185 * interoperability mode (b11826)
3187 * \param dir directory for dot and dotdot fixup.
3188 * \param obj child object for linking
3190 * \retval 0, on success
3191 * \retval -ve, on error
3193 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3194 struct osd_object *dir,
3195 struct inode *parent_dir, const char *name,
3196 const struct dt_rec *dot_fid,
3197 const struct dt_rec *dot_dot_fid,
3200 struct inode *inode = dir->oo_inode;
3201 struct ldiskfs_dentry_param *dot_ldp;
3202 struct ldiskfs_dentry_param *dot_dot_ldp;
3203 struct osd_thandle *oth;
3206 oth = container_of(th, struct osd_thandle, ot_super);
3207 LASSERT(oth->ot_handle->h_transaction != NULL);
3208 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3210 if (strcmp(name, dot) == 0) {
3211 if (dir->oo_compat_dot_created) {
3214 LASSERT(inode == parent_dir);
3215 dir->oo_compat_dot_created = 1;
3218 } else if(strcmp(name, dotdot) == 0) {
3219 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3220 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3222 if (!dir->oo_compat_dot_created)
3224 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3225 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3226 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3231 /* in case of rename, dotdot is already created */
3232 if (dir->oo_compat_dotdot_created) {
3233 return __osd_ea_add_rec(info, dir, parent_dir, name,
3234 dot_dot_fid, NULL, th);
3237 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3238 inode, dot_ldp, dot_dot_ldp);
3240 dir->oo_compat_dotdot_created = 1;
3248 * It will call the appropriate osd_add* function and return the
3249 * value, return by respective functions.
3251 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3252 struct inode *cinode, const char *name,
3253 const struct dt_rec *fid, struct thandle *th)
3255 struct osd_thread_info *info = osd_oti_get(env);
3256 struct htree_lock *hlock;
3259 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3261 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3263 if (hlock != NULL) {
3264 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3267 cfs_down_write(&pobj->oo_ext_idx_sem);
3269 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3270 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3273 if (hlock != NULL) {
3274 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3275 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3277 cfs_down_write(&pobj->oo_ext_idx_sem);
3280 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3284 ldiskfs_htree_unlock(hlock);
3286 cfs_up_write(&pobj->oo_ext_idx_sem);
3292 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3293 struct osd_idmap_cache *oic)
3295 struct osd_scrub *scrub = &dev->od_scrub;
3296 struct lu_fid *fid = &oic->oic_fid;
3297 struct osd_inode_id *id = &oti->oti_id;
3303 rc = osd_oi_lookup(oti, dev, fid, id);
3304 if (rc != 0 && rc != -ENOENT)
3307 if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3310 if (thread_is_running(&scrub->os_thread)) {
3311 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3312 /* There is race condition between osd_oi_lookup and OI scrub.
3313 * The OI scrub finished just after osd_oi_lookup() failure.
3314 * Under such case, it is unnecessary to trigger OI scrub again,
3315 * but try to call osd_oi_lookup() again. */
3316 if (unlikely(rc == -EAGAIN))
3322 if (!scrub->os_no_scrub && ++once == 1) {
3323 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3325 rc = osd_scrub_start(dev);
3326 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID", rc = %d\n",
3332 RETURN(rc = -EREMCHG);
3336 * Calls ->lookup() to find dentry. From dentry get inode and
3337 * read inode's ea to get fid. This is required for interoperability
3340 * \retval 0, on success
3341 * \retval -ve, on error
3343 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3344 struct dt_rec *rec, const struct dt_key *key)
3346 struct inode *dir = obj->oo_inode;
3347 struct dentry *dentry;
3348 struct ldiskfs_dir_entry_2 *de;
3349 struct buffer_head *bh;
3350 struct lu_fid *fid = (struct lu_fid *) rec;
3351 struct htree_lock *hlock = NULL;
3355 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3357 dentry = osd_child_dentry_get(env, obj,
3358 (char *)key, strlen((char *)key));
3360 if (obj->oo_hl_head != NULL) {
3361 hlock = osd_oti_get(env)->oti_hlock;
3362 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3363 dir, LDISKFS_HLOCK_LOOKUP);
3365 cfs_down_read(&obj->oo_ext_idx_sem);
3368 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3370 struct osd_thread_info *oti = osd_oti_get(env);
3371 struct osd_idmap_cache *oic = &oti->oti_cache;
3372 struct osd_device *dev = osd_obj2dev(obj);
3373 struct osd_scrub *scrub = &dev->od_scrub;
3374 struct scrub_file *sf = &scrub->os_file;
3376 ino = le32_to_cpu(de->inode);
3377 rc = osd_get_fid_from_dentry(de, rec);
3379 /* done with de, release bh */
3382 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
3384 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3386 if (rc != 0 || !fid_is_norm(fid))
3389 oic->oic_fid = *fid;
3390 if ((scrub->os_pos_current <= ino) &&
3391 (sf->sf_flags & SF_INCONSISTENT ||
3392 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3394 rc = osd_consistency_check(oti, dev, oic);
3403 ldiskfs_htree_unlock(hlock);
3405 cfs_up_read(&obj->oo_ext_idx_sem);
3410 * Find the osd object for given fid.
3412 * \param fid need to find the osd object having this fid
3414 * \retval osd_object on success
3415 * \retval -ve on error
3417 struct osd_object *osd_object_find(const struct lu_env *env,
3418 struct dt_object *dt,
3419 const struct lu_fid *fid)
3421 struct lu_device *ludev = dt->do_lu.lo_dev;
3422 struct osd_object *child = NULL;
3423 struct lu_object *luch;
3424 struct lu_object *lo;
3426 luch = lu_object_find(env, ludev, fid, NULL);
3427 if (!IS_ERR(luch)) {
3428 if (lu_object_exists(luch)) {
3429 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3431 child = osd_obj(lo);
3433 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3434 "lu_object can't be located"
3435 DFID"\n", PFID(fid));
3437 if (child == NULL) {
3438 lu_object_put(env, luch);
3439 CERROR("Unable to get osd_object\n");
3440 child = ERR_PTR(-ENOENT);
3443 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3444 "lu_object does not exists "DFID"\n",
3446 lu_object_put(env, luch);
3447 child = ERR_PTR(-ENOENT);
3450 child = (void *)luch;
3456 * Put the osd object once done with it.
3458 * \param obj osd object that needs to be put
3460 static inline void osd_object_put(const struct lu_env *env,
3461 struct osd_object *obj)
3463 lu_object_put(env, &obj->oo_dt.do_lu);
3466 static int osd_index_declare_ea_insert(const struct lu_env *env,
3467 struct dt_object *dt,
3468 const struct dt_rec *rec,
3469 const struct dt_key *key,
3470 struct thandle *handle)
3472 struct osd_thandle *oh;
3474 LASSERT(dt_object_exists(dt));
3475 LASSERT(handle != NULL);
3477 oh = container_of0(handle, struct osd_thandle, ot_super);
3478 LASSERT(oh->ot_handle == NULL);
3480 OSD_DECLARE_OP(oh, insert);
3481 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3483 LASSERT(osd_dt_obj(dt)->oo_inode);
3484 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3485 osd_dt_obj(dt)->oo_inode);
3486 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3487 osd_dt_obj(dt)->oo_inode);
3493 * Index add function for interoperability mode (b11826).
3494 * It will add the directory entry.This entry is needed to
3495 * maintain name->fid mapping.
3497 * \param key it is key i.e. file entry to be inserted
3498 * \param rec it is value of given key i.e. fid
3500 * \retval 0, on success
3501 * \retval -ve, on error
3503 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3504 const struct dt_rec *rec,
3505 const struct dt_key *key, struct thandle *th,
3506 struct lustre_capa *capa, int ignore_quota)
3508 struct osd_object *obj = osd_dt_obj(dt);
3509 struct lu_fid *fid = (struct lu_fid *) rec;
3510 const char *name = (const char *)key;
3511 struct osd_object *child;
3512 #ifdef HAVE_QUOTA_SUPPORT
3513 cfs_cap_t save = cfs_curproc_cap_pack();
3519 LASSERT(osd_invariant(obj));
3520 LASSERT(dt_object_exists(dt));
3521 LASSERT(th != NULL);
3523 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3526 child = osd_object_find(env, dt, fid);
3527 if (!IS_ERR(child)) {
3528 #ifdef HAVE_QUOTA_SUPPORT
3530 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3532 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3534 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3535 #ifdef HAVE_QUOTA_SUPPORT
3536 cfs_curproc_cap_unpack(save);
3538 osd_object_put(env, child);
3540 rc = PTR_ERR(child);
3543 LASSERT(osd_invariant(obj));
3548 * Initialize osd Iterator for given osd index object.
3550 * \param dt osd index object
3553 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3554 struct dt_object *dt,
3556 struct lustre_capa *capa)
3558 struct osd_it_iam *it;
3559 struct osd_thread_info *oti = osd_oti_get(env);
3560 struct osd_object *obj = osd_dt_obj(dt);
3561 struct lu_object *lo = &dt->do_lu;
3562 struct iam_path_descr *ipd;
3563 struct iam_container *bag = &obj->oo_dir->od_container;
3565 LASSERT(lu_object_exists(lo));
3567 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3568 return ERR_PTR(-EACCES);
3571 ipd = osd_it_ipd_get(env, bag);
3572 if (likely(ipd != NULL)) {
3576 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3577 return (struct dt_it *)it;
3579 return ERR_PTR(-ENOMEM);
3583 * free given Iterator.
3586 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3588 struct osd_it_iam *it = (struct osd_it_iam *)di;
3589 struct osd_object *obj = it->oi_obj;
3591 iam_it_fini(&it->oi_it);
3592 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3593 lu_object_put(env, &obj->oo_dt.do_lu);
3597 * Move Iterator to record specified by \a key
3599 * \param di osd iterator
3600 * \param key key for index
3602 * \retval +ve di points to record with least key not larger than key
3603 * \retval 0 di points to exact matched key
3604 * \retval -ve failure
3607 static int osd_it_iam_get(const struct lu_env *env,
3608 struct dt_it *di, const struct dt_key *key)
3610 struct osd_it_iam *it = (struct osd_it_iam *)di;
3612 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3618 * \param di osd iterator
3621 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3623 struct osd_it_iam *it = (struct osd_it_iam *)di;
3625 iam_it_put(&it->oi_it);
3629 * Move iterator by one record
3631 * \param di osd iterator
3633 * \retval +1 end of container reached
3635 * \retval -ve failure
3638 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3640 struct osd_it_iam *it = (struct osd_it_iam *)di;
3642 return iam_it_next(&it->oi_it);
3646 * Return pointer to the key under iterator.
3649 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3650 const struct dt_it *di)
3652 struct osd_it_iam *it = (struct osd_it_iam *)di;
3654 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3658 * Return size of key under iterator (in bytes)
3661 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3663 struct osd_it_iam *it = (struct osd_it_iam *)di;
3665 return iam_it_key_size(&it->oi_it);
3668 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3669 int len, __u16 type)
3671 struct luda_type *lt;
3672 const unsigned align = sizeof(struct luda_type) - 1;
3674 /* check if file type is required */
3675 if (attr & LUDA_TYPE) {
3676 len = (len + align) & ~align;
3678 lt = (void *) ent->lde_name + len;
3679 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3680 ent->lde_attrs |= LUDA_TYPE;
3683 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3687 * build lu direct from backend fs dirent.
3690 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3691 struct lu_fid *fid, __u64 offset,
3692 char *name, __u16 namelen,
3693 __u16 type, __u32 attr)
3695 fid_cpu_to_le(&ent->lde_fid, fid);
3696 ent->lde_attrs = LUDA_FID;
3698 ent->lde_hash = cpu_to_le64(offset);
3699 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3701 strncpy(ent->lde_name, name, namelen);
3702 ent->lde_namelen = cpu_to_le16(namelen);
3704 /* append lustre attributes */
3705 osd_it_append_attrs(ent, attr, namelen, type);
3709 * Return pointer to the record under iterator.
3711 static int osd_it_iam_rec(const struct lu_env *env,
3712 const struct dt_it *di,
3713 struct dt_rec *dtrec, __u32 attr)
3715 struct osd_it_iam *it = (struct osd_it_iam *)di;
3716 struct osd_thread_info *info = osd_oti_get(env);
3719 if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
3720 const struct osd_fid_pack *rec;
3721 struct lu_fid *fid = &info->oti_fid;
3722 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3728 name = (char *)iam_it_key_get(&it->oi_it);
3730 RETURN(PTR_ERR(name));
3732 namelen = iam_it_key_size(&it->oi_it);
3734 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
3736 RETURN(PTR_ERR(rec));
3738 rc = osd_fid_unpack(fid, rec);
3742 hash = iam_it_store(&it->oi_it);
3744 /* IAM does not store object type in IAM index (dir) */
3745 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3748 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
3749 (struct iam_rec *)dtrec);
3756 * Returns cookie for current Iterator position.
3758 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3760 struct osd_it_iam *it = (struct osd_it_iam *)di;
3762 return iam_it_store(&it->oi_it);
3766 * Restore iterator from cookie.
3768 * \param di osd iterator
3769 * \param hash Iterator location cookie
3771 * \retval +ve di points to record with least key not larger than key.
3772 * \retval 0 di points to exact matched key
3773 * \retval -ve failure
3776 static int osd_it_iam_load(const struct lu_env *env,
3777 const struct dt_it *di, __u64 hash)
3779 struct osd_it_iam *it = (struct osd_it_iam *)di;
3781 return iam_it_load(&it->oi_it, hash);
3784 static const struct dt_index_operations osd_index_iam_ops = {
3785 .dio_lookup = osd_index_iam_lookup,
3786 .dio_declare_insert = osd_index_declare_iam_insert,
3787 .dio_insert = osd_index_iam_insert,
3788 .dio_declare_delete = osd_index_declare_iam_delete,
3789 .dio_delete = osd_index_iam_delete,
3791 .init = osd_it_iam_init,
3792 .fini = osd_it_iam_fini,
3793 .get = osd_it_iam_get,
3794 .put = osd_it_iam_put,
3795 .next = osd_it_iam_next,
3796 .key = osd_it_iam_key,
3797 .key_size = osd_it_iam_key_size,
3798 .rec = osd_it_iam_rec,
3799 .store = osd_it_iam_store,
3800 .load = osd_it_iam_load
3805 * Creates or initializes iterator context.
3807 * \retval struct osd_it_ea, iterator structure on success
3810 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3811 struct dt_object *dt,
3813 struct lustre_capa *capa)
3815 struct osd_object *obj = osd_dt_obj(dt);
3816 struct osd_thread_info *info = osd_oti_get(env);
3817 struct osd_it_ea *it = &info->oti_it_ea;
3818 struct lu_object *lo = &dt->do_lu;
3819 struct dentry *obj_dentry = &info->oti_it_dentry;
3821 LASSERT(lu_object_exists(lo));
3823 obj_dentry->d_inode = obj->oo_inode;
3824 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3825 obj_dentry->d_name.hash = 0;
3827 it->oie_rd_dirent = 0;
3828 it->oie_it_dirent = 0;
3829 it->oie_dirent = NULL;
3830 it->oie_buf = info->oti_it_ea_buf;
3832 it->oie_file.f_pos = 0;
3833 it->oie_file.f_dentry = obj_dentry;
3834 if (attr & LUDA_64BITHASH)
3835 it->oie_file.f_flags = O_64BITHASH;
3837 it->oie_file.f_flags = O_32BITHASH;
3838 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3839 it->oie_file.f_op = obj->oo_inode->i_fop;
3840 it->oie_file.private_data = NULL;
3842 RETURN((struct dt_it *) it);
3846 * Destroy or finishes iterator context.
3848 * \param di iterator structure to be destroyed
3850 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3852 struct osd_it_ea *it = (struct osd_it_ea *)di;
3853 struct osd_object *obj = it->oie_obj;
3854 struct inode *inode = obj->oo_inode;
3857 it->oie_file.f_op->release(inode, &it->oie_file);
3858 lu_object_put(env, &obj->oo_dt.do_lu);
3863 * It position the iterator at given key, so that next lookup continues from
3864 * that key Or it is similar to dio_it->load() but based on a key,
3865 * rather than file position.
3867 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3870 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3872 static int osd_it_ea_get(const struct lu_env *env,
3873 struct dt_it *di, const struct dt_key *key)
3875 struct osd_it_ea *it = (struct osd_it_ea *)di;
3878 LASSERT(((const char *)key)[0] == '\0');
3879 it->oie_file.f_pos = 0;
3880 it->oie_rd_dirent = 0;
3881 it->oie_it_dirent = 0;
3882 it->oie_dirent = NULL;
3890 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3895 * It is called internally by ->readdir(). It fills the
3896 * iterator's in-memory data structure with required
3897 * information i.e. name, namelen, rec_size etc.
3899 * \param buf in which information to be filled in.
3900 * \param name name of the file in given dir
3902 * \retval 0 on success
3903 * \retval 1 on buffer full
3905 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3906 loff_t offset, __u64 ino,
3909 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3910 struct osd_it_ea_dirent *ent = it->oie_dirent;
3911 struct lu_fid *fid = &ent->oied_fid;
3912 struct osd_fid_pack *rec;
3915 /* this should never happen */
3916 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3917 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3921 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3925 if (d_type & LDISKFS_DIRENT_LUFID) {
3926 rec = (struct osd_fid_pack*) (name + namelen + 1);
3928 if (osd_fid_unpack(fid, rec) != 0)
3931 d_type &= ~LDISKFS_DIRENT_LUFID;
3936 ent->oied_ino = ino;
3937 ent->oied_off = offset;
3938 ent->oied_namelen = namelen;
3939 ent->oied_type = d_type;
3941 memcpy(ent->oied_name, name, namelen);
3943 it->oie_rd_dirent++;
3944 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3949 * Calls ->readdir() to load a directory entry at a time
3950 * and stored it in iterator's in-memory data structure.
3952 * \param di iterator's in memory structure
3954 * \retval 0 on success
3955 * \retval -ve on error
3957 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3958 const struct dt_it *di)
3960 struct osd_it_ea *it = (struct osd_it_ea *)di;
3961 struct osd_object *obj = it->oie_obj;
3962 struct inode *inode = obj->oo_inode;
3963 struct htree_lock *hlock = NULL;
3967 it->oie_dirent = it->oie_buf;
3968 it->oie_rd_dirent = 0;
3970 if (obj->oo_hl_head != NULL) {
3971 hlock = osd_oti_get(env)->oti_hlock;
3972 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3973 inode, LDISKFS_HLOCK_READDIR);
3975 cfs_down_read(&obj->oo_ext_idx_sem);
3978 result = inode->i_fop->readdir(&it->oie_file, it,
3979 (filldir_t) osd_ldiskfs_filldir);
3982 ldiskfs_htree_unlock(hlock);
3984 cfs_up_read(&obj->oo_ext_idx_sem);
3986 if (it->oie_rd_dirent == 0) {
3989 it->oie_dirent = it->oie_buf;
3990 it->oie_it_dirent = 1;
3997 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3998 * to load a directory entry at a time and stored it in
3999 * iterator's in-memory data structure.
4001 * \param di iterator's in memory structure
4003 * \retval +ve iterator reached to end
4004 * \retval 0 iterator not reached to end
4005 * \retval -ve on error
4007 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4009 struct osd_it_ea *it = (struct osd_it_ea *)di;
4014 if (it->oie_it_dirent < it->oie_rd_dirent) {
4016 (void *) it->oie_dirent +
4017 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4018 it->oie_dirent->oied_namelen);
4019 it->oie_it_dirent++;
4022 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
4025 rc = osd_ldiskfs_it_fill(env, di);
4032 * Returns the key at current position from iterator's in memory structure.
4034 * \param di iterator's in memory structure
4036 * \retval key i.e. struct dt_key on success
4038 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4039 const struct dt_it *di)
4041 struct osd_it_ea *it = (struct osd_it_ea *)di;
4043 return (struct dt_key *)it->oie_dirent->oied_name;
4047 * Returns the key's size at current position from iterator's in memory structure.
4049 * \param di iterator's in memory structure
4051 * \retval key_size i.e. struct dt_key on success
4053 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4055 struct osd_it_ea *it = (struct osd_it_ea *)di;
4057 return it->oie_dirent->oied_namelen;
4062 * Returns the value (i.e. fid/igif) at current position from iterator's
4063 * in memory structure.
4065 * \param di struct osd_it_ea, iterator's in memory structure
4066 * \param attr attr requested for dirent.
4067 * \param lde lustre dirent
4069 * \retval 0 no error and \param lde has correct lustre dirent.
4070 * \retval -ve on error
4072 static inline int osd_it_ea_rec(const struct lu_env *env,
4073 const struct dt_it *di,
4074 struct dt_rec *dtrec, __u32 attr)
4076 struct osd_it_ea *it = (struct osd_it_ea *)di;
4077 struct osd_object *obj = it->oie_obj;
4078 struct osd_device *dev = osd_obj2dev(obj);
4079 struct osd_scrub *scrub = &dev->od_scrub;
4080 struct scrub_file *sf = &scrub->os_file;
4081 struct osd_thread_info *oti = osd_oti_get(env);
4082 struct osd_idmap_cache *oic = &oti->oti_cache;
4083 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4084 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4085 __u32 ino = it->oie_dirent->oied_ino;
4089 if (!fid_is_sane(fid)) {
4090 rc = osd_ea_fid_get(env, obj, ino, fid, &oic->oic_lid);
4094 osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
4097 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4098 it->oie_dirent->oied_name,
4099 it->oie_dirent->oied_namelen,
4100 it->oie_dirent->oied_type, attr);
4102 if (!fid_is_norm(fid))
4105 oic->oic_fid = *fid;
4106 if ((scrub->os_pos_current <= ino) &&
4107 (sf->sf_flags & SF_INCONSISTENT ||
4108 ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
4109 rc = osd_consistency_check(oti, dev, oic);
4115 * Returns a cookie for current position of the iterator head, so that
4116 * user can use this cookie to load/start the iterator next time.
4118 * \param di iterator's in memory structure
4120 * \retval cookie for current position, on success
4122 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4124 struct osd_it_ea *it = (struct osd_it_ea *)di;
4126 return it->oie_dirent->oied_off;
4130 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4131 * to load a directory entry at a time and stored it i inn,
4132 * in iterator's in-memory data structure.
4134 * \param di struct osd_it_ea, iterator's in memory structure
4136 * \retval +ve on success
4137 * \retval -ve on error
4139 static int osd_it_ea_load(const struct lu_env *env,
4140 const struct dt_it *di, __u64 hash)
4142 struct osd_it_ea *it = (struct osd_it_ea *)di;
4146 it->oie_file.f_pos = hash;
4148 rc = osd_ldiskfs_it_fill(env, di);
4156 * Index lookup function for interoperability mode (b11826).
4158 * \param key, key i.e. file name to be searched
4160 * \retval +ve, on success
4161 * \retval -ve, on error
4163 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4164 struct dt_rec *rec, const struct dt_key *key,
4165 struct lustre_capa *capa)
4167 struct osd_object *obj = osd_dt_obj(dt);
4172 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4173 LINVRNT(osd_invariant(obj));
4175 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4178 rc = osd_ea_lookup_rec(env, obj, rec, key);
4186 * Index and Iterator operations for interoperability
4187 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4189 static const struct dt_index_operations osd_index_ea_ops = {
4190 .dio_lookup = osd_index_ea_lookup,
4191 .dio_declare_insert = osd_index_declare_ea_insert,
4192 .dio_insert = osd_index_ea_insert,
4193 .dio_declare_delete = osd_index_declare_ea_delete,
4194 .dio_delete = osd_index_ea_delete,
4196 .init = osd_it_ea_init,
4197 .fini = osd_it_ea_fini,
4198 .get = osd_it_ea_get,
4199 .put = osd_it_ea_put,
4200 .next = osd_it_ea_next,
4201 .key = osd_it_ea_key,
4202 .key_size = osd_it_ea_key_size,
4203 .rec = osd_it_ea_rec,
4204 .store = osd_it_ea_store,
4205 .load = osd_it_ea_load
4209 static void *osd_key_init(const struct lu_context *ctx,
4210 struct lu_context_key *key)
4212 struct osd_thread_info *info;
4214 OBD_ALLOC_PTR(info);
4216 return ERR_PTR(-ENOMEM);
4218 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4219 if (info->oti_it_ea_buf == NULL)
4222 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4224 info->oti_hlock = ldiskfs_htree_lock_alloc();
4225 if (info->oti_hlock == NULL)
4231 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4234 return ERR_PTR(-ENOMEM);
4237 static void osd_key_fini(const struct lu_context *ctx,
4238 struct lu_context_key *key, void* data)
4240 struct osd_thread_info *info = data;
4242 if (info->oti_hlock != NULL)
4243 ldiskfs_htree_lock_free(info->oti_hlock);
4244 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4248 static void osd_key_exit(const struct lu_context *ctx,
4249 struct lu_context_key *key, void *data)
4251 struct osd_thread_info *info = data;
4253 LASSERT(info->oti_r_locks == 0);
4254 LASSERT(info->oti_w_locks == 0);
4255 LASSERT(info->oti_txns == 0);
4258 /* type constructor/destructor: osd_type_init, osd_type_fini */
4259 LU_TYPE_INIT_FINI(osd, &osd_key);
4261 struct lu_context_key osd_key = {
4262 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
4263 .lct_init = osd_key_init,
4264 .lct_fini = osd_key_fini,
4265 .lct_exit = osd_key_exit
4269 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4270 const char *name, struct lu_device *next)
4272 struct osd_device *osd = osd_dev(d);
4274 strncpy(osd->od_svname, name, MAX_OBD_NAME);
4275 return osd_procfs_init(osd, name);
4278 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4282 osd_scrub_cleanup(env, o);
4285 fsfilt_put_ops(o->od_fsops);
4289 /* shutdown quota slave instance associated with the device */
4290 if (o->od_quota_slave != NULL) {
4291 qsd_fini(env, o->od_quota_slave);
4292 o->od_quota_slave = NULL;
4298 static int osd_mount(const struct lu_env *env,
4299 struct osd_device *o, struct lustre_cfg *cfg)
4301 struct lustre_mount_info *lmi;
4302 const char *dev = lustre_cfg_string(cfg, 0);
4303 struct lustre_disk_data *ldd;
4304 struct lustre_sb_info *lsi;
4309 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4310 if (o->od_fsops == NULL) {
4311 CERROR("Can't find fsfilt_ldiskfs\n");
4315 if (o->od_mount != NULL) {
4316 CERROR("Already mounted (%s)\n", dev);
4321 lmi = server_get_mount(dev);
4323 CERROR("Cannot get mount info for %s!\n", dev);
4327 LASSERT(lmi != NULL);
4328 /* save lustre_mount_info in dt_device */
4330 o->od_mnt = lmi->lmi_mnt;
4332 lsi = s2lsi(lmi->lmi_sb);
4335 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4337 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", dev);
4341 if (ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
4342 rc = osd_compat_init(o);
4344 CERROR("%s: can't initialize compats: %d\n", dev, rc);
4350 static struct lu_device *osd_device_fini(const struct lu_env *env,
4351 struct lu_device *d)
4356 osd_compat_fini(osd_dev(d));
4358 shrink_dcache_sb(osd_sb(osd_dev(d)));
4359 osd_sync(env, lu2dt_dev(d));
4361 rc = osd_procfs_fini(osd_dev(d));
4363 CERROR("proc fini error %d \n", rc);
4364 RETURN (ERR_PTR(rc));
4367 if (osd_dev(d)->od_mount)
4368 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4369 osd_dev(d)->od_mount->lmi_mnt);
4370 osd_dev(d)->od_mount = NULL;
4375 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4376 struct lu_device_type *t,
4377 struct lustre_cfg *cfg)
4379 struct lu_device *l;
4380 struct osd_device *o;
4386 result = dt_device_init(&o->od_dt_dev, t);
4389 l->ld_ops = &osd_lu_ops;
4390 o->od_dt_dev.dd_ops = &osd_dt_ops;
4391 cfs_spin_lock_init(&o->od_osfs_lock);
4392 cfs_mutex_init(&o->od_otable_mutex);
4393 o->od_osfs_age = cfs_time_shift_64(-1000);
4394 o->od_capa_hash = init_capa_hash();
4395 if (o->od_capa_hash == NULL) {
4396 dt_device_fini(&o->od_dt_dev);
4397 l = ERR_PTR(-ENOMEM);
4400 l = ERR_PTR(result);
4405 l = ERR_PTR(-ENOMEM);
4409 static struct lu_device *osd_device_free(const struct lu_env *env,
4410 struct lu_device *d)
4412 struct osd_device *o = osd_dev(d);
4415 cleanup_capa_hash(o->od_capa_hash);
4416 dt_device_fini(&o->od_dt_dev);
4421 static int osd_process_config(const struct lu_env *env,
4422 struct lu_device *d, struct lustre_cfg *cfg)
4424 struct osd_device *o = osd_dev(d);
4428 switch(cfg->lcfg_command) {
4430 err = osd_mount(env, o, cfg);
4433 err = osd_shutdown(env, o);
4442 static int osd_recovery_complete(const struct lu_env *env,
4443 struct lu_device *d)
4448 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4449 struct lu_device *dev)
4451 struct osd_device *osd = osd_dev(dev);
4455 /* 1. setup scrub, including OI files initialization */
4456 result = osd_scrub_setup(env, osd);
4460 /* 2. setup quota slave instance */
4461 osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
4462 osd->od_proc_entry);
4463 if (IS_ERR(osd->od_quota_slave)) {
4464 result = PTR_ERR(osd->od_quota_slave);
4465 osd->od_quota_slave = NULL;
4469 if (!lu_device_is_md(pdev))
4472 /* 3. setup local objects */
4473 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4477 static const struct lu_object_operations osd_lu_obj_ops = {
4478 .loo_object_init = osd_object_init,
4479 .loo_object_delete = osd_object_delete,
4480 .loo_object_release = osd_object_release,
4481 .loo_object_free = osd_object_free,
4482 .loo_object_print = osd_object_print,
4483 .loo_object_invariant = osd_object_invariant
4486 const struct lu_device_operations osd_lu_ops = {
4487 .ldo_object_alloc = osd_object_alloc,
4488 .ldo_process_config = osd_process_config,
4489 .ldo_recovery_complete = osd_recovery_complete,
4490 .ldo_prepare = osd_prepare,
4493 static const struct lu_device_type_operations osd_device_type_ops = {
4494 .ldto_init = osd_type_init,
4495 .ldto_fini = osd_type_fini,
4497 .ldto_start = osd_type_start,
4498 .ldto_stop = osd_type_stop,
4500 .ldto_device_alloc = osd_device_alloc,
4501 .ldto_device_free = osd_device_free,
4503 .ldto_device_init = osd_device_init,
4504 .ldto_device_fini = osd_device_fini
4507 static struct lu_device_type osd_device_type = {
4508 .ldt_tags = LU_DEVICE_DT,
4509 .ldt_name = LUSTRE_OSD_NAME,
4510 .ldt_ops = &osd_device_type_ops,
4511 .ldt_ctx_tags = LCT_LOCAL,
4515 * lprocfs legacy support.
4517 static struct obd_ops osd_obd_device_ops = {
4518 .o_owner = THIS_MODULE
4521 static int __init osd_mod_init(void)
4523 struct lprocfs_static_vars lvars;
4526 lprocfs_osd_init_vars(&lvars);
4527 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4528 LUSTRE_OSD_NAME, &osd_device_type);
4531 static void __exit osd_mod_exit(void)
4533 class_unregister_type(LUSTRE_OSD_NAME);
4536 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4537 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4538 MODULE_LICENSE("GPL");
4540 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);