Whamcloud - gitweb
landing b_cmobd_merge on 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 Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * These are the only exported functions, they provide some generic
22  * infrastructure for managing object devices
23  */
24
25 #define DEBUG_SUBSYSTEM S_CLASS
26 #ifdef __KERNEL__
27 #include <linux/kmod.h>   /* for request_module() */
28 #include <linux/module.h>
29 #include <linux/obd_class.h>
30 #include <linux/random.h>
31 #include <linux/slab.h>
32 #include <linux/pagemap.h>
33 #else
34 #include <liblustre.h>
35 #include <linux/obd_class.h>
36 #include <linux/obd.h>
37 #endif
38 #include <linux/lprocfs_status.h>
39
40 extern struct list_head obd_types;
41 static spinlock_t obd_types_lock = SPIN_LOCK_UNLOCKED;
42 kmem_cache_t *obdo_cachep = NULL;
43 kmem_cache_t *import_cachep = NULL;
44
45 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
46 void (*ptlrpc_abort_inflight_superhack)(struct obd_import *imp);
47
48 /*
49  * support functions: we could use inter-module communication, but this
50  * is more portable to other OS's
51  */
52 static struct obd_type *class_search_type(char *name)
53 {
54         struct list_head *tmp;
55         struct obd_type *type;
56
57         spin_lock(&obd_types_lock);
58         list_for_each(tmp, &obd_types) {
59                 type = list_entry(tmp, struct obd_type, typ_chain);
60                 if (strlen(type->typ_name) == strlen(name) &&
61                     strcmp(type->typ_name, name) == 0) {
62                         spin_unlock(&obd_types_lock);
63                         return type;
64                 }
65         }
66         spin_unlock(&obd_types_lock);
67         return NULL;
68 }
69
70 struct obd_type *class_get_type(char *name)
71 {
72         struct obd_type *type = class_search_type(name);
73
74 #ifdef CONFIG_KMOD
75         if (!type) {
76                 if (!request_module(name)) {
77                         CDEBUG(D_INFO, "Loaded module '%s'\n", name);
78                         type = class_search_type(name);
79                 } else
80                         CDEBUG(D_INFO, "Can't load module '%s'\n", name);
81         }
82 #endif
83         if (type)
84                 try_module_get(type->typ_ops->o_owner);
85         return type;
86 }
87
88 void class_put_type(struct obd_type *type)
89 {
90         LASSERT(type);
91         module_put(type->typ_ops->o_owner);
92 }
93
94 int class_register_type(struct obd_ops *ops, struct md_ops *md_ops,
95                         struct lprocfs_vars *vars, char *name)
96 {
97         struct obd_type *type;
98         int rc = 0;
99         ENTRY;
100
101         LASSERT(strnlen(name, 1024) < 1024);    /* sanity check */
102
103         if (class_search_type(name)) {
104                 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
105                 RETURN(-EEXIST);
106         }
107
108         rc = -ENOMEM;
109         OBD_ALLOC(type, sizeof(*type));
110         if (type == NULL)
111                 RETURN(rc);
112
113         OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
114         OBD_ALLOC(type->typ_name, strlen(name) + 1);
115         if (md_ops)
116                 OBD_ALLOC(type->typ_md_ops, sizeof(*type->typ_md_ops));
117         if (type->typ_ops == NULL || type->typ_name == NULL ||
118                         (md_ops && type->typ_md_ops == NULL))
119                 GOTO (failed, rc);
120
121         *(type->typ_ops) = *ops;
122         if (md_ops)
123                 *(type->typ_md_ops) = *md_ops;
124         else
125                 type->typ_md_ops = NULL;
126         strcpy(type->typ_name, name);
127
128 #ifdef LPROCFS
129         type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
130                                               vars, type);
131 #endif
132         if (IS_ERR(type->typ_procroot)) {
133                 rc = PTR_ERR(type->typ_procroot);
134                 type->typ_procroot = NULL;
135                 GOTO (failed, rc);
136         }
137
138         spin_lock(&obd_types_lock);
139         list_add(&type->typ_chain, &obd_types);
140         spin_unlock(&obd_types_lock);
141
142         RETURN (0);
143
144  failed:
145         if (type->typ_name != NULL)
146                 OBD_FREE(type->typ_name, strlen(name) + 1);
147         if (type->typ_ops != NULL)
148                 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
149         if (type->typ_md_ops != NULL)
150                 OBD_FREE (type->typ_md_ops, sizeof (*type->typ_md_ops));
151         OBD_FREE(type, sizeof(*type));
152         RETURN(rc);
153 }
154
155 int class_unregister_type(char *name)
156 {
157         struct obd_type *type = class_search_type(name);
158         ENTRY;
159
160         if (!type) {
161                 CERROR("unknown obd type\n");
162                 RETURN(-EINVAL);
163         }
164
165         if (type->typ_refcnt) {
166                 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
167                 /* This is a bad situation, let's make the best of it */
168                 /* Remove ops, but leave the name for debugging */
169                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
170                 RETURN(-EBUSY);
171         }
172
173         if (type->typ_procroot) {
174                 lprocfs_remove(type->typ_procroot);
175                 type->typ_procroot = NULL;
176         }
177
178         spin_lock(&obd_types_lock);
179         list_del(&type->typ_chain);
180         spin_unlock(&obd_types_lock);
181         OBD_FREE(type->typ_name, strlen(name) + 1);
182         if (type->typ_ops != NULL)
183                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
184         if (type->typ_md_ops != NULL)
185                 OBD_FREE (type->typ_md_ops, sizeof (*type->typ_md_ops));
186         OBD_FREE(type, sizeof(*type));
187         RETURN(0);
188 } /* class_unregister_type */
189
190 struct obd_device *class_newdev(int *dev)
191 {
192         struct obd_device *result = NULL;
193         int i;
194
195         for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
196                 struct obd_device *obd = &obd_dev[i];
197                 if (!obd->obd_type) {
198                         result = obd;
199                         if (dev)
200                                 *dev = i;
201                         break;
202                 }
203         }
204         return result;
205 }
206
207 int class_name2dev(char *name)
208 {
209         int i;
210
211         if (!name)
212                 return -1;
213
214         for (i = 0; i < MAX_OBD_DEVICES; i++) {
215                 struct obd_device *obd = &obd_dev[i];
216                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0)
217                         return i;
218         }
219
220         return -1;
221 }
222
223 struct obd_device *class_name2obd(char *name)
224 {
225         int dev = class_name2dev(name);
226         if (dev < 0)
227                 return NULL;
228         return &obd_dev[dev];
229 }
230
231 int class_uuid2dev(struct obd_uuid *uuid)
232 {
233         int i;
234
235         for (i = 0; i < MAX_OBD_DEVICES; i++) {
236                 struct obd_device *obd = &obd_dev[i];
237                 if (obd_uuid_equals(uuid, &obd->obd_uuid))
238                         return i;
239         }
240
241         return -1;
242 }
243
244 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
245 {
246         int dev = class_uuid2dev(uuid);
247         if (dev < 0)
248                 return NULL;
249         return &obd_dev[dev];
250 }
251
252 /* Search for a client OBD connected to tgt_uuid.  If grp_uuid is
253    specified, then only the client with that uuid is returned,
254    otherwise any client connected to the tgt is returned.
255    If tgt_uuid is NULL, the lov with grp_uuid is returned. */
256 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
257                                           char * typ_name,
258                                           struct obd_uuid *grp_uuid)
259 {
260         int i;
261
262         for (i = 0; i < MAX_OBD_DEVICES; i++) {
263                 struct obd_device *obd = &obd_dev[i];
264                 if (obd->obd_type == NULL)
265                         continue;
266                 if ((strncmp(obd->obd_type->typ_name, typ_name,
267                              strlen(typ_name)) == 0)) {
268                         struct client_obd *cli = &obd->u.cli;
269                         struct obd_import *imp = cli->cl_import;
270                         if (tgt_uuid == NULL) {
271                                 LASSERT(grp_uuid);
272                                 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid))
273                                         return obd;
274                                 continue;
275                         }
276                         if (obd_uuid_equals(tgt_uuid, &imp->imp_target_uuid) &&
277                             ((grp_uuid)? obd_uuid_equals(grp_uuid,
278                                                          &obd->obd_uuid) : 1)) {
279                                 return obd;
280                         }
281                 }
282         }
283
284         return NULL;
285 }
286
287 /* Iterate the obd_device list looking devices have grp_uuid. Start
288    searching at *next, and if a device is found, the next index to look
289    it is saved in *next. If next is NULL, then the first matching device
290    will always be returned. */
291 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
292 {
293         int i;
294         if (next == NULL) 
295                 i = 0;
296         else if (*next >= 0 && *next < MAX_OBD_DEVICES)
297                 i = *next;
298         else 
299                 return NULL;
300                 
301         for (; i < MAX_OBD_DEVICES; i++) {
302                 struct obd_device *obd = &obd_dev[i];
303                 if (obd->obd_type == NULL)
304                         continue;
305                 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
306                         if (next != NULL)
307                                 *next = i+1;
308                         return obd;
309                 }
310         }
311
312         return NULL;
313 }
314
315
316 void obd_cleanup_caches(void)
317 {
318         int rc;
319         ENTRY;
320         if (obdo_cachep) {
321                 rc = kmem_cache_destroy(obdo_cachep);
322                 if (rc)
323                         CERROR("Cannot destory ll_obdo_cache\n");
324                 obdo_cachep = NULL;
325         }
326         if (import_cachep) {
327                 rc = kmem_cache_destroy(import_cachep);
328                 if (rc)
329                         CERROR("Cannot destory ll_import_cache\n");
330                 import_cachep = NULL;
331         }
332         EXIT;
333 }
334
335 int obd_init_caches(void)
336 {
337         ENTRY;
338         LASSERT(obdo_cachep == NULL);
339         obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
340                                         0, 0, NULL, NULL);
341         if (!obdo_cachep)
342                 GOTO(out, -ENOMEM);
343
344         LASSERT(import_cachep == NULL);
345         import_cachep = kmem_cache_create("ll_import_cache",
346                                           sizeof(struct obd_import),
347                                           0, 0, NULL, NULL);
348         if (!import_cachep)
349                 GOTO(out, -ENOMEM);
350
351         RETURN(0);
352  out:
353         obd_cleanup_caches();
354         RETURN(-ENOMEM);
355
356 }
357
358 /* map connection to client */
359 struct obd_export *class_conn2export(struct lustre_handle *conn)
360 {
361         struct obd_export *export;
362         ENTRY;
363
364         if (!conn) {
365                 CDEBUG(D_CACHE, "looking for null handle\n");
366                 RETURN(NULL);
367         }
368
369         if (conn->cookie == -1) {  /* this means assign a new connection */
370                 CDEBUG(D_CACHE, "want a new connection\n");
371                 RETURN(NULL);
372         }
373
374         CDEBUG(D_IOCTL, "looking for export cookie "LPX64"\n", conn->cookie);
375         export = class_handle2object(conn->cookie);
376         RETURN(export);
377 }
378
379 struct obd_device *class_exp2obd(struct obd_export *exp)
380 {
381         if (exp)
382                 return exp->exp_obd;
383         return NULL;
384 }
385
386 struct obd_device *class_conn2obd(struct lustre_handle *conn)
387 {
388         struct obd_export *export;
389         export = class_conn2export(conn);
390         if (export) {
391                 struct obd_device *obd = export->exp_obd;
392                 class_export_put(export);
393                 return obd;
394         }
395         return NULL;
396 }
397
398 struct obd_import *class_exp2cliimp(struct obd_export *exp)
399 {
400         struct obd_device *obd = exp->exp_obd;
401         if (obd == NULL)
402                 return NULL;
403         return obd->u.cli.cl_import;
404 }
405
406 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
407 {
408         struct obd_device *obd = class_conn2obd(conn);
409         if (obd == NULL)
410                 return NULL;
411         return obd->u.cli.cl_import;
412 }
413
414 /* Export management functions */
415 static void export_handle_addref(void *export)
416 {
417         class_export_get(export);
418 }
419
420 void __class_export_put(struct obd_export *exp)
421 {
422         if (atomic_dec_and_test(&exp->exp_refcount)) {
423                 struct obd_device *obd = exp->exp_obd;
424                 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
425                        exp->exp_client_uuid.uuid);
426
427                 LASSERT(obd != NULL);
428
429                 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
430                 if (exp->exp_connection)
431                         ptlrpc_put_connection_superhack(exp->exp_connection);
432
433                 LASSERT(list_empty(&exp->exp_outstanding_replies));
434                 LASSERT(list_empty(&exp->exp_handle.h_link));
435                 obd_destroy_export(exp);
436
437                 OBD_FREE(exp, sizeof(*exp));
438                 if (obd->obd_set_up) {
439                         atomic_dec(&obd->obd_refcount);
440                         wake_up(&obd->obd_refcount_waitq);
441                 }
442         }
443 }
444
445 /* Creates a new export, adds it to the hash table, and returns a
446  * pointer to it. The refcount is 2: one for the hash reference, and
447  * one for the pointer returned by this function. */
448 struct obd_export *class_new_export(struct obd_device *obd)
449 {
450         struct obd_export *export;
451
452         OBD_ALLOC(export, sizeof(*export));
453         if (!export) {
454                 CERROR("no memory! (minor %d)\n", obd->obd_minor);
455                 return NULL;
456         }
457
458         export->exp_conn_cnt = 0;
459         atomic_set(&export->exp_refcount, 2);
460         export->exp_obd = obd;
461         INIT_LIST_HEAD(&export->exp_outstanding_replies);
462         /* XXX this should be in LDLM init */
463         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
464
465         INIT_LIST_HEAD(&export->exp_handle.h_link);
466         class_handle_hash(&export->exp_handle, export_handle_addref);
467         spin_lock_init(&export->exp_lock);
468
469         spin_lock(&obd->obd_dev_lock);
470         LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
471         atomic_inc(&obd->obd_refcount);
472         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
473         export->exp_obd->obd_num_exports++;
474         spin_unlock(&obd->obd_dev_lock);
475         obd_init_export(export);
476         return export;
477 }
478
479 void class_unlink_export(struct obd_export *exp)
480 {
481         class_handle_unhash(&exp->exp_handle);
482
483         spin_lock(&exp->exp_obd->obd_dev_lock);
484         list_del_init(&exp->exp_obd_chain);
485         exp->exp_obd->obd_num_exports--;
486         spin_unlock(&exp->exp_obd->obd_dev_lock);
487
488         class_export_put(exp);
489 }
490
491 /* Import management functions */
492 static void import_handle_addref(void *import)
493 {
494         class_import_get(import);
495 }
496
497 struct obd_import *class_import_get(struct obd_import *import)
498 {
499         atomic_inc(&import->imp_refcount);
500         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
501                atomic_read(&import->imp_refcount));
502         return import;
503 }
504
505 void class_import_put(struct obd_import *import)
506 {
507         ENTRY;
508
509         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
510                atomic_read(&import->imp_refcount) - 1);
511
512         LASSERT(atomic_read(&import->imp_refcount) > 0);
513         LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
514         if (!atomic_dec_and_test(&import->imp_refcount)) {
515                 EXIT;
516                 return;
517         }
518
519         CDEBUG(D_IOCTL, "destroying import %p\n", import);
520
521         ptlrpc_put_connection_superhack(import->imp_connection);
522
523         LASSERT(list_empty(&import->imp_handle.h_link));
524         OBD_FREE(import, sizeof(*import));
525         EXIT;
526 }
527
528 struct obd_import *class_new_import(void)
529 {
530         struct obd_import *imp;
531
532         OBD_ALLOC(imp, sizeof(*imp));
533         if (imp == NULL)
534                 return NULL;
535
536         INIT_LIST_HEAD(&imp->imp_replay_list);
537         INIT_LIST_HEAD(&imp->imp_sending_list);
538         INIT_LIST_HEAD(&imp->imp_delayed_list);
539         spin_lock_init(&imp->imp_lock);
540         imp->imp_conn_cnt = 0;
541         imp->imp_max_transno = 0;
542         imp->imp_peer_committed_transno = 0;
543         imp->imp_state = LUSTRE_IMP_NEW;
544         init_waitqueue_head(&imp->imp_recovery_waitq);
545
546         atomic_set(&imp->imp_refcount, 2);
547         atomic_set(&imp->imp_inflight, 0);
548         atomic_set(&imp->imp_replay_inflight, 0);
549         INIT_LIST_HEAD(&imp->imp_handle.h_link);
550         class_handle_hash(&imp->imp_handle, import_handle_addref);
551
552         return imp;
553 }
554
555 void class_destroy_import(struct obd_import *import)
556 {
557         LASSERT(import != NULL);
558         LASSERT(import != LP_POISON);
559
560         class_handle_unhash(&import->imp_handle);
561
562         /* Abort any inflight DLM requests and NULL out their (about to be
563          * freed) import. */
564         /* Invalidate all requests on import, would be better to call
565            ptlrpc_set_import_active(imp, 0); */
566         import->imp_generation++;
567         ptlrpc_abort_inflight_superhack(import);
568
569         class_import_put(import);
570 }
571
572 /* A connection defines an export context in which preallocation can
573    be managed. This releases the export pointer reference, and returns
574    the export handle, so the export refcount is 1 when this function
575    returns. */
576 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
577                   struct obd_uuid *cluuid)
578 {
579         struct obd_export *export;
580         LASSERT(conn != NULL);
581         LASSERT(obd != NULL);
582         LASSERT(cluuid != NULL);
583         ENTRY;
584
585         export = class_new_export(obd);
586         if (export == NULL)
587                 RETURN(-ENOMEM);
588
589         conn->cookie = export->exp_handle.h_cookie;
590         memcpy(&export->exp_client_uuid, cluuid,
591                sizeof(export->exp_client_uuid));
592         class_export_put(export);
593
594         CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
595                cluuid->uuid, conn->cookie);
596         RETURN(0);
597 }
598
599 /* This function removes two references from the export: one for the
600  * hash entry and one for the export pointer passed in.  The export
601  * pointer passed to this function is destroyed should not be used
602  * again. */
603 int class_disconnect(struct obd_export *export, int flags)
604 {
605         ENTRY;
606
607         if (export == NULL) {
608                 fixme();
609                 CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
610                 RETURN(-EINVAL);
611         }
612
613         /* XXX this shouldn't have to be here, but double-disconnect will crash
614          * otherwise, and sometimes double-disconnect happens.  abort_recovery,
615          * for example. */
616         if (list_empty(&export->exp_handle.h_link))
617                 RETURN(0);
618
619         CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
620                export->exp_handle.h_cookie);
621
622         class_unlink_export(export);
623         class_export_put(export);
624         RETURN(0);
625 }
626
627 void class_disconnect_exports(struct obd_device *obd, int flags)
628 {
629         int rc;
630         struct list_head *tmp, *n, work_list;
631         struct lustre_handle fake_conn;
632         struct obd_export *fake_exp, *exp;
633         ENTRY;
634
635         /* Move all of the exports from obd_exports to a work list, en masse. */
636         spin_lock(&obd->obd_dev_lock);
637         list_add(&work_list, &obd->obd_exports);
638         list_del_init(&obd->obd_exports);
639         spin_unlock(&obd->obd_dev_lock);
640
641         CDEBUG(D_HA, "OBD device %d (%p) has exports, "
642                "disconnecting them\n", obd->obd_minor, obd);
643         list_for_each_safe(tmp, n, &work_list) {
644                 exp = list_entry(tmp, struct obd_export, exp_obd_chain);
645                 class_export_get(exp);
646
647                 if (obd_uuid_equals(&exp->exp_client_uuid,
648                                     &exp->exp_obd->obd_uuid)) {
649                         CDEBUG(D_HA,
650                                "exp %p export uuid == obd uuid, don't discon\n",
651                                exp);
652                         /* Need to delete this now so we don't end up pointing
653                          * to work_list later when this export is cleaned up. */
654                         list_del_init(&exp->exp_obd_chain);
655                         class_export_put(exp);
656                         continue;
657                 }
658
659                 fake_conn.cookie = exp->exp_handle.h_cookie;
660                 fake_exp = class_conn2export(&fake_conn);
661                 if (!fake_exp) {
662                         class_export_put(exp);
663                         continue;
664                 }
665                 rc = obd_disconnect(fake_exp, flags);
666                 class_export_put(exp);
667                 if (rc) {
668                         CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
669                                exp, rc);
670                 } else {
671                         CDEBUG(D_HA, "export %p disconnected\n", exp);
672                 }
673         }
674         EXIT;
675 }
676
677 int oig_init(struct obd_io_group **oig_out)
678 {
679         struct obd_io_group *oig;
680         ENTRY;
681
682         OBD_ALLOC(oig, sizeof(*oig));
683         if (oig == NULL)
684                 RETURN(-ENOMEM);
685
686         spin_lock_init(&oig->oig_lock);
687         oig->oig_rc = 0;
688         oig->oig_pending = 0;
689         atomic_set(&oig->oig_refcount, 1);
690         init_waitqueue_head(&oig->oig_waitq);
691         INIT_LIST_HEAD(&oig->oig_occ_list);
692
693         *oig_out = oig;
694         RETURN(0);
695 };
696
697 static inline void oig_grab(struct obd_io_group *oig)
698 {
699         atomic_inc(&oig->oig_refcount);
700 }
701 void oig_release(struct obd_io_group *oig)
702 {
703         if (atomic_dec_and_test(&oig->oig_refcount))
704                 OBD_FREE(oig, sizeof(*oig));
705 }
706
707 void oig_add_one(struct obd_io_group *oig,
708                   struct oig_callback_context *occ)
709 {
710         unsigned long flags;
711         CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
712         spin_lock_irqsave(&oig->oig_lock, flags);
713         oig->oig_pending++;
714         if (occ != NULL)
715                 list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
716         spin_unlock_irqrestore(&oig->oig_lock, flags);
717         oig_grab(oig);
718 }
719
720 void oig_complete_one(struct obd_io_group *oig,
721                       struct oig_callback_context *occ, int rc)
722 {
723         unsigned long flags;
724         wait_queue_head_t *wake = NULL;
725         int old_rc;
726
727         spin_lock_irqsave(&oig->oig_lock, flags);
728
729         if (occ != NULL)
730                 list_del_init(&occ->occ_oig_item);
731
732         old_rc = oig->oig_rc;
733         if (oig->oig_rc == 0 && rc != 0)
734                 oig->oig_rc = rc;
735
736         if (--oig->oig_pending <= 0)
737                 wake = &oig->oig_waitq;
738
739         spin_unlock_irqrestore(&oig->oig_lock, flags);
740
741         CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
742                         "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
743                         oig->oig_pending);
744         if (wake)
745                 wake_up(wake);
746         oig_release(oig);
747 }
748
749 static int oig_done(struct obd_io_group *oig)
750 {
751         unsigned long flags;
752         int rc = 0;
753         spin_lock_irqsave(&oig->oig_lock, flags);
754         if (oig->oig_pending <= 0)
755                 rc = 1;
756         spin_unlock_irqrestore(&oig->oig_lock, flags);
757         return rc;
758 }
759
760 static void interrupted_oig(void *data)
761 {
762         struct obd_io_group *oig = data;
763         struct list_head *pos;
764         struct oig_callback_context *occ;
765         unsigned long flags;
766
767         spin_lock_irqsave(&oig->oig_lock, flags);
768         list_for_each(pos, &oig->oig_occ_list) {
769                 occ = list_entry(pos, struct oig_callback_context,
770                                  occ_oig_item);
771                 occ->occ_interrupted(occ);
772         }
773         spin_unlock_irqrestore(&oig->oig_lock, flags);
774 }
775
776 int oig_wait(struct obd_io_group *oig)
777 {
778         struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
779         int rc;
780
781         CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
782
783         do {
784                 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
785                 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
786                 /* we can't continue until the oig has emptied and stopped
787                  * referencing state that the caller will free upon return */
788                 if (rc == -EINTR)
789                         lwi = (struct l_wait_info){ 0, };
790         } while (rc == -EINTR);
791
792         LASSERTF(oig->oig_pending == 0,
793                  "exiting oig_wait(oig = %p) with %d pending\n", oig,
794                  oig->oig_pending);
795
796         CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);
797         return oig->oig_rc;
798 }