1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include <linux/module.h>
51 /* LUSTRE_VERSION_CODE */
52 #include <lustre_ver.h>
53 /* prerequisite for linux/xattr.h */
54 #include <linux/types.h>
55 /* prerequisite for linux/xattr.h */
57 /* XATTR_{REPLACE,CREATE} */
58 #include <linux/xattr.h>
63 * struct OBD_{ALLOC,FREE}*()
66 #include <obd_support.h>
67 /* struct ptlrpc_thread */
68 #include <lustre_net.h>
71 #include <lustre_fid.h>
73 #include "osd_internal.h"
76 /* llo_* api support */
77 #include <md_object.h>
79 static const char dot[] = ".";
80 static const char dotdot[] = "..";
81 static const char remote_obj_dir[] = "REM_OBJ_DIR";
83 struct osd_directory {
84 struct iam_container od_container;
85 struct iam_descr od_descr;
89 struct dt_object oo_dt;
91 * Inode for file system object represented by this osd_object. This
92 * inode is pinned for the whole duration of lu_object life.
94 * Not modified concurrently (either setup early during object
95 * creation, or assigned by osd_object_create() under write lock).
97 struct inode *oo_inode;
99 * to protect index ops.
101 cfs_rw_semaphore_t oo_ext_idx_sem;
102 cfs_rw_semaphore_t oo_sem;
103 struct osd_directory *oo_dir;
104 /** protects inode attributes. */
105 cfs_spinlock_t oo_guard;
107 * Following two members are used to indicate the presence of dot and
108 * dotdot in the given directory. This is required for interop mode
111 int oo_compat_dot_created;
112 int oo_compat_dotdot_created;
114 const struct lu_env *oo_owner;
115 #ifdef CONFIG_LOCKDEP
116 struct lockdep_map oo_dep_map;
120 static const struct lu_object_operations osd_lu_obj_ops;
121 static const struct lu_device_operations osd_lu_ops;
122 static struct lu_context_key osd_key;
123 static const struct dt_object_operations osd_obj_ops;
124 static const struct dt_object_operations osd_obj_ea_ops;
125 static const struct dt_body_operations osd_body_ops;
126 static const struct dt_index_operations osd_index_iam_ops;
127 static const struct dt_index_operations osd_index_ea_ops;
130 struct thandle ot_super;
132 struct journal_callback ot_jcb;
133 /* Link to the device, for debugging. */
134 struct lu_ref_link *ot_dev_link;
136 #if OSD_THANDLE_STATS
137 /** time when this handle was allocated */
138 cfs_time_t oth_alloced;
140 /** time when this thanle was started */
141 cfs_time_t oth_started;
148 static int lu_device_is_osd(const struct lu_device *d)
150 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
153 static struct osd_device *osd_dt_dev(const struct dt_device *d)
155 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
156 return container_of0(d, struct osd_device, od_dt_dev);
159 static struct osd_device *osd_dev(const struct lu_device *d)
161 LASSERT(lu_device_is_osd(d));
162 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
165 static struct osd_device *osd_obj2dev(const struct osd_object *o)
167 return osd_dev(o->oo_dt.do_lu.lo_dev);
170 static struct super_block *osd_sb(const struct osd_device *dev)
172 return dev->od_mount->lmi_mnt->mnt_sb;
175 static int osd_object_is_root(const struct osd_object *obj)
177 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
180 static struct osd_object *osd_obj(const struct lu_object *o)
182 LASSERT(lu_device_is_osd(o->lo_dev));
183 return container_of0(o, struct osd_object, oo_dt.do_lu);
186 static struct osd_object *osd_dt_obj(const struct dt_object *d)
188 return osd_obj(&d->do_lu);
191 static struct lu_device *osd2lu_dev(struct osd_device *osd)
193 return &osd->od_dt_dev.dd_lu_dev;
196 static journal_t *osd_journal(const struct osd_device *dev)
198 return LDISKFS_SB(osd_sb(dev))->s_journal;
201 static int osd_has_index(const struct osd_object *obj)
203 return obj->oo_dt.do_index_ops != NULL;
206 static int osd_object_invariant(const struct lu_object *l)
208 return osd_invariant(osd_obj(l));
211 #ifdef HAVE_QUOTA_SUPPORT
213 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
215 struct md_ucred *uc = md_ucred(env);
219 save->oc_uid = current->fsuid;
220 save->oc_gid = current->fsgid;
221 save->oc_cap = current->cap_effective;
222 current->fsuid = uc->mu_fsuid;
223 current->fsgid = uc->mu_fsgid;
224 current->cap_effective = uc->mu_cap;
228 osd_pop_ctxt(struct osd_ctxt *save)
230 current->fsuid = save->oc_uid;
231 current->fsgid = save->oc_gid;
232 current->cap_effective = save->oc_cap;
236 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
238 return lu_context_key_get(&env->le_ctx, &osd_key);
242 * Concurrency: doesn't matter
244 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
246 return osd_oti_get(env)->oti_r_locks > 0;
250 * Concurrency: doesn't matter
252 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
254 struct osd_thread_info *oti = osd_oti_get(env);
255 return oti->oti_w_locks > 0 && o->oo_owner == env;
259 * Concurrency: doesn't access mutable data
261 static int osd_root_get(const struct lu_env *env,
262 struct dt_device *dev, struct lu_fid *f)
266 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
267 LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
272 * OSD object methods.
276 * Concurrency: no concurrent access is possible that early in object
279 static struct lu_object *osd_object_alloc(const struct lu_env *env,
280 const struct lu_object_header *hdr,
283 struct osd_object *mo;
289 l = &mo->oo_dt.do_lu;
290 dt_object_init(&mo->oo_dt, NULL, d);
291 if (osd_dev(d)->od_iop_mode)
292 mo->oo_dt.do_ops = &osd_obj_ea_ops;
294 mo->oo_dt.do_ops = &osd_obj_ops;
296 l->lo_ops = &osd_lu_obj_ops;
297 cfs_init_rwsem(&mo->oo_sem);
298 cfs_init_rwsem(&mo->oo_ext_idx_sem);
299 cfs_spin_lock_init(&mo->oo_guard);
306 * retrieve object from backend ext fs.
308 static struct inode *osd_iget(struct osd_thread_info *info,
309 struct osd_device *dev,
310 const struct osd_inode_id *id)
312 struct inode *inode = NULL;
314 #ifdef HAVE_EXT4_LDISKFS
315 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
317 /* Newer kernels return an error instead of a NULL pointer */
320 inode = iget(osd_sb(dev), id->oii_ino);
323 CERROR("no inode\n");
324 inode = ERR_PTR(-EACCES);
325 } else if (id->oii_gen != OSD_OII_NOGEN &&
326 inode->i_generation != id->oii_gen) {
328 inode = ERR_PTR(-ESTALE);
329 } else if (inode->i_nlink == 0) {
330 /* due to parallel readdir and unlink,
331 * we can have dead inode here. */
332 CWARN("stale inode\n");
333 make_bad_inode(inode);
335 inode = ERR_PTR(-ESTALE);
336 } else if (is_bad_inode(inode)) {
337 CERROR("bad inode %lx\n",inode->i_ino);
339 inode = ERR_PTR(-ENOENT);
344 static int osd_fid_lookup(const struct lu_env *env,
345 struct osd_object *obj, const struct lu_fid *fid)
347 struct osd_thread_info *info;
348 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
349 struct osd_device *dev;
350 struct osd_inode_id *id;
355 LINVRNT(osd_invariant(obj));
356 LASSERT(obj->oo_inode == NULL);
357 LASSERT(fid_is_sane(fid) || osd_fid_is_root(fid));
359 * This assertion checks that osd layer sees only local
360 * fids. Unfortunately it is somewhat expensive (does a
361 * cache-lookup). Disabling it for production/acceptance-testing.
363 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
367 info = osd_oti_get(env);
372 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
375 result = osd_oi_lookup(info, oi, fid, id);
377 inode = osd_iget(info, dev, id);
378 if (!IS_ERR(inode)) {
379 obj->oo_inode = inode;
380 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
381 if (dev->od_iop_mode) {
382 obj->oo_compat_dot_created = 1;
383 obj->oo_compat_dotdot_created = 1;
388 * If fid wasn't found in oi, inode-less object is
389 * created, for which lu_object_exists() returns
390 * false. This is used in a (frequent) case when
391 * objects are created as locking anchors or
392 * place holders for objects yet to be created.
394 result = PTR_ERR(inode);
395 } else if (result == -ENOENT)
397 LINVRNT(osd_invariant(obj));
403 * Concurrency: shouldn't matter.
405 static void osd_object_init0(struct osd_object *obj)
407 LASSERT(obj->oo_inode != NULL);
408 obj->oo_dt.do_body_ops = &osd_body_ops;
409 obj->oo_dt.do_lu.lo_header->loh_attr |=
410 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
414 * Concurrency: no concurrent access is possible that early in object
417 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
418 const struct lu_object_conf *unused)
420 struct osd_object *obj = osd_obj(l);
423 LINVRNT(osd_invariant(obj));
425 result = osd_fid_lookup(env, obj, lu_object_fid(l));
427 if (obj->oo_inode != NULL)
428 osd_object_init0(obj);
430 LINVRNT(osd_invariant(obj));
435 * Concurrency: no concurrent access is possible that late in object
438 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
440 struct osd_object *obj = osd_obj(l);
442 LINVRNT(osd_invariant(obj));
444 dt_object_fini(&obj->oo_dt);
451 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
452 const struct iam_container *bag)
454 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
455 osd_oti_get(env)->oti_it_ipd);
458 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
459 const struct iam_container *bag)
461 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
462 osd_oti_get(env)->oti_idx_ipd);
465 static void osd_ipd_put(const struct lu_env *env,
466 const struct iam_container *bag,
467 struct iam_path_descr *ipd)
469 bag->ic_descr->id_ops->id_ipd_free(ipd);
473 * Concurrency: no concurrent access is possible that late in object
476 static void osd_index_fini(struct osd_object *o)
478 struct iam_container *bag;
480 if (o->oo_dir != NULL) {
481 bag = &o->oo_dir->od_container;
482 if (o->oo_inode != NULL) {
483 if (bag->ic_object == o->oo_inode)
484 iam_container_fini(bag);
486 OBD_FREE_PTR(o->oo_dir);
492 * Concurrency: no concurrent access is possible that late in object
493 * life-cycle (for all existing callers, that is. New callers have to provide
494 * their own locking.)
496 static int osd_inode_unlinked(const struct inode *inode)
498 return inode->i_nlink == 0;
502 OSD_TXN_OI_DELETE_CREDITS = 20,
503 OSD_TXN_INODE_DELETE_CREDITS = 20
510 #if OSD_THANDLE_STATS
512 * Set time when the handle is allocated
514 static void osd_th_alloced(struct osd_thandle *oth)
516 oth->oth_alloced = cfs_time_current();
520 * Set time when the handle started
522 static void osd_th_started(struct osd_thandle *oth)
524 oth->oth_started = cfs_time_current();
528 * Helper function to convert time interval to microseconds packed in
529 * long int (default time units for the counter in "stats" initialized
530 * by lu_time_init() )
532 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
536 cfs_duration_usec(cfs_time_sub(end, start), &val);
537 return val.tv_sec * 1000000 + val.tv_usec;
541 * Check whether the we deal with this handle for too long.
543 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
544 cfs_time_t alloced, cfs_time_t started,
547 cfs_time_t now = cfs_time_current();
549 LASSERT(dev != NULL);
551 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
552 interval_to_usec(alloced, started));
553 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
554 interval_to_usec(started, closed));
555 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
556 interval_to_usec(closed, now));
558 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
559 CWARN("transaction handle %p was open for too long: "
561 "alloced "CFS_TIME_T" ,"
562 "started "CFS_TIME_T" ,"
563 "closed "CFS_TIME_T"\n",
564 oth, now, alloced, started, closed);
565 libcfs_debug_dumpstack(NULL);
569 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
571 cfs_time_t __closed = cfs_time_current(); \
572 cfs_time_t __alloced = oth->oth_alloced; \
573 cfs_time_t __started = oth->oth_started; \
576 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
579 #else /* OSD_THANDLE_STATS */
581 #define osd_th_alloced(h) do {} while(0)
582 #define osd_th_started(h) do {} while(0)
583 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
585 #endif /* OSD_THANDLE_STATS */
588 * Concurrency: doesn't access mutable data.
590 static int osd_param_is_sane(const struct osd_device *dev,
591 const struct txn_param *param)
593 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
597 * Concurrency: shouldn't matter.
599 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
601 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
602 struct thandle *th = &oh->ot_super;
603 struct dt_device *dev = th->th_dev;
604 struct lu_device *lud = &dev->dd_lu_dev;
606 LASSERT(dev != NULL);
607 LASSERT(oh->ot_handle == NULL);
610 CERROR("transaction @0x%p commit error: %d\n", th, error);
612 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
614 * This od_env_for_commit is only for commit usage. see
617 lu_context_enter(&env->le_ctx);
618 dt_txn_hook_commit(env, th);
619 lu_context_exit(&env->le_ctx);
622 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
626 lu_context_exit(&th->th_ctx);
627 lu_context_fini(&th->th_ctx);
632 * Concurrency: shouldn't matter.
634 static struct thandle *osd_trans_start(const struct lu_env *env,
638 struct osd_device *dev = osd_dt_dev(d);
640 struct osd_thandle *oh;
646 hook_res = dt_txn_hook_start(env, d, p);
648 RETURN(ERR_PTR(hook_res));
650 if (osd_param_is_sane(dev, p)) {
651 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
653 struct osd_thread_info *oti = osd_oti_get(env);
656 * XXX temporary stuff. Some abstraction layer should
661 jh = ldiskfs_journal_start_sb(osd_sb(dev), p->tp_credits);
668 jh->h_sync = p->tp_sync;
669 lu_device_get(&d->dd_lu_dev);
670 oh->ot_dev_link = lu_ref_add
671 (&d->dd_lu_dev.ld_reference,
673 /* add commit callback */
674 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
675 lu_context_enter(&th->th_ctx);
676 osd_journal_callback_set(jh, osd_trans_commit_cb,
677 (struct journal_callback *)&oh->ot_jcb);
678 LASSERT(oti->oti_txns == 0);
679 LASSERT(oti->oti_r_locks == 0);
680 LASSERT(oti->oti_w_locks == 0);
687 th = ERR_PTR(-ENOMEM);
689 CERROR("Invalid transaction parameters\n");
690 th = ERR_PTR(-EINVAL);
697 * Concurrency: shouldn't matter.
699 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
702 struct osd_thandle *oh;
703 struct osd_thread_info *oti = osd_oti_get(env);
707 oh = container_of0(th, struct osd_thandle, ot_super);
708 if (oh->ot_handle != NULL) {
709 handle_t *hdl = oh->ot_handle;
711 LASSERT(oti->oti_txns == 1);
713 LASSERT(oti->oti_r_locks == 0);
714 LASSERT(oti->oti_w_locks == 0);
715 result = dt_txn_hook_stop(env, th);
717 CERROR("Failure in transaction hook: %d\n", result);
718 oh->ot_handle = NULL;
719 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
720 result = ldiskfs_journal_stop(hdl));
722 CERROR("Failure to stop transaction: %d\n", result);
728 * Concurrency: no concurrent access is possible that late in object
731 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
733 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
734 struct osd_device *osd = osd_obj2dev(obj);
735 struct osd_thread_info *oti = osd_oti_get(env);
736 struct txn_param *prm = &oti->oti_txn;
737 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
741 lu_env_init(env_del_obj, LCT_DT_THREAD);
742 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
743 OSD_TXN_INODE_DELETE_CREDITS);
744 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
746 result = osd_oi_delete(osd_oti_get(env_del_obj),
747 &osd->od_oi, fid, th);
748 osd_trans_stop(env_del_obj, th);
750 result = PTR_ERR(th);
752 lu_env_fini(env_del_obj);
757 * Called just before object is freed. Releases all resources except for
758 * object itself (that is released by osd_object_free()).
760 * Concurrency: no concurrent access is possible that late in object
763 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
765 struct osd_object *obj = osd_obj(l);
766 struct inode *inode = obj->oo_inode;
768 LINVRNT(osd_invariant(obj));
771 * If object is unlinked remove fid->ino mapping from object index.
778 if (osd_inode_unlinked(inode)) {
779 result = osd_inode_remove(env, obj);
781 LU_OBJECT_DEBUG(D_ERROR, env, l,
782 "Failed to cleanup: %d\n",
787 obj->oo_inode = NULL;
792 * Concurrency: ->loo_object_release() is called under site spin-lock.
794 static void osd_object_release(const struct lu_env *env,
797 struct osd_object *o = osd_obj(l);
799 LASSERT(!lu_object_is_dying(l->lo_header));
800 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
801 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
805 * Concurrency: shouldn't matter.
807 static int osd_object_print(const struct lu_env *env, void *cookie,
808 lu_printer_t p, const struct lu_object *l)
810 struct osd_object *o = osd_obj(l);
813 if (o->oo_dir != NULL)
814 d = o->oo_dir->od_container.ic_descr;
817 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
819 o->oo_inode ? o->oo_inode->i_ino : 0UL,
820 o->oo_inode ? o->oo_inode->i_generation : 0,
821 d ? d->id_ops->id_name : "plain");
825 * Concurrency: shouldn't matter.
827 int osd_statfs(const struct lu_env *env, struct dt_device *d,
830 struct osd_device *osd = osd_dt_dev(d);
831 struct super_block *sb = osd_sb(osd);
834 cfs_spin_lock(&osd->od_osfs_lock);
836 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
837 result = ll_do_statfs(sb, &osd->od_kstatfs);
838 if (likely(result == 0)) /* N.B. statfs can't really fail */
839 osd->od_osfs_age = cfs_time_current_64();
842 if (likely(result == 0))
843 *sfs = osd->od_kstatfs;
844 cfs_spin_unlock(&osd->od_osfs_lock);
850 * Concurrency: doesn't access mutable data.
852 static void osd_conf_get(const struct lu_env *env,
853 const struct dt_device *dev,
854 struct dt_device_param *param)
857 * XXX should be taken from not-yet-existing fs abstraction layer.
859 param->ddp_max_name_len = LDISKFS_NAME_LEN;
860 param->ddp_max_nlink = LDISKFS_LINK_MAX;
861 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
865 * Helper function to get and fill the buffer with input values.
867 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
871 buf = &osd_oti_get(env)->oti_buf;
878 * Concurrency: shouldn't matter.
880 static int osd_sync(const struct lu_env *env, struct dt_device *d)
882 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
883 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
887 * Start commit for OSD device.
889 * An implementation of dt_commit_async method for OSD device.
890 * Asychronously starts underlayng fs sync and thereby a transaction
893 * \param env environment
896 * \see dt_device_operations
898 static int osd_commit_async(const struct lu_env *env,
901 struct super_block *s = osd_sb(osd_dt_dev(d));
904 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
905 RETURN(s->s_op->sync_fs(s, 0));
909 * Concurrency: shouldn't matter.
911 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
913 static void osd_ro(const struct lu_env *env, struct dt_device *d)
917 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
919 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
920 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
926 * Concurrency: serialization provided by callers.
928 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
929 int mode, unsigned long timeout, __u32 alg,
930 struct lustre_capa_key *keys)
932 struct osd_device *dev = osd_dt_dev(d);
935 dev->od_fl_capa = mode;
936 dev->od_capa_timeout = timeout;
937 dev->od_capa_alg = alg;
938 dev->od_capa_keys = keys;
943 * Concurrency: serialization provided by callers.
945 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
946 struct dt_quota_ctxt *ctxt, void *data)
948 struct obd_device *obd = (void *)ctxt;
949 struct vfsmount *mnt = (struct vfsmount *)data;
952 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
953 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
954 obd->obd_lvfs_ctxt.pwdmnt = mnt;
955 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
956 obd->obd_lvfs_ctxt.fs = get_ds();
962 * Note: we do not count into QUOTA here.
963 * If we mount with --data_journal we may need more.
965 static const int osd_dto_credits_noquota[DTO_NR] = {
968 * INDEX_EXTRA_TRANS_BLOCKS(8) +
969 * SINGLEDATA_TRANS_BLOCKS(8)
970 * XXX Note: maybe iam need more, since iam have more level than
973 [DTO_INDEX_INSERT] = 16,
974 [DTO_INDEX_DELETE] = 16,
978 [DTO_IDNEX_UPDATE] = 16,
980 * Create a object. The same as create object in EXT3.
981 * DATA_TRANS_BLOCKS(14) +
982 * INDEX_EXTRA_BLOCKS(8) +
983 * 3(inode bits, groups, GDT)
985 [DTO_OBJECT_CREATE] = 25,
989 [DTO_OBJECT_DELETE] = 25,
992 * 3(inode bits, group, GDT)
994 [DTO_ATTR_SET_BASE] = 3,
996 * Xattr set. The same as xattr of EXT3.
997 * DATA_TRANS_BLOCKS(14)
998 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
999 * are also counted in. Do not know why?
1001 [DTO_XATTR_SET] = 14,
1004 * creadits for inode change during write.
1006 [DTO_WRITE_BASE] = 3,
1008 * credits for single block write.
1010 [DTO_WRITE_BLOCK] = 14,
1012 * Attr set credits for chown.
1013 * This is extra credits for setattr, and it is null without quota
1015 [DTO_ATTR_SET_CHOWN]= 0
1019 * Note: we count into QUOTA here.
1020 * If we mount with --data_journal we may need more.
1022 static const int osd_dto_credits_quota[DTO_NR] = {
1024 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1025 * SINGLEDATA_TRANS_BLOCKS(8) +
1026 * 2 * QUOTA_TRANS_BLOCKS(2)
1028 [DTO_INDEX_INSERT] = 20,
1030 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1031 * SINGLEDATA_TRANS_BLOCKS(8) +
1032 * 2 * QUOTA_TRANS_BLOCKS(2)
1034 [DTO_INDEX_DELETE] = 20,
1038 [DTO_IDNEX_UPDATE] = 16,
1040 * Create a object. Same as create object in EXT3 filesystem.
1041 * DATA_TRANS_BLOCKS(16) +
1042 * INDEX_EXTRA_BLOCKS(8) +
1043 * 3(inode bits, groups, GDT) +
1044 * 2 * QUOTA_INIT_BLOCKS(25)
1046 [DTO_OBJECT_CREATE] = 77,
1049 * DATA_TRANS_BLOCKS(16) +
1050 * INDEX_EXTRA_BLOCKS(8) +
1051 * 3(inode bits, groups, GDT) +
1054 [DTO_OBJECT_DELETE] = 27,
1057 * 3 (inode bit, group, GDT) +
1059 [DTO_ATTR_SET_BASE] = 3,
1061 * Xattr set. The same as xattr of EXT3.
1062 * DATA_TRANS_BLOCKS(16)
1063 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
1064 * also counted in. Do not know why?
1066 [DTO_XATTR_SET] = 16,
1069 * creadits for inode change during write.
1071 [DTO_WRITE_BASE] = 3,
1073 * credits for single block write.
1075 [DTO_WRITE_BLOCK] = 16,
1077 * Attr set credits for chown.
1078 * It is added to already set setattr credits
1079 * 2 * QUOTA_INIT_BLOCKS(25) +
1080 * 2 * QUOTA_DEL_BLOCKS(9)
1082 [DTO_ATTR_SET_CHOWN]= 68,
1085 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1088 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1089 ARRAY_SIZE(osd_dto_credits_quota));
1090 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1091 #ifdef HAVE_QUOTA_SUPPORT
1092 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1093 return osd_dto_credits_quota[op];
1096 return osd_dto_credits_noquota[op];
1099 static const struct dt_device_operations osd_dt_ops = {
1100 .dt_root_get = osd_root_get,
1101 .dt_statfs = osd_statfs,
1102 .dt_trans_start = osd_trans_start,
1103 .dt_trans_stop = osd_trans_stop,
1104 .dt_conf_get = osd_conf_get,
1105 .dt_sync = osd_sync,
1107 .dt_commit_async = osd_commit_async,
1108 .dt_credit_get = osd_credit_get,
1109 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1110 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1113 static void osd_object_read_lock(const struct lu_env *env,
1114 struct dt_object *dt, unsigned role)
1116 struct osd_object *obj = osd_dt_obj(dt);
1117 struct osd_thread_info *oti = osd_oti_get(env);
1119 LINVRNT(osd_invariant(obj));
1121 LASSERT(obj->oo_owner != env);
1122 cfs_down_read_nested(&obj->oo_sem, role);
1124 LASSERT(obj->oo_owner == NULL);
1128 static void osd_object_write_lock(const struct lu_env *env,
1129 struct dt_object *dt, unsigned role)
1131 struct osd_object *obj = osd_dt_obj(dt);
1132 struct osd_thread_info *oti = osd_oti_get(env);
1134 LINVRNT(osd_invariant(obj));
1136 LASSERT(obj->oo_owner != env);
1137 cfs_down_write_nested(&obj->oo_sem, role);
1139 LASSERT(obj->oo_owner == NULL);
1140 obj->oo_owner = env;
1144 static void osd_object_read_unlock(const struct lu_env *env,
1145 struct dt_object *dt)
1147 struct osd_object *obj = osd_dt_obj(dt);
1148 struct osd_thread_info *oti = osd_oti_get(env);
1150 LINVRNT(osd_invariant(obj));
1152 LASSERT(oti->oti_r_locks > 0);
1154 cfs_up_read(&obj->oo_sem);
1157 static void osd_object_write_unlock(const struct lu_env *env,
1158 struct dt_object *dt)
1160 struct osd_object *obj = osd_dt_obj(dt);
1161 struct osd_thread_info *oti = osd_oti_get(env);
1163 LINVRNT(osd_invariant(obj));
1165 LASSERT(obj->oo_owner == env);
1166 LASSERT(oti->oti_w_locks > 0);
1168 obj->oo_owner = NULL;
1169 cfs_up_write(&obj->oo_sem);
1172 static int osd_object_write_locked(const struct lu_env *env,
1173 struct dt_object *dt)
1175 struct osd_object *obj = osd_dt_obj(dt);
1177 LINVRNT(osd_invariant(obj));
1179 return obj->oo_owner == env;
1182 static int capa_is_sane(const struct lu_env *env,
1183 struct osd_device *dev,
1184 struct lustre_capa *capa,
1185 struct lustre_capa_key *keys)
1187 struct osd_thread_info *oti = osd_oti_get(env);
1188 struct lustre_capa *tcapa = &oti->oti_capa;
1189 struct obd_capa *oc;
1193 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1195 if (capa_is_expired(oc)) {
1196 DEBUG_CAPA(D_ERROR, capa, "expired");
1203 if (capa_is_expired_sec(capa)) {
1204 DEBUG_CAPA(D_ERROR, capa, "expired");
1208 cfs_spin_lock(&capa_lock);
1209 for (i = 0; i < 2; i++) {
1210 if (keys[i].lk_keyid == capa->lc_keyid) {
1211 oti->oti_capa_key = keys[i];
1215 cfs_spin_unlock(&capa_lock);
1218 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1222 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1226 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1227 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1231 oc = capa_add(dev->od_capa_hash, capa);
1237 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1238 struct lustre_capa *capa, __u64 opc)
1240 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1241 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1242 struct md_capainfo *ci;
1245 if (!dev->od_fl_capa)
1248 if (capa == BYPASS_CAPA)
1251 ci = md_capainfo(env);
1255 if (ci->mc_auth == LC_ID_NONE)
1259 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1263 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1264 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1269 if (!capa_opc_supported(capa, opc)) {
1270 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1274 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1275 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1282 static struct timespec *osd_inode_time(const struct lu_env *env,
1283 struct inode *inode, __u64 seconds)
1285 struct osd_thread_info *oti = osd_oti_get(env);
1286 struct timespec *t = &oti->oti_time;
1288 t->tv_sec = seconds;
1290 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1295 static void osd_inode_getattr(const struct lu_env *env,
1296 struct inode *inode, struct lu_attr *attr)
1298 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1299 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1300 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1302 attr->la_atime = LTIME_S(inode->i_atime);
1303 attr->la_mtime = LTIME_S(inode->i_mtime);
1304 attr->la_ctime = LTIME_S(inode->i_ctime);
1305 attr->la_mode = inode->i_mode;
1306 attr->la_size = i_size_read(inode);
1307 attr->la_blocks = inode->i_blocks;
1308 attr->la_uid = inode->i_uid;
1309 attr->la_gid = inode->i_gid;
1310 attr->la_flags = LDISKFS_I(inode)->i_flags;
1311 attr->la_nlink = inode->i_nlink;
1312 attr->la_rdev = inode->i_rdev;
1313 attr->la_blksize = ll_inode_blksize(inode);
1314 attr->la_blkbits = inode->i_blkbits;
1317 static int osd_attr_get(const struct lu_env *env,
1318 struct dt_object *dt,
1319 struct lu_attr *attr,
1320 struct lustre_capa *capa)
1322 struct osd_object *obj = osd_dt_obj(dt);
1324 LASSERT(dt_object_exists(dt));
1325 LINVRNT(osd_invariant(obj));
1327 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1330 cfs_spin_lock(&obj->oo_guard);
1331 osd_inode_getattr(env, obj->oo_inode, attr);
1332 cfs_spin_unlock(&obj->oo_guard);
1336 static int osd_inode_setattr(const struct lu_env *env,
1337 struct inode *inode, const struct lu_attr *attr)
1341 bits = attr->la_valid;
1343 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1345 #ifdef HAVE_QUOTA_SUPPORT
1346 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1347 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1348 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1354 iattr.ia_valid |= ATTR_UID;
1356 iattr.ia_valid |= ATTR_GID;
1357 iattr.ia_uid = attr->la_uid;
1358 iattr.ia_gid = attr->la_gid;
1359 osd_push_ctxt(env, save);
1360 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1367 if (bits & LA_ATIME)
1368 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1369 if (bits & LA_CTIME)
1370 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1371 if (bits & LA_MTIME)
1372 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1373 if (bits & LA_SIZE) {
1374 LDISKFS_I(inode)->i_disksize = attr->la_size;
1375 i_size_write(inode, attr->la_size);
1379 /* OSD should not change "i_blocks" which is used by quota.
1380 * "i_blocks" should be changed by ldiskfs only. */
1381 if (bits & LA_BLOCKS)
1382 inode->i_blocks = attr->la_blocks;
1385 inode->i_mode = (inode->i_mode & S_IFMT) |
1386 (attr->la_mode & ~S_IFMT);
1388 inode->i_uid = attr->la_uid;
1390 inode->i_gid = attr->la_gid;
1391 if (bits & LA_NLINK)
1392 inode->i_nlink = attr->la_nlink;
1394 inode->i_rdev = attr->la_rdev;
1396 if (bits & LA_FLAGS)
1397 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags);
1401 static int osd_attr_set(const struct lu_env *env,
1402 struct dt_object *dt,
1403 const struct lu_attr *attr,
1404 struct thandle *handle,
1405 struct lustre_capa *capa)
1407 struct osd_object *obj = osd_dt_obj(dt);
1410 LASSERT(handle != NULL);
1411 LASSERT(dt_object_exists(dt));
1412 LASSERT(osd_invariant(obj));
1414 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1417 cfs_spin_lock(&obj->oo_guard);
1418 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1419 cfs_spin_unlock(&obj->oo_guard);
1422 mark_inode_dirty(obj->oo_inode);
1429 * XXX temporary solution.
1431 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1432 struct lu_attr *attr, struct thandle *th)
1437 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1438 struct lu_attr *attr, struct thandle *th)
1440 osd_object_init0(obj);
1444 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1445 struct osd_object *obj,
1449 struct osd_thread_info *info = osd_oti_get(env);
1450 struct dentry *child_dentry = &info->oti_child_dentry;
1451 struct dentry *obj_dentry = &info->oti_obj_dentry;
1453 obj_dentry->d_inode = obj->oo_inode;
1454 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1455 obj_dentry->d_name.hash = 0;
1457 child_dentry->d_name.hash = 0;
1458 child_dentry->d_parent = obj_dentry;
1459 child_dentry->d_name.name = name;
1460 child_dentry->d_name.len = namelen;
1461 return child_dentry;
1465 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1467 struct dt_allocation_hint *hint,
1471 struct osd_device *osd = osd_obj2dev(obj);
1472 struct osd_thandle *oth;
1473 struct dt_object *parent;
1474 struct inode *inode;
1475 #ifdef HAVE_QUOTA_SUPPORT
1476 struct osd_ctxt *save = &info->oti_ctxt;
1479 LINVRNT(osd_invariant(obj));
1480 LASSERT(obj->oo_inode == NULL);
1482 oth = container_of(th, struct osd_thandle, ot_super);
1483 LASSERT(oth->ot_handle->h_transaction != NULL);
1485 if (hint && hint->dah_parent)
1486 parent = hint->dah_parent;
1488 parent = osd->od_obj_area;
1490 LASSERT(parent != NULL);
1491 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1493 #ifdef HAVE_QUOTA_SUPPORT
1494 osd_push_ctxt(info->oti_env, save);
1496 inode = ldiskfs_create_inode(oth->ot_handle,
1497 osd_dt_obj(parent)->oo_inode, mode);
1498 #ifdef HAVE_QUOTA_SUPPORT
1501 if (!IS_ERR(inode)) {
1502 obj->oo_inode = inode;
1505 result = PTR_ERR(inode);
1506 LINVRNT(osd_invariant(obj));
1514 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1515 struct lu_attr *attr,
1516 struct dt_allocation_hint *hint,
1517 struct dt_object_format *dof,
1521 struct osd_thandle *oth;
1522 struct osd_device *osd = osd_obj2dev(obj);
1523 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1525 LASSERT(S_ISDIR(attr->la_mode));
1527 oth = container_of(th, struct osd_thandle, ot_super);
1528 LASSERT(oth->ot_handle->h_transaction != NULL);
1529 result = osd_mkfile(info, obj, mode, hint, th);
1530 if (result == 0 && osd->od_iop_mode == 0) {
1531 LASSERT(obj->oo_inode != NULL);
1533 * XXX uh-oh... call low-level iam function directly.
1536 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1537 sizeof (struct osd_fid_pack),
1543 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1544 struct lu_attr *attr,
1545 struct dt_allocation_hint *hint,
1546 struct dt_object_format *dof,
1550 struct osd_thandle *oth;
1551 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1553 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1555 LASSERT(S_ISREG(attr->la_mode));
1557 oth = container_of(th, struct osd_thandle, ot_super);
1558 LASSERT(oth->ot_handle->h_transaction != NULL);
1560 result = osd_mkfile(info, obj, mode, hint, th);
1562 LASSERT(obj->oo_inode != NULL);
1563 if (feat->dif_flags & DT_IND_VARKEY)
1564 result = iam_lvar_create(obj->oo_inode,
1565 feat->dif_keysize_max,
1567 feat->dif_recsize_max,
1570 result = iam_lfix_create(obj->oo_inode,
1571 feat->dif_keysize_max,
1573 feat->dif_recsize_max,
1580 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1581 struct lu_attr *attr,
1582 struct dt_allocation_hint *hint,
1583 struct dt_object_format *dof,
1586 LASSERT(S_ISREG(attr->la_mode));
1587 return osd_mkfile(info, obj, (attr->la_mode &
1588 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1591 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1592 struct lu_attr *attr,
1593 struct dt_allocation_hint *hint,
1594 struct dt_object_format *dof,
1597 LASSERT(S_ISLNK(attr->la_mode));
1598 return osd_mkfile(info, obj, (attr->la_mode &
1599 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1602 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1603 struct lu_attr *attr,
1604 struct dt_allocation_hint *hint,
1605 struct dt_object_format *dof,
1608 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1611 LINVRNT(osd_invariant(obj));
1612 LASSERT(obj->oo_inode == NULL);
1613 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1614 S_ISFIFO(mode) || S_ISSOCK(mode));
1616 result = osd_mkfile(info, obj, mode, hint, th);
1618 LASSERT(obj->oo_inode != NULL);
1619 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1621 LINVRNT(osd_invariant(obj));
1625 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1627 struct dt_allocation_hint *hint,
1628 struct dt_object_format *dof,
1631 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1633 osd_obj_type_f result;
1649 result = osd_mk_index;
1660 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1661 struct dt_object *parent, cfs_umode_t child_mode)
1665 memset(ah, 0, sizeof(*ah));
1666 ah->dah_parent = parent;
1667 ah->dah_mode = child_mode;
1671 * Helper function for osd_object_create()
1673 * \retval 0, on success
1675 static int __osd_object_create(struct osd_thread_info *info,
1676 struct osd_object *obj, struct lu_attr *attr,
1677 struct dt_allocation_hint *hint,
1678 struct dt_object_format *dof,
1684 result = osd_create_pre(info, obj, attr, th);
1686 result = osd_create_type_f(dof->dof_type)(info, obj,
1687 attr, hint, dof, th);
1689 result = osd_create_post(info, obj, attr, th);
1695 * Helper function for osd_object_create()
1697 * \retval 0, on success
1699 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1700 const struct lu_fid *fid, struct thandle *th)
1702 struct osd_thread_info *info = osd_oti_get(env);
1703 struct osd_inode_id *id = &info->oti_id;
1704 struct osd_device *osd = osd_obj2dev(obj);
1705 struct md_ucred *uc = md_ucred(env);
1707 LASSERT(obj->oo_inode != NULL);
1708 LASSERT(uc != NULL);
1710 id->oii_ino = obj->oo_inode->i_ino;
1711 id->oii_gen = obj->oo_inode->i_generation;
1713 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1714 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1717 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1718 struct lu_attr *attr,
1719 struct dt_allocation_hint *hint,
1720 struct dt_object_format *dof,
1723 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1724 struct osd_object *obj = osd_dt_obj(dt);
1725 struct osd_thread_info *info = osd_oti_get(env);
1730 LINVRNT(osd_invariant(obj));
1731 LASSERT(!dt_object_exists(dt));
1732 LASSERT(osd_write_locked(env, obj));
1733 LASSERT(th != NULL);
1735 result = __osd_object_create(info, obj, attr, hint, dof, th);
1737 result = __osd_oi_insert(env, obj, fid, th);
1739 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1740 LASSERT(osd_invariant(obj));
1745 * Helper function for osd_xattr_set()
1747 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1748 const struct lu_buf *buf, const char *name, int fl)
1750 struct osd_object *obj = osd_dt_obj(dt);
1751 struct inode *inode = obj->oo_inode;
1752 struct osd_thread_info *info = osd_oti_get(env);
1753 struct dentry *dentry = &info->oti_child_dentry;
1754 struct timespec *t = &info->oti_time;
1758 LASSERT(dt_object_exists(dt));
1759 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1760 LASSERT(osd_write_locked(env, obj));
1762 if (fl & LU_XATTR_REPLACE)
1763 fs_flags |= XATTR_REPLACE;
1765 if (fl & LU_XATTR_CREATE)
1766 fs_flags |= XATTR_CREATE;
1768 dentry->d_inode = inode;
1769 *t = inode->i_ctime;
1770 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1771 buf->lb_len, fs_flags);
1772 /* ctime should not be updated with server-side time. */
1773 cfs_spin_lock(&obj->oo_guard);
1774 inode->i_ctime = *t;
1775 cfs_spin_unlock(&obj->oo_guard);
1776 mark_inode_dirty(inode);
1781 * Put the fid into lustre_mdt_attrs, and then place the structure
1782 * inode's ea. This fid should not be altered during the life time
1785 * \retval +ve, on success
1786 * \retval -ve, on error
1788 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1790 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1791 const struct lu_fid *fid)
1793 struct osd_thread_info *info = osd_oti_get(env);
1794 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1796 lustre_lma_init(mdt_attrs, fid);
1797 lustre_lma_swab(mdt_attrs);
1798 return __osd_xattr_set(env, dt,
1799 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1800 XATTR_NAME_LMA, LU_XATTR_CREATE);
1805 * Helper function to form igif
1807 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1810 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1814 * Helper function to pack the fid, ldiskfs stores fid in packed format.
1816 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1817 struct lu_fid *befider)
1819 fid_cpu_to_be(befider, (struct lu_fid *)fid);
1820 memcpy(pack->fp_area, befider, sizeof(*befider));
1821 pack->fp_len = sizeof(*befider) + 1;
1825 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1826 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1827 * To have compatilibility with 1.8 ldiskfs driver we need to have
1828 * magic number at start of fid data.
1829 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1832 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1833 const struct dt_rec *fid)
1835 param->edp_magic = LDISKFS_LUFID_MAGIC;
1836 param->edp_len = sizeof(struct lu_fid) + 1;
1838 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1839 (struct lu_fid *)fid);
1842 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1847 switch (pack->fp_len) {
1848 case sizeof *fid + 1:
1849 memcpy(fid, pack->fp_area, sizeof *fid);
1850 fid_be_to_cpu(fid, fid);
1853 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1860 * Try to read the fid from inode ea into dt_rec, if return value
1861 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1863 * \param fid object fid.
1865 * \retval 0 on success
1867 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1868 __u32 ino, struct lu_fid *fid)
1870 struct osd_thread_info *info = osd_oti_get(env);
1871 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1872 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1873 struct dentry *dentry = &info->oti_child_dentry;
1874 struct osd_inode_id *id = &info->oti_id;
1875 struct osd_device *dev;
1876 struct inode *inode;
1880 dev = osd_dev(ldev);
1883 id->oii_gen = OSD_OII_NOGEN;
1885 inode = osd_iget(info, dev, id);
1886 if (IS_ERR(inode)) {
1887 rc = PTR_ERR(inode);
1890 dentry->d_inode = inode;
1892 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1893 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1896 /* Check LMA compatibility */
1898 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
1899 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1900 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
1901 ~LMA_INCOMPAT_SUPP);
1906 lustre_lma_swab(mdt_attrs);
1907 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
1909 } else if (rc == -ENODATA) {
1910 osd_igif_get(env, inode, fid);
1919 * OSD layer object create function for interoperability mode (b11826).
1920 * This is mostly similar to osd_object_create(). Only difference being, fid is
1921 * inserted into inode ea here.
1923 * \retval 0, on success
1924 * \retval -ve, on error
1926 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1927 struct lu_attr *attr,
1928 struct dt_allocation_hint *hint,
1929 struct dt_object_format *dof,
1932 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1933 struct osd_object *obj = osd_dt_obj(dt);
1934 struct osd_thread_info *info = osd_oti_get(env);
1939 LASSERT(osd_invariant(obj));
1940 LASSERT(!dt_object_exists(dt));
1941 LASSERT(osd_write_locked(env, obj));
1942 LASSERT(th != NULL);
1944 result = __osd_object_create(info, obj, attr, hint, dof, th);
1946 /* objects under osd root shld have igif fid, so dont add fid EA */
1947 if (result == 0 && fid_seq(fid) >= FID_SEQ_DISTRIBUTED_START)
1948 result = osd_ea_fid_set(env, dt, fid);
1951 result = __osd_oi_insert(env, obj, fid, th);
1953 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1954 LINVRNT(osd_invariant(obj));
1959 * Concurrency: @dt is write locked.
1961 static void osd_object_ref_add(const struct lu_env *env,
1962 struct dt_object *dt,
1965 struct osd_object *obj = osd_dt_obj(dt);
1966 struct inode *inode = obj->oo_inode;
1968 LINVRNT(osd_invariant(obj));
1969 LASSERT(dt_object_exists(dt));
1970 LASSERT(osd_write_locked(env, obj));
1971 LASSERT(th != NULL);
1973 cfs_spin_lock(&obj->oo_guard);
1974 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1976 cfs_spin_unlock(&obj->oo_guard);
1977 mark_inode_dirty(inode);
1978 LINVRNT(osd_invariant(obj));
1982 * Concurrency: @dt is write locked.
1984 static void osd_object_ref_del(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 > 0);
1999 cfs_spin_unlock(&obj->oo_guard);
2000 mark_inode_dirty(inode);
2001 LINVRNT(osd_invariant(obj));
2005 * Concurrency: @dt is read locked.
2007 static int osd_xattr_get(const struct lu_env *env,
2008 struct dt_object *dt,
2011 struct lustre_capa *capa)
2013 struct osd_object *obj = osd_dt_obj(dt);
2014 struct inode *inode = obj->oo_inode;
2015 struct osd_thread_info *info = osd_oti_get(env);
2016 struct dentry *dentry = &info->oti_obj_dentry;
2018 LASSERT(dt_object_exists(dt));
2019 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2020 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2022 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2025 dentry->d_inode = inode;
2026 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2030 * Concurrency: @dt is write locked.
2032 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2033 const struct lu_buf *buf, const char *name, int fl,
2034 struct thandle *handle, struct lustre_capa *capa)
2036 LASSERT(handle != NULL);
2038 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2041 return __osd_xattr_set(env, dt, buf, name, fl);
2045 * Concurrency: @dt is read locked.
2047 static int osd_xattr_list(const struct lu_env *env,
2048 struct dt_object *dt,
2050 struct lustre_capa *capa)
2052 struct osd_object *obj = osd_dt_obj(dt);
2053 struct inode *inode = obj->oo_inode;
2054 struct osd_thread_info *info = osd_oti_get(env);
2055 struct dentry *dentry = &info->oti_obj_dentry;
2057 LASSERT(dt_object_exists(dt));
2058 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2059 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2061 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2064 dentry->d_inode = inode;
2065 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2069 * Concurrency: @dt is write locked.
2071 static int osd_xattr_del(const struct lu_env *env,
2072 struct dt_object *dt,
2074 struct thandle *handle,
2075 struct lustre_capa *capa)
2077 struct osd_object *obj = osd_dt_obj(dt);
2078 struct inode *inode = obj->oo_inode;
2079 struct osd_thread_info *info = osd_oti_get(env);
2080 struct dentry *dentry = &info->oti_obj_dentry;
2081 struct timespec *t = &info->oti_time;
2084 LASSERT(dt_object_exists(dt));
2085 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2086 LASSERT(osd_write_locked(env, obj));
2087 LASSERT(handle != NULL);
2089 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2092 dentry->d_inode = inode;
2093 *t = inode->i_ctime;
2094 rc = inode->i_op->removexattr(dentry, name);
2095 /* ctime should not be updated with server-side time. */
2096 cfs_spin_lock(&obj->oo_guard);
2097 inode->i_ctime = *t;
2098 cfs_spin_unlock(&obj->oo_guard);
2099 mark_inode_dirty(inode);
2103 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2104 struct dt_object *dt,
2105 struct lustre_capa *old,
2108 struct osd_thread_info *info = osd_oti_get(env);
2109 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2110 struct osd_object *obj = osd_dt_obj(dt);
2111 struct osd_device *dev = osd_obj2dev(obj);
2112 struct lustre_capa_key *key = &info->oti_capa_key;
2113 struct lustre_capa *capa = &info->oti_capa;
2114 struct obd_capa *oc;
2115 struct md_capainfo *ci;
2119 if (!dev->od_fl_capa)
2120 RETURN(ERR_PTR(-ENOENT));
2122 LASSERT(dt_object_exists(dt));
2123 LINVRNT(osd_invariant(obj));
2125 /* renewal sanity check */
2126 if (old && osd_object_auth(env, dt, old, opc))
2127 RETURN(ERR_PTR(-EACCES));
2129 ci = md_capainfo(env);
2131 RETURN(ERR_PTR(-ENOENT));
2133 switch (ci->mc_auth) {
2137 capa->lc_uid = obj->oo_inode->i_uid;
2138 capa->lc_gid = obj->oo_inode->i_gid;
2139 capa->lc_flags = LC_ID_PLAIN;
2141 case LC_ID_CONVERT: {
2144 s[0] = obj->oo_inode->i_uid;
2145 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2146 s[2] = obj->oo_inode->i_gid;
2147 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2148 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2150 RETURN(ERR_PTR(rc));
2152 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2153 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2154 capa->lc_flags = LC_ID_CONVERT;
2158 RETURN(ERR_PTR(-EINVAL));
2161 capa->lc_fid = *fid;
2163 capa->lc_flags |= dev->od_capa_alg << 24;
2164 capa->lc_timeout = dev->od_capa_timeout;
2165 capa->lc_expiry = 0;
2167 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2169 LASSERT(!capa_is_expired(oc));
2173 cfs_spin_lock(&capa_lock);
2174 *key = dev->od_capa_keys[1];
2175 cfs_spin_unlock(&capa_lock);
2177 capa->lc_keyid = key->lk_keyid;
2178 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2180 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2182 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2183 RETURN(ERR_PTR(rc));
2186 oc = capa_add(dev->od_capa_hash, capa);
2190 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2193 struct osd_object *obj = osd_dt_obj(dt);
2194 struct inode *inode = obj->oo_inode;
2195 struct osd_thread_info *info = osd_oti_get(env);
2196 struct dentry *dentry = &info->oti_obj_dentry;
2197 struct file *file = &info->oti_file;
2200 dentry->d_inode = inode;
2201 file->f_dentry = dentry;
2202 file->f_mapping = inode->i_mapping;
2203 file->f_op = inode->i_fop;
2204 LOCK_INODE_MUTEX(inode);
2205 rc = file->f_op->fsync(file, dentry, 0);
2206 UNLOCK_INODE_MUTEX(inode);
2211 * Get the 64-bit version for an inode.
2213 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2214 struct dt_object *dt)
2216 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2218 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2219 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2220 return LDISKFS_I(inode)->i_fs_version;
2224 * Set the 64-bit version and return the old version.
2226 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2227 dt_obj_version_t new_version)
2229 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2231 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2232 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2233 LDISKFS_I(inode)->i_fs_version = new_version;
2234 /** Version is set after all inode operations are finished,
2235 * so we should mark it dirty here */
2236 inode->i_sb->s_op->dirty_inode(inode);
2239 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2242 struct osd_object *obj = osd_dt_obj(dt);
2245 *data = (void *)obj->oo_inode;
2253 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2254 const struct dt_index_features *feat)
2256 struct iam_descr *descr;
2258 if (osd_object_is_root(o))
2259 return feat == &dt_directory_features;
2261 LASSERT(o->oo_dir != NULL);
2263 descr = o->oo_dir->od_container.ic_descr;
2264 if (feat == &dt_directory_features) {
2265 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2271 feat->dif_keysize_min <= descr->id_key_size &&
2272 descr->id_key_size <= feat->dif_keysize_max &&
2273 feat->dif_recsize_min <= descr->id_rec_size &&
2274 descr->id_rec_size <= feat->dif_recsize_max &&
2275 !(feat->dif_flags & (DT_IND_VARKEY |
2276 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2277 ergo(feat->dif_flags & DT_IND_UPDATE,
2278 1 /* XXX check that object (and file system) is
2283 static int osd_iam_container_init(const struct lu_env *env,
2284 struct osd_object *obj,
2285 struct osd_directory *dir)
2288 struct iam_container *bag;
2290 bag = &dir->od_container;
2291 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2293 result = iam_container_setup(bag);
2295 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2297 iam_container_fini(bag);
2304 * Concurrency: no external locking is necessary.
2306 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2307 const struct dt_index_features *feat)
2311 struct osd_object *obj = osd_dt_obj(dt);
2312 struct osd_device *osd = osd_obj2dev(obj);
2314 LINVRNT(osd_invariant(obj));
2315 LASSERT(dt_object_exists(dt));
2317 if (osd_object_is_root(obj)) {
2318 dt->do_index_ops = &osd_index_ea_ops;
2320 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2321 dt->do_index_ops = &osd_index_ea_ops;
2322 if (S_ISDIR(obj->oo_inode->i_mode))
2327 } else if (!osd_has_index(obj)) {
2328 struct osd_directory *dir;
2333 cfs_spin_lock(&obj->oo_guard);
2334 if (obj->oo_dir == NULL)
2338 * Concurrent thread allocated container data.
2341 cfs_spin_unlock(&obj->oo_guard);
2343 * Now, that we have container data, serialize its
2346 cfs_down_write(&obj->oo_ext_idx_sem);
2348 * recheck under lock.
2350 if (!osd_has_index(obj))
2351 result = osd_iam_container_init(env, obj, dir);
2354 cfs_up_write(&obj->oo_ext_idx_sem);
2360 if (result == 0 && ea_dir == 0) {
2361 if (!osd_iam_index_probe(env, obj, feat))
2364 LINVRNT(osd_invariant(obj));
2369 static const struct dt_object_operations osd_obj_ops = {
2370 .do_read_lock = osd_object_read_lock,
2371 .do_write_lock = osd_object_write_lock,
2372 .do_read_unlock = osd_object_read_unlock,
2373 .do_write_unlock = osd_object_write_unlock,
2374 .do_write_locked = osd_object_write_locked,
2375 .do_attr_get = osd_attr_get,
2376 .do_attr_set = osd_attr_set,
2377 .do_ah_init = osd_ah_init,
2378 .do_create = osd_object_create,
2379 .do_index_try = osd_index_try,
2380 .do_ref_add = osd_object_ref_add,
2381 .do_ref_del = osd_object_ref_del,
2382 .do_xattr_get = osd_xattr_get,
2383 .do_xattr_set = osd_xattr_set,
2384 .do_xattr_del = osd_xattr_del,
2385 .do_xattr_list = osd_xattr_list,
2386 .do_capa_get = osd_capa_get,
2387 .do_object_sync = osd_object_sync,
2388 .do_version_get = osd_object_version_get,
2389 .do_version_set = osd_object_version_set,
2390 .do_data_get = osd_data_get,
2394 * dt_object_operations for interoperability mode
2395 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2397 static const struct dt_object_operations osd_obj_ea_ops = {
2398 .do_read_lock = osd_object_read_lock,
2399 .do_write_lock = osd_object_write_lock,
2400 .do_read_unlock = osd_object_read_unlock,
2401 .do_write_unlock = osd_object_write_unlock,
2402 .do_write_locked = osd_object_write_locked,
2403 .do_attr_get = osd_attr_get,
2404 .do_attr_set = osd_attr_set,
2405 .do_ah_init = osd_ah_init,
2406 .do_create = osd_object_ea_create,
2407 .do_index_try = osd_index_try,
2408 .do_ref_add = osd_object_ref_add,
2409 .do_ref_del = osd_object_ref_del,
2410 .do_xattr_get = osd_xattr_get,
2411 .do_xattr_set = osd_xattr_set,
2412 .do_xattr_del = osd_xattr_del,
2413 .do_xattr_list = osd_xattr_list,
2414 .do_capa_get = osd_capa_get,
2415 .do_object_sync = osd_object_sync,
2416 .do_version_get = osd_object_version_get,
2417 .do_version_set = osd_object_version_set,
2418 .do_data_get = osd_data_get,
2426 * XXX: Another layering violation for now.
2428 * We don't want to use ->f_op->read methods, because generic file write
2430 * - serializes on ->i_sem, and
2432 * - does a lot of extra work like balance_dirty_pages(),
2434 * which doesn't work for globally shared files like /last-received.
2436 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2438 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2440 memcpy(buffer, (char*)ei->i_data, buflen);
2445 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2448 struct buffer_head *bh;
2449 unsigned long block;
2456 /* prevent reading after eof */
2457 spin_lock(&inode->i_lock);
2458 if (i_size_read(inode) < *offs + size) {
2459 size = i_size_read(inode) - *offs;
2460 spin_unlock(&inode->i_lock);
2462 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2463 i_size_read(inode), *offs);
2465 } else if (size == 0) {
2469 spin_unlock(&inode->i_lock);
2472 blocksize = 1 << inode->i_blkbits;
2475 block = *offs >> inode->i_blkbits;
2476 boffs = *offs & (blocksize - 1);
2477 csize = min(blocksize - boffs, size);
2478 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2480 CERROR("can't read block: %d\n", err);
2484 memcpy(buf, bh->b_data + boffs, csize);
2494 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2495 struct lu_buf *buf, loff_t *pos,
2496 struct lustre_capa *capa)
2498 struct osd_object *obj = osd_dt_obj(dt);
2499 struct inode *inode = obj->oo_inode;
2502 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2505 /* Read small symlink from inode body as we need to maintain correct
2506 * on-disk symlinks for ldiskfs.
2508 if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2509 (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2510 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2512 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2517 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2520 memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2522 LDISKFS_I(inode)->i_disksize = buflen;
2523 i_size_write(inode, buflen);
2524 inode->i_sb->s_op->dirty_inode(inode);
2529 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2530 loff_t *offs, handle_t *handle)
2532 struct buffer_head *bh = NULL;
2533 loff_t offset = *offs;
2534 loff_t new_size = i_size_read(inode);
2535 unsigned long block;
2536 int blocksize = 1 << inode->i_blkbits;
2540 int dirty_inode = 0;
2542 while (bufsize > 0) {
2546 block = offset >> inode->i_blkbits;
2547 boffs = offset & (blocksize - 1);
2548 size = min(blocksize - boffs, bufsize);
2549 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2551 CERROR("can't read/create block: %d\n", err);
2555 err = ldiskfs_journal_get_write_access(handle, bh);
2557 CERROR("journal_get_write_access() returned error %d\n",
2561 LASSERTF(boffs + size <= bh->b_size,
2562 "boffs %d size %d bh->b_size %lu",
2563 boffs, size, (unsigned long)bh->b_size);
2564 memcpy(bh->b_data + boffs, buf, size);
2565 err = ldiskfs_journal_dirty_metadata(handle, bh);
2569 if (offset + size > new_size)
2570 new_size = offset + size;
2578 /* correct in-core and on-disk sizes */
2579 if (new_size > i_size_read(inode)) {
2580 spin_lock(&inode->i_lock);
2581 if (new_size > i_size_read(inode))
2582 i_size_write(inode, new_size);
2583 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
2584 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
2587 spin_unlock(&inode->i_lock);
2589 inode->i_sb->s_op->dirty_inode(inode);
2597 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2598 const struct lu_buf *buf, loff_t *pos,
2599 struct thandle *handle, struct lustre_capa *capa,
2602 struct osd_object *obj = osd_dt_obj(dt);
2603 struct inode *inode = obj->oo_inode;
2604 struct osd_thandle *oh;
2606 #ifdef HAVE_QUOTA_SUPPORT
2607 cfs_cap_t save = current->cap_effective;
2610 LASSERT(handle != NULL);
2612 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2615 oh = container_of(handle, struct osd_thandle, ot_super);
2616 LASSERT(oh->ot_handle->h_transaction != NULL);
2617 #ifdef HAVE_QUOTA_SUPPORT
2619 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2621 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2623 /* Write small symlink to inode body as we need to maintain correct
2624 * on-disk symlinks for ldiskfs.
2626 if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2627 (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
2628 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2630 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
2633 #ifdef HAVE_QUOTA_SUPPORT
2634 current->cap_effective = save;
2637 result = buf->lb_len;
2641 static const struct dt_body_operations osd_body_ops = {
2642 .dbo_read = osd_read,
2643 .dbo_write = osd_write
2648 * delete a (key, value) pair from index \a dt specified by \a key
2650 * \param dt osd index object
2651 * \param key key for index
2652 * \param rec record reference
2653 * \param handle transaction handler
2656 * \retval -ve failure
2659 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2660 const struct dt_key *key, struct thandle *handle,
2661 struct lustre_capa *capa)
2663 struct osd_object *obj = osd_dt_obj(dt);
2664 struct osd_thandle *oh;
2665 struct iam_path_descr *ipd;
2666 struct iam_container *bag = &obj->oo_dir->od_container;
2671 LINVRNT(osd_invariant(obj));
2672 LASSERT(dt_object_exists(dt));
2673 LASSERT(bag->ic_object == obj->oo_inode);
2674 LASSERT(handle != NULL);
2676 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2679 ipd = osd_idx_ipd_get(env, bag);
2680 if (unlikely(ipd == NULL))
2683 oh = container_of0(handle, struct osd_thandle, ot_super);
2684 LASSERT(oh->ot_handle != NULL);
2685 LASSERT(oh->ot_handle->h_transaction != NULL);
2687 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2688 osd_ipd_put(env, bag, ipd);
2689 LINVRNT(osd_invariant(obj));
2693 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2696 struct osd_fid_pack *rec;
2699 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2700 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2701 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2707 * Index delete function for interoperability mode (b11826).
2708 * It will remove the directory entry added by osd_index_ea_insert().
2709 * This entry is needed to maintain name->fid mapping.
2711 * \param key, key i.e. file entry to be deleted
2713 * \retval 0, on success
2714 * \retval -ve, on error
2716 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2717 const struct dt_key *key, struct thandle *handle,
2718 struct lustre_capa *capa)
2720 struct osd_object *obj = osd_dt_obj(dt);
2721 struct inode *dir = obj->oo_inode;
2722 struct dentry *dentry;
2723 struct osd_thandle *oh;
2724 struct ldiskfs_dir_entry_2 *de;
2725 struct buffer_head *bh;
2731 LINVRNT(osd_invariant(obj));
2732 LASSERT(dt_object_exists(dt));
2733 LASSERT(handle != NULL);
2735 oh = container_of(handle, struct osd_thandle, ot_super);
2736 LASSERT(oh->ot_handle != NULL);
2737 LASSERT(oh->ot_handle->h_transaction != NULL);
2739 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2742 dentry = osd_child_dentry_get(env, obj,
2743 (char *)key, strlen((char *)key));
2745 cfs_down_write(&obj->oo_ext_idx_sem);
2746 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
2748 struct osd_thread_info *oti = osd_oti_get(env);
2749 struct timespec *ctime = &oti->oti_time;
2750 struct timespec *mtime = &oti->oti_time2;
2752 *ctime = dir->i_ctime;
2753 *mtime = dir->i_mtime;
2754 rc = ldiskfs_delete_entry(oh->ot_handle,
2756 /* xtime should not be updated with server-side time. */
2757 cfs_spin_lock(&obj->oo_guard);
2758 dir->i_ctime = *ctime;
2759 dir->i_mtime = *mtime;
2760 cfs_spin_unlock(&obj->oo_guard);
2761 mark_inode_dirty(dir);
2766 cfs_up_write(&obj->oo_ext_idx_sem);
2767 LASSERT(osd_invariant(obj));
2772 * Lookup index for \a key and copy record to \a rec.
2774 * \param dt osd index object
2775 * \param key key for index
2776 * \param rec record reference
2778 * \retval +ve success : exact mach
2779 * \retval 0 return record with key not greater than \a key
2780 * \retval -ve failure
2782 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2783 struct dt_rec *rec, const struct dt_key *key,
2784 struct lustre_capa *capa)
2786 struct osd_object *obj = osd_dt_obj(dt);
2787 struct iam_path_descr *ipd;
2788 struct iam_container *bag = &obj->oo_dir->od_container;
2789 struct osd_thread_info *oti = osd_oti_get(env);
2790 struct iam_iterator *it = &oti->oti_idx_it;
2791 struct iam_rec *iam_rec;
2795 LASSERT(osd_invariant(obj));
2796 LASSERT(dt_object_exists(dt));
2797 LASSERT(bag->ic_object == obj->oo_inode);
2799 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2802 ipd = osd_idx_ipd_get(env, bag);
2806 /* got ipd now we can start iterator. */
2807 iam_it_init(it, bag, 0, ipd);
2809 rc = iam_it_get(it, (struct iam_key *)key);
2811 if (S_ISDIR(obj->oo_inode->i_mode))
2812 iam_rec = (struct iam_rec *)oti->oti_ldp;
2814 iam_rec = (struct iam_rec *) rec;
2816 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2817 if (S_ISDIR(obj->oo_inode->i_mode))
2818 osd_fid_unpack((struct lu_fid *) rec,
2819 (struct osd_fid_pack *)iam_rec);
2823 osd_ipd_put(env, bag, ipd);
2825 LINVRNT(osd_invariant(obj));
2831 * Inserts (key, value) pair in \a dt index object.
2833 * \param dt osd index object
2834 * \param key key for index
2835 * \param rec record reference
2836 * \param th transaction handler
2839 * \retval -ve failure
2841 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2842 const struct dt_rec *rec, const struct dt_key *key,
2843 struct thandle *th, struct lustre_capa *capa,
2846 struct osd_object *obj = osd_dt_obj(dt);
2847 struct iam_path_descr *ipd;
2848 struct osd_thandle *oh;
2849 struct iam_container *bag = &obj->oo_dir->od_container;
2850 #ifdef HAVE_QUOTA_SUPPORT
2851 cfs_cap_t save = current->cap_effective;
2853 struct osd_thread_info *oti = osd_oti_get(env);
2854 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2859 LINVRNT(osd_invariant(obj));
2860 LASSERT(dt_object_exists(dt));
2861 LASSERT(bag->ic_object == obj->oo_inode);
2862 LASSERT(th != NULL);
2864 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2867 ipd = osd_idx_ipd_get(env, bag);
2868 if (unlikely(ipd == NULL))
2871 oh = container_of0(th, struct osd_thandle, ot_super);
2872 LASSERT(oh->ot_handle != NULL);
2873 LASSERT(oh->ot_handle->h_transaction != NULL);
2874 #ifdef HAVE_QUOTA_SUPPORT
2876 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2878 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2880 if (S_ISDIR(obj->oo_inode->i_mode))
2881 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2883 iam_rec = (struct iam_rec *) rec;
2884 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2886 #ifdef HAVE_QUOTA_SUPPORT
2887 current->cap_effective = save;
2889 osd_ipd_put(env, bag, ipd);
2890 LINVRNT(osd_invariant(obj));
2895 * Calls ldiskfs_add_entry() to add directory entry
2896 * into the directory. This is required for
2897 * interoperability mode (b11826)
2899 * \retval 0, on success
2900 * \retval -ve, on error
2902 static int __osd_ea_add_rec(struct osd_thread_info *info,
2903 struct osd_object *pobj,
2904 struct inode *cinode,
2906 const struct dt_rec *fid,
2909 struct ldiskfs_dentry_param *ldp;
2910 struct dentry *child;
2911 struct osd_thandle *oth;
2914 oth = container_of(th, struct osd_thandle, ot_super);
2915 LASSERT(oth->ot_handle != NULL);
2916 LASSERT(oth->ot_handle->h_transaction != NULL);
2918 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2920 if (fid_is_igif((struct lu_fid *)fid) ||
2921 fid_seq((struct lu_fid *)fid) >= FID_SEQ_DISTRIBUTED_START) {
2922 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2923 osd_get_ldiskfs_dirent_param(ldp, fid);
2924 child->d_fsdata = (void*) ldp;
2926 child->d_fsdata = NULL;
2927 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2933 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2934 * into the directory.Also sets flags into osd object to
2935 * indicate dot and dotdot are created. This is required for
2936 * interoperability mode (b11826)
2938 * \param dir directory for dot and dotdot fixup.
2939 * \param obj child object for linking
2941 * \retval 0, on success
2942 * \retval -ve, on error
2944 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2945 struct osd_object *dir,
2946 struct inode *parent_dir, const char *name,
2947 const struct dt_rec *dot_fid,
2948 const struct dt_rec *dot_dot_fid,
2951 struct inode *inode = dir->oo_inode;
2952 struct ldiskfs_dentry_param *dot_ldp;
2953 struct ldiskfs_dentry_param *dot_dot_ldp;
2954 struct osd_thandle *oth;
2957 oth = container_of(th, struct osd_thandle, ot_super);
2958 LASSERT(oth->ot_handle->h_transaction != NULL);
2959 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2961 if (strcmp(name, dot) == 0) {
2962 if (dir->oo_compat_dot_created) {
2965 LASSERT(inode == parent_dir);
2966 dir->oo_compat_dot_created = 1;
2969 } else if(strcmp(name, dotdot) == 0) {
2970 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2971 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2973 if (!dir->oo_compat_dot_created)
2975 if (fid_seq((struct lu_fid *) dot_fid) >= FID_SEQ_DISTRIBUTED_START) {
2976 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
2977 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2982 /* in case of rename, dotdot is already created */
2983 if (dir->oo_compat_dotdot_created) {
2984 return __osd_ea_add_rec(info, dir, parent_dir, name,
2988 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode,
2989 dot_ldp, dot_dot_ldp);
2991 dir->oo_compat_dotdot_created = 1;
2999 * It will call the appropriate osd_add* function and return the
3000 * value, return by respective functions.
3002 static int osd_ea_add_rec(const struct lu_env *env,
3003 struct osd_object *pobj,
3004 struct inode *cinode,
3006 const struct dt_rec *fid,
3009 struct osd_thread_info *info = osd_oti_get(env);
3012 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3014 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3015 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3018 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid, th);
3024 * Calls ->lookup() to find dentry. From dentry get inode and
3025 * read inode's ea to get fid. This is required for interoperability
3028 * \retval 0, on success
3029 * \retval -ve, on error
3031 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3032 struct dt_rec *rec, const struct dt_key *key)
3034 struct inode *dir = obj->oo_inode;
3035 struct dentry *dentry;
3036 struct ldiskfs_dir_entry_2 *de;
3037 struct buffer_head *bh;
3038 struct lu_fid *fid = (struct lu_fid *) rec;
3042 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3044 dentry = osd_child_dentry_get(env, obj,
3045 (char *)key, strlen((char *)key));
3047 cfs_down_read(&obj->oo_ext_idx_sem);
3048 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
3050 ino = le32_to_cpu(de->inode);
3051 rc = osd_get_fid_from_dentry(de, rec);
3053 /* done with de, release bh */
3056 rc = osd_ea_fid_get(env, obj, ino, fid);
3060 cfs_up_read(&obj->oo_ext_idx_sem);
3065 * Find the osd object for given fid.
3067 * \param fid need to find the osd object having this fid
3069 * \retval osd_object on success
3070 * \retval -ve on error
3072 struct osd_object *osd_object_find(const struct lu_env *env,
3073 struct dt_object *dt,
3074 const struct lu_fid *fid)
3076 struct lu_device *ludev = dt->do_lu.lo_dev;
3077 struct osd_object *child = NULL;
3078 struct lu_object *luch;
3079 struct lu_object *lo;
3081 luch = lu_object_find(env, ludev, fid, NULL);
3082 if (!IS_ERR(luch)) {
3083 if (lu_object_exists(luch)) {
3084 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3086 child = osd_obj(lo);
3088 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3089 "lu_object can't be located"
3090 ""DFID"\n", PFID(fid));
3092 if (child == NULL) {
3093 lu_object_put(env, luch);
3094 CERROR("Unable to get osd_object\n");
3095 child = ERR_PTR(-ENOENT);
3098 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3099 "lu_object does not exists "DFID"\n",
3101 child = ERR_PTR(-ENOENT);
3104 child = (void *)luch;
3110 * Put the osd object once done with it.
3112 * \param obj osd object that needs to be put
3114 static inline void osd_object_put(const struct lu_env *env,
3115 struct osd_object *obj)
3117 lu_object_put(env, &obj->oo_dt.do_lu);
3121 * Index add function for interoperability mode (b11826).
3122 * It will add the directory entry.This entry is needed to
3123 * maintain name->fid mapping.
3125 * \param key it is key i.e. file entry to be inserted
3126 * \param rec it is value of given key i.e. fid
3128 * \retval 0, on success
3129 * \retval -ve, on error
3131 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3132 const struct dt_rec *rec,
3133 const struct dt_key *key, struct thandle *th,
3134 struct lustre_capa *capa, int ignore_quota)
3136 struct osd_object *obj = osd_dt_obj(dt);
3137 struct lu_fid *fid = (struct lu_fid *) rec;
3138 const char *name = (const char *)key;
3139 struct osd_object *child;
3140 #ifdef HAVE_QUOTA_SUPPORT
3141 cfs_cap_t save = current->cap_effective;
3147 LASSERT(osd_invariant(obj));
3148 LASSERT(dt_object_exists(dt));
3149 LASSERT(th != NULL);
3151 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3154 child = osd_object_find(env, dt, fid);
3155 if (!IS_ERR(child)) {
3156 struct inode *inode = obj->oo_inode;
3157 struct osd_thread_info *oti = osd_oti_get(env);
3158 struct timespec *ctime = &oti->oti_time;
3159 struct timespec *mtime = &oti->oti_time2;
3161 *ctime = inode->i_ctime;
3162 *mtime = inode->i_mtime;
3163 #ifdef HAVE_QUOTA_SUPPORT
3165 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
3167 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
3169 cfs_down_write(&obj->oo_ext_idx_sem);
3170 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3171 cfs_up_write(&obj->oo_ext_idx_sem);
3172 #ifdef HAVE_QUOTA_SUPPORT
3173 current->cap_effective = save;
3175 osd_object_put(env, child);
3176 /* xtime should not be updated with server-side time. */
3177 cfs_spin_lock(&obj->oo_guard);
3178 inode->i_ctime = *ctime;
3179 inode->i_mtime = *mtime;
3180 cfs_spin_unlock(&obj->oo_guard);
3181 mark_inode_dirty(inode);
3183 rc = PTR_ERR(child);
3186 LASSERT(osd_invariant(obj));
3191 * Initialize osd Iterator for given osd index object.
3193 * \param dt osd index object
3196 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3197 struct dt_object *dt,
3198 struct lustre_capa *capa)
3200 struct osd_it_iam *it;
3201 struct osd_thread_info *oti = osd_oti_get(env);
3202 struct osd_object *obj = osd_dt_obj(dt);
3203 struct lu_object *lo = &dt->do_lu;
3204 struct iam_path_descr *ipd;
3205 struct iam_container *bag = &obj->oo_dir->od_container;
3207 LASSERT(lu_object_exists(lo));
3209 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3210 return ERR_PTR(-EACCES);
3213 ipd = osd_it_ipd_get(env, bag);
3214 if (likely(ipd != NULL)) {
3218 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3219 return (struct dt_it *)it;
3221 return ERR_PTR(-ENOMEM);
3225 * free given Iterator.
3228 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3230 struct osd_it_iam *it = (struct osd_it_iam *)di;
3231 struct osd_object *obj = it->oi_obj;
3233 iam_it_fini(&it->oi_it);
3234 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3235 lu_object_put(env, &obj->oo_dt.do_lu);
3239 * Move Iterator to record specified by \a key
3241 * \param di osd iterator
3242 * \param key key for index
3244 * \retval +ve di points to record with least key not larger than key
3245 * \retval 0 di points to exact matched key
3246 * \retval -ve failure
3249 static int osd_it_iam_get(const struct lu_env *env,
3250 struct dt_it *di, const struct dt_key *key)
3252 struct osd_it_iam *it = (struct osd_it_iam *)di;
3254 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3260 * \param di osd iterator
3263 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3265 struct osd_it_iam *it = (struct osd_it_iam *)di;
3267 iam_it_put(&it->oi_it);
3271 * Move iterator by one record
3273 * \param di osd iterator
3275 * \retval +1 end of container reached
3277 * \retval -ve failure
3280 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3282 struct osd_it_iam *it = (struct osd_it_iam *)di;
3284 return iam_it_next(&it->oi_it);
3288 * Return pointer to the key under iterator.
3291 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3292 const struct dt_it *di)
3294 struct osd_it_iam *it = (struct osd_it_iam *)di;
3296 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3300 * Return size of key under iterator (in bytes)
3303 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3305 struct osd_it_iam *it = (struct osd_it_iam *)di;
3307 return iam_it_key_size(&it->oi_it);
3310 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3315 struct luda_type *lt;
3316 const unsigned align = sizeof(struct luda_type) - 1;
3318 /* check if file type is required */
3319 if (attr & LUDA_TYPE) {
3320 len = (len + align) & ~align;
3322 lt = (void *) ent->lde_name + len;
3323 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3324 ent->lde_attrs |= LUDA_TYPE;
3327 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3331 * build lu direct from backend fs dirent.
3334 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3342 fid_cpu_to_le(&ent->lde_fid, fid);
3343 ent->lde_attrs = LUDA_FID;
3345 ent->lde_hash = cpu_to_le64(offset);
3346 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3348 strncpy(ent->lde_name, name, namelen);
3349 ent->lde_namelen = cpu_to_le16(namelen);
3351 /* append lustre attributes */
3352 osd_it_append_attrs(ent, attr, namelen, type);
3356 * Return pointer to the record under iterator.
3358 static int osd_it_iam_rec(const struct lu_env *env,
3359 const struct dt_it *di,
3360 struct lu_dirent *lde,
3363 struct osd_it_iam *it = (struct osd_it_iam *)di;
3364 struct osd_thread_info *info = osd_oti_get(env);
3365 struct lu_fid *fid = &info->oti_fid;
3366 const struct osd_fid_pack *rec;
3372 name = (char *)iam_it_key_get(&it->oi_it);
3374 RETURN(PTR_ERR(name));
3376 namelen = iam_it_key_size(&it->oi_it);
3378 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3380 RETURN(PTR_ERR(rec));
3382 rc = osd_fid_unpack(fid, rec);
3386 hash = iam_it_store(&it->oi_it);
3388 /* IAM does not store object type in IAM index (dir) */
3389 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3396 * Returns cookie for current Iterator position.
3398 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3400 struct osd_it_iam *it = (struct osd_it_iam *)di;
3402 return iam_it_store(&it->oi_it);
3406 * Restore iterator from cookie.
3408 * \param di osd iterator
3409 * \param hash Iterator location cookie
3411 * \retval +ve di points to record with least key not larger than key.
3412 * \retval 0 di points to exact matched key
3413 * \retval -ve failure
3416 static int osd_it_iam_load(const struct lu_env *env,
3417 const struct dt_it *di, __u64 hash)
3419 struct osd_it_iam *it = (struct osd_it_iam *)di;
3421 return iam_it_load(&it->oi_it, hash);
3424 static const struct dt_index_operations osd_index_iam_ops = {
3425 .dio_lookup = osd_index_iam_lookup,
3426 .dio_insert = osd_index_iam_insert,
3427 .dio_delete = osd_index_iam_delete,
3429 .init = osd_it_iam_init,
3430 .fini = osd_it_iam_fini,
3431 .get = osd_it_iam_get,
3432 .put = osd_it_iam_put,
3433 .next = osd_it_iam_next,
3434 .key = osd_it_iam_key,
3435 .key_size = osd_it_iam_key_size,
3436 .rec = osd_it_iam_rec,
3437 .store = osd_it_iam_store,
3438 .load = osd_it_iam_load
3443 * Creates or initializes iterator context.
3445 * \retval struct osd_it_ea, iterator structure on success
3448 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3449 struct dt_object *dt,
3450 struct lustre_capa *capa)
3452 struct osd_object *obj = osd_dt_obj(dt);
3453 struct osd_thread_info *info = osd_oti_get(env);
3454 struct osd_it_ea *it = &info->oti_it_ea;
3455 struct lu_object *lo = &dt->do_lu;
3456 struct dentry *obj_dentry = &info->oti_it_dentry;
3458 LASSERT(lu_object_exists(lo));
3460 obj_dentry->d_inode = obj->oo_inode;
3461 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3462 obj_dentry->d_name.hash = 0;
3464 it->oie_rd_dirent = 0;
3465 it->oie_it_dirent = 0;
3466 it->oie_dirent = NULL;
3467 it->oie_buf = info->oti_it_ea_buf;
3469 it->oie_file.f_pos = 0;
3470 it->oie_file.f_dentry = obj_dentry;
3471 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3472 it->oie_file.f_op = obj->oo_inode->i_fop;
3473 it->oie_file.private_data = NULL;
3475 RETURN((struct dt_it *) it);
3479 * Destroy or finishes iterator context.
3481 * \param di iterator structure to be destroyed
3483 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3485 struct osd_it_ea *it = (struct osd_it_ea *)di;
3486 struct osd_object *obj = it->oie_obj;
3487 struct inode *inode = obj->oo_inode;
3490 it->oie_file.f_op->release(inode, &it->oie_file);
3491 lu_object_put(env, &obj->oo_dt.do_lu);
3496 * It position the iterator at given key, so that next lookup continues from
3497 * that key Or it is similar to dio_it->load() but based on a key,
3498 * rather than file position.
3500 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3503 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3505 static int osd_it_ea_get(const struct lu_env *env,
3506 struct dt_it *di, const struct dt_key *key)
3508 struct osd_it_ea *it = (struct osd_it_ea *)di;
3511 LASSERT(((const char *)key)[0] == '\0');
3512 it->oie_file.f_pos = 0;
3513 it->oie_rd_dirent = 0;
3514 it->oie_it_dirent = 0;
3515 it->oie_dirent = NULL;
3523 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3528 * It is called internally by ->readdir(). It fills the
3529 * iterator's in-memory data structure with required
3530 * information i.e. name, namelen, rec_size etc.
3532 * \param buf in which information to be filled in.
3533 * \param name name of the file in given dir
3535 * \retval 0 on success
3536 * \retval 1 on buffer full
3538 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3539 loff_t offset, __u64 ino,
3542 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3543 struct osd_it_ea_dirent *ent = it->oie_dirent;
3544 struct lu_fid *fid = &ent->oied_fid;
3545 struct osd_fid_pack *rec;
3548 /* this should never happen */
3549 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3550 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3554 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3558 if (d_type & LDISKFS_DIRENT_LUFID) {
3559 rec = (struct osd_fid_pack*) (name + namelen + 1);
3561 if (osd_fid_unpack(fid, rec) != 0)
3564 d_type &= ~LDISKFS_DIRENT_LUFID;
3569 ent->oied_ino = ino;
3570 ent->oied_off = offset;
3571 ent->oied_namelen = namelen;
3572 ent->oied_type = d_type;
3574 memcpy(ent->oied_name, name, namelen);
3576 it->oie_rd_dirent++;
3577 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3582 * Calls ->readdir() to load a directory entry at a time
3583 * and stored it in iterator's in-memory data structure.
3585 * \param di iterator's in memory structure
3587 * \retval 0 on success
3588 * \retval -ve on error
3590 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3592 struct osd_it_ea *it = (struct osd_it_ea *)di;
3593 struct osd_object *obj = it->oie_obj;
3594 struct inode *inode = obj->oo_inode;
3598 it->oie_dirent = it->oie_buf;
3599 it->oie_rd_dirent = 0;
3601 cfs_down_read(&obj->oo_ext_idx_sem);
3602 result = inode->i_fop->readdir(&it->oie_file, it,
3603 (filldir_t) osd_ldiskfs_filldir);
3605 cfs_up_read(&obj->oo_ext_idx_sem);
3607 if (it->oie_rd_dirent == 0) {
3610 it->oie_dirent = it->oie_buf;
3611 it->oie_it_dirent = 1;
3618 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3619 * to load a directory entry at a time and stored it in
3620 * iterator's in-memory data structure.
3622 * \param di iterator's in memory structure
3624 * \retval +ve iterator reached to end
3625 * \retval 0 iterator not reached to end
3626 * \retval -ve on error
3628 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3630 struct osd_it_ea *it = (struct osd_it_ea *)di;
3635 if (it->oie_it_dirent < it->oie_rd_dirent) {
3637 (void *) it->oie_dirent +
3638 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3639 it->oie_dirent->oied_namelen);
3640 it->oie_it_dirent++;
3643 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3646 rc = osd_ldiskfs_it_fill(di);
3653 * Returns the key at current position from iterator's in memory structure.
3655 * \param di iterator's in memory structure
3657 * \retval key i.e. struct dt_key on success
3659 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3660 const struct dt_it *di)
3662 struct osd_it_ea *it = (struct osd_it_ea *)di;
3664 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3668 * Returns the key's size at current position from iterator's in memory structure.
3670 * \param di iterator's in memory structure
3672 * \retval key_size i.e. struct dt_key on success
3674 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3676 struct osd_it_ea *it = (struct osd_it_ea *)di;
3678 RETURN(it->oie_dirent->oied_namelen);
3683 * Returns the value (i.e. fid/igif) at current position from iterator's
3684 * in memory structure.
3686 * \param di struct osd_it_ea, iterator's in memory structure
3687 * \param attr attr requested for dirent.
3688 * \param lde lustre dirent
3690 * \retval 0 no error and \param lde has correct lustre dirent.
3691 * \retval -ve on error
3693 static inline int osd_it_ea_rec(const struct lu_env *env,
3694 const struct dt_it *di,
3695 struct lu_dirent *lde,
3698 struct osd_it_ea *it = (struct osd_it_ea *)di;
3699 struct osd_object *obj = it->oie_obj;
3700 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3705 if (!fid_is_sane(fid))
3706 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3709 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3710 it->oie_dirent->oied_name,
3711 it->oie_dirent->oied_namelen,
3712 it->oie_dirent->oied_type,
3718 * Returns a cookie for current position of the iterator head, so that
3719 * user can use this cookie to load/start the iterator next time.
3721 * \param di iterator's in memory structure
3723 * \retval cookie for current position, on success
3725 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3727 struct osd_it_ea *it = (struct osd_it_ea *)di;
3729 RETURN(it->oie_dirent->oied_off);
3733 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3734 * to load a directory entry at a time and stored it i inn,
3735 * in iterator's in-memory data structure.
3737 * \param di struct osd_it_ea, iterator's in memory structure
3739 * \retval +ve on success
3740 * \retval -ve on error
3742 static int osd_it_ea_load(const struct lu_env *env,
3743 const struct dt_it *di, __u64 hash)
3745 struct osd_it_ea *it = (struct osd_it_ea *)di;
3749 it->oie_file.f_pos = hash;
3751 rc = osd_ldiskfs_it_fill(di);
3759 * Index lookup function for interoperability mode (b11826).
3761 * \param key, key i.e. file name to be searched
3763 * \retval +ve, on success
3764 * \retval -ve, on error
3766 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3767 struct dt_rec *rec, const struct dt_key *key,
3768 struct lustre_capa *capa)
3770 struct osd_object *obj = osd_dt_obj(dt);
3775 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3776 LINVRNT(osd_invariant(obj));
3778 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3781 rc = osd_ea_lookup_rec(env, obj, rec, key);
3789 * Index and Iterator operations for interoperability
3790 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3792 static const struct dt_index_operations osd_index_ea_ops = {
3793 .dio_lookup = osd_index_ea_lookup,
3794 .dio_insert = osd_index_ea_insert,
3795 .dio_delete = osd_index_ea_delete,
3797 .init = osd_it_ea_init,
3798 .fini = osd_it_ea_fini,
3799 .get = osd_it_ea_get,
3800 .put = osd_it_ea_put,
3801 .next = osd_it_ea_next,
3802 .key = osd_it_ea_key,
3803 .key_size = osd_it_ea_key_size,
3804 .rec = osd_it_ea_rec,
3805 .store = osd_it_ea_store,
3806 .load = osd_it_ea_load
3810 static void *osd_key_init(const struct lu_context *ctx,
3811 struct lu_context_key *key)
3813 struct osd_thread_info *info;
3815 OBD_ALLOC_PTR(info);
3817 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3818 if (info->oti_it_ea_buf != NULL) {
3819 info->oti_env = container_of(ctx, struct lu_env,
3823 info = ERR_PTR(-ENOMEM);
3826 info = ERR_PTR(-ENOMEM);
3831 static void osd_key_fini(const struct lu_context *ctx,
3832 struct lu_context_key *key, void* data)
3834 struct osd_thread_info *info = data;
3836 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3840 static void osd_key_exit(const struct lu_context *ctx,
3841 struct lu_context_key *key, void *data)
3843 struct osd_thread_info *info = data;
3845 LASSERT(info->oti_r_locks == 0);
3846 LASSERT(info->oti_w_locks == 0);
3847 LASSERT(info->oti_txns == 0);
3850 /* type constructor/destructor: osd_type_init, osd_type_fini */
3851 LU_TYPE_INIT_FINI(osd, &osd_key);
3853 static struct lu_context_key osd_key = {
3854 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3855 .lct_init = osd_key_init,
3856 .lct_fini = osd_key_fini,
3857 .lct_exit = osd_key_exit
3861 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3862 const char *name, struct lu_device *next)
3865 struct lu_context *ctx;
3867 /* context for commit hooks */
3868 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3869 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3871 rc = osd_procfs_init(osd_dev(d), name);
3872 ctx->lc_cookie = 0x3;
3877 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3879 struct osd_thread_info *info = osd_oti_get(env);
3881 if (o->od_obj_area != NULL) {
3882 lu_object_put(env, &o->od_obj_area->do_lu);
3883 o->od_obj_area = NULL;
3885 osd_oi_fini(info, &o->od_oi);
3890 static int osd_mount(const struct lu_env *env,
3891 struct osd_device *o, struct lustre_cfg *cfg)
3893 struct lustre_mount_info *lmi;
3894 const char *dev = lustre_cfg_string(cfg, 0);
3895 struct lustre_disk_data *ldd;
3896 struct lustre_sb_info *lsi;
3899 if (o->od_mount != NULL) {
3900 CERROR("Already mounted (%s)\n", dev);
3905 lmi = server_get_mount(dev);
3907 CERROR("Cannot get mount info for %s!\n", dev);
3911 LASSERT(lmi != NULL);
3912 /* save lustre_mount_info in dt_device */
3915 lsi = s2lsi(lmi->lmi_sb);
3918 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3920 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3924 o->od_obj_area = NULL;
3928 static struct lu_device *osd_device_fini(const struct lu_env *env,
3929 struct lu_device *d)
3934 shrink_dcache_sb(osd_sb(osd_dev(d)));
3935 osd_sync(env, lu2dt_dev(d));
3937 rc = osd_procfs_fini(osd_dev(d));
3939 CERROR("proc fini error %d \n", rc);
3940 RETURN (ERR_PTR(rc));
3943 if (osd_dev(d)->od_mount)
3944 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3945 osd_dev(d)->od_mount->lmi_mnt);
3946 osd_dev(d)->od_mount = NULL;
3948 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3952 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3953 struct lu_device_type *t,
3954 struct lustre_cfg *cfg)
3956 struct lu_device *l;
3957 struct osd_device *o;
3963 result = dt_device_init(&o->od_dt_dev, t);
3966 l->ld_ops = &osd_lu_ops;
3967 o->od_dt_dev.dd_ops = &osd_dt_ops;
3968 cfs_spin_lock_init(&o->od_osfs_lock);
3969 o->od_osfs_age = cfs_time_shift_64(-1000);
3970 o->od_capa_hash = init_capa_hash();
3971 if (o->od_capa_hash == NULL) {
3972 dt_device_fini(&o->od_dt_dev);
3973 l = ERR_PTR(-ENOMEM);
3976 l = ERR_PTR(result);
3981 l = ERR_PTR(-ENOMEM);
3985 static struct lu_device *osd_device_free(const struct lu_env *env,
3986 struct lu_device *d)
3988 struct osd_device *o = osd_dev(d);
3991 cleanup_capa_hash(o->od_capa_hash);
3992 dt_device_fini(&o->od_dt_dev);
3997 static int osd_process_config(const struct lu_env *env,
3998 struct lu_device *d, struct lustre_cfg *cfg)
4000 struct osd_device *o = osd_dev(d);
4004 switch(cfg->lcfg_command) {
4006 err = osd_mount(env, o, cfg);
4009 err = osd_shutdown(env, o);
4018 static int osd_recovery_complete(const struct lu_env *env,
4019 struct lu_device *d)
4024 static int osd_prepare(const struct lu_env *env,
4025 struct lu_device *pdev,
4026 struct lu_device *dev)
4028 struct osd_device *osd = osd_dev(dev);
4029 struct lustre_sb_info *lsi;
4030 struct lustre_disk_data *ldd;
4031 struct lustre_mount_info *lmi;
4032 struct osd_thread_info *oti = osd_oti_get(env);
4033 struct dt_object *d;
4037 /* 1. initialize oi before any file create or file open */
4038 result = osd_oi_init(oti, &osd->od_oi,
4039 &osd->od_dt_dev, lu2md_dev(pdev));
4043 lmi = osd->od_mount;
4044 lsi = s2lsi(lmi->lmi_sb);
4047 /* 2. setup local objects */
4048 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4052 /* 3. open remote object dir */
4053 d = dt_store_open(env, lu2dt_dev(dev), "",
4054 remote_obj_dir, &oti->oti_fid);
4056 osd->od_obj_area = d;
4059 result = PTR_ERR(d);
4060 osd->od_obj_area = NULL;
4067 static const struct lu_object_operations osd_lu_obj_ops = {
4068 .loo_object_init = osd_object_init,
4069 .loo_object_delete = osd_object_delete,
4070 .loo_object_release = osd_object_release,
4071 .loo_object_free = osd_object_free,
4072 .loo_object_print = osd_object_print,
4073 .loo_object_invariant = osd_object_invariant
4076 static const struct lu_device_operations osd_lu_ops = {
4077 .ldo_object_alloc = osd_object_alloc,
4078 .ldo_process_config = osd_process_config,
4079 .ldo_recovery_complete = osd_recovery_complete,
4080 .ldo_prepare = osd_prepare,
4083 static const struct lu_device_type_operations osd_device_type_ops = {
4084 .ldto_init = osd_type_init,
4085 .ldto_fini = osd_type_fini,
4087 .ldto_start = osd_type_start,
4088 .ldto_stop = osd_type_stop,
4090 .ldto_device_alloc = osd_device_alloc,
4091 .ldto_device_free = osd_device_free,
4093 .ldto_device_init = osd_device_init,
4094 .ldto_device_fini = osd_device_fini
4097 static struct lu_device_type osd_device_type = {
4098 .ldt_tags = LU_DEVICE_DT,
4099 .ldt_name = LUSTRE_OSD_NAME,
4100 .ldt_ops = &osd_device_type_ops,
4101 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4105 * lprocfs legacy support.
4107 static struct obd_ops osd_obd_device_ops = {
4108 .o_owner = THIS_MODULE
4111 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4112 .llod_name = remote_obj_dir,
4113 .llod_oid = OSD_REM_OBJ_DIR_OID,
4115 .llod_feat = &dt_directory_features,
4118 static int __init osd_mod_init(void)
4120 struct lprocfs_static_vars lvars;
4123 llo_local_obj_register(&llod_osd_rem_obj_dir);
4124 lprocfs_osd_init_vars(&lvars);
4125 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4126 LUSTRE_OSD_NAME, &osd_device_type);
4129 static void __exit osd_mod_exit(void)
4131 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4132 class_unregister_type(LUSTRE_OSD_NAME);
4135 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4136 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4137 MODULE_LICENSE("GPL");
4139 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);