Whamcloud - gitweb
b=2225 fix obd_self_export issues
[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                 try_module_get(type->typ_ops->o_owner);
86         return type;
87 }
88
89 void class_put_type(struct obd_type *type)
90 {
91         LASSERT(type);
92         module_put(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         struct obd_device *obd = class_conn2obd(conn);
300         if (obd == NULL)
301                 return NULL;
302         return obd->u.cli.cl_import;
303 }
304
305 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
306 {
307         struct obd_export *export;
308         export = class_conn2export(conn);
309         if (export) {
310                 struct obd_import *imp = export->exp_ldlm_data.led_import;
311                 class_export_put(export);
312                 return imp;
313         }
314         fixme();
315         return NULL;
316 }
317
318 /* Export management functions */
319 static void export_handle_addref(void *export)
320 {
321         class_export_get(export);
322 }
323
324 struct obd_export *class_export_get(struct obd_export *exp)
325 {
326         atomic_inc(&exp->exp_refcount);
327         CDEBUG(D_INFO, "GETting export %p : new refcount %d\n", exp,
328                atomic_read(&exp->exp_refcount));
329         return exp;
330 }
331
332 void class_export_put(struct obd_export *exp)
333 {
334         ENTRY;
335         LASSERT(exp != NULL);
336
337         LASSERT(exp);
338         CDEBUG(D_INFO, "PUTting export %p : new refcount %d\n", exp,
339                atomic_read(&exp->exp_refcount) - 1);
340         LASSERT(atomic_read(&exp->exp_refcount) > 0);
341         LASSERT(atomic_read(&exp->exp_refcount) < 0x5a5a5a);
342         if (atomic_dec_and_test(&exp->exp_refcount)) {
343                 struct obd_device *obd = exp->exp_obd;
344                 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
345                        exp->exp_client_uuid.uuid);
346
347                 LASSERT(obd != NULL);
348
349                 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
350                 if (exp->exp_connection)
351                         ptlrpc_put_connection_superhack(exp->exp_connection);
352
353                 LASSERT(list_empty(&exp->exp_handle.h_link));
354
355                 obd_destroy_export(exp);
356
357                 OBD_FREE(exp, sizeof(*exp));
358                 atomic_dec(&obd->obd_refcount);
359                 wake_up(&obd->obd_refcount_waitq);
360         }
361         EXIT;
362 }
363
364 struct obd_export *class_new_export(struct obd_device *obd)
365 {
366         struct obd_export *export;
367
368         OBD_ALLOC(export, sizeof(*export));
369         if (!export) {
370                 CERROR("no memory! (minor %d)\n", obd->obd_minor);
371                 return NULL;
372         }
373
374         atomic_set(&export->exp_refcount, 2);
375         export->exp_obd = obd;
376         /* XXX this should be in LDLM init */
377         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
378
379         INIT_LIST_HEAD(&export->exp_handle.h_link);
380         class_handle_hash(&export->exp_handle, export_handle_addref);
381         spin_lock_init(&export->exp_lock);
382
383         spin_lock(&obd->obd_dev_lock);
384         LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
385         atomic_inc(&obd->obd_refcount);
386         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
387         export->exp_obd->obd_num_exports++;
388         spin_unlock(&obd->obd_dev_lock);
389         return export;
390 }
391
392 void class_unlink_export(struct obd_export *exp)
393 {
394         class_handle_unhash(&exp->exp_handle);
395
396         spin_lock(&exp->exp_obd->obd_dev_lock);
397         list_del_init(&exp->exp_obd_chain);
398         exp->exp_obd->obd_num_exports--;
399         spin_unlock(&exp->exp_obd->obd_dev_lock);
400
401         class_export_put(exp);
402 }
403
404 /* Import management functions */
405 static void import_handle_addref(void *import)
406 {
407         class_import_get(import);
408 }
409
410 struct obd_import *class_import_get(struct obd_import *import)
411 {
412         atomic_inc(&import->imp_refcount);
413         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
414                atomic_read(&import->imp_refcount));
415         return import;
416 }
417
418 void class_import_put(struct obd_import *import)
419 {
420         ENTRY;
421
422         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
423                atomic_read(&import->imp_refcount) - 1);
424
425         LASSERT(atomic_read(&import->imp_refcount) > 0);
426         LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
427         if (!atomic_dec_and_test(&import->imp_refcount)) {
428                 EXIT;
429                 return;
430         }
431
432         CDEBUG(D_IOCTL, "destroying import %p\n", import);
433
434         ptlrpc_put_connection_superhack(import->imp_connection);
435
436         LASSERT(list_empty(&import->imp_handle.h_link));
437         OBD_FREE(import, sizeof(*import));
438         EXIT;
439 }
440
441 struct obd_import *class_new_import(void)
442 {
443         struct obd_import *imp;
444
445         OBD_ALLOC(imp, sizeof(*imp));
446         if (imp == NULL)
447                 return NULL;
448
449         INIT_LIST_HEAD(&imp->imp_replay_list);
450         INIT_LIST_HEAD(&imp->imp_sending_list);
451         INIT_LIST_HEAD(&imp->imp_delayed_list);
452         spin_lock_init(&imp->imp_lock);
453         imp->imp_max_transno = 0;
454         imp->imp_peer_committed_transno = 0;
455
456         atomic_set(&imp->imp_refcount, 2);
457         INIT_LIST_HEAD(&imp->imp_handle.h_link);
458         class_handle_hash(&imp->imp_handle, import_handle_addref);
459
460         return imp;
461 }
462
463 void class_destroy_import(struct obd_import *import)
464 {
465         LASSERT(import != NULL);
466
467         class_handle_unhash(&import->imp_handle);
468
469         /* Abort any inflight DLM requests and NULL out their (about to be
470          * freed) import. */
471         /* Invalidate all requests on import, would be better to call
472            ptlrpc_set_import_active(imp, 0); */
473         import->imp_generation++;
474         ptlrpc_abort_inflight_superhack(import);
475
476         class_import_put(import);
477 }
478
479 /* a connection defines an export context in which preallocation can
480    be managed. */
481 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
482                   struct obd_uuid *cluuid)
483 {
484         struct obd_export *export;
485         LASSERT(conn != NULL);
486         LASSERT(obd != NULL);
487         LASSERT(cluuid != NULL);
488
489         export = class_new_export(obd);
490         if (export == NULL)
491                 return -ENOMEM;
492
493         conn->cookie = export->exp_handle.h_cookie;
494         memcpy(&export->exp_client_uuid, cluuid,
495                sizeof(export->exp_client_uuid));
496         class_export_put(export);
497
498         CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
499                cluuid->uuid, conn->cookie);
500         return 0;
501 }
502
503 int class_disconnect(struct lustre_handle *conn, int flags)
504 {
505         struct obd_export *export = class_conn2export(conn);
506         ENTRY;
507
508         if (export == NULL) {
509                 fixme();
510                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
511                        "nonexistent client "LPX64"\n", conn->cookie);
512                 RETURN(-EINVAL);
513         }
514
515         CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n", conn->cookie);
516
517         class_unlink_export(export);
518         class_export_put(export);
519         RETURN(0);
520 }
521
522 void class_disconnect_exports(struct obd_device *obd, int flags)
523 {
524         int rc;
525         struct list_head *tmp, *n, work_list;
526         struct lustre_handle fake_conn;
527         ENTRY;
528
529         /* Move all of the exports from obd_exports to a work list, en masse. */
530         spin_lock(&obd->obd_dev_lock);
531         list_add(&work_list, &obd->obd_exports);
532         list_del_init(&obd->obd_exports);
533         spin_unlock(&obd->obd_dev_lock);
534
535         CDEBUG(D_IOCTL, "OBD device %d (%p) has exports, "
536                "disconnecting them\n", obd->obd_minor, obd);
537         list_for_each_safe(tmp, n, &work_list) {
538                 struct obd_export *exp = list_entry(tmp, struct obd_export,
539                                                     exp_obd_chain);
540
541                 class_export_get(exp);
542                 fake_conn.cookie = exp->exp_handle.h_cookie;
543                 rc = obd_disconnect(&fake_conn, flags);
544                 /* exports created from last_rcvd data, and "fake"
545                    exports created by lctl don't have an import */
546                 if (exp->exp_ldlm_data.led_import != NULL)
547                         class_destroy_import(exp->exp_ldlm_data.led_import);
548                 class_export_put(exp);
549
550                 if (rc) {
551                         CDEBUG(D_IOCTL, "disconnecting export %p failed: %d\n",
552                                exp, rc);
553                 } else {
554                         CDEBUG(D_IOCTL, "export %p disconnected\n", exp);
555                 }
556         }
557         EXIT;
558 }