Whamcloud - gitweb
give friendly error message if crypto failed.
[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_env *env,
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(env, 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_env *env, 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(env, 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_env *env, 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(env, 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_env *env, struct dt_object *obj)
137 {
138         if (obj->do_index_ops == NULL)
139                 obj->do_ops->do_index_try(env, 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_env *env, 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(env, dir))
152                 result = dir->do_index_ops->dio_lookup(env, dir, rec, key,
153                                                        BYPASS_CAPA);
154         else
155                 result = -ENOTDIR;
156         return result;
157 }
158
159 static struct dt_object *dt_locate(const struct lu_env *env,
160                                    struct dt_device *dev,
161                                    const struct lu_fid *fid)
162 {
163         struct lu_object *obj;
164         struct dt_object *dt;
165
166         obj = lu_object_find(env, dev->dd_lu_dev.ld_site, fid);
167         if (!IS_ERR(obj)) {
168                 obj = lu_object_locate(obj->lo_header, dev->dd_lu_dev.ld_type);
169                 LASSERT(obj != NULL);
170                 dt = container_of(obj, struct dt_object, do_lu);
171         } else
172                 dt = (void *)obj;
173         return dt;
174 }
175
176 struct dt_object *dt_store_open(const struct lu_env *env,
177                                 struct dt_device *dt, const char *name,
178                                 struct lu_fid *fid)
179 {
180         int result;
181
182         struct dt_object *root;
183         struct dt_object *child;
184
185         result = dt->dd_ops->dt_root_get(env, dt, fid);
186         if (result == 0) {
187                 root = dt_locate(env, dt, fid);
188                 if (!IS_ERR(root)) {
189                         result = dt_lookup(env, root, name, fid);
190                         if (result == 0)
191                                 child = dt_locate(env, dt, fid);
192                         else
193                                 child = ERR_PTR(result);
194                         lu_object_put(env, &root->do_lu);
195                 } else {
196                         CERROR("No root\n");
197                         child = (void *)root;
198                 }
199         } else
200                 child = ERR_PTR(result);
201         return child;
202 }
203 EXPORT_SYMBOL(dt_store_open);
204
205 const struct dt_index_features dt_directory_features;
206 EXPORT_SYMBOL(dt_directory_features);
207