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 osd_object_write_locked(const struct lu_env *env,
1103 struct dt_object *dt)
1105 struct osd_object *obj = osd_dt_obj(dt);
1107 LINVRNT(osd_invariant(obj));
1109 return obj->oo_owner == env;
1112 static int capa_is_sane(const struct lu_env *env,
1113 struct osd_device *dev,
1114 struct lustre_capa *capa,
1115 struct lustre_capa_key *keys)
1117 struct osd_thread_info *oti = osd_oti_get(env);
1118 struct lustre_capa *tcapa = &oti->oti_capa;
1119 struct obd_capa *oc;
1123 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1125 if (capa_is_expired(oc)) {
1126 DEBUG_CAPA(D_ERROR, capa, "expired");
1133 if (capa_is_expired_sec(capa)) {
1134 DEBUG_CAPA(D_ERROR, capa, "expired");
1138 spin_lock(&capa_lock);
1139 for (i = 0; i < 2; i++) {
1140 if (keys[i].lk_keyid == capa->lc_keyid) {
1141 oti->oti_capa_key = keys[i];
1145 spin_unlock(&capa_lock);
1148 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1152 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1156 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1157 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1161 oc = capa_add(dev->od_capa_hash, capa);
1167 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1168 struct lustre_capa *capa, __u64 opc)
1170 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1171 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1172 struct md_capainfo *ci;
1175 if (!dev->od_fl_capa)
1178 if (capa == BYPASS_CAPA)
1181 ci = md_capainfo(env);
1185 if (ci->mc_auth == LC_ID_NONE)
1189 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1193 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1194 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1199 if (!capa_opc_supported(capa, opc)) {
1200 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1204 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1205 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1212 static int osd_attr_get(const struct lu_env *env,
1213 struct dt_object *dt,
1214 struct lu_attr *attr,
1215 struct lustre_capa *capa)
1217 struct osd_object *obj = osd_dt_obj(dt);
1219 LASSERT(dt_object_exists(dt));
1220 LINVRNT(osd_invariant(obj));
1222 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1225 spin_lock(&obj->oo_guard);
1226 osd_inode_getattr(env, obj->oo_inode, attr);
1227 spin_unlock(&obj->oo_guard);
1231 static int osd_attr_set(const struct lu_env *env,
1232 struct dt_object *dt,
1233 const struct lu_attr *attr,
1234 struct thandle *handle,
1235 struct lustre_capa *capa)
1237 struct osd_object *obj = osd_dt_obj(dt);
1240 LASSERT(handle != NULL);
1241 LASSERT(dt_object_exists(dt));
1242 LASSERT(osd_invariant(obj));
1244 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1247 spin_lock(&obj->oo_guard);
1248 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1249 spin_unlock(&obj->oo_guard);
1252 mark_inode_dirty(obj->oo_inode);
1256 static struct timespec *osd_inode_time(const struct lu_env *env,
1257 struct inode *inode, __u64 seconds)
1259 struct osd_thread_info *oti = osd_oti_get(env);
1260 struct timespec *t = &oti->oti_time;
1262 t->tv_sec = seconds;
1264 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1268 static int osd_inode_setattr(const struct lu_env *env,
1269 struct inode *inode, const struct lu_attr *attr)
1273 bits = attr->la_valid;
1275 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1277 #ifdef HAVE_QUOTA_SUPPORT
1278 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1279 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1280 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1286 iattr.ia_valid |= ATTR_UID;
1288 iattr.ia_valid |= ATTR_GID;
1289 iattr.ia_uid = attr->la_uid;
1290 iattr.ia_gid = attr->la_gid;
1291 osd_push_ctxt(env, save);
1292 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1299 if (bits & LA_ATIME)
1300 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1301 if (bits & LA_CTIME)
1302 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1303 if (bits & LA_MTIME)
1304 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1305 if (bits & LA_SIZE) {
1306 LDISKFS_I(inode)->i_disksize = attr->la_size;
1307 i_size_write(inode, attr->la_size);
1310 /* OSD should not change "i_blocks" which is used by quota.
1311 * "i_blocks" should be changed by ldiskfs only.
1312 * Enable this assignment for SOM purpose now, until it is
1313 * stored in SOM EA. */
1314 if (bits & LA_BLOCKS)
1315 inode->i_blocks = attr->la_blocks;
1318 inode->i_mode = (inode->i_mode & S_IFMT) |
1319 (attr->la_mode & ~S_IFMT);
1321 inode->i_uid = attr->la_uid;
1323 inode->i_gid = attr->la_gid;
1324 if (bits & LA_NLINK)
1325 inode->i_nlink = attr->la_nlink;
1327 inode->i_rdev = attr->la_rdev;
1329 if (bits & LA_FLAGS) {
1330 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1332 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1333 (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1341 * XXX temporary solution.
1344 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1345 struct lu_attr *attr, struct thandle *th)
1350 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1351 struct lu_attr *attr, struct thandle *th)
1353 osd_object_init0(obj);
1357 extern struct inode *ldiskfs_create_inode(handle_t *handle,
1358 struct inode * dir, int mode);
1359 extern int ldiskfs_add_entry(handle_t *handle, struct dentry *dentry,
1360 struct inode *inode);
1361 extern int ldiskfs_delete_entry(handle_t *handle,
1363 struct ldiskfs_dir_entry_2 * de_del,
1364 struct buffer_head * bh);
1365 extern struct buffer_head * ldiskfs_find_entry(struct dentry *dentry,
1366 struct ldiskfs_dir_entry_2
1368 extern int ldiskfs_add_dot_dotdot(handle_t *handle, struct inode *dir,
1369 struct inode *inode);
1371 extern int ldiskfs_xattr_set_handle(handle_t *handle, struct inode *inode,
1372 int name_index, const char *name,
1373 const void *value, size_t value_len,
1376 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1377 struct osd_object *obj,
1381 struct osd_thread_info *info = osd_oti_get(env);
1382 struct dentry *child_dentry = &info->oti_child_dentry;
1383 struct dentry *obj_dentry = &info->oti_obj_dentry;
1385 obj_dentry->d_inode = obj->oo_inode;
1386 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1387 obj_dentry->d_name.hash = 0;
1389 child_dentry->d_name.hash = 0;
1390 child_dentry->d_parent = obj_dentry;
1391 child_dentry->d_name.name = name;
1392 child_dentry->d_name.len = namelen;
1393 return child_dentry;
1397 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1399 struct dt_allocation_hint *hint,
1403 struct osd_device *osd = osd_obj2dev(obj);
1404 struct osd_thandle *oth;
1405 struct dt_object *parent;
1406 struct inode *inode;
1407 #ifdef HAVE_QUOTA_SUPPORT
1408 struct osd_ctxt *save = &info->oti_ctxt;
1411 LINVRNT(osd_invariant(obj));
1412 LASSERT(obj->oo_inode == NULL);
1414 oth = container_of(th, struct osd_thandle, ot_super);
1415 LASSERT(oth->ot_handle->h_transaction != NULL);
1417 if (hint && hint->dah_parent)
1418 parent = hint->dah_parent;
1420 parent = osd->od_obj_area;
1422 LASSERT(parent != NULL);
1423 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1425 #ifdef HAVE_QUOTA_SUPPORT
1426 osd_push_ctxt(info->oti_env, save);
1428 inode = ldiskfs_create_inode(oth->ot_handle,
1429 osd_dt_obj(parent)->oo_inode, mode);
1430 #ifdef HAVE_QUOTA_SUPPORT
1433 if (!IS_ERR(inode)) {
1434 obj->oo_inode = inode;
1437 result = PTR_ERR(inode);
1438 LINVRNT(osd_invariant(obj));
1443 extern int iam_lvar_create(struct inode *obj, int keysize, int ptrsize,
1444 int recsize, handle_t *handle);
1446 extern int iam_lfix_create(struct inode *obj, int keysize, int ptrsize,
1447 int recsize, handle_t *handle);
1454 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1455 struct lu_attr *attr,
1456 struct dt_allocation_hint *hint,
1457 struct dt_object_format *dof,
1461 struct osd_thandle *oth;
1462 struct osd_device *osd = osd_obj2dev(obj);
1463 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1465 LASSERT(S_ISDIR(attr->la_mode));
1467 oth = container_of(th, struct osd_thandle, ot_super);
1468 LASSERT(oth->ot_handle->h_transaction != NULL);
1469 result = osd_mkfile(info, obj, mode, hint, th);
1470 if (result == 0 && osd->od_iop_mode == 0) {
1471 LASSERT(obj->oo_inode != NULL);
1473 * XXX uh-oh... call low-level iam function directly.
1476 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1477 sizeof (struct lu_fid_pack),
1483 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1484 struct lu_attr *attr,
1485 struct dt_allocation_hint *hint,
1486 struct dt_object_format *dof,
1490 struct osd_thandle *oth;
1491 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1493 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1495 LASSERT(S_ISREG(attr->la_mode));
1497 oth = container_of(th, struct osd_thandle, ot_super);
1498 LASSERT(oth->ot_handle->h_transaction != NULL);
1500 result = osd_mkfile(info, obj, mode, hint, th);
1502 LASSERT(obj->oo_inode != NULL);
1503 if (feat->dif_flags & DT_IND_VARKEY)
1504 result = iam_lvar_create(obj->oo_inode,
1505 feat->dif_keysize_max,
1507 feat->dif_recsize_max,
1510 result = iam_lfix_create(obj->oo_inode,
1511 feat->dif_keysize_max,
1513 feat->dif_recsize_max,
1520 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1521 struct lu_attr *attr,
1522 struct dt_allocation_hint *hint,
1523 struct dt_object_format *dof,
1526 LASSERT(S_ISREG(attr->la_mode));
1527 return osd_mkfile(info, obj, (attr->la_mode &
1528 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1531 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1532 struct lu_attr *attr,
1533 struct dt_allocation_hint *hint,
1534 struct dt_object_format *dof,
1537 LASSERT(S_ISLNK(attr->la_mode));
1538 return osd_mkfile(info, obj, (attr->la_mode &
1539 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1542 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1543 struct lu_attr *attr,
1544 struct dt_allocation_hint *hint,
1545 struct dt_object_format *dof,
1548 umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1551 LINVRNT(osd_invariant(obj));
1552 LASSERT(obj->oo_inode == NULL);
1553 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1554 S_ISFIFO(mode) || S_ISSOCK(mode));
1556 result = osd_mkfile(info, obj, mode, hint, th);
1558 LASSERT(obj->oo_inode != NULL);
1559 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1561 LINVRNT(osd_invariant(obj));
1565 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1567 struct dt_allocation_hint *hint,
1568 struct dt_object_format *dof,
1571 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1573 osd_obj_type_f result;
1589 result = osd_mk_index;
1600 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1601 struct dt_object *parent, umode_t child_mode)
1605 memset(ah, 0, sizeof(*ah));
1606 ah->dah_parent = parent;
1607 ah->dah_mode = child_mode;
1611 * Helper function for osd_object_create()
1613 * \retval 0, on success
1615 static int __osd_object_create(struct osd_thread_info *info,
1616 struct osd_object *obj, struct lu_attr *attr,
1617 struct dt_allocation_hint *hint,
1618 struct dt_object_format *dof,
1624 result = osd_create_pre(info, obj, attr, th);
1626 result = osd_create_type_f(dof->dof_type)(info, obj,
1627 attr, hint, dof, th);
1629 result = osd_create_post(info, obj, attr, th);
1635 * Helper function for osd_object_create()
1637 * \retval 0, on success
1639 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1640 const struct lu_fid *fid, struct thandle *th)
1642 struct osd_thread_info *info = osd_oti_get(env);
1643 struct osd_inode_id *id = &info->oti_id;
1644 struct osd_device *osd = osd_obj2dev(obj);
1645 struct md_ucred *uc = md_ucred(env);
1647 LASSERT(obj->oo_inode != NULL);
1648 LASSERT(uc != NULL);
1650 id->oii_ino = obj->oo_inode->i_ino;
1651 id->oii_gen = obj->oo_inode->i_generation;
1653 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1654 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1657 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1658 struct lu_attr *attr,
1659 struct dt_allocation_hint *hint,
1660 struct dt_object_format *dof,
1663 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1664 struct osd_object *obj = osd_dt_obj(dt);
1665 struct osd_thread_info *info = osd_oti_get(env);
1670 LINVRNT(osd_invariant(obj));
1671 LASSERT(!dt_object_exists(dt));
1672 LASSERT(osd_write_locked(env, obj));
1673 LASSERT(th != NULL);
1675 result = __osd_object_create(info, obj, attr, hint, dof, th);
1677 result = __osd_oi_insert(env, obj, fid, th);
1679 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1680 LASSERT(osd_invariant(obj));
1685 * Helper function for osd_xattr_set()
1687 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1688 const struct lu_buf *buf, const char *name, int fl)
1690 struct osd_object *obj = osd_dt_obj(dt);
1691 struct inode *inode = obj->oo_inode;
1692 struct osd_thread_info *info = osd_oti_get(env);
1693 struct dentry *dentry = &info->oti_child_dentry;
1694 struct timespec *t = &info->oti_time;
1698 LASSERT(dt_object_exists(dt));
1699 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1700 LASSERT(osd_write_locked(env, obj));
1702 if (fl & LU_XATTR_REPLACE)
1703 fs_flags |= XATTR_REPLACE;
1705 if (fl & LU_XATTR_CREATE)
1706 fs_flags |= XATTR_CREATE;
1708 dentry->d_inode = inode;
1709 *t = inode->i_ctime;
1710 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1711 buf->lb_len, fs_flags);
1712 /* ctime should not be updated with server-side time. */
1713 spin_lock(&obj->oo_guard);
1714 inode->i_ctime = *t;
1715 spin_unlock(&obj->oo_guard);
1716 mark_inode_dirty(inode);
1721 * Put the fid into lustre_mdt_attrs, and then place the structure
1722 * inode's ea. This fid should not be altered during the life time
1725 * \retval +ve, on success
1726 * \retval -ve, on error
1728 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1730 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1731 const struct lu_fid *fid)
1733 struct osd_thread_info *info = osd_oti_get(env);
1734 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1736 fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1738 return __osd_xattr_set(env, dt,
1739 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1740 XATTR_NAME_LMA, LU_XATTR_CREATE);
1745 * Helper function to form igif
1747 static inline void osd_igif_get(const struct lu_env *env, struct dentry *dentry,
1750 struct inode *inode = dentry->d_inode;
1751 lu_igif_build(fid, inode->i_ino, inode->i_generation);
1755 * Helper function to pack the fid
1757 static inline void osd_fid_pack(const struct lu_env *env, const struct lu_fid *fid,
1758 struct lu_fid_pack *pack)
1760 fid_pack(pack, fid, &osd_oti_get(env)->oti_fid);
1764 * Try to read the fid from inode ea into dt_rec, if return value
1765 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1767 * \param rec, the data-structure into which fid/igif is read
1769 * \retval 0, on success
1771 static int osd_ea_fid_get(const struct lu_env *env, struct dentry *dentry,
1774 struct inode *inode = dentry->d_inode;
1775 struct osd_thread_info *info = osd_oti_get(env);
1776 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1777 struct lu_fid *fid = &info->oti_fid;
1780 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1782 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1785 /* Check LMA compatibility */
1787 (mdt_attrs->lma_incompat & ~cpu_to_be32(LMA_INCOMPAT_SUPP))) {
1788 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1789 inode->i_ino, be32_to_cpu(mdt_attrs->lma_incompat) &
1790 ~LMA_INCOMPAT_SUPP);
1795 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1797 } else if (rc == -ENODATA) {
1798 osd_igif_get(env, dentry, fid);
1803 osd_fid_pack(env, fid, (struct lu_fid_pack*)rec);
1809 * OSD layer object create function for interoperability mode (b11826).
1810 * This is mostly similar to osd_object_create(). Only difference being, fid is
1811 * inserted into inode ea here.
1813 * \retval 0, on success
1814 * \retval -ve, on error
1816 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1817 struct lu_attr *attr,
1818 struct dt_allocation_hint *hint,
1819 struct dt_object_format *dof,
1822 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1823 struct osd_object *obj = osd_dt_obj(dt);
1824 struct osd_thread_info *info = osd_oti_get(env);
1830 LASSERT(osd_invariant(obj));
1831 LASSERT(!dt_object_exists(dt));
1832 LASSERT(osd_write_locked(env, obj));
1833 LASSERT(th != NULL);
1835 result = __osd_object_create(info, obj, attr, hint, dof, th);
1837 if (hint && hint->dah_parent)
1838 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1840 /* objects under osd root shld have igif fid, so dont add fid EA */
1841 if (result == 0 && is_root == 0)
1842 result = osd_ea_fid_set(env, dt, fid);
1845 result = __osd_oi_insert(env, obj, fid, th);
1847 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1848 LINVRNT(osd_invariant(obj));
1853 * Concurrency: @dt is write locked.
1855 static void osd_object_ref_add(const struct lu_env *env,
1856 struct dt_object *dt,
1859 struct osd_object *obj = osd_dt_obj(dt);
1860 struct inode *inode = obj->oo_inode;
1862 LINVRNT(osd_invariant(obj));
1863 LASSERT(dt_object_exists(dt));
1864 LASSERT(osd_write_locked(env, obj));
1865 LASSERT(th != NULL);
1867 spin_lock(&obj->oo_guard);
1868 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1870 spin_unlock(&obj->oo_guard);
1871 mark_inode_dirty(inode);
1872 LINVRNT(osd_invariant(obj));
1876 * Concurrency: @dt is write locked.
1878 static void osd_object_ref_del(const struct lu_env *env,
1879 struct dt_object *dt,
1882 struct osd_object *obj = osd_dt_obj(dt);
1883 struct inode *inode = obj->oo_inode;
1885 LINVRNT(osd_invariant(obj));
1886 LASSERT(dt_object_exists(dt));
1887 LASSERT(osd_write_locked(env, obj));
1888 LASSERT(th != NULL);
1890 spin_lock(&obj->oo_guard);
1891 LASSERT(inode->i_nlink > 0);
1893 spin_unlock(&obj->oo_guard);
1894 mark_inode_dirty(inode);
1895 LINVRNT(osd_invariant(obj));
1899 * Concurrency: @dt is read locked.
1901 static int osd_xattr_get(const struct lu_env *env,
1902 struct dt_object *dt,
1905 struct lustre_capa *capa)
1907 struct osd_object *obj = osd_dt_obj(dt);
1908 struct inode *inode = obj->oo_inode;
1909 struct osd_thread_info *info = osd_oti_get(env);
1910 struct dentry *dentry = &info->oti_obj_dentry;
1912 LASSERT(dt_object_exists(dt));
1913 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1914 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1916 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1919 dentry->d_inode = inode;
1920 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1925 * Concurrency: @dt is write locked.
1927 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1928 const struct lu_buf *buf, const char *name, int fl,
1929 struct thandle *handle, struct lustre_capa *capa)
1931 LASSERT(handle != NULL);
1933 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1936 return __osd_xattr_set(env, dt, buf, name, fl);
1940 * Concurrency: @dt is read locked.
1942 static int osd_xattr_list(const struct lu_env *env,
1943 struct dt_object *dt,
1945 struct lustre_capa *capa)
1947 struct osd_object *obj = osd_dt_obj(dt);
1948 struct inode *inode = obj->oo_inode;
1949 struct osd_thread_info *info = osd_oti_get(env);
1950 struct dentry *dentry = &info->oti_obj_dentry;
1952 LASSERT(dt_object_exists(dt));
1953 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1954 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1956 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1959 dentry->d_inode = inode;
1960 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1964 * Concurrency: @dt is write locked.
1966 static int osd_xattr_del(const struct lu_env *env,
1967 struct dt_object *dt,
1969 struct thandle *handle,
1970 struct lustre_capa *capa)
1972 struct osd_object *obj = osd_dt_obj(dt);
1973 struct inode *inode = obj->oo_inode;
1974 struct osd_thread_info *info = osd_oti_get(env);
1975 struct dentry *dentry = &info->oti_obj_dentry;
1976 struct timespec *t = &info->oti_time;
1979 LASSERT(dt_object_exists(dt));
1980 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1981 LASSERT(osd_write_locked(env, obj));
1982 LASSERT(handle != NULL);
1984 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1987 dentry->d_inode = inode;
1988 *t = inode->i_ctime;
1989 rc = inode->i_op->removexattr(dentry, name);
1990 /* ctime should not be updated with server-side time. */
1991 spin_lock(&obj->oo_guard);
1992 inode->i_ctime = *t;
1993 spin_unlock(&obj->oo_guard);
1994 mark_inode_dirty(inode);
1998 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1999 struct dt_object *dt,
2000 struct lustre_capa *old,
2003 struct osd_thread_info *info = osd_oti_get(env);
2004 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2005 struct osd_object *obj = osd_dt_obj(dt);
2006 struct osd_device *dev = osd_obj2dev(obj);
2007 struct lustre_capa_key *key = &info->oti_capa_key;
2008 struct lustre_capa *capa = &info->oti_capa;
2009 struct obd_capa *oc;
2010 struct md_capainfo *ci;
2014 if (!dev->od_fl_capa)
2015 RETURN(ERR_PTR(-ENOENT));
2017 LASSERT(dt_object_exists(dt));
2018 LINVRNT(osd_invariant(obj));
2020 /* renewal sanity check */
2021 if (old && osd_object_auth(env, dt, old, opc))
2022 RETURN(ERR_PTR(-EACCES));
2024 ci = md_capainfo(env);
2026 RETURN(ERR_PTR(-ENOENT));
2028 switch (ci->mc_auth) {
2032 capa->lc_uid = obj->oo_inode->i_uid;
2033 capa->lc_gid = obj->oo_inode->i_gid;
2034 capa->lc_flags = LC_ID_PLAIN;
2036 case LC_ID_CONVERT: {
2039 s[0] = obj->oo_inode->i_uid;
2040 get_random_bytes(&(s[1]), sizeof(__u32));
2041 s[2] = obj->oo_inode->i_gid;
2042 get_random_bytes(&(s[3]), sizeof(__u32));
2043 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2045 RETURN(ERR_PTR(rc));
2047 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2048 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2049 capa->lc_flags = LC_ID_CONVERT;
2053 RETURN(ERR_PTR(-EINVAL));
2056 capa->lc_fid = *fid;
2058 capa->lc_flags |= dev->od_capa_alg << 24;
2059 capa->lc_timeout = dev->od_capa_timeout;
2060 capa->lc_expiry = 0;
2062 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2064 LASSERT(!capa_is_expired(oc));
2068 spin_lock(&capa_lock);
2069 *key = dev->od_capa_keys[1];
2070 spin_unlock(&capa_lock);
2072 capa->lc_keyid = key->lk_keyid;
2073 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2075 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2077 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2078 RETURN(ERR_PTR(rc));
2081 oc = capa_add(dev->od_capa_hash, capa);
2085 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2088 struct osd_object *obj = osd_dt_obj(dt);
2089 struct inode *inode = obj->oo_inode;
2090 struct osd_thread_info *info = osd_oti_get(env);
2091 struct dentry *dentry = &info->oti_obj_dentry;
2092 struct file *file = &info->oti_file;
2095 dentry->d_inode = inode;
2096 file->f_dentry = dentry;
2097 file->f_mapping = inode->i_mapping;
2098 file->f_op = inode->i_fop;
2099 LOCK_INODE_MUTEX(inode);
2100 rc = file->f_op->fsync(file, dentry, 0);
2101 UNLOCK_INODE_MUTEX(inode);
2106 * Get the 64-bit version for an inode.
2108 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2109 struct dt_object *dt)
2111 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2113 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2114 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2115 return LDISKFS_I(inode)->i_fs_version;
2119 * Set the 64-bit version and return the old version.
2121 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2122 dt_obj_version_t new_version)
2124 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2126 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2127 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2128 LDISKFS_I(inode)->i_fs_version = new_version;
2129 /** Version is set after all inode operations are finished,
2130 * so we should mark it dirty here */
2131 inode->i_sb->s_op->dirty_inode(inode);
2134 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2137 struct osd_object *obj = osd_dt_obj(dt);
2140 *data = (void *)obj->oo_inode;
2144 static const struct dt_object_operations osd_obj_ops = {
2145 .do_read_lock = osd_object_read_lock,
2146 .do_write_lock = osd_object_write_lock,
2147 .do_read_unlock = osd_object_read_unlock,
2148 .do_write_unlock = osd_object_write_unlock,
2149 .do_write_locked = osd_object_write_locked,
2150 .do_attr_get = osd_attr_get,
2151 .do_attr_set = osd_attr_set,
2152 .do_ah_init = osd_ah_init,
2153 .do_create = osd_object_create,
2154 .do_index_try = osd_index_try,
2155 .do_ref_add = osd_object_ref_add,
2156 .do_ref_del = osd_object_ref_del,
2157 .do_xattr_get = osd_xattr_get,
2158 .do_xattr_set = osd_xattr_set,
2159 .do_xattr_del = osd_xattr_del,
2160 .do_xattr_list = osd_xattr_list,
2161 .do_capa_get = osd_capa_get,
2162 .do_object_sync = osd_object_sync,
2163 .do_version_get = osd_object_version_get,
2164 .do_version_set = osd_object_version_set,
2165 .do_data_get = osd_data_get,
2169 * dt_object_operations for interoperability mode
2170 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2172 static const struct dt_object_operations osd_obj_ea_ops = {
2173 .do_read_lock = osd_object_read_lock,
2174 .do_write_lock = osd_object_write_lock,
2175 .do_read_unlock = osd_object_read_unlock,
2176 .do_write_unlock = osd_object_write_unlock,
2177 .do_write_locked = osd_object_write_locked,
2178 .do_attr_get = osd_attr_get,
2179 .do_attr_set = osd_attr_set,
2180 .do_ah_init = osd_ah_init,
2181 .do_create = osd_object_ea_create,
2182 .do_index_try = osd_index_try,
2183 .do_ref_add = osd_object_ref_add,
2184 .do_ref_del = osd_object_ref_del,
2185 .do_xattr_get = osd_xattr_get,
2186 .do_xattr_set = osd_xattr_set,
2187 .do_xattr_del = osd_xattr_del,
2188 .do_xattr_list = osd_xattr_list,
2189 .do_capa_get = osd_capa_get,
2190 .do_object_sync = osd_object_sync,
2191 .do_version_get = osd_object_version_get,
2192 .do_version_set = osd_object_version_set,
2193 .do_data_get = osd_data_get,
2201 * XXX: Another layering violation for now.
2203 * We don't want to use ->f_op->read methods, because generic file write
2205 * - serializes on ->i_sem, and
2207 * - does a lot of extra work like balance_dirty_pages(),
2209 * which doesn't work for globally shared files like /last-received.
2211 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2212 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2213 loff_t *offs, handle_t *handle);
2215 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2216 struct lu_buf *buf, loff_t *pos,
2217 struct lustre_capa *capa)
2219 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2221 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2224 return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2227 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2228 const struct lu_buf *buf, loff_t *pos,
2229 struct thandle *handle, struct lustre_capa *capa,
2232 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2233 struct osd_thandle *oh;
2235 #ifdef HAVE_QUOTA_SUPPORT
2236 cfs_cap_t save = current->cap_effective;
2239 LASSERT(handle != NULL);
2241 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2244 oh = container_of(handle, struct osd_thandle, ot_super);
2245 LASSERT(oh->ot_handle->h_transaction != NULL);
2246 #ifdef HAVE_QUOTA_SUPPORT
2248 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2250 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2252 result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2253 pos, oh->ot_handle);
2254 #ifdef HAVE_QUOTA_SUPPORT
2255 current->cap_effective = save;
2258 result = buf->lb_len;
2262 static const struct dt_body_operations osd_body_ops = {
2263 .dbo_read = osd_read,
2264 .dbo_write = osd_write
2271 static int osd_object_is_root(const struct osd_object *obj)
2273 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
2276 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2277 const struct dt_index_features *feat)
2279 struct iam_descr *descr;
2280 struct dt_object *dt = &o->oo_dt;
2282 if (osd_object_is_root(o))
2283 return feat == &dt_directory_features;
2285 LASSERT(o->oo_dir != NULL);
2287 descr = o->oo_dir->od_container.ic_descr;
2288 if (feat == &dt_directory_features) {
2289 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2292 if (descr == &iam_htree_compat_param) {
2293 /* if it is a HTREE dir then there is good chance that,
2294 * we dealing with ext3 directory here with no FIDs. */
2296 if (descr->id_rec_size ==
2297 sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2299 dt->do_index_ops = &osd_index_ea_ops;
2306 feat->dif_keysize_min <= descr->id_key_size &&
2307 descr->id_key_size <= feat->dif_keysize_max &&
2308 feat->dif_recsize_min <= descr->id_rec_size &&
2309 descr->id_rec_size <= feat->dif_recsize_max &&
2310 !(feat->dif_flags & (DT_IND_VARKEY |
2311 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2312 ergo(feat->dif_flags & DT_IND_UPDATE,
2313 1 /* XXX check that object (and file system) is
2318 static int osd_iam_container_init(const struct lu_env *env,
2319 struct osd_object *obj,
2320 struct osd_directory *dir)
2323 struct iam_container *bag;
2325 bag = &dir->od_container;
2326 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2328 result = iam_container_setup(bag);
2330 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2332 iam_container_fini(bag);
2338 * Concurrency: no external locking is necessary.
2340 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2341 const struct dt_index_features *feat)
2345 struct osd_object *obj = osd_dt_obj(dt);
2346 struct osd_device *osd = osd_obj2dev(obj);
2348 LINVRNT(osd_invariant(obj));
2349 LASSERT(dt_object_exists(dt));
2351 if (osd_object_is_root(obj)) {
2352 dt->do_index_ops = &osd_index_ea_ops;
2354 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2355 dt->do_index_ops = &osd_index_ea_ops;
2356 if (S_ISDIR(obj->oo_inode->i_mode))
2361 } else if (!osd_has_index(obj)) {
2362 struct osd_directory *dir;
2367 spin_lock(&obj->oo_guard);
2368 if (obj->oo_dir == NULL)
2372 * Concurrent thread allocated container data.
2375 spin_unlock(&obj->oo_guard);
2377 * Now, that we have container data, serialize its
2380 down_write(&obj->oo_ext_idx_sem);
2382 * recheck under lock.
2384 if (!osd_has_index(obj))
2385 result = osd_iam_container_init(env, obj, dir);
2388 up_write(&obj->oo_ext_idx_sem);
2394 if (result == 0 && ea_dir == 0) {
2395 if (!osd_iam_index_probe(env, obj, feat))
2398 LINVRNT(osd_invariant(obj));
2404 * delete a (key, value) pair from index \a dt specified by \a key
2406 * \param dt_object osd index object
2407 * \param key key for index
2408 * \param rec record reference
2409 * \param handle transaction handler
2412 * \retval -ve failure
2415 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2416 const struct dt_key *key, struct thandle *handle,
2417 struct lustre_capa *capa)
2419 struct osd_object *obj = osd_dt_obj(dt);
2420 struct osd_thandle *oh;
2421 struct iam_path_descr *ipd;
2422 struct iam_container *bag = &obj->oo_dir->od_container;
2427 LINVRNT(osd_invariant(obj));
2428 LASSERT(dt_object_exists(dt));
2429 LASSERT(bag->ic_object == obj->oo_inode);
2430 LASSERT(handle != NULL);
2432 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2435 ipd = osd_idx_ipd_get(env, bag);
2436 if (unlikely(ipd == NULL))
2439 oh = container_of0(handle, struct osd_thandle, ot_super);
2440 LASSERT(oh->ot_handle != NULL);
2441 LASSERT(oh->ot_handle->h_transaction != NULL);
2443 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2444 osd_ipd_put(env, bag, ipd);
2445 LINVRNT(osd_invariant(obj));
2450 * Index delete function for interoperability mode (b11826).
2451 * It will remove the directory entry added by osd_index_ea_insert().
2452 * This entry is needed to maintain name->fid mapping.
2454 * \param key, key i.e. file entry to be deleted
2456 * \retval 0, on success
2457 * \retval -ve, on error
2459 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2460 const struct dt_key *key, struct thandle *handle,
2461 struct lustre_capa *capa)
2463 struct osd_object *obj = osd_dt_obj(dt);
2464 struct inode *dir = obj->oo_inode;
2465 struct dentry *dentry;
2466 struct osd_thandle *oh;
2467 struct ldiskfs_dir_entry_2 *de;
2468 struct buffer_head *bh;
2474 LINVRNT(osd_invariant(obj));
2475 LASSERT(dt_object_exists(dt));
2476 LASSERT(handle != NULL);
2478 oh = container_of(handle, struct osd_thandle, ot_super);
2479 LASSERT(oh->ot_handle != NULL);
2480 LASSERT(oh->ot_handle->h_transaction != NULL);
2482 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2485 dentry = osd_child_dentry_get(env, obj,
2486 (char *)key, strlen((char *)key));
2488 down_write(&obj->oo_ext_idx_sem);
2489 bh = ldiskfs_find_entry(dentry, &de);
2491 struct osd_thread_info *oti = osd_oti_get(env);
2492 struct timespec *ctime = &oti->oti_time;
2493 struct timespec *mtime = &oti->oti_time2;
2495 *ctime = dir->i_ctime;
2496 *mtime = dir->i_mtime;
2497 rc = ldiskfs_delete_entry(oh->ot_handle,
2499 /* xtime should not be updated with server-side time. */
2500 spin_lock(&obj->oo_guard);
2501 dir->i_ctime = *ctime;
2502 dir->i_mtime = *mtime;
2503 spin_unlock(&obj->oo_guard);
2504 mark_inode_dirty(dir);
2509 up_write(&obj->oo_ext_idx_sem);
2510 LASSERT(osd_invariant(obj));
2515 * Lookup index for \a key and copy record to \a rec.
2517 * \param dt_object osd index object
2518 * \param key key for index
2519 * \param rec record reference
2521 * \retval +ve success : exact mach
2522 * \retval 0 return record with key not greater than \a key
2523 * \retval -ve failure
2525 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2526 struct dt_rec *rec, const struct dt_key *key,
2527 struct lustre_capa *capa)
2529 struct osd_object *obj = osd_dt_obj(dt);
2530 struct iam_path_descr *ipd;
2531 struct iam_container *bag = &obj->oo_dir->od_container;
2532 struct osd_thread_info *oti = osd_oti_get(env);
2533 struct iam_iterator *it = &oti->oti_idx_it;
2537 LASSERT(osd_invariant(obj));
2538 LASSERT(dt_object_exists(dt));
2539 LASSERT(bag->ic_object == obj->oo_inode);
2541 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2544 ipd = osd_idx_ipd_get(env, bag);
2548 /* got ipd now we can start iterator. */
2549 iam_it_init(it, bag, 0, ipd);
2551 rc = iam_it_get(it, (struct iam_key *)key);
2553 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2557 osd_ipd_put(env, bag, ipd);
2559 LINVRNT(osd_invariant(obj));
2565 * Inserts (key, value) pair in \a dt index object.
2567 * \param dt osd index object
2568 * \param key key for index
2569 * \param rec record reference
2570 * \param th transaction handler
2573 * \retval -ve failure
2575 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2576 const struct dt_rec *rec, const struct dt_key *key,
2577 struct thandle *th, struct lustre_capa *capa,
2580 struct osd_object *obj = osd_dt_obj(dt);
2581 struct iam_path_descr *ipd;
2582 struct osd_thandle *oh;
2583 struct iam_container *bag = &obj->oo_dir->od_container;
2584 #ifdef HAVE_QUOTA_SUPPORT
2585 cfs_cap_t save = current->cap_effective;
2591 LINVRNT(osd_invariant(obj));
2592 LASSERT(dt_object_exists(dt));
2593 LASSERT(bag->ic_object == obj->oo_inode);
2594 LASSERT(th != NULL);
2596 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2599 ipd = osd_idx_ipd_get(env, bag);
2600 if (unlikely(ipd == NULL))
2603 oh = container_of0(th, struct osd_thandle, ot_super);
2604 LASSERT(oh->ot_handle != NULL);
2605 LASSERT(oh->ot_handle->h_transaction != NULL);
2606 #ifdef HAVE_QUOTA_SUPPORT
2608 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2610 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2612 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2613 (struct iam_rec *)rec, ipd);
2614 #ifdef HAVE_QUOTA_SUPPORT
2615 current->cap_effective = save;
2617 osd_ipd_put(env, bag, ipd);
2618 LINVRNT(osd_invariant(obj));
2623 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2624 * into the directory.Also sets flags into osd object to
2625 * indicate dot and dotdot are created. This is required for
2626 * interoperability mode (b11826)
2628 * \param dir directory for dot and dotdot fixup.
2629 * \param obj child object for linking
2631 * \retval 0, on success
2632 * \retval -ve, on error
2634 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2635 struct osd_object *dir,
2636 struct osd_object *obj, const char *name,
2639 struct inode *parent_dir = obj->oo_inode;
2640 struct inode *inode = dir->oo_inode;
2641 struct osd_thandle *oth;
2644 oth = container_of(th, struct osd_thandle, ot_super);
2645 LASSERT(oth->ot_handle->h_transaction != NULL);
2646 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2648 if (strcmp(name, dot) == 0) {
2649 if (dir->oo_compat_dot_created) {
2652 LASSERT(obj == dir);
2653 dir->oo_compat_dot_created = 1;
2656 } else if(strcmp(name, dotdot) == 0) {
2657 if (!dir->oo_compat_dot_created)
2659 if (dir->oo_compat_dotdot_created)
2660 return __osd_ea_add_rec(info, dir, obj, name, th);
2662 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2664 dir->oo_compat_dotdot_created = 1;
2671 * Calls ldiskfs_add_entry() to add directory entry
2672 * into the directory. This is required for
2673 * interoperability mode (b11826)
2675 * \retval 0, on success
2676 * \retval -ve, on error
2678 static int __osd_ea_add_rec(struct osd_thread_info *info,
2679 struct osd_object *pobj,
2680 struct osd_object *cobj,
2684 struct dentry *child;
2685 struct osd_thandle *oth;
2686 struct inode *cinode = cobj->oo_inode;
2689 oth = container_of(th, struct osd_thandle, ot_super);
2690 LASSERT(oth->ot_handle != NULL);
2691 LASSERT(oth->ot_handle->h_transaction != NULL);
2693 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2694 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2700 * It will call the appropriate osd_add* function and return the
2701 * value, return by respective functions.
2703 static int osd_ea_add_rec(const struct lu_env *env,
2704 struct osd_object *pobj,
2705 struct osd_object *cobj,
2709 struct osd_thread_info *info = osd_oti_get(env);
2712 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2714 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2716 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2722 * Calls ->lookup() to find dentry. From dentry get inode and
2723 * read inode's ea to get fid. This is required for interoperability
2726 * \retval 0, on success
2727 * \retval -ve, on error
2729 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2730 struct dt_rec *rec, const struct dt_key *key)
2732 struct inode *dir = obj->oo_inode;
2733 struct osd_thread_info *info = osd_oti_get(env);
2734 struct dentry *dentry;
2735 struct osd_device *dev = osd_dev(obj->oo_dt.do_lu.lo_dev);
2736 struct osd_inode_id *id = &info->oti_id;
2737 struct ldiskfs_dir_entry_2 *de;
2738 struct buffer_head *bh;
2739 struct inode *inode;
2743 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2745 dentry = osd_child_dentry_get(env, obj,
2746 (char *)key, strlen((char *)key));
2748 down_read(&obj->oo_ext_idx_sem);
2749 bh = ldiskfs_find_entry(dentry, &de);
2751 ino = le32_to_cpu(de->inode);
2754 id->oii_gen = OSD_OII_NOGEN;
2756 inode = osd_iget(info, dev, id);
2757 if (!IS_ERR(inode)) {
2758 dentry->d_inode = inode;
2760 rc = osd_ea_fid_get(env, dentry, rec);
2763 rc = PTR_ERR(inode);
2767 up_read(&obj->oo_ext_idx_sem);
2772 * Find the osd object for given fid.
2774 * \param fid, need to find the osd object having this fid
2776 * \retval osd_object, on success
2777 * \retval -ve, on error
2779 struct osd_object *osd_object_find(const struct lu_env *env,
2780 struct dt_object *dt,
2781 const struct lu_fid *fid)
2783 struct lu_device *ludev = dt->do_lu.lo_dev;
2784 struct osd_object *child = NULL;
2785 struct lu_object *luch;
2786 struct lu_object *lo;
2788 luch = lu_object_find(env, ludev, fid, NULL);
2789 if (!IS_ERR(luch)) {
2790 if (lu_object_exists(luch)) {
2791 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2793 child = osd_obj(lo);
2795 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2796 "lu_object can't be located"
2797 ""DFID"\n", PFID(fid));
2799 if (child == NULL) {
2800 lu_object_put(env, luch);
2801 CERROR("Unable to get osd_object\n");
2802 child = ERR_PTR(-ENOENT);
2805 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2806 "lu_object does not exists "DFID"\n",
2808 child = ERR_PTR(-ENOENT);
2811 child = (void *)luch;
2817 * Put the osd object once done with it.
2819 * \param obj, osd object that needs to be put
2821 static inline void osd_object_put(const struct lu_env *env,
2822 struct osd_object *obj)
2824 lu_object_put(env, &obj->oo_dt.do_lu);
2828 * Index add function for interoperability mode (b11826).
2829 * It will add the directory entry.This entry is needed to
2830 * maintain name->fid mapping.
2832 * \param key, it is key i.e. file entry to be inserted
2833 * \param rec, it is value of given key i.e. fid
2835 * \retval 0, on success
2836 * \retval -ve, on error
2838 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2839 const struct dt_rec *rec,
2840 const struct dt_key *key, struct thandle *th,
2841 struct lustre_capa *capa, int ignore_quota)
2843 struct osd_object *obj = osd_dt_obj(dt);
2844 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
2845 const struct lu_fid_pack *pack = (const struct lu_fid_pack *)rec;
2846 const char *name = (const char *)key;
2847 struct osd_object *child;
2848 #ifdef HAVE_QUOTA_SUPPORT
2849 cfs_cap_t save = current->cap_effective;
2855 LASSERT(osd_invariant(obj));
2856 LASSERT(dt_object_exists(dt));
2857 LASSERT(th != NULL);
2859 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2862 rc = fid_unpack(pack, fid);
2865 child = osd_object_find(env, dt, fid);
2866 if (!IS_ERR(child)) {
2867 struct inode *inode = obj->oo_inode;
2868 struct osd_thread_info *oti = osd_oti_get(env);
2869 struct timespec *ctime = &oti->oti_time;
2870 struct timespec *mtime = &oti->oti_time2;
2872 *ctime = inode->i_ctime;
2873 *mtime = inode->i_mtime;
2874 #ifdef HAVE_QUOTA_SUPPORT
2876 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2878 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2880 down_write(&obj->oo_ext_idx_sem);
2881 rc = osd_ea_add_rec(env, obj, child, name, th);
2882 up_write(&obj->oo_ext_idx_sem);
2883 #ifdef HAVE_QUOTA_SUPPORT
2884 current->cap_effective = save;
2886 osd_object_put(env, child);
2887 /* xtime should not be updated with server-side time. */
2888 spin_lock(&obj->oo_guard);
2889 inode->i_ctime = *ctime;
2890 inode->i_mtime = *mtime;
2891 spin_unlock(&obj->oo_guard);
2892 mark_inode_dirty(inode);
2894 rc = PTR_ERR(child);
2897 LASSERT(osd_invariant(obj));
2902 * Initialize osd Iterator for given osd index object.
2904 * \param dt osd index object
2907 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2908 struct dt_object *dt,
2909 struct lustre_capa *capa)
2911 struct osd_it_iam *it;
2912 struct osd_thread_info *oti = osd_oti_get(env);
2913 struct osd_object *obj = osd_dt_obj(dt);
2914 struct lu_object *lo = &dt->do_lu;
2915 struct iam_path_descr *ipd;
2916 struct iam_container *bag = &obj->oo_dir->od_container;
2918 LASSERT(lu_object_exists(lo));
2920 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2921 return ERR_PTR(-EACCES);
2924 ipd = osd_it_ipd_get(env, bag);
2925 if (likely(ipd != NULL)) {
2929 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2930 return (struct dt_it *)it;
2932 return ERR_PTR(-ENOMEM);
2936 * free given Iterator.
2939 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2941 struct osd_it_iam *it = (struct osd_it_iam *)di;
2942 struct osd_object *obj = it->oi_obj;
2944 iam_it_fini(&it->oi_it);
2945 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2946 lu_object_put(env, &obj->oo_dt.do_lu);
2950 * Move Iterator to record specified by \a key
2952 * \param di osd iterator
2953 * \param key key for index
2955 * \retval +ve di points to record with least key not larger than key
2956 * \retval 0 di points to exact matched key
2957 * \retval -ve failure
2960 static int osd_it_iam_get(const struct lu_env *env,
2961 struct dt_it *di, const struct dt_key *key)
2963 struct osd_it_iam *it = (struct osd_it_iam *)di;
2965 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2971 * \param di osd iterator
2974 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2976 struct osd_it_iam *it = (struct osd_it_iam *)di;
2978 iam_it_put(&it->oi_it);
2982 * Move iterator by one record
2984 * \param di osd iterator
2986 * \retval +1 end of container reached
2988 * \retval -ve failure
2991 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2993 struct osd_it_iam *it = (struct osd_it_iam *)di;
2995 return iam_it_next(&it->oi_it);
2999 * Return pointer to the key under iterator.
3002 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3003 const struct dt_it *di)
3005 struct osd_it_iam *it = (struct osd_it_iam *)di;
3007 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3011 * Return size of key under iterator (in bytes)
3014 static int osd_it_iam_key_size(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_key_size(&it->oi_it);
3022 * Return pointer to the record under iterator.
3024 static struct dt_rec *osd_it_iam_rec(const struct lu_env *env,
3025 const struct dt_it *di)
3027 struct osd_it_iam *it = (struct osd_it_iam *)di;
3029 return (struct dt_rec *)iam_it_rec_get(&it->oi_it);
3033 * Returns cookie for current Iterator position.
3035 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3037 struct osd_it_iam *it = (struct osd_it_iam *)di;
3039 return iam_it_store(&it->oi_it);
3043 * Restore iterator from cookie.
3045 * \param di osd iterator
3046 * \param hash Iterator location cookie
3048 * \retval +ve di points to record with least key not larger than key.
3049 * \retval 0 di points to exact matched key
3050 * \retval -ve failure
3053 static int osd_it_iam_load(const struct lu_env *env,
3054 const struct dt_it *di, __u64 hash)
3056 struct osd_it_iam *it = (struct osd_it_iam *)di;
3058 return iam_it_load(&it->oi_it, hash);
3061 static const struct dt_index_operations osd_index_iam_ops = {
3062 .dio_lookup = osd_index_iam_lookup,
3063 .dio_insert = osd_index_iam_insert,
3064 .dio_delete = osd_index_iam_delete,
3066 .init = osd_it_iam_init,
3067 .fini = osd_it_iam_fini,
3068 .get = osd_it_iam_get,
3069 .put = osd_it_iam_put,
3070 .next = osd_it_iam_next,
3071 .key = osd_it_iam_key,
3072 .key_size = osd_it_iam_key_size,
3073 .rec = osd_it_iam_rec,
3074 .store = osd_it_iam_store,
3075 .load = osd_it_iam_load
3080 * Creates or initializes iterator context.
3082 * \retval struct osd_it_ea, iterator structure on success
3085 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3086 struct dt_object *dt,
3087 struct lustre_capa *capa)
3089 struct osd_object *obj = osd_dt_obj(dt);
3090 struct osd_thread_info *info = osd_oti_get(env);
3091 struct osd_it_ea *it = &info->oti_it_ea;
3092 struct lu_object *lo = &dt->do_lu;
3093 struct dentry *obj_dentry = &info->oti_it_dentry;
3095 LASSERT(lu_object_exists(lo));
3097 obj_dentry->d_inode = obj->oo_inode;
3098 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3099 obj_dentry->d_name.hash = 0;
3101 it->oie_rd_dirent = 0;
3102 it->oie_it_dirent = 0;
3103 it->oie_curr_pos = 0;
3104 it->oie_next_pos = 0;
3105 it->oie_dirent = NULL;
3106 it->oie_buf = info->oti_it_ea_buf;
3108 it->oie_file.f_dentry = obj_dentry;
3109 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3110 it->oie_file.f_op = obj->oo_inode->i_fop;
3111 it->oie_file.private_data = NULL;
3113 RETURN((struct dt_it*) it);
3117 * Destroy or finishes iterator context.
3119 * \param di, struct osd_it_ea, iterator structure to be destroyed
3121 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3123 struct osd_it_ea *it = (struct osd_it_ea *)di;
3124 struct osd_object *obj = it->oie_obj;
3125 struct inode *inode = obj->oo_inode;
3128 it->oie_file.f_op->release(inode, &it->oie_file);
3129 lu_object_put(env, &obj->oo_dt.do_lu);
3134 * It position the iterator at given key, so that next lookup continues from
3135 * that key Or it is similar to dio_it->load() but based on a key,
3136 * rather than file position.
3138 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3141 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3143 static int osd_it_ea_get(const struct lu_env *env,
3144 struct dt_it *di, const struct dt_key *key)
3146 struct osd_it_ea *it = (struct osd_it_ea *)di;
3149 LASSERT(((const char *)key)[0] == '\0');
3150 it->oie_curr_pos = 0;
3151 it->oie_next_pos = 0;
3152 it->oie_rd_dirent = 0;
3153 it->oie_it_dirent = 0;
3154 it->oie_dirent = NULL;
3162 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3167 * It is called internally by ->readdir(). It fills the
3168 * iterator's in-memory data structure with required
3169 * information i.e. name, namelen, rec_size etc.
3171 * \param buf, in which information to be filled in.
3172 * \param name, name of the file in given dir
3174 * \retval 0, on success
3175 * \retval 1, on buffer full
3177 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3178 loff_t offset, ino_t ino,
3179 unsigned int d_type)
3181 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3182 struct osd_it_ea_dirent *ent = it->oie_dirent;
3185 /* this should never happen */
3186 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3187 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3191 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3195 ent->oied_ino = ino;
3196 ent->oied_off = offset;
3197 ent->oied_namelen = namelen;
3198 memcpy(ent->oied_name, name, namelen);
3200 it->oie_rd_dirent++;
3201 it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3206 * Calls ->readdir() to load a directory entry at a time
3207 * and stored it in iterator's in-memory data structure.
3209 * \param di, struct osd_it_ea, iterator's in memory structure
3211 * \retval 0, on success
3212 * \retval -ve, on error
3214 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3216 struct osd_it_ea *it = (struct osd_it_ea *)di;
3217 struct osd_object *obj = it->oie_obj;
3218 struct inode *inode = obj->oo_inode;
3222 it->oie_dirent = it->oie_buf;
3223 it->oie_rd_dirent = 0;
3224 it->oie_file.f_pos = it->oie_curr_pos;
3226 down_read(&obj->oo_ext_idx_sem);
3227 result = inode->i_fop->readdir(&it->oie_file, it,
3228 (filldir_t) osd_ldiskfs_filldir);
3230 up_read(&obj->oo_ext_idx_sem);
3231 it->oie_next_pos = it->oie_file.f_pos;
3233 if (it->oie_rd_dirent == 0) {
3236 it->oie_dirent = it->oie_buf;
3237 it->oie_it_dirent = 1;
3244 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3245 * to load a directory entry at a time and stored it in
3246 * iterator's in-memory data structure.
3248 * \param di, struct osd_it_ea, iterator's in memory structure
3250 * \retval +ve, iterator reached to end
3251 * \retval 0, iterator not reached to end
3252 * \retval -ve, on error
3254 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3256 struct osd_it_ea *it = (struct osd_it_ea *)di;
3261 if (it->oie_it_dirent < it->oie_rd_dirent) {
3262 it->oie_dirent = (void *) it->oie_dirent +
3263 size_round(sizeof(struct osd_it_ea_dirent) +
3264 it->oie_dirent->oied_namelen);
3265 it->oie_it_dirent++;
3268 it->oie_curr_pos = it->oie_next_pos;
3270 if (it->oie_curr_pos == LDISKFS_HTREE_EOF)
3273 rc = osd_ldiskfs_it_fill(di);
3280 * Returns the key at current position from iterator's in memory structure.
3282 * \param di, struct osd_it_ea, iterator's in memory structure
3284 * \retval key i.e. struct dt_key on success
3286 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3287 const struct dt_it *di)
3289 struct osd_it_ea *it = (struct osd_it_ea *)di;
3291 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3295 * Returns the key's size at current position from iterator's in memory structure.
3297 * \param di, struct osd_it_ea, iterator's in memory structure
3299 * \retval key_size i.e. struct dt_key on success
3301 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3303 struct osd_it_ea *it = (struct osd_it_ea *)di;
3305 RETURN(it->oie_dirent->oied_namelen);
3309 * Returns the value (i.e. fid/igif) at current position from iterator's
3310 * in memory structure.
3312 * \param di, struct osd_it_ea, iterator's in memory structure
3314 * \retval value i.e. struct dt_rec on success
3316 static struct dt_rec *osd_it_ea_rec(const struct lu_env *env,
3317 const struct dt_it *di)
3319 struct osd_it_ea *it = (struct osd_it_ea *)di;
3320 struct osd_object *obj = it->oie_obj;
3321 struct osd_thread_info *info = osd_oti_get(env);
3322 struct osd_inode_id *id = &info->oti_id;
3323 struct lu_fid_pack *rec = &info->oti_pack;
3324 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3325 struct dentry *dentry = &info->oti_child_dentry;
3326 struct osd_device *dev;
3327 struct inode *inode;
3331 dev = osd_dev(ldev);
3332 id->oii_ino = it->oie_dirent->oied_ino;
3333 id->oii_gen = OSD_OII_NOGEN;
3334 inode = osd_iget(info, dev, id);
3335 if (!IS_ERR(inode)) {
3336 dentry->d_inode = inode;
3337 LASSERT(dentry->d_inode->i_sb == osd_sb(dev));
3339 RETURN((struct dt_rec *) PTR_ERR(inode));
3342 rc = osd_ea_fid_get(env, dentry, (struct dt_rec*) rec);
3347 RETURN((struct dt_rec *)rec);
3352 * Returns a cookie for current position of the iterator head, so that
3353 * user can use this cookie to load/start the iterator next time.
3355 * \param di, struct osd_it_ea, iterator's in memory structure
3357 * \retval cookie for current position, on success
3359 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3361 struct osd_it_ea *it = (struct osd_it_ea *)di;
3363 RETURN(it->oie_dirent->oied_off);
3367 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3368 * to load a directory entry at a time and stored it i inn,
3369 * in iterator's in-memory data structure.
3371 * \param di, struct osd_it_ea, iterator's in memory structure
3373 * \retval +ve, on success
3374 * \retval -ve, on error
3376 static int osd_it_ea_load(const struct lu_env *env,
3377 const struct dt_it *di, __u64 hash)
3379 struct osd_it_ea *it = (struct osd_it_ea *)di;
3383 it->oie_curr_pos = hash;
3385 rc = osd_ldiskfs_it_fill(di);
3392 * Index and Iterator operations for interoperability
3393 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3395 static const struct dt_index_operations osd_index_ea_ops = {
3396 .dio_lookup = osd_index_ea_lookup,
3397 .dio_insert = osd_index_ea_insert,
3398 .dio_delete = osd_index_ea_delete,
3400 .init = osd_it_ea_init,
3401 .fini = osd_it_ea_fini,
3402 .get = osd_it_ea_get,
3403 .put = osd_it_ea_put,
3404 .next = osd_it_ea_next,
3405 .key = osd_it_ea_key,
3406 .key_size = osd_it_ea_key_size,
3407 .rec = osd_it_ea_rec,
3408 .store = osd_it_ea_store,
3409 .load = osd_it_ea_load
3414 * Index lookup function for interoperability mode (b11826).
3416 * \param key, key i.e. file name to be searched
3418 * \retval +ve, on success
3419 * \retval -ve, on error
3421 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3422 struct dt_rec *rec, const struct dt_key *key,
3423 struct lustre_capa *capa)
3425 struct osd_object *obj = osd_dt_obj(dt);
3430 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3431 LINVRNT(osd_invariant(obj));
3433 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3436 rc = osd_ea_lookup_rec(env, obj, rec, key);
3443 /* type constructor/destructor: osd_type_init, osd_type_fini */
3444 LU_TYPE_INIT_FINI(osd, &osd_key);
3446 static struct lu_context_key osd_key = {
3447 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3448 .lct_init = osd_key_init,
3449 .lct_fini = osd_key_fini,
3450 .lct_exit = osd_key_exit
3453 static void *osd_key_init(const struct lu_context *ctx,
3454 struct lu_context_key *key)
3456 struct osd_thread_info *info;
3458 OBD_ALLOC_PTR(info);
3460 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3461 if (info->oti_it_ea_buf != NULL) {
3462 info->oti_env = container_of(ctx, struct lu_env,
3466 info = ERR_PTR(-ENOMEM);
3469 info = ERR_PTR(-ENOMEM);
3474 static void osd_key_fini(const struct lu_context *ctx,
3475 struct lu_context_key *key, void* data)
3477 struct osd_thread_info *info = data;
3479 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3483 static void osd_key_exit(const struct lu_context *ctx,
3484 struct lu_context_key *key, void *data)
3486 struct osd_thread_info *info = data;
3488 LASSERT(info->oti_r_locks == 0);
3489 LASSERT(info->oti_w_locks == 0);
3490 LASSERT(info->oti_txns == 0);
3493 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3494 const char *name, struct lu_device *next)
3497 struct lu_context *ctx;
3499 /* context for commit hooks */
3500 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3501 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3503 rc = osd_procfs_init(osd_dev(d), name);
3504 ctx->lc_cookie = 0x3;
3509 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3511 struct osd_thread_info *info = osd_oti_get(env);
3513 if (o->od_obj_area != NULL) {
3514 lu_object_put(env, &o->od_obj_area->do_lu);
3515 o->od_obj_area = NULL;
3517 osd_oi_fini(info, &o->od_oi);
3522 static int osd_mount(const struct lu_env *env,
3523 struct osd_device *o, struct lustre_cfg *cfg)
3525 struct lustre_mount_info *lmi;
3526 const char *dev = lustre_cfg_string(cfg, 0);
3527 struct lustre_disk_data *ldd;
3528 struct lustre_sb_info *lsi;
3532 if (o->od_mount != NULL) {
3533 CERROR("Already mounted (%s)\n", dev);
3538 lmi = server_get_mount(dev);
3540 CERROR("Cannot get mount info for %s!\n", dev);
3544 LASSERT(lmi != NULL);
3545 /* save lustre_mount_info in dt_device */
3548 lsi = s2lsi(lmi->lmi_sb);
3551 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3553 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3557 o->od_obj_area = NULL;
3561 static struct lu_device *osd_device_fini(const struct lu_env *env,
3562 struct lu_device *d)
3567 shrink_dcache_sb(osd_sb(osd_dev(d)));
3568 osd_sync(env, lu2dt_dev(d));
3570 rc = osd_procfs_fini(osd_dev(d));
3572 CERROR("proc fini error %d \n", rc);
3573 RETURN (ERR_PTR(rc));
3576 if (osd_dev(d)->od_mount)
3577 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3578 osd_dev(d)->od_mount->lmi_mnt);
3579 osd_dev(d)->od_mount = NULL;
3581 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3585 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3586 struct lu_device_type *t,
3587 struct lustre_cfg *cfg)
3589 struct lu_device *l;
3590 struct osd_device *o;
3596 result = dt_device_init(&o->od_dt_dev, t);
3599 l->ld_ops = &osd_lu_ops;
3600 o->od_dt_dev.dd_ops = &osd_dt_ops;
3601 spin_lock_init(&o->od_osfs_lock);
3602 o->od_osfs_age = cfs_time_shift_64(-1000);
3603 o->od_capa_hash = init_capa_hash();
3604 if (o->od_capa_hash == NULL) {
3605 dt_device_fini(&o->od_dt_dev);
3606 l = ERR_PTR(-ENOMEM);
3609 l = ERR_PTR(result);
3614 l = ERR_PTR(-ENOMEM);
3618 static struct lu_device *osd_device_free(const struct lu_env *env,
3619 struct lu_device *d)
3621 struct osd_device *o = osd_dev(d);
3624 cleanup_capa_hash(o->od_capa_hash);
3625 dt_device_fini(&o->od_dt_dev);
3630 static int osd_process_config(const struct lu_env *env,
3631 struct lu_device *d, struct lustre_cfg *cfg)
3633 struct osd_device *o = osd_dev(d);
3637 switch(cfg->lcfg_command) {
3639 err = osd_mount(env, o, cfg);
3642 err = osd_shutdown(env, o);
3651 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
3652 struct ldiskfs_super_block * es);
3654 static int osd_recovery_complete(const struct lu_env *env,
3655 struct lu_device *d)
3657 struct osd_device *o = osd_dev(d);
3659 /* TODO: orphans handling */
3660 ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3664 static int osd_prepare(const struct lu_env *env,
3665 struct lu_device *pdev,
3666 struct lu_device *dev)
3668 struct osd_device *osd = osd_dev(dev);
3669 struct lustre_sb_info *lsi;
3670 struct lustre_disk_data *ldd;
3671 struct lustre_mount_info *lmi;
3672 struct osd_thread_info *oti = osd_oti_get(env);
3673 struct dt_object *d;
3677 /* 1. initialize oi before any file create or file open */
3678 result = osd_oi_init(oti, &osd->od_oi,
3679 &osd->od_dt_dev, lu2md_dev(pdev));
3683 lmi = osd->od_mount;
3684 lsi = s2lsi(lmi->lmi_sb);
3687 /* 2. setup local objects */
3688 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3692 /* 3. open remote object dir */
3693 d = dt_store_open(env, lu2dt_dev(dev), "",
3694 remote_obj_dir, &oti->oti_fid);
3696 osd->od_obj_area = d;
3699 result = PTR_ERR(d);
3700 osd->od_obj_area = NULL;
3707 static struct inode *osd_iget(struct osd_thread_info *info,
3708 struct osd_device *dev,
3709 const struct osd_inode_id *id)
3711 struct inode *inode;
3713 inode = iget(osd_sb(dev), id->oii_ino);
3714 if (inode == NULL) {
3715 CERROR("no inode\n");
3716 inode = ERR_PTR(-EACCES);
3717 } else if (id->oii_gen != OSD_OII_NOGEN &&
3718 inode->i_generation != id->oii_gen) {
3720 inode = ERR_PTR(-ESTALE);
3721 } else if (inode->i_nlink == 0) {
3722 /* due to parallel readdir and unlink,
3723 * we can have dead inode here. */
3724 CWARN("stale inode\n");
3725 make_bad_inode(inode);
3727 inode = ERR_PTR(-ESTALE);
3728 } else if (is_bad_inode(inode)) {
3729 CERROR("bad inode %lx\n",inode->i_ino);
3731 inode = ERR_PTR(-ENOENT);
3737 static int osd_fid_lookup(const struct lu_env *env,
3738 struct osd_object *obj, const struct lu_fid *fid)
3740 struct osd_thread_info *info;
3741 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3742 struct osd_device *dev;
3743 struct osd_inode_id *id;
3745 struct inode *inode;
3748 LINVRNT(osd_invariant(obj));
3749 LASSERT(obj->oo_inode == NULL);
3750 LASSERT(fid_is_sane(fid));
3752 * This assertion checks that osd layer sees only local
3753 * fids. Unfortunately it is somewhat expensive (does a
3754 * cache-lookup). Disabling it for production/acceptance-testing.
3756 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
3760 info = osd_oti_get(env);
3761 dev = osd_dev(ldev);
3765 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
3768 result = osd_oi_lookup(info, oi, fid, id);
3770 inode = osd_iget(info, dev, id);
3771 if (!IS_ERR(inode)) {
3772 obj->oo_inode = inode;
3773 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
3774 if (dev->od_iop_mode) {
3775 obj->oo_compat_dot_created = 1;
3776 obj->oo_compat_dotdot_created = 1;
3781 * If fid wasn't found in oi, inode-less object is
3782 * created, for which lu_object_exists() returns
3783 * false. This is used in a (frequent) case when
3784 * objects are created as locking anchors or
3785 * place holders for objects yet to be created.
3787 result = PTR_ERR(inode);
3788 } else if (result == -ENOENT)
3790 LINVRNT(osd_invariant(obj));
3795 static void osd_inode_getattr(const struct lu_env *env,
3796 struct inode *inode, struct lu_attr *attr)
3798 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
3799 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
3800 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
3802 attr->la_atime = LTIME_S(inode->i_atime);
3803 attr->la_mtime = LTIME_S(inode->i_mtime);
3804 attr->la_ctime = LTIME_S(inode->i_ctime);
3805 attr->la_mode = inode->i_mode;
3806 attr->la_size = i_size_read(inode);
3807 attr->la_blocks = inode->i_blocks;
3808 attr->la_uid = inode->i_uid;
3809 attr->la_gid = inode->i_gid;
3810 attr->la_flags = LDISKFS_I(inode)->i_flags;
3811 attr->la_nlink = inode->i_nlink;
3812 attr->la_rdev = inode->i_rdev;
3813 attr->la_blksize = ll_inode_blksize(inode);
3814 attr->la_blkbits = inode->i_blkbits;
3821 static int lu_device_is_osd(const struct lu_device *d)
3823 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
3826 static struct osd_object *osd_obj(const struct lu_object *o)
3828 LASSERT(lu_device_is_osd(o->lo_dev));
3829 return container_of0(o, struct osd_object, oo_dt.do_lu);
3832 static struct osd_device *osd_dt_dev(const struct dt_device *d)
3834 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
3835 return container_of0(d, struct osd_device, od_dt_dev);
3838 static struct osd_device *osd_dev(const struct lu_device *d)
3840 LASSERT(lu_device_is_osd(d));
3841 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
3844 static struct osd_object *osd_dt_obj(const struct dt_object *d)
3846 return osd_obj(&d->do_lu);
3849 static struct osd_device *osd_obj2dev(const struct osd_object *o)
3851 return osd_dev(o->oo_dt.do_lu.lo_dev);
3854 static struct lu_device *osd2lu_dev(struct osd_device *osd)
3856 return &osd->od_dt_dev.dd_lu_dev;
3859 static struct super_block *osd_sb(const struct osd_device *dev)
3861 return dev->od_mount->lmi_mnt->mnt_sb;
3864 static journal_t *osd_journal(const struct osd_device *dev)
3866 return LDISKFS_SB(osd_sb(dev))->s_journal;
3869 static int osd_has_index(const struct osd_object *obj)
3871 return obj->oo_dt.do_index_ops != NULL;
3874 static int osd_object_invariant(const struct lu_object *l)
3876 return osd_invariant(osd_obj(l));
3879 static const struct lu_object_operations osd_lu_obj_ops = {
3880 .loo_object_init = osd_object_init,
3881 .loo_object_delete = osd_object_delete,
3882 .loo_object_release = osd_object_release,
3883 .loo_object_free = osd_object_free,
3884 .loo_object_print = osd_object_print,
3885 .loo_object_invariant = osd_object_invariant
3888 static const struct lu_device_operations osd_lu_ops = {
3889 .ldo_object_alloc = osd_object_alloc,
3890 .ldo_process_config = osd_process_config,
3891 .ldo_recovery_complete = osd_recovery_complete,
3892 .ldo_prepare = osd_prepare,
3895 static const struct lu_device_type_operations osd_device_type_ops = {
3896 .ldto_init = osd_type_init,
3897 .ldto_fini = osd_type_fini,
3899 .ldto_start = osd_type_start,
3900 .ldto_stop = osd_type_stop,
3902 .ldto_device_alloc = osd_device_alloc,
3903 .ldto_device_free = osd_device_free,
3905 .ldto_device_init = osd_device_init,
3906 .ldto_device_fini = osd_device_fini
3909 static struct lu_device_type osd_device_type = {
3910 .ldt_tags = LU_DEVICE_DT,
3911 .ldt_name = LUSTRE_OSD_NAME,
3912 .ldt_ops = &osd_device_type_ops,
3913 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3917 * lprocfs legacy support.
3919 static struct obd_ops osd_obd_device_ops = {
3920 .o_owner = THIS_MODULE
3923 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3924 .llod_name = remote_obj_dir,
3925 .llod_oid = OSD_REM_OBJ_DIR_OID,
3927 .llod_feat = &dt_directory_features,
3930 static int __init osd_mod_init(void)
3932 struct lprocfs_static_vars lvars;
3935 llo_local_obj_register(&llod_osd_rem_obj_dir);
3936 lprocfs_osd_init_vars(&lvars);
3937 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3938 LUSTRE_OSD_NAME, &osd_device_type);
3941 static void __exit osd_mod_exit(void)
3943 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3944 class_unregister_type(LUSTRE_OSD_NAME);
3947 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3948 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3949 MODULE_LICENSE("GPL");
3951 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);