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