Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / obdclass / dt_object.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Dt Object.
5  *
6  *  Copyright (C) 2006 Cluster File Systems, Inc.
7  *   Author: Nikita Danilov <nikita@clusterfs.com>
8  *
9  *   This file is part of the Lustre file system, http://www.lustre.org
10  *   Lustre is a trademark of Cluster File Systems, Inc.
11  *
12  *   You may have signed or agreed to another license before downloading
13  *   this software.  If so, you are bound by the terms and conditions
14  *   of that agreement, and the following does not apply to you.  See the
15  *   LICENSE file included with this distribution for more information.
16  *
17  *   If you did not agree to a different license, then this copy of Lustre
18  *   is open source software; you can redistribute it and/or modify it
19  *   under the terms of version 2 of the GNU General Public License as
20  *   published by the Free Software Foundation.
21  *
22  *   In either case, Lustre is distributed in the hope that it will be
23  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
24  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *   license text for more details.
26  *
27  * Generic functions from dt_object.h
28  */
29
30 #define DEBUG_SUBSYSTEM S_CLASS
31 #ifndef EXPORT_SYMTAB
32 # define EXPORT_SYMTAB
33 #endif
34
35 #include <obd.h>
36 #include <dt_object.h>
37 #include <libcfs/list.h>
38 /* fid_be_to_cpu() */
39 #include <lustre_fid.h>
40
41 /* no lock is necessary to protect the list, because call-backs
42  * are added during system startup. Please refer to "struct dt_device".
43  */
44 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb)
45 {
46         list_add(&cb->dtc_linkage, &dev->dd_txn_callbacks);
47 }
48 EXPORT_SYMBOL(dt_txn_callback_add);
49
50 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb)
51 {
52         list_del_init(&cb->dtc_linkage);
53 }
54 EXPORT_SYMBOL(dt_txn_callback_del);
55
56 int dt_txn_hook_start(const struct lu_env *env,
57                       struct dt_device *dev, struct txn_param *param)
58 {
59         int result;
60         struct dt_txn_callback *cb;
61
62         result = 0;
63         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
64                 if (cb->dtc_txn_start == NULL)
65                         continue;
66                 result = cb->dtc_txn_start(env, param, cb->dtc_cookie);
67                 if (result < 0)
68                         break;
69         }
70         return result;
71 }
72 EXPORT_SYMBOL(dt_txn_hook_start);
73
74 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *txn)
75 {
76         struct dt_device       *dev = txn->th_dev;
77         struct dt_txn_callback *cb;
78         int                     result;
79
80         result = 0;
81         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
82                 if (cb->dtc_txn_stop == NULL)
83                         continue;
84                 result = cb->dtc_txn_stop(env, txn, cb->dtc_cookie);
85                 if (result < 0)
86                         break;
87         }
88         return result;
89 }
90 EXPORT_SYMBOL(dt_txn_hook_stop);
91
92 int dt_txn_hook_commit(const struct lu_env *env, struct thandle *txn)
93 {
94         struct dt_device       *dev = txn->th_dev;
95         struct dt_txn_callback *cb;
96         int                     result;
97
98         result = 0;
99         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
100                 if (cb->dtc_txn_commit == NULL)
101                         continue;
102                 result = cb->dtc_txn_commit(env, txn, cb->dtc_cookie);
103                 if (result < 0)
104                         break;
105         }
106         return result;
107 }
108 EXPORT_SYMBOL(dt_txn_hook_commit);
109
110 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
111 {
112
113         CFS_INIT_LIST_HEAD(&dev->dd_txn_callbacks);
114         return lu_device_init(&dev->dd_lu_dev, t);
115 }
116 EXPORT_SYMBOL(dt_device_init);
117
118 void dt_device_fini(struct dt_device *dev)
119 {
120         lu_device_fini(&dev->dd_lu_dev);
121 }
122 EXPORT_SYMBOL(dt_device_fini);
123
124 int dt_object_init(struct dt_object *obj,
125                    struct lu_object_header *h, struct lu_device *d)
126
127 {
128         return lu_object_init(&obj->do_lu, h, d);
129 }
130 EXPORT_SYMBOL(dt_object_init);
131
132 void dt_object_fini(struct dt_object *obj)
133 {
134         lu_object_fini(&obj->do_lu);
135 }
136 EXPORT_SYMBOL(dt_object_fini);
137
138 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj)
139 {
140         if (obj->do_index_ops == NULL)
141                 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
142         return obj->do_index_ops != NULL;
143 }
144 EXPORT_SYMBOL(dt_try_as_dir);
145
146 extern struct lu_context_key lu_global_key;
147
148 static int dt_lookup(const struct lu_env *env, struct dt_object *dir,
149                      const char *name, struct lu_fid *fid)
150 {
151         struct lu_fid_pack  *pack = lu_context_key_get(&env->le_ctx,
152                                                        &lu_global_key);
153         struct dt_rec       *rec = (struct dt_rec *)pack;
154         const struct dt_key *key = (const struct dt_key *)name;
155         int result;
156
157         if (dt_try_as_dir(env, dir)) {
158                 result = dir->do_index_ops->dio_lookup(env, dir, rec, key,
159                                                        BYPASS_CAPA);
160                 if (result == 0)
161                         result = fid_unpack(pack, fid);
162         } else
163                 result = -ENOTDIR;
164         return result;
165 }
166
167 static struct dt_object *dt_locate(const struct lu_env *env,
168                                    struct dt_device *dev,
169                                    const struct lu_fid *fid)
170 {
171         struct lu_object *obj;
172         struct dt_object *dt;
173
174         obj = lu_object_find(env, dev->dd_lu_dev.ld_site, fid);
175         if (!IS_ERR(obj)) {
176                 obj = lu_object_locate(obj->lo_header, dev->dd_lu_dev.ld_type);
177                 LASSERT(obj != NULL);
178                 dt = container_of(obj, struct dt_object, do_lu);
179         } else
180                 dt = (void *)obj;
181         return dt;
182 }
183
184 struct dt_object *dt_store_open(const struct lu_env *env,
185                                 struct dt_device *dt, const char *name,
186                                 struct lu_fid *fid)
187 {
188         int result;
189
190         struct dt_object *root;
191         struct dt_object *child;
192
193         result = dt->dd_ops->dt_root_get(env, dt, fid);
194         if (result == 0) {
195                 root = dt_locate(env, dt, fid);
196                 if (!IS_ERR(root)) {
197                         result = dt_lookup(env, root, name, fid);
198                         if (result == 0)
199                                 child = dt_locate(env, dt, fid);
200                         else
201                                 child = ERR_PTR(result);
202                         lu_object_put(env, &root->do_lu);
203                 } else {
204                         CERROR("No root\n");
205                         child = (void *)root;
206                 }
207         } else
208                 child = ERR_PTR(result);
209         return child;
210 }
211 EXPORT_SYMBOL(dt_store_open);
212
213 const struct dt_index_features dt_directory_features;
214 EXPORT_SYMBOL(dt_directory_features);
215