1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_handler.c
38 * Top-level entry points into osd module
40 * Author: Nikita Danilov <nikita@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
50 /* LUSTRE_VERSION_CODE */
51 #include <lustre_ver.h>
52 /* prerequisite for linux/xattr.h */
53 #include <linux/types.h>
54 /* prerequisite for linux/xattr.h */
56 /* XATTR_{REPLACE,CREATE} */
57 #include <linux/xattr.h>
59 * XXX temporary stuff: direct access to ldiskfs/jdb. Interface between osd
60 * and file system is not yet specified.
62 /* handle_t, journal_start(), journal_stop() */
63 #include <linux/jbd.h>
65 #include <linux/ldiskfs_fs.h>
66 #include <linux/ldiskfs_jbd.h>
71 * struct OBD_{ALLOC,FREE}*()
74 #include <obd_support.h>
75 /* struct ptlrpc_thread */
76 #include <lustre_net.h>
79 #include <lustre_fid.h>
80 #include <linux/lustre_iam.h>
82 #include "osd_internal.h"
85 /* llo_* api support */
86 #include <md_object.h>
88 static const char dot[] = ".";
89 static const char dotdot[] = "..";
90 static const char remote_obj_dir[] = "REM_OBJ_DIR";
92 struct osd_directory {
93 struct iam_container od_container;
94 struct iam_descr od_descr;
98 struct dt_object oo_dt;
100 * Inode for file system object represented by this osd_object. This
101 * inode is pinned for the whole duration of lu_object life.
103 * Not modified concurrently (either setup early during object
104 * creation, or assigned by osd_object_create() under write lock).
106 struct inode *oo_inode;
108 * to protect index ops.
110 struct rw_semaphore oo_ext_idx_sem;
111 struct rw_semaphore oo_sem;
112 struct osd_directory *oo_dir;
113 /** protects inode attributes. */
116 * Following two members are used to indicate the presence of dot and
117 * dotdot in the given directory. This is required for interop mode
120 int oo_compat_dot_created;
121 int oo_compat_dotdot_created;
123 const struct lu_env *oo_owner;
124 #ifdef CONFIG_LOCKDEP
125 struct lockdep_map oo_dep_map;
129 static int osd_root_get (const struct lu_env *env,
130 struct dt_device *dev, struct lu_fid *f);
132 static int lu_device_is_osd (const struct lu_device *d);
133 static void osd_mod_exit (void) __exit;
134 static int osd_mod_init (void) __init;
135 static int osd_type_init (struct lu_device_type *t);
136 static void osd_type_fini (struct lu_device_type *t);
137 static int osd_object_init (const struct lu_env *env,
139 const struct lu_object_conf *_);
140 static void osd_object_release(const struct lu_env *env,
141 struct lu_object *l);
142 static int osd_object_print (const struct lu_env *env, void *cookie,
143 lu_printer_t p, const struct lu_object *o);
144 static struct lu_device *osd_device_free (const struct lu_env *env,
145 struct lu_device *m);
146 static void *osd_key_init (const struct lu_context *ctx,
147 struct lu_context_key *key);
148 static void osd_key_fini (const struct lu_context *ctx,
149 struct lu_context_key *key, void *data);
150 static void osd_key_exit (const struct lu_context *ctx,
151 struct lu_context_key *key, void *data);
152 static int osd_has_index (const struct osd_object *obj);
153 static void osd_object_init0 (struct osd_object *obj);
154 static int osd_device_init (const struct lu_env *env,
155 struct lu_device *d, const char *,
157 static int osd_fid_lookup (const struct lu_env *env,
158 struct osd_object *obj,
159 const struct lu_fid *fid);
160 static void osd_inode_getattr (const struct lu_env *env,
161 struct inode *inode, struct lu_attr *attr);
162 static int osd_inode_setattr (const struct lu_env *env,
163 struct inode *inode, const struct lu_attr *attr);
164 static int osd_param_is_sane (const struct osd_device *dev,
165 const struct txn_param *param);
166 static int osd_index_iam_lookup(const struct lu_env *env,
167 struct dt_object *dt,
168 struct dt_rec *rec, const struct dt_key *key,
169 struct lustre_capa *capa);
170 static int osd_index_ea_lookup(const struct lu_env *env,
171 struct dt_object *dt,
172 struct dt_rec *rec, const struct dt_key *key,
173 struct lustre_capa *capa);
174 static int osd_index_iam_insert(const struct lu_env *env,
175 struct dt_object *dt,
176 const struct dt_rec *rec,
177 const struct dt_key *key,
178 struct thandle *handle,
179 struct lustre_capa *capa,
181 static int osd_index_ea_insert (const struct lu_env *env,
182 struct dt_object *dt,
183 const struct dt_rec *rec,
184 const struct dt_key *key,
185 struct thandle *handle,
186 struct lustre_capa *capa,
188 static int osd_index_iam_delete(const struct lu_env *env,
189 struct dt_object *dt, const struct dt_key *key,
190 struct thandle *handle,
191 struct lustre_capa *capa);
192 static int osd_index_ea_delete (const struct lu_env *env,
193 struct dt_object *dt, const struct dt_key *key,
194 struct thandle *handle,
195 struct lustre_capa *capa);
197 static int osd_iam_index_probe (const struct lu_env *env,
198 struct osd_object *o,
199 const struct dt_index_features *feat);
200 static int osd_index_try (const struct lu_env *env,
201 struct dt_object *dt,
202 const struct dt_index_features *feat);
203 static void osd_index_fini (struct osd_object *o);
205 static void osd_it_iam_fini (const struct lu_env *env, struct dt_it *di);
206 static int osd_it_iam_get (const struct lu_env *env,
207 struct dt_it *di, const struct dt_key *key);
208 static void osd_it_iam_put (const struct lu_env *env, struct dt_it *di);
209 static int osd_it_iam_next (const struct lu_env *env, struct dt_it *di);
210 static int osd_it_iam_key_size (const struct lu_env *env,
211 const struct dt_it *di);
212 static void osd_it_ea_fini (const struct lu_env *env, struct dt_it *di);
213 static int osd_it_ea_get (const struct lu_env *env,
214 struct dt_it *di, const struct dt_key *key);
215 static void osd_it_ea_put (const struct lu_env *env, struct dt_it *di);
216 static int osd_it_ea_next (const struct lu_env *env, struct dt_it *di);
217 static int osd_it_ea_key_size(const struct lu_env *env,
218 const struct dt_it *di);
220 static void osd_conf_get (const struct lu_env *env,
221 const struct dt_device *dev,
222 struct dt_device_param *param);
223 static void osd_trans_stop (const struct lu_env *env,
225 static int osd_object_is_root(const struct osd_object *obj);
227 static struct osd_object *osd_obj (const struct lu_object *o);
228 static struct osd_device *osd_dev (const struct lu_device *d);
229 static struct osd_device *osd_dt_dev (const struct dt_device *d);
230 static struct osd_object *osd_dt_obj (const struct dt_object *d);
231 static struct osd_device *osd_obj2dev (const struct osd_object *o);
232 static struct lu_device *osd2lu_dev (struct osd_device *osd);
233 static struct lu_device *osd_device_fini (const struct lu_env *env,
234 struct lu_device *d);
235 static struct lu_device *osd_device_alloc (const struct lu_env *env,
236 struct lu_device_type *t,
237 struct lustre_cfg *cfg);
238 static struct lu_object *osd_object_alloc (const struct lu_env *env,
239 const struct lu_object_header *hdr,
240 struct lu_device *d);
241 static struct inode *osd_iget (struct osd_thread_info *info,
242 struct osd_device *dev,
243 const struct osd_inode_id *id);
244 static struct super_block *osd_sb (const struct osd_device *dev);
245 static struct dt_it *osd_it_iam_init (const struct lu_env *env,
246 struct dt_object *dt,
247 struct lustre_capa *capa);
248 static struct dt_key *osd_it_iam_key (const struct lu_env *env,
249 const struct dt_it *di);
250 static struct dt_rec *osd_it_iam_rec (const struct lu_env *env,
251 const struct dt_it *di);
252 static struct dt_it *osd_it_ea_init (const struct lu_env *env,
253 struct dt_object *dt,
254 struct lustre_capa *capa);
255 static struct dt_key *osd_it_ea_key (const struct lu_env *env,
256 const struct dt_it *di);
257 static struct dt_rec *osd_it_ea_rec (const struct lu_env *env,
258 const struct dt_it *di);
260 static struct timespec *osd_inode_time (const struct lu_env *env,
263 static struct thandle *osd_trans_start (const struct lu_env *env,
265 struct txn_param *p);
266 static journal_t *osd_journal (const struct osd_device *dev);
268 static int __osd_ea_add_rec(struct osd_thread_info *info,
269 struct osd_object *pobj,
270 struct osd_object *cobj,
274 static const struct lu_device_type_operations osd_device_type_ops;
275 static struct lu_device_type osd_device_type;
276 static const struct lu_object_operations osd_lu_obj_ops;
277 static struct obd_ops osd_obd_device_ops;
278 static const struct lu_device_operations osd_lu_ops;
279 static struct lu_context_key osd_key;
280 static const struct dt_object_operations osd_obj_ops;
281 static const struct dt_object_operations osd_obj_ea_ops;
282 static const struct dt_body_operations osd_body_ops;
283 static const struct dt_index_operations osd_index_iam_ops;
284 static const struct dt_index_operations osd_index_ea_ops;
287 struct thandle ot_super;
289 struct journal_callback ot_jcb;
290 /* Link to the device, for debugging. */
291 struct lu_ref_link *ot_dev_link;
295 #ifdef HAVE_QUOTA_SUPPORT
297 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
299 struct md_ucred *uc = md_ucred(env);
303 save->oc_uid = current->fsuid;
304 save->oc_gid = current->fsgid;
305 save->oc_cap = current->cap_effective;
306 current->fsuid = uc->mu_fsuid;
307 current->fsgid = uc->mu_fsgid;
308 current->cap_effective = uc->mu_cap;
312 osd_pop_ctxt(struct osd_ctxt *save)
314 current->fsuid = save->oc_uid;
315 current->fsgid = save->oc_gid;
316 current->cap_effective = save->oc_cap;
321 * Invariants, assertions.
325 * XXX: do not enable this, until invariant checking code is made thread safe
326 * in the face of pdirops locking.
328 #define OSD_INVARIANT_CHECKS (0)
330 #if OSD_INVARIANT_CHECKS
331 static int osd_invariant(const struct osd_object *obj)
335 ergo(obj->oo_inode != NULL,
336 obj->oo_inode->i_sb == osd_sb(osd_obj2dev(obj)) &&
337 atomic_read(&obj->oo_inode->i_count) > 0) &&
338 ergo(obj->oo_dir != NULL &&
339 obj->oo_dir->od_conationer.ic_object != NULL,
340 obj->oo_dir->od_conationer.ic_object == obj->oo_inode);
343 #define osd_invariant(obj) (1)
346 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
348 return lu_context_key_get(&env->le_ctx, &osd_key);
352 * Concurrency: doesn't matter
354 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
356 return osd_oti_get(env)->oti_r_locks > 0;
360 * Concurrency: doesn't matter
362 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
364 struct osd_thread_info *oti = osd_oti_get(env);
365 return oti->oti_w_locks > 0 && o->oo_owner == env;
369 * Concurrency: doesn't access mutable data
371 static int osd_root_get(const struct lu_env *env,
372 struct dt_device *dev, struct lu_fid *f)
376 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
377 lu_igif_build(f, inode->i_ino, inode->i_generation);
382 * OSD object methods.
386 * Concurrency: no concurrent access is possible that early in object
389 static struct lu_object *osd_object_alloc(const struct lu_env *env,
390 const struct lu_object_header *hdr,
393 struct osd_object *mo;
399 l = &mo->oo_dt.do_lu;
400 dt_object_init(&mo->oo_dt, NULL, d);
401 if (osd_dev(d)->od_iop_mode)
402 mo->oo_dt.do_ops = &osd_obj_ea_ops;
404 mo->oo_dt.do_ops = &osd_obj_ops;
406 l->lo_ops = &osd_lu_obj_ops;
407 init_rwsem(&mo->oo_sem);
408 init_rwsem(&mo->oo_ext_idx_sem);
409 spin_lock_init(&mo->oo_guard);
416 * Concurrency: shouldn't matter.
418 static void osd_object_init0(struct osd_object *obj)
420 LASSERT(obj->oo_inode != NULL);
421 obj->oo_dt.do_body_ops = &osd_body_ops;
422 obj->oo_dt.do_lu.lo_header->loh_attr |=
423 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
427 * Concurrency: no concurrent access is possible that early in object
430 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
431 const struct lu_object_conf *_)
433 struct osd_object *obj = osd_obj(l);
436 LINVRNT(osd_invariant(obj));
438 result = osd_fid_lookup(env, obj, lu_object_fid(l));
440 if (obj->oo_inode != NULL)
441 osd_object_init0(obj);
443 LINVRNT(osd_invariant(obj));
448 * Concurrency: no concurrent access is possible that late in object
451 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
453 struct osd_object *obj = osd_obj(l);
455 LINVRNT(osd_invariant(obj));
457 dt_object_fini(&obj->oo_dt);
461 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
462 const struct iam_container *bag)
464 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
465 osd_oti_get(env)->oti_it_ipd);
468 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
469 const struct iam_container *bag)
471 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
472 osd_oti_get(env)->oti_idx_ipd);
475 static void osd_ipd_put(const struct lu_env *env,
476 const struct iam_container *bag,
477 struct iam_path_descr *ipd)
479 bag->ic_descr->id_ops->id_ipd_free(ipd);
483 * Concurrency: no concurrent access is possible that late in object
486 static void osd_index_fini(struct osd_object *o)
488 struct iam_container *bag;
490 if (o->oo_dir != NULL) {
491 bag = &o->oo_dir->od_container;
492 if (o->oo_inode != NULL) {
493 if (bag->ic_object == o->oo_inode)
494 iam_container_fini(bag);
496 OBD_FREE_PTR(o->oo_dir);
502 * Concurrency: no concurrent access is possible that late in object
503 * life-cycle (for all existing callers, that is. New callers have to provide
504 * their own locking.)
506 static int osd_inode_unlinked(const struct inode *inode)
508 return inode->i_nlink == 0;
512 OSD_TXN_OI_DELETE_CREDITS = 20,
513 OSD_TXN_INODE_DELETE_CREDITS = 20
517 * Concurrency: no concurrent access is possible that late in object
520 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
522 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
523 struct osd_device *osd = osd_obj2dev(obj);
524 struct osd_thread_info *oti = osd_oti_get(env);
525 struct txn_param *prm = &oti->oti_txn;
526 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
530 lu_env_init(env_del_obj, LCT_DT_THREAD);
531 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
532 OSD_TXN_INODE_DELETE_CREDITS);
533 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
535 result = osd_oi_delete(osd_oti_get(env_del_obj),
536 &osd->od_oi, fid, th);
537 osd_trans_stop(env_del_obj, th);
539 result = PTR_ERR(th);
541 lu_env_fini(env_del_obj);
546 * Called just before object is freed. Releases all resources except for
547 * object itself (that is released by osd_object_free()).
549 * Concurrency: no concurrent access is possible that late in object
552 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
554 struct osd_object *obj = osd_obj(l);
555 struct inode *inode = obj->oo_inode;
557 LINVRNT(osd_invariant(obj));
560 * If object is unlinked remove fid->ino mapping from object index.
567 if (osd_inode_unlinked(inode)) {
568 result = osd_inode_remove(env, obj);
570 LU_OBJECT_DEBUG(D_ERROR, env, l,
571 "Failed to cleanup: %d\n",
576 obj->oo_inode = NULL;
581 * Concurrency: ->loo_object_release() is called under site spin-lock.
583 static void osd_object_release(const struct lu_env *env,
586 struct osd_object *o = osd_obj(l);
588 LASSERT(!lu_object_is_dying(l->lo_header));
589 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
590 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
594 * Concurrency: shouldn't matter.
596 static int osd_object_print(const struct lu_env *env, void *cookie,
597 lu_printer_t p, const struct lu_object *l)
599 struct osd_object *o = osd_obj(l);
602 if (o->oo_dir != NULL)
603 d = o->oo_dir->od_container.ic_descr;
606 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
608 o->oo_inode ? o->oo_inode->i_ino : 0UL,
609 o->oo_inode ? o->oo_inode->i_generation : 0,
610 d ? d->id_ops->id_name : "plain");
614 * Concurrency: shouldn't matter.
616 int osd_statfs(const struct lu_env *env, struct dt_device *d,
619 struct osd_device *osd = osd_dt_dev(d);
620 struct super_block *sb = osd_sb(osd);
623 spin_lock(&osd->od_osfs_lock);
625 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
626 result = ll_do_statfs(sb, &osd->od_kstatfs);
627 if (likely(result == 0)) /* N.B. statfs can't really fail */
628 osd->od_osfs_age = cfs_time_current_64();
631 if (likely(result == 0))
632 *sfs = osd->od_kstatfs;
633 spin_unlock(&osd->od_osfs_lock);
639 * Concurrency: doesn't access mutable data.
641 static void osd_conf_get(const struct lu_env *env,
642 const struct dt_device *dev,
643 struct dt_device_param *param)
646 * XXX should be taken from not-yet-existing fs abstraction layer.
648 param->ddp_max_name_len = LDISKFS_NAME_LEN;
649 param->ddp_max_nlink = LDISKFS_LINK_MAX;
650 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
654 * Helper function to get and fill the buffer with input values.
656 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
660 buf = &osd_oti_get(env)->oti_buf;
671 * Concurrency: doesn't access mutable data.
673 static int osd_param_is_sane(const struct osd_device *dev,
674 const struct txn_param *param)
676 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
680 * Concurrency: shouldn't matter.
682 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
684 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
685 struct thandle *th = &oh->ot_super;
686 struct dt_device *dev = th->th_dev;
687 struct lu_device *lud = &dev->dd_lu_dev;
689 LASSERT(dev != NULL);
690 LASSERT(oh->ot_handle == NULL);
693 CERROR("transaction @0x%p commit error: %d\n", th, error);
695 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
697 * This od_env_for_commit is only for commit usage. see
700 lu_context_enter(&env->le_ctx);
701 dt_txn_hook_commit(env, th);
702 lu_context_exit(&env->le_ctx);
705 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
709 lu_context_exit(&th->th_ctx);
710 lu_context_fini(&th->th_ctx);
715 * Concurrency: shouldn't matter.
717 static struct thandle *osd_trans_start(const struct lu_env *env,
721 struct osd_device *dev = osd_dt_dev(d);
723 struct osd_thandle *oh;
729 hook_res = dt_txn_hook_start(env, d, p);
731 RETURN(ERR_PTR(hook_res));
733 if (osd_param_is_sane(dev, p)) {
734 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
736 struct osd_thread_info *oti = osd_oti_get(env);
739 * XXX temporary stuff. Some abstraction layer should
743 jh = journal_start(osd_journal(dev), p->tp_credits);
749 jh->h_sync = p->tp_sync;
750 lu_device_get(&d->dd_lu_dev);
751 oh->ot_dev_link = lu_ref_add
752 (&d->dd_lu_dev.ld_reference,
754 /* add commit callback */
755 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
756 lu_context_enter(&th->th_ctx);
757 journal_callback_set(jh, osd_trans_commit_cb,
758 (struct journal_callback *)&oh->ot_jcb);
759 LASSERT(oti->oti_txns == 0);
760 LASSERT(oti->oti_r_locks == 0);
761 LASSERT(oti->oti_w_locks == 0);
768 th = ERR_PTR(-ENOMEM);
770 CERROR("Invalid transaction parameters\n");
771 th = ERR_PTR(-EINVAL);
778 * Concurrency: shouldn't matter.
780 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
783 struct osd_thandle *oh;
784 struct osd_thread_info *oti = osd_oti_get(env);
788 oh = container_of0(th, struct osd_thandle, ot_super);
789 if (oh->ot_handle != NULL) {
790 handle_t *hdl = oh->ot_handle;
792 LASSERT(oti->oti_txns == 1);
794 LASSERT(oti->oti_r_locks == 0);
795 LASSERT(oti->oti_w_locks == 0);
796 result = dt_txn_hook_stop(env, th);
798 CERROR("Failure in transaction hook: %d\n", result);
799 oh->ot_handle = NULL;
800 result = journal_stop(hdl);
802 CERROR("Failure to stop transaction: %d\n", result);
808 * Concurrency: shouldn't matter.
810 static int osd_sync(const struct lu_env *env, struct dt_device *d)
812 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
813 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
817 * Start commit for OSD device.
819 * An implementation of dt_commit_async method for OSD device.
820 * Asychronously starts underlayng fs sync and thereby a transaction
823 * \param env environment
826 * \see dt_device_operations
828 static int osd_commit_async(const struct lu_env *env,
831 struct super_block *s = osd_sb(osd_dt_dev(d));
834 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
835 RETURN(s->s_op->sync_fs(s, 0));
839 * Concurrency: shouldn't matter.
841 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
843 static void osd_ro(const struct lu_env *env, struct dt_device *d)
847 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
849 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
850 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
856 * Concurrency: serialization provided by callers.
858 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
859 int mode, unsigned long timeout, __u32 alg,
860 struct lustre_capa_key *keys)
862 struct osd_device *dev = osd_dt_dev(d);
865 dev->od_fl_capa = mode;
866 dev->od_capa_timeout = timeout;
867 dev->od_capa_alg = alg;
868 dev->od_capa_keys = keys;
873 * Concurrency: serialization provided by callers.
875 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
876 struct dt_quota_ctxt *ctxt, void *data)
878 struct obd_device *obd = (void *)ctxt;
879 struct vfsmount *mnt = (struct vfsmount *)data;
882 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
883 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
884 obd->obd_lvfs_ctxt.pwdmnt = mnt;
885 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
886 obd->obd_lvfs_ctxt.fs = get_ds();
892 * Note: we do not count into QUOTA here.
893 * If we mount with --data_journal we may need more.
895 static const int osd_dto_credits_noquota[DTO_NR] = {
898 * INDEX_EXTRA_TRANS_BLOCKS(8) +
899 * SINGLEDATA_TRANS_BLOCKS(8)
900 * XXX Note: maybe iam need more, since iam have more level than
903 [DTO_INDEX_INSERT] = 16,
904 [DTO_INDEX_DELETE] = 16,
908 [DTO_IDNEX_UPDATE] = 16,
910 * Create a object. The same as create object in EXT3.
911 * DATA_TRANS_BLOCKS(14) +
912 * INDEX_EXTRA_BLOCKS(8) +
913 * 3(inode bits, groups, GDT)
915 [DTO_OBJECT_CREATE] = 25,
919 [DTO_OBJECT_DELETE] = 25,
922 * 3(inode bits, group, GDT)
924 [DTO_ATTR_SET_BASE] = 3,
926 * Xattr set. The same as xattr of EXT3.
927 * DATA_TRANS_BLOCKS(14)
928 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
929 * also counted in. Do not know why?
931 [DTO_XATTR_SET] = 14,
934 * creadits for inode change during write.
936 [DTO_WRITE_BASE] = 3,
938 * credits for single block write.
940 [DTO_WRITE_BLOCK] = 14,
942 * Attr set credits for chown.
943 * 3 (inode bit, group, GDT)
945 [DTO_ATTR_SET_CHOWN]= 3
949 * Note: we count into QUOTA here.
950 * If we mount with --data_journal we may need more.
952 static const int osd_dto_credits_quota[DTO_NR] = {
954 * INDEX_EXTRA_TRANS_BLOCKS(8) +
955 * SINGLEDATA_TRANS_BLOCKS(8) +
956 * 2 * QUOTA_TRANS_BLOCKS(2)
958 [DTO_INDEX_INSERT] = 20,
960 * INDEX_EXTRA_TRANS_BLOCKS(8) +
961 * SINGLEDATA_TRANS_BLOCKS(8) +
962 * 2 * QUOTA_TRANS_BLOCKS(2)
964 [DTO_INDEX_DELETE] = 20,
968 [DTO_IDNEX_UPDATE] = 16,
970 * Create a object. Same as create object in EXT3 filesystem.
971 * DATA_TRANS_BLOCKS(16) +
972 * INDEX_EXTRA_BLOCKS(8) +
973 * 3(inode bits, groups, GDT) +
974 * 2 * QUOTA_INIT_BLOCKS(25)
976 [DTO_OBJECT_CREATE] = 77,
979 * DATA_TRANS_BLOCKS(16) +
980 * INDEX_EXTRA_BLOCKS(8) +
981 * 3(inode bits, groups, GDT) +
984 [DTO_OBJECT_DELETE] = 27,
987 * 3 (inode bit, group, GDT) +
989 [DTO_ATTR_SET_BASE] = 3,
991 * Xattr set. The same as xattr of EXT3.
992 * DATA_TRANS_BLOCKS(16)
993 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
994 * also counted in. Do not know why?
996 [DTO_XATTR_SET] = 16,
999 * creadits for inode change during write.
1001 [DTO_WRITE_BASE] = 3,
1003 * credits for single block write.
1005 [DTO_WRITE_BLOCK] = 16,
1007 * Attr set credits for chown.
1008 * 3 (inode bit, group, GDT) +
1009 * 2 * QUOTA_INIT_BLOCKS(25) +
1010 * 2 * QUOTA_DEL_BLOCKS(9)
1012 [DTO_ATTR_SET_CHOWN]= 71
1015 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1018 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1019 ARRAY_SIZE(osd_dto_credits_quota));
1020 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1021 #ifdef HAVE_QUOTA_SUPPORT
1022 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1023 return osd_dto_credits_quota[op];
1026 return osd_dto_credits_noquota[op];
1029 static const struct dt_device_operations osd_dt_ops = {
1030 .dt_root_get = osd_root_get,
1031 .dt_statfs = osd_statfs,
1032 .dt_trans_start = osd_trans_start,
1033 .dt_trans_stop = osd_trans_stop,
1034 .dt_conf_get = osd_conf_get,
1035 .dt_sync = osd_sync,
1037 .dt_commit_async = osd_commit_async,
1038 .dt_credit_get = osd_credit_get,
1039 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1040 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1043 static void osd_object_read_lock(const struct lu_env *env,
1044 struct dt_object *dt, unsigned role)
1046 struct osd_object *obj = osd_dt_obj(dt);
1047 struct osd_thread_info *oti = osd_oti_get(env);
1049 LINVRNT(osd_invariant(obj));
1051 LASSERT(obj->oo_owner != env);
1052 down_read_nested(&obj->oo_sem, role);
1054 LASSERT(obj->oo_owner == NULL);
1058 static void osd_object_write_lock(const struct lu_env *env,
1059 struct dt_object *dt, unsigned role)
1061 struct osd_object *obj = osd_dt_obj(dt);
1062 struct osd_thread_info *oti = osd_oti_get(env);
1064 LINVRNT(osd_invariant(obj));
1066 LASSERT(obj->oo_owner != env);
1067 down_write_nested(&obj->oo_sem, role);
1069 LASSERT(obj->oo_owner == NULL);
1070 obj->oo_owner = env;
1074 static void osd_object_read_unlock(const struct lu_env *env,
1075 struct dt_object *dt)
1077 struct osd_object *obj = osd_dt_obj(dt);
1078 struct osd_thread_info *oti = osd_oti_get(env);
1080 LINVRNT(osd_invariant(obj));
1082 LASSERT(oti->oti_r_locks > 0);
1084 up_read(&obj->oo_sem);
1087 static void osd_object_write_unlock(const struct lu_env *env,
1088 struct dt_object *dt)
1090 struct osd_object *obj = osd_dt_obj(dt);
1091 struct osd_thread_info *oti = osd_oti_get(env);
1093 LINVRNT(osd_invariant(obj));
1095 LASSERT(obj->oo_owner == env);
1096 LASSERT(oti->oti_w_locks > 0);
1098 obj->oo_owner = NULL;
1099 up_write(&obj->oo_sem);
1102 static int capa_is_sane(const struct lu_env *env,
1103 struct osd_device *dev,
1104 struct lustre_capa *capa,
1105 struct lustre_capa_key *keys)
1107 struct osd_thread_info *oti = osd_oti_get(env);
1108 struct lustre_capa *tcapa = &oti->oti_capa;
1109 struct obd_capa *oc;
1113 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1115 if (capa_is_expired(oc)) {
1116 DEBUG_CAPA(D_ERROR, capa, "expired");
1123 if (capa_is_expired_sec(capa)) {
1124 DEBUG_CAPA(D_ERROR, capa, "expired");
1128 spin_lock(&capa_lock);
1129 for (i = 0; i < 2; i++) {
1130 if (keys[i].lk_keyid == capa->lc_keyid) {
1131 oti->oti_capa_key = keys[i];
1135 spin_unlock(&capa_lock);
1138 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1142 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1146 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1147 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1151 oc = capa_add(dev->od_capa_hash, capa);
1157 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1158 struct lustre_capa *capa, __u64 opc)
1160 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1161 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1162 struct md_capainfo *ci;
1165 if (!dev->od_fl_capa)
1168 if (capa == BYPASS_CAPA)
1171 ci = md_capainfo(env);
1175 if (ci->mc_auth == LC_ID_NONE)
1179 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1183 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1184 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1189 if (!capa_opc_supported(capa, opc)) {
1190 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1194 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1195 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1202 static int osd_attr_get(const struct lu_env *env,
1203 struct dt_object *dt,
1204 struct lu_attr *attr,
1205 struct lustre_capa *capa)
1207 struct osd_object *obj = osd_dt_obj(dt);
1209 LASSERT(dt_object_exists(dt));
1210 LINVRNT(osd_invariant(obj));
1212 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1215 spin_lock(&obj->oo_guard);
1216 osd_inode_getattr(env, obj->oo_inode, attr);
1217 spin_unlock(&obj->oo_guard);
1221 static int osd_attr_set(const struct lu_env *env,
1222 struct dt_object *dt,
1223 const struct lu_attr *attr,
1224 struct thandle *handle,
1225 struct lustre_capa *capa)
1227 struct osd_object *obj = osd_dt_obj(dt);
1230 LASSERT(handle != NULL);
1231 LASSERT(dt_object_exists(dt));
1232 LASSERT(osd_invariant(obj));
1234 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1237 spin_lock(&obj->oo_guard);
1238 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1239 spin_unlock(&obj->oo_guard);
1242 mark_inode_dirty(obj->oo_inode);
1246 static struct timespec *osd_inode_time(const struct lu_env *env,
1247 struct inode *inode, __u64 seconds)
1249 struct osd_thread_info *oti = osd_oti_get(env);
1250 struct timespec *t = &oti->oti_time;
1252 t->tv_sec = seconds;
1254 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1258 static int osd_inode_setattr(const struct lu_env *env,
1259 struct inode *inode, const struct lu_attr *attr)
1263 bits = attr->la_valid;
1265 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1267 #ifdef HAVE_QUOTA_SUPPORT
1268 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1269 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1270 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1276 iattr.ia_valid |= ATTR_UID;
1278 iattr.ia_valid |= ATTR_GID;
1279 iattr.ia_uid = attr->la_uid;
1280 iattr.ia_gid = attr->la_gid;
1281 osd_push_ctxt(env, save);
1282 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1289 if (bits & LA_ATIME)
1290 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1291 if (bits & LA_CTIME)
1292 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1293 if (bits & LA_MTIME)
1294 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1295 if (bits & LA_SIZE) {
1296 LDISKFS_I(inode)->i_disksize = attr->la_size;
1297 i_size_write(inode, attr->la_size);
1300 /* OSD should not change "i_blocks" which is used by quota.
1301 * "i_blocks" should be changed by ldiskfs only.
1302 * Enable this assignment for SOM purpose now, until it is
1303 * stored in SOM EA. */
1304 if (bits & LA_BLOCKS)
1305 inode->i_blocks = attr->la_blocks;
1308 inode->i_mode = (inode->i_mode & S_IFMT) |
1309 (attr->la_mode & ~S_IFMT);
1311 inode->i_uid = attr->la_uid;
1313 inode->i_gid = attr->la_gid;
1314 if (bits & LA_NLINK)
1315 inode->i_nlink = attr->la_nlink;
1317 inode->i_rdev = attr->la_rdev;
1319 if (bits & LA_FLAGS) {
1320 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1322 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1323 (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1331 * XXX temporary solution.
1334 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1335 struct lu_attr *attr, struct thandle *th)
1340 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1341 struct lu_attr *attr, struct thandle *th)
1343 osd_object_init0(obj);
1347 extern struct inode *ldiskfs_create_inode(handle_t *handle,
1348 struct inode * dir, int mode);
1349 extern int ldiskfs_add_entry(handle_t *handle, struct dentry *dentry,
1350 struct inode *inode);
1351 extern int ldiskfs_delete_entry(handle_t *handle,
1353 struct ldiskfs_dir_entry_2 * de_del,
1354 struct buffer_head * bh);
1355 extern struct buffer_head * ldiskfs_find_entry(struct dentry *dentry,
1356 struct ldiskfs_dir_entry_2
1358 extern int ldiskfs_add_dot_dotdot(handle_t *handle, struct inode *dir,
1359 struct inode *inode);
1361 extern int ldiskfs_xattr_set_handle(handle_t *handle, struct inode *inode,
1362 int name_index, const char *name,
1363 const void *value, size_t value_len,
1366 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1367 struct osd_object *obj,
1371 struct osd_thread_info *info = osd_oti_get(env);
1372 struct dentry *child_dentry = &info->oti_child_dentry;
1373 struct dentry *obj_dentry = &info->oti_obj_dentry;
1375 obj_dentry->d_inode = obj->oo_inode;
1376 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1377 obj_dentry->d_name.hash = 0;
1379 child_dentry->d_name.hash = 0;
1380 child_dentry->d_parent = obj_dentry;
1381 child_dentry->d_name.name = name;
1382 child_dentry->d_name.len = namelen;
1383 return child_dentry;
1387 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1389 struct dt_allocation_hint *hint,
1393 struct osd_device *osd = osd_obj2dev(obj);
1394 struct osd_thandle *oth;
1395 struct dt_object *parent;
1396 struct inode *inode;
1397 #ifdef HAVE_QUOTA_SUPPORT
1398 struct osd_ctxt *save = &info->oti_ctxt;
1401 LINVRNT(osd_invariant(obj));
1402 LASSERT(obj->oo_inode == NULL);
1404 oth = container_of(th, struct osd_thandle, ot_super);
1405 LASSERT(oth->ot_handle->h_transaction != NULL);
1407 if (hint && hint->dah_parent)
1408 parent = hint->dah_parent;
1410 parent = osd->od_obj_area;
1412 LASSERT(parent != NULL);
1413 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1415 #ifdef HAVE_QUOTA_SUPPORT
1416 osd_push_ctxt(info->oti_env, save);
1418 inode = ldiskfs_create_inode(oth->ot_handle,
1419 osd_dt_obj(parent)->oo_inode, mode);
1420 #ifdef HAVE_QUOTA_SUPPORT
1423 if (!IS_ERR(inode)) {
1424 obj->oo_inode = inode;
1427 result = PTR_ERR(inode);
1428 LINVRNT(osd_invariant(obj));
1433 extern int iam_lvar_create(struct inode *obj, int keysize, int ptrsize,
1434 int recsize, handle_t *handle);
1436 extern int iam_lfix_create(struct inode *obj, int keysize, int ptrsize,
1437 int recsize, handle_t *handle);
1444 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1445 struct lu_attr *attr,
1446 struct dt_allocation_hint *hint,
1447 struct dt_object_format *dof,
1451 struct osd_thandle *oth;
1452 struct osd_device *osd = osd_obj2dev(obj);
1453 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1455 LASSERT(S_ISDIR(attr->la_mode));
1457 oth = container_of(th, struct osd_thandle, ot_super);
1458 LASSERT(oth->ot_handle->h_transaction != NULL);
1459 result = osd_mkfile(info, obj, mode, hint, th);
1460 if (result == 0 && osd->od_iop_mode == 0) {
1461 LASSERT(obj->oo_inode != NULL);
1463 * XXX uh-oh... call low-level iam function directly.
1466 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1467 sizeof (struct lu_fid_pack),
1473 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1474 struct lu_attr *attr,
1475 struct dt_allocation_hint *hint,
1476 struct dt_object_format *dof,
1480 struct osd_thandle *oth;
1481 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1483 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1485 LASSERT(S_ISREG(attr->la_mode));
1487 oth = container_of(th, struct osd_thandle, ot_super);
1488 LASSERT(oth->ot_handle->h_transaction != NULL);
1490 result = osd_mkfile(info, obj, mode, hint, th);
1492 LASSERT(obj->oo_inode != NULL);
1493 if (feat->dif_flags & DT_IND_VARKEY)
1494 result = iam_lvar_create(obj->oo_inode,
1495 feat->dif_keysize_max,
1497 feat->dif_recsize_max,
1500 result = iam_lfix_create(obj->oo_inode,
1501 feat->dif_keysize_max,
1503 feat->dif_recsize_max,
1510 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1511 struct lu_attr *attr,
1512 struct dt_allocation_hint *hint,
1513 struct dt_object_format *dof,
1516 LASSERT(S_ISREG(attr->la_mode));
1517 return osd_mkfile(info, obj, (attr->la_mode &
1518 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1521 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1522 struct lu_attr *attr,
1523 struct dt_allocation_hint *hint,
1524 struct dt_object_format *dof,
1527 LASSERT(S_ISLNK(attr->la_mode));
1528 return osd_mkfile(info, obj, (attr->la_mode &
1529 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1532 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1533 struct lu_attr *attr,
1534 struct dt_allocation_hint *hint,
1535 struct dt_object_format *dof,
1538 umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1541 LINVRNT(osd_invariant(obj));
1542 LASSERT(obj->oo_inode == NULL);
1543 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1544 S_ISFIFO(mode) || S_ISSOCK(mode));
1546 result = osd_mkfile(info, obj, mode, hint, th);
1548 LASSERT(obj->oo_inode != NULL);
1549 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1551 LINVRNT(osd_invariant(obj));
1555 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1557 struct dt_allocation_hint *hint,
1558 struct dt_object_format *dof,
1561 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1563 osd_obj_type_f result;
1579 result = osd_mk_index;
1590 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1591 struct dt_object *parent, umode_t child_mode)
1595 memset(ah, 0, sizeof(*ah));
1596 ah->dah_parent = parent;
1597 ah->dah_mode = child_mode;
1601 * Helper function for osd_object_create()
1603 * \retval 0, on success
1605 static int __osd_object_create(struct osd_thread_info *info,
1606 struct osd_object *obj, struct lu_attr *attr,
1607 struct dt_allocation_hint *hint,
1608 struct dt_object_format *dof,
1614 result = osd_create_pre(info, obj, attr, th);
1616 result = osd_create_type_f(dof->dof_type)(info, obj,
1617 attr, hint, dof, th);
1619 result = osd_create_post(info, obj, attr, th);
1625 * Helper function for osd_object_create()
1627 * \retval 0, on success
1629 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1630 const struct lu_fid *fid, struct thandle *th)
1632 struct osd_thread_info *info = osd_oti_get(env);
1633 struct osd_inode_id *id = &info->oti_id;
1634 struct osd_device *osd = osd_obj2dev(obj);
1635 struct md_ucred *uc = md_ucred(env);
1637 LASSERT(obj->oo_inode != NULL);
1638 LASSERT(uc != NULL);
1640 id->oii_ino = obj->oo_inode->i_ino;
1641 id->oii_gen = obj->oo_inode->i_generation;
1643 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1644 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1647 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1648 struct lu_attr *attr,
1649 struct dt_allocation_hint *hint,
1650 struct dt_object_format *dof,
1653 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1654 struct osd_object *obj = osd_dt_obj(dt);
1655 struct osd_thread_info *info = osd_oti_get(env);
1660 LINVRNT(osd_invariant(obj));
1661 LASSERT(!dt_object_exists(dt));
1662 LASSERT(osd_write_locked(env, obj));
1663 LASSERT(th != NULL);
1665 result = __osd_object_create(info, obj, attr, hint, dof, th);
1667 result = __osd_oi_insert(env, obj, fid, th);
1669 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1670 LASSERT(osd_invariant(obj));
1675 * Helper function for osd_xattr_set()
1677 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1678 const struct lu_buf *buf, const char *name, int fl)
1680 struct osd_object *obj = osd_dt_obj(dt);
1681 struct inode *inode = obj->oo_inode;
1682 struct osd_thread_info *info = osd_oti_get(env);
1683 struct dentry *dentry = &info->oti_child_dentry;
1684 struct timespec *t = &info->oti_time;
1688 LASSERT(dt_object_exists(dt));
1689 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1690 LASSERT(osd_write_locked(env, obj));
1692 if (fl & LU_XATTR_REPLACE)
1693 fs_flags |= XATTR_REPLACE;
1695 if (fl & LU_XATTR_CREATE)
1696 fs_flags |= XATTR_CREATE;
1698 dentry->d_inode = inode;
1699 *t = inode->i_ctime;
1700 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1701 buf->lb_len, fs_flags);
1702 /* ctime should not be updated with server-side time. */
1703 spin_lock(&obj->oo_guard);
1704 inode->i_ctime = *t;
1705 spin_unlock(&obj->oo_guard);
1706 mark_inode_dirty(inode);
1711 * Put the fid into lustre_mdt_attrs, and then place the structure
1712 * inode's ea. This fid should not be altered during the life time
1715 * \retval +ve, on success
1716 * \retval -ve, on error
1718 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1720 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1721 const struct lu_fid *fid)
1723 struct osd_thread_info *info = osd_oti_get(env);
1724 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1726 fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1728 return __osd_xattr_set(env, dt,
1729 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1730 XATTR_NAME_LMA, LU_XATTR_CREATE);
1735 * Helper function to form igif
1737 static inline void osd_igif_get(const struct lu_env *env, struct dentry *dentry,
1740 struct inode *inode = dentry->d_inode;
1741 lu_igif_build(fid, inode->i_ino, inode->i_generation);
1745 * Helper function to pack the fid
1747 static inline void osd_fid_pack(const struct lu_env *env, const struct lu_fid *fid,
1748 struct lu_fid_pack *pack)
1750 fid_pack(pack, fid, &osd_oti_get(env)->oti_fid);
1754 * Try to read the fid from inode ea into dt_rec, if return value
1755 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1757 * \param rec, the data-structure into which fid/igif is read
1759 * \retval 0, on success
1761 static int osd_ea_fid_get(const struct lu_env *env, struct dentry *dentry,
1764 struct inode *inode = dentry->d_inode;
1765 struct osd_thread_info *info = osd_oti_get(env);
1766 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1767 struct lu_fid *fid = &info->oti_fid;
1770 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1772 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1775 /* Check LMA compatibility */
1777 (mdt_attrs->lma_incompat & ~cpu_to_be32(LMA_INCOMPAT_SUPP))) {
1778 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1779 inode->i_ino, be32_to_cpu(mdt_attrs->lma_incompat) &
1780 ~LMA_INCOMPAT_SUPP);
1785 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1787 } else if (rc == -ENODATA) {
1788 osd_igif_get(env, dentry, fid);
1793 osd_fid_pack(env, fid, (struct lu_fid_pack*)rec);
1799 * OSD layer object create function for interoperability mode (b11826).
1800 * This is mostly similar to osd_object_create(). Only difference being, fid is
1801 * inserted into inode ea here.
1803 * \retval 0, on success
1804 * \retval -ve, on error
1806 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1807 struct lu_attr *attr,
1808 struct dt_allocation_hint *hint,
1809 struct dt_object_format *dof,
1812 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1813 struct osd_object *obj = osd_dt_obj(dt);
1814 struct osd_thread_info *info = osd_oti_get(env);
1820 LASSERT(osd_invariant(obj));
1821 LASSERT(!dt_object_exists(dt));
1822 LASSERT(osd_write_locked(env, obj));
1823 LASSERT(th != NULL);
1825 result = __osd_object_create(info, obj, attr, hint, dof, th);
1827 if (hint && hint->dah_parent)
1828 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1830 /* objects under osd root shld have igif fid, so dont add fid EA */
1831 if (result == 0 && is_root == 0)
1832 result = osd_ea_fid_set(env, dt, fid);
1835 result = __osd_oi_insert(env, obj, fid, th);
1837 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1838 LINVRNT(osd_invariant(obj));
1843 * Concurrency: @dt is write locked.
1845 static void osd_object_ref_add(const struct lu_env *env,
1846 struct dt_object *dt,
1849 struct osd_object *obj = osd_dt_obj(dt);
1850 struct inode *inode = obj->oo_inode;
1852 LINVRNT(osd_invariant(obj));
1853 LASSERT(dt_object_exists(dt));
1854 LASSERT(osd_write_locked(env, obj));
1855 LASSERT(th != NULL);
1857 spin_lock(&obj->oo_guard);
1858 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1860 spin_unlock(&obj->oo_guard);
1861 mark_inode_dirty(inode);
1862 LINVRNT(osd_invariant(obj));
1866 * Concurrency: @dt is write locked.
1868 static void osd_object_ref_del(const struct lu_env *env,
1869 struct dt_object *dt,
1872 struct osd_object *obj = osd_dt_obj(dt);
1873 struct inode *inode = obj->oo_inode;
1875 LINVRNT(osd_invariant(obj));
1876 LASSERT(dt_object_exists(dt));
1877 LASSERT(osd_write_locked(env, obj));
1878 LASSERT(th != NULL);
1880 spin_lock(&obj->oo_guard);
1881 LASSERT(inode->i_nlink > 0);
1883 spin_unlock(&obj->oo_guard);
1884 mark_inode_dirty(inode);
1885 LINVRNT(osd_invariant(obj));
1889 * Concurrency: @dt is read locked.
1891 static int osd_xattr_get(const struct lu_env *env,
1892 struct dt_object *dt,
1895 struct lustre_capa *capa)
1897 struct osd_object *obj = osd_dt_obj(dt);
1898 struct inode *inode = obj->oo_inode;
1899 struct osd_thread_info *info = osd_oti_get(env);
1900 struct dentry *dentry = &info->oti_obj_dentry;
1902 LASSERT(dt_object_exists(dt));
1903 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1904 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1906 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1909 dentry->d_inode = inode;
1910 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1915 * Concurrency: @dt is write locked.
1917 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1918 const struct lu_buf *buf, const char *name, int fl,
1919 struct thandle *handle, struct lustre_capa *capa)
1921 LASSERT(handle != NULL);
1923 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1926 return __osd_xattr_set(env, dt, buf, name, fl);
1930 * Concurrency: @dt is read locked.
1932 static int osd_xattr_list(const struct lu_env *env,
1933 struct dt_object *dt,
1935 struct lustre_capa *capa)
1937 struct osd_object *obj = osd_dt_obj(dt);
1938 struct inode *inode = obj->oo_inode;
1939 struct osd_thread_info *info = osd_oti_get(env);
1940 struct dentry *dentry = &info->oti_obj_dentry;
1942 LASSERT(dt_object_exists(dt));
1943 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1944 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1946 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1949 dentry->d_inode = inode;
1950 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1954 * Concurrency: @dt is write locked.
1956 static int osd_xattr_del(const struct lu_env *env,
1957 struct dt_object *dt,
1959 struct thandle *handle,
1960 struct lustre_capa *capa)
1962 struct osd_object *obj = osd_dt_obj(dt);
1963 struct inode *inode = obj->oo_inode;
1964 struct osd_thread_info *info = osd_oti_get(env);
1965 struct dentry *dentry = &info->oti_obj_dentry;
1966 struct timespec *t = &info->oti_time;
1969 LASSERT(dt_object_exists(dt));
1970 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1971 LASSERT(osd_write_locked(env, obj));
1972 LASSERT(handle != NULL);
1974 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1977 dentry->d_inode = inode;
1978 *t = inode->i_ctime;
1979 rc = inode->i_op->removexattr(dentry, name);
1980 /* ctime should not be updated with server-side time. */
1981 spin_lock(&obj->oo_guard);
1982 inode->i_ctime = *t;
1983 spin_unlock(&obj->oo_guard);
1984 mark_inode_dirty(inode);
1988 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1989 struct dt_object *dt,
1990 struct lustre_capa *old,
1993 struct osd_thread_info *info = osd_oti_get(env);
1994 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1995 struct osd_object *obj = osd_dt_obj(dt);
1996 struct osd_device *dev = osd_obj2dev(obj);
1997 struct lustre_capa_key *key = &info->oti_capa_key;
1998 struct lustre_capa *capa = &info->oti_capa;
1999 struct obd_capa *oc;
2000 struct md_capainfo *ci;
2004 if (!dev->od_fl_capa)
2005 RETURN(ERR_PTR(-ENOENT));
2007 LASSERT(dt_object_exists(dt));
2008 LINVRNT(osd_invariant(obj));
2010 /* renewal sanity check */
2011 if (old && osd_object_auth(env, dt, old, opc))
2012 RETURN(ERR_PTR(-EACCES));
2014 ci = md_capainfo(env);
2016 RETURN(ERR_PTR(-ENOENT));
2018 switch (ci->mc_auth) {
2022 capa->lc_uid = obj->oo_inode->i_uid;
2023 capa->lc_gid = obj->oo_inode->i_gid;
2024 capa->lc_flags = LC_ID_PLAIN;
2026 case LC_ID_CONVERT: {
2029 s[0] = obj->oo_inode->i_uid;
2030 get_random_bytes(&(s[1]), sizeof(__u32));
2031 s[2] = obj->oo_inode->i_gid;
2032 get_random_bytes(&(s[3]), sizeof(__u32));
2033 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2035 RETURN(ERR_PTR(rc));
2037 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2038 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2039 capa->lc_flags = LC_ID_CONVERT;
2043 RETURN(ERR_PTR(-EINVAL));
2046 capa->lc_fid = *fid;
2048 capa->lc_flags |= dev->od_capa_alg << 24;
2049 capa->lc_timeout = dev->od_capa_timeout;
2050 capa->lc_expiry = 0;
2052 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2054 LASSERT(!capa_is_expired(oc));
2058 spin_lock(&capa_lock);
2059 *key = dev->od_capa_keys[1];
2060 spin_unlock(&capa_lock);
2062 capa->lc_keyid = key->lk_keyid;
2063 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2065 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2067 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2068 RETURN(ERR_PTR(rc));
2071 oc = capa_add(dev->od_capa_hash, capa);
2075 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2078 struct osd_object *obj = osd_dt_obj(dt);
2079 struct inode *inode = obj->oo_inode;
2080 struct osd_thread_info *info = osd_oti_get(env);
2081 struct dentry *dentry = &info->oti_obj_dentry;
2082 struct file *file = &info->oti_file;
2085 dentry->d_inode = inode;
2086 file->f_dentry = dentry;
2087 file->f_mapping = inode->i_mapping;
2088 file->f_op = inode->i_fop;
2089 LOCK_INODE_MUTEX(inode);
2090 rc = file->f_op->fsync(file, dentry, 0);
2091 UNLOCK_INODE_MUTEX(inode);
2096 * Get the 64-bit version for an inode.
2098 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2099 struct dt_object *dt)
2101 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2103 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2104 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2105 return LDISKFS_I(inode)->i_fs_version;
2109 * Set the 64-bit version and return the old version.
2111 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2112 dt_obj_version_t new_version)
2114 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2116 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2117 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2118 LDISKFS_I(inode)->i_fs_version = new_version;
2119 /** Version is set after all inode operations are finished,
2120 * so we should mark it dirty here */
2121 inode->i_sb->s_op->dirty_inode(inode);
2124 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2127 struct osd_object *obj = osd_dt_obj(dt);
2130 *data = (void *)obj->oo_inode;
2134 static const struct dt_object_operations osd_obj_ops = {
2135 .do_read_lock = osd_object_read_lock,
2136 .do_write_lock = osd_object_write_lock,
2137 .do_read_unlock = osd_object_read_unlock,
2138 .do_write_unlock = osd_object_write_unlock,
2139 .do_attr_get = osd_attr_get,
2140 .do_attr_set = osd_attr_set,
2141 .do_ah_init = osd_ah_init,
2142 .do_create = osd_object_create,
2143 .do_index_try = osd_index_try,
2144 .do_ref_add = osd_object_ref_add,
2145 .do_ref_del = osd_object_ref_del,
2146 .do_xattr_get = osd_xattr_get,
2147 .do_xattr_set = osd_xattr_set,
2148 .do_xattr_del = osd_xattr_del,
2149 .do_xattr_list = osd_xattr_list,
2150 .do_capa_get = osd_capa_get,
2151 .do_object_sync = osd_object_sync,
2152 .do_version_get = osd_object_version_get,
2153 .do_version_set = osd_object_version_set,
2154 .do_data_get = osd_data_get,
2158 * dt_object_operations for interoperability mode
2159 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2161 static const struct dt_object_operations osd_obj_ea_ops = {
2162 .do_read_lock = osd_object_read_lock,
2163 .do_write_lock = osd_object_write_lock,
2164 .do_read_unlock = osd_object_read_unlock,
2165 .do_write_unlock = osd_object_write_unlock,
2166 .do_attr_get = osd_attr_get,
2167 .do_attr_set = osd_attr_set,
2168 .do_ah_init = osd_ah_init,
2169 .do_create = osd_object_ea_create,
2170 .do_index_try = osd_index_try,
2171 .do_ref_add = osd_object_ref_add,
2172 .do_ref_del = osd_object_ref_del,
2173 .do_xattr_get = osd_xattr_get,
2174 .do_xattr_set = osd_xattr_set,
2175 .do_xattr_del = osd_xattr_del,
2176 .do_xattr_list = osd_xattr_list,
2177 .do_capa_get = osd_capa_get,
2178 .do_object_sync = osd_object_sync,
2179 .do_version_get = osd_object_version_get,
2180 .do_version_set = osd_object_version_set,
2181 .do_data_get = osd_data_get,
2189 * XXX: Another layering violation for now.
2191 * We don't want to use ->f_op->read methods, because generic file write
2193 * - serializes on ->i_sem, and
2195 * - does a lot of extra work like balance_dirty_pages(),
2197 * which doesn't work for globally shared files like /last-received.
2199 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2200 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2201 loff_t *offs, handle_t *handle);
2203 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2204 struct lu_buf *buf, loff_t *pos,
2205 struct lustre_capa *capa)
2207 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2209 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2212 return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2215 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2216 const struct lu_buf *buf, loff_t *pos,
2217 struct thandle *handle, struct lustre_capa *capa,
2220 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2221 struct osd_thandle *oh;
2223 #ifdef HAVE_QUOTA_SUPPORT
2224 cfs_cap_t save = current->cap_effective;
2227 LASSERT(handle != NULL);
2229 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2232 oh = container_of(handle, struct osd_thandle, ot_super);
2233 LASSERT(oh->ot_handle->h_transaction != NULL);
2234 #ifdef HAVE_QUOTA_SUPPORT
2236 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2238 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2240 result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2241 pos, oh->ot_handle);
2242 #ifdef HAVE_QUOTA_SUPPORT
2243 current->cap_effective = save;
2246 result = buf->lb_len;
2250 static const struct dt_body_operations osd_body_ops = {
2251 .dbo_read = osd_read,
2252 .dbo_write = osd_write
2259 static int osd_object_is_root(const struct osd_object *obj)
2261 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
2264 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2265 const struct dt_index_features *feat)
2267 struct iam_descr *descr;
2268 struct dt_object *dt = &o->oo_dt;
2270 if (osd_object_is_root(o))
2271 return feat == &dt_directory_features;
2273 LASSERT(o->oo_dir != NULL);
2275 descr = o->oo_dir->od_container.ic_descr;
2276 if (feat == &dt_directory_features) {
2277 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2280 if (descr == &iam_htree_compat_param) {
2281 /* if it is a HTREE dir then there is good chance that,
2282 * we dealing with ext3 directory here with no FIDs. */
2284 if (descr->id_rec_size ==
2285 sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2287 dt->do_index_ops = &osd_index_ea_ops;
2294 feat->dif_keysize_min <= descr->id_key_size &&
2295 descr->id_key_size <= feat->dif_keysize_max &&
2296 feat->dif_recsize_min <= descr->id_rec_size &&
2297 descr->id_rec_size <= feat->dif_recsize_max &&
2298 !(feat->dif_flags & (DT_IND_VARKEY |
2299 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2300 ergo(feat->dif_flags & DT_IND_UPDATE,
2301 1 /* XXX check that object (and file system) is
2306 static int osd_iam_container_init(const struct lu_env *env,
2307 struct osd_object *obj,
2308 struct osd_directory *dir)
2311 struct iam_container *bag;
2313 bag = &dir->od_container;
2314 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2316 result = iam_container_setup(bag);
2318 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2320 iam_container_fini(bag);
2326 * Concurrency: no external locking is necessary.
2328 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2329 const struct dt_index_features *feat)
2333 struct osd_object *obj = osd_dt_obj(dt);
2334 struct osd_device *osd = osd_obj2dev(obj);
2336 LINVRNT(osd_invariant(obj));
2337 LASSERT(dt_object_exists(dt));
2339 if (osd_object_is_root(obj)) {
2340 dt->do_index_ops = &osd_index_ea_ops;
2342 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2343 dt->do_index_ops = &osd_index_ea_ops;
2344 if (S_ISDIR(obj->oo_inode->i_mode))
2349 } else if (!osd_has_index(obj)) {
2350 struct osd_directory *dir;
2355 spin_lock(&obj->oo_guard);
2356 if (obj->oo_dir == NULL)
2360 * Concurrent thread allocated container data.
2363 spin_unlock(&obj->oo_guard);
2365 * Now, that we have container data, serialize its
2368 down_write(&obj->oo_ext_idx_sem);
2370 * recheck under lock.
2372 if (!osd_has_index(obj))
2373 result = osd_iam_container_init(env, obj, dir);
2376 up_write(&obj->oo_ext_idx_sem);
2382 if (result == 0 && ea_dir == 0) {
2383 if (!osd_iam_index_probe(env, obj, feat))
2386 LINVRNT(osd_invariant(obj));
2392 * delete a (key, value) pair from index \a dt specified by \a key
2394 * \param dt_object osd index object
2395 * \param key key for index
2396 * \param rec record reference
2397 * \param handle transaction handler
2400 * \retval -ve failure
2403 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2404 const struct dt_key *key, struct thandle *handle,
2405 struct lustre_capa *capa)
2407 struct osd_object *obj = osd_dt_obj(dt);
2408 struct osd_thandle *oh;
2409 struct iam_path_descr *ipd;
2410 struct iam_container *bag = &obj->oo_dir->od_container;
2415 LINVRNT(osd_invariant(obj));
2416 LASSERT(dt_object_exists(dt));
2417 LASSERT(bag->ic_object == obj->oo_inode);
2418 LASSERT(handle != NULL);
2420 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2423 ipd = osd_idx_ipd_get(env, bag);
2424 if (unlikely(ipd == NULL))
2427 oh = container_of0(handle, struct osd_thandle, ot_super);
2428 LASSERT(oh->ot_handle != NULL);
2429 LASSERT(oh->ot_handle->h_transaction != NULL);
2431 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2432 osd_ipd_put(env, bag, ipd);
2433 LINVRNT(osd_invariant(obj));
2438 * Index delete function for interoperability mode (b11826).
2439 * It will remove the directory entry added by osd_index_ea_insert().
2440 * This entry is needed to maintain name->fid mapping.
2442 * \param key, key i.e. file entry to be deleted
2444 * \retval 0, on success
2445 * \retval -ve, on error
2447 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2448 const struct dt_key *key, struct thandle *handle,
2449 struct lustre_capa *capa)
2451 struct osd_object *obj = osd_dt_obj(dt);
2452 struct inode *dir = obj->oo_inode;
2453 struct dentry *dentry;
2454 struct osd_thandle *oh;
2455 struct ldiskfs_dir_entry_2 *de;
2456 struct buffer_head *bh;
2462 LINVRNT(osd_invariant(obj));
2463 LASSERT(dt_object_exists(dt));
2464 LASSERT(handle != NULL);
2466 oh = container_of(handle, struct osd_thandle, ot_super);
2467 LASSERT(oh->ot_handle != NULL);
2468 LASSERT(oh->ot_handle->h_transaction != NULL);
2470 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2473 dentry = osd_child_dentry_get(env, obj,
2474 (char *)key, strlen((char *)key));
2476 down_write(&obj->oo_ext_idx_sem);
2477 bh = ldiskfs_find_entry(dentry, &de);
2479 struct osd_thread_info *oti = osd_oti_get(env);
2480 struct timespec *ctime = &oti->oti_time;
2481 struct timespec *mtime = &oti->oti_time2;
2483 *ctime = dir->i_ctime;
2484 *mtime = dir->i_mtime;
2485 rc = ldiskfs_delete_entry(oh->ot_handle,
2487 /* xtime should not be updated with server-side time. */
2488 spin_lock(&obj->oo_guard);
2489 dir->i_ctime = *ctime;
2490 dir->i_mtime = *mtime;
2491 spin_unlock(&obj->oo_guard);
2492 mark_inode_dirty(dir);
2497 up_write(&obj->oo_ext_idx_sem);
2498 LASSERT(osd_invariant(obj));
2503 * Lookup index for \a key and copy record to \a rec.
2505 * \param dt_object osd index object
2506 * \param key key for index
2507 * \param rec record reference
2509 * \retval +ve success : exact mach
2510 * \retval 0 return record with key not greater than \a key
2511 * \retval -ve failure
2513 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2514 struct dt_rec *rec, const struct dt_key *key,
2515 struct lustre_capa *capa)
2517 struct osd_object *obj = osd_dt_obj(dt);
2518 struct iam_path_descr *ipd;
2519 struct iam_container *bag = &obj->oo_dir->od_container;
2520 struct osd_thread_info *oti = osd_oti_get(env);
2521 struct iam_iterator *it = &oti->oti_idx_it;
2525 LASSERT(osd_invariant(obj));
2526 LASSERT(dt_object_exists(dt));
2527 LASSERT(bag->ic_object == obj->oo_inode);
2529 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2532 ipd = osd_idx_ipd_get(env, bag);
2536 /* got ipd now we can start iterator. */
2537 iam_it_init(it, bag, 0, ipd);
2539 rc = iam_it_get(it, (struct iam_key *)key);
2541 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2545 osd_ipd_put(env, bag, ipd);
2547 LINVRNT(osd_invariant(obj));
2553 * Inserts (key, value) pair in \a dt index object.
2555 * \param dt osd index object
2556 * \param key key for index
2557 * \param rec record reference
2558 * \param th transaction handler
2561 * \retval -ve failure
2563 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2564 const struct dt_rec *rec, const struct dt_key *key,
2565 struct thandle *th, struct lustre_capa *capa,
2568 struct osd_object *obj = osd_dt_obj(dt);
2569 struct iam_path_descr *ipd;
2570 struct osd_thandle *oh;
2571 struct iam_container *bag = &obj->oo_dir->od_container;
2572 #ifdef HAVE_QUOTA_SUPPORT
2573 cfs_cap_t save = current->cap_effective;
2579 LINVRNT(osd_invariant(obj));
2580 LASSERT(dt_object_exists(dt));
2581 LASSERT(bag->ic_object == obj->oo_inode);
2582 LASSERT(th != NULL);
2584 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2587 ipd = osd_idx_ipd_get(env, bag);
2588 if (unlikely(ipd == NULL))
2591 oh = container_of0(th, struct osd_thandle, ot_super);
2592 LASSERT(oh->ot_handle != NULL);
2593 LASSERT(oh->ot_handle->h_transaction != NULL);
2594 #ifdef HAVE_QUOTA_SUPPORT
2596 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2598 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2600 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2601 (struct iam_rec *)rec, ipd);
2602 #ifdef HAVE_QUOTA_SUPPORT
2603 current->cap_effective = save;
2605 osd_ipd_put(env, bag, ipd);
2606 LINVRNT(osd_invariant(obj));
2611 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2612 * into the directory.Also sets flags into osd object to
2613 * indicate dot and dotdot are created. This is required for
2614 * interoperability mode (b11826)
2616 * \param dir directory for dot and dotdot fixup.
2617 * \param obj child object for linking
2619 * \retval 0, on success
2620 * \retval -ve, on error
2622 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2623 struct osd_object *dir,
2624 struct osd_object *obj, const char *name,
2627 struct inode *parent_dir = obj->oo_inode;
2628 struct inode *inode = dir->oo_inode;
2629 struct osd_thandle *oth;
2632 oth = container_of(th, struct osd_thandle, ot_super);
2633 LASSERT(oth->ot_handle->h_transaction != NULL);
2634 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2636 if (strcmp(name, dot) == 0) {
2637 if (dir->oo_compat_dot_created) {
2640 LASSERT(obj == dir);
2641 dir->oo_compat_dot_created = 1;
2644 } else if(strcmp(name, dotdot) == 0) {
2645 if (!dir->oo_compat_dot_created)
2647 if (dir->oo_compat_dotdot_created)
2648 return __osd_ea_add_rec(info, dir, obj, name, th);
2650 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2652 dir->oo_compat_dotdot_created = 1;
2659 * Calls ldiskfs_add_entry() to add directory entry
2660 * into the directory. This is required for
2661 * interoperability mode (b11826)
2663 * \retval 0, on success
2664 * \retval -ve, on error
2666 static int __osd_ea_add_rec(struct osd_thread_info *info,
2667 struct osd_object *pobj,
2668 struct osd_object *cobj,
2672 struct dentry *child;
2673 struct osd_thandle *oth;
2674 struct inode *cinode = cobj->oo_inode;
2677 oth = container_of(th, struct osd_thandle, ot_super);
2678 LASSERT(oth->ot_handle != NULL);
2679 LASSERT(oth->ot_handle->h_transaction != NULL);
2681 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2682 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2688 * It will call the appropriate osd_add* function and return the
2689 * value, return by respective functions.
2691 static int osd_ea_add_rec(const struct lu_env *env,
2692 struct osd_object *pobj,
2693 struct osd_object *cobj,
2697 struct osd_thread_info *info = osd_oti_get(env);
2700 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2702 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2704 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2710 * Calls ->lookup() to find dentry. From dentry get inode and
2711 * read inode's ea to get fid. This is required for interoperability
2714 * \retval 0, on success
2715 * \retval -ve, on error
2717 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2718 struct dt_rec *rec, const struct dt_key *key)
2720 struct inode *dir = obj->oo_inode;
2721 struct osd_thread_info *info = osd_oti_get(env);
2722 struct dentry *dentry;
2723 struct osd_device *dev = osd_dev(obj->oo_dt.do_lu.lo_dev);
2724 struct osd_inode_id *id = &info->oti_id;
2725 struct ldiskfs_dir_entry_2 *de;
2726 struct buffer_head *bh;
2727 struct inode *inode;
2731 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2733 dentry = osd_child_dentry_get(env, obj,
2734 (char *)key, strlen((char *)key));
2736 down_read(&obj->oo_ext_idx_sem);
2737 bh = ldiskfs_find_entry(dentry, &de);
2739 ino = le32_to_cpu(de->inode);
2742 id->oii_gen = OSD_OII_NOGEN;
2744 inode = osd_iget(info, dev, id);
2745 if (!IS_ERR(inode)) {
2746 dentry->d_inode = inode;
2748 rc = osd_ea_fid_get(env, dentry, rec);
2751 rc = PTR_ERR(inode);
2755 up_read(&obj->oo_ext_idx_sem);
2760 * Find the osd object for given fid.
2762 * \param fid, need to find the osd object having this fid
2764 * \retval osd_object, on success
2765 * \retval -ve, on error
2767 struct osd_object *osd_object_find(const struct lu_env *env,
2768 struct dt_object *dt,
2769 const struct lu_fid *fid)
2771 struct lu_device *ludev = dt->do_lu.lo_dev;
2772 struct osd_object *child = NULL;
2773 struct lu_object *luch;
2774 struct lu_object *lo;
2776 luch = lu_object_find(env, ludev, fid, NULL);
2777 if (!IS_ERR(luch)) {
2778 if (lu_object_exists(luch)) {
2779 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2781 child = osd_obj(lo);
2783 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2784 "lu_object can't be located"
2785 ""DFID"\n", PFID(fid));
2787 if (child == NULL) {
2788 lu_object_put(env, luch);
2789 CERROR("Unable to get osd_object\n");
2790 child = ERR_PTR(-ENOENT);
2793 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2794 "lu_object does not exists "DFID"\n",
2796 child = ERR_PTR(-ENOENT);
2799 child = (void *)luch;
2805 * Put the osd object once done with it.
2807 * \param obj, osd object that needs to be put
2809 static inline void osd_object_put(const struct lu_env *env,
2810 struct osd_object *obj)
2812 lu_object_put(env, &obj->oo_dt.do_lu);
2816 * Index add function for interoperability mode (b11826).
2817 * It will add the directory entry.This entry is needed to
2818 * maintain name->fid mapping.
2820 * \param key, it is key i.e. file entry to be inserted
2821 * \param rec, it is value of given key i.e. fid
2823 * \retval 0, on success
2824 * \retval -ve, on error
2826 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2827 const struct dt_rec *rec,
2828 const struct dt_key *key, struct thandle *th,
2829 struct lustre_capa *capa, int ignore_quota)
2831 struct osd_object *obj = osd_dt_obj(dt);
2832 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
2833 const struct lu_fid_pack *pack = (const struct lu_fid_pack *)rec;
2834 const char *name = (const char *)key;
2835 struct osd_object *child;
2836 #ifdef HAVE_QUOTA_SUPPORT
2837 cfs_cap_t save = current->cap_effective;
2843 LASSERT(osd_invariant(obj));
2844 LASSERT(dt_object_exists(dt));
2845 LASSERT(th != NULL);
2847 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2850 rc = fid_unpack(pack, fid);
2853 child = osd_object_find(env, dt, fid);
2854 if (!IS_ERR(child)) {
2855 struct inode *inode = obj->oo_inode;
2856 struct osd_thread_info *oti = osd_oti_get(env);
2857 struct timespec *ctime = &oti->oti_time;
2858 struct timespec *mtime = &oti->oti_time2;
2860 *ctime = inode->i_ctime;
2861 *mtime = inode->i_mtime;
2862 #ifdef HAVE_QUOTA_SUPPORT
2864 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2866 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2868 down_write(&obj->oo_ext_idx_sem);
2869 rc = osd_ea_add_rec(env, obj, child, name, th);
2870 up_write(&obj->oo_ext_idx_sem);
2871 #ifdef HAVE_QUOTA_SUPPORT
2872 current->cap_effective = save;
2874 osd_object_put(env, child);
2875 /* xtime should not be updated with server-side time. */
2876 spin_lock(&obj->oo_guard);
2877 inode->i_ctime = *ctime;
2878 inode->i_mtime = *mtime;
2879 spin_unlock(&obj->oo_guard);
2880 mark_inode_dirty(inode);
2882 rc = PTR_ERR(child);
2885 LASSERT(osd_invariant(obj));
2890 * Initialize osd Iterator for given osd index object.
2892 * \param dt osd index object
2895 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2896 struct dt_object *dt,
2897 struct lustre_capa *capa)
2899 struct osd_it_iam *it;
2900 struct osd_thread_info *oti = osd_oti_get(env);
2901 struct osd_object *obj = osd_dt_obj(dt);
2902 struct lu_object *lo = &dt->do_lu;
2903 struct iam_path_descr *ipd;
2904 struct iam_container *bag = &obj->oo_dir->od_container;
2906 LASSERT(lu_object_exists(lo));
2908 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2909 return ERR_PTR(-EACCES);
2912 ipd = osd_it_ipd_get(env, bag);
2913 if (likely(ipd != NULL)) {
2917 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2918 return (struct dt_it *)it;
2920 return ERR_PTR(-ENOMEM);
2924 * free given Iterator.
2927 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2929 struct osd_it_iam *it = (struct osd_it_iam *)di;
2930 struct osd_object *obj = it->oi_obj;
2932 iam_it_fini(&it->oi_it);
2933 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2934 lu_object_put(env, &obj->oo_dt.do_lu);
2938 * Move Iterator to record specified by \a key
2940 * \param di osd iterator
2941 * \param key key for index
2943 * \retval +ve di points to record with least key not larger than key
2944 * \retval 0 di points to exact matched key
2945 * \retval -ve failure
2948 static int osd_it_iam_get(const struct lu_env *env,
2949 struct dt_it *di, const struct dt_key *key)
2951 struct osd_it_iam *it = (struct osd_it_iam *)di;
2953 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2959 * \param di osd iterator
2962 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2964 struct osd_it_iam *it = (struct osd_it_iam *)di;
2966 iam_it_put(&it->oi_it);
2970 * Move iterator by one record
2972 * \param di osd iterator
2974 * \retval +1 end of container reached
2976 * \retval -ve failure
2979 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2981 struct osd_it_iam *it = (struct osd_it_iam *)di;
2983 return iam_it_next(&it->oi_it);
2987 * Return pointer to the key under iterator.
2990 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2991 const struct dt_it *di)
2993 struct osd_it_iam *it = (struct osd_it_iam *)di;
2995 return (struct dt_key *)iam_it_key_get(&it->oi_it);
2999 * Return size of key under iterator (in bytes)
3002 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3004 struct osd_it_iam *it = (struct osd_it_iam *)di;
3006 return iam_it_key_size(&it->oi_it);
3010 * Return pointer to the record under iterator.
3012 static struct dt_rec *osd_it_iam_rec(const struct lu_env *env,
3013 const struct dt_it *di)
3015 struct osd_it_iam *it = (struct osd_it_iam *)di;
3017 return (struct dt_rec *)iam_it_rec_get(&it->oi_it);
3021 * Returns cookie for current Iterator position.
3023 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3025 struct osd_it_iam *it = (struct osd_it_iam *)di;
3027 return iam_it_store(&it->oi_it);
3031 * Restore iterator from cookie.
3033 * \param di osd iterator
3034 * \param hash Iterator location cookie
3036 * \retval +ve di points to record with least key not larger than key.
3037 * \retval 0 di points to exact matched key
3038 * \retval -ve failure
3041 static int osd_it_iam_load(const struct lu_env *env,
3042 const struct dt_it *di, __u64 hash)
3044 struct osd_it_iam *it = (struct osd_it_iam *)di;
3046 return iam_it_load(&it->oi_it, hash);
3049 static const struct dt_index_operations osd_index_iam_ops = {
3050 .dio_lookup = osd_index_iam_lookup,
3051 .dio_insert = osd_index_iam_insert,
3052 .dio_delete = osd_index_iam_delete,
3054 .init = osd_it_iam_init,
3055 .fini = osd_it_iam_fini,
3056 .get = osd_it_iam_get,
3057 .put = osd_it_iam_put,
3058 .next = osd_it_iam_next,
3059 .key = osd_it_iam_key,
3060 .key_size = osd_it_iam_key_size,
3061 .rec = osd_it_iam_rec,
3062 .store = osd_it_iam_store,
3063 .load = osd_it_iam_load
3068 * Creates or initializes iterator context.
3070 * \retval struct osd_it_ea, iterator structure on success
3073 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3074 struct dt_object *dt,
3075 struct lustre_capa *capa)
3077 struct osd_object *obj = osd_dt_obj(dt);
3078 struct osd_thread_info *info = osd_oti_get(env);
3079 struct osd_it_ea *it = &info->oti_it_ea;
3080 struct lu_object *lo = &dt->do_lu;
3081 struct dentry *obj_dentry = &info->oti_it_dentry;
3083 LASSERT(lu_object_exists(lo));
3085 obj_dentry->d_inode = obj->oo_inode;
3086 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3087 obj_dentry->d_name.hash = 0;
3089 it->oie_rd_dirent = 0;
3090 it->oie_it_dirent = 0;
3091 it->oie_curr_pos = 0;
3092 it->oie_next_pos = 0;
3093 it->oie_dirent = NULL;
3094 it->oie_buf = info->oti_it_ea_buf;
3096 it->oie_file.f_dentry = obj_dentry;
3097 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3098 it->oie_file.f_op = obj->oo_inode->i_fop;
3099 it->oie_file.private_data = NULL;
3101 RETURN((struct dt_it*) it);
3105 * Destroy or finishes iterator context.
3107 * \param di, struct osd_it_ea, iterator structure to be destroyed
3109 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3111 struct osd_it_ea *it = (struct osd_it_ea *)di;
3112 struct osd_object *obj = it->oie_obj;
3113 struct inode *inode = obj->oo_inode;
3116 it->oie_file.f_op->release(inode, &it->oie_file);
3117 lu_object_put(env, &obj->oo_dt.do_lu);
3122 * It position the iterator at given key, so that next lookup continues from
3123 * that key Or it is similar to dio_it->load() but based on a key,
3124 * rather than file position.
3126 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3129 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3131 static int osd_it_ea_get(const struct lu_env *env,
3132 struct dt_it *di, const struct dt_key *key)
3134 struct osd_it_ea *it = (struct osd_it_ea *)di;
3137 LASSERT(((const char *)key)[0] == '\0');
3138 it->oie_curr_pos = 0;
3139 it->oie_next_pos = 0;
3140 it->oie_rd_dirent = 0;
3141 it->oie_it_dirent = 0;
3142 it->oie_dirent = NULL;
3150 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3155 * It is called internally by ->readdir(). It fills the
3156 * iterator's in-memory data structure with required
3157 * information i.e. name, namelen, rec_size etc.
3159 * \param buf, in which information to be filled in.
3160 * \param name, name of the file in given dir
3162 * \retval 0, on success
3163 * \retval 1, on buffer full
3165 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3166 loff_t offset, ino_t ino,
3167 unsigned int d_type)
3169 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3170 struct osd_it_ea_dirent *ent = it->oie_dirent;
3173 /* this should never happen */
3174 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3175 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3179 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3183 ent->oied_ino = ino;
3184 ent->oied_off = offset;
3185 ent->oied_namelen = namelen;
3186 memcpy(ent->oied_name, name, namelen);
3188 it->oie_rd_dirent++;
3189 it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3194 * Calls ->readdir() to load a directory entry at a time
3195 * and stored it in iterator's in-memory data structure.
3197 * \param di, struct osd_it_ea, iterator's in memory structure
3199 * \retval 0, on success
3200 * \retval -ve, on error
3202 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3204 struct osd_it_ea *it = (struct osd_it_ea *)di;
3205 struct osd_object *obj = it->oie_obj;
3206 struct inode *inode = obj->oo_inode;
3210 it->oie_dirent = it->oie_buf;
3211 it->oie_rd_dirent = 0;
3212 it->oie_file.f_pos = it->oie_curr_pos;
3214 down_read(&obj->oo_ext_idx_sem);
3215 result = inode->i_fop->readdir(&it->oie_file, it,
3216 (filldir_t) osd_ldiskfs_filldir);
3218 up_read(&obj->oo_ext_idx_sem);
3219 it->oie_next_pos = it->oie_file.f_pos;
3221 if (it->oie_rd_dirent == 0) {
3224 it->oie_dirent = it->oie_buf;
3225 it->oie_it_dirent = 1;
3232 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3233 * to load a directory entry at a time and stored it in
3234 * iterator's in-memory data structure.
3236 * \param di, struct osd_it_ea, iterator's in memory structure
3238 * \retval +ve, iterator reached to end
3239 * \retval 0, iterator not reached to end
3240 * \retval -ve, on error
3242 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3244 struct osd_it_ea *it = (struct osd_it_ea *)di;
3249 if (it->oie_it_dirent < it->oie_rd_dirent) {
3250 it->oie_dirent = (void *) it->oie_dirent +
3251 size_round(sizeof(struct osd_it_ea_dirent) +
3252 it->oie_dirent->oied_namelen);
3253 it->oie_it_dirent++;
3256 it->oie_curr_pos = it->oie_next_pos;
3258 if (it->oie_curr_pos == LDISKFS_HTREE_EOF)
3261 rc = osd_ldiskfs_it_fill(di);
3268 * Returns the key at current position from iterator's in memory structure.
3270 * \param di, struct osd_it_ea, iterator's in memory structure
3272 * \retval key i.e. struct dt_key on success
3274 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3275 const struct dt_it *di)
3277 struct osd_it_ea *it = (struct osd_it_ea *)di;
3279 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3283 * Returns the key's size at current position from iterator's in memory structure.
3285 * \param di, struct osd_it_ea, iterator's in memory structure
3287 * \retval key_size i.e. struct dt_key on success
3289 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3291 struct osd_it_ea *it = (struct osd_it_ea *)di;
3293 RETURN(it->oie_dirent->oied_namelen);
3297 * Returns the value (i.e. fid/igif) at current position from iterator's
3298 * in memory structure.
3300 * \param di, struct osd_it_ea, iterator's in memory structure
3302 * \retval value i.e. struct dt_rec on success
3304 static struct dt_rec *osd_it_ea_rec(const struct lu_env *env,
3305 const struct dt_it *di)
3307 struct osd_it_ea *it = (struct osd_it_ea *)di;
3308 struct osd_object *obj = it->oie_obj;
3309 struct osd_thread_info *info = osd_oti_get(env);
3310 struct osd_inode_id *id = &info->oti_id;
3311 struct lu_fid_pack *rec = &info->oti_pack;
3312 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3313 struct dentry *dentry = &info->oti_child_dentry;
3314 struct osd_device *dev;
3315 struct inode *inode;
3319 dev = osd_dev(ldev);
3320 id->oii_ino = it->oie_dirent->oied_ino;
3321 id->oii_gen = OSD_OII_NOGEN;
3322 inode = osd_iget(info, dev, id);
3323 if (!IS_ERR(inode)) {
3324 dentry->d_inode = inode;
3325 LASSERT(dentry->d_inode->i_sb == osd_sb(dev));
3327 RETURN((struct dt_rec *) PTR_ERR(inode));
3330 rc = osd_ea_fid_get(env, dentry, (struct dt_rec*) rec);
3335 RETURN((struct dt_rec *)rec);
3340 * Returns a cookie for current position of the iterator head, so that
3341 * user can use this cookie to load/start the iterator next time.
3343 * \param di, struct osd_it_ea, iterator's in memory structure
3345 * \retval cookie for current position, on success
3347 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3349 struct osd_it_ea *it = (struct osd_it_ea *)di;
3351 RETURN(it->oie_dirent->oied_off);
3355 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3356 * to load a directory entry at a time and stored it i inn,
3357 * in iterator's in-memory data structure.
3359 * \param di, struct osd_it_ea, iterator's in memory structure
3361 * \retval +ve, on success
3362 * \retval -ve, on error
3364 static int osd_it_ea_load(const struct lu_env *env,
3365 const struct dt_it *di, __u64 hash)
3367 struct osd_it_ea *it = (struct osd_it_ea *)di;
3371 it->oie_curr_pos = hash;
3373 rc = osd_ldiskfs_it_fill(di);
3380 * Index and Iterator operations for interoperability
3381 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3383 static const struct dt_index_operations osd_index_ea_ops = {
3384 .dio_lookup = osd_index_ea_lookup,
3385 .dio_insert = osd_index_ea_insert,
3386 .dio_delete = osd_index_ea_delete,
3388 .init = osd_it_ea_init,
3389 .fini = osd_it_ea_fini,
3390 .get = osd_it_ea_get,
3391 .put = osd_it_ea_put,
3392 .next = osd_it_ea_next,
3393 .key = osd_it_ea_key,
3394 .key_size = osd_it_ea_key_size,
3395 .rec = osd_it_ea_rec,
3396 .store = osd_it_ea_store,
3397 .load = osd_it_ea_load
3402 * Index lookup function for interoperability mode (b11826).
3404 * \param key, key i.e. file name to be searched
3406 * \retval +ve, on success
3407 * \retval -ve, on error
3409 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3410 struct dt_rec *rec, const struct dt_key *key,
3411 struct lustre_capa *capa)
3413 struct osd_object *obj = osd_dt_obj(dt);
3418 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3419 LINVRNT(osd_invariant(obj));
3421 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3424 rc = osd_ea_lookup_rec(env, obj, rec, key);
3431 /* type constructor/destructor: osd_type_init, osd_type_fini */
3432 LU_TYPE_INIT_FINI(osd, &osd_key);
3434 static struct lu_context_key osd_key = {
3435 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3436 .lct_init = osd_key_init,
3437 .lct_fini = osd_key_fini,
3438 .lct_exit = osd_key_exit
3441 static void *osd_key_init(const struct lu_context *ctx,
3442 struct lu_context_key *key)
3444 struct osd_thread_info *info;
3446 OBD_ALLOC_PTR(info);
3448 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3449 if (info->oti_it_ea_buf != NULL) {
3450 info->oti_env = container_of(ctx, struct lu_env,
3454 info = ERR_PTR(-ENOMEM);
3457 info = ERR_PTR(-ENOMEM);
3462 static void osd_key_fini(const struct lu_context *ctx,
3463 struct lu_context_key *key, void* data)
3465 struct osd_thread_info *info = data;
3467 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3471 static void osd_key_exit(const struct lu_context *ctx,
3472 struct lu_context_key *key, void *data)
3474 struct osd_thread_info *info = data;
3476 LASSERT(info->oti_r_locks == 0);
3477 LASSERT(info->oti_w_locks == 0);
3478 LASSERT(info->oti_txns == 0);
3481 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3482 const char *name, struct lu_device *next)
3485 struct lu_context *ctx;
3487 /* context for commit hooks */
3488 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3489 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3491 rc = osd_procfs_init(osd_dev(d), name);
3492 ctx->lc_cookie = 0x3;
3497 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3499 struct osd_thread_info *info = osd_oti_get(env);
3501 if (o->od_obj_area != NULL) {
3502 lu_object_put(env, &o->od_obj_area->do_lu);
3503 o->od_obj_area = NULL;
3505 osd_oi_fini(info, &o->od_oi);
3510 static int osd_mount(const struct lu_env *env,
3511 struct osd_device *o, struct lustre_cfg *cfg)
3513 struct lustre_mount_info *lmi;
3514 const char *dev = lustre_cfg_string(cfg, 0);
3515 struct lustre_disk_data *ldd;
3516 struct lustre_sb_info *lsi;
3520 if (o->od_mount != NULL) {
3521 CERROR("Already mounted (%s)\n", dev);
3526 lmi = server_get_mount(dev);
3528 CERROR("Cannot get mount info for %s!\n", dev);
3532 LASSERT(lmi != NULL);
3533 /* save lustre_mount_info in dt_device */
3536 lsi = s2lsi(lmi->lmi_sb);
3539 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3541 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3545 o->od_obj_area = NULL;
3549 static struct lu_device *osd_device_fini(const struct lu_env *env,
3550 struct lu_device *d)
3555 shrink_dcache_sb(osd_sb(osd_dev(d)));
3556 osd_sync(env, lu2dt_dev(d));
3558 rc = osd_procfs_fini(osd_dev(d));
3560 CERROR("proc fini error %d \n", rc);
3561 RETURN (ERR_PTR(rc));
3564 if (osd_dev(d)->od_mount)
3565 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3566 osd_dev(d)->od_mount->lmi_mnt);
3567 osd_dev(d)->od_mount = NULL;
3569 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3573 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3574 struct lu_device_type *t,
3575 struct lustre_cfg *cfg)
3577 struct lu_device *l;
3578 struct osd_device *o;
3584 result = dt_device_init(&o->od_dt_dev, t);
3587 l->ld_ops = &osd_lu_ops;
3588 o->od_dt_dev.dd_ops = &osd_dt_ops;
3589 spin_lock_init(&o->od_osfs_lock);
3590 o->od_osfs_age = cfs_time_shift_64(-1000);
3591 o->od_capa_hash = init_capa_hash();
3592 if (o->od_capa_hash == NULL) {
3593 dt_device_fini(&o->od_dt_dev);
3594 l = ERR_PTR(-ENOMEM);
3597 l = ERR_PTR(result);
3602 l = ERR_PTR(-ENOMEM);
3606 static struct lu_device *osd_device_free(const struct lu_env *env,
3607 struct lu_device *d)
3609 struct osd_device *o = osd_dev(d);
3612 cleanup_capa_hash(o->od_capa_hash);
3613 dt_device_fini(&o->od_dt_dev);
3618 static int osd_process_config(const struct lu_env *env,
3619 struct lu_device *d, struct lustre_cfg *cfg)
3621 struct osd_device *o = osd_dev(d);
3625 switch(cfg->lcfg_command) {
3627 err = osd_mount(env, o, cfg);
3630 err = osd_shutdown(env, o);
3639 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
3640 struct ldiskfs_super_block * es);
3642 static int osd_recovery_complete(const struct lu_env *env,
3643 struct lu_device *d)
3645 struct osd_device *o = osd_dev(d);
3647 /* TODO: orphans handling */
3648 ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3652 static int osd_prepare(const struct lu_env *env,
3653 struct lu_device *pdev,
3654 struct lu_device *dev)
3656 struct osd_device *osd = osd_dev(dev);
3657 struct lustre_sb_info *lsi;
3658 struct lustre_disk_data *ldd;
3659 struct lustre_mount_info *lmi;
3660 struct osd_thread_info *oti = osd_oti_get(env);
3661 struct dt_object *d;
3665 /* 1. initialize oi before any file create or file open */
3666 result = osd_oi_init(oti, &osd->od_oi,
3667 &osd->od_dt_dev, lu2md_dev(pdev));
3671 lmi = osd->od_mount;
3672 lsi = s2lsi(lmi->lmi_sb);
3675 /* 2. setup local objects */
3676 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3680 /* 3. open remote object dir */
3681 d = dt_store_open(env, lu2dt_dev(dev), "",
3682 remote_obj_dir, &oti->oti_fid);
3684 osd->od_obj_area = d;
3687 result = PTR_ERR(d);
3688 osd->od_obj_area = NULL;
3695 static struct inode *osd_iget(struct osd_thread_info *info,
3696 struct osd_device *dev,
3697 const struct osd_inode_id *id)
3699 struct inode *inode;
3701 inode = iget(osd_sb(dev), id->oii_ino);
3702 if (inode == NULL) {
3703 CERROR("no inode\n");
3704 inode = ERR_PTR(-EACCES);
3705 } else if (id->oii_gen != OSD_OII_NOGEN &&
3706 inode->i_generation != id->oii_gen) {
3708 inode = ERR_PTR(-ESTALE);
3709 } else if (inode->i_nlink == 0) {
3710 /* due to parallel readdir and unlink,
3711 * we can have dead inode here. */
3712 CWARN("stale inode\n");
3713 make_bad_inode(inode);
3715 inode = ERR_PTR(-ESTALE);
3716 } else if (is_bad_inode(inode)) {
3717 CERROR("bad inode %lx\n",inode->i_ino);
3719 inode = ERR_PTR(-ENOENT);
3725 static int osd_fid_lookup(const struct lu_env *env,
3726 struct osd_object *obj, const struct lu_fid *fid)
3728 struct osd_thread_info *info;
3729 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3730 struct osd_device *dev;
3731 struct osd_inode_id *id;
3733 struct inode *inode;
3736 LINVRNT(osd_invariant(obj));
3737 LASSERT(obj->oo_inode == NULL);
3738 LASSERT(fid_is_sane(fid));
3740 * This assertion checks that osd layer sees only local
3741 * fids. Unfortunately it is somewhat expensive (does a
3742 * cache-lookup). Disabling it for production/acceptance-testing.
3744 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
3748 info = osd_oti_get(env);
3749 dev = osd_dev(ldev);
3753 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
3756 result = osd_oi_lookup(info, oi, fid, id);
3758 inode = osd_iget(info, dev, id);
3759 if (!IS_ERR(inode)) {
3760 obj->oo_inode = inode;
3761 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
3762 if (dev->od_iop_mode) {
3763 obj->oo_compat_dot_created = 1;
3764 obj->oo_compat_dotdot_created = 1;
3769 * If fid wasn't found in oi, inode-less object is
3770 * created, for which lu_object_exists() returns
3771 * false. This is used in a (frequent) case when
3772 * objects are created as locking anchors or
3773 * place holders for objects yet to be created.
3775 result = PTR_ERR(inode);
3776 } else if (result == -ENOENT)
3778 LINVRNT(osd_invariant(obj));
3783 static void osd_inode_getattr(const struct lu_env *env,
3784 struct inode *inode, struct lu_attr *attr)
3786 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
3787 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
3788 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
3790 attr->la_atime = LTIME_S(inode->i_atime);
3791 attr->la_mtime = LTIME_S(inode->i_mtime);
3792 attr->la_ctime = LTIME_S(inode->i_ctime);
3793 attr->la_mode = inode->i_mode;
3794 attr->la_size = i_size_read(inode);
3795 attr->la_blocks = inode->i_blocks;
3796 attr->la_uid = inode->i_uid;
3797 attr->la_gid = inode->i_gid;
3798 attr->la_flags = LDISKFS_I(inode)->i_flags;
3799 attr->la_nlink = inode->i_nlink;
3800 attr->la_rdev = inode->i_rdev;
3801 attr->la_blksize = ll_inode_blksize(inode);
3802 attr->la_blkbits = inode->i_blkbits;
3809 static int lu_device_is_osd(const struct lu_device *d)
3811 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
3814 static struct osd_object *osd_obj(const struct lu_object *o)
3816 LASSERT(lu_device_is_osd(o->lo_dev));
3817 return container_of0(o, struct osd_object, oo_dt.do_lu);
3820 static struct osd_device *osd_dt_dev(const struct dt_device *d)
3822 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
3823 return container_of0(d, struct osd_device, od_dt_dev);
3826 static struct osd_device *osd_dev(const struct lu_device *d)
3828 LASSERT(lu_device_is_osd(d));
3829 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
3832 static struct osd_object *osd_dt_obj(const struct dt_object *d)
3834 return osd_obj(&d->do_lu);
3837 static struct osd_device *osd_obj2dev(const struct osd_object *o)
3839 return osd_dev(o->oo_dt.do_lu.lo_dev);
3842 static struct lu_device *osd2lu_dev(struct osd_device *osd)
3844 return &osd->od_dt_dev.dd_lu_dev;
3847 static struct super_block *osd_sb(const struct osd_device *dev)
3849 return dev->od_mount->lmi_mnt->mnt_sb;
3852 static journal_t *osd_journal(const struct osd_device *dev)
3854 return LDISKFS_SB(osd_sb(dev))->s_journal;
3857 static int osd_has_index(const struct osd_object *obj)
3859 return obj->oo_dt.do_index_ops != NULL;
3862 static int osd_object_invariant(const struct lu_object *l)
3864 return osd_invariant(osd_obj(l));
3867 static const struct lu_object_operations osd_lu_obj_ops = {
3868 .loo_object_init = osd_object_init,
3869 .loo_object_delete = osd_object_delete,
3870 .loo_object_release = osd_object_release,
3871 .loo_object_free = osd_object_free,
3872 .loo_object_print = osd_object_print,
3873 .loo_object_invariant = osd_object_invariant
3876 static const struct lu_device_operations osd_lu_ops = {
3877 .ldo_object_alloc = osd_object_alloc,
3878 .ldo_process_config = osd_process_config,
3879 .ldo_recovery_complete = osd_recovery_complete,
3880 .ldo_prepare = osd_prepare,
3883 static const struct lu_device_type_operations osd_device_type_ops = {
3884 .ldto_init = osd_type_init,
3885 .ldto_fini = osd_type_fini,
3887 .ldto_start = osd_type_start,
3888 .ldto_stop = osd_type_stop,
3890 .ldto_device_alloc = osd_device_alloc,
3891 .ldto_device_free = osd_device_free,
3893 .ldto_device_init = osd_device_init,
3894 .ldto_device_fini = osd_device_fini
3897 static struct lu_device_type osd_device_type = {
3898 .ldt_tags = LU_DEVICE_DT,
3899 .ldt_name = LUSTRE_OSD_NAME,
3900 .ldt_ops = &osd_device_type_ops,
3901 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3905 * lprocfs legacy support.
3907 static struct obd_ops osd_obd_device_ops = {
3908 .o_owner = THIS_MODULE
3911 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3912 .llod_name = remote_obj_dir,
3913 .llod_oid = OSD_REM_OBJ_DIR_OID,
3915 .llod_feat = &dt_directory_features,
3918 static int __init osd_mod_init(void)
3920 struct lprocfs_static_vars lvars;
3923 llo_local_obj_register(&llod_osd_rem_obj_dir);
3924 lprocfs_osd_init_vars(&lvars);
3925 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3926 LUSTRE_OSD_NAME, &osd_device_type);
3929 static void __exit osd_mod_exit(void)
3931 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3932 class_unregister_type(LUSTRE_OSD_NAME);
3935 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3936 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3937 MODULE_LICENSE("GPL");
3939 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);