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, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/obdclass/dt_object.c
41 * Generic functions from dt_object.h
43 * Author: Nikita Danilov <nikita@clusterfs.com>
46 #define DEBUG_SUBSYSTEM S_CLASS
48 # define EXPORT_SYMTAB
52 #include <dt_object.h>
53 #include <libcfs/list.h>
55 #include <lustre_fid.h>
58 struct lu_fid *dfh_fid;
59 struct dt_device *dfh_dt;
60 struct dt_object *dfh_o;
63 struct dt_thread_info {
64 char dti_buf[DT_MAX_PATH];
65 struct dt_find_hint dti_dfh;
68 /* context key constructor/destructor: dt_global_key_init, dt_global_key_fini */
69 LU_KEY_INIT(dt_global, struct dt_thread_info);
70 LU_KEY_FINI(dt_global, struct dt_thread_info);
72 static struct lu_context_key dt_key = {
73 .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD,
74 .lct_init = dt_global_key_init,
75 .lct_fini = dt_global_key_fini
78 /* no lock is necessary to protect the list, because call-backs
79 * are added during system startup. Please refer to "struct dt_device".
81 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb)
83 cfs_list_add(&cb->dtc_linkage, &dev->dd_txn_callbacks);
85 EXPORT_SYMBOL(dt_txn_callback_add);
87 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb)
89 cfs_list_del_init(&cb->dtc_linkage);
91 EXPORT_SYMBOL(dt_txn_callback_del);
93 int dt_txn_hook_start(const struct lu_env *env,
94 struct dt_device *dev, struct thandle *th)
97 struct dt_txn_callback *cb;
102 cfs_list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
103 if (cb->dtc_txn_start == NULL ||
104 !(cb->dtc_tag & env->le_ctx.lc_tags))
106 rc = cb->dtc_txn_start(env, th, cb->dtc_cookie);
112 EXPORT_SYMBOL(dt_txn_hook_start);
114 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *txn)
116 struct dt_device *dev = txn->th_dev;
117 struct dt_txn_callback *cb;
123 cfs_list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
124 if (cb->dtc_txn_stop == NULL ||
125 !(cb->dtc_tag & env->le_ctx.lc_tags))
127 rc = cb->dtc_txn_stop(env, txn, cb->dtc_cookie);
133 EXPORT_SYMBOL(dt_txn_hook_stop);
135 void dt_txn_hook_commit(struct thandle *txn)
137 struct dt_txn_callback *cb;
142 cfs_list_for_each_entry(cb, &txn->th_dev->dd_txn_callbacks,
144 if (cb->dtc_txn_commit)
145 cb->dtc_txn_commit(txn, cb->dtc_cookie);
148 EXPORT_SYMBOL(dt_txn_hook_commit);
150 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
153 CFS_INIT_LIST_HEAD(&dev->dd_txn_callbacks);
154 return lu_device_init(&dev->dd_lu_dev, t);
156 EXPORT_SYMBOL(dt_device_init);
158 void dt_device_fini(struct dt_device *dev)
160 lu_device_fini(&dev->dd_lu_dev);
162 EXPORT_SYMBOL(dt_device_fini);
164 int dt_object_init(struct dt_object *obj,
165 struct lu_object_header *h, struct lu_device *d)
168 return lu_object_init(&obj->do_lu, h, d);
170 EXPORT_SYMBOL(dt_object_init);
172 void dt_object_fini(struct dt_object *obj)
174 lu_object_fini(&obj->do_lu);
176 EXPORT_SYMBOL(dt_object_fini);
178 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj)
180 if (obj->do_index_ops == NULL)
181 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
182 return obj->do_index_ops != NULL;
184 EXPORT_SYMBOL(dt_try_as_dir);
186 enum dt_format_type dt_mode_to_dft(__u32 mode)
188 enum dt_format_type result;
190 switch (mode & S_IFMT) {
195 result = DFT_REGULAR;
212 EXPORT_SYMBOL(dt_mode_to_dft);
215 * lookup fid for object named \a name in directory \a dir.
218 int dt_lookup_dir(const struct lu_env *env, struct dt_object *dir,
219 const char *name, struct lu_fid *fid)
221 if (dt_try_as_dir(env, dir))
222 return dt_lookup(env, dir, (struct dt_rec *)fid,
223 (const struct dt_key *)name, BYPASS_CAPA);
226 EXPORT_SYMBOL(dt_lookup_dir);
228 * get object for given \a fid.
230 struct dt_object *dt_locate(const struct lu_env *env,
231 struct dt_device *dev,
232 const struct lu_fid *fid)
234 struct lu_object *obj;
235 struct dt_object *dt;
237 obj = lu_object_find(env, &dev->dd_lu_dev, fid, NULL);
239 obj = lu_object_locate(obj->lo_header, dev->dd_lu_dev.ld_type);
240 LASSERT(obj != NULL);
241 dt = container_of(obj, struct dt_object, do_lu);
243 dt = (struct dt_object *)obj;
246 EXPORT_SYMBOL(dt_locate);
249 * find a object named \a entry in given \a dfh->dfh_o directory.
251 static int dt_find_entry(const struct lu_env *env, const char *entry, void *data)
253 struct dt_find_hint *dfh = data;
254 struct dt_device *dt = dfh->dfh_dt;
255 struct lu_fid *fid = dfh->dfh_fid;
256 struct dt_object *obj = dfh->dfh_o;
259 result = dt_lookup_dir(env, obj, entry, fid);
260 lu_object_put(env, &obj->do_lu);
262 obj = dt_locate(env, dt, fid);
264 result = PTR_ERR(obj);
271 * Abstract function which parses path name. This function feeds
272 * path component to \a entry_func.
274 int dt_path_parser(const struct lu_env *env,
275 char *path, dt_entry_func_t entry_func,
282 e = strsep(&path, "/");
287 if (!path || path[0] == '\0')
291 rc = entry_func(env, e, data);
299 static struct dt_object *dt_store_resolve(const struct lu_env *env,
300 struct dt_device *dt,
304 struct dt_thread_info *info = lu_context_key_get(&env->le_ctx,
306 struct dt_find_hint *dfh = &info->dti_dfh;
307 struct dt_object *obj;
308 char *local = info->dti_buf;
314 strncpy(local, path, DT_MAX_PATH);
315 local[DT_MAX_PATH - 1] = '\0';
317 result = dt->dd_ops->dt_root_get(env, dt, fid);
319 obj = dt_locate(env, dt, fid);
322 result = dt_path_parser(env, local, dt_find_entry, dfh);
324 obj = ERR_PTR(result);
329 obj = ERR_PTR(result);
334 static struct dt_object *dt_reg_open(const struct lu_env *env,
335 struct dt_device *dt,
343 result = dt_lookup_dir(env, p, name, fid);
345 o = dt_locate(env, dt, fid);
354 * Open dt object named \a filename from \a dirname directory.
355 * \param dt dt device
356 * \param fid on success, object fid is stored in *fid
358 struct dt_object *dt_store_open(const struct lu_env *env,
359 struct dt_device *dt,
361 const char *filename,
364 struct dt_object *file;
365 struct dt_object *dir;
367 dir = dt_store_resolve(env, dt, dirname, fid);
369 file = dt_reg_open(env, dt, dir,
371 lu_object_put(env, &dir->do_lu);
377 EXPORT_SYMBOL(dt_store_open);
379 /* dt class init function. */
380 int dt_global_init(void)
384 LU_CONTEXT_KEY_INIT(&dt_key);
385 result = lu_context_key_register(&dt_key);
389 void dt_global_fini(void)
391 lu_context_key_degister(&dt_key);
394 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
395 struct lu_buf *buf, loff_t *pos)
399 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
401 rc = dt->do_body_ops->dbo_read(env, dt, buf, pos, BYPASS_CAPA);
403 if (rc == buf->lb_len)
409 EXPORT_SYMBOL(dt_record_read);
411 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
412 const struct lu_buf *buf, loff_t *pos, struct thandle *th)
416 LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
418 LASSERT(dt->do_body_ops);
419 LASSERT(dt->do_body_ops->dbo_write);
420 rc = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, BYPASS_CAPA, 1);
421 if (rc == buf->lb_len)
427 EXPORT_SYMBOL(dt_record_write);
429 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
433 char *xname = XATTR_NAME_VERSION;
437 vbuf.lb_len = sizeof(dt_obj_version_t);
438 return dt_declare_xattr_set(env, o, &vbuf, xname, 0, th);
441 EXPORT_SYMBOL(dt_declare_version_set);
443 void dt_version_set(const struct lu_env *env, struct dt_object *o,
444 dt_obj_version_t version, struct thandle *th)
447 char *xname = XATTR_NAME_VERSION;
451 vbuf.lb_buf = &version;
452 vbuf.lb_len = sizeof(version);
454 rc = dt_xattr_set(env, o, &vbuf, xname, 0, th, BYPASS_CAPA);
456 CDEBUG(D_INODE, "Can't set version, rc %d\n", rc);
459 EXPORT_SYMBOL(dt_version_set);
461 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o)
464 char *xname = XATTR_NAME_VERSION;
465 dt_obj_version_t version;
469 vbuf.lb_buf = &version;
470 vbuf.lb_len = sizeof(version);
471 rc = dt_xattr_get(env, o, &vbuf, xname, BYPASS_CAPA);
472 if (rc != sizeof(version)) {
473 CDEBUG(D_INODE, "Can't get version, rc %d\n", rc);
478 EXPORT_SYMBOL(dt_version_get);
480 const struct dt_index_features dt_directory_features;
481 EXPORT_SYMBOL(dt_directory_features);