Whamcloud - gitweb
b=2258
[fs/lustre-release.git] / lustre / ptlrpc / service.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 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 #ifndef __KERNEL__
28 #include <liblustre.h>
29 #include <libcfs/kp30.h>
30 #endif
31 #include <obd_support.h>
32 #include <obd_class.h>
33 #include <lustre_net.h>
34 #include <lnet/types.h>
35 #include "ptlrpc_internal.h"
36
37 int test_req_buffer_pressure = 0;
38 CFS_MODULE_PARM(test_req_buffer_pressure, "i", int, 0444,
39                 "set non-zero to put pressure on request buffer pools");
40
41 /* forward ref */
42 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
43
44 static CFS_LIST_HEAD (ptlrpc_all_services);
45 spinlock_t ptlrpc_all_services_lock;
46
47 static char *
48 ptlrpc_alloc_request_buffer (int size)
49 {
50         char *ptr;
51
52         if (size > SVC_BUF_VMALLOC_THRESHOLD)
53                 OBD_VMALLOC(ptr, size);
54         else
55                 OBD_ALLOC(ptr, size);
56
57         return (ptr);
58 }
59
60 static void
61 ptlrpc_free_request_buffer (char *ptr, int size)
62 {
63         if (size > SVC_BUF_VMALLOC_THRESHOLD)
64                 OBD_VFREE(ptr, size);
65         else
66                 OBD_FREE(ptr, size);
67 }
68
69 struct ptlrpc_request_buffer_desc *
70 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
71 {
72         struct ptlrpc_request_buffer_desc *rqbd;
73
74         OBD_ALLOC(rqbd, sizeof (*rqbd));
75         if (rqbd == NULL)
76                 return (NULL);
77
78         rqbd->rqbd_service = svc;
79         rqbd->rqbd_refcount = 0;
80         rqbd->rqbd_cbid.cbid_fn = request_in_callback;
81         rqbd->rqbd_cbid.cbid_arg = rqbd;
82         CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
83         rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
84
85         if (rqbd->rqbd_buffer == NULL) {
86                 OBD_FREE(rqbd, sizeof (*rqbd));
87                 return (NULL);
88         }
89
90         spin_lock(&svc->srv_lock);
91         list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
92         svc->srv_nbufs++;
93         spin_unlock(&svc->srv_lock);
94
95         return (rqbd);
96 }
97
98 void
99 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
100 {
101         struct ptlrpc_service *svc = rqbd->rqbd_service;
102
103         LASSERT (rqbd->rqbd_refcount == 0);
104         LASSERT (list_empty(&rqbd->rqbd_reqs));
105
106         spin_lock(&svc->srv_lock);
107         list_del(&rqbd->rqbd_list);
108         svc->srv_nbufs--;
109         spin_unlock(&svc->srv_lock);
110
111         ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
112         OBD_FREE (rqbd, sizeof (*rqbd));
113 }
114
115 int
116 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
117 {
118         struct ptlrpc_request_buffer_desc *rqbd;
119         int                                i;
120
121         CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
122                svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
123                svc->srv_nrqbd_receiving, svc->srv_nbufs);
124         for (i = 0; i < svc->srv_nbuf_per_group; i++) {
125                 rqbd = ptlrpc_alloc_rqbd(svc);
126
127                 if (rqbd == NULL) {
128                         CERROR ("%s: Can't allocate request buffer\n",
129                                 svc->srv_name);
130                         return (-ENOMEM);
131                 }
132
133                 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
134                         return (-EAGAIN);
135         }
136
137         return (0);
138 }
139
140 void
141 ptlrpc_save_lock (struct ptlrpc_request *req,
142                   struct lustre_handle *lock, int mode)
143 {
144         struct ptlrpc_reply_state *rs = req->rq_reply_state;
145         int                        idx;
146
147         LASSERT(rs != NULL);
148         LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
149
150         idx = rs->rs_nlocks++;
151         rs->rs_locks[idx] = *lock;
152         rs->rs_modes[idx] = mode;
153         rs->rs_difficult = 1;
154 }
155
156 void
157 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
158 {
159         struct ptlrpc_service *svc = rs->rs_service;
160
161 #ifdef CONFIG_SMP
162         LASSERT (spin_is_locked (&svc->srv_lock));
163 #endif
164         LASSERT (rs->rs_difficult);
165         rs->rs_scheduled_ever = 1;              /* flag any notification attempt */
166
167         if (rs->rs_scheduled)                   /* being set up or already notified */
168                 return;
169
170         rs->rs_scheduled = 1;
171         list_del (&rs->rs_list);
172         list_add (&rs->rs_list, &svc->srv_reply_queue);
173         cfs_waitq_signal (&svc->srv_waitq);
174 }
175
176 void
177 ptlrpc_commit_replies (struct obd_device *obd)
178 {
179         struct list_head   *tmp;
180         struct list_head   *nxt;
181
182         /* Find any replies that have been committed and get their service
183          * to attend to complete them. */
184
185         /* CAVEAT EMPTOR: spinlock ordering!!! */
186         spin_lock(&obd->obd_uncommitted_replies_lock);
187
188         list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
189                 struct ptlrpc_reply_state *rs =
190                         list_entry(tmp, struct ptlrpc_reply_state, rs_obd_list);
191
192                 LASSERT (rs->rs_difficult);
193
194                 if (rs->rs_transno <= obd->obd_last_committed) {
195                         struct ptlrpc_service *svc = rs->rs_service;
196
197                         spin_lock (&svc->srv_lock);
198                         list_del_init (&rs->rs_obd_list);
199                         ptlrpc_schedule_difficult_reply (rs);
200                         spin_unlock (&svc->srv_lock);
201                 }
202         }
203
204         spin_unlock(&obd->obd_uncommitted_replies_lock);
205 }
206
207 static int
208 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
209 {
210         struct ptlrpc_request_buffer_desc *rqbd;
211         int                                rc;
212         int                                posted = 0;
213
214         for (;;) {
215                 spin_lock(&svc->srv_lock);
216
217                 if (list_empty (&svc->srv_idle_rqbds)) {
218                         spin_unlock(&svc->srv_lock);
219                         return (posted);
220                 }
221
222                 rqbd = list_entry(svc->srv_idle_rqbds.next,
223                                   struct ptlrpc_request_buffer_desc,
224                                   rqbd_list);
225                 list_del (&rqbd->rqbd_list);
226
227                 /* assume we will post successfully */
228                 svc->srv_nrqbd_receiving++;
229                 list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
230
231                 spin_unlock(&svc->srv_lock);
232
233                 rc = ptlrpc_register_rqbd(rqbd);
234                 if (rc != 0)
235                         break;
236
237                 posted = 1;
238         }
239
240         spin_lock(&svc->srv_lock);
241
242         svc->srv_nrqbd_receiving--;
243         list_del(&rqbd->rqbd_list);
244         list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
245
246         /* Don't complain if no request buffers are posted right now; LNET
247          * won't drop requests because we set the portal lazy! */
248
249         spin_unlock(&svc->srv_lock);
250
251         return (-1);
252 }
253
254 /* @threadname should be 11 characters or less - 3 will be added on */
255 struct ptlrpc_service *
256 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
257                 int req_portal, int rep_portal, int watchdog_timeout,
258                 svc_handler_t handler, char *name,
259                 cfs_proc_dir_entry_t *proc_entry,
260                 svcreq_printfn_t svcreq_printfn, 
261                 int min_threads, int max_threads, char *threadname)
262 {
263         int                    rc;
264         struct ptlrpc_service *service;
265         ENTRY;
266
267         LASSERT (nbufs > 0);
268         LASSERT (bufsize >= max_req_size);
269         
270         OBD_ALLOC(service, sizeof(*service));
271         if (service == NULL)
272                 RETURN(NULL);
273
274         /* First initialise enough for early teardown */
275
276         service->srv_name = name;
277         spin_lock_init(&service->srv_lock);
278         CFS_INIT_LIST_HEAD(&service->srv_threads);
279         cfs_waitq_init(&service->srv_waitq);
280
281         service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
282         service->srv_max_req_size = max_req_size;
283         service->srv_buf_size = bufsize;
284         service->srv_rep_portal = rep_portal;
285         service->srv_req_portal = req_portal;
286         service->srv_watchdog_timeout = watchdog_timeout;
287         service->srv_handler = handler;
288         service->srv_request_history_print_fn = svcreq_printfn;
289         service->srv_request_seq = 1;           /* valid seq #s start at 1 */
290         service->srv_request_max_cull_seq = 0;
291         service->srv_threads_min = min_threads;
292         service->srv_threads_max = max_threads;
293         service->srv_thread_name = threadname;
294
295         rc = LNetSetLazyPortal(service->srv_req_portal);
296         LASSERT (rc == 0);
297
298         CFS_INIT_LIST_HEAD(&service->srv_request_queue);
299         CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
300         CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
301         CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
302         CFS_INIT_LIST_HEAD(&service->srv_request_history);
303         CFS_INIT_LIST_HEAD(&service->srv_active_replies);
304         CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
305         CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
306         cfs_waitq_init(&service->srv_free_rs_waitq);
307
308         spin_lock (&ptlrpc_all_services_lock);
309         list_add (&service->srv_list, &ptlrpc_all_services);
310         spin_unlock (&ptlrpc_all_services_lock);
311         
312         /* Now allocate the request buffers */
313         rc = ptlrpc_grow_req_bufs(service);
314         /* We shouldn't be under memory pressure at startup, so
315          * fail if we can't post all our buffers at this time. */
316         if (rc != 0)
317                 GOTO(failed, NULL);
318
319         /* Now allocate pool of reply buffers */
320         /* Increase max reply size to next power of two */
321         service->srv_max_reply_size = 1;
322         while (service->srv_max_reply_size < max_reply_size)
323                 service->srv_max_reply_size <<= 1;
324
325         if (proc_entry != NULL)
326                 ptlrpc_lprocfs_register_service(proc_entry, service);
327
328         CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
329                service->srv_name, service->srv_req_portal);
330
331         RETURN(service);
332 failed:
333         ptlrpc_unregister_service(service);
334         return NULL;
335 }
336
337 static void __ptlrpc_server_free_request(struct ptlrpc_request *req)
338 {
339         struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
340
341         list_del(&req->rq_list);
342
343         if (req->rq_reply_state != NULL) {
344                 ptlrpc_rs_decref(req->rq_reply_state);
345                 req->rq_reply_state = NULL;
346         }
347
348         if (req != &rqbd->rqbd_req) {
349                 /* NB request buffers use an embedded
350                  * req if the incoming req unlinked the
351                  * MD; this isn't one of them! */
352                 OBD_FREE(req, sizeof(*req));
353         }
354 }
355
356 static void
357 ptlrpc_server_free_request(struct ptlrpc_request *req)
358 {
359         struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
360         struct ptlrpc_service             *svc = rqbd->rqbd_service;
361         int                                refcount;
362         struct list_head                  *tmp;
363         struct list_head                  *nxt;
364
365         spin_lock(&svc->srv_lock);
366
367         svc->srv_n_active_reqs--;
368         list_add(&req->rq_list, &rqbd->rqbd_reqs);
369
370         refcount = --(rqbd->rqbd_refcount);
371         if (refcount == 0) {
372                 /* request buffer is now idle: add to history */
373                 list_del(&rqbd->rqbd_list);
374                 list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
375                 svc->srv_n_history_rqbds++;
376
377                 /* cull some history?
378                  * I expect only about 1 or 2 rqbds need to be recycled here */
379                 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
380                         rqbd = list_entry(svc->srv_history_rqbds.next,
381                                           struct ptlrpc_request_buffer_desc,
382                                           rqbd_list);
383
384                         list_del(&rqbd->rqbd_list);
385                         svc->srv_n_history_rqbds--;
386
387                         /* remove rqbd's reqs from svc's req history while
388                          * I've got the service lock */
389                         list_for_each(tmp, &rqbd->rqbd_reqs) {
390                                 req = list_entry(tmp, struct ptlrpc_request,
391                                                  rq_list);
392                                 /* Track the highest culled req seq */
393                                 if (req->rq_history_seq >
394                                     svc->srv_request_max_cull_seq)
395                                         svc->srv_request_max_cull_seq =
396                                                 req->rq_history_seq;
397                                 list_del(&req->rq_history_list);
398                         }
399
400                         spin_unlock(&svc->srv_lock);
401
402                         list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
403                                 req = list_entry(rqbd->rqbd_reqs.next,
404                                                  struct ptlrpc_request,
405                                                  rq_list);
406                                 __ptlrpc_server_free_request(req);
407                         }
408
409                         spin_lock(&svc->srv_lock);
410
411                         /* schedule request buffer for re-use.
412                          * NB I can only do this after I've disposed of their
413                          * reqs; particularly the embedded req */
414                         list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
415                 }
416         } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
417                  /* If we are low on memory, we are not interested in
418                     history */
419                 list_del(&req->rq_history_list);
420                 __ptlrpc_server_free_request(req);
421         }
422
423         spin_unlock(&svc->srv_lock);
424
425 }
426
427 /* This function makes sure dead exports are evicted in a timely manner.
428    This function is only called when some export receives a message (i.e.,
429    the network is up.) */
430 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
431 {
432         struct obd_export *oldest_exp;
433         time_t oldest_time;
434
435         ENTRY;
436
437         LASSERT(exp);
438
439         /* Compensate for slow machines, etc, by faking our request time
440            into the future.  Although this can break the strict time-ordering
441            of the list, we can be really lazy here - we don't have to evict
442            at the exact right moment.  Eventually, all silent exports
443            will make it to the top of the list. */
444         exp->exp_last_request_time = max(exp->exp_last_request_time,
445                                          (time_t)CURRENT_SECONDS + extra_delay);
446
447         CDEBUG(D_INFO, "updating export %s at %ld\n",
448                exp->exp_client_uuid.uuid,
449                exp->exp_last_request_time);
450
451         /* exports may get disconnected from the chain even though the
452            export has references, so we must keep the spin lock while
453            manipulating the lists */
454         spin_lock(&exp->exp_obd->obd_dev_lock);
455
456         if (list_empty(&exp->exp_obd_chain_timed)) {
457                 /* this one is not timed */
458                 spin_unlock(&exp->exp_obd->obd_dev_lock);
459                 EXIT;
460                 return;
461         }
462
463         list_move_tail(&exp->exp_obd_chain_timed,
464                        &exp->exp_obd->obd_exports_timed);
465
466         oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
467                                 struct obd_export, exp_obd_chain_timed);
468         oldest_time = oldest_exp->exp_last_request_time;
469         spin_unlock(&exp->exp_obd->obd_dev_lock);
470
471         if (exp->exp_obd->obd_recovering) {
472                 /* be nice to everyone during recovery */
473                 EXIT;
474                 return;
475         }
476
477         /* Note - racing to start/reset the obd_eviction timer is safe */
478         if (exp->exp_obd->obd_eviction_timer == 0) {
479                 /* Check if the oldest entry is expired. */
480                 if (CURRENT_SECONDS > (oldest_time +
481                                        (3 * obd_timeout / 2) + extra_delay)) {
482                         /* We need a second timer, in case the net was down and
483                          * it just came back. Since the pinger may skip every
484                          * other PING_INTERVAL (see note in ptlrpc_pinger_main),
485                          * we better wait for 3. */
486                         exp->exp_obd->obd_eviction_timer = CURRENT_SECONDS +
487                                 3 * PING_INTERVAL;
488                         CDEBUG(D_HA, "%s: Think about evicting %s from %ld\n",
489                                exp->exp_obd->obd_name, obd_export_nid2str(exp),
490                                oldest_time);
491                 }
492         } else {
493                 if (CURRENT_SECONDS > (exp->exp_obd->obd_eviction_timer +
494                                        extra_delay)) {
495                         /* The evictor won't evict anyone who we've heard from
496                          * recently, so we don't have to check before we start
497                          * it. */
498                         if (!ping_evictor_wake(exp))
499                                 exp->exp_obd->obd_eviction_timer = 0;
500                 }
501         }
502
503         EXIT;
504 }
505
506 static int
507 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
508                              struct ptlrpc_thread *thread)
509 {
510         struct obd_export     *export = NULL;
511         struct ptlrpc_request *request;
512         struct timeval         work_start;
513         struct timeval         work_end;
514         long                   timediff;
515         int                    rc, reply;
516         ENTRY;
517
518         LASSERT(svc);
519
520         spin_lock(&svc->srv_lock);
521         if (list_empty (&svc->srv_request_queue) ||
522             (svc->srv_n_difficult_replies != 0 &&
523              svc->srv_n_active_reqs >= (svc->srv_threads_running - 1))) {
524                 /* If all the other threads are handling requests, I must
525                  * remain free to handle any 'difficult' reply that might
526                  * block them */
527                 spin_unlock(&svc->srv_lock);
528                 RETURN(0);
529         }
530
531         request = list_entry (svc->srv_request_queue.next,
532                               struct ptlrpc_request, rq_list);
533         list_del_init (&request->rq_list);
534         svc->srv_n_queued_reqs--;
535         svc->srv_n_active_reqs++;
536
537         spin_unlock(&svc->srv_lock);
538
539         do_gettimeofday(&work_start);
540         timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
541         if (svc->srv_stats != NULL) {
542                 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
543                                     timediff);
544                 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
545                                     svc->srv_n_queued_reqs);
546                 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
547                                     svc->srv_n_active_reqs);
548         }
549
550 #if SWAB_PARANOIA
551         /* Clear request swab mask; this is a new request */
552         request->rq_req_swab_mask = 0;
553 #endif
554         rc = lustre_unpack_msg(request->rq_reqmsg, request->rq_reqlen);
555         if (rc != 0) {
556                 CERROR ("error unpacking request: ptl %d from %s"
557                         " xid "LPU64"\n", svc->srv_req_portal,
558                         libcfs_id2str(request->rq_peer), request->rq_xid);
559                 goto out;
560         }
561
562         rc = lustre_unpack_req_ptlrpc_body(request, MSG_PTLRPC_BODY_OFF);
563         if (rc) {
564                 CERROR ("error unpacking ptlrpc body: ptl %d from %s"
565                         " xid "LPU64"\n", svc->srv_req_portal,
566                         libcfs_id2str(request->rq_peer), request->rq_xid);
567                 goto out;
568         }
569
570         rc = -EINVAL;
571         if (lustre_msg_get_type(request->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
572                 CERROR("wrong packet type received (type=%u) from %s\n",
573                        lustre_msg_get_type(request->rq_reqmsg),
574                        libcfs_id2str(request->rq_peer));
575                 goto out;
576         }
577
578         CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
579
580         request->rq_svc_thread = thread;
581         request->rq_export = class_conn2export(
582                                      lustre_msg_get_handle(request->rq_reqmsg));
583
584         if (request->rq_export) {
585                 if (lustre_msg_get_conn_cnt(request->rq_reqmsg) <
586                     request->rq_export->exp_conn_cnt) {
587                         DEBUG_REQ(D_ERROR, request,
588                                   "DROPPING req from old connection %d < %d",
589                                   lustre_msg_get_conn_cnt(request->rq_reqmsg),
590                                   request->rq_export->exp_conn_cnt);
591                         goto put_conn;
592                 }
593                 if (request->rq_export->exp_obd &&
594                     request->rq_export->exp_obd->obd_fail) {
595                         /* Failing over, don't handle any more reqs, send
596                            error response instead. */
597                         CDEBUG(D_HA, "Dropping req %p for failed obd %s\n",
598                                request, request->rq_export->exp_obd->obd_name);
599                         request->rq_status = -ENODEV;
600                         ptlrpc_error(request);
601                         goto put_conn;
602                 }
603
604                 ptlrpc_update_export_timer(request->rq_export, timediff/500000);
605                 export = class_export_rpc_get(request->rq_export);
606         }
607
608         /* Discard requests queued for longer than my timeout.  If the
609          * client's timeout is similar to mine, she'll be timing out this
610          * REQ anyway (bug 1502) */
611         if (timediff / 1000000 > (long)obd_timeout) {
612                 CERROR("Dropping timed-out opc %d request from %s"
613                        ": %ld seconds old\n",
614                        lustre_msg_get_opc(request->rq_reqmsg),
615                        libcfs_id2str(request->rq_peer),
616                        timediff / 1000000);
617                 goto put_rpc_export;
618         }
619
620         request->rq_phase = RQ_PHASE_INTERPRET;
621
622         CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
623                "%s:%s+%d:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
624                (request->rq_export ?
625                 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
626                (request->rq_export ?
627                 atomic_read(&request->rq_export->exp_refcount) : -99),
628                lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
629                libcfs_id2str(request->rq_peer),
630                lustre_msg_get_opc(request->rq_reqmsg));
631
632         rc = svc->srv_handler(request);
633
634         request->rq_phase = RQ_PHASE_COMPLETE;
635
636         CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
637                "%s:%s+%d:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
638                (request->rq_export ?
639                 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
640                (request->rq_export ?
641                 atomic_read(&request->rq_export->exp_refcount) : -99),
642                lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
643                libcfs_id2str(request->rq_peer),
644                lustre_msg_get_opc(request->rq_reqmsg));
645
646 put_rpc_export:
647         if (export != NULL)
648                 class_export_rpc_put(export);
649
650 put_conn:
651         if (request->rq_export != NULL)
652                 class_export_put(request->rq_export);
653
654 out:
655         reply = request->rq_reply_state && request->rq_repmsg;  /* bug 11169 */ 
656          
657         do_gettimeofday(&work_end);
658         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
659         if (timediff / 1000000 > (long)obd_timeout)
660                 CERROR("request "LPU64" opc %u from %s processed in %lds "
661                        "trans "LPU64" rc %d/%d\n",
662                        request->rq_xid, lustre_msg_get_opc(request->rq_reqmsg),
663                        libcfs_id2str(request->rq_peer),
664                        cfs_timeval_sub(&work_end, &request->rq_arrival_time,
665                                        NULL) / 1000000,
666                        reply ? lustre_msg_get_transno(request->rq_repmsg) :
667                                request->rq_transno, 
668                        request->rq_status,
669                        reply ? lustre_msg_get_status(request->rq_repmsg): -999);
670         else
671                 CDEBUG(D_HA, "request "LPU64" opc %u from %s processed in "
672                        "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
673                        request->rq_xid, lustre_msg_get_opc(request->rq_reqmsg),
674                        libcfs_id2str(request->rq_peer), timediff,
675                        cfs_timeval_sub(&work_end, &request->rq_arrival_time,
676                                        NULL),
677                        request->rq_transno, request->rq_status,
678                        reply ? lustre_msg_get_status(request->rq_repmsg): -999);
679
680         if (svc->srv_stats != NULL) {
681                 int opc = opcode_offset(lustre_msg_get_opc(request->rq_reqmsg));
682                 if (opc > 0) {
683                         LASSERT(opc < LUSTRE_MAX_OPCODES);
684                         lprocfs_counter_add(svc->srv_stats,
685                                             opc + PTLRPC_LAST_CNTR,
686                                             timediff);
687                 }
688         }
689
690         ptlrpc_server_free_request(request);
691
692         RETURN(1);
693 }
694
695 static int
696 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
697 {
698         struct ptlrpc_reply_state *rs;
699         struct obd_export         *exp;
700         struct obd_device         *obd;
701         int                        nlocks;
702         int                        been_handled;
703         ENTRY;
704
705         spin_lock(&svc->srv_lock);
706         if (list_empty (&svc->srv_reply_queue)) {
707                 spin_unlock(&svc->srv_lock);
708                 RETURN(0);
709         }
710
711         rs = list_entry (svc->srv_reply_queue.next,
712                          struct ptlrpc_reply_state, rs_list);
713
714         exp = rs->rs_export;
715         obd = exp->exp_obd;
716
717         LASSERT (rs->rs_difficult);
718         LASSERT (rs->rs_scheduled);
719
720         list_del_init (&rs->rs_list);
721
722         /* Disengage from notifiers carefully (lock order - irqrestore below!)*/
723         spin_unlock(&svc->srv_lock);
724
725         spin_lock (&obd->obd_uncommitted_replies_lock);
726         /* Noop if removed already */
727         list_del_init (&rs->rs_obd_list);
728         spin_unlock (&obd->obd_uncommitted_replies_lock);
729
730         spin_lock (&exp->exp_lock);
731         /* Noop if removed already */
732         list_del_init (&rs->rs_exp_list);
733         spin_unlock (&exp->exp_lock);
734
735         spin_lock(&svc->srv_lock);
736
737         been_handled = rs->rs_handled;
738         rs->rs_handled = 1;
739
740         nlocks = rs->rs_nlocks;                 /* atomic "steal", but */
741         rs->rs_nlocks = 0;                      /* locks still on rs_locks! */
742
743         if (nlocks == 0 && !been_handled) {
744                 /* If we see this, we should already have seen the warning
745                  * in mds_steal_ack_locks()  */
746                 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
747                       " o%d NID %s\n",
748                       rs,
749                       rs->rs_xid, rs->rs_transno,
750                       lustre_msg_get_opc(rs->rs_msg),
751                       libcfs_nid2str(exp->exp_connection->c_peer.nid));
752         }
753
754         if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
755                 spin_unlock(&svc->srv_lock);
756
757                 if (!been_handled && rs->rs_on_net) {
758                         LNetMDUnlink(rs->rs_md_h);
759                         /* Ignore return code; we're racing with
760                          * completion... */
761                 }
762
763                 while (nlocks-- > 0)
764                         ldlm_lock_decref(&rs->rs_locks[nlocks],
765                                          rs->rs_modes[nlocks]);
766
767                 spin_lock(&svc->srv_lock);
768         }
769
770         rs->rs_scheduled = 0;
771
772         if (!rs->rs_on_net) {
773                 /* Off the net */
774                 svc->srv_n_difficult_replies--;
775                 spin_unlock(&svc->srv_lock);
776
777                 class_export_put (exp);
778                 rs->rs_export = NULL;
779                 ptlrpc_rs_decref (rs);
780                 atomic_dec (&svc->srv_outstanding_replies);
781                 RETURN(1);
782         }
783
784         /* still on the net; callback will schedule */
785         spin_unlock(&svc->srv_lock);
786         RETURN(1);
787 }
788
789 #ifndef __KERNEL__
790 /* FIXME make use of timeout later */
791 int
792 liblustre_check_services (void *arg)
793 {
794         int  did_something = 0;
795         int  rc;
796         struct list_head *tmp, *nxt;
797         ENTRY;
798
799         /* I'm relying on being single threaded, not to have to lock
800          * ptlrpc_all_services etc */
801         list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
802                 struct ptlrpc_service *svc =
803                         list_entry (tmp, struct ptlrpc_service, srv_list);
804
805                 if (svc->srv_threads_running != 0)     /* I've recursed */
806                         continue;
807
808                 /* service threads can block for bulk, so this limits us
809                  * (arbitrarily) to recursing 1 stack frame per service.
810                  * Note that the problem with recursion is that we have to
811                  * unwind completely before our caller can resume. */
812
813                 svc->srv_threads_running++;
814
815                 do {
816                         rc = ptlrpc_server_handle_reply(svc);
817                         rc |= ptlrpc_server_handle_request(svc, NULL);
818                         rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
819                         did_something |= rc;
820                 } while (rc);
821
822                 svc->srv_threads_running--;
823         }
824
825         RETURN(did_something);
826 }
827 #define ptlrpc_stop_all_threads(s) do {} while (0)
828
829 #else /* __KERNEL__ */
830
831 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
832 void ptlrpc_daemonize(char *name)
833 {
834         struct fs_struct *fs = current->fs;
835
836         atomic_inc(&fs->count);
837         cfs_daemonize(name);
838         exit_fs(cfs_current());
839         current->fs = fs;
840         ll_set_fs_pwd(current->fs, init_task.fs->pwdmnt, init_task.fs->pwd);
841 }
842
843 static void
844 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
845 {
846         int avail = svc->srv_nrqbd_receiving;
847         int low_water = test_req_buffer_pressure ? 0 :
848                         svc->srv_nbuf_per_group/2;
849
850         /* NB I'm not locking; just looking. */
851
852         /* CAVEAT EMPTOR: We might be allocating buffers here because we've
853          * allowed the request history to grow out of control.  We could put a
854          * sanity check on that here and cull some history if we need the
855          * space. */
856
857         if (avail <= low_water)
858                 ptlrpc_grow_req_bufs(svc);
859
860         if (svc->srv_stats)
861                 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
862                                     avail);
863 }
864
865 static int
866 ptlrpc_retry_rqbds(void *arg)
867 {
868         struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
869
870         svc->srv_rqbd_timeout = 0;
871         return (-ETIMEDOUT);
872 }
873
874 static int ptlrpc_main(void *arg)
875 {
876         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
877         struct ptlrpc_service  *svc = data->svc;
878         struct ptlrpc_thread   *thread = data->thread;
879         struct obd_device      *dev = data->dev;
880         struct ptlrpc_reply_state *rs;
881         struct lc_watchdog     *watchdog;
882 #ifdef WITH_GROUP_INFO
883         struct group_info *ginfo = NULL;
884 #endif
885         int rc = 0;
886         ENTRY;
887
888         ptlrpc_daemonize(data->name);
889
890 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9) && defined(CONFIG_NUMA)
891         /* we need to do this before any per-thread allocation is done so that
892          * we get the per-thread allocations on local node.  bug 7342 */
893         if (svc->srv_cpu_affinity) {
894                 int cpu, num_cpu;
895
896                 for (cpu = 0, num_cpu = 0; cpu < NR_CPUS; cpu++) {
897                         if (!cpu_online(cpu))
898                                 continue;
899                         if (num_cpu == thread->t_id % num_online_cpus())
900                                 break;
901                         num_cpu++;
902                 }
903                 set_cpus_allowed(cfs_current(), node_to_cpumask(cpu_to_node(cpu)));
904         }
905 #endif
906
907 #ifdef WITH_GROUP_INFO
908         ginfo = groups_alloc(0);
909         if (!ginfo) {
910                 rc = -ENOMEM;
911                 goto out;
912         }
913
914         set_current_groups(ginfo);
915         put_group_info(ginfo);
916 #endif
917
918         if (svc->srv_init != NULL) {
919                 rc = svc->srv_init(thread);
920                 if (rc)
921                         goto out;
922         }
923
924         /* Alloc reply state structure for this one */
925         OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
926         if (!rs) {
927                 rc = -ENOMEM;
928                 goto out_srv_init;
929         }
930
931         /* Record that the thread is running */
932         thread->t_flags = SVC_RUNNING;
933         /*
934          * wake up our creator. Note: @data is invalid after this point,
935          * because it's allocated on ptlrpc_start_thread() stack.
936          */
937         cfs_waitq_signal(&thread->t_ctl_waitq);
938
939         watchdog = lc_watchdog_add(svc->srv_watchdog_timeout, NULL, NULL);
940
941         spin_lock(&svc->srv_lock);
942         svc->srv_threads_running++;
943         list_add(&rs->rs_list, &svc->srv_free_rs_list);
944         spin_unlock(&svc->srv_lock);
945         cfs_waitq_signal(&svc->srv_free_rs_waitq);
946
947         CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
948                svc->srv_threads_running);
949
950         /* XXX maintain a list of all managed devices: insert here */
951
952         while ((thread->t_flags & SVC_STOPPING) == 0 ||
953                svc->srv_n_difficult_replies != 0) {
954                 /* Don't exit while there are replies to be handled */
955                 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
956                                                      ptlrpc_retry_rqbds, svc);
957
958                 lc_watchdog_disable(watchdog);
959
960                 l_wait_event_exclusive (svc->srv_waitq,
961                               ((thread->t_flags & SVC_STOPPING) != 0 &&
962                                svc->srv_n_difficult_replies == 0) ||
963                               (!list_empty(&svc->srv_idle_rqbds) &&
964                                svc->srv_rqbd_timeout == 0) ||
965                               !list_empty (&svc->srv_reply_queue) ||
966                               (!list_empty (&svc->srv_request_queue) &&
967                                (svc->srv_n_difficult_replies == 0 ||
968                                 svc->srv_n_active_reqs <
969                                 (svc->srv_threads_running - 1))),
970                               &lwi);
971
972                 lc_watchdog_touch(watchdog);
973
974                 ptlrpc_check_rqbd_pool(svc);
975                 
976                 if ((svc->srv_threads_started < svc->srv_threads_max) &&
977                     (svc->srv_n_active_reqs >= (svc->srv_threads_started - 1))){
978                         /* Ignore return code - we tried... */
979                         ptlrpc_start_thread(dev, svc);
980                 }
981
982                 if (!list_empty (&svc->srv_reply_queue))
983                         ptlrpc_server_handle_reply (svc);
984
985                 /* only handle requests if there are no difficult replies
986                  * outstanding, or I'm not the last thread handling
987                  * requests */
988                 if (!list_empty (&svc->srv_request_queue) &&
989                     (svc->srv_n_difficult_replies == 0 ||
990                      svc->srv_n_active_reqs < (svc->srv_threads_running - 1)))
991                         ptlrpc_server_handle_request(svc, thread);
992
993                 if (!list_empty(&svc->srv_idle_rqbds) &&
994                     ptlrpc_server_post_idle_rqbds(svc) < 0) {
995                         /* I just failed to repost request buffers.  Wait
996                          * for a timeout (unless something else happens)
997                          * before I try again */
998                         svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
999                         CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
1000                                svc->srv_nrqbd_receiving);
1001                 }
1002         }
1003
1004         lc_watchdog_delete(watchdog);
1005
1006 out_srv_init:
1007         /*
1008          * deconstruct service specific state created by ptlrpc_start_thread()
1009          */
1010         if (svc->srv_done != NULL)
1011                 svc->srv_done(thread);
1012
1013 out:
1014         CDEBUG(D_NET, "service thread %d exiting: rc %d\n", thread->t_id, rc);
1015
1016         spin_lock(&svc->srv_lock);
1017         svc->srv_threads_running--;              /* must know immediately */
1018         thread->t_id = rc;
1019         thread->t_flags = SVC_STOPPED;
1020
1021         cfs_waitq_signal(&thread->t_ctl_waitq);
1022         spin_unlock(&svc->srv_lock);
1023
1024         return rc;
1025 }
1026
1027 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
1028                                struct ptlrpc_thread *thread)
1029 {
1030         struct l_wait_info lwi = { 0 };
1031
1032         spin_lock(&svc->srv_lock);
1033         thread->t_flags = SVC_STOPPING;
1034         spin_unlock(&svc->srv_lock);
1035
1036         cfs_waitq_broadcast(&svc->srv_waitq);
1037         l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
1038                      &lwi);
1039
1040         spin_lock(&svc->srv_lock);
1041         list_del(&thread->t_link);
1042         spin_unlock(&svc->srv_lock);
1043
1044         OBD_FREE(thread, sizeof(*thread));
1045 }
1046
1047 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
1048 {
1049         struct ptlrpc_thread *thread;
1050
1051         spin_lock(&svc->srv_lock);
1052         while (!list_empty(&svc->srv_threads)) {
1053                 thread = list_entry(svc->srv_threads.next,
1054                                     struct ptlrpc_thread, t_link);
1055
1056                 spin_unlock(&svc->srv_lock);
1057                 ptlrpc_stop_thread(svc, thread);
1058                 spin_lock(&svc->srv_lock);
1059         }
1060
1061         spin_unlock(&svc->srv_lock);
1062 }
1063
1064 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc)
1065 {
1066         int i, rc = 0;
1067         ENTRY;
1068
1069         LASSERT(svc->srv_threads_min > 0);
1070         for (i = 0; i < svc->srv_threads_min; i++) {
1071                 rc = ptlrpc_start_thread(dev, svc);
1072                 if (rc) {
1073                         CERROR("cannot start %s thread #%d: rc %d\n",
1074                                svc->srv_thread_name, i, rc);
1075                         ptlrpc_stop_all_threads(svc);
1076                 }
1077         }
1078         RETURN(rc);
1079 }
1080
1081 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc)
1082 {
1083         struct l_wait_info lwi = { 0 };
1084         struct ptlrpc_svc_data d;
1085         struct ptlrpc_thread *thread;
1086         char name[32];
1087         int id, rc;
1088         ENTRY;
1089
1090         CDEBUG(D_RPCTRACE, "%s started %d min %d max %d running %d\n",
1091                svc->srv_name, svc->srv_threads_started, svc->srv_threads_min,
1092                svc->srv_threads_max, svc->srv_threads_running);
1093         if (svc->srv_threads_started >= svc->srv_threads_max) 
1094                 RETURN(-EMFILE);
1095
1096         OBD_ALLOC(thread, sizeof(*thread));
1097         if (thread == NULL)
1098                 RETURN(-ENOMEM);
1099         cfs_waitq_init(&thread->t_ctl_waitq);
1100
1101         spin_lock(&svc->srv_lock);
1102         if (svc->srv_threads_started >= svc->srv_threads_max) {
1103                 spin_unlock(&svc->srv_lock);
1104                 OBD_FREE(thread, sizeof(*thread));
1105                 RETURN(-EMFILE);
1106         }
1107         list_add(&thread->t_link, &svc->srv_threads);
1108         id = ++svc->srv_threads_started;
1109         spin_unlock(&svc->srv_lock);
1110
1111         thread->t_id = id;
1112         sprintf(name, "%s_%02d", svc->srv_thread_name, id);
1113         d.dev = dev;
1114         d.svc = svc;
1115         d.name = name;
1116         d.thread = thread;
1117
1118         CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
1119         
1120         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
1121          * just drop the VM and FILES in ptlrpc_daemonize() right away.
1122          */
1123         rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
1124         if (rc < 0) {
1125                 CERROR("cannot start thread '%s': rc %d\n", name, rc);
1126
1127                 spin_lock(&svc->srv_lock);
1128                 list_del(&thread->t_link);
1129                 --svc->srv_threads_started;
1130                 spin_unlock(&svc->srv_lock);
1131
1132                 OBD_FREE(thread, sizeof(*thread));
1133                 RETURN(rc);
1134         }
1135         l_wait_event(thread->t_ctl_waitq,
1136                      thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
1137
1138         rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
1139         RETURN(rc);
1140 }
1141 #endif
1142
1143 int ptlrpc_unregister_service(struct ptlrpc_service *service)
1144 {
1145         int                   rc;
1146         struct l_wait_info    lwi;
1147         struct list_head     *tmp;
1148         struct ptlrpc_reply_state *rs, *t;
1149
1150         ptlrpc_stop_all_threads(service);
1151         LASSERT(list_empty(&service->srv_threads));
1152
1153         spin_lock (&ptlrpc_all_services_lock);
1154         list_del_init (&service->srv_list);
1155         spin_unlock (&ptlrpc_all_services_lock);
1156
1157         ptlrpc_lprocfs_unregister_service(service);
1158
1159         /* All history will be culled when the next request buffer is
1160          * freed */
1161         service->srv_max_history_rqbds = 0;
1162
1163         CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
1164
1165         rc = LNetClearLazyPortal(service->srv_req_portal);
1166         LASSERT (rc == 0);
1167
1168         /* Unlink all the request buffers.  This forces a 'final' event with
1169          * its 'unlink' flag set for each posted rqbd */
1170         list_for_each(tmp, &service->srv_active_rqbds) {
1171                 struct ptlrpc_request_buffer_desc *rqbd =
1172                         list_entry(tmp, struct ptlrpc_request_buffer_desc, 
1173                                    rqbd_list);
1174
1175                 rc = LNetMDUnlink(rqbd->rqbd_md_h);
1176                 LASSERT (rc == 0 || rc == -ENOENT);
1177         }
1178
1179         /* Wait for the network to release any buffers it's currently
1180          * filling */
1181         for (;;) {
1182                 spin_lock(&service->srv_lock);
1183                 rc = service->srv_nrqbd_receiving;
1184                 spin_unlock(&service->srv_lock);
1185
1186                 if (rc == 0)
1187                         break;
1188
1189                 /* Network access will complete in finite time but the HUGE
1190                  * timeout lets us CWARN for visibility of sluggish NALs */
1191                 lwi = LWI_TIMEOUT(cfs_time_seconds(300), NULL, NULL);
1192                 rc = l_wait_event(service->srv_waitq,
1193                                   service->srv_nrqbd_receiving == 0,
1194                                   &lwi);
1195                 if (rc == -ETIMEDOUT)
1196                         CWARN("Service %s waiting for request buffers\n",
1197                               service->srv_name);
1198         }
1199
1200         /* schedule all outstanding replies to terminate them */
1201         spin_lock(&service->srv_lock);
1202         while (!list_empty(&service->srv_active_replies)) {
1203                 struct ptlrpc_reply_state *rs =
1204                         list_entry(service->srv_active_replies.next,
1205                                    struct ptlrpc_reply_state, rs_list);
1206                 ptlrpc_schedule_difficult_reply(rs);
1207         }
1208         spin_unlock(&service->srv_lock);
1209
1210         /* purge the request queue.  NB No new replies (rqbds all unlinked)
1211          * and no service threads, so I'm the only thread noodling the
1212          * request queue now */
1213         while (!list_empty(&service->srv_request_queue)) {
1214                 struct ptlrpc_request *req =
1215                         list_entry(service->srv_request_queue.next,
1216                                    struct ptlrpc_request,
1217                                    rq_list);
1218
1219                 list_del(&req->rq_list);
1220                 service->srv_n_queued_reqs--;
1221                 service->srv_n_active_reqs++;
1222
1223                 ptlrpc_server_free_request(req);
1224         }
1225         LASSERT(service->srv_n_queued_reqs == 0);
1226         LASSERT(service->srv_n_active_reqs == 0);
1227         LASSERT(service->srv_n_history_rqbds == 0);
1228         LASSERT(list_empty(&service->srv_active_rqbds));
1229
1230         /* Now free all the request buffers since nothing references them
1231          * any more... */
1232         while (!list_empty(&service->srv_idle_rqbds)) {
1233                 struct ptlrpc_request_buffer_desc *rqbd =
1234                         list_entry(service->srv_idle_rqbds.next,
1235                                    struct ptlrpc_request_buffer_desc,
1236                                    rqbd_list);
1237
1238                 ptlrpc_free_rqbd(rqbd);
1239         }
1240
1241         /* wait for all outstanding replies to complete (they were
1242          * scheduled having been flagged to abort above) */
1243         while (atomic_read(&service->srv_outstanding_replies) != 0) {
1244                 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
1245
1246                 rc = l_wait_event(service->srv_waitq,
1247                                   !list_empty(&service->srv_reply_queue), &lwi);
1248                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1249
1250                 if (rc == 0) {
1251                         ptlrpc_server_handle_reply(service);
1252                         continue;
1253                 }
1254                 CWARN("Unexpectedly long timeout %p\n", service);
1255         }
1256
1257         list_for_each_entry_safe(rs, t, &service->srv_free_rs_list, rs_list) {
1258                 list_del(&rs->rs_list);
1259                 OBD_FREE(rs, service->srv_max_reply_size);
1260         }
1261
1262         OBD_FREE(service, sizeof(*service));
1263         return 0;
1264 }
1265
1266 /* Returns 0 if the service is healthy.
1267  *
1268  * Right now, it just checks to make sure that requests aren't languishing
1269  * in the queue.  We'll use this health check to govern whether a node needs
1270  * to be shot, so it's intentionally non-aggressive. */
1271 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
1272 {
1273         struct ptlrpc_request *request;
1274         struct timeval         right_now;
1275         long                   timediff, cutoff;
1276         int                    rc = 0;
1277
1278         if (svc == NULL)
1279                 return 0;
1280
1281         spin_lock(&svc->srv_lock);
1282
1283         if (list_empty(&svc->srv_request_queue))
1284                 goto out;
1285
1286         request = list_entry(svc->srv_request_queue.next,
1287                              struct ptlrpc_request, rq_list);
1288
1289         do_gettimeofday(&right_now);
1290         timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
1291
1292         cutoff = obd_health_check_timeout;
1293
1294         if (timediff / 1000000 > cutoff) {
1295                 rc = -1;
1296                 goto out;
1297         }
1298
1299  out:
1300         spin_unlock(&svc->srv_lock);
1301         return rc;
1302 }