1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include <linux/module.h>
51 /* LUSTRE_VERSION_CODE */
52 #include <lustre_ver.h>
53 /* prerequisite for linux/xattr.h */
54 #include <linux/types.h>
55 /* prerequisite for linux/xattr.h */
57 /* XATTR_{REPLACE,CREATE} */
58 #include <linux/xattr.h>
63 * struct OBD_{ALLOC,FREE}*()
66 #include <obd_support.h>
67 /* struct ptlrpc_thread */
68 #include <lustre_net.h>
71 #include <lustre_fid.h>
73 #include "osd_internal.h"
76 /* llo_* api support */
77 #include <md_object.h>
79 static const char dot[] = ".";
80 static const char dotdot[] = "..";
81 static const char remote_obj_dir[] = "REM_OBJ_DIR";
83 struct osd_directory {
84 struct iam_container od_container;
85 struct iam_descr od_descr;
89 struct dt_object oo_dt;
91 * Inode for file system object represented by this osd_object. This
92 * inode is pinned for the whole duration of lu_object life.
94 * Not modified concurrently (either setup early during object
95 * creation, or assigned by osd_object_create() under write lock).
97 struct inode *oo_inode;
99 * to protect index ops.
101 cfs_rw_semaphore_t oo_ext_idx_sem;
102 cfs_rw_semaphore_t oo_sem;
103 struct osd_directory *oo_dir;
104 /** protects inode attributes. */
105 cfs_spinlock_t oo_guard;
107 * Following two members are used to indicate the presence of dot and
108 * dotdot in the given directory. This is required for interop mode
111 int oo_compat_dot_created;
112 int oo_compat_dotdot_created;
114 const struct lu_env *oo_owner;
115 #ifdef CONFIG_LOCKDEP
116 struct lockdep_map oo_dep_map;
120 static const struct lu_object_operations osd_lu_obj_ops;
121 static const struct lu_device_operations osd_lu_ops;
122 static struct lu_context_key osd_key;
123 static const struct dt_object_operations osd_obj_ops;
124 static const struct dt_object_operations osd_obj_ea_ops;
125 static const struct dt_body_operations osd_body_ops;
126 static const struct dt_index_operations osd_index_iam_ops;
127 static const struct dt_index_operations osd_index_ea_ops;
130 struct thandle ot_super;
132 struct journal_callback ot_jcb;
133 /* Link to the device, for debugging. */
134 struct lu_ref_link *ot_dev_link;
136 #if OSD_THANDLE_STATS
137 /** time when this handle was allocated */
138 cfs_time_t oth_alloced;
140 /** time when this thanle was started */
141 cfs_time_t oth_started;
148 static int lu_device_is_osd(const struct lu_device *d)
150 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
153 static struct osd_device *osd_dt_dev(const struct dt_device *d)
155 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
156 return container_of0(d, struct osd_device, od_dt_dev);
159 static struct osd_device *osd_dev(const struct lu_device *d)
161 LASSERT(lu_device_is_osd(d));
162 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
165 static struct osd_device *osd_obj2dev(const struct osd_object *o)
167 return osd_dev(o->oo_dt.do_lu.lo_dev);
170 static struct super_block *osd_sb(const struct osd_device *dev)
172 return dev->od_mount->lmi_mnt->mnt_sb;
175 static int osd_object_is_root(const struct osd_object *obj)
177 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
180 static struct osd_object *osd_obj(const struct lu_object *o)
182 LASSERT(lu_device_is_osd(o->lo_dev));
183 return container_of0(o, struct osd_object, oo_dt.do_lu);
186 static struct osd_object *osd_dt_obj(const struct dt_object *d)
188 return osd_obj(&d->do_lu);
191 static struct lu_device *osd2lu_dev(struct osd_device *osd)
193 return &osd->od_dt_dev.dd_lu_dev;
196 static journal_t *osd_journal(const struct osd_device *dev)
198 return LDISKFS_SB(osd_sb(dev))->s_journal;
201 static int osd_has_index(const struct osd_object *obj)
203 return obj->oo_dt.do_index_ops != NULL;
206 static int osd_object_invariant(const struct lu_object *l)
208 return osd_invariant(osd_obj(l));
211 #ifdef HAVE_QUOTA_SUPPORT
213 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
215 struct md_ucred *uc = md_ucred(env);
220 save->oc_uid = current_fsuid();
221 save->oc_gid = current_fsgid();
222 save->oc_cap = current_cap();
223 if ((tc = prepare_creds())) {
224 tc->fsuid = uc->mu_fsuid;
225 tc->fsgid = uc->mu_fsgid;
226 tc->cap_effective = uc->mu_cap;
232 osd_pop_ctxt(struct osd_ctxt *save)
236 if ((tc = prepare_creds())) {
237 tc->fsuid = save->oc_uid;
238 tc->fsgid = save->oc_gid;
239 tc->cap_effective = save->oc_cap;
245 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
247 return lu_context_key_get(&env->le_ctx, &osd_key);
251 * Concurrency: doesn't matter
253 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
255 return osd_oti_get(env)->oti_r_locks > 0;
259 * Concurrency: doesn't matter
261 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
263 struct osd_thread_info *oti = osd_oti_get(env);
264 return oti->oti_w_locks > 0 && o->oo_owner == env;
268 * Concurrency: doesn't access mutable data
270 static int osd_root_get(const struct lu_env *env,
271 struct dt_device *dev, struct lu_fid *f)
275 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
276 LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
281 * OSD object methods.
285 * Concurrency: no concurrent access is possible that early in object
288 static struct lu_object *osd_object_alloc(const struct lu_env *env,
289 const struct lu_object_header *hdr,
292 struct osd_object *mo;
298 l = &mo->oo_dt.do_lu;
299 dt_object_init(&mo->oo_dt, NULL, d);
300 if (osd_dev(d)->od_iop_mode)
301 mo->oo_dt.do_ops = &osd_obj_ea_ops;
303 mo->oo_dt.do_ops = &osd_obj_ops;
305 l->lo_ops = &osd_lu_obj_ops;
306 cfs_init_rwsem(&mo->oo_sem);
307 cfs_init_rwsem(&mo->oo_ext_idx_sem);
308 cfs_spin_lock_init(&mo->oo_guard);
315 * retrieve object from backend ext fs.
317 static struct inode *osd_iget(struct osd_thread_info *info,
318 struct osd_device *dev,
319 const struct osd_inode_id *id)
321 struct inode *inode = NULL;
323 #ifdef HAVE_EXT4_LDISKFS
324 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
326 /* Newer kernels return an error instead of a NULL pointer */
329 inode = iget(osd_sb(dev), id->oii_ino);
332 CERROR("no inode\n");
333 inode = ERR_PTR(-EACCES);
334 } else if (id->oii_gen != OSD_OII_NOGEN &&
335 inode->i_generation != id->oii_gen) {
337 inode = ERR_PTR(-ESTALE);
338 } else if (inode->i_nlink == 0) {
339 /* due to parallel readdir and unlink,
340 * we can have dead inode here. */
341 CWARN("stale inode\n");
342 make_bad_inode(inode);
344 inode = ERR_PTR(-ESTALE);
345 } else if (is_bad_inode(inode)) {
346 CERROR("bad inode %lx\n",inode->i_ino);
348 inode = ERR_PTR(-ENOENT);
353 static int osd_fid_lookup(const struct lu_env *env,
354 struct osd_object *obj, const struct lu_fid *fid)
356 struct osd_thread_info *info;
357 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
358 struct osd_device *dev;
359 struct osd_inode_id *id;
364 LINVRNT(osd_invariant(obj));
365 LASSERT(obj->oo_inode == NULL);
366 LASSERT(fid_is_sane(fid) || osd_fid_is_root(fid));
368 * This assertion checks that osd layer sees only local
369 * fids. Unfortunately it is somewhat expensive (does a
370 * cache-lookup). Disabling it for production/acceptance-testing.
372 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
376 info = osd_oti_get(env);
381 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
384 result = osd_oi_lookup(info, oi, fid, id);
386 inode = osd_iget(info, dev, id);
387 if (!IS_ERR(inode)) {
388 obj->oo_inode = inode;
389 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
390 if (dev->od_iop_mode) {
391 obj->oo_compat_dot_created = 1;
392 obj->oo_compat_dotdot_created = 1;
397 * If fid wasn't found in oi, inode-less object is
398 * created, for which lu_object_exists() returns
399 * false. This is used in a (frequent) case when
400 * objects are created as locking anchors or
401 * place holders for objects yet to be created.
403 result = PTR_ERR(inode);
404 } else if (result == -ENOENT)
406 LINVRNT(osd_invariant(obj));
412 * Concurrency: shouldn't matter.
414 static void osd_object_init0(struct osd_object *obj)
416 LASSERT(obj->oo_inode != NULL);
417 obj->oo_dt.do_body_ops = &osd_body_ops;
418 obj->oo_dt.do_lu.lo_header->loh_attr |=
419 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
423 * Concurrency: no concurrent access is possible that early in object
426 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
427 const struct lu_object_conf *unused)
429 struct osd_object *obj = osd_obj(l);
432 LINVRNT(osd_invariant(obj));
434 result = osd_fid_lookup(env, obj, lu_object_fid(l));
436 if (obj->oo_inode != NULL)
437 osd_object_init0(obj);
439 LINVRNT(osd_invariant(obj));
444 * Concurrency: no concurrent access is possible that late in object
447 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
449 struct osd_object *obj = osd_obj(l);
451 LINVRNT(osd_invariant(obj));
453 dt_object_fini(&obj->oo_dt);
460 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
461 const struct iam_container *bag)
463 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
464 osd_oti_get(env)->oti_it_ipd);
467 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
468 const struct iam_container *bag)
470 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
471 osd_oti_get(env)->oti_idx_ipd);
474 static void osd_ipd_put(const struct lu_env *env,
475 const struct iam_container *bag,
476 struct iam_path_descr *ipd)
478 bag->ic_descr->id_ops->id_ipd_free(ipd);
482 * Concurrency: no concurrent access is possible that late in object
485 static void osd_index_fini(struct osd_object *o)
487 struct iam_container *bag;
489 if (o->oo_dir != NULL) {
490 bag = &o->oo_dir->od_container;
491 if (o->oo_inode != NULL) {
492 if (bag->ic_object == o->oo_inode)
493 iam_container_fini(bag);
495 OBD_FREE_PTR(o->oo_dir);
501 * Concurrency: no concurrent access is possible that late in object
502 * life-cycle (for all existing callers, that is. New callers have to provide
503 * their own locking.)
505 static int osd_inode_unlinked(const struct inode *inode)
507 return inode->i_nlink == 0;
511 OSD_TXN_OI_DELETE_CREDITS = 20,
512 OSD_TXN_INODE_DELETE_CREDITS = 20
519 #if OSD_THANDLE_STATS
521 * Set time when the handle is allocated
523 static void osd_th_alloced(struct osd_thandle *oth)
525 oth->oth_alloced = cfs_time_current();
529 * Set time when the handle started
531 static void osd_th_started(struct osd_thandle *oth)
533 oth->oth_started = cfs_time_current();
537 * Helper function to convert time interval to microseconds packed in
538 * long int (default time units for the counter in "stats" initialized
539 * by lu_time_init() )
541 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
545 cfs_duration_usec(cfs_time_sub(end, start), &val);
546 return val.tv_sec * 1000000 + val.tv_usec;
550 * Check whether the we deal with this handle for too long.
552 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
553 cfs_time_t alloced, cfs_time_t started,
556 cfs_time_t now = cfs_time_current();
558 LASSERT(dev != NULL);
560 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
561 interval_to_usec(alloced, started));
562 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
563 interval_to_usec(started, closed));
564 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
565 interval_to_usec(closed, now));
567 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
568 CWARN("transaction handle %p was open for too long: "
570 "alloced "CFS_TIME_T" ,"
571 "started "CFS_TIME_T" ,"
572 "closed "CFS_TIME_T"\n",
573 oth, now, alloced, started, closed);
574 libcfs_debug_dumpstack(NULL);
578 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
580 cfs_time_t __closed = cfs_time_current(); \
581 cfs_time_t __alloced = oth->oth_alloced; \
582 cfs_time_t __started = oth->oth_started; \
585 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
588 #else /* OSD_THANDLE_STATS */
590 #define osd_th_alloced(h) do {} while(0)
591 #define osd_th_started(h) do {} while(0)
592 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
594 #endif /* OSD_THANDLE_STATS */
597 * Concurrency: doesn't access mutable data.
599 static int osd_param_is_sane(const struct osd_device *dev,
600 const struct txn_param *param)
602 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
606 * Concurrency: shouldn't matter.
608 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
610 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
611 struct thandle *th = &oh->ot_super;
612 struct dt_device *dev = th->th_dev;
613 struct lu_device *lud = &dev->dd_lu_dev;
615 LASSERT(dev != NULL);
616 LASSERT(oh->ot_handle == NULL);
619 CERROR("transaction @0x%p commit error: %d\n", th, error);
621 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
623 * This od_env_for_commit is only for commit usage. see
626 lu_context_enter(&env->le_ctx);
627 dt_txn_hook_commit(env, th);
628 lu_context_exit(&env->le_ctx);
631 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
635 lu_context_exit(&th->th_ctx);
636 lu_context_fini(&th->th_ctx);
641 * Concurrency: shouldn't matter.
643 static struct thandle *osd_trans_start(const struct lu_env *env,
647 struct osd_device *dev = osd_dt_dev(d);
649 struct osd_thandle *oh;
655 hook_res = dt_txn_hook_start(env, d, p);
657 RETURN(ERR_PTR(hook_res));
659 if (osd_param_is_sane(dev, p)) {
660 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
662 struct osd_thread_info *oti = osd_oti_get(env);
665 * XXX temporary stuff. Some abstraction layer should
670 jh = ldiskfs_journal_start_sb(osd_sb(dev), p->tp_credits);
677 jh->h_sync = p->tp_sync;
678 lu_device_get(&d->dd_lu_dev);
679 oh->ot_dev_link = lu_ref_add
680 (&d->dd_lu_dev.ld_reference,
682 /* add commit callback */
683 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
684 lu_context_enter(&th->th_ctx);
685 osd_journal_callback_set(jh, osd_trans_commit_cb,
686 (struct journal_callback *)&oh->ot_jcb);
687 LASSERT(oti->oti_txns == 0);
688 LASSERT(oti->oti_r_locks == 0);
689 LASSERT(oti->oti_w_locks == 0);
696 th = ERR_PTR(-ENOMEM);
698 CERROR("Invalid transaction parameters\n");
699 th = ERR_PTR(-EINVAL);
706 * Concurrency: shouldn't matter.
708 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
711 struct osd_thandle *oh;
712 struct osd_thread_info *oti = osd_oti_get(env);
716 oh = container_of0(th, struct osd_thandle, ot_super);
717 if (oh->ot_handle != NULL) {
718 handle_t *hdl = oh->ot_handle;
720 LASSERT(oti->oti_txns == 1);
722 LASSERT(oti->oti_r_locks == 0);
723 LASSERT(oti->oti_w_locks == 0);
724 result = dt_txn_hook_stop(env, th);
726 CERROR("Failure in transaction hook: %d\n", result);
727 oh->ot_handle = NULL;
728 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
729 result = ldiskfs_journal_stop(hdl));
731 CERROR("Failure to stop transaction: %d\n", result);
737 * Concurrency: no concurrent access is possible that late in object
740 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
742 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
743 struct osd_device *osd = osd_obj2dev(obj);
744 struct osd_thread_info *oti = osd_oti_get(env);
745 struct txn_param *prm = &oti->oti_txn;
746 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
750 lu_env_init(env_del_obj, LCT_DT_THREAD);
751 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
752 OSD_TXN_INODE_DELETE_CREDITS);
753 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
755 result = osd_oi_delete(osd_oti_get(env_del_obj),
756 &osd->od_oi, fid, th);
757 osd_trans_stop(env_del_obj, th);
759 result = PTR_ERR(th);
761 lu_env_fini(env_del_obj);
766 * Called just before object is freed. Releases all resources except for
767 * object itself (that is released by osd_object_free()).
769 * Concurrency: no concurrent access is possible that late in object
772 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
774 struct osd_object *obj = osd_obj(l);
775 struct inode *inode = obj->oo_inode;
777 LINVRNT(osd_invariant(obj));
780 * If object is unlinked remove fid->ino mapping from object index.
787 if (osd_inode_unlinked(inode)) {
788 result = osd_inode_remove(env, obj);
790 LU_OBJECT_DEBUG(D_ERROR, env, l,
791 "Failed to cleanup: %d\n",
796 obj->oo_inode = NULL;
801 * Concurrency: ->loo_object_release() is called under site spin-lock.
803 static void osd_object_release(const struct lu_env *env,
806 struct osd_object *o = osd_obj(l);
808 LASSERT(!lu_object_is_dying(l->lo_header));
809 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
810 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
814 * Concurrency: shouldn't matter.
816 static int osd_object_print(const struct lu_env *env, void *cookie,
817 lu_printer_t p, const struct lu_object *l)
819 struct osd_object *o = osd_obj(l);
822 if (o->oo_dir != NULL)
823 d = o->oo_dir->od_container.ic_descr;
826 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
828 o->oo_inode ? o->oo_inode->i_ino : 0UL,
829 o->oo_inode ? o->oo_inode->i_generation : 0,
830 d ? d->id_ops->id_name : "plain");
834 * Concurrency: shouldn't matter.
836 int osd_statfs(const struct lu_env *env, struct dt_device *d,
839 struct osd_device *osd = osd_dt_dev(d);
840 struct super_block *sb = osd_sb(osd);
843 cfs_spin_lock(&osd->od_osfs_lock);
845 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
846 result = ll_do_statfs(sb, &osd->od_kstatfs);
847 if (likely(result == 0)) /* N.B. statfs can't really fail */
848 osd->od_osfs_age = cfs_time_current_64();
851 if (likely(result == 0))
852 *sfs = osd->od_kstatfs;
853 cfs_spin_unlock(&osd->od_osfs_lock);
859 * Concurrency: doesn't access mutable data.
861 static void osd_conf_get(const struct lu_env *env,
862 const struct dt_device *dev,
863 struct dt_device_param *param)
866 * XXX should be taken from not-yet-existing fs abstraction layer.
868 param->ddp_max_name_len = LDISKFS_NAME_LEN;
869 param->ddp_max_nlink = LDISKFS_LINK_MAX;
870 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
874 * Helper function to get and fill the buffer with input values.
876 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
880 buf = &osd_oti_get(env)->oti_buf;
887 * Concurrency: shouldn't matter.
889 static int osd_sync(const struct lu_env *env, struct dt_device *d)
891 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
892 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
896 * Start commit for OSD device.
898 * An implementation of dt_commit_async method for OSD device.
899 * Asychronously starts underlayng fs sync and thereby a transaction
902 * \param env environment
905 * \see dt_device_operations
907 static int osd_commit_async(const struct lu_env *env,
910 struct super_block *s = osd_sb(osd_dt_dev(d));
913 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
914 RETURN(s->s_op->sync_fs(s, 0));
918 * Concurrency: shouldn't matter.
920 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
922 static void osd_ro(const struct lu_env *env, struct dt_device *d)
926 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
928 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
929 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
935 * Concurrency: serialization provided by callers.
937 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
938 int mode, unsigned long timeout, __u32 alg,
939 struct lustre_capa_key *keys)
941 struct osd_device *dev = osd_dt_dev(d);
944 dev->od_fl_capa = mode;
945 dev->od_capa_timeout = timeout;
946 dev->od_capa_alg = alg;
947 dev->od_capa_keys = keys;
952 * Concurrency: serialization provided by callers.
954 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
955 struct dt_quota_ctxt *ctxt, void *data)
957 struct obd_device *obd = (void *)ctxt;
958 struct vfsmount *mnt = (struct vfsmount *)data;
961 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
962 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
963 obd->obd_lvfs_ctxt.pwdmnt = mnt;
964 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
965 obd->obd_lvfs_ctxt.fs = get_ds();
971 * Note: we do not count into QUOTA here.
972 * If we mount with --data_journal we may need more.
974 static const int osd_dto_credits_noquota[DTO_NR] = {
977 * INDEX_EXTRA_TRANS_BLOCKS(8) +
978 * SINGLEDATA_TRANS_BLOCKS(8)
979 * XXX Note: maybe iam need more, since iam have more level than
982 [DTO_INDEX_INSERT] = 16,
983 [DTO_INDEX_DELETE] = 16,
987 [DTO_IDNEX_UPDATE] = 16,
989 * Create a object. The same as create object in EXT3.
990 * DATA_TRANS_BLOCKS(14) +
991 * INDEX_EXTRA_BLOCKS(8) +
992 * 3(inode bits, groups, GDT)
994 [DTO_OBJECT_CREATE] = 25,
998 [DTO_OBJECT_DELETE] = 25,
1001 * 3(inode bits, group, GDT)
1003 [DTO_ATTR_SET_BASE] = 3,
1005 * Xattr set. The same as xattr of EXT3.
1006 * DATA_TRANS_BLOCKS(14)
1007 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1008 * are also counted in. Do not know why?
1010 [DTO_XATTR_SET] = 14,
1013 * creadits for inode change during write.
1015 [DTO_WRITE_BASE] = 3,
1017 * credits for single block write.
1019 [DTO_WRITE_BLOCK] = 14,
1021 * Attr set credits for chown.
1022 * This is extra credits for setattr, and it is null without quota
1024 [DTO_ATTR_SET_CHOWN]= 0
1028 * Note: we count into QUOTA here.
1029 * If we mount with --data_journal we may need more.
1031 static const int osd_dto_credits_quota[DTO_NR] = {
1033 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1034 * SINGLEDATA_TRANS_BLOCKS(8) +
1035 * 2 * QUOTA_TRANS_BLOCKS(2)
1037 [DTO_INDEX_INSERT] = 20,
1039 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1040 * SINGLEDATA_TRANS_BLOCKS(8) +
1041 * 2 * QUOTA_TRANS_BLOCKS(2)
1043 [DTO_INDEX_DELETE] = 20,
1047 [DTO_IDNEX_UPDATE] = 16,
1049 * Create a object. Same as create object in EXT3 filesystem.
1050 * DATA_TRANS_BLOCKS(16) +
1051 * INDEX_EXTRA_BLOCKS(8) +
1052 * 3(inode bits, groups, GDT) +
1053 * 2 * QUOTA_INIT_BLOCKS(25)
1055 [DTO_OBJECT_CREATE] = 77,
1058 * DATA_TRANS_BLOCKS(16) +
1059 * INDEX_EXTRA_BLOCKS(8) +
1060 * 3(inode bits, groups, GDT) +
1063 [DTO_OBJECT_DELETE] = 27,
1066 * 3 (inode bit, group, GDT) +
1068 [DTO_ATTR_SET_BASE] = 3,
1070 * Xattr set. The same as xattr of EXT3.
1071 * DATA_TRANS_BLOCKS(16)
1072 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
1073 * also counted in. Do not know why?
1075 [DTO_XATTR_SET] = 16,
1078 * creadits for inode change during write.
1080 [DTO_WRITE_BASE] = 3,
1082 * credits for single block write.
1084 [DTO_WRITE_BLOCK] = 16,
1086 * Attr set credits for chown.
1087 * It is added to already set setattr credits
1088 * 2 * QUOTA_INIT_BLOCKS(25) +
1089 * 2 * QUOTA_DEL_BLOCKS(9)
1091 [DTO_ATTR_SET_CHOWN]= 68,
1094 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1097 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1098 ARRAY_SIZE(osd_dto_credits_quota));
1099 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1100 #ifdef HAVE_QUOTA_SUPPORT
1101 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1102 return osd_dto_credits_quota[op];
1105 return osd_dto_credits_noquota[op];
1108 static const struct dt_device_operations osd_dt_ops = {
1109 .dt_root_get = osd_root_get,
1110 .dt_statfs = osd_statfs,
1111 .dt_trans_start = osd_trans_start,
1112 .dt_trans_stop = osd_trans_stop,
1113 .dt_conf_get = osd_conf_get,
1114 .dt_sync = osd_sync,
1116 .dt_commit_async = osd_commit_async,
1117 .dt_credit_get = osd_credit_get,
1118 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1119 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1122 static void osd_object_read_lock(const struct lu_env *env,
1123 struct dt_object *dt, unsigned role)
1125 struct osd_object *obj = osd_dt_obj(dt);
1126 struct osd_thread_info *oti = osd_oti_get(env);
1128 LINVRNT(osd_invariant(obj));
1130 LASSERT(obj->oo_owner != env);
1131 cfs_down_read_nested(&obj->oo_sem, role);
1133 LASSERT(obj->oo_owner == NULL);
1137 static void osd_object_write_lock(const struct lu_env *env,
1138 struct dt_object *dt, unsigned role)
1140 struct osd_object *obj = osd_dt_obj(dt);
1141 struct osd_thread_info *oti = osd_oti_get(env);
1143 LINVRNT(osd_invariant(obj));
1145 LASSERT(obj->oo_owner != env);
1146 cfs_down_write_nested(&obj->oo_sem, role);
1148 LASSERT(obj->oo_owner == NULL);
1149 obj->oo_owner = env;
1153 static void osd_object_read_unlock(const struct lu_env *env,
1154 struct dt_object *dt)
1156 struct osd_object *obj = osd_dt_obj(dt);
1157 struct osd_thread_info *oti = osd_oti_get(env);
1159 LINVRNT(osd_invariant(obj));
1161 LASSERT(oti->oti_r_locks > 0);
1163 cfs_up_read(&obj->oo_sem);
1166 static void osd_object_write_unlock(const struct lu_env *env,
1167 struct dt_object *dt)
1169 struct osd_object *obj = osd_dt_obj(dt);
1170 struct osd_thread_info *oti = osd_oti_get(env);
1172 LINVRNT(osd_invariant(obj));
1174 LASSERT(obj->oo_owner == env);
1175 LASSERT(oti->oti_w_locks > 0);
1177 obj->oo_owner = NULL;
1178 cfs_up_write(&obj->oo_sem);
1181 static int osd_object_write_locked(const struct lu_env *env,
1182 struct dt_object *dt)
1184 struct osd_object *obj = osd_dt_obj(dt);
1186 LINVRNT(osd_invariant(obj));
1188 return obj->oo_owner == env;
1191 static int capa_is_sane(const struct lu_env *env,
1192 struct osd_device *dev,
1193 struct lustre_capa *capa,
1194 struct lustre_capa_key *keys)
1196 struct osd_thread_info *oti = osd_oti_get(env);
1197 struct lustre_capa *tcapa = &oti->oti_capa;
1198 struct obd_capa *oc;
1202 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1204 if (capa_is_expired(oc)) {
1205 DEBUG_CAPA(D_ERROR, capa, "expired");
1212 if (capa_is_expired_sec(capa)) {
1213 DEBUG_CAPA(D_ERROR, capa, "expired");
1217 cfs_spin_lock(&capa_lock);
1218 for (i = 0; i < 2; i++) {
1219 if (keys[i].lk_keyid == capa->lc_keyid) {
1220 oti->oti_capa_key = keys[i];
1224 cfs_spin_unlock(&capa_lock);
1227 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1231 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1235 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1236 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1240 oc = capa_add(dev->od_capa_hash, capa);
1246 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1247 struct lustre_capa *capa, __u64 opc)
1249 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1250 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1251 struct md_capainfo *ci;
1254 if (!dev->od_fl_capa)
1257 if (capa == BYPASS_CAPA)
1260 ci = md_capainfo(env);
1264 if (ci->mc_auth == LC_ID_NONE)
1268 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1272 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1273 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1278 if (!capa_opc_supported(capa, opc)) {
1279 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1283 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1284 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1291 static struct timespec *osd_inode_time(const struct lu_env *env,
1292 struct inode *inode, __u64 seconds)
1294 struct osd_thread_info *oti = osd_oti_get(env);
1295 struct timespec *t = &oti->oti_time;
1297 t->tv_sec = seconds;
1299 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1304 static void osd_inode_getattr(const struct lu_env *env,
1305 struct inode *inode, struct lu_attr *attr)
1307 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1308 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1309 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1311 attr->la_atime = LTIME_S(inode->i_atime);
1312 attr->la_mtime = LTIME_S(inode->i_mtime);
1313 attr->la_ctime = LTIME_S(inode->i_ctime);
1314 attr->la_mode = inode->i_mode;
1315 attr->la_size = i_size_read(inode);
1316 attr->la_blocks = inode->i_blocks;
1317 attr->la_uid = inode->i_uid;
1318 attr->la_gid = inode->i_gid;
1319 attr->la_flags = LDISKFS_I(inode)->i_flags;
1320 attr->la_nlink = inode->i_nlink;
1321 attr->la_rdev = inode->i_rdev;
1322 attr->la_blksize = ll_inode_blksize(inode);
1323 attr->la_blkbits = inode->i_blkbits;
1326 static int osd_attr_get(const struct lu_env *env,
1327 struct dt_object *dt,
1328 struct lu_attr *attr,
1329 struct lustre_capa *capa)
1331 struct osd_object *obj = osd_dt_obj(dt);
1333 LASSERT(dt_object_exists(dt));
1334 LINVRNT(osd_invariant(obj));
1336 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1339 cfs_spin_lock(&obj->oo_guard);
1340 osd_inode_getattr(env, obj->oo_inode, attr);
1341 cfs_spin_unlock(&obj->oo_guard);
1345 static int osd_inode_setattr(const struct lu_env *env,
1346 struct inode *inode, const struct lu_attr *attr)
1350 bits = attr->la_valid;
1352 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1354 #ifdef HAVE_QUOTA_SUPPORT
1355 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1356 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1357 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1363 iattr.ia_valid |= ATTR_UID;
1365 iattr.ia_valid |= ATTR_GID;
1366 iattr.ia_uid = attr->la_uid;
1367 iattr.ia_gid = attr->la_gid;
1368 osd_push_ctxt(env, save);
1369 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1376 if (bits & LA_ATIME)
1377 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1378 if (bits & LA_CTIME)
1379 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1380 if (bits & LA_MTIME)
1381 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1382 if (bits & LA_SIZE) {
1383 LDISKFS_I(inode)->i_disksize = attr->la_size;
1384 i_size_write(inode, attr->la_size);
1388 /* OSD should not change "i_blocks" which is used by quota.
1389 * "i_blocks" should be changed by ldiskfs only. */
1390 if (bits & LA_BLOCKS)
1391 inode->i_blocks = attr->la_blocks;
1394 inode->i_mode = (inode->i_mode & S_IFMT) |
1395 (attr->la_mode & ~S_IFMT);
1397 inode->i_uid = attr->la_uid;
1399 inode->i_gid = attr->la_gid;
1400 if (bits & LA_NLINK)
1401 inode->i_nlink = attr->la_nlink;
1403 inode->i_rdev = attr->la_rdev;
1405 if (bits & LA_FLAGS)
1406 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags);
1410 static int osd_attr_set(const struct lu_env *env,
1411 struct dt_object *dt,
1412 const struct lu_attr *attr,
1413 struct thandle *handle,
1414 struct lustre_capa *capa)
1416 struct osd_object *obj = osd_dt_obj(dt);
1419 LASSERT(handle != NULL);
1420 LASSERT(dt_object_exists(dt));
1421 LASSERT(osd_invariant(obj));
1423 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1426 cfs_spin_lock(&obj->oo_guard);
1427 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1428 cfs_spin_unlock(&obj->oo_guard);
1431 mark_inode_dirty(obj->oo_inode);
1438 * XXX temporary solution.
1440 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1441 struct lu_attr *attr, struct thandle *th)
1446 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1447 struct lu_attr *attr, struct thandle *th)
1449 osd_object_init0(obj);
1453 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1454 struct osd_object *obj,
1458 struct osd_thread_info *info = osd_oti_get(env);
1459 struct dentry *child_dentry = &info->oti_child_dentry;
1460 struct dentry *obj_dentry = &info->oti_obj_dentry;
1462 obj_dentry->d_inode = obj->oo_inode;
1463 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1464 obj_dentry->d_name.hash = 0;
1466 child_dentry->d_name.hash = 0;
1467 child_dentry->d_parent = obj_dentry;
1468 child_dentry->d_name.name = name;
1469 child_dentry->d_name.len = namelen;
1470 return child_dentry;
1474 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1476 struct dt_allocation_hint *hint,
1480 struct osd_device *osd = osd_obj2dev(obj);
1481 struct osd_thandle *oth;
1482 struct dt_object *parent;
1483 struct inode *inode;
1484 #ifdef HAVE_QUOTA_SUPPORT
1485 struct osd_ctxt *save = &info->oti_ctxt;
1488 LINVRNT(osd_invariant(obj));
1489 LASSERT(obj->oo_inode == NULL);
1491 oth = container_of(th, struct osd_thandle, ot_super);
1492 LASSERT(oth->ot_handle->h_transaction != NULL);
1494 if (hint && hint->dah_parent)
1495 parent = hint->dah_parent;
1497 parent = osd->od_obj_area;
1499 LASSERT(parent != NULL);
1500 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1502 #ifdef HAVE_QUOTA_SUPPORT
1503 osd_push_ctxt(info->oti_env, save);
1505 inode = ldiskfs_create_inode(oth->ot_handle,
1506 osd_dt_obj(parent)->oo_inode, mode);
1507 #ifdef HAVE_QUOTA_SUPPORT
1510 if (!IS_ERR(inode)) {
1511 obj->oo_inode = inode;
1514 result = PTR_ERR(inode);
1515 LINVRNT(osd_invariant(obj));
1523 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1524 struct lu_attr *attr,
1525 struct dt_allocation_hint *hint,
1526 struct dt_object_format *dof,
1530 struct osd_thandle *oth;
1531 struct osd_device *osd = osd_obj2dev(obj);
1532 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1534 LASSERT(S_ISDIR(attr->la_mode));
1536 oth = container_of(th, struct osd_thandle, ot_super);
1537 LASSERT(oth->ot_handle->h_transaction != NULL);
1538 result = osd_mkfile(info, obj, mode, hint, th);
1539 if (result == 0 && osd->od_iop_mode == 0) {
1540 LASSERT(obj->oo_inode != NULL);
1542 * XXX uh-oh... call low-level iam function directly.
1545 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1546 sizeof (struct osd_fid_pack),
1552 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1553 struct lu_attr *attr,
1554 struct dt_allocation_hint *hint,
1555 struct dt_object_format *dof,
1559 struct osd_thandle *oth;
1560 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1562 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1564 LASSERT(S_ISREG(attr->la_mode));
1566 oth = container_of(th, struct osd_thandle, ot_super);
1567 LASSERT(oth->ot_handle->h_transaction != NULL);
1569 result = osd_mkfile(info, obj, mode, hint, th);
1571 LASSERT(obj->oo_inode != NULL);
1572 if (feat->dif_flags & DT_IND_VARKEY)
1573 result = iam_lvar_create(obj->oo_inode,
1574 feat->dif_keysize_max,
1576 feat->dif_recsize_max,
1579 result = iam_lfix_create(obj->oo_inode,
1580 feat->dif_keysize_max,
1582 feat->dif_recsize_max,
1589 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1590 struct lu_attr *attr,
1591 struct dt_allocation_hint *hint,
1592 struct dt_object_format *dof,
1595 LASSERT(S_ISREG(attr->la_mode));
1596 return osd_mkfile(info, obj, (attr->la_mode &
1597 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1600 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1601 struct lu_attr *attr,
1602 struct dt_allocation_hint *hint,
1603 struct dt_object_format *dof,
1606 LASSERT(S_ISLNK(attr->la_mode));
1607 return osd_mkfile(info, obj, (attr->la_mode &
1608 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1611 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1612 struct lu_attr *attr,
1613 struct dt_allocation_hint *hint,
1614 struct dt_object_format *dof,
1617 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1620 LINVRNT(osd_invariant(obj));
1621 LASSERT(obj->oo_inode == NULL);
1622 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1623 S_ISFIFO(mode) || S_ISSOCK(mode));
1625 result = osd_mkfile(info, obj, mode, hint, th);
1627 LASSERT(obj->oo_inode != NULL);
1628 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1630 LINVRNT(osd_invariant(obj));
1634 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1636 struct dt_allocation_hint *hint,
1637 struct dt_object_format *dof,
1640 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1642 osd_obj_type_f result;
1658 result = osd_mk_index;
1669 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1670 struct dt_object *parent, cfs_umode_t child_mode)
1674 memset(ah, 0, sizeof(*ah));
1675 ah->dah_parent = parent;
1676 ah->dah_mode = child_mode;
1680 * Helper function for osd_object_create()
1682 * \retval 0, on success
1684 static int __osd_object_create(struct osd_thread_info *info,
1685 struct osd_object *obj, struct lu_attr *attr,
1686 struct dt_allocation_hint *hint,
1687 struct dt_object_format *dof,
1693 result = osd_create_pre(info, obj, attr, th);
1695 result = osd_create_type_f(dof->dof_type)(info, obj,
1696 attr, hint, dof, th);
1698 result = osd_create_post(info, obj, attr, th);
1704 * Helper function for osd_object_create()
1706 * \retval 0, on success
1708 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1709 const struct lu_fid *fid, struct thandle *th)
1711 struct osd_thread_info *info = osd_oti_get(env);
1712 struct osd_inode_id *id = &info->oti_id;
1713 struct osd_device *osd = osd_obj2dev(obj);
1714 struct md_ucred *uc = md_ucred(env);
1716 LASSERT(obj->oo_inode != NULL);
1717 LASSERT(uc != NULL);
1719 id->oii_ino = obj->oo_inode->i_ino;
1720 id->oii_gen = obj->oo_inode->i_generation;
1722 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1723 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1726 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1727 struct lu_attr *attr,
1728 struct dt_allocation_hint *hint,
1729 struct dt_object_format *dof,
1732 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1733 struct osd_object *obj = osd_dt_obj(dt);
1734 struct osd_thread_info *info = osd_oti_get(env);
1739 LINVRNT(osd_invariant(obj));
1740 LASSERT(!dt_object_exists(dt));
1741 LASSERT(osd_write_locked(env, obj));
1742 LASSERT(th != NULL);
1744 result = __osd_object_create(info, obj, attr, hint, dof, th);
1746 result = __osd_oi_insert(env, obj, fid, th);
1748 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1749 LASSERT(osd_invariant(obj));
1754 * Helper function for osd_xattr_set()
1756 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1757 const struct lu_buf *buf, const char *name, int fl)
1759 struct osd_object *obj = osd_dt_obj(dt);
1760 struct inode *inode = obj->oo_inode;
1761 struct osd_thread_info *info = osd_oti_get(env);
1762 struct dentry *dentry = &info->oti_child_dentry;
1763 struct timespec *t = &info->oti_time;
1767 LASSERT(dt_object_exists(dt));
1768 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1769 LASSERT(osd_write_locked(env, obj));
1771 if (fl & LU_XATTR_REPLACE)
1772 fs_flags |= XATTR_REPLACE;
1774 if (fl & LU_XATTR_CREATE)
1775 fs_flags |= XATTR_CREATE;
1777 dentry->d_inode = inode;
1778 *t = inode->i_ctime;
1779 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1780 buf->lb_len, fs_flags);
1781 /* ctime should not be updated with server-side time. */
1782 cfs_spin_lock(&obj->oo_guard);
1783 inode->i_ctime = *t;
1784 cfs_spin_unlock(&obj->oo_guard);
1785 mark_inode_dirty(inode);
1790 * Put the fid into lustre_mdt_attrs, and then place the structure
1791 * inode's ea. This fid should not be altered during the life time
1794 * \retval +ve, on success
1795 * \retval -ve, on error
1797 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1799 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1800 const struct lu_fid *fid)
1802 struct osd_thread_info *info = osd_oti_get(env);
1803 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1805 lustre_lma_init(mdt_attrs, fid);
1806 lustre_lma_swab(mdt_attrs);
1807 return __osd_xattr_set(env, dt,
1808 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1809 XATTR_NAME_LMA, LU_XATTR_CREATE);
1814 * Helper function to form igif
1816 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1819 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1823 * Helper function to pack the fid, ldiskfs stores fid in packed format.
1825 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1826 struct lu_fid *befider)
1828 fid_cpu_to_be(befider, (struct lu_fid *)fid);
1829 memcpy(pack->fp_area, befider, sizeof(*befider));
1830 pack->fp_len = sizeof(*befider) + 1;
1834 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1835 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1836 * To have compatilibility with 1.8 ldiskfs driver we need to have
1837 * magic number at start of fid data.
1838 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1841 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1842 const struct dt_rec *fid)
1844 param->edp_magic = LDISKFS_LUFID_MAGIC;
1845 param->edp_len = sizeof(struct lu_fid) + 1;
1847 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1848 (struct lu_fid *)fid);
1851 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1856 switch (pack->fp_len) {
1857 case sizeof *fid + 1:
1858 memcpy(fid, pack->fp_area, sizeof *fid);
1859 fid_be_to_cpu(fid, fid);
1862 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1869 * Try to read the fid from inode ea into dt_rec, if return value
1870 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1872 * \param fid object fid.
1874 * \retval 0 on success
1876 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1877 __u32 ino, struct lu_fid *fid)
1879 struct osd_thread_info *info = osd_oti_get(env);
1880 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1881 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1882 struct dentry *dentry = &info->oti_child_dentry;
1883 struct osd_inode_id *id = &info->oti_id;
1884 struct osd_device *dev;
1885 struct inode *inode;
1889 dev = osd_dev(ldev);
1892 id->oii_gen = OSD_OII_NOGEN;
1894 inode = osd_iget(info, dev, id);
1895 if (IS_ERR(inode)) {
1896 rc = PTR_ERR(inode);
1899 dentry->d_inode = inode;
1901 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1902 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1905 /* Check LMA compatibility */
1907 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
1908 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1909 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
1910 ~LMA_INCOMPAT_SUPP);
1915 lustre_lma_swab(mdt_attrs);
1916 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
1918 } else if (rc == -ENODATA) {
1919 osd_igif_get(env, inode, fid);
1928 * OSD layer object create function for interoperability mode (b11826).
1929 * This is mostly similar to osd_object_create(). Only difference being, fid is
1930 * inserted into inode ea here.
1932 * \retval 0, on success
1933 * \retval -ve, on error
1935 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1936 struct lu_attr *attr,
1937 struct dt_allocation_hint *hint,
1938 struct dt_object_format *dof,
1941 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1942 struct osd_object *obj = osd_dt_obj(dt);
1943 struct osd_thread_info *info = osd_oti_get(env);
1948 LASSERT(osd_invariant(obj));
1949 LASSERT(!dt_object_exists(dt));
1950 LASSERT(osd_write_locked(env, obj));
1951 LASSERT(th != NULL);
1953 result = __osd_object_create(info, obj, attr, hint, dof, th);
1955 /* objects under osd root shld have igif fid, so dont add fid EA */
1956 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
1957 result = osd_ea_fid_set(env, dt, fid);
1960 result = __osd_oi_insert(env, obj, fid, th);
1962 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1963 LINVRNT(osd_invariant(obj));
1968 * Concurrency: @dt is write locked.
1970 static void osd_object_ref_add(const struct lu_env *env,
1971 struct dt_object *dt,
1974 struct osd_object *obj = osd_dt_obj(dt);
1975 struct inode *inode = obj->oo_inode;
1977 LINVRNT(osd_invariant(obj));
1978 LASSERT(dt_object_exists(dt));
1979 LASSERT(osd_write_locked(env, obj));
1980 LASSERT(th != NULL);
1982 cfs_spin_lock(&obj->oo_guard);
1983 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1985 cfs_spin_unlock(&obj->oo_guard);
1986 mark_inode_dirty(inode);
1987 LINVRNT(osd_invariant(obj));
1991 * Concurrency: @dt is write locked.
1993 static void osd_object_ref_del(const struct lu_env *env,
1994 struct dt_object *dt,
1997 struct osd_object *obj = osd_dt_obj(dt);
1998 struct inode *inode = obj->oo_inode;
2000 LINVRNT(osd_invariant(obj));
2001 LASSERT(dt_object_exists(dt));
2002 LASSERT(osd_write_locked(env, obj));
2003 LASSERT(th != NULL);
2005 cfs_spin_lock(&obj->oo_guard);
2006 LASSERT(inode->i_nlink > 0);
2008 cfs_spin_unlock(&obj->oo_guard);
2009 mark_inode_dirty(inode);
2010 LINVRNT(osd_invariant(obj));
2014 * Concurrency: @dt is read locked.
2016 static int osd_xattr_get(const struct lu_env *env,
2017 struct dt_object *dt,
2020 struct lustre_capa *capa)
2022 struct osd_object *obj = osd_dt_obj(dt);
2023 struct inode *inode = obj->oo_inode;
2024 struct osd_thread_info *info = osd_oti_get(env);
2025 struct dentry *dentry = &info->oti_obj_dentry;
2027 LASSERT(dt_object_exists(dt));
2028 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2029 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2031 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2034 dentry->d_inode = inode;
2035 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2039 * Concurrency: @dt is write locked.
2041 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2042 const struct lu_buf *buf, const char *name, int fl,
2043 struct thandle *handle, struct lustre_capa *capa)
2045 LASSERT(handle != NULL);
2047 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2050 return __osd_xattr_set(env, dt, buf, name, fl);
2054 * Concurrency: @dt is read locked.
2056 static int osd_xattr_list(const struct lu_env *env,
2057 struct dt_object *dt,
2059 struct lustre_capa *capa)
2061 struct osd_object *obj = osd_dt_obj(dt);
2062 struct inode *inode = obj->oo_inode;
2063 struct osd_thread_info *info = osd_oti_get(env);
2064 struct dentry *dentry = &info->oti_obj_dentry;
2066 LASSERT(dt_object_exists(dt));
2067 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2068 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2070 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2073 dentry->d_inode = inode;
2074 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2078 * Concurrency: @dt is write locked.
2080 static int osd_xattr_del(const struct lu_env *env,
2081 struct dt_object *dt,
2083 struct thandle *handle,
2084 struct lustre_capa *capa)
2086 struct osd_object *obj = osd_dt_obj(dt);
2087 struct inode *inode = obj->oo_inode;
2088 struct osd_thread_info *info = osd_oti_get(env);
2089 struct dentry *dentry = &info->oti_obj_dentry;
2090 struct timespec *t = &info->oti_time;
2093 LASSERT(dt_object_exists(dt));
2094 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2095 LASSERT(osd_write_locked(env, obj));
2096 LASSERT(handle != NULL);
2098 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2101 dentry->d_inode = inode;
2102 *t = inode->i_ctime;
2103 rc = inode->i_op->removexattr(dentry, name);
2104 /* ctime should not be updated with server-side time. */
2105 cfs_spin_lock(&obj->oo_guard);
2106 inode->i_ctime = *t;
2107 cfs_spin_unlock(&obj->oo_guard);
2108 mark_inode_dirty(inode);
2112 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2113 struct dt_object *dt,
2114 struct lustre_capa *old,
2117 struct osd_thread_info *info = osd_oti_get(env);
2118 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2119 struct osd_object *obj = osd_dt_obj(dt);
2120 struct osd_device *dev = osd_obj2dev(obj);
2121 struct lustre_capa_key *key = &info->oti_capa_key;
2122 struct lustre_capa *capa = &info->oti_capa;
2123 struct obd_capa *oc;
2124 struct md_capainfo *ci;
2128 if (!dev->od_fl_capa)
2129 RETURN(ERR_PTR(-ENOENT));
2131 LASSERT(dt_object_exists(dt));
2132 LINVRNT(osd_invariant(obj));
2134 /* renewal sanity check */
2135 if (old && osd_object_auth(env, dt, old, opc))
2136 RETURN(ERR_PTR(-EACCES));
2138 ci = md_capainfo(env);
2140 RETURN(ERR_PTR(-ENOENT));
2142 switch (ci->mc_auth) {
2146 capa->lc_uid = obj->oo_inode->i_uid;
2147 capa->lc_gid = obj->oo_inode->i_gid;
2148 capa->lc_flags = LC_ID_PLAIN;
2150 case LC_ID_CONVERT: {
2153 s[0] = obj->oo_inode->i_uid;
2154 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2155 s[2] = obj->oo_inode->i_gid;
2156 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2157 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2159 RETURN(ERR_PTR(rc));
2161 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2162 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2163 capa->lc_flags = LC_ID_CONVERT;
2167 RETURN(ERR_PTR(-EINVAL));
2170 capa->lc_fid = *fid;
2172 capa->lc_flags |= dev->od_capa_alg << 24;
2173 capa->lc_timeout = dev->od_capa_timeout;
2174 capa->lc_expiry = 0;
2176 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2178 LASSERT(!capa_is_expired(oc));
2182 cfs_spin_lock(&capa_lock);
2183 *key = dev->od_capa_keys[1];
2184 cfs_spin_unlock(&capa_lock);
2186 capa->lc_keyid = key->lk_keyid;
2187 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2189 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2191 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2192 RETURN(ERR_PTR(rc));
2195 oc = capa_add(dev->od_capa_hash, capa);
2199 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2202 struct osd_object *obj = osd_dt_obj(dt);
2203 struct inode *inode = obj->oo_inode;
2204 struct osd_thread_info *info = osd_oti_get(env);
2205 struct dentry *dentry = &info->oti_obj_dentry;
2206 struct file *file = &info->oti_file;
2209 dentry->d_inode = inode;
2210 file->f_dentry = dentry;
2211 file->f_mapping = inode->i_mapping;
2212 file->f_op = inode->i_fop;
2213 LOCK_INODE_MUTEX(inode);
2214 rc = file->f_op->fsync(file, dentry, 0);
2215 UNLOCK_INODE_MUTEX(inode);
2220 * Get the 64-bit version for an inode.
2222 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2223 struct dt_object *dt)
2225 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2227 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2228 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2229 return LDISKFS_I(inode)->i_fs_version;
2233 * Set the 64-bit version and return the old version.
2235 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2236 dt_obj_version_t new_version)
2238 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2240 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2241 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2242 LDISKFS_I(inode)->i_fs_version = new_version;
2243 /** Version is set after all inode operations are finished,
2244 * so we should mark it dirty here */
2245 inode->i_sb->s_op->dirty_inode(inode);
2248 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2251 struct osd_object *obj = osd_dt_obj(dt);
2254 *data = (void *)obj->oo_inode;
2262 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2263 const struct dt_index_features *feat)
2265 struct iam_descr *descr;
2267 if (osd_object_is_root(o))
2268 return feat == &dt_directory_features;
2270 LASSERT(o->oo_dir != NULL);
2272 descr = o->oo_dir->od_container.ic_descr;
2273 if (feat == &dt_directory_features) {
2274 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2280 feat->dif_keysize_min <= descr->id_key_size &&
2281 descr->id_key_size <= feat->dif_keysize_max &&
2282 feat->dif_recsize_min <= descr->id_rec_size &&
2283 descr->id_rec_size <= feat->dif_recsize_max &&
2284 !(feat->dif_flags & (DT_IND_VARKEY |
2285 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2286 ergo(feat->dif_flags & DT_IND_UPDATE,
2287 1 /* XXX check that object (and file system) is
2292 static int osd_iam_container_init(const struct lu_env *env,
2293 struct osd_object *obj,
2294 struct osd_directory *dir)
2297 struct iam_container *bag;
2299 bag = &dir->od_container;
2300 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2302 result = iam_container_setup(bag);
2304 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2306 iam_container_fini(bag);
2313 * Concurrency: no external locking is necessary.
2315 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2316 const struct dt_index_features *feat)
2320 struct osd_object *obj = osd_dt_obj(dt);
2321 struct osd_device *osd = osd_obj2dev(obj);
2323 LINVRNT(osd_invariant(obj));
2324 LASSERT(dt_object_exists(dt));
2326 if (osd_object_is_root(obj)) {
2327 dt->do_index_ops = &osd_index_ea_ops;
2329 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2330 dt->do_index_ops = &osd_index_ea_ops;
2331 if (S_ISDIR(obj->oo_inode->i_mode))
2336 } else if (!osd_has_index(obj)) {
2337 struct osd_directory *dir;
2342 cfs_spin_lock(&obj->oo_guard);
2343 if (obj->oo_dir == NULL)
2347 * Concurrent thread allocated container data.
2350 cfs_spin_unlock(&obj->oo_guard);
2352 * Now, that we have container data, serialize its
2355 cfs_down_write(&obj->oo_ext_idx_sem);
2357 * recheck under lock.
2359 if (!osd_has_index(obj))
2360 result = osd_iam_container_init(env, obj, dir);
2363 cfs_up_write(&obj->oo_ext_idx_sem);
2369 if (result == 0 && ea_dir == 0) {
2370 if (!osd_iam_index_probe(env, obj, feat))
2373 LINVRNT(osd_invariant(obj));
2378 static const struct dt_object_operations osd_obj_ops = {
2379 .do_read_lock = osd_object_read_lock,
2380 .do_write_lock = osd_object_write_lock,
2381 .do_read_unlock = osd_object_read_unlock,
2382 .do_write_unlock = osd_object_write_unlock,
2383 .do_write_locked = osd_object_write_locked,
2384 .do_attr_get = osd_attr_get,
2385 .do_attr_set = osd_attr_set,
2386 .do_ah_init = osd_ah_init,
2387 .do_create = osd_object_create,
2388 .do_index_try = osd_index_try,
2389 .do_ref_add = osd_object_ref_add,
2390 .do_ref_del = osd_object_ref_del,
2391 .do_xattr_get = osd_xattr_get,
2392 .do_xattr_set = osd_xattr_set,
2393 .do_xattr_del = osd_xattr_del,
2394 .do_xattr_list = osd_xattr_list,
2395 .do_capa_get = osd_capa_get,
2396 .do_object_sync = osd_object_sync,
2397 .do_version_get = osd_object_version_get,
2398 .do_version_set = osd_object_version_set,
2399 .do_data_get = osd_data_get,
2403 * dt_object_operations for interoperability mode
2404 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2406 static const struct dt_object_operations osd_obj_ea_ops = {
2407 .do_read_lock = osd_object_read_lock,
2408 .do_write_lock = osd_object_write_lock,
2409 .do_read_unlock = osd_object_read_unlock,
2410 .do_write_unlock = osd_object_write_unlock,
2411 .do_write_locked = osd_object_write_locked,
2412 .do_attr_get = osd_attr_get,
2413 .do_attr_set = osd_attr_set,
2414 .do_ah_init = osd_ah_init,
2415 .do_create = osd_object_ea_create,
2416 .do_index_try = osd_index_try,
2417 .do_ref_add = osd_object_ref_add,
2418 .do_ref_del = osd_object_ref_del,
2419 .do_xattr_get = osd_xattr_get,
2420 .do_xattr_set = osd_xattr_set,
2421 .do_xattr_del = osd_xattr_del,
2422 .do_xattr_list = osd_xattr_list,
2423 .do_capa_get = osd_capa_get,
2424 .do_object_sync = osd_object_sync,
2425 .do_version_get = osd_object_version_get,
2426 .do_version_set = osd_object_version_set,
2427 .do_data_get = osd_data_get,
2435 * XXX: Another layering violation for now.
2437 * We don't want to use ->f_op->read methods, because generic file write
2439 * - serializes on ->i_sem, and
2441 * - does a lot of extra work like balance_dirty_pages(),
2443 * which doesn't work for globally shared files like /last-received.
2445 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2447 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2449 memcpy(buffer, (char*)ei->i_data, buflen);
2454 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2457 struct buffer_head *bh;
2458 unsigned long block;
2465 /* prevent reading after eof */
2466 spin_lock(&inode->i_lock);
2467 if (i_size_read(inode) < *offs + size) {
2468 size = i_size_read(inode) - *offs;
2469 spin_unlock(&inode->i_lock);
2471 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2472 i_size_read(inode), *offs);
2474 } else if (size == 0) {
2478 spin_unlock(&inode->i_lock);
2481 blocksize = 1 << inode->i_blkbits;
2484 block = *offs >> inode->i_blkbits;
2485 boffs = *offs & (blocksize - 1);
2486 csize = min(blocksize - boffs, size);
2487 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2489 CERROR("can't read block: %d\n", err);
2493 memcpy(buf, bh->b_data + boffs, csize);
2503 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2504 struct lu_buf *buf, loff_t *pos,
2505 struct lustre_capa *capa)
2507 struct osd_object *obj = osd_dt_obj(dt);
2508 struct inode *inode = obj->oo_inode;
2511 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2514 /* Read small symlink from inode body as we need to maintain correct
2515 * on-disk symlinks for ldiskfs.
2517 if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2518 (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2519 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2521 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2526 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2529 memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2531 LDISKFS_I(inode)->i_disksize = buflen;
2532 i_size_write(inode, buflen);
2533 inode->i_sb->s_op->dirty_inode(inode);
2538 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2539 loff_t *offs, handle_t *handle)
2541 struct buffer_head *bh = NULL;
2542 loff_t offset = *offs;
2543 loff_t new_size = i_size_read(inode);
2544 unsigned long block;
2545 int blocksize = 1 << inode->i_blkbits;
2549 int dirty_inode = 0;
2551 while (bufsize > 0) {
2555 block = offset >> inode->i_blkbits;
2556 boffs = offset & (blocksize - 1);
2557 size = min(blocksize - boffs, bufsize);
2558 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2560 CERROR("can't read/create block: %d\n", err);
2564 err = ldiskfs_journal_get_write_access(handle, bh);
2566 CERROR("journal_get_write_access() returned error %d\n",
2570 LASSERTF(boffs + size <= bh->b_size,
2571 "boffs %d size %d bh->b_size %lu",
2572 boffs, size, (unsigned long)bh->b_size);
2573 memcpy(bh->b_data + boffs, buf, size);
2574 err = ldiskfs_journal_dirty_metadata(handle, bh);
2578 if (offset + size > new_size)
2579 new_size = offset + size;
2587 /* correct in-core and on-disk sizes */
2588 if (new_size > i_size_read(inode)) {
2589 spin_lock(&inode->i_lock);
2590 if (new_size > i_size_read(inode))
2591 i_size_write(inode, new_size);
2592 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
2593 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
2596 spin_unlock(&inode->i_lock);
2598 inode->i_sb->s_op->dirty_inode(inode);
2606 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2607 const struct lu_buf *buf, loff_t *pos,
2608 struct thandle *handle, struct lustre_capa *capa,
2611 struct osd_object *obj = osd_dt_obj(dt);
2612 struct inode *inode = obj->oo_inode;
2613 struct osd_thandle *oh;
2615 #ifdef HAVE_QUOTA_SUPPORT
2616 cfs_cap_t save = current->cap_effective;
2619 LASSERT(handle != NULL);
2621 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2624 oh = container_of(handle, struct osd_thandle, ot_super);
2625 LASSERT(oh->ot_handle->h_transaction != NULL);
2626 #ifdef HAVE_QUOTA_SUPPORT
2628 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2630 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2632 /* Write small symlink to inode body as we need to maintain correct
2633 * on-disk symlinks for ldiskfs.
2635 if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2636 (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
2637 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2639 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
2642 #ifdef HAVE_QUOTA_SUPPORT
2643 current->cap_effective = save;
2646 result = buf->lb_len;
2650 static const struct dt_body_operations osd_body_ops = {
2651 .dbo_read = osd_read,
2652 .dbo_write = osd_write
2657 * delete a (key, value) pair from index \a dt specified by \a key
2659 * \param dt osd index object
2660 * \param key key for index
2661 * \param rec record reference
2662 * \param handle transaction handler
2665 * \retval -ve failure
2668 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2669 const struct dt_key *key, struct thandle *handle,
2670 struct lustre_capa *capa)
2672 struct osd_object *obj = osd_dt_obj(dt);
2673 struct osd_thandle *oh;
2674 struct iam_path_descr *ipd;
2675 struct iam_container *bag = &obj->oo_dir->od_container;
2680 LINVRNT(osd_invariant(obj));
2681 LASSERT(dt_object_exists(dt));
2682 LASSERT(bag->ic_object == obj->oo_inode);
2683 LASSERT(handle != NULL);
2685 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2688 ipd = osd_idx_ipd_get(env, bag);
2689 if (unlikely(ipd == NULL))
2692 oh = container_of0(handle, struct osd_thandle, ot_super);
2693 LASSERT(oh->ot_handle != NULL);
2694 LASSERT(oh->ot_handle->h_transaction != NULL);
2696 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2697 osd_ipd_put(env, bag, ipd);
2698 LINVRNT(osd_invariant(obj));
2702 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2705 struct osd_fid_pack *rec;
2708 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2709 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2710 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2716 * Index delete function for interoperability mode (b11826).
2717 * It will remove the directory entry added by osd_index_ea_insert().
2718 * This entry is needed to maintain name->fid mapping.
2720 * \param key, key i.e. file entry to be deleted
2722 * \retval 0, on success
2723 * \retval -ve, on error
2725 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2726 const struct dt_key *key, struct thandle *handle,
2727 struct lustre_capa *capa)
2729 struct osd_object *obj = osd_dt_obj(dt);
2730 struct inode *dir = obj->oo_inode;
2731 struct dentry *dentry;
2732 struct osd_thandle *oh;
2733 struct ldiskfs_dir_entry_2 *de;
2734 struct buffer_head *bh;
2740 LINVRNT(osd_invariant(obj));
2741 LASSERT(dt_object_exists(dt));
2742 LASSERT(handle != NULL);
2744 oh = container_of(handle, struct osd_thandle, ot_super);
2745 LASSERT(oh->ot_handle != NULL);
2746 LASSERT(oh->ot_handle->h_transaction != NULL);
2748 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2751 dentry = osd_child_dentry_get(env, obj,
2752 (char *)key, strlen((char *)key));
2754 cfs_down_write(&obj->oo_ext_idx_sem);
2755 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
2757 struct osd_thread_info *oti = osd_oti_get(env);
2758 struct timespec *ctime = &oti->oti_time;
2759 struct timespec *mtime = &oti->oti_time2;
2761 *ctime = dir->i_ctime;
2762 *mtime = dir->i_mtime;
2763 rc = ldiskfs_delete_entry(oh->ot_handle,
2765 /* xtime should not be updated with server-side time. */
2766 cfs_spin_lock(&obj->oo_guard);
2767 dir->i_ctime = *ctime;
2768 dir->i_mtime = *mtime;
2769 cfs_spin_unlock(&obj->oo_guard);
2770 mark_inode_dirty(dir);
2775 cfs_up_write(&obj->oo_ext_idx_sem);
2776 LASSERT(osd_invariant(obj));
2781 * Lookup index for \a key and copy record to \a rec.
2783 * \param dt osd index object
2784 * \param key key for index
2785 * \param rec record reference
2787 * \retval +ve success : exact mach
2788 * \retval 0 return record with key not greater than \a key
2789 * \retval -ve failure
2791 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2792 struct dt_rec *rec, const struct dt_key *key,
2793 struct lustre_capa *capa)
2795 struct osd_object *obj = osd_dt_obj(dt);
2796 struct iam_path_descr *ipd;
2797 struct iam_container *bag = &obj->oo_dir->od_container;
2798 struct osd_thread_info *oti = osd_oti_get(env);
2799 struct iam_iterator *it = &oti->oti_idx_it;
2800 struct iam_rec *iam_rec;
2804 LASSERT(osd_invariant(obj));
2805 LASSERT(dt_object_exists(dt));
2806 LASSERT(bag->ic_object == obj->oo_inode);
2808 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2811 ipd = osd_idx_ipd_get(env, bag);
2815 /* got ipd now we can start iterator. */
2816 iam_it_init(it, bag, 0, ipd);
2818 rc = iam_it_get(it, (struct iam_key *)key);
2820 if (S_ISDIR(obj->oo_inode->i_mode))
2821 iam_rec = (struct iam_rec *)oti->oti_ldp;
2823 iam_rec = (struct iam_rec *) rec;
2825 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2826 if (S_ISDIR(obj->oo_inode->i_mode))
2827 osd_fid_unpack((struct lu_fid *) rec,
2828 (struct osd_fid_pack *)iam_rec);
2832 osd_ipd_put(env, bag, ipd);
2834 LINVRNT(osd_invariant(obj));
2840 * Inserts (key, value) pair in \a dt index object.
2842 * \param dt osd index object
2843 * \param key key for index
2844 * \param rec record reference
2845 * \param th transaction handler
2848 * \retval -ve failure
2850 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2851 const struct dt_rec *rec, const struct dt_key *key,
2852 struct thandle *th, struct lustre_capa *capa,
2855 struct osd_object *obj = osd_dt_obj(dt);
2856 struct iam_path_descr *ipd;
2857 struct osd_thandle *oh;
2858 struct iam_container *bag = &obj->oo_dir->od_container;
2859 #ifdef HAVE_QUOTA_SUPPORT
2860 cfs_cap_t save = current->cap_effective;
2862 struct osd_thread_info *oti = osd_oti_get(env);
2863 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2868 LINVRNT(osd_invariant(obj));
2869 LASSERT(dt_object_exists(dt));
2870 LASSERT(bag->ic_object == obj->oo_inode);
2871 LASSERT(th != NULL);
2873 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2876 ipd = osd_idx_ipd_get(env, bag);
2877 if (unlikely(ipd == NULL))
2880 oh = container_of0(th, struct osd_thandle, ot_super);
2881 LASSERT(oh->ot_handle != NULL);
2882 LASSERT(oh->ot_handle->h_transaction != NULL);
2883 #ifdef HAVE_QUOTA_SUPPORT
2885 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2887 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2889 if (S_ISDIR(obj->oo_inode->i_mode))
2890 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2892 iam_rec = (struct iam_rec *) rec;
2893 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2895 #ifdef HAVE_QUOTA_SUPPORT
2896 current->cap_effective = save;
2898 osd_ipd_put(env, bag, ipd);
2899 LINVRNT(osd_invariant(obj));
2904 * Calls ldiskfs_add_entry() to add directory entry
2905 * into the directory. This is required for
2906 * interoperability mode (b11826)
2908 * \retval 0, on success
2909 * \retval -ve, on error
2911 static int __osd_ea_add_rec(struct osd_thread_info *info,
2912 struct osd_object *pobj,
2913 struct inode *cinode,
2915 const struct dt_rec *fid,
2918 struct ldiskfs_dentry_param *ldp;
2919 struct dentry *child;
2920 struct osd_thandle *oth;
2923 oth = container_of(th, struct osd_thandle, ot_super);
2924 LASSERT(oth->ot_handle != NULL);
2925 LASSERT(oth->ot_handle->h_transaction != NULL);
2927 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2929 if (fid_is_igif((struct lu_fid *)fid) ||
2930 fid_seq((struct lu_fid *)fid) >= FID_SEQ_NORMAL) {
2931 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2932 osd_get_ldiskfs_dirent_param(ldp, fid);
2933 child->d_fsdata = (void*) ldp;
2935 child->d_fsdata = NULL;
2936 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2942 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2943 * into the directory.Also sets flags into osd object to
2944 * indicate dot and dotdot are created. This is required for
2945 * interoperability mode (b11826)
2947 * \param dir directory for dot and dotdot fixup.
2948 * \param obj child object for linking
2950 * \retval 0, on success
2951 * \retval -ve, on error
2953 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2954 struct osd_object *dir,
2955 struct inode *parent_dir, const char *name,
2956 const struct dt_rec *dot_fid,
2957 const struct dt_rec *dot_dot_fid,
2960 struct inode *inode = dir->oo_inode;
2961 struct ldiskfs_dentry_param *dot_ldp;
2962 struct ldiskfs_dentry_param *dot_dot_ldp;
2963 struct osd_thandle *oth;
2966 oth = container_of(th, struct osd_thandle, ot_super);
2967 LASSERT(oth->ot_handle->h_transaction != NULL);
2968 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2970 if (strcmp(name, dot) == 0) {
2971 if (dir->oo_compat_dot_created) {
2974 LASSERT(inode == parent_dir);
2975 dir->oo_compat_dot_created = 1;
2978 } else if(strcmp(name, dotdot) == 0) {
2979 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2980 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2982 if (!dir->oo_compat_dot_created)
2984 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
2985 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
2986 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2991 /* in case of rename, dotdot is already created */
2992 if (dir->oo_compat_dotdot_created) {
2993 return __osd_ea_add_rec(info, dir, parent_dir, name,
2997 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode,
2998 dot_ldp, dot_dot_ldp);
3000 dir->oo_compat_dotdot_created = 1;
3008 * It will call the appropriate osd_add* function and return the
3009 * value, return by respective functions.
3011 static int osd_ea_add_rec(const struct lu_env *env,
3012 struct osd_object *pobj,
3013 struct inode *cinode,
3015 const struct dt_rec *fid,
3018 struct osd_thread_info *info = osd_oti_get(env);
3021 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3023 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3024 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3027 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid, th);
3033 * Calls ->lookup() to find dentry. From dentry get inode and
3034 * read inode's ea to get fid. This is required for interoperability
3037 * \retval 0, on success
3038 * \retval -ve, on error
3040 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3041 struct dt_rec *rec, const struct dt_key *key)
3043 struct inode *dir = obj->oo_inode;
3044 struct dentry *dentry;
3045 struct ldiskfs_dir_entry_2 *de;
3046 struct buffer_head *bh;
3047 struct lu_fid *fid = (struct lu_fid *) rec;
3051 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3053 dentry = osd_child_dentry_get(env, obj,
3054 (char *)key, strlen((char *)key));
3056 cfs_down_read(&obj->oo_ext_idx_sem);
3057 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
3059 ino = le32_to_cpu(de->inode);
3060 rc = osd_get_fid_from_dentry(de, rec);
3062 /* done with de, release bh */
3065 rc = osd_ea_fid_get(env, obj, ino, fid);
3069 cfs_up_read(&obj->oo_ext_idx_sem);
3074 * Find the osd object for given fid.
3076 * \param fid need to find the osd object having this fid
3078 * \retval osd_object on success
3079 * \retval -ve on error
3081 struct osd_object *osd_object_find(const struct lu_env *env,
3082 struct dt_object *dt,
3083 const struct lu_fid *fid)
3085 struct lu_device *ludev = dt->do_lu.lo_dev;
3086 struct osd_object *child = NULL;
3087 struct lu_object *luch;
3088 struct lu_object *lo;
3090 luch = lu_object_find(env, ludev, fid, NULL);
3091 if (!IS_ERR(luch)) {
3092 if (lu_object_exists(luch)) {
3093 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3095 child = osd_obj(lo);
3097 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3098 "lu_object can't be located"
3099 ""DFID"\n", PFID(fid));
3101 if (child == NULL) {
3102 lu_object_put(env, luch);
3103 CERROR("Unable to get osd_object\n");
3104 child = ERR_PTR(-ENOENT);
3107 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3108 "lu_object does not exists "DFID"\n",
3110 child = ERR_PTR(-ENOENT);
3113 child = (void *)luch;
3119 * Put the osd object once done with it.
3121 * \param obj osd object that needs to be put
3123 static inline void osd_object_put(const struct lu_env *env,
3124 struct osd_object *obj)
3126 lu_object_put(env, &obj->oo_dt.do_lu);
3130 * Index add function for interoperability mode (b11826).
3131 * It will add the directory entry.This entry is needed to
3132 * maintain name->fid mapping.
3134 * \param key it is key i.e. file entry to be inserted
3135 * \param rec it is value of given key i.e. fid
3137 * \retval 0, on success
3138 * \retval -ve, on error
3140 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3141 const struct dt_rec *rec,
3142 const struct dt_key *key, struct thandle *th,
3143 struct lustre_capa *capa, int ignore_quota)
3145 struct osd_object *obj = osd_dt_obj(dt);
3146 struct lu_fid *fid = (struct lu_fid *) rec;
3147 const char *name = (const char *)key;
3148 struct osd_object *child;
3149 #ifdef HAVE_QUOTA_SUPPORT
3150 cfs_cap_t save = current->cap_effective;
3156 LASSERT(osd_invariant(obj));
3157 LASSERT(dt_object_exists(dt));
3158 LASSERT(th != NULL);
3160 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3163 child = osd_object_find(env, dt, fid);
3164 if (!IS_ERR(child)) {
3165 struct inode *inode = obj->oo_inode;
3166 struct osd_thread_info *oti = osd_oti_get(env);
3167 struct timespec *ctime = &oti->oti_time;
3168 struct timespec *mtime = &oti->oti_time2;
3170 *ctime = inode->i_ctime;
3171 *mtime = inode->i_mtime;
3172 #ifdef HAVE_QUOTA_SUPPORT
3174 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
3176 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
3178 cfs_down_write(&obj->oo_ext_idx_sem);
3179 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3180 cfs_up_write(&obj->oo_ext_idx_sem);
3181 #ifdef HAVE_QUOTA_SUPPORT
3182 current->cap_effective = save;
3184 osd_object_put(env, child);
3185 /* xtime should not be updated with server-side time. */
3186 cfs_spin_lock(&obj->oo_guard);
3187 inode->i_ctime = *ctime;
3188 inode->i_mtime = *mtime;
3189 cfs_spin_unlock(&obj->oo_guard);
3190 mark_inode_dirty(inode);
3192 rc = PTR_ERR(child);
3195 LASSERT(osd_invariant(obj));
3200 * Initialize osd Iterator for given osd index object.
3202 * \param dt osd index object
3205 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3206 struct dt_object *dt,
3207 struct lustre_capa *capa)
3209 struct osd_it_iam *it;
3210 struct osd_thread_info *oti = osd_oti_get(env);
3211 struct osd_object *obj = osd_dt_obj(dt);
3212 struct lu_object *lo = &dt->do_lu;
3213 struct iam_path_descr *ipd;
3214 struct iam_container *bag = &obj->oo_dir->od_container;
3216 LASSERT(lu_object_exists(lo));
3218 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3219 return ERR_PTR(-EACCES);
3222 ipd = osd_it_ipd_get(env, bag);
3223 if (likely(ipd != NULL)) {
3227 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3228 return (struct dt_it *)it;
3230 return ERR_PTR(-ENOMEM);
3234 * free given Iterator.
3237 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3239 struct osd_it_iam *it = (struct osd_it_iam *)di;
3240 struct osd_object *obj = it->oi_obj;
3242 iam_it_fini(&it->oi_it);
3243 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3244 lu_object_put(env, &obj->oo_dt.do_lu);
3248 * Move Iterator to record specified by \a key
3250 * \param di osd iterator
3251 * \param key key for index
3253 * \retval +ve di points to record with least key not larger than key
3254 * \retval 0 di points to exact matched key
3255 * \retval -ve failure
3258 static int osd_it_iam_get(const struct lu_env *env,
3259 struct dt_it *di, const struct dt_key *key)
3261 struct osd_it_iam *it = (struct osd_it_iam *)di;
3263 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3269 * \param di osd iterator
3272 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3274 struct osd_it_iam *it = (struct osd_it_iam *)di;
3276 iam_it_put(&it->oi_it);
3280 * Move iterator by one record
3282 * \param di osd iterator
3284 * \retval +1 end of container reached
3286 * \retval -ve failure
3289 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3291 struct osd_it_iam *it = (struct osd_it_iam *)di;
3293 return iam_it_next(&it->oi_it);
3297 * Return pointer to the key under iterator.
3300 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3301 const struct dt_it *di)
3303 struct osd_it_iam *it = (struct osd_it_iam *)di;
3305 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3309 * Return size of key under iterator (in bytes)
3312 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3314 struct osd_it_iam *it = (struct osd_it_iam *)di;
3316 return iam_it_key_size(&it->oi_it);
3319 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3324 struct luda_type *lt;
3325 const unsigned align = sizeof(struct luda_type) - 1;
3327 /* check if file type is required */
3328 if (attr & LUDA_TYPE) {
3329 len = (len + align) & ~align;
3331 lt = (void *) ent->lde_name + len;
3332 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3333 ent->lde_attrs |= LUDA_TYPE;
3336 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3340 * build lu direct from backend fs dirent.
3343 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3351 fid_cpu_to_le(&ent->lde_fid, fid);
3352 ent->lde_attrs = LUDA_FID;
3354 ent->lde_hash = cpu_to_le64(offset);
3355 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3357 strncpy(ent->lde_name, name, namelen);
3358 ent->lde_namelen = cpu_to_le16(namelen);
3360 /* append lustre attributes */
3361 osd_it_append_attrs(ent, attr, namelen, type);
3365 * Return pointer to the record under iterator.
3367 static int osd_it_iam_rec(const struct lu_env *env,
3368 const struct dt_it *di,
3369 struct lu_dirent *lde,
3372 struct osd_it_iam *it = (struct osd_it_iam *)di;
3373 struct osd_thread_info *info = osd_oti_get(env);
3374 struct lu_fid *fid = &info->oti_fid;
3375 const struct osd_fid_pack *rec;
3381 name = (char *)iam_it_key_get(&it->oi_it);
3383 RETURN(PTR_ERR(name));
3385 namelen = iam_it_key_size(&it->oi_it);
3387 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3389 RETURN(PTR_ERR(rec));
3391 rc = osd_fid_unpack(fid, rec);
3395 hash = iam_it_store(&it->oi_it);
3397 /* IAM does not store object type in IAM index (dir) */
3398 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3405 * Returns cookie for current Iterator position.
3407 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3409 struct osd_it_iam *it = (struct osd_it_iam *)di;
3411 return iam_it_store(&it->oi_it);
3415 * Restore iterator from cookie.
3417 * \param di osd iterator
3418 * \param hash Iterator location cookie
3420 * \retval +ve di points to record with least key not larger than key.
3421 * \retval 0 di points to exact matched key
3422 * \retval -ve failure
3425 static int osd_it_iam_load(const struct lu_env *env,
3426 const struct dt_it *di, __u64 hash)
3428 struct osd_it_iam *it = (struct osd_it_iam *)di;
3430 return iam_it_load(&it->oi_it, hash);
3433 static const struct dt_index_operations osd_index_iam_ops = {
3434 .dio_lookup = osd_index_iam_lookup,
3435 .dio_insert = osd_index_iam_insert,
3436 .dio_delete = osd_index_iam_delete,
3438 .init = osd_it_iam_init,
3439 .fini = osd_it_iam_fini,
3440 .get = osd_it_iam_get,
3441 .put = osd_it_iam_put,
3442 .next = osd_it_iam_next,
3443 .key = osd_it_iam_key,
3444 .key_size = osd_it_iam_key_size,
3445 .rec = osd_it_iam_rec,
3446 .store = osd_it_iam_store,
3447 .load = osd_it_iam_load
3452 * Creates or initializes iterator context.
3454 * \retval struct osd_it_ea, iterator structure on success
3457 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3458 struct dt_object *dt,
3459 struct lustre_capa *capa)
3461 struct osd_object *obj = osd_dt_obj(dt);
3462 struct osd_thread_info *info = osd_oti_get(env);
3463 struct osd_it_ea *it = &info->oti_it_ea;
3464 struct lu_object *lo = &dt->do_lu;
3465 struct dentry *obj_dentry = &info->oti_it_dentry;
3467 LASSERT(lu_object_exists(lo));
3469 obj_dentry->d_inode = obj->oo_inode;
3470 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3471 obj_dentry->d_name.hash = 0;
3473 it->oie_rd_dirent = 0;
3474 it->oie_it_dirent = 0;
3475 it->oie_dirent = NULL;
3476 it->oie_buf = info->oti_it_ea_buf;
3478 it->oie_file.f_pos = 0;
3479 it->oie_file.f_dentry = obj_dentry;
3480 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3481 it->oie_file.f_op = obj->oo_inode->i_fop;
3482 it->oie_file.private_data = NULL;
3484 RETURN((struct dt_it *) it);
3488 * Destroy or finishes iterator context.
3490 * \param di iterator structure to be destroyed
3492 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3494 struct osd_it_ea *it = (struct osd_it_ea *)di;
3495 struct osd_object *obj = it->oie_obj;
3496 struct inode *inode = obj->oo_inode;
3499 it->oie_file.f_op->release(inode, &it->oie_file);
3500 lu_object_put(env, &obj->oo_dt.do_lu);
3505 * It position the iterator at given key, so that next lookup continues from
3506 * that key Or it is similar to dio_it->load() but based on a key,
3507 * rather than file position.
3509 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3512 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3514 static int osd_it_ea_get(const struct lu_env *env,
3515 struct dt_it *di, const struct dt_key *key)
3517 struct osd_it_ea *it = (struct osd_it_ea *)di;
3520 LASSERT(((const char *)key)[0] == '\0');
3521 it->oie_file.f_pos = 0;
3522 it->oie_rd_dirent = 0;
3523 it->oie_it_dirent = 0;
3524 it->oie_dirent = NULL;
3532 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3537 * It is called internally by ->readdir(). It fills the
3538 * iterator's in-memory data structure with required
3539 * information i.e. name, namelen, rec_size etc.
3541 * \param buf in which information to be filled in.
3542 * \param name name of the file in given dir
3544 * \retval 0 on success
3545 * \retval 1 on buffer full
3547 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3548 loff_t offset, __u64 ino,
3551 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3552 struct osd_it_ea_dirent *ent = it->oie_dirent;
3553 struct lu_fid *fid = &ent->oied_fid;
3554 struct osd_fid_pack *rec;
3557 /* this should never happen */
3558 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3559 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3563 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3567 if (d_type & LDISKFS_DIRENT_LUFID) {
3568 rec = (struct osd_fid_pack*) (name + namelen + 1);
3570 if (osd_fid_unpack(fid, rec) != 0)
3573 d_type &= ~LDISKFS_DIRENT_LUFID;
3578 ent->oied_ino = ino;
3579 ent->oied_off = offset;
3580 ent->oied_namelen = namelen;
3581 ent->oied_type = d_type;
3583 memcpy(ent->oied_name, name, namelen);
3585 it->oie_rd_dirent++;
3586 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3591 * Calls ->readdir() to load a directory entry at a time
3592 * and stored it in iterator's in-memory data structure.
3594 * \param di iterator's in memory structure
3596 * \retval 0 on success
3597 * \retval -ve on error
3599 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3601 struct osd_it_ea *it = (struct osd_it_ea *)di;
3602 struct osd_object *obj = it->oie_obj;
3603 struct inode *inode = obj->oo_inode;
3607 it->oie_dirent = it->oie_buf;
3608 it->oie_rd_dirent = 0;
3610 cfs_down_read(&obj->oo_ext_idx_sem);
3611 result = inode->i_fop->readdir(&it->oie_file, it,
3612 (filldir_t) osd_ldiskfs_filldir);
3614 cfs_up_read(&obj->oo_ext_idx_sem);
3616 if (it->oie_rd_dirent == 0) {
3619 it->oie_dirent = it->oie_buf;
3620 it->oie_it_dirent = 1;
3627 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3628 * to load a directory entry at a time and stored it in
3629 * iterator's in-memory data structure.
3631 * \param di iterator's in memory structure
3633 * \retval +ve iterator reached to end
3634 * \retval 0 iterator not reached to end
3635 * \retval -ve on error
3637 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3639 struct osd_it_ea *it = (struct osd_it_ea *)di;
3644 if (it->oie_it_dirent < it->oie_rd_dirent) {
3646 (void *) it->oie_dirent +
3647 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3648 it->oie_dirent->oied_namelen);
3649 it->oie_it_dirent++;
3652 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3655 rc = osd_ldiskfs_it_fill(di);
3662 * Returns the key at current position from iterator's in memory structure.
3664 * \param di iterator's in memory structure
3666 * \retval key i.e. struct dt_key on success
3668 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3669 const struct dt_it *di)
3671 struct osd_it_ea *it = (struct osd_it_ea *)di;
3673 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3677 * Returns the key's size at current position from iterator's in memory structure.
3679 * \param di iterator's in memory structure
3681 * \retval key_size i.e. struct dt_key on success
3683 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3685 struct osd_it_ea *it = (struct osd_it_ea *)di;
3687 RETURN(it->oie_dirent->oied_namelen);
3692 * Returns the value (i.e. fid/igif) at current position from iterator's
3693 * in memory structure.
3695 * \param di struct osd_it_ea, iterator's in memory structure
3696 * \param attr attr requested for dirent.
3697 * \param lde lustre dirent
3699 * \retval 0 no error and \param lde has correct lustre dirent.
3700 * \retval -ve on error
3702 static inline int osd_it_ea_rec(const struct lu_env *env,
3703 const struct dt_it *di,
3704 struct lu_dirent *lde,
3707 struct osd_it_ea *it = (struct osd_it_ea *)di;
3708 struct osd_object *obj = it->oie_obj;
3709 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3714 if (!fid_is_sane(fid))
3715 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3718 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3719 it->oie_dirent->oied_name,
3720 it->oie_dirent->oied_namelen,
3721 it->oie_dirent->oied_type,
3727 * Returns a cookie for current position of the iterator head, so that
3728 * user can use this cookie to load/start the iterator next time.
3730 * \param di iterator's in memory structure
3732 * \retval cookie for current position, on success
3734 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3736 struct osd_it_ea *it = (struct osd_it_ea *)di;
3738 RETURN(it->oie_dirent->oied_off);
3742 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3743 * to load a directory entry at a time and stored it i inn,
3744 * in iterator's in-memory data structure.
3746 * \param di struct osd_it_ea, iterator's in memory structure
3748 * \retval +ve on success
3749 * \retval -ve on error
3751 static int osd_it_ea_load(const struct lu_env *env,
3752 const struct dt_it *di, __u64 hash)
3754 struct osd_it_ea *it = (struct osd_it_ea *)di;
3758 it->oie_file.f_pos = hash;
3760 rc = osd_ldiskfs_it_fill(di);
3768 * Index lookup function for interoperability mode (b11826).
3770 * \param key, key i.e. file name to be searched
3772 * \retval +ve, on success
3773 * \retval -ve, on error
3775 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3776 struct dt_rec *rec, const struct dt_key *key,
3777 struct lustre_capa *capa)
3779 struct osd_object *obj = osd_dt_obj(dt);
3784 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3785 LINVRNT(osd_invariant(obj));
3787 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3790 rc = osd_ea_lookup_rec(env, obj, rec, key);
3798 * Index and Iterator operations for interoperability
3799 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3801 static const struct dt_index_operations osd_index_ea_ops = {
3802 .dio_lookup = osd_index_ea_lookup,
3803 .dio_insert = osd_index_ea_insert,
3804 .dio_delete = osd_index_ea_delete,
3806 .init = osd_it_ea_init,
3807 .fini = osd_it_ea_fini,
3808 .get = osd_it_ea_get,
3809 .put = osd_it_ea_put,
3810 .next = osd_it_ea_next,
3811 .key = osd_it_ea_key,
3812 .key_size = osd_it_ea_key_size,
3813 .rec = osd_it_ea_rec,
3814 .store = osd_it_ea_store,
3815 .load = osd_it_ea_load
3819 static void *osd_key_init(const struct lu_context *ctx,
3820 struct lu_context_key *key)
3822 struct osd_thread_info *info;
3824 OBD_ALLOC_PTR(info);
3826 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3827 if (info->oti_it_ea_buf != NULL) {
3828 info->oti_env = container_of(ctx, struct lu_env,
3832 info = ERR_PTR(-ENOMEM);
3835 info = ERR_PTR(-ENOMEM);
3840 static void osd_key_fini(const struct lu_context *ctx,
3841 struct lu_context_key *key, void* data)
3843 struct osd_thread_info *info = data;
3845 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3849 static void osd_key_exit(const struct lu_context *ctx,
3850 struct lu_context_key *key, void *data)
3852 struct osd_thread_info *info = data;
3854 LASSERT(info->oti_r_locks == 0);
3855 LASSERT(info->oti_w_locks == 0);
3856 LASSERT(info->oti_txns == 0);
3859 /* type constructor/destructor: osd_type_init, osd_type_fini */
3860 LU_TYPE_INIT_FINI(osd, &osd_key);
3862 static struct lu_context_key osd_key = {
3863 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3864 .lct_init = osd_key_init,
3865 .lct_fini = osd_key_fini,
3866 .lct_exit = osd_key_exit
3870 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3871 const char *name, struct lu_device *next)
3874 struct lu_context *ctx;
3876 /* context for commit hooks */
3877 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3878 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3880 rc = osd_procfs_init(osd_dev(d), name);
3881 ctx->lc_cookie = 0x3;
3886 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3888 struct osd_thread_info *info = osd_oti_get(env);
3890 if (o->od_obj_area != NULL) {
3891 lu_object_put(env, &o->od_obj_area->do_lu);
3892 o->od_obj_area = NULL;
3894 osd_oi_fini(info, &o->od_oi);
3899 static int osd_mount(const struct lu_env *env,
3900 struct osd_device *o, struct lustre_cfg *cfg)
3902 struct lustre_mount_info *lmi;
3903 const char *dev = lustre_cfg_string(cfg, 0);
3904 struct lustre_disk_data *ldd;
3905 struct lustre_sb_info *lsi;
3908 if (o->od_mount != NULL) {
3909 CERROR("Already mounted (%s)\n", dev);
3914 lmi = server_get_mount(dev);
3916 CERROR("Cannot get mount info for %s!\n", dev);
3920 LASSERT(lmi != NULL);
3921 /* save lustre_mount_info in dt_device */
3924 lsi = s2lsi(lmi->lmi_sb);
3927 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3929 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3933 o->od_obj_area = NULL;
3937 static struct lu_device *osd_device_fini(const struct lu_env *env,
3938 struct lu_device *d)
3943 shrink_dcache_sb(osd_sb(osd_dev(d)));
3944 osd_sync(env, lu2dt_dev(d));
3946 rc = osd_procfs_fini(osd_dev(d));
3948 CERROR("proc fini error %d \n", rc);
3949 RETURN (ERR_PTR(rc));
3952 if (osd_dev(d)->od_mount)
3953 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3954 osd_dev(d)->od_mount->lmi_mnt);
3955 osd_dev(d)->od_mount = NULL;
3957 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3961 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3962 struct lu_device_type *t,
3963 struct lustre_cfg *cfg)
3965 struct lu_device *l;
3966 struct osd_device *o;
3972 result = dt_device_init(&o->od_dt_dev, t);
3975 l->ld_ops = &osd_lu_ops;
3976 o->od_dt_dev.dd_ops = &osd_dt_ops;
3977 cfs_spin_lock_init(&o->od_osfs_lock);
3978 o->od_osfs_age = cfs_time_shift_64(-1000);
3979 o->od_capa_hash = init_capa_hash();
3980 if (o->od_capa_hash == NULL) {
3981 dt_device_fini(&o->od_dt_dev);
3982 l = ERR_PTR(-ENOMEM);
3985 l = ERR_PTR(result);
3990 l = ERR_PTR(-ENOMEM);
3994 static struct lu_device *osd_device_free(const struct lu_env *env,
3995 struct lu_device *d)
3997 struct osd_device *o = osd_dev(d);
4000 cleanup_capa_hash(o->od_capa_hash);
4001 dt_device_fini(&o->od_dt_dev);
4006 static int osd_process_config(const struct lu_env *env,
4007 struct lu_device *d, struct lustre_cfg *cfg)
4009 struct osd_device *o = osd_dev(d);
4013 switch(cfg->lcfg_command) {
4015 err = osd_mount(env, o, cfg);
4018 err = osd_shutdown(env, o);
4027 static int osd_recovery_complete(const struct lu_env *env,
4028 struct lu_device *d)
4033 static int osd_prepare(const struct lu_env *env,
4034 struct lu_device *pdev,
4035 struct lu_device *dev)
4037 struct osd_device *osd = osd_dev(dev);
4038 struct lustre_sb_info *lsi;
4039 struct lustre_disk_data *ldd;
4040 struct lustre_mount_info *lmi;
4041 struct osd_thread_info *oti = osd_oti_get(env);
4042 struct dt_object *d;
4046 /* 1. initialize oi before any file create or file open */
4047 result = osd_oi_init(oti, &osd->od_oi,
4048 &osd->od_dt_dev, lu2md_dev(pdev));
4052 lmi = osd->od_mount;
4053 lsi = s2lsi(lmi->lmi_sb);
4056 /* 2. setup local objects */
4057 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4061 /* 3. open remote object dir */
4062 d = dt_store_open(env, lu2dt_dev(dev), "",
4063 remote_obj_dir, &oti->oti_fid);
4065 osd->od_obj_area = d;
4068 result = PTR_ERR(d);
4069 osd->od_obj_area = NULL;
4076 static const struct lu_object_operations osd_lu_obj_ops = {
4077 .loo_object_init = osd_object_init,
4078 .loo_object_delete = osd_object_delete,
4079 .loo_object_release = osd_object_release,
4080 .loo_object_free = osd_object_free,
4081 .loo_object_print = osd_object_print,
4082 .loo_object_invariant = osd_object_invariant
4085 static const struct lu_device_operations osd_lu_ops = {
4086 .ldo_object_alloc = osd_object_alloc,
4087 .ldo_process_config = osd_process_config,
4088 .ldo_recovery_complete = osd_recovery_complete,
4089 .ldo_prepare = osd_prepare,
4092 static const struct lu_device_type_operations osd_device_type_ops = {
4093 .ldto_init = osd_type_init,
4094 .ldto_fini = osd_type_fini,
4096 .ldto_start = osd_type_start,
4097 .ldto_stop = osd_type_stop,
4099 .ldto_device_alloc = osd_device_alloc,
4100 .ldto_device_free = osd_device_free,
4102 .ldto_device_init = osd_device_init,
4103 .ldto_device_fini = osd_device_fini
4106 static struct lu_device_type osd_device_type = {
4107 .ldt_tags = LU_DEVICE_DT,
4108 .ldt_name = LUSTRE_OSD_NAME,
4109 .ldt_ops = &osd_device_type_ops,
4110 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4114 * lprocfs legacy support.
4116 static struct obd_ops osd_obd_device_ops = {
4117 .o_owner = THIS_MODULE
4120 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4121 .llod_name = remote_obj_dir,
4122 .llod_oid = OSD_REM_OBJ_DIR_OID,
4124 .llod_feat = &dt_directory_features,
4127 static int __init osd_mod_init(void)
4129 struct lprocfs_static_vars lvars;
4132 llo_local_obj_register(&llod_osd_rem_obj_dir);
4133 lprocfs_osd_init_vars(&lvars);
4134 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4135 LUSTRE_OSD_NAME, &osd_device_type);
4138 static void __exit osd_mod_exit(void)
4140 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4141 class_unregister_type(LUSTRE_OSD_NAME);
4144 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4145 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4146 MODULE_LICENSE("GPL");
4148 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);