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.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include <linux/module.h>
51 /* LUSTRE_VERSION_CODE */
52 #include <lustre_ver.h>
53 /* prerequisite for linux/xattr.h */
54 #include <linux/types.h>
55 /* prerequisite for linux/xattr.h */
57 /* XATTR_{REPLACE,CREATE} */
58 #include <linux/xattr.h>
63 * struct OBD_{ALLOC,FREE}*()
66 #include <obd_support.h>
67 /* struct ptlrpc_thread */
68 #include <lustre_net.h>
71 #include <lustre_fid.h>
73 #include "osd_internal.h"
76 /* llo_* api support */
77 #include <md_object.h>
79 static const char dot[] = ".";
80 static const char dotdot[] = "..";
81 static const char remote_obj_dir[] = "REM_OBJ_DIR";
83 struct osd_directory {
84 struct iam_container od_container;
85 struct iam_descr od_descr;
89 struct dt_object oo_dt;
91 * Inode for file system object represented by this osd_object. This
92 * inode is pinned for the whole duration of lu_object life.
94 * Not modified concurrently (either setup early during object
95 * creation, or assigned by osd_object_create() under write lock).
97 struct inode *oo_inode;
99 * to protect index ops.
101 cfs_rw_semaphore_t oo_ext_idx_sem;
102 cfs_rw_semaphore_t oo_sem;
103 struct osd_directory *oo_dir;
104 /** protects inode attributes. */
105 cfs_spinlock_t oo_guard;
107 * Following two members are used to indicate the presence of dot and
108 * dotdot in the given directory. This is required for interop mode
111 int oo_compat_dot_created;
112 int oo_compat_dotdot_created;
114 const struct lu_env *oo_owner;
115 #ifdef CONFIG_LOCKDEP
116 struct lockdep_map oo_dep_map;
120 static const struct lu_object_operations osd_lu_obj_ops;
121 static const struct lu_device_operations osd_lu_ops;
122 static struct lu_context_key osd_key;
123 static const struct dt_object_operations osd_obj_ops;
124 static const struct dt_object_operations osd_obj_ea_ops;
125 static const struct dt_body_operations osd_body_ops;
126 static const struct dt_index_operations osd_index_iam_ops;
127 static const struct dt_index_operations osd_index_ea_ops;
130 struct thandle ot_super;
132 struct journal_callback ot_jcb;
133 /* Link to the device, for debugging. */
134 struct lu_ref_link *ot_dev_link;
136 #if OSD_THANDLE_STATS
137 /** time when this handle was allocated */
138 cfs_time_t oth_alloced;
140 /** time when this thanle was started */
141 cfs_time_t oth_started;
148 static int lu_device_is_osd(const struct lu_device *d)
150 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
153 static struct osd_device *osd_dt_dev(const struct dt_device *d)
155 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
156 return container_of0(d, struct osd_device, od_dt_dev);
159 static struct osd_device *osd_dev(const struct lu_device *d)
161 LASSERT(lu_device_is_osd(d));
162 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
165 static struct osd_device *osd_obj2dev(const struct osd_object *o)
167 return osd_dev(o->oo_dt.do_lu.lo_dev);
170 static struct super_block *osd_sb(const struct osd_device *dev)
172 return dev->od_mount->lmi_mnt->mnt_sb;
175 static int osd_object_is_root(const struct osd_object *obj)
177 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
180 static struct osd_object *osd_obj(const struct lu_object *o)
182 LASSERT(lu_device_is_osd(o->lo_dev));
183 return container_of0(o, struct osd_object, oo_dt.do_lu);
186 static struct osd_object *osd_dt_obj(const struct dt_object *d)
188 return osd_obj(&d->do_lu);
191 static struct lu_device *osd2lu_dev(struct osd_device *osd)
193 return &osd->od_dt_dev.dd_lu_dev;
196 static journal_t *osd_journal(const struct osd_device *dev)
198 return LDISKFS_SB(osd_sb(dev))->s_journal;
201 static int osd_has_index(const struct osd_object *obj)
203 return obj->oo_dt.do_index_ops != NULL;
206 static int osd_object_invariant(const struct lu_object *l)
208 return osd_invariant(osd_obj(l));
211 #ifdef HAVE_QUOTA_SUPPORT
213 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
215 struct md_ucred *uc = md_ucred(env);
220 save->oc_uid = current_fsuid();
221 save->oc_gid = current_fsgid();
222 save->oc_cap = current_cap();
223 if ((tc = prepare_creds())) {
224 tc->fsuid = uc->mu_fsuid;
225 tc->fsgid = uc->mu_fsgid;
228 /* XXX not suboptimal */
229 cfs_curproc_cap_unpack(uc->mu_cap);
233 osd_pop_ctxt(struct osd_ctxt *save)
237 if ((tc = prepare_creds())) {
238 tc->fsuid = save->oc_uid;
239 tc->fsgid = save->oc_gid;
240 tc->cap_effective = save->oc_cap;
246 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
248 return lu_context_key_get(&env->le_ctx, &osd_key);
252 * Concurrency: doesn't matter
254 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
256 return osd_oti_get(env)->oti_r_locks > 0;
260 * Concurrency: doesn't matter
262 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
264 struct osd_thread_info *oti = osd_oti_get(env);
265 return oti->oti_w_locks > 0 && o->oo_owner == env;
269 * Concurrency: doesn't access mutable data
271 static int osd_root_get(const struct lu_env *env,
272 struct dt_device *dev, struct lu_fid *f)
276 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
277 LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
282 * OSD object methods.
286 * Concurrency: no concurrent access is possible that early in object
289 static struct lu_object *osd_object_alloc(const struct lu_env *env,
290 const struct lu_object_header *hdr,
293 struct osd_object *mo;
299 l = &mo->oo_dt.do_lu;
300 dt_object_init(&mo->oo_dt, NULL, d);
301 if (osd_dev(d)->od_iop_mode)
302 mo->oo_dt.do_ops = &osd_obj_ea_ops;
304 mo->oo_dt.do_ops = &osd_obj_ops;
306 l->lo_ops = &osd_lu_obj_ops;
307 cfs_init_rwsem(&mo->oo_sem);
308 cfs_init_rwsem(&mo->oo_ext_idx_sem);
309 cfs_spin_lock_init(&mo->oo_guard);
316 * retrieve object from backend ext fs.
318 static struct inode *osd_iget(struct osd_thread_info *info,
319 struct osd_device *dev,
320 const struct osd_inode_id *id)
322 struct inode *inode = NULL;
324 #ifdef HAVE_EXT4_LDISKFS
325 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
327 /* Newer kernels return an error instead of a NULL pointer */
330 inode = iget(osd_sb(dev), id->oii_ino);
333 CERROR("no inode\n");
334 inode = ERR_PTR(-EACCES);
335 } else if (id->oii_gen != OSD_OII_NOGEN &&
336 inode->i_generation != id->oii_gen) {
338 inode = ERR_PTR(-ESTALE);
339 } else if (inode->i_nlink == 0) {
340 /* due to parallel readdir and unlink,
341 * we can have dead inode here. */
342 CWARN("stale inode\n");
343 make_bad_inode(inode);
345 inode = ERR_PTR(-ESTALE);
346 } else if (is_bad_inode(inode)) {
347 CERROR("bad inode %lx\n",inode->i_ino);
349 inode = ERR_PTR(-ENOENT);
351 /* Do not update file c/mtime in ldiskfs.
352 * NB: we don't have any lock to protect this because we don't
353 * have reference on osd_object now, but contention with
354 * another lookup + attr_set can't happen in the tiny window
355 * between if (...) and set S_NOCMTIME. */
356 if (!(inode->i_flags & S_NOCMTIME))
357 inode->i_flags |= S_NOCMTIME;
362 static int osd_fid_lookup(const struct lu_env *env,
363 struct osd_object *obj, const struct lu_fid *fid)
365 struct osd_thread_info *info;
366 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
367 struct osd_device *dev;
368 struct osd_inode_id *id;
373 LINVRNT(osd_invariant(obj));
374 LASSERT(obj->oo_inode == NULL);
375 LASSERT(fid_is_sane(fid) || osd_fid_is_root(fid));
377 * This assertion checks that osd layer sees only local
378 * fids. Unfortunately it is somewhat expensive (does a
379 * cache-lookup). Disabling it for production/acceptance-testing.
381 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
385 info = osd_oti_get(env);
390 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
393 result = osd_oi_lookup(info, oi, fid, id);
395 inode = osd_iget(info, dev, id);
396 if (!IS_ERR(inode)) {
397 obj->oo_inode = inode;
398 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
399 if (dev->od_iop_mode) {
400 obj->oo_compat_dot_created = 1;
401 obj->oo_compat_dotdot_created = 1;
406 * If fid wasn't found in oi, inode-less object is
407 * created, for which lu_object_exists() returns
408 * false. This is used in a (frequent) case when
409 * objects are created as locking anchors or
410 * place holders for objects yet to be created.
412 result = PTR_ERR(inode);
413 } else if (result == -ENOENT)
415 LINVRNT(osd_invariant(obj));
421 * Concurrency: shouldn't matter.
423 static void osd_object_init0(struct osd_object *obj)
425 LASSERT(obj->oo_inode != NULL);
426 obj->oo_dt.do_body_ops = &osd_body_ops;
427 obj->oo_dt.do_lu.lo_header->loh_attr |=
428 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
432 * Concurrency: no concurrent access is possible that early in object
435 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
436 const struct lu_object_conf *unused)
438 struct osd_object *obj = osd_obj(l);
441 LINVRNT(osd_invariant(obj));
443 result = osd_fid_lookup(env, obj, lu_object_fid(l));
445 if (obj->oo_inode != NULL)
446 osd_object_init0(obj);
448 LINVRNT(osd_invariant(obj));
453 * Concurrency: no concurrent access is possible that late in object
456 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
458 struct osd_object *obj = osd_obj(l);
460 LINVRNT(osd_invariant(obj));
462 dt_object_fini(&obj->oo_dt);
469 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
470 const struct iam_container *bag)
472 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
473 osd_oti_get(env)->oti_it_ipd);
476 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
477 const struct iam_container *bag)
479 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
480 osd_oti_get(env)->oti_idx_ipd);
483 static void osd_ipd_put(const struct lu_env *env,
484 const struct iam_container *bag,
485 struct iam_path_descr *ipd)
487 bag->ic_descr->id_ops->id_ipd_free(ipd);
491 * Concurrency: no concurrent access is possible that late in object
494 static void osd_index_fini(struct osd_object *o)
496 struct iam_container *bag;
498 if (o->oo_dir != NULL) {
499 bag = &o->oo_dir->od_container;
500 if (o->oo_inode != NULL) {
501 if (bag->ic_object == o->oo_inode)
502 iam_container_fini(bag);
504 OBD_FREE_PTR(o->oo_dir);
510 * Concurrency: no concurrent access is possible that late in object
511 * life-cycle (for all existing callers, that is. New callers have to provide
512 * their own locking.)
514 static int osd_inode_unlinked(const struct inode *inode)
516 return inode->i_nlink == 0;
520 OSD_TXN_OI_DELETE_CREDITS = 20,
521 OSD_TXN_INODE_DELETE_CREDITS = 20
528 #if OSD_THANDLE_STATS
530 * Set time when the handle is allocated
532 static void osd_th_alloced(struct osd_thandle *oth)
534 oth->oth_alloced = cfs_time_current();
538 * Set time when the handle started
540 static void osd_th_started(struct osd_thandle *oth)
542 oth->oth_started = cfs_time_current();
546 * Helper function to convert time interval to microseconds packed in
547 * long int (default time units for the counter in "stats" initialized
548 * by lu_time_init() )
550 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
554 cfs_duration_usec(cfs_time_sub(end, start), &val);
555 return val.tv_sec * 1000000 + val.tv_usec;
559 * Check whether the we deal with this handle for too long.
561 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
562 cfs_time_t alloced, cfs_time_t started,
565 cfs_time_t now = cfs_time_current();
567 LASSERT(dev != NULL);
569 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
570 interval_to_usec(alloced, started));
571 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
572 interval_to_usec(started, closed));
573 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
574 interval_to_usec(closed, now));
576 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
577 CWARN("transaction handle %p was open for too long: "
579 "alloced "CFS_TIME_T" ,"
580 "started "CFS_TIME_T" ,"
581 "closed "CFS_TIME_T"\n",
582 oth, now, alloced, started, closed);
583 libcfs_debug_dumpstack(NULL);
587 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
589 cfs_time_t __closed = cfs_time_current(); \
590 cfs_time_t __alloced = oth->oth_alloced; \
591 cfs_time_t __started = oth->oth_started; \
594 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
597 #else /* OSD_THANDLE_STATS */
599 #define osd_th_alloced(h) do {} while(0)
600 #define osd_th_started(h) do {} while(0)
601 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
603 #endif /* OSD_THANDLE_STATS */
606 * Concurrency: doesn't access mutable data.
608 static int osd_param_is_sane(const struct osd_device *dev,
609 const struct txn_param *param)
611 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
615 * Concurrency: shouldn't matter.
617 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
619 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
620 struct thandle *th = &oh->ot_super;
621 struct dt_device *dev = th->th_dev;
622 struct lu_device *lud = &dev->dd_lu_dev;
624 LASSERT(dev != NULL);
625 LASSERT(oh->ot_handle == NULL);
628 CERROR("transaction @0x%p commit error: %d\n", th, error);
630 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
632 * This od_env_for_commit is only for commit usage. see
635 lu_context_enter(&env->le_ctx);
636 dt_txn_hook_commit(env, th);
637 lu_context_exit(&env->le_ctx);
640 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
644 lu_context_exit(&th->th_ctx);
645 lu_context_fini(&th->th_ctx);
650 * Concurrency: shouldn't matter.
652 static struct thandle *osd_trans_start(const struct lu_env *env,
656 struct osd_device *dev = osd_dt_dev(d);
658 struct osd_thandle *oh;
664 hook_res = dt_txn_hook_start(env, d, p);
666 RETURN(ERR_PTR(hook_res));
668 if (osd_param_is_sane(dev, p)) {
669 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
671 struct osd_thread_info *oti = osd_oti_get(env);
674 * XXX temporary stuff. Some abstraction layer should
679 jh = ldiskfs_journal_start_sb(osd_sb(dev), p->tp_credits);
686 jh->h_sync = p->tp_sync;
687 lu_device_get(&d->dd_lu_dev);
688 oh->ot_dev_link = lu_ref_add
689 (&d->dd_lu_dev.ld_reference,
691 /* add commit callback */
692 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
693 lu_context_enter(&th->th_ctx);
694 osd_journal_callback_set(jh, osd_trans_commit_cb,
695 (struct journal_callback *)&oh->ot_jcb);
696 LASSERT(oti->oti_txns == 0);
697 LASSERT(oti->oti_r_locks == 0);
698 LASSERT(oti->oti_w_locks == 0);
705 th = ERR_PTR(-ENOMEM);
707 CERROR("Invalid transaction parameters\n");
708 th = ERR_PTR(-EINVAL);
715 * Concurrency: shouldn't matter.
717 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
720 struct osd_thandle *oh;
721 struct osd_thread_info *oti = osd_oti_get(env);
725 oh = container_of0(th, struct osd_thandle, ot_super);
726 if (oh->ot_handle != NULL) {
727 handle_t *hdl = oh->ot_handle;
729 LASSERT(oti->oti_txns == 1);
731 LASSERT(oti->oti_r_locks == 0);
732 LASSERT(oti->oti_w_locks == 0);
733 result = dt_txn_hook_stop(env, th);
735 CERROR("Failure in transaction hook: %d\n", result);
736 oh->ot_handle = NULL;
737 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
738 result = ldiskfs_journal_stop(hdl));
740 CERROR("Failure to stop transaction: %d\n", result);
746 * Concurrency: no concurrent access is possible that late in object
749 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
751 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
752 struct osd_device *osd = osd_obj2dev(obj);
753 struct osd_thread_info *oti = osd_oti_get(env);
754 struct txn_param *prm = &oti->oti_txn;
755 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
759 lu_env_init(env_del_obj, LCT_DT_THREAD);
760 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
761 OSD_TXN_INODE_DELETE_CREDITS);
762 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
764 result = osd_oi_delete(osd_oti_get(env_del_obj),
765 &osd->od_oi, fid, th);
766 osd_trans_stop(env_del_obj, th);
768 result = PTR_ERR(th);
770 lu_env_fini(env_del_obj);
775 * Called just before object is freed. Releases all resources except for
776 * object itself (that is released by osd_object_free()).
778 * Concurrency: no concurrent access is possible that late in object
781 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
783 struct osd_object *obj = osd_obj(l);
784 struct inode *inode = obj->oo_inode;
786 LINVRNT(osd_invariant(obj));
789 * If object is unlinked remove fid->ino mapping from object index.
796 if (osd_inode_unlinked(inode)) {
797 result = osd_inode_remove(env, obj);
799 LU_OBJECT_DEBUG(D_ERROR, env, l,
800 "Failed to cleanup: %d\n",
805 obj->oo_inode = NULL;
810 * Concurrency: ->loo_object_release() is called under site spin-lock.
812 static void osd_object_release(const struct lu_env *env,
815 struct osd_object *o = osd_obj(l);
817 LASSERT(!lu_object_is_dying(l->lo_header));
818 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
819 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
823 * Concurrency: shouldn't matter.
825 static int osd_object_print(const struct lu_env *env, void *cookie,
826 lu_printer_t p, const struct lu_object *l)
828 struct osd_object *o = osd_obj(l);
831 if (o->oo_dir != NULL)
832 d = o->oo_dir->od_container.ic_descr;
835 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
837 o->oo_inode ? o->oo_inode->i_ino : 0UL,
838 o->oo_inode ? o->oo_inode->i_generation : 0,
839 d ? d->id_ops->id_name : "plain");
843 * Concurrency: shouldn't matter.
845 int osd_statfs(const struct lu_env *env, struct dt_device *d,
848 struct osd_device *osd = osd_dt_dev(d);
849 struct super_block *sb = osd_sb(osd);
852 cfs_spin_lock(&osd->od_osfs_lock);
854 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
855 result = ll_do_statfs(sb, &osd->od_kstatfs);
856 if (likely(result == 0)) /* N.B. statfs can't really fail */
857 osd->od_osfs_age = cfs_time_current_64();
860 if (likely(result == 0))
861 *sfs = osd->od_kstatfs;
862 cfs_spin_unlock(&osd->od_osfs_lock);
868 * Concurrency: doesn't access mutable data.
870 static void osd_conf_get(const struct lu_env *env,
871 const struct dt_device *dev,
872 struct dt_device_param *param)
875 * XXX should be taken from not-yet-existing fs abstraction layer.
877 param->ddp_max_name_len = LDISKFS_NAME_LEN;
878 param->ddp_max_nlink = LDISKFS_LINK_MAX;
879 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
883 * Helper function to get and fill the buffer with input values.
885 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
889 buf = &osd_oti_get(env)->oti_buf;
896 * Concurrency: shouldn't matter.
898 static int osd_sync(const struct lu_env *env, struct dt_device *d)
900 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
901 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
905 * Start commit for OSD device.
907 * An implementation of dt_commit_async method for OSD device.
908 * Asychronously starts underlayng fs sync and thereby a transaction
911 * \param env environment
914 * \see dt_device_operations
916 static int osd_commit_async(const struct lu_env *env,
919 struct super_block *s = osd_sb(osd_dt_dev(d));
922 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
923 RETURN(s->s_op->sync_fs(s, 0));
927 * Concurrency: shouldn't matter.
929 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
931 static void osd_ro(const struct lu_env *env, struct dt_device *d)
935 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
937 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
938 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
944 * Concurrency: serialization provided by callers.
946 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
947 int mode, unsigned long timeout, __u32 alg,
948 struct lustre_capa_key *keys)
950 struct osd_device *dev = osd_dt_dev(d);
953 dev->od_fl_capa = mode;
954 dev->od_capa_timeout = timeout;
955 dev->od_capa_alg = alg;
956 dev->od_capa_keys = keys;
961 * Concurrency: serialization provided by callers.
963 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
964 struct dt_quota_ctxt *ctxt, void *data)
966 struct obd_device *obd = (void *)ctxt;
967 struct vfsmount *mnt = (struct vfsmount *)data;
970 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
971 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
972 obd->obd_lvfs_ctxt.pwdmnt = mnt;
973 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
974 obd->obd_lvfs_ctxt.fs = get_ds();
980 * Note: we do not count into QUOTA here.
981 * If we mount with --data_journal we may need more.
983 static const int osd_dto_credits_noquota[DTO_NR] = {
986 * INDEX_EXTRA_TRANS_BLOCKS(8) +
987 * SINGLEDATA_TRANS_BLOCKS(8)
988 * XXX Note: maybe iam need more, since iam have more level than
991 [DTO_INDEX_INSERT] = 16,
992 [DTO_INDEX_DELETE] = 16,
996 [DTO_IDNEX_UPDATE] = 16,
998 * Create a object. The same as create object in EXT3.
999 * DATA_TRANS_BLOCKS(14) +
1000 * INDEX_EXTRA_BLOCKS(8) +
1001 * 3(inode bits, groups, GDT)
1003 [DTO_OBJECT_CREATE] = 25,
1007 [DTO_OBJECT_DELETE] = 25,
1010 * 3(inode bits, group, GDT)
1012 [DTO_ATTR_SET_BASE] = 3,
1014 * Xattr set. The same as xattr of EXT3.
1015 * DATA_TRANS_BLOCKS(14)
1016 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1017 * are also counted in. Do not know why?
1019 [DTO_XATTR_SET] = 14,
1022 * creadits for inode change during write.
1024 [DTO_WRITE_BASE] = 3,
1026 * credits for single block write.
1028 [DTO_WRITE_BLOCK] = 14,
1030 * Attr set credits for chown.
1031 * This is extra credits for setattr, and it is null without quota
1033 [DTO_ATTR_SET_CHOWN]= 0
1037 * Note: we count into QUOTA here.
1038 * If we mount with --data_journal we may need more.
1040 static const int osd_dto_credits_quota[DTO_NR] = {
1042 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1043 * SINGLEDATA_TRANS_BLOCKS(8) +
1044 * 2 * QUOTA_TRANS_BLOCKS(2)
1046 [DTO_INDEX_INSERT] = 20,
1048 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1049 * SINGLEDATA_TRANS_BLOCKS(8) +
1050 * 2 * QUOTA_TRANS_BLOCKS(2)
1052 [DTO_INDEX_DELETE] = 20,
1056 [DTO_IDNEX_UPDATE] = 16,
1058 * Create a object. Same as create object in EXT3 filesystem.
1059 * DATA_TRANS_BLOCKS(16) +
1060 * INDEX_EXTRA_BLOCKS(8) +
1061 * 3(inode bits, groups, GDT) +
1062 * 2 * QUOTA_INIT_BLOCKS(25)
1064 [DTO_OBJECT_CREATE] = 77,
1067 * DATA_TRANS_BLOCKS(16) +
1068 * INDEX_EXTRA_BLOCKS(8) +
1069 * 3(inode bits, groups, GDT) +
1072 [DTO_OBJECT_DELETE] = 27,
1075 * 3 (inode bit, group, GDT) +
1077 [DTO_ATTR_SET_BASE] = 3,
1079 * Xattr set. The same as xattr of EXT3.
1080 * DATA_TRANS_BLOCKS(16)
1081 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
1082 * also counted in. Do not know why?
1084 [DTO_XATTR_SET] = 16,
1087 * creadits for inode change during write.
1089 [DTO_WRITE_BASE] = 3,
1091 * credits for single block write.
1093 [DTO_WRITE_BLOCK] = 16,
1095 * Attr set credits for chown.
1096 * It is added to already set setattr credits
1097 * 2 * QUOTA_INIT_BLOCKS(25) +
1098 * 2 * QUOTA_DEL_BLOCKS(9)
1100 [DTO_ATTR_SET_CHOWN]= 68,
1103 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1106 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1107 ARRAY_SIZE(osd_dto_credits_quota));
1108 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1109 #ifdef HAVE_QUOTA_SUPPORT
1110 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1111 return osd_dto_credits_quota[op];
1114 return osd_dto_credits_noquota[op];
1117 static const struct dt_device_operations osd_dt_ops = {
1118 .dt_root_get = osd_root_get,
1119 .dt_statfs = osd_statfs,
1120 .dt_trans_start = osd_trans_start,
1121 .dt_trans_stop = osd_trans_stop,
1122 .dt_conf_get = osd_conf_get,
1123 .dt_sync = osd_sync,
1125 .dt_commit_async = osd_commit_async,
1126 .dt_credit_get = osd_credit_get,
1127 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1128 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1131 static void osd_object_read_lock(const struct lu_env *env,
1132 struct dt_object *dt, unsigned role)
1134 struct osd_object *obj = osd_dt_obj(dt);
1135 struct osd_thread_info *oti = osd_oti_get(env);
1137 LINVRNT(osd_invariant(obj));
1139 LASSERT(obj->oo_owner != env);
1140 cfs_down_read_nested(&obj->oo_sem, role);
1142 LASSERT(obj->oo_owner == NULL);
1146 static void osd_object_write_lock(const struct lu_env *env,
1147 struct dt_object *dt, unsigned role)
1149 struct osd_object *obj = osd_dt_obj(dt);
1150 struct osd_thread_info *oti = osd_oti_get(env);
1152 LINVRNT(osd_invariant(obj));
1154 LASSERT(obj->oo_owner != env);
1155 cfs_down_write_nested(&obj->oo_sem, role);
1157 LASSERT(obj->oo_owner == NULL);
1158 obj->oo_owner = env;
1162 static void osd_object_read_unlock(const struct lu_env *env,
1163 struct dt_object *dt)
1165 struct osd_object *obj = osd_dt_obj(dt);
1166 struct osd_thread_info *oti = osd_oti_get(env);
1168 LINVRNT(osd_invariant(obj));
1170 LASSERT(oti->oti_r_locks > 0);
1172 cfs_up_read(&obj->oo_sem);
1175 static void osd_object_write_unlock(const struct lu_env *env,
1176 struct dt_object *dt)
1178 struct osd_object *obj = osd_dt_obj(dt);
1179 struct osd_thread_info *oti = osd_oti_get(env);
1181 LINVRNT(osd_invariant(obj));
1183 LASSERT(obj->oo_owner == env);
1184 LASSERT(oti->oti_w_locks > 0);
1186 obj->oo_owner = NULL;
1187 cfs_up_write(&obj->oo_sem);
1190 static int osd_object_write_locked(const struct lu_env *env,
1191 struct dt_object *dt)
1193 struct osd_object *obj = osd_dt_obj(dt);
1195 LINVRNT(osd_invariant(obj));
1197 return obj->oo_owner == env;
1200 static int capa_is_sane(const struct lu_env *env,
1201 struct osd_device *dev,
1202 struct lustre_capa *capa,
1203 struct lustre_capa_key *keys)
1205 struct osd_thread_info *oti = osd_oti_get(env);
1206 struct lustre_capa *tcapa = &oti->oti_capa;
1207 struct obd_capa *oc;
1211 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1213 if (capa_is_expired(oc)) {
1214 DEBUG_CAPA(D_ERROR, capa, "expired");
1221 if (capa_is_expired_sec(capa)) {
1222 DEBUG_CAPA(D_ERROR, capa, "expired");
1226 cfs_spin_lock(&capa_lock);
1227 for (i = 0; i < 2; i++) {
1228 if (keys[i].lk_keyid == capa->lc_keyid) {
1229 oti->oti_capa_key = keys[i];
1233 cfs_spin_unlock(&capa_lock);
1236 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1240 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1244 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1245 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1249 oc = capa_add(dev->od_capa_hash, capa);
1255 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1256 struct lustre_capa *capa, __u64 opc)
1258 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1259 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1260 struct md_capainfo *ci;
1263 if (!dev->od_fl_capa)
1266 if (capa == BYPASS_CAPA)
1269 ci = md_capainfo(env);
1273 if (ci->mc_auth == LC_ID_NONE)
1277 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1281 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1282 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1287 if (!capa_opc_supported(capa, opc)) {
1288 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1292 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1293 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1300 static struct timespec *osd_inode_time(const struct lu_env *env,
1301 struct inode *inode, __u64 seconds)
1303 struct osd_thread_info *oti = osd_oti_get(env);
1304 struct timespec *t = &oti->oti_time;
1306 t->tv_sec = seconds;
1308 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1313 static void osd_inode_getattr(const struct lu_env *env,
1314 struct inode *inode, struct lu_attr *attr)
1316 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1317 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1318 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1320 attr->la_atime = LTIME_S(inode->i_atime);
1321 attr->la_mtime = LTIME_S(inode->i_mtime);
1322 attr->la_ctime = LTIME_S(inode->i_ctime);
1323 attr->la_mode = inode->i_mode;
1324 attr->la_size = i_size_read(inode);
1325 attr->la_blocks = inode->i_blocks;
1326 attr->la_uid = inode->i_uid;
1327 attr->la_gid = inode->i_gid;
1328 attr->la_flags = LDISKFS_I(inode)->i_flags;
1329 attr->la_nlink = inode->i_nlink;
1330 attr->la_rdev = inode->i_rdev;
1331 attr->la_blksize = ll_inode_blksize(inode);
1332 attr->la_blkbits = inode->i_blkbits;
1335 static int osd_attr_get(const struct lu_env *env,
1336 struct dt_object *dt,
1337 struct lu_attr *attr,
1338 struct lustre_capa *capa)
1340 struct osd_object *obj = osd_dt_obj(dt);
1342 LASSERT(dt_object_exists(dt));
1343 LINVRNT(osd_invariant(obj));
1345 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1348 cfs_spin_lock(&obj->oo_guard);
1349 osd_inode_getattr(env, obj->oo_inode, attr);
1350 cfs_spin_unlock(&obj->oo_guard);
1354 static int osd_inode_setattr(const struct lu_env *env,
1355 struct inode *inode, const struct lu_attr *attr)
1359 bits = attr->la_valid;
1361 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1363 #ifdef HAVE_QUOTA_SUPPORT
1364 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1365 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1366 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1372 iattr.ia_valid |= ATTR_UID;
1374 iattr.ia_valid |= ATTR_GID;
1375 iattr.ia_uid = attr->la_uid;
1376 iattr.ia_gid = attr->la_gid;
1377 osd_push_ctxt(env, save);
1378 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1385 if (bits & LA_ATIME)
1386 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1387 if (bits & LA_CTIME)
1388 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1389 if (bits & LA_MTIME)
1390 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1391 if (bits & LA_SIZE) {
1392 LDISKFS_I(inode)->i_disksize = attr->la_size;
1393 i_size_write(inode, attr->la_size);
1397 /* OSD should not change "i_blocks" which is used by quota.
1398 * "i_blocks" should be changed by ldiskfs only. */
1399 if (bits & LA_BLOCKS)
1400 inode->i_blocks = attr->la_blocks;
1403 inode->i_mode = (inode->i_mode & S_IFMT) |
1404 (attr->la_mode & ~S_IFMT);
1406 inode->i_uid = attr->la_uid;
1408 inode->i_gid = attr->la_gid;
1409 if (bits & LA_NLINK)
1410 inode->i_nlink = attr->la_nlink;
1412 inode->i_rdev = attr->la_rdev;
1414 if (bits & LA_FLAGS) {
1415 /* always keep S_NOCMTIME */
1416 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1422 static int osd_attr_set(const struct lu_env *env,
1423 struct dt_object *dt,
1424 const struct lu_attr *attr,
1425 struct thandle *handle,
1426 struct lustre_capa *capa)
1428 struct osd_object *obj = osd_dt_obj(dt);
1431 LASSERT(handle != NULL);
1432 LASSERT(dt_object_exists(dt));
1433 LASSERT(osd_invariant(obj));
1435 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1438 cfs_spin_lock(&obj->oo_guard);
1439 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1440 cfs_spin_unlock(&obj->oo_guard);
1443 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1450 * XXX temporary solution.
1452 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1453 struct lu_attr *attr, struct thandle *th)
1458 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1459 struct lu_attr *attr, struct thandle *th)
1461 osd_object_init0(obj);
1462 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1463 unlock_new_inode(obj->oo_inode);
1467 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1468 struct osd_object *obj,
1472 struct osd_thread_info *info = osd_oti_get(env);
1473 struct dentry *child_dentry = &info->oti_child_dentry;
1474 struct dentry *obj_dentry = &info->oti_obj_dentry;
1476 obj_dentry->d_inode = obj->oo_inode;
1477 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1478 obj_dentry->d_name.hash = 0;
1480 child_dentry->d_name.hash = 0;
1481 child_dentry->d_parent = obj_dentry;
1482 child_dentry->d_name.name = name;
1483 child_dentry->d_name.len = namelen;
1484 return child_dentry;
1488 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1490 struct dt_allocation_hint *hint,
1494 struct osd_device *osd = osd_obj2dev(obj);
1495 struct osd_thandle *oth;
1496 struct dt_object *parent;
1497 struct inode *inode;
1498 #ifdef HAVE_QUOTA_SUPPORT
1499 struct osd_ctxt *save = &info->oti_ctxt;
1502 LINVRNT(osd_invariant(obj));
1503 LASSERT(obj->oo_inode == NULL);
1505 oth = container_of(th, struct osd_thandle, ot_super);
1506 LASSERT(oth->ot_handle->h_transaction != NULL);
1508 if (hint && hint->dah_parent)
1509 parent = hint->dah_parent;
1511 parent = osd->od_obj_area;
1513 LASSERT(parent != NULL);
1514 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1516 #ifdef HAVE_QUOTA_SUPPORT
1517 osd_push_ctxt(info->oti_env, save);
1519 inode = ldiskfs_create_inode(oth->ot_handle,
1520 osd_dt_obj(parent)->oo_inode, mode);
1521 #ifdef HAVE_QUOTA_SUPPORT
1524 if (!IS_ERR(inode)) {
1525 /* Do not update file c/mtime in ldiskfs.
1526 * NB: don't need any lock because no contention at this
1528 inode->i_flags |= S_NOCMTIME;
1529 obj->oo_inode = inode;
1532 result = PTR_ERR(inode);
1533 LINVRNT(osd_invariant(obj));
1541 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1542 struct lu_attr *attr,
1543 struct dt_allocation_hint *hint,
1544 struct dt_object_format *dof,
1548 struct osd_thandle *oth;
1549 struct osd_device *osd = osd_obj2dev(obj);
1550 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1552 LASSERT(S_ISDIR(attr->la_mode));
1554 oth = container_of(th, struct osd_thandle, ot_super);
1555 LASSERT(oth->ot_handle->h_transaction != NULL);
1556 result = osd_mkfile(info, obj, mode, hint, th);
1557 if (result == 0 && osd->od_iop_mode == 0) {
1558 LASSERT(obj->oo_inode != NULL);
1560 * XXX uh-oh... call low-level iam function directly.
1563 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1564 sizeof (struct osd_fid_pack),
1570 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1571 struct lu_attr *attr,
1572 struct dt_allocation_hint *hint,
1573 struct dt_object_format *dof,
1577 struct osd_thandle *oth;
1578 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1580 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1582 LASSERT(S_ISREG(attr->la_mode));
1584 oth = container_of(th, struct osd_thandle, ot_super);
1585 LASSERT(oth->ot_handle->h_transaction != NULL);
1587 result = osd_mkfile(info, obj, mode, hint, th);
1589 LASSERT(obj->oo_inode != NULL);
1590 if (feat->dif_flags & DT_IND_VARKEY)
1591 result = iam_lvar_create(obj->oo_inode,
1592 feat->dif_keysize_max,
1594 feat->dif_recsize_max,
1597 result = iam_lfix_create(obj->oo_inode,
1598 feat->dif_keysize_max,
1600 feat->dif_recsize_max,
1607 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1608 struct lu_attr *attr,
1609 struct dt_allocation_hint *hint,
1610 struct dt_object_format *dof,
1613 LASSERT(S_ISREG(attr->la_mode));
1614 return osd_mkfile(info, obj, (attr->la_mode &
1615 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1618 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1619 struct lu_attr *attr,
1620 struct dt_allocation_hint *hint,
1621 struct dt_object_format *dof,
1624 LASSERT(S_ISLNK(attr->la_mode));
1625 return osd_mkfile(info, obj, (attr->la_mode &
1626 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1629 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1630 struct lu_attr *attr,
1631 struct dt_allocation_hint *hint,
1632 struct dt_object_format *dof,
1635 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1638 LINVRNT(osd_invariant(obj));
1639 LASSERT(obj->oo_inode == NULL);
1640 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1641 S_ISFIFO(mode) || S_ISSOCK(mode));
1643 result = osd_mkfile(info, obj, mode, hint, th);
1645 LASSERT(obj->oo_inode != NULL);
1646 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1648 LINVRNT(osd_invariant(obj));
1652 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1654 struct dt_allocation_hint *hint,
1655 struct dt_object_format *dof,
1658 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1660 osd_obj_type_f result;
1676 result = osd_mk_index;
1687 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1688 struct dt_object *parent, cfs_umode_t child_mode)
1692 memset(ah, 0, sizeof(*ah));
1693 ah->dah_parent = parent;
1694 ah->dah_mode = child_mode;
1698 * Helper function for osd_object_create()
1700 * \retval 0, on success
1702 static int __osd_object_create(struct osd_thread_info *info,
1703 struct osd_object *obj, struct lu_attr *attr,
1704 struct dt_allocation_hint *hint,
1705 struct dt_object_format *dof,
1711 result = osd_create_pre(info, obj, attr, th);
1713 result = osd_create_type_f(dof->dof_type)(info, obj,
1714 attr, hint, dof, th);
1716 result = osd_create_post(info, obj, attr, th);
1722 * Helper function for osd_object_create()
1724 * \retval 0, on success
1726 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1727 const struct lu_fid *fid, struct thandle *th)
1729 struct osd_thread_info *info = osd_oti_get(env);
1730 struct osd_inode_id *id = &info->oti_id;
1731 struct osd_device *osd = osd_obj2dev(obj);
1732 struct md_ucred *uc = md_ucred(env);
1734 LASSERT(obj->oo_inode != NULL);
1735 LASSERT(uc != NULL);
1737 id->oii_ino = obj->oo_inode->i_ino;
1738 id->oii_gen = obj->oo_inode->i_generation;
1740 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1741 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1744 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1745 struct lu_attr *attr,
1746 struct dt_allocation_hint *hint,
1747 struct dt_object_format *dof,
1750 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1751 struct osd_object *obj = osd_dt_obj(dt);
1752 struct osd_thread_info *info = osd_oti_get(env);
1757 LINVRNT(osd_invariant(obj));
1758 LASSERT(!dt_object_exists(dt));
1759 LASSERT(osd_write_locked(env, obj));
1760 LASSERT(th != NULL);
1762 result = __osd_object_create(info, obj, attr, hint, dof, th);
1764 result = __osd_oi_insert(env, obj, fid, th);
1766 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1767 LASSERT(osd_invariant(obj));
1772 * Helper function for osd_xattr_set()
1774 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1775 const struct lu_buf *buf, const char *name, int fl)
1777 struct osd_object *obj = osd_dt_obj(dt);
1778 struct inode *inode = obj->oo_inode;
1779 struct osd_thread_info *info = osd_oti_get(env);
1780 struct dentry *dentry = &info->oti_child_dentry;
1784 LASSERT(dt_object_exists(dt));
1785 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1786 LASSERT(osd_write_locked(env, obj));
1788 if (fl & LU_XATTR_REPLACE)
1789 fs_flags |= XATTR_REPLACE;
1791 if (fl & LU_XATTR_CREATE)
1792 fs_flags |= XATTR_CREATE;
1794 dentry->d_inode = inode;
1795 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1796 buf->lb_len, fs_flags);
1801 * Put the fid into lustre_mdt_attrs, and then place the structure
1802 * inode's ea. This fid should not be altered during the life time
1805 * \retval +ve, on success
1806 * \retval -ve, on error
1808 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1810 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1811 const struct lu_fid *fid)
1813 struct osd_thread_info *info = osd_oti_get(env);
1814 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1816 lustre_lma_init(mdt_attrs, fid);
1817 lustre_lma_swab(mdt_attrs);
1818 return __osd_xattr_set(env, dt,
1819 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1820 XATTR_NAME_LMA, LU_XATTR_CREATE);
1825 * Helper function to form igif
1827 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1830 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1834 * Helper function to pack the fid, ldiskfs stores fid in packed format.
1836 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1837 struct lu_fid *befider)
1839 fid_cpu_to_be(befider, (struct lu_fid *)fid);
1840 memcpy(pack->fp_area, befider, sizeof(*befider));
1841 pack->fp_len = sizeof(*befider) + 1;
1845 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1846 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1847 * To have compatilibility with 1.8 ldiskfs driver we need to have
1848 * magic number at start of fid data.
1849 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1852 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1853 const struct dt_rec *fid)
1855 param->edp_magic = LDISKFS_LUFID_MAGIC;
1856 param->edp_len = sizeof(struct lu_fid) + 1;
1858 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1859 (struct lu_fid *)fid);
1862 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1867 switch (pack->fp_len) {
1868 case sizeof *fid + 1:
1869 memcpy(fid, pack->fp_area, sizeof *fid);
1870 fid_be_to_cpu(fid, fid);
1873 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1880 * Try to read the fid from inode ea into dt_rec, if return value
1881 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1883 * \param fid object fid.
1885 * \retval 0 on success
1887 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1888 __u32 ino, struct lu_fid *fid)
1890 struct osd_thread_info *info = osd_oti_get(env);
1891 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1892 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1893 struct dentry *dentry = &info->oti_child_dentry;
1894 struct osd_inode_id *id = &info->oti_id;
1895 struct osd_device *dev;
1896 struct inode *inode;
1900 dev = osd_dev(ldev);
1903 id->oii_gen = OSD_OII_NOGEN;
1905 inode = osd_iget(info, dev, id);
1906 if (IS_ERR(inode)) {
1907 rc = PTR_ERR(inode);
1910 dentry->d_inode = inode;
1912 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1913 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1916 /* Check LMA compatibility */
1918 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
1919 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1920 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
1921 ~LMA_INCOMPAT_SUPP);
1926 lustre_lma_swab(mdt_attrs);
1927 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
1929 } else if (rc == -ENODATA) {
1930 osd_igif_get(env, inode, fid);
1939 * OSD layer object create function for interoperability mode (b11826).
1940 * This is mostly similar to osd_object_create(). Only difference being, fid is
1941 * inserted into inode ea here.
1943 * \retval 0, on success
1944 * \retval -ve, on error
1946 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1947 struct lu_attr *attr,
1948 struct dt_allocation_hint *hint,
1949 struct dt_object_format *dof,
1952 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1953 struct osd_object *obj = osd_dt_obj(dt);
1954 struct osd_thread_info *info = osd_oti_get(env);
1959 LASSERT(osd_invariant(obj));
1960 LASSERT(!dt_object_exists(dt));
1961 LASSERT(osd_write_locked(env, obj));
1962 LASSERT(th != NULL);
1964 result = __osd_object_create(info, obj, attr, hint, dof, th);
1966 /* objects under osd root shld have igif fid, so dont add fid EA */
1967 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
1968 result = osd_ea_fid_set(env, dt, fid);
1971 result = __osd_oi_insert(env, obj, fid, th);
1973 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1974 LINVRNT(osd_invariant(obj));
1979 * Concurrency: @dt is write locked.
1981 static void osd_object_ref_add(const struct lu_env *env,
1982 struct dt_object *dt,
1985 struct osd_object *obj = osd_dt_obj(dt);
1986 struct inode *inode = obj->oo_inode;
1988 LINVRNT(osd_invariant(obj));
1989 LASSERT(dt_object_exists(dt));
1990 LASSERT(osd_write_locked(env, obj));
1991 LASSERT(th != NULL);
1993 cfs_spin_lock(&obj->oo_guard);
1994 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1996 cfs_spin_unlock(&obj->oo_guard);
1997 inode->i_sb->s_op->dirty_inode(inode);
1998 LINVRNT(osd_invariant(obj));
2002 * Concurrency: @dt is write locked.
2004 static void osd_object_ref_del(const struct lu_env *env,
2005 struct dt_object *dt,
2008 struct osd_object *obj = osd_dt_obj(dt);
2009 struct inode *inode = obj->oo_inode;
2011 LINVRNT(osd_invariant(obj));
2012 LASSERT(dt_object_exists(dt));
2013 LASSERT(osd_write_locked(env, obj));
2014 LASSERT(th != NULL);
2016 cfs_spin_lock(&obj->oo_guard);
2017 LASSERT(inode->i_nlink > 0);
2019 cfs_spin_unlock(&obj->oo_guard);
2020 inode->i_sb->s_op->dirty_inode(inode);
2021 LINVRNT(osd_invariant(obj));
2025 * Concurrency: @dt is read locked.
2027 static int osd_xattr_get(const struct lu_env *env,
2028 struct dt_object *dt,
2031 struct lustre_capa *capa)
2033 struct osd_object *obj = osd_dt_obj(dt);
2034 struct inode *inode = obj->oo_inode;
2035 struct osd_thread_info *info = osd_oti_get(env);
2036 struct dentry *dentry = &info->oti_obj_dentry;
2038 LASSERT(dt_object_exists(dt));
2039 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2040 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2042 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2045 dentry->d_inode = inode;
2046 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2050 * Concurrency: @dt is write locked.
2052 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2053 const struct lu_buf *buf, const char *name, int fl,
2054 struct thandle *handle, struct lustre_capa *capa)
2056 LASSERT(handle != NULL);
2058 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2061 return __osd_xattr_set(env, dt, buf, name, fl);
2065 * Concurrency: @dt is read locked.
2067 static int osd_xattr_list(const struct lu_env *env,
2068 struct dt_object *dt,
2070 struct lustre_capa *capa)
2072 struct osd_object *obj = osd_dt_obj(dt);
2073 struct inode *inode = obj->oo_inode;
2074 struct osd_thread_info *info = osd_oti_get(env);
2075 struct dentry *dentry = &info->oti_obj_dentry;
2077 LASSERT(dt_object_exists(dt));
2078 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2079 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2081 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2084 dentry->d_inode = inode;
2085 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2089 * Concurrency: @dt is write locked.
2091 static int osd_xattr_del(const struct lu_env *env,
2092 struct dt_object *dt,
2094 struct thandle *handle,
2095 struct lustre_capa *capa)
2097 struct osd_object *obj = osd_dt_obj(dt);
2098 struct inode *inode = obj->oo_inode;
2099 struct osd_thread_info *info = osd_oti_get(env);
2100 struct dentry *dentry = &info->oti_obj_dentry;
2103 LASSERT(dt_object_exists(dt));
2104 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2105 LASSERT(osd_write_locked(env, obj));
2106 LASSERT(handle != NULL);
2108 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2111 dentry->d_inode = inode;
2112 rc = inode->i_op->removexattr(dentry, name);
2116 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2117 struct dt_object *dt,
2118 struct lustre_capa *old,
2121 struct osd_thread_info *info = osd_oti_get(env);
2122 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2123 struct osd_object *obj = osd_dt_obj(dt);
2124 struct osd_device *dev = osd_obj2dev(obj);
2125 struct lustre_capa_key *key = &info->oti_capa_key;
2126 struct lustre_capa *capa = &info->oti_capa;
2127 struct obd_capa *oc;
2128 struct md_capainfo *ci;
2132 if (!dev->od_fl_capa)
2133 RETURN(ERR_PTR(-ENOENT));
2135 LASSERT(dt_object_exists(dt));
2136 LINVRNT(osd_invariant(obj));
2138 /* renewal sanity check */
2139 if (old && osd_object_auth(env, dt, old, opc))
2140 RETURN(ERR_PTR(-EACCES));
2142 ci = md_capainfo(env);
2144 RETURN(ERR_PTR(-ENOENT));
2146 switch (ci->mc_auth) {
2150 capa->lc_uid = obj->oo_inode->i_uid;
2151 capa->lc_gid = obj->oo_inode->i_gid;
2152 capa->lc_flags = LC_ID_PLAIN;
2154 case LC_ID_CONVERT: {
2157 s[0] = obj->oo_inode->i_uid;
2158 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2159 s[2] = obj->oo_inode->i_gid;
2160 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2161 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2163 RETURN(ERR_PTR(rc));
2165 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2166 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2167 capa->lc_flags = LC_ID_CONVERT;
2171 RETURN(ERR_PTR(-EINVAL));
2174 capa->lc_fid = *fid;
2176 capa->lc_flags |= dev->od_capa_alg << 24;
2177 capa->lc_timeout = dev->od_capa_timeout;
2178 capa->lc_expiry = 0;
2180 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2182 LASSERT(!capa_is_expired(oc));
2186 cfs_spin_lock(&capa_lock);
2187 *key = dev->od_capa_keys[1];
2188 cfs_spin_unlock(&capa_lock);
2190 capa->lc_keyid = key->lk_keyid;
2191 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2193 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2195 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2196 RETURN(ERR_PTR(rc));
2199 oc = capa_add(dev->od_capa_hash, capa);
2203 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2206 struct osd_object *obj = osd_dt_obj(dt);
2207 struct inode *inode = obj->oo_inode;
2208 struct osd_thread_info *info = osd_oti_get(env);
2209 struct dentry *dentry = &info->oti_obj_dentry;
2210 struct file *file = &info->oti_file;
2213 dentry->d_inode = inode;
2214 file->f_dentry = dentry;
2215 file->f_mapping = inode->i_mapping;
2216 file->f_op = inode->i_fop;
2217 LOCK_INODE_MUTEX(inode);
2218 rc = file->f_op->fsync(file, dentry, 0);
2219 UNLOCK_INODE_MUTEX(inode);
2224 * Get the 64-bit version for an inode.
2226 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2227 struct dt_object *dt)
2229 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2231 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2232 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2233 return LDISKFS_I(inode)->i_fs_version;
2237 * Set the 64-bit version and return the old version.
2239 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2240 dt_obj_version_t new_version)
2242 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2244 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2245 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2246 LDISKFS_I(inode)->i_fs_version = new_version;
2247 /** Version is set after all inode operations are finished,
2248 * so we should mark it dirty here */
2249 inode->i_sb->s_op->dirty_inode(inode);
2252 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2255 struct osd_object *obj = osd_dt_obj(dt);
2258 *data = (void *)obj->oo_inode;
2266 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2267 const struct dt_index_features *feat)
2269 struct iam_descr *descr;
2271 if (osd_object_is_root(o))
2272 return feat == &dt_directory_features;
2274 LASSERT(o->oo_dir != NULL);
2276 descr = o->oo_dir->od_container.ic_descr;
2277 if (feat == &dt_directory_features) {
2278 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2284 feat->dif_keysize_min <= descr->id_key_size &&
2285 descr->id_key_size <= feat->dif_keysize_max &&
2286 feat->dif_recsize_min <= descr->id_rec_size &&
2287 descr->id_rec_size <= feat->dif_recsize_max &&
2288 !(feat->dif_flags & (DT_IND_VARKEY |
2289 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2290 ergo(feat->dif_flags & DT_IND_UPDATE,
2291 1 /* XXX check that object (and file system) is
2296 static int osd_iam_container_init(const struct lu_env *env,
2297 struct osd_object *obj,
2298 struct osd_directory *dir)
2301 struct iam_container *bag;
2303 bag = &dir->od_container;
2304 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2306 result = iam_container_setup(bag);
2308 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2310 iam_container_fini(bag);
2317 * Concurrency: no external locking is necessary.
2319 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2320 const struct dt_index_features *feat)
2324 struct osd_object *obj = osd_dt_obj(dt);
2325 struct osd_device *osd = osd_obj2dev(obj);
2327 LINVRNT(osd_invariant(obj));
2328 LASSERT(dt_object_exists(dt));
2330 if (osd_object_is_root(obj)) {
2331 dt->do_index_ops = &osd_index_ea_ops;
2333 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2334 dt->do_index_ops = &osd_index_ea_ops;
2335 if (S_ISDIR(obj->oo_inode->i_mode))
2340 } else if (!osd_has_index(obj)) {
2341 struct osd_directory *dir;
2346 cfs_spin_lock(&obj->oo_guard);
2347 if (obj->oo_dir == NULL)
2351 * Concurrent thread allocated container data.
2354 cfs_spin_unlock(&obj->oo_guard);
2356 * Now, that we have container data, serialize its
2359 cfs_down_write(&obj->oo_ext_idx_sem);
2361 * recheck under lock.
2363 if (!osd_has_index(obj))
2364 result = osd_iam_container_init(env, obj, dir);
2367 cfs_up_write(&obj->oo_ext_idx_sem);
2373 if (result == 0 && ea_dir == 0) {
2374 if (!osd_iam_index_probe(env, obj, feat))
2377 LINVRNT(osd_invariant(obj));
2382 static const struct dt_object_operations osd_obj_ops = {
2383 .do_read_lock = osd_object_read_lock,
2384 .do_write_lock = osd_object_write_lock,
2385 .do_read_unlock = osd_object_read_unlock,
2386 .do_write_unlock = osd_object_write_unlock,
2387 .do_write_locked = osd_object_write_locked,
2388 .do_attr_get = osd_attr_get,
2389 .do_attr_set = osd_attr_set,
2390 .do_ah_init = osd_ah_init,
2391 .do_create = osd_object_create,
2392 .do_index_try = osd_index_try,
2393 .do_ref_add = osd_object_ref_add,
2394 .do_ref_del = osd_object_ref_del,
2395 .do_xattr_get = osd_xattr_get,
2396 .do_xattr_set = osd_xattr_set,
2397 .do_xattr_del = osd_xattr_del,
2398 .do_xattr_list = osd_xattr_list,
2399 .do_capa_get = osd_capa_get,
2400 .do_object_sync = osd_object_sync,
2401 .do_version_get = osd_object_version_get,
2402 .do_version_set = osd_object_version_set,
2403 .do_data_get = osd_data_get,
2407 * dt_object_operations for interoperability mode
2408 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2410 static const struct dt_object_operations osd_obj_ea_ops = {
2411 .do_read_lock = osd_object_read_lock,
2412 .do_write_lock = osd_object_write_lock,
2413 .do_read_unlock = osd_object_read_unlock,
2414 .do_write_unlock = osd_object_write_unlock,
2415 .do_write_locked = osd_object_write_locked,
2416 .do_attr_get = osd_attr_get,
2417 .do_attr_set = osd_attr_set,
2418 .do_ah_init = osd_ah_init,
2419 .do_create = osd_object_ea_create,
2420 .do_index_try = osd_index_try,
2421 .do_ref_add = osd_object_ref_add,
2422 .do_ref_del = osd_object_ref_del,
2423 .do_xattr_get = osd_xattr_get,
2424 .do_xattr_set = osd_xattr_set,
2425 .do_xattr_del = osd_xattr_del,
2426 .do_xattr_list = osd_xattr_list,
2427 .do_capa_get = osd_capa_get,
2428 .do_object_sync = osd_object_sync,
2429 .do_version_get = osd_object_version_get,
2430 .do_version_set = osd_object_version_set,
2431 .do_data_get = osd_data_get,
2439 * XXX: Another layering violation for now.
2441 * We don't want to use ->f_op->read methods, because generic file write
2443 * - serializes on ->i_sem, and
2445 * - does a lot of extra work like balance_dirty_pages(),
2447 * which doesn't work for globally shared files like /last-received.
2449 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2451 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2453 memcpy(buffer, (char*)ei->i_data, buflen);
2458 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2461 struct buffer_head *bh;
2462 unsigned long block;
2469 /* prevent reading after eof */
2470 spin_lock(&inode->i_lock);
2471 if (i_size_read(inode) < *offs + size) {
2472 size = i_size_read(inode) - *offs;
2473 spin_unlock(&inode->i_lock);
2475 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2476 i_size_read(inode), *offs);
2478 } else if (size == 0) {
2482 spin_unlock(&inode->i_lock);
2485 blocksize = 1 << inode->i_blkbits;
2488 block = *offs >> inode->i_blkbits;
2489 boffs = *offs & (blocksize - 1);
2490 csize = min(blocksize - boffs, size);
2491 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2493 CERROR("can't read block: %d\n", err);
2497 memcpy(buf, bh->b_data + boffs, csize);
2507 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2508 struct lu_buf *buf, loff_t *pos,
2509 struct lustre_capa *capa)
2511 struct osd_object *obj = osd_dt_obj(dt);
2512 struct inode *inode = obj->oo_inode;
2515 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2518 /* Read small symlink from inode body as we need to maintain correct
2519 * on-disk symlinks for ldiskfs.
2521 if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2522 (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2523 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2525 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2530 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2533 memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2535 LDISKFS_I(inode)->i_disksize = buflen;
2536 i_size_write(inode, buflen);
2537 inode->i_sb->s_op->dirty_inode(inode);
2542 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2543 loff_t *offs, handle_t *handle)
2545 struct buffer_head *bh = NULL;
2546 loff_t offset = *offs;
2547 loff_t new_size = i_size_read(inode);
2548 unsigned long block;
2549 int blocksize = 1 << inode->i_blkbits;
2553 int dirty_inode = 0;
2555 while (bufsize > 0) {
2559 block = offset >> inode->i_blkbits;
2560 boffs = offset & (blocksize - 1);
2561 size = min(blocksize - boffs, bufsize);
2562 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2564 CERROR("can't read/create block: %d\n", err);
2568 err = ldiskfs_journal_get_write_access(handle, bh);
2570 CERROR("journal_get_write_access() returned error %d\n",
2574 LASSERTF(boffs + size <= bh->b_size,
2575 "boffs %d size %d bh->b_size %lu",
2576 boffs, size, (unsigned long)bh->b_size);
2577 memcpy(bh->b_data + boffs, buf, size);
2578 err = ldiskfs_journal_dirty_metadata(handle, bh);
2582 if (offset + size > new_size)
2583 new_size = offset + size;
2591 /* correct in-core and on-disk sizes */
2592 if (new_size > i_size_read(inode)) {
2593 spin_lock(&inode->i_lock);
2594 if (new_size > i_size_read(inode))
2595 i_size_write(inode, new_size);
2596 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
2597 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
2600 spin_unlock(&inode->i_lock);
2602 inode->i_sb->s_op->dirty_inode(inode);
2610 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2611 const struct lu_buf *buf, loff_t *pos,
2612 struct thandle *handle, struct lustre_capa *capa,
2615 struct osd_object *obj = osd_dt_obj(dt);
2616 struct inode *inode = obj->oo_inode;
2617 struct osd_thandle *oh;
2619 #ifdef HAVE_QUOTA_SUPPORT
2620 cfs_cap_t save = cfs_curproc_cap_pack();
2623 LASSERT(handle != NULL);
2625 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2628 oh = container_of(handle, struct osd_thandle, ot_super);
2629 LASSERT(oh->ot_handle->h_transaction != NULL);
2630 #ifdef HAVE_QUOTA_SUPPORT
2632 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2634 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2636 /* Write small symlink to inode body as we need to maintain correct
2637 * on-disk symlinks for ldiskfs.
2639 if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2640 (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
2641 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2643 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
2646 #ifdef HAVE_QUOTA_SUPPORT
2647 cfs_curproc_cap_unpack(save);
2650 result = buf->lb_len;
2654 static const struct dt_body_operations osd_body_ops = {
2655 .dbo_read = osd_read,
2656 .dbo_write = osd_write
2661 * delete a (key, value) pair from index \a dt specified by \a key
2663 * \param dt osd index object
2664 * \param key key for index
2665 * \param rec record reference
2666 * \param handle transaction handler
2669 * \retval -ve failure
2672 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2673 const struct dt_key *key, struct thandle *handle,
2674 struct lustre_capa *capa)
2676 struct osd_object *obj = osd_dt_obj(dt);
2677 struct osd_thandle *oh;
2678 struct iam_path_descr *ipd;
2679 struct iam_container *bag = &obj->oo_dir->od_container;
2684 LINVRNT(osd_invariant(obj));
2685 LASSERT(dt_object_exists(dt));
2686 LASSERT(bag->ic_object == obj->oo_inode);
2687 LASSERT(handle != NULL);
2689 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2692 ipd = osd_idx_ipd_get(env, bag);
2693 if (unlikely(ipd == NULL))
2696 oh = container_of0(handle, struct osd_thandle, ot_super);
2697 LASSERT(oh->ot_handle != NULL);
2698 LASSERT(oh->ot_handle->h_transaction != NULL);
2700 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2701 osd_ipd_put(env, bag, ipd);
2702 LINVRNT(osd_invariant(obj));
2706 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2709 struct osd_fid_pack *rec;
2712 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2713 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2714 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2720 * Index delete function for interoperability mode (b11826).
2721 * It will remove the directory entry added by osd_index_ea_insert().
2722 * This entry is needed to maintain name->fid mapping.
2724 * \param key, key i.e. file entry to be deleted
2726 * \retval 0, on success
2727 * \retval -ve, on error
2729 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2730 const struct dt_key *key, struct thandle *handle,
2731 struct lustre_capa *capa)
2733 struct osd_object *obj = osd_dt_obj(dt);
2734 struct inode *dir = obj->oo_inode;
2735 struct dentry *dentry;
2736 struct osd_thandle *oh;
2737 struct ldiskfs_dir_entry_2 *de;
2738 struct buffer_head *bh;
2744 LINVRNT(osd_invariant(obj));
2745 LASSERT(dt_object_exists(dt));
2746 LASSERT(handle != NULL);
2748 oh = container_of(handle, struct osd_thandle, ot_super);
2749 LASSERT(oh->ot_handle != NULL);
2750 LASSERT(oh->ot_handle->h_transaction != NULL);
2752 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2755 dentry = osd_child_dentry_get(env, obj,
2756 (char *)key, strlen((char *)key));
2758 cfs_down_write(&obj->oo_ext_idx_sem);
2759 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
2761 rc = ldiskfs_delete_entry(oh->ot_handle,
2767 cfs_up_write(&obj->oo_ext_idx_sem);
2768 LASSERT(osd_invariant(obj));
2773 * Lookup index for \a key and copy record to \a rec.
2775 * \param dt osd index object
2776 * \param key key for index
2777 * \param rec record reference
2779 * \retval +ve success : exact mach
2780 * \retval 0 return record with key not greater than \a key
2781 * \retval -ve failure
2783 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2784 struct dt_rec *rec, const struct dt_key *key,
2785 struct lustre_capa *capa)
2787 struct osd_object *obj = osd_dt_obj(dt);
2788 struct iam_path_descr *ipd;
2789 struct iam_container *bag = &obj->oo_dir->od_container;
2790 struct osd_thread_info *oti = osd_oti_get(env);
2791 struct iam_iterator *it = &oti->oti_idx_it;
2792 struct iam_rec *iam_rec;
2796 LASSERT(osd_invariant(obj));
2797 LASSERT(dt_object_exists(dt));
2798 LASSERT(bag->ic_object == obj->oo_inode);
2800 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2803 ipd = osd_idx_ipd_get(env, bag);
2807 /* got ipd now we can start iterator. */
2808 iam_it_init(it, bag, 0, ipd);
2810 rc = iam_it_get(it, (struct iam_key *)key);
2812 if (S_ISDIR(obj->oo_inode->i_mode))
2813 iam_rec = (struct iam_rec *)oti->oti_ldp;
2815 iam_rec = (struct iam_rec *) rec;
2817 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2818 if (S_ISDIR(obj->oo_inode->i_mode))
2819 osd_fid_unpack((struct lu_fid *) rec,
2820 (struct osd_fid_pack *)iam_rec);
2824 osd_ipd_put(env, bag, ipd);
2826 LINVRNT(osd_invariant(obj));
2832 * Inserts (key, value) pair in \a dt index object.
2834 * \param dt osd index object
2835 * \param key key for index
2836 * \param rec record reference
2837 * \param th transaction handler
2840 * \retval -ve failure
2842 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2843 const struct dt_rec *rec, const struct dt_key *key,
2844 struct thandle *th, struct lustre_capa *capa,
2847 struct osd_object *obj = osd_dt_obj(dt);
2848 struct iam_path_descr *ipd;
2849 struct osd_thandle *oh;
2850 struct iam_container *bag = &obj->oo_dir->od_container;
2851 #ifdef HAVE_QUOTA_SUPPORT
2852 cfs_cap_t save = cfs_curproc_cap_pack();
2854 struct osd_thread_info *oti = osd_oti_get(env);
2855 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2860 LINVRNT(osd_invariant(obj));
2861 LASSERT(dt_object_exists(dt));
2862 LASSERT(bag->ic_object == obj->oo_inode);
2863 LASSERT(th != NULL);
2865 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2868 ipd = osd_idx_ipd_get(env, bag);
2869 if (unlikely(ipd == NULL))
2872 oh = container_of0(th, struct osd_thandle, ot_super);
2873 LASSERT(oh->ot_handle != NULL);
2874 LASSERT(oh->ot_handle->h_transaction != NULL);
2875 #ifdef HAVE_QUOTA_SUPPORT
2877 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2879 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2881 if (S_ISDIR(obj->oo_inode->i_mode))
2882 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2884 iam_rec = (struct iam_rec *) rec;
2885 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2887 #ifdef HAVE_QUOTA_SUPPORT
2888 cfs_curproc_cap_unpack(save);
2890 osd_ipd_put(env, bag, ipd);
2891 LINVRNT(osd_invariant(obj));
2896 * Calls ldiskfs_add_entry() to add directory entry
2897 * into the directory. This is required for
2898 * interoperability mode (b11826)
2900 * \retval 0, on success
2901 * \retval -ve, on error
2903 static int __osd_ea_add_rec(struct osd_thread_info *info,
2904 struct osd_object *pobj,
2905 struct inode *cinode,
2907 const struct dt_rec *fid,
2910 struct ldiskfs_dentry_param *ldp;
2911 struct dentry *child;
2912 struct osd_thandle *oth;
2915 oth = container_of(th, struct osd_thandle, ot_super);
2916 LASSERT(oth->ot_handle != NULL);
2917 LASSERT(oth->ot_handle->h_transaction != NULL);
2919 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2921 if (fid_is_igif((struct lu_fid *)fid) ||
2922 fid_is_norm((struct lu_fid *)fid)) {
2923 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2924 osd_get_ldiskfs_dirent_param(ldp, fid);
2925 child->d_fsdata = (void*) ldp;
2927 child->d_fsdata = NULL;
2928 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2934 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2935 * into the directory.Also sets flags into osd object to
2936 * indicate dot and dotdot are created. This is required for
2937 * interoperability mode (b11826)
2939 * \param dir directory for dot and dotdot fixup.
2940 * \param obj child object for linking
2942 * \retval 0, on success
2943 * \retval -ve, on error
2945 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2946 struct osd_object *dir,
2947 struct inode *parent_dir, const char *name,
2948 const struct dt_rec *dot_fid,
2949 const struct dt_rec *dot_dot_fid,
2952 struct inode *inode = dir->oo_inode;
2953 struct ldiskfs_dentry_param *dot_ldp;
2954 struct ldiskfs_dentry_param *dot_dot_ldp;
2955 struct osd_thandle *oth;
2958 oth = container_of(th, struct osd_thandle, ot_super);
2959 LASSERT(oth->ot_handle->h_transaction != NULL);
2960 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2962 if (strcmp(name, dot) == 0) {
2963 if (dir->oo_compat_dot_created) {
2966 LASSERT(inode == parent_dir);
2967 dir->oo_compat_dot_created = 1;
2970 } else if(strcmp(name, dotdot) == 0) {
2971 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2972 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2974 if (!dir->oo_compat_dot_created)
2976 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
2977 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
2978 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2983 /* in case of rename, dotdot is already created */
2984 if (dir->oo_compat_dotdot_created) {
2985 return __osd_ea_add_rec(info, dir, parent_dir, name,
2989 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode,
2990 dot_ldp, dot_dot_ldp);
2992 dir->oo_compat_dotdot_created = 1;
3000 * It will call the appropriate osd_add* function and return the
3001 * value, return by respective functions.
3003 static int osd_ea_add_rec(const struct lu_env *env,
3004 struct osd_object *pobj,
3005 struct inode *cinode,
3007 const struct dt_rec *fid,
3010 struct osd_thread_info *info = osd_oti_get(env);
3013 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3015 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3016 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3019 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid, th);
3025 * Calls ->lookup() to find dentry. From dentry get inode and
3026 * read inode's ea to get fid. This is required for interoperability
3029 * \retval 0, on success
3030 * \retval -ve, on error
3032 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3033 struct dt_rec *rec, const struct dt_key *key)
3035 struct inode *dir = obj->oo_inode;
3036 struct dentry *dentry;
3037 struct ldiskfs_dir_entry_2 *de;
3038 struct buffer_head *bh;
3039 struct lu_fid *fid = (struct lu_fid *) rec;
3043 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3045 dentry = osd_child_dentry_get(env, obj,
3046 (char *)key, strlen((char *)key));
3048 cfs_down_read(&obj->oo_ext_idx_sem);
3049 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
3051 ino = le32_to_cpu(de->inode);
3052 rc = osd_get_fid_from_dentry(de, rec);
3054 /* done with de, release bh */
3057 rc = osd_ea_fid_get(env, obj, ino, fid);
3061 cfs_up_read(&obj->oo_ext_idx_sem);
3066 * Find the osd object for given fid.
3068 * \param fid need to find the osd object having this fid
3070 * \retval osd_object on success
3071 * \retval -ve on error
3073 struct osd_object *osd_object_find(const struct lu_env *env,
3074 struct dt_object *dt,
3075 const struct lu_fid *fid)
3077 struct lu_device *ludev = dt->do_lu.lo_dev;
3078 struct osd_object *child = NULL;
3079 struct lu_object *luch;
3080 struct lu_object *lo;
3082 luch = lu_object_find(env, ludev, fid, NULL);
3083 if (!IS_ERR(luch)) {
3084 if (lu_object_exists(luch)) {
3085 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3087 child = osd_obj(lo);
3089 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3090 "lu_object can't be located"
3091 ""DFID"\n", PFID(fid));
3093 if (child == NULL) {
3094 lu_object_put(env, luch);
3095 CERROR("Unable to get osd_object\n");
3096 child = ERR_PTR(-ENOENT);
3099 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3100 "lu_object does not exists "DFID"\n",
3102 child = ERR_PTR(-ENOENT);
3105 child = (void *)luch;
3111 * Put the osd object once done with it.
3113 * \param obj osd object that needs to be put
3115 static inline void osd_object_put(const struct lu_env *env,
3116 struct osd_object *obj)
3118 lu_object_put(env, &obj->oo_dt.do_lu);
3122 * Index add function for interoperability mode (b11826).
3123 * It will add the directory entry.This entry is needed to
3124 * maintain name->fid mapping.
3126 * \param key it is key i.e. file entry to be inserted
3127 * \param rec it is value of given key i.e. fid
3129 * \retval 0, on success
3130 * \retval -ve, on error
3132 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3133 const struct dt_rec *rec,
3134 const struct dt_key *key, struct thandle *th,
3135 struct lustre_capa *capa, int ignore_quota)
3137 struct osd_object *obj = osd_dt_obj(dt);
3138 struct lu_fid *fid = (struct lu_fid *) rec;
3139 const char *name = (const char *)key;
3140 struct osd_object *child;
3141 #ifdef HAVE_QUOTA_SUPPORT
3142 cfs_cap_t save = cfs_curproc_cap_pack();
3148 LASSERT(osd_invariant(obj));
3149 LASSERT(dt_object_exists(dt));
3150 LASSERT(th != NULL);
3152 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3155 child = osd_object_find(env, dt, fid);
3156 if (!IS_ERR(child)) {
3157 #ifdef HAVE_QUOTA_SUPPORT
3159 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3161 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3163 cfs_down_write(&obj->oo_ext_idx_sem);
3164 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3165 cfs_up_write(&obj->oo_ext_idx_sem);
3166 #ifdef HAVE_QUOTA_SUPPORT
3167 cfs_curproc_cap_unpack(save);
3169 osd_object_put(env, child);
3171 rc = PTR_ERR(child);
3174 LASSERT(osd_invariant(obj));
3179 * Initialize osd Iterator for given osd index object.
3181 * \param dt osd index object
3184 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3185 struct dt_object *dt,
3187 struct lustre_capa *capa)
3189 struct osd_it_iam *it;
3190 struct osd_thread_info *oti = osd_oti_get(env);
3191 struct osd_object *obj = osd_dt_obj(dt);
3192 struct lu_object *lo = &dt->do_lu;
3193 struct iam_path_descr *ipd;
3194 struct iam_container *bag = &obj->oo_dir->od_container;
3196 LASSERT(lu_object_exists(lo));
3198 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3199 return ERR_PTR(-EACCES);
3202 ipd = osd_it_ipd_get(env, bag);
3203 if (likely(ipd != NULL)) {
3207 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3208 return (struct dt_it *)it;
3210 return ERR_PTR(-ENOMEM);
3214 * free given Iterator.
3217 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3219 struct osd_it_iam *it = (struct osd_it_iam *)di;
3220 struct osd_object *obj = it->oi_obj;
3222 iam_it_fini(&it->oi_it);
3223 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3224 lu_object_put(env, &obj->oo_dt.do_lu);
3228 * Move Iterator to record specified by \a key
3230 * \param di osd iterator
3231 * \param key key for index
3233 * \retval +ve di points to record with least key not larger than key
3234 * \retval 0 di points to exact matched key
3235 * \retval -ve failure
3238 static int osd_it_iam_get(const struct lu_env *env,
3239 struct dt_it *di, const struct dt_key *key)
3241 struct osd_it_iam *it = (struct osd_it_iam *)di;
3243 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3249 * \param di osd iterator
3252 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3254 struct osd_it_iam *it = (struct osd_it_iam *)di;
3256 iam_it_put(&it->oi_it);
3260 * Move iterator by one record
3262 * \param di osd iterator
3264 * \retval +1 end of container reached
3266 * \retval -ve failure
3269 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3271 struct osd_it_iam *it = (struct osd_it_iam *)di;
3273 return iam_it_next(&it->oi_it);
3277 * Return pointer to the key under iterator.
3280 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3281 const struct dt_it *di)
3283 struct osd_it_iam *it = (struct osd_it_iam *)di;
3285 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3289 * Return size of key under iterator (in bytes)
3292 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3294 struct osd_it_iam *it = (struct osd_it_iam *)di;
3296 return iam_it_key_size(&it->oi_it);
3299 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3304 struct luda_type *lt;
3305 const unsigned align = sizeof(struct luda_type) - 1;
3307 /* check if file type is required */
3308 if (attr & LUDA_TYPE) {
3309 len = (len + align) & ~align;
3311 lt = (void *) ent->lde_name + len;
3312 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3313 ent->lde_attrs |= LUDA_TYPE;
3316 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3320 * build lu direct from backend fs dirent.
3323 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3331 fid_cpu_to_le(&ent->lde_fid, fid);
3332 ent->lde_attrs = LUDA_FID;
3334 ent->lde_hash = cpu_to_le64(offset);
3335 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3337 strncpy(ent->lde_name, name, namelen);
3338 ent->lde_namelen = cpu_to_le16(namelen);
3340 /* append lustre attributes */
3341 osd_it_append_attrs(ent, attr, namelen, type);
3345 * Return pointer to the record under iterator.
3347 static int osd_it_iam_rec(const struct lu_env *env,
3348 const struct dt_it *di,
3349 struct lu_dirent *lde,
3352 struct osd_it_iam *it = (struct osd_it_iam *)di;
3353 struct osd_thread_info *info = osd_oti_get(env);
3354 struct lu_fid *fid = &info->oti_fid;
3355 const struct osd_fid_pack *rec;
3361 name = (char *)iam_it_key_get(&it->oi_it);
3363 RETURN(PTR_ERR(name));
3365 namelen = iam_it_key_size(&it->oi_it);
3367 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3369 RETURN(PTR_ERR(rec));
3371 rc = osd_fid_unpack(fid, rec);
3375 hash = iam_it_store(&it->oi_it);
3377 /* IAM does not store object type in IAM index (dir) */
3378 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3385 * Returns cookie for current Iterator position.
3387 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3389 struct osd_it_iam *it = (struct osd_it_iam *)di;
3391 return iam_it_store(&it->oi_it);
3395 * Restore iterator from cookie.
3397 * \param di osd iterator
3398 * \param hash Iterator location cookie
3400 * \retval +ve di points to record with least key not larger than key.
3401 * \retval 0 di points to exact matched key
3402 * \retval -ve failure
3405 static int osd_it_iam_load(const struct lu_env *env,
3406 const struct dt_it *di, __u64 hash)
3408 struct osd_it_iam *it = (struct osd_it_iam *)di;
3410 return iam_it_load(&it->oi_it, hash);
3413 static const struct dt_index_operations osd_index_iam_ops = {
3414 .dio_lookup = osd_index_iam_lookup,
3415 .dio_insert = osd_index_iam_insert,
3416 .dio_delete = osd_index_iam_delete,
3418 .init = osd_it_iam_init,
3419 .fini = osd_it_iam_fini,
3420 .get = osd_it_iam_get,
3421 .put = osd_it_iam_put,
3422 .next = osd_it_iam_next,
3423 .key = osd_it_iam_key,
3424 .key_size = osd_it_iam_key_size,
3425 .rec = osd_it_iam_rec,
3426 .store = osd_it_iam_store,
3427 .load = osd_it_iam_load
3432 * Creates or initializes iterator context.
3434 * \retval struct osd_it_ea, iterator structure on success
3437 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3438 struct dt_object *dt,
3440 struct lustre_capa *capa)
3442 struct osd_object *obj = osd_dt_obj(dt);
3443 struct osd_thread_info *info = osd_oti_get(env);
3444 struct osd_it_ea *it = &info->oti_it_ea;
3445 struct lu_object *lo = &dt->do_lu;
3446 struct dentry *obj_dentry = &info->oti_it_dentry;
3448 LASSERT(lu_object_exists(lo));
3450 obj_dentry->d_inode = obj->oo_inode;
3451 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3452 obj_dentry->d_name.hash = 0;
3454 it->oie_rd_dirent = 0;
3455 it->oie_it_dirent = 0;
3456 it->oie_dirent = NULL;
3457 it->oie_buf = info->oti_it_ea_buf;
3459 it->oie_file.f_pos = 0;
3460 it->oie_file.f_dentry = obj_dentry;
3461 if (attr & LUDA_64BITHASH)
3462 it->oie_file.f_flags = O_64BITHASH;
3464 it->oie_file.f_flags = O_32BITHASH;
3465 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3466 it->oie_file.f_op = obj->oo_inode->i_fop;
3467 it->oie_file.private_data = NULL;
3469 RETURN((struct dt_it *) it);
3473 * Destroy or finishes iterator context.
3475 * \param di iterator structure to be destroyed
3477 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3479 struct osd_it_ea *it = (struct osd_it_ea *)di;
3480 struct osd_object *obj = it->oie_obj;
3481 struct inode *inode = obj->oo_inode;
3484 it->oie_file.f_op->release(inode, &it->oie_file);
3485 lu_object_put(env, &obj->oo_dt.do_lu);
3490 * It position the iterator at given key, so that next lookup continues from
3491 * that key Or it is similar to dio_it->load() but based on a key,
3492 * rather than file position.
3494 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3497 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3499 static int osd_it_ea_get(const struct lu_env *env,
3500 struct dt_it *di, const struct dt_key *key)
3502 struct osd_it_ea *it = (struct osd_it_ea *)di;
3505 LASSERT(((const char *)key)[0] == '\0');
3506 it->oie_file.f_pos = 0;
3507 it->oie_rd_dirent = 0;
3508 it->oie_it_dirent = 0;
3509 it->oie_dirent = NULL;
3517 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3522 * It is called internally by ->readdir(). It fills the
3523 * iterator's in-memory data structure with required
3524 * information i.e. name, namelen, rec_size etc.
3526 * \param buf in which information to be filled in.
3527 * \param name name of the file in given dir
3529 * \retval 0 on success
3530 * \retval 1 on buffer full
3532 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3533 loff_t offset, __u64 ino,
3536 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3537 struct osd_it_ea_dirent *ent = it->oie_dirent;
3538 struct lu_fid *fid = &ent->oied_fid;
3539 struct osd_fid_pack *rec;
3542 /* this should never happen */
3543 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3544 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3548 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3552 if (d_type & LDISKFS_DIRENT_LUFID) {
3553 rec = (struct osd_fid_pack*) (name + namelen + 1);
3555 if (osd_fid_unpack(fid, rec) != 0)
3558 d_type &= ~LDISKFS_DIRENT_LUFID;
3563 ent->oied_ino = ino;
3564 ent->oied_off = offset;
3565 ent->oied_namelen = namelen;
3566 ent->oied_type = d_type;
3568 memcpy(ent->oied_name, name, namelen);
3570 it->oie_rd_dirent++;
3571 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3576 * Calls ->readdir() to load a directory entry at a time
3577 * and stored it in iterator's in-memory data structure.
3579 * \param di iterator's in memory structure
3581 * \retval 0 on success
3582 * \retval -ve on error
3584 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3586 struct osd_it_ea *it = (struct osd_it_ea *)di;
3587 struct osd_object *obj = it->oie_obj;
3588 struct inode *inode = obj->oo_inode;
3592 it->oie_dirent = it->oie_buf;
3593 it->oie_rd_dirent = 0;
3595 cfs_down_read(&obj->oo_ext_idx_sem);
3596 result = inode->i_fop->readdir(&it->oie_file, it,
3597 (filldir_t) osd_ldiskfs_filldir);
3599 cfs_up_read(&obj->oo_ext_idx_sem);
3601 if (it->oie_rd_dirent == 0) {
3604 it->oie_dirent = it->oie_buf;
3605 it->oie_it_dirent = 1;
3612 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3613 * to load a directory entry at a time and stored it in
3614 * iterator's in-memory data structure.
3616 * \param di iterator's in memory structure
3618 * \retval +ve iterator reached to end
3619 * \retval 0 iterator not reached to end
3620 * \retval -ve on error
3622 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3624 struct osd_it_ea *it = (struct osd_it_ea *)di;
3629 if (it->oie_it_dirent < it->oie_rd_dirent) {
3631 (void *) it->oie_dirent +
3632 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3633 it->oie_dirent->oied_namelen);
3634 it->oie_it_dirent++;
3637 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3640 rc = osd_ldiskfs_it_fill(di);
3647 * Returns the key at current position from iterator's in memory structure.
3649 * \param di iterator's in memory structure
3651 * \retval key i.e. struct dt_key on success
3653 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3654 const struct dt_it *di)
3656 struct osd_it_ea *it = (struct osd_it_ea *)di;
3658 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3662 * Returns the key's size at current position from iterator's in memory structure.
3664 * \param di iterator's in memory structure
3666 * \retval key_size i.e. struct dt_key on success
3668 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3670 struct osd_it_ea *it = (struct osd_it_ea *)di;
3672 RETURN(it->oie_dirent->oied_namelen);
3677 * Returns the value (i.e. fid/igif) at current position from iterator's
3678 * in memory structure.
3680 * \param di struct osd_it_ea, iterator's in memory structure
3681 * \param attr attr requested for dirent.
3682 * \param lde lustre dirent
3684 * \retval 0 no error and \param lde has correct lustre dirent.
3685 * \retval -ve on error
3687 static inline int osd_it_ea_rec(const struct lu_env *env,
3688 const struct dt_it *di,
3689 struct lu_dirent *lde,
3692 struct osd_it_ea *it = (struct osd_it_ea *)di;
3693 struct osd_object *obj = it->oie_obj;
3694 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3699 if (!fid_is_sane(fid))
3700 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3703 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3704 it->oie_dirent->oied_name,
3705 it->oie_dirent->oied_namelen,
3706 it->oie_dirent->oied_type,
3712 * Returns a cookie for current position of the iterator head, so that
3713 * user can use this cookie to load/start the iterator next time.
3715 * \param di iterator's in memory structure
3717 * \retval cookie for current position, on success
3719 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3721 struct osd_it_ea *it = (struct osd_it_ea *)di;
3723 RETURN(it->oie_dirent->oied_off);
3727 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3728 * to load a directory entry at a time and stored it i inn,
3729 * in iterator's in-memory data structure.
3731 * \param di struct osd_it_ea, iterator's in memory structure
3733 * \retval +ve on success
3734 * \retval -ve on error
3736 static int osd_it_ea_load(const struct lu_env *env,
3737 const struct dt_it *di, __u64 hash)
3739 struct osd_it_ea *it = (struct osd_it_ea *)di;
3743 it->oie_file.f_pos = hash;
3745 rc = osd_ldiskfs_it_fill(di);
3753 * Index lookup function for interoperability mode (b11826).
3755 * \param key, key i.e. file name to be searched
3757 * \retval +ve, on success
3758 * \retval -ve, on error
3760 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3761 struct dt_rec *rec, const struct dt_key *key,
3762 struct lustre_capa *capa)
3764 struct osd_object *obj = osd_dt_obj(dt);
3769 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3770 LINVRNT(osd_invariant(obj));
3772 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3775 rc = osd_ea_lookup_rec(env, obj, rec, key);
3783 * Index and Iterator operations for interoperability
3784 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3786 static const struct dt_index_operations osd_index_ea_ops = {
3787 .dio_lookup = osd_index_ea_lookup,
3788 .dio_insert = osd_index_ea_insert,
3789 .dio_delete = osd_index_ea_delete,
3791 .init = osd_it_ea_init,
3792 .fini = osd_it_ea_fini,
3793 .get = osd_it_ea_get,
3794 .put = osd_it_ea_put,
3795 .next = osd_it_ea_next,
3796 .key = osd_it_ea_key,
3797 .key_size = osd_it_ea_key_size,
3798 .rec = osd_it_ea_rec,
3799 .store = osd_it_ea_store,
3800 .load = osd_it_ea_load
3804 static void *osd_key_init(const struct lu_context *ctx,
3805 struct lu_context_key *key)
3807 struct osd_thread_info *info;
3809 OBD_ALLOC_PTR(info);
3811 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3812 if (info->oti_it_ea_buf != NULL) {
3813 info->oti_env = container_of(ctx, struct lu_env,
3817 info = ERR_PTR(-ENOMEM);
3820 info = ERR_PTR(-ENOMEM);
3825 static void osd_key_fini(const struct lu_context *ctx,
3826 struct lu_context_key *key, void* data)
3828 struct osd_thread_info *info = data;
3830 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3834 static void osd_key_exit(const struct lu_context *ctx,
3835 struct lu_context_key *key, void *data)
3837 struct osd_thread_info *info = data;
3839 LASSERT(info->oti_r_locks == 0);
3840 LASSERT(info->oti_w_locks == 0);
3841 LASSERT(info->oti_txns == 0);
3844 /* type constructor/destructor: osd_type_init, osd_type_fini */
3845 LU_TYPE_INIT_FINI(osd, &osd_key);
3847 static struct lu_context_key osd_key = {
3848 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3849 .lct_init = osd_key_init,
3850 .lct_fini = osd_key_fini,
3851 .lct_exit = osd_key_exit
3855 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3856 const char *name, struct lu_device *next)
3859 struct lu_context *ctx;
3861 /* context for commit hooks */
3862 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3863 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3865 rc = osd_procfs_init(osd_dev(d), name);
3866 ctx->lc_cookie = 0x3;
3871 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3873 struct osd_thread_info *info = osd_oti_get(env);
3875 if (o->od_obj_area != NULL) {
3876 lu_object_put(env, &o->od_obj_area->do_lu);
3877 o->od_obj_area = NULL;
3879 osd_oi_fini(info, &o->od_oi);
3884 static int osd_mount(const struct lu_env *env,
3885 struct osd_device *o, struct lustre_cfg *cfg)
3887 struct lustre_mount_info *lmi;
3888 const char *dev = lustre_cfg_string(cfg, 0);
3889 struct lustre_disk_data *ldd;
3890 struct lustre_sb_info *lsi;
3893 if (o->od_mount != NULL) {
3894 CERROR("Already mounted (%s)\n", dev);
3899 lmi = server_get_mount(dev);
3901 CERROR("Cannot get mount info for %s!\n", dev);
3905 LASSERT(lmi != NULL);
3906 /* save lustre_mount_info in dt_device */
3909 lsi = s2lsi(lmi->lmi_sb);
3912 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3914 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3918 o->od_obj_area = NULL;
3922 static struct lu_device *osd_device_fini(const struct lu_env *env,
3923 struct lu_device *d)
3928 shrink_dcache_sb(osd_sb(osd_dev(d)));
3929 osd_sync(env, lu2dt_dev(d));
3931 rc = osd_procfs_fini(osd_dev(d));
3933 CERROR("proc fini error %d \n", rc);
3934 RETURN (ERR_PTR(rc));
3937 if (osd_dev(d)->od_mount)
3938 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3939 osd_dev(d)->od_mount->lmi_mnt);
3940 osd_dev(d)->od_mount = NULL;
3942 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3946 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3947 struct lu_device_type *t,
3948 struct lustre_cfg *cfg)
3950 struct lu_device *l;
3951 struct osd_device *o;
3957 result = dt_device_init(&o->od_dt_dev, t);
3960 l->ld_ops = &osd_lu_ops;
3961 o->od_dt_dev.dd_ops = &osd_dt_ops;
3962 cfs_spin_lock_init(&o->od_osfs_lock);
3963 o->od_osfs_age = cfs_time_shift_64(-1000);
3964 o->od_capa_hash = init_capa_hash();
3965 if (o->od_capa_hash == NULL) {
3966 dt_device_fini(&o->od_dt_dev);
3967 l = ERR_PTR(-ENOMEM);
3970 l = ERR_PTR(result);
3975 l = ERR_PTR(-ENOMEM);
3979 static struct lu_device *osd_device_free(const struct lu_env *env,
3980 struct lu_device *d)
3982 struct osd_device *o = osd_dev(d);
3985 cleanup_capa_hash(o->od_capa_hash);
3986 dt_device_fini(&o->od_dt_dev);
3991 static int osd_process_config(const struct lu_env *env,
3992 struct lu_device *d, struct lustre_cfg *cfg)
3994 struct osd_device *o = osd_dev(d);
3998 switch(cfg->lcfg_command) {
4000 err = osd_mount(env, o, cfg);
4003 err = osd_shutdown(env, o);
4012 static int osd_recovery_complete(const struct lu_env *env,
4013 struct lu_device *d)
4018 static int osd_prepare(const struct lu_env *env,
4019 struct lu_device *pdev,
4020 struct lu_device *dev)
4022 struct osd_device *osd = osd_dev(dev);
4023 struct lustre_sb_info *lsi;
4024 struct lustre_disk_data *ldd;
4025 struct lustre_mount_info *lmi;
4026 struct osd_thread_info *oti = osd_oti_get(env);
4027 struct dt_object *d;
4031 /* 1. initialize oi before any file create or file open */
4032 result = osd_oi_init(oti, &osd->od_oi,
4033 &osd->od_dt_dev, lu2md_dev(pdev));
4037 lmi = osd->od_mount;
4038 lsi = s2lsi(lmi->lmi_sb);
4041 /* 2. setup local objects */
4042 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4046 /* 3. open remote object dir */
4047 d = dt_store_open(env, lu2dt_dev(dev), "",
4048 remote_obj_dir, &oti->oti_fid);
4050 osd->od_obj_area = d;
4053 result = PTR_ERR(d);
4054 osd->od_obj_area = NULL;
4061 static const struct lu_object_operations osd_lu_obj_ops = {
4062 .loo_object_init = osd_object_init,
4063 .loo_object_delete = osd_object_delete,
4064 .loo_object_release = osd_object_release,
4065 .loo_object_free = osd_object_free,
4066 .loo_object_print = osd_object_print,
4067 .loo_object_invariant = osd_object_invariant
4070 static const struct lu_device_operations osd_lu_ops = {
4071 .ldo_object_alloc = osd_object_alloc,
4072 .ldo_process_config = osd_process_config,
4073 .ldo_recovery_complete = osd_recovery_complete,
4074 .ldo_prepare = osd_prepare,
4077 static const struct lu_device_type_operations osd_device_type_ops = {
4078 .ldto_init = osd_type_init,
4079 .ldto_fini = osd_type_fini,
4081 .ldto_start = osd_type_start,
4082 .ldto_stop = osd_type_stop,
4084 .ldto_device_alloc = osd_device_alloc,
4085 .ldto_device_free = osd_device_free,
4087 .ldto_device_init = osd_device_init,
4088 .ldto_device_fini = osd_device_fini
4091 static struct lu_device_type osd_device_type = {
4092 .ldt_tags = LU_DEVICE_DT,
4093 .ldt_name = LUSTRE_OSD_NAME,
4094 .ldt_ops = &osd_device_type_ops,
4095 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4099 * lprocfs legacy support.
4101 static struct obd_ops osd_obd_device_ops = {
4102 .o_owner = THIS_MODULE
4105 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4106 .llod_name = remote_obj_dir,
4107 .llod_oid = OSD_REM_OBJ_DIR_OID,
4109 .llod_feat = &dt_directory_features,
4112 static int __init osd_mod_init(void)
4114 struct lprocfs_static_vars lvars;
4117 llo_local_obj_register(&llod_osd_rem_obj_dir);
4118 lprocfs_osd_init_vars(&lvars);
4119 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4120 LUSTRE_OSD_NAME, &osd_device_type);
4123 static void __exit osd_mod_exit(void)
4125 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4126 class_unregister_type(LUSTRE_OSD_NAME);
4129 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4130 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4131 MODULE_LICENSE("GPL");
4133 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);