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