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>
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"
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");
61 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
63 static CFS_LIST_HEAD(ptlrpc_all_services);
64 spinlock_t ptlrpc_all_services_lock;
67 ptlrpc_alloc_request_buffer (int size)
71 if (size > SVC_BUF_VMALLOC_THRESHOLD)
72 OBD_VMALLOC(ptr, size);
80 ptlrpc_free_request_buffer (char *ptr, int size)
82 if (size > SVC_BUF_VMALLOC_THRESHOLD)
88 struct ptlrpc_request_buffer_desc *
89 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
91 struct ptlrpc_request_buffer_desc *rqbd;
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);
104 if (rqbd->rqbd_buffer == NULL) {
109 spin_lock(&svc->srv_lock);
110 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
112 spin_unlock(&svc->srv_lock);
118 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
120 struct ptlrpc_service *svc = rqbd->rqbd_service;
122 LASSERT (rqbd->rqbd_refcount == 0);
123 LASSERT (list_empty(&rqbd->rqbd_reqs));
125 spin_lock(&svc->srv_lock);
126 list_del(&rqbd->rqbd_list);
128 spin_unlock(&svc->srv_lock);
130 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
135 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
137 struct ptlrpc_request_buffer_desc *rqbd;
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);
147 CERROR ("%s: Can't allocate request buffer\n",
152 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
160 ptlrpc_save_lock (struct ptlrpc_request *req,
161 struct lustre_handle *lock, int mode)
163 struct ptlrpc_reply_state *rs = req->rq_reply_state;
167 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
169 idx = rs->rs_nlocks++;
170 rs->rs_locks[idx] = *lock;
171 rs->rs_modes[idx] = mode;
172 rs->rs_difficult = 1;
176 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
178 struct ptlrpc_service *svc = rs->rs_service;
181 LASSERT (spin_is_locked (&svc->srv_lock));
183 LASSERT (rs->rs_difficult);
184 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
186 if (rs->rs_scheduled) /* being set up or already notified */
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);
196 ptlrpc_commit_replies (struct obd_device *obd)
198 struct list_head *tmp;
199 struct list_head *nxt;
201 /* Find any replies that have been committed and get their service
202 * to attend to complete them. */
204 /* CAVEAT EMPTOR: spinlock ordering!!! */
205 spin_lock(&obd->obd_uncommitted_replies_lock);
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);
211 LASSERT (rs->rs_difficult);
213 if (rs->rs_transno <= obd->obd_last_committed) {
214 struct ptlrpc_service *svc = rs->rs_service;
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);
223 spin_unlock(&obd->obd_uncommitted_replies_lock);
227 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
229 struct ptlrpc_request_buffer_desc *rqbd;
234 spin_lock(&svc->srv_lock);
236 if (list_empty (&svc->srv_idle_rqbds)) {
237 spin_unlock(&svc->srv_lock);
241 rqbd = list_entry(svc->srv_idle_rqbds.next,
242 struct ptlrpc_request_buffer_desc,
244 list_del (&rqbd->rqbd_list);
246 /* assume we will post successfully */
247 svc->srv_nrqbd_receiving++;
248 list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
250 spin_unlock(&svc->srv_lock);
252 rc = ptlrpc_register_rqbd(rqbd);
259 spin_lock(&svc->srv_lock);
261 svc->srv_nrqbd_receiving--;
262 list_del(&rqbd->rqbd_list);
263 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
265 /* Don't complain if no request buffers are posted right now; LNET
266 * won't drop requests because we set the portal lazy! */
268 spin_unlock(&svc->srv_lock);
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,
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,
284 prntfn, c->psc_min_threads, c->psc_max_threads,
285 threadname, c->psc_ctx_tags);
287 EXPORT_SYMBOL(ptlrpc_init_svc_conf);
289 static void ptlrpc_at_timer(unsigned long castmeharder)
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);
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)
311 struct ptlrpc_service *service;
315 LASSERT (bufsize >= max_req_size + SPTLRPC_MAX_PAYLOAD);
316 LASSERT (ctx_tags != 0);
318 OBD_ALLOC_PTR(service);
322 /* First initialise enough for early teardown */
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);
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;
344 rc = LNetSetLazyPortal(service->srv_req_portal);
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);
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);
365 spin_lock (&ptlrpc_all_services_lock);
366 list_add (&service->srv_list, &ptlrpc_all_services);
367 spin_unlock (&ptlrpc_all_services_lock);
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. */
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;
383 if (proc_entry != NULL)
384 ptlrpc_lprocfs_register_service(proc_entry, service);
386 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
387 service->srv_name, service->srv_req_portal);
391 ptlrpc_unregister_service(service);
395 static void ptlrpc_server_req_decref(struct ptlrpc_request *req)
397 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
399 if (!atomic_dec_and_test(&req->rq_refcount))
402 sptlrpc_svc_ctx_decref(req);
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));
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);
421 static void __ptlrpc_server_free_request(struct ptlrpc_request *req)
423 list_del(&req->rq_list);
424 ptlrpc_req_drop_rs(req);
425 ptlrpc_server_req_decref(req);
429 ptlrpc_server_free_request(struct ptlrpc_request *req)
431 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
432 struct ptlrpc_service *svc = rqbd->rqbd_service;
434 struct list_head *tmp;
435 struct list_head *nxt;
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);
444 spin_lock(&svc->srv_lock);
446 svc->srv_n_active_reqs--;
447 list_add(&req->rq_list, &rqbd->rqbd_reqs);
449 refcount = --(rqbd->rqbd_refcount);
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++;
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,
463 list_del(&rqbd->rqbd_list);
464 svc->srv_n_history_rqbds--;
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,
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 =
476 list_del(&req->rq_history_list);
479 spin_unlock(&svc->srv_lock);
481 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
482 req = list_entry(rqbd->rqbd_reqs.next,
483 struct ptlrpc_request,
485 __ptlrpc_server_free_request(req);
488 spin_lock(&svc->srv_lock);
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
493 list_del(&req->rq_history_list);
494 __ptlrpc_server_free_request(req);
497 spin_unlock(&svc->srv_lock);
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)
506 struct obd_export *oldest_exp;
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);
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);
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);
530 if (list_empty(&exp->exp_obd_chain_timed)) {
531 /* this one is not timed */
532 spin_unlock(&exp->exp_obd->obd_dev_lock);
537 list_move_tail(&exp->exp_obd_chain_timed,
538 &exp->exp_obd->obd_exports_timed);
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);
545 if (exp->exp_obd->obd_recovering) {
546 /* be nice to everyone during recovery */
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 +
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),
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
572 if (!ping_evictor_wake(exp))
573 exp->exp_obd->obd_eviction_timer = 0;
581 int lu_context_init(struct lu_context *ctx, __u32 tags)
586 void lu_context_fini(struct lu_context *ctx)
590 void lu_context_enter(struct lu_context *ctx)
594 void lu_context_exit(struct lu_context *ctx)
600 static int ptlrpc_check_req(struct ptlrpc_request *req)
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);
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;
624 static void ptlrpc_at_set_timer(struct ptlrpc_service *svc)
626 struct ptlrpc_request *rq;
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);
636 /* Set timer for closest deadline */
637 rq = list_entry(svc->srv_at_list.next, struct ptlrpc_request,
639 next = (__s32)(rq->rq_deadline - cfs_time_current_sec() -
642 ptlrpc_at_timer((unsigned long)svc);
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);
649 /* Add rpc to early reply check list */
650 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
652 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
653 struct ptlrpc_request *rq;
659 if (req->rq_no_reply)
662 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
665 DEBUG_REQ(D_ADAPTTO, req, "add timed %lds",
666 req->rq_deadline - cfs_time_current_sec());
668 spin_lock(&svc->srv_at_lock);
670 if (unlikely(req->rq_sent_final)) {
671 spin_unlock(&svc->srv_at_lock);
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);
686 /* Add to front if shortest deadline or list empty */
687 list_add(&req->rq_timed_list, &svc->srv_at_list);
689 /* Check if we're the head of the list */
690 found = (svc->srv_at_list.next == &req->rq_timed_list);
692 spin_unlock(&svc->srv_at_lock);
695 ptlrpc_at_set_timer(svc);
700 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req,
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();
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);
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. */
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 "
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);
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());
754 OBD_ALLOC(reqcopy, sizeof *reqcopy);
757 OBD_ALLOC(reqmsg, req->rq_reqlen);
759 OBD_FREE(reqcopy, sizeof *reqcopy);
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);
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);
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);
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);
792 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
796 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
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 */
803 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
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);
811 class_export_rpc_put(reqcopy->rq_export);
812 class_export_put(reqcopy->rq_export);
814 sptlrpc_svc_ctx_decref(reqcopy);
815 OBD_FREE(reqmsg, req->rq_reqlen);
816 OBD_FREE(reqcopy, sizeof *reqcopy);
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)
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;
831 spin_lock(&svc->srv_at_lock);
832 if (svc->srv_at_check == 0) {
833 spin_unlock(&svc->srv_at_lock);
836 delay = cfs_time_sub(cfs_time_current(), svc->srv_at_checktime);
837 svc->srv_at_check = 0;
839 if (list_empty(&svc->srv_at_list)) {
840 spin_unlock(&svc->srv_at_lock);
844 /* The timer went off, but maybe the nearest rpc already completed. */
845 rq = list_entry(svc->srv_at_list.next, struct ptlrpc_request,
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);
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);
867 spin_unlock(&svc->srv_at_lock);
869 /* we have a new earliest deadline, restart the timer */
870 ptlrpc_at_set_timer(svc);
872 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
873 "replies\n", first, at_extra, counter);
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);
885 /* ptlrpc_server_free_request may delete an entry out of the work
887 spin_lock(&svc->srv_at_lock);
888 while (!list_empty(&work_list)) {
889 rq = list_entry(work_list.next, struct ptlrpc_request,
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);
897 if (ptlrpc_at_send_early_reply(rq, at_extra) == 0)
898 ptlrpc_at_add_timed(rq);
900 ptlrpc_server_req_decref(rq);
901 spin_lock(&svc->srv_at_lock);
903 spin_unlock(&svc->srv_at_lock);
908 /* Handle freshly incoming reqs, add to timed early reply list,
909 pass on to regular request queue */
911 ptlrpc_server_handle_req_in(struct ptlrpc_service *svc)
913 struct ptlrpc_request *req;
920 spin_lock(&svc->srv_lock);
921 if (list_empty(&svc->srv_req_in_queue)) {
922 spin_unlock(&svc->srv_lock);
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
931 spin_unlock(&svc->srv_lock);
933 /* go through security check/transform */
934 rc = sptlrpc_svc_unwrap_request(req);
938 case SECSVC_COMPLETE:
939 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
947 /* Clear request swab mask; this is a new request */
948 req->rq_req_swab_mask = 0;
950 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
952 CERROR("error unpacking request: ptl %d from %s x"LPU64"\n",
953 svc->srv_req_portal, libcfs_id2str(req->rq_peer),
958 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
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);
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));
974 CDEBUG(D_NET, "got req "LPD64"\n", req->rq_xid);
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);
981 rc = sptlrpc_target_export_check(req->rq_export, req);
983 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
984 "illegal security flavor,");
987 class_export_put(req->rq_export);
988 req->rq_export = NULL;
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);
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");
1009 ptlrpc_at_add_timed(req);
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);
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);
1029 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
1030 struct ptlrpc_thread *thread)
1032 struct obd_export *export = NULL;
1033 struct ptlrpc_request *request;
1034 struct timeval work_start;
1035 struct timeval work_end;
1042 spin_lock(&svc->srv_lock);
1043 if (unlikely(list_empty (&svc->srv_request_queue) ||
1046 /* !@%$# liblustre only has 1 thread */
1047 svc->srv_n_difficult_replies != 0 &&
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);
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++;
1064 spin_unlock(&svc->srv_lock);
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,
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));
1079 rc = lu_context_init(&request->rq_session, LCT_SESSION);
1081 CERROR("Failure to initialize session: %d\n", rc);
1084 request->rq_session.lc_thread = thread;
1085 lu_context_enter(&request->rq_session);
1087 CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
1089 request->rq_svc_thread = thread;
1091 request->rq_svc_thread->t_env->le_ses = &request->rq_session;
1093 request->rq_export = class_conn2export(
1094 lustre_msg_get_handle(request->rq_reqmsg));
1096 if (likely(request->rq_export)) {
1097 if (unlikely(ptlrpc_check_req(request)))
1099 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
1100 export = class_export_rpc_get(request->rq_export);
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;
1115 request->rq_phase = RQ_PHASE_INTERPRET;
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));
1127 OBD_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, obd_fail_val);
1129 rc = svc->srv_handler(request);
1131 request->rq_phase = RQ_PHASE_COMPLETE;
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));
1145 class_export_rpc_put(export);
1147 if (likely(request->rq_export != NULL))
1148 class_export_put(request->rq_export);
1150 lu_context_exit(&request->rq_session);
1151 lu_context_fini(&request->rq_session);
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);
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):
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,
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);
1190 ptlrpc_server_free_request(request);
1196 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
1198 struct ptlrpc_reply_state *rs;
1199 struct obd_export *exp;
1200 struct obd_device *obd;
1205 spin_lock(&svc->srv_lock);
1206 if (list_empty (&svc->srv_reply_queue)) {
1207 spin_unlock(&svc->srv_lock);
1211 rs = list_entry (svc->srv_reply_queue.next,
1212 struct ptlrpc_reply_state, rs_list);
1214 exp = rs->rs_export;
1217 LASSERT (rs->rs_difficult);
1218 LASSERT (rs->rs_scheduled);
1220 list_del_init (&rs->rs_list);
1222 /* Disengage from notifiers carefully (lock order - irqrestore below!)*/
1223 spin_unlock(&svc->srv_lock);
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);
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);
1235 spin_lock(&svc->srv_lock);
1237 been_handled = rs->rs_handled;
1240 nlocks = rs->rs_nlocks; /* atomic "steal", but */
1241 rs->rs_nlocks = 0; /* locks still on rs_locks! */
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
1249 rs->rs_xid, rs->rs_transno,
1250 lustre_msg_get_opc(rs->rs_msg),
1251 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1254 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1255 spin_unlock(&svc->srv_lock);
1257 if (!been_handled && rs->rs_on_net) {
1258 LNetMDUnlink(rs->rs_md_h);
1259 /* Ignore return code; we're racing with
1263 while (nlocks-- > 0)
1264 ldlm_lock_decref(&rs->rs_locks[nlocks],
1265 rs->rs_modes[nlocks]);
1267 spin_lock(&svc->srv_lock);
1270 rs->rs_scheduled = 0;
1272 if (!rs->rs_on_net) {
1274 svc->srv_n_difficult_replies--;
1275 spin_unlock(&svc->srv_lock);
1277 class_export_put (exp);
1278 rs->rs_export = NULL;
1279 ptlrpc_rs_decref (rs);
1280 atomic_dec (&svc->srv_outstanding_replies);
1284 /* still on the net; callback will schedule */
1285 spin_unlock(&svc->srv_lock);
1290 /* FIXME make use of timeout later */
1292 liblustre_check_services (void *arg)
1294 int did_something = 0;
1296 struct list_head *tmp, *nxt;
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);
1305 if (svc->srv_threads_running != 0) /* I've recursed */
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. */
1313 svc->srv_threads_running++;
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;
1324 svc->srv_threads_running--;
1327 RETURN(did_something);
1329 #define ptlrpc_stop_all_threads(s) do {} while (0)
1331 #else /* __KERNEL__ */
1333 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
1334 void ptlrpc_daemonize(char *name)
1336 struct fs_struct *fs = current->fs;
1338 atomic_inc(&fs->count);
1339 cfs_daemonize(name);
1340 exit_fs(cfs_current());
1342 ll_set_fs_pwd(current->fs, init_task.fs->pwdmnt, init_task.fs->pwd);
1346 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
1348 int avail = svc->srv_nrqbd_receiving;
1349 int low_water = test_req_buffer_pressure ? 0 :
1350 svc->srv_nbuf_per_group/2;
1352 /* NB I'm not locking; just looking. */
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
1359 if (avail <= low_water)
1360 ptlrpc_grow_req_bufs(svc);
1363 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
1368 ptlrpc_retry_rqbds(void *arg)
1370 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
1372 svc->srv_rqbd_timeout = 0;
1373 return (-ETIMEDOUT);
1376 static int ptlrpc_main(void *arg)
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;
1388 int counter = 0, rc = 0;
1391 ptlrpc_daemonize(data->name);
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) {
1399 for (cpu = 0, num_cpu = 0; cpu < num_possible_cpus(); cpu++) {
1400 if (!cpu_online(cpu))
1402 if (num_cpu == thread->t_id % num_online_cpus())
1406 set_cpus_allowed(cfs_current(), node_to_cpumask(cpu_to_node(cpu)));
1410 #ifdef WITH_GROUP_INFO
1411 ginfo = groups_alloc(0);
1417 set_current_groups(ginfo);
1418 put_group_info(ginfo);
1421 if (svc->srv_init != NULL) {
1422 rc = svc->srv_init(thread);
1427 rc = lu_context_init(&env.le_ctx, svc->srv_ctx_tags);
1431 thread->t_env = &env;
1432 env.le_ctx.lc_thread = thread;
1434 /* Alloc reply state structure for this one */
1435 OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
1441 /* Record that the thread is running */
1442 thread->t_flags = SVC_RUNNING;
1444 * wake up our creator. Note: @data is invalid after this point,
1445 * because it's allocated on ptlrpc_start_thread() stack.
1447 cfs_waitq_signal(&thread->t_ctl_waitq);
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);
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);
1459 CDEBUG(D_NET, "service thread %d (#%d)started\n", thread->t_id,
1460 svc->srv_threads_running);
1462 /* XXX maintain a list of all managed devices: insert here */
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);
1470 lc_watchdog_disable(watchdog);
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))) ||
1487 lc_watchdog_touch_ms(watchdog, max_t(int, obd_timeout,
1489 at_get(&svc->srv_at_estimate)) *
1490 svc->srv_watchdog_factor);
1492 ptlrpc_check_rqbd_pool(svc);
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);
1500 if (!list_empty(&svc->srv_reply_queue))
1501 ptlrpc_server_handle_reply(svc);
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)
1512 if (svc->srv_at_check)
1513 ptlrpc_at_check_timed(svc);
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);
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);
1534 lc_watchdog_delete(watchdog);
1538 * deconstruct service specific state created by ptlrpc_start_thread()
1540 if (svc->srv_done != NULL)
1541 svc->srv_done(thread);
1543 lu_context_fini(&env.le_ctx);
1545 CDEBUG(D_NET, "service thread %d exiting: rc %d\n", thread->t_id, rc);
1547 spin_lock(&svc->srv_lock);
1548 svc->srv_threads_running--; /* must know immediately */
1550 thread->t_flags = SVC_STOPPED;
1552 cfs_waitq_signal(&thread->t_ctl_waitq);
1553 spin_unlock(&svc->srv_lock);
1558 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
1559 struct ptlrpc_thread *thread)
1561 struct l_wait_info lwi = { 0 };
1563 spin_lock(&svc->srv_lock);
1564 thread->t_flags = SVC_STOPPING;
1565 spin_unlock(&svc->srv_lock);
1567 cfs_waitq_broadcast(&svc->srv_waitq);
1568 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
1571 spin_lock(&svc->srv_lock);
1572 list_del(&thread->t_link);
1573 spin_unlock(&svc->srv_lock);
1575 OBD_FREE_PTR(thread);
1578 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
1580 struct ptlrpc_thread *thread;
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);
1587 spin_unlock(&svc->srv_lock);
1588 ptlrpc_stop_thread(svc, thread);
1589 spin_lock(&svc->srv_lock);
1592 spin_unlock(&svc->srv_lock);
1595 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc)
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 */
1609 CERROR("cannot start %s thread #%d: rc %d\n",
1610 svc->srv_thread_name, i, rc);
1611 ptlrpc_stop_all_threads(svc);
1617 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc)
1619 struct l_wait_info lwi = { 0 };
1620 struct ptlrpc_svc_data d;
1621 struct ptlrpc_thread *thread;
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))
1634 OBD_ALLOC_PTR(thread);
1637 cfs_waitq_init(&thread->t_ctl_waitq);
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);
1645 list_add(&thread->t_link, &svc->srv_threads);
1646 id = svc->srv_threads_started++;
1647 spin_unlock(&svc->srv_lock);
1650 sprintf(name, "%s_%02d", svc->srv_thread_name, id);
1656 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
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.
1661 rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
1663 CERROR("cannot start thread '%s': rc %d\n", name, rc);
1665 spin_lock(&svc->srv_lock);
1666 list_del(&thread->t_link);
1667 --svc->srv_threads_started;
1668 spin_unlock(&svc->srv_lock);
1670 OBD_FREE(thread, sizeof(*thread));
1673 l_wait_event(thread->t_ctl_waitq,
1674 thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
1676 rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
1681 int ptlrpc_unregister_service(struct ptlrpc_service *service)
1684 struct l_wait_info lwi;
1685 struct list_head *tmp;
1686 struct ptlrpc_reply_state *rs, *t;
1688 cfs_timer_disarm(&service->srv_at_timer);
1690 ptlrpc_stop_all_threads(service);
1691 LASSERT(list_empty(&service->srv_threads));
1693 spin_lock (&ptlrpc_all_services_lock);
1694 list_del_init (&service->srv_list);
1695 spin_unlock (&ptlrpc_all_services_lock);
1697 ptlrpc_lprocfs_unregister_service(service);
1699 /* All history will be culled when the next request buffer is
1701 service->srv_max_history_rqbds = 0;
1703 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
1705 rc = LNetClearLazyPortal(service->srv_req_portal);
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,
1715 rc = LNetMDUnlink(rqbd->rqbd_md_h);
1716 LASSERT (rc == 0 || rc == -ENOENT);
1719 /* Wait for the network to release any buffers it's currently
1722 spin_lock(&service->srv_lock);
1723 rc = service->srv_nrqbd_receiving;
1724 spin_unlock(&service->srv_lock);
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,
1735 if (rc == -ETIMEDOUT)
1736 CWARN("Service %s waiting for request buffers\n",
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);
1748 spin_unlock(&service->srv_lock);
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,
1759 list_del(&req->rq_list);
1760 service->srv_n_queued_reqs--;
1761 service->srv_n_active_reqs++;
1762 ptlrpc_server_free_request(req);
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,
1770 list_del(&req->rq_list);
1771 service->srv_n_queued_reqs--;
1772 service->srv_n_active_reqs++;
1774 ptlrpc_server_free_request(req);
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));
1781 /* Now free all the request buffers since nothing references them
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,
1789 ptlrpc_free_rqbd(rqbd);
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);
1797 rc = l_wait_event(service->srv_waitq,
1798 !list_empty(&service->srv_reply_queue), &lwi);
1799 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1802 ptlrpc_server_handle_reply(service);
1805 CWARN("Unexpectedly long timeout %p\n", service);
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);
1813 /* In case somebody rearmed this in the meantime */
1814 cfs_timer_disarm(&service->srv_at_timer);
1816 OBD_FREE_PTR(service);
1820 /* Returns 0 if the service is healthy.
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)
1827 struct ptlrpc_request *request;
1828 struct timeval right_now;
1834 do_gettimeofday(&right_now);
1836 spin_lock(&svc->srv_lock);
1837 if (list_empty(&svc->srv_request_queue)) {
1838 spin_unlock(&svc->srv_lock);
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);
1848 if ((timediff / ONE_MILLION) > (AT_OFF ? obd_timeout * 3/2 :
1850 CERROR("%s: unhealthy - request has been waiting %lds\n",
1851 svc->srv_name, timediff / ONE_MILLION);