Whamcloud - gitweb
- lots of fixes and cleanups in cobd and cmobd.
[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 #include <linux/pagemap.h>
33 #else
34 #include <liblustre.h>
35 #include <linux/obd_class.h>
36 #include <linux/obd.h>
37 #endif
38 #include <linux/lprocfs_status.h>
39
40 extern struct list_head obd_types;
41 static spinlock_t obd_types_lock = SPIN_LOCK_UNLOCKED;
42 kmem_cache_t *obdo_cachep = NULL;
43 kmem_cache_t *import_cachep = NULL;
44
45 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
46 void (*ptlrpc_abort_inflight_superhack)(struct obd_import *imp);
47
48 /*
49  * support functions: we could use inter-module communication, but this
50  * is more portable to other OS's
51  */
52 static struct obd_type *class_search_type(char *name)
53 {
54         struct list_head *tmp;
55         struct obd_type *type;
56
57         spin_lock(&obd_types_lock);
58         list_for_each(tmp, &obd_types) {
59                 type = list_entry(tmp, struct obd_type, typ_chain);
60                 if (strlen(type->typ_name) == strlen(name) &&
61                     strcmp(type->typ_name, name) == 0) {
62                         spin_unlock(&obd_types_lock);
63                         return type;
64                 }
65         }
66         spin_unlock(&obd_types_lock);
67         return NULL;
68 }
69
70 struct obd_type *class_get_type(char *name)
71 {
72         struct obd_type *type = class_search_type(name);
73
74 #ifdef CONFIG_KMOD
75         if (!type) {
76                 if (!request_module(name)) {
77                         CDEBUG(D_INFO, "Loaded module '%s'\n", name);
78                         type = class_search_type(name);
79                 } else
80                         CDEBUG(D_INFO, "Can't load module '%s'\n", name);
81         }
82 #endif
83         if (type)
84                 try_module_get(type->typ_ops->o_owner);
85         return type;
86 }
87
88 void class_put_type(struct obd_type *type)
89 {
90         LASSERT(type);
91         module_put(type->typ_ops->o_owner);
92 }
93
94 int class_register_type(struct obd_ops *ops, struct md_ops *md_ops,
95                         struct lprocfs_vars *vars, char *name)
96 {
97         struct obd_type *type;
98         int rc = 0;
99         ENTRY;
100
101         LASSERT(strnlen(name, 1024) < 1024);    /* sanity check */
102
103         if (class_search_type(name)) {
104                 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
105                 RETURN(-EEXIST);
106         }
107
108         rc = -ENOMEM;
109         OBD_ALLOC(type, sizeof(*type));
110         if (type == NULL)
111                 RETURN(rc);
112
113         OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
114         OBD_ALLOC(type->typ_name, strlen(name) + 1);
115         if (md_ops)
116                 OBD_ALLOC(type->typ_md_ops, sizeof(*type->typ_md_ops));
117         if (type->typ_ops == NULL || type->typ_name == NULL ||
118                         (md_ops && type->typ_md_ops == NULL))
119                 GOTO (failed, rc);
120
121         *(type->typ_ops) = *ops;
122         if (md_ops)
123                 *(type->typ_md_ops) = *md_ops;
124         else
125                 type->typ_md_ops = NULL;
126         strcpy(type->typ_name, name);
127
128 #ifdef LPROCFS
129         type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
130                                               vars, type);
131 #endif
132         if (IS_ERR(type->typ_procroot)) {
133                 rc = PTR_ERR(type->typ_procroot);
134                 type->typ_procroot = NULL;
135                 GOTO (failed, rc);
136         }
137
138         spin_lock(&obd_types_lock);
139         list_add(&type->typ_chain, &obd_types);
140         spin_unlock(&obd_types_lock);
141
142         RETURN (0);
143
144  failed:
145         if (type->typ_name != NULL)
146                 OBD_FREE(type->typ_name, strlen(name) + 1);
147         if (type->typ_ops != NULL)
148                 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
149         if (type->typ_md_ops != NULL)
150                 OBD_FREE (type->typ_md_ops, sizeof (*type->typ_md_ops));
151         OBD_FREE(type, sizeof(*type));
152         RETURN(rc);
153 }
154
155 int class_unregister_type(char *name)
156 {
157         struct obd_type *type = class_search_type(name);
158         ENTRY;
159
160         if (!type) {
161                 CERROR("unknown obd type\n");
162                 RETURN(-EINVAL);
163         }
164
165         if (type->typ_refcnt) {
166                 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
167                 /* This is a bad situation, let's make the best of it */
168                 /* Remove ops, but leave the name for debugging */
169                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
170                 RETURN(-EBUSY);
171         }
172
173         if (type->typ_procroot) {
174                 lprocfs_remove(type->typ_procroot);
175                 type->typ_procroot = NULL;
176         }
177
178         spin_lock(&obd_types_lock);
179         list_del(&type->typ_chain);
180         spin_unlock(&obd_types_lock);
181         OBD_FREE(type->typ_name, strlen(name) + 1);
182         if (type->typ_ops != NULL)
183                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
184         if (type->typ_md_ops != NULL)
185                 OBD_FREE (type->typ_md_ops, sizeof(*type->typ_md_ops));
186         OBD_FREE(type, sizeof(*type));
187         RETURN(0);
188 } /* class_unregister_type */
189
190 struct obd_device *class_newdev(struct obd_type *type)
191 {
192         struct obd_device *result = NULL;
193         int i;
194
195         spin_lock(&obd_dev_lock);
196         for (i = 0 ; i < MAX_OBD_DEVICES && result == NULL; i++) {
197                 struct obd_device *obd = &obd_dev[i];
198                 if (!obd->obd_type) {
199                         LASSERT(obd->obd_minor == i);
200                         memset(obd, 0, sizeof(*obd));
201                         obd->obd_minor = i;
202                         obd->obd_type = type;
203                         result = obd;
204                 }
205         }
206         spin_unlock(&obd_dev_lock);
207         return result;
208 }
209
210 void class_release_dev(struct obd_device *obd)
211 {
212         int minor = obd->obd_minor;
213
214         spin_lock(&obd_dev_lock);
215         memset(obd, 0, sizeof(*obd));
216         obd->obd_minor = minor;
217         spin_unlock(&obd_dev_lock);
218 }
219
220 int class_name2dev(char *name)
221 {
222         int i;
223
224         if (!name)
225                 return -1;
226
227         spin_lock(&obd_dev_lock);
228         for (i = 0; i < MAX_OBD_DEVICES; i++) {
229                 struct obd_device *obd = &obd_dev[i];
230                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
231                         spin_unlock(&obd_dev_lock);
232                         return i;
233                 }
234         }
235         spin_unlock(&obd_dev_lock);
236         return -1;
237 }
238
239 struct obd_device *class_name2obd(char *name)
240 {
241         int dev = class_name2dev(name);
242         if (dev < 0)
243                 return NULL;
244         return &obd_dev[dev];
245 }
246
247 int class_uuid2dev(struct obd_uuid *uuid)
248 {
249         int i;
250         spin_lock(&obd_dev_lock);
251         for (i = 0; i < MAX_OBD_DEVICES; i++) {
252                 struct obd_device *obd = &obd_dev[i];
253                 if (obd_uuid_equals(uuid, &obd->obd_uuid)) {
254                         spin_unlock(&obd_dev_lock);
255                         return i;
256                 }
257         }
258         spin_unlock(&obd_dev_lock);
259         return -1;
260 }
261
262 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
263 {
264         int dev = class_uuid2dev(uuid);
265         if (dev < 0)
266                 return NULL;
267         return &obd_dev[dev];
268 }
269
270 /* Search for a client OBD connected to tgt_uuid.  If grp_uuid is
271    specified, then only the client with that uuid is returned,
272    otherwise any client connected to the tgt is returned.
273    If tgt_uuid is NULL, the lov with grp_uuid is returned. */
274 struct obd_device *class_find_client_obd(struct obd_uuid *tgt_uuid,
275                                          char *typ_name,
276                                          struct obd_uuid *grp_uuid)
277 {
278         int i;
279
280         spin_lock(&obd_dev_lock);
281         for (i = 0; i < MAX_OBD_DEVICES; i++) {
282                 struct obd_device *obd = &obd_dev[i];
283                 if (obd->obd_type == NULL)
284                         continue;
285                 if ((strncmp(obd->obd_type->typ_name, typ_name,
286                              strlen(typ_name)) == 0)) {
287                         struct client_obd *cli = &obd->u.cli;
288                         struct obd_import *imp = cli->cl_import;
289                         if (tgt_uuid == NULL) {
290                                 LASSERT(grp_uuid);
291                                 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid))
292                                         return obd;
293                                 continue;
294                         }
295                         if (obd_uuid_equals(tgt_uuid, &imp->imp_target_uuid) &&
296                             ((grp_uuid)? obd_uuid_equals(grp_uuid,
297                                                          &obd->obd_uuid) : 1)) {
298                                 spin_unlock(&obd_dev_lock);
299                                 return obd;
300                         }
301                 }
302         }
303         spin_unlock(&obd_dev_lock);
304         return NULL;
305 }
306
307 /* Iterate the obd_device list looking devices have grp_uuid. Start
308    searching at *next, and if a device is found, the next index to look
309    it is saved in *next. If next is NULL, then the first matching device
310    will always be returned. */
311 struct obd_device *class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
312 {
313         int i;
314
315         if (next == NULL) 
316                 i = 0;
317         else if (*next >= 0 && *next < MAX_OBD_DEVICES)
318                 i = *next;
319         else 
320                 return NULL;
321         spin_lock(&obd_dev_lock);                
322         for (; i < MAX_OBD_DEVICES; i++) {
323                 struct obd_device *obd = &obd_dev[i];
324                 if (obd->obd_type == NULL)
325                         continue;
326                 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
327                         if (next != NULL)
328                                 *next = i+1;
329                         spin_unlock(&obd_dev_lock);
330                         return obd;
331                 }
332         }
333
334         spin_unlock(&obd_dev_lock);
335         return NULL;
336 }
337
338 void obd_cleanup_caches(void)
339 {
340         ENTRY;
341         if (obdo_cachep) {
342                 LASSERTF(kmem_cache_destroy(obdo_cachep) == 0,
343                          "Cannot destory ll_obdo_cache\n");
344                 obdo_cachep = NULL;
345         }
346         if (import_cachep) {
347                 LASSERTF(kmem_cache_destroy(import_cachep) == 0,
348                          "Cannot destory ll_import_cache\n");
349                 import_cachep = NULL;
350         }
351         EXIT;
352 }
353
354 int obd_init_caches(void)
355 {
356         ENTRY;
357         LASSERT(obdo_cachep == NULL);
358         obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
359                                         0, 0, NULL, NULL);
360         if (!obdo_cachep)
361                 GOTO(out, -ENOMEM);
362
363         LASSERT(import_cachep == NULL);
364         import_cachep = kmem_cache_create("ll_import_cache",
365                                           sizeof(struct obd_import),
366                                           0, 0, NULL, NULL);
367         if (!import_cachep)
368                 GOTO(out, -ENOMEM);
369
370         RETURN(0);
371  out:
372         obd_cleanup_caches();
373         RETURN(-ENOMEM);
374
375 }
376
377 /* map connection to client */
378 struct obd_export *class_conn2export(struct lustre_handle *conn)
379 {
380         struct obd_export *export;
381         ENTRY;
382
383         if (!conn) {
384                 CDEBUG(D_CACHE, "looking for null handle\n");
385                 RETURN(NULL);
386         }
387
388         if (conn->cookie == -1) {  /* this means assign a new connection */
389                 CDEBUG(D_CACHE, "want a new connection\n");
390                 RETURN(NULL);
391         }
392
393         CDEBUG(D_IOCTL, "looking for export cookie "LPX64"\n", conn->cookie);
394         export = class_handle2object(conn->cookie);
395         RETURN(export);
396 }
397
398 struct obd_device *class_exp2obd(struct obd_export *exp)
399 {
400         if (exp)
401                 return exp->exp_obd;
402         return NULL;
403 }
404
405 struct obd_device *class_conn2obd(struct lustre_handle *conn)
406 {
407         struct obd_export *export;
408         export = class_conn2export(conn);
409         if (export) {
410                 struct obd_device *obd = export->exp_obd;
411                 class_export_put(export);
412                 return obd;
413         }
414         return NULL;
415 }
416
417 struct obd_import *class_exp2cliimp(struct obd_export *exp)
418 {
419         struct obd_device *obd = exp->exp_obd;
420         if (obd == NULL)
421                 return NULL;
422         return obd->u.cli.cl_import;
423 }
424
425 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
426 {
427         struct obd_device *obd = class_conn2obd(conn);
428         if (obd == NULL)
429                 return NULL;
430         return obd->u.cli.cl_import;
431 }
432
433 /* Export management functions */
434 static void export_handle_addref(void *export)
435 {
436         class_export_get(export);
437 }
438
439 void __class_export_put(struct obd_export *exp)
440 {
441         if (atomic_dec_and_test(&exp->exp_refcount)) {
442                 struct obd_device *obd = exp->exp_obd;
443                 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
444                        exp->exp_client_uuid.uuid);
445
446                 LASSERT(obd != NULL);
447
448                 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
449                 if (exp->exp_connection)
450                         ptlrpc_put_connection_superhack(exp->exp_connection);
451
452                 LASSERT(list_empty(&exp->exp_outstanding_replies));
453                 LASSERT(list_empty(&exp->exp_handle.h_link));
454                 obd_destroy_export(exp);
455
456                 OBD_FREE(exp, sizeof(*exp));
457                 if (obd->obd_set_up) {
458                         atomic_dec(&obd->obd_refcount);
459                         wake_up(&obd->obd_refcount_waitq);
460                 } else {
461                         CERROR("removing export %p from obd %s (%p) -- OBD "
462                                "not set up (refcount = %d)\n", exp,
463                                obd->obd_name, obd,
464                                atomic_read(&obd->obd_refcount));
465                 }
466         }
467 }
468
469 /* Creates a new export, adds it to the hash table, and returns a
470  * pointer to it. The refcount is 2: one for the hash reference, and
471  * one for the pointer returned by this function. */
472 struct obd_export *class_new_export(struct obd_device *obd)
473 {
474         struct obd_export *export;
475
476         OBD_ALLOC(export, sizeof(*export));
477         if (!export) {
478                 CERROR("no memory! (minor %d)\n", obd->obd_minor);
479                 return NULL;
480         }
481
482         export->exp_conn_cnt = 0;
483         atomic_set(&export->exp_refcount, 2);
484         atomic_set(&export->exp_rpc_count, 0);
485         export->exp_obd = obd;
486         export->exp_flags = 0;
487         INIT_LIST_HEAD(&export->exp_outstanding_replies);
488         /* XXX this should be in LDLM init */
489         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
490
491         INIT_LIST_HEAD(&export->exp_handle.h_link);
492         class_handle_hash(&export->exp_handle, export_handle_addref);
493         spin_lock_init(&export->exp_lock);
494
495         spin_lock(&obd->obd_dev_lock);
496         LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
497         atomic_inc(&obd->obd_refcount);
498         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
499         export->exp_obd->obd_num_exports++;
500         spin_unlock(&obd->obd_dev_lock);
501         obd_init_export(export);
502         return export;
503 }
504
505 void class_unlink_export(struct obd_export *exp)
506 {
507         class_handle_unhash(&exp->exp_handle);
508
509         spin_lock(&exp->exp_obd->obd_dev_lock);
510         list_del_init(&exp->exp_obd_chain);
511         exp->exp_obd->obd_num_exports--;
512         spin_unlock(&exp->exp_obd->obd_dev_lock);
513
514         class_export_put(exp);
515 }
516
517 /* Import management functions */
518 static void import_handle_addref(void *import)
519 {
520         class_import_get(import);
521 }
522
523 struct obd_import *class_import_get(struct obd_import *import)
524 {
525         atomic_inc(&import->imp_refcount);
526         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
527                atomic_read(&import->imp_refcount));
528         return import;
529 }
530
531 void class_import_put(struct obd_import *import)
532 {
533         ENTRY;
534
535         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
536                atomic_read(&import->imp_refcount) - 1);
537
538         LASSERT(atomic_read(&import->imp_refcount) > 0);
539         LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
540         
541         if (!atomic_dec_and_test(&import->imp_refcount)) {
542                 EXIT;
543                 return;
544         }
545
546         CDEBUG(D_IOCTL, "destroying import %p\n", import);
547         
548         if (import->imp_connection)
549                 ptlrpc_put_connection_superhack(import->imp_connection);
550
551         while (!list_empty(&import->imp_conn_list)) {
552                 struct obd_import_conn *imp_conn;
553
554                 imp_conn = list_entry(import->imp_conn_list.next,
555                                       struct obd_import_conn, oic_item);
556                 list_del(&imp_conn->oic_item);
557                 if (imp_conn->oic_conn)
558                         ptlrpc_put_connection_superhack(imp_conn->oic_conn);
559                 OBD_FREE(imp_conn, sizeof(*imp_conn));
560         }
561
562         LASSERT(list_empty(&import->imp_handle.h_link));
563         OBD_FREE(import, sizeof(*import));
564         EXIT;
565 }
566
567 struct obd_import *class_new_import(void)
568 {
569         struct obd_import *imp;
570
571         OBD_ALLOC(imp, sizeof(*imp));
572         if (imp == NULL)
573                 return NULL;
574
575         INIT_LIST_HEAD(&imp->imp_replay_list);
576         INIT_LIST_HEAD(&imp->imp_sending_list);
577         INIT_LIST_HEAD(&imp->imp_delayed_list);
578         spin_lock_init(&imp->imp_lock);
579         imp->imp_conn_cnt = 0;
580         imp->imp_max_transno = 0;
581         imp->imp_peer_committed_transno = 0;
582         imp->imp_state = LUSTRE_IMP_NEW;
583         init_waitqueue_head(&imp->imp_recovery_waitq);
584
585         atomic_set(&imp->imp_refcount, 2);
586         atomic_set(&imp->imp_inflight, 0);
587         atomic_set(&imp->imp_replay_inflight, 0);
588         INIT_LIST_HEAD(&imp->imp_conn_list);
589         INIT_LIST_HEAD(&imp->imp_handle.h_link);
590         class_handle_hash(&imp->imp_handle, import_handle_addref);
591
592         return imp;
593 }
594
595 void class_destroy_import(struct obd_import *import)
596 {
597         LASSERT(import != NULL);
598         LASSERT(import != LP_POISON);
599
600         class_handle_unhash(&import->imp_handle);
601
602         /* Abort any inflight DLM requests and NULL out their (about to be
603          * freed) import. */
604         /* Invalidate all requests on import, would be better to call
605            ptlrpc_set_import_active(imp, 0); */
606         import->imp_generation++;
607         ptlrpc_abort_inflight_superhack(import);
608
609         class_import_put(import);
610 }
611
612 /* A connection defines an export context in which preallocation can
613    be managed. This releases the export pointer reference, and returns
614    the export handle, so the export refcount is 1 when this function
615    returns. */
616 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
617                   struct obd_uuid *cluuid)
618 {
619         struct obd_export *export;
620         LASSERT(conn != NULL);
621         LASSERT(obd != NULL);
622         LASSERT(cluuid != NULL);
623         ENTRY;
624
625         export = class_new_export(obd);
626         if (export == NULL)
627                 RETURN(-ENOMEM);
628
629         conn->cookie = export->exp_handle.h_cookie;
630         memcpy(&export->exp_client_uuid, cluuid,
631                sizeof(export->exp_client_uuid));
632         class_export_put(export);
633
634         CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
635                cluuid->uuid, conn->cookie);
636         RETURN(0);
637 }
638
639 /* This function removes two references from the export: one for the
640  * hash entry and one for the export pointer passed in.  The export
641  * pointer passed to this function is destroyed should not be used
642  * again. */
643 int class_disconnect(struct obd_export *export, unsigned long flags)
644 {
645         ENTRY;
646
647         if (export == NULL) {
648                 fixme();
649                 CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
650                 RETURN(-EINVAL);
651         }
652
653         /* XXX this shouldn't have to be here, but double-disconnect will crash
654          * otherwise, and sometimes double-disconnect happens.  abort_recovery,
655          * for example. */
656         if (list_empty(&export->exp_handle.h_link))
657                 RETURN(0);
658
659         CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
660                export->exp_handle.h_cookie);
661
662         if (export->exp_handle.h_cookie == LL_POISON) {
663                 CERROR("disconnecting freed export %p, ignoring\n", export);
664         } else {
665                 class_unlink_export(export);
666                 class_export_put(export);
667         }
668         RETURN(0);
669 }
670
671 static void class_disconnect_export_list(struct list_head *list, unsigned long flags)
672 {
673         struct obd_export *fake_exp, *exp;
674         struct lustre_handle fake_conn;
675         int rc;
676         ENTRY;
677
678         /* Move all of the exports from obd_exports to a work list, en masse. */
679         /* It's possible that an export may disconnect itself, but
680          * nothing else will be added to this list. */
681         while(!list_empty(list)) {
682                 exp = list_entry(list->next, struct obd_export, exp_obd_chain);
683                 class_export_get(exp);
684
685                 if (obd_uuid_equals(&exp->exp_client_uuid,
686                                     &exp->exp_obd->obd_uuid)) {
687                         CDEBUG(D_HA,
688                                "exp %p export uuid == obd uuid, don't discon\n",
689                                exp);
690                         /*
691                          * need to delete this now so we don't end up pointing
692                          * to work_list later when this export is cleaned up.
693                          */
694                         list_del_init(&exp->exp_obd_chain);
695                         class_export_put(exp);
696                         continue;
697                 }
698
699                 fake_conn.cookie = exp->exp_handle.h_cookie;
700                 fake_exp = class_conn2export(&fake_conn);
701                 if (!fake_exp) {
702                         class_export_put(exp);
703                         continue;
704                 }
705                 
706                 rc = obd_disconnect(fake_exp, flags);
707                 class_export_put(exp);
708                 if (rc) {
709                         CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
710                                exp, rc);
711                 } else {
712                         CDEBUG(D_HA, "export %p disconnected\n", exp);
713                 }
714         }
715         EXIT;
716 }
717
718 void class_disconnect_exports(struct obd_device *obd, unsigned long flags)
719 {
720         struct list_head work_list;
721         ENTRY;
722
723         /* Move all of the exports from obd_exports to a work list, en masse. */
724         spin_lock(&obd->obd_dev_lock);
725         list_add(&work_list, &obd->obd_exports);
726         list_del_init(&obd->obd_exports);
727         spin_unlock(&obd->obd_dev_lock);
728
729         CDEBUG(D_HA, "OBD device %d (%p) has exports, "
730                "disconnecting them\n", obd->obd_minor, obd);
731         class_disconnect_export_list(&work_list, flags);
732         EXIT;
733 }
734
735 /* Remove exports that have not completed recovery.
736  */
737 void class_disconnect_stale_exports(struct obd_device *obd, unsigned long flags)
738 {
739         struct list_head work_list;
740         struct list_head *pos, *n;
741         struct obd_export *exp;
742         int cnt = 0;
743         ENTRY;
744
745         INIT_LIST_HEAD(&work_list);
746         spin_lock(&obd->obd_dev_lock);
747         list_for_each_safe(pos, n, &obd->obd_exports) {
748                 exp = list_entry(pos, struct obd_export, exp_obd_chain);
749                 if (exp->exp_replay_needed) {
750                         list_del(&exp->exp_obd_chain);
751                         list_add(&exp->exp_obd_chain, &work_list);
752                         cnt++;
753                 }
754         }
755         spin_unlock(&obd->obd_dev_lock);
756
757         CDEBUG(D_ERROR, "%s: disconnecting %d stale clients\n",
758                obd->obd_name, cnt);
759         class_disconnect_export_list(&work_list, flags);
760         EXIT;
761 }
762
763
764
765 int oig_init(struct obd_io_group **oig_out)
766 {
767         struct obd_io_group *oig;
768         ENTRY;
769
770         OBD_ALLOC(oig, sizeof(*oig));
771         if (oig == NULL)
772                 RETURN(-ENOMEM);
773
774         spin_lock_init(&oig->oig_lock);
775         oig->oig_rc = 0;
776         oig->oig_pending = 0;
777         atomic_set(&oig->oig_refcount, 1);
778         init_waitqueue_head(&oig->oig_waitq);
779         INIT_LIST_HEAD(&oig->oig_occ_list);
780
781         *oig_out = oig;
782         RETURN(0);
783 };
784
785 static inline void oig_grab(struct obd_io_group *oig)
786 {
787         atomic_inc(&oig->oig_refcount);
788 }
789 void oig_release(struct obd_io_group *oig)
790 {
791         if (atomic_dec_and_test(&oig->oig_refcount))
792                 OBD_FREE(oig, sizeof(*oig));
793 }
794
795 void oig_add_one(struct obd_io_group *oig, struct oig_callback_context *occ)
796 {
797         unsigned long flags;
798         CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
799         spin_lock_irqsave(&oig->oig_lock, flags);
800         oig->oig_pending++;
801         if (occ != NULL)
802                 list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
803         spin_unlock_irqrestore(&oig->oig_lock, flags);
804         oig_grab(oig);
805 }
806
807 void oig_complete_one(struct obd_io_group *oig,
808                       struct oig_callback_context *occ, int rc)
809 {
810         unsigned long flags;
811         wait_queue_head_t *wake = NULL;
812         int old_rc;
813
814         spin_lock_irqsave(&oig->oig_lock, flags);
815
816         if (occ != NULL)
817                 list_del_init(&occ->occ_oig_item);
818
819         old_rc = oig->oig_rc;
820         if (oig->oig_rc == 0 && rc != 0)
821                 oig->oig_rc = rc;
822
823         if (--oig->oig_pending <= 0)
824                 wake = &oig->oig_waitq;
825
826         spin_unlock_irqrestore(&oig->oig_lock, flags);
827
828         CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
829                         "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
830                         oig->oig_pending);
831         if (wake)
832                 wake_up(wake);
833         oig_release(oig);
834 }
835
836 static int oig_done(struct obd_io_group *oig)
837 {
838         unsigned long flags;
839         int rc = 0;
840         spin_lock_irqsave(&oig->oig_lock, flags);
841         if (oig->oig_pending <= 0)
842                 rc = 1;
843         spin_unlock_irqrestore(&oig->oig_lock, flags);
844         return rc;
845 }
846
847 static void interrupted_oig(void *data)
848 {
849         struct obd_io_group *oig = data;
850         struct oig_callback_context *occ;
851         unsigned long flags;
852
853         spin_lock_irqsave(&oig->oig_lock, flags);
854         /* We need to restart the processing each time we drop the lock, as
855          * it is possible other threads called oig_complete_one() to remove
856          * an entry elsewhere in the list while we dropped lock.  We need to
857          * drop the lock because osc_ap_completion() calls oig_complete_one()
858          * which re-gets this lock ;-) as well as a lock ordering issue. */
859 restart:
860         list_for_each_entry(occ, &oig->oig_occ_list, occ_oig_item) {
861                 if (occ->interrupted)
862                         continue;
863                 occ->interrupted = 1;
864                 spin_unlock_irqrestore(&oig->oig_lock, flags);
865                 occ->occ_interrupted(occ);
866                 spin_lock_irqsave(&oig->oig_lock, flags);
867                 goto restart;
868         }
869         spin_unlock_irqrestore(&oig->oig_lock, flags);
870 }
871
872 int oig_wait(struct obd_io_group *oig)
873 {
874         struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
875         int rc;
876
877         CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
878
879         do {
880                 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
881                 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
882                 /* we can't continue until the oig has emptied and stopped
883                  * referencing state that the caller will free upon return */
884                 if (rc == -EINTR)
885                         lwi = (struct l_wait_info){ 0, };
886         } while (rc == -EINTR);
887
888         LASSERTF(oig->oig_pending == 0,
889                  "exiting oig_wait(oig = %p) with %d pending\n", oig,
890                  oig->oig_pending);
891
892         CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);
893         return oig->oig_rc;
894 }