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.
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>
56 struct lu_fid *dfh_fid;
57 struct dt_device *dfh_dt;
58 struct dt_object *dfh_o;
61 struct dt_thread_info {
62 char dti_buf[DT_MAX_PATH];
63 struct dt_find_hint dti_dfh;
66 /* context key constructor/destructor: dt_global_key_init, dt_global_key_fini */
67 LU_KEY_INIT(dt_global, struct dt_thread_info);
68 LU_KEY_FINI(dt_global, struct dt_thread_info);
70 static struct lu_context_key dt_key = {
71 .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD,
72 .lct_init = dt_global_key_init,
73 .lct_fini = dt_global_key_fini
76 /* no lock is necessary to protect the list, because call-backs
77 * are added during system startup. Please refer to "struct dt_device".
79 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb)
81 cfs_list_add(&cb->dtc_linkage, &dev->dd_txn_callbacks);
83 EXPORT_SYMBOL(dt_txn_callback_add);
85 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb)
87 cfs_list_del_init(&cb->dtc_linkage);
89 EXPORT_SYMBOL(dt_txn_callback_del);
91 int dt_txn_hook_start(const struct lu_env *env,
92 struct dt_device *dev, struct txn_param *param)
95 struct dt_txn_callback *cb;
98 cfs_list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
99 if (cb->dtc_txn_start == NULL ||
100 !(cb->dtc_tag & env->le_ctx.lc_tags))
102 result = cb->dtc_txn_start(env, param, cb->dtc_cookie);
108 EXPORT_SYMBOL(dt_txn_hook_start);
110 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *txn)
112 struct dt_device *dev = txn->th_dev;
113 struct dt_txn_callback *cb;
117 cfs_list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
118 if (cb->dtc_txn_stop == NULL ||
119 !(cb->dtc_tag & env->le_ctx.lc_tags))
121 result = cb->dtc_txn_stop(env, txn, cb->dtc_cookie);
127 EXPORT_SYMBOL(dt_txn_hook_stop);
129 void dt_txn_hook_commit(struct thandle *txn)
131 struct dt_txn_callback *cb;
133 cfs_list_for_each_entry(cb, &txn->th_dev->dd_txn_callbacks,
135 if (cb->dtc_txn_commit)
136 cb->dtc_txn_commit(txn, cb->dtc_cookie);
139 EXPORT_SYMBOL(dt_txn_hook_commit);
141 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
144 CFS_INIT_LIST_HEAD(&dev->dd_txn_callbacks);
145 return lu_device_init(&dev->dd_lu_dev, t);
147 EXPORT_SYMBOL(dt_device_init);
149 void dt_device_fini(struct dt_device *dev)
151 lu_device_fini(&dev->dd_lu_dev);
153 EXPORT_SYMBOL(dt_device_fini);
155 int dt_object_init(struct dt_object *obj,
156 struct lu_object_header *h, struct lu_device *d)
159 return lu_object_init(&obj->do_lu, h, d);
161 EXPORT_SYMBOL(dt_object_init);
163 void dt_object_fini(struct dt_object *obj)
165 lu_object_fini(&obj->do_lu);
167 EXPORT_SYMBOL(dt_object_fini);
169 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj)
171 if (obj->do_index_ops == NULL)
172 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
173 return obj->do_index_ops != NULL;
175 EXPORT_SYMBOL(dt_try_as_dir);
177 enum dt_format_type dt_mode_to_dft(__u32 mode)
179 enum dt_format_type result;
181 switch (mode & S_IFMT) {
186 result = DFT_REGULAR;
204 EXPORT_SYMBOL(dt_mode_to_dft);
206 * lookup fid for object named \a name in directory \a dir.
209 static int dt_lookup(const struct lu_env *env, struct dt_object *dir,
210 const char *name, struct lu_fid *fid)
212 struct dt_rec *rec = (struct dt_rec *)fid;
213 const struct dt_key *key = (const struct dt_key *)name;
216 if (dt_try_as_dir(env, dir)) {
217 result = dir->do_index_ops->dio_lookup(env, dir, rec, key,
221 else if (result == 0)
229 * get object for given \a fid.
231 struct dt_object *dt_locate(const struct lu_env *env,
232 struct dt_device *dev,
233 const struct lu_fid *fid)
235 struct lu_object *obj;
236 struct dt_object *dt;
238 obj = lu_object_find(env, &dev->dd_lu_dev, fid, NULL);
240 obj = lu_object_locate(obj->lo_header, dev->dd_lu_dev.ld_type);
241 LASSERT(obj != NULL);
242 dt = container_of(obj, struct dt_object, do_lu);
244 dt = (struct dt_object *)obj;
247 EXPORT_SYMBOL(dt_locate);
250 * find a object named \a entry in given \a dfh->dfh_o directory.
252 static int dt_find_entry(const struct lu_env *env, const char *entry, void *data)
254 struct dt_find_hint *dfh = data;
255 struct dt_device *dt = dfh->dfh_dt;
256 struct lu_fid *fid = dfh->dfh_fid;
257 struct dt_object *obj = dfh->dfh_o;
260 result = dt_lookup(env, obj, entry, fid);
261 lu_object_put(env, &obj->do_lu);
263 obj = dt_locate(env, dt, fid);
265 result = PTR_ERR(obj);
272 * Abstract function which parses path name. This function feeds
273 * path component to \a entry_func.
275 int dt_path_parser(const struct lu_env *env,
276 char *path, dt_entry_func_t entry_func,
283 e = strsep(&path, "/");
288 if (!path || path[0] == '\0')
292 rc = entry_func(env, e, data);
300 static struct dt_object *dt_store_resolve(const struct lu_env *env,
301 struct dt_device *dt,
305 struct dt_thread_info *info = lu_context_key_get(&env->le_ctx,
307 struct dt_find_hint *dfh = &info->dti_dfh;
308 struct dt_object *obj;
309 char *local = info->dti_buf;
315 strncpy(local, path, DT_MAX_PATH);
316 local[DT_MAX_PATH - 1] = '\0';
318 result = dt->dd_ops->dt_root_get(env, dt, fid);
320 obj = dt_locate(env, dt, fid);
323 result = dt_path_parser(env, local, dt_find_entry, dfh);
325 obj = ERR_PTR(result);
330 obj = ERR_PTR(result);
335 static struct dt_object *dt_reg_open(const struct lu_env *env,
336 struct dt_device *dt,
344 result = dt_lookup(env, p, name, fid);
346 o = dt_locate(env, dt, fid);
355 * Open dt object named \a filename from \a dirname directory.
356 * \param dt dt device
357 * \param fid on success, object fid is stored in *fid
359 struct dt_object *dt_store_open(const struct lu_env *env,
360 struct dt_device *dt,
362 const char *filename,
365 struct dt_object *file;
366 struct dt_object *dir;
368 dir = dt_store_resolve(env, dt, dirname, fid);
370 file = dt_reg_open(env, dt, dir,
372 lu_object_put(env, &dir->do_lu);
378 EXPORT_SYMBOL(dt_store_open);
380 /* dt class init function. */
381 int dt_global_init(void)
385 LU_CONTEXT_KEY_INIT(&dt_key);
386 result = lu_context_key_register(&dt_key);
390 void dt_global_fini(void)
392 lu_context_key_degister(&dt_key);
395 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
396 struct lu_buf *buf, loff_t *pos)
400 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
402 rc = dt->do_body_ops->dbo_read(env, dt, buf, pos, BYPASS_CAPA);
404 if (rc == buf->lb_len)
410 EXPORT_SYMBOL(dt_record_read);
412 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
413 const struct lu_buf *buf, loff_t *pos, struct thandle *th)
417 LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
419 rc = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, BYPASS_CAPA, 1);
420 if (rc == buf->lb_len)
426 EXPORT_SYMBOL(dt_record_write);
428 const struct dt_index_features dt_directory_features;
429 EXPORT_SYMBOL(dt_directory_features);