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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * Copyright (c) 2011 Whamcloud, Inc.
39 * This file is part of Lustre, http://www.lustre.org/
40 * Lustre is a trademark of Sun Microsystems, Inc.
42 * lustre/osd/osd_handler.c
44 * Top-level entry points into osd module
46 * Author: Nikita Danilov <nikita@clusterfs.com>
47 * Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
51 # define EXPORT_SYMTAB
53 #define DEBUG_SUBSYSTEM S_MDS
55 #include <linux/module.h>
57 /* LUSTRE_VERSION_CODE */
58 #include <lustre_ver.h>
59 /* prerequisite for linux/xattr.h */
60 #include <linux/types.h>
61 /* prerequisite for linux/xattr.h */
63 /* XATTR_{REPLACE,CREATE} */
64 #include <linux/xattr.h>
69 * struct OBD_{ALLOC,FREE}*()
72 #include <obd_support.h>
73 /* struct ptlrpc_thread */
74 #include <lustre_net.h>
77 #include <lustre_fid.h>
79 #include "osd_internal.h"
82 /* llo_* api support */
83 #include <md_object.h>
85 static const char dot[] = ".";
86 static const char dotdot[] = "..";
87 static const char remote_obj_dir[] = "REM_OBJ_DIR";
89 struct osd_directory {
90 struct iam_container od_container;
91 struct iam_descr od_descr;
95 struct dt_object oo_dt;
97 * Inode for file system object represented by this osd_object. This
98 * inode is pinned for the whole duration of lu_object life.
100 * Not modified concurrently (either setup early during object
101 * creation, or assigned by osd_object_create() under write lock).
103 struct inode *oo_inode;
105 * to protect index ops.
107 cfs_rw_semaphore_t oo_ext_idx_sem;
108 cfs_rw_semaphore_t oo_sem;
109 struct osd_directory *oo_dir;
110 /** protects inode attributes. */
111 cfs_spinlock_t oo_guard;
113 * Following two members are used to indicate the presence of dot and
114 * dotdot in the given directory. This is required for interop mode
117 int oo_compat_dot_created;
118 int oo_compat_dotdot_created;
120 const struct lu_env *oo_owner;
121 #ifdef CONFIG_LOCKDEP
122 struct lockdep_map oo_dep_map;
126 static const struct lu_object_operations osd_lu_obj_ops;
127 static const struct lu_device_operations osd_lu_ops;
128 static struct lu_context_key osd_key;
129 static const struct dt_object_operations osd_obj_ops;
130 static const struct dt_object_operations osd_obj_ea_ops;
131 static const struct dt_body_operations osd_body_ops;
132 static const struct dt_index_operations osd_index_iam_ops;
133 static const struct dt_index_operations osd_index_ea_ops;
136 struct thandle ot_super;
138 struct journal_callback ot_jcb;
139 cfs_list_t ot_dcb_list;
140 /* Link to the device, for debugging. */
141 struct lu_ref_link *ot_dev_link;
143 #if OSD_THANDLE_STATS
144 /** time when this handle was allocated */
145 cfs_time_t oth_alloced;
147 /** time when this thanle was started */
148 cfs_time_t oth_started;
155 static int lu_device_is_osd(const struct lu_device *d)
157 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
160 static struct osd_device *osd_dt_dev(const struct dt_device *d)
162 LASSERT(lu_device_is_osd(&d->dd_lu_dev));
163 return container_of0(d, struct osd_device, od_dt_dev);
166 static struct osd_device *osd_dev(const struct lu_device *d)
168 LASSERT(lu_device_is_osd(d));
169 return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
172 static struct osd_device *osd_obj2dev(const struct osd_object *o)
174 return osd_dev(o->oo_dt.do_lu.lo_dev);
177 static struct super_block *osd_sb(const struct osd_device *dev)
179 return dev->od_mount->lmi_mnt->mnt_sb;
182 static int osd_object_is_root(const struct osd_object *obj)
184 return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
187 static struct osd_object *osd_obj(const struct lu_object *o)
189 LASSERT(lu_device_is_osd(o->lo_dev));
190 return container_of0(o, struct osd_object, oo_dt.do_lu);
193 static struct osd_object *osd_dt_obj(const struct dt_object *d)
195 return osd_obj(&d->do_lu);
198 static struct lu_device *osd2lu_dev(struct osd_device *osd)
200 return &osd->od_dt_dev.dd_lu_dev;
203 static journal_t *osd_journal(const struct osd_device *dev)
205 return LDISKFS_SB(osd_sb(dev))->s_journal;
208 static int osd_has_index(const struct osd_object *obj)
210 return obj->oo_dt.do_index_ops != NULL;
213 static int osd_object_invariant(const struct lu_object *l)
215 return osd_invariant(osd_obj(l));
218 #ifdef HAVE_QUOTA_SUPPORT
220 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
222 struct md_ucred *uc = md_ucred(env);
227 save->oc_uid = current_fsuid();
228 save->oc_gid = current_fsgid();
229 save->oc_cap = current_cap();
230 if ((tc = prepare_creds())) {
231 tc->fsuid = uc->mu_fsuid;
232 tc->fsgid = uc->mu_fsgid;
235 /* XXX not suboptimal */
236 cfs_curproc_cap_unpack(uc->mu_cap);
240 osd_pop_ctxt(struct osd_ctxt *save)
244 if ((tc = prepare_creds())) {
245 tc->fsuid = save->oc_uid;
246 tc->fsgid = save->oc_gid;
247 tc->cap_effective = save->oc_cap;
253 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
255 return lu_context_key_get(&env->le_ctx, &osd_key);
259 * Concurrency: doesn't matter
261 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
263 return osd_oti_get(env)->oti_r_locks > 0;
267 * Concurrency: doesn't matter
269 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
271 struct osd_thread_info *oti = osd_oti_get(env);
272 return oti->oti_w_locks > 0 && o->oo_owner == env;
276 * Concurrency: doesn't access mutable data
278 static int osd_root_get(const struct lu_env *env,
279 struct dt_device *dev, struct lu_fid *f)
283 inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
284 LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
289 * OSD object methods.
293 * Concurrency: no concurrent access is possible that early in object
296 static struct lu_object *osd_object_alloc(const struct lu_env *env,
297 const struct lu_object_header *hdr,
300 struct osd_object *mo;
306 l = &mo->oo_dt.do_lu;
307 dt_object_init(&mo->oo_dt, NULL, d);
308 if (osd_dev(d)->od_iop_mode)
309 mo->oo_dt.do_ops = &osd_obj_ea_ops;
311 mo->oo_dt.do_ops = &osd_obj_ops;
313 l->lo_ops = &osd_lu_obj_ops;
314 cfs_init_rwsem(&mo->oo_sem);
315 cfs_init_rwsem(&mo->oo_ext_idx_sem);
316 cfs_spin_lock_init(&mo->oo_guard);
323 * retrieve object from backend ext fs.
325 static struct inode *osd_iget(struct osd_thread_info *info,
326 struct osd_device *dev,
327 const struct osd_inode_id *id)
329 struct inode *inode = NULL;
331 #ifdef HAVE_EXT4_LDISKFS
332 inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
334 /* Newer kernels return an error instead of a NULL pointer */
337 inode = iget(osd_sb(dev), id->oii_ino);
340 CERROR("no inode\n");
341 inode = ERR_PTR(-EACCES);
342 } else if (id->oii_gen != OSD_OII_NOGEN &&
343 inode->i_generation != id->oii_gen) {
345 inode = ERR_PTR(-ESTALE);
346 } else if (inode->i_nlink == 0) {
347 /* due to parallel readdir and unlink,
348 * we can have dead inode here. */
349 CWARN("stale inode\n");
350 make_bad_inode(inode);
352 inode = ERR_PTR(-ESTALE);
353 } else if (is_bad_inode(inode)) {
354 CERROR("bad inode %lx\n",inode->i_ino);
356 inode = ERR_PTR(-ENOENT);
358 /* Do not update file c/mtime in ldiskfs.
359 * NB: we don't have any lock to protect this because we don't
360 * have reference on osd_object now, but contention with
361 * another lookup + attr_set can't happen in the tiny window
362 * between if (...) and set S_NOCMTIME. */
363 if (!(inode->i_flags & S_NOCMTIME))
364 inode->i_flags |= S_NOCMTIME;
369 static int osd_fid_lookup(const struct lu_env *env,
370 struct osd_object *obj, const struct lu_fid *fid)
372 struct osd_thread_info *info;
373 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
374 struct osd_device *dev;
375 struct osd_inode_id *id;
380 LINVRNT(osd_invariant(obj));
381 LASSERT(obj->oo_inode == NULL);
382 LASSERT(fid_is_sane(fid) || osd_fid_is_root(fid));
384 * This assertion checks that osd layer sees only local
385 * fids. Unfortunately it is somewhat expensive (does a
386 * cache-lookup). Disabling it for production/acceptance-testing.
388 LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
392 info = osd_oti_get(env);
397 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
400 result = osd_oi_lookup(info, oi, fid, id);
402 inode = osd_iget(info, dev, id);
403 if (!IS_ERR(inode)) {
404 obj->oo_inode = inode;
405 LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
406 if (dev->od_iop_mode) {
407 obj->oo_compat_dot_created = 1;
408 obj->oo_compat_dotdot_created = 1;
413 * If fid wasn't found in oi, inode-less object is
414 * created, for which lu_object_exists() returns
415 * false. This is used in a (frequent) case when
416 * objects are created as locking anchors or
417 * place holders for objects yet to be created.
419 result = PTR_ERR(inode);
420 } else if (result == -ENOENT)
422 LINVRNT(osd_invariant(obj));
428 * Concurrency: shouldn't matter.
430 static void osd_object_init0(struct osd_object *obj)
432 LASSERT(obj->oo_inode != NULL);
433 obj->oo_dt.do_body_ops = &osd_body_ops;
434 obj->oo_dt.do_lu.lo_header->loh_attr |=
435 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
439 * Concurrency: no concurrent access is possible that early in object
442 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
443 const struct lu_object_conf *unused)
445 struct osd_object *obj = osd_obj(l);
448 LINVRNT(osd_invariant(obj));
450 result = osd_fid_lookup(env, obj, lu_object_fid(l));
452 if (obj->oo_inode != NULL)
453 osd_object_init0(obj);
455 LINVRNT(osd_invariant(obj));
460 * Concurrency: no concurrent access is possible that late in object
463 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
465 struct osd_object *obj = osd_obj(l);
467 LINVRNT(osd_invariant(obj));
469 dt_object_fini(&obj->oo_dt);
476 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
477 const struct iam_container *bag)
479 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
480 osd_oti_get(env)->oti_it_ipd);
483 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
484 const struct iam_container *bag)
486 return bag->ic_descr->id_ops->id_ipd_alloc(bag,
487 osd_oti_get(env)->oti_idx_ipd);
490 static void osd_ipd_put(const struct lu_env *env,
491 const struct iam_container *bag,
492 struct iam_path_descr *ipd)
494 bag->ic_descr->id_ops->id_ipd_free(ipd);
498 * Concurrency: no concurrent access is possible that late in object
501 static void osd_index_fini(struct osd_object *o)
503 struct iam_container *bag;
505 if (o->oo_dir != NULL) {
506 bag = &o->oo_dir->od_container;
507 if (o->oo_inode != NULL) {
508 if (bag->ic_object == o->oo_inode)
509 iam_container_fini(bag);
511 OBD_FREE_PTR(o->oo_dir);
517 * Concurrency: no concurrent access is possible that late in object
518 * life-cycle (for all existing callers, that is. New callers have to provide
519 * their own locking.)
521 static int osd_inode_unlinked(const struct inode *inode)
523 return inode->i_nlink == 0;
527 OSD_TXN_OI_DELETE_CREDITS = 20,
528 OSD_TXN_INODE_DELETE_CREDITS = 20
535 #if OSD_THANDLE_STATS
537 * Set time when the handle is allocated
539 static void osd_th_alloced(struct osd_thandle *oth)
541 oth->oth_alloced = cfs_time_current();
545 * Set time when the handle started
547 static void osd_th_started(struct osd_thandle *oth)
549 oth->oth_started = cfs_time_current();
553 * Helper function to convert time interval to microseconds packed in
554 * long int (default time units for the counter in "stats" initialized
555 * by lu_time_init() )
557 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
561 cfs_duration_usec(cfs_time_sub(end, start), &val);
562 return val.tv_sec * 1000000 + val.tv_usec;
566 * Check whether the we deal with this handle for too long.
568 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
569 cfs_time_t alloced, cfs_time_t started,
572 cfs_time_t now = cfs_time_current();
574 LASSERT(dev != NULL);
576 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
577 interval_to_usec(alloced, started));
578 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
579 interval_to_usec(started, closed));
580 lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
581 interval_to_usec(closed, now));
583 if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
584 CWARN("transaction handle %p was open for too long: "
586 "alloced "CFS_TIME_T" ,"
587 "started "CFS_TIME_T" ,"
588 "closed "CFS_TIME_T"\n",
589 oth, now, alloced, started, closed);
590 libcfs_debug_dumpstack(NULL);
594 #define OSD_CHECK_SLOW_TH(oth, dev, expr) \
596 cfs_time_t __closed = cfs_time_current(); \
597 cfs_time_t __alloced = oth->oth_alloced; \
598 cfs_time_t __started = oth->oth_started; \
601 __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
604 #else /* OSD_THANDLE_STATS */
606 #define osd_th_alloced(h) do {} while(0)
607 #define osd_th_started(h) do {} while(0)
608 #define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
610 #endif /* OSD_THANDLE_STATS */
613 * Concurrency: doesn't access mutable data.
615 static int osd_param_is_sane(const struct osd_device *dev,
616 const struct txn_param *param)
618 return param->tp_credits <= osd_journal(dev)->j_max_transaction_buffers;
622 * Concurrency: shouldn't matter.
624 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
625 static void osd_trans_commit_cb(struct super_block *sb,
626 struct journal_callback *jcb, int error)
628 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
631 struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
632 struct thandle *th = &oh->ot_super;
633 struct lu_device *lud = &th->th_dev->dd_lu_dev;
634 struct dt_txn_commit_cb *dcb, *tmp;
636 LASSERT(oh->ot_handle == NULL);
639 CERROR("transaction @0x%p commit error: %d\n", th, error);
641 dt_txn_hook_commit(th);
643 /* call per-transaction callbacks if any */
644 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage)
645 dcb->dcb_func(NULL, th, dcb, error);
647 lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
651 lu_context_exit(&th->th_ctx);
652 lu_context_fini(&th->th_ctx);
657 * Concurrency: shouldn't matter.
659 static struct thandle *osd_trans_start(const struct lu_env *env,
663 struct osd_device *dev = osd_dt_dev(d);
665 struct osd_thandle *oh;
671 hook_res = dt_txn_hook_start(env, d, p);
673 RETURN(ERR_PTR(hook_res));
675 if (osd_param_is_sane(dev, p)) {
676 OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
678 struct osd_thread_info *oti = osd_oti_get(env);
681 * XXX temporary stuff. Some abstraction layer should
685 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
687 jh = ldiskfs_journal_start_sb(osd_sb(dev), p->tp_credits);
695 lu_device_get(&d->dd_lu_dev);
696 oh->ot_dev_link = lu_ref_add
697 (&d->dd_lu_dev.ld_reference,
699 /* add commit callback */
700 lu_context_init(&th->th_ctx, LCT_TX_HANDLE);
701 lu_context_enter(&th->th_ctx);
702 LASSERT(oti->oti_txns == 0);
703 LASSERT(oti->oti_r_locks == 0);
704 LASSERT(oti->oti_w_locks == 0);
711 th = ERR_PTR(-ENOMEM);
713 CERROR("Invalid transaction parameters\n");
714 th = ERR_PTR(-EINVAL);
721 * Concurrency: shouldn't matter.
723 static void osd_trans_stop(const struct lu_env *env, struct thandle *th)
726 struct osd_thandle *oh;
727 struct osd_thread_info *oti = osd_oti_get(env);
731 oh = container_of0(th, struct osd_thandle, ot_super);
732 if (oh->ot_handle != NULL) {
733 handle_t *hdl = oh->ot_handle;
735 hdl->h_sync = th->th_sync;
737 * add commit callback
738 * notice we don't do this in osd_trans_start()
739 * as underlying transaction can change during truncate
741 osd_journal_callback_set(hdl, osd_trans_commit_cb,
744 LASSERT(oti->oti_txns == 1);
746 LASSERT(oti->oti_r_locks == 0);
747 LASSERT(oti->oti_w_locks == 0);
748 result = dt_txn_hook_stop(env, th);
750 CERROR("Failure in transaction hook: %d\n", result);
751 oh->ot_handle = NULL;
752 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
753 result = ldiskfs_journal_stop(hdl));
755 CERROR("Failure to stop transaction: %d\n", result);
762 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
764 struct osd_thandle *oh = container_of0(th, struct osd_thandle,
767 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
773 * Concurrency: no concurrent access is possible that late in object
776 static int osd_inode_remove(const struct lu_env *env, struct osd_object *obj)
778 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
779 struct osd_device *osd = osd_obj2dev(obj);
780 struct osd_thread_info *oti = osd_oti_get(env);
781 struct txn_param *prm = &oti->oti_txn;
782 struct lu_env *env_del_obj = &oti->oti_obj_delete_tx_env;
786 lu_env_init(env_del_obj, LCT_DT_THREAD);
787 txn_param_init(prm, OSD_TXN_OI_DELETE_CREDITS +
788 OSD_TXN_INODE_DELETE_CREDITS);
789 th = osd_trans_start(env_del_obj, &osd->od_dt_dev, prm);
791 result = osd_oi_delete(osd_oti_get(env_del_obj),
792 &osd->od_oi, fid, th);
793 osd_trans_stop(env_del_obj, th);
795 result = PTR_ERR(th);
797 lu_env_fini(env_del_obj);
802 * Called just before object is freed. Releases all resources except for
803 * object itself (that is released by osd_object_free()).
805 * Concurrency: no concurrent access is possible that late in object
808 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
810 struct osd_object *obj = osd_obj(l);
811 struct inode *inode = obj->oo_inode;
813 LINVRNT(osd_invariant(obj));
816 * If object is unlinked remove fid->ino mapping from object index.
823 if (osd_inode_unlinked(inode)) {
824 result = osd_inode_remove(env, obj);
826 LU_OBJECT_DEBUG(D_ERROR, env, l,
827 "Failed to cleanup: %d\n",
832 obj->oo_inode = NULL;
837 * Concurrency: ->loo_object_release() is called under site spin-lock.
839 static void osd_object_release(const struct lu_env *env,
842 struct osd_object *o = osd_obj(l);
844 LASSERT(!lu_object_is_dying(l->lo_header));
845 if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
846 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
850 * Concurrency: shouldn't matter.
852 static int osd_object_print(const struct lu_env *env, void *cookie,
853 lu_printer_t p, const struct lu_object *l)
855 struct osd_object *o = osd_obj(l);
858 if (o->oo_dir != NULL)
859 d = o->oo_dir->od_container.ic_descr;
862 return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
864 o->oo_inode ? o->oo_inode->i_ino : 0UL,
865 o->oo_inode ? o->oo_inode->i_generation : 0,
866 d ? d->id_ops->id_name : "plain");
870 * Concurrency: shouldn't matter.
872 int osd_statfs(const struct lu_env *env, struct dt_device *d,
875 struct osd_device *osd = osd_dt_dev(d);
876 struct super_block *sb = osd_sb(osd);
879 cfs_spin_lock(&osd->od_osfs_lock);
881 if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
882 result = ll_do_statfs(sb, &osd->od_kstatfs);
883 if (likely(result == 0)) /* N.B. statfs can't really fail */
884 osd->od_osfs_age = cfs_time_current_64();
887 if (likely(result == 0))
888 *sfs = osd->od_kstatfs;
889 cfs_spin_unlock(&osd->od_osfs_lock);
895 * Concurrency: doesn't access mutable data.
897 static void osd_conf_get(const struct lu_env *env,
898 const struct dt_device *dev,
899 struct dt_device_param *param)
901 struct super_block *sb = osd_sb(osd_dt_dev(dev));
904 * XXX should be taken from not-yet-existing fs abstraction layer.
906 param->ddp_max_name_len = LDISKFS_NAME_LEN;
907 param->ddp_max_nlink = LDISKFS_LINK_MAX;
908 param->ddp_block_shift = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
909 param->ddp_mntopts = 0;
910 if (test_opt(sb, XATTR_USER))
911 param->ddp_mntopts |= MNTOPT_USERXATTR;
912 if (test_opt(sb, POSIX_ACL))
913 param->ddp_mntopts |= MNTOPT_ACL;
917 * Helper function to get and fill the buffer with input values.
919 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
923 buf = &osd_oti_get(env)->oti_buf;
930 * Concurrency: shouldn't matter.
932 static int osd_sync(const struct lu_env *env, struct dt_device *d)
934 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
935 return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
939 * Start commit for OSD device.
941 * An implementation of dt_commit_async method for OSD device.
942 * Asychronously starts underlayng fs sync and thereby a transaction
945 * \param env environment
948 * \see dt_device_operations
950 static int osd_commit_async(const struct lu_env *env,
953 struct super_block *s = osd_sb(osd_dt_dev(d));
956 CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
957 RETURN(s->s_op->sync_fs(s, 0));
961 * Concurrency: shouldn't matter.
963 lvfs_sbdev_type fsfilt_ldiskfs_journal_sbdev(struct super_block *);
965 static void osd_ro(const struct lu_env *env, struct dt_device *d)
969 CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
971 __lvfs_set_rdonly(lvfs_sbdev(osd_sb(osd_dt_dev(d))),
972 fsfilt_ldiskfs_journal_sbdev(osd_sb(osd_dt_dev(d))));
978 * Concurrency: serialization provided by callers.
980 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
981 int mode, unsigned long timeout, __u32 alg,
982 struct lustre_capa_key *keys)
984 struct osd_device *dev = osd_dt_dev(d);
987 dev->od_fl_capa = mode;
988 dev->od_capa_timeout = timeout;
989 dev->od_capa_alg = alg;
990 dev->od_capa_keys = keys;
995 * Concurrency: serialization provided by callers.
997 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
998 struct dt_quota_ctxt *ctxt, void *data)
1000 struct obd_device *obd = (void *)ctxt;
1001 struct vfsmount *mnt = (struct vfsmount *)data;
1004 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1005 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1006 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1007 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1008 obd->obd_lvfs_ctxt.fs = get_ds();
1014 * Note: we do not count into QUOTA here.
1015 * If we mount with --data_journal we may need more.
1017 static const int osd_dto_credits_noquota[DTO_NR] = {
1020 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1021 * SINGLEDATA_TRANS_BLOCKS(8)
1022 * XXX Note: maybe iam need more, since iam have more level than
1025 [DTO_INDEX_INSERT] = 16,
1026 [DTO_INDEX_DELETE] = 16,
1030 [DTO_IDNEX_UPDATE] = 16,
1032 * Create a object. The same as create object in EXT3.
1033 * DATA_TRANS_BLOCKS(14) +
1034 * INDEX_EXTRA_BLOCKS(8) +
1035 * 3(inode bits, groups, GDT)
1037 [DTO_OBJECT_CREATE] = 25,
1041 [DTO_OBJECT_DELETE] = 25,
1044 * 3(inode bits, group, GDT)
1046 [DTO_ATTR_SET_BASE] = 3,
1048 * Xattr set. The same as xattr of EXT3.
1049 * DATA_TRANS_BLOCKS(14)
1050 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1051 * are also counted in. Do not know why?
1053 [DTO_XATTR_SET] = 14,
1056 * creadits for inode change during write.
1058 [DTO_WRITE_BASE] = 3,
1060 * credits for single block write.
1062 [DTO_WRITE_BLOCK] = 14,
1064 * Attr set credits for chown.
1065 * This is extra credits for setattr, and it is null without quota
1067 [DTO_ATTR_SET_CHOWN]= 0
1071 * Note: we count into QUOTA here.
1072 * If we mount with --data_journal we may need more.
1074 static const int osd_dto_credits_quota[DTO_NR] = {
1076 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1077 * SINGLEDATA_TRANS_BLOCKS(8) +
1078 * 2 * QUOTA_TRANS_BLOCKS(2)
1080 [DTO_INDEX_INSERT] = 20,
1082 * INDEX_EXTRA_TRANS_BLOCKS(8) +
1083 * SINGLEDATA_TRANS_BLOCKS(8) +
1084 * 2 * QUOTA_TRANS_BLOCKS(2)
1086 [DTO_INDEX_DELETE] = 20,
1090 [DTO_IDNEX_UPDATE] = 16,
1092 * Create a object. Same as create object in EXT3 filesystem.
1093 * DATA_TRANS_BLOCKS(16) +
1094 * INDEX_EXTRA_BLOCKS(8) +
1095 * 3(inode bits, groups, GDT) +
1096 * 2 * QUOTA_INIT_BLOCKS(25)
1098 [DTO_OBJECT_CREATE] = 77,
1101 * DATA_TRANS_BLOCKS(16) +
1102 * INDEX_EXTRA_BLOCKS(8) +
1103 * 3(inode bits, groups, GDT) +
1106 [DTO_OBJECT_DELETE] = 27,
1109 * 3 (inode bit, group, GDT) +
1111 [DTO_ATTR_SET_BASE] = 3,
1113 * Xattr set. The same as xattr of EXT3.
1114 * DATA_TRANS_BLOCKS(16)
1115 * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS are
1116 * also counted in. Do not know why?
1118 [DTO_XATTR_SET] = 16,
1121 * creadits for inode change during write.
1123 [DTO_WRITE_BASE] = 3,
1125 * credits for single block write.
1127 [DTO_WRITE_BLOCK] = 16,
1129 * Attr set credits for chown.
1130 * It is added to already set setattr credits
1131 * 2 * QUOTA_INIT_BLOCKS(25) +
1132 * 2 * QUOTA_DEL_BLOCKS(9)
1134 [DTO_ATTR_SET_CHOWN]= 68,
1137 static int osd_credit_get(const struct lu_env *env, struct dt_device *d,
1140 LASSERT(ARRAY_SIZE(osd_dto_credits_noquota) ==
1141 ARRAY_SIZE(osd_dto_credits_quota));
1142 LASSERT(0 <= op && op < ARRAY_SIZE(osd_dto_credits_noquota));
1143 #ifdef HAVE_QUOTA_SUPPORT
1144 if (test_opt(osd_sb(osd_dt_dev(d)), QUOTA))
1145 return osd_dto_credits_quota[op];
1148 return osd_dto_credits_noquota[op];
1151 static const struct dt_device_operations osd_dt_ops = {
1152 .dt_root_get = osd_root_get,
1153 .dt_statfs = osd_statfs,
1154 .dt_trans_start = osd_trans_start,
1155 .dt_trans_stop = osd_trans_stop,
1156 .dt_trans_cb_add = osd_trans_cb_add,
1157 .dt_conf_get = osd_conf_get,
1158 .dt_sync = osd_sync,
1160 .dt_commit_async = osd_commit_async,
1161 .dt_credit_get = osd_credit_get,
1162 .dt_init_capa_ctxt = osd_init_capa_ctxt,
1163 .dt_init_quota_ctxt= osd_init_quota_ctxt,
1166 static void osd_object_read_lock(const struct lu_env *env,
1167 struct dt_object *dt, unsigned role)
1169 struct osd_object *obj = osd_dt_obj(dt);
1170 struct osd_thread_info *oti = osd_oti_get(env);
1172 LINVRNT(osd_invariant(obj));
1174 LASSERT(obj->oo_owner != env);
1175 cfs_down_read_nested(&obj->oo_sem, role);
1177 LASSERT(obj->oo_owner == NULL);
1181 static void osd_object_write_lock(const struct lu_env *env,
1182 struct dt_object *dt, unsigned role)
1184 struct osd_object *obj = osd_dt_obj(dt);
1185 struct osd_thread_info *oti = osd_oti_get(env);
1187 LINVRNT(osd_invariant(obj));
1189 LASSERT(obj->oo_owner != env);
1190 cfs_down_write_nested(&obj->oo_sem, role);
1192 LASSERT(obj->oo_owner == NULL);
1193 obj->oo_owner = env;
1197 static void osd_object_read_unlock(const struct lu_env *env,
1198 struct dt_object *dt)
1200 struct osd_object *obj = osd_dt_obj(dt);
1201 struct osd_thread_info *oti = osd_oti_get(env);
1203 LINVRNT(osd_invariant(obj));
1205 LASSERT(oti->oti_r_locks > 0);
1207 cfs_up_read(&obj->oo_sem);
1210 static void osd_object_write_unlock(const struct lu_env *env,
1211 struct dt_object *dt)
1213 struct osd_object *obj = osd_dt_obj(dt);
1214 struct osd_thread_info *oti = osd_oti_get(env);
1216 LINVRNT(osd_invariant(obj));
1218 LASSERT(obj->oo_owner == env);
1219 LASSERT(oti->oti_w_locks > 0);
1221 obj->oo_owner = NULL;
1222 cfs_up_write(&obj->oo_sem);
1225 static int osd_object_write_locked(const struct lu_env *env,
1226 struct dt_object *dt)
1228 struct osd_object *obj = osd_dt_obj(dt);
1230 LINVRNT(osd_invariant(obj));
1232 return obj->oo_owner == env;
1235 static int capa_is_sane(const struct lu_env *env,
1236 struct osd_device *dev,
1237 struct lustre_capa *capa,
1238 struct lustre_capa_key *keys)
1240 struct osd_thread_info *oti = osd_oti_get(env);
1241 struct lustre_capa *tcapa = &oti->oti_capa;
1242 struct obd_capa *oc;
1246 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1248 if (capa_is_expired(oc)) {
1249 DEBUG_CAPA(D_ERROR, capa, "expired");
1256 if (capa_is_expired_sec(capa)) {
1257 DEBUG_CAPA(D_ERROR, capa, "expired");
1261 cfs_spin_lock(&capa_lock);
1262 for (i = 0; i < 2; i++) {
1263 if (keys[i].lk_keyid == capa->lc_keyid) {
1264 oti->oti_capa_key = keys[i];
1268 cfs_spin_unlock(&capa_lock);
1271 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1275 rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1279 if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1280 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1284 oc = capa_add(dev->od_capa_hash, capa);
1290 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1291 struct lustre_capa *capa, __u64 opc)
1293 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1294 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1295 struct md_capainfo *ci;
1298 if (!dev->od_fl_capa)
1301 if (capa == BYPASS_CAPA)
1304 ci = md_capainfo(env);
1308 if (ci->mc_auth == LC_ID_NONE)
1312 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1316 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1317 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1322 if (!capa_opc_supported(capa, opc)) {
1323 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1327 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1328 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1335 static struct timespec *osd_inode_time(const struct lu_env *env,
1336 struct inode *inode, __u64 seconds)
1338 struct osd_thread_info *oti = osd_oti_get(env);
1339 struct timespec *t = &oti->oti_time;
1341 t->tv_sec = seconds;
1343 *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1348 static void osd_inode_getattr(const struct lu_env *env,
1349 struct inode *inode, struct lu_attr *attr)
1351 attr->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1352 LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1353 LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1355 attr->la_atime = LTIME_S(inode->i_atime);
1356 attr->la_mtime = LTIME_S(inode->i_mtime);
1357 attr->la_ctime = LTIME_S(inode->i_ctime);
1358 attr->la_mode = inode->i_mode;
1359 attr->la_size = i_size_read(inode);
1360 attr->la_blocks = inode->i_blocks;
1361 attr->la_uid = inode->i_uid;
1362 attr->la_gid = inode->i_gid;
1363 attr->la_flags = LDISKFS_I(inode)->i_flags;
1364 attr->la_nlink = inode->i_nlink;
1365 attr->la_rdev = inode->i_rdev;
1366 attr->la_blksize = ll_inode_blksize(inode);
1367 attr->la_blkbits = inode->i_blkbits;
1370 static int osd_attr_get(const struct lu_env *env,
1371 struct dt_object *dt,
1372 struct lu_attr *attr,
1373 struct lustre_capa *capa)
1375 struct osd_object *obj = osd_dt_obj(dt);
1377 LASSERT(dt_object_exists(dt));
1378 LINVRNT(osd_invariant(obj));
1380 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1383 cfs_spin_lock(&obj->oo_guard);
1384 osd_inode_getattr(env, obj->oo_inode, attr);
1385 cfs_spin_unlock(&obj->oo_guard);
1389 static int osd_inode_setattr(const struct lu_env *env,
1390 struct inode *inode, const struct lu_attr *attr)
1394 bits = attr->la_valid;
1396 LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1398 #ifdef HAVE_QUOTA_SUPPORT
1399 if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1400 (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1401 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1407 iattr.ia_valid |= ATTR_UID;
1409 iattr.ia_valid |= ATTR_GID;
1410 iattr.ia_uid = attr->la_uid;
1411 iattr.ia_gid = attr->la_gid;
1412 osd_push_ctxt(env, save);
1413 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1420 if (bits & LA_ATIME)
1421 inode->i_atime = *osd_inode_time(env, inode, attr->la_atime);
1422 if (bits & LA_CTIME)
1423 inode->i_ctime = *osd_inode_time(env, inode, attr->la_ctime);
1424 if (bits & LA_MTIME)
1425 inode->i_mtime = *osd_inode_time(env, inode, attr->la_mtime);
1426 if (bits & LA_SIZE) {
1427 LDISKFS_I(inode)->i_disksize = attr->la_size;
1428 i_size_write(inode, attr->la_size);
1432 /* OSD should not change "i_blocks" which is used by quota.
1433 * "i_blocks" should be changed by ldiskfs only. */
1434 if (bits & LA_BLOCKS)
1435 inode->i_blocks = attr->la_blocks;
1438 inode->i_mode = (inode->i_mode & S_IFMT) |
1439 (attr->la_mode & ~S_IFMT);
1441 inode->i_uid = attr->la_uid;
1443 inode->i_gid = attr->la_gid;
1444 if (bits & LA_NLINK)
1445 inode->i_nlink = attr->la_nlink;
1447 inode->i_rdev = attr->la_rdev;
1449 if (bits & LA_FLAGS) {
1450 /* always keep S_NOCMTIME */
1451 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1457 static int osd_attr_set(const struct lu_env *env,
1458 struct dt_object *dt,
1459 const struct lu_attr *attr,
1460 struct thandle *handle,
1461 struct lustre_capa *capa)
1463 struct osd_object *obj = osd_dt_obj(dt);
1466 LASSERT(handle != NULL);
1467 LASSERT(dt_object_exists(dt));
1468 LASSERT(osd_invariant(obj));
1470 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1473 cfs_spin_lock(&obj->oo_guard);
1474 rc = osd_inode_setattr(env, obj->oo_inode, attr);
1475 cfs_spin_unlock(&obj->oo_guard);
1478 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1485 * XXX temporary solution.
1487 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1488 struct lu_attr *attr, struct thandle *th)
1493 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1494 struct lu_attr *attr, struct thandle *th)
1496 osd_object_init0(obj);
1497 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1498 unlock_new_inode(obj->oo_inode);
1502 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1503 struct osd_object *obj,
1507 struct osd_thread_info *info = osd_oti_get(env);
1508 struct dentry *child_dentry = &info->oti_child_dentry;
1509 struct dentry *obj_dentry = &info->oti_obj_dentry;
1511 obj_dentry->d_inode = obj->oo_inode;
1512 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1513 obj_dentry->d_name.hash = 0;
1515 child_dentry->d_name.hash = 0;
1516 child_dentry->d_parent = obj_dentry;
1517 child_dentry->d_name.name = name;
1518 child_dentry->d_name.len = namelen;
1519 return child_dentry;
1523 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1525 struct dt_allocation_hint *hint,
1529 struct osd_device *osd = osd_obj2dev(obj);
1530 struct osd_thandle *oth;
1531 struct dt_object *parent;
1532 struct inode *inode;
1533 #ifdef HAVE_QUOTA_SUPPORT
1534 struct osd_ctxt *save = &info->oti_ctxt;
1537 LINVRNT(osd_invariant(obj));
1538 LASSERT(obj->oo_inode == NULL);
1540 oth = container_of(th, struct osd_thandle, ot_super);
1541 LASSERT(oth->ot_handle->h_transaction != NULL);
1543 if (hint && hint->dah_parent)
1544 parent = hint->dah_parent;
1546 parent = osd->od_obj_area;
1548 LASSERT(parent != NULL);
1549 LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1551 #ifdef HAVE_QUOTA_SUPPORT
1552 osd_push_ctxt(info->oti_env, save);
1554 inode = ldiskfs_create_inode(oth->ot_handle,
1555 osd_dt_obj(parent)->oo_inode, mode);
1556 #ifdef HAVE_QUOTA_SUPPORT
1559 if (!IS_ERR(inode)) {
1560 /* Do not update file c/mtime in ldiskfs.
1561 * NB: don't need any lock because no contention at this
1563 inode->i_flags |= S_NOCMTIME;
1564 obj->oo_inode = inode;
1567 result = PTR_ERR(inode);
1568 LINVRNT(osd_invariant(obj));
1576 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1577 struct lu_attr *attr,
1578 struct dt_allocation_hint *hint,
1579 struct dt_object_format *dof,
1583 struct osd_thandle *oth;
1584 struct osd_device *osd = osd_obj2dev(obj);
1585 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1587 LASSERT(S_ISDIR(attr->la_mode));
1589 oth = container_of(th, struct osd_thandle, ot_super);
1590 LASSERT(oth->ot_handle->h_transaction != NULL);
1591 result = osd_mkfile(info, obj, mode, hint, th);
1592 if (result == 0 && osd->od_iop_mode == 0) {
1593 LASSERT(obj->oo_inode != NULL);
1595 * XXX uh-oh... call low-level iam function directly.
1598 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1599 sizeof (struct osd_fid_pack),
1605 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1606 struct lu_attr *attr,
1607 struct dt_allocation_hint *hint,
1608 struct dt_object_format *dof,
1612 struct osd_thandle *oth;
1613 const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1615 __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1617 LASSERT(S_ISREG(attr->la_mode));
1619 oth = container_of(th, struct osd_thandle, ot_super);
1620 LASSERT(oth->ot_handle->h_transaction != NULL);
1622 result = osd_mkfile(info, obj, mode, hint, th);
1624 LASSERT(obj->oo_inode != NULL);
1625 if (feat->dif_flags & DT_IND_VARKEY)
1626 result = iam_lvar_create(obj->oo_inode,
1627 feat->dif_keysize_max,
1629 feat->dif_recsize_max,
1632 result = iam_lfix_create(obj->oo_inode,
1633 feat->dif_keysize_max,
1635 feat->dif_recsize_max,
1642 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1643 struct lu_attr *attr,
1644 struct dt_allocation_hint *hint,
1645 struct dt_object_format *dof,
1648 LASSERT(S_ISREG(attr->la_mode));
1649 return osd_mkfile(info, obj, (attr->la_mode &
1650 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1653 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1654 struct lu_attr *attr,
1655 struct dt_allocation_hint *hint,
1656 struct dt_object_format *dof,
1659 LASSERT(S_ISLNK(attr->la_mode));
1660 return osd_mkfile(info, obj, (attr->la_mode &
1661 (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1664 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1665 struct lu_attr *attr,
1666 struct dt_allocation_hint *hint,
1667 struct dt_object_format *dof,
1670 cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1673 LINVRNT(osd_invariant(obj));
1674 LASSERT(obj->oo_inode == NULL);
1675 LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1676 S_ISFIFO(mode) || S_ISSOCK(mode));
1678 result = osd_mkfile(info, obj, mode, hint, th);
1680 LASSERT(obj->oo_inode != NULL);
1681 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1683 LINVRNT(osd_invariant(obj));
1687 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1689 struct dt_allocation_hint *hint,
1690 struct dt_object_format *dof,
1693 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1695 osd_obj_type_f result;
1711 result = osd_mk_index;
1722 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1723 struct dt_object *parent, cfs_umode_t child_mode)
1727 memset(ah, 0, sizeof(*ah));
1728 ah->dah_parent = parent;
1729 ah->dah_mode = child_mode;
1733 * Helper function for osd_object_create()
1735 * \retval 0, on success
1737 static int __osd_object_create(struct osd_thread_info *info,
1738 struct osd_object *obj, struct lu_attr *attr,
1739 struct dt_allocation_hint *hint,
1740 struct dt_object_format *dof,
1746 result = osd_create_pre(info, obj, attr, th);
1748 result = osd_create_type_f(dof->dof_type)(info, obj,
1749 attr, hint, dof, th);
1751 result = osd_create_post(info, obj, attr, th);
1757 * Helper function for osd_object_create()
1759 * \retval 0, on success
1761 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1762 const struct lu_fid *fid, struct thandle *th)
1764 struct osd_thread_info *info = osd_oti_get(env);
1765 struct osd_inode_id *id = &info->oti_id;
1766 struct osd_device *osd = osd_obj2dev(obj);
1767 struct md_ucred *uc = md_ucred(env);
1769 LASSERT(obj->oo_inode != NULL);
1770 LASSERT(uc != NULL);
1772 id->oii_ino = obj->oo_inode->i_ino;
1773 id->oii_gen = obj->oo_inode->i_generation;
1775 return osd_oi_insert(info, &osd->od_oi, fid, id, th,
1776 uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1779 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1780 struct lu_attr *attr,
1781 struct dt_allocation_hint *hint,
1782 struct dt_object_format *dof,
1785 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1786 struct osd_object *obj = osd_dt_obj(dt);
1787 struct osd_thread_info *info = osd_oti_get(env);
1792 LINVRNT(osd_invariant(obj));
1793 LASSERT(!dt_object_exists(dt));
1794 LASSERT(osd_write_locked(env, obj));
1795 LASSERT(th != NULL);
1797 result = __osd_object_create(info, obj, attr, hint, dof, th);
1799 result = __osd_oi_insert(env, obj, fid, th);
1801 LASSERT(ergo(result == 0, dt_object_exists(dt)));
1802 LASSERT(osd_invariant(obj));
1807 * Helper function for osd_xattr_set()
1809 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
1810 const struct lu_buf *buf, const char *name, int fl)
1812 struct osd_object *obj = osd_dt_obj(dt);
1813 struct inode *inode = obj->oo_inode;
1814 struct osd_thread_info *info = osd_oti_get(env);
1815 struct dentry *dentry = &info->oti_child_dentry;
1819 LASSERT(dt_object_exists(dt));
1820 LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
1821 LASSERT(osd_write_locked(env, obj));
1823 if (fl & LU_XATTR_REPLACE)
1824 fs_flags |= XATTR_REPLACE;
1826 if (fl & LU_XATTR_CREATE)
1827 fs_flags |= XATTR_CREATE;
1829 dentry->d_inode = inode;
1830 rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
1831 buf->lb_len, fs_flags);
1836 * Put the fid into lustre_mdt_attrs, and then place the structure
1837 * inode's ea. This fid should not be altered during the life time
1840 * \retval +ve, on success
1841 * \retval -ve, on error
1843 * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
1845 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
1846 const struct lu_fid *fid)
1848 struct osd_thread_info *info = osd_oti_get(env);
1849 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1851 lustre_lma_init(mdt_attrs, fid);
1852 lustre_lma_swab(mdt_attrs);
1853 return __osd_xattr_set(env, dt,
1854 osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
1855 XATTR_NAME_LMA, LU_XATTR_CREATE);
1860 * Helper function to form igif
1862 static inline void osd_igif_get(const struct lu_env *env, struct inode *inode,
1865 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
1869 * Helper function to pack the fid, ldiskfs stores fid in packed format.
1871 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
1872 struct lu_fid *befider)
1874 fid_cpu_to_be(befider, (struct lu_fid *)fid);
1875 memcpy(pack->fp_area, befider, sizeof(*befider));
1876 pack->fp_len = sizeof(*befider) + 1;
1880 * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
1881 * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
1882 * To have compatilibility with 1.8 ldiskfs driver we need to have
1883 * magic number at start of fid data.
1884 * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
1887 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
1888 const struct dt_rec *fid)
1890 param->edp_magic = LDISKFS_LUFID_MAGIC;
1891 param->edp_len = sizeof(struct lu_fid) + 1;
1893 fid_cpu_to_be((struct lu_fid *)param->edp_data,
1894 (struct lu_fid *)fid);
1897 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
1902 switch (pack->fp_len) {
1903 case sizeof *fid + 1:
1904 memcpy(fid, pack->fp_area, sizeof *fid);
1905 fid_be_to_cpu(fid, fid);
1908 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1915 * Try to read the fid from inode ea into dt_rec, if return value
1916 * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
1918 * \param fid object fid.
1920 * \retval 0 on success
1922 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
1923 __u32 ino, struct lu_fid *fid)
1925 struct osd_thread_info *info = osd_oti_get(env);
1926 struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
1927 struct lu_device *ldev = obj->oo_dt.do_lu.lo_dev;
1928 struct dentry *dentry = &info->oti_child_dentry;
1929 struct osd_inode_id *id = &info->oti_id;
1930 struct osd_device *dev;
1931 struct inode *inode;
1935 dev = osd_dev(ldev);
1938 id->oii_gen = OSD_OII_NOGEN;
1940 inode = osd_iget(info, dev, id);
1941 if (IS_ERR(inode)) {
1942 rc = PTR_ERR(inode);
1945 dentry->d_inode = inode;
1947 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
1948 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
1951 /* Check LMA compatibility */
1953 (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
1954 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
1955 inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
1956 ~LMA_INCOMPAT_SUPP);
1961 lustre_lma_swab(mdt_attrs);
1962 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
1964 } else if (rc == -ENODATA) {
1965 osd_igif_get(env, inode, fid);
1974 * OSD layer object create function for interoperability mode (b11826).
1975 * This is mostly similar to osd_object_create(). Only difference being, fid is
1976 * inserted into inode ea here.
1978 * \retval 0, on success
1979 * \retval -ve, on error
1981 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
1982 struct lu_attr *attr,
1983 struct dt_allocation_hint *hint,
1984 struct dt_object_format *dof,
1987 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1988 struct osd_object *obj = osd_dt_obj(dt);
1989 struct osd_thread_info *info = osd_oti_get(env);
1994 LASSERT(osd_invariant(obj));
1995 LASSERT(!dt_object_exists(dt));
1996 LASSERT(osd_write_locked(env, obj));
1997 LASSERT(th != NULL);
1999 result = __osd_object_create(info, obj, attr, hint, dof, th);
2001 /* objects under osd root shld have igif fid, so dont add fid EA */
2002 if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2003 result = osd_ea_fid_set(env, dt, fid);
2006 result = __osd_oi_insert(env, obj, fid, th);
2008 LASSERT(ergo(result == 0, dt_object_exists(dt)));
2009 LINVRNT(osd_invariant(obj));
2014 * Concurrency: @dt is write locked.
2016 static void osd_object_ref_add(const struct lu_env *env,
2017 struct dt_object *dt,
2020 struct osd_object *obj = osd_dt_obj(dt);
2021 struct inode *inode = obj->oo_inode;
2023 LINVRNT(osd_invariant(obj));
2024 LASSERT(dt_object_exists(dt));
2025 LASSERT(osd_write_locked(env, obj));
2026 LASSERT(th != NULL);
2028 cfs_spin_lock(&obj->oo_guard);
2029 LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
2031 cfs_spin_unlock(&obj->oo_guard);
2032 inode->i_sb->s_op->dirty_inode(inode);
2033 LINVRNT(osd_invariant(obj));
2037 * Concurrency: @dt is write locked.
2039 static void osd_object_ref_del(const struct lu_env *env,
2040 struct dt_object *dt,
2043 struct osd_object *obj = osd_dt_obj(dt);
2044 struct inode *inode = obj->oo_inode;
2046 LINVRNT(osd_invariant(obj));
2047 LASSERT(dt_object_exists(dt));
2048 LASSERT(osd_write_locked(env, obj));
2049 LASSERT(th != NULL);
2051 cfs_spin_lock(&obj->oo_guard);
2052 LASSERT(inode->i_nlink > 0);
2054 cfs_spin_unlock(&obj->oo_guard);
2055 inode->i_sb->s_op->dirty_inode(inode);
2056 LINVRNT(osd_invariant(obj));
2060 * Concurrency: @dt is read locked.
2062 static int osd_xattr_get(const struct lu_env *env,
2063 struct dt_object *dt,
2066 struct lustre_capa *capa)
2068 struct osd_object *obj = osd_dt_obj(dt);
2069 struct inode *inode = obj->oo_inode;
2070 struct osd_thread_info *info = osd_oti_get(env);
2071 struct dentry *dentry = &info->oti_obj_dentry;
2073 LASSERT(dt_object_exists(dt));
2074 LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2075 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2077 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2080 dentry->d_inode = inode;
2081 return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2085 * Concurrency: @dt is write locked.
2087 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2088 const struct lu_buf *buf, const char *name, int fl,
2089 struct thandle *handle, struct lustre_capa *capa)
2091 LASSERT(handle != NULL);
2093 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2096 return __osd_xattr_set(env, dt, buf, name, fl);
2100 * Concurrency: @dt is read locked.
2102 static int osd_xattr_list(const struct lu_env *env,
2103 struct dt_object *dt,
2105 struct lustre_capa *capa)
2107 struct osd_object *obj = osd_dt_obj(dt);
2108 struct inode *inode = obj->oo_inode;
2109 struct osd_thread_info *info = osd_oti_get(env);
2110 struct dentry *dentry = &info->oti_obj_dentry;
2112 LASSERT(dt_object_exists(dt));
2113 LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2114 LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2116 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2119 dentry->d_inode = inode;
2120 return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2124 * Concurrency: @dt is write locked.
2126 static int osd_xattr_del(const struct lu_env *env,
2127 struct dt_object *dt,
2129 struct thandle *handle,
2130 struct lustre_capa *capa)
2132 struct osd_object *obj = osd_dt_obj(dt);
2133 struct inode *inode = obj->oo_inode;
2134 struct osd_thread_info *info = osd_oti_get(env);
2135 struct dentry *dentry = &info->oti_obj_dentry;
2138 LASSERT(dt_object_exists(dt));
2139 LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2140 LASSERT(osd_write_locked(env, obj));
2141 LASSERT(handle != NULL);
2143 if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2146 dentry->d_inode = inode;
2147 rc = inode->i_op->removexattr(dentry, name);
2151 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2152 struct dt_object *dt,
2153 struct lustre_capa *old,
2156 struct osd_thread_info *info = osd_oti_get(env);
2157 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2158 struct osd_object *obj = osd_dt_obj(dt);
2159 struct osd_device *dev = osd_obj2dev(obj);
2160 struct lustre_capa_key *key = &info->oti_capa_key;
2161 struct lustre_capa *capa = &info->oti_capa;
2162 struct obd_capa *oc;
2163 struct md_capainfo *ci;
2167 if (!dev->od_fl_capa)
2168 RETURN(ERR_PTR(-ENOENT));
2170 LASSERT(dt_object_exists(dt));
2171 LINVRNT(osd_invariant(obj));
2173 /* renewal sanity check */
2174 if (old && osd_object_auth(env, dt, old, opc))
2175 RETURN(ERR_PTR(-EACCES));
2177 ci = md_capainfo(env);
2179 RETURN(ERR_PTR(-ENOENT));
2181 switch (ci->mc_auth) {
2185 capa->lc_uid = obj->oo_inode->i_uid;
2186 capa->lc_gid = obj->oo_inode->i_gid;
2187 capa->lc_flags = LC_ID_PLAIN;
2189 case LC_ID_CONVERT: {
2192 s[0] = obj->oo_inode->i_uid;
2193 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2194 s[2] = obj->oo_inode->i_gid;
2195 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2196 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2198 RETURN(ERR_PTR(rc));
2200 capa->lc_uid = ((__u64)d[1] << 32) | d[0];
2201 capa->lc_gid = ((__u64)d[3] << 32) | d[2];
2202 capa->lc_flags = LC_ID_CONVERT;
2206 RETURN(ERR_PTR(-EINVAL));
2209 capa->lc_fid = *fid;
2211 capa->lc_flags |= dev->od_capa_alg << 24;
2212 capa->lc_timeout = dev->od_capa_timeout;
2213 capa->lc_expiry = 0;
2215 oc = capa_lookup(dev->od_capa_hash, capa, 1);
2217 LASSERT(!capa_is_expired(oc));
2221 cfs_spin_lock(&capa_lock);
2222 *key = dev->od_capa_keys[1];
2223 cfs_spin_unlock(&capa_lock);
2225 capa->lc_keyid = key->lk_keyid;
2226 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2228 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2230 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2231 RETURN(ERR_PTR(rc));
2234 oc = capa_add(dev->od_capa_hash, capa);
2238 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2241 struct osd_object *obj = osd_dt_obj(dt);
2242 struct inode *inode = obj->oo_inode;
2243 struct osd_thread_info *info = osd_oti_get(env);
2244 struct dentry *dentry = &info->oti_obj_dentry;
2245 struct file *file = &info->oti_file;
2248 dentry->d_inode = inode;
2249 file->f_dentry = dentry;
2250 file->f_mapping = inode->i_mapping;
2251 file->f_op = inode->i_fop;
2252 LOCK_INODE_MUTEX(inode);
2253 rc = file->f_op->fsync(file, dentry, 0);
2254 UNLOCK_INODE_MUTEX(inode);
2259 * Get the 64-bit version for an inode.
2261 static dt_obj_version_t osd_object_version_get(const struct lu_env *env,
2262 struct dt_object *dt)
2264 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2266 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
2267 LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2268 return LDISKFS_I(inode)->i_fs_version;
2272 * Set the 64-bit version and return the old version.
2274 static void osd_object_version_set(const struct lu_env *env, struct dt_object *dt,
2275 dt_obj_version_t new_version)
2277 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2279 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2280 new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2281 LDISKFS_I(inode)->i_fs_version = new_version;
2282 /** Version is set after all inode operations are finished,
2283 * so we should mark it dirty here */
2284 inode->i_sb->s_op->dirty_inode(inode);
2287 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2290 struct osd_object *obj = osd_dt_obj(dt);
2293 *data = (void *)obj->oo_inode;
2301 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2302 const struct dt_index_features *feat)
2304 struct iam_descr *descr;
2306 if (osd_object_is_root(o))
2307 return feat == &dt_directory_features;
2309 LASSERT(o->oo_dir != NULL);
2311 descr = o->oo_dir->od_container.ic_descr;
2312 if (feat == &dt_directory_features) {
2313 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2319 feat->dif_keysize_min <= descr->id_key_size &&
2320 descr->id_key_size <= feat->dif_keysize_max &&
2321 feat->dif_recsize_min <= descr->id_rec_size &&
2322 descr->id_rec_size <= feat->dif_recsize_max &&
2323 !(feat->dif_flags & (DT_IND_VARKEY |
2324 DT_IND_VARREC | DT_IND_NONUNQ)) &&
2325 ergo(feat->dif_flags & DT_IND_UPDATE,
2326 1 /* XXX check that object (and file system) is
2331 static int osd_iam_container_init(const struct lu_env *env,
2332 struct osd_object *obj,
2333 struct osd_directory *dir)
2336 struct iam_container *bag;
2338 bag = &dir->od_container;
2339 result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2341 result = iam_container_setup(bag);
2343 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2345 iam_container_fini(bag);
2352 * Concurrency: no external locking is necessary.
2354 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2355 const struct dt_index_features *feat)
2359 struct osd_object *obj = osd_dt_obj(dt);
2360 struct osd_device *osd = osd_obj2dev(obj);
2362 LINVRNT(osd_invariant(obj));
2363 LASSERT(dt_object_exists(dt));
2365 if (osd_object_is_root(obj)) {
2366 dt->do_index_ops = &osd_index_ea_ops;
2368 } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2369 dt->do_index_ops = &osd_index_ea_ops;
2370 if (S_ISDIR(obj->oo_inode->i_mode))
2375 } else if (!osd_has_index(obj)) {
2376 struct osd_directory *dir;
2381 cfs_spin_lock(&obj->oo_guard);
2382 if (obj->oo_dir == NULL)
2386 * Concurrent thread allocated container data.
2389 cfs_spin_unlock(&obj->oo_guard);
2391 * Now, that we have container data, serialize its
2394 cfs_down_write(&obj->oo_ext_idx_sem);
2396 * recheck under lock.
2398 if (!osd_has_index(obj))
2399 result = osd_iam_container_init(env, obj, dir);
2402 cfs_up_write(&obj->oo_ext_idx_sem);
2408 if (result == 0 && ea_dir == 0) {
2409 if (!osd_iam_index_probe(env, obj, feat))
2412 LINVRNT(osd_invariant(obj));
2417 static const struct dt_object_operations osd_obj_ops = {
2418 .do_read_lock = osd_object_read_lock,
2419 .do_write_lock = osd_object_write_lock,
2420 .do_read_unlock = osd_object_read_unlock,
2421 .do_write_unlock = osd_object_write_unlock,
2422 .do_write_locked = osd_object_write_locked,
2423 .do_attr_get = osd_attr_get,
2424 .do_attr_set = osd_attr_set,
2425 .do_ah_init = osd_ah_init,
2426 .do_create = osd_object_create,
2427 .do_index_try = osd_index_try,
2428 .do_ref_add = osd_object_ref_add,
2429 .do_ref_del = osd_object_ref_del,
2430 .do_xattr_get = osd_xattr_get,
2431 .do_xattr_set = osd_xattr_set,
2432 .do_xattr_del = osd_xattr_del,
2433 .do_xattr_list = osd_xattr_list,
2434 .do_capa_get = osd_capa_get,
2435 .do_object_sync = osd_object_sync,
2436 .do_version_get = osd_object_version_get,
2437 .do_version_set = osd_object_version_set,
2438 .do_data_get = osd_data_get,
2442 * dt_object_operations for interoperability mode
2443 * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2445 static const struct dt_object_operations osd_obj_ea_ops = {
2446 .do_read_lock = osd_object_read_lock,
2447 .do_write_lock = osd_object_write_lock,
2448 .do_read_unlock = osd_object_read_unlock,
2449 .do_write_unlock = osd_object_write_unlock,
2450 .do_write_locked = osd_object_write_locked,
2451 .do_attr_get = osd_attr_get,
2452 .do_attr_set = osd_attr_set,
2453 .do_ah_init = osd_ah_init,
2454 .do_create = osd_object_ea_create,
2455 .do_index_try = osd_index_try,
2456 .do_ref_add = osd_object_ref_add,
2457 .do_ref_del = osd_object_ref_del,
2458 .do_xattr_get = osd_xattr_get,
2459 .do_xattr_set = osd_xattr_set,
2460 .do_xattr_del = osd_xattr_del,
2461 .do_xattr_list = osd_xattr_list,
2462 .do_capa_get = osd_capa_get,
2463 .do_object_sync = osd_object_sync,
2464 .do_version_get = osd_object_version_get,
2465 .do_version_set = osd_object_version_set,
2466 .do_data_get = osd_data_get,
2474 * XXX: Another layering violation for now.
2476 * We don't want to use ->f_op->read methods, because generic file write
2478 * - serializes on ->i_sem, and
2480 * - does a lot of extra work like balance_dirty_pages(),
2482 * which doesn't work for globally shared files like /last-received.
2484 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2486 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2488 memcpy(buffer, (char*)ei->i_data, buflen);
2493 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2496 struct buffer_head *bh;
2497 unsigned long block;
2504 /* prevent reading after eof */
2505 spin_lock(&inode->i_lock);
2506 if (i_size_read(inode) < *offs + size) {
2507 size = i_size_read(inode) - *offs;
2508 spin_unlock(&inode->i_lock);
2510 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2511 i_size_read(inode), *offs);
2513 } else if (size == 0) {
2517 spin_unlock(&inode->i_lock);
2520 blocksize = 1 << inode->i_blkbits;
2523 block = *offs >> inode->i_blkbits;
2524 boffs = *offs & (blocksize - 1);
2525 csize = min(blocksize - boffs, size);
2526 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2528 CERROR("can't read block: %d\n", err);
2532 memcpy(buf, bh->b_data + boffs, csize);
2542 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2543 struct lu_buf *buf, loff_t *pos,
2544 struct lustre_capa *capa)
2546 struct osd_object *obj = osd_dt_obj(dt);
2547 struct inode *inode = obj->oo_inode;
2550 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2553 /* Read small symlink from inode body as we need to maintain correct
2554 * on-disk symlinks for ldiskfs.
2556 if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2557 (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2558 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2560 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2565 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2568 memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2570 LDISKFS_I(inode)->i_disksize = buflen;
2571 i_size_write(inode, buflen);
2572 inode->i_sb->s_op->dirty_inode(inode);
2577 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2578 loff_t *offs, handle_t *handle)
2580 struct buffer_head *bh = NULL;
2581 loff_t offset = *offs;
2582 loff_t new_size = i_size_read(inode);
2583 unsigned long block;
2584 int blocksize = 1 << inode->i_blkbits;
2588 int dirty_inode = 0;
2590 while (bufsize > 0) {
2594 block = offset >> inode->i_blkbits;
2595 boffs = offset & (blocksize - 1);
2596 size = min(blocksize - boffs, bufsize);
2597 bh = ldiskfs_bread(handle, inode, block, 1, &err);
2599 CERROR("can't read/create block: %d\n", err);
2603 err = ldiskfs_journal_get_write_access(handle, bh);
2605 CERROR("journal_get_write_access() returned error %d\n",
2609 LASSERTF(boffs + size <= bh->b_size,
2610 "boffs %d size %d bh->b_size %lu",
2611 boffs, size, (unsigned long)bh->b_size);
2612 memcpy(bh->b_data + boffs, buf, size);
2613 err = ldiskfs_journal_dirty_metadata(handle, bh);
2617 if (offset + size > new_size)
2618 new_size = offset + size;
2626 /* correct in-core and on-disk sizes */
2627 if (new_size > i_size_read(inode)) {
2628 spin_lock(&inode->i_lock);
2629 if (new_size > i_size_read(inode))
2630 i_size_write(inode, new_size);
2631 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
2632 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
2635 spin_unlock(&inode->i_lock);
2637 inode->i_sb->s_op->dirty_inode(inode);
2645 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2646 const struct lu_buf *buf, loff_t *pos,
2647 struct thandle *handle, struct lustre_capa *capa,
2650 struct osd_object *obj = osd_dt_obj(dt);
2651 struct inode *inode = obj->oo_inode;
2652 struct osd_thandle *oh;
2654 #ifdef HAVE_QUOTA_SUPPORT
2655 cfs_cap_t save = cfs_curproc_cap_pack();
2658 LASSERT(handle != NULL);
2660 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
2663 oh = container_of(handle, struct osd_thandle, ot_super);
2664 LASSERT(oh->ot_handle->h_transaction != NULL);
2665 #ifdef HAVE_QUOTA_SUPPORT
2667 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2669 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2671 /* Write small symlink to inode body as we need to maintain correct
2672 * on-disk symlinks for ldiskfs.
2674 if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2675 (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
2676 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2678 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
2681 #ifdef HAVE_QUOTA_SUPPORT
2682 cfs_curproc_cap_unpack(save);
2685 result = buf->lb_len;
2689 static const struct dt_body_operations osd_body_ops = {
2690 .dbo_read = osd_read,
2691 .dbo_write = osd_write
2696 * delete a (key, value) pair from index \a dt specified by \a key
2698 * \param dt osd index object
2699 * \param key key for index
2700 * \param rec record reference
2701 * \param handle transaction handler
2704 * \retval -ve failure
2707 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2708 const struct dt_key *key, struct thandle *handle,
2709 struct lustre_capa *capa)
2711 struct osd_object *obj = osd_dt_obj(dt);
2712 struct osd_thandle *oh;
2713 struct iam_path_descr *ipd;
2714 struct iam_container *bag = &obj->oo_dir->od_container;
2719 LINVRNT(osd_invariant(obj));
2720 LASSERT(dt_object_exists(dt));
2721 LASSERT(bag->ic_object == obj->oo_inode);
2722 LASSERT(handle != NULL);
2724 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2727 ipd = osd_idx_ipd_get(env, bag);
2728 if (unlikely(ipd == NULL))
2731 oh = container_of0(handle, struct osd_thandle, ot_super);
2732 LASSERT(oh->ot_handle != NULL);
2733 LASSERT(oh->ot_handle->h_transaction != NULL);
2735 rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2736 osd_ipd_put(env, bag, ipd);
2737 LINVRNT(osd_invariant(obj));
2741 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2744 struct osd_fid_pack *rec;
2747 if (de->file_type & LDISKFS_DIRENT_LUFID) {
2748 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2749 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2755 * Index delete function for interoperability mode (b11826).
2756 * It will remove the directory entry added by osd_index_ea_insert().
2757 * This entry is needed to maintain name->fid mapping.
2759 * \param key, key i.e. file entry to be deleted
2761 * \retval 0, on success
2762 * \retval -ve, on error
2764 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2765 const struct dt_key *key, struct thandle *handle,
2766 struct lustre_capa *capa)
2768 struct osd_object *obj = osd_dt_obj(dt);
2769 struct inode *dir = obj->oo_inode;
2770 struct dentry *dentry;
2771 struct osd_thandle *oh;
2772 struct ldiskfs_dir_entry_2 *de;
2773 struct buffer_head *bh;
2779 LINVRNT(osd_invariant(obj));
2780 LASSERT(dt_object_exists(dt));
2781 LASSERT(handle != NULL);
2783 oh = container_of(handle, struct osd_thandle, ot_super);
2784 LASSERT(oh->ot_handle != NULL);
2785 LASSERT(oh->ot_handle->h_transaction != NULL);
2787 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2790 dentry = osd_child_dentry_get(env, obj,
2791 (char *)key, strlen((char *)key));
2793 cfs_down_write(&obj->oo_ext_idx_sem);
2794 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
2796 rc = ldiskfs_delete_entry(oh->ot_handle,
2802 cfs_up_write(&obj->oo_ext_idx_sem);
2803 LASSERT(osd_invariant(obj));
2808 * Lookup index for \a key and copy record to \a rec.
2810 * \param dt osd index object
2811 * \param key key for index
2812 * \param rec record reference
2814 * \retval +ve success : exact mach
2815 * \retval 0 return record with key not greater than \a key
2816 * \retval -ve failure
2818 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
2819 struct dt_rec *rec, const struct dt_key *key,
2820 struct lustre_capa *capa)
2822 struct osd_object *obj = osd_dt_obj(dt);
2823 struct iam_path_descr *ipd;
2824 struct iam_container *bag = &obj->oo_dir->od_container;
2825 struct osd_thread_info *oti = osd_oti_get(env);
2826 struct iam_iterator *it = &oti->oti_idx_it;
2827 struct iam_rec *iam_rec;
2831 LASSERT(osd_invariant(obj));
2832 LASSERT(dt_object_exists(dt));
2833 LASSERT(bag->ic_object == obj->oo_inode);
2835 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
2838 ipd = osd_idx_ipd_get(env, bag);
2842 /* got ipd now we can start iterator. */
2843 iam_it_init(it, bag, 0, ipd);
2845 rc = iam_it_get(it, (struct iam_key *)key);
2847 if (S_ISDIR(obj->oo_inode->i_mode))
2848 iam_rec = (struct iam_rec *)oti->oti_ldp;
2850 iam_rec = (struct iam_rec *) rec;
2852 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
2853 if (S_ISDIR(obj->oo_inode->i_mode))
2854 osd_fid_unpack((struct lu_fid *) rec,
2855 (struct osd_fid_pack *)iam_rec);
2859 osd_ipd_put(env, bag, ipd);
2861 LINVRNT(osd_invariant(obj));
2867 * Inserts (key, value) pair in \a dt index object.
2869 * \param dt osd index object
2870 * \param key key for index
2871 * \param rec record reference
2872 * \param th transaction handler
2875 * \retval -ve failure
2877 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
2878 const struct dt_rec *rec, const struct dt_key *key,
2879 struct thandle *th, struct lustre_capa *capa,
2882 struct osd_object *obj = osd_dt_obj(dt);
2883 struct iam_path_descr *ipd;
2884 struct osd_thandle *oh;
2885 struct iam_container *bag = &obj->oo_dir->od_container;
2886 #ifdef HAVE_QUOTA_SUPPORT
2887 cfs_cap_t save = cfs_curproc_cap_pack();
2889 struct osd_thread_info *oti = osd_oti_get(env);
2890 struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
2895 LINVRNT(osd_invariant(obj));
2896 LASSERT(dt_object_exists(dt));
2897 LASSERT(bag->ic_object == obj->oo_inode);
2898 LASSERT(th != NULL);
2900 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
2903 ipd = osd_idx_ipd_get(env, bag);
2904 if (unlikely(ipd == NULL))
2907 oh = container_of0(th, struct osd_thandle, ot_super);
2908 LASSERT(oh->ot_handle != NULL);
2909 LASSERT(oh->ot_handle->h_transaction != NULL);
2910 #ifdef HAVE_QUOTA_SUPPORT
2912 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
2914 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
2916 if (S_ISDIR(obj->oo_inode->i_mode))
2917 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
2919 iam_rec = (struct iam_rec *) rec;
2920 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
2922 #ifdef HAVE_QUOTA_SUPPORT
2923 cfs_curproc_cap_unpack(save);
2925 osd_ipd_put(env, bag, ipd);
2926 LINVRNT(osd_invariant(obj));
2931 * Calls ldiskfs_add_entry() to add directory entry
2932 * into the directory. This is required for
2933 * interoperability mode (b11826)
2935 * \retval 0, on success
2936 * \retval -ve, on error
2938 static int __osd_ea_add_rec(struct osd_thread_info *info,
2939 struct osd_object *pobj,
2940 struct inode *cinode,
2942 const struct dt_rec *fid,
2945 struct ldiskfs_dentry_param *ldp;
2946 struct dentry *child;
2947 struct osd_thandle *oth;
2950 oth = container_of(th, struct osd_thandle, ot_super);
2951 LASSERT(oth->ot_handle != NULL);
2952 LASSERT(oth->ot_handle->h_transaction != NULL);
2954 child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
2956 if (fid_is_igif((struct lu_fid *)fid) ||
2957 fid_is_norm((struct lu_fid *)fid)) {
2958 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2959 osd_get_ldiskfs_dirent_param(ldp, fid);
2960 child->d_fsdata = (void*) ldp;
2962 child->d_fsdata = NULL;
2963 rc = ldiskfs_add_entry(oth->ot_handle, child, cinode);
2969 * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
2970 * into the directory.Also sets flags into osd object to
2971 * indicate dot and dotdot are created. This is required for
2972 * interoperability mode (b11826)
2974 * \param dir directory for dot and dotdot fixup.
2975 * \param obj child object for linking
2977 * \retval 0, on success
2978 * \retval -ve, on error
2980 static int osd_add_dot_dotdot(struct osd_thread_info *info,
2981 struct osd_object *dir,
2982 struct inode *parent_dir, const char *name,
2983 const struct dt_rec *dot_fid,
2984 const struct dt_rec *dot_dot_fid,
2987 struct inode *inode = dir->oo_inode;
2988 struct ldiskfs_dentry_param *dot_ldp;
2989 struct ldiskfs_dentry_param *dot_dot_ldp;
2990 struct osd_thandle *oth;
2993 oth = container_of(th, struct osd_thandle, ot_super);
2994 LASSERT(oth->ot_handle->h_transaction != NULL);
2995 LASSERT(S_ISDIR(dir->oo_inode->i_mode));
2997 if (strcmp(name, dot) == 0) {
2998 if (dir->oo_compat_dot_created) {
3001 LASSERT(inode == parent_dir);
3002 dir->oo_compat_dot_created = 1;
3005 } else if(strcmp(name, dotdot) == 0) {
3006 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3007 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3009 if (!dir->oo_compat_dot_created)
3011 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
3012 osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3013 osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3018 /* in case of rename, dotdot is already created */
3019 if (dir->oo_compat_dotdot_created) {
3020 return __osd_ea_add_rec(info, dir, parent_dir, name,
3024 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir, inode,
3025 dot_ldp, dot_dot_ldp);
3027 dir->oo_compat_dotdot_created = 1;
3035 * It will call the appropriate osd_add* function and return the
3036 * value, return by respective functions.
3038 static int osd_ea_add_rec(const struct lu_env *env,
3039 struct osd_object *pobj,
3040 struct inode *cinode,
3042 const struct dt_rec *fid,
3045 struct osd_thread_info *info = osd_oti_get(env);
3048 if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3050 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3051 (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3054 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid, th);
3060 * Calls ->lookup() to find dentry. From dentry get inode and
3061 * read inode's ea to get fid. This is required for interoperability
3064 * \retval 0, on success
3065 * \retval -ve, on error
3067 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3068 struct dt_rec *rec, const struct dt_key *key)
3070 struct inode *dir = obj->oo_inode;
3071 struct dentry *dentry;
3072 struct ldiskfs_dir_entry_2 *de;
3073 struct buffer_head *bh;
3074 struct lu_fid *fid = (struct lu_fid *) rec;
3078 LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3080 dentry = osd_child_dentry_get(env, obj,
3081 (char *)key, strlen((char *)key));
3083 cfs_down_read(&obj->oo_ext_idx_sem);
3084 bh = ll_ldiskfs_find_entry(dir, dentry, &de);
3086 ino = le32_to_cpu(de->inode);
3087 rc = osd_get_fid_from_dentry(de, rec);
3089 /* done with de, release bh */
3092 rc = osd_ea_fid_get(env, obj, ino, fid);
3096 cfs_up_read(&obj->oo_ext_idx_sem);
3101 * Find the osd object for given fid.
3103 * \param fid need to find the osd object having this fid
3105 * \retval osd_object on success
3106 * \retval -ve on error
3108 struct osd_object *osd_object_find(const struct lu_env *env,
3109 struct dt_object *dt,
3110 const struct lu_fid *fid)
3112 struct lu_device *ludev = dt->do_lu.lo_dev;
3113 struct osd_object *child = NULL;
3114 struct lu_object *luch;
3115 struct lu_object *lo;
3117 luch = lu_object_find(env, ludev, fid, NULL);
3118 if (!IS_ERR(luch)) {
3119 if (lu_object_exists(luch)) {
3120 lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3122 child = osd_obj(lo);
3124 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3125 "lu_object can't be located"
3126 ""DFID"\n", PFID(fid));
3128 if (child == NULL) {
3129 lu_object_put(env, luch);
3130 CERROR("Unable to get osd_object\n");
3131 child = ERR_PTR(-ENOENT);
3134 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3135 "lu_object does not exists "DFID"\n",
3137 child = ERR_PTR(-ENOENT);
3140 child = (void *)luch;
3146 * Put the osd object once done with it.
3148 * \param obj osd object that needs to be put
3150 static inline void osd_object_put(const struct lu_env *env,
3151 struct osd_object *obj)
3153 lu_object_put(env, &obj->oo_dt.do_lu);
3157 * Index add function for interoperability mode (b11826).
3158 * It will add the directory entry.This entry is needed to
3159 * maintain name->fid mapping.
3161 * \param key it is key i.e. file entry to be inserted
3162 * \param rec it is value of given key i.e. fid
3164 * \retval 0, on success
3165 * \retval -ve, on error
3167 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3168 const struct dt_rec *rec,
3169 const struct dt_key *key, struct thandle *th,
3170 struct lustre_capa *capa, int ignore_quota)
3172 struct osd_object *obj = osd_dt_obj(dt);
3173 struct lu_fid *fid = (struct lu_fid *) rec;
3174 const char *name = (const char *)key;
3175 struct osd_object *child;
3176 #ifdef HAVE_QUOTA_SUPPORT
3177 cfs_cap_t save = cfs_curproc_cap_pack();
3183 LASSERT(osd_invariant(obj));
3184 LASSERT(dt_object_exists(dt));
3185 LASSERT(th != NULL);
3187 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3190 child = osd_object_find(env, dt, fid);
3191 if (!IS_ERR(child)) {
3192 #ifdef HAVE_QUOTA_SUPPORT
3194 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3196 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3198 cfs_down_write(&obj->oo_ext_idx_sem);
3199 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3200 cfs_up_write(&obj->oo_ext_idx_sem);
3201 #ifdef HAVE_QUOTA_SUPPORT
3202 cfs_curproc_cap_unpack(save);
3204 osd_object_put(env, child);
3206 rc = PTR_ERR(child);
3209 LASSERT(osd_invariant(obj));
3214 * Initialize osd Iterator for given osd index object.
3216 * \param dt osd index object
3219 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3220 struct dt_object *dt,
3222 struct lustre_capa *capa)
3224 struct osd_it_iam *it;
3225 struct osd_thread_info *oti = osd_oti_get(env);
3226 struct osd_object *obj = osd_dt_obj(dt);
3227 struct lu_object *lo = &dt->do_lu;
3228 struct iam_path_descr *ipd;
3229 struct iam_container *bag = &obj->oo_dir->od_container;
3231 LASSERT(lu_object_exists(lo));
3233 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3234 return ERR_PTR(-EACCES);
3237 ipd = osd_it_ipd_get(env, bag);
3238 if (likely(ipd != NULL)) {
3242 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
3243 return (struct dt_it *)it;
3245 return ERR_PTR(-ENOMEM);
3249 * free given Iterator.
3252 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
3254 struct osd_it_iam *it = (struct osd_it_iam *)di;
3255 struct osd_object *obj = it->oi_obj;
3257 iam_it_fini(&it->oi_it);
3258 osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
3259 lu_object_put(env, &obj->oo_dt.do_lu);
3263 * Move Iterator to record specified by \a key
3265 * \param di osd iterator
3266 * \param key key for index
3268 * \retval +ve di points to record with least key not larger than key
3269 * \retval 0 di points to exact matched key
3270 * \retval -ve failure
3273 static int osd_it_iam_get(const struct lu_env *env,
3274 struct dt_it *di, const struct dt_key *key)
3276 struct osd_it_iam *it = (struct osd_it_iam *)di;
3278 return iam_it_get(&it->oi_it, (const struct iam_key *)key);
3284 * \param di osd iterator
3287 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
3289 struct osd_it_iam *it = (struct osd_it_iam *)di;
3291 iam_it_put(&it->oi_it);
3295 * Move iterator by one record
3297 * \param di osd iterator
3299 * \retval +1 end of container reached
3301 * \retval -ve failure
3304 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
3306 struct osd_it_iam *it = (struct osd_it_iam *)di;
3308 return iam_it_next(&it->oi_it);
3312 * Return pointer to the key under iterator.
3315 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
3316 const struct dt_it *di)
3318 struct osd_it_iam *it = (struct osd_it_iam *)di;
3320 return (struct dt_key *)iam_it_key_get(&it->oi_it);
3324 * Return size of key under iterator (in bytes)
3327 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
3329 struct osd_it_iam *it = (struct osd_it_iam *)di;
3331 return iam_it_key_size(&it->oi_it);
3334 static inline void osd_it_append_attrs(struct lu_dirent*ent,
3339 struct luda_type *lt;
3340 const unsigned align = sizeof(struct luda_type) - 1;
3342 /* check if file type is required */
3343 if (attr & LUDA_TYPE) {
3344 len = (len + align) & ~align;
3346 lt = (void *) ent->lde_name + len;
3347 lt->lt_type = cpu_to_le16(CFS_DTTOIF(type));
3348 ent->lde_attrs |= LUDA_TYPE;
3351 ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
3355 * build lu direct from backend fs dirent.
3358 static inline void osd_it_pack_dirent(struct lu_dirent *ent,
3366 fid_cpu_to_le(&ent->lde_fid, fid);
3367 ent->lde_attrs = LUDA_FID;
3369 ent->lde_hash = cpu_to_le64(offset);
3370 ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
3372 strncpy(ent->lde_name, name, namelen);
3373 ent->lde_namelen = cpu_to_le16(namelen);
3375 /* append lustre attributes */
3376 osd_it_append_attrs(ent, attr, namelen, type);
3380 * Return pointer to the record under iterator.
3382 static int osd_it_iam_rec(const struct lu_env *env,
3383 const struct dt_it *di,
3384 struct lu_dirent *lde,
3387 struct osd_it_iam *it = (struct osd_it_iam *)di;
3388 struct osd_thread_info *info = osd_oti_get(env);
3389 struct lu_fid *fid = &info->oti_fid;
3390 const struct osd_fid_pack *rec;
3396 name = (char *)iam_it_key_get(&it->oi_it);
3398 RETURN(PTR_ERR(name));
3400 namelen = iam_it_key_size(&it->oi_it);
3402 rec = (const struct osd_fid_pack *) iam_it_rec_get(&it->oi_it);
3404 RETURN(PTR_ERR(rec));
3406 rc = osd_fid_unpack(fid, rec);
3410 hash = iam_it_store(&it->oi_it);
3412 /* IAM does not store object type in IAM index (dir) */
3413 osd_it_pack_dirent(lde, fid, hash, name, namelen,
3420 * Returns cookie for current Iterator position.
3422 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
3424 struct osd_it_iam *it = (struct osd_it_iam *)di;
3426 return iam_it_store(&it->oi_it);
3430 * Restore iterator from cookie.
3432 * \param di osd iterator
3433 * \param hash Iterator location cookie
3435 * \retval +ve di points to record with least key not larger than key.
3436 * \retval 0 di points to exact matched key
3437 * \retval -ve failure
3440 static int osd_it_iam_load(const struct lu_env *env,
3441 const struct dt_it *di, __u64 hash)
3443 struct osd_it_iam *it = (struct osd_it_iam *)di;
3445 return iam_it_load(&it->oi_it, hash);
3448 static const struct dt_index_operations osd_index_iam_ops = {
3449 .dio_lookup = osd_index_iam_lookup,
3450 .dio_insert = osd_index_iam_insert,
3451 .dio_delete = osd_index_iam_delete,
3453 .init = osd_it_iam_init,
3454 .fini = osd_it_iam_fini,
3455 .get = osd_it_iam_get,
3456 .put = osd_it_iam_put,
3457 .next = osd_it_iam_next,
3458 .key = osd_it_iam_key,
3459 .key_size = osd_it_iam_key_size,
3460 .rec = osd_it_iam_rec,
3461 .store = osd_it_iam_store,
3462 .load = osd_it_iam_load
3467 * Creates or initializes iterator context.
3469 * \retval struct osd_it_ea, iterator structure on success
3472 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
3473 struct dt_object *dt,
3475 struct lustre_capa *capa)
3477 struct osd_object *obj = osd_dt_obj(dt);
3478 struct osd_thread_info *info = osd_oti_get(env);
3479 struct osd_it_ea *it = &info->oti_it_ea;
3480 struct lu_object *lo = &dt->do_lu;
3481 struct dentry *obj_dentry = &info->oti_it_dentry;
3483 LASSERT(lu_object_exists(lo));
3485 obj_dentry->d_inode = obj->oo_inode;
3486 obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
3487 obj_dentry->d_name.hash = 0;
3489 it->oie_rd_dirent = 0;
3490 it->oie_it_dirent = 0;
3491 it->oie_dirent = NULL;
3492 it->oie_buf = info->oti_it_ea_buf;
3494 it->oie_file.f_pos = 0;
3495 it->oie_file.f_dentry = obj_dentry;
3496 if (attr & LUDA_64BITHASH)
3497 it->oie_file.f_flags = O_64BITHASH;
3499 it->oie_file.f_flags = O_32BITHASH;
3500 it->oie_file.f_mapping = obj->oo_inode->i_mapping;
3501 it->oie_file.f_op = obj->oo_inode->i_fop;
3502 it->oie_file.private_data = NULL;
3504 RETURN((struct dt_it *) it);
3508 * Destroy or finishes iterator context.
3510 * \param di iterator structure to be destroyed
3512 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
3514 struct osd_it_ea *it = (struct osd_it_ea *)di;
3515 struct osd_object *obj = it->oie_obj;
3516 struct inode *inode = obj->oo_inode;
3519 it->oie_file.f_op->release(inode, &it->oie_file);
3520 lu_object_put(env, &obj->oo_dt.do_lu);
3525 * It position the iterator at given key, so that next lookup continues from
3526 * that key Or it is similar to dio_it->load() but based on a key,
3527 * rather than file position.
3529 * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
3532 * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
3534 static int osd_it_ea_get(const struct lu_env *env,
3535 struct dt_it *di, const struct dt_key *key)
3537 struct osd_it_ea *it = (struct osd_it_ea *)di;
3540 LASSERT(((const char *)key)[0] == '\0');
3541 it->oie_file.f_pos = 0;
3542 it->oie_rd_dirent = 0;
3543 it->oie_it_dirent = 0;
3544 it->oie_dirent = NULL;
3552 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
3557 * It is called internally by ->readdir(). It fills the
3558 * iterator's in-memory data structure with required
3559 * information i.e. name, namelen, rec_size etc.
3561 * \param buf in which information to be filled in.
3562 * \param name name of the file in given dir
3564 * \retval 0 on success
3565 * \retval 1 on buffer full
3567 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
3568 loff_t offset, __u64 ino,
3571 struct osd_it_ea *it = (struct osd_it_ea *)buf;
3572 struct osd_it_ea_dirent *ent = it->oie_dirent;
3573 struct lu_fid *fid = &ent->oied_fid;
3574 struct osd_fid_pack *rec;
3577 /* this should never happen */
3578 if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
3579 CERROR("ldiskfs return invalid namelen %d\n", namelen);
3583 if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
3587 if (d_type & LDISKFS_DIRENT_LUFID) {
3588 rec = (struct osd_fid_pack*) (name + namelen + 1);
3590 if (osd_fid_unpack(fid, rec) != 0)
3593 d_type &= ~LDISKFS_DIRENT_LUFID;
3598 ent->oied_ino = ino;
3599 ent->oied_off = offset;
3600 ent->oied_namelen = namelen;
3601 ent->oied_type = d_type;
3603 memcpy(ent->oied_name, name, namelen);
3605 it->oie_rd_dirent++;
3606 it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
3611 * Calls ->readdir() to load a directory entry at a time
3612 * and stored it in iterator's in-memory data structure.
3614 * \param di iterator's in memory structure
3616 * \retval 0 on success
3617 * \retval -ve on error
3619 static int osd_ldiskfs_it_fill(const struct dt_it *di)
3621 struct osd_it_ea *it = (struct osd_it_ea *)di;
3622 struct osd_object *obj = it->oie_obj;
3623 struct inode *inode = obj->oo_inode;
3627 it->oie_dirent = it->oie_buf;
3628 it->oie_rd_dirent = 0;
3630 cfs_down_read(&obj->oo_ext_idx_sem);
3631 result = inode->i_fop->readdir(&it->oie_file, it,
3632 (filldir_t) osd_ldiskfs_filldir);
3634 cfs_up_read(&obj->oo_ext_idx_sem);
3636 if (it->oie_rd_dirent == 0) {
3639 it->oie_dirent = it->oie_buf;
3640 it->oie_it_dirent = 1;
3647 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3648 * to load a directory entry at a time and stored it in
3649 * iterator's in-memory data structure.
3651 * \param di iterator's in memory structure
3653 * \retval +ve iterator reached to end
3654 * \retval 0 iterator not reached to end
3655 * \retval -ve on error
3657 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
3659 struct osd_it_ea *it = (struct osd_it_ea *)di;
3664 if (it->oie_it_dirent < it->oie_rd_dirent) {
3666 (void *) it->oie_dirent +
3667 cfs_size_round(sizeof(struct osd_it_ea_dirent) +
3668 it->oie_dirent->oied_namelen);
3669 it->oie_it_dirent++;
3672 if (it->oie_file.f_pos == LDISKFS_HTREE_EOF)
3675 rc = osd_ldiskfs_it_fill(di);
3682 * Returns the key at current position from iterator's in memory structure.
3684 * \param di iterator's in memory structure
3686 * \retval key i.e. struct dt_key on success
3688 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
3689 const struct dt_it *di)
3691 struct osd_it_ea *it = (struct osd_it_ea *)di;
3693 RETURN((struct dt_key *)it->oie_dirent->oied_name);
3697 * Returns the key's size at current position from iterator's in memory structure.
3699 * \param di iterator's in memory structure
3701 * \retval key_size i.e. struct dt_key on success
3703 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
3705 struct osd_it_ea *it = (struct osd_it_ea *)di;
3707 RETURN(it->oie_dirent->oied_namelen);
3712 * Returns the value (i.e. fid/igif) at current position from iterator's
3713 * in memory structure.
3715 * \param di struct osd_it_ea, iterator's in memory structure
3716 * \param attr attr requested for dirent.
3717 * \param lde lustre dirent
3719 * \retval 0 no error and \param lde has correct lustre dirent.
3720 * \retval -ve on error
3722 static inline int osd_it_ea_rec(const struct lu_env *env,
3723 const struct dt_it *di,
3724 struct lu_dirent *lde,
3727 struct osd_it_ea *it = (struct osd_it_ea *)di;
3728 struct osd_object *obj = it->oie_obj;
3729 struct lu_fid *fid = &it->oie_dirent->oied_fid;
3734 if (!fid_is_sane(fid))
3735 rc = osd_ea_fid_get(env, obj, it->oie_dirent->oied_ino, fid);
3738 osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
3739 it->oie_dirent->oied_name,
3740 it->oie_dirent->oied_namelen,
3741 it->oie_dirent->oied_type,
3747 * Returns a cookie for current position of the iterator head, so that
3748 * user can use this cookie to load/start the iterator next time.
3750 * \param di iterator's in memory structure
3752 * \retval cookie for current position, on success
3754 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
3756 struct osd_it_ea *it = (struct osd_it_ea *)di;
3758 RETURN(it->oie_dirent->oied_off);
3762 * It calls osd_ldiskfs_it_fill() which will use ->readdir()
3763 * to load a directory entry at a time and stored it i inn,
3764 * in iterator's in-memory data structure.
3766 * \param di struct osd_it_ea, iterator's in memory structure
3768 * \retval +ve on success
3769 * \retval -ve on error
3771 static int osd_it_ea_load(const struct lu_env *env,
3772 const struct dt_it *di, __u64 hash)
3774 struct osd_it_ea *it = (struct osd_it_ea *)di;
3778 it->oie_file.f_pos = hash;
3780 rc = osd_ldiskfs_it_fill(di);
3788 * Index lookup function for interoperability mode (b11826).
3790 * \param key, key i.e. file name to be searched
3792 * \retval +ve, on success
3793 * \retval -ve, on error
3795 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
3796 struct dt_rec *rec, const struct dt_key *key,
3797 struct lustre_capa *capa)
3799 struct osd_object *obj = osd_dt_obj(dt);
3804 LASSERT(S_ISDIR(obj->oo_inode->i_mode));
3805 LINVRNT(osd_invariant(obj));
3807 if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3810 rc = osd_ea_lookup_rec(env, obj, rec, key);
3818 * Index and Iterator operations for interoperability
3819 * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3821 static const struct dt_index_operations osd_index_ea_ops = {
3822 .dio_lookup = osd_index_ea_lookup,
3823 .dio_insert = osd_index_ea_insert,
3824 .dio_delete = osd_index_ea_delete,
3826 .init = osd_it_ea_init,
3827 .fini = osd_it_ea_fini,
3828 .get = osd_it_ea_get,
3829 .put = osd_it_ea_put,
3830 .next = osd_it_ea_next,
3831 .key = osd_it_ea_key,
3832 .key_size = osd_it_ea_key_size,
3833 .rec = osd_it_ea_rec,
3834 .store = osd_it_ea_store,
3835 .load = osd_it_ea_load
3839 static void *osd_key_init(const struct lu_context *ctx,
3840 struct lu_context_key *key)
3842 struct osd_thread_info *info;
3844 OBD_ALLOC_PTR(info);
3846 OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3847 if (info->oti_it_ea_buf != NULL) {
3848 info->oti_env = container_of(ctx, struct lu_env,
3852 info = ERR_PTR(-ENOMEM);
3855 info = ERR_PTR(-ENOMEM);
3860 static void osd_key_fini(const struct lu_context *ctx,
3861 struct lu_context_key *key, void* data)
3863 struct osd_thread_info *info = data;
3865 OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
3869 static void osd_key_exit(const struct lu_context *ctx,
3870 struct lu_context_key *key, void *data)
3872 struct osd_thread_info *info = data;
3874 LASSERT(info->oti_r_locks == 0);
3875 LASSERT(info->oti_w_locks == 0);
3876 LASSERT(info->oti_txns == 0);
3879 /* type constructor/destructor: osd_type_init, osd_type_fini */
3880 LU_TYPE_INIT_FINI(osd, &osd_key);
3882 static struct lu_context_key osd_key = {
3883 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD,
3884 .lct_init = osd_key_init,
3885 .lct_fini = osd_key_fini,
3886 .lct_exit = osd_key_exit
3890 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
3891 const char *name, struct lu_device *next)
3893 return osd_procfs_init(osd_dev(d), name);
3896 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
3898 struct osd_thread_info *info = osd_oti_get(env);
3900 if (o->od_obj_area != NULL) {
3901 lu_object_put(env, &o->od_obj_area->do_lu);
3902 o->od_obj_area = NULL;
3904 osd_oi_fini(info, &o->od_oi);
3909 static int osd_mount(const struct lu_env *env,
3910 struct osd_device *o, struct lustre_cfg *cfg)
3912 struct lustre_mount_info *lmi;
3913 const char *dev = lustre_cfg_string(cfg, 0);
3914 struct lustre_disk_data *ldd;
3915 struct lustre_sb_info *lsi;
3918 if (o->od_mount != NULL) {
3919 CERROR("Already mounted (%s)\n", dev);
3924 lmi = server_get_mount(dev);
3926 CERROR("Cannot get mount info for %s!\n", dev);
3930 LASSERT(lmi != NULL);
3931 /* save lustre_mount_info in dt_device */
3934 lsi = s2lsi(lmi->lmi_sb);
3937 if (ldd->ldd_flags & LDD_F_IAM_DIR) {
3939 LCONSOLE_WARN("OSD: IAM mode enabled\n");
3943 o->od_obj_area = NULL;
3947 static struct lu_device *osd_device_fini(const struct lu_env *env,
3948 struct lu_device *d)
3953 shrink_dcache_sb(osd_sb(osd_dev(d)));
3954 osd_sync(env, lu2dt_dev(d));
3956 rc = osd_procfs_fini(osd_dev(d));
3958 CERROR("proc fini error %d \n", rc);
3959 RETURN (ERR_PTR(rc));
3962 if (osd_dev(d)->od_mount)
3963 server_put_mount(osd_dev(d)->od_mount->lmi_name,
3964 osd_dev(d)->od_mount->lmi_mnt);
3965 osd_dev(d)->od_mount = NULL;
3970 static struct lu_device *osd_device_alloc(const struct lu_env *env,
3971 struct lu_device_type *t,
3972 struct lustre_cfg *cfg)
3974 struct lu_device *l;
3975 struct osd_device *o;
3981 result = dt_device_init(&o->od_dt_dev, t);
3984 l->ld_ops = &osd_lu_ops;
3985 o->od_dt_dev.dd_ops = &osd_dt_ops;
3986 cfs_spin_lock_init(&o->od_osfs_lock);
3987 o->od_osfs_age = cfs_time_shift_64(-1000);
3988 o->od_capa_hash = init_capa_hash();
3989 if (o->od_capa_hash == NULL) {
3990 dt_device_fini(&o->od_dt_dev);
3991 l = ERR_PTR(-ENOMEM);
3994 l = ERR_PTR(result);
3999 l = ERR_PTR(-ENOMEM);
4003 static struct lu_device *osd_device_free(const struct lu_env *env,
4004 struct lu_device *d)
4006 struct osd_device *o = osd_dev(d);
4009 cleanup_capa_hash(o->od_capa_hash);
4010 dt_device_fini(&o->od_dt_dev);
4015 static int osd_process_config(const struct lu_env *env,
4016 struct lu_device *d, struct lustre_cfg *cfg)
4018 struct osd_device *o = osd_dev(d);
4022 switch(cfg->lcfg_command) {
4024 err = osd_mount(env, o, cfg);
4027 err = osd_shutdown(env, o);
4036 static int osd_recovery_complete(const struct lu_env *env,
4037 struct lu_device *d)
4042 static int osd_prepare(const struct lu_env *env,
4043 struct lu_device *pdev,
4044 struct lu_device *dev)
4046 struct osd_device *osd = osd_dev(dev);
4047 struct lustre_sb_info *lsi;
4048 struct lustre_disk_data *ldd;
4049 struct lustre_mount_info *lmi;
4050 struct osd_thread_info *oti = osd_oti_get(env);
4051 struct dt_object *d;
4055 /* 1. initialize oi before any file create or file open */
4056 result = osd_oi_init(oti, &osd->od_oi,
4057 &osd->od_dt_dev, lu2md_dev(pdev));
4061 lmi = osd->od_mount;
4062 lsi = s2lsi(lmi->lmi_sb);
4065 /* 2. setup local objects */
4066 result = llo_local_objects_setup(env, lu2md_dev(pdev), lu2dt_dev(dev));
4070 /* 3. open remote object dir */
4071 d = dt_store_open(env, lu2dt_dev(dev), "",
4072 remote_obj_dir, &oti->oti_fid);
4074 osd->od_obj_area = d;
4077 result = PTR_ERR(d);
4078 osd->od_obj_area = NULL;
4085 static const struct lu_object_operations osd_lu_obj_ops = {
4086 .loo_object_init = osd_object_init,
4087 .loo_object_delete = osd_object_delete,
4088 .loo_object_release = osd_object_release,
4089 .loo_object_free = osd_object_free,
4090 .loo_object_print = osd_object_print,
4091 .loo_object_invariant = osd_object_invariant
4094 static const struct lu_device_operations osd_lu_ops = {
4095 .ldo_object_alloc = osd_object_alloc,
4096 .ldo_process_config = osd_process_config,
4097 .ldo_recovery_complete = osd_recovery_complete,
4098 .ldo_prepare = osd_prepare,
4101 static const struct lu_device_type_operations osd_device_type_ops = {
4102 .ldto_init = osd_type_init,
4103 .ldto_fini = osd_type_fini,
4105 .ldto_start = osd_type_start,
4106 .ldto_stop = osd_type_stop,
4108 .ldto_device_alloc = osd_device_alloc,
4109 .ldto_device_free = osd_device_free,
4111 .ldto_device_init = osd_device_init,
4112 .ldto_device_fini = osd_device_fini
4115 static struct lu_device_type osd_device_type = {
4116 .ldt_tags = LU_DEVICE_DT,
4117 .ldt_name = LUSTRE_OSD_NAME,
4118 .ldt_ops = &osd_device_type_ops,
4119 .ldt_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4123 * lprocfs legacy support.
4125 static struct obd_ops osd_obd_device_ops = {
4126 .o_owner = THIS_MODULE
4129 static struct lu_local_obj_desc llod_osd_rem_obj_dir = {
4130 .llod_name = remote_obj_dir,
4131 .llod_oid = OSD_REM_OBJ_DIR_OID,
4133 .llod_feat = &dt_directory_features,
4136 static int __init osd_mod_init(void)
4138 struct lprocfs_static_vars lvars;
4141 llo_local_obj_register(&llod_osd_rem_obj_dir);
4142 lprocfs_osd_init_vars(&lvars);
4143 return class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
4144 LUSTRE_OSD_NAME, &osd_device_type);
4147 static void __exit osd_mod_exit(void)
4149 llo_local_obj_unregister(&llod_osd_rem_obj_dir);
4150 class_unregister_type(LUSTRE_OSD_NAME);
4153 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4154 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_NAME")");
4155 MODULE_LICENSE("GPL");
4157 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);