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