Whamcloud - gitweb
Landing b_bug974 onto HEAD (20040213_1538).
[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 lprocfs_vars *vars,
95                         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 (type->typ_ops == NULL || type->typ_name == NULL)
116                 GOTO (failed, rc);
117
118         *(type->typ_ops) = *ops;
119         strcpy(type->typ_name, name);
120
121 #ifdef LPROCFS
122         type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
123                                               vars, type);
124 #endif
125         if (IS_ERR(type->typ_procroot)) {
126                 rc = PTR_ERR(type->typ_procroot);
127                 type->typ_procroot = NULL;
128                 GOTO (failed, rc);
129         }
130
131         spin_lock(&obd_types_lock);
132         list_add(&type->typ_chain, &obd_types);
133         spin_unlock(&obd_types_lock);
134
135         RETURN (0);
136
137  failed:
138         if (type->typ_name != NULL)
139                 OBD_FREE(type->typ_name, strlen(name) + 1);
140         if (type->typ_ops != NULL)
141                 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
142         OBD_FREE(type, sizeof(*type));
143         RETURN(rc);
144 }
145
146 int class_unregister_type(char *name)
147 {
148         struct obd_type *type = class_search_type(name);
149         ENTRY;
150
151         if (!type) {
152                 CERROR("unknown obd type\n");
153                 RETURN(-EINVAL);
154         }
155
156         if (type->typ_refcnt) {
157                 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
158                 /* This is a bad situation, let's make the best of it */
159                 /* Remove ops, but leave the name for debugging */
160                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
161                 RETURN(-EBUSY);
162         }
163
164         if (type->typ_procroot) {
165                 lprocfs_remove(type->typ_procroot);
166                 type->typ_procroot = NULL;
167         }
168
169         spin_lock(&obd_types_lock);
170         list_del(&type->typ_chain);
171         spin_unlock(&obd_types_lock);
172         OBD_FREE(type->typ_name, strlen(name) + 1);
173         if (type->typ_ops != NULL)
174                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
175         OBD_FREE(type, sizeof(*type));
176         RETURN(0);
177 } /* class_unregister_type */
178
179 struct obd_device *class_newdev(int *dev)
180 {
181         struct obd_device *result = NULL;
182         int i;
183
184         for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
185                 struct obd_device *obd = &obd_dev[i];
186                 if (!obd->obd_type) {
187                         result = obd;
188                         if (dev)
189                                 *dev = i;
190                         break;
191                 }
192         }
193         return result;
194 }
195
196 int class_name2dev(char *name)
197 {
198         int i;
199
200         if (!name)
201                 return -1;
202
203         for (i = 0; i < MAX_OBD_DEVICES; i++) {
204                 struct obd_device *obd = &obd_dev[i];
205                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0)
206                         return i;
207         }
208
209         return -1;
210 }
211
212 struct obd_device *class_name2obd(char *name)
213 {
214         int dev = class_name2dev(name);
215         if (dev < 0)
216                 return NULL;
217         return &obd_dev[dev];
218 }
219
220 int class_uuid2dev(struct obd_uuid *uuid)
221 {
222         int i;
223
224         for (i = 0; i < MAX_OBD_DEVICES; i++) {
225                 struct obd_device *obd = &obd_dev[i];
226                 if (obd_uuid_equals(uuid, &obd->obd_uuid))
227                         return i;
228         }
229
230         return -1;
231 }
232
233 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
234 {
235         int dev = class_uuid2dev(uuid);
236         if (dev < 0)
237                 return NULL;
238         return &obd_dev[dev];
239 }
240
241 /* Search for a client OBD connected to tgt_uuid.  If grp_uuid is
242    specified, then only the client with that uuid is returned,
243    otherwise any client connected to the tgt is returned. */
244 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
245                                           char * typ_name,
246                                           struct obd_uuid *grp_uuid)
247 {
248         int i;
249
250         for (i = 0; i < MAX_OBD_DEVICES; i++) {
251                 struct obd_device *obd = &obd_dev[i];
252                 if (obd->obd_type == NULL)
253                         continue;
254                 if ((strncmp(obd->obd_type->typ_name, typ_name,
255                              strlen(typ_name)) == 0)) {
256                         struct client_obd *cli = &obd->u.cli;
257                         struct obd_import *imp = cli->cl_import;
258                         if (obd_uuid_equals(tgt_uuid, &imp->imp_target_uuid) &&
259                             ((grp_uuid)? obd_uuid_equals(grp_uuid,
260                                                          &obd->obd_uuid) : 1)) {
261                                 return obd;
262                         }
263                 }
264         }
265
266         return NULL;
267 }
268
269 /* Iterate the obd_device list looking devices have grp_uuid. Start
270    searching at *next, and if a device is found, the next index to look
271    it is saved in *next. If next is NULL, then the first matching device
272    will always be returned. */
273 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
274 {
275         int i;
276         if (next == NULL) 
277                 i = 0;
278         else if (*next >= 0 && *next < MAX_OBD_DEVICES)
279                 i = *next;
280         else 
281                 return NULL;
282                 
283         for (; i < MAX_OBD_DEVICES; i++) {
284                 struct obd_device *obd = &obd_dev[i];
285                 if (obd->obd_type == NULL)
286                         continue;
287                 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
288                         if (next != NULL)
289                                 *next = i+1;
290                         return obd;
291                 }
292         }
293
294         return NULL;
295 }
296
297
298 void obd_cleanup_caches(void)
299 {
300         int rc;
301         ENTRY;
302         if (obdo_cachep) {
303                 rc = kmem_cache_destroy(obdo_cachep);
304                 if (rc)
305                         CERROR("Cannot destory ll_obdo_cache\n");
306                 obdo_cachep = NULL;
307         }
308         if (import_cachep) {
309                 rc = kmem_cache_destroy(import_cachep);
310                 if (rc)
311                         CERROR("Cannot destory ll_import_cache\n");
312                 import_cachep = NULL;
313         }
314         EXIT;
315 }
316
317 int obd_init_caches(void)
318 {
319         ENTRY;
320         LASSERT(obdo_cachep == NULL);
321         obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
322                                         0, 0, NULL, NULL);
323         if (!obdo_cachep)
324                 GOTO(out, -ENOMEM);
325
326         LASSERT(import_cachep == NULL);
327         import_cachep = kmem_cache_create("ll_import_cache",
328                                           sizeof(struct obd_import),
329                                           0, 0, NULL, NULL);
330         if (!import_cachep)
331                 GOTO(out, -ENOMEM);
332
333         RETURN(0);
334  out:
335         obd_cleanup_caches();
336         RETURN(-ENOMEM);
337
338 }
339
340 /* map connection to client */
341 struct obd_export *class_conn2export(struct lustre_handle *conn)
342 {
343         struct obd_export *export;
344         ENTRY;
345
346         if (!conn) {
347                 CDEBUG(D_CACHE, "looking for null handle\n");
348                 RETURN(NULL);
349         }
350
351         if (conn->cookie == -1) {  /* this means assign a new connection */
352                 CDEBUG(D_CACHE, "want a new connection\n");
353                 RETURN(NULL);
354         }
355
356         CDEBUG(D_IOCTL, "looking for export cookie "LPX64"\n", conn->cookie);
357         export = class_handle2object(conn->cookie);
358         RETURN(export);
359 }
360
361 struct obd_device *class_exp2obd(struct obd_export *exp)
362 {
363         if (exp)
364                 return exp->exp_obd;
365         return NULL;
366 }
367
368 struct obd_device *class_conn2obd(struct lustre_handle *conn)
369 {
370         struct obd_export *export;
371         export = class_conn2export(conn);
372         if (export) {
373                 struct obd_device *obd = export->exp_obd;
374                 class_export_put(export);
375                 return obd;
376         }
377         return NULL;
378 }
379
380 struct obd_import *class_exp2cliimp(struct obd_export *exp)
381 {
382         struct obd_device *obd = exp->exp_obd;
383         if (obd == NULL)
384                 return NULL;
385         return obd->u.cli.cl_import;
386 }
387
388 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
389 {
390         struct obd_device *obd = class_conn2obd(conn);
391         if (obd == NULL)
392                 return NULL;
393         return obd->u.cli.cl_import;
394 }
395
396
397 /* Export management functions */
398 static void export_handle_addref(void *export)
399 {
400         class_export_get(export);
401 }
402
403 void __class_export_put(struct obd_export *exp)
404 {
405         if (atomic_dec_and_test(&exp->exp_refcount)) {
406                 struct obd_device *obd = exp->exp_obd;
407                 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
408                        exp->exp_client_uuid.uuid);
409
410                 LASSERT(obd != NULL);
411
412                 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
413                 if (exp->exp_connection)
414                         ptlrpc_put_connection_superhack(exp->exp_connection);
415
416                 LASSERT(list_empty(&exp->exp_handle.h_link));
417                 obd_destroy_export(exp);
418
419                 OBD_FREE(exp, sizeof(*exp));
420                 if (obd->obd_set_up) {
421                         atomic_dec(&obd->obd_refcount);
422                         wake_up(&obd->obd_refcount_waitq);
423                 }
424         }
425 }
426
427 /* Creates a new export, adds it to the hash table, and returns a
428  * pointer to it. The refcount is 2: one for the hash reference, and
429  * one for the pointer returned by this function. */
430 struct obd_export *class_new_export(struct obd_device *obd)
431 {
432         struct obd_export *export;
433
434         OBD_ALLOC(export, sizeof(*export));
435         if (!export) {
436                 CERROR("no memory! (minor %d)\n", obd->obd_minor);
437                 return NULL;
438         }
439
440         export->exp_conn_cnt = 0;
441         atomic_set(&export->exp_refcount, 2);
442         export->exp_obd = obd;
443         /* XXX this should be in LDLM init */
444         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
445
446         INIT_LIST_HEAD(&export->exp_handle.h_link);
447         class_handle_hash(&export->exp_handle, export_handle_addref);
448         spin_lock_init(&export->exp_lock);
449
450         spin_lock(&obd->obd_dev_lock);
451         LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
452         atomic_inc(&obd->obd_refcount);
453         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
454         export->exp_obd->obd_num_exports++;
455         spin_unlock(&obd->obd_dev_lock);
456         obd_init_export(export);
457         return export;
458 }
459
460 void class_unlink_export(struct obd_export *exp)
461 {
462         class_handle_unhash(&exp->exp_handle);
463
464         spin_lock(&exp->exp_obd->obd_dev_lock);
465         list_del_init(&exp->exp_obd_chain);
466         exp->exp_obd->obd_num_exports--;
467         spin_unlock(&exp->exp_obd->obd_dev_lock);
468
469         class_export_put(exp);
470 }
471
472 /* Import management functions */
473 static void import_handle_addref(void *import)
474 {
475         class_import_get(import);
476 }
477
478 struct obd_import *class_import_get(struct obd_import *import)
479 {
480         atomic_inc(&import->imp_refcount);
481         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
482                atomic_read(&import->imp_refcount));
483         return import;
484 }
485
486 void class_import_put(struct obd_import *import)
487 {
488         ENTRY;
489
490         CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
491                atomic_read(&import->imp_refcount) - 1);
492
493         LASSERT(atomic_read(&import->imp_refcount) > 0);
494         LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
495         if (!atomic_dec_and_test(&import->imp_refcount)) {
496                 EXIT;
497                 return;
498         }
499
500         CDEBUG(D_IOCTL, "destroying import %p\n", import);
501
502         ptlrpc_put_connection_superhack(import->imp_connection);
503
504         LASSERT(list_empty(&import->imp_handle.h_link));
505         OBD_FREE(import, sizeof(*import));
506         EXIT;
507 }
508
509 struct obd_import *class_new_import(void)
510 {
511         struct obd_import *imp;
512
513         OBD_ALLOC(imp, sizeof(*imp));
514         if (imp == NULL)
515                 return NULL;
516
517         INIT_LIST_HEAD(&imp->imp_replay_list);
518         INIT_LIST_HEAD(&imp->imp_sending_list);
519         INIT_LIST_HEAD(&imp->imp_delayed_list);
520         spin_lock_init(&imp->imp_lock);
521         imp->imp_conn_cnt = 0;
522         imp->imp_max_transno = 0;
523         imp->imp_peer_committed_transno = 0;
524         imp->imp_state = LUSTRE_IMP_NEW;
525         init_waitqueue_head(&imp->imp_recovery_waitq);
526
527         atomic_set(&imp->imp_refcount, 2);
528         atomic_set(&imp->imp_replay_inflight, 0);
529         INIT_LIST_HEAD(&imp->imp_handle.h_link);
530         class_handle_hash(&imp->imp_handle, import_handle_addref);
531
532         return imp;
533 }
534
535 void class_destroy_import(struct obd_import *import)
536 {
537         LASSERT(import != NULL);
538         LASSERT((unsigned long)import != 0x5a5a5a5a);
539
540         class_handle_unhash(&import->imp_handle);
541
542         /* Abort any inflight DLM requests and NULL out their (about to be
543          * freed) import. */
544         /* Invalidate all requests on import, would be better to call
545            ptlrpc_set_import_active(imp, 0); */
546         import->imp_generation++;
547         ptlrpc_abort_inflight_superhack(import);
548
549         class_import_put(import);
550 }
551
552 /* A connection defines an export context in which preallocation can
553    be managed. This releases the export pointer reference, and returns
554    the export handle, so the export refcount is 1 when this function
555    returns. */
556 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
557                   struct obd_uuid *cluuid)
558 {
559         struct obd_export *export;
560         LASSERT(conn != NULL);
561         LASSERT(obd != NULL);
562         LASSERT(cluuid != NULL);
563         ENTRY;
564
565         export = class_new_export(obd);
566         if (export == NULL)
567                 RETURN(-ENOMEM);
568
569         conn->cookie = export->exp_handle.h_cookie;
570         memcpy(&export->exp_client_uuid, cluuid,
571                sizeof(export->exp_client_uuid));
572         class_export_put(export);
573
574         CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
575                cluuid->uuid, conn->cookie);
576         RETURN(0);
577 }
578
579 /* This function removes two references from the export: one for the
580  * hash entry and one for the export pointer passed in.  The export
581  * pointer passed to this function is destroyed should not be used
582  * again. */
583 int class_disconnect(struct obd_export *export, int flags)
584 {
585         ENTRY;
586
587         if (export == NULL) {
588                 fixme();
589                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
590                        "null export %p\n", export);
591                 RETURN(-EINVAL);
592         }
593
594         /* XXX this shouldn't have to be here, but double-disconnect will crash
595          * otherwise, and sometimes double-disconnect happens.  abort_recovery,
596          * for example. */
597         if (list_empty(&export->exp_handle.h_link))
598                 RETURN(0);
599
600         CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
601                export->exp_handle.h_cookie);
602
603         class_unlink_export(export);
604         class_export_put(export);
605         RETURN(0);
606 }
607
608 void class_disconnect_exports(struct obd_device *obd, int flags)
609 {
610         int rc;
611         struct list_head *tmp, *n, work_list;
612         struct lustre_handle fake_conn;
613         struct obd_export *fake_exp, *exp;
614         ENTRY;
615
616         /* Move all of the exports from obd_exports to a work list, en masse. */
617         spin_lock(&obd->obd_dev_lock);
618         list_add(&work_list, &obd->obd_exports);
619         list_del_init(&obd->obd_exports);
620         spin_unlock(&obd->obd_dev_lock);
621
622         CDEBUG(D_HA, "OBD device %d (%p) has exports, "
623                "disconnecting them\n", obd->obd_minor, obd);
624         list_for_each_safe(tmp, n, &work_list) {
625                 exp = list_entry(tmp, struct obd_export, exp_obd_chain);
626                 class_export_get(exp);
627
628                 if (obd_uuid_equals(&exp->exp_client_uuid,
629                                     &exp->exp_obd->obd_uuid)) {
630                         CDEBUG(D_HA,
631                                "exp %p export uuid == obd uuid, don't discon\n",
632                                exp);
633                         class_export_put(exp);
634                         continue;
635                 }
636
637                 fake_conn.cookie = exp->exp_handle.h_cookie;
638                 fake_exp = class_conn2export(&fake_conn);
639                 if (!fake_exp) {
640                         class_export_put(exp);
641                         continue;
642                 }
643                 rc = obd_disconnect(fake_exp, flags);
644                 class_export_put(exp);
645                 if (rc) {
646                         CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
647                                exp, rc);
648                 } else {
649                         CDEBUG(D_HA, "export %p disconnected\n", exp);
650                 }
651         }
652         EXIT;
653 }
654
655 void osic_init(struct obd_sync_io_container **osic_out)
656 {
657         struct obd_sync_io_container *osic;
658         OBD_ALLOC(osic, sizeof(*osic));
659         spin_lock_init(&osic->osic_lock);
660         osic->osic_rc = 0;
661         osic->osic_pending = 0;
662         atomic_set(&osic->osic_refcount, 1);
663         init_waitqueue_head(&osic->osic_waitq);
664         INIT_LIST_HEAD(&osic->osic_occ_list);
665         *osic_out = osic;
666 };
667
668 static inline void osic_grab(struct obd_sync_io_container *osic)
669 {
670         atomic_inc(&osic->osic_refcount);
671 }
672 void osic_release(struct obd_sync_io_container *osic)
673 {
674         if (atomic_dec_and_test(&osic->osic_refcount))
675                 OBD_FREE(osic, sizeof(*osic));
676 }
677
678 void osic_add_one(struct obd_sync_io_container *osic,
679                   struct osic_callback_context *occ)
680 {
681         unsigned long flags;
682         CDEBUG(D_CACHE, "osic %p ready to roll\n", osic);
683         spin_lock_irqsave(&osic->osic_lock, flags);
684         osic->osic_pending++;
685         if (occ != NULL)
686                 list_add_tail(&occ->occ_osic_item, &osic->osic_occ_list);
687         spin_unlock_irqrestore(&osic->osic_lock, flags);
688         osic_grab(osic);
689 }
690
691 void osic_complete_one(struct obd_sync_io_container *osic,
692                        struct osic_callback_context *occ, int rc)
693 {
694         unsigned long flags;
695         wait_queue_head_t *wake = NULL;
696         int old_rc;
697
698         spin_lock_irqsave(&osic->osic_lock, flags);
699
700         if (occ != NULL)
701                 list_del_init(&occ->occ_osic_item);
702
703         old_rc = osic->osic_rc;
704         if (osic->osic_rc == 0 && rc != 0)
705                 osic->osic_rc = rc;
706
707         if (--osic->osic_pending <= 0)
708                 wake = &osic->osic_waitq;
709
710         spin_unlock_irqrestore(&osic->osic_lock, flags);
711
712         CDEBUG(D_CACHE, "osic %p completed, rc %d -> %d via %d, %d now "
713                         "pending (racey)\n", osic, old_rc, osic->osic_rc, rc,
714                         osic->osic_pending);
715         if (wake)
716                 wake_up(wake);
717         osic_release(osic);
718 }
719
720 static int osic_done(struct obd_sync_io_container *osic)
721 {
722         unsigned long flags;
723         int rc = 0;
724         spin_lock_irqsave(&osic->osic_lock, flags);
725         if (osic->osic_pending <= 0)
726                 rc = 1;
727         spin_unlock_irqrestore(&osic->osic_lock, flags);
728         return rc;
729 }
730
731 static void interrupted_osic(void *data)
732 {
733         struct obd_sync_io_container *osic = data;
734         struct list_head *pos;
735         struct osic_callback_context *occ;
736         unsigned long flags;
737
738         spin_lock_irqsave(&osic->osic_lock, flags);
739         list_for_each(pos, &osic->osic_occ_list) {
740                 occ = list_entry(pos, struct osic_callback_context,
741                                  occ_osic_item);
742                 occ->occ_interrupted(occ);
743         }
744         spin_unlock_irqrestore(&osic->osic_lock, flags);
745 }
746
747 int osic_wait(struct obd_sync_io_container *osic)
748 {
749         struct l_wait_info lwi = LWI_INTR(interrupted_osic, osic);
750         int rc;
751
752         CDEBUG(D_CACHE, "waiting for osic %p\n", osic);
753
754         do {
755                 rc = l_wait_event(osic->osic_waitq, osic_done(osic), &lwi);
756                 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
757                 /* we can't continue until the osic has emptied and stopped
758                  * referencing state that the caller will free upon return */
759                 if (rc == -EINTR)
760                         lwi = (struct l_wait_info){ 0, };
761         } while (rc == -EINTR);
762
763         LASSERTF(osic->osic_pending == 0,
764                  "exiting osic_wait(osic = %p) with %d pending\n", osic,
765                  osic->osic_pending);
766
767         CDEBUG(D_CACHE, "done waiting on osic %p rc %d\n", osic, osic->osic_rc);
768         return osic->osic_rc;
769 }