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 2008 Sun Microsystems, Inc. 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>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
50 /* LUSTRE_VERSION_CODE */
51 #include <lustre_ver.h>
52 /* prerequisite for linux/xattr.h */
53 #include <linux/types.h>
54 /* prerequisite for linux/xattr.h */
56 /* XATTR_{REPLACE,CREATE} */
57 #include <linux/xattr.h>
59 * XXX temporary stuff: direct access to ldiskfs/jdb. Interface between osd
60 * and file system is not yet specified.
62 /* handle_t, journal_start(), journal_stop() */
63 #include <linux/jbd.h>
65 #include <linux/ldiskfs_fs.h>
66 #include <linux/ldiskfs_jbd.h>
71 * struct OBD_{ALLOC,FREE}*()
74 #include <obd_support.h>
75 /* struct ptlrpc_thread */
76 #include <lustre_net.h>
79 #include <lustre_fid.h>
80 #include <linux/lustre_iam.h>
82 #include "osd_internal.h"
85 /* llo_* api support */
86 #include <md_object.h>
88 static const char MDT_XATTR_NAME[] = "trusted.lma";
89 static const char dot[] = ".";
90 static const char dotdot[] = "..";
91 static const char remote_obj_dir[] = "REM_OBJ_DIR";
93 struct osd_directory {
94 struct iam_container od_container;
95 struct iam_descr od_descr;
96 struct semaphore od_sem;
100 struct dt_object oo_dt;
102 * Inode for file system object represented by this osd_object. This
103 * inode is pinned for the whole duration of lu_object life.
105 * Not modified concurrently (either setup early during object
106 * creation, or assigned by osd_object_create() under write lock).
108 struct inode *oo_inode;
109 struct rw_semaphore oo_sem;
110 struct osd_directory *oo_dir;
111 /** protects inode attributes. */
114 * Following two members are used to indicate the presence of dot and
115 * dotdot in the given directory. This is required for interop mode
118 int oo_compat_dot_created;
119 int oo_compat_dotdot_created;
121 const struct lu_env *oo_owner;
122 #ifdef CONFIG_LOCKDEP
123 struct lockdep_map oo_dep_map;
127 static int osd_root_get (const struct lu_env *env,
128 struct dt_device *dev, struct lu_fid *f);
130 static int lu_device_is_osd (const struct lu_device *d);
131 static void osd_mod_exit (void) __exit;
132 static int osd_mod_init (void) __init;
133 static int osd_type_init (struct lu_device_type *t);
134 static void osd_type_fini (struct lu_device_type *t);
135 static int osd_object_init (const struct lu_env *env,
137 const struct lu_object_conf *_);
138 static void osd_object_release(const struct lu_env *env,
139 struct lu_object *l);
140 static int osd_object_print (const struct lu_env *env, void *cookie,
141 lu_printer_t p, const struct lu_object *o);
142 static struct lu_device *osd_device_free (const struct lu_env *env,
143 struct lu_device *m);
144 static void *osd_key_init (const struct lu_context *ctx,
145 struct lu_context_key *key);
146 static void osd_key_fini (const struct lu_context *ctx,
147 struct lu_context_key *key, void *data);
148 static void osd_key_exit (const struct lu_context *ctx,
149 struct lu_context_key *key, void *data);
150 static int osd_has_index (const struct osd_object *obj);
151 static void osd_object_init0 (struct osd_object *obj);
152 static int osd_device_init (const struct lu_env *env,
153 struct lu_device *d, const char *,
155 static int osd_fid_lookup (const struct lu_env *env,
156 struct osd_object *obj,
157 const struct lu_fid *fid);
158 static void osd_inode_getattr (const struct lu_env *env,
159 struct inode *inode, struct lu_attr *attr);
160 static int osd_inode_setattr (const struct lu_env *env,
161 struct inode *inode, const struct lu_attr *attr);
162 static int osd_param_is_sane (const struct osd_device *dev,
163 const struct txn_param *param);
164 static int osd_index_iam_lookup(const struct lu_env *env,
165 struct dt_object *dt,
166 struct dt_rec *rec, const struct dt_key *key,
167 struct lustre_capa *capa);
168 static int osd_index_ea_lookup(const struct lu_env *env,
169 struct dt_object *dt,
170 struct dt_rec *rec, const struct dt_key *key,
171 struct lustre_capa *capa);
172 static int osd_index_iam_insert(const struct lu_env *env,
173 struct dt_object *dt,
174 const struct dt_rec *rec,
175 const struct dt_key *key,
176 struct thandle *handle,
177 struct lustre_capa *capa,
179 static int osd_index_ea_insert (const struct lu_env *env,
180 struct dt_object *dt,
181 const struct dt_rec *rec,
182 const struct dt_key *key,
183 struct thandle *handle,
184 struct lustre_capa *capa,
186 static int osd_index_iam_delete(const struct lu_env *env,
187 struct dt_object *dt, const struct dt_key *key,
188 struct thandle *handle,
189 struct lustre_capa *capa);
190 static int osd_index_ea_delete (const struct lu_env *env,
191 struct dt_object *dt, const struct dt_key *key,
192 struct thandle *handle,
193 struct lustre_capa *capa);
195 static int osd_iam_index_probe (const struct lu_env *env,
196 struct osd_object *o,
197 const struct dt_index_features *feat);
198 static int osd_index_try (const struct lu_env *env,
199 struct dt_object *dt,
200 const struct dt_index_features *feat);
201 static void osd_index_fini (struct osd_object *o);
203 static void osd_it_iam_fini (const struct lu_env *env, struct dt_it *di);
204 static int osd_it_iam_get (const struct lu_env *env,
205 struct dt_it *di, const struct dt_key *key);
206 static void osd_it_iam_put (const struct lu_env *env, struct dt_it *di);
207 static int osd_it_iam_next (const struct lu_env *env, struct dt_it *di);
208 static int osd_it_iam_key_size (const struct lu_env *env,
209 const struct dt_it *di);
210 static void osd_it_ea_fini (const struct lu_env *env, struct dt_it *di);
211 static int osd_it_ea_get (const struct lu_env *env,
212 struct dt_it *di, const struct dt_key *key);
213 static void osd_it_ea_put (const struct lu_env *env, struct dt_it *di);
214 static int osd_it_ea_next (const struct lu_env *env, struct dt_it *di);
215 static int osd_it_ea_key_size(const struct lu_env *env,
216 const struct dt_it *di);
218 static void osd_conf_get (const struct lu_env *env,
219 const struct dt_device *dev,
220 struct dt_device_param *param);
221 static void osd_trans_stop (const struct lu_env *env,
223 static int osd_object_is_root(const struct osd_object *obj);
225 static struct osd_object *osd_obj (const struct lu_object *o);
226 static struct osd_device *osd_dev (const struct lu_device *d);
227 static struct osd_device *osd_dt_dev (const struct dt_device *d);
228 static struct osd_object *osd_dt_obj (const struct dt_object *d);
229 static struct osd_device *osd_obj2dev (const struct osd_object *o);
230 static struct lu_device *osd2lu_dev (struct osd_device *osd);
231 static struct lu_device *osd_device_fini (const struct lu_env *env,
232 struct lu_device *d);
233 static struct lu_device *osd_device_alloc (const struct lu_env *env,
234 struct lu_device_type *t,
235 struct lustre_cfg *cfg);
236 static struct lu_object *osd_object_alloc (const struct lu_env *env,
237 const struct lu_object_header *hdr,
238 struct lu_device *d);
239 static struct inode *osd_iget (struct osd_thread_info *info,
240 struct osd_device *dev,
241 const struct osd_inode_id *id);
242 static struct super_block *osd_sb (const struct osd_device *dev);
243 static struct dt_it *osd_it_iam_init (const struct lu_env *env,
244 struct dt_object *dt,
245 struct lustre_capa *capa);
246 static struct dt_key *osd_it_iam_key (const struct lu_env *env,
247 const struct dt_it *di);
248 static struct dt_rec *osd_it_iam_rec (const struct lu_env *env,
249 const struct dt_it *di);
250 static struct dt_it *osd_it_ea_init (const struct lu_env *env,
251 struct dt_object *dt,
252 struct lustre_capa *capa);
253 static struct dt_key *osd_it_ea_key (const struct lu_env *env,
254 const struct dt_it *di);
255 static struct dt_rec *osd_it_ea_rec (const struct lu_env *env,
256 const struct dt_it *di);
258 static struct timespec *osd_inode_time (const struct lu_env *env,
261 static struct thandle *osd_trans_start (const struct lu_env *env,
263 struct txn_param *p);
264 static journal_t *osd_journal (const struct osd_device *dev);
266 static int __osd_ea_add_rec(struct osd_thread_info *info,
267 struct osd_object *pobj,
268 struct osd_object *cobj,
272 static const struct lu_device_type_operations osd_device_type_ops;
273 static struct lu_device_type osd_device_type;
274 static const struct lu_object_operations osd_lu_obj_ops;
275 static struct obd_ops osd_obd_device_ops;
276 static const struct lu_device_operations osd_lu_ops;
277 static struct lu_context_key osd_key;
278 static const struct dt_object_operations osd_obj_ops;
279 static const struct dt_object_operations osd_obj_ea_ops;
280 static const struct dt_body_operations osd_body_ops;
281 static const struct dt_index_operations osd_index_iam_ops;
282 static const struct dt_index_operations osd_index_ea_ops;
285 struct thandle ot_super;
287 struct journal_callback ot_jcb;
288 /* Link to the device, for debugging. */
289 struct lu_ref_link *ot_dev_link;
293 #ifdef HAVE_QUOTA_SUPPORT
295 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
297 struct md_ucred *uc = md_ucred(env);
301 save->oc_uid = current->fsuid;
302 save->oc_gid = current->fsgid;
303 save->oc_cap = current->cap_effective;
304 current->fsuid = uc->mu_fsuid;
305 current->fsgid = uc->mu_fsgid;
306 current->cap_effective = uc->mu_cap;
310 osd_pop_ctxt(struct osd_ctxt *save)
312 current->fsuid = save->oc_uid;
313 current->fsgid = save->oc_gid;
314 current->cap_effective = save->oc_cap;
319 * Invariants, assertions.
323 * XXX: do not enable this, until invariant checking code is made thread safe
324 * in the face of pdirops locking.
326 #define OSD_INVARIANT_CHECKS (0)
328 #if OSD_INVARIANT_CHECKS
329 static int osd_invariant(const struct osd_object *obj)
333 ergo(obj->oo_inode != NULL,
334 obj->oo_inode->i_sb == osd_sb(osd_obj2dev(obj)) &&
335 atomic_read(&obj->oo_inode->i_count) > 0) &&
336 ergo(obj->oo_dir != NULL &&
337 obj->oo_dir->od_conationer.ic_object != NULL,
338 obj->oo_dir->od_conationer.ic_object == obj->oo_inode);
341 #define osd_invariant(obj) (1)
344 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
346 return lu_context_key_get(&env->le_ctx, &osd_key);
350 * Concurrency: doesn't matter
352 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
354 return osd_oti_get(env)->oti_r_locks > 0;
358 * Concurrency: doesn't matter
360 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
362 struct osd_thread_info *oti = osd_oti_get(env);
363 return oti->oti_w_locks > 0 && o->oo_owner == env;
367 * Concurrency: doesn't access mutable data
369 static int osd_root_get(const struct lu_env *env,
370 struct dt_device *dev, struct lu_fid *f)
374 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
375 lu_igif_build(f, inode->i_ino, inode->i_generation);
380 * OSD object methods.
384 * Concurrency: no concurrent access is possible that early in object
387 static struct lu_object *osd_object_alloc(const struct lu_env *env,
388 const struct lu_object_header *hdr,
391 struct osd_object *mo;
397 l = &mo->oo_dt.do_lu;
398 dt_object_init(&mo->oo_dt, NULL, d);
399 if (osd_dev(d)->od_iop_mode)
400 mo->oo_dt.do_ops = &osd_obj_ea_ops;
402 mo->oo_dt.do_ops = &osd_obj_ops;
404 l->lo_ops = &osd_lu_obj_ops;
405 init_rwsem(&mo->oo_sem);
406 spin_lock_init(&mo->oo_guard);
413 * Concurrency: shouldn't matter.
415 static void osd_object_init0(struct osd_object *obj)
417 LASSERT(obj->oo_inode != NULL);
418 obj->oo_dt.do_body_ops = &osd_body_ops;
419 obj->oo_dt.do_lu.lo_header->loh_attr |=
420 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
424 * Concurrency: no concurrent access is possible that early in object
427 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
428 const struct lu_object_conf *_)
430 struct osd_object *obj = osd_obj(l);
433 LINVRNT(osd_invariant(obj));
435 result = osd_fid_lookup(env, obj, lu_object_fid(l));
437 if (obj->oo_inode != NULL)
438 osd_object_init0(obj);
440 LINVRNT(osd_invariant(obj));
445 * Concurrency: no concurrent access is possible that late in object
448 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
450 struct osd_object *obj = osd_obj(l);
452 LINVRNT(osd_invariant(obj));
454 dt_object_fini(&obj->oo_dt);
458 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
459 const struct iam_container *bag)
461 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
462 osd_oti_get(env)->oti_it_ipd);
465 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
466 const struct iam_container *bag)
468 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
469 osd_oti_get(env)->oti_idx_ipd);
472 static void osd_ipd_put(const struct lu_env *env,
473 const struct iam_container *bag,
474 struct iam_path_descr *ipd)
476 bag->ic_descr->id_ops->id_ipd_free(ipd);
480 * Concurrency: no concurrent access is possible that late in object
483 static void osd_index_fini(struct osd_object *o)
485 struct iam_container *bag;
487 if (o->oo_dir != NULL) {
488 bag = &o->oo_dir->od_container;
489 if (o->oo_inode != NULL) {
490 if (bag->ic_object == o->oo_inode)
491 iam_container_fini(bag);
493 OBD_FREE_PTR(o->oo_dir);
499 * Concurrency: no concurrent access is possible that late in object
500 * life-cycle (for all existing callers, that is. New callers have to provide
501 * their own locking.)
503 static int osd_inode_unlinked(const struct inode *inode)
505 return inode->i_nlink == 0;
509 OSD_TXN_OI_DELETE_CREDITS = 20,
510 OSD_TXN_INODE_DELETE_CREDITS = 20
514 * Concurrency: no concurrent access is possible that late in object
517 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
519 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
520 struct osd_device *osd = osd_obj2dev(obj);
521 struct osd_thread_info *oti = osd_oti_get(env);
522 struct txn_param *prm = &oti->oti_txn;
526 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
527 OSD_TXN_INODE_DELETE_CREDITS);
528 th = osd_trans_start(env, &osd->od_dt_dev, prm);
530 result = osd_oi_delete(oti, &osd->od_oi, fid, th);
531 osd_trans_stop(env, th);
533 result = PTR_ERR(th);
538 * Called just before object is freed. Releases all resources except for
539 * object itself (that is released by osd_object_free()).
541 * Concurrency: no concurrent access is possible that late in object
544 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
546 struct osd_object *obj = osd_obj(l);
547 struct inode *inode = obj->oo_inode;
549 LINVRNT(osd_invariant(obj));
552 * If object is unlinked remove fid->ino mapping from object index.
559 if (osd_inode_unlinked(inode)) {
560 result = osd_inode_remove(env, obj);
562 LU_OBJECT_DEBUG(D_ERROR, env, l,
563 "Failed to cleanup: %d\n",
568 obj->oo_inode = NULL;
573 * Concurrency: ->loo_object_release() is called under site spin-lock.
575 static void osd_object_release(const struct lu_env *env,
578 struct osd_object *o = osd_obj(l);
580 LASSERT(!lu_object_is_dying(l->lo_header));
581 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
582 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
586 * Concurrency: shouldn't matter.
588 static int osd_object_print(const struct lu_env *env, void *cookie,
589 lu_printer_t p, const struct lu_object *l)
591 struct osd_object *o = osd_obj(l);
594 if (o->oo_dir != NULL)
595 d = o->oo_dir->od_container.ic_descr;
598 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
600 o->oo_inode ? o->oo_inode->i_ino : 0UL,
601 o->oo_inode ? o->oo_inode->i_generation : 0,
602 d ? d->id_ops->id_name : "plain");
606 * Concurrency: shouldn't matter.
608 int osd_statfs(const struct lu_env *env, struct dt_device *d,
611 struct osd_device *osd = osd_dt_dev(d);
612 struct super_block *sb = osd_sb(osd);
615 spin_lock(&osd->od_osfs_lock);
617 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
618 result = ll_do_statfs(sb, &osd->od_kstatfs);
619 if (likely(result == 0)) /* N.B. statfs can't really fail */
620 osd->od_osfs_age = cfs_time_current_64();
623 if (likely(result == 0))
624 *sfs = osd->od_kstatfs;
625 spin_unlock(&osd->od_osfs_lock);
631 * Concurrency: doesn't access mutable data.
633 static void osd_conf_get(const struct lu_env *env,
634 const struct dt_device *dev,
635 struct dt_device_param *param)
638 * XXX should be taken from not-yet-existing fs abstraction layer.
640 param->ddp_max_name_len = LDISKFS_NAME_LEN;
641 param->ddp_max_nlink = LDISKFS_LINK_MAX;
642 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
646 * Helper function to get and fill the buffer with input values.
648 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
652 buf = &osd_oti_get(env)->oti_buf;
663 * Concurrency: doesn't access mutable data.
665 static int osd_param_is_sane(const struct osd_device *dev,
666 const struct txn_param *param)
668 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
672 * Concurrency: shouldn't matter.
674 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
676 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
677 struct thandle *th = &oh->ot_super;
678 struct dt_device *dev = th->th_dev;
679 struct lu_device *lud = &dev->dd_lu_dev;
681 LASSERT(dev != NULL);
682 LASSERT(oh->ot_handle == NULL);
685 CERROR("transaction @0x%p commit error: %d\n", th, error);
687 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
689 * This od_env_for_commit is only for commit usage. see
692 lu_context_enter(&env->le_ctx);
693 dt_txn_hook_commit(env, th);
694 lu_context_exit(&env->le_ctx);
697 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
701 lu_context_exit(&th->th_ctx);
702 lu_context_fini(&th->th_ctx);
707 * Concurrency: shouldn't matter.
709 static struct thandle *osd_trans_start(const struct lu_env *env,
713 struct osd_device *dev = osd_dt_dev(d);
715 struct osd_thandle *oh;
721 hook_res = dt_txn_hook_start(env, d, p);
723 RETURN(ERR_PTR(hook_res));
725 if (osd_param_is_sane(dev, p)) {
726 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
728 struct osd_thread_info *oti = osd_oti_get(env);
731 * XXX temporary stuff. Some abstraction layer should
735 jh = journal_start(osd_journal(dev), p->tp_credits);
741 jh->h_sync = p->tp_sync;
742 lu_device_get(&d->dd_lu_dev);
743 oh->ot_dev_link = lu_ref_add
744 (&d->dd_lu_dev.ld_reference,
746 /* add commit callback */
747 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
748 lu_context_enter(&th->th_ctx);
749 journal_callback_set(jh, osd_trans_commit_cb,
750 (struct journal_callback *)&oh->ot_jcb);
751 LASSERT(oti->oti_txns == 0);
752 LASSERT(oti->oti_r_locks == 0);
753 LASSERT(oti->oti_w_locks == 0);
760 th = ERR_PTR(-ENOMEM);
762 CERROR("Invalid transaction parameters\n");
763 th = ERR_PTR(-EINVAL);
770 * Concurrency: shouldn't matter.
772 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
775 struct osd_thandle *oh;
776 struct osd_thread_info *oti = osd_oti_get(env);
780 oh = container_of0(th, struct osd_thandle, ot_super);
781 if (oh->ot_handle != NULL) {
782 handle_t *hdl = oh->ot_handle;
784 LASSERT(oti->oti_txns == 1);
786 LASSERT(oti->oti_r_locks == 0);
787 LASSERT(oti->oti_w_locks == 0);
788 result = dt_txn_hook_stop(env, th);
790 CERROR("Failure in transaction hook: %d\n", result);
791 oh->ot_handle = NULL;
792 result = journal_stop(hdl);
794 CERROR("Failure to stop transaction: %d\n", result);
800 * Concurrency: shouldn't matter.
802 static int osd_sync(const struct lu_env *env, struct dt_device *d)
804 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
805 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
809 * Start commit for OSD device.
811 * An implementation of dt_commit_async method for OSD device.
812 * Asychronously starts underlayng fs sync and thereby a transaction
815 * \param env environment
818 * \see dt_device_operations
820 static int osd_commit_async(const struct lu_env *env,
823 struct super_block *s = osd_sb(osd_dt_dev(d));
826 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
827 RETURN(s->s_op->sync_fs(s, 0));
831 * Concurrency: shouldn't matter.
833 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
835 static void osd_ro(const struct lu_env *env, struct dt_device *d)
839 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
841 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
842 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
848 * Concurrency: serialization provided by callers.
850 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
851 int mode, unsigned long timeout, __u32 alg,
852 struct lustre_capa_key *keys)
854 struct osd_device *dev = osd_dt_dev(d);
857 dev->od_fl_capa = mode;
858 dev->od_capa_timeout = timeout;
859 dev->od_capa_alg = alg;
860 dev->od_capa_keys = keys;
865 * Concurrency: serialization provided by callers.
867 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
868 struct dt_quota_ctxt *ctxt, void *data)
870 struct obd_device *obd = (void *)ctxt;
871 struct vfsmount *mnt = (struct vfsmount *)data;
874 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
875 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
876 obd->obd_lvfs_ctxt.pwdmnt = mnt;
877 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
878 obd->obd_lvfs_ctxt.fs = get_ds();
884 * Note: we do not count into QUOTA here.
885 * If we mount with --data_journal we may need more.
887 static const int osd_dto_credits_noquota[DTO_NR] = {
890 * INDEX_EXTRA_TRANS_BLOCKS(8) +
891 * SINGLEDATA_TRANS_BLOCKS(8)
892 * XXX Note: maybe iam need more, since iam have more level than
895 [DTO_INDEX_INSERT] = 16,
896 [DTO_INDEX_DELETE] = 16,
900 [DTO_IDNEX_UPDATE] = 16,
902 * Create a object. The same as create object in EXT3.
903 * DATA_TRANS_BLOCKS(14) +
904 * INDEX_EXTRA_BLOCKS(8) +
905 * 3(inode bits, groups, GDT)
907 [DTO_OBJECT_CREATE] = 25,
911 [DTO_OBJECT_DELETE] = 25,
914 * 3(inode bits, group, GDT)
916 [DTO_ATTR_SET_BASE] = 3,
918 * Xattr set. The same as xattr of EXT3.
919 * DATA_TRANS_BLOCKS(14)
920 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
921 * also counted in. Do not know why?
923 [DTO_XATTR_SET] = 14,
926 * creadits for inode change during write.
928 [DTO_WRITE_BASE] = 3,
930 * credits for single block write.
932 [DTO_WRITE_BLOCK] = 14,
934 * Attr set credits for chown.
935 * 3 (inode bit, group, GDT)
937 [DTO_ATTR_SET_CHOWN]= 3
941 * Note: we count into QUOTA here.
942 * If we mount with --data_journal we may need more.
944 static const int osd_dto_credits_quota[DTO_NR] = {
946 * INDEX_EXTRA_TRANS_BLOCKS(8) +
947 * SINGLEDATA_TRANS_BLOCKS(8) +
948 * 2 * QUOTA_TRANS_BLOCKS(2)
950 [DTO_INDEX_INSERT] = 20,
952 * INDEX_EXTRA_TRANS_BLOCKS(8) +
953 * SINGLEDATA_TRANS_BLOCKS(8) +
954 * 2 * QUOTA_TRANS_BLOCKS(2)
956 [DTO_INDEX_DELETE] = 20,
960 [DTO_IDNEX_UPDATE] = 16,
962 * Create a object. Same as create object in EXT3 filesystem.
963 * DATA_TRANS_BLOCKS(16) +
964 * INDEX_EXTRA_BLOCKS(8) +
965 * 3(inode bits, groups, GDT) +
966 * 2 * QUOTA_INIT_BLOCKS(25)
968 [DTO_OBJECT_CREATE] = 77,
971 * DATA_TRANS_BLOCKS(16) +
972 * INDEX_EXTRA_BLOCKS(8) +
973 * 3(inode bits, groups, GDT) +
976 [DTO_OBJECT_DELETE] = 27,
979 * 3 (inode bit, group, GDT) +
981 [DTO_ATTR_SET_BASE] = 3,
983 * Xattr set. The same as xattr of EXT3.
984 * DATA_TRANS_BLOCKS(16)
985 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
986 * also counted in. Do not know why?
988 [DTO_XATTR_SET] = 16,
991 * creadits for inode change during write.
993 [DTO_WRITE_BASE] = 3,
995 * credits for single block write.
997 [DTO_WRITE_BLOCK] = 16,
999 * Attr set credits for chown.
1000 * 3 (inode bit, group, GDT) +
1001 * 2 * QUOTA_INIT_BLOCKS(25) +
1002 * 2 * QUOTA_DEL_BLOCKS(9)
1004 [DTO_ATTR_SET_CHOWN]= 71
1007 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1010 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1011 ARRAY_SIZE(osd_dto_credits_quota));
1012 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1013 #ifdef HAVE_QUOTA_SUPPORT
1014 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1015 return osd_dto_credits_quota[op];
1018 return osd_dto_credits_noquota[op];
1021 static const struct dt_device_operations osd_dt_ops = {
1022 .dt_root_get = osd_root_get,
1023 .dt_statfs = osd_statfs,
1024 .dt_trans_start = osd_trans_start,
1025 .dt_trans_stop = osd_trans_stop,
1026 .dt_conf_get = osd_conf_get,
1027 .dt_sync = osd_sync,
1029 .dt_commit_async = osd_commit_async,
1030 .dt_credit_get = osd_credit_get,
1031 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1032 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1035 static void osd_object_read_lock(const struct lu_env *env,
1036 struct dt_object *dt, unsigned role)
1038 struct osd_object *obj = osd_dt_obj(dt);
1039 struct osd_thread_info *oti = osd_oti_get(env);
1041 LINVRNT(osd_invariant(obj));
1043 LASSERT(obj->oo_owner != env);
1044 down_read_nested(&obj->oo_sem, role);
1046 LASSERT(obj->oo_owner == NULL);
1050 static void osd_object_write_lock(const struct lu_env *env,
1051 struct dt_object *dt, unsigned role)
1053 struct osd_object *obj = osd_dt_obj(dt);
1054 struct osd_thread_info *oti = osd_oti_get(env);
1056 LINVRNT(osd_invariant(obj));
1058 LASSERT(obj->oo_owner != env);
1059 down_write_nested(&obj->oo_sem, role);
1061 LASSERT(obj->oo_owner == NULL);
1062 obj->oo_owner = env;
1066 static void osd_object_read_unlock(const struct lu_env *env,
1067 struct dt_object *dt)
1069 struct osd_object *obj = osd_dt_obj(dt);
1070 struct osd_thread_info *oti = osd_oti_get(env);
1072 LINVRNT(osd_invariant(obj));
1074 LASSERT(oti->oti_r_locks > 0);
1076 up_read(&obj->oo_sem);
1079 static void osd_object_write_unlock(const struct lu_env *env,
1080 struct dt_object *dt)
1082 struct osd_object *obj = osd_dt_obj(dt);
1083 struct osd_thread_info *oti = osd_oti_get(env);
1085 LINVRNT(osd_invariant(obj));
1087 LASSERT(obj->oo_owner == env);
1088 LASSERT(oti->oti_w_locks > 0);
1090 obj->oo_owner = NULL;
1091 up_write(&obj->oo_sem);
1094 static int capa_is_sane(const struct lu_env *env,
1095 struct osd_device *dev,
1096 struct lustre_capa *capa,
1097 struct lustre_capa_key *keys)
1099 struct osd_thread_info *oti = osd_oti_get(env);
1100 struct lustre_capa *tcapa = &oti->oti_capa;
1101 struct obd_capa *oc;
1105 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1107 if (capa_is_expired(oc)) {
1108 DEBUG_CAPA(D_ERROR, capa, "expired");
1115 if (capa_is_expired_sec(capa)) {
1116 DEBUG_CAPA(D_ERROR, capa, "expired");
1120 spin_lock(&capa_lock);
1121 for (i = 0; i < 2; i++) {
1122 if (keys[i].lk_keyid == capa->lc_keyid) {
1123 oti->oti_capa_key = keys[i];
1127 spin_unlock(&capa_lock);
1130 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1134 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1138 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1139 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1143 oc = capa_add(dev->od_capa_hash, capa);
1149 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1150 struct lustre_capa *capa, __u64 opc)
1152 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1153 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1154 struct md_capainfo *ci;
1157 if (!dev->od_fl_capa)
1160 if (capa == BYPASS_CAPA)
1163 ci = md_capainfo(env);
1167 if (ci->mc_auth == LC_ID_NONE)
1171 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1175 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1176 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1181 if (!capa_opc_supported(capa, opc)) {
1182 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1186 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1187 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1194 static int osd_attr_get(const struct lu_env *env,
1195 struct dt_object *dt,
1196 struct lu_attr *attr,
1197 struct lustre_capa *capa)
1199 struct osd_object *obj = osd_dt_obj(dt);
1201 LASSERT(dt_object_exists(dt));
1202 LINVRNT(osd_invariant(obj));
1204 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1207 spin_lock(&obj->oo_guard);
1208 osd_inode_getattr(env, obj->oo_inode, attr);
1209 spin_unlock(&obj->oo_guard);
1213 static int osd_attr_set(const struct lu_env *env,
1214 struct dt_object *dt,
1215 const struct lu_attr *attr,
1216 struct thandle *handle,
1217 struct lustre_capa *capa)
1219 struct osd_object *obj = osd_dt_obj(dt);
1222 LASSERT(handle != NULL);
1223 LASSERT(dt_object_exists(dt));
1224 LASSERT(osd_invariant(obj));
1226 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1229 spin_lock(&obj->oo_guard);
1230 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1231 spin_unlock(&obj->oo_guard);
1234 mark_inode_dirty(obj->oo_inode);
1238 static struct timespec *osd_inode_time(const struct lu_env *env,
1239 struct inode *inode, __u64 seconds)
1241 struct osd_thread_info *oti = osd_oti_get(env);
1242 struct timespec *t = &oti->oti_time;
1244 t->tv_sec = seconds;
1246 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1250 static int osd_inode_setattr(const struct lu_env *env,
1251 struct inode *inode, const struct lu_attr *attr)
1255 bits = attr->la_valid;
1257 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1259 #ifdef HAVE_QUOTA_SUPPORT
1260 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1261 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1262 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1266 iattr.ia_valid = bits & (LA_UID | LA_GID);
1267 iattr.ia_uid = attr->la_uid;
1268 iattr.ia_gid = attr->la_gid;
1269 osd_push_ctxt(env, save);
1270 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1277 if (bits & LA_ATIME)
1278 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1279 if (bits & LA_CTIME)
1280 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1281 if (bits & LA_MTIME)
1282 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1283 if (bits & LA_SIZE) {
1284 LDISKFS_I(inode)->i_disksize = attr->la_size;
1285 i_size_write(inode, attr->la_size);
1289 * OSD should not change "i_blocks" which is used by quota.
1290 * "i_blocks" should be changed by ldiskfs only.
1291 * Disable this assignment until SOM to fix some EA field. */
1292 if (bits & LA_BLOCKS)
1293 inode->i_blocks = attr->la_blocks;
1296 inode->i_mode = (inode->i_mode & S_IFMT) |
1297 (attr->la_mode & ~S_IFMT);
1299 inode->i_uid = attr->la_uid;
1301 inode->i_gid = attr->la_gid;
1302 if (bits & LA_NLINK)
1303 inode->i_nlink = attr->la_nlink;
1305 inode->i_rdev = attr->la_rdev;
1307 if (bits & LA_FLAGS) {
1308 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1310 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1311 (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1319 * XXX temporary solution.
1322 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1323 struct lu_attr *attr, struct thandle *th)
1328 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1329 struct lu_attr *attr, struct thandle *th)
1331 LASSERT(obj->oo_inode != NULL);
1333 osd_object_init0(obj);
1337 extern struct inode *ldiskfs_create_inode(handle_t *handle,
1338 struct inode * dir, int mode);
1339 extern int ldiskfs_add_entry(handle_t *handle, struct dentry *dentry,
1340 struct inode *inode);
1341 extern int ldiskfs_delete_entry(handle_t *handle,
1343 struct ldiskfs_dir_entry_2 * de_del,
1344 struct buffer_head * bh);
1345 extern struct buffer_head * ldiskfs_find_entry(struct dentry *dentry,
1346 struct ldiskfs_dir_entry_2
1348 extern int ldiskfs_add_dot_dotdot(handle_t *handle, struct inode *dir,
1349 struct inode *inode);
1351 extern int ldiskfs_xattr_set_handle(handle_t *handle, struct inode *inode,
1352 int name_index, const char *name,
1353 const void *value, size_t value_len,
1356 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1357 struct osd_object *obj,
1361 struct osd_thread_info *info = osd_oti_get(env);
1362 struct dentry *child_dentry = &info->oti_child_dentry;
1363 struct dentry *obj_dentry = &info->oti_obj_dentry;
1365 obj_dentry->d_inode = obj->oo_inode;
1366 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1367 obj_dentry->d_name.hash = 0;
1369 child_dentry->d_name.hash = 0;
1370 child_dentry->d_parent = obj_dentry;
1371 child_dentry->d_name.name = name;
1372 child_dentry->d_name.len = namelen;
1373 return child_dentry;
1377 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1379 struct dt_allocation_hint *hint,
1383 struct osd_device *osd = osd_obj2dev(obj);
1384 struct osd_thandle *oth;
1385 struct dt_object *parent;
1386 struct inode *inode;
1387 #ifdef HAVE_QUOTA_SUPPORT
1388 struct osd_ctxt *save = &info->oti_ctxt;
1391 LINVRNT(osd_invariant(obj));
1392 LASSERT(obj->oo_inode == NULL);
1394 oth = container_of(th, struct osd_thandle, ot_super);
1395 LASSERT(oth->ot_handle->h_transaction != NULL);
1397 if (hint && hint->dah_parent)
1398 parent = hint->dah_parent;
1400 parent = osd->od_obj_area;
1402 LASSERT(parent != NULL);
1403 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1405 #ifdef HAVE_QUOTA_SUPPORT
1406 osd_push_ctxt(info->oti_env, save);
1408 inode = ldiskfs_create_inode(oth->ot_handle,
1409 osd_dt_obj(parent)->oo_inode, mode);
1410 #ifdef HAVE_QUOTA_SUPPORT
1413 if (!IS_ERR(inode)) {
1414 obj->oo_inode = inode;
1417 result = PTR_ERR(inode);
1418 LINVRNT(osd_invariant(obj));
1423 extern int iam_lvar_create(struct inode *obj, int keysize, int ptrsize,
1424 int recsize, handle_t *handle);
1426 extern int iam_lfix_create(struct inode *obj, int keysize, int ptrsize,
1427 int recsize, handle_t *handle);
1434 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1435 struct lu_attr *attr,
1436 struct dt_allocation_hint *hint,
1437 struct dt_object_format *dof,
1441 struct osd_thandle *oth;
1442 struct osd_device *osd = osd_obj2dev(obj);
1443 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1445 LASSERT(S_ISDIR(attr->la_mode));
1447 oth = container_of(th, struct osd_thandle, ot_super);
1448 LASSERT(oth->ot_handle->h_transaction != NULL);
1449 result = osd_mkfile(info, obj, mode, hint, th);
1450 if (result == 0 && osd->od_iop_mode == 0) {
1451 LASSERT(obj->oo_inode != NULL);
1453 * XXX uh-oh... call low-level iam function directly.
1456 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1457 sizeof (struct lu_fid_pack),
1463 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1464 struct lu_attr *attr,
1465 struct dt_allocation_hint *hint,
1466 struct dt_object_format *dof,
1470 struct osd_thandle *oth;
1471 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1473 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1475 LASSERT(S_ISREG(attr->la_mode));
1477 oth = container_of(th, struct osd_thandle, ot_super);
1478 LASSERT(oth->ot_handle->h_transaction != NULL);
1480 result = osd_mkfile(info, obj, mode, hint, th);
1482 LASSERT(obj->oo_inode != NULL);
1483 if (feat->dif_flags & DT_IND_VARKEY)
1484 result = iam_lvar_create(obj->oo_inode,
1485 feat->dif_keysize_max,
1487 feat->dif_recsize_max,
1490 result = iam_lfix_create(obj->oo_inode,
1491 feat->dif_keysize_max,
1493 feat->dif_recsize_max,
1500 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1501 struct lu_attr *attr,
1502 struct dt_allocation_hint *hint,
1503 struct dt_object_format *dof,
1506 LASSERT(S_ISREG(attr->la_mode));
1507 return osd_mkfile(info, obj, (attr->la_mode &
1508 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1511 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1512 struct lu_attr *attr,
1513 struct dt_allocation_hint *hint,
1514 struct dt_object_format *dof,
1517 LASSERT(S_ISLNK(attr->la_mode));
1518 return osd_mkfile(info, obj, (attr->la_mode &
1519 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1522 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1523 struct lu_attr *attr,
1524 struct dt_allocation_hint *hint,
1525 struct dt_object_format *dof,
1528 umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1531 LINVRNT(osd_invariant(obj));
1532 LASSERT(obj->oo_inode == NULL);
1533 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1534 S_ISFIFO(mode) || S_ISSOCK(mode));
1536 result = osd_mkfile(info, obj, mode, hint, th);
1538 LASSERT(obj->oo_inode != NULL);
1539 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1541 LINVRNT(osd_invariant(obj));
1545 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1547 struct dt_allocation_hint *hint,
1548 struct dt_object_format *dof,
1551 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1553 osd_obj_type_f result;
1569 result = osd_mk_index;
1580 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1581 struct dt_object *parent, umode_t child_mode)
1585 memset(ah, 0, sizeof(*ah));
1586 ah->dah_parent = parent;
1587 ah->dah_mode = child_mode;
1591 * Helper function for osd_object_create()
1593 * \retval 0, on success
1595 static int __osd_object_create(struct osd_thread_info *info,
1596 struct osd_object *obj, struct lu_attr *attr,
1597 struct dt_allocation_hint *hint,
1598 struct dt_object_format *dof,
1604 result = osd_create_pre(info, obj, attr, th);
1606 result = osd_create_type_f(dof->dof_type)(info, obj,
1607 attr, hint, dof, th);
1609 result = osd_create_post(info, obj, attr, th);
1615 * Helper function for osd_object_create()
1617 * \retval 0, on success
1619 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1620 const struct lu_fid *fid, struct thandle *th)
1622 struct osd_thread_info *info = osd_oti_get(env);
1623 struct osd_inode_id *id = &info->oti_id;
1624 struct osd_device *osd = osd_obj2dev(obj);
1625 struct md_ucred *uc = md_ucred(env);
1627 LASSERT(obj->oo_inode != NULL);
1628 LASSERT(uc != NULL);
1630 id->oii_ino = obj->oo_inode->i_ino;
1631 id->oii_gen = obj->oo_inode->i_generation;
1633 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1634 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1637 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1638 struct lu_attr *attr,
1639 struct dt_allocation_hint *hint,
1640 struct dt_object_format *dof,
1643 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1644 struct osd_object *obj = osd_dt_obj(dt);
1645 struct osd_thread_info *info = osd_oti_get(env);
1650 LINVRNT(osd_invariant(obj));
1651 LASSERT(!dt_object_exists(dt));
1652 LASSERT(osd_write_locked(env, obj));
1653 LASSERT(th != NULL);
1655 result = __osd_object_create(info, obj, attr, hint, dof, th);
1657 result = __osd_oi_insert(env, obj, fid, th);
1659 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1660 LASSERT(osd_invariant(obj));
1665 * Helper function for osd_xattr_set()
1667 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1668 const struct lu_buf *buf, const char *name, int fl)
1670 struct osd_object *obj = osd_dt_obj(dt);
1671 struct inode *inode = obj->oo_inode;
1672 struct osd_thread_info *info = osd_oti_get(env);
1673 struct dentry *dentry = &info->oti_child_dentry;
1674 struct timespec *t = &info->oti_time;
1678 LASSERT(dt_object_exists(dt));
1679 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1680 LASSERT(osd_write_locked(env, obj));
1682 if (fl & LU_XATTR_REPLACE)
1683 fs_flags |= XATTR_REPLACE;
1685 if (fl & LU_XATTR_CREATE)
1686 fs_flags |= XATTR_CREATE;
1688 dentry->d_inode = inode;
1689 *t = inode->i_ctime;
1690 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1691 buf->lb_len, fs_flags);
1692 if (likely(rc == 0)) {
1693 spin_lock(&obj->oo_guard);
1694 inode->i_ctime = *t;
1695 spin_unlock(&obj->oo_guard);
1696 mark_inode_dirty(inode);
1702 * Put the fid into lustre_mdt_attrs, and then place the structure
1703 * inode's ea. This fid should not be altered during the life time
1706 * \retval +ve, on success
1707 * \retval -ve, on error
1709 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1711 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1712 const struct lu_fid *fid)
1714 struct osd_thread_info *info = osd_oti_get(env);
1715 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1717 fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1719 return __osd_xattr_set(env, dt,
1720 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1721 MDT_XATTR_NAME, LU_XATTR_CREATE);
1726 * Helper function to form igif
1728 static inline void osd_igif_get(const struct lu_env *env, struct dentry *dentry,
1731 struct inode *inode = dentry->d_inode;
1732 lu_igif_build(fid, inode->i_ino, inode->i_generation);
1736 * Helper function to pack the fid
1738 static inline void osd_fid_pack(const struct lu_env *env, const struct lu_fid *fid,
1739 struct lu_fid_pack *pack)
1741 fid_pack(pack, fid, &osd_oti_get(env)->oti_fid);
1745 * Try to read the fid from inode ea into dt_rec, if return value
1746 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1748 * \param rec, the data-structure into which fid/igif is read
1750 * \retval 0, on success
1752 static int osd_ea_fid_get(const struct lu_env *env, struct dentry *dentry,
1755 struct inode *inode = dentry->d_inode;
1756 struct osd_thread_info *info = osd_oti_get(env);
1757 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1758 struct lu_fid *fid = &info->oti_fid;
1761 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1763 rc = inode->i_op->getxattr(dentry, MDT_XATTR_NAME, (void *)mdt_attrs,
1767 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1769 } else if (rc == -ENODATA) {
1770 osd_igif_get(env, dentry, fid);
1775 osd_fid_pack(env, fid, (struct lu_fid_pack*)rec);
1781 * OSD layer object create function for interoperability mode (b11826).
1782 * This is mostly similar to osd_object_create(). Only difference being, fid is
1783 * inserted into inode ea here.
1785 * \retval 0, on success
1786 * \retval -ve, on error
1788 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1789 struct lu_attr *attr,
1790 struct dt_allocation_hint *hint,
1791 struct dt_object_format *dof,
1794 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1795 struct osd_object *obj = osd_dt_obj(dt);
1796 struct osd_thread_info *info = osd_oti_get(env);
1802 LASSERT(osd_invariant(obj));
1803 LASSERT(!dt_object_exists(dt));
1804 LASSERT(osd_write_locked(env, obj));
1805 LASSERT(th != NULL);
1807 result = __osd_object_create(info, obj, attr, hint, dof, th);
1809 if (hint && hint->dah_parent)
1810 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1812 /* objects under osd root shld have igif fid, so dont add fid EA */
1813 if (result == 0 && is_root == 0)
1814 result = osd_ea_fid_set(env, dt, fid);
1817 result = __osd_oi_insert(env, obj, fid, th);
1819 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1820 LINVRNT(osd_invariant(obj));
1825 * Concurrency: @dt is write locked.
1827 static void osd_object_ref_add(const struct lu_env *env,
1828 struct dt_object *dt,
1831 struct osd_object *obj = osd_dt_obj(dt);
1832 struct inode *inode = obj->oo_inode;
1834 LINVRNT(osd_invariant(obj));
1835 LASSERT(dt_object_exists(dt));
1836 LASSERT(osd_write_locked(env, obj));
1837 LASSERT(th != NULL);
1839 spin_lock(&obj->oo_guard);
1840 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1842 spin_unlock(&obj->oo_guard);
1843 mark_inode_dirty(inode);
1844 LINVRNT(osd_invariant(obj));
1848 * Concurrency: @dt is write locked.
1850 static void osd_object_ref_del(const struct lu_env *env,
1851 struct dt_object *dt,
1854 struct osd_object *obj = osd_dt_obj(dt);
1855 struct inode *inode = obj->oo_inode;
1857 LINVRNT(osd_invariant(obj));
1858 LASSERT(dt_object_exists(dt));
1859 LASSERT(osd_write_locked(env, obj));
1860 LASSERT(th != NULL);
1862 spin_lock(&obj->oo_guard);
1863 LASSERT(inode->i_nlink > 0);
1865 spin_unlock(&obj->oo_guard);
1866 mark_inode_dirty(inode);
1867 LINVRNT(osd_invariant(obj));
1871 * Concurrency: @dt is read locked.
1873 static int osd_xattr_get(const struct lu_env *env,
1874 struct dt_object *dt,
1877 struct lustre_capa *capa)
1879 struct osd_object *obj = osd_dt_obj(dt);
1880 struct inode *inode = obj->oo_inode;
1881 struct osd_thread_info *info = osd_oti_get(env);
1882 struct dentry *dentry = &info->oti_obj_dentry;
1884 LASSERT(dt_object_exists(dt));
1885 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1886 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1888 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1891 dentry->d_inode = inode;
1892 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1897 * Concurrency: @dt is write locked.
1899 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1900 const struct lu_buf *buf, const char *name, int fl,
1901 struct thandle *handle, struct lustre_capa *capa)
1903 LASSERT(handle != NULL);
1905 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1908 return __osd_xattr_set(env, dt, buf, name, fl);
1912 * Concurrency: @dt is read locked.
1914 static int osd_xattr_list(const struct lu_env *env,
1915 struct dt_object *dt,
1917 struct lustre_capa *capa)
1919 struct osd_object *obj = osd_dt_obj(dt);
1920 struct inode *inode = obj->oo_inode;
1921 struct osd_thread_info *info = osd_oti_get(env);
1922 struct dentry *dentry = &info->oti_obj_dentry;
1924 LASSERT(dt_object_exists(dt));
1925 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1926 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1928 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1931 dentry->d_inode = inode;
1932 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1936 * Concurrency: @dt is write locked.
1938 static int osd_xattr_del(const struct lu_env *env,
1939 struct dt_object *dt,
1941 struct thandle *handle,
1942 struct lustre_capa *capa)
1944 struct osd_object *obj = osd_dt_obj(dt);
1945 struct inode *inode = obj->oo_inode;
1946 struct osd_thread_info *info = osd_oti_get(env);
1947 struct dentry *dentry = &info->oti_obj_dentry;
1948 struct timespec *t = &info->oti_time;
1951 LASSERT(dt_object_exists(dt));
1952 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1953 LASSERT(osd_write_locked(env, obj));
1954 LASSERT(handle != NULL);
1956 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1959 dentry->d_inode = inode;
1960 *t = inode->i_ctime;
1961 rc = inode->i_op->removexattr(dentry, name);
1962 if (likely(rc == 0)) {
1963 /* ctime should not be updated with server-side time. */
1964 spin_lock(&obj->oo_guard);
1965 inode->i_ctime = *t;
1966 spin_unlock(&obj->oo_guard);
1967 mark_inode_dirty(inode);
1972 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1973 struct dt_object *dt,
1974 struct lustre_capa *old,
1977 struct osd_thread_info *info = osd_oti_get(env);
1978 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1979 struct osd_object *obj = osd_dt_obj(dt);
1980 struct osd_device *dev = osd_obj2dev(obj);
1981 struct lustre_capa_key *key = &info->oti_capa_key;
1982 struct lustre_capa *capa = &info->oti_capa;
1983 struct obd_capa *oc;
1984 struct md_capainfo *ci;
1988 if (!dev->od_fl_capa)
1989 RETURN(ERR_PTR(-ENOENT));
1991 LASSERT(dt_object_exists(dt));
1992 LINVRNT(osd_invariant(obj));
1994 /* renewal sanity check */
1995 if (old && osd_object_auth(env, dt, old, opc))
1996 RETURN(ERR_PTR(-EACCES));
1998 ci = md_capainfo(env);
2000 RETURN(ERR_PTR(-ENOENT));
2002 switch (ci->mc_auth) {
2006 capa->lc_uid = obj->oo_inode->i_uid;
2007 capa->lc_gid = obj->oo_inode->i_gid;
2008 capa->lc_flags = LC_ID_PLAIN;
2010 case LC_ID_CONVERT: {
2013 s[0] = obj->oo_inode->i_uid;
2014 get_random_bytes(&(s[1]), sizeof(__u32));
2015 s[2] = obj->oo_inode->i_gid;
2016 get_random_bytes(&(s[3]), sizeof(__u32));
2017 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2019 RETURN(ERR_PTR(rc));
2021 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2022 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2023 capa->lc_flags = LC_ID_CONVERT;
2027 RETURN(ERR_PTR(-EINVAL));
2030 capa->lc_fid = *fid;
2032 capa->lc_flags |= dev->od_capa_alg << 24;
2033 capa->lc_timeout = dev->od_capa_timeout;
2034 capa->lc_expiry = 0;
2036 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2038 LASSERT(!capa_is_expired(oc));
2042 spin_lock(&capa_lock);
2043 *key = dev->od_capa_keys[1];
2044 spin_unlock(&capa_lock);
2046 capa->lc_keyid = key->lk_keyid;
2047 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2049 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2051 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2052 RETURN(ERR_PTR(rc));
2055 oc = capa_add(dev->od_capa_hash, capa);
2059 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2062 struct osd_object *obj = osd_dt_obj(dt);
2063 struct inode *inode = obj->oo_inode;
2064 struct osd_thread_info *info = osd_oti_get(env);
2065 struct dentry *dentry = &info->oti_obj_dentry;
2066 struct file *file = &info->oti_file;
2069 dentry->d_inode = inode;
2070 file->f_dentry = dentry;
2071 file->f_mapping = inode->i_mapping;
2072 file->f_op = inode->i_fop;
2073 LOCK_INODE_MUTEX(inode);
2074 rc = file->f_op->fsync(file, dentry, 0);
2075 UNLOCK_INODE_MUTEX(inode);
2079 static const struct dt_object_operations osd_obj_ops = {
2080 .do_read_lock = osd_object_read_lock,
2081 .do_write_lock = osd_object_write_lock,
2082 .do_read_unlock = osd_object_read_unlock,
2083 .do_write_unlock = osd_object_write_unlock,
2084 .do_attr_get = osd_attr_get,
2085 .do_attr_set = osd_attr_set,
2086 .do_ah_init = osd_ah_init,
2087 .do_create = osd_object_create,
2088 .do_index_try = osd_index_try,
2089 .do_ref_add = osd_object_ref_add,
2090 .do_ref_del = osd_object_ref_del,
2091 .do_xattr_get = osd_xattr_get,
2092 .do_xattr_set = osd_xattr_set,
2093 .do_xattr_del = osd_xattr_del,
2094 .do_xattr_list = osd_xattr_list,
2095 .do_capa_get = osd_capa_get,
2096 .do_object_sync = osd_object_sync,
2100 * dt_object_operations for interoperability mode
2101 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2103 static const struct dt_object_operations osd_obj_ea_ops = {
2104 .do_read_lock = osd_object_read_lock,
2105 .do_write_lock = osd_object_write_lock,
2106 .do_read_unlock = osd_object_read_unlock,
2107 .do_write_unlock = osd_object_write_unlock,
2108 .do_attr_get = osd_attr_get,
2109 .do_attr_set = osd_attr_set,
2110 .do_ah_init = osd_ah_init,
2111 .do_create = osd_object_ea_create,
2112 .do_index_try = osd_index_try,
2113 .do_ref_add = osd_object_ref_add,
2114 .do_ref_del = osd_object_ref_del,
2115 .do_xattr_get = osd_xattr_get,
2116 .do_xattr_set = osd_xattr_set,
2117 .do_xattr_del = osd_xattr_del,
2118 .do_xattr_list = osd_xattr_list,
2119 .do_capa_get = osd_capa_get,
2120 .do_object_sync = osd_object_sync,
2128 * XXX: Another layering violation for now.
2130 * We don't want to use ->f_op->read methods, because generic file write
2132 * - serializes on ->i_sem, and
2134 * - does a lot of extra work like balance_dirty_pages(),
2136 * which doesn't work for globally shared files like /last-received.
2138 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2139 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2140 loff_t *offs, handle_t *handle);
2142 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2143 struct lu_buf *buf, loff_t *pos,
2144 struct lustre_capa *capa)
2146 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2148 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2151 return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2154 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2155 const struct lu_buf *buf, loff_t *pos,
2156 struct thandle *handle, struct lustre_capa *capa,
2159 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2160 struct osd_thandle *oh;
2162 #ifdef HAVE_QUOTA_SUPPORT
2163 cfs_cap_t save = current->cap_effective;
2166 LASSERT(handle != NULL);
2168 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2171 oh = container_of(handle, struct osd_thandle, ot_super);
2172 LASSERT(oh->ot_handle->h_transaction != NULL);
2173 #ifdef HAVE_QUOTA_SUPPORT
2175 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2177 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2179 result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2180 pos, oh->ot_handle);
2181 #ifdef HAVE_QUOTA_SUPPORT
2182 current->cap_effective = save;
2185 result = buf->lb_len;
2189 static const struct dt_body_operations osd_body_ops = {
2190 .dbo_read = osd_read,
2191 .dbo_write = osd_write
2198 static int osd_object_is_root(const struct osd_object *obj)
2200 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
2203 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2204 const struct dt_index_features *feat)
2206 struct iam_descr *descr;
2207 struct dt_object *dt = &o->oo_dt;
2209 if (osd_object_is_root(o))
2210 return feat == &dt_directory_features;
2212 LASSERT(o->oo_dir != NULL);
2214 descr = o->oo_dir->od_container.ic_descr;
2215 if (feat == &dt_directory_features) {
2216 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2219 if (descr == &iam_htree_compat_param) {
2220 /* if it is a HTREE dir then there is good chance that,
2221 * we dealing with ext3 directory here with no FIDs. */
2223 if (descr->id_rec_size ==
2224 sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2226 dt->do_index_ops = &osd_index_ea_ops;
2233 feat->dif_keysize_min <= descr->id_key_size &&
2234 descr->id_key_size <= feat->dif_keysize_max &&
2235 feat->dif_recsize_min <= descr->id_rec_size &&
2236 descr->id_rec_size <= feat->dif_recsize_max &&
2237 !(feat->dif_flags & (DT_IND_VARKEY |
2238 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2239 ergo(feat->dif_flags & DT_IND_UPDATE,
2240 1 /* XXX check that object (and file system) is
2245 static int osd_iam_container_init(const struct lu_env *env,
2246 struct osd_object *obj,
2247 struct osd_directory *dir)
2250 struct iam_container *bag;
2252 bag = &dir->od_container;
2253 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2255 result = iam_container_setup(bag);
2257 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2259 iam_container_fini(bag);
2265 * Concurrency: no external locking is necessary.
2267 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2268 const struct dt_index_features *feat)
2272 struct osd_object *obj = osd_dt_obj(dt);
2273 struct osd_device *osd = osd_obj2dev(obj);
2275 LINVRNT(osd_invariant(obj));
2276 LASSERT(dt_object_exists(dt));
2278 if (osd_object_is_root(obj)) {
2279 dt->do_index_ops = &osd_index_ea_ops;
2281 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2282 dt->do_index_ops = &osd_index_ea_ops;
2283 if (S_ISDIR(obj->oo_inode->i_mode))
2288 } else if (!osd_has_index(obj)) {
2289 struct osd_directory *dir;
2293 sema_init(&dir->od_sem, 1);
2295 spin_lock(&obj->oo_guard);
2296 if (obj->oo_dir == NULL)
2300 * Concurrent thread allocated container data.
2303 spin_unlock(&obj->oo_guard);
2305 * Now, that we have container data, serialize its
2308 down(&obj->oo_dir->od_sem);
2310 * recheck under lock.
2312 if (!osd_has_index(obj))
2313 result = osd_iam_container_init(env, obj, dir);
2316 up(&obj->oo_dir->od_sem);
2322 if (result == 0 && ea_dir == 0) {
2323 if (!osd_iam_index_probe(env, obj, feat))
2326 LINVRNT(osd_invariant(obj));
2332 * delete a (key, value) pair from index \a dt specified by \a key
2334 * \param dt_object osd index object
2335 * \param key key for index
2336 * \param rec record reference
2337 * \param handle transaction handler
2340 * \retval -ve failure
2343 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2344 const struct dt_key *key, struct thandle *handle,
2345 struct lustre_capa *capa)
2347 struct osd_object *obj = osd_dt_obj(dt);
2348 struct osd_thandle *oh;
2349 struct iam_path_descr *ipd;
2350 struct iam_container *bag = &obj->oo_dir->od_container;
2355 LINVRNT(osd_invariant(obj));
2356 LASSERT(dt_object_exists(dt));
2357 LASSERT(bag->ic_object == obj->oo_inode);
2358 LASSERT(handle != NULL);
2360 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2363 ipd = osd_idx_ipd_get(env, bag);
2364 if (unlikely(ipd == NULL))
2367 oh = container_of0(handle, struct osd_thandle, ot_super);
2368 LASSERT(oh->ot_handle != NULL);
2369 LASSERT(oh->ot_handle->h_transaction != NULL);
2371 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2372 osd_ipd_put(env, bag, ipd);
2373 LINVRNT(osd_invariant(obj));
2378 * Index delete function for interoperability mode (b11826).
2379 * It will remove the directory entry added by osd_index_ea_insert().
2380 * This entry is needed to maintain name->fid mapping.
2382 * \param key, key i.e. file entry to be deleted
2384 * \retval 0, on success
2385 * \retval -ve, on error
2387 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2388 const struct dt_key *key, struct thandle *handle,
2389 struct lustre_capa *capa)
2391 struct osd_object *obj = osd_dt_obj(dt);
2392 struct inode *dir = obj->oo_inode;
2393 struct dentry *dentry;
2394 struct osd_thandle *oh;
2395 struct ldiskfs_dir_entry_2 *de;
2396 struct buffer_head *bh;
2402 LINVRNT(osd_invariant(obj));
2403 LASSERT(dt_object_exists(dt));
2404 LASSERT(handle != NULL);
2406 oh = container_of(handle, struct osd_thandle, ot_super);
2407 LASSERT(oh->ot_handle != NULL);
2408 LASSERT(oh->ot_handle->h_transaction != NULL);
2410 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2413 dentry = osd_child_dentry_get(env, obj,
2414 (char *)key, strlen((char *)key));
2415 bh = ldiskfs_find_entry(dentry, &de);
2417 rc = ldiskfs_delete_entry(oh->ot_handle,
2420 mark_inode_dirty(dir);
2425 LASSERT(osd_invariant(obj));
2430 * Lookup index for \a key and copy record to \a rec.
2432 * \param dt_object osd index object
2433 * \param key key for index
2434 * \param rec record reference
2436 * \retval +ve success : exact mach
2437 * \retval 0 return record with key not greater than \a key
2438 * \retval -ve failure
2440 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2441 struct dt_rec *rec, const struct dt_key *key,
2442 struct lustre_capa *capa)
2444 struct osd_object *obj = osd_dt_obj(dt);
2445 struct iam_path_descr *ipd;
2446 struct iam_container *bag = &obj->oo_dir->od_container;
2447 struct osd_thread_info *oti = osd_oti_get(env);
2448 struct iam_iterator *it = &oti->oti_idx_it;
2452 LASSERT(osd_invariant(obj));
2453 LASSERT(dt_object_exists(dt));
2454 LASSERT(bag->ic_object == obj->oo_inode);
2456 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2459 ipd = osd_idx_ipd_get(env, bag);
2463 /* got ipd now we can start iterator. */
2464 iam_it_init(it, bag, 0, ipd);
2466 rc = iam_it_get(it, (struct iam_key *)key);
2468 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2472 osd_ipd_put(env, bag, ipd);
2474 LINVRNT(osd_invariant(obj));
2480 * Inserts (key, value) pair in \a dt index object.
2482 * \param dt osd index object
2483 * \param key key for index
2484 * \param rec record reference
2485 * \param th transaction handler
2488 * \retval -ve failure
2490 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2491 const struct dt_rec *rec, const struct dt_key *key,
2492 struct thandle *th, struct lustre_capa *capa,
2495 struct osd_object *obj = osd_dt_obj(dt);
2496 struct iam_path_descr *ipd;
2497 struct osd_thandle *oh;
2498 struct iam_container *bag = &obj->oo_dir->od_container;
2499 #ifdef HAVE_QUOTA_SUPPORT
2500 cfs_cap_t save = current->cap_effective;
2506 LINVRNT(osd_invariant(obj));
2507 LASSERT(dt_object_exists(dt));
2508 LASSERT(bag->ic_object == obj->oo_inode);
2509 LASSERT(th != NULL);
2511 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2514 ipd = osd_idx_ipd_get(env, bag);
2515 if (unlikely(ipd == NULL))
2518 oh = container_of0(th, struct osd_thandle, ot_super);
2519 LASSERT(oh->ot_handle != NULL);
2520 LASSERT(oh->ot_handle->h_transaction != NULL);
2521 #ifdef HAVE_QUOTA_SUPPORT
2523 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2525 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2527 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2528 (struct iam_rec *)rec, ipd);
2529 #ifdef HAVE_QUOTA_SUPPORT
2530 current->cap_effective = save;
2532 osd_ipd_put(env, bag, ipd);
2533 LINVRNT(osd_invariant(obj));
2538 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2539 * into the directory.Also sets flags into osd object to
2540 * indicate dot and dotdot are created. This is required for
2541 * interoperability mode (b11826)
2543 * \param dir directory for dot and dotdot fixup.
2544 * \param obj child object for linking
2546 * \retval 0, on success
2547 * \retval -ve, on error
2549 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2550 struct osd_object *dir,
2551 struct osd_object *obj, const char *name,
2554 struct inode *parent_dir = obj->oo_inode;
2555 struct inode *inode = dir->oo_inode;
2556 struct osd_thandle *oth;
2559 oth = container_of(th, struct osd_thandle, ot_super);
2560 LASSERT(oth->ot_handle->h_transaction != NULL);
2561 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2563 if (strcmp(name, dot) == 0) {
2564 if (dir->oo_compat_dot_created) {
2567 LASSERT(obj == dir);
2568 dir->oo_compat_dot_created = 1;
2571 } else if(strcmp(name, dotdot) == 0) {
2572 if (!dir->oo_compat_dot_created)
2574 if (dir->oo_compat_dotdot_created)
2575 return __osd_ea_add_rec(info, dir, obj, name, th);
2577 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2579 dir->oo_compat_dotdot_created = 1;
2586 * Calls ldiskfs_add_entry() to add directory entry
2587 * into the directory. This is required for
2588 * interoperability mode (b11826)
2590 * \retval 0, on success
2591 * \retval -ve, on error
2593 static int __osd_ea_add_rec(struct osd_thread_info *info,
2594 struct osd_object *pobj,
2595 struct osd_object *cobj,
2599 struct dentry *child;
2600 struct osd_thandle *oth;
2601 struct inode *cinode = cobj->oo_inode;
2604 oth = container_of(th, struct osd_thandle, ot_super);
2605 LASSERT(oth->ot_handle != NULL);
2606 LASSERT(oth->ot_handle->h_transaction != NULL);
2608 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2609 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2615 * It will call the appropriate osd_add* function and return the
2616 * value, return by respective functions.
2618 static int osd_ea_add_rec(const struct lu_env *env,
2619 struct osd_object *pobj,
2620 struct osd_object *cobj,
2624 struct osd_thread_info *info = osd_oti_get(env);
2627 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2629 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2631 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2637 * Calls ->lookup() to find dentry. From dentry get inode and
2638 * read inode's ea to get fid. This is required for interoperability
2641 * \retval 0, on success
2642 * \retval -ve, on error
2644 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2645 struct dt_rec *rec, const struct dt_key *key)
2647 struct inode *dir = obj->oo_inode;
2648 struct osd_thread_info *info = osd_oti_get(env);
2649 struct dentry *dentry;
2650 struct osd_device *dev = osd_dev(obj->oo_dt.do_lu.lo_dev);
2651 struct osd_inode_id *id = &info->oti_id;
2652 struct ldiskfs_dir_entry_2 *de;
2653 struct buffer_head *bh;
2654 struct inode *inode;
2658 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2660 dentry = osd_child_dentry_get(env, obj,
2661 (char *)key, strlen((char *)key));
2662 bh = ldiskfs_find_entry(dentry, &de);
2664 ino = le32_to_cpu(de->inode);
2667 id->oii_gen = OSD_OII_NOGEN;
2669 inode = osd_iget(info, dev, id);
2670 if (!IS_ERR(inode)) {
2671 dentry->d_inode = inode;
2673 rc = osd_ea_fid_get(env, dentry, rec);
2684 * Find the osd object for given fid.
2686 * \param fid, need to find the osd object having this fid
2688 * \retval osd_object, on success
2689 * \retval -ve, on error
2691 struct osd_object *osd_object_find(const struct lu_env *env,
2692 struct dt_object *dt,
2693 const struct lu_fid *fid)
2695 struct lu_device *ludev = dt->do_lu.lo_dev;
2696 struct osd_object *child = NULL;
2697 struct lu_object *luch;
2698 struct lu_object *lo;
2700 luch = lu_object_find(env, ludev, fid, NULL);
2701 if (!IS_ERR(luch)) {
2702 if (lu_object_exists(luch)) {
2703 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2705 child = osd_obj(lo);
2707 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2708 "lu_object can't be located"
2709 ""DFID"\n", PFID(fid));
2711 if (child == NULL) {
2712 lu_object_put(env, luch);
2713 CERROR("Unable to get osd_object\n");
2714 child = ERR_PTR(-ENOENT);
2717 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2718 "lu_object does not exists "DFID"\n",
2720 child = ERR_PTR(-ENOENT);
2723 child = (void *)luch;
2729 * Put the osd object once done with it.
2731 * \param obj, osd object that needs to be put
2733 static inline void osd_object_put(const struct lu_env *env,
2734 struct osd_object *obj)
2736 lu_object_put(env, &obj->oo_dt.do_lu);
2740 * Index add function for interoperability mode (b11826).
2741 * It will add the directory entry.This entry is needed to
2742 * maintain name->fid mapping.
2744 * \param key, it is key i.e. file entry to be inserted
2745 * \param rec, it is value of given key i.e. fid
2747 * \retval 0, on success
2748 * \retval -ve, on error
2750 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2751 const struct dt_rec *rec,
2752 const struct dt_key *key, struct thandle *th,
2753 struct lustre_capa *capa, int ignore_quota)
2755 struct osd_object *obj = osd_dt_obj(dt);
2756 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
2757 const struct lu_fid_pack *pack = (const struct lu_fid_pack *)rec;
2758 const char *name = (const char *)key;
2759 struct osd_object *child;
2765 LASSERT(osd_invariant(obj));
2766 LASSERT(dt_object_exists(dt));
2767 LASSERT(th != NULL);
2769 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2772 rc = fid_unpack(pack, fid);
2775 child = osd_object_find(env, dt, fid);
2776 if (!IS_ERR(child)) {
2777 rc = osd_ea_add_rec(env, obj, child, name, th);
2778 osd_object_put(env, child);
2780 rc = PTR_ERR(child);
2783 LASSERT(osd_invariant(obj));
2788 * Initialize osd Iterator for given osd index object.
2790 * \param dt osd index object
2793 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2794 struct dt_object *dt,
2795 struct lustre_capa *capa)
2797 struct osd_it_iam *it;
2798 struct osd_thread_info *oti = osd_oti_get(env);
2799 struct osd_object *obj = osd_dt_obj(dt);
2800 struct lu_object *lo = &dt->do_lu;
2801 struct iam_path_descr *ipd;
2802 struct iam_container *bag = &obj->oo_dir->od_container;
2804 LASSERT(lu_object_exists(lo));
2806 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2807 return ERR_PTR(-EACCES);
2810 ipd = osd_it_ipd_get(env, bag);
2811 if (likely(ipd != NULL)) {
2815 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2816 return (struct dt_it *)it;
2818 return ERR_PTR(-ENOMEM);
2822 * free given Iterator.
2825 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2827 struct osd_it_iam *it = (struct osd_it_iam *)di;
2828 struct osd_object *obj = it->oi_obj;
2830 iam_it_fini(&it->oi_it);
2831 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2832 lu_object_put(env, &obj->oo_dt.do_lu);
2836 * Move Iterator to record specified by \a key
2838 * \param di osd iterator
2839 * \param key key for index
2841 * \retval +ve di points to record with least key not larger than key
2842 * \retval 0 di points to exact matched key
2843 * \retval -ve failure
2846 static int osd_it_iam_get(const struct lu_env *env,
2847 struct dt_it *di, const struct dt_key *key)
2849 struct osd_it_iam *it = (struct osd_it_iam *)di;
2851 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2857 * \param di osd iterator
2860 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2862 struct osd_it_iam *it = (struct osd_it_iam *)di;
2864 iam_it_put(&it->oi_it);
2868 * Move iterator by one record
2870 * \param di osd iterator
2872 * \retval +1 end of container reached
2874 * \retval -ve failure
2877 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2879 struct osd_it_iam *it = (struct osd_it_iam *)di;
2881 return iam_it_next(&it->oi_it);
2885 * Return pointer to the key under iterator.
2888 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2889 const struct dt_it *di)
2891 struct osd_it_iam *it = (struct osd_it_iam *)di;
2893 return (struct dt_key *)iam_it_key_get(&it->oi_it);
2897 * Return size of key under iterator (in bytes)
2900 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
2902 struct osd_it_iam *it = (struct osd_it_iam *)di;
2904 return iam_it_key_size(&it->oi_it);
2908 * Return pointer to the record under iterator.
2910 static struct dt_rec *osd_it_iam_rec(const struct lu_env *env,
2911 const struct dt_it *di)
2913 struct osd_it_iam *it = (struct osd_it_iam *)di;
2915 return (struct dt_rec *)iam_it_rec_get(&it->oi_it);
2919 * Returns cookie for current Iterator position.
2921 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
2923 struct osd_it_iam *it = (struct osd_it_iam *)di;
2925 return iam_it_store(&it->oi_it);
2929 * Restore iterator from cookie.
2931 * \param di osd iterator
2932 * \param hash Iterator location cookie
2934 * \retval +ve di points to record with least key not larger than key.
2935 * \retval 0 di points to exact matched key
2936 * \retval -ve failure
2939 static int osd_it_iam_load(const struct lu_env *env,
2940 const struct dt_it *di, __u64 hash)
2942 struct osd_it_iam *it = (struct osd_it_iam *)di;
2944 return iam_it_load(&it->oi_it, hash);
2947 static const struct dt_index_operations osd_index_iam_ops = {
2948 .dio_lookup = osd_index_iam_lookup,
2949 .dio_insert = osd_index_iam_insert,
2950 .dio_delete = osd_index_iam_delete,
2952 .init = osd_it_iam_init,
2953 .fini = osd_it_iam_fini,
2954 .get = osd_it_iam_get,
2955 .put = osd_it_iam_put,
2956 .next = osd_it_iam_next,
2957 .key = osd_it_iam_key,
2958 .key_size = osd_it_iam_key_size,
2959 .rec = osd_it_iam_rec,
2960 .store = osd_it_iam_store,
2961 .load = osd_it_iam_load
2966 * Creates or initializes iterator context.
2968 * \retval struct osd_it_ea, iterator structure on success
2971 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
2972 struct dt_object *dt,
2973 struct lustre_capa *capa)
2975 struct osd_object *obj = osd_dt_obj(dt);
2976 struct osd_thread_info *info = osd_oti_get(env);
2977 struct osd_it_ea *it = &info->oti_it_ea;
2978 struct lu_object *lo = &dt->do_lu;
2979 struct dentry *obj_dentry = &info->oti_it_dentry;
2981 LASSERT(lu_object_exists(lo));
2983 obj_dentry->d_inode = obj->oo_inode;
2984 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
2985 obj_dentry->d_name.hash = 0;
2987 it->oie_namelen = 0;
2988 it->oie_curr_pos = 0;
2989 it->oie_next_pos = 0;
2991 it->oie_file.f_dentry = obj_dentry;
2992 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
2993 it->oie_file.f_op = obj->oo_inode->i_fop;
2994 it->oie_file.private_data = NULL;
2997 RETURN((struct dt_it*) it);
3001 * Destroy or finishes iterator context.
3003 * \param di, struct osd_it_ea, iterator structure to be destroyed
3005 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3007 struct osd_it_ea *it = (struct osd_it_ea *)di;
3008 struct osd_object *obj = it->oie_obj;
3012 lu_object_put(env, &obj->oo_dt.do_lu);
3017 * It position the iterator at given key, so that next lookup continues from
3018 * that key Or it is similar to dio_it->load() but based on a key,
3019 * rather than file position.
3021 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3024 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3026 static int osd_it_ea_get(const struct lu_env *env,
3027 struct dt_it *di, const struct dt_key *key)
3029 struct osd_it_ea *it = (struct osd_it_ea *)di;
3032 LASSERT(((const char *)key)[0] == '\0');
3033 it->oie_namelen = 0;
3034 it->oie_curr_pos = 0;
3035 it->oie_next_pos = 0;
3043 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3048 * It is called internally by ->readdir(). It fills the
3049 * iterator's in-memory data structure with required
3050 * information i.e. name, namelen, rec_size etc.
3052 * \param buf, in which information to be filled in.
3053 * \param name, name of the file in given dir
3055 * \retval 0, on success
3056 * \retval 1, on buffer full
3058 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3059 loff_t offset, ino_t ino,
3060 unsigned int d_type)
3062 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3063 struct dirent64 *dirent = &it->oie_dirent64;
3064 int reclen = LDISKFS_DIR_REC_LEN(namelen);
3068 if (it->oie_namelen)
3071 if (namelen == 0 || namelen > LDISKFS_NAME_LEN)
3074 strncpy(dirent->d_name, name, LDISKFS_NAME_LEN);
3075 dirent->d_name[namelen] = 0;
3076 dirent->d_ino = ino;
3077 dirent->d_off = offset;
3078 dirent->d_reclen = reclen;
3079 it->oie_namelen = namelen;
3080 it->oie_curr_pos = offset;
3086 * Calls ->readdir() to load a directory entry at a time
3087 * and stored it in iterator's in-memory data structure.
3089 * \param di, struct osd_it_ea, iterator's in memory structure
3091 * \retval 0, on success
3092 * \retval -ve, on error
3094 int osd_ldiskfs_it_fill(const struct dt_it *di)
3096 struct osd_it_ea *it = (struct osd_it_ea *)di;
3097 struct osd_object *obj = it->oie_obj;
3098 struct inode *inode = obj->oo_inode;
3102 it->oie_namelen = 0;
3103 it->oie_file.f_pos = it->oie_curr_pos;
3105 result = inode->i_fop->readdir(&it->oie_file, it,
3106 (filldir_t) osd_ldiskfs_filldir);
3108 it->oie_next_pos = it->oie_file.f_pos;
3110 if(!result && it->oie_namelen == 0)
3117 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3118 * to load a directory entry at a time and stored it in
3119 * iterator's in-memory data structure.
3121 * \param di, struct osd_it_ea, iterator's in memory structure
3123 * \retval +ve, iterator reached to end
3124 * \retval 0, iterator not reached to end
3125 * \retval -ve, on error
3127 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3129 struct osd_it_ea *it = (struct osd_it_ea *)di;
3133 it->oie_curr_pos = it->oie_next_pos;
3135 if (it->oie_curr_pos == LDISKFS_HTREE_EOF)
3138 rc = osd_ldiskfs_it_fill(di);
3144 * Returns the key at current position from iterator's in memory structure.
3146 * \param di, struct osd_it_ea, iterator's in memory structure
3148 * \retval key i.e. struct dt_key on success
3150 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3151 const struct dt_it *di)
3153 struct osd_it_ea *it = (struct osd_it_ea *)di;
3155 RETURN((struct dt_key *)it->oie_dirent64.d_name);
3159 * Returns the key's size at current position from iterator's in memory structure.
3161 * \param di, struct osd_it_ea, iterator's in memory structure
3163 * \retval key_size i.e. struct dt_key on success
3165 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3167 struct osd_it_ea *it = (struct osd_it_ea *)di;
3169 RETURN(it->oie_namelen);
3173 * Returns the value (i.e. fid/igif) at current position from iterator's
3174 * in memory structure.
3176 * \param di, struct osd_it_ea, iterator's in memory structure
3178 * \retval value i.e. struct dt_rec on success
3180 static struct dt_rec *osd_it_ea_rec(const struct lu_env *env,
3181 const struct dt_it *di)
3183 struct osd_it_ea *it = (struct osd_it_ea *)di;
3184 struct osd_object *obj = it->oie_obj;
3185 struct osd_thread_info *info = osd_oti_get(env);
3186 struct osd_inode_id *id = &info->oti_id;
3187 struct lu_fid_pack *rec = &info->oti_pack;
3188 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3189 struct dentry *dentry = &info->oti_child_dentry;
3190 struct osd_device *dev;
3191 struct inode *inode;
3195 dev = osd_dev(ldev);
3196 id->oii_ino = it->oie_dirent64.d_ino;
3197 id->oii_gen = OSD_OII_NOGEN;
3198 inode = osd_iget(info, dev, id);
3199 if (!IS_ERR(inode)) {
3200 dentry->d_inode = inode;
3201 LASSERT(dentry->d_inode->i_sb == osd_sb(dev));
3203 CERROR("Error getting inode for ino =%d", id->oii_ino);
3204 RETURN((struct dt_rec *) PTR_ERR(inode));
3207 rc = osd_ea_fid_get(env, dentry, (struct dt_rec*) rec);
3210 RETURN((struct dt_rec *)rec);
3215 * Returns a cookie for current position of the iterator head, so that
3216 * user can use this cookie to load/start the iterator next time.
3218 * \param di, struct osd_it_ea, iterator's in memory structure
3220 * \retval cookie for current position, on success
3222 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3224 struct osd_it_ea *it = (struct osd_it_ea *)di;
3226 RETURN(it->oie_curr_pos);
3230 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3231 * to load a directory entry at a time and stored it i inn,
3232 * in iterator's in-memory data structure.
3234 * \param di, struct osd_it_ea, iterator's in memory structure
3236 * \retval +ve, on success
3237 * \retval -ve, on error
3239 static int osd_it_ea_load(const struct lu_env *env,
3240 const struct dt_it *di, __u64 hash)
3242 struct osd_it_ea *it = (struct osd_it_ea *)di;
3246 it->oie_curr_pos = it->oie_next_pos = hash;
3248 rc = osd_ldiskfs_it_fill(di);
3255 * Index and Iterator operations for interoperability
3256 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3258 static const struct dt_index_operations osd_index_ea_ops = {
3259 .dio_lookup = osd_index_ea_lookup,
3260 .dio_insert = osd_index_ea_insert,
3261 .dio_delete = osd_index_ea_delete,
3263 .init = osd_it_ea_init,
3264 .fini = osd_it_ea_fini,
3265 .get = osd_it_ea_get,
3266 .put = osd_it_ea_put,
3267 .next = osd_it_ea_next,
3268 .key = osd_it_ea_key,
3269 .key_size = osd_it_ea_key_size,
3270 .rec = osd_it_ea_rec,
3271 .store = osd_it_ea_store,
3272 .load = osd_it_ea_load
3277 * Index lookup function for interoperability mode (b11826).
3279 * \param key, key i.e. file name to be searched
3281 * \retval +ve, on success
3282 * \retval -ve, on error
3284 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3285 struct dt_rec *rec, const struct dt_key *key,
3286 struct lustre_capa *capa)
3288 struct osd_object *obj = osd_dt_obj(dt);
3293 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3294 LINVRNT(osd_invariant(obj));
3296 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3299 rc = osd_ea_lookup_rec(env, obj, rec, key);
3306 /* type constructor/destructor: osd_type_init, osd_type_fini */
3307 LU_TYPE_INIT_FINI(osd, &osd_key);
3309 static struct lu_context_key osd_key = {
3310 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3311 .lct_init = osd_key_init,
3312 .lct_fini = osd_key_fini,
3313 .lct_exit = osd_key_exit
3316 static void *osd_key_init(const struct lu_context *ctx,
3317 struct lu_context_key *key)
3319 struct osd_thread_info *info;
3321 OBD_ALLOC_PTR(info);
3323 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
3325 info = ERR_PTR(-ENOMEM);
3329 /* context key destructor: osd_key_fini */
3330 LU_KEY_FINI(osd, struct osd_thread_info);
3332 static void osd_key_exit(const struct lu_context *ctx,
3333 struct lu_context_key *key, void *data)
3335 struct osd_thread_info *info = data;
3337 LASSERT(info->oti_r_locks == 0);
3338 LASSERT(info->oti_w_locks == 0);
3339 LASSERT(info->oti_txns == 0);
3342 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3343 const char *name, struct lu_device *next)
3346 struct lu_context *ctx;
3348 /* context for commit hooks */
3349 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3350 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3352 rc = osd_procfs_init(osd_dev(d), name);
3353 ctx->lc_cookie = 0x3;
3358 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3360 struct osd_thread_info *info = osd_oti_get(env);
3362 if (o->od_obj_area != NULL) {
3363 lu_object_put(env, &o->od_obj_area->do_lu);
3364 o->od_obj_area = NULL;
3366 osd_oi_fini(info, &o->od_oi);
3371 static int osd_mount(const struct lu_env *env,
3372 struct osd_device *o, struct lustre_cfg *cfg)
3374 struct lustre_mount_info *lmi;
3375 const char *dev = lustre_cfg_string(cfg, 0);
3376 struct lustre_disk_data *ldd;
3377 struct lustre_sb_info *lsi;
3381 if (o->od_mount != NULL) {
3382 CERROR("Already mounted (%s)\n", dev);
3387 lmi = server_get_mount(dev);
3389 CERROR("Cannot get mount info for %s!\n", dev);
3393 LASSERT(lmi != NULL);
3394 /* save lustre_mount_info in dt_device */
3397 lsi = s2lsi(lmi->lmi_sb);
3400 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3402 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3406 o->od_obj_area = NULL;
3410 static struct lu_device *osd_device_fini(const struct lu_env *env,
3411 struct lu_device *d)
3416 shrink_dcache_sb(osd_sb(osd_dev(d)));
3417 osd_sync(env, lu2dt_dev(d));
3419 rc = osd_procfs_fini(osd_dev(d));
3421 CERROR("proc fini error %d \n", rc);
3422 RETURN (ERR_PTR(rc));
3425 if (osd_dev(d)->od_mount)
3426 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3427 osd_dev(d)->od_mount->lmi_mnt);
3428 osd_dev(d)->od_mount = NULL;
3430 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3434 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3435 struct lu_device_type *t,
3436 struct lustre_cfg *cfg)
3438 struct lu_device *l;
3439 struct osd_device *o;
3445 result = dt_device_init(&o->od_dt_dev, t);
3448 l->ld_ops = &osd_lu_ops;
3449 o->od_dt_dev.dd_ops = &osd_dt_ops;
3450 spin_lock_init(&o->od_osfs_lock);
3451 o->od_osfs_age = cfs_time_shift_64(-1000);
3452 o->od_capa_hash = init_capa_hash();
3453 if (o->od_capa_hash == NULL) {
3454 dt_device_fini(&o->od_dt_dev);
3455 l = ERR_PTR(-ENOMEM);
3458 l = ERR_PTR(result);
3463 l = ERR_PTR(-ENOMEM);
3467 static struct lu_device *osd_device_free(const struct lu_env *env,
3468 struct lu_device *d)
3470 struct osd_device *o = osd_dev(d);
3473 cleanup_capa_hash(o->od_capa_hash);
3474 dt_device_fini(&o->od_dt_dev);
3479 static int osd_process_config(const struct lu_env *env,
3480 struct lu_device *d, struct lustre_cfg *cfg)
3482 struct osd_device *o = osd_dev(d);
3486 switch(cfg->lcfg_command) {
3488 err = osd_mount(env, o, cfg);
3491 err = osd_shutdown(env, o);
3500 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
3501 struct ldiskfs_super_block * es);
3503 static int osd_recovery_complete(const struct lu_env *env,
3504 struct lu_device *d)
3506 struct osd_device *o = osd_dev(d);
3508 /* TODO: orphans handling */
3509 ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3513 static int osd_prepare(const struct lu_env *env,
3514 struct lu_device *pdev,
3515 struct lu_device *dev)
3517 struct osd_device *osd = osd_dev(dev);
3518 struct lustre_sb_info *lsi;
3519 struct lustre_disk_data *ldd;
3520 struct lustre_mount_info *lmi;
3521 struct osd_thread_info *oti = osd_oti_get(env);
3522 struct dt_object *d;
3526 /* 1. initialize oi before any file create or file open */
3527 result = osd_oi_init(oti, &osd->od_oi,
3528 &osd->od_dt_dev, lu2md_dev(pdev));
3532 lmi = osd->od_mount;
3533 lsi = s2lsi(lmi->lmi_sb);
3536 /* 2. setup local objects */
3537 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3541 /* 3. open remote object dir */
3542 d = dt_store_open(env, lu2dt_dev(dev), "",
3543 remote_obj_dir, &oti->oti_fid);
3545 osd->od_obj_area = d;
3548 result = PTR_ERR(d);
3549 osd->od_obj_area = NULL;
3556 static struct inode *osd_iget(struct osd_thread_info *info,
3557 struct osd_device *dev,
3558 const struct osd_inode_id *id)
3560 struct inode *inode;
3562 inode = iget(osd_sb(dev), id->oii_ino);
3563 if (inode == NULL) {
3564 CERROR("no inode\n");
3565 inode = ERR_PTR(-EACCES);
3566 } else if (is_bad_inode(inode)) {
3567 CERROR("bad inode\n");
3569 inode = ERR_PTR(-ENOENT);
3570 } else if (id->oii_gen != OSD_OII_NOGEN &&
3571 inode->i_generation != id->oii_gen) {
3572 CERROR("stale inode\n");
3574 inode = ERR_PTR(-ESTALE);
3581 static int osd_fid_lookup(const struct lu_env *env,
3582 struct osd_object *obj, const struct lu_fid *fid)
3584 struct osd_thread_info *info;
3585 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3586 struct osd_device *dev;
3587 struct osd_inode_id *id;
3589 struct inode *inode;
3592 LINVRNT(osd_invariant(obj));
3593 LASSERT(obj->oo_inode == NULL);
3594 LASSERT(fid_is_sane(fid));
3596 * This assertion checks that osd layer sees only local
3597 * fids. Unfortunately it is somewhat expensive (does a
3598 * cache-lookup). Disabling it for production/acceptance-testing.
3600 LASSERT(1 || fid_is_local(ldev->ld_site, fid));
3604 info = osd_oti_get(env);
3605 dev = osd_dev(ldev);
3609 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
3612 result = osd_oi_lookup(info, oi, fid, id);
3614 inode = osd_iget(info, dev, id);
3615 if (!IS_ERR(inode)) {
3616 obj->oo_inode = inode;
3617 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
3618 if (dev->od_iop_mode) {
3619 obj->oo_compat_dot_created = 1;
3620 obj->oo_compat_dotdot_created = 1;
3625 * If fid wasn't found in oi, inode-less object is
3626 * created, for which lu_object_exists() returns
3627 * false. This is used in a (frequent) case when
3628 * objects are created as locking anchors or
3629 * place holders for objects yet to be created.
3631 result = PTR_ERR(inode);
3632 } else if (result == -ENOENT)
3634 LINVRNT(osd_invariant(obj));
3639 static void osd_inode_getattr(const struct lu_env *env,
3640 struct inode *inode, struct lu_attr *attr)
3642 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
3643 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
3644 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
3646 attr->la_atime = LTIME_S(inode->i_atime);
3647 attr->la_mtime = LTIME_S(inode->i_mtime);
3648 attr->la_ctime = LTIME_S(inode->i_ctime);
3649 attr->la_mode = inode->i_mode;
3650 attr->la_size = i_size_read(inode);
3651 attr->la_blocks = inode->i_blocks;
3652 attr->la_uid = inode->i_uid;
3653 attr->la_gid = inode->i_gid;
3654 attr->la_flags = LDISKFS_I(inode)->i_flags;
3655 attr->la_nlink = inode->i_nlink;
3656 attr->la_rdev = inode->i_rdev;
3657 attr->la_blksize = ll_inode_blksize(inode);
3658 attr->la_blkbits = inode->i_blkbits;
3665 static int lu_device_is_osd(const struct lu_device *d)
3667 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
3670 static struct osd_object *osd_obj(const struct lu_object *o)
3672 LASSERT(lu_device_is_osd(o->lo_dev));
3673 return container_of0(o, struct osd_object, oo_dt.do_lu);
3676 static struct osd_device *osd_dt_dev(const struct dt_device *d)
3678 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
3679 return container_of0(d, struct osd_device, od_dt_dev);
3682 static struct osd_device *osd_dev(const struct lu_device *d)
3684 LASSERT(lu_device_is_osd(d));
3685 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
3688 static struct osd_object *osd_dt_obj(const struct dt_object *d)
3690 return osd_obj(&d->do_lu);
3693 static struct osd_device *osd_obj2dev(const struct osd_object *o)
3695 return osd_dev(o->oo_dt.do_lu.lo_dev);
3698 static struct lu_device *osd2lu_dev(struct osd_device *osd)
3700 return &osd->od_dt_dev.dd_lu_dev;
3703 static struct super_block *osd_sb(const struct osd_device *dev)
3705 return dev->od_mount->lmi_mnt->mnt_sb;
3708 static journal_t *osd_journal(const struct osd_device *dev)
3710 return LDISKFS_SB(osd_sb(dev))->s_journal;
3713 static int osd_has_index(const struct osd_object *obj)
3715 return obj->oo_dt.do_index_ops != NULL;
3718 static int osd_object_invariant(const struct lu_object *l)
3720 return osd_invariant(osd_obj(l));
3723 static const struct lu_object_operations osd_lu_obj_ops = {
3724 .loo_object_init = osd_object_init,
3725 .loo_object_delete = osd_object_delete,
3726 .loo_object_release = osd_object_release,
3727 .loo_object_free = osd_object_free,
3728 .loo_object_print = osd_object_print,
3729 .loo_object_invariant = osd_object_invariant
3732 static const struct lu_device_operations osd_lu_ops = {
3733 .ldo_object_alloc = osd_object_alloc,
3734 .ldo_process_config = osd_process_config,
3735 .ldo_recovery_complete = osd_recovery_complete,
3736 .ldo_prepare = osd_prepare,
3739 static const struct lu_device_type_operations osd_device_type_ops = {
3740 .ldto_init = osd_type_init,
3741 .ldto_fini = osd_type_fini,
3743 .ldto_start = osd_type_start,
3744 .ldto_stop = osd_type_stop,
3746 .ldto_device_alloc = osd_device_alloc,
3747 .ldto_device_free = osd_device_free,
3749 .ldto_device_init = osd_device_init,
3750 .ldto_device_fini = osd_device_fini
3753 static struct lu_device_type osd_device_type = {
3754 .ldt_tags = LU_DEVICE_DT,
3755 .ldt_name = LUSTRE_OSD_NAME,
3756 .ldt_ops = &osd_device_type_ops,
3757 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3761 * lprocfs legacy support.
3763 static struct obd_ops osd_obd_device_ops = {
3764 .o_owner = THIS_MODULE
3767 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3768 .llod_name = remote_obj_dir,
3769 .llod_oid = OSD_REM_OBJ_DIR_OID,
3771 .llod_feat = &dt_directory_features,
3774 static int __init osd_mod_init(void)
3776 struct lprocfs_static_vars lvars;
3779 llo_local_obj_register(&llod_osd_rem_obj_dir);
3780 lprocfs_osd_init_vars(&lvars);
3781 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3782 LUSTRE_OSD_NAME, &osd_device_type);
3785 static void __exit osd_mod_exit(void)
3787 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3788 class_unregister_type(LUSTRE_OSD_NAME);
3791 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3792 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3793 MODULE_LICENSE("GPL");
3795 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);