Whamcloud - gitweb
1a5730e01ff52c67ef9fde03ecfce9691e7c066f
[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 #endif
30 #include <obd_support.h>
31 #include <obd_class.h>
32 #include <lustre_net.h>
33 #include <lu_object.h>
34 #include <lnet/types.h>
35 #include "ptlrpc_internal.h"
36
37 /* The following are visible and mutable through /sys/module/ptlrpc */
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 unsigned int at_min = 0;
42 CFS_MODULE_PARM(at_min, "i", int, 0644,
43                 "Adaptive timeout minimum (sec)");
44 unsigned int at_max = 600;
45 EXPORT_SYMBOL(at_max);
46 CFS_MODULE_PARM(at_max, "i", int, 0644,
47                 "Adaptive timeout maximum (sec)");
48 unsigned int at_history = 600;
49 CFS_MODULE_PARM(at_history, "i", int, 0644,
50                 "Adaptive timeouts remember the slowest event that took place "
51                 "within this period (sec)");
52 static int at_early_margin = 5;
53 CFS_MODULE_PARM(at_early_margin, "i", int, 0644,
54                 "How soon before an RPC deadline to send an early reply");
55 static int at_extra = 30;
56 CFS_MODULE_PARM(at_extra, "i", int, 0644,
57                 "How much extra time to give with each early reply");
58
59
60 /* forward ref */
61 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
62
63 static CFS_LIST_HEAD(ptlrpc_all_services);
64 spinlock_t ptlrpc_all_services_lock;
65
66 static char *
67 ptlrpc_alloc_request_buffer (int size)
68 {
69         char *ptr;
70
71         if (size > SVC_BUF_VMALLOC_THRESHOLD)
72                 OBD_VMALLOC(ptr, size);
73         else
74                 OBD_ALLOC(ptr, size);
75
76         return (ptr);
77 }
78
79 static void
80 ptlrpc_free_request_buffer (char *ptr, int size)
81 {
82         if (size > SVC_BUF_VMALLOC_THRESHOLD)
83                 OBD_VFREE(ptr, size);
84         else
85                 OBD_FREE(ptr, size);
86 }
87
88 struct ptlrpc_request_buffer_desc *
89 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
90 {
91         struct ptlrpc_request_buffer_desc *rqbd;
92
93         OBD_ALLOC_PTR(rqbd);
94         if (rqbd == NULL)
95                 return (NULL);
96
97         rqbd->rqbd_service = svc;
98         rqbd->rqbd_refcount = 0;
99         rqbd->rqbd_cbid.cbid_fn = request_in_callback;
100         rqbd->rqbd_cbid.cbid_arg = rqbd;
101         CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
102         rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
103
104         if (rqbd->rqbd_buffer == NULL) {
105                 OBD_FREE_PTR(rqbd);
106                 return (NULL);
107         }
108
109         spin_lock(&svc->srv_lock);
110         list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
111         svc->srv_nbufs++;
112         spin_unlock(&svc->srv_lock);
113
114         return (rqbd);
115 }
116
117 void
118 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
119 {
120         struct ptlrpc_service *svc = rqbd->rqbd_service;
121
122         LASSERT (rqbd->rqbd_refcount == 0);
123         LASSERT (list_empty(&rqbd->rqbd_reqs));
124
125         spin_lock(&svc->srv_lock);
126         list_del(&rqbd->rqbd_list);
127         svc->srv_nbufs--;
128         spin_unlock(&svc->srv_lock);
129
130         ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
131         OBD_FREE_PTR(rqbd);
132 }
133
134 int
135 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
136 {
137         struct ptlrpc_request_buffer_desc *rqbd;
138         int                                i;
139
140         CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
141                svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
142                svc->srv_nrqbd_receiving, svc->srv_nbufs);
143         for (i = 0; i < svc->srv_nbuf_per_group; i++) {
144                 rqbd = ptlrpc_alloc_rqbd(svc);
145
146                 if (rqbd == NULL) {
147                         CERROR ("%s: Can't allocate request buffer\n",
148                                 svc->srv_name);
149                         return (-ENOMEM);
150                 }
151
152                 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
153                         return (-EAGAIN);
154         }
155
156         return (0);
157 }
158
159 void
160 ptlrpc_save_lock (struct ptlrpc_request *req,
161                   struct lustre_handle *lock, int mode)
162 {
163         struct ptlrpc_reply_state *rs = req->rq_reply_state;
164         int                        idx;
165
166         LASSERT(rs != NULL);
167         LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
168
169         idx = rs->rs_nlocks++;
170         rs->rs_locks[idx] = *lock;
171         rs->rs_modes[idx] = mode;
172         rs->rs_difficult = 1;
173 }
174
175 void
176 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
177 {
178         struct ptlrpc_service *svc = rs->rs_service;
179
180 #ifdef CONFIG_SMP
181         LASSERT (spin_is_locked (&svc->srv_lock));
182 #endif
183         LASSERT (rs->rs_difficult);
184         rs->rs_scheduled_ever = 1;              /* flag any notification attempt */
185
186         if (rs->rs_scheduled)                   /* being set up or already notified */
187                 return;
188
189         rs->rs_scheduled = 1;
190         list_del (&rs->rs_list);
191         list_add (&rs->rs_list, &svc->srv_reply_queue);
192         cfs_waitq_signal (&svc->srv_waitq);
193 }
194
195 void
196 ptlrpc_commit_replies (struct obd_device *obd)
197 {
198         struct list_head   *tmp;
199         struct list_head   *nxt;
200
201         /* Find any replies that have been committed and get their service
202          * to attend to complete them. */
203
204         /* CAVEAT EMPTOR: spinlock ordering!!! */
205         spin_lock(&obd->obd_uncommitted_replies_lock);
206
207         list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
208                 struct ptlrpc_reply_state *rs =
209                         list_entry(tmp, struct ptlrpc_reply_state, rs_obd_list);
210
211                 LASSERT (rs->rs_difficult);
212
213                 if (rs->rs_transno <= obd->obd_last_committed) {
214                         struct ptlrpc_service *svc = rs->rs_service;
215
216                         spin_lock (&svc->srv_lock);
217                         list_del_init (&rs->rs_obd_list);
218                         ptlrpc_schedule_difficult_reply (rs);
219                         spin_unlock (&svc->srv_lock);
220                 }
221         }
222
223         spin_unlock(&obd->obd_uncommitted_replies_lock);
224 }
225
226 static int
227 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
228 {
229         struct ptlrpc_request_buffer_desc *rqbd;
230         int                                rc;
231         int                                posted = 0;
232
233         for (;;) {
234                 spin_lock(&svc->srv_lock);
235
236                 if (list_empty (&svc->srv_idle_rqbds)) {
237                         spin_unlock(&svc->srv_lock);
238                         return (posted);
239                 }
240
241                 rqbd = list_entry(svc->srv_idle_rqbds.next,
242                                   struct ptlrpc_request_buffer_desc,
243                                   rqbd_list);
244                 list_del (&rqbd->rqbd_list);
245
246                 /* assume we will post successfully */
247                 svc->srv_nrqbd_receiving++;
248                 list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
249
250                 spin_unlock(&svc->srv_lock);
251
252                 rc = ptlrpc_register_rqbd(rqbd);
253                 if (rc != 0)
254                         break;
255
256                 posted = 1;
257         }
258
259         spin_lock(&svc->srv_lock);
260
261         svc->srv_nrqbd_receiving--;
262         list_del(&rqbd->rqbd_list);
263         list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
264
265         /* Don't complain if no request buffers are posted right now; LNET
266          * won't drop requests because we set the portal lazy! */
267
268         spin_unlock(&svc->srv_lock);
269
270         return (-1);
271 }
272
273 struct ptlrpc_service *ptlrpc_init_svc_conf(struct ptlrpc_service_conf *c,
274                                             svc_handler_t h, char *name,
275                                             struct proc_dir_entry *proc_entry,
276                                             svcreq_printfn_t prntfn,
277                                             char *threadname)
278 {
279         return ptlrpc_init_svc(c->psc_nbufs, c->psc_bufsize,
280                                c->psc_max_req_size, c->psc_max_reply_size,
281                                c->psc_req_portal, c->psc_rep_portal,
282                                c->psc_watchdog_factor,
283                                h, name, proc_entry,
284                                prntfn, c->psc_min_threads, c->psc_max_threads,
285                                threadname, c->psc_ctx_tags);
286 }
287 EXPORT_SYMBOL(ptlrpc_init_svc_conf);
288
289 static void ptlrpc_at_timer(unsigned long castmeharder)
290 {
291         struct ptlrpc_service *svc = (struct ptlrpc_service *)castmeharder;
292         CDEBUG(D_INFO, "at timer %s hit at %ld%s\n",
293                svc->srv_name, cfs_time_current_sec(),
294                list_empty(&svc->srv_at_list) ? ", empty" : "");
295         svc->srv_at_check = 1;
296         svc->srv_at_checktime = cfs_time_current();
297         cfs_waitq_signal(&svc->srv_waitq);
298 }
299
300 /* @threadname should be 11 characters or less - 3 will be added on */
301 struct ptlrpc_service *
302 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
303                 int req_portal, int rep_portal, int watchdog_factor,
304                 svc_handler_t handler, char *name,
305                 cfs_proc_dir_entry_t *proc_entry,
306                 svcreq_printfn_t svcreq_printfn,
307                 int min_threads, int max_threads,
308                 char *threadname, __u32 ctx_tags)
309 {
310         int                    rc;
311         struct ptlrpc_service *service;
312         ENTRY;
313
314         LASSERT (nbufs > 0);
315         LASSERT (bufsize >= max_req_size + SPTLRPC_MAX_PAYLOAD);
316         LASSERT (ctx_tags != 0);
317
318         OBD_ALLOC_PTR(service);
319         if (service == NULL)
320                 RETURN(NULL);
321
322         /* First initialise enough for early teardown */
323
324         service->srv_name = name;
325         spin_lock_init(&service->srv_lock);
326         CFS_INIT_LIST_HEAD(&service->srv_threads);
327         cfs_waitq_init(&service->srv_waitq);
328
329         service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
330         service->srv_max_req_size = max_req_size + SPTLRPC_MAX_PAYLOAD;
331         service->srv_buf_size = bufsize;
332         service->srv_rep_portal = rep_portal;
333         service->srv_req_portal = req_portal;
334         service->srv_watchdog_factor = watchdog_factor;
335         service->srv_handler = handler;
336         service->srv_request_history_print_fn = svcreq_printfn;
337         service->srv_request_seq = 1;           /* valid seq #s start at 1 */
338         service->srv_request_max_cull_seq = 0;
339         service->srv_threads_min = min_threads;
340         service->srv_threads_max = max_threads;
341         service->srv_thread_name = threadname;
342         service->srv_ctx_tags = ctx_tags;
343
344         rc = LNetSetLazyPortal(service->srv_req_portal);
345         LASSERT (rc == 0);
346
347         CFS_INIT_LIST_HEAD(&service->srv_request_queue);
348         CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
349         CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
350         CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
351         CFS_INIT_LIST_HEAD(&service->srv_request_history);
352         CFS_INIT_LIST_HEAD(&service->srv_active_replies);
353         CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
354         CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
355         cfs_waitq_init(&service->srv_free_rs_waitq);
356
357         spin_lock_init(&service->srv_at_lock);
358         CFS_INIT_LIST_HEAD(&service->srv_req_in_queue);
359         CFS_INIT_LIST_HEAD(&service->srv_at_list);
360         cfs_timer_init(&service->srv_at_timer, ptlrpc_at_timer, service);
361         /* At SOW, service time should be quick; 10s seems generous. If client
362            timeout is less than this, we'll be sending an early reply. */
363         at_init(&service->srv_at_estimate, 10, 0);
364
365         spin_lock (&ptlrpc_all_services_lock);
366         list_add (&service->srv_list, &ptlrpc_all_services);
367         spin_unlock (&ptlrpc_all_services_lock);
368
369         /* Now allocate the request buffers */
370         rc = ptlrpc_grow_req_bufs(service);
371         /* We shouldn't be under memory pressure at startup, so
372          * fail if we can't post all our buffers at this time. */
373         if (rc != 0)
374                 GOTO(failed, NULL);
375
376         /* Now allocate pool of reply buffers */
377         /* Increase max reply size to next power of two */
378         service->srv_max_reply_size = 1;
379         while (service->srv_max_reply_size <
380                max_reply_size + SPTLRPC_MAX_PAYLOAD)
381                 service->srv_max_reply_size <<= 1;
382
383         if (proc_entry != NULL)
384                 ptlrpc_lprocfs_register_service(proc_entry, service);
385
386         CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
387                service->srv_name, service->srv_req_portal);
388
389         RETURN(service);
390 failed:
391         ptlrpc_unregister_service(service);
392         return NULL;
393 }
394
395 static void ptlrpc_server_req_decref(struct ptlrpc_request *req)
396 {
397         struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
398
399         if (!atomic_dec_and_test(&req->rq_refcount))
400                 return;
401
402         sptlrpc_svc_ctx_decref(req);
403
404         LASSERT(list_empty(&req->rq_timed_list));
405         if (req != &rqbd->rqbd_req) {
406                 /* NB request buffers use an embedded
407                  * req if the incoming req unlinked the
408                  * MD; this isn't one of them! */
409                 OBD_FREE(req, sizeof(*req));
410         } else {
411                 struct ptlrpc_service *svc = rqbd->rqbd_service;
412                 /* schedule request buffer for re-use.
413                  * NB I can only do this after I've disposed of their
414                  * reqs; particularly the embedded req */
415                 spin_lock(&svc->srv_lock);
416                 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
417                 spin_unlock(&svc->srv_lock);
418         }
419 }
420
421 static void __ptlrpc_server_free_request(struct ptlrpc_request *req)
422 {
423         list_del(&req->rq_list);
424         ptlrpc_req_drop_rs(req);
425         ptlrpc_server_req_decref(req);
426 }
427
428 static void
429 ptlrpc_server_free_request(struct ptlrpc_request *req)
430 {
431         struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
432         struct ptlrpc_service             *svc = rqbd->rqbd_service;
433         int                                refcount;
434         struct list_head                  *tmp;
435         struct list_head                  *nxt;
436
437         if (req->rq_phase != RQ_PHASE_NEW) /* incorrect message magic */
438                 DEBUG_REQ(D_INFO, req, "free req");
439         spin_lock(&svc->srv_at_lock);
440         req->rq_sent_final = 1;
441         list_del_init(&req->rq_timed_list);
442         spin_unlock(&svc->srv_at_lock);
443
444         spin_lock(&svc->srv_lock);
445
446         svc->srv_n_active_reqs--;
447         list_add(&req->rq_list, &rqbd->rqbd_reqs);
448
449         refcount = --(rqbd->rqbd_refcount);
450         if (refcount == 0) {
451                 /* request buffer is now idle: add to history */
452                 list_del(&rqbd->rqbd_list);
453                 list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
454                 svc->srv_n_history_rqbds++;
455
456                 /* cull some history?
457                  * I expect only about 1 or 2 rqbds need to be recycled here */
458                 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
459                         rqbd = list_entry(svc->srv_history_rqbds.next,
460                                           struct ptlrpc_request_buffer_desc,
461                                           rqbd_list);
462
463                         list_del(&rqbd->rqbd_list);
464                         svc->srv_n_history_rqbds--;
465
466                         /* remove rqbd's reqs from svc's req history while
467                          * I've got the service lock */
468                         list_for_each(tmp, &rqbd->rqbd_reqs) {
469                                 req = list_entry(tmp, struct ptlrpc_request,
470                                                  rq_list);
471                                 /* Track the highest culled req seq */
472                                 if (req->rq_history_seq >
473                                     svc->srv_request_max_cull_seq)
474                                         svc->srv_request_max_cull_seq =
475                                                 req->rq_history_seq;
476                                 list_del(&req->rq_history_list);
477                         }
478
479                         spin_unlock(&svc->srv_lock);
480
481                         list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
482                                 req = list_entry(rqbd->rqbd_reqs.next,
483                                                  struct ptlrpc_request,
484                                                  rq_list);
485                                 __ptlrpc_server_free_request(req);
486                         }
487
488                         spin_lock(&svc->srv_lock);
489                 }
490         } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
491                  /* If we are low on memory, we are not interested in
492                     history */
493                 list_del(&req->rq_history_list);
494                 __ptlrpc_server_free_request(req);
495         }
496
497         spin_unlock(&svc->srv_lock);
498
499 }
500
501 /* This function makes sure dead exports are evicted in a timely manner.
502    This function is only called when some export receives a message (i.e.,
503    the network is up.) */
504 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
505 {
506         struct obd_export *oldest_exp;
507         time_t oldest_time;
508
509         ENTRY;
510
511         LASSERT(exp);
512
513         /* Compensate for slow machines, etc, by faking our request time
514            into the future.  Although this can break the strict time-ordering
515            of the list, we can be really lazy here - we don't have to evict
516            at the exact right moment.  Eventually, all silent exports
517            will make it to the top of the list. */
518         exp->exp_last_request_time = max(exp->exp_last_request_time,
519                                          cfs_time_current_sec() + extra_delay);
520
521         CDEBUG(D_HA, "updating export %s at "CFS_TIME_T" exp %p\n",
522                exp->exp_client_uuid.uuid,
523                exp->exp_last_request_time, exp);
524
525         /* exports may get disconnected from the chain even though the
526            export has references, so we must keep the spin lock while
527            manipulating the lists */
528         spin_lock(&exp->exp_obd->obd_dev_lock);
529
530         if (list_empty(&exp->exp_obd_chain_timed)) {
531                 /* this one is not timed */
532                 spin_unlock(&exp->exp_obd->obd_dev_lock);
533                 EXIT;
534                 return;
535         }
536
537         list_move_tail(&exp->exp_obd_chain_timed,
538                        &exp->exp_obd->obd_exports_timed);
539
540         oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
541                                 struct obd_export, exp_obd_chain_timed);
542         oldest_time = oldest_exp->exp_last_request_time;
543         spin_unlock(&exp->exp_obd->obd_dev_lock);
544
545         if (exp->exp_obd->obd_recovering) {
546                 /* be nice to everyone during recovery */
547                 EXIT;
548                 return;
549         }
550
551         /* Note - racing to start/reset the obd_eviction timer is safe */
552         if (exp->exp_obd->obd_eviction_timer == 0) {
553                 /* Check if the oldest entry is expired. */
554                 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
555                                               extra_delay)) {
556                         /* We need a second timer, in case the net was down and
557                          * it just came back. Since the pinger may skip every
558                          * other PING_INTERVAL (see note in ptlrpc_pinger_main),
559                          * we better wait for 3. */
560                         exp->exp_obd->obd_eviction_timer =
561                                 cfs_time_current_sec() + 3 * PING_INTERVAL;
562                         CDEBUG(D_HA, "%s: Think about evicting %s from "CFS_TIME_T"\n",
563                                exp->exp_obd->obd_name, obd_export_nid2str(exp),
564                                oldest_time);
565                 }
566         } else {
567                 if (cfs_time_current_sec() >
568                     (exp->exp_obd->obd_eviction_timer + extra_delay)) {
569                         /* The evictor won't evict anyone who we've heard from
570                          * recently, so we don't have to check before we start
571                          * it. */
572                         if (!ping_evictor_wake(exp))
573                                 exp->exp_obd->obd_eviction_timer = 0;
574                 }
575         }
576
577         EXIT;
578 }
579
580 #ifndef __KERNEL__
581 int lu_context_init(struct lu_context *ctx, __u32 tags)
582 {
583         return 0;
584 }
585
586 void lu_context_fini(struct lu_context *ctx)
587 {
588 }
589
590 void lu_context_enter(struct lu_context *ctx)
591 {
592 }
593
594 void lu_context_exit(struct lu_context *ctx)
595 {
596 }
597
598 #endif
599
600 static int ptlrpc_check_req(struct ptlrpc_request *req)
601 {
602         if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
603                      req->rq_export->exp_conn_cnt)) {
604                 DEBUG_REQ(D_ERROR, req,
605                           "DROPPING req from old connection %d < %d",
606                           lustre_msg_get_conn_cnt(req->rq_reqmsg),
607                           req->rq_export->exp_conn_cnt);
608                 return -EEXIST;
609         }
610         if (unlikely(req->rq_export->exp_obd &&
611                      req->rq_export->exp_obd->obd_fail)) {
612              /* Failing over, don't handle any more reqs, send
613                 error response instead. */
614                 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
615                        req, req->rq_export->exp_obd->obd_name);
616                 req->rq_status = -ENODEV;
617                 ptlrpc_error(req);
618                 return -ENODEV;
619         }
620
621         return 0;
622 }
623
624 static void ptlrpc_at_set_timer(struct ptlrpc_service *svc)
625 {
626         struct ptlrpc_request *rq;
627         __s32 next;
628
629         spin_lock(&svc->srv_at_lock);
630         if (list_empty(&svc->srv_at_list)) {
631                 cfs_timer_disarm(&svc->srv_at_timer);
632                 spin_unlock(&svc->srv_at_lock);
633                 return;
634         }
635
636         /* Set timer for closest deadline */
637         rq = list_entry(svc->srv_at_list.next, struct ptlrpc_request,
638                         rq_timed_list);
639         next = (__s32)(rq->rq_deadline - cfs_time_current_sec() - 
640                        at_early_margin);
641         if (next <= 0)
642                 ptlrpc_at_timer((unsigned long)svc);
643         else
644                 cfs_timer_arm(&svc->srv_at_timer, cfs_time_shift(next));
645         spin_unlock(&svc->srv_at_lock);
646         CDEBUG(D_INFO, "armed %s at %+lds\n", svc->srv_name, next);
647 }
648
649 /* Add rpc to early reply check list */
650 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
651 {
652         struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
653         struct ptlrpc_request *rq;
654         int found = 0;
655
656         if (AT_OFF)
657                 return(0);
658
659         if (req->rq_no_reply)
660                 return 0;
661
662         if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
663                 return(-ENOSYS);
664
665         DEBUG_REQ(D_ADAPTTO, req, "add timed %lds",
666                   req->rq_deadline - cfs_time_current_sec());
667
668         spin_lock(&svc->srv_at_lock);
669
670         if (unlikely(req->rq_sent_final)) {
671                 spin_unlock(&svc->srv_at_lock);
672                 return 0;
673         }
674
675         LASSERT(list_empty(&req->rq_timed_list));
676         /* Add to sorted list.  Presumably latest rpcs will have the latest
677            deadlines, so search backward. */
678         list_for_each_entry_reverse(rq, &svc->srv_at_list, rq_timed_list) {
679                 if (req->rq_deadline > rq->rq_deadline) {
680                         list_add(&req->rq_timed_list, &rq->rq_timed_list);
681                         found++;
682                         break;
683                 }
684         }
685         if (!found)
686                 /* Add to front if shortest deadline or list empty */
687                 list_add(&req->rq_timed_list, &svc->srv_at_list);
688
689         /* Check if we're the head of the list */
690         found = (svc->srv_at_list.next == &req->rq_timed_list);
691
692         spin_unlock(&svc->srv_at_lock);
693
694         if (found)
695                 ptlrpc_at_set_timer(svc);
696
697         return 0;
698 }
699
700 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req,
701                                       int extra_time)
702 {
703         struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
704         struct ptlrpc_request *reqcopy;
705         struct lustre_msg *reqmsg;
706         long olddl = req->rq_deadline - cfs_time_current_sec();
707         time_t newdl;
708         int rc;
709         ENTRY;
710
711         /* deadline is when the client expects us to reply, margin is the
712            difference between clients' and servers' expectations */
713         DEBUG_REQ(D_ADAPTTO, req,
714                   "%ssending early reply (deadline %+lds, margin %+lds) for "
715                   "%d+%d", AT_OFF ? "AT off - not " : "",
716                   olddl, olddl - at_get(&svc->srv_at_estimate),
717                   at_get(&svc->srv_at_estimate), extra_time);
718
719         if (AT_OFF)
720                 RETURN(0);
721
722         if (olddl < 0) {
723                 CDEBUG(D_WARNING, "x"LPU64": Already past deadline (%+lds), not"
724                        " sending early reply. Increase at_early_margin (%d)?\n",
725                        req->rq_xid, olddl, at_early_margin);
726                 /* Return an error so we're not re-added to the timed list. */
727                 RETURN(-ETIMEDOUT);
728         }
729
730         if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
731                 CDEBUG(D_INFO, "Wanted to ask client for more time, but no AT "
732                       "support\n");
733                 RETURN(-ENOSYS);
734         }
735
736         if (extra_time) {
737                 /* Fake our processing time into the future to ask the
738                    clients for some extra amount of time */
739                 extra_time += cfs_time_current_sec() -
740                         req->rq_arrival_time.tv_sec;
741                 at_add(&svc->srv_at_estimate, extra_time);
742         }
743
744         newdl = req->rq_arrival_time.tv_sec + at_get(&svc->srv_at_estimate);
745         if (req->rq_deadline >= newdl) {
746                 /* We're not adding any time, no need to send an early reply
747                    (e.g. maybe at adaptive_max) */
748                 CDEBUG(D_ADAPTTO, "x"LPU64": Couldn't add any time (%ld/%ld), "
749                        "not sending early reply\n", req->rq_xid, olddl,
750                        newdl - cfs_time_current_sec());
751                 RETURN(-ETIMEDOUT);
752         }
753
754         OBD_ALLOC(reqcopy, sizeof *reqcopy);
755         if (reqcopy == NULL)
756                 RETURN(-ENOMEM);
757         OBD_ALLOC(reqmsg, req->rq_reqlen);
758         if (!reqmsg) {
759                 OBD_FREE(reqcopy, sizeof *reqcopy);
760                 RETURN(-ENOMEM);
761         }
762
763         *reqcopy = *req;
764         reqcopy->rq_reply_state = NULL;
765         reqcopy->rq_rep_swab_mask = 0;
766         reqcopy->rq_pack_bulk = 0;
767         reqcopy->rq_pack_udesc = 0;
768         reqcopy->rq_packed_final = 0;
769         sptlrpc_svc_ctx_addref(reqcopy);
770         /* We only need the reqmsg for the magic */
771         reqcopy->rq_reqmsg = reqmsg;
772         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
773
774         if (req->rq_sent_final) {
775                 CDEBUG(D_ADAPTTO, "x"LPU64": normal reply already sent out, "
776                        "abort sending early reply\n", req->rq_xid);
777                 GOTO(out, rc = 0);
778         }
779
780         /* Connection ref */
781         reqcopy->rq_export = class_conn2export(
782                                      lustre_msg_get_handle(reqcopy->rq_reqmsg));
783         if (reqcopy->rq_export == NULL)
784                 GOTO(out, rc = -ENODEV);
785
786         /* RPC ref */
787         class_export_rpc_get(reqcopy->rq_export);
788         if (reqcopy->rq_export->exp_obd &&
789             reqcopy->rq_export->exp_obd->obd_fail)
790                 GOTO(out_put, rc = -ENODEV);
791
792         rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
793         if (rc)
794                 GOTO(out_put, rc);
795
796         rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
797
798         if (!rc) {
799                 /* Adjust our own deadline to what we told the client */
800                 req->rq_deadline = newdl;
801                 req->rq_early_count++; /* number sent, server side */
802         } else {
803                 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
804         }
805
806         /* Free the (early) reply state from lustre_pack_reply.
807            (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
808         ptlrpc_req_drop_rs(reqcopy);
809
810 out_put:
811         class_export_rpc_put(reqcopy->rq_export);
812         class_export_put(reqcopy->rq_export);
813 out:
814         sptlrpc_svc_ctx_decref(reqcopy);
815         OBD_FREE(reqmsg, req->rq_reqlen);
816         OBD_FREE(reqcopy, sizeof *reqcopy);
817         RETURN(rc);
818 }
819
820 /* Send early replies to everybody expiring within at_early_margin
821    asking for at_extra time */
822 static int ptlrpc_at_check_timed(struct ptlrpc_service *svc)
823 {
824         struct ptlrpc_request *rq, *n;
825         struct list_head work_list;
826         time_t now = cfs_time_current_sec();
827         cfs_duration_t delay;
828         int first, counter = 0;
829         ENTRY;
830
831         spin_lock(&svc->srv_at_lock);
832         if (svc->srv_at_check == 0) {
833                 spin_unlock(&svc->srv_at_lock);
834                 RETURN(0);
835         }
836         delay = cfs_time_sub(cfs_time_current(), svc->srv_at_checktime);
837         svc->srv_at_check = 0;
838
839         if (list_empty(&svc->srv_at_list)) {
840                 spin_unlock(&svc->srv_at_lock);
841                 RETURN(0);
842         }
843
844         /* The timer went off, but maybe the nearest rpc already completed. */
845         rq = list_entry(svc->srv_at_list.next, struct ptlrpc_request,
846                         rq_timed_list);
847         first = (int)(rq->rq_deadline - now);
848         if (first > at_early_margin) {
849                 /* We've still got plenty of time.  Reset the timer. */
850                 spin_unlock(&svc->srv_at_lock);
851                 ptlrpc_at_set_timer(svc);
852                 RETURN(0);
853         }
854
855         /* We're close to a timeout, and we don't know how much longer the
856            server will take. Send early replies to everyone expiring soon. */
857         CFS_INIT_LIST_HEAD(&work_list);
858         list_for_each_entry_safe(rq, n, &svc->srv_at_list, rq_timed_list) {
859                 if (rq->rq_deadline <= now + at_early_margin) {
860                         list_move_tail(&rq->rq_timed_list, &work_list);
861                         counter++;
862                 } else {
863                         break;
864                 }
865         }
866
867         spin_unlock(&svc->srv_at_lock);
868
869         /* we have a new earliest deadline, restart the timer */
870         ptlrpc_at_set_timer(svc);
871
872         CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
873                "replies\n", first, at_extra, counter);
874         if (first < 0) {
875                 /* We're already past request deadlines before we even get a
876                    chance to send early replies */
877                 LCONSOLE_WARN("%s: This server is not able to keep up with "
878                               "request traffic (cpu-bound).\n", svc->srv_name);
879                 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
880                       "delay="CFS_DURATION_T"(jiff)\n",
881                       counter, svc->srv_n_queued_reqs, svc->srv_n_active_reqs,
882                       at_get(&svc->srv_at_estimate), delay);
883         }
884
885         /* ptlrpc_server_free_request may delete an entry out of the work
886            list */
887         spin_lock(&svc->srv_at_lock);
888         while (!list_empty(&work_list)) {
889                 rq = list_entry(work_list.next, struct ptlrpc_request,
890                                 rq_timed_list);
891                 list_del_init(&rq->rq_timed_list);
892                 /* if the entry is still in the worklist, it hasn't been
893                    deleted, and is safe to take a ref to keep the req around */
894                 atomic_inc(&rq->rq_refcount);
895                 spin_unlock(&svc->srv_at_lock);
896
897                 if (ptlrpc_at_send_early_reply(rq, at_extra) == 0)
898                         ptlrpc_at_add_timed(rq);
899
900                 ptlrpc_server_req_decref(rq);
901                 spin_lock(&svc->srv_at_lock);
902         }
903         spin_unlock(&svc->srv_at_lock);
904
905         RETURN(0);
906 }
907
908 /* Handle freshly incoming reqs, add to timed early reply list,
909    pass on to regular request queue */
910 static int
911 ptlrpc_server_handle_req_in(struct ptlrpc_service *svc)
912 {
913         struct ptlrpc_request *req;
914         __u32                  deadline;
915         int                    rc;
916         ENTRY;
917
918         LASSERT(svc);
919
920         spin_lock(&svc->srv_lock);
921         if (list_empty(&svc->srv_req_in_queue)) {
922                 spin_unlock(&svc->srv_lock);
923                 RETURN(0);
924         }
925
926         req = list_entry(svc->srv_req_in_queue.next,
927                          struct ptlrpc_request, rq_list);
928         list_del_init (&req->rq_list);
929         /* Consider this still a "queued" request as far as stats are
930            concerned */
931         spin_unlock(&svc->srv_lock);
932
933         /* go through security check/transform */
934         rc = sptlrpc_svc_unwrap_request(req);
935         switch (rc) {
936         case SECSVC_OK:
937                 break;
938         case SECSVC_COMPLETE:
939                 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
940                 goto err_req;
941         case SECSVC_DROP:
942                 goto err_req;
943         default:
944                 LBUG();
945         }
946
947         /* Clear request swab mask; this is a new request */
948         req->rq_req_swab_mask = 0;
949
950         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
951         if (rc != 0) {
952                 CERROR("error unpacking request: ptl %d from %s x"LPU64"\n",
953                        svc->srv_req_portal, libcfs_id2str(req->rq_peer),
954                        req->rq_xid);
955                 goto err_req;
956         }
957
958         rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
959         if (rc) {
960                 CERROR ("error unpacking ptlrpc body: ptl %d from %s x"
961                         LPU64"\n", svc->srv_req_portal,
962                         libcfs_id2str(req->rq_peer), req->rq_xid);
963                 goto err_req;
964         }
965
966         rc = -EINVAL;
967         if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
968                 CERROR("wrong packet type received (type=%u) from %s\n",
969                        lustre_msg_get_type(req->rq_reqmsg),
970                        libcfs_id2str(req->rq_peer));
971                 goto err_req;
972         }
973
974         CDEBUG(D_NET, "got req "LPD64"\n", req->rq_xid);
975
976         req->rq_export = class_conn2export(
977                 lustre_msg_get_handle(req->rq_reqmsg));
978         if (req->rq_export) {
979                 rc = ptlrpc_check_req(req);
980                 if (rc == 0) {
981                         rc = sptlrpc_target_export_check(req->rq_export, req);
982                         if (rc)
983                                 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
984                                           "illegal security flavor,");
985                 }
986
987                 class_export_put(req->rq_export);
988                 req->rq_export = NULL;
989                 if (rc)
990                         goto err_req;
991         }
992
993         /* req_in handling should/must be fast */
994         if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
995                 DEBUG_REQ(D_WARNING, req, "Slow req_in handling %lus",
996                           cfs_time_current_sec() - req->rq_arrival_time.tv_sec);
997
998         /* Set rpc server deadline and add it to the timed list */
999         deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1000                     MSGHDR_AT_SUPPORT) ?
1001                    /* The max time the client expects us to take */
1002                    lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1003         req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1004         if (unlikely(deadline == 0)) {
1005                 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1006                 goto err_req;
1007         }
1008
1009         ptlrpc_at_add_timed(req);
1010
1011         /* Move it over to the request processing queue */
1012         spin_lock(&svc->srv_lock);
1013         list_add_tail(&req->rq_list, &svc->srv_request_queue);
1014         cfs_waitq_signal(&svc->srv_waitq);
1015         spin_unlock(&svc->srv_lock);
1016         RETURN(1);
1017
1018 err_req:
1019         spin_lock(&svc->srv_lock);
1020         svc->srv_n_queued_reqs--;
1021         svc->srv_n_active_reqs++;
1022         spin_unlock(&svc->srv_lock);
1023         ptlrpc_server_free_request(req);
1024
1025         RETURN(1);
1026 }
1027
1028 static int
1029 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
1030                              struct ptlrpc_thread *thread)
1031 {
1032         struct obd_export     *export = NULL;
1033         struct ptlrpc_request *request;
1034         struct timeval         work_start;
1035         struct timeval         work_end;
1036         long                   timediff;
1037         int                    rc;
1038         ENTRY;
1039
1040         LASSERT(svc);
1041
1042         spin_lock(&svc->srv_lock);
1043         if (unlikely(list_empty (&svc->srv_request_queue) ||
1044             (
1045 #ifndef __KERNEL__
1046              /* !@%$# liblustre only has 1 thread */
1047              svc->srv_n_difficult_replies != 0 &&
1048 #endif
1049              svc->srv_n_active_reqs >= (svc->srv_threads_running - 1)))) {
1050                  /* Don't handle regular requests in the last thread, in order               * re
1051                   * to handle difficult replies (which might block other threads)
1052                   * as well as handle any incoming reqs, early replies, etc.
1053                   * That means we always need at least 2 service threads. */
1054                 spin_unlock(&svc->srv_lock);
1055                 RETURN(0);
1056         }
1057
1058         request = list_entry (svc->srv_request_queue.next,
1059                               struct ptlrpc_request, rq_list);
1060         list_del_init (&request->rq_list);
1061         svc->srv_n_queued_reqs--;
1062         svc->srv_n_active_reqs++;
1063
1064         spin_unlock(&svc->srv_lock);
1065
1066         do_gettimeofday(&work_start);
1067         timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
1068         if (likely(svc->srv_stats != NULL)) {
1069                 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
1070                                     timediff);
1071                 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
1072                                     svc->srv_n_queued_reqs);
1073                 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
1074                                     svc->srv_n_active_reqs);
1075                 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
1076                                     at_get(&svc->srv_at_estimate));
1077         }
1078
1079         rc = lu_context_init(&request->rq_session, LCT_SESSION);
1080         if (rc) {
1081                 CERROR("Failure to initialize session: %d\n", rc);
1082                 goto out_req;
1083         }
1084         request->rq_session.lc_thread = thread;
1085         lu_context_enter(&request->rq_session);
1086
1087         CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
1088
1089         request->rq_svc_thread = thread;
1090         if (thread)
1091                 request->rq_svc_thread->t_env->le_ses = &request->rq_session;
1092
1093         request->rq_export = class_conn2export(
1094                                      lustre_msg_get_handle(request->rq_reqmsg));
1095
1096         if (likely(request->rq_export)) {
1097                 if (unlikely(ptlrpc_check_req(request)))
1098                         goto put_conn;
1099                 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
1100                 export = class_export_rpc_get(request->rq_export);
1101         }
1102
1103         /* Discard requests queued for longer than the deadline.
1104            The deadline is increased if we send an early reply. */
1105         if (cfs_time_current_sec() > request->rq_deadline) {
1106                 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
1107                           ": deadline %ld%+lds ago\n",
1108                           libcfs_id2str(request->rq_peer),
1109                           request->rq_deadline -
1110                           request->rq_arrival_time.tv_sec,
1111                           cfs_time_current_sec() - request->rq_deadline);
1112                 goto put_rpc_export;
1113         }
1114
1115         request->rq_phase = RQ_PHASE_INTERPRET;
1116
1117         CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
1118                "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1119                (request->rq_export ?
1120                 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1121                (request->rq_export ?
1122                 atomic_read(&request->rq_export->exp_refcount) : -99),
1123                lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1124                libcfs_id2str(request->rq_peer),
1125                lustre_msg_get_opc(request->rq_reqmsg));
1126
1127         OBD_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, obd_fail_val);
1128
1129         rc = svc->srv_handler(request);
1130
1131         request->rq_phase = RQ_PHASE_COMPLETE;
1132
1133         CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
1134                "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1135                (request->rq_export ?
1136                 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1137                (request->rq_export ?
1138                 atomic_read(&request->rq_export->exp_refcount) : -99),
1139                lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1140                libcfs_id2str(request->rq_peer),
1141                lustre_msg_get_opc(request->rq_reqmsg));
1142
1143 put_rpc_export:
1144         if (export != NULL)
1145                 class_export_rpc_put(export);
1146 put_conn:
1147         if (likely(request->rq_export != NULL))
1148                 class_export_put(request->rq_export);
1149
1150         lu_context_exit(&request->rq_session);
1151         lu_context_fini(&request->rq_session);
1152
1153         if (unlikely(cfs_time_current_sec() > request->rq_deadline)) {
1154                 DEBUG_REQ(D_WARNING, request, "Request x"LPU64" took longer "
1155                           "than estimated (%ld%+lds); client may timeout.",
1156                           request->rq_xid, request->rq_deadline -
1157                           request->rq_arrival_time.tv_sec,
1158                           cfs_time_current_sec() - request->rq_deadline);
1159         }
1160
1161         do_gettimeofday(&work_end);
1162         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1163         CDEBUG(D_RPCTRACE, "request x"LPU64" opc %u from %s processed in "
1164                "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
1165                request->rq_xid, lustre_msg_get_opc(request->rq_reqmsg),
1166                libcfs_id2str(request->rq_peer), timediff,
1167                cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
1168                request->rq_repmsg ? lustre_msg_get_transno(request->rq_repmsg) :
1169                request->rq_transno, request->rq_status,
1170                request->rq_repmsg ? lustre_msg_get_status(request->rq_repmsg):
1171                -999);
1172         if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
1173                 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
1174                 int opc = opcode_offset(op);
1175                 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
1176                         LASSERT(opc < LUSTRE_MAX_OPCODES);
1177                         lprocfs_counter_add(svc->srv_stats,
1178                                             opc + EXTRA_MAX_OPCODES,
1179                                             timediff);
1180                 }
1181         }
1182         if (unlikely(request->rq_early_count)) {
1183                 DEBUG_REQ(D_ADAPTTO, request,
1184                           "sent %d early replies before finishing in %lds",
1185                           request->rq_early_count,
1186                           work_end.tv_sec - request->rq_arrival_time.tv_sec);
1187         }
1188
1189 out_req:
1190         ptlrpc_server_free_request(request);
1191
1192         RETURN(1);
1193 }
1194
1195 static int
1196 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
1197 {
1198         struct ptlrpc_reply_state *rs;
1199         struct obd_export         *exp;
1200         struct obd_device         *obd;
1201         int                        nlocks;
1202         int                        been_handled;
1203         ENTRY;
1204
1205         spin_lock(&svc->srv_lock);
1206         if (list_empty (&svc->srv_reply_queue)) {
1207                 spin_unlock(&svc->srv_lock);
1208                 RETURN(0);
1209         }
1210
1211         rs = list_entry (svc->srv_reply_queue.next,
1212                          struct ptlrpc_reply_state, rs_list);
1213
1214         exp = rs->rs_export;
1215         obd = exp->exp_obd;
1216
1217         LASSERT (rs->rs_difficult);
1218         LASSERT (rs->rs_scheduled);
1219
1220         list_del_init (&rs->rs_list);
1221
1222         /* Disengage from notifiers carefully (lock order - irqrestore below!)*/
1223         spin_unlock(&svc->srv_lock);
1224
1225         spin_lock (&obd->obd_uncommitted_replies_lock);
1226         /* Noop if removed already */
1227         list_del_init (&rs->rs_obd_list);
1228         spin_unlock (&obd->obd_uncommitted_replies_lock);
1229
1230         spin_lock (&exp->exp_lock);
1231         /* Noop if removed already */
1232         list_del_init (&rs->rs_exp_list);
1233         spin_unlock (&exp->exp_lock);
1234
1235         spin_lock(&svc->srv_lock);
1236
1237         been_handled = rs->rs_handled;
1238         rs->rs_handled = 1;
1239
1240         nlocks = rs->rs_nlocks;                 /* atomic "steal", but */
1241         rs->rs_nlocks = 0;                      /* locks still on rs_locks! */
1242
1243         if (nlocks == 0 && !been_handled) {
1244                 /* If we see this, we should already have seen the warning
1245                  * in mds_steal_ack_locks()  */
1246                 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
1247                       " o%d NID %s\n",
1248                       rs,
1249                       rs->rs_xid, rs->rs_transno,
1250                       lustre_msg_get_opc(rs->rs_msg),
1251                       libcfs_nid2str(exp->exp_connection->c_peer.nid));
1252         }
1253
1254         if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1255                 spin_unlock(&svc->srv_lock);
1256
1257                 if (!been_handled && rs->rs_on_net) {
1258                         LNetMDUnlink(rs->rs_md_h);
1259                         /* Ignore return code; we're racing with
1260                          * completion... */
1261                 }
1262
1263                 while (nlocks-- > 0)
1264                         ldlm_lock_decref(&rs->rs_locks[nlocks],
1265                                          rs->rs_modes[nlocks]);
1266
1267                 spin_lock(&svc->srv_lock);
1268         }
1269
1270         rs->rs_scheduled = 0;
1271
1272         if (!rs->rs_on_net) {
1273                 /* Off the net */
1274                 svc->srv_n_difficult_replies--;
1275                 spin_unlock(&svc->srv_lock);
1276
1277                 class_export_put (exp);
1278                 rs->rs_export = NULL;
1279                 ptlrpc_rs_decref (rs);
1280                 atomic_dec (&svc->srv_outstanding_replies);
1281                 RETURN(1);
1282         }
1283
1284         /* still on the net; callback will schedule */
1285         spin_unlock(&svc->srv_lock);
1286         RETURN(1);
1287 }
1288
1289 #ifndef __KERNEL__
1290 /* FIXME make use of timeout later */
1291 int
1292 liblustre_check_services (void *arg)
1293 {
1294         int  did_something = 0;
1295         int  rc;
1296         struct list_head *tmp, *nxt;
1297         ENTRY;
1298
1299         /* I'm relying on being single threaded, not to have to lock
1300          * ptlrpc_all_services etc */
1301         list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
1302                 struct ptlrpc_service *svc =
1303                         list_entry (tmp, struct ptlrpc_service, srv_list);
1304
1305                 if (svc->srv_threads_running != 0)     /* I've recursed */
1306                         continue;
1307
1308                 /* service threads can block for bulk, so this limits us
1309                  * (arbitrarily) to recursing 1 stack frame per service.
1310                  * Note that the problem with recursion is that we have to
1311                  * unwind completely before our caller can resume. */
1312
1313                 svc->srv_threads_running++;
1314
1315                 do {
1316                         rc = ptlrpc_server_handle_req_in(svc);
1317                         rc |= ptlrpc_server_handle_reply(svc);
1318                         rc |= ptlrpc_at_check_timed(svc);
1319                         rc |= ptlrpc_server_handle_request(svc, NULL);
1320                         rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
1321                         did_something |= rc;
1322                 } while (rc);
1323
1324                 svc->srv_threads_running--;
1325         }
1326
1327         RETURN(did_something);
1328 }
1329 #define ptlrpc_stop_all_threads(s) do {} while (0)
1330
1331 #else /* __KERNEL__ */
1332
1333 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
1334 void ptlrpc_daemonize(char *name)
1335 {
1336         struct fs_struct *fs = current->fs;
1337
1338         atomic_inc(&fs->count);
1339         cfs_daemonize(name);
1340         exit_fs(cfs_current());
1341         current->fs = fs;
1342         ll_set_fs_pwd(current->fs, init_task.fs->pwdmnt, init_task.fs->pwd);
1343 }
1344
1345 static void
1346 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
1347 {
1348         int avail = svc->srv_nrqbd_receiving;
1349         int low_water = test_req_buffer_pressure ? 0 :
1350                         svc->srv_nbuf_per_group/2;
1351
1352         /* NB I'm not locking; just looking. */
1353
1354         /* CAVEAT EMPTOR: We might be allocating buffers here because we've
1355          * allowed the request history to grow out of control.  We could put a
1356          * sanity check on that here and cull some history if we need the
1357          * space. */
1358
1359         if (avail <= low_water)
1360                 ptlrpc_grow_req_bufs(svc);
1361
1362         if (svc->srv_stats)
1363                 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
1364                                     avail);
1365 }
1366
1367 static int
1368 ptlrpc_retry_rqbds(void *arg)
1369 {
1370         struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
1371
1372         svc->srv_rqbd_timeout = 0;
1373         return (-ETIMEDOUT);
1374 }
1375
1376 static int ptlrpc_main(void *arg)
1377 {
1378         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
1379         struct ptlrpc_service  *svc = data->svc;
1380         struct ptlrpc_thread   *thread = data->thread;
1381         struct obd_device      *dev = data->dev;
1382         struct ptlrpc_reply_state *rs;
1383         struct lc_watchdog     *watchdog;
1384 #ifdef WITH_GROUP_INFO
1385         struct group_info *ginfo = NULL;
1386 #endif
1387         struct lu_env env;
1388         int counter = 0, rc = 0;
1389         ENTRY;
1390
1391         ptlrpc_daemonize(data->name);
1392
1393 #if defined(HAVE_NODE_TO_CPUMASK) && defined(CONFIG_NUMA)
1394         /* we need to do this before any per-thread allocation is done so that
1395          * we get the per-thread allocations on local node.  bug 7342 */
1396         if (svc->srv_cpu_affinity) {
1397                 int cpu, num_cpu;
1398
1399                 for (cpu = 0, num_cpu = 0; cpu < num_possible_cpus(); cpu++) {
1400                         if (!cpu_online(cpu))
1401                                 continue;
1402                         if (num_cpu == thread->t_id % num_online_cpus())
1403                                 break;
1404                         num_cpu++;
1405                 }
1406                 set_cpus_allowed(cfs_current(), node_to_cpumask(cpu_to_node(cpu)));
1407         }
1408 #endif
1409
1410 #ifdef WITH_GROUP_INFO
1411         ginfo = groups_alloc(0);
1412         if (!ginfo) {
1413                 rc = -ENOMEM;
1414                 goto out;
1415         }
1416
1417         set_current_groups(ginfo);
1418         put_group_info(ginfo);
1419 #endif
1420
1421         if (svc->srv_init != NULL) {
1422                 rc = svc->srv_init(thread);
1423                 if (rc)
1424                         goto out;
1425         }
1426
1427         rc = lu_context_init(&env.le_ctx, svc->srv_ctx_tags);
1428         if (rc)
1429                 goto out_srv_fini;
1430
1431         thread->t_env = &env;
1432         env.le_ctx.lc_thread = thread;
1433
1434         /* Alloc reply state structure for this one */
1435         OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
1436         if (!rs) {
1437                 rc = -ENOMEM;
1438                 goto out_srv_fini;
1439         }
1440
1441         /* Record that the thread is running */
1442         thread->t_flags = SVC_RUNNING;
1443         /*
1444          * wake up our creator. Note: @data is invalid after this point,
1445          * because it's allocated on ptlrpc_start_thread() stack.
1446          */
1447         cfs_waitq_signal(&thread->t_ctl_waitq);
1448
1449         watchdog = lc_watchdog_add(max_t(int, obd_timeout, AT_OFF ? 0 :
1450                                    at_get(&svc->srv_at_estimate)) *
1451                                    svc->srv_watchdog_factor, NULL, NULL);
1452
1453         spin_lock(&svc->srv_lock);
1454         svc->srv_threads_running++;
1455         list_add(&rs->rs_list, &svc->srv_free_rs_list);
1456         spin_unlock(&svc->srv_lock);
1457         cfs_waitq_signal(&svc->srv_free_rs_waitq);
1458
1459         CDEBUG(D_NET, "service thread %d (#%d)started\n", thread->t_id,
1460               svc->srv_threads_running);
1461
1462         /* XXX maintain a list of all managed devices: insert here */
1463
1464         while ((thread->t_flags & SVC_STOPPING) == 0 ||
1465                svc->srv_n_difficult_replies != 0) {
1466                 /* Don't exit while there are replies to be handled */
1467                 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
1468                                                      ptlrpc_retry_rqbds, svc);
1469
1470                 lc_watchdog_disable(watchdog);
1471
1472                 cond_resched();
1473
1474                 l_wait_event_exclusive (svc->srv_waitq,
1475                               ((thread->t_flags & SVC_STOPPING) != 0 &&
1476                                svc->srv_n_difficult_replies == 0) ||
1477                               (!list_empty(&svc->srv_idle_rqbds) &&
1478                                svc->srv_rqbd_timeout == 0) ||
1479                               !list_empty(&svc->srv_req_in_queue) ||
1480                               !list_empty(&svc->srv_reply_queue) ||
1481                               (!list_empty(&svc->srv_request_queue) &&
1482                                (svc->srv_n_active_reqs <
1483                                 (svc->srv_threads_running - 1))) ||
1484                               svc->srv_at_check,
1485                               &lwi);
1486
1487                 lc_watchdog_touch_ms(watchdog, max_t(int, obd_timeout,
1488                                      AT_OFF ? 0 :
1489                                      at_get(&svc->srv_at_estimate)) *
1490                                      svc->srv_watchdog_factor);
1491
1492                 ptlrpc_check_rqbd_pool(svc);
1493
1494                 if ((svc->srv_threads_started < svc->srv_threads_max) &&
1495                     (svc->srv_n_active_reqs >= (svc->srv_threads_started - 1))){
1496                         /* Ignore return code - we tried... */
1497                         ptlrpc_start_thread(dev, svc);
1498                 }
1499
1500                 if (!list_empty(&svc->srv_reply_queue))
1501                         ptlrpc_server_handle_reply(svc);
1502
1503                 if (!list_empty(&svc->srv_req_in_queue)) {
1504                         /* Process all incoming reqs before handling any */
1505                         ptlrpc_server_handle_req_in(svc);
1506                         /* but limit ourselves in case of flood */
1507                         if (counter++ < 1000)
1508                                 continue;
1509                         counter = 0;
1510                 }
1511
1512                 if (svc->srv_at_check)
1513                         ptlrpc_at_check_timed(svc);
1514
1515                 /* don't handle requests in the last thread */
1516                 if (!list_empty (&svc->srv_request_queue) &&
1517                     (svc->srv_n_active_reqs < (svc->srv_threads_running - 1))) {
1518                         lu_context_enter(&env.le_ctx);
1519                         ptlrpc_server_handle_request(svc, thread);
1520                         lu_context_exit(&env.le_ctx);
1521                 }
1522
1523                 if (!list_empty(&svc->srv_idle_rqbds) &&
1524                     ptlrpc_server_post_idle_rqbds(svc) < 0) {
1525                         /* I just failed to repost request buffers.  Wait
1526                          * for a timeout (unless something else happens)
1527                          * before I try again */
1528                         svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
1529                         CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
1530                                svc->srv_nrqbd_receiving);
1531                 }
1532         }
1533
1534         lc_watchdog_delete(watchdog);
1535
1536 out_srv_fini:
1537         /*
1538          * deconstruct service specific state created by ptlrpc_start_thread()
1539          */
1540         if (svc->srv_done != NULL)
1541                 svc->srv_done(thread);
1542
1543         lu_context_fini(&env.le_ctx);
1544 out:
1545         CDEBUG(D_NET, "service thread %d exiting: rc %d\n", thread->t_id, rc);
1546
1547         spin_lock(&svc->srv_lock);
1548         svc->srv_threads_running--; /* must know immediately */
1549         thread->t_id = rc;
1550         thread->t_flags = SVC_STOPPED;
1551
1552         cfs_waitq_signal(&thread->t_ctl_waitq);
1553         spin_unlock(&svc->srv_lock);
1554
1555         return rc;
1556 }
1557
1558 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
1559                                struct ptlrpc_thread *thread)
1560 {
1561         struct l_wait_info lwi = { 0 };
1562
1563         spin_lock(&svc->srv_lock);
1564         thread->t_flags = SVC_STOPPING;
1565         spin_unlock(&svc->srv_lock);
1566
1567         cfs_waitq_broadcast(&svc->srv_waitq);
1568         l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
1569                      &lwi);
1570
1571         spin_lock(&svc->srv_lock);
1572         list_del(&thread->t_link);
1573         spin_unlock(&svc->srv_lock);
1574
1575         OBD_FREE_PTR(thread);
1576 }
1577
1578 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
1579 {
1580         struct ptlrpc_thread *thread;
1581
1582         spin_lock(&svc->srv_lock);
1583         while (!list_empty(&svc->srv_threads)) {
1584                 thread = list_entry(svc->srv_threads.next,
1585                                     struct ptlrpc_thread, t_link);
1586
1587                 spin_unlock(&svc->srv_lock);
1588                 ptlrpc_stop_thread(svc, thread);
1589                 spin_lock(&svc->srv_lock);
1590         }
1591
1592         spin_unlock(&svc->srv_lock);
1593 }
1594
1595 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc)
1596 {
1597         int i, rc = 0;
1598         ENTRY;
1599
1600         /* We require 2 threads min - see note in
1601            ptlrpc_server_handle_request */
1602         LASSERT(svc->srv_threads_min >= 2);
1603         for (i = 0; i < svc->srv_threads_min; i++) {
1604                 rc = ptlrpc_start_thread(dev, svc);
1605                 /* We have enough threads, don't start more.  b=15759 */
1606                 if (rc == -EMFILE)
1607                         break;
1608                 if (rc) {
1609                         CERROR("cannot start %s thread #%d: rc %d\n", 
1610                                svc->srv_thread_name, i, rc);
1611                         ptlrpc_stop_all_threads(svc);
1612                 }
1613         }
1614         RETURN(rc);
1615 }
1616
1617 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc)
1618 {
1619         struct l_wait_info lwi = { 0 };
1620         struct ptlrpc_svc_data d;
1621         struct ptlrpc_thread *thread;
1622         char name[32];
1623         int id, rc;
1624         ENTRY;
1625
1626         CDEBUG(D_RPCTRACE, "%s started %d min %d max %d running %d\n",
1627                svc->srv_name, svc->srv_threads_started, svc->srv_threads_min,
1628                svc->srv_threads_max, svc->srv_threads_running);
1629         if (unlikely(svc->srv_threads_started >= svc->srv_threads_max) ||
1630             (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
1631              svc->srv_threads_started == svc->srv_threads_min - 1))
1632                 RETURN(-EMFILE);
1633
1634         OBD_ALLOC_PTR(thread);
1635         if (thread == NULL)
1636                 RETURN(-ENOMEM);
1637         cfs_waitq_init(&thread->t_ctl_waitq);
1638
1639         spin_lock(&svc->srv_lock);
1640         if (svc->srv_threads_started >= svc->srv_threads_max) {
1641                 spin_unlock(&svc->srv_lock);
1642                 OBD_FREE_PTR(thread);
1643                 RETURN(-EMFILE);
1644         }
1645         list_add(&thread->t_link, &svc->srv_threads);
1646         id = svc->srv_threads_started++;
1647         spin_unlock(&svc->srv_lock);
1648
1649         thread->t_id = id;
1650         sprintf(name, "%s_%02d", svc->srv_thread_name, id);
1651         d.dev = dev;
1652         d.svc = svc;
1653         d.name = name;
1654         d.thread = thread;
1655
1656         CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
1657         
1658           /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
1659          * just drop the VM and FILES in ptlrpc_daemonize() right away.
1660          */
1661         rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
1662         if (rc < 0) {
1663                 CERROR("cannot start thread '%s': rc %d\n", name, rc);
1664
1665                 spin_lock(&svc->srv_lock);
1666                 list_del(&thread->t_link);
1667                 --svc->srv_threads_started;
1668                 spin_unlock(&svc->srv_lock);
1669
1670                 OBD_FREE(thread, sizeof(*thread));
1671                 RETURN(rc);
1672         }
1673         l_wait_event(thread->t_ctl_waitq,
1674                      thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
1675
1676         rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
1677         RETURN(rc);
1678 }
1679 #endif
1680
1681 int ptlrpc_unregister_service(struct ptlrpc_service *service)
1682 {
1683         int                   rc;
1684         struct l_wait_info    lwi;
1685         struct list_head     *tmp;
1686         struct ptlrpc_reply_state *rs, *t;
1687
1688         cfs_timer_disarm(&service->srv_at_timer);
1689
1690         ptlrpc_stop_all_threads(service);
1691         LASSERT(list_empty(&service->srv_threads));
1692
1693         spin_lock (&ptlrpc_all_services_lock);
1694         list_del_init (&service->srv_list);
1695         spin_unlock (&ptlrpc_all_services_lock);
1696
1697         ptlrpc_lprocfs_unregister_service(service);
1698
1699         /* All history will be culled when the next request buffer is
1700          * freed */
1701         service->srv_max_history_rqbds = 0;
1702
1703         CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
1704
1705         rc = LNetClearLazyPortal(service->srv_req_portal);
1706         LASSERT (rc == 0);
1707
1708         /* Unlink all the request buffers.  This forces a 'final' event with
1709          * its 'unlink' flag set for each posted rqbd */
1710         list_for_each(tmp, &service->srv_active_rqbds) {
1711                 struct ptlrpc_request_buffer_desc *rqbd =
1712                         list_entry(tmp, struct ptlrpc_request_buffer_desc,
1713                                    rqbd_list);
1714
1715                 rc = LNetMDUnlink(rqbd->rqbd_md_h);
1716                 LASSERT (rc == 0 || rc == -ENOENT);
1717         }
1718
1719         /* Wait for the network to release any buffers it's currently
1720          * filling */
1721         for (;;) {
1722                 spin_lock(&service->srv_lock);
1723                 rc = service->srv_nrqbd_receiving;
1724                 spin_unlock(&service->srv_lock);
1725
1726                 if (rc == 0)
1727                         break;
1728
1729                 /* Network access will complete in finite time but the HUGE
1730                  * timeout lets us CWARN for visibility of sluggish NALs */
1731                 lwi = LWI_TIMEOUT(cfs_time_seconds(LONG_UNLINK), NULL, NULL);
1732                 rc = l_wait_event(service->srv_waitq,
1733                                   service->srv_nrqbd_receiving == 0,
1734                                   &lwi);
1735                 if (rc == -ETIMEDOUT)
1736                         CWARN("Service %s waiting for request buffers\n",
1737                               service->srv_name);
1738         }
1739
1740         /* schedule all outstanding replies to terminate them */
1741         spin_lock(&service->srv_lock);
1742         while (!list_empty(&service->srv_active_replies)) {
1743                 struct ptlrpc_reply_state *rs =
1744                         list_entry(service->srv_active_replies.next,
1745                                    struct ptlrpc_reply_state, rs_list);
1746                 ptlrpc_schedule_difficult_reply(rs);
1747         }
1748         spin_unlock(&service->srv_lock);
1749
1750         /* purge the request queue.  NB No new replies (rqbds all unlinked)
1751          * and no service threads, so I'm the only thread noodling the
1752          * request queue now */
1753         while (!list_empty(&service->srv_req_in_queue)) {
1754                 struct ptlrpc_request *req =
1755                         list_entry(service->srv_req_in_queue.next,
1756                                    struct ptlrpc_request,
1757                                    rq_list);
1758
1759                 list_del(&req->rq_list);
1760                 service->srv_n_queued_reqs--;
1761                 service->srv_n_active_reqs++;
1762                 ptlrpc_server_free_request(req);
1763         }
1764         while (!list_empty(&service->srv_request_queue)) {
1765                 struct ptlrpc_request *req =
1766                         list_entry(service->srv_request_queue.next,
1767                                    struct ptlrpc_request,
1768                                    rq_list);
1769
1770                 list_del(&req->rq_list);
1771                 service->srv_n_queued_reqs--;
1772                 service->srv_n_active_reqs++;
1773
1774                 ptlrpc_server_free_request(req);
1775         }
1776         LASSERT(service->srv_n_queued_reqs == 0);
1777         LASSERT(service->srv_n_active_reqs == 0);
1778         LASSERT(service->srv_n_history_rqbds == 0);
1779         LASSERT(list_empty(&service->srv_active_rqbds));
1780
1781         /* Now free all the request buffers since nothing references them
1782          * any more... */
1783         while (!list_empty(&service->srv_idle_rqbds)) {
1784                 struct ptlrpc_request_buffer_desc *rqbd =
1785                         list_entry(service->srv_idle_rqbds.next,
1786                                    struct ptlrpc_request_buffer_desc,
1787                                    rqbd_list);
1788
1789                 ptlrpc_free_rqbd(rqbd);
1790         }
1791
1792         /* wait for all outstanding replies to complete (they were
1793          * scheduled having been flagged to abort above) */
1794         while (atomic_read(&service->srv_outstanding_replies) != 0) {
1795                 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
1796
1797                 rc = l_wait_event(service->srv_waitq,
1798                                   !list_empty(&service->srv_reply_queue), &lwi);
1799                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1800
1801                 if (rc == 0) {
1802                         ptlrpc_server_handle_reply(service);
1803                         continue;
1804                 }
1805                 CWARN("Unexpectedly long timeout %p\n", service);
1806         }
1807
1808         list_for_each_entry_safe(rs, t, &service->srv_free_rs_list, rs_list) {
1809                 list_del(&rs->rs_list);
1810                 OBD_FREE(rs, service->srv_max_reply_size);
1811         }
1812
1813         /* In case somebody rearmed this in the meantime */
1814         cfs_timer_disarm(&service->srv_at_timer);
1815
1816         OBD_FREE_PTR(service);
1817         return 0;
1818 }
1819
1820 /* Returns 0 if the service is healthy.
1821  *
1822  * Right now, it just checks to make sure that requests aren't languishing
1823  * in the queue.  We'll use this health check to govern whether a node needs
1824  * to be shot, so it's intentionally non-aggressive. */
1825 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
1826 {
1827         struct ptlrpc_request *request;
1828         struct timeval         right_now;
1829         long                   timediff;
1830
1831         if (svc == NULL)
1832                 return 0;
1833
1834         do_gettimeofday(&right_now);
1835
1836         spin_lock(&svc->srv_lock);
1837         if (list_empty(&svc->srv_request_queue)) {
1838                 spin_unlock(&svc->srv_lock);
1839                 return 0;
1840         }
1841
1842         /* How long has the next entry been waiting? */
1843         request = list_entry(svc->srv_request_queue.next,
1844                              struct ptlrpc_request, rq_list);
1845         timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
1846         spin_unlock(&svc->srv_lock);
1847
1848         if ((timediff / ONE_MILLION) > (AT_OFF ? obd_timeout * 3/2 :
1849                                         at_max)) {
1850                 CERROR("%s: unhealthy - request has been waiting %lds\n",
1851                        svc->srv_name, timediff / ONE_MILLION);
1852                 return (-1);
1853         }
1854
1855         return 0;
1856 }