1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/osd/osd_handler.c
41 * Top-level entry points into osd module
43 * Author: Nikita Danilov <nikita@clusterfs.com>
44 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
48 # define EXPORT_SYMTAB
50 #define DEBUG_SUBSYSTEM S_MDS
52 #include <linux/module.h>
54 /* LUSTRE_VERSION_CODE */
55 #include <lustre_ver.h>
56 /* prerequisite for linux/xattr.h */
57 #include <linux/types.h>
58 /* prerequisite for linux/xattr.h */
60 /* XATTR_{REPLACE,CREATE} */
61 #include <linux/xattr.h>
66 * struct OBD_{ALLOC,FREE}*()
69 #include <obd_support.h>
70 /* struct ptlrpc_thread */
71 #include <lustre_net.h>
74 #include <lustre_fid.h>
76 #include "osd_internal.h"
79 /* llo_* api support */
80 #include <md_object.h>
82 static const char dot[] = ".";
83 static const char dotdot[] = "..";
84 static const char remote_obj_dir[] = "REM_OBJ_DIR";
86 struct osd_directory {
87 struct iam_container od_container;
88 struct iam_descr od_descr;
92 struct dt_object oo_dt;
94 * Inode for file system object represented by this osd_object. This
95 * inode is pinned for the whole duration of lu_object life.
97 * Not modified concurrently (either setup early during object
98 * creation, or assigned by osd_object_create() under write lock).
100 struct inode *oo_inode;
102 * to protect index ops.
104 cfs_rw_semaphore_t oo_ext_idx_sem;
105 cfs_rw_semaphore_t oo_sem;
106 struct osd_directory *oo_dir;
107 /** protects inode attributes. */
108 cfs_spinlock_t oo_guard;
110 * Following two members are used to indicate the presence of dot and
111 * dotdot in the given directory. This is required for interop mode
114 int oo_compat_dot_created;
115 int oo_compat_dotdot_created;
117 const struct lu_env *oo_owner;
118 #ifdef CONFIG_LOCKDEP
119 struct lockdep_map oo_dep_map;
123 static const struct lu_object_operations osd_lu_obj_ops;
124 static const struct lu_device_operations osd_lu_ops;
125 static struct lu_context_key osd_key;
126 static const struct dt_object_operations osd_obj_ops;
127 static const struct dt_object_operations osd_obj_ea_ops;
128 static const struct dt_body_operations osd_body_ops;
129 static const struct dt_index_operations osd_index_iam_ops;
130 static const struct dt_index_operations osd_index_ea_ops;
133 struct thandle ot_super;
135 struct journal_callback ot_jcb;
136 /* Link to the device, for debugging. */
137 struct lu_ref_link *ot_dev_link;
139 #if OSD_THANDLE_STATS
140 /** time when this handle was allocated */
141 cfs_time_t oth_alloced;
143 /** time when this thanle was started */
144 cfs_time_t oth_started;
151 static int lu_device_is_osd(const struct lu_device *d)
153 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
156 static struct osd_device *osd_dt_dev(const struct dt_device *d)
158 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
159 return container_of0(d, struct osd_device, od_dt_dev);
162 static struct osd_device *osd_dev(const struct lu_device *d)
164 LASSERT(lu_device_is_osd(d));
165 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
168 static struct osd_device *osd_obj2dev(const struct osd_object *o)
170 return osd_dev(o->oo_dt.do_lu.lo_dev);
173 static struct super_block *osd_sb(const struct osd_device *dev)
175 return dev->od_mount->lmi_mnt->mnt_sb;
178 static int osd_object_is_root(const struct osd_object *obj)
180 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
183 static struct osd_object *osd_obj(const struct lu_object *o)
185 LASSERT(lu_device_is_osd(o->lo_dev));
186 return container_of0(o, struct osd_object, oo_dt.do_lu);
189 static struct osd_object *osd_dt_obj(const struct dt_object *d)
191 return osd_obj(&d->do_lu);
194 static struct lu_device *osd2lu_dev(struct osd_device *osd)
196 return &osd->od_dt_dev.dd_lu_dev;
199 static journal_t *osd_journal(const struct osd_device *dev)
201 return LDISKFS_SB(osd_sb(dev))->s_journal;
204 static int osd_has_index(const struct osd_object *obj)
206 return obj->oo_dt.do_index_ops != NULL;
209 static int osd_object_invariant(const struct lu_object *l)
211 return osd_invariant(osd_obj(l));
214 #ifdef HAVE_QUOTA_SUPPORT
216 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
218 struct md_ucred *uc = md_ucred(env);
223 save->oc_uid = current_fsuid();
224 save->oc_gid = current_fsgid();
225 save->oc_cap = current_cap();
226 if ((tc = prepare_creds())) {
227 tc->fsuid = uc->mu_fsuid;
228 tc->fsgid = uc->mu_fsgid;
231 /* XXX not suboptimal */
232 cfs_curproc_cap_unpack(uc->mu_cap);
236 osd_pop_ctxt(struct osd_ctxt *save)
240 if ((tc = prepare_creds())) {
241 tc->fsuid = save->oc_uid;
242 tc->fsgid = save->oc_gid;
243 tc->cap_effective = save->oc_cap;
249 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
251 return lu_context_key_get(&env->le_ctx, &osd_key);
255 * Concurrency: doesn't matter
257 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
259 return osd_oti_get(env)->oti_r_locks > 0;
263 * Concurrency: doesn't matter
265 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
267 struct osd_thread_info *oti = osd_oti_get(env);
268 return oti->oti_w_locks > 0 && o->oo_owner == env;
272 * Concurrency: doesn't access mutable data
274 static int osd_root_get(const struct lu_env *env,
275 struct dt_device *dev, struct lu_fid *f)
279 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
280 LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
285 * OSD object methods.
289 * Concurrency: no concurrent access is possible that early in object
292 static struct lu_object *osd_object_alloc(const struct lu_env *env,
293 const struct lu_object_header *hdr,
296 struct osd_object *mo;
302 l = &mo->oo_dt.do_lu;
303 dt_object_init(&mo->oo_dt, NULL, d);
304 if (osd_dev(d)->od_iop_mode)
305 mo->oo_dt.do_ops = &osd_obj_ea_ops;
307 mo->oo_dt.do_ops = &osd_obj_ops;
309 l->lo_ops = &osd_lu_obj_ops;
310 cfs_init_rwsem(&mo->oo_sem);
311 cfs_init_rwsem(&mo->oo_ext_idx_sem);
312 cfs_spin_lock_init(&mo->oo_guard);
319 * retrieve object from backend ext fs.
321 static struct inode *osd_iget(struct osd_thread_info *info,
322 struct osd_device *dev,
323 const struct osd_inode_id *id)
325 struct inode *inode = NULL;
327 #ifdef HAVE_EXT4_LDISKFS
328 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
330 /* Newer kernels return an error instead of a NULL pointer */
333 inode = iget(osd_sb(dev), id->oii_ino);
336 CERROR("no inode\n");
337 inode = ERR_PTR(-EACCES);
338 } else if (id->oii_gen != OSD_OII_NOGEN &&
339 inode->i_generation != id->oii_gen) {
341 inode = ERR_PTR(-ESTALE);
342 } else if (inode->i_nlink == 0) {
343 /* due to parallel readdir and unlink,
344 * we can have dead inode here. */
345 CWARN("stale inode\n");
346 make_bad_inode(inode);
348 inode = ERR_PTR(-ESTALE);
349 } else if (is_bad_inode(inode)) {
350 CERROR("bad inode %lx\n",inode->i_ino);
352 inode = ERR_PTR(-ENOENT);
354 /* Do not update file c/mtime in ldiskfs.
355 * NB: we don't have any lock to protect this because we don't
356 * have reference on osd_object now, but contention with
357 * another lookup + attr_set can't happen in the tiny window
358 * between if (...) and set S_NOCMTIME. */
359 if (!(inode->i_flags & S_NOCMTIME))
360 inode->i_flags |= S_NOCMTIME;
365 static int osd_fid_lookup(const struct lu_env *env,
366 struct osd_object *obj, const struct lu_fid *fid)
368 struct osd_thread_info *info;
369 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
370 struct osd_device *dev;
371 struct osd_inode_id *id;
376 LINVRNT(osd_invariant(obj));
377 LASSERT(obj->oo_inode == NULL);
378 LASSERT(fid_is_sane(fid) || osd_fid_is_root(fid));
380 * This assertion checks that osd layer sees only local
381 * fids. Unfortunately it is somewhat expensive (does a
382 * cache-lookup). Disabling it for production/acceptance-testing.
384 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
388 info = osd_oti_get(env);
393 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
396 result = osd_oi_lookup(info, oi, fid, id);
398 inode = osd_iget(info, dev, id);
399 if (!IS_ERR(inode)) {
400 obj->oo_inode = inode;
401 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
402 if (dev->od_iop_mode) {
403 obj->oo_compat_dot_created = 1;
404 obj->oo_compat_dotdot_created = 1;
409 * If fid wasn't found in oi, inode-less object is
410 * created, for which lu_object_exists() returns
411 * false. This is used in a (frequent) case when
412 * objects are created as locking anchors or
413 * place holders for objects yet to be created.
415 result = PTR_ERR(inode);
416 } else if (result == -ENOENT)
418 LINVRNT(osd_invariant(obj));
424 * Concurrency: shouldn't matter.
426 static void osd_object_init0(struct osd_object *obj)
428 LASSERT(obj->oo_inode != NULL);
429 obj->oo_dt.do_body_ops = &osd_body_ops;
430 obj->oo_dt.do_lu.lo_header->loh_attr |=
431 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
435 * Concurrency: no concurrent access is possible that early in object
438 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
439 const struct lu_object_conf *unused)
441 struct osd_object *obj = osd_obj(l);
444 LINVRNT(osd_invariant(obj));
446 result = osd_fid_lookup(env, obj, lu_object_fid(l));
448 if (obj->oo_inode != NULL)
449 osd_object_init0(obj);
451 LINVRNT(osd_invariant(obj));
456 * Concurrency: no concurrent access is possible that late in object
459 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
461 struct osd_object *obj = osd_obj(l);
463 LINVRNT(osd_invariant(obj));
465 dt_object_fini(&obj->oo_dt);
472 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
473 const struct iam_container *bag)
475 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
476 osd_oti_get(env)->oti_it_ipd);
479 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
480 const struct iam_container *bag)
482 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
483 osd_oti_get(env)->oti_idx_ipd);
486 static void osd_ipd_put(const struct lu_env *env,
487 const struct iam_container *bag,
488 struct iam_path_descr *ipd)
490 bag->ic_descr->id_ops->id_ipd_free(ipd);
494 * Concurrency: no concurrent access is possible that late in object
497 static void osd_index_fini(struct osd_object *o)
499 struct iam_container *bag;
501 if (o->oo_dir != NULL) {
502 bag = &o->oo_dir->od_container;
503 if (o->oo_inode != NULL) {
504 if (bag->ic_object == o->oo_inode)
505 iam_container_fini(bag);
507 OBD_FREE_PTR(o->oo_dir);
513 * Concurrency: no concurrent access is possible that late in object
514 * life-cycle (for all existing callers, that is. New callers have to provide
515 * their own locking.)
517 static int osd_inode_unlinked(const struct inode *inode)
519 return inode->i_nlink == 0;
523 OSD_TXN_OI_DELETE_CREDITS = 20,
524 OSD_TXN_INODE_DELETE_CREDITS = 20
531 #if OSD_THANDLE_STATS
533 * Set time when the handle is allocated
535 static void osd_th_alloced(struct osd_thandle *oth)
537 oth->oth_alloced = cfs_time_current();
541 * Set time when the handle started
543 static void osd_th_started(struct osd_thandle *oth)
545 oth->oth_started = cfs_time_current();
549 * Helper function to convert time interval to microseconds packed in
550 * long int (default time units for the counter in "stats" initialized
551 * by lu_time_init() )
553 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
557 cfs_duration_usec(cfs_time_sub(end, start), &val);
558 return val.tv_sec * 1000000 + val.tv_usec;
562 * Check whether the we deal with this handle for too long.
564 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
565 cfs_time_t alloced, cfs_time_t started,
568 cfs_time_t now = cfs_time_current();
570 LASSERT(dev != NULL);
572 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
573 interval_to_usec(alloced, started));
574 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
575 interval_to_usec(started, closed));
576 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
577 interval_to_usec(closed, now));
579 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
580 CWARN("transaction handle %p was open for too long: "
582 "alloced "CFS_TIME_T" ,"
583 "started "CFS_TIME_T" ,"
584 "closed "CFS_TIME_T"\n",
585 oth, now, alloced, started, closed);
586 libcfs_debug_dumpstack(NULL);
590 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
592 cfs_time_t __closed = cfs_time_current(); \
593 cfs_time_t __alloced = oth->oth_alloced; \
594 cfs_time_t __started = oth->oth_started; \
597 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
600 #else /* OSD_THANDLE_STATS */
602 #define osd_th_alloced(h) do {} while(0)
603 #define osd_th_started(h) do {} while(0)
604 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
606 #endif /* OSD_THANDLE_STATS */
609 * Concurrency: doesn't access mutable data.
611 static int osd_param_is_sane(const struct osd_device *dev,
612 const struct txn_param *param)
614 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
618 * Concurrency: shouldn't matter.
620 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
622 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
623 struct thandle *th = &oh->ot_super;
624 struct dt_device *dev = th->th_dev;
625 struct lu_device *lud = &dev->dd_lu_dev;
627 LASSERT(dev != NULL);
628 LASSERT(oh->ot_handle == NULL);
631 CERROR("transaction @0x%p commit error: %d\n", th, error);
633 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
635 * This od_env_for_commit is only for commit usage. see
638 lu_context_enter(&env->le_ctx);
639 dt_txn_hook_commit(env, th);
640 lu_context_exit(&env->le_ctx);
643 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
647 lu_context_exit(&th->th_ctx);
648 lu_context_fini(&th->th_ctx);
653 * Concurrency: shouldn't matter.
655 static struct thandle *osd_trans_start(const struct lu_env *env,
659 struct osd_device *dev = osd_dt_dev(d);
661 struct osd_thandle *oh;
667 hook_res = dt_txn_hook_start(env, d, p);
669 RETURN(ERR_PTR(hook_res));
671 if (osd_param_is_sane(dev, p)) {
672 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
674 struct osd_thread_info *oti = osd_oti_get(env);
677 * XXX temporary stuff. Some abstraction layer should
682 jh = ldiskfs_journal_start_sb(osd_sb(dev), p->tp_credits);
689 jh->h_sync = p->tp_sync;
690 lu_device_get(&d->dd_lu_dev);
691 oh->ot_dev_link = lu_ref_add
692 (&d->dd_lu_dev.ld_reference,
694 /* add commit callback */
695 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
696 lu_context_enter(&th->th_ctx);
697 osd_journal_callback_set(jh, osd_trans_commit_cb,
698 (struct journal_callback *)&oh->ot_jcb);
699 LASSERT(oti->oti_txns == 0);
700 LASSERT(oti->oti_r_locks == 0);
701 LASSERT(oti->oti_w_locks == 0);
708 th = ERR_PTR(-ENOMEM);
710 CERROR("Invalid transaction parameters\n");
711 th = ERR_PTR(-EINVAL);
718 * Concurrency: shouldn't matter.
720 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
723 struct osd_thandle *oh;
724 struct osd_thread_info *oti = osd_oti_get(env);
728 oh = container_of0(th, struct osd_thandle, ot_super);
729 if (oh->ot_handle != NULL) {
730 handle_t *hdl = oh->ot_handle;
732 LASSERT(oti->oti_txns == 1);
734 LASSERT(oti->oti_r_locks == 0);
735 LASSERT(oti->oti_w_locks == 0);
736 result = dt_txn_hook_stop(env, th);
738 CERROR("Failure in transaction hook: %d\n", result);
739 oh->ot_handle = NULL;
740 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
741 result = ldiskfs_journal_stop(hdl));
743 CERROR("Failure to stop transaction: %d\n", result);
749 * Concurrency: no concurrent access is possible that late in object
752 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
754 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
755 struct osd_device *osd = osd_obj2dev(obj);
756 struct osd_thread_info *oti = osd_oti_get(env);
757 struct txn_param *prm = &oti->oti_txn;
758 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
762 lu_env_init(env_del_obj, LCT_DT_THREAD);
763 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
764 OSD_TXN_INODE_DELETE_CREDITS);
765 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
767 result = osd_oi_delete(osd_oti_get(env_del_obj),
768 &osd->od_oi, fid, th);
769 osd_trans_stop(env_del_obj, th);
771 result = PTR_ERR(th);
773 lu_env_fini(env_del_obj);
778 * Called just before object is freed. Releases all resources except for
779 * object itself (that is released by osd_object_free()).
781 * Concurrency: no concurrent access is possible that late in object
784 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
786 struct osd_object *obj = osd_obj(l);
787 struct inode *inode = obj->oo_inode;
789 LINVRNT(osd_invariant(obj));
792 * If object is unlinked remove fid->ino mapping from object index.
799 if (osd_inode_unlinked(inode)) {
800 result = osd_inode_remove(env, obj);
802 LU_OBJECT_DEBUG(D_ERROR, env, l,
803 "Failed to cleanup: %d\n",
808 obj->oo_inode = NULL;
813 * Concurrency: ->loo_object_release() is called under site spin-lock.
815 static void osd_object_release(const struct lu_env *env,
818 struct osd_object *o = osd_obj(l);
820 LASSERT(!lu_object_is_dying(l->lo_header));
821 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
822 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
826 * Concurrency: shouldn't matter.
828 static int osd_object_print(const struct lu_env *env, void *cookie,
829 lu_printer_t p, const struct lu_object *l)
831 struct osd_object *o = osd_obj(l);
834 if (o->oo_dir != NULL)
835 d = o->oo_dir->od_container.ic_descr;
838 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
840 o->oo_inode ? o->oo_inode->i_ino : 0UL,
841 o->oo_inode ? o->oo_inode->i_generation : 0,
842 d ? d->id_ops->id_name : "plain");
846 * Concurrency: shouldn't matter.
848 int osd_statfs(const struct lu_env *env, struct dt_device *d,
851 struct osd_device *osd = osd_dt_dev(d);
852 struct super_block *sb = osd_sb(osd);
855 cfs_spin_lock(&osd->od_osfs_lock);
857 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
858 result = ll_do_statfs(sb, &osd->od_kstatfs);
859 if (likely(result == 0)) /* N.B. statfs can't really fail */
860 osd->od_osfs_age = cfs_time_current_64();
863 if (likely(result == 0))
864 *sfs = osd->od_kstatfs;
865 cfs_spin_unlock(&osd->od_osfs_lock);
871 * Concurrency: doesn't access mutable data.
873 static void osd_conf_get(const struct lu_env *env,
874 const struct dt_device *dev,
875 struct dt_device_param *param)
878 * XXX should be taken from not-yet-existing fs abstraction layer.
880 param->ddp_max_name_len = LDISKFS_NAME_LEN;
881 param->ddp_max_nlink = LDISKFS_LINK_MAX;
882 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
886 * Helper function to get and fill the buffer with input values.
888 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
892 buf = &osd_oti_get(env)->oti_buf;
899 * Concurrency: shouldn't matter.
901 static int osd_sync(const struct lu_env *env, struct dt_device *d)
903 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
904 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
908 * Start commit for OSD device.
910 * An implementation of dt_commit_async method for OSD device.
911 * Asychronously starts underlayng fs sync and thereby a transaction
914 * \param env environment
917 * \see dt_device_operations
919 static int osd_commit_async(const struct lu_env *env,
922 struct super_block *s = osd_sb(osd_dt_dev(d));
925 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
926 RETURN(s->s_op->sync_fs(s, 0));
930 * Concurrency: shouldn't matter.
932 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
934 static void osd_ro(const struct lu_env *env, struct dt_device *d)
938 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
940 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
941 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
947 * Concurrency: serialization provided by callers.
949 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
950 int mode, unsigned long timeout, __u32 alg,
951 struct lustre_capa_key *keys)
953 struct osd_device *dev = osd_dt_dev(d);
956 dev->od_fl_capa = mode;
957 dev->od_capa_timeout = timeout;
958 dev->od_capa_alg = alg;
959 dev->od_capa_keys = keys;
964 * Concurrency: serialization provided by callers.
966 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
967 struct dt_quota_ctxt *ctxt, void *data)
969 struct obd_device *obd = (void *)ctxt;
970 struct vfsmount *mnt = (struct vfsmount *)data;
973 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
974 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
975 obd->obd_lvfs_ctxt.pwdmnt = mnt;
976 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
977 obd->obd_lvfs_ctxt.fs = get_ds();
983 * Note: we do not count into QUOTA here.
984 * If we mount with --data_journal we may need more.
986 static const int osd_dto_credits_noquota[DTO_NR] = {
989 * INDEX_EXTRA_TRANS_BLOCKS(8) +
990 * SINGLEDATA_TRANS_BLOCKS(8)
991 * XXX Note: maybe iam need more, since iam have more level than
994 [DTO_INDEX_INSERT] = 16,
995 [DTO_INDEX_DELETE] = 16,
999 [DTO_IDNEX_UPDATE] = 16,
1001 * Create a object. The same as create object in EXT3.
1002 * DATA_TRANS_BLOCKS(14) +
1003 * INDEX_EXTRA_BLOCKS(8) +
1004 * 3(inode bits, groups, GDT)
1006 [DTO_OBJECT_CREATE] = 25,
1010 [DTO_OBJECT_DELETE] = 25,
1013 * 3(inode bits, group, GDT)
1015 [DTO_ATTR_SET_BASE] = 3,
1017 * Xattr set. The same as xattr of EXT3.
1018 * DATA_TRANS_BLOCKS(14)
1019 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1020 * are also counted in. Do not know why?
1022 [DTO_XATTR_SET] = 14,
1025 * creadits for inode change during write.
1027 [DTO_WRITE_BASE] = 3,
1029 * credits for single block write.
1031 [DTO_WRITE_BLOCK] = 14,
1033 * Attr set credits for chown.
1034 * This is extra credits for setattr, and it is null without quota
1036 [DTO_ATTR_SET_CHOWN]= 0
1040 * Note: we count into QUOTA here.
1041 * If we mount with --data_journal we may need more.
1043 static const int osd_dto_credits_quota[DTO_NR] = {
1045 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1046 * SINGLEDATA_TRANS_BLOCKS(8) +
1047 * 2 * QUOTA_TRANS_BLOCKS(2)
1049 [DTO_INDEX_INSERT] = 20,
1051 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1052 * SINGLEDATA_TRANS_BLOCKS(8) +
1053 * 2 * QUOTA_TRANS_BLOCKS(2)
1055 [DTO_INDEX_DELETE] = 20,
1059 [DTO_IDNEX_UPDATE] = 16,
1061 * Create a object. Same as create object in EXT3 filesystem.
1062 * DATA_TRANS_BLOCKS(16) +
1063 * INDEX_EXTRA_BLOCKS(8) +
1064 * 3(inode bits, groups, GDT) +
1065 * 2 * QUOTA_INIT_BLOCKS(25)
1067 [DTO_OBJECT_CREATE] = 77,
1070 * DATA_TRANS_BLOCKS(16) +
1071 * INDEX_EXTRA_BLOCKS(8) +
1072 * 3(inode bits, groups, GDT) +
1075 [DTO_OBJECT_DELETE] = 27,
1078 * 3 (inode bit, group, GDT) +
1080 [DTO_ATTR_SET_BASE] = 3,
1082 * Xattr set. The same as xattr of EXT3.
1083 * DATA_TRANS_BLOCKS(16)
1084 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
1085 * also counted in. Do not know why?
1087 [DTO_XATTR_SET] = 16,
1090 * creadits for inode change during write.
1092 [DTO_WRITE_BASE] = 3,
1094 * credits for single block write.
1096 [DTO_WRITE_BLOCK] = 16,
1098 * Attr set credits for chown.
1099 * It is added to already set setattr credits
1100 * 2 * QUOTA_INIT_BLOCKS(25) +
1101 * 2 * QUOTA_DEL_BLOCKS(9)
1103 [DTO_ATTR_SET_CHOWN]= 68,
1106 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1109 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1110 ARRAY_SIZE(osd_dto_credits_quota));
1111 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1112 #ifdef HAVE_QUOTA_SUPPORT
1113 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1114 return osd_dto_credits_quota[op];
1117 return osd_dto_credits_noquota[op];
1120 static const struct dt_device_operations osd_dt_ops = {
1121 .dt_root_get = osd_root_get,
1122 .dt_statfs = osd_statfs,
1123 .dt_trans_start = osd_trans_start,
1124 .dt_trans_stop = osd_trans_stop,
1125 .dt_conf_get = osd_conf_get,
1126 .dt_sync = osd_sync,
1128 .dt_commit_async = osd_commit_async,
1129 .dt_credit_get = osd_credit_get,
1130 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1131 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1134 static void osd_object_read_lock(const struct lu_env *env,
1135 struct dt_object *dt, unsigned role)
1137 struct osd_object *obj = osd_dt_obj(dt);
1138 struct osd_thread_info *oti = osd_oti_get(env);
1140 LINVRNT(osd_invariant(obj));
1142 LASSERT(obj->oo_owner != env);
1143 cfs_down_read_nested(&obj->oo_sem, role);
1145 LASSERT(obj->oo_owner == NULL);
1149 static void osd_object_write_lock(const struct lu_env *env,
1150 struct dt_object *dt, unsigned role)
1152 struct osd_object *obj = osd_dt_obj(dt);
1153 struct osd_thread_info *oti = osd_oti_get(env);
1155 LINVRNT(osd_invariant(obj));
1157 LASSERT(obj->oo_owner != env);
1158 cfs_down_write_nested(&obj->oo_sem, role);
1160 LASSERT(obj->oo_owner == NULL);
1161 obj->oo_owner = env;
1165 static void osd_object_read_unlock(const struct lu_env *env,
1166 struct dt_object *dt)
1168 struct osd_object *obj = osd_dt_obj(dt);
1169 struct osd_thread_info *oti = osd_oti_get(env);
1171 LINVRNT(osd_invariant(obj));
1173 LASSERT(oti->oti_r_locks > 0);
1175 cfs_up_read(&obj->oo_sem);
1178 static void osd_object_write_unlock(const struct lu_env *env,
1179 struct dt_object *dt)
1181 struct osd_object *obj = osd_dt_obj(dt);
1182 struct osd_thread_info *oti = osd_oti_get(env);
1184 LINVRNT(osd_invariant(obj));
1186 LASSERT(obj->oo_owner == env);
1187 LASSERT(oti->oti_w_locks > 0);
1189 obj->oo_owner = NULL;
1190 cfs_up_write(&obj->oo_sem);
1193 static int osd_object_write_locked(const struct lu_env *env,
1194 struct dt_object *dt)
1196 struct osd_object *obj = osd_dt_obj(dt);
1198 LINVRNT(osd_invariant(obj));
1200 return obj->oo_owner == env;
1203 static int capa_is_sane(const struct lu_env *env,
1204 struct osd_device *dev,
1205 struct lustre_capa *capa,
1206 struct lustre_capa_key *keys)
1208 struct osd_thread_info *oti = osd_oti_get(env);
1209 struct lustre_capa *tcapa = &oti->oti_capa;
1210 struct obd_capa *oc;
1214 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1216 if (capa_is_expired(oc)) {
1217 DEBUG_CAPA(D_ERROR, capa, "expired");
1224 if (capa_is_expired_sec(capa)) {
1225 DEBUG_CAPA(D_ERROR, capa, "expired");
1229 cfs_spin_lock(&capa_lock);
1230 for (i = 0; i < 2; i++) {
1231 if (keys[i].lk_keyid == capa->lc_keyid) {
1232 oti->oti_capa_key = keys[i];
1236 cfs_spin_unlock(&capa_lock);
1239 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1243 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1247 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1248 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1252 oc = capa_add(dev->od_capa_hash, capa);
1258 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1259 struct lustre_capa *capa, __u64 opc)
1261 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1262 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1263 struct md_capainfo *ci;
1266 if (!dev->od_fl_capa)
1269 if (capa == BYPASS_CAPA)
1272 ci = md_capainfo(env);
1276 if (ci->mc_auth == LC_ID_NONE)
1280 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1284 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1285 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1290 if (!capa_opc_supported(capa, opc)) {
1291 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1295 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1296 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1303 static struct timespec *osd_inode_time(const struct lu_env *env,
1304 struct inode *inode, __u64 seconds)
1306 struct osd_thread_info *oti = osd_oti_get(env);
1307 struct timespec *t = &oti->oti_time;
1309 t->tv_sec = seconds;
1311 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1316 static void osd_inode_getattr(const struct lu_env *env,
1317 struct inode *inode, struct lu_attr *attr)
1319 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1320 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1321 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1323 attr->la_atime = LTIME_S(inode->i_atime);
1324 attr->la_mtime = LTIME_S(inode->i_mtime);
1325 attr->la_ctime = LTIME_S(inode->i_ctime);
1326 attr->la_mode = inode->i_mode;
1327 attr->la_size = i_size_read(inode);
1328 attr->la_blocks = inode->i_blocks;
1329 attr->la_uid = inode->i_uid;
1330 attr->la_gid = inode->i_gid;
1331 attr->la_flags = LDISKFS_I(inode)->i_flags;
1332 attr->la_nlink = inode->i_nlink;
1333 attr->la_rdev = inode->i_rdev;
1334 attr->la_blksize = ll_inode_blksize(inode);
1335 attr->la_blkbits = inode->i_blkbits;
1338 static int osd_attr_get(const struct lu_env *env,
1339 struct dt_object *dt,
1340 struct lu_attr *attr,
1341 struct lustre_capa *capa)
1343 struct osd_object *obj = osd_dt_obj(dt);
1345 LASSERT(dt_object_exists(dt));
1346 LINVRNT(osd_invariant(obj));
1348 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1351 cfs_spin_lock(&obj->oo_guard);
1352 osd_inode_getattr(env, obj->oo_inode, attr);
1353 cfs_spin_unlock(&obj->oo_guard);
1357 static int osd_inode_setattr(const struct lu_env *env,
1358 struct inode *inode, const struct lu_attr *attr)
1362 bits = attr->la_valid;
1364 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1366 #ifdef HAVE_QUOTA_SUPPORT
1367 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1368 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1369 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1375 iattr.ia_valid |= ATTR_UID;
1377 iattr.ia_valid |= ATTR_GID;
1378 iattr.ia_uid = attr->la_uid;
1379 iattr.ia_gid = attr->la_gid;
1380 osd_push_ctxt(env, save);
1381 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1388 if (bits & LA_ATIME)
1389 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1390 if (bits & LA_CTIME)
1391 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1392 if (bits & LA_MTIME)
1393 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1394 if (bits & LA_SIZE) {
1395 LDISKFS_I(inode)->i_disksize = attr->la_size;
1396 i_size_write(inode, attr->la_size);
1400 /* OSD should not change "i_blocks" which is used by quota.
1401 * "i_blocks" should be changed by ldiskfs only. */
1402 if (bits & LA_BLOCKS)
1403 inode->i_blocks = attr->la_blocks;
1406 inode->i_mode = (inode->i_mode & S_IFMT) |
1407 (attr->la_mode & ~S_IFMT);
1409 inode->i_uid = attr->la_uid;
1411 inode->i_gid = attr->la_gid;
1412 if (bits & LA_NLINK)
1413 inode->i_nlink = attr->la_nlink;
1415 inode->i_rdev = attr->la_rdev;
1417 if (bits & LA_FLAGS) {
1418 /* always keep S_NOCMTIME */
1419 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1425 static int osd_attr_set(const struct lu_env *env,
1426 struct dt_object *dt,
1427 const struct lu_attr *attr,
1428 struct thandle *handle,
1429 struct lustre_capa *capa)
1431 struct osd_object *obj = osd_dt_obj(dt);
1434 LASSERT(handle != NULL);
1435 LASSERT(dt_object_exists(dt));
1436 LASSERT(osd_invariant(obj));
1438 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1441 cfs_spin_lock(&obj->oo_guard);
1442 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1443 cfs_spin_unlock(&obj->oo_guard);
1446 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1453 * XXX temporary solution.
1455 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1456 struct lu_attr *attr, struct thandle *th)
1461 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1462 struct lu_attr *attr, struct thandle *th)
1464 osd_object_init0(obj);
1465 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1466 unlock_new_inode(obj->oo_inode);
1470 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1471 struct osd_object *obj,
1475 struct osd_thread_info *info = osd_oti_get(env);
1476 struct dentry *child_dentry = &info->oti_child_dentry;
1477 struct dentry *obj_dentry = &info->oti_obj_dentry;
1479 obj_dentry->d_inode = obj->oo_inode;
1480 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1481 obj_dentry->d_name.hash = 0;
1483 child_dentry->d_name.hash = 0;
1484 child_dentry->d_parent = obj_dentry;
1485 child_dentry->d_name.name = name;
1486 child_dentry->d_name.len = namelen;
1487 return child_dentry;
1491 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1493 struct dt_allocation_hint *hint,
1497 struct osd_device *osd = osd_obj2dev(obj);
1498 struct osd_thandle *oth;
1499 struct dt_object *parent;
1500 struct inode *inode;
1501 #ifdef HAVE_QUOTA_SUPPORT
1502 struct osd_ctxt *save = &info->oti_ctxt;
1505 LINVRNT(osd_invariant(obj));
1506 LASSERT(obj->oo_inode == NULL);
1508 oth = container_of(th, struct osd_thandle, ot_super);
1509 LASSERT(oth->ot_handle->h_transaction != NULL);
1511 if (hint && hint->dah_parent)
1512 parent = hint->dah_parent;
1514 parent = osd->od_obj_area;
1516 LASSERT(parent != NULL);
1517 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1519 #ifdef HAVE_QUOTA_SUPPORT
1520 osd_push_ctxt(info->oti_env, save);
1522 inode = ldiskfs_create_inode(oth->ot_handle,
1523 osd_dt_obj(parent)->oo_inode, mode);
1524 #ifdef HAVE_QUOTA_SUPPORT
1527 if (!IS_ERR(inode)) {
1528 /* Do not update file c/mtime in ldiskfs.
1529 * NB: don't need any lock because no contention at this
1531 inode->i_flags |= S_NOCMTIME;
1532 obj->oo_inode = inode;
1535 result = PTR_ERR(inode);
1536 LINVRNT(osd_invariant(obj));
1544 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1545 struct lu_attr *attr,
1546 struct dt_allocation_hint *hint,
1547 struct dt_object_format *dof,
1551 struct osd_thandle *oth;
1552 struct osd_device *osd = osd_obj2dev(obj);
1553 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1555 LASSERT(S_ISDIR(attr->la_mode));
1557 oth = container_of(th, struct osd_thandle, ot_super);
1558 LASSERT(oth->ot_handle->h_transaction != NULL);
1559 result = osd_mkfile(info, obj, mode, hint, th);
1560 if (result == 0 && osd->od_iop_mode == 0) {
1561 LASSERT(obj->oo_inode != NULL);
1563 * XXX uh-oh... call low-level iam function directly.
1566 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1567 sizeof (struct osd_fid_pack),
1573 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1574 struct lu_attr *attr,
1575 struct dt_allocation_hint *hint,
1576 struct dt_object_format *dof,
1580 struct osd_thandle *oth;
1581 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1583 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1585 LASSERT(S_ISREG(attr->la_mode));
1587 oth = container_of(th, struct osd_thandle, ot_super);
1588 LASSERT(oth->ot_handle->h_transaction != NULL);
1590 result = osd_mkfile(info, obj, mode, hint, th);
1592 LASSERT(obj->oo_inode != NULL);
1593 if (feat->dif_flags & DT_IND_VARKEY)
1594 result = iam_lvar_create(obj->oo_inode,
1595 feat->dif_keysize_max,
1597 feat->dif_recsize_max,
1600 result = iam_lfix_create(obj->oo_inode,
1601 feat->dif_keysize_max,
1603 feat->dif_recsize_max,
1610 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1611 struct lu_attr *attr,
1612 struct dt_allocation_hint *hint,
1613 struct dt_object_format *dof,
1616 LASSERT(S_ISREG(attr->la_mode));
1617 return osd_mkfile(info, obj, (attr->la_mode &
1618 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1621 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1622 struct lu_attr *attr,
1623 struct dt_allocation_hint *hint,
1624 struct dt_object_format *dof,
1627 LASSERT(S_ISLNK(attr->la_mode));
1628 return osd_mkfile(info, obj, (attr->la_mode &
1629 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1632 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1633 struct lu_attr *attr,
1634 struct dt_allocation_hint *hint,
1635 struct dt_object_format *dof,
1638 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1641 LINVRNT(osd_invariant(obj));
1642 LASSERT(obj->oo_inode == NULL);
1643 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1644 S_ISFIFO(mode) || S_ISSOCK(mode));
1646 result = osd_mkfile(info, obj, mode, hint, th);
1648 LASSERT(obj->oo_inode != NULL);
1649 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1651 LINVRNT(osd_invariant(obj));
1655 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1657 struct dt_allocation_hint *hint,
1658 struct dt_object_format *dof,
1661 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1663 osd_obj_type_f result;
1679 result = osd_mk_index;
1690 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1691 struct dt_object *parent, cfs_umode_t child_mode)
1695 memset(ah, 0, sizeof(*ah));
1696 ah->dah_parent = parent;
1697 ah->dah_mode = child_mode;
1701 * Helper function for osd_object_create()
1703 * \retval 0, on success
1705 static int __osd_object_create(struct osd_thread_info *info,
1706 struct osd_object *obj, struct lu_attr *attr,
1707 struct dt_allocation_hint *hint,
1708 struct dt_object_format *dof,
1714 result = osd_create_pre(info, obj, attr, th);
1716 result = osd_create_type_f(dof->dof_type)(info, obj,
1717 attr, hint, dof, th);
1719 result = osd_create_post(info, obj, attr, th);
1725 * Helper function for osd_object_create()
1727 * \retval 0, on success
1729 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1730 const struct lu_fid *fid, struct thandle *th)
1732 struct osd_thread_info *info = osd_oti_get(env);
1733 struct osd_inode_id *id = &info->oti_id;
1734 struct osd_device *osd = osd_obj2dev(obj);
1735 struct md_ucred *uc = md_ucred(env);
1737 LASSERT(obj->oo_inode != NULL);
1738 LASSERT(uc != NULL);
1740 id->oii_ino = obj->oo_inode->i_ino;
1741 id->oii_gen = obj->oo_inode->i_generation;
1743 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1744 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1747 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1748 struct lu_attr *attr,
1749 struct dt_allocation_hint *hint,
1750 struct dt_object_format *dof,
1753 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1754 struct osd_object *obj = osd_dt_obj(dt);
1755 struct osd_thread_info *info = osd_oti_get(env);
1760 LINVRNT(osd_invariant(obj));
1761 LASSERT(!dt_object_exists(dt));
1762 LASSERT(osd_write_locked(env, obj));
1763 LASSERT(th != NULL);
1765 result = __osd_object_create(info, obj, attr, hint, dof, th);
1767 result = __osd_oi_insert(env, obj, fid, th);
1769 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1770 LASSERT(osd_invariant(obj));
1775 * Helper function for osd_xattr_set()
1777 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1778 const struct lu_buf *buf, const char *name, int fl)
1780 struct osd_object *obj = osd_dt_obj(dt);
1781 struct inode *inode = obj->oo_inode;
1782 struct osd_thread_info *info = osd_oti_get(env);
1783 struct dentry *dentry = &info->oti_child_dentry;
1787 LASSERT(dt_object_exists(dt));
1788 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1789 LASSERT(osd_write_locked(env, obj));
1791 if (fl & LU_XATTR_REPLACE)
1792 fs_flags |= XATTR_REPLACE;
1794 if (fl & LU_XATTR_CREATE)
1795 fs_flags |= XATTR_CREATE;
1797 dentry->d_inode = inode;
1798 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1799 buf->lb_len, fs_flags);
1804 * Put the fid into lustre_mdt_attrs, and then place the structure
1805 * inode's ea. This fid should not be altered during the life time
1808 * \retval +ve, on success
1809 * \retval -ve, on error
1811 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1813 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1814 const struct lu_fid *fid)
1816 struct osd_thread_info *info = osd_oti_get(env);
1817 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1819 lustre_lma_init(mdt_attrs, fid);
1820 lustre_lma_swab(mdt_attrs);
1821 return __osd_xattr_set(env, dt,
1822 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1823 XATTR_NAME_LMA, LU_XATTR_CREATE);
1828 * Helper function to form igif
1830 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1833 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1837 * Helper function to pack the fid, ldiskfs stores fid in packed format.
1839 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1840 struct lu_fid *befider)
1842 fid_cpu_to_be(befider, (struct lu_fid *)fid);
1843 memcpy(pack->fp_area, befider, sizeof(*befider));
1844 pack->fp_len = sizeof(*befider) + 1;
1848 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1849 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1850 * To have compatilibility with 1.8 ldiskfs driver we need to have
1851 * magic number at start of fid data.
1852 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1855 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1856 const struct dt_rec *fid)
1858 param->edp_magic = LDISKFS_LUFID_MAGIC;
1859 param->edp_len = sizeof(struct lu_fid) + 1;
1861 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1862 (struct lu_fid *)fid);
1865 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1870 switch (pack->fp_len) {
1871 case sizeof *fid + 1:
1872 memcpy(fid, pack->fp_area, sizeof *fid);
1873 fid_be_to_cpu(fid, fid);
1876 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1883 * Try to read the fid from inode ea into dt_rec, if return value
1884 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1886 * \param fid object fid.
1888 * \retval 0 on success
1890 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1891 __u32 ino, struct lu_fid *fid)
1893 struct osd_thread_info *info = osd_oti_get(env);
1894 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1895 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1896 struct dentry *dentry = &info->oti_child_dentry;
1897 struct osd_inode_id *id = &info->oti_id;
1898 struct osd_device *dev;
1899 struct inode *inode;
1903 dev = osd_dev(ldev);
1906 id->oii_gen = OSD_OII_NOGEN;
1908 inode = osd_iget(info, dev, id);
1909 if (IS_ERR(inode)) {
1910 rc = PTR_ERR(inode);
1913 dentry->d_inode = inode;
1915 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1916 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1919 /* Check LMA compatibility */
1921 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
1922 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1923 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
1924 ~LMA_INCOMPAT_SUPP);
1929 lustre_lma_swab(mdt_attrs);
1930 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
1932 } else if (rc == -ENODATA) {
1933 osd_igif_get(env, inode, fid);
1942 * OSD layer object create function for interoperability mode (b11826).
1943 * This is mostly similar to osd_object_create(). Only difference being, fid is
1944 * inserted into inode ea here.
1946 * \retval 0, on success
1947 * \retval -ve, on error
1949 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1950 struct lu_attr *attr,
1951 struct dt_allocation_hint *hint,
1952 struct dt_object_format *dof,
1955 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1956 struct osd_object *obj = osd_dt_obj(dt);
1957 struct osd_thread_info *info = osd_oti_get(env);
1962 LASSERT(osd_invariant(obj));
1963 LASSERT(!dt_object_exists(dt));
1964 LASSERT(osd_write_locked(env, obj));
1965 LASSERT(th != NULL);
1967 result = __osd_object_create(info, obj, attr, hint, dof, th);
1969 /* objects under osd root shld have igif fid, so dont add fid EA */
1970 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
1971 result = osd_ea_fid_set(env, dt, fid);
1974 result = __osd_oi_insert(env, obj, fid, th);
1976 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1977 LINVRNT(osd_invariant(obj));
1982 * Concurrency: @dt is write locked.
1984 static void osd_object_ref_add(const struct lu_env *env,
1985 struct dt_object *dt,
1988 struct osd_object *obj = osd_dt_obj(dt);
1989 struct inode *inode = obj->oo_inode;
1991 LINVRNT(osd_invariant(obj));
1992 LASSERT(dt_object_exists(dt));
1993 LASSERT(osd_write_locked(env, obj));
1994 LASSERT(th != NULL);
1996 cfs_spin_lock(&obj->oo_guard);
1997 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1999 cfs_spin_unlock(&obj->oo_guard);
2000 inode->i_sb->s_op->dirty_inode(inode);
2001 LINVRNT(osd_invariant(obj));
2005 * Concurrency: @dt is write locked.
2007 static void osd_object_ref_del(const struct lu_env *env,
2008 struct dt_object *dt,
2011 struct osd_object *obj = osd_dt_obj(dt);
2012 struct inode *inode = obj->oo_inode;
2014 LINVRNT(osd_invariant(obj));
2015 LASSERT(dt_object_exists(dt));
2016 LASSERT(osd_write_locked(env, obj));
2017 LASSERT(th != NULL);
2019 cfs_spin_lock(&obj->oo_guard);
2020 LASSERT(inode->i_nlink > 0);
2022 cfs_spin_unlock(&obj->oo_guard);
2023 inode->i_sb->s_op->dirty_inode(inode);
2024 LINVRNT(osd_invariant(obj));
2028 * Concurrency: @dt is read locked.
2030 static int osd_xattr_get(const struct lu_env *env,
2031 struct dt_object *dt,
2034 struct lustre_capa *capa)
2036 struct osd_object *obj = osd_dt_obj(dt);
2037 struct inode *inode = obj->oo_inode;
2038 struct osd_thread_info *info = osd_oti_get(env);
2039 struct dentry *dentry = &info->oti_obj_dentry;
2041 LASSERT(dt_object_exists(dt));
2042 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2043 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2045 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2048 dentry->d_inode = inode;
2049 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2053 * Concurrency: @dt is write locked.
2055 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2056 const struct lu_buf *buf, const char *name, int fl,
2057 struct thandle *handle, struct lustre_capa *capa)
2059 LASSERT(handle != NULL);
2061 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2064 return __osd_xattr_set(env, dt, buf, name, fl);
2068 * Concurrency: @dt is read locked.
2070 static int osd_xattr_list(const struct lu_env *env,
2071 struct dt_object *dt,
2073 struct lustre_capa *capa)
2075 struct osd_object *obj = osd_dt_obj(dt);
2076 struct inode *inode = obj->oo_inode;
2077 struct osd_thread_info *info = osd_oti_get(env);
2078 struct dentry *dentry = &info->oti_obj_dentry;
2080 LASSERT(dt_object_exists(dt));
2081 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2082 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2084 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2087 dentry->d_inode = inode;
2088 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2092 * Concurrency: @dt is write locked.
2094 static int osd_xattr_del(const struct lu_env *env,
2095 struct dt_object *dt,
2097 struct thandle *handle,
2098 struct lustre_capa *capa)
2100 struct osd_object *obj = osd_dt_obj(dt);
2101 struct inode *inode = obj->oo_inode;
2102 struct osd_thread_info *info = osd_oti_get(env);
2103 struct dentry *dentry = &info->oti_obj_dentry;
2106 LASSERT(dt_object_exists(dt));
2107 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2108 LASSERT(osd_write_locked(env, obj));
2109 LASSERT(handle != NULL);
2111 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2114 dentry->d_inode = inode;
2115 rc = inode->i_op->removexattr(dentry, name);
2119 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2120 struct dt_object *dt,
2121 struct lustre_capa *old,
2124 struct osd_thread_info *info = osd_oti_get(env);
2125 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2126 struct osd_object *obj = osd_dt_obj(dt);
2127 struct osd_device *dev = osd_obj2dev(obj);
2128 struct lustre_capa_key *key = &info->oti_capa_key;
2129 struct lustre_capa *capa = &info->oti_capa;
2130 struct obd_capa *oc;
2131 struct md_capainfo *ci;
2135 if (!dev->od_fl_capa)
2136 RETURN(ERR_PTR(-ENOENT));
2138 LASSERT(dt_object_exists(dt));
2139 LINVRNT(osd_invariant(obj));
2141 /* renewal sanity check */
2142 if (old && osd_object_auth(env, dt, old, opc))
2143 RETURN(ERR_PTR(-EACCES));
2145 ci = md_capainfo(env);
2147 RETURN(ERR_PTR(-ENOENT));
2149 switch (ci->mc_auth) {
2153 capa->lc_uid = obj->oo_inode->i_uid;
2154 capa->lc_gid = obj->oo_inode->i_gid;
2155 capa->lc_flags = LC_ID_PLAIN;
2157 case LC_ID_CONVERT: {
2160 s[0] = obj->oo_inode->i_uid;
2161 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2162 s[2] = obj->oo_inode->i_gid;
2163 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2164 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2166 RETURN(ERR_PTR(rc));
2168 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2169 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2170 capa->lc_flags = LC_ID_CONVERT;
2174 RETURN(ERR_PTR(-EINVAL));
2177 capa->lc_fid = *fid;
2179 capa->lc_flags |= dev->od_capa_alg << 24;
2180 capa->lc_timeout = dev->od_capa_timeout;
2181 capa->lc_expiry = 0;
2183 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2185 LASSERT(!capa_is_expired(oc));
2189 cfs_spin_lock(&capa_lock);
2190 *key = dev->od_capa_keys[1];
2191 cfs_spin_unlock(&capa_lock);
2193 capa->lc_keyid = key->lk_keyid;
2194 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2196 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2198 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2199 RETURN(ERR_PTR(rc));
2202 oc = capa_add(dev->od_capa_hash, capa);
2206 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2209 struct osd_object *obj = osd_dt_obj(dt);
2210 struct inode *inode = obj->oo_inode;
2211 struct osd_thread_info *info = osd_oti_get(env);
2212 struct dentry *dentry = &info->oti_obj_dentry;
2213 struct file *file = &info->oti_file;
2216 dentry->d_inode = inode;
2217 file->f_dentry = dentry;
2218 file->f_mapping = inode->i_mapping;
2219 file->f_op = inode->i_fop;
2220 LOCK_INODE_MUTEX(inode);
2221 rc = file->f_op->fsync(file, dentry, 0);
2222 UNLOCK_INODE_MUTEX(inode);
2227 * Get the 64-bit version for an inode.
2229 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2230 struct dt_object *dt)
2232 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2234 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2235 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2236 return LDISKFS_I(inode)->i_fs_version;
2240 * Set the 64-bit version and return the old version.
2242 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2243 dt_obj_version_t new_version)
2245 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2247 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2248 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2249 LDISKFS_I(inode)->i_fs_version = new_version;
2250 /** Version is set after all inode operations are finished,
2251 * so we should mark it dirty here */
2252 inode->i_sb->s_op->dirty_inode(inode);
2255 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2258 struct osd_object *obj = osd_dt_obj(dt);
2261 *data = (void *)obj->oo_inode;
2269 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2270 const struct dt_index_features *feat)
2272 struct iam_descr *descr;
2274 if (osd_object_is_root(o))
2275 return feat == &dt_directory_features;
2277 LASSERT(o->oo_dir != NULL);
2279 descr = o->oo_dir->od_container.ic_descr;
2280 if (feat == &dt_directory_features) {
2281 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2287 feat->dif_keysize_min <= descr->id_key_size &&
2288 descr->id_key_size <= feat->dif_keysize_max &&
2289 feat->dif_recsize_min <= descr->id_rec_size &&
2290 descr->id_rec_size <= feat->dif_recsize_max &&
2291 !(feat->dif_flags & (DT_IND_VARKEY |
2292 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2293 ergo(feat->dif_flags & DT_IND_UPDATE,
2294 1 /* XXX check that object (and file system) is
2299 static int osd_iam_container_init(const struct lu_env *env,
2300 struct osd_object *obj,
2301 struct osd_directory *dir)
2303 struct iam_container *bag = &dir->od_container;
2306 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2310 result = iam_container_setup(bag);
2314 if (osd_obj2dev(obj)->od_iop_mode) {
2315 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2317 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2323 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2325 iam_container_fini(bag);
2332 * Concurrency: no external locking is necessary.
2334 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2335 const struct dt_index_features *feat)
2339 struct osd_object *obj = osd_dt_obj(dt);
2340 struct osd_device *osd = osd_obj2dev(obj);
2342 LINVRNT(osd_invariant(obj));
2343 LASSERT(dt_object_exists(dt));
2345 if (osd_object_is_root(obj)) {
2346 dt->do_index_ops = &osd_index_ea_ops;
2348 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2349 dt->do_index_ops = &osd_index_ea_ops;
2350 if (S_ISDIR(obj->oo_inode->i_mode))
2355 } else if (!osd_has_index(obj)) {
2356 struct osd_directory *dir;
2361 cfs_spin_lock(&obj->oo_guard);
2362 if (obj->oo_dir == NULL)
2366 * Concurrent thread allocated container data.
2369 cfs_spin_unlock(&obj->oo_guard);
2371 * Now, that we have container data, serialize its
2374 cfs_down_write(&obj->oo_ext_idx_sem);
2376 * recheck under lock.
2378 if (!osd_has_index(obj))
2379 result = osd_iam_container_init(env, obj, dir);
2382 cfs_up_write(&obj->oo_ext_idx_sem);
2388 if (result == 0 && ea_dir == 0) {
2389 if (!osd_iam_index_probe(env, obj, feat))
2392 LINVRNT(osd_invariant(obj));
2397 static const struct dt_object_operations osd_obj_ops = {
2398 .do_read_lock = osd_object_read_lock,
2399 .do_write_lock = osd_object_write_lock,
2400 .do_read_unlock = osd_object_read_unlock,
2401 .do_write_unlock = osd_object_write_unlock,
2402 .do_write_locked = osd_object_write_locked,
2403 .do_attr_get = osd_attr_get,
2404 .do_attr_set = osd_attr_set,
2405 .do_ah_init = osd_ah_init,
2406 .do_create = osd_object_create,
2407 .do_index_try = osd_index_try,
2408 .do_ref_add = osd_object_ref_add,
2409 .do_ref_del = osd_object_ref_del,
2410 .do_xattr_get = osd_xattr_get,
2411 .do_xattr_set = osd_xattr_set,
2412 .do_xattr_del = osd_xattr_del,
2413 .do_xattr_list = osd_xattr_list,
2414 .do_capa_get = osd_capa_get,
2415 .do_object_sync = osd_object_sync,
2416 .do_version_get = osd_object_version_get,
2417 .do_version_set = osd_object_version_set,
2418 .do_data_get = osd_data_get,
2422 * dt_object_operations for interoperability mode
2423 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2425 static const struct dt_object_operations osd_obj_ea_ops = {
2426 .do_read_lock = osd_object_read_lock,
2427 .do_write_lock = osd_object_write_lock,
2428 .do_read_unlock = osd_object_read_unlock,
2429 .do_write_unlock = osd_object_write_unlock,
2430 .do_write_locked = osd_object_write_locked,
2431 .do_attr_get = osd_attr_get,
2432 .do_attr_set = osd_attr_set,
2433 .do_ah_init = osd_ah_init,
2434 .do_create = osd_object_ea_create,
2435 .do_index_try = osd_index_try,
2436 .do_ref_add = osd_object_ref_add,
2437 .do_ref_del = osd_object_ref_del,
2438 .do_xattr_get = osd_xattr_get,
2439 .do_xattr_set = osd_xattr_set,
2440 .do_xattr_del = osd_xattr_del,
2441 .do_xattr_list = osd_xattr_list,
2442 .do_capa_get = osd_capa_get,
2443 .do_object_sync = osd_object_sync,
2444 .do_version_get = osd_object_version_get,
2445 .do_version_set = osd_object_version_set,
2446 .do_data_get = osd_data_get,
2454 * XXX: Another layering violation for now.
2456 * We don't want to use ->f_op->read methods, because generic file write
2458 * - serializes on ->i_sem, and
2460 * - does a lot of extra work like balance_dirty_pages(),
2462 * which doesn't work for globally shared files like /last-received.
2464 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2466 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2468 memcpy(buffer, (char*)ei->i_data, buflen);
2473 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2476 struct buffer_head *bh;
2477 unsigned long block;
2484 /* prevent reading after eof */
2485 spin_lock(&inode->i_lock);
2486 if (i_size_read(inode) < *offs + size) {
2487 size = i_size_read(inode) - *offs;
2488 spin_unlock(&inode->i_lock);
2490 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2491 i_size_read(inode), *offs);
2493 } else if (size == 0) {
2497 spin_unlock(&inode->i_lock);
2500 blocksize = 1 << inode->i_blkbits;
2503 block = *offs >> inode->i_blkbits;
2504 boffs = *offs & (blocksize - 1);
2505 csize = min(blocksize - boffs, size);
2506 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2508 CERROR("can't read block: %d\n", err);
2512 memcpy(buf, bh->b_data + boffs, csize);
2522 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2523 struct lu_buf *buf, loff_t *pos,
2524 struct lustre_capa *capa)
2526 struct osd_object *obj = osd_dt_obj(dt);
2527 struct inode *inode = obj->oo_inode;
2530 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2533 /* Read small symlink from inode body as we need to maintain correct
2534 * on-disk symlinks for ldiskfs.
2536 if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2537 (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2538 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2540 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2545 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2548 memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2550 LDISKFS_I(inode)->i_disksize = buflen;
2551 i_size_write(inode, buflen);
2552 inode->i_sb->s_op->dirty_inode(inode);
2558 osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2559 int write_NUL, loff_t *offs, handle_t *handle)
2561 struct buffer_head *bh = NULL;
2562 loff_t offset = *offs;
2563 loff_t new_size = i_size_read(inode);
2564 unsigned long block;
2565 int blocksize = 1 << inode->i_blkbits;
2569 int dirty_inode = 0;
2573 * long symlink write does not count the NUL terminator in
2574 * bufsize, we write it, and the inode's file size does not
2575 * count the NUL terminator as well.
2577 ((char*)buf)[bufsize] = '\0';
2580 while (bufsize > 0) {
2584 block = offset >> inode->i_blkbits;
2585 boffs = offset & (blocksize - 1);
2586 size = min(blocksize - boffs, bufsize);
2587 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2589 CERROR("can't read/create block: %d\n", err);
2593 err = ldiskfs_journal_get_write_access(handle, bh);
2595 CERROR("journal_get_write_access() returned error %d\n",
2599 LASSERTF(boffs + size <= bh->b_size,
2600 "boffs %d size %d bh->b_size %lu",
2601 boffs, size, (unsigned long)bh->b_size);
2602 memcpy(bh->b_data + boffs, buf, size);
2603 err = ldiskfs_journal_dirty_metadata(handle, bh);
2607 if (offset + size > new_size)
2608 new_size = offset + size;
2618 /* correct in-core and on-disk sizes */
2619 if (new_size > i_size_read(inode)) {
2620 spin_lock(&inode->i_lock);
2621 if (new_size > i_size_read(inode))
2622 i_size_write(inode, new_size);
2623 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
2624 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
2627 spin_unlock(&inode->i_lock);
2629 inode->i_sb->s_op->dirty_inode(inode);
2637 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2638 const struct lu_buf *buf, loff_t *pos,
2639 struct thandle *handle, struct lustre_capa *capa,
2642 struct osd_object *obj = osd_dt_obj(dt);
2643 struct inode *inode = obj->oo_inode;
2644 struct osd_thandle *oh;
2647 #ifdef HAVE_QUOTA_SUPPORT
2648 cfs_cap_t save = cfs_curproc_cap_pack();
2651 LASSERT(handle != NULL);
2653 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2656 oh = container_of(handle, struct osd_thandle, ot_super);
2657 LASSERT(oh->ot_handle->h_transaction != NULL);
2658 #ifdef HAVE_QUOTA_SUPPORT
2660 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2662 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2664 /* Write small symlink to inode body as we need to maintain correct
2665 * on-disk symlinks for ldiskfs.
2666 * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
2667 * does not count it in.
2669 is_link = S_ISLNK(dt->do_lu.lo_header->loh_attr);
2670 if(is_link && (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
2671 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2673 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
2674 buf->lb_len, is_link, pos,
2676 #ifdef HAVE_QUOTA_SUPPORT
2677 cfs_curproc_cap_unpack(save);
2680 result = buf->lb_len;
2684 static const struct dt_body_operations osd_body_ops = {
2685 .dbo_read = osd_read,
2686 .dbo_write = osd_write
2691 * delete a (key, value) pair from index \a dt specified by \a key
2693 * \param dt osd index object
2694 * \param key key for index
2695 * \param rec record reference
2696 * \param handle transaction handler
2699 * \retval -ve failure
2702 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2703 const struct dt_key *key, struct thandle *handle,
2704 struct lustre_capa *capa)
2706 struct osd_object *obj = osd_dt_obj(dt);
2707 struct osd_thandle *oh;
2708 struct iam_path_descr *ipd;
2709 struct iam_container *bag = &obj->oo_dir->od_container;
2714 LINVRNT(osd_invariant(obj));
2715 LASSERT(dt_object_exists(dt));
2716 LASSERT(bag->ic_object == obj->oo_inode);
2717 LASSERT(handle != NULL);
2719 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2722 ipd = osd_idx_ipd_get(env, bag);
2723 if (unlikely(ipd == NULL))
2726 oh = container_of0(handle, struct osd_thandle, ot_super);
2727 LASSERT(oh->ot_handle != NULL);
2728 LASSERT(oh->ot_handle->h_transaction != NULL);
2730 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2731 osd_ipd_put(env, bag, ipd);
2732 LINVRNT(osd_invariant(obj));
2736 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2739 struct osd_fid_pack *rec;
2742 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2743 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2744 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2750 * Index delete function for interoperability mode (b11826).
2751 * It will remove the directory entry added by osd_index_ea_insert().
2752 * This entry is needed to maintain name->fid mapping.
2754 * \param key, key i.e. file entry to be deleted
2756 * \retval 0, on success
2757 * \retval -ve, on error
2759 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2760 const struct dt_key *key, struct thandle *handle,
2761 struct lustre_capa *capa)
2763 struct osd_object *obj = osd_dt_obj(dt);
2764 struct inode *dir = obj->oo_inode;
2765 struct dentry *dentry;
2766 struct osd_thandle *oh;
2767 struct ldiskfs_dir_entry_2 *de;
2768 struct buffer_head *bh;
2774 LINVRNT(osd_invariant(obj));
2775 LASSERT(dt_object_exists(dt));
2776 LASSERT(handle != NULL);
2778 oh = container_of(handle, struct osd_thandle, ot_super);
2779 LASSERT(oh->ot_handle != NULL);
2780 LASSERT(oh->ot_handle->h_transaction != NULL);
2782 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2785 dentry = osd_child_dentry_get(env, obj,
2786 (char *)key, strlen((char *)key));
2788 cfs_down_write(&obj->oo_ext_idx_sem);
2789 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
2791 rc = ldiskfs_delete_entry(oh->ot_handle,
2797 cfs_up_write(&obj->oo_ext_idx_sem);
2798 LASSERT(osd_invariant(obj));
2803 * Lookup index for \a key and copy record to \a rec.
2805 * \param dt osd index object
2806 * \param key key for index
2807 * \param rec record reference
2809 * \retval +ve success : exact mach
2810 * \retval 0 return record with key not greater than \a key
2811 * \retval -ve failure
2813 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2814 struct dt_rec *rec, const struct dt_key *key,
2815 struct lustre_capa *capa)
2817 struct osd_object *obj = osd_dt_obj(dt);
2818 struct iam_path_descr *ipd;
2819 struct iam_container *bag = &obj->oo_dir->od_container;
2820 struct osd_thread_info *oti = osd_oti_get(env);
2821 struct iam_iterator *it = &oti->oti_idx_it;
2822 struct iam_rec *iam_rec;
2826 LASSERT(osd_invariant(obj));
2827 LASSERT(dt_object_exists(dt));
2828 LASSERT(bag->ic_object == obj->oo_inode);
2830 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2833 ipd = osd_idx_ipd_get(env, bag);
2837 /* got ipd now we can start iterator. */
2838 iam_it_init(it, bag, 0, ipd);
2840 rc = iam_it_get(it, (struct iam_key *)key);
2842 if (S_ISDIR(obj->oo_inode->i_mode))
2843 iam_rec = (struct iam_rec *)oti->oti_ldp;
2845 iam_rec = (struct iam_rec *) rec;
2847 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2848 if (S_ISDIR(obj->oo_inode->i_mode))
2849 osd_fid_unpack((struct lu_fid *) rec,
2850 (struct osd_fid_pack *)iam_rec);
2854 osd_ipd_put(env, bag, ipd);
2856 LINVRNT(osd_invariant(obj));
2862 * Inserts (key, value) pair in \a dt index object.
2864 * \param dt osd index object
2865 * \param key key for index
2866 * \param rec record reference
2867 * \param th transaction handler
2870 * \retval -ve failure
2872 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2873 const struct dt_rec *rec, const struct dt_key *key,
2874 struct thandle *th, struct lustre_capa *capa,
2877 struct osd_object *obj = osd_dt_obj(dt);
2878 struct iam_path_descr *ipd;
2879 struct osd_thandle *oh;
2880 struct iam_container *bag = &obj->oo_dir->od_container;
2881 #ifdef HAVE_QUOTA_SUPPORT
2882 cfs_cap_t save = cfs_curproc_cap_pack();
2884 struct osd_thread_info *oti = osd_oti_get(env);
2885 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2890 LINVRNT(osd_invariant(obj));
2891 LASSERT(dt_object_exists(dt));
2892 LASSERT(bag->ic_object == obj->oo_inode);
2893 LASSERT(th != NULL);
2895 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2898 ipd = osd_idx_ipd_get(env, bag);
2899 if (unlikely(ipd == NULL))
2902 oh = container_of0(th, struct osd_thandle, ot_super);
2903 LASSERT(oh->ot_handle != NULL);
2904 LASSERT(oh->ot_handle->h_transaction != NULL);
2905 #ifdef HAVE_QUOTA_SUPPORT
2907 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2909 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2911 if (S_ISDIR(obj->oo_inode->i_mode))
2912 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2914 iam_rec = (struct iam_rec *) rec;
2915 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2917 #ifdef HAVE_QUOTA_SUPPORT
2918 cfs_curproc_cap_unpack(save);
2920 osd_ipd_put(env, bag, ipd);
2921 LINVRNT(osd_invariant(obj));
2926 * Calls ldiskfs_add_entry() to add directory entry
2927 * into the directory. This is required for
2928 * interoperability mode (b11826)
2930 * \retval 0, on success
2931 * \retval -ve, on error
2933 static int __osd_ea_add_rec(struct osd_thread_info *info,
2934 struct osd_object *pobj,
2935 struct inode *cinode,
2937 const struct dt_rec *fid,
2940 struct ldiskfs_dentry_param *ldp;
2941 struct dentry *child;
2942 struct osd_thandle *oth;
2945 oth = container_of(th, struct osd_thandle, ot_super);
2946 LASSERT(oth->ot_handle != NULL);
2947 LASSERT(oth->ot_handle->h_transaction != NULL);
2949 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2951 if (fid_is_igif((struct lu_fid *)fid) ||
2952 fid_is_norm((struct lu_fid *)fid)) {
2953 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2954 osd_get_ldiskfs_dirent_param(ldp, fid);
2955 child->d_fsdata = (void*) ldp;
2957 child->d_fsdata = NULL;
2958 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2964 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2965 * into the directory.Also sets flags into osd object to
2966 * indicate dot and dotdot are created. This is required for
2967 * interoperability mode (b11826)
2969 * \param dir directory for dot and dotdot fixup.
2970 * \param obj child object for linking
2972 * \retval 0, on success
2973 * \retval -ve, on error
2975 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2976 struct osd_object *dir,
2977 struct inode *parent_dir, const char *name,
2978 const struct dt_rec *dot_fid,
2979 const struct dt_rec *dot_dot_fid,
2982 struct inode *inode = dir->oo_inode;
2983 struct ldiskfs_dentry_param *dot_ldp;
2984 struct ldiskfs_dentry_param *dot_dot_ldp;
2985 struct osd_thandle *oth;
2988 oth = container_of(th, struct osd_thandle, ot_super);
2989 LASSERT(oth->ot_handle->h_transaction != NULL);
2990 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2992 if (strcmp(name, dot) == 0) {
2993 if (dir->oo_compat_dot_created) {
2996 LASSERT(inode == parent_dir);
2997 dir->oo_compat_dot_created = 1;
3000 } else if(strcmp(name, dotdot) == 0) {
3001 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3002 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3004 if (!dir->oo_compat_dot_created)
3006 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
3007 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3008 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3013 /* in case of rename, dotdot is already created */
3014 if (dir->oo_compat_dotdot_created) {
3015 return __osd_ea_add_rec(info, dir, parent_dir, name,
3019 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode,
3020 dot_ldp, dot_dot_ldp);
3022 dir->oo_compat_dotdot_created = 1;
3030 * It will call the appropriate osd_add* function and return the
3031 * value, return by respective functions.
3033 static int osd_ea_add_rec(const struct lu_env *env,
3034 struct osd_object *pobj,
3035 struct inode *cinode,
3037 const struct dt_rec *fid,
3040 struct osd_thread_info *info = osd_oti_get(env);
3043 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3045 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3046 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3049 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid, th);
3055 * Calls ->lookup() to find dentry. From dentry get inode and
3056 * read inode's ea to get fid. This is required for interoperability
3059 * \retval 0, on success
3060 * \retval -ve, on error
3062 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3063 struct dt_rec *rec, const struct dt_key *key)
3065 struct inode *dir = obj->oo_inode;
3066 struct dentry *dentry;
3067 struct ldiskfs_dir_entry_2 *de;
3068 struct buffer_head *bh;
3069 struct lu_fid *fid = (struct lu_fid *) rec;
3073 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3075 dentry = osd_child_dentry_get(env, obj,
3076 (char *)key, strlen((char *)key));
3078 cfs_down_read(&obj->oo_ext_idx_sem);
3079 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
3081 ino = le32_to_cpu(de->inode);
3082 rc = osd_get_fid_from_dentry(de, rec);
3084 /* done with de, release bh */
3087 rc = osd_ea_fid_get(env, obj, ino, fid);
3091 cfs_up_read(&obj->oo_ext_idx_sem);
3096 * Find the osd object for given fid.
3098 * \param fid need to find the osd object having this fid
3100 * \retval osd_object on success
3101 * \retval -ve on error
3103 struct osd_object *osd_object_find(const struct lu_env *env,
3104 struct dt_object *dt,
3105 const struct lu_fid *fid)
3107 struct lu_device *ludev = dt->do_lu.lo_dev;
3108 struct osd_object *child = NULL;
3109 struct lu_object *luch;
3110 struct lu_object *lo;
3112 luch = lu_object_find(env, ludev, fid, NULL);
3113 if (!IS_ERR(luch)) {
3114 if (lu_object_exists(luch)) {
3115 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3117 child = osd_obj(lo);
3119 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3120 "lu_object can't be located"
3121 ""DFID"\n", PFID(fid));
3123 if (child == NULL) {
3124 lu_object_put(env, luch);
3125 CERROR("Unable to get osd_object\n");
3126 child = ERR_PTR(-ENOENT);
3129 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3130 "lu_object does not exists "DFID"\n",
3132 child = ERR_PTR(-ENOENT);
3135 child = (void *)luch;
3141 * Put the osd object once done with it.
3143 * \param obj osd object that needs to be put
3145 static inline void osd_object_put(const struct lu_env *env,
3146 struct osd_object *obj)
3148 lu_object_put(env, &obj->oo_dt.do_lu);
3152 * Index add function for interoperability mode (b11826).
3153 * It will add the directory entry.This entry is needed to
3154 * maintain name->fid mapping.
3156 * \param key it is key i.e. file entry to be inserted
3157 * \param rec it is value of given key i.e. fid
3159 * \retval 0, on success
3160 * \retval -ve, on error
3162 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3163 const struct dt_rec *rec,
3164 const struct dt_key *key, struct thandle *th,
3165 struct lustre_capa *capa, int ignore_quota)
3167 struct osd_object *obj = osd_dt_obj(dt);
3168 struct lu_fid *fid = (struct lu_fid *) rec;
3169 const char *name = (const char *)key;
3170 struct osd_object *child;
3171 #ifdef HAVE_QUOTA_SUPPORT
3172 cfs_cap_t save = cfs_curproc_cap_pack();
3178 LASSERT(osd_invariant(obj));
3179 LASSERT(dt_object_exists(dt));
3180 LASSERT(th != NULL);
3182 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3185 child = osd_object_find(env, dt, fid);
3186 if (!IS_ERR(child)) {
3187 #ifdef HAVE_QUOTA_SUPPORT
3189 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3191 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3193 cfs_down_write(&obj->oo_ext_idx_sem);
3194 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3195 cfs_up_write(&obj->oo_ext_idx_sem);
3196 #ifdef HAVE_QUOTA_SUPPORT
3197 cfs_curproc_cap_unpack(save);
3199 osd_object_put(env, child);
3201 rc = PTR_ERR(child);
3204 LASSERT(osd_invariant(obj));
3209 * Initialize osd Iterator for given osd index object.
3211 * \param dt osd index object
3214 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3215 struct dt_object *dt,
3217 struct lustre_capa *capa)
3219 struct osd_it_iam *it;
3220 struct osd_thread_info *oti = osd_oti_get(env);
3221 struct osd_object *obj = osd_dt_obj(dt);
3222 struct lu_object *lo = &dt->do_lu;
3223 struct iam_path_descr *ipd;
3224 struct iam_container *bag = &obj->oo_dir->od_container;
3226 LASSERT(lu_object_exists(lo));
3228 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3229 return ERR_PTR(-EACCES);
3232 ipd = osd_it_ipd_get(env, bag);
3233 if (likely(ipd != NULL)) {
3237 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3238 return (struct dt_it *)it;
3240 return ERR_PTR(-ENOMEM);
3244 * free given Iterator.
3247 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3249 struct osd_it_iam *it = (struct osd_it_iam *)di;
3250 struct osd_object *obj = it->oi_obj;
3252 iam_it_fini(&it->oi_it);
3253 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3254 lu_object_put(env, &obj->oo_dt.do_lu);
3258 * Move Iterator to record specified by \a key
3260 * \param di osd iterator
3261 * \param key key for index
3263 * \retval +ve di points to record with least key not larger than key
3264 * \retval 0 di points to exact matched key
3265 * \retval -ve failure
3268 static int osd_it_iam_get(const struct lu_env *env,
3269 struct dt_it *di, const struct dt_key *key)
3271 struct osd_it_iam *it = (struct osd_it_iam *)di;
3273 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3279 * \param di osd iterator
3282 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3284 struct osd_it_iam *it = (struct osd_it_iam *)di;
3286 iam_it_put(&it->oi_it);
3290 * Move iterator by one record
3292 * \param di osd iterator
3294 * \retval +1 end of container reached
3296 * \retval -ve failure
3299 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3301 struct osd_it_iam *it = (struct osd_it_iam *)di;
3303 return iam_it_next(&it->oi_it);
3307 * Return pointer to the key under iterator.
3310 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3311 const struct dt_it *di)
3313 struct osd_it_iam *it = (struct osd_it_iam *)di;
3315 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3319 * Return size of key under iterator (in bytes)
3322 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3324 struct osd_it_iam *it = (struct osd_it_iam *)di;
3326 return iam_it_key_size(&it->oi_it);
3329 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3334 struct luda_type *lt;
3335 const unsigned align = sizeof(struct luda_type) - 1;
3337 /* check if file type is required */
3338 if (attr & LUDA_TYPE) {
3339 len = (len + align) & ~align;
3341 lt = (void *) ent->lde_name + len;
3342 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3343 ent->lde_attrs |= LUDA_TYPE;
3346 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3350 * build lu direct from backend fs dirent.
3353 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3361 fid_cpu_to_le(&ent->lde_fid, fid);
3362 ent->lde_attrs = LUDA_FID;
3364 ent->lde_hash = cpu_to_le64(offset);
3365 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3367 strncpy(ent->lde_name, name, namelen);
3368 ent->lde_namelen = cpu_to_le16(namelen);
3370 /* append lustre attributes */
3371 osd_it_append_attrs(ent, attr, namelen, type);
3375 * Return pointer to the record under iterator.
3377 static int osd_it_iam_rec(const struct lu_env *env,
3378 const struct dt_it *di,
3379 struct lu_dirent *lde,
3382 struct osd_it_iam *it = (struct osd_it_iam *)di;
3383 struct osd_thread_info *info = osd_oti_get(env);
3384 struct lu_fid *fid = &info->oti_fid;
3385 const struct osd_fid_pack *rec;
3391 name = (char *)iam_it_key_get(&it->oi_it);
3393 RETURN(PTR_ERR(name));
3395 namelen = iam_it_key_size(&it->oi_it);
3397 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3399 RETURN(PTR_ERR(rec));
3401 rc = osd_fid_unpack(fid, rec);
3405 hash = iam_it_store(&it->oi_it);
3407 /* IAM does not store object type in IAM index (dir) */
3408 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3415 * Returns cookie for current Iterator position.
3417 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3419 struct osd_it_iam *it = (struct osd_it_iam *)di;
3421 return iam_it_store(&it->oi_it);
3425 * Restore iterator from cookie.
3427 * \param di osd iterator
3428 * \param hash Iterator location cookie
3430 * \retval +ve di points to record with least key not larger than key.
3431 * \retval 0 di points to exact matched key
3432 * \retval -ve failure
3435 static int osd_it_iam_load(const struct lu_env *env,
3436 const struct dt_it *di, __u64 hash)
3438 struct osd_it_iam *it = (struct osd_it_iam *)di;
3440 return iam_it_load(&it->oi_it, hash);
3443 static const struct dt_index_operations osd_index_iam_ops = {
3444 .dio_lookup = osd_index_iam_lookup,
3445 .dio_insert = osd_index_iam_insert,
3446 .dio_delete = osd_index_iam_delete,
3448 .init = osd_it_iam_init,
3449 .fini = osd_it_iam_fini,
3450 .get = osd_it_iam_get,
3451 .put = osd_it_iam_put,
3452 .next = osd_it_iam_next,
3453 .key = osd_it_iam_key,
3454 .key_size = osd_it_iam_key_size,
3455 .rec = osd_it_iam_rec,
3456 .store = osd_it_iam_store,
3457 .load = osd_it_iam_load
3462 * Creates or initializes iterator context.
3464 * \retval struct osd_it_ea, iterator structure on success
3467 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3468 struct dt_object *dt,
3470 struct lustre_capa *capa)
3472 struct osd_object *obj = osd_dt_obj(dt);
3473 struct osd_thread_info *info = osd_oti_get(env);
3474 struct osd_it_ea *it = &info->oti_it_ea;
3475 struct lu_object *lo = &dt->do_lu;
3476 struct dentry *obj_dentry = &info->oti_it_dentry;
3478 LASSERT(lu_object_exists(lo));
3480 obj_dentry->d_inode = obj->oo_inode;
3481 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3482 obj_dentry->d_name.hash = 0;
3484 it->oie_rd_dirent = 0;
3485 it->oie_it_dirent = 0;
3486 it->oie_dirent = NULL;
3487 it->oie_buf = info->oti_it_ea_buf;
3489 it->oie_file.f_pos = 0;
3490 it->oie_file.f_dentry = obj_dentry;
3491 if (attr & LUDA_64BITHASH)
3492 it->oie_file.f_mode |= FMODE_64BITHASH;
3494 it->oie_file.f_mode |= FMODE_32BITHASH;
3495 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3496 it->oie_file.f_op = obj->oo_inode->i_fop;
3497 it->oie_file.private_data = NULL;
3499 RETURN((struct dt_it *) it);
3503 * Destroy or finishes iterator context.
3505 * \param di iterator structure to be destroyed
3507 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3509 struct osd_it_ea *it = (struct osd_it_ea *)di;
3510 struct osd_object *obj = it->oie_obj;
3511 struct inode *inode = obj->oo_inode;
3514 it->oie_file.f_op->release(inode, &it->oie_file);
3515 lu_object_put(env, &obj->oo_dt.do_lu);
3520 * It position the iterator at given key, so that next lookup continues from
3521 * that key Or it is similar to dio_it->load() but based on a key,
3522 * rather than file position.
3524 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3527 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3529 static int osd_it_ea_get(const struct lu_env *env,
3530 struct dt_it *di, const struct dt_key *key)
3532 struct osd_it_ea *it = (struct osd_it_ea *)di;
3535 LASSERT(((const char *)key)[0] == '\0');
3536 it->oie_file.f_pos = 0;
3537 it->oie_rd_dirent = 0;
3538 it->oie_it_dirent = 0;
3539 it->oie_dirent = NULL;
3547 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3552 * It is called internally by ->readdir(). It fills the
3553 * iterator's in-memory data structure with required
3554 * information i.e. name, namelen, rec_size etc.
3556 * \param buf in which information to be filled in.
3557 * \param name name of the file in given dir
3559 * \retval 0 on success
3560 * \retval 1 on buffer full
3562 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3563 loff_t offset, __u64 ino,
3566 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3567 struct osd_it_ea_dirent *ent = it->oie_dirent;
3568 struct lu_fid *fid = &ent->oied_fid;
3569 struct osd_fid_pack *rec;
3572 /* this should never happen */
3573 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3574 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3578 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3582 if (d_type & LDISKFS_DIRENT_LUFID) {
3583 rec = (struct osd_fid_pack*) (name + namelen + 1);
3585 if (osd_fid_unpack(fid, rec) != 0)
3588 d_type &= ~LDISKFS_DIRENT_LUFID;
3593 ent->oied_ino = ino;
3594 ent->oied_off = offset;
3595 ent->oied_namelen = namelen;
3596 ent->oied_type = d_type;
3598 memcpy(ent->oied_name, name, namelen);
3600 it->oie_rd_dirent++;
3601 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3606 * Calls ->readdir() to load a directory entry at a time
3607 * and stored it in iterator's in-memory data structure.
3609 * \param di iterator's in memory structure
3611 * \retval 0 on success
3612 * \retval -ve on error
3614 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3616 struct osd_it_ea *it = (struct osd_it_ea *)di;
3617 struct osd_object *obj = it->oie_obj;
3618 struct inode *inode = obj->oo_inode;
3622 it->oie_dirent = it->oie_buf;
3623 it->oie_rd_dirent = 0;
3625 cfs_down_read(&obj->oo_ext_idx_sem);
3626 result = inode->i_fop->readdir(&it->oie_file, it,
3627 (filldir_t) osd_ldiskfs_filldir);
3629 cfs_up_read(&obj->oo_ext_idx_sem);
3631 if (it->oie_rd_dirent == 0) {
3634 it->oie_dirent = it->oie_buf;
3635 it->oie_it_dirent = 1;
3642 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3643 * to load a directory entry at a time and stored it in
3644 * iterator's in-memory data structure.
3646 * \param di iterator's in memory structure
3648 * \retval +ve iterator reached to end
3649 * \retval 0 iterator not reached to end
3650 * \retval -ve on error
3652 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3654 struct osd_it_ea *it = (struct osd_it_ea *)di;
3659 if (it->oie_it_dirent < it->oie_rd_dirent) {
3661 (void *) it->oie_dirent +
3662 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3663 it->oie_dirent->oied_namelen);
3664 it->oie_it_dirent++;
3667 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
3670 rc = osd_ldiskfs_it_fill(di);
3677 * Returns the key at current position from iterator's in memory structure.
3679 * \param di iterator's in memory structure
3681 * \retval key i.e. struct dt_key on success
3683 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3684 const struct dt_it *di)
3686 struct osd_it_ea *it = (struct osd_it_ea *)di;
3688 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3692 * Returns the key's size at current position from iterator's in memory structure.
3694 * \param di iterator's in memory structure
3696 * \retval key_size i.e. struct dt_key on success
3698 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3700 struct osd_it_ea *it = (struct osd_it_ea *)di;
3702 RETURN(it->oie_dirent->oied_namelen);
3707 * Returns the value (i.e. fid/igif) at current position from iterator's
3708 * in memory structure.
3710 * \param di struct osd_it_ea, iterator's in memory structure
3711 * \param attr attr requested for dirent.
3712 * \param lde lustre dirent
3714 * \retval 0 no error and \param lde has correct lustre dirent.
3715 * \retval -ve on error
3717 static inline int osd_it_ea_rec(const struct lu_env *env,
3718 const struct dt_it *di,
3719 struct lu_dirent *lde,
3722 struct osd_it_ea *it = (struct osd_it_ea *)di;
3723 struct osd_object *obj = it->oie_obj;
3724 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3729 if (!fid_is_sane(fid))
3730 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3733 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3734 it->oie_dirent->oied_name,
3735 it->oie_dirent->oied_namelen,
3736 it->oie_dirent->oied_type,
3742 * Returns a cookie for current position of the iterator head, so that
3743 * user can use this cookie to load/start the iterator next time.
3745 * \param di iterator's in memory structure
3747 * \retval cookie for current position, on success
3749 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3751 struct osd_it_ea *it = (struct osd_it_ea *)di;
3753 RETURN(it->oie_dirent->oied_off);
3757 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3758 * to load a directory entry at a time and stored it i inn,
3759 * in iterator's in-memory data structure.
3761 * \param di struct osd_it_ea, iterator's in memory structure
3763 * \retval +ve on success
3764 * \retval -ve on error
3766 static int osd_it_ea_load(const struct lu_env *env,
3767 const struct dt_it *di, __u64 hash)
3769 struct osd_it_ea *it = (struct osd_it_ea *)di;
3773 it->oie_file.f_pos = hash;
3775 rc = osd_ldiskfs_it_fill(di);
3783 * Index lookup function for interoperability mode (b11826).
3785 * \param key, key i.e. file name to be searched
3787 * \retval +ve, on success
3788 * \retval -ve, on error
3790 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3791 struct dt_rec *rec, const struct dt_key *key,
3792 struct lustre_capa *capa)
3794 struct osd_object *obj = osd_dt_obj(dt);
3799 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3800 LINVRNT(osd_invariant(obj));
3802 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3805 rc = osd_ea_lookup_rec(env, obj, rec, key);
3813 * Index and Iterator operations for interoperability
3814 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3816 static const struct dt_index_operations osd_index_ea_ops = {
3817 .dio_lookup = osd_index_ea_lookup,
3818 .dio_insert = osd_index_ea_insert,
3819 .dio_delete = osd_index_ea_delete,
3821 .init = osd_it_ea_init,
3822 .fini = osd_it_ea_fini,
3823 .get = osd_it_ea_get,
3824 .put = osd_it_ea_put,
3825 .next = osd_it_ea_next,
3826 .key = osd_it_ea_key,
3827 .key_size = osd_it_ea_key_size,
3828 .rec = osd_it_ea_rec,
3829 .store = osd_it_ea_store,
3830 .load = osd_it_ea_load
3834 static void *osd_key_init(const struct lu_context *ctx,
3835 struct lu_context_key *key)
3837 struct osd_thread_info *info;
3839 OBD_ALLOC_PTR(info);
3841 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3842 if (info->oti_it_ea_buf != NULL) {
3843 info->oti_env = container_of(ctx, struct lu_env,
3847 info = ERR_PTR(-ENOMEM);
3850 info = ERR_PTR(-ENOMEM);
3855 static void osd_key_fini(const struct lu_context *ctx,
3856 struct lu_context_key *key, void* data)
3858 struct osd_thread_info *info = data;
3860 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3864 static void osd_key_exit(const struct lu_context *ctx,
3865 struct lu_context_key *key, void *data)
3867 struct osd_thread_info *info = data;
3869 LASSERT(info->oti_r_locks == 0);
3870 LASSERT(info->oti_w_locks == 0);
3871 LASSERT(info->oti_txns == 0);
3874 /* type constructor/destructor: osd_type_init, osd_type_fini */
3875 LU_TYPE_INIT_FINI(osd, &osd_key);
3877 static struct lu_context_key osd_key = {
3878 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3879 .lct_init = osd_key_init,
3880 .lct_fini = osd_key_fini,
3881 .lct_exit = osd_key_exit
3885 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3886 const char *name, struct lu_device *next)
3889 struct lu_context *ctx;
3891 /* context for commit hooks */
3892 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3893 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3895 rc = osd_procfs_init(osd_dev(d), name);
3896 ctx->lc_cookie = 0x3;
3901 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3903 struct osd_thread_info *info = osd_oti_get(env);
3905 if (o->od_obj_area != NULL) {
3906 lu_object_put(env, &o->od_obj_area->do_lu);
3907 o->od_obj_area = NULL;
3909 osd_oi_fini(info, &o->od_oi);
3914 static int osd_mount(const struct lu_env *env,
3915 struct osd_device *o, struct lustre_cfg *cfg)
3917 struct lustre_mount_info *lmi;
3918 const char *dev = lustre_cfg_string(cfg, 0);
3919 struct lustre_disk_data *ldd;
3920 struct lustre_sb_info *lsi;
3923 if (o->od_mount != NULL) {
3924 CERROR("Already mounted (%s)\n", dev);
3929 lmi = server_get_mount(dev);
3931 CERROR("Cannot get mount info for %s!\n", dev);
3935 LASSERT(lmi != NULL);
3936 /* save lustre_mount_info in dt_device */
3939 lsi = s2lsi(lmi->lmi_sb);
3942 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3944 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3948 o->od_obj_area = NULL;
3952 static struct lu_device *osd_device_fini(const struct lu_env *env,
3953 struct lu_device *d)
3958 shrink_dcache_sb(osd_sb(osd_dev(d)));
3959 osd_sync(env, lu2dt_dev(d));
3961 rc = osd_procfs_fini(osd_dev(d));
3963 CERROR("proc fini error %d \n", rc);
3964 RETURN (ERR_PTR(rc));
3967 if (osd_dev(d)->od_mount)
3968 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3969 osd_dev(d)->od_mount->lmi_mnt);
3970 osd_dev(d)->od_mount = NULL;
3972 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3976 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3977 struct lu_device_type *t,
3978 struct lustre_cfg *cfg)
3980 struct lu_device *l;
3981 struct osd_device *o;
3987 result = dt_device_init(&o->od_dt_dev, t);
3990 l->ld_ops = &osd_lu_ops;
3991 o->od_dt_dev.dd_ops = &osd_dt_ops;
3992 cfs_spin_lock_init(&o->od_osfs_lock);
3993 o->od_osfs_age = cfs_time_shift_64(-1000);
3994 o->od_capa_hash = init_capa_hash();
3995 if (o->od_capa_hash == NULL) {
3996 dt_device_fini(&o->od_dt_dev);
3997 l = ERR_PTR(-ENOMEM);
4000 l = ERR_PTR(result);
4005 l = ERR_PTR(-ENOMEM);
4009 static struct lu_device *osd_device_free(const struct lu_env *env,
4010 struct lu_device *d)
4012 struct osd_device *o = osd_dev(d);
4015 cleanup_capa_hash(o->od_capa_hash);
4016 dt_device_fini(&o->od_dt_dev);
4021 static int osd_process_config(const struct lu_env *env,
4022 struct lu_device *d, struct lustre_cfg *cfg)
4024 struct osd_device *o = osd_dev(d);
4028 switch(cfg->lcfg_command) {
4030 err = osd_mount(env, o, cfg);
4033 err = osd_shutdown(env, o);
4042 static int osd_recovery_complete(const struct lu_env *env,
4043 struct lu_device *d)
4048 static int osd_prepare(const struct lu_env *env,
4049 struct lu_device *pdev,
4050 struct lu_device *dev)
4052 struct osd_device *osd = osd_dev(dev);
4053 struct lustre_sb_info *lsi;
4054 struct lustre_disk_data *ldd;
4055 struct lustre_mount_info *lmi;
4056 struct osd_thread_info *oti = osd_oti_get(env);
4057 struct dt_object *d;
4061 /* 1. initialize oi before any file create or file open */
4062 result = osd_oi_init(oti, &osd->od_oi,
4063 &osd->od_dt_dev, lu2md_dev(pdev));
4067 lmi = osd->od_mount;
4068 lsi = s2lsi(lmi->lmi_sb);
4071 /* 2. setup local objects */
4072 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4076 /* 3. open remote object dir */
4077 d = dt_store_open(env, lu2dt_dev(dev), "",
4078 remote_obj_dir, &oti->oti_fid);
4080 osd->od_obj_area = d;
4083 result = PTR_ERR(d);
4084 osd->od_obj_area = NULL;
4091 static const struct lu_object_operations osd_lu_obj_ops = {
4092 .loo_object_init = osd_object_init,
4093 .loo_object_delete = osd_object_delete,
4094 .loo_object_release = osd_object_release,
4095 .loo_object_free = osd_object_free,
4096 .loo_object_print = osd_object_print,
4097 .loo_object_invariant = osd_object_invariant
4100 static const struct lu_device_operations osd_lu_ops = {
4101 .ldo_object_alloc = osd_object_alloc,
4102 .ldo_process_config = osd_process_config,
4103 .ldo_recovery_complete = osd_recovery_complete,
4104 .ldo_prepare = osd_prepare,
4107 static const struct lu_device_type_operations osd_device_type_ops = {
4108 .ldto_init = osd_type_init,
4109 .ldto_fini = osd_type_fini,
4111 .ldto_start = osd_type_start,
4112 .ldto_stop = osd_type_stop,
4114 .ldto_device_alloc = osd_device_alloc,
4115 .ldto_device_free = osd_device_free,
4117 .ldto_device_init = osd_device_init,
4118 .ldto_device_fini = osd_device_fini
4121 static struct lu_device_type osd_device_type = {
4122 .ldt_tags = LU_DEVICE_DT,
4123 .ldt_name = LUSTRE_OSD_NAME,
4124 .ldt_ops = &osd_device_type_ops,
4125 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4129 * lprocfs legacy support.
4131 static struct obd_ops osd_obd_device_ops = {
4132 .o_owner = THIS_MODULE
4135 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4136 .llod_name = remote_obj_dir,
4137 .llod_oid = OSD_REM_OBJ_DIR_OID,
4139 .llod_feat = &dt_directory_features,
4142 static int __init osd_mod_init(void)
4144 struct lprocfs_static_vars lvars;
4147 llo_local_obj_register(&llod_osd_rem_obj_dir);
4148 lprocfs_osd_init_vars(&lvars);
4149 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4150 LUSTRE_OSD_NAME, &osd_device_type);
4153 static void __exit osd_mod_exit(void)
4155 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4156 class_unregister_type(LUSTRE_OSD_NAME);
4159 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4160 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4161 MODULE_LICENSE("GPL");
4163 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);