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