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 int osd_ro(const struct lu_env *env, struct dt_device *d)
929 struct super_block *sb = osd_sb(osd_dt_dev(d));
933 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
935 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
940 * Concurrency: serialization provided by callers.
942 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
943 int mode, unsigned long timeout, __u32 alg,
944 struct lustre_capa_key *keys)
946 struct osd_device *dev = osd_dt_dev(d);
949 dev->od_fl_capa = mode;
950 dev->od_capa_timeout = timeout;
951 dev->od_capa_alg = alg;
952 dev->od_capa_keys = keys;
957 * Concurrency: serialization provided by callers.
959 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
960 struct dt_quota_ctxt *ctxt, void *data)
962 struct obd_device *obd = (void *)ctxt;
963 struct vfsmount *mnt = (struct vfsmount *)data;
966 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
967 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
968 obd->obd_lvfs_ctxt.pwdmnt = mnt;
969 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
970 obd->obd_lvfs_ctxt.fs = get_ds();
976 * Note: we do not count into QUOTA here.
977 * If we mount with --data_journal we may need more.
979 const int osd_dto_credits_noquota[DTO_NR] = {
982 * INDEX_EXTRA_TRANS_BLOCKS(8) +
983 * SINGLEDATA_TRANS_BLOCKS(8)
984 * XXX Note: maybe iam need more, since iam have more level than
987 [DTO_INDEX_INSERT] = 16,
988 [DTO_INDEX_DELETE] = 16,
992 [DTO_INDEX_UPDATE] = 16,
994 * Create a object. The same as create object in EXT3.
995 * DATA_TRANS_BLOCKS(14) +
996 * INDEX_EXTRA_BLOCKS(8) +
997 * 3(inode bits, groups, GDT)
999 [DTO_OBJECT_CREATE] = 25,
1001 * XXX: real credits to be fixed
1003 [DTO_OBJECT_DELETE] = 25,
1005 * Attr set credits (inode)
1007 [DTO_ATTR_SET_BASE] = 1,
1009 * Xattr set. The same as xattr of EXT3.
1010 * DATA_TRANS_BLOCKS(14)
1011 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1012 * are also counted in. Do not know why?
1014 [DTO_XATTR_SET] = 14,
1017 * credits for inode change during write.
1019 [DTO_WRITE_BASE] = 3,
1021 * credits for single block write.
1023 [DTO_WRITE_BLOCK] = 14,
1025 * Attr set credits for chown.
1026 * This is extra credits for setattr, and it is null without quota
1028 [DTO_ATTR_SET_CHOWN]= 0
1031 static const struct dt_device_operations osd_dt_ops = {
1032 .dt_root_get = osd_root_get,
1033 .dt_statfs = osd_statfs,
1034 .dt_trans_create = osd_trans_create,
1035 .dt_trans_start = osd_trans_start,
1036 .dt_trans_stop = osd_trans_stop,
1037 .dt_trans_cb_add = osd_trans_cb_add,
1038 .dt_conf_get = osd_conf_get,
1039 .dt_sync = osd_sync,
1041 .dt_commit_async = osd_commit_async,
1042 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1043 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1046 static void osd_object_read_lock(const struct lu_env *env,
1047 struct dt_object *dt, unsigned role)
1049 struct osd_object *obj = osd_dt_obj(dt);
1050 struct osd_thread_info *oti = osd_oti_get(env);
1052 LINVRNT(osd_invariant(obj));
1054 LASSERT(obj->oo_owner != env);
1055 cfs_down_read_nested(&obj->oo_sem, role);
1057 LASSERT(obj->oo_owner == NULL);
1061 static void osd_object_write_lock(const struct lu_env *env,
1062 struct dt_object *dt, unsigned role)
1064 struct osd_object *obj = osd_dt_obj(dt);
1065 struct osd_thread_info *oti = osd_oti_get(env);
1067 LINVRNT(osd_invariant(obj));
1069 LASSERT(obj->oo_owner != env);
1070 cfs_down_write_nested(&obj->oo_sem, role);
1072 LASSERT(obj->oo_owner == NULL);
1073 obj->oo_owner = env;
1077 static void osd_object_read_unlock(const struct lu_env *env,
1078 struct dt_object *dt)
1080 struct osd_object *obj = osd_dt_obj(dt);
1081 struct osd_thread_info *oti = osd_oti_get(env);
1083 LINVRNT(osd_invariant(obj));
1085 LASSERT(oti->oti_r_locks > 0);
1087 cfs_up_read(&obj->oo_sem);
1090 static void osd_object_write_unlock(const struct lu_env *env,
1091 struct dt_object *dt)
1093 struct osd_object *obj = osd_dt_obj(dt);
1094 struct osd_thread_info *oti = osd_oti_get(env);
1096 LINVRNT(osd_invariant(obj));
1098 LASSERT(obj->oo_owner == env);
1099 LASSERT(oti->oti_w_locks > 0);
1101 obj->oo_owner = NULL;
1102 cfs_up_write(&obj->oo_sem);
1105 static int osd_object_write_locked(const struct lu_env *env,
1106 struct dt_object *dt)
1108 struct osd_object *obj = osd_dt_obj(dt);
1110 LINVRNT(osd_invariant(obj));
1112 return obj->oo_owner == env;
1115 static int capa_is_sane(const struct lu_env *env,
1116 struct osd_device *dev,
1117 struct lustre_capa *capa,
1118 struct lustre_capa_key *keys)
1120 struct osd_thread_info *oti = osd_oti_get(env);
1121 struct lustre_capa *tcapa = &oti->oti_capa;
1122 struct obd_capa *oc;
1126 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1128 if (capa_is_expired(oc)) {
1129 DEBUG_CAPA(D_ERROR, capa, "expired");
1136 if (capa_is_expired_sec(capa)) {
1137 DEBUG_CAPA(D_ERROR, capa, "expired");
1141 cfs_spin_lock(&capa_lock);
1142 for (i = 0; i < 2; i++) {
1143 if (keys[i].lk_keyid == capa->lc_keyid) {
1144 oti->oti_capa_key = keys[i];
1148 cfs_spin_unlock(&capa_lock);
1151 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1155 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1159 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1160 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1164 oc = capa_add(dev->od_capa_hash, capa);
1170 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1171 struct lustre_capa *capa, __u64 opc)
1173 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1174 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1175 struct md_capainfo *ci;
1178 if (!dev->od_fl_capa)
1181 if (capa == BYPASS_CAPA)
1184 ci = md_capainfo(env);
1188 if (ci->mc_auth == LC_ID_NONE)
1192 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1196 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1197 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1202 if (!capa_opc_supported(capa, opc)) {
1203 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1207 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1208 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1215 static struct timespec *osd_inode_time(const struct lu_env *env,
1216 struct inode *inode, __u64 seconds)
1218 struct osd_thread_info *oti = osd_oti_get(env);
1219 struct timespec *t = &oti->oti_time;
1221 t->tv_sec = seconds;
1223 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1228 static void osd_inode_getattr(const struct lu_env *env,
1229 struct inode *inode, struct lu_attr *attr)
1231 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1232 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1233 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1235 attr->la_atime = LTIME_S(inode->i_atime);
1236 attr->la_mtime = LTIME_S(inode->i_mtime);
1237 attr->la_ctime = LTIME_S(inode->i_ctime);
1238 attr->la_mode = inode->i_mode;
1239 attr->la_size = i_size_read(inode);
1240 attr->la_blocks = inode->i_blocks;
1241 attr->la_uid = inode->i_uid;
1242 attr->la_gid = inode->i_gid;
1243 attr->la_flags = LDISKFS_I(inode)->i_flags;
1244 attr->la_nlink = inode->i_nlink;
1245 attr->la_rdev = inode->i_rdev;
1246 attr->la_blksize = ll_inode_blksize(inode);
1247 attr->la_blkbits = inode->i_blkbits;
1250 static int osd_attr_get(const struct lu_env *env,
1251 struct dt_object *dt,
1252 struct lu_attr *attr,
1253 struct lustre_capa *capa)
1255 struct osd_object *obj = osd_dt_obj(dt);
1257 LASSERT(dt_object_exists(dt));
1258 LINVRNT(osd_invariant(obj));
1260 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1263 cfs_spin_lock(&obj->oo_guard);
1264 osd_inode_getattr(env, obj->oo_inode, attr);
1265 cfs_spin_unlock(&obj->oo_guard);
1269 static int osd_declare_attr_set(const struct lu_env *env,
1270 struct dt_object *dt,
1271 const struct lu_attr *attr,
1272 struct thandle *handle)
1274 struct osd_thandle *oh;
1275 struct osd_object *obj;
1277 LASSERT(dt != NULL);
1278 LASSERT(handle != NULL);
1280 obj = osd_dt_obj(dt);
1281 LASSERT(osd_invariant(obj));
1283 oh = container_of0(handle, struct osd_thandle, ot_super);
1284 LASSERT(oh->ot_handle == NULL);
1286 OSD_DECLARE_OP(oh, attr_set);
1287 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1289 if (attr && attr->la_valid & LA_UID) {
1291 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1293 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1295 if (attr && attr->la_valid & LA_GID) {
1297 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1299 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1305 static int osd_inode_setattr(const struct lu_env *env,
1306 struct inode *inode, const struct lu_attr *attr)
1310 bits = attr->la_valid;
1312 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1314 #ifdef HAVE_QUOTA_SUPPORT
1315 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1316 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1317 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1323 iattr.ia_valid |= ATTR_UID;
1325 iattr.ia_valid |= ATTR_GID;
1326 iattr.ia_uid = attr->la_uid;
1327 iattr.ia_gid = attr->la_gid;
1328 osd_push_ctxt(env, save);
1329 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1336 if (bits & LA_ATIME)
1337 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1338 if (bits & LA_CTIME)
1339 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1340 if (bits & LA_MTIME)
1341 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1342 if (bits & LA_SIZE) {
1343 LDISKFS_I(inode)->i_disksize = attr->la_size;
1344 i_size_write(inode, attr->la_size);
1348 /* OSD should not change "i_blocks" which is used by quota.
1349 * "i_blocks" should be changed by ldiskfs only. */
1350 if (bits & LA_BLOCKS)
1351 inode->i_blocks = attr->la_blocks;
1354 inode->i_mode = (inode->i_mode & S_IFMT) |
1355 (attr->la_mode & ~S_IFMT);
1357 inode->i_uid = attr->la_uid;
1359 inode->i_gid = attr->la_gid;
1360 if (bits & LA_NLINK)
1361 inode->i_nlink = attr->la_nlink;
1363 inode->i_rdev = attr->la_rdev;
1365 if (bits & LA_FLAGS) {
1366 /* always keep S_NOCMTIME */
1367 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1373 static int osd_attr_set(const struct lu_env *env,
1374 struct dt_object *dt,
1375 const struct lu_attr *attr,
1376 struct thandle *handle,
1377 struct lustre_capa *capa)
1379 struct osd_object *obj = osd_dt_obj(dt);
1382 LASSERT(handle != NULL);
1383 LASSERT(dt_object_exists(dt));
1384 LASSERT(osd_invariant(obj));
1386 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1389 OSD_EXEC_OP(handle, attr_set);
1391 cfs_spin_lock(&obj->oo_guard);
1392 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1393 cfs_spin_unlock(&obj->oo_guard);
1396 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1403 * XXX temporary solution.
1405 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1406 struct lu_attr *attr, struct thandle *th)
1411 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1412 struct lu_attr *attr, struct thandle *th)
1414 osd_object_init0(obj);
1415 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1416 unlock_new_inode(obj->oo_inode);
1420 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1421 struct osd_object *obj,
1422 const char *name, const int namelen)
1424 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1427 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1429 struct dt_allocation_hint *hint,
1433 struct osd_device *osd = osd_obj2dev(obj);
1434 struct osd_thandle *oth;
1435 struct dt_object *parent;
1436 struct inode *inode;
1437 #ifdef HAVE_QUOTA_SUPPORT
1438 struct osd_ctxt *save = &info->oti_ctxt;
1441 LINVRNT(osd_invariant(obj));
1442 LASSERT(obj->oo_inode == NULL);
1443 LASSERT(obj->oo_hl_head == NULL);
1445 if (S_ISDIR(mode) && ldiskfs_pdo) {
1446 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1447 if (obj->oo_hl_head == NULL)
1451 oth = container_of(th, struct osd_thandle, ot_super);
1452 LASSERT(oth->ot_handle->h_transaction != NULL);
1454 if (hint && hint->dah_parent)
1455 parent = hint->dah_parent;
1457 parent = osd->od_obj_area;
1459 #ifdef HAVE_QUOTA_SUPPORT
1460 osd_push_ctxt(info->oti_env, save);
1462 inode = ldiskfs_create_inode(oth->ot_handle,
1463 parent ? osd_dt_obj(parent)->oo_inode :
1464 osd_sb(osd)->s_root->d_inode,
1466 #ifdef HAVE_QUOTA_SUPPORT
1469 if (!IS_ERR(inode)) {
1470 /* Do not update file c/mtime in ldiskfs.
1471 * NB: don't need any lock because no contention at this
1473 inode->i_flags |= S_NOCMTIME;
1474 obj->oo_inode = inode;
1477 if (obj->oo_hl_head != NULL) {
1478 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1479 obj->oo_hl_head = NULL;
1481 result = PTR_ERR(inode);
1483 LINVRNT(osd_invariant(obj));
1491 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1492 struct lu_attr *attr,
1493 struct dt_allocation_hint *hint,
1494 struct dt_object_format *dof,
1498 struct osd_thandle *oth;
1499 struct osd_device *osd = osd_obj2dev(obj);
1500 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1502 LASSERT(S_ISDIR(attr->la_mode));
1504 oth = container_of(th, struct osd_thandle, ot_super);
1505 LASSERT(oth->ot_handle->h_transaction != NULL);
1506 result = osd_mkfile(info, obj, mode, hint, th);
1507 if (result == 0 && osd->od_iop_mode == 0) {
1508 LASSERT(obj->oo_inode != NULL);
1510 * XXX uh-oh... call low-level iam function directly.
1513 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1514 sizeof (struct osd_fid_pack),
1520 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1521 struct lu_attr *attr,
1522 struct dt_allocation_hint *hint,
1523 struct dt_object_format *dof,
1527 struct osd_thandle *oth;
1528 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1530 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1532 LASSERT(S_ISREG(attr->la_mode));
1534 oth = container_of(th, struct osd_thandle, ot_super);
1535 LASSERT(oth->ot_handle->h_transaction != NULL);
1537 result = osd_mkfile(info, obj, mode, hint, th);
1539 LASSERT(obj->oo_inode != NULL);
1540 if (feat->dif_flags & DT_IND_VARKEY)
1541 result = iam_lvar_create(obj->oo_inode,
1542 feat->dif_keysize_max,
1544 feat->dif_recsize_max,
1547 result = iam_lfix_create(obj->oo_inode,
1548 feat->dif_keysize_max,
1550 feat->dif_recsize_max,
1557 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1558 struct lu_attr *attr,
1559 struct dt_allocation_hint *hint,
1560 struct dt_object_format *dof,
1563 LASSERT(S_ISREG(attr->la_mode));
1564 return osd_mkfile(info, obj, (attr->la_mode &
1565 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1568 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1569 struct lu_attr *attr,
1570 struct dt_allocation_hint *hint,
1571 struct dt_object_format *dof,
1574 LASSERT(S_ISLNK(attr->la_mode));
1575 return osd_mkfile(info, obj, (attr->la_mode &
1576 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1579 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1580 struct lu_attr *attr,
1581 struct dt_allocation_hint *hint,
1582 struct dt_object_format *dof,
1585 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1588 LINVRNT(osd_invariant(obj));
1589 LASSERT(obj->oo_inode == NULL);
1590 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1591 S_ISFIFO(mode) || S_ISSOCK(mode));
1593 result = osd_mkfile(info, obj, mode, hint, th);
1595 LASSERT(obj->oo_inode != NULL);
1596 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1598 LINVRNT(osd_invariant(obj));
1602 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1604 struct dt_allocation_hint *hint,
1605 struct dt_object_format *dof,
1608 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1610 osd_obj_type_f result;
1626 result = osd_mk_index;
1637 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1638 struct dt_object *parent, cfs_umode_t child_mode)
1642 memset(ah, 0, sizeof(*ah));
1643 ah->dah_parent = parent;
1644 ah->dah_mode = child_mode;
1648 * Helper function for osd_object_create()
1650 * \retval 0, on success
1652 static int __osd_object_create(struct osd_thread_info *info,
1653 struct osd_object *obj, struct lu_attr *attr,
1654 struct dt_allocation_hint *hint,
1655 struct dt_object_format *dof,
1661 result = osd_create_pre(info, obj, attr, th);
1663 result = osd_create_type_f(dof->dof_type)(info, obj,
1664 attr, hint, dof, th);
1666 result = osd_create_post(info, obj, attr, th);
1672 * Helper function for osd_object_create()
1674 * \retval 0, on success
1676 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1677 const struct lu_fid *fid, struct thandle *th)
1679 struct osd_thread_info *info = osd_oti_get(env);
1680 struct osd_inode_id *id = &info->oti_id;
1681 struct osd_device *osd = osd_obj2dev(obj);
1682 struct md_ucred *uc = md_ucred(env);
1684 LASSERT(obj->oo_inode != NULL);
1685 LASSERT(uc != NULL);
1687 id->oii_ino = obj->oo_inode->i_ino;
1688 id->oii_gen = obj->oo_inode->i_generation;
1690 return osd_oi_insert(info, osd, fid, id, th,
1691 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1694 static int osd_declare_object_create(const struct lu_env *env,
1695 struct dt_object *dt,
1696 struct lu_attr *attr,
1697 struct dt_allocation_hint *hint,
1698 struct dt_object_format *dof,
1699 struct thandle *handle)
1701 struct osd_thandle *oh;
1703 LASSERT(handle != NULL);
1705 oh = container_of0(handle, struct osd_thandle, ot_super);
1706 LASSERT(oh->ot_handle == NULL);
1708 OSD_DECLARE_OP(oh, create);
1709 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1710 /* XXX: So far, only normal fid needs be inserted into the oi,
1711 * things could be changed later. Revise following code then. */
1712 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1713 OSD_DECLARE_OP(oh, insert);
1714 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1716 /* If this is directory, then we expect . and .. to be inserted as
1717 * well. The one directory block always needs to be created for the
1718 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1719 * block), there is no danger of needing a tree for the first block.
1721 if (attr && S_ISDIR(attr->la_mode)) {
1722 OSD_DECLARE_OP(oh, insert);
1723 OSD_DECLARE_OP(oh, insert);
1724 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1728 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1729 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1734 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1735 struct lu_attr *attr,
1736 struct dt_allocation_hint *hint,
1737 struct dt_object_format *dof,
1740 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1741 struct osd_object *obj = osd_dt_obj(dt);
1742 struct osd_thread_info *info = osd_oti_get(env);
1747 LINVRNT(osd_invariant(obj));
1748 LASSERT(!dt_object_exists(dt));
1749 LASSERT(osd_write_locked(env, obj));
1750 LASSERT(th != NULL);
1752 OSD_EXEC_OP(th, create);
1754 result = __osd_object_create(info, obj, attr, hint, dof, th);
1756 result = __osd_oi_insert(env, obj, fid, th);
1758 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1759 LASSERT(osd_invariant(obj));
1764 * Called to destroy on-disk representation of the object
1766 * Concurrency: must be locked
1768 static int osd_declare_object_destroy(const struct lu_env *env,
1769 struct dt_object *dt,
1772 struct osd_object *obj = osd_dt_obj(dt);
1773 struct inode *inode = obj->oo_inode;
1774 struct osd_thandle *oh;
1778 oh = container_of0(th, struct osd_thandle, ot_super);
1779 LASSERT(oh->ot_handle == NULL);
1782 OSD_DECLARE_OP(oh, destroy);
1783 OSD_DECLARE_OP(oh, delete);
1784 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
1785 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
1787 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
1788 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
1793 static int osd_object_destroy(const struct lu_env *env,
1794 struct dt_object *dt,
1797 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1798 struct osd_object *obj = osd_dt_obj(dt);
1799 struct inode *inode = obj->oo_inode;
1800 struct osd_device *osd = osd_obj2dev(obj);
1801 struct osd_thandle *oh;
1805 oh = container_of0(th, struct osd_thandle, ot_super);
1806 LASSERT(oh->ot_handle);
1808 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1810 if (S_ISDIR(inode->i_mode)) {
1811 LASSERT(osd_inode_unlinked(inode) ||
1812 inode->i_nlink == 1);
1813 cfs_spin_lock(&obj->oo_guard);
1815 cfs_spin_unlock(&obj->oo_guard);
1816 inode->i_sb->s_op->dirty_inode(inode);
1818 LASSERT(osd_inode_unlinked(inode));
1821 OSD_EXEC_OP(th, destroy);
1823 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
1825 /* XXX: add to ext3 orphan list */
1826 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
1828 /* not needed in the cache anymore */
1829 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1835 * Helper function for osd_xattr_set()
1837 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1838 const struct lu_buf *buf, const char *name, int fl)
1840 struct osd_object *obj = osd_dt_obj(dt);
1841 struct inode *inode = obj->oo_inode;
1842 struct osd_thread_info *info = osd_oti_get(env);
1843 struct dentry *dentry = &info->oti_child_dentry;
1847 LASSERT(dt_object_exists(dt));
1848 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1849 LASSERT(osd_write_locked(env, obj));
1851 if (fl & LU_XATTR_REPLACE)
1852 fs_flags |= XATTR_REPLACE;
1854 if (fl & LU_XATTR_CREATE)
1855 fs_flags |= XATTR_CREATE;
1857 dentry->d_inode = inode;
1858 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1859 buf->lb_len, fs_flags);
1864 * Put the fid into lustre_mdt_attrs, and then place the structure
1865 * inode's ea. This fid should not be altered during the life time
1868 * \retval +ve, on success
1869 * \retval -ve, on error
1871 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1873 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1874 const struct lu_fid *fid)
1876 struct osd_thread_info *info = osd_oti_get(env);
1877 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1879 lustre_lma_init(mdt_attrs, fid);
1880 lustre_lma_swab(mdt_attrs);
1881 return __osd_xattr_set(env, dt,
1882 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1883 XATTR_NAME_LMA, LU_XATTR_CREATE);
1888 * Helper function to form igif
1890 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1893 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1897 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1898 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1899 * To have compatilibility with 1.8 ldiskfs driver we need to have
1900 * magic number at start of fid data.
1901 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1904 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1905 const struct dt_rec *fid)
1907 param->edp_magic = LDISKFS_LUFID_MAGIC;
1908 param->edp_len = sizeof(struct lu_fid) + 1;
1910 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1911 (struct lu_fid *)fid);
1915 * Try to read the fid from inode ea into dt_rec, if return value
1916 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1918 * \param fid object fid.
1920 * \retval 0 on success
1922 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1923 __u32 ino, struct lu_fid *fid)
1925 struct osd_thread_info *info = osd_oti_get(env);
1926 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1927 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1928 struct dentry *dentry = &info->oti_child_dentry;
1929 struct osd_inode_id *id = &info->oti_id;
1930 struct osd_device *dev;
1931 struct inode *inode;
1935 dev = osd_dev(ldev);
1938 id->oii_gen = OSD_OII_NOGEN;
1940 inode = osd_iget(info, dev, id);
1941 if (IS_ERR(inode)) {
1942 rc = PTR_ERR(inode);
1945 dentry->d_inode = inode;
1947 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1948 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1951 /* Check LMA compatibility */
1953 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
1954 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1955 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
1956 ~LMA_INCOMPAT_SUPP);
1961 lustre_lma_swab(mdt_attrs);
1962 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
1964 } else if (rc == -ENODATA) {
1965 osd_igif_get(env, inode, fid);
1974 * OSD layer object create function for interoperability mode (b11826).
1975 * This is mostly similar to osd_object_create(). Only difference being, fid is
1976 * inserted into inode ea here.
1978 * \retval 0, on success
1979 * \retval -ve, on error
1981 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1982 struct lu_attr *attr,
1983 struct dt_allocation_hint *hint,
1984 struct dt_object_format *dof,
1987 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1988 struct osd_object *obj = osd_dt_obj(dt);
1989 struct osd_thread_info *info = osd_oti_get(env);
1994 LASSERT(osd_invariant(obj));
1995 LASSERT(!dt_object_exists(dt));
1996 LASSERT(osd_write_locked(env, obj));
1997 LASSERT(th != NULL);
1999 OSD_EXEC_OP(th, create);
2001 result = __osd_object_create(info, obj, attr, hint, dof, th);
2003 /* objects under osd root shld have igif fid, so dont add fid EA */
2004 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2005 result = osd_ea_fid_set(env, dt, fid);
2008 result = __osd_oi_insert(env, obj, fid, th);
2010 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2011 LINVRNT(osd_invariant(obj));
2015 static int osd_declare_object_ref_add(const struct lu_env *env,
2016 struct dt_object *dt,
2017 struct thandle *handle)
2019 struct osd_thandle *oh;
2021 /* it's possible that object doesn't exist yet */
2022 LASSERT(handle != NULL);
2024 oh = container_of0(handle, struct osd_thandle, ot_super);
2025 LASSERT(oh->ot_handle == NULL);
2027 OSD_DECLARE_OP(oh, ref_add);
2028 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2034 * Concurrency: @dt is write locked.
2036 static int osd_object_ref_add(const struct lu_env *env,
2037 struct dt_object *dt, struct thandle *th)
2039 struct osd_object *obj = osd_dt_obj(dt);
2040 struct inode *inode = obj->oo_inode;
2042 LINVRNT(osd_invariant(obj));
2043 LASSERT(dt_object_exists(dt));
2044 LASSERT(osd_write_locked(env, obj));
2045 LASSERT(th != NULL);
2047 OSD_EXEC_OP(th, ref_add);
2050 * DIR_NLINK feature is set for compatibility reasons if:
2051 * 1) nlinks > LDISKFS_LINK_MAX, or
2052 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2054 * It is easier to always set this flag (rather than check and set),
2055 * since it has less overhead, and the superblock will be dirtied
2056 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2057 * do not actually care whether this flag is set or not.
2059 cfs_spin_lock(&obj->oo_guard);
2061 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2062 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2063 inode->i_nlink == 2)
2066 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
2067 cfs_spin_unlock(&obj->oo_guard);
2068 inode->i_sb->s_op->dirty_inode(inode);
2069 LINVRNT(osd_invariant(obj));
2074 static int osd_declare_object_ref_del(const struct lu_env *env,
2075 struct dt_object *dt,
2076 struct thandle *handle)
2078 struct osd_thandle *oh;
2080 LASSERT(dt_object_exists(dt));
2081 LASSERT(handle != NULL);
2083 oh = container_of0(handle, struct osd_thandle, ot_super);
2084 LASSERT(oh->ot_handle == NULL);
2086 OSD_DECLARE_OP(oh, ref_del);
2087 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2093 * Concurrency: @dt is write locked.
2095 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2098 struct osd_object *obj = osd_dt_obj(dt);
2099 struct inode *inode = obj->oo_inode;
2101 LINVRNT(osd_invariant(obj));
2102 LASSERT(dt_object_exists(dt));
2103 LASSERT(osd_write_locked(env, obj));
2104 LASSERT(th != NULL);
2106 OSD_EXEC_OP(th, ref_del);
2108 cfs_spin_lock(&obj->oo_guard);
2109 LASSERT(inode->i_nlink > 0);
2111 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2112 * then the nlink count is 1. Don't let it be set to 0 or the directory
2113 * inode will be deleted incorrectly. */
2114 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2116 cfs_spin_unlock(&obj->oo_guard);
2117 inode->i_sb->s_op->dirty_inode(inode);
2118 LINVRNT(osd_invariant(obj));
2124 * Get the 64-bit version for an inode.
2126 static int osd_object_version_get(const struct lu_env *env,
2127 struct dt_object *dt, dt_obj_version_t *ver)
2129 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2131 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2132 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2133 *ver = LDISKFS_I(inode)->i_fs_version;
2138 * Concurrency: @dt is read locked.
2140 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2141 struct lu_buf *buf, const char *name,
2142 struct lustre_capa *capa)
2144 struct osd_object *obj = osd_dt_obj(dt);
2145 struct inode *inode = obj->oo_inode;
2146 struct osd_thread_info *info = osd_oti_get(env);
2147 struct dentry *dentry = &info->oti_obj_dentry;
2149 /* version get is not real XATTR but uses xattr API */
2150 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2151 /* for version we are just using xattr API but change inode
2153 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2154 osd_object_version_get(env, dt, buf->lb_buf);
2155 return sizeof(dt_obj_version_t);
2158 LASSERT(dt_object_exists(dt));
2159 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2160 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2162 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2165 dentry->d_inode = inode;
2166 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2170 static int osd_declare_xattr_set(const struct lu_env *env,
2171 struct dt_object *dt,
2172 const struct lu_buf *buf, const char *name,
2173 int fl, struct thandle *handle)
2175 struct osd_thandle *oh;
2177 LASSERT(handle != NULL);
2179 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2180 /* no credits for version */
2184 oh = container_of0(handle, struct osd_thandle, ot_super);
2185 LASSERT(oh->ot_handle == NULL);
2187 OSD_DECLARE_OP(oh, xattr_set);
2188 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2194 * Set the 64-bit version for object
2196 static void osd_object_version_set(const struct lu_env *env,
2197 struct dt_object *dt,
2198 dt_obj_version_t *new_version)
2200 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2202 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2203 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2205 LDISKFS_I(inode)->i_fs_version = *new_version;
2206 /** Version is set after all inode operations are finished,
2207 * so we should mark it dirty here */
2208 inode->i_sb->s_op->dirty_inode(inode);
2212 * Concurrency: @dt is write locked.
2214 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2215 const struct lu_buf *buf, const char *name, int fl,
2216 struct thandle *handle, struct lustre_capa *capa)
2218 LASSERT(handle != NULL);
2220 /* version set is not real XATTR */
2221 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2222 /* for version we are just using xattr API but change inode
2224 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2225 osd_object_version_set(env, dt, buf->lb_buf);
2226 return sizeof(dt_obj_version_t);
2229 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2232 OSD_EXEC_OP(handle, xattr_set);
2233 return __osd_xattr_set(env, dt, buf, name, fl);
2237 * Concurrency: @dt is read locked.
2239 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2240 struct lu_buf *buf, struct lustre_capa *capa)
2242 struct osd_object *obj = osd_dt_obj(dt);
2243 struct inode *inode = obj->oo_inode;
2244 struct osd_thread_info *info = osd_oti_get(env);
2245 struct dentry *dentry = &info->oti_obj_dentry;
2247 LASSERT(dt_object_exists(dt));
2248 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2249 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2251 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2254 dentry->d_inode = inode;
2255 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2258 static int osd_declare_xattr_del(const struct lu_env *env,
2259 struct dt_object *dt, const char *name,
2260 struct thandle *handle)
2262 struct osd_thandle *oh;
2264 LASSERT(dt_object_exists(dt));
2265 LASSERT(handle != NULL);
2267 oh = container_of0(handle, struct osd_thandle, ot_super);
2268 LASSERT(oh->ot_handle == NULL);
2270 OSD_DECLARE_OP(oh, xattr_set);
2271 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2277 * Concurrency: @dt is write locked.
2279 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2280 const char *name, struct thandle *handle,
2281 struct lustre_capa *capa)
2283 struct osd_object *obj = osd_dt_obj(dt);
2284 struct inode *inode = obj->oo_inode;
2285 struct osd_thread_info *info = osd_oti_get(env);
2286 struct dentry *dentry = &info->oti_obj_dentry;
2289 LASSERT(dt_object_exists(dt));
2290 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2291 LASSERT(osd_write_locked(env, obj));
2292 LASSERT(handle != NULL);
2294 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2297 OSD_EXEC_OP(handle, xattr_set);
2299 dentry->d_inode = inode;
2300 rc = inode->i_op->removexattr(dentry, name);
2304 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2305 struct dt_object *dt,
2306 struct lustre_capa *old,
2309 struct osd_thread_info *info = osd_oti_get(env);
2310 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2311 struct osd_object *obj = osd_dt_obj(dt);
2312 struct osd_device *dev = osd_obj2dev(obj);
2313 struct lustre_capa_key *key = &info->oti_capa_key;
2314 struct lustre_capa *capa = &info->oti_capa;
2315 struct obd_capa *oc;
2316 struct md_capainfo *ci;
2320 if (!dev->od_fl_capa)
2321 RETURN(ERR_PTR(-ENOENT));
2323 LASSERT(dt_object_exists(dt));
2324 LINVRNT(osd_invariant(obj));
2326 /* renewal sanity check */
2327 if (old && osd_object_auth(env, dt, old, opc))
2328 RETURN(ERR_PTR(-EACCES));
2330 ci = md_capainfo(env);
2332 RETURN(ERR_PTR(-ENOENT));
2334 switch (ci->mc_auth) {
2338 capa->lc_uid = obj->oo_inode->i_uid;
2339 capa->lc_gid = obj->oo_inode->i_gid;
2340 capa->lc_flags = LC_ID_PLAIN;
2342 case LC_ID_CONVERT: {
2345 s[0] = obj->oo_inode->i_uid;
2346 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2347 s[2] = obj->oo_inode->i_gid;
2348 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2349 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2351 RETURN(ERR_PTR(rc));
2353 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2354 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2355 capa->lc_flags = LC_ID_CONVERT;
2359 RETURN(ERR_PTR(-EINVAL));
2362 capa->lc_fid = *fid;
2364 capa->lc_flags |= dev->od_capa_alg << 24;
2365 capa->lc_timeout = dev->od_capa_timeout;
2366 capa->lc_expiry = 0;
2368 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2370 LASSERT(!capa_is_expired(oc));
2374 cfs_spin_lock(&capa_lock);
2375 *key = dev->od_capa_keys[1];
2376 cfs_spin_unlock(&capa_lock);
2378 capa->lc_keyid = key->lk_keyid;
2379 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2381 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2383 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2384 RETURN(ERR_PTR(rc));
2387 oc = capa_add(dev->od_capa_hash, capa);
2391 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2393 struct osd_object *obj = osd_dt_obj(dt);
2394 struct inode *inode = obj->oo_inode;
2395 struct osd_thread_info *info = osd_oti_get(env);
2396 struct dentry *dentry = &info->oti_obj_dentry;
2397 struct file *file = &info->oti_file;
2402 dentry->d_inode = inode;
2403 file->f_dentry = dentry;
2404 file->f_mapping = inode->i_mapping;
2405 file->f_op = inode->i_fop;
2406 LOCK_INODE_MUTEX(inode);
2407 rc = file->f_op->fsync(file, dentry, 0);
2408 UNLOCK_INODE_MUTEX(inode);
2412 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2415 struct osd_object *obj = osd_dt_obj(dt);
2418 *data = (void *)obj->oo_inode;
2426 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2427 const struct dt_index_features *feat)
2429 struct iam_descr *descr;
2431 if (osd_object_is_root(o))
2432 return feat == &dt_directory_features;
2434 LASSERT(o->oo_dir != NULL);
2436 descr = o->oo_dir->od_container.ic_descr;
2437 if (feat == &dt_directory_features) {
2438 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2444 feat->dif_keysize_min <= descr->id_key_size &&
2445 descr->id_key_size <= feat->dif_keysize_max &&
2446 feat->dif_recsize_min <= descr->id_rec_size &&
2447 descr->id_rec_size <= feat->dif_recsize_max &&
2448 !(feat->dif_flags & (DT_IND_VARKEY |
2449 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2450 ergo(feat->dif_flags & DT_IND_UPDATE,
2451 1 /* XXX check that object (and file system) is
2456 static int osd_iam_container_init(const struct lu_env *env,
2457 struct osd_object *obj,
2458 struct osd_directory *dir)
2460 struct iam_container *bag = &dir->od_container;
2463 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2467 result = iam_container_setup(bag);
2471 if (osd_obj2dev(obj)->od_iop_mode) {
2472 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2474 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2480 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2482 iam_container_fini(bag);
2489 * Concurrency: no external locking is necessary.
2491 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2492 const struct dt_index_features *feat)
2496 struct osd_object *obj = osd_dt_obj(dt);
2497 struct osd_device *osd = osd_obj2dev(obj);
2499 LINVRNT(osd_invariant(obj));
2500 LASSERT(dt_object_exists(dt));
2502 if (osd_object_is_root(obj)) {
2503 dt->do_index_ops = &osd_index_ea_ops;
2505 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2506 dt->do_index_ops = &osd_index_ea_ops;
2507 if (S_ISDIR(obj->oo_inode->i_mode))
2512 } else if (!osd_has_index(obj)) {
2513 struct osd_directory *dir;
2518 cfs_spin_lock(&obj->oo_guard);
2519 if (obj->oo_dir == NULL)
2523 * Concurrent thread allocated container data.
2526 cfs_spin_unlock(&obj->oo_guard);
2528 * Now, that we have container data, serialize its
2531 cfs_down_write(&obj->oo_ext_idx_sem);
2533 * recheck under lock.
2535 if (!osd_has_index(obj))
2536 result = osd_iam_container_init(env, obj, dir);
2539 cfs_up_write(&obj->oo_ext_idx_sem);
2547 if (result == 0 && ea_dir == 0) {
2548 if (!osd_iam_index_probe(env, obj, feat))
2551 LINVRNT(osd_invariant(obj));
2556 static const struct dt_object_operations osd_obj_ops = {
2557 .do_read_lock = osd_object_read_lock,
2558 .do_write_lock = osd_object_write_lock,
2559 .do_read_unlock = osd_object_read_unlock,
2560 .do_write_unlock = osd_object_write_unlock,
2561 .do_write_locked = osd_object_write_locked,
2562 .do_attr_get = osd_attr_get,
2563 .do_declare_attr_set = osd_declare_attr_set,
2564 .do_attr_set = osd_attr_set,
2565 .do_ah_init = osd_ah_init,
2566 .do_declare_create = osd_declare_object_create,
2567 .do_create = osd_object_create,
2568 .do_declare_destroy = osd_declare_object_destroy,
2569 .do_destroy = osd_object_destroy,
2570 .do_index_try = osd_index_try,
2571 .do_declare_ref_add = osd_declare_object_ref_add,
2572 .do_ref_add = osd_object_ref_add,
2573 .do_declare_ref_del = osd_declare_object_ref_del,
2574 .do_ref_del = osd_object_ref_del,
2575 .do_xattr_get = osd_xattr_get,
2576 .do_declare_xattr_set = osd_declare_xattr_set,
2577 .do_xattr_set = osd_xattr_set,
2578 .do_declare_xattr_del = osd_declare_xattr_del,
2579 .do_xattr_del = osd_xattr_del,
2580 .do_xattr_list = osd_xattr_list,
2581 .do_capa_get = osd_capa_get,
2582 .do_object_sync = osd_object_sync,
2583 .do_data_get = osd_data_get,
2587 * dt_object_operations for interoperability mode
2588 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2590 static const struct dt_object_operations osd_obj_ea_ops = {
2591 .do_read_lock = osd_object_read_lock,
2592 .do_write_lock = osd_object_write_lock,
2593 .do_read_unlock = osd_object_read_unlock,
2594 .do_write_unlock = osd_object_write_unlock,
2595 .do_write_locked = osd_object_write_locked,
2596 .do_attr_get = osd_attr_get,
2597 .do_declare_attr_set = osd_declare_attr_set,
2598 .do_attr_set = osd_attr_set,
2599 .do_ah_init = osd_ah_init,
2600 .do_declare_create = osd_declare_object_create,
2601 .do_create = osd_object_ea_create,
2602 .do_declare_destroy = osd_declare_object_destroy,
2603 .do_destroy = osd_object_destroy,
2604 .do_index_try = osd_index_try,
2605 .do_declare_ref_add = osd_declare_object_ref_add,
2606 .do_ref_add = osd_object_ref_add,
2607 .do_declare_ref_del = osd_declare_object_ref_del,
2608 .do_ref_del = osd_object_ref_del,
2609 .do_xattr_get = osd_xattr_get,
2610 .do_declare_xattr_set = osd_declare_xattr_set,
2611 .do_xattr_set = osd_xattr_set,
2612 .do_declare_xattr_del = osd_declare_xattr_del,
2613 .do_xattr_del = osd_xattr_del,
2614 .do_xattr_list = osd_xattr_list,
2615 .do_capa_get = osd_capa_get,
2616 .do_object_sync = osd_object_sync,
2617 .do_data_get = osd_data_get,
2620 static int osd_index_declare_iam_delete(const struct lu_env *env,
2621 struct dt_object *dt,
2622 const struct dt_key *key,
2623 struct thandle *handle)
2625 struct osd_thandle *oh;
2627 oh = container_of0(handle, struct osd_thandle, ot_super);
2628 LASSERT(oh->ot_handle == NULL);
2630 OSD_DECLARE_OP(oh, delete);
2631 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2637 * delete a (key, value) pair from index \a dt specified by \a key
2639 * \param dt osd index object
2640 * \param key key for index
2641 * \param rec record reference
2642 * \param handle transaction handler
2645 * \retval -ve failure
2648 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2649 const struct dt_key *key,
2650 struct thandle *handle,
2651 struct lustre_capa *capa)
2653 struct osd_object *obj = osd_dt_obj(dt);
2654 struct osd_thandle *oh;
2655 struct iam_path_descr *ipd;
2656 struct iam_container *bag = &obj->oo_dir->od_container;
2661 LINVRNT(osd_invariant(obj));
2662 LASSERT(dt_object_exists(dt));
2663 LASSERT(bag->ic_object == obj->oo_inode);
2664 LASSERT(handle != NULL);
2666 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2669 OSD_EXEC_OP(handle, delete);
2671 ipd = osd_idx_ipd_get(env, bag);
2672 if (unlikely(ipd == NULL))
2675 oh = container_of0(handle, struct osd_thandle, ot_super);
2676 LASSERT(oh->ot_handle != NULL);
2677 LASSERT(oh->ot_handle->h_transaction != NULL);
2679 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2680 osd_ipd_put(env, bag, ipd);
2681 LINVRNT(osd_invariant(obj));
2685 static int osd_index_declare_ea_delete(const struct lu_env *env,
2686 struct dt_object *dt,
2687 const struct dt_key *key,
2688 struct thandle *handle)
2690 struct osd_thandle *oh;
2692 LASSERT(dt_object_exists(dt));
2693 LASSERT(handle != NULL);
2695 oh = container_of0(handle, struct osd_thandle, ot_super);
2696 LASSERT(oh->ot_handle == NULL);
2698 OSD_DECLARE_OP(oh, delete);
2699 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2701 LASSERT(osd_dt_obj(dt)->oo_inode);
2702 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2703 osd_dt_obj(dt)->oo_inode);
2704 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2705 osd_dt_obj(dt)->oo_inode);
2710 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2713 struct osd_fid_pack *rec;
2716 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2717 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2718 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2724 * Index delete function for interoperability mode (b11826).
2725 * It will remove the directory entry added by osd_index_ea_insert().
2726 * This entry is needed to maintain name->fid mapping.
2728 * \param key, key i.e. file entry to be deleted
2730 * \retval 0, on success
2731 * \retval -ve, on error
2733 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2734 const struct dt_key *key,
2735 struct thandle *handle,
2736 struct lustre_capa *capa)
2738 struct osd_object *obj = osd_dt_obj(dt);
2739 struct inode *dir = obj->oo_inode;
2740 struct dentry *dentry;
2741 struct osd_thandle *oh;
2742 struct ldiskfs_dir_entry_2 *de;
2743 struct buffer_head *bh;
2744 struct htree_lock *hlock = NULL;
2749 LINVRNT(osd_invariant(obj));
2750 LASSERT(dt_object_exists(dt));
2751 LASSERT(handle != NULL);
2753 OSD_EXEC_OP(handle, delete);
2755 oh = container_of(handle, struct osd_thandle, ot_super);
2756 LASSERT(oh->ot_handle != NULL);
2757 LASSERT(oh->ot_handle->h_transaction != NULL);
2759 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2762 dentry = osd_child_dentry_get(env, obj,
2763 (char *)key, strlen((char *)key));
2765 if (obj->oo_hl_head != NULL) {
2766 hlock = osd_oti_get(env)->oti_hlock;
2767 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2768 dir, LDISKFS_HLOCK_DEL);
2770 cfs_down_write(&obj->oo_ext_idx_sem);
2773 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2775 rc = ldiskfs_delete_entry(oh->ot_handle,
2782 ldiskfs_htree_unlock(hlock);
2784 cfs_up_write(&obj->oo_ext_idx_sem);
2786 LASSERT(osd_invariant(obj));
2791 * Lookup index for \a key and copy record to \a rec.
2793 * \param dt osd index object
2794 * \param key key for index
2795 * \param rec record reference
2797 * \retval +ve success : exact mach
2798 * \retval 0 return record with key not greater than \a key
2799 * \retval -ve failure
2801 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2802 struct dt_rec *rec, const struct dt_key *key,
2803 struct lustre_capa *capa)
2805 struct osd_object *obj = osd_dt_obj(dt);
2806 struct iam_path_descr *ipd;
2807 struct iam_container *bag = &obj->oo_dir->od_container;
2808 struct osd_thread_info *oti = osd_oti_get(env);
2809 struct iam_iterator *it = &oti->oti_idx_it;
2810 struct iam_rec *iam_rec;
2815 LASSERT(osd_invariant(obj));
2816 LASSERT(dt_object_exists(dt));
2817 LASSERT(bag->ic_object == obj->oo_inode);
2819 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2822 ipd = osd_idx_ipd_get(env, bag);
2826 /* got ipd now we can start iterator. */
2827 iam_it_init(it, bag, 0, ipd);
2829 rc = iam_it_get(it, (struct iam_key *)key);
2831 if (S_ISDIR(obj->oo_inode->i_mode))
2832 iam_rec = (struct iam_rec *)oti->oti_ldp;
2834 iam_rec = (struct iam_rec *) rec;
2836 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2837 if (S_ISDIR(obj->oo_inode->i_mode))
2838 osd_fid_unpack((struct lu_fid *) rec,
2839 (struct osd_fid_pack *)iam_rec);
2843 osd_ipd_put(env, bag, ipd);
2845 LINVRNT(osd_invariant(obj));
2850 static int osd_index_declare_iam_insert(const struct lu_env *env,
2851 struct dt_object *dt,
2852 const struct dt_rec *rec,
2853 const struct dt_key *key,
2854 struct thandle *handle)
2856 struct osd_thandle *oh;
2858 LASSERT(dt_object_exists(dt));
2859 LASSERT(handle != NULL);
2861 oh = container_of0(handle, struct osd_thandle, ot_super);
2862 LASSERT(oh->ot_handle == NULL);
2864 OSD_DECLARE_OP(oh, insert);
2865 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
2871 * Inserts (key, value) pair in \a dt index object.
2873 * \param dt osd index object
2874 * \param key key for index
2875 * \param rec record reference
2876 * \param th transaction handler
2879 * \retval -ve failure
2881 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2882 const struct dt_rec *rec,
2883 const struct dt_key *key, struct thandle *th,
2884 struct lustre_capa *capa, int ignore_quota)
2886 struct osd_object *obj = osd_dt_obj(dt);
2887 struct iam_path_descr *ipd;
2888 struct osd_thandle *oh;
2889 struct iam_container *bag = &obj->oo_dir->od_container;
2890 #ifdef HAVE_QUOTA_SUPPORT
2891 cfs_cap_t save = cfs_curproc_cap_pack();
2893 struct osd_thread_info *oti = osd_oti_get(env);
2894 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2899 LINVRNT(osd_invariant(obj));
2900 LASSERT(dt_object_exists(dt));
2901 LASSERT(bag->ic_object == obj->oo_inode);
2902 LASSERT(th != NULL);
2904 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2907 OSD_EXEC_OP(th, insert);
2909 ipd = osd_idx_ipd_get(env, bag);
2910 if (unlikely(ipd == NULL))
2913 oh = container_of0(th, struct osd_thandle, ot_super);
2914 LASSERT(oh->ot_handle != NULL);
2915 LASSERT(oh->ot_handle->h_transaction != NULL);
2916 #ifdef HAVE_QUOTA_SUPPORT
2918 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2920 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2922 if (S_ISDIR(obj->oo_inode->i_mode))
2923 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2925 iam_rec = (struct iam_rec *) rec;
2926 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2928 #ifdef HAVE_QUOTA_SUPPORT
2929 cfs_curproc_cap_unpack(save);
2931 osd_ipd_put(env, bag, ipd);
2932 LINVRNT(osd_invariant(obj));
2937 * Calls ldiskfs_add_entry() to add directory entry
2938 * into the directory. This is required for
2939 * interoperability mode (b11826)
2941 * \retval 0, on success
2942 * \retval -ve, on error
2944 static int __osd_ea_add_rec(struct osd_thread_info *info,
2945 struct osd_object *pobj, struct inode *cinode,
2946 const char *name, const struct dt_rec *fid,
2947 struct htree_lock *hlock, struct thandle *th)
2949 struct ldiskfs_dentry_param *ldp;
2950 struct dentry *child;
2951 struct osd_thandle *oth;
2954 oth = container_of(th, struct osd_thandle, ot_super);
2955 LASSERT(oth->ot_handle != NULL);
2956 LASSERT(oth->ot_handle->h_transaction != NULL);
2958 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2960 if (fid_is_igif((struct lu_fid *)fid) ||
2961 fid_is_norm((struct lu_fid *)fid)) {
2962 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2963 osd_get_ldiskfs_dirent_param(ldp, fid);
2964 child->d_fsdata = (void*) ldp;
2966 child->d_fsdata = NULL;
2967 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
2973 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2974 * into the directory.Also sets flags into osd object to
2975 * indicate dot and dotdot are created. This is required for
2976 * interoperability mode (b11826)
2978 * \param dir directory for dot and dotdot fixup.
2979 * \param obj child object for linking
2981 * \retval 0, on success
2982 * \retval -ve, on error
2984 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2985 struct osd_object *dir,
2986 struct inode *parent_dir, const char *name,
2987 const struct dt_rec *dot_fid,
2988 const struct dt_rec *dot_dot_fid,
2991 struct inode *inode = dir->oo_inode;
2992 struct ldiskfs_dentry_param *dot_ldp;
2993 struct ldiskfs_dentry_param *dot_dot_ldp;
2994 struct osd_thandle *oth;
2997 oth = container_of(th, struct osd_thandle, ot_super);
2998 LASSERT(oth->ot_handle->h_transaction != NULL);
2999 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3001 if (strcmp(name, dot) == 0) {
3002 if (dir->oo_compat_dot_created) {
3005 LASSERT(inode == parent_dir);
3006 dir->oo_compat_dot_created = 1;
3009 } else if(strcmp(name, dotdot) == 0) {
3010 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3011 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3013 if (!dir->oo_compat_dot_created)
3015 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
3016 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3017 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3022 /* in case of rename, dotdot is already created */
3023 if (dir->oo_compat_dotdot_created) {
3024 return __osd_ea_add_rec(info, dir, parent_dir, name,
3025 dot_dot_fid, NULL, th);
3028 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3029 inode, dot_ldp, dot_dot_ldp);
3031 dir->oo_compat_dotdot_created = 1;
3039 * It will call the appropriate osd_add* function and return the
3040 * value, return by respective functions.
3042 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3043 struct inode *cinode, const char *name,
3044 const struct dt_rec *fid, struct thandle *th)
3046 struct osd_thread_info *info = osd_oti_get(env);
3047 struct htree_lock *hlock;
3050 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3052 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3054 if (hlock != NULL) {
3055 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3058 cfs_down_write(&pobj->oo_ext_idx_sem);
3060 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3061 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3064 if (hlock != NULL) {
3065 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3066 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3068 cfs_down_write(&pobj->oo_ext_idx_sem);
3071 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3075 ldiskfs_htree_unlock(hlock);
3077 cfs_up_write(&pobj->oo_ext_idx_sem);
3083 * Calls ->lookup() to find dentry. From dentry get inode and
3084 * read inode's ea to get fid. This is required for interoperability
3087 * \retval 0, on success
3088 * \retval -ve, on error
3090 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3091 struct dt_rec *rec, const struct dt_key *key)
3093 struct inode *dir = obj->oo_inode;
3094 struct dentry *dentry;
3095 struct ldiskfs_dir_entry_2 *de;
3096 struct buffer_head *bh;
3097 struct lu_fid *fid = (struct lu_fid *) rec;
3098 struct htree_lock *hlock = NULL;
3102 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3104 dentry = osd_child_dentry_get(env, obj,
3105 (char *)key, strlen((char *)key));
3107 if (obj->oo_hl_head != NULL) {
3108 hlock = osd_oti_get(env)->oti_hlock;
3109 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3110 dir, LDISKFS_HLOCK_LOOKUP);
3112 cfs_down_read(&obj->oo_ext_idx_sem);
3115 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3117 ino = le32_to_cpu(de->inode);
3118 rc = osd_get_fid_from_dentry(de, rec);
3120 /* done with de, release bh */
3123 rc = osd_ea_fid_get(env, obj, ino, fid);
3129 ldiskfs_htree_unlock(hlock);
3131 cfs_up_read(&obj->oo_ext_idx_sem);
3136 * Find the osd object for given fid.
3138 * \param fid need to find the osd object having this fid
3140 * \retval osd_object on success
3141 * \retval -ve on error
3143 struct osd_object *osd_object_find(const struct lu_env *env,
3144 struct dt_object *dt,
3145 const struct lu_fid *fid)
3147 struct lu_device *ludev = dt->do_lu.lo_dev;
3148 struct osd_object *child = NULL;
3149 struct lu_object *luch;
3150 struct lu_object *lo;
3152 luch = lu_object_find(env, ludev, fid, NULL);
3153 if (!IS_ERR(luch)) {
3154 if (lu_object_exists(luch)) {
3155 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3157 child = osd_obj(lo);
3159 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3160 "lu_object can't be located"
3161 ""DFID"\n", PFID(fid));
3163 if (child == NULL) {
3164 lu_object_put(env, luch);
3165 CERROR("Unable to get osd_object\n");
3166 child = ERR_PTR(-ENOENT);
3169 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3170 "lu_object does not exists "DFID"\n",
3172 child = ERR_PTR(-ENOENT);
3175 child = (void *)luch;
3181 * Put the osd object once done with it.
3183 * \param obj osd object that needs to be put
3185 static inline void osd_object_put(const struct lu_env *env,
3186 struct osd_object *obj)
3188 lu_object_put(env, &obj->oo_dt.do_lu);
3191 static int osd_index_declare_ea_insert(const struct lu_env *env,
3192 struct dt_object *dt,
3193 const struct dt_rec *rec,
3194 const struct dt_key *key,
3195 struct thandle *handle)
3197 struct osd_thandle *oh;
3199 LASSERT(dt_object_exists(dt));
3200 LASSERT(handle != NULL);
3202 oh = container_of0(handle, struct osd_thandle, ot_super);
3203 LASSERT(oh->ot_handle == NULL);
3205 OSD_DECLARE_OP(oh, insert);
3206 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3208 LASSERT(osd_dt_obj(dt)->oo_inode);
3209 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3210 osd_dt_obj(dt)->oo_inode);
3211 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3212 osd_dt_obj(dt)->oo_inode);
3218 * Index add function for interoperability mode (b11826).
3219 * It will add the directory entry.This entry is needed to
3220 * maintain name->fid mapping.
3222 * \param key it is key i.e. file entry to be inserted
3223 * \param rec it is value of given key i.e. fid
3225 * \retval 0, on success
3226 * \retval -ve, on error
3228 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3229 const struct dt_rec *rec,
3230 const struct dt_key *key, struct thandle *th,
3231 struct lustre_capa *capa, int ignore_quota)
3233 struct osd_object *obj = osd_dt_obj(dt);
3234 struct lu_fid *fid = (struct lu_fid *) rec;
3235 const char *name = (const char *)key;
3236 struct osd_object *child;
3237 #ifdef HAVE_QUOTA_SUPPORT
3238 cfs_cap_t save = cfs_curproc_cap_pack();
3244 LASSERT(osd_invariant(obj));
3245 LASSERT(dt_object_exists(dt));
3246 LASSERT(th != NULL);
3248 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3251 child = osd_object_find(env, dt, fid);
3252 if (!IS_ERR(child)) {
3253 #ifdef HAVE_QUOTA_SUPPORT
3255 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3257 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3259 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3260 #ifdef HAVE_QUOTA_SUPPORT
3261 cfs_curproc_cap_unpack(save);
3263 osd_object_put(env, child);
3265 rc = PTR_ERR(child);
3268 LASSERT(osd_invariant(obj));
3273 * Initialize osd Iterator for given osd index object.
3275 * \param dt osd index object
3278 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3279 struct dt_object *dt,
3281 struct lustre_capa *capa)
3283 struct osd_it_iam *it;
3284 struct osd_thread_info *oti = osd_oti_get(env);
3285 struct osd_object *obj = osd_dt_obj(dt);
3286 struct lu_object *lo = &dt->do_lu;
3287 struct iam_path_descr *ipd;
3288 struct iam_container *bag = &obj->oo_dir->od_container;
3290 LASSERT(lu_object_exists(lo));
3292 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3293 return ERR_PTR(-EACCES);
3296 ipd = osd_it_ipd_get(env, bag);
3297 if (likely(ipd != NULL)) {
3301 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3302 return (struct dt_it *)it;
3304 return ERR_PTR(-ENOMEM);
3308 * free given Iterator.
3311 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3313 struct osd_it_iam *it = (struct osd_it_iam *)di;
3314 struct osd_object *obj = it->oi_obj;
3316 iam_it_fini(&it->oi_it);
3317 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3318 lu_object_put(env, &obj->oo_dt.do_lu);
3322 * Move Iterator to record specified by \a key
3324 * \param di osd iterator
3325 * \param key key for index
3327 * \retval +ve di points to record with least key not larger than key
3328 * \retval 0 di points to exact matched key
3329 * \retval -ve failure
3332 static int osd_it_iam_get(const struct lu_env *env,
3333 struct dt_it *di, const struct dt_key *key)
3335 struct osd_it_iam *it = (struct osd_it_iam *)di;
3337 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3343 * \param di osd iterator
3346 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3348 struct osd_it_iam *it = (struct osd_it_iam *)di;
3350 iam_it_put(&it->oi_it);
3354 * Move iterator by one record
3356 * \param di osd iterator
3358 * \retval +1 end of container reached
3360 * \retval -ve failure
3363 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3365 struct osd_it_iam *it = (struct osd_it_iam *)di;
3367 return iam_it_next(&it->oi_it);
3371 * Return pointer to the key under iterator.
3374 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3375 const struct dt_it *di)
3377 struct osd_it_iam *it = (struct osd_it_iam *)di;
3379 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3383 * Return size of key under iterator (in bytes)
3386 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3388 struct osd_it_iam *it = (struct osd_it_iam *)di;
3390 return iam_it_key_size(&it->oi_it);
3393 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3394 int len, __u16 type)
3396 struct luda_type *lt;
3397 const unsigned align = sizeof(struct luda_type) - 1;
3399 /* check if file type is required */
3400 if (attr & LUDA_TYPE) {
3401 len = (len + align) & ~align;
3403 lt = (void *) ent->lde_name + len;
3404 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3405 ent->lde_attrs |= LUDA_TYPE;
3408 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3412 * build lu direct from backend fs dirent.
3415 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3416 struct lu_fid *fid, __u64 offset,
3417 char *name, __u16 namelen,
3418 __u16 type, __u32 attr)
3420 fid_cpu_to_le(&ent->lde_fid, fid);
3421 ent->lde_attrs = LUDA_FID;
3423 ent->lde_hash = cpu_to_le64(offset);
3424 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3426 strncpy(ent->lde_name, name, namelen);
3427 ent->lde_namelen = cpu_to_le16(namelen);
3429 /* append lustre attributes */
3430 osd_it_append_attrs(ent, attr, namelen, type);
3434 * Return pointer to the record under iterator.
3436 static int osd_it_iam_rec(const struct lu_env *env,
3437 const struct dt_it *di,
3438 struct dt_rec *dtrec, __u32 attr)
3440 struct osd_it_iam *it = (struct osd_it_iam *)di;
3441 struct osd_thread_info *info = osd_oti_get(env);
3442 struct lu_fid *fid = &info->oti_fid;
3443 const struct osd_fid_pack *rec;
3444 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3450 name = (char *)iam_it_key_get(&it->oi_it);
3452 RETURN(PTR_ERR(name));
3454 namelen = iam_it_key_size(&it->oi_it);
3456 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3458 RETURN(PTR_ERR(rec));
3460 rc = osd_fid_unpack(fid, rec);
3464 hash = iam_it_store(&it->oi_it);
3466 /* IAM does not store object type in IAM index (dir) */
3467 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3474 * Returns cookie for current Iterator position.
3476 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3478 struct osd_it_iam *it = (struct osd_it_iam *)di;
3480 return iam_it_store(&it->oi_it);
3484 * Restore iterator from cookie.
3486 * \param di osd iterator
3487 * \param hash Iterator location cookie
3489 * \retval +ve di points to record with least key not larger than key.
3490 * \retval 0 di points to exact matched key
3491 * \retval -ve failure
3494 static int osd_it_iam_load(const struct lu_env *env,
3495 const struct dt_it *di, __u64 hash)
3497 struct osd_it_iam *it = (struct osd_it_iam *)di;
3499 return iam_it_load(&it->oi_it, hash);
3502 static const struct dt_index_operations osd_index_iam_ops = {
3503 .dio_lookup = osd_index_iam_lookup,
3504 .dio_declare_insert = osd_index_declare_iam_insert,
3505 .dio_insert = osd_index_iam_insert,
3506 .dio_declare_delete = osd_index_declare_iam_delete,
3507 .dio_delete = osd_index_iam_delete,
3509 .init = osd_it_iam_init,
3510 .fini = osd_it_iam_fini,
3511 .get = osd_it_iam_get,
3512 .put = osd_it_iam_put,
3513 .next = osd_it_iam_next,
3514 .key = osd_it_iam_key,
3515 .key_size = osd_it_iam_key_size,
3516 .rec = osd_it_iam_rec,
3517 .store = osd_it_iam_store,
3518 .load = osd_it_iam_load
3523 * Creates or initializes iterator context.
3525 * \retval struct osd_it_ea, iterator structure on success
3528 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3529 struct dt_object *dt,
3531 struct lustre_capa *capa)
3533 struct osd_object *obj = osd_dt_obj(dt);
3534 struct osd_thread_info *info = osd_oti_get(env);
3535 struct osd_it_ea *it = &info->oti_it_ea;
3536 struct lu_object *lo = &dt->do_lu;
3537 struct dentry *obj_dentry = &info->oti_it_dentry;
3539 LASSERT(lu_object_exists(lo));
3541 obj_dentry->d_inode = obj->oo_inode;
3542 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3543 obj_dentry->d_name.hash = 0;
3545 it->oie_rd_dirent = 0;
3546 it->oie_it_dirent = 0;
3547 it->oie_dirent = NULL;
3548 it->oie_buf = info->oti_it_ea_buf;
3550 it->oie_file.f_pos = 0;
3551 it->oie_file.f_dentry = obj_dentry;
3552 if (attr & LUDA_64BITHASH)
3553 it->oie_file.f_flags = O_64BITHASH;
3555 it->oie_file.f_flags = O_32BITHASH;
3556 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3557 it->oie_file.f_op = obj->oo_inode->i_fop;
3558 it->oie_file.private_data = NULL;
3560 RETURN((struct dt_it *) it);
3564 * Destroy or finishes iterator context.
3566 * \param di iterator structure to be destroyed
3568 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3570 struct osd_it_ea *it = (struct osd_it_ea *)di;
3571 struct osd_object *obj = it->oie_obj;
3572 struct inode *inode = obj->oo_inode;
3575 it->oie_file.f_op->release(inode, &it->oie_file);
3576 lu_object_put(env, &obj->oo_dt.do_lu);
3581 * It position the iterator at given key, so that next lookup continues from
3582 * that key Or it is similar to dio_it->load() but based on a key,
3583 * rather than file position.
3585 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3588 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3590 static int osd_it_ea_get(const struct lu_env *env,
3591 struct dt_it *di, const struct dt_key *key)
3593 struct osd_it_ea *it = (struct osd_it_ea *)di;
3596 LASSERT(((const char *)key)[0] == '\0');
3597 it->oie_file.f_pos = 0;
3598 it->oie_rd_dirent = 0;
3599 it->oie_it_dirent = 0;
3600 it->oie_dirent = NULL;
3608 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3613 * It is called internally by ->readdir(). It fills the
3614 * iterator's in-memory data structure with required
3615 * information i.e. name, namelen, rec_size etc.
3617 * \param buf in which information to be filled in.
3618 * \param name name of the file in given dir
3620 * \retval 0 on success
3621 * \retval 1 on buffer full
3623 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3624 loff_t offset, __u64 ino,
3627 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3628 struct osd_it_ea_dirent *ent = it->oie_dirent;
3629 struct lu_fid *fid = &ent->oied_fid;
3630 struct osd_fid_pack *rec;
3633 /* this should never happen */
3634 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3635 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3639 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3643 if (d_type & LDISKFS_DIRENT_LUFID) {
3644 rec = (struct osd_fid_pack*) (name + namelen + 1);
3646 if (osd_fid_unpack(fid, rec) != 0)
3649 d_type &= ~LDISKFS_DIRENT_LUFID;
3654 ent->oied_ino = ino;
3655 ent->oied_off = offset;
3656 ent->oied_namelen = namelen;
3657 ent->oied_type = d_type;
3659 memcpy(ent->oied_name, name, namelen);
3661 it->oie_rd_dirent++;
3662 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3667 * Calls ->readdir() to load a directory entry at a time
3668 * and stored it in iterator's in-memory data structure.
3670 * \param di iterator's in memory structure
3672 * \retval 0 on success
3673 * \retval -ve on error
3675 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3676 const struct dt_it *di)
3678 struct osd_it_ea *it = (struct osd_it_ea *)di;
3679 struct osd_object *obj = it->oie_obj;
3680 struct inode *inode = obj->oo_inode;
3681 struct htree_lock *hlock = NULL;
3685 it->oie_dirent = it->oie_buf;
3686 it->oie_rd_dirent = 0;
3688 if (obj->oo_hl_head != NULL) {
3689 hlock = osd_oti_get(env)->oti_hlock;
3690 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3691 inode, LDISKFS_HLOCK_READDIR);
3693 cfs_down_read(&obj->oo_ext_idx_sem);
3696 result = inode->i_fop->readdir(&it->oie_file, it,
3697 (filldir_t) osd_ldiskfs_filldir);
3700 ldiskfs_htree_unlock(hlock);
3702 cfs_up_read(&obj->oo_ext_idx_sem);
3704 if (it->oie_rd_dirent == 0) {
3707 it->oie_dirent = it->oie_buf;
3708 it->oie_it_dirent = 1;
3715 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3716 * to load a directory entry at a time and stored it in
3717 * iterator's in-memory data structure.
3719 * \param di iterator's in memory structure
3721 * \retval +ve iterator reached to end
3722 * \retval 0 iterator not reached to end
3723 * \retval -ve on error
3725 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3727 struct osd_it_ea *it = (struct osd_it_ea *)di;
3732 if (it->oie_it_dirent < it->oie_rd_dirent) {
3734 (void *) it->oie_dirent +
3735 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3736 it->oie_dirent->oied_namelen);
3737 it->oie_it_dirent++;
3740 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3743 rc = osd_ldiskfs_it_fill(env, di);
3750 * Returns the key at current position from iterator's in memory structure.
3752 * \param di iterator's in memory structure
3754 * \retval key i.e. struct dt_key on success
3756 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3757 const struct dt_it *di)
3759 struct osd_it_ea *it = (struct osd_it_ea *)di;
3761 return (struct dt_key *)it->oie_dirent->oied_name;
3765 * Returns the key's size at current position from iterator's in memory structure.
3767 * \param di iterator's in memory structure
3769 * \retval key_size i.e. struct dt_key on success
3771 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3773 struct osd_it_ea *it = (struct osd_it_ea *)di;
3775 return it->oie_dirent->oied_namelen;
3780 * Returns the value (i.e. fid/igif) at current position from iterator's
3781 * in memory structure.
3783 * \param di struct osd_it_ea, iterator's in memory structure
3784 * \param attr attr requested for dirent.
3785 * \param lde lustre dirent
3787 * \retval 0 no error and \param lde has correct lustre dirent.
3788 * \retval -ve on error
3790 static inline int osd_it_ea_rec(const struct lu_env *env,
3791 const struct dt_it *di,
3792 struct dt_rec *dtrec, __u32 attr)
3794 struct osd_it_ea *it = (struct osd_it_ea *)di;
3795 struct osd_object *obj = it->oie_obj;
3796 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3797 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3802 if (!fid_is_sane(fid))
3803 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3806 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3807 it->oie_dirent->oied_name,
3808 it->oie_dirent->oied_namelen,
3809 it->oie_dirent->oied_type,
3815 * Returns a cookie for current position of the iterator head, so that
3816 * user can use this cookie to load/start the iterator next time.
3818 * \param di iterator's in memory structure
3820 * \retval cookie for current position, on success
3822 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3824 struct osd_it_ea *it = (struct osd_it_ea *)di;
3826 return it->oie_dirent->oied_off;
3830 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3831 * to load a directory entry at a time and stored it i inn,
3832 * in iterator's in-memory data structure.
3834 * \param di struct osd_it_ea, iterator's in memory structure
3836 * \retval +ve on success
3837 * \retval -ve on error
3839 static int osd_it_ea_load(const struct lu_env *env,
3840 const struct dt_it *di, __u64 hash)
3842 struct osd_it_ea *it = (struct osd_it_ea *)di;
3846 it->oie_file.f_pos = hash;
3848 rc = osd_ldiskfs_it_fill(env, di);
3856 * Index lookup function for interoperability mode (b11826).
3858 * \param key, key i.e. file name to be searched
3860 * \retval +ve, on success
3861 * \retval -ve, on error
3863 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3864 struct dt_rec *rec, const struct dt_key *key,
3865 struct lustre_capa *capa)
3867 struct osd_object *obj = osd_dt_obj(dt);
3872 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3873 LINVRNT(osd_invariant(obj));
3875 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3878 rc = osd_ea_lookup_rec(env, obj, rec, key);
3886 * Index and Iterator operations for interoperability
3887 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3889 static const struct dt_index_operations osd_index_ea_ops = {
3890 .dio_lookup = osd_index_ea_lookup,
3891 .dio_declare_insert = osd_index_declare_ea_insert,
3892 .dio_insert = osd_index_ea_insert,
3893 .dio_declare_delete = osd_index_declare_ea_delete,
3894 .dio_delete = osd_index_ea_delete,
3896 .init = osd_it_ea_init,
3897 .fini = osd_it_ea_fini,
3898 .get = osd_it_ea_get,
3899 .put = osd_it_ea_put,
3900 .next = osd_it_ea_next,
3901 .key = osd_it_ea_key,
3902 .key_size = osd_it_ea_key_size,
3903 .rec = osd_it_ea_rec,
3904 .store = osd_it_ea_store,
3905 .load = osd_it_ea_load
3909 static void *osd_key_init(const struct lu_context *ctx,
3910 struct lu_context_key *key)
3912 struct osd_thread_info *info;
3914 OBD_ALLOC_PTR(info);
3916 return ERR_PTR(-ENOMEM);
3918 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3919 if (info->oti_it_ea_buf == NULL)
3922 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
3924 info->oti_hlock = ldiskfs_htree_lock_alloc();
3925 if (info->oti_hlock == NULL)
3931 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3934 return ERR_PTR(-ENOMEM);
3937 static void osd_key_fini(const struct lu_context *ctx,
3938 struct lu_context_key *key, void* data)
3940 struct osd_thread_info *info = data;
3942 if (info->oti_hlock != NULL)
3943 ldiskfs_htree_lock_free(info->oti_hlock);
3944 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3948 static void osd_key_exit(const struct lu_context *ctx,
3949 struct lu_context_key *key, void *data)
3951 struct osd_thread_info *info = data;
3953 LASSERT(info->oti_r_locks == 0);
3954 LASSERT(info->oti_w_locks == 0);
3955 LASSERT(info->oti_txns == 0);
3958 /* type constructor/destructor: osd_type_init, osd_type_fini */
3959 LU_TYPE_INIT_FINI(osd, &osd_key);
3961 struct lu_context_key osd_key = {
3962 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3963 .lct_init = osd_key_init,
3964 .lct_fini = osd_key_fini,
3965 .lct_exit = osd_key_exit
3969 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3970 const char *name, struct lu_device *next)
3972 return osd_procfs_init(osd_dev(d), name);
3975 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3977 struct osd_thread_info *info = osd_oti_get(env);
3979 if (o->od_obj_area != NULL) {
3980 lu_object_put(env, &o->od_obj_area->do_lu);
3981 o->od_obj_area = NULL;
3983 if (o->od_oi_table != NULL)
3984 osd_oi_fini(info, o);
3987 fsfilt_put_ops(o->od_fsops);
3994 static int osd_mount(const struct lu_env *env,
3995 struct osd_device *o, struct lustre_cfg *cfg)
3997 struct lustre_mount_info *lmi;
3998 const char *dev = lustre_cfg_string(cfg, 0);
3999 struct lustre_disk_data *ldd;
4000 struct lustre_sb_info *lsi;
4004 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4005 if (o->od_fsops == NULL) {
4006 CERROR("Can't find fsfilt_ldiskfs\n");
4010 if (o->od_mount != NULL) {
4011 CERROR("Already mounted (%s)\n", dev);
4016 lmi = server_get_mount(dev);
4018 CERROR("Cannot get mount info for %s!\n", dev);
4022 LASSERT(lmi != NULL);
4023 /* save lustre_mount_info in dt_device */
4026 lsi = s2lsi(lmi->lmi_sb);
4029 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4031 LCONSOLE_WARN("OSD: IAM mode enabled\n");
4035 o->od_obj_area = NULL;
4039 static struct lu_device *osd_device_fini(const struct lu_env *env,
4040 struct lu_device *d)
4045 shrink_dcache_sb(osd_sb(osd_dev(d)));
4046 osd_sync(env, lu2dt_dev(d));
4048 rc = osd_procfs_fini(osd_dev(d));
4050 CERROR("proc fini error %d \n", rc);
4051 RETURN (ERR_PTR(rc));
4054 if (osd_dev(d)->od_mount)
4055 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4056 osd_dev(d)->od_mount->lmi_mnt);
4057 osd_dev(d)->od_mount = NULL;
4062 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4063 struct lu_device_type *t,
4064 struct lustre_cfg *cfg)
4066 struct lu_device *l;
4067 struct osd_device *o;
4073 result = dt_device_init(&o->od_dt_dev, t);
4076 l->ld_ops = &osd_lu_ops;
4077 o->od_dt_dev.dd_ops = &osd_dt_ops;
4078 cfs_spin_lock_init(&o->od_osfs_lock);
4079 o->od_osfs_age = cfs_time_shift_64(-1000);
4080 o->od_capa_hash = init_capa_hash();
4081 if (o->od_capa_hash == NULL) {
4082 dt_device_fini(&o->od_dt_dev);
4083 l = ERR_PTR(-ENOMEM);
4086 l = ERR_PTR(result);
4091 l = ERR_PTR(-ENOMEM);
4095 static struct lu_device *osd_device_free(const struct lu_env *env,
4096 struct lu_device *d)
4098 struct osd_device *o = osd_dev(d);
4101 cleanup_capa_hash(o->od_capa_hash);
4102 dt_device_fini(&o->od_dt_dev);
4107 static int osd_process_config(const struct lu_env *env,
4108 struct lu_device *d, struct lustre_cfg *cfg)
4110 struct osd_device *o = osd_dev(d);
4114 switch(cfg->lcfg_command) {
4116 err = osd_mount(env, o, cfg);
4119 err = osd_shutdown(env, o);
4128 static int osd_recovery_complete(const struct lu_env *env,
4129 struct lu_device *d)
4134 static int osd_prepare(const struct lu_env *env,
4135 struct lu_device *pdev,
4136 struct lu_device *dev)
4138 struct osd_device *osd = osd_dev(dev);
4139 struct lustre_sb_info *lsi;
4140 struct lustre_disk_data *ldd;
4141 struct lustre_mount_info *lmi;
4142 struct osd_thread_info *oti = osd_oti_get(env);
4143 struct dt_object *d;
4147 /* 1. initialize oi before any file create or file open */
4148 result = osd_oi_init(oti, osd);
4152 if (!lu_device_is_md(pdev))
4155 lmi = osd->od_mount;
4156 lsi = s2lsi(lmi->lmi_sb);
4159 /* 2. setup local objects */
4160 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4164 /* 3. open remote object dir */
4165 d = dt_store_open(env, lu2dt_dev(dev), "",
4166 remote_obj_dir, &oti->oti_fid);
4168 osd->od_obj_area = d;
4171 result = PTR_ERR(d);
4172 osd->od_obj_area = NULL;
4179 static const struct lu_object_operations osd_lu_obj_ops = {
4180 .loo_object_init = osd_object_init,
4181 .loo_object_delete = osd_object_delete,
4182 .loo_object_release = osd_object_release,
4183 .loo_object_free = osd_object_free,
4184 .loo_object_print = osd_object_print,
4185 .loo_object_invariant = osd_object_invariant
4188 const struct lu_device_operations osd_lu_ops = {
4189 .ldo_object_alloc = osd_object_alloc,
4190 .ldo_process_config = osd_process_config,
4191 .ldo_recovery_complete = osd_recovery_complete,
4192 .ldo_prepare = osd_prepare,
4195 static const struct lu_device_type_operations osd_device_type_ops = {
4196 .ldto_init = osd_type_init,
4197 .ldto_fini = osd_type_fini,
4199 .ldto_start = osd_type_start,
4200 .ldto_stop = osd_type_stop,
4202 .ldto_device_alloc = osd_device_alloc,
4203 .ldto_device_free = osd_device_free,
4205 .ldto_device_init = osd_device_init,
4206 .ldto_device_fini = osd_device_fini
4209 static struct lu_device_type osd_device_type = {
4210 .ldt_tags = LU_DEVICE_DT,
4211 .ldt_name = LUSTRE_OSD_NAME,
4212 .ldt_ops = &osd_device_type_ops,
4213 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4217 * lprocfs legacy support.
4219 static struct obd_ops osd_obd_device_ops = {
4220 .o_owner = THIS_MODULE
4223 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4224 .llod_name = remote_obj_dir,
4225 .llod_oid = OSD_REM_OBJ_DIR_OID,
4227 .llod_feat = &dt_directory_features,
4230 static int __init osd_mod_init(void)
4232 struct lprocfs_static_vars lvars;
4235 llo_local_obj_register(&llod_osd_rem_obj_dir);
4236 lprocfs_osd_init_vars(&lvars);
4237 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4238 LUSTRE_OSD_NAME, &osd_device_type);
4241 static void __exit osd_mod_exit(void)
4243 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4244 class_unregister_type(LUSTRE_OSD_NAME);
4247 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4248 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4249 MODULE_LICENSE("GPL");
4251 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);