Whamcloud - gitweb
b=617434
[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         desc->bd_connection->c_level = LUSTRE_CONN_RECOVD;
42         desc->bd_flags |= PTL_RPC_FL_TIMEOUT;
43         if (desc->bd_connection && class_signal_connection_failure) {
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 "LPX64" cookie "LPX64"\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(export->exp_cookie));
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         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)
389                 spin_lock(&exp->exp_connection->c_lock);
390         list_del(&exp->exp_conn_chain);
391         if (exp->exp_connection) {
392                 spin_unlock(&exp->exp_connection->c_lock);
393                 //ptlrpc_put_connection(exp->exp_connection);
394         }
395
396         kmem_cache_free(export_cachep, exp);
397
398         EXIT;
399 }
400
401 /* a connection defines an export context in which preallocation can
402    be managed. */
403 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
404                   obd_uuid_t cluuid)
405 {
406         struct obd_export * export;
407         if (conn == NULL) {
408                 LBUG();
409                 return -EINVAL;
410         }
411
412         if (obd == NULL) {
413                 LBUG();
414                 return -EINVAL;
415         }
416
417         export = class_new_export(obd);
418         if (!export)
419                 return -ENOMEM;
420
421
422         conn->addr = (__u64) (unsigned long)export;
423         conn->cookie = export->exp_cookie;
424
425         CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
426                (long long)conn->addr, (long long)conn->cookie);
427         return 0;
428 }
429
430 int class_disconnect(struct lustre_handle *conn)
431 {
432         struct obd_export *export;
433         ENTRY;
434
435         if (!(export = class_conn2export(conn))) {
436                 fixme();
437                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
438                        "nonexistent client "LPX64"\n", conn->addr);
439                 RETURN(-EINVAL);
440         }
441
442         CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
443                        (long long)conn->addr, (long long)conn->cookie);
444
445         class_destroy_export(export);
446
447         RETURN(0);
448 }
449
450 void class_disconnect_all(struct obd_device *obddev)
451 {
452         int again = 1;
453
454         while (again) {
455                 spin_lock(&obddev->obd_dev_lock);
456                 if (!list_empty(&obddev->obd_exports)) {
457                         struct obd_export *export;
458                         struct lustre_handle conn;
459                         int rc;
460
461                         export = list_entry(obddev->obd_exports.next,
462                                             struct obd_export,
463                                             exp_obd_chain);
464                         conn.addr = (__u64)(unsigned long)export;
465                         conn.cookie = export->exp_cookie;
466                         spin_unlock(&obddev->obd_dev_lock);
467                         CERROR("force disconnecting export %p\n", export);
468                         rc = obd_disconnect(&conn);
469                         if (rc < 0) {
470                                 /* AED: not so sure about this...  We can't
471                                  * loop here forever, yet we shouldn't leak
472                                  * exports on a struct we will soon destroy.
473                                  */
474                                 CERROR("destroy export %p with err: rc = %d\n",
475                                        export, rc);
476                                 class_destroy_export(export);
477                         }
478                 } else {
479                         spin_unlock(&obddev->obd_dev_lock);
480                         again = 0;
481                 }
482         }
483 }
484
485 #if 0
486
487 /* FIXME: Data is a space- or comma-separated list of device IDs.  This will
488  * have to change. */
489 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
490 {
491         int count, rc;
492         char *p;
493         ENTRY;
494
495         for (p = data, count = 0; p < (char *)data + len; count++) {
496                 char *end;
497                 int tmp = simple_strtoul(p, &end, 0);
498
499                 if (p == end) {
500                         CERROR("invalid device ID starting at: %s\n", p);
501                         GOTO(err_disconnect, rc = -EINVAL);
502                 }
503
504                 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
505                         CERROR("Trying to sub dev %d  - dev no too large\n",
506                                tmp);
507                         GOTO(err_disconnect, rc  = -EINVAL);
508                 }
509
510                 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
511                 if (rc) {
512                         CERROR("cannot connect to device %d: rc = %d\n", tmp,
513                                rc);
514                         GOTO(err_disconnect, rc);
515                 }
516
517                 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
518                        obd_dev[tmp].obd_type->typ_name);
519
520                 p = end + 1;
521         }
522
523         obddev->obd_multi_count = count;
524
525         RETURN(0);
526
527  err_disconnect:
528         for (count--; count >= 0; count--)
529                 obd_disconnect(&obddev->obd_multi_conn[count]);
530         return rc;
531 }
532
533 /*
534  *    remove all connections to this device
535  *    close all connections to lower devices
536  *    needed for forced unloads of OBD client drivers
537  */
538 int class_multi_cleanup(struct obd_device *obddev)
539 {
540         int i;
541
542         for (i = 0; i < obddev->obd_multi_count; i++) {
543                 int rc;
544                 struct obd_device *obd =
545                         class_conn2obd(&obddev->obd_multi_conn[i]);
546
547                 if (!obd) {
548                         CERROR("no such device [i %d]\n", i);
549                         RETURN(-EINVAL);
550                 }
551
552                 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
553                 if (rc)
554                         CERROR("disconnect failure %d\n", obd->obd_minor);
555         }
556         return 0;
557 }
558 #endif