Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[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  *  Copyright (c) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * These are the only exported functions, they provide some generic
22  * infrastructure for managing object devices
23  */
24
25 #define DEBUG_SUBSYSTEM S_CLASS
26 #include <linux/kmod.h>   /* for request_module() */
27 #include <linux/module.h>
28 #include <linux/obd_class.h>
29 #include <linux/random.h>
30 #include <linux/slab.h>
31 #include <linux/lprocfs_status.h>
32
33 extern struct list_head obd_types;
34 kmem_cache_t *obdo_cachep = NULL;
35 kmem_cache_t *import_cachep = NULL;
36 kmem_cache_t *export_cachep = NULL;
37
38 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
39
40 /*
41  * support functions: we could use inter-module communication, but this
42  * is more portable to other OS's
43  */
44 static struct obd_type *class_search_type(char *name)
45 {
46         struct list_head *tmp;
47         struct obd_type *type;
48         CDEBUG(D_INFO, "SEARCH %s\n", name);
49
50         tmp = &obd_types;
51         list_for_each(tmp, &obd_types) {
52                 type = list_entry(tmp, struct obd_type, typ_chain);
53                 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
54                 if (strlen(type->typ_name) == strlen(name) &&
55                     strcmp(type->typ_name, name) == 0) {
56                         return type;
57                 }
58         }
59         return NULL;
60 }
61
62 struct obd_type *class_get_type(char *name)
63 {
64         struct obd_type *type = class_search_type(name);
65
66 #ifdef CONFIG_KMOD
67         if (!type) {
68                 if (!request_module(name)) {
69                         CDEBUG(D_INFO, "Loaded module '%s'\n", name);
70                         type = class_search_type(name);
71                 } else
72                         CDEBUG(D_INFO, "Can't load module '%s'\n", name);
73         }
74 #endif
75         if (type)
76                 __MOD_INC_USE_COUNT(type->typ_ops->o_owner);
77         return type;
78 }
79
80 void class_put_type(struct obd_type *type)
81 {
82         LASSERT(type);
83         __MOD_DEC_USE_COUNT(type->typ_ops->o_owner);
84 }
85
86 int class_register_type(struct obd_ops *ops, struct lprocfs_vars *vars,
87                         char *name)
88 {
89         struct obd_type *type;
90         int rc;
91         ENTRY;
92
93         LASSERT(strnlen(name, 1024) < 1024);    /* sanity check */
94
95         if (class_search_type(name)) {
96                 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
97                 RETURN(-EEXIST);
98         }
99
100         rc = -ENOMEM;
101         OBD_ALLOC(type, sizeof(*type));
102         if (type == NULL)
103                 RETURN(rc);
104
105         OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
106         OBD_ALLOC(type->typ_name, strlen(name) + 1);
107         if (type->typ_ops == NULL || type->typ_name == NULL)
108                 GOTO (failed, rc);
109
110         *(type->typ_ops) = *ops;
111         strcpy(type->typ_name, name);
112         list_add(&type->typ_chain, &obd_types);
113
114         rc = lprocfs_reg_class(type, vars, type);
115         if (rc != 0) {
116                 list_del(&type->typ_chain);
117                 GOTO(failed, rc);
118         }
119
120         RETURN (0);
121
122  failed:
123         if (type->typ_ops != NULL)
124                 OBD_FREE(type->typ_name, strlen(name) + 1);
125         if (type->typ_ops != NULL)
126                 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
127         RETURN(rc);
128 }
129
130 int class_unregister_type(char *name)
131 {
132         struct obd_type *type = class_search_type(name);
133         ENTRY;
134
135         if (!type) {
136                 CERROR("unknown obd type\n");
137                 RETURN(-EINVAL);
138         }
139
140         if (type->typ_refcnt) {
141                 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
142                 /* This is a bad situation, let's make the best of it */
143                 /* Remove ops, but leave the name for debugging */
144                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
145                 RETURN(-EBUSY);
146         }
147         if(type->typ_procroot)
148                 lprocfs_dereg_class(type);
149
150         list_del(&type->typ_chain);
151         OBD_FREE(type->typ_name, strlen(name) + 1);
152         if (type->typ_ops != NULL)
153                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
154         OBD_FREE(type, sizeof(*type));
155         RETURN(0);
156 } /* class_unregister_type */
157
158 int class_name2dev(char *name)
159 {
160         int res = -1;
161         int i;
162
163         if (!name)
164                 return -1;
165
166         for (i = 0; i < MAX_OBD_DEVICES; i++) {
167                 struct obd_device *obd = &obd_dev[i];
168                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
169                         res = i;
170                         return res;
171                 }
172         }
173
174         return res;
175 }
176
177 int class_uuid2dev(char *uuid)
178 {
179         int res = -1;
180         int i;
181
182         for (i = 0; i < MAX_OBD_DEVICES; i++) {
183                 struct obd_device *obd = &obd_dev[i];
184                 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0) {
185                         res = i;
186                         return res;
187                 }
188         }
189
190         return res;
191 }
192
193
194 struct obd_device *class_uuid2obd(char *uuid)
195 {
196         int i;
197
198         for (i = 0; i < MAX_OBD_DEVICES; i++) {
199                 struct obd_device *obd = &obd_dev[i];
200                 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0)
201                         return obd;
202         }
203
204         return NULL;
205 }
206
207 void obd_cleanup_caches(void)
208 {
209         int rc;
210         ENTRY;
211         if (obdo_cachep) {
212                 rc = kmem_cache_destroy(obdo_cachep);
213                 if (rc)
214                         CERROR("Cannot destory ll_obdo_cache\n");
215                 obdo_cachep = NULL;
216         }
217         if (import_cachep) {
218                 rc = kmem_cache_destroy(import_cachep);
219                 if (rc)
220                         CERROR("Cannot destory ll_import_cache\n");
221                 import_cachep = NULL;
222         }
223         if (export_cachep) {
224                 rc = kmem_cache_destroy(export_cachep);
225                 if (rc)
226                         CERROR("Cannot destory ll_export_cache\n");
227                 export_cachep = NULL;
228         }
229         EXIT;
230 }
231
232 int obd_init_caches(void)
233 {
234         ENTRY;
235         LASSERT(obdo_cachep == NULL);
236         obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
237                                         0, 0, NULL, NULL);
238         if (!obdo_cachep)
239                 GOTO(out, -ENOMEM);
240
241         LASSERT(export_cachep == NULL);
242         export_cachep = kmem_cache_create("ll_export_cache",
243                                           sizeof(struct obd_export),
244                                           0, 0, NULL, NULL);
245         if (!export_cachep)
246                 GOTO(out, -ENOMEM);
247
248         LASSERT(import_cachep == NULL);
249         import_cachep = kmem_cache_create("ll_import_cache",
250                                           sizeof(struct obd_import),
251                                           0, 0, NULL, NULL);
252         if (!import_cachep)
253                 GOTO(out, -ENOMEM);
254
255         RETURN(0);
256  out:
257         obd_cleanup_caches();
258         RETURN(-ENOMEM);
259
260 }
261
262 /* map connection to client */
263 struct obd_export *class_conn2export(struct lustre_handle *conn)
264 {
265         struct obd_export *export;
266         ENTRY;
267
268         if (!conn) {
269                 CDEBUG(D_CACHE, "looking for null handle\n");
270                 RETURN(NULL);
271         }
272
273         if (conn->addr == -1) {  /* this means assign a new connection */
274                 CDEBUG(D_CACHE, "want a new connection\n");
275                 RETURN(NULL);
276         }
277
278         if (!conn->addr) {
279                 CDEBUG(D_CACHE, "looking for null addr\n");
280                 fixme();
281                 RETURN(NULL);
282         }
283
284         CDEBUG(D_IOCTL, "looking for export addr "LPX64" cookie "LPX64"\n",
285                conn->addr, conn->cookie);
286         export = (struct obd_export *) (unsigned long)conn->addr;
287         if (!kmem_cache_validate(export_cachep, (void *)export))
288                 RETURN(NULL);
289
290         if (export->exp_cookie != conn->cookie)
291                 RETURN(NULL);
292         RETURN(export);
293 } /* class_conn2export */
294
295 struct obd_device *class_conn2obd(struct lustre_handle *conn)
296 {
297         struct obd_export *export;
298         export = class_conn2export(conn);
299         if (export)
300                 return export->exp_obd;
301         fixme();
302         return NULL;
303 }
304
305 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
306 {
307         return &class_conn2obd(conn)->u.cli.cl_import;
308 }
309
310 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
311 {
312         return &class_conn2export(conn)->exp_ldlm_data.led_import;
313 }
314
315 struct obd_export *class_new_export(struct obd_device *obddev)
316 {
317         struct obd_export * export;
318
319         export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
320         if (!export) {
321                 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
322                 return NULL;
323         }
324
325         memset(export, 0, sizeof(*export));
326         get_random_bytes(&export->exp_cookie, sizeof(export->exp_cookie));
327         export->exp_obd = obddev;
328         /* XXX this should be in LDLM init */
329         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
330         INIT_LIST_HEAD(&export->exp_conn_chain);
331         spin_lock(&obddev->obd_dev_lock);
332         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
333         spin_unlock(&obddev->obd_dev_lock);
334         return export;
335 }
336
337 void class_destroy_export(struct obd_export *exp)
338 {
339         ENTRY;
340
341         LASSERT(exp->exp_cookie != DEAD_HANDLE_MAGIC);
342
343         spin_lock(&exp->exp_obd->obd_dev_lock);
344         list_del(&exp->exp_obd_chain);
345         spin_unlock(&exp->exp_obd->obd_dev_lock);
346
347         /* XXXshaver no connection here... */
348         if (exp->exp_connection)
349                 spin_lock(&exp->exp_connection->c_lock);
350         list_del(&exp->exp_conn_chain);
351         if (exp->exp_connection) {
352                 spin_unlock(&exp->exp_connection->c_lock);
353                 ptlrpc_put_connection_superhack(exp->exp_connection);
354         }
355
356         exp->exp_cookie = DEAD_HANDLE_MAGIC;
357         kmem_cache_free(export_cachep, exp);
358
359         EXIT;
360 }
361
362 /* a connection defines an export context in which preallocation can
363    be managed. */
364 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
365                   obd_uuid_t cluuid)
366 {
367         struct obd_export * export;
368         if (conn == NULL) {
369                 LBUG();
370                 return -EINVAL;
371         }
372
373         if (obd == NULL) {
374                 LBUG();
375                 return -EINVAL;
376         }
377
378         export = class_new_export(obd);
379         if (!export)
380                 return -ENOMEM;
381
382         conn->addr = (__u64) (unsigned long)export;
383         conn->cookie = export->exp_cookie;
384
385         CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
386                (long long)conn->addr, (long long)conn->cookie);
387         return 0;
388 }
389
390 int class_disconnect(struct lustre_handle *conn)
391 {
392         struct obd_export *export;
393         ENTRY;
394
395         if (!(export = class_conn2export(conn))) {
396                 fixme();
397                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
398                        "nonexistent client "LPX64"\n", conn->addr);
399                 RETURN(-EINVAL);
400         }
401
402         CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
403                        (long long)conn->addr, (long long)conn->cookie);
404
405         class_destroy_export(export);
406
407         RETURN(0);
408 }
409
410 void class_disconnect_all(struct obd_device *obddev)
411 {
412         int again = 1;
413
414         while (again) {
415                 spin_lock(&obddev->obd_dev_lock);
416                 if (!list_empty(&obddev->obd_exports)) {
417                         struct obd_export *export;
418                         struct lustre_handle conn;
419                         int rc;
420
421                         export = list_entry(obddev->obd_exports.next,
422                                             struct obd_export,
423                                             exp_obd_chain);
424                         conn.addr = (__u64)(unsigned long)export;
425                         conn.cookie = export->exp_cookie;
426                         spin_unlock(&obddev->obd_dev_lock);
427                         CERROR("force disconnecting %s:%s export %p\n",
428                                export->exp_obd->obd_type->typ_name,
429                                export->exp_connection ?
430                                (char *)export->exp_connection->c_remote_uuid :
431                                "<unconnected>", export);
432                         rc = obd_disconnect(&conn);
433                         if (rc < 0) {
434                                 /* AED: not so sure about this...  We can't
435                                  * loop here forever, yet we shouldn't leak
436                                  * exports on a struct we will soon destroy.
437                                  */
438                                 CERROR("destroy export %p with err: rc = %d\n",
439                                        export, rc);
440                                 class_destroy_export(export);
441                         }
442                 } else {
443                         spin_unlock(&obddev->obd_dev_lock);
444                         again = 0;
445                 }
446         }
447 }
448
449 #if 0
450
451 /* FIXME: Data is a space- or comma-separated list of device IDs.  This will
452  * have to change. */
453 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
454 {
455         int count, rc;
456         char *p;
457         ENTRY;
458
459         for (p = data, count = 0; p < (char *)data + len; count++) {
460                 char *end;
461                 int tmp = simple_strtoul(p, &end, 0);
462
463                 if (p == end) {
464                         CERROR("invalid device ID starting at: %s\n", p);
465                         GOTO(err_disconnect, rc = -EINVAL);
466                 }
467
468                 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
469                         CERROR("Trying to sub dev %d  - dev no too large\n",
470                                tmp);
471                         GOTO(err_disconnect, rc  = -EINVAL);
472                 }
473
474                 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
475                 if (rc) {
476                         CERROR("cannot connect to device %d: rc = %d\n", tmp,
477                                rc);
478                         GOTO(err_disconnect, rc);
479                 }
480
481                 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
482                        obd_dev[tmp].obd_type->typ_name);
483
484                 p = end + 1;
485         }
486
487         obddev->obd_multi_count = count;
488
489         RETURN(0);
490
491  err_disconnect:
492         for (count--; count >= 0; count--)
493                 obd_disconnect(&obddev->obd_multi_conn[count]);
494         return rc;
495 }
496
497 /*
498  *    remove all connections to this device
499  *    close all connections to lower devices
500  *    needed for forced unloads of OBD client drivers
501  */
502 int class_multi_cleanup(struct obd_device *obddev)
503 {
504         int i;
505
506         for (i = 0; i < obddev->obd_multi_count; i++) {
507                 int rc;
508                 struct obd_device *obd =
509                         class_conn2obd(&obddev->obd_multi_conn[i]);
510
511                 if (!obd) {
512                         CERROR("no such device [i %d]\n", i);
513                         RETURN(-EINVAL);
514                 }
515
516                 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
517                 if (rc)
518                         CERROR("disconnect failure %d\n", obd->obd_minor);
519         }
520         return 0;
521 }
522 #endif