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 static 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);
319 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
322 result = osd_oi_lookup(info, osd_fid2oi(dev, fid), fid, id);
324 if (result == -ENOENT)
329 inode = osd_iget(info, dev, id);
332 * If fid wasn't found in oi, inode-less object is
333 * created, for which lu_object_exists() returns
334 * false. This is used in a (frequent) case when
335 * objects are created as locking anchors or
336 * place holders for objects yet to be created.
338 result = PTR_ERR(inode);
342 obj->oo_inode = inode;
343 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
344 if (dev->od_iop_mode) {
345 obj->oo_compat_dot_created = 1;
346 obj->oo_compat_dotdot_created = 1;
349 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
352 LASSERT(obj->oo_hl_head == NULL);
353 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
354 if (obj->oo_hl_head == NULL) {
355 obj->oo_inode = NULL;
360 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);
417 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
418 const struct iam_container *bag)
420 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
421 osd_oti_get(env)->oti_it_ipd);
424 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
425 const struct iam_container *bag)
427 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
428 osd_oti_get(env)->oti_idx_ipd);
431 static void osd_ipd_put(const struct lu_env *env,
432 const struct iam_container *bag,
433 struct iam_path_descr *ipd)
435 bag->ic_descr->id_ops->id_ipd_free(ipd);
439 * Concurrency: no concurrent access is possible that late in object
442 static void osd_index_fini(struct osd_object *o)
444 struct iam_container *bag;
446 if (o->oo_dir != NULL) {
447 bag = &o->oo_dir->od_container;
448 if (o->oo_inode != NULL) {
449 if (bag->ic_object == o->oo_inode)
450 iam_container_fini(bag);
452 OBD_FREE_PTR(o->oo_dir);
458 * Concurrency: no concurrent access is possible that late in object
459 * life-cycle (for all existing callers, that is. New callers have to provide
460 * their own locking.)
462 static int osd_inode_unlinked(const struct inode *inode)
464 return inode->i_nlink == 0;
468 OSD_TXN_OI_DELETE_CREDITS = 20,
469 OSD_TXN_INODE_DELETE_CREDITS = 20
476 #if OSD_THANDLE_STATS
478 * Set time when the handle is allocated
480 static void osd_th_alloced(struct osd_thandle *oth)
482 oth->oth_alloced = cfs_time_current();
486 * Set time when the handle started
488 static void osd_th_started(struct osd_thandle *oth)
490 oth->oth_started = cfs_time_current();
494 * Helper function to convert time interval to microseconds packed in
495 * long int (default time units for the counter in "stats" initialized
496 * by lu_time_init() )
498 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
502 cfs_duration_usec(cfs_time_sub(end, start), &val);
503 return val.tv_sec * 1000000 + val.tv_usec;
507 * Check whether the we deal with this handle for too long.
509 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
510 cfs_time_t alloced, cfs_time_t started,
513 cfs_time_t now = cfs_time_current();
515 LASSERT(dev != NULL);
517 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
518 interval_to_usec(alloced, started));
519 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
520 interval_to_usec(started, closed));
521 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
522 interval_to_usec(closed, now));
524 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
525 CWARN("transaction handle %p was open for too long: "
527 "alloced "CFS_TIME_T" ,"
528 "started "CFS_TIME_T" ,"
529 "closed "CFS_TIME_T"\n",
530 oth, now, alloced, started, closed);
531 libcfs_debug_dumpstack(NULL);
535 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
537 cfs_time_t __closed = cfs_time_current(); \
538 cfs_time_t __alloced = oth->oth_alloced; \
539 cfs_time_t __started = oth->oth_started; \
542 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
545 #else /* OSD_THANDLE_STATS */
547 #define osd_th_alloced(h) do {} while(0)
548 #define osd_th_started(h) do {} while(0)
549 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
551 #endif /* OSD_THANDLE_STATS */
554 * Concurrency: doesn't access mutable data.
556 static int osd_param_is_sane(const struct osd_device *dev,
557 const struct thandle *th)
559 struct osd_thandle *oh;
560 oh = container_of0(th, struct osd_thandle, ot_super);
561 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
565 * Concurrency: shouldn't matter.
567 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
568 static void osd_trans_commit_cb(struct super_block *sb,
569 struct journal_callback *jcb, int error)
571 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
574 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
575 struct thandle *th = &oh->ot_super;
576 struct lu_device *lud = &th->th_dev->dd_lu_dev;
577 struct dt_txn_commit_cb *dcb, *tmp;
579 LASSERT(oh->ot_handle == NULL);
582 CERROR("transaction @0x%p commit error: %d\n", th, error);
584 dt_txn_hook_commit(th);
586 /* call per-transaction callbacks if any */
587 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage)
588 dcb->dcb_func(NULL, th, dcb, error);
590 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
594 lu_context_exit(&th->th_ctx);
595 lu_context_fini(&th->th_ctx);
599 static struct thandle *osd_trans_create(const struct lu_env *env,
602 struct osd_thread_info *oti = osd_oti_get(env);
603 struct osd_iobuf *iobuf = &oti->oti_iobuf;
604 struct osd_thandle *oh;
608 /* on pending IO in this thread should left from prev. request */
609 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
611 th = ERR_PTR(-ENOMEM);
612 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
617 th->th_tags = LCT_TX_HANDLE;
619 oti->oti_dev = osd_dt_dev(d);
620 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
627 * Concurrency: shouldn't matter.
629 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
632 struct osd_thread_info *oti = osd_oti_get(env);
633 struct osd_device *dev = osd_dt_dev(d);
635 struct osd_thandle *oh;
640 LASSERT(current->journal_info == NULL);
642 oh = container_of0(th, struct osd_thandle, ot_super);
644 LASSERT(oh->ot_handle == NULL);
646 rc = dt_txn_hook_start(env, d, th);
650 if (!osd_param_is_sane(dev, th)) {
651 CWARN("%s: too many transaction credits (%d > %d)\n",
652 d->dd_lu_dev.ld_obd->obd_name, oh->ot_credits,
653 osd_journal(dev)->j_max_transaction_buffers);
654 /* XXX Limit the credits to 'max_transaction_buffers', and
655 * let the underlying filesystem to catch the error if
656 * we really need so many credits.
658 * This should be removed when we can calculate the
659 * credits precisely. */
660 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
661 #ifdef OSD_TRACK_DECLARES
662 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
663 oh->ot_declare_attr_set, oh->ot_declare_punch,
664 oh->ot_declare_xattr_set);
665 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
666 oh->ot_declare_create, oh->ot_declare_ref_add,
667 oh->ot_declare_ref_del, oh->ot_declare_write);
668 CERROR(" insert: %d, delete: %d, destroy: %d\n",
669 oh->ot_declare_insert, oh->ot_declare_delete,
670 oh->ot_declare_destroy);
675 * XXX temporary stuff. Some abstraction layer should
678 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
682 LASSERT(oti->oti_txns == 0);
683 lu_context_init(&th->th_ctx, th->th_tags);
684 lu_context_enter(&th->th_ctx);
686 lu_device_get(&d->dd_lu_dev);
687 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
691 * XXX: current rule is that we first start tx,
692 * then lock object(s), but we can't use
693 * this rule for data (due to locking specifics
694 * in ldiskfs). also in long-term we'd like to
695 * use usually-used (locks;tx) ordering. so,
696 * UGLY thing is that we'll use one ordering for
697 * data (ofd) and reverse ordering for metadata
698 * (mdd). then at some point we'll fix the latter
700 if (lu_device_is_md(&d->dd_lu_dev)) {
701 LASSERT(oti->oti_r_locks == 0);
702 LASSERT(oti->oti_w_locks == 0);
715 * Concurrency: shouldn't matter.
717 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
720 struct osd_thandle *oh;
721 struct osd_thread_info *oti = osd_oti_get(env);
722 struct osd_iobuf *iobuf = &oti->oti_iobuf;
726 oh = container_of0(th, struct osd_thandle, ot_super);
728 if (oh->ot_handle != NULL) {
729 handle_t *hdl = oh->ot_handle;
731 hdl->h_sync = th->th_sync;
734 * add commit callback
735 * notice we don't do this in osd_trans_start()
736 * as underlying transaction can change during truncate
738 osd_journal_callback_set(hdl, osd_trans_commit_cb,
741 LASSERT(oti->oti_txns == 1);
744 * XXX: current rule is that we first start tx,
745 * then lock object(s), but we can't use
746 * this rule for data (due to locking specifics
747 * in ldiskfs). also in long-term we'd like to
748 * use usually-used (locks;tx) ordering. so,
749 * UGLY thing is that we'll use one ordering for
750 * data (ofd) and reverse ordering for metadata
751 * (mdd). then at some point we'll fix the latter
753 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
754 LASSERT(oti->oti_r_locks == 0);
755 LASSERT(oti->oti_w_locks == 0);
757 rc = dt_txn_hook_stop(env, th);
759 CERROR("Failure in transaction hook: %d\n", rc);
760 oh->ot_handle = NULL;
761 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
762 rc = ldiskfs_journal_stop(hdl));
764 CERROR("Failure to stop transaction: %d\n", rc);
769 /* as we want IO to journal and data IO be concurrent, we don't block
770 * awaiting data IO completion in osd_do_bio(), instead we wait here
771 * once transaction is submitted to the journal. all reqular requests
772 * don't do direct IO (except read/write), thus this wait_event becomes
775 * IMPORTANT: we have to wait till any IO submited by the thread is
776 * completed otherwise iobuf may be corrupted by different request
778 cfs_wait_event(iobuf->dr_wait, cfs_atomic_read(&iobuf->dr_numreqs)==0);
780 rc = iobuf->dr_error;
785 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
787 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
790 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
796 * Called just before object is freed. Releases all resources except for
797 * object itself (that is released by osd_object_free()).
799 * Concurrency: no concurrent access is possible that late in object
802 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
804 struct osd_object *obj = osd_obj(l);
805 struct inode *inode = obj->oo_inode;
807 LINVRNT(osd_invariant(obj));
810 * If object is unlinked remove fid->ino mapping from object index.
816 obj->oo_inode = NULL;
821 * Concurrency: ->loo_object_release() is called under site spin-lock.
823 static void osd_object_release(const struct lu_env *env,
829 * Concurrency: shouldn't matter.
831 static int osd_object_print(const struct lu_env *env, void *cookie,
832 lu_printer_t p, const struct lu_object *l)
834 struct osd_object *o = osd_obj(l);
837 if (o->oo_dir != NULL)
838 d = o->oo_dir->od_container.ic_descr;
841 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
843 o->oo_inode ? o->oo_inode->i_ino : 0UL,
844 o->oo_inode ? o->oo_inode->i_generation : 0,
845 d ? d->id_ops->id_name : "plain");
849 * Concurrency: shouldn't matter.
851 int osd_statfs(const struct lu_env *env, struct dt_device *d,
854 struct osd_device *osd = osd_dt_dev(d);
855 struct super_block *sb = osd_sb(osd);
858 cfs_spin_lock(&osd->od_osfs_lock);
860 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
861 result = ll_do_statfs(sb, &osd->od_kstatfs);
862 if (likely(result == 0)) /* N.B. statfs can't really fail */
863 osd->od_osfs_age = cfs_time_current_64();
866 if (likely(result == 0))
867 *sfs = osd->od_kstatfs;
868 cfs_spin_unlock(&osd->od_osfs_lock);
874 * Concurrency: doesn't access mutable data.
876 static void osd_conf_get(const struct lu_env *env,
877 const struct dt_device *dev,
878 struct dt_device_param *param)
880 struct super_block *sb = osd_sb(osd_dt_dev(dev));
883 * XXX should be taken from not-yet-existing fs abstraction layer.
885 param->ddp_max_name_len = LDISKFS_NAME_LEN;
886 param->ddp_max_nlink = LDISKFS_LINK_MAX;
887 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
888 param->ddp_mntopts = 0;
889 if (test_opt(sb, XATTR_USER))
890 param->ddp_mntopts |= MNTOPT_USERXATTR;
891 if (test_opt(sb, POSIX_ACL))
892 param->ddp_mntopts |= MNTOPT_ACL;
894 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
895 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
896 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
899 param->ddp_max_ea_size = sb->s_blocksize;
904 * Helper function to get and fill the buffer with input values.
906 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
910 buf = &osd_oti_get(env)->oti_buf;
917 * Concurrency: shouldn't matter.
919 static int osd_sync(const struct lu_env *env, struct dt_device *d)
921 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
922 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
926 * Start commit for OSD device.
928 * An implementation of dt_commit_async method for OSD device.
929 * Asychronously starts underlayng fs sync and thereby a transaction
932 * \param env environment
935 * \see dt_device_operations
937 static int osd_commit_async(const struct lu_env *env,
940 struct super_block *s = osd_sb(osd_dt_dev(d));
943 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
944 RETURN(s->s_op->sync_fs(s, 0));
948 * Concurrency: shouldn't matter.
951 static void osd_ro(const struct lu_env *env, struct dt_device *d)
953 struct super_block *sb = osd_sb(osd_dt_dev(d));
956 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
958 __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
963 * Concurrency: serialization provided by callers.
965 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
966 int mode, unsigned long timeout, __u32 alg,
967 struct lustre_capa_key *keys)
969 struct osd_device *dev = osd_dt_dev(d);
972 dev->od_fl_capa = mode;
973 dev->od_capa_timeout = timeout;
974 dev->od_capa_alg = alg;
975 dev->od_capa_keys = keys;
980 * Concurrency: serialization provided by callers.
982 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
983 struct dt_quota_ctxt *ctxt, void *data)
985 struct obd_device *obd = (void *)ctxt;
986 struct vfsmount *mnt = (struct vfsmount *)data;
989 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
990 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
991 obd->obd_lvfs_ctxt.pwdmnt = mnt;
992 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
993 obd->obd_lvfs_ctxt.fs = get_ds();
999 * Note: we do not count into QUOTA here.
1000 * If we mount with --data_journal we may need more.
1002 const int osd_dto_credits_noquota[DTO_NR] = {
1005 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1006 * SINGLEDATA_TRANS_BLOCKS(8)
1007 * XXX Note: maybe iam need more, since iam have more level than
1010 [DTO_INDEX_INSERT] = 16,
1011 [DTO_INDEX_DELETE] = 16,
1015 [DTO_INDEX_UPDATE] = 16,
1017 * Create a object. The same as create object in EXT3.
1018 * DATA_TRANS_BLOCKS(14) +
1019 * INDEX_EXTRA_BLOCKS(8) +
1020 * 3(inode bits, groups, GDT)
1022 [DTO_OBJECT_CREATE] = 25,
1024 * XXX: real credits to be fixed
1026 [DTO_OBJECT_DELETE] = 25,
1028 * Attr set credits (inode)
1030 [DTO_ATTR_SET_BASE] = 1,
1032 * Xattr set. The same as xattr of EXT3.
1033 * DATA_TRANS_BLOCKS(14)
1034 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1035 * are also counted in. Do not know why?
1037 [DTO_XATTR_SET] = 14,
1040 * credits for inode change during write.
1042 [DTO_WRITE_BASE] = 3,
1044 * credits for single block write.
1046 [DTO_WRITE_BLOCK] = 14,
1048 * Attr set credits for chown.
1049 * This is extra credits for setattr, and it is null without quota
1051 [DTO_ATTR_SET_CHOWN]= 0
1054 static const struct dt_device_operations osd_dt_ops = {
1055 .dt_root_get = osd_root_get,
1056 .dt_statfs = osd_statfs,
1057 .dt_trans_create = osd_trans_create,
1058 .dt_trans_start = osd_trans_start,
1059 .dt_trans_stop = osd_trans_stop,
1060 .dt_trans_cb_add = osd_trans_cb_add,
1061 .dt_conf_get = osd_conf_get,
1062 .dt_sync = osd_sync,
1064 .dt_commit_async = osd_commit_async,
1065 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1066 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1069 static void osd_object_read_lock(const struct lu_env *env,
1070 struct dt_object *dt, unsigned role)
1072 struct osd_object *obj = osd_dt_obj(dt);
1073 struct osd_thread_info *oti = osd_oti_get(env);
1075 LINVRNT(osd_invariant(obj));
1077 LASSERT(obj->oo_owner != env);
1078 cfs_down_read_nested(&obj->oo_sem, role);
1080 LASSERT(obj->oo_owner == NULL);
1084 static void osd_object_write_lock(const struct lu_env *env,
1085 struct dt_object *dt, unsigned role)
1087 struct osd_object *obj = osd_dt_obj(dt);
1088 struct osd_thread_info *oti = osd_oti_get(env);
1090 LINVRNT(osd_invariant(obj));
1092 LASSERT(obj->oo_owner != env);
1093 cfs_down_write_nested(&obj->oo_sem, role);
1095 LASSERT(obj->oo_owner == NULL);
1096 obj->oo_owner = env;
1100 static void osd_object_read_unlock(const struct lu_env *env,
1101 struct dt_object *dt)
1103 struct osd_object *obj = osd_dt_obj(dt);
1104 struct osd_thread_info *oti = osd_oti_get(env);
1106 LINVRNT(osd_invariant(obj));
1108 LASSERT(oti->oti_r_locks > 0);
1110 cfs_up_read(&obj->oo_sem);
1113 static void osd_object_write_unlock(const struct lu_env *env,
1114 struct dt_object *dt)
1116 struct osd_object *obj = osd_dt_obj(dt);
1117 struct osd_thread_info *oti = osd_oti_get(env);
1119 LINVRNT(osd_invariant(obj));
1121 LASSERT(obj->oo_owner == env);
1122 LASSERT(oti->oti_w_locks > 0);
1124 obj->oo_owner = NULL;
1125 cfs_up_write(&obj->oo_sem);
1128 static int osd_object_write_locked(const struct lu_env *env,
1129 struct dt_object *dt)
1131 struct osd_object *obj = osd_dt_obj(dt);
1133 LINVRNT(osd_invariant(obj));
1135 return obj->oo_owner == env;
1138 static int capa_is_sane(const struct lu_env *env,
1139 struct osd_device *dev,
1140 struct lustre_capa *capa,
1141 struct lustre_capa_key *keys)
1143 struct osd_thread_info *oti = osd_oti_get(env);
1144 struct lustre_capa *tcapa = &oti->oti_capa;
1145 struct obd_capa *oc;
1149 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1151 if (capa_is_expired(oc)) {
1152 DEBUG_CAPA(D_ERROR, capa, "expired");
1159 if (capa_is_expired_sec(capa)) {
1160 DEBUG_CAPA(D_ERROR, capa, "expired");
1164 cfs_spin_lock(&capa_lock);
1165 for (i = 0; i < 2; i++) {
1166 if (keys[i].lk_keyid == capa->lc_keyid) {
1167 oti->oti_capa_key = keys[i];
1171 cfs_spin_unlock(&capa_lock);
1174 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1178 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1182 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1183 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1187 oc = capa_add(dev->od_capa_hash, capa);
1193 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1194 struct lustre_capa *capa, __u64 opc)
1196 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1197 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1198 struct md_capainfo *ci;
1201 if (!dev->od_fl_capa)
1204 if (capa == BYPASS_CAPA)
1207 ci = md_capainfo(env);
1211 if (ci->mc_auth == LC_ID_NONE)
1215 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1219 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1220 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1225 if (!capa_opc_supported(capa, opc)) {
1226 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1230 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1231 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1238 static struct timespec *osd_inode_time(const struct lu_env *env,
1239 struct inode *inode, __u64 seconds)
1241 struct osd_thread_info *oti = osd_oti_get(env);
1242 struct timespec *t = &oti->oti_time;
1244 t->tv_sec = seconds;
1246 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1251 static void osd_inode_getattr(const struct lu_env *env,
1252 struct inode *inode, struct lu_attr *attr)
1254 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1255 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1256 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1258 attr->la_atime = LTIME_S(inode->i_atime);
1259 attr->la_mtime = LTIME_S(inode->i_mtime);
1260 attr->la_ctime = LTIME_S(inode->i_ctime);
1261 attr->la_mode = inode->i_mode;
1262 attr->la_size = i_size_read(inode);
1263 attr->la_blocks = inode->i_blocks;
1264 attr->la_uid = inode->i_uid;
1265 attr->la_gid = inode->i_gid;
1266 attr->la_flags = LDISKFS_I(inode)->i_flags;
1267 attr->la_nlink = inode->i_nlink;
1268 attr->la_rdev = inode->i_rdev;
1269 attr->la_blksize = ll_inode_blksize(inode);
1270 attr->la_blkbits = inode->i_blkbits;
1273 static int osd_attr_get(const struct lu_env *env,
1274 struct dt_object *dt,
1275 struct lu_attr *attr,
1276 struct lustre_capa *capa)
1278 struct osd_object *obj = osd_dt_obj(dt);
1280 LASSERT(dt_object_exists(dt));
1281 LINVRNT(osd_invariant(obj));
1283 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1286 cfs_spin_lock(&obj->oo_guard);
1287 osd_inode_getattr(env, obj->oo_inode, attr);
1288 cfs_spin_unlock(&obj->oo_guard);
1292 static int osd_declare_attr_set(const struct lu_env *env,
1293 struct dt_object *dt,
1294 const struct lu_attr *attr,
1295 struct thandle *handle)
1297 struct osd_thandle *oh;
1298 struct osd_object *obj;
1300 LASSERT(dt != NULL);
1301 LASSERT(handle != NULL);
1303 obj = osd_dt_obj(dt);
1304 LASSERT(osd_invariant(obj));
1306 oh = container_of0(handle, struct osd_thandle, ot_super);
1307 LASSERT(oh->ot_handle == NULL);
1309 OSD_DECLARE_OP(oh, attr_set);
1310 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1312 if (attr && attr->la_valid & LA_UID) {
1314 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1316 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1318 if (attr && attr->la_valid & LA_GID) {
1320 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1322 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1328 static int osd_inode_setattr(const struct lu_env *env,
1329 struct inode *inode, const struct lu_attr *attr)
1333 bits = attr->la_valid;
1335 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1337 #ifdef HAVE_QUOTA_SUPPORT
1338 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1339 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1340 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1346 iattr.ia_valid |= ATTR_UID;
1348 iattr.ia_valid |= ATTR_GID;
1349 iattr.ia_uid = attr->la_uid;
1350 iattr.ia_gid = attr->la_gid;
1351 osd_push_ctxt(env, save);
1352 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1359 if (bits & LA_ATIME)
1360 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1361 if (bits & LA_CTIME)
1362 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1363 if (bits & LA_MTIME)
1364 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1365 if (bits & LA_SIZE) {
1366 LDISKFS_I(inode)->i_disksize = attr->la_size;
1367 i_size_write(inode, attr->la_size);
1371 /* OSD should not change "i_blocks" which is used by quota.
1372 * "i_blocks" should be changed by ldiskfs only. */
1373 if (bits & LA_BLOCKS)
1374 inode->i_blocks = attr->la_blocks;
1377 inode->i_mode = (inode->i_mode & S_IFMT) |
1378 (attr->la_mode & ~S_IFMT);
1380 inode->i_uid = attr->la_uid;
1382 inode->i_gid = attr->la_gid;
1383 if (bits & LA_NLINK)
1384 inode->i_nlink = attr->la_nlink;
1386 inode->i_rdev = attr->la_rdev;
1388 if (bits & LA_FLAGS) {
1389 /* always keep S_NOCMTIME */
1390 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1396 static int osd_attr_set(const struct lu_env *env,
1397 struct dt_object *dt,
1398 const struct lu_attr *attr,
1399 struct thandle *handle,
1400 struct lustre_capa *capa)
1402 struct osd_object *obj = osd_dt_obj(dt);
1405 LASSERT(handle != NULL);
1406 LASSERT(dt_object_exists(dt));
1407 LASSERT(osd_invariant(obj));
1409 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1412 OSD_EXEC_OP(handle, attr_set);
1414 cfs_spin_lock(&obj->oo_guard);
1415 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1416 cfs_spin_unlock(&obj->oo_guard);
1419 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1426 * XXX temporary solution.
1428 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1429 struct lu_attr *attr, struct thandle *th)
1434 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1435 struct lu_attr *attr, struct thandle *th)
1437 osd_object_init0(obj);
1438 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1439 unlock_new_inode(obj->oo_inode);
1443 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1444 struct osd_object *obj,
1448 struct osd_thread_info *info = osd_oti_get(env);
1449 struct dentry *child_dentry = &info->oti_child_dentry;
1450 struct dentry *obj_dentry = &info->oti_obj_dentry;
1452 obj_dentry->d_inode = obj->oo_inode;
1453 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1454 obj_dentry->d_name.hash = 0;
1456 child_dentry->d_name.hash = 0;
1457 child_dentry->d_parent = obj_dentry;
1458 child_dentry->d_name.name = name;
1459 child_dentry->d_name.len = namelen;
1460 return child_dentry;
1464 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1466 struct dt_allocation_hint *hint,
1470 struct osd_device *osd = osd_obj2dev(obj);
1471 struct osd_thandle *oth;
1472 struct dt_object *parent;
1473 struct inode *inode;
1474 #ifdef HAVE_QUOTA_SUPPORT
1475 struct osd_ctxt *save = &info->oti_ctxt;
1478 LINVRNT(osd_invariant(obj));
1479 LASSERT(obj->oo_inode == NULL);
1480 LASSERT(obj->oo_hl_head == NULL);
1482 if (S_ISDIR(mode) && ldiskfs_pdo) {
1483 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1484 if (obj->oo_hl_head == NULL)
1488 oth = container_of(th, struct osd_thandle, ot_super);
1489 LASSERT(oth->ot_handle->h_transaction != NULL);
1491 if (hint && hint->dah_parent)
1492 parent = hint->dah_parent;
1494 parent = osd->od_obj_area;
1496 #ifdef HAVE_QUOTA_SUPPORT
1497 osd_push_ctxt(info->oti_env, save);
1499 inode = ldiskfs_create_inode(oth->ot_handle,
1500 parent ? osd_dt_obj(parent)->oo_inode :
1501 osd_sb(osd)->s_root->d_inode,
1503 #ifdef HAVE_QUOTA_SUPPORT
1506 if (!IS_ERR(inode)) {
1507 /* Do not update file c/mtime in ldiskfs.
1508 * NB: don't need any lock because no contention at this
1510 inode->i_flags |= S_NOCMTIME;
1511 obj->oo_inode = inode;
1514 if (obj->oo_hl_head != NULL) {
1515 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1516 obj->oo_hl_head = NULL;
1518 result = PTR_ERR(inode);
1520 LINVRNT(osd_invariant(obj));
1528 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1529 struct lu_attr *attr,
1530 struct dt_allocation_hint *hint,
1531 struct dt_object_format *dof,
1535 struct osd_thandle *oth;
1536 struct osd_device *osd = osd_obj2dev(obj);
1537 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1539 LASSERT(S_ISDIR(attr->la_mode));
1541 oth = container_of(th, struct osd_thandle, ot_super);
1542 LASSERT(oth->ot_handle->h_transaction != NULL);
1543 result = osd_mkfile(info, obj, mode, hint, th);
1544 if (result == 0 && osd->od_iop_mode == 0) {
1545 LASSERT(obj->oo_inode != NULL);
1547 * XXX uh-oh... call low-level iam function directly.
1550 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1551 sizeof (struct osd_fid_pack),
1557 static int osd_mk_index(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,
1564 struct osd_thandle *oth;
1565 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1567 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1569 LASSERT(S_ISREG(attr->la_mode));
1571 oth = container_of(th, struct osd_thandle, ot_super);
1572 LASSERT(oth->ot_handle->h_transaction != NULL);
1574 result = osd_mkfile(info, obj, mode, hint, th);
1576 LASSERT(obj->oo_inode != NULL);
1577 if (feat->dif_flags & DT_IND_VARKEY)
1578 result = iam_lvar_create(obj->oo_inode,
1579 feat->dif_keysize_max,
1581 feat->dif_recsize_max,
1584 result = iam_lfix_create(obj->oo_inode,
1585 feat->dif_keysize_max,
1587 feat->dif_recsize_max,
1594 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1595 struct lu_attr *attr,
1596 struct dt_allocation_hint *hint,
1597 struct dt_object_format *dof,
1600 LASSERT(S_ISREG(attr->la_mode));
1601 return osd_mkfile(info, obj, (attr->la_mode &
1602 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1605 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1606 struct lu_attr *attr,
1607 struct dt_allocation_hint *hint,
1608 struct dt_object_format *dof,
1611 LASSERT(S_ISLNK(attr->la_mode));
1612 return osd_mkfile(info, obj, (attr->la_mode &
1613 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1616 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1617 struct lu_attr *attr,
1618 struct dt_allocation_hint *hint,
1619 struct dt_object_format *dof,
1622 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1625 LINVRNT(osd_invariant(obj));
1626 LASSERT(obj->oo_inode == NULL);
1627 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1628 S_ISFIFO(mode) || S_ISSOCK(mode));
1630 result = osd_mkfile(info, obj, mode, hint, th);
1632 LASSERT(obj->oo_inode != NULL);
1633 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1635 LINVRNT(osd_invariant(obj));
1639 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1641 struct dt_allocation_hint *hint,
1642 struct dt_object_format *dof,
1645 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1647 osd_obj_type_f result;
1663 result = osd_mk_index;
1674 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1675 struct dt_object *parent, cfs_umode_t child_mode)
1679 memset(ah, 0, sizeof(*ah));
1680 ah->dah_parent = parent;
1681 ah->dah_mode = child_mode;
1685 * Helper function for osd_object_create()
1687 * \retval 0, on success
1689 static int __osd_object_create(struct osd_thread_info *info,
1690 struct osd_object *obj, struct lu_attr *attr,
1691 struct dt_allocation_hint *hint,
1692 struct dt_object_format *dof,
1698 result = osd_create_pre(info, obj, attr, th);
1700 result = osd_create_type_f(dof->dof_type)(info, obj,
1701 attr, hint, dof, th);
1703 result = osd_create_post(info, obj, attr, th);
1709 * Helper function for osd_object_create()
1711 * \retval 0, on success
1713 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1714 const struct lu_fid *fid, struct thandle *th)
1716 struct osd_thread_info *info = osd_oti_get(env);
1717 struct osd_inode_id *id = &info->oti_id;
1718 struct osd_device *osd = osd_obj2dev(obj);
1719 struct md_ucred *uc = md_ucred(env);
1721 LASSERT(obj->oo_inode != NULL);
1722 LASSERT(uc != NULL);
1724 id->oii_ino = obj->oo_inode->i_ino;
1725 id->oii_gen = obj->oo_inode->i_generation;
1727 return osd_oi_insert(info, osd_fid2oi(osd, fid), fid, id, th,
1728 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1731 static int osd_declare_object_create(const struct lu_env *env,
1732 struct dt_object *dt,
1733 struct lu_attr *attr,
1734 struct dt_allocation_hint *hint,
1735 struct dt_object_format *dof,
1736 struct thandle *handle)
1738 struct osd_thandle *oh;
1740 LASSERT(handle != NULL);
1742 oh = container_of0(handle, struct osd_thandle, ot_super);
1743 LASSERT(oh->ot_handle == NULL);
1745 OSD_DECLARE_OP(oh, create);
1746 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1747 /* XXX: So far, only normal fid needs be inserted into the oi,
1748 * things could be changed later. Revise following code then. */
1749 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1750 OSD_DECLARE_OP(oh, insert);
1751 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1753 /* If this is directory, then we expect . and .. to be inserted as
1754 * well. The one directory block always needs to be created for the
1755 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1756 * block), there is no danger of needing a tree for the first block.
1758 if (attr && S_ISDIR(attr->la_mode)) {
1759 OSD_DECLARE_OP(oh, insert);
1760 OSD_DECLARE_OP(oh, insert);
1761 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1765 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1766 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1771 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1772 struct lu_attr *attr,
1773 struct dt_allocation_hint *hint,
1774 struct dt_object_format *dof,
1777 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1778 struct osd_object *obj = osd_dt_obj(dt);
1779 struct osd_thread_info *info = osd_oti_get(env);
1784 LINVRNT(osd_invariant(obj));
1785 LASSERT(!dt_object_exists(dt));
1786 LASSERT(osd_write_locked(env, obj));
1787 LASSERT(th != NULL);
1789 OSD_EXEC_OP(th, create);
1791 result = __osd_object_create(info, obj, attr, hint, dof, th);
1793 result = __osd_oi_insert(env, obj, fid, th);
1795 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1796 LASSERT(osd_invariant(obj));
1801 * Called to destroy on-disk representation of the object
1803 * Concurrency: must be locked
1805 static int osd_declare_object_destroy(const struct lu_env *env,
1806 struct dt_object *dt,
1809 struct osd_object *obj = osd_dt_obj(dt);
1810 struct inode *inode = obj->oo_inode;
1811 struct osd_thandle *oh;
1815 oh = container_of0(th, struct osd_thandle, ot_super);
1816 LASSERT(oh->ot_handle == NULL);
1819 OSD_DECLARE_OP(oh, destroy);
1820 OSD_DECLARE_OP(oh, delete);
1821 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
1822 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
1824 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
1825 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
1830 static int osd_object_destroy(const struct lu_env *env,
1831 struct dt_object *dt,
1834 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1835 struct osd_object *obj = osd_dt_obj(dt);
1836 struct inode *inode = obj->oo_inode;
1837 struct osd_device *osd = osd_obj2dev(obj);
1838 struct osd_thandle *oh;
1842 oh = container_of0(th, struct osd_thandle, ot_super);
1843 LASSERT(oh->ot_handle);
1845 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1847 if (S_ISDIR(inode->i_mode)) {
1848 LASSERT(osd_inode_unlinked(inode) ||
1849 inode->i_nlink == 1);
1850 cfs_spin_lock(&obj->oo_guard);
1852 cfs_spin_unlock(&obj->oo_guard);
1853 inode->i_sb->s_op->dirty_inode(inode);
1855 LASSERT(osd_inode_unlinked(inode));
1858 OSD_EXEC_OP(th, destroy);
1860 result = osd_oi_delete(osd_oti_get(env),
1861 osd_fid2oi(osd, fid), fid, th);
1863 /* XXX: add to ext3 orphan list */
1864 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
1866 /* not needed in the cache anymore */
1867 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1873 * Helper function for osd_xattr_set()
1875 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1876 const struct lu_buf *buf, const char *name, int fl)
1878 struct osd_object *obj = osd_dt_obj(dt);
1879 struct inode *inode = obj->oo_inode;
1880 struct osd_thread_info *info = osd_oti_get(env);
1881 struct dentry *dentry = &info->oti_child_dentry;
1885 LASSERT(dt_object_exists(dt));
1886 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1887 LASSERT(osd_write_locked(env, obj));
1889 if (fl & LU_XATTR_REPLACE)
1890 fs_flags |= XATTR_REPLACE;
1892 if (fl & LU_XATTR_CREATE)
1893 fs_flags |= XATTR_CREATE;
1895 dentry->d_inode = inode;
1896 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1897 buf->lb_len, fs_flags);
1902 * Put the fid into lustre_mdt_attrs, and then place the structure
1903 * inode's ea. This fid should not be altered during the life time
1906 * \retval +ve, on success
1907 * \retval -ve, on error
1909 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1911 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1912 const struct lu_fid *fid)
1914 struct osd_thread_info *info = osd_oti_get(env);
1915 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1917 lustre_lma_init(mdt_attrs, fid);
1918 lustre_lma_swab(mdt_attrs);
1919 return __osd_xattr_set(env, dt,
1920 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1921 XATTR_NAME_LMA, LU_XATTR_CREATE);
1926 * Helper function to form igif
1928 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1931 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1935 * Helper function to pack the fid, ldiskfs stores fid in packed format.
1937 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1938 struct lu_fid *befider)
1940 fid_cpu_to_be(befider, (struct lu_fid *)fid);
1941 memcpy(pack->fp_area, befider, sizeof(*befider));
1942 pack->fp_len = sizeof(*befider) + 1;
1946 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1947 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1948 * To have compatilibility with 1.8 ldiskfs driver we need to have
1949 * magic number at start of fid data.
1950 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1953 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1954 const struct dt_rec *fid)
1956 param->edp_magic = LDISKFS_LUFID_MAGIC;
1957 param->edp_len = sizeof(struct lu_fid) + 1;
1959 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1960 (struct lu_fid *)fid);
1963 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1968 switch (pack->fp_len) {
1969 case sizeof *fid + 1:
1970 memcpy(fid, pack->fp_area, sizeof *fid);
1971 fid_be_to_cpu(fid, fid);
1974 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1981 * Try to read the fid from inode ea into dt_rec, if return value
1982 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1984 * \param fid object fid.
1986 * \retval 0 on success
1988 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1989 __u32 ino, struct lu_fid *fid)
1991 struct osd_thread_info *info = osd_oti_get(env);
1992 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1993 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1994 struct dentry *dentry = &info->oti_child_dentry;
1995 struct osd_inode_id *id = &info->oti_id;
1996 struct osd_device *dev;
1997 struct inode *inode;
2001 dev = osd_dev(ldev);
2004 id->oii_gen = OSD_OII_NOGEN;
2006 inode = osd_iget(info, dev, id);
2007 if (IS_ERR(inode)) {
2008 rc = PTR_ERR(inode);
2011 dentry->d_inode = inode;
2013 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2014 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
2017 /* Check LMA compatibility */
2019 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
2020 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
2021 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
2022 ~LMA_INCOMPAT_SUPP);
2027 lustre_lma_swab(mdt_attrs);
2028 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
2030 } else if (rc == -ENODATA) {
2031 osd_igif_get(env, inode, fid);
2040 * OSD layer object create function for interoperability mode (b11826).
2041 * This is mostly similar to osd_object_create(). Only difference being, fid is
2042 * inserted into inode ea here.
2044 * \retval 0, on success
2045 * \retval -ve, on error
2047 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2048 struct lu_attr *attr,
2049 struct dt_allocation_hint *hint,
2050 struct dt_object_format *dof,
2053 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2054 struct osd_object *obj = osd_dt_obj(dt);
2055 struct osd_thread_info *info = osd_oti_get(env);
2060 LASSERT(osd_invariant(obj));
2061 LASSERT(!dt_object_exists(dt));
2062 LASSERT(osd_write_locked(env, obj));
2063 LASSERT(th != NULL);
2065 OSD_EXEC_OP(th, create);
2067 result = __osd_object_create(info, obj, attr, hint, dof, th);
2069 /* objects under osd root shld have igif fid, so dont add fid EA */
2070 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2071 result = osd_ea_fid_set(env, dt, fid);
2074 result = __osd_oi_insert(env, obj, fid, th);
2076 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2077 LINVRNT(osd_invariant(obj));
2081 static int osd_declare_object_ref_add(const struct lu_env *env,
2082 struct dt_object *dt,
2083 struct thandle *handle)
2085 struct osd_thandle *oh;
2087 /* it's possible that object doesn't exist yet */
2088 LASSERT(handle != NULL);
2090 oh = container_of0(handle, struct osd_thandle, ot_super);
2091 LASSERT(oh->ot_handle == NULL);
2093 OSD_DECLARE_OP(oh, ref_add);
2094 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2100 * Concurrency: @dt is write locked.
2102 static int osd_object_ref_add(const struct lu_env *env,
2103 struct dt_object *dt, struct thandle *th)
2105 struct osd_object *obj = osd_dt_obj(dt);
2106 struct inode *inode = obj->oo_inode;
2108 LINVRNT(osd_invariant(obj));
2109 LASSERT(dt_object_exists(dt));
2110 LASSERT(osd_write_locked(env, obj));
2111 LASSERT(th != NULL);
2113 OSD_EXEC_OP(th, ref_add);
2116 * DIR_NLINK feature is set for compatibility reasons if:
2117 * 1) nlinks > LDISKFS_LINK_MAX, or
2118 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2120 * It is easier to always set this flag (rather than check and set),
2121 * since it has less overhead, and the superblock will be dirtied
2122 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2123 * do not actually care whether this flag is set or not.
2125 cfs_spin_lock(&obj->oo_guard);
2127 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2128 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2129 inode->i_nlink == 2)
2132 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
2133 cfs_spin_unlock(&obj->oo_guard);
2134 inode->i_sb->s_op->dirty_inode(inode);
2135 LINVRNT(osd_invariant(obj));
2140 static int osd_declare_object_ref_del(const struct lu_env *env,
2141 struct dt_object *dt,
2142 struct thandle *handle)
2144 struct osd_thandle *oh;
2146 LASSERT(dt_object_exists(dt));
2147 LASSERT(handle != NULL);
2149 oh = container_of0(handle, struct osd_thandle, ot_super);
2150 LASSERT(oh->ot_handle == NULL);
2152 OSD_DECLARE_OP(oh, ref_del);
2153 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2159 * Concurrency: @dt is write locked.
2161 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2164 struct osd_object *obj = osd_dt_obj(dt);
2165 struct inode *inode = obj->oo_inode;
2167 LINVRNT(osd_invariant(obj));
2168 LASSERT(dt_object_exists(dt));
2169 LASSERT(osd_write_locked(env, obj));
2170 LASSERT(th != NULL);
2172 OSD_EXEC_OP(th, ref_del);
2174 cfs_spin_lock(&obj->oo_guard);
2175 LASSERT(inode->i_nlink > 0);
2177 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2178 * then the nlink count is 1. Don't let it be set to 0 or the directory
2179 * inode will be deleted incorrectly. */
2180 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2182 cfs_spin_unlock(&obj->oo_guard);
2183 inode->i_sb->s_op->dirty_inode(inode);
2184 LINVRNT(osd_invariant(obj));
2190 * Get the 64-bit version for an inode.
2192 static int osd_object_version_get(const struct lu_env *env,
2193 struct dt_object *dt, dt_obj_version_t *ver)
2195 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2197 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2198 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2199 *ver = LDISKFS_I(inode)->i_fs_version;
2204 * Concurrency: @dt is read locked.
2206 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2207 struct lu_buf *buf, const char *name,
2208 struct lustre_capa *capa)
2210 struct osd_object *obj = osd_dt_obj(dt);
2211 struct inode *inode = obj->oo_inode;
2212 struct osd_thread_info *info = osd_oti_get(env);
2213 struct dentry *dentry = &info->oti_obj_dentry;
2215 /* version get is not real XATTR but uses xattr API */
2216 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2217 /* for version we are just using xattr API but change inode
2219 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2220 osd_object_version_get(env, dt, buf->lb_buf);
2221 return sizeof(dt_obj_version_t);
2224 LASSERT(dt_object_exists(dt));
2225 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2226 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2228 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2231 dentry->d_inode = inode;
2232 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2236 static int osd_declare_xattr_set(const struct lu_env *env,
2237 struct dt_object *dt,
2238 const struct lu_buf *buf, const char *name,
2239 int fl, struct thandle *handle)
2241 struct osd_thandle *oh;
2243 LASSERT(handle != NULL);
2245 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2246 /* no credits for version */
2250 oh = container_of0(handle, struct osd_thandle, ot_super);
2251 LASSERT(oh->ot_handle == NULL);
2253 OSD_DECLARE_OP(oh, xattr_set);
2254 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2260 * Set the 64-bit version for object
2262 static void osd_object_version_set(const struct lu_env *env,
2263 struct dt_object *dt,
2264 dt_obj_version_t *new_version)
2266 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2268 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2269 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2271 LDISKFS_I(inode)->i_fs_version = *new_version;
2272 /** Version is set after all inode operations are finished,
2273 * so we should mark it dirty here */
2274 inode->i_sb->s_op->dirty_inode(inode);
2278 * Concurrency: @dt is write locked.
2280 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2281 const struct lu_buf *buf, const char *name, int fl,
2282 struct thandle *handle, struct lustre_capa *capa)
2284 LASSERT(handle != NULL);
2286 /* version set is not real XATTR */
2287 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2288 /* for version we are just using xattr API but change inode
2290 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2291 osd_object_version_set(env, dt, buf->lb_buf);
2292 return sizeof(dt_obj_version_t);
2295 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2298 OSD_EXEC_OP(handle, xattr_set);
2299 return __osd_xattr_set(env, dt, buf, name, fl);
2303 * Concurrency: @dt is read locked.
2305 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2306 struct lu_buf *buf, struct lustre_capa *capa)
2308 struct osd_object *obj = osd_dt_obj(dt);
2309 struct inode *inode = obj->oo_inode;
2310 struct osd_thread_info *info = osd_oti_get(env);
2311 struct dentry *dentry = &info->oti_obj_dentry;
2313 LASSERT(dt_object_exists(dt));
2314 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2315 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2317 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2320 dentry->d_inode = inode;
2321 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2324 static int osd_declare_xattr_del(const struct lu_env *env,
2325 struct dt_object *dt, const char *name,
2326 struct thandle *handle)
2328 struct osd_thandle *oh;
2330 LASSERT(dt_object_exists(dt));
2331 LASSERT(handle != NULL);
2333 oh = container_of0(handle, struct osd_thandle, ot_super);
2334 LASSERT(oh->ot_handle == NULL);
2336 OSD_DECLARE_OP(oh, xattr_set);
2337 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2343 * Concurrency: @dt is write locked.
2345 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2346 const char *name, struct thandle *handle,
2347 struct lustre_capa *capa)
2349 struct osd_object *obj = osd_dt_obj(dt);
2350 struct inode *inode = obj->oo_inode;
2351 struct osd_thread_info *info = osd_oti_get(env);
2352 struct dentry *dentry = &info->oti_obj_dentry;
2355 LASSERT(dt_object_exists(dt));
2356 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2357 LASSERT(osd_write_locked(env, obj));
2358 LASSERT(handle != NULL);
2360 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2363 OSD_EXEC_OP(handle, xattr_set);
2365 dentry->d_inode = inode;
2366 rc = inode->i_op->removexattr(dentry, name);
2370 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2371 struct dt_object *dt,
2372 struct lustre_capa *old,
2375 struct osd_thread_info *info = osd_oti_get(env);
2376 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2377 struct osd_object *obj = osd_dt_obj(dt);
2378 struct osd_device *dev = osd_obj2dev(obj);
2379 struct lustre_capa_key *key = &info->oti_capa_key;
2380 struct lustre_capa *capa = &info->oti_capa;
2381 struct obd_capa *oc;
2382 struct md_capainfo *ci;
2386 if (!dev->od_fl_capa)
2387 RETURN(ERR_PTR(-ENOENT));
2389 LASSERT(dt_object_exists(dt));
2390 LINVRNT(osd_invariant(obj));
2392 /* renewal sanity check */
2393 if (old && osd_object_auth(env, dt, old, opc))
2394 RETURN(ERR_PTR(-EACCES));
2396 ci = md_capainfo(env);
2398 RETURN(ERR_PTR(-ENOENT));
2400 switch (ci->mc_auth) {
2404 capa->lc_uid = obj->oo_inode->i_uid;
2405 capa->lc_gid = obj->oo_inode->i_gid;
2406 capa->lc_flags = LC_ID_PLAIN;
2408 case LC_ID_CONVERT: {
2411 s[0] = obj->oo_inode->i_uid;
2412 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2413 s[2] = obj->oo_inode->i_gid;
2414 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2415 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2417 RETURN(ERR_PTR(rc));
2419 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2420 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2421 capa->lc_flags = LC_ID_CONVERT;
2425 RETURN(ERR_PTR(-EINVAL));
2428 capa->lc_fid = *fid;
2430 capa->lc_flags |= dev->od_capa_alg << 24;
2431 capa->lc_timeout = dev->od_capa_timeout;
2432 capa->lc_expiry = 0;
2434 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2436 LASSERT(!capa_is_expired(oc));
2440 cfs_spin_lock(&capa_lock);
2441 *key = dev->od_capa_keys[1];
2442 cfs_spin_unlock(&capa_lock);
2444 capa->lc_keyid = key->lk_keyid;
2445 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2447 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2449 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2450 RETURN(ERR_PTR(rc));
2453 oc = capa_add(dev->od_capa_hash, capa);
2457 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2459 struct osd_object *obj = osd_dt_obj(dt);
2460 struct inode *inode = obj->oo_inode;
2461 struct osd_thread_info *info = osd_oti_get(env);
2462 struct dentry *dentry = &info->oti_obj_dentry;
2463 struct file *file = &info->oti_file;
2468 dentry->d_inode = inode;
2469 file->f_dentry = dentry;
2470 file->f_mapping = inode->i_mapping;
2471 file->f_op = inode->i_fop;
2472 LOCK_INODE_MUTEX(inode);
2473 rc = file->f_op->fsync(file, dentry, 0);
2474 UNLOCK_INODE_MUTEX(inode);
2478 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2481 struct osd_object *obj = osd_dt_obj(dt);
2484 *data = (void *)obj->oo_inode;
2492 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2493 const struct dt_index_features *feat)
2495 struct iam_descr *descr;
2497 if (osd_object_is_root(o))
2498 return feat == &dt_directory_features;
2500 LASSERT(o->oo_dir != NULL);
2502 descr = o->oo_dir->od_container.ic_descr;
2503 if (feat == &dt_directory_features) {
2504 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2510 feat->dif_keysize_min <= descr->id_key_size &&
2511 descr->id_key_size <= feat->dif_keysize_max &&
2512 feat->dif_recsize_min <= descr->id_rec_size &&
2513 descr->id_rec_size <= feat->dif_recsize_max &&
2514 !(feat->dif_flags & (DT_IND_VARKEY |
2515 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2516 ergo(feat->dif_flags & DT_IND_UPDATE,
2517 1 /* XXX check that object (and file system) is
2522 static int osd_iam_container_init(const struct lu_env *env,
2523 struct osd_object *obj,
2524 struct osd_directory *dir)
2526 struct iam_container *bag = &dir->od_container;
2529 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2533 result = iam_container_setup(bag);
2537 if (osd_obj2dev(obj)->od_iop_mode) {
2538 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2540 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2546 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2548 iam_container_fini(bag);
2555 * Concurrency: no external locking is necessary.
2557 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2558 const struct dt_index_features *feat)
2562 struct osd_object *obj = osd_dt_obj(dt);
2563 struct osd_device *osd = osd_obj2dev(obj);
2565 LINVRNT(osd_invariant(obj));
2566 LASSERT(dt_object_exists(dt));
2568 if (osd_object_is_root(obj)) {
2569 dt->do_index_ops = &osd_index_ea_ops;
2571 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2572 dt->do_index_ops = &osd_index_ea_ops;
2573 if (S_ISDIR(obj->oo_inode->i_mode))
2578 } else if (!osd_has_index(obj)) {
2579 struct osd_directory *dir;
2584 cfs_spin_lock(&obj->oo_guard);
2585 if (obj->oo_dir == NULL)
2589 * Concurrent thread allocated container data.
2592 cfs_spin_unlock(&obj->oo_guard);
2594 * Now, that we have container data, serialize its
2597 cfs_down_write(&obj->oo_ext_idx_sem);
2599 * recheck under lock.
2601 if (!osd_has_index(obj))
2602 result = osd_iam_container_init(env, obj, dir);
2605 cfs_up_write(&obj->oo_ext_idx_sem);
2613 if (result == 0 && ea_dir == 0) {
2614 if (!osd_iam_index_probe(env, obj, feat))
2617 LINVRNT(osd_invariant(obj));
2622 static const struct dt_object_operations osd_obj_ops = {
2623 .do_read_lock = osd_object_read_lock,
2624 .do_write_lock = osd_object_write_lock,
2625 .do_read_unlock = osd_object_read_unlock,
2626 .do_write_unlock = osd_object_write_unlock,
2627 .do_write_locked = osd_object_write_locked,
2628 .do_attr_get = osd_attr_get,
2629 .do_declare_attr_set = osd_declare_attr_set,
2630 .do_attr_set = osd_attr_set,
2631 .do_ah_init = osd_ah_init,
2632 .do_declare_create = osd_declare_object_create,
2633 .do_create = osd_object_create,
2634 .do_declare_destroy = osd_declare_object_destroy,
2635 .do_destroy = osd_object_destroy,
2636 .do_index_try = osd_index_try,
2637 .do_declare_ref_add = osd_declare_object_ref_add,
2638 .do_ref_add = osd_object_ref_add,
2639 .do_declare_ref_del = osd_declare_object_ref_del,
2640 .do_ref_del = osd_object_ref_del,
2641 .do_xattr_get = osd_xattr_get,
2642 .do_declare_xattr_set = osd_declare_xattr_set,
2643 .do_xattr_set = osd_xattr_set,
2644 .do_declare_xattr_del = osd_declare_xattr_del,
2645 .do_xattr_del = osd_xattr_del,
2646 .do_xattr_list = osd_xattr_list,
2647 .do_capa_get = osd_capa_get,
2648 .do_object_sync = osd_object_sync,
2649 .do_data_get = osd_data_get,
2653 * dt_object_operations for interoperability mode
2654 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2656 static const struct dt_object_operations osd_obj_ea_ops = {
2657 .do_read_lock = osd_object_read_lock,
2658 .do_write_lock = osd_object_write_lock,
2659 .do_read_unlock = osd_object_read_unlock,
2660 .do_write_unlock = osd_object_write_unlock,
2661 .do_write_locked = osd_object_write_locked,
2662 .do_attr_get = osd_attr_get,
2663 .do_declare_attr_set = osd_declare_attr_set,
2664 .do_attr_set = osd_attr_set,
2665 .do_ah_init = osd_ah_init,
2666 .do_declare_create = osd_declare_object_create,
2667 .do_create = osd_object_ea_create,
2668 .do_declare_destroy = osd_declare_object_destroy,
2669 .do_destroy = osd_object_destroy,
2670 .do_index_try = osd_index_try,
2671 .do_declare_ref_add = osd_declare_object_ref_add,
2672 .do_ref_add = osd_object_ref_add,
2673 .do_declare_ref_del = osd_declare_object_ref_del,
2674 .do_ref_del = osd_object_ref_del,
2675 .do_xattr_get = osd_xattr_get,
2676 .do_declare_xattr_set = osd_declare_xattr_set,
2677 .do_xattr_set = osd_xattr_set,
2678 .do_declare_xattr_del = osd_declare_xattr_del,
2679 .do_xattr_del = osd_xattr_del,
2680 .do_xattr_list = osd_xattr_list,
2681 .do_capa_get = osd_capa_get,
2682 .do_object_sync = osd_object_sync,
2683 .do_data_get = osd_data_get,
2686 static int osd_index_declare_iam_delete(const struct lu_env *env,
2687 struct dt_object *dt,
2688 const struct dt_key *key,
2689 struct thandle *handle)
2691 struct osd_thandle *oh;
2693 oh = container_of0(handle, struct osd_thandle, ot_super);
2694 LASSERT(oh->ot_handle == NULL);
2696 OSD_DECLARE_OP(oh, delete);
2697 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2703 * delete a (key, value) pair from index \a dt specified by \a key
2705 * \param dt osd index object
2706 * \param key key for index
2707 * \param rec record reference
2708 * \param handle transaction handler
2711 * \retval -ve failure
2714 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2715 const struct dt_key *key,
2716 struct thandle *handle,
2717 struct lustre_capa *capa)
2719 struct osd_object *obj = osd_dt_obj(dt);
2720 struct osd_thandle *oh;
2721 struct iam_path_descr *ipd;
2722 struct iam_container *bag = &obj->oo_dir->od_container;
2727 LINVRNT(osd_invariant(obj));
2728 LASSERT(dt_object_exists(dt));
2729 LASSERT(bag->ic_object == obj->oo_inode);
2730 LASSERT(handle != NULL);
2732 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2735 OSD_EXEC_OP(handle, delete);
2737 ipd = osd_idx_ipd_get(env, bag);
2738 if (unlikely(ipd == NULL))
2741 oh = container_of0(handle, struct osd_thandle, ot_super);
2742 LASSERT(oh->ot_handle != NULL);
2743 LASSERT(oh->ot_handle->h_transaction != NULL);
2745 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2746 osd_ipd_put(env, bag, ipd);
2747 LINVRNT(osd_invariant(obj));
2751 static int osd_index_declare_ea_delete(const struct lu_env *env,
2752 struct dt_object *dt,
2753 const struct dt_key *key,
2754 struct thandle *handle)
2756 struct osd_thandle *oh;
2758 LASSERT(dt_object_exists(dt));
2759 LASSERT(handle != NULL);
2761 oh = container_of0(handle, struct osd_thandle, ot_super);
2762 LASSERT(oh->ot_handle == NULL);
2764 OSD_DECLARE_OP(oh, delete);
2765 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2767 LASSERT(osd_dt_obj(dt)->oo_inode);
2768 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2769 osd_dt_obj(dt)->oo_inode);
2770 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2771 osd_dt_obj(dt)->oo_inode);
2776 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2779 struct osd_fid_pack *rec;
2782 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2783 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2784 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2790 * Index delete function for interoperability mode (b11826).
2791 * It will remove the directory entry added by osd_index_ea_insert().
2792 * This entry is needed to maintain name->fid mapping.
2794 * \param key, key i.e. file entry to be deleted
2796 * \retval 0, on success
2797 * \retval -ve, on error
2799 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2800 const struct dt_key *key,
2801 struct thandle *handle,
2802 struct lustre_capa *capa)
2804 struct osd_object *obj = osd_dt_obj(dt);
2805 struct inode *dir = obj->oo_inode;
2806 struct dentry *dentry;
2807 struct osd_thandle *oh;
2808 struct ldiskfs_dir_entry_2 *de;
2809 struct buffer_head *bh;
2810 struct htree_lock *hlock = NULL;
2815 LINVRNT(osd_invariant(obj));
2816 LASSERT(dt_object_exists(dt));
2817 LASSERT(handle != NULL);
2819 OSD_EXEC_OP(handle, delete);
2821 oh = container_of(handle, struct osd_thandle, ot_super);
2822 LASSERT(oh->ot_handle != NULL);
2823 LASSERT(oh->ot_handle->h_transaction != NULL);
2825 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2828 dentry = osd_child_dentry_get(env, obj,
2829 (char *)key, strlen((char *)key));
2831 if (obj->oo_hl_head != NULL) {
2832 hlock = osd_oti_get(env)->oti_hlock;
2833 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2834 dir, LDISKFS_HLOCK_DEL);
2836 cfs_down_write(&obj->oo_ext_idx_sem);
2839 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2841 rc = ldiskfs_delete_entry(oh->ot_handle,
2848 ldiskfs_htree_unlock(hlock);
2850 cfs_up_write(&obj->oo_ext_idx_sem);
2852 LASSERT(osd_invariant(obj));
2857 * Lookup index for \a key and copy record to \a rec.
2859 * \param dt osd index object
2860 * \param key key for index
2861 * \param rec record reference
2863 * \retval +ve success : exact mach
2864 * \retval 0 return record with key not greater than \a key
2865 * \retval -ve failure
2867 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2868 struct dt_rec *rec, const struct dt_key *key,
2869 struct lustre_capa *capa)
2871 struct osd_object *obj = osd_dt_obj(dt);
2872 struct iam_path_descr *ipd;
2873 struct iam_container *bag = &obj->oo_dir->od_container;
2874 struct osd_thread_info *oti = osd_oti_get(env);
2875 struct iam_iterator *it = &oti->oti_idx_it;
2876 struct iam_rec *iam_rec;
2881 LASSERT(osd_invariant(obj));
2882 LASSERT(dt_object_exists(dt));
2883 LASSERT(bag->ic_object == obj->oo_inode);
2885 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2888 ipd = osd_idx_ipd_get(env, bag);
2892 /* got ipd now we can start iterator. */
2893 iam_it_init(it, bag, 0, ipd);
2895 rc = iam_it_get(it, (struct iam_key *)key);
2897 if (S_ISDIR(obj->oo_inode->i_mode))
2898 iam_rec = (struct iam_rec *)oti->oti_ldp;
2900 iam_rec = (struct iam_rec *) rec;
2902 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2903 if (S_ISDIR(obj->oo_inode->i_mode))
2904 osd_fid_unpack((struct lu_fid *) rec,
2905 (struct osd_fid_pack *)iam_rec);
2909 osd_ipd_put(env, bag, ipd);
2911 LINVRNT(osd_invariant(obj));
2916 static int osd_index_declare_iam_insert(const struct lu_env *env,
2917 struct dt_object *dt,
2918 const struct dt_rec *rec,
2919 const struct dt_key *key,
2920 struct thandle *handle)
2922 struct osd_thandle *oh;
2924 LASSERT(dt_object_exists(dt));
2925 LASSERT(handle != NULL);
2927 oh = container_of0(handle, struct osd_thandle, ot_super);
2928 LASSERT(oh->ot_handle == NULL);
2930 OSD_DECLARE_OP(oh, insert);
2931 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
2937 * Inserts (key, value) pair in \a dt index object.
2939 * \param dt osd index object
2940 * \param key key for index
2941 * \param rec record reference
2942 * \param th transaction handler
2945 * \retval -ve failure
2947 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2948 const struct dt_rec *rec,
2949 const struct dt_key *key, struct thandle *th,
2950 struct lustre_capa *capa, int ignore_quota)
2952 struct osd_object *obj = osd_dt_obj(dt);
2953 struct iam_path_descr *ipd;
2954 struct osd_thandle *oh;
2955 struct iam_container *bag = &obj->oo_dir->od_container;
2956 #ifdef HAVE_QUOTA_SUPPORT
2957 cfs_cap_t save = cfs_curproc_cap_pack();
2959 struct osd_thread_info *oti = osd_oti_get(env);
2960 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2965 LINVRNT(osd_invariant(obj));
2966 LASSERT(dt_object_exists(dt));
2967 LASSERT(bag->ic_object == obj->oo_inode);
2968 LASSERT(th != NULL);
2970 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2973 OSD_EXEC_OP(th, insert);
2975 ipd = osd_idx_ipd_get(env, bag);
2976 if (unlikely(ipd == NULL))
2979 oh = container_of0(th, struct osd_thandle, ot_super);
2980 LASSERT(oh->ot_handle != NULL);
2981 LASSERT(oh->ot_handle->h_transaction != NULL);
2982 #ifdef HAVE_QUOTA_SUPPORT
2984 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2986 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2988 if (S_ISDIR(obj->oo_inode->i_mode))
2989 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2991 iam_rec = (struct iam_rec *) rec;
2992 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2994 #ifdef HAVE_QUOTA_SUPPORT
2995 cfs_curproc_cap_unpack(save);
2997 osd_ipd_put(env, bag, ipd);
2998 LINVRNT(osd_invariant(obj));
3003 * Calls ldiskfs_add_entry() to add directory entry
3004 * into the directory. This is required for
3005 * interoperability mode (b11826)
3007 * \retval 0, on success
3008 * \retval -ve, on error
3010 static int __osd_ea_add_rec(struct osd_thread_info *info,
3011 struct osd_object *pobj, struct inode *cinode,
3012 const char *name, const struct dt_rec *fid,
3013 struct htree_lock *hlock, struct thandle *th)
3015 struct ldiskfs_dentry_param *ldp;
3016 struct dentry *child;
3017 struct osd_thandle *oth;
3020 oth = container_of(th, struct osd_thandle, ot_super);
3021 LASSERT(oth->ot_handle != NULL);
3022 LASSERT(oth->ot_handle->h_transaction != NULL);
3024 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3026 if (fid_is_igif((struct lu_fid *)fid) ||
3027 fid_is_norm((struct lu_fid *)fid)) {
3028 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3029 osd_get_ldiskfs_dirent_param(ldp, fid);
3030 child->d_fsdata = (void*) ldp;
3032 child->d_fsdata = NULL;
3033 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3039 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3040 * into the directory.Also sets flags into osd object to
3041 * indicate dot and dotdot are created. This is required for
3042 * interoperability mode (b11826)
3044 * \param dir directory for dot and dotdot fixup.
3045 * \param obj child object for linking
3047 * \retval 0, on success
3048 * \retval -ve, on error
3050 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3051 struct osd_object *dir,
3052 struct inode *parent_dir, const char *name,
3053 const struct dt_rec *dot_fid,
3054 const struct dt_rec *dot_dot_fid,
3057 struct inode *inode = dir->oo_inode;
3058 struct ldiskfs_dentry_param *dot_ldp;
3059 struct ldiskfs_dentry_param *dot_dot_ldp;
3060 struct osd_thandle *oth;
3063 oth = container_of(th, struct osd_thandle, ot_super);
3064 LASSERT(oth->ot_handle->h_transaction != NULL);
3065 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3067 if (strcmp(name, dot) == 0) {
3068 if (dir->oo_compat_dot_created) {
3071 LASSERT(inode == parent_dir);
3072 dir->oo_compat_dot_created = 1;
3075 } else if(strcmp(name, dotdot) == 0) {
3076 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3077 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3079 if (!dir->oo_compat_dot_created)
3081 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
3082 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3083 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3088 /* in case of rename, dotdot is already created */
3089 if (dir->oo_compat_dotdot_created) {
3090 return __osd_ea_add_rec(info, dir, parent_dir, name,
3091 dot_dot_fid, NULL, th);
3094 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3095 inode, dot_ldp, dot_dot_ldp);
3097 dir->oo_compat_dotdot_created = 1;
3105 * It will call the appropriate osd_add* function and return the
3106 * value, return by respective functions.
3108 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3109 struct inode *cinode, const char *name,
3110 const struct dt_rec *fid, struct thandle *th)
3112 struct osd_thread_info *info = osd_oti_get(env);
3113 struct htree_lock *hlock;
3116 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3118 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3120 if (hlock != NULL) {
3121 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3124 cfs_down_write(&pobj->oo_ext_idx_sem);
3126 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3127 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3130 if (hlock != NULL) {
3131 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3132 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3134 cfs_down_write(&pobj->oo_ext_idx_sem);
3137 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3141 ldiskfs_htree_unlock(hlock);
3143 cfs_up_write(&pobj->oo_ext_idx_sem);
3149 * Calls ->lookup() to find dentry. From dentry get inode and
3150 * read inode's ea to get fid. This is required for interoperability
3153 * \retval 0, on success
3154 * \retval -ve, on error
3156 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3157 struct dt_rec *rec, const struct dt_key *key)
3159 struct inode *dir = obj->oo_inode;
3160 struct dentry *dentry;
3161 struct ldiskfs_dir_entry_2 *de;
3162 struct buffer_head *bh;
3163 struct lu_fid *fid = (struct lu_fid *) rec;
3164 struct htree_lock *hlock = NULL;
3168 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3170 dentry = osd_child_dentry_get(env, obj,
3171 (char *)key, strlen((char *)key));
3173 if (obj->oo_hl_head != NULL) {
3174 hlock = osd_oti_get(env)->oti_hlock;
3175 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3176 dir, LDISKFS_HLOCK_LOOKUP);
3178 cfs_down_read(&obj->oo_ext_idx_sem);
3181 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3183 ino = le32_to_cpu(de->inode);
3184 rc = osd_get_fid_from_dentry(de, rec);
3186 /* done with de, release bh */
3189 rc = osd_ea_fid_get(env, obj, ino, fid);
3195 ldiskfs_htree_unlock(hlock);
3197 cfs_up_read(&obj->oo_ext_idx_sem);
3202 * Find the osd object for given fid.
3204 * \param fid need to find the osd object having this fid
3206 * \retval osd_object on success
3207 * \retval -ve on error
3209 struct osd_object *osd_object_find(const struct lu_env *env,
3210 struct dt_object *dt,
3211 const struct lu_fid *fid)
3213 struct lu_device *ludev = dt->do_lu.lo_dev;
3214 struct osd_object *child = NULL;
3215 struct lu_object *luch;
3216 struct lu_object *lo;
3218 luch = lu_object_find(env, ludev, fid, NULL);
3219 if (!IS_ERR(luch)) {
3220 if (lu_object_exists(luch)) {
3221 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3223 child = osd_obj(lo);
3225 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3226 "lu_object can't be located"
3227 ""DFID"\n", PFID(fid));
3229 if (child == NULL) {
3230 lu_object_put(env, luch);
3231 CERROR("Unable to get osd_object\n");
3232 child = ERR_PTR(-ENOENT);
3235 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3236 "lu_object does not exists "DFID"\n",
3238 child = ERR_PTR(-ENOENT);
3241 child = (void *)luch;
3247 * Put the osd object once done with it.
3249 * \param obj osd object that needs to be put
3251 static inline void osd_object_put(const struct lu_env *env,
3252 struct osd_object *obj)
3254 lu_object_put(env, &obj->oo_dt.do_lu);
3257 static int osd_index_declare_ea_insert(const struct lu_env *env,
3258 struct dt_object *dt,
3259 const struct dt_rec *rec,
3260 const struct dt_key *key,
3261 struct thandle *handle)
3263 struct osd_thandle *oh;
3265 LASSERT(dt_object_exists(dt));
3266 LASSERT(handle != NULL);
3268 oh = container_of0(handle, struct osd_thandle, ot_super);
3269 LASSERT(oh->ot_handle == NULL);
3271 OSD_DECLARE_OP(oh, insert);
3272 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3274 LASSERT(osd_dt_obj(dt)->oo_inode);
3275 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3276 osd_dt_obj(dt)->oo_inode);
3277 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3278 osd_dt_obj(dt)->oo_inode);
3284 * Index add function for interoperability mode (b11826).
3285 * It will add the directory entry.This entry is needed to
3286 * maintain name->fid mapping.
3288 * \param key it is key i.e. file entry to be inserted
3289 * \param rec it is value of given key i.e. fid
3291 * \retval 0, on success
3292 * \retval -ve, on error
3294 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3295 const struct dt_rec *rec,
3296 const struct dt_key *key, struct thandle *th,
3297 struct lustre_capa *capa, int ignore_quota)
3299 struct osd_object *obj = osd_dt_obj(dt);
3300 struct lu_fid *fid = (struct lu_fid *) rec;
3301 const char *name = (const char *)key;
3302 struct osd_object *child;
3303 #ifdef HAVE_QUOTA_SUPPORT
3304 cfs_cap_t save = cfs_curproc_cap_pack();
3310 LASSERT(osd_invariant(obj));
3311 LASSERT(dt_object_exists(dt));
3312 LASSERT(th != NULL);
3314 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3317 child = osd_object_find(env, dt, fid);
3318 if (!IS_ERR(child)) {
3319 #ifdef HAVE_QUOTA_SUPPORT
3321 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3323 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3325 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3326 #ifdef HAVE_QUOTA_SUPPORT
3327 cfs_curproc_cap_unpack(save);
3329 osd_object_put(env, child);
3331 rc = PTR_ERR(child);
3334 LASSERT(osd_invariant(obj));
3339 * Initialize osd Iterator for given osd index object.
3341 * \param dt osd index object
3344 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3345 struct dt_object *dt,
3347 struct lustre_capa *capa)
3349 struct osd_it_iam *it;
3350 struct osd_thread_info *oti = osd_oti_get(env);
3351 struct osd_object *obj = osd_dt_obj(dt);
3352 struct lu_object *lo = &dt->do_lu;
3353 struct iam_path_descr *ipd;
3354 struct iam_container *bag = &obj->oo_dir->od_container;
3356 LASSERT(lu_object_exists(lo));
3358 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3359 return ERR_PTR(-EACCES);
3362 ipd = osd_it_ipd_get(env, bag);
3363 if (likely(ipd != NULL)) {
3367 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3368 return (struct dt_it *)it;
3370 return ERR_PTR(-ENOMEM);
3374 * free given Iterator.
3377 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3379 struct osd_it_iam *it = (struct osd_it_iam *)di;
3380 struct osd_object *obj = it->oi_obj;
3382 iam_it_fini(&it->oi_it);
3383 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3384 lu_object_put(env, &obj->oo_dt.do_lu);
3388 * Move Iterator to record specified by \a key
3390 * \param di osd iterator
3391 * \param key key for index
3393 * \retval +ve di points to record with least key not larger than key
3394 * \retval 0 di points to exact matched key
3395 * \retval -ve failure
3398 static int osd_it_iam_get(const struct lu_env *env,
3399 struct dt_it *di, const struct dt_key *key)
3401 struct osd_it_iam *it = (struct osd_it_iam *)di;
3403 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3409 * \param di osd iterator
3412 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3414 struct osd_it_iam *it = (struct osd_it_iam *)di;
3416 iam_it_put(&it->oi_it);
3420 * Move iterator by one record
3422 * \param di osd iterator
3424 * \retval +1 end of container reached
3426 * \retval -ve failure
3429 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3431 struct osd_it_iam *it = (struct osd_it_iam *)di;
3433 return iam_it_next(&it->oi_it);
3437 * Return pointer to the key under iterator.
3440 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3441 const struct dt_it *di)
3443 struct osd_it_iam *it = (struct osd_it_iam *)di;
3445 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3449 * Return size of key under iterator (in bytes)
3452 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3454 struct osd_it_iam *it = (struct osd_it_iam *)di;
3456 return iam_it_key_size(&it->oi_it);
3459 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3460 int len, __u16 type)
3462 struct luda_type *lt;
3463 const unsigned align = sizeof(struct luda_type) - 1;
3465 /* check if file type is required */
3466 if (attr & LUDA_TYPE) {
3467 len = (len + align) & ~align;
3469 lt = (void *) ent->lde_name + len;
3470 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3471 ent->lde_attrs |= LUDA_TYPE;
3474 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3478 * build lu direct from backend fs dirent.
3481 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3482 struct lu_fid *fid, __u64 offset,
3483 char *name, __u16 namelen,
3484 __u16 type, __u32 attr)
3486 fid_cpu_to_le(&ent->lde_fid, fid);
3487 ent->lde_attrs = LUDA_FID;
3489 ent->lde_hash = cpu_to_le64(offset);
3490 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3492 strncpy(ent->lde_name, name, namelen);
3493 ent->lde_namelen = cpu_to_le16(namelen);
3495 /* append lustre attributes */
3496 osd_it_append_attrs(ent, attr, namelen, type);
3500 * Return pointer to the record under iterator.
3502 static int osd_it_iam_rec(const struct lu_env *env,
3503 const struct dt_it *di,
3504 struct dt_rec *dtrec, __u32 attr)
3506 struct osd_it_iam *it = (struct osd_it_iam *)di;
3507 struct osd_thread_info *info = osd_oti_get(env);
3508 struct lu_fid *fid = &info->oti_fid;
3509 const struct osd_fid_pack *rec;
3510 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3516 name = (char *)iam_it_key_get(&it->oi_it);
3518 RETURN(PTR_ERR(name));
3520 namelen = iam_it_key_size(&it->oi_it);
3522 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3524 RETURN(PTR_ERR(rec));
3526 rc = osd_fid_unpack(fid, rec);
3530 hash = iam_it_store(&it->oi_it);
3532 /* IAM does not store object type in IAM index (dir) */
3533 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3540 * Returns cookie for current Iterator position.
3542 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3544 struct osd_it_iam *it = (struct osd_it_iam *)di;
3546 return iam_it_store(&it->oi_it);
3550 * Restore iterator from cookie.
3552 * \param di osd iterator
3553 * \param hash Iterator location cookie
3555 * \retval +ve di points to record with least key not larger than key.
3556 * \retval 0 di points to exact matched key
3557 * \retval -ve failure
3560 static int osd_it_iam_load(const struct lu_env *env,
3561 const struct dt_it *di, __u64 hash)
3563 struct osd_it_iam *it = (struct osd_it_iam *)di;
3565 return iam_it_load(&it->oi_it, hash);
3568 static const struct dt_index_operations osd_index_iam_ops = {
3569 .dio_lookup = osd_index_iam_lookup,
3570 .dio_declare_insert = osd_index_declare_iam_insert,
3571 .dio_insert = osd_index_iam_insert,
3572 .dio_declare_delete = osd_index_declare_iam_delete,
3573 .dio_delete = osd_index_iam_delete,
3575 .init = osd_it_iam_init,
3576 .fini = osd_it_iam_fini,
3577 .get = osd_it_iam_get,
3578 .put = osd_it_iam_put,
3579 .next = osd_it_iam_next,
3580 .key = osd_it_iam_key,
3581 .key_size = osd_it_iam_key_size,
3582 .rec = osd_it_iam_rec,
3583 .store = osd_it_iam_store,
3584 .load = osd_it_iam_load
3589 * Creates or initializes iterator context.
3591 * \retval struct osd_it_ea, iterator structure on success
3594 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3595 struct dt_object *dt,
3597 struct lustre_capa *capa)
3599 struct osd_object *obj = osd_dt_obj(dt);
3600 struct osd_thread_info *info = osd_oti_get(env);
3601 struct osd_it_ea *it = &info->oti_it_ea;
3602 struct lu_object *lo = &dt->do_lu;
3603 struct dentry *obj_dentry = &info->oti_it_dentry;
3605 LASSERT(lu_object_exists(lo));
3607 obj_dentry->d_inode = obj->oo_inode;
3608 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3609 obj_dentry->d_name.hash = 0;
3611 it->oie_rd_dirent = 0;
3612 it->oie_it_dirent = 0;
3613 it->oie_dirent = NULL;
3614 it->oie_buf = info->oti_it_ea_buf;
3616 it->oie_file.f_pos = 0;
3617 it->oie_file.f_dentry = obj_dentry;
3618 if (attr & LUDA_64BITHASH)
3619 it->oie_file.f_flags = O_64BITHASH;
3621 it->oie_file.f_flags = O_32BITHASH;
3622 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3623 it->oie_file.f_op = obj->oo_inode->i_fop;
3624 it->oie_file.private_data = NULL;
3626 RETURN((struct dt_it *) it);
3630 * Destroy or finishes iterator context.
3632 * \param di iterator structure to be destroyed
3634 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3636 struct osd_it_ea *it = (struct osd_it_ea *)di;
3637 struct osd_object *obj = it->oie_obj;
3638 struct inode *inode = obj->oo_inode;
3641 it->oie_file.f_op->release(inode, &it->oie_file);
3642 lu_object_put(env, &obj->oo_dt.do_lu);
3647 * It position the iterator at given key, so that next lookup continues from
3648 * that key Or it is similar to dio_it->load() but based on a key,
3649 * rather than file position.
3651 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3654 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3656 static int osd_it_ea_get(const struct lu_env *env,
3657 struct dt_it *di, const struct dt_key *key)
3659 struct osd_it_ea *it = (struct osd_it_ea *)di;
3662 LASSERT(((const char *)key)[0] == '\0');
3663 it->oie_file.f_pos = 0;
3664 it->oie_rd_dirent = 0;
3665 it->oie_it_dirent = 0;
3666 it->oie_dirent = NULL;
3674 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3679 * It is called internally by ->readdir(). It fills the
3680 * iterator's in-memory data structure with required
3681 * information i.e. name, namelen, rec_size etc.
3683 * \param buf in which information to be filled in.
3684 * \param name name of the file in given dir
3686 * \retval 0 on success
3687 * \retval 1 on buffer full
3689 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3690 loff_t offset, __u64 ino,
3693 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3694 struct osd_it_ea_dirent *ent = it->oie_dirent;
3695 struct lu_fid *fid = &ent->oied_fid;
3696 struct osd_fid_pack *rec;
3699 /* this should never happen */
3700 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3701 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3705 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3709 if (d_type & LDISKFS_DIRENT_LUFID) {
3710 rec = (struct osd_fid_pack*) (name + namelen + 1);
3712 if (osd_fid_unpack(fid, rec) != 0)
3715 d_type &= ~LDISKFS_DIRENT_LUFID;
3720 ent->oied_ino = ino;
3721 ent->oied_off = offset;
3722 ent->oied_namelen = namelen;
3723 ent->oied_type = d_type;
3725 memcpy(ent->oied_name, name, namelen);
3727 it->oie_rd_dirent++;
3728 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3733 * Calls ->readdir() to load a directory entry at a time
3734 * and stored it in iterator's in-memory data structure.
3736 * \param di iterator's in memory structure
3738 * \retval 0 on success
3739 * \retval -ve on error
3741 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3742 const struct dt_it *di)
3744 struct osd_it_ea *it = (struct osd_it_ea *)di;
3745 struct osd_object *obj = it->oie_obj;
3746 struct inode *inode = obj->oo_inode;
3747 struct htree_lock *hlock = NULL;
3751 it->oie_dirent = it->oie_buf;
3752 it->oie_rd_dirent = 0;
3754 if (obj->oo_hl_head != NULL) {
3755 hlock = osd_oti_get(env)->oti_hlock;
3756 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3757 inode, LDISKFS_HLOCK_READDIR);
3759 cfs_down_read(&obj->oo_ext_idx_sem);
3762 result = inode->i_fop->readdir(&it->oie_file, it,
3763 (filldir_t) osd_ldiskfs_filldir);
3766 ldiskfs_htree_unlock(hlock);
3768 cfs_up_read(&obj->oo_ext_idx_sem);
3770 if (it->oie_rd_dirent == 0) {
3773 it->oie_dirent = it->oie_buf;
3774 it->oie_it_dirent = 1;
3781 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3782 * to load a directory entry at a time and stored it in
3783 * iterator's in-memory data structure.
3785 * \param di iterator's in memory structure
3787 * \retval +ve iterator reached to end
3788 * \retval 0 iterator not reached to end
3789 * \retval -ve on error
3791 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3793 struct osd_it_ea *it = (struct osd_it_ea *)di;
3798 if (it->oie_it_dirent < it->oie_rd_dirent) {
3800 (void *) it->oie_dirent +
3801 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3802 it->oie_dirent->oied_namelen);
3803 it->oie_it_dirent++;
3806 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3809 rc = osd_ldiskfs_it_fill(env, di);
3816 * Returns the key at current position from iterator's in memory structure.
3818 * \param di iterator's in memory structure
3820 * \retval key i.e. struct dt_key on success
3822 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3823 const struct dt_it *di)
3825 struct osd_it_ea *it = (struct osd_it_ea *)di;
3827 return (struct dt_key *)it->oie_dirent->oied_name;
3831 * Returns the key's size at current position from iterator's in memory structure.
3833 * \param di iterator's in memory structure
3835 * \retval key_size i.e. struct dt_key on success
3837 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3839 struct osd_it_ea *it = (struct osd_it_ea *)di;
3841 return it->oie_dirent->oied_namelen;
3846 * Returns the value (i.e. fid/igif) at current position from iterator's
3847 * in memory structure.
3849 * \param di struct osd_it_ea, iterator's in memory structure
3850 * \param attr attr requested for dirent.
3851 * \param lde lustre dirent
3853 * \retval 0 no error and \param lde has correct lustre dirent.
3854 * \retval -ve on error
3856 static inline int osd_it_ea_rec(const struct lu_env *env,
3857 const struct dt_it *di,
3858 struct dt_rec *dtrec, __u32 attr)
3860 struct osd_it_ea *it = (struct osd_it_ea *)di;
3861 struct osd_object *obj = it->oie_obj;
3862 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3863 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3868 if (!fid_is_sane(fid))
3869 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3872 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3873 it->oie_dirent->oied_name,
3874 it->oie_dirent->oied_namelen,
3875 it->oie_dirent->oied_type,
3881 * Returns a cookie for current position of the iterator head, so that
3882 * user can use this cookie to load/start the iterator next time.
3884 * \param di iterator's in memory structure
3886 * \retval cookie for current position, on success
3888 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3890 struct osd_it_ea *it = (struct osd_it_ea *)di;
3892 return it->oie_dirent->oied_off;
3896 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3897 * to load a directory entry at a time and stored it i inn,
3898 * in iterator's in-memory data structure.
3900 * \param di struct osd_it_ea, iterator's in memory structure
3902 * \retval +ve on success
3903 * \retval -ve on error
3905 static int osd_it_ea_load(const struct lu_env *env,
3906 const struct dt_it *di, __u64 hash)
3908 struct osd_it_ea *it = (struct osd_it_ea *)di;
3912 it->oie_file.f_pos = hash;
3914 rc = osd_ldiskfs_it_fill(env, di);
3922 * Index lookup function for interoperability mode (b11826).
3924 * \param key, key i.e. file name to be searched
3926 * \retval +ve, on success
3927 * \retval -ve, on error
3929 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3930 struct dt_rec *rec, const struct dt_key *key,
3931 struct lustre_capa *capa)
3933 struct osd_object *obj = osd_dt_obj(dt);
3938 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3939 LINVRNT(osd_invariant(obj));
3941 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3944 rc = osd_ea_lookup_rec(env, obj, rec, key);
3952 * Index and Iterator operations for interoperability
3953 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3955 static const struct dt_index_operations osd_index_ea_ops = {
3956 .dio_lookup = osd_index_ea_lookup,
3957 .dio_declare_insert = osd_index_declare_ea_insert,
3958 .dio_insert = osd_index_ea_insert,
3959 .dio_declare_delete = osd_index_declare_ea_delete,
3960 .dio_delete = osd_index_ea_delete,
3962 .init = osd_it_ea_init,
3963 .fini = osd_it_ea_fini,
3964 .get = osd_it_ea_get,
3965 .put = osd_it_ea_put,
3966 .next = osd_it_ea_next,
3967 .key = osd_it_ea_key,
3968 .key_size = osd_it_ea_key_size,
3969 .rec = osd_it_ea_rec,
3970 .store = osd_it_ea_store,
3971 .load = osd_it_ea_load
3975 static void *osd_key_init(const struct lu_context *ctx,
3976 struct lu_context_key *key)
3978 struct osd_thread_info *info;
3980 OBD_ALLOC_PTR(info);
3982 return ERR_PTR(-ENOMEM);
3984 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3985 if (info->oti_it_ea_buf == NULL)
3988 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
3990 info->oti_hlock = ldiskfs_htree_lock_alloc();
3991 if (info->oti_hlock == NULL)
3997 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4000 return ERR_PTR(-ENOMEM);
4003 static void osd_key_fini(const struct lu_context *ctx,
4004 struct lu_context_key *key, void* data)
4006 struct osd_thread_info *info = data;
4008 if (info->oti_hlock != NULL)
4009 ldiskfs_htree_lock_free(info->oti_hlock);
4010 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4014 static void osd_key_exit(const struct lu_context *ctx,
4015 struct lu_context_key *key, void *data)
4017 struct osd_thread_info *info = data;
4019 LASSERT(info->oti_r_locks == 0);
4020 LASSERT(info->oti_w_locks == 0);
4021 LASSERT(info->oti_txns == 0);
4024 /* type constructor/destructor: osd_type_init, osd_type_fini */
4025 LU_TYPE_INIT_FINI(osd, &osd_key);
4027 struct lu_context_key osd_key = {
4028 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
4029 .lct_init = osd_key_init,
4030 .lct_fini = osd_key_fini,
4031 .lct_exit = osd_key_exit
4035 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4036 const char *name, struct lu_device *next)
4038 return osd_procfs_init(osd_dev(d), name);
4041 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4043 struct osd_thread_info *info = osd_oti_get(env);
4045 if (o->od_obj_area != NULL) {
4046 lu_object_put(env, &o->od_obj_area->do_lu);
4047 o->od_obj_area = NULL;
4049 if (o->od_oi_table != NULL)
4050 osd_oi_fini(info, &o->od_oi_table, o->od_oi_count);
4053 fsfilt_put_ops(o->od_fsops);
4060 static int osd_mount(const struct lu_env *env,
4061 struct osd_device *o, struct lustre_cfg *cfg)
4063 struct lustre_mount_info *lmi;
4064 const char *dev = lustre_cfg_string(cfg, 0);
4065 struct lustre_disk_data *ldd;
4066 struct lustre_sb_info *lsi;
4070 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4071 if (o->od_fsops == NULL) {
4072 CERROR("Can't find fsfilt_ldiskfs\n");
4076 if (o->od_mount != NULL) {
4077 CERROR("Already mounted (%s)\n", dev);
4082 lmi = server_get_mount(dev);
4084 CERROR("Cannot get mount info for %s!\n", dev);
4088 LASSERT(lmi != NULL);
4089 /* save lustre_mount_info in dt_device */
4092 lsi = s2lsi(lmi->lmi_sb);
4095 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4097 LCONSOLE_WARN("OSD: IAM mode enabled\n");
4101 o->od_obj_area = NULL;
4105 static struct lu_device *osd_device_fini(const struct lu_env *env,
4106 struct lu_device *d)
4111 shrink_dcache_sb(osd_sb(osd_dev(d)));
4112 osd_sync(env, lu2dt_dev(d));
4114 rc = osd_procfs_fini(osd_dev(d));
4116 CERROR("proc fini error %d \n", rc);
4117 RETURN (ERR_PTR(rc));
4120 if (osd_dev(d)->od_mount)
4121 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4122 osd_dev(d)->od_mount->lmi_mnt);
4123 osd_dev(d)->od_mount = NULL;
4128 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4129 struct lu_device_type *t,
4130 struct lustre_cfg *cfg)
4132 struct lu_device *l;
4133 struct osd_device *o;
4139 result = dt_device_init(&o->od_dt_dev, t);
4142 l->ld_ops = &osd_lu_ops;
4143 o->od_dt_dev.dd_ops = &osd_dt_ops;
4144 cfs_spin_lock_init(&o->od_osfs_lock);
4145 o->od_osfs_age = cfs_time_shift_64(-1000);
4146 o->od_capa_hash = init_capa_hash();
4147 if (o->od_capa_hash == NULL) {
4148 dt_device_fini(&o->od_dt_dev);
4149 l = ERR_PTR(-ENOMEM);
4152 l = ERR_PTR(result);
4157 l = ERR_PTR(-ENOMEM);
4161 static struct lu_device *osd_device_free(const struct lu_env *env,
4162 struct lu_device *d)
4164 struct osd_device *o = osd_dev(d);
4167 cleanup_capa_hash(o->od_capa_hash);
4168 dt_device_fini(&o->od_dt_dev);
4173 static int osd_process_config(const struct lu_env *env,
4174 struct lu_device *d, struct lustre_cfg *cfg)
4176 struct osd_device *o = osd_dev(d);
4180 switch(cfg->lcfg_command) {
4182 err = osd_mount(env, o, cfg);
4185 err = osd_shutdown(env, o);
4194 static int osd_recovery_complete(const struct lu_env *env,
4195 struct lu_device *d)
4200 static int osd_prepare(const struct lu_env *env,
4201 struct lu_device *pdev,
4202 struct lu_device *dev)
4204 struct osd_device *osd = osd_dev(dev);
4205 struct lustre_sb_info *lsi;
4206 struct lustre_disk_data *ldd;
4207 struct lustre_mount_info *lmi;
4208 struct osd_thread_info *oti = osd_oti_get(env);
4209 struct dt_object *d;
4213 /* 1. initialize oi before any file create or file open */
4214 result = osd_oi_init(oti, &osd->od_oi_table,
4215 &osd->od_dt_dev, lu2md_dev(pdev));
4219 LASSERT(result > 0);
4220 osd->od_oi_count = result;
4222 lmi = osd->od_mount;
4223 lsi = s2lsi(lmi->lmi_sb);
4226 /* 2. setup local objects */
4227 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4231 /* 3. open remote object dir */
4232 d = dt_store_open(env, lu2dt_dev(dev), "",
4233 remote_obj_dir, &oti->oti_fid);
4235 osd->od_obj_area = d;
4238 result = PTR_ERR(d);
4239 osd->od_obj_area = NULL;
4246 static const struct lu_object_operations osd_lu_obj_ops = {
4247 .loo_object_init = osd_object_init,
4248 .loo_object_delete = osd_object_delete,
4249 .loo_object_release = osd_object_release,
4250 .loo_object_free = osd_object_free,
4251 .loo_object_print = osd_object_print,
4252 .loo_object_invariant = osd_object_invariant
4255 const struct lu_device_operations osd_lu_ops = {
4256 .ldo_object_alloc = osd_object_alloc,
4257 .ldo_process_config = osd_process_config,
4258 .ldo_recovery_complete = osd_recovery_complete,
4259 .ldo_prepare = osd_prepare,
4262 static const struct lu_device_type_operations osd_device_type_ops = {
4263 .ldto_init = osd_type_init,
4264 .ldto_fini = osd_type_fini,
4266 .ldto_start = osd_type_start,
4267 .ldto_stop = osd_type_stop,
4269 .ldto_device_alloc = osd_device_alloc,
4270 .ldto_device_free = osd_device_free,
4272 .ldto_device_init = osd_device_init,
4273 .ldto_device_fini = osd_device_fini
4276 static struct lu_device_type osd_device_type = {
4277 .ldt_tags = LU_DEVICE_DT,
4278 .ldt_name = LUSTRE_OSD_NAME,
4279 .ldt_ops = &osd_device_type_ops,
4280 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4284 * lprocfs legacy support.
4286 static struct obd_ops osd_obd_device_ops = {
4287 .o_owner = THIS_MODULE
4290 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4291 .llod_name = remote_obj_dir,
4292 .llod_oid = OSD_REM_OBJ_DIR_OID,
4294 .llod_feat = &dt_directory_features,
4297 static int __init osd_mod_init(void)
4299 struct lprocfs_static_vars lvars;
4302 llo_local_obj_register(&llod_osd_rem_obj_dir);
4303 lprocfs_osd_init_vars(&lvars);
4304 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4305 LUSTRE_OSD_NAME, &osd_device_type);
4308 static void __exit osd_mod_exit(void)
4310 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4311 class_unregister_type(LUSTRE_OSD_NAME);
4314 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4315 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4316 MODULE_LICENSE("GPL");
4318 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);