Whamcloud - gitweb
- Add a slab for allocating OSC lock handles - 16-byte handles are a waste
[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 kmem_cache_t *handle_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;
34         ENTRY;
35
36         LASSERT(cbd);
37         desc = cbd->desc;
38
39         LASSERT(desc);
40         LASSERT(desc->bd_connection);
41
42         CERROR("IO of %d pages to/from %s:%d (conn %p) timed out\n",
43                desc->bd_page_count, desc->bd_connection->c_remote_uuid,
44                desc->bd_portal, desc->bd_connection);
45         desc->bd_connection->c_level = LUSTRE_CONN_RECOVD;
46         desc->bd_flags |= PTL_RPC_FL_TIMEOUT;
47         if (desc->bd_connection && class_signal_connection_failure) {
48                 class_signal_connection_failure(desc->bd_connection);
49
50                 /* We go back to sleep, until we're resumed or interrupted. */
51                 RETURN(0);
52         }
53
54         /* If we can't be recovered, just abort the syscall with -ETIMEDOUT. */
55         RETURN(1);
56 }
57
58 static int sync_io_intr(void *data)
59 {
60         struct io_cb_data *cbd = data;
61         struct ptlrpc_bulk_desc *desc = cbd->desc;
62
63         ENTRY;
64         desc->bd_flags |= PTL_RPC_FL_INTR;
65         RETURN(1); /* ignored, as of this writing */
66 }
67
68 int ll_sync_io_cb(struct io_cb_data *data, int err, int phase)
69 {
70         int ret;
71         ENTRY;
72
73         if (phase == CB_PHASE_START) {
74                 struct l_wait_info lwi;
75                 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, sync_io_timeout,
76                                        sync_io_intr, data);
77                 ret = l_wait_event(data->waitq, data->complete, &lwi);
78                 if (atomic_dec_and_test(&data->refcount))
79                         OBD_FREE(data, sizeof(*data));
80                 if (ret == -EINTR)
81                         return ret;
82         } else if (phase == CB_PHASE_FINISH) {
83                 data->err = err;
84                 data->complete = 1;
85                 wake_up(&data->waitq);
86                 if (atomic_dec_and_test(&data->refcount))
87                         OBD_FREE(data, sizeof(*data));
88                 return err;
89         } else
90                 LBUG();
91         EXIT;
92         return 0;
93 }
94
95 struct io_cb_data *ll_init_cb(void)
96 {
97         struct io_cb_data *d;
98
99         OBD_ALLOC(d, sizeof(*d));
100         if (d) {
101                 init_waitqueue_head(&d->waitq);
102                 atomic_set(&d->refcount, 2);
103         }
104         RETURN(d);
105 }
106
107 /*
108  * support functions: we could use inter-module communication, but this
109  * is more portable to other OS's
110  */
111 static struct obd_type *class_search_type(char *nm)
112 {
113         struct list_head *tmp;
114         struct obd_type *type;
115         CDEBUG(D_INFO, "SEARCH %s\n", nm);
116
117         tmp = &obd_types;
118         list_for_each(tmp, &obd_types) {
119                 type = list_entry(tmp, struct obd_type, typ_chain);
120                 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
121                 if (strlen(type->typ_name) == strlen(nm) &&
122                     strcmp(type->typ_name, nm) == 0 ) {
123                         return type;
124                 }
125         }
126         return NULL;
127 }
128
129 struct obd_type *class_nm_to_type(char *nm)
130 {
131         struct obd_type *type = class_search_type(nm);
132
133 #ifdef CONFIG_KMOD
134         if ( !type ) {
135                 if ( !request_module(nm) ) {
136                         CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
137                         type = class_search_type(nm);
138                 } else {
139                         CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
140                 }
141         }
142 #endif
143         return type;
144 }
145
146 int class_register_type(struct obd_ops *ops, char *nm)
147 {
148         struct obd_type *type;
149
150         ENTRY;
151
152         if (class_search_type(nm)) {
153                 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
154                 RETURN(-EEXIST);
155         }
156
157         OBD_ALLOC(type, sizeof(*type));
158         OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
159         OBD_ALLOC(type->typ_name, strlen(nm) + 1);
160         if (!type)
161                 RETURN(-ENOMEM);
162         INIT_LIST_HEAD(&type->typ_chain);
163         MOD_INC_USE_COUNT;
164         list_add(&type->typ_chain, &obd_types);
165         memcpy(type->typ_ops, ops, sizeof(*type->typ_ops));
166         strcpy(type->typ_name, nm);
167         RETURN(0);
168 }
169
170 int class_unregister_type(char *nm)
171 {
172         struct obd_type *type = class_nm_to_type(nm);
173
174         ENTRY;
175
176         if (!type) {
177                 CERROR("unknown obd type\n");
178                 RETURN(-EINVAL);
179         }
180
181         if (type->typ_refcnt) {
182                 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
183                 /* This is a bad situation, let's make the best of it */
184                 /* Remove ops, but leave the name for debugging */
185                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
186                 RETURN(-EBUSY);
187         }
188
189         list_del(&type->typ_chain);
190         OBD_FREE(type->typ_name, strlen(nm) + 1);
191         if (type->typ_ops != NULL)
192                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
193         OBD_FREE(type, sizeof(*type));
194         MOD_DEC_USE_COUNT;
195         RETURN(0);
196 } /* class_unregister_type */
197
198 int class_name2dev(char *name)
199 {
200         int res = -1;
201         int i;
202
203         if (!name)
204                 return -1;
205
206         for (i=0; i < MAX_OBD_DEVICES; i++) {
207                 struct obd_device *obd = &obd_dev[i];
208                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
209                         res = i;
210                         return res;
211                 }
212         }
213
214         return res;
215 }
216
217 int class_uuid2dev(char *uuid)
218 {
219         int res = -1;
220         int i;
221
222         for (i=0; i < MAX_OBD_DEVICES; i++) {
223                 struct obd_device *obd = &obd_dev[i];
224                 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0) {
225                         res = i;
226                         return res;
227                 }
228         }
229
230         return res;
231 }
232
233
234 struct obd_device *class_uuid2obd(char *uuid)
235 {
236         int i;
237
238         for (i=0; i < MAX_OBD_DEVICES; i++) {
239                 struct obd_device *obd = &obd_dev[i];
240                 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0)
241                         return obd;
242         }
243
244         return NULL;
245 }
246
247 void obd_cleanup_caches(void)
248 {
249         int rc;
250         ENTRY;
251         if (obdo_cachep) {
252                 rc = kmem_cache_destroy(obdo_cachep);
253                 if (rc)
254                         CERROR("Cannot destory ll_obdo_cache\n");
255                 obdo_cachep = NULL;
256         }
257         if (import_cachep) {
258                 rc = kmem_cache_destroy(import_cachep);
259                 if (rc)
260                         CERROR("Cannot destory ll_import_cache\n");
261                 import_cachep = NULL;
262         }
263         if (export_cachep) {
264                 rc = kmem_cache_destroy(export_cachep);
265                 if (rc)
266                         CERROR("Cannot destory ll_export_cache\n");
267                 export_cachep = NULL;
268         }
269         if (handle_cachep) {
270                 rc = kmem_cache_destroy(handle_cachep);
271                 if (rc)
272                         CERROR("Cannot destory ll_handle_cache\n");
273                 handle_cachep = NULL;
274         }
275         EXIT;
276 }
277
278 int obd_init_caches(void)
279 {
280         ENTRY;
281         LASSERT(obdo_cachep == NULL);
282         obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
283                                         0, 0, NULL, NULL);
284         if (!obdo_cachep)
285                 GOTO(out, -ENOMEM);
286
287         LASSERT(export_cachep == NULL);
288         export_cachep = kmem_cache_create("ll_export_cache",
289                                           sizeof(struct obd_export),
290                                           0, 0, NULL, NULL);
291         if (!export_cachep)
292                 GOTO(out, -ENOMEM);
293
294         LASSERT(import_cachep == NULL);
295         import_cachep = kmem_cache_create("ll_import_cache",
296                                           sizeof(struct obd_import),
297                                           0, 0, NULL, NULL);
298         if (!import_cachep)
299                 GOTO(out, -ENOMEM);
300
301         LASSERT(handle_cachep == NULL);
302         handle_cachep = kmem_cache_create("ll_handle_cache",
303                                           sizeof(struct lustre_handle),
304                                           0, 0, NULL, NULL);
305         if (!handle_cachep)
306                 RETURN(-ENOMEM);
307
308         RETURN(0);
309  out:
310         obd_cleanup_caches();
311         RETURN(-ENOMEM);
312
313 }
314
315 /* map connection to client */
316 struct obd_export *class_conn2export(struct lustre_handle *conn)
317 {
318         struct obd_export *export;
319
320         if (!conn) {
321                 CDEBUG(D_CACHE, "looking for null handle\n");
322                 RETURN(NULL);
323         }
324
325         if (conn->addr == -1) {  /* this means assign a new connection */
326                 CDEBUG(D_CACHE, "want a new connection\n");
327                 RETURN(NULL);
328         }
329
330         if (!conn->addr) {
331                 CDEBUG(D_CACHE, "looking for null addr\n");
332                 fixme();
333                 RETURN(NULL);
334         }
335
336         CDEBUG(D_IOCTL, "looking for export addr "LPX64" cookie "LPX64"\n",
337                conn->addr, conn->cookie);
338         export = (struct obd_export *) (unsigned long)conn->addr;
339         if (!kmem_cache_validate(export_cachep, (void *)export))
340                 RETURN(NULL);
341
342         if (export->exp_cookie != conn->cookie)
343                 return NULL;
344         return export;
345 } /* class_conn2export */
346
347 struct obd_device *class_conn2obd(struct lustre_handle *conn)
348 {
349         struct obd_export *export;
350         export = class_conn2export(conn);
351         if (export)
352                 return export->exp_obd;
353         fixme();
354         return NULL;
355 }
356
357 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
358 {
359         return &class_conn2obd(conn)->u.cli.cl_import;
360 }
361
362 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
363 {
364         return &class_conn2export(conn)->exp_ldlm_data.led_import;
365 }
366
367 struct obd_export *class_new_export(struct obd_device *obddev)
368 {
369         struct obd_export * export;
370
371         export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
372         if (!export) {
373                 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
374                 return NULL;
375         }
376
377         memset(export, 0, sizeof(*export));
378         get_random_bytes(&export->exp_cookie, sizeof(export->exp_cookie));
379         export->exp_obd = obddev;
380         /* XXX this should be in LDLM init */
381         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
382         INIT_LIST_HEAD(&export->exp_conn_chain);
383         spin_lock(&obddev->obd_dev_lock);
384         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
385         spin_unlock(&obddev->obd_dev_lock);
386         return export;
387 }
388
389 void class_destroy_export(struct obd_export *exp)
390 {
391         ENTRY;
392
393         spin_lock(&exp->exp_obd->obd_dev_lock);
394         list_del(&exp->exp_obd_chain);
395         spin_unlock(&exp->exp_obd->obd_dev_lock);
396
397         /* XXXshaver no connection here... */
398         if (exp->exp_connection)
399                 spin_lock(&exp->exp_connection->c_lock);
400         list_del(&exp->exp_conn_chain);
401         if (exp->exp_connection) {
402                 spin_unlock(&exp->exp_connection->c_lock);
403                 //ptlrpc_put_connection(exp->exp_connection);
404         }
405
406         exp->exp_cookie = DEAD_HANDLE_MAGIC;
407         kmem_cache_free(export_cachep, exp);
408
409         EXIT;
410 }
411
412 /* a connection defines an export context in which preallocation can
413    be managed. */
414 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
415                   obd_uuid_t cluuid)
416 {
417         struct obd_export * export;
418         if (conn == NULL) {
419                 LBUG();
420                 return -EINVAL;
421         }
422
423         if (obd == NULL) {
424                 LBUG();
425                 return -EINVAL;
426         }
427
428         export = class_new_export(obd);
429         if (!export)
430                 return -ENOMEM;
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 "LPX64"\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 =
555                         class_conn2obd(&obddev->obd_multi_conn[i]);
556
557                 if (!obd) {
558                         CERROR("no such device [i %d]\n", i);
559                         RETURN(-EINVAL);
560                 }
561
562                 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
563                 if (rc)
564                         CERROR("disconnect failure %d\n", obd->obd_minor);
565         }
566         return 0;
567 }
568 #endif