Whamcloud - gitweb
file mgmt_svc.c was initially added on branch b_devel.
[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 #ifdef __KERNEL__
27 #include <linux/kmod.h>   /* for request_module() */
28 #include <linux/module.h>
29 #include <linux/obd_class.h>
30 #include <linux/random.h>
31 #include <linux/slab.h>
32 #else 
33 #include <liblustre.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd.h>
36 #endif
37 #include <linux/lprocfs_status.h>
38
39 extern struct list_head obd_types;
40 static spinlock_t obd_types_lock = SPIN_LOCK_UNLOCKED;
41 kmem_cache_t *obdo_cachep = NULL;
42 kmem_cache_t *import_cachep = NULL;
43
44 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
45 void (*ptlrpc_abort_inflight_superhack)(struct obd_import *imp);
46
47 struct obd_uuid lctl_fake_uuid = { .uuid = "OBD_CLASS_UUID" };
48
49 /*
50  * support functions: we could use inter-module communication, but this
51  * is more portable to other OS's
52  */
53 static struct obd_type *class_search_type(char *name)
54 {
55         struct list_head *tmp;
56         struct obd_type *type;
57
58         spin_lock(&obd_types_lock);
59         list_for_each(tmp, &obd_types) {
60                 type = list_entry(tmp, struct obd_type, typ_chain);
61                 if (strlen(type->typ_name) == strlen(name) &&
62                     strcmp(type->typ_name, name) == 0) {
63                         spin_unlock(&obd_types_lock);
64                         return type;
65                 }
66         }
67         spin_unlock(&obd_types_lock);
68         return NULL;
69 }
70
71 struct obd_type *class_get_type(char *name)
72 {
73         struct obd_type *type = class_search_type(name);
74
75 #ifdef CONFIG_KMOD
76         if (!type) {
77                 if (!request_module(name)) {
78                         CDEBUG(D_INFO, "Loaded module '%s'\n", name);
79                         type = class_search_type(name);
80                 } else
81                         CDEBUG(D_INFO, "Can't load module '%s'\n", name);
82         }
83 #endif
84         if (type)
85                 __MOD_INC_USE_COUNT(type->typ_ops->o_owner);
86         return type;
87 }
88
89 void class_put_type(struct obd_type *type)
90 {
91         LASSERT(type);
92         __MOD_DEC_USE_COUNT(type->typ_ops->o_owner);
93 }
94
95 int class_register_type(struct obd_ops *ops, struct lprocfs_vars *vars,
96                         char *name)
97 {
98         struct obd_type *type;
99         int rc = 0;
100         ENTRY;
101
102         LASSERT(strnlen(name, 1024) < 1024);    /* sanity check */
103
104         if (class_search_type(name)) {
105                 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
106                 RETURN(-EEXIST);
107         }
108
109         rc = -ENOMEM;
110         OBD_ALLOC(type, sizeof(*type));
111         if (type == NULL)
112                 RETURN(rc);
113
114         OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
115         OBD_ALLOC(type->typ_name, strlen(name) + 1);
116         if (type->typ_ops == NULL || type->typ_name == NULL)
117                 GOTO (failed, rc);
118
119         *(type->typ_ops) = *ops;
120         strcpy(type->typ_name, name);
121
122         type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
123                                               vars, type);
124         if (IS_ERR(type->typ_procroot)) {
125                 rc = PTR_ERR(type->typ_procroot);
126                 type->typ_procroot = NULL;
127                 GOTO (failed, rc);
128         }
129
130         spin_lock(&obd_types_lock);
131         list_add(&type->typ_chain, &obd_types);
132         spin_unlock(&obd_types_lock);
133
134         RETURN (0);
135
136  failed:
137         if (type->typ_ops != NULL)
138                 OBD_FREE(type->typ_name, strlen(name) + 1);
139         if (type->typ_ops != NULL)
140                 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
141         RETURN(rc);
142 }
143
144 int class_unregister_type(char *name)
145 {
146         struct obd_type *type = class_search_type(name);
147         ENTRY;
148
149         if (!type) {
150                 CERROR("unknown obd type\n");
151                 RETURN(-EINVAL);
152         }
153
154         if (type->typ_refcnt) {
155                 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
156                 /* This is a bad situation, let's make the best of it */
157                 /* Remove ops, but leave the name for debugging */
158                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
159                 RETURN(-EBUSY);
160         }
161
162         if (type->typ_procroot) {
163                 lprocfs_remove(type->typ_procroot);
164                 type->typ_procroot = NULL;
165         }
166
167         spin_lock(&obd_types_lock);
168         list_del(&type->typ_chain);
169         spin_unlock(&obd_types_lock);
170         OBD_FREE(type->typ_name, strlen(name) + 1);
171         if (type->typ_ops != NULL)
172                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
173         OBD_FREE(type, sizeof(*type));
174         RETURN(0);
175 } /* class_unregister_type */
176
177 int class_name2dev(char *name)
178 {
179         int i;
180
181         if (!name)
182                 return -1;
183
184         for (i = 0; i < MAX_OBD_DEVICES; i++) {
185                 struct obd_device *obd = &obd_dev[i];
186                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0)
187                         return i;
188         }
189
190         return -1;
191 }
192
193 int class_uuid2dev(struct obd_uuid *uuid)
194 {
195         int i;
196
197         for (i = 0; i < MAX_OBD_DEVICES; i++) {
198                 struct obd_device *obd = &obd_dev[i];
199                 if (obd_uuid_equals(uuid, &obd->obd_uuid))
200                         return i;
201         }
202
203         return -1;
204 }
205
206 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
207 {
208         int i;
209
210         for (i = 0; i < MAX_OBD_DEVICES; i++) {
211                 struct obd_device *obd = &obd_dev[i];
212                 if (obd_uuid_equals(uuid, &obd->obd_uuid))
213                         return obd;
214         }
215
216         return NULL;
217 }
218
219 void obd_cleanup_caches(void)
220 {
221         int rc;
222         ENTRY;
223         if (obdo_cachep) {
224                 rc = kmem_cache_destroy(obdo_cachep);
225                 if (rc)
226                         CERROR("Cannot destory ll_obdo_cache\n");
227                 obdo_cachep = NULL;
228         }
229         if (import_cachep) {
230                 rc = kmem_cache_destroy(import_cachep);
231                 if (rc)
232                         CERROR("Cannot destory ll_import_cache\n");
233                 import_cachep = NULL;
234         }
235         EXIT;
236 }
237
238 int obd_init_caches(void)
239 {
240         ENTRY;
241         LASSERT(obdo_cachep == NULL);
242         obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
243                                         0, 0, NULL, NULL);
244         if (!obdo_cachep)
245                 GOTO(out, -ENOMEM);
246
247         LASSERT(import_cachep == NULL);
248         import_cachep = kmem_cache_create("ll_import_cache",
249                                           sizeof(struct obd_import),
250                                           0, 0, NULL, NULL);
251         if (!import_cachep)
252                 GOTO(out, -ENOMEM);
253
254         RETURN(0);
255  out:
256         obd_cleanup_caches();
257         RETURN(-ENOMEM);
258
259 }
260
261 /* map connection to client */
262 struct obd_export *class_conn2export(struct lustre_handle *conn)
263 {
264         struct obd_export *export;
265         ENTRY;
266
267         if (!conn) {
268                 CDEBUG(D_CACHE, "looking for null handle\n");
269                 RETURN(NULL);
270         }
271
272         if (conn->cookie == -1) {  /* this means assign a new connection */
273                 CDEBUG(D_CACHE, "want a new connection\n");
274                 RETURN(NULL);
275         }
276
277         CDEBUG(D_IOCTL, "looking for export cookie "LPX64"\n", conn->cookie);
278         export = class_handle2object(conn->cookie);
279         RETURN(export);
280 }
281
282 struct obd_device *class_conn2obd(struct lustre_handle *conn)
283 {
284         struct obd_export *export;
285         export = class_conn2export(conn);
286         if (export) {
287                 struct obd_device *obd = export->exp_obd;
288                 class_export_put(export);
289                 return obd;
290         }
291         return NULL;
292 }
293
294 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
295 {
296         return class_conn2obd(conn)->u.cli.cl_import;
297 }
298
299 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
300 {
301         struct obd_export *export;
302         export = class_conn2export(conn);
303         if (export) {
304                 struct obd_import *imp = export->exp_ldlm_data.led_import;
305                 class_export_put(export);
306                 return imp;
307         }
308         fixme();
309         return NULL;
310 }
311
312 /* Export management functions */
313 static void export_handle_addref(void *export)
314 {
315         class_export_get(export);
316 }
317
318 struct obd_export *class_export_get(struct obd_export *exp)
319 {
320         atomic_inc(&exp->exp_refcount);
321         CDEBUG(D_INFO, "GETting export %p : new refcount %d\n", exp,
322                atomic_read(&exp->exp_refcount));
323         return exp;
324 }
325
326 void class_export_put(struct obd_export *exp)
327 {
328         ENTRY;
329
330         CDEBUG(D_INFO, "PUTting export %p : new refcount %d\n", exp,
331                atomic_read(&exp->exp_refcount) - 1);
332         LASSERT(atomic_read(&exp->exp_refcount) > 0);
333         LASSERT(atomic_read(&exp->exp_refcount) < 0x5a5a5a);
334         if (atomic_dec_and_test(&exp->exp_refcount)) {
335                 struct obd_device *obd = exp->exp_obd;
336                 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
337                        exp->exp_client_uuid.uuid);
338
339                 LASSERT(obd != NULL);
340
341                 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
342                 if (exp->exp_connection)
343                         ptlrpc_put_connection_superhack(exp->exp_connection);
344
345                 LASSERT(list_empty(&exp->exp_handle.h_link));
346
347                 obd_destroy_export(exp);
348
349                 OBD_FREE(exp, sizeof(*exp));
350                 atomic_dec(&obd->obd_refcount);
351                 wake_up(&obd->obd_refcount_waitq);
352         }
353         EXIT;
354 }
355
356 struct obd_export *class_new_export(struct obd_device *obddev)
357 {
358         struct obd_export *export;
359
360         OBD_ALLOC(export, sizeof(*export));
361         if (!export) {
362                 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
363                 return NULL;
364         }
365
366         atomic_set(&export->exp_refcount, 2);
367         export->exp_obd = obddev;
368         /* XXX this should be in LDLM init */
369         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
370
371         INIT_LIST_HEAD(&export->exp_handle.h_link);
372         class_handle_hash(&export->exp_handle, export_handle_addref);
373         spin_lock_init(&export->exp_lock);
374
375         spin_lock(&obddev->obd_dev_lock);
376         LASSERT(!obddev->obd_stopping); /* shouldn't happen, but might race */
377         atomic_inc(&obddev->obd_refcount);
378         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
379         spin_unlock(&obddev->obd_dev_lock);
380         return export;
381 }
382
383 void class_unlink_export(struct obd_export *exp)
384 {
385         class_handle_unhash(&exp->exp_handle);
386
387         spin_lock(&exp->exp_obd->obd_dev_lock);
388         list_del_init(&exp->exp_obd_chain);
389         spin_unlock(&exp->exp_obd->obd_dev_lock);
390
391         class_export_put(exp);
392 }
393
394 /* Import management functions */
395 static void import_handle_addref(void *import)
396 {
397         class_import_get(import);
398 }
399
400 struct obd_import *class_import_get(struct obd_import *import)
401 {
402         atomic_inc(&import->imp_refcount);
403         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
404                atomic_read(&import->imp_refcount));
405         return import;
406 }
407
408 void class_import_put(struct obd_import *import)
409 {
410         ENTRY;
411
412         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
413                atomic_read(&import->imp_refcount) - 1);
414
415         LASSERT(atomic_read(&import->imp_refcount) > 0);
416         LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
417         if (!atomic_dec_and_test(&import->imp_refcount)) {
418                 EXIT;
419                 return;
420         }
421
422         CDEBUG(D_IOCTL, "destroying import %p\n", import);
423
424         ptlrpc_put_connection_superhack(import->imp_connection);
425
426         LASSERT(list_empty(&import->imp_handle.h_link));
427         OBD_FREE(import, sizeof(*import));
428         EXIT;
429 }
430
431 struct obd_import *class_new_import(void)
432 {
433         struct obd_import *imp;
434
435         OBD_ALLOC(imp, sizeof(*imp));
436         if (imp == NULL)
437                 return NULL;
438
439         INIT_LIST_HEAD(&imp->imp_replay_list);
440         INIT_LIST_HEAD(&imp->imp_sending_list);
441         INIT_LIST_HEAD(&imp->imp_delayed_list);
442         spin_lock_init(&imp->imp_lock);
443         imp->imp_max_transno = 0;
444         imp->imp_peer_committed_transno = 0;
445
446         atomic_set(&imp->imp_refcount, 2);
447         INIT_LIST_HEAD(&imp->imp_handle.h_link);
448         class_handle_hash(&imp->imp_handle, import_handle_addref);
449
450         return imp;
451 }
452
453 void class_destroy_import(struct obd_import *import)
454 {
455         LASSERT(import != NULL);
456
457         class_handle_unhash(&import->imp_handle);
458
459         /* Abort any inflight DLM requests and NULL out their (about to be
460          * freed) import. */
461         ptlrpc_abort_inflight_superhack(import);
462
463         class_import_put(import);
464 }
465
466 /* a connection defines an export context in which preallocation can
467    be managed. */
468 int class_connect(struct lustre_handle *exporth, struct obd_device *obd,
469                   struct obd_uuid *cluuid)
470 {
471         struct obd_export *export;
472         LASSERT(exporth != NULL);
473         LASSERT(obd != NULL);
474         LASSERT(cluuid != NULL);
475
476         export = class_new_export(obd);
477         if (export == NULL)
478                 return -ENOMEM;
479
480         exporth->cookie = export->exp_handle.h_cookie;
481         memcpy(&export->exp_client_uuid, cluuid,
482                sizeof(export->exp_client_uuid));
483         class_export_put(export);
484
485         CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
486                cluuid->uuid, exporth->cookie);
487         return 0;
488 }
489
490 int class_disconnect(struct lustre_handle *conn, int failover)
491 {
492         struct obd_export *export = class_conn2export(conn);
493         ENTRY;
494
495         if (export == NULL) {
496                 fixme();
497                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
498                        "nonexistent client "LPX64"\n", conn->cookie);
499                 RETURN(-EINVAL);
500         }
501
502         CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n", conn->cookie);
503
504         class_unlink_export(export);
505         class_export_put(export);
506         RETURN(0);
507 }
508
509 void class_disconnect_exports(struct obd_device *obd, int failover)
510 {
511         int rc;
512         struct list_head *tmp, *n, work_list;
513         struct lustre_handle fake_conn;
514         ENTRY;
515
516         /* Move all of the exports from obd_exports to a work list, en masse. */
517         spin_lock(&obd->obd_dev_lock);
518         list_add(&work_list, &obd->obd_exports);
519         list_del_init(&obd->obd_exports);
520         spin_unlock(&obd->obd_dev_lock);
521
522         CDEBUG(D_IOCTL, "OBD device %d (%p) has exports, "
523                "disconnecting them\n", obd->obd_minor, obd);
524         list_for_each_safe(tmp, n, &work_list) {
525                 struct obd_export *exp = list_entry(tmp, struct obd_export,
526                                                     exp_obd_chain);
527
528                 class_export_get(exp);
529                 fake_conn.cookie = exp->exp_handle.h_cookie;
530                 rc = obd_disconnect(&fake_conn, failover);
531                 /* exports created from last_rcvd data, and "fake"
532                    exports created by lctl don't have an import */
533                 if (exp->exp_ldlm_data.led_import != NULL)
534                         class_destroy_import(exp->exp_ldlm_data.led_import);
535                 class_export_put(exp);
536
537                 if (rc) {
538                         CDEBUG(D_IOCTL, "disconnecting export %p failed: %d\n",
539                                exp, rc);
540                 } else {
541                         CDEBUG(D_IOCTL, "export %p disconnected\n", exp);
542                 }
543         }
544         EXIT;
545 }