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);
2082 * Get the 64-bit version for an inode.
2084 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2085 struct dt_object *dt)
2087 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2089 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2090 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2091 return LDISKFS_I(inode)->i_fs_version;
2095 * Set the 64-bit version and return the old version.
2097 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2098 dt_obj_version_t new_version)
2100 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2102 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2103 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2104 LDISKFS_I(inode)->i_fs_version = new_version;
2105 /** Version is set after all inode operations are finished,
2106 * so we should mark it dirty here */
2107 inode->i_sb->s_op->dirty_inode(inode);
2110 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2113 struct osd_object *obj = osd_dt_obj(dt);
2116 *data = (void *)obj->oo_inode;
2120 static const struct dt_object_operations osd_obj_ops = {
2121 .do_read_lock = osd_object_read_lock,
2122 .do_write_lock = osd_object_write_lock,
2123 .do_read_unlock = osd_object_read_unlock,
2124 .do_write_unlock = osd_object_write_unlock,
2125 .do_attr_get = osd_attr_get,
2126 .do_attr_set = osd_attr_set,
2127 .do_ah_init = osd_ah_init,
2128 .do_create = osd_object_create,
2129 .do_index_try = osd_index_try,
2130 .do_ref_add = osd_object_ref_add,
2131 .do_ref_del = osd_object_ref_del,
2132 .do_xattr_get = osd_xattr_get,
2133 .do_xattr_set = osd_xattr_set,
2134 .do_xattr_del = osd_xattr_del,
2135 .do_xattr_list = osd_xattr_list,
2136 .do_capa_get = osd_capa_get,
2137 .do_object_sync = osd_object_sync,
2138 .do_version_get = osd_object_version_get,
2139 .do_version_set = osd_object_version_set,
2140 .do_data_get = osd_data_get,
2144 * dt_object_operations for interoperability mode
2145 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2147 static const struct dt_object_operations osd_obj_ea_ops = {
2148 .do_read_lock = osd_object_read_lock,
2149 .do_write_lock = osd_object_write_lock,
2150 .do_read_unlock = osd_object_read_unlock,
2151 .do_write_unlock = osd_object_write_unlock,
2152 .do_attr_get = osd_attr_get,
2153 .do_attr_set = osd_attr_set,
2154 .do_ah_init = osd_ah_init,
2155 .do_create = osd_object_ea_create,
2156 .do_index_try = osd_index_try,
2157 .do_ref_add = osd_object_ref_add,
2158 .do_ref_del = osd_object_ref_del,
2159 .do_xattr_get = osd_xattr_get,
2160 .do_xattr_set = osd_xattr_set,
2161 .do_xattr_del = osd_xattr_del,
2162 .do_xattr_list = osd_xattr_list,
2163 .do_capa_get = osd_capa_get,
2164 .do_object_sync = osd_object_sync,
2165 .do_version_get = osd_object_version_get,
2166 .do_version_set = osd_object_version_set,
2167 .do_data_get = osd_data_get,
2175 * XXX: Another layering violation for now.
2177 * We don't want to use ->f_op->read methods, because generic file write
2179 * - serializes on ->i_sem, and
2181 * - does a lot of extra work like balance_dirty_pages(),
2183 * which doesn't work for globally shared files like /last-received.
2185 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2186 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2187 loff_t *offs, handle_t *handle);
2189 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2190 struct lu_buf *buf, loff_t *pos,
2191 struct lustre_capa *capa)
2193 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2195 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2198 return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2201 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2202 const struct lu_buf *buf, loff_t *pos,
2203 struct thandle *handle, struct lustre_capa *capa,
2206 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2207 struct osd_thandle *oh;
2209 #ifdef HAVE_QUOTA_SUPPORT
2210 cfs_cap_t save = current->cap_effective;
2213 LASSERT(handle != NULL);
2215 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2218 oh = container_of(handle, struct osd_thandle, ot_super);
2219 LASSERT(oh->ot_handle->h_transaction != NULL);
2220 #ifdef HAVE_QUOTA_SUPPORT
2222 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2224 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2226 result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2227 pos, oh->ot_handle);
2228 #ifdef HAVE_QUOTA_SUPPORT
2229 current->cap_effective = save;
2232 result = buf->lb_len;
2236 static const struct dt_body_operations osd_body_ops = {
2237 .dbo_read = osd_read,
2238 .dbo_write = osd_write
2245 static int osd_object_is_root(const struct osd_object *obj)
2247 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
2250 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2251 const struct dt_index_features *feat)
2253 struct iam_descr *descr;
2254 struct dt_object *dt = &o->oo_dt;
2256 if (osd_object_is_root(o))
2257 return feat == &dt_directory_features;
2259 LASSERT(o->oo_dir != NULL);
2261 descr = o->oo_dir->od_container.ic_descr;
2262 if (feat == &dt_directory_features) {
2263 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2266 if (descr == &iam_htree_compat_param) {
2267 /* if it is a HTREE dir then there is good chance that,
2268 * we dealing with ext3 directory here with no FIDs. */
2270 if (descr->id_rec_size ==
2271 sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2273 dt->do_index_ops = &osd_index_ea_ops;
2280 feat->dif_keysize_min <= descr->id_key_size &&
2281 descr->id_key_size <= feat->dif_keysize_max &&
2282 feat->dif_recsize_min <= descr->id_rec_size &&
2283 descr->id_rec_size <= feat->dif_recsize_max &&
2284 !(feat->dif_flags & (DT_IND_VARKEY |
2285 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2286 ergo(feat->dif_flags & DT_IND_UPDATE,
2287 1 /* XXX check that object (and file system) is
2292 static int osd_iam_container_init(const struct lu_env *env,
2293 struct osd_object *obj,
2294 struct osd_directory *dir)
2297 struct iam_container *bag;
2299 bag = &dir->od_container;
2300 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2302 result = iam_container_setup(bag);
2304 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2306 iam_container_fini(bag);
2312 * Concurrency: no external locking is necessary.
2314 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2315 const struct dt_index_features *feat)
2319 struct osd_object *obj = osd_dt_obj(dt);
2320 struct osd_device *osd = osd_obj2dev(obj);
2322 LINVRNT(osd_invariant(obj));
2323 LASSERT(dt_object_exists(dt));
2325 if (osd_object_is_root(obj)) {
2326 dt->do_index_ops = &osd_index_ea_ops;
2328 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2329 dt->do_index_ops = &osd_index_ea_ops;
2330 if (S_ISDIR(obj->oo_inode->i_mode))
2335 } else if (!osd_has_index(obj)) {
2336 struct osd_directory *dir;
2341 spin_lock(&obj->oo_guard);
2342 if (obj->oo_dir == NULL)
2346 * Concurrent thread allocated container data.
2349 spin_unlock(&obj->oo_guard);
2351 * Now, that we have container data, serialize its
2354 down_write(&obj->oo_ext_idx_sem);
2356 * recheck under lock.
2358 if (!osd_has_index(obj))
2359 result = osd_iam_container_init(env, obj, dir);
2362 up_write(&obj->oo_ext_idx_sem);
2368 if (result == 0 && ea_dir == 0) {
2369 if (!osd_iam_index_probe(env, obj, feat))
2372 LINVRNT(osd_invariant(obj));
2378 * delete a (key, value) pair from index \a dt specified by \a key
2380 * \param dt_object osd index object
2381 * \param key key for index
2382 * \param rec record reference
2383 * \param handle transaction handler
2386 * \retval -ve failure
2389 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2390 const struct dt_key *key, struct thandle *handle,
2391 struct lustre_capa *capa)
2393 struct osd_object *obj = osd_dt_obj(dt);
2394 struct osd_thandle *oh;
2395 struct iam_path_descr *ipd;
2396 struct iam_container *bag = &obj->oo_dir->od_container;
2401 LINVRNT(osd_invariant(obj));
2402 LASSERT(dt_object_exists(dt));
2403 LASSERT(bag->ic_object == obj->oo_inode);
2404 LASSERT(handle != NULL);
2406 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2409 ipd = osd_idx_ipd_get(env, bag);
2410 if (unlikely(ipd == NULL))
2413 oh = container_of0(handle, struct osd_thandle, ot_super);
2414 LASSERT(oh->ot_handle != NULL);
2415 LASSERT(oh->ot_handle->h_transaction != NULL);
2417 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2418 osd_ipd_put(env, bag, ipd);
2419 LINVRNT(osd_invariant(obj));
2424 * Index delete function for interoperability mode (b11826).
2425 * It will remove the directory entry added by osd_index_ea_insert().
2426 * This entry is needed to maintain name->fid mapping.
2428 * \param key, key i.e. file entry to be deleted
2430 * \retval 0, on success
2431 * \retval -ve, on error
2433 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2434 const struct dt_key *key, struct thandle *handle,
2435 struct lustre_capa *capa)
2437 struct osd_object *obj = osd_dt_obj(dt);
2438 struct inode *dir = obj->oo_inode;
2439 struct dentry *dentry;
2440 struct osd_thandle *oh;
2441 struct ldiskfs_dir_entry_2 *de;
2442 struct buffer_head *bh;
2448 LINVRNT(osd_invariant(obj));
2449 LASSERT(dt_object_exists(dt));
2450 LASSERT(handle != NULL);
2452 oh = container_of(handle, struct osd_thandle, ot_super);
2453 LASSERT(oh->ot_handle != NULL);
2454 LASSERT(oh->ot_handle->h_transaction != NULL);
2456 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2459 dentry = osd_child_dentry_get(env, obj,
2460 (char *)key, strlen((char *)key));
2462 down_write(&obj->oo_ext_idx_sem);
2463 bh = ldiskfs_find_entry(dentry, &de);
2465 struct osd_thread_info *oti = osd_oti_get(env);
2466 struct timespec *ctime = &oti->oti_time;
2467 struct timespec *mtime = &oti->oti_time2;
2469 *ctime = dir->i_ctime;
2470 *mtime = dir->i_mtime;
2471 rc = ldiskfs_delete_entry(oh->ot_handle,
2473 /* xtime should not be updated with server-side time. */
2474 spin_lock(&obj->oo_guard);
2475 dir->i_ctime = *ctime;
2476 dir->i_mtime = *mtime;
2477 spin_unlock(&obj->oo_guard);
2478 mark_inode_dirty(dir);
2483 up_write(&obj->oo_ext_idx_sem);
2484 LASSERT(osd_invariant(obj));
2489 * Lookup index for \a key and copy record to \a rec.
2491 * \param dt_object osd index object
2492 * \param key key for index
2493 * \param rec record reference
2495 * \retval +ve success : exact mach
2496 * \retval 0 return record with key not greater than \a key
2497 * \retval -ve failure
2499 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2500 struct dt_rec *rec, const struct dt_key *key,
2501 struct lustre_capa *capa)
2503 struct osd_object *obj = osd_dt_obj(dt);
2504 struct iam_path_descr *ipd;
2505 struct iam_container *bag = &obj->oo_dir->od_container;
2506 struct osd_thread_info *oti = osd_oti_get(env);
2507 struct iam_iterator *it = &oti->oti_idx_it;
2511 LASSERT(osd_invariant(obj));
2512 LASSERT(dt_object_exists(dt));
2513 LASSERT(bag->ic_object == obj->oo_inode);
2515 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2518 ipd = osd_idx_ipd_get(env, bag);
2522 /* got ipd now we can start iterator. */
2523 iam_it_init(it, bag, 0, ipd);
2525 rc = iam_it_get(it, (struct iam_key *)key);
2527 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2531 osd_ipd_put(env, bag, ipd);
2533 LINVRNT(osd_invariant(obj));
2539 * Inserts (key, value) pair in \a dt index object.
2541 * \param dt osd index object
2542 * \param key key for index
2543 * \param rec record reference
2544 * \param th transaction handler
2547 * \retval -ve failure
2549 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2550 const struct dt_rec *rec, const struct dt_key *key,
2551 struct thandle *th, struct lustre_capa *capa,
2554 struct osd_object *obj = osd_dt_obj(dt);
2555 struct iam_path_descr *ipd;
2556 struct osd_thandle *oh;
2557 struct iam_container *bag = &obj->oo_dir->od_container;
2558 #ifdef HAVE_QUOTA_SUPPORT
2559 cfs_cap_t save = current->cap_effective;
2565 LINVRNT(osd_invariant(obj));
2566 LASSERT(dt_object_exists(dt));
2567 LASSERT(bag->ic_object == obj->oo_inode);
2568 LASSERT(th != NULL);
2570 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2573 ipd = osd_idx_ipd_get(env, bag);
2574 if (unlikely(ipd == NULL))
2577 oh = container_of0(th, struct osd_thandle, ot_super);
2578 LASSERT(oh->ot_handle != NULL);
2579 LASSERT(oh->ot_handle->h_transaction != NULL);
2580 #ifdef HAVE_QUOTA_SUPPORT
2582 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2584 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2586 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2587 (struct iam_rec *)rec, ipd);
2588 #ifdef HAVE_QUOTA_SUPPORT
2589 current->cap_effective = save;
2591 osd_ipd_put(env, bag, ipd);
2592 LINVRNT(osd_invariant(obj));
2597 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2598 * into the directory.Also sets flags into osd object to
2599 * indicate dot and dotdot are created. This is required for
2600 * interoperability mode (b11826)
2602 * \param dir directory for dot and dotdot fixup.
2603 * \param obj child object for linking
2605 * \retval 0, on success
2606 * \retval -ve, on error
2608 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2609 struct osd_object *dir,
2610 struct osd_object *obj, const char *name,
2613 struct inode *parent_dir = obj->oo_inode;
2614 struct inode *inode = dir->oo_inode;
2615 struct osd_thandle *oth;
2618 oth = container_of(th, struct osd_thandle, ot_super);
2619 LASSERT(oth->ot_handle->h_transaction != NULL);
2620 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2622 if (strcmp(name, dot) == 0) {
2623 if (dir->oo_compat_dot_created) {
2626 LASSERT(obj == dir);
2627 dir->oo_compat_dot_created = 1;
2630 } else if(strcmp(name, dotdot) == 0) {
2631 if (!dir->oo_compat_dot_created)
2633 if (dir->oo_compat_dotdot_created)
2634 return __osd_ea_add_rec(info, dir, obj, name, th);
2636 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2638 dir->oo_compat_dotdot_created = 1;
2645 * Calls ldiskfs_add_entry() to add directory entry
2646 * into the directory. This is required for
2647 * interoperability mode (b11826)
2649 * \retval 0, on success
2650 * \retval -ve, on error
2652 static int __osd_ea_add_rec(struct osd_thread_info *info,
2653 struct osd_object *pobj,
2654 struct osd_object *cobj,
2658 struct dentry *child;
2659 struct osd_thandle *oth;
2660 struct inode *cinode = cobj->oo_inode;
2663 oth = container_of(th, struct osd_thandle, ot_super);
2664 LASSERT(oth->ot_handle != NULL);
2665 LASSERT(oth->ot_handle->h_transaction != NULL);
2667 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2668 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2674 * It will call the appropriate osd_add* function and return the
2675 * value, return by respective functions.
2677 static int osd_ea_add_rec(const struct lu_env *env,
2678 struct osd_object *pobj,
2679 struct osd_object *cobj,
2683 struct osd_thread_info *info = osd_oti_get(env);
2686 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2688 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2690 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2696 * Calls ->lookup() to find dentry. From dentry get inode and
2697 * read inode's ea to get fid. This is required for interoperability
2700 * \retval 0, on success
2701 * \retval -ve, on error
2703 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2704 struct dt_rec *rec, const struct dt_key *key)
2706 struct inode *dir = obj->oo_inode;
2707 struct osd_thread_info *info = osd_oti_get(env);
2708 struct dentry *dentry;
2709 struct osd_device *dev = osd_dev(obj->oo_dt.do_lu.lo_dev);
2710 struct osd_inode_id *id = &info->oti_id;
2711 struct ldiskfs_dir_entry_2 *de;
2712 struct buffer_head *bh;
2713 struct inode *inode;
2717 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2719 dentry = osd_child_dentry_get(env, obj,
2720 (char *)key, strlen((char *)key));
2722 down_read(&obj->oo_ext_idx_sem);
2723 bh = ldiskfs_find_entry(dentry, &de);
2725 ino = le32_to_cpu(de->inode);
2728 id->oii_gen = OSD_OII_NOGEN;
2730 inode = osd_iget(info, dev, id);
2731 if (!IS_ERR(inode)) {
2732 dentry->d_inode = inode;
2734 rc = osd_ea_fid_get(env, dentry, rec);
2737 rc = PTR_ERR(inode);
2741 up_read(&obj->oo_ext_idx_sem);
2746 * Find the osd object for given fid.
2748 * \param fid, need to find the osd object having this fid
2750 * \retval osd_object, on success
2751 * \retval -ve, on error
2753 struct osd_object *osd_object_find(const struct lu_env *env,
2754 struct dt_object *dt,
2755 const struct lu_fid *fid)
2757 struct lu_device *ludev = dt->do_lu.lo_dev;
2758 struct osd_object *child = NULL;
2759 struct lu_object *luch;
2760 struct lu_object *lo;
2762 luch = lu_object_find(env, ludev, fid, NULL);
2763 if (!IS_ERR(luch)) {
2764 if (lu_object_exists(luch)) {
2765 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2767 child = osd_obj(lo);
2769 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2770 "lu_object can't be located"
2771 ""DFID"\n", PFID(fid));
2773 if (child == NULL) {
2774 lu_object_put(env, luch);
2775 CERROR("Unable to get osd_object\n");
2776 child = ERR_PTR(-ENOENT);
2779 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2780 "lu_object does not exists "DFID"\n",
2782 child = ERR_PTR(-ENOENT);
2785 child = (void *)luch;
2791 * Put the osd object once done with it.
2793 * \param obj, osd object that needs to be put
2795 static inline void osd_object_put(const struct lu_env *env,
2796 struct osd_object *obj)
2798 lu_object_put(env, &obj->oo_dt.do_lu);
2802 * Index add function for interoperability mode (b11826).
2803 * It will add the directory entry.This entry is needed to
2804 * maintain name->fid mapping.
2806 * \param key, it is key i.e. file entry to be inserted
2807 * \param rec, it is value of given key i.e. fid
2809 * \retval 0, on success
2810 * \retval -ve, on error
2812 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2813 const struct dt_rec *rec,
2814 const struct dt_key *key, struct thandle *th,
2815 struct lustre_capa *capa, int ignore_quota)
2817 struct osd_object *obj = osd_dt_obj(dt);
2818 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
2819 const struct lu_fid_pack *pack = (const struct lu_fid_pack *)rec;
2820 const char *name = (const char *)key;
2821 struct osd_object *child;
2822 #ifdef HAVE_QUOTA_SUPPORT
2823 cfs_cap_t save = current->cap_effective;
2829 LASSERT(osd_invariant(obj));
2830 LASSERT(dt_object_exists(dt));
2831 LASSERT(th != NULL);
2833 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2836 rc = fid_unpack(pack, fid);
2839 child = osd_object_find(env, dt, fid);
2840 if (!IS_ERR(child)) {
2841 struct inode *inode = obj->oo_inode;
2842 struct osd_thread_info *oti = osd_oti_get(env);
2843 struct timespec *ctime = &oti->oti_time;
2844 struct timespec *mtime = &oti->oti_time2;
2846 *ctime = inode->i_ctime;
2847 *mtime = inode->i_mtime;
2848 #ifdef HAVE_QUOTA_SUPPORT
2850 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2852 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2854 down_write(&obj->oo_ext_idx_sem);
2855 rc = osd_ea_add_rec(env, obj, child, name, th);
2856 up_write(&obj->oo_ext_idx_sem);
2857 #ifdef HAVE_QUOTA_SUPPORT
2858 current->cap_effective = save;
2860 osd_object_put(env, child);
2861 /* xtime should not be updated with server-side time. */
2862 spin_lock(&obj->oo_guard);
2863 inode->i_ctime = *ctime;
2864 inode->i_mtime = *mtime;
2865 spin_unlock(&obj->oo_guard);
2866 mark_inode_dirty(inode);
2868 rc = PTR_ERR(child);
2871 LASSERT(osd_invariant(obj));
2876 * Initialize osd Iterator for given osd index object.
2878 * \param dt osd index object
2881 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2882 struct dt_object *dt,
2883 struct lustre_capa *capa)
2885 struct osd_it_iam *it;
2886 struct osd_thread_info *oti = osd_oti_get(env);
2887 struct osd_object *obj = osd_dt_obj(dt);
2888 struct lu_object *lo = &dt->do_lu;
2889 struct iam_path_descr *ipd;
2890 struct iam_container *bag = &obj->oo_dir->od_container;
2892 LASSERT(lu_object_exists(lo));
2894 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2895 return ERR_PTR(-EACCES);
2898 ipd = osd_it_ipd_get(env, bag);
2899 if (likely(ipd != NULL)) {
2903 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2904 return (struct dt_it *)it;
2906 return ERR_PTR(-ENOMEM);
2910 * free given Iterator.
2913 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2915 struct osd_it_iam *it = (struct osd_it_iam *)di;
2916 struct osd_object *obj = it->oi_obj;
2918 iam_it_fini(&it->oi_it);
2919 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2920 lu_object_put(env, &obj->oo_dt.do_lu);
2924 * Move Iterator to record specified by \a key
2926 * \param di osd iterator
2927 * \param key key for index
2929 * \retval +ve di points to record with least key not larger than key
2930 * \retval 0 di points to exact matched key
2931 * \retval -ve failure
2934 static int osd_it_iam_get(const struct lu_env *env,
2935 struct dt_it *di, const struct dt_key *key)
2937 struct osd_it_iam *it = (struct osd_it_iam *)di;
2939 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2945 * \param di osd iterator
2948 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2950 struct osd_it_iam *it = (struct osd_it_iam *)di;
2952 iam_it_put(&it->oi_it);
2956 * Move iterator by one record
2958 * \param di osd iterator
2960 * \retval +1 end of container reached
2962 * \retval -ve failure
2965 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2967 struct osd_it_iam *it = (struct osd_it_iam *)di;
2969 return iam_it_next(&it->oi_it);
2973 * Return pointer to the key under iterator.
2976 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
2977 const struct dt_it *di)
2979 struct osd_it_iam *it = (struct osd_it_iam *)di;
2981 return (struct dt_key *)iam_it_key_get(&it->oi_it);
2985 * Return size of key under iterator (in bytes)
2988 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
2990 struct osd_it_iam *it = (struct osd_it_iam *)di;
2992 return iam_it_key_size(&it->oi_it);
2996 * Return pointer to the record under iterator.
2998 static struct dt_rec *osd_it_iam_rec(const struct lu_env *env,
2999 const struct dt_it *di)
3001 struct osd_it_iam *it = (struct osd_it_iam *)di;
3003 return (struct dt_rec *)iam_it_rec_get(&it->oi_it);
3007 * Returns cookie for current Iterator position.
3009 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3011 struct osd_it_iam *it = (struct osd_it_iam *)di;
3013 return iam_it_store(&it->oi_it);
3017 * Restore iterator from cookie.
3019 * \param di osd iterator
3020 * \param hash Iterator location cookie
3022 * \retval +ve di points to record with least key not larger than key.
3023 * \retval 0 di points to exact matched key
3024 * \retval -ve failure
3027 static int osd_it_iam_load(const struct lu_env *env,
3028 const struct dt_it *di, __u64 hash)
3030 struct osd_it_iam *it = (struct osd_it_iam *)di;
3032 return iam_it_load(&it->oi_it, hash);
3035 static const struct dt_index_operations osd_index_iam_ops = {
3036 .dio_lookup = osd_index_iam_lookup,
3037 .dio_insert = osd_index_iam_insert,
3038 .dio_delete = osd_index_iam_delete,
3040 .init = osd_it_iam_init,
3041 .fini = osd_it_iam_fini,
3042 .get = osd_it_iam_get,
3043 .put = osd_it_iam_put,
3044 .next = osd_it_iam_next,
3045 .key = osd_it_iam_key,
3046 .key_size = osd_it_iam_key_size,
3047 .rec = osd_it_iam_rec,
3048 .store = osd_it_iam_store,
3049 .load = osd_it_iam_load
3054 * Creates or initializes iterator context.
3056 * \retval struct osd_it_ea, iterator structure on success
3059 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3060 struct dt_object *dt,
3061 struct lustre_capa *capa)
3063 struct osd_object *obj = osd_dt_obj(dt);
3064 struct osd_thread_info *info = osd_oti_get(env);
3065 struct osd_it_ea *it = &info->oti_it_ea;
3066 struct lu_object *lo = &dt->do_lu;
3067 struct dentry *obj_dentry = &info->oti_it_dentry;
3069 LASSERT(lu_object_exists(lo));
3071 obj_dentry->d_inode = obj->oo_inode;
3072 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3073 obj_dentry->d_name.hash = 0;
3075 it->oie_rd_dirent = 0;
3076 it->oie_it_dirent = 0;
3077 it->oie_curr_pos = 0;
3078 it->oie_next_pos = 0;
3079 it->oie_dirent = NULL;
3080 it->oie_buf = info->oti_it_ea_buf;
3082 it->oie_file.f_dentry = obj_dentry;
3083 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3084 it->oie_file.f_op = obj->oo_inode->i_fop;
3085 it->oie_file.private_data = NULL;
3087 RETURN((struct dt_it*) it);
3091 * Destroy or finishes iterator context.
3093 * \param di, struct osd_it_ea, iterator structure to be destroyed
3095 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3097 struct osd_it_ea *it = (struct osd_it_ea *)di;
3098 struct osd_object *obj = it->oie_obj;
3099 struct inode *inode = obj->oo_inode;
3102 it->oie_file.f_op->release(inode, &it->oie_file);
3103 lu_object_put(env, &obj->oo_dt.do_lu);
3108 * It position the iterator at given key, so that next lookup continues from
3109 * that key Or it is similar to dio_it->load() but based on a key,
3110 * rather than file position.
3112 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3115 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3117 static int osd_it_ea_get(const struct lu_env *env,
3118 struct dt_it *di, const struct dt_key *key)
3120 struct osd_it_ea *it = (struct osd_it_ea *)di;
3123 LASSERT(((const char *)key)[0] == '\0');
3124 it->oie_curr_pos = 0;
3125 it->oie_next_pos = 0;
3126 it->oie_rd_dirent = 0;
3127 it->oie_it_dirent = 0;
3128 it->oie_dirent = NULL;
3136 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3141 * It is called internally by ->readdir(). It fills the
3142 * iterator's in-memory data structure with required
3143 * information i.e. name, namelen, rec_size etc.
3145 * \param buf, in which information to be filled in.
3146 * \param name, name of the file in given dir
3148 * \retval 0, on success
3149 * \retval 1, on buffer full
3151 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3152 loff_t offset, ino_t ino,
3153 unsigned int d_type)
3155 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3156 struct osd_it_ea_dirent *ent = it->oie_dirent;
3159 /* this should never happen */
3160 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3161 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3165 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3169 ent->oied_ino = ino;
3170 ent->oied_off = offset;
3171 ent->oied_namelen = namelen;
3172 memcpy(ent->oied_name, name, namelen);
3174 it->oie_rd_dirent++;
3175 it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3180 * Calls ->readdir() to load a directory entry at a time
3181 * and stored it in iterator's in-memory data structure.
3183 * \param di, struct osd_it_ea, iterator's in memory structure
3185 * \retval 0, on success
3186 * \retval -ve, on error
3188 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3190 struct osd_it_ea *it = (struct osd_it_ea *)di;
3191 struct osd_object *obj = it->oie_obj;
3192 struct inode *inode = obj->oo_inode;
3196 it->oie_dirent = it->oie_buf;
3197 it->oie_rd_dirent = 0;
3198 it->oie_file.f_pos = it->oie_curr_pos;
3200 down_read(&obj->oo_ext_idx_sem);
3201 result = inode->i_fop->readdir(&it->oie_file, it,
3202 (filldir_t) osd_ldiskfs_filldir);
3204 up_read(&obj->oo_ext_idx_sem);
3205 it->oie_next_pos = it->oie_file.f_pos;
3207 if (it->oie_rd_dirent == 0) {
3210 it->oie_dirent = it->oie_buf;
3211 it->oie_it_dirent = 1;
3218 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3219 * to load a directory entry at a time and stored it in
3220 * iterator's in-memory data structure.
3222 * \param di, struct osd_it_ea, iterator's in memory structure
3224 * \retval +ve, iterator reached to end
3225 * \retval 0, iterator not reached to end
3226 * \retval -ve, on error
3228 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3230 struct osd_it_ea *it = (struct osd_it_ea *)di;
3235 if (it->oie_it_dirent < it->oie_rd_dirent) {
3236 it->oie_dirent = (void *) it->oie_dirent +
3237 size_round(sizeof(struct osd_it_ea_dirent) +
3238 it->oie_dirent->oied_namelen);
3239 it->oie_it_dirent++;
3242 it->oie_curr_pos = it->oie_next_pos;
3244 if (it->oie_curr_pos == LDISKFS_HTREE_EOF)
3247 rc = osd_ldiskfs_it_fill(di);
3254 * Returns the key at current position from iterator's in memory structure.
3256 * \param di, struct osd_it_ea, iterator's in memory structure
3258 * \retval key i.e. struct dt_key on success
3260 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3261 const struct dt_it *di)
3263 struct osd_it_ea *it = (struct osd_it_ea *)di;
3265 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3269 * Returns the key's size at current position from iterator's in memory structure.
3271 * \param di, struct osd_it_ea, iterator's in memory structure
3273 * \retval key_size i.e. struct dt_key on success
3275 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3277 struct osd_it_ea *it = (struct osd_it_ea *)di;
3279 RETURN(it->oie_dirent->oied_namelen);
3283 * Returns the value (i.e. fid/igif) at current position from iterator's
3284 * in memory structure.
3286 * \param di, struct osd_it_ea, iterator's in memory structure
3288 * \retval value i.e. struct dt_rec on success
3290 static struct dt_rec *osd_it_ea_rec(const struct lu_env *env,
3291 const struct dt_it *di)
3293 struct osd_it_ea *it = (struct osd_it_ea *)di;
3294 struct osd_object *obj = it->oie_obj;
3295 struct osd_thread_info *info = osd_oti_get(env);
3296 struct osd_inode_id *id = &info->oti_id;
3297 struct lu_fid_pack *rec = &info->oti_pack;
3298 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3299 struct dentry *dentry = &info->oti_child_dentry;
3300 struct osd_device *dev;
3301 struct inode *inode;
3305 dev = osd_dev(ldev);
3306 id->oii_ino = it->oie_dirent->oied_ino;
3307 id->oii_gen = OSD_OII_NOGEN;
3308 inode = osd_iget(info, dev, id);
3309 if (!IS_ERR(inode)) {
3310 dentry->d_inode = inode;
3311 LASSERT(dentry->d_inode->i_sb == osd_sb(dev));
3313 RETURN((struct dt_rec *) PTR_ERR(inode));
3316 rc = osd_ea_fid_get(env, dentry, (struct dt_rec*) rec);
3321 RETURN((struct dt_rec *)rec);
3326 * Returns a cookie for current position of the iterator head, so that
3327 * user can use this cookie to load/start the iterator next time.
3329 * \param di, struct osd_it_ea, iterator's in memory structure
3331 * \retval cookie for current position, on success
3333 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3335 struct osd_it_ea *it = (struct osd_it_ea *)di;
3337 RETURN(it->oie_dirent->oied_off);
3341 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3342 * to load a directory entry at a time and stored it i inn,
3343 * in iterator's in-memory data structure.
3345 * \param di, struct osd_it_ea, iterator's in memory structure
3347 * \retval +ve, on success
3348 * \retval -ve, on error
3350 static int osd_it_ea_load(const struct lu_env *env,
3351 const struct dt_it *di, __u64 hash)
3353 struct osd_it_ea *it = (struct osd_it_ea *)di;
3357 it->oie_curr_pos = hash;
3359 rc = osd_ldiskfs_it_fill(di);
3366 * Index and Iterator operations for interoperability
3367 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3369 static const struct dt_index_operations osd_index_ea_ops = {
3370 .dio_lookup = osd_index_ea_lookup,
3371 .dio_insert = osd_index_ea_insert,
3372 .dio_delete = osd_index_ea_delete,
3374 .init = osd_it_ea_init,
3375 .fini = osd_it_ea_fini,
3376 .get = osd_it_ea_get,
3377 .put = osd_it_ea_put,
3378 .next = osd_it_ea_next,
3379 .key = osd_it_ea_key,
3380 .key_size = osd_it_ea_key_size,
3381 .rec = osd_it_ea_rec,
3382 .store = osd_it_ea_store,
3383 .load = osd_it_ea_load
3388 * Index lookup function for interoperability mode (b11826).
3390 * \param key, key i.e. file name to be searched
3392 * \retval +ve, on success
3393 * \retval -ve, on error
3395 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3396 struct dt_rec *rec, const struct dt_key *key,
3397 struct lustre_capa *capa)
3399 struct osd_object *obj = osd_dt_obj(dt);
3404 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3405 LINVRNT(osd_invariant(obj));
3407 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3410 rc = osd_ea_lookup_rec(env, obj, rec, key);
3417 /* type constructor/destructor: osd_type_init, osd_type_fini */
3418 LU_TYPE_INIT_FINI(osd, &osd_key);
3420 static struct lu_context_key osd_key = {
3421 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3422 .lct_init = osd_key_init,
3423 .lct_fini = osd_key_fini,
3424 .lct_exit = osd_key_exit
3427 static void *osd_key_init(const struct lu_context *ctx,
3428 struct lu_context_key *key)
3430 struct osd_thread_info *info;
3432 OBD_ALLOC_PTR(info);
3434 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3435 if (info->oti_it_ea_buf != NULL) {
3436 info->oti_env = container_of(ctx, struct lu_env,
3440 info = ERR_PTR(-ENOMEM);
3443 info = ERR_PTR(-ENOMEM);
3448 static void osd_key_fini(const struct lu_context *ctx,
3449 struct lu_context_key *key, void* data)
3451 struct osd_thread_info *info = data;
3453 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3457 static void osd_key_exit(const struct lu_context *ctx,
3458 struct lu_context_key *key, void *data)
3460 struct osd_thread_info *info = data;
3462 LASSERT(info->oti_r_locks == 0);
3463 LASSERT(info->oti_w_locks == 0);
3464 LASSERT(info->oti_txns == 0);
3467 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3468 const char *name, struct lu_device *next)
3471 struct lu_context *ctx;
3473 /* context for commit hooks */
3474 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3475 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3477 rc = osd_procfs_init(osd_dev(d), name);
3478 ctx->lc_cookie = 0x3;
3483 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3485 struct osd_thread_info *info = osd_oti_get(env);
3487 if (o->od_obj_area != NULL) {
3488 lu_object_put(env, &o->od_obj_area->do_lu);
3489 o->od_obj_area = NULL;
3491 osd_oi_fini(info, &o->od_oi);
3496 static int osd_mount(const struct lu_env *env,
3497 struct osd_device *o, struct lustre_cfg *cfg)
3499 struct lustre_mount_info *lmi;
3500 const char *dev = lustre_cfg_string(cfg, 0);
3501 struct lustre_disk_data *ldd;
3502 struct lustre_sb_info *lsi;
3506 if (o->od_mount != NULL) {
3507 CERROR("Already mounted (%s)\n", dev);
3512 lmi = server_get_mount(dev);
3514 CERROR("Cannot get mount info for %s!\n", dev);
3518 LASSERT(lmi != NULL);
3519 /* save lustre_mount_info in dt_device */
3522 lsi = s2lsi(lmi->lmi_sb);
3525 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3527 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3531 o->od_obj_area = NULL;
3535 static struct lu_device *osd_device_fini(const struct lu_env *env,
3536 struct lu_device *d)
3541 shrink_dcache_sb(osd_sb(osd_dev(d)));
3542 osd_sync(env, lu2dt_dev(d));
3544 rc = osd_procfs_fini(osd_dev(d));
3546 CERROR("proc fini error %d \n", rc);
3547 RETURN (ERR_PTR(rc));
3550 if (osd_dev(d)->od_mount)
3551 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3552 osd_dev(d)->od_mount->lmi_mnt);
3553 osd_dev(d)->od_mount = NULL;
3555 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3559 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3560 struct lu_device_type *t,
3561 struct lustre_cfg *cfg)
3563 struct lu_device *l;
3564 struct osd_device *o;
3570 result = dt_device_init(&o->od_dt_dev, t);
3573 l->ld_ops = &osd_lu_ops;
3574 o->od_dt_dev.dd_ops = &osd_dt_ops;
3575 spin_lock_init(&o->od_osfs_lock);
3576 o->od_osfs_age = cfs_time_shift_64(-1000);
3577 o->od_capa_hash = init_capa_hash();
3578 if (o->od_capa_hash == NULL) {
3579 dt_device_fini(&o->od_dt_dev);
3580 l = ERR_PTR(-ENOMEM);
3583 l = ERR_PTR(result);
3588 l = ERR_PTR(-ENOMEM);
3592 static struct lu_device *osd_device_free(const struct lu_env *env,
3593 struct lu_device *d)
3595 struct osd_device *o = osd_dev(d);
3598 cleanup_capa_hash(o->od_capa_hash);
3599 dt_device_fini(&o->od_dt_dev);
3604 static int osd_process_config(const struct lu_env *env,
3605 struct lu_device *d, struct lustre_cfg *cfg)
3607 struct osd_device *o = osd_dev(d);
3611 switch(cfg->lcfg_command) {
3613 err = osd_mount(env, o, cfg);
3616 err = osd_shutdown(env, o);
3625 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
3626 struct ldiskfs_super_block * es);
3628 static int osd_recovery_complete(const struct lu_env *env,
3629 struct lu_device *d)
3631 struct osd_device *o = osd_dev(d);
3633 /* TODO: orphans handling */
3634 ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3638 static int osd_prepare(const struct lu_env *env,
3639 struct lu_device *pdev,
3640 struct lu_device *dev)
3642 struct osd_device *osd = osd_dev(dev);
3643 struct lustre_sb_info *lsi;
3644 struct lustre_disk_data *ldd;
3645 struct lustre_mount_info *lmi;
3646 struct osd_thread_info *oti = osd_oti_get(env);
3647 struct dt_object *d;
3651 /* 1. initialize oi before any file create or file open */
3652 result = osd_oi_init(oti, &osd->od_oi,
3653 &osd->od_dt_dev, lu2md_dev(pdev));
3657 lmi = osd->od_mount;
3658 lsi = s2lsi(lmi->lmi_sb);
3661 /* 2. setup local objects */
3662 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3666 /* 3. open remote object dir */
3667 d = dt_store_open(env, lu2dt_dev(dev), "",
3668 remote_obj_dir, &oti->oti_fid);
3670 osd->od_obj_area = d;
3673 result = PTR_ERR(d);
3674 osd->od_obj_area = NULL;
3681 static struct inode *osd_iget(struct osd_thread_info *info,
3682 struct osd_device *dev,
3683 const struct osd_inode_id *id)
3685 struct inode *inode;
3687 inode = iget(osd_sb(dev), id->oii_ino);
3688 if (inode == NULL) {
3689 CERROR("no inode\n");
3690 inode = ERR_PTR(-EACCES);
3691 } else if (is_bad_inode(inode)) {
3692 CERROR("bad inode\n");
3694 inode = ERR_PTR(-ENOENT);
3695 } else if (id->oii_gen != OSD_OII_NOGEN &&
3696 inode->i_generation != id->oii_gen) {
3697 CERROR("stale inode\n");
3699 inode = ERR_PTR(-ESTALE);
3700 } else if (inode->i_nlink == 0) {
3701 /* due to parallel readdir and unlink,
3702 * we can have dead inode here. */
3703 make_bad_inode(inode);
3705 inode = ERR_PTR(-ESTALE);
3712 static int osd_fid_lookup(const struct lu_env *env,
3713 struct osd_object *obj, const struct lu_fid *fid)
3715 struct osd_thread_info *info;
3716 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3717 struct osd_device *dev;
3718 struct osd_inode_id *id;
3720 struct inode *inode;
3723 LINVRNT(osd_invariant(obj));
3724 LASSERT(obj->oo_inode == NULL);
3725 LASSERT(fid_is_sane(fid));
3727 * This assertion checks that osd layer sees only local
3728 * fids. Unfortunately it is somewhat expensive (does a
3729 * cache-lookup). Disabling it for production/acceptance-testing.
3731 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
3735 info = osd_oti_get(env);
3736 dev = osd_dev(ldev);
3740 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
3743 result = osd_oi_lookup(info, oi, fid, id);
3745 inode = osd_iget(info, dev, id);
3746 if (!IS_ERR(inode)) {
3747 obj->oo_inode = inode;
3748 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
3749 if (dev->od_iop_mode) {
3750 obj->oo_compat_dot_created = 1;
3751 obj->oo_compat_dotdot_created = 1;
3756 * If fid wasn't found in oi, inode-less object is
3757 * created, for which lu_object_exists() returns
3758 * false. This is used in a (frequent) case when
3759 * objects are created as locking anchors or
3760 * place holders for objects yet to be created.
3762 result = PTR_ERR(inode);
3763 } else if (result == -ENOENT)
3765 LINVRNT(osd_invariant(obj));
3770 static void osd_inode_getattr(const struct lu_env *env,
3771 struct inode *inode, struct lu_attr *attr)
3773 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
3774 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
3775 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
3777 attr->la_atime = LTIME_S(inode->i_atime);
3778 attr->la_mtime = LTIME_S(inode->i_mtime);
3779 attr->la_ctime = LTIME_S(inode->i_ctime);
3780 attr->la_mode = inode->i_mode;
3781 attr->la_size = i_size_read(inode);
3782 attr->la_blocks = inode->i_blocks;
3783 attr->la_uid = inode->i_uid;
3784 attr->la_gid = inode->i_gid;
3785 attr->la_flags = LDISKFS_I(inode)->i_flags;
3786 attr->la_nlink = inode->i_nlink;
3787 attr->la_rdev = inode->i_rdev;
3788 attr->la_blksize = ll_inode_blksize(inode);
3789 attr->la_blkbits = inode->i_blkbits;
3796 static int lu_device_is_osd(const struct lu_device *d)
3798 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
3801 static struct osd_object *osd_obj(const struct lu_object *o)
3803 LASSERT(lu_device_is_osd(o->lo_dev));
3804 return container_of0(o, struct osd_object, oo_dt.do_lu);
3807 static struct osd_device *osd_dt_dev(const struct dt_device *d)
3809 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
3810 return container_of0(d, struct osd_device, od_dt_dev);
3813 static struct osd_device *osd_dev(const struct lu_device *d)
3815 LASSERT(lu_device_is_osd(d));
3816 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
3819 static struct osd_object *osd_dt_obj(const struct dt_object *d)
3821 return osd_obj(&d->do_lu);
3824 static struct osd_device *osd_obj2dev(const struct osd_object *o)
3826 return osd_dev(o->oo_dt.do_lu.lo_dev);
3829 static struct lu_device *osd2lu_dev(struct osd_device *osd)
3831 return &osd->od_dt_dev.dd_lu_dev;
3834 static struct super_block *osd_sb(const struct osd_device *dev)
3836 return dev->od_mount->lmi_mnt->mnt_sb;
3839 static journal_t *osd_journal(const struct osd_device *dev)
3841 return LDISKFS_SB(osd_sb(dev))->s_journal;
3844 static int osd_has_index(const struct osd_object *obj)
3846 return obj->oo_dt.do_index_ops != NULL;
3849 static int osd_object_invariant(const struct lu_object *l)
3851 return osd_invariant(osd_obj(l));
3854 static const struct lu_object_operations osd_lu_obj_ops = {
3855 .loo_object_init = osd_object_init,
3856 .loo_object_delete = osd_object_delete,
3857 .loo_object_release = osd_object_release,
3858 .loo_object_free = osd_object_free,
3859 .loo_object_print = osd_object_print,
3860 .loo_object_invariant = osd_object_invariant
3863 static const struct lu_device_operations osd_lu_ops = {
3864 .ldo_object_alloc = osd_object_alloc,
3865 .ldo_process_config = osd_process_config,
3866 .ldo_recovery_complete = osd_recovery_complete,
3867 .ldo_prepare = osd_prepare,
3870 static const struct lu_device_type_operations osd_device_type_ops = {
3871 .ldto_init = osd_type_init,
3872 .ldto_fini = osd_type_fini,
3874 .ldto_start = osd_type_start,
3875 .ldto_stop = osd_type_stop,
3877 .ldto_device_alloc = osd_device_alloc,
3878 .ldto_device_free = osd_device_free,
3880 .ldto_device_init = osd_device_init,
3881 .ldto_device_fini = osd_device_fini
3884 static struct lu_device_type osd_device_type = {
3885 .ldt_tags = LU_DEVICE_DT,
3886 .ldt_name = LUSTRE_OSD_NAME,
3887 .ldt_ops = &osd_device_type_ops,
3888 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3892 * lprocfs legacy support.
3894 static struct obd_ops osd_obd_device_ops = {
3895 .o_owner = THIS_MODULE
3898 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
3899 .llod_name = remote_obj_dir,
3900 .llod_oid = OSD_REM_OBJ_DIR_OID,
3902 .llod_feat = &dt_directory_features,
3905 static int __init osd_mod_init(void)
3907 struct lprocfs_static_vars lvars;
3910 llo_local_obj_register(&llod_osd_rem_obj_dir);
3911 lprocfs_osd_init_vars(&lvars);
3912 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
3913 LUSTRE_OSD_NAME, &osd_device_type);
3916 static void __exit osd_mod_exit(void)
3918 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
3919 class_unregister_type(LUSTRE_OSD_NAME);
3922 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3923 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
3924 MODULE_LICENSE("GPL");
3926 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);