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