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 Whamcloud, Inc.
36 * Copyright (c) 2011 Whamcloud, Inc.
39 * This file is part of Lustre, http://www.lustre.org/
40 * Lustre is a trademark of Sun Microsystems, Inc.
42 * lustre/osd/osd_handler.c
44 * Top-level entry points into osd module
46 * Author: Nikita Danilov <nikita@clusterfs.com>
47 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
51 # define EXPORT_SYMTAB
53 #define DEBUG_SUBSYSTEM S_MDS
55 #include <linux/module.h>
57 /* LUSTRE_VERSION_CODE */
58 #include <lustre_ver.h>
59 /* prerequisite for linux/xattr.h */
60 #include <linux/types.h>
61 /* prerequisite for linux/xattr.h */
63 /* XATTR_{REPLACE,CREATE} */
64 #include <linux/xattr.h>
69 * struct OBD_{ALLOC,FREE}*()
72 #include <obd_support.h>
73 /* struct ptlrpc_thread */
74 #include <lustre_net.h>
77 #include <lustre_fid.h>
79 #include "osd_internal.h"
82 /* llo_* api support */
83 #include <md_object.h>
85 #ifdef HAVE_LDISKFS_PDO
87 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
88 "ldiskfs with parallel directory operations");
93 static const char dot[] = ".";
94 static const char dotdot[] = "..";
95 static const char remote_obj_dir[] = "REM_OBJ_DIR";
97 struct osd_directory {
98 struct iam_container od_container;
99 struct iam_descr od_descr;
103 struct dt_object oo_dt;
105 * Inode for file system object represented by this osd_object. This
106 * inode is pinned for the whole duration of lu_object life.
108 * Not modified concurrently (either setup early during object
109 * creation, or assigned by osd_object_create() under write lock).
111 struct inode *oo_inode;
113 * to protect index ops.
115 struct htree_lock_head *oo_hl_head;
116 cfs_rw_semaphore_t oo_ext_idx_sem;
117 cfs_rw_semaphore_t oo_sem;
118 struct osd_directory *oo_dir;
119 /** protects inode attributes. */
120 cfs_spinlock_t oo_guard;
122 * Following two members are used to indicate the presence of dot and
123 * dotdot in the given directory. This is required for interop mode
126 int oo_compat_dot_created;
127 int oo_compat_dotdot_created;
129 const struct lu_env *oo_owner;
130 #ifdef CONFIG_LOCKDEP
131 struct lockdep_map oo_dep_map;
135 static const struct lu_object_operations osd_lu_obj_ops;
136 static const struct lu_device_operations osd_lu_ops;
137 static struct lu_context_key osd_key;
138 static const struct dt_object_operations osd_obj_ops;
139 static const struct dt_object_operations osd_obj_ea_ops;
140 static const struct dt_body_operations osd_body_ops;
141 static const struct dt_body_operations osd_body_ops_new;
142 static const struct dt_index_operations osd_index_iam_ops;
143 static const struct dt_index_operations osd_index_ea_ops;
145 #define OSD_TRACK_DECLARES
146 #ifdef OSD_TRACK_DECLARES
147 #define OSD_DECLARE_OP(oh, op) { \
148 LASSERT(oh->ot_handle == NULL); \
149 ((oh)->ot_declare_ ##op)++; }
150 #define OSD_EXEC_OP(handle, op) { \
151 struct osd_thandle *oh; \
152 oh = container_of0(handle, struct osd_thandle, ot_super);\
153 LASSERT((oh)->ot_declare_ ##op > 0); \
154 ((oh)->ot_declare_ ##op)--; }
156 #define OSD_DECLARE_OP(oh, op)
157 #define OSD_EXEC_OP(oh, op)
161 struct thandle ot_super;
163 struct journal_callback ot_jcb;
164 cfs_list_t ot_dcb_list;
165 /* Link to the device, for debugging. */
166 struct lu_ref_link *ot_dev_link;
169 #ifdef OSD_TRACK_DECLARES
170 unsigned char ot_declare_attr_set;
171 unsigned char ot_declare_punch;
172 unsigned char ot_declare_xattr_set;
173 unsigned char ot_declare_create;
174 unsigned char ot_declare_destroy;
175 unsigned char ot_declare_ref_add;
176 unsigned char ot_declare_ref_del;
177 unsigned char ot_declare_write;
178 unsigned char ot_declare_insert;
179 unsigned char ot_declare_delete;
182 #if OSD_THANDLE_STATS
183 /** time when this handle was allocated */
184 cfs_time_t oth_alloced;
186 /** time when this thanle was started */
187 cfs_time_t oth_started;
192 * Basic transaction credit op
202 DTO_LOG_REC, /**< XXX temporary: dt layer knows nothing about llog. */
213 static int lu_device_is_osd(const struct lu_device *d)
215 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
218 static struct osd_device *osd_dt_dev(const struct dt_device *d)
220 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
221 return container_of0(d, struct osd_device, od_dt_dev);
224 static struct osd_device *osd_dev(const struct lu_device *d)
226 LASSERT(lu_device_is_osd(d));
227 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
230 static struct osd_device *osd_obj2dev(const struct osd_object *o)
232 return osd_dev(o->oo_dt.do_lu.lo_dev);
235 static struct super_block *osd_sb(const struct osd_device *dev)
237 return dev->od_mount->lmi_mnt->mnt_sb;
240 static int osd_object_is_root(const struct osd_object *obj)
242 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
245 static struct osd_object *osd_obj(const struct lu_object *o)
247 LASSERT(lu_device_is_osd(o->lo_dev));
248 return container_of0(o, struct osd_object, oo_dt.do_lu);
251 static struct osd_object *osd_dt_obj(const struct dt_object *d)
253 return osd_obj(&d->do_lu);
256 static struct lu_device *osd2lu_dev(struct osd_device *osd)
258 return &osd->od_dt_dev.dd_lu_dev;
261 static journal_t *osd_journal(const struct osd_device *dev)
263 return LDISKFS_SB(osd_sb(dev))->s_journal;
266 static int osd_has_index(const struct osd_object *obj)
268 return obj->oo_dt.do_index_ops != NULL;
271 static int osd_object_invariant(const struct lu_object *l)
273 return osd_invariant(osd_obj(l));
276 #ifdef HAVE_QUOTA_SUPPORT
278 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
280 struct md_ucred *uc = md_ucred(env);
285 save->oc_uid = current_fsuid();
286 save->oc_gid = current_fsgid();
287 save->oc_cap = current_cap();
288 if ((tc = prepare_creds())) {
289 tc->fsuid = uc->mu_fsuid;
290 tc->fsgid = uc->mu_fsgid;
293 /* XXX not suboptimal */
294 cfs_curproc_cap_unpack(uc->mu_cap);
298 osd_pop_ctxt(struct osd_ctxt *save)
302 if ((tc = prepare_creds())) {
303 tc->fsuid = save->oc_uid;
304 tc->fsgid = save->oc_gid;
305 tc->cap_effective = save->oc_cap;
311 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
313 return lu_context_key_get(&env->le_ctx, &osd_key);
317 * Concurrency: doesn't matter
319 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
321 return osd_oti_get(env)->oti_r_locks > 0;
325 * Concurrency: doesn't matter
327 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
329 struct osd_thread_info *oti = osd_oti_get(env);
330 return oti->oti_w_locks > 0 && o->oo_owner == env;
334 * Concurrency: doesn't access mutable data
336 static int osd_root_get(const struct lu_env *env,
337 struct dt_device *dev, struct lu_fid *f)
341 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
342 LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
347 * OSD object methods.
351 * Concurrency: no concurrent access is possible that early in object
354 static struct lu_object *osd_object_alloc(const struct lu_env *env,
355 const struct lu_object_header *hdr,
358 struct osd_object *mo;
364 l = &mo->oo_dt.do_lu;
365 dt_object_init(&mo->oo_dt, NULL, d);
366 if (osd_dev(d)->od_iop_mode)
367 mo->oo_dt.do_ops = &osd_obj_ea_ops;
369 mo->oo_dt.do_ops = &osd_obj_ops;
371 l->lo_ops = &osd_lu_obj_ops;
372 cfs_init_rwsem(&mo->oo_sem);
373 cfs_init_rwsem(&mo->oo_ext_idx_sem);
374 cfs_spin_lock_init(&mo->oo_guard);
382 * retrieve object from backend ext fs.
384 static struct inode *osd_iget(struct osd_thread_info *info,
385 struct osd_device *dev,
386 const struct osd_inode_id *id)
388 struct inode *inode = NULL;
390 #ifdef HAVE_EXT4_LDISKFS
391 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
393 /* Newer kernels return an error instead of a NULL pointer */
396 inode = iget(osd_sb(dev), id->oii_ino);
399 CERROR("no inode\n");
400 inode = ERR_PTR(-EACCES);
401 } else if (id->oii_gen != OSD_OII_NOGEN &&
402 inode->i_generation != id->oii_gen) {
404 inode = ERR_PTR(-ESTALE);
405 } else if (inode->i_nlink == 0) {
406 /* due to parallel readdir and unlink,
407 * we can have dead inode here. */
408 CWARN("stale inode\n");
409 make_bad_inode(inode);
411 inode = ERR_PTR(-ESTALE);
412 } else if (is_bad_inode(inode)) {
413 CERROR("bad inode %lx\n",inode->i_ino);
415 inode = ERR_PTR(-ENOENT);
417 /* Do not update file c/mtime in ldiskfs.
418 * NB: we don't have any lock to protect this because we don't
419 * have reference on osd_object now, but contention with
420 * another lookup + attr_set can't happen in the tiny window
421 * between if (...) and set S_NOCMTIME. */
422 if (!(inode->i_flags & S_NOCMTIME))
423 inode->i_flags |= S_NOCMTIME;
428 static int osd_fid_lookup(const struct lu_env *env,
429 struct osd_object *obj, const struct lu_fid *fid)
431 struct osd_thread_info *info;
432 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
433 struct osd_device *dev;
434 struct osd_inode_id *id;
439 LINVRNT(osd_invariant(obj));
440 LASSERT(obj->oo_inode == NULL);
441 LASSERTF(fid_is_sane(fid) || osd_fid_is_root(fid), DFID, PFID(fid));
443 * This assertion checks that osd layer sees only local
444 * fids. Unfortunately it is somewhat expensive (does a
445 * cache-lookup). Disabling it for production/acceptance-testing.
447 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
451 info = osd_oti_get(env);
456 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
459 result = osd_oi_lookup(info, oi, fid, id);
461 if (result == -ENOENT)
466 inode = osd_iget(info, dev, id);
469 * If fid wasn't found in oi, inode-less object is
470 * created, for which lu_object_exists() returns
471 * false. This is used in a (frequent) case when
472 * objects are created as locking anchors or
473 * place holders for objects yet to be created.
475 result = PTR_ERR(inode);
479 obj->oo_inode = inode;
480 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
481 if (dev->od_iop_mode) {
482 obj->oo_compat_dot_created = 1;
483 obj->oo_compat_dotdot_created = 1;
486 if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
489 LASSERT(obj->oo_hl_head == NULL);
490 obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
491 if (obj->oo_hl_head == NULL) {
492 obj->oo_inode = NULL;
497 LINVRNT(osd_invariant(obj));
503 * Concurrency: shouldn't matter.
505 static void osd_object_init0(struct osd_object *obj)
507 LASSERT(obj->oo_inode != NULL);
508 obj->oo_dt.do_body_ops = &osd_body_ops;
509 obj->oo_dt.do_lu.lo_header->loh_attr |=
510 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
514 * Concurrency: no concurrent access is possible that early in object
517 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
518 const struct lu_object_conf *unused)
520 struct osd_object *obj = osd_obj(l);
523 LINVRNT(osd_invariant(obj));
525 result = osd_fid_lookup(env, obj, lu_object_fid(l));
526 obj->oo_dt.do_body_ops = &osd_body_ops_new;
528 if (obj->oo_inode != NULL)
529 osd_object_init0(obj);
531 LINVRNT(osd_invariant(obj));
536 * Concurrency: no concurrent access is possible that late in object
539 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
541 struct osd_object *obj = osd_obj(l);
543 LINVRNT(osd_invariant(obj));
545 dt_object_fini(&obj->oo_dt);
546 if (obj->oo_hl_head != NULL)
547 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
554 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
555 const struct iam_container *bag)
557 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
558 osd_oti_get(env)->oti_it_ipd);
561 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
562 const struct iam_container *bag)
564 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
565 osd_oti_get(env)->oti_idx_ipd);
568 static void osd_ipd_put(const struct lu_env *env,
569 const struct iam_container *bag,
570 struct iam_path_descr *ipd)
572 bag->ic_descr->id_ops->id_ipd_free(ipd);
576 * Concurrency: no concurrent access is possible that late in object
579 static void osd_index_fini(struct osd_object *o)
581 struct iam_container *bag;
583 if (o->oo_dir != NULL) {
584 bag = &o->oo_dir->od_container;
585 if (o->oo_inode != NULL) {
586 if (bag->ic_object == o->oo_inode)
587 iam_container_fini(bag);
589 OBD_FREE_PTR(o->oo_dir);
595 * Concurrency: no concurrent access is possible that late in object
596 * life-cycle (for all existing callers, that is. New callers have to provide
597 * their own locking.)
599 static int osd_inode_unlinked(const struct inode *inode)
601 return inode->i_nlink == 0;
605 OSD_TXN_OI_DELETE_CREDITS = 20,
606 OSD_TXN_INODE_DELETE_CREDITS = 20
613 #if OSD_THANDLE_STATS
615 * Set time when the handle is allocated
617 static void osd_th_alloced(struct osd_thandle *oth)
619 oth->oth_alloced = cfs_time_current();
623 * Set time when the handle started
625 static void osd_th_started(struct osd_thandle *oth)
627 oth->oth_started = cfs_time_current();
631 * Helper function to convert time interval to microseconds packed in
632 * long int (default time units for the counter in "stats" initialized
633 * by lu_time_init() )
635 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
639 cfs_duration_usec(cfs_time_sub(end, start), &val);
640 return val.tv_sec * 1000000 + val.tv_usec;
644 * Check whether the we deal with this handle for too long.
646 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
647 cfs_time_t alloced, cfs_time_t started,
650 cfs_time_t now = cfs_time_current();
652 LASSERT(dev != NULL);
654 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
655 interval_to_usec(alloced, started));
656 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
657 interval_to_usec(started, closed));
658 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
659 interval_to_usec(closed, now));
661 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
662 CWARN("transaction handle %p was open for too long: "
664 "alloced "CFS_TIME_T" ,"
665 "started "CFS_TIME_T" ,"
666 "closed "CFS_TIME_T"\n",
667 oth, now, alloced, started, closed);
668 libcfs_debug_dumpstack(NULL);
672 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
674 cfs_time_t __closed = cfs_time_current(); \
675 cfs_time_t __alloced = oth->oth_alloced; \
676 cfs_time_t __started = oth->oth_started; \
679 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
682 #else /* OSD_THANDLE_STATS */
684 #define osd_th_alloced(h) do {} while(0)
685 #define osd_th_started(h) do {} while(0)
686 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
688 #endif /* OSD_THANDLE_STATS */
691 * Concurrency: doesn't access mutable data.
693 static int osd_param_is_sane(const struct osd_device *dev,
694 const struct thandle *th)
696 struct osd_thandle *oh;
697 oh = container_of0(th, struct osd_thandle, ot_super);
698 return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
702 * Concurrency: shouldn't matter.
704 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
705 static void osd_trans_commit_cb(struct super_block *sb,
706 struct journal_callback *jcb, int error)
708 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
711 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
712 struct thandle *th = &oh->ot_super;
713 struct lu_device *lud = &th->th_dev->dd_lu_dev;
714 struct dt_txn_commit_cb *dcb, *tmp;
716 LASSERT(oh->ot_handle == NULL);
719 CERROR("transaction @0x%p commit error: %d\n", th, error);
721 dt_txn_hook_commit(th);
723 /* call per-transaction callbacks if any */
724 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage)
725 dcb->dcb_func(NULL, th, dcb, error);
727 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
731 lu_context_exit(&th->th_ctx);
732 lu_context_fini(&th->th_ctx);
736 static struct thandle *osd_trans_create(const struct lu_env *env,
739 struct osd_thread_info *oti = osd_oti_get(env);
740 struct osd_thandle *oh;
744 th = ERR_PTR(-ENOMEM);
745 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
750 th->th_tags = LCT_TX_HANDLE;
752 oti->oti_dev = osd_dt_dev(d);
753 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
760 * Concurrency: shouldn't matter.
762 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
765 struct osd_thread_info *oti = osd_oti_get(env);
766 struct osd_device *dev = osd_dt_dev(d);
768 struct osd_thandle *oh;
773 LASSERT(current->journal_info == NULL);
775 oh = container_of0(th, struct osd_thandle, ot_super);
777 LASSERT(oh->ot_handle == NULL);
779 rc = dt_txn_hook_start(env, d, th);
783 oh->ot_credits += LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(dev));
785 if (!osd_param_is_sane(dev, th)) {
786 CWARN("%s: too many transaction credits (%d > %d)\n",
787 d->dd_lu_dev.ld_obd->obd_name, oh->ot_credits,
788 osd_journal(dev)->j_max_transaction_buffers);
789 #ifdef OSD_TRACK_DECLARES
790 CERROR(" attr_set: %d, punch: %d, xattr_set: %d,\n",
791 oh->ot_declare_attr_set, oh->ot_declare_punch,
792 oh->ot_declare_xattr_set);
793 CERROR(" create: %d, ref_add: %d, ref_del: %d, write: %d\n",
794 oh->ot_declare_create, oh->ot_declare_ref_add,
795 oh->ot_declare_ref_del, oh->ot_declare_write);
796 CERROR(" insert: %d, delete: %d\n",
797 oh->ot_declare_insert, oh->ot_declare_delete);
802 * XXX temporary stuff. Some abstraction layer should
805 jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
809 LASSERT(oti->oti_txns == 0);
810 lu_context_init(&th->th_ctx, th->th_tags);
811 lu_context_enter(&th->th_ctx);
813 lu_device_get(&d->dd_lu_dev);
814 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
818 * XXX: current rule is that we first start tx,
819 * then lock object(s), but we can't use
820 * this rule for data (due to locking specifics
821 * in ldiskfs). also in long-term we'd like to
822 * use usually-used (locks;tx) ordering. so,
823 * UGLY thing is that we'll use one ordering for
824 * data (ofd) and reverse ordering for metadata
825 * (mdd). then at some point we'll fix the latter
827 if (lu_device_is_md(&d->dd_lu_dev)) {
828 LASSERT(oti->oti_r_locks == 0);
829 LASSERT(oti->oti_w_locks == 0);
842 * Concurrency: shouldn't matter.
844 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
847 struct osd_thandle *oh;
848 struct osd_thread_info *oti = osd_oti_get(env);
852 oh = container_of0(th, struct osd_thandle, ot_super);
854 if (oh->ot_handle != NULL) {
855 handle_t *hdl = oh->ot_handle;
857 hdl->h_sync = th->th_sync;
860 * add commit callback
861 * notice we don't do this in osd_trans_start()
862 * as underlying transaction can change during truncate
864 osd_journal_callback_set(hdl, osd_trans_commit_cb,
867 LASSERT(oti->oti_txns == 1);
870 * XXX: current rule is that we first start tx,
871 * then lock object(s), but we can't use
872 * this rule for data (due to locking specifics
873 * in ldiskfs). also in long-term we'd like to
874 * use usually-used (locks;tx) ordering. so,
875 * UGLY thing is that we'll use one ordering for
876 * data (ofd) and reverse ordering for metadata
877 * (mdd). then at some point we'll fix the latter
879 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
880 LASSERT(oti->oti_r_locks == 0);
881 LASSERT(oti->oti_w_locks == 0);
883 rc = dt_txn_hook_stop(env, th);
885 CERROR("Failure in transaction hook: %d\n", rc);
886 oh->ot_handle = NULL;
887 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
888 rc = ldiskfs_journal_stop(hdl));
890 CERROR("Failure to stop transaction: %d\n", rc);
898 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
900 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
903 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
909 * Called just before object is freed. Releases all resources except for
910 * object itself (that is released by osd_object_free()).
912 * Concurrency: no concurrent access is possible that late in object
915 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
917 struct osd_object *obj = osd_obj(l);
918 struct inode *inode = obj->oo_inode;
920 LINVRNT(osd_invariant(obj));
923 * If object is unlinked remove fid->ino mapping from object index.
929 obj->oo_inode = NULL;
934 * Concurrency: ->loo_object_release() is called under site spin-lock.
936 static void osd_object_release(const struct lu_env *env,
942 * Concurrency: shouldn't matter.
944 static int osd_object_print(const struct lu_env *env, void *cookie,
945 lu_printer_t p, const struct lu_object *l)
947 struct osd_object *o = osd_obj(l);
950 if (o->oo_dir != NULL)
951 d = o->oo_dir->od_container.ic_descr;
954 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
956 o->oo_inode ? o->oo_inode->i_ino : 0UL,
957 o->oo_inode ? o->oo_inode->i_generation : 0,
958 d ? d->id_ops->id_name : "plain");
962 * Concurrency: shouldn't matter.
964 int osd_statfs(const struct lu_env *env, struct dt_device *d,
967 struct osd_device *osd = osd_dt_dev(d);
968 struct super_block *sb = osd_sb(osd);
971 cfs_spin_lock(&osd->od_osfs_lock);
973 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
974 result = ll_do_statfs(sb, &osd->od_kstatfs);
975 if (likely(result == 0)) /* N.B. statfs can't really fail */
976 osd->od_osfs_age = cfs_time_current_64();
979 if (likely(result == 0))
980 *sfs = osd->od_kstatfs;
981 cfs_spin_unlock(&osd->od_osfs_lock);
987 * Concurrency: doesn't access mutable data.
989 static void osd_conf_get(const struct lu_env *env,
990 const struct dt_device *dev,
991 struct dt_device_param *param)
993 struct super_block *sb = osd_sb(osd_dt_dev(dev));
996 * XXX should be taken from not-yet-existing fs abstraction layer.
998 param->ddp_max_name_len = LDISKFS_NAME_LEN;
999 param->ddp_max_nlink = LDISKFS_LINK_MAX;
1000 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
1001 param->ddp_mntopts = 0;
1002 if (test_opt(sb, XATTR_USER))
1003 param->ddp_mntopts |= MNTOPT_USERXATTR;
1004 if (test_opt(sb, POSIX_ACL))
1005 param->ddp_mntopts |= MNTOPT_ACL;
1007 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1008 if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1009 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1012 param->ddp_max_ea_size = sb->s_blocksize;
1017 * Helper function to get and fill the buffer with input values.
1019 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1023 buf = &osd_oti_get(env)->oti_buf;
1030 * Concurrency: shouldn't matter.
1032 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1034 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1035 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1039 * Start commit for OSD device.
1041 * An implementation of dt_commit_async method for OSD device.
1042 * Asychronously starts underlayng fs sync and thereby a transaction
1045 * \param env environment
1046 * \param d dt device
1048 * \see dt_device_operations
1050 static int osd_commit_async(const struct lu_env *env,
1051 struct dt_device *d)
1053 struct super_block *s = osd_sb(osd_dt_dev(d));
1056 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1057 RETURN(s->s_op->sync_fs(s, 0));
1061 * Concurrency: shouldn't matter.
1064 static void osd_ro(const struct lu_env *env, struct dt_device *d)
1066 struct super_block *sb = osd_sb(osd_dt_dev(d));
1069 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1071 __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1076 * Concurrency: serialization provided by callers.
1078 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1079 int mode, unsigned long timeout, __u32 alg,
1080 struct lustre_capa_key *keys)
1082 struct osd_device *dev = osd_dt_dev(d);
1085 dev->od_fl_capa = mode;
1086 dev->od_capa_timeout = timeout;
1087 dev->od_capa_alg = alg;
1088 dev->od_capa_keys = keys;
1093 * Concurrency: serialization provided by callers.
1095 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1096 struct dt_quota_ctxt *ctxt, void *data)
1098 struct obd_device *obd = (void *)ctxt;
1099 struct vfsmount *mnt = (struct vfsmount *)data;
1102 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1103 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1104 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1105 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1106 obd->obd_lvfs_ctxt.fs = get_ds();
1112 * Note: we do not count into QUOTA here.
1113 * If we mount with --data_journal we may need more.
1115 static const int osd_dto_credits_noquota[DTO_NR] = {
1118 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1119 * SINGLEDATA_TRANS_BLOCKS(8)
1120 * XXX Note: maybe iam need more, since iam have more level than
1123 [DTO_INDEX_INSERT] = 16,
1124 [DTO_INDEX_DELETE] = 16,
1128 [DTO_INDEX_UPDATE] = 16,
1130 * Create a object. The same as create object in EXT3.
1131 * DATA_TRANS_BLOCKS(14) +
1132 * INDEX_EXTRA_BLOCKS(8) +
1133 * 3(inode bits, groups, GDT)
1135 [DTO_OBJECT_CREATE] = 25,
1137 * XXX: real credits to be fixed
1139 [DTO_OBJECT_DELETE] = 25,
1141 * Attr set credits (inode)
1143 [DTO_ATTR_SET_BASE] = 1,
1145 * Xattr set. The same as xattr of EXT3.
1146 * DATA_TRANS_BLOCKS(14)
1147 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1148 * are also counted in. Do not know why?
1150 [DTO_XATTR_SET] = 14,
1153 * credits for inode change during write.
1155 [DTO_WRITE_BASE] = 3,
1157 * credits for single block write.
1159 [DTO_WRITE_BLOCK] = 14,
1161 * Attr set credits for chown.
1162 * This is extra credits for setattr, and it is null without quota
1164 [DTO_ATTR_SET_CHOWN]= 0
1168 * Note: we count into QUOTA here.
1169 * If we mount with --data_journal we may need more.
1171 static const int osd_dto_credits_quota[DTO_NR] = {
1173 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1174 * SINGLEDATA_TRANS_BLOCKS(8) +
1175 * 2 * QUOTA_TRANS_BLOCKS(2)
1177 [DTO_INDEX_INSERT] = 20,
1179 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1180 * SINGLEDATA_TRANS_BLOCKS(8) +
1181 * 2 * QUOTA_TRANS_BLOCKS(2)
1183 [DTO_INDEX_DELETE] = 20,
1187 [DTO_INDEX_UPDATE] = 16,
1189 * Create a object. Same as create object in EXT3 filesystem.
1190 * DATA_TRANS_BLOCKS(16) +
1191 * INDEX_EXTRA_BLOCKS(8) +
1192 * 3(inode bits, groups, GDT) +
1193 * 2 * QUOTA_INIT_BLOCKS(25)
1195 [DTO_OBJECT_CREATE] = 77,
1198 * DATA_TRANS_BLOCKS(16) +
1199 * INDEX_EXTRA_BLOCKS(8) +
1200 * 3(inode bits, groups, GDT) +
1203 [DTO_OBJECT_DELETE] = 27,
1206 * 3 (inode bit, group, GDT) +
1208 [DTO_ATTR_SET_BASE] = 3,
1210 * Xattr set. The same as xattr of EXT3.
1211 * DATA_TRANS_BLOCKS(16)
1212 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
1213 * also counted in. Do not know why?
1215 [DTO_XATTR_SET] = 16,
1218 * creadits for inode change during write.
1220 [DTO_WRITE_BASE] = 3,
1222 * credits for single block write.
1224 [DTO_WRITE_BLOCK] = 16,
1226 * Attr set credits for chown.
1227 * It is added to already set setattr credits
1228 * 2 * QUOTA_INIT_BLOCKS(25) +
1229 * 2 * QUOTA_DEL_BLOCKS(9)
1231 [DTO_ATTR_SET_CHOWN]= 68,
1234 static const struct dt_device_operations osd_dt_ops = {
1235 .dt_root_get = osd_root_get,
1236 .dt_statfs = osd_statfs,
1237 .dt_trans_create = osd_trans_create,
1238 .dt_trans_start = osd_trans_start,
1239 .dt_trans_stop = osd_trans_stop,
1240 .dt_trans_cb_add = osd_trans_cb_add,
1241 .dt_conf_get = osd_conf_get,
1242 .dt_sync = osd_sync,
1244 .dt_commit_async = osd_commit_async,
1245 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1246 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1249 static void osd_object_read_lock(const struct lu_env *env,
1250 struct dt_object *dt, unsigned role)
1252 struct osd_object *obj = osd_dt_obj(dt);
1253 struct osd_thread_info *oti = osd_oti_get(env);
1255 LINVRNT(osd_invariant(obj));
1257 LASSERT(obj->oo_owner != env);
1258 cfs_down_read_nested(&obj->oo_sem, role);
1260 LASSERT(obj->oo_owner == NULL);
1264 static void osd_object_write_lock(const struct lu_env *env,
1265 struct dt_object *dt, unsigned role)
1267 struct osd_object *obj = osd_dt_obj(dt);
1268 struct osd_thread_info *oti = osd_oti_get(env);
1270 LINVRNT(osd_invariant(obj));
1272 LASSERT(obj->oo_owner != env);
1273 cfs_down_write_nested(&obj->oo_sem, role);
1275 LASSERT(obj->oo_owner == NULL);
1276 obj->oo_owner = env;
1280 static void osd_object_read_unlock(const struct lu_env *env,
1281 struct dt_object *dt)
1283 struct osd_object *obj = osd_dt_obj(dt);
1284 struct osd_thread_info *oti = osd_oti_get(env);
1286 LINVRNT(osd_invariant(obj));
1288 LASSERT(oti->oti_r_locks > 0);
1290 cfs_up_read(&obj->oo_sem);
1293 static void osd_object_write_unlock(const struct lu_env *env,
1294 struct dt_object *dt)
1296 struct osd_object *obj = osd_dt_obj(dt);
1297 struct osd_thread_info *oti = osd_oti_get(env);
1299 LINVRNT(osd_invariant(obj));
1301 LASSERT(obj->oo_owner == env);
1302 LASSERT(oti->oti_w_locks > 0);
1304 obj->oo_owner = NULL;
1305 cfs_up_write(&obj->oo_sem);
1308 static int osd_object_write_locked(const struct lu_env *env,
1309 struct dt_object *dt)
1311 struct osd_object *obj = osd_dt_obj(dt);
1313 LINVRNT(osd_invariant(obj));
1315 return obj->oo_owner == env;
1318 static int capa_is_sane(const struct lu_env *env,
1319 struct osd_device *dev,
1320 struct lustre_capa *capa,
1321 struct lustre_capa_key *keys)
1323 struct osd_thread_info *oti = osd_oti_get(env);
1324 struct lustre_capa *tcapa = &oti->oti_capa;
1325 struct obd_capa *oc;
1329 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1331 if (capa_is_expired(oc)) {
1332 DEBUG_CAPA(D_ERROR, capa, "expired");
1339 if (capa_is_expired_sec(capa)) {
1340 DEBUG_CAPA(D_ERROR, capa, "expired");
1344 cfs_spin_lock(&capa_lock);
1345 for (i = 0; i < 2; i++) {
1346 if (keys[i].lk_keyid == capa->lc_keyid) {
1347 oti->oti_capa_key = keys[i];
1351 cfs_spin_unlock(&capa_lock);
1354 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1358 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1362 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1363 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1367 oc = capa_add(dev->od_capa_hash, capa);
1373 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1374 struct lustre_capa *capa, __u64 opc)
1376 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1377 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1378 struct md_capainfo *ci;
1381 if (!dev->od_fl_capa)
1384 if (capa == BYPASS_CAPA)
1387 ci = md_capainfo(env);
1391 if (ci->mc_auth == LC_ID_NONE)
1395 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1399 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1400 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1405 if (!capa_opc_supported(capa, opc)) {
1406 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1410 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1411 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1418 static struct timespec *osd_inode_time(const struct lu_env *env,
1419 struct inode *inode, __u64 seconds)
1421 struct osd_thread_info *oti = osd_oti_get(env);
1422 struct timespec *t = &oti->oti_time;
1424 t->tv_sec = seconds;
1426 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1431 static void osd_inode_getattr(const struct lu_env *env,
1432 struct inode *inode, struct lu_attr *attr)
1434 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1435 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1436 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1438 attr->la_atime = LTIME_S(inode->i_atime);
1439 attr->la_mtime = LTIME_S(inode->i_mtime);
1440 attr->la_ctime = LTIME_S(inode->i_ctime);
1441 attr->la_mode = inode->i_mode;
1442 attr->la_size = i_size_read(inode);
1443 attr->la_blocks = inode->i_blocks;
1444 attr->la_uid = inode->i_uid;
1445 attr->la_gid = inode->i_gid;
1446 attr->la_flags = LDISKFS_I(inode)->i_flags;
1447 attr->la_nlink = inode->i_nlink;
1448 attr->la_rdev = inode->i_rdev;
1449 attr->la_blksize = ll_inode_blksize(inode);
1450 attr->la_blkbits = inode->i_blkbits;
1453 static int osd_attr_get(const struct lu_env *env,
1454 struct dt_object *dt,
1455 struct lu_attr *attr,
1456 struct lustre_capa *capa)
1458 struct osd_object *obj = osd_dt_obj(dt);
1460 LASSERT(dt_object_exists(dt));
1461 LINVRNT(osd_invariant(obj));
1463 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1466 cfs_spin_lock(&obj->oo_guard);
1467 osd_inode_getattr(env, obj->oo_inode, attr);
1468 cfs_spin_unlock(&obj->oo_guard);
1472 static int osd_declare_attr_set(const struct lu_env *env,
1473 struct dt_object *dt,
1474 const struct lu_attr *attr,
1475 struct thandle *handle)
1477 struct osd_thandle *oh;
1479 LASSERT(handle != NULL);
1480 LASSERT(osd_invariant(obj));
1482 oh = container_of0(handle, struct osd_thandle, ot_super);
1483 LASSERT(oh->ot_handle == NULL);
1485 OSD_DECLARE_OP(oh, attr_set);
1486 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1491 static int osd_inode_setattr(const struct lu_env *env,
1492 struct inode *inode, const struct lu_attr *attr)
1496 bits = attr->la_valid;
1498 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1500 #ifdef HAVE_QUOTA_SUPPORT
1501 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1502 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1503 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1509 iattr.ia_valid |= ATTR_UID;
1511 iattr.ia_valid |= ATTR_GID;
1512 iattr.ia_uid = attr->la_uid;
1513 iattr.ia_gid = attr->la_gid;
1514 osd_push_ctxt(env, save);
1515 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1522 if (bits & LA_ATIME)
1523 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1524 if (bits & LA_CTIME)
1525 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1526 if (bits & LA_MTIME)
1527 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1528 if (bits & LA_SIZE) {
1529 LDISKFS_I(inode)->i_disksize = attr->la_size;
1530 i_size_write(inode, attr->la_size);
1534 /* OSD should not change "i_blocks" which is used by quota.
1535 * "i_blocks" should be changed by ldiskfs only. */
1536 if (bits & LA_BLOCKS)
1537 inode->i_blocks = attr->la_blocks;
1540 inode->i_mode = (inode->i_mode & S_IFMT) |
1541 (attr->la_mode & ~S_IFMT);
1543 inode->i_uid = attr->la_uid;
1545 inode->i_gid = attr->la_gid;
1546 if (bits & LA_NLINK)
1547 inode->i_nlink = attr->la_nlink;
1549 inode->i_rdev = attr->la_rdev;
1551 if (bits & LA_FLAGS) {
1552 /* always keep S_NOCMTIME */
1553 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1559 static int osd_attr_set(const struct lu_env *env,
1560 struct dt_object *dt,
1561 const struct lu_attr *attr,
1562 struct thandle *handle,
1563 struct lustre_capa *capa)
1565 struct osd_object *obj = osd_dt_obj(dt);
1568 LASSERT(handle != NULL);
1569 LASSERT(dt_object_exists(dt));
1570 LASSERT(osd_invariant(obj));
1572 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1575 OSD_EXEC_OP(handle, attr_set);
1577 cfs_spin_lock(&obj->oo_guard);
1578 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1579 cfs_spin_unlock(&obj->oo_guard);
1582 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1589 * XXX temporary solution.
1591 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1592 struct lu_attr *attr, struct thandle *th)
1597 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1598 struct lu_attr *attr, struct thandle *th)
1600 osd_object_init0(obj);
1601 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1602 unlock_new_inode(obj->oo_inode);
1606 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1607 struct osd_object *obj,
1611 struct osd_thread_info *info = osd_oti_get(env);
1612 struct dentry *child_dentry = &info->oti_child_dentry;
1613 struct dentry *obj_dentry = &info->oti_obj_dentry;
1615 obj_dentry->d_inode = obj->oo_inode;
1616 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1617 obj_dentry->d_name.hash = 0;
1619 child_dentry->d_name.hash = 0;
1620 child_dentry->d_parent = obj_dentry;
1621 child_dentry->d_name.name = name;
1622 child_dentry->d_name.len = namelen;
1623 return child_dentry;
1627 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1629 struct dt_allocation_hint *hint,
1633 struct osd_device *osd = osd_obj2dev(obj);
1634 struct osd_thandle *oth;
1635 struct dt_object *parent;
1636 struct inode *inode;
1637 #ifdef HAVE_QUOTA_SUPPORT
1638 struct osd_ctxt *save = &info->oti_ctxt;
1641 LINVRNT(osd_invariant(obj));
1642 LASSERT(obj->oo_inode == NULL);
1643 LASSERT(obj->oo_hl_head == NULL);
1645 if (S_ISDIR(mode) && ldiskfs_pdo) {
1646 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1647 if (obj->oo_hl_head == NULL)
1651 oth = container_of(th, struct osd_thandle, ot_super);
1652 LASSERT(oth->ot_handle->h_transaction != NULL);
1654 if (hint && hint->dah_parent)
1655 parent = hint->dah_parent;
1657 parent = osd->od_obj_area;
1659 LASSERT(parent != NULL);
1660 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1662 #ifdef HAVE_QUOTA_SUPPORT
1663 osd_push_ctxt(info->oti_env, save);
1665 inode = ldiskfs_create_inode(oth->ot_handle,
1666 osd_dt_obj(parent)->oo_inode, mode);
1667 #ifdef HAVE_QUOTA_SUPPORT
1670 if (!IS_ERR(inode)) {
1671 /* Do not update file c/mtime in ldiskfs.
1672 * NB: don't need any lock because no contention at this
1674 inode->i_flags |= S_NOCMTIME;
1675 obj->oo_inode = inode;
1678 if (obj->oo_hl_head != NULL) {
1679 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1680 obj->oo_hl_head = NULL;
1682 result = PTR_ERR(inode);
1684 LINVRNT(osd_invariant(obj));
1692 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1693 struct lu_attr *attr,
1694 struct dt_allocation_hint *hint,
1695 struct dt_object_format *dof,
1699 struct osd_thandle *oth;
1700 struct osd_device *osd = osd_obj2dev(obj);
1701 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1703 LASSERT(S_ISDIR(attr->la_mode));
1705 oth = container_of(th, struct osd_thandle, ot_super);
1706 LASSERT(oth->ot_handle->h_transaction != NULL);
1707 result = osd_mkfile(info, obj, mode, hint, th);
1708 if (result == 0 && osd->od_iop_mode == 0) {
1709 LASSERT(obj->oo_inode != NULL);
1711 * XXX uh-oh... call low-level iam function directly.
1714 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1715 sizeof (struct osd_fid_pack),
1721 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1722 struct lu_attr *attr,
1723 struct dt_allocation_hint *hint,
1724 struct dt_object_format *dof,
1728 struct osd_thandle *oth;
1729 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1731 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1733 LASSERT(S_ISREG(attr->la_mode));
1735 oth = container_of(th, struct osd_thandle, ot_super);
1736 LASSERT(oth->ot_handle->h_transaction != NULL);
1738 result = osd_mkfile(info, obj, mode, hint, th);
1740 LASSERT(obj->oo_inode != NULL);
1741 if (feat->dif_flags & DT_IND_VARKEY)
1742 result = iam_lvar_create(obj->oo_inode,
1743 feat->dif_keysize_max,
1745 feat->dif_recsize_max,
1748 result = iam_lfix_create(obj->oo_inode,
1749 feat->dif_keysize_max,
1751 feat->dif_recsize_max,
1758 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1759 struct lu_attr *attr,
1760 struct dt_allocation_hint *hint,
1761 struct dt_object_format *dof,
1764 LASSERT(S_ISREG(attr->la_mode));
1765 return osd_mkfile(info, obj, (attr->la_mode &
1766 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1769 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1770 struct lu_attr *attr,
1771 struct dt_allocation_hint *hint,
1772 struct dt_object_format *dof,
1775 LASSERT(S_ISLNK(attr->la_mode));
1776 return osd_mkfile(info, obj, (attr->la_mode &
1777 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1780 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1781 struct lu_attr *attr,
1782 struct dt_allocation_hint *hint,
1783 struct dt_object_format *dof,
1786 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1789 LINVRNT(osd_invariant(obj));
1790 LASSERT(obj->oo_inode == NULL);
1791 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1792 S_ISFIFO(mode) || S_ISSOCK(mode));
1794 result = osd_mkfile(info, obj, mode, hint, th);
1796 LASSERT(obj->oo_inode != NULL);
1797 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1799 LINVRNT(osd_invariant(obj));
1803 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1805 struct dt_allocation_hint *hint,
1806 struct dt_object_format *dof,
1809 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1811 osd_obj_type_f result;
1827 result = osd_mk_index;
1838 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1839 struct dt_object *parent, cfs_umode_t child_mode)
1843 memset(ah, 0, sizeof(*ah));
1844 ah->dah_parent = parent;
1845 ah->dah_mode = child_mode;
1849 * Helper function for osd_object_create()
1851 * \retval 0, on success
1853 static int __osd_object_create(struct osd_thread_info *info,
1854 struct osd_object *obj, struct lu_attr *attr,
1855 struct dt_allocation_hint *hint,
1856 struct dt_object_format *dof,
1862 result = osd_create_pre(info, obj, attr, th);
1864 result = osd_create_type_f(dof->dof_type)(info, obj,
1865 attr, hint, dof, th);
1867 result = osd_create_post(info, obj, attr, th);
1873 * Helper function for osd_object_create()
1875 * \retval 0, on success
1877 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1878 const struct lu_fid *fid, struct thandle *th)
1880 struct osd_thread_info *info = osd_oti_get(env);
1881 struct osd_inode_id *id = &info->oti_id;
1882 struct osd_device *osd = osd_obj2dev(obj);
1883 struct md_ucred *uc = md_ucred(env);
1885 LASSERT(obj->oo_inode != NULL);
1886 LASSERT(uc != NULL);
1888 id->oii_ino = obj->oo_inode->i_ino;
1889 id->oii_gen = obj->oo_inode->i_generation;
1891 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1892 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1895 static int osd_declare_object_create(const struct lu_env *env,
1896 struct dt_object *dt,
1897 struct lu_attr *attr,
1898 struct dt_allocation_hint *hint,
1899 struct dt_object_format *dof,
1900 struct thandle *handle)
1902 struct osd_thandle *oh;
1904 LASSERT(handle != NULL);
1906 oh = container_of0(handle, struct osd_thandle, ot_super);
1907 LASSERT(oh->ot_handle == NULL);
1909 OSD_DECLARE_OP(oh, insert);
1910 OSD_DECLARE_OP(oh, create);
1911 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1912 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1914 /* if this is directory, then we expect . and ..
1915 * to be inserted as well */
1916 OSD_DECLARE_OP(oh, insert);
1917 OSD_DECLARE_OP(oh, insert);
1918 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1922 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1923 struct lu_attr *attr,
1924 struct dt_allocation_hint *hint,
1925 struct dt_object_format *dof,
1928 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1929 struct osd_object *obj = osd_dt_obj(dt);
1930 struct osd_thread_info *info = osd_oti_get(env);
1935 LINVRNT(osd_invariant(obj));
1936 LASSERT(!dt_object_exists(dt));
1937 LASSERT(osd_write_locked(env, obj));
1938 LASSERT(th != NULL);
1940 OSD_EXEC_OP(th, create);
1942 result = __osd_object_create(info, obj, attr, hint, dof, th);
1944 result = __osd_oi_insert(env, obj, fid, th);
1946 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1947 LASSERT(osd_invariant(obj));
1952 * Called to destroy on-disk representation of the object
1954 * Concurrency: must be locked
1956 static int osd_declare_object_destroy(const struct lu_env *env,
1957 struct dt_object *dt,
1960 struct osd_object *obj = osd_dt_obj(dt);
1961 struct inode *inode = obj->oo_inode;
1962 struct osd_thandle *oh;
1965 oh = container_of0(th, struct osd_thandle, ot_super);
1966 LASSERT(oh->ot_handle == NULL);
1968 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1970 OSD_DECLARE_OP(oh, destroy);
1971 OSD_DECLARE_OP(oh, delete);
1972 oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
1973 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
1978 static int osd_object_destroy(const struct lu_env *env,
1979 struct dt_object *dt,
1982 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1983 struct osd_object *obj = osd_dt_obj(dt);
1984 struct inode *inode = obj->oo_inode;
1985 struct osd_device *osd = osd_obj2dev(obj);
1986 struct osd_thandle *oh;
1990 oh = container_of0(th, struct osd_thandle, ot_super);
1991 LASSERT(oh->ot_handle);
1993 LASSERT(osd_inode_unlinked(inode));
1995 OSD_EXEC_OP(th, destroy);
1997 result = osd_oi_delete(osd_oti_get(env), &osd->od_oi, fid, th);
1999 /* XXX: add to ext3 orphan list */
2000 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2002 /* not needed in the cache anymore */
2003 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2009 * Helper function for osd_xattr_set()
2011 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2012 const struct lu_buf *buf, const char *name, int fl)
2014 struct osd_object *obj = osd_dt_obj(dt);
2015 struct inode *inode = obj->oo_inode;
2016 struct osd_thread_info *info = osd_oti_get(env);
2017 struct dentry *dentry = &info->oti_child_dentry;
2021 LASSERT(dt_object_exists(dt));
2022 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2023 LASSERT(osd_write_locked(env, obj));
2025 if (fl & LU_XATTR_REPLACE)
2026 fs_flags |= XATTR_REPLACE;
2028 if (fl & LU_XATTR_CREATE)
2029 fs_flags |= XATTR_CREATE;
2031 dentry->d_inode = inode;
2032 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2033 buf->lb_len, fs_flags);
2038 * Put the fid into lustre_mdt_attrs, and then place the structure
2039 * inode's ea. This fid should not be altered during the life time
2042 * \retval +ve, on success
2043 * \retval -ve, on error
2045 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2047 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2048 const struct lu_fid *fid)
2050 struct osd_thread_info *info = osd_oti_get(env);
2051 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2053 lustre_lma_init(mdt_attrs, fid);
2054 lustre_lma_swab(mdt_attrs);
2055 return __osd_xattr_set(env, dt,
2056 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2057 XATTR_NAME_LMA, LU_XATTR_CREATE);
2062 * Helper function to form igif
2064 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
2067 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
2071 * Helper function to pack the fid, ldiskfs stores fid in packed format.
2073 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
2074 struct lu_fid *befider)
2076 fid_cpu_to_be(befider, (struct lu_fid *)fid);
2077 memcpy(pack->fp_area, befider, sizeof(*befider));
2078 pack->fp_len = sizeof(*befider) + 1;
2082 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2083 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2084 * To have compatilibility with 1.8 ldiskfs driver we need to have
2085 * magic number at start of fid data.
2086 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2089 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2090 const struct dt_rec *fid)
2092 param->edp_magic = LDISKFS_LUFID_MAGIC;
2093 param->edp_len = sizeof(struct lu_fid) + 1;
2095 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2096 (struct lu_fid *)fid);
2099 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
2104 switch (pack->fp_len) {
2105 case sizeof *fid + 1:
2106 memcpy(fid, pack->fp_area, sizeof *fid);
2107 fid_be_to_cpu(fid, fid);
2110 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
2117 * Try to read the fid from inode ea into dt_rec, if return value
2118 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2120 * \param fid object fid.
2122 * \retval 0 on success
2124 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2125 __u32 ino, struct lu_fid *fid)
2127 struct osd_thread_info *info = osd_oti_get(env);
2128 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2129 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
2130 struct dentry *dentry = &info->oti_child_dentry;
2131 struct osd_inode_id *id = &info->oti_id;
2132 struct osd_device *dev;
2133 struct inode *inode;
2137 dev = osd_dev(ldev);
2140 id->oii_gen = OSD_OII_NOGEN;
2142 inode = osd_iget(info, dev, id);
2143 if (IS_ERR(inode)) {
2144 rc = PTR_ERR(inode);
2147 dentry->d_inode = inode;
2149 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2150 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
2153 /* Check LMA compatibility */
2155 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
2156 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
2157 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
2158 ~LMA_INCOMPAT_SUPP);
2163 lustre_lma_swab(mdt_attrs);
2164 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
2166 } else if (rc == -ENODATA) {
2167 osd_igif_get(env, inode, fid);
2176 * OSD layer object create function for interoperability mode (b11826).
2177 * This is mostly similar to osd_object_create(). Only difference being, fid is
2178 * inserted into inode ea here.
2180 * \retval 0, on success
2181 * \retval -ve, on error
2183 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2184 struct lu_attr *attr,
2185 struct dt_allocation_hint *hint,
2186 struct dt_object_format *dof,
2189 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2190 struct osd_object *obj = osd_dt_obj(dt);
2191 struct osd_thread_info *info = osd_oti_get(env);
2196 LASSERT(osd_invariant(obj));
2197 LASSERT(!dt_object_exists(dt));
2198 LASSERT(osd_write_locked(env, obj));
2199 LASSERT(th != NULL);
2201 OSD_EXEC_OP(th, create);
2203 result = __osd_object_create(info, obj, attr, hint, dof, th);
2205 /* objects under osd root shld have igif fid, so dont add fid EA */
2206 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2207 result = osd_ea_fid_set(env, dt, fid);
2210 result = __osd_oi_insert(env, obj, fid, th);
2212 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2213 LINVRNT(osd_invariant(obj));
2217 static int osd_declare_object_ref_add(const struct lu_env *env,
2218 struct dt_object *dt,
2219 struct thandle *handle)
2221 struct osd_thandle *oh;
2223 /* it's possible that object doesn't exist yet */
2224 LASSERT(handle != NULL);
2226 oh = container_of0(handle, struct osd_thandle, ot_super);
2227 LASSERT(oh->ot_handle == NULL);
2229 OSD_DECLARE_OP(oh, ref_add);
2230 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2236 * Concurrency: @dt is write locked.
2238 static int osd_object_ref_add(const struct lu_env *env,
2239 struct dt_object *dt,
2242 struct osd_object *obj = osd_dt_obj(dt);
2243 struct inode *inode = obj->oo_inode;
2245 LINVRNT(osd_invariant(obj));
2246 LASSERT(dt_object_exists(dt));
2247 LASSERT(osd_write_locked(env, obj));
2248 LASSERT(th != NULL);
2250 OSD_EXEC_OP(th, ref_add);
2252 cfs_spin_lock(&obj->oo_guard);
2253 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
2255 cfs_spin_unlock(&obj->oo_guard);
2256 inode->i_sb->s_op->dirty_inode(inode);
2257 LINVRNT(osd_invariant(obj));
2262 static int osd_declare_object_ref_del(const struct lu_env *env,
2263 struct dt_object *dt,
2264 struct thandle *handle)
2266 struct osd_thandle *oh;
2268 LASSERT(dt_object_exists(dt));
2269 LASSERT(handle != NULL);
2271 oh = container_of0(handle, struct osd_thandle, ot_super);
2272 LASSERT(oh->ot_handle == NULL);
2274 OSD_DECLARE_OP(oh, ref_del);
2275 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2281 * Concurrency: @dt is write locked.
2283 static int osd_object_ref_del(const struct lu_env *env,
2284 struct dt_object *dt,
2287 struct osd_object *obj = osd_dt_obj(dt);
2288 struct inode *inode = obj->oo_inode;
2290 LINVRNT(osd_invariant(obj));
2291 LASSERT(dt_object_exists(dt));
2292 LASSERT(osd_write_locked(env, obj));
2293 LASSERT(th != NULL);
2295 OSD_EXEC_OP(th, ref_del);
2297 cfs_spin_lock(&obj->oo_guard);
2298 LASSERT(inode->i_nlink > 0);
2300 cfs_spin_unlock(&obj->oo_guard);
2301 inode->i_sb->s_op->dirty_inode(inode);
2302 LINVRNT(osd_invariant(obj));
2308 * Get the 64-bit version for an inode.
2310 static int osd_object_version_get(const struct lu_env *env,
2311 struct dt_object *dt, dt_obj_version_t *ver)
2313 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2315 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2316 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2317 *ver = LDISKFS_I(inode)->i_fs_version;
2322 * Concurrency: @dt is read locked.
2324 static int osd_xattr_get(const struct lu_env *env,
2325 struct dt_object *dt,
2328 struct lustre_capa *capa)
2330 struct osd_object *obj = osd_dt_obj(dt);
2331 struct inode *inode = obj->oo_inode;
2332 struct osd_thread_info *info = osd_oti_get(env);
2333 struct dentry *dentry = &info->oti_obj_dentry;
2335 /* version get is not real XATTR but uses xattr API */
2336 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2337 /* for version we are just using xattr API but change inode
2339 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2340 osd_object_version_get(env, dt, buf->lb_buf);
2341 return sizeof(dt_obj_version_t);
2344 LASSERT(dt_object_exists(dt));
2345 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2346 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2348 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2351 dentry->d_inode = inode;
2352 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2356 static int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
2357 const struct lu_buf *buf, const char *name,
2358 int fl, struct thandle *handle)
2360 struct osd_thandle *oh;
2362 LASSERT(handle != NULL);
2364 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2365 /* no credits for version */
2369 oh = container_of0(handle, struct osd_thandle, ot_super);
2370 LASSERT(oh->ot_handle == NULL);
2372 OSD_DECLARE_OP(oh, xattr_set);
2373 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2379 * Set the 64-bit version for object
2381 static void osd_object_version_set(const struct lu_env *env,
2382 struct dt_object *dt,
2383 dt_obj_version_t *new_version)
2385 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2387 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2388 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2390 LDISKFS_I(inode)->i_fs_version = *new_version;
2391 /** Version is set after all inode operations are finished,
2392 * so we should mark it dirty here */
2393 inode->i_sb->s_op->dirty_inode(inode);
2397 * Concurrency: @dt is write locked.
2399 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2400 const struct lu_buf *buf, const char *name, int fl,
2401 struct thandle *handle, struct lustre_capa *capa)
2403 LASSERT(handle != NULL);
2405 /* version set is not real XATTR */
2406 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2407 /* for version we are just using xattr API but change inode
2409 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2410 osd_object_version_set(env, dt, buf->lb_buf);
2411 return sizeof(dt_obj_version_t);
2414 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2417 OSD_EXEC_OP(handle, xattr_set);
2418 return __osd_xattr_set(env, dt, buf, name, fl);
2422 * Concurrency: @dt is read locked.
2424 static int osd_xattr_list(const struct lu_env *env,
2425 struct dt_object *dt,
2427 struct lustre_capa *capa)
2429 struct osd_object *obj = osd_dt_obj(dt);
2430 struct inode *inode = obj->oo_inode;
2431 struct osd_thread_info *info = osd_oti_get(env);
2432 struct dentry *dentry = &info->oti_obj_dentry;
2434 LASSERT(dt_object_exists(dt));
2435 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2436 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2438 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2441 dentry->d_inode = inode;
2442 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2445 static int osd_declare_xattr_del(const struct lu_env *env,
2446 struct dt_object *dt,
2448 struct thandle *handle)
2450 struct osd_thandle *oh;
2452 LASSERT(dt_object_exists(dt));
2453 LASSERT(handle != NULL);
2455 oh = container_of0(handle, struct osd_thandle, ot_super);
2456 LASSERT(oh->ot_handle == NULL);
2458 OSD_DECLARE_OP(oh, xattr_set);
2459 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2465 * Concurrency: @dt is write locked.
2467 static int osd_xattr_del(const struct lu_env *env,
2468 struct dt_object *dt,
2470 struct thandle *handle,
2471 struct lustre_capa *capa)
2473 struct osd_object *obj = osd_dt_obj(dt);
2474 struct inode *inode = obj->oo_inode;
2475 struct osd_thread_info *info = osd_oti_get(env);
2476 struct dentry *dentry = &info->oti_obj_dentry;
2479 LASSERT(dt_object_exists(dt));
2480 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2481 LASSERT(osd_write_locked(env, obj));
2482 LASSERT(handle != NULL);
2484 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2487 OSD_EXEC_OP(handle, xattr_set);
2489 dentry->d_inode = inode;
2490 rc = inode->i_op->removexattr(dentry, name);
2494 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2495 struct dt_object *dt,
2496 struct lustre_capa *old,
2499 struct osd_thread_info *info = osd_oti_get(env);
2500 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2501 struct osd_object *obj = osd_dt_obj(dt);
2502 struct osd_device *dev = osd_obj2dev(obj);
2503 struct lustre_capa_key *key = &info->oti_capa_key;
2504 struct lustre_capa *capa = &info->oti_capa;
2505 struct obd_capa *oc;
2506 struct md_capainfo *ci;
2510 if (!dev->od_fl_capa)
2511 RETURN(ERR_PTR(-ENOENT));
2513 LASSERT(dt_object_exists(dt));
2514 LINVRNT(osd_invariant(obj));
2516 /* renewal sanity check */
2517 if (old && osd_object_auth(env, dt, old, opc))
2518 RETURN(ERR_PTR(-EACCES));
2520 ci = md_capainfo(env);
2522 RETURN(ERR_PTR(-ENOENT));
2524 switch (ci->mc_auth) {
2528 capa->lc_uid = obj->oo_inode->i_uid;
2529 capa->lc_gid = obj->oo_inode->i_gid;
2530 capa->lc_flags = LC_ID_PLAIN;
2532 case LC_ID_CONVERT: {
2535 s[0] = obj->oo_inode->i_uid;
2536 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2537 s[2] = obj->oo_inode->i_gid;
2538 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2539 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2541 RETURN(ERR_PTR(rc));
2543 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2544 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2545 capa->lc_flags = LC_ID_CONVERT;
2549 RETURN(ERR_PTR(-EINVAL));
2552 capa->lc_fid = *fid;
2554 capa->lc_flags |= dev->od_capa_alg << 24;
2555 capa->lc_timeout = dev->od_capa_timeout;
2556 capa->lc_expiry = 0;
2558 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2560 LASSERT(!capa_is_expired(oc));
2564 cfs_spin_lock(&capa_lock);
2565 *key = dev->od_capa_keys[1];
2566 cfs_spin_unlock(&capa_lock);
2568 capa->lc_keyid = key->lk_keyid;
2569 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2571 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2573 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2574 RETURN(ERR_PTR(rc));
2577 oc = capa_add(dev->od_capa_hash, capa);
2581 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2584 struct osd_object *obj = osd_dt_obj(dt);
2585 struct inode *inode = obj->oo_inode;
2586 struct osd_thread_info *info = osd_oti_get(env);
2587 struct dentry *dentry = &info->oti_obj_dentry;
2588 struct file *file = &info->oti_file;
2591 dentry->d_inode = inode;
2592 file->f_dentry = dentry;
2593 file->f_mapping = inode->i_mapping;
2594 file->f_op = inode->i_fop;
2595 LOCK_INODE_MUTEX(inode);
2596 rc = file->f_op->fsync(file, dentry, 0);
2597 UNLOCK_INODE_MUTEX(inode);
2601 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2604 struct osd_object *obj = osd_dt_obj(dt);
2607 *data = (void *)obj->oo_inode;
2615 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2616 const struct dt_index_features *feat)
2618 struct iam_descr *descr;
2620 if (osd_object_is_root(o))
2621 return feat == &dt_directory_features;
2623 LASSERT(o->oo_dir != NULL);
2625 descr = o->oo_dir->od_container.ic_descr;
2626 if (feat == &dt_directory_features) {
2627 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2633 feat->dif_keysize_min <= descr->id_key_size &&
2634 descr->id_key_size <= feat->dif_keysize_max &&
2635 feat->dif_recsize_min <= descr->id_rec_size &&
2636 descr->id_rec_size <= feat->dif_recsize_max &&
2637 !(feat->dif_flags & (DT_IND_VARKEY |
2638 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2639 ergo(feat->dif_flags & DT_IND_UPDATE,
2640 1 /* XXX check that object (and file system) is
2645 static int osd_iam_container_init(const struct lu_env *env,
2646 struct osd_object *obj,
2647 struct osd_directory *dir)
2649 struct iam_container *bag = &dir->od_container;
2652 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2656 result = iam_container_setup(bag);
2660 if (osd_obj2dev(obj)->od_iop_mode) {
2661 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2663 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2669 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2671 iam_container_fini(bag);
2678 * Concurrency: no external locking is necessary.
2680 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2681 const struct dt_index_features *feat)
2685 struct osd_object *obj = osd_dt_obj(dt);
2686 struct osd_device *osd = osd_obj2dev(obj);
2688 LINVRNT(osd_invariant(obj));
2689 LASSERT(dt_object_exists(dt));
2691 if (osd_object_is_root(obj)) {
2692 dt->do_index_ops = &osd_index_ea_ops;
2694 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2695 dt->do_index_ops = &osd_index_ea_ops;
2696 if (S_ISDIR(obj->oo_inode->i_mode))
2701 } else if (!osd_has_index(obj)) {
2702 struct osd_directory *dir;
2707 cfs_spin_lock(&obj->oo_guard);
2708 if (obj->oo_dir == NULL)
2712 * Concurrent thread allocated container data.
2715 cfs_spin_unlock(&obj->oo_guard);
2717 * Now, that we have container data, serialize its
2720 cfs_down_write(&obj->oo_ext_idx_sem);
2722 * recheck under lock.
2724 if (!osd_has_index(obj))
2725 result = osd_iam_container_init(env, obj, dir);
2728 cfs_up_write(&obj->oo_ext_idx_sem);
2736 if (result == 0 && ea_dir == 0) {
2737 if (!osd_iam_index_probe(env, obj, feat))
2740 LINVRNT(osd_invariant(obj));
2745 static const struct dt_object_operations osd_obj_ops = {
2746 .do_read_lock = osd_object_read_lock,
2747 .do_write_lock = osd_object_write_lock,
2748 .do_read_unlock = osd_object_read_unlock,
2749 .do_write_unlock = osd_object_write_unlock,
2750 .do_write_locked = osd_object_write_locked,
2751 .do_attr_get = osd_attr_get,
2752 .do_declare_attr_set = osd_declare_attr_set,
2753 .do_attr_set = osd_attr_set,
2754 .do_ah_init = osd_ah_init,
2755 .do_declare_create = osd_declare_object_create,
2756 .do_create = osd_object_create,
2757 .do_declare_destroy = osd_declare_object_destroy,
2758 .do_destroy = osd_object_destroy,
2759 .do_index_try = osd_index_try,
2760 .do_declare_ref_add = osd_declare_object_ref_add,
2761 .do_ref_add = osd_object_ref_add,
2762 .do_declare_ref_del = osd_declare_object_ref_del,
2763 .do_ref_del = osd_object_ref_del,
2764 .do_xattr_get = osd_xattr_get,
2765 .do_declare_xattr_set = osd_declare_xattr_set,
2766 .do_xattr_set = osd_xattr_set,
2767 .do_declare_xattr_del = osd_declare_xattr_del,
2768 .do_xattr_del = osd_xattr_del,
2769 .do_xattr_list = osd_xattr_list,
2770 .do_capa_get = osd_capa_get,
2771 .do_object_sync = osd_object_sync,
2772 .do_data_get = osd_data_get,
2776 * dt_object_operations for interoperability mode
2777 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2779 static const struct dt_object_operations osd_obj_ea_ops = {
2780 .do_read_lock = osd_object_read_lock,
2781 .do_write_lock = osd_object_write_lock,
2782 .do_read_unlock = osd_object_read_unlock,
2783 .do_write_unlock = osd_object_write_unlock,
2784 .do_write_locked = osd_object_write_locked,
2785 .do_attr_get = osd_attr_get,
2786 .do_declare_attr_set = osd_declare_attr_set,
2787 .do_attr_set = osd_attr_set,
2788 .do_ah_init = osd_ah_init,
2789 .do_declare_create = osd_declare_object_create,
2790 .do_create = osd_object_ea_create,
2791 .do_declare_destroy = osd_declare_object_destroy,
2792 .do_destroy = osd_object_destroy,
2793 .do_index_try = osd_index_try,
2794 .do_declare_ref_add = osd_declare_object_ref_add,
2795 .do_ref_add = osd_object_ref_add,
2796 .do_declare_ref_del = osd_declare_object_ref_del,
2797 .do_ref_del = osd_object_ref_del,
2798 .do_xattr_get = osd_xattr_get,
2799 .do_declare_xattr_set = osd_declare_xattr_set,
2800 .do_xattr_set = osd_xattr_set,
2801 .do_declare_xattr_del = osd_declare_xattr_del,
2802 .do_xattr_del = osd_xattr_del,
2803 .do_xattr_list = osd_xattr_list,
2804 .do_capa_get = osd_capa_get,
2805 .do_object_sync = osd_object_sync,
2806 .do_data_get = osd_data_get,
2814 * XXX: Another layering violation for now.
2816 * We don't want to use ->f_op->read methods, because generic file write
2818 * - serializes on ->i_sem, and
2820 * - does a lot of extra work like balance_dirty_pages(),
2822 * which doesn't work for globally shared files like /last-received.
2824 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2826 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2828 memcpy(buffer, (char*)ei->i_data, buflen);
2833 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2836 struct buffer_head *bh;
2837 unsigned long block;
2844 /* prevent reading after eof */
2845 spin_lock(&inode->i_lock);
2846 if (i_size_read(inode) < *offs + size) {
2847 size = i_size_read(inode) - *offs;
2848 spin_unlock(&inode->i_lock);
2850 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2851 i_size_read(inode), *offs);
2853 } else if (size == 0) {
2857 spin_unlock(&inode->i_lock);
2860 blocksize = 1 << inode->i_blkbits;
2863 block = *offs >> inode->i_blkbits;
2864 boffs = *offs & (blocksize - 1);
2865 csize = min(blocksize - boffs, size);
2866 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2868 CERROR("can't read block: %d\n", err);
2872 memcpy(buf, bh->b_data + boffs, csize);
2882 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2883 struct lu_buf *buf, loff_t *pos,
2884 struct lustre_capa *capa)
2886 struct osd_object *obj = osd_dt_obj(dt);
2887 struct inode *inode = obj->oo_inode;
2890 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2893 /* Read small symlink from inode body as we need to maintain correct
2894 * on-disk symlinks for ldiskfs.
2896 if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2897 (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2898 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2900 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2905 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2908 memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2910 LDISKFS_I(inode)->i_disksize = buflen;
2911 i_size_write(inode, buflen);
2912 inode->i_sb->s_op->dirty_inode(inode);
2917 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2918 loff_t *offs, handle_t *handle)
2920 struct buffer_head *bh = NULL;
2921 loff_t offset = *offs;
2922 loff_t new_size = i_size_read(inode);
2923 unsigned long block;
2924 int blocksize = 1 << inode->i_blkbits;
2928 int dirty_inode = 0;
2930 while (bufsize > 0) {
2934 block = offset >> inode->i_blkbits;
2935 boffs = offset & (blocksize - 1);
2936 size = min(blocksize - boffs, bufsize);
2937 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2939 CERROR("can't read/create block: %d\n", err);
2943 err = ldiskfs_journal_get_write_access(handle, bh);
2945 CERROR("journal_get_write_access() returned error %d\n",
2949 LASSERTF(boffs + size <= bh->b_size,
2950 "boffs %d size %d bh->b_size %lu",
2951 boffs, size, (unsigned long)bh->b_size);
2952 memcpy(bh->b_data + boffs, buf, size);
2953 err = ldiskfs_journal_dirty_metadata(handle, bh);
2957 if (offset + size > new_size)
2958 new_size = offset + size;
2966 /* correct in-core and on-disk sizes */
2967 if (new_size > i_size_read(inode)) {
2968 spin_lock(&inode->i_lock);
2969 if (new_size > i_size_read(inode))
2970 i_size_write(inode, new_size);
2971 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
2972 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
2975 spin_unlock(&inode->i_lock);
2977 inode->i_sb->s_op->dirty_inode(inode);
2985 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
2986 const loff_t size, loff_t pos,
2987 struct thandle *handle)
2989 struct osd_thandle *oh;
2991 LASSERT(handle != NULL);
2993 oh = container_of0(handle, struct osd_thandle, ot_super);
2994 LASSERT(oh->ot_handle == NULL);
2996 OSD_DECLARE_OP(oh, write);
2997 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BLOCK];
3002 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
3003 const struct lu_buf *buf, loff_t *pos,
3004 struct thandle *handle, struct lustre_capa *capa,
3007 struct osd_object *obj = osd_dt_obj(dt);
3008 struct inode *inode = obj->oo_inode;
3009 struct osd_thandle *oh;
3011 #ifdef HAVE_QUOTA_SUPPORT
3012 cfs_cap_t save = cfs_curproc_cap_pack();
3015 LASSERT(handle != NULL);
3017 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
3020 oh = container_of(handle, struct osd_thandle, ot_super);
3021 LASSERT(oh->ot_handle->h_transaction != NULL);
3022 #ifdef HAVE_QUOTA_SUPPORT
3024 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3026 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3028 /* Write small symlink to inode body as we need to maintain correct
3029 * on-disk symlinks for ldiskfs.
3031 if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
3032 (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
3033 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
3035 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
3038 #ifdef HAVE_QUOTA_SUPPORT
3039 cfs_curproc_cap_unpack(save);
3042 result = buf->lb_len;
3047 * in some cases we may need declare methods for objects being created
3048 * e.g., when we create symlink
3050 static const struct dt_body_operations osd_body_ops_new = {
3051 .dbo_declare_write = osd_declare_write,
3054 static const struct dt_body_operations osd_body_ops = {
3055 .dbo_read = osd_read,
3056 .dbo_declare_write = osd_declare_write,
3057 .dbo_write = osd_write
3060 static int osd_index_declare_iam_delete(const struct lu_env *env,
3061 struct dt_object *dt,
3062 const struct dt_key *key,
3063 struct thandle *handle)
3065 struct osd_thandle *oh;
3067 oh = container_of0(handle, struct osd_thandle, ot_super);
3068 LASSERT(oh->ot_handle == NULL);
3070 OSD_DECLARE_OP(oh, delete);
3071 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3077 * delete a (key, value) pair from index \a dt specified by \a key
3079 * \param dt osd index object
3080 * \param key key for index
3081 * \param rec record reference
3082 * \param handle transaction handler
3085 * \retval -ve failure
3088 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3089 const struct dt_key *key, struct thandle *handle,
3090 struct lustre_capa *capa)
3092 struct osd_object *obj = osd_dt_obj(dt);
3093 struct osd_thandle *oh;
3094 struct iam_path_descr *ipd;
3095 struct iam_container *bag = &obj->oo_dir->od_container;
3100 LINVRNT(osd_invariant(obj));
3101 LASSERT(dt_object_exists(dt));
3102 LASSERT(bag->ic_object == obj->oo_inode);
3103 LASSERT(handle != NULL);
3105 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3108 OSD_EXEC_OP(handle, delete);
3110 ipd = osd_idx_ipd_get(env, bag);
3111 if (unlikely(ipd == NULL))
3114 oh = container_of0(handle, struct osd_thandle, ot_super);
3115 LASSERT(oh->ot_handle != NULL);
3116 LASSERT(oh->ot_handle->h_transaction != NULL);
3118 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3119 osd_ipd_put(env, bag, ipd);
3120 LINVRNT(osd_invariant(obj));
3124 static int osd_index_declare_ea_delete(const struct lu_env *env,
3125 struct dt_object *dt,
3126 const struct dt_key *key,
3127 struct thandle *handle)
3129 struct osd_thandle *oh;
3131 LASSERT(dt_object_exists(dt));
3132 LASSERT(handle != NULL);
3134 oh = container_of0(handle, struct osd_thandle, ot_super);
3135 LASSERT(oh->ot_handle == NULL);
3137 OSD_DECLARE_OP(oh, delete);
3138 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3143 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3146 struct osd_fid_pack *rec;
3149 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3150 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3151 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3157 * Index delete function for interoperability mode (b11826).
3158 * It will remove the directory entry added by osd_index_ea_insert().
3159 * This entry is needed to maintain name->fid mapping.
3161 * \param key, key i.e. file entry to be deleted
3163 * \retval 0, on success
3164 * \retval -ve, on error
3166 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3167 const struct dt_key *key, struct thandle *handle,
3168 struct lustre_capa *capa)
3170 struct osd_object *obj = osd_dt_obj(dt);
3171 struct inode *dir = obj->oo_inode;
3172 struct dentry *dentry;
3173 struct osd_thandle *oh;
3174 struct ldiskfs_dir_entry_2 *de;
3175 struct buffer_head *bh;
3176 struct htree_lock *hlock = NULL;
3182 LINVRNT(osd_invariant(obj));
3183 LASSERT(dt_object_exists(dt));
3184 LASSERT(handle != NULL);
3186 OSD_EXEC_OP(handle, delete);
3188 oh = container_of(handle, struct osd_thandle, ot_super);
3189 LASSERT(oh->ot_handle != NULL);
3190 LASSERT(oh->ot_handle->h_transaction != NULL);
3192 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3195 dentry = osd_child_dentry_get(env, obj,
3196 (char *)key, strlen((char *)key));
3198 if (obj->oo_hl_head != NULL) {
3199 hlock = osd_oti_get(env)->oti_hlock;
3200 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3201 dir, LDISKFS_HLOCK_DEL);
3203 cfs_down_write(&obj->oo_ext_idx_sem);
3206 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3208 rc = ldiskfs_delete_entry(oh->ot_handle,
3215 ldiskfs_htree_unlock(hlock);
3217 cfs_up_write(&obj->oo_ext_idx_sem);
3219 LASSERT(osd_invariant(obj));
3224 * Lookup index for \a key and copy record to \a rec.
3226 * \param dt osd index object
3227 * \param key key for index
3228 * \param rec record reference
3230 * \retval +ve success : exact mach
3231 * \retval 0 return record with key not greater than \a key
3232 * \retval -ve failure
3234 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3235 struct dt_rec *rec, const struct dt_key *key,
3236 struct lustre_capa *capa)
3238 struct osd_object *obj = osd_dt_obj(dt);
3239 struct iam_path_descr *ipd;
3240 struct iam_container *bag = &obj->oo_dir->od_container;
3241 struct osd_thread_info *oti = osd_oti_get(env);
3242 struct iam_iterator *it = &oti->oti_idx_it;
3243 struct iam_rec *iam_rec;
3247 LASSERT(osd_invariant(obj));
3248 LASSERT(dt_object_exists(dt));
3249 LASSERT(bag->ic_object == obj->oo_inode);
3251 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3254 ipd = osd_idx_ipd_get(env, bag);
3258 /* got ipd now we can start iterator. */
3259 iam_it_init(it, bag, 0, ipd);
3261 rc = iam_it_get(it, (struct iam_key *)key);
3263 if (S_ISDIR(obj->oo_inode->i_mode))
3264 iam_rec = (struct iam_rec *)oti->oti_ldp;
3266 iam_rec = (struct iam_rec *) rec;
3268 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3269 if (S_ISDIR(obj->oo_inode->i_mode))
3270 osd_fid_unpack((struct lu_fid *) rec,
3271 (struct osd_fid_pack *)iam_rec);
3275 osd_ipd_put(env, bag, ipd);
3277 LINVRNT(osd_invariant(obj));
3282 static int osd_index_declare_iam_insert(const struct lu_env *env,
3283 struct dt_object *dt,
3284 const struct dt_rec *rec,
3285 const struct dt_key *key,
3286 struct thandle *handle)
3288 struct osd_thandle *oh;
3290 LASSERT(dt_object_exists(dt));
3291 LASSERT(handle != NULL);
3293 oh = container_of0(handle, struct osd_thandle, ot_super);
3294 LASSERT(oh->ot_handle == NULL);
3296 OSD_DECLARE_OP(oh, insert);
3297 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3303 * Inserts (key, value) pair in \a dt index object.
3305 * \param dt osd index object
3306 * \param key key for index
3307 * \param rec record reference
3308 * \param th transaction handler
3311 * \retval -ve failure
3313 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3314 const struct dt_rec *rec, const struct dt_key *key,
3315 struct thandle *th, struct lustre_capa *capa,
3318 struct osd_object *obj = osd_dt_obj(dt);
3319 struct iam_path_descr *ipd;
3320 struct osd_thandle *oh;
3321 struct iam_container *bag = &obj->oo_dir->od_container;
3322 #ifdef HAVE_QUOTA_SUPPORT
3323 cfs_cap_t save = cfs_curproc_cap_pack();
3325 struct osd_thread_info *oti = osd_oti_get(env);
3326 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3331 LINVRNT(osd_invariant(obj));
3332 LASSERT(dt_object_exists(dt));
3333 LASSERT(bag->ic_object == obj->oo_inode);
3334 LASSERT(th != NULL);
3336 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3339 OSD_EXEC_OP(th, insert);
3341 ipd = osd_idx_ipd_get(env, bag);
3342 if (unlikely(ipd == NULL))
3345 oh = container_of0(th, struct osd_thandle, ot_super);
3346 LASSERT(oh->ot_handle != NULL);
3347 LASSERT(oh->ot_handle->h_transaction != NULL);
3348 #ifdef HAVE_QUOTA_SUPPORT
3350 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3352 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3354 if (S_ISDIR(obj->oo_inode->i_mode))
3355 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3357 iam_rec = (struct iam_rec *) rec;
3358 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3360 #ifdef HAVE_QUOTA_SUPPORT
3361 cfs_curproc_cap_unpack(save);
3363 osd_ipd_put(env, bag, ipd);
3364 LINVRNT(osd_invariant(obj));
3369 * Calls ldiskfs_add_entry() to add directory entry
3370 * into the directory. This is required for
3371 * interoperability mode (b11826)
3373 * \retval 0, on success
3374 * \retval -ve, on error
3376 static int __osd_ea_add_rec(struct osd_thread_info *info,
3377 struct osd_object *pobj,
3378 struct inode *cinode,
3380 const struct dt_rec *fid,
3381 struct htree_lock *hlock,
3384 struct ldiskfs_dentry_param *ldp;
3385 struct dentry *child;
3386 struct osd_thandle *oth;
3389 oth = container_of(th, struct osd_thandle, ot_super);
3390 LASSERT(oth->ot_handle != NULL);
3391 LASSERT(oth->ot_handle->h_transaction != NULL);
3393 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3395 if (fid_is_igif((struct lu_fid *)fid) ||
3396 fid_is_norm((struct lu_fid *)fid)) {
3397 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3398 osd_get_ldiskfs_dirent_param(ldp, fid);
3399 child->d_fsdata = (void*) ldp;
3401 child->d_fsdata = NULL;
3402 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3408 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3409 * into the directory.Also sets flags into osd object to
3410 * indicate dot and dotdot are created. This is required for
3411 * interoperability mode (b11826)
3413 * \param dir directory for dot and dotdot fixup.
3414 * \param obj child object for linking
3416 * \retval 0, on success
3417 * \retval -ve, on error
3419 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3420 struct osd_object *dir,
3421 struct inode *parent_dir, const char *name,
3422 const struct dt_rec *dot_fid,
3423 const struct dt_rec *dot_dot_fid,
3426 struct inode *inode = dir->oo_inode;
3427 struct ldiskfs_dentry_param *dot_ldp;
3428 struct ldiskfs_dentry_param *dot_dot_ldp;
3429 struct osd_thandle *oth;
3432 oth = container_of(th, struct osd_thandle, ot_super);
3433 LASSERT(oth->ot_handle->h_transaction != NULL);
3434 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3436 if (strcmp(name, dot) == 0) {
3437 if (dir->oo_compat_dot_created) {
3440 LASSERT(inode == parent_dir);
3441 dir->oo_compat_dot_created = 1;
3444 } else if(strcmp(name, dotdot) == 0) {
3445 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3446 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3448 if (!dir->oo_compat_dot_created)
3450 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
3451 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3452 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3457 /* in case of rename, dotdot is already created */
3458 if (dir->oo_compat_dotdot_created) {
3459 return __osd_ea_add_rec(info, dir, parent_dir, name,
3460 dot_dot_fid, NULL, th);
3463 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3464 inode, dot_ldp, dot_dot_ldp);
3466 dir->oo_compat_dotdot_created = 1;
3474 * It will call the appropriate osd_add* function and return the
3475 * value, return by respective functions.
3477 static int osd_ea_add_rec(const struct lu_env *env,
3478 struct osd_object *pobj,
3479 struct inode *cinode,
3481 const struct dt_rec *fid,
3484 struct osd_thread_info *info = osd_oti_get(env);
3485 struct htree_lock *hlock;
3488 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3490 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3492 if (hlock != NULL) {
3493 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3496 cfs_down_write(&pobj->oo_ext_idx_sem);
3498 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3499 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3502 if (hlock != NULL) {
3503 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3504 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3506 cfs_down_write(&pobj->oo_ext_idx_sem);
3509 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3513 ldiskfs_htree_unlock(hlock);
3515 cfs_up_write(&pobj->oo_ext_idx_sem);
3521 * Calls ->lookup() to find dentry. From dentry get inode and
3522 * read inode's ea to get fid. This is required for interoperability
3525 * \retval 0, on success
3526 * \retval -ve, on error
3528 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3529 struct dt_rec *rec, const struct dt_key *key)
3531 struct inode *dir = obj->oo_inode;
3532 struct dentry *dentry;
3533 struct ldiskfs_dir_entry_2 *de;
3534 struct buffer_head *bh;
3535 struct lu_fid *fid = (struct lu_fid *) rec;
3536 struct htree_lock *hlock = NULL;
3540 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3542 dentry = osd_child_dentry_get(env, obj,
3543 (char *)key, strlen((char *)key));
3545 if (obj->oo_hl_head != NULL) {
3546 hlock = osd_oti_get(env)->oti_hlock;
3547 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3548 dir, LDISKFS_HLOCK_LOOKUP);
3550 cfs_down_read(&obj->oo_ext_idx_sem);
3553 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3555 ino = le32_to_cpu(de->inode);
3556 rc = osd_get_fid_from_dentry(de, rec);
3558 /* done with de, release bh */
3561 rc = osd_ea_fid_get(env, obj, ino, fid);
3567 ldiskfs_htree_unlock(hlock);
3569 cfs_up_read(&obj->oo_ext_idx_sem);
3574 * Find the osd object for given fid.
3576 * \param fid need to find the osd object having this fid
3578 * \retval osd_object on success
3579 * \retval -ve on error
3581 struct osd_object *osd_object_find(const struct lu_env *env,
3582 struct dt_object *dt,
3583 const struct lu_fid *fid)
3585 struct lu_device *ludev = dt->do_lu.lo_dev;
3586 struct osd_object *child = NULL;
3587 struct lu_object *luch;
3588 struct lu_object *lo;
3590 luch = lu_object_find(env, ludev, fid, NULL);
3591 if (!IS_ERR(luch)) {
3592 if (lu_object_exists(luch)) {
3593 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3595 child = osd_obj(lo);
3597 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3598 "lu_object can't be located"
3599 ""DFID"\n", PFID(fid));
3601 if (child == NULL) {
3602 lu_object_put(env, luch);
3603 CERROR("Unable to get osd_object\n");
3604 child = ERR_PTR(-ENOENT);
3607 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3608 "lu_object does not exists "DFID"\n",
3610 child = ERR_PTR(-ENOENT);
3613 child = (void *)luch;
3619 * Put the osd object once done with it.
3621 * \param obj osd object that needs to be put
3623 static inline void osd_object_put(const struct lu_env *env,
3624 struct osd_object *obj)
3626 lu_object_put(env, &obj->oo_dt.do_lu);
3629 static int osd_index_declare_ea_insert(const struct lu_env *env,
3630 struct dt_object *dt,
3631 const struct dt_rec *rec,
3632 const struct dt_key *key,
3633 struct thandle *handle)
3635 struct osd_thandle *oh;
3637 LASSERT(dt_object_exists(dt));
3638 LASSERT(handle != NULL);
3640 oh = container_of0(handle, struct osd_thandle, ot_super);
3641 LASSERT(oh->ot_handle == NULL);
3643 OSD_DECLARE_OP(oh, insert);
3644 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3650 * Index add function for interoperability mode (b11826).
3651 * It will add the directory entry.This entry is needed to
3652 * maintain name->fid mapping.
3654 * \param key it is key i.e. file entry to be inserted
3655 * \param rec it is value of given key i.e. fid
3657 * \retval 0, on success
3658 * \retval -ve, on error
3660 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3661 const struct dt_rec *rec,
3662 const struct dt_key *key, struct thandle *th,
3663 struct lustre_capa *capa, int ignore_quota)
3665 struct osd_object *obj = osd_dt_obj(dt);
3666 struct lu_fid *fid = (struct lu_fid *) rec;
3667 const char *name = (const char *)key;
3668 struct osd_object *child;
3669 #ifdef HAVE_QUOTA_SUPPORT
3670 cfs_cap_t save = cfs_curproc_cap_pack();
3676 LASSERT(osd_invariant(obj));
3677 LASSERT(dt_object_exists(dt));
3678 LASSERT(th != NULL);
3680 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3683 child = osd_object_find(env, dt, fid);
3684 if (!IS_ERR(child)) {
3685 #ifdef HAVE_QUOTA_SUPPORT
3687 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3689 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3691 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3692 #ifdef HAVE_QUOTA_SUPPORT
3693 cfs_curproc_cap_unpack(save);
3695 osd_object_put(env, child);
3697 rc = PTR_ERR(child);
3700 LASSERT(osd_invariant(obj));
3705 * Initialize osd Iterator for given osd index object.
3707 * \param dt osd index object
3710 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3711 struct dt_object *dt,
3713 struct lustre_capa *capa)
3715 struct osd_it_iam *it;
3716 struct osd_thread_info *oti = osd_oti_get(env);
3717 struct osd_object *obj = osd_dt_obj(dt);
3718 struct lu_object *lo = &dt->do_lu;
3719 struct iam_path_descr *ipd;
3720 struct iam_container *bag = &obj->oo_dir->od_container;
3722 LASSERT(lu_object_exists(lo));
3724 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3725 return ERR_PTR(-EACCES);
3728 ipd = osd_it_ipd_get(env, bag);
3729 if (likely(ipd != NULL)) {
3733 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3734 return (struct dt_it *)it;
3736 return ERR_PTR(-ENOMEM);
3740 * free given Iterator.
3743 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3745 struct osd_it_iam *it = (struct osd_it_iam *)di;
3746 struct osd_object *obj = it->oi_obj;
3748 iam_it_fini(&it->oi_it);
3749 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3750 lu_object_put(env, &obj->oo_dt.do_lu);
3754 * Move Iterator to record specified by \a key
3756 * \param di osd iterator
3757 * \param key key for index
3759 * \retval +ve di points to record with least key not larger than key
3760 * \retval 0 di points to exact matched key
3761 * \retval -ve failure
3764 static int osd_it_iam_get(const struct lu_env *env,
3765 struct dt_it *di, const struct dt_key *key)
3767 struct osd_it_iam *it = (struct osd_it_iam *)di;
3769 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3775 * \param di osd iterator
3778 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3780 struct osd_it_iam *it = (struct osd_it_iam *)di;
3782 iam_it_put(&it->oi_it);
3786 * Move iterator by one record
3788 * \param di osd iterator
3790 * \retval +1 end of container reached
3792 * \retval -ve failure
3795 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3797 struct osd_it_iam *it = (struct osd_it_iam *)di;
3799 return iam_it_next(&it->oi_it);
3803 * Return pointer to the key under iterator.
3806 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3807 const struct dt_it *di)
3809 struct osd_it_iam *it = (struct osd_it_iam *)di;
3811 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3815 * Return size of key under iterator (in bytes)
3818 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3820 struct osd_it_iam *it = (struct osd_it_iam *)di;
3822 return iam_it_key_size(&it->oi_it);
3825 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3830 struct luda_type *lt;
3831 const unsigned align = sizeof(struct luda_type) - 1;
3833 /* check if file type is required */
3834 if (attr & LUDA_TYPE) {
3835 len = (len + align) & ~align;
3837 lt = (void *) ent->lde_name + len;
3838 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3839 ent->lde_attrs |= LUDA_TYPE;
3842 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3846 * build lu direct from backend fs dirent.
3849 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3857 fid_cpu_to_le(&ent->lde_fid, fid);
3858 ent->lde_attrs = LUDA_FID;
3860 ent->lde_hash = cpu_to_le64(offset);
3861 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3863 strncpy(ent->lde_name, name, namelen);
3864 ent->lde_namelen = cpu_to_le16(namelen);
3866 /* append lustre attributes */
3867 osd_it_append_attrs(ent, attr, namelen, type);
3871 * Return pointer to the record under iterator.
3873 static int osd_it_iam_rec(const struct lu_env *env,
3874 const struct dt_it *di,
3875 struct dt_rec *dtrec,
3878 struct osd_it_iam *it = (struct osd_it_iam *)di;
3879 struct osd_thread_info *info = osd_oti_get(env);
3880 struct lu_fid *fid = &info->oti_fid;
3881 const struct osd_fid_pack *rec;
3882 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3888 name = (char *)iam_it_key_get(&it->oi_it);
3890 RETURN(PTR_ERR(name));
3892 namelen = iam_it_key_size(&it->oi_it);
3894 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3896 RETURN(PTR_ERR(rec));
3898 rc = osd_fid_unpack(fid, rec);
3902 hash = iam_it_store(&it->oi_it);
3904 /* IAM does not store object type in IAM index (dir) */
3905 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3912 * Returns cookie for current Iterator position.
3914 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3916 struct osd_it_iam *it = (struct osd_it_iam *)di;
3918 return iam_it_store(&it->oi_it);
3922 * Restore iterator from cookie.
3924 * \param di osd iterator
3925 * \param hash Iterator location cookie
3927 * \retval +ve di points to record with least key not larger than key.
3928 * \retval 0 di points to exact matched key
3929 * \retval -ve failure
3932 static int osd_it_iam_load(const struct lu_env *env,
3933 const struct dt_it *di, __u64 hash)
3935 struct osd_it_iam *it = (struct osd_it_iam *)di;
3937 return iam_it_load(&it->oi_it, hash);
3940 static const struct dt_index_operations osd_index_iam_ops = {
3941 .dio_lookup = osd_index_iam_lookup,
3942 .dio_declare_insert = osd_index_declare_iam_insert,
3943 .dio_insert = osd_index_iam_insert,
3944 .dio_declare_delete = osd_index_declare_iam_delete,
3945 .dio_delete = osd_index_iam_delete,
3947 .init = osd_it_iam_init,
3948 .fini = osd_it_iam_fini,
3949 .get = osd_it_iam_get,
3950 .put = osd_it_iam_put,
3951 .next = osd_it_iam_next,
3952 .key = osd_it_iam_key,
3953 .key_size = osd_it_iam_key_size,
3954 .rec = osd_it_iam_rec,
3955 .store = osd_it_iam_store,
3956 .load = osd_it_iam_load
3961 * Creates or initializes iterator context.
3963 * \retval struct osd_it_ea, iterator structure on success
3966 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3967 struct dt_object *dt,
3969 struct lustre_capa *capa)
3971 struct osd_object *obj = osd_dt_obj(dt);
3972 struct osd_thread_info *info = osd_oti_get(env);
3973 struct osd_it_ea *it = &info->oti_it_ea;
3974 struct lu_object *lo = &dt->do_lu;
3975 struct dentry *obj_dentry = &info->oti_it_dentry;
3977 LASSERT(lu_object_exists(lo));
3979 obj_dentry->d_inode = obj->oo_inode;
3980 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3981 obj_dentry->d_name.hash = 0;
3983 it->oie_rd_dirent = 0;
3984 it->oie_it_dirent = 0;
3985 it->oie_dirent = NULL;
3986 it->oie_buf = info->oti_it_ea_buf;
3988 it->oie_file.f_pos = 0;
3989 it->oie_file.f_dentry = obj_dentry;
3990 if (attr & LUDA_64BITHASH)
3991 it->oie_file.f_flags = O_64BITHASH;
3993 it->oie_file.f_flags = O_32BITHASH;
3994 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3995 it->oie_file.f_op = obj->oo_inode->i_fop;
3996 it->oie_file.private_data = NULL;
3998 RETURN((struct dt_it *) it);
4002 * Destroy or finishes iterator context.
4004 * \param di iterator structure to be destroyed
4006 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4008 struct osd_it_ea *it = (struct osd_it_ea *)di;
4009 struct osd_object *obj = it->oie_obj;
4010 struct inode *inode = obj->oo_inode;
4013 it->oie_file.f_op->release(inode, &it->oie_file);
4014 lu_object_put(env, &obj->oo_dt.do_lu);
4019 * It position the iterator at given key, so that next lookup continues from
4020 * that key Or it is similar to dio_it->load() but based on a key,
4021 * rather than file position.
4023 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4026 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4028 static int osd_it_ea_get(const struct lu_env *env,
4029 struct dt_it *di, const struct dt_key *key)
4031 struct osd_it_ea *it = (struct osd_it_ea *)di;
4034 LASSERT(((const char *)key)[0] == '\0');
4035 it->oie_file.f_pos = 0;
4036 it->oie_rd_dirent = 0;
4037 it->oie_it_dirent = 0;
4038 it->oie_dirent = NULL;
4046 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4051 * It is called internally by ->readdir(). It fills the
4052 * iterator's in-memory data structure with required
4053 * information i.e. name, namelen, rec_size etc.
4055 * \param buf in which information to be filled in.
4056 * \param name name of the file in given dir
4058 * \retval 0 on success
4059 * \retval 1 on buffer full
4061 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4062 loff_t offset, __u64 ino,
4065 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4066 struct osd_it_ea_dirent *ent = it->oie_dirent;
4067 struct lu_fid *fid = &ent->oied_fid;
4068 struct osd_fid_pack *rec;
4071 /* this should never happen */
4072 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4073 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4077 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4081 if (d_type & LDISKFS_DIRENT_LUFID) {
4082 rec = (struct osd_fid_pack*) (name + namelen + 1);
4084 if (osd_fid_unpack(fid, rec) != 0)
4087 d_type &= ~LDISKFS_DIRENT_LUFID;
4092 ent->oied_ino = ino;
4093 ent->oied_off = offset;
4094 ent->oied_namelen = namelen;
4095 ent->oied_type = d_type;
4097 memcpy(ent->oied_name, name, namelen);
4099 it->oie_rd_dirent++;
4100 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4105 * Calls ->readdir() to load a directory entry at a time
4106 * and stored it in iterator's in-memory data structure.
4108 * \param di iterator's in memory structure
4110 * \retval 0 on success
4111 * \retval -ve on error
4113 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4114 const struct dt_it *di)
4116 struct osd_it_ea *it = (struct osd_it_ea *)di;
4117 struct osd_object *obj = it->oie_obj;
4118 struct inode *inode = obj->oo_inode;
4119 struct htree_lock *hlock = NULL;
4123 it->oie_dirent = it->oie_buf;
4124 it->oie_rd_dirent = 0;
4126 if (obj->oo_hl_head != NULL) {
4127 hlock = osd_oti_get(env)->oti_hlock;
4128 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4129 inode, LDISKFS_HLOCK_READDIR);
4131 cfs_down_read(&obj->oo_ext_idx_sem);
4134 result = inode->i_fop->readdir(&it->oie_file, it,
4135 (filldir_t) osd_ldiskfs_filldir);
4138 ldiskfs_htree_unlock(hlock);
4140 cfs_up_read(&obj->oo_ext_idx_sem);
4142 if (it->oie_rd_dirent == 0) {
4145 it->oie_dirent = it->oie_buf;
4146 it->oie_it_dirent = 1;
4153 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4154 * to load a directory entry at a time and stored it in
4155 * iterator's in-memory data structure.
4157 * \param di iterator's in memory structure
4159 * \retval +ve iterator reached to end
4160 * \retval 0 iterator not reached to end
4161 * \retval -ve on error
4163 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4165 struct osd_it_ea *it = (struct osd_it_ea *)di;
4170 if (it->oie_it_dirent < it->oie_rd_dirent) {
4172 (void *) it->oie_dirent +
4173 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4174 it->oie_dirent->oied_namelen);
4175 it->oie_it_dirent++;
4178 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
4181 rc = osd_ldiskfs_it_fill(env, di);
4188 * Returns the key at current position from iterator's in memory structure.
4190 * \param di iterator's in memory structure
4192 * \retval key i.e. struct dt_key on success
4194 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4195 const struct dt_it *di)
4197 struct osd_it_ea *it = (struct osd_it_ea *)di;
4199 RETURN((struct dt_key *)it->oie_dirent->oied_name);
4203 * Returns the key's size at current position from iterator's in memory structure.
4205 * \param di iterator's in memory structure
4207 * \retval key_size i.e. struct dt_key on success
4209 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4211 struct osd_it_ea *it = (struct osd_it_ea *)di;
4213 RETURN(it->oie_dirent->oied_namelen);
4218 * Returns the value (i.e. fid/igif) at current position from iterator's
4219 * in memory structure.
4221 * \param di struct osd_it_ea, iterator's in memory structure
4222 * \param attr attr requested for dirent.
4223 * \param lde lustre dirent
4225 * \retval 0 no error and \param lde has correct lustre dirent.
4226 * \retval -ve on error
4228 static inline int osd_it_ea_rec(const struct lu_env *env,
4229 const struct dt_it *di,
4230 struct dt_rec *dtrec,
4233 struct osd_it_ea *it = (struct osd_it_ea *)di;
4234 struct osd_object *obj = it->oie_obj;
4235 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4236 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4241 if (!fid_is_sane(fid))
4242 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
4245 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4246 it->oie_dirent->oied_name,
4247 it->oie_dirent->oied_namelen,
4248 it->oie_dirent->oied_type,
4254 * Returns a cookie for current position of the iterator head, so that
4255 * user can use this cookie to load/start the iterator next time.
4257 * \param di iterator's in memory structure
4259 * \retval cookie for current position, on success
4261 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4263 struct osd_it_ea *it = (struct osd_it_ea *)di;
4265 RETURN(it->oie_dirent->oied_off);
4269 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4270 * to load a directory entry at a time and stored it i inn,
4271 * in iterator's in-memory data structure.
4273 * \param di struct osd_it_ea, iterator's in memory structure
4275 * \retval +ve on success
4276 * \retval -ve on error
4278 static int osd_it_ea_load(const struct lu_env *env,
4279 const struct dt_it *di, __u64 hash)
4281 struct osd_it_ea *it = (struct osd_it_ea *)di;
4285 it->oie_file.f_pos = hash;
4287 rc = osd_ldiskfs_it_fill(env, di);
4295 * Index lookup function for interoperability mode (b11826).
4297 * \param key, key i.e. file name to be searched
4299 * \retval +ve, on success
4300 * \retval -ve, on error
4302 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4303 struct dt_rec *rec, const struct dt_key *key,
4304 struct lustre_capa *capa)
4306 struct osd_object *obj = osd_dt_obj(dt);
4311 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4312 LINVRNT(osd_invariant(obj));
4314 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4317 rc = osd_ea_lookup_rec(env, obj, rec, key);
4325 * Index and Iterator operations for interoperability
4326 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4328 static const struct dt_index_operations osd_index_ea_ops = {
4329 .dio_lookup = osd_index_ea_lookup,
4330 .dio_declare_insert = osd_index_declare_ea_insert,
4331 .dio_insert = osd_index_ea_insert,
4332 .dio_declare_delete = osd_index_declare_ea_delete,
4333 .dio_delete = osd_index_ea_delete,
4335 .init = osd_it_ea_init,
4336 .fini = osd_it_ea_fini,
4337 .get = osd_it_ea_get,
4338 .put = osd_it_ea_put,
4339 .next = osd_it_ea_next,
4340 .key = osd_it_ea_key,
4341 .key_size = osd_it_ea_key_size,
4342 .rec = osd_it_ea_rec,
4343 .store = osd_it_ea_store,
4344 .load = osd_it_ea_load
4348 static void *osd_key_init(const struct lu_context *ctx,
4349 struct lu_context_key *key)
4351 struct osd_thread_info *info;
4353 OBD_ALLOC_PTR(info);
4355 return ERR_PTR(-ENOMEM);
4357 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4358 if (info->oti_it_ea_buf == NULL)
4361 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4363 info->oti_hlock = ldiskfs_htree_lock_alloc();
4364 if (info->oti_hlock == NULL)
4370 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4373 return ERR_PTR(-ENOMEM);
4376 static void osd_key_fini(const struct lu_context *ctx,
4377 struct lu_context_key *key, void* data)
4379 struct osd_thread_info *info = data;
4381 if (info->oti_hlock != NULL)
4382 ldiskfs_htree_lock_free(info->oti_hlock);
4383 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4387 static void osd_key_exit(const struct lu_context *ctx,
4388 struct lu_context_key *key, void *data)
4390 struct osd_thread_info *info = data;
4392 LASSERT(info->oti_r_locks == 0);
4393 LASSERT(info->oti_w_locks == 0);
4394 LASSERT(info->oti_txns == 0);
4397 /* type constructor/destructor: osd_type_init, osd_type_fini */
4398 LU_TYPE_INIT_FINI(osd, &osd_key);
4400 static struct lu_context_key osd_key = {
4401 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
4402 .lct_init = osd_key_init,
4403 .lct_fini = osd_key_fini,
4404 .lct_exit = osd_key_exit
4408 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4409 const char *name, struct lu_device *next)
4411 return osd_procfs_init(osd_dev(d), name);
4414 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4416 struct osd_thread_info *info = osd_oti_get(env);
4418 if (o->od_obj_area != NULL) {
4419 lu_object_put(env, &o->od_obj_area->do_lu);
4420 o->od_obj_area = NULL;
4422 osd_oi_fini(info, &o->od_oi);
4427 static int osd_mount(const struct lu_env *env,
4428 struct osd_device *o, struct lustre_cfg *cfg)
4430 struct lustre_mount_info *lmi;
4431 const char *dev = lustre_cfg_string(cfg, 0);
4432 struct lustre_disk_data *ldd;
4433 struct lustre_sb_info *lsi;
4436 if (o->od_mount != NULL) {
4437 CERROR("Already mounted (%s)\n", dev);
4442 lmi = server_get_mount(dev);
4444 CERROR("Cannot get mount info for %s!\n", dev);
4448 LASSERT(lmi != NULL);
4449 /* save lustre_mount_info in dt_device */
4452 lsi = s2lsi(lmi->lmi_sb);
4455 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4457 LCONSOLE_WARN("OSD: IAM mode enabled\n");
4461 o->od_obj_area = NULL;
4465 static struct lu_device *osd_device_fini(const struct lu_env *env,
4466 struct lu_device *d)
4471 shrink_dcache_sb(osd_sb(osd_dev(d)));
4472 osd_sync(env, lu2dt_dev(d));
4474 rc = osd_procfs_fini(osd_dev(d));
4476 CERROR("proc fini error %d \n", rc);
4477 RETURN (ERR_PTR(rc));
4480 if (osd_dev(d)->od_mount)
4481 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4482 osd_dev(d)->od_mount->lmi_mnt);
4483 osd_dev(d)->od_mount = NULL;
4488 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4489 struct lu_device_type *t,
4490 struct lustre_cfg *cfg)
4492 struct lu_device *l;
4493 struct osd_device *o;
4499 result = dt_device_init(&o->od_dt_dev, t);
4502 l->ld_ops = &osd_lu_ops;
4503 o->od_dt_dev.dd_ops = &osd_dt_ops;
4504 cfs_spin_lock_init(&o->od_osfs_lock);
4505 o->od_osfs_age = cfs_time_shift_64(-1000);
4506 o->od_capa_hash = init_capa_hash();
4507 if (o->od_capa_hash == NULL) {
4508 dt_device_fini(&o->od_dt_dev);
4509 l = ERR_PTR(-ENOMEM);
4512 l = ERR_PTR(result);
4517 l = ERR_PTR(-ENOMEM);
4521 static struct lu_device *osd_device_free(const struct lu_env *env,
4522 struct lu_device *d)
4524 struct osd_device *o = osd_dev(d);
4527 cleanup_capa_hash(o->od_capa_hash);
4528 dt_device_fini(&o->od_dt_dev);
4533 static int osd_process_config(const struct lu_env *env,
4534 struct lu_device *d, struct lustre_cfg *cfg)
4536 struct osd_device *o = osd_dev(d);
4540 switch(cfg->lcfg_command) {
4542 err = osd_mount(env, o, cfg);
4545 err = osd_shutdown(env, o);
4554 static int osd_recovery_complete(const struct lu_env *env,
4555 struct lu_device *d)
4560 static int osd_prepare(const struct lu_env *env,
4561 struct lu_device *pdev,
4562 struct lu_device *dev)
4564 struct osd_device *osd = osd_dev(dev);
4565 struct lustre_sb_info *lsi;
4566 struct lustre_disk_data *ldd;
4567 struct lustre_mount_info *lmi;
4568 struct osd_thread_info *oti = osd_oti_get(env);
4569 struct dt_object *d;
4573 /* 1. initialize oi before any file create or file open */
4574 result = osd_oi_init(oti, &osd->od_oi,
4575 &osd->od_dt_dev, lu2md_dev(pdev));
4579 lmi = osd->od_mount;
4580 lsi = s2lsi(lmi->lmi_sb);
4583 /* 2. setup local objects */
4584 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4588 /* 3. open remote object dir */
4589 d = dt_store_open(env, lu2dt_dev(dev), "",
4590 remote_obj_dir, &oti->oti_fid);
4592 osd->od_obj_area = d;
4595 result = PTR_ERR(d);
4596 osd->od_obj_area = NULL;
4603 static const struct lu_object_operations osd_lu_obj_ops = {
4604 .loo_object_init = osd_object_init,
4605 .loo_object_delete = osd_object_delete,
4606 .loo_object_release = osd_object_release,
4607 .loo_object_free = osd_object_free,
4608 .loo_object_print = osd_object_print,
4609 .loo_object_invariant = osd_object_invariant
4612 static const struct lu_device_operations osd_lu_ops = {
4613 .ldo_object_alloc = osd_object_alloc,
4614 .ldo_process_config = osd_process_config,
4615 .ldo_recovery_complete = osd_recovery_complete,
4616 .ldo_prepare = osd_prepare,
4619 static const struct lu_device_type_operations osd_device_type_ops = {
4620 .ldto_init = osd_type_init,
4621 .ldto_fini = osd_type_fini,
4623 .ldto_start = osd_type_start,
4624 .ldto_stop = osd_type_stop,
4626 .ldto_device_alloc = osd_device_alloc,
4627 .ldto_device_free = osd_device_free,
4629 .ldto_device_init = osd_device_init,
4630 .ldto_device_fini = osd_device_fini
4633 static struct lu_device_type osd_device_type = {
4634 .ldt_tags = LU_DEVICE_DT,
4635 .ldt_name = LUSTRE_OSD_NAME,
4636 .ldt_ops = &osd_device_type_ops,
4637 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4641 * lprocfs legacy support.
4643 static struct obd_ops osd_obd_device_ops = {
4644 .o_owner = THIS_MODULE
4647 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4648 .llod_name = remote_obj_dir,
4649 .llod_oid = OSD_REM_OBJ_DIR_OID,
4651 .llod_feat = &dt_directory_features,
4654 static int __init osd_mod_init(void)
4656 struct lprocfs_static_vars lvars;
4659 llo_local_obj_register(&llod_osd_rem_obj_dir);
4660 lprocfs_osd_init_vars(&lvars);
4661 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4662 LUSTRE_OSD_NAME, &osd_device_type);
4665 static void __exit osd_mod_exit(void)
4667 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4668 class_unregister_type(LUSTRE_OSD_NAME);
4671 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4672 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4673 MODULE_LICENSE("GPL");
4675 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);