Whamcloud - gitweb
- much of the striping configuration managment and setup builds up
[fs/lustre-release.git] / lustre / obdclass / genops.c
1 /*
2  *  linux/fs/ext2_obd/sim_obd.c
3  * Copyright (C) 2001  Cluster File Systems, Inc.
4  *
5  * This code is issued under the GNU General Public License.
6  * See the file COPYING in this distribution
7  *
8  * These are the only exported functions, they provide some generic
9  * infrastructure for managing object devices
10  *
11  */
12
13 #define DEBUG_SUBSYSTEM S_CLASS
14 #include <linux/module.h>
15 #include <linux/obd_class.h>
16 #include <linux/random.h>
17 #include <linux/slab.h>
18
19 extern struct list_head obd_types; 
20 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
21 kmem_cache_t *obdo_cachep = NULL;
22 kmem_cache_t *export_cachep = NULL;
23 kmem_cache_t *import_cachep = NULL;
24
25 /*
26  * support functions: we could use inter-module communication, but this
27  * is more portable to other OS's
28  */
29 static struct obd_type *class_search_type(char *nm)
30 {
31         struct list_head *tmp;
32         struct obd_type *type;
33         CDEBUG(D_INFO, "SEARCH %s\n", nm);
34
35         tmp = &obd_types;
36         while ( (tmp = tmp->next) != &obd_types ) {
37                 type = list_entry(tmp, struct obd_type, typ_chain);
38                 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
39                 if (strlen(type->typ_name) == strlen(nm) &&
40                     strcmp(type->typ_name, nm) == 0 ) {
41                         return type;
42                 }
43         }
44         return NULL;
45 }
46
47 struct obd_type *class_nm_to_type(char *nm)
48 {
49         struct obd_type *type = class_search_type(nm);
50
51 #ifdef CONFIG_KMOD
52         if ( !type ) {
53                 if ( !request_module(nm) ) {
54                         CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
55                         type = class_search_type(nm);
56                 } else {
57                         CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
58                 }
59         }
60 #endif
61         return type;
62 }
63
64 int class_register_type(struct obd_ops *ops, char *nm)
65 {
66         struct obd_type *type;
67
68         ENTRY;
69
70         if (class_nm_to_type(nm)) {
71                 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
72                 RETURN(-EEXIST);
73         }
74
75         OBD_ALLOC(type, sizeof(*type));
76         if (!type)
77                 RETURN(-ENOMEM);
78         INIT_LIST_HEAD(&type->typ_chain);
79         MOD_INC_USE_COUNT;
80         list_add(&type->typ_chain, obd_types.next);
81         type->typ_ops = ops;
82         type->typ_name = nm;
83         RETURN(0);
84 }
85
86 int class_unregister_type(char *nm)
87 {
88         struct obd_type *type = class_nm_to_type(nm);
89
90         ENTRY;
91
92         if ( !type ) {
93                 MOD_DEC_USE_COUNT;
94                 CERROR("unknown obd type\n");
95                 RETURN(-EINVAL);
96         }
97
98         if ( type->typ_refcnt ) {
99                 MOD_DEC_USE_COUNT;
100                 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
101                 RETURN(-EBUSY);
102         }
103
104         list_del(&type->typ_chain);
105         OBD_FREE(type, sizeof(*type));
106         MOD_DEC_USE_COUNT;
107         RETURN(0);
108 } /* class_unregister_type */
109
110 int class_name2dev(char *name)
111 {
112         int res = -1;
113         int i;
114
115         for (i=0; i < MAX_OBD_DEVICES; i++) {
116                 struct obd_device *obd = &obd_dev[i];
117                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
118                         res = i;
119                         return res;
120                 }
121         }
122
123         return res;
124 }
125
126 int class_uuid2dev(char *name)
127 {
128         int res = -1;
129         int i;
130
131         for (i=0; i < MAX_OBD_DEVICES; i++) {
132                 struct obd_device *obd = &obd_dev[i];
133                 if (obd->obd_name && strncmp(name, obd->obd_uuid, 37) == 0) {
134                         res = i;
135                         return res;
136                 }
137         }
138
139         return res;
140 }
141
142
143 struct obd_device *class_uuid2obd(char *name)
144 {
145         int i;
146
147         for (i=0; i < MAX_OBD_DEVICES; i++) {
148                 struct obd_device *obd = &obd_dev[i];
149                 if (obd->obd_name && strncmp(name, obd->obd_uuid, 37) == 0) {
150                         return obd;
151                 }
152         }
153
154         return NULL;
155 }
156
157 void obd_cleanup_caches(void)
158 {
159         int rc;
160         ENTRY;
161         if (obdo_cachep) { 
162                 rc = kmem_cache_destroy(obdo_cachep);
163                 if (rc)
164                         CERROR("Cannot destory obdo_cachep\n");
165                 obdo_cachep = NULL;
166         }
167         if (import_cachep) { 
168                 rc = kmem_cache_destroy(import_cachep);
169                 if (rc)
170                         CERROR("Cannot destory import_cachep\n");
171                 import_cachep = NULL;
172         }
173         if (export_cachep) { 
174                 rc = kmem_cache_destroy(export_cachep);
175                 if (rc)
176                         CERROR("Cannot destory import_cachep\n");
177                 export_cachep = NULL;
178         }
179         EXIT;
180 }
181
182 int obd_init_caches(void)
183 {
184         ENTRY;
185         if (obdo_cachep == NULL) {
186                 obdo_cachep = kmem_cache_create("obdo_cache",
187                                                 sizeof(struct obdo),
188                                                 0, SLAB_HWCACHE_ALIGN,
189                                                 NULL, NULL);
190                 if (obdo_cachep == NULL)
191                         GOTO(out, -ENOMEM);
192         }
193
194         if (export_cachep == NULL) {
195                 export_cachep = kmem_cache_create("export_cache",
196                                                 sizeof(struct obd_export),
197                                                 0, SLAB_HWCACHE_ALIGN,
198                                                 NULL, NULL);
199                 if (export_cachep == NULL)
200                         GOTO(out, -ENOMEM);
201         }
202
203         if (import_cachep == NULL) {
204                 import_cachep = kmem_cache_create("import_cache",
205                                                 sizeof(struct obd_import),
206                                                 0, SLAB_HWCACHE_ALIGN,
207                                                 NULL, NULL);
208                 if (import_cachep == NULL)
209                         GOTO(out, -ENOMEM);
210         }
211         RETURN(0);
212  out:
213         obd_cleanup_caches();
214         RETURN(-ENOMEM);
215
216 }
217
218 /* map connection to client */
219 struct obd_export *class_conn2export(struct lustre_handle *conn)
220 {
221         struct obd_export * export;
222
223         if (!conn)
224                 RETURN(NULL);
225
226         if (!conn->addr || conn->addr == -1 ) { 
227                 fixme();
228                 RETURN(NULL);
229         }
230               
231         export = (struct obd_export *) (unsigned long)conn->addr;
232         if (!kmem_cache_validate(export_cachep, (void *)export))
233                 RETURN(NULL);
234
235         if (export->export_cookie != conn->cookie)
236                 return NULL;
237         return export;
238 } /* class_conn2export */
239
240 struct obd_device *class_conn2obd(struct lustre_handle *conn)
241 {
242         struct obd_export *export;
243         export = class_conn2export(conn); 
244         if (export) 
245                 return export->export_obd;
246         fixme();
247         return NULL;
248 }
249
250 /* a connection defines an export context in which preallocation can
251    be managed. */
252 int class_connect (struct lustre_handle *conn, struct obd_device *obd)
253 {
254         struct obd_export * export;
255
256         export = kmem_cache_alloc(export_cachep, GFP_KERNEL); 
257         if ( !export ) {
258                 CERROR("no memory! (minor %d)\n", obd->obd_minor);
259                 return -ENOMEM;
260         }
261
262         memset(export, 0, sizeof(*export));
263         get_random_bytes(&export->export_cookie, sizeof(__u64));
264         export->export_obd = obd; 
265         export->export_import.addr = conn->addr;
266         export->export_import.cookie = conn->cookie;
267         
268         list_add(&(export->export_chain), export->export_obd->obd_exports.prev);
269         conn->addr = (__u64) (unsigned long)export;
270         conn->cookie = export->export_cookie;
271         return 0;
272
273
274 int class_disconnect(struct lustre_handle *conn)
275 {
276         struct obd_export * export;
277         ENTRY;
278
279         if (!(export = class_conn2export(conn))) {
280                 fixme();
281                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
282                        "nonexistent client %Lx\n", conn->addr);
283                 RETURN(-EINVAL);
284         }
285         list_del(&export->export_chain);
286         kmem_cache_free(export_cachep, export);
287
288         RETURN(0);
289
290
291 #if 0
292
293 /* FIXME: Data is a space- or comma-separated list of device IDs.  This will
294  * have to change. */
295 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
296 {
297         int count, rc;
298         char *p;
299         ENTRY;
300
301         for (p = data, count = 0; p < (char *)data + len; count++) {
302                 char *end;
303                 int tmp = simple_strtoul(p, &end, 0);
304
305                 if (p == end) {
306                         CERROR("invalid device ID starting at: %s\n", p);
307                         GOTO(err_disconnect, rc = -EINVAL);
308                 }
309                 
310                 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) { 
311                         CERROR("Trying to sub dev %d  - dev no too large\n", 
312                                tmp);
313                         GOTO(err_disconnect, rc  = -EINVAL); 
314                 }
315
316                 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
317                 if (rc) {
318                         CERROR("cannot connect to device %d: rc = %d\n", tmp,
319                                rc);
320                         GOTO(err_disconnect, rc);
321                 }
322
323                 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
324                        obd_dev[tmp].obd_type->typ_name);
325
326                 p = end + 1;
327         }
328
329         obddev->obd_multi_count = count;
330
331         RETURN(0);
332
333  err_disconnect:
334         for (count--; count >= 0; count--)
335                 obd_disconnect(&obddev->obd_multi_conn[count]);
336         return rc;
337 }
338
339 /*
340  *    remove all connections to this device
341  *    close all connections to lower devices
342  *    needed for forced unloads of OBD client drivers
343  */
344 int class_multi_cleanup(struct obd_device *obddev)
345 {
346         int i;
347
348         for (i = 0; i < obddev->obd_multi_count; i++) {
349                 int rc;
350                 struct obd_device *obd = class_conn2obd(&obddev->obd_multi_conn[i]);
351
352                 if (!obd) { 
353                         CERROR("no such device [i %d]\n", i); 
354                         RETURN(-EINVAL);
355                 }
356
357                 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
358                 if (rc)
359                         CERROR("disconnect failure %d\n", obd->obd_minor);
360         }
361         return 0;
362 }
363 #endif