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