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>
62 * struct OBD_{ALLOC,FREE}*()
65 #include <obd_support.h>
66 /* struct ptlrpc_thread */
67 #include <lustre_net.h>
70 #include <lustre_fid.h>
72 #include "osd_internal.h"
75 /* llo_* api support */
76 #include <md_object.h>
78 static const char dot[] = ".";
79 static const char dotdot[] = "..";
80 static const char remote_obj_dir[] = "REM_OBJ_DIR";
82 struct osd_directory {
83 struct iam_container od_container;
84 struct iam_descr od_descr;
88 struct dt_object oo_dt;
90 * Inode for file system object represented by this osd_object. This
91 * inode is pinned for the whole duration of lu_object life.
93 * Not modified concurrently (either setup early during object
94 * creation, or assigned by osd_object_create() under write lock).
96 struct inode *oo_inode;
98 * to protect index ops.
100 struct rw_semaphore oo_ext_idx_sem;
101 struct rw_semaphore oo_sem;
102 struct osd_directory *oo_dir;
103 /** protects inode attributes. */
106 * Following two members are used to indicate the presence of dot and
107 * dotdot in the given directory. This is required for interop mode
110 int oo_compat_dot_created;
111 int oo_compat_dotdot_created;
113 const struct lu_env *oo_owner;
114 #ifdef CONFIG_LOCKDEP
115 struct lockdep_map oo_dep_map;
119 static const struct lu_object_operations osd_lu_obj_ops;
120 static const struct lu_device_operations osd_lu_ops;
121 static struct lu_context_key osd_key;
122 static const struct dt_object_operations osd_obj_ops;
123 static const struct dt_object_operations osd_obj_ea_ops;
124 static const struct dt_body_operations osd_body_ops;
125 static const struct dt_index_operations osd_index_iam_ops;
126 static const struct dt_index_operations osd_index_ea_ops;
129 struct thandle ot_super;
131 struct journal_callback ot_jcb;
132 /* Link to the device, for debugging. */
133 struct lu_ref_link *ot_dev_link;
140 static int lu_device_is_osd(const struct lu_device *d)
142 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
145 static struct osd_device *osd_dt_dev(const struct dt_device *d)
147 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
148 return container_of0(d, struct osd_device, od_dt_dev);
151 static struct osd_device *osd_dev(const struct lu_device *d)
153 LASSERT(lu_device_is_osd(d));
154 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
157 static struct osd_device *osd_obj2dev(const struct osd_object *o)
159 return osd_dev(o->oo_dt.do_lu.lo_dev);
162 static struct super_block *osd_sb(const struct osd_device *dev)
164 return dev->od_mount->lmi_mnt->mnt_sb;
167 static int osd_object_is_root(const struct osd_object *obj)
169 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
172 static struct osd_object *osd_obj(const struct lu_object *o)
174 LASSERT(lu_device_is_osd(o->lo_dev));
175 return container_of0(o, struct osd_object, oo_dt.do_lu);
178 static struct osd_object *osd_dt_obj(const struct dt_object *d)
180 return osd_obj(&d->do_lu);
183 static struct lu_device *osd2lu_dev(struct osd_device *osd)
185 return &osd->od_dt_dev.dd_lu_dev;
188 static journal_t *osd_journal(const struct osd_device *dev)
190 return LDISKFS_SB(osd_sb(dev))->s_journal;
193 static int osd_has_index(const struct osd_object *obj)
195 return obj->oo_dt.do_index_ops != NULL;
198 static int osd_object_invariant(const struct lu_object *l)
200 return osd_invariant(osd_obj(l));
203 #ifdef HAVE_QUOTA_SUPPORT
205 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
207 struct md_ucred *uc = md_ucred(env);
211 save->oc_uid = current->fsuid;
212 save->oc_gid = current->fsgid;
213 save->oc_cap = current->cap_effective;
214 current->fsuid = uc->mu_fsuid;
215 current->fsgid = uc->mu_fsgid;
216 current->cap_effective = uc->mu_cap;
220 osd_pop_ctxt(struct osd_ctxt *save)
222 current->fsuid = save->oc_uid;
223 current->fsgid = save->oc_gid;
224 current->cap_effective = save->oc_cap;
228 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
230 return lu_context_key_get(&env->le_ctx, &osd_key);
234 * Concurrency: doesn't matter
236 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
238 return osd_oti_get(env)->oti_r_locks > 0;
242 * Concurrency: doesn't matter
244 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
246 struct osd_thread_info *oti = osd_oti_get(env);
247 return oti->oti_w_locks > 0 && o->oo_owner == env;
251 * Concurrency: doesn't access mutable data
253 static int osd_root_get(const struct lu_env *env,
254 struct dt_device *dev, struct lu_fid *f)
258 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
259 lu_igif_build(f, inode->i_ino, inode->i_generation);
264 * OSD object methods.
268 * Concurrency: no concurrent access is possible that early in object
271 static struct lu_object *osd_object_alloc(const struct lu_env *env,
272 const struct lu_object_header *hdr,
275 struct osd_object *mo;
281 l = &mo->oo_dt.do_lu;
282 dt_object_init(&mo->oo_dt, NULL, d);
283 if (osd_dev(d)->od_iop_mode)
284 mo->oo_dt.do_ops = &osd_obj_ea_ops;
286 mo->oo_dt.do_ops = &osd_obj_ops;
288 l->lo_ops = &osd_lu_obj_ops;
289 init_rwsem(&mo->oo_sem);
290 init_rwsem(&mo->oo_ext_idx_sem);
291 spin_lock_init(&mo->oo_guard);
298 * retrieve object from backend ext fs.
300 static struct inode *osd_iget(struct osd_thread_info *info,
301 struct osd_device *dev,
302 const struct osd_inode_id *id)
304 struct inode *inode = NULL;
306 #ifdef HAVE_EXT4_LDISKFS
307 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
309 /* Newer kernels return an error instead of a NULL pointer */
312 inode = iget(osd_sb(dev), id->oii_ino);
315 CERROR("no inode\n");
316 inode = ERR_PTR(-EACCES);
317 } else if (id->oii_gen != OSD_OII_NOGEN &&
318 inode->i_generation != id->oii_gen) {
320 inode = ERR_PTR(-ESTALE);
321 } else if (inode->i_nlink == 0) {
322 /* due to parallel readdir and unlink,
323 * we can have dead inode here. */
324 CWARN("stale inode\n");
325 make_bad_inode(inode);
327 inode = ERR_PTR(-ESTALE);
328 } else if (is_bad_inode(inode)) {
329 CERROR("bad inode %lx\n",inode->i_ino);
331 inode = ERR_PTR(-ENOENT);
336 static int osd_fid_lookup(const struct lu_env *env,
337 struct osd_object *obj, const struct lu_fid *fid)
339 struct osd_thread_info *info;
340 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
341 struct osd_device *dev;
342 struct osd_inode_id *id;
347 LINVRNT(osd_invariant(obj));
348 LASSERT(obj->oo_inode == NULL);
349 LASSERT(fid_is_sane(fid));
351 * This assertion checks that osd layer sees only local
352 * fids. Unfortunately it is somewhat expensive (does a
353 * cache-lookup). Disabling it for production/acceptance-testing.
355 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
359 info = osd_oti_get(env);
364 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
367 result = osd_oi_lookup(info, oi, fid, id);
369 inode = osd_iget(info, dev, id);
370 if (!IS_ERR(inode)) {
371 obj->oo_inode = inode;
372 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
373 if (dev->od_iop_mode) {
374 obj->oo_compat_dot_created = 1;
375 obj->oo_compat_dotdot_created = 1;
380 * If fid wasn't found in oi, inode-less object is
381 * created, for which lu_object_exists() returns
382 * false. This is used in a (frequent) case when
383 * objects are created as locking anchors or
384 * place holders for objects yet to be created.
386 result = PTR_ERR(inode);
387 } else if (result == -ENOENT)
389 LINVRNT(osd_invariant(obj));
395 * Concurrency: shouldn't matter.
397 static void osd_object_init0(struct osd_object *obj)
399 LASSERT(obj->oo_inode != NULL);
400 obj->oo_dt.do_body_ops = &osd_body_ops;
401 obj->oo_dt.do_lu.lo_header->loh_attr |=
402 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
406 * Concurrency: no concurrent access is possible that early in object
409 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
410 const struct lu_object_conf *unused)
412 struct osd_object *obj = osd_obj(l);
415 LINVRNT(osd_invariant(obj));
417 result = osd_fid_lookup(env, obj, lu_object_fid(l));
419 if (obj->oo_inode != NULL)
420 osd_object_init0(obj);
422 LINVRNT(osd_invariant(obj));
427 * Concurrency: no concurrent access is possible that late in object
430 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
432 struct osd_object *obj = osd_obj(l);
434 LINVRNT(osd_invariant(obj));
436 dt_object_fini(&obj->oo_dt);
443 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
444 const struct iam_container *bag)
446 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
447 osd_oti_get(env)->oti_it_ipd);
450 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
451 const struct iam_container *bag)
453 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
454 osd_oti_get(env)->oti_idx_ipd);
457 static void osd_ipd_put(const struct lu_env *env,
458 const struct iam_container *bag,
459 struct iam_path_descr *ipd)
461 bag->ic_descr->id_ops->id_ipd_free(ipd);
465 * Concurrency: no concurrent access is possible that late in object
468 static void osd_index_fini(struct osd_object *o)
470 struct iam_container *bag;
472 if (o->oo_dir != NULL) {
473 bag = &o->oo_dir->od_container;
474 if (o->oo_inode != NULL) {
475 if (bag->ic_object == o->oo_inode)
476 iam_container_fini(bag);
478 OBD_FREE_PTR(o->oo_dir);
484 * Concurrency: no concurrent access is possible that late in object
485 * life-cycle (for all existing callers, that is. New callers have to provide
486 * their own locking.)
488 static int osd_inode_unlinked(const struct inode *inode)
490 return inode->i_nlink == 0;
494 OSD_TXN_OI_DELETE_CREDITS = 20,
495 OSD_TXN_INODE_DELETE_CREDITS = 20
503 * Concurrency: doesn't access mutable data.
505 static int osd_param_is_sane(const struct osd_device *dev,
506 const struct txn_param *param)
508 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
512 * Concurrency: shouldn't matter.
514 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
516 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
517 struct thandle *th = &oh->ot_super;
518 struct dt_device *dev = th->th_dev;
519 struct lu_device *lud = &dev->dd_lu_dev;
521 LASSERT(dev != NULL);
522 LASSERT(oh->ot_handle == NULL);
525 CERROR("transaction @0x%p commit error: %d\n", th, error);
527 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
529 * This od_env_for_commit is only for commit usage. see
532 lu_context_enter(&env->le_ctx);
533 dt_txn_hook_commit(env, th);
534 lu_context_exit(&env->le_ctx);
537 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
541 lu_context_exit(&th->th_ctx);
542 lu_context_fini(&th->th_ctx);
547 * Concurrency: shouldn't matter.
549 static struct thandle *osd_trans_start(const struct lu_env *env,
553 struct osd_device *dev = osd_dt_dev(d);
555 struct osd_thandle *oh;
561 hook_res = dt_txn_hook_start(env, d, p);
563 RETURN(ERR_PTR(hook_res));
565 if (osd_param_is_sane(dev, p)) {
566 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
568 struct osd_thread_info *oti = osd_oti_get(env);
571 * XXX temporary stuff. Some abstraction layer should
575 jh = ldiskfs_journal_start_sb(osd_sb(dev), p->tp_credits);
581 jh->h_sync = p->tp_sync;
582 lu_device_get(&d->dd_lu_dev);
583 oh->ot_dev_link = lu_ref_add
584 (&d->dd_lu_dev.ld_reference,
586 /* add commit callback */
587 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
588 lu_context_enter(&th->th_ctx);
589 osd_journal_callback_set(jh, osd_trans_commit_cb,
590 (struct journal_callback *)&oh->ot_jcb);
591 LASSERT(oti->oti_txns == 0);
592 LASSERT(oti->oti_r_locks == 0);
593 LASSERT(oti->oti_w_locks == 0);
600 th = ERR_PTR(-ENOMEM);
602 CERROR("Invalid transaction parameters\n");
603 th = ERR_PTR(-EINVAL);
610 * Concurrency: shouldn't matter.
612 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
615 struct osd_thandle *oh;
616 struct osd_thread_info *oti = osd_oti_get(env);
620 oh = container_of0(th, struct osd_thandle, ot_super);
621 if (oh->ot_handle != NULL) {
622 handle_t *hdl = oh->ot_handle;
624 LASSERT(oti->oti_txns == 1);
626 LASSERT(oti->oti_r_locks == 0);
627 LASSERT(oti->oti_w_locks == 0);
628 result = dt_txn_hook_stop(env, th);
630 CERROR("Failure in transaction hook: %d\n", result);
631 oh->ot_handle = NULL;
632 result = ldiskfs_journal_stop(hdl);
634 CERROR("Failure to stop transaction: %d\n", result);
640 * Concurrency: no concurrent access is possible that late in object
643 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
645 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
646 struct osd_device *osd = osd_obj2dev(obj);
647 struct osd_thread_info *oti = osd_oti_get(env);
648 struct txn_param *prm = &oti->oti_txn;
649 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
653 lu_env_init(env_del_obj, LCT_DT_THREAD);
654 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
655 OSD_TXN_INODE_DELETE_CREDITS);
656 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
658 result = osd_oi_delete(osd_oti_get(env_del_obj),
659 &osd->od_oi, fid, th);
660 osd_trans_stop(env_del_obj, th);
662 result = PTR_ERR(th);
664 lu_env_fini(env_del_obj);
669 * Called just before object is freed. Releases all resources except for
670 * object itself (that is released by osd_object_free()).
672 * Concurrency: no concurrent access is possible that late in object
675 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
677 struct osd_object *obj = osd_obj(l);
678 struct inode *inode = obj->oo_inode;
680 LINVRNT(osd_invariant(obj));
683 * If object is unlinked remove fid->ino mapping from object index.
690 if (osd_inode_unlinked(inode)) {
691 result = osd_inode_remove(env, obj);
693 LU_OBJECT_DEBUG(D_ERROR, env, l,
694 "Failed to cleanup: %d\n",
699 obj->oo_inode = NULL;
704 * Concurrency: ->loo_object_release() is called under site spin-lock.
706 static void osd_object_release(const struct lu_env *env,
709 struct osd_object *o = osd_obj(l);
711 LASSERT(!lu_object_is_dying(l->lo_header));
712 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
713 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
717 * Concurrency: shouldn't matter.
719 static int osd_object_print(const struct lu_env *env, void *cookie,
720 lu_printer_t p, const struct lu_object *l)
722 struct osd_object *o = osd_obj(l);
725 if (o->oo_dir != NULL)
726 d = o->oo_dir->od_container.ic_descr;
729 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
731 o->oo_inode ? o->oo_inode->i_ino : 0UL,
732 o->oo_inode ? o->oo_inode->i_generation : 0,
733 d ? d->id_ops->id_name : "plain");
737 * Concurrency: shouldn't matter.
739 int osd_statfs(const struct lu_env *env, struct dt_device *d,
742 struct osd_device *osd = osd_dt_dev(d);
743 struct super_block *sb = osd_sb(osd);
746 spin_lock(&osd->od_osfs_lock);
748 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
749 result = ll_do_statfs(sb, &osd->od_kstatfs);
750 if (likely(result == 0)) /* N.B. statfs can't really fail */
751 osd->od_osfs_age = cfs_time_current_64();
754 if (likely(result == 0))
755 *sfs = osd->od_kstatfs;
756 spin_unlock(&osd->od_osfs_lock);
762 * Concurrency: doesn't access mutable data.
764 static void osd_conf_get(const struct lu_env *env,
765 const struct dt_device *dev,
766 struct dt_device_param *param)
769 * XXX should be taken from not-yet-existing fs abstraction layer.
771 param->ddp_max_name_len = LDISKFS_NAME_LEN;
772 param->ddp_max_nlink = LDISKFS_LINK_MAX;
773 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
777 * Helper function to get and fill the buffer with input values.
779 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
783 buf = &osd_oti_get(env)->oti_buf;
790 * Concurrency: shouldn't matter.
792 static int osd_sync(const struct lu_env *env, struct dt_device *d)
794 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
795 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
799 * Start commit for OSD device.
801 * An implementation of dt_commit_async method for OSD device.
802 * Asychronously starts underlayng fs sync and thereby a transaction
805 * \param env environment
808 * \see dt_device_operations
810 static int osd_commit_async(const struct lu_env *env,
813 struct super_block *s = osd_sb(osd_dt_dev(d));
816 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
817 RETURN(s->s_op->sync_fs(s, 0));
821 * Concurrency: shouldn't matter.
823 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
825 static void osd_ro(const struct lu_env *env, struct dt_device *d)
829 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
831 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
832 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
838 * Concurrency: serialization provided by callers.
840 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
841 int mode, unsigned long timeout, __u32 alg,
842 struct lustre_capa_key *keys)
844 struct osd_device *dev = osd_dt_dev(d);
847 dev->od_fl_capa = mode;
848 dev->od_capa_timeout = timeout;
849 dev->od_capa_alg = alg;
850 dev->od_capa_keys = keys;
855 * Concurrency: serialization provided by callers.
857 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
858 struct dt_quota_ctxt *ctxt, void *data)
860 struct obd_device *obd = (void *)ctxt;
861 struct vfsmount *mnt = (struct vfsmount *)data;
864 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
865 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
866 obd->obd_lvfs_ctxt.pwdmnt = mnt;
867 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
868 obd->obd_lvfs_ctxt.fs = get_ds();
874 * Note: we do not count into QUOTA here.
875 * If we mount with --data_journal we may need more.
877 static const int osd_dto_credits_noquota[DTO_NR] = {
880 * INDEX_EXTRA_TRANS_BLOCKS(8) +
881 * SINGLEDATA_TRANS_BLOCKS(8)
882 * XXX Note: maybe iam need more, since iam have more level than
885 [DTO_INDEX_INSERT] = 16,
886 [DTO_INDEX_DELETE] = 16,
890 [DTO_IDNEX_UPDATE] = 16,
892 * Create a object. The same as create object in EXT3.
893 * DATA_TRANS_BLOCKS(14) +
894 * INDEX_EXTRA_BLOCKS(8) +
895 * 3(inode bits, groups, GDT)
897 [DTO_OBJECT_CREATE] = 25,
901 [DTO_OBJECT_DELETE] = 25,
904 * 3(inode bits, group, GDT)
906 [DTO_ATTR_SET_BASE] = 3,
908 * Xattr set. The same as xattr of EXT3.
909 * DATA_TRANS_BLOCKS(14)
910 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
911 * also counted in. Do not know why?
913 [DTO_XATTR_SET] = 14,
916 * creadits for inode change during write.
918 [DTO_WRITE_BASE] = 3,
920 * credits for single block write.
922 [DTO_WRITE_BLOCK] = 14,
924 * Attr set credits for chown.
925 * 3 (inode bit, group, GDT)
927 [DTO_ATTR_SET_CHOWN]= 3
931 * Note: we count into QUOTA here.
932 * If we mount with --data_journal we may need more.
934 static const int osd_dto_credits_quota[DTO_NR] = {
936 * INDEX_EXTRA_TRANS_BLOCKS(8) +
937 * SINGLEDATA_TRANS_BLOCKS(8) +
938 * 2 * QUOTA_TRANS_BLOCKS(2)
940 [DTO_INDEX_INSERT] = 20,
942 * INDEX_EXTRA_TRANS_BLOCKS(8) +
943 * SINGLEDATA_TRANS_BLOCKS(8) +
944 * 2 * QUOTA_TRANS_BLOCKS(2)
946 [DTO_INDEX_DELETE] = 20,
950 [DTO_IDNEX_UPDATE] = 16,
952 * Create a object. Same as create object in EXT3 filesystem.
953 * DATA_TRANS_BLOCKS(16) +
954 * INDEX_EXTRA_BLOCKS(8) +
955 * 3(inode bits, groups, GDT) +
956 * 2 * QUOTA_INIT_BLOCKS(25)
958 [DTO_OBJECT_CREATE] = 77,
961 * DATA_TRANS_BLOCKS(16) +
962 * INDEX_EXTRA_BLOCKS(8) +
963 * 3(inode bits, groups, GDT) +
966 [DTO_OBJECT_DELETE] = 27,
969 * 3 (inode bit, group, GDT) +
971 [DTO_ATTR_SET_BASE] = 3,
973 * Xattr set. The same as xattr of EXT3.
974 * DATA_TRANS_BLOCKS(16)
975 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
976 * also counted in. Do not know why?
978 [DTO_XATTR_SET] = 16,
981 * creadits for inode change during write.
983 [DTO_WRITE_BASE] = 3,
985 * credits for single block write.
987 [DTO_WRITE_BLOCK] = 16,
989 * Attr set credits for chown.
990 * 3 (inode bit, group, GDT) +
991 * 2 * QUOTA_INIT_BLOCKS(25) +
992 * 2 * QUOTA_DEL_BLOCKS(9)
994 [DTO_ATTR_SET_CHOWN]= 71
997 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1000 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1001 ARRAY_SIZE(osd_dto_credits_quota));
1002 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1003 #ifdef HAVE_QUOTA_SUPPORT
1004 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1005 return osd_dto_credits_quota[op];
1008 return osd_dto_credits_noquota[op];
1011 static const struct dt_device_operations osd_dt_ops = {
1012 .dt_root_get = osd_root_get,
1013 .dt_statfs = osd_statfs,
1014 .dt_trans_start = osd_trans_start,
1015 .dt_trans_stop = osd_trans_stop,
1016 .dt_conf_get = osd_conf_get,
1017 .dt_sync = osd_sync,
1019 .dt_commit_async = osd_commit_async,
1020 .dt_credit_get = osd_credit_get,
1021 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1022 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1025 static void osd_object_read_lock(const struct lu_env *env,
1026 struct dt_object *dt, unsigned role)
1028 struct osd_object *obj = osd_dt_obj(dt);
1029 struct osd_thread_info *oti = osd_oti_get(env);
1031 LINVRNT(osd_invariant(obj));
1033 LASSERT(obj->oo_owner != env);
1034 down_read_nested(&obj->oo_sem, role);
1036 LASSERT(obj->oo_owner == NULL);
1040 static void osd_object_write_lock(const struct lu_env *env,
1041 struct dt_object *dt, unsigned role)
1043 struct osd_object *obj = osd_dt_obj(dt);
1044 struct osd_thread_info *oti = osd_oti_get(env);
1046 LINVRNT(osd_invariant(obj));
1048 LASSERT(obj->oo_owner != env);
1049 down_write_nested(&obj->oo_sem, role);
1051 LASSERT(obj->oo_owner == NULL);
1052 obj->oo_owner = env;
1056 static void osd_object_read_unlock(const struct lu_env *env,
1057 struct dt_object *dt)
1059 struct osd_object *obj = osd_dt_obj(dt);
1060 struct osd_thread_info *oti = osd_oti_get(env);
1062 LINVRNT(osd_invariant(obj));
1064 LASSERT(oti->oti_r_locks > 0);
1066 up_read(&obj->oo_sem);
1069 static void osd_object_write_unlock(const struct lu_env *env,
1070 struct dt_object *dt)
1072 struct osd_object *obj = osd_dt_obj(dt);
1073 struct osd_thread_info *oti = osd_oti_get(env);
1075 LINVRNT(osd_invariant(obj));
1077 LASSERT(obj->oo_owner == env);
1078 LASSERT(oti->oti_w_locks > 0);
1080 obj->oo_owner = NULL;
1081 up_write(&obj->oo_sem);
1084 static int osd_object_write_locked(const struct lu_env *env,
1085 struct dt_object *dt)
1087 struct osd_object *obj = osd_dt_obj(dt);
1089 LINVRNT(osd_invariant(obj));
1091 return obj->oo_owner == env;
1094 static int capa_is_sane(const struct lu_env *env,
1095 struct osd_device *dev,
1096 struct lustre_capa *capa,
1097 struct lustre_capa_key *keys)
1099 struct osd_thread_info *oti = osd_oti_get(env);
1100 struct lustre_capa *tcapa = &oti->oti_capa;
1101 struct obd_capa *oc;
1105 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1107 if (capa_is_expired(oc)) {
1108 DEBUG_CAPA(D_ERROR, capa, "expired");
1115 if (capa_is_expired_sec(capa)) {
1116 DEBUG_CAPA(D_ERROR, capa, "expired");
1120 spin_lock(&capa_lock);
1121 for (i = 0; i < 2; i++) {
1122 if (keys[i].lk_keyid == capa->lc_keyid) {
1123 oti->oti_capa_key = keys[i];
1127 spin_unlock(&capa_lock);
1130 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1134 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1138 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1139 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1143 oc = capa_add(dev->od_capa_hash, capa);
1149 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1150 struct lustre_capa *capa, __u64 opc)
1152 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1153 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1154 struct md_capainfo *ci;
1157 if (!dev->od_fl_capa)
1160 if (capa == BYPASS_CAPA)
1163 ci = md_capainfo(env);
1167 if (ci->mc_auth == LC_ID_NONE)
1171 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1175 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1176 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1181 if (!capa_opc_supported(capa, opc)) {
1182 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1186 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1187 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1194 static struct timespec *osd_inode_time(const struct lu_env *env,
1195 struct inode *inode, __u64 seconds)
1197 struct osd_thread_info *oti = osd_oti_get(env);
1198 struct timespec *t = &oti->oti_time;
1200 t->tv_sec = seconds;
1202 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1207 static void osd_inode_getattr(const struct lu_env *env,
1208 struct inode *inode, struct lu_attr *attr)
1210 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1211 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1212 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1214 attr->la_atime = LTIME_S(inode->i_atime);
1215 attr->la_mtime = LTIME_S(inode->i_mtime);
1216 attr->la_ctime = LTIME_S(inode->i_ctime);
1217 attr->la_mode = inode->i_mode;
1218 attr->la_size = i_size_read(inode);
1219 attr->la_blocks = inode->i_blocks;
1220 attr->la_uid = inode->i_uid;
1221 attr->la_gid = inode->i_gid;
1222 attr->la_flags = LDISKFS_I(inode)->i_flags;
1223 attr->la_nlink = inode->i_nlink;
1224 attr->la_rdev = inode->i_rdev;
1225 attr->la_blksize = ll_inode_blksize(inode);
1226 attr->la_blkbits = inode->i_blkbits;
1229 static int osd_attr_get(const struct lu_env *env,
1230 struct dt_object *dt,
1231 struct lu_attr *attr,
1232 struct lustre_capa *capa)
1234 struct osd_object *obj = osd_dt_obj(dt);
1236 LASSERT(dt_object_exists(dt));
1237 LINVRNT(osd_invariant(obj));
1239 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1242 spin_lock(&obj->oo_guard);
1243 osd_inode_getattr(env, obj->oo_inode, attr);
1244 spin_unlock(&obj->oo_guard);
1248 static int osd_inode_setattr(const struct lu_env *env,
1249 struct inode *inode, const struct lu_attr *attr)
1253 bits = attr->la_valid;
1255 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1257 #ifdef HAVE_QUOTA_SUPPORT
1258 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1259 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1260 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1266 iattr.ia_valid |= ATTR_UID;
1268 iattr.ia_valid |= ATTR_GID;
1269 iattr.ia_uid = attr->la_uid;
1270 iattr.ia_gid = attr->la_gid;
1271 osd_push_ctxt(env, save);
1272 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1279 if (bits & LA_ATIME)
1280 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1281 if (bits & LA_CTIME)
1282 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1283 if (bits & LA_MTIME)
1284 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1285 if (bits & LA_SIZE) {
1286 LDISKFS_I(inode)->i_disksize = attr->la_size;
1287 i_size_write(inode, attr->la_size);
1290 /* OSD should not change "i_blocks" which is used by quota.
1291 * "i_blocks" should be changed by ldiskfs only.
1292 * Enable this assignment for SOM purpose now, until it is
1293 * stored in SOM EA. */
1294 if (bits & LA_BLOCKS)
1295 inode->i_blocks = attr->la_blocks;
1298 inode->i_mode = (inode->i_mode & S_IFMT) |
1299 (attr->la_mode & ~S_IFMT);
1301 inode->i_uid = attr->la_uid;
1303 inode->i_gid = attr->la_gid;
1304 if (bits & LA_NLINK)
1305 inode->i_nlink = attr->la_nlink;
1307 inode->i_rdev = attr->la_rdev;
1309 if (bits & LA_FLAGS) {
1310 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1312 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1313 (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1318 static int osd_attr_set(const struct lu_env *env,
1319 struct dt_object *dt,
1320 const struct lu_attr *attr,
1321 struct thandle *handle,
1322 struct lustre_capa *capa)
1324 struct osd_object *obj = osd_dt_obj(dt);
1327 LASSERT(handle != NULL);
1328 LASSERT(dt_object_exists(dt));
1329 LASSERT(osd_invariant(obj));
1331 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1334 spin_lock(&obj->oo_guard);
1335 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1336 spin_unlock(&obj->oo_guard);
1339 mark_inode_dirty(obj->oo_inode);
1346 * XXX temporary solution.
1348 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1349 struct lu_attr *attr, struct thandle *th)
1354 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1355 struct lu_attr *attr, struct thandle *th)
1357 osd_object_init0(obj);
1361 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1362 struct osd_object *obj,
1366 struct osd_thread_info *info = osd_oti_get(env);
1367 struct dentry *child_dentry = &info->oti_child_dentry;
1368 struct dentry *obj_dentry = &info->oti_obj_dentry;
1370 obj_dentry->d_inode = obj->oo_inode;
1371 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1372 obj_dentry->d_name.hash = 0;
1374 child_dentry->d_name.hash = 0;
1375 child_dentry->d_parent = obj_dentry;
1376 child_dentry->d_name.name = name;
1377 child_dentry->d_name.len = namelen;
1378 return child_dentry;
1382 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1384 struct dt_allocation_hint *hint,
1388 struct osd_device *osd = osd_obj2dev(obj);
1389 struct osd_thandle *oth;
1390 struct dt_object *parent;
1391 struct inode *inode;
1392 #ifdef HAVE_QUOTA_SUPPORT
1393 struct osd_ctxt *save = &info->oti_ctxt;
1396 LINVRNT(osd_invariant(obj));
1397 LASSERT(obj->oo_inode == NULL);
1399 oth = container_of(th, struct osd_thandle, ot_super);
1400 LASSERT(oth->ot_handle->h_transaction != NULL);
1402 if (hint && hint->dah_parent)
1403 parent = hint->dah_parent;
1405 parent = osd->od_obj_area;
1407 LASSERT(parent != NULL);
1408 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1410 #ifdef HAVE_QUOTA_SUPPORT
1411 osd_push_ctxt(info->oti_env, save);
1413 inode = ldiskfs_create_inode(oth->ot_handle,
1414 osd_dt_obj(parent)->oo_inode, mode);
1415 #ifdef HAVE_QUOTA_SUPPORT
1418 if (!IS_ERR(inode)) {
1419 obj->oo_inode = inode;
1422 result = PTR_ERR(inode);
1423 LINVRNT(osd_invariant(obj));
1431 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1432 struct lu_attr *attr,
1433 struct dt_allocation_hint *hint,
1434 struct dt_object_format *dof,
1438 struct osd_thandle *oth;
1439 struct osd_device *osd = osd_obj2dev(obj);
1440 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1442 LASSERT(S_ISDIR(attr->la_mode));
1444 oth = container_of(th, struct osd_thandle, ot_super);
1445 LASSERT(oth->ot_handle->h_transaction != NULL);
1446 result = osd_mkfile(info, obj, mode, hint, th);
1447 if (result == 0 && osd->od_iop_mode == 0) {
1448 LASSERT(obj->oo_inode != NULL);
1450 * XXX uh-oh... call low-level iam function directly.
1453 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1454 sizeof (struct osd_fid_pack),
1460 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1461 struct lu_attr *attr,
1462 struct dt_allocation_hint *hint,
1463 struct dt_object_format *dof,
1467 struct osd_thandle *oth;
1468 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1470 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1472 LASSERT(S_ISREG(attr->la_mode));
1474 oth = container_of(th, struct osd_thandle, ot_super);
1475 LASSERT(oth->ot_handle->h_transaction != NULL);
1477 result = osd_mkfile(info, obj, mode, hint, th);
1479 LASSERT(obj->oo_inode != NULL);
1480 if (feat->dif_flags & DT_IND_VARKEY)
1481 result = iam_lvar_create(obj->oo_inode,
1482 feat->dif_keysize_max,
1484 feat->dif_recsize_max,
1487 result = iam_lfix_create(obj->oo_inode,
1488 feat->dif_keysize_max,
1490 feat->dif_recsize_max,
1497 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1498 struct lu_attr *attr,
1499 struct dt_allocation_hint *hint,
1500 struct dt_object_format *dof,
1503 LASSERT(S_ISREG(attr->la_mode));
1504 return osd_mkfile(info, obj, (attr->la_mode &
1505 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1508 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1509 struct lu_attr *attr,
1510 struct dt_allocation_hint *hint,
1511 struct dt_object_format *dof,
1514 LASSERT(S_ISLNK(attr->la_mode));
1515 return osd_mkfile(info, obj, (attr->la_mode &
1516 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1519 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1520 struct lu_attr *attr,
1521 struct dt_allocation_hint *hint,
1522 struct dt_object_format *dof,
1525 umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1528 LINVRNT(osd_invariant(obj));
1529 LASSERT(obj->oo_inode == NULL);
1530 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1531 S_ISFIFO(mode) || S_ISSOCK(mode));
1533 result = osd_mkfile(info, obj, mode, hint, th);
1535 LASSERT(obj->oo_inode != NULL);
1536 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1538 LINVRNT(osd_invariant(obj));
1542 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1544 struct dt_allocation_hint *hint,
1545 struct dt_object_format *dof,
1548 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1550 osd_obj_type_f result;
1566 result = osd_mk_index;
1577 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1578 struct dt_object *parent, umode_t child_mode)
1582 memset(ah, 0, sizeof(*ah));
1583 ah->dah_parent = parent;
1584 ah->dah_mode = child_mode;
1588 * Helper function for osd_object_create()
1590 * \retval 0, on success
1592 static int __osd_object_create(struct osd_thread_info *info,
1593 struct osd_object *obj, struct lu_attr *attr,
1594 struct dt_allocation_hint *hint,
1595 struct dt_object_format *dof,
1601 result = osd_create_pre(info, obj, attr, th);
1603 result = osd_create_type_f(dof->dof_type)(info, obj,
1604 attr, hint, dof, th);
1606 result = osd_create_post(info, obj, attr, th);
1612 * Helper function for osd_object_create()
1614 * \retval 0, on success
1616 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1617 const struct lu_fid *fid, struct thandle *th)
1619 struct osd_thread_info *info = osd_oti_get(env);
1620 struct osd_inode_id *id = &info->oti_id;
1621 struct osd_device *osd = osd_obj2dev(obj);
1622 struct md_ucred *uc = md_ucred(env);
1624 LASSERT(obj->oo_inode != NULL);
1625 LASSERT(uc != NULL);
1627 id->oii_ino = obj->oo_inode->i_ino;
1628 id->oii_gen = obj->oo_inode->i_generation;
1630 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1631 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1634 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1635 struct lu_attr *attr,
1636 struct dt_allocation_hint *hint,
1637 struct dt_object_format *dof,
1640 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1641 struct osd_object *obj = osd_dt_obj(dt);
1642 struct osd_thread_info *info = osd_oti_get(env);
1647 LINVRNT(osd_invariant(obj));
1648 LASSERT(!dt_object_exists(dt));
1649 LASSERT(osd_write_locked(env, obj));
1650 LASSERT(th != NULL);
1652 result = __osd_object_create(info, obj, attr, hint, dof, th);
1654 result = __osd_oi_insert(env, obj, fid, th);
1656 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1657 LASSERT(osd_invariant(obj));
1662 * Helper function for osd_xattr_set()
1664 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1665 const struct lu_buf *buf, const char *name, int fl)
1667 struct osd_object *obj = osd_dt_obj(dt);
1668 struct inode *inode = obj->oo_inode;
1669 struct osd_thread_info *info = osd_oti_get(env);
1670 struct dentry *dentry = &info->oti_child_dentry;
1671 struct timespec *t = &info->oti_time;
1675 LASSERT(dt_object_exists(dt));
1676 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1677 LASSERT(osd_write_locked(env, obj));
1679 if (fl & LU_XATTR_REPLACE)
1680 fs_flags |= XATTR_REPLACE;
1682 if (fl & LU_XATTR_CREATE)
1683 fs_flags |= XATTR_CREATE;
1685 dentry->d_inode = inode;
1686 *t = inode->i_ctime;
1687 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1688 buf->lb_len, fs_flags);
1689 /* ctime should not be updated with server-side time. */
1690 spin_lock(&obj->oo_guard);
1691 inode->i_ctime = *t;
1692 spin_unlock(&obj->oo_guard);
1693 mark_inode_dirty(inode);
1698 * Put the fid into lustre_mdt_attrs, and then place the structure
1699 * inode's ea. This fid should not be altered during the life time
1702 * \retval +ve, on success
1703 * \retval -ve, on error
1705 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1707 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1708 const struct lu_fid *fid)
1710 struct osd_thread_info *info = osd_oti_get(env);
1711 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1713 fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1715 return __osd_xattr_set(env, dt,
1716 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1717 XATTR_NAME_LMA, LU_XATTR_CREATE);
1722 * Helper function to form igif
1724 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1727 lu_igif_build(fid, inode->i_ino, inode->i_generation);
1731 * Helper function to pack the fid
1733 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1734 struct lu_fid *befider)
1736 fid_cpu_to_be(befider, (struct lu_fid *)fid);
1737 memcpy(pack->fp_area, befider, sizeof(*befider));
1738 pack->fp_len = sizeof(*befider) + 1;
1741 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1746 switch (pack->fp_len) {
1747 case sizeof *fid + 1:
1748 memcpy(fid, pack->fp_area, sizeof *fid);
1749 fid_be_to_cpu(fid, fid);
1752 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1759 * Try to read the fid from inode ea into dt_rec, if return value
1760 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1762 * \param fid, object fid.
1764 * \retval 0, on success
1766 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1767 __u32 ino, struct lu_fid *fid)
1769 struct osd_thread_info *info = osd_oti_get(env);
1770 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1771 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1772 struct dentry *dentry = &info->oti_child_dentry;
1773 struct osd_inode_id *id = &info->oti_id;
1774 struct osd_device *dev;
1775 struct inode *inode;
1779 dev = osd_dev(ldev);
1782 id->oii_gen = OSD_OII_NOGEN;
1784 inode = osd_iget(info, dev, id);
1785 if (IS_ERR(inode)) {
1786 rc = PTR_ERR(inode);
1789 dentry->d_inode = inode;
1791 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1792 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1795 /* Check LMA compatibility */
1797 (mdt_attrs->lma_incompat & ~cpu_to_be32(LMA_INCOMPAT_SUPP))) {
1798 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1799 inode->i_ino, be32_to_cpu(mdt_attrs->lma_incompat) &
1800 ~LMA_INCOMPAT_SUPP);
1805 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1807 } else if (rc == -ENODATA) {
1808 osd_igif_get(env, inode, fid);
1818 * OSD layer object create function for interoperability mode (b11826).
1819 * This is mostly similar to osd_object_create(). Only difference being, fid is
1820 * inserted into inode ea here.
1822 * \retval 0, on success
1823 * \retval -ve, on error
1825 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1826 struct lu_attr *attr,
1827 struct dt_allocation_hint *hint,
1828 struct dt_object_format *dof,
1831 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1832 struct osd_object *obj = osd_dt_obj(dt);
1833 struct osd_thread_info *info = osd_oti_get(env);
1839 LASSERT(osd_invariant(obj));
1840 LASSERT(!dt_object_exists(dt));
1841 LASSERT(osd_write_locked(env, obj));
1842 LASSERT(th != NULL);
1844 result = __osd_object_create(info, obj, attr, hint, dof, th);
1846 if (hint && hint->dah_parent)
1847 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1849 /* objects under osd root shld have igif fid, so dont add fid EA */
1850 if (result == 0 && is_root == 0)
1851 result = osd_ea_fid_set(env, dt, fid);
1854 result = __osd_oi_insert(env, obj, fid, th);
1856 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1857 LINVRNT(osd_invariant(obj));
1862 * Concurrency: @dt is write locked.
1864 static void osd_object_ref_add(const struct lu_env *env,
1865 struct dt_object *dt,
1868 struct osd_object *obj = osd_dt_obj(dt);
1869 struct inode *inode = obj->oo_inode;
1871 LINVRNT(osd_invariant(obj));
1872 LASSERT(dt_object_exists(dt));
1873 LASSERT(osd_write_locked(env, obj));
1874 LASSERT(th != NULL);
1876 spin_lock(&obj->oo_guard);
1877 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1879 spin_unlock(&obj->oo_guard);
1880 mark_inode_dirty(inode);
1881 LINVRNT(osd_invariant(obj));
1885 * Concurrency: @dt is write locked.
1887 static void osd_object_ref_del(const struct lu_env *env,
1888 struct dt_object *dt,
1891 struct osd_object *obj = osd_dt_obj(dt);
1892 struct inode *inode = obj->oo_inode;
1894 LINVRNT(osd_invariant(obj));
1895 LASSERT(dt_object_exists(dt));
1896 LASSERT(osd_write_locked(env, obj));
1897 LASSERT(th != NULL);
1899 spin_lock(&obj->oo_guard);
1900 LASSERT(inode->i_nlink > 0);
1902 spin_unlock(&obj->oo_guard);
1903 mark_inode_dirty(inode);
1904 LINVRNT(osd_invariant(obj));
1908 * Concurrency: @dt is read locked.
1910 static int osd_xattr_get(const struct lu_env *env,
1911 struct dt_object *dt,
1914 struct lustre_capa *capa)
1916 struct osd_object *obj = osd_dt_obj(dt);
1917 struct inode *inode = obj->oo_inode;
1918 struct osd_thread_info *info = osd_oti_get(env);
1919 struct dentry *dentry = &info->oti_obj_dentry;
1921 LASSERT(dt_object_exists(dt));
1922 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1923 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1925 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1928 dentry->d_inode = inode;
1929 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1933 * Concurrency: @dt is write locked.
1935 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1936 const struct lu_buf *buf, const char *name, int fl,
1937 struct thandle *handle, struct lustre_capa *capa)
1939 LASSERT(handle != NULL);
1941 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1944 return __osd_xattr_set(env, dt, buf, name, fl);
1948 * Concurrency: @dt is read locked.
1950 static int osd_xattr_list(const struct lu_env *env,
1951 struct dt_object *dt,
1953 struct lustre_capa *capa)
1955 struct osd_object *obj = osd_dt_obj(dt);
1956 struct inode *inode = obj->oo_inode;
1957 struct osd_thread_info *info = osd_oti_get(env);
1958 struct dentry *dentry = &info->oti_obj_dentry;
1960 LASSERT(dt_object_exists(dt));
1961 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1962 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1964 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1967 dentry->d_inode = inode;
1968 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1972 * Concurrency: @dt is write locked.
1974 static int osd_xattr_del(const struct lu_env *env,
1975 struct dt_object *dt,
1977 struct thandle *handle,
1978 struct lustre_capa *capa)
1980 struct osd_object *obj = osd_dt_obj(dt);
1981 struct inode *inode = obj->oo_inode;
1982 struct osd_thread_info *info = osd_oti_get(env);
1983 struct dentry *dentry = &info->oti_obj_dentry;
1984 struct timespec *t = &info->oti_time;
1987 LASSERT(dt_object_exists(dt));
1988 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1989 LASSERT(osd_write_locked(env, obj));
1990 LASSERT(handle != NULL);
1992 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1995 dentry->d_inode = inode;
1996 *t = inode->i_ctime;
1997 rc = inode->i_op->removexattr(dentry, name);
1998 /* ctime should not be updated with server-side time. */
1999 spin_lock(&obj->oo_guard);
2000 inode->i_ctime = *t;
2001 spin_unlock(&obj->oo_guard);
2002 mark_inode_dirty(inode);
2006 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2007 struct dt_object *dt,
2008 struct lustre_capa *old,
2011 struct osd_thread_info *info = osd_oti_get(env);
2012 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2013 struct osd_object *obj = osd_dt_obj(dt);
2014 struct osd_device *dev = osd_obj2dev(obj);
2015 struct lustre_capa_key *key = &info->oti_capa_key;
2016 struct lustre_capa *capa = &info->oti_capa;
2017 struct obd_capa *oc;
2018 struct md_capainfo *ci;
2022 if (!dev->od_fl_capa)
2023 RETURN(ERR_PTR(-ENOENT));
2025 LASSERT(dt_object_exists(dt));
2026 LINVRNT(osd_invariant(obj));
2028 /* renewal sanity check */
2029 if (old && osd_object_auth(env, dt, old, opc))
2030 RETURN(ERR_PTR(-EACCES));
2032 ci = md_capainfo(env);
2034 RETURN(ERR_PTR(-ENOENT));
2036 switch (ci->mc_auth) {
2040 capa->lc_uid = obj->oo_inode->i_uid;
2041 capa->lc_gid = obj->oo_inode->i_gid;
2042 capa->lc_flags = LC_ID_PLAIN;
2044 case LC_ID_CONVERT: {
2047 s[0] = obj->oo_inode->i_uid;
2048 get_random_bytes(&(s[1]), sizeof(__u32));
2049 s[2] = obj->oo_inode->i_gid;
2050 get_random_bytes(&(s[3]), sizeof(__u32));
2051 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2053 RETURN(ERR_PTR(rc));
2055 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2056 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2057 capa->lc_flags = LC_ID_CONVERT;
2061 RETURN(ERR_PTR(-EINVAL));
2064 capa->lc_fid = *fid;
2066 capa->lc_flags |= dev->od_capa_alg << 24;
2067 capa->lc_timeout = dev->od_capa_timeout;
2068 capa->lc_expiry = 0;
2070 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2072 LASSERT(!capa_is_expired(oc));
2076 spin_lock(&capa_lock);
2077 *key = dev->od_capa_keys[1];
2078 spin_unlock(&capa_lock);
2080 capa->lc_keyid = key->lk_keyid;
2081 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2083 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2085 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2086 RETURN(ERR_PTR(rc));
2089 oc = capa_add(dev->od_capa_hash, capa);
2093 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2096 struct osd_object *obj = osd_dt_obj(dt);
2097 struct inode *inode = obj->oo_inode;
2098 struct osd_thread_info *info = osd_oti_get(env);
2099 struct dentry *dentry = &info->oti_obj_dentry;
2100 struct file *file = &info->oti_file;
2103 dentry->d_inode = inode;
2104 file->f_dentry = dentry;
2105 file->f_mapping = inode->i_mapping;
2106 file->f_op = inode->i_fop;
2107 LOCK_INODE_MUTEX(inode);
2108 rc = file->f_op->fsync(file, dentry, 0);
2109 UNLOCK_INODE_MUTEX(inode);
2114 * Get the 64-bit version for an inode.
2116 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2117 struct dt_object *dt)
2119 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2121 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2122 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2123 return LDISKFS_I(inode)->i_fs_version;
2127 * Set the 64-bit version and return the old version.
2129 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2130 dt_obj_version_t new_version)
2132 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2134 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2135 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2136 LDISKFS_I(inode)->i_fs_version = new_version;
2137 /** Version is set after all inode operations are finished,
2138 * so we should mark it dirty here */
2139 inode->i_sb->s_op->dirty_inode(inode);
2142 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2145 struct osd_object *obj = osd_dt_obj(dt);
2148 *data = (void *)obj->oo_inode;
2156 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2157 const struct dt_index_features *feat)
2159 struct iam_descr *descr;
2161 if (osd_object_is_root(o))
2162 return feat == &dt_directory_features;
2164 LASSERT(o->oo_dir != NULL);
2166 descr = o->oo_dir->od_container.ic_descr;
2167 if (feat == &dt_directory_features) {
2168 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2174 feat->dif_keysize_min <= descr->id_key_size &&
2175 descr->id_key_size <= feat->dif_keysize_max &&
2176 feat->dif_recsize_min <= descr->id_rec_size &&
2177 descr->id_rec_size <= feat->dif_recsize_max &&
2178 !(feat->dif_flags & (DT_IND_VARKEY |
2179 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2180 ergo(feat->dif_flags & DT_IND_UPDATE,
2181 1 /* XXX check that object (and file system) is
2186 static int osd_iam_container_init(const struct lu_env *env,
2187 struct osd_object *obj,
2188 struct osd_directory *dir)
2191 struct iam_container *bag;
2193 bag = &dir->od_container;
2194 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2196 result = iam_container_setup(bag);
2198 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2200 iam_container_fini(bag);
2207 * Concurrency: no external locking is necessary.
2209 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2210 const struct dt_index_features *feat)
2214 struct osd_object *obj = osd_dt_obj(dt);
2215 struct osd_device *osd = osd_obj2dev(obj);
2217 LINVRNT(osd_invariant(obj));
2218 LASSERT(dt_object_exists(dt));
2220 if (osd_object_is_root(obj)) {
2221 dt->do_index_ops = &osd_index_ea_ops;
2223 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2224 dt->do_index_ops = &osd_index_ea_ops;
2225 if (S_ISDIR(obj->oo_inode->i_mode))
2230 } else if (!osd_has_index(obj)) {
2231 struct osd_directory *dir;
2236 spin_lock(&obj->oo_guard);
2237 if (obj->oo_dir == NULL)
2241 * Concurrent thread allocated container data.
2244 spin_unlock(&obj->oo_guard);
2246 * Now, that we have container data, serialize its
2249 down_write(&obj->oo_ext_idx_sem);
2251 * recheck under lock.
2253 if (!osd_has_index(obj))
2254 result = osd_iam_container_init(env, obj, dir);
2257 up_write(&obj->oo_ext_idx_sem);
2263 if (result == 0 && ea_dir == 0) {
2264 if (!osd_iam_index_probe(env, obj, feat))
2267 LINVRNT(osd_invariant(obj));
2272 static const struct dt_object_operations osd_obj_ops = {
2273 .do_read_lock = osd_object_read_lock,
2274 .do_write_lock = osd_object_write_lock,
2275 .do_read_unlock = osd_object_read_unlock,
2276 .do_write_unlock = osd_object_write_unlock,
2277 .do_write_locked = osd_object_write_locked,
2278 .do_attr_get = osd_attr_get,
2279 .do_attr_set = osd_attr_set,
2280 .do_ah_init = osd_ah_init,
2281 .do_create = osd_object_create,
2282 .do_index_try = osd_index_try,
2283 .do_ref_add = osd_object_ref_add,
2284 .do_ref_del = osd_object_ref_del,
2285 .do_xattr_get = osd_xattr_get,
2286 .do_xattr_set = osd_xattr_set,
2287 .do_xattr_del = osd_xattr_del,
2288 .do_xattr_list = osd_xattr_list,
2289 .do_capa_get = osd_capa_get,
2290 .do_object_sync = osd_object_sync,
2291 .do_version_get = osd_object_version_get,
2292 .do_version_set = osd_object_version_set,
2293 .do_data_get = osd_data_get,
2297 * dt_object_operations for interoperability mode
2298 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2300 static const struct dt_object_operations osd_obj_ea_ops = {
2301 .do_read_lock = osd_object_read_lock,
2302 .do_write_lock = osd_object_write_lock,
2303 .do_read_unlock = osd_object_read_unlock,
2304 .do_write_unlock = osd_object_write_unlock,
2305 .do_write_locked = osd_object_write_locked,
2306 .do_attr_get = osd_attr_get,
2307 .do_attr_set = osd_attr_set,
2308 .do_ah_init = osd_ah_init,
2309 .do_create = osd_object_ea_create,
2310 .do_index_try = osd_index_try,
2311 .do_ref_add = osd_object_ref_add,
2312 .do_ref_del = osd_object_ref_del,
2313 .do_xattr_get = osd_xattr_get,
2314 .do_xattr_set = osd_xattr_set,
2315 .do_xattr_del = osd_xattr_del,
2316 .do_xattr_list = osd_xattr_list,
2317 .do_capa_get = osd_capa_get,
2318 .do_object_sync = osd_object_sync,
2319 .do_version_get = osd_object_version_get,
2320 .do_version_set = osd_object_version_set,
2321 .do_data_get = osd_data_get,
2329 * XXX: Another layering violation for now.
2331 * We don't want to use ->f_op->read methods, because generic file write
2333 * - serializes on ->i_sem, and
2335 * - does a lot of extra work like balance_dirty_pages(),
2337 * which doesn't work for globally shared files like /last-received.
2339 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2340 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2341 loff_t *offs, handle_t *handle);
2343 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2344 struct lu_buf *buf, loff_t *pos,
2345 struct lustre_capa *capa)
2347 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2349 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2352 return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2355 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2356 const struct lu_buf *buf, loff_t *pos,
2357 struct thandle *handle, struct lustre_capa *capa,
2360 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2361 struct osd_thandle *oh;
2363 #ifdef HAVE_QUOTA_SUPPORT
2364 cfs_cap_t save = current->cap_effective;
2367 LASSERT(handle != NULL);
2369 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2372 oh = container_of(handle, struct osd_thandle, ot_super);
2373 LASSERT(oh->ot_handle->h_transaction != NULL);
2374 #ifdef HAVE_QUOTA_SUPPORT
2376 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2378 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2380 result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2381 pos, oh->ot_handle);
2382 #ifdef HAVE_QUOTA_SUPPORT
2383 current->cap_effective = save;
2386 result = buf->lb_len;
2390 static const struct dt_body_operations osd_body_ops = {
2391 .dbo_read = osd_read,
2392 .dbo_write = osd_write
2397 * delete a (key, value) pair from index \a dt specified by \a key
2399 * \param dt_object osd index object
2400 * \param key key for index
2401 * \param rec record reference
2402 * \param handle transaction handler
2405 * \retval -ve failure
2408 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2409 const struct dt_key *key, struct thandle *handle,
2410 struct lustre_capa *capa)
2412 struct osd_object *obj = osd_dt_obj(dt);
2413 struct osd_thandle *oh;
2414 struct iam_path_descr *ipd;
2415 struct iam_container *bag = &obj->oo_dir->od_container;
2420 LINVRNT(osd_invariant(obj));
2421 LASSERT(dt_object_exists(dt));
2422 LASSERT(bag->ic_object == obj->oo_inode);
2423 LASSERT(handle != NULL);
2425 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2428 ipd = osd_idx_ipd_get(env, bag);
2429 if (unlikely(ipd == NULL))
2432 oh = container_of0(handle, struct osd_thandle, ot_super);
2433 LASSERT(oh->ot_handle != NULL);
2434 LASSERT(oh->ot_handle->h_transaction != NULL);
2436 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2437 osd_ipd_put(env, bag, ipd);
2438 LINVRNT(osd_invariant(obj));
2443 * Index delete function for interoperability mode (b11826).
2444 * It will remove the directory entry added by osd_index_ea_insert().
2445 * This entry is needed to maintain name->fid mapping.
2447 * \param key, key i.e. file entry to be deleted
2449 * \retval 0, on success
2450 * \retval -ve, on error
2452 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2453 const struct dt_key *key, struct thandle *handle,
2454 struct lustre_capa *capa)
2456 struct osd_object *obj = osd_dt_obj(dt);
2457 struct inode *dir = obj->oo_inode;
2458 struct dentry *dentry;
2459 struct osd_thandle *oh;
2460 struct ldiskfs_dir_entry_2 *de;
2461 struct buffer_head *bh;
2467 LINVRNT(osd_invariant(obj));
2468 LASSERT(dt_object_exists(dt));
2469 LASSERT(handle != NULL);
2471 oh = container_of(handle, struct osd_thandle, ot_super);
2472 LASSERT(oh->ot_handle != NULL);
2473 LASSERT(oh->ot_handle->h_transaction != NULL);
2475 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2478 dentry = osd_child_dentry_get(env, obj,
2479 (char *)key, strlen((char *)key));
2481 down_write(&obj->oo_ext_idx_sem);
2482 bh = ldiskfs_find_entry(dentry, &de);
2484 struct osd_thread_info *oti = osd_oti_get(env);
2485 struct timespec *ctime = &oti->oti_time;
2486 struct timespec *mtime = &oti->oti_time2;
2488 *ctime = dir->i_ctime;
2489 *mtime = dir->i_mtime;
2490 rc = ldiskfs_delete_entry(oh->ot_handle,
2492 /* xtime should not be updated with server-side time. */
2493 spin_lock(&obj->oo_guard);
2494 dir->i_ctime = *ctime;
2495 dir->i_mtime = *mtime;
2496 spin_unlock(&obj->oo_guard);
2497 mark_inode_dirty(dir);
2502 up_write(&obj->oo_ext_idx_sem);
2503 LASSERT(osd_invariant(obj));
2508 * Lookup index for \a key and copy record to \a rec.
2510 * \param dt_object osd index object
2511 * \param key key for index
2512 * \param rec record reference
2514 * \retval +ve success : exact mach
2515 * \retval 0 return record with key not greater than \a key
2516 * \retval -ve failure
2518 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2519 struct dt_rec *rec, const struct dt_key *key,
2520 struct lustre_capa *capa)
2522 struct osd_object *obj = osd_dt_obj(dt);
2523 struct iam_path_descr *ipd;
2524 struct iam_container *bag = &obj->oo_dir->od_container;
2525 struct osd_thread_info *oti = osd_oti_get(env);
2526 struct iam_iterator *it = &oti->oti_idx_it;
2527 struct iam_rec *iam_rec;
2531 LASSERT(osd_invariant(obj));
2532 LASSERT(dt_object_exists(dt));
2533 LASSERT(bag->ic_object == obj->oo_inode);
2535 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2538 ipd = osd_idx_ipd_get(env, bag);
2542 /* got ipd now we can start iterator. */
2543 iam_it_init(it, bag, 0, ipd);
2545 rc = iam_it_get(it, (struct iam_key *)key);
2547 if (S_ISDIR(obj->oo_inode->i_mode))
2548 iam_rec = (struct iam_rec *)oti->oti_fid_packed;
2550 iam_rec = (struct iam_rec *) rec;
2552 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2553 if (S_ISDIR(obj->oo_inode->i_mode))
2554 osd_fid_unpack((struct lu_fid *) rec,
2555 (struct osd_fid_pack *)iam_rec);
2559 osd_ipd_put(env, bag, ipd);
2561 LINVRNT(osd_invariant(obj));
2567 * Inserts (key, value) pair in \a dt index object.
2569 * \param dt osd index object
2570 * \param key key for index
2571 * \param rec record reference
2572 * \param th transaction handler
2575 * \retval -ve failure
2577 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2578 const struct dt_rec *rec, const struct dt_key *key,
2579 struct thandle *th, struct lustre_capa *capa,
2582 struct osd_object *obj = osd_dt_obj(dt);
2583 struct iam_path_descr *ipd;
2584 struct osd_thandle *oh;
2585 struct iam_container *bag = &obj->oo_dir->od_container;
2586 #ifdef HAVE_QUOTA_SUPPORT
2587 cfs_cap_t save = current->cap_effective;
2589 struct osd_thread_info *oti = osd_oti_get(env);
2590 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_fid_packed;
2595 LINVRNT(osd_invariant(obj));
2596 LASSERT(dt_object_exists(dt));
2597 LASSERT(bag->ic_object == obj->oo_inode);
2598 LASSERT(th != NULL);
2600 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2603 ipd = osd_idx_ipd_get(env, bag);
2604 if (unlikely(ipd == NULL))
2607 oh = container_of0(th, struct osd_thandle, ot_super);
2608 LASSERT(oh->ot_handle != NULL);
2609 LASSERT(oh->ot_handle->h_transaction != NULL);
2610 #ifdef HAVE_QUOTA_SUPPORT
2612 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2614 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2616 if (S_ISDIR(obj->oo_inode->i_mode))
2617 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2619 iam_rec = (struct iam_rec *) rec;
2620 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2622 #ifdef HAVE_QUOTA_SUPPORT
2623 current->cap_effective = save;
2625 osd_ipd_put(env, bag, ipd);
2626 LINVRNT(osd_invariant(obj));
2631 * Calls ldiskfs_add_entry() to add directory entry
2632 * into the directory. This is required for
2633 * interoperability mode (b11826)
2635 * \retval 0, on success
2636 * \retval -ve, on error
2638 static int __osd_ea_add_rec(struct osd_thread_info *info,
2639 struct osd_object *pobj,
2640 struct osd_object *cobj,
2644 struct dentry *child;
2645 struct osd_thandle *oth;
2646 struct inode *cinode = cobj->oo_inode;
2649 oth = container_of(th, struct osd_thandle, ot_super);
2650 LASSERT(oth->ot_handle != NULL);
2651 LASSERT(oth->ot_handle->h_transaction != NULL);
2653 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2654 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2660 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2661 * into the directory.Also sets flags into osd object to
2662 * indicate dot and dotdot are created. This is required for
2663 * interoperability mode (b11826)
2665 * \param dir directory for dot and dotdot fixup.
2666 * \param obj child object for linking
2668 * \retval 0, on success
2669 * \retval -ve, on error
2671 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2672 struct osd_object *dir,
2673 struct osd_object *obj, const char *name,
2676 struct inode *parent_dir = obj->oo_inode;
2677 struct inode *inode = dir->oo_inode;
2678 struct osd_thandle *oth;
2681 oth = container_of(th, struct osd_thandle, ot_super);
2682 LASSERT(oth->ot_handle->h_transaction != NULL);
2683 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2685 if (strcmp(name, dot) == 0) {
2686 if (dir->oo_compat_dot_created) {
2689 LASSERT(obj == dir);
2690 dir->oo_compat_dot_created = 1;
2693 } else if(strcmp(name, dotdot) == 0) {
2694 if (!dir->oo_compat_dot_created)
2696 if (dir->oo_compat_dotdot_created)
2697 return __osd_ea_add_rec(info, dir, obj, name, th);
2699 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2701 dir->oo_compat_dotdot_created = 1;
2709 * It will call the appropriate osd_add* function and return the
2710 * value, return by respective functions.
2712 static int osd_ea_add_rec(const struct lu_env *env,
2713 struct osd_object *pobj,
2714 struct osd_object *cobj,
2718 struct osd_thread_info *info = osd_oti_get(env);
2721 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2723 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2725 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2731 * Calls ->lookup() to find dentry. From dentry get inode and
2732 * read inode's ea to get fid. This is required for interoperability
2735 * \retval 0, on success
2736 * \retval -ve, on error
2738 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2739 struct dt_rec *rec, const struct dt_key *key)
2741 struct inode *dir = obj->oo_inode;
2742 struct dentry *dentry;
2743 struct ldiskfs_dir_entry_2 *de;
2744 struct buffer_head *bh;
2745 struct lu_fid *fid = (struct lu_fid *) rec;
2749 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2751 dentry = osd_child_dentry_get(env, obj,
2752 (char *)key, strlen((char *)key));
2754 down_read(&obj->oo_ext_idx_sem);
2755 bh = ldiskfs_find_entry(dentry, &de);
2757 ino = le32_to_cpu(de->inode);
2759 rc = osd_ea_fid_get(env, obj, ino, fid);
2763 up_read(&obj->oo_ext_idx_sem);
2768 * Find the osd object for given fid.
2770 * \param fid, need to find the osd object having this fid
2772 * \retval osd_object, on success
2773 * \retval -ve, on error
2775 struct osd_object *osd_object_find(const struct lu_env *env,
2776 struct dt_object *dt,
2777 const struct lu_fid *fid)
2779 struct lu_device *ludev = dt->do_lu.lo_dev;
2780 struct osd_object *child = NULL;
2781 struct lu_object *luch;
2782 struct lu_object *lo;
2784 luch = lu_object_find(env, ludev, fid, NULL);
2785 if (!IS_ERR(luch)) {
2786 if (lu_object_exists(luch)) {
2787 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2789 child = osd_obj(lo);
2791 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2792 "lu_object can't be located"
2793 ""DFID"\n", PFID(fid));
2795 if (child == NULL) {
2796 lu_object_put(env, luch);
2797 CERROR("Unable to get osd_object\n");
2798 child = ERR_PTR(-ENOENT);
2801 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2802 "lu_object does not exists "DFID"\n",
2804 child = ERR_PTR(-ENOENT);
2807 child = (void *)luch;
2813 * Put the osd object once done with it.
2815 * \param obj, osd object that needs to be put
2817 static inline void osd_object_put(const struct lu_env *env,
2818 struct osd_object *obj)
2820 lu_object_put(env, &obj->oo_dt.do_lu);
2824 * Index add function for interoperability mode (b11826).
2825 * It will add the directory entry.This entry is needed to
2826 * maintain name->fid mapping.
2828 * \param key, it is key i.e. file entry to be inserted
2829 * \param rec, it is value of given key i.e. fid
2831 * \retval 0, on success
2832 * \retval -ve, on error
2834 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2835 const struct dt_rec *rec,
2836 const struct dt_key *key, struct thandle *th,
2837 struct lustre_capa *capa, int ignore_quota)
2839 struct osd_object *obj = osd_dt_obj(dt);
2840 struct lu_fid *fid = (struct lu_fid *) rec;
2841 const char *name = (const char *)key;
2842 struct osd_object *child;
2843 #ifdef HAVE_QUOTA_SUPPORT
2844 cfs_cap_t save = current->cap_effective;
2850 LASSERT(osd_invariant(obj));
2851 LASSERT(dt_object_exists(dt));
2852 LASSERT(th != NULL);
2854 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2857 child = osd_object_find(env, dt, fid);
2858 if (!IS_ERR(child)) {
2859 struct inode *inode = obj->oo_inode;
2860 struct osd_thread_info *oti = osd_oti_get(env);
2861 struct timespec *ctime = &oti->oti_time;
2862 struct timespec *mtime = &oti->oti_time2;
2864 *ctime = inode->i_ctime;
2865 *mtime = inode->i_mtime;
2866 #ifdef HAVE_QUOTA_SUPPORT
2868 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2870 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2872 down_write(&obj->oo_ext_idx_sem);
2873 rc = osd_ea_add_rec(env, obj, child, name, th);
2874 up_write(&obj->oo_ext_idx_sem);
2875 #ifdef HAVE_QUOTA_SUPPORT
2876 current->cap_effective = save;
2878 osd_object_put(env, child);
2879 /* xtime should not be updated with server-side time. */
2880 spin_lock(&obj->oo_guard);
2881 inode->i_ctime = *ctime;
2882 inode->i_mtime = *mtime;
2883 spin_unlock(&obj->oo_guard);
2884 mark_inode_dirty(inode);
2886 rc = PTR_ERR(child);
2889 LASSERT(osd_invariant(obj));
2894 * Initialize osd Iterator for given osd index object.
2896 * \param dt osd index object
2899 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2900 struct dt_object *dt,
2901 struct lustre_capa *capa)
2903 struct osd_it_iam *it;
2904 struct osd_thread_info *oti = osd_oti_get(env);
2905 struct osd_object *obj = osd_dt_obj(dt);
2906 struct lu_object *lo = &dt->do_lu;
2907 struct iam_path_descr *ipd;
2908 struct iam_container *bag = &obj->oo_dir->od_container;
2910 LASSERT(lu_object_exists(lo));
2912 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2913 return ERR_PTR(-EACCES);
2916 ipd = osd_it_ipd_get(env, bag);
2917 if (likely(ipd != NULL)) {
2921 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2922 return (struct dt_it *)it;
2924 return ERR_PTR(-ENOMEM);
2928 * free given Iterator.
2931 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2933 struct osd_it_iam *it = (struct osd_it_iam *)di;
2934 struct osd_object *obj = it->oi_obj;
2936 iam_it_fini(&it->oi_it);
2937 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2938 lu_object_put(env, &obj->oo_dt.do_lu);
2942 * Move Iterator to record specified by \a key
2944 * \param di osd iterator
2945 * \param key key for index
2947 * \retval +ve di points to record with least key not larger than key
2948 * \retval 0 di points to exact matched key
2949 * \retval -ve failure
2952 static int osd_it_iam_get(const struct lu_env *env,
2953 struct dt_it *di, const struct dt_key *key)
2955 struct osd_it_iam *it = (struct osd_it_iam *)di;
2957 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2963 * \param di osd iterator
2966 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2968 struct osd_it_iam *it = (struct osd_it_iam *)di;
2970 iam_it_put(&it->oi_it);
2974 * Move iterator by one record
2976 * \param di osd iterator
2978 * \retval +1 end of container reached
2980 * \retval -ve failure
2983 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2985 struct osd_it_iam *it = (struct osd_it_iam *)di;
2987 return iam_it_next(&it->oi_it);
2991 * Return pointer to the key under iterator.
2994 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2995 const struct dt_it *di)
2997 struct osd_it_iam *it = (struct osd_it_iam *)di;
2999 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3003 * Return size of key under iterator (in bytes)
3006 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3008 struct osd_it_iam *it = (struct osd_it_iam *)di;
3010 return iam_it_key_size(&it->oi_it);
3013 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3018 struct luda_type *lt;
3019 const unsigned align = sizeof(struct luda_type) - 1;
3021 /* check if file type is required */
3022 if (attr & LUDA_TYPE) {
3023 len = (len + align) & ~align;
3025 lt = (void *) ent->lde_name + len;
3026 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3027 ent->lde_attrs |= LUDA_TYPE;
3030 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3034 * build lu direct from backend fs dirent.
3037 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3045 fid_cpu_to_le(&ent->lde_fid, fid);
3046 ent->lde_attrs = LUDA_FID;
3048 ent->lde_hash = cpu_to_le64(offset);
3049 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3051 strncpy(ent->lde_name, name, namelen);
3052 ent->lde_namelen = cpu_to_le16(namelen);
3054 /* append lustre attributes */
3055 osd_it_append_attrs(ent, attr, namelen, type);
3059 * Return pointer to the record under iterator.
3061 static int osd_it_iam_rec(const struct lu_env *env,
3062 const struct dt_it *di,
3063 struct lu_dirent *lde,
3066 struct osd_it_iam *it = (struct osd_it_iam *)di;
3067 struct osd_thread_info *info = osd_oti_get(env);
3068 struct lu_fid *fid = &info->oti_fid;
3069 const struct osd_fid_pack *rec;
3075 name = (char *)iam_it_key_get(&it->oi_it);
3077 RETURN(PTR_ERR(name));
3079 namelen = iam_it_key_size(&it->oi_it);
3081 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3083 RETURN(PTR_ERR(rec));
3085 rc = osd_fid_unpack(fid, rec);
3089 hash = iam_it_store(&it->oi_it);
3091 /* IAM does not store object type in IAM index (dir) */
3092 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3099 * Returns cookie for current Iterator position.
3101 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3103 struct osd_it_iam *it = (struct osd_it_iam *)di;
3105 return iam_it_store(&it->oi_it);
3109 * Restore iterator from cookie.
3111 * \param di osd iterator
3112 * \param hash Iterator location cookie
3114 * \retval +ve di points to record with least key not larger than key.
3115 * \retval 0 di points to exact matched key
3116 * \retval -ve failure
3119 static int osd_it_iam_load(const struct lu_env *env,
3120 const struct dt_it *di, __u64 hash)
3122 struct osd_it_iam *it = (struct osd_it_iam *)di;
3124 return iam_it_load(&it->oi_it, hash);
3127 static const struct dt_index_operations osd_index_iam_ops = {
3128 .dio_lookup = osd_index_iam_lookup,
3129 .dio_insert = osd_index_iam_insert,
3130 .dio_delete = osd_index_iam_delete,
3132 .init = osd_it_iam_init,
3133 .fini = osd_it_iam_fini,
3134 .get = osd_it_iam_get,
3135 .put = osd_it_iam_put,
3136 .next = osd_it_iam_next,
3137 .key = osd_it_iam_key,
3138 .key_size = osd_it_iam_key_size,
3139 .rec = osd_it_iam_rec,
3140 .store = osd_it_iam_store,
3141 .load = osd_it_iam_load
3146 * Creates or initializes iterator context.
3148 * \retval struct osd_it_ea, iterator structure on success
3151 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3152 struct dt_object *dt,
3153 struct lustre_capa *capa)
3155 struct osd_object *obj = osd_dt_obj(dt);
3156 struct osd_thread_info *info = osd_oti_get(env);
3157 struct osd_it_ea *it = &info->oti_it_ea;
3158 struct lu_object *lo = &dt->do_lu;
3159 struct dentry *obj_dentry = &info->oti_it_dentry;
3161 LASSERT(lu_object_exists(lo));
3163 obj_dentry->d_inode = obj->oo_inode;
3164 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3165 obj_dentry->d_name.hash = 0;
3167 it->oie_rd_dirent = 0;
3168 it->oie_it_dirent = 0;
3169 it->oie_dirent = NULL;
3170 it->oie_buf = info->oti_it_ea_buf;
3172 it->oie_file.f_pos = 0;
3173 it->oie_file.f_dentry = obj_dentry;
3174 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3175 it->oie_file.f_op = obj->oo_inode->i_fop;
3176 it->oie_file.private_data = NULL;
3178 RETURN((struct dt_it *) it);
3182 * Destroy or finishes iterator context.
3184 * \param di, struct osd_it_ea, iterator structure to be destroyed
3186 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3188 struct osd_it_ea *it = (struct osd_it_ea *)di;
3189 struct osd_object *obj = it->oie_obj;
3190 struct inode *inode = obj->oo_inode;
3193 it->oie_file.f_op->release(inode, &it->oie_file);
3194 lu_object_put(env, &obj->oo_dt.do_lu);
3199 * It position the iterator at given key, so that next lookup continues from
3200 * that key Or it is similar to dio_it->load() but based on a key,
3201 * rather than file position.
3203 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3206 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3208 static int osd_it_ea_get(const struct lu_env *env,
3209 struct dt_it *di, const struct dt_key *key)
3211 struct osd_it_ea *it = (struct osd_it_ea *)di;
3214 LASSERT(((const char *)key)[0] == '\0');
3215 it->oie_file.f_pos = 0;
3216 it->oie_rd_dirent = 0;
3217 it->oie_it_dirent = 0;
3218 it->oie_dirent = NULL;
3226 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3231 * It is called internally by ->readdir(). It fills the
3232 * iterator's in-memory data structure with required
3233 * information i.e. name, namelen, rec_size etc.
3235 * \param buf, in which information to be filled in.
3236 * \param name, name of the file in given dir
3238 * \retval 0, on success
3239 * \retval 1, on buffer full
3241 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3242 loff_t offset, __u64 ino,
3245 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3246 struct osd_it_ea_dirent *ent = it->oie_dirent;
3249 /* this should never happen */
3250 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3251 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3255 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3259 ent->oied_ino = ino;
3260 ent->oied_off = offset;
3261 ent->oied_namelen = namelen;
3262 ent->oied_type = d_type;
3264 memcpy(ent->oied_name, name, namelen);
3266 it->oie_rd_dirent++;
3267 it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3272 * Calls ->readdir() to load a directory entry at a time
3273 * and stored it in iterator's in-memory data structure.
3275 * \param di, struct osd_it_ea, iterator's in memory structure
3277 * \retval 0, on success
3278 * \retval -ve, on error
3280 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3282 struct osd_it_ea *it = (struct osd_it_ea *)di;
3283 struct osd_object *obj = it->oie_obj;
3284 struct inode *inode = obj->oo_inode;
3288 it->oie_dirent = it->oie_buf;
3289 it->oie_rd_dirent = 0;
3291 down_read(&obj->oo_ext_idx_sem);
3292 result = inode->i_fop->readdir(&it->oie_file, it,
3293 (filldir_t) osd_ldiskfs_filldir);
3295 up_read(&obj->oo_ext_idx_sem);
3297 if (it->oie_rd_dirent == 0) {
3300 it->oie_dirent = it->oie_buf;
3301 it->oie_it_dirent = 1;
3308 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3309 * to load a directory entry at a time and stored it in
3310 * iterator's in-memory data structure.
3312 * \param di, struct osd_it_ea, iterator's in memory structure
3314 * \retval +ve, iterator reached to end
3315 * \retval 0, iterator not reached to end
3316 * \retval -ve, on error
3318 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3320 struct osd_it_ea *it = (struct osd_it_ea *)di;
3325 if (it->oie_it_dirent < it->oie_rd_dirent) {
3326 it->oie_dirent = (void *) it->oie_dirent +
3327 size_round(sizeof(struct osd_it_ea_dirent) +
3328 it->oie_dirent->oied_namelen);
3329 it->oie_it_dirent++;
3332 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3335 rc = osd_ldiskfs_it_fill(di);
3342 * Returns the key at current position from iterator's in memory structure.
3344 * \param di, struct osd_it_ea, iterator's in memory structure
3346 * \retval key i.e. struct dt_key on success
3348 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3349 const struct dt_it *di)
3351 struct osd_it_ea *it = (struct osd_it_ea *)di;
3353 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3357 * Returns the key's size at current position from iterator's in memory structure.
3359 * \param di, struct osd_it_ea, iterator's in memory structure
3361 * \retval key_size i.e. struct dt_key on success
3363 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3365 struct osd_it_ea *it = (struct osd_it_ea *)di;
3367 RETURN(it->oie_dirent->oied_namelen);
3372 * Returns the value (i.e. fid/igif) at current position from iterator's
3373 * in memory structure.
3375 * \param di, struct osd_it_ea, iterator's in memory structure
3376 * \param attr, attr requested for dirent.
3377 * \param lde, lustre dirent
3379 * \retval 0, no error and \param lde has correct lustre dirent.
3380 * \retval -ve, on error
3382 static inline int osd_it_ea_rec(const struct lu_env *env,
3383 const struct dt_it *di,
3384 struct lu_dirent *lde,
3387 struct osd_it_ea *it = (struct osd_it_ea *)di;
3388 struct osd_object *obj = it->oie_obj;
3389 struct osd_thread_info *info = osd_oti_get(env);
3390 struct lu_fid *fid = &info->oti_fid;
3395 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3398 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3399 it->oie_dirent->oied_name,
3400 it->oie_dirent->oied_namelen,
3401 it->oie_dirent->oied_type,
3407 * Returns a cookie for current position of the iterator head, so that
3408 * user can use this cookie to load/start the iterator next time.
3410 * \param di, struct osd_it_ea, iterator's in memory structure
3412 * \retval cookie for current position, on success
3414 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3416 struct osd_it_ea *it = (struct osd_it_ea *)di;
3418 RETURN(it->oie_dirent->oied_off);
3422 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3423 * to load a directory entry at a time and stored it i inn,
3424 * in iterator's in-memory data structure.
3426 * \param di, struct osd_it_ea, iterator's in memory structure
3428 * \retval +ve, on success
3429 * \retval -ve, on error
3431 static int osd_it_ea_load(const struct lu_env *env,
3432 const struct dt_it *di, __u64 hash)
3434 struct osd_it_ea *it = (struct osd_it_ea *)di;
3438 it->oie_file.f_pos = hash;
3440 rc = osd_ldiskfs_it_fill(di);
3448 * Index lookup function for interoperability mode (b11826).
3450 * \param key, key i.e. file name to be searched
3452 * \retval +ve, on success
3453 * \retval -ve, on error
3455 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3456 struct dt_rec *rec, const struct dt_key *key,
3457 struct lustre_capa *capa)
3459 struct osd_object *obj = osd_dt_obj(dt);
3464 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3465 LINVRNT(osd_invariant(obj));
3467 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3470 rc = osd_ea_lookup_rec(env, obj, rec, key);
3478 * Index and Iterator operations for interoperability
3479 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3481 static const struct dt_index_operations osd_index_ea_ops = {
3482 .dio_lookup = osd_index_ea_lookup,
3483 .dio_insert = osd_index_ea_insert,
3484 .dio_delete = osd_index_ea_delete,
3486 .init = osd_it_ea_init,
3487 .fini = osd_it_ea_fini,
3488 .get = osd_it_ea_get,
3489 .put = osd_it_ea_put,
3490 .next = osd_it_ea_next,
3491 .key = osd_it_ea_key,
3492 .key_size = osd_it_ea_key_size,
3493 .rec = osd_it_ea_rec,
3494 .store = osd_it_ea_store,
3495 .load = osd_it_ea_load
3499 static void *osd_key_init(const struct lu_context *ctx,
3500 struct lu_context_key *key)
3502 struct osd_thread_info *info;
3504 OBD_ALLOC_PTR(info);
3506 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3507 if (info->oti_it_ea_buf != NULL) {
3508 info->oti_env = container_of(ctx, struct lu_env,
3512 info = ERR_PTR(-ENOMEM);
3515 info = ERR_PTR(-ENOMEM);
3520 static void osd_key_fini(const struct lu_context *ctx,
3521 struct lu_context_key *key, void* data)
3523 struct osd_thread_info *info = data;
3525 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3529 static void osd_key_exit(const struct lu_context *ctx,
3530 struct lu_context_key *key, void *data)
3532 struct osd_thread_info *info = data;
3534 LASSERT(info->oti_r_locks == 0);
3535 LASSERT(info->oti_w_locks == 0);
3536 LASSERT(info->oti_txns == 0);
3539 /* type constructor/destructor: osd_type_init, osd_type_fini */
3540 LU_TYPE_INIT_FINI(osd, &osd_key);
3542 static struct lu_context_key osd_key = {
3543 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3544 .lct_init = osd_key_init,
3545 .lct_fini = osd_key_fini,
3546 .lct_exit = osd_key_exit
3550 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3551 const char *name, struct lu_device *next)
3554 struct lu_context *ctx;
3556 /* context for commit hooks */
3557 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3558 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3560 rc = osd_procfs_init(osd_dev(d), name);
3561 ctx->lc_cookie = 0x3;
3566 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3568 struct osd_thread_info *info = osd_oti_get(env);
3570 if (o->od_obj_area != NULL) {
3571 lu_object_put(env, &o->od_obj_area->do_lu);
3572 o->od_obj_area = NULL;
3574 osd_oi_fini(info, &o->od_oi);
3579 static int osd_mount(const struct lu_env *env,
3580 struct osd_device *o, struct lustre_cfg *cfg)
3582 struct lustre_mount_info *lmi;
3583 const char *dev = lustre_cfg_string(cfg, 0);
3584 struct lustre_disk_data *ldd;
3585 struct lustre_sb_info *lsi;
3588 if (o->od_mount != NULL) {
3589 CERROR("Already mounted (%s)\n", dev);
3594 lmi = server_get_mount(dev);
3596 CERROR("Cannot get mount info for %s!\n", dev);
3600 LASSERT(lmi != NULL);
3601 /* save lustre_mount_info in dt_device */
3604 lsi = s2lsi(lmi->lmi_sb);
3607 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3609 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3613 o->od_obj_area = NULL;
3617 static struct lu_device *osd_device_fini(const struct lu_env *env,
3618 struct lu_device *d)
3623 shrink_dcache_sb(osd_sb(osd_dev(d)));
3624 osd_sync(env, lu2dt_dev(d));
3626 rc = osd_procfs_fini(osd_dev(d));
3628 CERROR("proc fini error %d \n", rc);
3629 RETURN (ERR_PTR(rc));
3632 if (osd_dev(d)->od_mount)
3633 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3634 osd_dev(d)->od_mount->lmi_mnt);
3635 osd_dev(d)->od_mount = NULL;
3637 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3641 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3642 struct lu_device_type *t,
3643 struct lustre_cfg *cfg)
3645 struct lu_device *l;
3646 struct osd_device *o;
3652 result = dt_device_init(&o->od_dt_dev, t);
3655 l->ld_ops = &osd_lu_ops;
3656 o->od_dt_dev.dd_ops = &osd_dt_ops;
3657 spin_lock_init(&o->od_osfs_lock);
3658 o->od_osfs_age = cfs_time_shift_64(-1000);
3659 o->od_capa_hash = init_capa_hash();
3660 if (o->od_capa_hash == NULL) {
3661 dt_device_fini(&o->od_dt_dev);
3662 l = ERR_PTR(-ENOMEM);
3665 l = ERR_PTR(result);
3670 l = ERR_PTR(-ENOMEM);
3674 static struct lu_device *osd_device_free(const struct lu_env *env,
3675 struct lu_device *d)
3677 struct osd_device *o = osd_dev(d);
3680 cleanup_capa_hash(o->od_capa_hash);
3681 dt_device_fini(&o->od_dt_dev);
3686 static int osd_process_config(const struct lu_env *env,
3687 struct lu_device *d, struct lustre_cfg *cfg)
3689 struct osd_device *o = osd_dev(d);
3693 switch(cfg->lcfg_command) {
3695 err = osd_mount(env, o, cfg);
3698 err = osd_shutdown(env, o);
3707 static int osd_recovery_complete(const struct lu_env *env,
3708 struct lu_device *d)
3710 struct osd_device *o = osd_dev(d);
3712 /* TODO: orphans handling */
3713 ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3717 static int osd_prepare(const struct lu_env *env,
3718 struct lu_device *pdev,
3719 struct lu_device *dev)
3721 struct osd_device *osd = osd_dev(dev);
3722 struct lustre_sb_info *lsi;
3723 struct lustre_disk_data *ldd;
3724 struct lustre_mount_info *lmi;
3725 struct osd_thread_info *oti = osd_oti_get(env);
3726 struct dt_object *d;
3730 /* 1. initialize oi before any file create or file open */
3731 result = osd_oi_init(oti, &osd->od_oi,
3732 &osd->od_dt_dev, lu2md_dev(pdev));
3736 lmi = osd->od_mount;
3737 lsi = s2lsi(lmi->lmi_sb);
3740 /* 2. setup local objects */
3741 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3745 /* 3. open remote object dir */
3746 d = dt_store_open(env, lu2dt_dev(dev), "",
3747 remote_obj_dir, &oti->oti_fid);
3749 osd->od_obj_area = d;
3752 result = PTR_ERR(d);
3753 osd->od_obj_area = NULL;
3760 static const struct lu_object_operations osd_lu_obj_ops = {
3761 .loo_object_init = osd_object_init,
3762 .loo_object_delete = osd_object_delete,
3763 .loo_object_release = osd_object_release,
3764 .loo_object_free = osd_object_free,
3765 .loo_object_print = osd_object_print,
3766 .loo_object_invariant = osd_object_invariant
3769 static const struct lu_device_operations osd_lu_ops = {
3770 .ldo_object_alloc = osd_object_alloc,
3771 .ldo_process_config = osd_process_config,
3772 .ldo_recovery_complete = osd_recovery_complete,
3773 .ldo_prepare = osd_prepare,
3776 static const struct lu_device_type_operations osd_device_type_ops = {
3777 .ldto_init = osd_type_init,
3778 .ldto_fini = osd_type_fini,
3780 .ldto_start = osd_type_start,
3781 .ldto_stop = osd_type_stop,
3783 .ldto_device_alloc = osd_device_alloc,
3784 .ldto_device_free = osd_device_free,
3786 .ldto_device_init = osd_device_init,
3787 .ldto_device_fini = osd_device_fini
3790 static struct lu_device_type osd_device_type = {
3791 .ldt_tags = LU_DEVICE_DT,
3792 .ldt_name = LUSTRE_OSD_NAME,
3793 .ldt_ops = &osd_device_type_ops,
3794 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3798 * lprocfs legacy support.
3800 static struct obd_ops osd_obd_device_ops = {
3801 .o_owner = THIS_MODULE
3804 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3805 .llod_name = remote_obj_dir,
3806 .llod_oid = OSD_REM_OBJ_DIR_OID,
3808 .llod_feat = &dt_directory_features,
3811 static int __init osd_mod_init(void)
3813 struct lprocfs_static_vars lvars;
3816 llo_local_obj_register(&llod_osd_rem_obj_dir);
3817 lprocfs_osd_init_vars(&lvars);
3818 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3819 LUSTRE_OSD_NAME, &osd_device_type);
3822 static void __exit osd_mod_exit(void)
3824 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3825 class_unregister_type(LUSTRE_OSD_NAME);
3828 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3829 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3830 MODULE_LICENSE("GPL");
3832 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);