4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
60 * struct OBD_{ALLOC,FREE}*()
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
68 #include <lustre_fid.h>
70 #include "osd_internal.h"
73 /* llo_* api support */
74 #include <md_object.h>
76 #ifdef HAVE_LDISKFS_PDO
78 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
79 "ldiskfs with parallel directory operations");
84 static const char dot[] = ".";
85 static const char dotdot[] = "..";
86 static const char remote_obj_dir[] = "REM_OBJ_DIR";
88 static const struct lu_object_operations osd_lu_obj_ops;
89 static const struct dt_object_operations osd_obj_ops;
90 static const struct dt_object_operations osd_obj_ea_ops;
91 static const struct dt_index_operations osd_index_iam_ops;
92 static const struct dt_index_operations osd_index_ea_ops;
94 static int osd_has_index(const struct osd_object *obj)
96 return obj->oo_dt.do_index_ops != NULL;
99 static int osd_object_invariant(const struct lu_object *l)
101 return osd_invariant(osd_obj(l));
104 #ifdef HAVE_QUOTA_SUPPORT
106 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
108 struct md_ucred *uc = md_ucred(env);
113 save->oc_uid = current_fsuid();
114 save->oc_gid = current_fsgid();
115 save->oc_cap = current_cap();
116 if ((tc = prepare_creds())) {
117 tc->fsuid = uc->mu_fsuid;
118 tc->fsgid = uc->mu_fsgid;
121 /* XXX not suboptimal */
122 cfs_curproc_cap_unpack(uc->mu_cap);
126 osd_pop_ctxt(struct osd_ctxt *save)
130 if ((tc = prepare_creds())) {
131 tc->fsuid = save->oc_uid;
132 tc->fsgid = save->oc_gid;
133 tc->cap_effective = save->oc_cap;
140 * Concurrency: doesn't matter
142 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
144 return osd_oti_get(env)->oti_r_locks > 0;
148 * Concurrency: doesn't matter
150 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
152 struct osd_thread_info *oti = osd_oti_get(env);
153 return oti->oti_w_locks > 0 && o->oo_owner == env;
157 * Concurrency: doesn't access mutable data
159 static int osd_root_get(const struct lu_env *env,
160 struct dt_device *dev, struct lu_fid *f)
162 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
166 static inline int osd_qid_type(struct osd_thandle *oh, int i)
168 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
171 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
173 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
176 void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
177 int type, uid_t id, struct inode *inode)
180 int i, allocated = 0;
181 struct osd_object *obj;
185 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
188 /* id entry is allocated in the quota file */
189 if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
192 for (i = 0; i < oh->ot_id_cnt; i++) {
193 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
197 if (unlikely(i >= OSD_MAX_UGID_CNT)) {
198 CERROR("more than %d uid/gids for a transaction?\n", i);
202 oh->ot_id_array[i] = id;
203 osd_qid_set_type(oh, i, type);
205 obj = osd_dt_obj(dt);
206 oh->ot_credits += (allocated || id == 0) ?
207 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
212 * OSD object methods.
216 * Concurrency: no concurrent access is possible that early in object
219 static struct lu_object *osd_object_alloc(const struct lu_env *env,
220 const struct lu_object_header *hdr,
223 struct osd_object *mo;
229 l = &mo->oo_dt.do_lu;
230 dt_object_init(&mo->oo_dt, NULL, d);
231 if (osd_dev(d)->od_iop_mode)
232 mo->oo_dt.do_ops = &osd_obj_ea_ops;
234 mo->oo_dt.do_ops = &osd_obj_ops;
236 l->lo_ops = &osd_lu_obj_ops;
237 cfs_init_rwsem(&mo->oo_sem);
238 cfs_init_rwsem(&mo->oo_ext_idx_sem);
239 cfs_spin_lock_init(&mo->oo_guard);
246 static int osd_get_lma(struct inode *inode, struct dentry *dentry,
247 struct lustre_mdt_attrs *lma)
251 dentry->d_inode = inode;
252 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
255 /* Check LMA compatibility */
256 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
257 CWARN("%.16s: unsupported incompat LMA feature(s) "
259 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
260 inode->i_ino, le32_to_cpu(lma->lma_incompat) &
264 lustre_lma_swab(lma);
267 } else if (rc == 0) {
275 * retrieve object from backend ext fs.
277 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
278 struct osd_inode_id *id)
280 struct inode *inode = NULL;
282 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
284 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
285 id->oii_ino, PTR_ERR(inode));
286 } else if (id->oii_gen != OSD_OII_NOGEN &&
287 inode->i_generation != id->oii_gen) {
288 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
290 id->oii_ino, id->oii_gen, inode->i_generation);
292 inode = ERR_PTR(-ESTALE);
293 } else if (inode->i_nlink == 0) {
294 /* due to parallel readdir and unlink,
295 * we can have dead inode here. */
296 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
297 make_bad_inode(inode);
299 inode = ERR_PTR(-ESTALE);
300 } else if (is_bad_inode(inode)) {
301 CWARN("%s: bad inode: ino = %u\n",
302 dev->od_dt_dev.dd_lu_dev.ld_obd->obd_name, id->oii_ino);
304 inode = ERR_PTR(-ENOENT);
306 if (id->oii_gen == OSD_OII_NOGEN)
307 osd_id_gen(id, inode->i_ino, inode->i_generation);
309 /* Do not update file c/mtime in ldiskfs.
310 * NB: we don't have any lock to protect this because we don't
311 * have reference on osd_object now, but contention with
312 * another lookup + attr_set can't happen in the tiny window
313 * between if (...) and set S_NOCMTIME. */
314 if (!(inode->i_flags & S_NOCMTIME))
315 inode->i_flags |= S_NOCMTIME;
320 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
321 struct osd_inode_id *id, struct lu_fid *fid)
323 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
327 inode = osd_iget(info, dev, id);
331 rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
333 *fid = lma->lma_self_fid;
334 } else if (rc == -ENODATA) {
335 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
343 static int osd_fid_lookup(const struct lu_env *env,
344 struct osd_object *obj, const struct lu_fid *fid)
346 struct osd_thread_info *info;
347 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
348 struct osd_device *dev;
349 struct osd_inode_id *id;
353 LINVRNT(osd_invariant(obj));
354 LASSERT(obj->oo_inode == NULL);
355 LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
357 * This assertion checks that osd layer sees only local
358 * fids. Unfortunately it is somewhat expensive (does a
359 * cache-lookup). Disabling it for production/acceptance-testing.
361 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
365 info = osd_oti_get(env);
370 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
373 result = osd_oi_lookup(info, dev, fid, id);
375 if (result == -ENOENT)
380 inode = osd_iget(info, dev, id);
383 * If fid wasn't found in oi, inode-less object is
384 * created, for which lu_object_exists() returns
385 * false. This is used in a (frequent) case when
386 * objects are created as locking anchors or
387 * place holders for objects yet to be created.
389 result = PTR_ERR(inode);
393 obj->oo_inode = inode;
394 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
395 if (dev->od_iop_mode) {
396 obj->oo_compat_dot_created = 1;
397 obj->oo_compat_dotdot_created = 1;
400 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
401 GOTO(out, result = 0);
403 LASSERT(obj->oo_hl_head == NULL);
404 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
405 if (obj->oo_hl_head == NULL) {
406 obj->oo_inode = NULL;
408 GOTO(out, result = -ENOMEM);
410 GOTO(out, result = 0);
413 LINVRNT(osd_invariant(obj));
418 * Concurrency: shouldn't matter.
420 static void osd_object_init0(struct osd_object *obj)
422 LASSERT(obj->oo_inode != NULL);
423 obj->oo_dt.do_body_ops = &osd_body_ops;
424 obj->oo_dt.do_lu.lo_header->loh_attr |=
425 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
429 * Concurrency: no concurrent access is possible that early in object
432 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
433 const struct lu_object_conf *unused)
435 struct osd_object *obj = osd_obj(l);
438 LINVRNT(osd_invariant(obj));
440 result = osd_fid_lookup(env, obj, lu_object_fid(l));
441 obj->oo_dt.do_body_ops = &osd_body_ops_new;
443 if (obj->oo_inode != NULL)
444 osd_object_init0(obj);
446 LINVRNT(osd_invariant(obj));
451 * Concurrency: no concurrent access is possible that late in object
454 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
456 struct osd_object *obj = osd_obj(l);
458 LINVRNT(osd_invariant(obj));
460 dt_object_fini(&obj->oo_dt);
461 if (obj->oo_hl_head != NULL)
462 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
467 * Concurrency: no concurrent access is possible that late in object
470 static void osd_index_fini(struct osd_object *o)
472 struct iam_container *bag;
474 if (o->oo_dir != NULL) {
475 bag = &o->oo_dir->od_container;
476 if (o->oo_inode != NULL) {
477 if (bag->ic_object == o->oo_inode)
478 iam_container_fini(bag);
480 OBD_FREE_PTR(o->oo_dir);
486 * Concurrency: no concurrent access is possible that late in object
487 * life-cycle (for all existing callers, that is. New callers have to provide
488 * their own locking.)
490 static int osd_inode_unlinked(const struct inode *inode)
492 return inode->i_nlink == 0;
496 OSD_TXN_OI_DELETE_CREDITS = 20,
497 OSD_TXN_INODE_DELETE_CREDITS = 20
504 #if OSD_THANDLE_STATS
506 * Set time when the handle is allocated
508 static void osd_th_alloced(struct osd_thandle *oth)
510 oth->oth_alloced = cfs_time_current();
514 * Set time when the handle started
516 static void osd_th_started(struct osd_thandle *oth)
518 oth->oth_started = cfs_time_current();
522 * Helper function to convert time interval to microseconds packed in
523 * long int (default time units for the counter in "stats" initialized
524 * by lu_time_init() )
526 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
530 cfs_duration_usec(cfs_time_sub(end, start), &val);
531 return val.tv_sec * 1000000 + val.tv_usec;
535 * Check whether the we deal with this handle for too long.
537 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
538 cfs_time_t alloced, cfs_time_t started,
541 cfs_time_t now = cfs_time_current();
543 LASSERT(dev != NULL);
545 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
546 interval_to_usec(alloced, started));
547 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
548 interval_to_usec(started, closed));
549 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
550 interval_to_usec(closed, now));
552 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
553 CWARN("transaction handle %p was open for too long: "
555 "alloced "CFS_TIME_T" ,"
556 "started "CFS_TIME_T" ,"
557 "closed "CFS_TIME_T"\n",
558 oth, now, alloced, started, closed);
559 libcfs_debug_dumpstack(NULL);
563 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
565 cfs_time_t __closed = cfs_time_current(); \
566 cfs_time_t __alloced = oth->oth_alloced; \
567 cfs_time_t __started = oth->oth_started; \
570 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
573 #else /* OSD_THANDLE_STATS */
575 #define osd_th_alloced(h) do {} while(0)
576 #define osd_th_started(h) do {} while(0)
577 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
579 #endif /* OSD_THANDLE_STATS */
582 * Concurrency: doesn't access mutable data.
584 static int osd_param_is_sane(const struct osd_device *dev,
585 const struct thandle *th)
587 struct osd_thandle *oh;
588 oh = container_of0(th, struct osd_thandle, ot_super);
589 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
593 * Concurrency: shouldn't matter.
595 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
596 static void osd_trans_commit_cb(struct super_block *sb,
597 struct journal_callback *jcb, int error)
599 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
602 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
603 struct thandle *th = &oh->ot_super;
604 struct lu_device *lud = &th->th_dev->dd_lu_dev;
605 struct dt_txn_commit_cb *dcb, *tmp;
607 LASSERT(oh->ot_handle == NULL);
610 CERROR("transaction @0x%p commit error: %d\n", th, error);
612 dt_txn_hook_commit(th);
614 /* call per-transaction callbacks if any */
615 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
616 cfs_list_del_init(&dcb->dcb_linkage);
617 dcb->dcb_func(NULL, th, dcb, error);
620 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
624 lu_context_exit(&th->th_ctx);
625 lu_context_fini(&th->th_ctx);
629 static struct thandle *osd_trans_create(const struct lu_env *env,
632 struct osd_thread_info *oti = osd_oti_get(env);
633 struct osd_iobuf *iobuf = &oti->oti_iobuf;
634 struct osd_thandle *oh;
638 /* on pending IO in this thread should left from prev. request */
639 LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
641 th = ERR_PTR(-ENOMEM);
642 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
647 th->th_tags = LCT_TX_HANDLE;
649 oti->oti_dev = osd_dt_dev(d);
650 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
657 * Concurrency: shouldn't matter.
659 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
662 struct osd_thread_info *oti = osd_oti_get(env);
663 struct osd_device *dev = osd_dt_dev(d);
665 struct osd_thandle *oh;
670 LASSERT(current->journal_info == NULL);
672 oh = container_of0(th, struct osd_thandle, ot_super);
674 LASSERT(oh->ot_handle == NULL);
676 rc = dt_txn_hook_start(env, d, th);
680 if (!osd_param_is_sane(dev, th)) {
681 CWARN("%s: too many transaction credits (%d > %d)\n",
682 d->dd_lu_dev.ld_obd->obd_name, oh->ot_credits,
683 osd_journal(dev)->j_max_transaction_buffers);
684 /* XXX Limit the credits to 'max_transaction_buffers', and
685 * let the underlying filesystem to catch the error if
686 * we really need so many credits.
688 * This should be removed when we can calculate the
689 * credits precisely. */
690 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
691 #ifdef OSD_TRACK_DECLARES
692 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
693 oh->ot_declare_attr_set, oh->ot_declare_punch,
694 oh->ot_declare_xattr_set);
695 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
696 oh->ot_declare_create, oh->ot_declare_ref_add,
697 oh->ot_declare_ref_del, oh->ot_declare_write);
698 CERROR(" insert: %d, delete: %d, destroy: %d\n",
699 oh->ot_declare_insert, oh->ot_declare_delete,
700 oh->ot_declare_destroy);
705 * XXX temporary stuff. Some abstraction layer should
708 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
712 LASSERT(oti->oti_txns == 0);
713 lu_context_init(&th->th_ctx, th->th_tags);
714 lu_context_enter(&th->th_ctx);
716 lu_device_get(&d->dd_lu_dev);
717 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
721 * XXX: current rule is that we first start tx,
722 * then lock object(s), but we can't use
723 * this rule for data (due to locking specifics
724 * in ldiskfs). also in long-term we'd like to
725 * use usually-used (locks;tx) ordering. so,
726 * UGLY thing is that we'll use one ordering for
727 * data (ofd) and reverse ordering for metadata
728 * (mdd). then at some point we'll fix the latter
730 if (lu_device_is_md(&d->dd_lu_dev)) {
731 LASSERT(oti->oti_r_locks == 0);
732 LASSERT(oti->oti_w_locks == 0);
745 * Concurrency: shouldn't matter.
747 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
750 struct osd_thandle *oh;
751 struct osd_thread_info *oti = osd_oti_get(env);
752 struct osd_iobuf *iobuf = &oti->oti_iobuf;
756 oh = container_of0(th, struct osd_thandle, ot_super);
758 if (oh->ot_handle != NULL) {
759 handle_t *hdl = oh->ot_handle;
761 hdl->h_sync = th->th_sync;
764 * add commit callback
765 * notice we don't do this in osd_trans_start()
766 * as underlying transaction can change during truncate
768 osd_journal_callback_set(hdl, osd_trans_commit_cb,
771 LASSERT(oti->oti_txns == 1);
774 * XXX: current rule is that we first start tx,
775 * then lock object(s), but we can't use
776 * this rule for data (due to locking specifics
777 * in ldiskfs). also in long-term we'd like to
778 * use usually-used (locks;tx) ordering. so,
779 * UGLY thing is that we'll use one ordering for
780 * data (ofd) and reverse ordering for metadata
781 * (mdd). then at some point we'll fix the latter
783 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
784 LASSERT(oti->oti_r_locks == 0);
785 LASSERT(oti->oti_w_locks == 0);
787 rc = dt_txn_hook_stop(env, th);
789 CERROR("Failure in transaction hook: %d\n", rc);
790 oh->ot_handle = NULL;
791 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
792 rc = ldiskfs_journal_stop(hdl));
794 CERROR("Failure to stop transaction: %d\n", rc);
799 /* as we want IO to journal and data IO be concurrent, we don't block
800 * awaiting data IO completion in osd_do_bio(), instead we wait here
801 * once transaction is submitted to the journal. all reqular requests
802 * don't do direct IO (except read/write), thus this wait_event becomes
805 * IMPORTANT: we have to wait till any IO submited by the thread is
806 * completed otherwise iobuf may be corrupted by different request
808 cfs_wait_event(iobuf->dr_wait,
809 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
811 rc = iobuf->dr_error;
816 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
818 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
821 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
827 * Called just before object is freed. Releases all resources except for
828 * object itself (that is released by osd_object_free()).
830 * Concurrency: no concurrent access is possible that late in object
833 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
835 struct osd_object *obj = osd_obj(l);
836 struct inode *inode = obj->oo_inode;
838 LINVRNT(osd_invariant(obj));
841 * If object is unlinked remove fid->ino mapping from object index.
847 obj->oo_inode = NULL;
852 * Concurrency: ->loo_object_release() is called under site spin-lock.
854 static void osd_object_release(const struct lu_env *env,
860 * Concurrency: shouldn't matter.
862 static int osd_object_print(const struct lu_env *env, void *cookie,
863 lu_printer_t p, const struct lu_object *l)
865 struct osd_object *o = osd_obj(l);
868 if (o->oo_dir != NULL)
869 d = o->oo_dir->od_container.ic_descr;
872 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
874 o->oo_inode ? o->oo_inode->i_ino : 0UL,
875 o->oo_inode ? o->oo_inode->i_generation : 0,
876 d ? d->id_ops->id_name : "plain");
880 * Concurrency: shouldn't matter.
882 int osd_statfs(const struct lu_env *env, struct dt_device *d,
883 struct obd_statfs *sfs)
885 struct osd_device *osd = osd_dt_dev(d);
886 struct super_block *sb = osd_sb(osd);
887 struct kstatfs *ksfs;
890 /* osd_lproc.c call this without env, allocate ksfs for that case */
891 if (unlikely(env == NULL)) {
896 ksfs = &osd_oti_get(env)->oti_ksfs;
899 cfs_spin_lock(&osd->od_osfs_lock);
901 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
902 result = ll_do_statfs(sb, ksfs);
903 if (likely(result == 0)) { /* N.B. statfs can't really fail */
904 osd->od_osfs_age = cfs_time_current_64();
905 statfs_pack(&osd->od_statfs, ksfs);
909 if (likely(result == 0))
910 *sfs = osd->od_statfs;
911 cfs_spin_unlock(&osd->od_osfs_lock);
913 if (unlikely(env == NULL))
920 * Concurrency: doesn't access mutable data.
922 static void osd_conf_get(const struct lu_env *env,
923 const struct dt_device *dev,
924 struct dt_device_param *param)
926 struct super_block *sb = osd_sb(osd_dt_dev(dev));
929 * XXX should be taken from not-yet-existing fs abstraction layer.
931 param->ddp_max_name_len = LDISKFS_NAME_LEN;
932 param->ddp_max_nlink = LDISKFS_LINK_MAX;
933 param->ddp_block_shift = sb->s_blocksize_bits;
934 param->ddp_mntopts = 0;
935 if (test_opt(sb, XATTR_USER))
936 param->ddp_mntopts |= MNTOPT_USERXATTR;
937 if (test_opt(sb, POSIX_ACL))
938 param->ddp_mntopts |= MNTOPT_ACL;
940 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
941 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
942 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
945 param->ddp_max_ea_size = sb->s_blocksize;
950 * Helper function to get and fill the buffer with input values.
952 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
956 buf = &osd_oti_get(env)->oti_buf;
963 * Concurrency: shouldn't matter.
965 static int osd_sync(const struct lu_env *env, struct dt_device *d)
967 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
968 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
972 * Start commit for OSD device.
974 * An implementation of dt_commit_async method for OSD device.
975 * Asychronously starts underlayng fs sync and thereby a transaction
978 * \param env environment
981 * \see dt_device_operations
983 static int osd_commit_async(const struct lu_env *env,
986 struct super_block *s = osd_sb(osd_dt_dev(d));
989 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
990 RETURN(s->s_op->sync_fs(s, 0));
994 * Concurrency: shouldn't matter.
997 static int osd_ro(const struct lu_env *env, struct dt_device *d)
999 struct super_block *sb = osd_sb(osd_dt_dev(d));
1003 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1005 rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1010 * Concurrency: serialization provided by callers.
1012 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1013 int mode, unsigned long timeout, __u32 alg,
1014 struct lustre_capa_key *keys)
1016 struct osd_device *dev = osd_dt_dev(d);
1019 dev->od_fl_capa = mode;
1020 dev->od_capa_timeout = timeout;
1021 dev->od_capa_alg = alg;
1022 dev->od_capa_keys = keys;
1027 * Concurrency: serialization provided by callers.
1029 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1030 struct dt_quota_ctxt *ctxt, void *data)
1032 struct obd_device *obd = (void *)ctxt;
1033 struct vfsmount *mnt = (struct vfsmount *)data;
1036 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1037 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1038 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1039 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1040 obd->obd_lvfs_ctxt.fs = get_ds();
1046 * Note: we do not count into QUOTA here.
1047 * If we mount with --data_journal we may need more.
1049 const int osd_dto_credits_noquota[DTO_NR] = {
1052 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1053 * SINGLEDATA_TRANS_BLOCKS(8)
1054 * XXX Note: maybe iam need more, since iam have more level than
1057 [DTO_INDEX_INSERT] = 16,
1058 [DTO_INDEX_DELETE] = 16,
1062 [DTO_INDEX_UPDATE] = 16,
1064 * Create a object. The same as create object in EXT3.
1065 * DATA_TRANS_BLOCKS(14) +
1066 * INDEX_EXTRA_BLOCKS(8) +
1067 * 3(inode bits, groups, GDT)
1069 [DTO_OBJECT_CREATE] = 25,
1071 * XXX: real credits to be fixed
1073 [DTO_OBJECT_DELETE] = 25,
1075 * Attr set credits (inode)
1077 [DTO_ATTR_SET_BASE] = 1,
1079 * Xattr set. The same as xattr of EXT3.
1080 * DATA_TRANS_BLOCKS(14)
1081 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1082 * are also counted in. Do not know why?
1084 [DTO_XATTR_SET] = 14,
1087 * credits for inode change during write.
1089 [DTO_WRITE_BASE] = 3,
1091 * credits for single block write.
1093 [DTO_WRITE_BLOCK] = 14,
1095 * Attr set credits for chown.
1096 * This is extra credits for setattr, and it is null without quota
1098 [DTO_ATTR_SET_CHOWN]= 0
1101 static const struct dt_device_operations osd_dt_ops = {
1102 .dt_root_get = osd_root_get,
1103 .dt_statfs = osd_statfs,
1104 .dt_trans_create = osd_trans_create,
1105 .dt_trans_start = osd_trans_start,
1106 .dt_trans_stop = osd_trans_stop,
1107 .dt_trans_cb_add = osd_trans_cb_add,
1108 .dt_conf_get = osd_conf_get,
1109 .dt_sync = osd_sync,
1111 .dt_commit_async = osd_commit_async,
1112 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1113 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1116 static void osd_object_read_lock(const struct lu_env *env,
1117 struct dt_object *dt, unsigned role)
1119 struct osd_object *obj = osd_dt_obj(dt);
1120 struct osd_thread_info *oti = osd_oti_get(env);
1122 LINVRNT(osd_invariant(obj));
1124 LASSERT(obj->oo_owner != env);
1125 cfs_down_read_nested(&obj->oo_sem, role);
1127 LASSERT(obj->oo_owner == NULL);
1131 static void osd_object_write_lock(const struct lu_env *env,
1132 struct dt_object *dt, unsigned role)
1134 struct osd_object *obj = osd_dt_obj(dt);
1135 struct osd_thread_info *oti = osd_oti_get(env);
1137 LINVRNT(osd_invariant(obj));
1139 LASSERT(obj->oo_owner != env);
1140 cfs_down_write_nested(&obj->oo_sem, role);
1142 LASSERT(obj->oo_owner == NULL);
1143 obj->oo_owner = env;
1147 static void osd_object_read_unlock(const struct lu_env *env,
1148 struct dt_object *dt)
1150 struct osd_object *obj = osd_dt_obj(dt);
1151 struct osd_thread_info *oti = osd_oti_get(env);
1153 LINVRNT(osd_invariant(obj));
1155 LASSERT(oti->oti_r_locks > 0);
1157 cfs_up_read(&obj->oo_sem);
1160 static void osd_object_write_unlock(const struct lu_env *env,
1161 struct dt_object *dt)
1163 struct osd_object *obj = osd_dt_obj(dt);
1164 struct osd_thread_info *oti = osd_oti_get(env);
1166 LINVRNT(osd_invariant(obj));
1168 LASSERT(obj->oo_owner == env);
1169 LASSERT(oti->oti_w_locks > 0);
1171 obj->oo_owner = NULL;
1172 cfs_up_write(&obj->oo_sem);
1175 static int osd_object_write_locked(const struct lu_env *env,
1176 struct dt_object *dt)
1178 struct osd_object *obj = osd_dt_obj(dt);
1180 LINVRNT(osd_invariant(obj));
1182 return obj->oo_owner == env;
1185 static int capa_is_sane(const struct lu_env *env,
1186 struct osd_device *dev,
1187 struct lustre_capa *capa,
1188 struct lustre_capa_key *keys)
1190 struct osd_thread_info *oti = osd_oti_get(env);
1191 struct lustre_capa *tcapa = &oti->oti_capa;
1192 struct obd_capa *oc;
1196 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1198 if (capa_is_expired(oc)) {
1199 DEBUG_CAPA(D_ERROR, capa, "expired");
1206 if (capa_is_expired_sec(capa)) {
1207 DEBUG_CAPA(D_ERROR, capa, "expired");
1211 cfs_spin_lock(&capa_lock);
1212 for (i = 0; i < 2; i++) {
1213 if (keys[i].lk_keyid == capa->lc_keyid) {
1214 oti->oti_capa_key = keys[i];
1218 cfs_spin_unlock(&capa_lock);
1221 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1225 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1229 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1230 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1234 oc = capa_add(dev->od_capa_hash, capa);
1240 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1241 struct lustre_capa *capa, __u64 opc)
1243 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1244 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1245 struct md_capainfo *ci;
1248 if (!dev->od_fl_capa)
1251 if (capa == BYPASS_CAPA)
1254 ci = md_capainfo(env);
1258 if (ci->mc_auth == LC_ID_NONE)
1262 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1266 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1267 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1272 if (!capa_opc_supported(capa, opc)) {
1273 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1277 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1278 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1285 static struct timespec *osd_inode_time(const struct lu_env *env,
1286 struct inode *inode, __u64 seconds)
1288 struct osd_thread_info *oti = osd_oti_get(env);
1289 struct timespec *t = &oti->oti_time;
1291 t->tv_sec = seconds;
1293 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1298 static void osd_inode_getattr(const struct lu_env *env,
1299 struct inode *inode, struct lu_attr *attr)
1301 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1302 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1303 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1305 attr->la_atime = LTIME_S(inode->i_atime);
1306 attr->la_mtime = LTIME_S(inode->i_mtime);
1307 attr->la_ctime = LTIME_S(inode->i_ctime);
1308 attr->la_mode = inode->i_mode;
1309 attr->la_size = i_size_read(inode);
1310 attr->la_blocks = inode->i_blocks;
1311 attr->la_uid = inode->i_uid;
1312 attr->la_gid = inode->i_gid;
1313 attr->la_flags = LDISKFS_I(inode)->i_flags;
1314 attr->la_nlink = inode->i_nlink;
1315 attr->la_rdev = inode->i_rdev;
1316 attr->la_blksize = ll_inode_blksize(inode);
1317 attr->la_blkbits = inode->i_blkbits;
1320 static int osd_attr_get(const struct lu_env *env,
1321 struct dt_object *dt,
1322 struct lu_attr *attr,
1323 struct lustre_capa *capa)
1325 struct osd_object *obj = osd_dt_obj(dt);
1327 LASSERT(dt_object_exists(dt));
1328 LINVRNT(osd_invariant(obj));
1330 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1333 cfs_spin_lock(&obj->oo_guard);
1334 osd_inode_getattr(env, obj->oo_inode, attr);
1335 cfs_spin_unlock(&obj->oo_guard);
1339 static int osd_declare_attr_set(const struct lu_env *env,
1340 struct dt_object *dt,
1341 const struct lu_attr *attr,
1342 struct thandle *handle)
1344 struct osd_thandle *oh;
1345 struct osd_object *obj;
1347 LASSERT(dt != NULL);
1348 LASSERT(handle != NULL);
1350 obj = osd_dt_obj(dt);
1351 LASSERT(osd_invariant(obj));
1353 oh = container_of0(handle, struct osd_thandle, ot_super);
1354 LASSERT(oh->ot_handle == NULL);
1356 OSD_DECLARE_OP(oh, attr_set);
1357 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1359 if (attr && attr->la_valid & LA_UID) {
1361 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1363 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1365 if (attr && attr->la_valid & LA_GID) {
1367 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1369 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1375 static int osd_inode_setattr(const struct lu_env *env,
1376 struct inode *inode, const struct lu_attr *attr)
1380 bits = attr->la_valid;
1382 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1384 if (bits & LA_ATIME)
1385 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1386 if (bits & LA_CTIME)
1387 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1388 if (bits & LA_MTIME)
1389 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1390 if (bits & LA_SIZE) {
1391 LDISKFS_I(inode)->i_disksize = attr->la_size;
1392 i_size_write(inode, attr->la_size);
1396 /* OSD should not change "i_blocks" which is used by quota.
1397 * "i_blocks" should be changed by ldiskfs only. */
1398 if (bits & LA_BLOCKS)
1399 inode->i_blocks = attr->la_blocks;
1402 inode->i_mode = (inode->i_mode & S_IFMT) |
1403 (attr->la_mode & ~S_IFMT);
1405 inode->i_uid = attr->la_uid;
1407 inode->i_gid = attr->la_gid;
1408 if (bits & LA_NLINK)
1409 inode->i_nlink = attr->la_nlink;
1411 inode->i_rdev = attr->la_rdev;
1413 if (bits & LA_FLAGS) {
1414 /* always keep S_NOCMTIME */
1415 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1421 static int osd_attr_set(const struct lu_env *env,
1422 struct dt_object *dt,
1423 const struct lu_attr *attr,
1424 struct thandle *handle,
1425 struct lustre_capa *capa)
1427 struct osd_object *obj = osd_dt_obj(dt);
1428 struct inode *inode;
1431 LASSERT(handle != NULL);
1432 LASSERT(dt_object_exists(dt));
1433 LASSERT(osd_invariant(obj));
1435 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1438 OSD_EXEC_OP(handle, attr_set);
1440 inode = obj->oo_inode;
1441 #ifdef HAVE_QUOTA_SUPPORT
1442 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1443 (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1444 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1449 if (attr->la_valid & LA_UID)
1450 iattr.ia_valid |= ATTR_UID;
1451 if (attr->la_valid & LA_GID)
1452 iattr.ia_valid |= ATTR_GID;
1453 iattr.ia_uid = attr->la_uid;
1454 iattr.ia_gid = attr->la_gid;
1455 osd_push_ctxt(env, save);
1456 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1463 cfs_spin_lock(&obj->oo_guard);
1464 rc = osd_inode_setattr(env, inode, attr);
1465 cfs_spin_unlock(&obj->oo_guard);
1468 inode->i_sb->s_op->dirty_inode(inode);
1475 * XXX temporary solution.
1477 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1478 struct lu_attr *attr, struct thandle *th)
1483 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1484 struct lu_attr *attr, struct thandle *th)
1486 osd_object_init0(obj);
1487 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1488 unlock_new_inode(obj->oo_inode);
1492 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1493 struct osd_object *obj,
1494 const char *name, const int namelen)
1496 return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1499 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1501 struct dt_allocation_hint *hint,
1505 struct osd_device *osd = osd_obj2dev(obj);
1506 struct osd_thandle *oth;
1507 struct dt_object *parent = NULL;
1508 struct inode *inode;
1509 #ifdef HAVE_QUOTA_SUPPORT
1510 struct osd_ctxt *save = &info->oti_ctxt;
1513 LINVRNT(osd_invariant(obj));
1514 LASSERT(obj->oo_inode == NULL);
1515 LASSERT(obj->oo_hl_head == NULL);
1517 if (S_ISDIR(mode) && ldiskfs_pdo) {
1518 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1519 if (obj->oo_hl_head == NULL)
1523 oth = container_of(th, struct osd_thandle, ot_super);
1524 LASSERT(oth->ot_handle->h_transaction != NULL);
1526 if (hint && hint->dah_parent)
1527 parent = hint->dah_parent;
1529 #ifdef HAVE_QUOTA_SUPPORT
1530 osd_push_ctxt(info->oti_env, save);
1532 inode = ldiskfs_create_inode(oth->ot_handle,
1533 parent ? osd_dt_obj(parent)->oo_inode :
1534 osd_sb(osd)->s_root->d_inode,
1536 #ifdef HAVE_QUOTA_SUPPORT
1539 if (!IS_ERR(inode)) {
1540 /* Do not update file c/mtime in ldiskfs.
1541 * NB: don't need any lock because no contention at this
1543 inode->i_flags |= S_NOCMTIME;
1544 inode->i_state |= I_LUSTRE_NOSCRUB;
1545 obj->oo_inode = inode;
1548 if (obj->oo_hl_head != NULL) {
1549 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1550 obj->oo_hl_head = NULL;
1552 result = PTR_ERR(inode);
1554 LINVRNT(osd_invariant(obj));
1562 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1563 struct lu_attr *attr,
1564 struct dt_allocation_hint *hint,
1565 struct dt_object_format *dof,
1569 struct osd_thandle *oth;
1570 struct osd_device *osd = osd_obj2dev(obj);
1571 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1573 LASSERT(S_ISDIR(attr->la_mode));
1575 oth = container_of(th, struct osd_thandle, ot_super);
1576 LASSERT(oth->ot_handle->h_transaction != NULL);
1577 result = osd_mkfile(info, obj, mode, hint, th);
1578 if (result == 0 && osd->od_iop_mode == 0) {
1579 LASSERT(obj->oo_inode != NULL);
1581 * XXX uh-oh... call low-level iam function directly.
1584 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1585 sizeof (struct osd_fid_pack),
1591 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1592 struct lu_attr *attr,
1593 struct dt_allocation_hint *hint,
1594 struct dt_object_format *dof,
1598 struct osd_thandle *oth;
1599 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1601 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1603 LASSERT(S_ISREG(attr->la_mode));
1605 oth = container_of(th, struct osd_thandle, ot_super);
1606 LASSERT(oth->ot_handle->h_transaction != NULL);
1608 result = osd_mkfile(info, obj, mode, hint, th);
1610 LASSERT(obj->oo_inode != NULL);
1611 if (feat->dif_flags & DT_IND_VARKEY)
1612 result = iam_lvar_create(obj->oo_inode,
1613 feat->dif_keysize_max,
1615 feat->dif_recsize_max,
1618 result = iam_lfix_create(obj->oo_inode,
1619 feat->dif_keysize_max,
1621 feat->dif_recsize_max,
1628 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1629 struct lu_attr *attr,
1630 struct dt_allocation_hint *hint,
1631 struct dt_object_format *dof,
1634 LASSERT(S_ISREG(attr->la_mode));
1635 return osd_mkfile(info, obj, (attr->la_mode &
1636 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1639 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1640 struct lu_attr *attr,
1641 struct dt_allocation_hint *hint,
1642 struct dt_object_format *dof,
1645 LASSERT(S_ISLNK(attr->la_mode));
1646 return osd_mkfile(info, obj, (attr->la_mode &
1647 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1650 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1651 struct lu_attr *attr,
1652 struct dt_allocation_hint *hint,
1653 struct dt_object_format *dof,
1656 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1659 LINVRNT(osd_invariant(obj));
1660 LASSERT(obj->oo_inode == NULL);
1661 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1662 S_ISFIFO(mode) || S_ISSOCK(mode));
1664 result = osd_mkfile(info, obj, mode, hint, th);
1666 LASSERT(obj->oo_inode != NULL);
1667 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1669 LINVRNT(osd_invariant(obj));
1673 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1675 struct dt_allocation_hint *hint,
1676 struct dt_object_format *dof,
1679 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1681 osd_obj_type_f result;
1697 result = osd_mk_index;
1708 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1709 struct dt_object *parent, cfs_umode_t child_mode)
1713 memset(ah, 0, sizeof(*ah));
1714 ah->dah_parent = parent;
1715 ah->dah_mode = child_mode;
1719 * Helper function for osd_object_create()
1721 * \retval 0, on success
1723 static int __osd_object_create(struct osd_thread_info *info,
1724 struct osd_object *obj, struct lu_attr *attr,
1725 struct dt_allocation_hint *hint,
1726 struct dt_object_format *dof,
1732 result = osd_create_pre(info, obj, attr, th);
1734 result = osd_create_type_f(dof->dof_type)(info, obj,
1735 attr, hint, dof, th);
1737 result = osd_create_post(info, obj, attr, th);
1743 * Helper function for osd_object_create()
1745 * \retval 0, on success
1747 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1748 const struct lu_fid *fid, struct thandle *th)
1750 struct osd_thread_info *info = osd_oti_get(env);
1751 struct osd_inode_id *id = &info->oti_id;
1752 struct osd_device *osd = osd_obj2dev(obj);
1753 struct md_ucred *uc = md_ucred(env);
1755 LASSERT(obj->oo_inode != NULL);
1756 LASSERT(uc != NULL);
1758 osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1759 return osd_oi_insert(info, osd, fid, id, th);
1762 static int osd_declare_object_create(const struct lu_env *env,
1763 struct dt_object *dt,
1764 struct lu_attr *attr,
1765 struct dt_allocation_hint *hint,
1766 struct dt_object_format *dof,
1767 struct thandle *handle)
1769 struct osd_thandle *oh;
1771 LASSERT(handle != NULL);
1773 oh = container_of0(handle, struct osd_thandle, ot_super);
1774 LASSERT(oh->ot_handle == NULL);
1776 OSD_DECLARE_OP(oh, create);
1777 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1778 /* XXX: So far, only normal fid needs be inserted into the oi,
1779 * things could be changed later. Revise following code then. */
1780 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1781 OSD_DECLARE_OP(oh, insert);
1782 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1784 /* If this is directory, then we expect . and .. to be inserted as
1785 * well. The one directory block always needs to be created for the
1786 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1787 * block), there is no danger of needing a tree for the first block.
1789 if (attr && S_ISDIR(attr->la_mode)) {
1790 OSD_DECLARE_OP(oh, insert);
1791 OSD_DECLARE_OP(oh, insert);
1792 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1796 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1797 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1802 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1803 struct lu_attr *attr,
1804 struct dt_allocation_hint *hint,
1805 struct dt_object_format *dof,
1808 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1809 struct osd_object *obj = osd_dt_obj(dt);
1810 struct osd_thread_info *info = osd_oti_get(env);
1815 LINVRNT(osd_invariant(obj));
1816 LASSERT(!dt_object_exists(dt));
1817 LASSERT(osd_write_locked(env, obj));
1818 LASSERT(th != NULL);
1820 OSD_EXEC_OP(th, create);
1822 result = __osd_object_create(info, obj, attr, hint, dof, th);
1824 result = __osd_oi_insert(env, obj, fid, th);
1826 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1827 LASSERT(osd_invariant(obj));
1832 * Called to destroy on-disk representation of the object
1834 * Concurrency: must be locked
1836 static int osd_declare_object_destroy(const struct lu_env *env,
1837 struct dt_object *dt,
1840 struct osd_object *obj = osd_dt_obj(dt);
1841 struct inode *inode = obj->oo_inode;
1842 struct osd_thandle *oh;
1846 oh = container_of0(th, struct osd_thandle, ot_super);
1847 LASSERT(oh->ot_handle == NULL);
1850 OSD_DECLARE_OP(oh, destroy);
1851 OSD_DECLARE_OP(oh, delete);
1852 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
1853 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
1855 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
1856 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
1861 static int osd_object_destroy(const struct lu_env *env,
1862 struct dt_object *dt,
1865 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1866 struct osd_object *obj = osd_dt_obj(dt);
1867 struct inode *inode = obj->oo_inode;
1868 struct osd_device *osd = osd_obj2dev(obj);
1869 struct osd_thandle *oh;
1873 oh = container_of0(th, struct osd_thandle, ot_super);
1874 LASSERT(oh->ot_handle);
1876 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1878 /* Parallel control for OI scrub. For most of cases, there is no
1879 * lock contention. So it will not affect unlink performance. */
1880 cfs_mutex_lock(&inode->i_mutex);
1881 if (S_ISDIR(inode->i_mode)) {
1882 LASSERT(osd_inode_unlinked(inode) ||
1883 inode->i_nlink == 1);
1884 cfs_spin_lock(&obj->oo_guard);
1886 cfs_spin_unlock(&obj->oo_guard);
1887 inode->i_sb->s_op->dirty_inode(inode);
1889 LASSERT(osd_inode_unlinked(inode));
1892 OSD_EXEC_OP(th, destroy);
1894 result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
1895 cfs_mutex_unlock(&inode->i_mutex);
1897 /* XXX: add to ext3 orphan list */
1898 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
1900 /* not needed in the cache anymore */
1901 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1907 * Helper function for osd_xattr_set()
1909 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1910 const struct lu_buf *buf, const char *name, int fl)
1912 struct osd_object *obj = osd_dt_obj(dt);
1913 struct inode *inode = obj->oo_inode;
1914 struct osd_thread_info *info = osd_oti_get(env);
1915 struct dentry *dentry = &info->oti_child_dentry;
1919 LASSERT(dt_object_exists(dt));
1920 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1921 LASSERT(osd_write_locked(env, obj));
1923 if (fl & LU_XATTR_REPLACE)
1924 fs_flags |= XATTR_REPLACE;
1926 if (fl & LU_XATTR_CREATE)
1927 fs_flags |= XATTR_CREATE;
1929 dentry->d_inode = inode;
1930 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1931 buf->lb_len, fs_flags);
1936 * Put the fid into lustre_mdt_attrs, and then place the structure
1937 * inode's ea. This fid should not be altered during the life time
1940 * \retval +ve, on success
1941 * \retval -ve, on error
1943 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1945 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1946 const struct lu_fid *fid)
1948 struct osd_thread_info *info = osd_oti_get(env);
1949 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1951 lustre_lma_init(mdt_attrs, fid);
1952 lustre_lma_swab(mdt_attrs);
1953 return __osd_xattr_set(env, dt,
1954 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1955 XATTR_NAME_LMA, LU_XATTR_CREATE);
1960 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1961 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1962 * To have compatilibility with 1.8 ldiskfs driver we need to have
1963 * magic number at start of fid data.
1964 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1967 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1968 const struct dt_rec *fid)
1970 param->edp_magic = LDISKFS_LUFID_MAGIC;
1971 param->edp_len = sizeof(struct lu_fid) + 1;
1973 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1974 (struct lu_fid *)fid);
1978 * Try to read the fid from inode ea into dt_rec, if return value
1979 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1981 * \param fid object fid.
1983 * \retval 0 on success
1985 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1986 __u32 ino, struct lu_fid *fid)
1988 struct osd_thread_info *info = osd_oti_get(env);
1989 struct osd_inode_id *id = &info->oti_id;
1990 struct inode *inode;
1993 osd_id_gen(id, ino, OSD_OII_NOGEN);
1994 inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
1996 RETURN(PTR_ERR(inode));
2003 * OSD layer object create function for interoperability mode (b11826).
2004 * This is mostly similar to osd_object_create(). Only difference being, fid is
2005 * inserted into inode ea here.
2007 * \retval 0, on success
2008 * \retval -ve, on error
2010 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2011 struct lu_attr *attr,
2012 struct dt_allocation_hint *hint,
2013 struct dt_object_format *dof,
2016 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2017 struct osd_object *obj = osd_dt_obj(dt);
2018 struct osd_thread_info *info = osd_oti_get(env);
2023 LASSERT(osd_invariant(obj));
2024 LASSERT(!dt_object_exists(dt));
2025 LASSERT(osd_write_locked(env, obj));
2026 LASSERT(th != NULL);
2028 OSD_EXEC_OP(th, create);
2030 result = __osd_object_create(info, obj, attr, hint, dof, th);
2031 /* objects under osd root shld have igif fid, so dont add fid EA */
2032 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2033 result = osd_ea_fid_set(env, dt, fid);
2036 result = __osd_oi_insert(env, obj, fid, th);
2038 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2039 LINVRNT(osd_invariant(obj));
2043 static int osd_declare_object_ref_add(const struct lu_env *env,
2044 struct dt_object *dt,
2045 struct thandle *handle)
2047 struct osd_thandle *oh;
2049 /* it's possible that object doesn't exist yet */
2050 LASSERT(handle != NULL);
2052 oh = container_of0(handle, struct osd_thandle, ot_super);
2053 LASSERT(oh->ot_handle == NULL);
2055 OSD_DECLARE_OP(oh, ref_add);
2056 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2062 * Concurrency: @dt is write locked.
2064 static int osd_object_ref_add(const struct lu_env *env,
2065 struct dt_object *dt, struct thandle *th)
2067 struct osd_object *obj = osd_dt_obj(dt);
2068 struct inode *inode = obj->oo_inode;
2070 LINVRNT(osd_invariant(obj));
2071 LASSERT(dt_object_exists(dt));
2072 LASSERT(osd_write_locked(env, obj));
2073 LASSERT(th != NULL);
2075 OSD_EXEC_OP(th, ref_add);
2078 * DIR_NLINK feature is set for compatibility reasons if:
2079 * 1) nlinks > LDISKFS_LINK_MAX, or
2080 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2082 * It is easier to always set this flag (rather than check and set),
2083 * since it has less overhead, and the superblock will be dirtied
2084 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2085 * do not actually care whether this flag is set or not.
2087 cfs_spin_lock(&obj->oo_guard);
2089 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2090 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2091 inode->i_nlink == 2)
2094 LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2095 cfs_spin_unlock(&obj->oo_guard);
2096 inode->i_sb->s_op->dirty_inode(inode);
2097 LINVRNT(osd_invariant(obj));
2102 static int osd_declare_object_ref_del(const struct lu_env *env,
2103 struct dt_object *dt,
2104 struct thandle *handle)
2106 struct osd_thandle *oh;
2108 LASSERT(dt_object_exists(dt));
2109 LASSERT(handle != NULL);
2111 oh = container_of0(handle, struct osd_thandle, ot_super);
2112 LASSERT(oh->ot_handle == NULL);
2114 OSD_DECLARE_OP(oh, ref_del);
2115 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2121 * Concurrency: @dt is write locked.
2123 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2126 struct osd_object *obj = osd_dt_obj(dt);
2127 struct inode *inode = obj->oo_inode;
2129 LINVRNT(osd_invariant(obj));
2130 LASSERT(dt_object_exists(dt));
2131 LASSERT(osd_write_locked(env, obj));
2132 LASSERT(th != NULL);
2134 OSD_EXEC_OP(th, ref_del);
2136 cfs_spin_lock(&obj->oo_guard);
2137 LASSERT(inode->i_nlink > 0);
2139 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2140 * then the nlink count is 1. Don't let it be set to 0 or the directory
2141 * inode will be deleted incorrectly. */
2142 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2144 cfs_spin_unlock(&obj->oo_guard);
2145 inode->i_sb->s_op->dirty_inode(inode);
2146 LINVRNT(osd_invariant(obj));
2152 * Get the 64-bit version for an inode.
2154 static int osd_object_version_get(const struct lu_env *env,
2155 struct dt_object *dt, dt_obj_version_t *ver)
2157 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2159 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2160 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2161 *ver = LDISKFS_I(inode)->i_fs_version;
2166 * Concurrency: @dt is read locked.
2168 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2169 struct lu_buf *buf, const char *name,
2170 struct lustre_capa *capa)
2172 struct osd_object *obj = osd_dt_obj(dt);
2173 struct inode *inode = obj->oo_inode;
2174 struct osd_thread_info *info = osd_oti_get(env);
2175 struct dentry *dentry = &info->oti_obj_dentry;
2177 /* version get is not real XATTR but uses xattr API */
2178 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2179 /* for version we are just using xattr API but change inode
2181 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2182 osd_object_version_get(env, dt, buf->lb_buf);
2183 return sizeof(dt_obj_version_t);
2186 LASSERT(dt_object_exists(dt));
2187 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2188 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2190 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2193 dentry->d_inode = inode;
2194 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2198 static int osd_declare_xattr_set(const struct lu_env *env,
2199 struct dt_object *dt,
2200 const struct lu_buf *buf, const char *name,
2201 int fl, struct thandle *handle)
2203 struct osd_thandle *oh;
2205 LASSERT(handle != NULL);
2207 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2208 /* no credits for version */
2212 oh = container_of0(handle, struct osd_thandle, ot_super);
2213 LASSERT(oh->ot_handle == NULL);
2215 OSD_DECLARE_OP(oh, xattr_set);
2216 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2222 * Set the 64-bit version for object
2224 static void osd_object_version_set(const struct lu_env *env,
2225 struct dt_object *dt,
2226 dt_obj_version_t *new_version)
2228 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2230 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2231 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2233 LDISKFS_I(inode)->i_fs_version = *new_version;
2234 /** Version is set after all inode operations are finished,
2235 * so we should mark it dirty here */
2236 inode->i_sb->s_op->dirty_inode(inode);
2240 * Concurrency: @dt is write locked.
2242 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2243 const struct lu_buf *buf, const char *name, int fl,
2244 struct thandle *handle, struct lustre_capa *capa)
2246 LASSERT(handle != NULL);
2248 /* version set is not real XATTR */
2249 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2250 /* for version we are just using xattr API but change inode
2252 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2253 osd_object_version_set(env, dt, buf->lb_buf);
2254 return sizeof(dt_obj_version_t);
2257 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2260 OSD_EXEC_OP(handle, xattr_set);
2261 return __osd_xattr_set(env, dt, buf, name, fl);
2265 * Concurrency: @dt is read locked.
2267 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2268 struct lu_buf *buf, struct lustre_capa *capa)
2270 struct osd_object *obj = osd_dt_obj(dt);
2271 struct inode *inode = obj->oo_inode;
2272 struct osd_thread_info *info = osd_oti_get(env);
2273 struct dentry *dentry = &info->oti_obj_dentry;
2275 LASSERT(dt_object_exists(dt));
2276 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2277 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2279 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2282 dentry->d_inode = inode;
2283 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2286 static int osd_declare_xattr_del(const struct lu_env *env,
2287 struct dt_object *dt, const char *name,
2288 struct thandle *handle)
2290 struct osd_thandle *oh;
2292 LASSERT(dt_object_exists(dt));
2293 LASSERT(handle != NULL);
2295 oh = container_of0(handle, struct osd_thandle, ot_super);
2296 LASSERT(oh->ot_handle == NULL);
2298 OSD_DECLARE_OP(oh, xattr_set);
2299 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2305 * Concurrency: @dt is write locked.
2307 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2308 const char *name, struct thandle *handle,
2309 struct lustre_capa *capa)
2311 struct osd_object *obj = osd_dt_obj(dt);
2312 struct inode *inode = obj->oo_inode;
2313 struct osd_thread_info *info = osd_oti_get(env);
2314 struct dentry *dentry = &info->oti_obj_dentry;
2317 LASSERT(dt_object_exists(dt));
2318 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2319 LASSERT(osd_write_locked(env, obj));
2320 LASSERT(handle != NULL);
2322 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2325 OSD_EXEC_OP(handle, xattr_set);
2327 dentry->d_inode = inode;
2328 rc = inode->i_op->removexattr(dentry, name);
2332 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2333 struct dt_object *dt,
2334 struct lustre_capa *old,
2337 struct osd_thread_info *info = osd_oti_get(env);
2338 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2339 struct osd_object *obj = osd_dt_obj(dt);
2340 struct osd_device *dev = osd_obj2dev(obj);
2341 struct lustre_capa_key *key = &info->oti_capa_key;
2342 struct lustre_capa *capa = &info->oti_capa;
2343 struct obd_capa *oc;
2344 struct md_capainfo *ci;
2348 if (!dev->od_fl_capa)
2349 RETURN(ERR_PTR(-ENOENT));
2351 LASSERT(dt_object_exists(dt));
2352 LINVRNT(osd_invariant(obj));
2354 /* renewal sanity check */
2355 if (old && osd_object_auth(env, dt, old, opc))
2356 RETURN(ERR_PTR(-EACCES));
2358 ci = md_capainfo(env);
2360 RETURN(ERR_PTR(-ENOENT));
2362 switch (ci->mc_auth) {
2366 capa->lc_uid = obj->oo_inode->i_uid;
2367 capa->lc_gid = obj->oo_inode->i_gid;
2368 capa->lc_flags = LC_ID_PLAIN;
2370 case LC_ID_CONVERT: {
2373 s[0] = obj->oo_inode->i_uid;
2374 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2375 s[2] = obj->oo_inode->i_gid;
2376 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2377 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2379 RETURN(ERR_PTR(rc));
2381 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2382 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2383 capa->lc_flags = LC_ID_CONVERT;
2387 RETURN(ERR_PTR(-EINVAL));
2390 capa->lc_fid = *fid;
2392 capa->lc_flags |= dev->od_capa_alg << 24;
2393 capa->lc_timeout = dev->od_capa_timeout;
2394 capa->lc_expiry = 0;
2396 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2398 LASSERT(!capa_is_expired(oc));
2402 cfs_spin_lock(&capa_lock);
2403 *key = dev->od_capa_keys[1];
2404 cfs_spin_unlock(&capa_lock);
2406 capa->lc_keyid = key->lk_keyid;
2407 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2409 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2411 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2412 RETURN(ERR_PTR(rc));
2415 oc = capa_add(dev->od_capa_hash, capa);
2419 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2421 struct osd_object *obj = osd_dt_obj(dt);
2422 struct inode *inode = obj->oo_inode;
2423 struct osd_thread_info *info = osd_oti_get(env);
2424 struct dentry *dentry = &info->oti_obj_dentry;
2425 struct file *file = &info->oti_file;
2430 dentry->d_inode = inode;
2431 file->f_dentry = dentry;
2432 file->f_mapping = inode->i_mapping;
2433 file->f_op = inode->i_fop;
2434 LOCK_INODE_MUTEX(inode);
2435 rc = file->f_op->fsync(file, dentry, 0);
2436 UNLOCK_INODE_MUTEX(inode);
2440 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2443 struct osd_object *obj = osd_dt_obj(dt);
2446 *data = (void *)obj->oo_inode;
2454 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2455 const struct dt_index_features *feat)
2457 struct iam_descr *descr;
2459 if (osd_object_is_root(o))
2460 return feat == &dt_directory_features;
2462 LASSERT(o->oo_dir != NULL);
2464 descr = o->oo_dir->od_container.ic_descr;
2465 if (feat == &dt_directory_features) {
2466 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2472 feat->dif_keysize_min <= descr->id_key_size &&
2473 descr->id_key_size <= feat->dif_keysize_max &&
2474 feat->dif_recsize_min <= descr->id_rec_size &&
2475 descr->id_rec_size <= feat->dif_recsize_max &&
2476 !(feat->dif_flags & (DT_IND_VARKEY |
2477 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2478 ergo(feat->dif_flags & DT_IND_UPDATE,
2479 1 /* XXX check that object (and file system) is
2484 static int osd_iam_container_init(const struct lu_env *env,
2485 struct osd_object *obj,
2486 struct osd_directory *dir)
2488 struct iam_container *bag = &dir->od_container;
2491 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2495 result = iam_container_setup(bag);
2499 if (osd_obj2dev(obj)->od_iop_mode) {
2500 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2502 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2508 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2510 iam_container_fini(bag);
2517 * Concurrency: no external locking is necessary.
2519 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2520 const struct dt_index_features *feat)
2524 struct osd_object *obj = osd_dt_obj(dt);
2525 struct osd_device *osd = osd_obj2dev(obj);
2527 LINVRNT(osd_invariant(obj));
2528 LASSERT(dt_object_exists(dt));
2530 if (osd_object_is_root(obj)) {
2531 dt->do_index_ops = &osd_index_ea_ops;
2533 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2534 dt->do_index_ops = &osd_index_ea_ops;
2535 if (S_ISDIR(obj->oo_inode->i_mode))
2540 } else if (!osd_has_index(obj)) {
2541 struct osd_directory *dir;
2546 cfs_spin_lock(&obj->oo_guard);
2547 if (obj->oo_dir == NULL)
2551 * Concurrent thread allocated container data.
2554 cfs_spin_unlock(&obj->oo_guard);
2556 * Now, that we have container data, serialize its
2559 cfs_down_write(&obj->oo_ext_idx_sem);
2561 * recheck under lock.
2563 if (!osd_has_index(obj))
2564 result = osd_iam_container_init(env, obj, dir);
2567 cfs_up_write(&obj->oo_ext_idx_sem);
2575 if (result == 0 && ea_dir == 0) {
2576 if (!osd_iam_index_probe(env, obj, feat))
2579 LINVRNT(osd_invariant(obj));
2584 static const struct dt_object_operations osd_obj_ops = {
2585 .do_read_lock = osd_object_read_lock,
2586 .do_write_lock = osd_object_write_lock,
2587 .do_read_unlock = osd_object_read_unlock,
2588 .do_write_unlock = osd_object_write_unlock,
2589 .do_write_locked = osd_object_write_locked,
2590 .do_attr_get = osd_attr_get,
2591 .do_declare_attr_set = osd_declare_attr_set,
2592 .do_attr_set = osd_attr_set,
2593 .do_ah_init = osd_ah_init,
2594 .do_declare_create = osd_declare_object_create,
2595 .do_create = osd_object_create,
2596 .do_declare_destroy = osd_declare_object_destroy,
2597 .do_destroy = osd_object_destroy,
2598 .do_index_try = osd_index_try,
2599 .do_declare_ref_add = osd_declare_object_ref_add,
2600 .do_ref_add = osd_object_ref_add,
2601 .do_declare_ref_del = osd_declare_object_ref_del,
2602 .do_ref_del = osd_object_ref_del,
2603 .do_xattr_get = osd_xattr_get,
2604 .do_declare_xattr_set = osd_declare_xattr_set,
2605 .do_xattr_set = osd_xattr_set,
2606 .do_declare_xattr_del = osd_declare_xattr_del,
2607 .do_xattr_del = osd_xattr_del,
2608 .do_xattr_list = osd_xattr_list,
2609 .do_capa_get = osd_capa_get,
2610 .do_object_sync = osd_object_sync,
2611 .do_data_get = osd_data_get,
2615 * dt_object_operations for interoperability mode
2616 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2618 static const struct dt_object_operations osd_obj_ea_ops = {
2619 .do_read_lock = osd_object_read_lock,
2620 .do_write_lock = osd_object_write_lock,
2621 .do_read_unlock = osd_object_read_unlock,
2622 .do_write_unlock = osd_object_write_unlock,
2623 .do_write_locked = osd_object_write_locked,
2624 .do_attr_get = osd_attr_get,
2625 .do_declare_attr_set = osd_declare_attr_set,
2626 .do_attr_set = osd_attr_set,
2627 .do_ah_init = osd_ah_init,
2628 .do_declare_create = osd_declare_object_create,
2629 .do_create = osd_object_ea_create,
2630 .do_declare_destroy = osd_declare_object_destroy,
2631 .do_destroy = osd_object_destroy,
2632 .do_index_try = osd_index_try,
2633 .do_declare_ref_add = osd_declare_object_ref_add,
2634 .do_ref_add = osd_object_ref_add,
2635 .do_declare_ref_del = osd_declare_object_ref_del,
2636 .do_ref_del = osd_object_ref_del,
2637 .do_xattr_get = osd_xattr_get,
2638 .do_declare_xattr_set = osd_declare_xattr_set,
2639 .do_xattr_set = osd_xattr_set,
2640 .do_declare_xattr_del = osd_declare_xattr_del,
2641 .do_xattr_del = osd_xattr_del,
2642 .do_xattr_list = osd_xattr_list,
2643 .do_capa_get = osd_capa_get,
2644 .do_object_sync = osd_object_sync,
2645 .do_data_get = osd_data_get,
2648 static int osd_index_declare_iam_delete(const struct lu_env *env,
2649 struct dt_object *dt,
2650 const struct dt_key *key,
2651 struct thandle *handle)
2653 struct osd_thandle *oh;
2655 oh = container_of0(handle, struct osd_thandle, ot_super);
2656 LASSERT(oh->ot_handle == NULL);
2658 OSD_DECLARE_OP(oh, delete);
2659 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2665 * delete a (key, value) pair from index \a dt specified by \a key
2667 * \param dt osd index object
2668 * \param key key for index
2669 * \param rec record reference
2670 * \param handle transaction handler
2673 * \retval -ve failure
2676 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2677 const struct dt_key *key,
2678 struct thandle *handle,
2679 struct lustre_capa *capa)
2681 struct osd_object *obj = osd_dt_obj(dt);
2682 struct osd_thandle *oh;
2683 struct iam_path_descr *ipd;
2684 struct iam_container *bag = &obj->oo_dir->od_container;
2689 LINVRNT(osd_invariant(obj));
2690 LASSERT(dt_object_exists(dt));
2691 LASSERT(bag->ic_object == obj->oo_inode);
2692 LASSERT(handle != NULL);
2694 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2697 OSD_EXEC_OP(handle, delete);
2699 ipd = osd_idx_ipd_get(env, bag);
2700 if (unlikely(ipd == NULL))
2703 oh = container_of0(handle, struct osd_thandle, ot_super);
2704 LASSERT(oh->ot_handle != NULL);
2705 LASSERT(oh->ot_handle->h_transaction != NULL);
2707 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2708 osd_ipd_put(env, bag, ipd);
2709 LINVRNT(osd_invariant(obj));
2713 static int osd_index_declare_ea_delete(const struct lu_env *env,
2714 struct dt_object *dt,
2715 const struct dt_key *key,
2716 struct thandle *handle)
2718 struct osd_thandle *oh;
2720 LASSERT(dt_object_exists(dt));
2721 LASSERT(handle != NULL);
2723 oh = container_of0(handle, struct osd_thandle, ot_super);
2724 LASSERT(oh->ot_handle == NULL);
2726 OSD_DECLARE_OP(oh, delete);
2727 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2729 LASSERT(osd_dt_obj(dt)->oo_inode);
2730 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2731 osd_dt_obj(dt)->oo_inode);
2732 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2733 osd_dt_obj(dt)->oo_inode);
2738 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2741 struct osd_fid_pack *rec;
2744 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2745 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2746 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2752 * Index delete function for interoperability mode (b11826).
2753 * It will remove the directory entry added by osd_index_ea_insert().
2754 * This entry is needed to maintain name->fid mapping.
2756 * \param key, key i.e. file entry to be deleted
2758 * \retval 0, on success
2759 * \retval -ve, on error
2761 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2762 const struct dt_key *key,
2763 struct thandle *handle,
2764 struct lustre_capa *capa)
2766 struct osd_object *obj = osd_dt_obj(dt);
2767 struct inode *dir = obj->oo_inode;
2768 struct dentry *dentry;
2769 struct osd_thandle *oh;
2770 struct ldiskfs_dir_entry_2 *de;
2771 struct buffer_head *bh;
2772 struct htree_lock *hlock = NULL;
2777 LINVRNT(osd_invariant(obj));
2778 LASSERT(dt_object_exists(dt));
2779 LASSERT(handle != NULL);
2781 OSD_EXEC_OP(handle, delete);
2783 oh = container_of(handle, struct osd_thandle, ot_super);
2784 LASSERT(oh->ot_handle != NULL);
2785 LASSERT(oh->ot_handle->h_transaction != NULL);
2787 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2790 dentry = osd_child_dentry_get(env, obj,
2791 (char *)key, strlen((char *)key));
2793 if (obj->oo_hl_head != NULL) {
2794 hlock = osd_oti_get(env)->oti_hlock;
2795 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
2796 dir, LDISKFS_HLOCK_DEL);
2798 cfs_down_write(&obj->oo_ext_idx_sem);
2801 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
2803 rc = ldiskfs_delete_entry(oh->ot_handle,
2810 ldiskfs_htree_unlock(hlock);
2812 cfs_up_write(&obj->oo_ext_idx_sem);
2814 LASSERT(osd_invariant(obj));
2819 * Lookup index for \a key and copy record to \a rec.
2821 * \param dt osd index object
2822 * \param key key for index
2823 * \param rec record reference
2825 * \retval +ve success : exact mach
2826 * \retval 0 return record with key not greater than \a key
2827 * \retval -ve failure
2829 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2830 struct dt_rec *rec, const struct dt_key *key,
2831 struct lustre_capa *capa)
2833 struct osd_object *obj = osd_dt_obj(dt);
2834 struct iam_path_descr *ipd;
2835 struct iam_container *bag = &obj->oo_dir->od_container;
2836 struct osd_thread_info *oti = osd_oti_get(env);
2837 struct iam_iterator *it = &oti->oti_idx_it;
2838 struct iam_rec *iam_rec;
2843 LASSERT(osd_invariant(obj));
2844 LASSERT(dt_object_exists(dt));
2845 LASSERT(bag->ic_object == obj->oo_inode);
2847 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2850 ipd = osd_idx_ipd_get(env, bag);
2854 /* got ipd now we can start iterator. */
2855 iam_it_init(it, bag, 0, ipd);
2857 rc = iam_it_get(it, (struct iam_key *)key);
2859 if (S_ISDIR(obj->oo_inode->i_mode))
2860 iam_rec = (struct iam_rec *)oti->oti_ldp;
2862 iam_rec = (struct iam_rec *) rec;
2864 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2865 if (S_ISDIR(obj->oo_inode->i_mode))
2866 osd_fid_unpack((struct lu_fid *) rec,
2867 (struct osd_fid_pack *)iam_rec);
2871 osd_ipd_put(env, bag, ipd);
2873 LINVRNT(osd_invariant(obj));
2878 static int osd_index_declare_iam_insert(const struct lu_env *env,
2879 struct dt_object *dt,
2880 const struct dt_rec *rec,
2881 const struct dt_key *key,
2882 struct thandle *handle)
2884 struct osd_thandle *oh;
2886 LASSERT(dt_object_exists(dt));
2887 LASSERT(handle != NULL);
2889 oh = container_of0(handle, struct osd_thandle, ot_super);
2890 LASSERT(oh->ot_handle == NULL);
2892 OSD_DECLARE_OP(oh, insert);
2893 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
2899 * Inserts (key, value) pair in \a dt index object.
2901 * \param dt osd index object
2902 * \param key key for index
2903 * \param rec record reference
2904 * \param th transaction handler
2907 * \retval -ve failure
2909 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2910 const struct dt_rec *rec,
2911 const struct dt_key *key, struct thandle *th,
2912 struct lustre_capa *capa, int ignore_quota)
2914 struct osd_object *obj = osd_dt_obj(dt);
2915 struct iam_path_descr *ipd;
2916 struct osd_thandle *oh;
2917 struct iam_container *bag = &obj->oo_dir->od_container;
2918 #ifdef HAVE_QUOTA_SUPPORT
2919 cfs_cap_t save = cfs_curproc_cap_pack();
2921 struct osd_thread_info *oti = osd_oti_get(env);
2922 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2927 LINVRNT(osd_invariant(obj));
2928 LASSERT(dt_object_exists(dt));
2929 LASSERT(bag->ic_object == obj->oo_inode);
2930 LASSERT(th != NULL);
2932 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2935 OSD_EXEC_OP(th, insert);
2937 ipd = osd_idx_ipd_get(env, bag);
2938 if (unlikely(ipd == NULL))
2941 oh = container_of0(th, struct osd_thandle, ot_super);
2942 LASSERT(oh->ot_handle != NULL);
2943 LASSERT(oh->ot_handle->h_transaction != NULL);
2944 #ifdef HAVE_QUOTA_SUPPORT
2946 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2948 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2950 if (S_ISDIR(obj->oo_inode->i_mode))
2951 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2953 iam_rec = (struct iam_rec *) rec;
2954 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2956 #ifdef HAVE_QUOTA_SUPPORT
2957 cfs_curproc_cap_unpack(save);
2959 osd_ipd_put(env, bag, ipd);
2960 LINVRNT(osd_invariant(obj));
2965 * Calls ldiskfs_add_entry() to add directory entry
2966 * into the directory. This is required for
2967 * interoperability mode (b11826)
2969 * \retval 0, on success
2970 * \retval -ve, on error
2972 static int __osd_ea_add_rec(struct osd_thread_info *info,
2973 struct osd_object *pobj, struct inode *cinode,
2974 const char *name, const struct dt_rec *fid,
2975 struct htree_lock *hlock, struct thandle *th)
2977 struct ldiskfs_dentry_param *ldp;
2978 struct dentry *child;
2979 struct osd_thandle *oth;
2982 oth = container_of(th, struct osd_thandle, ot_super);
2983 LASSERT(oth->ot_handle != NULL);
2984 LASSERT(oth->ot_handle->h_transaction != NULL);
2986 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2988 /* XXX: remove fid_is_igif() check here.
2989 * IGIF check is just to handle insertion of .. when it is 'ROOT',
2990 * it is IGIF now but needs FID in dir entry as well for readdir
2992 * LU-838 should fix that and remove fid_is_igif() check */
2993 if (fid_is_igif((struct lu_fid *)fid) ||
2994 fid_is_norm((struct lu_fid *)fid)) {
2995 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2996 osd_get_ldiskfs_dirent_param(ldp, fid);
2997 child->d_fsdata = (void *)ldp;
2999 child->d_fsdata = NULL;
3001 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3007 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3008 * into the directory.Also sets flags into osd object to
3009 * indicate dot and dotdot are created. This is required for
3010 * interoperability mode (b11826)
3012 * \param dir directory for dot and dotdot fixup.
3013 * \param obj child object for linking
3015 * \retval 0, on success
3016 * \retval -ve, on error
3018 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3019 struct osd_object *dir,
3020 struct inode *parent_dir, const char *name,
3021 const struct dt_rec *dot_fid,
3022 const struct dt_rec *dot_dot_fid,
3025 struct inode *inode = dir->oo_inode;
3026 struct ldiskfs_dentry_param *dot_ldp;
3027 struct ldiskfs_dentry_param *dot_dot_ldp;
3028 struct osd_thandle *oth;
3031 oth = container_of(th, struct osd_thandle, ot_super);
3032 LASSERT(oth->ot_handle->h_transaction != NULL);
3033 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3035 if (strcmp(name, dot) == 0) {
3036 if (dir->oo_compat_dot_created) {
3039 LASSERT(inode == parent_dir);
3040 dir->oo_compat_dot_created = 1;
3043 } else if(strcmp(name, dotdot) == 0) {
3044 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3045 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3047 if (!dir->oo_compat_dot_created)
3049 if (!fid_is_igif((struct lu_fid *)dot_fid)) {
3050 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3051 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3056 /* in case of rename, dotdot is already created */
3057 if (dir->oo_compat_dotdot_created) {
3058 return __osd_ea_add_rec(info, dir, parent_dir, name,
3059 dot_dot_fid, NULL, th);
3062 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3063 inode, dot_ldp, dot_dot_ldp);
3065 dir->oo_compat_dotdot_created = 1;
3073 * It will call the appropriate osd_add* function and return the
3074 * value, return by respective functions.
3076 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3077 struct inode *cinode, const char *name,
3078 const struct dt_rec *fid, struct thandle *th)
3080 struct osd_thread_info *info = osd_oti_get(env);
3081 struct htree_lock *hlock;
3084 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3086 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3088 if (hlock != NULL) {
3089 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3092 cfs_down_write(&pobj->oo_ext_idx_sem);
3094 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3095 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3098 if (hlock != NULL) {
3099 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3100 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3102 cfs_down_write(&pobj->oo_ext_idx_sem);
3105 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3109 ldiskfs_htree_unlock(hlock);
3111 cfs_up_write(&pobj->oo_ext_idx_sem);
3117 * Calls ->lookup() to find dentry. From dentry get inode and
3118 * read inode's ea to get fid. This is required for interoperability
3121 * \retval 0, on success
3122 * \retval -ve, on error
3124 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3125 struct dt_rec *rec, const struct dt_key *key)
3127 struct inode *dir = obj->oo_inode;
3128 struct dentry *dentry;
3129 struct ldiskfs_dir_entry_2 *de;
3130 struct buffer_head *bh;
3131 struct lu_fid *fid = (struct lu_fid *) rec;
3132 struct htree_lock *hlock = NULL;
3136 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3138 dentry = osd_child_dentry_get(env, obj,
3139 (char *)key, strlen((char *)key));
3141 if (obj->oo_hl_head != NULL) {
3142 hlock = osd_oti_get(env)->oti_hlock;
3143 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3144 dir, LDISKFS_HLOCK_LOOKUP);
3146 cfs_down_read(&obj->oo_ext_idx_sem);
3149 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3151 ino = le32_to_cpu(de->inode);
3152 rc = osd_get_fid_from_dentry(de, rec);
3154 /* done with de, release bh */
3157 rc = osd_ea_fid_get(env, obj, ino, fid);
3163 ldiskfs_htree_unlock(hlock);
3165 cfs_up_read(&obj->oo_ext_idx_sem);
3170 * Find the osd object for given fid.
3172 * \param fid need to find the osd object having this fid
3174 * \retval osd_object on success
3175 * \retval -ve on error
3177 struct osd_object *osd_object_find(const struct lu_env *env,
3178 struct dt_object *dt,
3179 const struct lu_fid *fid)
3181 struct lu_device *ludev = dt->do_lu.lo_dev;
3182 struct osd_object *child = NULL;
3183 struct lu_object *luch;
3184 struct lu_object *lo;
3186 luch = lu_object_find(env, ludev, fid, NULL);
3187 if (!IS_ERR(luch)) {
3188 if (lu_object_exists(luch)) {
3189 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3191 child = osd_obj(lo);
3193 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3194 "lu_object can't be located"
3195 DFID"\n", PFID(fid));
3197 if (child == NULL) {
3198 lu_object_put(env, luch);
3199 CERROR("Unable to get osd_object\n");
3200 child = ERR_PTR(-ENOENT);
3203 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3204 "lu_object does not exists "DFID"\n",
3206 lu_object_put(env, luch);
3207 child = ERR_PTR(-ENOENT);
3210 child = (void *)luch;
3216 * Put the osd object once done with it.
3218 * \param obj osd object that needs to be put
3220 static inline void osd_object_put(const struct lu_env *env,
3221 struct osd_object *obj)
3223 lu_object_put(env, &obj->oo_dt.do_lu);
3226 static int osd_index_declare_ea_insert(const struct lu_env *env,
3227 struct dt_object *dt,
3228 const struct dt_rec *rec,
3229 const struct dt_key *key,
3230 struct thandle *handle)
3232 struct osd_thandle *oh;
3234 LASSERT(dt_object_exists(dt));
3235 LASSERT(handle != NULL);
3237 oh = container_of0(handle, struct osd_thandle, ot_super);
3238 LASSERT(oh->ot_handle == NULL);
3240 OSD_DECLARE_OP(oh, insert);
3241 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3243 LASSERT(osd_dt_obj(dt)->oo_inode);
3244 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3245 osd_dt_obj(dt)->oo_inode);
3246 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3247 osd_dt_obj(dt)->oo_inode);
3253 * Index add function for interoperability mode (b11826).
3254 * It will add the directory entry.This entry is needed to
3255 * maintain name->fid mapping.
3257 * \param key it is key i.e. file entry to be inserted
3258 * \param rec it is value of given key i.e. fid
3260 * \retval 0, on success
3261 * \retval -ve, on error
3263 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3264 const struct dt_rec *rec,
3265 const struct dt_key *key, struct thandle *th,
3266 struct lustre_capa *capa, int ignore_quota)
3268 struct osd_object *obj = osd_dt_obj(dt);
3269 struct lu_fid *fid = (struct lu_fid *) rec;
3270 const char *name = (const char *)key;
3271 struct osd_object *child;
3272 #ifdef HAVE_QUOTA_SUPPORT
3273 cfs_cap_t save = cfs_curproc_cap_pack();
3279 LASSERT(osd_invariant(obj));
3280 LASSERT(dt_object_exists(dt));
3281 LASSERT(th != NULL);
3283 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3286 child = osd_object_find(env, dt, fid);
3287 if (!IS_ERR(child)) {
3288 #ifdef HAVE_QUOTA_SUPPORT
3290 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3292 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3294 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3295 #ifdef HAVE_QUOTA_SUPPORT
3296 cfs_curproc_cap_unpack(save);
3298 osd_object_put(env, child);
3300 rc = PTR_ERR(child);
3303 LASSERT(osd_invariant(obj));
3308 * Initialize osd Iterator for given osd index object.
3310 * \param dt osd index object
3313 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3314 struct dt_object *dt,
3316 struct lustre_capa *capa)
3318 struct osd_it_iam *it;
3319 struct osd_thread_info *oti = osd_oti_get(env);
3320 struct osd_object *obj = osd_dt_obj(dt);
3321 struct lu_object *lo = &dt->do_lu;
3322 struct iam_path_descr *ipd;
3323 struct iam_container *bag = &obj->oo_dir->od_container;
3325 LASSERT(lu_object_exists(lo));
3327 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3328 return ERR_PTR(-EACCES);
3331 ipd = osd_it_ipd_get(env, bag);
3332 if (likely(ipd != NULL)) {
3336 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3337 return (struct dt_it *)it;
3339 return ERR_PTR(-ENOMEM);
3343 * free given Iterator.
3346 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3348 struct osd_it_iam *it = (struct osd_it_iam *)di;
3349 struct osd_object *obj = it->oi_obj;
3351 iam_it_fini(&it->oi_it);
3352 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3353 lu_object_put(env, &obj->oo_dt.do_lu);
3357 * Move Iterator to record specified by \a key
3359 * \param di osd iterator
3360 * \param key key for index
3362 * \retval +ve di points to record with least key not larger than key
3363 * \retval 0 di points to exact matched key
3364 * \retval -ve failure
3367 static int osd_it_iam_get(const struct lu_env *env,
3368 struct dt_it *di, const struct dt_key *key)
3370 struct osd_it_iam *it = (struct osd_it_iam *)di;
3372 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3378 * \param di osd iterator
3381 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3383 struct osd_it_iam *it = (struct osd_it_iam *)di;
3385 iam_it_put(&it->oi_it);
3389 * Move iterator by one record
3391 * \param di osd iterator
3393 * \retval +1 end of container reached
3395 * \retval -ve failure
3398 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3400 struct osd_it_iam *it = (struct osd_it_iam *)di;
3402 return iam_it_next(&it->oi_it);
3406 * Return pointer to the key under iterator.
3409 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3410 const struct dt_it *di)
3412 struct osd_it_iam *it = (struct osd_it_iam *)di;
3414 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3418 * Return size of key under iterator (in bytes)
3421 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3423 struct osd_it_iam *it = (struct osd_it_iam *)di;
3425 return iam_it_key_size(&it->oi_it);
3428 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3429 int len, __u16 type)
3431 struct luda_type *lt;
3432 const unsigned align = sizeof(struct luda_type) - 1;
3434 /* check if file type is required */
3435 if (attr & LUDA_TYPE) {
3436 len = (len + align) & ~align;
3438 lt = (void *) ent->lde_name + len;
3439 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3440 ent->lde_attrs |= LUDA_TYPE;
3443 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3447 * build lu direct from backend fs dirent.
3450 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3451 struct lu_fid *fid, __u64 offset,
3452 char *name, __u16 namelen,
3453 __u16 type, __u32 attr)
3455 fid_cpu_to_le(&ent->lde_fid, fid);
3456 ent->lde_attrs = LUDA_FID;
3458 ent->lde_hash = cpu_to_le64(offset);
3459 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3461 strncpy(ent->lde_name, name, namelen);
3462 ent->lde_namelen = cpu_to_le16(namelen);
3464 /* append lustre attributes */
3465 osd_it_append_attrs(ent, attr, namelen, type);
3469 * Return pointer to the record under iterator.
3471 static int osd_it_iam_rec(const struct lu_env *env,
3472 const struct dt_it *di,
3473 struct dt_rec *dtrec, __u32 attr)
3475 struct osd_it_iam *it = (struct osd_it_iam *)di;
3476 struct osd_thread_info *info = osd_oti_get(env);
3477 struct lu_fid *fid = &info->oti_fid;
3478 const struct osd_fid_pack *rec;
3479 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3485 name = (char *)iam_it_key_get(&it->oi_it);
3487 RETURN(PTR_ERR(name));
3489 namelen = iam_it_key_size(&it->oi_it);
3491 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3493 RETURN(PTR_ERR(rec));
3495 rc = osd_fid_unpack(fid, rec);
3499 hash = iam_it_store(&it->oi_it);
3501 /* IAM does not store object type in IAM index (dir) */
3502 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3509 * Returns cookie for current Iterator position.
3511 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3513 struct osd_it_iam *it = (struct osd_it_iam *)di;
3515 return iam_it_store(&it->oi_it);
3519 * Restore iterator from cookie.
3521 * \param di osd iterator
3522 * \param hash Iterator location cookie
3524 * \retval +ve di points to record with least key not larger than key.
3525 * \retval 0 di points to exact matched key
3526 * \retval -ve failure
3529 static int osd_it_iam_load(const struct lu_env *env,
3530 const struct dt_it *di, __u64 hash)
3532 struct osd_it_iam *it = (struct osd_it_iam *)di;
3534 return iam_it_load(&it->oi_it, hash);
3537 static const struct dt_index_operations osd_index_iam_ops = {
3538 .dio_lookup = osd_index_iam_lookup,
3539 .dio_declare_insert = osd_index_declare_iam_insert,
3540 .dio_insert = osd_index_iam_insert,
3541 .dio_declare_delete = osd_index_declare_iam_delete,
3542 .dio_delete = osd_index_iam_delete,
3544 .init = osd_it_iam_init,
3545 .fini = osd_it_iam_fini,
3546 .get = osd_it_iam_get,
3547 .put = osd_it_iam_put,
3548 .next = osd_it_iam_next,
3549 .key = osd_it_iam_key,
3550 .key_size = osd_it_iam_key_size,
3551 .rec = osd_it_iam_rec,
3552 .store = osd_it_iam_store,
3553 .load = osd_it_iam_load
3558 * Creates or initializes iterator context.
3560 * \retval struct osd_it_ea, iterator structure on success
3563 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3564 struct dt_object *dt,
3566 struct lustre_capa *capa)
3568 struct osd_object *obj = osd_dt_obj(dt);
3569 struct osd_thread_info *info = osd_oti_get(env);
3570 struct osd_it_ea *it = &info->oti_it_ea;
3571 struct lu_object *lo = &dt->do_lu;
3572 struct dentry *obj_dentry = &info->oti_it_dentry;
3574 LASSERT(lu_object_exists(lo));
3576 obj_dentry->d_inode = obj->oo_inode;
3577 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3578 obj_dentry->d_name.hash = 0;
3580 it->oie_rd_dirent = 0;
3581 it->oie_it_dirent = 0;
3582 it->oie_dirent = NULL;
3583 it->oie_buf = info->oti_it_ea_buf;
3585 it->oie_file.f_pos = 0;
3586 it->oie_file.f_dentry = obj_dentry;
3587 if (attr & LUDA_64BITHASH)
3588 it->oie_file.f_flags = O_64BITHASH;
3590 it->oie_file.f_flags = O_32BITHASH;
3591 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3592 it->oie_file.f_op = obj->oo_inode->i_fop;
3593 it->oie_file.private_data = NULL;
3595 RETURN((struct dt_it *) it);
3599 * Destroy or finishes iterator context.
3601 * \param di iterator structure to be destroyed
3603 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3605 struct osd_it_ea *it = (struct osd_it_ea *)di;
3606 struct osd_object *obj = it->oie_obj;
3607 struct inode *inode = obj->oo_inode;
3610 it->oie_file.f_op->release(inode, &it->oie_file);
3611 lu_object_put(env, &obj->oo_dt.do_lu);
3616 * It position the iterator at given key, so that next lookup continues from
3617 * that key Or it is similar to dio_it->load() but based on a key,
3618 * rather than file position.
3620 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3623 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3625 static int osd_it_ea_get(const struct lu_env *env,
3626 struct dt_it *di, const struct dt_key *key)
3628 struct osd_it_ea *it = (struct osd_it_ea *)di;
3631 LASSERT(((const char *)key)[0] == '\0');
3632 it->oie_file.f_pos = 0;
3633 it->oie_rd_dirent = 0;
3634 it->oie_it_dirent = 0;
3635 it->oie_dirent = NULL;
3643 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3648 * It is called internally by ->readdir(). It fills the
3649 * iterator's in-memory data structure with required
3650 * information i.e. name, namelen, rec_size etc.
3652 * \param buf in which information to be filled in.
3653 * \param name name of the file in given dir
3655 * \retval 0 on success
3656 * \retval 1 on buffer full
3658 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3659 loff_t offset, __u64 ino,
3662 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3663 struct osd_it_ea_dirent *ent = it->oie_dirent;
3664 struct lu_fid *fid = &ent->oied_fid;
3665 struct osd_fid_pack *rec;
3668 /* this should never happen */
3669 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3670 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3674 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3678 if (d_type & LDISKFS_DIRENT_LUFID) {
3679 rec = (struct osd_fid_pack*) (name + namelen + 1);
3681 if (osd_fid_unpack(fid, rec) != 0)
3684 d_type &= ~LDISKFS_DIRENT_LUFID;
3689 ent->oied_ino = ino;
3690 ent->oied_off = offset;
3691 ent->oied_namelen = namelen;
3692 ent->oied_type = d_type;
3694 memcpy(ent->oied_name, name, namelen);
3696 it->oie_rd_dirent++;
3697 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3702 * Calls ->readdir() to load a directory entry at a time
3703 * and stored it in iterator's in-memory data structure.
3705 * \param di iterator's in memory structure
3707 * \retval 0 on success
3708 * \retval -ve on error
3710 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3711 const struct dt_it *di)
3713 struct osd_it_ea *it = (struct osd_it_ea *)di;
3714 struct osd_object *obj = it->oie_obj;
3715 struct inode *inode = obj->oo_inode;
3716 struct htree_lock *hlock = NULL;
3720 it->oie_dirent = it->oie_buf;
3721 it->oie_rd_dirent = 0;
3723 if (obj->oo_hl_head != NULL) {
3724 hlock = osd_oti_get(env)->oti_hlock;
3725 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3726 inode, LDISKFS_HLOCK_READDIR);
3728 cfs_down_read(&obj->oo_ext_idx_sem);
3731 result = inode->i_fop->readdir(&it->oie_file, it,
3732 (filldir_t) osd_ldiskfs_filldir);
3735 ldiskfs_htree_unlock(hlock);
3737 cfs_up_read(&obj->oo_ext_idx_sem);
3739 if (it->oie_rd_dirent == 0) {
3742 it->oie_dirent = it->oie_buf;
3743 it->oie_it_dirent = 1;
3750 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3751 * to load a directory entry at a time and stored it in
3752 * iterator's in-memory data structure.
3754 * \param di iterator's in memory structure
3756 * \retval +ve iterator reached to end
3757 * \retval 0 iterator not reached to end
3758 * \retval -ve on error
3760 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3762 struct osd_it_ea *it = (struct osd_it_ea *)di;
3767 if (it->oie_it_dirent < it->oie_rd_dirent) {
3769 (void *) it->oie_dirent +
3770 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3771 it->oie_dirent->oied_namelen);
3772 it->oie_it_dirent++;
3775 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3778 rc = osd_ldiskfs_it_fill(env, di);
3785 * Returns the key at current position from iterator's in memory structure.
3787 * \param di iterator's in memory structure
3789 * \retval key i.e. struct dt_key on success
3791 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3792 const struct dt_it *di)
3794 struct osd_it_ea *it = (struct osd_it_ea *)di;
3796 return (struct dt_key *)it->oie_dirent->oied_name;
3800 * Returns the key's size at current position from iterator's in memory structure.
3802 * \param di iterator's in memory structure
3804 * \retval key_size i.e. struct dt_key on success
3806 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3808 struct osd_it_ea *it = (struct osd_it_ea *)di;
3810 return it->oie_dirent->oied_namelen;
3815 * Returns the value (i.e. fid/igif) at current position from iterator's
3816 * in memory structure.
3818 * \param di struct osd_it_ea, iterator's in memory structure
3819 * \param attr attr requested for dirent.
3820 * \param lde lustre dirent
3822 * \retval 0 no error and \param lde has correct lustre dirent.
3823 * \retval -ve on error
3825 static inline int osd_it_ea_rec(const struct lu_env *env,
3826 const struct dt_it *di,
3827 struct dt_rec *dtrec, __u32 attr)
3829 struct osd_it_ea *it = (struct osd_it_ea *)di;
3830 struct osd_object *obj = it->oie_obj;
3831 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3832 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3837 if (!fid_is_sane(fid))
3838 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3841 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3842 it->oie_dirent->oied_name,
3843 it->oie_dirent->oied_namelen,
3844 it->oie_dirent->oied_type,
3850 * Returns a cookie for current position of the iterator head, so that
3851 * user can use this cookie to load/start the iterator next time.
3853 * \param di iterator's in memory structure
3855 * \retval cookie for current position, on success
3857 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3859 struct osd_it_ea *it = (struct osd_it_ea *)di;
3861 return it->oie_dirent->oied_off;
3865 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3866 * to load a directory entry at a time and stored it i inn,
3867 * in iterator's in-memory data structure.
3869 * \param di struct osd_it_ea, iterator's in memory structure
3871 * \retval +ve on success
3872 * \retval -ve on error
3874 static int osd_it_ea_load(const struct lu_env *env,
3875 const struct dt_it *di, __u64 hash)
3877 struct osd_it_ea *it = (struct osd_it_ea *)di;
3881 it->oie_file.f_pos = hash;
3883 rc = osd_ldiskfs_it_fill(env, di);
3891 * Index lookup function for interoperability mode (b11826).
3893 * \param key, key i.e. file name to be searched
3895 * \retval +ve, on success
3896 * \retval -ve, on error
3898 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3899 struct dt_rec *rec, const struct dt_key *key,
3900 struct lustre_capa *capa)
3902 struct osd_object *obj = osd_dt_obj(dt);
3907 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3908 LINVRNT(osd_invariant(obj));
3910 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3913 rc = osd_ea_lookup_rec(env, obj, rec, key);
3921 * Index and Iterator operations for interoperability
3922 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3924 static const struct dt_index_operations osd_index_ea_ops = {
3925 .dio_lookup = osd_index_ea_lookup,
3926 .dio_declare_insert = osd_index_declare_ea_insert,
3927 .dio_insert = osd_index_ea_insert,
3928 .dio_declare_delete = osd_index_declare_ea_delete,
3929 .dio_delete = osd_index_ea_delete,
3931 .init = osd_it_ea_init,
3932 .fini = osd_it_ea_fini,
3933 .get = osd_it_ea_get,
3934 .put = osd_it_ea_put,
3935 .next = osd_it_ea_next,
3936 .key = osd_it_ea_key,
3937 .key_size = osd_it_ea_key_size,
3938 .rec = osd_it_ea_rec,
3939 .store = osd_it_ea_store,
3940 .load = osd_it_ea_load
3944 static void *osd_key_init(const struct lu_context *ctx,
3945 struct lu_context_key *key)
3947 struct osd_thread_info *info;
3949 OBD_ALLOC_PTR(info);
3951 return ERR_PTR(-ENOMEM);
3953 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3954 if (info->oti_it_ea_buf == NULL)
3957 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
3959 info->oti_hlock = ldiskfs_htree_lock_alloc();
3960 if (info->oti_hlock == NULL)
3966 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3969 return ERR_PTR(-ENOMEM);
3972 static void osd_key_fini(const struct lu_context *ctx,
3973 struct lu_context_key *key, void* data)
3975 struct osd_thread_info *info = data;
3977 if (info->oti_hlock != NULL)
3978 ldiskfs_htree_lock_free(info->oti_hlock);
3979 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3983 static void osd_key_exit(const struct lu_context *ctx,
3984 struct lu_context_key *key, void *data)
3986 struct osd_thread_info *info = data;
3988 LASSERT(info->oti_r_locks == 0);
3989 LASSERT(info->oti_w_locks == 0);
3990 LASSERT(info->oti_txns == 0);
3993 /* type constructor/destructor: osd_type_init, osd_type_fini */
3994 LU_TYPE_INIT_FINI(osd, &osd_key);
3996 struct lu_context_key osd_key = {
3997 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
3998 .lct_init = osd_key_init,
3999 .lct_fini = osd_key_fini,
4000 .lct_exit = osd_key_exit
4004 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4005 const char *name, struct lu_device *next)
4007 return osd_procfs_init(osd_dev(d), name);
4010 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4014 osd_scrub_cleanup(env, o);
4017 fsfilt_put_ops(o->od_fsops);
4024 static int osd_mount(const struct lu_env *env,
4025 struct osd_device *o, struct lustre_cfg *cfg)
4027 struct lustre_mount_info *lmi;
4028 const char *dev = lustre_cfg_string(cfg, 0);
4029 struct lustre_disk_data *ldd;
4030 struct lustre_sb_info *lsi;
4035 o->od_fsops = fsfilt_get_ops(mt_str(LDD_MT_LDISKFS));
4036 if (o->od_fsops == NULL) {
4037 CERROR("Can't find fsfilt_ldiskfs\n");
4041 if (o->od_mount != NULL) {
4042 CERROR("Already mounted (%s)\n", dev);
4047 lmi = server_get_mount(dev);
4049 CERROR("Cannot get mount info for %s!\n", dev);
4053 LASSERT(lmi != NULL);
4054 /* save lustre_mount_info in dt_device */
4056 o->od_mnt = lmi->lmi_mnt;
4058 lsi = s2lsi(lmi->lmi_sb);
4061 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4063 LCONSOLE_WARN("%s: OSD: IAM mode enabled\n", dev);
4067 if (ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
4068 rc = osd_compat_init(o);
4070 CERROR("%s: can't initialize compats: %d\n", dev, rc);
4076 static struct lu_device *osd_device_fini(const struct lu_env *env,
4077 struct lu_device *d)
4082 osd_compat_fini(osd_dev(d));
4084 shrink_dcache_sb(osd_sb(osd_dev(d)));
4085 osd_sync(env, lu2dt_dev(d));
4087 rc = osd_procfs_fini(osd_dev(d));
4089 CERROR("proc fini error %d \n", rc);
4090 RETURN (ERR_PTR(rc));
4093 if (osd_dev(d)->od_mount)
4094 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4095 osd_dev(d)->od_mount->lmi_mnt);
4096 osd_dev(d)->od_mount = NULL;
4101 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4102 struct lu_device_type *t,
4103 struct lustre_cfg *cfg)
4105 struct lu_device *l;
4106 struct osd_device *o;
4112 result = dt_device_init(&o->od_dt_dev, t);
4115 l->ld_ops = &osd_lu_ops;
4116 o->od_dt_dev.dd_ops = &osd_dt_ops;
4117 cfs_spin_lock_init(&o->od_osfs_lock);
4118 cfs_mutex_init(&o->od_otable_mutex);
4119 o->od_osfs_age = cfs_time_shift_64(-1000);
4120 o->od_capa_hash = init_capa_hash();
4121 if (o->od_capa_hash == NULL) {
4122 dt_device_fini(&o->od_dt_dev);
4123 l = ERR_PTR(-ENOMEM);
4126 l = ERR_PTR(result);
4131 l = ERR_PTR(-ENOMEM);
4135 static struct lu_device *osd_device_free(const struct lu_env *env,
4136 struct lu_device *d)
4138 struct osd_device *o = osd_dev(d);
4141 cleanup_capa_hash(o->od_capa_hash);
4142 dt_device_fini(&o->od_dt_dev);
4147 static int osd_process_config(const struct lu_env *env,
4148 struct lu_device *d, struct lustre_cfg *cfg)
4150 struct osd_device *o = osd_dev(d);
4154 switch(cfg->lcfg_command) {
4156 err = osd_mount(env, o, cfg);
4159 err = osd_shutdown(env, o);
4168 static int osd_recovery_complete(const struct lu_env *env,
4169 struct lu_device *d)
4174 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
4175 struct lu_device *dev)
4177 struct osd_device *osd = osd_dev(dev);
4181 /* 1. setup scrub, including OI files initialization */
4182 result = osd_scrub_setup(env, osd);
4186 if (!lu_device_is_md(pdev))
4189 /* 2. setup local objects */
4190 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4194 static const struct lu_object_operations osd_lu_obj_ops = {
4195 .loo_object_init = osd_object_init,
4196 .loo_object_delete = osd_object_delete,
4197 .loo_object_release = osd_object_release,
4198 .loo_object_free = osd_object_free,
4199 .loo_object_print = osd_object_print,
4200 .loo_object_invariant = osd_object_invariant
4203 const struct lu_device_operations osd_lu_ops = {
4204 .ldo_object_alloc = osd_object_alloc,
4205 .ldo_process_config = osd_process_config,
4206 .ldo_recovery_complete = osd_recovery_complete,
4207 .ldo_prepare = osd_prepare,
4210 static const struct lu_device_type_operations osd_device_type_ops = {
4211 .ldto_init = osd_type_init,
4212 .ldto_fini = osd_type_fini,
4214 .ldto_start = osd_type_start,
4215 .ldto_stop = osd_type_stop,
4217 .ldto_device_alloc = osd_device_alloc,
4218 .ldto_device_free = osd_device_free,
4220 .ldto_device_init = osd_device_init,
4221 .ldto_device_fini = osd_device_fini
4224 static struct lu_device_type osd_device_type = {
4225 .ldt_tags = LU_DEVICE_DT,
4226 .ldt_name = LUSTRE_OSD_NAME,
4227 .ldt_ops = &osd_device_type_ops,
4228 .ldt_ctx_tags = LCT_LOCAL,
4232 * lprocfs legacy support.
4234 static struct obd_ops osd_obd_device_ops = {
4235 .o_owner = THIS_MODULE
4238 static int __init osd_mod_init(void)
4240 struct lprocfs_static_vars lvars;
4243 lprocfs_osd_init_vars(&lvars);
4244 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4245 LUSTRE_OSD_NAME, &osd_device_type);
4248 static void __exit osd_mod_exit(void)
4250 class_unregister_type(LUSTRE_OSD_NAME);
4253 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4254 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4255 MODULE_LICENSE("GPL");
4257 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);