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