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 if (((oh)->ot_declare_ ##op) > 0) { \
154 ((oh)->ot_declare_ ##op)--; \
158 #define OSD_DECLARE_OP(oh, op)
159 #define OSD_EXEC_OP(oh, op)
163 struct thandle ot_super;
165 struct journal_callback ot_jcb;
166 cfs_list_t ot_dcb_list;
167 /* Link to the device, for debugging. */
168 struct lu_ref_link *ot_dev_link;
171 #ifdef OSD_TRACK_DECLARES
172 unsigned char ot_declare_attr_set;
173 unsigned char ot_declare_punch;
174 unsigned char ot_declare_xattr_set;
175 unsigned char ot_declare_create;
176 unsigned char ot_declare_destroy;
177 unsigned char ot_declare_ref_add;
178 unsigned char ot_declare_ref_del;
179 unsigned char ot_declare_write;
180 unsigned char ot_declare_insert;
181 unsigned char ot_declare_delete;
184 #if OSD_THANDLE_STATS
185 /** time when this handle was allocated */
186 cfs_time_t oth_alloced;
188 /** time when this thanle was started */
189 cfs_time_t oth_started;
194 * Basic transaction credit op
204 DTO_LOG_REC, /**< XXX temporary: dt layer knows nothing about llog. */
215 static int lu_device_is_osd(const struct lu_device *d)
217 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
220 static struct osd_device *osd_dt_dev(const struct dt_device *d)
222 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
223 return container_of0(d, struct osd_device, od_dt_dev);
226 static struct osd_device *osd_dev(const struct lu_device *d)
228 LASSERT(lu_device_is_osd(d));
229 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
232 static struct osd_device *osd_obj2dev(const struct osd_object *o)
234 return osd_dev(o->oo_dt.do_lu.lo_dev);
237 static struct super_block *osd_sb(const struct osd_device *dev)
239 return dev->od_mount->lmi_mnt->mnt_sb;
242 static int osd_object_is_root(const struct osd_object *obj)
244 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
247 static struct osd_object *osd_obj(const struct lu_object *o)
249 LASSERT(lu_device_is_osd(o->lo_dev));
250 return container_of0(o, struct osd_object, oo_dt.do_lu);
253 static struct osd_object *osd_dt_obj(const struct dt_object *d)
255 return osd_obj(&d->do_lu);
258 static struct lu_device *osd2lu_dev(struct osd_device *osd)
260 return &osd->od_dt_dev.dd_lu_dev;
263 static journal_t *osd_journal(const struct osd_device *dev)
265 return LDISKFS_SB(osd_sb(dev))->s_journal;
268 static int osd_has_index(const struct osd_object *obj)
270 return obj->oo_dt.do_index_ops != NULL;
273 static int osd_object_invariant(const struct lu_object *l)
275 return osd_invariant(osd_obj(l));
278 #ifdef HAVE_QUOTA_SUPPORT
280 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
282 struct md_ucred *uc = md_ucred(env);
287 save->oc_uid = current_fsuid();
288 save->oc_gid = current_fsgid();
289 save->oc_cap = current_cap();
290 if ((tc = prepare_creds())) {
291 tc->fsuid = uc->mu_fsuid;
292 tc->fsgid = uc->mu_fsgid;
295 /* XXX not suboptimal */
296 cfs_curproc_cap_unpack(uc->mu_cap);
300 osd_pop_ctxt(struct osd_ctxt *save)
304 if ((tc = prepare_creds())) {
305 tc->fsuid = save->oc_uid;
306 tc->fsgid = save->oc_gid;
307 tc->cap_effective = save->oc_cap;
313 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
315 return lu_context_key_get(&env->le_ctx, &osd_key);
319 * Concurrency: doesn't matter
321 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
323 return osd_oti_get(env)->oti_r_locks > 0;
327 * Concurrency: doesn't matter
329 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
331 struct osd_thread_info *oti = osd_oti_get(env);
332 return oti->oti_w_locks > 0 && o->oo_owner == env;
336 * Concurrency: doesn't access mutable data
338 static int osd_root_get(const struct lu_env *env,
339 struct dt_device *dev, struct lu_fid *f)
343 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
344 LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
349 * OSD object methods.
353 * Concurrency: no concurrent access is possible that early in object
356 static struct lu_object *osd_object_alloc(const struct lu_env *env,
357 const struct lu_object_header *hdr,
360 struct osd_object *mo;
366 l = &mo->oo_dt.do_lu;
367 dt_object_init(&mo->oo_dt, NULL, d);
368 if (osd_dev(d)->od_iop_mode)
369 mo->oo_dt.do_ops = &osd_obj_ea_ops;
371 mo->oo_dt.do_ops = &osd_obj_ops;
373 l->lo_ops = &osd_lu_obj_ops;
374 cfs_init_rwsem(&mo->oo_sem);
375 cfs_init_rwsem(&mo->oo_ext_idx_sem);
376 cfs_spin_lock_init(&mo->oo_guard);
384 * retrieve object from backend ext fs.
386 static struct inode *osd_iget(struct osd_thread_info *info,
387 struct osd_device *dev,
388 const struct osd_inode_id *id)
390 struct inode *inode = NULL;
392 #ifdef HAVE_EXT4_LDISKFS
393 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
395 /* Newer kernels return an error instead of a NULL pointer */
398 inode = iget(osd_sb(dev), id->oii_ino);
401 CERROR("no inode\n");
402 inode = ERR_PTR(-EACCES);
403 } else if (id->oii_gen != OSD_OII_NOGEN &&
404 inode->i_generation != id->oii_gen) {
406 inode = ERR_PTR(-ESTALE);
407 } else if (inode->i_nlink == 0) {
408 /* due to parallel readdir and unlink,
409 * we can have dead inode here. */
410 CWARN("stale inode\n");
411 make_bad_inode(inode);
413 inode = ERR_PTR(-ESTALE);
414 } else if (is_bad_inode(inode)) {
415 CERROR("bad inode %lx\n",inode->i_ino);
417 inode = ERR_PTR(-ENOENT);
419 /* Do not update file c/mtime in ldiskfs.
420 * NB: we don't have any lock to protect this because we don't
421 * have reference on osd_object now, but contention with
422 * another lookup + attr_set can't happen in the tiny window
423 * between if (...) and set S_NOCMTIME. */
424 if (!(inode->i_flags & S_NOCMTIME))
425 inode->i_flags |= S_NOCMTIME;
430 static int osd_fid_lookup(const struct lu_env *env,
431 struct osd_object *obj, const struct lu_fid *fid)
433 struct osd_thread_info *info;
434 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
435 struct osd_device *dev;
436 struct osd_inode_id *id;
440 LINVRNT(osd_invariant(obj));
441 LASSERT(obj->oo_inode == NULL);
442 LASSERTF(fid_is_sane(fid) || osd_fid_is_root(fid), DFID, PFID(fid));
444 * This assertion checks that osd layer sees only local
445 * fids. Unfortunately it is somewhat expensive (does a
446 * cache-lookup). Disabling it for production/acceptance-testing.
448 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
452 info = osd_oti_get(env);
456 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
459 result = osd_oi_lookup(info, osd_fid2oi(dev, fid), 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_fid2oi(osd, fid), 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);
1994 if (S_ISDIR(inode->i_mode)) {
1995 LASSERT(osd_inode_unlinked(inode) ||
1996 inode->i_nlink == 1);
1997 cfs_spin_lock(&obj->oo_guard);
1999 cfs_spin_unlock(&obj->oo_guard);
2000 inode->i_sb->s_op->dirty_inode(inode);
2002 LASSERT(osd_inode_unlinked(inode));
2005 OSD_EXEC_OP(th, destroy);
2007 result = osd_oi_delete(osd_oti_get(env),
2008 osd_fid2oi(osd, fid), fid, th);
2010 /* XXX: add to ext3 orphan list */
2011 /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2013 /* not needed in the cache anymore */
2014 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2020 * Helper function for osd_xattr_set()
2022 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2023 const struct lu_buf *buf, const char *name, int fl)
2025 struct osd_object *obj = osd_dt_obj(dt);
2026 struct inode *inode = obj->oo_inode;
2027 struct osd_thread_info *info = osd_oti_get(env);
2028 struct dentry *dentry = &info->oti_child_dentry;
2032 LASSERT(dt_object_exists(dt));
2033 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2034 LASSERT(osd_write_locked(env, obj));
2036 if (fl & LU_XATTR_REPLACE)
2037 fs_flags |= XATTR_REPLACE;
2039 if (fl & LU_XATTR_CREATE)
2040 fs_flags |= XATTR_CREATE;
2042 dentry->d_inode = inode;
2043 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2044 buf->lb_len, fs_flags);
2049 * Put the fid into lustre_mdt_attrs, and then place the structure
2050 * inode's ea. This fid should not be altered during the life time
2053 * \retval +ve, on success
2054 * \retval -ve, on error
2056 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2058 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2059 const struct lu_fid *fid)
2061 struct osd_thread_info *info = osd_oti_get(env);
2062 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2064 lustre_lma_init(mdt_attrs, fid);
2065 lustre_lma_swab(mdt_attrs);
2066 return __osd_xattr_set(env, dt,
2067 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2068 XATTR_NAME_LMA, LU_XATTR_CREATE);
2073 * Helper function to form igif
2075 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
2078 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
2082 * Helper function to pack the fid, ldiskfs stores fid in packed format.
2084 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
2085 struct lu_fid *befider)
2087 fid_cpu_to_be(befider, (struct lu_fid *)fid);
2088 memcpy(pack->fp_area, befider, sizeof(*befider));
2089 pack->fp_len = sizeof(*befider) + 1;
2093 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2094 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2095 * To have compatilibility with 1.8 ldiskfs driver we need to have
2096 * magic number at start of fid data.
2097 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2100 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2101 const struct dt_rec *fid)
2103 param->edp_magic = LDISKFS_LUFID_MAGIC;
2104 param->edp_len = sizeof(struct lu_fid) + 1;
2106 fid_cpu_to_be((struct lu_fid *)param->edp_data,
2107 (struct lu_fid *)fid);
2110 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
2115 switch (pack->fp_len) {
2116 case sizeof *fid + 1:
2117 memcpy(fid, pack->fp_area, sizeof *fid);
2118 fid_be_to_cpu(fid, fid);
2121 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
2128 * Try to read the fid from inode ea into dt_rec, if return value
2129 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2131 * \param fid object fid.
2133 * \retval 0 on success
2135 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2136 __u32 ino, struct lu_fid *fid)
2138 struct osd_thread_info *info = osd_oti_get(env);
2139 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2140 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
2141 struct dentry *dentry = &info->oti_child_dentry;
2142 struct osd_inode_id *id = &info->oti_id;
2143 struct osd_device *dev;
2144 struct inode *inode;
2148 dev = osd_dev(ldev);
2151 id->oii_gen = OSD_OII_NOGEN;
2153 inode = osd_iget(info, dev, id);
2154 if (IS_ERR(inode)) {
2155 rc = PTR_ERR(inode);
2158 dentry->d_inode = inode;
2160 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2161 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
2164 /* Check LMA compatibility */
2166 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
2167 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
2168 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
2169 ~LMA_INCOMPAT_SUPP);
2174 lustre_lma_swab(mdt_attrs);
2175 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
2177 } else if (rc == -ENODATA) {
2178 osd_igif_get(env, inode, fid);
2187 * OSD layer object create function for interoperability mode (b11826).
2188 * This is mostly similar to osd_object_create(). Only difference being, fid is
2189 * inserted into inode ea here.
2191 * \retval 0, on success
2192 * \retval -ve, on error
2194 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2195 struct lu_attr *attr,
2196 struct dt_allocation_hint *hint,
2197 struct dt_object_format *dof,
2200 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2201 struct osd_object *obj = osd_dt_obj(dt);
2202 struct osd_thread_info *info = osd_oti_get(env);
2207 LASSERT(osd_invariant(obj));
2208 LASSERT(!dt_object_exists(dt));
2209 LASSERT(osd_write_locked(env, obj));
2210 LASSERT(th != NULL);
2212 OSD_EXEC_OP(th, create);
2214 result = __osd_object_create(info, obj, attr, hint, dof, th);
2216 /* objects under osd root shld have igif fid, so dont add fid EA */
2217 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2218 result = osd_ea_fid_set(env, dt, fid);
2221 result = __osd_oi_insert(env, obj, fid, th);
2223 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2224 LINVRNT(osd_invariant(obj));
2228 static int osd_declare_object_ref_add(const struct lu_env *env,
2229 struct dt_object *dt,
2230 struct thandle *handle)
2232 struct osd_thandle *oh;
2234 /* it's possible that object doesn't exist yet */
2235 LASSERT(handle != NULL);
2237 oh = container_of0(handle, struct osd_thandle, ot_super);
2238 LASSERT(oh->ot_handle == NULL);
2240 OSD_DECLARE_OP(oh, ref_add);
2241 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2247 * Concurrency: @dt is write locked.
2249 static int osd_object_ref_add(const struct lu_env *env,
2250 struct dt_object *dt,
2253 struct osd_object *obj = osd_dt_obj(dt);
2254 struct inode *inode = obj->oo_inode;
2256 LINVRNT(osd_invariant(obj));
2257 LASSERT(dt_object_exists(dt));
2258 LASSERT(osd_write_locked(env, obj));
2259 LASSERT(th != NULL);
2261 OSD_EXEC_OP(th, ref_add);
2264 * DIR_NLINK feature is set for compatibility reasons if:
2265 * 1) nlinks > LDISKFS_LINK_MAX, or
2266 * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2268 * It is easier to always set this flag (rather than check and set),
2269 * since it has less overhead, and the superblock will be dirtied
2270 * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2271 * do not actually care whether this flag is set or not.
2273 cfs_spin_lock(&obj->oo_guard);
2275 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2276 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2277 inode->i_nlink == 2)
2280 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
2281 cfs_spin_unlock(&obj->oo_guard);
2282 inode->i_sb->s_op->dirty_inode(inode);
2283 LINVRNT(osd_invariant(obj));
2288 static int osd_declare_object_ref_del(const struct lu_env *env,
2289 struct dt_object *dt,
2290 struct thandle *handle)
2292 struct osd_thandle *oh;
2294 LASSERT(dt_object_exists(dt));
2295 LASSERT(handle != NULL);
2297 oh = container_of0(handle, struct osd_thandle, ot_super);
2298 LASSERT(oh->ot_handle == NULL);
2300 OSD_DECLARE_OP(oh, ref_del);
2301 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2307 * Concurrency: @dt is write locked.
2309 static int osd_object_ref_del(const struct lu_env *env,
2310 struct dt_object *dt,
2313 struct osd_object *obj = osd_dt_obj(dt);
2314 struct inode *inode = obj->oo_inode;
2316 LINVRNT(osd_invariant(obj));
2317 LASSERT(dt_object_exists(dt));
2318 LASSERT(osd_write_locked(env, obj));
2319 LASSERT(th != NULL);
2321 OSD_EXEC_OP(th, ref_del);
2323 cfs_spin_lock(&obj->oo_guard);
2324 LASSERT(inode->i_nlink > 0);
2326 /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2327 * then the nlink count is 1. Don't let it be set to 0 or the directory
2328 * inode will be deleted incorrectly. */
2329 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2331 cfs_spin_unlock(&obj->oo_guard);
2332 inode->i_sb->s_op->dirty_inode(inode);
2333 LINVRNT(osd_invariant(obj));
2339 * Get the 64-bit version for an inode.
2341 static int osd_object_version_get(const struct lu_env *env,
2342 struct dt_object *dt, dt_obj_version_t *ver)
2344 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2346 CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2347 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2348 *ver = LDISKFS_I(inode)->i_fs_version;
2353 * Concurrency: @dt is read locked.
2355 static int osd_xattr_get(const struct lu_env *env,
2356 struct dt_object *dt,
2359 struct lustre_capa *capa)
2361 struct osd_object *obj = osd_dt_obj(dt);
2362 struct inode *inode = obj->oo_inode;
2363 struct osd_thread_info *info = osd_oti_get(env);
2364 struct dentry *dentry = &info->oti_obj_dentry;
2366 /* version get is not real XATTR but uses xattr API */
2367 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2368 /* for version we are just using xattr API but change inode
2370 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2371 osd_object_version_get(env, dt, buf->lb_buf);
2372 return sizeof(dt_obj_version_t);
2375 LASSERT(dt_object_exists(dt));
2376 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2377 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2379 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2382 dentry->d_inode = inode;
2383 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2387 static int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
2388 const struct lu_buf *buf, const char *name,
2389 int fl, struct thandle *handle)
2391 struct osd_thandle *oh;
2393 LASSERT(handle != NULL);
2395 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2396 /* no credits for version */
2400 oh = container_of0(handle, struct osd_thandle, ot_super);
2401 LASSERT(oh->ot_handle == NULL);
2403 OSD_DECLARE_OP(oh, xattr_set);
2404 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2410 * Set the 64-bit version for object
2412 static void osd_object_version_set(const struct lu_env *env,
2413 struct dt_object *dt,
2414 dt_obj_version_t *new_version)
2416 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2418 CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2419 *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2421 LDISKFS_I(inode)->i_fs_version = *new_version;
2422 /** Version is set after all inode operations are finished,
2423 * so we should mark it dirty here */
2424 inode->i_sb->s_op->dirty_inode(inode);
2428 * Concurrency: @dt is write locked.
2430 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2431 const struct lu_buf *buf, const char *name, int fl,
2432 struct thandle *handle, struct lustre_capa *capa)
2434 LASSERT(handle != NULL);
2436 /* version set is not real XATTR */
2437 if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2438 /* for version we are just using xattr API but change inode
2440 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2441 osd_object_version_set(env, dt, buf->lb_buf);
2442 return sizeof(dt_obj_version_t);
2445 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2448 OSD_EXEC_OP(handle, xattr_set);
2449 return __osd_xattr_set(env, dt, buf, name, fl);
2453 * Concurrency: @dt is read locked.
2455 static int osd_xattr_list(const struct lu_env *env,
2456 struct dt_object *dt,
2458 struct lustre_capa *capa)
2460 struct osd_object *obj = osd_dt_obj(dt);
2461 struct inode *inode = obj->oo_inode;
2462 struct osd_thread_info *info = osd_oti_get(env);
2463 struct dentry *dentry = &info->oti_obj_dentry;
2465 LASSERT(dt_object_exists(dt));
2466 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2467 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2469 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2472 dentry->d_inode = inode;
2473 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2476 static int osd_declare_xattr_del(const struct lu_env *env,
2477 struct dt_object *dt,
2479 struct thandle *handle)
2481 struct osd_thandle *oh;
2483 LASSERT(dt_object_exists(dt));
2484 LASSERT(handle != NULL);
2486 oh = container_of0(handle, struct osd_thandle, ot_super);
2487 LASSERT(oh->ot_handle == NULL);
2489 OSD_DECLARE_OP(oh, xattr_set);
2490 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2496 * Concurrency: @dt is write locked.
2498 static int osd_xattr_del(const struct lu_env *env,
2499 struct dt_object *dt,
2501 struct thandle *handle,
2502 struct lustre_capa *capa)
2504 struct osd_object *obj = osd_dt_obj(dt);
2505 struct inode *inode = obj->oo_inode;
2506 struct osd_thread_info *info = osd_oti_get(env);
2507 struct dentry *dentry = &info->oti_obj_dentry;
2510 LASSERT(dt_object_exists(dt));
2511 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2512 LASSERT(osd_write_locked(env, obj));
2513 LASSERT(handle != NULL);
2515 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2518 OSD_EXEC_OP(handle, xattr_set);
2520 dentry->d_inode = inode;
2521 rc = inode->i_op->removexattr(dentry, name);
2525 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2526 struct dt_object *dt,
2527 struct lustre_capa *old,
2530 struct osd_thread_info *info = osd_oti_get(env);
2531 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2532 struct osd_object *obj = osd_dt_obj(dt);
2533 struct osd_device *dev = osd_obj2dev(obj);
2534 struct lustre_capa_key *key = &info->oti_capa_key;
2535 struct lustre_capa *capa = &info->oti_capa;
2536 struct obd_capa *oc;
2537 struct md_capainfo *ci;
2541 if (!dev->od_fl_capa)
2542 RETURN(ERR_PTR(-ENOENT));
2544 LASSERT(dt_object_exists(dt));
2545 LINVRNT(osd_invariant(obj));
2547 /* renewal sanity check */
2548 if (old && osd_object_auth(env, dt, old, opc))
2549 RETURN(ERR_PTR(-EACCES));
2551 ci = md_capainfo(env);
2553 RETURN(ERR_PTR(-ENOENT));
2555 switch (ci->mc_auth) {
2559 capa->lc_uid = obj->oo_inode->i_uid;
2560 capa->lc_gid = obj->oo_inode->i_gid;
2561 capa->lc_flags = LC_ID_PLAIN;
2563 case LC_ID_CONVERT: {
2566 s[0] = obj->oo_inode->i_uid;
2567 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2568 s[2] = obj->oo_inode->i_gid;
2569 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2570 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2572 RETURN(ERR_PTR(rc));
2574 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2575 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2576 capa->lc_flags = LC_ID_CONVERT;
2580 RETURN(ERR_PTR(-EINVAL));
2583 capa->lc_fid = *fid;
2585 capa->lc_flags |= dev->od_capa_alg << 24;
2586 capa->lc_timeout = dev->od_capa_timeout;
2587 capa->lc_expiry = 0;
2589 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2591 LASSERT(!capa_is_expired(oc));
2595 cfs_spin_lock(&capa_lock);
2596 *key = dev->od_capa_keys[1];
2597 cfs_spin_unlock(&capa_lock);
2599 capa->lc_keyid = key->lk_keyid;
2600 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2602 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2604 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2605 RETURN(ERR_PTR(rc));
2608 oc = capa_add(dev->od_capa_hash, capa);
2612 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2615 struct osd_object *obj = osd_dt_obj(dt);
2616 struct inode *inode = obj->oo_inode;
2617 struct osd_thread_info *info = osd_oti_get(env);
2618 struct dentry *dentry = &info->oti_obj_dentry;
2619 struct file *file = &info->oti_file;
2622 dentry->d_inode = inode;
2623 file->f_dentry = dentry;
2624 file->f_mapping = inode->i_mapping;
2625 file->f_op = inode->i_fop;
2626 LOCK_INODE_MUTEX(inode);
2627 rc = file->f_op->fsync(file, dentry, 0);
2628 UNLOCK_INODE_MUTEX(inode);
2632 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2635 struct osd_object *obj = osd_dt_obj(dt);
2638 *data = (void *)obj->oo_inode;
2646 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2647 const struct dt_index_features *feat)
2649 struct iam_descr *descr;
2651 if (osd_object_is_root(o))
2652 return feat == &dt_directory_features;
2654 LASSERT(o->oo_dir != NULL);
2656 descr = o->oo_dir->od_container.ic_descr;
2657 if (feat == &dt_directory_features) {
2658 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2664 feat->dif_keysize_min <= descr->id_key_size &&
2665 descr->id_key_size <= feat->dif_keysize_max &&
2666 feat->dif_recsize_min <= descr->id_rec_size &&
2667 descr->id_rec_size <= feat->dif_recsize_max &&
2668 !(feat->dif_flags & (DT_IND_VARKEY |
2669 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2670 ergo(feat->dif_flags & DT_IND_UPDATE,
2671 1 /* XXX check that object (and file system) is
2676 static int osd_iam_container_init(const struct lu_env *env,
2677 struct osd_object *obj,
2678 struct osd_directory *dir)
2680 struct iam_container *bag = &dir->od_container;
2683 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2687 result = iam_container_setup(bag);
2691 if (osd_obj2dev(obj)->od_iop_mode) {
2692 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2694 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2700 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2702 iam_container_fini(bag);
2709 * Concurrency: no external locking is necessary.
2711 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2712 const struct dt_index_features *feat)
2716 struct osd_object *obj = osd_dt_obj(dt);
2717 struct osd_device *osd = osd_obj2dev(obj);
2719 LINVRNT(osd_invariant(obj));
2720 LASSERT(dt_object_exists(dt));
2722 if (osd_object_is_root(obj)) {
2723 dt->do_index_ops = &osd_index_ea_ops;
2725 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2726 dt->do_index_ops = &osd_index_ea_ops;
2727 if (S_ISDIR(obj->oo_inode->i_mode))
2732 } else if (!osd_has_index(obj)) {
2733 struct osd_directory *dir;
2738 cfs_spin_lock(&obj->oo_guard);
2739 if (obj->oo_dir == NULL)
2743 * Concurrent thread allocated container data.
2746 cfs_spin_unlock(&obj->oo_guard);
2748 * Now, that we have container data, serialize its
2751 cfs_down_write(&obj->oo_ext_idx_sem);
2753 * recheck under lock.
2755 if (!osd_has_index(obj))
2756 result = osd_iam_container_init(env, obj, dir);
2759 cfs_up_write(&obj->oo_ext_idx_sem);
2767 if (result == 0 && ea_dir == 0) {
2768 if (!osd_iam_index_probe(env, obj, feat))
2771 LINVRNT(osd_invariant(obj));
2776 static const struct dt_object_operations osd_obj_ops = {
2777 .do_read_lock = osd_object_read_lock,
2778 .do_write_lock = osd_object_write_lock,
2779 .do_read_unlock = osd_object_read_unlock,
2780 .do_write_unlock = osd_object_write_unlock,
2781 .do_write_locked = osd_object_write_locked,
2782 .do_attr_get = osd_attr_get,
2783 .do_declare_attr_set = osd_declare_attr_set,
2784 .do_attr_set = osd_attr_set,
2785 .do_ah_init = osd_ah_init,
2786 .do_declare_create = osd_declare_object_create,
2787 .do_create = osd_object_create,
2788 .do_declare_destroy = osd_declare_object_destroy,
2789 .do_destroy = osd_object_destroy,
2790 .do_index_try = osd_index_try,
2791 .do_declare_ref_add = osd_declare_object_ref_add,
2792 .do_ref_add = osd_object_ref_add,
2793 .do_declare_ref_del = osd_declare_object_ref_del,
2794 .do_ref_del = osd_object_ref_del,
2795 .do_xattr_get = osd_xattr_get,
2796 .do_declare_xattr_set = osd_declare_xattr_set,
2797 .do_xattr_set = osd_xattr_set,
2798 .do_declare_xattr_del = osd_declare_xattr_del,
2799 .do_xattr_del = osd_xattr_del,
2800 .do_xattr_list = osd_xattr_list,
2801 .do_capa_get = osd_capa_get,
2802 .do_object_sync = osd_object_sync,
2803 .do_data_get = osd_data_get,
2807 * dt_object_operations for interoperability mode
2808 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2810 static const struct dt_object_operations osd_obj_ea_ops = {
2811 .do_read_lock = osd_object_read_lock,
2812 .do_write_lock = osd_object_write_lock,
2813 .do_read_unlock = osd_object_read_unlock,
2814 .do_write_unlock = osd_object_write_unlock,
2815 .do_write_locked = osd_object_write_locked,
2816 .do_attr_get = osd_attr_get,
2817 .do_declare_attr_set = osd_declare_attr_set,
2818 .do_attr_set = osd_attr_set,
2819 .do_ah_init = osd_ah_init,
2820 .do_declare_create = osd_declare_object_create,
2821 .do_create = osd_object_ea_create,
2822 .do_declare_destroy = osd_declare_object_destroy,
2823 .do_destroy = osd_object_destroy,
2824 .do_index_try = osd_index_try,
2825 .do_declare_ref_add = osd_declare_object_ref_add,
2826 .do_ref_add = osd_object_ref_add,
2827 .do_declare_ref_del = osd_declare_object_ref_del,
2828 .do_ref_del = osd_object_ref_del,
2829 .do_xattr_get = osd_xattr_get,
2830 .do_declare_xattr_set = osd_declare_xattr_set,
2831 .do_xattr_set = osd_xattr_set,
2832 .do_declare_xattr_del = osd_declare_xattr_del,
2833 .do_xattr_del = osd_xattr_del,
2834 .do_xattr_list = osd_xattr_list,
2835 .do_capa_get = osd_capa_get,
2836 .do_object_sync = osd_object_sync,
2837 .do_data_get = osd_data_get,
2845 * XXX: Another layering violation for now.
2847 * We don't want to use ->f_op->read methods, because generic file write
2849 * - serializes on ->i_sem, and
2851 * - does a lot of extra work like balance_dirty_pages(),
2853 * which doesn't work for globally shared files like /last-received.
2855 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2857 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2859 memcpy(buffer, (char*)ei->i_data, buflen);
2864 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2867 struct buffer_head *bh;
2868 unsigned long block;
2875 /* prevent reading after eof */
2876 spin_lock(&inode->i_lock);
2877 if (i_size_read(inode) < *offs + size) {
2878 size = i_size_read(inode) - *offs;
2879 spin_unlock(&inode->i_lock);
2881 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2882 i_size_read(inode), *offs);
2884 } else if (size == 0) {
2888 spin_unlock(&inode->i_lock);
2891 blocksize = 1 << inode->i_blkbits;
2894 block = *offs >> inode->i_blkbits;
2895 boffs = *offs & (blocksize - 1);
2896 csize = min(blocksize - boffs, size);
2897 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2899 CERROR("can't read block: %d\n", err);
2903 memcpy(buf, bh->b_data + boffs, csize);
2913 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2914 struct lu_buf *buf, loff_t *pos,
2915 struct lustre_capa *capa)
2917 struct osd_object *obj = osd_dt_obj(dt);
2918 struct inode *inode = obj->oo_inode;
2921 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2924 /* Read small symlink from inode body as we need to maintain correct
2925 * on-disk symlinks for ldiskfs.
2927 if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2928 (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2929 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2931 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2936 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2939 memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2941 LDISKFS_I(inode)->i_disksize = buflen;
2942 i_size_write(inode, buflen);
2943 inode->i_sb->s_op->dirty_inode(inode);
2948 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2949 loff_t *offs, handle_t *handle)
2951 struct buffer_head *bh = NULL;
2952 loff_t offset = *offs;
2953 loff_t new_size = i_size_read(inode);
2954 unsigned long block;
2955 int blocksize = 1 << inode->i_blkbits;
2959 int dirty_inode = 0;
2961 while (bufsize > 0) {
2965 block = offset >> inode->i_blkbits;
2966 boffs = offset & (blocksize - 1);
2967 size = min(blocksize - boffs, bufsize);
2968 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2970 CERROR("can't read/create block: %d\n", err);
2974 err = ldiskfs_journal_get_write_access(handle, bh);
2976 CERROR("journal_get_write_access() returned error %d\n",
2980 LASSERTF(boffs + size <= bh->b_size,
2981 "boffs %d size %d bh->b_size %lu",
2982 boffs, size, (unsigned long)bh->b_size);
2983 memcpy(bh->b_data + boffs, buf, size);
2984 err = ldiskfs_journal_dirty_metadata(handle, bh);
2988 if (offset + size > new_size)
2989 new_size = offset + size;
2997 /* correct in-core and on-disk sizes */
2998 if (new_size > i_size_read(inode)) {
2999 spin_lock(&inode->i_lock);
3000 if (new_size > i_size_read(inode))
3001 i_size_write(inode, new_size);
3002 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
3003 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
3006 spin_unlock(&inode->i_lock);
3008 inode->i_sb->s_op->dirty_inode(inode);
3016 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
3017 const loff_t size, loff_t pos,
3018 struct thandle *handle)
3020 struct osd_thandle *oh;
3022 LASSERT(handle != NULL);
3024 oh = container_of0(handle, struct osd_thandle, ot_super);
3025 LASSERT(oh->ot_handle == NULL);
3027 OSD_DECLARE_OP(oh, write);
3028 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BLOCK];
3033 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
3034 const struct lu_buf *buf, loff_t *pos,
3035 struct thandle *handle, struct lustre_capa *capa,
3038 struct osd_object *obj = osd_dt_obj(dt);
3039 struct inode *inode = obj->oo_inode;
3040 struct osd_thandle *oh;
3042 #ifdef HAVE_QUOTA_SUPPORT
3043 cfs_cap_t save = cfs_curproc_cap_pack();
3046 LASSERT(handle != NULL);
3048 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
3051 oh = container_of(handle, struct osd_thandle, ot_super);
3052 LASSERT(oh->ot_handle->h_transaction != NULL);
3053 #ifdef HAVE_QUOTA_SUPPORT
3055 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3057 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3059 /* Write small symlink to inode body as we need to maintain correct
3060 * on-disk symlinks for ldiskfs.
3062 if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
3063 (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
3064 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
3066 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
3069 #ifdef HAVE_QUOTA_SUPPORT
3070 cfs_curproc_cap_unpack(save);
3073 result = buf->lb_len;
3078 * in some cases we may need declare methods for objects being created
3079 * e.g., when we create symlink
3081 static const struct dt_body_operations osd_body_ops_new = {
3082 .dbo_declare_write = osd_declare_write,
3085 static const struct dt_body_operations osd_body_ops = {
3086 .dbo_read = osd_read,
3087 .dbo_declare_write = osd_declare_write,
3088 .dbo_write = osd_write
3091 static int osd_index_declare_iam_delete(const struct lu_env *env,
3092 struct dt_object *dt,
3093 const struct dt_key *key,
3094 struct thandle *handle)
3096 struct osd_thandle *oh;
3098 oh = container_of0(handle, struct osd_thandle, ot_super);
3099 LASSERT(oh->ot_handle == NULL);
3101 OSD_DECLARE_OP(oh, delete);
3102 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3108 * delete a (key, value) pair from index \a dt specified by \a key
3110 * \param dt osd index object
3111 * \param key key for index
3112 * \param rec record reference
3113 * \param handle transaction handler
3116 * \retval -ve failure
3119 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3120 const struct dt_key *key, struct thandle *handle,
3121 struct lustre_capa *capa)
3123 struct osd_object *obj = osd_dt_obj(dt);
3124 struct osd_thandle *oh;
3125 struct iam_path_descr *ipd;
3126 struct iam_container *bag = &obj->oo_dir->od_container;
3131 LINVRNT(osd_invariant(obj));
3132 LASSERT(dt_object_exists(dt));
3133 LASSERT(bag->ic_object == obj->oo_inode);
3134 LASSERT(handle != NULL);
3136 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3139 OSD_EXEC_OP(handle, delete);
3141 ipd = osd_idx_ipd_get(env, bag);
3142 if (unlikely(ipd == NULL))
3145 oh = container_of0(handle, struct osd_thandle, ot_super);
3146 LASSERT(oh->ot_handle != NULL);
3147 LASSERT(oh->ot_handle->h_transaction != NULL);
3149 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3150 osd_ipd_put(env, bag, ipd);
3151 LINVRNT(osd_invariant(obj));
3155 static int osd_index_declare_ea_delete(const struct lu_env *env,
3156 struct dt_object *dt,
3157 const struct dt_key *key,
3158 struct thandle *handle)
3160 struct osd_thandle *oh;
3162 LASSERT(dt_object_exists(dt));
3163 LASSERT(handle != NULL);
3165 oh = container_of0(handle, struct osd_thandle, ot_super);
3166 LASSERT(oh->ot_handle == NULL);
3168 OSD_DECLARE_OP(oh, delete);
3169 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3174 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3177 struct osd_fid_pack *rec;
3180 if (de->file_type & LDISKFS_DIRENT_LUFID) {
3181 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3182 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3188 * Index delete function for interoperability mode (b11826).
3189 * It will remove the directory entry added by osd_index_ea_insert().
3190 * This entry is needed to maintain name->fid mapping.
3192 * \param key, key i.e. file entry to be deleted
3194 * \retval 0, on success
3195 * \retval -ve, on error
3197 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3198 const struct dt_key *key, struct thandle *handle,
3199 struct lustre_capa *capa)
3201 struct osd_object *obj = osd_dt_obj(dt);
3202 struct inode *dir = obj->oo_inode;
3203 struct dentry *dentry;
3204 struct osd_thandle *oh;
3205 struct ldiskfs_dir_entry_2 *de;
3206 struct buffer_head *bh;
3207 struct htree_lock *hlock = NULL;
3213 LINVRNT(osd_invariant(obj));
3214 LASSERT(dt_object_exists(dt));
3215 LASSERT(handle != NULL);
3217 OSD_EXEC_OP(handle, delete);
3219 oh = container_of(handle, struct osd_thandle, ot_super);
3220 LASSERT(oh->ot_handle != NULL);
3221 LASSERT(oh->ot_handle->h_transaction != NULL);
3223 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3226 dentry = osd_child_dentry_get(env, obj,
3227 (char *)key, strlen((char *)key));
3229 if (obj->oo_hl_head != NULL) {
3230 hlock = osd_oti_get(env)->oti_hlock;
3231 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3232 dir, LDISKFS_HLOCK_DEL);
3234 cfs_down_write(&obj->oo_ext_idx_sem);
3237 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3239 rc = ldiskfs_delete_entry(oh->ot_handle,
3246 ldiskfs_htree_unlock(hlock);
3248 cfs_up_write(&obj->oo_ext_idx_sem);
3250 LASSERT(osd_invariant(obj));
3255 * Lookup index for \a key and copy record to \a rec.
3257 * \param dt osd index object
3258 * \param key key for index
3259 * \param rec record reference
3261 * \retval +ve success : exact mach
3262 * \retval 0 return record with key not greater than \a key
3263 * \retval -ve failure
3265 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3266 struct dt_rec *rec, const struct dt_key *key,
3267 struct lustre_capa *capa)
3269 struct osd_object *obj = osd_dt_obj(dt);
3270 struct iam_path_descr *ipd;
3271 struct iam_container *bag = &obj->oo_dir->od_container;
3272 struct osd_thread_info *oti = osd_oti_get(env);
3273 struct iam_iterator *it = &oti->oti_idx_it;
3274 struct iam_rec *iam_rec;
3278 LASSERT(osd_invariant(obj));
3279 LASSERT(dt_object_exists(dt));
3280 LASSERT(bag->ic_object == obj->oo_inode);
3282 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3285 ipd = osd_idx_ipd_get(env, bag);
3289 /* got ipd now we can start iterator. */
3290 iam_it_init(it, bag, 0, ipd);
3292 rc = iam_it_get(it, (struct iam_key *)key);
3294 if (S_ISDIR(obj->oo_inode->i_mode))
3295 iam_rec = (struct iam_rec *)oti->oti_ldp;
3297 iam_rec = (struct iam_rec *) rec;
3299 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3300 if (S_ISDIR(obj->oo_inode->i_mode))
3301 osd_fid_unpack((struct lu_fid *) rec,
3302 (struct osd_fid_pack *)iam_rec);
3306 osd_ipd_put(env, bag, ipd);
3308 LINVRNT(osd_invariant(obj));
3313 static int osd_index_declare_iam_insert(const struct lu_env *env,
3314 struct dt_object *dt,
3315 const struct dt_rec *rec,
3316 const struct dt_key *key,
3317 struct thandle *handle)
3319 struct osd_thandle *oh;
3321 LASSERT(dt_object_exists(dt));
3322 LASSERT(handle != NULL);
3324 oh = container_of0(handle, struct osd_thandle, ot_super);
3325 LASSERT(oh->ot_handle == NULL);
3327 OSD_DECLARE_OP(oh, insert);
3328 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3334 * Inserts (key, value) pair in \a dt index object.
3336 * \param dt osd index object
3337 * \param key key for index
3338 * \param rec record reference
3339 * \param th transaction handler
3342 * \retval -ve failure
3344 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3345 const struct dt_rec *rec, const struct dt_key *key,
3346 struct thandle *th, struct lustre_capa *capa,
3349 struct osd_object *obj = osd_dt_obj(dt);
3350 struct iam_path_descr *ipd;
3351 struct osd_thandle *oh;
3352 struct iam_container *bag = &obj->oo_dir->od_container;
3353 #ifdef HAVE_QUOTA_SUPPORT
3354 cfs_cap_t save = cfs_curproc_cap_pack();
3356 struct osd_thread_info *oti = osd_oti_get(env);
3357 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3362 LINVRNT(osd_invariant(obj));
3363 LASSERT(dt_object_exists(dt));
3364 LASSERT(bag->ic_object == obj->oo_inode);
3365 LASSERT(th != NULL);
3367 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3370 OSD_EXEC_OP(th, insert);
3372 ipd = osd_idx_ipd_get(env, bag);
3373 if (unlikely(ipd == NULL))
3376 oh = container_of0(th, struct osd_thandle, ot_super);
3377 LASSERT(oh->ot_handle != NULL);
3378 LASSERT(oh->ot_handle->h_transaction != NULL);
3379 #ifdef HAVE_QUOTA_SUPPORT
3381 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3383 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3385 if (S_ISDIR(obj->oo_inode->i_mode))
3386 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3388 iam_rec = (struct iam_rec *) rec;
3389 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3391 #ifdef HAVE_QUOTA_SUPPORT
3392 cfs_curproc_cap_unpack(save);
3394 osd_ipd_put(env, bag, ipd);
3395 LINVRNT(osd_invariant(obj));
3400 * Calls ldiskfs_add_entry() to add directory entry
3401 * into the directory. This is required for
3402 * interoperability mode (b11826)
3404 * \retval 0, on success
3405 * \retval -ve, on error
3407 static int __osd_ea_add_rec(struct osd_thread_info *info,
3408 struct osd_object *pobj,
3409 struct inode *cinode,
3411 const struct dt_rec *fid,
3412 struct htree_lock *hlock,
3415 struct ldiskfs_dentry_param *ldp;
3416 struct dentry *child;
3417 struct osd_thandle *oth;
3420 oth = container_of(th, struct osd_thandle, ot_super);
3421 LASSERT(oth->ot_handle != NULL);
3422 LASSERT(oth->ot_handle->h_transaction != NULL);
3424 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3426 if (fid_is_igif((struct lu_fid *)fid) ||
3427 fid_is_norm((struct lu_fid *)fid)) {
3428 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3429 osd_get_ldiskfs_dirent_param(ldp, fid);
3430 child->d_fsdata = (void*) ldp;
3432 child->d_fsdata = NULL;
3433 rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3439 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3440 * into the directory.Also sets flags into osd object to
3441 * indicate dot and dotdot are created. This is required for
3442 * interoperability mode (b11826)
3444 * \param dir directory for dot and dotdot fixup.
3445 * \param obj child object for linking
3447 * \retval 0, on success
3448 * \retval -ve, on error
3450 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3451 struct osd_object *dir,
3452 struct inode *parent_dir, const char *name,
3453 const struct dt_rec *dot_fid,
3454 const struct dt_rec *dot_dot_fid,
3457 struct inode *inode = dir->oo_inode;
3458 struct ldiskfs_dentry_param *dot_ldp;
3459 struct ldiskfs_dentry_param *dot_dot_ldp;
3460 struct osd_thandle *oth;
3463 oth = container_of(th, struct osd_thandle, ot_super);
3464 LASSERT(oth->ot_handle->h_transaction != NULL);
3465 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3467 if (strcmp(name, dot) == 0) {
3468 if (dir->oo_compat_dot_created) {
3471 LASSERT(inode == parent_dir);
3472 dir->oo_compat_dot_created = 1;
3475 } else if(strcmp(name, dotdot) == 0) {
3476 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3477 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3479 if (!dir->oo_compat_dot_created)
3481 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
3482 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3483 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3488 /* in case of rename, dotdot is already created */
3489 if (dir->oo_compat_dotdot_created) {
3490 return __osd_ea_add_rec(info, dir, parent_dir, name,
3491 dot_dot_fid, NULL, th);
3494 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3495 inode, dot_ldp, dot_dot_ldp);
3497 dir->oo_compat_dotdot_created = 1;
3505 * It will call the appropriate osd_add* function and return the
3506 * value, return by respective functions.
3508 static int osd_ea_add_rec(const struct lu_env *env,
3509 struct osd_object *pobj,
3510 struct inode *cinode,
3512 const struct dt_rec *fid,
3515 struct osd_thread_info *info = osd_oti_get(env);
3516 struct htree_lock *hlock;
3519 hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3521 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3523 if (hlock != NULL) {
3524 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3527 cfs_down_write(&pobj->oo_ext_idx_sem);
3529 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3530 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3533 if (hlock != NULL) {
3534 ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3535 pobj->oo_inode, LDISKFS_HLOCK_ADD);
3537 cfs_down_write(&pobj->oo_ext_idx_sem);
3540 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3544 ldiskfs_htree_unlock(hlock);
3546 cfs_up_write(&pobj->oo_ext_idx_sem);
3552 * Calls ->lookup() to find dentry. From dentry get inode and
3553 * read inode's ea to get fid. This is required for interoperability
3556 * \retval 0, on success
3557 * \retval -ve, on error
3559 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3560 struct dt_rec *rec, const struct dt_key *key)
3562 struct inode *dir = obj->oo_inode;
3563 struct dentry *dentry;
3564 struct ldiskfs_dir_entry_2 *de;
3565 struct buffer_head *bh;
3566 struct lu_fid *fid = (struct lu_fid *) rec;
3567 struct htree_lock *hlock = NULL;
3571 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3573 dentry = osd_child_dentry_get(env, obj,
3574 (char *)key, strlen((char *)key));
3576 if (obj->oo_hl_head != NULL) {
3577 hlock = osd_oti_get(env)->oti_hlock;
3578 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3579 dir, LDISKFS_HLOCK_LOOKUP);
3581 cfs_down_read(&obj->oo_ext_idx_sem);
3584 bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3586 ino = le32_to_cpu(de->inode);
3587 rc = osd_get_fid_from_dentry(de, rec);
3589 /* done with de, release bh */
3592 rc = osd_ea_fid_get(env, obj, ino, fid);
3598 ldiskfs_htree_unlock(hlock);
3600 cfs_up_read(&obj->oo_ext_idx_sem);
3605 * Find the osd object for given fid.
3607 * \param fid need to find the osd object having this fid
3609 * \retval osd_object on success
3610 * \retval -ve on error
3612 struct osd_object *osd_object_find(const struct lu_env *env,
3613 struct dt_object *dt,
3614 const struct lu_fid *fid)
3616 struct lu_device *ludev = dt->do_lu.lo_dev;
3617 struct osd_object *child = NULL;
3618 struct lu_object *luch;
3619 struct lu_object *lo;
3621 luch = lu_object_find(env, ludev, fid, NULL);
3622 if (!IS_ERR(luch)) {
3623 if (lu_object_exists(luch)) {
3624 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3626 child = osd_obj(lo);
3628 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3629 "lu_object can't be located"
3630 ""DFID"\n", PFID(fid));
3632 if (child == NULL) {
3633 lu_object_put(env, luch);
3634 CERROR("Unable to get osd_object\n");
3635 child = ERR_PTR(-ENOENT);
3638 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3639 "lu_object does not exists "DFID"\n",
3641 child = ERR_PTR(-ENOENT);
3644 child = (void *)luch;
3650 * Put the osd object once done with it.
3652 * \param obj osd object that needs to be put
3654 static inline void osd_object_put(const struct lu_env *env,
3655 struct osd_object *obj)
3657 lu_object_put(env, &obj->oo_dt.do_lu);
3660 static int osd_index_declare_ea_insert(const struct lu_env *env,
3661 struct dt_object *dt,
3662 const struct dt_rec *rec,
3663 const struct dt_key *key,
3664 struct thandle *handle)
3666 struct osd_thandle *oh;
3668 LASSERT(dt_object_exists(dt));
3669 LASSERT(handle != NULL);
3671 oh = container_of0(handle, struct osd_thandle, ot_super);
3672 LASSERT(oh->ot_handle == NULL);
3674 OSD_DECLARE_OP(oh, insert);
3675 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3681 * Index add function for interoperability mode (b11826).
3682 * It will add the directory entry.This entry is needed to
3683 * maintain name->fid mapping.
3685 * \param key it is key i.e. file entry to be inserted
3686 * \param rec it is value of given key i.e. fid
3688 * \retval 0, on success
3689 * \retval -ve, on error
3691 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3692 const struct dt_rec *rec,
3693 const struct dt_key *key, struct thandle *th,
3694 struct lustre_capa *capa, int ignore_quota)
3696 struct osd_object *obj = osd_dt_obj(dt);
3697 struct lu_fid *fid = (struct lu_fid *) rec;
3698 const char *name = (const char *)key;
3699 struct osd_object *child;
3700 #ifdef HAVE_QUOTA_SUPPORT
3701 cfs_cap_t save = cfs_curproc_cap_pack();
3707 LASSERT(osd_invariant(obj));
3708 LASSERT(dt_object_exists(dt));
3709 LASSERT(th != NULL);
3711 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3714 child = osd_object_find(env, dt, fid);
3715 if (!IS_ERR(child)) {
3716 #ifdef HAVE_QUOTA_SUPPORT
3718 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3720 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3722 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3723 #ifdef HAVE_QUOTA_SUPPORT
3724 cfs_curproc_cap_unpack(save);
3726 osd_object_put(env, child);
3728 rc = PTR_ERR(child);
3731 LASSERT(osd_invariant(obj));
3736 * Initialize osd Iterator for given osd index object.
3738 * \param dt osd index object
3741 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3742 struct dt_object *dt,
3744 struct lustre_capa *capa)
3746 struct osd_it_iam *it;
3747 struct osd_thread_info *oti = osd_oti_get(env);
3748 struct osd_object *obj = osd_dt_obj(dt);
3749 struct lu_object *lo = &dt->do_lu;
3750 struct iam_path_descr *ipd;
3751 struct iam_container *bag = &obj->oo_dir->od_container;
3753 LASSERT(lu_object_exists(lo));
3755 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3756 return ERR_PTR(-EACCES);
3759 ipd = osd_it_ipd_get(env, bag);
3760 if (likely(ipd != NULL)) {
3764 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3765 return (struct dt_it *)it;
3767 return ERR_PTR(-ENOMEM);
3771 * free given Iterator.
3774 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3776 struct osd_it_iam *it = (struct osd_it_iam *)di;
3777 struct osd_object *obj = it->oi_obj;
3779 iam_it_fini(&it->oi_it);
3780 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3781 lu_object_put(env, &obj->oo_dt.do_lu);
3785 * Move Iterator to record specified by \a key
3787 * \param di osd iterator
3788 * \param key key for index
3790 * \retval +ve di points to record with least key not larger than key
3791 * \retval 0 di points to exact matched key
3792 * \retval -ve failure
3795 static int osd_it_iam_get(const struct lu_env *env,
3796 struct dt_it *di, const struct dt_key *key)
3798 struct osd_it_iam *it = (struct osd_it_iam *)di;
3800 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3806 * \param di osd iterator
3809 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3811 struct osd_it_iam *it = (struct osd_it_iam *)di;
3813 iam_it_put(&it->oi_it);
3817 * Move iterator by one record
3819 * \param di osd iterator
3821 * \retval +1 end of container reached
3823 * \retval -ve failure
3826 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3828 struct osd_it_iam *it = (struct osd_it_iam *)di;
3830 return iam_it_next(&it->oi_it);
3834 * Return pointer to the key under iterator.
3837 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3838 const struct dt_it *di)
3840 struct osd_it_iam *it = (struct osd_it_iam *)di;
3842 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3846 * Return size of key under iterator (in bytes)
3849 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3851 struct osd_it_iam *it = (struct osd_it_iam *)di;
3853 return iam_it_key_size(&it->oi_it);
3856 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3861 struct luda_type *lt;
3862 const unsigned align = sizeof(struct luda_type) - 1;
3864 /* check if file type is required */
3865 if (attr & LUDA_TYPE) {
3866 len = (len + align) & ~align;
3868 lt = (void *) ent->lde_name + len;
3869 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3870 ent->lde_attrs |= LUDA_TYPE;
3873 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3877 * build lu direct from backend fs dirent.
3880 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3888 fid_cpu_to_le(&ent->lde_fid, fid);
3889 ent->lde_attrs = LUDA_FID;
3891 ent->lde_hash = cpu_to_le64(offset);
3892 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3894 strncpy(ent->lde_name, name, namelen);
3895 ent->lde_namelen = cpu_to_le16(namelen);
3897 /* append lustre attributes */
3898 osd_it_append_attrs(ent, attr, namelen, type);
3902 * Return pointer to the record under iterator.
3904 static int osd_it_iam_rec(const struct lu_env *env,
3905 const struct dt_it *di,
3906 struct dt_rec *dtrec,
3909 struct osd_it_iam *it = (struct osd_it_iam *)di;
3910 struct osd_thread_info *info = osd_oti_get(env);
3911 struct lu_fid *fid = &info->oti_fid;
3912 const struct osd_fid_pack *rec;
3913 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
3919 name = (char *)iam_it_key_get(&it->oi_it);
3921 RETURN(PTR_ERR(name));
3923 namelen = iam_it_key_size(&it->oi_it);
3925 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3927 RETURN(PTR_ERR(rec));
3929 rc = osd_fid_unpack(fid, rec);
3933 hash = iam_it_store(&it->oi_it);
3935 /* IAM does not store object type in IAM index (dir) */
3936 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3943 * Returns cookie for current Iterator position.
3945 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3947 struct osd_it_iam *it = (struct osd_it_iam *)di;
3949 return iam_it_store(&it->oi_it);
3953 * Restore iterator from cookie.
3955 * \param di osd iterator
3956 * \param hash Iterator location cookie
3958 * \retval +ve di points to record with least key not larger than key.
3959 * \retval 0 di points to exact matched key
3960 * \retval -ve failure
3963 static int osd_it_iam_load(const struct lu_env *env,
3964 const struct dt_it *di, __u64 hash)
3966 struct osd_it_iam *it = (struct osd_it_iam *)di;
3968 return iam_it_load(&it->oi_it, hash);
3971 static const struct dt_index_operations osd_index_iam_ops = {
3972 .dio_lookup = osd_index_iam_lookup,
3973 .dio_declare_insert = osd_index_declare_iam_insert,
3974 .dio_insert = osd_index_iam_insert,
3975 .dio_declare_delete = osd_index_declare_iam_delete,
3976 .dio_delete = osd_index_iam_delete,
3978 .init = osd_it_iam_init,
3979 .fini = osd_it_iam_fini,
3980 .get = osd_it_iam_get,
3981 .put = osd_it_iam_put,
3982 .next = osd_it_iam_next,
3983 .key = osd_it_iam_key,
3984 .key_size = osd_it_iam_key_size,
3985 .rec = osd_it_iam_rec,
3986 .store = osd_it_iam_store,
3987 .load = osd_it_iam_load
3992 * Creates or initializes iterator context.
3994 * \retval struct osd_it_ea, iterator structure on success
3997 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3998 struct dt_object *dt,
4000 struct lustre_capa *capa)
4002 struct osd_object *obj = osd_dt_obj(dt);
4003 struct osd_thread_info *info = osd_oti_get(env);
4004 struct osd_it_ea *it = &info->oti_it_ea;
4005 struct lu_object *lo = &dt->do_lu;
4006 struct dentry *obj_dentry = &info->oti_it_dentry;
4008 LASSERT(lu_object_exists(lo));
4010 obj_dentry->d_inode = obj->oo_inode;
4011 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4012 obj_dentry->d_name.hash = 0;
4014 it->oie_rd_dirent = 0;
4015 it->oie_it_dirent = 0;
4016 it->oie_dirent = NULL;
4017 it->oie_buf = info->oti_it_ea_buf;
4019 it->oie_file.f_pos = 0;
4020 it->oie_file.f_dentry = obj_dentry;
4021 if (attr & LUDA_64BITHASH)
4022 it->oie_file.f_flags = O_64BITHASH;
4024 it->oie_file.f_flags = O_32BITHASH;
4025 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
4026 it->oie_file.f_op = obj->oo_inode->i_fop;
4027 it->oie_file.private_data = NULL;
4029 RETURN((struct dt_it *) it);
4033 * Destroy or finishes iterator context.
4035 * \param di iterator structure to be destroyed
4037 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4039 struct osd_it_ea *it = (struct osd_it_ea *)di;
4040 struct osd_object *obj = it->oie_obj;
4041 struct inode *inode = obj->oo_inode;
4044 it->oie_file.f_op->release(inode, &it->oie_file);
4045 lu_object_put(env, &obj->oo_dt.do_lu);
4050 * It position the iterator at given key, so that next lookup continues from
4051 * that key Or it is similar to dio_it->load() but based on a key,
4052 * rather than file position.
4054 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4057 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4059 static int osd_it_ea_get(const struct lu_env *env,
4060 struct dt_it *di, const struct dt_key *key)
4062 struct osd_it_ea *it = (struct osd_it_ea *)di;
4065 LASSERT(((const char *)key)[0] == '\0');
4066 it->oie_file.f_pos = 0;
4067 it->oie_rd_dirent = 0;
4068 it->oie_it_dirent = 0;
4069 it->oie_dirent = NULL;
4077 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4082 * It is called internally by ->readdir(). It fills the
4083 * iterator's in-memory data structure with required
4084 * information i.e. name, namelen, rec_size etc.
4086 * \param buf in which information to be filled in.
4087 * \param name name of the file in given dir
4089 * \retval 0 on success
4090 * \retval 1 on buffer full
4092 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4093 loff_t offset, __u64 ino,
4096 struct osd_it_ea *it = (struct osd_it_ea *)buf;
4097 struct osd_it_ea_dirent *ent = it->oie_dirent;
4098 struct lu_fid *fid = &ent->oied_fid;
4099 struct osd_fid_pack *rec;
4102 /* this should never happen */
4103 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4104 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4108 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4112 if (d_type & LDISKFS_DIRENT_LUFID) {
4113 rec = (struct osd_fid_pack*) (name + namelen + 1);
4115 if (osd_fid_unpack(fid, rec) != 0)
4118 d_type &= ~LDISKFS_DIRENT_LUFID;
4123 ent->oied_ino = ino;
4124 ent->oied_off = offset;
4125 ent->oied_namelen = namelen;
4126 ent->oied_type = d_type;
4128 memcpy(ent->oied_name, name, namelen);
4130 it->oie_rd_dirent++;
4131 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4136 * Calls ->readdir() to load a directory entry at a time
4137 * and stored it in iterator's in-memory data structure.
4139 * \param di iterator's in memory structure
4141 * \retval 0 on success
4142 * \retval -ve on error
4144 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4145 const struct dt_it *di)
4147 struct osd_it_ea *it = (struct osd_it_ea *)di;
4148 struct osd_object *obj = it->oie_obj;
4149 struct inode *inode = obj->oo_inode;
4150 struct htree_lock *hlock = NULL;
4154 it->oie_dirent = it->oie_buf;
4155 it->oie_rd_dirent = 0;
4157 if (obj->oo_hl_head != NULL) {
4158 hlock = osd_oti_get(env)->oti_hlock;
4159 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4160 inode, LDISKFS_HLOCK_READDIR);
4162 cfs_down_read(&obj->oo_ext_idx_sem);
4165 result = inode->i_fop->readdir(&it->oie_file, it,
4166 (filldir_t) osd_ldiskfs_filldir);
4169 ldiskfs_htree_unlock(hlock);
4171 cfs_up_read(&obj->oo_ext_idx_sem);
4173 if (it->oie_rd_dirent == 0) {
4176 it->oie_dirent = it->oie_buf;
4177 it->oie_it_dirent = 1;
4184 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4185 * to load a directory entry at a time and stored it in
4186 * iterator's in-memory data structure.
4188 * \param di iterator's in memory structure
4190 * \retval +ve iterator reached to end
4191 * \retval 0 iterator not reached to end
4192 * \retval -ve on error
4194 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4196 struct osd_it_ea *it = (struct osd_it_ea *)di;
4201 if (it->oie_it_dirent < it->oie_rd_dirent) {
4203 (void *) it->oie_dirent +
4204 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4205 it->oie_dirent->oied_namelen);
4206 it->oie_it_dirent++;
4209 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
4212 rc = osd_ldiskfs_it_fill(env, di);
4219 * Returns the key at current position from iterator's in memory structure.
4221 * \param di iterator's in memory structure
4223 * \retval key i.e. struct dt_key on success
4225 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4226 const struct dt_it *di)
4228 struct osd_it_ea *it = (struct osd_it_ea *)di;
4230 RETURN((struct dt_key *)it->oie_dirent->oied_name);
4234 * Returns the key's size at current position from iterator's in memory structure.
4236 * \param di iterator's in memory structure
4238 * \retval key_size i.e. struct dt_key on success
4240 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4242 struct osd_it_ea *it = (struct osd_it_ea *)di;
4244 RETURN(it->oie_dirent->oied_namelen);
4249 * Returns the value (i.e. fid/igif) at current position from iterator's
4250 * in memory structure.
4252 * \param di struct osd_it_ea, iterator's in memory structure
4253 * \param attr attr requested for dirent.
4254 * \param lde lustre dirent
4256 * \retval 0 no error and \param lde has correct lustre dirent.
4257 * \retval -ve on error
4259 static inline int osd_it_ea_rec(const struct lu_env *env,
4260 const struct dt_it *di,
4261 struct dt_rec *dtrec,
4264 struct osd_it_ea *it = (struct osd_it_ea *)di;
4265 struct osd_object *obj = it->oie_obj;
4266 struct lu_fid *fid = &it->oie_dirent->oied_fid;
4267 struct lu_dirent *lde = (struct lu_dirent *)dtrec;
4272 if (!fid_is_sane(fid))
4273 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
4276 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
4277 it->oie_dirent->oied_name,
4278 it->oie_dirent->oied_namelen,
4279 it->oie_dirent->oied_type,
4285 * Returns a cookie for current position of the iterator head, so that
4286 * user can use this cookie to load/start the iterator next time.
4288 * \param di iterator's in memory structure
4290 * \retval cookie for current position, on success
4292 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
4294 struct osd_it_ea *it = (struct osd_it_ea *)di;
4296 RETURN(it->oie_dirent->oied_off);
4300 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4301 * to load a directory entry at a time and stored it i inn,
4302 * in iterator's in-memory data structure.
4304 * \param di struct osd_it_ea, iterator's in memory structure
4306 * \retval +ve on success
4307 * \retval -ve on error
4309 static int osd_it_ea_load(const struct lu_env *env,
4310 const struct dt_it *di, __u64 hash)
4312 struct osd_it_ea *it = (struct osd_it_ea *)di;
4316 it->oie_file.f_pos = hash;
4318 rc = osd_ldiskfs_it_fill(env, di);
4326 * Index lookup function for interoperability mode (b11826).
4328 * \param key, key i.e. file name to be searched
4330 * \retval +ve, on success
4331 * \retval -ve, on error
4333 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
4334 struct dt_rec *rec, const struct dt_key *key,
4335 struct lustre_capa *capa)
4337 struct osd_object *obj = osd_dt_obj(dt);
4342 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
4343 LINVRNT(osd_invariant(obj));
4345 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
4348 rc = osd_ea_lookup_rec(env, obj, rec, key);
4356 * Index and Iterator operations for interoperability
4357 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
4359 static const struct dt_index_operations osd_index_ea_ops = {
4360 .dio_lookup = osd_index_ea_lookup,
4361 .dio_declare_insert = osd_index_declare_ea_insert,
4362 .dio_insert = osd_index_ea_insert,
4363 .dio_declare_delete = osd_index_declare_ea_delete,
4364 .dio_delete = osd_index_ea_delete,
4366 .init = osd_it_ea_init,
4367 .fini = osd_it_ea_fini,
4368 .get = osd_it_ea_get,
4369 .put = osd_it_ea_put,
4370 .next = osd_it_ea_next,
4371 .key = osd_it_ea_key,
4372 .key_size = osd_it_ea_key_size,
4373 .rec = osd_it_ea_rec,
4374 .store = osd_it_ea_store,
4375 .load = osd_it_ea_load
4379 static void *osd_key_init(const struct lu_context *ctx,
4380 struct lu_context_key *key)
4382 struct osd_thread_info *info;
4384 OBD_ALLOC_PTR(info);
4386 return ERR_PTR(-ENOMEM);
4388 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4389 if (info->oti_it_ea_buf == NULL)
4392 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
4394 info->oti_hlock = ldiskfs_htree_lock_alloc();
4395 if (info->oti_hlock == NULL)
4401 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4404 return ERR_PTR(-ENOMEM);
4407 static void osd_key_fini(const struct lu_context *ctx,
4408 struct lu_context_key *key, void* data)
4410 struct osd_thread_info *info = data;
4412 if (info->oti_hlock != NULL)
4413 ldiskfs_htree_lock_free(info->oti_hlock);
4414 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
4418 static void osd_key_exit(const struct lu_context *ctx,
4419 struct lu_context_key *key, void *data)
4421 struct osd_thread_info *info = data;
4423 LASSERT(info->oti_r_locks == 0);
4424 LASSERT(info->oti_w_locks == 0);
4425 LASSERT(info->oti_txns == 0);
4428 /* type constructor/destructor: osd_type_init, osd_type_fini */
4429 LU_TYPE_INIT_FINI(osd, &osd_key);
4431 static struct lu_context_key osd_key = {
4432 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
4433 .lct_init = osd_key_init,
4434 .lct_fini = osd_key_fini,
4435 .lct_exit = osd_key_exit
4439 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
4440 const char *name, struct lu_device *next)
4442 return osd_procfs_init(osd_dev(d), name);
4445 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
4447 struct osd_thread_info *info = osd_oti_get(env);
4449 if (o->od_obj_area != NULL) {
4450 lu_object_put(env, &o->od_obj_area->do_lu);
4451 o->od_obj_area = NULL;
4453 if (o->od_oi_table != NULL)
4454 osd_oi_fini(info, &o->od_oi_table, o->od_oi_count);
4459 static int osd_mount(const struct lu_env *env,
4460 struct osd_device *o, struct lustre_cfg *cfg)
4462 struct lustre_mount_info *lmi;
4463 const char *dev = lustre_cfg_string(cfg, 0);
4464 struct lustre_disk_data *ldd;
4465 struct lustre_sb_info *lsi;
4468 if (o->od_mount != NULL) {
4469 CERROR("Already mounted (%s)\n", dev);
4474 lmi = server_get_mount(dev);
4476 CERROR("Cannot get mount info for %s!\n", dev);
4480 LASSERT(lmi != NULL);
4481 /* save lustre_mount_info in dt_device */
4484 lsi = s2lsi(lmi->lmi_sb);
4487 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
4489 LCONSOLE_WARN("OSD: IAM mode enabled\n");
4493 o->od_obj_area = NULL;
4497 static struct lu_device *osd_device_fini(const struct lu_env *env,
4498 struct lu_device *d)
4503 shrink_dcache_sb(osd_sb(osd_dev(d)));
4504 osd_sync(env, lu2dt_dev(d));
4506 rc = osd_procfs_fini(osd_dev(d));
4508 CERROR("proc fini error %d \n", rc);
4509 RETURN (ERR_PTR(rc));
4512 if (osd_dev(d)->od_mount)
4513 server_put_mount(osd_dev(d)->od_mount->lmi_name,
4514 osd_dev(d)->od_mount->lmi_mnt);
4515 osd_dev(d)->od_mount = NULL;
4520 static struct lu_device *osd_device_alloc(const struct lu_env *env,
4521 struct lu_device_type *t,
4522 struct lustre_cfg *cfg)
4524 struct lu_device *l;
4525 struct osd_device *o;
4531 result = dt_device_init(&o->od_dt_dev, t);
4534 l->ld_ops = &osd_lu_ops;
4535 o->od_dt_dev.dd_ops = &osd_dt_ops;
4536 cfs_spin_lock_init(&o->od_osfs_lock);
4537 o->od_osfs_age = cfs_time_shift_64(-1000);
4538 o->od_capa_hash = init_capa_hash();
4539 if (o->od_capa_hash == NULL) {
4540 dt_device_fini(&o->od_dt_dev);
4541 l = ERR_PTR(-ENOMEM);
4544 l = ERR_PTR(result);
4549 l = ERR_PTR(-ENOMEM);
4553 static struct lu_device *osd_device_free(const struct lu_env *env,
4554 struct lu_device *d)
4556 struct osd_device *o = osd_dev(d);
4559 cleanup_capa_hash(o->od_capa_hash);
4560 dt_device_fini(&o->od_dt_dev);
4565 static int osd_process_config(const struct lu_env *env,
4566 struct lu_device *d, struct lustre_cfg *cfg)
4568 struct osd_device *o = osd_dev(d);
4572 switch(cfg->lcfg_command) {
4574 err = osd_mount(env, o, cfg);
4577 err = osd_shutdown(env, o);
4586 static int osd_recovery_complete(const struct lu_env *env,
4587 struct lu_device *d)
4592 static int osd_prepare(const struct lu_env *env,
4593 struct lu_device *pdev,
4594 struct lu_device *dev)
4596 struct osd_device *osd = osd_dev(dev);
4597 struct lustre_sb_info *lsi;
4598 struct lustre_disk_data *ldd;
4599 struct lustre_mount_info *lmi;
4600 struct osd_thread_info *oti = osd_oti_get(env);
4601 struct dt_object *d;
4605 /* 1. initialize oi before any file create or file open */
4606 result = osd_oi_init(oti, &osd->od_oi_table,
4607 &osd->od_dt_dev, lu2md_dev(pdev));
4611 LASSERT(result > 0);
4612 osd->od_oi_count = result;
4614 lmi = osd->od_mount;
4615 lsi = s2lsi(lmi->lmi_sb);
4618 /* 2. setup local objects */
4619 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4623 /* 3. open remote object dir */
4624 d = dt_store_open(env, lu2dt_dev(dev), "",
4625 remote_obj_dir, &oti->oti_fid);
4627 osd->od_obj_area = d;
4630 result = PTR_ERR(d);
4631 osd->od_obj_area = NULL;
4638 static const struct lu_object_operations osd_lu_obj_ops = {
4639 .loo_object_init = osd_object_init,
4640 .loo_object_delete = osd_object_delete,
4641 .loo_object_release = osd_object_release,
4642 .loo_object_free = osd_object_free,
4643 .loo_object_print = osd_object_print,
4644 .loo_object_invariant = osd_object_invariant
4647 static const struct lu_device_operations osd_lu_ops = {
4648 .ldo_object_alloc = osd_object_alloc,
4649 .ldo_process_config = osd_process_config,
4650 .ldo_recovery_complete = osd_recovery_complete,
4651 .ldo_prepare = osd_prepare,
4654 static const struct lu_device_type_operations osd_device_type_ops = {
4655 .ldto_init = osd_type_init,
4656 .ldto_fini = osd_type_fini,
4658 .ldto_start = osd_type_start,
4659 .ldto_stop = osd_type_stop,
4661 .ldto_device_alloc = osd_device_alloc,
4662 .ldto_device_free = osd_device_free,
4664 .ldto_device_init = osd_device_init,
4665 .ldto_device_fini = osd_device_fini
4668 static struct lu_device_type osd_device_type = {
4669 .ldt_tags = LU_DEVICE_DT,
4670 .ldt_name = LUSTRE_OSD_NAME,
4671 .ldt_ops = &osd_device_type_ops,
4672 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4676 * lprocfs legacy support.
4678 static struct obd_ops osd_obd_device_ops = {
4679 .o_owner = THIS_MODULE
4682 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4683 .llod_name = remote_obj_dir,
4684 .llod_oid = OSD_REM_OBJ_DIR_OID,
4686 .llod_feat = &dt_directory_features,
4689 static int __init osd_mod_init(void)
4691 struct lprocfs_static_vars lvars;
4694 llo_local_obj_register(&llod_osd_rem_obj_dir);
4695 lprocfs_osd_init_vars(&lvars);
4696 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4697 LUSTRE_OSD_NAME, &osd_device_type);
4700 static void __exit osd_mod_exit(void)
4702 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4703 class_unregister_type(LUSTRE_OSD_NAME);
4706 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4707 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4708 MODULE_LICENSE("GPL");
4710 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);