1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/osd/osd_handler.c
40 * Top-level entry points into osd module
42 * Author: Nikita Danilov <nikita@clusterfs.com>
43 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
53 /* LUSTRE_VERSION_CODE */
54 #include <lustre_ver.h>
55 /* prerequisite for linux/xattr.h */
56 #include <linux/types.h>
57 /* prerequisite for linux/xattr.h */
59 /* XATTR_{REPLACE,CREATE} */
60 #include <linux/xattr.h>
65 * struct OBD_{ALLOC,FREE}*()
68 #include <obd_support.h>
69 /* struct ptlrpc_thread */
70 #include <lustre_net.h>
73 #include <lustre_fid.h>
75 #include "osd_internal.h"
78 /* llo_* api support */
79 #include <md_object.h>
81 #ifdef HAVE_LDISKFS_PDO
83 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
84 "ldiskfs with parallel directory operations");
89 static const char dot[] = ".";
90 static const char dotdot[] = "..";
91 static const char remote_obj_dir[] = "REM_OBJ_DIR";
93 static const struct lu_object_operations osd_lu_obj_ops;
94 static const struct dt_object_operations osd_obj_ops;
95 static const struct dt_object_operations osd_obj_ea_ops;
96 static const struct dt_body_operations osd_body_ops_new;
97 static const struct dt_index_operations osd_index_iam_ops;
98 static const struct dt_index_operations osd_index_ea_ops;
100 static int osd_has_index(const struct osd_object *obj)
102 return obj->oo_dt.do_index_ops != NULL;
105 static int osd_object_invariant(const struct lu_object *l)
107 return osd_invariant(osd_obj(l));
110 #ifdef HAVE_QUOTA_SUPPORT
112 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
114 struct md_ucred *uc = md_ucred(env);
119 save->oc_uid = current_fsuid();
120 save->oc_gid = current_fsgid();
121 save->oc_cap = current_cap();
122 if ((tc = prepare_creds())) {
123 tc->fsuid = uc->mu_fsuid;
124 tc->fsgid = uc->mu_fsgid;
127 /* XXX not suboptimal */
128 cfs_curproc_cap_unpack(uc->mu_cap);
132 osd_pop_ctxt(struct osd_ctxt *save)
136 if ((tc = prepare_creds())) {
137 tc->fsuid = save->oc_uid;
138 tc->fsgid = save->oc_gid;
139 tc->cap_effective = save->oc_cap;
146 * Concurrency: doesn't matter
148 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
150 return osd_oti_get(env)->oti_r_locks > 0;
154 * Concurrency: doesn't matter
156 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
158 struct osd_thread_info *oti = osd_oti_get(env);
159 return oti->oti_w_locks > 0 && o->oo_owner == env;
163 * Concurrency: doesn't access mutable data
165 static int osd_root_get(const struct lu_env *env,
166 struct dt_device *dev, struct lu_fid *f)
170 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
171 LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
175 static inline int osd_qid_type(struct osd_thandle *oh, int i)
177 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
180 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
182 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
185 static void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
186 int type, uid_t id, struct inode *inode)
189 int i, allocated = 0;
190 struct osd_object *obj;
194 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
197 /* id entry is allocated in the quota file */
198 if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
201 for (i = 0; i < oh->ot_id_cnt; i++) {
202 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
206 if (unlikely(i >= OSD_MAX_UGID_CNT)) {
207 CERROR("more than %d uid/gids for a transaction?\n", i);
211 oh->ot_id_array[i] = id;
212 osd_qid_set_type(oh, i, type);
214 obj = osd_dt_obj(dt);
215 oh->ot_credits += (allocated || id == 0) ?
216 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
221 * OSD object methods.
225 * Concurrency: no concurrent access is possible that early in object
228 static struct lu_object *osd_object_alloc(const struct lu_env *env,
229 const struct lu_object_header *hdr,
232 struct osd_object *mo;
238 l = &mo->oo_dt.do_lu;
239 dt_object_init(&mo->oo_dt, NULL, d);
240 if (osd_dev(d)->od_iop_mode)
241 mo->oo_dt.do_ops = &osd_obj_ea_ops;
243 mo->oo_dt.do_ops = &osd_obj_ops;
245 l->lo_ops = &osd_lu_obj_ops;
246 cfs_init_rwsem(&mo->oo_sem);
247 cfs_init_rwsem(&mo->oo_ext_idx_sem);
248 cfs_spin_lock_init(&mo->oo_guard);
256 * retrieve object from backend ext fs.
258 static struct inode *osd_iget(struct osd_thread_info *info,
259 struct osd_device *dev,
260 const struct osd_inode_id *id)
262 struct inode *inode = NULL;
264 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
266 CERROR("Cannot get inode, rc = %li\n", PTR_ERR(inode));
267 } else if (id->oii_gen != OSD_OII_NOGEN &&
268 inode->i_generation != id->oii_gen) {
270 inode = ERR_PTR(-ESTALE);
271 } else if (inode->i_nlink == 0) {
272 /* due to parallel readdir and unlink,
273 * we can have dead inode here. */
274 CWARN("stale inode\n");
275 make_bad_inode(inode);
277 inode = ERR_PTR(-ESTALE);
278 } else if (is_bad_inode(inode)) {
279 CERROR("bad inode %lx\n",inode->i_ino);
281 inode = ERR_PTR(-ENOENT);
283 /* Do not update file c/mtime in ldiskfs.
284 * NB: we don't have any lock to protect this because we don't
285 * have reference on osd_object now, but contention with
286 * another lookup + attr_set can't happen in the tiny window
287 * between if (...) and set S_NOCMTIME. */
288 if (!(inode->i_flags & S_NOCMTIME))
289 inode->i_flags |= S_NOCMTIME;
294 static int osd_fid_lookup(const struct lu_env *env,
295 struct osd_object *obj, const struct lu_fid *fid)
297 struct osd_thread_info *info;
298 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
299 struct osd_device *dev;
300 struct osd_inode_id *id;
304 LINVRNT(osd_invariant(obj));
305 LASSERT(obj->oo_inode == NULL);
306 LASSERTF(fid_is_sane(fid) || osd_fid_is_root(fid), DFID, PFID(fid));
308 * This assertion checks that osd layer sees only local
309 * fids. Unfortunately it is somewhat expensive (does a
310 * cache-lookup). Disabling it for production/acceptance-testing.
312 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
316 info = osd_oti_get(env);
320 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
323 result = osd_oi_lookup(info, osd_fid2oi(dev, fid), fid, id);
325 if (result == -ENOENT)
330 inode = osd_iget(info, dev, id);
333 * If fid wasn't found in oi, inode-less object is
334 * created, for which lu_object_exists() returns
335 * false. This is used in a (frequent) case when
336 * objects are created as locking anchors or
337 * place holders for objects yet to be created.
339 result = PTR_ERR(inode);
343 obj->oo_inode = inode;
344 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
345 if (dev->od_iop_mode) {
346 obj->oo_compat_dot_created = 1;
347 obj->oo_compat_dotdot_created = 1;
350 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
353 LASSERT(obj->oo_hl_head == NULL);
354 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
355 if (obj->oo_hl_head == NULL) {
356 obj->oo_inode = NULL;
361 LINVRNT(osd_invariant(obj));
367 * Concurrency: shouldn't matter.
369 static void osd_object_init0(struct osd_object *obj)
371 LASSERT(obj->oo_inode != NULL);
372 obj->oo_dt.do_body_ops = &osd_body_ops;
373 obj->oo_dt.do_lu.lo_header->loh_attr |=
374 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
378 * Concurrency: no concurrent access is possible that early in object
381 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
382 const struct lu_object_conf *unused)
384 struct osd_object *obj = osd_obj(l);
387 LINVRNT(osd_invariant(obj));
389 result = osd_fid_lookup(env, obj, lu_object_fid(l));
390 obj->oo_dt.do_body_ops = &osd_body_ops_new;
392 if (obj->oo_inode != NULL)
393 osd_object_init0(obj);
395 LINVRNT(osd_invariant(obj));
400 * Concurrency: no concurrent access is possible that late in object
403 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
405 struct osd_object *obj = osd_obj(l);
407 LINVRNT(osd_invariant(obj));
409 dt_object_fini(&obj->oo_dt);
410 if (obj->oo_hl_head != NULL)
411 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
418 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
419 const struct iam_container *bag)
421 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
422 osd_oti_get(env)->oti_it_ipd);
425 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
426 const struct iam_container *bag)
428 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
429 osd_oti_get(env)->oti_idx_ipd);
432 static void osd_ipd_put(const struct lu_env *env,
433 const struct iam_container *bag,
434 struct iam_path_descr *ipd)
436 bag->ic_descr->id_ops->id_ipd_free(ipd);
440 * Concurrency: no concurrent access is possible that late in object
443 static void osd_index_fini(struct osd_object *o)
445 struct iam_container *bag;
447 if (o->oo_dir != NULL) {
448 bag = &o->oo_dir->od_container;
449 if (o->oo_inode != NULL) {
450 if (bag->ic_object == o->oo_inode)
451 iam_container_fini(bag);
453 OBD_FREE_PTR(o->oo_dir);
459 * Concurrency: no concurrent access is possible that late in object
460 * life-cycle (for all existing callers, that is. New callers have to provide
461 * their own locking.)
463 static int osd_inode_unlinked(const struct inode *inode)
465 return inode->i_nlink == 0;
469 OSD_TXN_OI_DELETE_CREDITS = 20,
470 OSD_TXN_INODE_DELETE_CREDITS = 20
477 #if OSD_THANDLE_STATS
479 * Set time when the handle is allocated
481 static void osd_th_alloced(struct osd_thandle *oth)
483 oth->oth_alloced = cfs_time_current();
487 * Set time when the handle started
489 static void osd_th_started(struct osd_thandle *oth)
491 oth->oth_started = cfs_time_current();
495 * Helper function to convert time interval to microseconds packed in
496 * long int (default time units for the counter in "stats" initialized
497 * by lu_time_init() )
499 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
503 cfs_duration_usec(cfs_time_sub(end, start), &val);
504 return val.tv_sec * 1000000 + val.tv_usec;
508 * Check whether the we deal with this handle for too long.
510 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
511 cfs_time_t alloced, cfs_time_t started,
514 cfs_time_t now = cfs_time_current();
516 LASSERT(dev != NULL);
518 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
519 interval_to_usec(alloced, started));
520 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
521 interval_to_usec(started, closed));
522 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
523 interval_to_usec(closed, now));
525 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
526 CWARN("transaction handle %p was open for too long: "
528 "alloced "CFS_TIME_T" ,"
529 "started "CFS_TIME_T" ,"
530 "closed "CFS_TIME_T"\n",
531 oth, now, alloced, started, closed);
532 libcfs_debug_dumpstack(NULL);
536 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
538 cfs_time_t __closed = cfs_time_current(); \
539 cfs_time_t __alloced = oth->oth_alloced; \
540 cfs_time_t __started = oth->oth_started; \
543 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
546 #else /* OSD_THANDLE_STATS */
548 #define osd_th_alloced(h) do {} while(0)
549 #define osd_th_started(h) do {} while(0)
550 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
552 #endif /* OSD_THANDLE_STATS */
555 * Concurrency: doesn't access mutable data.
557 static int osd_param_is_sane(const struct osd_device *dev,
558 const struct thandle *th)
560 struct osd_thandle *oh;
561 oh = container_of0(th, struct osd_thandle, ot_super);
562 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
566 * Concurrency: shouldn't matter.
568 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
569 static void osd_trans_commit_cb(struct super_block *sb,
570 struct journal_callback *jcb, int error)
572 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
575 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
576 struct thandle *th = &oh->ot_super;
577 struct lu_device *lud = &th->th_dev->dd_lu_dev;
578 struct dt_txn_commit_cb *dcb, *tmp;
580 LASSERT(oh->ot_handle == NULL);
583 CERROR("transaction @0x%p commit error: %d\n", th, error);
585 dt_txn_hook_commit(th);
587 /* call per-transaction callbacks if any */
588 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage)
589 dcb->dcb_func(NULL, th, dcb, error);
591 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
595 lu_context_exit(&th->th_ctx);
596 lu_context_fini(&th->th_ctx);
600 static struct thandle *osd_trans_create(const struct lu_env *env,
603 struct osd_thread_info *oti = osd_oti_get(env);
604 struct osd_thandle *oh;
608 th = ERR_PTR(-ENOMEM);
609 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
614 th->th_tags = LCT_TX_HANDLE;
616 oti->oti_dev = osd_dt_dev(d);
617 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
624 * Concurrency: shouldn't matter.
626 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
629 struct osd_thread_info *oti = osd_oti_get(env);
630 struct osd_device *dev = osd_dt_dev(d);
632 struct osd_thandle *oh;
637 LASSERT(current->journal_info == NULL);
639 oh = container_of0(th, struct osd_thandle, ot_super);
641 LASSERT(oh->ot_handle == NULL);
643 rc = dt_txn_hook_start(env, d, th);
647 if (!osd_param_is_sane(dev, th)) {
648 CWARN("%s: too many transaction credits (%d > %d)\n",
649 d->dd_lu_dev.ld_obd->obd_name, oh->ot_credits,
650 osd_journal(dev)->j_max_transaction_buffers);
651 /* XXX Limit the credits to 'max_transaction_buffers', and
652 * let the underlying filesystem to catch the error if
653 * we really need so many credits.
655 * This should be removed when we can calculate the
656 * credits precisely. */
657 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
658 #ifdef OSD_TRACK_DECLARES
659 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
660 oh->ot_declare_attr_set, oh->ot_declare_punch,
661 oh->ot_declare_xattr_set);
662 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
663 oh->ot_declare_create, oh->ot_declare_ref_add,
664 oh->ot_declare_ref_del, oh->ot_declare_write);
665 CERROR(" insert: %d, delete: %d, destroy: %d\n",
666 oh->ot_declare_insert, oh->ot_declare_delete,
667 oh->ot_declare_destroy);
672 * XXX temporary stuff. Some abstraction layer should
675 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
679 LASSERT(oti->oti_txns == 0);
680 lu_context_init(&th->th_ctx, th->th_tags);
681 lu_context_enter(&th->th_ctx);
683 lu_device_get(&d->dd_lu_dev);
684 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
688 * XXX: current rule is that we first start tx,
689 * then lock object(s), but we can't use
690 * this rule for data (due to locking specifics
691 * in ldiskfs). also in long-term we'd like to
692 * use usually-used (locks;tx) ordering. so,
693 * UGLY thing is that we'll use one ordering for
694 * data (ofd) and reverse ordering for metadata
695 * (mdd). then at some point we'll fix the latter
697 if (lu_device_is_md(&d->dd_lu_dev)) {
698 LASSERT(oti->oti_r_locks == 0);
699 LASSERT(oti->oti_w_locks == 0);
712 * Concurrency: shouldn't matter.
714 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
717 struct osd_thandle *oh;
718 struct osd_thread_info *oti = osd_oti_get(env);
722 oh = container_of0(th, struct osd_thandle, ot_super);
724 if (oh->ot_handle != NULL) {
725 handle_t *hdl = oh->ot_handle;
727 hdl->h_sync = th->th_sync;
730 * add commit callback
731 * notice we don't do this in osd_trans_start()
732 * as underlying transaction can change during truncate
734 osd_journal_callback_set(hdl, osd_trans_commit_cb,
737 LASSERT(oti->oti_txns == 1);
740 * XXX: current rule is that we first start tx,
741 * then lock object(s), but we can't use
742 * this rule for data (due to locking specifics
743 * in ldiskfs). also in long-term we'd like to
744 * use usually-used (locks;tx) ordering. so,
745 * UGLY thing is that we'll use one ordering for
746 * data (ofd) and reverse ordering for metadata
747 * (mdd). then at some point we'll fix the latter
749 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
750 LASSERT(oti->oti_r_locks == 0);
751 LASSERT(oti->oti_w_locks == 0);
753 rc = dt_txn_hook_stop(env, th);
755 CERROR("Failure in transaction hook: %d\n", rc);
756 oh->ot_handle = NULL;
757 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
758 rc = ldiskfs_journal_stop(hdl));
760 CERROR("Failure to stop transaction: %d\n", rc);
768 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
770 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
773 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
779 * Called just before object is freed. Releases all resources except for
780 * object itself (that is released by osd_object_free()).
782 * Concurrency: no concurrent access is possible that late in object
785 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
787 struct osd_object *obj = osd_obj(l);
788 struct inode *inode = obj->oo_inode;
790 LINVRNT(osd_invariant(obj));
793 * If object is unlinked remove fid->ino mapping from object index.
799 obj->oo_inode = NULL;
804 * Concurrency: ->loo_object_release() is called under site spin-lock.
806 static void osd_object_release(const struct lu_env *env,
812 * Concurrency: shouldn't matter.
814 static int osd_object_print(const struct lu_env *env, void *cookie,
815 lu_printer_t p, const struct lu_object *l)
817 struct osd_object *o = osd_obj(l);
820 if (o->oo_dir != NULL)
821 d = o->oo_dir->od_container.ic_descr;
824 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
826 o->oo_inode ? o->oo_inode->i_ino : 0UL,
827 o->oo_inode ? o->oo_inode->i_generation : 0,
828 d ? d->id_ops->id_name : "plain");
832 * Concurrency: shouldn't matter.
834 int osd_statfs(const struct lu_env *env, struct dt_device *d,
837 struct osd_device *osd = osd_dt_dev(d);
838 struct super_block *sb = osd_sb(osd);
841 cfs_spin_lock(&osd->od_osfs_lock);
843 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
844 result = ll_do_statfs(sb, &osd->od_kstatfs);
845 if (likely(result == 0)) /* N.B. statfs can't really fail */
846 osd->od_osfs_age = cfs_time_current_64();
849 if (likely(result == 0))
850 *sfs = osd->od_kstatfs;
851 cfs_spin_unlock(&osd->od_osfs_lock);
857 * Concurrency: doesn't access mutable data.
859 static void osd_conf_get(const struct lu_env *env,
860 const struct dt_device *dev,
861 struct dt_device_param *param)
863 struct super_block *sb = osd_sb(osd_dt_dev(dev));
866 * XXX should be taken from not-yet-existing fs abstraction layer.
868 param->ddp_max_name_len = LDISKFS_NAME_LEN;
869 param->ddp_max_nlink = LDISKFS_LINK_MAX;
870 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
871 param->ddp_mntopts = 0;
872 if (test_opt(sb, XATTR_USER))
873 param->ddp_mntopts |= MNTOPT_USERXATTR;
874 if (test_opt(sb, POSIX_ACL))
875 param->ddp_mntopts |= MNTOPT_ACL;
877 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
878 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
879 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
882 param->ddp_max_ea_size = sb->s_blocksize;
887 * Helper function to get and fill the buffer with input values.
889 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
893 buf = &osd_oti_get(env)->oti_buf;
900 * Concurrency: shouldn't matter.
902 static int osd_sync(const struct lu_env *env, struct dt_device *d)
904 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
905 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
909 * Start commit for OSD device.
911 * An implementation of dt_commit_async method for OSD device.
912 * Asychronously starts underlayng fs sync and thereby a transaction
915 * \param env environment
918 * \see dt_device_operations
920 static int osd_commit_async(const struct lu_env *env,
923 struct super_block *s = osd_sb(osd_dt_dev(d));
926 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
927 RETURN(s->s_op->sync_fs(s, 0));
931 * Concurrency: shouldn't matter.
934 static void osd_ro(const struct lu_env *env, struct dt_device *d)
936 struct super_block *sb = osd_sb(osd_dt_dev(d));
939 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
941 __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
946 * Concurrency: serialization provided by callers.
948 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
949 int mode, unsigned long timeout, __u32 alg,
950 struct lustre_capa_key *keys)
952 struct osd_device *dev = osd_dt_dev(d);
955 dev->od_fl_capa = mode;
956 dev->od_capa_timeout = timeout;
957 dev->od_capa_alg = alg;
958 dev->od_capa_keys = keys;
963 * Concurrency: serialization provided by callers.
965 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
966 struct dt_quota_ctxt *ctxt, void *data)
968 struct obd_device *obd = (void *)ctxt;
969 struct vfsmount *mnt = (struct vfsmount *)data;
972 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
973 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
974 obd->obd_lvfs_ctxt.pwdmnt = mnt;
975 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
976 obd->obd_lvfs_ctxt.fs = get_ds();
982 * Note: we do not count into QUOTA here.
983 * If we mount with --data_journal we may need more.
985 const int osd_dto_credits_noquota[DTO_NR] = {
988 * INDEX_EXTRA_TRANS_BLOCKS(8) +
989 * SINGLEDATA_TRANS_BLOCKS(8)
990 * XXX Note: maybe iam need more, since iam have more level than
993 [DTO_INDEX_INSERT] = 16,
994 [DTO_INDEX_DELETE] = 16,
998 [DTO_INDEX_UPDATE] = 16,
1000 * Create a object. The same as create object in EXT3.
1001 * DATA_TRANS_BLOCKS(14) +
1002 * INDEX_EXTRA_BLOCKS(8) +
1003 * 3(inode bits, groups, GDT)
1005 [DTO_OBJECT_CREATE] = 25,
1007 * XXX: real credits to be fixed
1009 [DTO_OBJECT_DELETE] = 25,
1011 * Attr set credits (inode)
1013 [DTO_ATTR_SET_BASE] = 1,
1015 * Xattr set. The same as xattr of EXT3.
1016 * DATA_TRANS_BLOCKS(14)
1017 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1018 * are also counted in. Do not know why?
1020 [DTO_XATTR_SET] = 14,
1023 * credits for inode change during write.
1025 [DTO_WRITE_BASE] = 3,
1027 * credits for single block write.
1029 [DTO_WRITE_BLOCK] = 14,
1031 * Attr set credits for chown.
1032 * This is extra credits for setattr, and it is null without quota
1034 [DTO_ATTR_SET_CHOWN]= 0
1037 static const struct dt_device_operations osd_dt_ops = {
1038 .dt_root_get = osd_root_get,
1039 .dt_statfs = osd_statfs,
1040 .dt_trans_create = osd_trans_create,
1041 .dt_trans_start = osd_trans_start,
1042 .dt_trans_stop = osd_trans_stop,
1043 .dt_trans_cb_add = osd_trans_cb_add,
1044 .dt_conf_get = osd_conf_get,
1045 .dt_sync = osd_sync,
1047 .dt_commit_async = osd_commit_async,
1048 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1049 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1052 static void osd_object_read_lock(const struct lu_env *env,
1053 struct dt_object *dt, unsigned role)
1055 struct osd_object *obj = osd_dt_obj(dt);
1056 struct osd_thread_info *oti = osd_oti_get(env);
1058 LINVRNT(osd_invariant(obj));
1060 LASSERT(obj->oo_owner != env);
1061 cfs_down_read_nested(&obj->oo_sem, role);
1063 LASSERT(obj->oo_owner == NULL);
1067 static void osd_object_write_lock(const struct lu_env *env,
1068 struct dt_object *dt, unsigned role)
1070 struct osd_object *obj = osd_dt_obj(dt);
1071 struct osd_thread_info *oti = osd_oti_get(env);
1073 LINVRNT(osd_invariant(obj));
1075 LASSERT(obj->oo_owner != env);
1076 cfs_down_write_nested(&obj->oo_sem, role);
1078 LASSERT(obj->oo_owner == NULL);
1079 obj->oo_owner = env;
1083 static void osd_object_read_unlock(const struct lu_env *env,
1084 struct dt_object *dt)
1086 struct osd_object *obj = osd_dt_obj(dt);
1087 struct osd_thread_info *oti = osd_oti_get(env);
1089 LINVRNT(osd_invariant(obj));
1091 LASSERT(oti->oti_r_locks > 0);
1093 cfs_up_read(&obj->oo_sem);
1096 static void osd_object_write_unlock(const struct lu_env *env,
1097 struct dt_object *dt)
1099 struct osd_object *obj = osd_dt_obj(dt);
1100 struct osd_thread_info *oti = osd_oti_get(env);
1102 LINVRNT(osd_invariant(obj));
1104 LASSERT(obj->oo_owner == env);
1105 LASSERT(oti->oti_w_locks > 0);
1107 obj->oo_owner = NULL;
1108 cfs_up_write(&obj->oo_sem);
1111 static int osd_object_write_locked(const struct lu_env *env,
1112 struct dt_object *dt)
1114 struct osd_object *obj = osd_dt_obj(dt);
1116 LINVRNT(osd_invariant(obj));
1118 return obj->oo_owner == env;
1121 static int capa_is_sane(const struct lu_env *env,
1122 struct osd_device *dev,
1123 struct lustre_capa *capa,
1124 struct lustre_capa_key *keys)
1126 struct osd_thread_info *oti = osd_oti_get(env);
1127 struct lustre_capa *tcapa = &oti->oti_capa;
1128 struct obd_capa *oc;
1132 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1134 if (capa_is_expired(oc)) {
1135 DEBUG_CAPA(D_ERROR, capa, "expired");
1142 if (capa_is_expired_sec(capa)) {
1143 DEBUG_CAPA(D_ERROR, capa, "expired");
1147 cfs_spin_lock(&capa_lock);
1148 for (i = 0; i < 2; i++) {
1149 if (keys[i].lk_keyid == capa->lc_keyid) {
1150 oti->oti_capa_key = keys[i];
1154 cfs_spin_unlock(&capa_lock);
1157 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1161 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1165 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1166 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1170 oc = capa_add(dev->od_capa_hash, capa);
1176 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1177 struct lustre_capa *capa, __u64 opc)
1179 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1180 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1181 struct md_capainfo *ci;
1184 if (!dev->od_fl_capa)
1187 if (capa == BYPASS_CAPA)
1190 ci = md_capainfo(env);
1194 if (ci->mc_auth == LC_ID_NONE)
1198 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1202 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1203 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1208 if (!capa_opc_supported(capa, opc)) {
1209 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1213 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1214 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1221 static struct timespec *osd_inode_time(const struct lu_env *env,
1222 struct inode *inode, __u64 seconds)
1224 struct osd_thread_info *oti = osd_oti_get(env);
1225 struct timespec *t = &oti->oti_time;
1227 t->tv_sec = seconds;
1229 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1234 static void osd_inode_getattr(const struct lu_env *env,
1235 struct inode *inode, struct lu_attr *attr)
1237 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1238 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1239 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1241 attr->la_atime = LTIME_S(inode->i_atime);
1242 attr->la_mtime = LTIME_S(inode->i_mtime);
1243 attr->la_ctime = LTIME_S(inode->i_ctime);
1244 attr->la_mode = inode->i_mode;
1245 attr->la_size = i_size_read(inode);
1246 attr->la_blocks = inode->i_blocks;
1247 attr->la_uid = inode->i_uid;
1248 attr->la_gid = inode->i_gid;
1249 attr->la_flags = LDISKFS_I(inode)->i_flags;
1250 attr->la_nlink = inode->i_nlink;
1251 attr->la_rdev = inode->i_rdev;
1252 attr->la_blksize = ll_inode_blksize(inode);
1253 attr->la_blkbits = inode->i_blkbits;
1256 static int osd_attr_get(const struct lu_env *env,
1257 struct dt_object *dt,
1258 struct lu_attr *attr,
1259 struct lustre_capa *capa)
1261 struct osd_object *obj = osd_dt_obj(dt);
1263 LASSERT(dt_object_exists(dt));
1264 LINVRNT(osd_invariant(obj));
1266 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1269 cfs_spin_lock(&obj->oo_guard);
1270 osd_inode_getattr(env, obj->oo_inode, attr);
1271 cfs_spin_unlock(&obj->oo_guard);
1275 static int osd_declare_attr_set(const struct lu_env *env,
1276 struct dt_object *dt,
1277 const struct lu_attr *attr,
1278 struct thandle *handle)
1280 struct osd_thandle *oh;
1281 struct osd_object *obj;
1283 LASSERT(dt != NULL);
1284 LASSERT(handle != NULL);
1286 obj = osd_dt_obj(dt);
1287 LASSERT(osd_invariant(obj));
1289 oh = container_of0(handle, struct osd_thandle, ot_super);
1290 LASSERT(oh->ot_handle == NULL);
1292 OSD_DECLARE_OP(oh, attr_set);
1293 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1295 if (attr && attr->la_valid & LA_UID) {
1297 osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1299 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1301 if (attr && attr->la_valid & LA_GID) {
1303 osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1305 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1311 static int osd_inode_setattr(const struct lu_env *env,
1312 struct inode *inode, const struct lu_attr *attr)
1316 bits = attr->la_valid;
1318 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1320 #ifdef HAVE_QUOTA_SUPPORT
1321 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1322 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1323 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1329 iattr.ia_valid |= ATTR_UID;
1331 iattr.ia_valid |= ATTR_GID;
1332 iattr.ia_uid = attr->la_uid;
1333 iattr.ia_gid = attr->la_gid;
1334 osd_push_ctxt(env, save);
1335 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1342 if (bits & LA_ATIME)
1343 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1344 if (bits & LA_CTIME)
1345 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1346 if (bits & LA_MTIME)
1347 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1348 if (bits & LA_SIZE) {
1349 LDISKFS_I(inode)->i_disksize = attr->la_size;
1350 i_size_write(inode, attr->la_size);
1354 /* OSD should not change "i_blocks" which is used by quota.
1355 * "i_blocks" should be changed by ldiskfs only. */
1356 if (bits & LA_BLOCKS)
1357 inode->i_blocks = attr->la_blocks;
1360 inode->i_mode = (inode->i_mode & S_IFMT) |
1361 (attr->la_mode & ~S_IFMT);
1363 inode->i_uid = attr->la_uid;
1365 inode->i_gid = attr->la_gid;
1366 if (bits & LA_NLINK)
1367 inode->i_nlink = attr->la_nlink;
1369 inode->i_rdev = attr->la_rdev;
1371 if (bits & LA_FLAGS) {
1372 /* always keep S_NOCMTIME */
1373 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1379 static int osd_attr_set(const struct lu_env *env,
1380 struct dt_object *dt,
1381 const struct lu_attr *attr,
1382 struct thandle *handle,
1383 struct lustre_capa *capa)
1385 struct osd_object *obj = osd_dt_obj(dt);
1388 LASSERT(handle != NULL);
1389 LASSERT(dt_object_exists(dt));
1390 LASSERT(osd_invariant(obj));
1392 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1395 OSD_EXEC_OP(handle, attr_set);
1397 cfs_spin_lock(&obj->oo_guard);
1398 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1399 cfs_spin_unlock(&obj->oo_guard);
1402 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1409 * XXX temporary solution.
1411 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1412 struct lu_attr *attr, struct thandle *th)
1417 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1418 struct lu_attr *attr, struct thandle *th)
1420 osd_object_init0(obj);
1421 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1422 unlock_new_inode(obj->oo_inode);
1426 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1427 struct osd_object *obj,
1431 struct osd_thread_info *info = osd_oti_get(env);
1432 struct dentry *child_dentry = &info->oti_child_dentry;
1433 struct dentry *obj_dentry = &info->oti_obj_dentry;
1435 obj_dentry->d_inode = obj->oo_inode;
1436 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1437 obj_dentry->d_name.hash = 0;
1439 child_dentry->d_name.hash = 0;
1440 child_dentry->d_parent = obj_dentry;
1441 child_dentry->d_name.name = name;
1442 child_dentry->d_name.len = namelen;
1443 return child_dentry;
1447 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1449 struct dt_allocation_hint *hint,
1453 struct osd_device *osd = osd_obj2dev(obj);
1454 struct osd_thandle *oth;
1455 struct dt_object *parent;
1456 struct inode *inode;
1457 #ifdef HAVE_QUOTA_SUPPORT
1458 struct osd_ctxt *save = &info->oti_ctxt;
1461 LINVRNT(osd_invariant(obj));
1462 LASSERT(obj->oo_inode == NULL);
1463 LASSERT(obj->oo_hl_head == NULL);
1465 if (S_ISDIR(mode) && ldiskfs_pdo) {
1466 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1467 if (obj->oo_hl_head == NULL)
1471 oth = container_of(th, struct osd_thandle, ot_super);
1472 LASSERT(oth->ot_handle->h_transaction != NULL);
1474 if (hint && hint->dah_parent)
1475 parent = hint->dah_parent;
1477 parent = osd->od_obj_area;
1479 LASSERT(parent != NULL);
1480 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1482 #ifdef HAVE_QUOTA_SUPPORT
1483 osd_push_ctxt(info->oti_env, save);
1485 inode = ldiskfs_create_inode(oth->ot_handle,
1486 osd_dt_obj(parent)->oo_inode, mode);
1487 #ifdef HAVE_QUOTA_SUPPORT
1490 if (!IS_ERR(inode)) {
1491 /* Do not update file c/mtime in ldiskfs.
1492 * NB: don't need any lock because no contention at this
1494 inode->i_flags |= S_NOCMTIME;
1495 obj->oo_inode = inode;
1498 if (obj->oo_hl_head != NULL) {
1499 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1500 obj->oo_hl_head = NULL;
1502 result = PTR_ERR(inode);
1504 LINVRNT(osd_invariant(obj));
1512 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1513 struct lu_attr *attr,
1514 struct dt_allocation_hint *hint,
1515 struct dt_object_format *dof,
1519 struct osd_thandle *oth;
1520 struct osd_device *osd = osd_obj2dev(obj);
1521 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1523 LASSERT(S_ISDIR(attr->la_mode));
1525 oth = container_of(th, struct osd_thandle, ot_super);
1526 LASSERT(oth->ot_handle->h_transaction != NULL);
1527 result = osd_mkfile(info, obj, mode, hint, th);
1528 if (result == 0 && osd->od_iop_mode == 0) {
1529 LASSERT(obj->oo_inode != NULL);
1531 * XXX uh-oh... call low-level iam function directly.
1534 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1535 sizeof (struct osd_fid_pack),
1541 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1542 struct lu_attr *attr,
1543 struct dt_allocation_hint *hint,
1544 struct dt_object_format *dof,
1548 struct osd_thandle *oth;
1549 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1551 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1553 LASSERT(S_ISREG(attr->la_mode));
1555 oth = container_of(th, struct osd_thandle, ot_super);
1556 LASSERT(oth->ot_handle->h_transaction != NULL);
1558 result = osd_mkfile(info, obj, mode, hint, th);
1560 LASSERT(obj->oo_inode != NULL);
1561 if (feat->dif_flags & DT_IND_VARKEY)
1562 result = iam_lvar_create(obj->oo_inode,
1563 feat->dif_keysize_max,
1565 feat->dif_recsize_max,
1568 result = iam_lfix_create(obj->oo_inode,
1569 feat->dif_keysize_max,
1571 feat->dif_recsize_max,
1578 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1579 struct lu_attr *attr,
1580 struct dt_allocation_hint *hint,
1581 struct dt_object_format *dof,
1584 LASSERT(S_ISREG(attr->la_mode));
1585 return osd_mkfile(info, obj, (attr->la_mode &
1586 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1589 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1590 struct lu_attr *attr,
1591 struct dt_allocation_hint *hint,
1592 struct dt_object_format *dof,
1595 LASSERT(S_ISLNK(attr->la_mode));
1596 return osd_mkfile(info, obj, (attr->la_mode &
1597 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1600 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1601 struct lu_attr *attr,
1602 struct dt_allocation_hint *hint,
1603 struct dt_object_format *dof,
1606 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1609 LINVRNT(osd_invariant(obj));
1610 LASSERT(obj->oo_inode == NULL);
1611 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1612 S_ISFIFO(mode) || S_ISSOCK(mode));
1614 result = osd_mkfile(info, obj, mode, hint, th);
1616 LASSERT(obj->oo_inode != NULL);
1617 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1619 LINVRNT(osd_invariant(obj));
1623 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1625 struct dt_allocation_hint *hint,
1626 struct dt_object_format *dof,
1629 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1631 osd_obj_type_f result;
1647 result = osd_mk_index;
1658 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1659 struct dt_object *parent, cfs_umode_t child_mode)
1663 memset(ah, 0, sizeof(*ah));
1664 ah->dah_parent = parent;
1665 ah->dah_mode = child_mode;
1669 * Helper function for osd_object_create()
1671 * \retval 0, on success
1673 static int __osd_object_create(struct osd_thread_info *info,
1674 struct osd_object *obj, struct lu_attr *attr,
1675 struct dt_allocation_hint *hint,
1676 struct dt_object_format *dof,
1682 result = osd_create_pre(info, obj, attr, th);
1684 result = osd_create_type_f(dof->dof_type)(info, obj,
1685 attr, hint, dof, th);
1687 result = osd_create_post(info, obj, attr, th);
1693 * Helper function for osd_object_create()
1695 * \retval 0, on success
1697 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1698 const struct lu_fid *fid, struct thandle *th)
1700 struct osd_thread_info *info = osd_oti_get(env);
1701 struct osd_inode_id *id = &info->oti_id;
1702 struct osd_device *osd = osd_obj2dev(obj);
1703 struct md_ucred *uc = md_ucred(env);
1705 LASSERT(obj->oo_inode != NULL);
1706 LASSERT(uc != NULL);
1708 id->oii_ino = obj->oo_inode->i_ino;
1709 id->oii_gen = obj->oo_inode->i_generation;
1711 return osd_oi_insert(info, osd_fid2oi(osd, fid), fid, id, th,
1712 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1715 static int osd_declare_object_create(const struct lu_env *env,
1716 struct dt_object *dt,
1717 struct lu_attr *attr,
1718 struct dt_allocation_hint *hint,
1719 struct dt_object_format *dof,
1720 struct thandle *handle)
1722 struct osd_thandle *oh;
1724 LASSERT(handle != NULL);
1726 oh = container_of0(handle, struct osd_thandle, ot_super);
1727 LASSERT(oh->ot_handle == NULL);
1729 OSD_DECLARE_OP(oh, create);
1730 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1731 /* XXX: So far, only normal fid needs be inserted into the oi,
1732 * things could be changed later. Revise following code then. */
1733 if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1734 OSD_DECLARE_OP(oh, insert);
1735 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1737 /* If this is directory, then we expect . and .. to be inserted as
1738 * well. The one directory block always needs to be created for the
1739 * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1740 * block), there is no danger of needing a tree for the first block.
1742 if (attr && S_ISDIR(attr->la_mode)) {
1743 OSD_DECLARE_OP(oh, insert);
1744 OSD_DECLARE_OP(oh, insert);
1745 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1749 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1750 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1755 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1756 struct lu_attr *attr,
1757 struct dt_allocation_hint *hint,
1758 struct dt_object_format *dof,
1761 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1762 struct osd_object *obj = osd_dt_obj(dt);
1763 struct osd_thread_info *info = osd_oti_get(env);
1768 LINVRNT(osd_invariant(obj));
1769 LASSERT(!dt_object_exists(dt));
1770 LASSERT(osd_write_locked(env, obj));
1771 LASSERT(th != NULL);
1773 OSD_EXEC_OP(th, create);
1775 result = __osd_object_create(info, obj, attr, hint, dof, th);
1777 result = __osd_oi_insert(env, obj, fid, th);
1779 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1780 LASSERT(osd_invariant(obj));
1785 * Called to destroy on-disk representation of the object
1787 * Concurrency: must be locked
1789 static int osd_declare_object_destroy(const struct lu_env *env,
1790 struct dt_object *dt,
1793 struct osd_object *obj = osd_dt_obj(dt);
1794 struct inode *inode = obj->oo_inode;
1795 struct osd_thandle *oh;
1799 oh = container_of0(th, struct osd_thandle, ot_super);
1800 LASSERT(oh->ot_handle == NULL);
1803 OSD_DECLARE_OP(oh, destroy);
1804 OSD_DECLARE_OP(oh, delete);
1805 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
1806 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
1808 osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
1809 osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
1814 static int osd_object_destroy(const struct lu_env *env,
1815 struct dt_object *dt,
1818 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1819 struct osd_object *obj = osd_dt_obj(dt);
1820 struct inode *inode = obj->oo_inode;
1821 struct osd_device *osd = osd_obj2dev(obj);
1822 struct osd_thandle *oh;
1826 oh = container_of0(th, struct osd_thandle, ot_super);
1827 LASSERT(oh->ot_handle);
1829 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1831 if (S_ISDIR(inode->i_mode)) {
1832 LASSERT(osd_inode_unlinked(inode) ||
1833 inode->i_nlink == 1);
1834 cfs_spin_lock(&obj->oo_guard);
1836 cfs_spin_unlock(&obj->oo_guard);
1837 inode->i_sb->s_op->dirty_inode(inode);
1839 LASSERT(osd_inode_unlinked(inode));
1842 OSD_EXEC_OP(th, destroy);
1844 result = osd_oi_delete(osd_oti_get(env),
1845 osd_fid2oi(osd, fid), fid, th);
1847 /* XXX: add to ext3 orphan list */
1848 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
1850 /* not needed in the cache anymore */
1851 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1857 * Helper function for osd_xattr_set()
1859 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1860 const struct lu_buf *buf, const char *name, int fl)
1862 struct osd_object *obj = osd_dt_obj(dt);
1863 struct inode *inode = obj->oo_inode;
1864 struct osd_thread_info *info = osd_oti_get(env);
1865 struct dentry *dentry = &info->oti_child_dentry;
1869 LASSERT(dt_object_exists(dt));
1870 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1871 LASSERT(osd_write_locked(env, obj));
1873 if (fl & LU_XATTR_REPLACE)
1874 fs_flags |= XATTR_REPLACE;
1876 if (fl & LU_XATTR_CREATE)
1877 fs_flags |= XATTR_CREATE;
1879 dentry->d_inode = inode;
1880 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1881 buf->lb_len, fs_flags);
1886 * Put the fid into lustre_mdt_attrs, and then place the structure
1887 * inode's ea. This fid should not be altered during the life time
1890 * \retval +ve, on success
1891 * \retval -ve, on error
1893 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1895 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1896 const struct lu_fid *fid)
1898 struct osd_thread_info *info = osd_oti_get(env);
1899 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1901 lustre_lma_init(mdt_attrs, fid);
1902 lustre_lma_swab(mdt_attrs);
1903 return __osd_xattr_set(env, dt,
1904 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1905 XATTR_NAME_LMA, LU_XATTR_CREATE);
1910 * Helper function to form igif
1912 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1915 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1919 * Helper function to pack the fid, ldiskfs stores fid in packed format.
1921 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1922 struct lu_fid *befider)
1924 fid_cpu_to_be(befider, (struct lu_fid *)fid);
1925 memcpy(pack->fp_area, befider, sizeof(*befider));
1926 pack->fp_len = sizeof(*befider) + 1;
1930 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1931 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1932 * To have compatilibility with 1.8 ldiskfs driver we need to have
1933 * magic number at start of fid data.
1934 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1937 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1938 const struct dt_rec *fid)
1940 param->edp_magic = LDISKFS_LUFID_MAGIC;
1941 param->edp_len = sizeof(struct lu_fid) + 1;
1943 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1944 (struct lu_fid *)fid);
1947 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1952 switch (pack->fp_len) {
1953 case sizeof *fid + 1:
1954 memcpy(fid, pack->fp_area, sizeof *fid);
1955 fid_be_to_cpu(fid, fid);
1958 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1965 * Try to read the fid from inode ea into dt_rec, if return value
1966 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1968 * \param fid object fid.
1970 * \retval 0 on success
1972 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1973 __u32 ino, struct lu_fid *fid)
1975 struct osd_thread_info *info = osd_oti_get(env);
1976 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1977 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1978 struct dentry *dentry = &info->oti_child_dentry;
1979 struct osd_inode_id *id = &info->oti_id;
1980 struct osd_device *dev;
1981 struct inode *inode;
1985 dev = osd_dev(ldev);
1988 id->oii_gen = OSD_OII_NOGEN;
1990 inode = osd_iget(info, dev, id);
1991 if (IS_ERR(inode)) {
1992 rc = PTR_ERR(inode);
1995 dentry->d_inode = inode;
1997 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1998 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
2001 /* Check LMA compatibility */
2003 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
2004 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
2005 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
2006 ~LMA_INCOMPAT_SUPP);
2011 lustre_lma_swab(mdt_attrs);
2012 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
2014 } else if (rc == -ENODATA) {
2015 osd_igif_get(env, inode, fid);
2024 * OSD layer object create function for interoperability mode (b11826).
2025 * This is mostly similar to osd_object_create(). Only difference being, fid is
2026 * inserted into inode ea here.
2028 * \retval 0, on success
2029 * \retval -ve, on error
2031 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2032 struct lu_attr *attr,
2033 struct dt_allocation_hint *hint,
2034 struct dt_object_format *dof,
2037 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2038 struct osd_object *obj = osd_dt_obj(dt);
2039 struct osd_thread_info *info = osd_oti_get(env);
2044 LASSERT(osd_invariant(obj));
2045 LASSERT(!dt_object_exists(dt));
2046 LASSERT(osd_write_locked(env, obj));
2047 LASSERT(th != NULL);
2049 OSD_EXEC_OP(th, create);
2051 result = __osd_object_create(info, obj, attr, hint, dof, th);
2053 /* objects under osd root shld have igif fid, so dont add fid EA */
2054 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2055 result = osd_ea_fid_set(env, dt, fid);
2058 result = __osd_oi_insert(env, obj, fid, th);
2060 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2061 LINVRNT(osd_invariant(obj));
2065 static int osd_declare_object_ref_add(const struct lu_env *env,
2066 struct dt_object *dt,
2067 struct thandle *handle)
2069 struct osd_thandle *oh;
2071 /* it's possible that object doesn't exist yet */
2072 LASSERT(handle != NULL);
2074 oh = container_of0(handle, struct osd_thandle, ot_super);
2075 LASSERT(oh->ot_handle == NULL);
2077 OSD_DECLARE_OP(oh, ref_add);
2078 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2084 * Concurrency: @dt is write locked.
2086 static int osd_object_ref_add(const struct lu_env *env,
2087 struct dt_object *dt, struct thandle *th)
2089 struct osd_object *obj = osd_dt_obj(dt);
2090 struct inode *inode = obj->oo_inode;
2092 LINVRNT(osd_invariant(obj));
2093 LASSERT(dt_object_exists(dt));
2094 LASSERT(osd_write_locked(env, obj));
2095 LASSERT(th != NULL);
2097 OSD_EXEC_OP(th, ref_add);
2100 * DIR_NLINK feature is set for compatibility reasons if:
2101 * 1) nlinks > LDISKFS_LINK_MAX, or
2102 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2104 * It is easier to always set this flag (rather than check and set),
2105 * since it has less overhead, and the superblock will be dirtied
2106 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2107 * do not actually care whether this flag is set or not.
2109 cfs_spin_lock(&obj->oo_guard);
2111 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2112 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2113 inode->i_nlink == 2)
2116 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
2117 cfs_spin_unlock(&obj->oo_guard);
2118 inode->i_sb->s_op->dirty_inode(inode);
2119 LINVRNT(osd_invariant(obj));
2124 static int osd_declare_object_ref_del(const struct lu_env *env,
2125 struct dt_object *dt,
2126 struct thandle *handle)
2128 struct osd_thandle *oh;
2130 LASSERT(dt_object_exists(dt));
2131 LASSERT(handle != NULL);
2133 oh = container_of0(handle, struct osd_thandle, ot_super);
2134 LASSERT(oh->ot_handle == NULL);
2136 OSD_DECLARE_OP(oh, ref_del);
2137 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2143 * Concurrency: @dt is write locked.
2145 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2148 struct osd_object *obj = osd_dt_obj(dt);
2149 struct inode *inode = obj->oo_inode;
2151 LINVRNT(osd_invariant(obj));
2152 LASSERT(dt_object_exists(dt));
2153 LASSERT(osd_write_locked(env, obj));
2154 LASSERT(th != NULL);
2156 OSD_EXEC_OP(th, ref_del);
2158 cfs_spin_lock(&obj->oo_guard);
2159 LASSERT(inode->i_nlink > 0);
2161 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2162 * then the nlink count is 1. Don't let it be set to 0 or the directory
2163 * inode will be deleted incorrectly. */
2164 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2166 cfs_spin_unlock(&obj->oo_guard);
2167 inode->i_sb->s_op->dirty_inode(inode);
2168 LINVRNT(osd_invariant(obj));
2174 * Get the 64-bit version for an inode.
2176 static int osd_object_version_get(const struct lu_env *env,
2177 struct dt_object *dt, dt_obj_version_t *ver)
2179 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2181 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2182 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2183 *ver = LDISKFS_I(inode)->i_fs_version;
2188 * Concurrency: @dt is read locked.
2190 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2191 struct lu_buf *buf, const char *name,
2192 struct lustre_capa *capa)
2194 struct osd_object *obj = osd_dt_obj(dt);
2195 struct inode *inode = obj->oo_inode;
2196 struct osd_thread_info *info = osd_oti_get(env);
2197 struct dentry *dentry = &info->oti_obj_dentry;
2199 /* version get is not real XATTR but uses xattr API */
2200 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2201 /* for version we are just using xattr API but change inode
2203 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2204 osd_object_version_get(env, dt, buf->lb_buf);
2205 return sizeof(dt_obj_version_t);
2208 LASSERT(dt_object_exists(dt));
2209 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2210 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2212 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2215 dentry->d_inode = inode;
2216 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2220 static int osd_declare_xattr_set(const struct lu_env *env,
2221 struct dt_object *dt,
2222 const struct lu_buf *buf, const char *name,
2223 int fl, struct thandle *handle)
2225 struct osd_thandle *oh;
2227 LASSERT(handle != NULL);
2229 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2230 /* no credits for version */
2234 oh = container_of0(handle, struct osd_thandle, ot_super);
2235 LASSERT(oh->ot_handle == NULL);
2237 OSD_DECLARE_OP(oh, xattr_set);
2238 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2244 * Set the 64-bit version for object
2246 static void osd_object_version_set(const struct lu_env *env,
2247 struct dt_object *dt,
2248 dt_obj_version_t *new_version)
2250 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2252 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2253 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2255 LDISKFS_I(inode)->i_fs_version = *new_version;
2256 /** Version is set after all inode operations are finished,
2257 * so we should mark it dirty here */
2258 inode->i_sb->s_op->dirty_inode(inode);
2262 * Concurrency: @dt is write locked.
2264 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2265 const struct lu_buf *buf, const char *name, int fl,
2266 struct thandle *handle, struct lustre_capa *capa)
2268 LASSERT(handle != NULL);
2270 /* version set is not real XATTR */
2271 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2272 /* for version we are just using xattr API but change inode
2274 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2275 osd_object_version_set(env, dt, buf->lb_buf);
2276 return sizeof(dt_obj_version_t);
2279 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2282 OSD_EXEC_OP(handle, xattr_set);
2283 return __osd_xattr_set(env, dt, buf, name, fl);
2287 * Concurrency: @dt is read locked.
2289 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2290 struct lu_buf *buf, struct lustre_capa *capa)
2292 struct osd_object *obj = osd_dt_obj(dt);
2293 struct inode *inode = obj->oo_inode;
2294 struct osd_thread_info *info = osd_oti_get(env);
2295 struct dentry *dentry = &info->oti_obj_dentry;
2297 LASSERT(dt_object_exists(dt));
2298 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2299 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2301 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2304 dentry->d_inode = inode;
2305 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2308 static int osd_declare_xattr_del(const struct lu_env *env,
2309 struct dt_object *dt, const char *name,
2310 struct thandle *handle)
2312 struct osd_thandle *oh;
2314 LASSERT(dt_object_exists(dt));
2315 LASSERT(handle != NULL);
2317 oh = container_of0(handle, struct osd_thandle, ot_super);
2318 LASSERT(oh->ot_handle == NULL);
2320 OSD_DECLARE_OP(oh, xattr_set);
2321 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2327 * Concurrency: @dt is write locked.
2329 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2330 const char *name, struct thandle *handle,
2331 struct lustre_capa *capa)
2333 struct osd_object *obj = osd_dt_obj(dt);
2334 struct inode *inode = obj->oo_inode;
2335 struct osd_thread_info *info = osd_oti_get(env);
2336 struct dentry *dentry = &info->oti_obj_dentry;
2339 LASSERT(dt_object_exists(dt));
2340 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2341 LASSERT(osd_write_locked(env, obj));
2342 LASSERT(handle != NULL);
2344 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2347 OSD_EXEC_OP(handle, xattr_set);
2349 dentry->d_inode = inode;
2350 rc = inode->i_op->removexattr(dentry, name);
2354 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2355 struct dt_object *dt,
2356 struct lustre_capa *old,
2359 struct osd_thread_info *info = osd_oti_get(env);
2360 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2361 struct osd_object *obj = osd_dt_obj(dt);
2362 struct osd_device *dev = osd_obj2dev(obj);
2363 struct lustre_capa_key *key = &info->oti_capa_key;
2364 struct lustre_capa *capa = &info->oti_capa;
2365 struct obd_capa *oc;
2366 struct md_capainfo *ci;
2370 if (!dev->od_fl_capa)
2371 RETURN(ERR_PTR(-ENOENT));
2373 LASSERT(dt_object_exists(dt));
2374 LINVRNT(osd_invariant(obj));
2376 /* renewal sanity check */
2377 if (old && osd_object_auth(env, dt, old, opc))
2378 RETURN(ERR_PTR(-EACCES));
2380 ci = md_capainfo(env);
2382 RETURN(ERR_PTR(-ENOENT));
2384 switch (ci->mc_auth) {
2388 capa->lc_uid = obj->oo_inode->i_uid;
2389 capa->lc_gid = obj->oo_inode->i_gid;
2390 capa->lc_flags = LC_ID_PLAIN;
2392 case LC_ID_CONVERT: {
2395 s[0] = obj->oo_inode->i_uid;
2396 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2397 s[2] = obj->oo_inode->i_gid;
2398 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2399 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2401 RETURN(ERR_PTR(rc));
2403 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2404 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2405 capa->lc_flags = LC_ID_CONVERT;
2409 RETURN(ERR_PTR(-EINVAL));
2412 capa->lc_fid = *fid;
2414 capa->lc_flags |= dev->od_capa_alg << 24;
2415 capa->lc_timeout = dev->od_capa_timeout;
2416 capa->lc_expiry = 0;
2418 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2420 LASSERT(!capa_is_expired(oc));
2424 cfs_spin_lock(&capa_lock);
2425 *key = dev->od_capa_keys[1];
2426 cfs_spin_unlock(&capa_lock);
2428 capa->lc_keyid = key->lk_keyid;
2429 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2431 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2433 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2434 RETURN(ERR_PTR(rc));
2437 oc = capa_add(dev->od_capa_hash, capa);
2441 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2443 struct osd_object *obj = osd_dt_obj(dt);
2444 struct inode *inode = obj->oo_inode;
2445 struct osd_thread_info *info = osd_oti_get(env);
2446 struct dentry *dentry = &info->oti_obj_dentry;
2447 struct file *file = &info->oti_file;
2452 dentry->d_inode = inode;
2453 file->f_dentry = dentry;
2454 file->f_mapping = inode->i_mapping;
2455 file->f_op = inode->i_fop;
2456 LOCK_INODE_MUTEX(inode);
2457 rc = file->f_op->fsync(file, dentry, 0);
2458 UNLOCK_INODE_MUTEX(inode);
2462 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2465 struct osd_object *obj = osd_dt_obj(dt);
2468 *data = (void *)obj->oo_inode;
2476 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2477 const struct dt_index_features *feat)
2479 struct iam_descr *descr;
2481 if (osd_object_is_root(o))
2482 return feat == &dt_directory_features;
2484 LASSERT(o->oo_dir != NULL);
2486 descr = o->oo_dir->od_container.ic_descr;
2487 if (feat == &dt_directory_features) {
2488 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2494 feat->dif_keysize_min <= descr->id_key_size &&
2495 descr->id_key_size <= feat->dif_keysize_max &&
2496 feat->dif_recsize_min <= descr->id_rec_size &&
2497 descr->id_rec_size <= feat->dif_recsize_max &&
2498 !(feat->dif_flags & (DT_IND_VARKEY |
2499 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2500 ergo(feat->dif_flags & DT_IND_UPDATE,
2501 1 /* XXX check that object (and file system) is
2506 static int osd_iam_container_init(const struct lu_env *env,
2507 struct osd_object *obj,
2508 struct osd_directory *dir)
2510 struct iam_container *bag = &dir->od_container;
2513 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2517 result = iam_container_setup(bag);
2521 if (osd_obj2dev(obj)->od_iop_mode) {
2522 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2524 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2530 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2532 iam_container_fini(bag);
2539 * Concurrency: no external locking is necessary.
2541 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2542 const struct dt_index_features *feat)
2546 struct osd_object *obj = osd_dt_obj(dt);
2547 struct osd_device *osd = osd_obj2dev(obj);
2549 LINVRNT(osd_invariant(obj));
2550 LASSERT(dt_object_exists(dt));
2552 if (osd_object_is_root(obj)) {
2553 dt->do_index_ops = &osd_index_ea_ops;
2555 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2556 dt->do_index_ops = &osd_index_ea_ops;
2557 if (S_ISDIR(obj->oo_inode->i_mode))
2562 } else if (!osd_has_index(obj)) {
2563 struct osd_directory *dir;
2568 cfs_spin_lock(&obj->oo_guard);
2569 if (obj->oo_dir == NULL)
2573 * Concurrent thread allocated container data.
2576 cfs_spin_unlock(&obj->oo_guard);
2578 * Now, that we have container data, serialize its
2581 cfs_down_write(&obj->oo_ext_idx_sem);
2583 * recheck under lock.
2585 if (!osd_has_index(obj))
2586 result = osd_iam_container_init(env, obj, dir);
2589 cfs_up_write(&obj->oo_ext_idx_sem);
2597 if (result == 0 && ea_dir == 0) {
2598 if (!osd_iam_index_probe(env, obj, feat))
2601 LINVRNT(osd_invariant(obj));
2606 static const struct dt_object_operations osd_obj_ops = {
2607 .do_read_lock = osd_object_read_lock,
2608 .do_write_lock = osd_object_write_lock,
2609 .do_read_unlock = osd_object_read_unlock,
2610 .do_write_unlock = osd_object_write_unlock,
2611 .do_write_locked = osd_object_write_locked,
2612 .do_attr_get = osd_attr_get,
2613 .do_declare_attr_set = osd_declare_attr_set,
2614 .do_attr_set = osd_attr_set,
2615 .do_ah_init = osd_ah_init,
2616 .do_declare_create = osd_declare_object_create,
2617 .do_create = osd_object_create,
2618 .do_declare_destroy = osd_declare_object_destroy,
2619 .do_destroy = osd_object_destroy,
2620 .do_index_try = osd_index_try,
2621 .do_declare_ref_add = osd_declare_object_ref_add,
2622 .do_ref_add = osd_object_ref_add,
2623 .do_declare_ref_del = osd_declare_object_ref_del,
2624 .do_ref_del = osd_object_ref_del,
2625 .do_xattr_get = osd_xattr_get,
2626 .do_declare_xattr_set = osd_declare_xattr_set,
2627 .do_xattr_set = osd_xattr_set,
2628 .do_declare_xattr_del = osd_declare_xattr_del,
2629 .do_xattr_del = osd_xattr_del,
2630 .do_xattr_list = osd_xattr_list,
2631 .do_capa_get = osd_capa_get,
2632 .do_object_sync = osd_object_sync,
2633 .do_data_get = osd_data_get,
2637 * dt_object_operations for interoperability mode
2638 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2640 static const struct dt_object_operations osd_obj_ea_ops = {
2641 .do_read_lock = osd_object_read_lock,
2642 .do_write_lock = osd_object_write_lock,
2643 .do_read_unlock = osd_object_read_unlock,
2644 .do_write_unlock = osd_object_write_unlock,
2645 .do_write_locked = osd_object_write_locked,
2646 .do_attr_get = osd_attr_get,
2647 .do_declare_attr_set = osd_declare_attr_set,
2648 .do_attr_set = osd_attr_set,
2649 .do_ah_init = osd_ah_init,
2650 .do_declare_create = osd_declare_object_create,
2651 .do_create = osd_object_ea_create,
2652 .do_declare_destroy = osd_declare_object_destroy,
2653 .do_destroy = osd_object_destroy,
2654 .do_index_try = osd_index_try,
2655 .do_declare_ref_add = osd_declare_object_ref_add,
2656 .do_ref_add = osd_object_ref_add,
2657 .do_declare_ref_del = osd_declare_object_ref_del,
2658 .do_ref_del = osd_object_ref_del,
2659 .do_xattr_get = osd_xattr_get,
2660 .do_declare_xattr_set = osd_declare_xattr_set,
2661 .do_xattr_set = osd_xattr_set,
2662 .do_declare_xattr_del = osd_declare_xattr_del,
2663 .do_xattr_del = osd_xattr_del,
2664 .do_xattr_list = osd_xattr_list,
2665 .do_capa_get = osd_capa_get,
2666 .do_object_sync = osd_object_sync,
2667 .do_data_get = osd_data_get,
2675 * XXX: Another layering violation for now.
2677 * We don't want to use ->f_op->read methods, because generic file write
2679 * - serializes on ->i_sem, and
2681 * - does a lot of extra work like balance_dirty_pages(),
2683 * which doesn't work for globally shared files like /last-received.
2685 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2687 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2689 memcpy(buffer, (char*)ei->i_data, buflen);
2694 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2697 struct buffer_head *bh;
2698 unsigned long block;
2705 /* prevent reading after eof */
2706 spin_lock(&inode->i_lock);
2707 if (i_size_read(inode) < *offs + size) {
2708 size = i_size_read(inode) - *offs;
2709 spin_unlock(&inode->i_lock);
2711 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2712 i_size_read(inode), *offs);
2714 } else if (size == 0) {
2718 spin_unlock(&inode->i_lock);
2721 blocksize = 1 << inode->i_blkbits;
2724 block = *offs >> inode->i_blkbits;
2725 boffs = *offs & (blocksize - 1);
2726 csize = min(blocksize - boffs, size);
2727 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2729 CERROR("can't read block: %d\n", err);
2733 memcpy(buf, bh->b_data + boffs, csize);
2743 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2744 struct lu_buf *buf, loff_t *pos,
2745 struct lustre_capa *capa)
2747 struct osd_object *obj = osd_dt_obj(dt);
2748 struct inode *inode = obj->oo_inode;
2751 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2754 /* Read small symlink from inode body as we need to maintain correct
2755 * on-disk symlinks for ldiskfs.
2757 if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2758 (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2759 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2761 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2766 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2769 memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2771 LDISKFS_I(inode)->i_disksize = buflen;
2772 i_size_write(inode, buflen);
2773 inode->i_sb->s_op->dirty_inode(inode);
2778 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2779 loff_t *offs, handle_t *handle)
2781 struct buffer_head *bh = NULL;
2782 loff_t offset = *offs;
2783 loff_t new_size = i_size_read(inode);
2784 unsigned long block;
2785 int blocksize = 1 << inode->i_blkbits;
2789 int dirty_inode = 0;
2791 while (bufsize > 0) {
2795 block = offset >> inode->i_blkbits;
2796 boffs = offset & (blocksize - 1);
2797 size = min(blocksize - boffs, bufsize);
2798 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2800 CERROR("can't read/create block: %d\n", err);
2804 err = ldiskfs_journal_get_write_access(handle, bh);
2806 CERROR("journal_get_write_access() returned error %d\n",
2810 LASSERTF(boffs + size <= bh->b_size,
2811 "boffs %d size %d bh->b_size %lu",
2812 boffs, size, (unsigned long)bh->b_size);
2813 memcpy(bh->b_data + boffs, buf, size);
2814 err = ldiskfs_journal_dirty_metadata(handle, bh);
2818 if (offset + size > new_size)
2819 new_size = offset + size;
2827 /* correct in-core and on-disk sizes */
2828 if (new_size > i_size_read(inode)) {
2829 spin_lock(&inode->i_lock);
2830 if (new_size > i_size_read(inode))
2831 i_size_write(inode, new_size);
2832 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
2833 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
2836 spin_unlock(&inode->i_lock);
2838 inode->i_sb->s_op->dirty_inode(inode);
2846 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
2847 const loff_t size, loff_t pos,
2848 struct thandle *handle)
2850 struct osd_thandle *oh;
2853 LASSERT(handle != NULL);
2855 oh = container_of0(handle, struct osd_thandle, ot_super);
2856 LASSERT(oh->ot_handle == NULL);
2858 /* XXX: size == 0 or INT_MAX indicating a catalog header update or
2859 * llog write, see comment in mdd_declare_llog_record().
2861 * This hack should be removed in 2.3
2863 if (size == DECLARE_LLOG_REWRITE)
2865 else if (size == DECLARE_LLOG_WRITE)
2868 credits = osd_dto_credits_noquota[DTO_WRITE_BLOCK];
2870 OSD_DECLARE_OP(oh, write);
2871 oh->ot_credits += credits;
2873 if (osd_dt_obj(dt)->oo_inode == NULL)
2876 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2877 osd_dt_obj(dt)->oo_inode);
2878 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2879 osd_dt_obj(dt)->oo_inode);
2883 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2884 const struct lu_buf *buf, loff_t *pos,
2885 struct thandle *handle, struct lustre_capa *capa,
2888 struct osd_object *obj = osd_dt_obj(dt);
2889 struct inode *inode = obj->oo_inode;
2890 struct osd_thandle *oh;
2892 #ifdef HAVE_QUOTA_SUPPORT
2893 cfs_cap_t save = cfs_curproc_cap_pack();
2896 LASSERT(handle != NULL);
2898 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2901 oh = container_of(handle, struct osd_thandle, ot_super);
2902 LASSERT(oh->ot_handle->h_transaction != NULL);
2903 #ifdef HAVE_QUOTA_SUPPORT
2905 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2907 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2909 /* Write small symlink to inode body as we need to maintain correct
2910 * on-disk symlinks for ldiskfs.
2912 if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2913 (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
2914 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2916 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
2919 #ifdef HAVE_QUOTA_SUPPORT
2920 cfs_curproc_cap_unpack(save);
2923 result = buf->lb_len;
2928 * in some cases we may need declare methods for objects being created
2929 * e.g., when we create symlink
2931 static const struct dt_body_operations osd_body_ops_new = {
2932 .dbo_declare_write = osd_declare_write,
2935 const struct dt_body_operations osd_body_ops = {
2936 .dbo_read = osd_read,
2937 .dbo_declare_write = osd_declare_write,
2938 .dbo_write = osd_write
2941 static int osd_index_declare_iam_delete(const struct lu_env *env,
2942 struct dt_object *dt,
2943 const struct dt_key *key,
2944 struct thandle *handle)
2946 struct osd_thandle *oh;
2948 oh = container_of0(handle, struct osd_thandle, ot_super);
2949 LASSERT(oh->ot_handle == NULL);
2951 OSD_DECLARE_OP(oh, delete);
2952 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2958 * delete a (key, value) pair from index \a dt specified by \a key
2960 * \param dt osd index object
2961 * \param key key for index
2962 * \param rec record reference
2963 * \param handle transaction handler
2966 * \retval -ve failure
2969 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2970 const struct dt_key *key,
2971 struct thandle *handle,
2972 struct lustre_capa *capa)
2974 struct osd_object *obj = osd_dt_obj(dt);
2975 struct osd_thandle *oh;
2976 struct iam_path_descr *ipd;
2977 struct iam_container *bag = &obj->oo_dir->od_container;
2982 LINVRNT(osd_invariant(obj));
2983 LASSERT(dt_object_exists(dt));
2984 LASSERT(bag->ic_object == obj->oo_inode);
2985 LASSERT(handle != NULL);
2987 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2990 OSD_EXEC_OP(handle, delete);
2992 ipd = osd_idx_ipd_get(env, bag);
2993 if (unlikely(ipd == NULL))
2996 oh = container_of0(handle, struct osd_thandle, ot_super);
2997 LASSERT(oh->ot_handle != NULL);
2998 LASSERT(oh->ot_handle->h_transaction != NULL);
3000 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3001 osd_ipd_put(env, bag, ipd);
3002 LINVRNT(osd_invariant(obj));
3006 static int osd_index_declare_ea_delete(const struct lu_env *env,
3007 struct dt_object *dt,
3008 const struct dt_key *key,
3009 struct thandle *handle)
3011 struct osd_thandle *oh;
3013 LASSERT(dt_object_exists(dt));
3014 LASSERT(handle != NULL);
3016 oh = container_of0(handle, struct osd_thandle, ot_super);
3017 LASSERT(oh->ot_handle == NULL);
3019 OSD_DECLARE_OP(oh, delete);
3020 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3022 LASSERT(osd_dt_obj(dt)->oo_inode);
3023 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3024 osd_dt_obj(dt)->oo_inode);
3025 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3026 osd_dt_obj(dt)->oo_inode);
3031 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3034 struct osd_fid_pack *rec;
3037 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3038 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3039 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3045 * Index delete function for interoperability mode (b11826).
3046 * It will remove the directory entry added by osd_index_ea_insert().
3047 * This entry is needed to maintain name->fid mapping.
3049 * \param key, key i.e. file entry to be deleted
3051 * \retval 0, on success
3052 * \retval -ve, on error
3054 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3055 const struct dt_key *key,
3056 struct thandle *handle,
3057 struct lustre_capa *capa)
3059 struct osd_object *obj = osd_dt_obj(dt);
3060 struct inode *dir = obj->oo_inode;
3061 struct dentry *dentry;
3062 struct osd_thandle *oh;
3063 struct ldiskfs_dir_entry_2 *de;
3064 struct buffer_head *bh;
3065 struct htree_lock *hlock = NULL;
3070 LINVRNT(osd_invariant(obj));
3071 LASSERT(dt_object_exists(dt));
3072 LASSERT(handle != NULL);
3074 OSD_EXEC_OP(handle, delete);
3076 oh = container_of(handle, struct osd_thandle, ot_super);
3077 LASSERT(oh->ot_handle != NULL);
3078 LASSERT(oh->ot_handle->h_transaction != NULL);
3080 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3083 dentry = osd_child_dentry_get(env, obj,
3084 (char *)key, strlen((char *)key));
3086 if (obj->oo_hl_head != NULL) {
3087 hlock = osd_oti_get(env)->oti_hlock;
3088 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3089 dir, LDISKFS_HLOCK_DEL);
3091 cfs_down_write(&obj->oo_ext_idx_sem);
3094 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3096 rc = ldiskfs_delete_entry(oh->ot_handle,
3103 ldiskfs_htree_unlock(hlock);
3105 cfs_up_write(&obj->oo_ext_idx_sem);
3107 LASSERT(osd_invariant(obj));
3112 * Lookup index for \a key and copy record to \a rec.
3114 * \param dt osd index object
3115 * \param key key for index
3116 * \param rec record reference
3118 * \retval +ve success : exact mach
3119 * \retval 0 return record with key not greater than \a key
3120 * \retval -ve failure
3122 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3123 struct dt_rec *rec, const struct dt_key *key,
3124 struct lustre_capa *capa)
3126 struct osd_object *obj = osd_dt_obj(dt);
3127 struct iam_path_descr *ipd;
3128 struct iam_container *bag = &obj->oo_dir->od_container;
3129 struct osd_thread_info *oti = osd_oti_get(env);
3130 struct iam_iterator *it = &oti->oti_idx_it;
3131 struct iam_rec *iam_rec;
3136 LASSERT(osd_invariant(obj));
3137 LASSERT(dt_object_exists(dt));
3138 LASSERT(bag->ic_object == obj->oo_inode);
3140 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3143 ipd = osd_idx_ipd_get(env, bag);
3147 /* got ipd now we can start iterator. */
3148 iam_it_init(it, bag, 0, ipd);
3150 rc = iam_it_get(it, (struct iam_key *)key);
3152 if (S_ISDIR(obj->oo_inode->i_mode))
3153 iam_rec = (struct iam_rec *)oti->oti_ldp;
3155 iam_rec = (struct iam_rec *) rec;
3157 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3158 if (S_ISDIR(obj->oo_inode->i_mode))
3159 osd_fid_unpack((struct lu_fid *) rec,
3160 (struct osd_fid_pack *)iam_rec);
3164 osd_ipd_put(env, bag, ipd);
3166 LINVRNT(osd_invariant(obj));
3171 static int osd_index_declare_iam_insert(const struct lu_env *env,
3172 struct dt_object *dt,
3173 const struct dt_rec *rec,
3174 const struct dt_key *key,
3175 struct thandle *handle)
3177 struct osd_thandle *oh;
3179 LASSERT(dt_object_exists(dt));
3180 LASSERT(handle != NULL);
3182 oh = container_of0(handle, struct osd_thandle, ot_super);
3183 LASSERT(oh->ot_handle == NULL);
3185 OSD_DECLARE_OP(oh, insert);
3186 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3192 * Inserts (key, value) pair in \a dt index object.
3194 * \param dt osd index object
3195 * \param key key for index
3196 * \param rec record reference
3197 * \param th transaction handler
3200 * \retval -ve failure
3202 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3203 const struct dt_rec *rec,
3204 const struct dt_key *key, struct thandle *th,
3205 struct lustre_capa *capa, int ignore_quota)
3207 struct osd_object *obj = osd_dt_obj(dt);
3208 struct iam_path_descr *ipd;
3209 struct osd_thandle *oh;
3210 struct iam_container *bag = &obj->oo_dir->od_container;
3211 #ifdef HAVE_QUOTA_SUPPORT
3212 cfs_cap_t save = cfs_curproc_cap_pack();
3214 struct osd_thread_info *oti = osd_oti_get(env);
3215 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3220 LINVRNT(osd_invariant(obj));
3221 LASSERT(dt_object_exists(dt));
3222 LASSERT(bag->ic_object == obj->oo_inode);
3223 LASSERT(th != NULL);
3225 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3228 OSD_EXEC_OP(th, insert);
3230 ipd = osd_idx_ipd_get(env, bag);
3231 if (unlikely(ipd == NULL))
3234 oh = container_of0(th, struct osd_thandle, ot_super);
3235 LASSERT(oh->ot_handle != NULL);
3236 LASSERT(oh->ot_handle->h_transaction != NULL);
3237 #ifdef HAVE_QUOTA_SUPPORT
3239 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3241 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3243 if (S_ISDIR(obj->oo_inode->i_mode))
3244 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3246 iam_rec = (struct iam_rec *) rec;
3247 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3249 #ifdef HAVE_QUOTA_SUPPORT
3250 cfs_curproc_cap_unpack(save);
3252 osd_ipd_put(env, bag, ipd);
3253 LINVRNT(osd_invariant(obj));
3258 * Calls ldiskfs_add_entry() to add directory entry
3259 * into the directory. This is required for
3260 * interoperability mode (b11826)
3262 * \retval 0, on success
3263 * \retval -ve, on error
3265 static int __osd_ea_add_rec(struct osd_thread_info *info,
3266 struct osd_object *pobj, struct inode *cinode,
3267 const char *name, const struct dt_rec *fid,
3268 struct htree_lock *hlock, struct thandle *th)
3270 struct ldiskfs_dentry_param *ldp;
3271 struct dentry *child;
3272 struct osd_thandle *oth;
3275 oth = container_of(th, struct osd_thandle, ot_super);
3276 LASSERT(oth->ot_handle != NULL);
3277 LASSERT(oth->ot_handle->h_transaction != NULL);
3279 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3281 if (fid_is_igif((struct lu_fid *)fid) ||
3282 fid_is_norm((struct lu_fid *)fid)) {
3283 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3284 osd_get_ldiskfs_dirent_param(ldp, fid);
3285 child->d_fsdata = (void*) ldp;
3287 child->d_fsdata = NULL;
3288 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3294 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3295 * into the directory.Also sets flags into osd object to
3296 * indicate dot and dotdot are created. This is required for
3297 * interoperability mode (b11826)
3299 * \param dir directory for dot and dotdot fixup.
3300 * \param obj child object for linking
3302 * \retval 0, on success
3303 * \retval -ve, on error
3305 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3306 struct osd_object *dir,
3307 struct inode *parent_dir, const char *name,
3308 const struct dt_rec *dot_fid,
3309 const struct dt_rec *dot_dot_fid,
3312 struct inode *inode = dir->oo_inode;
3313 struct ldiskfs_dentry_param *dot_ldp;
3314 struct ldiskfs_dentry_param *dot_dot_ldp;
3315 struct osd_thandle *oth;
3318 oth = container_of(th, struct osd_thandle, ot_super);
3319 LASSERT(oth->ot_handle->h_transaction != NULL);
3320 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3322 if (strcmp(name, dot) == 0) {
3323 if (dir->oo_compat_dot_created) {
3326 LASSERT(inode == parent_dir);
3327 dir->oo_compat_dot_created = 1;
3330 } else if(strcmp(name, dotdot) == 0) {
3331 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3332 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3334 if (!dir->oo_compat_dot_created)
3336 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
3337 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3338 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3343 /* in case of rename, dotdot is already created */
3344 if (dir->oo_compat_dotdot_created) {
3345 return __osd_ea_add_rec(info, dir, parent_dir, name,
3346 dot_dot_fid, NULL, th);
3349 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3350 inode, dot_ldp, dot_dot_ldp);
3352 dir->oo_compat_dotdot_created = 1;
3360 * It will call the appropriate osd_add* function and return the
3361 * value, return by respective functions.
3363 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3364 struct inode *cinode, const char *name,
3365 const struct dt_rec *fid, struct thandle *th)
3367 struct osd_thread_info *info = osd_oti_get(env);
3368 struct htree_lock *hlock;
3371 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3373 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3375 if (hlock != NULL) {
3376 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3379 cfs_down_write(&pobj->oo_ext_idx_sem);
3381 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3382 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3385 if (hlock != NULL) {
3386 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3387 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3389 cfs_down_write(&pobj->oo_ext_idx_sem);
3392 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3396 ldiskfs_htree_unlock(hlock);
3398 cfs_up_write(&pobj->oo_ext_idx_sem);
3404 * Calls ->lookup() to find dentry. From dentry get inode and
3405 * read inode's ea to get fid. This is required for interoperability
3408 * \retval 0, on success
3409 * \retval -ve, on error
3411 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3412 struct dt_rec *rec, const struct dt_key *key)
3414 struct inode *dir = obj->oo_inode;
3415 struct dentry *dentry;
3416 struct ldiskfs_dir_entry_2 *de;
3417 struct buffer_head *bh;
3418 struct lu_fid *fid = (struct lu_fid *) rec;
3419 struct htree_lock *hlock = NULL;
3423 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3425 dentry = osd_child_dentry_get(env, obj,
3426 (char *)key, strlen((char *)key));
3428 if (obj->oo_hl_head != NULL) {
3429 hlock = osd_oti_get(env)->oti_hlock;
3430 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3431 dir, LDISKFS_HLOCK_LOOKUP);
3433 cfs_down_read(&obj->oo_ext_idx_sem);
3436 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3438 ino = le32_to_cpu(de->inode);
3439 rc = osd_get_fid_from_dentry(de, rec);
3441 /* done with de, release bh */
3444 rc = osd_ea_fid_get(env, obj, ino, fid);
3450 ldiskfs_htree_unlock(hlock);
3452 cfs_up_read(&obj->oo_ext_idx_sem);
3457 * Find the osd object for given fid.
3459 * \param fid need to find the osd object having this fid
3461 * \retval osd_object on success
3462 * \retval -ve on error
3464 struct osd_object *osd_object_find(const struct lu_env *env,
3465 struct dt_object *dt,
3466 const struct lu_fid *fid)
3468 struct lu_device *ludev = dt->do_lu.lo_dev;
3469 struct osd_object *child = NULL;
3470 struct lu_object *luch;
3471 struct lu_object *lo;
3473 luch = lu_object_find(env, ludev, fid, NULL);
3474 if (!IS_ERR(luch)) {
3475 if (lu_object_exists(luch)) {
3476 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3478 child = osd_obj(lo);
3480 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3481 "lu_object can't be located"
3482 ""DFID"\n", PFID(fid));
3484 if (child == NULL) {
3485 lu_object_put(env, luch);
3486 CERROR("Unable to get osd_object\n");
3487 child = ERR_PTR(-ENOENT);
3490 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3491 "lu_object does not exists "DFID"\n",
3493 child = ERR_PTR(-ENOENT);
3496 child = (void *)luch;
3502 * Put the osd object once done with it.
3504 * \param obj osd object that needs to be put
3506 static inline void osd_object_put(const struct lu_env *env,
3507 struct osd_object *obj)
3509 lu_object_put(env, &obj->oo_dt.do_lu);
3512 static int osd_index_declare_ea_insert(const struct lu_env *env,
3513 struct dt_object *dt,
3514 const struct dt_rec *rec,
3515 const struct dt_key *key,
3516 struct thandle *handle)
3518 struct osd_thandle *oh;
3520 LASSERT(dt_object_exists(dt));
3521 LASSERT(handle != NULL);
3523 oh = container_of0(handle, struct osd_thandle, ot_super);
3524 LASSERT(oh->ot_handle == NULL);
3526 OSD_DECLARE_OP(oh, insert);
3527 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3529 LASSERT(osd_dt_obj(dt)->oo_inode);
3530 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3531 osd_dt_obj(dt)->oo_inode);
3532 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3533 osd_dt_obj(dt)->oo_inode);
3539 * Index add function for interoperability mode (b11826).
3540 * It will add the directory entry.This entry is needed to
3541 * maintain name->fid mapping.
3543 * \param key it is key i.e. file entry to be inserted
3544 * \param rec it is value of given key i.e. fid
3546 * \retval 0, on success
3547 * \retval -ve, on error
3549 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3550 const struct dt_rec *rec,
3551 const struct dt_key *key, struct thandle *th,
3552 struct lustre_capa *capa, int ignore_quota)
3554 struct osd_object *obj = osd_dt_obj(dt);
3555 struct lu_fid *fid = (struct lu_fid *) rec;
3556 const char *name = (const char *)key;
3557 struct osd_object *child;
3558 #ifdef HAVE_QUOTA_SUPPORT
3559 cfs_cap_t save = cfs_curproc_cap_pack();
3565 LASSERT(osd_invariant(obj));
3566 LASSERT(dt_object_exists(dt));
3567 LASSERT(th != NULL);
3569 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3572 child = osd_object_find(env, dt, fid);
3573 if (!IS_ERR(child)) {
3574 #ifdef HAVE_QUOTA_SUPPORT
3576 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3578 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3580 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3581 #ifdef HAVE_QUOTA_SUPPORT
3582 cfs_curproc_cap_unpack(save);
3584 osd_object_put(env, child);
3586 rc = PTR_ERR(child);
3589 LASSERT(osd_invariant(obj));
3594 * Initialize osd Iterator for given osd index object.
3596 * \param dt osd index object
3599 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3600 struct dt_object *dt,
3602 struct lustre_capa *capa)
3604 struct osd_it_iam *it;
3605 struct osd_thread_info *oti = osd_oti_get(env);
3606 struct osd_object *obj = osd_dt_obj(dt);
3607 struct lu_object *lo = &dt->do_lu;
3608 struct iam_path_descr *ipd;
3609 struct iam_container *bag = &obj->oo_dir->od_container;
3611 LASSERT(lu_object_exists(lo));
3613 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3614 return ERR_PTR(-EACCES);
3617 ipd = osd_it_ipd_get(env, bag);
3618 if (likely(ipd != NULL)) {
3622 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3623 return (struct dt_it *)it;
3625 return ERR_PTR(-ENOMEM);
3629 * free given Iterator.
3632 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3634 struct osd_it_iam *it = (struct osd_it_iam *)di;
3635 struct osd_object *obj = it->oi_obj;
3637 iam_it_fini(&it->oi_it);
3638 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3639 lu_object_put(env, &obj->oo_dt.do_lu);
3643 * Move Iterator to record specified by \a key
3645 * \param di osd iterator
3646 * \param key key for index
3648 * \retval +ve di points to record with least key not larger than key
3649 * \retval 0 di points to exact matched key
3650 * \retval -ve failure
3653 static int osd_it_iam_get(const struct lu_env *env,
3654 struct dt_it *di, const struct dt_key *key)
3656 struct osd_it_iam *it = (struct osd_it_iam *)di;
3658 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3664 * \param di osd iterator
3667 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3669 struct osd_it_iam *it = (struct osd_it_iam *)di;
3671 iam_it_put(&it->oi_it);
3675 * Move iterator by one record
3677 * \param di osd iterator
3679 * \retval +1 end of container reached
3681 * \retval -ve failure
3684 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3686 struct osd_it_iam *it = (struct osd_it_iam *)di;
3688 return iam_it_next(&it->oi_it);
3692 * Return pointer to the key under iterator.
3695 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3696 const struct dt_it *di)
3698 struct osd_it_iam *it = (struct osd_it_iam *)di;
3700 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3704 * Return size of key under iterator (in bytes)
3707 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3709 struct osd_it_iam *it = (struct osd_it_iam *)di;
3711 return iam_it_key_size(&it->oi_it);
3714 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
3715 int len, __u16 type)
3717 struct luda_type *lt;
3718 const unsigned align = sizeof(struct luda_type) - 1;
3720 /* check if file type is required */
3721 if (attr & LUDA_TYPE) {
3722 len = (len + align) & ~align;
3724 lt = (void *) ent->lde_name + len;
3725 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3726 ent->lde_attrs |= LUDA_TYPE;
3729 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3733 * build lu direct from backend fs dirent.
3736 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3737 struct lu_fid *fid, __u64 offset,
3738 char *name, __u16 namelen,
3739 __u16 type, __u32 attr)
3741 fid_cpu_to_le(&ent->lde_fid, fid);
3742 ent->lde_attrs = LUDA_FID;
3744 ent->lde_hash = cpu_to_le64(offset);
3745 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3747 strncpy(ent->lde_name, name, namelen);
3748 ent->lde_namelen = cpu_to_le16(namelen);
3750 /* append lustre attributes */
3751 osd_it_append_attrs(ent, attr, namelen, type);
3755 * Return pointer to the record under iterator.
3757 static int osd_it_iam_rec(const struct lu_env *env,
3758 const struct dt_it *di,
3759 struct dt_rec *dtrec, __u32 attr)
3761 struct osd_it_iam *it = (struct osd_it_iam *)di;
3762 struct osd_thread_info *info = osd_oti_get(env);
3763 struct lu_fid *fid = &info->oti_fid;
3764 const struct osd_fid_pack *rec;
3765 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3771 name = (char *)iam_it_key_get(&it->oi_it);
3773 RETURN(PTR_ERR(name));
3775 namelen = iam_it_key_size(&it->oi_it);
3777 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3779 RETURN(PTR_ERR(rec));
3781 rc = osd_fid_unpack(fid, rec);
3785 hash = iam_it_store(&it->oi_it);
3787 /* IAM does not store object type in IAM index (dir) */
3788 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3795 * Returns cookie for current Iterator position.
3797 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3799 struct osd_it_iam *it = (struct osd_it_iam *)di;
3801 return iam_it_store(&it->oi_it);
3805 * Restore iterator from cookie.
3807 * \param di osd iterator
3808 * \param hash Iterator location cookie
3810 * \retval +ve di points to record with least key not larger than key.
3811 * \retval 0 di points to exact matched key
3812 * \retval -ve failure
3815 static int osd_it_iam_load(const struct lu_env *env,
3816 const struct dt_it *di, __u64 hash)
3818 struct osd_it_iam *it = (struct osd_it_iam *)di;
3820 return iam_it_load(&it->oi_it, hash);
3823 static const struct dt_index_operations osd_index_iam_ops = {
3824 .dio_lookup = osd_index_iam_lookup,
3825 .dio_declare_insert = osd_index_declare_iam_insert,
3826 .dio_insert = osd_index_iam_insert,
3827 .dio_declare_delete = osd_index_declare_iam_delete,
3828 .dio_delete = osd_index_iam_delete,
3830 .init = osd_it_iam_init,
3831 .fini = osd_it_iam_fini,
3832 .get = osd_it_iam_get,
3833 .put = osd_it_iam_put,
3834 .next = osd_it_iam_next,
3835 .key = osd_it_iam_key,
3836 .key_size = osd_it_iam_key_size,
3837 .rec = osd_it_iam_rec,
3838 .store = osd_it_iam_store,
3839 .load = osd_it_iam_load
3844 * Creates or initializes iterator context.
3846 * \retval struct osd_it_ea, iterator structure on success
3849 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3850 struct dt_object *dt,
3852 struct lustre_capa *capa)
3854 struct osd_object *obj = osd_dt_obj(dt);
3855 struct osd_thread_info *info = osd_oti_get(env);
3856 struct osd_it_ea *it = &info->oti_it_ea;
3857 struct lu_object *lo = &dt->do_lu;
3858 struct dentry *obj_dentry = &info->oti_it_dentry;
3860 LASSERT(lu_object_exists(lo));
3862 obj_dentry->d_inode = obj->oo_inode;
3863 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3864 obj_dentry->d_name.hash = 0;
3866 it->oie_rd_dirent = 0;
3867 it->oie_it_dirent = 0;
3868 it->oie_dirent = NULL;
3869 it->oie_buf = info->oti_it_ea_buf;
3871 it->oie_file.f_pos = 0;
3872 it->oie_file.f_dentry = obj_dentry;
3873 if (attr & LUDA_64BITHASH)
3874 it->oie_file.f_flags = O_64BITHASH;
3876 it->oie_file.f_flags = O_32BITHASH;
3877 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3878 it->oie_file.f_op = obj->oo_inode->i_fop;
3879 it->oie_file.private_data = NULL;
3881 RETURN((struct dt_it *) it);
3885 * Destroy or finishes iterator context.
3887 * \param di iterator structure to be destroyed
3889 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3891 struct osd_it_ea *it = (struct osd_it_ea *)di;
3892 struct osd_object *obj = it->oie_obj;
3893 struct inode *inode = obj->oo_inode;
3896 it->oie_file.f_op->release(inode, &it->oie_file);
3897 lu_object_put(env, &obj->oo_dt.do_lu);
3902 * It position the iterator at given key, so that next lookup continues from
3903 * that key Or it is similar to dio_it->load() but based on a key,
3904 * rather than file position.
3906 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3909 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3911 static int osd_it_ea_get(const struct lu_env *env,
3912 struct dt_it *di, const struct dt_key *key)
3914 struct osd_it_ea *it = (struct osd_it_ea *)di;
3917 LASSERT(((const char *)key)[0] == '\0');
3918 it->oie_file.f_pos = 0;
3919 it->oie_rd_dirent = 0;
3920 it->oie_it_dirent = 0;
3921 it->oie_dirent = NULL;
3929 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3934 * It is called internally by ->readdir(). It fills the
3935 * iterator's in-memory data structure with required
3936 * information i.e. name, namelen, rec_size etc.
3938 * \param buf in which information to be filled in.
3939 * \param name name of the file in given dir
3941 * \retval 0 on success
3942 * \retval 1 on buffer full
3944 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3945 loff_t offset, __u64 ino,
3948 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3949 struct osd_it_ea_dirent *ent = it->oie_dirent;
3950 struct lu_fid *fid = &ent->oied_fid;
3951 struct osd_fid_pack *rec;
3954 /* this should never happen */
3955 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3956 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3960 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3964 if (d_type & LDISKFS_DIRENT_LUFID) {
3965 rec = (struct osd_fid_pack*) (name + namelen + 1);
3967 if (osd_fid_unpack(fid, rec) != 0)
3970 d_type &= ~LDISKFS_DIRENT_LUFID;
3975 ent->oied_ino = ino;
3976 ent->oied_off = offset;
3977 ent->oied_namelen = namelen;
3978 ent->oied_type = d_type;
3980 memcpy(ent->oied_name, name, namelen);
3982 it->oie_rd_dirent++;
3983 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3988 * Calls ->readdir() to load a directory entry at a time
3989 * and stored it in iterator's in-memory data structure.
3991 * \param di iterator's in memory structure
3993 * \retval 0 on success
3994 * \retval -ve on error
3996 static int osd_ldiskfs_it_fill(const struct lu_env *env,
3997 const struct dt_it *di)
3999 struct osd_it_ea *it = (struct osd_it_ea *)di;
4000 struct osd_object *obj = it->oie_obj;
4001 struct inode *inode = obj->oo_inode;
4002 struct htree_lock *hlock = NULL;
4006 it->oie_dirent = it->oie_buf;
4007 it->oie_rd_dirent = 0;
4009 if (obj->oo_hl_head != NULL) {
4010 hlock = osd_oti_get(env)->oti_hlock;
4011 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4012 inode, LDISKFS_HLOCK_READDIR);
4014 cfs_down_read(&obj->oo_ext_idx_sem);
4017 result = inode->i_fop->readdir(&it->oie_file, it,
4018 (filldir_t) osd_ldiskfs_filldir);
4021 ldiskfs_htree_unlock(hlock);
4023 cfs_up_read(&obj->oo_ext_idx_sem);
4025 if (it->oie_rd_dirent == 0) {
4028 it->oie_dirent = it->oie_buf;
4029 it->oie_it_dirent = 1;
4036 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4037 * to load a directory entry at a time and stored it in
4038 * iterator's in-memory data structure.
4040 * \param di iterator's in memory structure
4042 * \retval +ve iterator reached to end
4043 * \retval 0 iterator not reached to end
4044 * \retval -ve on error
4046 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4048 struct osd_it_ea *it = (struct osd_it_ea *)di;
4053 if (it->oie_it_dirent < it->oie_rd_dirent) {
4055 (void *) it->oie_dirent +
4056 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4057 it->oie_dirent->oied_namelen);
4058 it->oie_it_dirent++;
4061 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
4064 rc = osd_ldiskfs_it_fill(env, di);
4071 * Returns the key at current position from iterator's in memory structure.
4073 * \param di iterator's in memory structure
4075 * \retval key i.e. struct dt_key on success
4077 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4078 const struct dt_it *di)
4080 struct osd_it_ea *it = (struct osd_it_ea *)di;
4082 return (struct dt_key *)it->oie_dirent->oied_name;
4086 * Returns the key's size at current position from iterator's in memory structure.
4088 * \param di iterator's in memory structure
4090 * \retval key_size i.e. struct dt_key on success
4092 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4094 struct osd_it_ea *it = (struct osd_it_ea *)di;
4096 return it->oie_dirent->oied_namelen;
4101 * Returns the value (i.e. fid/igif) at current position from iterator's
4102 * in memory structure.
4104 * \param di struct osd_it_ea, iterator's in memory structure
4105 * \param attr attr requested for dirent.
4106 * \param lde lustre dirent
4108 * \retval 0 no error and \param lde has correct lustre dirent.
4109 * \retval -ve on error
4111 static inline int osd_it_ea_rec(const struct lu_env *env,
4112 const struct dt_it *di,
4113 struct dt_rec *dtrec, __u32 attr)
4115 struct osd_it_ea *it = (struct osd_it_ea *)di;
4116 struct osd_object *obj = it->oie_obj;
4117 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4118 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4123 if (!fid_is_sane(fid))
4124 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
4127 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4128 it->oie_dirent->oied_name,
4129 it->oie_dirent->oied_namelen,
4130 it->oie_dirent->oied_type,
4136 * Returns a cookie for current position of the iterator head, so that
4137 * user can use this cookie to load/start the iterator next time.
4139 * \param di iterator's in memory structure
4141 * \retval cookie for current position, on success
4143 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4145 struct osd_it_ea *it = (struct osd_it_ea *)di;
4147 return it->oie_dirent->oied_off;
4151 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4152 * to load a directory entry at a time and stored it i inn,
4153 * in iterator's in-memory data structure.
4155 * \param di struct osd_it_ea, iterator's in memory structure
4157 * \retval +ve on success
4158 * \retval -ve on error
4160 static int osd_it_ea_load(const struct lu_env *env,
4161 const struct dt_it *di, __u64 hash)
4163 struct osd_it_ea *it = (struct osd_it_ea *)di;
4167 it->oie_file.f_pos = hash;
4169 rc = osd_ldiskfs_it_fill(env, di);
4177 * Index lookup function for interoperability mode (b11826).
4179 * \param key, key i.e. file name to be searched
4181 * \retval +ve, on success
4182 * \retval -ve, on error
4184 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4185 struct dt_rec *rec, const struct dt_key *key,
4186 struct lustre_capa *capa)
4188 struct osd_object *obj = osd_dt_obj(dt);
4193 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4194 LINVRNT(osd_invariant(obj));
4196 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4199 rc = osd_ea_lookup_rec(env, obj, rec, key);
4207 * Index and Iterator operations for interoperability
4208 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4210 static const struct dt_index_operations osd_index_ea_ops = {
4211 .dio_lookup = osd_index_ea_lookup,
4212 .dio_declare_insert = osd_index_declare_ea_insert,
4213 .dio_insert = osd_index_ea_insert,
4214 .dio_declare_delete = osd_index_declare_ea_delete,
4215 .dio_delete = osd_index_ea_delete,
4217 .init = osd_it_ea_init,
4218 .fini = osd_it_ea_fini,
4219 .get = osd_it_ea_get,
4220 .put = osd_it_ea_put,
4221 .next = osd_it_ea_next,
4222 .key = osd_it_ea_key,
4223 .key_size = osd_it_ea_key_size,
4224 .rec = osd_it_ea_rec,
4225 .store = osd_it_ea_store,
4226 .load = osd_it_ea_load
4230 static void *osd_key_init(const struct lu_context *ctx,
4231 struct lu_context_key *key)
4233 struct osd_thread_info *info;
4235 OBD_ALLOC_PTR(info);
4237 return ERR_PTR(-ENOMEM);
4239 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4240 if (info->oti_it_ea_buf == NULL)
4243 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4245 info->oti_hlock = ldiskfs_htree_lock_alloc();
4246 if (info->oti_hlock == NULL)
4252 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4255 return ERR_PTR(-ENOMEM);
4258 static void osd_key_fini(const struct lu_context *ctx,
4259 struct lu_context_key *key, void* data)
4261 struct osd_thread_info *info = data;
4263 if (info->oti_hlock != NULL)
4264 ldiskfs_htree_lock_free(info->oti_hlock);
4265 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4269 static void osd_key_exit(const struct lu_context *ctx,
4270 struct lu_context_key *key, void *data)
4272 struct osd_thread_info *info = data;
4274 LASSERT(info->oti_r_locks == 0);
4275 LASSERT(info->oti_w_locks == 0);
4276 LASSERT(info->oti_txns == 0);
4279 /* type constructor/destructor: osd_type_init, osd_type_fini */
4280 LU_TYPE_INIT_FINI(osd, &osd_key);
4282 struct lu_context_key osd_key = {
4283 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
4284 .lct_init = osd_key_init,
4285 .lct_fini = osd_key_fini,
4286 .lct_exit = osd_key_exit
4290 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4291 const char *name, struct lu_device *next)
4293 return osd_procfs_init(osd_dev(d), name);
4296 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4298 struct osd_thread_info *info = osd_oti_get(env);
4300 if (o->od_obj_area != NULL) {
4301 lu_object_put(env, &o->od_obj_area->do_lu);
4302 o->od_obj_area = NULL;
4304 if (o->od_oi_table != NULL)
4305 osd_oi_fini(info, &o->od_oi_table, o->od_oi_count);
4310 static int osd_mount(const struct lu_env *env,
4311 struct osd_device *o, struct lustre_cfg *cfg)
4313 struct lustre_mount_info *lmi;
4314 const char *dev = lustre_cfg_string(cfg, 0);
4315 struct lustre_disk_data *ldd;
4316 struct lustre_sb_info *lsi;
4319 if (o->od_mount != NULL) {
4320 CERROR("Already mounted (%s)\n", dev);
4325 lmi = server_get_mount(dev);
4327 CERROR("Cannot get mount info for %s!\n", dev);
4331 LASSERT(lmi != NULL);
4332 /* save lustre_mount_info in dt_device */
4335 lsi = s2lsi(lmi->lmi_sb);
4338 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4340 LCONSOLE_WARN("OSD: IAM mode enabled\n");
4344 o->od_obj_area = NULL;
4348 static struct lu_device *osd_device_fini(const struct lu_env *env,
4349 struct lu_device *d)
4354 shrink_dcache_sb(osd_sb(osd_dev(d)));
4355 osd_sync(env, lu2dt_dev(d));
4357 rc = osd_procfs_fini(osd_dev(d));
4359 CERROR("proc fini error %d \n", rc);
4360 RETURN (ERR_PTR(rc));
4363 if (osd_dev(d)->od_mount)
4364 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4365 osd_dev(d)->od_mount->lmi_mnt);
4366 osd_dev(d)->od_mount = NULL;
4371 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4372 struct lu_device_type *t,
4373 struct lustre_cfg *cfg)
4375 struct lu_device *l;
4376 struct osd_device *o;
4382 result = dt_device_init(&o->od_dt_dev, t);
4385 l->ld_ops = &osd_lu_ops;
4386 o->od_dt_dev.dd_ops = &osd_dt_ops;
4387 cfs_spin_lock_init(&o->od_osfs_lock);
4388 o->od_osfs_age = cfs_time_shift_64(-1000);
4389 o->od_capa_hash = init_capa_hash();
4390 if (o->od_capa_hash == NULL) {
4391 dt_device_fini(&o->od_dt_dev);
4392 l = ERR_PTR(-ENOMEM);
4395 l = ERR_PTR(result);
4400 l = ERR_PTR(-ENOMEM);
4404 static struct lu_device *osd_device_free(const struct lu_env *env,
4405 struct lu_device *d)
4407 struct osd_device *o = osd_dev(d);
4410 cleanup_capa_hash(o->od_capa_hash);
4411 dt_device_fini(&o->od_dt_dev);
4416 static int osd_process_config(const struct lu_env *env,
4417 struct lu_device *d, struct lustre_cfg *cfg)
4419 struct osd_device *o = osd_dev(d);
4423 switch(cfg->lcfg_command) {
4425 err = osd_mount(env, o, cfg);
4428 err = osd_shutdown(env, o);
4437 static int osd_recovery_complete(const struct lu_env *env,
4438 struct lu_device *d)
4443 static int osd_prepare(const struct lu_env *env,
4444 struct lu_device *pdev,
4445 struct lu_device *dev)
4447 struct osd_device *osd = osd_dev(dev);
4448 struct lustre_sb_info *lsi;
4449 struct lustre_disk_data *ldd;
4450 struct lustre_mount_info *lmi;
4451 struct osd_thread_info *oti = osd_oti_get(env);
4452 struct dt_object *d;
4456 /* 1. initialize oi before any file create or file open */
4457 result = osd_oi_init(oti, &osd->od_oi_table,
4458 &osd->od_dt_dev, lu2md_dev(pdev));
4462 LASSERT(result > 0);
4463 osd->od_oi_count = result;
4465 lmi = osd->od_mount;
4466 lsi = s2lsi(lmi->lmi_sb);
4469 /* 2. setup local objects */
4470 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4474 /* 3. open remote object dir */
4475 d = dt_store_open(env, lu2dt_dev(dev), "",
4476 remote_obj_dir, &oti->oti_fid);
4478 osd->od_obj_area = d;
4481 result = PTR_ERR(d);
4482 osd->od_obj_area = NULL;
4489 static const struct lu_object_operations osd_lu_obj_ops = {
4490 .loo_object_init = osd_object_init,
4491 .loo_object_delete = osd_object_delete,
4492 .loo_object_release = osd_object_release,
4493 .loo_object_free = osd_object_free,
4494 .loo_object_print = osd_object_print,
4495 .loo_object_invariant = osd_object_invariant
4498 const struct lu_device_operations osd_lu_ops = {
4499 .ldo_object_alloc = osd_object_alloc,
4500 .ldo_process_config = osd_process_config,
4501 .ldo_recovery_complete = osd_recovery_complete,
4502 .ldo_prepare = osd_prepare,
4505 static const struct lu_device_type_operations osd_device_type_ops = {
4506 .ldto_init = osd_type_init,
4507 .ldto_fini = osd_type_fini,
4509 .ldto_start = osd_type_start,
4510 .ldto_stop = osd_type_stop,
4512 .ldto_device_alloc = osd_device_alloc,
4513 .ldto_device_free = osd_device_free,
4515 .ldto_device_init = osd_device_init,
4516 .ldto_device_fini = osd_device_fini
4519 static struct lu_device_type osd_device_type = {
4520 .ldt_tags = LU_DEVICE_DT,
4521 .ldt_name = LUSTRE_OSD_NAME,
4522 .ldt_ops = &osd_device_type_ops,
4523 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4527 * lprocfs legacy support.
4529 static struct obd_ops osd_obd_device_ops = {
4530 .o_owner = THIS_MODULE
4533 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4534 .llod_name = remote_obj_dir,
4535 .llod_oid = OSD_REM_OBJ_DIR_OID,
4537 .llod_feat = &dt_directory_features,
4540 static int __init osd_mod_init(void)
4542 struct lprocfs_static_vars lvars;
4545 llo_local_obj_register(&llod_osd_rem_obj_dir);
4546 lprocfs_osd_init_vars(&lvars);
4547 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4548 LUSTRE_OSD_NAME, &osd_device_type);
4551 static void __exit osd_mod_exit(void)
4553 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4554 class_unregister_type(LUSTRE_OSD_NAME);
4557 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4558 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4559 MODULE_LICENSE("GPL");
4561 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);