Whamcloud - gitweb
Merge b_md into HEAD
[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  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * These are the only exported functions, they provide some generic
22  * infrastructure for managing object devices
23  */
24
25 #define DEBUG_SUBSYSTEM S_CLASS
26 #include <linux/kmod.h>   /* for request_module() */
27 #include <linux/module.h>
28 #include <linux/obd_class.h>
29 #include <linux/random.h>
30 #include <linux/slab.h>
31 #include <linux/lprocfs_status.h>
32
33 extern struct list_head obd_types;
34 kmem_cache_t *obdo_cachep = NULL;
35 kmem_cache_t *import_cachep = NULL;
36 kmem_cache_t *export_cachep = NULL;
37
38 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
39 void (*ptlrpc_abort_inflight_superhack)(struct obd_import *imp,
40                                         int dying_import);
41
42 /*
43  * support functions: we could use inter-module communication, but this
44  * is more portable to other OS's
45  */
46 static struct obd_type *class_search_type(char *name)
47 {
48         struct list_head *tmp;
49         struct obd_type *type;
50         CDEBUG(D_INFO, "SEARCH %s\n", name);
51
52         tmp = &obd_types;
53         list_for_each(tmp, &obd_types) {
54                 type = list_entry(tmp, struct obd_type, typ_chain);
55                 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
56                 if (strlen(type->typ_name) == strlen(name) &&
57                     strcmp(type->typ_name, name) == 0) {
58                         return type;
59                 }
60         }
61         return NULL;
62 }
63
64 struct obd_type *class_get_type(char *name)
65 {
66         struct obd_type *type = class_search_type(name);
67
68 #ifdef CONFIG_KMOD
69         if (!type) {
70                 if (!request_module(name)) {
71                         CDEBUG(D_INFO, "Loaded module '%s'\n", name);
72                         type = class_search_type(name);
73                 } else
74                         CDEBUG(D_INFO, "Can't load module '%s'\n", name);
75         }
76 #endif
77         if (type)
78                 __MOD_INC_USE_COUNT(type->typ_ops->o_owner);
79         return type;
80 }
81
82 void class_put_type(struct obd_type *type)
83 {
84         LASSERT(type);
85         __MOD_DEC_USE_COUNT(type->typ_ops->o_owner);
86 }
87
88 int class_register_type(struct obd_ops *ops, struct lprocfs_vars *vars,
89                         char *name)
90 {
91         struct obd_type *type;
92         int rc = 0;
93         ENTRY;
94
95         LASSERT(strnlen(name, 1024) < 1024);    /* sanity check */
96
97         if (class_search_type(name)) {
98                 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
99                 RETURN(-EEXIST);
100         }
101
102         rc = -ENOMEM;
103         OBD_ALLOC(type, sizeof(*type));
104         if (type == NULL)
105                 RETURN(rc);
106
107         OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
108         OBD_ALLOC(type->typ_name, strlen(name) + 1);
109         if (type->typ_ops == NULL || type->typ_name == NULL)
110                 GOTO (failed, rc);
111
112         *(type->typ_ops) = *ops;
113         strcpy(type->typ_name, name);
114         list_add(&type->typ_chain, &obd_types);
115
116         type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
117                                               vars, type);
118         if (IS_ERR(type->typ_procroot)) {
119                 rc = PTR_ERR(type->typ_procroot);
120                 type->typ_procroot = NULL;
121                 list_del(&type->typ_chain);
122                 GOTO (failed, rc);
123         }
124
125         RETURN (0);
126
127  failed:
128         if (type->typ_ops != NULL)
129                 OBD_FREE(type->typ_name, strlen(name) + 1);
130         if (type->typ_ops != NULL)
131                 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
132         RETURN(rc);
133 }
134
135 int class_unregister_type(char *name)
136 {
137         struct obd_type *type = class_search_type(name);
138         ENTRY;
139
140         if (!type) {
141                 CERROR("unknown obd type\n");
142                 RETURN(-EINVAL);
143         }
144
145         if (type->typ_refcnt) {
146                 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
147                 /* This is a bad situation, let's make the best of it */
148                 /* Remove ops, but leave the name for debugging */
149                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
150                 RETURN(-EBUSY);
151         }
152
153         if (type->typ_procroot) {
154                 lprocfs_remove(type->typ_procroot);
155                 type->typ_procroot = NULL;
156         }
157
158         list_del(&type->typ_chain);
159         OBD_FREE(type->typ_name, strlen(name) + 1);
160         if (type->typ_ops != NULL)
161                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
162         OBD_FREE(type, sizeof(*type));
163         RETURN(0);
164 } /* class_unregister_type */
165
166 int class_name2dev(char *name)
167 {
168         int res = -1;
169         int i;
170
171         if (!name)
172                 return -1;
173
174         for (i = 0; i < MAX_OBD_DEVICES; i++) {
175                 struct obd_device *obd = &obd_dev[i];
176                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
177                         res = i;
178                         return res;
179                 }
180         }
181
182         return res;
183 }
184
185 int class_uuid2dev(struct obd_uuid *uuid)
186 {
187         int res = -1;
188         int i;
189
190         for (i = 0; i < MAX_OBD_DEVICES; i++) {
191                 struct obd_device *obd = &obd_dev[i];
192                 if (strncmp(uuid->uuid, obd->obd_uuid.uuid, sizeof(obd->obd_uuid.uuid)) == 0) {
193                         res = i;
194                         return res;
195                 }
196         }
197
198         return res;
199 }
200
201
202 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
203 {
204         int i;
205
206         for (i = 0; i < MAX_OBD_DEVICES; i++) {
207                 struct obd_device *obd = &obd_dev[i];
208                 if (strncmp(uuid->uuid, obd->obd_uuid.uuid, sizeof(obd->obd_uuid.uuid)) == 0)
209                         return obd;
210         }
211
212         return NULL;
213 }
214
215 void obd_cleanup_caches(void)
216 {
217         int rc;
218         ENTRY;
219         if (obdo_cachep) {
220                 rc = kmem_cache_destroy(obdo_cachep);
221                 if (rc)
222                         CERROR("Cannot destory ll_obdo_cache\n");
223                 obdo_cachep = NULL;
224         }
225         if (import_cachep) {
226                 rc = kmem_cache_destroy(import_cachep);
227                 if (rc)
228                         CERROR("Cannot destory ll_import_cache\n");
229                 import_cachep = NULL;
230         }
231         if (export_cachep) {
232                 rc = kmem_cache_destroy(export_cachep);
233                 if (rc)
234                         CERROR("Cannot destory ll_export_cache\n");
235                 export_cachep = NULL;
236         }
237         EXIT;
238 }
239
240 int obd_init_caches(void)
241 {
242         ENTRY;
243         LASSERT(obdo_cachep == NULL);
244         obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
245                                         0, 0, NULL, NULL);
246         if (!obdo_cachep)
247                 GOTO(out, -ENOMEM);
248
249         LASSERT(export_cachep == NULL);
250         export_cachep = kmem_cache_create("ll_export_cache",
251                                           sizeof(struct obd_export),
252                                           0, 0, NULL, NULL);
253         if (!export_cachep)
254                 GOTO(out, -ENOMEM);
255
256         LASSERT(import_cachep == NULL);
257         import_cachep = kmem_cache_create("ll_import_cache",
258                                           sizeof(struct obd_import),
259                                           0, 0, NULL, NULL);
260         if (!import_cachep)
261                 GOTO(out, -ENOMEM);
262
263         RETURN(0);
264  out:
265         obd_cleanup_caches();
266         RETURN(-ENOMEM);
267
268 }
269
270 /* map connection to client */
271 struct obd_export *class_conn2export(struct lustre_handle *conn)
272 {
273         struct obd_export *export;
274         ENTRY;
275
276         if (!conn) {
277                 CDEBUG(D_CACHE, "looking for null handle\n");
278                 RETURN(NULL);
279         }
280
281         if (conn->addr == -1) {  /* this means assign a new connection */
282                 CDEBUG(D_CACHE, "want a new connection\n");
283                 RETURN(NULL);
284         }
285
286         if (!conn->addr) {
287                 CDEBUG(D_CACHE, "looking for null addr\n");
288                 fixme();
289                 RETURN(NULL);
290         }
291
292         CDEBUG(D_IOCTL, "looking for export addr "LPX64" cookie "LPX64"\n",
293                conn->addr, conn->cookie);
294         export = (struct obd_export *) (unsigned long)conn->addr;
295         if (!kmem_cache_validate(export_cachep, (void *)export))
296                 RETURN(NULL);
297
298         if (export->exp_cookie != conn->cookie)
299                 RETURN(NULL);
300         RETURN(export);
301 } /* class_conn2export */
302
303 struct obd_device *class_conn2obd(struct lustre_handle *conn)
304 {
305         struct obd_export *export;
306         export = class_conn2export(conn);
307         if (export)
308                 return export->exp_obd;
309         fixme();
310         return NULL;
311 }
312
313 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
314 {
315         return &class_conn2obd(conn)->u.cli.cl_import;
316 }
317
318 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
319 {
320         return &class_conn2export(conn)->exp_ldlm_data.led_import;
321 }
322
323 struct obd_export *class_new_export(struct obd_device *obddev)
324 {
325         struct obd_export * export;
326
327         export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
328         if (!export) {
329                 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
330                 return NULL;
331         }
332
333         memset(export, 0, sizeof(*export));
334         get_random_bytes(&export->exp_cookie, sizeof(export->exp_cookie));
335         export->exp_obd = obddev;
336         /* XXX this should be in LDLM init */
337         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
338         INIT_LIST_HEAD(&export->exp_conn_chain);
339         spin_lock(&obddev->obd_dev_lock);
340         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
341         spin_unlock(&obddev->obd_dev_lock);
342         return export;
343 }
344
345 void class_destroy_export(struct obd_export *exp)
346 {
347         ENTRY;
348
349         LASSERT(exp->exp_cookie != DEAD_HANDLE_MAGIC);
350
351         spin_lock(&exp->exp_obd->obd_dev_lock);
352         list_del(&exp->exp_obd_chain);
353         spin_unlock(&exp->exp_obd->obd_dev_lock);
354
355         /* XXXshaver no connection here... */
356         if (exp->exp_connection)
357                 spin_lock(&exp->exp_connection->c_lock);
358         list_del(&exp->exp_conn_chain);
359         if (exp->exp_connection) {
360                 spin_unlock(&exp->exp_connection->c_lock);
361                 ptlrpc_put_connection_superhack(exp->exp_connection);
362         }
363
364         /* Abort any inflight DLM requests and NULL out their (about to be
365          * freed) import. */
366         if (exp->exp_ldlm_data.led_import.imp_obd)
367                 ptlrpc_abort_inflight_superhack(&exp->exp_ldlm_data.led_import,
368                                                 1);
369
370         exp->exp_cookie = DEAD_HANDLE_MAGIC;
371         kmem_cache_free(export_cachep, exp);
372
373         EXIT;
374 }
375
376 /* a connection defines an export context in which preallocation can
377    be managed. */
378 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
379                   struct obd_uuid *cluuid)
380 {
381         struct obd_export * export;
382         if (conn == NULL) {
383                 LBUG();
384                 return -EINVAL;
385         }
386
387         if (obd == NULL) {
388                 LBUG();
389                 return -EINVAL;
390         }
391
392         if (cluuid == NULL) {
393                 LBUG();
394                 return -EINVAL;
395         }
396
397         export = class_new_export(obd);
398         if (!export)
399                 return -ENOMEM;
400
401         conn->addr = (__u64) (unsigned long)export;
402         conn->cookie = export->exp_cookie;
403         memcpy(&export->exp_client_uuid, cluuid, sizeof(export->exp_client_uuid));
404
405         CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
406                (long long)conn->addr, (long long)conn->cookie);
407         return 0;
408 }
409
410 int class_disconnect(struct lustre_handle *conn)
411 {
412         struct obd_export *export;
413         ENTRY;
414
415         if (!(export = class_conn2export(conn))) {
416                 fixme();
417                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
418                        "nonexistent client "LPX64"\n", conn->addr);
419                 RETURN(-EINVAL);
420         }
421
422         CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
423                        (long long)conn->addr, (long long)conn->cookie);
424
425         class_destroy_export(export);
426
427         RETURN(0);
428 }
429
430 void class_disconnect_all(struct obd_device *obddev)
431 {
432         int again = 1;
433
434         while (again) {
435                 spin_lock(&obddev->obd_dev_lock);
436                 if (!list_empty(&obddev->obd_exports)) {
437                         struct obd_export *export;
438                         struct lustre_handle conn;
439                         int rc;
440
441                         export = list_entry(obddev->obd_exports.next,
442                                             struct obd_export,
443                                             exp_obd_chain);
444                         conn.addr = (__u64)(unsigned long)export;
445                         conn.cookie = export->exp_cookie;
446                         spin_unlock(&obddev->obd_dev_lock);
447                         CERROR("force disconnecting %s:%s export %p\n",
448                                export->exp_obd->obd_type->typ_name,
449                                export->exp_connection ?
450                                (char *)export->exp_connection->c_remote_uuid.uuid :
451                                "<unconnected>", export);
452                         rc = obd_disconnect(&conn);
453                         if (rc < 0) {
454                                 /* AED: not so sure about this...  We can't
455                                  * loop here forever, yet we shouldn't leak
456                                  * exports on a struct we will soon destroy.
457                                  */
458                                 CERROR("destroy export %p with err: rc = %d\n",
459                                        export, rc);
460                                 class_destroy_export(export);
461                         }
462                 } else {
463                         spin_unlock(&obddev->obd_dev_lock);
464                         again = 0;
465                 }
466         }
467 }
468
469 #if 0
470
471 /* FIXME: Data is a space- or comma-separated list of device IDs.  This will
472  * have to change. */
473 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
474 {
475         int count, rc;
476         char *p;
477         ENTRY;
478
479         for (p = data, count = 0; p < (char *)data + len; count++) {
480                 char *end;
481                 int tmp = simple_strtoul(p, &end, 0);
482
483                 if (p == end) {
484                         CERROR("invalid device ID starting at: %s\n", p);
485                         GOTO(err_disconnect, rc = -EINVAL);
486                 }
487
488                 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
489                         CERROR("Trying to sub dev %d  - dev no too large\n",
490                                tmp);
491                         GOTO(err_disconnect, rc  = -EINVAL);
492                 }
493
494                 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
495                 if (rc) {
496                         CERROR("cannot connect to device %d: rc = %d\n", tmp,
497                                rc);
498                         GOTO(err_disconnect, rc);
499                 }
500
501                 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
502                        obd_dev[tmp].obd_type->typ_name);
503
504                 p = end + 1;
505         }
506
507         obddev->obd_multi_count = count;
508
509         RETURN(0);
510
511  err_disconnect:
512         for (count--; count >= 0; count--)
513                 obd_disconnect(&obddev->obd_multi_conn[count]);
514         return rc;
515 }
516
517 /*
518  *    remove all connections to this device
519  *    close all connections to lower devices
520  *    needed for forced unloads of OBD client drivers
521  */
522 int class_multi_cleanup(struct obd_device *obddev)
523 {
524         int i;
525
526         for (i = 0; i < obddev->obd_multi_count; i++) {
527                 int rc;
528                 struct obd_device *obd =
529                         class_conn2obd(&obddev->obd_multi_conn[i]);
530
531                 if (!obd) {
532                         CERROR("no such device [i %d]\n", i);
533                         RETURN(-EINVAL);
534                 }
535
536                 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
537                 if (rc)
538                         CERROR("disconnect failure %d\n", obd->obd_minor);
539         }
540         return 0;
541 }
542 #endif