1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/osd/osd_handler.c
41 * Top-level entry points into osd module
43 * Author: Nikita Danilov <nikita@clusterfs.com>
44 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
48 # define EXPORT_SYMTAB
50 #define DEBUG_SUBSYSTEM S_MDS
52 #include <linux/module.h>
54 /* LUSTRE_VERSION_CODE */
55 #include <lustre_ver.h>
56 /* prerequisite for linux/xattr.h */
57 #include <linux/types.h>
58 /* prerequisite for linux/xattr.h */
60 /* XATTR_{REPLACE,CREATE} */
61 #include <linux/xattr.h>
66 * struct OBD_{ALLOC,FREE}*()
69 #include <obd_support.h>
70 /* struct ptlrpc_thread */
71 #include <lustre_net.h>
74 #include <lustre_fid.h>
76 #include "osd_internal.h"
79 /* llo_* api support */
80 #include <md_object.h>
82 static const char dot[] = ".";
83 static const char dotdot[] = "..";
84 static const char remote_obj_dir[] = "REM_OBJ_DIR";
86 struct osd_directory {
87 struct iam_container od_container;
88 struct iam_descr od_descr;
92 struct dt_object oo_dt;
94 * Inode for file system object represented by this osd_object. This
95 * inode is pinned for the whole duration of lu_object life.
97 * Not modified concurrently (either setup early during object
98 * creation, or assigned by osd_object_create() under write lock).
100 struct inode *oo_inode;
102 * to protect index ops.
104 cfs_rw_semaphore_t oo_ext_idx_sem;
105 cfs_rw_semaphore_t oo_sem;
106 struct osd_directory *oo_dir;
107 /** protects inode attributes. */
108 cfs_spinlock_t oo_guard;
110 * Following two members are used to indicate the presence of dot and
111 * dotdot in the given directory. This is required for interop mode
114 int oo_compat_dot_created;
115 int oo_compat_dotdot_created;
117 const struct lu_env *oo_owner;
118 #ifdef CONFIG_LOCKDEP
119 struct lockdep_map oo_dep_map;
123 static const struct lu_object_operations osd_lu_obj_ops;
124 static const struct lu_device_operations osd_lu_ops;
125 static struct lu_context_key osd_key;
126 static const struct dt_object_operations osd_obj_ops;
127 static const struct dt_object_operations osd_obj_ea_ops;
128 static const struct dt_body_operations osd_body_ops;
129 static const struct dt_index_operations osd_index_iam_ops;
130 static const struct dt_index_operations osd_index_ea_ops;
133 struct thandle ot_super;
135 struct journal_callback ot_jcb;
136 /* Link to the device, for debugging. */
137 struct lu_ref_link *ot_dev_link;
139 #if OSD_THANDLE_STATS
140 /** time when this handle was allocated */
141 cfs_time_t oth_alloced;
143 /** time when this thanle was started */
144 cfs_time_t oth_started;
151 static int lu_device_is_osd(const struct lu_device *d)
153 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
156 static struct osd_device *osd_dt_dev(const struct dt_device *d)
158 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
159 return container_of0(d, struct osd_device, od_dt_dev);
162 static struct osd_device *osd_dev(const struct lu_device *d)
164 LASSERT(lu_device_is_osd(d));
165 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
168 static struct osd_device *osd_obj2dev(const struct osd_object *o)
170 return osd_dev(o->oo_dt.do_lu.lo_dev);
173 static struct super_block *osd_sb(const struct osd_device *dev)
175 return dev->od_mount->lmi_mnt->mnt_sb;
178 static int osd_object_is_root(const struct osd_object *obj)
180 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
183 static struct osd_object *osd_obj(const struct lu_object *o)
185 LASSERT(lu_device_is_osd(o->lo_dev));
186 return container_of0(o, struct osd_object, oo_dt.do_lu);
189 static struct osd_object *osd_dt_obj(const struct dt_object *d)
191 return osd_obj(&d->do_lu);
194 static struct lu_device *osd2lu_dev(struct osd_device *osd)
196 return &osd->od_dt_dev.dd_lu_dev;
199 static journal_t *osd_journal(const struct osd_device *dev)
201 return LDISKFS_SB(osd_sb(dev))->s_journal;
204 static int osd_has_index(const struct osd_object *obj)
206 return obj->oo_dt.do_index_ops != NULL;
209 static int osd_object_invariant(const struct lu_object *l)
211 return osd_invariant(osd_obj(l));
214 #ifdef HAVE_QUOTA_SUPPORT
216 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
218 struct md_ucred *uc = md_ucred(env);
223 save->oc_uid = current_fsuid();
224 save->oc_gid = current_fsgid();
225 save->oc_cap = current_cap();
226 if ((tc = prepare_creds())) {
227 tc->fsuid = uc->mu_fsuid;
228 tc->fsgid = uc->mu_fsgid;
231 /* XXX not suboptimal */
232 cfs_curproc_cap_unpack(uc->mu_cap);
236 osd_pop_ctxt(struct osd_ctxt *save)
240 if ((tc = prepare_creds())) {
241 tc->fsuid = save->oc_uid;
242 tc->fsgid = save->oc_gid;
243 tc->cap_effective = save->oc_cap;
249 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
251 return lu_context_key_get(&env->le_ctx, &osd_key);
255 * Concurrency: doesn't matter
257 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
259 return osd_oti_get(env)->oti_r_locks > 0;
263 * Concurrency: doesn't matter
265 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
267 struct osd_thread_info *oti = osd_oti_get(env);
268 return oti->oti_w_locks > 0 && o->oo_owner == env;
272 * Concurrency: doesn't access mutable data
274 static int osd_root_get(const struct lu_env *env,
275 struct dt_device *dev, struct lu_fid *f)
279 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
280 LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
285 * OSD object methods.
289 * Concurrency: no concurrent access is possible that early in object
292 static struct lu_object *osd_object_alloc(const struct lu_env *env,
293 const struct lu_object_header *hdr,
296 struct osd_object *mo;
302 l = &mo->oo_dt.do_lu;
303 dt_object_init(&mo->oo_dt, NULL, d);
304 if (osd_dev(d)->od_iop_mode)
305 mo->oo_dt.do_ops = &osd_obj_ea_ops;
307 mo->oo_dt.do_ops = &osd_obj_ops;
309 l->lo_ops = &osd_lu_obj_ops;
310 cfs_init_rwsem(&mo->oo_sem);
311 cfs_init_rwsem(&mo->oo_ext_idx_sem);
312 cfs_spin_lock_init(&mo->oo_guard);
319 * retrieve object from backend ext fs.
321 static struct inode *osd_iget(struct osd_thread_info *info,
322 struct osd_device *dev,
323 const struct osd_inode_id *id)
325 struct inode *inode = NULL;
327 #ifdef HAVE_EXT4_LDISKFS
328 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
330 /* Newer kernels return an error instead of a NULL pointer */
333 inode = iget(osd_sb(dev), id->oii_ino);
336 CERROR("no inode\n");
337 inode = ERR_PTR(-EACCES);
338 } else if (id->oii_gen != OSD_OII_NOGEN &&
339 inode->i_generation != id->oii_gen) {
341 inode = ERR_PTR(-ESTALE);
342 } else if (inode->i_nlink == 0) {
343 /* due to parallel readdir and unlink,
344 * we can have dead inode here. */
345 CWARN("stale inode\n");
346 make_bad_inode(inode);
348 inode = ERR_PTR(-ESTALE);
349 } else if (is_bad_inode(inode)) {
350 CERROR("bad inode %lx\n",inode->i_ino);
352 inode = ERR_PTR(-ENOENT);
354 /* Do not update file c/mtime in ldiskfs.
355 * NB: we don't have any lock to protect this because we don't
356 * have reference on osd_object now, but contention with
357 * another lookup + attr_set can't happen in the tiny window
358 * between if (...) and set S_NOCMTIME. */
359 if (!(inode->i_flags & S_NOCMTIME))
360 inode->i_flags |= S_NOCMTIME;
365 static int osd_fid_lookup(const struct lu_env *env,
366 struct osd_object *obj, const struct lu_fid *fid)
368 struct osd_thread_info *info;
369 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
370 struct osd_device *dev;
371 struct osd_inode_id *id;
376 LINVRNT(osd_invariant(obj));
377 LASSERT(obj->oo_inode == NULL);
378 LASSERT(fid_is_sane(fid) || osd_fid_is_root(fid));
380 * This assertion checks that osd layer sees only local
381 * fids. Unfortunately it is somewhat expensive (does a
382 * cache-lookup). Disabling it for production/acceptance-testing.
384 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
388 info = osd_oti_get(env);
393 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
396 result = osd_oi_lookup(info, oi, fid, id);
398 inode = osd_iget(info, dev, id);
399 if (!IS_ERR(inode)) {
400 obj->oo_inode = inode;
401 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
402 if (dev->od_iop_mode) {
403 obj->oo_compat_dot_created = 1;
404 obj->oo_compat_dotdot_created = 1;
409 * If fid wasn't found in oi, inode-less object is
410 * created, for which lu_object_exists() returns
411 * false. This is used in a (frequent) case when
412 * objects are created as locking anchors or
413 * place holders for objects yet to be created.
415 result = PTR_ERR(inode);
416 } else if (result == -ENOENT)
418 LINVRNT(osd_invariant(obj));
424 * Concurrency: shouldn't matter.
426 static void osd_object_init0(struct osd_object *obj)
428 LASSERT(obj->oo_inode != NULL);
429 obj->oo_dt.do_body_ops = &osd_body_ops;
430 obj->oo_dt.do_lu.lo_header->loh_attr |=
431 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
435 * Concurrency: no concurrent access is possible that early in object
438 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
439 const struct lu_object_conf *unused)
441 struct osd_object *obj = osd_obj(l);
444 LINVRNT(osd_invariant(obj));
446 result = osd_fid_lookup(env, obj, lu_object_fid(l));
448 if (obj->oo_inode != NULL)
449 osd_object_init0(obj);
451 LINVRNT(osd_invariant(obj));
456 * Concurrency: no concurrent access is possible that late in object
459 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
461 struct osd_object *obj = osd_obj(l);
463 LINVRNT(osd_invariant(obj));
465 dt_object_fini(&obj->oo_dt);
472 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
473 const struct iam_container *bag)
475 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
476 osd_oti_get(env)->oti_it_ipd);
479 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
480 const struct iam_container *bag)
482 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
483 osd_oti_get(env)->oti_idx_ipd);
486 static void osd_ipd_put(const struct lu_env *env,
487 const struct iam_container *bag,
488 struct iam_path_descr *ipd)
490 bag->ic_descr->id_ops->id_ipd_free(ipd);
494 * Concurrency: no concurrent access is possible that late in object
497 static void osd_index_fini(struct osd_object *o)
499 struct iam_container *bag;
501 if (o->oo_dir != NULL) {
502 bag = &o->oo_dir->od_container;
503 if (o->oo_inode != NULL) {
504 if (bag->ic_object == o->oo_inode)
505 iam_container_fini(bag);
507 OBD_FREE_PTR(o->oo_dir);
513 * Concurrency: no concurrent access is possible that late in object
514 * life-cycle (for all existing callers, that is. New callers have to provide
515 * their own locking.)
517 static int osd_inode_unlinked(const struct inode *inode)
519 return inode->i_nlink == 0;
523 OSD_TXN_OI_DELETE_CREDITS = 20,
524 OSD_TXN_INODE_DELETE_CREDITS = 20
531 #if OSD_THANDLE_STATS
533 * Set time when the handle is allocated
535 static void osd_th_alloced(struct osd_thandle *oth)
537 oth->oth_alloced = cfs_time_current();
541 * Set time when the handle started
543 static void osd_th_started(struct osd_thandle *oth)
545 oth->oth_started = cfs_time_current();
549 * Helper function to convert time interval to microseconds packed in
550 * long int (default time units for the counter in "stats" initialized
551 * by lu_time_init() )
553 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
557 cfs_duration_usec(cfs_time_sub(end, start), &val);
558 return val.tv_sec * 1000000 + val.tv_usec;
562 * Check whether the we deal with this handle for too long.
564 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
565 cfs_time_t alloced, cfs_time_t started,
568 cfs_time_t now = cfs_time_current();
570 LASSERT(dev != NULL);
572 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
573 interval_to_usec(alloced, started));
574 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
575 interval_to_usec(started, closed));
576 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
577 interval_to_usec(closed, now));
579 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
580 CWARN("transaction handle %p was open for too long: "
582 "alloced "CFS_TIME_T" ,"
583 "started "CFS_TIME_T" ,"
584 "closed "CFS_TIME_T"\n",
585 oth, now, alloced, started, closed);
586 libcfs_debug_dumpstack(NULL);
590 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
592 cfs_time_t __closed = cfs_time_current(); \
593 cfs_time_t __alloced = oth->oth_alloced; \
594 cfs_time_t __started = oth->oth_started; \
597 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
600 #else /* OSD_THANDLE_STATS */
602 #define osd_th_alloced(h) do {} while(0)
603 #define osd_th_started(h) do {} while(0)
604 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
606 #endif /* OSD_THANDLE_STATS */
609 * Concurrency: doesn't access mutable data.
611 static int osd_param_is_sane(const struct osd_device *dev,
612 const struct txn_param *param)
614 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
618 * Concurrency: shouldn't matter.
620 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
622 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
623 struct thandle *th = &oh->ot_super;
624 struct dt_device *dev = th->th_dev;
625 struct lu_device *lud = &dev->dd_lu_dev;
627 LASSERT(dev != NULL);
628 LASSERT(oh->ot_handle == NULL);
631 CERROR("transaction @0x%p commit error: %d\n", th, error);
633 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
635 * This od_env_for_commit is only for commit usage. see
638 lu_context_enter(&env->le_ctx);
639 dt_txn_hook_commit(env, th);
640 lu_context_exit(&env->le_ctx);
643 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
647 lu_context_exit(&th->th_ctx);
648 lu_context_fini(&th->th_ctx);
653 * Concurrency: shouldn't matter.
655 static struct thandle *osd_trans_start(const struct lu_env *env,
659 struct osd_device *dev = osd_dt_dev(d);
661 struct osd_thandle *oh;
667 hook_res = dt_txn_hook_start(env, d, p);
669 RETURN(ERR_PTR(hook_res));
671 if (osd_param_is_sane(dev, p)) {
672 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
674 struct osd_thread_info *oti = osd_oti_get(env);
677 * XXX temporary stuff. Some abstraction layer should
682 jh = ldiskfs_journal_start_sb(osd_sb(dev), p->tp_credits);
689 jh->h_sync = p->tp_sync;
690 lu_device_get(&d->dd_lu_dev);
691 oh->ot_dev_link = lu_ref_add
692 (&d->dd_lu_dev.ld_reference,
694 /* add commit callback */
695 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
696 lu_context_enter(&th->th_ctx);
697 osd_journal_callback_set(jh, osd_trans_commit_cb,
698 (struct journal_callback *)&oh->ot_jcb);
699 LASSERT(oti->oti_txns == 0);
700 LASSERT(oti->oti_r_locks == 0);
701 LASSERT(oti->oti_w_locks == 0);
708 th = ERR_PTR(-ENOMEM);
710 CERROR("Invalid transaction parameters\n");
711 th = ERR_PTR(-EINVAL);
718 * Concurrency: shouldn't matter.
720 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
723 struct osd_thandle *oh;
724 struct osd_thread_info *oti = osd_oti_get(env);
728 oh = container_of0(th, struct osd_thandle, ot_super);
729 if (oh->ot_handle != NULL) {
730 handle_t *hdl = oh->ot_handle;
732 LASSERT(oti->oti_txns == 1);
734 LASSERT(oti->oti_r_locks == 0);
735 LASSERT(oti->oti_w_locks == 0);
736 result = dt_txn_hook_stop(env, th);
738 CERROR("Failure in transaction hook: %d\n", result);
739 oh->ot_handle = NULL;
740 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
741 result = ldiskfs_journal_stop(hdl));
743 CERROR("Failure to stop transaction: %d\n", result);
749 * Concurrency: no concurrent access is possible that late in object
752 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
754 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
755 struct osd_device *osd = osd_obj2dev(obj);
756 struct osd_thread_info *oti = osd_oti_get(env);
757 struct txn_param *prm = &oti->oti_txn;
758 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
762 lu_env_init(env_del_obj, LCT_DT_THREAD);
763 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
764 OSD_TXN_INODE_DELETE_CREDITS);
765 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
767 result = osd_oi_delete(osd_oti_get(env_del_obj),
768 &osd->od_oi, fid, th);
769 osd_trans_stop(env_del_obj, th);
771 result = PTR_ERR(th);
773 lu_env_fini(env_del_obj);
778 * Called just before object is freed. Releases all resources except for
779 * object itself (that is released by osd_object_free()).
781 * Concurrency: no concurrent access is possible that late in object
784 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
786 struct osd_object *obj = osd_obj(l);
787 struct inode *inode = obj->oo_inode;
789 LINVRNT(osd_invariant(obj));
792 * If object is unlinked remove fid->ino mapping from object index.
799 if (osd_inode_unlinked(inode)) {
800 result = osd_inode_remove(env, obj);
802 LU_OBJECT_DEBUG(D_ERROR, env, l,
803 "Failed to cleanup: %d\n",
808 obj->oo_inode = NULL;
813 * Concurrency: ->loo_object_release() is called under site spin-lock.
815 static void osd_object_release(const struct lu_env *env,
818 struct osd_object *o = osd_obj(l);
820 LASSERT(!lu_object_is_dying(l->lo_header));
821 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
822 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
826 * Concurrency: shouldn't matter.
828 static int osd_object_print(const struct lu_env *env, void *cookie,
829 lu_printer_t p, const struct lu_object *l)
831 struct osd_object *o = osd_obj(l);
834 if (o->oo_dir != NULL)
835 d = o->oo_dir->od_container.ic_descr;
838 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
840 o->oo_inode ? o->oo_inode->i_ino : 0UL,
841 o->oo_inode ? o->oo_inode->i_generation : 0,
842 d ? d->id_ops->id_name : "plain");
846 * Concurrency: shouldn't matter.
848 int osd_statfs(const struct lu_env *env, struct dt_device *d,
851 struct osd_device *osd = osd_dt_dev(d);
852 struct super_block *sb = osd_sb(osd);
855 cfs_spin_lock(&osd->od_osfs_lock);
857 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
858 result = ll_do_statfs(sb, &osd->od_kstatfs);
859 if (likely(result == 0)) /* N.B. statfs can't really fail */
860 osd->od_osfs_age = cfs_time_current_64();
863 if (likely(result == 0))
864 *sfs = osd->od_kstatfs;
865 cfs_spin_unlock(&osd->od_osfs_lock);
871 * Concurrency: doesn't access mutable data.
873 static void osd_conf_get(const struct lu_env *env,
874 const struct dt_device *dev,
875 struct dt_device_param *param)
878 * XXX should be taken from not-yet-existing fs abstraction layer.
880 param->ddp_max_name_len = LDISKFS_NAME_LEN;
881 param->ddp_max_nlink = LDISKFS_LINK_MAX;
882 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
886 * Helper function to get and fill the buffer with input values.
888 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
892 buf = &osd_oti_get(env)->oti_buf;
899 * Concurrency: shouldn't matter.
901 static int osd_sync(const struct lu_env *env, struct dt_device *d)
903 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
904 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
908 * Start commit for OSD device.
910 * An implementation of dt_commit_async method for OSD device.
911 * Asychronously starts underlayng fs sync and thereby a transaction
914 * \param env environment
917 * \see dt_device_operations
919 static int osd_commit_async(const struct lu_env *env,
922 struct super_block *s = osd_sb(osd_dt_dev(d));
925 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
926 RETURN(s->s_op->sync_fs(s, 0));
930 * Concurrency: shouldn't matter.
932 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
934 static void osd_ro(const struct lu_env *env, struct dt_device *d)
938 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
940 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
941 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
947 * Concurrency: serialization provided by callers.
949 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
950 int mode, unsigned long timeout, __u32 alg,
951 struct lustre_capa_key *keys)
953 struct osd_device *dev = osd_dt_dev(d);
956 dev->od_fl_capa = mode;
957 dev->od_capa_timeout = timeout;
958 dev->od_capa_alg = alg;
959 dev->od_capa_keys = keys;
964 * Concurrency: serialization provided by callers.
966 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
967 struct dt_quota_ctxt *ctxt, void *data)
969 struct obd_device *obd = (void *)ctxt;
970 struct vfsmount *mnt = (struct vfsmount *)data;
973 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
974 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
975 obd->obd_lvfs_ctxt.pwdmnt = mnt;
976 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
977 obd->obd_lvfs_ctxt.fs = get_ds();
983 * Note: we do not count into QUOTA here.
984 * If we mount with --data_journal we may need more.
986 static const int osd_dto_credits_noquota[DTO_NR] = {
989 * INDEX_EXTRA_TRANS_BLOCKS(8) +
990 * SINGLEDATA_TRANS_BLOCKS(8)
991 * XXX Note: maybe iam need more, since iam have more level than
994 [DTO_INDEX_INSERT] = 16,
995 [DTO_INDEX_DELETE] = 16,
999 [DTO_IDNEX_UPDATE] = 16,
1001 * Create a object. The same as create object in EXT3.
1002 * DATA_TRANS_BLOCKS(14) +
1003 * INDEX_EXTRA_BLOCKS(8) +
1004 * 3(inode bits, groups, GDT)
1006 [DTO_OBJECT_CREATE] = 25,
1010 [DTO_OBJECT_DELETE] = 25,
1013 * 3(inode bits, group, GDT)
1015 [DTO_ATTR_SET_BASE] = 3,
1017 * Xattr set. The same as xattr of EXT3.
1018 * DATA_TRANS_BLOCKS(14)
1019 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1020 * are also counted in. Do not know why?
1022 [DTO_XATTR_SET] = 14,
1025 * creadits for inode change during write.
1027 [DTO_WRITE_BASE] = 3,
1029 * credits for single block write.
1031 [DTO_WRITE_BLOCK] = 14,
1033 * Attr set credits for chown.
1034 * This is extra credits for setattr, and it is null without quota
1036 [DTO_ATTR_SET_CHOWN]= 0
1040 * Note: we count into QUOTA here.
1041 * If we mount with --data_journal we may need more.
1043 static const int osd_dto_credits_quota[DTO_NR] = {
1045 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1046 * SINGLEDATA_TRANS_BLOCKS(8) +
1047 * 2 * QUOTA_TRANS_BLOCKS(2)
1049 [DTO_INDEX_INSERT] = 20,
1051 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1052 * SINGLEDATA_TRANS_BLOCKS(8) +
1053 * 2 * QUOTA_TRANS_BLOCKS(2)
1055 [DTO_INDEX_DELETE] = 20,
1059 [DTO_IDNEX_UPDATE] = 16,
1061 * Create a object. Same as create object in EXT3 filesystem.
1062 * DATA_TRANS_BLOCKS(16) +
1063 * INDEX_EXTRA_BLOCKS(8) +
1064 * 3(inode bits, groups, GDT) +
1065 * 2 * QUOTA_INIT_BLOCKS(25)
1067 [DTO_OBJECT_CREATE] = 77,
1070 * DATA_TRANS_BLOCKS(16) +
1071 * INDEX_EXTRA_BLOCKS(8) +
1072 * 3(inode bits, groups, GDT) +
1075 [DTO_OBJECT_DELETE] = 27,
1078 * 3 (inode bit, group, GDT) +
1080 [DTO_ATTR_SET_BASE] = 3,
1082 * Xattr set. The same as xattr of EXT3.
1083 * DATA_TRANS_BLOCKS(16)
1084 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
1085 * also counted in. Do not know why?
1087 [DTO_XATTR_SET] = 16,
1090 * creadits for inode change during write.
1092 [DTO_WRITE_BASE] = 3,
1094 * credits for single block write.
1096 [DTO_WRITE_BLOCK] = 16,
1098 * Attr set credits for chown.
1099 * It is added to already set setattr credits
1100 * 2 * QUOTA_INIT_BLOCKS(25) +
1101 * 2 * QUOTA_DEL_BLOCKS(9)
1103 [DTO_ATTR_SET_CHOWN]= 68,
1106 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1109 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1110 ARRAY_SIZE(osd_dto_credits_quota));
1111 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1112 #ifdef HAVE_QUOTA_SUPPORT
1113 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1114 return osd_dto_credits_quota[op];
1117 return osd_dto_credits_noquota[op];
1120 static const struct dt_device_operations osd_dt_ops = {
1121 .dt_root_get = osd_root_get,
1122 .dt_statfs = osd_statfs,
1123 .dt_trans_start = osd_trans_start,
1124 .dt_trans_stop = osd_trans_stop,
1125 .dt_conf_get = osd_conf_get,
1126 .dt_sync = osd_sync,
1128 .dt_commit_async = osd_commit_async,
1129 .dt_credit_get = osd_credit_get,
1130 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1131 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1134 static void osd_object_read_lock(const struct lu_env *env,
1135 struct dt_object *dt, unsigned role)
1137 struct osd_object *obj = osd_dt_obj(dt);
1138 struct osd_thread_info *oti = osd_oti_get(env);
1140 LINVRNT(osd_invariant(obj));
1142 LASSERT(obj->oo_owner != env);
1143 cfs_down_read_nested(&obj->oo_sem, role);
1145 LASSERT(obj->oo_owner == NULL);
1149 static void osd_object_write_lock(const struct lu_env *env,
1150 struct dt_object *dt, unsigned role)
1152 struct osd_object *obj = osd_dt_obj(dt);
1153 struct osd_thread_info *oti = osd_oti_get(env);
1155 LINVRNT(osd_invariant(obj));
1157 LASSERT(obj->oo_owner != env);
1158 cfs_down_write_nested(&obj->oo_sem, role);
1160 LASSERT(obj->oo_owner == NULL);
1161 obj->oo_owner = env;
1165 static void osd_object_read_unlock(const struct lu_env *env,
1166 struct dt_object *dt)
1168 struct osd_object *obj = osd_dt_obj(dt);
1169 struct osd_thread_info *oti = osd_oti_get(env);
1171 LINVRNT(osd_invariant(obj));
1173 LASSERT(oti->oti_r_locks > 0);
1175 cfs_up_read(&obj->oo_sem);
1178 static void osd_object_write_unlock(const struct lu_env *env,
1179 struct dt_object *dt)
1181 struct osd_object *obj = osd_dt_obj(dt);
1182 struct osd_thread_info *oti = osd_oti_get(env);
1184 LINVRNT(osd_invariant(obj));
1186 LASSERT(obj->oo_owner == env);
1187 LASSERT(oti->oti_w_locks > 0);
1189 obj->oo_owner = NULL;
1190 cfs_up_write(&obj->oo_sem);
1193 static int osd_object_write_locked(const struct lu_env *env,
1194 struct dt_object *dt)
1196 struct osd_object *obj = osd_dt_obj(dt);
1198 LINVRNT(osd_invariant(obj));
1200 return obj->oo_owner == env;
1203 static int capa_is_sane(const struct lu_env *env,
1204 struct osd_device *dev,
1205 struct lustre_capa *capa,
1206 struct lustre_capa_key *keys)
1208 struct osd_thread_info *oti = osd_oti_get(env);
1209 struct lustre_capa *tcapa = &oti->oti_capa;
1210 struct obd_capa *oc;
1214 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1216 if (capa_is_expired(oc)) {
1217 DEBUG_CAPA(D_ERROR, capa, "expired");
1224 if (capa_is_expired_sec(capa)) {
1225 DEBUG_CAPA(D_ERROR, capa, "expired");
1229 cfs_spin_lock(&capa_lock);
1230 for (i = 0; i < 2; i++) {
1231 if (keys[i].lk_keyid == capa->lc_keyid) {
1232 oti->oti_capa_key = keys[i];
1236 cfs_spin_unlock(&capa_lock);
1239 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1243 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1247 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1248 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1252 oc = capa_add(dev->od_capa_hash, capa);
1258 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1259 struct lustre_capa *capa, __u64 opc)
1261 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1262 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1263 struct md_capainfo *ci;
1266 if (!dev->od_fl_capa)
1269 if (capa == BYPASS_CAPA)
1272 ci = md_capainfo(env);
1276 if (ci->mc_auth == LC_ID_NONE)
1280 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1284 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1285 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1290 if (!capa_opc_supported(capa, opc)) {
1291 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1295 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1296 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1303 static struct timespec *osd_inode_time(const struct lu_env *env,
1304 struct inode *inode, __u64 seconds)
1306 struct osd_thread_info *oti = osd_oti_get(env);
1307 struct timespec *t = &oti->oti_time;
1309 t->tv_sec = seconds;
1311 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1316 static void osd_inode_getattr(const struct lu_env *env,
1317 struct inode *inode, struct lu_attr *attr)
1319 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1320 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1321 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1323 attr->la_atime = LTIME_S(inode->i_atime);
1324 attr->la_mtime = LTIME_S(inode->i_mtime);
1325 attr->la_ctime = LTIME_S(inode->i_ctime);
1326 attr->la_mode = inode->i_mode;
1327 attr->la_size = i_size_read(inode);
1328 attr->la_blocks = inode->i_blocks;
1329 attr->la_uid = inode->i_uid;
1330 attr->la_gid = inode->i_gid;
1331 attr->la_flags = LDISKFS_I(inode)->i_flags;
1332 attr->la_nlink = inode->i_nlink;
1333 attr->la_rdev = inode->i_rdev;
1334 attr->la_blksize = ll_inode_blksize(inode);
1335 attr->la_blkbits = inode->i_blkbits;
1338 static int osd_attr_get(const struct lu_env *env,
1339 struct dt_object *dt,
1340 struct lu_attr *attr,
1341 struct lustre_capa *capa)
1343 struct osd_object *obj = osd_dt_obj(dt);
1345 LASSERT(dt_object_exists(dt));
1346 LINVRNT(osd_invariant(obj));
1348 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1351 cfs_spin_lock(&obj->oo_guard);
1352 osd_inode_getattr(env, obj->oo_inode, attr);
1353 cfs_spin_unlock(&obj->oo_guard);
1357 static int osd_inode_setattr(const struct lu_env *env,
1358 struct inode *inode, const struct lu_attr *attr)
1362 bits = attr->la_valid;
1364 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1366 #ifdef HAVE_QUOTA_SUPPORT
1367 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1368 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1369 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1375 iattr.ia_valid |= ATTR_UID;
1377 iattr.ia_valid |= ATTR_GID;
1378 iattr.ia_uid = attr->la_uid;
1379 iattr.ia_gid = attr->la_gid;
1380 osd_push_ctxt(env, save);
1381 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1388 if (bits & LA_ATIME)
1389 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1390 if (bits & LA_CTIME)
1391 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1392 if (bits & LA_MTIME)
1393 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1394 if (bits & LA_SIZE) {
1395 LDISKFS_I(inode)->i_disksize = attr->la_size;
1396 i_size_write(inode, attr->la_size);
1400 /* OSD should not change "i_blocks" which is used by quota.
1401 * "i_blocks" should be changed by ldiskfs only. */
1402 if (bits & LA_BLOCKS)
1403 inode->i_blocks = attr->la_blocks;
1406 inode->i_mode = (inode->i_mode & S_IFMT) |
1407 (attr->la_mode & ~S_IFMT);
1409 inode->i_uid = attr->la_uid;
1411 inode->i_gid = attr->la_gid;
1412 if (bits & LA_NLINK)
1413 inode->i_nlink = attr->la_nlink;
1415 inode->i_rdev = attr->la_rdev;
1417 if (bits & LA_FLAGS) {
1418 /* always keep S_NOCMTIME */
1419 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1425 static int osd_attr_set(const struct lu_env *env,
1426 struct dt_object *dt,
1427 const struct lu_attr *attr,
1428 struct thandle *handle,
1429 struct lustre_capa *capa)
1431 struct osd_object *obj = osd_dt_obj(dt);
1434 LASSERT(handle != NULL);
1435 LASSERT(dt_object_exists(dt));
1436 LASSERT(osd_invariant(obj));
1438 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1441 cfs_spin_lock(&obj->oo_guard);
1442 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1443 cfs_spin_unlock(&obj->oo_guard);
1446 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1453 * XXX temporary solution.
1455 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1456 struct lu_attr *attr, struct thandle *th)
1461 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1462 struct lu_attr *attr, struct thandle *th)
1464 osd_object_init0(obj);
1465 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1466 unlock_new_inode(obj->oo_inode);
1470 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1471 struct osd_object *obj,
1475 struct osd_thread_info *info = osd_oti_get(env);
1476 struct dentry *child_dentry = &info->oti_child_dentry;
1477 struct dentry *obj_dentry = &info->oti_obj_dentry;
1479 obj_dentry->d_inode = obj->oo_inode;
1480 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1481 obj_dentry->d_name.hash = 0;
1483 child_dentry->d_name.hash = 0;
1484 child_dentry->d_parent = obj_dentry;
1485 child_dentry->d_name.name = name;
1486 child_dentry->d_name.len = namelen;
1487 return child_dentry;
1491 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1493 struct dt_allocation_hint *hint,
1497 struct osd_device *osd = osd_obj2dev(obj);
1498 struct osd_thandle *oth;
1499 struct dt_object *parent;
1500 struct inode *inode;
1501 #ifdef HAVE_QUOTA_SUPPORT
1502 struct osd_ctxt *save = &info->oti_ctxt;
1505 LINVRNT(osd_invariant(obj));
1506 LASSERT(obj->oo_inode == NULL);
1508 oth = container_of(th, struct osd_thandle, ot_super);
1509 LASSERT(oth->ot_handle->h_transaction != NULL);
1511 if (hint && hint->dah_parent)
1512 parent = hint->dah_parent;
1514 parent = osd->od_obj_area;
1516 LASSERT(parent != NULL);
1517 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1519 #ifdef HAVE_QUOTA_SUPPORT
1520 osd_push_ctxt(info->oti_env, save);
1522 inode = ldiskfs_create_inode(oth->ot_handle,
1523 osd_dt_obj(parent)->oo_inode, mode);
1524 #ifdef HAVE_QUOTA_SUPPORT
1527 if (!IS_ERR(inode)) {
1528 /* Do not update file c/mtime in ldiskfs.
1529 * NB: don't need any lock because no contention at this
1531 inode->i_flags |= S_NOCMTIME;
1532 obj->oo_inode = inode;
1535 result = PTR_ERR(inode);
1536 LINVRNT(osd_invariant(obj));
1544 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1545 struct lu_attr *attr,
1546 struct dt_allocation_hint *hint,
1547 struct dt_object_format *dof,
1551 struct osd_thandle *oth;
1552 struct osd_device *osd = osd_obj2dev(obj);
1553 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1555 LASSERT(S_ISDIR(attr->la_mode));
1557 oth = container_of(th, struct osd_thandle, ot_super);
1558 LASSERT(oth->ot_handle->h_transaction != NULL);
1559 result = osd_mkfile(info, obj, mode, hint, th);
1560 if (result == 0 && osd->od_iop_mode == 0) {
1561 LASSERT(obj->oo_inode != NULL);
1563 * XXX uh-oh... call low-level iam function directly.
1566 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1567 sizeof (struct osd_fid_pack),
1573 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1574 struct lu_attr *attr,
1575 struct dt_allocation_hint *hint,
1576 struct dt_object_format *dof,
1580 struct osd_thandle *oth;
1581 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1583 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1585 LASSERT(S_ISREG(attr->la_mode));
1587 oth = container_of(th, struct osd_thandle, ot_super);
1588 LASSERT(oth->ot_handle->h_transaction != NULL);
1590 result = osd_mkfile(info, obj, mode, hint, th);
1592 LASSERT(obj->oo_inode != NULL);
1593 if (feat->dif_flags & DT_IND_VARKEY)
1594 result = iam_lvar_create(obj->oo_inode,
1595 feat->dif_keysize_max,
1597 feat->dif_recsize_max,
1600 result = iam_lfix_create(obj->oo_inode,
1601 feat->dif_keysize_max,
1603 feat->dif_recsize_max,
1610 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1611 struct lu_attr *attr,
1612 struct dt_allocation_hint *hint,
1613 struct dt_object_format *dof,
1616 LASSERT(S_ISREG(attr->la_mode));
1617 return osd_mkfile(info, obj, (attr->la_mode &
1618 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1621 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1622 struct lu_attr *attr,
1623 struct dt_allocation_hint *hint,
1624 struct dt_object_format *dof,
1627 LASSERT(S_ISLNK(attr->la_mode));
1628 return osd_mkfile(info, obj, (attr->la_mode &
1629 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1632 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1633 struct lu_attr *attr,
1634 struct dt_allocation_hint *hint,
1635 struct dt_object_format *dof,
1638 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1641 LINVRNT(osd_invariant(obj));
1642 LASSERT(obj->oo_inode == NULL);
1643 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1644 S_ISFIFO(mode) || S_ISSOCK(mode));
1646 result = osd_mkfile(info, obj, mode, hint, th);
1648 LASSERT(obj->oo_inode != NULL);
1649 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1651 LINVRNT(osd_invariant(obj));
1655 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1657 struct dt_allocation_hint *hint,
1658 struct dt_object_format *dof,
1661 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1663 osd_obj_type_f result;
1679 result = osd_mk_index;
1690 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1691 struct dt_object *parent, cfs_umode_t child_mode)
1695 memset(ah, 0, sizeof(*ah));
1696 ah->dah_parent = parent;
1697 ah->dah_mode = child_mode;
1701 * Helper function for osd_object_create()
1703 * \retval 0, on success
1705 static int __osd_object_create(struct osd_thread_info *info,
1706 struct osd_object *obj, struct lu_attr *attr,
1707 struct dt_allocation_hint *hint,
1708 struct dt_object_format *dof,
1714 result = osd_create_pre(info, obj, attr, th);
1716 result = osd_create_type_f(dof->dof_type)(info, obj,
1717 attr, hint, dof, th);
1719 result = osd_create_post(info, obj, attr, th);
1725 * Helper function for osd_object_create()
1727 * \retval 0, on success
1729 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1730 const struct lu_fid *fid, struct thandle *th)
1732 struct osd_thread_info *info = osd_oti_get(env);
1733 struct osd_inode_id *id = &info->oti_id;
1734 struct osd_device *osd = osd_obj2dev(obj);
1735 struct md_ucred *uc = md_ucred(env);
1737 LASSERT(obj->oo_inode != NULL);
1738 LASSERT(uc != NULL);
1740 id->oii_ino = obj->oo_inode->i_ino;
1741 id->oii_gen = obj->oo_inode->i_generation;
1743 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1744 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1747 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1748 struct lu_attr *attr,
1749 struct dt_allocation_hint *hint,
1750 struct dt_object_format *dof,
1753 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1754 struct osd_object *obj = osd_dt_obj(dt);
1755 struct osd_thread_info *info = osd_oti_get(env);
1760 LINVRNT(osd_invariant(obj));
1761 LASSERT(!dt_object_exists(dt));
1762 LASSERT(osd_write_locked(env, obj));
1763 LASSERT(th != NULL);
1765 result = __osd_object_create(info, obj, attr, hint, dof, th);
1767 result = __osd_oi_insert(env, obj, fid, th);
1769 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1770 LASSERT(osd_invariant(obj));
1775 * Helper function for osd_xattr_set()
1777 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1778 const struct lu_buf *buf, const char *name, int fl)
1780 struct osd_object *obj = osd_dt_obj(dt);
1781 struct inode *inode = obj->oo_inode;
1782 struct osd_thread_info *info = osd_oti_get(env);
1783 struct dentry *dentry = &info->oti_child_dentry;
1787 LASSERT(dt_object_exists(dt));
1788 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1789 LASSERT(osd_write_locked(env, obj));
1791 if (fl & LU_XATTR_REPLACE)
1792 fs_flags |= XATTR_REPLACE;
1794 if (fl & LU_XATTR_CREATE)
1795 fs_flags |= XATTR_CREATE;
1797 dentry->d_inode = inode;
1798 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1799 buf->lb_len, fs_flags);
1804 * Put the fid into lustre_mdt_attrs, and then place the structure
1805 * inode's ea. This fid should not be altered during the life time
1808 * \retval +ve, on success
1809 * \retval -ve, on error
1811 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1813 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1814 const struct lu_fid *fid)
1816 struct osd_thread_info *info = osd_oti_get(env);
1817 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1819 lustre_lma_init(mdt_attrs, fid);
1820 lustre_lma_swab(mdt_attrs);
1821 return __osd_xattr_set(env, dt,
1822 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1823 XATTR_NAME_LMA, LU_XATTR_CREATE);
1828 * Helper function to form igif
1830 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1833 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1837 * Helper function to pack the fid, ldiskfs stores fid in packed format.
1839 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1840 struct lu_fid *befider)
1842 fid_cpu_to_be(befider, (struct lu_fid *)fid);
1843 memcpy(pack->fp_area, befider, sizeof(*befider));
1844 pack->fp_len = sizeof(*befider) + 1;
1848 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1849 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1850 * To have compatilibility with 1.8 ldiskfs driver we need to have
1851 * magic number at start of fid data.
1852 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1855 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1856 const struct dt_rec *fid)
1858 param->edp_magic = LDISKFS_LUFID_MAGIC;
1859 param->edp_len = sizeof(struct lu_fid) + 1;
1861 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1862 (struct lu_fid *)fid);
1865 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1870 switch (pack->fp_len) {
1871 case sizeof *fid + 1:
1872 memcpy(fid, pack->fp_area, sizeof *fid);
1873 fid_be_to_cpu(fid, fid);
1876 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1883 * Try to read the fid from inode ea into dt_rec, if return value
1884 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1886 * \param fid object fid.
1888 * \retval 0 on success
1890 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1891 __u32 ino, struct lu_fid *fid)
1893 struct osd_thread_info *info = osd_oti_get(env);
1894 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1895 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1896 struct dentry *dentry = &info->oti_child_dentry;
1897 struct osd_inode_id *id = &info->oti_id;
1898 struct osd_device *dev;
1899 struct inode *inode;
1903 dev = osd_dev(ldev);
1906 id->oii_gen = OSD_OII_NOGEN;
1908 inode = osd_iget(info, dev, id);
1909 if (IS_ERR(inode)) {
1910 rc = PTR_ERR(inode);
1913 dentry->d_inode = inode;
1915 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1916 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1919 /* Check LMA compatibility */
1921 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
1922 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1923 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
1924 ~LMA_INCOMPAT_SUPP);
1929 lustre_lma_swab(mdt_attrs);
1930 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
1932 } else if (rc == -ENODATA) {
1933 osd_igif_get(env, inode, fid);
1942 * OSD layer object create function for interoperability mode (b11826).
1943 * This is mostly similar to osd_object_create(). Only difference being, fid is
1944 * inserted into inode ea here.
1946 * \retval 0, on success
1947 * \retval -ve, on error
1949 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1950 struct lu_attr *attr,
1951 struct dt_allocation_hint *hint,
1952 struct dt_object_format *dof,
1955 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1956 struct osd_object *obj = osd_dt_obj(dt);
1957 struct osd_thread_info *info = osd_oti_get(env);
1962 LASSERT(osd_invariant(obj));
1963 LASSERT(!dt_object_exists(dt));
1964 LASSERT(osd_write_locked(env, obj));
1965 LASSERT(th != NULL);
1967 result = __osd_object_create(info, obj, attr, hint, dof, th);
1969 /* objects under osd root shld have igif fid, so dont add fid EA */
1970 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
1971 result = osd_ea_fid_set(env, dt, fid);
1974 result = __osd_oi_insert(env, obj, fid, th);
1976 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1977 LINVRNT(osd_invariant(obj));
1982 * Concurrency: @dt is write locked.
1984 static void osd_object_ref_add(const struct lu_env *env,
1985 struct dt_object *dt,
1988 struct osd_object *obj = osd_dt_obj(dt);
1989 struct inode *inode = obj->oo_inode;
1991 LINVRNT(osd_invariant(obj));
1992 LASSERT(dt_object_exists(dt));
1993 LASSERT(osd_write_locked(env, obj));
1994 LASSERT(th != NULL);
1996 cfs_spin_lock(&obj->oo_guard);
1997 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1999 cfs_spin_unlock(&obj->oo_guard);
2000 inode->i_sb->s_op->dirty_inode(inode);
2001 LINVRNT(osd_invariant(obj));
2005 * Concurrency: @dt is write locked.
2007 static void osd_object_ref_del(const struct lu_env *env,
2008 struct dt_object *dt,
2011 struct osd_object *obj = osd_dt_obj(dt);
2012 struct inode *inode = obj->oo_inode;
2014 LINVRNT(osd_invariant(obj));
2015 LASSERT(dt_object_exists(dt));
2016 LASSERT(osd_write_locked(env, obj));
2017 LASSERT(th != NULL);
2019 cfs_spin_lock(&obj->oo_guard);
2020 LASSERT(inode->i_nlink > 0);
2022 cfs_spin_unlock(&obj->oo_guard);
2023 inode->i_sb->s_op->dirty_inode(inode);
2024 LINVRNT(osd_invariant(obj));
2028 * Concurrency: @dt is read locked.
2030 static int osd_xattr_get(const struct lu_env *env,
2031 struct dt_object *dt,
2034 struct lustre_capa *capa)
2036 struct osd_object *obj = osd_dt_obj(dt);
2037 struct inode *inode = obj->oo_inode;
2038 struct osd_thread_info *info = osd_oti_get(env);
2039 struct dentry *dentry = &info->oti_obj_dentry;
2041 LASSERT(dt_object_exists(dt));
2042 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2043 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2045 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2048 dentry->d_inode = inode;
2049 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2053 * Concurrency: @dt is write locked.
2055 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2056 const struct lu_buf *buf, const char *name, int fl,
2057 struct thandle *handle, struct lustre_capa *capa)
2059 LASSERT(handle != NULL);
2061 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2064 return __osd_xattr_set(env, dt, buf, name, fl);
2068 * Concurrency: @dt is read locked.
2070 static int osd_xattr_list(const struct lu_env *env,
2071 struct dt_object *dt,
2073 struct lustre_capa *capa)
2075 struct osd_object *obj = osd_dt_obj(dt);
2076 struct inode *inode = obj->oo_inode;
2077 struct osd_thread_info *info = osd_oti_get(env);
2078 struct dentry *dentry = &info->oti_obj_dentry;
2080 LASSERT(dt_object_exists(dt));
2081 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2082 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2084 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2087 dentry->d_inode = inode;
2088 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2092 * Concurrency: @dt is write locked.
2094 static int osd_xattr_del(const struct lu_env *env,
2095 struct dt_object *dt,
2097 struct thandle *handle,
2098 struct lustre_capa *capa)
2100 struct osd_object *obj = osd_dt_obj(dt);
2101 struct inode *inode = obj->oo_inode;
2102 struct osd_thread_info *info = osd_oti_get(env);
2103 struct dentry *dentry = &info->oti_obj_dentry;
2106 LASSERT(dt_object_exists(dt));
2107 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2108 LASSERT(osd_write_locked(env, obj));
2109 LASSERT(handle != NULL);
2111 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2114 dentry->d_inode = inode;
2115 rc = inode->i_op->removexattr(dentry, name);
2119 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2120 struct dt_object *dt,
2121 struct lustre_capa *old,
2124 struct osd_thread_info *info = osd_oti_get(env);
2125 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2126 struct osd_object *obj = osd_dt_obj(dt);
2127 struct osd_device *dev = osd_obj2dev(obj);
2128 struct lustre_capa_key *key = &info->oti_capa_key;
2129 struct lustre_capa *capa = &info->oti_capa;
2130 struct obd_capa *oc;
2131 struct md_capainfo *ci;
2135 if (!dev->od_fl_capa)
2136 RETURN(ERR_PTR(-ENOENT));
2138 LASSERT(dt_object_exists(dt));
2139 LINVRNT(osd_invariant(obj));
2141 /* renewal sanity check */
2142 if (old && osd_object_auth(env, dt, old, opc))
2143 RETURN(ERR_PTR(-EACCES));
2145 ci = md_capainfo(env);
2147 RETURN(ERR_PTR(-ENOENT));
2149 switch (ci->mc_auth) {
2153 capa->lc_uid = obj->oo_inode->i_uid;
2154 capa->lc_gid = obj->oo_inode->i_gid;
2155 capa->lc_flags = LC_ID_PLAIN;
2157 case LC_ID_CONVERT: {
2160 s[0] = obj->oo_inode->i_uid;
2161 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2162 s[2] = obj->oo_inode->i_gid;
2163 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2164 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2166 RETURN(ERR_PTR(rc));
2168 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2169 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2170 capa->lc_flags = LC_ID_CONVERT;
2174 RETURN(ERR_PTR(-EINVAL));
2177 capa->lc_fid = *fid;
2179 capa->lc_flags |= dev->od_capa_alg << 24;
2180 capa->lc_timeout = dev->od_capa_timeout;
2181 capa->lc_expiry = 0;
2183 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2185 LASSERT(!capa_is_expired(oc));
2189 cfs_spin_lock(&capa_lock);
2190 *key = dev->od_capa_keys[1];
2191 cfs_spin_unlock(&capa_lock);
2193 capa->lc_keyid = key->lk_keyid;
2194 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2196 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2198 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2199 RETURN(ERR_PTR(rc));
2202 oc = capa_add(dev->od_capa_hash, capa);
2206 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2209 struct osd_object *obj = osd_dt_obj(dt);
2210 struct inode *inode = obj->oo_inode;
2211 struct osd_thread_info *info = osd_oti_get(env);
2212 struct dentry *dentry = &info->oti_obj_dentry;
2213 struct file *file = &info->oti_file;
2216 dentry->d_inode = inode;
2217 file->f_dentry = dentry;
2218 file->f_mapping = inode->i_mapping;
2219 file->f_op = inode->i_fop;
2220 LOCK_INODE_MUTEX(inode);
2221 rc = file->f_op->fsync(file, dentry, 0);
2222 UNLOCK_INODE_MUTEX(inode);
2227 * Get the 64-bit version for an inode.
2229 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2230 struct dt_object *dt)
2232 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2234 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2235 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2236 return LDISKFS_I(inode)->i_fs_version;
2240 * Set the 64-bit version and return the old version.
2242 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2243 dt_obj_version_t new_version)
2245 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2247 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2248 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2249 LDISKFS_I(inode)->i_fs_version = new_version;
2250 /** Version is set after all inode operations are finished,
2251 * so we should mark it dirty here */
2252 inode->i_sb->s_op->dirty_inode(inode);
2255 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2258 struct osd_object *obj = osd_dt_obj(dt);
2261 *data = (void *)obj->oo_inode;
2269 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2270 const struct dt_index_features *feat)
2272 struct iam_descr *descr;
2274 if (osd_object_is_root(o))
2275 return feat == &dt_directory_features;
2277 LASSERT(o->oo_dir != NULL);
2279 descr = o->oo_dir->od_container.ic_descr;
2280 if (feat == &dt_directory_features) {
2281 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2287 feat->dif_keysize_min <= descr->id_key_size &&
2288 descr->id_key_size <= feat->dif_keysize_max &&
2289 feat->dif_recsize_min <= descr->id_rec_size &&
2290 descr->id_rec_size <= feat->dif_recsize_max &&
2291 !(feat->dif_flags & (DT_IND_VARKEY |
2292 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2293 ergo(feat->dif_flags & DT_IND_UPDATE,
2294 1 /* XXX check that object (and file system) is
2299 static int osd_iam_container_init(const struct lu_env *env,
2300 struct osd_object *obj,
2301 struct osd_directory *dir)
2304 struct iam_container *bag;
2306 bag = &dir->od_container;
2307 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2309 result = iam_container_setup(bag);
2311 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2313 iam_container_fini(bag);
2320 * Concurrency: no external locking is necessary.
2322 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2323 const struct dt_index_features *feat)
2327 struct osd_object *obj = osd_dt_obj(dt);
2328 struct osd_device *osd = osd_obj2dev(obj);
2330 LINVRNT(osd_invariant(obj));
2331 LASSERT(dt_object_exists(dt));
2333 if (osd_object_is_root(obj)) {
2334 dt->do_index_ops = &osd_index_ea_ops;
2336 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2337 dt->do_index_ops = &osd_index_ea_ops;
2338 if (S_ISDIR(obj->oo_inode->i_mode))
2343 } else if (!osd_has_index(obj)) {
2344 struct osd_directory *dir;
2349 cfs_spin_lock(&obj->oo_guard);
2350 if (obj->oo_dir == NULL)
2354 * Concurrent thread allocated container data.
2357 cfs_spin_unlock(&obj->oo_guard);
2359 * Now, that we have container data, serialize its
2362 cfs_down_write(&obj->oo_ext_idx_sem);
2364 * recheck under lock.
2366 if (!osd_has_index(obj))
2367 result = osd_iam_container_init(env, obj, dir);
2370 cfs_up_write(&obj->oo_ext_idx_sem);
2376 if (result == 0 && ea_dir == 0) {
2377 if (!osd_iam_index_probe(env, obj, feat))
2380 LINVRNT(osd_invariant(obj));
2385 static const struct dt_object_operations osd_obj_ops = {
2386 .do_read_lock = osd_object_read_lock,
2387 .do_write_lock = osd_object_write_lock,
2388 .do_read_unlock = osd_object_read_unlock,
2389 .do_write_unlock = osd_object_write_unlock,
2390 .do_write_locked = osd_object_write_locked,
2391 .do_attr_get = osd_attr_get,
2392 .do_attr_set = osd_attr_set,
2393 .do_ah_init = osd_ah_init,
2394 .do_create = osd_object_create,
2395 .do_index_try = osd_index_try,
2396 .do_ref_add = osd_object_ref_add,
2397 .do_ref_del = osd_object_ref_del,
2398 .do_xattr_get = osd_xattr_get,
2399 .do_xattr_set = osd_xattr_set,
2400 .do_xattr_del = osd_xattr_del,
2401 .do_xattr_list = osd_xattr_list,
2402 .do_capa_get = osd_capa_get,
2403 .do_object_sync = osd_object_sync,
2404 .do_version_get = osd_object_version_get,
2405 .do_version_set = osd_object_version_set,
2406 .do_data_get = osd_data_get,
2410 * dt_object_operations for interoperability mode
2411 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2413 static const struct dt_object_operations osd_obj_ea_ops = {
2414 .do_read_lock = osd_object_read_lock,
2415 .do_write_lock = osd_object_write_lock,
2416 .do_read_unlock = osd_object_read_unlock,
2417 .do_write_unlock = osd_object_write_unlock,
2418 .do_write_locked = osd_object_write_locked,
2419 .do_attr_get = osd_attr_get,
2420 .do_attr_set = osd_attr_set,
2421 .do_ah_init = osd_ah_init,
2422 .do_create = osd_object_ea_create,
2423 .do_index_try = osd_index_try,
2424 .do_ref_add = osd_object_ref_add,
2425 .do_ref_del = osd_object_ref_del,
2426 .do_xattr_get = osd_xattr_get,
2427 .do_xattr_set = osd_xattr_set,
2428 .do_xattr_del = osd_xattr_del,
2429 .do_xattr_list = osd_xattr_list,
2430 .do_capa_get = osd_capa_get,
2431 .do_object_sync = osd_object_sync,
2432 .do_version_get = osd_object_version_get,
2433 .do_version_set = osd_object_version_set,
2434 .do_data_get = osd_data_get,
2442 * XXX: Another layering violation for now.
2444 * We don't want to use ->f_op->read methods, because generic file write
2446 * - serializes on ->i_sem, and
2448 * - does a lot of extra work like balance_dirty_pages(),
2450 * which doesn't work for globally shared files like /last-received.
2452 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2454 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2456 memcpy(buffer, (char*)ei->i_data, buflen);
2461 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2464 struct buffer_head *bh;
2465 unsigned long block;
2472 /* prevent reading after eof */
2473 spin_lock(&inode->i_lock);
2474 if (i_size_read(inode) < *offs + size) {
2475 size = i_size_read(inode) - *offs;
2476 spin_unlock(&inode->i_lock);
2478 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2479 i_size_read(inode), *offs);
2481 } else if (size == 0) {
2485 spin_unlock(&inode->i_lock);
2488 blocksize = 1 << inode->i_blkbits;
2491 block = *offs >> inode->i_blkbits;
2492 boffs = *offs & (blocksize - 1);
2493 csize = min(blocksize - boffs, size);
2494 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2496 CERROR("can't read block: %d\n", err);
2500 memcpy(buf, bh->b_data + boffs, csize);
2510 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2511 struct lu_buf *buf, loff_t *pos,
2512 struct lustre_capa *capa)
2514 struct osd_object *obj = osd_dt_obj(dt);
2515 struct inode *inode = obj->oo_inode;
2518 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2521 /* Read small symlink from inode body as we need to maintain correct
2522 * on-disk symlinks for ldiskfs.
2524 if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2525 (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2526 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2528 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2533 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2536 memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2538 LDISKFS_I(inode)->i_disksize = buflen;
2539 i_size_write(inode, buflen);
2540 inode->i_sb->s_op->dirty_inode(inode);
2545 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2546 loff_t *offs, handle_t *handle)
2548 struct buffer_head *bh = NULL;
2549 loff_t offset = *offs;
2550 loff_t new_size = i_size_read(inode);
2551 unsigned long block;
2552 int blocksize = 1 << inode->i_blkbits;
2556 int dirty_inode = 0;
2558 while (bufsize > 0) {
2562 block = offset >> inode->i_blkbits;
2563 boffs = offset & (blocksize - 1);
2564 size = min(blocksize - boffs, bufsize);
2565 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2567 CERROR("can't read/create block: %d\n", err);
2571 err = ldiskfs_journal_get_write_access(handle, bh);
2573 CERROR("journal_get_write_access() returned error %d\n",
2577 LASSERTF(boffs + size <= bh->b_size,
2578 "boffs %d size %d bh->b_size %lu",
2579 boffs, size, (unsigned long)bh->b_size);
2580 memcpy(bh->b_data + boffs, buf, size);
2581 err = ldiskfs_journal_dirty_metadata(handle, bh);
2585 if (offset + size > new_size)
2586 new_size = offset + size;
2594 /* correct in-core and on-disk sizes */
2595 if (new_size > i_size_read(inode)) {
2596 spin_lock(&inode->i_lock);
2597 if (new_size > i_size_read(inode))
2598 i_size_write(inode, new_size);
2599 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
2600 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
2603 spin_unlock(&inode->i_lock);
2605 inode->i_sb->s_op->dirty_inode(inode);
2613 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2614 const struct lu_buf *buf, loff_t *pos,
2615 struct thandle *handle, struct lustre_capa *capa,
2618 struct osd_object *obj = osd_dt_obj(dt);
2619 struct inode *inode = obj->oo_inode;
2620 struct osd_thandle *oh;
2622 #ifdef HAVE_QUOTA_SUPPORT
2623 cfs_cap_t save = cfs_curproc_cap_pack();
2626 LASSERT(handle != NULL);
2628 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2631 oh = container_of(handle, struct osd_thandle, ot_super);
2632 LASSERT(oh->ot_handle->h_transaction != NULL);
2633 #ifdef HAVE_QUOTA_SUPPORT
2635 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2637 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2639 /* Write small symlink to inode body as we need to maintain correct
2640 * on-disk symlinks for ldiskfs.
2642 if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2643 (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
2644 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2646 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
2649 #ifdef HAVE_QUOTA_SUPPORT
2650 cfs_curproc_cap_unpack(save);
2653 result = buf->lb_len;
2657 static const struct dt_body_operations osd_body_ops = {
2658 .dbo_read = osd_read,
2659 .dbo_write = osd_write
2664 * delete a (key, value) pair from index \a dt specified by \a key
2666 * \param dt osd index object
2667 * \param key key for index
2668 * \param rec record reference
2669 * \param handle transaction handler
2672 * \retval -ve failure
2675 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2676 const struct dt_key *key, struct thandle *handle,
2677 struct lustre_capa *capa)
2679 struct osd_object *obj = osd_dt_obj(dt);
2680 struct osd_thandle *oh;
2681 struct iam_path_descr *ipd;
2682 struct iam_container *bag = &obj->oo_dir->od_container;
2687 LINVRNT(osd_invariant(obj));
2688 LASSERT(dt_object_exists(dt));
2689 LASSERT(bag->ic_object == obj->oo_inode);
2690 LASSERT(handle != NULL);
2692 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2695 ipd = osd_idx_ipd_get(env, bag);
2696 if (unlikely(ipd == NULL))
2699 oh = container_of0(handle, struct osd_thandle, ot_super);
2700 LASSERT(oh->ot_handle != NULL);
2701 LASSERT(oh->ot_handle->h_transaction != NULL);
2703 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2704 osd_ipd_put(env, bag, ipd);
2705 LINVRNT(osd_invariant(obj));
2709 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2712 struct osd_fid_pack *rec;
2715 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2716 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2717 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2723 * Index delete function for interoperability mode (b11826).
2724 * It will remove the directory entry added by osd_index_ea_insert().
2725 * This entry is needed to maintain name->fid mapping.
2727 * \param key, key i.e. file entry to be deleted
2729 * \retval 0, on success
2730 * \retval -ve, on error
2732 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2733 const struct dt_key *key, struct thandle *handle,
2734 struct lustre_capa *capa)
2736 struct osd_object *obj = osd_dt_obj(dt);
2737 struct inode *dir = obj->oo_inode;
2738 struct dentry *dentry;
2739 struct osd_thandle *oh;
2740 struct ldiskfs_dir_entry_2 *de;
2741 struct buffer_head *bh;
2747 LINVRNT(osd_invariant(obj));
2748 LASSERT(dt_object_exists(dt));
2749 LASSERT(handle != NULL);
2751 oh = container_of(handle, struct osd_thandle, ot_super);
2752 LASSERT(oh->ot_handle != NULL);
2753 LASSERT(oh->ot_handle->h_transaction != NULL);
2755 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2758 dentry = osd_child_dentry_get(env, obj,
2759 (char *)key, strlen((char *)key));
2761 cfs_down_write(&obj->oo_ext_idx_sem);
2762 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
2764 rc = ldiskfs_delete_entry(oh->ot_handle,
2770 cfs_up_write(&obj->oo_ext_idx_sem);
2771 LASSERT(osd_invariant(obj));
2776 * Lookup index for \a key and copy record to \a rec.
2778 * \param dt osd index object
2779 * \param key key for index
2780 * \param rec record reference
2782 * \retval +ve success : exact mach
2783 * \retval 0 return record with key not greater than \a key
2784 * \retval -ve failure
2786 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2787 struct dt_rec *rec, const struct dt_key *key,
2788 struct lustre_capa *capa)
2790 struct osd_object *obj = osd_dt_obj(dt);
2791 struct iam_path_descr *ipd;
2792 struct iam_container *bag = &obj->oo_dir->od_container;
2793 struct osd_thread_info *oti = osd_oti_get(env);
2794 struct iam_iterator *it = &oti->oti_idx_it;
2795 struct iam_rec *iam_rec;
2799 LASSERT(osd_invariant(obj));
2800 LASSERT(dt_object_exists(dt));
2801 LASSERT(bag->ic_object == obj->oo_inode);
2803 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2806 ipd = osd_idx_ipd_get(env, bag);
2810 /* got ipd now we can start iterator. */
2811 iam_it_init(it, bag, 0, ipd);
2813 rc = iam_it_get(it, (struct iam_key *)key);
2815 if (S_ISDIR(obj->oo_inode->i_mode))
2816 iam_rec = (struct iam_rec *)oti->oti_ldp;
2818 iam_rec = (struct iam_rec *) rec;
2820 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2821 if (S_ISDIR(obj->oo_inode->i_mode))
2822 osd_fid_unpack((struct lu_fid *) rec,
2823 (struct osd_fid_pack *)iam_rec);
2827 osd_ipd_put(env, bag, ipd);
2829 LINVRNT(osd_invariant(obj));
2835 * Inserts (key, value) pair in \a dt index object.
2837 * \param dt osd index object
2838 * \param key key for index
2839 * \param rec record reference
2840 * \param th transaction handler
2843 * \retval -ve failure
2845 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2846 const struct dt_rec *rec, const struct dt_key *key,
2847 struct thandle *th, struct lustre_capa *capa,
2850 struct osd_object *obj = osd_dt_obj(dt);
2851 struct iam_path_descr *ipd;
2852 struct osd_thandle *oh;
2853 struct iam_container *bag = &obj->oo_dir->od_container;
2854 #ifdef HAVE_QUOTA_SUPPORT
2855 cfs_cap_t save = cfs_curproc_cap_pack();
2857 struct osd_thread_info *oti = osd_oti_get(env);
2858 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2863 LINVRNT(osd_invariant(obj));
2864 LASSERT(dt_object_exists(dt));
2865 LASSERT(bag->ic_object == obj->oo_inode);
2866 LASSERT(th != NULL);
2868 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2871 ipd = osd_idx_ipd_get(env, bag);
2872 if (unlikely(ipd == NULL))
2875 oh = container_of0(th, struct osd_thandle, ot_super);
2876 LASSERT(oh->ot_handle != NULL);
2877 LASSERT(oh->ot_handle->h_transaction != NULL);
2878 #ifdef HAVE_QUOTA_SUPPORT
2880 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2882 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2884 if (S_ISDIR(obj->oo_inode->i_mode))
2885 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2887 iam_rec = (struct iam_rec *) rec;
2888 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2890 #ifdef HAVE_QUOTA_SUPPORT
2891 cfs_curproc_cap_unpack(save);
2893 osd_ipd_put(env, bag, ipd);
2894 LINVRNT(osd_invariant(obj));
2899 * Calls ldiskfs_add_entry() to add directory entry
2900 * into the directory. This is required for
2901 * interoperability mode (b11826)
2903 * \retval 0, on success
2904 * \retval -ve, on error
2906 static int __osd_ea_add_rec(struct osd_thread_info *info,
2907 struct osd_object *pobj,
2908 struct inode *cinode,
2910 const struct dt_rec *fid,
2913 struct ldiskfs_dentry_param *ldp;
2914 struct dentry *child;
2915 struct osd_thandle *oth;
2918 oth = container_of(th, struct osd_thandle, ot_super);
2919 LASSERT(oth->ot_handle != NULL);
2920 LASSERT(oth->ot_handle->h_transaction != NULL);
2922 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2924 if (fid_is_igif((struct lu_fid *)fid) ||
2925 fid_is_norm((struct lu_fid *)fid)) {
2926 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2927 osd_get_ldiskfs_dirent_param(ldp, fid);
2928 child->d_fsdata = (void*) ldp;
2930 child->d_fsdata = NULL;
2931 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2937 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2938 * into the directory.Also sets flags into osd object to
2939 * indicate dot and dotdot are created. This is required for
2940 * interoperability mode (b11826)
2942 * \param dir directory for dot and dotdot fixup.
2943 * \param obj child object for linking
2945 * \retval 0, on success
2946 * \retval -ve, on error
2948 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2949 struct osd_object *dir,
2950 struct inode *parent_dir, const char *name,
2951 const struct dt_rec *dot_fid,
2952 const struct dt_rec *dot_dot_fid,
2955 struct inode *inode = dir->oo_inode;
2956 struct ldiskfs_dentry_param *dot_ldp;
2957 struct ldiskfs_dentry_param *dot_dot_ldp;
2958 struct osd_thandle *oth;
2961 oth = container_of(th, struct osd_thandle, ot_super);
2962 LASSERT(oth->ot_handle->h_transaction != NULL);
2963 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2965 if (strcmp(name, dot) == 0) {
2966 if (dir->oo_compat_dot_created) {
2969 LASSERT(inode == parent_dir);
2970 dir->oo_compat_dot_created = 1;
2973 } else if(strcmp(name, dotdot) == 0) {
2974 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2975 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2977 if (!dir->oo_compat_dot_created)
2979 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
2980 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
2981 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2986 /* in case of rename, dotdot is already created */
2987 if (dir->oo_compat_dotdot_created) {
2988 return __osd_ea_add_rec(info, dir, parent_dir, name,
2992 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode,
2993 dot_ldp, dot_dot_ldp);
2995 dir->oo_compat_dotdot_created = 1;
3003 * It will call the appropriate osd_add* function and return the
3004 * value, return by respective functions.
3006 static int osd_ea_add_rec(const struct lu_env *env,
3007 struct osd_object *pobj,
3008 struct inode *cinode,
3010 const struct dt_rec *fid,
3013 struct osd_thread_info *info = osd_oti_get(env);
3016 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3018 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3019 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3022 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid, th);
3028 * Calls ->lookup() to find dentry. From dentry get inode and
3029 * read inode's ea to get fid. This is required for interoperability
3032 * \retval 0, on success
3033 * \retval -ve, on error
3035 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3036 struct dt_rec *rec, const struct dt_key *key)
3038 struct inode *dir = obj->oo_inode;
3039 struct dentry *dentry;
3040 struct ldiskfs_dir_entry_2 *de;
3041 struct buffer_head *bh;
3042 struct lu_fid *fid = (struct lu_fid *) rec;
3046 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3048 dentry = osd_child_dentry_get(env, obj,
3049 (char *)key, strlen((char *)key));
3051 cfs_down_read(&obj->oo_ext_idx_sem);
3052 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
3054 ino = le32_to_cpu(de->inode);
3055 rc = osd_get_fid_from_dentry(de, rec);
3057 /* done with de, release bh */
3060 rc = osd_ea_fid_get(env, obj, ino, fid);
3064 cfs_up_read(&obj->oo_ext_idx_sem);
3069 * Find the osd object for given fid.
3071 * \param fid need to find the osd object having this fid
3073 * \retval osd_object on success
3074 * \retval -ve on error
3076 struct osd_object *osd_object_find(const struct lu_env *env,
3077 struct dt_object *dt,
3078 const struct lu_fid *fid)
3080 struct lu_device *ludev = dt->do_lu.lo_dev;
3081 struct osd_object *child = NULL;
3082 struct lu_object *luch;
3083 struct lu_object *lo;
3085 luch = lu_object_find(env, ludev, fid, NULL);
3086 if (!IS_ERR(luch)) {
3087 if (lu_object_exists(luch)) {
3088 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3090 child = osd_obj(lo);
3092 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3093 "lu_object can't be located"
3094 ""DFID"\n", PFID(fid));
3096 if (child == NULL) {
3097 lu_object_put(env, luch);
3098 CERROR("Unable to get osd_object\n");
3099 child = ERR_PTR(-ENOENT);
3102 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3103 "lu_object does not exists "DFID"\n",
3105 child = ERR_PTR(-ENOENT);
3108 child = (void *)luch;
3114 * Put the osd object once done with it.
3116 * \param obj osd object that needs to be put
3118 static inline void osd_object_put(const struct lu_env *env,
3119 struct osd_object *obj)
3121 lu_object_put(env, &obj->oo_dt.do_lu);
3125 * Index add function for interoperability mode (b11826).
3126 * It will add the directory entry.This entry is needed to
3127 * maintain name->fid mapping.
3129 * \param key it is key i.e. file entry to be inserted
3130 * \param rec it is value of given key i.e. fid
3132 * \retval 0, on success
3133 * \retval -ve, on error
3135 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3136 const struct dt_rec *rec,
3137 const struct dt_key *key, struct thandle *th,
3138 struct lustre_capa *capa, int ignore_quota)
3140 struct osd_object *obj = osd_dt_obj(dt);
3141 struct lu_fid *fid = (struct lu_fid *) rec;
3142 const char *name = (const char *)key;
3143 struct osd_object *child;
3144 #ifdef HAVE_QUOTA_SUPPORT
3145 cfs_cap_t save = cfs_curproc_cap_pack();
3151 LASSERT(osd_invariant(obj));
3152 LASSERT(dt_object_exists(dt));
3153 LASSERT(th != NULL);
3155 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3158 child = osd_object_find(env, dt, fid);
3159 if (!IS_ERR(child)) {
3160 #ifdef HAVE_QUOTA_SUPPORT
3162 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3164 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3166 cfs_down_write(&obj->oo_ext_idx_sem);
3167 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3168 cfs_up_write(&obj->oo_ext_idx_sem);
3169 #ifdef HAVE_QUOTA_SUPPORT
3170 cfs_curproc_cap_unpack(save);
3172 osd_object_put(env, child);
3174 rc = PTR_ERR(child);
3177 LASSERT(osd_invariant(obj));
3182 * Initialize osd Iterator for given osd index object.
3184 * \param dt osd index object
3187 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3188 struct dt_object *dt,
3190 struct lustre_capa *capa)
3192 struct osd_it_iam *it;
3193 struct osd_thread_info *oti = osd_oti_get(env);
3194 struct osd_object *obj = osd_dt_obj(dt);
3195 struct lu_object *lo = &dt->do_lu;
3196 struct iam_path_descr *ipd;
3197 struct iam_container *bag = &obj->oo_dir->od_container;
3199 LASSERT(lu_object_exists(lo));
3201 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3202 return ERR_PTR(-EACCES);
3205 ipd = osd_it_ipd_get(env, bag);
3206 if (likely(ipd != NULL)) {
3210 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3211 return (struct dt_it *)it;
3213 return ERR_PTR(-ENOMEM);
3217 * free given Iterator.
3220 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3222 struct osd_it_iam *it = (struct osd_it_iam *)di;
3223 struct osd_object *obj = it->oi_obj;
3225 iam_it_fini(&it->oi_it);
3226 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3227 lu_object_put(env, &obj->oo_dt.do_lu);
3231 * Move Iterator to record specified by \a key
3233 * \param di osd iterator
3234 * \param key key for index
3236 * \retval +ve di points to record with least key not larger than key
3237 * \retval 0 di points to exact matched key
3238 * \retval -ve failure
3241 static int osd_it_iam_get(const struct lu_env *env,
3242 struct dt_it *di, const struct dt_key *key)
3244 struct osd_it_iam *it = (struct osd_it_iam *)di;
3246 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3252 * \param di osd iterator
3255 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3257 struct osd_it_iam *it = (struct osd_it_iam *)di;
3259 iam_it_put(&it->oi_it);
3263 * Move iterator by one record
3265 * \param di osd iterator
3267 * \retval +1 end of container reached
3269 * \retval -ve failure
3272 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3274 struct osd_it_iam *it = (struct osd_it_iam *)di;
3276 return iam_it_next(&it->oi_it);
3280 * Return pointer to the key under iterator.
3283 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3284 const struct dt_it *di)
3286 struct osd_it_iam *it = (struct osd_it_iam *)di;
3288 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3292 * Return size of key under iterator (in bytes)
3295 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3297 struct osd_it_iam *it = (struct osd_it_iam *)di;
3299 return iam_it_key_size(&it->oi_it);
3302 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3307 struct luda_type *lt;
3308 const unsigned align = sizeof(struct luda_type) - 1;
3310 /* check if file type is required */
3311 if (attr & LUDA_TYPE) {
3312 len = (len + align) & ~align;
3314 lt = (void *) ent->lde_name + len;
3315 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3316 ent->lde_attrs |= LUDA_TYPE;
3319 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3323 * build lu direct from backend fs dirent.
3326 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3334 fid_cpu_to_le(&ent->lde_fid, fid);
3335 ent->lde_attrs = LUDA_FID;
3337 ent->lde_hash = cpu_to_le64(offset);
3338 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3340 strncpy(ent->lde_name, name, namelen);
3341 ent->lde_namelen = cpu_to_le16(namelen);
3343 /* append lustre attributes */
3344 osd_it_append_attrs(ent, attr, namelen, type);
3348 * Return pointer to the record under iterator.
3350 static int osd_it_iam_rec(const struct lu_env *env,
3351 const struct dt_it *di,
3352 struct lu_dirent *lde,
3355 struct osd_it_iam *it = (struct osd_it_iam *)di;
3356 struct osd_thread_info *info = osd_oti_get(env);
3357 struct lu_fid *fid = &info->oti_fid;
3358 const struct osd_fid_pack *rec;
3364 name = (char *)iam_it_key_get(&it->oi_it);
3366 RETURN(PTR_ERR(name));
3368 namelen = iam_it_key_size(&it->oi_it);
3370 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3372 RETURN(PTR_ERR(rec));
3374 rc = osd_fid_unpack(fid, rec);
3378 hash = iam_it_store(&it->oi_it);
3380 /* IAM does not store object type in IAM index (dir) */
3381 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3388 * Returns cookie for current Iterator position.
3390 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3392 struct osd_it_iam *it = (struct osd_it_iam *)di;
3394 return iam_it_store(&it->oi_it);
3398 * Restore iterator from cookie.
3400 * \param di osd iterator
3401 * \param hash Iterator location cookie
3403 * \retval +ve di points to record with least key not larger than key.
3404 * \retval 0 di points to exact matched key
3405 * \retval -ve failure
3408 static int osd_it_iam_load(const struct lu_env *env,
3409 const struct dt_it *di, __u64 hash)
3411 struct osd_it_iam *it = (struct osd_it_iam *)di;
3413 return iam_it_load(&it->oi_it, hash);
3416 static const struct dt_index_operations osd_index_iam_ops = {
3417 .dio_lookup = osd_index_iam_lookup,
3418 .dio_insert = osd_index_iam_insert,
3419 .dio_delete = osd_index_iam_delete,
3421 .init = osd_it_iam_init,
3422 .fini = osd_it_iam_fini,
3423 .get = osd_it_iam_get,
3424 .put = osd_it_iam_put,
3425 .next = osd_it_iam_next,
3426 .key = osd_it_iam_key,
3427 .key_size = osd_it_iam_key_size,
3428 .rec = osd_it_iam_rec,
3429 .store = osd_it_iam_store,
3430 .load = osd_it_iam_load
3435 * Creates or initializes iterator context.
3437 * \retval struct osd_it_ea, iterator structure on success
3440 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3441 struct dt_object *dt,
3443 struct lustre_capa *capa)
3445 struct osd_object *obj = osd_dt_obj(dt);
3446 struct osd_thread_info *info = osd_oti_get(env);
3447 struct osd_it_ea *it = &info->oti_it_ea;
3448 struct lu_object *lo = &dt->do_lu;
3449 struct dentry *obj_dentry = &info->oti_it_dentry;
3451 LASSERT(lu_object_exists(lo));
3453 obj_dentry->d_inode = obj->oo_inode;
3454 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3455 obj_dentry->d_name.hash = 0;
3457 it->oie_rd_dirent = 0;
3458 it->oie_it_dirent = 0;
3459 it->oie_dirent = NULL;
3460 it->oie_buf = info->oti_it_ea_buf;
3462 it->oie_file.f_pos = 0;
3463 it->oie_file.f_dentry = obj_dentry;
3464 if (attr & LUDA_64BITHASH)
3465 it->oie_file.f_flags = O_64BITHASH;
3467 it->oie_file.f_flags = O_32BITHASH;
3468 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3469 it->oie_file.f_op = obj->oo_inode->i_fop;
3470 it->oie_file.private_data = NULL;
3472 RETURN((struct dt_it *) it);
3476 * Destroy or finishes iterator context.
3478 * \param di iterator structure to be destroyed
3480 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3482 struct osd_it_ea *it = (struct osd_it_ea *)di;
3483 struct osd_object *obj = it->oie_obj;
3484 struct inode *inode = obj->oo_inode;
3487 it->oie_file.f_op->release(inode, &it->oie_file);
3488 lu_object_put(env, &obj->oo_dt.do_lu);
3493 * It position the iterator at given key, so that next lookup continues from
3494 * that key Or it is similar to dio_it->load() but based on a key,
3495 * rather than file position.
3497 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3500 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3502 static int osd_it_ea_get(const struct lu_env *env,
3503 struct dt_it *di, const struct dt_key *key)
3505 struct osd_it_ea *it = (struct osd_it_ea *)di;
3508 LASSERT(((const char *)key)[0] == '\0');
3509 it->oie_file.f_pos = 0;
3510 it->oie_rd_dirent = 0;
3511 it->oie_it_dirent = 0;
3512 it->oie_dirent = NULL;
3520 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3525 * It is called internally by ->readdir(). It fills the
3526 * iterator's in-memory data structure with required
3527 * information i.e. name, namelen, rec_size etc.
3529 * \param buf in which information to be filled in.
3530 * \param name name of the file in given dir
3532 * \retval 0 on success
3533 * \retval 1 on buffer full
3535 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3536 loff_t offset, __u64 ino,
3539 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3540 struct osd_it_ea_dirent *ent = it->oie_dirent;
3541 struct lu_fid *fid = &ent->oied_fid;
3542 struct osd_fid_pack *rec;
3545 /* this should never happen */
3546 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3547 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3551 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3555 if (d_type & LDISKFS_DIRENT_LUFID) {
3556 rec = (struct osd_fid_pack*) (name + namelen + 1);
3558 if (osd_fid_unpack(fid, rec) != 0)
3561 d_type &= ~LDISKFS_DIRENT_LUFID;
3566 ent->oied_ino = ino;
3567 ent->oied_off = offset;
3568 ent->oied_namelen = namelen;
3569 ent->oied_type = d_type;
3571 memcpy(ent->oied_name, name, namelen);
3573 it->oie_rd_dirent++;
3574 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3579 * Calls ->readdir() to load a directory entry at a time
3580 * and stored it in iterator's in-memory data structure.
3582 * \param di iterator's in memory structure
3584 * \retval 0 on success
3585 * \retval -ve on error
3587 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3589 struct osd_it_ea *it = (struct osd_it_ea *)di;
3590 struct osd_object *obj = it->oie_obj;
3591 struct inode *inode = obj->oo_inode;
3595 it->oie_dirent = it->oie_buf;
3596 it->oie_rd_dirent = 0;
3598 cfs_down_read(&obj->oo_ext_idx_sem);
3599 result = inode->i_fop->readdir(&it->oie_file, it,
3600 (filldir_t) osd_ldiskfs_filldir);
3602 cfs_up_read(&obj->oo_ext_idx_sem);
3604 if (it->oie_rd_dirent == 0) {
3607 it->oie_dirent = it->oie_buf;
3608 it->oie_it_dirent = 1;
3615 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3616 * to load a directory entry at a time and stored it in
3617 * iterator's in-memory data structure.
3619 * \param di iterator's in memory structure
3621 * \retval +ve iterator reached to end
3622 * \retval 0 iterator not reached to end
3623 * \retval -ve on error
3625 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3627 struct osd_it_ea *it = (struct osd_it_ea *)di;
3632 if (it->oie_it_dirent < it->oie_rd_dirent) {
3634 (void *) it->oie_dirent +
3635 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3636 it->oie_dirent->oied_namelen);
3637 it->oie_it_dirent++;
3640 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3643 rc = osd_ldiskfs_it_fill(di);
3650 * Returns the key at current position from iterator's in memory structure.
3652 * \param di iterator's in memory structure
3654 * \retval key i.e. struct dt_key on success
3656 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3657 const struct dt_it *di)
3659 struct osd_it_ea *it = (struct osd_it_ea *)di;
3661 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3665 * Returns the key's size at current position from iterator's in memory structure.
3667 * \param di iterator's in memory structure
3669 * \retval key_size i.e. struct dt_key on success
3671 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3673 struct osd_it_ea *it = (struct osd_it_ea *)di;
3675 RETURN(it->oie_dirent->oied_namelen);
3680 * Returns the value (i.e. fid/igif) at current position from iterator's
3681 * in memory structure.
3683 * \param di struct osd_it_ea, iterator's in memory structure
3684 * \param attr attr requested for dirent.
3685 * \param lde lustre dirent
3687 * \retval 0 no error and \param lde has correct lustre dirent.
3688 * \retval -ve on error
3690 static inline int osd_it_ea_rec(const struct lu_env *env,
3691 const struct dt_it *di,
3692 struct lu_dirent *lde,
3695 struct osd_it_ea *it = (struct osd_it_ea *)di;
3696 struct osd_object *obj = it->oie_obj;
3697 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3702 if (!fid_is_sane(fid))
3703 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3706 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3707 it->oie_dirent->oied_name,
3708 it->oie_dirent->oied_namelen,
3709 it->oie_dirent->oied_type,
3715 * Returns a cookie for current position of the iterator head, so that
3716 * user can use this cookie to load/start the iterator next time.
3718 * \param di iterator's in memory structure
3720 * \retval cookie for current position, on success
3722 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3724 struct osd_it_ea *it = (struct osd_it_ea *)di;
3726 RETURN(it->oie_dirent->oied_off);
3730 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3731 * to load a directory entry at a time and stored it i inn,
3732 * in iterator's in-memory data structure.
3734 * \param di struct osd_it_ea, iterator's in memory structure
3736 * \retval +ve on success
3737 * \retval -ve on error
3739 static int osd_it_ea_load(const struct lu_env *env,
3740 const struct dt_it *di, __u64 hash)
3742 struct osd_it_ea *it = (struct osd_it_ea *)di;
3746 it->oie_file.f_pos = hash;
3748 rc = osd_ldiskfs_it_fill(di);
3756 * Index lookup function for interoperability mode (b11826).
3758 * \param key, key i.e. file name to be searched
3760 * \retval +ve, on success
3761 * \retval -ve, on error
3763 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3764 struct dt_rec *rec, const struct dt_key *key,
3765 struct lustre_capa *capa)
3767 struct osd_object *obj = osd_dt_obj(dt);
3772 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3773 LINVRNT(osd_invariant(obj));
3775 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3778 rc = osd_ea_lookup_rec(env, obj, rec, key);
3786 * Index and Iterator operations for interoperability
3787 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3789 static const struct dt_index_operations osd_index_ea_ops = {
3790 .dio_lookup = osd_index_ea_lookup,
3791 .dio_insert = osd_index_ea_insert,
3792 .dio_delete = osd_index_ea_delete,
3794 .init = osd_it_ea_init,
3795 .fini = osd_it_ea_fini,
3796 .get = osd_it_ea_get,
3797 .put = osd_it_ea_put,
3798 .next = osd_it_ea_next,
3799 .key = osd_it_ea_key,
3800 .key_size = osd_it_ea_key_size,
3801 .rec = osd_it_ea_rec,
3802 .store = osd_it_ea_store,
3803 .load = osd_it_ea_load
3807 static void *osd_key_init(const struct lu_context *ctx,
3808 struct lu_context_key *key)
3810 struct osd_thread_info *info;
3812 OBD_ALLOC_PTR(info);
3814 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3815 if (info->oti_it_ea_buf != NULL) {
3816 info->oti_env = container_of(ctx, struct lu_env,
3820 info = ERR_PTR(-ENOMEM);
3823 info = ERR_PTR(-ENOMEM);
3828 static void osd_key_fini(const struct lu_context *ctx,
3829 struct lu_context_key *key, void* data)
3831 struct osd_thread_info *info = data;
3833 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3837 static void osd_key_exit(const struct lu_context *ctx,
3838 struct lu_context_key *key, void *data)
3840 struct osd_thread_info *info = data;
3842 LASSERT(info->oti_r_locks == 0);
3843 LASSERT(info->oti_w_locks == 0);
3844 LASSERT(info->oti_txns == 0);
3847 /* type constructor/destructor: osd_type_init, osd_type_fini */
3848 LU_TYPE_INIT_FINI(osd, &osd_key);
3850 static struct lu_context_key osd_key = {
3851 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3852 .lct_init = osd_key_init,
3853 .lct_fini = osd_key_fini,
3854 .lct_exit = osd_key_exit
3858 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3859 const char *name, struct lu_device *next)
3862 struct lu_context *ctx;
3864 /* context for commit hooks */
3865 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3866 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3868 rc = osd_procfs_init(osd_dev(d), name);
3869 ctx->lc_cookie = 0x3;
3874 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3876 struct osd_thread_info *info = osd_oti_get(env);
3878 if (o->od_obj_area != NULL) {
3879 lu_object_put(env, &o->od_obj_area->do_lu);
3880 o->od_obj_area = NULL;
3882 osd_oi_fini(info, &o->od_oi);
3887 static int osd_mount(const struct lu_env *env,
3888 struct osd_device *o, struct lustre_cfg *cfg)
3890 struct lustre_mount_info *lmi;
3891 const char *dev = lustre_cfg_string(cfg, 0);
3892 struct lustre_disk_data *ldd;
3893 struct lustre_sb_info *lsi;
3896 if (o->od_mount != NULL) {
3897 CERROR("Already mounted (%s)\n", dev);
3902 lmi = server_get_mount(dev);
3904 CERROR("Cannot get mount info for %s!\n", dev);
3908 LASSERT(lmi != NULL);
3909 /* save lustre_mount_info in dt_device */
3912 lsi = s2lsi(lmi->lmi_sb);
3915 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3917 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3921 o->od_obj_area = NULL;
3925 static struct lu_device *osd_device_fini(const struct lu_env *env,
3926 struct lu_device *d)
3931 shrink_dcache_sb(osd_sb(osd_dev(d)));
3932 osd_sync(env, lu2dt_dev(d));
3934 rc = osd_procfs_fini(osd_dev(d));
3936 CERROR("proc fini error %d \n", rc);
3937 RETURN (ERR_PTR(rc));
3940 if (osd_dev(d)->od_mount)
3941 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3942 osd_dev(d)->od_mount->lmi_mnt);
3943 osd_dev(d)->od_mount = NULL;
3945 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3949 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3950 struct lu_device_type *t,
3951 struct lustre_cfg *cfg)
3953 struct lu_device *l;
3954 struct osd_device *o;
3960 result = dt_device_init(&o->od_dt_dev, t);
3963 l->ld_ops = &osd_lu_ops;
3964 o->od_dt_dev.dd_ops = &osd_dt_ops;
3965 cfs_spin_lock_init(&o->od_osfs_lock);
3966 o->od_osfs_age = cfs_time_shift_64(-1000);
3967 o->od_capa_hash = init_capa_hash();
3968 if (o->od_capa_hash == NULL) {
3969 dt_device_fini(&o->od_dt_dev);
3970 l = ERR_PTR(-ENOMEM);
3973 l = ERR_PTR(result);
3978 l = ERR_PTR(-ENOMEM);
3982 static struct lu_device *osd_device_free(const struct lu_env *env,
3983 struct lu_device *d)
3985 struct osd_device *o = osd_dev(d);
3988 cleanup_capa_hash(o->od_capa_hash);
3989 dt_device_fini(&o->od_dt_dev);
3994 static int osd_process_config(const struct lu_env *env,
3995 struct lu_device *d, struct lustre_cfg *cfg)
3997 struct osd_device *o = osd_dev(d);
4001 switch(cfg->lcfg_command) {
4003 err = osd_mount(env, o, cfg);
4006 err = osd_shutdown(env, o);
4015 static int osd_recovery_complete(const struct lu_env *env,
4016 struct lu_device *d)
4021 static int osd_prepare(const struct lu_env *env,
4022 struct lu_device *pdev,
4023 struct lu_device *dev)
4025 struct osd_device *osd = osd_dev(dev);
4026 struct lustre_sb_info *lsi;
4027 struct lustre_disk_data *ldd;
4028 struct lustre_mount_info *lmi;
4029 struct osd_thread_info *oti = osd_oti_get(env);
4030 struct dt_object *d;
4034 /* 1. initialize oi before any file create or file open */
4035 result = osd_oi_init(oti, &osd->od_oi,
4036 &osd->od_dt_dev, lu2md_dev(pdev));
4040 lmi = osd->od_mount;
4041 lsi = s2lsi(lmi->lmi_sb);
4044 /* 2. setup local objects */
4045 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4049 /* 3. open remote object dir */
4050 d = dt_store_open(env, lu2dt_dev(dev), "",
4051 remote_obj_dir, &oti->oti_fid);
4053 osd->od_obj_area = d;
4056 result = PTR_ERR(d);
4057 osd->od_obj_area = NULL;
4064 static const struct lu_object_operations osd_lu_obj_ops = {
4065 .loo_object_init = osd_object_init,
4066 .loo_object_delete = osd_object_delete,
4067 .loo_object_release = osd_object_release,
4068 .loo_object_free = osd_object_free,
4069 .loo_object_print = osd_object_print,
4070 .loo_object_invariant = osd_object_invariant
4073 static const struct lu_device_operations osd_lu_ops = {
4074 .ldo_object_alloc = osd_object_alloc,
4075 .ldo_process_config = osd_process_config,
4076 .ldo_recovery_complete = osd_recovery_complete,
4077 .ldo_prepare = osd_prepare,
4080 static const struct lu_device_type_operations osd_device_type_ops = {
4081 .ldto_init = osd_type_init,
4082 .ldto_fini = osd_type_fini,
4084 .ldto_start = osd_type_start,
4085 .ldto_stop = osd_type_stop,
4087 .ldto_device_alloc = osd_device_alloc,
4088 .ldto_device_free = osd_device_free,
4090 .ldto_device_init = osd_device_init,
4091 .ldto_device_fini = osd_device_fini
4094 static struct lu_device_type osd_device_type = {
4095 .ldt_tags = LU_DEVICE_DT,
4096 .ldt_name = LUSTRE_OSD_NAME,
4097 .ldt_ops = &osd_device_type_ops,
4098 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4102 * lprocfs legacy support.
4104 static struct obd_ops osd_obd_device_ops = {
4105 .o_owner = THIS_MODULE
4108 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4109 .llod_name = remote_obj_dir,
4110 .llod_oid = OSD_REM_OBJ_DIR_OID,
4112 .llod_feat = &dt_directory_features,
4115 static int __init osd_mod_init(void)
4117 struct lprocfs_static_vars lvars;
4120 llo_local_obj_register(&llod_osd_rem_obj_dir);
4121 lprocfs_osd_init_vars(&lvars);
4122 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4123 LUSTRE_OSD_NAME, &osd_device_type);
4126 static void __exit osd_mod_exit(void)
4128 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4129 class_unregister_type(LUSTRE_OSD_NAME);
4132 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4133 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4134 MODULE_LICENSE("GPL");
4136 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);