Whamcloud - gitweb
b=13147
[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 the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  *
24  * These are the only exported functions, they provide some generic
25  * infrastructure for managing object devices
26  */
27
28 #define DEBUG_SUBSYSTEM S_CLASS
29 #ifndef __KERNEL__
30 #include <liblustre.h>
31 #endif
32 #include <obd_ost.h>
33 #include <obd_class.h>
34 #include <lprocfs_status.h>
35
36 extern struct list_head obd_types;
37 spinlock_t obd_types_lock;
38
39 cfs_mem_cache_t *obd_device_cachep;
40 cfs_mem_cache_t *obdo_cachep;
41 EXPORT_SYMBOL(obdo_cachep);
42 cfs_mem_cache_t *import_cachep;
43
44 struct list_head  obd_zombie_imports;
45 struct list_head  obd_zombie_exports;
46 spinlock_t        obd_zombie_impexp_lock;
47 void            (*obd_zombie_impexp_notify)(void) = NULL;
48 EXPORT_SYMBOL(obd_zombie_impexp_notify);
49
50
51 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
52
53 /*
54  * support functions: we could use inter-module communication, but this
55  * is more portable to other OS's
56  */
57 static struct obd_device *obd_device_alloc(void)
58 {
59         struct obd_device *obd;
60
61         OBD_SLAB_ALLOC_PTR(obd, obd_device_cachep);
62         if (obd != NULL) {
63                 obd->obd_magic = OBD_DEVICE_MAGIC;
64         }
65         return obd;
66 }
67 EXPORT_SYMBOL(obd_device_alloc);
68
69 static void obd_device_free(struct obd_device *obd)
70 {
71         LASSERT(obd != NULL);
72         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "obd %p obd_magic %08x != %08x\n", 
73                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
74         OBD_SLAB_FREE_PTR(obd, obd_device_cachep);
75 }
76 EXPORT_SYMBOL(obd_device_free);
77
78 struct obd_type *class_search_type(const char *name)
79 {
80         struct list_head *tmp;
81         struct obd_type *type;
82
83         spin_lock(&obd_types_lock);
84         list_for_each(tmp, &obd_types) {
85                 type = list_entry(tmp, struct obd_type, typ_chain);
86                 if (strcmp(type->typ_name, name) == 0) {
87                         spin_unlock(&obd_types_lock);
88                         return type;
89                 }
90         }
91         spin_unlock(&obd_types_lock);
92         return NULL;
93 }
94
95 struct obd_type *class_get_type(const char *name)
96 {
97         struct obd_type *type = class_search_type(name);
98
99 #ifdef CONFIG_KMOD
100         if (!type) {
101                 const char *modname = name;
102                 if (!request_module(modname)) {
103                         CDEBUG(D_INFO, "Loaded module '%s'\n", modname);
104                         type = class_search_type(name);
105                 } else {
106                         LCONSOLE_ERROR_MSG(0x158, "Can't load module '%s'\n",
107                                            modname);
108                 }
109         }
110 #endif
111         if (type) {
112                 spin_lock(&type->obd_type_lock);
113                 type->typ_refcnt++;
114                 try_module_get(type->typ_dt_ops->o_owner);
115                 spin_unlock(&type->obd_type_lock);
116         }
117         return type;
118 }
119
120 void class_put_type(struct obd_type *type)
121 {
122         LASSERT(type);
123         spin_lock(&type->obd_type_lock);
124         type->typ_refcnt--;
125         module_put(type->typ_dt_ops->o_owner);
126         spin_unlock(&type->obd_type_lock);
127 }
128
129 #define CLASS_MAX_NAME 1024
130
131 int class_register_type(struct obd_ops *dt_ops, struct md_ops *md_ops, 
132                         struct lprocfs_vars *vars, const char *name, 
133                         struct lu_device_type *ldt)
134 {
135         struct obd_type *type;
136         int rc = 0;
137         ENTRY;
138
139         /* sanity check */
140         LASSERT(strnlen(name, CLASS_MAX_NAME) < CLASS_MAX_NAME);
141
142         if (class_search_type(name)) {
143                 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
144                 RETURN(-EEXIST);
145         }
146
147         rc = -ENOMEM;
148         OBD_ALLOC(type, sizeof(*type));
149         if (type == NULL)
150                 RETURN(rc);
151
152         OBD_ALLOC_PTR(type->typ_dt_ops);
153         OBD_ALLOC_PTR(type->typ_md_ops);
154         OBD_ALLOC(type->typ_name, strlen(name) + 1);
155         
156         if (type->typ_dt_ops == NULL || 
157             type->typ_md_ops == NULL || 
158             type->typ_name == NULL)
159                 GOTO (failed, rc);
160
161         *(type->typ_dt_ops) = *dt_ops;
162         /* md_ops is optional */
163         if (md_ops)
164                 *(type->typ_md_ops) = *md_ops;
165         strcpy(type->typ_name, name);
166         spin_lock_init(&type->obd_type_lock);
167
168 #ifdef LPROCFS
169         type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
170                                               vars, type);
171         if (IS_ERR(type->typ_procroot)) {
172                 rc = PTR_ERR(type->typ_procroot);
173                 type->typ_procroot = NULL;
174                 GOTO (failed, rc);
175         }
176 #endif
177         if (ldt != NULL) {
178                 type->typ_lu = ldt;
179                 rc = ldt->ldt_ops->ldto_init(ldt);
180                 if (rc != 0)
181                         GOTO (failed, rc);
182         }
183
184         spin_lock(&obd_types_lock);
185         list_add(&type->typ_chain, &obd_types);
186         spin_unlock(&obd_types_lock);
187
188         RETURN (0);
189
190  failed:
191         if (type->typ_name != NULL)
192                 OBD_FREE(type->typ_name, strlen(name) + 1);
193         if (type->typ_md_ops != NULL)
194                 OBD_FREE_PTR(type->typ_md_ops);
195         if (type->typ_dt_ops != NULL)
196                 OBD_FREE_PTR(type->typ_dt_ops);
197         OBD_FREE(type, sizeof(*type));
198         RETURN(rc);
199 }
200
201 int class_unregister_type(const char *name)
202 {
203         struct obd_type *type = class_search_type(name);
204         ENTRY;
205
206         if (!type) {
207                 CERROR("unknown obd type\n");
208                 RETURN(-EINVAL);
209         }
210
211         if (type->typ_refcnt) {
212                 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
213                 /* This is a bad situation, let's make the best of it */
214                 /* Remove ops, but leave the name for debugging */
215                 OBD_FREE_PTR(type->typ_dt_ops);
216                 OBD_FREE_PTR(type->typ_md_ops);
217                 RETURN(-EBUSY);
218         }
219
220         if (type->typ_procroot) {
221                 lprocfs_remove(&type->typ_procroot);
222         }
223
224         if (type->typ_lu)
225                 type->typ_lu->ldt_ops->ldto_fini(type->typ_lu);
226
227         spin_lock(&obd_types_lock);
228         list_del(&type->typ_chain);
229         spin_unlock(&obd_types_lock);
230         OBD_FREE(type->typ_name, strlen(name) + 1);
231         if (type->typ_dt_ops != NULL)
232                 OBD_FREE_PTR(type->typ_dt_ops);
233         if (type->typ_md_ops != NULL)
234                 OBD_FREE_PTR(type->typ_md_ops);
235         OBD_FREE(type, sizeof(*type));
236         RETURN(0);
237 } /* class_unregister_type */
238
239 struct obd_device *class_newdev(const char *type_name, const char *name)
240 {
241         struct obd_device *result = NULL;
242         struct obd_device *newdev;
243         struct obd_type *type = NULL;
244         int i;
245         int new_obd_minor = 0;
246
247         if (strlen(name) > MAX_OBD_NAME) {
248                 CERROR("name/uuid must be < %u bytes long\n", MAX_OBD_NAME);
249                 RETURN(ERR_PTR(-EINVAL));
250         }
251
252         type = class_get_type(type_name); 
253         if (type == NULL){
254                 CERROR("OBD: unknown type: %s\n", type_name);
255                 RETURN(ERR_PTR(-ENODEV));
256         }
257
258         newdev = obd_device_alloc();
259         if (newdev == NULL) { 
260                 class_put_type(type);
261                 RETURN(ERR_PTR(-ENOMEM));
262         }
263         LASSERT(newdev->obd_magic == OBD_DEVICE_MAGIC);
264
265         spin_lock(&obd_dev_lock);
266         for (i = 0; i < class_devno_max(); i++) {
267                 struct obd_device *obd = class_num2obd(i);
268                 if (obd && obd->obd_name &&
269                     (strcmp(name, obd->obd_name) == 0)) {
270                         CERROR("Device %s already exists, won't add\n", name);
271                         if (result) {
272                                 LASSERTF(result->obd_magic == OBD_DEVICE_MAGIC,
273                                          "%p obd_magic %08x != %08x\n", result,
274                                          result->obd_magic, OBD_DEVICE_MAGIC);
275                                 LASSERTF(result->obd_minor == new_obd_minor,
276                                          "%p obd_minor %d != %d\n", result,
277                                          result->obd_minor, new_obd_minor);
278
279                                 obd_devs[result->obd_minor] = NULL;
280                                 result->obd_name[0]='\0';
281                          }
282                         result = ERR_PTR(-EEXIST);
283                         break;
284                 }
285                 if (!result && !obd) {
286                         result = newdev;
287                         result->obd_minor = i;
288                         new_obd_minor = i;
289                         result->obd_type = type;
290                         memcpy(result->obd_name, name, strlen(name));
291                         obd_devs[i] = result;
292                 }
293         }
294         spin_unlock(&obd_dev_lock);
295         
296         if (result == NULL && i >= class_devno_max()) {
297                 CERROR("all %u OBD devices used, increase MAX_OBD_DEVICES\n",
298                        class_devno_max());
299                 result = ERR_PTR(-EOVERFLOW);
300         }
301         
302         if (IS_ERR(result)) {
303                 obd_device_free(newdev);
304                 class_put_type(type);
305         } else {
306                 CDEBUG(D_IOCTL, "Adding new device %s (%p)\n",
307                        result->obd_name, result);
308         }
309         return result;
310 }
311
312 void class_release_dev(struct obd_device *obd)
313 {
314         struct obd_type *obd_type = obd->obd_type;
315
316         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "%p obd_magic %08x != %08x\n",
317                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
318         LASSERTF(obd == obd_devs[obd->obd_minor], "obd %p != obd_devs[%d] %p\n",
319                  obd, obd->obd_minor, obd_devs[obd->obd_minor]);
320         LASSERT(obd_type != NULL);
321
322         CDEBUG(D_INFO, "Release obd device %s obd_type name =%s\n",
323                obd->obd_name,obd->obd_type->typ_name);
324
325         spin_lock(&obd_dev_lock);
326         obd_devs[obd->obd_minor] = NULL;
327         spin_unlock(&obd_dev_lock);
328         obd_device_free(obd);
329
330         class_put_type(obd_type);
331 }
332
333 int class_name2dev(const char *name)
334 {
335         int i;
336
337         if (!name)
338                 return -1;
339
340         spin_lock(&obd_dev_lock);
341         for (i = 0; i < class_devno_max(); i++) {
342                 struct obd_device *obd = class_num2obd(i);
343                 if (obd && obd->obd_name && strcmp(name, obd->obd_name) == 0) {
344                         /* Make sure we finished attaching before we give
345                            out any references */
346                         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
347                         if (obd->obd_attached) {
348                                 spin_unlock(&obd_dev_lock);
349                                 return i;
350                         }
351                         break;
352                 }
353         }
354         spin_unlock(&obd_dev_lock);
355
356         return -1;
357 }
358
359 struct obd_device *class_name2obd(const char *name)
360 {
361         int dev = class_name2dev(name);
362
363         if (dev < 0 || dev > class_devno_max())
364                 return NULL;
365         return class_num2obd(dev);
366 }
367
368 int class_uuid2dev(struct obd_uuid *uuid)
369 {
370         int i;
371
372         spin_lock(&obd_dev_lock);
373         for (i = 0; i < class_devno_max(); i++) {
374                 struct obd_device *obd = class_num2obd(i);
375                 if (obd && obd_uuid_equals(uuid, &obd->obd_uuid)) {
376                         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
377                         spin_unlock(&obd_dev_lock);
378                         return i;
379                 }
380         }
381         spin_unlock(&obd_dev_lock);
382
383         return -1;
384 }
385
386 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
387 {
388         int dev = class_uuid2dev(uuid);
389         if (dev < 0)
390                 return NULL;
391         return class_num2obd(dev);
392 }
393
394 struct obd_device *class_num2obd(int num)
395 {
396         struct obd_device *obd = NULL;
397
398         if (num < class_devno_max()) {
399                 obd = obd_devs[num];
400                 if (obd == NULL) {
401                         return NULL;
402                 }
403
404                 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
405                          "%p obd_magic %08x != %08x\n",
406                          obd, obd->obd_magic, OBD_DEVICE_MAGIC);
407                 LASSERTF(obd->obd_minor == num,
408                          "%p obd_minor %0d != %0d\n",
409                          obd, obd->obd_minor, num);
410         }
411
412         return obd;
413 }
414
415 void class_obd_list(void)
416 {
417         char *status;
418         int i;
419
420         spin_lock(&obd_dev_lock);
421         for (i = 0; i < class_devno_max(); i++) {
422                 struct obd_device *obd = class_num2obd(i);
423                 if (obd == NULL)
424                         continue;
425                 if (obd->obd_stopping)
426                         status = "ST";
427                 else if (obd->obd_set_up)
428                         status = "UP";
429                 else if (obd->obd_attached)
430                         status = "AT";
431                 else
432                         status = "--";
433                 LCONSOLE(D_CONFIG, "%3d %s %s %s %s %d\n",
434                          i, status, obd->obd_type->typ_name,
435                          obd->obd_name, obd->obd_uuid.uuid,
436                          atomic_read(&obd->obd_refcount));
437         }
438         spin_unlock(&obd_dev_lock);
439         return;
440 }
441
442 /* Search for a client OBD connected to tgt_uuid.  If grp_uuid is
443    specified, then only the client with that uuid is returned,
444    otherwise any client connected to the tgt is returned. */
445 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
446                                           const char * typ_name,
447                                           struct obd_uuid *grp_uuid)
448 {
449         int i;
450
451         spin_lock(&obd_dev_lock);
452         for (i = 0; i < class_devno_max(); i++) {
453                 struct obd_device *obd = class_num2obd(i);
454                 if (obd == NULL)
455                         continue;
456                 if ((strncmp(obd->obd_type->typ_name, typ_name,
457                              strlen(typ_name)) == 0)) {
458                         if (obd_uuid_equals(tgt_uuid,
459                                             &obd->u.cli.cl_target_uuid) &&
460                             ((grp_uuid)? obd_uuid_equals(grp_uuid,
461                                                          &obd->obd_uuid) : 1)) {
462                                 spin_unlock(&obd_dev_lock);
463                                 return obd;
464                         }
465                 }
466         }
467         spin_unlock(&obd_dev_lock);
468
469         return NULL;
470 }
471
472 struct obd_device *class_find_client_notype(struct obd_uuid *tgt_uuid,
473                                             struct obd_uuid *grp_uuid)
474 {
475         struct obd_device *obd;
476
477         obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME, NULL);
478         if (!obd)
479                 obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME,
480                                             grp_uuid);
481         return obd;
482 }
483
484 /* Iterate the obd_device list looking devices have grp_uuid. Start
485    searching at *next, and if a device is found, the next index to look
486    at is saved in *next. If next is NULL, then the first matching device
487    will always be returned. */
488 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
489 {
490         int i;
491
492         if (next == NULL)
493                 i = 0;
494         else if (*next >= 0 && *next < class_devno_max())
495                 i = *next;
496         else
497                 return NULL;
498
499         spin_lock(&obd_dev_lock);
500         for (; i < class_devno_max(); i++) {
501                 struct obd_device *obd = class_num2obd(i);
502                 if (obd == NULL)
503                         continue;
504                 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
505                         if (next != NULL)
506                                 *next = i+1;
507                         spin_unlock(&obd_dev_lock);
508                         return obd;
509                 }
510         }
511         spin_unlock(&obd_dev_lock);
512
513         return NULL;
514 }
515
516
517 void obd_cleanup_caches(void)
518 {
519         int rc;
520
521         ENTRY;
522         if (obd_device_cachep) {
523                 rc = cfs_mem_cache_destroy(obd_device_cachep);
524                 LASSERTF(rc == 0, "Cannot destropy ll_obd_device_cache: rc %d\n", rc);
525                 obd_device_cachep = NULL;
526         }
527         if (obdo_cachep) {
528                 rc = cfs_mem_cache_destroy(obdo_cachep);
529                 LASSERTF(rc == 0, "Cannot destory ll_obdo_cache\n");
530                 obdo_cachep = NULL;
531         }
532         if (import_cachep) {
533                 rc = cfs_mem_cache_destroy(import_cachep);
534                 LASSERTF(rc == 0, "Cannot destory ll_import_cache\n");
535                 import_cachep = NULL;
536         }
537         if (capa_cachep) {
538                 rc = cfs_mem_cache_destroy(capa_cachep);
539                 LASSERTF(rc == 0, "Cannot destory capa_cache\n");
540                 capa_cachep = NULL;
541         }
542         EXIT;
543 }
544
545 int obd_init_caches(void)
546 {
547         ENTRY;
548
549         LASSERT(obd_device_cachep == NULL);
550         obd_device_cachep = cfs_mem_cache_create("ll_obd_dev_cache",
551                                               sizeof(struct obd_device), 0, 0);
552         if (!obd_device_cachep)
553                 GOTO(out, -ENOMEM);
554
555         LASSERT(obdo_cachep == NULL);
556         obdo_cachep = cfs_mem_cache_create("ll_obdo_cache", sizeof(struct obdo),
557                                         0, 0);
558         if (!obdo_cachep)
559                 GOTO(out, -ENOMEM);
560
561         LASSERT(import_cachep == NULL);
562         import_cachep = cfs_mem_cache_create("ll_import_cache",
563                                           sizeof(struct obd_import),
564                                           0, 0);
565         if (!import_cachep)
566                 GOTO(out, -ENOMEM);
567
568         LASSERT(capa_cachep == NULL);
569         capa_cachep = cfs_mem_cache_create("capa_cache",
570                                            sizeof(struct obd_capa), 0, 0);
571         if (!capa_cachep)
572                 GOTO(out, -ENOMEM);
573
574         RETURN(0);
575  out:
576         obd_cleanup_caches();
577         RETURN(-ENOMEM);
578
579 }
580
581 /* map connection to client */
582 struct obd_export *class_conn2export(struct lustre_handle *conn)
583 {
584         struct obd_export *export;
585         ENTRY;
586
587         if (!conn) {
588                 CDEBUG(D_CACHE, "looking for null handle\n");
589                 RETURN(NULL);
590         }
591
592         if (conn->cookie == -1) {  /* this means assign a new connection */
593                 CDEBUG(D_CACHE, "want a new connection\n");
594                 RETURN(NULL);
595         }
596
597         CDEBUG(D_INFO, "looking for export cookie "LPX64"\n", conn->cookie);
598         export = class_handle2object(conn->cookie);
599         RETURN(export);
600 }
601
602 struct obd_device *class_exp2obd(struct obd_export *exp)
603 {
604         if (exp)
605                 return exp->exp_obd;
606         return NULL;
607 }
608
609 struct obd_device *class_conn2obd(struct lustre_handle *conn)
610 {
611         struct obd_export *export;
612         export = class_conn2export(conn);
613         if (export) {
614                 struct obd_device *obd = export->exp_obd;
615                 class_export_put(export);
616                 return obd;
617         }
618         return NULL;
619 }
620
621 struct obd_import *class_exp2cliimp(struct obd_export *exp)
622 {
623         struct obd_device *obd = exp->exp_obd;
624         if (obd == NULL)
625                 return NULL;
626         return obd->u.cli.cl_import;
627 }
628
629 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
630 {
631         struct obd_device *obd = class_conn2obd(conn);
632         if (obd == NULL)
633                 return NULL;
634         return obd->u.cli.cl_import;
635 }
636
637 /* Export management functions */
638 static void export_handle_addref(void *export)
639 {
640         class_export_get(export);
641 }
642
643 void __class_export_put(struct obd_export *exp)
644 {
645         if (atomic_dec_and_test(&exp->exp_refcount)) {
646                 LASSERT (list_empty(&exp->exp_obd_chain));
647
648                 CDEBUG(D_IOCTL, "final put %p/%s\n",
649                        exp, exp->exp_client_uuid.uuid);
650         
651                 spin_lock(&obd_zombie_impexp_lock);
652                 list_add(&exp->exp_obd_chain, &obd_zombie_exports);
653                 spin_unlock(&obd_zombie_impexp_lock);
654
655                 if (obd_zombie_impexp_notify != NULL)
656                         obd_zombie_impexp_notify();
657         }
658 }
659 EXPORT_SYMBOL(__class_export_put);
660
661 void class_export_destroy(struct obd_export *exp)
662 {
663         struct obd_device *obd = exp->exp_obd;
664         ENTRY;
665
666         LASSERT (atomic_read(&exp->exp_refcount) == 0);
667
668         CDEBUG(D_IOCTL, "destroying export %p/%s for %s\n", exp,
669                exp->exp_client_uuid.uuid, obd->obd_name);
670
671         LASSERT(obd != NULL);
672
673         /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
674         if (exp->exp_connection)
675                 ptlrpc_put_connection_superhack(exp->exp_connection);
676
677         LASSERT(list_empty(&exp->exp_outstanding_replies));
678         obd_destroy_export(exp);
679  
680         OBD_FREE_RCU(exp, sizeof(*exp), &exp->exp_handle);
681         class_decref(obd);
682         EXIT;
683 }
684
685 /* Creates a new export, adds it to the hash table, and returns a
686  * pointer to it. The refcount is 2: one for the hash reference, and
687  * one for the pointer returned by this function. */
688 struct obd_export *class_new_export(struct obd_device *obd,
689                                     struct obd_uuid *cluuid)
690 {
691         struct obd_export *export, *tmp;
692
693         OBD_ALLOC(export, sizeof(*export));
694         if (!export)
695                 return ERR_PTR(-ENOMEM);
696
697         export->exp_conn_cnt = 0;
698         atomic_set(&export->exp_refcount, 2);
699         atomic_set(&export->exp_rpc_count, 0);
700         export->exp_obd = obd;
701         CFS_INIT_LIST_HEAD(&export->exp_outstanding_replies);
702         /* XXX this should be in LDLM init */
703         CFS_INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
704         spin_lock_init(&export->exp_ldlm_data.led_lock);
705
706         CFS_INIT_LIST_HEAD(&export->exp_handle.h_link);
707         class_handle_hash(&export->exp_handle, export_handle_addref);
708         export->exp_last_request_time = CURRENT_SECONDS;
709         spin_lock_init(&export->exp_lock);
710
711         export->exp_client_uuid = *cluuid;
712         obd_init_export(export);
713
714         spin_lock(&obd->obd_dev_lock);
715         if (!obd_uuid_equals(cluuid, &obd->obd_uuid)) {
716                 list_for_each_entry(tmp, &obd->obd_exports, exp_obd_chain) {
717                         if (obd_uuid_equals(cluuid, &tmp->exp_client_uuid)) {
718                                 spin_unlock(&obd->obd_dev_lock);
719                                 CWARN("%s: denying duplicate export for %s\n",
720                                       obd->obd_name, cluuid->uuid);
721                                 class_handle_unhash(&export->exp_handle);
722                                 OBD_FREE_PTR(export);
723                                 return ERR_PTR(-EALREADY);
724                         }
725                 }
726         }
727         LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
728         class_incref(obd);
729         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
730         list_add_tail(&export->exp_obd_chain_timed,
731                       &export->exp_obd->obd_exports_timed);
732         export->exp_obd->obd_num_exports++;
733         spin_unlock(&obd->obd_dev_lock);
734
735         return export;
736 }
737 EXPORT_SYMBOL(class_new_export);
738
739 void class_unlink_export(struct obd_export *exp)
740 {
741         class_handle_unhash(&exp->exp_handle);
742
743         spin_lock(&exp->exp_obd->obd_dev_lock);
744         list_del_init(&exp->exp_obd_chain);
745         list_del_init(&exp->exp_obd_chain_timed);
746         exp->exp_obd->obd_num_exports--;
747         spin_unlock(&exp->exp_obd->obd_dev_lock);
748
749         class_export_put(exp);
750 }
751 EXPORT_SYMBOL(class_unlink_export);
752
753 /* Import management functions */
754 static void import_handle_addref(void *import)
755 {
756         class_import_get(import);
757 }
758
759 struct obd_import *class_import_get(struct obd_import *import)
760 {
761         LASSERT(atomic_read(&import->imp_refcount) >= 0);
762         LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
763         atomic_inc(&import->imp_refcount);
764         CDEBUG(D_INFO, "import %p refcount=%d\n", import,
765                atomic_read(&import->imp_refcount));
766         return import;
767 }
768 EXPORT_SYMBOL(class_import_get);
769
770 void class_import_put(struct obd_import *import)
771 {
772         ENTRY;
773
774         CDEBUG(D_INFO, "import %p refcount=%d\n", import,
775                atomic_read(&import->imp_refcount) - 1);
776
777         LASSERT(atomic_read(&import->imp_refcount) > 0);
778         LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
779         LASSERT(list_empty(&import->imp_zombie_chain));
780
781         if (atomic_dec_and_test(&import->imp_refcount)) {
782
783                 CDEBUG(D_INFO, "final put import %p\n", import);
784                 
785                 spin_lock(&obd_zombie_impexp_lock);
786                 list_add(&import->imp_zombie_chain, &obd_zombie_imports);
787                 spin_unlock(&obd_zombie_impexp_lock);
788
789                 if (obd_zombie_impexp_notify != NULL)
790                         obd_zombie_impexp_notify();
791         }
792
793         EXIT;
794 }
795
796 void class_import_destroy(struct obd_import *import)
797 {
798         ENTRY;
799         
800         CDEBUG(D_IOCTL, "destroying import %p for %s\n", import,
801                 import->imp_obd->obd_name);
802
803         LASSERT(atomic_read(&import->imp_refcount) == 0);
804
805         ptlrpc_put_connection_superhack(import->imp_connection);
806
807         while (!list_empty(&import->imp_conn_list)) {
808                 struct obd_import_conn *imp_conn;
809
810                 imp_conn = list_entry(import->imp_conn_list.next,
811                                       struct obd_import_conn, oic_item);
812                 list_del(&imp_conn->oic_item);
813                 ptlrpc_put_connection_superhack(imp_conn->oic_conn);
814                 OBD_FREE(imp_conn, sizeof(*imp_conn));
815         }
816
817         LASSERT(import->imp_sec == NULL);
818         class_decref(import->imp_obd);
819         OBD_FREE_RCU(import, sizeof(*import), &import->imp_handle);
820         EXIT;
821 }
822 EXPORT_SYMBOL(class_import_put);
823
824 struct obd_import *class_new_import(struct obd_device *obd)
825 {
826         struct obd_import *imp;
827
828         OBD_ALLOC(imp, sizeof(*imp));
829         if (imp == NULL)
830                 return NULL;
831
832         CFS_INIT_LIST_HEAD(&imp->imp_zombie_chain);
833         CFS_INIT_LIST_HEAD(&imp->imp_replay_list);
834         CFS_INIT_LIST_HEAD(&imp->imp_sending_list);
835         CFS_INIT_LIST_HEAD(&imp->imp_delayed_list);
836         spin_lock_init(&imp->imp_lock);
837         imp->imp_last_success_conn = 0;
838         imp->imp_state = LUSTRE_IMP_NEW;
839         imp->imp_obd = class_incref(obd);
840         cfs_waitq_init(&imp->imp_recovery_waitq);
841
842         atomic_set(&imp->imp_refcount, 2);
843         atomic_set(&imp->imp_inflight, 0);
844         atomic_set(&imp->imp_replay_inflight, 0);
845         atomic_set(&imp->imp_inval_count, 0);
846         CFS_INIT_LIST_HEAD(&imp->imp_conn_list);
847         CFS_INIT_LIST_HEAD(&imp->imp_handle.h_link);
848         class_handle_hash(&imp->imp_handle, import_handle_addref);
849
850         /* the default magic is V1, will be used in connect RPC, and
851          * then adjusted according to the flags in request/reply. */
852         imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V1;
853
854         return imp;
855 }
856 EXPORT_SYMBOL(class_new_import);
857
858 void class_destroy_import(struct obd_import *import)
859 {
860         LASSERT(import != NULL);
861         LASSERT(import != LP_POISON);
862
863         class_handle_unhash(&import->imp_handle);
864
865         spin_lock(&import->imp_lock);
866         import->imp_generation++;
867         spin_unlock(&import->imp_lock);
868         class_import_put(import);
869 }
870 EXPORT_SYMBOL(class_destroy_import);
871
872 /* A connection defines an export context in which preallocation can
873    be managed. This releases the export pointer reference, and returns
874    the export handle, so the export refcount is 1 when this function
875    returns. */
876 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
877                   struct obd_uuid *cluuid)
878 {
879         struct obd_export *export;
880         LASSERT(conn != NULL);
881         LASSERT(obd != NULL);
882         LASSERT(cluuid != NULL);
883         ENTRY;
884
885         export = class_new_export(obd, cluuid);
886         if (IS_ERR(export))
887                 RETURN(PTR_ERR(export));
888
889         conn->cookie = export->exp_handle.h_cookie;
890         class_export_put(export);
891
892         CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
893                cluuid->uuid, conn->cookie);
894         RETURN(0);
895 }
896 EXPORT_SYMBOL(class_connect);
897
898 /* if export is involved in recovery then clean up related things */
899 void class_export_recovery_cleanup(struct obd_export *exp)
900 {
901         struct obd_device *obd = exp->exp_obd;
902
903         spin_lock_bh(&obd->obd_processing_task_lock);
904         if (obd->obd_recovering && exp->exp_in_recovery) {
905                 spin_lock(&exp->exp_lock);
906                 exp->exp_in_recovery = 0;
907                 spin_unlock(&exp->exp_lock);
908                 obd->obd_connected_clients--;
909                 /* each connected client is counted as recoverable */
910                 obd->obd_recoverable_clients--;
911                 if (exp->exp_req_replay_needed) {
912                         spin_lock(&exp->exp_lock);
913                         exp->exp_req_replay_needed = 0;
914                         spin_unlock(&exp->exp_lock);
915                         LASSERT(atomic_read(&obd->obd_req_replay_clients));
916                         atomic_dec(&obd->obd_req_replay_clients);
917                 }
918                 if (exp->exp_lock_replay_needed) {
919                         spin_lock(&exp->exp_lock);
920                         exp->exp_lock_replay_needed = 0;
921                         spin_unlock(&exp->exp_lock);
922                         LASSERT(atomic_read(&obd->obd_lock_replay_clients));
923                         atomic_dec(&obd->obd_lock_replay_clients);                
924                 }
925         }
926         spin_unlock_bh(&obd->obd_processing_task_lock);
927 }
928
929 /* This function removes two references from the export: one for the
930  * hash entry and one for the export pointer passed in.  The export
931  * pointer passed to this function is destroyed should not be used
932  * again. */
933 int class_disconnect(struct obd_export *export)
934 {
935         int already_disconnected;
936         ENTRY;
937
938         if (export == NULL) {
939                 fixme();
940                 CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
941                 RETURN(-EINVAL);
942         }
943
944         spin_lock(&export->exp_lock);
945         already_disconnected = export->exp_disconnected;
946         export->exp_disconnected = 1;
947         spin_unlock(&export->exp_lock);
948
949         /* class_cleanup(), abort_recovery(), and class_fail_export()
950          * all end up in here, and if any of them race we shouldn't
951          * call extra class_export_puts(). */
952         if (already_disconnected)
953                 RETURN(0);
954
955         CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
956                export->exp_handle.h_cookie);
957
958         class_export_recovery_cleanup(export);
959         class_unlink_export(export);
960         class_export_put(export);
961         RETURN(0);
962 }
963
964 static void class_disconnect_export_list(struct list_head *list, int flags)
965 {
966         int rc;
967         struct lustre_handle fake_conn;
968         struct obd_export *fake_exp, *exp;
969         ENTRY;
970
971         /* It's possible that an export may disconnect itself, but
972          * nothing else will be added to this list. */
973         while (!list_empty(list)) {
974                 exp = list_entry(list->next, struct obd_export, exp_obd_chain);
975                 class_export_get(exp);
976
977                 spin_lock(&exp->exp_lock);
978                 exp->exp_flags = flags;
979                 spin_unlock(&exp->exp_lock);
980
981                 if (obd_uuid_equals(&exp->exp_client_uuid,
982                                     &exp->exp_obd->obd_uuid)) {
983                         CDEBUG(D_HA,
984                                "exp %p export uuid == obd uuid, don't discon\n",
985                                exp);
986                         /* Need to delete this now so we don't end up pointing
987                          * to work_list later when this export is cleaned up. */
988                         list_del_init(&exp->exp_obd_chain);
989                         class_export_put(exp);
990                         continue;
991                 }
992
993                 fake_conn.cookie = exp->exp_handle.h_cookie;
994                 fake_exp = class_conn2export(&fake_conn);
995                 if (!fake_exp) {
996                         class_export_put(exp);
997                         continue;
998                 }
999
1000                 spin_lock(&fake_exp->exp_lock);
1001                 fake_exp->exp_flags = flags;
1002                 spin_unlock(&fake_exp->exp_lock);
1003
1004                 rc = obd_disconnect(fake_exp);
1005                 class_export_put(exp);
1006                 if (rc) {
1007                         CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
1008                                exp, rc);
1009                 } else {
1010                         CDEBUG(D_HA, "export %p disconnected\n", exp);
1011                 }
1012         }
1013         EXIT;
1014 }
1015
1016 static inline int get_exp_flags_from_obd(struct obd_device *obd)
1017 {
1018         return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
1019                 (obd->obd_force ? OBD_OPT_FORCE : 0));
1020 }
1021
1022 void class_disconnect_exports(struct obd_device *obd)
1023 {
1024         struct list_head work_list;
1025         ENTRY;
1026
1027         /* Move all of the exports from obd_exports to a work list, en masse. */
1028         spin_lock(&obd->obd_dev_lock);
1029         list_add(&work_list, &obd->obd_exports);
1030         list_del_init(&obd->obd_exports);
1031         spin_unlock(&obd->obd_dev_lock);
1032         
1033         if (!list_empty(&work_list)) {
1034                 CDEBUG(D_HA, "OBD device %d (%p) has exports, "
1035                        "disconnecting them\n", obd->obd_minor, obd);
1036                 class_disconnect_export_list(&work_list, 
1037                                              get_exp_flags_from_obd(obd));
1038         } else
1039                 CDEBUG(D_HA, "OBD device %d (%p) has no exports\n",
1040                        obd->obd_minor, obd);
1041         EXIT;
1042 }
1043 EXPORT_SYMBOL(class_disconnect_exports);
1044
1045 /* Remove exports that have not completed recovery.
1046  */
1047 int class_disconnect_stale_exports(struct obd_device *obd,
1048                                    int (*test_export)(struct obd_export *))
1049 {
1050         struct list_head work_list;
1051         struct list_head *pos, *n;
1052         struct obd_export *exp;
1053         int cnt = 0;
1054         ENTRY;
1055
1056         CFS_INIT_LIST_HEAD(&work_list);
1057         spin_lock(&obd->obd_dev_lock);
1058         list_for_each_safe(pos, n, &obd->obd_exports) {
1059                 exp = list_entry(pos, struct obd_export, exp_obd_chain);
1060                 if (test_export(exp))
1061                         continue;
1062                 
1063                 list_del(&exp->exp_obd_chain);
1064                 list_add(&exp->exp_obd_chain, &work_list);
1065                 /* don't count self-export as client */
1066                 if (obd_uuid_equals(&exp->exp_client_uuid,
1067                                      &exp->exp_obd->obd_uuid))
1068                         continue;
1069
1070                 cnt++;
1071                 CDEBUG(D_ERROR, "%s: disconnect stale client %s@%s\n",
1072                        obd->obd_name, exp->exp_client_uuid.uuid,
1073                        exp->exp_connection == NULL ? "<unknown>" :
1074                        libcfs_nid2str(exp->exp_connection->c_peer.nid));
1075         }
1076         spin_unlock(&obd->obd_dev_lock);
1077
1078         CDEBUG(D_ERROR, "%s: disconnecting %d stale clients\n",
1079                obd->obd_name, cnt);
1080         class_disconnect_export_list(&work_list, get_exp_flags_from_obd(obd));
1081         RETURN(cnt);
1082 }
1083 EXPORT_SYMBOL(class_disconnect_stale_exports);
1084
1085 int oig_init(struct obd_io_group **oig_out)
1086 {
1087         struct obd_io_group *oig;
1088         ENTRY;
1089
1090         OBD_ALLOC(oig, sizeof(*oig));
1091         if (oig == NULL)
1092                 RETURN(-ENOMEM);
1093
1094         spin_lock_init(&oig->oig_lock);
1095         oig->oig_rc = 0;
1096         oig->oig_pending = 0;
1097         atomic_set(&oig->oig_refcount, 1);
1098         cfs_waitq_init(&oig->oig_waitq);
1099         CFS_INIT_LIST_HEAD(&oig->oig_occ_list);
1100
1101         *oig_out = oig;
1102         RETURN(0);
1103 };
1104 EXPORT_SYMBOL(oig_init);
1105
1106 static inline void oig_grab(struct obd_io_group *oig)
1107 {
1108         atomic_inc(&oig->oig_refcount);
1109 }
1110
1111 void oig_release(struct obd_io_group *oig)
1112 {
1113         if (atomic_dec_and_test(&oig->oig_refcount))
1114                 OBD_FREE(oig, sizeof(*oig));
1115 }
1116 EXPORT_SYMBOL(oig_release);
1117
1118 int oig_add_one(struct obd_io_group *oig, struct oig_callback_context *occ)
1119 {
1120         int rc = 0;
1121         CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
1122         spin_lock(&oig->oig_lock);
1123         if (oig->oig_rc) {
1124                 rc = oig->oig_rc;
1125         } else {
1126                 oig->oig_pending++;
1127                 if (occ != NULL)
1128                         list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
1129         }
1130         spin_unlock(&oig->oig_lock);
1131         oig_grab(oig);
1132
1133         return rc;
1134 }
1135 EXPORT_SYMBOL(oig_add_one);
1136
1137 void oig_complete_one(struct obd_io_group *oig,
1138                       struct oig_callback_context *occ, int rc)
1139 {
1140         cfs_waitq_t *wake = NULL;
1141         int old_rc;
1142
1143         spin_lock(&oig->oig_lock);
1144
1145         if (occ != NULL)
1146                 list_del_init(&occ->occ_oig_item);
1147
1148         old_rc = oig->oig_rc;
1149         if (oig->oig_rc == 0 && rc != 0)
1150                 oig->oig_rc = rc;
1151
1152         if (--oig->oig_pending <= 0)
1153                 wake = &oig->oig_waitq;
1154
1155         spin_unlock(&oig->oig_lock);
1156
1157         CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
1158                         "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
1159                         oig->oig_pending);
1160         if (wake)
1161                 cfs_waitq_signal(wake);
1162         oig_release(oig);
1163 }
1164 EXPORT_SYMBOL(oig_complete_one);
1165
1166 static int oig_done(struct obd_io_group *oig)
1167 {
1168         int rc = 0;
1169         spin_lock(&oig->oig_lock);
1170         if (oig->oig_pending <= 0)
1171                 rc = 1;
1172         spin_unlock(&oig->oig_lock);
1173         return rc;
1174 }
1175
1176 static void interrupted_oig(void *data)
1177 {
1178         struct obd_io_group *oig = data;
1179         struct oig_callback_context *occ;
1180
1181         spin_lock(&oig->oig_lock);
1182         /* We need to restart the processing each time we drop the lock, as
1183          * it is possible other threads called oig_complete_one() to remove
1184          * an entry elsewhere in the list while we dropped lock.  We need to
1185          * drop the lock because osc_ap_completion() calls oig_complete_one()
1186          * which re-gets this lock ;-) as well as a lock ordering issue. */
1187 restart:
1188         list_for_each_entry(occ, &oig->oig_occ_list, occ_oig_item) {
1189                 if (occ->interrupted)
1190                         continue;
1191                 occ->interrupted = 1;
1192                 spin_unlock(&oig->oig_lock);
1193                 occ->occ_interrupted(occ);
1194                 spin_lock(&oig->oig_lock);
1195                 goto restart;
1196         }
1197         spin_unlock(&oig->oig_lock);
1198 }
1199
1200 int oig_wait(struct obd_io_group *oig)
1201 {
1202         struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
1203         int rc;
1204
1205         CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
1206
1207         do {
1208                 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
1209                 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
1210                 /* we can't continue until the oig has emptied and stopped
1211                  * referencing state that the caller will free upon return */
1212                 if (rc == -EINTR)
1213                         lwi = (struct l_wait_info){ 0, };
1214         } while (rc == -EINTR);
1215
1216         LASSERTF(oig->oig_pending == 0,
1217                  "exiting oig_wait(oig = %p) with %d pending\n", oig,
1218                  oig->oig_pending);
1219
1220         CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);
1221         return oig->oig_rc;
1222 }
1223 EXPORT_SYMBOL(oig_wait);
1224
1225 void class_fail_export(struct obd_export *exp)
1226 {
1227         int rc, already_failed;
1228
1229         spin_lock(&exp->exp_lock);
1230         already_failed = exp->exp_failed;
1231         exp->exp_failed = 1;
1232         spin_unlock(&exp->exp_lock);
1233
1234         if (already_failed) {
1235                 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
1236                        exp, exp->exp_client_uuid.uuid);
1237                 return;
1238         }
1239
1240         CDEBUG(D_HA, "disconnecting export %p/%s\n",
1241                exp, exp->exp_client_uuid.uuid);
1242
1243         if (obd_dump_on_timeout)
1244                 libcfs_debug_dumplog();
1245
1246         /* Most callers into obd_disconnect are removing their own reference
1247          * (request, for example) in addition to the one from the hash table.
1248          * We don't have such a reference here, so make one. */
1249         class_export_get(exp);
1250         rc = obd_disconnect(exp);
1251         if (rc)
1252                 CERROR("disconnecting export %p failed: %d\n", exp, rc);
1253         else
1254                 CDEBUG(D_HA, "disconnected export %p/%s\n",
1255                        exp, exp->exp_client_uuid.uuid);
1256 }
1257 EXPORT_SYMBOL(class_fail_export);
1258
1259 char *obd_export_nid2str(struct obd_export *exp)
1260 {
1261         if (exp->exp_connection != NULL)
1262                 return libcfs_nid2str(exp->exp_connection->c_peer.nid);
1263
1264         return "(no nid)";
1265 }
1266 EXPORT_SYMBOL(obd_export_nid2str);
1267
1268 #define EVICT_BATCH 32
1269 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid)
1270 {
1271         struct obd_export *doomed_exp[EVICT_BATCH] = { NULL };
1272         struct list_head *p;
1273         int exports_evicted = 0, num_to_evict = 0, i;
1274
1275 search_again:
1276         spin_lock(&obd->obd_dev_lock);
1277         list_for_each(p, &obd->obd_exports) {
1278                 doomed_exp[num_to_evict] = list_entry(p, struct obd_export,
1279                                                       exp_obd_chain);
1280                 if (strcmp(obd_export_nid2str(doomed_exp[num_to_evict]),
1281                            nid) == 0) {
1282                         class_export_get(doomed_exp[num_to_evict]);
1283                         if (++num_to_evict == EVICT_BATCH)
1284                                 break;
1285                 }
1286         }
1287         spin_unlock(&obd->obd_dev_lock);
1288
1289         for (i = 0; i < num_to_evict; i++) {
1290                 exports_evicted++;
1291                 CWARN("%s: evict NID '%s' (%s) #%d at adminstrative request\n",
1292                        obd->obd_name, nid, doomed_exp[i]->exp_client_uuid.uuid,
1293                        exports_evicted);
1294                 class_fail_export(doomed_exp[i]);
1295                 class_export_put(doomed_exp[i]);
1296         }
1297         if (num_to_evict == EVICT_BATCH) {
1298                 num_to_evict = 0;
1299                 goto search_again;
1300         }
1301
1302         if (!exports_evicted)
1303                 CDEBUG(D_HA,"%s: can't disconnect NID '%s': no exports found\n",
1304                        obd->obd_name, nid);
1305         return exports_evicted;
1306 }
1307 EXPORT_SYMBOL(obd_export_evict_by_nid);
1308
1309 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid)
1310 {
1311         struct obd_export *doomed_exp = NULL;
1312         struct list_head *p;
1313         struct obd_uuid doomed;
1314         int exports_evicted = 0;
1315
1316         obd_str2uuid(&doomed, uuid);
1317
1318         spin_lock(&obd->obd_dev_lock);
1319         list_for_each(p, &obd->obd_exports) {
1320                 doomed_exp = list_entry(p, struct obd_export, exp_obd_chain);
1321
1322                 if (obd_uuid_equals(&doomed, &doomed_exp->exp_client_uuid)) {
1323                         class_export_get(doomed_exp);
1324                         break;
1325                 }
1326                 doomed_exp = NULL;
1327         }
1328         spin_unlock(&obd->obd_dev_lock);
1329
1330         if (doomed_exp == NULL) {
1331                 CERROR("%s: can't disconnect %s: no exports found\n",
1332                        obd->obd_name, uuid);
1333         } else {
1334                 CWARN("%s: evicting %s at adminstrative request\n",
1335                        obd->obd_name, doomed_exp->exp_client_uuid.uuid);
1336                 class_fail_export(doomed_exp);
1337                 class_export_put(doomed_exp);
1338                 exports_evicted++;
1339         }
1340
1341         return exports_evicted;
1342 }
1343 EXPORT_SYMBOL(obd_export_evict_by_uuid);
1344
1345 void obd_zombie_impexp_cull(void) 
1346 {
1347         struct obd_import *import;
1348         struct obd_export *export;
1349         ENTRY;
1350         
1351         do {
1352                 spin_lock (&obd_zombie_impexp_lock);
1353
1354                 import = NULL;
1355                 if (!list_empty(&obd_zombie_imports)) {
1356                         import = list_entry(obd_zombie_imports.next,
1357                                             struct obd_import,
1358                                             imp_zombie_chain);
1359                         list_del(&import->imp_zombie_chain);
1360                 }
1361                 
1362                 export = NULL;
1363                 if (!list_empty(&obd_zombie_exports)) {
1364                         export = list_entry(obd_zombie_exports.next,
1365                                             struct obd_export,
1366                                             exp_obd_chain);
1367                         list_del_init(&export->exp_obd_chain);
1368                 }
1369
1370                 spin_unlock(&obd_zombie_impexp_lock);
1371                 
1372                 if (import != NULL)
1373                         class_import_destroy(import);
1374
1375                 if (export != NULL)
1376                         class_export_destroy(export);
1377
1378         } while (import != NULL || export != NULL);
1379         EXIT;
1380 }
1381 EXPORT_SYMBOL(obd_zombie_impexp_cull);
1382
1383 void obd_zombie_impexp_init(void)
1384 {
1385         INIT_LIST_HEAD(&obd_zombie_imports);
1386         INIT_LIST_HEAD(&obd_zombie_exports);
1387         spin_lock_init(&obd_zombie_impexp_lock);
1388 }
1389