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 lu_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 static inline void osd_fid_pack(const struct lu_env *env, const struct lu_fid *fid,
1736 struct dt_rec *pack)
1738 fid_pack((struct lu_fid_pack *)pack, fid, &osd_oti_get(env)->oti_fid);
1742 * Try to read the fid from inode ea into dt_rec, if return value
1743 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1745 * \param fid, object fid.
1747 * \retval 0, on success
1749 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1750 __u32 ino, struct lu_fid *fid)
1752 struct osd_thread_info *info = osd_oti_get(env);
1753 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1754 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1755 struct dentry *dentry = &info->oti_child_dentry;
1756 struct osd_inode_id *id = &info->oti_id;
1757 struct osd_device *dev;
1758 struct inode *inode;
1762 dev = osd_dev(ldev);
1765 id->oii_gen = OSD_OII_NOGEN;
1767 inode = osd_iget(info, dev, id);
1768 if (IS_ERR(inode)) {
1769 rc = PTR_ERR(inode);
1772 dentry->d_inode = inode;
1774 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1775 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1778 /* Check LMA compatibility */
1780 (mdt_attrs->lma_incompat & ~cpu_to_be32(LMA_INCOMPAT_SUPP))) {
1781 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1782 inode->i_ino, be32_to_cpu(mdt_attrs->lma_incompat) &
1783 ~LMA_INCOMPAT_SUPP);
1788 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1790 } else if (rc == -ENODATA) {
1791 osd_igif_get(env, inode, fid);
1801 * OSD layer object create function for interoperability mode (b11826).
1802 * This is mostly similar to osd_object_create(). Only difference being, fid is
1803 * inserted into inode ea here.
1805 * \retval 0, on success
1806 * \retval -ve, on error
1808 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1809 struct lu_attr *attr,
1810 struct dt_allocation_hint *hint,
1811 struct dt_object_format *dof,
1814 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1815 struct osd_object *obj = osd_dt_obj(dt);
1816 struct osd_thread_info *info = osd_oti_get(env);
1822 LASSERT(osd_invariant(obj));
1823 LASSERT(!dt_object_exists(dt));
1824 LASSERT(osd_write_locked(env, obj));
1825 LASSERT(th != NULL);
1827 result = __osd_object_create(info, obj, attr, hint, dof, th);
1829 if (hint && hint->dah_parent)
1830 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1832 /* objects under osd root shld have igif fid, so dont add fid EA */
1833 if (result == 0 && is_root == 0)
1834 result = osd_ea_fid_set(env, dt, fid);
1837 result = __osd_oi_insert(env, obj, fid, th);
1839 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1840 LINVRNT(osd_invariant(obj));
1845 * Concurrency: @dt is write locked.
1847 static void osd_object_ref_add(const struct lu_env *env,
1848 struct dt_object *dt,
1851 struct osd_object *obj = osd_dt_obj(dt);
1852 struct inode *inode = obj->oo_inode;
1854 LINVRNT(osd_invariant(obj));
1855 LASSERT(dt_object_exists(dt));
1856 LASSERT(osd_write_locked(env, obj));
1857 LASSERT(th != NULL);
1859 spin_lock(&obj->oo_guard);
1860 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1862 spin_unlock(&obj->oo_guard);
1863 mark_inode_dirty(inode);
1864 LINVRNT(osd_invariant(obj));
1868 * Concurrency: @dt is write locked.
1870 static void osd_object_ref_del(const struct lu_env *env,
1871 struct dt_object *dt,
1874 struct osd_object *obj = osd_dt_obj(dt);
1875 struct inode *inode = obj->oo_inode;
1877 LINVRNT(osd_invariant(obj));
1878 LASSERT(dt_object_exists(dt));
1879 LASSERT(osd_write_locked(env, obj));
1880 LASSERT(th != NULL);
1882 spin_lock(&obj->oo_guard);
1883 LASSERT(inode->i_nlink > 0);
1885 spin_unlock(&obj->oo_guard);
1886 mark_inode_dirty(inode);
1887 LINVRNT(osd_invariant(obj));
1891 * Concurrency: @dt is read locked.
1893 static int osd_xattr_get(const struct lu_env *env,
1894 struct dt_object *dt,
1897 struct lustre_capa *capa)
1899 struct osd_object *obj = osd_dt_obj(dt);
1900 struct inode *inode = obj->oo_inode;
1901 struct osd_thread_info *info = osd_oti_get(env);
1902 struct dentry *dentry = &info->oti_obj_dentry;
1904 LASSERT(dt_object_exists(dt));
1905 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1906 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1908 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1911 dentry->d_inode = inode;
1912 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1916 * Concurrency: @dt is write locked.
1918 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1919 const struct lu_buf *buf, const char *name, int fl,
1920 struct thandle *handle, struct lustre_capa *capa)
1922 LASSERT(handle != NULL);
1924 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1927 return __osd_xattr_set(env, dt, buf, name, fl);
1931 * Concurrency: @dt is read locked.
1933 static int osd_xattr_list(const struct lu_env *env,
1934 struct dt_object *dt,
1936 struct lustre_capa *capa)
1938 struct osd_object *obj = osd_dt_obj(dt);
1939 struct inode *inode = obj->oo_inode;
1940 struct osd_thread_info *info = osd_oti_get(env);
1941 struct dentry *dentry = &info->oti_obj_dentry;
1943 LASSERT(dt_object_exists(dt));
1944 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1945 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1947 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1950 dentry->d_inode = inode;
1951 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1955 * Concurrency: @dt is write locked.
1957 static int osd_xattr_del(const struct lu_env *env,
1958 struct dt_object *dt,
1960 struct thandle *handle,
1961 struct lustre_capa *capa)
1963 struct osd_object *obj = osd_dt_obj(dt);
1964 struct inode *inode = obj->oo_inode;
1965 struct osd_thread_info *info = osd_oti_get(env);
1966 struct dentry *dentry = &info->oti_obj_dentry;
1967 struct timespec *t = &info->oti_time;
1970 LASSERT(dt_object_exists(dt));
1971 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1972 LASSERT(osd_write_locked(env, obj));
1973 LASSERT(handle != NULL);
1975 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1978 dentry->d_inode = inode;
1979 *t = inode->i_ctime;
1980 rc = inode->i_op->removexattr(dentry, name);
1981 /* ctime should not be updated with server-side time. */
1982 spin_lock(&obj->oo_guard);
1983 inode->i_ctime = *t;
1984 spin_unlock(&obj->oo_guard);
1985 mark_inode_dirty(inode);
1989 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1990 struct dt_object *dt,
1991 struct lustre_capa *old,
1994 struct osd_thread_info *info = osd_oti_get(env);
1995 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1996 struct osd_object *obj = osd_dt_obj(dt);
1997 struct osd_device *dev = osd_obj2dev(obj);
1998 struct lustre_capa_key *key = &info->oti_capa_key;
1999 struct lustre_capa *capa = &info->oti_capa;
2000 struct obd_capa *oc;
2001 struct md_capainfo *ci;
2005 if (!dev->od_fl_capa)
2006 RETURN(ERR_PTR(-ENOENT));
2008 LASSERT(dt_object_exists(dt));
2009 LINVRNT(osd_invariant(obj));
2011 /* renewal sanity check */
2012 if (old && osd_object_auth(env, dt, old, opc))
2013 RETURN(ERR_PTR(-EACCES));
2015 ci = md_capainfo(env);
2017 RETURN(ERR_PTR(-ENOENT));
2019 switch (ci->mc_auth) {
2023 capa->lc_uid = obj->oo_inode->i_uid;
2024 capa->lc_gid = obj->oo_inode->i_gid;
2025 capa->lc_flags = LC_ID_PLAIN;
2027 case LC_ID_CONVERT: {
2030 s[0] = obj->oo_inode->i_uid;
2031 get_random_bytes(&(s[1]), sizeof(__u32));
2032 s[2] = obj->oo_inode->i_gid;
2033 get_random_bytes(&(s[3]), sizeof(__u32));
2034 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2036 RETURN(ERR_PTR(rc));
2038 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2039 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2040 capa->lc_flags = LC_ID_CONVERT;
2044 RETURN(ERR_PTR(-EINVAL));
2047 capa->lc_fid = *fid;
2049 capa->lc_flags |= dev->od_capa_alg << 24;
2050 capa->lc_timeout = dev->od_capa_timeout;
2051 capa->lc_expiry = 0;
2053 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2055 LASSERT(!capa_is_expired(oc));
2059 spin_lock(&capa_lock);
2060 *key = dev->od_capa_keys[1];
2061 spin_unlock(&capa_lock);
2063 capa->lc_keyid = key->lk_keyid;
2064 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2066 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2068 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2069 RETURN(ERR_PTR(rc));
2072 oc = capa_add(dev->od_capa_hash, capa);
2076 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2079 struct osd_object *obj = osd_dt_obj(dt);
2080 struct inode *inode = obj->oo_inode;
2081 struct osd_thread_info *info = osd_oti_get(env);
2082 struct dentry *dentry = &info->oti_obj_dentry;
2083 struct file *file = &info->oti_file;
2086 dentry->d_inode = inode;
2087 file->f_dentry = dentry;
2088 file->f_mapping = inode->i_mapping;
2089 file->f_op = inode->i_fop;
2090 LOCK_INODE_MUTEX(inode);
2091 rc = file->f_op->fsync(file, dentry, 0);
2092 UNLOCK_INODE_MUTEX(inode);
2097 * Get the 64-bit version for an inode.
2099 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2100 struct dt_object *dt)
2102 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2104 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2105 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2106 return LDISKFS_I(inode)->i_fs_version;
2110 * Set the 64-bit version and return the old version.
2112 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2113 dt_obj_version_t new_version)
2115 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2117 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2118 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2119 LDISKFS_I(inode)->i_fs_version = new_version;
2120 /** Version is set after all inode operations are finished,
2121 * so we should mark it dirty here */
2122 inode->i_sb->s_op->dirty_inode(inode);
2125 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2128 struct osd_object *obj = osd_dt_obj(dt);
2131 *data = (void *)obj->oo_inode;
2139 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2140 const struct dt_index_features *feat)
2142 struct iam_descr *descr;
2144 if (osd_object_is_root(o))
2145 return feat == &dt_directory_features;
2147 LASSERT(o->oo_dir != NULL);
2149 descr = o->oo_dir->od_container.ic_descr;
2150 if (feat == &dt_directory_features) {
2151 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2157 feat->dif_keysize_min <= descr->id_key_size &&
2158 descr->id_key_size <= feat->dif_keysize_max &&
2159 feat->dif_recsize_min <= descr->id_rec_size &&
2160 descr->id_rec_size <= feat->dif_recsize_max &&
2161 !(feat->dif_flags & (DT_IND_VARKEY |
2162 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2163 ergo(feat->dif_flags & DT_IND_UPDATE,
2164 1 /* XXX check that object (and file system) is
2169 static int osd_iam_container_init(const struct lu_env *env,
2170 struct osd_object *obj,
2171 struct osd_directory *dir)
2174 struct iam_container *bag;
2176 bag = &dir->od_container;
2177 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2179 result = iam_container_setup(bag);
2181 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2183 iam_container_fini(bag);
2190 * Concurrency: no external locking is necessary.
2192 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2193 const struct dt_index_features *feat)
2197 struct osd_object *obj = osd_dt_obj(dt);
2198 struct osd_device *osd = osd_obj2dev(obj);
2200 LINVRNT(osd_invariant(obj));
2201 LASSERT(dt_object_exists(dt));
2203 if (osd_object_is_root(obj)) {
2204 dt->do_index_ops = &osd_index_ea_ops;
2206 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2207 dt->do_index_ops = &osd_index_ea_ops;
2208 if (S_ISDIR(obj->oo_inode->i_mode))
2213 } else if (!osd_has_index(obj)) {
2214 struct osd_directory *dir;
2219 spin_lock(&obj->oo_guard);
2220 if (obj->oo_dir == NULL)
2224 * Concurrent thread allocated container data.
2227 spin_unlock(&obj->oo_guard);
2229 * Now, that we have container data, serialize its
2232 down_write(&obj->oo_ext_idx_sem);
2234 * recheck under lock.
2236 if (!osd_has_index(obj))
2237 result = osd_iam_container_init(env, obj, dir);
2240 up_write(&obj->oo_ext_idx_sem);
2246 if (result == 0 && ea_dir == 0) {
2247 if (!osd_iam_index_probe(env, obj, feat))
2250 LINVRNT(osd_invariant(obj));
2255 static const struct dt_object_operations osd_obj_ops = {
2256 .do_read_lock = osd_object_read_lock,
2257 .do_write_lock = osd_object_write_lock,
2258 .do_read_unlock = osd_object_read_unlock,
2259 .do_write_unlock = osd_object_write_unlock,
2260 .do_write_locked = osd_object_write_locked,
2261 .do_attr_get = osd_attr_get,
2262 .do_attr_set = osd_attr_set,
2263 .do_ah_init = osd_ah_init,
2264 .do_create = osd_object_create,
2265 .do_index_try = osd_index_try,
2266 .do_ref_add = osd_object_ref_add,
2267 .do_ref_del = osd_object_ref_del,
2268 .do_xattr_get = osd_xattr_get,
2269 .do_xattr_set = osd_xattr_set,
2270 .do_xattr_del = osd_xattr_del,
2271 .do_xattr_list = osd_xattr_list,
2272 .do_capa_get = osd_capa_get,
2273 .do_object_sync = osd_object_sync,
2274 .do_version_get = osd_object_version_get,
2275 .do_version_set = osd_object_version_set,
2276 .do_data_get = osd_data_get,
2280 * dt_object_operations for interoperability mode
2281 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2283 static const struct dt_object_operations osd_obj_ea_ops = {
2284 .do_read_lock = osd_object_read_lock,
2285 .do_write_lock = osd_object_write_lock,
2286 .do_read_unlock = osd_object_read_unlock,
2287 .do_write_unlock = osd_object_write_unlock,
2288 .do_write_locked = osd_object_write_locked,
2289 .do_attr_get = osd_attr_get,
2290 .do_attr_set = osd_attr_set,
2291 .do_ah_init = osd_ah_init,
2292 .do_create = osd_object_ea_create,
2293 .do_index_try = osd_index_try,
2294 .do_ref_add = osd_object_ref_add,
2295 .do_ref_del = osd_object_ref_del,
2296 .do_xattr_get = osd_xattr_get,
2297 .do_xattr_set = osd_xattr_set,
2298 .do_xattr_del = osd_xattr_del,
2299 .do_xattr_list = osd_xattr_list,
2300 .do_capa_get = osd_capa_get,
2301 .do_object_sync = osd_object_sync,
2302 .do_version_get = osd_object_version_get,
2303 .do_version_set = osd_object_version_set,
2304 .do_data_get = osd_data_get,
2312 * XXX: Another layering violation for now.
2314 * We don't want to use ->f_op->read methods, because generic file write
2316 * - serializes on ->i_sem, and
2318 * - does a lot of extra work like balance_dirty_pages(),
2320 * which doesn't work for globally shared files like /last-received.
2322 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2323 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2324 loff_t *offs, handle_t *handle);
2326 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2327 struct lu_buf *buf, loff_t *pos,
2328 struct lustre_capa *capa)
2330 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2332 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2335 return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2338 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2339 const struct lu_buf *buf, loff_t *pos,
2340 struct thandle *handle, struct lustre_capa *capa,
2343 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2344 struct osd_thandle *oh;
2346 #ifdef HAVE_QUOTA_SUPPORT
2347 cfs_cap_t save = current->cap_effective;
2350 LASSERT(handle != NULL);
2352 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2355 oh = container_of(handle, struct osd_thandle, ot_super);
2356 LASSERT(oh->ot_handle->h_transaction != NULL);
2357 #ifdef HAVE_QUOTA_SUPPORT
2359 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2361 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2363 result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2364 pos, oh->ot_handle);
2365 #ifdef HAVE_QUOTA_SUPPORT
2366 current->cap_effective = save;
2369 result = buf->lb_len;
2373 static const struct dt_body_operations osd_body_ops = {
2374 .dbo_read = osd_read,
2375 .dbo_write = osd_write
2380 * delete a (key, value) pair from index \a dt specified by \a key
2382 * \param dt_object osd index object
2383 * \param key key for index
2384 * \param rec record reference
2385 * \param handle transaction handler
2388 * \retval -ve failure
2391 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2392 const struct dt_key *key, struct thandle *handle,
2393 struct lustre_capa *capa)
2395 struct osd_object *obj = osd_dt_obj(dt);
2396 struct osd_thandle *oh;
2397 struct iam_path_descr *ipd;
2398 struct iam_container *bag = &obj->oo_dir->od_container;
2403 LINVRNT(osd_invariant(obj));
2404 LASSERT(dt_object_exists(dt));
2405 LASSERT(bag->ic_object == obj->oo_inode);
2406 LASSERT(handle != NULL);
2408 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2411 ipd = osd_idx_ipd_get(env, bag);
2412 if (unlikely(ipd == NULL))
2415 oh = container_of0(handle, struct osd_thandle, ot_super);
2416 LASSERT(oh->ot_handle != NULL);
2417 LASSERT(oh->ot_handle->h_transaction != NULL);
2419 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2420 osd_ipd_put(env, bag, ipd);
2421 LINVRNT(osd_invariant(obj));
2426 * Index delete function for interoperability mode (b11826).
2427 * It will remove the directory entry added by osd_index_ea_insert().
2428 * This entry is needed to maintain name->fid mapping.
2430 * \param key, key i.e. file entry to be deleted
2432 * \retval 0, on success
2433 * \retval -ve, on error
2435 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2436 const struct dt_key *key, struct thandle *handle,
2437 struct lustre_capa *capa)
2439 struct osd_object *obj = osd_dt_obj(dt);
2440 struct inode *dir = obj->oo_inode;
2441 struct dentry *dentry;
2442 struct osd_thandle *oh;
2443 struct ldiskfs_dir_entry_2 *de;
2444 struct buffer_head *bh;
2450 LINVRNT(osd_invariant(obj));
2451 LASSERT(dt_object_exists(dt));
2452 LASSERT(handle != NULL);
2454 oh = container_of(handle, struct osd_thandle, ot_super);
2455 LASSERT(oh->ot_handle != NULL);
2456 LASSERT(oh->ot_handle->h_transaction != NULL);
2458 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2461 dentry = osd_child_dentry_get(env, obj,
2462 (char *)key, strlen((char *)key));
2464 down_write(&obj->oo_ext_idx_sem);
2465 bh = ldiskfs_find_entry(dentry, &de);
2467 struct osd_thread_info *oti = osd_oti_get(env);
2468 struct timespec *ctime = &oti->oti_time;
2469 struct timespec *mtime = &oti->oti_time2;
2471 *ctime = dir->i_ctime;
2472 *mtime = dir->i_mtime;
2473 rc = ldiskfs_delete_entry(oh->ot_handle,
2475 /* xtime should not be updated with server-side time. */
2476 spin_lock(&obj->oo_guard);
2477 dir->i_ctime = *ctime;
2478 dir->i_mtime = *mtime;
2479 spin_unlock(&obj->oo_guard);
2480 mark_inode_dirty(dir);
2485 up_write(&obj->oo_ext_idx_sem);
2486 LASSERT(osd_invariant(obj));
2491 * Lookup index for \a key and copy record to \a rec.
2493 * \param dt_object osd index object
2494 * \param key key for index
2495 * \param rec record reference
2497 * \retval +ve success : exact mach
2498 * \retval 0 return record with key not greater than \a key
2499 * \retval -ve failure
2501 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2502 struct dt_rec *rec, const struct dt_key *key,
2503 struct lustre_capa *capa)
2505 struct osd_object *obj = osd_dt_obj(dt);
2506 struct iam_path_descr *ipd;
2507 struct iam_container *bag = &obj->oo_dir->od_container;
2508 struct osd_thread_info *oti = osd_oti_get(env);
2509 struct iam_iterator *it = &oti->oti_idx_it;
2513 LASSERT(osd_invariant(obj));
2514 LASSERT(dt_object_exists(dt));
2515 LASSERT(bag->ic_object == obj->oo_inode);
2517 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2520 ipd = osd_idx_ipd_get(env, bag);
2524 /* got ipd now we can start iterator. */
2525 iam_it_init(it, bag, 0, ipd);
2527 rc = iam_it_get(it, (struct iam_key *)key);
2529 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2533 osd_ipd_put(env, bag, ipd);
2535 LINVRNT(osd_invariant(obj));
2541 * Inserts (key, value) pair in \a dt index object.
2543 * \param dt osd index object
2544 * \param key key for index
2545 * \param rec record reference
2546 * \param th transaction handler
2549 * \retval -ve failure
2551 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2552 const struct dt_rec *rec, const struct dt_key *key,
2553 struct thandle *th, struct lustre_capa *capa,
2556 struct osd_object *obj = osd_dt_obj(dt);
2557 struct iam_path_descr *ipd;
2558 struct osd_thandle *oh;
2559 struct iam_container *bag = &obj->oo_dir->od_container;
2560 #ifdef HAVE_QUOTA_SUPPORT
2561 cfs_cap_t save = current->cap_effective;
2567 LINVRNT(osd_invariant(obj));
2568 LASSERT(dt_object_exists(dt));
2569 LASSERT(bag->ic_object == obj->oo_inode);
2570 LASSERT(th != NULL);
2572 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2575 ipd = osd_idx_ipd_get(env, bag);
2576 if (unlikely(ipd == NULL))
2579 oh = container_of0(th, struct osd_thandle, ot_super);
2580 LASSERT(oh->ot_handle != NULL);
2581 LASSERT(oh->ot_handle->h_transaction != NULL);
2582 #ifdef HAVE_QUOTA_SUPPORT
2584 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2586 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2588 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2589 (struct iam_rec *)rec, ipd);
2590 #ifdef HAVE_QUOTA_SUPPORT
2591 current->cap_effective = save;
2593 osd_ipd_put(env, bag, ipd);
2594 LINVRNT(osd_invariant(obj));
2599 * Calls ldiskfs_add_entry() to add directory entry
2600 * into the directory. This is required for
2601 * interoperability mode (b11826)
2603 * \retval 0, on success
2604 * \retval -ve, on error
2606 static int __osd_ea_add_rec(struct osd_thread_info *info,
2607 struct osd_object *pobj,
2608 struct osd_object *cobj,
2612 struct dentry *child;
2613 struct osd_thandle *oth;
2614 struct inode *cinode = cobj->oo_inode;
2617 oth = container_of(th, struct osd_thandle, ot_super);
2618 LASSERT(oth->ot_handle != NULL);
2619 LASSERT(oth->ot_handle->h_transaction != NULL);
2621 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2622 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2628 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2629 * into the directory.Also sets flags into osd object to
2630 * indicate dot and dotdot are created. This is required for
2631 * interoperability mode (b11826)
2633 * \param dir directory for dot and dotdot fixup.
2634 * \param obj child object for linking
2636 * \retval 0, on success
2637 * \retval -ve, on error
2639 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2640 struct osd_object *dir,
2641 struct osd_object *obj, const char *name,
2644 struct inode *parent_dir = obj->oo_inode;
2645 struct inode *inode = dir->oo_inode;
2646 struct osd_thandle *oth;
2649 oth = container_of(th, struct osd_thandle, ot_super);
2650 LASSERT(oth->ot_handle->h_transaction != NULL);
2651 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2653 if (strcmp(name, dot) == 0) {
2654 if (dir->oo_compat_dot_created) {
2657 LASSERT(obj == dir);
2658 dir->oo_compat_dot_created = 1;
2661 } else if(strcmp(name, dotdot) == 0) {
2662 if (!dir->oo_compat_dot_created)
2664 if (dir->oo_compat_dotdot_created)
2665 return __osd_ea_add_rec(info, dir, obj, name, th);
2667 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2669 dir->oo_compat_dotdot_created = 1;
2677 * It will call the appropriate osd_add* function and return the
2678 * value, return by respective functions.
2680 static int osd_ea_add_rec(const struct lu_env *env,
2681 struct osd_object *pobj,
2682 struct osd_object *cobj,
2686 struct osd_thread_info *info = osd_oti_get(env);
2689 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2691 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2693 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2699 * Calls ->lookup() to find dentry. From dentry get inode and
2700 * read inode's ea to get fid. This is required for interoperability
2703 * \retval 0, on success
2704 * \retval -ve, on error
2706 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2707 struct dt_rec *rec, const struct dt_key *key)
2709 struct inode *dir = obj->oo_inode;
2710 struct osd_thread_info *info = osd_oti_get(env);
2711 struct dentry *dentry;
2712 struct ldiskfs_dir_entry_2 *de;
2713 struct buffer_head *bh;
2714 struct lu_fid *fid = &info->oti_fid;
2718 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2720 dentry = osd_child_dentry_get(env, obj,
2721 (char *)key, strlen((char *)key));
2723 down_read(&obj->oo_ext_idx_sem);
2724 bh = ldiskfs_find_entry(dentry, &de);
2726 ino = le32_to_cpu(de->inode);
2728 rc = osd_ea_fid_get(env, obj, ino, fid);
2730 osd_fid_pack(env, fid, rec);
2734 up_read(&obj->oo_ext_idx_sem);
2739 * Find the osd object for given fid.
2741 * \param fid, need to find the osd object having this fid
2743 * \retval osd_object, on success
2744 * \retval -ve, on error
2746 struct osd_object *osd_object_find(const struct lu_env *env,
2747 struct dt_object *dt,
2748 const struct lu_fid *fid)
2750 struct lu_device *ludev = dt->do_lu.lo_dev;
2751 struct osd_object *child = NULL;
2752 struct lu_object *luch;
2753 struct lu_object *lo;
2755 luch = lu_object_find(env, ludev, fid, NULL);
2756 if (!IS_ERR(luch)) {
2757 if (lu_object_exists(luch)) {
2758 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2760 child = osd_obj(lo);
2762 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2763 "lu_object can't be located"
2764 ""DFID"\n", PFID(fid));
2766 if (child == NULL) {
2767 lu_object_put(env, luch);
2768 CERROR("Unable to get osd_object\n");
2769 child = ERR_PTR(-ENOENT);
2772 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2773 "lu_object does not exists "DFID"\n",
2775 child = ERR_PTR(-ENOENT);
2778 child = (void *)luch;
2784 * Put the osd object once done with it.
2786 * \param obj, osd object that needs to be put
2788 static inline void osd_object_put(const struct lu_env *env,
2789 struct osd_object *obj)
2791 lu_object_put(env, &obj->oo_dt.do_lu);
2795 * Index add function for interoperability mode (b11826).
2796 * It will add the directory entry.This entry is needed to
2797 * maintain name->fid mapping.
2799 * \param key, it is key i.e. file entry to be inserted
2800 * \param rec, it is value of given key i.e. fid
2802 * \retval 0, on success
2803 * \retval -ve, on error
2805 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2806 const struct dt_rec *rec,
2807 const struct dt_key *key, struct thandle *th,
2808 struct lustre_capa *capa, int ignore_quota)
2810 struct osd_object *obj = osd_dt_obj(dt);
2811 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
2812 const struct lu_fid_pack *pack = (const struct lu_fid_pack *)rec;
2813 const char *name = (const char *)key;
2814 struct osd_object *child;
2815 #ifdef HAVE_QUOTA_SUPPORT
2816 cfs_cap_t save = current->cap_effective;
2822 LASSERT(osd_invariant(obj));
2823 LASSERT(dt_object_exists(dt));
2824 LASSERT(th != NULL);
2826 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2829 rc = fid_unpack(pack, fid);
2832 child = osd_object_find(env, dt, fid);
2833 if (!IS_ERR(child)) {
2834 struct inode *inode = obj->oo_inode;
2835 struct osd_thread_info *oti = osd_oti_get(env);
2836 struct timespec *ctime = &oti->oti_time;
2837 struct timespec *mtime = &oti->oti_time2;
2839 *ctime = inode->i_ctime;
2840 *mtime = inode->i_mtime;
2841 #ifdef HAVE_QUOTA_SUPPORT
2843 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2845 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2847 down_write(&obj->oo_ext_idx_sem);
2848 rc = osd_ea_add_rec(env, obj, child, name, th);
2849 up_write(&obj->oo_ext_idx_sem);
2850 #ifdef HAVE_QUOTA_SUPPORT
2851 current->cap_effective = save;
2853 osd_object_put(env, child);
2854 /* xtime should not be updated with server-side time. */
2855 spin_lock(&obj->oo_guard);
2856 inode->i_ctime = *ctime;
2857 inode->i_mtime = *mtime;
2858 spin_unlock(&obj->oo_guard);
2859 mark_inode_dirty(inode);
2861 rc = PTR_ERR(child);
2864 LASSERT(osd_invariant(obj));
2869 * Initialize osd Iterator for given osd index object.
2871 * \param dt osd index object
2874 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2875 struct dt_object *dt,
2876 struct lustre_capa *capa)
2878 struct osd_it_iam *it;
2879 struct osd_thread_info *oti = osd_oti_get(env);
2880 struct osd_object *obj = osd_dt_obj(dt);
2881 struct lu_object *lo = &dt->do_lu;
2882 struct iam_path_descr *ipd;
2883 struct iam_container *bag = &obj->oo_dir->od_container;
2885 LASSERT(lu_object_exists(lo));
2887 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2888 return ERR_PTR(-EACCES);
2891 ipd = osd_it_ipd_get(env, bag);
2892 if (likely(ipd != NULL)) {
2896 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2897 return (struct dt_it *)it;
2899 return ERR_PTR(-ENOMEM);
2903 * free given Iterator.
2906 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2908 struct osd_it_iam *it = (struct osd_it_iam *)di;
2909 struct osd_object *obj = it->oi_obj;
2911 iam_it_fini(&it->oi_it);
2912 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2913 lu_object_put(env, &obj->oo_dt.do_lu);
2917 * Move Iterator to record specified by \a key
2919 * \param di osd iterator
2920 * \param key key for index
2922 * \retval +ve di points to record with least key not larger than key
2923 * \retval 0 di points to exact matched key
2924 * \retval -ve failure
2927 static int osd_it_iam_get(const struct lu_env *env,
2928 struct dt_it *di, const struct dt_key *key)
2930 struct osd_it_iam *it = (struct osd_it_iam *)di;
2932 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2938 * \param di osd iterator
2941 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2943 struct osd_it_iam *it = (struct osd_it_iam *)di;
2945 iam_it_put(&it->oi_it);
2949 * Move iterator by one record
2951 * \param di osd iterator
2953 * \retval +1 end of container reached
2955 * \retval -ve failure
2958 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2960 struct osd_it_iam *it = (struct osd_it_iam *)di;
2962 return iam_it_next(&it->oi_it);
2966 * Return pointer to the key under iterator.
2969 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2970 const struct dt_it *di)
2972 struct osd_it_iam *it = (struct osd_it_iam *)di;
2974 return (struct dt_key *)iam_it_key_get(&it->oi_it);
2978 * Return size of key under iterator (in bytes)
2981 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
2983 struct osd_it_iam *it = (struct osd_it_iam *)di;
2985 return iam_it_key_size(&it->oi_it);
2988 static inline void osd_it_append_attrs(struct lu_dirent*ent,
2993 struct luda_type *lt;
2994 const unsigned align = sizeof(struct luda_type) - 1;
2996 /* check if file type is required */
2997 if (attr & LUDA_TYPE) {
2998 len = (len + align) & ~align;
3000 lt = (void *) ent->lde_name + len;
3001 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3002 ent->lde_attrs |= LUDA_TYPE;
3005 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3009 * build lu direct from backend fs dirent.
3012 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3020 fid_cpu_to_le(&ent->lde_fid, fid);
3021 ent->lde_attrs = LUDA_FID;
3023 ent->lde_hash = cpu_to_le64(offset);
3024 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3026 strncpy(ent->lde_name, name, namelen);
3027 ent->lde_namelen = cpu_to_le16(namelen);
3029 /* append lustre attributes */
3030 osd_it_append_attrs(ent, attr, namelen, type);
3034 * Return pointer to the record under iterator.
3036 static int osd_it_iam_rec(const struct lu_env *env,
3037 const struct dt_it *di,
3038 struct lu_dirent *lde,
3041 struct osd_it_iam *it = (struct osd_it_iam *)di;
3042 struct osd_thread_info *info = osd_oti_get(env);
3043 struct lu_fid *fid = &info->oti_fid;
3044 const struct lu_fid_pack *rec;
3050 name = (char *)iam_it_key_get(&it->oi_it);
3052 RETURN(PTR_ERR(name));
3054 namelen = iam_it_key_size(&it->oi_it);
3056 rec = (const struct lu_fid_pack *) iam_it_rec_get(&it->oi_it);
3058 RETURN(PTR_ERR(rec));
3060 rc = fid_unpack(rec, fid);
3064 hash = iam_it_store(&it->oi_it);
3066 /* IAM does not store object type in IAM index (dir) */
3067 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3074 * Returns cookie for current Iterator position.
3076 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3078 struct osd_it_iam *it = (struct osd_it_iam *)di;
3080 return iam_it_store(&it->oi_it);
3084 * Restore iterator from cookie.
3086 * \param di osd iterator
3087 * \param hash Iterator location cookie
3089 * \retval +ve di points to record with least key not larger than key.
3090 * \retval 0 di points to exact matched key
3091 * \retval -ve failure
3094 static int osd_it_iam_load(const struct lu_env *env,
3095 const struct dt_it *di, __u64 hash)
3097 struct osd_it_iam *it = (struct osd_it_iam *)di;
3099 return iam_it_load(&it->oi_it, hash);
3102 static const struct dt_index_operations osd_index_iam_ops = {
3103 .dio_lookup = osd_index_iam_lookup,
3104 .dio_insert = osd_index_iam_insert,
3105 .dio_delete = osd_index_iam_delete,
3107 .init = osd_it_iam_init,
3108 .fini = osd_it_iam_fini,
3109 .get = osd_it_iam_get,
3110 .put = osd_it_iam_put,
3111 .next = osd_it_iam_next,
3112 .key = osd_it_iam_key,
3113 .key_size = osd_it_iam_key_size,
3114 .rec = osd_it_iam_rec,
3115 .store = osd_it_iam_store,
3116 .load = osd_it_iam_load
3121 * Creates or initializes iterator context.
3123 * \retval struct osd_it_ea, iterator structure on success
3126 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3127 struct dt_object *dt,
3128 struct lustre_capa *capa)
3130 struct osd_object *obj = osd_dt_obj(dt);
3131 struct osd_thread_info *info = osd_oti_get(env);
3132 struct osd_it_ea *it = &info->oti_it_ea;
3133 struct lu_object *lo = &dt->do_lu;
3134 struct dentry *obj_dentry = &info->oti_it_dentry;
3136 LASSERT(lu_object_exists(lo));
3138 obj_dentry->d_inode = obj->oo_inode;
3139 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3140 obj_dentry->d_name.hash = 0;
3142 it->oie_rd_dirent = 0;
3143 it->oie_it_dirent = 0;
3144 it->oie_dirent = NULL;
3145 it->oie_buf = info->oti_it_ea_buf;
3147 it->oie_file.f_pos = 0;
3148 it->oie_file.f_dentry = obj_dentry;
3149 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3150 it->oie_file.f_op = obj->oo_inode->i_fop;
3151 it->oie_file.private_data = NULL;
3153 RETURN((struct dt_it*) it);
3157 * Destroy or finishes iterator context.
3159 * \param di, struct osd_it_ea, iterator structure to be destroyed
3161 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3163 struct osd_it_ea *it = (struct osd_it_ea *)di;
3164 struct osd_object *obj = it->oie_obj;
3165 struct inode *inode = obj->oo_inode;
3168 it->oie_file.f_op->release(inode, &it->oie_file);
3169 lu_object_put(env, &obj->oo_dt.do_lu);
3174 * It position the iterator at given key, so that next lookup continues from
3175 * that key Or it is similar to dio_it->load() but based on a key,
3176 * rather than file position.
3178 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3181 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3183 static int osd_it_ea_get(const struct lu_env *env,
3184 struct dt_it *di, const struct dt_key *key)
3186 struct osd_it_ea *it = (struct osd_it_ea *)di;
3189 LASSERT(((const char *)key)[0] == '\0');
3190 it->oie_file.f_pos = 0;
3191 it->oie_rd_dirent = 0;
3192 it->oie_it_dirent = 0;
3193 it->oie_dirent = NULL;
3201 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3206 * It is called internally by ->readdir(). It fills the
3207 * iterator's in-memory data structure with required
3208 * information i.e. name, namelen, rec_size etc.
3210 * \param buf, in which information to be filled in.
3211 * \param name, name of the file in given dir
3213 * \retval 0, on success
3214 * \retval 1, on buffer full
3216 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3217 loff_t offset, __u64 ino,
3220 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3221 struct osd_it_ea_dirent *ent = it->oie_dirent;
3224 /* this should never happen */
3225 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3226 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3230 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3234 ent->oied_ino = ino;
3235 ent->oied_off = offset;
3236 ent->oied_namelen = namelen;
3237 ent->oied_type = d_type;
3239 memcpy(ent->oied_name, name, namelen);
3241 it->oie_rd_dirent++;
3242 it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3247 * Calls ->readdir() to load a directory entry at a time
3248 * and stored it in iterator's in-memory data structure.
3250 * \param di, struct osd_it_ea, iterator's in memory structure
3252 * \retval 0, on success
3253 * \retval -ve, on error
3255 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3257 struct osd_it_ea *it = (struct osd_it_ea *)di;
3258 struct osd_object *obj = it->oie_obj;
3259 struct inode *inode = obj->oo_inode;
3263 it->oie_dirent = it->oie_buf;
3264 it->oie_rd_dirent = 0;
3266 down_read(&obj->oo_ext_idx_sem);
3267 result = inode->i_fop->readdir(&it->oie_file, it,
3268 (filldir_t) osd_ldiskfs_filldir);
3270 up_read(&obj->oo_ext_idx_sem);
3272 if (it->oie_rd_dirent == 0) {
3275 it->oie_dirent = it->oie_buf;
3276 it->oie_it_dirent = 1;
3283 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3284 * to load a directory entry at a time and stored it in
3285 * iterator's in-memory data structure.
3287 * \param di, struct osd_it_ea, iterator's in memory structure
3289 * \retval +ve, iterator reached to end
3290 * \retval 0, iterator not reached to end
3291 * \retval -ve, on error
3293 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3295 struct osd_it_ea *it = (struct osd_it_ea *)di;
3300 if (it->oie_it_dirent < it->oie_rd_dirent) {
3301 it->oie_dirent = (void *) it->oie_dirent +
3302 size_round(sizeof(struct osd_it_ea_dirent) +
3303 it->oie_dirent->oied_namelen);
3304 it->oie_it_dirent++;
3307 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3310 rc = osd_ldiskfs_it_fill(di);
3317 * Returns the key at current position from iterator's in memory structure.
3319 * \param di, struct osd_it_ea, iterator's in memory structure
3321 * \retval key i.e. struct dt_key on success
3323 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3324 const struct dt_it *di)
3326 struct osd_it_ea *it = (struct osd_it_ea *)di;
3328 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3332 * Returns the key's size at current position from iterator's in memory structure.
3334 * \param di, struct osd_it_ea, iterator's in memory structure
3336 * \retval key_size i.e. struct dt_key on success
3338 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3340 struct osd_it_ea *it = (struct osd_it_ea *)di;
3342 RETURN(it->oie_dirent->oied_namelen);
3347 * Returns the value (i.e. fid/igif) at current position from iterator's
3348 * in memory structure.
3350 * \param di, struct osd_it_ea, iterator's in memory structure
3351 * \param attr, attr requested for dirent.
3352 * \param lde, lustre dirent
3354 * \retval 0, no error and \param lde has correct lustre dirent.
3355 * \retval -ve, on error
3357 static inline int osd_it_ea_rec(const struct lu_env *env,
3358 const struct dt_it *di,
3359 struct lu_dirent *lde,
3362 struct osd_it_ea *it = (struct osd_it_ea *)di;
3363 struct osd_object *obj = it->oie_obj;
3364 struct osd_thread_info *info = osd_oti_get(env);
3365 struct lu_fid *fid = &info->oti_fid;
3370 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3373 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3374 it->oie_dirent->oied_name,
3375 it->oie_dirent->oied_namelen,
3376 it->oie_dirent->oied_type,
3382 * Returns a cookie for current position of the iterator head, so that
3383 * user can use this cookie to load/start the iterator next time.
3385 * \param di, struct osd_it_ea, iterator's in memory structure
3387 * \retval cookie for current position, on success
3389 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3391 struct osd_it_ea *it = (struct osd_it_ea *)di;
3393 RETURN(it->oie_dirent->oied_off);
3397 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3398 * to load a directory entry at a time and stored it i inn,
3399 * in iterator's in-memory data structure.
3401 * \param di, struct osd_it_ea, iterator's in memory structure
3403 * \retval +ve, on success
3404 * \retval -ve, on error
3406 static int osd_it_ea_load(const struct lu_env *env,
3407 const struct dt_it *di, __u64 hash)
3409 struct osd_it_ea *it = (struct osd_it_ea *)di;
3413 it->oie_file.f_pos = hash;
3415 rc = osd_ldiskfs_it_fill(di);
3423 * Index lookup function for interoperability mode (b11826).
3425 * \param key, key i.e. file name to be searched
3427 * \retval +ve, on success
3428 * \retval -ve, on error
3430 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3431 struct dt_rec *rec, const struct dt_key *key,
3432 struct lustre_capa *capa)
3434 struct osd_object *obj = osd_dt_obj(dt);
3439 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3440 LINVRNT(osd_invariant(obj));
3442 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3445 rc = osd_ea_lookup_rec(env, obj, rec, key);
3453 * Index and Iterator operations for interoperability
3454 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3456 static const struct dt_index_operations osd_index_ea_ops = {
3457 .dio_lookup = osd_index_ea_lookup,
3458 .dio_insert = osd_index_ea_insert,
3459 .dio_delete = osd_index_ea_delete,
3461 .init = osd_it_ea_init,
3462 .fini = osd_it_ea_fini,
3463 .get = osd_it_ea_get,
3464 .put = osd_it_ea_put,
3465 .next = osd_it_ea_next,
3466 .key = osd_it_ea_key,
3467 .key_size = osd_it_ea_key_size,
3468 .rec = osd_it_ea_rec,
3469 .store = osd_it_ea_store,
3470 .load = osd_it_ea_load
3474 static void *osd_key_init(const struct lu_context *ctx,
3475 struct lu_context_key *key)
3477 struct osd_thread_info *info;
3479 OBD_ALLOC_PTR(info);
3481 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3482 if (info->oti_it_ea_buf != NULL) {
3483 info->oti_env = container_of(ctx, struct lu_env,
3487 info = ERR_PTR(-ENOMEM);
3490 info = ERR_PTR(-ENOMEM);
3495 static void osd_key_fini(const struct lu_context *ctx,
3496 struct lu_context_key *key, void* data)
3498 struct osd_thread_info *info = data;
3500 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3504 static void osd_key_exit(const struct lu_context *ctx,
3505 struct lu_context_key *key, void *data)
3507 struct osd_thread_info *info = data;
3509 LASSERT(info->oti_r_locks == 0);
3510 LASSERT(info->oti_w_locks == 0);
3511 LASSERT(info->oti_txns == 0);
3514 /* type constructor/destructor: osd_type_init, osd_type_fini */
3515 LU_TYPE_INIT_FINI(osd, &osd_key);
3517 static struct lu_context_key osd_key = {
3518 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3519 .lct_init = osd_key_init,
3520 .lct_fini = osd_key_fini,
3521 .lct_exit = osd_key_exit
3525 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3526 const char *name, struct lu_device *next)
3529 struct lu_context *ctx;
3531 /* context for commit hooks */
3532 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3533 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3535 rc = osd_procfs_init(osd_dev(d), name);
3536 ctx->lc_cookie = 0x3;
3541 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3543 struct osd_thread_info *info = osd_oti_get(env);
3545 if (o->od_obj_area != NULL) {
3546 lu_object_put(env, &o->od_obj_area->do_lu);
3547 o->od_obj_area = NULL;
3549 osd_oi_fini(info, &o->od_oi);
3554 static int osd_mount(const struct lu_env *env,
3555 struct osd_device *o, struct lustre_cfg *cfg)
3557 struct lustre_mount_info *lmi;
3558 const char *dev = lustre_cfg_string(cfg, 0);
3559 struct lustre_disk_data *ldd;
3560 struct lustre_sb_info *lsi;
3564 if (o->od_mount != NULL) {
3565 CERROR("Already mounted (%s)\n", dev);
3570 lmi = server_get_mount(dev);
3572 CERROR("Cannot get mount info for %s!\n", dev);
3576 LASSERT(lmi != NULL);
3577 /* save lustre_mount_info in dt_device */
3580 lsi = s2lsi(lmi->lmi_sb);
3583 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3585 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3589 o->od_obj_area = NULL;
3593 static struct lu_device *osd_device_fini(const struct lu_env *env,
3594 struct lu_device *d)
3599 shrink_dcache_sb(osd_sb(osd_dev(d)));
3600 osd_sync(env, lu2dt_dev(d));
3602 rc = osd_procfs_fini(osd_dev(d));
3604 CERROR("proc fini error %d \n", rc);
3605 RETURN (ERR_PTR(rc));
3608 if (osd_dev(d)->od_mount)
3609 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3610 osd_dev(d)->od_mount->lmi_mnt);
3611 osd_dev(d)->od_mount = NULL;
3613 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3617 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3618 struct lu_device_type *t,
3619 struct lustre_cfg *cfg)
3621 struct lu_device *l;
3622 struct osd_device *o;
3628 result = dt_device_init(&o->od_dt_dev, t);
3631 l->ld_ops = &osd_lu_ops;
3632 o->od_dt_dev.dd_ops = &osd_dt_ops;
3633 spin_lock_init(&o->od_osfs_lock);
3634 o->od_osfs_age = cfs_time_shift_64(-1000);
3635 o->od_capa_hash = init_capa_hash();
3636 if (o->od_capa_hash == NULL) {
3637 dt_device_fini(&o->od_dt_dev);
3638 l = ERR_PTR(-ENOMEM);
3641 l = ERR_PTR(result);
3646 l = ERR_PTR(-ENOMEM);
3650 static struct lu_device *osd_device_free(const struct lu_env *env,
3651 struct lu_device *d)
3653 struct osd_device *o = osd_dev(d);
3656 cleanup_capa_hash(o->od_capa_hash);
3657 dt_device_fini(&o->od_dt_dev);
3662 static int osd_process_config(const struct lu_env *env,
3663 struct lu_device *d, struct lustre_cfg *cfg)
3665 struct osd_device *o = osd_dev(d);
3669 switch(cfg->lcfg_command) {
3671 err = osd_mount(env, o, cfg);
3674 err = osd_shutdown(env, o);
3683 static int osd_recovery_complete(const struct lu_env *env,
3684 struct lu_device *d)
3686 struct osd_device *o = osd_dev(d);
3688 /* TODO: orphans handling */
3689 ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3693 static int osd_prepare(const struct lu_env *env,
3694 struct lu_device *pdev,
3695 struct lu_device *dev)
3697 struct osd_device *osd = osd_dev(dev);
3698 struct lustre_sb_info *lsi;
3699 struct lustre_disk_data *ldd;
3700 struct lustre_mount_info *lmi;
3701 struct osd_thread_info *oti = osd_oti_get(env);
3702 struct dt_object *d;
3706 /* 1. initialize oi before any file create or file open */
3707 result = osd_oi_init(oti, &osd->od_oi,
3708 &osd->od_dt_dev, lu2md_dev(pdev));
3712 lmi = osd->od_mount;
3713 lsi = s2lsi(lmi->lmi_sb);
3716 /* 2. setup local objects */
3717 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3721 /* 3. open remote object dir */
3722 d = dt_store_open(env, lu2dt_dev(dev), "",
3723 remote_obj_dir, &oti->oti_fid);
3725 osd->od_obj_area = d;
3728 result = PTR_ERR(d);
3729 osd->od_obj_area = NULL;
3736 static const struct lu_object_operations osd_lu_obj_ops = {
3737 .loo_object_init = osd_object_init,
3738 .loo_object_delete = osd_object_delete,
3739 .loo_object_release = osd_object_release,
3740 .loo_object_free = osd_object_free,
3741 .loo_object_print = osd_object_print,
3742 .loo_object_invariant = osd_object_invariant
3745 static const struct lu_device_operations osd_lu_ops = {
3746 .ldo_object_alloc = osd_object_alloc,
3747 .ldo_process_config = osd_process_config,
3748 .ldo_recovery_complete = osd_recovery_complete,
3749 .ldo_prepare = osd_prepare,
3752 static const struct lu_device_type_operations osd_device_type_ops = {
3753 .ldto_init = osd_type_init,
3754 .ldto_fini = osd_type_fini,
3756 .ldto_start = osd_type_start,
3757 .ldto_stop = osd_type_stop,
3759 .ldto_device_alloc = osd_device_alloc,
3760 .ldto_device_free = osd_device_free,
3762 .ldto_device_init = osd_device_init,
3763 .ldto_device_fini = osd_device_fini
3766 static struct lu_device_type osd_device_type = {
3767 .ldt_tags = LU_DEVICE_DT,
3768 .ldt_name = LUSTRE_OSD_NAME,
3769 .ldt_ops = &osd_device_type_ops,
3770 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3774 * lprocfs legacy support.
3776 static struct obd_ops osd_obd_device_ops = {
3777 .o_owner = THIS_MODULE
3780 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3781 .llod_name = remote_obj_dir,
3782 .llod_oid = OSD_REM_OBJ_DIR_OID,
3784 .llod_feat = &dt_directory_features,
3787 static int __init osd_mod_init(void)
3789 struct lprocfs_static_vars lvars;
3792 llo_local_obj_register(&llod_osd_rem_obj_dir);
3793 lprocfs_osd_init_vars(&lvars);
3794 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3795 LUSTRE_OSD_NAME, &osd_device_type);
3798 static void __exit osd_mod_exit(void)
3800 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3801 class_unregister_type(LUSTRE_OSD_NAME);
3804 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3805 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3806 MODULE_LICENSE("GPL");
3808 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);