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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
50 /* LUSTRE_VERSION_CODE */
51 #include <lustre_ver.h>
52 /* prerequisite for linux/xattr.h */
53 #include <linux/types.h>
54 /* prerequisite for linux/xattr.h */
56 /* XATTR_{REPLACE,CREATE} */
57 #include <linux/xattr.h>
59 * XXX temporary stuff: direct access to ldiskfs/jdb. Interface between osd
60 * and file system is not yet specified.
62 /* handle_t, journal_start(), journal_stop() */
63 #include <linux/jbd.h>
65 #include <linux/ldiskfs_fs.h>
66 #include <linux/ldiskfs_jbd.h>
71 * struct OBD_{ALLOC,FREE}*()
74 #include <obd_support.h>
75 /* struct ptlrpc_thread */
76 #include <lustre_net.h>
79 #include <lustre_fid.h>
81 #include "osd_internal.h"
84 /* llo_* api support */
85 #include <md_object.h>
87 static const char dot[] = ".";
88 static const char dotdot[] = "..";
89 static const char remote_obj_dir[] = "REM_OBJ_DIR";
91 struct osd_directory {
92 struct iam_container od_container;
93 struct iam_descr od_descr;
97 struct dt_object oo_dt;
99 * Inode for file system object represented by this osd_object. This
100 * inode is pinned for the whole duration of lu_object life.
102 * Not modified concurrently (either setup early during object
103 * creation, or assigned by osd_object_create() under write lock).
105 struct inode *oo_inode;
107 * to protect index ops.
109 struct rw_semaphore oo_ext_idx_sem;
110 struct rw_semaphore oo_sem;
111 struct osd_directory *oo_dir;
112 /** protects inode attributes. */
115 * Following two members are used to indicate the presence of dot and
116 * dotdot in the given directory. This is required for interop mode
119 int oo_compat_dot_created;
120 int oo_compat_dotdot_created;
122 const struct lu_env *oo_owner;
123 #ifdef CONFIG_LOCKDEP
124 struct lockdep_map oo_dep_map;
128 static const struct lu_object_operations osd_lu_obj_ops;
129 static const struct lu_device_operations osd_lu_ops;
130 static struct lu_context_key osd_key;
131 static const struct dt_object_operations osd_obj_ops;
132 static const struct dt_object_operations osd_obj_ea_ops;
133 static const struct dt_body_operations osd_body_ops;
134 static const struct dt_index_operations osd_index_iam_ops;
135 static const struct dt_index_operations osd_index_ea_ops;
138 struct thandle ot_super;
140 struct journal_callback ot_jcb;
141 /* Link to the device, for debugging. */
142 struct lu_ref_link *ot_dev_link;
149 static int lu_device_is_osd(const struct lu_device *d)
151 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
154 static struct osd_device *osd_dt_dev(const struct dt_device *d)
156 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
157 return container_of0(d, struct osd_device, od_dt_dev);
160 static struct osd_device *osd_dev(const struct lu_device *d)
162 LASSERT(lu_device_is_osd(d));
163 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
166 static struct osd_device *osd_obj2dev(const struct osd_object *o)
168 return osd_dev(o->oo_dt.do_lu.lo_dev);
171 static struct super_block *osd_sb(const struct osd_device *dev)
173 return dev->od_mount->lmi_mnt->mnt_sb;
176 static int osd_object_is_root(const struct osd_object *obj)
178 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
181 static struct osd_object *osd_obj(const struct lu_object *o)
183 LASSERT(lu_device_is_osd(o->lo_dev));
184 return container_of0(o, struct osd_object, oo_dt.do_lu);
187 static struct osd_object *osd_dt_obj(const struct dt_object *d)
189 return osd_obj(&d->do_lu);
192 static struct lu_device *osd2lu_dev(struct osd_device *osd)
194 return &osd->od_dt_dev.dd_lu_dev;
197 static journal_t *osd_journal(const struct osd_device *dev)
199 return LDISKFS_SB(osd_sb(dev))->s_journal;
202 static int osd_has_index(const struct osd_object *obj)
204 return obj->oo_dt.do_index_ops != NULL;
207 static int osd_object_invariant(const struct lu_object *l)
209 return osd_invariant(osd_obj(l));
212 #ifdef HAVE_QUOTA_SUPPORT
214 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
216 struct md_ucred *uc = md_ucred(env);
220 save->oc_uid = current->fsuid;
221 save->oc_gid = current->fsgid;
222 save->oc_cap = current->cap_effective;
223 current->fsuid = uc->mu_fsuid;
224 current->fsgid = uc->mu_fsgid;
225 current->cap_effective = uc->mu_cap;
229 osd_pop_ctxt(struct osd_ctxt *save)
231 current->fsuid = save->oc_uid;
232 current->fsgid = save->oc_gid;
233 current->cap_effective = save->oc_cap;
237 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
239 return lu_context_key_get(&env->le_ctx, &osd_key);
243 * Concurrency: doesn't matter
245 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
247 return osd_oti_get(env)->oti_r_locks > 0;
251 * Concurrency: doesn't matter
253 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
255 struct osd_thread_info *oti = osd_oti_get(env);
256 return oti->oti_w_locks > 0 && o->oo_owner == env;
260 * Concurrency: doesn't access mutable data
262 static int osd_root_get(const struct lu_env *env,
263 struct dt_device *dev, struct lu_fid *f)
267 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
268 lu_igif_build(f, inode->i_ino, inode->i_generation);
273 * OSD object methods.
277 * Concurrency: no concurrent access is possible that early in object
280 static struct lu_object *osd_object_alloc(const struct lu_env *env,
281 const struct lu_object_header *hdr,
284 struct osd_object *mo;
290 l = &mo->oo_dt.do_lu;
291 dt_object_init(&mo->oo_dt, NULL, d);
292 if (osd_dev(d)->od_iop_mode)
293 mo->oo_dt.do_ops = &osd_obj_ea_ops;
295 mo->oo_dt.do_ops = &osd_obj_ops;
297 l->lo_ops = &osd_lu_obj_ops;
298 init_rwsem(&mo->oo_sem);
299 init_rwsem(&mo->oo_ext_idx_sem);
300 spin_lock_init(&mo->oo_guard);
307 * retrieve object from backend ext fs.
309 static struct inode *osd_iget(struct osd_thread_info *info,
310 struct osd_device *dev,
311 const struct osd_inode_id *id)
315 inode = iget(osd_sb(dev), id->oii_ino);
317 CERROR("no inode\n");
318 inode = ERR_PTR(-EACCES);
319 } else if (id->oii_gen != OSD_OII_NOGEN &&
320 inode->i_generation != id->oii_gen) {
322 inode = ERR_PTR(-ESTALE);
323 } else if (inode->i_nlink == 0) {
324 /* due to parallel readdir and unlink,
325 * we can have dead inode here. */
326 CWARN("stale inode\n");
327 make_bad_inode(inode);
329 inode = ERR_PTR(-ESTALE);
330 } else if (is_bad_inode(inode)) {
331 CERROR("bad inode %lx\n",inode->i_ino);
333 inode = ERR_PTR(-ENOENT);
338 static int osd_fid_lookup(const struct lu_env *env,
339 struct osd_object *obj, const struct lu_fid *fid)
341 struct osd_thread_info *info;
342 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
343 struct osd_device *dev;
344 struct osd_inode_id *id;
349 LINVRNT(osd_invariant(obj));
350 LASSERT(obj->oo_inode == NULL);
351 LASSERT(fid_is_sane(fid));
353 * This assertion checks that osd layer sees only local
354 * fids. Unfortunately it is somewhat expensive (does a
355 * cache-lookup). Disabling it for production/acceptance-testing.
357 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
361 info = osd_oti_get(env);
366 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
369 result = osd_oi_lookup(info, oi, fid, id);
371 inode = osd_iget(info, dev, id);
372 if (!IS_ERR(inode)) {
373 obj->oo_inode = inode;
374 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
375 if (dev->od_iop_mode) {
376 obj->oo_compat_dot_created = 1;
377 obj->oo_compat_dotdot_created = 1;
382 * If fid wasn't found in oi, inode-less object is
383 * created, for which lu_object_exists() returns
384 * false. This is used in a (frequent) case when
385 * objects are created as locking anchors or
386 * place holders for objects yet to be created.
388 result = PTR_ERR(inode);
389 } else if (result == -ENOENT)
391 LINVRNT(osd_invariant(obj));
397 * Concurrency: shouldn't matter.
399 static void osd_object_init0(struct osd_object *obj)
401 LASSERT(obj->oo_inode != NULL);
402 obj->oo_dt.do_body_ops = &osd_body_ops;
403 obj->oo_dt.do_lu.lo_header->loh_attr |=
404 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
408 * Concurrency: no concurrent access is possible that early in object
411 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
412 const struct lu_object_conf *unused)
414 struct osd_object *obj = osd_obj(l);
417 LINVRNT(osd_invariant(obj));
419 result = osd_fid_lookup(env, obj, lu_object_fid(l));
421 if (obj->oo_inode != NULL)
422 osd_object_init0(obj);
424 LINVRNT(osd_invariant(obj));
429 * Concurrency: no concurrent access is possible that late in object
432 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
434 struct osd_object *obj = osd_obj(l);
436 LINVRNT(osd_invariant(obj));
438 dt_object_fini(&obj->oo_dt);
445 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
446 const struct iam_container *bag)
448 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
449 osd_oti_get(env)->oti_it_ipd);
452 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
453 const struct iam_container *bag)
455 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
456 osd_oti_get(env)->oti_idx_ipd);
459 static void osd_ipd_put(const struct lu_env *env,
460 const struct iam_container *bag,
461 struct iam_path_descr *ipd)
463 bag->ic_descr->id_ops->id_ipd_free(ipd);
467 * Concurrency: no concurrent access is possible that late in object
470 static void osd_index_fini(struct osd_object *o)
472 struct iam_container *bag;
474 if (o->oo_dir != NULL) {
475 bag = &o->oo_dir->od_container;
476 if (o->oo_inode != NULL) {
477 if (bag->ic_object == o->oo_inode)
478 iam_container_fini(bag);
480 OBD_FREE_PTR(o->oo_dir);
486 * Concurrency: no concurrent access is possible that late in object
487 * life-cycle (for all existing callers, that is. New callers have to provide
488 * their own locking.)
490 static int osd_inode_unlinked(const struct inode *inode)
492 return inode->i_nlink == 0;
496 OSD_TXN_OI_DELETE_CREDITS = 20,
497 OSD_TXN_INODE_DELETE_CREDITS = 20
505 * Concurrency: doesn't access mutable data.
507 static int osd_param_is_sane(const struct osd_device *dev,
508 const struct txn_param *param)
510 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
514 * Concurrency: shouldn't matter.
516 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
518 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
519 struct thandle *th = &oh->ot_super;
520 struct dt_device *dev = th->th_dev;
521 struct lu_device *lud = &dev->dd_lu_dev;
523 LASSERT(dev != NULL);
524 LASSERT(oh->ot_handle == NULL);
527 CERROR("transaction @0x%p commit error: %d\n", th, error);
529 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
531 * This od_env_for_commit is only for commit usage. see
534 lu_context_enter(&env->le_ctx);
535 dt_txn_hook_commit(env, th);
536 lu_context_exit(&env->le_ctx);
539 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
543 lu_context_exit(&th->th_ctx);
544 lu_context_fini(&th->th_ctx);
549 * Concurrency: shouldn't matter.
551 static struct thandle *osd_trans_start(const struct lu_env *env,
555 struct osd_device *dev = osd_dt_dev(d);
557 struct osd_thandle *oh;
563 hook_res = dt_txn_hook_start(env, d, p);
565 RETURN(ERR_PTR(hook_res));
567 if (osd_param_is_sane(dev, p)) {
568 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
570 struct osd_thread_info *oti = osd_oti_get(env);
573 * XXX temporary stuff. Some abstraction layer should
577 jh = journal_start(osd_journal(dev), p->tp_credits);
583 jh->h_sync = p->tp_sync;
584 lu_device_get(&d->dd_lu_dev);
585 oh->ot_dev_link = lu_ref_add
586 (&d->dd_lu_dev.ld_reference,
588 /* add commit callback */
589 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
590 lu_context_enter(&th->th_ctx);
591 journal_callback_set(jh, osd_trans_commit_cb,
592 (struct journal_callback *)&oh->ot_jcb);
593 LASSERT(oti->oti_txns == 0);
594 LASSERT(oti->oti_r_locks == 0);
595 LASSERT(oti->oti_w_locks == 0);
602 th = ERR_PTR(-ENOMEM);
604 CERROR("Invalid transaction parameters\n");
605 th = ERR_PTR(-EINVAL);
612 * Concurrency: shouldn't matter.
614 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
617 struct osd_thandle *oh;
618 struct osd_thread_info *oti = osd_oti_get(env);
622 oh = container_of0(th, struct osd_thandle, ot_super);
623 if (oh->ot_handle != NULL) {
624 handle_t *hdl = oh->ot_handle;
626 LASSERT(oti->oti_txns == 1);
628 LASSERT(oti->oti_r_locks == 0);
629 LASSERT(oti->oti_w_locks == 0);
630 result = dt_txn_hook_stop(env, th);
632 CERROR("Failure in transaction hook: %d\n", result);
633 oh->ot_handle = NULL;
634 result = journal_stop(hdl);
636 CERROR("Failure to stop transaction: %d\n", result);
642 * Concurrency: no concurrent access is possible that late in object
645 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
647 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
648 struct osd_device *osd = osd_obj2dev(obj);
649 struct osd_thread_info *oti = osd_oti_get(env);
650 struct txn_param *prm = &oti->oti_txn;
651 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
655 lu_env_init(env_del_obj, LCT_DT_THREAD);
656 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
657 OSD_TXN_INODE_DELETE_CREDITS);
658 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
660 result = osd_oi_delete(osd_oti_get(env_del_obj),
661 &osd->od_oi, fid, th);
662 osd_trans_stop(env_del_obj, th);
664 result = PTR_ERR(th);
666 lu_env_fini(env_del_obj);
671 * Called just before object is freed. Releases all resources except for
672 * object itself (that is released by osd_object_free()).
674 * Concurrency: no concurrent access is possible that late in object
677 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
679 struct osd_object *obj = osd_obj(l);
680 struct inode *inode = obj->oo_inode;
682 LINVRNT(osd_invariant(obj));
685 * If object is unlinked remove fid->ino mapping from object index.
692 if (osd_inode_unlinked(inode)) {
693 result = osd_inode_remove(env, obj);
695 LU_OBJECT_DEBUG(D_ERROR, env, l,
696 "Failed to cleanup: %d\n",
701 obj->oo_inode = NULL;
706 * Concurrency: ->loo_object_release() is called under site spin-lock.
708 static void osd_object_release(const struct lu_env *env,
711 struct osd_object *o = osd_obj(l);
713 LASSERT(!lu_object_is_dying(l->lo_header));
714 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
715 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
719 * Concurrency: shouldn't matter.
721 static int osd_object_print(const struct lu_env *env, void *cookie,
722 lu_printer_t p, const struct lu_object *l)
724 struct osd_object *o = osd_obj(l);
727 if (o->oo_dir != NULL)
728 d = o->oo_dir->od_container.ic_descr;
731 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
733 o->oo_inode ? o->oo_inode->i_ino : 0UL,
734 o->oo_inode ? o->oo_inode->i_generation : 0,
735 d ? d->id_ops->id_name : "plain");
739 * Concurrency: shouldn't matter.
741 int osd_statfs(const struct lu_env *env, struct dt_device *d,
744 struct osd_device *osd = osd_dt_dev(d);
745 struct super_block *sb = osd_sb(osd);
748 spin_lock(&osd->od_osfs_lock);
750 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
751 result = ll_do_statfs(sb, &osd->od_kstatfs);
752 if (likely(result == 0)) /* N.B. statfs can't really fail */
753 osd->od_osfs_age = cfs_time_current_64();
756 if (likely(result == 0))
757 *sfs = osd->od_kstatfs;
758 spin_unlock(&osd->od_osfs_lock);
764 * Concurrency: doesn't access mutable data.
766 static void osd_conf_get(const struct lu_env *env,
767 const struct dt_device *dev,
768 struct dt_device_param *param)
771 * XXX should be taken from not-yet-existing fs abstraction layer.
773 param->ddp_max_name_len = LDISKFS_NAME_LEN;
774 param->ddp_max_nlink = LDISKFS_LINK_MAX;
775 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
779 * Helper function to get and fill the buffer with input values.
781 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
785 buf = &osd_oti_get(env)->oti_buf;
792 * Concurrency: shouldn't matter.
794 static int osd_sync(const struct lu_env *env, struct dt_device *d)
796 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
797 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
801 * Start commit for OSD device.
803 * An implementation of dt_commit_async method for OSD device.
804 * Asychronously starts underlayng fs sync and thereby a transaction
807 * \param env environment
810 * \see dt_device_operations
812 static int osd_commit_async(const struct lu_env *env,
815 struct super_block *s = osd_sb(osd_dt_dev(d));
818 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
819 RETURN(s->s_op->sync_fs(s, 0));
823 * Concurrency: shouldn't matter.
825 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
827 static void osd_ro(const struct lu_env *env, struct dt_device *d)
831 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
833 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
834 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
840 * Concurrency: serialization provided by callers.
842 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
843 int mode, unsigned long timeout, __u32 alg,
844 struct lustre_capa_key *keys)
846 struct osd_device *dev = osd_dt_dev(d);
849 dev->od_fl_capa = mode;
850 dev->od_capa_timeout = timeout;
851 dev->od_capa_alg = alg;
852 dev->od_capa_keys = keys;
857 * Concurrency: serialization provided by callers.
859 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
860 struct dt_quota_ctxt *ctxt, void *data)
862 struct obd_device *obd = (void *)ctxt;
863 struct vfsmount *mnt = (struct vfsmount *)data;
866 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
867 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
868 obd->obd_lvfs_ctxt.pwdmnt = mnt;
869 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
870 obd->obd_lvfs_ctxt.fs = get_ds();
876 * Note: we do not count into QUOTA here.
877 * If we mount with --data_journal we may need more.
879 static const int osd_dto_credits_noquota[DTO_NR] = {
882 * INDEX_EXTRA_TRANS_BLOCKS(8) +
883 * SINGLEDATA_TRANS_BLOCKS(8)
884 * XXX Note: maybe iam need more, since iam have more level than
887 [DTO_INDEX_INSERT] = 16,
888 [DTO_INDEX_DELETE] = 16,
892 [DTO_IDNEX_UPDATE] = 16,
894 * Create a object. The same as create object in EXT3.
895 * DATA_TRANS_BLOCKS(14) +
896 * INDEX_EXTRA_BLOCKS(8) +
897 * 3(inode bits, groups, GDT)
899 [DTO_OBJECT_CREATE] = 25,
903 [DTO_OBJECT_DELETE] = 25,
906 * 3(inode bits, group, GDT)
908 [DTO_ATTR_SET_BASE] = 3,
910 * Xattr set. The same as xattr of EXT3.
911 * DATA_TRANS_BLOCKS(14)
912 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
913 * also counted in. Do not know why?
915 [DTO_XATTR_SET] = 14,
918 * creadits for inode change during write.
920 [DTO_WRITE_BASE] = 3,
922 * credits for single block write.
924 [DTO_WRITE_BLOCK] = 14,
926 * Attr set credits for chown.
927 * 3 (inode bit, group, GDT)
929 [DTO_ATTR_SET_CHOWN]= 3
933 * Note: we count into QUOTA here.
934 * If we mount with --data_journal we may need more.
936 static const int osd_dto_credits_quota[DTO_NR] = {
938 * INDEX_EXTRA_TRANS_BLOCKS(8) +
939 * SINGLEDATA_TRANS_BLOCKS(8) +
940 * 2 * QUOTA_TRANS_BLOCKS(2)
942 [DTO_INDEX_INSERT] = 20,
944 * INDEX_EXTRA_TRANS_BLOCKS(8) +
945 * SINGLEDATA_TRANS_BLOCKS(8) +
946 * 2 * QUOTA_TRANS_BLOCKS(2)
948 [DTO_INDEX_DELETE] = 20,
952 [DTO_IDNEX_UPDATE] = 16,
954 * Create a object. Same as create object in EXT3 filesystem.
955 * DATA_TRANS_BLOCKS(16) +
956 * INDEX_EXTRA_BLOCKS(8) +
957 * 3(inode bits, groups, GDT) +
958 * 2 * QUOTA_INIT_BLOCKS(25)
960 [DTO_OBJECT_CREATE] = 77,
963 * DATA_TRANS_BLOCKS(16) +
964 * INDEX_EXTRA_BLOCKS(8) +
965 * 3(inode bits, groups, GDT) +
968 [DTO_OBJECT_DELETE] = 27,
971 * 3 (inode bit, group, GDT) +
973 [DTO_ATTR_SET_BASE] = 3,
975 * Xattr set. The same as xattr of EXT3.
976 * DATA_TRANS_BLOCKS(16)
977 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
978 * also counted in. Do not know why?
980 [DTO_XATTR_SET] = 16,
983 * creadits for inode change during write.
985 [DTO_WRITE_BASE] = 3,
987 * credits for single block write.
989 [DTO_WRITE_BLOCK] = 16,
991 * Attr set credits for chown.
992 * 3 (inode bit, group, GDT) +
993 * 2 * QUOTA_INIT_BLOCKS(25) +
994 * 2 * QUOTA_DEL_BLOCKS(9)
996 [DTO_ATTR_SET_CHOWN]= 71
999 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1002 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1003 ARRAY_SIZE(osd_dto_credits_quota));
1004 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1005 #ifdef HAVE_QUOTA_SUPPORT
1006 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1007 return osd_dto_credits_quota[op];
1010 return osd_dto_credits_noquota[op];
1013 static const struct dt_device_operations osd_dt_ops = {
1014 .dt_root_get = osd_root_get,
1015 .dt_statfs = osd_statfs,
1016 .dt_trans_start = osd_trans_start,
1017 .dt_trans_stop = osd_trans_stop,
1018 .dt_conf_get = osd_conf_get,
1019 .dt_sync = osd_sync,
1021 .dt_commit_async = osd_commit_async,
1022 .dt_credit_get = osd_credit_get,
1023 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1024 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1027 static void osd_object_read_lock(const struct lu_env *env,
1028 struct dt_object *dt, unsigned role)
1030 struct osd_object *obj = osd_dt_obj(dt);
1031 struct osd_thread_info *oti = osd_oti_get(env);
1033 LINVRNT(osd_invariant(obj));
1035 LASSERT(obj->oo_owner != env);
1036 down_read_nested(&obj->oo_sem, role);
1038 LASSERT(obj->oo_owner == NULL);
1042 static void osd_object_write_lock(const struct lu_env *env,
1043 struct dt_object *dt, unsigned role)
1045 struct osd_object *obj = osd_dt_obj(dt);
1046 struct osd_thread_info *oti = osd_oti_get(env);
1048 LINVRNT(osd_invariant(obj));
1050 LASSERT(obj->oo_owner != env);
1051 down_write_nested(&obj->oo_sem, role);
1053 LASSERT(obj->oo_owner == NULL);
1054 obj->oo_owner = env;
1058 static void osd_object_read_unlock(const struct lu_env *env,
1059 struct dt_object *dt)
1061 struct osd_object *obj = osd_dt_obj(dt);
1062 struct osd_thread_info *oti = osd_oti_get(env);
1064 LINVRNT(osd_invariant(obj));
1066 LASSERT(oti->oti_r_locks > 0);
1068 up_read(&obj->oo_sem);
1071 static void osd_object_write_unlock(const struct lu_env *env,
1072 struct dt_object *dt)
1074 struct osd_object *obj = osd_dt_obj(dt);
1075 struct osd_thread_info *oti = osd_oti_get(env);
1077 LINVRNT(osd_invariant(obj));
1079 LASSERT(obj->oo_owner == env);
1080 LASSERT(oti->oti_w_locks > 0);
1082 obj->oo_owner = NULL;
1083 up_write(&obj->oo_sem);
1086 static int osd_object_write_locked(const struct lu_env *env,
1087 struct dt_object *dt)
1089 struct osd_object *obj = osd_dt_obj(dt);
1091 LINVRNT(osd_invariant(obj));
1093 return obj->oo_owner == env;
1096 static int capa_is_sane(const struct lu_env *env,
1097 struct osd_device *dev,
1098 struct lustre_capa *capa,
1099 struct lustre_capa_key *keys)
1101 struct osd_thread_info *oti = osd_oti_get(env);
1102 struct lustre_capa *tcapa = &oti->oti_capa;
1103 struct obd_capa *oc;
1107 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1109 if (capa_is_expired(oc)) {
1110 DEBUG_CAPA(D_ERROR, capa, "expired");
1117 if (capa_is_expired_sec(capa)) {
1118 DEBUG_CAPA(D_ERROR, capa, "expired");
1122 spin_lock(&capa_lock);
1123 for (i = 0; i < 2; i++) {
1124 if (keys[i].lk_keyid == capa->lc_keyid) {
1125 oti->oti_capa_key = keys[i];
1129 spin_unlock(&capa_lock);
1132 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1136 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1140 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1141 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1145 oc = capa_add(dev->od_capa_hash, capa);
1151 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1152 struct lustre_capa *capa, __u64 opc)
1154 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1155 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1156 struct md_capainfo *ci;
1159 if (!dev->od_fl_capa)
1162 if (capa == BYPASS_CAPA)
1165 ci = md_capainfo(env);
1169 if (ci->mc_auth == LC_ID_NONE)
1173 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1177 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1178 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1183 if (!capa_opc_supported(capa, opc)) {
1184 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1188 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1189 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1196 static struct timespec *osd_inode_time(const struct lu_env *env,
1197 struct inode *inode, __u64 seconds)
1199 struct osd_thread_info *oti = osd_oti_get(env);
1200 struct timespec *t = &oti->oti_time;
1202 t->tv_sec = seconds;
1204 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1209 static void osd_inode_getattr(const struct lu_env *env,
1210 struct inode *inode, struct lu_attr *attr)
1212 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1213 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1214 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1216 attr->la_atime = LTIME_S(inode->i_atime);
1217 attr->la_mtime = LTIME_S(inode->i_mtime);
1218 attr->la_ctime = LTIME_S(inode->i_ctime);
1219 attr->la_mode = inode->i_mode;
1220 attr->la_size = i_size_read(inode);
1221 attr->la_blocks = inode->i_blocks;
1222 attr->la_uid = inode->i_uid;
1223 attr->la_gid = inode->i_gid;
1224 attr->la_flags = LDISKFS_I(inode)->i_flags;
1225 attr->la_nlink = inode->i_nlink;
1226 attr->la_rdev = inode->i_rdev;
1227 attr->la_blksize = ll_inode_blksize(inode);
1228 attr->la_blkbits = inode->i_blkbits;
1231 static int osd_attr_get(const struct lu_env *env,
1232 struct dt_object *dt,
1233 struct lu_attr *attr,
1234 struct lustre_capa *capa)
1236 struct osd_object *obj = osd_dt_obj(dt);
1238 LASSERT(dt_object_exists(dt));
1239 LINVRNT(osd_invariant(obj));
1241 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1244 spin_lock(&obj->oo_guard);
1245 osd_inode_getattr(env, obj->oo_inode, attr);
1246 spin_unlock(&obj->oo_guard);
1250 static int osd_inode_setattr(const struct lu_env *env,
1251 struct inode *inode, const struct lu_attr *attr)
1255 bits = attr->la_valid;
1257 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1259 #ifdef HAVE_QUOTA_SUPPORT
1260 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1261 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1262 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1268 iattr.ia_valid |= ATTR_UID;
1270 iattr.ia_valid |= ATTR_GID;
1271 iattr.ia_uid = attr->la_uid;
1272 iattr.ia_gid = attr->la_gid;
1273 osd_push_ctxt(env, save);
1274 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1281 if (bits & LA_ATIME)
1282 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1283 if (bits & LA_CTIME)
1284 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1285 if (bits & LA_MTIME)
1286 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1287 if (bits & LA_SIZE) {
1288 LDISKFS_I(inode)->i_disksize = attr->la_size;
1289 i_size_write(inode, attr->la_size);
1292 /* OSD should not change "i_blocks" which is used by quota.
1293 * "i_blocks" should be changed by ldiskfs only.
1294 * Enable this assignment for SOM purpose now, until it is
1295 * stored in SOM EA. */
1296 if (bits & LA_BLOCKS)
1297 inode->i_blocks = attr->la_blocks;
1300 inode->i_mode = (inode->i_mode & S_IFMT) |
1301 (attr->la_mode & ~S_IFMT);
1303 inode->i_uid = attr->la_uid;
1305 inode->i_gid = attr->la_gid;
1306 if (bits & LA_NLINK)
1307 inode->i_nlink = attr->la_nlink;
1309 inode->i_rdev = attr->la_rdev;
1311 if (bits & LA_FLAGS) {
1312 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1314 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1315 (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1320 static int osd_attr_set(const struct lu_env *env,
1321 struct dt_object *dt,
1322 const struct lu_attr *attr,
1323 struct thandle *handle,
1324 struct lustre_capa *capa)
1326 struct osd_object *obj = osd_dt_obj(dt);
1329 LASSERT(handle != NULL);
1330 LASSERT(dt_object_exists(dt));
1331 LASSERT(osd_invariant(obj));
1333 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1336 spin_lock(&obj->oo_guard);
1337 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1338 spin_unlock(&obj->oo_guard);
1341 mark_inode_dirty(obj->oo_inode);
1348 * XXX temporary solution.
1350 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1351 struct lu_attr *attr, struct thandle *th)
1356 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1357 struct lu_attr *attr, struct thandle *th)
1359 osd_object_init0(obj);
1363 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1364 struct osd_object *obj,
1368 struct osd_thread_info *info = osd_oti_get(env);
1369 struct dentry *child_dentry = &info->oti_child_dentry;
1370 struct dentry *obj_dentry = &info->oti_obj_dentry;
1372 obj_dentry->d_inode = obj->oo_inode;
1373 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1374 obj_dentry->d_name.hash = 0;
1376 child_dentry->d_name.hash = 0;
1377 child_dentry->d_parent = obj_dentry;
1378 child_dentry->d_name.name = name;
1379 child_dentry->d_name.len = namelen;
1380 return child_dentry;
1384 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1386 struct dt_allocation_hint *hint,
1390 struct osd_device *osd = osd_obj2dev(obj);
1391 struct osd_thandle *oth;
1392 struct dt_object *parent;
1393 struct inode *inode;
1394 #ifdef HAVE_QUOTA_SUPPORT
1395 struct osd_ctxt *save = &info->oti_ctxt;
1398 LINVRNT(osd_invariant(obj));
1399 LASSERT(obj->oo_inode == NULL);
1401 oth = container_of(th, struct osd_thandle, ot_super);
1402 LASSERT(oth->ot_handle->h_transaction != NULL);
1404 if (hint && hint->dah_parent)
1405 parent = hint->dah_parent;
1407 parent = osd->od_obj_area;
1409 LASSERT(parent != NULL);
1410 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1412 #ifdef HAVE_QUOTA_SUPPORT
1413 osd_push_ctxt(info->oti_env, save);
1415 inode = ldiskfs_create_inode(oth->ot_handle,
1416 osd_dt_obj(parent)->oo_inode, mode);
1417 #ifdef HAVE_QUOTA_SUPPORT
1420 if (!IS_ERR(inode)) {
1421 obj->oo_inode = inode;
1424 result = PTR_ERR(inode);
1425 LINVRNT(osd_invariant(obj));
1433 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1434 struct lu_attr *attr,
1435 struct dt_allocation_hint *hint,
1436 struct dt_object_format *dof,
1440 struct osd_thandle *oth;
1441 struct osd_device *osd = osd_obj2dev(obj);
1442 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1444 LASSERT(S_ISDIR(attr->la_mode));
1446 oth = container_of(th, struct osd_thandle, ot_super);
1447 LASSERT(oth->ot_handle->h_transaction != NULL);
1448 result = osd_mkfile(info, obj, mode, hint, th);
1449 if (result == 0 && osd->od_iop_mode == 0) {
1450 LASSERT(obj->oo_inode != NULL);
1452 * XXX uh-oh... call low-level iam function directly.
1455 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1456 sizeof (struct osd_fid_pack),
1462 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1463 struct lu_attr *attr,
1464 struct dt_allocation_hint *hint,
1465 struct dt_object_format *dof,
1469 struct osd_thandle *oth;
1470 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1472 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1474 LASSERT(S_ISREG(attr->la_mode));
1476 oth = container_of(th, struct osd_thandle, ot_super);
1477 LASSERT(oth->ot_handle->h_transaction != NULL);
1479 result = osd_mkfile(info, obj, mode, hint, th);
1481 LASSERT(obj->oo_inode != NULL);
1482 if (feat->dif_flags & DT_IND_VARKEY)
1483 result = iam_lvar_create(obj->oo_inode,
1484 feat->dif_keysize_max,
1486 feat->dif_recsize_max,
1489 result = iam_lfix_create(obj->oo_inode,
1490 feat->dif_keysize_max,
1492 feat->dif_recsize_max,
1499 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1500 struct lu_attr *attr,
1501 struct dt_allocation_hint *hint,
1502 struct dt_object_format *dof,
1505 LASSERT(S_ISREG(attr->la_mode));
1506 return osd_mkfile(info, obj, (attr->la_mode &
1507 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1510 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1511 struct lu_attr *attr,
1512 struct dt_allocation_hint *hint,
1513 struct dt_object_format *dof,
1516 LASSERT(S_ISLNK(attr->la_mode));
1517 return osd_mkfile(info, obj, (attr->la_mode &
1518 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1521 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1522 struct lu_attr *attr,
1523 struct dt_allocation_hint *hint,
1524 struct dt_object_format *dof,
1527 umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1530 LINVRNT(osd_invariant(obj));
1531 LASSERT(obj->oo_inode == NULL);
1532 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1533 S_ISFIFO(mode) || S_ISSOCK(mode));
1535 result = osd_mkfile(info, obj, mode, hint, th);
1537 LASSERT(obj->oo_inode != NULL);
1538 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1540 LINVRNT(osd_invariant(obj));
1544 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1546 struct dt_allocation_hint *hint,
1547 struct dt_object_format *dof,
1550 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1552 osd_obj_type_f result;
1568 result = osd_mk_index;
1579 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1580 struct dt_object *parent, umode_t child_mode)
1584 memset(ah, 0, sizeof(*ah));
1585 ah->dah_parent = parent;
1586 ah->dah_mode = child_mode;
1590 * Helper function for osd_object_create()
1592 * \retval 0, on success
1594 static int __osd_object_create(struct osd_thread_info *info,
1595 struct osd_object *obj, struct lu_attr *attr,
1596 struct dt_allocation_hint *hint,
1597 struct dt_object_format *dof,
1603 result = osd_create_pre(info, obj, attr, th);
1605 result = osd_create_type_f(dof->dof_type)(info, obj,
1606 attr, hint, dof, th);
1608 result = osd_create_post(info, obj, attr, th);
1614 * Helper function for osd_object_create()
1616 * \retval 0, on success
1618 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1619 const struct lu_fid *fid, struct thandle *th)
1621 struct osd_thread_info *info = osd_oti_get(env);
1622 struct osd_inode_id *id = &info->oti_id;
1623 struct osd_device *osd = osd_obj2dev(obj);
1624 struct md_ucred *uc = md_ucred(env);
1626 LASSERT(obj->oo_inode != NULL);
1627 LASSERT(uc != NULL);
1629 id->oii_ino = obj->oo_inode->i_ino;
1630 id->oii_gen = obj->oo_inode->i_generation;
1632 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1633 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1636 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1637 struct lu_attr *attr,
1638 struct dt_allocation_hint *hint,
1639 struct dt_object_format *dof,
1642 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1643 struct osd_object *obj = osd_dt_obj(dt);
1644 struct osd_thread_info *info = osd_oti_get(env);
1649 LINVRNT(osd_invariant(obj));
1650 LASSERT(!dt_object_exists(dt));
1651 LASSERT(osd_write_locked(env, obj));
1652 LASSERT(th != NULL);
1654 result = __osd_object_create(info, obj, attr, hint, dof, th);
1656 result = __osd_oi_insert(env, obj, fid, th);
1658 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1659 LASSERT(osd_invariant(obj));
1664 * Helper function for osd_xattr_set()
1666 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1667 const struct lu_buf *buf, const char *name, int fl)
1669 struct osd_object *obj = osd_dt_obj(dt);
1670 struct inode *inode = obj->oo_inode;
1671 struct osd_thread_info *info = osd_oti_get(env);
1672 struct dentry *dentry = &info->oti_child_dentry;
1673 struct timespec *t = &info->oti_time;
1677 LASSERT(dt_object_exists(dt));
1678 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1679 LASSERT(osd_write_locked(env, obj));
1681 if (fl & LU_XATTR_REPLACE)
1682 fs_flags |= XATTR_REPLACE;
1684 if (fl & LU_XATTR_CREATE)
1685 fs_flags |= XATTR_CREATE;
1687 dentry->d_inode = inode;
1688 *t = inode->i_ctime;
1689 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1690 buf->lb_len, fs_flags);
1691 /* ctime should not be updated with server-side time. */
1692 spin_lock(&obj->oo_guard);
1693 inode->i_ctime = *t;
1694 spin_unlock(&obj->oo_guard);
1695 mark_inode_dirty(inode);
1700 * Put the fid into lustre_mdt_attrs, and then place the structure
1701 * inode's ea. This fid should not be altered during the life time
1704 * \retval +ve, on success
1705 * \retval -ve, on error
1707 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1709 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1710 const struct lu_fid *fid)
1712 struct osd_thread_info *info = osd_oti_get(env);
1713 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1715 fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1717 return __osd_xattr_set(env, dt,
1718 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1719 XATTR_NAME_LMA, LU_XATTR_CREATE);
1724 * Helper function to form igif
1726 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1729 lu_igif_build(fid, inode->i_ino, inode->i_generation);
1733 * Helper function to pack the fid
1735 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1736 struct lu_fid *befider)
1738 fid_cpu_to_be(befider, (struct lu_fid *)fid);
1739 memcpy(pack->fp_area, befider, sizeof(*befider));
1740 pack->fp_len = sizeof(*befider) + 1;
1743 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1748 switch (pack->fp_len) {
1749 case sizeof *fid + 1:
1750 memcpy(fid, pack->fp_area, sizeof *fid);
1751 fid_be_to_cpu(fid, fid);
1754 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1761 * Try to read the fid from inode ea into dt_rec, if return value
1762 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1764 * \param fid, object fid.
1766 * \retval 0, on success
1768 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1769 __u32 ino, struct lu_fid *fid)
1771 struct osd_thread_info *info = osd_oti_get(env);
1772 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1773 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1774 struct dentry *dentry = &info->oti_child_dentry;
1775 struct osd_inode_id *id = &info->oti_id;
1776 struct osd_device *dev;
1777 struct inode *inode;
1781 dev = osd_dev(ldev);
1784 id->oii_gen = OSD_OII_NOGEN;
1786 inode = osd_iget(info, dev, id);
1787 if (IS_ERR(inode)) {
1788 rc = PTR_ERR(inode);
1791 dentry->d_inode = inode;
1793 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1794 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1797 /* Check LMA compatibility */
1799 (mdt_attrs->lma_incompat & ~cpu_to_be32(LMA_INCOMPAT_SUPP))) {
1800 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1801 inode->i_ino, be32_to_cpu(mdt_attrs->lma_incompat) &
1802 ~LMA_INCOMPAT_SUPP);
1807 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1809 } else if (rc == -ENODATA) {
1810 osd_igif_get(env, inode, fid);
1820 * OSD layer object create function for interoperability mode (b11826).
1821 * This is mostly similar to osd_object_create(). Only difference being, fid is
1822 * inserted into inode ea here.
1824 * \retval 0, on success
1825 * \retval -ve, on error
1827 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1828 struct lu_attr *attr,
1829 struct dt_allocation_hint *hint,
1830 struct dt_object_format *dof,
1833 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1834 struct osd_object *obj = osd_dt_obj(dt);
1835 struct osd_thread_info *info = osd_oti_get(env);
1841 LASSERT(osd_invariant(obj));
1842 LASSERT(!dt_object_exists(dt));
1843 LASSERT(osd_write_locked(env, obj));
1844 LASSERT(th != NULL);
1846 result = __osd_object_create(info, obj, attr, hint, dof, th);
1848 if (hint && hint->dah_parent)
1849 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1851 /* objects under osd root shld have igif fid, so dont add fid EA */
1852 if (result == 0 && is_root == 0)
1853 result = osd_ea_fid_set(env, dt, fid);
1856 result = __osd_oi_insert(env, obj, fid, th);
1858 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1859 LINVRNT(osd_invariant(obj));
1864 * Concurrency: @dt is write locked.
1866 static void osd_object_ref_add(const struct lu_env *env,
1867 struct dt_object *dt,
1870 struct osd_object *obj = osd_dt_obj(dt);
1871 struct inode *inode = obj->oo_inode;
1873 LINVRNT(osd_invariant(obj));
1874 LASSERT(dt_object_exists(dt));
1875 LASSERT(osd_write_locked(env, obj));
1876 LASSERT(th != NULL);
1878 spin_lock(&obj->oo_guard);
1879 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1881 spin_unlock(&obj->oo_guard);
1882 mark_inode_dirty(inode);
1883 LINVRNT(osd_invariant(obj));
1887 * Concurrency: @dt is write locked.
1889 static void osd_object_ref_del(const struct lu_env *env,
1890 struct dt_object *dt,
1893 struct osd_object *obj = osd_dt_obj(dt);
1894 struct inode *inode = obj->oo_inode;
1896 LINVRNT(osd_invariant(obj));
1897 LASSERT(dt_object_exists(dt));
1898 LASSERT(osd_write_locked(env, obj));
1899 LASSERT(th != NULL);
1901 spin_lock(&obj->oo_guard);
1902 LASSERT(inode->i_nlink > 0);
1904 spin_unlock(&obj->oo_guard);
1905 mark_inode_dirty(inode);
1906 LINVRNT(osd_invariant(obj));
1910 * Concurrency: @dt is read locked.
1912 static int osd_xattr_get(const struct lu_env *env,
1913 struct dt_object *dt,
1916 struct lustre_capa *capa)
1918 struct osd_object *obj = osd_dt_obj(dt);
1919 struct inode *inode = obj->oo_inode;
1920 struct osd_thread_info *info = osd_oti_get(env);
1921 struct dentry *dentry = &info->oti_obj_dentry;
1923 LASSERT(dt_object_exists(dt));
1924 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1925 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1927 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1930 dentry->d_inode = inode;
1931 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1935 * Concurrency: @dt is write locked.
1937 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1938 const struct lu_buf *buf, const char *name, int fl,
1939 struct thandle *handle, struct lustre_capa *capa)
1941 LASSERT(handle != NULL);
1943 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1946 return __osd_xattr_set(env, dt, buf, name, fl);
1950 * Concurrency: @dt is read locked.
1952 static int osd_xattr_list(const struct lu_env *env,
1953 struct dt_object *dt,
1955 struct lustre_capa *capa)
1957 struct osd_object *obj = osd_dt_obj(dt);
1958 struct inode *inode = obj->oo_inode;
1959 struct osd_thread_info *info = osd_oti_get(env);
1960 struct dentry *dentry = &info->oti_obj_dentry;
1962 LASSERT(dt_object_exists(dt));
1963 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1964 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1966 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1969 dentry->d_inode = inode;
1970 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1974 * Concurrency: @dt is write locked.
1976 static int osd_xattr_del(const struct lu_env *env,
1977 struct dt_object *dt,
1979 struct thandle *handle,
1980 struct lustre_capa *capa)
1982 struct osd_object *obj = osd_dt_obj(dt);
1983 struct inode *inode = obj->oo_inode;
1984 struct osd_thread_info *info = osd_oti_get(env);
1985 struct dentry *dentry = &info->oti_obj_dentry;
1986 struct timespec *t = &info->oti_time;
1989 LASSERT(dt_object_exists(dt));
1990 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1991 LASSERT(osd_write_locked(env, obj));
1992 LASSERT(handle != NULL);
1994 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1997 dentry->d_inode = inode;
1998 *t = inode->i_ctime;
1999 rc = inode->i_op->removexattr(dentry, name);
2000 /* ctime should not be updated with server-side time. */
2001 spin_lock(&obj->oo_guard);
2002 inode->i_ctime = *t;
2003 spin_unlock(&obj->oo_guard);
2004 mark_inode_dirty(inode);
2008 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2009 struct dt_object *dt,
2010 struct lustre_capa *old,
2013 struct osd_thread_info *info = osd_oti_get(env);
2014 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2015 struct osd_object *obj = osd_dt_obj(dt);
2016 struct osd_device *dev = osd_obj2dev(obj);
2017 struct lustre_capa_key *key = &info->oti_capa_key;
2018 struct lustre_capa *capa = &info->oti_capa;
2019 struct obd_capa *oc;
2020 struct md_capainfo *ci;
2024 if (!dev->od_fl_capa)
2025 RETURN(ERR_PTR(-ENOENT));
2027 LASSERT(dt_object_exists(dt));
2028 LINVRNT(osd_invariant(obj));
2030 /* renewal sanity check */
2031 if (old && osd_object_auth(env, dt, old, opc))
2032 RETURN(ERR_PTR(-EACCES));
2034 ci = md_capainfo(env);
2036 RETURN(ERR_PTR(-ENOENT));
2038 switch (ci->mc_auth) {
2042 capa->lc_uid = obj->oo_inode->i_uid;
2043 capa->lc_gid = obj->oo_inode->i_gid;
2044 capa->lc_flags = LC_ID_PLAIN;
2046 case LC_ID_CONVERT: {
2049 s[0] = obj->oo_inode->i_uid;
2050 get_random_bytes(&(s[1]), sizeof(__u32));
2051 s[2] = obj->oo_inode->i_gid;
2052 get_random_bytes(&(s[3]), sizeof(__u32));
2053 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2055 RETURN(ERR_PTR(rc));
2057 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2058 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2059 capa->lc_flags = LC_ID_CONVERT;
2063 RETURN(ERR_PTR(-EINVAL));
2066 capa->lc_fid = *fid;
2068 capa->lc_flags |= dev->od_capa_alg << 24;
2069 capa->lc_timeout = dev->od_capa_timeout;
2070 capa->lc_expiry = 0;
2072 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2074 LASSERT(!capa_is_expired(oc));
2078 spin_lock(&capa_lock);
2079 *key = dev->od_capa_keys[1];
2080 spin_unlock(&capa_lock);
2082 capa->lc_keyid = key->lk_keyid;
2083 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2085 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2087 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2088 RETURN(ERR_PTR(rc));
2091 oc = capa_add(dev->od_capa_hash, capa);
2095 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2098 struct osd_object *obj = osd_dt_obj(dt);
2099 struct inode *inode = obj->oo_inode;
2100 struct osd_thread_info *info = osd_oti_get(env);
2101 struct dentry *dentry = &info->oti_obj_dentry;
2102 struct file *file = &info->oti_file;
2105 dentry->d_inode = inode;
2106 file->f_dentry = dentry;
2107 file->f_mapping = inode->i_mapping;
2108 file->f_op = inode->i_fop;
2109 LOCK_INODE_MUTEX(inode);
2110 rc = file->f_op->fsync(file, dentry, 0);
2111 UNLOCK_INODE_MUTEX(inode);
2116 * Get the 64-bit version for an inode.
2118 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2119 struct dt_object *dt)
2121 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2123 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2124 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2125 return LDISKFS_I(inode)->i_fs_version;
2129 * Set the 64-bit version and return the old version.
2131 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2132 dt_obj_version_t new_version)
2134 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2136 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2137 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2138 LDISKFS_I(inode)->i_fs_version = new_version;
2139 /** Version is set after all inode operations are finished,
2140 * so we should mark it dirty here */
2141 inode->i_sb->s_op->dirty_inode(inode);
2144 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2147 struct osd_object *obj = osd_dt_obj(dt);
2150 *data = (void *)obj->oo_inode;
2158 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2159 const struct dt_index_features *feat)
2161 struct iam_descr *descr;
2163 if (osd_object_is_root(o))
2164 return feat == &dt_directory_features;
2166 LASSERT(o->oo_dir != NULL);
2168 descr = o->oo_dir->od_container.ic_descr;
2169 if (feat == &dt_directory_features) {
2170 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2176 feat->dif_keysize_min <= descr->id_key_size &&
2177 descr->id_key_size <= feat->dif_keysize_max &&
2178 feat->dif_recsize_min <= descr->id_rec_size &&
2179 descr->id_rec_size <= feat->dif_recsize_max &&
2180 !(feat->dif_flags & (DT_IND_VARKEY |
2181 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2182 ergo(feat->dif_flags & DT_IND_UPDATE,
2183 1 /* XXX check that object (and file system) is
2188 static int osd_iam_container_init(const struct lu_env *env,
2189 struct osd_object *obj,
2190 struct osd_directory *dir)
2193 struct iam_container *bag;
2195 bag = &dir->od_container;
2196 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2198 result = iam_container_setup(bag);
2200 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2202 iam_container_fini(bag);
2209 * Concurrency: no external locking is necessary.
2211 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2212 const struct dt_index_features *feat)
2216 struct osd_object *obj = osd_dt_obj(dt);
2217 struct osd_device *osd = osd_obj2dev(obj);
2219 LINVRNT(osd_invariant(obj));
2220 LASSERT(dt_object_exists(dt));
2222 if (osd_object_is_root(obj)) {
2223 dt->do_index_ops = &osd_index_ea_ops;
2225 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2226 dt->do_index_ops = &osd_index_ea_ops;
2227 if (S_ISDIR(obj->oo_inode->i_mode))
2232 } else if (!osd_has_index(obj)) {
2233 struct osd_directory *dir;
2238 spin_lock(&obj->oo_guard);
2239 if (obj->oo_dir == NULL)
2243 * Concurrent thread allocated container data.
2246 spin_unlock(&obj->oo_guard);
2248 * Now, that we have container data, serialize its
2251 down_write(&obj->oo_ext_idx_sem);
2253 * recheck under lock.
2255 if (!osd_has_index(obj))
2256 result = osd_iam_container_init(env, obj, dir);
2259 up_write(&obj->oo_ext_idx_sem);
2265 if (result == 0 && ea_dir == 0) {
2266 if (!osd_iam_index_probe(env, obj, feat))
2269 LINVRNT(osd_invariant(obj));
2274 static const struct dt_object_operations osd_obj_ops = {
2275 .do_read_lock = osd_object_read_lock,
2276 .do_write_lock = osd_object_write_lock,
2277 .do_read_unlock = osd_object_read_unlock,
2278 .do_write_unlock = osd_object_write_unlock,
2279 .do_write_locked = osd_object_write_locked,
2280 .do_attr_get = osd_attr_get,
2281 .do_attr_set = osd_attr_set,
2282 .do_ah_init = osd_ah_init,
2283 .do_create = osd_object_create,
2284 .do_index_try = osd_index_try,
2285 .do_ref_add = osd_object_ref_add,
2286 .do_ref_del = osd_object_ref_del,
2287 .do_xattr_get = osd_xattr_get,
2288 .do_xattr_set = osd_xattr_set,
2289 .do_xattr_del = osd_xattr_del,
2290 .do_xattr_list = osd_xattr_list,
2291 .do_capa_get = osd_capa_get,
2292 .do_object_sync = osd_object_sync,
2293 .do_version_get = osd_object_version_get,
2294 .do_version_set = osd_object_version_set,
2295 .do_data_get = osd_data_get,
2299 * dt_object_operations for interoperability mode
2300 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2302 static const struct dt_object_operations osd_obj_ea_ops = {
2303 .do_read_lock = osd_object_read_lock,
2304 .do_write_lock = osd_object_write_lock,
2305 .do_read_unlock = osd_object_read_unlock,
2306 .do_write_unlock = osd_object_write_unlock,
2307 .do_write_locked = osd_object_write_locked,
2308 .do_attr_get = osd_attr_get,
2309 .do_attr_set = osd_attr_set,
2310 .do_ah_init = osd_ah_init,
2311 .do_create = osd_object_ea_create,
2312 .do_index_try = osd_index_try,
2313 .do_ref_add = osd_object_ref_add,
2314 .do_ref_del = osd_object_ref_del,
2315 .do_xattr_get = osd_xattr_get,
2316 .do_xattr_set = osd_xattr_set,
2317 .do_xattr_del = osd_xattr_del,
2318 .do_xattr_list = osd_xattr_list,
2319 .do_capa_get = osd_capa_get,
2320 .do_object_sync = osd_object_sync,
2321 .do_version_get = osd_object_version_get,
2322 .do_version_set = osd_object_version_set,
2323 .do_data_get = osd_data_get,
2331 * XXX: Another layering violation for now.
2333 * We don't want to use ->f_op->read methods, because generic file write
2335 * - serializes on ->i_sem, and
2337 * - does a lot of extra work like balance_dirty_pages(),
2339 * which doesn't work for globally shared files like /last-received.
2341 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2342 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2343 loff_t *offs, handle_t *handle);
2345 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2346 struct lu_buf *buf, loff_t *pos,
2347 struct lustre_capa *capa)
2349 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2351 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2354 return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2357 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2358 const struct lu_buf *buf, loff_t *pos,
2359 struct thandle *handle, struct lustre_capa *capa,
2362 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2363 struct osd_thandle *oh;
2365 #ifdef HAVE_QUOTA_SUPPORT
2366 cfs_cap_t save = current->cap_effective;
2369 LASSERT(handle != NULL);
2371 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2374 oh = container_of(handle, struct osd_thandle, ot_super);
2375 LASSERT(oh->ot_handle->h_transaction != NULL);
2376 #ifdef HAVE_QUOTA_SUPPORT
2378 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2380 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2382 result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2383 pos, oh->ot_handle);
2384 #ifdef HAVE_QUOTA_SUPPORT
2385 current->cap_effective = save;
2388 result = buf->lb_len;
2392 static const struct dt_body_operations osd_body_ops = {
2393 .dbo_read = osd_read,
2394 .dbo_write = osd_write
2399 * delete a (key, value) pair from index \a dt specified by \a key
2401 * \param dt_object osd index object
2402 * \param key key for index
2403 * \param rec record reference
2404 * \param handle transaction handler
2407 * \retval -ve failure
2410 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2411 const struct dt_key *key, struct thandle *handle,
2412 struct lustre_capa *capa)
2414 struct osd_object *obj = osd_dt_obj(dt);
2415 struct osd_thandle *oh;
2416 struct iam_path_descr *ipd;
2417 struct iam_container *bag = &obj->oo_dir->od_container;
2422 LINVRNT(osd_invariant(obj));
2423 LASSERT(dt_object_exists(dt));
2424 LASSERT(bag->ic_object == obj->oo_inode);
2425 LASSERT(handle != NULL);
2427 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2430 ipd = osd_idx_ipd_get(env, bag);
2431 if (unlikely(ipd == NULL))
2434 oh = container_of0(handle, struct osd_thandle, ot_super);
2435 LASSERT(oh->ot_handle != NULL);
2436 LASSERT(oh->ot_handle->h_transaction != NULL);
2438 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2439 osd_ipd_put(env, bag, ipd);
2440 LINVRNT(osd_invariant(obj));
2445 * Index delete function for interoperability mode (b11826).
2446 * It will remove the directory entry added by osd_index_ea_insert().
2447 * This entry is needed to maintain name->fid mapping.
2449 * \param key, key i.e. file entry to be deleted
2451 * \retval 0, on success
2452 * \retval -ve, on error
2454 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2455 const struct dt_key *key, struct thandle *handle,
2456 struct lustre_capa *capa)
2458 struct osd_object *obj = osd_dt_obj(dt);
2459 struct inode *dir = obj->oo_inode;
2460 struct dentry *dentry;
2461 struct osd_thandle *oh;
2462 struct ldiskfs_dir_entry_2 *de;
2463 struct buffer_head *bh;
2469 LINVRNT(osd_invariant(obj));
2470 LASSERT(dt_object_exists(dt));
2471 LASSERT(handle != NULL);
2473 oh = container_of(handle, struct osd_thandle, ot_super);
2474 LASSERT(oh->ot_handle != NULL);
2475 LASSERT(oh->ot_handle->h_transaction != NULL);
2477 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2480 dentry = osd_child_dentry_get(env, obj,
2481 (char *)key, strlen((char *)key));
2483 down_write(&obj->oo_ext_idx_sem);
2484 bh = ldiskfs_find_entry(dentry, &de);
2486 struct osd_thread_info *oti = osd_oti_get(env);
2487 struct timespec *ctime = &oti->oti_time;
2488 struct timespec *mtime = &oti->oti_time2;
2490 *ctime = dir->i_ctime;
2491 *mtime = dir->i_mtime;
2492 rc = ldiskfs_delete_entry(oh->ot_handle,
2494 /* xtime should not be updated with server-side time. */
2495 spin_lock(&obj->oo_guard);
2496 dir->i_ctime = *ctime;
2497 dir->i_mtime = *mtime;
2498 spin_unlock(&obj->oo_guard);
2499 mark_inode_dirty(dir);
2504 up_write(&obj->oo_ext_idx_sem);
2505 LASSERT(osd_invariant(obj));
2510 * Lookup index for \a key and copy record to \a rec.
2512 * \param dt_object osd index object
2513 * \param key key for index
2514 * \param rec record reference
2516 * \retval +ve success : exact mach
2517 * \retval 0 return record with key not greater than \a key
2518 * \retval -ve failure
2520 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2521 struct dt_rec *rec, const struct dt_key *key,
2522 struct lustre_capa *capa)
2524 struct osd_object *obj = osd_dt_obj(dt);
2525 struct iam_path_descr *ipd;
2526 struct iam_container *bag = &obj->oo_dir->od_container;
2527 struct osd_thread_info *oti = osd_oti_get(env);
2528 struct iam_iterator *it = &oti->oti_idx_it;
2529 struct iam_rec *iam_rec;
2533 LASSERT(osd_invariant(obj));
2534 LASSERT(dt_object_exists(dt));
2535 LASSERT(bag->ic_object == obj->oo_inode);
2537 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2540 ipd = osd_idx_ipd_get(env, bag);
2544 /* got ipd now we can start iterator. */
2545 iam_it_init(it, bag, 0, ipd);
2547 rc = iam_it_get(it, (struct iam_key *)key);
2549 if (S_ISDIR(obj->oo_inode->i_mode))
2550 iam_rec = (struct iam_rec *)oti->oti_fid_packed;
2552 iam_rec = (struct iam_rec *) rec;
2554 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2555 if (S_ISDIR(obj->oo_inode->i_mode))
2556 osd_fid_unpack((struct lu_fid *) rec,
2557 (struct osd_fid_pack *)iam_rec);
2561 osd_ipd_put(env, bag, ipd);
2563 LINVRNT(osd_invariant(obj));
2569 * Inserts (key, value) pair in \a dt index object.
2571 * \param dt osd index object
2572 * \param key key for index
2573 * \param rec record reference
2574 * \param th transaction handler
2577 * \retval -ve failure
2579 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2580 const struct dt_rec *rec, const struct dt_key *key,
2581 struct thandle *th, struct lustre_capa *capa,
2584 struct osd_object *obj = osd_dt_obj(dt);
2585 struct iam_path_descr *ipd;
2586 struct osd_thandle *oh;
2587 struct iam_container *bag = &obj->oo_dir->od_container;
2588 #ifdef HAVE_QUOTA_SUPPORT
2589 cfs_cap_t save = current->cap_effective;
2591 struct osd_thread_info *oti = osd_oti_get(env);
2592 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_fid_packed;
2597 LINVRNT(osd_invariant(obj));
2598 LASSERT(dt_object_exists(dt));
2599 LASSERT(bag->ic_object == obj->oo_inode);
2600 LASSERT(th != NULL);
2602 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2605 ipd = osd_idx_ipd_get(env, bag);
2606 if (unlikely(ipd == NULL))
2609 oh = container_of0(th, struct osd_thandle, ot_super);
2610 LASSERT(oh->ot_handle != NULL);
2611 LASSERT(oh->ot_handle->h_transaction != NULL);
2612 #ifdef HAVE_QUOTA_SUPPORT
2614 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2616 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2618 if (S_ISDIR(obj->oo_inode->i_mode))
2619 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2621 iam_rec = (struct iam_rec *) rec;
2622 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2624 #ifdef HAVE_QUOTA_SUPPORT
2625 current->cap_effective = save;
2627 osd_ipd_put(env, bag, ipd);
2628 LINVRNT(osd_invariant(obj));
2633 * Calls ldiskfs_add_entry() to add directory entry
2634 * into the directory. This is required for
2635 * interoperability mode (b11826)
2637 * \retval 0, on success
2638 * \retval -ve, on error
2640 static int __osd_ea_add_rec(struct osd_thread_info *info,
2641 struct osd_object *pobj,
2642 struct osd_object *cobj,
2646 struct dentry *child;
2647 struct osd_thandle *oth;
2648 struct inode *cinode = cobj->oo_inode;
2651 oth = container_of(th, struct osd_thandle, ot_super);
2652 LASSERT(oth->ot_handle != NULL);
2653 LASSERT(oth->ot_handle->h_transaction != NULL);
2655 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2656 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2662 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2663 * into the directory.Also sets flags into osd object to
2664 * indicate dot and dotdot are created. This is required for
2665 * interoperability mode (b11826)
2667 * \param dir directory for dot and dotdot fixup.
2668 * \param obj child object for linking
2670 * \retval 0, on success
2671 * \retval -ve, on error
2673 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2674 struct osd_object *dir,
2675 struct osd_object *obj, const char *name,
2678 struct inode *parent_dir = obj->oo_inode;
2679 struct inode *inode = dir->oo_inode;
2680 struct osd_thandle *oth;
2683 oth = container_of(th, struct osd_thandle, ot_super);
2684 LASSERT(oth->ot_handle->h_transaction != NULL);
2685 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2687 if (strcmp(name, dot) == 0) {
2688 if (dir->oo_compat_dot_created) {
2691 LASSERT(obj == dir);
2692 dir->oo_compat_dot_created = 1;
2695 } else if(strcmp(name, dotdot) == 0) {
2696 if (!dir->oo_compat_dot_created)
2698 if (dir->oo_compat_dotdot_created)
2699 return __osd_ea_add_rec(info, dir, obj, name, th);
2701 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2703 dir->oo_compat_dotdot_created = 1;
2711 * It will call the appropriate osd_add* function and return the
2712 * value, return by respective functions.
2714 static int osd_ea_add_rec(const struct lu_env *env,
2715 struct osd_object *pobj,
2716 struct osd_object *cobj,
2720 struct osd_thread_info *info = osd_oti_get(env);
2723 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2725 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2727 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2733 * Calls ->lookup() to find dentry. From dentry get inode and
2734 * read inode's ea to get fid. This is required for interoperability
2737 * \retval 0, on success
2738 * \retval -ve, on error
2740 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2741 struct dt_rec *rec, const struct dt_key *key)
2743 struct inode *dir = obj->oo_inode;
2744 struct dentry *dentry;
2745 struct ldiskfs_dir_entry_2 *de;
2746 struct buffer_head *bh;
2747 struct lu_fid *fid = (struct lu_fid *) rec;
2751 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2753 dentry = osd_child_dentry_get(env, obj,
2754 (char *)key, strlen((char *)key));
2756 down_read(&obj->oo_ext_idx_sem);
2757 bh = ldiskfs_find_entry(dentry, &de);
2759 ino = le32_to_cpu(de->inode);
2761 rc = osd_ea_fid_get(env, obj, ino, fid);
2765 up_read(&obj->oo_ext_idx_sem);
2770 * Find the osd object for given fid.
2772 * \param fid, need to find the osd object having this fid
2774 * \retval osd_object, on success
2775 * \retval -ve, on error
2777 struct osd_object *osd_object_find(const struct lu_env *env,
2778 struct dt_object *dt,
2779 const struct lu_fid *fid)
2781 struct lu_device *ludev = dt->do_lu.lo_dev;
2782 struct osd_object *child = NULL;
2783 struct lu_object *luch;
2784 struct lu_object *lo;
2786 luch = lu_object_find(env, ludev, fid, NULL);
2787 if (!IS_ERR(luch)) {
2788 if (lu_object_exists(luch)) {
2789 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2791 child = osd_obj(lo);
2793 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2794 "lu_object can't be located"
2795 ""DFID"\n", PFID(fid));
2797 if (child == NULL) {
2798 lu_object_put(env, luch);
2799 CERROR("Unable to get osd_object\n");
2800 child = ERR_PTR(-ENOENT);
2803 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2804 "lu_object does not exists "DFID"\n",
2806 child = ERR_PTR(-ENOENT);
2809 child = (void *)luch;
2815 * Put the osd object once done with it.
2817 * \param obj, osd object that needs to be put
2819 static inline void osd_object_put(const struct lu_env *env,
2820 struct osd_object *obj)
2822 lu_object_put(env, &obj->oo_dt.do_lu);
2826 * Index add function for interoperability mode (b11826).
2827 * It will add the directory entry.This entry is needed to
2828 * maintain name->fid mapping.
2830 * \param key, it is key i.e. file entry to be inserted
2831 * \param rec, it is value of given key i.e. fid
2833 * \retval 0, on success
2834 * \retval -ve, on error
2836 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2837 const struct dt_rec *rec,
2838 const struct dt_key *key, struct thandle *th,
2839 struct lustre_capa *capa, int ignore_quota)
2841 struct osd_object *obj = osd_dt_obj(dt);
2842 struct lu_fid *fid = (struct lu_fid *) rec;
2843 const char *name = (const char *)key;
2844 struct osd_object *child;
2845 #ifdef HAVE_QUOTA_SUPPORT
2846 cfs_cap_t save = current->cap_effective;
2852 LASSERT(osd_invariant(obj));
2853 LASSERT(dt_object_exists(dt));
2854 LASSERT(th != NULL);
2856 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2859 child = osd_object_find(env, dt, fid);
2860 if (!IS_ERR(child)) {
2861 struct inode *inode = obj->oo_inode;
2862 struct osd_thread_info *oti = osd_oti_get(env);
2863 struct timespec *ctime = &oti->oti_time;
2864 struct timespec *mtime = &oti->oti_time2;
2866 *ctime = inode->i_ctime;
2867 *mtime = inode->i_mtime;
2868 #ifdef HAVE_QUOTA_SUPPORT
2870 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2872 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2874 down_write(&obj->oo_ext_idx_sem);
2875 rc = osd_ea_add_rec(env, obj, child, name, th);
2876 up_write(&obj->oo_ext_idx_sem);
2877 #ifdef HAVE_QUOTA_SUPPORT
2878 current->cap_effective = save;
2880 osd_object_put(env, child);
2881 /* xtime should not be updated with server-side time. */
2882 spin_lock(&obj->oo_guard);
2883 inode->i_ctime = *ctime;
2884 inode->i_mtime = *mtime;
2885 spin_unlock(&obj->oo_guard);
2886 mark_inode_dirty(inode);
2888 rc = PTR_ERR(child);
2891 LASSERT(osd_invariant(obj));
2896 * Initialize osd Iterator for given osd index object.
2898 * \param dt osd index object
2901 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2902 struct dt_object *dt,
2903 struct lustre_capa *capa)
2905 struct osd_it_iam *it;
2906 struct osd_thread_info *oti = osd_oti_get(env);
2907 struct osd_object *obj = osd_dt_obj(dt);
2908 struct lu_object *lo = &dt->do_lu;
2909 struct iam_path_descr *ipd;
2910 struct iam_container *bag = &obj->oo_dir->od_container;
2912 LASSERT(lu_object_exists(lo));
2914 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2915 return ERR_PTR(-EACCES);
2918 ipd = osd_it_ipd_get(env, bag);
2919 if (likely(ipd != NULL)) {
2923 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2924 return (struct dt_it *)it;
2926 return ERR_PTR(-ENOMEM);
2930 * free given Iterator.
2933 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2935 struct osd_it_iam *it = (struct osd_it_iam *)di;
2936 struct osd_object *obj = it->oi_obj;
2938 iam_it_fini(&it->oi_it);
2939 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2940 lu_object_put(env, &obj->oo_dt.do_lu);
2944 * Move Iterator to record specified by \a key
2946 * \param di osd iterator
2947 * \param key key for index
2949 * \retval +ve di points to record with least key not larger than key
2950 * \retval 0 di points to exact matched key
2951 * \retval -ve failure
2954 static int osd_it_iam_get(const struct lu_env *env,
2955 struct dt_it *di, const struct dt_key *key)
2957 struct osd_it_iam *it = (struct osd_it_iam *)di;
2959 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2965 * \param di osd iterator
2968 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2970 struct osd_it_iam *it = (struct osd_it_iam *)di;
2972 iam_it_put(&it->oi_it);
2976 * Move iterator by one record
2978 * \param di osd iterator
2980 * \retval +1 end of container reached
2982 * \retval -ve failure
2985 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2987 struct osd_it_iam *it = (struct osd_it_iam *)di;
2989 return iam_it_next(&it->oi_it);
2993 * Return pointer to the key under iterator.
2996 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2997 const struct dt_it *di)
2999 struct osd_it_iam *it = (struct osd_it_iam *)di;
3001 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3005 * Return size of key under iterator (in bytes)
3008 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3010 struct osd_it_iam *it = (struct osd_it_iam *)di;
3012 return iam_it_key_size(&it->oi_it);
3015 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3020 struct luda_type *lt;
3021 const unsigned align = sizeof(struct luda_type) - 1;
3023 /* check if file type is required */
3024 if (attr & LUDA_TYPE) {
3025 len = (len + align) & ~align;
3027 lt = (void *) ent->lde_name + len;
3028 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3029 ent->lde_attrs |= LUDA_TYPE;
3032 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3036 * build lu direct from backend fs dirent.
3039 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3047 fid_cpu_to_le(&ent->lde_fid, fid);
3048 ent->lde_attrs = LUDA_FID;
3050 ent->lde_hash = cpu_to_le64(offset);
3051 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3053 strncpy(ent->lde_name, name, namelen);
3054 ent->lde_namelen = cpu_to_le16(namelen);
3056 /* append lustre attributes */
3057 osd_it_append_attrs(ent, attr, namelen, type);
3061 * Return pointer to the record under iterator.
3063 static int osd_it_iam_rec(const struct lu_env *env,
3064 const struct dt_it *di,
3065 struct lu_dirent *lde,
3068 struct osd_it_iam *it = (struct osd_it_iam *)di;
3069 struct osd_thread_info *info = osd_oti_get(env);
3070 struct lu_fid *fid = &info->oti_fid;
3071 const struct osd_fid_pack *rec;
3077 name = (char *)iam_it_key_get(&it->oi_it);
3079 RETURN(PTR_ERR(name));
3081 namelen = iam_it_key_size(&it->oi_it);
3083 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3085 RETURN(PTR_ERR(rec));
3087 rc = osd_fid_unpack(fid, rec);
3091 hash = iam_it_store(&it->oi_it);
3093 /* IAM does not store object type in IAM index (dir) */
3094 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3101 * Returns cookie for current Iterator position.
3103 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3105 struct osd_it_iam *it = (struct osd_it_iam *)di;
3107 return iam_it_store(&it->oi_it);
3111 * Restore iterator from cookie.
3113 * \param di osd iterator
3114 * \param hash Iterator location cookie
3116 * \retval +ve di points to record with least key not larger than key.
3117 * \retval 0 di points to exact matched key
3118 * \retval -ve failure
3121 static int osd_it_iam_load(const struct lu_env *env,
3122 const struct dt_it *di, __u64 hash)
3124 struct osd_it_iam *it = (struct osd_it_iam *)di;
3126 return iam_it_load(&it->oi_it, hash);
3129 static const struct dt_index_operations osd_index_iam_ops = {
3130 .dio_lookup = osd_index_iam_lookup,
3131 .dio_insert = osd_index_iam_insert,
3132 .dio_delete = osd_index_iam_delete,
3134 .init = osd_it_iam_init,
3135 .fini = osd_it_iam_fini,
3136 .get = osd_it_iam_get,
3137 .put = osd_it_iam_put,
3138 .next = osd_it_iam_next,
3139 .key = osd_it_iam_key,
3140 .key_size = osd_it_iam_key_size,
3141 .rec = osd_it_iam_rec,
3142 .store = osd_it_iam_store,
3143 .load = osd_it_iam_load
3148 * Creates or initializes iterator context.
3150 * \retval struct osd_it_ea, iterator structure on success
3153 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3154 struct dt_object *dt,
3155 struct lustre_capa *capa)
3157 struct osd_object *obj = osd_dt_obj(dt);
3158 struct osd_thread_info *info = osd_oti_get(env);
3159 struct osd_it_ea *it = &info->oti_it_ea;
3160 struct lu_object *lo = &dt->do_lu;
3161 struct dentry *obj_dentry = &info->oti_it_dentry;
3163 LASSERT(lu_object_exists(lo));
3165 obj_dentry->d_inode = obj->oo_inode;
3166 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3167 obj_dentry->d_name.hash = 0;
3169 it->oie_rd_dirent = 0;
3170 it->oie_it_dirent = 0;
3171 it->oie_dirent = NULL;
3172 it->oie_buf = info->oti_it_ea_buf;
3174 it->oie_file.f_pos = 0;
3175 it->oie_file.f_dentry = obj_dentry;
3176 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3177 it->oie_file.f_op = obj->oo_inode->i_fop;
3178 it->oie_file.private_data = NULL;
3180 RETURN((struct dt_it *) it);
3184 * Destroy or finishes iterator context.
3186 * \param di, struct osd_it_ea, iterator structure to be destroyed
3188 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3190 struct osd_it_ea *it = (struct osd_it_ea *)di;
3191 struct osd_object *obj = it->oie_obj;
3192 struct inode *inode = obj->oo_inode;
3195 it->oie_file.f_op->release(inode, &it->oie_file);
3196 lu_object_put(env, &obj->oo_dt.do_lu);
3201 * It position the iterator at given key, so that next lookup continues from
3202 * that key Or it is similar to dio_it->load() but based on a key,
3203 * rather than file position.
3205 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3208 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3210 static int osd_it_ea_get(const struct lu_env *env,
3211 struct dt_it *di, const struct dt_key *key)
3213 struct osd_it_ea *it = (struct osd_it_ea *)di;
3216 LASSERT(((const char *)key)[0] == '\0');
3217 it->oie_file.f_pos = 0;
3218 it->oie_rd_dirent = 0;
3219 it->oie_it_dirent = 0;
3220 it->oie_dirent = NULL;
3228 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3233 * It is called internally by ->readdir(). It fills the
3234 * iterator's in-memory data structure with required
3235 * information i.e. name, namelen, rec_size etc.
3237 * \param buf, in which information to be filled in.
3238 * \param name, name of the file in given dir
3240 * \retval 0, on success
3241 * \retval 1, on buffer full
3243 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3244 loff_t offset, __u64 ino,
3247 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3248 struct osd_it_ea_dirent *ent = it->oie_dirent;
3251 /* this should never happen */
3252 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3253 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3257 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3261 ent->oied_ino = ino;
3262 ent->oied_off = offset;
3263 ent->oied_namelen = namelen;
3264 ent->oied_type = d_type;
3266 memcpy(ent->oied_name, name, namelen);
3268 it->oie_rd_dirent++;
3269 it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3274 * Calls ->readdir() to load a directory entry at a time
3275 * and stored it in iterator's in-memory data structure.
3277 * \param di, struct osd_it_ea, iterator's in memory structure
3279 * \retval 0, on success
3280 * \retval -ve, on error
3282 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3284 struct osd_it_ea *it = (struct osd_it_ea *)di;
3285 struct osd_object *obj = it->oie_obj;
3286 struct inode *inode = obj->oo_inode;
3290 it->oie_dirent = it->oie_buf;
3291 it->oie_rd_dirent = 0;
3293 down_read(&obj->oo_ext_idx_sem);
3294 result = inode->i_fop->readdir(&it->oie_file, it,
3295 (filldir_t) osd_ldiskfs_filldir);
3297 up_read(&obj->oo_ext_idx_sem);
3299 if (it->oie_rd_dirent == 0) {
3302 it->oie_dirent = it->oie_buf;
3303 it->oie_it_dirent = 1;
3310 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3311 * to load a directory entry at a time and stored it in
3312 * iterator's in-memory data structure.
3314 * \param di, struct osd_it_ea, iterator's in memory structure
3316 * \retval +ve, iterator reached to end
3317 * \retval 0, iterator not reached to end
3318 * \retval -ve, on error
3320 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3322 struct osd_it_ea *it = (struct osd_it_ea *)di;
3327 if (it->oie_it_dirent < it->oie_rd_dirent) {
3328 it->oie_dirent = (void *) it->oie_dirent +
3329 size_round(sizeof(struct osd_it_ea_dirent) +
3330 it->oie_dirent->oied_namelen);
3331 it->oie_it_dirent++;
3334 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3337 rc = osd_ldiskfs_it_fill(di);
3344 * Returns the key at current position from iterator's in memory structure.
3346 * \param di, struct osd_it_ea, iterator's in memory structure
3348 * \retval key i.e. struct dt_key on success
3350 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3351 const struct dt_it *di)
3353 struct osd_it_ea *it = (struct osd_it_ea *)di;
3355 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3359 * Returns the key's size at current position from iterator's in memory structure.
3361 * \param di, struct osd_it_ea, iterator's in memory structure
3363 * \retval key_size i.e. struct dt_key on success
3365 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3367 struct osd_it_ea *it = (struct osd_it_ea *)di;
3369 RETURN(it->oie_dirent->oied_namelen);
3374 * Returns the value (i.e. fid/igif) at current position from iterator's
3375 * in memory structure.
3377 * \param di, struct osd_it_ea, iterator's in memory structure
3378 * \param attr, attr requested for dirent.
3379 * \param lde, lustre dirent
3381 * \retval 0, no error and \param lde has correct lustre dirent.
3382 * \retval -ve, on error
3384 static inline int osd_it_ea_rec(const struct lu_env *env,
3385 const struct dt_it *di,
3386 struct lu_dirent *lde,
3389 struct osd_it_ea *it = (struct osd_it_ea *)di;
3390 struct osd_object *obj = it->oie_obj;
3391 struct osd_thread_info *info = osd_oti_get(env);
3392 struct lu_fid *fid = &info->oti_fid;
3397 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3400 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3401 it->oie_dirent->oied_name,
3402 it->oie_dirent->oied_namelen,
3403 it->oie_dirent->oied_type,
3409 * Returns a cookie for current position of the iterator head, so that
3410 * user can use this cookie to load/start the iterator next time.
3412 * \param di, struct osd_it_ea, iterator's in memory structure
3414 * \retval cookie for current position, on success
3416 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3418 struct osd_it_ea *it = (struct osd_it_ea *)di;
3420 RETURN(it->oie_dirent->oied_off);
3424 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3425 * to load a directory entry at a time and stored it i inn,
3426 * in iterator's in-memory data structure.
3428 * \param di, struct osd_it_ea, iterator's in memory structure
3430 * \retval +ve, on success
3431 * \retval -ve, on error
3433 static int osd_it_ea_load(const struct lu_env *env,
3434 const struct dt_it *di, __u64 hash)
3436 struct osd_it_ea *it = (struct osd_it_ea *)di;
3440 it->oie_file.f_pos = hash;
3442 rc = osd_ldiskfs_it_fill(di);
3450 * Index lookup function for interoperability mode (b11826).
3452 * \param key, key i.e. file name to be searched
3454 * \retval +ve, on success
3455 * \retval -ve, on error
3457 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3458 struct dt_rec *rec, const struct dt_key *key,
3459 struct lustre_capa *capa)
3461 struct osd_object *obj = osd_dt_obj(dt);
3466 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3467 LINVRNT(osd_invariant(obj));
3469 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3472 rc = osd_ea_lookup_rec(env, obj, rec, key);
3480 * Index and Iterator operations for interoperability
3481 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3483 static const struct dt_index_operations osd_index_ea_ops = {
3484 .dio_lookup = osd_index_ea_lookup,
3485 .dio_insert = osd_index_ea_insert,
3486 .dio_delete = osd_index_ea_delete,
3488 .init = osd_it_ea_init,
3489 .fini = osd_it_ea_fini,
3490 .get = osd_it_ea_get,
3491 .put = osd_it_ea_put,
3492 .next = osd_it_ea_next,
3493 .key = osd_it_ea_key,
3494 .key_size = osd_it_ea_key_size,
3495 .rec = osd_it_ea_rec,
3496 .store = osd_it_ea_store,
3497 .load = osd_it_ea_load
3501 static void *osd_key_init(const struct lu_context *ctx,
3502 struct lu_context_key *key)
3504 struct osd_thread_info *info;
3506 OBD_ALLOC_PTR(info);
3508 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3509 if (info->oti_it_ea_buf != NULL) {
3510 info->oti_env = container_of(ctx, struct lu_env,
3514 info = ERR_PTR(-ENOMEM);
3517 info = ERR_PTR(-ENOMEM);
3522 static void osd_key_fini(const struct lu_context *ctx,
3523 struct lu_context_key *key, void* data)
3525 struct osd_thread_info *info = data;
3527 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3531 static void osd_key_exit(const struct lu_context *ctx,
3532 struct lu_context_key *key, void *data)
3534 struct osd_thread_info *info = data;
3536 LASSERT(info->oti_r_locks == 0);
3537 LASSERT(info->oti_w_locks == 0);
3538 LASSERT(info->oti_txns == 0);
3541 /* type constructor/destructor: osd_type_init, osd_type_fini */
3542 LU_TYPE_INIT_FINI(osd, &osd_key);
3544 static struct lu_context_key osd_key = {
3545 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3546 .lct_init = osd_key_init,
3547 .lct_fini = osd_key_fini,
3548 .lct_exit = osd_key_exit
3552 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3553 const char *name, struct lu_device *next)
3556 struct lu_context *ctx;
3558 /* context for commit hooks */
3559 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3560 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3562 rc = osd_procfs_init(osd_dev(d), name);
3563 ctx->lc_cookie = 0x3;
3568 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3570 struct osd_thread_info *info = osd_oti_get(env);
3572 if (o->od_obj_area != NULL) {
3573 lu_object_put(env, &o->od_obj_area->do_lu);
3574 o->od_obj_area = NULL;
3576 osd_oi_fini(info, &o->od_oi);
3581 static int osd_mount(const struct lu_env *env,
3582 struct osd_device *o, struct lustre_cfg *cfg)
3584 struct lustre_mount_info *lmi;
3585 const char *dev = lustre_cfg_string(cfg, 0);
3586 struct lustre_disk_data *ldd;
3587 struct lustre_sb_info *lsi;
3591 if (o->od_mount != NULL) {
3592 CERROR("Already mounted (%s)\n", dev);
3597 lmi = server_get_mount(dev);
3599 CERROR("Cannot get mount info for %s!\n", dev);
3603 LASSERT(lmi != NULL);
3604 /* save lustre_mount_info in dt_device */
3607 lsi = s2lsi(lmi->lmi_sb);
3610 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3612 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3616 o->od_obj_area = NULL;
3620 static struct lu_device *osd_device_fini(const struct lu_env *env,
3621 struct lu_device *d)
3626 shrink_dcache_sb(osd_sb(osd_dev(d)));
3627 osd_sync(env, lu2dt_dev(d));
3629 rc = osd_procfs_fini(osd_dev(d));
3631 CERROR("proc fini error %d \n", rc);
3632 RETURN (ERR_PTR(rc));
3635 if (osd_dev(d)->od_mount)
3636 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3637 osd_dev(d)->od_mount->lmi_mnt);
3638 osd_dev(d)->od_mount = NULL;
3640 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3644 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3645 struct lu_device_type *t,
3646 struct lustre_cfg *cfg)
3648 struct lu_device *l;
3649 struct osd_device *o;
3655 result = dt_device_init(&o->od_dt_dev, t);
3658 l->ld_ops = &osd_lu_ops;
3659 o->od_dt_dev.dd_ops = &osd_dt_ops;
3660 spin_lock_init(&o->od_osfs_lock);
3661 o->od_osfs_age = cfs_time_shift_64(-1000);
3662 o->od_capa_hash = init_capa_hash();
3663 if (o->od_capa_hash == NULL) {
3664 dt_device_fini(&o->od_dt_dev);
3665 l = ERR_PTR(-ENOMEM);
3668 l = ERR_PTR(result);
3673 l = ERR_PTR(-ENOMEM);
3677 static struct lu_device *osd_device_free(const struct lu_env *env,
3678 struct lu_device *d)
3680 struct osd_device *o = osd_dev(d);
3683 cleanup_capa_hash(o->od_capa_hash);
3684 dt_device_fini(&o->od_dt_dev);
3689 static int osd_process_config(const struct lu_env *env,
3690 struct lu_device *d, struct lustre_cfg *cfg)
3692 struct osd_device *o = osd_dev(d);
3696 switch(cfg->lcfg_command) {
3698 err = osd_mount(env, o, cfg);
3701 err = osd_shutdown(env, o);
3710 static int osd_recovery_complete(const struct lu_env *env,
3711 struct lu_device *d)
3713 struct osd_device *o = osd_dev(d);
3715 /* TODO: orphans handling */
3716 ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3720 static int osd_prepare(const struct lu_env *env,
3721 struct lu_device *pdev,
3722 struct lu_device *dev)
3724 struct osd_device *osd = osd_dev(dev);
3725 struct lustre_sb_info *lsi;
3726 struct lustre_disk_data *ldd;
3727 struct lustre_mount_info *lmi;
3728 struct osd_thread_info *oti = osd_oti_get(env);
3729 struct dt_object *d;
3733 /* 1. initialize oi before any file create or file open */
3734 result = osd_oi_init(oti, &osd->od_oi,
3735 &osd->od_dt_dev, lu2md_dev(pdev));
3739 lmi = osd->od_mount;
3740 lsi = s2lsi(lmi->lmi_sb);
3743 /* 2. setup local objects */
3744 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3748 /* 3. open remote object dir */
3749 d = dt_store_open(env, lu2dt_dev(dev), "",
3750 remote_obj_dir, &oti->oti_fid);
3752 osd->od_obj_area = d;
3755 result = PTR_ERR(d);
3756 osd->od_obj_area = NULL;
3763 static const struct lu_object_operations osd_lu_obj_ops = {
3764 .loo_object_init = osd_object_init,
3765 .loo_object_delete = osd_object_delete,
3766 .loo_object_release = osd_object_release,
3767 .loo_object_free = osd_object_free,
3768 .loo_object_print = osd_object_print,
3769 .loo_object_invariant = osd_object_invariant
3772 static const struct lu_device_operations osd_lu_ops = {
3773 .ldo_object_alloc = osd_object_alloc,
3774 .ldo_process_config = osd_process_config,
3775 .ldo_recovery_complete = osd_recovery_complete,
3776 .ldo_prepare = osd_prepare,
3779 static const struct lu_device_type_operations osd_device_type_ops = {
3780 .ldto_init = osd_type_init,
3781 .ldto_fini = osd_type_fini,
3783 .ldto_start = osd_type_start,
3784 .ldto_stop = osd_type_stop,
3786 .ldto_device_alloc = osd_device_alloc,
3787 .ldto_device_free = osd_device_free,
3789 .ldto_device_init = osd_device_init,
3790 .ldto_device_fini = osd_device_fini
3793 static struct lu_device_type osd_device_type = {
3794 .ldt_tags = LU_DEVICE_DT,
3795 .ldt_name = LUSTRE_OSD_NAME,
3796 .ldt_ops = &osd_device_type_ops,
3797 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3801 * lprocfs legacy support.
3803 static struct obd_ops osd_obd_device_ops = {
3804 .o_owner = THIS_MODULE
3807 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3808 .llod_name = remote_obj_dir,
3809 .llod_oid = OSD_REM_OBJ_DIR_OID,
3811 .llod_feat = &dt_directory_features,
3814 static int __init osd_mod_init(void)
3816 struct lprocfs_static_vars lvars;
3819 llo_local_obj_register(&llod_osd_rem_obj_dir);
3820 lprocfs_osd_init_vars(&lvars);
3821 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3822 LUSTRE_OSD_NAME, &osd_device_type);
3825 static void __exit osd_mod_exit(void)
3827 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3828 class_unregister_type(LUSTRE_OSD_NAME);
3831 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3832 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3833 MODULE_LICENSE("GPL");
3835 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);