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 CFS_MODULE_PARM(at_min, "i", int, 0644,
53 "Adaptive timeout minimum (sec)");
54 CFS_MODULE_PARM(at_max, "i", int, 0644,
55 "Adaptive timeout maximum (sec)");
56 CFS_MODULE_PARM(at_history, "i", int, 0644,
57 "Adaptive timeouts remember the slowest event that took place "
58 "within this period (sec)");
59 CFS_MODULE_PARM(at_early_margin, "i", int, 0644,
60 "How soon before an RPC deadline to send an early reply");
61 CFS_MODULE_PARM(at_extra, "i", int, 0644,
62 "How much extra time to give with each early reply");
66 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
68 static CFS_LIST_HEAD(ptlrpc_all_services);
69 cfs_spinlock_t ptlrpc_all_services_lock;
72 ptlrpc_alloc_request_buffer (int size)
76 if (size > SVC_BUF_VMALLOC_THRESHOLD)
77 OBD_VMALLOC(ptr, size);
85 ptlrpc_free_request_buffer (char *ptr, int size)
87 if (size > SVC_BUF_VMALLOC_THRESHOLD)
93 struct ptlrpc_request_buffer_desc *
94 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
96 struct ptlrpc_request_buffer_desc *rqbd;
102 rqbd->rqbd_service = svc;
103 rqbd->rqbd_refcount = 0;
104 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
105 rqbd->rqbd_cbid.cbid_arg = rqbd;
106 CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
107 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
109 if (rqbd->rqbd_buffer == NULL) {
114 cfs_spin_lock(&svc->srv_lock);
115 cfs_list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
117 cfs_spin_unlock(&svc->srv_lock);
123 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
125 struct ptlrpc_service *svc = rqbd->rqbd_service;
127 LASSERT (rqbd->rqbd_refcount == 0);
128 LASSERT (cfs_list_empty(&rqbd->rqbd_reqs));
130 cfs_spin_lock(&svc->srv_lock);
131 cfs_list_del(&rqbd->rqbd_list);
133 cfs_spin_unlock(&svc->srv_lock);
135 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
140 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
142 struct ptlrpc_request_buffer_desc *rqbd;
145 CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
146 svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
147 svc->srv_nrqbd_receiving, svc->srv_nbufs);
148 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
149 rqbd = ptlrpc_alloc_rqbd(svc);
152 CERROR ("%s: Can't allocate request buffer\n",
157 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
165 ptlrpc_save_lock(struct ptlrpc_request *req,
166 struct lustre_handle *lock, int mode, int no_ack)
168 struct ptlrpc_reply_state *rs = req->rq_reply_state;
172 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
174 if (req->rq_export->exp_disconnected) {
175 ldlm_lock_decref(lock, mode);
177 idx = rs->rs_nlocks++;
178 rs->rs_locks[idx] = *lock;
179 rs->rs_modes[idx] = mode;
180 rs->rs_difficult = 1;
181 rs->rs_no_ack = !!no_ack;
187 #define HRT_RUNNING 0
188 #define HRT_STOPPING 1
190 struct ptlrpc_hr_thread {
191 cfs_spinlock_t hrt_lock;
192 unsigned long hrt_flags;
193 cfs_waitq_t hrt_wait;
194 cfs_list_t hrt_queue;
195 cfs_completion_t hrt_completion;
198 struct ptlrpc_hr_service {
202 struct ptlrpc_hr_thread hr_threads[0];
206 cfs_list_t rsb_replies;
207 struct ptlrpc_service *rsb_svc;
208 unsigned int rsb_n_replies;
212 * A pointer to per-node reply handling service.
214 static struct ptlrpc_hr_service *ptlrpc_hr = NULL;
217 * maximum mumber of replies scheduled in one batch
219 #define MAX_SCHEDULED 256
222 * Initialize a reply batch.
226 static void rs_batch_init(struct rs_batch *b)
228 memset(b, 0, sizeof *b);
229 CFS_INIT_LIST_HEAD(&b->rsb_replies);
233 * Dispatch all replies accumulated in the batch to one from
234 * dedicated reply handling threads.
238 static void rs_batch_dispatch(struct rs_batch *b)
240 if (b->rsb_n_replies != 0) {
241 struct ptlrpc_hr_service *hr = ptlrpc_hr;
244 idx = hr->hr_index++;
245 if (hr->hr_index >= hr->hr_n_threads)
248 cfs_spin_lock(&hr->hr_threads[idx].hrt_lock);
249 cfs_list_splice_init(&b->rsb_replies,
250 &hr->hr_threads[idx].hrt_queue);
251 cfs_spin_unlock(&hr->hr_threads[idx].hrt_lock);
252 cfs_waitq_signal(&hr->hr_threads[idx].hrt_wait);
253 b->rsb_n_replies = 0;
258 * Add a reply to a batch.
259 * Add one reply object to a batch, schedule batched replies if overload.
264 static void rs_batch_add(struct rs_batch *b, struct ptlrpc_reply_state *rs)
266 struct ptlrpc_service *svc = rs->rs_service;
268 if (svc != b->rsb_svc || b->rsb_n_replies >= MAX_SCHEDULED) {
269 if (b->rsb_svc != NULL) {
270 rs_batch_dispatch(b);
271 cfs_spin_unlock(&b->rsb_svc->srv_lock);
273 cfs_spin_lock(&svc->srv_lock);
276 cfs_spin_lock(&rs->rs_lock);
277 rs->rs_scheduled_ever = 1;
278 if (rs->rs_scheduled == 0) {
279 cfs_list_move(&rs->rs_list, &b->rsb_replies);
280 rs->rs_scheduled = 1;
283 rs->rs_committed = 1;
284 cfs_spin_unlock(&rs->rs_lock);
288 * Reply batch finalization.
289 * Dispatch remaining replies from the batch
290 * and release remaining spinlock.
294 static void rs_batch_fini(struct rs_batch *b)
296 if (b->rsb_svc != 0) {
297 rs_batch_dispatch(b);
298 cfs_spin_unlock(&b->rsb_svc->srv_lock);
302 #define DECLARE_RS_BATCH(b) struct rs_batch b
304 #else /* __KERNEL__ */
306 #define rs_batch_init(b) do{}while(0)
307 #define rs_batch_fini(b) do{}while(0)
308 #define rs_batch_add(b, r) ptlrpc_schedule_difficult_reply(r)
309 #define DECLARE_RS_BATCH(b)
311 #endif /* __KERNEL__ */
313 void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs)
316 struct ptlrpc_hr_service *hr = ptlrpc_hr;
320 LASSERT(cfs_list_empty(&rs->rs_list));
322 idx = hr->hr_index++;
323 if (hr->hr_index >= hr->hr_n_threads)
325 cfs_spin_lock(&hr->hr_threads[idx].hrt_lock);
326 cfs_list_add_tail(&rs->rs_list, &hr->hr_threads[idx].hrt_queue);
327 cfs_spin_unlock(&hr->hr_threads[idx].hrt_lock);
328 cfs_waitq_signal(&hr->hr_threads[idx].hrt_wait);
331 cfs_list_add_tail(&rs->rs_list, &rs->rs_service->srv_reply_queue);
336 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
340 LASSERT_SPIN_LOCKED(&rs->rs_service->srv_lock);
341 LASSERT_SPIN_LOCKED(&rs->rs_lock);
342 LASSERT (rs->rs_difficult);
343 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
345 if (rs->rs_scheduled) { /* being set up or already notified */
350 rs->rs_scheduled = 1;
351 cfs_list_del_init(&rs->rs_list);
352 ptlrpc_dispatch_difficult_reply(rs);
356 void ptlrpc_commit_replies(struct obd_export *exp)
358 struct ptlrpc_reply_state *rs, *nxt;
359 DECLARE_RS_BATCH(batch);
362 rs_batch_init(&batch);
363 /* Find any replies that have been committed and get their service
364 * to attend to complete them. */
366 /* CAVEAT EMPTOR: spinlock ordering!!! */
367 cfs_spin_lock(&exp->exp_uncommitted_replies_lock);
368 cfs_list_for_each_entry_safe(rs, nxt, &exp->exp_uncommitted_replies,
370 LASSERT (rs->rs_difficult);
371 /* VBR: per-export last_committed */
372 LASSERT(rs->rs_export);
373 if (rs->rs_transno <= exp->exp_last_committed) {
374 cfs_list_del_init(&rs->rs_obd_list);
375 rs_batch_add(&batch, rs);
378 cfs_spin_unlock(&exp->exp_uncommitted_replies_lock);
379 rs_batch_fini(&batch);
384 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
386 struct ptlrpc_request_buffer_desc *rqbd;
391 cfs_spin_lock(&svc->srv_lock);
393 if (cfs_list_empty (&svc->srv_idle_rqbds)) {
394 cfs_spin_unlock(&svc->srv_lock);
398 rqbd = cfs_list_entry(svc->srv_idle_rqbds.next,
399 struct ptlrpc_request_buffer_desc,
401 cfs_list_del (&rqbd->rqbd_list);
403 /* assume we will post successfully */
404 svc->srv_nrqbd_receiving++;
405 cfs_list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
407 cfs_spin_unlock(&svc->srv_lock);
409 rc = ptlrpc_register_rqbd(rqbd);
416 cfs_spin_lock(&svc->srv_lock);
418 svc->srv_nrqbd_receiving--;
419 cfs_list_del(&rqbd->rqbd_list);
420 cfs_list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
422 /* Don't complain if no request buffers are posted right now; LNET
423 * won't drop requests because we set the portal lazy! */
425 cfs_spin_unlock(&svc->srv_lock);
430 struct ptlrpc_service *ptlrpc_init_svc_conf(struct ptlrpc_service_conf *c,
431 svc_handler_t h, char *name,
432 struct proc_dir_entry *proc_entry,
433 svcreq_printfn_t prntfn,
436 return ptlrpc_init_svc(c->psc_nbufs, c->psc_bufsize,
437 c->psc_max_req_size, c->psc_max_reply_size,
438 c->psc_req_portal, c->psc_rep_portal,
439 c->psc_watchdog_factor,
441 prntfn, c->psc_min_threads, c->psc_max_threads,
442 threadname, c->psc_ctx_tags, NULL);
444 EXPORT_SYMBOL(ptlrpc_init_svc_conf);
446 static void ptlrpc_at_timer(unsigned long castmeharder)
448 struct ptlrpc_service *svc = (struct ptlrpc_service *)castmeharder;
449 svc->srv_at_check = 1;
450 svc->srv_at_checktime = cfs_time_current();
451 cfs_waitq_signal(&svc->srv_waitq);
454 /* @threadname should be 11 characters or less - 3 will be added on */
455 struct ptlrpc_service *
456 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
457 int req_portal, int rep_portal, int watchdog_factor,
458 svc_handler_t handler, char *name,
459 cfs_proc_dir_entry_t *proc_entry,
460 svcreq_printfn_t svcreq_printfn,
461 int min_threads, int max_threads,
462 char *threadname, __u32 ctx_tags,
463 svc_hpreq_handler_t hp_handler)
466 struct ptlrpc_at_array *array;
467 struct ptlrpc_service *service;
468 unsigned int size, index;
472 LASSERT (bufsize >= max_req_size + SPTLRPC_MAX_PAYLOAD);
473 LASSERT (ctx_tags != 0);
475 OBD_ALLOC_PTR(service);
479 /* First initialise enough for early teardown */
481 service->srv_name = name;
482 cfs_spin_lock_init(&service->srv_lock);
483 CFS_INIT_LIST_HEAD(&service->srv_threads);
484 cfs_waitq_init(&service->srv_waitq);
486 service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
487 service->srv_max_req_size = max_req_size + SPTLRPC_MAX_PAYLOAD;
488 service->srv_buf_size = bufsize;
489 service->srv_rep_portal = rep_portal;
490 service->srv_req_portal = req_portal;
491 service->srv_watchdog_factor = watchdog_factor;
492 service->srv_handler = handler;
493 service->srv_request_history_print_fn = svcreq_printfn;
494 service->srv_request_seq = 1; /* valid seq #s start at 1 */
495 service->srv_request_max_cull_seq = 0;
496 service->srv_threads_min = min_threads;
497 service->srv_threads_max = max_threads;
498 service->srv_thread_name = threadname;
499 service->srv_ctx_tags = ctx_tags;
500 service->srv_hpreq_handler = hp_handler;
501 service->srv_hpreq_ratio = PTLRPC_SVC_HP_RATIO;
502 service->srv_hpreq_count = 0;
503 service->srv_n_hpreq = 0;
505 rc = LNetSetLazyPortal(service->srv_req_portal);
508 CFS_INIT_LIST_HEAD(&service->srv_request_queue);
509 CFS_INIT_LIST_HEAD(&service->srv_request_hpq);
510 CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
511 CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
512 CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
513 CFS_INIT_LIST_HEAD(&service->srv_request_history);
514 CFS_INIT_LIST_HEAD(&service->srv_active_replies);
516 CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
518 CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
519 cfs_waitq_init(&service->srv_free_rs_waitq);
520 cfs_atomic_set(&service->srv_n_difficult_replies, 0);
522 cfs_spin_lock_init(&service->srv_at_lock);
523 CFS_INIT_LIST_HEAD(&service->srv_req_in_queue);
525 array = &service->srv_at_array;
526 size = at_est2timeout(at_max);
527 array->paa_size = size;
528 array->paa_count = 0;
529 array->paa_deadline = -1;
531 /* allocate memory for srv_at_array (ptlrpc_at_array) */
532 OBD_ALLOC(array->paa_reqs_array, sizeof(cfs_list_t) * size);
533 if (array->paa_reqs_array == NULL)
536 for (index = 0; index < size; index++)
537 CFS_INIT_LIST_HEAD(&array->paa_reqs_array[index]);
539 OBD_ALLOC(array->paa_reqs_count, sizeof(__u32) * size);
540 if (array->paa_reqs_count == NULL)
543 cfs_timer_init(&service->srv_at_timer, ptlrpc_at_timer, service);
544 /* At SOW, service time should be quick; 10s seems generous. If client
545 timeout is less than this, we'll be sending an early reply. */
546 at_init(&service->srv_at_estimate, 10, 0);
548 cfs_spin_lock (&ptlrpc_all_services_lock);
549 cfs_list_add (&service->srv_list, &ptlrpc_all_services);
550 cfs_spin_unlock (&ptlrpc_all_services_lock);
552 /* Now allocate the request buffers */
553 rc = ptlrpc_grow_req_bufs(service);
554 /* We shouldn't be under memory pressure at startup, so
555 * fail if we can't post all our buffers at this time. */
559 /* Now allocate pool of reply buffers */
560 /* Increase max reply size to next power of two */
561 service->srv_max_reply_size = 1;
562 while (service->srv_max_reply_size <
563 max_reply_size + SPTLRPC_MAX_PAYLOAD)
564 service->srv_max_reply_size <<= 1;
566 if (proc_entry != NULL)
567 ptlrpc_lprocfs_register_service(proc_entry, service);
569 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
570 service->srv_name, service->srv_req_portal);
574 ptlrpc_unregister_service(service);
579 * to actually free the request, must be called without holding svc_lock.
580 * note it's caller's responsibility to unlink req->rq_list.
582 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
584 LASSERT(cfs_atomic_read(&req->rq_refcount) == 0);
585 LASSERT(cfs_list_empty(&req->rq_timed_list));
587 /* DEBUG_REQ() assumes the reply state of a request with a valid
588 * ref will not be destroyed until that reference is dropped. */
589 ptlrpc_req_drop_rs(req);
591 sptlrpc_svc_ctx_decref(req);
593 if (req != &req->rq_rqbd->rqbd_req) {
594 /* NB request buffers use an embedded
595 * req if the incoming req unlinked the
596 * MD; this isn't one of them! */
597 OBD_FREE(req, sizeof(*req));
602 * drop a reference count of the request. if it reaches 0, we either
603 * put it into history list, or free it immediately.
605 void ptlrpc_server_drop_request(struct ptlrpc_request *req)
607 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
608 struct ptlrpc_service *svc = rqbd->rqbd_service;
613 if (!cfs_atomic_dec_and_test(&req->rq_refcount))
616 cfs_spin_lock(&svc->srv_at_lock);
617 if (req->rq_at_linked) {
618 struct ptlrpc_at_array *array = &svc->srv_at_array;
619 __u32 index = req->rq_at_index;
621 LASSERT(!cfs_list_empty(&req->rq_timed_list));
622 cfs_list_del_init(&req->rq_timed_list);
623 req->rq_at_linked = 0;
624 array->paa_reqs_count[index]--;
627 LASSERT(cfs_list_empty(&req->rq_timed_list));
628 cfs_spin_unlock(&svc->srv_at_lock);
630 /* finalize request */
631 if (req->rq_export) {
632 class_export_put(req->rq_export);
633 req->rq_export = NULL;
636 cfs_spin_lock(&svc->srv_lock);
638 svc->srv_n_active_reqs--;
639 cfs_list_add(&req->rq_list, &rqbd->rqbd_reqs);
641 refcount = --(rqbd->rqbd_refcount);
643 /* request buffer is now idle: add to history */
644 cfs_list_del(&rqbd->rqbd_list);
645 cfs_list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
646 svc->srv_n_history_rqbds++;
648 /* cull some history?
649 * I expect only about 1 or 2 rqbds need to be recycled here */
650 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
651 rqbd = cfs_list_entry(svc->srv_history_rqbds.next,
652 struct ptlrpc_request_buffer_desc,
655 cfs_list_del(&rqbd->rqbd_list);
656 svc->srv_n_history_rqbds--;
658 /* remove rqbd's reqs from svc's req history while
659 * I've got the service lock */
660 cfs_list_for_each(tmp, &rqbd->rqbd_reqs) {
661 req = cfs_list_entry(tmp, struct ptlrpc_request,
663 /* Track the highest culled req seq */
664 if (req->rq_history_seq >
665 svc->srv_request_max_cull_seq)
666 svc->srv_request_max_cull_seq =
668 cfs_list_del(&req->rq_history_list);
671 cfs_spin_unlock(&svc->srv_lock);
673 cfs_list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
674 req = cfs_list_entry(rqbd->rqbd_reqs.next,
675 struct ptlrpc_request,
677 cfs_list_del(&req->rq_list);
678 ptlrpc_server_free_request(req);
681 cfs_spin_lock(&svc->srv_lock);
683 * now all reqs including the embedded req has been
684 * disposed, schedule request buffer for re-use.
686 LASSERT(cfs_atomic_read(&rqbd->rqbd_req.rq_refcount) ==
688 cfs_list_add_tail(&rqbd->rqbd_list,
689 &svc->srv_idle_rqbds);
692 cfs_spin_unlock(&svc->srv_lock);
693 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
694 /* If we are low on memory, we are not interested in history */
695 cfs_list_del(&req->rq_list);
696 cfs_list_del_init(&req->rq_history_list);
697 cfs_spin_unlock(&svc->srv_lock);
699 ptlrpc_server_free_request(req);
701 cfs_spin_unlock(&svc->srv_lock);
706 * to finish a request: stop sending more early replies, and release
707 * the request. should be called after we finished handling the request.
709 static void ptlrpc_server_finish_request(struct ptlrpc_request *req)
711 ptlrpc_server_drop_request(req);
714 /* This function makes sure dead exports are evicted in a timely manner.
715 This function is only called when some export receives a message (i.e.,
716 the network is up.) */
717 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
719 struct obd_export *oldest_exp;
720 time_t oldest_time, new_time;
726 /* Compensate for slow machines, etc, by faking our request time
727 into the future. Although this can break the strict time-ordering
728 of the list, we can be really lazy here - we don't have to evict
729 at the exact right moment. Eventually, all silent exports
730 will make it to the top of the list. */
732 /* Do not pay attention on 1sec or smaller renewals. */
733 new_time = cfs_time_current_sec() + extra_delay;
734 if (exp->exp_last_request_time + 1 /*second */ >= new_time)
737 exp->exp_last_request_time = new_time;
738 CDEBUG(D_HA, "updating export %s at "CFS_TIME_T" exp %p\n",
739 exp->exp_client_uuid.uuid,
740 exp->exp_last_request_time, exp);
742 /* exports may get disconnected from the chain even though the
743 export has references, so we must keep the spin lock while
744 manipulating the lists */
745 cfs_spin_lock(&exp->exp_obd->obd_dev_lock);
747 if (cfs_list_empty(&exp->exp_obd_chain_timed)) {
748 /* this one is not timed */
749 cfs_spin_unlock(&exp->exp_obd->obd_dev_lock);
753 cfs_list_move_tail(&exp->exp_obd_chain_timed,
754 &exp->exp_obd->obd_exports_timed);
756 oldest_exp = cfs_list_entry(exp->exp_obd->obd_exports_timed.next,
757 struct obd_export, exp_obd_chain_timed);
758 oldest_time = oldest_exp->exp_last_request_time;
759 cfs_spin_unlock(&exp->exp_obd->obd_dev_lock);
761 if (exp->exp_obd->obd_recovering) {
762 /* be nice to everyone during recovery */
767 /* Note - racing to start/reset the obd_eviction timer is safe */
768 if (exp->exp_obd->obd_eviction_timer == 0) {
769 /* Check if the oldest entry is expired. */
770 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
772 /* We need a second timer, in case the net was down and
773 * it just came back. Since the pinger may skip every
774 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
775 * we better wait for 3. */
776 exp->exp_obd->obd_eviction_timer =
777 cfs_time_current_sec() + 3 * PING_INTERVAL;
778 CDEBUG(D_HA, "%s: Think about evicting %s from "CFS_TIME_T"\n",
779 exp->exp_obd->obd_name,
780 obd_export_nid2str(oldest_exp), oldest_time);
783 if (cfs_time_current_sec() >
784 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
785 /* The evictor won't evict anyone who we've heard from
786 * recently, so we don't have to check before we start
788 if (!ping_evictor_wake(exp))
789 exp->exp_obd->obd_eviction_timer = 0;
796 static int ptlrpc_check_req(struct ptlrpc_request *req)
798 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
799 req->rq_export->exp_conn_cnt)) {
800 DEBUG_REQ(D_ERROR, req,
801 "DROPPING req from old connection %d < %d",
802 lustre_msg_get_conn_cnt(req->rq_reqmsg),
803 req->rq_export->exp_conn_cnt);
806 if (unlikely(req->rq_export->exp_obd &&
807 req->rq_export->exp_obd->obd_fail)) {
808 /* Failing over, don't handle any more reqs, send
809 error response instead. */
810 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
811 req, req->rq_export->exp_obd->obd_name);
812 req->rq_status = -ENODEV;
820 static void ptlrpc_at_set_timer(struct ptlrpc_service *svc)
822 struct ptlrpc_at_array *array = &svc->srv_at_array;
825 cfs_spin_lock(&svc->srv_at_lock);
826 if (array->paa_count == 0) {
827 cfs_timer_disarm(&svc->srv_at_timer);
828 cfs_spin_unlock(&svc->srv_at_lock);
832 /* Set timer for closest deadline */
833 next = (__s32)(array->paa_deadline - cfs_time_current_sec() -
836 ptlrpc_at_timer((unsigned long)svc);
838 cfs_timer_arm(&svc->srv_at_timer, cfs_time_shift(next));
839 cfs_spin_unlock(&svc->srv_at_lock);
840 CDEBUG(D_INFO, "armed %s at %+ds\n", svc->srv_name, next);
843 /* Add rpc to early reply check list */
844 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
846 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
847 struct ptlrpc_request *rq = NULL;
848 struct ptlrpc_at_array *array = &svc->srv_at_array;
855 if (req->rq_no_reply)
858 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
861 cfs_spin_lock(&svc->srv_at_lock);
862 LASSERT(cfs_list_empty(&req->rq_timed_list));
864 index = (unsigned long)req->rq_deadline % array->paa_size;
865 if (array->paa_reqs_count[index] > 0) {
866 /* latest rpcs will have the latest deadlines in the list,
867 * so search backward. */
868 cfs_list_for_each_entry_reverse(rq,
869 &array->paa_reqs_array[index],
871 if (req->rq_deadline >= rq->rq_deadline) {
872 cfs_list_add(&req->rq_timed_list,
879 /* Add the request at the head of the list */
880 if (cfs_list_empty(&req->rq_timed_list))
881 cfs_list_add(&req->rq_timed_list,
882 &array->paa_reqs_array[index]);
884 req->rq_at_linked = 1;
885 req->rq_at_index = index;
886 array->paa_reqs_count[index]++;
888 if (array->paa_count == 1 || array->paa_deadline > req->rq_deadline) {
889 array->paa_deadline = req->rq_deadline;
892 cfs_spin_unlock(&svc->srv_at_lock);
895 ptlrpc_at_set_timer(svc);
900 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req)
902 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
903 struct ptlrpc_request *reqcopy;
904 struct lustre_msg *reqmsg;
905 cfs_duration_t olddl = req->rq_deadline - cfs_time_current_sec();
910 /* deadline is when the client expects us to reply, margin is the
911 difference between clients' and servers' expectations */
912 DEBUG_REQ(D_ADAPTTO, req,
913 "%ssending early reply (deadline %+lds, margin %+lds) for "
914 "%d+%d", AT_OFF ? "AT off - not " : "",
915 olddl, olddl - at_get(&svc->srv_at_estimate),
916 at_get(&svc->srv_at_estimate), at_extra);
922 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), "
923 "not sending early reply. Consider increasing "
924 "at_early_margin (%d)?", olddl, at_early_margin);
926 /* Return an error so we're not re-added to the timed list. */
930 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
931 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, "
932 "but no AT support");
936 if (req->rq_export &&
937 lustre_msg_get_flags(req->rq_reqmsg) &
938 (MSG_REPLAY | MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE)) {
940 * Use at_extra as early reply period for recovery requests but
941 * make sure it is not bigger than recovery time / 4
943 at_add(&svc->srv_at_estimate,
945 req->rq_export->exp_obd->obd_recovery_timeout / 4));
947 /* Fake our processing time into the future to ask the clients
948 * for some extra amount of time */
949 at_add(&svc->srv_at_estimate, at_extra);
952 newdl = cfs_time_current_sec() + at_get(&svc->srv_at_estimate);
953 if (req->rq_deadline >= newdl) {
954 /* We're not adding any time, no need to send an early reply
955 (e.g. maybe at adaptive_max) */
956 DEBUG_REQ(D_WARNING, req, "Couldn't add any time ("
957 CFS_DURATION_T"/"CFS_DURATION_T"), "
958 "not sending early reply\n", olddl,
959 cfs_time_sub(newdl, cfs_time_current_sec()));
963 OBD_ALLOC(reqcopy, sizeof *reqcopy);
966 OBD_ALLOC(reqmsg, req->rq_reqlen);
968 OBD_FREE(reqcopy, sizeof *reqcopy);
973 reqcopy->rq_reply_state = NULL;
974 reqcopy->rq_rep_swab_mask = 0;
975 reqcopy->rq_pack_bulk = 0;
976 reqcopy->rq_pack_udesc = 0;
977 reqcopy->rq_packed_final = 0;
978 sptlrpc_svc_ctx_addref(reqcopy);
979 /* We only need the reqmsg for the magic */
980 reqcopy->rq_reqmsg = reqmsg;
981 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
983 LASSERT(cfs_atomic_read(&req->rq_refcount));
984 /** if it is last refcount then early reply isn't needed */
985 if (cfs_atomic_read(&req->rq_refcount) == 1) {
986 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, "
987 "abort sending early reply\n");
988 GOTO(out, rc = -EINVAL);
992 reqcopy->rq_export = class_conn2export(
993 lustre_msg_get_handle(reqcopy->rq_reqmsg));
994 if (reqcopy->rq_export == NULL)
995 GOTO(out, rc = -ENODEV);
998 class_export_rpc_get(reqcopy->rq_export);
999 if (reqcopy->rq_export->exp_obd &&
1000 reqcopy->rq_export->exp_obd->obd_fail)
1001 GOTO(out_put, rc = -ENODEV);
1003 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
1007 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
1010 /* Adjust our own deadline to what we told the client */
1011 req->rq_deadline = newdl;
1012 req->rq_early_count++; /* number sent, server side */
1014 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
1017 /* Free the (early) reply state from lustre_pack_reply.
1018 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
1019 ptlrpc_req_drop_rs(reqcopy);
1022 class_export_rpc_put(reqcopy->rq_export);
1023 class_export_put(reqcopy->rq_export);
1025 sptlrpc_svc_ctx_decref(reqcopy);
1026 OBD_FREE(reqmsg, req->rq_reqlen);
1027 OBD_FREE(reqcopy, sizeof *reqcopy);
1031 /* Send early replies to everybody expiring within at_early_margin
1032 asking for at_extra time */
1033 static int ptlrpc_at_check_timed(struct ptlrpc_service *svc)
1035 struct ptlrpc_request *rq, *n;
1036 cfs_list_t work_list;
1037 struct ptlrpc_at_array *array = &svc->srv_at_array;
1040 time_t now = cfs_time_current_sec();
1041 cfs_duration_t delay;
1042 int first, counter = 0;
1045 cfs_spin_lock(&svc->srv_at_lock);
1046 if (svc->srv_at_check == 0) {
1047 cfs_spin_unlock(&svc->srv_at_lock);
1050 delay = cfs_time_sub(cfs_time_current(), svc->srv_at_checktime);
1051 svc->srv_at_check = 0;
1053 if (array->paa_count == 0) {
1054 cfs_spin_unlock(&svc->srv_at_lock);
1058 /* The timer went off, but maybe the nearest rpc already completed. */
1059 first = array->paa_deadline - now;
1060 if (first > at_early_margin) {
1061 /* We've still got plenty of time. Reset the timer. */
1062 cfs_spin_unlock(&svc->srv_at_lock);
1063 ptlrpc_at_set_timer(svc);
1067 /* We're close to a timeout, and we don't know how much longer the
1068 server will take. Send early replies to everyone expiring soon. */
1069 CFS_INIT_LIST_HEAD(&work_list);
1071 index = (unsigned long)array->paa_deadline % array->paa_size;
1072 count = array->paa_count;
1074 count -= array->paa_reqs_count[index];
1075 cfs_list_for_each_entry_safe(rq, n,
1076 &array->paa_reqs_array[index],
1078 if (rq->rq_deadline <= now + at_early_margin) {
1079 cfs_list_del_init(&rq->rq_timed_list);
1081 * ptlrpc_server_drop_request() may drop
1082 * refcount to 0 already. Let's check this and
1083 * don't add entry to work_list
1085 if (likely(cfs_atomic_inc_not_zero(&rq->rq_refcount)))
1086 cfs_list_add(&rq->rq_timed_list, &work_list);
1088 array->paa_reqs_count[index]--;
1090 rq->rq_at_linked = 0;
1094 /* update the earliest deadline */
1095 if (deadline == -1 || rq->rq_deadline < deadline)
1096 deadline = rq->rq_deadline;
1101 if (++index >= array->paa_size)
1104 array->paa_deadline = deadline;
1105 cfs_spin_unlock(&svc->srv_at_lock);
1107 /* we have a new earliest deadline, restart the timer */
1108 ptlrpc_at_set_timer(svc);
1110 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
1111 "replies\n", first, at_extra, counter);
1113 /* We're already past request deadlines before we even get a
1114 chance to send early replies */
1115 LCONSOLE_WARN("%s: This server is not able to keep up with "
1116 "request traffic (cpu-bound).\n", svc->srv_name);
1117 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
1118 "delay="CFS_DURATION_T"(jiff)\n",
1119 counter, svc->srv_n_queued_reqs, svc->srv_n_active_reqs,
1120 at_get(&svc->srv_at_estimate), delay);
1123 /* we took additional refcount so entries can't be deleted from list, no
1124 * locking is needed */
1125 while (!cfs_list_empty(&work_list)) {
1126 rq = cfs_list_entry(work_list.next, struct ptlrpc_request,
1128 cfs_list_del_init(&rq->rq_timed_list);
1130 if (ptlrpc_at_send_early_reply(rq) == 0)
1131 ptlrpc_at_add_timed(rq);
1133 ptlrpc_server_drop_request(rq);
1140 * Put the request to the export list if the request may become
1141 * a high priority one.
1143 static int ptlrpc_hpreq_init(struct ptlrpc_service *svc,
1144 struct ptlrpc_request *req)
1149 if (svc->srv_hpreq_handler) {
1150 rc = svc->srv_hpreq_handler(req);
1154 if (req->rq_export && req->rq_ops) {
1155 cfs_spin_lock(&req->rq_export->exp_lock);
1156 cfs_list_add(&req->rq_exp_list,
1157 &req->rq_export->exp_queued_rpc);
1158 cfs_spin_unlock(&req->rq_export->exp_lock);
1164 /** Remove the request from the export list. */
1165 static void ptlrpc_hpreq_fini(struct ptlrpc_request *req)
1168 if (req->rq_export && req->rq_ops) {
1169 cfs_spin_lock(&req->rq_export->exp_lock);
1170 cfs_list_del_init(&req->rq_exp_list);
1171 cfs_spin_unlock(&req->rq_export->exp_lock);
1177 * Make the request a high priority one.
1179 * All the high priority requests are queued in a separate FIFO
1180 * ptlrpc_service::srv_request_hpq list which is parallel to
1181 * ptlrpc_service::srv_request_queue list but has a higher priority
1184 * \see ptlrpc_server_handle_request().
1186 static void ptlrpc_hpreq_reorder_nolock(struct ptlrpc_service *svc,
1187 struct ptlrpc_request *req)
1190 LASSERT(svc != NULL);
1191 cfs_spin_lock(&req->rq_lock);
1192 if (req->rq_hp == 0) {
1193 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1195 /* Add to the high priority queue. */
1196 cfs_list_move_tail(&req->rq_list, &svc->srv_request_hpq);
1198 if (opc != OBD_PING)
1199 DEBUG_REQ(D_NET, req, "high priority req");
1201 cfs_spin_unlock(&req->rq_lock);
1205 void ptlrpc_hpreq_reorder(struct ptlrpc_request *req)
1207 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
1210 cfs_spin_lock(&svc->srv_lock);
1211 /* It may happen that the request is already taken for the processing
1212 * but still in the export list, do not re-add it into the HP list. */
1213 if (req->rq_phase == RQ_PHASE_NEW)
1214 ptlrpc_hpreq_reorder_nolock(svc, req);
1215 cfs_spin_unlock(&svc->srv_lock);
1219 /** Check if the request is a high priority one. */
1220 static int ptlrpc_server_hpreq_check(struct ptlrpc_request *req)
1225 /* Check by request opc. */
1226 opc = lustre_msg_get_opc(req->rq_reqmsg);
1227 if (opc == OBD_PING)
1230 /* Perform request specific check. */
1231 if (req->rq_ops && req->rq_ops->hpreq_check)
1232 rc = req->rq_ops->hpreq_check(req);
1236 /** Check if a request is a high priority one. */
1237 static int ptlrpc_server_request_add(struct ptlrpc_service *svc,
1238 struct ptlrpc_request *req)
1243 rc = ptlrpc_server_hpreq_check(req);
1247 cfs_spin_lock(&svc->srv_lock);
1248 /* Before inserting the request into the queue, check if it is not
1249 * inserted yet, or even already handled -- it may happen due to
1250 * a racing ldlm_server_blocking_ast(). */
1251 if (req->rq_phase == RQ_PHASE_NEW && cfs_list_empty(&req->rq_list)) {
1253 ptlrpc_hpreq_reorder_nolock(svc, req);
1255 cfs_list_add_tail(&req->rq_list,
1256 &svc->srv_request_queue);
1258 cfs_spin_unlock(&svc->srv_lock);
1263 /* Only allow normal priority requests on a service that has a high-priority
1264 * queue if forced (i.e. cleanup), if there are other high priority requests
1265 * already being processed (i.e. those threads can service more high-priority
1266 * requests), or if there are enough idle threads that a later thread can do
1267 * a high priority request. */
1268 static int ptlrpc_server_allow_normal(struct ptlrpc_service *svc, int force)
1270 return force || !svc->srv_hpreq_handler || svc->srv_n_hpreq > 0 ||
1271 svc->srv_n_active_reqs < svc->srv_threads_running - 2;
1274 static struct ptlrpc_request *
1275 ptlrpc_server_request_get(struct ptlrpc_service *svc, int force)
1277 struct ptlrpc_request *req = NULL;
1280 if (ptlrpc_server_allow_normal(svc, force) &&
1281 !cfs_list_empty(&svc->srv_request_queue) &&
1282 (cfs_list_empty(&svc->srv_request_hpq) ||
1283 svc->srv_hpreq_count >= svc->srv_hpreq_ratio)) {
1284 req = cfs_list_entry(svc->srv_request_queue.next,
1285 struct ptlrpc_request, rq_list);
1286 svc->srv_hpreq_count = 0;
1287 } else if (!cfs_list_empty(&svc->srv_request_hpq)) {
1288 req = cfs_list_entry(svc->srv_request_hpq.next,
1289 struct ptlrpc_request, rq_list);
1290 svc->srv_hpreq_count++;
1295 static int ptlrpc_server_request_pending(struct ptlrpc_service *svc, int force)
1297 return ((ptlrpc_server_allow_normal(svc, force) &&
1298 !cfs_list_empty(&svc->srv_request_queue)) ||
1299 !cfs_list_empty(&svc->srv_request_hpq));
1302 /* Handle freshly incoming reqs, add to timed early reply list,
1303 pass on to regular request queue */
1305 ptlrpc_server_handle_req_in(struct ptlrpc_service *svc)
1307 struct ptlrpc_request *req;
1314 cfs_spin_lock(&svc->srv_lock);
1315 if (cfs_list_empty(&svc->srv_req_in_queue)) {
1316 cfs_spin_unlock(&svc->srv_lock);
1320 req = cfs_list_entry(svc->srv_req_in_queue.next,
1321 struct ptlrpc_request, rq_list);
1322 cfs_list_del_init (&req->rq_list);
1323 /* Consider this still a "queued" request as far as stats are
1325 cfs_spin_unlock(&svc->srv_lock);
1327 /* go through security check/transform */
1328 rc = sptlrpc_svc_unwrap_request(req);
1332 case SECSVC_COMPLETE:
1333 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
1342 * for null-flavored rpc, msg has been unpacked by sptlrpc, although
1343 * redo it wouldn't be harmful.
1345 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
1346 rc = ptlrpc_unpack_req_msg(req, req->rq_reqlen);
1348 CERROR("error unpacking request: ptl %d from %s "
1349 "x"LPU64"\n", svc->srv_req_portal,
1350 libcfs_id2str(req->rq_peer), req->rq_xid);
1355 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
1357 CERROR ("error unpacking ptlrpc body: ptl %d from %s x"
1358 LPU64"\n", svc->srv_req_portal,
1359 libcfs_id2str(req->rq_peer), req->rq_xid);
1363 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_REQ_OPC) &&
1364 lustre_msg_get_opc(req->rq_reqmsg) == obd_fail_val) {
1365 CERROR("drop incoming rpc opc %u, x"LPU64"\n",
1366 obd_fail_val, req->rq_xid);
1371 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
1372 CERROR("wrong packet type received (type=%u) from %s\n",
1373 lustre_msg_get_type(req->rq_reqmsg),
1374 libcfs_id2str(req->rq_peer));
1378 switch(lustre_msg_get_opc(req->rq_reqmsg)) {
1381 req->rq_bulk_write = 1;
1385 req->rq_bulk_read = 1;
1389 CDEBUG(D_NET, "got req "LPU64"\n", req->rq_xid);
1391 req->rq_export = class_conn2export(
1392 lustre_msg_get_handle(req->rq_reqmsg));
1393 if (req->rq_export) {
1394 rc = ptlrpc_check_req(req);
1396 rc = sptlrpc_target_export_check(req->rq_export, req);
1398 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
1399 "illegal security flavor,");
1404 ptlrpc_update_export_timer(req->rq_export, 0);
1407 /* req_in handling should/must be fast */
1408 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
1409 DEBUG_REQ(D_WARNING, req, "Slow req_in handling "CFS_DURATION_T"s",
1410 cfs_time_sub(cfs_time_current_sec(),
1411 req->rq_arrival_time.tv_sec));
1413 /* Set rpc server deadline and add it to the timed list */
1414 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1415 MSGHDR_AT_SUPPORT) ?
1416 /* The max time the client expects us to take */
1417 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1418 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1419 if (unlikely(deadline == 0)) {
1420 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1424 ptlrpc_at_add_timed(req);
1425 rc = ptlrpc_hpreq_init(svc, req);
1429 /* Move it over to the request processing queue */
1430 rc = ptlrpc_server_request_add(svc, req);
1433 cfs_waitq_signal(&svc->srv_waitq);
1437 cfs_spin_lock(&svc->srv_lock);
1438 svc->srv_n_queued_reqs--;
1439 svc->srv_n_active_reqs++;
1440 cfs_spin_unlock(&svc->srv_lock);
1441 ptlrpc_server_finish_request(req);
1447 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
1448 struct ptlrpc_thread *thread)
1450 struct obd_export *export = NULL;
1451 struct ptlrpc_request *request;
1452 struct timeval work_start;
1453 struct timeval work_end;
1461 cfs_spin_lock(&svc->srv_lock);
1462 if (unlikely(!ptlrpc_server_request_pending(svc, 0) ||
1465 /* !@%$# liblustre only has 1 thread */
1466 cfs_atomic_read(&svc->srv_n_difficult_replies) != 0 &&
1468 svc->srv_n_active_reqs >= (svc->srv_threads_running - 1)))) {
1469 /* Don't handle regular requests in the last thread, in order * re
1470 * to handle difficult replies (which might block other threads)
1471 * as well as handle any incoming reqs, early replies, etc.
1472 * That means we always need at least 2 service threads. */
1473 cfs_spin_unlock(&svc->srv_lock);
1477 request = ptlrpc_server_request_get(svc, 0);
1478 if (request == NULL) {
1479 cfs_spin_unlock(&svc->srv_lock);
1483 opc = lustre_msg_get_opc(request->rq_reqmsg);
1484 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT))
1485 fail_opc = OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT;
1486 else if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
1487 fail_opc = OBD_FAIL_PTLRPC_HPREQ_TIMEOUT;
1489 if (unlikely(fail_opc)) {
1490 if (request->rq_export && request->rq_ops) {
1491 cfs_spin_unlock(&svc->srv_lock);
1492 OBD_FAIL_TIMEOUT(fail_opc, 4);
1493 cfs_spin_lock(&svc->srv_lock);
1494 request = ptlrpc_server_request_get(svc, 0);
1495 if (request == NULL) {
1496 cfs_spin_unlock(&svc->srv_lock);
1499 LASSERT(ptlrpc_server_request_pending(svc, 0));
1503 cfs_list_del_init(&request->rq_list);
1504 svc->srv_n_queued_reqs--;
1505 svc->srv_n_active_reqs++;
1509 /* The phase is changed under the lock here because we need to know
1510 * the request is under processing (see ptlrpc_hpreq_reorder()). */
1511 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
1512 cfs_spin_unlock(&svc->srv_lock);
1514 ptlrpc_hpreq_fini(request);
1516 if(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
1517 libcfs_debug_dumplog();
1519 cfs_gettimeofday(&work_start);
1520 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
1521 if (likely(svc->srv_stats != NULL)) {
1522 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
1524 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
1525 svc->srv_n_queued_reqs);
1526 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
1527 svc->srv_n_active_reqs);
1528 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
1529 at_get(&svc->srv_at_estimate));
1532 rc = lu_context_init(&request->rq_session,
1533 LCT_SESSION|LCT_REMEMBER|LCT_NOREF);
1535 CERROR("Failure to initialize session: %d\n", rc);
1538 request->rq_session.lc_thread = thread;
1539 request->rq_session.lc_cookie = 0x5;
1540 lu_context_enter(&request->rq_session);
1542 CDEBUG(D_NET, "got req "LPU64"\n", request->rq_xid);
1544 request->rq_svc_thread = thread;
1546 request->rq_svc_thread->t_env->le_ses = &request->rq_session;
1548 if (likely(request->rq_export)) {
1549 if (unlikely(ptlrpc_check_req(request)))
1551 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
1552 export = class_export_rpc_get(request->rq_export);
1555 /* Discard requests queued for longer than the deadline.
1556 The deadline is increased if we send an early reply. */
1557 if (cfs_time_current_sec() > request->rq_deadline) {
1558 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
1559 ": deadline "CFS_DURATION_T":"CFS_DURATION_T"s ago\n",
1560 libcfs_id2str(request->rq_peer),
1561 cfs_time_sub(request->rq_deadline,
1562 request->rq_arrival_time.tv_sec),
1563 cfs_time_sub(cfs_time_current_sec(),
1564 request->rq_deadline));
1565 goto put_rpc_export;
1568 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
1569 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1570 (request->rq_export ?
1571 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1572 (request->rq_export ?
1573 cfs_atomic_read(&request->rq_export->exp_refcount) : -99),
1574 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1575 libcfs_id2str(request->rq_peer),
1576 lustre_msg_get_opc(request->rq_reqmsg));
1578 if (lustre_msg_get_opc(request->rq_reqmsg) != OBD_PING)
1579 OBD_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, obd_fail_val);
1581 rc = svc->srv_handler(request);
1583 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
1587 class_export_rpc_put(export);
1589 lu_context_exit(&request->rq_session);
1590 lu_context_fini(&request->rq_session);
1592 if (unlikely(cfs_time_current_sec() > request->rq_deadline)) {
1593 DEBUG_REQ(D_WARNING, request, "Request x"LPU64" took longer "
1594 "than estimated ("CFS_DURATION_T":"CFS_DURATION_T"s);"
1595 " client may timeout.",
1596 request->rq_xid, cfs_time_sub(request->rq_deadline,
1597 request->rq_arrival_time.tv_sec),
1598 cfs_time_sub(cfs_time_current_sec(),
1599 request->rq_deadline));
1602 cfs_gettimeofday(&work_end);
1603 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1604 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
1605 "%s:%s+%d:%d:x"LPU64":%s:%d Request procesed in "
1606 "%lds (%lds total) trans "LPU64" rc %d/%d\n",
1608 (request->rq_export ?
1609 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1610 (request->rq_export ?
1611 cfs_atomic_read(&request->rq_export->exp_refcount) : -99),
1612 lustre_msg_get_status(request->rq_reqmsg),
1614 libcfs_id2str(request->rq_peer),
1615 lustre_msg_get_opc(request->rq_reqmsg),
1617 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
1618 (request->rq_repmsg ?
1619 lustre_msg_get_transno(request->rq_repmsg) :
1620 request->rq_transno),
1622 (request->rq_repmsg ?
1623 lustre_msg_get_status(request->rq_repmsg) : -999));
1624 if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
1625 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
1626 int opc = opcode_offset(op);
1627 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
1628 LASSERT(opc < LUSTRE_MAX_OPCODES);
1629 lprocfs_counter_add(svc->srv_stats,
1630 opc + EXTRA_MAX_OPCODES,
1634 if (unlikely(request->rq_early_count)) {
1635 DEBUG_REQ(D_ADAPTTO, request,
1636 "sent %d early replies before finishing in "
1638 request->rq_early_count,
1639 cfs_time_sub(work_end.tv_sec,
1640 request->rq_arrival_time.tv_sec));
1644 cfs_spin_lock(&svc->srv_lock);
1647 cfs_spin_unlock(&svc->srv_lock);
1648 ptlrpc_server_finish_request(request);
1654 * An internal function to process a single reply state object.
1657 ptlrpc_handle_rs (struct ptlrpc_reply_state *rs)
1659 struct ptlrpc_service *svc = rs->rs_service;
1660 struct obd_export *exp;
1661 struct obd_device *obd;
1666 exp = rs->rs_export;
1669 LASSERT (rs->rs_difficult);
1670 LASSERT (rs->rs_scheduled);
1671 LASSERT (cfs_list_empty(&rs->rs_list));
1673 cfs_spin_lock (&exp->exp_lock);
1674 /* Noop if removed already */
1675 cfs_list_del_init (&rs->rs_exp_list);
1676 cfs_spin_unlock (&exp->exp_lock);
1678 /* The disk commit callback holds exp_uncommitted_replies_lock while it
1679 * iterates over newly committed replies, removing them from
1680 * exp_uncommitted_replies. It then drops this lock and schedules the
1681 * replies it found for handling here.
1683 * We can avoid contention for exp_uncommitted_replies_lock between the
1684 * HRT threads and further commit callbacks by checking rs_committed
1685 * which is set in the commit callback while it holds both
1686 * rs_lock and exp_uncommitted_reples.
1688 * If we see rs_committed clear, the commit callback _may_ not have
1689 * handled this reply yet and we race with it to grab
1690 * exp_uncommitted_replies_lock before removing the reply from
1691 * exp_uncommitted_replies. Note that if we lose the race and the
1692 * reply has already been removed, list_del_init() is a noop.
1694 * If we see rs_committed set, we know the commit callback is handling,
1695 * or has handled this reply since store reordering might allow us to
1696 * see rs_committed set out of sequence. But since this is done
1697 * holding rs_lock, we can be sure it has all completed once we hold
1698 * rs_lock, which we do right next.
1700 if (!rs->rs_committed) {
1701 cfs_spin_lock(&exp->exp_uncommitted_replies_lock);
1702 cfs_list_del_init(&rs->rs_obd_list);
1703 cfs_spin_unlock(&exp->exp_uncommitted_replies_lock);
1706 cfs_spin_lock(&rs->rs_lock);
1708 been_handled = rs->rs_handled;
1711 nlocks = rs->rs_nlocks; /* atomic "steal", but */
1712 rs->rs_nlocks = 0; /* locks still on rs_locks! */
1714 if (nlocks == 0 && !been_handled) {
1715 /* If we see this, we should already have seen the warning
1716 * in mds_steal_ack_locks() */
1717 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
1720 rs->rs_xid, rs->rs_transno, rs->rs_opc,
1721 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1724 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1725 cfs_spin_unlock(&rs->rs_lock);
1727 if (!been_handled && rs->rs_on_net) {
1728 LNetMDUnlink(rs->rs_md_h);
1729 /* Ignore return code; we're racing with
1733 while (nlocks-- > 0)
1734 ldlm_lock_decref(&rs->rs_locks[nlocks],
1735 rs->rs_modes[nlocks]);
1737 cfs_spin_lock(&rs->rs_lock);
1740 rs->rs_scheduled = 0;
1742 if (!rs->rs_on_net) {
1744 cfs_spin_unlock(&rs->rs_lock);
1746 class_export_put (exp);
1747 rs->rs_export = NULL;
1748 ptlrpc_rs_decref (rs);
1749 cfs_atomic_dec (&svc->srv_outstanding_replies);
1750 if (cfs_atomic_dec_and_test(&svc->srv_n_difficult_replies) &&
1751 svc->srv_is_stopping)
1752 cfs_waitq_broadcast(&svc->srv_waitq);
1756 /* still on the net; callback will schedule */
1757 cfs_spin_unlock(&rs->rs_lock);
1764 * Check whether given service has a reply available for processing
1767 * \param svc a ptlrpc service
1768 * \retval 0 no replies processed
1769 * \retval 1 one reply processed
1772 ptlrpc_server_handle_reply(struct ptlrpc_service *svc)
1774 struct ptlrpc_reply_state *rs = NULL;
1777 cfs_spin_lock(&svc->srv_lock);
1778 if (!cfs_list_empty(&svc->srv_reply_queue)) {
1779 rs = cfs_list_entry(svc->srv_reply_queue.prev,
1780 struct ptlrpc_reply_state,
1782 cfs_list_del_init(&rs->rs_list);
1784 cfs_spin_unlock(&svc->srv_lock);
1786 ptlrpc_handle_rs(rs);
1790 /* FIXME make use of timeout later */
1792 liblustre_check_services (void *arg)
1794 int did_something = 0;
1796 cfs_list_t *tmp, *nxt;
1799 /* I'm relying on being single threaded, not to have to lock
1800 * ptlrpc_all_services etc */
1801 cfs_list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
1802 struct ptlrpc_service *svc =
1803 cfs_list_entry (tmp, struct ptlrpc_service, srv_list);
1805 if (svc->srv_threads_running != 0) /* I've recursed */
1808 /* service threads can block for bulk, so this limits us
1809 * (arbitrarily) to recursing 1 stack frame per service.
1810 * Note that the problem with recursion is that we have to
1811 * unwind completely before our caller can resume. */
1813 svc->srv_threads_running++;
1816 rc = ptlrpc_server_handle_req_in(svc);
1817 rc |= ptlrpc_server_handle_reply(svc);
1818 rc |= ptlrpc_at_check_timed(svc);
1819 rc |= ptlrpc_server_handle_request(svc, NULL);
1820 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
1821 did_something |= rc;
1824 svc->srv_threads_running--;
1827 RETURN(did_something);
1829 #define ptlrpc_stop_all_threads(s) do {} while (0)
1831 #else /* __KERNEL__ */
1834 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
1836 int avail = svc->srv_nrqbd_receiving;
1837 int low_water = test_req_buffer_pressure ? 0 :
1838 svc->srv_nbuf_per_group/2;
1840 /* NB I'm not locking; just looking. */
1842 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
1843 * allowed the request history to grow out of control. We could put a
1844 * sanity check on that here and cull some history if we need the
1847 if (avail <= low_water)
1848 ptlrpc_grow_req_bufs(svc);
1851 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
1856 ptlrpc_retry_rqbds(void *arg)
1858 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
1860 svc->srv_rqbd_timeout = 0;
1861 return (-ETIMEDOUT);
1864 static int ptlrpc_main(void *arg)
1866 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
1867 struct ptlrpc_service *svc = data->svc;
1868 struct ptlrpc_thread *thread = data->thread;
1869 struct obd_device *dev = data->dev;
1870 struct ptlrpc_reply_state *rs;
1871 #ifdef WITH_GROUP_INFO
1872 cfs_group_info_t *ginfo = NULL;
1875 int counter = 0, rc = 0;
1878 thread->t_pid = cfs_curproc_pid();
1879 cfs_daemonize_ctxt(data->name);
1881 #if defined(HAVE_NODE_TO_CPUMASK) && defined(CONFIG_NUMA)
1882 /* we need to do this before any per-thread allocation is done so that
1883 * we get the per-thread allocations on local node. bug 7342 */
1884 if (svc->srv_cpu_affinity) {
1887 for (cpu = 0, num_cpu = 0; cpu < cfs_num_possible_cpus();
1889 if (!cfs_cpu_online(cpu))
1891 if (num_cpu == thread->t_id % cfs_num_online_cpus())
1895 cfs_set_cpus_allowed(cfs_current(),
1896 node_to_cpumask(cpu_to_node(cpu)));
1900 #ifdef WITH_GROUP_INFO
1901 ginfo = cfs_groups_alloc(0);
1907 cfs_set_current_groups(ginfo);
1908 cfs_put_group_info(ginfo);
1911 if (svc->srv_init != NULL) {
1912 rc = svc->srv_init(thread);
1917 rc = lu_context_init(&env.le_ctx,
1918 svc->srv_ctx_tags|LCT_REMEMBER|LCT_NOREF);
1922 thread->t_env = &env;
1923 env.le_ctx.lc_thread = thread;
1924 env.le_ctx.lc_cookie = 0x6;
1926 /* Alloc reply state structure for this one */
1927 OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
1933 cfs_spin_lock(&svc->srv_lock);
1934 /* SVC_STOPPING may already be set here if someone else is trying
1935 * to stop the service while this new thread has been dynamically
1936 * forked. We still set SVC_RUNNING to let our creator know that
1937 * we are now running, however we will exit as soon as possible */
1938 thread->t_flags |= SVC_RUNNING;
1939 cfs_spin_unlock(&svc->srv_lock);
1942 * wake up our creator. Note: @data is invalid after this point,
1943 * because it's allocated on ptlrpc_start_thread() stack.
1945 cfs_waitq_signal(&thread->t_ctl_waitq);
1947 thread->t_watchdog = lc_watchdog_add(CFS_GET_TIMEOUT(svc), NULL, NULL);
1949 cfs_spin_lock(&svc->srv_lock);
1950 svc->srv_threads_running++;
1951 cfs_list_add(&rs->rs_list, &svc->srv_free_rs_list);
1952 cfs_spin_unlock(&svc->srv_lock);
1953 cfs_waitq_signal(&svc->srv_free_rs_waitq);
1955 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
1956 svc->srv_threads_running);
1958 /* XXX maintain a list of all managed devices: insert here */
1960 while (!(thread->t_flags & SVC_STOPPING) && !svc->srv_is_stopping) {
1961 /* Don't exit while there are replies to be handled */
1962 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
1963 ptlrpc_retry_rqbds, svc);
1965 lc_watchdog_disable(thread->t_watchdog);
1969 l_wait_event_exclusive (svc->srv_waitq,
1970 thread->t_flags & SVC_STOPPING ||
1971 svc->srv_is_stopping ||
1972 (!cfs_list_empty(&svc->srv_idle_rqbds) &&
1973 svc->srv_rqbd_timeout == 0) ||
1974 !cfs_list_empty(&svc->srv_req_in_queue) ||
1975 (ptlrpc_server_request_pending(svc, 0) &&
1976 (svc->srv_n_active_reqs <
1977 (svc->srv_threads_running - 1))) ||
1981 if (thread->t_flags & SVC_STOPPING || svc->srv_is_stopping)
1984 lc_watchdog_touch(thread->t_watchdog, CFS_GET_TIMEOUT(svc));
1986 ptlrpc_check_rqbd_pool(svc);
1988 if (svc->srv_threads_started < svc->srv_threads_max &&
1989 svc->srv_n_active_reqs >= (svc->srv_threads_started - 1))
1990 /* Ignore return code - we tried... */
1991 ptlrpc_start_thread(dev, svc);
1993 if (!cfs_list_empty(&svc->srv_req_in_queue)) {
1994 /* Process all incoming reqs before handling any */
1995 ptlrpc_server_handle_req_in(svc);
1996 /* but limit ourselves in case of flood */
1997 if (counter++ < 1000)
2002 if (svc->srv_at_check)
2003 ptlrpc_at_check_timed(svc);
2005 /* don't handle requests in the last thread */
2006 if (ptlrpc_server_request_pending(svc, 0) &&
2007 (svc->srv_n_active_reqs < (svc->srv_threads_running - 1))) {
2008 lu_context_enter(&env.le_ctx);
2009 ptlrpc_server_handle_request(svc, thread);
2010 lu_context_exit(&env.le_ctx);
2013 if (!cfs_list_empty(&svc->srv_idle_rqbds) &&
2014 ptlrpc_server_post_idle_rqbds(svc) < 0) {
2015 /* I just failed to repost request buffers. Wait
2016 * for a timeout (unless something else happens)
2017 * before I try again */
2018 svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
2019 CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
2020 svc->srv_nrqbd_receiving);
2024 lc_watchdog_delete(thread->t_watchdog);
2025 thread->t_watchdog = NULL;
2029 * deconstruct service specific state created by ptlrpc_start_thread()
2031 if (svc->srv_done != NULL)
2032 svc->srv_done(thread);
2034 lu_context_fini(&env.le_ctx);
2036 CDEBUG(D_RPCTRACE, "service thread [ %p : %u ] %d exiting: rc %d\n",
2037 thread, thread->t_pid, thread->t_id, rc);
2039 cfs_spin_lock(&svc->srv_lock);
2040 svc->srv_threads_running--; /* must know immediately */
2042 thread->t_flags = SVC_STOPPED;
2044 cfs_waitq_signal(&thread->t_ctl_waitq);
2045 cfs_spin_unlock(&svc->srv_lock);
2050 struct ptlrpc_hr_args {
2053 struct ptlrpc_hr_service *hrs;
2056 static int hrt_dont_sleep(struct ptlrpc_hr_thread *t,
2057 cfs_list_t *replies)
2061 cfs_spin_lock(&t->hrt_lock);
2062 cfs_list_splice_init(&t->hrt_queue, replies);
2063 result = cfs_test_bit(HRT_STOPPING, &t->hrt_flags) ||
2064 !cfs_list_empty(replies);
2065 cfs_spin_unlock(&t->hrt_lock);
2069 static int ptlrpc_hr_main(void *arg)
2071 struct ptlrpc_hr_args * hr_args = arg;
2072 struct ptlrpc_hr_service *hr = hr_args->hrs;
2073 struct ptlrpc_hr_thread *t = &hr->hr_threads[hr_args->thread_index];
2074 char threadname[20];
2075 CFS_LIST_HEAD(replies);
2077 snprintf(threadname, sizeof(threadname),
2078 "ptlrpc_hr_%d", hr_args->thread_index);
2080 cfs_daemonize_ctxt(threadname);
2081 #if defined(CONFIG_SMP) && defined(HAVE_NODE_TO_CPUMASK)
2082 cfs_set_cpus_allowed(cfs_current(),
2083 node_to_cpumask(cpu_to_node(hr_args->cpu_index)));
2085 cfs_set_bit(HRT_RUNNING, &t->hrt_flags);
2086 cfs_waitq_signal(&t->hrt_wait);
2088 while (!cfs_test_bit(HRT_STOPPING, &t->hrt_flags)) {
2090 l_cfs_wait_event(t->hrt_wait, hrt_dont_sleep(t, &replies));
2091 while (!cfs_list_empty(&replies)) {
2092 struct ptlrpc_reply_state *rs;
2094 rs = cfs_list_entry(replies.prev,
2095 struct ptlrpc_reply_state,
2097 cfs_list_del_init(&rs->rs_list);
2098 ptlrpc_handle_rs(rs);
2102 cfs_clear_bit(HRT_RUNNING, &t->hrt_flags);
2103 cfs_complete(&t->hrt_completion);
2108 static int ptlrpc_start_hr_thread(struct ptlrpc_hr_service *hr, int n, int cpu)
2110 struct ptlrpc_hr_thread *t = &hr->hr_threads[n];
2111 struct ptlrpc_hr_args args;
2115 args.thread_index = n;
2116 args.cpu_index = cpu;
2119 rc = cfs_kernel_thread(ptlrpc_hr_main, (void*)&args,
2120 CLONE_VM|CLONE_FILES);
2122 cfs_complete(&t->hrt_completion);
2125 l_cfs_wait_event(t->hrt_wait, cfs_test_bit(HRT_RUNNING, &t->hrt_flags));
2131 static void ptlrpc_stop_hr_thread(struct ptlrpc_hr_thread *t)
2135 cfs_set_bit(HRT_STOPPING, &t->hrt_flags);
2136 cfs_waitq_signal(&t->hrt_wait);
2137 cfs_wait_for_completion(&t->hrt_completion);
2142 static void ptlrpc_stop_hr_threads(struct ptlrpc_hr_service *hrs)
2147 for (n = 0; n < hrs->hr_n_threads; n++)
2148 ptlrpc_stop_hr_thread(&hrs->hr_threads[n]);
2153 static int ptlrpc_start_hr_threads(struct ptlrpc_hr_service *hr)
2156 int n, cpu, threads_started = 0;
2159 LASSERT(hr != NULL);
2160 LASSERT(hr->hr_n_threads > 0);
2162 for (n = 0, cpu = 0; n < hr->hr_n_threads; n++) {
2163 #if defined(CONFIG_SMP) && defined(HAVE_NODE_TO_CPUMASK)
2164 while(!cfs_cpu_online(cpu)) {
2166 if (cpu >= cfs_num_possible_cpus())
2170 rc = ptlrpc_start_hr_thread(hr, n, cpu);
2176 if (threads_started == 0) {
2177 CERROR("No reply handling threads started\n");
2180 if (threads_started < hr->hr_n_threads) {
2181 CWARN("Started only %d reply handling threads from %d\n",
2182 threads_started, hr->hr_n_threads);
2183 hr->hr_n_threads = threads_started;
2188 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
2189 struct ptlrpc_thread *thread)
2191 struct l_wait_info lwi = { 0 };
2194 CDEBUG(D_RPCTRACE, "Stopping thread [ %p : %u ]\n",
2195 thread, thread->t_pid);
2197 cfs_spin_lock(&svc->srv_lock);
2198 /* let the thread know that we would like it to stop asap */
2199 thread->t_flags |= SVC_STOPPING;
2200 cfs_spin_unlock(&svc->srv_lock);
2202 cfs_waitq_broadcast(&svc->srv_waitq);
2203 l_wait_event(thread->t_ctl_waitq,
2204 (thread->t_flags & SVC_STOPPED), &lwi);
2206 cfs_spin_lock(&svc->srv_lock);
2207 cfs_list_del(&thread->t_link);
2208 cfs_spin_unlock(&svc->srv_lock);
2210 OBD_FREE_PTR(thread);
2214 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
2216 struct ptlrpc_thread *thread;
2219 cfs_spin_lock(&svc->srv_lock);
2220 while (!cfs_list_empty(&svc->srv_threads)) {
2221 thread = cfs_list_entry(svc->srv_threads.next,
2222 struct ptlrpc_thread, t_link);
2224 cfs_spin_unlock(&svc->srv_lock);
2225 ptlrpc_stop_thread(svc, thread);
2226 cfs_spin_lock(&svc->srv_lock);
2229 cfs_spin_unlock(&svc->srv_lock);
2233 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc)
2238 /* We require 2 threads min - see note in
2239 ptlrpc_server_handle_request */
2240 LASSERT(svc->srv_threads_min >= 2);
2241 for (i = 0; i < svc->srv_threads_min; i++) {
2242 rc = ptlrpc_start_thread(dev, svc);
2243 /* We have enough threads, don't start more. b=15759 */
2247 CERROR("cannot start %s thread #%d: rc %d\n",
2248 svc->srv_thread_name, i, rc);
2249 ptlrpc_stop_all_threads(svc);
2256 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc)
2258 struct l_wait_info lwi = { 0 };
2259 struct ptlrpc_svc_data d;
2260 struct ptlrpc_thread *thread;
2265 CDEBUG(D_RPCTRACE, "%s started %d min %d max %d running %d\n",
2266 svc->srv_name, svc->srv_threads_started, svc->srv_threads_min,
2267 svc->srv_threads_max, svc->srv_threads_running);
2269 if (unlikely(svc->srv_is_stopping))
2272 if (unlikely(svc->srv_threads_started >= svc->srv_threads_max) ||
2273 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
2274 svc->srv_threads_started == svc->srv_threads_min - 1))
2277 OBD_ALLOC_PTR(thread);
2280 cfs_waitq_init(&thread->t_ctl_waitq);
2282 cfs_spin_lock(&svc->srv_lock);
2283 if (svc->srv_threads_started >= svc->srv_threads_max) {
2284 cfs_spin_unlock(&svc->srv_lock);
2285 OBD_FREE_PTR(thread);
2288 cfs_list_add(&thread->t_link, &svc->srv_threads);
2289 id = svc->srv_threads_started++;
2290 cfs_spin_unlock(&svc->srv_lock);
2292 thread->t_svc = svc;
2294 sprintf(name, "%s_%02d", svc->srv_thread_name, id);
2300 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
2302 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
2303 * just drop the VM and FILES in cfs_daemonize_ctxt() right away.
2305 rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
2307 CERROR("cannot start thread '%s': rc %d\n", name, rc);
2309 cfs_spin_lock(&svc->srv_lock);
2310 cfs_list_del(&thread->t_link);
2311 --svc->srv_threads_started;
2312 cfs_spin_unlock(&svc->srv_lock);
2314 OBD_FREE(thread, sizeof(*thread));
2317 l_wait_event(thread->t_ctl_waitq,
2318 thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
2320 rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
2325 int ptlrpc_hr_init(void)
2328 int n_cpus = cfs_num_online_cpus();
2329 struct ptlrpc_hr_service *hr;
2334 LASSERT(ptlrpc_hr == NULL);
2336 size = offsetof(struct ptlrpc_hr_service, hr_threads[n_cpus]);
2337 OBD_ALLOC(hr, size);
2340 for (i = 0; i < n_cpus; i++) {
2341 struct ptlrpc_hr_thread *t = &hr->hr_threads[i];
2343 cfs_spin_lock_init(&t->hrt_lock);
2344 cfs_waitq_init(&t->hrt_wait);
2345 CFS_INIT_LIST_HEAD(&t->hrt_queue);
2346 cfs_init_completion(&t->hrt_completion);
2348 hr->hr_n_threads = n_cpus;
2352 rc = ptlrpc_start_hr_threads(hr);
2354 OBD_FREE(hr, hr->hr_size);
2360 void ptlrpc_hr_fini(void)
2362 if (ptlrpc_hr != NULL) {
2363 ptlrpc_stop_hr_threads(ptlrpc_hr);
2364 OBD_FREE(ptlrpc_hr, ptlrpc_hr->hr_size);
2369 #endif /* __KERNEL__ */
2372 * Wait until all already scheduled replies are processed.
2374 static void ptlrpc_wait_replies(struct ptlrpc_service *svc)
2378 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10),
2380 rc = l_wait_event(svc->srv_waitq, cfs_atomic_read(&svc-> \
2381 srv_n_difficult_replies) == 0,
2385 CWARN("Unexpectedly long timeout %p\n", svc);
2389 int ptlrpc_unregister_service(struct ptlrpc_service *service)
2392 struct l_wait_info lwi;
2394 struct ptlrpc_reply_state *rs, *t;
2395 struct ptlrpc_at_array *array = &service->srv_at_array;
2398 service->srv_is_stopping = 1;
2399 cfs_timer_disarm(&service->srv_at_timer);
2401 ptlrpc_stop_all_threads(service);
2402 LASSERT(cfs_list_empty(&service->srv_threads));
2404 cfs_spin_lock (&ptlrpc_all_services_lock);
2405 cfs_list_del_init (&service->srv_list);
2406 cfs_spin_unlock (&ptlrpc_all_services_lock);
2408 ptlrpc_lprocfs_unregister_service(service);
2410 /* All history will be culled when the next request buffer is
2412 service->srv_max_history_rqbds = 0;
2414 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
2416 rc = LNetClearLazyPortal(service->srv_req_portal);
2419 /* Unlink all the request buffers. This forces a 'final' event with
2420 * its 'unlink' flag set for each posted rqbd */
2421 cfs_list_for_each(tmp, &service->srv_active_rqbds) {
2422 struct ptlrpc_request_buffer_desc *rqbd =
2423 cfs_list_entry(tmp, struct ptlrpc_request_buffer_desc,
2426 rc = LNetMDUnlink(rqbd->rqbd_md_h);
2427 LASSERT (rc == 0 || rc == -ENOENT);
2430 /* Wait for the network to release any buffers it's currently
2433 cfs_spin_lock(&service->srv_lock);
2434 rc = service->srv_nrqbd_receiving;
2435 cfs_spin_unlock(&service->srv_lock);
2440 /* Network access will complete in finite time but the HUGE
2441 * timeout lets us CWARN for visibility of sluggish NALs */
2442 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
2443 cfs_time_seconds(1), NULL, NULL);
2444 rc = l_wait_event(service->srv_waitq,
2445 service->srv_nrqbd_receiving == 0,
2447 if (rc == -ETIMEDOUT)
2448 CWARN("Service %s waiting for request buffers\n",
2452 /* schedule all outstanding replies to terminate them */
2453 cfs_spin_lock(&service->srv_lock);
2454 while (!cfs_list_empty(&service->srv_active_replies)) {
2455 struct ptlrpc_reply_state *rs =
2456 cfs_list_entry(service->srv_active_replies.next,
2457 struct ptlrpc_reply_state, rs_list);
2458 cfs_spin_lock(&rs->rs_lock);
2459 ptlrpc_schedule_difficult_reply(rs);
2460 cfs_spin_unlock(&rs->rs_lock);
2462 cfs_spin_unlock(&service->srv_lock);
2464 /* purge the request queue. NB No new replies (rqbds all unlinked)
2465 * and no service threads, so I'm the only thread noodling the
2466 * request queue now */
2467 while (!cfs_list_empty(&service->srv_req_in_queue)) {
2468 struct ptlrpc_request *req =
2469 cfs_list_entry(service->srv_req_in_queue.next,
2470 struct ptlrpc_request,
2473 cfs_list_del(&req->rq_list);
2474 service->srv_n_queued_reqs--;
2475 service->srv_n_active_reqs++;
2476 ptlrpc_server_finish_request(req);
2478 while (ptlrpc_server_request_pending(service, 1)) {
2479 struct ptlrpc_request *req;
2481 req = ptlrpc_server_request_get(service, 1);
2482 cfs_list_del(&req->rq_list);
2483 service->srv_n_queued_reqs--;
2484 service->srv_n_active_reqs++;
2485 ptlrpc_hpreq_fini(req);
2486 ptlrpc_server_finish_request(req);
2488 LASSERT(service->srv_n_queued_reqs == 0);
2489 LASSERT(service->srv_n_active_reqs == 0);
2490 LASSERT(service->srv_n_history_rqbds == 0);
2491 LASSERT(cfs_list_empty(&service->srv_active_rqbds));
2493 /* Now free all the request buffers since nothing references them
2495 while (!cfs_list_empty(&service->srv_idle_rqbds)) {
2496 struct ptlrpc_request_buffer_desc *rqbd =
2497 cfs_list_entry(service->srv_idle_rqbds.next,
2498 struct ptlrpc_request_buffer_desc,
2501 ptlrpc_free_rqbd(rqbd);
2504 ptlrpc_wait_replies(service);
2506 cfs_list_for_each_entry_safe(rs, t, &service->srv_free_rs_list,
2508 cfs_list_del(&rs->rs_list);
2509 OBD_FREE(rs, service->srv_max_reply_size);
2512 /* In case somebody rearmed this in the meantime */
2513 cfs_timer_disarm(&service->srv_at_timer);
2515 if (array->paa_reqs_array != NULL) {
2516 OBD_FREE(array->paa_reqs_array,
2517 sizeof(cfs_list_t) * array->paa_size);
2518 array->paa_reqs_array = NULL;
2521 if (array->paa_reqs_count != NULL) {
2522 OBD_FREE(array->paa_reqs_count,
2523 sizeof(__u32) * array->paa_size);
2524 array->paa_reqs_count= NULL;
2527 OBD_FREE_PTR(service);
2531 /* Returns 0 if the service is healthy.
2533 * Right now, it just checks to make sure that requests aren't languishing
2534 * in the queue. We'll use this health check to govern whether a node needs
2535 * to be shot, so it's intentionally non-aggressive. */
2536 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
2538 struct ptlrpc_request *request;
2539 struct timeval right_now;
2545 cfs_gettimeofday(&right_now);
2547 cfs_spin_lock(&svc->srv_lock);
2548 if (!ptlrpc_server_request_pending(svc, 1)) {
2549 cfs_spin_unlock(&svc->srv_lock);
2553 /* How long has the next entry been waiting? */
2554 if (cfs_list_empty(&svc->srv_request_queue))
2555 request = cfs_list_entry(svc->srv_request_hpq.next,
2556 struct ptlrpc_request, rq_list);
2558 request = cfs_list_entry(svc->srv_request_queue.next,
2559 struct ptlrpc_request, rq_list);
2560 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
2561 cfs_spin_unlock(&svc->srv_lock);
2563 if ((timediff / ONE_MILLION) > (AT_OFF ? obd_timeout * 3/2 :
2565 CERROR("%s: unhealthy - request has been waiting %lds\n",
2566 svc->srv_name, timediff / ONE_MILLION);