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