Whamcloud - gitweb
- add debug message
[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         obd->obd_type = NULL;
216         //memset(obd, 0, sizeof(*obd));
217         obd->obd_minor = minor;
218         spin_unlock(&obd_dev_lock);
219 }
220
221 int class_name2dev(char *name)
222 {
223         int i;
224
225         if (!name)
226                 return -1;
227
228         spin_lock(&obd_dev_lock);
229         for (i = 0; i < MAX_OBD_DEVICES; i++) {
230                 struct obd_device *obd = &obd_dev[i];
231                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
232                         spin_unlock(&obd_dev_lock);
233                         return i;
234                 }
235         }
236         spin_unlock(&obd_dev_lock);
237         return -1;
238 }
239
240 struct obd_device *class_name2obd(char *name)
241 {
242         int dev = class_name2dev(name);
243         if (dev < 0)
244                 return NULL;
245         return &obd_dev[dev];
246 }
247
248 int class_uuid2dev(struct obd_uuid *uuid)
249 {
250         int i;
251         spin_lock(&obd_dev_lock);
252         for (i = 0; i < MAX_OBD_DEVICES; i++) {
253                 struct obd_device *obd = &obd_dev[i];
254                 if (obd_uuid_equals(uuid, &obd->obd_uuid)) {
255                         spin_unlock(&obd_dev_lock);
256                         return i;
257                 }
258         }
259         spin_unlock(&obd_dev_lock);
260         return -1;
261 }
262
263 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
264 {
265         int dev = class_uuid2dev(uuid);
266         if (dev < 0)
267                 return NULL;
268         return &obd_dev[dev];
269 }
270
271 /* Search for a client OBD connected to tgt_uuid.  If grp_uuid is
272    specified, then only the client with that uuid is returned,
273    otherwise any client connected to the tgt is returned.
274    If tgt_uuid is NULL, the lov with grp_uuid is returned. */
275 struct obd_device *class_find_client_obd(struct obd_uuid *tgt_uuid,
276                                          char *typ_name,
277                                          struct obd_uuid *grp_uuid)
278 {
279         int i;
280
281         spin_lock(&obd_dev_lock);
282         for (i = 0; i < MAX_OBD_DEVICES; i++) {
283                 struct obd_device *obd = &obd_dev[i];
284                 if (obd->obd_type == NULL)
285                         continue;
286                 if ((strncmp(obd->obd_type->typ_name, typ_name,
287                              strlen(typ_name)) == 0)) {
288                         struct client_obd *cli = &obd->u.cli;
289                         struct obd_import *imp = cli->cl_import;
290                         if (tgt_uuid == NULL) {
291                                 LASSERT(grp_uuid);
292                                 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
293                                         spin_unlock(&obd_dev_lock);
294                                         return obd;
295                                 }
296                                 continue;
297                         }
298                         if (obd_uuid_equals(tgt_uuid, &imp->imp_target_uuid) &&
299                             ((grp_uuid)? obd_uuid_equals(grp_uuid,
300                                                          &obd->obd_uuid) : 1)) {
301                                 spin_unlock(&obd_dev_lock);
302                                 return obd;
303                         }
304                 }
305         }
306         spin_unlock(&obd_dev_lock);
307         return NULL;
308 }
309
310 /* Iterate the obd_device list looking devices have grp_uuid. Start
311    searching at *next, and if a device is found, the next index to look
312    it is saved in *next. If next is NULL, then the first matching device
313    will always be returned. */
314 struct obd_device *class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
315 {
316         int i;
317
318         if (next == NULL) 
319                 i = 0;
320         else if (*next >= 0 && *next < MAX_OBD_DEVICES)
321                 i = *next;
322         else 
323                 return NULL;
324         spin_lock(&obd_dev_lock);                
325         for (; i < MAX_OBD_DEVICES; i++) {
326                 struct obd_device *obd = &obd_dev[i];
327                 if (obd->obd_type == NULL)
328                         continue;
329                 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
330                         if (next != NULL)
331                                 *next = i+1;
332                         spin_unlock(&obd_dev_lock);
333                         return obd;
334                 }
335         }
336
337         spin_unlock(&obd_dev_lock);
338         return NULL;
339 }
340
341 void obd_cleanup_caches(void)
342 {
343         ENTRY;
344         if (obdo_cachep) {
345                 LASSERTF(kmem_cache_destroy(obdo_cachep) == 0,
346                          "Cannot destory ll_obdo_cache\n");
347                 obdo_cachep = NULL;
348         }
349         if (import_cachep) {
350                 LASSERTF(kmem_cache_destroy(import_cachep) == 0,
351                          "Cannot destory ll_import_cache\n");
352                 import_cachep = NULL;
353         }
354         EXIT;
355 }
356
357 int obd_init_caches(void)
358 {
359         ENTRY;
360         LASSERT(obdo_cachep == NULL);
361         obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
362                                         0, 0, NULL, NULL);
363         if (!obdo_cachep)
364                 GOTO(out, -ENOMEM);
365
366         LASSERT(import_cachep == NULL);
367         import_cachep = kmem_cache_create("ll_import_cache",
368                                           sizeof(struct obd_import),
369                                           0, 0, NULL, NULL);
370         if (!import_cachep)
371                 GOTO(out, -ENOMEM);
372
373         RETURN(0);
374  out:
375         obd_cleanup_caches();
376         RETURN(-ENOMEM);
377
378 }
379
380 /* map connection to client */
381 struct obd_export *class_conn2export(struct lustre_handle *conn)
382 {
383         struct obd_export *export;
384         ENTRY;
385
386         if (!conn) {
387                 CDEBUG(D_CACHE, "looking for null handle\n");
388                 RETURN(NULL);
389         }
390
391         if (conn->cookie == -1) {  /* this means assign a new connection */
392                 CDEBUG(D_CACHE, "want a new connection\n");
393                 RETURN(NULL);
394         }
395
396         CDEBUG(D_IOCTL, "looking for export cookie "LPX64"\n", conn->cookie);
397         export = class_handle2object(conn->cookie);
398         RETURN(export);
399 }
400
401 struct obd_device *class_exp2obd(struct obd_export *exp)
402 {
403         if (exp)
404                 return exp->exp_obd;
405         return NULL;
406 }
407
408 struct obd_device *class_conn2obd(struct lustre_handle *conn)
409 {
410         struct obd_export *export;
411         export = class_conn2export(conn);
412         if (export) {
413                 struct obd_device *obd = export->exp_obd;
414                 class_export_put(export);
415                 return obd;
416         }
417         return NULL;
418 }
419
420 struct obd_import *class_exp2cliimp(struct obd_export *exp)
421 {
422         struct obd_device *obd = exp->exp_obd;
423         if (obd == NULL)
424                 return NULL;
425         return obd->u.cli.cl_import;
426 }
427
428 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
429 {
430         struct obd_device *obd = class_conn2obd(conn);
431         if (obd == NULL)
432                 return NULL;
433         return obd->u.cli.cl_import;
434 }
435
436 /* Export management functions */
437 static void export_handle_addref(void *export)
438 {
439         class_export_get(export);
440 }
441
442 void __class_export_put(struct obd_export *exp)
443 {
444         if (atomic_dec_and_test(&exp->exp_refcount)) {
445                 struct obd_device *obd = exp->exp_obd;
446                 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
447                        exp->exp_client_uuid.uuid);
448
449                 LASSERT(obd != NULL);
450
451                 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
452                 if (exp->exp_connection)
453                         ptlrpc_put_connection_superhack(exp->exp_connection);
454
455                 LASSERT(list_empty(&exp->exp_outstanding_replies));
456                 LASSERT(list_empty(&exp->exp_handle.h_link));
457                 obd_destroy_export(exp);
458
459                 OBD_FREE(exp, sizeof(*exp));
460                 if (obd->obd_set_up) {
461                         atomic_dec(&obd->obd_refcount);
462                         wake_up(&obd->obd_refcount_waitq);
463                 } else {
464                         CERROR("removing export %p from obd %s (%p) -- OBD "
465                                "not set up (refcount = %d)\n", exp,
466                                obd->obd_name, obd,
467                                atomic_read(&obd->obd_refcount));
468                 }
469         }
470 }
471
472 /* Creates a new export, adds it to the hash table, and returns a
473  * pointer to it. The refcount is 2: one for the hash reference, and
474  * one for the pointer returned by this function. */
475 struct obd_export *class_new_export(struct obd_device *obd)
476 {
477         struct obd_export *export;
478
479         OBD_ALLOC(export, sizeof(*export));
480         if (!export) {
481                 CERROR("no memory! (minor %d)\n", obd->obd_minor);
482                 return NULL;
483         }
484
485         export->exp_conn_cnt = 0;
486         atomic_set(&export->exp_refcount, 2);
487         atomic_set(&export->exp_rpc_count, 0);
488         export->exp_obd = obd;
489         export->exp_flags = 0;
490         INIT_LIST_HEAD(&export->exp_outstanding_replies);
491         /* XXX this should be in LDLM init */
492         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
493
494         INIT_LIST_HEAD(&export->exp_handle.h_link);
495         class_handle_hash(&export->exp_handle, export_handle_addref);
496         spin_lock_init(&export->exp_lock);
497
498         spin_lock(&obd->obd_dev_lock);
499         LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
500         atomic_inc(&obd->obd_refcount);
501         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
502         export->exp_obd->obd_num_exports++;
503         spin_unlock(&obd->obd_dev_lock);
504         export->exp_connected = 1;
505         obd_init_export(export);
506         return export;
507 }
508
509 void class_unlink_export(struct obd_export *exp)
510 {
511         class_handle_unhash(&exp->exp_handle);
512
513         spin_lock(&exp->exp_obd->obd_dev_lock);
514         list_del_init(&exp->exp_obd_chain);
515         exp->exp_obd->obd_num_exports--;
516         spin_unlock(&exp->exp_obd->obd_dev_lock);
517
518         class_export_put(exp);
519 }
520
521 /* Import management functions */
522 static void import_handle_addref(void *import)
523 {
524         class_import_get(import);
525 }
526
527 struct obd_import *class_import_get(struct obd_import *import)
528 {
529         atomic_inc(&import->imp_refcount);
530         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
531                atomic_read(&import->imp_refcount));
532         return import;
533 }
534
535 void class_import_put(struct obd_import *import)
536 {
537         ENTRY;
538
539         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
540                atomic_read(&import->imp_refcount) - 1);
541
542         LASSERT(atomic_read(&import->imp_refcount) > 0);
543         LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
544         
545         if (!atomic_dec_and_test(&import->imp_refcount)) {
546                 EXIT;
547                 return;
548         }
549
550         CDEBUG(D_IOCTL, "destroying import %p\n", import);
551         
552         if (import->imp_connection)
553                 ptlrpc_put_connection_superhack(import->imp_connection);
554
555         LASSERT(!import->imp_sec);
556         while (!list_empty(&import->imp_conn_list)) {
557                 struct obd_import_conn *imp_conn;
558
559                 imp_conn = list_entry(import->imp_conn_list.next,
560                                       struct obd_import_conn, oic_item);
561                 list_del(&imp_conn->oic_item);
562                 if (imp_conn->oic_conn)
563                         ptlrpc_put_connection_superhack(imp_conn->oic_conn);
564                 OBD_FREE(imp_conn, sizeof(*imp_conn));
565         }
566
567         LASSERT(list_empty(&import->imp_handle.h_link));
568         OBD_FREE(import, sizeof(*import));
569         EXIT;
570 }
571
572 struct obd_import *class_new_import(void)
573 {
574         struct obd_import *imp;
575
576         OBD_ALLOC(imp, sizeof(*imp));
577         if (imp == NULL)
578                 return NULL;
579
580         INIT_LIST_HEAD(&imp->imp_replay_list);
581         INIT_LIST_HEAD(&imp->imp_sending_list);
582         INIT_LIST_HEAD(&imp->imp_delayed_list);
583         INIT_LIST_HEAD(&imp->imp_rawrpc_list);
584         spin_lock_init(&imp->imp_lock);
585         imp->imp_conn_cnt = 0;
586         imp->imp_max_transno = 0;
587         imp->imp_peer_committed_transno = 0;
588         imp->imp_state = LUSTRE_IMP_NEW;
589         init_waitqueue_head(&imp->imp_recovery_waitq);
590
591         atomic_set(&imp->imp_refcount, 2);
592         atomic_set(&imp->imp_inflight, 0);
593         atomic_set(&imp->imp_replay_inflight, 0);
594         INIT_LIST_HEAD(&imp->imp_conn_list);
595         INIT_LIST_HEAD(&imp->imp_handle.h_link);
596         class_handle_hash(&imp->imp_handle, import_handle_addref);
597         imp->imp_waiting_ping_reply = 0;
598
599         return imp;
600 }
601
602 void class_destroy_import(struct obd_import *import)
603 {
604         LASSERT(import != NULL);
605         LASSERT(import != LP_POISON);
606
607         class_handle_unhash(&import->imp_handle);
608
609         /* Abort any inflight DLM requests and NULL out their (about to be
610          * freed) import. */
611         /* Invalidate all requests on import, would be better to call
612            ptlrpc_set_import_active(imp, 0); */
613         import->imp_generation++;
614         ptlrpc_abort_inflight_superhack(import);
615
616         class_import_put(import);
617 }
618
619 /* A connection defines an export context in which preallocation can
620    be managed. This releases the export pointer reference, and returns
621    the export handle, so the export refcount is 1 when this function
622    returns. */
623 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
624                   struct obd_uuid *cluuid)
625 {
626         struct obd_export *export;
627         LASSERT(conn != NULL);
628         LASSERT(obd != NULL);
629         LASSERT(cluuid != NULL);
630         ENTRY;
631
632         export = class_new_export(obd);
633         if (export == NULL)
634                 RETURN(-ENOMEM);
635
636         conn->cookie = export->exp_handle.h_cookie;
637         memcpy(&export->exp_client_uuid, cluuid,
638                sizeof(export->exp_client_uuid));
639         class_export_put(export);
640
641         CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
642                cluuid->uuid, conn->cookie);
643         RETURN(0);
644 }
645
646 /* This function removes two references from the export: one for the
647  * hash entry and one for the export pointer passed in.  The export
648  * pointer passed to this function is destroyed should not be used
649  * again. */
650 int class_disconnect(struct obd_export *export, unsigned long flags)
651 {
652         ENTRY;
653
654         if (export == NULL) {
655                 fixme();
656                 CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
657                 RETURN(-EINVAL);
658         }
659
660         /* XXX this shouldn't have to be here, but double-disconnect will crash
661          * otherwise, and sometimes double-disconnect happens.  abort_recovery,
662          * for example. */
663         if (list_empty(&export->exp_handle.h_link))
664                 RETURN(0);
665
666         CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
667                export->exp_handle.h_cookie);
668
669         if (export->exp_handle.h_cookie == LL_POISON) {
670                 CERROR("disconnecting freed export %p, ignoring\n", export);
671         } else {
672                 class_unlink_export(export);
673                 class_export_put(export);
674         }
675         RETURN(0);
676 }
677
678 static void class_disconnect_export_list(struct list_head *list, unsigned long flags)
679 {
680         struct obd_export *fake_exp, *exp;
681         struct lustre_handle fake_conn;
682         int rc;
683         ENTRY;
684
685         /* Move all of the exports from obd_exports to a work list, en masse. */
686         /* It's possible that an export may disconnect itself, but
687          * nothing else will be added to this list. */
688         while(!list_empty(list)) {
689                 exp = list_entry(list->next, struct obd_export, exp_obd_chain);
690                 class_export_get(exp);
691
692                 if (obd_uuid_equals(&exp->exp_client_uuid,
693                                     &exp->exp_obd->obd_uuid)) {
694                         CDEBUG(D_HA,
695                                "exp %p export uuid == obd uuid, don't discon\n",
696                                exp);
697                         /*
698                          * need to delete this now so we don't end up pointing
699                          * to work_list later when this export is cleaned up.
700                          */
701                         list_del_init(&exp->exp_obd_chain);
702                         class_export_put(exp);
703                         continue;
704                 }
705
706                 fake_conn.cookie = exp->exp_handle.h_cookie;
707                 fake_exp = class_conn2export(&fake_conn);
708                 if (!fake_exp) {
709                         class_export_put(exp);
710                         continue;
711                 }
712                 
713                 rc = obd_disconnect(fake_exp, flags);
714                 class_export_put(exp);
715                 if (rc) {
716                         CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
717                                exp, rc);
718                 } else {
719                         CDEBUG(D_HA, "export %p disconnected\n", exp);
720                 }
721         }
722         EXIT;
723 }
724
725 void class_disconnect_exports(struct obd_device *obd, unsigned long flags)
726 {
727         struct list_head work_list;
728         ENTRY;
729
730         /* Move all of the exports from obd_exports to a work list, en masse. */
731         spin_lock(&obd->obd_dev_lock);
732         list_add(&work_list, &obd->obd_exports);
733         list_del_init(&obd->obd_exports);
734         spin_unlock(&obd->obd_dev_lock);
735
736         CDEBUG(D_HA, "OBD device %d (%p) has exports, "
737                "disconnecting them\n", obd->obd_minor, obd);
738         class_disconnect_export_list(&work_list, flags);
739         EXIT;
740 }
741
742 /* Remove exports that have not completed recovery.
743  */
744 int class_disconnect_stale_exports(struct obd_device *obd,
745                                    int (*test_export)(struct obd_export *),
746                                    unsigned long flags)
747 {
748         char str[PTL_NALFMT_SIZE];
749         struct list_head work_list;
750         struct list_head *pos, *n;
751         struct obd_export *exp;
752         int cnt = 0;
753         ENTRY;
754
755         INIT_LIST_HEAD(&work_list);
756         spin_lock(&obd->obd_dev_lock);
757         list_for_each_safe(pos, n, &obd->obd_exports) {
758                 exp = list_entry(pos, struct obd_export, exp_obd_chain);
759                 if (!test_export(exp)) {
760                         list_del(&exp->exp_obd_chain);
761                         list_add(&exp->exp_obd_chain, &work_list);
762                         cnt++;
763                         ptlrpc_peernid2str(&exp->exp_connection->c_peer, str);
764                         CDEBUG(D_ERROR, "%s: disconnect stale client %s@%s\n",
765                                obd->obd_name, exp->exp_client_uuid.uuid, str);
766                 }
767         }
768         spin_unlock(&obd->obd_dev_lock);
769
770         CDEBUG(D_ERROR, "%s: disconnecting %d stale clients\n",
771                obd->obd_name, cnt);
772         class_disconnect_export_list(&work_list, flags);
773         RETURN(cnt);
774 }
775
776
777
778 int oig_init(struct obd_io_group **oig_out)
779 {
780         struct obd_io_group *oig;
781         ENTRY;
782
783         OBD_ALLOC(oig, sizeof(*oig));
784         if (oig == NULL)
785                 RETURN(-ENOMEM);
786
787         spin_lock_init(&oig->oig_lock);
788         oig->oig_rc = 0;
789         oig->oig_pending = 0;
790         atomic_set(&oig->oig_refcount, 1);
791         init_waitqueue_head(&oig->oig_waitq);
792         INIT_LIST_HEAD(&oig->oig_occ_list);
793
794         *oig_out = oig;
795         RETURN(0);
796 };
797
798 static inline void oig_grab(struct obd_io_group *oig)
799 {
800         atomic_inc(&oig->oig_refcount);
801 }
802
803 void oig_release(struct obd_io_group *oig)
804 {
805         if (atomic_dec_and_test(&oig->oig_refcount))
806                 OBD_FREE(oig, sizeof(*oig));
807 }
808
809 void oig_add_one(struct obd_io_group *oig, struct oig_callback_context *occ)
810 {
811         unsigned long flags;
812         CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
813         spin_lock_irqsave(&oig->oig_lock, flags);
814         oig->oig_pending++;
815         if (occ != NULL)
816                 list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
817         spin_unlock_irqrestore(&oig->oig_lock, flags);
818         oig_grab(oig);
819 }
820
821 void oig_complete_one(struct obd_io_group *oig,
822                       struct oig_callback_context *occ, int rc)
823 {
824         unsigned long flags;
825         wait_queue_head_t *wake = NULL;
826         int old_rc;
827
828         spin_lock_irqsave(&oig->oig_lock, flags);
829
830         if (occ != NULL)
831                 list_del_init(&occ->occ_oig_item);
832
833         old_rc = oig->oig_rc;
834         if (oig->oig_rc == 0 && rc != 0)
835                 oig->oig_rc = rc;
836
837         if (--oig->oig_pending <= 0)
838                 wake = &oig->oig_waitq;
839
840         spin_unlock_irqrestore(&oig->oig_lock, flags);
841
842         CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
843                         "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
844                         oig->oig_pending);
845         if (wake)
846                 wake_up(wake);
847         oig_release(oig);
848 }
849
850 static int oig_done(struct obd_io_group *oig)
851 {
852         unsigned long flags;
853         int rc = 0;
854         spin_lock_irqsave(&oig->oig_lock, flags);
855         if (oig->oig_pending <= 0)
856                 rc = 1;
857         spin_unlock_irqrestore(&oig->oig_lock, flags);
858         return rc;
859 }
860
861 static void interrupted_oig(void *data)
862 {
863         struct obd_io_group *oig = data;
864         struct oig_callback_context *occ;
865         unsigned long flags;
866
867         spin_lock_irqsave(&oig->oig_lock, flags);
868         /* We need to restart the processing each time we drop the lock, as
869          * it is possible other threads called oig_complete_one() to remove
870          * an entry elsewhere in the list while we dropped lock.  We need to
871          * drop the lock because osc_ap_completion() calls oig_complete_one()
872          * which re-gets this lock ;-) as well as a lock ordering issue. */
873 restart:
874         list_for_each_entry(occ, &oig->oig_occ_list, occ_oig_item) {
875                 if (occ->interrupted)
876                         continue;
877                 occ->interrupted = 1;
878                 spin_unlock_irqrestore(&oig->oig_lock, flags);
879                 occ->occ_interrupted(occ);
880                 spin_lock_irqsave(&oig->oig_lock, flags);
881                 goto restart;
882         }
883         spin_unlock_irqrestore(&oig->oig_lock, flags);
884 }
885
886 int oig_wait(struct obd_io_group *oig)
887 {
888         struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
889         int rc;
890
891         CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
892
893         do {
894                 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
895                 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
896                 /* we can't continue until the oig has emptied and stopped
897                  * referencing state that the caller will free upon return */
898                 if (rc == -EINTR)
899                         lwi = (struct l_wait_info){ 0, };
900         } while (rc == -EINTR);
901
902         LASSERTF(oig->oig_pending == 0,
903                  "exiting oig_wait(oig = %p) with %d pending\n", oig,
904                  oig->oig_pending);
905
906         CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);
907         return oig->oig_rc;
908 }