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;
98 struct dt_object oo_dt;
100 * Inode for file system object represented by this osd_object. This
101 * inode is pinned for the whole duration of lu_object life.
103 * Not modified concurrently (either setup early during object
104 * creation, or assigned by osd_object_create() under write lock).
106 struct inode *oo_inode;
108 * to protect index ops.
110 struct rw_semaphore oo_ext_idx_sem;
111 struct rw_semaphore oo_sem;
112 struct osd_directory *oo_dir;
113 /** protects inode attributes. */
116 * Following two members are used to indicate the presence of dot and
117 * dotdot in the given directory. This is required for interop mode
120 int oo_compat_dot_created;
121 int oo_compat_dotdot_created;
123 const struct lu_env *oo_owner;
124 #ifdef CONFIG_LOCKDEP
125 struct lockdep_map oo_dep_map;
129 static int osd_root_get (const struct lu_env *env,
130 struct dt_device *dev, struct lu_fid *f);
132 static int lu_device_is_osd (const struct lu_device *d);
133 static void osd_mod_exit (void) __exit;
134 static int osd_mod_init (void) __init;
135 static int osd_type_init (struct lu_device_type *t);
136 static void osd_type_fini (struct lu_device_type *t);
137 static int osd_object_init (const struct lu_env *env,
139 const struct lu_object_conf *_);
140 static void osd_object_release(const struct lu_env *env,
141 struct lu_object *l);
142 static int osd_object_print (const struct lu_env *env, void *cookie,
143 lu_printer_t p, const struct lu_object *o);
144 static struct lu_device *osd_device_free (const struct lu_env *env,
145 struct lu_device *m);
146 static void *osd_key_init (const struct lu_context *ctx,
147 struct lu_context_key *key);
148 static void osd_key_fini (const struct lu_context *ctx,
149 struct lu_context_key *key, void *data);
150 static void osd_key_exit (const struct lu_context *ctx,
151 struct lu_context_key *key, void *data);
152 static int osd_has_index (const struct osd_object *obj);
153 static void osd_object_init0 (struct osd_object *obj);
154 static int osd_device_init (const struct lu_env *env,
155 struct lu_device *d, const char *,
157 static int osd_fid_lookup (const struct lu_env *env,
158 struct osd_object *obj,
159 const struct lu_fid *fid);
160 static void osd_inode_getattr (const struct lu_env *env,
161 struct inode *inode, struct lu_attr *attr);
162 static int osd_inode_setattr (const struct lu_env *env,
163 struct inode *inode, const struct lu_attr *attr);
164 static int osd_param_is_sane (const struct osd_device *dev,
165 const struct txn_param *param);
166 static int osd_index_iam_lookup(const struct lu_env *env,
167 struct dt_object *dt,
168 struct dt_rec *rec, const struct dt_key *key,
169 struct lustre_capa *capa);
170 static int osd_index_ea_lookup(const struct lu_env *env,
171 struct dt_object *dt,
172 struct dt_rec *rec, const struct dt_key *key,
173 struct lustre_capa *capa);
174 static int osd_index_iam_insert(const struct lu_env *env,
175 struct dt_object *dt,
176 const struct dt_rec *rec,
177 const struct dt_key *key,
178 struct thandle *handle,
179 struct lustre_capa *capa,
181 static int osd_index_ea_insert (const struct lu_env *env,
182 struct dt_object *dt,
183 const struct dt_rec *rec,
184 const struct dt_key *key,
185 struct thandle *handle,
186 struct lustre_capa *capa,
188 static int osd_index_iam_delete(const struct lu_env *env,
189 struct dt_object *dt, const struct dt_key *key,
190 struct thandle *handle,
191 struct lustre_capa *capa);
192 static int osd_index_ea_delete (const struct lu_env *env,
193 struct dt_object *dt, const struct dt_key *key,
194 struct thandle *handle,
195 struct lustre_capa *capa);
197 static int osd_iam_index_probe (const struct lu_env *env,
198 struct osd_object *o,
199 const struct dt_index_features *feat);
200 static int osd_index_try (const struct lu_env *env,
201 struct dt_object *dt,
202 const struct dt_index_features *feat);
203 static void osd_index_fini (struct osd_object *o);
205 static void osd_it_iam_fini (const struct lu_env *env, struct dt_it *di);
206 static int osd_it_iam_get (const struct lu_env *env,
207 struct dt_it *di, const struct dt_key *key);
208 static void osd_it_iam_put (const struct lu_env *env, struct dt_it *di);
209 static int osd_it_iam_next (const struct lu_env *env, struct dt_it *di);
210 static int osd_it_iam_key_size (const struct lu_env *env,
211 const struct dt_it *di);
212 static void osd_it_ea_fini (const struct lu_env *env, struct dt_it *di);
213 static int osd_it_ea_get (const struct lu_env *env,
214 struct dt_it *di, const struct dt_key *key);
215 static void osd_it_ea_put (const struct lu_env *env, struct dt_it *di);
216 static int osd_it_ea_next (const struct lu_env *env, struct dt_it *di);
217 static int osd_it_ea_key_size(const struct lu_env *env,
218 const struct dt_it *di);
220 static void osd_conf_get (const struct lu_env *env,
221 const struct dt_device *dev,
222 struct dt_device_param *param);
223 static void osd_trans_stop (const struct lu_env *env,
225 static int osd_object_is_root(const struct osd_object *obj);
227 static struct osd_object *osd_obj (const struct lu_object *o);
228 static struct osd_device *osd_dev (const struct lu_device *d);
229 static struct osd_device *osd_dt_dev (const struct dt_device *d);
230 static struct osd_object *osd_dt_obj (const struct dt_object *d);
231 static struct osd_device *osd_obj2dev (const struct osd_object *o);
232 static struct lu_device *osd2lu_dev (struct osd_device *osd);
233 static struct lu_device *osd_device_fini (const struct lu_env *env,
234 struct lu_device *d);
235 static struct lu_device *osd_device_alloc (const struct lu_env *env,
236 struct lu_device_type *t,
237 struct lustre_cfg *cfg);
238 static struct lu_object *osd_object_alloc (const struct lu_env *env,
239 const struct lu_object_header *hdr,
240 struct lu_device *d);
241 static struct inode *osd_iget (struct osd_thread_info *info,
242 struct osd_device *dev,
243 const struct osd_inode_id *id);
244 static struct super_block *osd_sb (const struct osd_device *dev);
245 static struct dt_it *osd_it_iam_init (const struct lu_env *env,
246 struct dt_object *dt,
247 struct lustre_capa *capa);
248 static struct dt_key *osd_it_iam_key (const struct lu_env *env,
249 const struct dt_it *di);
250 static struct dt_rec *osd_it_iam_rec (const struct lu_env *env,
251 const struct dt_it *di);
252 static struct dt_it *osd_it_ea_init (const struct lu_env *env,
253 struct dt_object *dt,
254 struct lustre_capa *capa);
255 static struct dt_key *osd_it_ea_key (const struct lu_env *env,
256 const struct dt_it *di);
257 static struct dt_rec *osd_it_ea_rec (const struct lu_env *env,
258 const struct dt_it *di);
260 static struct timespec *osd_inode_time (const struct lu_env *env,
263 static struct thandle *osd_trans_start (const struct lu_env *env,
265 struct txn_param *p);
266 static journal_t *osd_journal (const struct osd_device *dev);
268 static int __osd_ea_add_rec(struct osd_thread_info *info,
269 struct osd_object *pobj,
270 struct osd_object *cobj,
274 static const struct lu_device_type_operations osd_device_type_ops;
275 static struct lu_device_type osd_device_type;
276 static const struct lu_object_operations osd_lu_obj_ops;
277 static struct obd_ops osd_obd_device_ops;
278 static const struct lu_device_operations osd_lu_ops;
279 static struct lu_context_key osd_key;
280 static const struct dt_object_operations osd_obj_ops;
281 static const struct dt_object_operations osd_obj_ea_ops;
282 static const struct dt_body_operations osd_body_ops;
283 static const struct dt_index_operations osd_index_iam_ops;
284 static const struct dt_index_operations osd_index_ea_ops;
287 struct thandle ot_super;
289 struct journal_callback ot_jcb;
290 /* Link to the device, for debugging. */
291 struct lu_ref_link *ot_dev_link;
295 #ifdef HAVE_QUOTA_SUPPORT
297 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
299 struct md_ucred *uc = md_ucred(env);
303 save->oc_uid = current->fsuid;
304 save->oc_gid = current->fsgid;
305 save->oc_cap = current->cap_effective;
306 current->fsuid = uc->mu_fsuid;
307 current->fsgid = uc->mu_fsgid;
308 current->cap_effective = uc->mu_cap;
312 osd_pop_ctxt(struct osd_ctxt *save)
314 current->fsuid = save->oc_uid;
315 current->fsgid = save->oc_gid;
316 current->cap_effective = save->oc_cap;
321 * Invariants, assertions.
325 * XXX: do not enable this, until invariant checking code is made thread safe
326 * in the face of pdirops locking.
328 #define OSD_INVARIANT_CHECKS (0)
330 #if OSD_INVARIANT_CHECKS
331 static int osd_invariant(const struct osd_object *obj)
335 ergo(obj->oo_inode != NULL,
336 obj->oo_inode->i_sb == osd_sb(osd_obj2dev(obj)) &&
337 atomic_read(&obj->oo_inode->i_count) > 0) &&
338 ergo(obj->oo_dir != NULL &&
339 obj->oo_dir->od_conationer.ic_object != NULL,
340 obj->oo_dir->od_conationer.ic_object == obj->oo_inode);
343 #define osd_invariant(obj) (1)
346 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
348 return lu_context_key_get(&env->le_ctx, &osd_key);
352 * Concurrency: doesn't matter
354 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
356 return osd_oti_get(env)->oti_r_locks > 0;
360 * Concurrency: doesn't matter
362 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
364 struct osd_thread_info *oti = osd_oti_get(env);
365 return oti->oti_w_locks > 0 && o->oo_owner == env;
369 * Concurrency: doesn't access mutable data
371 static int osd_root_get(const struct lu_env *env,
372 struct dt_device *dev, struct lu_fid *f)
376 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
377 lu_igif_build(f, inode->i_ino, inode->i_generation);
382 * OSD object methods.
386 * Concurrency: no concurrent access is possible that early in object
389 static struct lu_object *osd_object_alloc(const struct lu_env *env,
390 const struct lu_object_header *hdr,
393 struct osd_object *mo;
399 l = &mo->oo_dt.do_lu;
400 dt_object_init(&mo->oo_dt, NULL, d);
401 if (osd_dev(d)->od_iop_mode)
402 mo->oo_dt.do_ops = &osd_obj_ea_ops;
404 mo->oo_dt.do_ops = &osd_obj_ops;
406 l->lo_ops = &osd_lu_obj_ops;
407 init_rwsem(&mo->oo_sem);
408 init_rwsem(&mo->oo_ext_idx_sem);
409 spin_lock_init(&mo->oo_guard);
416 * Concurrency: shouldn't matter.
418 static void osd_object_init0(struct osd_object *obj)
420 LASSERT(obj->oo_inode != NULL);
421 obj->oo_dt.do_body_ops = &osd_body_ops;
422 obj->oo_dt.do_lu.lo_header->loh_attr |=
423 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
427 * Concurrency: no concurrent access is possible that early in object
430 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
431 const struct lu_object_conf *_)
433 struct osd_object *obj = osd_obj(l);
436 LINVRNT(osd_invariant(obj));
438 result = osd_fid_lookup(env, obj, lu_object_fid(l));
440 if (obj->oo_inode != NULL)
441 osd_object_init0(obj);
443 LINVRNT(osd_invariant(obj));
448 * Concurrency: no concurrent access is possible that late in object
451 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
453 struct osd_object *obj = osd_obj(l);
455 LINVRNT(osd_invariant(obj));
457 dt_object_fini(&obj->oo_dt);
461 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
462 const struct iam_container *bag)
464 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
465 osd_oti_get(env)->oti_it_ipd);
468 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
469 const struct iam_container *bag)
471 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
472 osd_oti_get(env)->oti_idx_ipd);
475 static void osd_ipd_put(const struct lu_env *env,
476 const struct iam_container *bag,
477 struct iam_path_descr *ipd)
479 bag->ic_descr->id_ops->id_ipd_free(ipd);
483 * Concurrency: no concurrent access is possible that late in object
486 static void osd_index_fini(struct osd_object *o)
488 struct iam_container *bag;
490 if (o->oo_dir != NULL) {
491 bag = &o->oo_dir->od_container;
492 if (o->oo_inode != NULL) {
493 if (bag->ic_object == o->oo_inode)
494 iam_container_fini(bag);
496 OBD_FREE_PTR(o->oo_dir);
502 * Concurrency: no concurrent access is possible that late in object
503 * life-cycle (for all existing callers, that is. New callers have to provide
504 * their own locking.)
506 static int osd_inode_unlinked(const struct inode *inode)
508 return inode->i_nlink == 0;
512 OSD_TXN_OI_DELETE_CREDITS = 20,
513 OSD_TXN_INODE_DELETE_CREDITS = 20
517 * Concurrency: no concurrent access is possible that late in object
520 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
522 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
523 struct osd_device *osd = osd_obj2dev(obj);
524 struct osd_thread_info *oti = osd_oti_get(env);
525 struct txn_param *prm = &oti->oti_txn;
526 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
530 lu_env_init(env_del_obj, LCT_DT_THREAD);
531 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
532 OSD_TXN_INODE_DELETE_CREDITS);
533 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
535 result = osd_oi_delete(osd_oti_get(env_del_obj),
536 &osd->od_oi, fid, th);
537 osd_trans_stop(env_del_obj, th);
539 result = PTR_ERR(th);
541 lu_env_fini(env_del_obj);
546 * Called just before object is freed. Releases all resources except for
547 * object itself (that is released by osd_object_free()).
549 * Concurrency: no concurrent access is possible that late in object
552 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
554 struct osd_object *obj = osd_obj(l);
555 struct inode *inode = obj->oo_inode;
557 LINVRNT(osd_invariant(obj));
560 * If object is unlinked remove fid->ino mapping from object index.
567 if (osd_inode_unlinked(inode)) {
568 result = osd_inode_remove(env, obj);
570 LU_OBJECT_DEBUG(D_ERROR, env, l,
571 "Failed to cleanup: %d\n",
576 obj->oo_inode = NULL;
581 * Concurrency: ->loo_object_release() is called under site spin-lock.
583 static void osd_object_release(const struct lu_env *env,
586 struct osd_object *o = osd_obj(l);
588 LASSERT(!lu_object_is_dying(l->lo_header));
589 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
590 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
594 * Concurrency: shouldn't matter.
596 static int osd_object_print(const struct lu_env *env, void *cookie,
597 lu_printer_t p, const struct lu_object *l)
599 struct osd_object *o = osd_obj(l);
602 if (o->oo_dir != NULL)
603 d = o->oo_dir->od_container.ic_descr;
606 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
608 o->oo_inode ? o->oo_inode->i_ino : 0UL,
609 o->oo_inode ? o->oo_inode->i_generation : 0,
610 d ? d->id_ops->id_name : "plain");
614 * Concurrency: shouldn't matter.
616 int osd_statfs(const struct lu_env *env, struct dt_device *d,
619 struct osd_device *osd = osd_dt_dev(d);
620 struct super_block *sb = osd_sb(osd);
623 spin_lock(&osd->od_osfs_lock);
625 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
626 result = ll_do_statfs(sb, &osd->od_kstatfs);
627 if (likely(result == 0)) /* N.B. statfs can't really fail */
628 osd->od_osfs_age = cfs_time_current_64();
631 if (likely(result == 0))
632 *sfs = osd->od_kstatfs;
633 spin_unlock(&osd->od_osfs_lock);
639 * Concurrency: doesn't access mutable data.
641 static void osd_conf_get(const struct lu_env *env,
642 const struct dt_device *dev,
643 struct dt_device_param *param)
646 * XXX should be taken from not-yet-existing fs abstraction layer.
648 param->ddp_max_name_len = LDISKFS_NAME_LEN;
649 param->ddp_max_nlink = LDISKFS_LINK_MAX;
650 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
654 * Helper function to get and fill the buffer with input values.
656 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
660 buf = &osd_oti_get(env)->oti_buf;
671 * Concurrency: doesn't access mutable data.
673 static int osd_param_is_sane(const struct osd_device *dev,
674 const struct txn_param *param)
676 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
680 * Concurrency: shouldn't matter.
682 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
684 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
685 struct thandle *th = &oh->ot_super;
686 struct dt_device *dev = th->th_dev;
687 struct lu_device *lud = &dev->dd_lu_dev;
689 LASSERT(dev != NULL);
690 LASSERT(oh->ot_handle == NULL);
693 CERROR("transaction @0x%p commit error: %d\n", th, error);
695 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
697 * This od_env_for_commit is only for commit usage. see
700 lu_context_enter(&env->le_ctx);
701 dt_txn_hook_commit(env, th);
702 lu_context_exit(&env->le_ctx);
705 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
709 lu_context_exit(&th->th_ctx);
710 lu_context_fini(&th->th_ctx);
715 * Concurrency: shouldn't matter.
717 static struct thandle *osd_trans_start(const struct lu_env *env,
721 struct osd_device *dev = osd_dt_dev(d);
723 struct osd_thandle *oh;
729 hook_res = dt_txn_hook_start(env, d, p);
731 RETURN(ERR_PTR(hook_res));
733 if (osd_param_is_sane(dev, p)) {
734 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
736 struct osd_thread_info *oti = osd_oti_get(env);
739 * XXX temporary stuff. Some abstraction layer should
743 jh = journal_start(osd_journal(dev), p->tp_credits);
749 jh->h_sync = p->tp_sync;
750 lu_device_get(&d->dd_lu_dev);
751 oh->ot_dev_link = lu_ref_add
752 (&d->dd_lu_dev.ld_reference,
754 /* add commit callback */
755 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
756 lu_context_enter(&th->th_ctx);
757 journal_callback_set(jh, osd_trans_commit_cb,
758 (struct journal_callback *)&oh->ot_jcb);
759 LASSERT(oti->oti_txns == 0);
760 LASSERT(oti->oti_r_locks == 0);
761 LASSERT(oti->oti_w_locks == 0);
768 th = ERR_PTR(-ENOMEM);
770 CERROR("Invalid transaction parameters\n");
771 th = ERR_PTR(-EINVAL);
778 * Concurrency: shouldn't matter.
780 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
783 struct osd_thandle *oh;
784 struct osd_thread_info *oti = osd_oti_get(env);
788 oh = container_of0(th, struct osd_thandle, ot_super);
789 if (oh->ot_handle != NULL) {
790 handle_t *hdl = oh->ot_handle;
792 LASSERT(oti->oti_txns == 1);
794 LASSERT(oti->oti_r_locks == 0);
795 LASSERT(oti->oti_w_locks == 0);
796 result = dt_txn_hook_stop(env, th);
798 CERROR("Failure in transaction hook: %d\n", result);
799 oh->ot_handle = NULL;
800 result = journal_stop(hdl);
802 CERROR("Failure to stop transaction: %d\n", result);
808 * Concurrency: shouldn't matter.
810 static int osd_sync(const struct lu_env *env, struct dt_device *d)
812 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
813 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
817 * Start commit for OSD device.
819 * An implementation of dt_commit_async method for OSD device.
820 * Asychronously starts underlayng fs sync and thereby a transaction
823 * \param env environment
826 * \see dt_device_operations
828 static int osd_commit_async(const struct lu_env *env,
831 struct super_block *s = osd_sb(osd_dt_dev(d));
834 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
835 RETURN(s->s_op->sync_fs(s, 0));
839 * Concurrency: shouldn't matter.
841 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
843 static void osd_ro(const struct lu_env *env, struct dt_device *d)
847 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
849 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
850 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
856 * Concurrency: serialization provided by callers.
858 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
859 int mode, unsigned long timeout, __u32 alg,
860 struct lustre_capa_key *keys)
862 struct osd_device *dev = osd_dt_dev(d);
865 dev->od_fl_capa = mode;
866 dev->od_capa_timeout = timeout;
867 dev->od_capa_alg = alg;
868 dev->od_capa_keys = keys;
873 * Concurrency: serialization provided by callers.
875 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
876 struct dt_quota_ctxt *ctxt, void *data)
878 struct obd_device *obd = (void *)ctxt;
879 struct vfsmount *mnt = (struct vfsmount *)data;
882 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
883 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
884 obd->obd_lvfs_ctxt.pwdmnt = mnt;
885 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
886 obd->obd_lvfs_ctxt.fs = get_ds();
892 * Note: we do not count into QUOTA here.
893 * If we mount with --data_journal we may need more.
895 static const int osd_dto_credits_noquota[DTO_NR] = {
898 * INDEX_EXTRA_TRANS_BLOCKS(8) +
899 * SINGLEDATA_TRANS_BLOCKS(8)
900 * XXX Note: maybe iam need more, since iam have more level than
903 [DTO_INDEX_INSERT] = 16,
904 [DTO_INDEX_DELETE] = 16,
908 [DTO_IDNEX_UPDATE] = 16,
910 * Create a object. The same as create object in EXT3.
911 * DATA_TRANS_BLOCKS(14) +
912 * INDEX_EXTRA_BLOCKS(8) +
913 * 3(inode bits, groups, GDT)
915 [DTO_OBJECT_CREATE] = 25,
919 [DTO_OBJECT_DELETE] = 25,
922 * 3(inode bits, group, GDT)
924 [DTO_ATTR_SET_BASE] = 3,
926 * Xattr set. The same as xattr of EXT3.
927 * DATA_TRANS_BLOCKS(14)
928 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
929 * also counted in. Do not know why?
931 [DTO_XATTR_SET] = 14,
934 * creadits for inode change during write.
936 [DTO_WRITE_BASE] = 3,
938 * credits for single block write.
940 [DTO_WRITE_BLOCK] = 14,
942 * Attr set credits for chown.
943 * 3 (inode bit, group, GDT)
945 [DTO_ATTR_SET_CHOWN]= 3
949 * Note: we count into QUOTA here.
950 * If we mount with --data_journal we may need more.
952 static const int osd_dto_credits_quota[DTO_NR] = {
954 * INDEX_EXTRA_TRANS_BLOCKS(8) +
955 * SINGLEDATA_TRANS_BLOCKS(8) +
956 * 2 * QUOTA_TRANS_BLOCKS(2)
958 [DTO_INDEX_INSERT] = 20,
960 * INDEX_EXTRA_TRANS_BLOCKS(8) +
961 * SINGLEDATA_TRANS_BLOCKS(8) +
962 * 2 * QUOTA_TRANS_BLOCKS(2)
964 [DTO_INDEX_DELETE] = 20,
968 [DTO_IDNEX_UPDATE] = 16,
970 * Create a object. Same as create object in EXT3 filesystem.
971 * DATA_TRANS_BLOCKS(16) +
972 * INDEX_EXTRA_BLOCKS(8) +
973 * 3(inode bits, groups, GDT) +
974 * 2 * QUOTA_INIT_BLOCKS(25)
976 [DTO_OBJECT_CREATE] = 77,
979 * DATA_TRANS_BLOCKS(16) +
980 * INDEX_EXTRA_BLOCKS(8) +
981 * 3(inode bits, groups, GDT) +
984 [DTO_OBJECT_DELETE] = 27,
987 * 3 (inode bit, group, GDT) +
989 [DTO_ATTR_SET_BASE] = 3,
991 * Xattr set. The same as xattr of EXT3.
992 * DATA_TRANS_BLOCKS(16)
993 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
994 * also counted in. Do not know why?
996 [DTO_XATTR_SET] = 16,
999 * creadits for inode change during write.
1001 [DTO_WRITE_BASE] = 3,
1003 * credits for single block write.
1005 [DTO_WRITE_BLOCK] = 16,
1007 * Attr set credits for chown.
1008 * 3 (inode bit, group, GDT) +
1009 * 2 * QUOTA_INIT_BLOCKS(25) +
1010 * 2 * QUOTA_DEL_BLOCKS(9)
1012 [DTO_ATTR_SET_CHOWN]= 71
1015 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1018 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1019 ARRAY_SIZE(osd_dto_credits_quota));
1020 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1021 #ifdef HAVE_QUOTA_SUPPORT
1022 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1023 return osd_dto_credits_quota[op];
1026 return osd_dto_credits_noquota[op];
1029 static const struct dt_device_operations osd_dt_ops = {
1030 .dt_root_get = osd_root_get,
1031 .dt_statfs = osd_statfs,
1032 .dt_trans_start = osd_trans_start,
1033 .dt_trans_stop = osd_trans_stop,
1034 .dt_conf_get = osd_conf_get,
1035 .dt_sync = osd_sync,
1037 .dt_commit_async = osd_commit_async,
1038 .dt_credit_get = osd_credit_get,
1039 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1040 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1043 static void osd_object_read_lock(const struct lu_env *env,
1044 struct dt_object *dt, unsigned role)
1046 struct osd_object *obj = osd_dt_obj(dt);
1047 struct osd_thread_info *oti = osd_oti_get(env);
1049 LINVRNT(osd_invariant(obj));
1051 LASSERT(obj->oo_owner != env);
1052 down_read_nested(&obj->oo_sem, role);
1054 LASSERT(obj->oo_owner == NULL);
1058 static void osd_object_write_lock(const struct lu_env *env,
1059 struct dt_object *dt, unsigned role)
1061 struct osd_object *obj = osd_dt_obj(dt);
1062 struct osd_thread_info *oti = osd_oti_get(env);
1064 LINVRNT(osd_invariant(obj));
1066 LASSERT(obj->oo_owner != env);
1067 down_write_nested(&obj->oo_sem, role);
1069 LASSERT(obj->oo_owner == NULL);
1070 obj->oo_owner = env;
1074 static void osd_object_read_unlock(const struct lu_env *env,
1075 struct dt_object *dt)
1077 struct osd_object *obj = osd_dt_obj(dt);
1078 struct osd_thread_info *oti = osd_oti_get(env);
1080 LINVRNT(osd_invariant(obj));
1082 LASSERT(oti->oti_r_locks > 0);
1084 up_read(&obj->oo_sem);
1087 static void osd_object_write_unlock(const struct lu_env *env,
1088 struct dt_object *dt)
1090 struct osd_object *obj = osd_dt_obj(dt);
1091 struct osd_thread_info *oti = osd_oti_get(env);
1093 LINVRNT(osd_invariant(obj));
1095 LASSERT(obj->oo_owner == env);
1096 LASSERT(oti->oti_w_locks > 0);
1098 obj->oo_owner = NULL;
1099 up_write(&obj->oo_sem);
1102 static int capa_is_sane(const struct lu_env *env,
1103 struct osd_device *dev,
1104 struct lustre_capa *capa,
1105 struct lustre_capa_key *keys)
1107 struct osd_thread_info *oti = osd_oti_get(env);
1108 struct lustre_capa *tcapa = &oti->oti_capa;
1109 struct obd_capa *oc;
1113 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1115 if (capa_is_expired(oc)) {
1116 DEBUG_CAPA(D_ERROR, capa, "expired");
1123 if (capa_is_expired_sec(capa)) {
1124 DEBUG_CAPA(D_ERROR, capa, "expired");
1128 spin_lock(&capa_lock);
1129 for (i = 0; i < 2; i++) {
1130 if (keys[i].lk_keyid == capa->lc_keyid) {
1131 oti->oti_capa_key = keys[i];
1135 spin_unlock(&capa_lock);
1138 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1142 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1146 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1147 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1151 oc = capa_add(dev->od_capa_hash, capa);
1157 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1158 struct lustre_capa *capa, __u64 opc)
1160 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1161 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1162 struct md_capainfo *ci;
1165 if (!dev->od_fl_capa)
1168 if (capa == BYPASS_CAPA)
1171 ci = md_capainfo(env);
1175 if (ci->mc_auth == LC_ID_NONE)
1179 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1183 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1184 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1189 if (!capa_opc_supported(capa, opc)) {
1190 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1194 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1195 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1202 static int osd_attr_get(const struct lu_env *env,
1203 struct dt_object *dt,
1204 struct lu_attr *attr,
1205 struct lustre_capa *capa)
1207 struct osd_object *obj = osd_dt_obj(dt);
1209 LASSERT(dt_object_exists(dt));
1210 LINVRNT(osd_invariant(obj));
1212 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1215 spin_lock(&obj->oo_guard);
1216 osd_inode_getattr(env, obj->oo_inode, attr);
1217 spin_unlock(&obj->oo_guard);
1221 static int osd_attr_set(const struct lu_env *env,
1222 struct dt_object *dt,
1223 const struct lu_attr *attr,
1224 struct thandle *handle,
1225 struct lustre_capa *capa)
1227 struct osd_object *obj = osd_dt_obj(dt);
1230 LASSERT(handle != NULL);
1231 LASSERT(dt_object_exists(dt));
1232 LASSERT(osd_invariant(obj));
1234 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1237 spin_lock(&obj->oo_guard);
1238 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1239 spin_unlock(&obj->oo_guard);
1242 mark_inode_dirty(obj->oo_inode);
1246 static struct timespec *osd_inode_time(const struct lu_env *env,
1247 struct inode *inode, __u64 seconds)
1249 struct osd_thread_info *oti = osd_oti_get(env);
1250 struct timespec *t = &oti->oti_time;
1252 t->tv_sec = seconds;
1254 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1258 static int osd_inode_setattr(const struct lu_env *env,
1259 struct inode *inode, const struct lu_attr *attr)
1263 bits = attr->la_valid;
1265 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1267 #ifdef HAVE_QUOTA_SUPPORT
1268 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1269 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1270 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1276 iattr.ia_valid |= ATTR_UID;
1278 iattr.ia_valid |= ATTR_GID;
1279 iattr.ia_uid = attr->la_uid;
1280 iattr.ia_gid = attr->la_gid;
1281 osd_push_ctxt(env, save);
1282 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1289 if (bits & LA_ATIME)
1290 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1291 if (bits & LA_CTIME)
1292 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1293 if (bits & LA_MTIME)
1294 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1295 if (bits & LA_SIZE) {
1296 LDISKFS_I(inode)->i_disksize = attr->la_size;
1297 i_size_write(inode, attr->la_size);
1300 /* OSD should not change "i_blocks" which is used by quota.
1301 * "i_blocks" should be changed by ldiskfs only.
1302 * Enable this assignment for SOM purpose now, until it is
1303 * stored in SOM EA. */
1304 if (bits & LA_BLOCKS)
1305 inode->i_blocks = attr->la_blocks;
1308 inode->i_mode = (inode->i_mode & S_IFMT) |
1309 (attr->la_mode & ~S_IFMT);
1311 inode->i_uid = attr->la_uid;
1313 inode->i_gid = attr->la_gid;
1314 if (bits & LA_NLINK)
1315 inode->i_nlink = attr->la_nlink;
1317 inode->i_rdev = attr->la_rdev;
1319 if (bits & LA_FLAGS) {
1320 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1322 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1323 (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1331 * XXX temporary solution.
1334 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1335 struct lu_attr *attr, struct thandle *th)
1340 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1341 struct lu_attr *attr, struct thandle *th)
1343 osd_object_init0(obj);
1347 extern struct inode *ldiskfs_create_inode(handle_t *handle,
1348 struct inode * dir, int mode);
1349 extern int ldiskfs_add_entry(handle_t *handle, struct dentry *dentry,
1350 struct inode *inode);
1351 extern int ldiskfs_delete_entry(handle_t *handle,
1353 struct ldiskfs_dir_entry_2 * de_del,
1354 struct buffer_head * bh);
1355 extern struct buffer_head * ldiskfs_find_entry(struct dentry *dentry,
1356 struct ldiskfs_dir_entry_2
1358 extern int ldiskfs_add_dot_dotdot(handle_t *handle, struct inode *dir,
1359 struct inode *inode);
1361 extern int ldiskfs_xattr_set_handle(handle_t *handle, struct inode *inode,
1362 int name_index, const char *name,
1363 const void *value, size_t value_len,
1366 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1367 struct osd_object *obj,
1371 struct osd_thread_info *info = osd_oti_get(env);
1372 struct dentry *child_dentry = &info->oti_child_dentry;
1373 struct dentry *obj_dentry = &info->oti_obj_dentry;
1375 obj_dentry->d_inode = obj->oo_inode;
1376 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1377 obj_dentry->d_name.hash = 0;
1379 child_dentry->d_name.hash = 0;
1380 child_dentry->d_parent = obj_dentry;
1381 child_dentry->d_name.name = name;
1382 child_dentry->d_name.len = namelen;
1383 return child_dentry;
1387 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1389 struct dt_allocation_hint *hint,
1393 struct osd_device *osd = osd_obj2dev(obj);
1394 struct osd_thandle *oth;
1395 struct dt_object *parent;
1396 struct inode *inode;
1397 #ifdef HAVE_QUOTA_SUPPORT
1398 struct osd_ctxt *save = &info->oti_ctxt;
1401 LINVRNT(osd_invariant(obj));
1402 LASSERT(obj->oo_inode == NULL);
1404 oth = container_of(th, struct osd_thandle, ot_super);
1405 LASSERT(oth->ot_handle->h_transaction != NULL);
1407 if (hint && hint->dah_parent)
1408 parent = hint->dah_parent;
1410 parent = osd->od_obj_area;
1412 LASSERT(parent != NULL);
1413 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1415 #ifdef HAVE_QUOTA_SUPPORT
1416 osd_push_ctxt(info->oti_env, save);
1418 inode = ldiskfs_create_inode(oth->ot_handle,
1419 osd_dt_obj(parent)->oo_inode, mode);
1420 #ifdef HAVE_QUOTA_SUPPORT
1423 if (!IS_ERR(inode)) {
1424 obj->oo_inode = inode;
1427 result = PTR_ERR(inode);
1428 LINVRNT(osd_invariant(obj));
1433 extern int iam_lvar_create(struct inode *obj, int keysize, int ptrsize,
1434 int recsize, handle_t *handle);
1436 extern int iam_lfix_create(struct inode *obj, int keysize, int ptrsize,
1437 int recsize, handle_t *handle);
1444 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1445 struct lu_attr *attr,
1446 struct dt_allocation_hint *hint,
1447 struct dt_object_format *dof,
1451 struct osd_thandle *oth;
1452 struct osd_device *osd = osd_obj2dev(obj);
1453 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1455 LASSERT(S_ISDIR(attr->la_mode));
1457 oth = container_of(th, struct osd_thandle, ot_super);
1458 LASSERT(oth->ot_handle->h_transaction != NULL);
1459 result = osd_mkfile(info, obj, mode, hint, th);
1460 if (result == 0 && osd->od_iop_mode == 0) {
1461 LASSERT(obj->oo_inode != NULL);
1463 * XXX uh-oh... call low-level iam function directly.
1466 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1467 sizeof (struct lu_fid_pack),
1473 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1474 struct lu_attr *attr,
1475 struct dt_allocation_hint *hint,
1476 struct dt_object_format *dof,
1480 struct osd_thandle *oth;
1481 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1483 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1485 LASSERT(S_ISREG(attr->la_mode));
1487 oth = container_of(th, struct osd_thandle, ot_super);
1488 LASSERT(oth->ot_handle->h_transaction != NULL);
1490 result = osd_mkfile(info, obj, mode, hint, th);
1492 LASSERT(obj->oo_inode != NULL);
1493 if (feat->dif_flags & DT_IND_VARKEY)
1494 result = iam_lvar_create(obj->oo_inode,
1495 feat->dif_keysize_max,
1497 feat->dif_recsize_max,
1500 result = iam_lfix_create(obj->oo_inode,
1501 feat->dif_keysize_max,
1503 feat->dif_recsize_max,
1510 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1511 struct lu_attr *attr,
1512 struct dt_allocation_hint *hint,
1513 struct dt_object_format *dof,
1516 LASSERT(S_ISREG(attr->la_mode));
1517 return osd_mkfile(info, obj, (attr->la_mode &
1518 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1521 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1522 struct lu_attr *attr,
1523 struct dt_allocation_hint *hint,
1524 struct dt_object_format *dof,
1527 LASSERT(S_ISLNK(attr->la_mode));
1528 return osd_mkfile(info, obj, (attr->la_mode &
1529 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1532 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1533 struct lu_attr *attr,
1534 struct dt_allocation_hint *hint,
1535 struct dt_object_format *dof,
1538 umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1541 LINVRNT(osd_invariant(obj));
1542 LASSERT(obj->oo_inode == NULL);
1543 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1544 S_ISFIFO(mode) || S_ISSOCK(mode));
1546 result = osd_mkfile(info, obj, mode, hint, th);
1548 LASSERT(obj->oo_inode != NULL);
1549 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1551 LINVRNT(osd_invariant(obj));
1555 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1557 struct dt_allocation_hint *hint,
1558 struct dt_object_format *dof,
1561 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1563 osd_obj_type_f result;
1579 result = osd_mk_index;
1590 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1591 struct dt_object *parent, umode_t child_mode)
1595 memset(ah, 0, sizeof(*ah));
1596 ah->dah_parent = parent;
1597 ah->dah_mode = child_mode;
1601 * Helper function for osd_object_create()
1603 * \retval 0, on success
1605 static int __osd_object_create(struct osd_thread_info *info,
1606 struct osd_object *obj, struct lu_attr *attr,
1607 struct dt_allocation_hint *hint,
1608 struct dt_object_format *dof,
1614 result = osd_create_pre(info, obj, attr, th);
1616 result = osd_create_type_f(dof->dof_type)(info, obj,
1617 attr, hint, dof, th);
1619 result = osd_create_post(info, obj, attr, th);
1625 * Helper function for osd_object_create()
1627 * \retval 0, on success
1629 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1630 const struct lu_fid *fid, struct thandle *th)
1632 struct osd_thread_info *info = osd_oti_get(env);
1633 struct osd_inode_id *id = &info->oti_id;
1634 struct osd_device *osd = osd_obj2dev(obj);
1635 struct md_ucred *uc = md_ucred(env);
1637 LASSERT(obj->oo_inode != NULL);
1638 LASSERT(uc != NULL);
1640 id->oii_ino = obj->oo_inode->i_ino;
1641 id->oii_gen = obj->oo_inode->i_generation;
1643 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1644 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1647 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1648 struct lu_attr *attr,
1649 struct dt_allocation_hint *hint,
1650 struct dt_object_format *dof,
1653 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1654 struct osd_object *obj = osd_dt_obj(dt);
1655 struct osd_thread_info *info = osd_oti_get(env);
1660 LINVRNT(osd_invariant(obj));
1661 LASSERT(!dt_object_exists(dt));
1662 LASSERT(osd_write_locked(env, obj));
1663 LASSERT(th != NULL);
1665 result = __osd_object_create(info, obj, attr, hint, dof, th);
1667 result = __osd_oi_insert(env, obj, fid, th);
1669 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1670 LASSERT(osd_invariant(obj));
1675 * Helper function for osd_xattr_set()
1677 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1678 const struct lu_buf *buf, const char *name, int fl)
1680 struct osd_object *obj = osd_dt_obj(dt);
1681 struct inode *inode = obj->oo_inode;
1682 struct osd_thread_info *info = osd_oti_get(env);
1683 struct dentry *dentry = &info->oti_child_dentry;
1684 struct timespec *t = &info->oti_time;
1688 LASSERT(dt_object_exists(dt));
1689 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1690 LASSERT(osd_write_locked(env, obj));
1692 if (fl & LU_XATTR_REPLACE)
1693 fs_flags |= XATTR_REPLACE;
1695 if (fl & LU_XATTR_CREATE)
1696 fs_flags |= XATTR_CREATE;
1698 dentry->d_inode = inode;
1699 *t = inode->i_ctime;
1700 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1701 buf->lb_len, fs_flags);
1702 /* ctime should not be updated with server-side time. */
1703 spin_lock(&obj->oo_guard);
1704 inode->i_ctime = *t;
1705 spin_unlock(&obj->oo_guard);
1706 mark_inode_dirty(inode);
1711 * Put the fid into lustre_mdt_attrs, and then place the structure
1712 * inode's ea. This fid should not be altered during the life time
1715 * \retval +ve, on success
1716 * \retval -ve, on error
1718 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1720 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1721 const struct lu_fid *fid)
1723 struct osd_thread_info *info = osd_oti_get(env);
1724 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1726 fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1728 return __osd_xattr_set(env, dt,
1729 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1730 XATTR_NAME_LMA, LU_XATTR_CREATE);
1735 * Helper function to form igif
1737 static inline void osd_igif_get(const struct lu_env *env, struct dentry *dentry,
1740 struct inode *inode = dentry->d_inode;
1741 lu_igif_build(fid, inode->i_ino, inode->i_generation);
1745 * Helper function to pack the fid
1747 static inline void osd_fid_pack(const struct lu_env *env, const struct lu_fid *fid,
1748 struct lu_fid_pack *pack)
1750 fid_pack(pack, fid, &osd_oti_get(env)->oti_fid);
1754 * Try to read the fid from inode ea into dt_rec, if return value
1755 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1757 * \param rec, the data-structure into which fid/igif is read
1759 * \retval 0, on success
1761 static int osd_ea_fid_get(const struct lu_env *env, struct dentry *dentry,
1764 struct inode *inode = dentry->d_inode;
1765 struct osd_thread_info *info = osd_oti_get(env);
1766 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1767 struct lu_fid *fid = &info->oti_fid;
1770 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1772 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1776 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1778 } else if (rc == -ENODATA) {
1779 osd_igif_get(env, dentry, fid);
1784 osd_fid_pack(env, fid, (struct lu_fid_pack*)rec);
1790 * OSD layer object create function for interoperability mode (b11826).
1791 * This is mostly similar to osd_object_create(). Only difference being, fid is
1792 * inserted into inode ea here.
1794 * \retval 0, on success
1795 * \retval -ve, on error
1797 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1798 struct lu_attr *attr,
1799 struct dt_allocation_hint *hint,
1800 struct dt_object_format *dof,
1803 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1804 struct osd_object *obj = osd_dt_obj(dt);
1805 struct osd_thread_info *info = osd_oti_get(env);
1811 LASSERT(osd_invariant(obj));
1812 LASSERT(!dt_object_exists(dt));
1813 LASSERT(osd_write_locked(env, obj));
1814 LASSERT(th != NULL);
1816 result = __osd_object_create(info, obj, attr, hint, dof, th);
1818 if (hint && hint->dah_parent)
1819 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1821 /* objects under osd root shld have igif fid, so dont add fid EA */
1822 if (result == 0 && is_root == 0)
1823 result = osd_ea_fid_set(env, dt, fid);
1826 result = __osd_oi_insert(env, obj, fid, th);
1828 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1829 LINVRNT(osd_invariant(obj));
1834 * Concurrency: @dt is write locked.
1836 static void osd_object_ref_add(const struct lu_env *env,
1837 struct dt_object *dt,
1840 struct osd_object *obj = osd_dt_obj(dt);
1841 struct inode *inode = obj->oo_inode;
1843 LINVRNT(osd_invariant(obj));
1844 LASSERT(dt_object_exists(dt));
1845 LASSERT(osd_write_locked(env, obj));
1846 LASSERT(th != NULL);
1848 spin_lock(&obj->oo_guard);
1849 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1851 spin_unlock(&obj->oo_guard);
1852 mark_inode_dirty(inode);
1853 LINVRNT(osd_invariant(obj));
1857 * Concurrency: @dt is write locked.
1859 static void osd_object_ref_del(const struct lu_env *env,
1860 struct dt_object *dt,
1863 struct osd_object *obj = osd_dt_obj(dt);
1864 struct inode *inode = obj->oo_inode;
1866 LINVRNT(osd_invariant(obj));
1867 LASSERT(dt_object_exists(dt));
1868 LASSERT(osd_write_locked(env, obj));
1869 LASSERT(th != NULL);
1871 spin_lock(&obj->oo_guard);
1872 LASSERT(inode->i_nlink > 0);
1874 spin_unlock(&obj->oo_guard);
1875 mark_inode_dirty(inode);
1876 LINVRNT(osd_invariant(obj));
1880 * Concurrency: @dt is read locked.
1882 static int osd_xattr_get(const struct lu_env *env,
1883 struct dt_object *dt,
1886 struct lustre_capa *capa)
1888 struct osd_object *obj = osd_dt_obj(dt);
1889 struct inode *inode = obj->oo_inode;
1890 struct osd_thread_info *info = osd_oti_get(env);
1891 struct dentry *dentry = &info->oti_obj_dentry;
1893 LASSERT(dt_object_exists(dt));
1894 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1895 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1897 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1900 dentry->d_inode = inode;
1901 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1906 * Concurrency: @dt is write locked.
1908 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1909 const struct lu_buf *buf, const char *name, int fl,
1910 struct thandle *handle, struct lustre_capa *capa)
1912 LASSERT(handle != NULL);
1914 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1917 return __osd_xattr_set(env, dt, buf, name, fl);
1921 * Concurrency: @dt is read locked.
1923 static int osd_xattr_list(const struct lu_env *env,
1924 struct dt_object *dt,
1926 struct lustre_capa *capa)
1928 struct osd_object *obj = osd_dt_obj(dt);
1929 struct inode *inode = obj->oo_inode;
1930 struct osd_thread_info *info = osd_oti_get(env);
1931 struct dentry *dentry = &info->oti_obj_dentry;
1933 LASSERT(dt_object_exists(dt));
1934 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1935 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1937 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1940 dentry->d_inode = inode;
1941 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1945 * Concurrency: @dt is write locked.
1947 static int osd_xattr_del(const struct lu_env *env,
1948 struct dt_object *dt,
1950 struct thandle *handle,
1951 struct lustre_capa *capa)
1953 struct osd_object *obj = osd_dt_obj(dt);
1954 struct inode *inode = obj->oo_inode;
1955 struct osd_thread_info *info = osd_oti_get(env);
1956 struct dentry *dentry = &info->oti_obj_dentry;
1957 struct timespec *t = &info->oti_time;
1960 LASSERT(dt_object_exists(dt));
1961 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1962 LASSERT(osd_write_locked(env, obj));
1963 LASSERT(handle != NULL);
1965 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1968 dentry->d_inode = inode;
1969 *t = inode->i_ctime;
1970 rc = inode->i_op->removexattr(dentry, name);
1971 /* ctime should not be updated with server-side time. */
1972 spin_lock(&obj->oo_guard);
1973 inode->i_ctime = *t;
1974 spin_unlock(&obj->oo_guard);
1975 mark_inode_dirty(inode);
1979 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1980 struct dt_object *dt,
1981 struct lustre_capa *old,
1984 struct osd_thread_info *info = osd_oti_get(env);
1985 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1986 struct osd_object *obj = osd_dt_obj(dt);
1987 struct osd_device *dev = osd_obj2dev(obj);
1988 struct lustre_capa_key *key = &info->oti_capa_key;
1989 struct lustre_capa *capa = &info->oti_capa;
1990 struct obd_capa *oc;
1991 struct md_capainfo *ci;
1995 if (!dev->od_fl_capa)
1996 RETURN(ERR_PTR(-ENOENT));
1998 LASSERT(dt_object_exists(dt));
1999 LINVRNT(osd_invariant(obj));
2001 /* renewal sanity check */
2002 if (old && osd_object_auth(env, dt, old, opc))
2003 RETURN(ERR_PTR(-EACCES));
2005 ci = md_capainfo(env);
2007 RETURN(ERR_PTR(-ENOENT));
2009 switch (ci->mc_auth) {
2013 capa->lc_uid = obj->oo_inode->i_uid;
2014 capa->lc_gid = obj->oo_inode->i_gid;
2015 capa->lc_flags = LC_ID_PLAIN;
2017 case LC_ID_CONVERT: {
2020 s[0] = obj->oo_inode->i_uid;
2021 get_random_bytes(&(s[1]), sizeof(__u32));
2022 s[2] = obj->oo_inode->i_gid;
2023 get_random_bytes(&(s[3]), sizeof(__u32));
2024 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2026 RETURN(ERR_PTR(rc));
2028 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2029 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2030 capa->lc_flags = LC_ID_CONVERT;
2034 RETURN(ERR_PTR(-EINVAL));
2037 capa->lc_fid = *fid;
2039 capa->lc_flags |= dev->od_capa_alg << 24;
2040 capa->lc_timeout = dev->od_capa_timeout;
2041 capa->lc_expiry = 0;
2043 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2045 LASSERT(!capa_is_expired(oc));
2049 spin_lock(&capa_lock);
2050 *key = dev->od_capa_keys[1];
2051 spin_unlock(&capa_lock);
2053 capa->lc_keyid = key->lk_keyid;
2054 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2056 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2058 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2059 RETURN(ERR_PTR(rc));
2062 oc = capa_add(dev->od_capa_hash, capa);
2066 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2069 struct osd_object *obj = osd_dt_obj(dt);
2070 struct inode *inode = obj->oo_inode;
2071 struct osd_thread_info *info = osd_oti_get(env);
2072 struct dentry *dentry = &info->oti_obj_dentry;
2073 struct file *file = &info->oti_file;
2076 dentry->d_inode = inode;
2077 file->f_dentry = dentry;
2078 file->f_mapping = inode->i_mapping;
2079 file->f_op = inode->i_fop;
2080 LOCK_INODE_MUTEX(inode);
2081 rc = file->f_op->fsync(file, dentry, 0);
2082 UNLOCK_INODE_MUTEX(inode);
2087 * Get the 64-bit version for an inode.
2089 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2090 struct dt_object *dt)
2092 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2094 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2095 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2096 return LDISKFS_I(inode)->i_fs_version;
2100 * Set the 64-bit version and return the old version.
2102 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2103 dt_obj_version_t new_version)
2105 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2107 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2108 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2109 LDISKFS_I(inode)->i_fs_version = new_version;
2110 /** Version is set after all inode operations are finished,
2111 * so we should mark it dirty here */
2112 inode->i_sb->s_op->dirty_inode(inode);
2115 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2118 struct osd_object *obj = osd_dt_obj(dt);
2121 *data = (void *)obj->oo_inode;
2125 static const struct dt_object_operations osd_obj_ops = {
2126 .do_read_lock = osd_object_read_lock,
2127 .do_write_lock = osd_object_write_lock,
2128 .do_read_unlock = osd_object_read_unlock,
2129 .do_write_unlock = osd_object_write_unlock,
2130 .do_attr_get = osd_attr_get,
2131 .do_attr_set = osd_attr_set,
2132 .do_ah_init = osd_ah_init,
2133 .do_create = osd_object_create,
2134 .do_index_try = osd_index_try,
2135 .do_ref_add = osd_object_ref_add,
2136 .do_ref_del = osd_object_ref_del,
2137 .do_xattr_get = osd_xattr_get,
2138 .do_xattr_set = osd_xattr_set,
2139 .do_xattr_del = osd_xattr_del,
2140 .do_xattr_list = osd_xattr_list,
2141 .do_capa_get = osd_capa_get,
2142 .do_object_sync = osd_object_sync,
2143 .do_version_get = osd_object_version_get,
2144 .do_version_set = osd_object_version_set,
2145 .do_data_get = osd_data_get,
2149 * dt_object_operations for interoperability mode
2150 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2152 static const struct dt_object_operations osd_obj_ea_ops = {
2153 .do_read_lock = osd_object_read_lock,
2154 .do_write_lock = osd_object_write_lock,
2155 .do_read_unlock = osd_object_read_unlock,
2156 .do_write_unlock = osd_object_write_unlock,
2157 .do_attr_get = osd_attr_get,
2158 .do_attr_set = osd_attr_set,
2159 .do_ah_init = osd_ah_init,
2160 .do_create = osd_object_ea_create,
2161 .do_index_try = osd_index_try,
2162 .do_ref_add = osd_object_ref_add,
2163 .do_ref_del = osd_object_ref_del,
2164 .do_xattr_get = osd_xattr_get,
2165 .do_xattr_set = osd_xattr_set,
2166 .do_xattr_del = osd_xattr_del,
2167 .do_xattr_list = osd_xattr_list,
2168 .do_capa_get = osd_capa_get,
2169 .do_object_sync = osd_object_sync,
2170 .do_version_get = osd_object_version_get,
2171 .do_version_set = osd_object_version_set,
2172 .do_data_get = osd_data_get,
2180 * XXX: Another layering violation for now.
2182 * We don't want to use ->f_op->read methods, because generic file write
2184 * - serializes on ->i_sem, and
2186 * - does a lot of extra work like balance_dirty_pages(),
2188 * which doesn't work for globally shared files like /last-received.
2190 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2191 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2192 loff_t *offs, handle_t *handle);
2194 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2195 struct lu_buf *buf, loff_t *pos,
2196 struct lustre_capa *capa)
2198 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2200 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2203 return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2206 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2207 const struct lu_buf *buf, loff_t *pos,
2208 struct thandle *handle, struct lustre_capa *capa,
2211 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2212 struct osd_thandle *oh;
2214 #ifdef HAVE_QUOTA_SUPPORT
2215 cfs_cap_t save = current->cap_effective;
2218 LASSERT(handle != NULL);
2220 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2223 oh = container_of(handle, struct osd_thandle, ot_super);
2224 LASSERT(oh->ot_handle->h_transaction != NULL);
2225 #ifdef HAVE_QUOTA_SUPPORT
2227 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2229 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2231 result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2232 pos, oh->ot_handle);
2233 #ifdef HAVE_QUOTA_SUPPORT
2234 current->cap_effective = save;
2237 result = buf->lb_len;
2241 static const struct dt_body_operations osd_body_ops = {
2242 .dbo_read = osd_read,
2243 .dbo_write = osd_write
2250 static int osd_object_is_root(const struct osd_object *obj)
2252 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
2255 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2256 const struct dt_index_features *feat)
2258 struct iam_descr *descr;
2259 struct dt_object *dt = &o->oo_dt;
2261 if (osd_object_is_root(o))
2262 return feat == &dt_directory_features;
2264 LASSERT(o->oo_dir != NULL);
2266 descr = o->oo_dir->od_container.ic_descr;
2267 if (feat == &dt_directory_features) {
2268 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2271 if (descr == &iam_htree_compat_param) {
2272 /* if it is a HTREE dir then there is good chance that,
2273 * we dealing with ext3 directory here with no FIDs. */
2275 if (descr->id_rec_size ==
2276 sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2278 dt->do_index_ops = &osd_index_ea_ops;
2285 feat->dif_keysize_min <= descr->id_key_size &&
2286 descr->id_key_size <= feat->dif_keysize_max &&
2287 feat->dif_recsize_min <= descr->id_rec_size &&
2288 descr->id_rec_size <= feat->dif_recsize_max &&
2289 !(feat->dif_flags & (DT_IND_VARKEY |
2290 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2291 ergo(feat->dif_flags & DT_IND_UPDATE,
2292 1 /* XXX check that object (and file system) is
2297 static int osd_iam_container_init(const struct lu_env *env,
2298 struct osd_object *obj,
2299 struct osd_directory *dir)
2302 struct iam_container *bag;
2304 bag = &dir->od_container;
2305 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2307 result = iam_container_setup(bag);
2309 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2311 iam_container_fini(bag);
2317 * Concurrency: no external locking is necessary.
2319 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2320 const struct dt_index_features *feat)
2324 struct osd_object *obj = osd_dt_obj(dt);
2325 struct osd_device *osd = osd_obj2dev(obj);
2327 LINVRNT(osd_invariant(obj));
2328 LASSERT(dt_object_exists(dt));
2330 if (osd_object_is_root(obj)) {
2331 dt->do_index_ops = &osd_index_ea_ops;
2333 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2334 dt->do_index_ops = &osd_index_ea_ops;
2335 if (S_ISDIR(obj->oo_inode->i_mode))
2340 } else if (!osd_has_index(obj)) {
2341 struct osd_directory *dir;
2346 spin_lock(&obj->oo_guard);
2347 if (obj->oo_dir == NULL)
2351 * Concurrent thread allocated container data.
2354 spin_unlock(&obj->oo_guard);
2356 * Now, that we have container data, serialize its
2359 down_write(&obj->oo_ext_idx_sem);
2361 * recheck under lock.
2363 if (!osd_has_index(obj))
2364 result = osd_iam_container_init(env, obj, dir);
2367 up_write(&obj->oo_ext_idx_sem);
2373 if (result == 0 && ea_dir == 0) {
2374 if (!osd_iam_index_probe(env, obj, feat))
2377 LINVRNT(osd_invariant(obj));
2383 * delete a (key, value) pair from index \a dt specified by \a key
2385 * \param dt_object osd index object
2386 * \param key key for index
2387 * \param rec record reference
2388 * \param handle transaction handler
2391 * \retval -ve failure
2394 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2395 const struct dt_key *key, struct thandle *handle,
2396 struct lustre_capa *capa)
2398 struct osd_object *obj = osd_dt_obj(dt);
2399 struct osd_thandle *oh;
2400 struct iam_path_descr *ipd;
2401 struct iam_container *bag = &obj->oo_dir->od_container;
2406 LINVRNT(osd_invariant(obj));
2407 LASSERT(dt_object_exists(dt));
2408 LASSERT(bag->ic_object == obj->oo_inode);
2409 LASSERT(handle != NULL);
2411 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2414 ipd = osd_idx_ipd_get(env, bag);
2415 if (unlikely(ipd == NULL))
2418 oh = container_of0(handle, struct osd_thandle, ot_super);
2419 LASSERT(oh->ot_handle != NULL);
2420 LASSERT(oh->ot_handle->h_transaction != NULL);
2422 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2423 osd_ipd_put(env, bag, ipd);
2424 LINVRNT(osd_invariant(obj));
2429 * Index delete function for interoperability mode (b11826).
2430 * It will remove the directory entry added by osd_index_ea_insert().
2431 * This entry is needed to maintain name->fid mapping.
2433 * \param key, key i.e. file entry to be deleted
2435 * \retval 0, on success
2436 * \retval -ve, on error
2438 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2439 const struct dt_key *key, struct thandle *handle,
2440 struct lustre_capa *capa)
2442 struct osd_object *obj = osd_dt_obj(dt);
2443 struct inode *dir = obj->oo_inode;
2444 struct dentry *dentry;
2445 struct osd_thandle *oh;
2446 struct ldiskfs_dir_entry_2 *de;
2447 struct buffer_head *bh;
2453 LINVRNT(osd_invariant(obj));
2454 LASSERT(dt_object_exists(dt));
2455 LASSERT(handle != NULL);
2457 oh = container_of(handle, struct osd_thandle, ot_super);
2458 LASSERT(oh->ot_handle != NULL);
2459 LASSERT(oh->ot_handle->h_transaction != NULL);
2461 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2464 dentry = osd_child_dentry_get(env, obj,
2465 (char *)key, strlen((char *)key));
2467 down_write(&obj->oo_ext_idx_sem);
2468 bh = ldiskfs_find_entry(dentry, &de);
2470 struct osd_thread_info *oti = osd_oti_get(env);
2471 struct timespec *ctime = &oti->oti_time;
2472 struct timespec *mtime = &oti->oti_time2;
2474 *ctime = dir->i_ctime;
2475 *mtime = dir->i_mtime;
2476 rc = ldiskfs_delete_entry(oh->ot_handle,
2478 /* xtime should not be updated with server-side time. */
2479 spin_lock(&obj->oo_guard);
2480 dir->i_ctime = *ctime;
2481 dir->i_mtime = *mtime;
2482 spin_unlock(&obj->oo_guard);
2483 mark_inode_dirty(dir);
2488 up_write(&obj->oo_ext_idx_sem);
2489 LASSERT(osd_invariant(obj));
2494 * Lookup index for \a key and copy record to \a rec.
2496 * \param dt_object osd index object
2497 * \param key key for index
2498 * \param rec record reference
2500 * \retval +ve success : exact mach
2501 * \retval 0 return record with key not greater than \a key
2502 * \retval -ve failure
2504 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2505 struct dt_rec *rec, const struct dt_key *key,
2506 struct lustre_capa *capa)
2508 struct osd_object *obj = osd_dt_obj(dt);
2509 struct iam_path_descr *ipd;
2510 struct iam_container *bag = &obj->oo_dir->od_container;
2511 struct osd_thread_info *oti = osd_oti_get(env);
2512 struct iam_iterator *it = &oti->oti_idx_it;
2516 LASSERT(osd_invariant(obj));
2517 LASSERT(dt_object_exists(dt));
2518 LASSERT(bag->ic_object == obj->oo_inode);
2520 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2523 ipd = osd_idx_ipd_get(env, bag);
2527 /* got ipd now we can start iterator. */
2528 iam_it_init(it, bag, 0, ipd);
2530 rc = iam_it_get(it, (struct iam_key *)key);
2532 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2536 osd_ipd_put(env, bag, ipd);
2538 LINVRNT(osd_invariant(obj));
2544 * Inserts (key, value) pair in \a dt index object.
2546 * \param dt osd index object
2547 * \param key key for index
2548 * \param rec record reference
2549 * \param th transaction handler
2552 * \retval -ve failure
2554 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2555 const struct dt_rec *rec, const struct dt_key *key,
2556 struct thandle *th, struct lustre_capa *capa,
2559 struct osd_object *obj = osd_dt_obj(dt);
2560 struct iam_path_descr *ipd;
2561 struct osd_thandle *oh;
2562 struct iam_container *bag = &obj->oo_dir->od_container;
2563 #ifdef HAVE_QUOTA_SUPPORT
2564 cfs_cap_t save = current->cap_effective;
2570 LINVRNT(osd_invariant(obj));
2571 LASSERT(dt_object_exists(dt));
2572 LASSERT(bag->ic_object == obj->oo_inode);
2573 LASSERT(th != NULL);
2575 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2578 ipd = osd_idx_ipd_get(env, bag);
2579 if (unlikely(ipd == NULL))
2582 oh = container_of0(th, struct osd_thandle, ot_super);
2583 LASSERT(oh->ot_handle != NULL);
2584 LASSERT(oh->ot_handle->h_transaction != NULL);
2585 #ifdef HAVE_QUOTA_SUPPORT
2587 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2589 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2591 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2592 (struct iam_rec *)rec, ipd);
2593 #ifdef HAVE_QUOTA_SUPPORT
2594 current->cap_effective = save;
2596 osd_ipd_put(env, bag, ipd);
2597 LINVRNT(osd_invariant(obj));
2602 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2603 * into the directory.Also sets flags into osd object to
2604 * indicate dot and dotdot are created. This is required for
2605 * interoperability mode (b11826)
2607 * \param dir directory for dot and dotdot fixup.
2608 * \param obj child object for linking
2610 * \retval 0, on success
2611 * \retval -ve, on error
2613 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2614 struct osd_object *dir,
2615 struct osd_object *obj, const char *name,
2618 struct inode *parent_dir = obj->oo_inode;
2619 struct inode *inode = dir->oo_inode;
2620 struct osd_thandle *oth;
2623 oth = container_of(th, struct osd_thandle, ot_super);
2624 LASSERT(oth->ot_handle->h_transaction != NULL);
2625 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2627 if (strcmp(name, dot) == 0) {
2628 if (dir->oo_compat_dot_created) {
2631 LASSERT(obj == dir);
2632 dir->oo_compat_dot_created = 1;
2635 } else if(strcmp(name, dotdot) == 0) {
2636 if (!dir->oo_compat_dot_created)
2638 if (dir->oo_compat_dotdot_created)
2639 return __osd_ea_add_rec(info, dir, obj, name, th);
2641 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2643 dir->oo_compat_dotdot_created = 1;
2650 * Calls ldiskfs_add_entry() to add directory entry
2651 * into the directory. This is required for
2652 * interoperability mode (b11826)
2654 * \retval 0, on success
2655 * \retval -ve, on error
2657 static int __osd_ea_add_rec(struct osd_thread_info *info,
2658 struct osd_object *pobj,
2659 struct osd_object *cobj,
2663 struct dentry *child;
2664 struct osd_thandle *oth;
2665 struct inode *cinode = cobj->oo_inode;
2668 oth = container_of(th, struct osd_thandle, ot_super);
2669 LASSERT(oth->ot_handle != NULL);
2670 LASSERT(oth->ot_handle->h_transaction != NULL);
2672 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2673 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2679 * It will call the appropriate osd_add* function and return the
2680 * value, return by respective functions.
2682 static int osd_ea_add_rec(const struct lu_env *env,
2683 struct osd_object *pobj,
2684 struct osd_object *cobj,
2688 struct osd_thread_info *info = osd_oti_get(env);
2691 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2693 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2695 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2701 * Calls ->lookup() to find dentry. From dentry get inode and
2702 * read inode's ea to get fid. This is required for interoperability
2705 * \retval 0, on success
2706 * \retval -ve, on error
2708 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2709 struct dt_rec *rec, const struct dt_key *key)
2711 struct inode *dir = obj->oo_inode;
2712 struct osd_thread_info *info = osd_oti_get(env);
2713 struct dentry *dentry;
2714 struct osd_device *dev = osd_dev(obj->oo_dt.do_lu.lo_dev);
2715 struct osd_inode_id *id = &info->oti_id;
2716 struct ldiskfs_dir_entry_2 *de;
2717 struct buffer_head *bh;
2718 struct inode *inode;
2722 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2724 dentry = osd_child_dentry_get(env, obj,
2725 (char *)key, strlen((char *)key));
2727 down_read(&obj->oo_ext_idx_sem);
2728 bh = ldiskfs_find_entry(dentry, &de);
2730 ino = le32_to_cpu(de->inode);
2733 id->oii_gen = OSD_OII_NOGEN;
2735 inode = osd_iget(info, dev, id);
2736 if (!IS_ERR(inode)) {
2737 dentry->d_inode = inode;
2739 rc = osd_ea_fid_get(env, dentry, rec);
2742 rc = PTR_ERR(inode);
2746 up_read(&obj->oo_ext_idx_sem);
2751 * Find the osd object for given fid.
2753 * \param fid, need to find the osd object having this fid
2755 * \retval osd_object, on success
2756 * \retval -ve, on error
2758 struct osd_object *osd_object_find(const struct lu_env *env,
2759 struct dt_object *dt,
2760 const struct lu_fid *fid)
2762 struct lu_device *ludev = dt->do_lu.lo_dev;
2763 struct osd_object *child = NULL;
2764 struct lu_object *luch;
2765 struct lu_object *lo;
2767 luch = lu_object_find(env, ludev, fid, NULL);
2768 if (!IS_ERR(luch)) {
2769 if (lu_object_exists(luch)) {
2770 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2772 child = osd_obj(lo);
2774 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2775 "lu_object can't be located"
2776 ""DFID"\n", PFID(fid));
2778 if (child == NULL) {
2779 lu_object_put(env, luch);
2780 CERROR("Unable to get osd_object\n");
2781 child = ERR_PTR(-ENOENT);
2784 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2785 "lu_object does not exists "DFID"\n",
2787 child = ERR_PTR(-ENOENT);
2790 child = (void *)luch;
2796 * Put the osd object once done with it.
2798 * \param obj, osd object that needs to be put
2800 static inline void osd_object_put(const struct lu_env *env,
2801 struct osd_object *obj)
2803 lu_object_put(env, &obj->oo_dt.do_lu);
2807 * Index add function for interoperability mode (b11826).
2808 * It will add the directory entry.This entry is needed to
2809 * maintain name->fid mapping.
2811 * \param key, it is key i.e. file entry to be inserted
2812 * \param rec, it is value of given key i.e. fid
2814 * \retval 0, on success
2815 * \retval -ve, on error
2817 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2818 const struct dt_rec *rec,
2819 const struct dt_key *key, struct thandle *th,
2820 struct lustre_capa *capa, int ignore_quota)
2822 struct osd_object *obj = osd_dt_obj(dt);
2823 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
2824 const struct lu_fid_pack *pack = (const struct lu_fid_pack *)rec;
2825 const char *name = (const char *)key;
2826 struct osd_object *child;
2827 #ifdef HAVE_QUOTA_SUPPORT
2828 cfs_cap_t save = current->cap_effective;
2834 LASSERT(osd_invariant(obj));
2835 LASSERT(dt_object_exists(dt));
2836 LASSERT(th != NULL);
2838 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2841 rc = fid_unpack(pack, fid);
2844 child = osd_object_find(env, dt, fid);
2845 if (!IS_ERR(child)) {
2846 struct inode *inode = obj->oo_inode;
2847 struct osd_thread_info *oti = osd_oti_get(env);
2848 struct timespec *ctime = &oti->oti_time;
2849 struct timespec *mtime = &oti->oti_time2;
2851 *ctime = inode->i_ctime;
2852 *mtime = inode->i_mtime;
2853 #ifdef HAVE_QUOTA_SUPPORT
2855 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2857 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2859 down_write(&obj->oo_ext_idx_sem);
2860 rc = osd_ea_add_rec(env, obj, child, name, th);
2861 up_write(&obj->oo_ext_idx_sem);
2862 #ifdef HAVE_QUOTA_SUPPORT
2863 current->cap_effective = save;
2865 osd_object_put(env, child);
2866 /* xtime should not be updated with server-side time. */
2867 spin_lock(&obj->oo_guard);
2868 inode->i_ctime = *ctime;
2869 inode->i_mtime = *mtime;
2870 spin_unlock(&obj->oo_guard);
2871 mark_inode_dirty(inode);
2873 rc = PTR_ERR(child);
2876 LASSERT(osd_invariant(obj));
2881 * Initialize osd Iterator for given osd index object.
2883 * \param dt osd index object
2886 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2887 struct dt_object *dt,
2888 struct lustre_capa *capa)
2890 struct osd_it_iam *it;
2891 struct osd_thread_info *oti = osd_oti_get(env);
2892 struct osd_object *obj = osd_dt_obj(dt);
2893 struct lu_object *lo = &dt->do_lu;
2894 struct iam_path_descr *ipd;
2895 struct iam_container *bag = &obj->oo_dir->od_container;
2897 LASSERT(lu_object_exists(lo));
2899 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2900 return ERR_PTR(-EACCES);
2903 ipd = osd_it_ipd_get(env, bag);
2904 if (likely(ipd != NULL)) {
2908 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2909 return (struct dt_it *)it;
2911 return ERR_PTR(-ENOMEM);
2915 * free given Iterator.
2918 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2920 struct osd_it_iam *it = (struct osd_it_iam *)di;
2921 struct osd_object *obj = it->oi_obj;
2923 iam_it_fini(&it->oi_it);
2924 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2925 lu_object_put(env, &obj->oo_dt.do_lu);
2929 * Move Iterator to record specified by \a key
2931 * \param di osd iterator
2932 * \param key key for index
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_get(const struct lu_env *env,
2940 struct dt_it *di, const struct dt_key *key)
2942 struct osd_it_iam *it = (struct osd_it_iam *)di;
2944 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2950 * \param di osd iterator
2953 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2955 struct osd_it_iam *it = (struct osd_it_iam *)di;
2957 iam_it_put(&it->oi_it);
2961 * Move iterator by one record
2963 * \param di osd iterator
2965 * \retval +1 end of container reached
2967 * \retval -ve failure
2970 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2972 struct osd_it_iam *it = (struct osd_it_iam *)di;
2974 return iam_it_next(&it->oi_it);
2978 * Return pointer to the key under iterator.
2981 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2982 const struct dt_it *di)
2984 struct osd_it_iam *it = (struct osd_it_iam *)di;
2986 return (struct dt_key *)iam_it_key_get(&it->oi_it);
2990 * Return size of key under iterator (in bytes)
2993 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
2995 struct osd_it_iam *it = (struct osd_it_iam *)di;
2997 return iam_it_key_size(&it->oi_it);
3001 * Return pointer to the record under iterator.
3003 static struct dt_rec *osd_it_iam_rec(const struct lu_env *env,
3004 const struct dt_it *di)
3006 struct osd_it_iam *it = (struct osd_it_iam *)di;
3008 return (struct dt_rec *)iam_it_rec_get(&it->oi_it);
3012 * Returns cookie for current Iterator position.
3014 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3016 struct osd_it_iam *it = (struct osd_it_iam *)di;
3018 return iam_it_store(&it->oi_it);
3022 * Restore iterator from cookie.
3024 * \param di osd iterator
3025 * \param hash Iterator location cookie
3027 * \retval +ve di points to record with least key not larger than key.
3028 * \retval 0 di points to exact matched key
3029 * \retval -ve failure
3032 static int osd_it_iam_load(const struct lu_env *env,
3033 const struct dt_it *di, __u64 hash)
3035 struct osd_it_iam *it = (struct osd_it_iam *)di;
3037 return iam_it_load(&it->oi_it, hash);
3040 static const struct dt_index_operations osd_index_iam_ops = {
3041 .dio_lookup = osd_index_iam_lookup,
3042 .dio_insert = osd_index_iam_insert,
3043 .dio_delete = osd_index_iam_delete,
3045 .init = osd_it_iam_init,
3046 .fini = osd_it_iam_fini,
3047 .get = osd_it_iam_get,
3048 .put = osd_it_iam_put,
3049 .next = osd_it_iam_next,
3050 .key = osd_it_iam_key,
3051 .key_size = osd_it_iam_key_size,
3052 .rec = osd_it_iam_rec,
3053 .store = osd_it_iam_store,
3054 .load = osd_it_iam_load
3059 * Creates or initializes iterator context.
3061 * \retval struct osd_it_ea, iterator structure on success
3064 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3065 struct dt_object *dt,
3066 struct lustre_capa *capa)
3068 struct osd_object *obj = osd_dt_obj(dt);
3069 struct osd_thread_info *info = osd_oti_get(env);
3070 struct osd_it_ea *it = &info->oti_it_ea;
3071 struct lu_object *lo = &dt->do_lu;
3072 struct dentry *obj_dentry = &info->oti_it_dentry;
3074 LASSERT(lu_object_exists(lo));
3076 obj_dentry->d_inode = obj->oo_inode;
3077 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3078 obj_dentry->d_name.hash = 0;
3080 it->oie_rd_dirent = 0;
3081 it->oie_it_dirent = 0;
3082 it->oie_curr_pos = 0;
3083 it->oie_next_pos = 0;
3084 it->oie_dirent = NULL;
3085 it->oie_buf = info->oti_it_ea_buf;
3087 it->oie_file.f_dentry = obj_dentry;
3088 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3089 it->oie_file.f_op = obj->oo_inode->i_fop;
3090 it->oie_file.private_data = NULL;
3092 RETURN((struct dt_it*) it);
3096 * Destroy or finishes iterator context.
3098 * \param di, struct osd_it_ea, iterator structure to be destroyed
3100 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3102 struct osd_it_ea *it = (struct osd_it_ea *)di;
3103 struct osd_object *obj = it->oie_obj;
3104 struct inode *inode = obj->oo_inode;
3107 it->oie_file.f_op->release(inode, &it->oie_file);
3108 lu_object_put(env, &obj->oo_dt.do_lu);
3113 * It position the iterator at given key, so that next lookup continues from
3114 * that key Or it is similar to dio_it->load() but based on a key,
3115 * rather than file position.
3117 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3120 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3122 static int osd_it_ea_get(const struct lu_env *env,
3123 struct dt_it *di, const struct dt_key *key)
3125 struct osd_it_ea *it = (struct osd_it_ea *)di;
3128 LASSERT(((const char *)key)[0] == '\0');
3129 it->oie_curr_pos = 0;
3130 it->oie_next_pos = 0;
3131 it->oie_rd_dirent = 0;
3132 it->oie_it_dirent = 0;
3133 it->oie_dirent = NULL;
3141 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3146 * It is called internally by ->readdir(). It fills the
3147 * iterator's in-memory data structure with required
3148 * information i.e. name, namelen, rec_size etc.
3150 * \param buf, in which information to be filled in.
3151 * \param name, name of the file in given dir
3153 * \retval 0, on success
3154 * \retval 1, on buffer full
3156 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3157 loff_t offset, ino_t ino,
3158 unsigned int d_type)
3160 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3161 struct osd_it_ea_dirent *ent = it->oie_dirent;
3164 /* this should never happen */
3165 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3166 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3170 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3174 ent->oied_ino = ino;
3175 ent->oied_off = offset;
3176 ent->oied_namelen = namelen;
3177 memcpy(ent->oied_name, name, namelen);
3179 it->oie_rd_dirent++;
3180 it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3185 * Calls ->readdir() to load a directory entry at a time
3186 * and stored it in iterator's in-memory data structure.
3188 * \param di, struct osd_it_ea, iterator's in memory structure
3190 * \retval 0, on success
3191 * \retval -ve, on error
3193 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3195 struct osd_it_ea *it = (struct osd_it_ea *)di;
3196 struct osd_object *obj = it->oie_obj;
3197 struct inode *inode = obj->oo_inode;
3201 it->oie_dirent = it->oie_buf;
3202 it->oie_rd_dirent = 0;
3203 it->oie_file.f_pos = it->oie_curr_pos;
3205 down_read(&obj->oo_ext_idx_sem);
3206 result = inode->i_fop->readdir(&it->oie_file, it,
3207 (filldir_t) osd_ldiskfs_filldir);
3209 up_read(&obj->oo_ext_idx_sem);
3210 it->oie_next_pos = it->oie_file.f_pos;
3212 if (it->oie_rd_dirent == 0) {
3215 it->oie_dirent = it->oie_buf;
3216 it->oie_it_dirent = 1;
3223 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3224 * to load a directory entry at a time and stored it in
3225 * iterator's in-memory data structure.
3227 * \param di, struct osd_it_ea, iterator's in memory structure
3229 * \retval +ve, iterator reached to end
3230 * \retval 0, iterator not reached to end
3231 * \retval -ve, on error
3233 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3235 struct osd_it_ea *it = (struct osd_it_ea *)di;
3240 if (it->oie_it_dirent < it->oie_rd_dirent) {
3241 it->oie_dirent = (void *) it->oie_dirent +
3242 size_round(sizeof(struct osd_it_ea_dirent) +
3243 it->oie_dirent->oied_namelen);
3244 it->oie_it_dirent++;
3247 it->oie_curr_pos = it->oie_next_pos;
3249 if (it->oie_curr_pos == LDISKFS_HTREE_EOF)
3252 rc = osd_ldiskfs_it_fill(di);
3259 * Returns the key at current position from iterator's in memory structure.
3261 * \param di, struct osd_it_ea, iterator's in memory structure
3263 * \retval key i.e. struct dt_key on success
3265 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3266 const struct dt_it *di)
3268 struct osd_it_ea *it = (struct osd_it_ea *)di;
3270 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3274 * Returns the key's size at current position from iterator's in memory structure.
3276 * \param di, struct osd_it_ea, iterator's in memory structure
3278 * \retval key_size i.e. struct dt_key on success
3280 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3282 struct osd_it_ea *it = (struct osd_it_ea *)di;
3284 RETURN(it->oie_dirent->oied_namelen);
3288 * Returns the value (i.e. fid/igif) at current position from iterator's
3289 * in memory structure.
3291 * \param di, struct osd_it_ea, iterator's in memory structure
3293 * \retval value i.e. struct dt_rec on success
3295 static struct dt_rec *osd_it_ea_rec(const struct lu_env *env,
3296 const struct dt_it *di)
3298 struct osd_it_ea *it = (struct osd_it_ea *)di;
3299 struct osd_object *obj = it->oie_obj;
3300 struct osd_thread_info *info = osd_oti_get(env);
3301 struct osd_inode_id *id = &info->oti_id;
3302 struct lu_fid_pack *rec = &info->oti_pack;
3303 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3304 struct dentry *dentry = &info->oti_child_dentry;
3305 struct osd_device *dev;
3306 struct inode *inode;
3310 dev = osd_dev(ldev);
3311 id->oii_ino = it->oie_dirent->oied_ino;
3312 id->oii_gen = OSD_OII_NOGEN;
3313 inode = osd_iget(info, dev, id);
3314 if (!IS_ERR(inode)) {
3315 dentry->d_inode = inode;
3316 LASSERT(dentry->d_inode->i_sb == osd_sb(dev));
3318 RETURN((struct dt_rec *) PTR_ERR(inode));
3321 rc = osd_ea_fid_get(env, dentry, (struct dt_rec*) rec);
3326 RETURN((struct dt_rec *)rec);
3331 * Returns a cookie for current position of the iterator head, so that
3332 * user can use this cookie to load/start the iterator next time.
3334 * \param di, struct osd_it_ea, iterator's in memory structure
3336 * \retval cookie for current position, on success
3338 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3340 struct osd_it_ea *it = (struct osd_it_ea *)di;
3342 RETURN(it->oie_dirent->oied_off);
3346 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3347 * to load a directory entry at a time and stored it i inn,
3348 * in iterator's in-memory data structure.
3350 * \param di, struct osd_it_ea, iterator's in memory structure
3352 * \retval +ve, on success
3353 * \retval -ve, on error
3355 static int osd_it_ea_load(const struct lu_env *env,
3356 const struct dt_it *di, __u64 hash)
3358 struct osd_it_ea *it = (struct osd_it_ea *)di;
3362 it->oie_curr_pos = hash;
3364 rc = osd_ldiskfs_it_fill(di);
3371 * Index and Iterator operations for interoperability
3372 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3374 static const struct dt_index_operations osd_index_ea_ops = {
3375 .dio_lookup = osd_index_ea_lookup,
3376 .dio_insert = osd_index_ea_insert,
3377 .dio_delete = osd_index_ea_delete,
3379 .init = osd_it_ea_init,
3380 .fini = osd_it_ea_fini,
3381 .get = osd_it_ea_get,
3382 .put = osd_it_ea_put,
3383 .next = osd_it_ea_next,
3384 .key = osd_it_ea_key,
3385 .key_size = osd_it_ea_key_size,
3386 .rec = osd_it_ea_rec,
3387 .store = osd_it_ea_store,
3388 .load = osd_it_ea_load
3393 * Index lookup function for interoperability mode (b11826).
3395 * \param key, key i.e. file name to be searched
3397 * \retval +ve, on success
3398 * \retval -ve, on error
3400 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3401 struct dt_rec *rec, const struct dt_key *key,
3402 struct lustre_capa *capa)
3404 struct osd_object *obj = osd_dt_obj(dt);
3409 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3410 LINVRNT(osd_invariant(obj));
3412 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3415 rc = osd_ea_lookup_rec(env, obj, rec, key);
3422 /* type constructor/destructor: osd_type_init, osd_type_fini */
3423 LU_TYPE_INIT_FINI(osd, &osd_key);
3425 static struct lu_context_key osd_key = {
3426 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3427 .lct_init = osd_key_init,
3428 .lct_fini = osd_key_fini,
3429 .lct_exit = osd_key_exit
3432 static void *osd_key_init(const struct lu_context *ctx,
3433 struct lu_context_key *key)
3435 struct osd_thread_info *info;
3437 OBD_ALLOC_PTR(info);
3439 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3440 if (info->oti_it_ea_buf != NULL) {
3441 info->oti_env = container_of(ctx, struct lu_env,
3445 info = ERR_PTR(-ENOMEM);
3448 info = ERR_PTR(-ENOMEM);
3453 static void osd_key_fini(const struct lu_context *ctx,
3454 struct lu_context_key *key, void* data)
3456 struct osd_thread_info *info = data;
3458 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3462 static void osd_key_exit(const struct lu_context *ctx,
3463 struct lu_context_key *key, void *data)
3465 struct osd_thread_info *info = data;
3467 LASSERT(info->oti_r_locks == 0);
3468 LASSERT(info->oti_w_locks == 0);
3469 LASSERT(info->oti_txns == 0);
3472 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3473 const char *name, struct lu_device *next)
3476 struct lu_context *ctx;
3478 /* context for commit hooks */
3479 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3480 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3482 rc = osd_procfs_init(osd_dev(d), name);
3483 ctx->lc_cookie = 0x3;
3488 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3490 struct osd_thread_info *info = osd_oti_get(env);
3492 if (o->od_obj_area != NULL) {
3493 lu_object_put(env, &o->od_obj_area->do_lu);
3494 o->od_obj_area = NULL;
3496 osd_oi_fini(info, &o->od_oi);
3501 static int osd_mount(const struct lu_env *env,
3502 struct osd_device *o, struct lustre_cfg *cfg)
3504 struct lustre_mount_info *lmi;
3505 const char *dev = lustre_cfg_string(cfg, 0);
3506 struct lustre_disk_data *ldd;
3507 struct lustre_sb_info *lsi;
3511 if (o->od_mount != NULL) {
3512 CERROR("Already mounted (%s)\n", dev);
3517 lmi = server_get_mount(dev);
3519 CERROR("Cannot get mount info for %s!\n", dev);
3523 LASSERT(lmi != NULL);
3524 /* save lustre_mount_info in dt_device */
3527 lsi = s2lsi(lmi->lmi_sb);
3530 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3532 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3536 o->od_obj_area = NULL;
3540 static struct lu_device *osd_device_fini(const struct lu_env *env,
3541 struct lu_device *d)
3546 shrink_dcache_sb(osd_sb(osd_dev(d)));
3547 osd_sync(env, lu2dt_dev(d));
3549 rc = osd_procfs_fini(osd_dev(d));
3551 CERROR("proc fini error %d \n", rc);
3552 RETURN (ERR_PTR(rc));
3555 if (osd_dev(d)->od_mount)
3556 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3557 osd_dev(d)->od_mount->lmi_mnt);
3558 osd_dev(d)->od_mount = NULL;
3560 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3564 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3565 struct lu_device_type *t,
3566 struct lustre_cfg *cfg)
3568 struct lu_device *l;
3569 struct osd_device *o;
3575 result = dt_device_init(&o->od_dt_dev, t);
3578 l->ld_ops = &osd_lu_ops;
3579 o->od_dt_dev.dd_ops = &osd_dt_ops;
3580 spin_lock_init(&o->od_osfs_lock);
3581 o->od_osfs_age = cfs_time_shift_64(-1000);
3582 o->od_capa_hash = init_capa_hash();
3583 if (o->od_capa_hash == NULL) {
3584 dt_device_fini(&o->od_dt_dev);
3585 l = ERR_PTR(-ENOMEM);
3588 l = ERR_PTR(result);
3593 l = ERR_PTR(-ENOMEM);
3597 static struct lu_device *osd_device_free(const struct lu_env *env,
3598 struct lu_device *d)
3600 struct osd_device *o = osd_dev(d);
3603 cleanup_capa_hash(o->od_capa_hash);
3604 dt_device_fini(&o->od_dt_dev);
3609 static int osd_process_config(const struct lu_env *env,
3610 struct lu_device *d, struct lustre_cfg *cfg)
3612 struct osd_device *o = osd_dev(d);
3616 switch(cfg->lcfg_command) {
3618 err = osd_mount(env, o, cfg);
3621 err = osd_shutdown(env, o);
3630 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
3631 struct ldiskfs_super_block * es);
3633 static int osd_recovery_complete(const struct lu_env *env,
3634 struct lu_device *d)
3636 struct osd_device *o = osd_dev(d);
3638 /* TODO: orphans handling */
3639 ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3643 static int osd_prepare(const struct lu_env *env,
3644 struct lu_device *pdev,
3645 struct lu_device *dev)
3647 struct osd_device *osd = osd_dev(dev);
3648 struct lustre_sb_info *lsi;
3649 struct lustre_disk_data *ldd;
3650 struct lustre_mount_info *lmi;
3651 struct osd_thread_info *oti = osd_oti_get(env);
3652 struct dt_object *d;
3656 /* 1. initialize oi before any file create or file open */
3657 result = osd_oi_init(oti, &osd->od_oi,
3658 &osd->od_dt_dev, lu2md_dev(pdev));
3662 lmi = osd->od_mount;
3663 lsi = s2lsi(lmi->lmi_sb);
3666 /* 2. setup local objects */
3667 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3671 /* 3. open remote object dir */
3672 d = dt_store_open(env, lu2dt_dev(dev), "",
3673 remote_obj_dir, &oti->oti_fid);
3675 osd->od_obj_area = d;
3678 result = PTR_ERR(d);
3679 osd->od_obj_area = NULL;
3686 static struct inode *osd_iget(struct osd_thread_info *info,
3687 struct osd_device *dev,
3688 const struct osd_inode_id *id)
3690 struct inode *inode;
3692 inode = iget(osd_sb(dev), id->oii_ino);
3693 if (inode == NULL) {
3694 CERROR("no inode\n");
3695 inode = ERR_PTR(-EACCES);
3696 } else if (id->oii_gen != OSD_OII_NOGEN &&
3697 inode->i_generation != id->oii_gen) {
3699 inode = ERR_PTR(-ESTALE);
3700 } else if (inode->i_nlink == 0) {
3701 /* due to parallel readdir and unlink,
3702 * we can have dead inode here. */
3703 CWARN("stale inode\n");
3704 make_bad_inode(inode);
3706 inode = ERR_PTR(-ESTALE);
3707 } else if (is_bad_inode(inode)) {
3708 CERROR("bad inode %lx\n",inode->i_ino);
3710 inode = ERR_PTR(-ENOENT);
3716 static int osd_fid_lookup(const struct lu_env *env,
3717 struct osd_object *obj, const struct lu_fid *fid)
3719 struct osd_thread_info *info;
3720 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3721 struct osd_device *dev;
3722 struct osd_inode_id *id;
3724 struct inode *inode;
3727 LINVRNT(osd_invariant(obj));
3728 LASSERT(obj->oo_inode == NULL);
3729 LASSERT(fid_is_sane(fid));
3731 * This assertion checks that osd layer sees only local
3732 * fids. Unfortunately it is somewhat expensive (does a
3733 * cache-lookup). Disabling it for production/acceptance-testing.
3735 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
3739 info = osd_oti_get(env);
3740 dev = osd_dev(ldev);
3744 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
3747 result = osd_oi_lookup(info, oi, fid, id);
3749 inode = osd_iget(info, dev, id);
3750 if (!IS_ERR(inode)) {
3751 obj->oo_inode = inode;
3752 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
3753 if (dev->od_iop_mode) {
3754 obj->oo_compat_dot_created = 1;
3755 obj->oo_compat_dotdot_created = 1;
3760 * If fid wasn't found in oi, inode-less object is
3761 * created, for which lu_object_exists() returns
3762 * false. This is used in a (frequent) case when
3763 * objects are created as locking anchors or
3764 * place holders for objects yet to be created.
3766 result = PTR_ERR(inode);
3767 } else if (result == -ENOENT)
3769 LINVRNT(osd_invariant(obj));
3774 static void osd_inode_getattr(const struct lu_env *env,
3775 struct inode *inode, struct lu_attr *attr)
3777 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
3778 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
3779 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
3781 attr->la_atime = LTIME_S(inode->i_atime);
3782 attr->la_mtime = LTIME_S(inode->i_mtime);
3783 attr->la_ctime = LTIME_S(inode->i_ctime);
3784 attr->la_mode = inode->i_mode;
3785 attr->la_size = i_size_read(inode);
3786 attr->la_blocks = inode->i_blocks;
3787 attr->la_uid = inode->i_uid;
3788 attr->la_gid = inode->i_gid;
3789 attr->la_flags = LDISKFS_I(inode)->i_flags;
3790 attr->la_nlink = inode->i_nlink;
3791 attr->la_rdev = inode->i_rdev;
3792 attr->la_blksize = ll_inode_blksize(inode);
3793 attr->la_blkbits = inode->i_blkbits;
3800 static int lu_device_is_osd(const struct lu_device *d)
3802 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
3805 static struct osd_object *osd_obj(const struct lu_object *o)
3807 LASSERT(lu_device_is_osd(o->lo_dev));
3808 return container_of0(o, struct osd_object, oo_dt.do_lu);
3811 static struct osd_device *osd_dt_dev(const struct dt_device *d)
3813 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
3814 return container_of0(d, struct osd_device, od_dt_dev);
3817 static struct osd_device *osd_dev(const struct lu_device *d)
3819 LASSERT(lu_device_is_osd(d));
3820 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
3823 static struct osd_object *osd_dt_obj(const struct dt_object *d)
3825 return osd_obj(&d->do_lu);
3828 static struct osd_device *osd_obj2dev(const struct osd_object *o)
3830 return osd_dev(o->oo_dt.do_lu.lo_dev);
3833 static struct lu_device *osd2lu_dev(struct osd_device *osd)
3835 return &osd->od_dt_dev.dd_lu_dev;
3838 static struct super_block *osd_sb(const struct osd_device *dev)
3840 return dev->od_mount->lmi_mnt->mnt_sb;
3843 static journal_t *osd_journal(const struct osd_device *dev)
3845 return LDISKFS_SB(osd_sb(dev))->s_journal;
3848 static int osd_has_index(const struct osd_object *obj)
3850 return obj->oo_dt.do_index_ops != NULL;
3853 static int osd_object_invariant(const struct lu_object *l)
3855 return osd_invariant(osd_obj(l));
3858 static const struct lu_object_operations osd_lu_obj_ops = {
3859 .loo_object_init = osd_object_init,
3860 .loo_object_delete = osd_object_delete,
3861 .loo_object_release = osd_object_release,
3862 .loo_object_free = osd_object_free,
3863 .loo_object_print = osd_object_print,
3864 .loo_object_invariant = osd_object_invariant
3867 static const struct lu_device_operations osd_lu_ops = {
3868 .ldo_object_alloc = osd_object_alloc,
3869 .ldo_process_config = osd_process_config,
3870 .ldo_recovery_complete = osd_recovery_complete,
3871 .ldo_prepare = osd_prepare,
3874 static const struct lu_device_type_operations osd_device_type_ops = {
3875 .ldto_init = osd_type_init,
3876 .ldto_fini = osd_type_fini,
3878 .ldto_start = osd_type_start,
3879 .ldto_stop = osd_type_stop,
3881 .ldto_device_alloc = osd_device_alloc,
3882 .ldto_device_free = osd_device_free,
3884 .ldto_device_init = osd_device_init,
3885 .ldto_device_fini = osd_device_fini
3888 static struct lu_device_type osd_device_type = {
3889 .ldt_tags = LU_DEVICE_DT,
3890 .ldt_name = LUSTRE_OSD_NAME,
3891 .ldt_ops = &osd_device_type_ops,
3892 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3896 * lprocfs legacy support.
3898 static struct obd_ops osd_obd_device_ops = {
3899 .o_owner = THIS_MODULE
3902 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3903 .llod_name = remote_obj_dir,
3904 .llod_oid = OSD_REM_OBJ_DIR_OID,
3906 .llod_feat = &dt_directory_features,
3909 static int __init osd_mod_init(void)
3911 struct lprocfs_static_vars lvars;
3914 llo_local_obj_register(&llod_osd_rem_obj_dir);
3915 lprocfs_osd_init_vars(&lvars);
3916 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3917 LUSTRE_OSD_NAME, &osd_device_type);
3920 static void __exit osd_mod_exit(void)
3922 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3923 class_unregister_type(LUSTRE_OSD_NAME);
3926 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3927 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3928 MODULE_LICENSE("GPL");
3930 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);