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