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