Whamcloud - gitweb
merge b_devel into HEAD (20030703)
[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 struct obd_device *class_name2obd(char *name)
194 {
195         int dev = class_name2dev(name);
196         if (dev < 0)
197                 return NULL;
198         return &obd_dev[dev];
199 }
200
201 int class_uuid2dev(struct obd_uuid *uuid)
202 {
203         int i;
204
205         for (i = 0; i < MAX_OBD_DEVICES; i++) {
206                 struct obd_device *obd = &obd_dev[i];
207                 if (obd_uuid_equals(uuid, &obd->obd_uuid))
208                         return i;
209         }
210
211         return -1;
212 }
213
214 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
215 {
216         int dev = class_uuid2dev(uuid);
217         if (dev < 0)
218                 return NULL;
219         return &obd_dev[dev];
220 }
221
222 void obd_cleanup_caches(void)
223 {
224         int rc;
225         ENTRY;
226         if (obdo_cachep) {
227                 rc = kmem_cache_destroy(obdo_cachep);
228                 if (rc)
229                         CERROR("Cannot destory ll_obdo_cache\n");
230                 obdo_cachep = NULL;
231         }
232         if (import_cachep) {
233                 rc = kmem_cache_destroy(import_cachep);
234                 if (rc)
235                         CERROR("Cannot destory ll_import_cache\n");
236                 import_cachep = NULL;
237         }
238         EXIT;
239 }
240
241 int obd_init_caches(void)
242 {
243         ENTRY;
244         LASSERT(obdo_cachep == NULL);
245         obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
246                                         0, 0, NULL, NULL);
247         if (!obdo_cachep)
248                 GOTO(out, -ENOMEM);
249
250         LASSERT(import_cachep == NULL);
251         import_cachep = kmem_cache_create("ll_import_cache",
252                                           sizeof(struct obd_import),
253                                           0, 0, NULL, NULL);
254         if (!import_cachep)
255                 GOTO(out, -ENOMEM);
256
257         RETURN(0);
258  out:
259         obd_cleanup_caches();
260         RETURN(-ENOMEM);
261
262 }
263
264 /* map connection to client */
265 struct obd_export *class_conn2export(struct lustre_handle *conn)
266 {
267         struct obd_export *export;
268         ENTRY;
269
270         if (!conn) {
271                 CDEBUG(D_CACHE, "looking for null handle\n");
272                 RETURN(NULL);
273         }
274
275         if (conn->cookie == -1) {  /* this means assign a new connection */
276                 CDEBUG(D_CACHE, "want a new connection\n");
277                 RETURN(NULL);
278         }
279
280         CDEBUG(D_IOCTL, "looking for export cookie "LPX64"\n", conn->cookie);
281         export = class_handle2object(conn->cookie);
282         RETURN(export);
283 }
284
285 struct obd_device *class_conn2obd(struct lustre_handle *conn)
286 {
287         struct obd_export *export;
288         export = class_conn2export(conn);
289         if (export) {
290                 struct obd_device *obd = export->exp_obd;
291                 class_export_put(export);
292                 return obd;
293         }
294         return NULL;
295 }
296
297 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
298 {
299         return class_conn2obd(conn)->u.cli.cl_import;
300 }
301
302 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
303 {
304         struct obd_export *export;
305         export = class_conn2export(conn);
306         if (export) {
307                 struct obd_import *imp = export->exp_ldlm_data.led_import;
308                 class_export_put(export);
309                 return imp;
310         }
311         fixme();
312         return NULL;
313 }
314
315 /* Export management functions */
316 static void export_handle_addref(void *export)
317 {
318         class_export_get(export);
319 }
320
321 struct obd_export *class_export_get(struct obd_export *exp)
322 {
323         atomic_inc(&exp->exp_refcount);
324         CDEBUG(D_INFO, "GETting export %p : new refcount %d\n", exp,
325                atomic_read(&exp->exp_refcount));
326         return exp;
327 }
328
329 void class_export_put(struct obd_export *exp)
330 {
331         ENTRY;
332
333         LASSERT(exp);
334         CDEBUG(D_INFO, "PUTting export %p : new refcount %d\n", exp,
335                atomic_read(&exp->exp_refcount) - 1);
336         LASSERT(atomic_read(&exp->exp_refcount) > 0);
337         LASSERT(atomic_read(&exp->exp_refcount) < 0x5a5a5a);
338         if (atomic_dec_and_test(&exp->exp_refcount)) {
339                 struct obd_device *obd = exp->exp_obd;
340                 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
341                        exp->exp_client_uuid.uuid);
342
343                 LASSERT(obd != NULL);
344
345                 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
346                 if (exp->exp_connection)
347                         ptlrpc_put_connection_superhack(exp->exp_connection);
348
349                 LASSERT(list_empty(&exp->exp_handle.h_link));
350
351                 obd_destroy_export(exp);
352
353                 OBD_FREE(exp, sizeof(*exp));
354                 atomic_dec(&obd->obd_refcount);
355                 wake_up(&obd->obd_refcount_waitq);
356         }
357         EXIT;
358 }
359
360 struct obd_export *class_new_export(struct obd_device *obddev)
361 {
362         struct obd_export *export;
363
364         OBD_ALLOC(export, sizeof(*export));
365         if (!export) {
366                 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
367                 return NULL;
368         }
369
370         atomic_set(&export->exp_refcount, 2);
371         export->exp_obd = obddev;
372         /* XXX this should be in LDLM init */
373         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
374
375         INIT_LIST_HEAD(&export->exp_handle.h_link);
376         class_handle_hash(&export->exp_handle, export_handle_addref);
377         spin_lock_init(&export->exp_lock);
378
379         spin_lock(&obddev->obd_dev_lock);
380         LASSERT(!obddev->obd_stopping); /* shouldn't happen, but might race */
381         atomic_inc(&obddev->obd_refcount);
382         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
383         export->exp_obd->obd_num_exports++;
384         spin_unlock(&obddev->obd_dev_lock);
385         return export;
386 }
387
388 void class_unlink_export(struct obd_export *exp)
389 {
390         class_handle_unhash(&exp->exp_handle);
391
392         spin_lock(&exp->exp_obd->obd_dev_lock);
393         list_del_init(&exp->exp_obd_chain);
394         exp->exp_obd->obd_num_exports--;
395         spin_unlock(&exp->exp_obd->obd_dev_lock);
396
397         class_export_put(exp);
398 }
399
400 /* Import management functions */
401 static void import_handle_addref(void *import)
402 {
403         class_import_get(import);
404 }
405
406 struct obd_import *class_import_get(struct obd_import *import)
407 {
408         atomic_inc(&import->imp_refcount);
409         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
410                atomic_read(&import->imp_refcount));
411         return import;
412 }
413
414 void class_import_put(struct obd_import *import)
415 {
416         ENTRY;
417
418         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
419                atomic_read(&import->imp_refcount) - 1);
420
421         LASSERT(atomic_read(&import->imp_refcount) > 0);
422         LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
423         if (!atomic_dec_and_test(&import->imp_refcount)) {
424                 EXIT;
425                 return;
426         }
427
428         CDEBUG(D_IOCTL, "destroying import %p\n", import);
429
430         ptlrpc_put_connection_superhack(import->imp_connection);
431
432         LASSERT(list_empty(&import->imp_handle.h_link));
433         OBD_FREE(import, sizeof(*import));
434         EXIT;
435 }
436
437 struct obd_import *class_new_import(void)
438 {
439         struct obd_import *imp;
440
441         OBD_ALLOC(imp, sizeof(*imp));
442         if (imp == NULL)
443                 return NULL;
444
445         INIT_LIST_HEAD(&imp->imp_replay_list);
446         INIT_LIST_HEAD(&imp->imp_sending_list);
447         INIT_LIST_HEAD(&imp->imp_delayed_list);
448         spin_lock_init(&imp->imp_lock);
449         imp->imp_max_transno = 0;
450         imp->imp_peer_committed_transno = 0;
451
452         atomic_set(&imp->imp_refcount, 2);
453         INIT_LIST_HEAD(&imp->imp_handle.h_link);
454         class_handle_hash(&imp->imp_handle, import_handle_addref);
455
456         return imp;
457 }
458
459 void class_destroy_import(struct obd_import *import)
460 {
461         LASSERT(import != NULL);
462
463         class_handle_unhash(&import->imp_handle);
464
465         /* Abort any inflight DLM requests and NULL out their (about to be
466          * freed) import. */
467         /* Invalidate all requests on import, would be better to call
468            ptlrpc_set_import_active(imp, 0); */
469         import->imp_generation++;
470         ptlrpc_abort_inflight_superhack(import);
471
472         class_import_put(import);
473 }
474
475 /* a connection defines an export context in which preallocation can
476    be managed. */
477 int class_connect(struct lustre_handle *exporth, struct obd_device *obd,
478                   struct obd_uuid *cluuid)
479 {
480         struct obd_export *export;
481         LASSERT(exporth != NULL);
482         LASSERT(obd != NULL);
483         LASSERT(cluuid != NULL);
484
485         export = class_new_export(obd);
486         if (export == NULL)
487                 return -ENOMEM;
488
489         exporth->cookie = export->exp_handle.h_cookie;
490         memcpy(&export->exp_client_uuid, cluuid,
491                sizeof(export->exp_client_uuid));
492         class_export_put(export);
493
494         CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
495                cluuid->uuid, exporth->cookie);
496         return 0;
497 }
498
499 int class_disconnect(struct lustre_handle *conn, int failover)
500 {
501         struct obd_export *export = class_conn2export(conn);
502         ENTRY;
503
504         if (export == NULL) {
505                 fixme();
506                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
507                        "nonexistent client "LPX64"\n", conn->cookie);
508                 RETURN(-EINVAL);
509         }
510
511         CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n", conn->cookie);
512
513         class_unlink_export(export);
514         class_export_put(export);
515         RETURN(0);
516 }
517
518 void class_disconnect_exports(struct obd_device *obd, int failover)
519 {
520         int rc;
521         struct list_head *tmp, *n, work_list;
522         struct lustre_handle fake_conn;
523         ENTRY;
524
525         /* Move all of the exports from obd_exports to a work list, en masse. */
526         spin_lock(&obd->obd_dev_lock);
527         list_add(&work_list, &obd->obd_exports);
528         list_del_init(&obd->obd_exports);
529         spin_unlock(&obd->obd_dev_lock);
530
531         CDEBUG(D_IOCTL, "OBD device %d (%p) has exports, "
532                "disconnecting them\n", obd->obd_minor, obd);
533         list_for_each_safe(tmp, n, &work_list) {
534                 struct obd_export *exp = list_entry(tmp, struct obd_export,
535                                                     exp_obd_chain);
536
537                 class_export_get(exp);
538                 fake_conn.cookie = exp->exp_handle.h_cookie;
539                 rc = obd_disconnect(&fake_conn, failover);
540                 /* exports created from last_rcvd data, and "fake"
541                    exports created by lctl don't have an import */
542                 if (exp->exp_ldlm_data.led_import != NULL)
543                         class_destroy_import(exp->exp_ldlm_data.led_import);
544                 class_export_put(exp);
545
546                 if (rc) {
547                         CDEBUG(D_IOCTL, "disconnecting export %p failed: %d\n",
548                                exp, rc);
549                 } else {
550                         CDEBUG(D_IOCTL, "export %p disconnected\n", exp);
551                 }
552         }
553         EXIT;
554 }