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;
529 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
530 OSD_TXN_INODE_DELETE_CREDITS);
531 th = osd_trans_start(env, &osd->od_dt_dev, prm);
533 result = osd_oi_delete(oti, &osd->od_oi, fid, th);
534 osd_trans_stop(env, th);
536 result = PTR_ERR(th);
541 * Called just before object is freed. Releases all resources except for
542 * object itself (that is released by osd_object_free()).
544 * Concurrency: no concurrent access is possible that late in object
547 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
549 struct osd_object *obj = osd_obj(l);
550 struct inode *inode = obj->oo_inode;
552 LINVRNT(osd_invariant(obj));
555 * If object is unlinked remove fid->ino mapping from object index.
562 if (osd_inode_unlinked(inode)) {
563 result = osd_inode_remove(env, obj);
565 LU_OBJECT_DEBUG(D_ERROR, env, l,
566 "Failed to cleanup: %d\n",
571 obj->oo_inode = NULL;
576 * Concurrency: ->loo_object_release() is called under site spin-lock.
578 static void osd_object_release(const struct lu_env *env,
581 struct osd_object *o = osd_obj(l);
583 LASSERT(!lu_object_is_dying(l->lo_header));
584 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
585 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
589 * Concurrency: shouldn't matter.
591 static int osd_object_print(const struct lu_env *env, void *cookie,
592 lu_printer_t p, const struct lu_object *l)
594 struct osd_object *o = osd_obj(l);
597 if (o->oo_dir != NULL)
598 d = o->oo_dir->od_container.ic_descr;
601 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
603 o->oo_inode ? o->oo_inode->i_ino : 0UL,
604 o->oo_inode ? o->oo_inode->i_generation : 0,
605 d ? d->id_ops->id_name : "plain");
609 * Concurrency: shouldn't matter.
611 int osd_statfs(const struct lu_env *env, struct dt_device *d,
614 struct osd_device *osd = osd_dt_dev(d);
615 struct super_block *sb = osd_sb(osd);
618 spin_lock(&osd->od_osfs_lock);
620 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
621 result = ll_do_statfs(sb, &osd->od_kstatfs);
622 if (likely(result == 0)) /* N.B. statfs can't really fail */
623 osd->od_osfs_age = cfs_time_current_64();
626 if (likely(result == 0))
627 *sfs = osd->od_kstatfs;
628 spin_unlock(&osd->od_osfs_lock);
634 * Concurrency: doesn't access mutable data.
636 static void osd_conf_get(const struct lu_env *env,
637 const struct dt_device *dev,
638 struct dt_device_param *param)
641 * XXX should be taken from not-yet-existing fs abstraction layer.
643 param->ddp_max_name_len = LDISKFS_NAME_LEN;
644 param->ddp_max_nlink = LDISKFS_LINK_MAX;
645 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
649 * Helper function to get and fill the buffer with input values.
651 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
655 buf = &osd_oti_get(env)->oti_buf;
666 * Concurrency: doesn't access mutable data.
668 static int osd_param_is_sane(const struct osd_device *dev,
669 const struct txn_param *param)
671 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
675 * Concurrency: shouldn't matter.
677 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
679 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
680 struct thandle *th = &oh->ot_super;
681 struct dt_device *dev = th->th_dev;
682 struct lu_device *lud = &dev->dd_lu_dev;
684 LASSERT(dev != NULL);
685 LASSERT(oh->ot_handle == NULL);
688 CERROR("transaction @0x%p commit error: %d\n", th, error);
690 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
692 * This od_env_for_commit is only for commit usage. see
695 lu_context_enter(&env->le_ctx);
696 dt_txn_hook_commit(env, th);
697 lu_context_exit(&env->le_ctx);
700 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
704 lu_context_exit(&th->th_ctx);
705 lu_context_fini(&th->th_ctx);
710 * Concurrency: shouldn't matter.
712 static struct thandle *osd_trans_start(const struct lu_env *env,
716 struct osd_device *dev = osd_dt_dev(d);
718 struct osd_thandle *oh;
724 hook_res = dt_txn_hook_start(env, d, p);
726 RETURN(ERR_PTR(hook_res));
728 if (osd_param_is_sane(dev, p)) {
729 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
731 struct osd_thread_info *oti = osd_oti_get(env);
734 * XXX temporary stuff. Some abstraction layer should
738 jh = journal_start(osd_journal(dev), p->tp_credits);
744 jh->h_sync = p->tp_sync;
745 lu_device_get(&d->dd_lu_dev);
746 oh->ot_dev_link = lu_ref_add
747 (&d->dd_lu_dev.ld_reference,
749 /* add commit callback */
750 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
751 lu_context_enter(&th->th_ctx);
752 journal_callback_set(jh, osd_trans_commit_cb,
753 (struct journal_callback *)&oh->ot_jcb);
754 LASSERT(oti->oti_txns == 0);
755 LASSERT(oti->oti_r_locks == 0);
756 LASSERT(oti->oti_w_locks == 0);
763 th = ERR_PTR(-ENOMEM);
765 CERROR("Invalid transaction parameters\n");
766 th = ERR_PTR(-EINVAL);
773 * Concurrency: shouldn't matter.
775 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
778 struct osd_thandle *oh;
779 struct osd_thread_info *oti = osd_oti_get(env);
783 oh = container_of0(th, struct osd_thandle, ot_super);
784 if (oh->ot_handle != NULL) {
785 handle_t *hdl = oh->ot_handle;
787 LASSERT(oti->oti_txns == 1);
789 LASSERT(oti->oti_r_locks == 0);
790 LASSERT(oti->oti_w_locks == 0);
791 result = dt_txn_hook_stop(env, th);
793 CERROR("Failure in transaction hook: %d\n", result);
794 oh->ot_handle = NULL;
795 result = journal_stop(hdl);
797 CERROR("Failure to stop transaction: %d\n", result);
803 * Concurrency: shouldn't matter.
805 static int osd_sync(const struct lu_env *env, struct dt_device *d)
807 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
808 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
812 * Start commit for OSD device.
814 * An implementation of dt_commit_async method for OSD device.
815 * Asychronously starts underlayng fs sync and thereby a transaction
818 * \param env environment
821 * \see dt_device_operations
823 static int osd_commit_async(const struct lu_env *env,
826 struct super_block *s = osd_sb(osd_dt_dev(d));
829 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
830 RETURN(s->s_op->sync_fs(s, 0));
834 * Concurrency: shouldn't matter.
836 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
838 static void osd_ro(const struct lu_env *env, struct dt_device *d)
842 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
844 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
845 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
851 * Concurrency: serialization provided by callers.
853 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
854 int mode, unsigned long timeout, __u32 alg,
855 struct lustre_capa_key *keys)
857 struct osd_device *dev = osd_dt_dev(d);
860 dev->od_fl_capa = mode;
861 dev->od_capa_timeout = timeout;
862 dev->od_capa_alg = alg;
863 dev->od_capa_keys = keys;
868 * Concurrency: serialization provided by callers.
870 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
871 struct dt_quota_ctxt *ctxt, void *data)
873 struct obd_device *obd = (void *)ctxt;
874 struct vfsmount *mnt = (struct vfsmount *)data;
877 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
878 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
879 obd->obd_lvfs_ctxt.pwdmnt = mnt;
880 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
881 obd->obd_lvfs_ctxt.fs = get_ds();
887 * Note: we do not count into QUOTA here.
888 * If we mount with --data_journal we may need more.
890 static const int osd_dto_credits_noquota[DTO_NR] = {
893 * INDEX_EXTRA_TRANS_BLOCKS(8) +
894 * SINGLEDATA_TRANS_BLOCKS(8)
895 * XXX Note: maybe iam need more, since iam have more level than
898 [DTO_INDEX_INSERT] = 16,
899 [DTO_INDEX_DELETE] = 16,
903 [DTO_IDNEX_UPDATE] = 16,
905 * Create a object. The same as create object in EXT3.
906 * DATA_TRANS_BLOCKS(14) +
907 * INDEX_EXTRA_BLOCKS(8) +
908 * 3(inode bits, groups, GDT)
910 [DTO_OBJECT_CREATE] = 25,
914 [DTO_OBJECT_DELETE] = 25,
917 * 3(inode bits, group, GDT)
919 [DTO_ATTR_SET_BASE] = 3,
921 * Xattr set. The same as xattr of EXT3.
922 * DATA_TRANS_BLOCKS(14)
923 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
924 * also counted in. Do not know why?
926 [DTO_XATTR_SET] = 14,
929 * creadits for inode change during write.
931 [DTO_WRITE_BASE] = 3,
933 * credits for single block write.
935 [DTO_WRITE_BLOCK] = 14,
937 * Attr set credits for chown.
938 * 3 (inode bit, group, GDT)
940 [DTO_ATTR_SET_CHOWN]= 3
944 * Note: we count into QUOTA here.
945 * If we mount with --data_journal we may need more.
947 static const int osd_dto_credits_quota[DTO_NR] = {
949 * INDEX_EXTRA_TRANS_BLOCKS(8) +
950 * SINGLEDATA_TRANS_BLOCKS(8) +
951 * 2 * QUOTA_TRANS_BLOCKS(2)
953 [DTO_INDEX_INSERT] = 20,
955 * INDEX_EXTRA_TRANS_BLOCKS(8) +
956 * SINGLEDATA_TRANS_BLOCKS(8) +
957 * 2 * QUOTA_TRANS_BLOCKS(2)
959 [DTO_INDEX_DELETE] = 20,
963 [DTO_IDNEX_UPDATE] = 16,
965 * Create a object. Same as create object in EXT3 filesystem.
966 * DATA_TRANS_BLOCKS(16) +
967 * INDEX_EXTRA_BLOCKS(8) +
968 * 3(inode bits, groups, GDT) +
969 * 2 * QUOTA_INIT_BLOCKS(25)
971 [DTO_OBJECT_CREATE] = 77,
974 * DATA_TRANS_BLOCKS(16) +
975 * INDEX_EXTRA_BLOCKS(8) +
976 * 3(inode bits, groups, GDT) +
979 [DTO_OBJECT_DELETE] = 27,
982 * 3 (inode bit, group, GDT) +
984 [DTO_ATTR_SET_BASE] = 3,
986 * Xattr set. The same as xattr of EXT3.
987 * DATA_TRANS_BLOCKS(16)
988 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
989 * also counted in. Do not know why?
991 [DTO_XATTR_SET] = 16,
994 * creadits for inode change during write.
996 [DTO_WRITE_BASE] = 3,
998 * credits for single block write.
1000 [DTO_WRITE_BLOCK] = 16,
1002 * Attr set credits for chown.
1003 * 3 (inode bit, group, GDT) +
1004 * 2 * QUOTA_INIT_BLOCKS(25) +
1005 * 2 * QUOTA_DEL_BLOCKS(9)
1007 [DTO_ATTR_SET_CHOWN]= 71
1010 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1013 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1014 ARRAY_SIZE(osd_dto_credits_quota));
1015 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1016 #ifdef HAVE_QUOTA_SUPPORT
1017 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1018 return osd_dto_credits_quota[op];
1021 return osd_dto_credits_noquota[op];
1024 static const struct dt_device_operations osd_dt_ops = {
1025 .dt_root_get = osd_root_get,
1026 .dt_statfs = osd_statfs,
1027 .dt_trans_start = osd_trans_start,
1028 .dt_trans_stop = osd_trans_stop,
1029 .dt_conf_get = osd_conf_get,
1030 .dt_sync = osd_sync,
1032 .dt_commit_async = osd_commit_async,
1033 .dt_credit_get = osd_credit_get,
1034 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1035 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1038 static void osd_object_read_lock(const struct lu_env *env,
1039 struct dt_object *dt, unsigned role)
1041 struct osd_object *obj = osd_dt_obj(dt);
1042 struct osd_thread_info *oti = osd_oti_get(env);
1044 LINVRNT(osd_invariant(obj));
1046 LASSERT(obj->oo_owner != env);
1047 down_read_nested(&obj->oo_sem, role);
1049 LASSERT(obj->oo_owner == NULL);
1053 static void osd_object_write_lock(const struct lu_env *env,
1054 struct dt_object *dt, unsigned role)
1056 struct osd_object *obj = osd_dt_obj(dt);
1057 struct osd_thread_info *oti = osd_oti_get(env);
1059 LINVRNT(osd_invariant(obj));
1061 LASSERT(obj->oo_owner != env);
1062 down_write_nested(&obj->oo_sem, role);
1064 LASSERT(obj->oo_owner == NULL);
1065 obj->oo_owner = env;
1069 static void osd_object_read_unlock(const struct lu_env *env,
1070 struct dt_object *dt)
1072 struct osd_object *obj = osd_dt_obj(dt);
1073 struct osd_thread_info *oti = osd_oti_get(env);
1075 LINVRNT(osd_invariant(obj));
1077 LASSERT(oti->oti_r_locks > 0);
1079 up_read(&obj->oo_sem);
1082 static void osd_object_write_unlock(const struct lu_env *env,
1083 struct dt_object *dt)
1085 struct osd_object *obj = osd_dt_obj(dt);
1086 struct osd_thread_info *oti = osd_oti_get(env);
1088 LINVRNT(osd_invariant(obj));
1090 LASSERT(obj->oo_owner == env);
1091 LASSERT(oti->oti_w_locks > 0);
1093 obj->oo_owner = NULL;
1094 up_write(&obj->oo_sem);
1097 static int capa_is_sane(const struct lu_env *env,
1098 struct osd_device *dev,
1099 struct lustre_capa *capa,
1100 struct lustre_capa_key *keys)
1102 struct osd_thread_info *oti = osd_oti_get(env);
1103 struct lustre_capa *tcapa = &oti->oti_capa;
1104 struct obd_capa *oc;
1108 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1110 if (capa_is_expired(oc)) {
1111 DEBUG_CAPA(D_ERROR, capa, "expired");
1118 if (capa_is_expired_sec(capa)) {
1119 DEBUG_CAPA(D_ERROR, capa, "expired");
1123 spin_lock(&capa_lock);
1124 for (i = 0; i < 2; i++) {
1125 if (keys[i].lk_keyid == capa->lc_keyid) {
1126 oti->oti_capa_key = keys[i];
1130 spin_unlock(&capa_lock);
1133 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1137 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1141 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1142 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1146 oc = capa_add(dev->od_capa_hash, capa);
1152 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1153 struct lustre_capa *capa, __u64 opc)
1155 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1156 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1157 struct md_capainfo *ci;
1160 if (!dev->od_fl_capa)
1163 if (capa == BYPASS_CAPA)
1166 ci = md_capainfo(env);
1170 if (ci->mc_auth == LC_ID_NONE)
1174 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1178 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1179 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1184 if (!capa_opc_supported(capa, opc)) {
1185 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1189 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1190 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1197 static int osd_attr_get(const struct lu_env *env,
1198 struct dt_object *dt,
1199 struct lu_attr *attr,
1200 struct lustre_capa *capa)
1202 struct osd_object *obj = osd_dt_obj(dt);
1204 LASSERT(dt_object_exists(dt));
1205 LINVRNT(osd_invariant(obj));
1207 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1210 spin_lock(&obj->oo_guard);
1211 osd_inode_getattr(env, obj->oo_inode, attr);
1212 spin_unlock(&obj->oo_guard);
1216 static int osd_attr_set(const struct lu_env *env,
1217 struct dt_object *dt,
1218 const struct lu_attr *attr,
1219 struct thandle *handle,
1220 struct lustre_capa *capa)
1222 struct osd_object *obj = osd_dt_obj(dt);
1225 LASSERT(handle != NULL);
1226 LASSERT(dt_object_exists(dt));
1227 LASSERT(osd_invariant(obj));
1229 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1232 spin_lock(&obj->oo_guard);
1233 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1234 spin_unlock(&obj->oo_guard);
1237 mark_inode_dirty(obj->oo_inode);
1241 static struct timespec *osd_inode_time(const struct lu_env *env,
1242 struct inode *inode, __u64 seconds)
1244 struct osd_thread_info *oti = osd_oti_get(env);
1245 struct timespec *t = &oti->oti_time;
1247 t->tv_sec = seconds;
1249 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1253 static int osd_inode_setattr(const struct lu_env *env,
1254 struct inode *inode, const struct lu_attr *attr)
1258 bits = attr->la_valid;
1260 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1262 #ifdef HAVE_QUOTA_SUPPORT
1263 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1264 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1265 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1271 iattr.ia_valid |= ATTR_UID;
1273 iattr.ia_valid |= ATTR_GID;
1274 iattr.ia_uid = attr->la_uid;
1275 iattr.ia_gid = attr->la_gid;
1276 osd_push_ctxt(env, save);
1277 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1284 if (bits & LA_ATIME)
1285 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1286 if (bits & LA_CTIME)
1287 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1288 if (bits & LA_MTIME)
1289 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1290 if (bits & LA_SIZE) {
1291 LDISKFS_I(inode)->i_disksize = attr->la_size;
1292 i_size_write(inode, attr->la_size);
1296 * OSD should not change "i_blocks" which is used by quota.
1297 * "i_blocks" should be changed by ldiskfs only.
1298 * Disable this assignment until SOM to fix some EA field. */
1299 if (bits & LA_BLOCKS)
1300 inode->i_blocks = attr->la_blocks;
1303 inode->i_mode = (inode->i_mode & S_IFMT) |
1304 (attr->la_mode & ~S_IFMT);
1306 inode->i_uid = attr->la_uid;
1308 inode->i_gid = attr->la_gid;
1309 if (bits & LA_NLINK)
1310 inode->i_nlink = attr->la_nlink;
1312 inode->i_rdev = attr->la_rdev;
1314 if (bits & LA_FLAGS) {
1315 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1317 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1318 (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1326 * XXX temporary solution.
1329 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1330 struct lu_attr *attr, struct thandle *th)
1335 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1336 struct lu_attr *attr, struct thandle *th)
1338 osd_object_init0(obj);
1342 extern struct inode *ldiskfs_create_inode(handle_t *handle,
1343 struct inode * dir, int mode);
1344 extern int ldiskfs_add_entry(handle_t *handle, struct dentry *dentry,
1345 struct inode *inode);
1346 extern int ldiskfs_delete_entry(handle_t *handle,
1348 struct ldiskfs_dir_entry_2 * de_del,
1349 struct buffer_head * bh);
1350 extern struct buffer_head * ldiskfs_find_entry(struct dentry *dentry,
1351 struct ldiskfs_dir_entry_2
1353 extern int ldiskfs_add_dot_dotdot(handle_t *handle, struct inode *dir,
1354 struct inode *inode);
1356 extern int ldiskfs_xattr_set_handle(handle_t *handle, struct inode *inode,
1357 int name_index, const char *name,
1358 const void *value, size_t value_len,
1361 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1362 struct osd_object *obj,
1366 struct osd_thread_info *info = osd_oti_get(env);
1367 struct dentry *child_dentry = &info->oti_child_dentry;
1368 struct dentry *obj_dentry = &info->oti_obj_dentry;
1370 obj_dentry->d_inode = obj->oo_inode;
1371 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1372 obj_dentry->d_name.hash = 0;
1374 child_dentry->d_name.hash = 0;
1375 child_dentry->d_parent = obj_dentry;
1376 child_dentry->d_name.name = name;
1377 child_dentry->d_name.len = namelen;
1378 return child_dentry;
1382 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1384 struct dt_allocation_hint *hint,
1388 struct osd_device *osd = osd_obj2dev(obj);
1389 struct osd_thandle *oth;
1390 struct dt_object *parent;
1391 struct inode *inode;
1392 #ifdef HAVE_QUOTA_SUPPORT
1393 struct osd_ctxt *save = &info->oti_ctxt;
1396 LINVRNT(osd_invariant(obj));
1397 LASSERT(obj->oo_inode == NULL);
1399 oth = container_of(th, struct osd_thandle, ot_super);
1400 LASSERT(oth->ot_handle->h_transaction != NULL);
1402 if (hint && hint->dah_parent)
1403 parent = hint->dah_parent;
1405 parent = osd->od_obj_area;
1407 LASSERT(parent != NULL);
1408 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1410 #ifdef HAVE_QUOTA_SUPPORT
1411 osd_push_ctxt(info->oti_env, save);
1413 inode = ldiskfs_create_inode(oth->ot_handle,
1414 osd_dt_obj(parent)->oo_inode, mode);
1415 #ifdef HAVE_QUOTA_SUPPORT
1418 if (!IS_ERR(inode)) {
1419 obj->oo_inode = inode;
1422 result = PTR_ERR(inode);
1423 LINVRNT(osd_invariant(obj));
1428 extern int iam_lvar_create(struct inode *obj, int keysize, int ptrsize,
1429 int recsize, handle_t *handle);
1431 extern int iam_lfix_create(struct inode *obj, int keysize, int ptrsize,
1432 int recsize, handle_t *handle);
1439 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1440 struct lu_attr *attr,
1441 struct dt_allocation_hint *hint,
1442 struct dt_object_format *dof,
1446 struct osd_thandle *oth;
1447 struct osd_device *osd = osd_obj2dev(obj);
1448 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1450 LASSERT(S_ISDIR(attr->la_mode));
1452 oth = container_of(th, struct osd_thandle, ot_super);
1453 LASSERT(oth->ot_handle->h_transaction != NULL);
1454 result = osd_mkfile(info, obj, mode, hint, th);
1455 if (result == 0 && osd->od_iop_mode == 0) {
1456 LASSERT(obj->oo_inode != NULL);
1458 * XXX uh-oh... call low-level iam function directly.
1461 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1462 sizeof (struct lu_fid_pack),
1468 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1469 struct lu_attr *attr,
1470 struct dt_allocation_hint *hint,
1471 struct dt_object_format *dof,
1475 struct osd_thandle *oth;
1476 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1478 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1480 LASSERT(S_ISREG(attr->la_mode));
1482 oth = container_of(th, struct osd_thandle, ot_super);
1483 LASSERT(oth->ot_handle->h_transaction != NULL);
1485 result = osd_mkfile(info, obj, mode, hint, th);
1487 LASSERT(obj->oo_inode != NULL);
1488 if (feat->dif_flags & DT_IND_VARKEY)
1489 result = iam_lvar_create(obj->oo_inode,
1490 feat->dif_keysize_max,
1492 feat->dif_recsize_max,
1495 result = iam_lfix_create(obj->oo_inode,
1496 feat->dif_keysize_max,
1498 feat->dif_recsize_max,
1505 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1506 struct lu_attr *attr,
1507 struct dt_allocation_hint *hint,
1508 struct dt_object_format *dof,
1511 LASSERT(S_ISREG(attr->la_mode));
1512 return osd_mkfile(info, obj, (attr->la_mode &
1513 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1516 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1517 struct lu_attr *attr,
1518 struct dt_allocation_hint *hint,
1519 struct dt_object_format *dof,
1522 LASSERT(S_ISLNK(attr->la_mode));
1523 return osd_mkfile(info, obj, (attr->la_mode &
1524 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1527 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1528 struct lu_attr *attr,
1529 struct dt_allocation_hint *hint,
1530 struct dt_object_format *dof,
1533 umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1536 LINVRNT(osd_invariant(obj));
1537 LASSERT(obj->oo_inode == NULL);
1538 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1539 S_ISFIFO(mode) || S_ISSOCK(mode));
1541 result = osd_mkfile(info, obj, mode, hint, th);
1543 LASSERT(obj->oo_inode != NULL);
1544 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1546 LINVRNT(osd_invariant(obj));
1550 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1552 struct dt_allocation_hint *hint,
1553 struct dt_object_format *dof,
1556 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1558 osd_obj_type_f result;
1574 result = osd_mk_index;
1585 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1586 struct dt_object *parent, umode_t child_mode)
1590 memset(ah, 0, sizeof(*ah));
1591 ah->dah_parent = parent;
1592 ah->dah_mode = child_mode;
1596 * Helper function for osd_object_create()
1598 * \retval 0, on success
1600 static int __osd_object_create(struct osd_thread_info *info,
1601 struct osd_object *obj, struct lu_attr *attr,
1602 struct dt_allocation_hint *hint,
1603 struct dt_object_format *dof,
1609 result = osd_create_pre(info, obj, attr, th);
1611 result = osd_create_type_f(dof->dof_type)(info, obj,
1612 attr, hint, dof, th);
1614 result = osd_create_post(info, obj, attr, th);
1620 * Helper function for osd_object_create()
1622 * \retval 0, on success
1624 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1625 const struct lu_fid *fid, struct thandle *th)
1627 struct osd_thread_info *info = osd_oti_get(env);
1628 struct osd_inode_id *id = &info->oti_id;
1629 struct osd_device *osd = osd_obj2dev(obj);
1630 struct md_ucred *uc = md_ucred(env);
1632 LASSERT(obj->oo_inode != NULL);
1633 LASSERT(uc != NULL);
1635 id->oii_ino = obj->oo_inode->i_ino;
1636 id->oii_gen = obj->oo_inode->i_generation;
1638 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1639 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1642 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1643 struct lu_attr *attr,
1644 struct dt_allocation_hint *hint,
1645 struct dt_object_format *dof,
1648 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1649 struct osd_object *obj = osd_dt_obj(dt);
1650 struct osd_thread_info *info = osd_oti_get(env);
1655 LINVRNT(osd_invariant(obj));
1656 LASSERT(!dt_object_exists(dt));
1657 LASSERT(osd_write_locked(env, obj));
1658 LASSERT(th != NULL);
1660 result = __osd_object_create(info, obj, attr, hint, dof, th);
1662 result = __osd_oi_insert(env, obj, fid, th);
1664 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1665 LASSERT(osd_invariant(obj));
1670 * Helper function for osd_xattr_set()
1672 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1673 const struct lu_buf *buf, const char *name, int fl)
1675 struct osd_object *obj = osd_dt_obj(dt);
1676 struct inode *inode = obj->oo_inode;
1677 struct osd_thread_info *info = osd_oti_get(env);
1678 struct dentry *dentry = &info->oti_child_dentry;
1679 struct timespec *t = &info->oti_time;
1683 LASSERT(dt_object_exists(dt));
1684 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1685 LASSERT(osd_write_locked(env, obj));
1687 if (fl & LU_XATTR_REPLACE)
1688 fs_flags |= XATTR_REPLACE;
1690 if (fl & LU_XATTR_CREATE)
1691 fs_flags |= XATTR_CREATE;
1693 dentry->d_inode = inode;
1694 *t = inode->i_ctime;
1695 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1696 buf->lb_len, fs_flags);
1697 /* ctime should not be updated with server-side time. */
1698 spin_lock(&obj->oo_guard);
1699 inode->i_ctime = *t;
1700 spin_unlock(&obj->oo_guard);
1701 mark_inode_dirty(inode);
1706 * Put the fid into lustre_mdt_attrs, and then place the structure
1707 * inode's ea. This fid should not be altered during the life time
1710 * \retval +ve, on success
1711 * \retval -ve, on error
1713 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1715 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1716 const struct lu_fid *fid)
1718 struct osd_thread_info *info = osd_oti_get(env);
1719 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1721 fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1723 return __osd_xattr_set(env, dt,
1724 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1725 XATTR_NAME_LMA, LU_XATTR_CREATE);
1730 * Helper function to form igif
1732 static inline void osd_igif_get(const struct lu_env *env, struct dentry *dentry,
1735 struct inode *inode = dentry->d_inode;
1736 lu_igif_build(fid, inode->i_ino, inode->i_generation);
1740 * Helper function to pack the fid
1742 static inline void osd_fid_pack(const struct lu_env *env, const struct lu_fid *fid,
1743 struct lu_fid_pack *pack)
1745 fid_pack(pack, fid, &osd_oti_get(env)->oti_fid);
1749 * Try to read the fid from inode ea into dt_rec, if return value
1750 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1752 * \param rec, the data-structure into which fid/igif is read
1754 * \retval 0, on success
1756 static int osd_ea_fid_get(const struct lu_env *env, struct dentry *dentry,
1759 struct inode *inode = dentry->d_inode;
1760 struct osd_thread_info *info = osd_oti_get(env);
1761 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1762 struct lu_fid *fid = &info->oti_fid;
1765 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1767 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1771 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1773 } else if (rc == -ENODATA) {
1774 osd_igif_get(env, dentry, fid);
1779 osd_fid_pack(env, fid, (struct lu_fid_pack*)rec);
1785 * OSD layer object create function for interoperability mode (b11826).
1786 * This is mostly similar to osd_object_create(). Only difference being, fid is
1787 * inserted into inode ea here.
1789 * \retval 0, on success
1790 * \retval -ve, on error
1792 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1793 struct lu_attr *attr,
1794 struct dt_allocation_hint *hint,
1795 struct dt_object_format *dof,
1798 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1799 struct osd_object *obj = osd_dt_obj(dt);
1800 struct osd_thread_info *info = osd_oti_get(env);
1806 LASSERT(osd_invariant(obj));
1807 LASSERT(!dt_object_exists(dt));
1808 LASSERT(osd_write_locked(env, obj));
1809 LASSERT(th != NULL);
1811 result = __osd_object_create(info, obj, attr, hint, dof, th);
1813 if (hint && hint->dah_parent)
1814 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1816 /* objects under osd root shld have igif fid, so dont add fid EA */
1817 if (result == 0 && is_root == 0)
1818 result = osd_ea_fid_set(env, dt, fid);
1821 result = __osd_oi_insert(env, obj, fid, th);
1823 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1824 LINVRNT(osd_invariant(obj));
1829 * Concurrency: @dt is write locked.
1831 static void osd_object_ref_add(const struct lu_env *env,
1832 struct dt_object *dt,
1835 struct osd_object *obj = osd_dt_obj(dt);
1836 struct inode *inode = obj->oo_inode;
1838 LINVRNT(osd_invariant(obj));
1839 LASSERT(dt_object_exists(dt));
1840 LASSERT(osd_write_locked(env, obj));
1841 LASSERT(th != NULL);
1843 spin_lock(&obj->oo_guard);
1844 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1846 spin_unlock(&obj->oo_guard);
1847 mark_inode_dirty(inode);
1848 LINVRNT(osd_invariant(obj));
1852 * Concurrency: @dt is write locked.
1854 static void osd_object_ref_del(const struct lu_env *env,
1855 struct dt_object *dt,
1858 struct osd_object *obj = osd_dt_obj(dt);
1859 struct inode *inode = obj->oo_inode;
1861 LINVRNT(osd_invariant(obj));
1862 LASSERT(dt_object_exists(dt));
1863 LASSERT(osd_write_locked(env, obj));
1864 LASSERT(th != NULL);
1866 spin_lock(&obj->oo_guard);
1867 LASSERT(inode->i_nlink > 0);
1869 spin_unlock(&obj->oo_guard);
1870 mark_inode_dirty(inode);
1871 LINVRNT(osd_invariant(obj));
1875 * Concurrency: @dt is read locked.
1877 static int osd_xattr_get(const struct lu_env *env,
1878 struct dt_object *dt,
1881 struct lustre_capa *capa)
1883 struct osd_object *obj = osd_dt_obj(dt);
1884 struct inode *inode = obj->oo_inode;
1885 struct osd_thread_info *info = osd_oti_get(env);
1886 struct dentry *dentry = &info->oti_obj_dentry;
1888 LASSERT(dt_object_exists(dt));
1889 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1890 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1892 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1895 dentry->d_inode = inode;
1896 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1901 * Concurrency: @dt is write locked.
1903 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1904 const struct lu_buf *buf, const char *name, int fl,
1905 struct thandle *handle, struct lustre_capa *capa)
1907 LASSERT(handle != NULL);
1909 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1912 return __osd_xattr_set(env, dt, buf, name, fl);
1916 * Concurrency: @dt is read locked.
1918 static int osd_xattr_list(const struct lu_env *env,
1919 struct dt_object *dt,
1921 struct lustre_capa *capa)
1923 struct osd_object *obj = osd_dt_obj(dt);
1924 struct inode *inode = obj->oo_inode;
1925 struct osd_thread_info *info = osd_oti_get(env);
1926 struct dentry *dentry = &info->oti_obj_dentry;
1928 LASSERT(dt_object_exists(dt));
1929 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1930 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1932 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1935 dentry->d_inode = inode;
1936 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1940 * Concurrency: @dt is write locked.
1942 static int osd_xattr_del(const struct lu_env *env,
1943 struct dt_object *dt,
1945 struct thandle *handle,
1946 struct lustre_capa *capa)
1948 struct osd_object *obj = osd_dt_obj(dt);
1949 struct inode *inode = obj->oo_inode;
1950 struct osd_thread_info *info = osd_oti_get(env);
1951 struct dentry *dentry = &info->oti_obj_dentry;
1952 struct timespec *t = &info->oti_time;
1955 LASSERT(dt_object_exists(dt));
1956 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1957 LASSERT(osd_write_locked(env, obj));
1958 LASSERT(handle != NULL);
1960 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1963 dentry->d_inode = inode;
1964 *t = inode->i_ctime;
1965 rc = inode->i_op->removexattr(dentry, name);
1966 /* ctime should not be updated with server-side time. */
1967 spin_lock(&obj->oo_guard);
1968 inode->i_ctime = *t;
1969 spin_unlock(&obj->oo_guard);
1970 mark_inode_dirty(inode);
1974 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1975 struct dt_object *dt,
1976 struct lustre_capa *old,
1979 struct osd_thread_info *info = osd_oti_get(env);
1980 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1981 struct osd_object *obj = osd_dt_obj(dt);
1982 struct osd_device *dev = osd_obj2dev(obj);
1983 struct lustre_capa_key *key = &info->oti_capa_key;
1984 struct lustre_capa *capa = &info->oti_capa;
1985 struct obd_capa *oc;
1986 struct md_capainfo *ci;
1990 if (!dev->od_fl_capa)
1991 RETURN(ERR_PTR(-ENOENT));
1993 LASSERT(dt_object_exists(dt));
1994 LINVRNT(osd_invariant(obj));
1996 /* renewal sanity check */
1997 if (old && osd_object_auth(env, dt, old, opc))
1998 RETURN(ERR_PTR(-EACCES));
2000 ci = md_capainfo(env);
2002 RETURN(ERR_PTR(-ENOENT));
2004 switch (ci->mc_auth) {
2008 capa->lc_uid = obj->oo_inode->i_uid;
2009 capa->lc_gid = obj->oo_inode->i_gid;
2010 capa->lc_flags = LC_ID_PLAIN;
2012 case LC_ID_CONVERT: {
2015 s[0] = obj->oo_inode->i_uid;
2016 get_random_bytes(&(s[1]), sizeof(__u32));
2017 s[2] = obj->oo_inode->i_gid;
2018 get_random_bytes(&(s[3]), sizeof(__u32));
2019 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2021 RETURN(ERR_PTR(rc));
2023 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2024 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2025 capa->lc_flags = LC_ID_CONVERT;
2029 RETURN(ERR_PTR(-EINVAL));
2032 capa->lc_fid = *fid;
2034 capa->lc_flags |= dev->od_capa_alg << 24;
2035 capa->lc_timeout = dev->od_capa_timeout;
2036 capa->lc_expiry = 0;
2038 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2040 LASSERT(!capa_is_expired(oc));
2044 spin_lock(&capa_lock);
2045 *key = dev->od_capa_keys[1];
2046 spin_unlock(&capa_lock);
2048 capa->lc_keyid = key->lk_keyid;
2049 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2051 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2053 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2054 RETURN(ERR_PTR(rc));
2057 oc = capa_add(dev->od_capa_hash, capa);
2061 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2064 struct osd_object *obj = osd_dt_obj(dt);
2065 struct inode *inode = obj->oo_inode;
2066 struct osd_thread_info *info = osd_oti_get(env);
2067 struct dentry *dentry = &info->oti_obj_dentry;
2068 struct file *file = &info->oti_file;
2071 dentry->d_inode = inode;
2072 file->f_dentry = dentry;
2073 file->f_mapping = inode->i_mapping;
2074 file->f_op = inode->i_fop;
2075 LOCK_INODE_MUTEX(inode);
2076 rc = file->f_op->fsync(file, dentry, 0);
2077 UNLOCK_INODE_MUTEX(inode);
2081 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2084 struct osd_object *obj = osd_dt_obj(dt);
2087 *data = (void *)obj->oo_inode;
2091 static const struct dt_object_operations osd_obj_ops = {
2092 .do_read_lock = osd_object_read_lock,
2093 .do_write_lock = osd_object_write_lock,
2094 .do_read_unlock = osd_object_read_unlock,
2095 .do_write_unlock = osd_object_write_unlock,
2096 .do_attr_get = osd_attr_get,
2097 .do_attr_set = osd_attr_set,
2098 .do_ah_init = osd_ah_init,
2099 .do_create = osd_object_create,
2100 .do_index_try = osd_index_try,
2101 .do_ref_add = osd_object_ref_add,
2102 .do_ref_del = osd_object_ref_del,
2103 .do_xattr_get = osd_xattr_get,
2104 .do_xattr_set = osd_xattr_set,
2105 .do_xattr_del = osd_xattr_del,
2106 .do_xattr_list = osd_xattr_list,
2107 .do_capa_get = osd_capa_get,
2108 .do_object_sync = osd_object_sync,
2109 .do_data_get = osd_data_get,
2113 * dt_object_operations for interoperability mode
2114 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2116 static const struct dt_object_operations osd_obj_ea_ops = {
2117 .do_read_lock = osd_object_read_lock,
2118 .do_write_lock = osd_object_write_lock,
2119 .do_read_unlock = osd_object_read_unlock,
2120 .do_write_unlock = osd_object_write_unlock,
2121 .do_attr_get = osd_attr_get,
2122 .do_attr_set = osd_attr_set,
2123 .do_ah_init = osd_ah_init,
2124 .do_create = osd_object_ea_create,
2125 .do_index_try = osd_index_try,
2126 .do_ref_add = osd_object_ref_add,
2127 .do_ref_del = osd_object_ref_del,
2128 .do_xattr_get = osd_xattr_get,
2129 .do_xattr_set = osd_xattr_set,
2130 .do_xattr_del = osd_xattr_del,
2131 .do_xattr_list = osd_xattr_list,
2132 .do_capa_get = osd_capa_get,
2133 .do_object_sync = osd_object_sync,
2134 .do_data_get = osd_data_get,
2142 * XXX: Another layering violation for now.
2144 * We don't want to use ->f_op->read methods, because generic file write
2146 * - serializes on ->i_sem, and
2148 * - does a lot of extra work like balance_dirty_pages(),
2150 * which doesn't work for globally shared files like /last-received.
2152 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2153 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2154 loff_t *offs, handle_t *handle);
2156 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2157 struct lu_buf *buf, loff_t *pos,
2158 struct lustre_capa *capa)
2160 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2162 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2165 return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2168 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2169 const struct lu_buf *buf, loff_t *pos,
2170 struct thandle *handle, struct lustre_capa *capa,
2173 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2174 struct osd_thandle *oh;
2176 #ifdef HAVE_QUOTA_SUPPORT
2177 cfs_cap_t save = current->cap_effective;
2180 LASSERT(handle != NULL);
2182 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2185 oh = container_of(handle, struct osd_thandle, ot_super);
2186 LASSERT(oh->ot_handle->h_transaction != NULL);
2187 #ifdef HAVE_QUOTA_SUPPORT
2189 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2191 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2193 result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2194 pos, oh->ot_handle);
2195 #ifdef HAVE_QUOTA_SUPPORT
2196 current->cap_effective = save;
2199 result = buf->lb_len;
2203 static const struct dt_body_operations osd_body_ops = {
2204 .dbo_read = osd_read,
2205 .dbo_write = osd_write
2212 static int osd_object_is_root(const struct osd_object *obj)
2214 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
2217 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2218 const struct dt_index_features *feat)
2220 struct iam_descr *descr;
2221 struct dt_object *dt = &o->oo_dt;
2223 if (osd_object_is_root(o))
2224 return feat == &dt_directory_features;
2226 LASSERT(o->oo_dir != NULL);
2228 descr = o->oo_dir->od_container.ic_descr;
2229 if (feat == &dt_directory_features) {
2230 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2233 if (descr == &iam_htree_compat_param) {
2234 /* if it is a HTREE dir then there is good chance that,
2235 * we dealing with ext3 directory here with no FIDs. */
2237 if (descr->id_rec_size ==
2238 sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2240 dt->do_index_ops = &osd_index_ea_ops;
2247 feat->dif_keysize_min <= descr->id_key_size &&
2248 descr->id_key_size <= feat->dif_keysize_max &&
2249 feat->dif_recsize_min <= descr->id_rec_size &&
2250 descr->id_rec_size <= feat->dif_recsize_max &&
2251 !(feat->dif_flags & (DT_IND_VARKEY |
2252 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2253 ergo(feat->dif_flags & DT_IND_UPDATE,
2254 1 /* XXX check that object (and file system) is
2259 static int osd_iam_container_init(const struct lu_env *env,
2260 struct osd_object *obj,
2261 struct osd_directory *dir)
2264 struct iam_container *bag;
2266 bag = &dir->od_container;
2267 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2269 result = iam_container_setup(bag);
2271 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2273 iam_container_fini(bag);
2279 * Concurrency: no external locking is necessary.
2281 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2282 const struct dt_index_features *feat)
2286 struct osd_object *obj = osd_dt_obj(dt);
2287 struct osd_device *osd = osd_obj2dev(obj);
2289 LINVRNT(osd_invariant(obj));
2290 LASSERT(dt_object_exists(dt));
2292 if (osd_object_is_root(obj)) {
2293 dt->do_index_ops = &osd_index_ea_ops;
2295 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2296 dt->do_index_ops = &osd_index_ea_ops;
2297 if (S_ISDIR(obj->oo_inode->i_mode))
2302 } else if (!osd_has_index(obj)) {
2303 struct osd_directory *dir;
2308 spin_lock(&obj->oo_guard);
2309 if (obj->oo_dir == NULL)
2313 * Concurrent thread allocated container data.
2316 spin_unlock(&obj->oo_guard);
2318 * Now, that we have container data, serialize its
2321 down_write(&obj->oo_ext_idx_sem);
2323 * recheck under lock.
2325 if (!osd_has_index(obj))
2326 result = osd_iam_container_init(env, obj, dir);
2329 up_write(&obj->oo_ext_idx_sem);
2335 if (result == 0 && ea_dir == 0) {
2336 if (!osd_iam_index_probe(env, obj, feat))
2339 LINVRNT(osd_invariant(obj));
2345 * delete a (key, value) pair from index \a dt specified by \a key
2347 * \param dt_object osd index object
2348 * \param key key for index
2349 * \param rec record reference
2350 * \param handle transaction handler
2353 * \retval -ve failure
2356 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2357 const struct dt_key *key, struct thandle *handle,
2358 struct lustre_capa *capa)
2360 struct osd_object *obj = osd_dt_obj(dt);
2361 struct osd_thandle *oh;
2362 struct iam_path_descr *ipd;
2363 struct iam_container *bag = &obj->oo_dir->od_container;
2368 LINVRNT(osd_invariant(obj));
2369 LASSERT(dt_object_exists(dt));
2370 LASSERT(bag->ic_object == obj->oo_inode);
2371 LASSERT(handle != NULL);
2373 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2376 ipd = osd_idx_ipd_get(env, bag);
2377 if (unlikely(ipd == NULL))
2380 oh = container_of0(handle, struct osd_thandle, ot_super);
2381 LASSERT(oh->ot_handle != NULL);
2382 LASSERT(oh->ot_handle->h_transaction != NULL);
2384 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2385 osd_ipd_put(env, bag, ipd);
2386 LINVRNT(osd_invariant(obj));
2391 * Index delete function for interoperability mode (b11826).
2392 * It will remove the directory entry added by osd_index_ea_insert().
2393 * This entry is needed to maintain name->fid mapping.
2395 * \param key, key i.e. file entry to be deleted
2397 * \retval 0, on success
2398 * \retval -ve, on error
2400 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2401 const struct dt_key *key, struct thandle *handle,
2402 struct lustre_capa *capa)
2404 struct osd_object *obj = osd_dt_obj(dt);
2405 struct inode *dir = obj->oo_inode;
2406 struct dentry *dentry;
2407 struct osd_thandle *oh;
2408 struct ldiskfs_dir_entry_2 *de;
2409 struct buffer_head *bh;
2415 LINVRNT(osd_invariant(obj));
2416 LASSERT(dt_object_exists(dt));
2417 LASSERT(handle != NULL);
2419 oh = container_of(handle, struct osd_thandle, ot_super);
2420 LASSERT(oh->ot_handle != NULL);
2421 LASSERT(oh->ot_handle->h_transaction != NULL);
2423 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2426 dentry = osd_child_dentry_get(env, obj,
2427 (char *)key, strlen((char *)key));
2429 down_write(&obj->oo_ext_idx_sem);
2430 bh = ldiskfs_find_entry(dentry, &de);
2432 struct osd_thread_info *oti = osd_oti_get(env);
2433 struct timespec *ctime = &oti->oti_time;
2434 struct timespec *mtime = &oti->oti_time2;
2436 *ctime = dir->i_ctime;
2437 *mtime = dir->i_mtime;
2438 rc = ldiskfs_delete_entry(oh->ot_handle,
2440 /* xtime should not be updated with server-side time. */
2441 spin_lock(&obj->oo_guard);
2442 dir->i_ctime = *ctime;
2443 dir->i_mtime = *mtime;
2444 spin_unlock(&obj->oo_guard);
2445 mark_inode_dirty(dir);
2450 up_write(&obj->oo_ext_idx_sem);
2451 LASSERT(osd_invariant(obj));
2456 * Lookup index for \a key and copy record to \a rec.
2458 * \param dt_object osd index object
2459 * \param key key for index
2460 * \param rec record reference
2462 * \retval +ve success : exact mach
2463 * \retval 0 return record with key not greater than \a key
2464 * \retval -ve failure
2466 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2467 struct dt_rec *rec, const struct dt_key *key,
2468 struct lustre_capa *capa)
2470 struct osd_object *obj = osd_dt_obj(dt);
2471 struct iam_path_descr *ipd;
2472 struct iam_container *bag = &obj->oo_dir->od_container;
2473 struct osd_thread_info *oti = osd_oti_get(env);
2474 struct iam_iterator *it = &oti->oti_idx_it;
2478 LASSERT(osd_invariant(obj));
2479 LASSERT(dt_object_exists(dt));
2480 LASSERT(bag->ic_object == obj->oo_inode);
2482 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2485 ipd = osd_idx_ipd_get(env, bag);
2489 /* got ipd now we can start iterator. */
2490 iam_it_init(it, bag, 0, ipd);
2492 rc = iam_it_get(it, (struct iam_key *)key);
2494 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2498 osd_ipd_put(env, bag, ipd);
2500 LINVRNT(osd_invariant(obj));
2506 * Inserts (key, value) pair in \a dt index object.
2508 * \param dt osd index object
2509 * \param key key for index
2510 * \param rec record reference
2511 * \param th transaction handler
2514 * \retval -ve failure
2516 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2517 const struct dt_rec *rec, const struct dt_key *key,
2518 struct thandle *th, struct lustre_capa *capa,
2521 struct osd_object *obj = osd_dt_obj(dt);
2522 struct iam_path_descr *ipd;
2523 struct osd_thandle *oh;
2524 struct iam_container *bag = &obj->oo_dir->od_container;
2525 #ifdef HAVE_QUOTA_SUPPORT
2526 cfs_cap_t save = current->cap_effective;
2532 LINVRNT(osd_invariant(obj));
2533 LASSERT(dt_object_exists(dt));
2534 LASSERT(bag->ic_object == obj->oo_inode);
2535 LASSERT(th != NULL);
2537 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2540 ipd = osd_idx_ipd_get(env, bag);
2541 if (unlikely(ipd == NULL))
2544 oh = container_of0(th, struct osd_thandle, ot_super);
2545 LASSERT(oh->ot_handle != NULL);
2546 LASSERT(oh->ot_handle->h_transaction != NULL);
2547 #ifdef HAVE_QUOTA_SUPPORT
2549 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2551 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2553 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2554 (struct iam_rec *)rec, ipd);
2555 #ifdef HAVE_QUOTA_SUPPORT
2556 current->cap_effective = save;
2558 osd_ipd_put(env, bag, ipd);
2559 LINVRNT(osd_invariant(obj));
2564 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2565 * into the directory.Also sets flags into osd object to
2566 * indicate dot and dotdot are created. This is required for
2567 * interoperability mode (b11826)
2569 * \param dir directory for dot and dotdot fixup.
2570 * \param obj child object for linking
2572 * \retval 0, on success
2573 * \retval -ve, on error
2575 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2576 struct osd_object *dir,
2577 struct osd_object *obj, const char *name,
2580 struct inode *parent_dir = obj->oo_inode;
2581 struct inode *inode = dir->oo_inode;
2582 struct osd_thandle *oth;
2585 oth = container_of(th, struct osd_thandle, ot_super);
2586 LASSERT(oth->ot_handle->h_transaction != NULL);
2587 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2589 if (strcmp(name, dot) == 0) {
2590 if (dir->oo_compat_dot_created) {
2593 LASSERT(obj == dir);
2594 dir->oo_compat_dot_created = 1;
2597 } else if(strcmp(name, dotdot) == 0) {
2598 if (!dir->oo_compat_dot_created)
2600 if (dir->oo_compat_dotdot_created)
2601 return __osd_ea_add_rec(info, dir, obj, name, th);
2603 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2605 dir->oo_compat_dotdot_created = 1;
2612 * Calls ldiskfs_add_entry() to add directory entry
2613 * into the directory. This is required for
2614 * interoperability mode (b11826)
2616 * \retval 0, on success
2617 * \retval -ve, on error
2619 static int __osd_ea_add_rec(struct osd_thread_info *info,
2620 struct osd_object *pobj,
2621 struct osd_object *cobj,
2625 struct dentry *child;
2626 struct osd_thandle *oth;
2627 struct inode *cinode = cobj->oo_inode;
2630 oth = container_of(th, struct osd_thandle, ot_super);
2631 LASSERT(oth->ot_handle != NULL);
2632 LASSERT(oth->ot_handle->h_transaction != NULL);
2634 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2635 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2641 * It will call the appropriate osd_add* function and return the
2642 * value, return by respective functions.
2644 static int osd_ea_add_rec(const struct lu_env *env,
2645 struct osd_object *pobj,
2646 struct osd_object *cobj,
2650 struct osd_thread_info *info = osd_oti_get(env);
2653 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2655 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2657 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2663 * Calls ->lookup() to find dentry. From dentry get inode and
2664 * read inode's ea to get fid. This is required for interoperability
2667 * \retval 0, on success
2668 * \retval -ve, on error
2670 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2671 struct dt_rec *rec, const struct dt_key *key)
2673 struct inode *dir = obj->oo_inode;
2674 struct osd_thread_info *info = osd_oti_get(env);
2675 struct dentry *dentry;
2676 struct osd_device *dev = osd_dev(obj->oo_dt.do_lu.lo_dev);
2677 struct osd_inode_id *id = &info->oti_id;
2678 struct ldiskfs_dir_entry_2 *de;
2679 struct buffer_head *bh;
2680 struct inode *inode;
2684 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2686 dentry = osd_child_dentry_get(env, obj,
2687 (char *)key, strlen((char *)key));
2689 down_read(&obj->oo_ext_idx_sem);
2690 bh = ldiskfs_find_entry(dentry, &de);
2692 ino = le32_to_cpu(de->inode);
2695 id->oii_gen = OSD_OII_NOGEN;
2697 inode = osd_iget(info, dev, id);
2698 if (!IS_ERR(inode)) {
2699 dentry->d_inode = inode;
2701 rc = osd_ea_fid_get(env, dentry, rec);
2704 rc = PTR_ERR(inode);
2708 up_read(&obj->oo_ext_idx_sem);
2713 * Find the osd object for given fid.
2715 * \param fid, need to find the osd object having this fid
2717 * \retval osd_object, on success
2718 * \retval -ve, on error
2720 struct osd_object *osd_object_find(const struct lu_env *env,
2721 struct dt_object *dt,
2722 const struct lu_fid *fid)
2724 struct lu_device *ludev = dt->do_lu.lo_dev;
2725 struct osd_object *child = NULL;
2726 struct lu_object *luch;
2727 struct lu_object *lo;
2729 luch = lu_object_find(env, ludev, fid, NULL);
2730 if (!IS_ERR(luch)) {
2731 if (lu_object_exists(luch)) {
2732 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2734 child = osd_obj(lo);
2736 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2737 "lu_object can't be located"
2738 ""DFID"\n", PFID(fid));
2740 if (child == NULL) {
2741 lu_object_put(env, luch);
2742 CERROR("Unable to get osd_object\n");
2743 child = ERR_PTR(-ENOENT);
2746 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2747 "lu_object does not exists "DFID"\n",
2749 child = ERR_PTR(-ENOENT);
2752 child = (void *)luch;
2758 * Put the osd object once done with it.
2760 * \param obj, osd object that needs to be put
2762 static inline void osd_object_put(const struct lu_env *env,
2763 struct osd_object *obj)
2765 lu_object_put(env, &obj->oo_dt.do_lu);
2769 * Index add function for interoperability mode (b11826).
2770 * It will add the directory entry.This entry is needed to
2771 * maintain name->fid mapping.
2773 * \param key, it is key i.e. file entry to be inserted
2774 * \param rec, it is value of given key i.e. fid
2776 * \retval 0, on success
2777 * \retval -ve, on error
2779 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2780 const struct dt_rec *rec,
2781 const struct dt_key *key, struct thandle *th,
2782 struct lustre_capa *capa, int ignore_quota)
2784 struct osd_object *obj = osd_dt_obj(dt);
2785 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
2786 const struct lu_fid_pack *pack = (const struct lu_fid_pack *)rec;
2787 const char *name = (const char *)key;
2788 struct osd_object *child;
2789 #ifdef HAVE_QUOTA_SUPPORT
2790 cfs_cap_t save = current->cap_effective;
2796 LASSERT(osd_invariant(obj));
2797 LASSERT(dt_object_exists(dt));
2798 LASSERT(th != NULL);
2800 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2803 rc = fid_unpack(pack, fid);
2806 child = osd_object_find(env, dt, fid);
2807 if (!IS_ERR(child)) {
2808 struct inode *inode = obj->oo_inode;
2809 struct osd_thread_info *oti = osd_oti_get(env);
2810 struct timespec *ctime = &oti->oti_time;
2811 struct timespec *mtime = &oti->oti_time2;
2813 *ctime = inode->i_ctime;
2814 *mtime = inode->i_mtime;
2815 #ifdef HAVE_QUOTA_SUPPORT
2817 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2819 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2821 down_write(&obj->oo_ext_idx_sem);
2822 rc = osd_ea_add_rec(env, obj, child, name, th);
2823 up_write(&obj->oo_ext_idx_sem);
2824 #ifdef HAVE_QUOTA_SUPPORT
2825 current->cap_effective = save;
2827 osd_object_put(env, child);
2828 /* xtime should not be updated with server-side time. */
2829 spin_lock(&obj->oo_guard);
2830 inode->i_ctime = *ctime;
2831 inode->i_mtime = *mtime;
2832 spin_unlock(&obj->oo_guard);
2833 mark_inode_dirty(inode);
2835 rc = PTR_ERR(child);
2838 LASSERT(osd_invariant(obj));
2843 * Initialize osd Iterator for given osd index object.
2845 * \param dt osd index object
2848 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2849 struct dt_object *dt,
2850 struct lustre_capa *capa)
2852 struct osd_it_iam *it;
2853 struct osd_thread_info *oti = osd_oti_get(env);
2854 struct osd_object *obj = osd_dt_obj(dt);
2855 struct lu_object *lo = &dt->do_lu;
2856 struct iam_path_descr *ipd;
2857 struct iam_container *bag = &obj->oo_dir->od_container;
2859 LASSERT(lu_object_exists(lo));
2861 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2862 return ERR_PTR(-EACCES);
2865 ipd = osd_it_ipd_get(env, bag);
2866 if (likely(ipd != NULL)) {
2870 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2871 return (struct dt_it *)it;
2873 return ERR_PTR(-ENOMEM);
2877 * free given Iterator.
2880 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2882 struct osd_it_iam *it = (struct osd_it_iam *)di;
2883 struct osd_object *obj = it->oi_obj;
2885 iam_it_fini(&it->oi_it);
2886 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2887 lu_object_put(env, &obj->oo_dt.do_lu);
2891 * Move Iterator to record specified by \a key
2893 * \param di osd iterator
2894 * \param key key for index
2896 * \retval +ve di points to record with least key not larger than key
2897 * \retval 0 di points to exact matched key
2898 * \retval -ve failure
2901 static int osd_it_iam_get(const struct lu_env *env,
2902 struct dt_it *di, const struct dt_key *key)
2904 struct osd_it_iam *it = (struct osd_it_iam *)di;
2906 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2912 * \param di osd iterator
2915 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2917 struct osd_it_iam *it = (struct osd_it_iam *)di;
2919 iam_it_put(&it->oi_it);
2923 * Move iterator by one record
2925 * \param di osd iterator
2927 * \retval +1 end of container reached
2929 * \retval -ve failure
2932 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2934 struct osd_it_iam *it = (struct osd_it_iam *)di;
2936 return iam_it_next(&it->oi_it);
2940 * Return pointer to the key under iterator.
2943 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2944 const struct dt_it *di)
2946 struct osd_it_iam *it = (struct osd_it_iam *)di;
2948 return (struct dt_key *)iam_it_key_get(&it->oi_it);
2952 * Return size of key under iterator (in bytes)
2955 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
2957 struct osd_it_iam *it = (struct osd_it_iam *)di;
2959 return iam_it_key_size(&it->oi_it);
2963 * Return pointer to the record under iterator.
2965 static struct dt_rec *osd_it_iam_rec(const struct lu_env *env,
2966 const struct dt_it *di)
2968 struct osd_it_iam *it = (struct osd_it_iam *)di;
2970 return (struct dt_rec *)iam_it_rec_get(&it->oi_it);
2974 * Returns cookie for current Iterator position.
2976 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
2978 struct osd_it_iam *it = (struct osd_it_iam *)di;
2980 return iam_it_store(&it->oi_it);
2984 * Restore iterator from cookie.
2986 * \param di osd iterator
2987 * \param hash Iterator location cookie
2989 * \retval +ve di points to record with least key not larger than key.
2990 * \retval 0 di points to exact matched key
2991 * \retval -ve failure
2994 static int osd_it_iam_load(const struct lu_env *env,
2995 const struct dt_it *di, __u64 hash)
2997 struct osd_it_iam *it = (struct osd_it_iam *)di;
2999 return iam_it_load(&it->oi_it, hash);
3002 static const struct dt_index_operations osd_index_iam_ops = {
3003 .dio_lookup = osd_index_iam_lookup,
3004 .dio_insert = osd_index_iam_insert,
3005 .dio_delete = osd_index_iam_delete,
3007 .init = osd_it_iam_init,
3008 .fini = osd_it_iam_fini,
3009 .get = osd_it_iam_get,
3010 .put = osd_it_iam_put,
3011 .next = osd_it_iam_next,
3012 .key = osd_it_iam_key,
3013 .key_size = osd_it_iam_key_size,
3014 .rec = osd_it_iam_rec,
3015 .store = osd_it_iam_store,
3016 .load = osd_it_iam_load
3021 * Creates or initializes iterator context.
3023 * \retval struct osd_it_ea, iterator structure on success
3026 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3027 struct dt_object *dt,
3028 struct lustre_capa *capa)
3030 struct osd_object *obj = osd_dt_obj(dt);
3031 struct osd_thread_info *info = osd_oti_get(env);
3032 struct osd_it_ea *it = &info->oti_it_ea;
3033 struct lu_object *lo = &dt->do_lu;
3034 struct dentry *obj_dentry = &info->oti_it_dentry;
3036 LASSERT(lu_object_exists(lo));
3038 obj_dentry->d_inode = obj->oo_inode;
3039 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3040 obj_dentry->d_name.hash = 0;
3042 it->oie_namelen = 0;
3043 it->oie_curr_pos = 0;
3044 it->oie_next_pos = 0;
3046 it->oie_file.f_dentry = obj_dentry;
3047 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3048 it->oie_file.f_op = obj->oo_inode->i_fop;
3049 it->oie_file.private_data = NULL;
3051 RETURN((struct dt_it*) it);
3055 * Destroy or finishes iterator context.
3057 * \param di, struct osd_it_ea, iterator structure to be destroyed
3059 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3061 struct osd_it_ea *it = (struct osd_it_ea *)di;
3062 struct osd_object *obj = it->oie_obj;
3063 struct inode *inode = obj->oo_inode;
3066 it->oie_file.f_op->release(inode, &it->oie_file);
3067 lu_object_put(env, &obj->oo_dt.do_lu);
3072 * It position the iterator at given key, so that next lookup continues from
3073 * that key Or it is similar to dio_it->load() but based on a key,
3074 * rather than file position.
3076 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3079 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3081 static int osd_it_ea_get(const struct lu_env *env,
3082 struct dt_it *di, const struct dt_key *key)
3084 struct osd_it_ea *it = (struct osd_it_ea *)di;
3087 LASSERT(((const char *)key)[0] == '\0');
3088 it->oie_namelen = 0;
3089 it->oie_curr_pos = 0;
3090 it->oie_next_pos = 0;
3098 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3103 * It is called internally by ->readdir(). It fills the
3104 * iterator's in-memory data structure with required
3105 * information i.e. name, namelen, rec_size etc.
3107 * \param buf, in which information to be filled in.
3108 * \param name, name of the file in given dir
3110 * \retval 0, on success
3111 * \retval 1, on buffer full
3113 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3114 loff_t offset, ino_t ino,
3115 unsigned int d_type)
3117 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3118 struct dirent64 *dirent = &it->oie_dirent64;
3121 if (it->oie_namelen)
3124 if (namelen == 0 || namelen > LDISKFS_NAME_LEN)
3127 strncpy(dirent->d_name, name, LDISKFS_NAME_LEN);
3128 dirent->d_name[namelen] = 0;
3129 dirent->d_ino = ino;
3130 it->oie_namelen = namelen;
3131 it->oie_curr_pos = offset;
3137 * Calls ->readdir() to load a directory entry at a time
3138 * and stored it in iterator's in-memory data structure.
3140 * \param di, struct osd_it_ea, iterator's in memory structure
3142 * \retval 0, on success
3143 * \retval -ve, on error
3145 int osd_ldiskfs_it_fill(const struct dt_it *di)
3147 struct osd_it_ea *it = (struct osd_it_ea *)di;
3148 struct osd_object *obj = it->oie_obj;
3149 struct inode *inode = obj->oo_inode;
3153 it->oie_namelen = 0;
3154 it->oie_file.f_pos = it->oie_curr_pos;
3156 down_read(&obj->oo_ext_idx_sem);
3157 result = inode->i_fop->readdir(&it->oie_file, it,
3158 (filldir_t) osd_ldiskfs_filldir);
3160 up_read(&obj->oo_ext_idx_sem);
3161 it->oie_next_pos = it->oie_file.f_pos;
3163 if (it->oie_namelen == 0)
3170 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3171 * to load a directory entry at a time and stored it in
3172 * iterator's in-memory data structure.
3174 * \param di, struct osd_it_ea, iterator's in memory structure
3176 * \retval +ve, iterator reached to end
3177 * \retval 0, iterator not reached to end
3178 * \retval -ve, on error
3180 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3182 struct osd_it_ea *it = (struct osd_it_ea *)di;
3186 it->oie_curr_pos = it->oie_next_pos;
3188 if (it->oie_curr_pos == LDISKFS_HTREE_EOF)
3191 rc = osd_ldiskfs_it_fill(di);
3197 * Returns the key at current position from iterator's in memory structure.
3199 * \param di, struct osd_it_ea, iterator's in memory structure
3201 * \retval key i.e. struct dt_key on success
3203 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3204 const struct dt_it *di)
3206 struct osd_it_ea *it = (struct osd_it_ea *)di;
3208 RETURN((struct dt_key *)it->oie_dirent64.d_name);
3212 * Returns the key's size at current position from iterator's in memory structure.
3214 * \param di, struct osd_it_ea, iterator's in memory structure
3216 * \retval key_size i.e. struct dt_key on success
3218 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3220 struct osd_it_ea *it = (struct osd_it_ea *)di;
3222 RETURN(it->oie_namelen);
3226 * Returns the value (i.e. fid/igif) at current position from iterator's
3227 * in memory structure.
3229 * \param di, struct osd_it_ea, iterator's in memory structure
3231 * \retval value i.e. struct dt_rec on success
3233 static struct dt_rec *osd_it_ea_rec(const struct lu_env *env,
3234 const struct dt_it *di)
3236 struct osd_it_ea *it = (struct osd_it_ea *)di;
3237 struct osd_object *obj = it->oie_obj;
3238 struct osd_thread_info *info = osd_oti_get(env);
3239 struct osd_inode_id *id = &info->oti_id;
3240 struct lu_fid_pack *rec = &info->oti_pack;
3241 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3242 struct dentry *dentry = &info->oti_child_dentry;
3243 struct osd_device *dev;
3244 struct inode *inode;
3248 dev = osd_dev(ldev);
3249 id->oii_ino = it->oie_dirent64.d_ino;
3250 id->oii_gen = OSD_OII_NOGEN;
3251 inode = osd_iget(info, dev, id);
3252 if (!IS_ERR(inode)) {
3253 dentry->d_inode = inode;
3254 LASSERT(dentry->d_inode->i_sb == osd_sb(dev));
3256 CERROR("Error getting inode for ino =%d", id->oii_ino);
3257 RETURN((struct dt_rec *) PTR_ERR(inode));
3260 rc = osd_ea_fid_get(env, dentry, (struct dt_rec*) rec);
3265 RETURN((struct dt_rec *)rec);
3270 * Returns a cookie for current position of the iterator head, so that
3271 * user can use this cookie to load/start the iterator next time.
3273 * \param di, struct osd_it_ea, iterator's in memory structure
3275 * \retval cookie for current position, on success
3277 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3279 struct osd_it_ea *it = (struct osd_it_ea *)di;
3281 RETURN(it->oie_curr_pos);
3285 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3286 * to load a directory entry at a time and stored it i inn,
3287 * in iterator's in-memory data structure.
3289 * \param di, struct osd_it_ea, iterator's in memory structure
3291 * \retval +ve, on success
3292 * \retval -ve, on error
3294 static int osd_it_ea_load(const struct lu_env *env,
3295 const struct dt_it *di, __u64 hash)
3297 struct osd_it_ea *it = (struct osd_it_ea *)di;
3301 it->oie_curr_pos = hash;
3303 rc = osd_ldiskfs_it_fill(di);
3310 * Index and Iterator operations for interoperability
3311 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3313 static const struct dt_index_operations osd_index_ea_ops = {
3314 .dio_lookup = osd_index_ea_lookup,
3315 .dio_insert = osd_index_ea_insert,
3316 .dio_delete = osd_index_ea_delete,
3318 .init = osd_it_ea_init,
3319 .fini = osd_it_ea_fini,
3320 .get = osd_it_ea_get,
3321 .put = osd_it_ea_put,
3322 .next = osd_it_ea_next,
3323 .key = osd_it_ea_key,
3324 .key_size = osd_it_ea_key_size,
3325 .rec = osd_it_ea_rec,
3326 .store = osd_it_ea_store,
3327 .load = osd_it_ea_load
3332 * Index lookup function for interoperability mode (b11826).
3334 * \param key, key i.e. file name to be searched
3336 * \retval +ve, on success
3337 * \retval -ve, on error
3339 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3340 struct dt_rec *rec, const struct dt_key *key,
3341 struct lustre_capa *capa)
3343 struct osd_object *obj = osd_dt_obj(dt);
3348 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3349 LINVRNT(osd_invariant(obj));
3351 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3354 rc = osd_ea_lookup_rec(env, obj, rec, key);
3361 /* type constructor/destructor: osd_type_init, osd_type_fini */
3362 LU_TYPE_INIT_FINI(osd, &osd_key);
3364 static struct lu_context_key osd_key = {
3365 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3366 .lct_init = osd_key_init,
3367 .lct_fini = osd_key_fini,
3368 .lct_exit = osd_key_exit
3371 static void *osd_key_init(const struct lu_context *ctx,
3372 struct lu_context_key *key)
3374 struct osd_thread_info *info;
3376 OBD_ALLOC_PTR(info);
3378 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
3380 info = ERR_PTR(-ENOMEM);
3384 /* context key destructor: osd_key_fini */
3385 LU_KEY_FINI(osd, struct osd_thread_info);
3387 static void osd_key_exit(const struct lu_context *ctx,
3388 struct lu_context_key *key, void *data)
3390 struct osd_thread_info *info = data;
3392 LASSERT(info->oti_r_locks == 0);
3393 LASSERT(info->oti_w_locks == 0);
3394 LASSERT(info->oti_txns == 0);
3397 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3398 const char *name, struct lu_device *next)
3401 struct lu_context *ctx;
3403 /* context for commit hooks */
3404 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3405 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3407 rc = osd_procfs_init(osd_dev(d), name);
3408 ctx->lc_cookie = 0x3;
3413 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3415 struct osd_thread_info *info = osd_oti_get(env);
3417 if (o->od_obj_area != NULL) {
3418 lu_object_put(env, &o->od_obj_area->do_lu);
3419 o->od_obj_area = NULL;
3421 osd_oi_fini(info, &o->od_oi);
3426 static int osd_mount(const struct lu_env *env,
3427 struct osd_device *o, struct lustre_cfg *cfg)
3429 struct lustre_mount_info *lmi;
3430 const char *dev = lustre_cfg_string(cfg, 0);
3431 struct lustre_disk_data *ldd;
3432 struct lustre_sb_info *lsi;
3436 if (o->od_mount != NULL) {
3437 CERROR("Already mounted (%s)\n", dev);
3442 lmi = server_get_mount(dev);
3444 CERROR("Cannot get mount info for %s!\n", dev);
3448 LASSERT(lmi != NULL);
3449 /* save lustre_mount_info in dt_device */
3452 lsi = s2lsi(lmi->lmi_sb);
3455 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3457 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3461 o->od_obj_area = NULL;
3465 static struct lu_device *osd_device_fini(const struct lu_env *env,
3466 struct lu_device *d)
3471 shrink_dcache_sb(osd_sb(osd_dev(d)));
3472 osd_sync(env, lu2dt_dev(d));
3474 rc = osd_procfs_fini(osd_dev(d));
3476 CERROR("proc fini error %d \n", rc);
3477 RETURN (ERR_PTR(rc));
3480 if (osd_dev(d)->od_mount)
3481 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3482 osd_dev(d)->od_mount->lmi_mnt);
3483 osd_dev(d)->od_mount = NULL;
3485 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3489 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3490 struct lu_device_type *t,
3491 struct lustre_cfg *cfg)
3493 struct lu_device *l;
3494 struct osd_device *o;
3500 result = dt_device_init(&o->od_dt_dev, t);
3503 l->ld_ops = &osd_lu_ops;
3504 o->od_dt_dev.dd_ops = &osd_dt_ops;
3505 spin_lock_init(&o->od_osfs_lock);
3506 o->od_osfs_age = cfs_time_shift_64(-1000);
3507 o->od_capa_hash = init_capa_hash();
3508 if (o->od_capa_hash == NULL) {
3509 dt_device_fini(&o->od_dt_dev);
3510 l = ERR_PTR(-ENOMEM);
3513 l = ERR_PTR(result);
3518 l = ERR_PTR(-ENOMEM);
3522 static struct lu_device *osd_device_free(const struct lu_env *env,
3523 struct lu_device *d)
3525 struct osd_device *o = osd_dev(d);
3528 cleanup_capa_hash(o->od_capa_hash);
3529 dt_device_fini(&o->od_dt_dev);
3534 static int osd_process_config(const struct lu_env *env,
3535 struct lu_device *d, struct lustre_cfg *cfg)
3537 struct osd_device *o = osd_dev(d);
3541 switch(cfg->lcfg_command) {
3543 err = osd_mount(env, o, cfg);
3546 err = osd_shutdown(env, o);
3555 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
3556 struct ldiskfs_super_block * es);
3558 static int osd_recovery_complete(const struct lu_env *env,
3559 struct lu_device *d)
3561 struct osd_device *o = osd_dev(d);
3563 /* TODO: orphans handling */
3564 ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3568 static int osd_prepare(const struct lu_env *env,
3569 struct lu_device *pdev,
3570 struct lu_device *dev)
3572 struct osd_device *osd = osd_dev(dev);
3573 struct lustre_sb_info *lsi;
3574 struct lustre_disk_data *ldd;
3575 struct lustre_mount_info *lmi;
3576 struct osd_thread_info *oti = osd_oti_get(env);
3577 struct dt_object *d;
3581 /* 1. initialize oi before any file create or file open */
3582 result = osd_oi_init(oti, &osd->od_oi,
3583 &osd->od_dt_dev, lu2md_dev(pdev));
3587 lmi = osd->od_mount;
3588 lsi = s2lsi(lmi->lmi_sb);
3591 /* 2. setup local objects */
3592 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3596 /* 3. open remote object dir */
3597 d = dt_store_open(env, lu2dt_dev(dev), "",
3598 remote_obj_dir, &oti->oti_fid);
3600 osd->od_obj_area = d;
3603 result = PTR_ERR(d);
3604 osd->od_obj_area = NULL;
3611 static struct inode *osd_iget(struct osd_thread_info *info,
3612 struct osd_device *dev,
3613 const struct osd_inode_id *id)
3615 struct inode *inode;
3617 inode = iget(osd_sb(dev), id->oii_ino);
3618 if (inode == NULL) {
3619 CERROR("no inode\n");
3620 inode = ERR_PTR(-EACCES);
3621 } else if (is_bad_inode(inode)) {
3622 CERROR("bad inode\n");
3624 inode = ERR_PTR(-ENOENT);
3625 } else if (id->oii_gen != OSD_OII_NOGEN &&
3626 inode->i_generation != id->oii_gen) {
3627 CERROR("stale inode\n");
3629 inode = ERR_PTR(-ESTALE);
3636 static int osd_fid_lookup(const struct lu_env *env,
3637 struct osd_object *obj, const struct lu_fid *fid)
3639 struct osd_thread_info *info;
3640 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3641 struct osd_device *dev;
3642 struct osd_inode_id *id;
3644 struct inode *inode;
3647 LINVRNT(osd_invariant(obj));
3648 LASSERT(obj->oo_inode == NULL);
3649 LASSERT(fid_is_sane(fid));
3651 * This assertion checks that osd layer sees only local
3652 * fids. Unfortunately it is somewhat expensive (does a
3653 * cache-lookup). Disabling it for production/acceptance-testing.
3655 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
3659 info = osd_oti_get(env);
3660 dev = osd_dev(ldev);
3664 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
3667 result = osd_oi_lookup(info, oi, fid, id);
3669 inode = osd_iget(info, dev, id);
3670 if (!IS_ERR(inode)) {
3671 obj->oo_inode = inode;
3672 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
3673 if (dev->od_iop_mode) {
3674 obj->oo_compat_dot_created = 1;
3675 obj->oo_compat_dotdot_created = 1;
3680 * If fid wasn't found in oi, inode-less object is
3681 * created, for which lu_object_exists() returns
3682 * false. This is used in a (frequent) case when
3683 * objects are created as locking anchors or
3684 * place holders for objects yet to be created.
3686 result = PTR_ERR(inode);
3687 } else if (result == -ENOENT)
3689 LINVRNT(osd_invariant(obj));
3694 static void osd_inode_getattr(const struct lu_env *env,
3695 struct inode *inode, struct lu_attr *attr)
3697 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
3698 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
3699 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
3701 attr->la_atime = LTIME_S(inode->i_atime);
3702 attr->la_mtime = LTIME_S(inode->i_mtime);
3703 attr->la_ctime = LTIME_S(inode->i_ctime);
3704 attr->la_mode = inode->i_mode;
3705 attr->la_size = i_size_read(inode);
3706 attr->la_blocks = inode->i_blocks;
3707 attr->la_uid = inode->i_uid;
3708 attr->la_gid = inode->i_gid;
3709 attr->la_flags = LDISKFS_I(inode)->i_flags;
3710 attr->la_nlink = inode->i_nlink;
3711 attr->la_rdev = inode->i_rdev;
3712 attr->la_blksize = ll_inode_blksize(inode);
3713 attr->la_blkbits = inode->i_blkbits;
3720 static int lu_device_is_osd(const struct lu_device *d)
3722 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
3725 static struct osd_object *osd_obj(const struct lu_object *o)
3727 LASSERT(lu_device_is_osd(o->lo_dev));
3728 return container_of0(o, struct osd_object, oo_dt.do_lu);
3731 static struct osd_device *osd_dt_dev(const struct dt_device *d)
3733 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
3734 return container_of0(d, struct osd_device, od_dt_dev);
3737 static struct osd_device *osd_dev(const struct lu_device *d)
3739 LASSERT(lu_device_is_osd(d));
3740 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
3743 static struct osd_object *osd_dt_obj(const struct dt_object *d)
3745 return osd_obj(&d->do_lu);
3748 static struct osd_device *osd_obj2dev(const struct osd_object *o)
3750 return osd_dev(o->oo_dt.do_lu.lo_dev);
3753 static struct lu_device *osd2lu_dev(struct osd_device *osd)
3755 return &osd->od_dt_dev.dd_lu_dev;
3758 static struct super_block *osd_sb(const struct osd_device *dev)
3760 return dev->od_mount->lmi_mnt->mnt_sb;
3763 static journal_t *osd_journal(const struct osd_device *dev)
3765 return LDISKFS_SB(osd_sb(dev))->s_journal;
3768 static int osd_has_index(const struct osd_object *obj)
3770 return obj->oo_dt.do_index_ops != NULL;
3773 static int osd_object_invariant(const struct lu_object *l)
3775 return osd_invariant(osd_obj(l));
3778 static const struct lu_object_operations osd_lu_obj_ops = {
3779 .loo_object_init = osd_object_init,
3780 .loo_object_delete = osd_object_delete,
3781 .loo_object_release = osd_object_release,
3782 .loo_object_free = osd_object_free,
3783 .loo_object_print = osd_object_print,
3784 .loo_object_invariant = osd_object_invariant
3787 static const struct lu_device_operations osd_lu_ops = {
3788 .ldo_object_alloc = osd_object_alloc,
3789 .ldo_process_config = osd_process_config,
3790 .ldo_recovery_complete = osd_recovery_complete,
3791 .ldo_prepare = osd_prepare,
3794 static const struct lu_device_type_operations osd_device_type_ops = {
3795 .ldto_init = osd_type_init,
3796 .ldto_fini = osd_type_fini,
3798 .ldto_start = osd_type_start,
3799 .ldto_stop = osd_type_stop,
3801 .ldto_device_alloc = osd_device_alloc,
3802 .ldto_device_free = osd_device_free,
3804 .ldto_device_init = osd_device_init,
3805 .ldto_device_fini = osd_device_fini
3808 static struct lu_device_type osd_device_type = {
3809 .ldt_tags = LU_DEVICE_DT,
3810 .ldt_name = LUSTRE_OSD_NAME,
3811 .ldt_ops = &osd_device_type_ops,
3812 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3816 * lprocfs legacy support.
3818 static struct obd_ops osd_obd_device_ops = {
3819 .o_owner = THIS_MODULE
3822 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3823 .llod_name = remote_obj_dir,
3824 .llod_oid = OSD_REM_OBJ_DIR_OID,
3826 .llod_feat = &dt_directory_features,
3829 static int __init osd_mod_init(void)
3831 struct lprocfs_static_vars lvars;
3834 llo_local_obj_register(&llod_osd_rem_obj_dir);
3835 lprocfs_osd_init_vars(&lvars);
3836 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3837 LUSTRE_OSD_NAME, &osd_device_type);
3840 static void __exit osd_mod_exit(void)
3842 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3843 class_unregister_type(LUSTRE_OSD_NAME);
3846 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3847 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3848 MODULE_LICENSE("GPL");
3850 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);