1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/dt_object.c
39 * Generic functions from dt_object.h
41 * Author: Nikita Danilov <nikita@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_CLASS
46 # define EXPORT_SYMTAB
50 #include <dt_object.h>
51 #include <libcfs/list.h>
53 #include <lustre_fid.h>
55 /* no lock is necessary to protect the list, because call-backs
56 * are added during system startup. Please refer to "struct dt_device".
58 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb)
60 list_add(&cb->dtc_linkage, &dev->dd_txn_callbacks);
62 EXPORT_SYMBOL(dt_txn_callback_add);
64 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb)
66 list_del_init(&cb->dtc_linkage);
68 EXPORT_SYMBOL(dt_txn_callback_del);
70 int dt_txn_hook_start(const struct lu_env *env,
71 struct dt_device *dev, struct txn_param *param)
74 struct dt_txn_callback *cb;
77 list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
78 if (cb->dtc_txn_start == NULL)
80 result = cb->dtc_txn_start(env, param, cb->dtc_cookie);
86 EXPORT_SYMBOL(dt_txn_hook_start);
88 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *txn)
90 struct dt_device *dev = txn->th_dev;
91 struct dt_txn_callback *cb;
95 list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
96 if (cb->dtc_txn_stop == NULL)
98 result = cb->dtc_txn_stop(env, txn, cb->dtc_cookie);
104 EXPORT_SYMBOL(dt_txn_hook_stop);
106 int dt_txn_hook_commit(const struct lu_env *env, struct thandle *txn)
108 struct dt_device *dev = txn->th_dev;
109 struct dt_txn_callback *cb;
113 list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
114 if (cb->dtc_txn_commit == NULL)
116 result = cb->dtc_txn_commit(env, txn, cb->dtc_cookie);
122 EXPORT_SYMBOL(dt_txn_hook_commit);
124 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
127 CFS_INIT_LIST_HEAD(&dev->dd_txn_callbacks);
128 return lu_device_init(&dev->dd_lu_dev, t);
130 EXPORT_SYMBOL(dt_device_init);
132 void dt_device_fini(struct dt_device *dev)
134 lu_device_fini(&dev->dd_lu_dev);
136 EXPORT_SYMBOL(dt_device_fini);
138 int dt_object_init(struct dt_object *obj,
139 struct lu_object_header *h, struct lu_device *d)
142 return lu_object_init(&obj->do_lu, h, d);
144 EXPORT_SYMBOL(dt_object_init);
146 void dt_object_fini(struct dt_object *obj)
148 lu_object_fini(&obj->do_lu);
150 EXPORT_SYMBOL(dt_object_fini);
152 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj)
154 if (obj->do_index_ops == NULL)
155 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
156 return obj->do_index_ops != NULL;
158 EXPORT_SYMBOL(dt_try_as_dir);
160 extern struct lu_context_key lu_global_key;
162 static int dt_lookup(const struct lu_env *env, struct dt_object *dir,
163 const char *name, struct lu_fid *fid)
165 struct lu_fid_pack *pack = lu_context_key_get(&env->le_ctx,
167 struct dt_rec *rec = (struct dt_rec *)pack;
168 const struct dt_key *key = (const struct dt_key *)name;
171 if (dt_try_as_dir(env, dir)) {
172 result = dir->do_index_ops->dio_lookup(env, dir, rec, key,
175 result = fid_unpack(pack, fid);
181 static struct dt_object *dt_locate(const struct lu_env *env,
182 struct dt_device *dev,
183 const struct lu_fid *fid)
185 struct lu_object *obj;
186 struct dt_object *dt;
188 obj = lu_object_find(env, &dev->dd_lu_dev, fid, NULL);
190 obj = lu_object_locate(obj->lo_header, dev->dd_lu_dev.ld_type);
191 LASSERT(obj != NULL);
192 dt = container_of(obj, struct dt_object, do_lu);
198 struct dt_object *dt_store_open(const struct lu_env *env,
199 struct dt_device *dt, const char *name,
204 struct dt_object *root;
205 struct dt_object *child;
207 result = dt->dd_ops->dt_root_get(env, dt, fid);
209 root = dt_locate(env, dt, fid);
211 result = dt_lookup(env, root, name, fid);
213 child = dt_locate(env, dt, fid);
215 child = ERR_PTR(result);
216 lu_object_put(env, &root->do_lu);
219 child = (void *)root;
222 child = ERR_PTR(result);
225 EXPORT_SYMBOL(dt_store_open);
227 const struct dt_index_features dt_directory_features;
228 EXPORT_SYMBOL(dt_directory_features);