Whamcloud - gitweb
fix build problem on Cray XT3 machines. See bz-13315
[fs/lustre-release.git] / lustre / ptlrpc / events.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 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  */
25
26 #define DEBUG_SUBSYSTEM S_RPC
27
28 #ifndef __KERNEL__
29 # include <liblustre.h>
30 #else
31 # ifdef __mips64__
32 #  include <linux/kernel.h>
33 # endif
34 #endif
35 #include <obd_class.h>
36 #include <lustre_net.h>
37 #include "ptlrpc_internal.h"
38
39 lnet_handle_eq_t   ptlrpc_eq_h;
40
41 /*  
42  *  Client's outgoing request callback
43  */
44 void request_out_callback(lnet_event_t *ev)
45 {
46         struct ptlrpc_cb_id   *cbid = ev->md.user_ptr;
47         struct ptlrpc_request *req = cbid->cbid_arg;
48         ENTRY;
49
50         LASSERT (ev->type == LNET_EVENT_SEND ||
51                  ev->type == LNET_EVENT_UNLINK);
52         LASSERT (ev->unlinked);
53
54         DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
55                   "type %d, status %d", ev->type, ev->status);
56
57         if (ev->type == LNET_EVENT_UNLINK || ev->status != 0) {
58
59                 /* Failed send: make it seem like the reply timed out, just
60                  * like failing sends in client.c does currently...  */
61
62                 spin_lock(&req->rq_lock);
63                 req->rq_net_err = 1;
64                 spin_unlock(&req->rq_lock);
65
66                 ptlrpc_wake_client_req(req);
67         }
68
69         /* these balance the references in ptl_send_rpc() */
70         atomic_dec(&req->rq_import->imp_inflight);
71         ptlrpc_req_finished(req);
72
73         EXIT;
74 }
75
76 /*
77  * Client's incoming reply callback
78  */
79 void reply_in_callback(lnet_event_t *ev)
80 {
81         struct ptlrpc_cb_id   *cbid = ev->md.user_ptr;
82         struct ptlrpc_request *req = cbid->cbid_arg;
83         ENTRY;
84
85         DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
86                   "type %d, status %d", ev->type, ev->status);
87         
88         LASSERT(ev->type == LNET_EVENT_PUT || ev->type == LNET_EVENT_UNLINK);
89         LASSERT(ev->md.start == req->rq_repbuf);
90         LASSERT(ev->mlength <= req->rq_replen);
91
92         spin_lock(&req->rq_lock);
93
94         req->rq_receiving_reply = 0;
95         req->rq_early = 0;
96         
97         if (ev->status)
98                 goto out_wake;
99         if (ev->type == LNET_EVENT_UNLINK) {
100                 req->rq_must_unlink = 0;
101                 DEBUG_REQ(D_RPCTRACE, req, "unlink");
102                 goto out_wake;
103         }
104
105         /* We've set LNET_MD_MANAGE_REMOTE for all outgoing requests
106            for adaptive timeouts' early reply. */
107         LASSERT(ev->md.options & LNET_MD_MANAGE_REMOTE);
108
109         if ((ev->offset == 0) && 
110             lustre_msg_get_flags(req->rq_reqmsg) & MSG_AT_SUPPORT) {
111                 /* Early reply */
112                 DEBUG_REQ(D_ADAPTTO, req,
113                           "Early reply received: mlen=%u offset=%d replen=%d "
114                           "replied=%d", ev->mlength, ev->offset, req->rq_replen,
115                           req->rq_replied);
116                 
117                 LASSERT(ev->mlength == lustre_msg_early_size());
118                 req->rq_early_count++; /* number received, client side */
119                 if (req->rq_replied) 
120                         /* If we already got the real reply, ignore the early 
121                            reply, but signal so we can unlink */
122                         goto out_wake;
123                 req->rq_early = 1;
124                 req->rq_nob_received = ev->mlength;
125                 /* repmsg points to early reply */
126                 req->rq_repmsg = req->rq_repbuf;
127                 /* And we're still receiving */
128                 req->rq_receiving_reply = 1;
129         } else {
130                 /* Real reply */
131                 req->rq_replied = 1;
132                 req->rq_nob_received = ev->mlength;
133                 /* repmsg points to real reply */
134                 req->rq_repmsg = (struct lustre_msg *)((char *)req->rq_repbuf +
135                                                        ev->offset);
136                 /* LNetMDUnlink can't be called under the LNET_LOCK,
137                    so we must unlink in ptlrpc_unregister_reply */
138                 DEBUG_REQ(D_INFO, req, 
139                           "reply in flags=%x mlen=%u offset=%d replen=%d",
140                           lustre_msg_get_flags(req->rq_reqmsg),
141                           ev->mlength, ev->offset, req->rq_replen);
142         }
143
144         req->rq_import->imp_last_reply_time = cfs_time_current_sec();
145
146 out_wake:
147         /* NB don't unlock till after wakeup; req can disappear under us
148          * since we don't have our own ref */
149         ptlrpc_wake_client_req(req);
150         spin_unlock(&req->rq_lock);
151         EXIT;
152 }
153
154 /* 
155  * Client's bulk has been written/read
156  */
157 void client_bulk_callback (lnet_event_t *ev)
158 {
159         struct ptlrpc_cb_id     *cbid = ev->md.user_ptr;
160         struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
161         ENTRY;
162
163         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_CLIENT_BULK_CB))
164                 ev->status = -EIO;
165
166         LASSERT ((desc->bd_type == BULK_PUT_SINK && 
167                   ev->type == LNET_EVENT_PUT) ||
168                  (desc->bd_type == BULK_GET_SOURCE &&
169                   ev->type == LNET_EVENT_GET) ||
170                  ev->type == LNET_EVENT_UNLINK);
171         LASSERT (ev->unlinked);
172
173         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
174                "event type %d, status %d, desc %p\n", 
175                ev->type, ev->status, desc);
176
177         spin_lock(&desc->bd_lock);
178
179         LASSERT(desc->bd_network_rw);
180         desc->bd_network_rw = 0;
181
182         if (ev->type != LNET_EVENT_UNLINK && ev->status == 0) {
183                 desc->bd_success = 1;
184                 desc->bd_nob_transferred = ev->mlength;
185                 desc->bd_sender = ev->sender;
186         }
187
188         /* NB don't unlock till after wakeup; desc can disappear under us
189          * otherwise */
190         ptlrpc_wake_client_req(desc->bd_req);
191
192         spin_unlock(&desc->bd_lock);
193         EXIT;
194 }
195
196 /* 
197  * Server's incoming request callback
198  */
199 void request_in_callback(lnet_event_t *ev)
200 {
201         struct ptlrpc_cb_id               *cbid = ev->md.user_ptr;
202         struct ptlrpc_request_buffer_desc *rqbd = cbid->cbid_arg;
203         struct ptlrpc_service             *service = rqbd->rqbd_service;
204         struct ptlrpc_request             *req;
205         ENTRY;
206
207         LASSERT (ev->type == LNET_EVENT_PUT ||
208                  ev->type == LNET_EVENT_UNLINK);
209         LASSERT ((char *)ev->md.start >= rqbd->rqbd_buffer);
210         LASSERT ((char *)ev->md.start + ev->offset + ev->mlength <=
211                  rqbd->rqbd_buffer + service->srv_buf_size);
212
213         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
214                "event type %d, status %d, service %s\n", 
215                ev->type, ev->status, service->srv_name);
216
217         if (ev->unlinked) {
218                 /* If this is the last request message to fit in the
219                  * request buffer we can use the request object embedded in
220                  * rqbd.  Note that if we failed to allocate a request,
221                  * we'd have to re-post the rqbd, which we can't do in this
222                  * context. */
223                 req = &rqbd->rqbd_req;
224                 memset(req, 0, sizeof (*req));
225         } else {
226                 LASSERT (ev->type == LNET_EVENT_PUT);
227                 if (ev->status != 0) {
228                         /* We moaned above already... */
229                         return;
230                 }
231                 OBD_ALLOC_GFP(req, sizeof(*req), CFS_ALLOC_ATOMIC_TRY);
232                 if (req == NULL) {
233                         CERROR("Can't allocate incoming request descriptor: "
234                                "Dropping %s RPC from %s\n",
235                                service->srv_name, 
236                                libcfs_id2str(ev->initiator));
237                         return;
238                 }
239         }
240
241         /* NB we ABSOLUTELY RELY on req being zeroed, so pointers are NULL,
242          * flags are reset and scalars are zero.  We only set the message
243          * size to non-zero if this was a successful receive. */
244         req->rq_xid = ev->match_bits;
245         req->rq_reqmsg = ev->md.start + ev->offset;
246         if (ev->type == LNET_EVENT_PUT && ev->status == 0)
247                 req->rq_reqlen = ev->mlength;
248         do_gettimeofday(&req->rq_arrival_time);
249         req->rq_peer = ev->initiator;
250         req->rq_self = ev->target.nid;
251         req->rq_rqbd = rqbd;
252         req->rq_phase = RQ_PHASE_NEW;
253 #ifdef CRAY_XT3
254         req->rq_uid = ev->uid;
255 #endif
256         spin_lock_init(&req->rq_lock);
257         sema_init(&req->rq_rs_sem, 1);
258         CFS_INIT_LIST_HEAD(&req->rq_timed_list);
259         atomic_set(&req->rq_refcount, 1);
260         if (ev->type == LNET_EVENT_PUT)
261                 DEBUG_REQ(D_RPCTRACE, req, "incoming req");
262
263         spin_lock(&service->srv_lock);
264
265         req->rq_history_seq = service->srv_request_seq++;
266         list_add_tail(&req->rq_history_list, &service->srv_request_history);
267
268         if (ev->unlinked) {
269                 service->srv_nrqbd_receiving--;
270                 CDEBUG(D_INFO, "Buffer complete: %d buffers still posted\n",
271                        service->srv_nrqbd_receiving);
272
273                 /* Normally, don't complain about 0 buffers posted; LNET won't
274                  * drop incoming reqs since we set the portal lazy */
275                 if (test_req_buffer_pressure &&
276                     ev->type != LNET_EVENT_UNLINK &&
277                     service->srv_nrqbd_receiving == 0)
278                         CWARN("All %s request buffers busy\n",
279                               service->srv_name);
280
281                 /* req takes over the network's ref on rqbd */
282         } else {
283                 /* req takes a ref on rqbd */
284                 rqbd->rqbd_refcount++;
285         }
286
287         list_add_tail(&req->rq_list, &service->srv_req_in_queue);
288         service->srv_n_queued_reqs++;
289
290         /* NB everything can disappear under us once the request
291          * has been queued and we unlock, so do the wake now... */
292         cfs_waitq_signal(&service->srv_waitq);
293
294         spin_unlock(&service->srv_lock);
295         EXIT;
296 }
297
298 /*
299  *  Server's outgoing reply callback
300  */
301 void reply_out_callback(lnet_event_t *ev)
302 {
303         struct ptlrpc_cb_id       *cbid = ev->md.user_ptr;
304         struct ptlrpc_reply_state *rs = cbid->cbid_arg;
305         struct ptlrpc_service     *svc = rs->rs_service;
306         ENTRY;
307
308         LASSERT (ev->type == LNET_EVENT_SEND ||
309                  ev->type == LNET_EVENT_ACK ||
310                  ev->type == LNET_EVENT_UNLINK);
311
312         if (!rs->rs_difficult) {
313                 /* 'Easy' replies have no further processing so I drop the
314                  * net's ref on 'rs' */
315                 LASSERT (ev->unlinked);
316                 ptlrpc_rs_decref(rs);
317                 atomic_dec (&svc->srv_outstanding_replies);
318                 EXIT;
319                 return;
320         }
321
322         LASSERT (rs->rs_on_net);
323
324         if (ev->unlinked) {
325                 /* Last network callback.  The net's ref on 'rs' stays put
326                  * until ptlrpc_server_handle_reply() is done with it */
327                 spin_lock(&svc->srv_lock);
328                 rs->rs_on_net = 0;
329                 ptlrpc_schedule_difficult_reply (rs);
330                 spin_unlock(&svc->srv_lock);
331         }
332
333         EXIT;
334 }
335
336 /*
337  * Server's bulk completion callback
338  */
339 void server_bulk_callback (lnet_event_t *ev)
340 {
341         struct ptlrpc_cb_id     *cbid = ev->md.user_ptr;
342         struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
343         ENTRY;
344
345         LASSERT (ev->type == LNET_EVENT_SEND ||
346                  ev->type == LNET_EVENT_UNLINK ||
347                  (desc->bd_type == BULK_PUT_SOURCE &&
348                   ev->type == LNET_EVENT_ACK) ||
349                  (desc->bd_type == BULK_GET_SINK &&
350                   ev->type == LNET_EVENT_REPLY));
351
352         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
353                "event type %d, status %d, desc %p\n", 
354                ev->type, ev->status, desc);
355
356         spin_lock(&desc->bd_lock);
357         
358         if ((ev->type == LNET_EVENT_ACK ||
359              ev->type == LNET_EVENT_REPLY) &&
360             ev->status == 0) {
361                 /* We heard back from the peer, so even if we get this
362                  * before the SENT event (oh yes we can), we know we
363                  * read/wrote the peer buffer and how much... */
364                 desc->bd_success = 1;
365                 desc->bd_nob_transferred = ev->mlength;
366                 desc->bd_sender = ev->sender;
367         }
368
369         if (ev->unlinked) {
370                 /* This is the last callback no matter what... */
371                 desc->bd_network_rw = 0;
372                 cfs_waitq_signal(&desc->bd_waitq);
373         }
374
375         spin_unlock(&desc->bd_lock);
376         EXIT;
377 }
378
379 static void ptlrpc_master_callback(lnet_event_t *ev)
380 {
381         struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
382         void (*callback)(lnet_event_t *ev) = cbid->cbid_fn;
383
384         /* Honestly, it's best to find out early. */
385         LASSERT (cbid->cbid_arg != LP_POISON);
386         LASSERT (callback == request_out_callback ||
387                  callback == reply_in_callback ||
388                  callback == client_bulk_callback ||
389                  callback == request_in_callback ||
390                  callback == reply_out_callback ||
391                  callback == server_bulk_callback);
392         
393         callback (ev);
394 }
395
396 int ptlrpc_uuid_to_peer (struct obd_uuid *uuid, 
397                          lnet_process_id_t *peer, lnet_nid_t *self)
398 {
399         int               best_dist = 0;
400         int               best_order = 0;
401         int               count = 0;
402         int               rc = -ENOENT;
403         int               portals_compatibility;
404         int               dist;
405         int               order;
406         lnet_nid_t        dst_nid;
407         lnet_nid_t        src_nid;
408
409         portals_compatibility = LNetCtl(IOC_LIBCFS_PORTALS_COMPATIBILITY, NULL);
410
411         peer->pid = LUSTRE_SRV_LNET_PID;
412
413         /* Choose the matching UUID that's closest */
414         while (lustre_uuid_to_peer(uuid->uuid, &dst_nid, count++) == 0) {
415                 dist = LNetDist(dst_nid, &src_nid, &order);
416                 if (dist < 0)
417                         continue;
418
419                 if (dist == 0) {                /* local! use loopback LND */
420                         peer->nid = *self = LNET_MKNID(LNET_MKNET(LOLND, 0), 0);
421                         rc = 0;
422                         break;
423                 }
424                 
425                 LASSERT (order >= 0);
426                 if (rc < 0 ||
427                     dist < best_dist ||
428                     (dist == best_dist && order < best_order)) {
429                         best_dist = dist;
430                         best_order = order;
431
432                         if (portals_compatibility > 1) {
433                                 /* Strong portals compatibility: Zero the nid's
434                                  * NET, so if I'm reading new config logs, or
435                                  * getting configured by (new) lconf I can
436                                  * still talk to old servers. */
437                                 dst_nid = LNET_MKNID(0, LNET_NIDADDR(dst_nid));
438                                 src_nid = LNET_MKNID(0, LNET_NIDADDR(src_nid));
439                         }
440                         peer->nid = dst_nid;
441                         *self = src_nid;
442                         rc = 0;
443                 }
444         }
445
446         CDEBUG(D_NET,"%s->%s\n", uuid->uuid, libcfs_id2str(*peer));
447         if (rc != 0) 
448                 CERROR("No NID found for %s\n", uuid->uuid);
449         return rc;
450 }
451
452 void ptlrpc_ni_fini(void)
453 {
454         cfs_waitq_t         waitq;
455         struct l_wait_info  lwi;
456         int                 rc;
457         int                 retries;
458         
459         /* Wait for the event queue to become idle since there may still be
460          * messages in flight with pending events (i.e. the fire-and-forget
461          * messages == client requests and "non-difficult" server
462          * replies */
463
464         for (retries = 0;; retries++) {
465                 rc = LNetEQFree(ptlrpc_eq_h);
466                 switch (rc) {
467                 default:
468                         LBUG();
469
470                 case 0:
471                         LNetNIFini();
472                         return;
473                         
474                 case -EBUSY:
475                         if (retries != 0)
476                                 CWARN("Event queue still busy\n");
477                         
478                         /* Wait for a bit */
479                         cfs_waitq_init(&waitq);
480                         lwi = LWI_TIMEOUT(cfs_time_seconds(2), NULL, NULL);
481                         l_wait_event(waitq, 0, &lwi);
482                         break;
483                 }
484         }
485         /* notreached */
486 }
487
488 lnet_pid_t ptl_get_pid(void)
489 {
490         lnet_pid_t        pid;
491
492 #ifndef  __KERNEL__
493         pid = getpid();
494 #else
495         pid = LUSTRE_SRV_LNET_PID;
496 #endif
497         return pid;
498 }
499         
500 int ptlrpc_ni_init(void)
501 {
502         int              rc;
503         lnet_pid_t       pid;
504
505         pid = ptl_get_pid();
506         CDEBUG(D_NET, "My pid is: %x\n", pid);
507
508         /* We're not passing any limits yet... */
509         rc = LNetNIInit(pid);
510         if (rc < 0) {
511                 CDEBUG (D_NET, "Can't init network interface: %d\n", rc);
512                 return (-ENOENT);
513         }
514
515         /* CAVEAT EMPTOR: how we process portals events is _radically_
516          * different depending on... */
517 #ifdef __KERNEL__
518         /* kernel portals calls our master callback when events are added to
519          * the event queue.  In fact lustre never pulls events off this queue,
520          * so it's only sized for some debug history. */
521         rc = LNetEQAlloc(1024, ptlrpc_master_callback, &ptlrpc_eq_h);
522 #else
523         /* liblustre calls the master callback when it removes events from the
524          * event queue.  The event queue has to be big enough not to drop
525          * anything */
526         rc = LNetEQAlloc(10240, LNET_EQ_HANDLER_NONE, &ptlrpc_eq_h);
527 #endif
528         if (rc == 0)
529                 return 0;
530
531         CERROR ("Failed to allocate event queue: %d\n", rc);
532         LNetNIFini();
533
534         return (-ENOMEM);
535 }
536
537 #ifndef __KERNEL__
538 CFS_LIST_HEAD(liblustre_wait_callbacks);
539 CFS_LIST_HEAD(liblustre_idle_callbacks);
540 void *liblustre_services_callback;
541
542 void *
543 liblustre_register_waitidle_callback (struct list_head *callback_list,
544                                       const char *name,
545                                       int (*fn)(void *arg), void *arg)
546 {
547         struct liblustre_wait_callback *llwc;
548         
549         OBD_ALLOC(llwc, sizeof(*llwc));
550         LASSERT (llwc != NULL);
551         
552         llwc->llwc_name = name;
553         llwc->llwc_fn = fn;
554         llwc->llwc_arg = arg;
555         list_add_tail(&llwc->llwc_list, callback_list);
556         
557         return (llwc);
558 }
559
560 void
561 liblustre_deregister_waitidle_callback (void *opaque)
562 {
563         struct liblustre_wait_callback *llwc = opaque;
564         
565         list_del(&llwc->llwc_list);
566         OBD_FREE(llwc, sizeof(*llwc));
567 }
568
569 void *
570 liblustre_register_wait_callback (const char *name,
571                                   int (*fn)(void *arg), void *arg)
572 {
573         return liblustre_register_waitidle_callback(&liblustre_wait_callbacks,
574                                                     name, fn, arg);
575 }
576
577 void
578 liblustre_deregister_wait_callback (void *opaque)
579 {
580         liblustre_deregister_waitidle_callback(opaque);
581 }
582
583 void *
584 liblustre_register_idle_callback (const char *name,
585                                   int (*fn)(void *arg), void *arg)
586 {
587         return liblustre_register_waitidle_callback(&liblustre_idle_callbacks,
588                                                     name, fn, arg);
589 }
590
591 void
592 liblustre_deregister_idle_callback (void *opaque)
593 {
594         liblustre_deregister_waitidle_callback(opaque);
595 }
596
597 int
598 liblustre_check_events (int timeout)
599 {
600         lnet_event_t ev;
601         int         rc;
602         int         i;
603         ENTRY;
604
605         rc = LNetEQPoll(&ptlrpc_eq_h, 1, timeout * 1000, &ev, &i);
606         if (rc == 0)
607                 RETURN(0);
608         
609         LASSERT (rc == -EOVERFLOW || rc == 1);
610         
611         /* liblustre: no asynch callback so we can't affort to miss any
612          * events... */
613         if (rc == -EOVERFLOW) {
614                 CERROR ("Dropped an event!!!\n");
615                 abort();
616         }
617         
618         ptlrpc_master_callback (&ev);
619         RETURN(1);
620 }
621
622 int liblustre_waiting = 0;
623
624 int
625 liblustre_wait_event (int timeout)
626 {
627         struct list_head               *tmp;
628         struct liblustre_wait_callback *llwc;
629         int                             found_something = 0;
630
631         /* single threaded recursion check... */
632         liblustre_waiting = 1;
633
634         for (;;) {
635                 /* Deal with all pending events */
636                 while (liblustre_check_events(0))
637                         found_something = 1;
638
639                 /* Give all registered callbacks a bite at the cherry */
640                 list_for_each(tmp, &liblustre_wait_callbacks) {
641                         llwc = list_entry(tmp, struct liblustre_wait_callback, 
642                                           llwc_list);
643                 
644                         if (llwc->llwc_fn(llwc->llwc_arg))
645                                 found_something = 1;
646                 }
647
648                 if (found_something || timeout == 0)
649                         break;
650
651                 /* Nothing so far, but I'm allowed to block... */
652                 found_something = liblustre_check_events(timeout);
653                 if (!found_something)           /* still nothing */
654                         break;                  /* I timed out */
655         }
656
657         liblustre_waiting = 0;
658
659         return found_something;
660 }
661
662 void
663 liblustre_wait_idle(void)
664 {
665         static int recursed = 0;
666         
667         struct list_head               *tmp;
668         struct liblustre_wait_callback *llwc;
669         int                             idle = 0;
670
671         LASSERT(!recursed);
672         recursed = 1;
673         
674         do {
675                 liblustre_wait_event(0);
676
677                 idle = 1;
678
679                 list_for_each(tmp, &liblustre_idle_callbacks) {
680                         llwc = list_entry(tmp, struct liblustre_wait_callback,
681                                           llwc_list);
682                         
683                         if (!llwc->llwc_fn(llwc->llwc_arg)) {
684                                 idle = 0;
685                                 break;
686                         }
687                 }
688                         
689         } while (!idle);
690
691         recursed = 0;
692 }
693
694 #endif /* __KERNEL__ */
695
696 int ptlrpc_init_portals(void)
697 {
698         int   rc = ptlrpc_ni_init();
699
700         if (rc != 0) {
701                 CERROR("network initialisation failed\n");
702                 return -EIO;
703         }
704 #ifndef __KERNEL__
705         liblustre_services_callback = 
706                 liblustre_register_wait_callback("liblustre_check_services",
707                                                  &liblustre_check_services, NULL);
708 #endif
709         rc = ptlrpcd_addref();
710         if (rc == 0)
711                 return 0;
712         
713         CERROR("rpcd initialisation failed\n");
714 #ifndef __KERNEL__
715         liblustre_deregister_wait_callback(liblustre_services_callback);
716 #endif
717         ptlrpc_ni_fini();
718         return rc;
719 }
720
721 void ptlrpc_exit_portals(void)
722 {
723 #ifndef __KERNEL__
724         liblustre_deregister_wait_callback(liblustre_services_callback);
725 #endif
726         ptlrpcd_decref();
727         ptlrpc_ni_fini();
728 }