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