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 *unused);
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 int osd_it_iam_rec (const struct lu_env *env,
251 const struct dt_it *di,
252 struct lu_dirent *lde,
254 static struct dt_it *osd_it_ea_init (const struct lu_env *env,
255 struct dt_object *dt,
256 struct lustre_capa *capa);
257 static struct dt_key *osd_it_ea_key (const struct lu_env *env,
258 const struct dt_it *di);
259 static inline int osd_it_ea_rec (const struct lu_env *env,
260 const struct dt_it *di,
261 struct lu_dirent *lde,
264 static struct timespec *osd_inode_time (const struct lu_env *env,
267 static struct thandle *osd_trans_start (const struct lu_env *env,
269 struct txn_param *p);
270 static journal_t *osd_journal (const struct osd_device *dev);
272 static int __osd_ea_add_rec(struct osd_thread_info *info,
273 struct osd_object *pobj,
274 struct osd_object *cobj,
278 static const struct lu_device_type_operations osd_device_type_ops;
279 static struct lu_device_type osd_device_type;
280 static const struct lu_object_operations osd_lu_obj_ops;
281 static struct obd_ops osd_obd_device_ops;
282 static const struct lu_device_operations osd_lu_ops;
283 static struct lu_context_key osd_key;
284 static const struct dt_object_operations osd_obj_ops;
285 static const struct dt_object_operations osd_obj_ea_ops;
286 static const struct dt_body_operations osd_body_ops;
287 static const struct dt_index_operations osd_index_iam_ops;
288 static const struct dt_index_operations osd_index_ea_ops;
291 struct thandle ot_super;
293 struct journal_callback ot_jcb;
294 /* Link to the device, for debugging. */
295 struct lu_ref_link *ot_dev_link;
299 #ifdef HAVE_QUOTA_SUPPORT
301 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
303 struct md_ucred *uc = md_ucred(env);
307 save->oc_uid = current->fsuid;
308 save->oc_gid = current->fsgid;
309 save->oc_cap = current->cap_effective;
310 current->fsuid = uc->mu_fsuid;
311 current->fsgid = uc->mu_fsgid;
312 current->cap_effective = uc->mu_cap;
316 osd_pop_ctxt(struct osd_ctxt *save)
318 current->fsuid = save->oc_uid;
319 current->fsgid = save->oc_gid;
320 current->cap_effective = save->oc_cap;
325 * Invariants, assertions.
329 * XXX: do not enable this, until invariant checking code is made thread safe
330 * in the face of pdirops locking.
332 #define OSD_INVARIANT_CHECKS (0)
334 #if OSD_INVARIANT_CHECKS
335 static int osd_invariant(const struct osd_object *obj)
339 ergo(obj->oo_inode != NULL,
340 obj->oo_inode->i_sb == osd_sb(osd_obj2dev(obj)) &&
341 atomic_read(&obj->oo_inode->i_count) > 0) &&
342 ergo(obj->oo_dir != NULL &&
343 obj->oo_dir->od_conationer.ic_object != NULL,
344 obj->oo_dir->od_conationer.ic_object == obj->oo_inode);
347 #define osd_invariant(obj) (1)
350 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
352 return lu_context_key_get(&env->le_ctx, &osd_key);
356 * Concurrency: doesn't matter
358 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
360 return osd_oti_get(env)->oti_r_locks > 0;
364 * Concurrency: doesn't matter
366 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
368 struct osd_thread_info *oti = osd_oti_get(env);
369 return oti->oti_w_locks > 0 && o->oo_owner == env;
373 * Concurrency: doesn't access mutable data
375 static int osd_root_get(const struct lu_env *env,
376 struct dt_device *dev, struct lu_fid *f)
380 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
381 lu_igif_build(f, inode->i_ino, inode->i_generation);
386 * OSD object methods.
390 * Concurrency: no concurrent access is possible that early in object
393 static struct lu_object *osd_object_alloc(const struct lu_env *env,
394 const struct lu_object_header *hdr,
397 struct osd_object *mo;
403 l = &mo->oo_dt.do_lu;
404 dt_object_init(&mo->oo_dt, NULL, d);
405 if (osd_dev(d)->od_iop_mode)
406 mo->oo_dt.do_ops = &osd_obj_ea_ops;
408 mo->oo_dt.do_ops = &osd_obj_ops;
410 l->lo_ops = &osd_lu_obj_ops;
411 init_rwsem(&mo->oo_sem);
412 init_rwsem(&mo->oo_ext_idx_sem);
413 spin_lock_init(&mo->oo_guard);
420 * Concurrency: shouldn't matter.
422 static void osd_object_init0(struct osd_object *obj)
424 LASSERT(obj->oo_inode != NULL);
425 obj->oo_dt.do_body_ops = &osd_body_ops;
426 obj->oo_dt.do_lu.lo_header->loh_attr |=
427 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
431 * Concurrency: no concurrent access is possible that early in object
434 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
435 const struct lu_object_conf *unused)
437 struct osd_object *obj = osd_obj(l);
440 LINVRNT(osd_invariant(obj));
442 result = osd_fid_lookup(env, obj, lu_object_fid(l));
444 if (obj->oo_inode != NULL)
445 osd_object_init0(obj);
447 LINVRNT(osd_invariant(obj));
452 * Concurrency: no concurrent access is possible that late in object
455 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
457 struct osd_object *obj = osd_obj(l);
459 LINVRNT(osd_invariant(obj));
461 dt_object_fini(&obj->oo_dt);
465 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
466 const struct iam_container *bag)
468 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
469 osd_oti_get(env)->oti_it_ipd);
472 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
473 const struct iam_container *bag)
475 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
476 osd_oti_get(env)->oti_idx_ipd);
479 static void osd_ipd_put(const struct lu_env *env,
480 const struct iam_container *bag,
481 struct iam_path_descr *ipd)
483 bag->ic_descr->id_ops->id_ipd_free(ipd);
487 * Concurrency: no concurrent access is possible that late in object
490 static void osd_index_fini(struct osd_object *o)
492 struct iam_container *bag;
494 if (o->oo_dir != NULL) {
495 bag = &o->oo_dir->od_container;
496 if (o->oo_inode != NULL) {
497 if (bag->ic_object == o->oo_inode)
498 iam_container_fini(bag);
500 OBD_FREE_PTR(o->oo_dir);
506 * Concurrency: no concurrent access is possible that late in object
507 * life-cycle (for all existing callers, that is. New callers have to provide
508 * their own locking.)
510 static int osd_inode_unlinked(const struct inode *inode)
512 return inode->i_nlink == 0;
516 OSD_TXN_OI_DELETE_CREDITS = 20,
517 OSD_TXN_INODE_DELETE_CREDITS = 20
521 * Concurrency: no concurrent access is possible that late in object
524 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
526 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
527 struct osd_device *osd = osd_obj2dev(obj);
528 struct osd_thread_info *oti = osd_oti_get(env);
529 struct txn_param *prm = &oti->oti_txn;
530 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
534 lu_env_init(env_del_obj, LCT_DT_THREAD);
535 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
536 OSD_TXN_INODE_DELETE_CREDITS);
537 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
539 result = osd_oi_delete(osd_oti_get(env_del_obj),
540 &osd->od_oi, fid, th);
541 osd_trans_stop(env_del_obj, th);
543 result = PTR_ERR(th);
545 lu_env_fini(env_del_obj);
550 * Called just before object is freed. Releases all resources except for
551 * object itself (that is released by osd_object_free()).
553 * Concurrency: no concurrent access is possible that late in object
556 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
558 struct osd_object *obj = osd_obj(l);
559 struct inode *inode = obj->oo_inode;
561 LINVRNT(osd_invariant(obj));
564 * If object is unlinked remove fid->ino mapping from object index.
571 if (osd_inode_unlinked(inode)) {
572 result = osd_inode_remove(env, obj);
574 LU_OBJECT_DEBUG(D_ERROR, env, l,
575 "Failed to cleanup: %d\n",
580 obj->oo_inode = NULL;
585 * Concurrency: ->loo_object_release() is called under site spin-lock.
587 static void osd_object_release(const struct lu_env *env,
590 struct osd_object *o = osd_obj(l);
592 LASSERT(!lu_object_is_dying(l->lo_header));
593 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
594 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
598 * Concurrency: shouldn't matter.
600 static int osd_object_print(const struct lu_env *env, void *cookie,
601 lu_printer_t p, const struct lu_object *l)
603 struct osd_object *o = osd_obj(l);
606 if (o->oo_dir != NULL)
607 d = o->oo_dir->od_container.ic_descr;
610 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
612 o->oo_inode ? o->oo_inode->i_ino : 0UL,
613 o->oo_inode ? o->oo_inode->i_generation : 0,
614 d ? d->id_ops->id_name : "plain");
618 * Concurrency: shouldn't matter.
620 int osd_statfs(const struct lu_env *env, struct dt_device *d,
623 struct osd_device *osd = osd_dt_dev(d);
624 struct super_block *sb = osd_sb(osd);
627 spin_lock(&osd->od_osfs_lock);
629 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
630 result = ll_do_statfs(sb, &osd->od_kstatfs);
631 if (likely(result == 0)) /* N.B. statfs can't really fail */
632 osd->od_osfs_age = cfs_time_current_64();
635 if (likely(result == 0))
636 *sfs = osd->od_kstatfs;
637 spin_unlock(&osd->od_osfs_lock);
643 * Concurrency: doesn't access mutable data.
645 static void osd_conf_get(const struct lu_env *env,
646 const struct dt_device *dev,
647 struct dt_device_param *param)
650 * XXX should be taken from not-yet-existing fs abstraction layer.
652 param->ddp_max_name_len = LDISKFS_NAME_LEN;
653 param->ddp_max_nlink = LDISKFS_LINK_MAX;
654 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
658 * Helper function to get and fill the buffer with input values.
660 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
664 buf = &osd_oti_get(env)->oti_buf;
675 * Concurrency: doesn't access mutable data.
677 static int osd_param_is_sane(const struct osd_device *dev,
678 const struct txn_param *param)
680 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
684 * Concurrency: shouldn't matter.
686 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
688 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
689 struct thandle *th = &oh->ot_super;
690 struct dt_device *dev = th->th_dev;
691 struct lu_device *lud = &dev->dd_lu_dev;
693 LASSERT(dev != NULL);
694 LASSERT(oh->ot_handle == NULL);
697 CERROR("transaction @0x%p commit error: %d\n", th, error);
699 struct lu_env *env = &osd_dt_dev(dev)->od_env_for_commit;
701 * This od_env_for_commit is only for commit usage. see
704 lu_context_enter(&env->le_ctx);
705 dt_txn_hook_commit(env, th);
706 lu_context_exit(&env->le_ctx);
709 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
713 lu_context_exit(&th->th_ctx);
714 lu_context_fini(&th->th_ctx);
719 * Concurrency: shouldn't matter.
721 static struct thandle *osd_trans_start(const struct lu_env *env,
725 struct osd_device *dev = osd_dt_dev(d);
727 struct osd_thandle *oh;
733 hook_res = dt_txn_hook_start(env, d, p);
735 RETURN(ERR_PTR(hook_res));
737 if (osd_param_is_sane(dev, p)) {
738 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
740 struct osd_thread_info *oti = osd_oti_get(env);
743 * XXX temporary stuff. Some abstraction layer should
747 jh = journal_start(osd_journal(dev), p->tp_credits);
753 jh->h_sync = p->tp_sync;
754 lu_device_get(&d->dd_lu_dev);
755 oh->ot_dev_link = lu_ref_add
756 (&d->dd_lu_dev.ld_reference,
758 /* add commit callback */
759 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
760 lu_context_enter(&th->th_ctx);
761 journal_callback_set(jh, osd_trans_commit_cb,
762 (struct journal_callback *)&oh->ot_jcb);
763 LASSERT(oti->oti_txns == 0);
764 LASSERT(oti->oti_r_locks == 0);
765 LASSERT(oti->oti_w_locks == 0);
772 th = ERR_PTR(-ENOMEM);
774 CERROR("Invalid transaction parameters\n");
775 th = ERR_PTR(-EINVAL);
782 * Concurrency: shouldn't matter.
784 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
787 struct osd_thandle *oh;
788 struct osd_thread_info *oti = osd_oti_get(env);
792 oh = container_of0(th, struct osd_thandle, ot_super);
793 if (oh->ot_handle != NULL) {
794 handle_t *hdl = oh->ot_handle;
796 LASSERT(oti->oti_txns == 1);
798 LASSERT(oti->oti_r_locks == 0);
799 LASSERT(oti->oti_w_locks == 0);
800 result = dt_txn_hook_stop(env, th);
802 CERROR("Failure in transaction hook: %d\n", result);
803 oh->ot_handle = NULL;
804 result = journal_stop(hdl);
806 CERROR("Failure to stop transaction: %d\n", result);
812 * Concurrency: shouldn't matter.
814 static int osd_sync(const struct lu_env *env, struct dt_device *d)
816 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
817 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
821 * Start commit for OSD device.
823 * An implementation of dt_commit_async method for OSD device.
824 * Asychronously starts underlayng fs sync and thereby a transaction
827 * \param env environment
830 * \see dt_device_operations
832 static int osd_commit_async(const struct lu_env *env,
835 struct super_block *s = osd_sb(osd_dt_dev(d));
838 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
839 RETURN(s->s_op->sync_fs(s, 0));
843 * Concurrency: shouldn't matter.
845 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
847 static void osd_ro(const struct lu_env *env, struct dt_device *d)
851 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
853 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
854 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
860 * Concurrency: serialization provided by callers.
862 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
863 int mode, unsigned long timeout, __u32 alg,
864 struct lustre_capa_key *keys)
866 struct osd_device *dev = osd_dt_dev(d);
869 dev->od_fl_capa = mode;
870 dev->od_capa_timeout = timeout;
871 dev->od_capa_alg = alg;
872 dev->od_capa_keys = keys;
877 * Concurrency: serialization provided by callers.
879 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
880 struct dt_quota_ctxt *ctxt, void *data)
882 struct obd_device *obd = (void *)ctxt;
883 struct vfsmount *mnt = (struct vfsmount *)data;
886 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
887 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
888 obd->obd_lvfs_ctxt.pwdmnt = mnt;
889 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
890 obd->obd_lvfs_ctxt.fs = get_ds();
896 * Note: we do not count into QUOTA here.
897 * If we mount with --data_journal we may need more.
899 static const int osd_dto_credits_noquota[DTO_NR] = {
902 * INDEX_EXTRA_TRANS_BLOCKS(8) +
903 * SINGLEDATA_TRANS_BLOCKS(8)
904 * XXX Note: maybe iam need more, since iam have more level than
907 [DTO_INDEX_INSERT] = 16,
908 [DTO_INDEX_DELETE] = 16,
912 [DTO_IDNEX_UPDATE] = 16,
914 * Create a object. The same as create object in EXT3.
915 * DATA_TRANS_BLOCKS(14) +
916 * INDEX_EXTRA_BLOCKS(8) +
917 * 3(inode bits, groups, GDT)
919 [DTO_OBJECT_CREATE] = 25,
923 [DTO_OBJECT_DELETE] = 25,
926 * 3(inode bits, group, GDT)
928 [DTO_ATTR_SET_BASE] = 3,
930 * Xattr set. The same as xattr of EXT3.
931 * DATA_TRANS_BLOCKS(14)
932 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
933 * also counted in. Do not know why?
935 [DTO_XATTR_SET] = 14,
938 * creadits for inode change during write.
940 [DTO_WRITE_BASE] = 3,
942 * credits for single block write.
944 [DTO_WRITE_BLOCK] = 14,
946 * Attr set credits for chown.
947 * 3 (inode bit, group, GDT)
949 [DTO_ATTR_SET_CHOWN]= 3
953 * Note: we count into QUOTA here.
954 * If we mount with --data_journal we may need more.
956 static const int osd_dto_credits_quota[DTO_NR] = {
958 * INDEX_EXTRA_TRANS_BLOCKS(8) +
959 * SINGLEDATA_TRANS_BLOCKS(8) +
960 * 2 * QUOTA_TRANS_BLOCKS(2)
962 [DTO_INDEX_INSERT] = 20,
964 * INDEX_EXTRA_TRANS_BLOCKS(8) +
965 * SINGLEDATA_TRANS_BLOCKS(8) +
966 * 2 * QUOTA_TRANS_BLOCKS(2)
968 [DTO_INDEX_DELETE] = 20,
972 [DTO_IDNEX_UPDATE] = 16,
974 * Create a object. Same as create object in EXT3 filesystem.
975 * DATA_TRANS_BLOCKS(16) +
976 * INDEX_EXTRA_BLOCKS(8) +
977 * 3(inode bits, groups, GDT) +
978 * 2 * QUOTA_INIT_BLOCKS(25)
980 [DTO_OBJECT_CREATE] = 77,
983 * DATA_TRANS_BLOCKS(16) +
984 * INDEX_EXTRA_BLOCKS(8) +
985 * 3(inode bits, groups, GDT) +
988 [DTO_OBJECT_DELETE] = 27,
991 * 3 (inode bit, group, GDT) +
993 [DTO_ATTR_SET_BASE] = 3,
995 * Xattr set. The same as xattr of EXT3.
996 * DATA_TRANS_BLOCKS(16)
997 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
998 * also counted in. Do not know why?
1000 [DTO_XATTR_SET] = 16,
1003 * creadits for inode change during write.
1005 [DTO_WRITE_BASE] = 3,
1007 * credits for single block write.
1009 [DTO_WRITE_BLOCK] = 16,
1011 * Attr set credits for chown.
1012 * 3 (inode bit, group, GDT) +
1013 * 2 * QUOTA_INIT_BLOCKS(25) +
1014 * 2 * QUOTA_DEL_BLOCKS(9)
1016 [DTO_ATTR_SET_CHOWN]= 71
1019 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1022 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1023 ARRAY_SIZE(osd_dto_credits_quota));
1024 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1025 #ifdef HAVE_QUOTA_SUPPORT
1026 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1027 return osd_dto_credits_quota[op];
1030 return osd_dto_credits_noquota[op];
1033 static const struct dt_device_operations osd_dt_ops = {
1034 .dt_root_get = osd_root_get,
1035 .dt_statfs = osd_statfs,
1036 .dt_trans_start = osd_trans_start,
1037 .dt_trans_stop = osd_trans_stop,
1038 .dt_conf_get = osd_conf_get,
1039 .dt_sync = osd_sync,
1041 .dt_commit_async = osd_commit_async,
1042 .dt_credit_get = osd_credit_get,
1043 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1044 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1047 static void osd_object_read_lock(const struct lu_env *env,
1048 struct dt_object *dt, unsigned role)
1050 struct osd_object *obj = osd_dt_obj(dt);
1051 struct osd_thread_info *oti = osd_oti_get(env);
1053 LINVRNT(osd_invariant(obj));
1055 LASSERT(obj->oo_owner != env);
1056 down_read_nested(&obj->oo_sem, role);
1058 LASSERT(obj->oo_owner == NULL);
1062 static void osd_object_write_lock(const struct lu_env *env,
1063 struct dt_object *dt, unsigned role)
1065 struct osd_object *obj = osd_dt_obj(dt);
1066 struct osd_thread_info *oti = osd_oti_get(env);
1068 LINVRNT(osd_invariant(obj));
1070 LASSERT(obj->oo_owner != env);
1071 down_write_nested(&obj->oo_sem, role);
1073 LASSERT(obj->oo_owner == NULL);
1074 obj->oo_owner = env;
1078 static void osd_object_read_unlock(const struct lu_env *env,
1079 struct dt_object *dt)
1081 struct osd_object *obj = osd_dt_obj(dt);
1082 struct osd_thread_info *oti = osd_oti_get(env);
1084 LINVRNT(osd_invariant(obj));
1086 LASSERT(oti->oti_r_locks > 0);
1088 up_read(&obj->oo_sem);
1091 static void osd_object_write_unlock(const struct lu_env *env,
1092 struct dt_object *dt)
1094 struct osd_object *obj = osd_dt_obj(dt);
1095 struct osd_thread_info *oti = osd_oti_get(env);
1097 LINVRNT(osd_invariant(obj));
1099 LASSERT(obj->oo_owner == env);
1100 LASSERT(oti->oti_w_locks > 0);
1102 obj->oo_owner = NULL;
1103 up_write(&obj->oo_sem);
1106 static int osd_object_write_locked(const struct lu_env *env,
1107 struct dt_object *dt)
1109 struct osd_object *obj = osd_dt_obj(dt);
1111 LINVRNT(osd_invariant(obj));
1113 return obj->oo_owner == env;
1116 static int capa_is_sane(const struct lu_env *env,
1117 struct osd_device *dev,
1118 struct lustre_capa *capa,
1119 struct lustre_capa_key *keys)
1121 struct osd_thread_info *oti = osd_oti_get(env);
1122 struct lustre_capa *tcapa = &oti->oti_capa;
1123 struct obd_capa *oc;
1127 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1129 if (capa_is_expired(oc)) {
1130 DEBUG_CAPA(D_ERROR, capa, "expired");
1137 if (capa_is_expired_sec(capa)) {
1138 DEBUG_CAPA(D_ERROR, capa, "expired");
1142 spin_lock(&capa_lock);
1143 for (i = 0; i < 2; i++) {
1144 if (keys[i].lk_keyid == capa->lc_keyid) {
1145 oti->oti_capa_key = keys[i];
1149 spin_unlock(&capa_lock);
1152 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1156 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1160 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1161 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1165 oc = capa_add(dev->od_capa_hash, capa);
1171 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1172 struct lustre_capa *capa, __u64 opc)
1174 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1175 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1176 struct md_capainfo *ci;
1179 if (!dev->od_fl_capa)
1182 if (capa == BYPASS_CAPA)
1185 ci = md_capainfo(env);
1189 if (ci->mc_auth == LC_ID_NONE)
1193 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1197 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1198 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1203 if (!capa_opc_supported(capa, opc)) {
1204 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1208 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1209 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1216 static int osd_attr_get(const struct lu_env *env,
1217 struct dt_object *dt,
1218 struct lu_attr *attr,
1219 struct lustre_capa *capa)
1221 struct osd_object *obj = osd_dt_obj(dt);
1223 LASSERT(dt_object_exists(dt));
1224 LINVRNT(osd_invariant(obj));
1226 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1229 spin_lock(&obj->oo_guard);
1230 osd_inode_getattr(env, obj->oo_inode, attr);
1231 spin_unlock(&obj->oo_guard);
1235 static int osd_attr_set(const struct lu_env *env,
1236 struct dt_object *dt,
1237 const struct lu_attr *attr,
1238 struct thandle *handle,
1239 struct lustre_capa *capa)
1241 struct osd_object *obj = osd_dt_obj(dt);
1244 LASSERT(handle != NULL);
1245 LASSERT(dt_object_exists(dt));
1246 LASSERT(osd_invariant(obj));
1248 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1251 spin_lock(&obj->oo_guard);
1252 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1253 spin_unlock(&obj->oo_guard);
1256 mark_inode_dirty(obj->oo_inode);
1260 static struct timespec *osd_inode_time(const struct lu_env *env,
1261 struct inode *inode, __u64 seconds)
1263 struct osd_thread_info *oti = osd_oti_get(env);
1264 struct timespec *t = &oti->oti_time;
1266 t->tv_sec = seconds;
1268 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1272 static int osd_inode_setattr(const struct lu_env *env,
1273 struct inode *inode, const struct lu_attr *attr)
1277 bits = attr->la_valid;
1279 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1281 #ifdef HAVE_QUOTA_SUPPORT
1282 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1283 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1284 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1290 iattr.ia_valid |= ATTR_UID;
1292 iattr.ia_valid |= ATTR_GID;
1293 iattr.ia_uid = attr->la_uid;
1294 iattr.ia_gid = attr->la_gid;
1295 osd_push_ctxt(env, save);
1296 rc = DQUOT_TRANSFER(inode, &iattr) ? -EDQUOT : 0;
1303 if (bits & LA_ATIME)
1304 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1305 if (bits & LA_CTIME)
1306 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1307 if (bits & LA_MTIME)
1308 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1309 if (bits & LA_SIZE) {
1310 LDISKFS_I(inode)->i_disksize = attr->la_size;
1311 i_size_write(inode, attr->la_size);
1314 /* OSD should not change "i_blocks" which is used by quota.
1315 * "i_blocks" should be changed by ldiskfs only.
1316 * Enable this assignment for SOM purpose now, until it is
1317 * stored in SOM EA. */
1318 if (bits & LA_BLOCKS)
1319 inode->i_blocks = attr->la_blocks;
1322 inode->i_mode = (inode->i_mode & S_IFMT) |
1323 (attr->la_mode & ~S_IFMT);
1325 inode->i_uid = attr->la_uid;
1327 inode->i_gid = attr->la_gid;
1328 if (bits & LA_NLINK)
1329 inode->i_nlink = attr->la_nlink;
1331 inode->i_rdev = attr->la_rdev;
1333 if (bits & LA_FLAGS) {
1334 struct ldiskfs_inode_info *li = LDISKFS_I(inode);
1336 li->i_flags = (li->i_flags & ~LDISKFS_FL_USER_MODIFIABLE) |
1337 (attr->la_flags & LDISKFS_FL_USER_MODIFIABLE);
1345 * XXX temporary solution.
1348 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1349 struct lu_attr *attr, struct thandle *th)
1354 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1355 struct lu_attr *attr, struct thandle *th)
1357 osd_object_init0(obj);
1361 extern struct inode *ldiskfs_create_inode(handle_t *handle,
1362 struct inode * dir, int mode);
1363 extern int ldiskfs_add_entry(handle_t *handle, struct dentry *dentry,
1364 struct inode *inode);
1365 extern int ldiskfs_delete_entry(handle_t *handle,
1367 struct ldiskfs_dir_entry_2 * de_del,
1368 struct buffer_head * bh);
1369 extern struct buffer_head * ldiskfs_find_entry(struct dentry *dentry,
1370 struct ldiskfs_dir_entry_2
1372 extern int ldiskfs_add_dot_dotdot(handle_t *handle, struct inode *dir,
1373 struct inode *inode);
1375 extern int ldiskfs_xattr_set_handle(handle_t *handle, struct inode *inode,
1376 int name_index, const char *name,
1377 const void *value, size_t value_len,
1380 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1381 struct osd_object *obj,
1385 struct osd_thread_info *info = osd_oti_get(env);
1386 struct dentry *child_dentry = &info->oti_child_dentry;
1387 struct dentry *obj_dentry = &info->oti_obj_dentry;
1389 obj_dentry->d_inode = obj->oo_inode;
1390 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1391 obj_dentry->d_name.hash = 0;
1393 child_dentry->d_name.hash = 0;
1394 child_dentry->d_parent = obj_dentry;
1395 child_dentry->d_name.name = name;
1396 child_dentry->d_name.len = namelen;
1397 return child_dentry;
1401 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1403 struct dt_allocation_hint *hint,
1407 struct osd_device *osd = osd_obj2dev(obj);
1408 struct osd_thandle *oth;
1409 struct dt_object *parent;
1410 struct inode *inode;
1411 #ifdef HAVE_QUOTA_SUPPORT
1412 struct osd_ctxt *save = &info->oti_ctxt;
1415 LINVRNT(osd_invariant(obj));
1416 LASSERT(obj->oo_inode == NULL);
1418 oth = container_of(th, struct osd_thandle, ot_super);
1419 LASSERT(oth->ot_handle->h_transaction != NULL);
1421 if (hint && hint->dah_parent)
1422 parent = hint->dah_parent;
1424 parent = osd->od_obj_area;
1426 LASSERT(parent != NULL);
1427 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1429 #ifdef HAVE_QUOTA_SUPPORT
1430 osd_push_ctxt(info->oti_env, save);
1432 inode = ldiskfs_create_inode(oth->ot_handle,
1433 osd_dt_obj(parent)->oo_inode, mode);
1434 #ifdef HAVE_QUOTA_SUPPORT
1437 if (!IS_ERR(inode)) {
1438 obj->oo_inode = inode;
1441 result = PTR_ERR(inode);
1442 LINVRNT(osd_invariant(obj));
1447 extern int iam_lvar_create(struct inode *obj, int keysize, int ptrsize,
1448 int recsize, handle_t *handle);
1450 extern int iam_lfix_create(struct inode *obj, int keysize, int ptrsize,
1451 int recsize, handle_t *handle);
1458 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1459 struct lu_attr *attr,
1460 struct dt_allocation_hint *hint,
1461 struct dt_object_format *dof,
1465 struct osd_thandle *oth;
1466 struct osd_device *osd = osd_obj2dev(obj);
1467 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1469 LASSERT(S_ISDIR(attr->la_mode));
1471 oth = container_of(th, struct osd_thandle, ot_super);
1472 LASSERT(oth->ot_handle->h_transaction != NULL);
1473 result = osd_mkfile(info, obj, mode, hint, th);
1474 if (result == 0 && osd->od_iop_mode == 0) {
1475 LASSERT(obj->oo_inode != NULL);
1477 * XXX uh-oh... call low-level iam function directly.
1480 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1481 sizeof (struct lu_fid_pack),
1487 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1488 struct lu_attr *attr,
1489 struct dt_allocation_hint *hint,
1490 struct dt_object_format *dof,
1494 struct osd_thandle *oth;
1495 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1497 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1499 LASSERT(S_ISREG(attr->la_mode));
1501 oth = container_of(th, struct osd_thandle, ot_super);
1502 LASSERT(oth->ot_handle->h_transaction != NULL);
1504 result = osd_mkfile(info, obj, mode, hint, th);
1506 LASSERT(obj->oo_inode != NULL);
1507 if (feat->dif_flags & DT_IND_VARKEY)
1508 result = iam_lvar_create(obj->oo_inode,
1509 feat->dif_keysize_max,
1511 feat->dif_recsize_max,
1514 result = iam_lfix_create(obj->oo_inode,
1515 feat->dif_keysize_max,
1517 feat->dif_recsize_max,
1524 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1525 struct lu_attr *attr,
1526 struct dt_allocation_hint *hint,
1527 struct dt_object_format *dof,
1530 LASSERT(S_ISREG(attr->la_mode));
1531 return osd_mkfile(info, obj, (attr->la_mode &
1532 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1535 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1536 struct lu_attr *attr,
1537 struct dt_allocation_hint *hint,
1538 struct dt_object_format *dof,
1541 LASSERT(S_ISLNK(attr->la_mode));
1542 return osd_mkfile(info, obj, (attr->la_mode &
1543 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1546 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1547 struct lu_attr *attr,
1548 struct dt_allocation_hint *hint,
1549 struct dt_object_format *dof,
1552 umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1555 LINVRNT(osd_invariant(obj));
1556 LASSERT(obj->oo_inode == NULL);
1557 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1558 S_ISFIFO(mode) || S_ISSOCK(mode));
1560 result = osd_mkfile(info, obj, mode, hint, th);
1562 LASSERT(obj->oo_inode != NULL);
1563 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1565 LINVRNT(osd_invariant(obj));
1569 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1571 struct dt_allocation_hint *hint,
1572 struct dt_object_format *dof,
1575 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1577 osd_obj_type_f result;
1593 result = osd_mk_index;
1604 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1605 struct dt_object *parent, umode_t child_mode)
1609 memset(ah, 0, sizeof(*ah));
1610 ah->dah_parent = parent;
1611 ah->dah_mode = child_mode;
1615 * Helper function for osd_object_create()
1617 * \retval 0, on success
1619 static int __osd_object_create(struct osd_thread_info *info,
1620 struct osd_object *obj, struct lu_attr *attr,
1621 struct dt_allocation_hint *hint,
1622 struct dt_object_format *dof,
1628 result = osd_create_pre(info, obj, attr, th);
1630 result = osd_create_type_f(dof->dof_type)(info, obj,
1631 attr, hint, dof, th);
1633 result = osd_create_post(info, obj, attr, th);
1639 * Helper function for osd_object_create()
1641 * \retval 0, on success
1643 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1644 const struct lu_fid *fid, struct thandle *th)
1646 struct osd_thread_info *info = osd_oti_get(env);
1647 struct osd_inode_id *id = &info->oti_id;
1648 struct osd_device *osd = osd_obj2dev(obj);
1649 struct md_ucred *uc = md_ucred(env);
1651 LASSERT(obj->oo_inode != NULL);
1652 LASSERT(uc != NULL);
1654 id->oii_ino = obj->oo_inode->i_ino;
1655 id->oii_gen = obj->oo_inode->i_generation;
1657 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1658 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1661 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1662 struct lu_attr *attr,
1663 struct dt_allocation_hint *hint,
1664 struct dt_object_format *dof,
1667 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1668 struct osd_object *obj = osd_dt_obj(dt);
1669 struct osd_thread_info *info = osd_oti_get(env);
1674 LINVRNT(osd_invariant(obj));
1675 LASSERT(!dt_object_exists(dt));
1676 LASSERT(osd_write_locked(env, obj));
1677 LASSERT(th != NULL);
1679 result = __osd_object_create(info, obj, attr, hint, dof, th);
1681 result = __osd_oi_insert(env, obj, fid, th);
1683 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1684 LASSERT(osd_invariant(obj));
1689 * Helper function for osd_xattr_set()
1691 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1692 const struct lu_buf *buf, const char *name, int fl)
1694 struct osd_object *obj = osd_dt_obj(dt);
1695 struct inode *inode = obj->oo_inode;
1696 struct osd_thread_info *info = osd_oti_get(env);
1697 struct dentry *dentry = &info->oti_child_dentry;
1698 struct timespec *t = &info->oti_time;
1702 LASSERT(dt_object_exists(dt));
1703 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1704 LASSERT(osd_write_locked(env, obj));
1706 if (fl & LU_XATTR_REPLACE)
1707 fs_flags |= XATTR_REPLACE;
1709 if (fl & LU_XATTR_CREATE)
1710 fs_flags |= XATTR_CREATE;
1712 dentry->d_inode = inode;
1713 *t = inode->i_ctime;
1714 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1715 buf->lb_len, fs_flags);
1716 /* ctime should not be updated with server-side time. */
1717 spin_lock(&obj->oo_guard);
1718 inode->i_ctime = *t;
1719 spin_unlock(&obj->oo_guard);
1720 mark_inode_dirty(inode);
1725 * Put the fid into lustre_mdt_attrs, and then place the structure
1726 * inode's ea. This fid should not be altered during the life time
1729 * \retval +ve, on success
1730 * \retval -ve, on error
1732 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1734 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1735 const struct lu_fid *fid)
1737 struct osd_thread_info *info = osd_oti_get(env);
1738 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1740 fid_cpu_to_be(&mdt_attrs->lma_self_fid, fid);
1742 return __osd_xattr_set(env, dt,
1743 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1744 XATTR_NAME_LMA, LU_XATTR_CREATE);
1749 * Helper function to form igif
1751 static inline void osd_igif_get(const struct lu_env *env, struct dentry *dentry,
1754 struct inode *inode = dentry->d_inode;
1755 lu_igif_build(fid, inode->i_ino, inode->i_generation);
1759 * Helper function to pack the fid
1761 static inline void osd_fid_pack(const struct lu_env *env, const struct lu_fid *fid,
1762 struct dt_rec *pack)
1764 fid_pack((struct lu_fid_pack *)pack, fid, &osd_oti_get(env)->oti_fid);
1768 * Try to read the fid from inode ea into dt_rec, if return value
1769 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1771 * \param fid, object fid.
1773 * \retval 0, on success
1775 static int osd_ea_fid_get(const struct lu_env *env, struct dentry *dentry,
1778 struct inode *inode = dentry->d_inode;
1779 struct osd_thread_info *info = osd_oti_get(env);
1780 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1783 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1785 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1788 /* Check LMA compatibility */
1790 (mdt_attrs->lma_incompat & ~cpu_to_be32(LMA_INCOMPAT_SUPP))) {
1791 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1792 inode->i_ino, be32_to_cpu(mdt_attrs->lma_incompat) &
1793 ~LMA_INCOMPAT_SUPP);
1798 fid_be_to_cpu(fid, &mdt_attrs->lma_self_fid);
1800 } else if (rc == -ENODATA) {
1801 osd_igif_get(env, dentry, fid);
1809 * OSD layer object create function for interoperability mode (b11826).
1810 * This is mostly similar to osd_object_create(). Only difference being, fid is
1811 * inserted into inode ea here.
1813 * \retval 0, on success
1814 * \retval -ve, on error
1816 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1817 struct lu_attr *attr,
1818 struct dt_allocation_hint *hint,
1819 struct dt_object_format *dof,
1822 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1823 struct osd_object *obj = osd_dt_obj(dt);
1824 struct osd_thread_info *info = osd_oti_get(env);
1830 LASSERT(osd_invariant(obj));
1831 LASSERT(!dt_object_exists(dt));
1832 LASSERT(osd_write_locked(env, obj));
1833 LASSERT(th != NULL);
1835 result = __osd_object_create(info, obj, attr, hint, dof, th);
1837 if (hint && hint->dah_parent)
1838 is_root = osd_object_is_root(osd_dt_obj(hint->dah_parent));
1840 /* objects under osd root shld have igif fid, so dont add fid EA */
1841 if (result == 0 && is_root == 0)
1842 result = osd_ea_fid_set(env, dt, fid);
1845 result = __osd_oi_insert(env, obj, fid, th);
1847 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1848 LINVRNT(osd_invariant(obj));
1853 * Concurrency: @dt is write locked.
1855 static void osd_object_ref_add(const struct lu_env *env,
1856 struct dt_object *dt,
1859 struct osd_object *obj = osd_dt_obj(dt);
1860 struct inode *inode = obj->oo_inode;
1862 LINVRNT(osd_invariant(obj));
1863 LASSERT(dt_object_exists(dt));
1864 LASSERT(osd_write_locked(env, obj));
1865 LASSERT(th != NULL);
1867 spin_lock(&obj->oo_guard);
1868 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
1870 spin_unlock(&obj->oo_guard);
1871 mark_inode_dirty(inode);
1872 LINVRNT(osd_invariant(obj));
1876 * Concurrency: @dt is write locked.
1878 static void osd_object_ref_del(const struct lu_env *env,
1879 struct dt_object *dt,
1882 struct osd_object *obj = osd_dt_obj(dt);
1883 struct inode *inode = obj->oo_inode;
1885 LINVRNT(osd_invariant(obj));
1886 LASSERT(dt_object_exists(dt));
1887 LASSERT(osd_write_locked(env, obj));
1888 LASSERT(th != NULL);
1890 spin_lock(&obj->oo_guard);
1891 LASSERT(inode->i_nlink > 0);
1893 spin_unlock(&obj->oo_guard);
1894 mark_inode_dirty(inode);
1895 LINVRNT(osd_invariant(obj));
1899 * Concurrency: @dt is read locked.
1901 static int osd_xattr_get(const struct lu_env *env,
1902 struct dt_object *dt,
1905 struct lustre_capa *capa)
1907 struct osd_object *obj = osd_dt_obj(dt);
1908 struct inode *inode = obj->oo_inode;
1909 struct osd_thread_info *info = osd_oti_get(env);
1910 struct dentry *dentry = &info->oti_obj_dentry;
1912 LASSERT(dt_object_exists(dt));
1913 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1914 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1916 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1919 dentry->d_inode = inode;
1920 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
1925 * Concurrency: @dt is write locked.
1927 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1928 const struct lu_buf *buf, const char *name, int fl,
1929 struct thandle *handle, struct lustre_capa *capa)
1931 LASSERT(handle != NULL);
1933 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1936 return __osd_xattr_set(env, dt, buf, name, fl);
1940 * Concurrency: @dt is read locked.
1942 static int osd_xattr_list(const struct lu_env *env,
1943 struct dt_object *dt,
1945 struct lustre_capa *capa)
1947 struct osd_object *obj = osd_dt_obj(dt);
1948 struct inode *inode = obj->oo_inode;
1949 struct osd_thread_info *info = osd_oti_get(env);
1950 struct dentry *dentry = &info->oti_obj_dentry;
1952 LASSERT(dt_object_exists(dt));
1953 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
1954 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
1956 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1959 dentry->d_inode = inode;
1960 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
1964 * Concurrency: @dt is write locked.
1966 static int osd_xattr_del(const struct lu_env *env,
1967 struct dt_object *dt,
1969 struct thandle *handle,
1970 struct lustre_capa *capa)
1972 struct osd_object *obj = osd_dt_obj(dt);
1973 struct inode *inode = obj->oo_inode;
1974 struct osd_thread_info *info = osd_oti_get(env);
1975 struct dentry *dentry = &info->oti_obj_dentry;
1976 struct timespec *t = &info->oti_time;
1979 LASSERT(dt_object_exists(dt));
1980 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
1981 LASSERT(osd_write_locked(env, obj));
1982 LASSERT(handle != NULL);
1984 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1987 dentry->d_inode = inode;
1988 *t = inode->i_ctime;
1989 rc = inode->i_op->removexattr(dentry, name);
1990 /* ctime should not be updated with server-side time. */
1991 spin_lock(&obj->oo_guard);
1992 inode->i_ctime = *t;
1993 spin_unlock(&obj->oo_guard);
1994 mark_inode_dirty(inode);
1998 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1999 struct dt_object *dt,
2000 struct lustre_capa *old,
2003 struct osd_thread_info *info = osd_oti_get(env);
2004 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2005 struct osd_object *obj = osd_dt_obj(dt);
2006 struct osd_device *dev = osd_obj2dev(obj);
2007 struct lustre_capa_key *key = &info->oti_capa_key;
2008 struct lustre_capa *capa = &info->oti_capa;
2009 struct obd_capa *oc;
2010 struct md_capainfo *ci;
2014 if (!dev->od_fl_capa)
2015 RETURN(ERR_PTR(-ENOENT));
2017 LASSERT(dt_object_exists(dt));
2018 LINVRNT(osd_invariant(obj));
2020 /* renewal sanity check */
2021 if (old && osd_object_auth(env, dt, old, opc))
2022 RETURN(ERR_PTR(-EACCES));
2024 ci = md_capainfo(env);
2026 RETURN(ERR_PTR(-ENOENT));
2028 switch (ci->mc_auth) {
2032 capa->lc_uid = obj->oo_inode->i_uid;
2033 capa->lc_gid = obj->oo_inode->i_gid;
2034 capa->lc_flags = LC_ID_PLAIN;
2036 case LC_ID_CONVERT: {
2039 s[0] = obj->oo_inode->i_uid;
2040 get_random_bytes(&(s[1]), sizeof(__u32));
2041 s[2] = obj->oo_inode->i_gid;
2042 get_random_bytes(&(s[3]), sizeof(__u32));
2043 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2045 RETURN(ERR_PTR(rc));
2047 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2048 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2049 capa->lc_flags = LC_ID_CONVERT;
2053 RETURN(ERR_PTR(-EINVAL));
2056 capa->lc_fid = *fid;
2058 capa->lc_flags |= dev->od_capa_alg << 24;
2059 capa->lc_timeout = dev->od_capa_timeout;
2060 capa->lc_expiry = 0;
2062 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2064 LASSERT(!capa_is_expired(oc));
2068 spin_lock(&capa_lock);
2069 *key = dev->od_capa_keys[1];
2070 spin_unlock(&capa_lock);
2072 capa->lc_keyid = key->lk_keyid;
2073 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2075 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2077 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2078 RETURN(ERR_PTR(rc));
2081 oc = capa_add(dev->od_capa_hash, capa);
2085 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2088 struct osd_object *obj = osd_dt_obj(dt);
2089 struct inode *inode = obj->oo_inode;
2090 struct osd_thread_info *info = osd_oti_get(env);
2091 struct dentry *dentry = &info->oti_obj_dentry;
2092 struct file *file = &info->oti_file;
2095 dentry->d_inode = inode;
2096 file->f_dentry = dentry;
2097 file->f_mapping = inode->i_mapping;
2098 file->f_op = inode->i_fop;
2099 LOCK_INODE_MUTEX(inode);
2100 rc = file->f_op->fsync(file, dentry, 0);
2101 UNLOCK_INODE_MUTEX(inode);
2106 * Get the 64-bit version for an inode.
2108 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2109 struct dt_object *dt)
2111 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2113 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2114 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2115 return LDISKFS_I(inode)->i_fs_version;
2119 * Set the 64-bit version and return the old version.
2121 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2122 dt_obj_version_t new_version)
2124 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2126 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2127 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2128 LDISKFS_I(inode)->i_fs_version = new_version;
2129 /** Version is set after all inode operations are finished,
2130 * so we should mark it dirty here */
2131 inode->i_sb->s_op->dirty_inode(inode);
2134 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2137 struct osd_object *obj = osd_dt_obj(dt);
2140 *data = (void *)obj->oo_inode;
2144 static const struct dt_object_operations osd_obj_ops = {
2145 .do_read_lock = osd_object_read_lock,
2146 .do_write_lock = osd_object_write_lock,
2147 .do_read_unlock = osd_object_read_unlock,
2148 .do_write_unlock = osd_object_write_unlock,
2149 .do_write_locked = osd_object_write_locked,
2150 .do_attr_get = osd_attr_get,
2151 .do_attr_set = osd_attr_set,
2152 .do_ah_init = osd_ah_init,
2153 .do_create = osd_object_create,
2154 .do_index_try = osd_index_try,
2155 .do_ref_add = osd_object_ref_add,
2156 .do_ref_del = osd_object_ref_del,
2157 .do_xattr_get = osd_xattr_get,
2158 .do_xattr_set = osd_xattr_set,
2159 .do_xattr_del = osd_xattr_del,
2160 .do_xattr_list = osd_xattr_list,
2161 .do_capa_get = osd_capa_get,
2162 .do_object_sync = osd_object_sync,
2163 .do_version_get = osd_object_version_get,
2164 .do_version_set = osd_object_version_set,
2165 .do_data_get = osd_data_get,
2169 * dt_object_operations for interoperability mode
2170 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2172 static const struct dt_object_operations osd_obj_ea_ops = {
2173 .do_read_lock = osd_object_read_lock,
2174 .do_write_lock = osd_object_write_lock,
2175 .do_read_unlock = osd_object_read_unlock,
2176 .do_write_unlock = osd_object_write_unlock,
2177 .do_write_locked = osd_object_write_locked,
2178 .do_attr_get = osd_attr_get,
2179 .do_attr_set = osd_attr_set,
2180 .do_ah_init = osd_ah_init,
2181 .do_create = osd_object_ea_create,
2182 .do_index_try = osd_index_try,
2183 .do_ref_add = osd_object_ref_add,
2184 .do_ref_del = osd_object_ref_del,
2185 .do_xattr_get = osd_xattr_get,
2186 .do_xattr_set = osd_xattr_set,
2187 .do_xattr_del = osd_xattr_del,
2188 .do_xattr_list = osd_xattr_list,
2189 .do_capa_get = osd_capa_get,
2190 .do_object_sync = osd_object_sync,
2191 .do_version_get = osd_object_version_get,
2192 .do_version_set = osd_object_version_set,
2193 .do_data_get = osd_data_get,
2201 * XXX: Another layering violation for now.
2203 * We don't want to use ->f_op->read methods, because generic file write
2205 * - serializes on ->i_sem, and
2207 * - does a lot of extra work like balance_dirty_pages(),
2209 * which doesn't work for globally shared files like /last-received.
2211 int fsfilt_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs);
2212 int fsfilt_ldiskfs_write_handle(struct inode *inode, void *buf, int bufsize,
2213 loff_t *offs, handle_t *handle);
2215 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2216 struct lu_buf *buf, loff_t *pos,
2217 struct lustre_capa *capa)
2219 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2221 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2224 return fsfilt_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2227 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2228 const struct lu_buf *buf, loff_t *pos,
2229 struct thandle *handle, struct lustre_capa *capa,
2232 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2233 struct osd_thandle *oh;
2235 #ifdef HAVE_QUOTA_SUPPORT
2236 cfs_cap_t save = current->cap_effective;
2239 LASSERT(handle != NULL);
2241 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2244 oh = container_of(handle, struct osd_thandle, ot_super);
2245 LASSERT(oh->ot_handle->h_transaction != NULL);
2246 #ifdef HAVE_QUOTA_SUPPORT
2248 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2250 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2252 result = fsfilt_ldiskfs_write_handle(inode, buf->lb_buf, buf->lb_len,
2253 pos, oh->ot_handle);
2254 #ifdef HAVE_QUOTA_SUPPORT
2255 current->cap_effective = save;
2258 result = buf->lb_len;
2262 static const struct dt_body_operations osd_body_ops = {
2263 .dbo_read = osd_read,
2264 .dbo_write = osd_write
2271 static int osd_object_is_root(const struct osd_object *obj)
2273 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
2276 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2277 const struct dt_index_features *feat)
2279 struct iam_descr *descr;
2280 struct dt_object *dt = &o->oo_dt;
2282 if (osd_object_is_root(o))
2283 return feat == &dt_directory_features;
2285 LASSERT(o->oo_dir != NULL);
2287 descr = o->oo_dir->od_container.ic_descr;
2288 if (feat == &dt_directory_features) {
2289 if (descr->id_rec_size == sizeof(struct lu_fid_pack))
2292 if (descr == &iam_htree_compat_param) {
2293 /* if it is a HTREE dir then there is good chance that,
2294 * we dealing with ext3 directory here with no FIDs. */
2296 if (descr->id_rec_size ==
2297 sizeof ((struct ldiskfs_dir_entry_2 *)NULL)->inode) {
2299 dt->do_index_ops = &osd_index_ea_ops;
2306 feat->dif_keysize_min <= descr->id_key_size &&
2307 descr->id_key_size <= feat->dif_keysize_max &&
2308 feat->dif_recsize_min <= descr->id_rec_size &&
2309 descr->id_rec_size <= feat->dif_recsize_max &&
2310 !(feat->dif_flags & (DT_IND_VARKEY |
2311 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2312 ergo(feat->dif_flags & DT_IND_UPDATE,
2313 1 /* XXX check that object (and file system) is
2318 static int osd_iam_container_init(const struct lu_env *env,
2319 struct osd_object *obj,
2320 struct osd_directory *dir)
2323 struct iam_container *bag;
2325 bag = &dir->od_container;
2326 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2328 result = iam_container_setup(bag);
2330 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2332 iam_container_fini(bag);
2338 * Concurrency: no external locking is necessary.
2340 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2341 const struct dt_index_features *feat)
2345 struct osd_object *obj = osd_dt_obj(dt);
2346 struct osd_device *osd = osd_obj2dev(obj);
2348 LINVRNT(osd_invariant(obj));
2349 LASSERT(dt_object_exists(dt));
2351 if (osd_object_is_root(obj)) {
2352 dt->do_index_ops = &osd_index_ea_ops;
2354 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2355 dt->do_index_ops = &osd_index_ea_ops;
2356 if (S_ISDIR(obj->oo_inode->i_mode))
2361 } else if (!osd_has_index(obj)) {
2362 struct osd_directory *dir;
2367 spin_lock(&obj->oo_guard);
2368 if (obj->oo_dir == NULL)
2372 * Concurrent thread allocated container data.
2375 spin_unlock(&obj->oo_guard);
2377 * Now, that we have container data, serialize its
2380 down_write(&obj->oo_ext_idx_sem);
2382 * recheck under lock.
2384 if (!osd_has_index(obj))
2385 result = osd_iam_container_init(env, obj, dir);
2388 up_write(&obj->oo_ext_idx_sem);
2394 if (result == 0 && ea_dir == 0) {
2395 if (!osd_iam_index_probe(env, obj, feat))
2398 LINVRNT(osd_invariant(obj));
2404 * delete a (key, value) pair from index \a dt specified by \a key
2406 * \param dt_object osd index object
2407 * \param key key for index
2408 * \param rec record reference
2409 * \param handle transaction handler
2412 * \retval -ve failure
2415 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2416 const struct dt_key *key, struct thandle *handle,
2417 struct lustre_capa *capa)
2419 struct osd_object *obj = osd_dt_obj(dt);
2420 struct osd_thandle *oh;
2421 struct iam_path_descr *ipd;
2422 struct iam_container *bag = &obj->oo_dir->od_container;
2427 LINVRNT(osd_invariant(obj));
2428 LASSERT(dt_object_exists(dt));
2429 LASSERT(bag->ic_object == obj->oo_inode);
2430 LASSERT(handle != NULL);
2432 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2435 ipd = osd_idx_ipd_get(env, bag);
2436 if (unlikely(ipd == NULL))
2439 oh = container_of0(handle, struct osd_thandle, ot_super);
2440 LASSERT(oh->ot_handle != NULL);
2441 LASSERT(oh->ot_handle->h_transaction != NULL);
2443 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2444 osd_ipd_put(env, bag, ipd);
2445 LINVRNT(osd_invariant(obj));
2450 * Index delete function for interoperability mode (b11826).
2451 * It will remove the directory entry added by osd_index_ea_insert().
2452 * This entry is needed to maintain name->fid mapping.
2454 * \param key, key i.e. file entry to be deleted
2456 * \retval 0, on success
2457 * \retval -ve, on error
2459 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2460 const struct dt_key *key, struct thandle *handle,
2461 struct lustre_capa *capa)
2463 struct osd_object *obj = osd_dt_obj(dt);
2464 struct inode *dir = obj->oo_inode;
2465 struct dentry *dentry;
2466 struct osd_thandle *oh;
2467 struct ldiskfs_dir_entry_2 *de;
2468 struct buffer_head *bh;
2474 LINVRNT(osd_invariant(obj));
2475 LASSERT(dt_object_exists(dt));
2476 LASSERT(handle != NULL);
2478 oh = container_of(handle, struct osd_thandle, ot_super);
2479 LASSERT(oh->ot_handle != NULL);
2480 LASSERT(oh->ot_handle->h_transaction != NULL);
2482 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2485 dentry = osd_child_dentry_get(env, obj,
2486 (char *)key, strlen((char *)key));
2488 down_write(&obj->oo_ext_idx_sem);
2489 bh = ldiskfs_find_entry(dentry, &de);
2491 struct osd_thread_info *oti = osd_oti_get(env);
2492 struct timespec *ctime = &oti->oti_time;
2493 struct timespec *mtime = &oti->oti_time2;
2495 *ctime = dir->i_ctime;
2496 *mtime = dir->i_mtime;
2497 rc = ldiskfs_delete_entry(oh->ot_handle,
2499 /* xtime should not be updated with server-side time. */
2500 spin_lock(&obj->oo_guard);
2501 dir->i_ctime = *ctime;
2502 dir->i_mtime = *mtime;
2503 spin_unlock(&obj->oo_guard);
2504 mark_inode_dirty(dir);
2509 up_write(&obj->oo_ext_idx_sem);
2510 LASSERT(osd_invariant(obj));
2515 * Lookup index for \a key and copy record to \a rec.
2517 * \param dt_object osd index object
2518 * \param key key for index
2519 * \param rec record reference
2521 * \retval +ve success : exact mach
2522 * \retval 0 return record with key not greater than \a key
2523 * \retval -ve failure
2525 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2526 struct dt_rec *rec, const struct dt_key *key,
2527 struct lustre_capa *capa)
2529 struct osd_object *obj = osd_dt_obj(dt);
2530 struct iam_path_descr *ipd;
2531 struct iam_container *bag = &obj->oo_dir->od_container;
2532 struct osd_thread_info *oti = osd_oti_get(env);
2533 struct iam_iterator *it = &oti->oti_idx_it;
2537 LASSERT(osd_invariant(obj));
2538 LASSERT(dt_object_exists(dt));
2539 LASSERT(bag->ic_object == obj->oo_inode);
2541 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2544 ipd = osd_idx_ipd_get(env, bag);
2548 /* got ipd now we can start iterator. */
2549 iam_it_init(it, bag, 0, ipd);
2551 rc = iam_it_get(it, (struct iam_key *)key);
2553 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
2557 osd_ipd_put(env, bag, ipd);
2559 LINVRNT(osd_invariant(obj));
2565 * Inserts (key, value) pair in \a dt index object.
2567 * \param dt osd index object
2568 * \param key key for index
2569 * \param rec record reference
2570 * \param th transaction handler
2573 * \retval -ve failure
2575 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2576 const struct dt_rec *rec, const struct dt_key *key,
2577 struct thandle *th, struct lustre_capa *capa,
2580 struct osd_object *obj = osd_dt_obj(dt);
2581 struct iam_path_descr *ipd;
2582 struct osd_thandle *oh;
2583 struct iam_container *bag = &obj->oo_dir->od_container;
2584 #ifdef HAVE_QUOTA_SUPPORT
2585 cfs_cap_t save = current->cap_effective;
2591 LINVRNT(osd_invariant(obj));
2592 LASSERT(dt_object_exists(dt));
2593 LASSERT(bag->ic_object == obj->oo_inode);
2594 LASSERT(th != NULL);
2596 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2599 ipd = osd_idx_ipd_get(env, bag);
2600 if (unlikely(ipd == NULL))
2603 oh = container_of0(th, struct osd_thandle, ot_super);
2604 LASSERT(oh->ot_handle != NULL);
2605 LASSERT(oh->ot_handle->h_transaction != NULL);
2606 #ifdef HAVE_QUOTA_SUPPORT
2608 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2610 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2612 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2613 (struct iam_rec *)rec, ipd);
2614 #ifdef HAVE_QUOTA_SUPPORT
2615 current->cap_effective = save;
2617 osd_ipd_put(env, bag, ipd);
2618 LINVRNT(osd_invariant(obj));
2623 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2624 * into the directory.Also sets flags into osd object to
2625 * indicate dot and dotdot are created. This is required for
2626 * interoperability mode (b11826)
2628 * \param dir directory for dot and dotdot fixup.
2629 * \param obj child object for linking
2631 * \retval 0, on success
2632 * \retval -ve, on error
2634 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2635 struct osd_object *dir,
2636 struct osd_object *obj, const char *name,
2639 struct inode *parent_dir = obj->oo_inode;
2640 struct inode *inode = dir->oo_inode;
2641 struct osd_thandle *oth;
2644 oth = container_of(th, struct osd_thandle, ot_super);
2645 LASSERT(oth->ot_handle->h_transaction != NULL);
2646 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2648 if (strcmp(name, dot) == 0) {
2649 if (dir->oo_compat_dot_created) {
2652 LASSERT(obj == dir);
2653 dir->oo_compat_dot_created = 1;
2656 } else if(strcmp(name, dotdot) == 0) {
2657 if (!dir->oo_compat_dot_created)
2659 if (dir->oo_compat_dotdot_created)
2660 return __osd_ea_add_rec(info, dir, obj, name, th);
2662 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode);
2664 dir->oo_compat_dotdot_created = 1;
2671 * Calls ldiskfs_add_entry() to add directory entry
2672 * into the directory. This is required for
2673 * interoperability mode (b11826)
2675 * \retval 0, on success
2676 * \retval -ve, on error
2678 static int __osd_ea_add_rec(struct osd_thread_info *info,
2679 struct osd_object *pobj,
2680 struct osd_object *cobj,
2684 struct dentry *child;
2685 struct osd_thandle *oth;
2686 struct inode *cinode = cobj->oo_inode;
2689 oth = container_of(th, struct osd_thandle, ot_super);
2690 LASSERT(oth->ot_handle != NULL);
2691 LASSERT(oth->ot_handle->h_transaction != NULL);
2693 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2694 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2700 * It will call the appropriate osd_add* function and return the
2701 * value, return by respective functions.
2703 static int osd_ea_add_rec(const struct lu_env *env,
2704 struct osd_object *pobj,
2705 struct osd_object *cobj,
2709 struct osd_thread_info *info = osd_oti_get(env);
2712 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
2714 rc = osd_add_dot_dotdot(info, pobj, cobj, name, th);
2716 rc = __osd_ea_add_rec(info, pobj, cobj, name, th);
2722 * Calls ->lookup() to find dentry. From dentry get inode and
2723 * read inode's ea to get fid. This is required for interoperability
2726 * \retval 0, on success
2727 * \retval -ve, on error
2729 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
2730 struct dt_rec *rec, const struct dt_key *key)
2732 struct inode *dir = obj->oo_inode;
2733 struct osd_thread_info *info = osd_oti_get(env);
2734 struct dentry *dentry;
2735 struct osd_device *dev = osd_dev(obj->oo_dt.do_lu.lo_dev);
2736 struct osd_inode_id *id = &info->oti_id;
2737 struct ldiskfs_dir_entry_2 *de;
2738 struct buffer_head *bh;
2739 struct lu_fid * fid = &info->oti_fid;
2740 struct inode *inode;
2744 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
2746 dentry = osd_child_dentry_get(env, obj,
2747 (char *)key, strlen((char *)key));
2749 down_read(&obj->oo_ext_idx_sem);
2750 bh = ldiskfs_find_entry(dentry, &de);
2752 ino = le32_to_cpu(de->inode);
2755 id->oii_gen = OSD_OII_NOGEN;
2757 inode = osd_iget(info, dev, id);
2758 if (!IS_ERR(inode)) {
2759 dentry->d_inode = inode;
2761 rc = osd_ea_fid_get(env, dentry, fid);
2763 osd_fid_pack(env, fid, rec);
2767 rc = PTR_ERR(inode);
2771 up_read(&obj->oo_ext_idx_sem);
2776 * Find the osd object for given fid.
2778 * \param fid, need to find the osd object having this fid
2780 * \retval osd_object, on success
2781 * \retval -ve, on error
2783 struct osd_object *osd_object_find(const struct lu_env *env,
2784 struct dt_object *dt,
2785 const struct lu_fid *fid)
2787 struct lu_device *ludev = dt->do_lu.lo_dev;
2788 struct osd_object *child = NULL;
2789 struct lu_object *luch;
2790 struct lu_object *lo;
2792 luch = lu_object_find(env, ludev, fid, NULL);
2793 if (!IS_ERR(luch)) {
2794 if (lu_object_exists(luch)) {
2795 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
2797 child = osd_obj(lo);
2799 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2800 "lu_object can't be located"
2801 ""DFID"\n", PFID(fid));
2803 if (child == NULL) {
2804 lu_object_put(env, luch);
2805 CERROR("Unable to get osd_object\n");
2806 child = ERR_PTR(-ENOENT);
2809 LU_OBJECT_DEBUG(D_ERROR, env, luch,
2810 "lu_object does not exists "DFID"\n",
2812 child = ERR_PTR(-ENOENT);
2815 child = (void *)luch;
2821 * Put the osd object once done with it.
2823 * \param obj, osd object that needs to be put
2825 static inline void osd_object_put(const struct lu_env *env,
2826 struct osd_object *obj)
2828 lu_object_put(env, &obj->oo_dt.do_lu);
2832 * Index add function for interoperability mode (b11826).
2833 * It will add the directory entry.This entry is needed to
2834 * maintain name->fid mapping.
2836 * \param key, it is key i.e. file entry to be inserted
2837 * \param rec, it is value of given key i.e. fid
2839 * \retval 0, on success
2840 * \retval -ve, on error
2842 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
2843 const struct dt_rec *rec,
2844 const struct dt_key *key, struct thandle *th,
2845 struct lustre_capa *capa, int ignore_quota)
2847 struct osd_object *obj = osd_dt_obj(dt);
2848 struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
2849 const struct lu_fid_pack *pack = (const struct lu_fid_pack *)rec;
2850 const char *name = (const char *)key;
2851 struct osd_object *child;
2852 #ifdef HAVE_QUOTA_SUPPORT
2853 cfs_cap_t save = current->cap_effective;
2859 LASSERT(osd_invariant(obj));
2860 LASSERT(dt_object_exists(dt));
2861 LASSERT(th != NULL);
2863 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2866 rc = fid_unpack(pack, fid);
2869 child = osd_object_find(env, dt, fid);
2870 if (!IS_ERR(child)) {
2871 struct inode *inode = obj->oo_inode;
2872 struct osd_thread_info *oti = osd_oti_get(env);
2873 struct timespec *ctime = &oti->oti_time;
2874 struct timespec *mtime = &oti->oti_time2;
2876 *ctime = inode->i_ctime;
2877 *mtime = inode->i_mtime;
2878 #ifdef HAVE_QUOTA_SUPPORT
2880 current->cap_effective |= CFS_CAP_SYS_RESOURCE_MASK;
2882 current->cap_effective &= ~CFS_CAP_SYS_RESOURCE_MASK;
2884 down_write(&obj->oo_ext_idx_sem);
2885 rc = osd_ea_add_rec(env, obj, child, name, th);
2886 up_write(&obj->oo_ext_idx_sem);
2887 #ifdef HAVE_QUOTA_SUPPORT
2888 current->cap_effective = save;
2890 osd_object_put(env, child);
2891 /* xtime should not be updated with server-side time. */
2892 spin_lock(&obj->oo_guard);
2893 inode->i_ctime = *ctime;
2894 inode->i_mtime = *mtime;
2895 spin_unlock(&obj->oo_guard);
2896 mark_inode_dirty(inode);
2898 rc = PTR_ERR(child);
2901 LASSERT(osd_invariant(obj));
2906 * Initialize osd Iterator for given osd index object.
2908 * \param dt osd index object
2911 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
2912 struct dt_object *dt,
2913 struct lustre_capa *capa)
2915 struct osd_it_iam *it;
2916 struct osd_thread_info *oti = osd_oti_get(env);
2917 struct osd_object *obj = osd_dt_obj(dt);
2918 struct lu_object *lo = &dt->do_lu;
2919 struct iam_path_descr *ipd;
2920 struct iam_container *bag = &obj->oo_dir->od_container;
2922 LASSERT(lu_object_exists(lo));
2924 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2925 return ERR_PTR(-EACCES);
2928 ipd = osd_it_ipd_get(env, bag);
2929 if (likely(ipd != NULL)) {
2933 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
2934 return (struct dt_it *)it;
2936 return ERR_PTR(-ENOMEM);
2940 * free given Iterator.
2943 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
2945 struct osd_it_iam *it = (struct osd_it_iam *)di;
2946 struct osd_object *obj = it->oi_obj;
2948 iam_it_fini(&it->oi_it);
2949 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
2950 lu_object_put(env, &obj->oo_dt.do_lu);
2954 * Move Iterator to record specified by \a key
2956 * \param di osd iterator
2957 * \param key key for index
2959 * \retval +ve di points to record with least key not larger than key
2960 * \retval 0 di points to exact matched key
2961 * \retval -ve failure
2964 static int osd_it_iam_get(const struct lu_env *env,
2965 struct dt_it *di, const struct dt_key *key)
2967 struct osd_it_iam *it = (struct osd_it_iam *)di;
2969 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
2975 * \param di osd iterator
2978 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
2980 struct osd_it_iam *it = (struct osd_it_iam *)di;
2982 iam_it_put(&it->oi_it);
2986 * Move iterator by one record
2988 * \param di osd iterator
2990 * \retval +1 end of container reached
2992 * \retval -ve failure
2995 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
2997 struct osd_it_iam *it = (struct osd_it_iam *)di;
2999 return iam_it_next(&it->oi_it);
3003 * Return pointer to the key under iterator.
3006 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3007 const struct dt_it *di)
3009 struct osd_it_iam *it = (struct osd_it_iam *)di;
3011 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3015 * Return size of key under iterator (in bytes)
3018 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3020 struct osd_it_iam *it = (struct osd_it_iam *)di;
3022 return iam_it_key_size(&it->oi_it);
3025 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3030 struct luda_type *lt;
3031 const unsigned align = sizeof(struct luda_type) - 1;
3033 /* check if file type is required */
3034 if (attr & LUDA_TYPE) {
3035 len = (len + align) & ~align;
3037 lt = (void *) ent->lde_name + len;
3038 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3039 ent->lde_attrs |= LUDA_TYPE;
3042 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3046 * build lu direct from backend fs dirent.
3049 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3057 fid_cpu_to_le(&ent->lde_fid, fid);
3058 ent->lde_attrs = LUDA_FID;
3060 ent->lde_hash = cpu_to_le64(offset);
3061 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3063 strncpy(ent->lde_name, name, namelen);
3064 ent->lde_namelen = cpu_to_le16(namelen);
3066 /* append lustre attributes */
3067 osd_it_append_attrs(ent, attr, namelen, type);
3071 * Return pointer to the record under iterator.
3073 static int osd_it_iam_rec(const struct lu_env *env,
3074 const struct dt_it *di,
3075 struct lu_dirent *lde,
3078 struct osd_it_iam *it = (struct osd_it_iam *)di;
3079 struct osd_thread_info *info = osd_oti_get(env);
3080 struct lu_fid *fid = &info->oti_fid;
3081 const struct lu_fid_pack *rec;
3087 name = (char *)iam_it_key_get(&it->oi_it);
3089 RETURN(PTR_ERR(name));
3091 namelen = iam_it_key_size(&it->oi_it);
3093 rec = (const struct lu_fid_pack *) iam_it_rec_get(&it->oi_it);
3095 RETURN(PTR_ERR(rec));
3097 rc = fid_unpack(rec, fid);
3101 hash = iam_it_store(&it->oi_it);
3103 /* IAM does not store object type in IAM index (dir) */
3104 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3111 * Returns cookie for current Iterator position.
3113 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3115 struct osd_it_iam *it = (struct osd_it_iam *)di;
3117 return iam_it_store(&it->oi_it);
3121 * Restore iterator from cookie.
3123 * \param di osd iterator
3124 * \param hash Iterator location cookie
3126 * \retval +ve di points to record with least key not larger than key.
3127 * \retval 0 di points to exact matched key
3128 * \retval -ve failure
3131 static int osd_it_iam_load(const struct lu_env *env,
3132 const struct dt_it *di, __u64 hash)
3134 struct osd_it_iam *it = (struct osd_it_iam *)di;
3136 return iam_it_load(&it->oi_it, hash);
3139 static const struct dt_index_operations osd_index_iam_ops = {
3140 .dio_lookup = osd_index_iam_lookup,
3141 .dio_insert = osd_index_iam_insert,
3142 .dio_delete = osd_index_iam_delete,
3144 .init = osd_it_iam_init,
3145 .fini = osd_it_iam_fini,
3146 .get = osd_it_iam_get,
3147 .put = osd_it_iam_put,
3148 .next = osd_it_iam_next,
3149 .key = osd_it_iam_key,
3150 .key_size = osd_it_iam_key_size,
3151 .rec = osd_it_iam_rec,
3152 .store = osd_it_iam_store,
3153 .load = osd_it_iam_load
3158 * Creates or initializes iterator context.
3160 * \retval struct osd_it_ea, iterator structure on success
3163 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3164 struct dt_object *dt,
3165 struct lustre_capa *capa)
3167 struct osd_object *obj = osd_dt_obj(dt);
3168 struct osd_thread_info *info = osd_oti_get(env);
3169 struct osd_it_ea *it = &info->oti_it_ea;
3170 struct lu_object *lo = &dt->do_lu;
3171 struct dentry *obj_dentry = &info->oti_it_dentry;
3173 LASSERT(lu_object_exists(lo));
3175 obj_dentry->d_inode = obj->oo_inode;
3176 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3177 obj_dentry->d_name.hash = 0;
3179 it->oie_rd_dirent = 0;
3180 it->oie_it_dirent = 0;
3181 it->oie_curr_pos = 0;
3182 it->oie_next_pos = 0;
3183 it->oie_dirent = NULL;
3184 it->oie_buf = info->oti_it_ea_buf;
3186 it->oie_file.f_dentry = obj_dentry;
3187 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3188 it->oie_file.f_op = obj->oo_inode->i_fop;
3189 it->oie_file.private_data = NULL;
3191 RETURN((struct dt_it*) it);
3195 * Destroy or finishes iterator context.
3197 * \param di, struct osd_it_ea, iterator structure to be destroyed
3199 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3201 struct osd_it_ea *it = (struct osd_it_ea *)di;
3202 struct osd_object *obj = it->oie_obj;
3203 struct inode *inode = obj->oo_inode;
3206 it->oie_file.f_op->release(inode, &it->oie_file);
3207 lu_object_put(env, &obj->oo_dt.do_lu);
3212 * It position the iterator at given key, so that next lookup continues from
3213 * that key Or it is similar to dio_it->load() but based on a key,
3214 * rather than file position.
3216 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3219 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3221 static int osd_it_ea_get(const struct lu_env *env,
3222 struct dt_it *di, const struct dt_key *key)
3224 struct osd_it_ea *it = (struct osd_it_ea *)di;
3227 LASSERT(((const char *)key)[0] == '\0');
3228 it->oie_curr_pos = 0;
3229 it->oie_next_pos = 0;
3230 it->oie_rd_dirent = 0;
3231 it->oie_it_dirent = 0;
3232 it->oie_dirent = NULL;
3240 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3245 * It is called internally by ->readdir(). It fills the
3246 * iterator's in-memory data structure with required
3247 * information i.e. name, namelen, rec_size etc.
3249 * \param buf, in which information to be filled in.
3250 * \param name, name of the file in given dir
3252 * \retval 0, on success
3253 * \retval 1, on buffer full
3255 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3256 loff_t offset, ino_t ino,
3257 unsigned int d_type)
3259 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3260 struct osd_it_ea_dirent *ent = it->oie_dirent;
3263 /* this should never happen */
3264 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3265 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3269 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3273 ent->oied_ino = ino;
3274 ent->oied_off = offset;
3275 ent->oied_namelen = namelen;
3276 ent->oied_type = d_type;
3278 memcpy(ent->oied_name, name, namelen);
3280 it->oie_rd_dirent++;
3281 it->oie_dirent = (void *) ent + size_round(sizeof(*ent) + namelen);
3286 * Calls ->readdir() to load a directory entry at a time
3287 * and stored it in iterator's in-memory data structure.
3289 * \param di, struct osd_it_ea, iterator's in memory structure
3291 * \retval 0, on success
3292 * \retval -ve, on error
3294 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3296 struct osd_it_ea *it = (struct osd_it_ea *)di;
3297 struct osd_object *obj = it->oie_obj;
3298 struct inode *inode = obj->oo_inode;
3302 it->oie_dirent = it->oie_buf;
3303 it->oie_rd_dirent = 0;
3304 it->oie_file.f_pos = it->oie_curr_pos;
3306 down_read(&obj->oo_ext_idx_sem);
3307 result = inode->i_fop->readdir(&it->oie_file, it,
3308 (filldir_t) osd_ldiskfs_filldir);
3310 up_read(&obj->oo_ext_idx_sem);
3311 it->oie_next_pos = it->oie_file.f_pos;
3313 if (it->oie_rd_dirent == 0) {
3316 it->oie_dirent = it->oie_buf;
3317 it->oie_it_dirent = 1;
3324 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3325 * to load a directory entry at a time and stored it in
3326 * iterator's in-memory data structure.
3328 * \param di, struct osd_it_ea, iterator's in memory structure
3330 * \retval +ve, iterator reached to end
3331 * \retval 0, iterator not reached to end
3332 * \retval -ve, on error
3334 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3336 struct osd_it_ea *it = (struct osd_it_ea *)di;
3341 if (it->oie_it_dirent < it->oie_rd_dirent) {
3342 it->oie_dirent = (void *) it->oie_dirent +
3343 size_round(sizeof(struct osd_it_ea_dirent) +
3344 it->oie_dirent->oied_namelen);
3345 it->oie_it_dirent++;
3348 it->oie_curr_pos = it->oie_next_pos;
3350 if (it->oie_curr_pos == LDISKFS_HTREE_EOF)
3353 rc = osd_ldiskfs_it_fill(di);
3360 * Returns the key at current position from iterator's in memory structure.
3362 * \param di, struct osd_it_ea, iterator's in memory structure
3364 * \retval key i.e. struct dt_key on success
3366 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3367 const struct dt_it *di)
3369 struct osd_it_ea *it = (struct osd_it_ea *)di;
3371 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3375 * Returns the key's size at current position from iterator's in memory structure.
3377 * \param di, struct osd_it_ea, iterator's in memory structure
3379 * \retval key_size i.e. struct dt_key on success
3381 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3383 struct osd_it_ea *it = (struct osd_it_ea *)di;
3385 RETURN(it->oie_dirent->oied_namelen);
3390 * Returns the value (i.e. fid/igif) at current position from iterator's
3391 * in memory structure.
3393 * \param di, struct osd_it_ea, iterator's in memory structure
3394 * \param attr, attr requested for dirent.
3395 * \param lde, lustre dirent
3397 * \retval 0, no error and \param lde has correct lustre dirent.
3398 * \retval -ve, on error
3400 static inline int osd_it_ea_rec(const struct lu_env *env,
3401 const struct dt_it *di,
3402 struct lu_dirent *lde,
3405 struct osd_it_ea *it = (struct osd_it_ea *)di;
3406 struct osd_object *obj = it->oie_obj;
3407 struct osd_thread_info *info = osd_oti_get(env);
3408 struct osd_inode_id *id = &info->oti_id;
3409 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3410 struct dentry *dentry = &info->oti_child_dentry;
3411 struct osd_device *dev;
3412 struct lu_fid *fid = &info->oti_fid;
3413 struct inode *inode;
3417 dev = osd_dev(ldev);
3418 id->oii_ino = it->oie_dirent->oied_ino;
3419 id->oii_gen = OSD_OII_NOGEN;
3420 inode = osd_iget(info, dev, id);
3421 if (!IS_ERR(inode)) {
3422 dentry->d_inode = inode;
3423 LASSERT(dentry->d_inode->i_sb == osd_sb(dev));
3425 RETURN(PTR_ERR(inode));
3428 rc = osd_ea_fid_get(env, dentry, fid);
3431 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3432 it->oie_dirent->oied_name,
3433 it->oie_dirent->oied_namelen,
3434 it->oie_dirent->oied_type,
3441 * Returns a cookie for current position of the iterator head, so that
3442 * user can use this cookie to load/start the iterator next time.
3444 * \param di, struct osd_it_ea, iterator's in memory structure
3446 * \retval cookie for current position, on success
3448 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3450 struct osd_it_ea *it = (struct osd_it_ea *)di;
3452 RETURN(it->oie_dirent->oied_off);
3456 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3457 * to load a directory entry at a time and stored it i inn,
3458 * in iterator's in-memory data structure.
3460 * \param di, struct osd_it_ea, iterator's in memory structure
3462 * \retval +ve, on success
3463 * \retval -ve, on error
3465 static int osd_it_ea_load(const struct lu_env *env,
3466 const struct dt_it *di, __u64 hash)
3468 struct osd_it_ea *it = (struct osd_it_ea *)di;
3472 it->oie_curr_pos = hash;
3474 rc = osd_ldiskfs_it_fill(di);
3481 * Index and Iterator operations for interoperability
3482 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3484 static const struct dt_index_operations osd_index_ea_ops = {
3485 .dio_lookup = osd_index_ea_lookup,
3486 .dio_insert = osd_index_ea_insert,
3487 .dio_delete = osd_index_ea_delete,
3489 .init = osd_it_ea_init,
3490 .fini = osd_it_ea_fini,
3491 .get = osd_it_ea_get,
3492 .put = osd_it_ea_put,
3493 .next = osd_it_ea_next,
3494 .key = osd_it_ea_key,
3495 .key_size = osd_it_ea_key_size,
3496 .rec = osd_it_ea_rec,
3497 .store = osd_it_ea_store,
3498 .load = osd_it_ea_load
3503 * Index lookup function for interoperability mode (b11826).
3505 * \param key, key i.e. file name to be searched
3507 * \retval +ve, on success
3508 * \retval -ve, on error
3510 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3511 struct dt_rec *rec, const struct dt_key *key,
3512 struct lustre_capa *capa)
3514 struct osd_object *obj = osd_dt_obj(dt);
3519 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3520 LINVRNT(osd_invariant(obj));
3522 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3525 rc = osd_ea_lookup_rec(env, obj, rec, key);
3532 /* type constructor/destructor: osd_type_init, osd_type_fini */
3533 LU_TYPE_INIT_FINI(osd, &osd_key);
3535 static struct lu_context_key osd_key = {
3536 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3537 .lct_init = osd_key_init,
3538 .lct_fini = osd_key_fini,
3539 .lct_exit = osd_key_exit
3542 static void *osd_key_init(const struct lu_context *ctx,
3543 struct lu_context_key *key)
3545 struct osd_thread_info *info;
3547 OBD_ALLOC_PTR(info);
3549 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3550 if (info->oti_it_ea_buf != NULL) {
3551 info->oti_env = container_of(ctx, struct lu_env,
3555 info = ERR_PTR(-ENOMEM);
3558 info = ERR_PTR(-ENOMEM);
3563 static void osd_key_fini(const struct lu_context *ctx,
3564 struct lu_context_key *key, void* data)
3566 struct osd_thread_info *info = data;
3568 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3572 static void osd_key_exit(const struct lu_context *ctx,
3573 struct lu_context_key *key, void *data)
3575 struct osd_thread_info *info = data;
3577 LASSERT(info->oti_r_locks == 0);
3578 LASSERT(info->oti_w_locks == 0);
3579 LASSERT(info->oti_txns == 0);
3582 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3583 const char *name, struct lu_device *next)
3586 struct lu_context *ctx;
3588 /* context for commit hooks */
3589 ctx = &osd_dev(d)->od_env_for_commit.le_ctx;
3590 rc = lu_context_init(ctx, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
3592 rc = osd_procfs_init(osd_dev(d), name);
3593 ctx->lc_cookie = 0x3;
3598 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3600 struct osd_thread_info *info = osd_oti_get(env);
3602 if (o->od_obj_area != NULL) {
3603 lu_object_put(env, &o->od_obj_area->do_lu);
3604 o->od_obj_area = NULL;
3606 osd_oi_fini(info, &o->od_oi);
3611 static int osd_mount(const struct lu_env *env,
3612 struct osd_device *o, struct lustre_cfg *cfg)
3614 struct lustre_mount_info *lmi;
3615 const char *dev = lustre_cfg_string(cfg, 0);
3616 struct lustre_disk_data *ldd;
3617 struct lustre_sb_info *lsi;
3621 if (o->od_mount != NULL) {
3622 CERROR("Already mounted (%s)\n", dev);
3627 lmi = server_get_mount(dev);
3629 CERROR("Cannot get mount info for %s!\n", dev);
3633 LASSERT(lmi != NULL);
3634 /* save lustre_mount_info in dt_device */
3637 lsi = s2lsi(lmi->lmi_sb);
3640 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3642 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3646 o->od_obj_area = NULL;
3650 static struct lu_device *osd_device_fini(const struct lu_env *env,
3651 struct lu_device *d)
3656 shrink_dcache_sb(osd_sb(osd_dev(d)));
3657 osd_sync(env, lu2dt_dev(d));
3659 rc = osd_procfs_fini(osd_dev(d));
3661 CERROR("proc fini error %d \n", rc);
3662 RETURN (ERR_PTR(rc));
3665 if (osd_dev(d)->od_mount)
3666 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3667 osd_dev(d)->od_mount->lmi_mnt);
3668 osd_dev(d)->od_mount = NULL;
3670 lu_context_fini(&osd_dev(d)->od_env_for_commit.le_ctx);
3674 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3675 struct lu_device_type *t,
3676 struct lustre_cfg *cfg)
3678 struct lu_device *l;
3679 struct osd_device *o;
3685 result = dt_device_init(&o->od_dt_dev, t);
3688 l->ld_ops = &osd_lu_ops;
3689 o->od_dt_dev.dd_ops = &osd_dt_ops;
3690 spin_lock_init(&o->od_osfs_lock);
3691 o->od_osfs_age = cfs_time_shift_64(-1000);
3692 o->od_capa_hash = init_capa_hash();
3693 if (o->od_capa_hash == NULL) {
3694 dt_device_fini(&o->od_dt_dev);
3695 l = ERR_PTR(-ENOMEM);
3698 l = ERR_PTR(result);
3703 l = ERR_PTR(-ENOMEM);
3707 static struct lu_device *osd_device_free(const struct lu_env *env,
3708 struct lu_device *d)
3710 struct osd_device *o = osd_dev(d);
3713 cleanup_capa_hash(o->od_capa_hash);
3714 dt_device_fini(&o->od_dt_dev);
3719 static int osd_process_config(const struct lu_env *env,
3720 struct lu_device *d, struct lustre_cfg *cfg)
3722 struct osd_device *o = osd_dev(d);
3726 switch(cfg->lcfg_command) {
3728 err = osd_mount(env, o, cfg);
3731 err = osd_shutdown(env, o);
3740 extern void ldiskfs_orphan_cleanup (struct super_block * sb,
3741 struct ldiskfs_super_block * es);
3743 static int osd_recovery_complete(const struct lu_env *env,
3744 struct lu_device *d)
3746 struct osd_device *o = osd_dev(d);
3748 /* TODO: orphans handling */
3749 ldiskfs_orphan_cleanup(osd_sb(o), LDISKFS_SB(osd_sb(o))->s_es);
3753 static int osd_prepare(const struct lu_env *env,
3754 struct lu_device *pdev,
3755 struct lu_device *dev)
3757 struct osd_device *osd = osd_dev(dev);
3758 struct lustre_sb_info *lsi;
3759 struct lustre_disk_data *ldd;
3760 struct lustre_mount_info *lmi;
3761 struct osd_thread_info *oti = osd_oti_get(env);
3762 struct dt_object *d;
3766 /* 1. initialize oi before any file create or file open */
3767 result = osd_oi_init(oti, &osd->od_oi,
3768 &osd->od_dt_dev, lu2md_dev(pdev));
3772 lmi = osd->od_mount;
3773 lsi = s2lsi(lmi->lmi_sb);
3776 /* 2. setup local objects */
3777 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
3781 /* 3. open remote object dir */
3782 d = dt_store_open(env, lu2dt_dev(dev), "",
3783 remote_obj_dir, &oti->oti_fid);
3785 osd->od_obj_area = d;
3788 result = PTR_ERR(d);
3789 osd->od_obj_area = NULL;
3796 static struct inode *osd_iget(struct osd_thread_info *info,
3797 struct osd_device *dev,
3798 const struct osd_inode_id *id)
3800 struct inode *inode;
3802 inode = iget(osd_sb(dev), id->oii_ino);
3803 if (inode == NULL) {
3804 CERROR("no inode\n");
3805 inode = ERR_PTR(-EACCES);
3806 } else if (id->oii_gen != OSD_OII_NOGEN &&
3807 inode->i_generation != id->oii_gen) {
3809 inode = ERR_PTR(-ESTALE);
3810 } else if (inode->i_nlink == 0) {
3811 /* due to parallel readdir and unlink,
3812 * we can have dead inode here. */
3813 CWARN("stale inode\n");
3814 make_bad_inode(inode);
3816 inode = ERR_PTR(-ESTALE);
3817 } else if (is_bad_inode(inode)) {
3818 CERROR("bad inode %lx\n",inode->i_ino);
3820 inode = ERR_PTR(-ENOENT);
3826 static int osd_fid_lookup(const struct lu_env *env,
3827 struct osd_object *obj, const struct lu_fid *fid)
3829 struct osd_thread_info *info;
3830 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
3831 struct osd_device *dev;
3832 struct osd_inode_id *id;
3834 struct inode *inode;
3837 LINVRNT(osd_invariant(obj));
3838 LASSERT(obj->oo_inode == NULL);
3839 LASSERT(fid_is_sane(fid));
3841 * This assertion checks that osd layer sees only local
3842 * fids. Unfortunately it is somewhat expensive (does a
3843 * cache-lookup). Disabling it for production/acceptance-testing.
3845 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
3849 info = osd_oti_get(env);
3850 dev = osd_dev(ldev);
3854 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
3857 result = osd_oi_lookup(info, oi, fid, id);
3859 inode = osd_iget(info, dev, id);
3860 if (!IS_ERR(inode)) {
3861 obj->oo_inode = inode;
3862 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
3863 if (dev->od_iop_mode) {
3864 obj->oo_compat_dot_created = 1;
3865 obj->oo_compat_dotdot_created = 1;
3870 * If fid wasn't found in oi, inode-less object is
3871 * created, for which lu_object_exists() returns
3872 * false. This is used in a (frequent) case when
3873 * objects are created as locking anchors or
3874 * place holders for objects yet to be created.
3876 result = PTR_ERR(inode);
3877 } else if (result == -ENOENT)
3879 LINVRNT(osd_invariant(obj));
3884 static void osd_inode_getattr(const struct lu_env *env,
3885 struct inode *inode, struct lu_attr *attr)
3887 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
3888 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
3889 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
3891 attr->la_atime = LTIME_S(inode->i_atime);
3892 attr->la_mtime = LTIME_S(inode->i_mtime);
3893 attr->la_ctime = LTIME_S(inode->i_ctime);
3894 attr->la_mode = inode->i_mode;
3895 attr->la_size = i_size_read(inode);
3896 attr->la_blocks = inode->i_blocks;
3897 attr->la_uid = inode->i_uid;
3898 attr->la_gid = inode->i_gid;
3899 attr->la_flags = LDISKFS_I(inode)->i_flags;
3900 attr->la_nlink = inode->i_nlink;
3901 attr->la_rdev = inode->i_rdev;
3902 attr->la_blksize = ll_inode_blksize(inode);
3903 attr->la_blkbits = inode->i_blkbits;
3910 static int lu_device_is_osd(const struct lu_device *d)
3912 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
3915 static struct osd_object *osd_obj(const struct lu_object *o)
3917 LASSERT(lu_device_is_osd(o->lo_dev));
3918 return container_of0(o, struct osd_object, oo_dt.do_lu);
3921 static struct osd_device *osd_dt_dev(const struct dt_device *d)
3923 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
3924 return container_of0(d, struct osd_device, od_dt_dev);
3927 static struct osd_device *osd_dev(const struct lu_device *d)
3929 LASSERT(lu_device_is_osd(d));
3930 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
3933 static struct osd_object *osd_dt_obj(const struct dt_object *d)
3935 return osd_obj(&d->do_lu);
3938 static struct osd_device *osd_obj2dev(const struct osd_object *o)
3940 return osd_dev(o->oo_dt.do_lu.lo_dev);
3943 static struct lu_device *osd2lu_dev(struct osd_device *osd)
3945 return &osd->od_dt_dev.dd_lu_dev;
3948 static struct super_block *osd_sb(const struct osd_device *dev)
3950 return dev->od_mount->lmi_mnt->mnt_sb;
3953 static journal_t *osd_journal(const struct osd_device *dev)
3955 return LDISKFS_SB(osd_sb(dev))->s_journal;
3958 static int osd_has_index(const struct osd_object *obj)
3960 return obj->oo_dt.do_index_ops != NULL;
3963 static int osd_object_invariant(const struct lu_object *l)
3965 return osd_invariant(osd_obj(l));
3968 static const struct lu_object_operations osd_lu_obj_ops = {
3969 .loo_object_init = osd_object_init,
3970 .loo_object_delete = osd_object_delete,
3971 .loo_object_release = osd_object_release,
3972 .loo_object_free = osd_object_free,
3973 .loo_object_print = osd_object_print,
3974 .loo_object_invariant = osd_object_invariant
3977 static const struct lu_device_operations osd_lu_ops = {
3978 .ldo_object_alloc = osd_object_alloc,
3979 .ldo_process_config = osd_process_config,
3980 .ldo_recovery_complete = osd_recovery_complete,
3981 .ldo_prepare = osd_prepare,
3984 static const struct lu_device_type_operations osd_device_type_ops = {
3985 .ldto_init = osd_type_init,
3986 .ldto_fini = osd_type_fini,
3988 .ldto_start = osd_type_start,
3989 .ldto_stop = osd_type_stop,
3991 .ldto_device_alloc = osd_device_alloc,
3992 .ldto_device_free = osd_device_free,
3994 .ldto_device_init = osd_device_init,
3995 .ldto_device_fini = osd_device_fini
3998 static struct lu_device_type osd_device_type = {
3999 .ldt_tags = LU_DEVICE_DT,
4000 .ldt_name = LUSTRE_OSD_NAME,
4001 .ldt_ops = &osd_device_type_ops,
4002 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4006 * lprocfs legacy support.
4008 static struct obd_ops osd_obd_device_ops = {
4009 .o_owner = THIS_MODULE
4012 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4013 .llod_name = remote_obj_dir,
4014 .llod_oid = OSD_REM_OBJ_DIR_OID,
4016 .llod_feat = &dt_directory_features,
4019 static int __init osd_mod_init(void)
4021 struct lprocfs_static_vars lvars;
4024 llo_local_obj_register(&llod_osd_rem_obj_dir);
4025 lprocfs_osd_init_vars(&lvars);
4026 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4027 LUSTRE_OSD_NAME, &osd_device_type);
4030 static void __exit osd_mod_exit(void)
4032 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4033 class_unregister_type(LUSTRE_OSD_NAME);
4036 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4037 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4038 MODULE_LICENSE("GPL");
4040 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);