1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
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.
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.
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.
26 #define DEBUG_SUBSYSTEM S_RPC
28 #include <liblustre.h>
29 #include <libcfs/kp30.h>
31 #include <obd_support.h>
32 #include <obd_class.h>
33 #include <lustre_net.h>
34 #include <lnet/types.h>
35 #include "ptlrpc_internal.h"
37 int test_req_buffer_pressure = 0;
38 CFS_MODULE_PARM(test_req_buffer_pressure, "i", int, 0444,
39 "set non-zero to put pressure on request buffer pools");
42 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
44 static CFS_LIST_HEAD (ptlrpc_all_services);
45 spinlock_t ptlrpc_all_services_lock;
48 ptlrpc_alloc_request_buffer (int size)
52 if (size > SVC_BUF_VMALLOC_THRESHOLD)
53 OBD_VMALLOC(ptr, size);
61 ptlrpc_free_request_buffer (char *ptr, int size)
63 if (size > SVC_BUF_VMALLOC_THRESHOLD)
69 struct ptlrpc_request_buffer_desc *
70 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
72 struct ptlrpc_request_buffer_desc *rqbd;
74 OBD_ALLOC(rqbd, sizeof (*rqbd));
78 rqbd->rqbd_service = svc;
79 rqbd->rqbd_refcount = 0;
80 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
81 rqbd->rqbd_cbid.cbid_arg = rqbd;
82 CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
83 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
85 if (rqbd->rqbd_buffer == NULL) {
86 OBD_FREE(rqbd, sizeof (*rqbd));
90 spin_lock(&svc->srv_lock);
91 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
93 spin_unlock(&svc->srv_lock);
99 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
101 struct ptlrpc_service *svc = rqbd->rqbd_service;
103 LASSERT (rqbd->rqbd_refcount == 0);
104 LASSERT (list_empty(&rqbd->rqbd_reqs));
106 spin_lock(&svc->srv_lock);
107 list_del(&rqbd->rqbd_list);
109 spin_unlock(&svc->srv_lock);
111 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
112 OBD_FREE (rqbd, sizeof (*rqbd));
116 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
118 struct ptlrpc_request_buffer_desc *rqbd;
121 CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
122 svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
123 svc->srv_nrqbd_receiving, svc->srv_nbufs);
124 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
125 rqbd = ptlrpc_alloc_rqbd(svc);
128 CERROR ("%s: Can't allocate request buffer\n",
133 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
141 ptlrpc_save_lock (struct ptlrpc_request *req,
142 struct lustre_handle *lock, int mode)
144 struct ptlrpc_reply_state *rs = req->rq_reply_state;
148 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
150 idx = rs->rs_nlocks++;
151 rs->rs_locks[idx] = *lock;
152 rs->rs_modes[idx] = mode;
153 rs->rs_difficult = 1;
157 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
159 struct ptlrpc_service *svc = rs->rs_service;
162 LASSERT (spin_is_locked (&svc->srv_lock));
164 LASSERT (rs->rs_difficult);
165 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
167 if (rs->rs_scheduled) /* being set up or already notified */
170 rs->rs_scheduled = 1;
171 list_del (&rs->rs_list);
172 list_add (&rs->rs_list, &svc->srv_reply_queue);
173 cfs_waitq_signal (&svc->srv_waitq);
177 ptlrpc_commit_replies (struct obd_device *obd)
179 struct list_head *tmp;
180 struct list_head *nxt;
182 /* Find any replies that have been committed and get their service
183 * to attend to complete them. */
185 /* CAVEAT EMPTOR: spinlock ordering!!! */
186 spin_lock(&obd->obd_uncommitted_replies_lock);
188 list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
189 struct ptlrpc_reply_state *rs =
190 list_entry(tmp, struct ptlrpc_reply_state, rs_obd_list);
192 LASSERT (rs->rs_difficult);
194 if (rs->rs_transno <= obd->obd_last_committed) {
195 struct ptlrpc_service *svc = rs->rs_service;
197 spin_lock (&svc->srv_lock);
198 list_del_init (&rs->rs_obd_list);
199 ptlrpc_schedule_difficult_reply (rs);
200 spin_unlock (&svc->srv_lock);
204 spin_unlock(&obd->obd_uncommitted_replies_lock);
208 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
210 struct ptlrpc_request_buffer_desc *rqbd;
215 spin_lock(&svc->srv_lock);
217 if (list_empty (&svc->srv_idle_rqbds)) {
218 spin_unlock(&svc->srv_lock);
222 rqbd = list_entry(svc->srv_idle_rqbds.next,
223 struct ptlrpc_request_buffer_desc,
225 list_del (&rqbd->rqbd_list);
227 /* assume we will post successfully */
228 svc->srv_nrqbd_receiving++;
229 list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
231 spin_unlock(&svc->srv_lock);
233 rc = ptlrpc_register_rqbd(rqbd);
240 spin_lock(&svc->srv_lock);
242 svc->srv_nrqbd_receiving--;
243 list_del(&rqbd->rqbd_list);
244 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
246 /* Don't complain if no request buffers are posted right now; LNET
247 * won't drop requests because we set the portal lazy! */
249 spin_unlock(&svc->srv_lock);
254 static void ptlrpc_at_timer(unsigned long castmeharder)
256 struct ptlrpc_service *svc = (struct ptlrpc_service *)castmeharder;
257 CDEBUG(D_ADAPTTO, "at timer %s hit at %ld%s\n",
258 svc->srv_name, cfs_time_current_sec(),
259 list_empty(&svc->srv_at_list) ? ", empty" : "");
260 svc->srv_at_check = 1;
261 cfs_waitq_signal(&svc->srv_waitq);
264 /* @threadname should be 11 characters or less - 3 will be added on */
265 struct ptlrpc_service *
266 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
267 int req_portal, int rep_portal, int watchdog_factor,
268 svc_handler_t handler, char *name,
269 cfs_proc_dir_entry_t *proc_entry,
270 svcreq_printfn_t svcreq_printfn,
271 int min_threads, int max_threads, char *threadname)
274 struct ptlrpc_service *service;
278 LASSERT (bufsize >= max_req_size);
280 OBD_ALLOC(service, sizeof(*service));
284 /* First initialise enough for early teardown */
286 service->srv_name = name;
287 spin_lock_init(&service->srv_lock);
288 CFS_INIT_LIST_HEAD(&service->srv_threads);
289 cfs_waitq_init(&service->srv_waitq);
291 service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
292 service->srv_max_req_size = max_req_size;
293 service->srv_buf_size = bufsize;
294 service->srv_rep_portal = rep_portal;
295 service->srv_req_portal = req_portal;
296 service->srv_watchdog_factor = watchdog_factor;
297 service->srv_handler = handler;
298 service->srv_request_history_print_fn = svcreq_printfn;
299 service->srv_request_seq = 1; /* valid seq #s start at 1 */
300 service->srv_request_max_cull_seq = 0;
301 service->srv_threads_min = min_threads;
302 service->srv_threads_max = max_threads;
303 service->srv_thread_name = threadname;
305 rc = LNetSetLazyPortal(service->srv_req_portal);
308 CFS_INIT_LIST_HEAD(&service->srv_request_queue);
309 CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
310 CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
311 CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
312 CFS_INIT_LIST_HEAD(&service->srv_request_history);
313 CFS_INIT_LIST_HEAD(&service->srv_active_replies);
314 CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
315 CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
316 cfs_waitq_init(&service->srv_free_rs_waitq);
318 spin_lock_init(&service->srv_at_lock);
319 CFS_INIT_LIST_HEAD(&service->srv_req_in_queue);
320 CFS_INIT_LIST_HEAD(&service->srv_at_list);
321 cfs_timer_init(&service->srv_at_timer, ptlrpc_at_timer, service);
322 /* At SOW, service time should be quick; 10s seems generous. If client
323 timeout is less than this, we'll be sending an early reply. */
324 at_init(&service->srv_at_estimate, 10, AT_TIMEBASE_DEFAULT, 0);
326 spin_lock (&ptlrpc_all_services_lock);
327 list_add (&service->srv_list, &ptlrpc_all_services);
328 spin_unlock (&ptlrpc_all_services_lock);
330 /* Now allocate the request buffers */
331 rc = ptlrpc_grow_req_bufs(service);
332 /* We shouldn't be under memory pressure at startup, so
333 * fail if we can't post all our buffers at this time. */
337 /* Now allocate pool of reply buffers */
338 /* Increase max reply size to next power of two */
339 service->srv_max_reply_size = 1;
340 while (service->srv_max_reply_size < max_reply_size)
341 service->srv_max_reply_size <<= 1;
343 if (proc_entry != NULL)
344 ptlrpc_lprocfs_register_service(proc_entry, service);
346 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
347 service->srv_name, service->srv_req_portal);
351 ptlrpc_unregister_service(service);
355 static void ptlrpc_server_decref(struct ptlrpc_request *req)
357 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
359 if (!atomic_dec_and_test(&req->rq_refcount))
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));
368 struct ptlrpc_service *svc = rqbd->rqbd_service;
369 /* schedule request buffer for re-use.
370 * NB I can only do this after I've disposed of their
371 * reqs; particularly the embedded req */
372 spin_lock(&svc->srv_lock);
373 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
374 spin_unlock(&svc->srv_lock);
378 static void __ptlrpc_server_free_request(struct ptlrpc_request *req)
380 list_del(&req->rq_list);
381 ptlrpc_req_drop_rs(req);
382 ptlrpc_server_decref(req);
385 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
387 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
388 struct ptlrpc_service *svc = rqbd->rqbd_service;
390 struct list_head *tmp;
391 struct list_head *nxt;
393 DEBUG_REQ(D_INFO, req, "free req");
394 spin_lock(&svc->srv_at_lock);
395 list_del_init(&req->rq_timed_list);
396 spin_unlock(&svc->srv_at_lock);
398 spin_lock(&svc->srv_lock);
400 svc->srv_n_active_reqs--;
401 list_add(&req->rq_list, &rqbd->rqbd_reqs);
403 refcount = --(rqbd->rqbd_refcount);
405 /* request buffer is now idle: add to history */
406 list_del(&rqbd->rqbd_list);
407 list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
408 svc->srv_n_history_rqbds++;
410 /* cull some history?
411 * I expect only about 1 or 2 rqbds need to be recycled here */
412 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
413 rqbd = list_entry(svc->srv_history_rqbds.next,
414 struct ptlrpc_request_buffer_desc,
417 list_del(&rqbd->rqbd_list);
418 svc->srv_n_history_rqbds--;
420 /* remove rqbd's reqs from svc's req history while
421 * I've got the service lock */
422 list_for_each(tmp, &rqbd->rqbd_reqs) {
423 req = list_entry(tmp, struct ptlrpc_request,
425 /* Track the highest culled req seq */
426 if (req->rq_history_seq >
427 svc->srv_request_max_cull_seq)
428 svc->srv_request_max_cull_seq =
430 list_del(&req->rq_history_list);
433 spin_unlock(&svc->srv_lock);
435 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
436 req = list_entry(rqbd->rqbd_reqs.next,
437 struct ptlrpc_request,
439 __ptlrpc_server_free_request(req);
442 spin_lock(&svc->srv_lock);
444 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
445 /* If we are low on memory, we are not interested in
447 list_del(&req->rq_history_list);
448 __ptlrpc_server_free_request(req);
451 spin_unlock(&svc->srv_lock);
454 /* This function makes sure dead exports are evicted in a timely manner.
455 This function is only called when some export receives a message (i.e.,
456 the network is up.) */
457 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
459 struct obd_export *oldest_exp;
466 /* Compensate for slow machines, etc, by faking our request time
467 into the future. Although this can break the strict time-ordering
468 of the list, we can be really lazy here - we don't have to evict
469 at the exact right moment. Eventually, all silent exports
470 will make it to the top of the list. */
471 exp->exp_last_request_time = max(exp->exp_last_request_time,
472 cfs_time_current_sec() + extra_delay);
474 CDEBUG(D_INFO, "updating export %s at %ld\n",
475 exp->exp_client_uuid.uuid,
476 exp->exp_last_request_time);
478 /* exports may get disconnected from the chain even though the
479 export has references, so we must keep the spin lock while
480 manipulating the lists */
481 spin_lock(&exp->exp_obd->obd_dev_lock);
483 if (list_empty(&exp->exp_obd_chain_timed)) {
484 /* this one is not timed */
485 spin_unlock(&exp->exp_obd->obd_dev_lock);
490 list_move_tail(&exp->exp_obd_chain_timed,
491 &exp->exp_obd->obd_exports_timed);
493 oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
494 struct obd_export, exp_obd_chain_timed);
495 oldest_time = oldest_exp->exp_last_request_time;
496 spin_unlock(&exp->exp_obd->obd_dev_lock);
498 if (exp->exp_obd->obd_recovering) {
499 /* be nice to everyone during recovery */
504 /* Note - racing to start/reset the obd_eviction timer is safe */
505 if (exp->exp_obd->obd_eviction_timer == 0) {
506 /* Check if the oldest entry is expired. */
507 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
509 /* We need a second timer, in case the net was down and
510 * it just came back. Since the pinger may skip every
511 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
512 * we better wait for 3. */
513 exp->exp_obd->obd_eviction_timer =
514 cfs_time_current_sec() + 3 * PING_INTERVAL;
515 CDEBUG(D_HA, "%s: Think about evicting %s from %ld\n",
516 exp->exp_obd->obd_name, obd_export_nid2str(exp),
520 if (cfs_time_current_sec() >
521 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
522 /* The evictor won't evict anyone who we've heard from
523 * recently, so we don't have to check before we start
525 if (!ping_evictor_wake(exp))
526 exp->exp_obd->obd_eviction_timer = 0;
533 static int ptlrpc_check_req(struct ptlrpc_request *req)
535 if (lustre_msg_get_conn_cnt(req->rq_reqmsg) <
536 req->rq_export->exp_conn_cnt) {
537 DEBUG_REQ(D_ERROR, req,
538 "DROPPING req from old connection %d < %d",
539 lustre_msg_get_conn_cnt(req->rq_reqmsg),
540 req->rq_export->exp_conn_cnt);
543 if (req->rq_export->exp_obd && req->rq_export->exp_obd->obd_fail) {
544 /* Failing over, don't handle any more reqs, send
545 error response instead. */
546 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
547 req, req->rq_export->exp_obd->obd_name);
548 req->rq_status = -ENODEV;
555 /* If closest exipiration is within EARLY_MIN, send early replies to everybody
556 expiring within EARLY_MAX, asking for AT_EXTRA time */
557 #define AT_EARLY_MIN 2 /* Min time needed to send an early reply */
558 #define AT_EARLY_MAX 5 /* Dont send early replies if deadline is beyond */
559 #define AT_EXTRA 10 /* Early replies add this time to client timeout */
561 static void ptlrpc_at_set_timer(struct ptlrpc_service *svc)
563 struct ptlrpc_request *rq;
566 spin_lock(&svc->srv_at_lock);
567 if (list_empty(&svc->srv_at_list)) {
568 cfs_timer_disarm(&svc->srv_at_timer);
569 spin_unlock(&svc->srv_at_lock);
573 /* Set timer for closest deadline */
574 rq = list_entry(svc->srv_at_list.next, struct ptlrpc_request,
576 next = rq->rq_deadline - cfs_time_current_sec() - AT_EARLY_MIN;
578 ptlrpc_at_timer((unsigned long)svc);
580 cfs_timer_arm(&svc->srv_at_timer, cfs_time_shift(next));
581 spin_unlock(&svc->srv_at_lock);
582 CDEBUG(D_ADAPTTO, "armed %s at %+lds\n", svc->srv_name, next);
585 /* Add rpc to early reply check list */
586 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
588 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
589 struct ptlrpc_request *rq;
595 if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_AT_SUPPORT) == 0)
598 DEBUG_REQ(D_ADAPTTO, req, "add timed %lds",
599 req->rq_deadline - cfs_time_current_sec());
601 spin_lock(&svc->srv_at_lock);
603 LASSERT(list_empty(&req->rq_timed_list));
604 /* Add to sorted list. Presumably latest rpcs will have the latest
605 deadlines, so search backward. */
606 list_for_each_entry_reverse(rq, &svc->srv_at_list, rq_timed_list) {
607 if (req->rq_deadline > rq->rq_deadline) {
608 list_add(&req->rq_timed_list, &rq->rq_timed_list);
614 /* Add to front if shortest deadline or list empty */
615 list_add(&req->rq_timed_list, &svc->srv_at_list);
617 /* Check if we're the head of the list */
618 found = (svc->srv_at_list.next == &req->rq_timed_list);
620 spin_unlock(&svc->srv_at_lock);
623 ptlrpc_at_set_timer(svc);
628 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req,
631 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
632 long deadline = req->rq_deadline - cfs_time_current_sec();
636 /* deadline is when the client expects us to reply, margin is the
637 difference between clients' and servers' expectations */
638 DEBUG_REQ(D_ADAPTTO, req,
639 "%ssending early reply (deadline %+lds, margin %+lds) for "
640 "%d+%d", AT_OFF ? "AT off - not " : "",
641 deadline, deadline - at_get(&svc->srv_at_estimate),
642 at_get(&svc->srv_at_estimate), extra_time);
647 if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_AT_SUPPORT) == 0) {
648 CERROR("Wanted to ask client for more time, but no AT "
653 rc = lustre_pack_reply_flags(req, 1, NULL, NULL, LPRFL_EARLY_REPLY);
655 /* EALREADY means final reply was already packed */
659 /* Fake our processing time into the future to ask the
660 clients for some extra amount of time */
661 extra_time += cfs_time_current_sec() -
662 req->rq_arrival_time.tv_sec;
663 at_add(&svc->srv_at_estimate, extra_time);
666 req->rq_early_count++; /* number sent, server side */
667 rc = ptlrpc_send_reply(req, PTLRPC_REPLY_EARLY);
670 /* Adjust our own deadline to what we told the client */
671 req->rq_deadline = req->rq_arrival_time.tv_sec +
672 at_get(&svc->srv_at_estimate);
674 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
678 req->rq_rep_swab_mask = 0;
679 /* Free the (early) reply state from lustre_pack_reply.
680 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
681 ptlrpc_req_drop_rs(req);
686 /* Check if we need to send any early replies, and send them */
687 static int ptlrpc_at_check_timed(struct ptlrpc_service *svc)
689 struct ptlrpc_request *rq, *n;
690 struct list_head work_list;
691 time_t now = cfs_time_current_sec();
692 int first, counter = 0;
698 spin_lock(&svc->srv_at_lock);
699 if (svc->srv_at_check == 0) {
700 spin_unlock(&svc->srv_at_lock);
703 svc->srv_at_check = 0;
705 if (list_empty(&svc->srv_at_list)) {
706 spin_unlock(&svc->srv_at_lock);
710 /* The timer went off, but maybe the nearest rpc already completed. */
711 rq = list_entry(svc->srv_at_list.next, struct ptlrpc_request,
713 first = (int)(rq->rq_deadline - now);
714 if (first > AT_EARLY_MIN) {
715 /* We've still got plenty of time. Reset the timer. */
716 spin_unlock(&svc->srv_at_lock);
717 ptlrpc_at_set_timer(svc);
721 /* We're close to a timeout, and we don't know how much longer the
722 server will take. Send early replies to everyone expiring soon. */
723 CFS_INIT_LIST_HEAD(&work_list);
724 list_for_each_entry_safe(rq, n, &svc->srv_at_list, rq_timed_list) {
725 if (rq->rq_deadline <= now + AT_EARLY_MAX) {
726 list_move(&rq->rq_timed_list, &work_list);
733 spin_unlock(&svc->srv_at_lock);
735 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
736 "replies\n", first, AT_EXTRA, counter);
738 /* We're already past request deadlines before we even get a
739 chance to send early replies */
740 LCONSOLE_WARN("%s: This server is not able to keep up with "
741 "request traffic (cpu-bound).\n", svc->srv_name);
743 /* ptlrpc_server_free_request may delete an entry out of the work
746 spin_lock(&svc->srv_at_lock);
747 while (!list_empty(&work_list)) {
748 rq = list_entry(work_list.next, struct ptlrpc_request,
750 list_del_init(&rq->rq_timed_list);
751 /* if the entry is still in the worklist, it hasn't been
752 deleted, and is safe to take a ref to keep the req around */
753 atomic_inc(&rq->rq_refcount);
754 spin_unlock(&svc->srv_at_lock);
756 (ptlrpc_at_send_early_reply(rq, AT_EXTRA) == 0)) {
758 ptlrpc_at_add_timed(rq);
759 } else if (rq->rq_final) {
760 DEBUG_REQ(D_ADAPTTO, rq, "already packed final reply, "
761 "not sending early");
763 ptlrpc_server_decref(rq);
764 spin_lock(&svc->srv_at_lock);
766 spin_unlock(&svc->srv_at_lock);
769 /* Nothing added to timed list, so we have to kick the timer
771 ptlrpc_at_set_timer(svc);
776 /* Handle freshly incoming reqs, check timeout, send early reply if needed,
777 pass on to regular request queue */
779 ptlrpc_server_handle_req_in(struct ptlrpc_service *svc)
781 struct ptlrpc_request *req;
788 spin_lock(&svc->srv_lock);
789 if (list_empty(&svc->srv_req_in_queue)) {
790 spin_unlock(&svc->srv_lock);
794 req = list_entry(svc->srv_req_in_queue.next,
795 struct ptlrpc_request, rq_list);
796 list_del_init (&req->rq_list);
797 /* Consider this still a "queued" request as far as stats are
799 spin_unlock(&svc->srv_lock);
802 /* Clear request swab mask; this is a new request */
803 req->rq_req_swab_mask = 0;
805 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
807 CERROR ("error unpacking request: ptl %d from %s"
808 " xid "LPU64"\n", svc->srv_req_portal,
809 libcfs_id2str(req->rq_peer), req->rq_xid);
813 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
815 CERROR ("error unpacking ptlrpc body: ptl %d from %s"
816 " xid "LPU64"\n", svc->srv_req_portal,
817 libcfs_id2str(req->rq_peer), req->rq_xid);
822 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
823 CERROR("wrong packet type received (type=%u) from %s\n",
824 lustre_msg_get_type(req->rq_reqmsg),
825 libcfs_id2str(req->rq_peer));
829 CDEBUG(D_NET, "got req "LPD64"\n", req->rq_xid);
831 req->rq_export = class_conn2export(
832 lustre_msg_get_handle(req->rq_reqmsg));
833 if (req->rq_export) {
834 rc = ptlrpc_check_req(req);
835 class_export_put(req->rq_export);
840 /* req_in handling should/must be fast */
841 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
842 DEBUG_REQ(D_WARNING, req, "Slow req_in handling %lus",
843 cfs_time_current_sec() - req->rq_arrival_time.tv_sec);
845 /* Set rpc server deadline and add it to the timed list */
846 deadline = (lustre_msg_get_flags(req->rq_reqmsg) & MSG_AT_SUPPORT) ?
847 /* The max time the client expects us to take */
848 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
849 LASSERT(deadline > 0);
850 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
852 ptlrpc_at_add_timed(req);
854 /* Move it over to the request processing queue */
855 spin_lock(&svc->srv_lock);
856 list_add_tail(&req->rq_list, &svc->srv_request_queue);
857 cfs_waitq_signal(&svc->srv_waitq);
858 spin_unlock(&svc->srv_lock);
862 spin_lock(&svc->srv_lock);
863 svc->srv_n_queued_reqs--;
864 svc->srv_n_active_reqs++;
865 spin_unlock(&svc->srv_lock);
866 ptlrpc_server_free_request(req);
872 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
873 struct ptlrpc_thread *thread)
875 struct obd_export *export = NULL;
876 struct ptlrpc_request *request;
877 struct timeval work_start;
878 struct timeval work_end;
885 spin_lock(&svc->srv_lock);
886 if (list_empty (&svc->srv_request_queue) ||
889 /* !@%$# liblustre only has 1 thread */
890 svc->srv_n_difficult_replies != 0 &&
892 svc->srv_n_active_reqs >= (svc->srv_threads_running - 1))) {
893 /* Don't handle regular requests in the last thread, in order * remain free to handle any 'difficult' replies (that might
894 * to handle difficult replies (which might block other threads)
895 * as well as handle any incoming reqs, early replies, etc.
896 * That means we always need at least 2 service threads. */
897 spin_unlock(&svc->srv_lock);
901 request = list_entry (svc->srv_request_queue.next,
902 struct ptlrpc_request, rq_list);
903 list_del_init (&request->rq_list);
904 svc->srv_n_queued_reqs--;
905 svc->srv_n_active_reqs++;
907 spin_unlock(&svc->srv_lock);
909 do_gettimeofday(&work_start);
910 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
911 if (svc->srv_stats != NULL) {
912 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
914 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
915 svc->srv_n_queued_reqs);
916 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
917 svc->srv_n_active_reqs);
918 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
919 at_get(&svc->srv_at_estimate));
922 CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
924 request->rq_svc_thread = thread;
925 request->rq_export = class_conn2export(
926 lustre_msg_get_handle(request->rq_reqmsg));
928 if (request->rq_export) {
929 if (ptlrpc_check_req(request))
931 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
932 export = class_export_rpc_get(request->rq_export);
935 request->rq_phase = RQ_PHASE_INTERPRET;
937 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
938 "%s:%s+%d:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
939 (request->rq_export ?
940 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
941 (request->rq_export ?
942 atomic_read(&request->rq_export->exp_refcount) : -99),
943 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
944 libcfs_id2str(request->rq_peer),
945 lustre_msg_get_opc(request->rq_reqmsg));
947 OBD_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, obd_fail_val);
949 rc = svc->srv_handler(request);
951 request->rq_phase = RQ_PHASE_COMPLETE;
953 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
954 "%s:%s+%d:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
955 (request->rq_export ?
956 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
957 (request->rq_export ?
958 atomic_read(&request->rq_export->exp_refcount) : -99),
959 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
960 libcfs_id2str(request->rq_peer),
961 lustre_msg_get_opc(request->rq_reqmsg));
964 class_export_rpc_put(export);
967 if (request->rq_export != NULL)
968 class_export_put(request->rq_export);
970 if (cfs_time_current_sec() > request->rq_deadline) {
971 DEBUG_REQ(D_WARNING, request, "Request took longer than"
972 " estimated (%+lds); client may timeout.",
973 cfs_time_current_sec() - request->rq_deadline);
976 do_gettimeofday(&work_end);
977 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
978 CDEBUG(D_RPCTRACE, "request "LPU64" opc %u from %s processed in "
979 "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
980 request->rq_xid, lustre_msg_get_opc(request->rq_reqmsg),
981 libcfs_id2str(request->rq_peer), timediff,
982 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
983 request->rq_repmsg ? lustre_msg_get_transno(request->rq_repmsg) :
984 request->rq_transno, request->rq_status,
985 request->rq_repmsg ? lustre_msg_get_status(request->rq_repmsg):
987 if (svc->srv_stats != NULL) {
988 int opc = opcode_offset(lustre_msg_get_opc(request->rq_reqmsg));
990 LASSERT(opc < LUSTRE_MAX_OPCODES);
991 lprocfs_counter_add(svc->srv_stats,
992 opc + PTLRPC_LAST_CNTR,
996 if (request->rq_early_count) {
997 DEBUG_REQ(D_ADAPTTO, request,
998 "sent %d early replies before finishing in %lds",
999 request->rq_early_count,
1000 work_end.tv_sec - request->rq_arrival_time.tv_sec);
1003 ptlrpc_server_free_request(request);
1009 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
1011 struct ptlrpc_reply_state *rs;
1012 struct obd_export *exp;
1013 struct obd_device *obd;
1018 spin_lock(&svc->srv_lock);
1019 if (list_empty (&svc->srv_reply_queue)) {
1020 spin_unlock(&svc->srv_lock);
1024 rs = list_entry (svc->srv_reply_queue.next,
1025 struct ptlrpc_reply_state, rs_list);
1027 exp = rs->rs_export;
1030 LASSERT (rs->rs_difficult);
1031 LASSERT (rs->rs_scheduled);
1033 list_del_init (&rs->rs_list);
1035 /* Disengage from notifiers carefully (lock order - irqrestore below!)*/
1036 spin_unlock(&svc->srv_lock);
1038 spin_lock (&obd->obd_uncommitted_replies_lock);
1039 /* Noop if removed already */
1040 list_del_init (&rs->rs_obd_list);
1041 spin_unlock (&obd->obd_uncommitted_replies_lock);
1043 spin_lock (&exp->exp_lock);
1044 /* Noop if removed already */
1045 list_del_init (&rs->rs_exp_list);
1046 spin_unlock (&exp->exp_lock);
1048 spin_lock(&svc->srv_lock);
1050 been_handled = rs->rs_handled;
1053 nlocks = rs->rs_nlocks; /* atomic "steal", but */
1054 rs->rs_nlocks = 0; /* locks still on rs_locks! */
1056 if (nlocks == 0 && !been_handled) {
1057 /* If we see this, we should already have seen the warning
1058 * in mds_steal_ack_locks() */
1059 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
1062 rs->rs_xid, rs->rs_transno,
1063 lustre_msg_get_opc(rs->rs_msg),
1064 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1067 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1068 spin_unlock(&svc->srv_lock);
1070 if (!been_handled && rs->rs_on_net) {
1071 LNetMDUnlink(rs->rs_md_h);
1072 /* Ignore return code; we're racing with
1076 while (nlocks-- > 0)
1077 ldlm_lock_decref(&rs->rs_locks[nlocks],
1078 rs->rs_modes[nlocks]);
1080 spin_lock(&svc->srv_lock);
1083 rs->rs_scheduled = 0;
1085 if (!rs->rs_on_net) {
1087 svc->srv_n_difficult_replies--;
1088 spin_unlock(&svc->srv_lock);
1090 class_export_put (exp);
1091 rs->rs_export = NULL;
1092 ptlrpc_rs_decref (rs);
1093 atomic_dec (&svc->srv_outstanding_replies);
1097 /* still on the net; callback will schedule */
1098 spin_unlock(&svc->srv_lock);
1103 /* FIXME make use of timeout later */
1105 liblustre_check_services (void *arg)
1107 int did_something = 0;
1109 struct list_head *tmp, *nxt;
1112 /* I'm relying on being single threaded, not to have to lock
1113 * ptlrpc_all_services etc */
1114 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
1115 struct ptlrpc_service *svc =
1116 list_entry (tmp, struct ptlrpc_service, srv_list);
1118 if (svc->srv_threads_running != 0) /* I've recursed */
1121 /* service threads can block for bulk, so this limits us
1122 * (arbitrarily) to recursing 1 stack frame per service.
1123 * Note that the problem with recursion is that we have to
1124 * unwind completely before our caller can resume. */
1126 svc->srv_threads_running++;
1129 rc = ptlrpc_server_handle_req_in(svc);
1130 rc |= ptlrpc_server_handle_reply(svc);
1131 rc |= ptlrpc_at_check_timed(svc);
1132 rc |= ptlrpc_server_handle_request(svc, NULL);
1133 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
1134 did_something |= rc;
1137 svc->srv_threads_running--;
1140 RETURN(did_something);
1142 #define ptlrpc_stop_all_threads(s) do {} while (0)
1144 #else /* __KERNEL__ */
1146 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
1147 void ptlrpc_daemonize(char *name)
1149 struct fs_struct *fs = current->fs;
1151 atomic_inc(&fs->count);
1152 cfs_daemonize(name);
1153 exit_fs(cfs_current());
1155 ll_set_fs_pwd(current->fs, init_task.fs->pwdmnt, init_task.fs->pwd);
1159 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
1161 int avail = svc->srv_nrqbd_receiving;
1162 int low_water = test_req_buffer_pressure ? 0 :
1163 svc->srv_nbuf_per_group/2;
1165 /* NB I'm not locking; just looking. */
1167 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
1168 * allowed the request history to grow out of control. We could put a
1169 * sanity check on that here and cull some history if we need the
1172 if (avail <= low_water)
1173 ptlrpc_grow_req_bufs(svc);
1176 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
1181 ptlrpc_retry_rqbds(void *arg)
1183 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
1185 svc->srv_rqbd_timeout = 0;
1186 return (-ETIMEDOUT);
1189 static int ptlrpc_main(void *arg)
1191 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
1192 struct ptlrpc_service *svc = data->svc;
1193 struct ptlrpc_thread *thread = data->thread;
1194 struct obd_device *dev = data->dev;
1195 struct ptlrpc_reply_state *rs;
1196 struct lc_watchdog *watchdog;
1197 #ifdef WITH_GROUP_INFO
1198 struct group_info *ginfo = NULL;
1200 int counter, rc = 0;
1203 ptlrpc_daemonize(data->name);
1205 #if defined(HAVE_NODE_TO_CPUMASK) && defined(CONFIG_NUMA)
1206 /* we need to do this before any per-thread allocation is done so that
1207 * we get the per-thread allocations on local node. bug 7342 */
1208 if (svc->srv_cpu_affinity) {
1211 for (cpu = 0, num_cpu = 0; cpu < NR_CPUS; cpu++) {
1212 if (!cpu_online(cpu))
1214 if (num_cpu == thread->t_id % num_online_cpus())
1218 set_cpus_allowed(cfs_current(), node_to_cpumask(cpu_to_node(cpu)));
1222 #ifdef WITH_GROUP_INFO
1223 ginfo = groups_alloc(0);
1229 set_current_groups(ginfo);
1230 put_group_info(ginfo);
1233 if (svc->srv_init != NULL) {
1234 rc = svc->srv_init(thread);
1239 /* Alloc reply state structure for this one */
1240 OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
1246 /* Record that the thread is running */
1247 thread->t_flags = SVC_RUNNING;
1249 * wake up our creator. Note: @data is invalid after this point,
1250 * because it's allocated on ptlrpc_start_thread() stack.
1252 cfs_waitq_signal(&thread->t_ctl_waitq);
1254 watchdog = lc_watchdog_add(max_t(int, obd_timeout, AT_OFF ? 0 :
1255 at_get(&svc->srv_at_estimate)) *
1256 svc->srv_watchdog_factor, NULL, NULL);
1258 spin_lock(&svc->srv_lock);
1259 svc->srv_threads_running++;
1260 list_add(&rs->rs_list, &svc->srv_free_rs_list);
1261 spin_unlock(&svc->srv_lock);
1262 cfs_waitq_signal(&svc->srv_free_rs_waitq);
1264 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
1265 svc->srv_threads_running);
1267 /* XXX maintain a list of all managed devices: insert here */
1269 while ((thread->t_flags & SVC_STOPPING) == 0 ||
1270 svc->srv_n_difficult_replies != 0) {
1271 /* Don't exit while there are replies to be handled */
1272 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
1273 ptlrpc_retry_rqbds, svc);
1275 lc_watchdog_disable(watchdog);
1279 l_wait_event_exclusive (svc->srv_waitq,
1280 ((thread->t_flags & SVC_STOPPING) != 0 &&
1281 svc->srv_n_difficult_replies == 0) ||
1282 (!list_empty(&svc->srv_idle_rqbds) &&
1283 svc->srv_rqbd_timeout == 0) ||
1284 !list_empty(&svc->srv_req_in_queue) ||
1285 !list_empty(&svc->srv_reply_queue) ||
1286 (!list_empty(&svc->srv_request_queue) &&
1287 (svc->srv_n_active_reqs <
1288 (svc->srv_threads_running - 1))) ||
1292 lc_watchdog_touch_ms(watchdog, max_t(int, obd_timeout,
1294 at_get(&svc->srv_at_estimate)) *
1295 svc->srv_watchdog_factor);
1297 ptlrpc_check_rqbd_pool(svc);
1299 if ((svc->srv_threads_started < svc->srv_threads_max) &&
1300 (svc->srv_n_active_reqs >= (svc->srv_threads_started - 1))){
1301 /* Ignore return code - we tried... */
1302 ptlrpc_start_thread(dev, svc);
1305 if (!list_empty(&svc->srv_reply_queue))
1306 ptlrpc_server_handle_reply (svc);
1309 while(!list_empty(&svc->srv_req_in_queue)) {
1310 /* Process all incoming reqs before handling any */
1311 ptlrpc_server_handle_req_in(svc);
1312 /* but limit ourselves in case of flood */
1313 if (counter++ > 1000)
1317 if (svc->srv_at_check)
1318 ptlrpc_at_check_timed(svc);
1320 /* don't handle requests in the last thread */
1321 if (!list_empty (&svc->srv_request_queue) &&
1322 (svc->srv_n_active_reqs < (svc->srv_threads_running - 1)))
1323 ptlrpc_server_handle_request(svc, thread);
1325 if (!list_empty(&svc->srv_idle_rqbds) &&
1326 ptlrpc_server_post_idle_rqbds(svc) < 0) {
1327 /* I just failed to repost request buffers. Wait
1328 * for a timeout (unless something else happens)
1329 * before I try again */
1330 svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
1331 CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
1332 svc->srv_nrqbd_receiving);
1336 lc_watchdog_delete(watchdog);
1340 * deconstruct service specific state created by ptlrpc_start_thread()
1342 if (svc->srv_done != NULL)
1343 svc->srv_done(thread);
1346 CDEBUG(D_NET, "service thread %d exiting: rc %d\n", thread->t_id, rc);
1348 spin_lock(&svc->srv_lock);
1349 svc->srv_threads_running--; /* must know immediately */
1351 thread->t_flags = SVC_STOPPED;
1353 cfs_waitq_signal(&thread->t_ctl_waitq);
1354 spin_unlock(&svc->srv_lock);
1359 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
1360 struct ptlrpc_thread *thread)
1362 struct l_wait_info lwi = { 0 };
1364 spin_lock(&svc->srv_lock);
1365 thread->t_flags = SVC_STOPPING;
1366 spin_unlock(&svc->srv_lock);
1368 cfs_waitq_broadcast(&svc->srv_waitq);
1369 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
1372 spin_lock(&svc->srv_lock);
1373 list_del(&thread->t_link);
1374 spin_unlock(&svc->srv_lock);
1376 OBD_FREE(thread, sizeof(*thread));
1379 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
1381 struct ptlrpc_thread *thread;
1383 spin_lock(&svc->srv_lock);
1384 while (!list_empty(&svc->srv_threads)) {
1385 thread = list_entry(svc->srv_threads.next,
1386 struct ptlrpc_thread, t_link);
1388 spin_unlock(&svc->srv_lock);
1389 ptlrpc_stop_thread(svc, thread);
1390 spin_lock(&svc->srv_lock);
1393 spin_unlock(&svc->srv_lock);
1396 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc)
1401 /* We require 2 threads min - see note in
1402 ptlrpc_server_handle_request */
1403 LASSERT(svc->srv_threads_min >= 2);
1404 for (i = 0; i < svc->srv_threads_min; i++) {
1405 rc = ptlrpc_start_thread(dev, svc);
1407 CERROR("cannot start %s thread #%d: rc %d\n",
1408 svc->srv_thread_name, i, rc);
1409 ptlrpc_stop_all_threads(svc);
1415 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc)
1417 struct l_wait_info lwi = { 0 };
1418 struct ptlrpc_svc_data d;
1419 struct ptlrpc_thread *thread;
1424 CDEBUG(D_RPCTRACE, "%s started %d min %d max %d running %d\n",
1425 svc->srv_name, svc->srv_threads_started, svc->srv_threads_min,
1426 svc->srv_threads_max, svc->srv_threads_running);
1427 if (svc->srv_threads_started >= svc->srv_threads_max)
1430 OBD_ALLOC(thread, sizeof(*thread));
1433 cfs_waitq_init(&thread->t_ctl_waitq);
1435 spin_lock(&svc->srv_lock);
1436 if (svc->srv_threads_started >= svc->srv_threads_max) {
1437 spin_unlock(&svc->srv_lock);
1438 OBD_FREE(thread, sizeof(*thread));
1441 list_add(&thread->t_link, &svc->srv_threads);
1442 id = svc->srv_threads_started++;
1443 spin_unlock(&svc->srv_lock);
1446 sprintf(name, "%s_%02d", svc->srv_thread_name, id);
1452 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
1454 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
1455 * just drop the VM and FILES in ptlrpc_daemonize() right away.
1457 rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
1459 CERROR("cannot start thread '%s': rc %d\n", name, rc);
1461 spin_lock(&svc->srv_lock);
1462 list_del(&thread->t_link);
1463 --svc->srv_threads_started;
1464 spin_unlock(&svc->srv_lock);
1466 OBD_FREE(thread, sizeof(*thread));
1469 l_wait_event(thread->t_ctl_waitq,
1470 thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
1472 rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
1477 int ptlrpc_unregister_service(struct ptlrpc_service *service)
1480 struct l_wait_info lwi;
1481 struct list_head *tmp;
1482 struct ptlrpc_reply_state *rs, *t;
1484 cfs_timer_disarm(&service->srv_at_timer);
1486 ptlrpc_stop_all_threads(service);
1487 LASSERT(list_empty(&service->srv_threads));
1489 spin_lock (&ptlrpc_all_services_lock);
1490 list_del_init (&service->srv_list);
1491 spin_unlock (&ptlrpc_all_services_lock);
1493 ptlrpc_lprocfs_unregister_service(service);
1495 /* All history will be culled when the next request buffer is
1497 service->srv_max_history_rqbds = 0;
1499 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
1501 rc = LNetClearLazyPortal(service->srv_req_portal);
1504 /* Unlink all the request buffers. This forces a 'final' event with
1505 * its 'unlink' flag set for each posted rqbd */
1506 list_for_each(tmp, &service->srv_active_rqbds) {
1507 struct ptlrpc_request_buffer_desc *rqbd =
1508 list_entry(tmp, struct ptlrpc_request_buffer_desc,
1511 rc = LNetMDUnlink(rqbd->rqbd_md_h);
1512 LASSERT (rc == 0 || rc == -ENOENT);
1515 /* Wait for the network to release any buffers it's currently
1518 spin_lock(&service->srv_lock);
1519 rc = service->srv_nrqbd_receiving;
1520 spin_unlock(&service->srv_lock);
1525 /* Network access will complete in finite time but the HUGE
1526 * timeout lets us CWARN for visibility of sluggish NALs */
1527 lwi = LWI_TIMEOUT(cfs_time_seconds(FOREVER), NULL, NULL);
1528 rc = l_wait_event(service->srv_waitq,
1529 service->srv_nrqbd_receiving == 0,
1531 if (rc == -ETIMEDOUT)
1532 CWARN("Service %s waiting for request buffers\n",
1536 /* schedule all outstanding replies to terminate them */
1537 spin_lock(&service->srv_lock);
1538 while (!list_empty(&service->srv_active_replies)) {
1539 struct ptlrpc_reply_state *rs =
1540 list_entry(service->srv_active_replies.next,
1541 struct ptlrpc_reply_state, rs_list);
1542 ptlrpc_schedule_difficult_reply(rs);
1544 spin_unlock(&service->srv_lock);
1546 /* purge the request queue. NB No new replies (rqbds all unlinked)
1547 * and no service threads, so I'm the only thread noodling the
1548 * request queue now */
1549 while (!list_empty(&service->srv_req_in_queue)) {
1550 struct ptlrpc_request *req =
1551 list_entry(service->srv_req_in_queue.next,
1552 struct ptlrpc_request,
1555 list_del(&req->rq_list);
1556 service->srv_n_queued_reqs--;
1557 service->srv_n_active_reqs++;
1558 ptlrpc_server_free_request(req);
1560 while (!list_empty(&service->srv_request_queue)) {
1561 struct ptlrpc_request *req =
1562 list_entry(service->srv_request_queue.next,
1563 struct ptlrpc_request,
1566 list_del(&req->rq_list);
1567 service->srv_n_queued_reqs--;
1568 service->srv_n_active_reqs++;
1569 ptlrpc_server_free_request(req);
1571 LASSERT(service->srv_n_queued_reqs == 0);
1572 LASSERT(service->srv_n_active_reqs == 0);
1573 LASSERT(service->srv_n_history_rqbds == 0);
1574 LASSERT(list_empty(&service->srv_active_rqbds));
1576 /* Now free all the request buffers since nothing references them
1578 while (!list_empty(&service->srv_idle_rqbds)) {
1579 struct ptlrpc_request_buffer_desc *rqbd =
1580 list_entry(service->srv_idle_rqbds.next,
1581 struct ptlrpc_request_buffer_desc,
1584 ptlrpc_free_rqbd(rqbd);
1587 /* wait for all outstanding replies to complete (they were
1588 * scheduled having been flagged to abort above) */
1589 while (atomic_read(&service->srv_outstanding_replies) != 0) {
1590 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
1592 rc = l_wait_event(service->srv_waitq,
1593 !list_empty(&service->srv_reply_queue), &lwi);
1594 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1597 ptlrpc_server_handle_reply(service);
1600 CWARN("Unexpectedly long timeout %p\n", service);
1603 list_for_each_entry_safe(rs, t, &service->srv_free_rs_list, rs_list) {
1604 list_del(&rs->rs_list);
1605 OBD_FREE(rs, service->srv_max_reply_size);
1608 /* In case somebody rearmed this in the meantime */
1609 cfs_timer_disarm(&service->srv_at_timer);
1611 OBD_FREE(service, sizeof(*service));
1615 /* Returns 0 if the service is healthy.
1617 * Right now, it just checks to make sure that requests aren't languishing
1618 * in the queue. We'll use this health check to govern whether a node needs
1619 * to be shot, so it's intentionally non-aggressive. */
1620 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
1622 struct ptlrpc_request *request;
1623 struct timeval right_now;
1629 do_gettimeofday(&right_now);
1631 spin_lock(&svc->srv_lock);
1632 if (list_empty(&svc->srv_request_queue)) {
1633 spin_unlock(&svc->srv_lock);
1637 /* How long has the next entry been waiting? */
1638 request = list_entry(svc->srv_request_queue.next,
1639 struct ptlrpc_request, rq_list);
1640 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
1641 spin_unlock(&svc->srv_lock);
1643 if ((timediff / ONE_MILLION) > (AT_OFF ? obd_timeout * 3/2 :
1644 adaptive_timeout_max)) {
1645 CERROR("%s: unhealthy - request has been waiting %lds\n",
1646 svc->srv_name, timediff / ONE_MILLION);