1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_RPC
39 #include <liblustre.h>
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <lustre_net.h>
44 #include <lu_object.h>
45 #include <lnet/types.h>
46 #include "ptlrpc_internal.h"
48 /* The following are visible and mutable through /sys/module/ptlrpc */
49 int test_req_buffer_pressure = 0;
50 CFS_MODULE_PARM(test_req_buffer_pressure, "i", int, 0444,
51 "set non-zero to put pressure on request buffer pools");
52 unsigned int at_min = 0;
53 CFS_MODULE_PARM(at_min, "i", int, 0644,
54 "Adaptive timeout minimum (sec)");
55 unsigned int at_max = 600;
56 EXPORT_SYMBOL(at_max);
57 CFS_MODULE_PARM(at_max, "i", int, 0644,
58 "Adaptive timeout maximum (sec)");
59 unsigned int at_history = 600;
60 CFS_MODULE_PARM(at_history, "i", int, 0644,
61 "Adaptive timeouts remember the slowest event that took place "
62 "within this period (sec)");
63 static int at_early_margin = 5;
64 CFS_MODULE_PARM(at_early_margin, "i", int, 0644,
65 "How soon before an RPC deadline to send an early reply");
66 static int at_extra = 30;
67 CFS_MODULE_PARM(at_extra, "i", int, 0644,
68 "How much extra time to give with each early reply");
72 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
74 static CFS_LIST_HEAD(ptlrpc_all_services);
75 spinlock_t ptlrpc_all_services_lock;
78 ptlrpc_alloc_request_buffer (int size)
82 if (size > SVC_BUF_VMALLOC_THRESHOLD)
83 OBD_VMALLOC(ptr, size);
91 ptlrpc_free_request_buffer (char *ptr, int size)
93 if (size > SVC_BUF_VMALLOC_THRESHOLD)
99 struct ptlrpc_request_buffer_desc *
100 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
102 struct ptlrpc_request_buffer_desc *rqbd;
108 rqbd->rqbd_service = svc;
109 rqbd->rqbd_refcount = 0;
110 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
111 rqbd->rqbd_cbid.cbid_arg = rqbd;
112 CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
113 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
115 if (rqbd->rqbd_buffer == NULL) {
120 spin_lock(&svc->srv_lock);
121 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
123 spin_unlock(&svc->srv_lock);
129 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
131 struct ptlrpc_service *svc = rqbd->rqbd_service;
133 LASSERT (rqbd->rqbd_refcount == 0);
134 LASSERT (list_empty(&rqbd->rqbd_reqs));
136 spin_lock(&svc->srv_lock);
137 list_del(&rqbd->rqbd_list);
139 spin_unlock(&svc->srv_lock);
141 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
146 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
148 struct ptlrpc_request_buffer_desc *rqbd;
151 CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
152 svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
153 svc->srv_nrqbd_receiving, svc->srv_nbufs);
154 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
155 rqbd = ptlrpc_alloc_rqbd(svc);
158 CERROR ("%s: Can't allocate request buffer\n",
163 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
171 ptlrpc_save_lock (struct ptlrpc_request *req,
172 struct lustre_handle *lock, int mode)
174 struct ptlrpc_reply_state *rs = req->rq_reply_state;
178 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
180 idx = rs->rs_nlocks++;
181 rs->rs_locks[idx] = *lock;
182 rs->rs_modes[idx] = mode;
183 rs->rs_difficult = 1;
187 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
189 struct ptlrpc_service *svc = rs->rs_service;
192 LASSERT (spin_is_locked (&svc->srv_lock));
194 LASSERT (rs->rs_difficult);
195 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
197 if (rs->rs_scheduled) /* being set up or already notified */
200 rs->rs_scheduled = 1;
201 list_del (&rs->rs_list);
202 list_add (&rs->rs_list, &svc->srv_reply_queue);
203 cfs_waitq_signal (&svc->srv_waitq);
207 ptlrpc_commit_replies (struct obd_device *obd)
209 struct list_head *tmp;
210 struct list_head *nxt;
212 /* Find any replies that have been committed and get their service
213 * to attend to complete them. */
215 /* CAVEAT EMPTOR: spinlock ordering!!! */
216 spin_lock(&obd->obd_uncommitted_replies_lock);
218 list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
219 struct ptlrpc_reply_state *rs =
220 list_entry(tmp, struct ptlrpc_reply_state, rs_obd_list);
222 LASSERT (rs->rs_difficult);
224 if (rs->rs_transno <= obd->obd_last_committed) {
225 struct ptlrpc_service *svc = rs->rs_service;
227 spin_lock (&svc->srv_lock);
228 list_del_init (&rs->rs_obd_list);
229 ptlrpc_schedule_difficult_reply (rs);
230 spin_unlock (&svc->srv_lock);
234 spin_unlock(&obd->obd_uncommitted_replies_lock);
238 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
240 struct ptlrpc_request_buffer_desc *rqbd;
245 spin_lock(&svc->srv_lock);
247 if (list_empty (&svc->srv_idle_rqbds)) {
248 spin_unlock(&svc->srv_lock);
252 rqbd = list_entry(svc->srv_idle_rqbds.next,
253 struct ptlrpc_request_buffer_desc,
255 list_del (&rqbd->rqbd_list);
257 /* assume we will post successfully */
258 svc->srv_nrqbd_receiving++;
259 list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
261 spin_unlock(&svc->srv_lock);
263 rc = ptlrpc_register_rqbd(rqbd);
270 spin_lock(&svc->srv_lock);
272 svc->srv_nrqbd_receiving--;
273 list_del(&rqbd->rqbd_list);
274 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
276 /* Don't complain if no request buffers are posted right now; LNET
277 * won't drop requests because we set the portal lazy! */
279 spin_unlock(&svc->srv_lock);
284 struct ptlrpc_service *ptlrpc_init_svc_conf(struct ptlrpc_service_conf *c,
285 svc_handler_t h, char *name,
286 struct proc_dir_entry *proc_entry,
287 svcreq_printfn_t prntfn,
290 return ptlrpc_init_svc(c->psc_nbufs, c->psc_bufsize,
291 c->psc_max_req_size, c->psc_max_reply_size,
292 c->psc_req_portal, c->psc_rep_portal,
293 c->psc_watchdog_factor,
295 prntfn, c->psc_min_threads, c->psc_max_threads,
296 threadname, c->psc_ctx_tags);
298 EXPORT_SYMBOL(ptlrpc_init_svc_conf);
300 static void ptlrpc_at_timer(unsigned long castmeharder)
302 struct ptlrpc_service *svc = (struct ptlrpc_service *)castmeharder;
303 svc->srv_at_check = 1;
304 svc->srv_at_checktime = cfs_time_current();
305 cfs_waitq_signal(&svc->srv_waitq);
308 /* @threadname should be 11 characters or less - 3 will be added on */
309 struct ptlrpc_service *
310 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
311 int req_portal, int rep_portal, int watchdog_factor,
312 svc_handler_t handler, char *name,
313 cfs_proc_dir_entry_t *proc_entry,
314 svcreq_printfn_t svcreq_printfn,
315 int min_threads, int max_threads,
316 char *threadname, __u32 ctx_tags)
319 struct ptlrpc_service *service;
323 LASSERT (bufsize >= max_req_size + SPTLRPC_MAX_PAYLOAD);
324 LASSERT (ctx_tags != 0);
326 OBD_ALLOC_PTR(service);
330 /* First initialise enough for early teardown */
332 service->srv_name = name;
333 spin_lock_init(&service->srv_lock);
334 CFS_INIT_LIST_HEAD(&service->srv_threads);
335 cfs_waitq_init(&service->srv_waitq);
337 service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
338 service->srv_max_req_size = max_req_size + SPTLRPC_MAX_PAYLOAD;
339 service->srv_buf_size = bufsize;
340 service->srv_rep_portal = rep_portal;
341 service->srv_req_portal = req_portal;
342 service->srv_watchdog_factor = watchdog_factor;
343 service->srv_handler = handler;
344 service->srv_request_history_print_fn = svcreq_printfn;
345 service->srv_request_seq = 1; /* valid seq #s start at 1 */
346 service->srv_request_max_cull_seq = 0;
347 service->srv_threads_min = min_threads;
348 service->srv_threads_max = max_threads;
349 service->srv_thread_name = threadname;
350 service->srv_ctx_tags = ctx_tags;
352 rc = LNetSetLazyPortal(service->srv_req_portal);
355 CFS_INIT_LIST_HEAD(&service->srv_request_queue);
356 CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
357 CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
358 CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
359 CFS_INIT_LIST_HEAD(&service->srv_request_history);
360 CFS_INIT_LIST_HEAD(&service->srv_active_replies);
361 CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
362 CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
363 cfs_waitq_init(&service->srv_free_rs_waitq);
365 spin_lock_init(&service->srv_at_lock);
366 CFS_INIT_LIST_HEAD(&service->srv_req_in_queue);
367 CFS_INIT_LIST_HEAD(&service->srv_at_list);
368 cfs_timer_init(&service->srv_at_timer, ptlrpc_at_timer, service);
369 /* At SOW, service time should be quick; 10s seems generous. If client
370 timeout is less than this, we'll be sending an early reply. */
371 at_init(&service->srv_at_estimate, 10, 0);
373 spin_lock (&ptlrpc_all_services_lock);
374 list_add (&service->srv_list, &ptlrpc_all_services);
375 spin_unlock (&ptlrpc_all_services_lock);
377 /* Now allocate the request buffers */
378 rc = ptlrpc_grow_req_bufs(service);
379 /* We shouldn't be under memory pressure at startup, so
380 * fail if we can't post all our buffers at this time. */
384 /* Now allocate pool of reply buffers */
385 /* Increase max reply size to next power of two */
386 service->srv_max_reply_size = 1;
387 while (service->srv_max_reply_size <
388 max_reply_size + SPTLRPC_MAX_PAYLOAD)
389 service->srv_max_reply_size <<= 1;
391 if (proc_entry != NULL)
392 ptlrpc_lprocfs_register_service(proc_entry, service);
394 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
395 service->srv_name, service->srv_req_portal);
399 ptlrpc_unregister_service(service);
404 * to actually free the request, must be called without holding svc_lock.
405 * note it's caller's responsibility to unlink req->rq_list.
407 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
409 LASSERT(atomic_read(&req->rq_refcount) == 0);
410 LASSERT(list_empty(&req->rq_timed_list));
412 /* DEBUG_REQ() assumes the reply state of a request with a valid
413 * ref will not be destroyed until that reference is dropped. */
414 ptlrpc_req_drop_rs(req);
416 sptlrpc_svc_ctx_decref(req);
418 if (req != &req->rq_rqbd->rqbd_req) {
419 /* NB request buffers use an embedded
420 * req if the incoming req unlinked the
421 * MD; this isn't one of them! */
422 OBD_FREE(req, sizeof(*req));
427 * drop a reference count of the request. if it reaches 0, we either
428 * put it into history list, or free it immediately.
430 static void ptlrpc_server_drop_request(struct ptlrpc_request *req)
432 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
433 struct ptlrpc_service *svc = rqbd->rqbd_service;
435 struct list_head *tmp;
436 struct list_head *nxt;
438 if (!atomic_dec_and_test(&req->rq_refcount))
441 spin_lock(&svc->srv_lock);
443 svc->srv_n_active_reqs--;
444 list_add(&req->rq_list, &rqbd->rqbd_reqs);
446 refcount = --(rqbd->rqbd_refcount);
448 /* request buffer is now idle: add to history */
449 list_del(&rqbd->rqbd_list);
450 list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
451 svc->srv_n_history_rqbds++;
453 /* cull some history?
454 * I expect only about 1 or 2 rqbds need to be recycled here */
455 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
456 rqbd = list_entry(svc->srv_history_rqbds.next,
457 struct ptlrpc_request_buffer_desc,
460 list_del(&rqbd->rqbd_list);
461 svc->srv_n_history_rqbds--;
463 /* remove rqbd's reqs from svc's req history while
464 * I've got the service lock */
465 list_for_each(tmp, &rqbd->rqbd_reqs) {
466 req = list_entry(tmp, struct ptlrpc_request,
468 /* Track the highest culled req seq */
469 if (req->rq_history_seq >
470 svc->srv_request_max_cull_seq)
471 svc->srv_request_max_cull_seq =
473 list_del(&req->rq_history_list);
476 spin_unlock(&svc->srv_lock);
478 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
479 req = list_entry(rqbd->rqbd_reqs.next,
480 struct ptlrpc_request,
482 list_del(&req->rq_list);
483 ptlrpc_server_free_request(req);
486 spin_lock(&svc->srv_lock);
488 * now all reqs including the embedded req has been
489 * disposed, schedule request buffer for re-use.
491 LASSERT(atomic_read(&rqbd->rqbd_req.rq_refcount) == 0);
492 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
495 spin_unlock(&svc->srv_lock);
496 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
497 /* If we are low on memory, we are not interested in history */
498 list_del(&req->rq_list);
499 list_del_init(&req->rq_history_list);
500 spin_unlock(&svc->srv_lock);
502 ptlrpc_server_free_request(req);
504 spin_unlock(&svc->srv_lock);
509 * to finish a request: stop sending more early replies, and release
510 * the request. should be called after we finished handling the request.
512 static void ptlrpc_server_finish_request(struct ptlrpc_request *req)
514 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
516 if (req->rq_phase != RQ_PHASE_NEW) /* incorrect message magic */
517 DEBUG_REQ(D_INFO, req, "free req");
519 spin_lock(&svc->srv_at_lock);
520 req->rq_sent_final = 1;
521 list_del_init(&req->rq_timed_list);
522 spin_unlock(&svc->srv_at_lock);
524 ptlrpc_server_drop_request(req);
527 /* This function makes sure dead exports are evicted in a timely manner.
528 This function is only called when some export receives a message (i.e.,
529 the network is up.) */
530 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
532 struct obd_export *oldest_exp;
539 /* Compensate for slow machines, etc, by faking our request time
540 into the future. Although this can break the strict time-ordering
541 of the list, we can be really lazy here - we don't have to evict
542 at the exact right moment. Eventually, all silent exports
543 will make it to the top of the list. */
544 exp->exp_last_request_time = max(exp->exp_last_request_time,
545 cfs_time_current_sec() + extra_delay);
547 CDEBUG(D_HA, "updating export %s at "CFS_TIME_T" exp %p\n",
548 exp->exp_client_uuid.uuid,
549 exp->exp_last_request_time, exp);
551 /* exports may get disconnected from the chain even though the
552 export has references, so we must keep the spin lock while
553 manipulating the lists */
554 spin_lock(&exp->exp_obd->obd_dev_lock);
556 if (list_empty(&exp->exp_obd_chain_timed)) {
557 /* this one is not timed */
558 spin_unlock(&exp->exp_obd->obd_dev_lock);
563 list_move_tail(&exp->exp_obd_chain_timed,
564 &exp->exp_obd->obd_exports_timed);
566 oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
567 struct obd_export, exp_obd_chain_timed);
568 oldest_time = oldest_exp->exp_last_request_time;
569 spin_unlock(&exp->exp_obd->obd_dev_lock);
571 if (exp->exp_obd->obd_recovering) {
572 /* be nice to everyone during recovery */
577 /* Note - racing to start/reset the obd_eviction timer is safe */
578 if (exp->exp_obd->obd_eviction_timer == 0) {
579 /* Check if the oldest entry is expired. */
580 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
582 /* We need a second timer, in case the net was down and
583 * it just came back. Since the pinger may skip every
584 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
585 * we better wait for 3. */
586 exp->exp_obd->obd_eviction_timer =
587 cfs_time_current_sec() + 3 * PING_INTERVAL;
588 CDEBUG(D_HA, "%s: Think about evicting %s from "CFS_TIME_T"\n",
589 exp->exp_obd->obd_name, obd_export_nid2str(exp),
593 if (cfs_time_current_sec() >
594 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
595 /* The evictor won't evict anyone who we've heard from
596 * recently, so we don't have to check before we start
598 if (!ping_evictor_wake(exp))
599 exp->exp_obd->obd_eviction_timer = 0;
606 static int ptlrpc_check_req(struct ptlrpc_request *req)
608 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
609 req->rq_export->exp_conn_cnt)) {
610 DEBUG_REQ(D_ERROR, req,
611 "DROPPING req from old connection %d < %d",
612 lustre_msg_get_conn_cnt(req->rq_reqmsg),
613 req->rq_export->exp_conn_cnt);
616 if (unlikely(req->rq_export->exp_obd &&
617 req->rq_export->exp_obd->obd_fail)) {
618 /* Failing over, don't handle any more reqs, send
619 error response instead. */
620 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
621 req, req->rq_export->exp_obd->obd_name);
622 req->rq_status = -ENODEV;
630 static void ptlrpc_at_set_timer(struct ptlrpc_service *svc)
632 struct ptlrpc_request *rq;
635 spin_lock(&svc->srv_at_lock);
636 if (list_empty(&svc->srv_at_list)) {
637 cfs_timer_disarm(&svc->srv_at_timer);
638 spin_unlock(&svc->srv_at_lock);
642 /* Set timer for closest deadline */
643 rq = list_entry(svc->srv_at_list.next, struct ptlrpc_request,
645 next = (__s32)(rq->rq_deadline - cfs_time_current_sec() -
648 ptlrpc_at_timer((unsigned long)svc);
650 cfs_timer_arm(&svc->srv_at_timer, cfs_time_shift(next));
651 spin_unlock(&svc->srv_at_lock);
652 CDEBUG(D_INFO, "armed %s at %+ds\n", svc->srv_name, next);
655 /* Add rpc to early reply check list */
656 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
658 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
659 struct ptlrpc_request *rq;
665 if (req->rq_no_reply)
668 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
671 spin_lock(&svc->srv_at_lock);
673 if (unlikely(req->rq_sent_final)) {
674 spin_unlock(&svc->srv_at_lock);
678 LASSERT(list_empty(&req->rq_timed_list));
679 /* Add to sorted list. Presumably latest rpcs will have the latest
680 deadlines, so search backward. */
681 list_for_each_entry_reverse(rq, &svc->srv_at_list, rq_timed_list) {
682 if (req->rq_deadline >= rq->rq_deadline) {
683 list_add(&req->rq_timed_list, &rq->rq_timed_list);
689 /* Add to front if shortest deadline or list empty */
690 list_add(&req->rq_timed_list, &svc->srv_at_list);
692 /* Check if we're the head of the list */
693 found = (svc->srv_at_list.next == &req->rq_timed_list);
695 spin_unlock(&svc->srv_at_lock);
698 ptlrpc_at_set_timer(svc);
703 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req,
706 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
707 struct ptlrpc_request *reqcopy;
708 struct lustre_msg *reqmsg;
709 cfs_duration_t olddl = req->rq_deadline - cfs_time_current_sec();
714 /* deadline is when the client expects us to reply, margin is the
715 difference between clients' and servers' expectations */
716 DEBUG_REQ(D_ADAPTTO, req,
717 "%ssending early reply (deadline %+lds, margin %+lds) for "
718 "%d+%d", AT_OFF ? "AT off - not " : "",
719 olddl, olddl - at_get(&svc->srv_at_estimate),
720 at_get(&svc->srv_at_estimate), extra_time);
726 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), "
727 "not sending early reply. Consider increasing "
728 "at_early_margin (%d)?", olddl, at_early_margin);
730 /* Return an error so we're not re-added to the timed list. */
734 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
735 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, "
736 "but no AT support");
741 /* Fake our processing time into the future to ask the
742 clients for some extra amount of time */
743 extra_time += cfs_time_current_sec() -
744 req->rq_arrival_time.tv_sec;
745 at_add(&svc->srv_at_estimate, extra_time);
748 newdl = req->rq_arrival_time.tv_sec + at_get(&svc->srv_at_estimate);
749 if (req->rq_deadline >= newdl) {
750 /* We're not adding any time, no need to send an early reply
751 (e.g. maybe at adaptive_max) */
752 DEBUG_REQ(D_WARNING, req, "Couldn't add any time ("
753 CFS_DURATION_T"/"CFS_DURATION_T"), "
754 "not sending early reply\n", olddl,
755 cfs_time_sub(newdl, cfs_time_current_sec()));
759 OBD_ALLOC(reqcopy, sizeof *reqcopy);
762 OBD_ALLOC(reqmsg, req->rq_reqlen);
764 OBD_FREE(reqcopy, sizeof *reqcopy);
769 reqcopy->rq_reply_state = NULL;
770 reqcopy->rq_rep_swab_mask = 0;
771 reqcopy->rq_pack_bulk = 0;
772 reqcopy->rq_pack_udesc = 0;
773 reqcopy->rq_packed_final = 0;
774 sptlrpc_svc_ctx_addref(reqcopy);
775 /* We only need the reqmsg for the magic */
776 reqcopy->rq_reqmsg = reqmsg;
777 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
779 if (req->rq_sent_final) {
780 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, "
781 "abort sending early reply\n");
786 reqcopy->rq_export = class_conn2export(
787 lustre_msg_get_handle(reqcopy->rq_reqmsg));
788 if (reqcopy->rq_export == NULL)
789 GOTO(out, rc = -ENODEV);
792 class_export_rpc_get(reqcopy->rq_export);
793 if (reqcopy->rq_export->exp_obd &&
794 reqcopy->rq_export->exp_obd->obd_fail)
795 GOTO(out_put, rc = -ENODEV);
797 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
801 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
804 /* Adjust our own deadline to what we told the client */
805 req->rq_deadline = newdl;
806 req->rq_early_count++; /* number sent, server side */
808 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
811 /* Free the (early) reply state from lustre_pack_reply.
812 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
813 ptlrpc_req_drop_rs(reqcopy);
816 class_export_rpc_put(reqcopy->rq_export);
817 class_export_put(reqcopy->rq_export);
819 sptlrpc_svc_ctx_decref(reqcopy);
820 OBD_FREE(reqmsg, req->rq_reqlen);
821 OBD_FREE(reqcopy, sizeof *reqcopy);
825 /* Send early replies to everybody expiring within at_early_margin
826 asking for at_extra time */
827 static int ptlrpc_at_check_timed(struct ptlrpc_service *svc)
829 struct ptlrpc_request *rq, *n;
830 struct list_head work_list;
831 time_t now = cfs_time_current_sec();
832 cfs_duration_t delay;
833 int first, counter = 0;
836 spin_lock(&svc->srv_at_lock);
837 if (svc->srv_at_check == 0) {
838 spin_unlock(&svc->srv_at_lock);
841 delay = cfs_time_sub(cfs_time_current(), svc->srv_at_checktime);
842 svc->srv_at_check = 0;
844 if (list_empty(&svc->srv_at_list)) {
845 spin_unlock(&svc->srv_at_lock);
849 /* The timer went off, but maybe the nearest rpc already completed. */
850 rq = list_entry(svc->srv_at_list.next, struct ptlrpc_request,
852 first = (int)(rq->rq_deadline - now);
853 if (first > at_early_margin) {
854 /* We've still got plenty of time. Reset the timer. */
855 spin_unlock(&svc->srv_at_lock);
856 ptlrpc_at_set_timer(svc);
860 /* We're close to a timeout, and we don't know how much longer the
861 server will take. Send early replies to everyone expiring soon. */
862 CFS_INIT_LIST_HEAD(&work_list);
863 list_for_each_entry_safe(rq, n, &svc->srv_at_list, rq_timed_list) {
864 if (rq->rq_deadline <= now + at_early_margin) {
865 list_move_tail(&rq->rq_timed_list, &work_list);
872 spin_unlock(&svc->srv_at_lock);
874 /* we have a new earliest deadline, restart the timer */
875 ptlrpc_at_set_timer(svc);
877 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
878 "replies\n", first, at_extra, counter);
880 /* We're already past request deadlines before we even get a
881 chance to send early replies */
882 LCONSOLE_WARN("%s: This server is not able to keep up with "
883 "request traffic (cpu-bound).\n", svc->srv_name);
884 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
885 "delay="CFS_DURATION_T"(jiff)\n",
886 counter, svc->srv_n_queued_reqs, svc->srv_n_active_reqs,
887 at_get(&svc->srv_at_estimate), delay);
890 /* ptlrpc_server_finish_request may delete an entry out of
892 spin_lock(&svc->srv_at_lock);
893 while (!list_empty(&work_list)) {
894 rq = list_entry(work_list.next, struct ptlrpc_request,
896 list_del_init(&rq->rq_timed_list);
897 /* if the entry is still in the worklist, it hasn't been
898 deleted, and is safe to take a ref to keep the req around */
899 atomic_inc(&rq->rq_refcount);
900 spin_unlock(&svc->srv_at_lock);
902 if (ptlrpc_at_send_early_reply(rq, at_extra) == 0)
903 ptlrpc_at_add_timed(rq);
905 ptlrpc_server_drop_request(rq);
906 spin_lock(&svc->srv_at_lock);
908 spin_unlock(&svc->srv_at_lock);
913 /* Handle freshly incoming reqs, add to timed early reply list,
914 pass on to regular request queue */
916 ptlrpc_server_handle_req_in(struct ptlrpc_service *svc)
918 struct ptlrpc_request *req;
925 spin_lock(&svc->srv_lock);
926 if (list_empty(&svc->srv_req_in_queue)) {
927 spin_unlock(&svc->srv_lock);
931 req = list_entry(svc->srv_req_in_queue.next,
932 struct ptlrpc_request, rq_list);
933 list_del_init (&req->rq_list);
934 /* Consider this still a "queued" request as far as stats are
936 spin_unlock(&svc->srv_lock);
938 /* go through security check/transform */
939 rc = sptlrpc_svc_unwrap_request(req);
943 case SECSVC_COMPLETE:
944 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
952 /* Clear request swab mask; this is a new request */
953 req->rq_req_swab_mask = 0;
955 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
957 CERROR("error unpacking request: ptl %d from %s x"LPU64"\n",
958 svc->srv_req_portal, libcfs_id2str(req->rq_peer),
963 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
965 CERROR ("error unpacking ptlrpc body: ptl %d from %s x"
966 LPU64"\n", svc->srv_req_portal,
967 libcfs_id2str(req->rq_peer), req->rq_xid);
972 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
973 CERROR("wrong packet type received (type=%u) from %s\n",
974 lustre_msg_get_type(req->rq_reqmsg),
975 libcfs_id2str(req->rq_peer));
979 CDEBUG(D_NET, "got req "LPD64"\n", req->rq_xid);
981 req->rq_export = class_conn2export(
982 lustre_msg_get_handle(req->rq_reqmsg));
983 if (req->rq_export) {
984 rc = ptlrpc_check_req(req);
986 rc = sptlrpc_target_export_check(req->rq_export, req);
988 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
989 "illegal security flavor,");
992 class_export_put(req->rq_export);
993 req->rq_export = NULL;
998 /* req_in handling should/must be fast */
999 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
1000 DEBUG_REQ(D_WARNING, req, "Slow req_in handling "CFS_DURATION_T"s",
1001 cfs_time_sub(cfs_time_current_sec(),
1002 req->rq_arrival_time.tv_sec));
1004 /* Set rpc server deadline and add it to the timed list */
1005 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1006 MSGHDR_AT_SUPPORT) ?
1007 /* The max time the client expects us to take */
1008 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1009 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1010 if (unlikely(deadline == 0)) {
1011 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1015 ptlrpc_at_add_timed(req);
1017 /* Move it over to the request processing queue */
1018 spin_lock(&svc->srv_lock);
1019 list_add_tail(&req->rq_list, &svc->srv_request_queue);
1020 cfs_waitq_signal(&svc->srv_waitq);
1021 spin_unlock(&svc->srv_lock);
1025 spin_lock(&svc->srv_lock);
1026 svc->srv_n_queued_reqs--;
1027 svc->srv_n_active_reqs++;
1028 spin_unlock(&svc->srv_lock);
1029 ptlrpc_server_finish_request(req);
1035 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
1036 struct ptlrpc_thread *thread)
1038 struct obd_export *export = NULL;
1039 struct ptlrpc_request *request;
1040 struct timeval work_start;
1041 struct timeval work_end;
1048 spin_lock(&svc->srv_lock);
1049 if (unlikely(list_empty (&svc->srv_request_queue) ||
1052 /* !@%$# liblustre only has 1 thread */
1053 svc->srv_n_difficult_replies != 0 &&
1055 svc->srv_n_active_reqs >= (svc->srv_threads_running - 1)))) {
1056 /* Don't handle regular requests in the last thread, in order * re
1057 * to handle difficult replies (which might block other threads)
1058 * as well as handle any incoming reqs, early replies, etc.
1059 * That means we always need at least 2 service threads. */
1060 spin_unlock(&svc->srv_lock);
1064 request = list_entry (svc->srv_request_queue.next,
1065 struct ptlrpc_request, rq_list);
1066 list_del_init (&request->rq_list);
1067 svc->srv_n_queued_reqs--;
1068 svc->srv_n_active_reqs++;
1070 spin_unlock(&svc->srv_lock);
1072 if(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
1073 libcfs_debug_dumplog();
1075 do_gettimeofday(&work_start);
1076 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
1077 if (likely(svc->srv_stats != NULL)) {
1078 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
1080 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
1081 svc->srv_n_queued_reqs);
1082 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
1083 svc->srv_n_active_reqs);
1084 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
1085 at_get(&svc->srv_at_estimate));
1088 rc = lu_context_init(&request->rq_session, LCT_SESSION);
1090 CERROR("Failure to initialize session: %d\n", rc);
1093 request->rq_session.lc_thread = thread;
1094 lu_context_enter(&request->rq_session);
1096 CDEBUG(D_NET, "got req "LPU64"\n", request->rq_xid);
1098 request->rq_svc_thread = thread;
1100 request->rq_svc_thread->t_env->le_ses = &request->rq_session;
1102 request->rq_export = class_conn2export(
1103 lustre_msg_get_handle(request->rq_reqmsg));
1105 if (likely(request->rq_export)) {
1106 if (unlikely(ptlrpc_check_req(request)))
1108 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
1109 export = class_export_rpc_get(request->rq_export);
1112 /* Discard requests queued for longer than the deadline.
1113 The deadline is increased if we send an early reply. */
1114 if (cfs_time_current_sec() > request->rq_deadline) {
1115 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
1116 ": deadline "CFS_DURATION_T":"CFS_DURATION_T"s ago\n",
1117 libcfs_id2str(request->rq_peer),
1118 cfs_time_sub(request->rq_deadline,
1119 request->rq_arrival_time.tv_sec),
1120 cfs_time_sub(cfs_time_current_sec(),
1121 request->rq_deadline));
1122 goto put_rpc_export;
1125 request->rq_phase = RQ_PHASE_INTERPRET;
1127 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
1128 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1129 (request->rq_export ?
1130 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1131 (request->rq_export ?
1132 atomic_read(&request->rq_export->exp_refcount) : -99),
1133 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1134 libcfs_id2str(request->rq_peer),
1135 lustre_msg_get_opc(request->rq_reqmsg));
1137 OBD_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, obd_fail_val);
1139 rc = svc->srv_handler(request);
1141 request->rq_phase = RQ_PHASE_COMPLETE;
1143 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
1144 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1145 (request->rq_export ?
1146 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1147 (request->rq_export ?
1148 atomic_read(&request->rq_export->exp_refcount) : -99),
1149 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1150 libcfs_id2str(request->rq_peer),
1151 lustre_msg_get_opc(request->rq_reqmsg));
1155 class_export_rpc_put(export);
1157 if (likely(request->rq_export != NULL))
1158 class_export_put(request->rq_export);
1160 lu_context_exit(&request->rq_session);
1161 lu_context_fini(&request->rq_session);
1163 if (unlikely(cfs_time_current_sec() > request->rq_deadline)) {
1164 DEBUG_REQ(D_WARNING, request, "Request x"LPU64" took longer "
1165 "than estimated ("CFS_DURATION_T":"CFS_DURATION_T"s);"
1166 " client may timeout.",
1167 request->rq_xid, cfs_time_sub(request->rq_deadline,
1168 request->rq_arrival_time.tv_sec),
1169 cfs_time_sub(cfs_time_current_sec(),
1170 request->rq_deadline));
1173 do_gettimeofday(&work_end);
1174 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1175 CDEBUG(D_RPCTRACE, "request x"LPU64" opc %u from %s processed in "
1176 "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
1177 request->rq_xid, lustre_msg_get_opc(request->rq_reqmsg),
1178 libcfs_id2str(request->rq_peer), timediff,
1179 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
1180 request->rq_repmsg ? lustre_msg_get_transno(request->rq_repmsg) :
1181 request->rq_transno, request->rq_status,
1182 request->rq_repmsg ? lustre_msg_get_status(request->rq_repmsg):
1184 if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
1185 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
1186 int opc = opcode_offset(op);
1187 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
1188 LASSERT(opc < LUSTRE_MAX_OPCODES);
1189 lprocfs_counter_add(svc->srv_stats,
1190 opc + EXTRA_MAX_OPCODES,
1194 if (unlikely(request->rq_early_count)) {
1195 DEBUG_REQ(D_ADAPTTO, request,
1196 "sent %d early replies before finishing in "
1198 request->rq_early_count,
1199 cfs_time_sub(work_end.tv_sec,
1200 request->rq_arrival_time.tv_sec));
1204 ptlrpc_server_finish_request(request);
1210 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
1212 struct ptlrpc_reply_state *rs;
1213 struct obd_export *exp;
1214 struct obd_device *obd;
1219 spin_lock(&svc->srv_lock);
1220 if (list_empty (&svc->srv_reply_queue)) {
1221 spin_unlock(&svc->srv_lock);
1225 rs = list_entry (svc->srv_reply_queue.next,
1226 struct ptlrpc_reply_state, rs_list);
1228 exp = rs->rs_export;
1231 LASSERT (rs->rs_difficult);
1232 LASSERT (rs->rs_scheduled);
1234 list_del_init (&rs->rs_list);
1236 /* Disengage from notifiers carefully (lock order - irqrestore below!)*/
1237 spin_unlock(&svc->srv_lock);
1239 spin_lock (&obd->obd_uncommitted_replies_lock);
1240 /* Noop if removed already */
1241 list_del_init (&rs->rs_obd_list);
1242 spin_unlock (&obd->obd_uncommitted_replies_lock);
1244 spin_lock (&exp->exp_lock);
1245 /* Noop if removed already */
1246 list_del_init (&rs->rs_exp_list);
1247 spin_unlock (&exp->exp_lock);
1249 spin_lock(&svc->srv_lock);
1251 been_handled = rs->rs_handled;
1254 nlocks = rs->rs_nlocks; /* atomic "steal", but */
1255 rs->rs_nlocks = 0; /* locks still on rs_locks! */
1257 if (nlocks == 0 && !been_handled) {
1258 /* If we see this, we should already have seen the warning
1259 * in mds_steal_ack_locks() */
1260 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
1263 rs->rs_xid, rs->rs_transno,
1264 lustre_msg_get_opc(rs->rs_msg),
1265 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1268 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1269 spin_unlock(&svc->srv_lock);
1271 if (!been_handled && rs->rs_on_net) {
1272 LNetMDUnlink(rs->rs_md_h);
1273 /* Ignore return code; we're racing with
1277 while (nlocks-- > 0)
1278 ldlm_lock_decref(&rs->rs_locks[nlocks],
1279 rs->rs_modes[nlocks]);
1281 spin_lock(&svc->srv_lock);
1284 rs->rs_scheduled = 0;
1286 if (!rs->rs_on_net) {
1288 svc->srv_n_difficult_replies--;
1289 spin_unlock(&svc->srv_lock);
1291 class_export_put (exp);
1292 rs->rs_export = NULL;
1293 ptlrpc_rs_decref (rs);
1294 atomic_dec (&svc->srv_outstanding_replies);
1298 /* still on the net; callback will schedule */
1299 spin_unlock(&svc->srv_lock);
1304 /* FIXME make use of timeout later */
1306 liblustre_check_services (void *arg)
1308 int did_something = 0;
1310 struct list_head *tmp, *nxt;
1313 /* I'm relying on being single threaded, not to have to lock
1314 * ptlrpc_all_services etc */
1315 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
1316 struct ptlrpc_service *svc =
1317 list_entry (tmp, struct ptlrpc_service, srv_list);
1319 if (svc->srv_threads_running != 0) /* I've recursed */
1322 /* service threads can block for bulk, so this limits us
1323 * (arbitrarily) to recursing 1 stack frame per service.
1324 * Note that the problem with recursion is that we have to
1325 * unwind completely before our caller can resume. */
1327 svc->srv_threads_running++;
1330 rc = ptlrpc_server_handle_req_in(svc);
1331 rc |= ptlrpc_server_handle_reply(svc);
1332 rc |= ptlrpc_at_check_timed(svc);
1333 rc |= ptlrpc_server_handle_request(svc, NULL);
1334 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
1335 did_something |= rc;
1338 svc->srv_threads_running--;
1341 RETURN(did_something);
1343 #define ptlrpc_stop_all_threads(s) do {} while (0)
1345 #else /* __KERNEL__ */
1347 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
1348 void ptlrpc_daemonize(char *name)
1350 struct fs_struct *fs = current->fs;
1352 atomic_inc(&fs->count);
1353 cfs_daemonize(name);
1354 exit_fs(cfs_current());
1356 ll_set_fs_pwd(current->fs, init_task.fs->pwdmnt, init_task.fs->pwd);
1360 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
1362 int avail = svc->srv_nrqbd_receiving;
1363 int low_water = test_req_buffer_pressure ? 0 :
1364 svc->srv_nbuf_per_group/2;
1366 /* NB I'm not locking; just looking. */
1368 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
1369 * allowed the request history to grow out of control. We could put a
1370 * sanity check on that here and cull some history if we need the
1373 if (avail <= low_water)
1374 ptlrpc_grow_req_bufs(svc);
1377 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
1382 ptlrpc_retry_rqbds(void *arg)
1384 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
1386 svc->srv_rqbd_timeout = 0;
1387 return (-ETIMEDOUT);
1390 static int ptlrpc_main(void *arg)
1392 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
1393 struct ptlrpc_service *svc = data->svc;
1394 struct ptlrpc_thread *thread = data->thread;
1395 struct obd_device *dev = data->dev;
1396 struct ptlrpc_reply_state *rs;
1397 struct lc_watchdog *watchdog;
1398 #ifdef WITH_GROUP_INFO
1399 struct group_info *ginfo = NULL;
1402 int counter = 0, rc = 0;
1405 ptlrpc_daemonize(data->name);
1407 #if defined(HAVE_NODE_TO_CPUMASK) && defined(CONFIG_NUMA)
1408 /* we need to do this before any per-thread allocation is done so that
1409 * we get the per-thread allocations on local node. bug 7342 */
1410 if (svc->srv_cpu_affinity) {
1413 for (cpu = 0, num_cpu = 0; cpu < num_possible_cpus(); cpu++) {
1414 if (!cpu_online(cpu))
1416 if (num_cpu == thread->t_id % num_online_cpus())
1420 set_cpus_allowed(cfs_current(), node_to_cpumask(cpu_to_node(cpu)));
1424 #ifdef WITH_GROUP_INFO
1425 ginfo = groups_alloc(0);
1431 set_current_groups(ginfo);
1432 put_group_info(ginfo);
1435 if (svc->srv_init != NULL) {
1436 rc = svc->srv_init(thread);
1441 rc = lu_context_init(&env.le_ctx, svc->srv_ctx_tags);
1445 thread->t_env = &env;
1446 env.le_ctx.lc_thread = thread;
1448 /* Alloc reply state structure for this one */
1449 OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
1455 /* Record that the thread is running */
1456 thread->t_flags = SVC_RUNNING;
1458 * wake up our creator. Note: @data is invalid after this point,
1459 * because it's allocated on ptlrpc_start_thread() stack.
1461 cfs_waitq_signal(&thread->t_ctl_waitq);
1463 watchdog = lc_watchdog_add(max_t(int, obd_timeout, AT_OFF ? 0 :
1464 at_get(&svc->srv_at_estimate)) *
1465 svc->srv_watchdog_factor, NULL, NULL);
1467 spin_lock(&svc->srv_lock);
1468 svc->srv_threads_running++;
1469 list_add(&rs->rs_list, &svc->srv_free_rs_list);
1470 spin_unlock(&svc->srv_lock);
1471 cfs_waitq_signal(&svc->srv_free_rs_waitq);
1473 CDEBUG(D_NET, "service thread %d (#%d)started\n", thread->t_id,
1474 svc->srv_threads_running);
1476 /* XXX maintain a list of all managed devices: insert here */
1478 while ((thread->t_flags & SVC_STOPPING) == 0 ||
1479 svc->srv_n_difficult_replies != 0) {
1480 /* Don't exit while there are replies to be handled */
1481 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
1482 ptlrpc_retry_rqbds, svc);
1484 lc_watchdog_disable(watchdog);
1488 l_wait_event_exclusive (svc->srv_waitq,
1489 ((thread->t_flags & SVC_STOPPING) != 0 &&
1490 svc->srv_n_difficult_replies == 0) ||
1491 (!list_empty(&svc->srv_idle_rqbds) &&
1492 svc->srv_rqbd_timeout == 0) ||
1493 !list_empty(&svc->srv_req_in_queue) ||
1494 !list_empty(&svc->srv_reply_queue) ||
1495 (!list_empty(&svc->srv_request_queue) &&
1496 (svc->srv_n_active_reqs <
1497 (svc->srv_threads_running - 1))) ||
1501 lc_watchdog_touch_ms(watchdog, max_t(int, obd_timeout,
1503 at_get(&svc->srv_at_estimate)) *
1504 svc->srv_watchdog_factor);
1506 ptlrpc_check_rqbd_pool(svc);
1508 if ((svc->srv_threads_started < svc->srv_threads_max) &&
1509 (svc->srv_n_active_reqs >= (svc->srv_threads_started - 1))){
1510 /* Ignore return code - we tried... */
1511 ptlrpc_start_thread(dev, svc);
1514 if (!list_empty(&svc->srv_reply_queue))
1515 ptlrpc_server_handle_reply(svc);
1517 if (!list_empty(&svc->srv_req_in_queue)) {
1518 /* Process all incoming reqs before handling any */
1519 ptlrpc_server_handle_req_in(svc);
1520 /* but limit ourselves in case of flood */
1521 if (counter++ < 1000)
1526 if (svc->srv_at_check)
1527 ptlrpc_at_check_timed(svc);
1529 /* don't handle requests in the last thread */
1530 if (!list_empty (&svc->srv_request_queue) &&
1531 (svc->srv_n_active_reqs < (svc->srv_threads_running - 1))) {
1532 lu_context_enter(&env.le_ctx);
1533 ptlrpc_server_handle_request(svc, thread);
1534 lu_context_exit(&env.le_ctx);
1537 if (!list_empty(&svc->srv_idle_rqbds) &&
1538 ptlrpc_server_post_idle_rqbds(svc) < 0) {
1539 /* I just failed to repost request buffers. Wait
1540 * for a timeout (unless something else happens)
1541 * before I try again */
1542 svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
1543 CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
1544 svc->srv_nrqbd_receiving);
1548 lc_watchdog_delete(watchdog);
1552 * deconstruct service specific state created by ptlrpc_start_thread()
1554 if (svc->srv_done != NULL)
1555 svc->srv_done(thread);
1557 lu_context_fini(&env.le_ctx);
1559 CDEBUG(D_NET, "service thread %d exiting: rc %d\n", thread->t_id, rc);
1561 spin_lock(&svc->srv_lock);
1562 svc->srv_threads_running--; /* must know immediately */
1564 thread->t_flags = SVC_STOPPED;
1566 cfs_waitq_signal(&thread->t_ctl_waitq);
1567 spin_unlock(&svc->srv_lock);
1572 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
1573 struct ptlrpc_thread *thread)
1575 struct l_wait_info lwi = { 0 };
1577 spin_lock(&svc->srv_lock);
1578 thread->t_flags = SVC_STOPPING;
1579 spin_unlock(&svc->srv_lock);
1581 cfs_waitq_broadcast(&svc->srv_waitq);
1582 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
1585 spin_lock(&svc->srv_lock);
1586 list_del(&thread->t_link);
1587 spin_unlock(&svc->srv_lock);
1589 OBD_FREE_PTR(thread);
1592 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
1594 struct ptlrpc_thread *thread;
1596 spin_lock(&svc->srv_lock);
1597 while (!list_empty(&svc->srv_threads)) {
1598 thread = list_entry(svc->srv_threads.next,
1599 struct ptlrpc_thread, t_link);
1601 spin_unlock(&svc->srv_lock);
1602 ptlrpc_stop_thread(svc, thread);
1603 spin_lock(&svc->srv_lock);
1606 spin_unlock(&svc->srv_lock);
1609 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc)
1614 /* We require 2 threads min - see note in
1615 ptlrpc_server_handle_request */
1616 LASSERT(svc->srv_threads_min >= 2);
1617 for (i = 0; i < svc->srv_threads_min; i++) {
1618 rc = ptlrpc_start_thread(dev, svc);
1619 /* We have enough threads, don't start more. b=15759 */
1623 CERROR("cannot start %s thread #%d: rc %d\n",
1624 svc->srv_thread_name, i, rc);
1625 ptlrpc_stop_all_threads(svc);
1631 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc)
1633 struct l_wait_info lwi = { 0 };
1634 struct ptlrpc_svc_data d;
1635 struct ptlrpc_thread *thread;
1640 CDEBUG(D_RPCTRACE, "%s started %d min %d max %d running %d\n",
1641 svc->srv_name, svc->srv_threads_started, svc->srv_threads_min,
1642 svc->srv_threads_max, svc->srv_threads_running);
1643 if (unlikely(svc->srv_threads_started >= svc->srv_threads_max) ||
1644 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
1645 svc->srv_threads_started == svc->srv_threads_min - 1))
1648 OBD_ALLOC_PTR(thread);
1651 cfs_waitq_init(&thread->t_ctl_waitq);
1653 spin_lock(&svc->srv_lock);
1654 if (svc->srv_threads_started >= svc->srv_threads_max) {
1655 spin_unlock(&svc->srv_lock);
1656 OBD_FREE_PTR(thread);
1659 list_add(&thread->t_link, &svc->srv_threads);
1660 id = svc->srv_threads_started++;
1661 spin_unlock(&svc->srv_lock);
1664 sprintf(name, "%s_%02d", svc->srv_thread_name, id);
1670 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
1672 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
1673 * just drop the VM and FILES in ptlrpc_daemonize() right away.
1675 rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
1677 CERROR("cannot start thread '%s': rc %d\n", name, rc);
1679 spin_lock(&svc->srv_lock);
1680 list_del(&thread->t_link);
1681 --svc->srv_threads_started;
1682 spin_unlock(&svc->srv_lock);
1684 OBD_FREE(thread, sizeof(*thread));
1687 l_wait_event(thread->t_ctl_waitq,
1688 thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
1690 rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
1695 int ptlrpc_unregister_service(struct ptlrpc_service *service)
1698 struct l_wait_info lwi;
1699 struct list_head *tmp;
1700 struct ptlrpc_reply_state *rs, *t;
1702 cfs_timer_disarm(&service->srv_at_timer);
1704 ptlrpc_stop_all_threads(service);
1705 LASSERT(list_empty(&service->srv_threads));
1707 spin_lock (&ptlrpc_all_services_lock);
1708 list_del_init (&service->srv_list);
1709 spin_unlock (&ptlrpc_all_services_lock);
1711 ptlrpc_lprocfs_unregister_service(service);
1713 /* All history will be culled when the next request buffer is
1715 service->srv_max_history_rqbds = 0;
1717 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
1719 rc = LNetClearLazyPortal(service->srv_req_portal);
1722 /* Unlink all the request buffers. This forces a 'final' event with
1723 * its 'unlink' flag set for each posted rqbd */
1724 list_for_each(tmp, &service->srv_active_rqbds) {
1725 struct ptlrpc_request_buffer_desc *rqbd =
1726 list_entry(tmp, struct ptlrpc_request_buffer_desc,
1729 rc = LNetMDUnlink(rqbd->rqbd_md_h);
1730 LASSERT (rc == 0 || rc == -ENOENT);
1733 /* Wait for the network to release any buffers it's currently
1736 spin_lock(&service->srv_lock);
1737 rc = service->srv_nrqbd_receiving;
1738 spin_unlock(&service->srv_lock);
1743 /* Network access will complete in finite time but the HUGE
1744 * timeout lets us CWARN for visibility of sluggish NALs */
1745 lwi = LWI_TIMEOUT(cfs_time_seconds(LONG_UNLINK), NULL, NULL);
1746 rc = l_wait_event(service->srv_waitq,
1747 service->srv_nrqbd_receiving == 0,
1749 if (rc == -ETIMEDOUT)
1750 CWARN("Service %s waiting for request buffers\n",
1754 /* schedule all outstanding replies to terminate them */
1755 spin_lock(&service->srv_lock);
1756 while (!list_empty(&service->srv_active_replies)) {
1757 struct ptlrpc_reply_state *rs =
1758 list_entry(service->srv_active_replies.next,
1759 struct ptlrpc_reply_state, rs_list);
1760 ptlrpc_schedule_difficult_reply(rs);
1762 spin_unlock(&service->srv_lock);
1764 /* purge the request queue. NB No new replies (rqbds all unlinked)
1765 * and no service threads, so I'm the only thread noodling the
1766 * request queue now */
1767 while (!list_empty(&service->srv_req_in_queue)) {
1768 struct ptlrpc_request *req =
1769 list_entry(service->srv_req_in_queue.next,
1770 struct ptlrpc_request,
1773 list_del(&req->rq_list);
1774 service->srv_n_queued_reqs--;
1775 service->srv_n_active_reqs++;
1776 ptlrpc_server_finish_request(req);
1778 while (!list_empty(&service->srv_request_queue)) {
1779 struct ptlrpc_request *req =
1780 list_entry(service->srv_request_queue.next,
1781 struct ptlrpc_request,
1784 list_del(&req->rq_list);
1785 service->srv_n_queued_reqs--;
1786 service->srv_n_active_reqs++;
1788 ptlrpc_server_finish_request(req);
1790 LASSERT(service->srv_n_queued_reqs == 0);
1791 LASSERT(service->srv_n_active_reqs == 0);
1792 LASSERT(service->srv_n_history_rqbds == 0);
1793 LASSERT(list_empty(&service->srv_active_rqbds));
1795 /* Now free all the request buffers since nothing references them
1797 while (!list_empty(&service->srv_idle_rqbds)) {
1798 struct ptlrpc_request_buffer_desc *rqbd =
1799 list_entry(service->srv_idle_rqbds.next,
1800 struct ptlrpc_request_buffer_desc,
1803 ptlrpc_free_rqbd(rqbd);
1806 /* wait for all outstanding replies to complete (they were
1807 * scheduled having been flagged to abort above) */
1808 while (atomic_read(&service->srv_outstanding_replies) != 0) {
1809 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
1811 rc = l_wait_event(service->srv_waitq,
1812 !list_empty(&service->srv_reply_queue), &lwi);
1813 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1816 ptlrpc_server_handle_reply(service);
1819 CWARN("Unexpectedly long timeout %p\n", service);
1822 list_for_each_entry_safe(rs, t, &service->srv_free_rs_list, rs_list) {
1823 list_del(&rs->rs_list);
1824 OBD_FREE(rs, service->srv_max_reply_size);
1827 /* In case somebody rearmed this in the meantime */
1828 cfs_timer_disarm(&service->srv_at_timer);
1830 OBD_FREE_PTR(service);
1834 /* Returns 0 if the service is healthy.
1836 * Right now, it just checks to make sure that requests aren't languishing
1837 * in the queue. We'll use this health check to govern whether a node needs
1838 * to be shot, so it's intentionally non-aggressive. */
1839 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
1841 struct ptlrpc_request *request;
1842 struct timeval right_now;
1848 do_gettimeofday(&right_now);
1850 spin_lock(&svc->srv_lock);
1851 if (list_empty(&svc->srv_request_queue)) {
1852 spin_unlock(&svc->srv_lock);
1856 /* How long has the next entry been waiting? */
1857 request = list_entry(svc->srv_request_queue.next,
1858 struct ptlrpc_request, rq_list);
1859 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
1860 spin_unlock(&svc->srv_lock);
1862 if ((timediff / ONE_MILLION) > (AT_OFF ? obd_timeout * 3/2 :
1864 CERROR("%s: unhealthy - request has been waiting %lds\n",
1865 svc->srv_name, timediff / ONE_MILLION);