4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
60 * struct OBD_{ALLOC,FREE}*()
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
68 #include <lustre_fid.h>
70 #include "osd_internal.h"
73 /* llo_* api support */
74 #include <md_object.h>
76 #ifdef HAVE_LDISKFS_PDO
78 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
79 "ldiskfs with parallel directory operations");
84 static const char dot[] = ".";
85 static const char dotdot[] = "..";
86 static const char remote_obj_dir[] = "REM_OBJ_DIR";
88 static const struct lu_object_operations osd_lu_obj_ops;
89 static const struct dt_object_operations osd_obj_ops;
90 static const struct dt_object_operations osd_obj_ea_ops;
91 static const struct dt_index_operations osd_index_iam_ops;
92 static const struct dt_index_operations osd_index_ea_ops;
94 static int osd_has_index(const struct osd_object *obj)
96 return obj->oo_dt.do_index_ops != NULL;
99 static int osd_object_invariant(const struct lu_object *l)
101 return osd_invariant(osd_obj(l));
104 #ifdef HAVE_QUOTA_SUPPORT
106 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
108 struct md_ucred *uc = md_ucred(env);
113 save->oc_uid = current_fsuid();
114 save->oc_gid = current_fsgid();
115 save->oc_cap = current_cap();
116 if ((tc = prepare_creds())) {
117 tc->fsuid = uc->mu_fsuid;
118 tc->fsgid = uc->mu_fsgid;
121 /* XXX not suboptimal */
122 cfs_curproc_cap_unpack(uc->mu_cap);
126 osd_pop_ctxt(struct osd_ctxt *save)
130 if ((tc = prepare_creds())) {
131 tc->fsuid = save->oc_uid;
132 tc->fsgid = save->oc_gid;
133 tc->cap_effective = save->oc_cap;
140 * Concurrency: doesn't matter
142 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
144 return osd_oti_get(env)->oti_r_locks > 0;
148 * Concurrency: doesn't matter
150 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
152 struct osd_thread_info *oti = osd_oti_get(env);
153 return oti->oti_w_locks > 0 && o->oo_owner == env;
157 * Concurrency: doesn't access mutable data
159 static int osd_root_get(const struct lu_env *env,
160 struct dt_device *dev, struct lu_fid *f)
162 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
166 static inline int osd_qid_type(struct osd_thandle *oh, int i)
168 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
171 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
173 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
176 void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
177 int type, uid_t id, struct inode *inode)
180 int i, allocated = 0;
181 struct osd_object *obj;
185 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
188 /* id entry is allocated in the quota file */
189 if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
192 for (i = 0; i < oh->ot_id_cnt; i++) {
193 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
197 if (unlikely(i >= OSD_MAX_UGID_CNT)) {
198 CERROR("more than %d uid/gids for a transaction?\n", i);
202 oh->ot_id_array[i] = id;
203 osd_qid_set_type(oh, i, type);
205 obj = osd_dt_obj(dt);
206 oh->ot_credits += (allocated || id == 0) ?
207 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
212 * OSD object methods.
216 * Concurrency: no concurrent access is possible that early in object
219 static struct lu_object *osd_object_alloc(const struct lu_env *env,
220 const struct lu_object_header *hdr,
223 struct osd_object *mo;
229 l = &mo->oo_dt.do_lu;
230 dt_object_init(&mo->oo_dt, NULL, d);
231 if (osd_dev(d)->od_iop_mode)
232 mo->oo_dt.do_ops = &osd_obj_ea_ops;
234 mo->oo_dt.do_ops = &osd_obj_ops;
236 l->lo_ops = &osd_lu_obj_ops;
237 cfs_init_rwsem(&mo->oo_sem);
238 cfs_init_rwsem(&mo->oo_ext_idx_sem);
239 cfs_spin_lock_init(&mo->oo_guard);
247 * retrieve object from backend ext fs.
249 struct inode *osd_iget(struct osd_thread_info *info,
250 struct osd_device *dev,
251 const struct osd_inode_id *id)
253 struct inode *inode = NULL;
255 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
257 CERROR("Cannot get inode, rc = %li\n", PTR_ERR(inode));
258 } else if (id->oii_gen != OSD_OII_NOGEN &&
259 inode->i_generation != id->oii_gen) {
261 inode = ERR_PTR(-ESTALE);
262 } else if (inode->i_nlink == 0) {
263 /* due to parallel readdir and unlink,
264 * we can have dead inode here. */
265 CWARN("stale inode\n");
266 make_bad_inode(inode);
268 inode = ERR_PTR(-ESTALE);
269 } else if (is_bad_inode(inode)) {
270 CERROR("bad inode %lx\n",inode->i_ino);
272 inode = ERR_PTR(-ENOENT);
274 /* Do not update file c/mtime in ldiskfs.
275 * NB: we don't have any lock to protect this because we don't
276 * have reference on osd_object now, but contention with
277 * another lookup + attr_set can't happen in the tiny window
278 * between if (...) and set S_NOCMTIME. */
279 if (!(inode->i_flags & S_NOCMTIME))
280 inode->i_flags |= S_NOCMTIME;
285 static int osd_fid_lookup(const struct lu_env *env,
286 struct osd_object *obj, const struct lu_fid *fid)
288 struct osd_thread_info *info;
289 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
290 struct osd_device *dev;
291 struct osd_inode_id *id;
295 LINVRNT(osd_invariant(obj));
296 LASSERT(obj->oo_inode == NULL);
297 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
299 * This assertion checks that osd layer sees only local
300 * fids. Unfortunately it is somewhat expensive (does a
301 * cache-lookup). Disabling it for production/acceptance-testing.
303 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
307 info = osd_oti_get(env);
312 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
315 result = osd_oi_lookup(info, dev, fid, id);
317 if (result == -ENOENT)
322 inode = osd_iget(info, dev, id);
325 * If fid wasn't found in oi, inode-less object is
326 * created, for which lu_object_exists() returns
327 * false. This is used in a (frequent) case when
328 * objects are created as locking anchors or
329 * place holders for objects yet to be created.
331 result = PTR_ERR(inode);
335 obj->oo_inode = inode;
336 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
337 if (dev->od_iop_mode) {
338 obj->oo_compat_dot_created = 1;
339 obj->oo_compat_dotdot_created = 1;
342 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
345 LASSERT(obj->oo_hl_head == NULL);
346 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
347 if (obj->oo_hl_head == NULL) {
348 obj->oo_inode = NULL;
353 LINVRNT(osd_invariant(obj));
358 * Concurrency: shouldn't matter.
360 static void osd_object_init0(struct osd_object *obj)
362 LASSERT(obj->oo_inode != NULL);
363 obj->oo_dt.do_body_ops = &osd_body_ops;
364 obj->oo_dt.do_lu.lo_header->loh_attr |=
365 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
369 * Concurrency: no concurrent access is possible that early in object
372 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
373 const struct lu_object_conf *unused)
375 struct osd_object *obj = osd_obj(l);
378 LINVRNT(osd_invariant(obj));
380 result = osd_fid_lookup(env, obj, lu_object_fid(l));
381 obj->oo_dt.do_body_ops = &osd_body_ops_new;
383 if (obj->oo_inode != NULL)
384 osd_object_init0(obj);
386 LINVRNT(osd_invariant(obj));
391 * Concurrency: no concurrent access is possible that late in object
394 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
396 struct osd_object *obj = osd_obj(l);
398 LINVRNT(osd_invariant(obj));
400 dt_object_fini(&obj->oo_dt);
401 if (obj->oo_hl_head != NULL)
402 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
407 * Concurrency: no concurrent access is possible that late in object
410 static void osd_index_fini(struct osd_object *o)
412 struct iam_container *bag;
414 if (o->oo_dir != NULL) {
415 bag = &o->oo_dir->od_container;
416 if (o->oo_inode != NULL) {
417 if (bag->ic_object == o->oo_inode)
418 iam_container_fini(bag);
420 OBD_FREE_PTR(o->oo_dir);
426 * Concurrency: no concurrent access is possible that late in object
427 * life-cycle (for all existing callers, that is. New callers have to provide
428 * their own locking.)
430 static int osd_inode_unlinked(const struct inode *inode)
432 return inode->i_nlink == 0;
436 OSD_TXN_OI_DELETE_CREDITS = 20,
437 OSD_TXN_INODE_DELETE_CREDITS = 20
444 #if OSD_THANDLE_STATS
446 * Set time when the handle is allocated
448 static void osd_th_alloced(struct osd_thandle *oth)
450 oth->oth_alloced = cfs_time_current();
454 * Set time when the handle started
456 static void osd_th_started(struct osd_thandle *oth)
458 oth->oth_started = cfs_time_current();
462 * Helper function to convert time interval to microseconds packed in
463 * long int (default time units for the counter in "stats" initialized
464 * by lu_time_init() )
466 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
470 cfs_duration_usec(cfs_time_sub(end, start), &val);
471 return val.tv_sec * 1000000 + val.tv_usec;
475 * Check whether the we deal with this handle for too long.
477 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
478 cfs_time_t alloced, cfs_time_t started,
481 cfs_time_t now = cfs_time_current();
483 LASSERT(dev != NULL);
485 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
486 interval_to_usec(alloced, started));
487 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
488 interval_to_usec(started, closed));
489 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
490 interval_to_usec(closed, now));
492 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
493 CWARN("transaction handle %p was open for too long: "
495 "alloced "CFS_TIME_T" ,"
496 "started "CFS_TIME_T" ,"
497 "closed "CFS_TIME_T"\n",
498 oth, now, alloced, started, closed);
499 libcfs_debug_dumpstack(NULL);
503 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
505 cfs_time_t __closed = cfs_time_current(); \
506 cfs_time_t __alloced = oth->oth_alloced; \
507 cfs_time_t __started = oth->oth_started; \
510 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
513 #else /* OSD_THANDLE_STATS */
515 #define osd_th_alloced(h) do {} while(0)
516 #define osd_th_started(h) do {} while(0)
517 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
519 #endif /* OSD_THANDLE_STATS */
522 * Concurrency: doesn't access mutable data.
524 static int osd_param_is_sane(const struct osd_device *dev,
525 const struct thandle *th)
527 struct osd_thandle *oh;
528 oh = container_of0(th, struct osd_thandle, ot_super);
529 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
533 * Concurrency: shouldn't matter.
535 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
536 static void osd_trans_commit_cb(struct super_block *sb,
537 struct journal_callback *jcb, int error)
539 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
542 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
543 struct thandle *th = &oh->ot_super;
544 struct lu_device *lud = &th->th_dev->dd_lu_dev;
545 struct dt_txn_commit_cb *dcb, *tmp;
547 LASSERT(oh->ot_handle == NULL);
550 CERROR("transaction @0x%p commit error: %d\n", th, error);
552 dt_txn_hook_commit(th);
554 /* call per-transaction callbacks if any */
555 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
556 cfs_list_del_init(&dcb->dcb_linkage);
557 dcb->dcb_func(NULL, th, dcb, error);
560 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
564 lu_context_exit(&th->th_ctx);
565 lu_context_fini(&th->th_ctx);
569 static struct thandle *osd_trans_create(const struct lu_env *env,
572 struct osd_thread_info *oti = osd_oti_get(env);
573 struct osd_iobuf *iobuf = &oti->oti_iobuf;
574 struct osd_thandle *oh;
578 /* on pending IO in this thread should left from prev. request */
579 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
581 th = ERR_PTR(-ENOMEM);
582 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
587 th->th_tags = LCT_TX_HANDLE;
589 oti->oti_dev = osd_dt_dev(d);
590 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
597 * Concurrency: shouldn't matter.
599 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
602 struct osd_thread_info *oti = osd_oti_get(env);
603 struct osd_device *dev = osd_dt_dev(d);
605 struct osd_thandle *oh;
610 LASSERT(current->journal_info == NULL);
612 oh = container_of0(th, struct osd_thandle, ot_super);
614 LASSERT(oh->ot_handle == NULL);
616 rc = dt_txn_hook_start(env, d, th);
620 if (!osd_param_is_sane(dev, th)) {
621 CWARN("%s: too many transaction credits (%d > %d)\n",
622 d->dd_lu_dev.ld_obd->obd_name, oh->ot_credits,
623 osd_journal(dev)->j_max_transaction_buffers);
624 /* XXX Limit the credits to 'max_transaction_buffers', and
625 * let the underlying filesystem to catch the error if
626 * we really need so many credits.
628 * This should be removed when we can calculate the
629 * credits precisely. */
630 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
631 #ifdef OSD_TRACK_DECLARES
632 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
633 oh->ot_declare_attr_set, oh->ot_declare_punch,
634 oh->ot_declare_xattr_set);
635 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
636 oh->ot_declare_create, oh->ot_declare_ref_add,
637 oh->ot_declare_ref_del, oh->ot_declare_write);
638 CERROR(" insert: %d, delete: %d, destroy: %d\n",
639 oh->ot_declare_insert, oh->ot_declare_delete,
640 oh->ot_declare_destroy);
645 * XXX temporary stuff. Some abstraction layer should
648 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
652 LASSERT(oti->oti_txns == 0);
653 lu_context_init(&th->th_ctx, th->th_tags);
654 lu_context_enter(&th->th_ctx);
656 lu_device_get(&d->dd_lu_dev);
657 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
661 * XXX: current rule is that we first start tx,
662 * then lock object(s), but we can't use
663 * this rule for data (due to locking specifics
664 * in ldiskfs). also in long-term we'd like to
665 * use usually-used (locks;tx) ordering. so,
666 * UGLY thing is that we'll use one ordering for
667 * data (ofd) and reverse ordering for metadata
668 * (mdd). then at some point we'll fix the latter
670 if (lu_device_is_md(&d->dd_lu_dev)) {
671 LASSERT(oti->oti_r_locks == 0);
672 LASSERT(oti->oti_w_locks == 0);
685 * Concurrency: shouldn't matter.
687 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
690 struct osd_thandle *oh;
691 struct osd_thread_info *oti = osd_oti_get(env);
692 struct osd_iobuf *iobuf = &oti->oti_iobuf;
696 oh = container_of0(th, struct osd_thandle, ot_super);
698 if (oh->ot_handle != NULL) {
699 handle_t *hdl = oh->ot_handle;
701 hdl->h_sync = th->th_sync;
704 * add commit callback
705 * notice we don't do this in osd_trans_start()
706 * as underlying transaction can change during truncate
708 osd_journal_callback_set(hdl, osd_trans_commit_cb,
711 LASSERT(oti->oti_txns == 1);
714 * XXX: current rule is that we first start tx,
715 * then lock object(s), but we can't use
716 * this rule for data (due to locking specifics
717 * in ldiskfs). also in long-term we'd like to
718 * use usually-used (locks;tx) ordering. so,
719 * UGLY thing is that we'll use one ordering for
720 * data (ofd) and reverse ordering for metadata
721 * (mdd). then at some point we'll fix the latter
723 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
724 LASSERT(oti->oti_r_locks == 0);
725 LASSERT(oti->oti_w_locks == 0);
727 rc = dt_txn_hook_stop(env, th);
729 CERROR("Failure in transaction hook: %d\n", rc);
730 oh->ot_handle = NULL;
731 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
732 rc = ldiskfs_journal_stop(hdl));
734 CERROR("Failure to stop transaction: %d\n", rc);
739 /* as we want IO to journal and data IO be concurrent, we don't block
740 * awaiting data IO completion in osd_do_bio(), instead we wait here
741 * once transaction is submitted to the journal. all reqular requests
742 * don't do direct IO (except read/write), thus this wait_event becomes
745 * IMPORTANT: we have to wait till any IO submited by the thread is
746 * completed otherwise iobuf may be corrupted by different request
748 cfs_wait_event(iobuf->dr_wait,
749 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
751 rc = iobuf->dr_error;
756 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
758 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
761 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
767 * Called just before object is freed. Releases all resources except for
768 * object itself (that is released by osd_object_free()).
770 * Concurrency: no concurrent access is possible that late in object
773 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
775 struct osd_object *obj = osd_obj(l);
776 struct inode *inode = obj->oo_inode;
778 LINVRNT(osd_invariant(obj));
781 * If object is unlinked remove fid->ino mapping from object index.
787 obj->oo_inode = NULL;
792 * Concurrency: ->loo_object_release() is called under site spin-lock.
794 static void osd_object_release(const struct lu_env *env,
800 * Concurrency: shouldn't matter.
802 static int osd_object_print(const struct lu_env *env, void *cookie,
803 lu_printer_t p, const struct lu_object *l)
805 struct osd_object *o = osd_obj(l);
808 if (o->oo_dir != NULL)
809 d = o->oo_dir->od_container.ic_descr;
812 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
814 o->oo_inode ? o->oo_inode->i_ino : 0UL,
815 o->oo_inode ? o->oo_inode->i_generation : 0,
816 d ? d->id_ops->id_name : "plain");
820 * Concurrency: shouldn't matter.
822 int osd_statfs(const struct lu_env *env, struct dt_device *d,
823 struct obd_statfs *sfs)
825 struct osd_device *osd = osd_dt_dev(d);
826 struct super_block *sb = osd_sb(osd);
827 struct kstatfs *ksfs;
830 /* osd_lproc.c call this without env, allocate ksfs for that case */
831 if (unlikely(env == NULL)) {
836 ksfs = &osd_oti_get(env)->oti_ksfs;
839 cfs_spin_lock(&osd->od_osfs_lock);
841 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
842 result = ll_do_statfs(sb, ksfs);
843 if (likely(result == 0)) { /* N.B. statfs can't really fail */
844 osd->od_osfs_age = cfs_time_current_64();
845 statfs_pack(&osd->od_statfs, ksfs);
849 if (likely(result == 0))
850 *sfs = osd->od_statfs;
851 cfs_spin_unlock(&osd->od_osfs_lock);
853 if (unlikely(env == NULL))
860 * Concurrency: doesn't access mutable data.
862 static void osd_conf_get(const struct lu_env *env,
863 const struct dt_device *dev,
864 struct dt_device_param *param)
866 struct super_block *sb = osd_sb(osd_dt_dev(dev));
869 * XXX should be taken from not-yet-existing fs abstraction layer.
871 param->ddp_max_name_len = LDISKFS_NAME_LEN;
872 param->ddp_max_nlink = LDISKFS_LINK_MAX;
873 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
874 param->ddp_mntopts = 0;
875 if (test_opt(sb, XATTR_USER))
876 param->ddp_mntopts |= MNTOPT_USERXATTR;
877 if (test_opt(sb, POSIX_ACL))
878 param->ddp_mntopts |= MNTOPT_ACL;
880 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
881 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
882 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
885 param->ddp_max_ea_size = sb->s_blocksize;
890 * Helper function to get and fill the buffer with input values.
892 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
896 buf = &osd_oti_get(env)->oti_buf;
903 * Concurrency: shouldn't matter.
905 static int osd_sync(const struct lu_env *env, struct dt_device *d)
907 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
908 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
912 * Start commit for OSD device.
914 * An implementation of dt_commit_async method for OSD device.
915 * Asychronously starts underlayng fs sync and thereby a transaction
918 * \param env environment
921 * \see dt_device_operations
923 static int osd_commit_async(const struct lu_env *env,
926 struct super_block *s = osd_sb(osd_dt_dev(d));
929 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
930 RETURN(s->s_op->sync_fs(s, 0));
934 * Concurrency: shouldn't matter.
937 static int osd_ro(const struct lu_env *env, struct dt_device *d)
939 struct super_block *sb = osd_sb(osd_dt_dev(d));
943 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
945 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
950 * Concurrency: serialization provided by callers.
952 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
953 int mode, unsigned long timeout, __u32 alg,
954 struct lustre_capa_key *keys)
956 struct osd_device *dev = osd_dt_dev(d);
959 dev->od_fl_capa = mode;
960 dev->od_capa_timeout = timeout;
961 dev->od_capa_alg = alg;
962 dev->od_capa_keys = keys;
967 * Concurrency: serialization provided by callers.
969 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
970 struct dt_quota_ctxt *ctxt, void *data)
972 struct obd_device *obd = (void *)ctxt;
973 struct vfsmount *mnt = (struct vfsmount *)data;
976 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
977 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
978 obd->obd_lvfs_ctxt.pwdmnt = mnt;
979 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
980 obd->obd_lvfs_ctxt.fs = get_ds();
986 * Note: we do not count into QUOTA here.
987 * If we mount with --data_journal we may need more.
989 const int osd_dto_credits_noquota[DTO_NR] = {
992 * INDEX_EXTRA_TRANS_BLOCKS(8) +
993 * SINGLEDATA_TRANS_BLOCKS(8)
994 * XXX Note: maybe iam need more, since iam have more level than
997 [DTO_INDEX_INSERT] = 16,
998 [DTO_INDEX_DELETE] = 16,
1002 [DTO_INDEX_UPDATE] = 16,
1004 * Create a object. The same as create object in EXT3.
1005 * DATA_TRANS_BLOCKS(14) +
1006 * INDEX_EXTRA_BLOCKS(8) +
1007 * 3(inode bits, groups, GDT)
1009 [DTO_OBJECT_CREATE] = 25,
1011 * XXX: real credits to be fixed
1013 [DTO_OBJECT_DELETE] = 25,
1015 * Attr set credits (inode)
1017 [DTO_ATTR_SET_BASE] = 1,
1019 * Xattr set. The same as xattr of EXT3.
1020 * DATA_TRANS_BLOCKS(14)
1021 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1022 * are also counted in. Do not know why?
1024 [DTO_XATTR_SET] = 14,
1027 * credits for inode change during write.
1029 [DTO_WRITE_BASE] = 3,
1031 * credits for single block write.
1033 [DTO_WRITE_BLOCK] = 14,
1035 * Attr set credits for chown.
1036 * This is extra credits for setattr, and it is null without quota
1038 [DTO_ATTR_SET_CHOWN]= 0
1041 static const struct dt_device_operations osd_dt_ops = {
1042 .dt_root_get = osd_root_get,
1043 .dt_statfs = osd_statfs,
1044 .dt_trans_create = osd_trans_create,
1045 .dt_trans_start = osd_trans_start,
1046 .dt_trans_stop = osd_trans_stop,
1047 .dt_trans_cb_add = osd_trans_cb_add,
1048 .dt_conf_get = osd_conf_get,
1049 .dt_sync = osd_sync,
1051 .dt_commit_async = osd_commit_async,
1052 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1053 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1056 static void osd_object_read_lock(const struct lu_env *env,
1057 struct dt_object *dt, unsigned role)
1059 struct osd_object *obj = osd_dt_obj(dt);
1060 struct osd_thread_info *oti = osd_oti_get(env);
1062 LINVRNT(osd_invariant(obj));
1064 LASSERT(obj->oo_owner != env);
1065 cfs_down_read_nested(&obj->oo_sem, role);
1067 LASSERT(obj->oo_owner == NULL);
1071 static void osd_object_write_lock(const struct lu_env *env,
1072 struct dt_object *dt, unsigned role)
1074 struct osd_object *obj = osd_dt_obj(dt);
1075 struct osd_thread_info *oti = osd_oti_get(env);
1077 LINVRNT(osd_invariant(obj));
1079 LASSERT(obj->oo_owner != env);
1080 cfs_down_write_nested(&obj->oo_sem, role);
1082 LASSERT(obj->oo_owner == NULL);
1083 obj->oo_owner = env;
1087 static void osd_object_read_unlock(const struct lu_env *env,
1088 struct dt_object *dt)
1090 struct osd_object *obj = osd_dt_obj(dt);
1091 struct osd_thread_info *oti = osd_oti_get(env);
1093 LINVRNT(osd_invariant(obj));
1095 LASSERT(oti->oti_r_locks > 0);
1097 cfs_up_read(&obj->oo_sem);
1100 static void osd_object_write_unlock(const struct lu_env *env,
1101 struct dt_object *dt)
1103 struct osd_object *obj = osd_dt_obj(dt);
1104 struct osd_thread_info *oti = osd_oti_get(env);
1106 LINVRNT(osd_invariant(obj));
1108 LASSERT(obj->oo_owner == env);
1109 LASSERT(oti->oti_w_locks > 0);
1111 obj->oo_owner = NULL;
1112 cfs_up_write(&obj->oo_sem);
1115 static int osd_object_write_locked(const struct lu_env *env,
1116 struct dt_object *dt)
1118 struct osd_object *obj = osd_dt_obj(dt);
1120 LINVRNT(osd_invariant(obj));
1122 return obj->oo_owner == env;
1125 static int capa_is_sane(const struct lu_env *env,
1126 struct osd_device *dev,
1127 struct lustre_capa *capa,
1128 struct lustre_capa_key *keys)
1130 struct osd_thread_info *oti = osd_oti_get(env);
1131 struct lustre_capa *tcapa = &oti->oti_capa;
1132 struct obd_capa *oc;
1136 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1138 if (capa_is_expired(oc)) {
1139 DEBUG_CAPA(D_ERROR, capa, "expired");
1146 if (capa_is_expired_sec(capa)) {
1147 DEBUG_CAPA(D_ERROR, capa, "expired");
1151 cfs_spin_lock(&capa_lock);
1152 for (i = 0; i < 2; i++) {
1153 if (keys[i].lk_keyid == capa->lc_keyid) {
1154 oti->oti_capa_key = keys[i];
1158 cfs_spin_unlock(&capa_lock);
1161 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1165 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1169 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1170 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1174 oc = capa_add(dev->od_capa_hash, capa);
1180 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1181 struct lustre_capa *capa, __u64 opc)
1183 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1184 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1185 struct md_capainfo *ci;
1188 if (!dev->od_fl_capa)
1191 if (capa == BYPASS_CAPA)
1194 ci = md_capainfo(env);
1198 if (ci->mc_auth == LC_ID_NONE)
1202 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1206 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1207 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1212 if (!capa_opc_supported(capa, opc)) {
1213 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1217 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1218 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1225 static struct timespec *osd_inode_time(const struct lu_env *env,
1226 struct inode *inode, __u64 seconds)
1228 struct osd_thread_info *oti = osd_oti_get(env);
1229 struct timespec *t = &oti->oti_time;
1231 t->tv_sec = seconds;
1233 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1238 static void osd_inode_getattr(const struct lu_env *env,
1239 struct inode *inode, struct lu_attr *attr)
1241 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1242 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1243 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1245 attr->la_atime = LTIME_S(inode->i_atime);
1246 attr->la_mtime = LTIME_S(inode->i_mtime);
1247 attr->la_ctime = LTIME_S(inode->i_ctime);
1248 attr->la_mode = inode->i_mode;
1249 attr->la_size = i_size_read(inode);
1250 attr->la_blocks = inode->i_blocks;
1251 attr->la_uid = inode->i_uid;
1252 attr->la_gid = inode->i_gid;
1253 attr->la_flags = LDISKFS_I(inode)->i_flags;
1254 attr->la_nlink = inode->i_nlink;
1255 attr->la_rdev = inode->i_rdev;
1256 attr->la_blksize = ll_inode_blksize(inode);
1257 attr->la_blkbits = inode->i_blkbits;
1260 static int osd_attr_get(const struct lu_env *env,
1261 struct dt_object *dt,
1262 struct lu_attr *attr,
1263 struct lustre_capa *capa)
1265 struct osd_object *obj = osd_dt_obj(dt);
1267 LASSERT(dt_object_exists(dt));
1268 LINVRNT(osd_invariant(obj));
1270 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1273 cfs_spin_lock(&obj->oo_guard);
1274 osd_inode_getattr(env, obj->oo_inode, attr);
1275 cfs_spin_unlock(&obj->oo_guard);
1279 static int osd_declare_attr_set(const struct lu_env *env,
1280 struct dt_object *dt,
1281 const struct lu_attr *attr,
1282 struct thandle *handle)
1284 struct osd_thandle *oh;
1285 struct osd_object *obj;
1287 LASSERT(dt != NULL);
1288 LASSERT(handle != NULL);
1290 obj = osd_dt_obj(dt);
1291 LASSERT(osd_invariant(obj));
1293 oh = container_of0(handle, struct osd_thandle, ot_super);
1294 LASSERT(oh->ot_handle == NULL);
1296 OSD_DECLARE_OP(oh, attr_set);
1297 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1299 if (attr && attr->la_valid & LA_UID) {
1301 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1303 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1305 if (attr && attr->la_valid & LA_GID) {
1307 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1309 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1315 static int osd_inode_setattr(const struct lu_env *env,
1316 struct inode *inode, const struct lu_attr *attr)
1320 bits = attr->la_valid;
1322 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1324 if (bits & LA_ATIME)
1325 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1326 if (bits & LA_CTIME)
1327 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1328 if (bits & LA_MTIME)
1329 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1330 if (bits & LA_SIZE) {
1331 LDISKFS_I(inode)->i_disksize = attr->la_size;
1332 i_size_write(inode, attr->la_size);
1336 /* OSD should not change "i_blocks" which is used by quota.
1337 * "i_blocks" should be changed by ldiskfs only. */
1338 if (bits & LA_BLOCKS)
1339 inode->i_blocks = attr->la_blocks;
1342 inode->i_mode = (inode->i_mode & S_IFMT) |
1343 (attr->la_mode & ~S_IFMT);
1345 inode->i_uid = attr->la_uid;
1347 inode->i_gid = attr->la_gid;
1348 if (bits & LA_NLINK)
1349 inode->i_nlink = attr->la_nlink;
1351 inode->i_rdev = attr->la_rdev;
1353 if (bits & LA_FLAGS) {
1354 /* always keep S_NOCMTIME */
1355 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1361 static int osd_attr_set(const struct lu_env *env,
1362 struct dt_object *dt,
1363 const struct lu_attr *attr,
1364 struct thandle *handle,
1365 struct lustre_capa *capa)
1367 struct osd_object *obj = osd_dt_obj(dt);
1368 struct inode *inode;
1371 LASSERT(handle != NULL);
1372 LASSERT(dt_object_exists(dt));
1373 LASSERT(osd_invariant(obj));
1375 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1378 OSD_EXEC_OP(handle, attr_set);
1380 inode = obj->oo_inode;
1381 #ifdef HAVE_QUOTA_SUPPORT
1382 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1383 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1384 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1389 if (attr->la_valid & LA_UID)
1390 iattr.ia_valid |= ATTR_UID;
1391 if (attr->la_valid & LA_GID)
1392 iattr.ia_valid |= ATTR_GID;
1393 iattr.ia_uid = attr->la_uid;
1394 iattr.ia_gid = attr->la_gid;
1395 osd_push_ctxt(env, save);
1396 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1403 cfs_spin_lock(&obj->oo_guard);
1404 rc = osd_inode_setattr(env, inode, attr);
1405 cfs_spin_unlock(&obj->oo_guard);
1408 inode->i_sb->s_op->dirty_inode(inode);
1415 * XXX temporary solution.
1417 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1418 struct lu_attr *attr, struct thandle *th)
1423 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1424 struct lu_attr *attr, struct thandle *th)
1426 osd_object_init0(obj);
1427 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1428 unlock_new_inode(obj->oo_inode);
1432 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1433 struct osd_object *obj,
1434 const char *name, const int namelen)
1436 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1439 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1441 struct dt_allocation_hint *hint,
1445 struct osd_device *osd = osd_obj2dev(obj);
1446 struct osd_thandle *oth;
1447 struct dt_object *parent = NULL;
1448 struct inode *inode;
1449 #ifdef HAVE_QUOTA_SUPPORT
1450 struct osd_ctxt *save = &info->oti_ctxt;
1453 LINVRNT(osd_invariant(obj));
1454 LASSERT(obj->oo_inode == NULL);
1455 LASSERT(obj->oo_hl_head == NULL);
1457 if (S_ISDIR(mode) && ldiskfs_pdo) {
1458 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1459 if (obj->oo_hl_head == NULL)
1463 oth = container_of(th, struct osd_thandle, ot_super);
1464 LASSERT(oth->ot_handle->h_transaction != NULL);
1466 if (hint && hint->dah_parent)
1467 parent = hint->dah_parent;
1469 #ifdef HAVE_QUOTA_SUPPORT
1470 osd_push_ctxt(info->oti_env, save);
1472 inode = ldiskfs_create_inode(oth->ot_handle,
1473 parent ? osd_dt_obj(parent)->oo_inode :
1474 osd_sb(osd)->s_root->d_inode,
1476 #ifdef HAVE_QUOTA_SUPPORT
1479 if (!IS_ERR(inode)) {
1480 /* Do not update file c/mtime in ldiskfs.
1481 * NB: don't need any lock because no contention at this
1483 inode->i_flags |= S_NOCMTIME;
1484 obj->oo_inode = inode;
1487 if (obj->oo_hl_head != NULL) {
1488 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1489 obj->oo_hl_head = NULL;
1491 result = PTR_ERR(inode);
1493 LINVRNT(osd_invariant(obj));
1501 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1502 struct lu_attr *attr,
1503 struct dt_allocation_hint *hint,
1504 struct dt_object_format *dof,
1508 struct osd_thandle *oth;
1509 struct osd_device *osd = osd_obj2dev(obj);
1510 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1512 LASSERT(S_ISDIR(attr->la_mode));
1514 oth = container_of(th, struct osd_thandle, ot_super);
1515 LASSERT(oth->ot_handle->h_transaction != NULL);
1516 result = osd_mkfile(info, obj, mode, hint, th);
1517 if (result == 0 && osd->od_iop_mode == 0) {
1518 LASSERT(obj->oo_inode != NULL);
1520 * XXX uh-oh... call low-level iam function directly.
1523 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1524 sizeof (struct osd_fid_pack),
1530 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1531 struct lu_attr *attr,
1532 struct dt_allocation_hint *hint,
1533 struct dt_object_format *dof,
1537 struct osd_thandle *oth;
1538 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1540 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1542 LASSERT(S_ISREG(attr->la_mode));
1544 oth = container_of(th, struct osd_thandle, ot_super);
1545 LASSERT(oth->ot_handle->h_transaction != NULL);
1547 result = osd_mkfile(info, obj, mode, hint, th);
1549 LASSERT(obj->oo_inode != NULL);
1550 if (feat->dif_flags & DT_IND_VARKEY)
1551 result = iam_lvar_create(obj->oo_inode,
1552 feat->dif_keysize_max,
1554 feat->dif_recsize_max,
1557 result = iam_lfix_create(obj->oo_inode,
1558 feat->dif_keysize_max,
1560 feat->dif_recsize_max,
1567 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1568 struct lu_attr *attr,
1569 struct dt_allocation_hint *hint,
1570 struct dt_object_format *dof,
1573 LASSERT(S_ISREG(attr->la_mode));
1574 return osd_mkfile(info, obj, (attr->la_mode &
1575 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1578 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1579 struct lu_attr *attr,
1580 struct dt_allocation_hint *hint,
1581 struct dt_object_format *dof,
1584 LASSERT(S_ISLNK(attr->la_mode));
1585 return osd_mkfile(info, obj, (attr->la_mode &
1586 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1589 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1590 struct lu_attr *attr,
1591 struct dt_allocation_hint *hint,
1592 struct dt_object_format *dof,
1595 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1598 LINVRNT(osd_invariant(obj));
1599 LASSERT(obj->oo_inode == NULL);
1600 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1601 S_ISFIFO(mode) || S_ISSOCK(mode));
1603 result = osd_mkfile(info, obj, mode, hint, th);
1605 LASSERT(obj->oo_inode != NULL);
1606 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1608 LINVRNT(osd_invariant(obj));
1612 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1614 struct dt_allocation_hint *hint,
1615 struct dt_object_format *dof,
1618 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1620 osd_obj_type_f result;
1636 result = osd_mk_index;
1647 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1648 struct dt_object *parent, cfs_umode_t child_mode)
1652 memset(ah, 0, sizeof(*ah));
1653 ah->dah_parent = parent;
1654 ah->dah_mode = child_mode;
1658 * Helper function for osd_object_create()
1660 * \retval 0, on success
1662 static int __osd_object_create(struct osd_thread_info *info,
1663 struct osd_object *obj, struct lu_attr *attr,
1664 struct dt_allocation_hint *hint,
1665 struct dt_object_format *dof,
1671 result = osd_create_pre(info, obj, attr, th);
1673 result = osd_create_type_f(dof->dof_type)(info, obj,
1674 attr, hint, dof, th);
1676 result = osd_create_post(info, obj, attr, th);
1682 * Helper function for osd_object_create()
1684 * \retval 0, on success
1686 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1687 const struct lu_fid *fid, struct thandle *th)
1689 struct osd_thread_info *info = osd_oti_get(env);
1690 struct osd_inode_id *id = &info->oti_id;
1691 struct osd_device *osd = osd_obj2dev(obj);
1692 struct md_ucred *uc = md_ucred(env);
1694 LASSERT(obj->oo_inode != NULL);
1695 LASSERT(uc != NULL);
1697 id->oii_ino = obj->oo_inode->i_ino;
1698 id->oii_gen = obj->oo_inode->i_generation;
1700 return osd_oi_insert(info, osd, fid, id, th,
1701 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1704 static int osd_declare_object_create(const struct lu_env *env,
1705 struct dt_object *dt,
1706 struct lu_attr *attr,
1707 struct dt_allocation_hint *hint,
1708 struct dt_object_format *dof,
1709 struct thandle *handle)
1711 struct osd_thandle *oh;
1713 LASSERT(handle != NULL);
1715 oh = container_of0(handle, struct osd_thandle, ot_super);
1716 LASSERT(oh->ot_handle == NULL);
1718 OSD_DECLARE_OP(oh, create);
1719 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1720 /* XXX: So far, only normal fid needs be inserted into the oi,
1721 * things could be changed later. Revise following code then. */
1722 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1723 OSD_DECLARE_OP(oh, insert);
1724 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1726 /* If this is directory, then we expect . and .. to be inserted as
1727 * well. The one directory block always needs to be created for the
1728 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1729 * block), there is no danger of needing a tree for the first block.
1731 if (attr && S_ISDIR(attr->la_mode)) {
1732 OSD_DECLARE_OP(oh, insert);
1733 OSD_DECLARE_OP(oh, insert);
1734 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1738 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1739 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1744 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1745 struct lu_attr *attr,
1746 struct dt_allocation_hint *hint,
1747 struct dt_object_format *dof,
1750 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1751 struct osd_object *obj = osd_dt_obj(dt);
1752 struct osd_thread_info *info = osd_oti_get(env);
1757 LINVRNT(osd_invariant(obj));
1758 LASSERT(!dt_object_exists(dt));
1759 LASSERT(osd_write_locked(env, obj));
1760 LASSERT(th != NULL);
1762 OSD_EXEC_OP(th, create);
1764 result = __osd_object_create(info, obj, attr, hint, dof, th);
1766 result = __osd_oi_insert(env, obj, fid, th);
1768 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1769 LASSERT(osd_invariant(obj));
1774 * Called to destroy on-disk representation of the object
1776 * Concurrency: must be locked
1778 static int osd_declare_object_destroy(const struct lu_env *env,
1779 struct dt_object *dt,
1782 struct osd_object *obj = osd_dt_obj(dt);
1783 struct inode *inode = obj->oo_inode;
1784 struct osd_thandle *oh;
1788 oh = container_of0(th, struct osd_thandle, ot_super);
1789 LASSERT(oh->ot_handle == NULL);
1792 OSD_DECLARE_OP(oh, destroy);
1793 OSD_DECLARE_OP(oh, delete);
1794 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
1795 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
1797 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
1798 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
1803 static int osd_object_destroy(const struct lu_env *env,
1804 struct dt_object *dt,
1807 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1808 struct osd_object *obj = osd_dt_obj(dt);
1809 struct inode *inode = obj->oo_inode;
1810 struct osd_device *osd = osd_obj2dev(obj);
1811 struct osd_thandle *oh;
1815 oh = container_of0(th, struct osd_thandle, ot_super);
1816 LASSERT(oh->ot_handle);
1818 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1820 if (S_ISDIR(inode->i_mode)) {
1821 LASSERT(osd_inode_unlinked(inode) ||
1822 inode->i_nlink == 1);
1823 cfs_spin_lock(&obj->oo_guard);
1825 cfs_spin_unlock(&obj->oo_guard);
1826 inode->i_sb->s_op->dirty_inode(inode);
1828 LASSERT(osd_inode_unlinked(inode));
1831 OSD_EXEC_OP(th, destroy);
1833 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
1835 /* XXX: add to ext3 orphan list */
1836 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
1838 /* not needed in the cache anymore */
1839 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1845 * Helper function for osd_xattr_set()
1847 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1848 const struct lu_buf *buf, const char *name, int fl)
1850 struct osd_object *obj = osd_dt_obj(dt);
1851 struct inode *inode = obj->oo_inode;
1852 struct osd_thread_info *info = osd_oti_get(env);
1853 struct dentry *dentry = &info->oti_child_dentry;
1857 LASSERT(dt_object_exists(dt));
1858 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1859 LASSERT(osd_write_locked(env, obj));
1861 if (fl & LU_XATTR_REPLACE)
1862 fs_flags |= XATTR_REPLACE;
1864 if (fl & LU_XATTR_CREATE)
1865 fs_flags |= XATTR_CREATE;
1867 dentry->d_inode = inode;
1868 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1869 buf->lb_len, fs_flags);
1874 * Put the fid into lustre_mdt_attrs, and then place the structure
1875 * inode's ea. This fid should not be altered during the life time
1878 * \retval +ve, on success
1879 * \retval -ve, on error
1881 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1883 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1884 const struct lu_fid *fid)
1886 struct osd_thread_info *info = osd_oti_get(env);
1887 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1889 lustre_lma_init(mdt_attrs, fid);
1890 lustre_lma_swab(mdt_attrs);
1891 return __osd_xattr_set(env, dt,
1892 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1893 XATTR_NAME_LMA, LU_XATTR_CREATE);
1898 * Helper function to form igif
1900 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1903 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1907 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1908 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1909 * To have compatilibility with 1.8 ldiskfs driver we need to have
1910 * magic number at start of fid data.
1911 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1914 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1915 const struct dt_rec *fid)
1917 param->edp_magic = LDISKFS_LUFID_MAGIC;
1918 param->edp_len = sizeof(struct lu_fid) + 1;
1920 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1921 (struct lu_fid *)fid);
1925 * Try to read the fid from inode ea into dt_rec, if return value
1926 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1928 * \param fid object fid.
1930 * \retval 0 on success
1932 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1933 __u32 ino, struct lu_fid *fid)
1935 struct osd_thread_info *info = osd_oti_get(env);
1936 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1937 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1938 struct dentry *dentry = &info->oti_child_dentry;
1939 struct osd_inode_id *id = &info->oti_id;
1940 struct osd_device *dev;
1941 struct inode *inode;
1945 dev = osd_dev(ldev);
1948 id->oii_gen = OSD_OII_NOGEN;
1950 inode = osd_iget(info, dev, id);
1951 if (IS_ERR(inode)) {
1952 rc = PTR_ERR(inode);
1955 dentry->d_inode = inode;
1957 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1958 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1961 /* Check LMA compatibility */
1963 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
1964 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1965 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
1966 ~LMA_INCOMPAT_SUPP);
1971 lustre_lma_swab(mdt_attrs);
1972 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
1974 } else if (rc == -ENODATA) {
1975 osd_igif_get(env, inode, fid);
1984 * OSD layer object create function for interoperability mode (b11826).
1985 * This is mostly similar to osd_object_create(). Only difference being, fid is
1986 * inserted into inode ea here.
1988 * \retval 0, on success
1989 * \retval -ve, on error
1991 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1992 struct lu_attr *attr,
1993 struct dt_allocation_hint *hint,
1994 struct dt_object_format *dof,
1997 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1998 struct osd_object *obj = osd_dt_obj(dt);
1999 struct osd_thread_info *info = osd_oti_get(env);
2004 LASSERT(osd_invariant(obj));
2005 LASSERT(!dt_object_exists(dt));
2006 LASSERT(osd_write_locked(env, obj));
2007 LASSERT(th != NULL);
2009 OSD_EXEC_OP(th, create);
2011 result = __osd_object_create(info, obj, attr, hint, dof, th);
2012 /* objects under osd root shld have igif fid, so dont add fid EA */
2013 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2014 result = osd_ea_fid_set(env, dt, fid);
2017 result = __osd_oi_insert(env, obj, fid, th);
2019 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2020 LINVRNT(osd_invariant(obj));
2024 static int osd_declare_object_ref_add(const struct lu_env *env,
2025 struct dt_object *dt,
2026 struct thandle *handle)
2028 struct osd_thandle *oh;
2030 /* it's possible that object doesn't exist yet */
2031 LASSERT(handle != NULL);
2033 oh = container_of0(handle, struct osd_thandle, ot_super);
2034 LASSERT(oh->ot_handle == NULL);
2036 OSD_DECLARE_OP(oh, ref_add);
2037 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2043 * Concurrency: @dt is write locked.
2045 static int osd_object_ref_add(const struct lu_env *env,
2046 struct dt_object *dt, struct thandle *th)
2048 struct osd_object *obj = osd_dt_obj(dt);
2049 struct inode *inode = obj->oo_inode;
2051 LINVRNT(osd_invariant(obj));
2052 LASSERT(dt_object_exists(dt));
2053 LASSERT(osd_write_locked(env, obj));
2054 LASSERT(th != NULL);
2056 OSD_EXEC_OP(th, ref_add);
2059 * DIR_NLINK feature is set for compatibility reasons if:
2060 * 1) nlinks > LDISKFS_LINK_MAX, or
2061 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2063 * It is easier to always set this flag (rather than check and set),
2064 * since it has less overhead, and the superblock will be dirtied
2065 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2066 * do not actually care whether this flag is set or not.
2068 cfs_spin_lock(&obj->oo_guard);
2070 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2071 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2072 inode->i_nlink == 2)
2075 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2076 cfs_spin_unlock(&obj->oo_guard);
2077 inode->i_sb->s_op->dirty_inode(inode);
2078 LINVRNT(osd_invariant(obj));
2083 static int osd_declare_object_ref_del(const struct lu_env *env,
2084 struct dt_object *dt,
2085 struct thandle *handle)
2087 struct osd_thandle *oh;
2089 LASSERT(dt_object_exists(dt));
2090 LASSERT(handle != NULL);
2092 oh = container_of0(handle, struct osd_thandle, ot_super);
2093 LASSERT(oh->ot_handle == NULL);
2095 OSD_DECLARE_OP(oh, ref_del);
2096 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2102 * Concurrency: @dt is write locked.
2104 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2107 struct osd_object *obj = osd_dt_obj(dt);
2108 struct inode *inode = obj->oo_inode;
2110 LINVRNT(osd_invariant(obj));
2111 LASSERT(dt_object_exists(dt));
2112 LASSERT(osd_write_locked(env, obj));
2113 LASSERT(th != NULL);
2115 OSD_EXEC_OP(th, ref_del);
2117 cfs_spin_lock(&obj->oo_guard);
2118 LASSERT(inode->i_nlink > 0);
2120 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2121 * then the nlink count is 1. Don't let it be set to 0 or the directory
2122 * inode will be deleted incorrectly. */
2123 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2125 cfs_spin_unlock(&obj->oo_guard);
2126 inode->i_sb->s_op->dirty_inode(inode);
2127 LINVRNT(osd_invariant(obj));
2133 * Get the 64-bit version for an inode.
2135 static int osd_object_version_get(const struct lu_env *env,
2136 struct dt_object *dt, dt_obj_version_t *ver)
2138 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2140 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2141 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2142 *ver = LDISKFS_I(inode)->i_fs_version;
2147 * Concurrency: @dt is read locked.
2149 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2150 struct lu_buf *buf, const char *name,
2151 struct lustre_capa *capa)
2153 struct osd_object *obj = osd_dt_obj(dt);
2154 struct inode *inode = obj->oo_inode;
2155 struct osd_thread_info *info = osd_oti_get(env);
2156 struct dentry *dentry = &info->oti_obj_dentry;
2158 /* version get is not real XATTR but uses xattr API */
2159 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2160 /* for version we are just using xattr API but change inode
2162 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2163 osd_object_version_get(env, dt, buf->lb_buf);
2164 return sizeof(dt_obj_version_t);
2167 LASSERT(dt_object_exists(dt));
2168 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2169 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2171 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2174 dentry->d_inode = inode;
2175 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2179 static int osd_declare_xattr_set(const struct lu_env *env,
2180 struct dt_object *dt,
2181 const struct lu_buf *buf, const char *name,
2182 int fl, struct thandle *handle)
2184 struct osd_thandle *oh;
2186 LASSERT(handle != NULL);
2188 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2189 /* no credits for version */
2193 oh = container_of0(handle, struct osd_thandle, ot_super);
2194 LASSERT(oh->ot_handle == NULL);
2196 OSD_DECLARE_OP(oh, xattr_set);
2197 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2203 * Set the 64-bit version for object
2205 static void osd_object_version_set(const struct lu_env *env,
2206 struct dt_object *dt,
2207 dt_obj_version_t *new_version)
2209 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2211 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2212 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2214 LDISKFS_I(inode)->i_fs_version = *new_version;
2215 /** Version is set after all inode operations are finished,
2216 * so we should mark it dirty here */
2217 inode->i_sb->s_op->dirty_inode(inode);
2221 * Concurrency: @dt is write locked.
2223 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2224 const struct lu_buf *buf, const char *name, int fl,
2225 struct thandle *handle, struct lustre_capa *capa)
2227 LASSERT(handle != NULL);
2229 /* version set is not real XATTR */
2230 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2231 /* for version we are just using xattr API but change inode
2233 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2234 osd_object_version_set(env, dt, buf->lb_buf);
2235 return sizeof(dt_obj_version_t);
2238 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2241 OSD_EXEC_OP(handle, xattr_set);
2242 return __osd_xattr_set(env, dt, buf, name, fl);
2246 * Concurrency: @dt is read locked.
2248 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2249 struct lu_buf *buf, struct lustre_capa *capa)
2251 struct osd_object *obj = osd_dt_obj(dt);
2252 struct inode *inode = obj->oo_inode;
2253 struct osd_thread_info *info = osd_oti_get(env);
2254 struct dentry *dentry = &info->oti_obj_dentry;
2256 LASSERT(dt_object_exists(dt));
2257 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2258 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2260 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2263 dentry->d_inode = inode;
2264 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2267 static int osd_declare_xattr_del(const struct lu_env *env,
2268 struct dt_object *dt, const char *name,
2269 struct thandle *handle)
2271 struct osd_thandle *oh;
2273 LASSERT(dt_object_exists(dt));
2274 LASSERT(handle != NULL);
2276 oh = container_of0(handle, struct osd_thandle, ot_super);
2277 LASSERT(oh->ot_handle == NULL);
2279 OSD_DECLARE_OP(oh, xattr_set);
2280 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2286 * Concurrency: @dt is write locked.
2288 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2289 const char *name, struct thandle *handle,
2290 struct lustre_capa *capa)
2292 struct osd_object *obj = osd_dt_obj(dt);
2293 struct inode *inode = obj->oo_inode;
2294 struct osd_thread_info *info = osd_oti_get(env);
2295 struct dentry *dentry = &info->oti_obj_dentry;
2298 LASSERT(dt_object_exists(dt));
2299 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2300 LASSERT(osd_write_locked(env, obj));
2301 LASSERT(handle != NULL);
2303 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2306 OSD_EXEC_OP(handle, xattr_set);
2308 dentry->d_inode = inode;
2309 rc = inode->i_op->removexattr(dentry, name);
2313 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2314 struct dt_object *dt,
2315 struct lustre_capa *old,
2318 struct osd_thread_info *info = osd_oti_get(env);
2319 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2320 struct osd_object *obj = osd_dt_obj(dt);
2321 struct osd_device *dev = osd_obj2dev(obj);
2322 struct lustre_capa_key *key = &info->oti_capa_key;
2323 struct lustre_capa *capa = &info->oti_capa;
2324 struct obd_capa *oc;
2325 struct md_capainfo *ci;
2329 if (!dev->od_fl_capa)
2330 RETURN(ERR_PTR(-ENOENT));
2332 LASSERT(dt_object_exists(dt));
2333 LINVRNT(osd_invariant(obj));
2335 /* renewal sanity check */
2336 if (old && osd_object_auth(env, dt, old, opc))
2337 RETURN(ERR_PTR(-EACCES));
2339 ci = md_capainfo(env);
2341 RETURN(ERR_PTR(-ENOENT));
2343 switch (ci->mc_auth) {
2347 capa->lc_uid = obj->oo_inode->i_uid;
2348 capa->lc_gid = obj->oo_inode->i_gid;
2349 capa->lc_flags = LC_ID_PLAIN;
2351 case LC_ID_CONVERT: {
2354 s[0] = obj->oo_inode->i_uid;
2355 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2356 s[2] = obj->oo_inode->i_gid;
2357 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2358 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2360 RETURN(ERR_PTR(rc));
2362 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2363 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2364 capa->lc_flags = LC_ID_CONVERT;
2368 RETURN(ERR_PTR(-EINVAL));
2371 capa->lc_fid = *fid;
2373 capa->lc_flags |= dev->od_capa_alg << 24;
2374 capa->lc_timeout = dev->od_capa_timeout;
2375 capa->lc_expiry = 0;
2377 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2379 LASSERT(!capa_is_expired(oc));
2383 cfs_spin_lock(&capa_lock);
2384 *key = dev->od_capa_keys[1];
2385 cfs_spin_unlock(&capa_lock);
2387 capa->lc_keyid = key->lk_keyid;
2388 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2390 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2392 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2393 RETURN(ERR_PTR(rc));
2396 oc = capa_add(dev->od_capa_hash, capa);
2400 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2402 struct osd_object *obj = osd_dt_obj(dt);
2403 struct inode *inode = obj->oo_inode;
2404 struct osd_thread_info *info = osd_oti_get(env);
2405 struct dentry *dentry = &info->oti_obj_dentry;
2406 struct file *file = &info->oti_file;
2411 dentry->d_inode = inode;
2412 file->f_dentry = dentry;
2413 file->f_mapping = inode->i_mapping;
2414 file->f_op = inode->i_fop;
2415 LOCK_INODE_MUTEX(inode);
2416 rc = file->f_op->fsync(file, dentry, 0);
2417 UNLOCK_INODE_MUTEX(inode);
2421 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2424 struct osd_object *obj = osd_dt_obj(dt);
2427 *data = (void *)obj->oo_inode;
2435 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2436 const struct dt_index_features *feat)
2438 struct iam_descr *descr;
2440 if (osd_object_is_root(o))
2441 return feat == &dt_directory_features;
2443 LASSERT(o->oo_dir != NULL);
2445 descr = o->oo_dir->od_container.ic_descr;
2446 if (feat == &dt_directory_features) {
2447 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2453 feat->dif_keysize_min <= descr->id_key_size &&
2454 descr->id_key_size <= feat->dif_keysize_max &&
2455 feat->dif_recsize_min <= descr->id_rec_size &&
2456 descr->id_rec_size <= feat->dif_recsize_max &&
2457 !(feat->dif_flags & (DT_IND_VARKEY |
2458 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2459 ergo(feat->dif_flags & DT_IND_UPDATE,
2460 1 /* XXX check that object (and file system) is
2465 static int osd_iam_container_init(const struct lu_env *env,
2466 struct osd_object *obj,
2467 struct osd_directory *dir)
2469 struct iam_container *bag = &dir->od_container;
2472 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2476 result = iam_container_setup(bag);
2480 if (osd_obj2dev(obj)->od_iop_mode) {
2481 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2483 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2489 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2491 iam_container_fini(bag);
2498 * Concurrency: no external locking is necessary.
2500 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2501 const struct dt_index_features *feat)
2505 struct osd_object *obj = osd_dt_obj(dt);
2506 struct osd_device *osd = osd_obj2dev(obj);
2508 LINVRNT(osd_invariant(obj));
2509 LASSERT(dt_object_exists(dt));
2511 if (osd_object_is_root(obj)) {
2512 dt->do_index_ops = &osd_index_ea_ops;
2514 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2515 dt->do_index_ops = &osd_index_ea_ops;
2516 if (S_ISDIR(obj->oo_inode->i_mode))
2521 } else if (!osd_has_index(obj)) {
2522 struct osd_directory *dir;
2527 cfs_spin_lock(&obj->oo_guard);
2528 if (obj->oo_dir == NULL)
2532 * Concurrent thread allocated container data.
2535 cfs_spin_unlock(&obj->oo_guard);
2537 * Now, that we have container data, serialize its
2540 cfs_down_write(&obj->oo_ext_idx_sem);
2542 * recheck under lock.
2544 if (!osd_has_index(obj))
2545 result = osd_iam_container_init(env, obj, dir);
2548 cfs_up_write(&obj->oo_ext_idx_sem);
2556 if (result == 0 && ea_dir == 0) {
2557 if (!osd_iam_index_probe(env, obj, feat))
2560 LINVRNT(osd_invariant(obj));
2565 static const struct dt_object_operations osd_obj_ops = {
2566 .do_read_lock = osd_object_read_lock,
2567 .do_write_lock = osd_object_write_lock,
2568 .do_read_unlock = osd_object_read_unlock,
2569 .do_write_unlock = osd_object_write_unlock,
2570 .do_write_locked = osd_object_write_locked,
2571 .do_attr_get = osd_attr_get,
2572 .do_declare_attr_set = osd_declare_attr_set,
2573 .do_attr_set = osd_attr_set,
2574 .do_ah_init = osd_ah_init,
2575 .do_declare_create = osd_declare_object_create,
2576 .do_create = osd_object_create,
2577 .do_declare_destroy = osd_declare_object_destroy,
2578 .do_destroy = osd_object_destroy,
2579 .do_index_try = osd_index_try,
2580 .do_declare_ref_add = osd_declare_object_ref_add,
2581 .do_ref_add = osd_object_ref_add,
2582 .do_declare_ref_del = osd_declare_object_ref_del,
2583 .do_ref_del = osd_object_ref_del,
2584 .do_xattr_get = osd_xattr_get,
2585 .do_declare_xattr_set = osd_declare_xattr_set,
2586 .do_xattr_set = osd_xattr_set,
2587 .do_declare_xattr_del = osd_declare_xattr_del,
2588 .do_xattr_del = osd_xattr_del,
2589 .do_xattr_list = osd_xattr_list,
2590 .do_capa_get = osd_capa_get,
2591 .do_object_sync = osd_object_sync,
2592 .do_data_get = osd_data_get,
2596 * dt_object_operations for interoperability mode
2597 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2599 static const struct dt_object_operations osd_obj_ea_ops = {
2600 .do_read_lock = osd_object_read_lock,
2601 .do_write_lock = osd_object_write_lock,
2602 .do_read_unlock = osd_object_read_unlock,
2603 .do_write_unlock = osd_object_write_unlock,
2604 .do_write_locked = osd_object_write_locked,
2605 .do_attr_get = osd_attr_get,
2606 .do_declare_attr_set = osd_declare_attr_set,
2607 .do_attr_set = osd_attr_set,
2608 .do_ah_init = osd_ah_init,
2609 .do_declare_create = osd_declare_object_create,
2610 .do_create = osd_object_ea_create,
2611 .do_declare_destroy = osd_declare_object_destroy,
2612 .do_destroy = osd_object_destroy,
2613 .do_index_try = osd_index_try,
2614 .do_declare_ref_add = osd_declare_object_ref_add,
2615 .do_ref_add = osd_object_ref_add,
2616 .do_declare_ref_del = osd_declare_object_ref_del,
2617 .do_ref_del = osd_object_ref_del,
2618 .do_xattr_get = osd_xattr_get,
2619 .do_declare_xattr_set = osd_declare_xattr_set,
2620 .do_xattr_set = osd_xattr_set,
2621 .do_declare_xattr_del = osd_declare_xattr_del,
2622 .do_xattr_del = osd_xattr_del,
2623 .do_xattr_list = osd_xattr_list,
2624 .do_capa_get = osd_capa_get,
2625 .do_object_sync = osd_object_sync,
2626 .do_data_get = osd_data_get,
2629 static int osd_index_declare_iam_delete(const struct lu_env *env,
2630 struct dt_object *dt,
2631 const struct dt_key *key,
2632 struct thandle *handle)
2634 struct osd_thandle *oh;
2636 oh = container_of0(handle, struct osd_thandle, ot_super);
2637 LASSERT(oh->ot_handle == NULL);
2639 OSD_DECLARE_OP(oh, delete);
2640 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2646 * delete a (key, value) pair from index \a dt specified by \a key
2648 * \param dt osd index object
2649 * \param key key for index
2650 * \param rec record reference
2651 * \param handle transaction handler
2654 * \retval -ve failure
2657 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2658 const struct dt_key *key,
2659 struct thandle *handle,
2660 struct lustre_capa *capa)
2662 struct osd_object *obj = osd_dt_obj(dt);
2663 struct osd_thandle *oh;
2664 struct iam_path_descr *ipd;
2665 struct iam_container *bag = &obj->oo_dir->od_container;
2670 LINVRNT(osd_invariant(obj));
2671 LASSERT(dt_object_exists(dt));
2672 LASSERT(bag->ic_object == obj->oo_inode);
2673 LASSERT(handle != NULL);
2675 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2678 OSD_EXEC_OP(handle, delete);
2680 ipd = osd_idx_ipd_get(env, bag);
2681 if (unlikely(ipd == NULL))
2684 oh = container_of0(handle, struct osd_thandle, ot_super);
2685 LASSERT(oh->ot_handle != NULL);
2686 LASSERT(oh->ot_handle->h_transaction != NULL);
2688 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2689 osd_ipd_put(env, bag, ipd);
2690 LINVRNT(osd_invariant(obj));
2694 static int osd_index_declare_ea_delete(const struct lu_env *env,
2695 struct dt_object *dt,
2696 const struct dt_key *key,
2697 struct thandle *handle)
2699 struct osd_thandle *oh;
2701 LASSERT(dt_object_exists(dt));
2702 LASSERT(handle != NULL);
2704 oh = container_of0(handle, struct osd_thandle, ot_super);
2705 LASSERT(oh->ot_handle == NULL);
2707 OSD_DECLARE_OP(oh, delete);
2708 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2710 LASSERT(osd_dt_obj(dt)->oo_inode);
2711 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2712 osd_dt_obj(dt)->oo_inode);
2713 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2714 osd_dt_obj(dt)->oo_inode);
2719 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2722 struct osd_fid_pack *rec;
2725 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2726 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2727 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2733 * Index delete function for interoperability mode (b11826).
2734 * It will remove the directory entry added by osd_index_ea_insert().
2735 * This entry is needed to maintain name->fid mapping.
2737 * \param key, key i.e. file entry to be deleted
2739 * \retval 0, on success
2740 * \retval -ve, on error
2742 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2743 const struct dt_key *key,
2744 struct thandle *handle,
2745 struct lustre_capa *capa)
2747 struct osd_object *obj = osd_dt_obj(dt);
2748 struct inode *dir = obj->oo_inode;
2749 struct dentry *dentry;
2750 struct osd_thandle *oh;
2751 struct ldiskfs_dir_entry_2 *de;
2752 struct buffer_head *bh;
2753 struct htree_lock *hlock = NULL;
2758 LINVRNT(osd_invariant(obj));
2759 LASSERT(dt_object_exists(dt));
2760 LASSERT(handle != NULL);
2762 OSD_EXEC_OP(handle, delete);
2764 oh = container_of(handle, struct osd_thandle, ot_super);
2765 LASSERT(oh->ot_handle != NULL);
2766 LASSERT(oh->ot_handle->h_transaction != NULL);
2768 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2771 dentry = osd_child_dentry_get(env, obj,
2772 (char *)key, strlen((char *)key));
2774 if (obj->oo_hl_head != NULL) {
2775 hlock = osd_oti_get(env)->oti_hlock;
2776 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2777 dir, LDISKFS_HLOCK_DEL);
2779 cfs_down_write(&obj->oo_ext_idx_sem);
2782 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2784 rc = ldiskfs_delete_entry(oh->ot_handle,
2791 ldiskfs_htree_unlock(hlock);
2793 cfs_up_write(&obj->oo_ext_idx_sem);
2795 LASSERT(osd_invariant(obj));
2800 * Lookup index for \a key and copy record to \a rec.
2802 * \param dt osd index object
2803 * \param key key for index
2804 * \param rec record reference
2806 * \retval +ve success : exact mach
2807 * \retval 0 return record with key not greater than \a key
2808 * \retval -ve failure
2810 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2811 struct dt_rec *rec, const struct dt_key *key,
2812 struct lustre_capa *capa)
2814 struct osd_object *obj = osd_dt_obj(dt);
2815 struct iam_path_descr *ipd;
2816 struct iam_container *bag = &obj->oo_dir->od_container;
2817 struct osd_thread_info *oti = osd_oti_get(env);
2818 struct iam_iterator *it = &oti->oti_idx_it;
2819 struct iam_rec *iam_rec;
2824 LASSERT(osd_invariant(obj));
2825 LASSERT(dt_object_exists(dt));
2826 LASSERT(bag->ic_object == obj->oo_inode);
2828 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2831 ipd = osd_idx_ipd_get(env, bag);
2835 /* got ipd now we can start iterator. */
2836 iam_it_init(it, bag, 0, ipd);
2838 rc = iam_it_get(it, (struct iam_key *)key);
2840 if (S_ISDIR(obj->oo_inode->i_mode))
2841 iam_rec = (struct iam_rec *)oti->oti_ldp;
2843 iam_rec = (struct iam_rec *) rec;
2845 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2846 if (S_ISDIR(obj->oo_inode->i_mode))
2847 osd_fid_unpack((struct lu_fid *) rec,
2848 (struct osd_fid_pack *)iam_rec);
2852 osd_ipd_put(env, bag, ipd);
2854 LINVRNT(osd_invariant(obj));
2859 static int osd_index_declare_iam_insert(const struct lu_env *env,
2860 struct dt_object *dt,
2861 const struct dt_rec *rec,
2862 const struct dt_key *key,
2863 struct thandle *handle)
2865 struct osd_thandle *oh;
2867 LASSERT(dt_object_exists(dt));
2868 LASSERT(handle != NULL);
2870 oh = container_of0(handle, struct osd_thandle, ot_super);
2871 LASSERT(oh->ot_handle == NULL);
2873 OSD_DECLARE_OP(oh, insert);
2874 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
2880 * Inserts (key, value) pair in \a dt index object.
2882 * \param dt osd index object
2883 * \param key key for index
2884 * \param rec record reference
2885 * \param th transaction handler
2888 * \retval -ve failure
2890 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2891 const struct dt_rec *rec,
2892 const struct dt_key *key, struct thandle *th,
2893 struct lustre_capa *capa, int ignore_quota)
2895 struct osd_object *obj = osd_dt_obj(dt);
2896 struct iam_path_descr *ipd;
2897 struct osd_thandle *oh;
2898 struct iam_container *bag = &obj->oo_dir->od_container;
2899 #ifdef HAVE_QUOTA_SUPPORT
2900 cfs_cap_t save = cfs_curproc_cap_pack();
2902 struct osd_thread_info *oti = osd_oti_get(env);
2903 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2908 LINVRNT(osd_invariant(obj));
2909 LASSERT(dt_object_exists(dt));
2910 LASSERT(bag->ic_object == obj->oo_inode);
2911 LASSERT(th != NULL);
2913 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2916 OSD_EXEC_OP(th, insert);
2918 ipd = osd_idx_ipd_get(env, bag);
2919 if (unlikely(ipd == NULL))
2922 oh = container_of0(th, struct osd_thandle, ot_super);
2923 LASSERT(oh->ot_handle != NULL);
2924 LASSERT(oh->ot_handle->h_transaction != NULL);
2925 #ifdef HAVE_QUOTA_SUPPORT
2927 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2929 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2931 if (S_ISDIR(obj->oo_inode->i_mode))
2932 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2934 iam_rec = (struct iam_rec *) rec;
2935 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2937 #ifdef HAVE_QUOTA_SUPPORT
2938 cfs_curproc_cap_unpack(save);
2940 osd_ipd_put(env, bag, ipd);
2941 LINVRNT(osd_invariant(obj));
2946 * Calls ldiskfs_add_entry() to add directory entry
2947 * into the directory. This is required for
2948 * interoperability mode (b11826)
2950 * \retval 0, on success
2951 * \retval -ve, on error
2953 static int __osd_ea_add_rec(struct osd_thread_info *info,
2954 struct osd_object *pobj, struct inode *cinode,
2955 const char *name, const struct dt_rec *fid,
2956 struct htree_lock *hlock, struct thandle *th)
2958 struct ldiskfs_dentry_param *ldp;
2959 struct dentry *child;
2960 struct osd_thandle *oth;
2963 oth = container_of(th, struct osd_thandle, ot_super);
2964 LASSERT(oth->ot_handle != NULL);
2965 LASSERT(oth->ot_handle->h_transaction != NULL);
2967 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2969 /* XXX: remove fid_is_igif() check here.
2970 * IGIF check is just to handle insertion of .. when it is 'ROOT',
2971 * it is IGIF now but needs FID in dir entry as well for readdir
2973 * LU-838 should fix that and remove fid_is_igif() check */
2974 if (fid_is_igif((struct lu_fid *)fid) ||
2975 fid_is_norm((struct lu_fid *)fid)) {
2976 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2977 osd_get_ldiskfs_dirent_param(ldp, fid);
2978 child->d_fsdata = (void *)ldp;
2980 child->d_fsdata = NULL;
2982 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
2988 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2989 * into the directory.Also sets flags into osd object to
2990 * indicate dot and dotdot are created. This is required for
2991 * interoperability mode (b11826)
2993 * \param dir directory for dot and dotdot fixup.
2994 * \param obj child object for linking
2996 * \retval 0, on success
2997 * \retval -ve, on error
2999 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3000 struct osd_object *dir,
3001 struct inode *parent_dir, const char *name,
3002 const struct dt_rec *dot_fid,
3003 const struct dt_rec *dot_dot_fid,
3006 struct inode *inode = dir->oo_inode;
3007 struct ldiskfs_dentry_param *dot_ldp;
3008 struct ldiskfs_dentry_param *dot_dot_ldp;
3009 struct osd_thandle *oth;
3012 oth = container_of(th, struct osd_thandle, ot_super);
3013 LASSERT(oth->ot_handle->h_transaction != NULL);
3014 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3016 if (strcmp(name, dot) == 0) {
3017 if (dir->oo_compat_dot_created) {
3020 LASSERT(inode == parent_dir);
3021 dir->oo_compat_dot_created = 1;
3024 } else if(strcmp(name, dotdot) == 0) {
3025 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3026 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3028 if (!dir->oo_compat_dot_created)
3030 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3031 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3032 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3037 /* in case of rename, dotdot is already created */
3038 if (dir->oo_compat_dotdot_created) {
3039 return __osd_ea_add_rec(info, dir, parent_dir, name,
3040 dot_dot_fid, NULL, th);
3043 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3044 inode, dot_ldp, dot_dot_ldp);
3046 dir->oo_compat_dotdot_created = 1;
3054 * It will call the appropriate osd_add* function and return the
3055 * value, return by respective functions.
3057 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3058 struct inode *cinode, const char *name,
3059 const struct dt_rec *fid, struct thandle *th)
3061 struct osd_thread_info *info = osd_oti_get(env);
3062 struct htree_lock *hlock;
3065 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3067 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3069 if (hlock != NULL) {
3070 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3073 cfs_down_write(&pobj->oo_ext_idx_sem);
3075 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3076 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3079 if (hlock != NULL) {
3080 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3081 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3083 cfs_down_write(&pobj->oo_ext_idx_sem);
3086 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3090 ldiskfs_htree_unlock(hlock);
3092 cfs_up_write(&pobj->oo_ext_idx_sem);
3098 * Calls ->lookup() to find dentry. From dentry get inode and
3099 * read inode's ea to get fid. This is required for interoperability
3102 * \retval 0, on success
3103 * \retval -ve, on error
3105 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3106 struct dt_rec *rec, const struct dt_key *key)
3108 struct inode *dir = obj->oo_inode;
3109 struct dentry *dentry;
3110 struct ldiskfs_dir_entry_2 *de;
3111 struct buffer_head *bh;
3112 struct lu_fid *fid = (struct lu_fid *) rec;
3113 struct htree_lock *hlock = NULL;
3117 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3119 dentry = osd_child_dentry_get(env, obj,
3120 (char *)key, strlen((char *)key));
3122 if (obj->oo_hl_head != NULL) {
3123 hlock = osd_oti_get(env)->oti_hlock;
3124 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3125 dir, LDISKFS_HLOCK_LOOKUP);
3127 cfs_down_read(&obj->oo_ext_idx_sem);
3130 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3132 ino = le32_to_cpu(de->inode);
3133 rc = osd_get_fid_from_dentry(de, rec);
3135 /* done with de, release bh */
3138 rc = osd_ea_fid_get(env, obj, ino, fid);
3144 ldiskfs_htree_unlock(hlock);
3146 cfs_up_read(&obj->oo_ext_idx_sem);
3151 * Find the osd object for given fid.
3153 * \param fid need to find the osd object having this fid
3155 * \retval osd_object on success
3156 * \retval -ve on error
3158 struct osd_object *osd_object_find(const struct lu_env *env,
3159 struct dt_object *dt,
3160 const struct lu_fid *fid)
3162 struct lu_device *ludev = dt->do_lu.lo_dev;
3163 struct osd_object *child = NULL;
3164 struct lu_object *luch;
3165 struct lu_object *lo;
3167 luch = lu_object_find(env, ludev, fid, NULL);
3168 if (!IS_ERR(luch)) {
3169 if (lu_object_exists(luch)) {
3170 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3172 child = osd_obj(lo);
3174 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3175 "lu_object can't be located"
3176 ""DFID"\n", PFID(fid));
3178 if (child == NULL) {
3179 lu_object_put(env, luch);
3180 CERROR("Unable to get osd_object\n");
3181 child = ERR_PTR(-ENOENT);
3184 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3185 "lu_object does not exists "DFID"\n",
3187 child = ERR_PTR(-ENOENT);
3190 child = (void *)luch;
3196 * Put the osd object once done with it.
3198 * \param obj osd object that needs to be put
3200 static inline void osd_object_put(const struct lu_env *env,
3201 struct osd_object *obj)
3203 lu_object_put(env, &obj->oo_dt.do_lu);
3206 static int osd_index_declare_ea_insert(const struct lu_env *env,
3207 struct dt_object *dt,
3208 const struct dt_rec *rec,
3209 const struct dt_key *key,
3210 struct thandle *handle)
3212 struct osd_thandle *oh;
3214 LASSERT(dt_object_exists(dt));
3215 LASSERT(handle != NULL);
3217 oh = container_of0(handle, struct osd_thandle, ot_super);
3218 LASSERT(oh->ot_handle == NULL);
3220 OSD_DECLARE_OP(oh, insert);
3221 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3223 LASSERT(osd_dt_obj(dt)->oo_inode);
3224 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3225 osd_dt_obj(dt)->oo_inode);
3226 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3227 osd_dt_obj(dt)->oo_inode);
3233 * Index add function for interoperability mode (b11826).
3234 * It will add the directory entry.This entry is needed to
3235 * maintain name->fid mapping.
3237 * \param key it is key i.e. file entry to be inserted
3238 * \param rec it is value of given key i.e. fid
3240 * \retval 0, on success
3241 * \retval -ve, on error
3243 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3244 const struct dt_rec *rec,
3245 const struct dt_key *key, struct thandle *th,
3246 struct lustre_capa *capa, int ignore_quota)
3248 struct osd_object *obj = osd_dt_obj(dt);
3249 struct lu_fid *fid = (struct lu_fid *) rec;
3250 const char *name = (const char *)key;
3251 struct osd_object *child;
3252 #ifdef HAVE_QUOTA_SUPPORT
3253 cfs_cap_t save = cfs_curproc_cap_pack();
3259 LASSERT(osd_invariant(obj));
3260 LASSERT(dt_object_exists(dt));
3261 LASSERT(th != NULL);
3263 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3266 child = osd_object_find(env, dt, fid);
3267 if (!IS_ERR(child)) {
3268 #ifdef HAVE_QUOTA_SUPPORT
3270 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3272 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3274 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3275 #ifdef HAVE_QUOTA_SUPPORT
3276 cfs_curproc_cap_unpack(save);
3278 osd_object_put(env, child);
3280 rc = PTR_ERR(child);
3283 LASSERT(osd_invariant(obj));
3288 * Initialize osd Iterator for given osd index object.
3290 * \param dt osd index object
3293 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3294 struct dt_object *dt,
3296 struct lustre_capa *capa)
3298 struct osd_it_iam *it;
3299 struct osd_thread_info *oti = osd_oti_get(env);
3300 struct osd_object *obj = osd_dt_obj(dt);
3301 struct lu_object *lo = &dt->do_lu;
3302 struct iam_path_descr *ipd;
3303 struct iam_container *bag = &obj->oo_dir->od_container;
3305 LASSERT(lu_object_exists(lo));
3307 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3308 return ERR_PTR(-EACCES);
3311 ipd = osd_it_ipd_get(env, bag);
3312 if (likely(ipd != NULL)) {
3316 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3317 return (struct dt_it *)it;
3319 return ERR_PTR(-ENOMEM);
3323 * free given Iterator.
3326 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3328 struct osd_it_iam *it = (struct osd_it_iam *)di;
3329 struct osd_object *obj = it->oi_obj;
3331 iam_it_fini(&it->oi_it);
3332 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3333 lu_object_put(env, &obj->oo_dt.do_lu);
3337 * Move Iterator to record specified by \a key
3339 * \param di osd iterator
3340 * \param key key for index
3342 * \retval +ve di points to record with least key not larger than key
3343 * \retval 0 di points to exact matched key
3344 * \retval -ve failure
3347 static int osd_it_iam_get(const struct lu_env *env,
3348 struct dt_it *di, const struct dt_key *key)
3350 struct osd_it_iam *it = (struct osd_it_iam *)di;
3352 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3358 * \param di osd iterator
3361 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3363 struct osd_it_iam *it = (struct osd_it_iam *)di;
3365 iam_it_put(&it->oi_it);
3369 * Move iterator by one record
3371 * \param di osd iterator
3373 * \retval +1 end of container reached
3375 * \retval -ve failure
3378 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3380 struct osd_it_iam *it = (struct osd_it_iam *)di;
3382 return iam_it_next(&it->oi_it);
3386 * Return pointer to the key under iterator.
3389 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3390 const struct dt_it *di)
3392 struct osd_it_iam *it = (struct osd_it_iam *)di;
3394 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3398 * Return size of key under iterator (in bytes)
3401 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3403 struct osd_it_iam *it = (struct osd_it_iam *)di;
3405 return iam_it_key_size(&it->oi_it);
3408 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3409 int len, __u16 type)
3411 struct luda_type *lt;
3412 const unsigned align = sizeof(struct luda_type) - 1;
3414 /* check if file type is required */
3415 if (attr & LUDA_TYPE) {
3416 len = (len + align) & ~align;
3418 lt = (void *) ent->lde_name + len;
3419 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3420 ent->lde_attrs |= LUDA_TYPE;
3423 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3427 * build lu direct from backend fs dirent.
3430 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3431 struct lu_fid *fid, __u64 offset,
3432 char *name, __u16 namelen,
3433 __u16 type, __u32 attr)
3435 fid_cpu_to_le(&ent->lde_fid, fid);
3436 ent->lde_attrs = LUDA_FID;
3438 ent->lde_hash = cpu_to_le64(offset);
3439 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3441 strncpy(ent->lde_name, name, namelen);
3442 ent->lde_namelen = cpu_to_le16(namelen);
3444 /* append lustre attributes */
3445 osd_it_append_attrs(ent, attr, namelen, type);
3449 * Return pointer to the record under iterator.
3451 static int osd_it_iam_rec(const struct lu_env *env,
3452 const struct dt_it *di,
3453 struct dt_rec *dtrec, __u32 attr)
3455 struct osd_it_iam *it = (struct osd_it_iam *)di;
3456 struct osd_thread_info *info = osd_oti_get(env);
3457 struct lu_fid *fid = &info->oti_fid;
3458 const struct osd_fid_pack *rec;
3459 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3465 name = (char *)iam_it_key_get(&it->oi_it);
3467 RETURN(PTR_ERR(name));
3469 namelen = iam_it_key_size(&it->oi_it);
3471 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3473 RETURN(PTR_ERR(rec));
3475 rc = osd_fid_unpack(fid, rec);
3479 hash = iam_it_store(&it->oi_it);
3481 /* IAM does not store object type in IAM index (dir) */
3482 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3489 * Returns cookie for current Iterator position.
3491 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3493 struct osd_it_iam *it = (struct osd_it_iam *)di;
3495 return iam_it_store(&it->oi_it);
3499 * Restore iterator from cookie.
3501 * \param di osd iterator
3502 * \param hash Iterator location cookie
3504 * \retval +ve di points to record with least key not larger than key.
3505 * \retval 0 di points to exact matched key
3506 * \retval -ve failure
3509 static int osd_it_iam_load(const struct lu_env *env,
3510 const struct dt_it *di, __u64 hash)
3512 struct osd_it_iam *it = (struct osd_it_iam *)di;
3514 return iam_it_load(&it->oi_it, hash);
3517 static const struct dt_index_operations osd_index_iam_ops = {
3518 .dio_lookup = osd_index_iam_lookup,
3519 .dio_declare_insert = osd_index_declare_iam_insert,
3520 .dio_insert = osd_index_iam_insert,
3521 .dio_declare_delete = osd_index_declare_iam_delete,
3522 .dio_delete = osd_index_iam_delete,
3524 .init = osd_it_iam_init,
3525 .fini = osd_it_iam_fini,
3526 .get = osd_it_iam_get,
3527 .put = osd_it_iam_put,
3528 .next = osd_it_iam_next,
3529 .key = osd_it_iam_key,
3530 .key_size = osd_it_iam_key_size,
3531 .rec = osd_it_iam_rec,
3532 .store = osd_it_iam_store,
3533 .load = osd_it_iam_load
3538 * Creates or initializes iterator context.
3540 * \retval struct osd_it_ea, iterator structure on success
3543 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3544 struct dt_object *dt,
3546 struct lustre_capa *capa)
3548 struct osd_object *obj = osd_dt_obj(dt);
3549 struct osd_thread_info *info = osd_oti_get(env);
3550 struct osd_it_ea *it = &info->oti_it_ea;
3551 struct lu_object *lo = &dt->do_lu;
3552 struct dentry *obj_dentry = &info->oti_it_dentry;
3554 LASSERT(lu_object_exists(lo));
3556 obj_dentry->d_inode = obj->oo_inode;
3557 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3558 obj_dentry->d_name.hash = 0;
3560 it->oie_rd_dirent = 0;
3561 it->oie_it_dirent = 0;
3562 it->oie_dirent = NULL;
3563 it->oie_buf = info->oti_it_ea_buf;
3565 it->oie_file.f_pos = 0;
3566 it->oie_file.f_dentry = obj_dentry;
3567 if (attr & LUDA_64BITHASH)
3568 it->oie_file.f_flags = O_64BITHASH;
3570 it->oie_file.f_flags = O_32BITHASH;
3571 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3572 it->oie_file.f_op = obj->oo_inode->i_fop;
3573 it->oie_file.private_data = NULL;
3575 RETURN((struct dt_it *) it);
3579 * Destroy or finishes iterator context.
3581 * \param di iterator structure to be destroyed
3583 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3585 struct osd_it_ea *it = (struct osd_it_ea *)di;
3586 struct osd_object *obj = it->oie_obj;
3587 struct inode *inode = obj->oo_inode;
3590 it->oie_file.f_op->release(inode, &it->oie_file);
3591 lu_object_put(env, &obj->oo_dt.do_lu);
3596 * It position the iterator at given key, so that next lookup continues from
3597 * that key Or it is similar to dio_it->load() but based on a key,
3598 * rather than file position.
3600 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3603 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3605 static int osd_it_ea_get(const struct lu_env *env,
3606 struct dt_it *di, const struct dt_key *key)
3608 struct osd_it_ea *it = (struct osd_it_ea *)di;
3611 LASSERT(((const char *)key)[0] == '\0');
3612 it->oie_file.f_pos = 0;
3613 it->oie_rd_dirent = 0;
3614 it->oie_it_dirent = 0;
3615 it->oie_dirent = NULL;
3623 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3628 * It is called internally by ->readdir(). It fills the
3629 * iterator's in-memory data structure with required
3630 * information i.e. name, namelen, rec_size etc.
3632 * \param buf in which information to be filled in.
3633 * \param name name of the file in given dir
3635 * \retval 0 on success
3636 * \retval 1 on buffer full
3638 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3639 loff_t offset, __u64 ino,
3642 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3643 struct osd_it_ea_dirent *ent = it->oie_dirent;
3644 struct lu_fid *fid = &ent->oied_fid;
3645 struct osd_fid_pack *rec;
3648 /* this should never happen */
3649 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3650 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3654 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3658 if (d_type & LDISKFS_DIRENT_LUFID) {
3659 rec = (struct osd_fid_pack*) (name + namelen + 1);
3661 if (osd_fid_unpack(fid, rec) != 0)
3664 d_type &= ~LDISKFS_DIRENT_LUFID;
3669 ent->oied_ino = ino;
3670 ent->oied_off = offset;
3671 ent->oied_namelen = namelen;
3672 ent->oied_type = d_type;
3674 memcpy(ent->oied_name, name, namelen);
3676 it->oie_rd_dirent++;
3677 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3682 * Calls ->readdir() to load a directory entry at a time
3683 * and stored it in iterator's in-memory data structure.
3685 * \param di iterator's in memory structure
3687 * \retval 0 on success
3688 * \retval -ve on error
3690 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3691 const struct dt_it *di)
3693 struct osd_it_ea *it = (struct osd_it_ea *)di;
3694 struct osd_object *obj = it->oie_obj;
3695 struct inode *inode = obj->oo_inode;
3696 struct htree_lock *hlock = NULL;
3700 it->oie_dirent = it->oie_buf;
3701 it->oie_rd_dirent = 0;
3703 if (obj->oo_hl_head != NULL) {
3704 hlock = osd_oti_get(env)->oti_hlock;
3705 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3706 inode, LDISKFS_HLOCK_READDIR);
3708 cfs_down_read(&obj->oo_ext_idx_sem);
3711 result = inode->i_fop->readdir(&it->oie_file, it,
3712 (filldir_t) osd_ldiskfs_filldir);
3715 ldiskfs_htree_unlock(hlock);
3717 cfs_up_read(&obj->oo_ext_idx_sem);
3719 if (it->oie_rd_dirent == 0) {
3722 it->oie_dirent = it->oie_buf;
3723 it->oie_it_dirent = 1;
3730 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3731 * to load a directory entry at a time and stored it in
3732 * iterator's in-memory data structure.
3734 * \param di iterator's in memory structure
3736 * \retval +ve iterator reached to end
3737 * \retval 0 iterator not reached to end
3738 * \retval -ve on error
3740 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3742 struct osd_it_ea *it = (struct osd_it_ea *)di;
3747 if (it->oie_it_dirent < it->oie_rd_dirent) {
3749 (void *) it->oie_dirent +
3750 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3751 it->oie_dirent->oied_namelen);
3752 it->oie_it_dirent++;
3755 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3758 rc = osd_ldiskfs_it_fill(env, di);
3765 * Returns the key at current position from iterator's in memory structure.
3767 * \param di iterator's in memory structure
3769 * \retval key i.e. struct dt_key on success
3771 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3772 const struct dt_it *di)
3774 struct osd_it_ea *it = (struct osd_it_ea *)di;
3776 return (struct dt_key *)it->oie_dirent->oied_name;
3780 * Returns the key's size at current position from iterator's in memory structure.
3782 * \param di iterator's in memory structure
3784 * \retval key_size i.e. struct dt_key on success
3786 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3788 struct osd_it_ea *it = (struct osd_it_ea *)di;
3790 return it->oie_dirent->oied_namelen;
3795 * Returns the value (i.e. fid/igif) at current position from iterator's
3796 * in memory structure.
3798 * \param di struct osd_it_ea, iterator's in memory structure
3799 * \param attr attr requested for dirent.
3800 * \param lde lustre dirent
3802 * \retval 0 no error and \param lde has correct lustre dirent.
3803 * \retval -ve on error
3805 static inline int osd_it_ea_rec(const struct lu_env *env,
3806 const struct dt_it *di,
3807 struct dt_rec *dtrec, __u32 attr)
3809 struct osd_it_ea *it = (struct osd_it_ea *)di;
3810 struct osd_object *obj = it->oie_obj;
3811 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3812 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3817 if (!fid_is_sane(fid))
3818 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3821 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3822 it->oie_dirent->oied_name,
3823 it->oie_dirent->oied_namelen,
3824 it->oie_dirent->oied_type,
3830 * Returns a cookie for current position of the iterator head, so that
3831 * user can use this cookie to load/start the iterator next time.
3833 * \param di iterator's in memory structure
3835 * \retval cookie for current position, on success
3837 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3839 struct osd_it_ea *it = (struct osd_it_ea *)di;
3841 return it->oie_dirent->oied_off;
3845 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3846 * to load a directory entry at a time and stored it i inn,
3847 * in iterator's in-memory data structure.
3849 * \param di struct osd_it_ea, iterator's in memory structure
3851 * \retval +ve on success
3852 * \retval -ve on error
3854 static int osd_it_ea_load(const struct lu_env *env,
3855 const struct dt_it *di, __u64 hash)
3857 struct osd_it_ea *it = (struct osd_it_ea *)di;
3861 it->oie_file.f_pos = hash;
3863 rc = osd_ldiskfs_it_fill(env, di);
3871 * Index lookup function for interoperability mode (b11826).
3873 * \param key, key i.e. file name to be searched
3875 * \retval +ve, on success
3876 * \retval -ve, on error
3878 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3879 struct dt_rec *rec, const struct dt_key *key,
3880 struct lustre_capa *capa)
3882 struct osd_object *obj = osd_dt_obj(dt);
3887 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3888 LINVRNT(osd_invariant(obj));
3890 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3893 rc = osd_ea_lookup_rec(env, obj, rec, key);
3901 * Index and Iterator operations for interoperability
3902 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3904 static const struct dt_index_operations osd_index_ea_ops = {
3905 .dio_lookup = osd_index_ea_lookup,
3906 .dio_declare_insert = osd_index_declare_ea_insert,
3907 .dio_insert = osd_index_ea_insert,
3908 .dio_declare_delete = osd_index_declare_ea_delete,
3909 .dio_delete = osd_index_ea_delete,
3911 .init = osd_it_ea_init,
3912 .fini = osd_it_ea_fini,
3913 .get = osd_it_ea_get,
3914 .put = osd_it_ea_put,
3915 .next = osd_it_ea_next,
3916 .key = osd_it_ea_key,
3917 .key_size = osd_it_ea_key_size,
3918 .rec = osd_it_ea_rec,
3919 .store = osd_it_ea_store,
3920 .load = osd_it_ea_load
3924 static void *osd_key_init(const struct lu_context *ctx,
3925 struct lu_context_key *key)
3927 struct osd_thread_info *info;
3929 OBD_ALLOC_PTR(info);
3931 return ERR_PTR(-ENOMEM);
3933 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3934 if (info->oti_it_ea_buf == NULL)
3937 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
3939 info->oti_hlock = ldiskfs_htree_lock_alloc();
3940 if (info->oti_hlock == NULL)
3946 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3949 return ERR_PTR(-ENOMEM);
3952 static void osd_key_fini(const struct lu_context *ctx,
3953 struct lu_context_key *key, void* data)
3955 struct osd_thread_info *info = data;
3957 if (info->oti_hlock != NULL)
3958 ldiskfs_htree_lock_free(info->oti_hlock);
3959 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3963 static void osd_key_exit(const struct lu_context *ctx,
3964 struct lu_context_key *key, void *data)
3966 struct osd_thread_info *info = data;
3968 LASSERT(info->oti_r_locks == 0);
3969 LASSERT(info->oti_w_locks == 0);
3970 LASSERT(info->oti_txns == 0);
3973 /* type constructor/destructor: osd_type_init, osd_type_fini */
3974 LU_TYPE_INIT_FINI(osd, &osd_key);
3976 struct lu_context_key osd_key = {
3977 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
3978 .lct_init = osd_key_init,
3979 .lct_fini = osd_key_fini,
3980 .lct_exit = osd_key_exit
3984 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3985 const char *name, struct lu_device *next)
3987 return osd_procfs_init(osd_dev(d), name);
3990 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3992 struct osd_thread_info *info = osd_oti_get(env);
3996 if (o->od_oi_table != NULL)
3997 osd_oi_fini(info, o);
4000 fsfilt_put_ops(o->od_fsops);
4007 static int osd_mount(const struct lu_env *env,
4008 struct osd_device *o, struct lustre_cfg *cfg)
4010 struct lustre_mount_info *lmi;
4011 const char *dev = lustre_cfg_string(cfg, 0);
4012 struct lustre_disk_data *ldd;
4013 struct lustre_sb_info *lsi;
4018 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4019 if (o->od_fsops == NULL) {
4020 CERROR("Can't find fsfilt_ldiskfs\n");
4024 if (o->od_mount != NULL) {
4025 CERROR("Already mounted (%s)\n", dev);
4030 lmi = server_get_mount(dev);
4032 CERROR("Cannot get mount info for %s!\n", dev);
4036 LASSERT(lmi != NULL);
4037 /* save lustre_mount_info in dt_device */
4039 o->od_mnt = lmi->lmi_mnt;
4041 lsi = s2lsi(lmi->lmi_sb);
4044 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4046 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", dev);
4050 if (ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
4051 rc = osd_compat_init(o);
4053 CERROR("%s: can't initialize compats: %d\n", dev, rc);
4059 static struct lu_device *osd_device_fini(const struct lu_env *env,
4060 struct lu_device *d)
4065 osd_compat_fini(osd_dev(d));
4067 shrink_dcache_sb(osd_sb(osd_dev(d)));
4068 osd_sync(env, lu2dt_dev(d));
4070 rc = osd_procfs_fini(osd_dev(d));
4072 CERROR("proc fini error %d \n", rc);
4073 RETURN (ERR_PTR(rc));
4076 if (osd_dev(d)->od_mount)
4077 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4078 osd_dev(d)->od_mount->lmi_mnt);
4079 osd_dev(d)->od_mount = NULL;
4084 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4085 struct lu_device_type *t,
4086 struct lustre_cfg *cfg)
4088 struct lu_device *l;
4089 struct osd_device *o;
4095 result = dt_device_init(&o->od_dt_dev, t);
4098 l->ld_ops = &osd_lu_ops;
4099 o->od_dt_dev.dd_ops = &osd_dt_ops;
4100 cfs_spin_lock_init(&o->od_osfs_lock);
4101 o->od_osfs_age = cfs_time_shift_64(-1000);
4102 o->od_capa_hash = init_capa_hash();
4103 if (o->od_capa_hash == NULL) {
4104 dt_device_fini(&o->od_dt_dev);
4105 l = ERR_PTR(-ENOMEM);
4108 l = ERR_PTR(result);
4113 l = ERR_PTR(-ENOMEM);
4117 static struct lu_device *osd_device_free(const struct lu_env *env,
4118 struct lu_device *d)
4120 struct osd_device *o = osd_dev(d);
4123 cleanup_capa_hash(o->od_capa_hash);
4124 dt_device_fini(&o->od_dt_dev);
4129 static int osd_process_config(const struct lu_env *env,
4130 struct lu_device *d, struct lustre_cfg *cfg)
4132 struct osd_device *o = osd_dev(d);
4136 switch(cfg->lcfg_command) {
4138 err = osd_mount(env, o, cfg);
4141 err = osd_shutdown(env, o);
4150 static int osd_recovery_complete(const struct lu_env *env,
4151 struct lu_device *d)
4156 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4157 struct lu_device *dev)
4159 struct osd_device *osd = osd_dev(dev);
4160 struct osd_thread_info *oti = osd_oti_get(env);
4165 /* 1. initialize oi before any file create or file open */
4166 result = osd_oi_init(oti, osd);
4170 if (!lu_device_is_md(pdev))
4173 /* 2. setup local objects */
4174 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4178 static const struct lu_object_operations osd_lu_obj_ops = {
4179 .loo_object_init = osd_object_init,
4180 .loo_object_delete = osd_object_delete,
4181 .loo_object_release = osd_object_release,
4182 .loo_object_free = osd_object_free,
4183 .loo_object_print = osd_object_print,
4184 .loo_object_invariant = osd_object_invariant
4187 const struct lu_device_operations osd_lu_ops = {
4188 .ldo_object_alloc = osd_object_alloc,
4189 .ldo_process_config = osd_process_config,
4190 .ldo_recovery_complete = osd_recovery_complete,
4191 .ldo_prepare = osd_prepare,
4194 static const struct lu_device_type_operations osd_device_type_ops = {
4195 .ldto_init = osd_type_init,
4196 .ldto_fini = osd_type_fini,
4198 .ldto_start = osd_type_start,
4199 .ldto_stop = osd_type_stop,
4201 .ldto_device_alloc = osd_device_alloc,
4202 .ldto_device_free = osd_device_free,
4204 .ldto_device_init = osd_device_init,
4205 .ldto_device_fini = osd_device_fini
4208 static struct lu_device_type osd_device_type = {
4209 .ldt_tags = LU_DEVICE_DT,
4210 .ldt_name = LUSTRE_OSD_NAME,
4211 .ldt_ops = &osd_device_type_ops,
4212 .ldt_ctx_tags = LCT_LOCAL,
4216 * lprocfs legacy support.
4218 static struct obd_ops osd_obd_device_ops = {
4219 .o_owner = THIS_MODULE
4222 static int __init osd_mod_init(void)
4224 struct lprocfs_static_vars lvars;
4227 lprocfs_osd_init_vars(&lvars);
4228 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4229 LUSTRE_OSD_NAME, &osd_device_type);
4232 static void __exit osd_mod_exit(void)
4234 class_unregister_type(LUSTRE_OSD_NAME);
4237 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4238 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4239 MODULE_LICENSE("GPL");
4241 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);