1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/osd/osd_handler.c
40 * Top-level entry points into osd module
42 * Author: Nikita Danilov <nikita@clusterfs.com>
43 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
53 /* LUSTRE_VERSION_CODE */
54 #include <lustre_ver.h>
55 /* prerequisite for linux/xattr.h */
56 #include <linux/types.h>
57 /* prerequisite for linux/xattr.h */
59 /* XATTR_{REPLACE,CREATE} */
60 #include <linux/xattr.h>
65 * struct OBD_{ALLOC,FREE}*()
68 #include <obd_support.h>
69 /* struct ptlrpc_thread */
70 #include <lustre_net.h>
73 #include <lustre_fid.h>
75 #include "osd_internal.h"
78 /* llo_* api support */
79 #include <md_object.h>
81 #ifdef HAVE_LDISKFS_PDO
83 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
84 "ldiskfs with parallel directory operations");
89 static const char dot[] = ".";
90 static const char dotdot[] = "..";
91 static const char remote_obj_dir[] = "REM_OBJ_DIR";
93 static const struct lu_object_operations osd_lu_obj_ops;
94 static const struct dt_object_operations osd_obj_ops;
95 static const struct dt_object_operations osd_obj_ea_ops;
96 static const struct dt_index_operations osd_index_iam_ops;
97 static const struct dt_index_operations osd_index_ea_ops;
99 static int osd_has_index(const struct osd_object *obj)
101 return obj->oo_dt.do_index_ops != NULL;
104 static int osd_object_invariant(const struct lu_object *l)
106 return osd_invariant(osd_obj(l));
109 #ifdef HAVE_QUOTA_SUPPORT
111 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
113 struct md_ucred *uc = md_ucred(env);
118 save->oc_uid = current_fsuid();
119 save->oc_gid = current_fsgid();
120 save->oc_cap = current_cap();
121 if ((tc = prepare_creds())) {
122 tc->fsuid = uc->mu_fsuid;
123 tc->fsgid = uc->mu_fsgid;
126 /* XXX not suboptimal */
127 cfs_curproc_cap_unpack(uc->mu_cap);
131 osd_pop_ctxt(struct osd_ctxt *save)
135 if ((tc = prepare_creds())) {
136 tc->fsuid = save->oc_uid;
137 tc->fsgid = save->oc_gid;
138 tc->cap_effective = save->oc_cap;
145 * Concurrency: doesn't matter
147 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
149 return osd_oti_get(env)->oti_r_locks > 0;
153 * Concurrency: doesn't matter
155 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
157 struct osd_thread_info *oti = osd_oti_get(env);
158 return oti->oti_w_locks > 0 && o->oo_owner == env;
162 * Concurrency: doesn't access mutable data
164 static int osd_root_get(const struct lu_env *env,
165 struct dt_device *dev, struct lu_fid *f)
169 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
170 LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
174 static inline int osd_qid_type(struct osd_thandle *oh, int i)
176 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
179 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
181 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
184 void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
185 int type, uid_t id, struct inode *inode)
188 int i, allocated = 0;
189 struct osd_object *obj;
193 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
196 /* id entry is allocated in the quota file */
197 if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
200 for (i = 0; i < oh->ot_id_cnt; i++) {
201 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
205 if (unlikely(i >= OSD_MAX_UGID_CNT)) {
206 CERROR("more than %d uid/gids for a transaction?\n", i);
210 oh->ot_id_array[i] = id;
211 osd_qid_set_type(oh, i, type);
213 obj = osd_dt_obj(dt);
214 oh->ot_credits += (allocated || id == 0) ?
215 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
220 * OSD object methods.
224 * Concurrency: no concurrent access is possible that early in object
227 static struct lu_object *osd_object_alloc(const struct lu_env *env,
228 const struct lu_object_header *hdr,
231 struct osd_object *mo;
237 l = &mo->oo_dt.do_lu;
238 dt_object_init(&mo->oo_dt, NULL, d);
239 if (osd_dev(d)->od_iop_mode)
240 mo->oo_dt.do_ops = &osd_obj_ea_ops;
242 mo->oo_dt.do_ops = &osd_obj_ops;
244 l->lo_ops = &osd_lu_obj_ops;
245 cfs_init_rwsem(&mo->oo_sem);
246 cfs_init_rwsem(&mo->oo_ext_idx_sem);
247 cfs_spin_lock_init(&mo->oo_guard);
255 * retrieve object from backend ext fs.
257 struct inode *osd_iget(struct osd_thread_info *info,
258 struct osd_device *dev,
259 const struct osd_inode_id *id)
261 struct inode *inode = NULL;
263 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
265 CERROR("Cannot get inode, rc = %li\n", PTR_ERR(inode));
266 } else if (id->oii_gen != OSD_OII_NOGEN &&
267 inode->i_generation != id->oii_gen) {
269 inode = ERR_PTR(-ESTALE);
270 } else if (inode->i_nlink == 0) {
271 /* due to parallel readdir and unlink,
272 * we can have dead inode here. */
273 CWARN("stale inode\n");
274 make_bad_inode(inode);
276 inode = ERR_PTR(-ESTALE);
277 } else if (is_bad_inode(inode)) {
278 CERROR("bad inode %lx\n",inode->i_ino);
280 inode = ERR_PTR(-ENOENT);
282 /* Do not update file c/mtime in ldiskfs.
283 * NB: we don't have any lock to protect this because we don't
284 * have reference on osd_object now, but contention with
285 * another lookup + attr_set can't happen in the tiny window
286 * between if (...) and set S_NOCMTIME. */
287 if (!(inode->i_flags & S_NOCMTIME))
288 inode->i_flags |= S_NOCMTIME;
293 static int osd_fid_lookup(const struct lu_env *env,
294 struct osd_object *obj, const struct lu_fid *fid)
296 struct osd_thread_info *info;
297 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
298 struct osd_device *dev;
299 struct osd_inode_id *id;
303 LINVRNT(osd_invariant(obj));
304 LASSERT(obj->oo_inode == NULL);
305 LASSERTF(fid_is_sane(fid) || osd_fid_is_root(fid), DFID, PFID(fid));
307 * This assertion checks that osd layer sees only local
308 * fids. Unfortunately it is somewhat expensive (does a
309 * cache-lookup). Disabling it for production/acceptance-testing.
311 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
315 info = osd_oti_get(env);
320 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
323 result = osd_oi_lookup(info, dev, fid, id);
325 if (result == -ENOENT)
330 inode = osd_iget(info, dev, id);
333 * If fid wasn't found in oi, inode-less object is
334 * created, for which lu_object_exists() returns
335 * false. This is used in a (frequent) case when
336 * objects are created as locking anchors or
337 * place holders for objects yet to be created.
339 result = PTR_ERR(inode);
343 obj->oo_inode = inode;
344 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
345 if (dev->od_iop_mode) {
346 obj->oo_compat_dot_created = 1;
347 obj->oo_compat_dotdot_created = 1;
350 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
353 LASSERT(obj->oo_hl_head == NULL);
354 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
355 if (obj->oo_hl_head == NULL) {
356 obj->oo_inode = NULL;
361 LINVRNT(osd_invariant(obj));
366 * Concurrency: shouldn't matter.
368 static void osd_object_init0(struct osd_object *obj)
370 LASSERT(obj->oo_inode != NULL);
371 obj->oo_dt.do_body_ops = &osd_body_ops;
372 obj->oo_dt.do_lu.lo_header->loh_attr |=
373 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
377 * Concurrency: no concurrent access is possible that early in object
380 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
381 const struct lu_object_conf *unused)
383 struct osd_object *obj = osd_obj(l);
386 LINVRNT(osd_invariant(obj));
388 result = osd_fid_lookup(env, obj, lu_object_fid(l));
389 obj->oo_dt.do_body_ops = &osd_body_ops_new;
391 if (obj->oo_inode != NULL)
392 osd_object_init0(obj);
394 LINVRNT(osd_invariant(obj));
399 * Concurrency: no concurrent access is possible that late in object
402 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
404 struct osd_object *obj = osd_obj(l);
406 LINVRNT(osd_invariant(obj));
408 dt_object_fini(&obj->oo_dt);
409 if (obj->oo_hl_head != NULL)
410 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
415 * Concurrency: no concurrent access is possible that late in object
418 static void osd_index_fini(struct osd_object *o)
420 struct iam_container *bag;
422 if (o->oo_dir != NULL) {
423 bag = &o->oo_dir->od_container;
424 if (o->oo_inode != NULL) {
425 if (bag->ic_object == o->oo_inode)
426 iam_container_fini(bag);
428 OBD_FREE_PTR(o->oo_dir);
434 * Concurrency: no concurrent access is possible that late in object
435 * life-cycle (for all existing callers, that is. New callers have to provide
436 * their own locking.)
438 static int osd_inode_unlinked(const struct inode *inode)
440 return inode->i_nlink == 0;
444 OSD_TXN_OI_DELETE_CREDITS = 20,
445 OSD_TXN_INODE_DELETE_CREDITS = 20
452 #if OSD_THANDLE_STATS
454 * Set time when the handle is allocated
456 static void osd_th_alloced(struct osd_thandle *oth)
458 oth->oth_alloced = cfs_time_current();
462 * Set time when the handle started
464 static void osd_th_started(struct osd_thandle *oth)
466 oth->oth_started = cfs_time_current();
470 * Helper function to convert time interval to microseconds packed in
471 * long int (default time units for the counter in "stats" initialized
472 * by lu_time_init() )
474 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
478 cfs_duration_usec(cfs_time_sub(end, start), &val);
479 return val.tv_sec * 1000000 + val.tv_usec;
483 * Check whether the we deal with this handle for too long.
485 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
486 cfs_time_t alloced, cfs_time_t started,
489 cfs_time_t now = cfs_time_current();
491 LASSERT(dev != NULL);
493 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
494 interval_to_usec(alloced, started));
495 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
496 interval_to_usec(started, closed));
497 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
498 interval_to_usec(closed, now));
500 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
501 CWARN("transaction handle %p was open for too long: "
503 "alloced "CFS_TIME_T" ,"
504 "started "CFS_TIME_T" ,"
505 "closed "CFS_TIME_T"\n",
506 oth, now, alloced, started, closed);
507 libcfs_debug_dumpstack(NULL);
511 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
513 cfs_time_t __closed = cfs_time_current(); \
514 cfs_time_t __alloced = oth->oth_alloced; \
515 cfs_time_t __started = oth->oth_started; \
518 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
521 #else /* OSD_THANDLE_STATS */
523 #define osd_th_alloced(h) do {} while(0)
524 #define osd_th_started(h) do {} while(0)
525 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
527 #endif /* OSD_THANDLE_STATS */
530 * Concurrency: doesn't access mutable data.
532 static int osd_param_is_sane(const struct osd_device *dev,
533 const struct thandle *th)
535 struct osd_thandle *oh;
536 oh = container_of0(th, struct osd_thandle, ot_super);
537 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
541 * Concurrency: shouldn't matter.
543 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
544 static void osd_trans_commit_cb(struct super_block *sb,
545 struct journal_callback *jcb, int error)
547 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
550 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
551 struct thandle *th = &oh->ot_super;
552 struct lu_device *lud = &th->th_dev->dd_lu_dev;
553 struct dt_txn_commit_cb *dcb, *tmp;
555 LASSERT(oh->ot_handle == NULL);
558 CERROR("transaction @0x%p commit error: %d\n", th, error);
560 dt_txn_hook_commit(th);
562 /* call per-transaction callbacks if any */
563 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage)
564 dcb->dcb_func(NULL, th, dcb, error);
566 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
570 lu_context_exit(&th->th_ctx);
571 lu_context_fini(&th->th_ctx);
575 static struct thandle *osd_trans_create(const struct lu_env *env,
578 struct osd_thread_info *oti = osd_oti_get(env);
579 struct osd_iobuf *iobuf = &oti->oti_iobuf;
580 struct osd_thandle *oh;
584 /* on pending IO in this thread should left from prev. request */
585 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
587 th = ERR_PTR(-ENOMEM);
588 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
593 th->th_tags = LCT_TX_HANDLE;
595 oti->oti_dev = osd_dt_dev(d);
596 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
603 * Concurrency: shouldn't matter.
605 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
608 struct osd_thread_info *oti = osd_oti_get(env);
609 struct osd_device *dev = osd_dt_dev(d);
611 struct osd_thandle *oh;
616 LASSERT(current->journal_info == NULL);
618 oh = container_of0(th, struct osd_thandle, ot_super);
620 LASSERT(oh->ot_handle == NULL);
622 rc = dt_txn_hook_start(env, d, th);
626 if (!osd_param_is_sane(dev, th)) {
627 CWARN("%s: too many transaction credits (%d > %d)\n",
628 d->dd_lu_dev.ld_obd->obd_name, oh->ot_credits,
629 osd_journal(dev)->j_max_transaction_buffers);
630 /* XXX Limit the credits to 'max_transaction_buffers', and
631 * let the underlying filesystem to catch the error if
632 * we really need so many credits.
634 * This should be removed when we can calculate the
635 * credits precisely. */
636 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
637 #ifdef OSD_TRACK_DECLARES
638 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
639 oh->ot_declare_attr_set, oh->ot_declare_punch,
640 oh->ot_declare_xattr_set);
641 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
642 oh->ot_declare_create, oh->ot_declare_ref_add,
643 oh->ot_declare_ref_del, oh->ot_declare_write);
644 CERROR(" insert: %d, delete: %d, destroy: %d\n",
645 oh->ot_declare_insert, oh->ot_declare_delete,
646 oh->ot_declare_destroy);
651 * XXX temporary stuff. Some abstraction layer should
654 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
658 LASSERT(oti->oti_txns == 0);
659 lu_context_init(&th->th_ctx, th->th_tags);
660 lu_context_enter(&th->th_ctx);
662 lu_device_get(&d->dd_lu_dev);
663 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
667 * XXX: current rule is that we first start tx,
668 * then lock object(s), but we can't use
669 * this rule for data (due to locking specifics
670 * in ldiskfs). also in long-term we'd like to
671 * use usually-used (locks;tx) ordering. so,
672 * UGLY thing is that we'll use one ordering for
673 * data (ofd) and reverse ordering for metadata
674 * (mdd). then at some point we'll fix the latter
676 if (lu_device_is_md(&d->dd_lu_dev)) {
677 LASSERT(oti->oti_r_locks == 0);
678 LASSERT(oti->oti_w_locks == 0);
691 * Concurrency: shouldn't matter.
693 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
696 struct osd_thandle *oh;
697 struct osd_thread_info *oti = osd_oti_get(env);
698 struct osd_iobuf *iobuf = &oti->oti_iobuf;
702 oh = container_of0(th, struct osd_thandle, ot_super);
704 if (oh->ot_handle != NULL) {
705 handle_t *hdl = oh->ot_handle;
707 hdl->h_sync = th->th_sync;
710 * add commit callback
711 * notice we don't do this in osd_trans_start()
712 * as underlying transaction can change during truncate
714 osd_journal_callback_set(hdl, osd_trans_commit_cb,
717 LASSERT(oti->oti_txns == 1);
720 * XXX: current rule is that we first start tx,
721 * then lock object(s), but we can't use
722 * this rule for data (due to locking specifics
723 * in ldiskfs). also in long-term we'd like to
724 * use usually-used (locks;tx) ordering. so,
725 * UGLY thing is that we'll use one ordering for
726 * data (ofd) and reverse ordering for metadata
727 * (mdd). then at some point we'll fix the latter
729 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
730 LASSERT(oti->oti_r_locks == 0);
731 LASSERT(oti->oti_w_locks == 0);
733 rc = dt_txn_hook_stop(env, th);
735 CERROR("Failure in transaction hook: %d\n", rc);
736 oh->ot_handle = NULL;
737 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
738 rc = ldiskfs_journal_stop(hdl));
740 CERROR("Failure to stop transaction: %d\n", rc);
745 /* as we want IO to journal and data IO be concurrent, we don't block
746 * awaiting data IO completion in osd_do_bio(), instead we wait here
747 * once transaction is submitted to the journal. all reqular requests
748 * don't do direct IO (except read/write), thus this wait_event becomes
751 * IMPORTANT: we have to wait till any IO submited by the thread is
752 * completed otherwise iobuf may be corrupted by different request
754 cfs_wait_event(iobuf->dr_wait, cfs_atomic_read(&iobuf->dr_numreqs)==0);
756 rc = iobuf->dr_error;
761 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
763 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
766 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
772 * Called just before object is freed. Releases all resources except for
773 * object itself (that is released by osd_object_free()).
775 * Concurrency: no concurrent access is possible that late in object
778 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
780 struct osd_object *obj = osd_obj(l);
781 struct inode *inode = obj->oo_inode;
783 LINVRNT(osd_invariant(obj));
786 * If object is unlinked remove fid->ino mapping from object index.
792 obj->oo_inode = NULL;
797 * Concurrency: ->loo_object_release() is called under site spin-lock.
799 static void osd_object_release(const struct lu_env *env,
805 * Concurrency: shouldn't matter.
807 static int osd_object_print(const struct lu_env *env, void *cookie,
808 lu_printer_t p, const struct lu_object *l)
810 struct osd_object *o = osd_obj(l);
813 if (o->oo_dir != NULL)
814 d = o->oo_dir->od_container.ic_descr;
817 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
819 o->oo_inode ? o->oo_inode->i_ino : 0UL,
820 o->oo_inode ? o->oo_inode->i_generation : 0,
821 d ? d->id_ops->id_name : "plain");
825 * Concurrency: shouldn't matter.
827 int osd_statfs(const struct lu_env *env, struct dt_device *d,
830 struct osd_device *osd = osd_dt_dev(d);
831 struct super_block *sb = osd_sb(osd);
834 cfs_spin_lock(&osd->od_osfs_lock);
836 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
837 result = ll_do_statfs(sb, &osd->od_kstatfs);
838 if (likely(result == 0)) /* N.B. statfs can't really fail */
839 osd->od_osfs_age = cfs_time_current_64();
842 if (likely(result == 0))
843 *sfs = osd->od_kstatfs;
844 cfs_spin_unlock(&osd->od_osfs_lock);
850 * Concurrency: doesn't access mutable data.
852 static void osd_conf_get(const struct lu_env *env,
853 const struct dt_device *dev,
854 struct dt_device_param *param)
856 struct super_block *sb = osd_sb(osd_dt_dev(dev));
859 * XXX should be taken from not-yet-existing fs abstraction layer.
861 param->ddp_max_name_len = LDISKFS_NAME_LEN;
862 param->ddp_max_nlink = LDISKFS_LINK_MAX;
863 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
864 param->ddp_mntopts = 0;
865 if (test_opt(sb, XATTR_USER))
866 param->ddp_mntopts |= MNTOPT_USERXATTR;
867 if (test_opt(sb, POSIX_ACL))
868 param->ddp_mntopts |= MNTOPT_ACL;
870 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
871 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
872 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
875 param->ddp_max_ea_size = sb->s_blocksize;
880 * Helper function to get and fill the buffer with input values.
882 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
886 buf = &osd_oti_get(env)->oti_buf;
893 * Concurrency: shouldn't matter.
895 static int osd_sync(const struct lu_env *env, struct dt_device *d)
897 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
898 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
902 * Start commit for OSD device.
904 * An implementation of dt_commit_async method for OSD device.
905 * Asychronously starts underlayng fs sync and thereby a transaction
908 * \param env environment
911 * \see dt_device_operations
913 static int osd_commit_async(const struct lu_env *env,
916 struct super_block *s = osd_sb(osd_dt_dev(d));
919 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
920 RETURN(s->s_op->sync_fs(s, 0));
924 * Concurrency: shouldn't matter.
927 static void osd_ro(const struct lu_env *env, struct dt_device *d)
929 struct super_block *sb = osd_sb(osd_dt_dev(d));
932 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
934 __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
939 * Concurrency: serialization provided by callers.
941 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
942 int mode, unsigned long timeout, __u32 alg,
943 struct lustre_capa_key *keys)
945 struct osd_device *dev = osd_dt_dev(d);
948 dev->od_fl_capa = mode;
949 dev->od_capa_timeout = timeout;
950 dev->od_capa_alg = alg;
951 dev->od_capa_keys = keys;
956 * Concurrency: serialization provided by callers.
958 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
959 struct dt_quota_ctxt *ctxt, void *data)
961 struct obd_device *obd = (void *)ctxt;
962 struct vfsmount *mnt = (struct vfsmount *)data;
965 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
966 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
967 obd->obd_lvfs_ctxt.pwdmnt = mnt;
968 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
969 obd->obd_lvfs_ctxt.fs = get_ds();
975 * Note: we do not count into QUOTA here.
976 * If we mount with --data_journal we may need more.
978 const int osd_dto_credits_noquota[DTO_NR] = {
981 * INDEX_EXTRA_TRANS_BLOCKS(8) +
982 * SINGLEDATA_TRANS_BLOCKS(8)
983 * XXX Note: maybe iam need more, since iam have more level than
986 [DTO_INDEX_INSERT] = 16,
987 [DTO_INDEX_DELETE] = 16,
991 [DTO_INDEX_UPDATE] = 16,
993 * Create a object. The same as create object in EXT3.
994 * DATA_TRANS_BLOCKS(14) +
995 * INDEX_EXTRA_BLOCKS(8) +
996 * 3(inode bits, groups, GDT)
998 [DTO_OBJECT_CREATE] = 25,
1000 * XXX: real credits to be fixed
1002 [DTO_OBJECT_DELETE] = 25,
1004 * Attr set credits (inode)
1006 [DTO_ATTR_SET_BASE] = 1,
1008 * Xattr set. The same as xattr of EXT3.
1009 * DATA_TRANS_BLOCKS(14)
1010 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1011 * are also counted in. Do not know why?
1013 [DTO_XATTR_SET] = 14,
1016 * credits for inode change during write.
1018 [DTO_WRITE_BASE] = 3,
1020 * credits for single block write.
1022 [DTO_WRITE_BLOCK] = 14,
1024 * Attr set credits for chown.
1025 * This is extra credits for setattr, and it is null without quota
1027 [DTO_ATTR_SET_CHOWN]= 0
1030 static const struct dt_device_operations osd_dt_ops = {
1031 .dt_root_get = osd_root_get,
1032 .dt_statfs = osd_statfs,
1033 .dt_trans_create = osd_trans_create,
1034 .dt_trans_start = osd_trans_start,
1035 .dt_trans_stop = osd_trans_stop,
1036 .dt_trans_cb_add = osd_trans_cb_add,
1037 .dt_conf_get = osd_conf_get,
1038 .dt_sync = osd_sync,
1040 .dt_commit_async = osd_commit_async,
1041 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1042 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1045 static void osd_object_read_lock(const struct lu_env *env,
1046 struct dt_object *dt, unsigned role)
1048 struct osd_object *obj = osd_dt_obj(dt);
1049 struct osd_thread_info *oti = osd_oti_get(env);
1051 LINVRNT(osd_invariant(obj));
1053 LASSERT(obj->oo_owner != env);
1054 cfs_down_read_nested(&obj->oo_sem, role);
1056 LASSERT(obj->oo_owner == NULL);
1060 static void osd_object_write_lock(const struct lu_env *env,
1061 struct dt_object *dt, unsigned role)
1063 struct osd_object *obj = osd_dt_obj(dt);
1064 struct osd_thread_info *oti = osd_oti_get(env);
1066 LINVRNT(osd_invariant(obj));
1068 LASSERT(obj->oo_owner != env);
1069 cfs_down_write_nested(&obj->oo_sem, role);
1071 LASSERT(obj->oo_owner == NULL);
1072 obj->oo_owner = env;
1076 static void osd_object_read_unlock(const struct lu_env *env,
1077 struct dt_object *dt)
1079 struct osd_object *obj = osd_dt_obj(dt);
1080 struct osd_thread_info *oti = osd_oti_get(env);
1082 LINVRNT(osd_invariant(obj));
1084 LASSERT(oti->oti_r_locks > 0);
1086 cfs_up_read(&obj->oo_sem);
1089 static void osd_object_write_unlock(const struct lu_env *env,
1090 struct dt_object *dt)
1092 struct osd_object *obj = osd_dt_obj(dt);
1093 struct osd_thread_info *oti = osd_oti_get(env);
1095 LINVRNT(osd_invariant(obj));
1097 LASSERT(obj->oo_owner == env);
1098 LASSERT(oti->oti_w_locks > 0);
1100 obj->oo_owner = NULL;
1101 cfs_up_write(&obj->oo_sem);
1104 static int osd_object_write_locked(const struct lu_env *env,
1105 struct dt_object *dt)
1107 struct osd_object *obj = osd_dt_obj(dt);
1109 LINVRNT(osd_invariant(obj));
1111 return obj->oo_owner == env;
1114 static int capa_is_sane(const struct lu_env *env,
1115 struct osd_device *dev,
1116 struct lustre_capa *capa,
1117 struct lustre_capa_key *keys)
1119 struct osd_thread_info *oti = osd_oti_get(env);
1120 struct lustre_capa *tcapa = &oti->oti_capa;
1121 struct obd_capa *oc;
1125 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1127 if (capa_is_expired(oc)) {
1128 DEBUG_CAPA(D_ERROR, capa, "expired");
1135 if (capa_is_expired_sec(capa)) {
1136 DEBUG_CAPA(D_ERROR, capa, "expired");
1140 cfs_spin_lock(&capa_lock);
1141 for (i = 0; i < 2; i++) {
1142 if (keys[i].lk_keyid == capa->lc_keyid) {
1143 oti->oti_capa_key = keys[i];
1147 cfs_spin_unlock(&capa_lock);
1150 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1154 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1158 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1159 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1163 oc = capa_add(dev->od_capa_hash, capa);
1169 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1170 struct lustre_capa *capa, __u64 opc)
1172 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1173 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1174 struct md_capainfo *ci;
1177 if (!dev->od_fl_capa)
1180 if (capa == BYPASS_CAPA)
1183 ci = md_capainfo(env);
1187 if (ci->mc_auth == LC_ID_NONE)
1191 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1195 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1196 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1201 if (!capa_opc_supported(capa, opc)) {
1202 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1206 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1207 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1214 static struct timespec *osd_inode_time(const struct lu_env *env,
1215 struct inode *inode, __u64 seconds)
1217 struct osd_thread_info *oti = osd_oti_get(env);
1218 struct timespec *t = &oti->oti_time;
1220 t->tv_sec = seconds;
1222 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1227 static void osd_inode_getattr(const struct lu_env *env,
1228 struct inode *inode, struct lu_attr *attr)
1230 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1231 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1232 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1234 attr->la_atime = LTIME_S(inode->i_atime);
1235 attr->la_mtime = LTIME_S(inode->i_mtime);
1236 attr->la_ctime = LTIME_S(inode->i_ctime);
1237 attr->la_mode = inode->i_mode;
1238 attr->la_size = i_size_read(inode);
1239 attr->la_blocks = inode->i_blocks;
1240 attr->la_uid = inode->i_uid;
1241 attr->la_gid = inode->i_gid;
1242 attr->la_flags = LDISKFS_I(inode)->i_flags;
1243 attr->la_nlink = inode->i_nlink;
1244 attr->la_rdev = inode->i_rdev;
1245 attr->la_blksize = ll_inode_blksize(inode);
1246 attr->la_blkbits = inode->i_blkbits;
1249 static int osd_attr_get(const struct lu_env *env,
1250 struct dt_object *dt,
1251 struct lu_attr *attr,
1252 struct lustre_capa *capa)
1254 struct osd_object *obj = osd_dt_obj(dt);
1256 LASSERT(dt_object_exists(dt));
1257 LINVRNT(osd_invariant(obj));
1259 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1262 cfs_spin_lock(&obj->oo_guard);
1263 osd_inode_getattr(env, obj->oo_inode, attr);
1264 cfs_spin_unlock(&obj->oo_guard);
1268 static int osd_declare_attr_set(const struct lu_env *env,
1269 struct dt_object *dt,
1270 const struct lu_attr *attr,
1271 struct thandle *handle)
1273 struct osd_thandle *oh;
1274 struct osd_object *obj;
1276 LASSERT(dt != NULL);
1277 LASSERT(handle != NULL);
1279 obj = osd_dt_obj(dt);
1280 LASSERT(osd_invariant(obj));
1282 oh = container_of0(handle, struct osd_thandle, ot_super);
1283 LASSERT(oh->ot_handle == NULL);
1285 OSD_DECLARE_OP(oh, attr_set);
1286 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1288 if (attr && attr->la_valid & LA_UID) {
1290 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1292 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1294 if (attr && attr->la_valid & LA_GID) {
1296 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1298 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1304 static int osd_inode_setattr(const struct lu_env *env,
1305 struct inode *inode, const struct lu_attr *attr)
1309 bits = attr->la_valid;
1311 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1313 #ifdef HAVE_QUOTA_SUPPORT
1314 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1315 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1316 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1322 iattr.ia_valid |= ATTR_UID;
1324 iattr.ia_valid |= ATTR_GID;
1325 iattr.ia_uid = attr->la_uid;
1326 iattr.ia_gid = attr->la_gid;
1327 osd_push_ctxt(env, save);
1328 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1335 if (bits & LA_ATIME)
1336 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1337 if (bits & LA_CTIME)
1338 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1339 if (bits & LA_MTIME)
1340 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1341 if (bits & LA_SIZE) {
1342 LDISKFS_I(inode)->i_disksize = attr->la_size;
1343 i_size_write(inode, attr->la_size);
1347 /* OSD should not change "i_blocks" which is used by quota.
1348 * "i_blocks" should be changed by ldiskfs only. */
1349 if (bits & LA_BLOCKS)
1350 inode->i_blocks = attr->la_blocks;
1353 inode->i_mode = (inode->i_mode & S_IFMT) |
1354 (attr->la_mode & ~S_IFMT);
1356 inode->i_uid = attr->la_uid;
1358 inode->i_gid = attr->la_gid;
1359 if (bits & LA_NLINK)
1360 inode->i_nlink = attr->la_nlink;
1362 inode->i_rdev = attr->la_rdev;
1364 if (bits & LA_FLAGS) {
1365 /* always keep S_NOCMTIME */
1366 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1372 static int osd_attr_set(const struct lu_env *env,
1373 struct dt_object *dt,
1374 const struct lu_attr *attr,
1375 struct thandle *handle,
1376 struct lustre_capa *capa)
1378 struct osd_object *obj = osd_dt_obj(dt);
1381 LASSERT(handle != NULL);
1382 LASSERT(dt_object_exists(dt));
1383 LASSERT(osd_invariant(obj));
1385 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1388 OSD_EXEC_OP(handle, attr_set);
1390 cfs_spin_lock(&obj->oo_guard);
1391 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1392 cfs_spin_unlock(&obj->oo_guard);
1395 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1402 * XXX temporary solution.
1404 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1405 struct lu_attr *attr, struct thandle *th)
1410 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1411 struct lu_attr *attr, struct thandle *th)
1413 osd_object_init0(obj);
1414 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1415 unlock_new_inode(obj->oo_inode);
1419 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1420 struct osd_object *obj,
1421 const char *name, const int namelen)
1423 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1426 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1428 struct dt_allocation_hint *hint,
1432 struct osd_device *osd = osd_obj2dev(obj);
1433 struct osd_thandle *oth;
1434 struct dt_object *parent;
1435 struct inode *inode;
1436 #ifdef HAVE_QUOTA_SUPPORT
1437 struct osd_ctxt *save = &info->oti_ctxt;
1440 LINVRNT(osd_invariant(obj));
1441 LASSERT(obj->oo_inode == NULL);
1442 LASSERT(obj->oo_hl_head == NULL);
1444 if (S_ISDIR(mode) && ldiskfs_pdo) {
1445 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1446 if (obj->oo_hl_head == NULL)
1450 oth = container_of(th, struct osd_thandle, ot_super);
1451 LASSERT(oth->ot_handle->h_transaction != NULL);
1453 if (hint && hint->dah_parent)
1454 parent = hint->dah_parent;
1456 parent = osd->od_obj_area;
1458 #ifdef HAVE_QUOTA_SUPPORT
1459 osd_push_ctxt(info->oti_env, save);
1461 inode = ldiskfs_create_inode(oth->ot_handle,
1462 parent ? osd_dt_obj(parent)->oo_inode :
1463 osd_sb(osd)->s_root->d_inode,
1465 #ifdef HAVE_QUOTA_SUPPORT
1468 if (!IS_ERR(inode)) {
1469 /* Do not update file c/mtime in ldiskfs.
1470 * NB: don't need any lock because no contention at this
1472 inode->i_flags |= S_NOCMTIME;
1473 obj->oo_inode = inode;
1476 if (obj->oo_hl_head != NULL) {
1477 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1478 obj->oo_hl_head = NULL;
1480 result = PTR_ERR(inode);
1482 LINVRNT(osd_invariant(obj));
1490 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1491 struct lu_attr *attr,
1492 struct dt_allocation_hint *hint,
1493 struct dt_object_format *dof,
1497 struct osd_thandle *oth;
1498 struct osd_device *osd = osd_obj2dev(obj);
1499 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1501 LASSERT(S_ISDIR(attr->la_mode));
1503 oth = container_of(th, struct osd_thandle, ot_super);
1504 LASSERT(oth->ot_handle->h_transaction != NULL);
1505 result = osd_mkfile(info, obj, mode, hint, th);
1506 if (result == 0 && osd->od_iop_mode == 0) {
1507 LASSERT(obj->oo_inode != NULL);
1509 * XXX uh-oh... call low-level iam function directly.
1512 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1513 sizeof (struct osd_fid_pack),
1519 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1520 struct lu_attr *attr,
1521 struct dt_allocation_hint *hint,
1522 struct dt_object_format *dof,
1526 struct osd_thandle *oth;
1527 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1529 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1531 LASSERT(S_ISREG(attr->la_mode));
1533 oth = container_of(th, struct osd_thandle, ot_super);
1534 LASSERT(oth->ot_handle->h_transaction != NULL);
1536 result = osd_mkfile(info, obj, mode, hint, th);
1538 LASSERT(obj->oo_inode != NULL);
1539 if (feat->dif_flags & DT_IND_VARKEY)
1540 result = iam_lvar_create(obj->oo_inode,
1541 feat->dif_keysize_max,
1543 feat->dif_recsize_max,
1546 result = iam_lfix_create(obj->oo_inode,
1547 feat->dif_keysize_max,
1549 feat->dif_recsize_max,
1556 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1557 struct lu_attr *attr,
1558 struct dt_allocation_hint *hint,
1559 struct dt_object_format *dof,
1562 LASSERT(S_ISREG(attr->la_mode));
1563 return osd_mkfile(info, obj, (attr->la_mode &
1564 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1567 static int osd_mksym(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_ISLNK(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_mknod(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 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1587 LINVRNT(osd_invariant(obj));
1588 LASSERT(obj->oo_inode == NULL);
1589 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1590 S_ISFIFO(mode) || S_ISSOCK(mode));
1592 result = osd_mkfile(info, obj, mode, hint, th);
1594 LASSERT(obj->oo_inode != NULL);
1595 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1597 LINVRNT(osd_invariant(obj));
1601 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1603 struct dt_allocation_hint *hint,
1604 struct dt_object_format *dof,
1607 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1609 osd_obj_type_f result;
1625 result = osd_mk_index;
1636 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1637 struct dt_object *parent, cfs_umode_t child_mode)
1641 memset(ah, 0, sizeof(*ah));
1642 ah->dah_parent = parent;
1643 ah->dah_mode = child_mode;
1647 * Helper function for osd_object_create()
1649 * \retval 0, on success
1651 static int __osd_object_create(struct osd_thread_info *info,
1652 struct osd_object *obj, struct lu_attr *attr,
1653 struct dt_allocation_hint *hint,
1654 struct dt_object_format *dof,
1660 result = osd_create_pre(info, obj, attr, th);
1662 result = osd_create_type_f(dof->dof_type)(info, obj,
1663 attr, hint, dof, th);
1665 result = osd_create_post(info, obj, attr, th);
1671 * Helper function for osd_object_create()
1673 * \retval 0, on success
1675 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1676 const struct lu_fid *fid, struct thandle *th)
1678 struct osd_thread_info *info = osd_oti_get(env);
1679 struct osd_inode_id *id = &info->oti_id;
1680 struct osd_device *osd = osd_obj2dev(obj);
1681 struct md_ucred *uc = md_ucred(env);
1683 LASSERT(obj->oo_inode != NULL);
1684 LASSERT(uc != NULL);
1686 id->oii_ino = obj->oo_inode->i_ino;
1687 id->oii_gen = obj->oo_inode->i_generation;
1689 return osd_oi_insert(info, osd, fid, id, th,
1690 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1693 static int osd_declare_object_create(const struct lu_env *env,
1694 struct dt_object *dt,
1695 struct lu_attr *attr,
1696 struct dt_allocation_hint *hint,
1697 struct dt_object_format *dof,
1698 struct thandle *handle)
1700 struct osd_thandle *oh;
1702 LASSERT(handle != NULL);
1704 oh = container_of0(handle, struct osd_thandle, ot_super);
1705 LASSERT(oh->ot_handle == NULL);
1707 OSD_DECLARE_OP(oh, create);
1708 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1709 /* XXX: So far, only normal fid needs be inserted into the oi,
1710 * things could be changed later. Revise following code then. */
1711 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1712 OSD_DECLARE_OP(oh, insert);
1713 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1715 /* If this is directory, then we expect . and .. to be inserted as
1716 * well. The one directory block always needs to be created for the
1717 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1718 * block), there is no danger of needing a tree for the first block.
1720 if (attr && S_ISDIR(attr->la_mode)) {
1721 OSD_DECLARE_OP(oh, insert);
1722 OSD_DECLARE_OP(oh, insert);
1723 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1727 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1728 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1733 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1734 struct lu_attr *attr,
1735 struct dt_allocation_hint *hint,
1736 struct dt_object_format *dof,
1739 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1740 struct osd_object *obj = osd_dt_obj(dt);
1741 struct osd_thread_info *info = osd_oti_get(env);
1746 LINVRNT(osd_invariant(obj));
1747 LASSERT(!dt_object_exists(dt));
1748 LASSERT(osd_write_locked(env, obj));
1749 LASSERT(th != NULL);
1751 OSD_EXEC_OP(th, create);
1753 result = __osd_object_create(info, obj, attr, hint, dof, th);
1755 result = __osd_oi_insert(env, obj, fid, th);
1757 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1758 LASSERT(osd_invariant(obj));
1763 * Called to destroy on-disk representation of the object
1765 * Concurrency: must be locked
1767 static int osd_declare_object_destroy(const struct lu_env *env,
1768 struct dt_object *dt,
1771 struct osd_object *obj = osd_dt_obj(dt);
1772 struct inode *inode = obj->oo_inode;
1773 struct osd_thandle *oh;
1777 oh = container_of0(th, struct osd_thandle, ot_super);
1778 LASSERT(oh->ot_handle == NULL);
1781 OSD_DECLARE_OP(oh, destroy);
1782 OSD_DECLARE_OP(oh, delete);
1783 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
1784 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
1786 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
1787 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
1792 static int osd_object_destroy(const struct lu_env *env,
1793 struct dt_object *dt,
1796 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1797 struct osd_object *obj = osd_dt_obj(dt);
1798 struct inode *inode = obj->oo_inode;
1799 struct osd_device *osd = osd_obj2dev(obj);
1800 struct osd_thandle *oh;
1804 oh = container_of0(th, struct osd_thandle, ot_super);
1805 LASSERT(oh->ot_handle);
1807 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1809 if (S_ISDIR(inode->i_mode)) {
1810 LASSERT(osd_inode_unlinked(inode) ||
1811 inode->i_nlink == 1);
1812 cfs_spin_lock(&obj->oo_guard);
1814 cfs_spin_unlock(&obj->oo_guard);
1815 inode->i_sb->s_op->dirty_inode(inode);
1817 LASSERT(osd_inode_unlinked(inode));
1820 OSD_EXEC_OP(th, destroy);
1822 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
1824 /* XXX: add to ext3 orphan list */
1825 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
1827 /* not needed in the cache anymore */
1828 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1834 * Helper function for osd_xattr_set()
1836 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1837 const struct lu_buf *buf, const char *name, int fl)
1839 struct osd_object *obj = osd_dt_obj(dt);
1840 struct inode *inode = obj->oo_inode;
1841 struct osd_thread_info *info = osd_oti_get(env);
1842 struct dentry *dentry = &info->oti_child_dentry;
1846 LASSERT(dt_object_exists(dt));
1847 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1848 LASSERT(osd_write_locked(env, obj));
1850 if (fl & LU_XATTR_REPLACE)
1851 fs_flags |= XATTR_REPLACE;
1853 if (fl & LU_XATTR_CREATE)
1854 fs_flags |= XATTR_CREATE;
1856 dentry->d_inode = inode;
1857 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1858 buf->lb_len, fs_flags);
1863 * Put the fid into lustre_mdt_attrs, and then place the structure
1864 * inode's ea. This fid should not be altered during the life time
1867 * \retval +ve, on success
1868 * \retval -ve, on error
1870 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1872 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1873 const struct lu_fid *fid)
1875 struct osd_thread_info *info = osd_oti_get(env);
1876 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1878 lustre_lma_init(mdt_attrs, fid);
1879 lustre_lma_swab(mdt_attrs);
1880 return __osd_xattr_set(env, dt,
1881 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1882 XATTR_NAME_LMA, LU_XATTR_CREATE);
1887 * Helper function to form igif
1889 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1892 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1896 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1897 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1898 * To have compatilibility with 1.8 ldiskfs driver we need to have
1899 * magic number at start of fid data.
1900 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1903 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1904 const struct dt_rec *fid)
1906 param->edp_magic = LDISKFS_LUFID_MAGIC;
1907 param->edp_len = sizeof(struct lu_fid) + 1;
1909 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1910 (struct lu_fid *)fid);
1914 * Try to read the fid from inode ea into dt_rec, if return value
1915 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1917 * \param fid object fid.
1919 * \retval 0 on success
1921 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1922 __u32 ino, struct lu_fid *fid)
1924 struct osd_thread_info *info = osd_oti_get(env);
1925 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1926 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1927 struct dentry *dentry = &info->oti_child_dentry;
1928 struct osd_inode_id *id = &info->oti_id;
1929 struct osd_device *dev;
1930 struct inode *inode;
1934 dev = osd_dev(ldev);
1937 id->oii_gen = OSD_OII_NOGEN;
1939 inode = osd_iget(info, dev, id);
1940 if (IS_ERR(inode)) {
1941 rc = PTR_ERR(inode);
1944 dentry->d_inode = inode;
1946 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1947 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1950 /* Check LMA compatibility */
1952 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
1953 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1954 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
1955 ~LMA_INCOMPAT_SUPP);
1960 lustre_lma_swab(mdt_attrs);
1961 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
1963 } else if (rc == -ENODATA) {
1964 osd_igif_get(env, inode, fid);
1973 * OSD layer object create function for interoperability mode (b11826).
1974 * This is mostly similar to osd_object_create(). Only difference being, fid is
1975 * inserted into inode ea here.
1977 * \retval 0, on success
1978 * \retval -ve, on error
1980 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1981 struct lu_attr *attr,
1982 struct dt_allocation_hint *hint,
1983 struct dt_object_format *dof,
1986 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1987 struct osd_object *obj = osd_dt_obj(dt);
1988 struct osd_thread_info *info = osd_oti_get(env);
1993 LASSERT(osd_invariant(obj));
1994 LASSERT(!dt_object_exists(dt));
1995 LASSERT(osd_write_locked(env, obj));
1996 LASSERT(th != NULL);
1998 OSD_EXEC_OP(th, create);
2000 result = __osd_object_create(info, obj, attr, hint, dof, th);
2002 /* objects under osd root shld have igif fid, so dont add fid EA */
2003 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2004 result = osd_ea_fid_set(env, dt, fid);
2007 result = __osd_oi_insert(env, obj, fid, th);
2009 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2010 LINVRNT(osd_invariant(obj));
2014 static int osd_declare_object_ref_add(const struct lu_env *env,
2015 struct dt_object *dt,
2016 struct thandle *handle)
2018 struct osd_thandle *oh;
2020 /* it's possible that object doesn't exist yet */
2021 LASSERT(handle != NULL);
2023 oh = container_of0(handle, struct osd_thandle, ot_super);
2024 LASSERT(oh->ot_handle == NULL);
2026 OSD_DECLARE_OP(oh, ref_add);
2027 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2033 * Concurrency: @dt is write locked.
2035 static int osd_object_ref_add(const struct lu_env *env,
2036 struct dt_object *dt, struct thandle *th)
2038 struct osd_object *obj = osd_dt_obj(dt);
2039 struct inode *inode = obj->oo_inode;
2041 LINVRNT(osd_invariant(obj));
2042 LASSERT(dt_object_exists(dt));
2043 LASSERT(osd_write_locked(env, obj));
2044 LASSERT(th != NULL);
2046 OSD_EXEC_OP(th, ref_add);
2049 * DIR_NLINK feature is set for compatibility reasons if:
2050 * 1) nlinks > LDISKFS_LINK_MAX, or
2051 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2053 * It is easier to always set this flag (rather than check and set),
2054 * since it has less overhead, and the superblock will be dirtied
2055 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2056 * do not actually care whether this flag is set or not.
2058 cfs_spin_lock(&obj->oo_guard);
2060 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2061 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2062 inode->i_nlink == 2)
2065 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
2066 cfs_spin_unlock(&obj->oo_guard);
2067 inode->i_sb->s_op->dirty_inode(inode);
2068 LINVRNT(osd_invariant(obj));
2073 static int osd_declare_object_ref_del(const struct lu_env *env,
2074 struct dt_object *dt,
2075 struct thandle *handle)
2077 struct osd_thandle *oh;
2079 LASSERT(dt_object_exists(dt));
2080 LASSERT(handle != NULL);
2082 oh = container_of0(handle, struct osd_thandle, ot_super);
2083 LASSERT(oh->ot_handle == NULL);
2085 OSD_DECLARE_OP(oh, ref_del);
2086 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2092 * Concurrency: @dt is write locked.
2094 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2097 struct osd_object *obj = osd_dt_obj(dt);
2098 struct inode *inode = obj->oo_inode;
2100 LINVRNT(osd_invariant(obj));
2101 LASSERT(dt_object_exists(dt));
2102 LASSERT(osd_write_locked(env, obj));
2103 LASSERT(th != NULL);
2105 OSD_EXEC_OP(th, ref_del);
2107 cfs_spin_lock(&obj->oo_guard);
2108 LASSERT(inode->i_nlink > 0);
2110 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2111 * then the nlink count is 1. Don't let it be set to 0 or the directory
2112 * inode will be deleted incorrectly. */
2113 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2115 cfs_spin_unlock(&obj->oo_guard);
2116 inode->i_sb->s_op->dirty_inode(inode);
2117 LINVRNT(osd_invariant(obj));
2123 * Get the 64-bit version for an inode.
2125 static int osd_object_version_get(const struct lu_env *env,
2126 struct dt_object *dt, dt_obj_version_t *ver)
2128 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2130 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2131 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2132 *ver = LDISKFS_I(inode)->i_fs_version;
2137 * Concurrency: @dt is read locked.
2139 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2140 struct lu_buf *buf, const char *name,
2141 struct lustre_capa *capa)
2143 struct osd_object *obj = osd_dt_obj(dt);
2144 struct inode *inode = obj->oo_inode;
2145 struct osd_thread_info *info = osd_oti_get(env);
2146 struct dentry *dentry = &info->oti_obj_dentry;
2148 /* version get is not real XATTR but uses xattr API */
2149 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2150 /* for version we are just using xattr API but change inode
2152 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2153 osd_object_version_get(env, dt, buf->lb_buf);
2154 return sizeof(dt_obj_version_t);
2157 LASSERT(dt_object_exists(dt));
2158 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2159 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2161 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2164 dentry->d_inode = inode;
2165 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2169 static int osd_declare_xattr_set(const struct lu_env *env,
2170 struct dt_object *dt,
2171 const struct lu_buf *buf, const char *name,
2172 int fl, struct thandle *handle)
2174 struct osd_thandle *oh;
2176 LASSERT(handle != NULL);
2178 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2179 /* no credits for version */
2183 oh = container_of0(handle, struct osd_thandle, ot_super);
2184 LASSERT(oh->ot_handle == NULL);
2186 OSD_DECLARE_OP(oh, xattr_set);
2187 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2193 * Set the 64-bit version for object
2195 static void osd_object_version_set(const struct lu_env *env,
2196 struct dt_object *dt,
2197 dt_obj_version_t *new_version)
2199 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2201 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2202 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2204 LDISKFS_I(inode)->i_fs_version = *new_version;
2205 /** Version is set after all inode operations are finished,
2206 * so we should mark it dirty here */
2207 inode->i_sb->s_op->dirty_inode(inode);
2211 * Concurrency: @dt is write locked.
2213 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2214 const struct lu_buf *buf, const char *name, int fl,
2215 struct thandle *handle, struct lustre_capa *capa)
2217 LASSERT(handle != NULL);
2219 /* version set is not real XATTR */
2220 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2221 /* for version we are just using xattr API but change inode
2223 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2224 osd_object_version_set(env, dt, buf->lb_buf);
2225 return sizeof(dt_obj_version_t);
2228 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2231 OSD_EXEC_OP(handle, xattr_set);
2232 return __osd_xattr_set(env, dt, buf, name, fl);
2236 * Concurrency: @dt is read locked.
2238 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2239 struct lu_buf *buf, struct lustre_capa *capa)
2241 struct osd_object *obj = osd_dt_obj(dt);
2242 struct inode *inode = obj->oo_inode;
2243 struct osd_thread_info *info = osd_oti_get(env);
2244 struct dentry *dentry = &info->oti_obj_dentry;
2246 LASSERT(dt_object_exists(dt));
2247 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2248 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2250 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2253 dentry->d_inode = inode;
2254 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2257 static int osd_declare_xattr_del(const struct lu_env *env,
2258 struct dt_object *dt, const char *name,
2259 struct thandle *handle)
2261 struct osd_thandle *oh;
2263 LASSERT(dt_object_exists(dt));
2264 LASSERT(handle != NULL);
2266 oh = container_of0(handle, struct osd_thandle, ot_super);
2267 LASSERT(oh->ot_handle == NULL);
2269 OSD_DECLARE_OP(oh, xattr_set);
2270 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2276 * Concurrency: @dt is write locked.
2278 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2279 const char *name, struct thandle *handle,
2280 struct lustre_capa *capa)
2282 struct osd_object *obj = osd_dt_obj(dt);
2283 struct inode *inode = obj->oo_inode;
2284 struct osd_thread_info *info = osd_oti_get(env);
2285 struct dentry *dentry = &info->oti_obj_dentry;
2288 LASSERT(dt_object_exists(dt));
2289 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2290 LASSERT(osd_write_locked(env, obj));
2291 LASSERT(handle != NULL);
2293 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2296 OSD_EXEC_OP(handle, xattr_set);
2298 dentry->d_inode = inode;
2299 rc = inode->i_op->removexattr(dentry, name);
2303 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2304 struct dt_object *dt,
2305 struct lustre_capa *old,
2308 struct osd_thread_info *info = osd_oti_get(env);
2309 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2310 struct osd_object *obj = osd_dt_obj(dt);
2311 struct osd_device *dev = osd_obj2dev(obj);
2312 struct lustre_capa_key *key = &info->oti_capa_key;
2313 struct lustre_capa *capa = &info->oti_capa;
2314 struct obd_capa *oc;
2315 struct md_capainfo *ci;
2319 if (!dev->od_fl_capa)
2320 RETURN(ERR_PTR(-ENOENT));
2322 LASSERT(dt_object_exists(dt));
2323 LINVRNT(osd_invariant(obj));
2325 /* renewal sanity check */
2326 if (old && osd_object_auth(env, dt, old, opc))
2327 RETURN(ERR_PTR(-EACCES));
2329 ci = md_capainfo(env);
2331 RETURN(ERR_PTR(-ENOENT));
2333 switch (ci->mc_auth) {
2337 capa->lc_uid = obj->oo_inode->i_uid;
2338 capa->lc_gid = obj->oo_inode->i_gid;
2339 capa->lc_flags = LC_ID_PLAIN;
2341 case LC_ID_CONVERT: {
2344 s[0] = obj->oo_inode->i_uid;
2345 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2346 s[2] = obj->oo_inode->i_gid;
2347 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2348 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2350 RETURN(ERR_PTR(rc));
2352 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2353 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2354 capa->lc_flags = LC_ID_CONVERT;
2358 RETURN(ERR_PTR(-EINVAL));
2361 capa->lc_fid = *fid;
2363 capa->lc_flags |= dev->od_capa_alg << 24;
2364 capa->lc_timeout = dev->od_capa_timeout;
2365 capa->lc_expiry = 0;
2367 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2369 LASSERT(!capa_is_expired(oc));
2373 cfs_spin_lock(&capa_lock);
2374 *key = dev->od_capa_keys[1];
2375 cfs_spin_unlock(&capa_lock);
2377 capa->lc_keyid = key->lk_keyid;
2378 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2380 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2382 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2383 RETURN(ERR_PTR(rc));
2386 oc = capa_add(dev->od_capa_hash, capa);
2390 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2392 struct osd_object *obj = osd_dt_obj(dt);
2393 struct inode *inode = obj->oo_inode;
2394 struct osd_thread_info *info = osd_oti_get(env);
2395 struct dentry *dentry = &info->oti_obj_dentry;
2396 struct file *file = &info->oti_file;
2401 dentry->d_inode = inode;
2402 file->f_dentry = dentry;
2403 file->f_mapping = inode->i_mapping;
2404 file->f_op = inode->i_fop;
2405 LOCK_INODE_MUTEX(inode);
2406 rc = file->f_op->fsync(file, dentry, 0);
2407 UNLOCK_INODE_MUTEX(inode);
2411 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2414 struct osd_object *obj = osd_dt_obj(dt);
2417 *data = (void *)obj->oo_inode;
2425 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2426 const struct dt_index_features *feat)
2428 struct iam_descr *descr;
2430 if (osd_object_is_root(o))
2431 return feat == &dt_directory_features;
2433 LASSERT(o->oo_dir != NULL);
2435 descr = o->oo_dir->od_container.ic_descr;
2436 if (feat == &dt_directory_features) {
2437 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2443 feat->dif_keysize_min <= descr->id_key_size &&
2444 descr->id_key_size <= feat->dif_keysize_max &&
2445 feat->dif_recsize_min <= descr->id_rec_size &&
2446 descr->id_rec_size <= feat->dif_recsize_max &&
2447 !(feat->dif_flags & (DT_IND_VARKEY |
2448 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2449 ergo(feat->dif_flags & DT_IND_UPDATE,
2450 1 /* XXX check that object (and file system) is
2455 static int osd_iam_container_init(const struct lu_env *env,
2456 struct osd_object *obj,
2457 struct osd_directory *dir)
2459 struct iam_container *bag = &dir->od_container;
2462 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2466 result = iam_container_setup(bag);
2470 if (osd_obj2dev(obj)->od_iop_mode) {
2471 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2473 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2479 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2481 iam_container_fini(bag);
2488 * Concurrency: no external locking is necessary.
2490 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2491 const struct dt_index_features *feat)
2495 struct osd_object *obj = osd_dt_obj(dt);
2496 struct osd_device *osd = osd_obj2dev(obj);
2498 LINVRNT(osd_invariant(obj));
2499 LASSERT(dt_object_exists(dt));
2501 if (osd_object_is_root(obj)) {
2502 dt->do_index_ops = &osd_index_ea_ops;
2504 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2505 dt->do_index_ops = &osd_index_ea_ops;
2506 if (S_ISDIR(obj->oo_inode->i_mode))
2511 } else if (!osd_has_index(obj)) {
2512 struct osd_directory *dir;
2517 cfs_spin_lock(&obj->oo_guard);
2518 if (obj->oo_dir == NULL)
2522 * Concurrent thread allocated container data.
2525 cfs_spin_unlock(&obj->oo_guard);
2527 * Now, that we have container data, serialize its
2530 cfs_down_write(&obj->oo_ext_idx_sem);
2532 * recheck under lock.
2534 if (!osd_has_index(obj))
2535 result = osd_iam_container_init(env, obj, dir);
2538 cfs_up_write(&obj->oo_ext_idx_sem);
2546 if (result == 0 && ea_dir == 0) {
2547 if (!osd_iam_index_probe(env, obj, feat))
2550 LINVRNT(osd_invariant(obj));
2555 static const struct dt_object_operations osd_obj_ops = {
2556 .do_read_lock = osd_object_read_lock,
2557 .do_write_lock = osd_object_write_lock,
2558 .do_read_unlock = osd_object_read_unlock,
2559 .do_write_unlock = osd_object_write_unlock,
2560 .do_write_locked = osd_object_write_locked,
2561 .do_attr_get = osd_attr_get,
2562 .do_declare_attr_set = osd_declare_attr_set,
2563 .do_attr_set = osd_attr_set,
2564 .do_ah_init = osd_ah_init,
2565 .do_declare_create = osd_declare_object_create,
2566 .do_create = osd_object_create,
2567 .do_declare_destroy = osd_declare_object_destroy,
2568 .do_destroy = osd_object_destroy,
2569 .do_index_try = osd_index_try,
2570 .do_declare_ref_add = osd_declare_object_ref_add,
2571 .do_ref_add = osd_object_ref_add,
2572 .do_declare_ref_del = osd_declare_object_ref_del,
2573 .do_ref_del = osd_object_ref_del,
2574 .do_xattr_get = osd_xattr_get,
2575 .do_declare_xattr_set = osd_declare_xattr_set,
2576 .do_xattr_set = osd_xattr_set,
2577 .do_declare_xattr_del = osd_declare_xattr_del,
2578 .do_xattr_del = osd_xattr_del,
2579 .do_xattr_list = osd_xattr_list,
2580 .do_capa_get = osd_capa_get,
2581 .do_object_sync = osd_object_sync,
2582 .do_data_get = osd_data_get,
2586 * dt_object_operations for interoperability mode
2587 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2589 static const struct dt_object_operations osd_obj_ea_ops = {
2590 .do_read_lock = osd_object_read_lock,
2591 .do_write_lock = osd_object_write_lock,
2592 .do_read_unlock = osd_object_read_unlock,
2593 .do_write_unlock = osd_object_write_unlock,
2594 .do_write_locked = osd_object_write_locked,
2595 .do_attr_get = osd_attr_get,
2596 .do_declare_attr_set = osd_declare_attr_set,
2597 .do_attr_set = osd_attr_set,
2598 .do_ah_init = osd_ah_init,
2599 .do_declare_create = osd_declare_object_create,
2600 .do_create = osd_object_ea_create,
2601 .do_declare_destroy = osd_declare_object_destroy,
2602 .do_destroy = osd_object_destroy,
2603 .do_index_try = osd_index_try,
2604 .do_declare_ref_add = osd_declare_object_ref_add,
2605 .do_ref_add = osd_object_ref_add,
2606 .do_declare_ref_del = osd_declare_object_ref_del,
2607 .do_ref_del = osd_object_ref_del,
2608 .do_xattr_get = osd_xattr_get,
2609 .do_declare_xattr_set = osd_declare_xattr_set,
2610 .do_xattr_set = osd_xattr_set,
2611 .do_declare_xattr_del = osd_declare_xattr_del,
2612 .do_xattr_del = osd_xattr_del,
2613 .do_xattr_list = osd_xattr_list,
2614 .do_capa_get = osd_capa_get,
2615 .do_object_sync = osd_object_sync,
2616 .do_data_get = osd_data_get,
2619 static int osd_index_declare_iam_delete(const struct lu_env *env,
2620 struct dt_object *dt,
2621 const struct dt_key *key,
2622 struct thandle *handle)
2624 struct osd_thandle *oh;
2626 oh = container_of0(handle, struct osd_thandle, ot_super);
2627 LASSERT(oh->ot_handle == NULL);
2629 OSD_DECLARE_OP(oh, delete);
2630 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2636 * delete a (key, value) pair from index \a dt specified by \a key
2638 * \param dt osd index object
2639 * \param key key for index
2640 * \param rec record reference
2641 * \param handle transaction handler
2644 * \retval -ve failure
2647 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2648 const struct dt_key *key,
2649 struct thandle *handle,
2650 struct lustre_capa *capa)
2652 struct osd_object *obj = osd_dt_obj(dt);
2653 struct osd_thandle *oh;
2654 struct iam_path_descr *ipd;
2655 struct iam_container *bag = &obj->oo_dir->od_container;
2660 LINVRNT(osd_invariant(obj));
2661 LASSERT(dt_object_exists(dt));
2662 LASSERT(bag->ic_object == obj->oo_inode);
2663 LASSERT(handle != NULL);
2665 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2668 OSD_EXEC_OP(handle, delete);
2670 ipd = osd_idx_ipd_get(env, bag);
2671 if (unlikely(ipd == NULL))
2674 oh = container_of0(handle, struct osd_thandle, ot_super);
2675 LASSERT(oh->ot_handle != NULL);
2676 LASSERT(oh->ot_handle->h_transaction != NULL);
2678 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2679 osd_ipd_put(env, bag, ipd);
2680 LINVRNT(osd_invariant(obj));
2684 static int osd_index_declare_ea_delete(const struct lu_env *env,
2685 struct dt_object *dt,
2686 const struct dt_key *key,
2687 struct thandle *handle)
2689 struct osd_thandle *oh;
2691 LASSERT(dt_object_exists(dt));
2692 LASSERT(handle != NULL);
2694 oh = container_of0(handle, struct osd_thandle, ot_super);
2695 LASSERT(oh->ot_handle == NULL);
2697 OSD_DECLARE_OP(oh, delete);
2698 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2700 LASSERT(osd_dt_obj(dt)->oo_inode);
2701 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2702 osd_dt_obj(dt)->oo_inode);
2703 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2704 osd_dt_obj(dt)->oo_inode);
2709 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2712 struct osd_fid_pack *rec;
2715 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2716 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2717 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2723 * Index delete function for interoperability mode (b11826).
2724 * It will remove the directory entry added by osd_index_ea_insert().
2725 * This entry is needed to maintain name->fid mapping.
2727 * \param key, key i.e. file entry to be deleted
2729 * \retval 0, on success
2730 * \retval -ve, on error
2732 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2733 const struct dt_key *key,
2734 struct thandle *handle,
2735 struct lustre_capa *capa)
2737 struct osd_object *obj = osd_dt_obj(dt);
2738 struct inode *dir = obj->oo_inode;
2739 struct dentry *dentry;
2740 struct osd_thandle *oh;
2741 struct ldiskfs_dir_entry_2 *de;
2742 struct buffer_head *bh;
2743 struct htree_lock *hlock = NULL;
2748 LINVRNT(osd_invariant(obj));
2749 LASSERT(dt_object_exists(dt));
2750 LASSERT(handle != NULL);
2752 OSD_EXEC_OP(handle, delete);
2754 oh = container_of(handle, struct osd_thandle, ot_super);
2755 LASSERT(oh->ot_handle != NULL);
2756 LASSERT(oh->ot_handle->h_transaction != NULL);
2758 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2761 dentry = osd_child_dentry_get(env, obj,
2762 (char *)key, strlen((char *)key));
2764 if (obj->oo_hl_head != NULL) {
2765 hlock = osd_oti_get(env)->oti_hlock;
2766 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2767 dir, LDISKFS_HLOCK_DEL);
2769 cfs_down_write(&obj->oo_ext_idx_sem);
2772 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2774 rc = ldiskfs_delete_entry(oh->ot_handle,
2781 ldiskfs_htree_unlock(hlock);
2783 cfs_up_write(&obj->oo_ext_idx_sem);
2785 LASSERT(osd_invariant(obj));
2790 * Lookup index for \a key and copy record to \a rec.
2792 * \param dt osd index object
2793 * \param key key for index
2794 * \param rec record reference
2796 * \retval +ve success : exact mach
2797 * \retval 0 return record with key not greater than \a key
2798 * \retval -ve failure
2800 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2801 struct dt_rec *rec, const struct dt_key *key,
2802 struct lustre_capa *capa)
2804 struct osd_object *obj = osd_dt_obj(dt);
2805 struct iam_path_descr *ipd;
2806 struct iam_container *bag = &obj->oo_dir->od_container;
2807 struct osd_thread_info *oti = osd_oti_get(env);
2808 struct iam_iterator *it = &oti->oti_idx_it;
2809 struct iam_rec *iam_rec;
2814 LASSERT(osd_invariant(obj));
2815 LASSERT(dt_object_exists(dt));
2816 LASSERT(bag->ic_object == obj->oo_inode);
2818 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2821 ipd = osd_idx_ipd_get(env, bag);
2825 /* got ipd now we can start iterator. */
2826 iam_it_init(it, bag, 0, ipd);
2828 rc = iam_it_get(it, (struct iam_key *)key);
2830 if (S_ISDIR(obj->oo_inode->i_mode))
2831 iam_rec = (struct iam_rec *)oti->oti_ldp;
2833 iam_rec = (struct iam_rec *) rec;
2835 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2836 if (S_ISDIR(obj->oo_inode->i_mode))
2837 osd_fid_unpack((struct lu_fid *) rec,
2838 (struct osd_fid_pack *)iam_rec);
2842 osd_ipd_put(env, bag, ipd);
2844 LINVRNT(osd_invariant(obj));
2849 static int osd_index_declare_iam_insert(const struct lu_env *env,
2850 struct dt_object *dt,
2851 const struct dt_rec *rec,
2852 const struct dt_key *key,
2853 struct thandle *handle)
2855 struct osd_thandle *oh;
2857 LASSERT(dt_object_exists(dt));
2858 LASSERT(handle != NULL);
2860 oh = container_of0(handle, struct osd_thandle, ot_super);
2861 LASSERT(oh->ot_handle == NULL);
2863 OSD_DECLARE_OP(oh, insert);
2864 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
2870 * Inserts (key, value) pair in \a dt index object.
2872 * \param dt osd index object
2873 * \param key key for index
2874 * \param rec record reference
2875 * \param th transaction handler
2878 * \retval -ve failure
2880 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2881 const struct dt_rec *rec,
2882 const struct dt_key *key, struct thandle *th,
2883 struct lustre_capa *capa, int ignore_quota)
2885 struct osd_object *obj = osd_dt_obj(dt);
2886 struct iam_path_descr *ipd;
2887 struct osd_thandle *oh;
2888 struct iam_container *bag = &obj->oo_dir->od_container;
2889 #ifdef HAVE_QUOTA_SUPPORT
2890 cfs_cap_t save = cfs_curproc_cap_pack();
2892 struct osd_thread_info *oti = osd_oti_get(env);
2893 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2898 LINVRNT(osd_invariant(obj));
2899 LASSERT(dt_object_exists(dt));
2900 LASSERT(bag->ic_object == obj->oo_inode);
2901 LASSERT(th != NULL);
2903 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2906 OSD_EXEC_OP(th, insert);
2908 ipd = osd_idx_ipd_get(env, bag);
2909 if (unlikely(ipd == NULL))
2912 oh = container_of0(th, struct osd_thandle, ot_super);
2913 LASSERT(oh->ot_handle != NULL);
2914 LASSERT(oh->ot_handle->h_transaction != NULL);
2915 #ifdef HAVE_QUOTA_SUPPORT
2917 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2919 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2921 if (S_ISDIR(obj->oo_inode->i_mode))
2922 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2924 iam_rec = (struct iam_rec *) rec;
2925 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2927 #ifdef HAVE_QUOTA_SUPPORT
2928 cfs_curproc_cap_unpack(save);
2930 osd_ipd_put(env, bag, ipd);
2931 LINVRNT(osd_invariant(obj));
2936 * Calls ldiskfs_add_entry() to add directory entry
2937 * into the directory. This is required for
2938 * interoperability mode (b11826)
2940 * \retval 0, on success
2941 * \retval -ve, on error
2943 static int __osd_ea_add_rec(struct osd_thread_info *info,
2944 struct osd_object *pobj, struct inode *cinode,
2945 const char *name, const struct dt_rec *fid,
2946 struct htree_lock *hlock, struct thandle *th)
2948 struct ldiskfs_dentry_param *ldp;
2949 struct dentry *child;
2950 struct osd_thandle *oth;
2953 oth = container_of(th, struct osd_thandle, ot_super);
2954 LASSERT(oth->ot_handle != NULL);
2955 LASSERT(oth->ot_handle->h_transaction != NULL);
2957 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2959 if (fid_is_igif((struct lu_fid *)fid) ||
2960 fid_is_norm((struct lu_fid *)fid)) {
2961 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2962 osd_get_ldiskfs_dirent_param(ldp, fid);
2963 child->d_fsdata = (void*) ldp;
2965 child->d_fsdata = NULL;
2966 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
2972 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2973 * into the directory.Also sets flags into osd object to
2974 * indicate dot and dotdot are created. This is required for
2975 * interoperability mode (b11826)
2977 * \param dir directory for dot and dotdot fixup.
2978 * \param obj child object for linking
2980 * \retval 0, on success
2981 * \retval -ve, on error
2983 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2984 struct osd_object *dir,
2985 struct inode *parent_dir, const char *name,
2986 const struct dt_rec *dot_fid,
2987 const struct dt_rec *dot_dot_fid,
2990 struct inode *inode = dir->oo_inode;
2991 struct ldiskfs_dentry_param *dot_ldp;
2992 struct ldiskfs_dentry_param *dot_dot_ldp;
2993 struct osd_thandle *oth;
2996 oth = container_of(th, struct osd_thandle, ot_super);
2997 LASSERT(oth->ot_handle->h_transaction != NULL);
2998 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3000 if (strcmp(name, dot) == 0) {
3001 if (dir->oo_compat_dot_created) {
3004 LASSERT(inode == parent_dir);
3005 dir->oo_compat_dot_created = 1;
3008 } else if(strcmp(name, dotdot) == 0) {
3009 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3010 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3012 if (!dir->oo_compat_dot_created)
3014 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
3015 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3016 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3021 /* in case of rename, dotdot is already created */
3022 if (dir->oo_compat_dotdot_created) {
3023 return __osd_ea_add_rec(info, dir, parent_dir, name,
3024 dot_dot_fid, NULL, th);
3027 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3028 inode, dot_ldp, dot_dot_ldp);
3030 dir->oo_compat_dotdot_created = 1;
3038 * It will call the appropriate osd_add* function and return the
3039 * value, return by respective functions.
3041 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3042 struct inode *cinode, const char *name,
3043 const struct dt_rec *fid, struct thandle *th)
3045 struct osd_thread_info *info = osd_oti_get(env);
3046 struct htree_lock *hlock;
3049 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3051 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3053 if (hlock != NULL) {
3054 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3057 cfs_down_write(&pobj->oo_ext_idx_sem);
3059 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3060 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3063 if (hlock != NULL) {
3064 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3065 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3067 cfs_down_write(&pobj->oo_ext_idx_sem);
3070 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3074 ldiskfs_htree_unlock(hlock);
3076 cfs_up_write(&pobj->oo_ext_idx_sem);
3082 * Calls ->lookup() to find dentry. From dentry get inode and
3083 * read inode's ea to get fid. This is required for interoperability
3086 * \retval 0, on success
3087 * \retval -ve, on error
3089 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3090 struct dt_rec *rec, const struct dt_key *key)
3092 struct inode *dir = obj->oo_inode;
3093 struct dentry *dentry;
3094 struct ldiskfs_dir_entry_2 *de;
3095 struct buffer_head *bh;
3096 struct lu_fid *fid = (struct lu_fid *) rec;
3097 struct htree_lock *hlock = NULL;
3101 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3103 dentry = osd_child_dentry_get(env, obj,
3104 (char *)key, strlen((char *)key));
3106 if (obj->oo_hl_head != NULL) {
3107 hlock = osd_oti_get(env)->oti_hlock;
3108 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3109 dir, LDISKFS_HLOCK_LOOKUP);
3111 cfs_down_read(&obj->oo_ext_idx_sem);
3114 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3116 ino = le32_to_cpu(de->inode);
3117 rc = osd_get_fid_from_dentry(de, rec);
3119 /* done with de, release bh */
3122 rc = osd_ea_fid_get(env, obj, ino, fid);
3128 ldiskfs_htree_unlock(hlock);
3130 cfs_up_read(&obj->oo_ext_idx_sem);
3135 * Find the osd object for given fid.
3137 * \param fid need to find the osd object having this fid
3139 * \retval osd_object on success
3140 * \retval -ve on error
3142 struct osd_object *osd_object_find(const struct lu_env *env,
3143 struct dt_object *dt,
3144 const struct lu_fid *fid)
3146 struct lu_device *ludev = dt->do_lu.lo_dev;
3147 struct osd_object *child = NULL;
3148 struct lu_object *luch;
3149 struct lu_object *lo;
3151 luch = lu_object_find(env, ludev, fid, NULL);
3152 if (!IS_ERR(luch)) {
3153 if (lu_object_exists(luch)) {
3154 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3156 child = osd_obj(lo);
3158 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3159 "lu_object can't be located"
3160 ""DFID"\n", PFID(fid));
3162 if (child == NULL) {
3163 lu_object_put(env, luch);
3164 CERROR("Unable to get osd_object\n");
3165 child = ERR_PTR(-ENOENT);
3168 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3169 "lu_object does not exists "DFID"\n",
3171 child = ERR_PTR(-ENOENT);
3174 child = (void *)luch;
3180 * Put the osd object once done with it.
3182 * \param obj osd object that needs to be put
3184 static inline void osd_object_put(const struct lu_env *env,
3185 struct osd_object *obj)
3187 lu_object_put(env, &obj->oo_dt.do_lu);
3190 static int osd_index_declare_ea_insert(const struct lu_env *env,
3191 struct dt_object *dt,
3192 const struct dt_rec *rec,
3193 const struct dt_key *key,
3194 struct thandle *handle)
3196 struct osd_thandle *oh;
3198 LASSERT(dt_object_exists(dt));
3199 LASSERT(handle != NULL);
3201 oh = container_of0(handle, struct osd_thandle, ot_super);
3202 LASSERT(oh->ot_handle == NULL);
3204 OSD_DECLARE_OP(oh, insert);
3205 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3207 LASSERT(osd_dt_obj(dt)->oo_inode);
3208 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3209 osd_dt_obj(dt)->oo_inode);
3210 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3211 osd_dt_obj(dt)->oo_inode);
3217 * Index add function for interoperability mode (b11826).
3218 * It will add the directory entry.This entry is needed to
3219 * maintain name->fid mapping.
3221 * \param key it is key i.e. file entry to be inserted
3222 * \param rec it is value of given key i.e. fid
3224 * \retval 0, on success
3225 * \retval -ve, on error
3227 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3228 const struct dt_rec *rec,
3229 const struct dt_key *key, struct thandle *th,
3230 struct lustre_capa *capa, int ignore_quota)
3232 struct osd_object *obj = osd_dt_obj(dt);
3233 struct lu_fid *fid = (struct lu_fid *) rec;
3234 const char *name = (const char *)key;
3235 struct osd_object *child;
3236 #ifdef HAVE_QUOTA_SUPPORT
3237 cfs_cap_t save = cfs_curproc_cap_pack();
3243 LASSERT(osd_invariant(obj));
3244 LASSERT(dt_object_exists(dt));
3245 LASSERT(th != NULL);
3247 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3250 child = osd_object_find(env, dt, fid);
3251 if (!IS_ERR(child)) {
3252 #ifdef HAVE_QUOTA_SUPPORT
3254 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3256 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3258 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3259 #ifdef HAVE_QUOTA_SUPPORT
3260 cfs_curproc_cap_unpack(save);
3262 osd_object_put(env, child);
3264 rc = PTR_ERR(child);
3267 LASSERT(osd_invariant(obj));
3272 * Initialize osd Iterator for given osd index object.
3274 * \param dt osd index object
3277 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3278 struct dt_object *dt,
3280 struct lustre_capa *capa)
3282 struct osd_it_iam *it;
3283 struct osd_thread_info *oti = osd_oti_get(env);
3284 struct osd_object *obj = osd_dt_obj(dt);
3285 struct lu_object *lo = &dt->do_lu;
3286 struct iam_path_descr *ipd;
3287 struct iam_container *bag = &obj->oo_dir->od_container;
3289 LASSERT(lu_object_exists(lo));
3291 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3292 return ERR_PTR(-EACCES);
3295 ipd = osd_it_ipd_get(env, bag);
3296 if (likely(ipd != NULL)) {
3300 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3301 return (struct dt_it *)it;
3303 return ERR_PTR(-ENOMEM);
3307 * free given Iterator.
3310 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3312 struct osd_it_iam *it = (struct osd_it_iam *)di;
3313 struct osd_object *obj = it->oi_obj;
3315 iam_it_fini(&it->oi_it);
3316 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3317 lu_object_put(env, &obj->oo_dt.do_lu);
3321 * Move Iterator to record specified by \a key
3323 * \param di osd iterator
3324 * \param key key for index
3326 * \retval +ve di points to record with least key not larger than key
3327 * \retval 0 di points to exact matched key
3328 * \retval -ve failure
3331 static int osd_it_iam_get(const struct lu_env *env,
3332 struct dt_it *di, const struct dt_key *key)
3334 struct osd_it_iam *it = (struct osd_it_iam *)di;
3336 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3342 * \param di osd iterator
3345 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3347 struct osd_it_iam *it = (struct osd_it_iam *)di;
3349 iam_it_put(&it->oi_it);
3353 * Move iterator by one record
3355 * \param di osd iterator
3357 * \retval +1 end of container reached
3359 * \retval -ve failure
3362 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3364 struct osd_it_iam *it = (struct osd_it_iam *)di;
3366 return iam_it_next(&it->oi_it);
3370 * Return pointer to the key under iterator.
3373 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3374 const struct dt_it *di)
3376 struct osd_it_iam *it = (struct osd_it_iam *)di;
3378 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3382 * Return size of key under iterator (in bytes)
3385 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3387 struct osd_it_iam *it = (struct osd_it_iam *)di;
3389 return iam_it_key_size(&it->oi_it);
3392 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3393 int len, __u16 type)
3395 struct luda_type *lt;
3396 const unsigned align = sizeof(struct luda_type) - 1;
3398 /* check if file type is required */
3399 if (attr & LUDA_TYPE) {
3400 len = (len + align) & ~align;
3402 lt = (void *) ent->lde_name + len;
3403 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3404 ent->lde_attrs |= LUDA_TYPE;
3407 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3411 * build lu direct from backend fs dirent.
3414 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3415 struct lu_fid *fid, __u64 offset,
3416 char *name, __u16 namelen,
3417 __u16 type, __u32 attr)
3419 fid_cpu_to_le(&ent->lde_fid, fid);
3420 ent->lde_attrs = LUDA_FID;
3422 ent->lde_hash = cpu_to_le64(offset);
3423 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3425 strncpy(ent->lde_name, name, namelen);
3426 ent->lde_namelen = cpu_to_le16(namelen);
3428 /* append lustre attributes */
3429 osd_it_append_attrs(ent, attr, namelen, type);
3433 * Return pointer to the record under iterator.
3435 static int osd_it_iam_rec(const struct lu_env *env,
3436 const struct dt_it *di,
3437 struct dt_rec *dtrec, __u32 attr)
3439 struct osd_it_iam *it = (struct osd_it_iam *)di;
3440 struct osd_thread_info *info = osd_oti_get(env);
3441 struct lu_fid *fid = &info->oti_fid;
3442 const struct osd_fid_pack *rec;
3443 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3449 name = (char *)iam_it_key_get(&it->oi_it);
3451 RETURN(PTR_ERR(name));
3453 namelen = iam_it_key_size(&it->oi_it);
3455 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3457 RETURN(PTR_ERR(rec));
3459 rc = osd_fid_unpack(fid, rec);
3463 hash = iam_it_store(&it->oi_it);
3465 /* IAM does not store object type in IAM index (dir) */
3466 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3473 * Returns cookie for current Iterator position.
3475 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3477 struct osd_it_iam *it = (struct osd_it_iam *)di;
3479 return iam_it_store(&it->oi_it);
3483 * Restore iterator from cookie.
3485 * \param di osd iterator
3486 * \param hash Iterator location cookie
3488 * \retval +ve di points to record with least key not larger than key.
3489 * \retval 0 di points to exact matched key
3490 * \retval -ve failure
3493 static int osd_it_iam_load(const struct lu_env *env,
3494 const struct dt_it *di, __u64 hash)
3496 struct osd_it_iam *it = (struct osd_it_iam *)di;
3498 return iam_it_load(&it->oi_it, hash);
3501 static const struct dt_index_operations osd_index_iam_ops = {
3502 .dio_lookup = osd_index_iam_lookup,
3503 .dio_declare_insert = osd_index_declare_iam_insert,
3504 .dio_insert = osd_index_iam_insert,
3505 .dio_declare_delete = osd_index_declare_iam_delete,
3506 .dio_delete = osd_index_iam_delete,
3508 .init = osd_it_iam_init,
3509 .fini = osd_it_iam_fini,
3510 .get = osd_it_iam_get,
3511 .put = osd_it_iam_put,
3512 .next = osd_it_iam_next,
3513 .key = osd_it_iam_key,
3514 .key_size = osd_it_iam_key_size,
3515 .rec = osd_it_iam_rec,
3516 .store = osd_it_iam_store,
3517 .load = osd_it_iam_load
3522 * Creates or initializes iterator context.
3524 * \retval struct osd_it_ea, iterator structure on success
3527 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3528 struct dt_object *dt,
3530 struct lustre_capa *capa)
3532 struct osd_object *obj = osd_dt_obj(dt);
3533 struct osd_thread_info *info = osd_oti_get(env);
3534 struct osd_it_ea *it = &info->oti_it_ea;
3535 struct lu_object *lo = &dt->do_lu;
3536 struct dentry *obj_dentry = &info->oti_it_dentry;
3538 LASSERT(lu_object_exists(lo));
3540 obj_dentry->d_inode = obj->oo_inode;
3541 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3542 obj_dentry->d_name.hash = 0;
3544 it->oie_rd_dirent = 0;
3545 it->oie_it_dirent = 0;
3546 it->oie_dirent = NULL;
3547 it->oie_buf = info->oti_it_ea_buf;
3549 it->oie_file.f_pos = 0;
3550 it->oie_file.f_dentry = obj_dentry;
3551 if (attr & LUDA_64BITHASH)
3552 it->oie_file.f_flags = O_64BITHASH;
3554 it->oie_file.f_flags = O_32BITHASH;
3555 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3556 it->oie_file.f_op = obj->oo_inode->i_fop;
3557 it->oie_file.private_data = NULL;
3559 RETURN((struct dt_it *) it);
3563 * Destroy or finishes iterator context.
3565 * \param di iterator structure to be destroyed
3567 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3569 struct osd_it_ea *it = (struct osd_it_ea *)di;
3570 struct osd_object *obj = it->oie_obj;
3571 struct inode *inode = obj->oo_inode;
3574 it->oie_file.f_op->release(inode, &it->oie_file);
3575 lu_object_put(env, &obj->oo_dt.do_lu);
3580 * It position the iterator at given key, so that next lookup continues from
3581 * that key Or it is similar to dio_it->load() but based on a key,
3582 * rather than file position.
3584 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3587 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3589 static int osd_it_ea_get(const struct lu_env *env,
3590 struct dt_it *di, const struct dt_key *key)
3592 struct osd_it_ea *it = (struct osd_it_ea *)di;
3595 LASSERT(((const char *)key)[0] == '\0');
3596 it->oie_file.f_pos = 0;
3597 it->oie_rd_dirent = 0;
3598 it->oie_it_dirent = 0;
3599 it->oie_dirent = NULL;
3607 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3612 * It is called internally by ->readdir(). It fills the
3613 * iterator's in-memory data structure with required
3614 * information i.e. name, namelen, rec_size etc.
3616 * \param buf in which information to be filled in.
3617 * \param name name of the file in given dir
3619 * \retval 0 on success
3620 * \retval 1 on buffer full
3622 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3623 loff_t offset, __u64 ino,
3626 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3627 struct osd_it_ea_dirent *ent = it->oie_dirent;
3628 struct lu_fid *fid = &ent->oied_fid;
3629 struct osd_fid_pack *rec;
3632 /* this should never happen */
3633 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3634 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3638 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3642 if (d_type & LDISKFS_DIRENT_LUFID) {
3643 rec = (struct osd_fid_pack*) (name + namelen + 1);
3645 if (osd_fid_unpack(fid, rec) != 0)
3648 d_type &= ~LDISKFS_DIRENT_LUFID;
3653 ent->oied_ino = ino;
3654 ent->oied_off = offset;
3655 ent->oied_namelen = namelen;
3656 ent->oied_type = d_type;
3658 memcpy(ent->oied_name, name, namelen);
3660 it->oie_rd_dirent++;
3661 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3666 * Calls ->readdir() to load a directory entry at a time
3667 * and stored it in iterator's in-memory data structure.
3669 * \param di iterator's in memory structure
3671 * \retval 0 on success
3672 * \retval -ve on error
3674 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3675 const struct dt_it *di)
3677 struct osd_it_ea *it = (struct osd_it_ea *)di;
3678 struct osd_object *obj = it->oie_obj;
3679 struct inode *inode = obj->oo_inode;
3680 struct htree_lock *hlock = NULL;
3684 it->oie_dirent = it->oie_buf;
3685 it->oie_rd_dirent = 0;
3687 if (obj->oo_hl_head != NULL) {
3688 hlock = osd_oti_get(env)->oti_hlock;
3689 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3690 inode, LDISKFS_HLOCK_READDIR);
3692 cfs_down_read(&obj->oo_ext_idx_sem);
3695 result = inode->i_fop->readdir(&it->oie_file, it,
3696 (filldir_t) osd_ldiskfs_filldir);
3699 ldiskfs_htree_unlock(hlock);
3701 cfs_up_read(&obj->oo_ext_idx_sem);
3703 if (it->oie_rd_dirent == 0) {
3706 it->oie_dirent = it->oie_buf;
3707 it->oie_it_dirent = 1;
3714 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3715 * to load a directory entry at a time and stored it in
3716 * iterator's in-memory data structure.
3718 * \param di iterator's in memory structure
3720 * \retval +ve iterator reached to end
3721 * \retval 0 iterator not reached to end
3722 * \retval -ve on error
3724 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3726 struct osd_it_ea *it = (struct osd_it_ea *)di;
3731 if (it->oie_it_dirent < it->oie_rd_dirent) {
3733 (void *) it->oie_dirent +
3734 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3735 it->oie_dirent->oied_namelen);
3736 it->oie_it_dirent++;
3739 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3742 rc = osd_ldiskfs_it_fill(env, di);
3749 * Returns the key at current position from iterator's in memory structure.
3751 * \param di iterator's in memory structure
3753 * \retval key i.e. struct dt_key on success
3755 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3756 const struct dt_it *di)
3758 struct osd_it_ea *it = (struct osd_it_ea *)di;
3760 return (struct dt_key *)it->oie_dirent->oied_name;
3764 * Returns the key's size at current position from iterator's in memory structure.
3766 * \param di iterator's in memory structure
3768 * \retval key_size i.e. struct dt_key on success
3770 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3772 struct osd_it_ea *it = (struct osd_it_ea *)di;
3774 return it->oie_dirent->oied_namelen;
3779 * Returns the value (i.e. fid/igif) at current position from iterator's
3780 * in memory structure.
3782 * \param di struct osd_it_ea, iterator's in memory structure
3783 * \param attr attr requested for dirent.
3784 * \param lde lustre dirent
3786 * \retval 0 no error and \param lde has correct lustre dirent.
3787 * \retval -ve on error
3789 static inline int osd_it_ea_rec(const struct lu_env *env,
3790 const struct dt_it *di,
3791 struct dt_rec *dtrec, __u32 attr)
3793 struct osd_it_ea *it = (struct osd_it_ea *)di;
3794 struct osd_object *obj = it->oie_obj;
3795 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3796 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3801 if (!fid_is_sane(fid))
3802 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3805 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3806 it->oie_dirent->oied_name,
3807 it->oie_dirent->oied_namelen,
3808 it->oie_dirent->oied_type,
3814 * Returns a cookie for current position of the iterator head, so that
3815 * user can use this cookie to load/start the iterator next time.
3817 * \param di iterator's in memory structure
3819 * \retval cookie for current position, on success
3821 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3823 struct osd_it_ea *it = (struct osd_it_ea *)di;
3825 return it->oie_dirent->oied_off;
3829 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3830 * to load a directory entry at a time and stored it i inn,
3831 * in iterator's in-memory data structure.
3833 * \param di struct osd_it_ea, iterator's in memory structure
3835 * \retval +ve on success
3836 * \retval -ve on error
3838 static int osd_it_ea_load(const struct lu_env *env,
3839 const struct dt_it *di, __u64 hash)
3841 struct osd_it_ea *it = (struct osd_it_ea *)di;
3845 it->oie_file.f_pos = hash;
3847 rc = osd_ldiskfs_it_fill(env, di);
3855 * Index lookup function for interoperability mode (b11826).
3857 * \param key, key i.e. file name to be searched
3859 * \retval +ve, on success
3860 * \retval -ve, on error
3862 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3863 struct dt_rec *rec, const struct dt_key *key,
3864 struct lustre_capa *capa)
3866 struct osd_object *obj = osd_dt_obj(dt);
3871 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3872 LINVRNT(osd_invariant(obj));
3874 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3877 rc = osd_ea_lookup_rec(env, obj, rec, key);
3885 * Index and Iterator operations for interoperability
3886 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3888 static const struct dt_index_operations osd_index_ea_ops = {
3889 .dio_lookup = osd_index_ea_lookup,
3890 .dio_declare_insert = osd_index_declare_ea_insert,
3891 .dio_insert = osd_index_ea_insert,
3892 .dio_declare_delete = osd_index_declare_ea_delete,
3893 .dio_delete = osd_index_ea_delete,
3895 .init = osd_it_ea_init,
3896 .fini = osd_it_ea_fini,
3897 .get = osd_it_ea_get,
3898 .put = osd_it_ea_put,
3899 .next = osd_it_ea_next,
3900 .key = osd_it_ea_key,
3901 .key_size = osd_it_ea_key_size,
3902 .rec = osd_it_ea_rec,
3903 .store = osd_it_ea_store,
3904 .load = osd_it_ea_load
3908 static void *osd_key_init(const struct lu_context *ctx,
3909 struct lu_context_key *key)
3911 struct osd_thread_info *info;
3913 OBD_ALLOC_PTR(info);
3915 return ERR_PTR(-ENOMEM);
3917 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3918 if (info->oti_it_ea_buf == NULL)
3921 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
3923 info->oti_hlock = ldiskfs_htree_lock_alloc();
3924 if (info->oti_hlock == NULL)
3930 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3933 return ERR_PTR(-ENOMEM);
3936 static void osd_key_fini(const struct lu_context *ctx,
3937 struct lu_context_key *key, void* data)
3939 struct osd_thread_info *info = data;
3941 if (info->oti_hlock != NULL)
3942 ldiskfs_htree_lock_free(info->oti_hlock);
3943 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3947 static void osd_key_exit(const struct lu_context *ctx,
3948 struct lu_context_key *key, void *data)
3950 struct osd_thread_info *info = data;
3952 LASSERT(info->oti_r_locks == 0);
3953 LASSERT(info->oti_w_locks == 0);
3954 LASSERT(info->oti_txns == 0);
3957 /* type constructor/destructor: osd_type_init, osd_type_fini */
3958 LU_TYPE_INIT_FINI(osd, &osd_key);
3960 struct lu_context_key osd_key = {
3961 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3962 .lct_init = osd_key_init,
3963 .lct_fini = osd_key_fini,
3964 .lct_exit = osd_key_exit
3968 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3969 const char *name, struct lu_device *next)
3971 return osd_procfs_init(osd_dev(d), name);
3974 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3976 struct osd_thread_info *info = osd_oti_get(env);
3978 if (o->od_obj_area != NULL) {
3979 lu_object_put(env, &o->od_obj_area->do_lu);
3980 o->od_obj_area = NULL;
3982 if (o->od_oi_table != NULL)
3983 osd_oi_fini(info, o);
3986 fsfilt_put_ops(o->od_fsops);
3993 static int osd_mount(const struct lu_env *env,
3994 struct osd_device *o, struct lustre_cfg *cfg)
3996 struct lustre_mount_info *lmi;
3997 const char *dev = lustre_cfg_string(cfg, 0);
3998 struct lustre_disk_data *ldd;
3999 struct lustre_sb_info *lsi;
4003 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4004 if (o->od_fsops == NULL) {
4005 CERROR("Can't find fsfilt_ldiskfs\n");
4009 if (o->od_mount != NULL) {
4010 CERROR("Already mounted (%s)\n", dev);
4015 lmi = server_get_mount(dev);
4017 CERROR("Cannot get mount info for %s!\n", dev);
4021 LASSERT(lmi != NULL);
4022 /* save lustre_mount_info in dt_device */
4025 lsi = s2lsi(lmi->lmi_sb);
4028 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4030 LCONSOLE_WARN("OSD: IAM mode enabled\n");
4034 o->od_obj_area = NULL;
4038 static struct lu_device *osd_device_fini(const struct lu_env *env,
4039 struct lu_device *d)
4044 shrink_dcache_sb(osd_sb(osd_dev(d)));
4045 osd_sync(env, lu2dt_dev(d));
4047 rc = osd_procfs_fini(osd_dev(d));
4049 CERROR("proc fini error %d \n", rc);
4050 RETURN (ERR_PTR(rc));
4053 if (osd_dev(d)->od_mount)
4054 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4055 osd_dev(d)->od_mount->lmi_mnt);
4056 osd_dev(d)->od_mount = NULL;
4061 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4062 struct lu_device_type *t,
4063 struct lustre_cfg *cfg)
4065 struct lu_device *l;
4066 struct osd_device *o;
4072 result = dt_device_init(&o->od_dt_dev, t);
4075 l->ld_ops = &osd_lu_ops;
4076 o->od_dt_dev.dd_ops = &osd_dt_ops;
4077 cfs_spin_lock_init(&o->od_osfs_lock);
4078 o->od_osfs_age = cfs_time_shift_64(-1000);
4079 o->od_capa_hash = init_capa_hash();
4080 if (o->od_capa_hash == NULL) {
4081 dt_device_fini(&o->od_dt_dev);
4082 l = ERR_PTR(-ENOMEM);
4085 l = ERR_PTR(result);
4090 l = ERR_PTR(-ENOMEM);
4094 static struct lu_device *osd_device_free(const struct lu_env *env,
4095 struct lu_device *d)
4097 struct osd_device *o = osd_dev(d);
4100 cleanup_capa_hash(o->od_capa_hash);
4101 dt_device_fini(&o->od_dt_dev);
4106 static int osd_process_config(const struct lu_env *env,
4107 struct lu_device *d, struct lustre_cfg *cfg)
4109 struct osd_device *o = osd_dev(d);
4113 switch(cfg->lcfg_command) {
4115 err = osd_mount(env, o, cfg);
4118 err = osd_shutdown(env, o);
4127 static int osd_recovery_complete(const struct lu_env *env,
4128 struct lu_device *d)
4133 static int osd_prepare(const struct lu_env *env,
4134 struct lu_device *pdev,
4135 struct lu_device *dev)
4137 struct osd_device *osd = osd_dev(dev);
4138 struct lustre_sb_info *lsi;
4139 struct lustre_disk_data *ldd;
4140 struct lustre_mount_info *lmi;
4141 struct osd_thread_info *oti = osd_oti_get(env);
4142 struct dt_object *d;
4146 /* 1. initialize oi before any file create or file open */
4147 result = osd_oi_init(oti, osd);
4151 if (!lu_device_is_md(pdev))
4154 lmi = osd->od_mount;
4155 lsi = s2lsi(lmi->lmi_sb);
4158 /* 2. setup local objects */
4159 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4163 /* 3. open remote object dir */
4164 d = dt_store_open(env, lu2dt_dev(dev), "",
4165 remote_obj_dir, &oti->oti_fid);
4167 osd->od_obj_area = d;
4170 result = PTR_ERR(d);
4171 osd->od_obj_area = NULL;
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_MD_THREAD|LCT_DT_THREAD
4216 * lprocfs legacy support.
4218 static struct obd_ops osd_obd_device_ops = {
4219 .o_owner = THIS_MODULE
4222 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4223 .llod_name = remote_obj_dir,
4224 .llod_oid = OSD_REM_OBJ_DIR_OID,
4226 .llod_feat = &dt_directory_features,
4229 static int __init osd_mod_init(void)
4231 struct lprocfs_static_vars lvars;
4234 llo_local_obj_register(&llod_osd_rem_obj_dir);
4235 lprocfs_osd_init_vars(&lvars);
4236 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4237 LUSTRE_OSD_NAME, &osd_device_type);
4240 static void __exit osd_mod_exit(void)
4242 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4243 class_unregister_type(LUSTRE_OSD_NAME);
4246 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4247 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4248 MODULE_LICENSE("GPL");
4250 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);