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