1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * Copyright (c) 2011 Whamcloud, Inc.
39 * This file is part of Lustre, http://www.lustre.org/
40 * Lustre is a trademark of Sun Microsystems, Inc.
42 * lustre/osd/osd_handler.c
44 * Top-level entry points into osd module
46 * Author: Nikita Danilov <nikita@clusterfs.com>
47 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
51 # define EXPORT_SYMTAB
53 #define DEBUG_SUBSYSTEM S_MDS
55 #include <linux/module.h>
57 /* LUSTRE_VERSION_CODE */
58 #include <lustre_ver.h>
59 /* prerequisite for linux/xattr.h */
60 #include <linux/types.h>
61 /* prerequisite for linux/xattr.h */
63 /* XATTR_{REPLACE,CREATE} */
64 #include <linux/xattr.h>
69 * struct OBD_{ALLOC,FREE}*()
72 #include <obd_support.h>
73 /* struct ptlrpc_thread */
74 #include <lustre_net.h>
77 #include <lustre_fid.h>
79 #include "osd_internal.h"
82 /* llo_* api support */
83 #include <md_object.h>
85 static const char dot[] = ".";
86 static const char dotdot[] = "..";
87 static const char remote_obj_dir[] = "REM_OBJ_DIR";
89 struct osd_directory {
90 struct iam_container od_container;
91 struct iam_descr od_descr;
95 struct dt_object oo_dt;
97 * Inode for file system object represented by this osd_object. This
98 * inode is pinned for the whole duration of lu_object life.
100 * Not modified concurrently (either setup early during object
101 * creation, or assigned by osd_object_create() under write lock).
103 struct inode *oo_inode;
105 * to protect index ops.
107 cfs_rw_semaphore_t oo_ext_idx_sem;
108 cfs_rw_semaphore_t oo_sem;
109 struct osd_directory *oo_dir;
110 /** protects inode attributes. */
111 cfs_spinlock_t oo_guard;
113 * Following two members are used to indicate the presence of dot and
114 * dotdot in the given directory. This is required for interop mode
117 int oo_compat_dot_created;
118 int oo_compat_dotdot_created;
120 const struct lu_env *oo_owner;
121 #ifdef CONFIG_LOCKDEP
122 struct lockdep_map oo_dep_map;
126 static const struct lu_object_operations osd_lu_obj_ops;
127 static const struct lu_device_operations osd_lu_ops;
128 static struct lu_context_key osd_key;
129 static const struct dt_object_operations osd_obj_ops;
130 static const struct dt_object_operations osd_obj_ea_ops;
131 static const struct dt_body_operations osd_body_ops;
132 static const struct dt_index_operations osd_index_iam_ops;
133 static const struct dt_index_operations osd_index_ea_ops;
136 struct thandle ot_super;
138 struct journal_callback ot_jcb;
139 /* Link to the device, for debugging. */
140 struct lu_ref_link *ot_dev_link;
142 #if OSD_THANDLE_STATS
143 /** time when this handle was allocated */
144 cfs_time_t oth_alloced;
146 /** time when this thanle was started */
147 cfs_time_t oth_started;
154 static int lu_device_is_osd(const struct lu_device *d)
156 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
159 static struct osd_device *osd_dt_dev(const struct dt_device *d)
161 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
162 return container_of0(d, struct osd_device, od_dt_dev);
165 static struct osd_device *osd_dev(const struct lu_device *d)
167 LASSERT(lu_device_is_osd(d));
168 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
171 static struct osd_device *osd_obj2dev(const struct osd_object *o)
173 return osd_dev(o->oo_dt.do_lu.lo_dev);
176 static struct super_block *osd_sb(const struct osd_device *dev)
178 return dev->od_mount->lmi_mnt->mnt_sb;
181 static int osd_object_is_root(const struct osd_object *obj)
183 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
186 static struct osd_object *osd_obj(const struct lu_object *o)
188 LASSERT(lu_device_is_osd(o->lo_dev));
189 return container_of0(o, struct osd_object, oo_dt.do_lu);
192 static struct osd_object *osd_dt_obj(const struct dt_object *d)
194 return osd_obj(&d->do_lu);
197 static struct lu_device *osd2lu_dev(struct osd_device *osd)
199 return &osd->od_dt_dev.dd_lu_dev;
202 static journal_t *osd_journal(const struct osd_device *dev)
204 return LDISKFS_SB(osd_sb(dev))->s_journal;
207 static int osd_has_index(const struct osd_object *obj)
209 return obj->oo_dt.do_index_ops != NULL;
212 static int osd_object_invariant(const struct lu_object *l)
214 return osd_invariant(osd_obj(l));
217 #ifdef HAVE_QUOTA_SUPPORT
219 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
221 struct md_ucred *uc = md_ucred(env);
226 save->oc_uid = current_fsuid();
227 save->oc_gid = current_fsgid();
228 save->oc_cap = current_cap();
229 if ((tc = prepare_creds())) {
230 tc->fsuid = uc->mu_fsuid;
231 tc->fsgid = uc->mu_fsgid;
234 /* XXX not suboptimal */
235 cfs_curproc_cap_unpack(uc->mu_cap);
239 osd_pop_ctxt(struct osd_ctxt *save)
243 if ((tc = prepare_creds())) {
244 tc->fsuid = save->oc_uid;
245 tc->fsgid = save->oc_gid;
246 tc->cap_effective = save->oc_cap;
252 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
254 return lu_context_key_get(&env->le_ctx, &osd_key);
258 * Concurrency: doesn't matter
260 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
262 return osd_oti_get(env)->oti_r_locks > 0;
266 * Concurrency: doesn't matter
268 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
270 struct osd_thread_info *oti = osd_oti_get(env);
271 return oti->oti_w_locks > 0 && o->oo_owner == env;
275 * Concurrency: doesn't access mutable data
277 static int osd_root_get(const struct lu_env *env,
278 struct dt_device *dev, struct lu_fid *f)
282 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
283 LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
288 * OSD object methods.
292 * Concurrency: no concurrent access is possible that early in object
295 static struct lu_object *osd_object_alloc(const struct lu_env *env,
296 const struct lu_object_header *hdr,
299 struct osd_object *mo;
305 l = &mo->oo_dt.do_lu;
306 dt_object_init(&mo->oo_dt, NULL, d);
307 if (osd_dev(d)->od_iop_mode)
308 mo->oo_dt.do_ops = &osd_obj_ea_ops;
310 mo->oo_dt.do_ops = &osd_obj_ops;
312 l->lo_ops = &osd_lu_obj_ops;
313 cfs_init_rwsem(&mo->oo_sem);
314 cfs_init_rwsem(&mo->oo_ext_idx_sem);
315 cfs_spin_lock_init(&mo->oo_guard);
322 * retrieve object from backend ext fs.
324 static struct inode *osd_iget(struct osd_thread_info *info,
325 struct osd_device *dev,
326 const struct osd_inode_id *id)
328 struct inode *inode = NULL;
330 #ifdef HAVE_EXT4_LDISKFS
331 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
333 /* Newer kernels return an error instead of a NULL pointer */
336 inode = iget(osd_sb(dev), id->oii_ino);
339 CERROR("no inode\n");
340 inode = ERR_PTR(-EACCES);
341 } else if (id->oii_gen != OSD_OII_NOGEN &&
342 inode->i_generation != id->oii_gen) {
344 inode = ERR_PTR(-ESTALE);
345 } else if (inode->i_nlink == 0) {
346 /* due to parallel readdir and unlink,
347 * we can have dead inode here. */
348 CWARN("stale inode\n");
349 make_bad_inode(inode);
351 inode = ERR_PTR(-ESTALE);
352 } else if (is_bad_inode(inode)) {
353 CERROR("bad inode %lx\n",inode->i_ino);
355 inode = ERR_PTR(-ENOENT);
357 /* Do not update file c/mtime in ldiskfs.
358 * NB: we don't have any lock to protect this because we don't
359 * have reference on osd_object now, but contention with
360 * another lookup + attr_set can't happen in the tiny window
361 * between if (...) and set S_NOCMTIME. */
362 if (!(inode->i_flags & S_NOCMTIME))
363 inode->i_flags |= S_NOCMTIME;
368 static int osd_fid_lookup(const struct lu_env *env,
369 struct osd_object *obj, const struct lu_fid *fid)
371 struct osd_thread_info *info;
372 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
373 struct osd_device *dev;
374 struct osd_inode_id *id;
379 LINVRNT(osd_invariant(obj));
380 LASSERT(obj->oo_inode == NULL);
381 LASSERT(fid_is_sane(fid) || osd_fid_is_root(fid));
383 * This assertion checks that osd layer sees only local
384 * fids. Unfortunately it is somewhat expensive (does a
385 * cache-lookup). Disabling it for production/acceptance-testing.
387 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
391 info = osd_oti_get(env);
396 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
399 result = osd_oi_lookup(info, oi, fid, id);
401 inode = osd_iget(info, dev, id);
402 if (!IS_ERR(inode)) {
403 obj->oo_inode = inode;
404 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
405 if (dev->od_iop_mode) {
406 obj->oo_compat_dot_created = 1;
407 obj->oo_compat_dotdot_created = 1;
412 * If fid wasn't found in oi, inode-less object is
413 * created, for which lu_object_exists() returns
414 * false. This is used in a (frequent) case when
415 * objects are created as locking anchors or
416 * place holders for objects yet to be created.
418 result = PTR_ERR(inode);
419 } else if (result == -ENOENT)
421 LINVRNT(osd_invariant(obj));
427 * Concurrency: shouldn't matter.
429 static void osd_object_init0(struct osd_object *obj)
431 LASSERT(obj->oo_inode != NULL);
432 obj->oo_dt.do_body_ops = &osd_body_ops;
433 obj->oo_dt.do_lu.lo_header->loh_attr |=
434 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
438 * Concurrency: no concurrent access is possible that early in object
441 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
442 const struct lu_object_conf *unused)
444 struct osd_object *obj = osd_obj(l);
447 LINVRNT(osd_invariant(obj));
449 result = osd_fid_lookup(env, obj, lu_object_fid(l));
451 if (obj->oo_inode != NULL)
452 osd_object_init0(obj);
454 LINVRNT(osd_invariant(obj));
459 * Concurrency: no concurrent access is possible that late in object
462 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
464 struct osd_object *obj = osd_obj(l);
466 LINVRNT(osd_invariant(obj));
468 dt_object_fini(&obj->oo_dt);
475 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
476 const struct iam_container *bag)
478 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
479 osd_oti_get(env)->oti_it_ipd);
482 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
483 const struct iam_container *bag)
485 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
486 osd_oti_get(env)->oti_idx_ipd);
489 static void osd_ipd_put(const struct lu_env *env,
490 const struct iam_container *bag,
491 struct iam_path_descr *ipd)
493 bag->ic_descr->id_ops->id_ipd_free(ipd);
497 * Concurrency: no concurrent access is possible that late in object
500 static void osd_index_fini(struct osd_object *o)
502 struct iam_container *bag;
504 if (o->oo_dir != NULL) {
505 bag = &o->oo_dir->od_container;
506 if (o->oo_inode != NULL) {
507 if (bag->ic_object == o->oo_inode)
508 iam_container_fini(bag);
510 OBD_FREE_PTR(o->oo_dir);
516 * Concurrency: no concurrent access is possible that late in object
517 * life-cycle (for all existing callers, that is. New callers have to provide
518 * their own locking.)
520 static int osd_inode_unlinked(const struct inode *inode)
522 return inode->i_nlink == 0;
526 OSD_TXN_OI_DELETE_CREDITS = 20,
527 OSD_TXN_INODE_DELETE_CREDITS = 20
534 #if OSD_THANDLE_STATS
536 * Set time when the handle is allocated
538 static void osd_th_alloced(struct osd_thandle *oth)
540 oth->oth_alloced = cfs_time_current();
544 * Set time when the handle started
546 static void osd_th_started(struct osd_thandle *oth)
548 oth->oth_started = cfs_time_current();
552 * Helper function to convert time interval to microseconds packed in
553 * long int (default time units for the counter in "stats" initialized
554 * by lu_time_init() )
556 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
560 cfs_duration_usec(cfs_time_sub(end, start), &val);
561 return val.tv_sec * 1000000 + val.tv_usec;
565 * Check whether the we deal with this handle for too long.
567 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
568 cfs_time_t alloced, cfs_time_t started,
571 cfs_time_t now = cfs_time_current();
573 LASSERT(dev != NULL);
575 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
576 interval_to_usec(alloced, started));
577 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
578 interval_to_usec(started, closed));
579 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
580 interval_to_usec(closed, now));
582 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
583 CWARN("transaction handle %p was open for too long: "
585 "alloced "CFS_TIME_T" ,"
586 "started "CFS_TIME_T" ,"
587 "closed "CFS_TIME_T"\n",
588 oth, now, alloced, started, closed);
589 libcfs_debug_dumpstack(NULL);
593 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
595 cfs_time_t __closed = cfs_time_current(); \
596 cfs_time_t __alloced = oth->oth_alloced; \
597 cfs_time_t __started = oth->oth_started; \
600 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
603 #else /* OSD_THANDLE_STATS */
605 #define osd_th_alloced(h) do {} while(0)
606 #define osd_th_started(h) do {} while(0)
607 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
609 #endif /* OSD_THANDLE_STATS */
612 * Concurrency: doesn't access mutable data.
614 static int osd_param_is_sane(const struct osd_device *dev,
615 const struct txn_param *param)
617 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
621 * Concurrency: shouldn't matter.
623 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
624 static void osd_trans_commit_cb(struct super_block *sb,
625 struct journal_callback *jcb, int error)
627 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
630 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
631 struct thandle *th = &oh->ot_super;
632 struct dt_device *dev = th->th_dev;
633 struct lu_device *lud = &dev->dd_lu_dev;
635 LASSERT(dev != NULL);
636 LASSERT(oh->ot_handle == NULL);
639 CERROR("transaction @0x%p commit error: %d\n", th, error);
641 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
643 * This od_env_for_commit is only for commit usage. see
646 lu_context_enter(&env->le_ctx);
647 dt_txn_hook_commit(env, th);
648 lu_context_exit(&env->le_ctx);
651 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
655 lu_context_exit(&th->th_ctx);
656 lu_context_fini(&th->th_ctx);
661 * Concurrency: shouldn't matter.
663 static struct thandle *osd_trans_start(const struct lu_env *env,
667 struct osd_device *dev = osd_dt_dev(d);
669 struct osd_thandle *oh;
675 hook_res = dt_txn_hook_start(env, d, p);
677 RETURN(ERR_PTR(hook_res));
679 if (osd_param_is_sane(dev, p)) {
680 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
682 struct osd_thread_info *oti = osd_oti_get(env);
685 * XXX temporary stuff. Some abstraction layer should
690 jh = ldiskfs_journal_start_sb(osd_sb(dev), p->tp_credits);
697 jh->h_sync = p->tp_sync;
698 lu_device_get(&d->dd_lu_dev);
699 oh->ot_dev_link = lu_ref_add
700 (&d->dd_lu_dev.ld_reference,
702 /* add commit callback */
703 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
704 lu_context_enter(&th->th_ctx);
705 osd_journal_callback_set(jh,osd_trans_commit_cb,
707 LASSERT(oti->oti_txns == 0);
708 LASSERT(oti->oti_r_locks == 0);
709 LASSERT(oti->oti_w_locks == 0);
716 th = ERR_PTR(-ENOMEM);
718 CERROR("Invalid transaction parameters\n");
719 th = ERR_PTR(-EINVAL);
726 * Concurrency: shouldn't matter.
728 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
731 struct osd_thandle *oh;
732 struct osd_thread_info *oti = osd_oti_get(env);
736 oh = container_of0(th, struct osd_thandle, ot_super);
737 if (oh->ot_handle != NULL) {
738 handle_t *hdl = oh->ot_handle;
740 LASSERT(oti->oti_txns == 1);
742 LASSERT(oti->oti_r_locks == 0);
743 LASSERT(oti->oti_w_locks == 0);
744 result = dt_txn_hook_stop(env, th);
746 CERROR("Failure in transaction hook: %d\n", result);
747 oh->ot_handle = NULL;
748 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
749 result = ldiskfs_journal_stop(hdl));
751 CERROR("Failure to stop transaction: %d\n", result);
757 * Concurrency: no concurrent access is possible that late in object
760 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
762 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
763 struct osd_device *osd = osd_obj2dev(obj);
764 struct osd_thread_info *oti = osd_oti_get(env);
765 struct txn_param *prm = &oti->oti_txn;
766 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
770 lu_env_init(env_del_obj, LCT_DT_THREAD);
771 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
772 OSD_TXN_INODE_DELETE_CREDITS);
773 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
775 result = osd_oi_delete(osd_oti_get(env_del_obj),
776 &osd->od_oi, fid, th);
777 osd_trans_stop(env_del_obj, th);
779 result = PTR_ERR(th);
781 lu_env_fini(env_del_obj);
786 * Called just before object is freed. Releases all resources except for
787 * object itself (that is released by osd_object_free()).
789 * Concurrency: no concurrent access is possible that late in object
792 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
794 struct osd_object *obj = osd_obj(l);
795 struct inode *inode = obj->oo_inode;
797 LINVRNT(osd_invariant(obj));
800 * If object is unlinked remove fid->ino mapping from object index.
807 if (osd_inode_unlinked(inode)) {
808 result = osd_inode_remove(env, obj);
810 LU_OBJECT_DEBUG(D_ERROR, env, l,
811 "Failed to cleanup: %d\n",
816 obj->oo_inode = NULL;
821 * Concurrency: ->loo_object_release() is called under site spin-lock.
823 static void osd_object_release(const struct lu_env *env,
826 struct osd_object *o = osd_obj(l);
828 LASSERT(!lu_object_is_dying(l->lo_header));
829 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
830 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
834 * Concurrency: shouldn't matter.
836 static int osd_object_print(const struct lu_env *env, void *cookie,
837 lu_printer_t p, const struct lu_object *l)
839 struct osd_object *o = osd_obj(l);
842 if (o->oo_dir != NULL)
843 d = o->oo_dir->od_container.ic_descr;
846 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
848 o->oo_inode ? o->oo_inode->i_ino : 0UL,
849 o->oo_inode ? o->oo_inode->i_generation : 0,
850 d ? d->id_ops->id_name : "plain");
854 * Concurrency: shouldn't matter.
856 int osd_statfs(const struct lu_env *env, struct dt_device *d,
859 struct osd_device *osd = osd_dt_dev(d);
860 struct super_block *sb = osd_sb(osd);
863 cfs_spin_lock(&osd->od_osfs_lock);
865 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
866 result = ll_do_statfs(sb, &osd->od_kstatfs);
867 if (likely(result == 0)) /* N.B. statfs can't really fail */
868 osd->od_osfs_age = cfs_time_current_64();
871 if (likely(result == 0))
872 *sfs = osd->od_kstatfs;
873 cfs_spin_unlock(&osd->od_osfs_lock);
879 * Concurrency: doesn't access mutable data.
881 static void osd_conf_get(const struct lu_env *env,
882 const struct dt_device *dev,
883 struct dt_device_param *param)
885 struct super_block *sb = osd_sb(osd_dt_dev(dev));
888 * XXX should be taken from not-yet-existing fs abstraction layer.
890 param->ddp_max_name_len = LDISKFS_NAME_LEN;
891 param->ddp_max_nlink = LDISKFS_LINK_MAX;
892 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
893 param->ddp_mntopts = 0;
894 if (test_opt(sb, XATTR_USER))
895 param->ddp_mntopts |= MNTOPT_USERXATTR;
896 if (test_opt(sb, POSIX_ACL))
897 param->ddp_mntopts |= MNTOPT_ACL;
901 * Helper function to get and fill the buffer with input values.
903 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
907 buf = &osd_oti_get(env)->oti_buf;
914 * Concurrency: shouldn't matter.
916 static int osd_sync(const struct lu_env *env, struct dt_device *d)
918 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
919 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
923 * Start commit for OSD device.
925 * An implementation of dt_commit_async method for OSD device.
926 * Asychronously starts underlayng fs sync and thereby a transaction
929 * \param env environment
932 * \see dt_device_operations
934 static int osd_commit_async(const struct lu_env *env,
937 struct super_block *s = osd_sb(osd_dt_dev(d));
940 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
941 RETURN(s->s_op->sync_fs(s, 0));
945 * Concurrency: shouldn't matter.
947 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
949 static void osd_ro(const struct lu_env *env, struct dt_device *d)
953 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
955 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
956 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
962 * Concurrency: serialization provided by callers.
964 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
965 int mode, unsigned long timeout, __u32 alg,
966 struct lustre_capa_key *keys)
968 struct osd_device *dev = osd_dt_dev(d);
971 dev->od_fl_capa = mode;
972 dev->od_capa_timeout = timeout;
973 dev->od_capa_alg = alg;
974 dev->od_capa_keys = keys;
979 * Concurrency: serialization provided by callers.
981 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
982 struct dt_quota_ctxt *ctxt, void *data)
984 struct obd_device *obd = (void *)ctxt;
985 struct vfsmount *mnt = (struct vfsmount *)data;
988 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
989 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
990 obd->obd_lvfs_ctxt.pwdmnt = mnt;
991 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
992 obd->obd_lvfs_ctxt.fs = get_ds();
998 * Note: we do not count into QUOTA here.
999 * If we mount with --data_journal we may need more.
1001 static const int osd_dto_credits_noquota[DTO_NR] = {
1004 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1005 * SINGLEDATA_TRANS_BLOCKS(8)
1006 * XXX Note: maybe iam need more, since iam have more level than
1009 [DTO_INDEX_INSERT] = 16,
1010 [DTO_INDEX_DELETE] = 16,
1014 [DTO_IDNEX_UPDATE] = 16,
1016 * Create a object. The same as create object in EXT3.
1017 * DATA_TRANS_BLOCKS(14) +
1018 * INDEX_EXTRA_BLOCKS(8) +
1019 * 3(inode bits, groups, GDT)
1021 [DTO_OBJECT_CREATE] = 25,
1025 [DTO_OBJECT_DELETE] = 25,
1028 * 3(inode bits, group, GDT)
1030 [DTO_ATTR_SET_BASE] = 3,
1032 * Xattr set. The same as xattr of EXT3.
1033 * DATA_TRANS_BLOCKS(14)
1034 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1035 * are also counted in. Do not know why?
1037 [DTO_XATTR_SET] = 14,
1040 * creadits for inode change during write.
1042 [DTO_WRITE_BASE] = 3,
1044 * credits for single block write.
1046 [DTO_WRITE_BLOCK] = 14,
1048 * Attr set credits for chown.
1049 * This is extra credits for setattr, and it is null without quota
1051 [DTO_ATTR_SET_CHOWN]= 0
1055 * Note: we count into QUOTA here.
1056 * If we mount with --data_journal we may need more.
1058 static const int osd_dto_credits_quota[DTO_NR] = {
1060 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1061 * SINGLEDATA_TRANS_BLOCKS(8) +
1062 * 2 * QUOTA_TRANS_BLOCKS(2)
1064 [DTO_INDEX_INSERT] = 20,
1066 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1067 * SINGLEDATA_TRANS_BLOCKS(8) +
1068 * 2 * QUOTA_TRANS_BLOCKS(2)
1070 [DTO_INDEX_DELETE] = 20,
1074 [DTO_IDNEX_UPDATE] = 16,
1076 * Create a object. Same as create object in EXT3 filesystem.
1077 * DATA_TRANS_BLOCKS(16) +
1078 * INDEX_EXTRA_BLOCKS(8) +
1079 * 3(inode bits, groups, GDT) +
1080 * 2 * QUOTA_INIT_BLOCKS(25)
1082 [DTO_OBJECT_CREATE] = 77,
1085 * DATA_TRANS_BLOCKS(16) +
1086 * INDEX_EXTRA_BLOCKS(8) +
1087 * 3(inode bits, groups, GDT) +
1090 [DTO_OBJECT_DELETE] = 27,
1093 * 3 (inode bit, group, GDT) +
1095 [DTO_ATTR_SET_BASE] = 3,
1097 * Xattr set. The same as xattr of EXT3.
1098 * DATA_TRANS_BLOCKS(16)
1099 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
1100 * also counted in. Do not know why?
1102 [DTO_XATTR_SET] = 16,
1105 * creadits for inode change during write.
1107 [DTO_WRITE_BASE] = 3,
1109 * credits for single block write.
1111 [DTO_WRITE_BLOCK] = 16,
1113 * Attr set credits for chown.
1114 * It is added to already set setattr credits
1115 * 2 * QUOTA_INIT_BLOCKS(25) +
1116 * 2 * QUOTA_DEL_BLOCKS(9)
1118 [DTO_ATTR_SET_CHOWN]= 68,
1121 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1124 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1125 ARRAY_SIZE(osd_dto_credits_quota));
1126 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1127 #ifdef HAVE_QUOTA_SUPPORT
1128 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1129 return osd_dto_credits_quota[op];
1132 return osd_dto_credits_noquota[op];
1135 static const struct dt_device_operations osd_dt_ops = {
1136 .dt_root_get = osd_root_get,
1137 .dt_statfs = osd_statfs,
1138 .dt_trans_start = osd_trans_start,
1139 .dt_trans_stop = osd_trans_stop,
1140 .dt_conf_get = osd_conf_get,
1141 .dt_sync = osd_sync,
1143 .dt_commit_async = osd_commit_async,
1144 .dt_credit_get = osd_credit_get,
1145 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1146 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1149 static void osd_object_read_lock(const struct lu_env *env,
1150 struct dt_object *dt, unsigned role)
1152 struct osd_object *obj = osd_dt_obj(dt);
1153 struct osd_thread_info *oti = osd_oti_get(env);
1155 LINVRNT(osd_invariant(obj));
1157 LASSERT(obj->oo_owner != env);
1158 cfs_down_read_nested(&obj->oo_sem, role);
1160 LASSERT(obj->oo_owner == NULL);
1164 static void osd_object_write_lock(const struct lu_env *env,
1165 struct dt_object *dt, unsigned role)
1167 struct osd_object *obj = osd_dt_obj(dt);
1168 struct osd_thread_info *oti = osd_oti_get(env);
1170 LINVRNT(osd_invariant(obj));
1172 LASSERT(obj->oo_owner != env);
1173 cfs_down_write_nested(&obj->oo_sem, role);
1175 LASSERT(obj->oo_owner == NULL);
1176 obj->oo_owner = env;
1180 static void osd_object_read_unlock(const struct lu_env *env,
1181 struct dt_object *dt)
1183 struct osd_object *obj = osd_dt_obj(dt);
1184 struct osd_thread_info *oti = osd_oti_get(env);
1186 LINVRNT(osd_invariant(obj));
1188 LASSERT(oti->oti_r_locks > 0);
1190 cfs_up_read(&obj->oo_sem);
1193 static void osd_object_write_unlock(const struct lu_env *env,
1194 struct dt_object *dt)
1196 struct osd_object *obj = osd_dt_obj(dt);
1197 struct osd_thread_info *oti = osd_oti_get(env);
1199 LINVRNT(osd_invariant(obj));
1201 LASSERT(obj->oo_owner == env);
1202 LASSERT(oti->oti_w_locks > 0);
1204 obj->oo_owner = NULL;
1205 cfs_up_write(&obj->oo_sem);
1208 static int osd_object_write_locked(const struct lu_env *env,
1209 struct dt_object *dt)
1211 struct osd_object *obj = osd_dt_obj(dt);
1213 LINVRNT(osd_invariant(obj));
1215 return obj->oo_owner == env;
1218 static int capa_is_sane(const struct lu_env *env,
1219 struct osd_device *dev,
1220 struct lustre_capa *capa,
1221 struct lustre_capa_key *keys)
1223 struct osd_thread_info *oti = osd_oti_get(env);
1224 struct lustre_capa *tcapa = &oti->oti_capa;
1225 struct obd_capa *oc;
1229 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1231 if (capa_is_expired(oc)) {
1232 DEBUG_CAPA(D_ERROR, capa, "expired");
1239 if (capa_is_expired_sec(capa)) {
1240 DEBUG_CAPA(D_ERROR, capa, "expired");
1244 cfs_spin_lock(&capa_lock);
1245 for (i = 0; i < 2; i++) {
1246 if (keys[i].lk_keyid == capa->lc_keyid) {
1247 oti->oti_capa_key = keys[i];
1251 cfs_spin_unlock(&capa_lock);
1254 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1258 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1262 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1263 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1267 oc = capa_add(dev->od_capa_hash, capa);
1273 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1274 struct lustre_capa *capa, __u64 opc)
1276 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1277 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1278 struct md_capainfo *ci;
1281 if (!dev->od_fl_capa)
1284 if (capa == BYPASS_CAPA)
1287 ci = md_capainfo(env);
1291 if (ci->mc_auth == LC_ID_NONE)
1295 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1299 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1300 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1305 if (!capa_opc_supported(capa, opc)) {
1306 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1310 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1311 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1318 static struct timespec *osd_inode_time(const struct lu_env *env,
1319 struct inode *inode, __u64 seconds)
1321 struct osd_thread_info *oti = osd_oti_get(env);
1322 struct timespec *t = &oti->oti_time;
1324 t->tv_sec = seconds;
1326 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1331 static void osd_inode_getattr(const struct lu_env *env,
1332 struct inode *inode, struct lu_attr *attr)
1334 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1335 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1336 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1338 attr->la_atime = LTIME_S(inode->i_atime);
1339 attr->la_mtime = LTIME_S(inode->i_mtime);
1340 attr->la_ctime = LTIME_S(inode->i_ctime);
1341 attr->la_mode = inode->i_mode;
1342 attr->la_size = i_size_read(inode);
1343 attr->la_blocks = inode->i_blocks;
1344 attr->la_uid = inode->i_uid;
1345 attr->la_gid = inode->i_gid;
1346 attr->la_flags = LDISKFS_I(inode)->i_flags;
1347 attr->la_nlink = inode->i_nlink;
1348 attr->la_rdev = inode->i_rdev;
1349 attr->la_blksize = ll_inode_blksize(inode);
1350 attr->la_blkbits = inode->i_blkbits;
1353 static int osd_attr_get(const struct lu_env *env,
1354 struct dt_object *dt,
1355 struct lu_attr *attr,
1356 struct lustre_capa *capa)
1358 struct osd_object *obj = osd_dt_obj(dt);
1360 LASSERT(dt_object_exists(dt));
1361 LINVRNT(osd_invariant(obj));
1363 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1366 cfs_spin_lock(&obj->oo_guard);
1367 osd_inode_getattr(env, obj->oo_inode, attr);
1368 cfs_spin_unlock(&obj->oo_guard);
1372 static int osd_inode_setattr(const struct lu_env *env,
1373 struct inode *inode, const struct lu_attr *attr)
1377 bits = attr->la_valid;
1379 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1381 #ifdef HAVE_QUOTA_SUPPORT
1382 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1383 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1384 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1390 iattr.ia_valid |= ATTR_UID;
1392 iattr.ia_valid |= ATTR_GID;
1393 iattr.ia_uid = attr->la_uid;
1394 iattr.ia_gid = attr->la_gid;
1395 osd_push_ctxt(env, save);
1396 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1403 if (bits & LA_ATIME)
1404 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1405 if (bits & LA_CTIME)
1406 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1407 if (bits & LA_MTIME)
1408 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1409 if (bits & LA_SIZE) {
1410 LDISKFS_I(inode)->i_disksize = attr->la_size;
1411 i_size_write(inode, attr->la_size);
1415 /* OSD should not change "i_blocks" which is used by quota.
1416 * "i_blocks" should be changed by ldiskfs only. */
1417 if (bits & LA_BLOCKS)
1418 inode->i_blocks = attr->la_blocks;
1421 inode->i_mode = (inode->i_mode & S_IFMT) |
1422 (attr->la_mode & ~S_IFMT);
1424 inode->i_uid = attr->la_uid;
1426 inode->i_gid = attr->la_gid;
1427 if (bits & LA_NLINK)
1428 inode->i_nlink = attr->la_nlink;
1430 inode->i_rdev = attr->la_rdev;
1432 if (bits & LA_FLAGS) {
1433 /* always keep S_NOCMTIME */
1434 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1440 static int osd_attr_set(const struct lu_env *env,
1441 struct dt_object *dt,
1442 const struct lu_attr *attr,
1443 struct thandle *handle,
1444 struct lustre_capa *capa)
1446 struct osd_object *obj = osd_dt_obj(dt);
1449 LASSERT(handle != NULL);
1450 LASSERT(dt_object_exists(dt));
1451 LASSERT(osd_invariant(obj));
1453 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1456 cfs_spin_lock(&obj->oo_guard);
1457 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1458 cfs_spin_unlock(&obj->oo_guard);
1461 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1468 * XXX temporary solution.
1470 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1471 struct lu_attr *attr, struct thandle *th)
1476 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1477 struct lu_attr *attr, struct thandle *th)
1479 osd_object_init0(obj);
1480 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1481 unlock_new_inode(obj->oo_inode);
1485 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1486 struct osd_object *obj,
1490 struct osd_thread_info *info = osd_oti_get(env);
1491 struct dentry *child_dentry = &info->oti_child_dentry;
1492 struct dentry *obj_dentry = &info->oti_obj_dentry;
1494 obj_dentry->d_inode = obj->oo_inode;
1495 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1496 obj_dentry->d_name.hash = 0;
1498 child_dentry->d_name.hash = 0;
1499 child_dentry->d_parent = obj_dentry;
1500 child_dentry->d_name.name = name;
1501 child_dentry->d_name.len = namelen;
1502 return child_dentry;
1506 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1508 struct dt_allocation_hint *hint,
1512 struct osd_device *osd = osd_obj2dev(obj);
1513 struct osd_thandle *oth;
1514 struct dt_object *parent;
1515 struct inode *inode;
1516 #ifdef HAVE_QUOTA_SUPPORT
1517 struct osd_ctxt *save = &info->oti_ctxt;
1520 LINVRNT(osd_invariant(obj));
1521 LASSERT(obj->oo_inode == NULL);
1523 oth = container_of(th, struct osd_thandle, ot_super);
1524 LASSERT(oth->ot_handle->h_transaction != NULL);
1526 if (hint && hint->dah_parent)
1527 parent = hint->dah_parent;
1529 parent = osd->od_obj_area;
1531 LASSERT(parent != NULL);
1532 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1534 #ifdef HAVE_QUOTA_SUPPORT
1535 osd_push_ctxt(info->oti_env, save);
1537 inode = ldiskfs_create_inode(oth->ot_handle,
1538 osd_dt_obj(parent)->oo_inode, mode);
1539 #ifdef HAVE_QUOTA_SUPPORT
1542 if (!IS_ERR(inode)) {
1543 /* Do not update file c/mtime in ldiskfs.
1544 * NB: don't need any lock because no contention at this
1546 inode->i_flags |= S_NOCMTIME;
1547 obj->oo_inode = inode;
1550 result = PTR_ERR(inode);
1551 LINVRNT(osd_invariant(obj));
1559 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1560 struct lu_attr *attr,
1561 struct dt_allocation_hint *hint,
1562 struct dt_object_format *dof,
1566 struct osd_thandle *oth;
1567 struct osd_device *osd = osd_obj2dev(obj);
1568 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1570 LASSERT(S_ISDIR(attr->la_mode));
1572 oth = container_of(th, struct osd_thandle, ot_super);
1573 LASSERT(oth->ot_handle->h_transaction != NULL);
1574 result = osd_mkfile(info, obj, mode, hint, th);
1575 if (result == 0 && osd->od_iop_mode == 0) {
1576 LASSERT(obj->oo_inode != NULL);
1578 * XXX uh-oh... call low-level iam function directly.
1581 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1582 sizeof (struct osd_fid_pack),
1588 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1589 struct lu_attr *attr,
1590 struct dt_allocation_hint *hint,
1591 struct dt_object_format *dof,
1595 struct osd_thandle *oth;
1596 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1598 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1600 LASSERT(S_ISREG(attr->la_mode));
1602 oth = container_of(th, struct osd_thandle, ot_super);
1603 LASSERT(oth->ot_handle->h_transaction != NULL);
1605 result = osd_mkfile(info, obj, mode, hint, th);
1607 LASSERT(obj->oo_inode != NULL);
1608 if (feat->dif_flags & DT_IND_VARKEY)
1609 result = iam_lvar_create(obj->oo_inode,
1610 feat->dif_keysize_max,
1612 feat->dif_recsize_max,
1615 result = iam_lfix_create(obj->oo_inode,
1616 feat->dif_keysize_max,
1618 feat->dif_recsize_max,
1625 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1626 struct lu_attr *attr,
1627 struct dt_allocation_hint *hint,
1628 struct dt_object_format *dof,
1631 LASSERT(S_ISREG(attr->la_mode));
1632 return osd_mkfile(info, obj, (attr->la_mode &
1633 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1636 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1637 struct lu_attr *attr,
1638 struct dt_allocation_hint *hint,
1639 struct dt_object_format *dof,
1642 LASSERT(S_ISLNK(attr->la_mode));
1643 return osd_mkfile(info, obj, (attr->la_mode &
1644 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1647 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1648 struct lu_attr *attr,
1649 struct dt_allocation_hint *hint,
1650 struct dt_object_format *dof,
1653 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1656 LINVRNT(osd_invariant(obj));
1657 LASSERT(obj->oo_inode == NULL);
1658 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1659 S_ISFIFO(mode) || S_ISSOCK(mode));
1661 result = osd_mkfile(info, obj, mode, hint, th);
1663 LASSERT(obj->oo_inode != NULL);
1664 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1666 LINVRNT(osd_invariant(obj));
1670 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1672 struct dt_allocation_hint *hint,
1673 struct dt_object_format *dof,
1676 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1678 osd_obj_type_f result;
1694 result = osd_mk_index;
1705 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1706 struct dt_object *parent, cfs_umode_t child_mode)
1710 memset(ah, 0, sizeof(*ah));
1711 ah->dah_parent = parent;
1712 ah->dah_mode = child_mode;
1716 * Helper function for osd_object_create()
1718 * \retval 0, on success
1720 static int __osd_object_create(struct osd_thread_info *info,
1721 struct osd_object *obj, struct lu_attr *attr,
1722 struct dt_allocation_hint *hint,
1723 struct dt_object_format *dof,
1729 result = osd_create_pre(info, obj, attr, th);
1731 result = osd_create_type_f(dof->dof_type)(info, obj,
1732 attr, hint, dof, th);
1734 result = osd_create_post(info, obj, attr, th);
1740 * Helper function for osd_object_create()
1742 * \retval 0, on success
1744 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1745 const struct lu_fid *fid, struct thandle *th)
1747 struct osd_thread_info *info = osd_oti_get(env);
1748 struct osd_inode_id *id = &info->oti_id;
1749 struct osd_device *osd = osd_obj2dev(obj);
1750 struct md_ucred *uc = md_ucred(env);
1752 LASSERT(obj->oo_inode != NULL);
1753 LASSERT(uc != NULL);
1755 id->oii_ino = obj->oo_inode->i_ino;
1756 id->oii_gen = obj->oo_inode->i_generation;
1758 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1759 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1762 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1763 struct lu_attr *attr,
1764 struct dt_allocation_hint *hint,
1765 struct dt_object_format *dof,
1768 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1769 struct osd_object *obj = osd_dt_obj(dt);
1770 struct osd_thread_info *info = osd_oti_get(env);
1775 LINVRNT(osd_invariant(obj));
1776 LASSERT(!dt_object_exists(dt));
1777 LASSERT(osd_write_locked(env, obj));
1778 LASSERT(th != NULL);
1780 result = __osd_object_create(info, obj, attr, hint, dof, th);
1782 result = __osd_oi_insert(env, obj, fid, th);
1784 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1785 LASSERT(osd_invariant(obj));
1790 * Helper function for osd_xattr_set()
1792 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1793 const struct lu_buf *buf, const char *name, int fl)
1795 struct osd_object *obj = osd_dt_obj(dt);
1796 struct inode *inode = obj->oo_inode;
1797 struct osd_thread_info *info = osd_oti_get(env);
1798 struct dentry *dentry = &info->oti_child_dentry;
1802 LASSERT(dt_object_exists(dt));
1803 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1804 LASSERT(osd_write_locked(env, obj));
1806 if (fl & LU_XATTR_REPLACE)
1807 fs_flags |= XATTR_REPLACE;
1809 if (fl & LU_XATTR_CREATE)
1810 fs_flags |= XATTR_CREATE;
1812 dentry->d_inode = inode;
1813 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1814 buf->lb_len, fs_flags);
1819 * Put the fid into lustre_mdt_attrs, and then place the structure
1820 * inode's ea. This fid should not be altered during the life time
1823 * \retval +ve, on success
1824 * \retval -ve, on error
1826 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1828 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1829 const struct lu_fid *fid)
1831 struct osd_thread_info *info = osd_oti_get(env);
1832 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1834 lustre_lma_init(mdt_attrs, fid);
1835 lustre_lma_swab(mdt_attrs);
1836 return __osd_xattr_set(env, dt,
1837 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1838 XATTR_NAME_LMA, LU_XATTR_CREATE);
1843 * Helper function to form igif
1845 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1848 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1852 * Helper function to pack the fid, ldiskfs stores fid in packed format.
1854 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1855 struct lu_fid *befider)
1857 fid_cpu_to_be(befider, (struct lu_fid *)fid);
1858 memcpy(pack->fp_area, befider, sizeof(*befider));
1859 pack->fp_len = sizeof(*befider) + 1;
1863 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1864 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1865 * To have compatilibility with 1.8 ldiskfs driver we need to have
1866 * magic number at start of fid data.
1867 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1870 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1871 const struct dt_rec *fid)
1873 param->edp_magic = LDISKFS_LUFID_MAGIC;
1874 param->edp_len = sizeof(struct lu_fid) + 1;
1876 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1877 (struct lu_fid *)fid);
1880 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1885 switch (pack->fp_len) {
1886 case sizeof *fid + 1:
1887 memcpy(fid, pack->fp_area, sizeof *fid);
1888 fid_be_to_cpu(fid, fid);
1891 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1898 * Try to read the fid from inode ea into dt_rec, if return value
1899 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1901 * \param fid object fid.
1903 * \retval 0 on success
1905 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1906 __u32 ino, struct lu_fid *fid)
1908 struct osd_thread_info *info = osd_oti_get(env);
1909 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1910 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1911 struct dentry *dentry = &info->oti_child_dentry;
1912 struct osd_inode_id *id = &info->oti_id;
1913 struct osd_device *dev;
1914 struct inode *inode;
1918 dev = osd_dev(ldev);
1921 id->oii_gen = OSD_OII_NOGEN;
1923 inode = osd_iget(info, dev, id);
1924 if (IS_ERR(inode)) {
1925 rc = PTR_ERR(inode);
1928 dentry->d_inode = inode;
1930 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1931 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1934 /* Check LMA compatibility */
1936 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
1937 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1938 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
1939 ~LMA_INCOMPAT_SUPP);
1944 lustre_lma_swab(mdt_attrs);
1945 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
1947 } else if (rc == -ENODATA) {
1948 osd_igif_get(env, inode, fid);
1957 * OSD layer object create function for interoperability mode (b11826).
1958 * This is mostly similar to osd_object_create(). Only difference being, fid is
1959 * inserted into inode ea here.
1961 * \retval 0, on success
1962 * \retval -ve, on error
1964 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1965 struct lu_attr *attr,
1966 struct dt_allocation_hint *hint,
1967 struct dt_object_format *dof,
1970 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1971 struct osd_object *obj = osd_dt_obj(dt);
1972 struct osd_thread_info *info = osd_oti_get(env);
1977 LASSERT(osd_invariant(obj));
1978 LASSERT(!dt_object_exists(dt));
1979 LASSERT(osd_write_locked(env, obj));
1980 LASSERT(th != NULL);
1982 result = __osd_object_create(info, obj, attr, hint, dof, th);
1984 /* objects under osd root shld have igif fid, so dont add fid EA */
1985 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
1986 result = osd_ea_fid_set(env, dt, fid);
1989 result = __osd_oi_insert(env, obj, fid, th);
1991 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1992 LINVRNT(osd_invariant(obj));
1997 * Concurrency: @dt is write locked.
1999 static void osd_object_ref_add(const struct lu_env *env,
2000 struct dt_object *dt,
2003 struct osd_object *obj = osd_dt_obj(dt);
2004 struct inode *inode = obj->oo_inode;
2006 LINVRNT(osd_invariant(obj));
2007 LASSERT(dt_object_exists(dt));
2008 LASSERT(osd_write_locked(env, obj));
2009 LASSERT(th != NULL);
2011 cfs_spin_lock(&obj->oo_guard);
2012 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
2014 cfs_spin_unlock(&obj->oo_guard);
2015 inode->i_sb->s_op->dirty_inode(inode);
2016 LINVRNT(osd_invariant(obj));
2020 * Concurrency: @dt is write locked.
2022 static void osd_object_ref_del(const struct lu_env *env,
2023 struct dt_object *dt,
2026 struct osd_object *obj = osd_dt_obj(dt);
2027 struct inode *inode = obj->oo_inode;
2029 LINVRNT(osd_invariant(obj));
2030 LASSERT(dt_object_exists(dt));
2031 LASSERT(osd_write_locked(env, obj));
2032 LASSERT(th != NULL);
2034 cfs_spin_lock(&obj->oo_guard);
2035 LASSERT(inode->i_nlink > 0);
2037 cfs_spin_unlock(&obj->oo_guard);
2038 inode->i_sb->s_op->dirty_inode(inode);
2039 LINVRNT(osd_invariant(obj));
2043 * Concurrency: @dt is read locked.
2045 static int osd_xattr_get(const struct lu_env *env,
2046 struct dt_object *dt,
2049 struct lustre_capa *capa)
2051 struct osd_object *obj = osd_dt_obj(dt);
2052 struct inode *inode = obj->oo_inode;
2053 struct osd_thread_info *info = osd_oti_get(env);
2054 struct dentry *dentry = &info->oti_obj_dentry;
2056 LASSERT(dt_object_exists(dt));
2057 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2058 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2060 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2063 dentry->d_inode = inode;
2064 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2068 * Concurrency: @dt is write locked.
2070 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2071 const struct lu_buf *buf, const char *name, int fl,
2072 struct thandle *handle, struct lustre_capa *capa)
2074 LASSERT(handle != NULL);
2076 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2079 return __osd_xattr_set(env, dt, buf, name, fl);
2083 * Concurrency: @dt is read locked.
2085 static int osd_xattr_list(const struct lu_env *env,
2086 struct dt_object *dt,
2088 struct lustre_capa *capa)
2090 struct osd_object *obj = osd_dt_obj(dt);
2091 struct inode *inode = obj->oo_inode;
2092 struct osd_thread_info *info = osd_oti_get(env);
2093 struct dentry *dentry = &info->oti_obj_dentry;
2095 LASSERT(dt_object_exists(dt));
2096 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2097 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2099 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2102 dentry->d_inode = inode;
2103 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2107 * Concurrency: @dt is write locked.
2109 static int osd_xattr_del(const struct lu_env *env,
2110 struct dt_object *dt,
2112 struct thandle *handle,
2113 struct lustre_capa *capa)
2115 struct osd_object *obj = osd_dt_obj(dt);
2116 struct inode *inode = obj->oo_inode;
2117 struct osd_thread_info *info = osd_oti_get(env);
2118 struct dentry *dentry = &info->oti_obj_dentry;
2121 LASSERT(dt_object_exists(dt));
2122 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2123 LASSERT(osd_write_locked(env, obj));
2124 LASSERT(handle != NULL);
2126 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2129 dentry->d_inode = inode;
2130 rc = inode->i_op->removexattr(dentry, name);
2134 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2135 struct dt_object *dt,
2136 struct lustre_capa *old,
2139 struct osd_thread_info *info = osd_oti_get(env);
2140 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2141 struct osd_object *obj = osd_dt_obj(dt);
2142 struct osd_device *dev = osd_obj2dev(obj);
2143 struct lustre_capa_key *key = &info->oti_capa_key;
2144 struct lustre_capa *capa = &info->oti_capa;
2145 struct obd_capa *oc;
2146 struct md_capainfo *ci;
2150 if (!dev->od_fl_capa)
2151 RETURN(ERR_PTR(-ENOENT));
2153 LASSERT(dt_object_exists(dt));
2154 LINVRNT(osd_invariant(obj));
2156 /* renewal sanity check */
2157 if (old && osd_object_auth(env, dt, old, opc))
2158 RETURN(ERR_PTR(-EACCES));
2160 ci = md_capainfo(env);
2162 RETURN(ERR_PTR(-ENOENT));
2164 switch (ci->mc_auth) {
2168 capa->lc_uid = obj->oo_inode->i_uid;
2169 capa->lc_gid = obj->oo_inode->i_gid;
2170 capa->lc_flags = LC_ID_PLAIN;
2172 case LC_ID_CONVERT: {
2175 s[0] = obj->oo_inode->i_uid;
2176 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2177 s[2] = obj->oo_inode->i_gid;
2178 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2179 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2181 RETURN(ERR_PTR(rc));
2183 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2184 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2185 capa->lc_flags = LC_ID_CONVERT;
2189 RETURN(ERR_PTR(-EINVAL));
2192 capa->lc_fid = *fid;
2194 capa->lc_flags |= dev->od_capa_alg << 24;
2195 capa->lc_timeout = dev->od_capa_timeout;
2196 capa->lc_expiry = 0;
2198 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2200 LASSERT(!capa_is_expired(oc));
2204 cfs_spin_lock(&capa_lock);
2205 *key = dev->od_capa_keys[1];
2206 cfs_spin_unlock(&capa_lock);
2208 capa->lc_keyid = key->lk_keyid;
2209 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2211 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2213 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2214 RETURN(ERR_PTR(rc));
2217 oc = capa_add(dev->od_capa_hash, capa);
2221 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2224 struct osd_object *obj = osd_dt_obj(dt);
2225 struct inode *inode = obj->oo_inode;
2226 struct osd_thread_info *info = osd_oti_get(env);
2227 struct dentry *dentry = &info->oti_obj_dentry;
2228 struct file *file = &info->oti_file;
2231 dentry->d_inode = inode;
2232 file->f_dentry = dentry;
2233 file->f_mapping = inode->i_mapping;
2234 file->f_op = inode->i_fop;
2235 LOCK_INODE_MUTEX(inode);
2236 rc = file->f_op->fsync(file, dentry, 0);
2237 UNLOCK_INODE_MUTEX(inode);
2242 * Get the 64-bit version for an inode.
2244 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2245 struct dt_object *dt)
2247 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2249 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2250 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2251 return LDISKFS_I(inode)->i_fs_version;
2255 * Set the 64-bit version and return the old version.
2257 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2258 dt_obj_version_t new_version)
2260 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2262 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2263 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2264 LDISKFS_I(inode)->i_fs_version = new_version;
2265 /** Version is set after all inode operations are finished,
2266 * so we should mark it dirty here */
2267 inode->i_sb->s_op->dirty_inode(inode);
2270 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2273 struct osd_object *obj = osd_dt_obj(dt);
2276 *data = (void *)obj->oo_inode;
2284 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2285 const struct dt_index_features *feat)
2287 struct iam_descr *descr;
2289 if (osd_object_is_root(o))
2290 return feat == &dt_directory_features;
2292 LASSERT(o->oo_dir != NULL);
2294 descr = o->oo_dir->od_container.ic_descr;
2295 if (feat == &dt_directory_features) {
2296 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2302 feat->dif_keysize_min <= descr->id_key_size &&
2303 descr->id_key_size <= feat->dif_keysize_max &&
2304 feat->dif_recsize_min <= descr->id_rec_size &&
2305 descr->id_rec_size <= feat->dif_recsize_max &&
2306 !(feat->dif_flags & (DT_IND_VARKEY |
2307 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2308 ergo(feat->dif_flags & DT_IND_UPDATE,
2309 1 /* XXX check that object (and file system) is
2314 static int osd_iam_container_init(const struct lu_env *env,
2315 struct osd_object *obj,
2316 struct osd_directory *dir)
2319 struct iam_container *bag;
2321 bag = &dir->od_container;
2322 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2324 result = iam_container_setup(bag);
2326 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2328 iam_container_fini(bag);
2335 * Concurrency: no external locking is necessary.
2337 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2338 const struct dt_index_features *feat)
2342 struct osd_object *obj = osd_dt_obj(dt);
2343 struct osd_device *osd = osd_obj2dev(obj);
2345 LINVRNT(osd_invariant(obj));
2346 LASSERT(dt_object_exists(dt));
2348 if (osd_object_is_root(obj)) {
2349 dt->do_index_ops = &osd_index_ea_ops;
2351 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2352 dt->do_index_ops = &osd_index_ea_ops;
2353 if (S_ISDIR(obj->oo_inode->i_mode))
2358 } else if (!osd_has_index(obj)) {
2359 struct osd_directory *dir;
2364 cfs_spin_lock(&obj->oo_guard);
2365 if (obj->oo_dir == NULL)
2369 * Concurrent thread allocated container data.
2372 cfs_spin_unlock(&obj->oo_guard);
2374 * Now, that we have container data, serialize its
2377 cfs_down_write(&obj->oo_ext_idx_sem);
2379 * recheck under lock.
2381 if (!osd_has_index(obj))
2382 result = osd_iam_container_init(env, obj, dir);
2385 cfs_up_write(&obj->oo_ext_idx_sem);
2391 if (result == 0 && ea_dir == 0) {
2392 if (!osd_iam_index_probe(env, obj, feat))
2395 LINVRNT(osd_invariant(obj));
2400 static const struct dt_object_operations osd_obj_ops = {
2401 .do_read_lock = osd_object_read_lock,
2402 .do_write_lock = osd_object_write_lock,
2403 .do_read_unlock = osd_object_read_unlock,
2404 .do_write_unlock = osd_object_write_unlock,
2405 .do_write_locked = osd_object_write_locked,
2406 .do_attr_get = osd_attr_get,
2407 .do_attr_set = osd_attr_set,
2408 .do_ah_init = osd_ah_init,
2409 .do_create = osd_object_create,
2410 .do_index_try = osd_index_try,
2411 .do_ref_add = osd_object_ref_add,
2412 .do_ref_del = osd_object_ref_del,
2413 .do_xattr_get = osd_xattr_get,
2414 .do_xattr_set = osd_xattr_set,
2415 .do_xattr_del = osd_xattr_del,
2416 .do_xattr_list = osd_xattr_list,
2417 .do_capa_get = osd_capa_get,
2418 .do_object_sync = osd_object_sync,
2419 .do_version_get = osd_object_version_get,
2420 .do_version_set = osd_object_version_set,
2421 .do_data_get = osd_data_get,
2425 * dt_object_operations for interoperability mode
2426 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2428 static const struct dt_object_operations osd_obj_ea_ops = {
2429 .do_read_lock = osd_object_read_lock,
2430 .do_write_lock = osd_object_write_lock,
2431 .do_read_unlock = osd_object_read_unlock,
2432 .do_write_unlock = osd_object_write_unlock,
2433 .do_write_locked = osd_object_write_locked,
2434 .do_attr_get = osd_attr_get,
2435 .do_attr_set = osd_attr_set,
2436 .do_ah_init = osd_ah_init,
2437 .do_create = osd_object_ea_create,
2438 .do_index_try = osd_index_try,
2439 .do_ref_add = osd_object_ref_add,
2440 .do_ref_del = osd_object_ref_del,
2441 .do_xattr_get = osd_xattr_get,
2442 .do_xattr_set = osd_xattr_set,
2443 .do_xattr_del = osd_xattr_del,
2444 .do_xattr_list = osd_xattr_list,
2445 .do_capa_get = osd_capa_get,
2446 .do_object_sync = osd_object_sync,
2447 .do_version_get = osd_object_version_get,
2448 .do_version_set = osd_object_version_set,
2449 .do_data_get = osd_data_get,
2457 * XXX: Another layering violation for now.
2459 * We don't want to use ->f_op->read methods, because generic file write
2461 * - serializes on ->i_sem, and
2463 * - does a lot of extra work like balance_dirty_pages(),
2465 * which doesn't work for globally shared files like /last-received.
2467 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2469 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2471 memcpy(buffer, (char*)ei->i_data, buflen);
2476 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2479 struct buffer_head *bh;
2480 unsigned long block;
2487 /* prevent reading after eof */
2488 spin_lock(&inode->i_lock);
2489 if (i_size_read(inode) < *offs + size) {
2490 size = i_size_read(inode) - *offs;
2491 spin_unlock(&inode->i_lock);
2493 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2494 i_size_read(inode), *offs);
2496 } else if (size == 0) {
2500 spin_unlock(&inode->i_lock);
2503 blocksize = 1 << inode->i_blkbits;
2506 block = *offs >> inode->i_blkbits;
2507 boffs = *offs & (blocksize - 1);
2508 csize = min(blocksize - boffs, size);
2509 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2511 CERROR("can't read block: %d\n", err);
2515 memcpy(buf, bh->b_data + boffs, csize);
2525 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2526 struct lu_buf *buf, loff_t *pos,
2527 struct lustre_capa *capa)
2529 struct osd_object *obj = osd_dt_obj(dt);
2530 struct inode *inode = obj->oo_inode;
2533 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2536 /* Read small symlink from inode body as we need to maintain correct
2537 * on-disk symlinks for ldiskfs.
2539 if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2540 (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2541 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2543 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2548 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2551 memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2553 LDISKFS_I(inode)->i_disksize = buflen;
2554 i_size_write(inode, buflen);
2555 inode->i_sb->s_op->dirty_inode(inode);
2560 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2561 loff_t *offs, handle_t *handle)
2563 struct buffer_head *bh = NULL;
2564 loff_t offset = *offs;
2565 loff_t new_size = i_size_read(inode);
2566 unsigned long block;
2567 int blocksize = 1 << inode->i_blkbits;
2571 int dirty_inode = 0;
2573 while (bufsize > 0) {
2577 block = offset >> inode->i_blkbits;
2578 boffs = offset & (blocksize - 1);
2579 size = min(blocksize - boffs, bufsize);
2580 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2582 CERROR("can't read/create block: %d\n", err);
2586 err = ldiskfs_journal_get_write_access(handle, bh);
2588 CERROR("journal_get_write_access() returned error %d\n",
2592 LASSERTF(boffs + size <= bh->b_size,
2593 "boffs %d size %d bh->b_size %lu",
2594 boffs, size, (unsigned long)bh->b_size);
2595 memcpy(bh->b_data + boffs, buf, size);
2596 err = ldiskfs_journal_dirty_metadata(handle, bh);
2600 if (offset + size > new_size)
2601 new_size = offset + size;
2609 /* correct in-core and on-disk sizes */
2610 if (new_size > i_size_read(inode)) {
2611 spin_lock(&inode->i_lock);
2612 if (new_size > i_size_read(inode))
2613 i_size_write(inode, new_size);
2614 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
2615 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
2618 spin_unlock(&inode->i_lock);
2620 inode->i_sb->s_op->dirty_inode(inode);
2628 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2629 const struct lu_buf *buf, loff_t *pos,
2630 struct thandle *handle, struct lustre_capa *capa,
2633 struct osd_object *obj = osd_dt_obj(dt);
2634 struct inode *inode = obj->oo_inode;
2635 struct osd_thandle *oh;
2637 #ifdef HAVE_QUOTA_SUPPORT
2638 cfs_cap_t save = cfs_curproc_cap_pack();
2641 LASSERT(handle != NULL);
2643 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2646 oh = container_of(handle, struct osd_thandle, ot_super);
2647 LASSERT(oh->ot_handle->h_transaction != NULL);
2648 #ifdef HAVE_QUOTA_SUPPORT
2650 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2652 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2654 /* Write small symlink to inode body as we need to maintain correct
2655 * on-disk symlinks for ldiskfs.
2657 if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2658 (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
2659 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2661 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
2664 #ifdef HAVE_QUOTA_SUPPORT
2665 cfs_curproc_cap_unpack(save);
2668 result = buf->lb_len;
2672 static const struct dt_body_operations osd_body_ops = {
2673 .dbo_read = osd_read,
2674 .dbo_write = osd_write
2679 * delete a (key, value) pair from index \a dt specified by \a key
2681 * \param dt osd index object
2682 * \param key key for index
2683 * \param rec record reference
2684 * \param handle transaction handler
2687 * \retval -ve failure
2690 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2691 const struct dt_key *key, struct thandle *handle,
2692 struct lustre_capa *capa)
2694 struct osd_object *obj = osd_dt_obj(dt);
2695 struct osd_thandle *oh;
2696 struct iam_path_descr *ipd;
2697 struct iam_container *bag = &obj->oo_dir->od_container;
2702 LINVRNT(osd_invariant(obj));
2703 LASSERT(dt_object_exists(dt));
2704 LASSERT(bag->ic_object == obj->oo_inode);
2705 LASSERT(handle != NULL);
2707 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2710 ipd = osd_idx_ipd_get(env, bag);
2711 if (unlikely(ipd == NULL))
2714 oh = container_of0(handle, struct osd_thandle, ot_super);
2715 LASSERT(oh->ot_handle != NULL);
2716 LASSERT(oh->ot_handle->h_transaction != NULL);
2718 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2719 osd_ipd_put(env, bag, ipd);
2720 LINVRNT(osd_invariant(obj));
2724 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2727 struct osd_fid_pack *rec;
2730 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2731 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2732 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2738 * Index delete function for interoperability mode (b11826).
2739 * It will remove the directory entry added by osd_index_ea_insert().
2740 * This entry is needed to maintain name->fid mapping.
2742 * \param key, key i.e. file entry to be deleted
2744 * \retval 0, on success
2745 * \retval -ve, on error
2747 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2748 const struct dt_key *key, struct thandle *handle,
2749 struct lustre_capa *capa)
2751 struct osd_object *obj = osd_dt_obj(dt);
2752 struct inode *dir = obj->oo_inode;
2753 struct dentry *dentry;
2754 struct osd_thandle *oh;
2755 struct ldiskfs_dir_entry_2 *de;
2756 struct buffer_head *bh;
2762 LINVRNT(osd_invariant(obj));
2763 LASSERT(dt_object_exists(dt));
2764 LASSERT(handle != NULL);
2766 oh = container_of(handle, struct osd_thandle, ot_super);
2767 LASSERT(oh->ot_handle != NULL);
2768 LASSERT(oh->ot_handle->h_transaction != NULL);
2770 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2773 dentry = osd_child_dentry_get(env, obj,
2774 (char *)key, strlen((char *)key));
2776 cfs_down_write(&obj->oo_ext_idx_sem);
2777 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
2779 rc = ldiskfs_delete_entry(oh->ot_handle,
2785 cfs_up_write(&obj->oo_ext_idx_sem);
2786 LASSERT(osd_invariant(obj));
2791 * Lookup index for \a key and copy record to \a rec.
2793 * \param dt osd index object
2794 * \param key key for index
2795 * \param rec record reference
2797 * \retval +ve success : exact mach
2798 * \retval 0 return record with key not greater than \a key
2799 * \retval -ve failure
2801 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2802 struct dt_rec *rec, const struct dt_key *key,
2803 struct lustre_capa *capa)
2805 struct osd_object *obj = osd_dt_obj(dt);
2806 struct iam_path_descr *ipd;
2807 struct iam_container *bag = &obj->oo_dir->od_container;
2808 struct osd_thread_info *oti = osd_oti_get(env);
2809 struct iam_iterator *it = &oti->oti_idx_it;
2810 struct iam_rec *iam_rec;
2814 LASSERT(osd_invariant(obj));
2815 LASSERT(dt_object_exists(dt));
2816 LASSERT(bag->ic_object == obj->oo_inode);
2818 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2821 ipd = osd_idx_ipd_get(env, bag);
2825 /* got ipd now we can start iterator. */
2826 iam_it_init(it, bag, 0, ipd);
2828 rc = iam_it_get(it, (struct iam_key *)key);
2830 if (S_ISDIR(obj->oo_inode->i_mode))
2831 iam_rec = (struct iam_rec *)oti->oti_ldp;
2833 iam_rec = (struct iam_rec *) rec;
2835 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2836 if (S_ISDIR(obj->oo_inode->i_mode))
2837 osd_fid_unpack((struct lu_fid *) rec,
2838 (struct osd_fid_pack *)iam_rec);
2842 osd_ipd_put(env, bag, ipd);
2844 LINVRNT(osd_invariant(obj));
2850 * Inserts (key, value) pair in \a dt index object.
2852 * \param dt osd index object
2853 * \param key key for index
2854 * \param rec record reference
2855 * \param th transaction handler
2858 * \retval -ve failure
2860 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2861 const struct dt_rec *rec, const struct dt_key *key,
2862 struct thandle *th, struct lustre_capa *capa,
2865 struct osd_object *obj = osd_dt_obj(dt);
2866 struct iam_path_descr *ipd;
2867 struct osd_thandle *oh;
2868 struct iam_container *bag = &obj->oo_dir->od_container;
2869 #ifdef HAVE_QUOTA_SUPPORT
2870 cfs_cap_t save = cfs_curproc_cap_pack();
2872 struct osd_thread_info *oti = osd_oti_get(env);
2873 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2878 LINVRNT(osd_invariant(obj));
2879 LASSERT(dt_object_exists(dt));
2880 LASSERT(bag->ic_object == obj->oo_inode);
2881 LASSERT(th != NULL);
2883 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2886 ipd = osd_idx_ipd_get(env, bag);
2887 if (unlikely(ipd == NULL))
2890 oh = container_of0(th, struct osd_thandle, ot_super);
2891 LASSERT(oh->ot_handle != NULL);
2892 LASSERT(oh->ot_handle->h_transaction != NULL);
2893 #ifdef HAVE_QUOTA_SUPPORT
2895 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2897 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2899 if (S_ISDIR(obj->oo_inode->i_mode))
2900 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2902 iam_rec = (struct iam_rec *) rec;
2903 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2905 #ifdef HAVE_QUOTA_SUPPORT
2906 cfs_curproc_cap_unpack(save);
2908 osd_ipd_put(env, bag, ipd);
2909 LINVRNT(osd_invariant(obj));
2914 * Calls ldiskfs_add_entry() to add directory entry
2915 * into the directory. This is required for
2916 * interoperability mode (b11826)
2918 * \retval 0, on success
2919 * \retval -ve, on error
2921 static int __osd_ea_add_rec(struct osd_thread_info *info,
2922 struct osd_object *pobj,
2923 struct inode *cinode,
2925 const struct dt_rec *fid,
2928 struct ldiskfs_dentry_param *ldp;
2929 struct dentry *child;
2930 struct osd_thandle *oth;
2933 oth = container_of(th, struct osd_thandle, ot_super);
2934 LASSERT(oth->ot_handle != NULL);
2935 LASSERT(oth->ot_handle->h_transaction != NULL);
2937 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2939 if (fid_is_igif((struct lu_fid *)fid) ||
2940 fid_is_norm((struct lu_fid *)fid)) {
2941 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2942 osd_get_ldiskfs_dirent_param(ldp, fid);
2943 child->d_fsdata = (void*) ldp;
2945 child->d_fsdata = NULL;
2946 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2952 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2953 * into the directory.Also sets flags into osd object to
2954 * indicate dot and dotdot are created. This is required for
2955 * interoperability mode (b11826)
2957 * \param dir directory for dot and dotdot fixup.
2958 * \param obj child object for linking
2960 * \retval 0, on success
2961 * \retval -ve, on error
2963 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2964 struct osd_object *dir,
2965 struct inode *parent_dir, const char *name,
2966 const struct dt_rec *dot_fid,
2967 const struct dt_rec *dot_dot_fid,
2970 struct inode *inode = dir->oo_inode;
2971 struct ldiskfs_dentry_param *dot_ldp;
2972 struct ldiskfs_dentry_param *dot_dot_ldp;
2973 struct osd_thandle *oth;
2976 oth = container_of(th, struct osd_thandle, ot_super);
2977 LASSERT(oth->ot_handle->h_transaction != NULL);
2978 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2980 if (strcmp(name, dot) == 0) {
2981 if (dir->oo_compat_dot_created) {
2984 LASSERT(inode == parent_dir);
2985 dir->oo_compat_dot_created = 1;
2988 } else if(strcmp(name, dotdot) == 0) {
2989 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2990 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2992 if (!dir->oo_compat_dot_created)
2994 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
2995 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
2996 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3001 /* in case of rename, dotdot is already created */
3002 if (dir->oo_compat_dotdot_created) {
3003 return __osd_ea_add_rec(info, dir, parent_dir, name,
3007 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode,
3008 dot_ldp, dot_dot_ldp);
3010 dir->oo_compat_dotdot_created = 1;
3018 * It will call the appropriate osd_add* function and return the
3019 * value, return by respective functions.
3021 static int osd_ea_add_rec(const struct lu_env *env,
3022 struct osd_object *pobj,
3023 struct inode *cinode,
3025 const struct dt_rec *fid,
3028 struct osd_thread_info *info = osd_oti_get(env);
3031 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3033 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3034 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3037 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid, th);
3043 * Calls ->lookup() to find dentry. From dentry get inode and
3044 * read inode's ea to get fid. This is required for interoperability
3047 * \retval 0, on success
3048 * \retval -ve, on error
3050 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3051 struct dt_rec *rec, const struct dt_key *key)
3053 struct inode *dir = obj->oo_inode;
3054 struct dentry *dentry;
3055 struct ldiskfs_dir_entry_2 *de;
3056 struct buffer_head *bh;
3057 struct lu_fid *fid = (struct lu_fid *) rec;
3061 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3063 dentry = osd_child_dentry_get(env, obj,
3064 (char *)key, strlen((char *)key));
3066 cfs_down_read(&obj->oo_ext_idx_sem);
3067 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
3069 ino = le32_to_cpu(de->inode);
3070 rc = osd_get_fid_from_dentry(de, rec);
3072 /* done with de, release bh */
3075 rc = osd_ea_fid_get(env, obj, ino, fid);
3079 cfs_up_read(&obj->oo_ext_idx_sem);
3084 * Find the osd object for given fid.
3086 * \param fid need to find the osd object having this fid
3088 * \retval osd_object on success
3089 * \retval -ve on error
3091 struct osd_object *osd_object_find(const struct lu_env *env,
3092 struct dt_object *dt,
3093 const struct lu_fid *fid)
3095 struct lu_device *ludev = dt->do_lu.lo_dev;
3096 struct osd_object *child = NULL;
3097 struct lu_object *luch;
3098 struct lu_object *lo;
3100 luch = lu_object_find(env, ludev, fid, NULL);
3101 if (!IS_ERR(luch)) {
3102 if (lu_object_exists(luch)) {
3103 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3105 child = osd_obj(lo);
3107 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3108 "lu_object can't be located"
3109 ""DFID"\n", PFID(fid));
3111 if (child == NULL) {
3112 lu_object_put(env, luch);
3113 CERROR("Unable to get osd_object\n");
3114 child = ERR_PTR(-ENOENT);
3117 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3118 "lu_object does not exists "DFID"\n",
3120 child = ERR_PTR(-ENOENT);
3123 child = (void *)luch;
3129 * Put the osd object once done with it.
3131 * \param obj osd object that needs to be put
3133 static inline void osd_object_put(const struct lu_env *env,
3134 struct osd_object *obj)
3136 lu_object_put(env, &obj->oo_dt.do_lu);
3140 * Index add function for interoperability mode (b11826).
3141 * It will add the directory entry.This entry is needed to
3142 * maintain name->fid mapping.
3144 * \param key it is key i.e. file entry to be inserted
3145 * \param rec it is value of given key i.e. fid
3147 * \retval 0, on success
3148 * \retval -ve, on error
3150 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3151 const struct dt_rec *rec,
3152 const struct dt_key *key, struct thandle *th,
3153 struct lustre_capa *capa, int ignore_quota)
3155 struct osd_object *obj = osd_dt_obj(dt);
3156 struct lu_fid *fid = (struct lu_fid *) rec;
3157 const char *name = (const char *)key;
3158 struct osd_object *child;
3159 #ifdef HAVE_QUOTA_SUPPORT
3160 cfs_cap_t save = cfs_curproc_cap_pack();
3166 LASSERT(osd_invariant(obj));
3167 LASSERT(dt_object_exists(dt));
3168 LASSERT(th != NULL);
3170 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3173 child = osd_object_find(env, dt, fid);
3174 if (!IS_ERR(child)) {
3175 #ifdef HAVE_QUOTA_SUPPORT
3177 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3179 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3181 cfs_down_write(&obj->oo_ext_idx_sem);
3182 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3183 cfs_up_write(&obj->oo_ext_idx_sem);
3184 #ifdef HAVE_QUOTA_SUPPORT
3185 cfs_curproc_cap_unpack(save);
3187 osd_object_put(env, child);
3189 rc = PTR_ERR(child);
3192 LASSERT(osd_invariant(obj));
3197 * Initialize osd Iterator for given osd index object.
3199 * \param dt osd index object
3202 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3203 struct dt_object *dt,
3205 struct lustre_capa *capa)
3207 struct osd_it_iam *it;
3208 struct osd_thread_info *oti = osd_oti_get(env);
3209 struct osd_object *obj = osd_dt_obj(dt);
3210 struct lu_object *lo = &dt->do_lu;
3211 struct iam_path_descr *ipd;
3212 struct iam_container *bag = &obj->oo_dir->od_container;
3214 LASSERT(lu_object_exists(lo));
3216 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3217 return ERR_PTR(-EACCES);
3220 ipd = osd_it_ipd_get(env, bag);
3221 if (likely(ipd != NULL)) {
3225 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3226 return (struct dt_it *)it;
3228 return ERR_PTR(-ENOMEM);
3232 * free given Iterator.
3235 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3237 struct osd_it_iam *it = (struct osd_it_iam *)di;
3238 struct osd_object *obj = it->oi_obj;
3240 iam_it_fini(&it->oi_it);
3241 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3242 lu_object_put(env, &obj->oo_dt.do_lu);
3246 * Move Iterator to record specified by \a key
3248 * \param di osd iterator
3249 * \param key key for index
3251 * \retval +ve di points to record with least key not larger than key
3252 * \retval 0 di points to exact matched key
3253 * \retval -ve failure
3256 static int osd_it_iam_get(const struct lu_env *env,
3257 struct dt_it *di, const struct dt_key *key)
3259 struct osd_it_iam *it = (struct osd_it_iam *)di;
3261 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3267 * \param di osd iterator
3270 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3272 struct osd_it_iam *it = (struct osd_it_iam *)di;
3274 iam_it_put(&it->oi_it);
3278 * Move iterator by one record
3280 * \param di osd iterator
3282 * \retval +1 end of container reached
3284 * \retval -ve failure
3287 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3289 struct osd_it_iam *it = (struct osd_it_iam *)di;
3291 return iam_it_next(&it->oi_it);
3295 * Return pointer to the key under iterator.
3298 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3299 const struct dt_it *di)
3301 struct osd_it_iam *it = (struct osd_it_iam *)di;
3303 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3307 * Return size of key under iterator (in bytes)
3310 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3312 struct osd_it_iam *it = (struct osd_it_iam *)di;
3314 return iam_it_key_size(&it->oi_it);
3317 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3322 struct luda_type *lt;
3323 const unsigned align = sizeof(struct luda_type) - 1;
3325 /* check if file type is required */
3326 if (attr & LUDA_TYPE) {
3327 len = (len + align) & ~align;
3329 lt = (void *) ent->lde_name + len;
3330 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3331 ent->lde_attrs |= LUDA_TYPE;
3334 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3338 * build lu direct from backend fs dirent.
3341 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3349 fid_cpu_to_le(&ent->lde_fid, fid);
3350 ent->lde_attrs = LUDA_FID;
3352 ent->lde_hash = cpu_to_le64(offset);
3353 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3355 strncpy(ent->lde_name, name, namelen);
3356 ent->lde_namelen = cpu_to_le16(namelen);
3358 /* append lustre attributes */
3359 osd_it_append_attrs(ent, attr, namelen, type);
3363 * Return pointer to the record under iterator.
3365 static int osd_it_iam_rec(const struct lu_env *env,
3366 const struct dt_it *di,
3367 struct lu_dirent *lde,
3370 struct osd_it_iam *it = (struct osd_it_iam *)di;
3371 struct osd_thread_info *info = osd_oti_get(env);
3372 struct lu_fid *fid = &info->oti_fid;
3373 const struct osd_fid_pack *rec;
3379 name = (char *)iam_it_key_get(&it->oi_it);
3381 RETURN(PTR_ERR(name));
3383 namelen = iam_it_key_size(&it->oi_it);
3385 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3387 RETURN(PTR_ERR(rec));
3389 rc = osd_fid_unpack(fid, rec);
3393 hash = iam_it_store(&it->oi_it);
3395 /* IAM does not store object type in IAM index (dir) */
3396 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3403 * Returns cookie for current Iterator position.
3405 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3407 struct osd_it_iam *it = (struct osd_it_iam *)di;
3409 return iam_it_store(&it->oi_it);
3413 * Restore iterator from cookie.
3415 * \param di osd iterator
3416 * \param hash Iterator location cookie
3418 * \retval +ve di points to record with least key not larger than key.
3419 * \retval 0 di points to exact matched key
3420 * \retval -ve failure
3423 static int osd_it_iam_load(const struct lu_env *env,
3424 const struct dt_it *di, __u64 hash)
3426 struct osd_it_iam *it = (struct osd_it_iam *)di;
3428 return iam_it_load(&it->oi_it, hash);
3431 static const struct dt_index_operations osd_index_iam_ops = {
3432 .dio_lookup = osd_index_iam_lookup,
3433 .dio_insert = osd_index_iam_insert,
3434 .dio_delete = osd_index_iam_delete,
3436 .init = osd_it_iam_init,
3437 .fini = osd_it_iam_fini,
3438 .get = osd_it_iam_get,
3439 .put = osd_it_iam_put,
3440 .next = osd_it_iam_next,
3441 .key = osd_it_iam_key,
3442 .key_size = osd_it_iam_key_size,
3443 .rec = osd_it_iam_rec,
3444 .store = osd_it_iam_store,
3445 .load = osd_it_iam_load
3450 * Creates or initializes iterator context.
3452 * \retval struct osd_it_ea, iterator structure on success
3455 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3456 struct dt_object *dt,
3458 struct lustre_capa *capa)
3460 struct osd_object *obj = osd_dt_obj(dt);
3461 struct osd_thread_info *info = osd_oti_get(env);
3462 struct osd_it_ea *it = &info->oti_it_ea;
3463 struct lu_object *lo = &dt->do_lu;
3464 struct dentry *obj_dentry = &info->oti_it_dentry;
3466 LASSERT(lu_object_exists(lo));
3468 obj_dentry->d_inode = obj->oo_inode;
3469 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3470 obj_dentry->d_name.hash = 0;
3472 it->oie_rd_dirent = 0;
3473 it->oie_it_dirent = 0;
3474 it->oie_dirent = NULL;
3475 it->oie_buf = info->oti_it_ea_buf;
3477 it->oie_file.f_pos = 0;
3478 it->oie_file.f_dentry = obj_dentry;
3479 if (attr & LUDA_64BITHASH)
3480 it->oie_file.f_flags = O_64BITHASH;
3482 it->oie_file.f_flags = O_32BITHASH;
3483 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3484 it->oie_file.f_op = obj->oo_inode->i_fop;
3485 it->oie_file.private_data = NULL;
3487 RETURN((struct dt_it *) it);
3491 * Destroy or finishes iterator context.
3493 * \param di iterator structure to be destroyed
3495 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3497 struct osd_it_ea *it = (struct osd_it_ea *)di;
3498 struct osd_object *obj = it->oie_obj;
3499 struct inode *inode = obj->oo_inode;
3502 it->oie_file.f_op->release(inode, &it->oie_file);
3503 lu_object_put(env, &obj->oo_dt.do_lu);
3508 * It position the iterator at given key, so that next lookup continues from
3509 * that key Or it is similar to dio_it->load() but based on a key,
3510 * rather than file position.
3512 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3515 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3517 static int osd_it_ea_get(const struct lu_env *env,
3518 struct dt_it *di, const struct dt_key *key)
3520 struct osd_it_ea *it = (struct osd_it_ea *)di;
3523 LASSERT(((const char *)key)[0] == '\0');
3524 it->oie_file.f_pos = 0;
3525 it->oie_rd_dirent = 0;
3526 it->oie_it_dirent = 0;
3527 it->oie_dirent = NULL;
3535 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3540 * It is called internally by ->readdir(). It fills the
3541 * iterator's in-memory data structure with required
3542 * information i.e. name, namelen, rec_size etc.
3544 * \param buf in which information to be filled in.
3545 * \param name name of the file in given dir
3547 * \retval 0 on success
3548 * \retval 1 on buffer full
3550 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3551 loff_t offset, __u64 ino,
3554 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3555 struct osd_it_ea_dirent *ent = it->oie_dirent;
3556 struct lu_fid *fid = &ent->oied_fid;
3557 struct osd_fid_pack *rec;
3560 /* this should never happen */
3561 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3562 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3566 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3570 if (d_type & LDISKFS_DIRENT_LUFID) {
3571 rec = (struct osd_fid_pack*) (name + namelen + 1);
3573 if (osd_fid_unpack(fid, rec) != 0)
3576 d_type &= ~LDISKFS_DIRENT_LUFID;
3581 ent->oied_ino = ino;
3582 ent->oied_off = offset;
3583 ent->oied_namelen = namelen;
3584 ent->oied_type = d_type;
3586 memcpy(ent->oied_name, name, namelen);
3588 it->oie_rd_dirent++;
3589 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3594 * Calls ->readdir() to load a directory entry at a time
3595 * and stored it in iterator's in-memory data structure.
3597 * \param di iterator's in memory structure
3599 * \retval 0 on success
3600 * \retval -ve on error
3602 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3604 struct osd_it_ea *it = (struct osd_it_ea *)di;
3605 struct osd_object *obj = it->oie_obj;
3606 struct inode *inode = obj->oo_inode;
3610 it->oie_dirent = it->oie_buf;
3611 it->oie_rd_dirent = 0;
3613 cfs_down_read(&obj->oo_ext_idx_sem);
3614 result = inode->i_fop->readdir(&it->oie_file, it,
3615 (filldir_t) osd_ldiskfs_filldir);
3617 cfs_up_read(&obj->oo_ext_idx_sem);
3619 if (it->oie_rd_dirent == 0) {
3622 it->oie_dirent = it->oie_buf;
3623 it->oie_it_dirent = 1;
3630 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3631 * to load a directory entry at a time and stored it in
3632 * iterator's in-memory data structure.
3634 * \param di iterator's in memory structure
3636 * \retval +ve iterator reached to end
3637 * \retval 0 iterator not reached to end
3638 * \retval -ve on error
3640 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3642 struct osd_it_ea *it = (struct osd_it_ea *)di;
3647 if (it->oie_it_dirent < it->oie_rd_dirent) {
3649 (void *) it->oie_dirent +
3650 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3651 it->oie_dirent->oied_namelen);
3652 it->oie_it_dirent++;
3655 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3658 rc = osd_ldiskfs_it_fill(di);
3665 * Returns the key at current position from iterator's in memory structure.
3667 * \param di iterator's in memory structure
3669 * \retval key i.e. struct dt_key on success
3671 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3672 const struct dt_it *di)
3674 struct osd_it_ea *it = (struct osd_it_ea *)di;
3676 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3680 * Returns the key's size at current position from iterator's in memory structure.
3682 * \param di iterator's in memory structure
3684 * \retval key_size i.e. struct dt_key on success
3686 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3688 struct osd_it_ea *it = (struct osd_it_ea *)di;
3690 RETURN(it->oie_dirent->oied_namelen);
3695 * Returns the value (i.e. fid/igif) at current position from iterator's
3696 * in memory structure.
3698 * \param di struct osd_it_ea, iterator's in memory structure
3699 * \param attr attr requested for dirent.
3700 * \param lde lustre dirent
3702 * \retval 0 no error and \param lde has correct lustre dirent.
3703 * \retval -ve on error
3705 static inline int osd_it_ea_rec(const struct lu_env *env,
3706 const struct dt_it *di,
3707 struct lu_dirent *lde,
3710 struct osd_it_ea *it = (struct osd_it_ea *)di;
3711 struct osd_object *obj = it->oie_obj;
3712 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3717 if (!fid_is_sane(fid))
3718 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3721 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3722 it->oie_dirent->oied_name,
3723 it->oie_dirent->oied_namelen,
3724 it->oie_dirent->oied_type,
3730 * Returns a cookie for current position of the iterator head, so that
3731 * user can use this cookie to load/start the iterator next time.
3733 * \param di iterator's in memory structure
3735 * \retval cookie for current position, on success
3737 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3739 struct osd_it_ea *it = (struct osd_it_ea *)di;
3741 RETURN(it->oie_dirent->oied_off);
3745 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3746 * to load a directory entry at a time and stored it i inn,
3747 * in iterator's in-memory data structure.
3749 * \param di struct osd_it_ea, iterator's in memory structure
3751 * \retval +ve on success
3752 * \retval -ve on error
3754 static int osd_it_ea_load(const struct lu_env *env,
3755 const struct dt_it *di, __u64 hash)
3757 struct osd_it_ea *it = (struct osd_it_ea *)di;
3761 it->oie_file.f_pos = hash;
3763 rc = osd_ldiskfs_it_fill(di);
3771 * Index lookup function for interoperability mode (b11826).
3773 * \param key, key i.e. file name to be searched
3775 * \retval +ve, on success
3776 * \retval -ve, on error
3778 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3779 struct dt_rec *rec, const struct dt_key *key,
3780 struct lustre_capa *capa)
3782 struct osd_object *obj = osd_dt_obj(dt);
3787 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3788 LINVRNT(osd_invariant(obj));
3790 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3793 rc = osd_ea_lookup_rec(env, obj, rec, key);
3801 * Index and Iterator operations for interoperability
3802 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3804 static const struct dt_index_operations osd_index_ea_ops = {
3805 .dio_lookup = osd_index_ea_lookup,
3806 .dio_insert = osd_index_ea_insert,
3807 .dio_delete = osd_index_ea_delete,
3809 .init = osd_it_ea_init,
3810 .fini = osd_it_ea_fini,
3811 .get = osd_it_ea_get,
3812 .put = osd_it_ea_put,
3813 .next = osd_it_ea_next,
3814 .key = osd_it_ea_key,
3815 .key_size = osd_it_ea_key_size,
3816 .rec = osd_it_ea_rec,
3817 .store = osd_it_ea_store,
3818 .load = osd_it_ea_load
3822 static void *osd_key_init(const struct lu_context *ctx,
3823 struct lu_context_key *key)
3825 struct osd_thread_info *info;
3827 OBD_ALLOC_PTR(info);
3829 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3830 if (info->oti_it_ea_buf != NULL) {
3831 info->oti_env = container_of(ctx, struct lu_env,
3835 info = ERR_PTR(-ENOMEM);
3838 info = ERR_PTR(-ENOMEM);
3843 static void osd_key_fini(const struct lu_context *ctx,
3844 struct lu_context_key *key, void* data)
3846 struct osd_thread_info *info = data;
3848 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3852 static void osd_key_exit(const struct lu_context *ctx,
3853 struct lu_context_key *key, void *data)
3855 struct osd_thread_info *info = data;
3857 LASSERT(info->oti_r_locks == 0);
3858 LASSERT(info->oti_w_locks == 0);
3859 LASSERT(info->oti_txns == 0);
3862 /* type constructor/destructor: osd_type_init, osd_type_fini */
3863 LU_TYPE_INIT_FINI(osd, &osd_key);
3865 static struct lu_context_key osd_key = {
3866 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3867 .lct_init = osd_key_init,
3868 .lct_fini = osd_key_fini,
3869 .lct_exit = osd_key_exit
3873 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3874 const char *name, struct lu_device *next)
3877 struct lu_context *ctx;
3879 /* context for commit hooks */
3880 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3881 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3883 rc = osd_procfs_init(osd_dev(d), name);
3884 ctx->lc_cookie = 0x3;
3889 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3891 struct osd_thread_info *info = osd_oti_get(env);
3893 if (o->od_obj_area != NULL) {
3894 lu_object_put(env, &o->od_obj_area->do_lu);
3895 o->od_obj_area = NULL;
3897 osd_oi_fini(info, &o->od_oi);
3902 static int osd_mount(const struct lu_env *env,
3903 struct osd_device *o, struct lustre_cfg *cfg)
3905 struct lustre_mount_info *lmi;
3906 const char *dev = lustre_cfg_string(cfg, 0);
3907 struct lustre_disk_data *ldd;
3908 struct lustre_sb_info *lsi;
3911 if (o->od_mount != NULL) {
3912 CERROR("Already mounted (%s)\n", dev);
3917 lmi = server_get_mount(dev);
3919 CERROR("Cannot get mount info for %s!\n", dev);
3923 LASSERT(lmi != NULL);
3924 /* save lustre_mount_info in dt_device */
3927 lsi = s2lsi(lmi->lmi_sb);
3930 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3932 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3936 o->od_obj_area = NULL;
3940 static struct lu_device *osd_device_fini(const struct lu_env *env,
3941 struct lu_device *d)
3946 shrink_dcache_sb(osd_sb(osd_dev(d)));
3947 osd_sync(env, lu2dt_dev(d));
3949 rc = osd_procfs_fini(osd_dev(d));
3951 CERROR("proc fini error %d \n", rc);
3952 RETURN (ERR_PTR(rc));
3955 if (osd_dev(d)->od_mount)
3956 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3957 osd_dev(d)->od_mount->lmi_mnt);
3958 osd_dev(d)->od_mount = NULL;
3960 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3964 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3965 struct lu_device_type *t,
3966 struct lustre_cfg *cfg)
3968 struct lu_device *l;
3969 struct osd_device *o;
3975 result = dt_device_init(&o->od_dt_dev, t);
3978 l->ld_ops = &osd_lu_ops;
3979 o->od_dt_dev.dd_ops = &osd_dt_ops;
3980 cfs_spin_lock_init(&o->od_osfs_lock);
3981 o->od_osfs_age = cfs_time_shift_64(-1000);
3982 o->od_capa_hash = init_capa_hash();
3983 if (o->od_capa_hash == NULL) {
3984 dt_device_fini(&o->od_dt_dev);
3985 l = ERR_PTR(-ENOMEM);
3988 l = ERR_PTR(result);
3993 l = ERR_PTR(-ENOMEM);
3997 static struct lu_device *osd_device_free(const struct lu_env *env,
3998 struct lu_device *d)
4000 struct osd_device *o = osd_dev(d);
4003 cleanup_capa_hash(o->od_capa_hash);
4004 dt_device_fini(&o->od_dt_dev);
4009 static int osd_process_config(const struct lu_env *env,
4010 struct lu_device *d, struct lustre_cfg *cfg)
4012 struct osd_device *o = osd_dev(d);
4016 switch(cfg->lcfg_command) {
4018 err = osd_mount(env, o, cfg);
4021 err = osd_shutdown(env, o);
4030 static int osd_recovery_complete(const struct lu_env *env,
4031 struct lu_device *d)
4036 static int osd_prepare(const struct lu_env *env,
4037 struct lu_device *pdev,
4038 struct lu_device *dev)
4040 struct osd_device *osd = osd_dev(dev);
4041 struct lustre_sb_info *lsi;
4042 struct lustre_disk_data *ldd;
4043 struct lustre_mount_info *lmi;
4044 struct osd_thread_info *oti = osd_oti_get(env);
4045 struct dt_object *d;
4049 /* 1. initialize oi before any file create or file open */
4050 result = osd_oi_init(oti, &osd->od_oi,
4051 &osd->od_dt_dev, lu2md_dev(pdev));
4055 lmi = osd->od_mount;
4056 lsi = s2lsi(lmi->lmi_sb);
4059 /* 2. setup local objects */
4060 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4064 /* 3. open remote object dir */
4065 d = dt_store_open(env, lu2dt_dev(dev), "",
4066 remote_obj_dir, &oti->oti_fid);
4068 osd->od_obj_area = d;
4071 result = PTR_ERR(d);
4072 osd->od_obj_area = NULL;
4079 static const struct lu_object_operations osd_lu_obj_ops = {
4080 .loo_object_init = osd_object_init,
4081 .loo_object_delete = osd_object_delete,
4082 .loo_object_release = osd_object_release,
4083 .loo_object_free = osd_object_free,
4084 .loo_object_print = osd_object_print,
4085 .loo_object_invariant = osd_object_invariant
4088 static const struct lu_device_operations osd_lu_ops = {
4089 .ldo_object_alloc = osd_object_alloc,
4090 .ldo_process_config = osd_process_config,
4091 .ldo_recovery_complete = osd_recovery_complete,
4092 .ldo_prepare = osd_prepare,
4095 static const struct lu_device_type_operations osd_device_type_ops = {
4096 .ldto_init = osd_type_init,
4097 .ldto_fini = osd_type_fini,
4099 .ldto_start = osd_type_start,
4100 .ldto_stop = osd_type_stop,
4102 .ldto_device_alloc = osd_device_alloc,
4103 .ldto_device_free = osd_device_free,
4105 .ldto_device_init = osd_device_init,
4106 .ldto_device_fini = osd_device_fini
4109 static struct lu_device_type osd_device_type = {
4110 .ldt_tags = LU_DEVICE_DT,
4111 .ldt_name = LUSTRE_OSD_NAME,
4112 .ldt_ops = &osd_device_type_ops,
4113 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4117 * lprocfs legacy support.
4119 static struct obd_ops osd_obd_device_ops = {
4120 .o_owner = THIS_MODULE
4123 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4124 .llod_name = remote_obj_dir,
4125 .llod_oid = OSD_REM_OBJ_DIR_OID,
4127 .llod_feat = &dt_directory_features,
4130 static int __init osd_mod_init(void)
4132 struct lprocfs_static_vars lvars;
4135 llo_local_obj_register(&llod_osd_rem_obj_dir);
4136 lprocfs_osd_init_vars(&lvars);
4137 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4138 LUSTRE_OSD_NAME, &osd_device_type);
4141 static void __exit osd_mod_exit(void)
4143 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4144 class_unregister_type(LUSTRE_OSD_NAME);
4147 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4148 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4149 MODULE_LICENSE("GPL");
4151 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);