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 (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
39 #define DEBUG_SUBSYSTEM S_RPC
41 #include <liblustre.h>
43 #include <obd_support.h>
44 #include <obd_class.h>
45 #include <lustre_net.h>
46 #include <lu_object.h>
47 #include <lnet/types.h>
48 #include "ptlrpc_internal.h"
50 /* The following are visible and mutable through /sys/module/ptlrpc */
51 int test_req_buffer_pressure = 0;
52 CFS_MODULE_PARM(test_req_buffer_pressure, "i", int, 0444,
53 "set non-zero to put pressure on request buffer pools");
54 CFS_MODULE_PARM(at_min, "i", int, 0644,
55 "Adaptive timeout minimum (sec)");
56 CFS_MODULE_PARM(at_max, "i", int, 0644,
57 "Adaptive timeout maximum (sec)");
58 CFS_MODULE_PARM(at_history, "i", int, 0644,
59 "Adaptive timeouts remember the slowest event that took place "
60 "within this period (sec)");
61 CFS_MODULE_PARM(at_early_margin, "i", int, 0644,
62 "How soon before an RPC deadline to send an early reply");
63 CFS_MODULE_PARM(at_extra, "i", int, 0644,
64 "How much extra time to give with each early reply");
68 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
69 static void ptlrpc_hpreq_fini(struct ptlrpc_request *req);
71 static CFS_LIST_HEAD(ptlrpc_all_services);
72 cfs_spinlock_t ptlrpc_all_services_lock;
74 struct ptlrpc_request_buffer_desc *
75 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
77 struct ptlrpc_request_buffer_desc *rqbd;
83 rqbd->rqbd_service = svc;
84 rqbd->rqbd_refcount = 0;
85 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
86 rqbd->rqbd_cbid.cbid_arg = rqbd;
87 CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
88 OBD_ALLOC_LARGE(rqbd->rqbd_buffer, svc->srv_buf_size);
90 if (rqbd->rqbd_buffer == NULL) {
95 cfs_spin_lock(&svc->srv_lock);
96 cfs_list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
98 cfs_spin_unlock(&svc->srv_lock);
104 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
106 struct ptlrpc_service *svc = rqbd->rqbd_service;
108 LASSERT (rqbd->rqbd_refcount == 0);
109 LASSERT (cfs_list_empty(&rqbd->rqbd_reqs));
111 cfs_spin_lock(&svc->srv_lock);
112 cfs_list_del(&rqbd->rqbd_list);
114 cfs_spin_unlock(&svc->srv_lock);
116 OBD_FREE_LARGE(rqbd->rqbd_buffer, svc->srv_buf_size);
121 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
123 struct ptlrpc_request_buffer_desc *rqbd;
126 CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
127 svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
128 svc->srv_nrqbd_receiving, svc->srv_nbufs);
129 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
130 rqbd = ptlrpc_alloc_rqbd(svc);
133 CERROR ("%s: Can't allocate request buffer\n",
138 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
146 * Part of Rep-Ack logic.
147 * Puts a lock and its mode into reply state assotiated to request reply.
150 ptlrpc_save_lock(struct ptlrpc_request *req,
151 struct lustre_handle *lock, int mode, int no_ack)
153 struct ptlrpc_reply_state *rs = req->rq_reply_state;
157 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
159 if (req->rq_export->exp_disconnected) {
160 ldlm_lock_decref(lock, mode);
162 idx = rs->rs_nlocks++;
163 rs->rs_locks[idx] = *lock;
164 rs->rs_modes[idx] = mode;
165 rs->rs_difficult = 1;
166 rs->rs_no_ack = !!no_ack;
172 #define HRT_RUNNING 0
173 #define HRT_STOPPING 1
175 struct ptlrpc_hr_thread {
176 cfs_spinlock_t hrt_lock;
177 unsigned long hrt_flags;
178 cfs_waitq_t hrt_wait;
179 cfs_list_t hrt_queue;
180 cfs_completion_t hrt_completion;
183 struct ptlrpc_hr_service {
187 struct ptlrpc_hr_thread hr_threads[0];
191 cfs_list_t rsb_replies;
192 struct ptlrpc_service *rsb_svc;
193 unsigned int rsb_n_replies;
197 * A pointer to per-node reply handling service.
199 static struct ptlrpc_hr_service *ptlrpc_hr = NULL;
202 * maximum mumber of replies scheduled in one batch
204 #define MAX_SCHEDULED 256
207 * Initialize a reply batch.
211 static void rs_batch_init(struct rs_batch *b)
213 memset(b, 0, sizeof *b);
214 CFS_INIT_LIST_HEAD(&b->rsb_replies);
218 * Choose an hr thread to dispatch requests to.
220 static unsigned int get_hr_thread_index(struct ptlrpc_hr_service *hr)
224 /* Concurrent modification of hr_index w/o any spinlock
225 protection is harmless as long as the result fits
226 [0..(hr_n_threads-1)] range and each thread gets near equal
229 hr->hr_index = (idx >= hr->hr_n_threads - 1) ? 0 : idx + 1;
234 * Dispatch all replies accumulated in the batch to one from
235 * dedicated reply handling threads.
239 static void rs_batch_dispatch(struct rs_batch *b)
241 if (b->rsb_n_replies != 0) {
242 struct ptlrpc_hr_service *hr = ptlrpc_hr;
245 idx = get_hr_thread_index(hr);
247 cfs_spin_lock(&hr->hr_threads[idx].hrt_lock);
248 cfs_list_splice_init(&b->rsb_replies,
249 &hr->hr_threads[idx].hrt_queue);
250 cfs_spin_unlock(&hr->hr_threads[idx].hrt_lock);
251 cfs_waitq_signal(&hr->hr_threads[idx].hrt_wait);
252 b->rsb_n_replies = 0;
257 * Add a reply to a batch.
258 * Add one reply object to a batch, schedule batched replies if overload.
263 static void rs_batch_add(struct rs_batch *b, struct ptlrpc_reply_state *rs)
265 struct ptlrpc_service *svc = rs->rs_service;
267 if (svc != b->rsb_svc || b->rsb_n_replies >= MAX_SCHEDULED) {
268 if (b->rsb_svc != NULL) {
269 rs_batch_dispatch(b);
270 cfs_spin_unlock(&b->rsb_svc->srv_rs_lock);
272 cfs_spin_lock(&svc->srv_rs_lock);
275 cfs_spin_lock(&rs->rs_lock);
276 rs->rs_scheduled_ever = 1;
277 if (rs->rs_scheduled == 0) {
278 cfs_list_move(&rs->rs_list, &b->rsb_replies);
279 rs->rs_scheduled = 1;
282 rs->rs_committed = 1;
283 cfs_spin_unlock(&rs->rs_lock);
287 * Reply batch finalization.
288 * Dispatch remaining replies from the batch
289 * and release remaining spinlock.
293 static void rs_batch_fini(struct rs_batch *b)
295 if (b->rsb_svc != 0) {
296 rs_batch_dispatch(b);
297 cfs_spin_unlock(&b->rsb_svc->srv_rs_lock);
301 #define DECLARE_RS_BATCH(b) struct rs_batch b
303 #else /* __KERNEL__ */
305 #define rs_batch_init(b) do{}while(0)
306 #define rs_batch_fini(b) do{}while(0)
307 #define rs_batch_add(b, r) ptlrpc_schedule_difficult_reply(r)
308 #define DECLARE_RS_BATCH(b)
310 #endif /* __KERNEL__ */
313 * Put reply state into a queue for processing because we received
314 * ACK from the client
316 void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs)
319 struct ptlrpc_hr_service *hr = ptlrpc_hr;
323 LASSERT(cfs_list_empty(&rs->rs_list));
325 idx = get_hr_thread_index(hr);
326 cfs_spin_lock(&hr->hr_threads[idx].hrt_lock);
327 cfs_list_add_tail(&rs->rs_list, &hr->hr_threads[idx].hrt_queue);
328 cfs_spin_unlock(&hr->hr_threads[idx].hrt_lock);
329 cfs_waitq_signal(&hr->hr_threads[idx].hrt_wait);
332 cfs_list_add_tail(&rs->rs_list, &rs->rs_service->srv_reply_queue);
337 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
341 LASSERT_SPIN_LOCKED(&rs->rs_service->srv_rs_lock);
342 LASSERT_SPIN_LOCKED(&rs->rs_lock);
343 LASSERT (rs->rs_difficult);
344 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
346 if (rs->rs_scheduled) { /* being set up or already notified */
351 rs->rs_scheduled = 1;
352 cfs_list_del_init(&rs->rs_list);
353 ptlrpc_dispatch_difficult_reply(rs);
357 void ptlrpc_commit_replies(struct obd_export *exp)
359 struct ptlrpc_reply_state *rs, *nxt;
360 DECLARE_RS_BATCH(batch);
363 rs_batch_init(&batch);
364 /* Find any replies that have been committed and get their service
365 * to attend to complete them. */
367 /* CAVEAT EMPTOR: spinlock ordering!!! */
368 cfs_spin_lock(&exp->exp_uncommitted_replies_lock);
369 cfs_list_for_each_entry_safe(rs, nxt, &exp->exp_uncommitted_replies,
371 LASSERT (rs->rs_difficult);
372 /* VBR: per-export last_committed */
373 LASSERT(rs->rs_export);
374 if (rs->rs_transno <= exp->exp_last_committed) {
375 cfs_list_del_init(&rs->rs_obd_list);
376 rs_batch_add(&batch, rs);
379 cfs_spin_unlock(&exp->exp_uncommitted_replies_lock);
380 rs_batch_fini(&batch);
385 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
387 struct ptlrpc_request_buffer_desc *rqbd;
392 cfs_spin_lock(&svc->srv_lock);
394 if (cfs_list_empty (&svc->srv_idle_rqbds)) {
395 cfs_spin_unlock(&svc->srv_lock);
399 rqbd = cfs_list_entry(svc->srv_idle_rqbds.next,
400 struct ptlrpc_request_buffer_desc,
402 cfs_list_del (&rqbd->rqbd_list);
404 /* assume we will post successfully */
405 svc->srv_nrqbd_receiving++;
406 cfs_list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
408 cfs_spin_unlock(&svc->srv_lock);
410 rc = ptlrpc_register_rqbd(rqbd);
417 cfs_spin_lock(&svc->srv_lock);
419 svc->srv_nrqbd_receiving--;
420 cfs_list_del(&rqbd->rqbd_list);
421 cfs_list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
423 /* Don't complain if no request buffers are posted right now; LNET
424 * won't drop requests because we set the portal lazy! */
426 cfs_spin_unlock(&svc->srv_lock);
432 * Start a service with parameters from struct ptlrpc_service_conf \a c
433 * as opposed to directly calling ptlrpc_init_svc with tons of arguments.
435 struct ptlrpc_service *ptlrpc_init_svc_conf(struct ptlrpc_service_conf *c,
436 svc_handler_t h, char *name,
437 struct proc_dir_entry *proc_entry,
438 svc_req_printfn_t prntfn,
441 return ptlrpc_init_svc(c->psc_nbufs, c->psc_bufsize,
442 c->psc_max_req_size, c->psc_max_reply_size,
443 c->psc_req_portal, c->psc_rep_portal,
444 c->psc_watchdog_factor,
446 prntfn, c->psc_min_threads, c->psc_max_threads,
447 threadname, c->psc_ctx_tags, NULL);
449 EXPORT_SYMBOL(ptlrpc_init_svc_conf);
451 static void ptlrpc_at_timer(unsigned long castmeharder)
453 struct ptlrpc_service *svc = (struct ptlrpc_service *)castmeharder;
454 svc->srv_at_check = 1;
455 svc->srv_at_checktime = cfs_time_current();
456 cfs_waitq_signal(&svc->srv_waitq);
460 * Initialize service on a given portal.
461 * This includes starting serving threads , allocating and posting rqbds and
463 * \a nbufs is how many buffers to post
464 * \a bufsize is buffer size to post
465 * \a max_req_size - maximum request size to be accepted for this service
466 * \a max_reply_size maximum reply size this service can ever send
467 * \a req_portal - portal to listed for requests on
468 * \a rep_portal - portal of where to send replies to
469 * \a watchdog_factor soft watchdog timeout multiplifier to print stuck service traces.
470 * \a handler - function to process every new request
471 * \a name - service name
472 * \a proc_entry - entry in the /proc tree for sttistics reporting
473 * \a min_threads \a max_threads - min/max number of service threads to start.
474 * \a threadname should be 11 characters or less - 3 will be added on
475 * \a hp_handler - function to determine priority of the request, also called
476 * on every new request.
478 struct ptlrpc_service *
479 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
480 int req_portal, int rep_portal, int watchdog_factor,
481 svc_handler_t handler, char *name,
482 cfs_proc_dir_entry_t *proc_entry,
483 svc_req_printfn_t svcreq_printfn,
484 int min_threads, int max_threads,
485 char *threadname, __u32 ctx_tags,
486 svc_hpreq_handler_t hp_handler)
489 struct ptlrpc_at_array *array;
490 struct ptlrpc_service *service;
491 unsigned int size, index;
495 LASSERT (bufsize >= max_req_size + SPTLRPC_MAX_PAYLOAD);
496 LASSERT (ctx_tags != 0);
498 OBD_ALLOC_PTR(service);
502 /* First initialise enough for early teardown */
504 service->srv_name = name;
505 cfs_spin_lock_init(&service->srv_lock);
506 cfs_spin_lock_init(&service->srv_rq_lock);
507 cfs_spin_lock_init(&service->srv_rs_lock);
508 CFS_INIT_LIST_HEAD(&service->srv_threads);
509 cfs_waitq_init(&service->srv_waitq);
511 service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
512 service->srv_max_req_size = max_req_size + SPTLRPC_MAX_PAYLOAD;
513 service->srv_buf_size = bufsize;
514 service->srv_rep_portal = rep_portal;
515 service->srv_req_portal = req_portal;
516 service->srv_watchdog_factor = watchdog_factor;
517 service->srv_handler = handler;
518 service->srv_req_printfn = svcreq_printfn;
519 service->srv_request_seq = 1; /* valid seq #s start at 1 */
520 service->srv_request_max_cull_seq = 0;
521 service->srv_threads_min = min_threads;
522 service->srv_threads_max = max_threads;
523 service->srv_thread_name = threadname;
524 service->srv_ctx_tags = ctx_tags;
525 service->srv_hpreq_handler = hp_handler;
526 service->srv_hpreq_ratio = PTLRPC_SVC_HP_RATIO;
527 service->srv_hpreq_count = 0;
528 service->srv_n_active_hpreq = 0;
530 rc = LNetSetLazyPortal(service->srv_req_portal);
533 CFS_INIT_LIST_HEAD(&service->srv_request_queue);
534 CFS_INIT_LIST_HEAD(&service->srv_request_hpq);
535 CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
536 CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
537 CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
538 CFS_INIT_LIST_HEAD(&service->srv_request_history);
539 CFS_INIT_LIST_HEAD(&service->srv_active_replies);
541 CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
543 CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
544 cfs_waitq_init(&service->srv_free_rs_waitq);
545 cfs_atomic_set(&service->srv_n_difficult_replies, 0);
547 cfs_spin_lock_init(&service->srv_at_lock);
548 CFS_INIT_LIST_HEAD(&service->srv_req_in_queue);
550 array = &service->srv_at_array;
551 size = at_est2timeout(at_max);
552 array->paa_size = size;
553 array->paa_count = 0;
554 array->paa_deadline = -1;
556 /* allocate memory for srv_at_array (ptlrpc_at_array) */
557 OBD_ALLOC(array->paa_reqs_array, sizeof(cfs_list_t) * size);
558 if (array->paa_reqs_array == NULL)
561 for (index = 0; index < size; index++)
562 CFS_INIT_LIST_HEAD(&array->paa_reqs_array[index]);
564 OBD_ALLOC(array->paa_reqs_count, sizeof(__u32) * size);
565 if (array->paa_reqs_count == NULL)
568 cfs_timer_init(&service->srv_at_timer, ptlrpc_at_timer, service);
569 /* At SOW, service time should be quick; 10s seems generous. If client
570 timeout is less than this, we'll be sending an early reply. */
571 at_init(&service->srv_at_estimate, 10, 0);
573 cfs_spin_lock (&ptlrpc_all_services_lock);
574 cfs_list_add (&service->srv_list, &ptlrpc_all_services);
575 cfs_spin_unlock (&ptlrpc_all_services_lock);
577 /* Now allocate the request buffers */
578 rc = ptlrpc_grow_req_bufs(service);
579 /* We shouldn't be under memory pressure at startup, so
580 * fail if we can't post all our buffers at this time. */
584 /* Now allocate pool of reply buffers */
585 /* Increase max reply size to next power of two */
586 service->srv_max_reply_size = 1;
587 while (service->srv_max_reply_size <
588 max_reply_size + SPTLRPC_MAX_PAYLOAD)
589 service->srv_max_reply_size <<= 1;
591 if (proc_entry != NULL)
592 ptlrpc_lprocfs_register_service(proc_entry, service);
594 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
595 service->srv_name, service->srv_req_portal);
599 ptlrpc_unregister_service(service);
604 * to actually free the request, must be called without holding svc_lock.
605 * note it's caller's responsibility to unlink req->rq_list.
607 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
609 LASSERT(cfs_atomic_read(&req->rq_refcount) == 0);
610 LASSERT(cfs_list_empty(&req->rq_timed_list));
612 /* DEBUG_REQ() assumes the reply state of a request with a valid
613 * ref will not be destroyed until that reference is dropped. */
614 ptlrpc_req_drop_rs(req);
616 sptlrpc_svc_ctx_decref(req);
618 if (req != &req->rq_rqbd->rqbd_req) {
619 /* NB request buffers use an embedded
620 * req if the incoming req unlinked the
621 * MD; this isn't one of them! */
622 OBD_FREE(req, sizeof(*req));
627 * drop a reference count of the request. if it reaches 0, we either
628 * put it into history list, or free it immediately.
630 void ptlrpc_server_drop_request(struct ptlrpc_request *req)
632 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
633 struct ptlrpc_service *svc = rqbd->rqbd_service;
638 if (!cfs_atomic_dec_and_test(&req->rq_refcount))
641 cfs_spin_lock(&svc->srv_at_lock);
642 if (req->rq_at_linked) {
643 struct ptlrpc_at_array *array = &svc->srv_at_array;
644 __u32 index = req->rq_at_index;
646 LASSERT(!cfs_list_empty(&req->rq_timed_list));
647 cfs_list_del_init(&req->rq_timed_list);
648 cfs_spin_lock(&req->rq_lock);
649 req->rq_at_linked = 0;
650 cfs_spin_unlock(&req->rq_lock);
651 array->paa_reqs_count[index]--;
654 LASSERT(cfs_list_empty(&req->rq_timed_list));
655 cfs_spin_unlock(&svc->srv_at_lock);
657 /* finalize request */
658 if (req->rq_export) {
659 class_export_put(req->rq_export);
660 req->rq_export = NULL;
663 cfs_spin_lock(&svc->srv_lock);
665 cfs_list_add(&req->rq_list, &rqbd->rqbd_reqs);
667 refcount = --(rqbd->rqbd_refcount);
669 /* request buffer is now idle: add to history */
670 cfs_list_del(&rqbd->rqbd_list);
671 cfs_list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
672 svc->srv_n_history_rqbds++;
674 /* cull some history?
675 * I expect only about 1 or 2 rqbds need to be recycled here */
676 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
677 rqbd = cfs_list_entry(svc->srv_history_rqbds.next,
678 struct ptlrpc_request_buffer_desc,
681 cfs_list_del(&rqbd->rqbd_list);
682 svc->srv_n_history_rqbds--;
684 /* remove rqbd's reqs from svc's req history while
685 * I've got the service lock */
686 cfs_list_for_each(tmp, &rqbd->rqbd_reqs) {
687 req = cfs_list_entry(tmp, struct ptlrpc_request,
689 /* Track the highest culled req seq */
690 if (req->rq_history_seq >
691 svc->srv_request_max_cull_seq)
692 svc->srv_request_max_cull_seq =
694 cfs_list_del(&req->rq_history_list);
697 cfs_spin_unlock(&svc->srv_lock);
699 cfs_list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
700 req = cfs_list_entry(rqbd->rqbd_reqs.next,
701 struct ptlrpc_request,
703 cfs_list_del(&req->rq_list);
704 ptlrpc_server_free_request(req);
707 cfs_spin_lock(&svc->srv_lock);
709 * now all reqs including the embedded req has been
710 * disposed, schedule request buffer for re-use.
712 LASSERT(cfs_atomic_read(&rqbd->rqbd_req.rq_refcount) ==
714 cfs_list_add_tail(&rqbd->rqbd_list,
715 &svc->srv_idle_rqbds);
718 cfs_spin_unlock(&svc->srv_lock);
719 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
720 /* If we are low on memory, we are not interested in history */
721 cfs_list_del(&req->rq_list);
722 cfs_list_del_init(&req->rq_history_list);
723 cfs_spin_unlock(&svc->srv_lock);
725 ptlrpc_server_free_request(req);
727 cfs_spin_unlock(&svc->srv_lock);
732 * to finish a request: stop sending more early replies, and release
733 * the request. should be called after we finished handling the request.
735 static void ptlrpc_server_finish_request(struct ptlrpc_service *svc,
736 struct ptlrpc_request *req)
738 ptlrpc_hpreq_fini(req);
740 cfs_spin_lock(&svc->srv_rq_lock);
741 svc->srv_n_active_reqs--;
743 svc->srv_n_active_hpreq--;
744 cfs_spin_unlock(&svc->srv_rq_lock);
746 ptlrpc_server_drop_request(req);
750 * This function makes sure dead exports are evicted in a timely manner.
751 * This function is only called when some export receives a message (i.e.,
752 * the network is up.)
754 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
756 struct obd_export *oldest_exp;
757 time_t oldest_time, new_time;
763 /* Compensate for slow machines, etc, by faking our request time
764 into the future. Although this can break the strict time-ordering
765 of the list, we can be really lazy here - we don't have to evict
766 at the exact right moment. Eventually, all silent exports
767 will make it to the top of the list. */
769 /* Do not pay attention on 1sec or smaller renewals. */
770 new_time = cfs_time_current_sec() + extra_delay;
771 if (exp->exp_last_request_time + 1 /*second */ >= new_time)
774 exp->exp_last_request_time = new_time;
775 CDEBUG(D_HA, "updating export %s at "CFS_TIME_T" exp %p\n",
776 exp->exp_client_uuid.uuid,
777 exp->exp_last_request_time, exp);
779 /* exports may get disconnected from the chain even though the
780 export has references, so we must keep the spin lock while
781 manipulating the lists */
782 cfs_spin_lock(&exp->exp_obd->obd_dev_lock);
784 if (cfs_list_empty(&exp->exp_obd_chain_timed)) {
785 /* this one is not timed */
786 cfs_spin_unlock(&exp->exp_obd->obd_dev_lock);
790 cfs_list_move_tail(&exp->exp_obd_chain_timed,
791 &exp->exp_obd->obd_exports_timed);
793 oldest_exp = cfs_list_entry(exp->exp_obd->obd_exports_timed.next,
794 struct obd_export, exp_obd_chain_timed);
795 oldest_time = oldest_exp->exp_last_request_time;
796 cfs_spin_unlock(&exp->exp_obd->obd_dev_lock);
798 if (exp->exp_obd->obd_recovering) {
799 /* be nice to everyone during recovery */
804 /* Note - racing to start/reset the obd_eviction timer is safe */
805 if (exp->exp_obd->obd_eviction_timer == 0) {
806 /* Check if the oldest entry is expired. */
807 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
809 /* We need a second timer, in case the net was down and
810 * it just came back. Since the pinger may skip every
811 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
812 * we better wait for 3. */
813 exp->exp_obd->obd_eviction_timer =
814 cfs_time_current_sec() + 3 * PING_INTERVAL;
815 CDEBUG(D_HA, "%s: Think about evicting %s from "CFS_TIME_T"\n",
816 exp->exp_obd->obd_name,
817 obd_export_nid2str(oldest_exp), oldest_time);
820 if (cfs_time_current_sec() >
821 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
822 /* The evictor won't evict anyone who we've heard from
823 * recently, so we don't have to check before we start
825 if (!ping_evictor_wake(exp))
826 exp->exp_obd->obd_eviction_timer = 0;
834 * Sanity check request \a req.
835 * Return 0 if all is ok, error code otherwise.
837 static int ptlrpc_check_req(struct ptlrpc_request *req)
841 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
842 req->rq_export->exp_conn_cnt)) {
843 DEBUG_REQ(D_ERROR, req,
844 "DROPPING req from old connection %d < %d",
845 lustre_msg_get_conn_cnt(req->rq_reqmsg),
846 req->rq_export->exp_conn_cnt);
849 if (unlikely(req->rq_export->exp_obd &&
850 req->rq_export->exp_obd->obd_fail)) {
851 /* Failing over, don't handle any more reqs, send
852 error response instead. */
853 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
854 req, req->rq_export->exp_obd->obd_name);
856 } else if (lustre_msg_get_flags(req->rq_reqmsg) &
857 (MSG_REPLAY | MSG_REQ_REPLAY_DONE) &&
858 !(req->rq_export->exp_obd->obd_recovering)) {
859 DEBUG_REQ(D_ERROR, req,
860 "Invalid replay without recovery");
861 class_fail_export(req->rq_export);
863 } else if (lustre_msg_get_transno(req->rq_reqmsg) != 0 &&
864 !(req->rq_export->exp_obd->obd_recovering)) {
865 DEBUG_REQ(D_ERROR, req, "Invalid req with transno "
866 LPU64" without recovery",
867 lustre_msg_get_transno(req->rq_reqmsg));
868 class_fail_export(req->rq_export);
872 if (unlikely(rc < 0)) {
879 static void ptlrpc_at_set_timer(struct ptlrpc_service *svc)
881 struct ptlrpc_at_array *array = &svc->srv_at_array;
884 cfs_spin_lock(&svc->srv_at_lock);
885 if (array->paa_count == 0) {
886 cfs_timer_disarm(&svc->srv_at_timer);
887 cfs_spin_unlock(&svc->srv_at_lock);
891 /* Set timer for closest deadline */
892 next = (__s32)(array->paa_deadline - cfs_time_current_sec() -
895 ptlrpc_at_timer((unsigned long)svc);
897 cfs_timer_arm(&svc->srv_at_timer, cfs_time_shift(next));
898 cfs_spin_unlock(&svc->srv_at_lock);
899 CDEBUG(D_INFO, "armed %s at %+ds\n", svc->srv_name, next);
902 /* Add rpc to early reply check list */
903 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
905 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
906 struct ptlrpc_request *rq = NULL;
907 struct ptlrpc_at_array *array = &svc->srv_at_array;
914 if (req->rq_no_reply)
917 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
920 cfs_spin_lock(&svc->srv_at_lock);
921 LASSERT(cfs_list_empty(&req->rq_timed_list));
923 index = (unsigned long)req->rq_deadline % array->paa_size;
924 if (array->paa_reqs_count[index] > 0) {
925 /* latest rpcs will have the latest deadlines in the list,
926 * so search backward. */
927 cfs_list_for_each_entry_reverse(rq,
928 &array->paa_reqs_array[index],
930 if (req->rq_deadline >= rq->rq_deadline) {
931 cfs_list_add(&req->rq_timed_list,
938 /* Add the request at the head of the list */
939 if (cfs_list_empty(&req->rq_timed_list))
940 cfs_list_add(&req->rq_timed_list,
941 &array->paa_reqs_array[index]);
943 cfs_spin_lock(&req->rq_lock);
944 req->rq_at_linked = 1;
945 cfs_spin_unlock(&req->rq_lock);
946 req->rq_at_index = index;
947 array->paa_reqs_count[index]++;
949 if (array->paa_count == 1 || array->paa_deadline > req->rq_deadline) {
950 array->paa_deadline = req->rq_deadline;
953 cfs_spin_unlock(&svc->srv_at_lock);
956 ptlrpc_at_set_timer(svc);
961 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req)
963 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
964 struct ptlrpc_request *reqcopy;
965 struct lustre_msg *reqmsg;
966 cfs_duration_t olddl = req->rq_deadline - cfs_time_current_sec();
971 /* deadline is when the client expects us to reply, margin is the
972 difference between clients' and servers' expectations */
973 DEBUG_REQ(D_ADAPTTO, req,
974 "%ssending early reply (deadline %+lds, margin %+lds) for "
975 "%d+%d", AT_OFF ? "AT off - not " : "",
976 olddl, olddl - at_get(&svc->srv_at_estimate),
977 at_get(&svc->srv_at_estimate), at_extra);
983 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), "
984 "not sending early reply. Consider increasing "
985 "at_early_margin (%d)?", olddl, at_early_margin);
987 /* Return an error so we're not re-added to the timed list. */
991 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
992 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, "
993 "but no AT support");
997 if (req->rq_export &&
998 lustre_msg_get_flags(req->rq_reqmsg) &
999 (MSG_REPLAY | MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE)) {
1000 /* During recovery, we don't want to send too many early
1001 * replies, but on the other hand we want to make sure the
1002 * client has enough time to resend if the rpc is lost. So
1003 * during the recovery period send at least 4 early replies,
1004 * spacing them every at_extra if we can. at_estimate should
1005 * always equal this fixed value during recovery. */
1006 at_measured(&svc->srv_at_estimate, min(at_extra,
1007 req->rq_export->exp_obd->obd_recovery_timeout / 4));
1009 /* Fake our processing time into the future to ask the clients
1010 * for some extra amount of time */
1011 at_measured(&svc->srv_at_estimate, at_extra +
1012 cfs_time_current_sec() -
1013 req->rq_arrival_time.tv_sec);
1015 /* Check to see if we've actually increased the deadline -
1016 * we may be past adaptive_max */
1017 if (req->rq_deadline >= req->rq_arrival_time.tv_sec +
1018 at_get(&svc->srv_at_estimate)) {
1019 DEBUG_REQ(D_WARNING, req, "Couldn't add any time "
1020 "(%ld/%ld), not sending early reply\n",
1021 olddl, req->rq_arrival_time.tv_sec +
1022 at_get(&svc->srv_at_estimate) -
1023 cfs_time_current_sec());
1027 newdl = cfs_time_current_sec() + at_get(&svc->srv_at_estimate);
1029 OBD_ALLOC(reqcopy, sizeof *reqcopy);
1030 if (reqcopy == NULL)
1032 OBD_ALLOC_LARGE(reqmsg, req->rq_reqlen);
1034 OBD_FREE(reqcopy, sizeof *reqcopy);
1039 reqcopy->rq_reply_state = NULL;
1040 reqcopy->rq_rep_swab_mask = 0;
1041 reqcopy->rq_pack_bulk = 0;
1042 reqcopy->rq_pack_udesc = 0;
1043 reqcopy->rq_packed_final = 0;
1044 sptlrpc_svc_ctx_addref(reqcopy);
1045 /* We only need the reqmsg for the magic */
1046 reqcopy->rq_reqmsg = reqmsg;
1047 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1049 LASSERT(cfs_atomic_read(&req->rq_refcount));
1050 /** if it is last refcount then early reply isn't needed */
1051 if (cfs_atomic_read(&req->rq_refcount) == 1) {
1052 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, "
1053 "abort sending early reply\n");
1054 GOTO(out, rc = -EINVAL);
1057 /* Connection ref */
1058 reqcopy->rq_export = class_conn2export(
1059 lustre_msg_get_handle(reqcopy->rq_reqmsg));
1060 if (reqcopy->rq_export == NULL)
1061 GOTO(out, rc = -ENODEV);
1064 class_export_rpc_get(reqcopy->rq_export);
1065 if (reqcopy->rq_export->exp_obd &&
1066 reqcopy->rq_export->exp_obd->obd_fail)
1067 GOTO(out_put, rc = -ENODEV);
1069 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
1073 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
1076 /* Adjust our own deadline to what we told the client */
1077 req->rq_deadline = newdl;
1078 req->rq_early_count++; /* number sent, server side */
1080 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
1083 /* Free the (early) reply state from lustre_pack_reply.
1084 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
1085 ptlrpc_req_drop_rs(reqcopy);
1088 class_export_rpc_put(reqcopy->rq_export);
1089 class_export_put(reqcopy->rq_export);
1091 sptlrpc_svc_ctx_decref(reqcopy);
1092 OBD_FREE_LARGE(reqmsg, req->rq_reqlen);
1093 OBD_FREE(reqcopy, sizeof *reqcopy);
1097 /* Send early replies to everybody expiring within at_early_margin
1098 asking for at_extra time */
1099 static int ptlrpc_at_check_timed(struct ptlrpc_service *svc)
1101 struct ptlrpc_request *rq, *n;
1102 cfs_list_t work_list;
1103 struct ptlrpc_at_array *array = &svc->srv_at_array;
1106 time_t now = cfs_time_current_sec();
1107 cfs_duration_t delay;
1108 int first, counter = 0;
1111 cfs_spin_lock(&svc->srv_at_lock);
1112 if (svc->srv_at_check == 0) {
1113 cfs_spin_unlock(&svc->srv_at_lock);
1116 delay = cfs_time_sub(cfs_time_current(), svc->srv_at_checktime);
1117 svc->srv_at_check = 0;
1119 if (array->paa_count == 0) {
1120 cfs_spin_unlock(&svc->srv_at_lock);
1124 /* The timer went off, but maybe the nearest rpc already completed. */
1125 first = array->paa_deadline - now;
1126 if (first > at_early_margin) {
1127 /* We've still got plenty of time. Reset the timer. */
1128 cfs_spin_unlock(&svc->srv_at_lock);
1129 ptlrpc_at_set_timer(svc);
1133 /* We're close to a timeout, and we don't know how much longer the
1134 server will take. Send early replies to everyone expiring soon. */
1135 CFS_INIT_LIST_HEAD(&work_list);
1137 index = (unsigned long)array->paa_deadline % array->paa_size;
1138 count = array->paa_count;
1140 count -= array->paa_reqs_count[index];
1141 cfs_list_for_each_entry_safe(rq, n,
1142 &array->paa_reqs_array[index],
1144 if (rq->rq_deadline <= now + at_early_margin) {
1145 cfs_list_del_init(&rq->rq_timed_list);
1147 * ptlrpc_server_drop_request() may drop
1148 * refcount to 0 already. Let's check this and
1149 * don't add entry to work_list
1151 if (likely(cfs_atomic_inc_not_zero(&rq->rq_refcount)))
1152 cfs_list_add(&rq->rq_timed_list, &work_list);
1154 array->paa_reqs_count[index]--;
1156 cfs_spin_lock(&rq->rq_lock);
1157 rq->rq_at_linked = 0;
1158 cfs_spin_unlock(&rq->rq_lock);
1162 /* update the earliest deadline */
1163 if (deadline == -1 || rq->rq_deadline < deadline)
1164 deadline = rq->rq_deadline;
1169 if (++index >= array->paa_size)
1172 array->paa_deadline = deadline;
1173 cfs_spin_unlock(&svc->srv_at_lock);
1175 /* we have a new earliest deadline, restart the timer */
1176 ptlrpc_at_set_timer(svc);
1178 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
1179 "replies\n", first, at_extra, counter);
1181 /* We're already past request deadlines before we even get a
1182 chance to send early replies */
1183 LCONSOLE_WARN("%s: This server is not able to keep up with "
1184 "request traffic (cpu-bound).\n", svc->srv_name);
1185 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
1186 "delay="CFS_DURATION_T"(jiff)\n",
1187 counter, svc->srv_n_queued_reqs, svc->srv_n_active_reqs,
1188 at_get(&svc->srv_at_estimate), delay);
1191 /* we took additional refcount so entries can't be deleted from list, no
1192 * locking is needed */
1193 while (!cfs_list_empty(&work_list)) {
1194 rq = cfs_list_entry(work_list.next, struct ptlrpc_request,
1196 cfs_list_del_init(&rq->rq_timed_list);
1198 if (ptlrpc_at_send_early_reply(rq) == 0)
1199 ptlrpc_at_add_timed(rq);
1201 ptlrpc_server_drop_request(rq);
1208 * Put the request to the export list if the request may become
1209 * a high priority one.
1211 static int ptlrpc_hpreq_init(struct ptlrpc_service *svc,
1212 struct ptlrpc_request *req)
1217 if (svc->srv_hpreq_handler) {
1218 rc = svc->srv_hpreq_handler(req);
1222 if (req->rq_export && req->rq_ops) {
1223 /* Perform request specific check. We should do this check
1224 * before the request is added into exp_hp_rpcs list otherwise
1225 * it may hit swab race at LU-1044. */
1226 if (req->rq_ops->hpreq_check)
1227 rc = req->rq_ops->hpreq_check(req);
1229 cfs_spin_lock_bh(&req->rq_export->exp_rpc_lock);
1230 cfs_list_add(&req->rq_exp_list,
1231 &req->rq_export->exp_hp_rpcs);
1232 cfs_spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1238 /** Remove the request from the export list. */
1239 static void ptlrpc_hpreq_fini(struct ptlrpc_request *req)
1242 if (req->rq_export && req->rq_ops) {
1243 /* refresh lock timeout again so that client has more
1244 * room to send lock cancel RPC. */
1245 if (req->rq_ops->hpreq_fini)
1246 req->rq_ops->hpreq_fini(req);
1248 cfs_spin_lock_bh(&req->rq_export->exp_rpc_lock);
1249 cfs_list_del_init(&req->rq_exp_list);
1250 cfs_spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1256 * Make the request a high priority one.
1258 * All the high priority requests are queued in a separate FIFO
1259 * ptlrpc_service::srv_request_hpq list which is parallel to
1260 * ptlrpc_service::srv_request_queue list but has a higher priority
1263 * \see ptlrpc_server_handle_request().
1265 static void ptlrpc_hpreq_reorder_nolock(struct ptlrpc_service *svc,
1266 struct ptlrpc_request *req)
1269 LASSERT(svc != NULL);
1270 cfs_spin_lock(&req->rq_lock);
1271 if (req->rq_hp == 0) {
1272 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1274 /* Add to the high priority queue. */
1275 cfs_list_move_tail(&req->rq_list, &svc->srv_request_hpq);
1277 if (opc != OBD_PING)
1278 DEBUG_REQ(D_RPCTRACE, req, "high priority req");
1280 cfs_spin_unlock(&req->rq_lock);
1285 * \see ptlrpc_hpreq_reorder_nolock
1287 void ptlrpc_hpreq_reorder(struct ptlrpc_request *req)
1289 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
1292 cfs_spin_lock(&svc->srv_rq_lock);
1293 /* It may happen that the request is already taken for the processing
1294 * but still in the export list, or the request is not in the request
1295 * queue but in the export list already, do not add it into the
1297 if (!cfs_list_empty(&req->rq_list))
1298 ptlrpc_hpreq_reorder_nolock(svc, req);
1299 cfs_spin_unlock(&svc->srv_rq_lock);
1303 /** Check if the request is a high priority one. */
1304 static int ptlrpc_server_hpreq_check(struct ptlrpc_service *svc,
1305 struct ptlrpc_request *req)
1309 /* Check by request opc. */
1310 if (OBD_PING == lustre_msg_get_opc(req->rq_reqmsg))
1313 RETURN(ptlrpc_hpreq_init(svc, req));
1316 /** Check if a request is a high priority one. */
1317 static int ptlrpc_server_request_add(struct ptlrpc_service *svc,
1318 struct ptlrpc_request *req)
1323 rc = ptlrpc_server_hpreq_check(svc, req);
1327 cfs_spin_lock(&svc->srv_rq_lock);
1330 ptlrpc_hpreq_reorder_nolock(svc, req);
1332 cfs_list_add_tail(&req->rq_list,
1333 &svc->srv_request_queue);
1335 cfs_spin_unlock(&svc->srv_rq_lock);
1341 * Allow to handle high priority request
1342 * User can call it w/o any lock but need to hold ptlrpc_service::srv_rq_lock
1343 * to get reliable result
1345 static int ptlrpc_server_allow_high(struct ptlrpc_service *svc, int force)
1350 if (svc->srv_n_active_reqs >= svc->srv_threads_running - 1)
1353 return cfs_list_empty(&svc->srv_request_queue) ||
1354 svc->srv_hpreq_count < svc->srv_hpreq_ratio;
1357 static int ptlrpc_server_high_pending(struct ptlrpc_service *svc, int force)
1359 return ptlrpc_server_allow_high(svc, force) &&
1360 !cfs_list_empty(&svc->srv_request_hpq);
1364 * Only allow normal priority requests on a service that has a high-priority
1365 * queue if forced (i.e. cleanup), if there are other high priority requests
1366 * already being processed (i.e. those threads can service more high-priority
1367 * requests), or if there are enough idle threads that a later thread can do
1368 * a high priority request.
1369 * User can call it w/o any lock but need to hold ptlrpc_service::srv_rq_lock
1370 * to get reliable result
1372 static int ptlrpc_server_allow_normal(struct ptlrpc_service *svc, int force)
1375 if (1) /* always allow to handle normal request for liblustre */
1379 svc->srv_n_active_reqs < svc->srv_threads_running - 2)
1382 if (svc->srv_n_active_reqs >= svc->srv_threads_running - 1)
1385 return svc->srv_n_active_hpreq > 0 || svc->srv_hpreq_handler == NULL;
1388 static int ptlrpc_server_normal_pending(struct ptlrpc_service *svc, int force)
1390 return ptlrpc_server_allow_normal(svc, force) &&
1391 !cfs_list_empty(&svc->srv_request_queue);
1395 * Returns true if there are requests available in incoming
1396 * request queue for processing and it is allowed to fetch them.
1397 * User can call it w/o any lock but need to hold ptlrpc_service::srv_rq_lock
1398 * to get reliable result
1399 * \see ptlrpc_server_allow_normal
1400 * \see ptlrpc_server_allow high
1403 ptlrpc_server_request_pending(struct ptlrpc_service *svc, int force)
1405 return ptlrpc_server_high_pending(svc, force) ||
1406 ptlrpc_server_normal_pending(svc, force);
1410 * Fetch a request for processing from queue of unprocessed requests.
1411 * Favors high-priority requests.
1412 * Returns a pointer to fetched request.
1414 static struct ptlrpc_request *
1415 ptlrpc_server_request_get(struct ptlrpc_service *svc, int force)
1417 struct ptlrpc_request *req;
1420 if (ptlrpc_server_high_pending(svc, force)) {
1421 req = cfs_list_entry(svc->srv_request_hpq.next,
1422 struct ptlrpc_request, rq_list);
1423 svc->srv_hpreq_count++;
1428 if (ptlrpc_server_normal_pending(svc, force)) {
1429 req = cfs_list_entry(svc->srv_request_queue.next,
1430 struct ptlrpc_request, rq_list);
1431 svc->srv_hpreq_count = 0;
1438 * Handle freshly incoming reqs, add to timed early reply list,
1439 * pass on to regular request queue.
1440 * All incoming requests pass through here before getting into
1441 * ptlrpc_server_handle_req later on.
1444 ptlrpc_server_handle_req_in(struct ptlrpc_service *svc)
1446 struct ptlrpc_request *req;
1453 cfs_spin_lock(&svc->srv_lock);
1454 if (cfs_list_empty(&svc->srv_req_in_queue)) {
1455 cfs_spin_unlock(&svc->srv_lock);
1459 req = cfs_list_entry(svc->srv_req_in_queue.next,
1460 struct ptlrpc_request, rq_list);
1461 cfs_list_del_init (&req->rq_list);
1462 svc->srv_n_queued_reqs--;
1463 /* Consider this still a "queued" request as far as stats are
1465 cfs_spin_unlock(&svc->srv_lock);
1467 /* go through security check/transform */
1468 rc = sptlrpc_svc_unwrap_request(req);
1472 case SECSVC_COMPLETE:
1473 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
1482 * for null-flavored rpc, msg has been unpacked by sptlrpc, although
1483 * redo it wouldn't be harmful.
1485 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
1486 rc = ptlrpc_unpack_req_msg(req, req->rq_reqlen);
1488 CERROR("error unpacking request: ptl %d from %s "
1489 "x"LPU64"\n", svc->srv_req_portal,
1490 libcfs_id2str(req->rq_peer), req->rq_xid);
1495 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
1497 CERROR ("error unpacking ptlrpc body: ptl %d from %s x"
1498 LPU64"\n", svc->srv_req_portal,
1499 libcfs_id2str(req->rq_peer), req->rq_xid);
1503 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_REQ_OPC) &&
1504 lustre_msg_get_opc(req->rq_reqmsg) == cfs_fail_val) {
1505 CERROR("drop incoming rpc opc %u, x"LPU64"\n",
1506 cfs_fail_val, req->rq_xid);
1511 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
1512 CERROR("wrong packet type received (type=%u) from %s\n",
1513 lustre_msg_get_type(req->rq_reqmsg),
1514 libcfs_id2str(req->rq_peer));
1518 switch(lustre_msg_get_opc(req->rq_reqmsg)) {
1521 req->rq_bulk_write = 1;
1525 case MGS_CONFIG_READ:
1526 req->rq_bulk_read = 1;
1530 CDEBUG(D_RPCTRACE, "got req x"LPU64"\n", req->rq_xid);
1532 req->rq_export = class_conn2export(
1533 lustre_msg_get_handle(req->rq_reqmsg));
1534 if (req->rq_export) {
1535 rc = ptlrpc_check_req(req);
1537 rc = sptlrpc_target_export_check(req->rq_export, req);
1539 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
1540 "illegal security flavor,");
1545 ptlrpc_update_export_timer(req->rq_export, 0);
1548 /* req_in handling should/must be fast */
1549 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
1550 DEBUG_REQ(D_WARNING, req, "Slow req_in handling "CFS_DURATION_T"s",
1551 cfs_time_sub(cfs_time_current_sec(),
1552 req->rq_arrival_time.tv_sec));
1554 /* Set rpc server deadline and add it to the timed list */
1555 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1556 MSGHDR_AT_SUPPORT) ?
1557 /* The max time the client expects us to take */
1558 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1559 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1560 if (unlikely(deadline == 0)) {
1561 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1565 ptlrpc_at_add_timed(req);
1567 /* Move it over to the request processing queue */
1568 rc = ptlrpc_server_request_add(svc, req);
1570 ptlrpc_hpreq_fini(req);
1573 cfs_waitq_signal(&svc->srv_waitq);
1577 cfs_spin_lock(&svc->srv_rq_lock);
1578 svc->srv_n_active_reqs++;
1579 cfs_spin_unlock(&svc->srv_rq_lock);
1580 ptlrpc_server_finish_request(svc, req);
1586 * Main incoming request handling logic.
1587 * Calls handler function from service to do actual processing.
1590 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
1591 struct ptlrpc_thread *thread)
1593 struct obd_export *export = NULL;
1594 struct ptlrpc_request *request;
1595 struct timeval work_start;
1596 struct timeval work_end;
1604 cfs_spin_lock(&svc->srv_rq_lock);
1606 /* !@%$# liblustre only has 1 thread */
1607 if (cfs_atomic_read(&svc->srv_n_difficult_replies) != 0) {
1608 cfs_spin_unlock(&svc->srv_rq_lock);
1612 request = ptlrpc_server_request_get(svc, 0);
1613 if (request == NULL) {
1614 cfs_spin_unlock(&svc->srv_rq_lock);
1618 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT))
1619 fail_opc = OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT;
1620 else if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
1621 fail_opc = OBD_FAIL_PTLRPC_HPREQ_TIMEOUT;
1623 if (unlikely(fail_opc)) {
1624 if (request->rq_export && request->rq_ops) {
1625 cfs_spin_unlock(&svc->srv_rq_lock);
1626 OBD_FAIL_TIMEOUT(fail_opc, 4);
1627 cfs_spin_lock(&svc->srv_rq_lock);
1628 request = ptlrpc_server_request_get(svc, 0);
1629 if (request == NULL) {
1630 cfs_spin_unlock(&svc->srv_rq_lock);
1636 cfs_list_del_init(&request->rq_list);
1637 svc->srv_n_active_reqs++;
1639 svc->srv_n_active_hpreq++;
1641 cfs_spin_unlock(&svc->srv_rq_lock);
1643 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
1645 if(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
1646 libcfs_debug_dumplog();
1648 cfs_gettimeofday(&work_start);
1649 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
1650 if (likely(svc->srv_stats != NULL)) {
1651 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
1653 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
1654 svc->srv_n_queued_reqs);
1655 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
1656 svc->srv_n_active_reqs);
1657 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
1658 at_get(&svc->srv_at_estimate));
1661 rc = lu_context_init(&request->rq_session,
1662 LCT_SESSION|LCT_REMEMBER|LCT_NOREF);
1664 CERROR("Failure to initialize session: %d\n", rc);
1667 request->rq_session.lc_thread = thread;
1668 request->rq_session.lc_cookie = 0x5;
1669 lu_context_enter(&request->rq_session);
1671 CDEBUG(D_NET, "got req "LPU64"\n", request->rq_xid);
1673 request->rq_svc_thread = thread;
1675 request->rq_svc_thread->t_env->le_ses = &request->rq_session;
1677 if (likely(request->rq_export)) {
1678 if (unlikely(ptlrpc_check_req(request)))
1680 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
1681 export = class_export_rpc_get(request->rq_export);
1684 /* Discard requests queued for longer than the deadline.
1685 The deadline is increased if we send an early reply. */
1686 if (cfs_time_current_sec() > request->rq_deadline) {
1687 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
1688 ": deadline "CFS_DURATION_T":"CFS_DURATION_T"s ago\n",
1689 libcfs_id2str(request->rq_peer),
1690 cfs_time_sub(request->rq_deadline,
1691 request->rq_arrival_time.tv_sec),
1692 cfs_time_sub(cfs_time_current_sec(),
1693 request->rq_deadline));
1694 goto put_rpc_export;
1697 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
1698 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1699 (request->rq_export ?
1700 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1701 (request->rq_export ?
1702 cfs_atomic_read(&request->rq_export->exp_refcount) : -99),
1703 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1704 libcfs_id2str(request->rq_peer),
1705 lustre_msg_get_opc(request->rq_reqmsg));
1707 if (lustre_msg_get_opc(request->rq_reqmsg) != OBD_PING)
1708 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, cfs_fail_val);
1710 rc = svc->srv_handler(request);
1712 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
1716 class_export_rpc_put(export);
1718 lu_context_exit(&request->rq_session);
1719 lu_context_fini(&request->rq_session);
1721 if (unlikely(cfs_time_current_sec() > request->rq_deadline)) {
1722 DEBUG_REQ(D_WARNING, request, "Request x"LPU64" took longer "
1723 "than estimated ("CFS_DURATION_T":"CFS_DURATION_T"s);"
1724 " client may timeout.",
1725 request->rq_xid, cfs_time_sub(request->rq_deadline,
1726 request->rq_arrival_time.tv_sec),
1727 cfs_time_sub(cfs_time_current_sec(),
1728 request->rq_deadline));
1731 cfs_gettimeofday(&work_end);
1732 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1733 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
1734 "%s:%s+%d:%d:x"LPU64":%s:%d Request procesed in "
1735 "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
1737 (request->rq_export ?
1738 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1739 (request->rq_export ?
1740 cfs_atomic_read(&request->rq_export->exp_refcount) : -99),
1741 lustre_msg_get_status(request->rq_reqmsg),
1743 libcfs_id2str(request->rq_peer),
1744 lustre_msg_get_opc(request->rq_reqmsg),
1746 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
1747 (request->rq_repmsg ?
1748 lustre_msg_get_transno(request->rq_repmsg) :
1749 request->rq_transno),
1751 (request->rq_repmsg ?
1752 lustre_msg_get_status(request->rq_repmsg) : -999));
1753 if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
1754 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
1755 int opc = opcode_offset(op);
1756 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
1757 LASSERT(opc < LUSTRE_MAX_OPCODES);
1758 lprocfs_counter_add(svc->srv_stats,
1759 opc + EXTRA_MAX_OPCODES,
1763 if (unlikely(request->rq_early_count)) {
1764 DEBUG_REQ(D_ADAPTTO, request,
1765 "sent %d early replies before finishing in "
1767 request->rq_early_count,
1768 cfs_time_sub(work_end.tv_sec,
1769 request->rq_arrival_time.tv_sec));
1773 ptlrpc_server_finish_request(svc, request);
1779 * An internal function to process a single reply state object.
1782 ptlrpc_handle_rs (struct ptlrpc_reply_state *rs)
1784 struct ptlrpc_service *svc = rs->rs_service;
1785 struct obd_export *exp;
1790 exp = rs->rs_export;
1792 LASSERT (rs->rs_difficult);
1793 LASSERT (rs->rs_scheduled);
1794 LASSERT (cfs_list_empty(&rs->rs_list));
1796 cfs_spin_lock (&exp->exp_lock);
1797 /* Noop if removed already */
1798 cfs_list_del_init (&rs->rs_exp_list);
1799 cfs_spin_unlock (&exp->exp_lock);
1801 /* The disk commit callback holds exp_uncommitted_replies_lock while it
1802 * iterates over newly committed replies, removing them from
1803 * exp_uncommitted_replies. It then drops this lock and schedules the
1804 * replies it found for handling here.
1806 * We can avoid contention for exp_uncommitted_replies_lock between the
1807 * HRT threads and further commit callbacks by checking rs_committed
1808 * which is set in the commit callback while it holds both
1809 * rs_lock and exp_uncommitted_reples.
1811 * If we see rs_committed clear, the commit callback _may_ not have
1812 * handled this reply yet and we race with it to grab
1813 * exp_uncommitted_replies_lock before removing the reply from
1814 * exp_uncommitted_replies. Note that if we lose the race and the
1815 * reply has already been removed, list_del_init() is a noop.
1817 * If we see rs_committed set, we know the commit callback is handling,
1818 * or has handled this reply since store reordering might allow us to
1819 * see rs_committed set out of sequence. But since this is done
1820 * holding rs_lock, we can be sure it has all completed once we hold
1821 * rs_lock, which we do right next.
1823 if (!rs->rs_committed) {
1824 cfs_spin_lock(&exp->exp_uncommitted_replies_lock);
1825 cfs_list_del_init(&rs->rs_obd_list);
1826 cfs_spin_unlock(&exp->exp_uncommitted_replies_lock);
1829 cfs_spin_lock(&rs->rs_lock);
1831 been_handled = rs->rs_handled;
1834 nlocks = rs->rs_nlocks; /* atomic "steal", but */
1835 rs->rs_nlocks = 0; /* locks still on rs_locks! */
1837 if (nlocks == 0 && !been_handled) {
1838 /* If we see this, we should already have seen the warning
1839 * in mds_steal_ack_locks() */
1840 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
1843 rs->rs_xid, rs->rs_transno, rs->rs_opc,
1844 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1847 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1848 cfs_spin_unlock(&rs->rs_lock);
1850 if (!been_handled && rs->rs_on_net) {
1851 LNetMDUnlink(rs->rs_md_h);
1852 /* Ignore return code; we're racing with
1856 while (nlocks-- > 0)
1857 ldlm_lock_decref(&rs->rs_locks[nlocks],
1858 rs->rs_modes[nlocks]);
1860 cfs_spin_lock(&rs->rs_lock);
1863 rs->rs_scheduled = 0;
1865 if (!rs->rs_on_net) {
1867 cfs_spin_unlock(&rs->rs_lock);
1869 class_export_put (exp);
1870 rs->rs_export = NULL;
1871 ptlrpc_rs_decref (rs);
1872 if (cfs_atomic_dec_and_test(&svc->srv_n_difficult_replies) &&
1873 svc->srv_is_stopping)
1874 cfs_waitq_broadcast(&svc->srv_waitq);
1878 /* still on the net; callback will schedule */
1879 cfs_spin_unlock(&rs->rs_lock);
1886 * Check whether given service has a reply available for processing
1889 * \param svc a ptlrpc service
1890 * \retval 0 no replies processed
1891 * \retval 1 one reply processed
1894 ptlrpc_server_handle_reply(struct ptlrpc_service *svc)
1896 struct ptlrpc_reply_state *rs = NULL;
1899 cfs_spin_lock(&svc->srv_rs_lock);
1900 if (!cfs_list_empty(&svc->srv_reply_queue)) {
1901 rs = cfs_list_entry(svc->srv_reply_queue.prev,
1902 struct ptlrpc_reply_state,
1904 cfs_list_del_init(&rs->rs_list);
1906 cfs_spin_unlock(&svc->srv_rs_lock);
1908 ptlrpc_handle_rs(rs);
1912 /* FIXME make use of timeout later */
1914 liblustre_check_services (void *arg)
1916 int did_something = 0;
1918 cfs_list_t *tmp, *nxt;
1921 /* I'm relying on being single threaded, not to have to lock
1922 * ptlrpc_all_services etc */
1923 cfs_list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
1924 struct ptlrpc_service *svc =
1925 cfs_list_entry (tmp, struct ptlrpc_service, srv_list);
1927 if (svc->srv_threads_running != 0) /* I've recursed */
1930 /* service threads can block for bulk, so this limits us
1931 * (arbitrarily) to recursing 1 stack frame per service.
1932 * Note that the problem with recursion is that we have to
1933 * unwind completely before our caller can resume. */
1935 svc->srv_threads_running++;
1938 rc = ptlrpc_server_handle_req_in(svc);
1939 rc |= ptlrpc_server_handle_reply(svc);
1940 rc |= ptlrpc_at_check_timed(svc);
1941 rc |= ptlrpc_server_handle_request(svc, NULL);
1942 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
1943 did_something |= rc;
1946 svc->srv_threads_running--;
1949 RETURN(did_something);
1951 #define ptlrpc_stop_all_threads(s) do {} while (0)
1953 #else /* __KERNEL__ */
1956 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
1958 int avail = svc->srv_nrqbd_receiving;
1959 int low_water = test_req_buffer_pressure ? 0 :
1960 svc->srv_nbuf_per_group/2;
1962 /* NB I'm not locking; just looking. */
1964 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
1965 * allowed the request history to grow out of control. We could put a
1966 * sanity check on that here and cull some history if we need the
1969 if (avail <= low_water)
1970 ptlrpc_grow_req_bufs(svc);
1973 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
1978 ptlrpc_retry_rqbds(void *arg)
1980 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
1982 svc->srv_rqbd_timeout = 0;
1983 return (-ETIMEDOUT);
1987 ptlrpc_threads_enough(struct ptlrpc_service *svc)
1989 return svc->srv_n_active_reqs <
1990 svc->srv_threads_running - 1 - (svc->srv_hpreq_handler != NULL);
1994 * allowed to create more threads
1995 * user can call it w/o any lock but need to hold ptlrpc_service::srv_lock to
1996 * get reliable result
1999 ptlrpc_threads_increasable(struct ptlrpc_service *svc)
2001 return svc->srv_threads_running +
2002 svc->srv_threads_starting < svc->srv_threads_max;
2006 * too many requests and allowed to create more threads
2009 ptlrpc_threads_need_create(struct ptlrpc_service *svc)
2011 return !ptlrpc_threads_enough(svc) && ptlrpc_threads_increasable(svc);
2015 ptlrpc_thread_stopping(struct ptlrpc_thread *thread)
2017 return thread_is_stopping(thread) ||
2018 thread->t_svc->srv_is_stopping;
2022 ptlrpc_rqbd_pending(struct ptlrpc_service *svc)
2024 return !cfs_list_empty(&svc->srv_idle_rqbds) &&
2025 svc->srv_rqbd_timeout == 0;
2029 ptlrpc_at_check(struct ptlrpc_service *svc)
2031 return svc->srv_at_check;
2035 * requests wait on preprocessing
2036 * user can call it w/o any lock but need to hold ptlrpc_service::srv_lock to
2037 * get reliable result
2040 ptlrpc_server_request_waiting(struct ptlrpc_service *svc)
2042 return !cfs_list_empty(&svc->srv_req_in_queue);
2045 static __attribute__((__noinline__)) int
2046 ptlrpc_wait_event(struct ptlrpc_service *svc,
2047 struct ptlrpc_thread *thread)
2049 /* Don't exit while there are replies to be handled */
2050 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
2051 ptlrpc_retry_rqbds, svc);
2053 lc_watchdog_disable(thread->t_watchdog);
2057 l_wait_event_exclusive_head(svc->srv_waitq,
2058 ptlrpc_thread_stopping(thread) ||
2059 ptlrpc_server_request_waiting(svc) ||
2060 ptlrpc_server_request_pending(svc, 0) ||
2061 ptlrpc_rqbd_pending(svc) ||
2062 ptlrpc_at_check(svc), &lwi);
2064 if (ptlrpc_thread_stopping(thread))
2067 lc_watchdog_touch(thread->t_watchdog, CFS_GET_TIMEOUT(svc));
2073 * Main thread body for service threads.
2074 * Waits in a loop waiting for new requests to process to appear.
2075 * Every time an incoming requests is added to its queue, a waitq
2076 * is woken up and one of the threads will handle it.
2078 static int ptlrpc_main(void *arg)
2080 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
2081 struct ptlrpc_service *svc = data->svc;
2082 struct ptlrpc_thread *thread = data->thread;
2083 struct ptlrpc_reply_state *rs;
2084 #ifdef WITH_GROUP_INFO
2085 cfs_group_info_t *ginfo = NULL;
2088 int counter = 0, rc = 0;
2091 thread->t_pid = cfs_curproc_pid();
2092 cfs_daemonize_ctxt(data->name);
2094 #if defined(HAVE_NODE_TO_CPUMASK) && defined(CONFIG_NUMA)
2095 /* we need to do this before any per-thread allocation is done so that
2096 * we get the per-thread allocations on local node. bug 7342 */
2097 if (svc->srv_cpu_affinity) {
2100 for (cpu = 0, num_cpu = 0; cpu < cfs_num_possible_cpus();
2102 if (!cfs_cpu_online(cpu))
2104 if (num_cpu == thread->t_id % cfs_num_online_cpus())
2108 cfs_set_cpus_allowed(cfs_current(),
2109 node_to_cpumask(cpu_to_node(cpu)));
2113 #ifdef WITH_GROUP_INFO
2114 ginfo = cfs_groups_alloc(0);
2120 cfs_set_current_groups(ginfo);
2121 cfs_put_group_info(ginfo);
2124 if (svc->srv_init != NULL) {
2125 rc = svc->srv_init(thread);
2136 rc = lu_context_init(&env->le_ctx,
2137 svc->srv_ctx_tags|LCT_REMEMBER|LCT_NOREF);
2141 thread->t_env = env;
2142 env->le_ctx.lc_thread = thread;
2143 env->le_ctx.lc_cookie = 0x6;
2145 /* Alloc reply state structure for this one */
2146 OBD_ALLOC_LARGE(rs, svc->srv_max_reply_size);
2152 cfs_spin_lock(&svc->srv_lock);
2154 LASSERT(thread_is_starting(thread));
2155 thread_clear_flags(thread, SVC_STARTING);
2156 svc->srv_threads_starting--;
2158 /* SVC_STOPPING may already be set here if someone else is trying
2159 * to stop the service while this new thread has been dynamically
2160 * forked. We still set SVC_RUNNING to let our creator know that
2161 * we are now running, however we will exit as soon as possible */
2162 thread_add_flags(thread, SVC_RUNNING);
2163 svc->srv_threads_running++;
2164 cfs_spin_unlock(&svc->srv_lock);
2167 * wake up our creator. Note: @data is invalid after this point,
2168 * because it's allocated on ptlrpc_start_thread() stack.
2170 cfs_waitq_signal(&thread->t_ctl_waitq);
2172 thread->t_watchdog = lc_watchdog_add(CFS_GET_TIMEOUT(svc), NULL, NULL);
2174 cfs_spin_lock(&svc->srv_rs_lock);
2175 cfs_list_add(&rs->rs_list, &svc->srv_free_rs_list);
2176 cfs_waitq_signal(&svc->srv_free_rs_waitq);
2177 cfs_spin_unlock(&svc->srv_rs_lock);
2179 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
2180 svc->srv_threads_running);
2182 /* XXX maintain a list of all managed devices: insert here */
2183 while (!ptlrpc_thread_stopping(thread)) {
2184 if (ptlrpc_wait_event(svc, thread))
2187 ptlrpc_check_rqbd_pool(svc);
2189 if (ptlrpc_threads_need_create(svc)) {
2190 /* Ignore return code - we tried... */
2191 ptlrpc_start_thread(svc);
2194 /* Process all incoming reqs before handling any */
2195 if (ptlrpc_server_request_waiting(svc)) {
2196 ptlrpc_server_handle_req_in(svc);
2197 /* but limit ourselves in case of flood */
2198 if (counter++ < 100)
2203 if (ptlrpc_at_check(svc))
2204 ptlrpc_at_check_timed(svc);
2206 if (ptlrpc_server_request_pending(svc, 0)) {
2207 lu_context_enter(&env->le_ctx);
2208 ptlrpc_server_handle_request(svc, thread);
2209 lu_context_exit(&env->le_ctx);
2212 if (ptlrpc_rqbd_pending(svc) &&
2213 ptlrpc_server_post_idle_rqbds(svc) < 0) {
2214 /* I just failed to repost request buffers.
2215 * Wait for a timeout (unless something else
2216 * happens) before I try again */
2217 svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
2218 CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
2219 svc->srv_nrqbd_receiving);
2223 lc_watchdog_delete(thread->t_watchdog);
2224 thread->t_watchdog = NULL;
2228 * deconstruct service specific state created by ptlrpc_start_thread()
2230 if (svc->srv_done != NULL)
2231 svc->srv_done(thread);
2234 lu_context_fini(&env->le_ctx);
2238 CDEBUG(D_RPCTRACE, "service thread [ %p : %u ] %d exiting: rc %d\n",
2239 thread, thread->t_pid, thread->t_id, rc);
2241 cfs_spin_lock(&svc->srv_lock);
2242 if (thread_test_and_clear_flags(thread, SVC_STARTING))
2243 svc->srv_threads_starting--;
2245 if (thread_test_and_clear_flags(thread, SVC_RUNNING))
2246 /* must know immediately */
2247 svc->srv_threads_running--;
2250 thread_add_flags(thread, SVC_STOPPED);
2252 cfs_waitq_signal(&thread->t_ctl_waitq);
2253 cfs_spin_unlock(&svc->srv_lock);
2258 struct ptlrpc_hr_args {
2261 struct ptlrpc_hr_service *hrs;
2264 static int hrt_dont_sleep(struct ptlrpc_hr_thread *t,
2265 cfs_list_t *replies)
2269 cfs_spin_lock(&t->hrt_lock);
2270 cfs_list_splice_init(&t->hrt_queue, replies);
2271 result = cfs_test_bit(HRT_STOPPING, &t->hrt_flags) ||
2272 !cfs_list_empty(replies);
2273 cfs_spin_unlock(&t->hrt_lock);
2278 * Main body of "handle reply" function.
2279 * It processes acked reply states
2281 static int ptlrpc_hr_main(void *arg)
2283 struct ptlrpc_hr_args * hr_args = arg;
2284 struct ptlrpc_hr_service *hr = hr_args->hrs;
2285 struct ptlrpc_hr_thread *t = &hr->hr_threads[hr_args->thread_index];
2286 char threadname[20];
2287 CFS_LIST_HEAD(replies);
2289 snprintf(threadname, sizeof(threadname),
2290 "ptlrpc_hr_%d", hr_args->thread_index);
2292 cfs_daemonize_ctxt(threadname);
2293 #if defined(CONFIG_NUMA) && defined(HAVE_NODE_TO_CPUMASK)
2294 cfs_set_cpus_allowed(cfs_current(),
2295 node_to_cpumask(cpu_to_node(hr_args->cpu_index)));
2297 cfs_set_bit(HRT_RUNNING, &t->hrt_flags);
2298 cfs_waitq_signal(&t->hrt_wait);
2300 while (!cfs_test_bit(HRT_STOPPING, &t->hrt_flags)) {
2302 l_wait_condition(t->hrt_wait, hrt_dont_sleep(t, &replies));
2303 while (!cfs_list_empty(&replies)) {
2304 struct ptlrpc_reply_state *rs;
2306 rs = cfs_list_entry(replies.prev,
2307 struct ptlrpc_reply_state,
2309 cfs_list_del_init(&rs->rs_list);
2310 ptlrpc_handle_rs(rs);
2314 cfs_clear_bit(HRT_RUNNING, &t->hrt_flags);
2315 cfs_complete(&t->hrt_completion);
2320 static int ptlrpc_start_hr_thread(struct ptlrpc_hr_service *hr, int n, int cpu)
2322 struct ptlrpc_hr_thread *t = &hr->hr_threads[n];
2323 struct ptlrpc_hr_args args;
2327 args.thread_index = n;
2328 args.cpu_index = cpu;
2331 rc = cfs_create_thread(ptlrpc_hr_main, (void*)&args, CFS_DAEMON_FLAGS);
2333 cfs_complete(&t->hrt_completion);
2336 l_wait_condition(t->hrt_wait, cfs_test_bit(HRT_RUNNING, &t->hrt_flags));
2342 static void ptlrpc_stop_hr_thread(struct ptlrpc_hr_thread *t)
2346 cfs_set_bit(HRT_STOPPING, &t->hrt_flags);
2347 cfs_waitq_signal(&t->hrt_wait);
2348 cfs_wait_for_completion(&t->hrt_completion);
2353 static void ptlrpc_stop_hr_threads(struct ptlrpc_hr_service *hrs)
2358 for (n = 0; n < hrs->hr_n_threads; n++)
2359 ptlrpc_stop_hr_thread(&hrs->hr_threads[n]);
2364 static int ptlrpc_start_hr_threads(struct ptlrpc_hr_service *hr)
2367 int n, cpu, threads_started = 0;
2370 LASSERT(hr != NULL);
2371 LASSERT(hr->hr_n_threads > 0);
2373 for (n = 0, cpu = 0; n < hr->hr_n_threads; n++) {
2374 #if defined(CONFIG_SMP) && defined(HAVE_NODE_TO_CPUMASK)
2375 while(!cfs_cpu_online(cpu)) {
2377 if (cpu >= cfs_num_possible_cpus())
2381 rc = ptlrpc_start_hr_thread(hr, n, cpu);
2387 if (threads_started == 0) {
2388 CERROR("No reply handling threads started\n");
2391 if (threads_started < hr->hr_n_threads) {
2392 CWARN("Started only %d reply handling threads from %d\n",
2393 threads_started, hr->hr_n_threads);
2394 hr->hr_n_threads = threads_started;
2399 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
2400 struct ptlrpc_thread *thread)
2402 struct l_wait_info lwi = { 0 };
2405 CDEBUG(D_RPCTRACE, "Stopping thread [ %p : %u ]\n",
2406 thread, thread->t_pid);
2408 cfs_spin_lock(&svc->srv_lock);
2409 /* let the thread know that we would like it to stop asap */
2410 thread_add_flags(thread, SVC_STOPPING);
2411 cfs_spin_unlock(&svc->srv_lock);
2413 cfs_waitq_broadcast(&svc->srv_waitq);
2414 l_wait_event(thread->t_ctl_waitq,
2415 thread_is_stopped(thread), &lwi);
2417 cfs_spin_lock(&svc->srv_lock);
2418 cfs_list_del(&thread->t_link);
2419 cfs_spin_unlock(&svc->srv_lock);
2421 OBD_FREE_PTR(thread);
2426 * Stops all threads of a particular service \a svc
2428 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
2430 struct ptlrpc_thread *thread;
2433 cfs_spin_lock(&svc->srv_lock);
2434 while (!cfs_list_empty(&svc->srv_threads)) {
2435 thread = cfs_list_entry(svc->srv_threads.next,
2436 struct ptlrpc_thread, t_link);
2438 cfs_spin_unlock(&svc->srv_lock);
2439 ptlrpc_stop_thread(svc, thread);
2440 cfs_spin_lock(&svc->srv_lock);
2443 cfs_spin_unlock(&svc->srv_lock);
2447 int ptlrpc_start_threads(struct ptlrpc_service *svc)
2452 /* We require 2 threads min - see note in
2453 ptlrpc_server_handle_request */
2454 LASSERT(svc->srv_threads_min >= 2);
2455 for (i = 0; i < svc->srv_threads_min; i++) {
2456 rc = ptlrpc_start_thread(svc);
2457 /* We have enough threads, don't start more. b=15759 */
2458 if (rc == -EMFILE) {
2463 CERROR("cannot start %s thread #%d: rc %d\n",
2464 svc->srv_thread_name, i, rc);
2465 ptlrpc_stop_all_threads(svc);
2472 int ptlrpc_start_thread(struct ptlrpc_service *svc)
2474 struct l_wait_info lwi = { 0 };
2475 struct ptlrpc_svc_data d;
2476 struct ptlrpc_thread *thread;
2481 CDEBUG(D_RPCTRACE, "%s started %d min %d max %d running %d\n",
2482 svc->srv_name, svc->srv_threads_running, svc->srv_threads_min,
2483 svc->srv_threads_max, svc->srv_threads_running);
2485 if (unlikely(svc->srv_is_stopping))
2488 if (!ptlrpc_threads_increasable(svc) ||
2489 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
2490 svc->srv_threads_running == svc->srv_threads_min - 1))
2493 OBD_ALLOC_PTR(thread);
2496 cfs_waitq_init(&thread->t_ctl_waitq);
2498 cfs_spin_lock(&svc->srv_lock);
2499 if (!ptlrpc_threads_increasable(svc)) {
2500 cfs_spin_unlock(&svc->srv_lock);
2501 OBD_FREE_PTR(thread);
2505 svc->srv_threads_starting++;
2506 thread->t_id = svc->srv_threads_next_id++;
2507 thread_add_flags(thread, SVC_STARTING);
2508 thread->t_svc = svc;
2510 cfs_list_add(&thread->t_link, &svc->srv_threads);
2511 cfs_spin_unlock(&svc->srv_lock);
2513 sprintf(name, "%s_%02d", svc->srv_thread_name, thread->t_id);
2518 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
2520 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
2521 * just drop the VM and FILES in cfs_daemonize_ctxt() right away.
2523 rc = cfs_create_thread(ptlrpc_main, &d, CFS_DAEMON_FLAGS);
2525 CERROR("cannot start thread '%s': rc %d\n", name, rc);
2527 cfs_spin_lock(&svc->srv_lock);
2528 cfs_list_del(&thread->t_link);
2529 --svc->srv_threads_starting;
2530 cfs_spin_unlock(&svc->srv_lock);
2532 OBD_FREE(thread, sizeof(*thread));
2535 l_wait_event(thread->t_ctl_waitq,
2536 thread_is_running(thread) || thread_is_stopped(thread),
2539 rc = thread_is_stopped(thread) ? thread->t_id : 0;
2544 int ptlrpc_hr_init(void)
2547 int n_cpus = cfs_num_online_cpus();
2548 struct ptlrpc_hr_service *hr;
2553 LASSERT(ptlrpc_hr == NULL);
2555 size = offsetof(struct ptlrpc_hr_service, hr_threads[n_cpus]);
2556 OBD_ALLOC(hr, size);
2559 for (i = 0; i < n_cpus; i++) {
2560 struct ptlrpc_hr_thread *t = &hr->hr_threads[i];
2562 cfs_spin_lock_init(&t->hrt_lock);
2563 cfs_waitq_init(&t->hrt_wait);
2564 CFS_INIT_LIST_HEAD(&t->hrt_queue);
2565 cfs_init_completion(&t->hrt_completion);
2567 hr->hr_n_threads = n_cpus;
2571 rc = ptlrpc_start_hr_threads(hr);
2573 OBD_FREE(hr, hr->hr_size);
2579 void ptlrpc_hr_fini(void)
2581 if (ptlrpc_hr != NULL) {
2582 ptlrpc_stop_hr_threads(ptlrpc_hr);
2583 OBD_FREE(ptlrpc_hr, ptlrpc_hr->hr_size);
2588 #endif /* __KERNEL__ */
2591 * Wait until all already scheduled replies are processed.
2593 static void ptlrpc_wait_replies(struct ptlrpc_service *svc)
2597 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10),
2599 rc = l_wait_event(svc->srv_waitq, cfs_atomic_read(&svc-> \
2600 srv_n_difficult_replies) == 0,
2604 CWARN("Unexpectedly long timeout %p\n", svc);
2608 int ptlrpc_unregister_service(struct ptlrpc_service *service)
2611 struct l_wait_info lwi;
2613 struct ptlrpc_reply_state *rs, *t;
2614 struct ptlrpc_at_array *array = &service->srv_at_array;
2617 service->srv_is_stopping = 1;
2618 cfs_timer_disarm(&service->srv_at_timer);
2620 ptlrpc_stop_all_threads(service);
2621 LASSERT(cfs_list_empty(&service->srv_threads));
2623 cfs_spin_lock (&ptlrpc_all_services_lock);
2624 cfs_list_del_init (&service->srv_list);
2625 cfs_spin_unlock (&ptlrpc_all_services_lock);
2627 ptlrpc_lprocfs_unregister_service(service);
2629 /* All history will be culled when the next request buffer is
2631 service->srv_max_history_rqbds = 0;
2633 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
2635 rc = LNetClearLazyPortal(service->srv_req_portal);
2638 /* Unlink all the request buffers. This forces a 'final' event with
2639 * its 'unlink' flag set for each posted rqbd */
2640 cfs_list_for_each(tmp, &service->srv_active_rqbds) {
2641 struct ptlrpc_request_buffer_desc *rqbd =
2642 cfs_list_entry(tmp, struct ptlrpc_request_buffer_desc,
2645 rc = LNetMDUnlink(rqbd->rqbd_md_h);
2646 LASSERT (rc == 0 || rc == -ENOENT);
2649 /* Wait for the network to release any buffers it's currently
2652 cfs_spin_lock(&service->srv_lock);
2653 rc = service->srv_nrqbd_receiving;
2654 cfs_spin_unlock(&service->srv_lock);
2659 /* Network access will complete in finite time but the HUGE
2660 * timeout lets us CWARN for visibility of sluggish NALs */
2661 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
2662 cfs_time_seconds(1), NULL, NULL);
2663 rc = l_wait_event(service->srv_waitq,
2664 service->srv_nrqbd_receiving == 0,
2666 if (rc == -ETIMEDOUT)
2667 CWARN("Service %s waiting for request buffers\n",
2671 /* schedule all outstanding replies to terminate them */
2672 cfs_spin_lock(&service->srv_rs_lock);
2673 while (!cfs_list_empty(&service->srv_active_replies)) {
2674 struct ptlrpc_reply_state *rs =
2675 cfs_list_entry(service->srv_active_replies.next,
2676 struct ptlrpc_reply_state, rs_list);
2677 cfs_spin_lock(&rs->rs_lock);
2678 ptlrpc_schedule_difficult_reply(rs);
2679 cfs_spin_unlock(&rs->rs_lock);
2681 cfs_spin_unlock(&service->srv_rs_lock);
2683 /* purge the request queue. NB No new replies (rqbds all unlinked)
2684 * and no service threads, so I'm the only thread noodling the
2685 * request queue now */
2686 while (!cfs_list_empty(&service->srv_req_in_queue)) {
2687 struct ptlrpc_request *req =
2688 cfs_list_entry(service->srv_req_in_queue.next,
2689 struct ptlrpc_request,
2692 cfs_list_del(&req->rq_list);
2693 service->srv_n_queued_reqs--;
2694 service->srv_n_active_reqs++;
2695 ptlrpc_server_finish_request(service, req);
2697 while (ptlrpc_server_request_pending(service, 1)) {
2698 struct ptlrpc_request *req;
2700 req = ptlrpc_server_request_get(service, 1);
2701 cfs_list_del(&req->rq_list);
2702 service->srv_n_active_reqs++;
2703 ptlrpc_server_finish_request(service, req);
2705 LASSERT(service->srv_n_queued_reqs == 0);
2706 LASSERT(service->srv_n_active_reqs == 0);
2707 LASSERT(service->srv_n_history_rqbds == 0);
2708 LASSERT(cfs_list_empty(&service->srv_active_rqbds));
2710 /* Now free all the request buffers since nothing references them
2712 while (!cfs_list_empty(&service->srv_idle_rqbds)) {
2713 struct ptlrpc_request_buffer_desc *rqbd =
2714 cfs_list_entry(service->srv_idle_rqbds.next,
2715 struct ptlrpc_request_buffer_desc,
2718 ptlrpc_free_rqbd(rqbd);
2721 ptlrpc_wait_replies(service);
2723 cfs_list_for_each_entry_safe(rs, t, &service->srv_free_rs_list,
2725 cfs_list_del(&rs->rs_list);
2726 OBD_FREE_LARGE(rs, service->srv_max_reply_size);
2729 /* In case somebody rearmed this in the meantime */
2730 cfs_timer_disarm(&service->srv_at_timer);
2732 if (array->paa_reqs_array != NULL) {
2733 OBD_FREE(array->paa_reqs_array,
2734 sizeof(cfs_list_t) * array->paa_size);
2735 array->paa_reqs_array = NULL;
2738 if (array->paa_reqs_count != NULL) {
2739 OBD_FREE(array->paa_reqs_count,
2740 sizeof(__u32) * array->paa_size);
2741 array->paa_reqs_count= NULL;
2744 OBD_FREE_PTR(service);
2749 * Returns 0 if the service is healthy.
2751 * Right now, it just checks to make sure that requests aren't languishing
2752 * in the queue. We'll use this health check to govern whether a node needs
2753 * to be shot, so it's intentionally non-aggressive. */
2754 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
2756 struct ptlrpc_request *request;
2757 struct timeval right_now;
2763 cfs_gettimeofday(&right_now);
2765 cfs_spin_lock(&svc->srv_rq_lock);
2766 if (!ptlrpc_server_request_pending(svc, 1)) {
2767 cfs_spin_unlock(&svc->srv_rq_lock);
2771 /* How long has the next entry been waiting? */
2772 if (cfs_list_empty(&svc->srv_request_queue))
2773 request = cfs_list_entry(svc->srv_request_hpq.next,
2774 struct ptlrpc_request, rq_list);
2776 request = cfs_list_entry(svc->srv_request_queue.next,
2777 struct ptlrpc_request, rq_list);
2778 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
2779 cfs_spin_unlock(&svc->srv_rq_lock);
2781 if ((timediff / ONE_MILLION) > (AT_OFF ? obd_timeout * 3/2 :
2783 CERROR("%s: unhealthy - request has been waiting %lds\n",
2784 svc->srv_name, timediff / ONE_MILLION);