Whamcloud - gitweb
- update from 1_5
[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
39 /* no lock is necessary to protect the list, because call-backs 
40  * are added during system startup. Please refer to "struct dt_device".
41  */
42 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb)
43 {
44         list_add(&cb->dtc_linkage, &dev->dd_txn_callbacks);
45 }
46 EXPORT_SYMBOL(dt_txn_callback_add);
47
48 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb)
49 {
50         list_del_init(&cb->dtc_linkage);
51 }
52 EXPORT_SYMBOL(dt_txn_callback_del);
53
54 int dt_txn_hook_start(const struct lu_context *ctxt,
55                       struct dt_device *dev, struct txn_param *param)
56 {
57         int result;
58         struct dt_txn_callback *cb;
59
60         result = 0;
61         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
62                 if (cb->dtc_txn_start == NULL)
63                         continue;
64                 result = cb->dtc_txn_start(ctxt, param, cb->dtc_cookie);
65                 if (result < 0)
66                         break;
67         }
68         return result;
69 }
70 EXPORT_SYMBOL(dt_txn_hook_start);
71
72 int dt_txn_hook_stop(const struct lu_context *ctxt, struct thandle *txn)
73 {
74         struct dt_device       *dev = txn->th_dev;
75         struct dt_txn_callback *cb;
76         int                     result;
77
78         result = 0;
79         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
80                 if (cb->dtc_txn_stop == NULL)
81                         continue;
82                 result = cb->dtc_txn_stop(ctxt, txn, cb->dtc_cookie);
83                 if (result < 0)
84                         break;
85         }
86         return result;
87 }
88 EXPORT_SYMBOL(dt_txn_hook_stop);
89
90 int dt_txn_hook_commit(const struct lu_context *ctxt, struct thandle *txn)
91 {
92         struct dt_device       *dev = txn->th_dev;
93         struct dt_txn_callback *cb;
94         int                     result;
95
96         result = 0;
97         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
98                 if (cb->dtc_txn_commit == NULL)
99                         continue;
100                 result = cb->dtc_txn_commit(ctxt, txn, cb->dtc_cookie);
101                 if (result < 0)
102                         break;
103         }
104         return result;
105 }
106 EXPORT_SYMBOL(dt_txn_hook_commit);
107
108 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
109 {
110
111         CFS_INIT_LIST_HEAD(&dev->dd_txn_callbacks);
112         return lu_device_init(&dev->dd_lu_dev, t);
113 }
114 EXPORT_SYMBOL(dt_device_init);
115
116 void dt_device_fini(struct dt_device *dev)
117 {
118         lu_device_fini(&dev->dd_lu_dev);
119 }
120 EXPORT_SYMBOL(dt_device_fini);
121
122 int dt_object_init(struct dt_object *obj,
123                    struct lu_object_header *h, struct lu_device *d)
124
125 {
126         return lu_object_init(&obj->do_lu, h, d);
127 }
128 EXPORT_SYMBOL(dt_object_init);
129
130 void dt_object_fini(struct dt_object *obj)
131 {
132         lu_object_fini(&obj->do_lu);
133 }
134 EXPORT_SYMBOL(dt_object_fini);
135
136 int dt_try_as_dir(const struct lu_context *ctx, struct dt_object *obj)
137 {
138         if (obj->do_index_ops == NULL)
139                 obj->do_ops->do_index_try(ctx, obj, &dt_directory_features);
140         return obj->do_index_ops != NULL;
141 }
142 EXPORT_SYMBOL(dt_try_as_dir);
143
144 static int dt_lookup(const struct lu_context *ctx, struct dt_object *dir,
145                      const char *name, struct lu_fid *fid)
146 {
147         struct dt_rec       *rec = (struct dt_rec *)fid;
148         const struct dt_key *key = (const struct dt_key *)name;
149         int result;
150
151         if (dt_try_as_dir(ctx, dir))
152                 result = dir->do_index_ops->dio_lookup(ctx, dir, rec, key);
153         else
154                 result = -ENOTDIR;
155         return result;
156 }
157
158 static struct dt_object *dt_locate(const struct lu_context *ctx,
159                                    struct dt_device *dev,
160                                    const struct lu_fid *fid)
161 {
162         struct lu_object *obj;
163         struct dt_object *dt;
164
165         obj = lu_object_find(ctx, dev->dd_lu_dev.ld_site, fid);
166         if (!IS_ERR(obj)) {
167                 obj = lu_object_locate(obj->lo_header, dev->dd_lu_dev.ld_type);
168                 LASSERT(obj != NULL);
169                 dt = container_of(obj, struct dt_object, do_lu);
170         } else
171                 dt = (void *)obj;
172         return dt;
173 }
174
175 struct dt_object *dt_store_open(const struct lu_context *ctx,
176                                 struct dt_device *dt, const char *name,
177                                 struct lu_fid *fid)
178 {
179         int result;
180
181         struct dt_object *root;
182         struct dt_object *child;
183
184         result = dt->dd_ops->dt_root_get(ctx, dt, fid);
185         if (result == 0) {
186                 root = dt_locate(ctx, dt, fid);
187                 if (!IS_ERR(root)) {
188                         result = dt_lookup(ctx, root, name, fid);
189                         if (result == 0)
190                                 child = dt_locate(ctx, dt, fid);
191                         else
192                                 child = ERR_PTR(result);
193                         lu_object_put(ctx, &root->do_lu);
194                 } else {
195                         CERROR("No root\n");
196                         child = (void *)root;
197                 }
198         } else
199                 child = ERR_PTR(result);
200         return child;
201 }
202 EXPORT_SYMBOL(dt_store_open);
203
204 const struct dt_index_features dt_directory_features;
205 EXPORT_SYMBOL(dt_directory_features);
206