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