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