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 dot[] = ".";
89 static const char dotdot[] = "..";
90 static const char remote_obj_dir[] = "REM_OBJ_DIR";
92 struct osd_directory {
93 struct iam_container od_container;
94 struct iam_descr od_descr;
95 struct semaphore od_sem;
99 struct dt_object oo_dt;
101 * Inode for file system object represented by this osd_object. This
102 * inode is pinned for the whole duration of lu_object life.
104 * Not modified concurrently (either setup early during object
105 * creation, or assigned by osd_object_create() under write lock).
107 struct inode *oo_inode;
108 struct rw_semaphore oo_sem;
109 struct osd_directory *oo_dir;
110 /** protects inode attributes. */
113 * Following two members are used to indicate the presence of dot and
114 * dotdot in the given directory. This is required for interop mode
117 int oo_compat_dot_created;
118 int oo_compat_dotdot_created;
120 const struct lu_env *oo_owner;
121 #ifdef CONFIG_LOCKDEP
122 struct lockdep_map oo_dep_map;
126 static int osd_root_get (const struct lu_env *env,
127 struct dt_device *dev, struct lu_fid *f);
129 static int lu_device_is_osd (const struct lu_device *d);
130 static void osd_mod_exit (void) __exit;
131 static int osd_mod_init (void) __init;
132 static int osd_type_init (struct lu_device_type *t);
133 static void osd_type_fini (struct lu_device_type *t);
134 static int osd_object_init (const struct lu_env *env,
136 const struct lu_object_conf *_);
137 static void osd_object_release(const struct lu_env *env,
138 struct lu_object *l);
139 static int osd_object_print (const struct lu_env *env, void *cookie,
140 lu_printer_t p, const struct lu_object *o);
141 static struct lu_device *osd_device_free (const struct lu_env *env,
142 struct lu_device *m);
143 static void *osd_key_init (const struct lu_context *ctx,
144 struct lu_context_key *key);
145 static void osd_key_fini (const struct lu_context *ctx,
146 struct lu_context_key *key, void *data);
147 static void osd_key_exit (const struct lu_context *ctx,
148 struct lu_context_key *key, void *data);
149 static int osd_has_index (const struct osd_object *obj);
150 static void osd_object_init0 (struct osd_object *obj);
151 static int osd_device_init (const struct lu_env *env,
152 struct lu_device *d, const char *,
154 static int osd_fid_lookup (const struct lu_env *env,
155 struct osd_object *obj,
156 const struct lu_fid *fid);
157 static void osd_inode_getattr (const struct lu_env *env,
158 struct inode *inode, struct lu_attr *attr);
159 static int osd_inode_setattr (const struct lu_env *env,
160 struct inode *inode, const struct lu_attr *attr);
161 static int osd_param_is_sane (const struct osd_device *dev,
162 const struct txn_param *param);
163 static int osd_index_iam_lookup(const struct lu_env *env,
164 struct dt_object *dt,
165 struct dt_rec *rec, const struct dt_key *key,
166 struct lustre_capa *capa);
167 static int osd_index_ea_lookup(const struct lu_env *env,
168 struct dt_object *dt,
169 struct dt_rec *rec, const struct dt_key *key,
170 struct lustre_capa *capa);
171 static int osd_index_iam_insert(const struct lu_env *env,
172 struct dt_object *dt,
173 const struct dt_rec *rec,
174 const struct dt_key *key,
175 struct thandle *handle,
176 struct lustre_capa *capa,
178 static int osd_index_ea_insert (const struct lu_env *env,
179 struct dt_object *dt,
180 const struct dt_rec *rec,
181 const struct dt_key *key,
182 struct thandle *handle,
183 struct lustre_capa *capa,
185 static int osd_index_iam_delete(const struct lu_env *env,
186 struct dt_object *dt, const struct dt_key *key,
187 struct thandle *handle,
188 struct lustre_capa *capa);
189 static int osd_index_ea_delete (const struct lu_env *env,
190 struct dt_object *dt, const struct dt_key *key,
191 struct thandle *handle,
192 struct lustre_capa *capa);
194 static int osd_iam_index_probe (const struct lu_env *env,
195 struct osd_object *o,
196 const struct dt_index_features *feat);
197 static int osd_index_try (const struct lu_env *env,
198 struct dt_object *dt,
199 const struct dt_index_features *feat);
200 static void osd_index_fini (struct osd_object *o);
202 static void osd_it_iam_fini (const struct lu_env *env, struct dt_it *di);
203 static int osd_it_iam_get (const struct lu_env *env,
204 struct dt_it *di, const struct dt_key *key);
205 static void osd_it_iam_put (const struct lu_env *env, struct dt_it *di);
206 static int osd_it_iam_next (const struct lu_env *env, struct dt_it *di);
207 static int osd_it_iam_key_size (const struct lu_env *env,
208 const struct dt_it *di);
209 static void osd_it_ea_fini (const struct lu_env *env, struct dt_it *di);
210 static int osd_it_ea_get (const struct lu_env *env,
211 struct dt_it *di, const struct dt_key *key);
212 static void osd_it_ea_put (const struct lu_env *env, struct dt_it *di);
213 static int osd_it_ea_next (const struct lu_env *env, struct dt_it *di);
214 static int osd_it_ea_key_size(const struct lu_env *env,
215 const struct dt_it *di);
217 static void osd_conf_get (const struct lu_env *env,
218 const struct dt_device *dev,
219 struct dt_device_param *param);
220 static void osd_trans_stop (const struct lu_env *env,
222 static int osd_object_is_root(const struct osd_object *obj);
224 static struct osd_object *osd_obj (const struct lu_object *o);
225 static struct osd_device *osd_dev (const struct lu_device *d);
226 static struct osd_device *osd_dt_dev (const struct dt_device *d);
227 static struct osd_object *osd_dt_obj (const struct dt_object *d);
228 static struct osd_device *osd_obj2dev (const struct osd_object *o);
229 static struct lu_device *osd2lu_dev (struct osd_device *osd);
230 static struct lu_device *osd_device_fini (const struct lu_env *env,
231 struct lu_device *d);
232 static struct lu_device *osd_device_alloc (const struct lu_env *env,
233 struct lu_device_type *t,
234 struct lustre_cfg *cfg);
235 static struct lu_object *osd_object_alloc (const struct lu_env *env,
236 const struct lu_object_header *hdr,
237 struct lu_device *d);
238 static struct inode *osd_iget (struct osd_thread_info *info,
239 struct osd_device *dev,
240 const struct osd_inode_id *id);
241 static struct super_block *osd_sb (const struct osd_device *dev);
242 static struct dt_it *osd_it_iam_init (const struct lu_env *env,
243 struct dt_object *dt,
244 struct lustre_capa *capa);
245 static struct dt_key *osd_it_iam_key (const struct lu_env *env,
246 const struct dt_it *di);
247 static struct dt_rec *osd_it_iam_rec (const struct lu_env *env,
248 const struct dt_it *di);
249 static struct dt_it *osd_it_ea_init (const struct lu_env *env,
250 struct dt_object *dt,
251 struct lustre_capa *capa);
252 static struct dt_key *osd_it_ea_key (const struct lu_env *env,
253 const struct dt_it *di);
254 static struct dt_rec *osd_it_ea_rec (const struct lu_env *env,
255 const struct dt_it *di);
257 static struct timespec *osd_inode_time (const struct lu_env *env,
260 static struct thandle *osd_trans_start (const struct lu_env *env,
262 struct txn_param *p);
263 static journal_t *osd_journal (const struct osd_device *dev);
265 static int __osd_ea_add_rec(struct osd_thread_info *info,
266 struct osd_object *pobj,
267 struct osd_object *cobj,
271 static const struct lu_device_type_operations osd_device_type_ops;
272 static struct lu_device_type osd_device_type;
273 static const struct lu_object_operations osd_lu_obj_ops;
274 static struct obd_ops osd_obd_device_ops;
275 static const struct lu_device_operations osd_lu_ops;
276 static struct lu_context_key osd_key;
277 static const struct dt_object_operations osd_obj_ops;
278 static const struct dt_object_operations osd_obj_ea_ops;
279 static const struct dt_body_operations osd_body_ops;
280 static const struct dt_index_operations osd_index_iam_ops;
281 static const struct dt_index_operations osd_index_ea_ops;
284 struct thandle ot_super;
286 struct journal_callback ot_jcb;
287 /* Link to the device, for debugging. */
288 struct lu_ref_link *ot_dev_link;
292 #ifdef HAVE_QUOTA_SUPPORT
294 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
296 struct md_ucred *uc = md_ucred(env);
300 save->oc_uid = current->fsuid;
301 save->oc_gid = current->fsgid;
302 save->oc_cap = current->cap_effective;
303 current->fsuid = uc->mu_fsuid;
304 current->fsgid = uc->mu_fsgid;
305 current->cap_effective = uc->mu_cap;
309 osd_pop_ctxt(struct osd_ctxt *save)
311 current->fsuid = save->oc_uid;
312 current->fsgid = save->oc_gid;
313 current->cap_effective = save->oc_cap;
318 * Invariants, assertions.
322 * XXX: do not enable this, until invariant checking code is made thread safe
323 * in the face of pdirops locking.
325 #define OSD_INVARIANT_CHECKS (0)
327 #if OSD_INVARIANT_CHECKS
328 static int osd_invariant(const struct osd_object *obj)
332 ergo(obj->oo_inode != NULL,
333 obj->oo_inode->i_sb == osd_sb(osd_obj2dev(obj)) &&
334 atomic_read(&obj->oo_inode->i_count) > 0) &&
335 ergo(obj->oo_dir != NULL &&
336 obj->oo_dir->od_conationer.ic_object != NULL,
337 obj->oo_dir->od_conationer.ic_object == obj->oo_inode);
340 #define osd_invariant(obj) (1)
343 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
345 return lu_context_key_get(&env->le_ctx, &osd_key);
349 * Concurrency: doesn't matter
351 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
353 return osd_oti_get(env)->oti_r_locks > 0;
357 * Concurrency: doesn't matter
359 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
361 struct osd_thread_info *oti = osd_oti_get(env);
362 return oti->oti_w_locks > 0 && o->oo_owner == env;
366 * Concurrency: doesn't access mutable data
368 static int osd_root_get(const struct lu_env *env,
369 struct dt_device *dev, struct lu_fid *f)
373 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
374 lu_igif_build(f, inode->i_ino, inode->i_generation);
379 * OSD object methods.
383 * Concurrency: no concurrent access is possible that early in object
386 static struct lu_object *osd_object_alloc(const struct lu_env *env,
387 const struct lu_object_header *hdr,
390 struct osd_object *mo;
396 l = &mo->oo_dt.do_lu;
397 dt_object_init(&mo->oo_dt, NULL, d);
398 if (osd_dev(d)->od_iop_mode)
399 mo->oo_dt.do_ops = &osd_obj_ea_ops;
401 mo->oo_dt.do_ops = &osd_obj_ops;
403 l->lo_ops = &osd_lu_obj_ops;
404 init_rwsem(&mo->oo_sem);
405 spin_lock_init(&mo->oo_guard);
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 *_)
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);
457 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
458 const struct iam_container *bag)
460 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
461 osd_oti_get(env)->oti_it_ipd);
464 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
465 const struct iam_container *bag)
467 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
468 osd_oti_get(env)->oti_idx_ipd);
471 static void osd_ipd_put(const struct lu_env *env,
472 const struct iam_container *bag,
473 struct iam_path_descr *ipd)
475 bag->ic_descr->id_ops->id_ipd_free(ipd);
479 * Concurrency: no concurrent access is possible that late in object
482 static void osd_index_fini(struct osd_object *o)
484 struct iam_container *bag;
486 if (o->oo_dir != NULL) {
487 bag = &o->oo_dir->od_container;
488 if (o->oo_inode != NULL) {
489 if (bag->ic_object == o->oo_inode)
490 iam_container_fini(bag);
492 OBD_FREE_PTR(o->oo_dir);
498 * Concurrency: no concurrent access is possible that late in object
499 * life-cycle (for all existing callers, that is. New callers have to provide
500 * their own locking.)
502 static int osd_inode_unlinked(const struct inode *inode)
504 return inode->i_nlink == 0;
508 OSD_TXN_OI_DELETE_CREDITS = 20,
509 OSD_TXN_INODE_DELETE_CREDITS = 20
513 * Concurrency: no concurrent access is possible that late in object
516 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
518 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
519 struct osd_device *osd = osd_obj2dev(obj);
520 struct osd_thread_info *oti = osd_oti_get(env);
521 struct txn_param *prm = &oti->oti_txn;
525 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
526 OSD_TXN_INODE_DELETE_CREDITS);
527 th = osd_trans_start(env, &osd->od_dt_dev, prm);
529 result = osd_oi_delete(oti, &osd->od_oi, fid, th);
530 osd_trans_stop(env, th);
532 result = PTR_ERR(th);
537 * Called just before object is freed. Releases all resources except for
538 * object itself (that is released by osd_object_free()).
540 * Concurrency: no concurrent access is possible that late in object
543 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
545 struct osd_object *obj = osd_obj(l);
546 struct inode *inode = obj->oo_inode;
548 LINVRNT(osd_invariant(obj));
551 * If object is unlinked remove fid->ino mapping from object index.
558 if (osd_inode_unlinked(inode)) {
559 result = osd_inode_remove(env, obj);
561 LU_OBJECT_DEBUG(D_ERROR, env, l,
562 "Failed to cleanup: %d\n",
567 obj->oo_inode = NULL;
572 * Concurrency: ->loo_object_release() is called under site spin-lock.
574 static void osd_object_release(const struct lu_env *env,
577 struct osd_object *o = osd_obj(l);
579 LASSERT(!lu_object_is_dying(l->lo_header));
580 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
581 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
585 * Concurrency: shouldn't matter.
587 static int osd_object_print(const struct lu_env *env, void *cookie,
588 lu_printer_t p, const struct lu_object *l)
590 struct osd_object *o = osd_obj(l);
593 if (o->oo_dir != NULL)
594 d = o->oo_dir->od_container.ic_descr;
597 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
599 o->oo_inode ? o->oo_inode->i_ino : 0UL,
600 o->oo_inode ? o->oo_inode->i_generation : 0,
601 d ? d->id_ops->id_name : "plain");
605 * Concurrency: shouldn't matter.
607 int osd_statfs(const struct lu_env *env, struct dt_device *d,
610 struct osd_device *osd = osd_dt_dev(d);
611 struct super_block *sb = osd_sb(osd);
614 spin_lock(&osd->od_osfs_lock);
616 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
617 result = ll_do_statfs(sb, &osd->od_kstatfs);
618 if (likely(result == 0)) /* N.B. statfs can't really fail */
619 osd->od_osfs_age = cfs_time_current_64();
622 if (likely(result == 0))
623 *sfs = osd->od_kstatfs;
624 spin_unlock(&osd->od_osfs_lock);
630 * Concurrency: doesn't access mutable data.
632 static void osd_conf_get(const struct lu_env *env,
633 const struct dt_device *dev,
634 struct dt_device_param *param)
637 * XXX should be taken from not-yet-existing fs abstraction layer.
639 param->ddp_max_name_len = LDISKFS_NAME_LEN;
640 param->ddp_max_nlink = LDISKFS_LINK_MAX;
641 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
645 * Helper function to get and fill the buffer with input values.
647 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
651 buf = &osd_oti_get(env)->oti_buf;
662 * Concurrency: doesn't access mutable data.
664 static int osd_param_is_sane(const struct osd_device *dev,
665 const struct txn_param *param)
667 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
671 * Concurrency: shouldn't matter.
673 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
675 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
676 struct thandle *th = &oh->ot_super;
677 struct dt_device *dev = th->th_dev;
678 struct lu_device *lud = &dev->dd_lu_dev;
680 LASSERT(dev != NULL);
681 LASSERT(oh->ot_handle == NULL);
684 CERROR("transaction @0x%p commit error: %d\n", th, error);
686 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
688 * This od_env_for_commit is only for commit usage. see
691 lu_context_enter(&env->le_ctx);
692 dt_txn_hook_commit(env, th);
693 lu_context_exit(&env->le_ctx);
696 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
700 lu_context_exit(&th->th_ctx);
701 lu_context_fini(&th->th_ctx);
706 * Concurrency: shouldn't matter.
708 static struct thandle *osd_trans_start(const struct lu_env *env,
712 struct osd_device *dev = osd_dt_dev(d);
714 struct osd_thandle *oh;
720 hook_res = dt_txn_hook_start(env, d, p);
722 RETURN(ERR_PTR(hook_res));
724 if (osd_param_is_sane(dev, p)) {
725 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
727 struct osd_thread_info *oti = osd_oti_get(env);
730 * XXX temporary stuff. Some abstraction layer should
734 jh = journal_start(osd_journal(dev), p->tp_credits);
740 jh->h_sync = p->tp_sync;
741 lu_device_get(&d->dd_lu_dev);
742 oh->ot_dev_link = lu_ref_add
743 (&d->dd_lu_dev.ld_reference,
745 /* add commit callback */
746 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
747 lu_context_enter(&th->th_ctx);
748 journal_callback_set(jh, osd_trans_commit_cb,
749 (struct journal_callback *)&oh->ot_jcb);
750 LASSERT(oti->oti_txns == 0);
751 LASSERT(oti->oti_r_locks == 0);
752 LASSERT(oti->oti_w_locks == 0);
759 th = ERR_PTR(-ENOMEM);
761 CERROR("Invalid transaction parameters\n");
762 th = ERR_PTR(-EINVAL);
769 * Concurrency: shouldn't matter.
771 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
774 struct osd_thandle *oh;
775 struct osd_thread_info *oti = osd_oti_get(env);
779 oh = container_of0(th, struct osd_thandle, ot_super);
780 if (oh->ot_handle != NULL) {
781 handle_t *hdl = oh->ot_handle;
783 LASSERT(oti->oti_txns == 1);
785 LASSERT(oti->oti_r_locks == 0);
786 LASSERT(oti->oti_w_locks == 0);
787 result = dt_txn_hook_stop(env, th);
789 CERROR("Failure in transaction hook: %d\n", result);
790 oh->ot_handle = NULL;
791 result = journal_stop(hdl);
793 CERROR("Failure to stop transaction: %d\n", result);
799 * Concurrency: shouldn't matter.
801 static int osd_sync(const struct lu_env *env, struct dt_device *d)
803 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
804 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
808 * Start commit for OSD device.
810 * An implementation of dt_commit_async method for OSD device.
811 * Asychronously starts underlayng fs sync and thereby a transaction
814 * \param env environment
817 * \see dt_device_operations
819 static int osd_commit_async(const struct lu_env *env,
822 struct super_block *s = osd_sb(osd_dt_dev(d));
825 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
826 RETURN(s->s_op->sync_fs(s, 0));
830 * Concurrency: shouldn't matter.
832 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
834 static void osd_ro(const struct lu_env *env, struct dt_device *d)
838 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
840 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
841 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
847 * Concurrency: serialization provided by callers.
849 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
850 int mode, unsigned long timeout, __u32 alg,
851 struct lustre_capa_key *keys)
853 struct osd_device *dev = osd_dt_dev(d);
856 dev->od_fl_capa = mode;
857 dev->od_capa_timeout = timeout;
858 dev->od_capa_alg = alg;
859 dev->od_capa_keys = keys;
864 * Concurrency: serialization provided by callers.
866 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
867 struct dt_quota_ctxt *ctxt, void *data)
869 struct obd_device *obd = (void *)ctxt;
870 struct vfsmount *mnt = (struct vfsmount *)data;
873 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
874 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
875 obd->obd_lvfs_ctxt.pwdmnt = mnt;
876 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
877 obd->obd_lvfs_ctxt.fs = get_ds();
883 * Note: we do not count into QUOTA here.
884 * If we mount with --data_journal we may need more.
886 static const int osd_dto_credits_noquota[DTO_NR] = {
889 * INDEX_EXTRA_TRANS_BLOCKS(8) +
890 * SINGLEDATA_TRANS_BLOCKS(8)
891 * XXX Note: maybe iam need more, since iam have more level than
894 [DTO_INDEX_INSERT] = 16,
895 [DTO_INDEX_DELETE] = 16,
899 [DTO_IDNEX_UPDATE] = 16,
901 * Create a object. The same as create object in EXT3.
902 * DATA_TRANS_BLOCKS(14) +
903 * INDEX_EXTRA_BLOCKS(8) +
904 * 3(inode bits, groups, GDT)
906 [DTO_OBJECT_CREATE] = 25,
910 [DTO_OBJECT_DELETE] = 25,
913 * 3(inode bits, group, GDT)
915 [DTO_ATTR_SET_BASE] = 3,
917 * Xattr set. The same as xattr of EXT3.
918 * DATA_TRANS_BLOCKS(14)
919 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
920 * also counted in. Do not know why?
922 [DTO_XATTR_SET] = 14,
925 * creadits for inode change during write.
927 [DTO_WRITE_BASE] = 3,
929 * credits for single block write.
931 [DTO_WRITE_BLOCK] = 14,
933 * Attr set credits for chown.
934 * 3 (inode bit, group, GDT)
936 [DTO_ATTR_SET_CHOWN]= 3
940 * Note: we count into QUOTA here.
941 * If we mount with --data_journal we may need more.
943 static const int osd_dto_credits_quota[DTO_NR] = {
945 * INDEX_EXTRA_TRANS_BLOCKS(8) +
946 * SINGLEDATA_TRANS_BLOCKS(8) +
947 * 2 * QUOTA_TRANS_BLOCKS(2)
949 [DTO_INDEX_INSERT] = 20,
951 * INDEX_EXTRA_TRANS_BLOCKS(8) +
952 * SINGLEDATA_TRANS_BLOCKS(8) +
953 * 2 * QUOTA_TRANS_BLOCKS(2)
955 [DTO_INDEX_DELETE] = 20,
959 [DTO_IDNEX_UPDATE] = 16,
961 * Create a object. Same as create object in EXT3 filesystem.
962 * DATA_TRANS_BLOCKS(16) +
963 * INDEX_EXTRA_BLOCKS(8) +
964 * 3(inode bits, groups, GDT) +
965 * 2 * QUOTA_INIT_BLOCKS(25)
967 [DTO_OBJECT_CREATE] = 77,
970 * DATA_TRANS_BLOCKS(16) +
971 * INDEX_EXTRA_BLOCKS(8) +
972 * 3(inode bits, groups, GDT) +
975 [DTO_OBJECT_DELETE] = 27,
978 * 3 (inode bit, group, GDT) +
980 [DTO_ATTR_SET_BASE] = 3,
982 * Xattr set. The same as xattr of EXT3.
983 * DATA_TRANS_BLOCKS(16)
984 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
985 * also counted in. Do not know why?
987 [DTO_XATTR_SET] = 16,
990 * creadits for inode change during write.
992 [DTO_WRITE_BASE] = 3,
994 * credits for single block write.
996 [DTO_WRITE_BLOCK] = 16,
998 * Attr set credits for chown.
999 * 3 (inode bit, group, GDT) +
1000 * 2 * QUOTA_INIT_BLOCKS(25) +
1001 * 2 * QUOTA_DEL_BLOCKS(9)
1003 [DTO_ATTR_SET_CHOWN]= 71
1006 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1009 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1010 ARRAY_SIZE(osd_dto_credits_quota));
1011 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1012 #ifdef HAVE_QUOTA_SUPPORT
1013 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1014 return osd_dto_credits_quota[op];
1017 return osd_dto_credits_noquota[op];
1020 static const struct dt_device_operations osd_dt_ops = {
1021 .dt_root_get = osd_root_get,
1022 .dt_statfs = osd_statfs,
1023 .dt_trans_start = osd_trans_start,
1024 .dt_trans_stop = osd_trans_stop,
1025 .dt_conf_get = osd_conf_get,
1026 .dt_sync = osd_sync,
1028 .dt_commit_async = osd_commit_async,
1029 .dt_credit_get = osd_credit_get,
1030 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1031 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1034 static void osd_object_read_lock(const struct lu_env *env,
1035 struct dt_object *dt, unsigned role)
1037 struct osd_object *obj = osd_dt_obj(dt);
1038 struct osd_thread_info *oti = osd_oti_get(env);
1040 LINVRNT(osd_invariant(obj));
1042 LASSERT(obj->oo_owner != env);
1043 down_read_nested(&obj->oo_sem, role);
1045 LASSERT(obj->oo_owner == NULL);
1049 static void osd_object_write_lock(const struct lu_env *env,
1050 struct dt_object *dt, unsigned role)
1052 struct osd_object *obj = osd_dt_obj(dt);
1053 struct osd_thread_info *oti = osd_oti_get(env);
1055 LINVRNT(osd_invariant(obj));
1057 LASSERT(obj->oo_owner != env);
1058 down_write_nested(&obj->oo_sem, role);
1060 LASSERT(obj->oo_owner == NULL);
1061 obj->oo_owner = env;
1065 static void osd_object_read_unlock(const struct lu_env *env,
1066 struct dt_object *dt)
1068 struct osd_object *obj = osd_dt_obj(dt);
1069 struct osd_thread_info *oti = osd_oti_get(env);
1071 LINVRNT(osd_invariant(obj));
1073 LASSERT(oti->oti_r_locks > 0);
1075 up_read(&obj->oo_sem);
1078 static void osd_object_write_unlock(const struct lu_env *env,
1079 struct dt_object *dt)
1081 struct osd_object *obj = osd_dt_obj(dt);
1082 struct osd_thread_info *oti = osd_oti_get(env);
1084 LINVRNT(osd_invariant(obj));
1086 LASSERT(obj->oo_owner == env);
1087 LASSERT(oti->oti_w_locks > 0);
1089 obj->oo_owner = NULL;
1090 up_write(&obj->oo_sem);
1093 static int capa_is_sane(const struct lu_env *env,
1094 struct osd_device *dev,
1095 struct lustre_capa *capa,
1096 struct lustre_capa_key *keys)
1098 struct osd_thread_info *oti = osd_oti_get(env);
1099 struct lustre_capa *tcapa = &oti->oti_capa;
1100 struct obd_capa *oc;
1104 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1106 if (capa_is_expired(oc)) {
1107 DEBUG_CAPA(D_ERROR, capa, "expired");
1114 if (capa_is_expired_sec(capa)) {
1115 DEBUG_CAPA(D_ERROR, capa, "expired");
1119 spin_lock(&capa_lock);
1120 for (i = 0; i < 2; i++) {
1121 if (keys[i].lk_keyid == capa->lc_keyid) {
1122 oti->oti_capa_key = keys[i];
1126 spin_unlock(&capa_lock);
1129 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1133 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1137 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1138 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1142 oc = capa_add(dev->od_capa_hash, capa);
1148 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1149 struct lustre_capa *capa, __u64 opc)
1151 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1152 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1153 struct md_capainfo *ci;
1156 if (!dev->od_fl_capa)
1159 if (capa == BYPASS_CAPA)
1162 ci = md_capainfo(env);
1166 if (ci->mc_auth == LC_ID_NONE)
1170 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1174 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1175 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1180 if (!capa_opc_supported(capa, opc)) {
1181 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1185 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1186 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1193 static int osd_attr_get(const struct lu_env *env,
1194 struct dt_object *dt,
1195 struct lu_attr *attr,
1196 struct lustre_capa *capa)
1198 struct osd_object *obj = osd_dt_obj(dt);
1200 LASSERT(dt_object_exists(dt));
1201 LINVRNT(osd_invariant(obj));
1203 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1206 spin_lock(&obj->oo_guard);
1207 osd_inode_getattr(env, obj->oo_inode, attr);
1208 spin_unlock(&obj->oo_guard);
1212 static int osd_attr_set(const struct lu_env *env,
1213 struct dt_object *dt,
1214 const struct lu_attr *attr,
1215 struct thandle *handle,
1216 struct lustre_capa *capa)
1218 struct osd_object *obj = osd_dt_obj(dt);
1221 LASSERT(handle != NULL);
1222 LASSERT(dt_object_exists(dt));
1223 LASSERT(osd_invariant(obj));
1225 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1228 spin_lock(&obj->oo_guard);
1229 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1230 spin_unlock(&obj->oo_guard);
1233 mark_inode_dirty(obj->oo_inode);
1237 static struct timespec *osd_inode_time(const struct lu_env *env,
1238 struct inode *inode, __u64 seconds)
1240 struct osd_thread_info *oti = osd_oti_get(env);
1241 struct timespec *t = &oti->oti_time;
1243 t->tv_sec = seconds;
1245 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1249 static int osd_inode_setattr(const struct lu_env *env,
1250 struct inode *inode, const struct lu_attr *attr)
1254 bits = attr->la_valid;
1256 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1258 #ifdef HAVE_QUOTA_SUPPORT
1259 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1260 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1261 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1267 iattr.ia_valid |= ATTR_UID;
1269 iattr.ia_valid |= ATTR_GID;
1270 iattr.ia_uid = attr->la_uid;
1271 iattr.ia_gid = attr->la_gid;
1272 osd_push_ctxt(env, save);
1273 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1280 if (bits & LA_ATIME)
1281 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1282 if (bits & LA_CTIME)
1283 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1284 if (bits & LA_MTIME)
1285 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1286 if (bits & LA_SIZE) {
1287 LDISKFS_I(inode)->i_disksize = attr->la_size;
1288 i_size_write(inode, attr->la_size);
1292 * OSD should not change "i_blocks" which is used by quota.
1293 * "i_blocks" should be changed by ldiskfs only.
1294 * Disable this assignment until SOM to fix some EA field. */
1295 if (bits & LA_BLOCKS)
1296 inode->i_blocks = attr->la_blocks;
1299 inode->i_mode = (inode->i_mode & S_IFMT) |
1300 (attr->la_mode & ~S_IFMT);
1302 inode->i_uid = attr->la_uid;
1304 inode->i_gid = attr->la_gid;
1305 if (bits & LA_NLINK)
1306 inode->i_nlink = attr->la_nlink;
1308 inode->i_rdev = attr->la_rdev;
1310 if (bits & LA_FLAGS) {
1311 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1313 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1314 (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1322 * XXX temporary solution.
1325 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1326 struct lu_attr *attr, struct thandle *th)
1331 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1332 struct lu_attr *attr, struct thandle *th)
1334 LASSERT(obj->oo_inode != NULL);
1336 osd_object_init0(obj);
1340 extern struct inode *ldiskfs_create_inode(handle_t *handle,
1341 struct inode * dir, int mode);
1342 extern int ldiskfs_add_entry(handle_t *handle, struct dentry *dentry,
1343 struct inode *inode);
1344 extern int ldiskfs_delete_entry(handle_t *handle,
1346 struct ldiskfs_dir_entry_2 * de_del,
1347 struct buffer_head * bh);
1348 extern struct buffer_head * ldiskfs_find_entry(struct dentry *dentry,
1349 struct ldiskfs_dir_entry_2
1351 extern int ldiskfs_add_dot_dotdot(handle_t *handle, struct inode *dir,
1352 struct inode *inode);
1354 extern int ldiskfs_xattr_set_handle(handle_t *handle, struct inode *inode,
1355 int name_index, const char *name,
1356 const void *value, size_t value_len,
1359 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1360 struct osd_object *obj,
1364 struct osd_thread_info *info = osd_oti_get(env);
1365 struct dentry *child_dentry = &info->oti_child_dentry;
1366 struct dentry *obj_dentry = &info->oti_obj_dentry;
1368 obj_dentry->d_inode = obj->oo_inode;
1369 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1370 obj_dentry->d_name.hash = 0;
1372 child_dentry->d_name.hash = 0;
1373 child_dentry->d_parent = obj_dentry;
1374 child_dentry->d_name.name = name;
1375 child_dentry->d_name.len = namelen;
1376 return child_dentry;
1380 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1382 struct dt_allocation_hint *hint,
1386 struct osd_device *osd = osd_obj2dev(obj);
1387 struct osd_thandle *oth;
1388 struct dt_object *parent;
1389 struct inode *inode;
1390 #ifdef HAVE_QUOTA_SUPPORT
1391 struct osd_ctxt *save = &info->oti_ctxt;
1394 LINVRNT(osd_invariant(obj));
1395 LASSERT(obj->oo_inode == NULL);
1397 oth = container_of(th, struct osd_thandle, ot_super);
1398 LASSERT(oth->ot_handle->h_transaction != NULL);
1400 if (hint && hint->dah_parent)
1401 parent = hint->dah_parent;
1403 parent = osd->od_obj_area;
1405 LASSERT(parent != NULL);
1406 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1408 #ifdef HAVE_QUOTA_SUPPORT
1409 osd_push_ctxt(info->oti_env, save);
1411 inode = ldiskfs_create_inode(oth->ot_handle,
1412 osd_dt_obj(parent)->oo_inode, mode);
1413 #ifdef HAVE_QUOTA_SUPPORT
1416 if (!IS_ERR(inode)) {
1417 obj->oo_inode = inode;
1420 result = PTR_ERR(inode);
1421 LINVRNT(osd_invariant(obj));
1426 extern int iam_lvar_create(struct inode *obj, int keysize, int ptrsize,
1427 int recsize, handle_t *handle);
1429 extern int iam_lfix_create(struct inode *obj, int keysize, int ptrsize,
1430 int recsize, handle_t *handle);
1437 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1438 struct lu_attr *attr,
1439 struct dt_allocation_hint *hint,
1440 struct dt_object_format *dof,
1444 struct osd_thandle *oth;
1445 struct osd_device *osd = osd_obj2dev(obj);
1446 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1448 LASSERT(S_ISDIR(attr->la_mode));
1450 oth = container_of(th, struct osd_thandle, ot_super);
1451 LASSERT(oth->ot_handle->h_transaction != NULL);
1452 result = osd_mkfile(info, obj, mode, hint, th);
1453 if (result == 0 && osd->od_iop_mode == 0) {
1454 LASSERT(obj->oo_inode != NULL);
1456 * XXX uh-oh... call low-level iam function directly.
1459 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1460 sizeof (struct lu_fid_pack),
1466 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1467 struct lu_attr *attr,
1468 struct dt_allocation_hint *hint,
1469 struct dt_object_format *dof,
1473 struct osd_thandle *oth;
1474 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1476 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1478 LASSERT(S_ISREG(attr->la_mode));
1480 oth = container_of(th, struct osd_thandle, ot_super);
1481 LASSERT(oth->ot_handle->h_transaction != NULL);
1483 result = osd_mkfile(info, obj, mode, hint, th);
1485 LASSERT(obj->oo_inode != NULL);
1486 if (feat->dif_flags & DT_IND_VARKEY)
1487 result = iam_lvar_create(obj->oo_inode,
1488 feat->dif_keysize_max,
1490 feat->dif_recsize_max,
1493 result = iam_lfix_create(obj->oo_inode,
1494 feat->dif_keysize_max,
1496 feat->dif_recsize_max,
1503 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1504 struct lu_attr *attr,
1505 struct dt_allocation_hint *hint,
1506 struct dt_object_format *dof,
1509 LASSERT(S_ISREG(attr->la_mode));
1510 return osd_mkfile(info, obj, (attr->la_mode &
1511 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1514 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1515 struct lu_attr *attr,
1516 struct dt_allocation_hint *hint,
1517 struct dt_object_format *dof,
1520 LASSERT(S_ISLNK(attr->la_mode));
1521 return osd_mkfile(info, obj, (attr->la_mode &
1522 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1525 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1526 struct lu_attr *attr,
1527 struct dt_allocation_hint *hint,
1528 struct dt_object_format *dof,
1531 umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1534 LINVRNT(osd_invariant(obj));
1535 LASSERT(obj->oo_inode == NULL);
1536 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1537 S_ISFIFO(mode) || S_ISSOCK(mode));
1539 result = osd_mkfile(info, obj, mode, hint, th);
1541 LASSERT(obj->oo_inode != NULL);
1542 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1544 LINVRNT(osd_invariant(obj));
1548 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1550 struct dt_allocation_hint *hint,
1551 struct dt_object_format *dof,
1554 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1556 osd_obj_type_f result;
1572 result = osd_mk_index;
1583 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1584 struct dt_object *parent, umode_t child_mode)
1588 memset(ah, 0, sizeof(*ah));
1589 ah->dah_parent = parent;
1590 ah->dah_mode = child_mode;
1594 * Helper function for osd_object_create()
1596 * \retval 0, on success
1598 static int __osd_object_create(struct osd_thread_info *info,
1599 struct osd_object *obj, struct lu_attr *attr,
1600 struct dt_allocation_hint *hint,
1601 struct dt_object_format *dof,
1607 result = osd_create_pre(info, obj, attr, th);
1609 result = osd_create_type_f(dof->dof_type)(info, obj,
1610 attr, hint, dof, th);
1612 result = osd_create_post(info, obj, attr, th);
1618 * Helper function for osd_object_create()
1620 * \retval 0, on success
1622 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1623 const struct lu_fid *fid, struct thandle *th)
1625 struct osd_thread_info *info = osd_oti_get(env);
1626 struct osd_inode_id *id = &info->oti_id;
1627 struct osd_device *osd = osd_obj2dev(obj);
1628 struct md_ucred *uc = md_ucred(env);
1630 LASSERT(obj->oo_inode != NULL);
1631 LASSERT(uc != NULL);
1633 id->oii_ino = obj->oo_inode->i_ino;
1634 id->oii_gen = obj->oo_inode->i_generation;
1636 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1637 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1640 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1641 struct lu_attr *attr,
1642 struct dt_allocation_hint *hint,
1643 struct dt_object_format *dof,
1646 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1647 struct osd_object *obj = osd_dt_obj(dt);
1648 struct osd_thread_info *info = osd_oti_get(env);
1653 LINVRNT(osd_invariant(obj));
1654 LASSERT(!dt_object_exists(dt));
1655 LASSERT(osd_write_locked(env, obj));
1656 LASSERT(th != NULL);
1658 result = __osd_object_create(info, obj, attr, hint, dof, th);
1660 result = __osd_oi_insert(env, obj, fid, th);
1662 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1663 LASSERT(osd_invariant(obj));
1668 * Helper function for osd_xattr_set()
1670 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1671 const struct lu_buf *buf, const char *name, int fl)
1673 struct osd_object *obj = osd_dt_obj(dt);
1674 struct inode *inode = obj->oo_inode;
1675 struct osd_thread_info *info = osd_oti_get(env);
1676 struct dentry *dentry = &info->oti_child_dentry;
1677 struct timespec *t = &info->oti_time;
1681 LASSERT(dt_object_exists(dt));
1682 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1683 LASSERT(osd_write_locked(env, obj));
1685 if (fl & LU_XATTR_REPLACE)
1686 fs_flags |= XATTR_REPLACE;
1688 if (fl & LU_XATTR_CREATE)
1689 fs_flags |= XATTR_CREATE;
1691 dentry->d_inode = inode;
1692 *t = inode->i_ctime;
1693 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1694 buf->lb_len, fs_flags);
1695 if (likely(rc == 0)) {
1696 spin_lock(&obj->oo_guard);
1697 inode->i_ctime = *t;
1698 spin_unlock(&obj->oo_guard);
1699 mark_inode_dirty(inode);
1705 * Put the fid into lustre_mdt_attrs, and then place the structure
1706 * inode's ea. This fid should not be altered during the life time
1709 * \retval +ve, on success
1710 * \retval -ve, on error
1712 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1714 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1715 const struct lu_fid *fid)
1717 struct osd_thread_info *info = osd_oti_get(env);
1718 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1720 fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1722 return __osd_xattr_set(env, dt,
1723 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1724 XATTR_NAME_LMA, LU_XATTR_CREATE);
1729 * Helper function to form igif
1731 static inline void osd_igif_get(const struct lu_env *env, struct dentry *dentry,
1734 struct inode *inode = dentry->d_inode;
1735 lu_igif_build(fid, inode->i_ino, inode->i_generation);
1739 * Helper function to pack the fid
1741 static inline void osd_fid_pack(const struct lu_env *env, const struct lu_fid *fid,
1742 struct lu_fid_pack *pack)
1744 fid_pack(pack, fid, &osd_oti_get(env)->oti_fid);
1748 * Try to read the fid from inode ea into dt_rec, if return value
1749 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1751 * \param rec, the data-structure into which fid/igif is read
1753 * \retval 0, on success
1755 static int osd_ea_fid_get(const struct lu_env *env, struct dentry *dentry,
1758 struct inode *inode = dentry->d_inode;
1759 struct osd_thread_info *info = osd_oti_get(env);
1760 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1761 struct lu_fid *fid = &info->oti_fid;
1764 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1766 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1770 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1772 } else if (rc == -ENODATA) {
1773 osd_igif_get(env, dentry, fid);
1778 osd_fid_pack(env, fid, (struct lu_fid_pack*)rec);
1784 * OSD layer object create function for interoperability mode (b11826).
1785 * This is mostly similar to osd_object_create(). Only difference being, fid is
1786 * inserted into inode ea here.
1788 * \retval 0, on success
1789 * \retval -ve, on error
1791 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1792 struct lu_attr *attr,
1793 struct dt_allocation_hint *hint,
1794 struct dt_object_format *dof,
1797 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1798 struct osd_object *obj = osd_dt_obj(dt);
1799 struct osd_thread_info *info = osd_oti_get(env);
1805 LASSERT(osd_invariant(obj));
1806 LASSERT(!dt_object_exists(dt));
1807 LASSERT(osd_write_locked(env, obj));
1808 LASSERT(th != NULL);
1810 result = __osd_object_create(info, obj, attr, hint, dof, th);
1812 if (hint && hint->dah_parent)
1813 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1815 /* objects under osd root shld have igif fid, so dont add fid EA */
1816 if (result == 0 && is_root == 0)
1817 result = osd_ea_fid_set(env, dt, fid);
1820 result = __osd_oi_insert(env, obj, fid, th);
1822 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1823 LINVRNT(osd_invariant(obj));
1828 * Concurrency: @dt is write locked.
1830 static void osd_object_ref_add(const struct lu_env *env,
1831 struct dt_object *dt,
1834 struct osd_object *obj = osd_dt_obj(dt);
1835 struct inode *inode = obj->oo_inode;
1837 LINVRNT(osd_invariant(obj));
1838 LASSERT(dt_object_exists(dt));
1839 LASSERT(osd_write_locked(env, obj));
1840 LASSERT(th != NULL);
1842 spin_lock(&obj->oo_guard);
1843 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1845 spin_unlock(&obj->oo_guard);
1846 mark_inode_dirty(inode);
1847 LINVRNT(osd_invariant(obj));
1851 * Concurrency: @dt is write locked.
1853 static void osd_object_ref_del(const struct lu_env *env,
1854 struct dt_object *dt,
1857 struct osd_object *obj = osd_dt_obj(dt);
1858 struct inode *inode = obj->oo_inode;
1860 LINVRNT(osd_invariant(obj));
1861 LASSERT(dt_object_exists(dt));
1862 LASSERT(osd_write_locked(env, obj));
1863 LASSERT(th != NULL);
1865 spin_lock(&obj->oo_guard);
1866 LASSERT(inode->i_nlink > 0);
1868 spin_unlock(&obj->oo_guard);
1869 mark_inode_dirty(inode);
1870 LINVRNT(osd_invariant(obj));
1874 * Concurrency: @dt is read locked.
1876 static int osd_xattr_get(const struct lu_env *env,
1877 struct dt_object *dt,
1880 struct lustre_capa *capa)
1882 struct osd_object *obj = osd_dt_obj(dt);
1883 struct inode *inode = obj->oo_inode;
1884 struct osd_thread_info *info = osd_oti_get(env);
1885 struct dentry *dentry = &info->oti_obj_dentry;
1887 LASSERT(dt_object_exists(dt));
1888 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1889 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1891 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1894 dentry->d_inode = inode;
1895 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1900 * Concurrency: @dt is write locked.
1902 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1903 const struct lu_buf *buf, const char *name, int fl,
1904 struct thandle *handle, struct lustre_capa *capa)
1906 LASSERT(handle != NULL);
1908 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1911 return __osd_xattr_set(env, dt, buf, name, fl);
1915 * Concurrency: @dt is read locked.
1917 static int osd_xattr_list(const struct lu_env *env,
1918 struct dt_object *dt,
1920 struct lustre_capa *capa)
1922 struct osd_object *obj = osd_dt_obj(dt);
1923 struct inode *inode = obj->oo_inode;
1924 struct osd_thread_info *info = osd_oti_get(env);
1925 struct dentry *dentry = &info->oti_obj_dentry;
1927 LASSERT(dt_object_exists(dt));
1928 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1929 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1931 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1934 dentry->d_inode = inode;
1935 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1939 * Concurrency: @dt is write locked.
1941 static int osd_xattr_del(const struct lu_env *env,
1942 struct dt_object *dt,
1944 struct thandle *handle,
1945 struct lustre_capa *capa)
1947 struct osd_object *obj = osd_dt_obj(dt);
1948 struct inode *inode = obj->oo_inode;
1949 struct osd_thread_info *info = osd_oti_get(env);
1950 struct dentry *dentry = &info->oti_obj_dentry;
1951 struct timespec *t = &info->oti_time;
1954 LASSERT(dt_object_exists(dt));
1955 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1956 LASSERT(osd_write_locked(env, obj));
1957 LASSERT(handle != NULL);
1959 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1962 dentry->d_inode = inode;
1963 *t = inode->i_ctime;
1964 rc = inode->i_op->removexattr(dentry, name);
1965 if (likely(rc == 0)) {
1966 /* ctime should not be updated with server-side time. */
1967 spin_lock(&obj->oo_guard);
1968 inode->i_ctime = *t;
1969 spin_unlock(&obj->oo_guard);
1970 mark_inode_dirty(inode);
1975 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1976 struct dt_object *dt,
1977 struct lustre_capa *old,
1980 struct osd_thread_info *info = osd_oti_get(env);
1981 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1982 struct osd_object *obj = osd_dt_obj(dt);
1983 struct osd_device *dev = osd_obj2dev(obj);
1984 struct lustre_capa_key *key = &info->oti_capa_key;
1985 struct lustre_capa *capa = &info->oti_capa;
1986 struct obd_capa *oc;
1987 struct md_capainfo *ci;
1991 if (!dev->od_fl_capa)
1992 RETURN(ERR_PTR(-ENOENT));
1994 LASSERT(dt_object_exists(dt));
1995 LINVRNT(osd_invariant(obj));
1997 /* renewal sanity check */
1998 if (old && osd_object_auth(env, dt, old, opc))
1999 RETURN(ERR_PTR(-EACCES));
2001 ci = md_capainfo(env);
2003 RETURN(ERR_PTR(-ENOENT));
2005 switch (ci->mc_auth) {
2009 capa->lc_uid = obj->oo_inode->i_uid;
2010 capa->lc_gid = obj->oo_inode->i_gid;
2011 capa->lc_flags = LC_ID_PLAIN;
2013 case LC_ID_CONVERT: {
2016 s[0] = obj->oo_inode->i_uid;
2017 get_random_bytes(&(s[1]), sizeof(__u32));
2018 s[2] = obj->oo_inode->i_gid;
2019 get_random_bytes(&(s[3]), sizeof(__u32));
2020 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2022 RETURN(ERR_PTR(rc));
2024 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2025 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2026 capa->lc_flags = LC_ID_CONVERT;
2030 RETURN(ERR_PTR(-EINVAL));
2033 capa->lc_fid = *fid;
2035 capa->lc_flags |= dev->od_capa_alg << 24;
2036 capa->lc_timeout = dev->od_capa_timeout;
2037 capa->lc_expiry = 0;
2039 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2041 LASSERT(!capa_is_expired(oc));
2045 spin_lock(&capa_lock);
2046 *key = dev->od_capa_keys[1];
2047 spin_unlock(&capa_lock);
2049 capa->lc_keyid = key->lk_keyid;
2050 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2052 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2054 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2055 RETURN(ERR_PTR(rc));
2058 oc = capa_add(dev->od_capa_hash, capa);
2062 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2065 struct osd_object *obj = osd_dt_obj(dt);
2066 struct inode *inode = obj->oo_inode;
2067 struct osd_thread_info *info = osd_oti_get(env);
2068 struct dentry *dentry = &info->oti_obj_dentry;
2069 struct file *file = &info->oti_file;
2072 dentry->d_inode = inode;
2073 file->f_dentry = dentry;
2074 file->f_mapping = inode->i_mapping;
2075 file->f_op = inode->i_fop;
2076 LOCK_INODE_MUTEX(inode);
2077 rc = file->f_op->fsync(file, dentry, 0);
2078 UNLOCK_INODE_MUTEX(inode);
2082 static const struct dt_object_operations osd_obj_ops = {
2083 .do_read_lock = osd_object_read_lock,
2084 .do_write_lock = osd_object_write_lock,
2085 .do_read_unlock = osd_object_read_unlock,
2086 .do_write_unlock = osd_object_write_unlock,
2087 .do_attr_get = osd_attr_get,
2088 .do_attr_set = osd_attr_set,
2089 .do_ah_init = osd_ah_init,
2090 .do_create = osd_object_create,
2091 .do_index_try = osd_index_try,
2092 .do_ref_add = osd_object_ref_add,
2093 .do_ref_del = osd_object_ref_del,
2094 .do_xattr_get = osd_xattr_get,
2095 .do_xattr_set = osd_xattr_set,
2096 .do_xattr_del = osd_xattr_del,
2097 .do_xattr_list = osd_xattr_list,
2098 .do_capa_get = osd_capa_get,
2099 .do_object_sync = osd_object_sync,
2103 * dt_object_operations for interoperability mode
2104 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2106 static const struct dt_object_operations osd_obj_ea_ops = {
2107 .do_read_lock = osd_object_read_lock,
2108 .do_write_lock = osd_object_write_lock,
2109 .do_read_unlock = osd_object_read_unlock,
2110 .do_write_unlock = osd_object_write_unlock,
2111 .do_attr_get = osd_attr_get,
2112 .do_attr_set = osd_attr_set,
2113 .do_ah_init = osd_ah_init,
2114 .do_create = osd_object_ea_create,
2115 .do_index_try = osd_index_try,
2116 .do_ref_add = osd_object_ref_add,
2117 .do_ref_del = osd_object_ref_del,
2118 .do_xattr_get = osd_xattr_get,
2119 .do_xattr_set = osd_xattr_set,
2120 .do_xattr_del = osd_xattr_del,
2121 .do_xattr_list = osd_xattr_list,
2122 .do_capa_get = osd_capa_get,
2123 .do_object_sync = osd_object_sync,
2131 * XXX: Another layering violation for now.
2133 * We don't want to use ->f_op->read methods, because generic file write
2135 * - serializes on ->i_sem, and
2137 * - does a lot of extra work like balance_dirty_pages(),
2139 * which doesn't work for globally shared files like /last-received.
2141 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2142 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2143 loff_t *offs, handle_t *handle);
2145 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2146 struct lu_buf *buf, loff_t *pos,
2147 struct lustre_capa *capa)
2149 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2151 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2154 return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2157 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2158 const struct lu_buf *buf, loff_t *pos,
2159 struct thandle *handle, struct lustre_capa *capa,
2162 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2163 struct osd_thandle *oh;
2165 #ifdef HAVE_QUOTA_SUPPORT
2166 cfs_cap_t save = current->cap_effective;
2169 LASSERT(handle != NULL);
2171 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2174 oh = container_of(handle, struct osd_thandle, ot_super);
2175 LASSERT(oh->ot_handle->h_transaction != NULL);
2176 #ifdef HAVE_QUOTA_SUPPORT
2178 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2180 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2182 result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2183 pos, oh->ot_handle);
2184 #ifdef HAVE_QUOTA_SUPPORT
2185 current->cap_effective = save;
2188 result = buf->lb_len;
2192 static const struct dt_body_operations osd_body_ops = {
2193 .dbo_read = osd_read,
2194 .dbo_write = osd_write
2201 static int osd_object_is_root(const struct osd_object *obj)
2203 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
2206 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2207 const struct dt_index_features *feat)
2209 struct iam_descr *descr;
2210 struct dt_object *dt = &o->oo_dt;
2212 if (osd_object_is_root(o))
2213 return feat == &dt_directory_features;
2215 LASSERT(o->oo_dir != NULL);
2217 descr = o->oo_dir->od_container.ic_descr;
2218 if (feat == &dt_directory_features) {
2219 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2222 if (descr == &iam_htree_compat_param) {
2223 /* if it is a HTREE dir then there is good chance that,
2224 * we dealing with ext3 directory here with no FIDs. */
2226 if (descr->id_rec_size ==
2227 sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2229 dt->do_index_ops = &osd_index_ea_ops;
2236 feat->dif_keysize_min <= descr->id_key_size &&
2237 descr->id_key_size <= feat->dif_keysize_max &&
2238 feat->dif_recsize_min <= descr->id_rec_size &&
2239 descr->id_rec_size <= feat->dif_recsize_max &&
2240 !(feat->dif_flags & (DT_IND_VARKEY |
2241 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2242 ergo(feat->dif_flags & DT_IND_UPDATE,
2243 1 /* XXX check that object (and file system) is
2248 static int osd_iam_container_init(const struct lu_env *env,
2249 struct osd_object *obj,
2250 struct osd_directory *dir)
2253 struct iam_container *bag;
2255 bag = &dir->od_container;
2256 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2258 result = iam_container_setup(bag);
2260 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2262 iam_container_fini(bag);
2268 * Concurrency: no external locking is necessary.
2270 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2271 const struct dt_index_features *feat)
2275 struct osd_object *obj = osd_dt_obj(dt);
2276 struct osd_device *osd = osd_obj2dev(obj);
2278 LINVRNT(osd_invariant(obj));
2279 LASSERT(dt_object_exists(dt));
2281 if (osd_object_is_root(obj)) {
2282 dt->do_index_ops = &osd_index_ea_ops;
2284 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2285 dt->do_index_ops = &osd_index_ea_ops;
2286 if (S_ISDIR(obj->oo_inode->i_mode))
2291 } else if (!osd_has_index(obj)) {
2292 struct osd_directory *dir;
2296 sema_init(&dir->od_sem, 1);
2298 spin_lock(&obj->oo_guard);
2299 if (obj->oo_dir == NULL)
2303 * Concurrent thread allocated container data.
2306 spin_unlock(&obj->oo_guard);
2308 * Now, that we have container data, serialize its
2311 down(&obj->oo_dir->od_sem);
2313 * recheck under lock.
2315 if (!osd_has_index(obj))
2316 result = osd_iam_container_init(env, obj, dir);
2319 up(&obj->oo_dir->od_sem);
2325 if (result == 0 && ea_dir == 0) {
2326 if (!osd_iam_index_probe(env, obj, feat))
2329 LINVRNT(osd_invariant(obj));
2335 * delete a (key, value) pair from index \a dt specified by \a key
2337 * \param dt_object osd index object
2338 * \param key key for index
2339 * \param rec record reference
2340 * \param handle transaction handler
2343 * \retval -ve failure
2346 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2347 const struct dt_key *key, struct thandle *handle,
2348 struct lustre_capa *capa)
2350 struct osd_object *obj = osd_dt_obj(dt);
2351 struct osd_thandle *oh;
2352 struct iam_path_descr *ipd;
2353 struct iam_container *bag = &obj->oo_dir->od_container;
2358 LINVRNT(osd_invariant(obj));
2359 LASSERT(dt_object_exists(dt));
2360 LASSERT(bag->ic_object == obj->oo_inode);
2361 LASSERT(handle != NULL);
2363 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2366 ipd = osd_idx_ipd_get(env, bag);
2367 if (unlikely(ipd == NULL))
2370 oh = container_of0(handle, struct osd_thandle, ot_super);
2371 LASSERT(oh->ot_handle != NULL);
2372 LASSERT(oh->ot_handle->h_transaction != NULL);
2374 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2375 osd_ipd_put(env, bag, ipd);
2376 LINVRNT(osd_invariant(obj));
2381 * Index delete function for interoperability mode (b11826).
2382 * It will remove the directory entry added by osd_index_ea_insert().
2383 * This entry is needed to maintain name->fid mapping.
2385 * \param key, key i.e. file entry to be deleted
2387 * \retval 0, on success
2388 * \retval -ve, on error
2390 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2391 const struct dt_key *key, struct thandle *handle,
2392 struct lustre_capa *capa)
2394 struct osd_object *obj = osd_dt_obj(dt);
2395 struct inode *dir = obj->oo_inode;
2396 struct dentry *dentry;
2397 struct osd_thandle *oh;
2398 struct ldiskfs_dir_entry_2 *de;
2399 struct buffer_head *bh;
2405 LINVRNT(osd_invariant(obj));
2406 LASSERT(dt_object_exists(dt));
2407 LASSERT(handle != NULL);
2409 oh = container_of(handle, struct osd_thandle, ot_super);
2410 LASSERT(oh->ot_handle != NULL);
2411 LASSERT(oh->ot_handle->h_transaction != NULL);
2413 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2416 dentry = osd_child_dentry_get(env, obj,
2417 (char *)key, strlen((char *)key));
2418 bh = ldiskfs_find_entry(dentry, &de);
2420 rc = ldiskfs_delete_entry(oh->ot_handle,
2423 mark_inode_dirty(dir);
2428 LASSERT(osd_invariant(obj));
2433 * Lookup index for \a key and copy record to \a rec.
2435 * \param dt_object osd index object
2436 * \param key key for index
2437 * \param rec record reference
2439 * \retval +ve success : exact mach
2440 * \retval 0 return record with key not greater than \a key
2441 * \retval -ve failure
2443 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2444 struct dt_rec *rec, const struct dt_key *key,
2445 struct lustre_capa *capa)
2447 struct osd_object *obj = osd_dt_obj(dt);
2448 struct iam_path_descr *ipd;
2449 struct iam_container *bag = &obj->oo_dir->od_container;
2450 struct osd_thread_info *oti = osd_oti_get(env);
2451 struct iam_iterator *it = &oti->oti_idx_it;
2455 LASSERT(osd_invariant(obj));
2456 LASSERT(dt_object_exists(dt));
2457 LASSERT(bag->ic_object == obj->oo_inode);
2459 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2462 ipd = osd_idx_ipd_get(env, bag);
2466 /* got ipd now we can start iterator. */
2467 iam_it_init(it, bag, 0, ipd);
2469 rc = iam_it_get(it, (struct iam_key *)key);
2471 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2475 osd_ipd_put(env, bag, ipd);
2477 LINVRNT(osd_invariant(obj));
2483 * Inserts (key, value) pair in \a dt index object.
2485 * \param dt osd index object
2486 * \param key key for index
2487 * \param rec record reference
2488 * \param th transaction handler
2491 * \retval -ve failure
2493 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2494 const struct dt_rec *rec, const struct dt_key *key,
2495 struct thandle *th, struct lustre_capa *capa,
2498 struct osd_object *obj = osd_dt_obj(dt);
2499 struct iam_path_descr *ipd;
2500 struct osd_thandle *oh;
2501 struct iam_container *bag = &obj->oo_dir->od_container;
2502 #ifdef HAVE_QUOTA_SUPPORT
2503 cfs_cap_t save = current->cap_effective;
2509 LINVRNT(osd_invariant(obj));
2510 LASSERT(dt_object_exists(dt));
2511 LASSERT(bag->ic_object == obj->oo_inode);
2512 LASSERT(th != NULL);
2514 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2517 ipd = osd_idx_ipd_get(env, bag);
2518 if (unlikely(ipd == NULL))
2521 oh = container_of0(th, struct osd_thandle, ot_super);
2522 LASSERT(oh->ot_handle != NULL);
2523 LASSERT(oh->ot_handle->h_transaction != NULL);
2524 #ifdef HAVE_QUOTA_SUPPORT
2526 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2528 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2530 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2531 (struct iam_rec *)rec, ipd);
2532 #ifdef HAVE_QUOTA_SUPPORT
2533 current->cap_effective = save;
2535 osd_ipd_put(env, bag, ipd);
2536 LINVRNT(osd_invariant(obj));
2541 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2542 * into the directory.Also sets flags into osd object to
2543 * indicate dot and dotdot are created. This is required for
2544 * interoperability mode (b11826)
2546 * \param dir directory for dot and dotdot fixup.
2547 * \param obj child object for linking
2549 * \retval 0, on success
2550 * \retval -ve, on error
2552 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2553 struct osd_object *dir,
2554 struct osd_object *obj, const char *name,
2557 struct inode *parent_dir = obj->oo_inode;
2558 struct inode *inode = dir->oo_inode;
2559 struct osd_thandle *oth;
2562 oth = container_of(th, struct osd_thandle, ot_super);
2563 LASSERT(oth->ot_handle->h_transaction != NULL);
2564 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2566 if (strcmp(name, dot) == 0) {
2567 if (dir->oo_compat_dot_created) {
2570 LASSERT(obj == dir);
2571 dir->oo_compat_dot_created = 1;
2574 } else if(strcmp(name, dotdot) == 0) {
2575 if (!dir->oo_compat_dot_created)
2577 if (dir->oo_compat_dotdot_created)
2578 return __osd_ea_add_rec(info, dir, obj, name, th);
2580 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2582 dir->oo_compat_dotdot_created = 1;
2589 * Calls ldiskfs_add_entry() to add directory entry
2590 * into the directory. This is required for
2591 * interoperability mode (b11826)
2593 * \retval 0, on success
2594 * \retval -ve, on error
2596 static int __osd_ea_add_rec(struct osd_thread_info *info,
2597 struct osd_object *pobj,
2598 struct osd_object *cobj,
2602 struct dentry *child;
2603 struct osd_thandle *oth;
2604 struct inode *cinode = cobj->oo_inode;
2607 oth = container_of(th, struct osd_thandle, ot_super);
2608 LASSERT(oth->ot_handle != NULL);
2609 LASSERT(oth->ot_handle->h_transaction != NULL);
2611 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2612 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2618 * It will call the appropriate osd_add* function and return the
2619 * value, return by respective functions.
2621 static int osd_ea_add_rec(const struct lu_env *env,
2622 struct osd_object *pobj,
2623 struct osd_object *cobj,
2627 struct osd_thread_info *info = osd_oti_get(env);
2630 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2632 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2634 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2640 * Calls ->lookup() to find dentry. From dentry get inode and
2641 * read inode's ea to get fid. This is required for interoperability
2644 * \retval 0, on success
2645 * \retval -ve, on error
2647 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2648 struct dt_rec *rec, const struct dt_key *key)
2650 struct inode *dir = obj->oo_inode;
2651 struct osd_thread_info *info = osd_oti_get(env);
2652 struct dentry *dentry;
2653 struct osd_device *dev = osd_dev(obj->oo_dt.do_lu.lo_dev);
2654 struct osd_inode_id *id = &info->oti_id;
2655 struct ldiskfs_dir_entry_2 *de;
2656 struct buffer_head *bh;
2657 struct inode *inode;
2661 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2663 dentry = osd_child_dentry_get(env, obj,
2664 (char *)key, strlen((char *)key));
2665 bh = ldiskfs_find_entry(dentry, &de);
2667 ino = le32_to_cpu(de->inode);
2670 id->oii_gen = OSD_OII_NOGEN;
2672 inode = osd_iget(info, dev, id);
2673 if (!IS_ERR(inode)) {
2674 dentry->d_inode = inode;
2676 rc = osd_ea_fid_get(env, dentry, rec);
2687 * Find the osd object for given fid.
2689 * \param fid, need to find the osd object having this fid
2691 * \retval osd_object, on success
2692 * \retval -ve, on error
2694 struct osd_object *osd_object_find(const struct lu_env *env,
2695 struct dt_object *dt,
2696 const struct lu_fid *fid)
2698 struct lu_device *ludev = dt->do_lu.lo_dev;
2699 struct osd_object *child = NULL;
2700 struct lu_object *luch;
2701 struct lu_object *lo;
2703 luch = lu_object_find(env, ludev, fid, NULL);
2704 if (!IS_ERR(luch)) {
2705 if (lu_object_exists(luch)) {
2706 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2708 child = osd_obj(lo);
2710 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2711 "lu_object can't be located"
2712 ""DFID"\n", PFID(fid));
2714 if (child == NULL) {
2715 lu_object_put(env, luch);
2716 CERROR("Unable to get osd_object\n");
2717 child = ERR_PTR(-ENOENT);
2720 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2721 "lu_object does not exists "DFID"\n",
2723 child = ERR_PTR(-ENOENT);
2726 child = (void *)luch;
2732 * Put the osd object once done with it.
2734 * \param obj, osd object that needs to be put
2736 static inline void osd_object_put(const struct lu_env *env,
2737 struct osd_object *obj)
2739 lu_object_put(env, &obj->oo_dt.do_lu);
2743 * Index add function for interoperability mode (b11826).
2744 * It will add the directory entry.This entry is needed to
2745 * maintain name->fid mapping.
2747 * \param key, it is key i.e. file entry to be inserted
2748 * \param rec, it is value of given key i.e. fid
2750 * \retval 0, on success
2751 * \retval -ve, on error
2753 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2754 const struct dt_rec *rec,
2755 const struct dt_key *key, struct thandle *th,
2756 struct lustre_capa *capa, int ignore_quota)
2758 struct osd_object *obj = osd_dt_obj(dt);
2759 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
2760 const struct lu_fid_pack *pack = (const struct lu_fid_pack *)rec;
2761 const char *name = (const char *)key;
2762 struct osd_object *child;
2763 #ifdef HAVE_QUOTA_SUPPORT
2764 cfs_cap_t save = current->cap_effective;
2770 LASSERT(osd_invariant(obj));
2771 LASSERT(dt_object_exists(dt));
2772 LASSERT(th != NULL);
2774 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2777 rc = fid_unpack(pack, fid);
2780 child = osd_object_find(env, dt, fid);
2781 if (!IS_ERR(child)) {
2782 #ifdef HAVE_QUOTA_SUPPORT
2784 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2786 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2788 rc = osd_ea_add_rec(env, obj, child, name, th);
2790 #ifdef HAVE_QUOTA_SUPPORT
2791 current->cap_effective = save;
2793 osd_object_put(env, child);
2795 rc = PTR_ERR(child);
2798 LASSERT(osd_invariant(obj));
2803 * Initialize osd Iterator for given osd index object.
2805 * \param dt osd index object
2808 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2809 struct dt_object *dt,
2810 struct lustre_capa *capa)
2812 struct osd_it_iam *it;
2813 struct osd_thread_info *oti = osd_oti_get(env);
2814 struct osd_object *obj = osd_dt_obj(dt);
2815 struct lu_object *lo = &dt->do_lu;
2816 struct iam_path_descr *ipd;
2817 struct iam_container *bag = &obj->oo_dir->od_container;
2819 LASSERT(lu_object_exists(lo));
2821 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2822 return ERR_PTR(-EACCES);
2825 ipd = osd_it_ipd_get(env, bag);
2826 if (likely(ipd != NULL)) {
2830 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2831 return (struct dt_it *)it;
2833 return ERR_PTR(-ENOMEM);
2837 * free given Iterator.
2840 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2842 struct osd_it_iam *it = (struct osd_it_iam *)di;
2843 struct osd_object *obj = it->oi_obj;
2845 iam_it_fini(&it->oi_it);
2846 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2847 lu_object_put(env, &obj->oo_dt.do_lu);
2851 * Move Iterator to record specified by \a key
2853 * \param di osd iterator
2854 * \param key key for index
2856 * \retval +ve di points to record with least key not larger than key
2857 * \retval 0 di points to exact matched key
2858 * \retval -ve failure
2861 static int osd_it_iam_get(const struct lu_env *env,
2862 struct dt_it *di, const struct dt_key *key)
2864 struct osd_it_iam *it = (struct osd_it_iam *)di;
2866 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2872 * \param di osd iterator
2875 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2877 struct osd_it_iam *it = (struct osd_it_iam *)di;
2879 iam_it_put(&it->oi_it);
2883 * Move iterator by one record
2885 * \param di osd iterator
2887 * \retval +1 end of container reached
2889 * \retval -ve failure
2892 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2894 struct osd_it_iam *it = (struct osd_it_iam *)di;
2896 return iam_it_next(&it->oi_it);
2900 * Return pointer to the key under iterator.
2903 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2904 const struct dt_it *di)
2906 struct osd_it_iam *it = (struct osd_it_iam *)di;
2908 return (struct dt_key *)iam_it_key_get(&it->oi_it);
2912 * Return size of key under iterator (in bytes)
2915 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
2917 struct osd_it_iam *it = (struct osd_it_iam *)di;
2919 return iam_it_key_size(&it->oi_it);
2923 * Return pointer to the record under iterator.
2925 static struct dt_rec *osd_it_iam_rec(const struct lu_env *env,
2926 const struct dt_it *di)
2928 struct osd_it_iam *it = (struct osd_it_iam *)di;
2930 return (struct dt_rec *)iam_it_rec_get(&it->oi_it);
2934 * Returns cookie for current Iterator position.
2936 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
2938 struct osd_it_iam *it = (struct osd_it_iam *)di;
2940 return iam_it_store(&it->oi_it);
2944 * Restore iterator from cookie.
2946 * \param di osd iterator
2947 * \param hash Iterator location cookie
2949 * \retval +ve di points to record with least key not larger than key.
2950 * \retval 0 di points to exact matched key
2951 * \retval -ve failure
2954 static int osd_it_iam_load(const struct lu_env *env,
2955 const struct dt_it *di, __u64 hash)
2957 struct osd_it_iam *it = (struct osd_it_iam *)di;
2959 return iam_it_load(&it->oi_it, hash);
2962 static const struct dt_index_operations osd_index_iam_ops = {
2963 .dio_lookup = osd_index_iam_lookup,
2964 .dio_insert = osd_index_iam_insert,
2965 .dio_delete = osd_index_iam_delete,
2967 .init = osd_it_iam_init,
2968 .fini = osd_it_iam_fini,
2969 .get = osd_it_iam_get,
2970 .put = osd_it_iam_put,
2971 .next = osd_it_iam_next,
2972 .key = osd_it_iam_key,
2973 .key_size = osd_it_iam_key_size,
2974 .rec = osd_it_iam_rec,
2975 .store = osd_it_iam_store,
2976 .load = osd_it_iam_load
2981 * Creates or initializes iterator context.
2983 * \retval struct osd_it_ea, iterator structure on success
2986 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
2987 struct dt_object *dt,
2988 struct lustre_capa *capa)
2990 struct osd_object *obj = osd_dt_obj(dt);
2991 struct osd_thread_info *info = osd_oti_get(env);
2992 struct osd_it_ea *it = &info->oti_it_ea;
2993 struct lu_object *lo = &dt->do_lu;
2994 struct dentry *obj_dentry = &info->oti_it_dentry;
2996 LASSERT(lu_object_exists(lo));
2998 obj_dentry->d_inode = obj->oo_inode;
2999 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3000 obj_dentry->d_name.hash = 0;
3002 it->oie_namelen = 0;
3003 it->oie_curr_pos = 0;
3004 it->oie_next_pos = 0;
3006 it->oie_file.f_dentry = obj_dentry;
3007 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3008 it->oie_file.f_op = obj->oo_inode->i_fop;
3009 it->oie_file.private_data = NULL;
3012 RETURN((struct dt_it*) it);
3016 * Destroy or finishes iterator context.
3018 * \param di, struct osd_it_ea, iterator structure to be destroyed
3020 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3022 struct osd_it_ea *it = (struct osd_it_ea *)di;
3023 struct osd_object *obj = it->oie_obj;
3027 lu_object_put(env, &obj->oo_dt.do_lu);
3032 * It position the iterator at given key, so that next lookup continues from
3033 * that key Or it is similar to dio_it->load() but based on a key,
3034 * rather than file position.
3036 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3039 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3041 static int osd_it_ea_get(const struct lu_env *env,
3042 struct dt_it *di, const struct dt_key *key)
3044 struct osd_it_ea *it = (struct osd_it_ea *)di;
3047 LASSERT(((const char *)key)[0] == '\0');
3048 it->oie_namelen = 0;
3049 it->oie_curr_pos = 0;
3050 it->oie_next_pos = 0;
3058 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3063 * It is called internally by ->readdir(). It fills the
3064 * iterator's in-memory data structure with required
3065 * information i.e. name, namelen, rec_size etc.
3067 * \param buf, in which information to be filled in.
3068 * \param name, name of the file in given dir
3070 * \retval 0, on success
3071 * \retval 1, on buffer full
3073 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3074 loff_t offset, ino_t ino,
3075 unsigned int d_type)
3077 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3078 struct dirent64 *dirent = &it->oie_dirent64;
3079 int reclen = LDISKFS_DIR_REC_LEN(namelen);
3083 if (it->oie_namelen)
3086 if (namelen == 0 || namelen > LDISKFS_NAME_LEN)
3089 strncpy(dirent->d_name, name, LDISKFS_NAME_LEN);
3090 dirent->d_name[namelen] = 0;
3091 dirent->d_ino = ino;
3092 dirent->d_off = offset;
3093 dirent->d_reclen = reclen;
3094 it->oie_namelen = namelen;
3095 it->oie_curr_pos = offset;
3101 * Calls ->readdir() to load a directory entry at a time
3102 * and stored it in iterator's in-memory data structure.
3104 * \param di, struct osd_it_ea, iterator's in memory structure
3106 * \retval 0, on success
3107 * \retval -ve, on error
3109 int osd_ldiskfs_it_fill(const struct dt_it *di)
3111 struct osd_it_ea *it = (struct osd_it_ea *)di;
3112 struct osd_object *obj = it->oie_obj;
3113 struct inode *inode = obj->oo_inode;
3117 it->oie_namelen = 0;
3118 it->oie_file.f_pos = it->oie_curr_pos;
3120 result = inode->i_fop->readdir(&it->oie_file, it,
3121 (filldir_t) osd_ldiskfs_filldir);
3123 it->oie_next_pos = it->oie_file.f_pos;
3125 if(!result && it->oie_namelen == 0)
3132 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3133 * to load a directory entry at a time and stored it in
3134 * iterator's in-memory data structure.
3136 * \param di, struct osd_it_ea, iterator's in memory structure
3138 * \retval +ve, iterator reached to end
3139 * \retval 0, iterator not reached to end
3140 * \retval -ve, on error
3142 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3144 struct osd_it_ea *it = (struct osd_it_ea *)di;
3148 it->oie_curr_pos = it->oie_next_pos;
3150 if (it->oie_curr_pos == LDISKFS_HTREE_EOF)
3153 rc = osd_ldiskfs_it_fill(di);
3159 * Returns the key at current position from iterator's in memory structure.
3161 * \param di, struct osd_it_ea, iterator's in memory structure
3163 * \retval key i.e. struct dt_key on success
3165 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3166 const struct dt_it *di)
3168 struct osd_it_ea *it = (struct osd_it_ea *)di;
3170 RETURN((struct dt_key *)it->oie_dirent64.d_name);
3174 * Returns the key's size at current position from iterator's in memory structure.
3176 * \param di, struct osd_it_ea, iterator's in memory structure
3178 * \retval key_size i.e. struct dt_key on success
3180 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3182 struct osd_it_ea *it = (struct osd_it_ea *)di;
3184 RETURN(it->oie_namelen);
3188 * Returns the value (i.e. fid/igif) at current position from iterator's
3189 * in memory structure.
3191 * \param di, struct osd_it_ea, iterator's in memory structure
3193 * \retval value i.e. struct dt_rec on success
3195 static struct dt_rec *osd_it_ea_rec(const struct lu_env *env,
3196 const struct dt_it *di)
3198 struct osd_it_ea *it = (struct osd_it_ea *)di;
3199 struct osd_object *obj = it->oie_obj;
3200 struct osd_thread_info *info = osd_oti_get(env);
3201 struct osd_inode_id *id = &info->oti_id;
3202 struct lu_fid_pack *rec = &info->oti_pack;
3203 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3204 struct dentry *dentry = &info->oti_child_dentry;
3205 struct osd_device *dev;
3206 struct inode *inode;
3210 dev = osd_dev(ldev);
3211 id->oii_ino = it->oie_dirent64.d_ino;
3212 id->oii_gen = OSD_OII_NOGEN;
3213 inode = osd_iget(info, dev, id);
3214 if (!IS_ERR(inode)) {
3215 dentry->d_inode = inode;
3216 LASSERT(dentry->d_inode->i_sb == osd_sb(dev));
3218 CERROR("Error getting inode for ino =%d", id->oii_ino);
3219 RETURN((struct dt_rec *) PTR_ERR(inode));
3222 rc = osd_ea_fid_get(env, dentry, (struct dt_rec*) rec);
3225 RETURN((struct dt_rec *)rec);
3230 * Returns a cookie for current position of the iterator head, so that
3231 * user can use this cookie to load/start the iterator next time.
3233 * \param di, struct osd_it_ea, iterator's in memory structure
3235 * \retval cookie for current position, on success
3237 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3239 struct osd_it_ea *it = (struct osd_it_ea *)di;
3241 RETURN(it->oie_curr_pos);
3245 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3246 * to load a directory entry at a time and stored it i inn,
3247 * in iterator's in-memory data structure.
3249 * \param di, struct osd_it_ea, iterator's in memory structure
3251 * \retval +ve, on success
3252 * \retval -ve, on error
3254 static int osd_it_ea_load(const struct lu_env *env,
3255 const struct dt_it *di, __u64 hash)
3257 struct osd_it_ea *it = (struct osd_it_ea *)di;
3261 it->oie_curr_pos = it->oie_next_pos = hash;
3263 rc = osd_ldiskfs_it_fill(di);
3270 * Index and Iterator operations for interoperability
3271 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3273 static const struct dt_index_operations osd_index_ea_ops = {
3274 .dio_lookup = osd_index_ea_lookup,
3275 .dio_insert = osd_index_ea_insert,
3276 .dio_delete = osd_index_ea_delete,
3278 .init = osd_it_ea_init,
3279 .fini = osd_it_ea_fini,
3280 .get = osd_it_ea_get,
3281 .put = osd_it_ea_put,
3282 .next = osd_it_ea_next,
3283 .key = osd_it_ea_key,
3284 .key_size = osd_it_ea_key_size,
3285 .rec = osd_it_ea_rec,
3286 .store = osd_it_ea_store,
3287 .load = osd_it_ea_load
3292 * Index lookup function for interoperability mode (b11826).
3294 * \param key, key i.e. file name to be searched
3296 * \retval +ve, on success
3297 * \retval -ve, on error
3299 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3300 struct dt_rec *rec, const struct dt_key *key,
3301 struct lustre_capa *capa)
3303 struct osd_object *obj = osd_dt_obj(dt);
3308 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3309 LINVRNT(osd_invariant(obj));
3311 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3314 rc = osd_ea_lookup_rec(env, obj, rec, key);
3321 /* type constructor/destructor: osd_type_init, osd_type_fini */
3322 LU_TYPE_INIT_FINI(osd, &osd_key);
3324 static struct lu_context_key osd_key = {
3325 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3326 .lct_init = osd_key_init,
3327 .lct_fini = osd_key_fini,
3328 .lct_exit = osd_key_exit
3331 static void *osd_key_init(const struct lu_context *ctx,
3332 struct lu_context_key *key)
3334 struct osd_thread_info *info;
3336 OBD_ALLOC_PTR(info);
3338 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
3340 info = ERR_PTR(-ENOMEM);
3344 /* context key destructor: osd_key_fini */
3345 LU_KEY_FINI(osd, struct osd_thread_info);
3347 static void osd_key_exit(const struct lu_context *ctx,
3348 struct lu_context_key *key, void *data)
3350 struct osd_thread_info *info = data;
3352 LASSERT(info->oti_r_locks == 0);
3353 LASSERT(info->oti_w_locks == 0);
3354 LASSERT(info->oti_txns == 0);
3357 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3358 const char *name, struct lu_device *next)
3361 struct lu_context *ctx;
3363 /* context for commit hooks */
3364 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3365 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3367 rc = osd_procfs_init(osd_dev(d), name);
3368 ctx->lc_cookie = 0x3;
3373 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3375 struct osd_thread_info *info = osd_oti_get(env);
3377 if (o->od_obj_area != NULL) {
3378 lu_object_put(env, &o->od_obj_area->do_lu);
3379 o->od_obj_area = NULL;
3381 osd_oi_fini(info, &o->od_oi);
3386 static int osd_mount(const struct lu_env *env,
3387 struct osd_device *o, struct lustre_cfg *cfg)
3389 struct lustre_mount_info *lmi;
3390 const char *dev = lustre_cfg_string(cfg, 0);
3391 struct lustre_disk_data *ldd;
3392 struct lustre_sb_info *lsi;
3396 if (o->od_mount != NULL) {
3397 CERROR("Already mounted (%s)\n", dev);
3402 lmi = server_get_mount(dev);
3404 CERROR("Cannot get mount info for %s!\n", dev);
3408 LASSERT(lmi != NULL);
3409 /* save lustre_mount_info in dt_device */
3412 lsi = s2lsi(lmi->lmi_sb);
3415 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3417 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3421 o->od_obj_area = NULL;
3425 static struct lu_device *osd_device_fini(const struct lu_env *env,
3426 struct lu_device *d)
3431 shrink_dcache_sb(osd_sb(osd_dev(d)));
3432 osd_sync(env, lu2dt_dev(d));
3434 rc = osd_procfs_fini(osd_dev(d));
3436 CERROR("proc fini error %d \n", rc);
3437 RETURN (ERR_PTR(rc));
3440 if (osd_dev(d)->od_mount)
3441 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3442 osd_dev(d)->od_mount->lmi_mnt);
3443 osd_dev(d)->od_mount = NULL;
3445 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3449 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3450 struct lu_device_type *t,
3451 struct lustre_cfg *cfg)
3453 struct lu_device *l;
3454 struct osd_device *o;
3460 result = dt_device_init(&o->od_dt_dev, t);
3463 l->ld_ops = &osd_lu_ops;
3464 o->od_dt_dev.dd_ops = &osd_dt_ops;
3465 spin_lock_init(&o->od_osfs_lock);
3466 o->od_osfs_age = cfs_time_shift_64(-1000);
3467 o->od_capa_hash = init_capa_hash();
3468 if (o->od_capa_hash == NULL) {
3469 dt_device_fini(&o->od_dt_dev);
3470 l = ERR_PTR(-ENOMEM);
3473 l = ERR_PTR(result);
3478 l = ERR_PTR(-ENOMEM);
3482 static struct lu_device *osd_device_free(const struct lu_env *env,
3483 struct lu_device *d)
3485 struct osd_device *o = osd_dev(d);
3488 cleanup_capa_hash(o->od_capa_hash);
3489 dt_device_fini(&o->od_dt_dev);
3494 static int osd_process_config(const struct lu_env *env,
3495 struct lu_device *d, struct lustre_cfg *cfg)
3497 struct osd_device *o = osd_dev(d);
3501 switch(cfg->lcfg_command) {
3503 err = osd_mount(env, o, cfg);
3506 err = osd_shutdown(env, o);
3515 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
3516 struct ldiskfs_super_block * es);
3518 static int osd_recovery_complete(const struct lu_env *env,
3519 struct lu_device *d)
3521 struct osd_device *o = osd_dev(d);
3523 /* TODO: orphans handling */
3524 ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3528 static int osd_prepare(const struct lu_env *env,
3529 struct lu_device *pdev,
3530 struct lu_device *dev)
3532 struct osd_device *osd = osd_dev(dev);
3533 struct lustre_sb_info *lsi;
3534 struct lustre_disk_data *ldd;
3535 struct lustre_mount_info *lmi;
3536 struct osd_thread_info *oti = osd_oti_get(env);
3537 struct dt_object *d;
3541 /* 1. initialize oi before any file create or file open */
3542 result = osd_oi_init(oti, &osd->od_oi,
3543 &osd->od_dt_dev, lu2md_dev(pdev));
3547 lmi = osd->od_mount;
3548 lsi = s2lsi(lmi->lmi_sb);
3551 /* 2. setup local objects */
3552 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3556 /* 3. open remote object dir */
3557 d = dt_store_open(env, lu2dt_dev(dev), "",
3558 remote_obj_dir, &oti->oti_fid);
3560 osd->od_obj_area = d;
3563 result = PTR_ERR(d);
3564 osd->od_obj_area = NULL;
3571 static struct inode *osd_iget(struct osd_thread_info *info,
3572 struct osd_device *dev,
3573 const struct osd_inode_id *id)
3575 struct inode *inode;
3577 inode = iget(osd_sb(dev), id->oii_ino);
3578 if (inode == NULL) {
3579 CERROR("no inode\n");
3580 inode = ERR_PTR(-EACCES);
3581 } else if (is_bad_inode(inode)) {
3582 CERROR("bad inode\n");
3584 inode = ERR_PTR(-ENOENT);
3585 } else if (id->oii_gen != OSD_OII_NOGEN &&
3586 inode->i_generation != id->oii_gen) {
3587 CERROR("stale inode\n");
3589 inode = ERR_PTR(-ESTALE);
3596 static int osd_fid_lookup(const struct lu_env *env,
3597 struct osd_object *obj, const struct lu_fid *fid)
3599 struct osd_thread_info *info;
3600 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3601 struct osd_device *dev;
3602 struct osd_inode_id *id;
3604 struct inode *inode;
3607 LINVRNT(osd_invariant(obj));
3608 LASSERT(obj->oo_inode == NULL);
3609 LASSERT(fid_is_sane(fid));
3611 * This assertion checks that osd layer sees only local
3612 * fids. Unfortunately it is somewhat expensive (does a
3613 * cache-lookup). Disabling it for production/acceptance-testing.
3615 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
3619 info = osd_oti_get(env);
3620 dev = osd_dev(ldev);
3624 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
3627 result = osd_oi_lookup(info, oi, fid, id);
3629 inode = osd_iget(info, dev, id);
3630 if (!IS_ERR(inode)) {
3631 obj->oo_inode = inode;
3632 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
3633 if (dev->od_iop_mode) {
3634 obj->oo_compat_dot_created = 1;
3635 obj->oo_compat_dotdot_created = 1;
3640 * If fid wasn't found in oi, inode-less object is
3641 * created, for which lu_object_exists() returns
3642 * false. This is used in a (frequent) case when
3643 * objects are created as locking anchors or
3644 * place holders for objects yet to be created.
3646 result = PTR_ERR(inode);
3647 } else if (result == -ENOENT)
3649 LINVRNT(osd_invariant(obj));
3654 static void osd_inode_getattr(const struct lu_env *env,
3655 struct inode *inode, struct lu_attr *attr)
3657 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
3658 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
3659 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
3661 attr->la_atime = LTIME_S(inode->i_atime);
3662 attr->la_mtime = LTIME_S(inode->i_mtime);
3663 attr->la_ctime = LTIME_S(inode->i_ctime);
3664 attr->la_mode = inode->i_mode;
3665 attr->la_size = i_size_read(inode);
3666 attr->la_blocks = inode->i_blocks;
3667 attr->la_uid = inode->i_uid;
3668 attr->la_gid = inode->i_gid;
3669 attr->la_flags = LDISKFS_I(inode)->i_flags;
3670 attr->la_nlink = inode->i_nlink;
3671 attr->la_rdev = inode->i_rdev;
3672 attr->la_blksize = ll_inode_blksize(inode);
3673 attr->la_blkbits = inode->i_blkbits;
3680 static int lu_device_is_osd(const struct lu_device *d)
3682 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
3685 static struct osd_object *osd_obj(const struct lu_object *o)
3687 LASSERT(lu_device_is_osd(o->lo_dev));
3688 return container_of0(o, struct osd_object, oo_dt.do_lu);
3691 static struct osd_device *osd_dt_dev(const struct dt_device *d)
3693 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
3694 return container_of0(d, struct osd_device, od_dt_dev);
3697 static struct osd_device *osd_dev(const struct lu_device *d)
3699 LASSERT(lu_device_is_osd(d));
3700 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
3703 static struct osd_object *osd_dt_obj(const struct dt_object *d)
3705 return osd_obj(&d->do_lu);
3708 static struct osd_device *osd_obj2dev(const struct osd_object *o)
3710 return osd_dev(o->oo_dt.do_lu.lo_dev);
3713 static struct lu_device *osd2lu_dev(struct osd_device *osd)
3715 return &osd->od_dt_dev.dd_lu_dev;
3718 static struct super_block *osd_sb(const struct osd_device *dev)
3720 return dev->od_mount->lmi_mnt->mnt_sb;
3723 static journal_t *osd_journal(const struct osd_device *dev)
3725 return LDISKFS_SB(osd_sb(dev))->s_journal;
3728 static int osd_has_index(const struct osd_object *obj)
3730 return obj->oo_dt.do_index_ops != NULL;
3733 static int osd_object_invariant(const struct lu_object *l)
3735 return osd_invariant(osd_obj(l));
3738 static const struct lu_object_operations osd_lu_obj_ops = {
3739 .loo_object_init = osd_object_init,
3740 .loo_object_delete = osd_object_delete,
3741 .loo_object_release = osd_object_release,
3742 .loo_object_free = osd_object_free,
3743 .loo_object_print = osd_object_print,
3744 .loo_object_invariant = osd_object_invariant
3747 static const struct lu_device_operations osd_lu_ops = {
3748 .ldo_object_alloc = osd_object_alloc,
3749 .ldo_process_config = osd_process_config,
3750 .ldo_recovery_complete = osd_recovery_complete,
3751 .ldo_prepare = osd_prepare,
3754 static const struct lu_device_type_operations osd_device_type_ops = {
3755 .ldto_init = osd_type_init,
3756 .ldto_fini = osd_type_fini,
3758 .ldto_start = osd_type_start,
3759 .ldto_stop = osd_type_stop,
3761 .ldto_device_alloc = osd_device_alloc,
3762 .ldto_device_free = osd_device_free,
3764 .ldto_device_init = osd_device_init,
3765 .ldto_device_fini = osd_device_fini
3768 static struct lu_device_type osd_device_type = {
3769 .ldt_tags = LU_DEVICE_DT,
3770 .ldt_name = LUSTRE_OSD_NAME,
3771 .ldt_ops = &osd_device_type_ops,
3772 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3776 * lprocfs legacy support.
3778 static struct obd_ops osd_obd_device_ops = {
3779 .o_owner = THIS_MODULE
3782 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3783 .llod_name = remote_obj_dir,
3784 .llod_oid = OSD_REM_OBJ_DIR_OID,
3786 .llod_feat = &dt_directory_features,
3789 static int __init osd_mod_init(void)
3791 struct lprocfs_static_vars lvars;
3794 llo_local_obj_register(&llod_osd_rem_obj_dir);
3795 lprocfs_osd_init_vars(&lvars);
3796 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3797 LUSTRE_OSD_NAME, &osd_device_type);
3800 static void __exit osd_mod_exit(void)
3802 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3803 class_unregister_type(LUSTRE_OSD_NAME);
3806 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3807 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3808 MODULE_LICENSE("GPL");
3810 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);