Whamcloud - gitweb
land b_md onto HEAD. the highlights:
[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 *nm)
45 {
46         struct list_head *tmp;
47         struct obd_type *type;
48         CDEBUG(D_INFO, "SEARCH %s\n", nm);
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(nm) &&
55                     strcmp(type->typ_name, nm) == 0 ) {
56                         return type;
57                 }
58         }
59         return NULL;
60 }
61
62 struct obd_type *class_nm_to_type(char *nm)
63 {
64         struct obd_type *type = class_search_type(nm);
65
66 #ifdef CONFIG_KMOD
67         if ( !type ) {
68                 if ( !request_module(nm) ) {
69                         CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
70                         type = class_search_type(nm);
71                 } else {
72                         CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
73                 }
74         }
75 #endif
76         return type;
77 }
78
79 int class_register_type(struct obd_ops *ops, struct lprocfs_vars *vars,
80                         char *nm)
81 {
82         struct obd_type *type;
83         int rc;
84
85         ENTRY;
86
87         LASSERT (strnlen (nm, 1024) < 1024);    /* sanity check */
88         
89         if (class_search_type(nm)) {
90                 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
91                 RETURN(-EEXIST);
92         }
93
94         rc = -ENOMEM;
95         OBD_ALLOC(type, sizeof(*type));
96         if (type == NULL)
97                 RETURN(rc);
98
99         OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
100         OBD_ALLOC(type->typ_name, strlen(nm) + 1);
101         if (type->typ_ops == NULL ||
102             type->typ_name == NULL)
103                 GOTO (failed, rc);
104         
105         *(type->typ_ops) = *ops;
106         strcpy(type->typ_name, nm);
107         list_add(&type->typ_chain, &obd_types);
108
109         rc = lprocfs_reg_class(type, vars, type);
110         if (rc != 0) {
111                 list_del (&type->typ_chain);
112                 GOTO (failed, rc);
113         }
114         
115         CDEBUG(D_INFO, "MOD_INC_USE for register_type: count = %d\n",
116                atomic_read(&(THIS_MODULE)->uc.usecount));
117         MOD_INC_USE_COUNT;
118         RETURN (0);
119
120  failed:
121         if (type->typ_ops != NULL)
122                 OBD_FREE (type->typ_name, strlen (nm) + 1);
123         if (type->typ_ops != NULL)
124                 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
125         RETURN(rc);
126 }
127
128 int class_unregister_type(char *nm)
129 {
130         struct obd_type *type = class_nm_to_type(nm);
131
132         ENTRY;
133
134         if (!type) {
135                 CERROR("unknown obd type\n");
136                 RETURN(-EINVAL);
137         }
138
139         if (type->typ_refcnt) {
140                 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
141                 /* This is a bad situation, let's make the best of it */
142                 /* Remove ops, but leave the name for debugging */
143                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
144                 RETURN(-EBUSY);
145         }
146         if(type->typ_procroot)
147                 lprocfs_dereg_class(type);
148
149         list_del(&type->typ_chain);
150         OBD_FREE(type->typ_name, strlen(nm) + 1);
151         if (type->typ_ops != NULL)
152                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
153         OBD_FREE(type, sizeof(*type));
154         CDEBUG(D_INFO, "MOD_DEC_USE for register_type: count = %d\n",
155                atomic_read(&(THIS_MODULE)->uc.usecount) - 1);
156         MOD_DEC_USE_COUNT;
157         RETURN(0);
158 } /* class_unregister_type */
159
160 int class_name2dev(char *name)
161 {
162         int res = -1;
163         int i;
164
165         if (!name)
166                 return -1;
167
168         for (i=0; i < MAX_OBD_DEVICES; i++) {
169                 struct obd_device *obd = &obd_dev[i];
170                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
171                         res = i;
172                         return res;
173                 }
174         }
175
176         return res;
177 }
178
179 int class_uuid2dev(char *uuid)
180 {
181         int res = -1;
182         int i;
183
184         for (i=0; i < MAX_OBD_DEVICES; i++) {
185                 struct obd_device *obd = &obd_dev[i];
186                 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0) {
187                         res = i;
188                         return res;
189                 }
190         }
191
192         return res;
193 }
194
195
196 struct obd_device *class_uuid2obd(char *uuid)
197 {
198         int i;
199
200         for (i=0; i < MAX_OBD_DEVICES; i++) {
201                 struct obd_device *obd = &obd_dev[i];
202                 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0)
203                         return obd;
204         }
205
206         return NULL;
207 }
208
209 void obd_cleanup_caches(void)
210 {
211         int rc;
212         ENTRY;
213         if (obdo_cachep) {
214                 rc = kmem_cache_destroy(obdo_cachep);
215                 if (rc)
216                         CERROR("Cannot destory ll_obdo_cache\n");
217                 obdo_cachep = NULL;
218         }
219         if (import_cachep) {
220                 rc = kmem_cache_destroy(import_cachep);
221                 if (rc)
222                         CERROR("Cannot destory ll_import_cache\n");
223                 import_cachep = NULL;
224         }
225         if (export_cachep) {
226                 rc = kmem_cache_destroy(export_cachep);
227                 if (rc)
228                         CERROR("Cannot destory ll_export_cache\n");
229                 export_cachep = NULL;
230         }
231         EXIT;
232 }
233
234 int obd_init_caches(void)
235 {
236         ENTRY;
237         LASSERT(obdo_cachep == NULL);
238         obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
239                                         0, 0, NULL, NULL);
240         if (!obdo_cachep)
241                 GOTO(out, -ENOMEM);
242
243         LASSERT(export_cachep == NULL);
244         export_cachep = kmem_cache_create("ll_export_cache",
245                                           sizeof(struct obd_export),
246                                           0, 0, NULL, NULL);
247         if (!export_cachep)
248                 GOTO(out, -ENOMEM);
249
250         LASSERT(import_cachep == NULL);
251         import_cachep = kmem_cache_create("ll_import_cache",
252                                           sizeof(struct obd_import),
253                                           0, 0, NULL, NULL);
254         if (!import_cachep)
255                 GOTO(out, -ENOMEM);
256
257         RETURN(0);
258  out:
259         obd_cleanup_caches();
260         RETURN(-ENOMEM);
261
262 }
263
264 /* map connection to client */
265 struct obd_export *class_conn2export(struct lustre_handle *conn)
266 {
267         struct obd_export *export;
268         ENTRY;
269
270         if (!conn) {
271                 CDEBUG(D_CACHE, "looking for null handle\n");
272                 RETURN(NULL);
273         }
274
275         if (conn->addr == -1) {  /* this means assign a new connection */
276                 CDEBUG(D_CACHE, "want a new connection\n");
277                 RETURN(NULL);
278         }
279
280         if (!conn->addr) {
281                 CDEBUG(D_CACHE, "looking for null addr\n");
282                 fixme();
283                 RETURN(NULL);
284         }
285
286         CDEBUG(D_IOCTL, "looking for export addr "LPX64" cookie "LPX64"\n",
287                conn->addr, conn->cookie);
288         export = (struct obd_export *) (unsigned long)conn->addr;
289         if (!kmem_cache_validate(export_cachep, (void *)export))
290                 RETURN(NULL);
291
292         if (export->exp_cookie != conn->cookie)
293                 RETURN(NULL);
294         RETURN(export);
295 } /* class_conn2export */
296
297 struct obd_device *class_conn2obd(struct lustre_handle *conn)
298 {
299         struct obd_export *export;
300         export = class_conn2export(conn);
301         if (export)
302                 return export->exp_obd;
303         fixme();
304         return NULL;
305 }
306
307 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
308 {
309         return &class_conn2obd(conn)->u.cli.cl_import;
310 }
311
312 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
313 {
314         return &class_conn2export(conn)->exp_ldlm_data.led_import;
315 }
316
317 struct obd_export *class_new_export(struct obd_device *obddev)
318 {
319         struct obd_export * export;
320
321         export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
322         if (!export) {
323                 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
324                 return NULL;
325         }
326
327         memset(export, 0, sizeof(*export));
328         get_random_bytes(&export->exp_cookie, sizeof(export->exp_cookie));
329         export->exp_obd = obddev;
330         /* XXX this should be in LDLM init */
331         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
332         INIT_LIST_HEAD(&export->exp_conn_chain);
333         spin_lock(&obddev->obd_dev_lock);
334         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
335         spin_unlock(&obddev->obd_dev_lock);
336         return export;
337 }
338
339 void class_destroy_export(struct obd_export *exp)
340 {
341         ENTRY;
342
343         LASSERT(exp->exp_cookie != DEAD_HANDLE_MAGIC);
344
345         spin_lock(&exp->exp_obd->obd_dev_lock);
346         list_del(&exp->exp_obd_chain);
347         spin_unlock(&exp->exp_obd->obd_dev_lock);
348
349         /* XXXshaver no connection here... */
350         if (exp->exp_connection)
351                 spin_lock(&exp->exp_connection->c_lock);
352         list_del(&exp->exp_conn_chain);
353         if (exp->exp_connection) {
354                 spin_unlock(&exp->exp_connection->c_lock);
355                 ptlrpc_put_connection_superhack(exp->exp_connection);
356         }
357
358         exp->exp_cookie = DEAD_HANDLE_MAGIC;
359         kmem_cache_free(export_cachep, exp);
360
361         EXIT;
362 }
363
364 /* a connection defines an export context in which preallocation can
365    be managed. */
366 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
367                   obd_uuid_t cluuid)
368 {
369         struct obd_export * export;
370         if (conn == NULL) {
371                 LBUG();
372                 return -EINVAL;
373         }
374
375         if (obd == NULL) {
376                 LBUG();
377                 return -EINVAL;
378         }
379
380         export = class_new_export(obd);
381         if (!export)
382                 return -ENOMEM;
383
384         conn->addr = (__u64) (unsigned long)export;
385         conn->cookie = export->exp_cookie;
386
387         CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
388                (long long)conn->addr, (long long)conn->cookie);
389         return 0;
390 }
391
392 int class_disconnect(struct lustre_handle *conn)
393 {
394         struct obd_export *export;
395         ENTRY;
396
397         if (!(export = class_conn2export(conn))) {
398                 fixme();
399                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
400                        "nonexistent client "LPX64"\n", conn->addr);
401                 RETURN(-EINVAL);
402         }
403
404         CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
405                        (long long)conn->addr, (long long)conn->cookie);
406
407         class_destroy_export(export);
408
409         RETURN(0);
410 }
411
412 void class_disconnect_all(struct obd_device *obddev)
413 {
414         int again = 1;
415
416         while (again) {
417                 spin_lock(&obddev->obd_dev_lock);
418                 if (!list_empty(&obddev->obd_exports)) {
419                         struct obd_export *export;
420                         struct lustre_handle conn;
421                         int rc;
422
423                         export = list_entry(obddev->obd_exports.next,
424                                             struct obd_export,
425                                             exp_obd_chain);
426                         conn.addr = (__u64)(unsigned long)export;
427                         conn.cookie = export->exp_cookie;
428                         spin_unlock(&obddev->obd_dev_lock);
429                         CERROR("force disconnecting %s:%s export %p\n",
430                                export->exp_obd->obd_type->typ_name,
431                                export->exp_connection->c_remote_uuid, 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