4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
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 | LCT_MG_THREAD | LCT_LOCAL,
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 thandle *th)
95 struct dt_txn_callback *cb;
100 cfs_list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
101 if (cb->dtc_txn_start == NULL ||
102 !(cb->dtc_tag & env->le_ctx.lc_tags))
104 rc = cb->dtc_txn_start(env, th, cb->dtc_cookie);
110 EXPORT_SYMBOL(dt_txn_hook_start);
112 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *txn)
114 struct dt_device *dev = txn->th_dev;
115 struct dt_txn_callback *cb;
121 cfs_list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
122 if (cb->dtc_txn_stop == NULL ||
123 !(cb->dtc_tag & env->le_ctx.lc_tags))
125 rc = cb->dtc_txn_stop(env, txn, cb->dtc_cookie);
131 EXPORT_SYMBOL(dt_txn_hook_stop);
133 void dt_txn_hook_commit(struct thandle *txn)
135 struct dt_txn_callback *cb;
140 cfs_list_for_each_entry(cb, &txn->th_dev->dd_txn_callbacks,
142 if (cb->dtc_txn_commit)
143 cb->dtc_txn_commit(txn, cb->dtc_cookie);
146 EXPORT_SYMBOL(dt_txn_hook_commit);
148 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
151 CFS_INIT_LIST_HEAD(&dev->dd_txn_callbacks);
152 return lu_device_init(&dev->dd_lu_dev, t);
154 EXPORT_SYMBOL(dt_device_init);
156 void dt_device_fini(struct dt_device *dev)
158 lu_device_fini(&dev->dd_lu_dev);
160 EXPORT_SYMBOL(dt_device_fini);
162 int dt_object_init(struct dt_object *obj,
163 struct lu_object_header *h, struct lu_device *d)
166 return lu_object_init(&obj->do_lu, h, d);
168 EXPORT_SYMBOL(dt_object_init);
170 void dt_object_fini(struct dt_object *obj)
172 lu_object_fini(&obj->do_lu);
174 EXPORT_SYMBOL(dt_object_fini);
176 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj)
178 if (obj->do_index_ops == NULL)
179 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
180 return obj->do_index_ops != NULL;
182 EXPORT_SYMBOL(dt_try_as_dir);
184 enum dt_format_type dt_mode_to_dft(__u32 mode)
186 enum dt_format_type result;
188 switch (mode & S_IFMT) {
193 result = DFT_REGULAR;
210 EXPORT_SYMBOL(dt_mode_to_dft);
213 * lookup fid for object named \a name in directory \a dir.
216 int dt_lookup_dir(const struct lu_env *env, struct dt_object *dir,
217 const char *name, struct lu_fid *fid)
219 if (dt_try_as_dir(env, dir))
220 return dt_lookup(env, dir, (struct dt_rec *)fid,
221 (const struct dt_key *)name, BYPASS_CAPA);
224 EXPORT_SYMBOL(dt_lookup_dir);
226 * get object for given \a fid.
228 struct dt_object *dt_locate(const struct lu_env *env,
229 struct dt_device *dev,
230 const struct lu_fid *fid)
232 struct lu_object *obj;
233 struct dt_object *dt;
235 obj = lu_object_find(env, &dev->dd_lu_dev, fid, NULL);
237 obj = lu_object_locate(obj->lo_header, dev->dd_lu_dev.ld_type);
238 LASSERT(obj != NULL);
239 dt = container_of(obj, struct dt_object, do_lu);
241 dt = (struct dt_object *)obj;
244 EXPORT_SYMBOL(dt_locate);
247 * find a object named \a entry in given \a dfh->dfh_o directory.
249 static int dt_find_entry(const struct lu_env *env, const char *entry, void *data)
251 struct dt_find_hint *dfh = data;
252 struct dt_device *dt = dfh->dfh_dt;
253 struct lu_fid *fid = dfh->dfh_fid;
254 struct dt_object *obj = dfh->dfh_o;
257 result = dt_lookup_dir(env, obj, entry, fid);
258 lu_object_put(env, &obj->do_lu);
260 obj = dt_locate(env, dt, fid);
262 result = PTR_ERR(obj);
269 * Abstract function which parses path name. This function feeds
270 * path component to \a entry_func.
272 int dt_path_parser(const struct lu_env *env,
273 char *path, dt_entry_func_t entry_func,
280 e = strsep(&path, "/");
285 if (!path || path[0] == '\0')
289 rc = entry_func(env, e, data);
297 static struct dt_object *dt_store_resolve(const struct lu_env *env,
298 struct dt_device *dt,
302 struct dt_thread_info *info = lu_context_key_get(&env->le_ctx,
304 struct dt_find_hint *dfh = &info->dti_dfh;
305 struct dt_object *obj;
306 char *local = info->dti_buf;
312 strncpy(local, path, DT_MAX_PATH);
313 local[DT_MAX_PATH - 1] = '\0';
315 result = dt->dd_ops->dt_root_get(env, dt, fid);
317 obj = dt_locate(env, dt, fid);
320 result = dt_path_parser(env, local, dt_find_entry, dfh);
322 obj = ERR_PTR(result);
327 obj = ERR_PTR(result);
332 static struct dt_object *dt_reg_open(const struct lu_env *env,
333 struct dt_device *dt,
341 result = dt_lookup_dir(env, p, name, fid);
343 o = dt_locate(env, dt, fid);
352 * Open dt object named \a filename from \a dirname directory.
353 * \param dt dt device
354 * \param fid on success, object fid is stored in *fid
356 struct dt_object *dt_store_open(const struct lu_env *env,
357 struct dt_device *dt,
359 const char *filename,
362 struct dt_object *file;
363 struct dt_object *dir;
365 dir = dt_store_resolve(env, dt, dirname, fid);
367 file = dt_reg_open(env, dt, dir,
369 lu_object_put(env, &dir->do_lu);
375 EXPORT_SYMBOL(dt_store_open);
377 /* dt class init function. */
378 int dt_global_init(void)
382 LU_CONTEXT_KEY_INIT(&dt_key);
383 result = lu_context_key_register(&dt_key);
387 void dt_global_fini(void)
389 lu_context_key_degister(&dt_key);
393 * Generic read helper. May return an error for partial reads.
395 * \param env lustre environment
396 * \param dt object to be read
397 * \param buf lu_buf to be filled, with buffer pointer and length
398 * \param pos position to start reading, updated as data is read
400 * \retval real size of data read
401 * \retval -ve errno on failure
403 int dt_read(const struct lu_env *env, struct dt_object *dt,
404 struct lu_buf *buf, loff_t *pos)
406 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
407 return dt->do_body_ops->dbo_read(env, dt, buf, pos, BYPASS_CAPA);
409 EXPORT_SYMBOL(dt_read);
412 * Read structures of fixed size from storage. Unlike dt_read(), using
413 * dt_record_read() will return an error for partial reads.
415 * \param env lustre environment
416 * \param dt object to be read
417 * \param buf lu_buf to be filled, with buffer pointer and length
418 * \param pos position to start reading, updated as data is read
420 * \retval 0 on successfully reading full buffer
421 * \retval -EFAULT on short read
422 * \retval -ve errno on failure
424 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
425 struct lu_buf *buf, loff_t *pos)
429 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
431 rc = dt->do_body_ops->dbo_read(env, dt, buf, pos, BYPASS_CAPA);
433 if (rc == buf->lb_len)
439 EXPORT_SYMBOL(dt_record_read);
441 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
442 const struct lu_buf *buf, loff_t *pos, struct thandle *th)
446 LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
448 LASSERT(dt->do_body_ops);
449 LASSERT(dt->do_body_ops->dbo_write);
450 rc = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, BYPASS_CAPA, 1);
451 if (rc == buf->lb_len)
457 EXPORT_SYMBOL(dt_record_write);
459 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
463 char *xname = XATTR_NAME_VERSION;
467 vbuf.lb_len = sizeof(dt_obj_version_t);
468 return dt_declare_xattr_set(env, o, &vbuf, xname, 0, th);
471 EXPORT_SYMBOL(dt_declare_version_set);
473 void dt_version_set(const struct lu_env *env, struct dt_object *o,
474 dt_obj_version_t version, struct thandle *th)
477 char *xname = XATTR_NAME_VERSION;
481 vbuf.lb_buf = &version;
482 vbuf.lb_len = sizeof(version);
484 rc = dt_xattr_set(env, o, &vbuf, xname, 0, th, BYPASS_CAPA);
486 CDEBUG(D_INODE, "Can't set version, rc %d\n", rc);
489 EXPORT_SYMBOL(dt_version_set);
491 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o)
494 char *xname = XATTR_NAME_VERSION;
495 dt_obj_version_t version;
499 vbuf.lb_buf = &version;
500 vbuf.lb_len = sizeof(version);
501 rc = dt_xattr_get(env, o, &vbuf, xname, BYPASS_CAPA);
502 if (rc != sizeof(version)) {
503 CDEBUG(D_INODE, "Can't get version, rc %d\n", rc);
508 EXPORT_SYMBOL(dt_version_get);
510 const struct dt_index_features dt_directory_features;
511 EXPORT_SYMBOL(dt_directory_features);