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