1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_RPC
39 #include <liblustre.h>
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <lustre_net.h>
44 #include <lu_object.h>
45 #include <lnet/types.h>
46 #include "ptlrpc_internal.h"
48 /* The following are visible and mutable through /sys/module/ptlrpc */
49 int test_req_buffer_pressure = 0;
50 CFS_MODULE_PARM(test_req_buffer_pressure, "i", int, 0444,
51 "set non-zero to put pressure on request buffer pools");
52 unsigned int at_min = 0;
53 CFS_MODULE_PARM(at_min, "i", int, 0644,
54 "Adaptive timeout minimum (sec)");
55 unsigned int at_max = 600;
56 EXPORT_SYMBOL(at_max);
57 CFS_MODULE_PARM(at_max, "i", int, 0644,
58 "Adaptive timeout maximum (sec)");
59 unsigned int at_history = 600;
60 CFS_MODULE_PARM(at_history, "i", int, 0644,
61 "Adaptive timeouts remember the slowest event that took place "
62 "within this period (sec)");
63 static int at_early_margin = 5;
64 CFS_MODULE_PARM(at_early_margin, "i", int, 0644,
65 "How soon before an RPC deadline to send an early reply");
66 static int at_extra = 30;
67 CFS_MODULE_PARM(at_extra, "i", int, 0644,
68 "How much extra time to give with each early reply");
72 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
74 static CFS_LIST_HEAD(ptlrpc_all_services);
75 spinlock_t ptlrpc_all_services_lock;
78 ptlrpc_alloc_request_buffer (int size)
82 if (size > SVC_BUF_VMALLOC_THRESHOLD)
83 OBD_VMALLOC(ptr, size);
91 ptlrpc_free_request_buffer (char *ptr, int size)
93 if (size > SVC_BUF_VMALLOC_THRESHOLD)
99 struct ptlrpc_request_buffer_desc *
100 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
102 struct ptlrpc_request_buffer_desc *rqbd;
108 rqbd->rqbd_service = svc;
109 rqbd->rqbd_refcount = 0;
110 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
111 rqbd->rqbd_cbid.cbid_arg = rqbd;
112 CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
113 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
115 if (rqbd->rqbd_buffer == NULL) {
120 spin_lock(&svc->srv_lock);
121 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
123 spin_unlock(&svc->srv_lock);
129 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
131 struct ptlrpc_service *svc = rqbd->rqbd_service;
133 LASSERT (rqbd->rqbd_refcount == 0);
134 LASSERT (list_empty(&rqbd->rqbd_reqs));
136 spin_lock(&svc->srv_lock);
137 list_del(&rqbd->rqbd_list);
139 spin_unlock(&svc->srv_lock);
141 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
146 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
148 struct ptlrpc_request_buffer_desc *rqbd;
151 CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
152 svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
153 svc->srv_nrqbd_receiving, svc->srv_nbufs);
154 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
155 rqbd = ptlrpc_alloc_rqbd(svc);
158 CERROR ("%s: Can't allocate request buffer\n",
163 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
171 ptlrpc_save_lock (struct ptlrpc_request *req,
172 struct lustre_handle *lock, int mode, int no_ack)
174 struct ptlrpc_reply_state *rs = req->rq_reply_state;
178 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
180 idx = rs->rs_nlocks++;
181 rs->rs_locks[idx] = *lock;
182 rs->rs_modes[idx] = mode;
183 rs->rs_difficult = 1;
184 rs->rs_no_ack = !!no_ack;
189 #define HRT_RUNNING 0
190 #define HRT_STOPPING 1
192 struct ptlrpc_hr_thread {
194 unsigned long hrt_flags;
195 cfs_waitq_t hrt_wait;
196 struct list_head hrt_queue;
197 struct completion hrt_completion;
200 struct ptlrpc_hr_service {
204 struct ptlrpc_hr_thread hr_threads[0];
208 struct list_head rsb_replies;
209 struct ptlrpc_service *rsb_svc;
210 unsigned int rsb_n_replies;
214 * A pointer to per-node reply handling service.
216 static struct ptlrpc_hr_service *ptlrpc_hr = NULL;
219 * maximum mumber of replies scheduled in one batch
221 #define MAX_SCHEDULED 256
224 * Initialize a reply batch.
228 static void rs_batch_init(struct rs_batch *b)
230 memset(b, 0, sizeof *b);
231 CFS_INIT_LIST_HEAD(&b->rsb_replies);
235 * Dispatch all replies accumulated in the batch to one from
236 * dedicated reply handing threads.
240 static void rs_batch_dispatch(struct rs_batch *b)
242 if (b->rsb_n_replies != 0) {
243 struct ptlrpc_hr_service *hr = ptlrpc_hr;
246 idx = hr->hr_index++;
247 if (hr->hr_index >= hr->hr_n_threads)
250 spin_lock(&hr->hr_threads[idx].hrt_lock);
251 list_splice_init(&b->rsb_replies,
252 &hr->hr_threads[idx].hrt_queue);
253 spin_unlock(&hr->hr_threads[idx].hrt_lock);
254 cfs_waitq_signal(&hr->hr_threads[idx].hrt_wait);
255 b->rsb_n_replies = 0;
260 * Add a reply to a batch.
261 * Add one reply object to a batch, schedule batched replies if overload.
266 static void rs_batch_add(struct rs_batch *b, struct ptlrpc_reply_state *rs)
268 struct ptlrpc_service *svc = rs->rs_service;
270 if (svc != b->rsb_svc || b->rsb_n_replies >= MAX_SCHEDULED) {
271 if (b->rsb_svc != NULL) {
272 rs_batch_dispatch(b);
273 spin_unlock(&b->rsb_svc->srv_lock);
275 spin_lock(&svc->srv_lock);
278 spin_lock(&rs->rs_lock);
279 rs->rs_scheduled_ever = 1;
280 if (rs->rs_scheduled == 0) {
281 list_move(&rs->rs_list, &b->rsb_replies);
282 rs->rs_scheduled = 1;
285 spin_unlock(&rs->rs_lock);
289 * Reply batch finalization.
290 * Dispatch remaining replies from the batch
291 * and release remaining spinlock.
295 static void rs_batch_fini(struct rs_batch *b)
297 if (b->rsb_svc != 0) {
298 rs_batch_dispatch(b);
299 spin_unlock(&b->rsb_svc->srv_lock);
303 #define DECLARE_RS_BATCH(b) struct rs_batch b
305 #else /* __KERNEL__ */
307 #define rs_batch_init(b) do{}while(0)
308 #define rs_batch_fini(b) do{}while(0)
309 #define rs_batch_add(b, r) ptlrpc_schedule_difficult_reply(r)
310 #define DECLARE_RS_BATCH(b)
312 #endif /* __KERNEL__ */
314 void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs)
317 struct ptlrpc_hr_service *hr = ptlrpc_hr;
321 LASSERT(list_empty(&rs->rs_list));
323 idx = hr->hr_index++;
324 if (hr->hr_index >= hr->hr_n_threads)
326 spin_lock(&hr->hr_threads[idx].hrt_lock);
327 list_add_tail(&rs->rs_list, &hr->hr_threads[idx].hrt_queue);
328 spin_unlock(&hr->hr_threads[idx].hrt_lock);
329 cfs_waitq_signal(&hr->hr_threads[idx].hrt_wait);
332 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_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 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 spin_lock(&exp->exp_uncommitted_replies_lock);
369 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 list_del_init(&rs->rs_obd_list);
376 rs_batch_add(&batch, rs);
379 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 spin_lock(&svc->srv_lock);
394 if (list_empty (&svc->srv_idle_rqbds)) {
395 spin_unlock(&svc->srv_lock);
399 rqbd = list_entry(svc->srv_idle_rqbds.next,
400 struct ptlrpc_request_buffer_desc,
402 list_del (&rqbd->rqbd_list);
404 /* assume we will post successfully */
405 svc->srv_nrqbd_receiving++;
406 list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
408 spin_unlock(&svc->srv_lock);
410 rc = ptlrpc_register_rqbd(rqbd);
417 spin_lock(&svc->srv_lock);
419 svc->srv_nrqbd_receiving--;
420 list_del(&rqbd->rqbd_list);
421 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 spin_unlock(&svc->srv_lock);
431 struct ptlrpc_service *ptlrpc_init_svc_conf(struct ptlrpc_service_conf *c,
432 svc_handler_t h, char *name,
433 struct proc_dir_entry *proc_entry,
434 svcreq_printfn_t prntfn,
437 return ptlrpc_init_svc(c->psc_nbufs, c->psc_bufsize,
438 c->psc_max_req_size, c->psc_max_reply_size,
439 c->psc_req_portal, c->psc_rep_portal,
440 c->psc_watchdog_factor,
442 prntfn, c->psc_min_threads, c->psc_max_threads,
443 threadname, c->psc_ctx_tags, NULL);
445 EXPORT_SYMBOL(ptlrpc_init_svc_conf);
447 static void ptlrpc_at_timer(unsigned long castmeharder)
449 struct ptlrpc_service *svc = (struct ptlrpc_service *)castmeharder;
450 svc->srv_at_check = 1;
451 svc->srv_at_checktime = cfs_time_current();
452 cfs_waitq_signal(&svc->srv_waitq);
455 /* @threadname should be 11 characters or less - 3 will be added on */
456 struct ptlrpc_service *
457 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
458 int req_portal, int rep_portal, int watchdog_factor,
459 svc_handler_t handler, char *name,
460 cfs_proc_dir_entry_t *proc_entry,
461 svcreq_printfn_t svcreq_printfn,
462 int min_threads, int max_threads,
463 char *threadname, __u32 ctx_tags,
464 svc_hpreq_handler_t hp_handler)
467 struct ptlrpc_at_array *array;
468 struct ptlrpc_service *service;
469 unsigned int size, index;
473 LASSERT (bufsize >= max_req_size + SPTLRPC_MAX_PAYLOAD);
474 LASSERT (ctx_tags != 0);
476 OBD_ALLOC_PTR(service);
480 /* First initialise enough for early teardown */
482 service->srv_name = name;
483 spin_lock_init(&service->srv_lock);
484 CFS_INIT_LIST_HEAD(&service->srv_threads);
485 cfs_waitq_init(&service->srv_waitq);
487 service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
488 service->srv_max_req_size = max_req_size + SPTLRPC_MAX_PAYLOAD;
489 service->srv_buf_size = bufsize;
490 service->srv_rep_portal = rep_portal;
491 service->srv_req_portal = req_portal;
492 service->srv_watchdog_factor = watchdog_factor;
493 service->srv_handler = handler;
494 service->srv_request_history_print_fn = svcreq_printfn;
495 service->srv_request_seq = 1; /* valid seq #s start at 1 */
496 service->srv_request_max_cull_seq = 0;
497 service->srv_threads_min = min_threads;
498 service->srv_threads_max = max_threads;
499 service->srv_thread_name = threadname;
500 service->srv_ctx_tags = ctx_tags;
501 service->srv_hpreq_handler = hp_handler;
502 service->srv_hpreq_ratio = PTLRPC_SVC_HP_RATIO;
503 service->srv_hpreq_count = 0;
504 service->srv_n_hpreq = 0;
506 rc = LNetSetLazyPortal(service->srv_req_portal);
509 CFS_INIT_LIST_HEAD(&service->srv_request_queue);
510 CFS_INIT_LIST_HEAD(&service->srv_request_hpq);
511 CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
512 CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
513 CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
514 CFS_INIT_LIST_HEAD(&service->srv_request_history);
515 CFS_INIT_LIST_HEAD(&service->srv_active_replies);
517 CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
519 CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
520 cfs_waitq_init(&service->srv_free_rs_waitq);
521 atomic_set(&service->srv_n_difficult_replies, 0);
523 spin_lock_init(&service->srv_at_lock);
524 CFS_INIT_LIST_HEAD(&service->srv_req_in_queue);
526 array = &service->srv_at_array;
527 size = at_est2timeout(at_max);
528 array->paa_size = size;
529 array->paa_count = 0;
530 array->paa_deadline = -1;
532 /* allocate memory for srv_at_array (ptlrpc_at_array) */
533 OBD_ALLOC(array->paa_reqs_array, sizeof(struct list_head) * size);
534 if (array->paa_reqs_array == NULL)
537 for (index = 0; index < size; index++)
538 CFS_INIT_LIST_HEAD(&array->paa_reqs_array[index]);
540 OBD_ALLOC(array->paa_reqs_count, sizeof(__u32) * size);
541 if (array->paa_reqs_count == NULL)
544 cfs_timer_init(&service->srv_at_timer, ptlrpc_at_timer, service);
545 /* At SOW, service time should be quick; 10s seems generous. If client
546 timeout is less than this, we'll be sending an early reply. */
547 at_init(&service->srv_at_estimate, 10, 0);
549 spin_lock (&ptlrpc_all_services_lock);
550 list_add (&service->srv_list, &ptlrpc_all_services);
551 spin_unlock (&ptlrpc_all_services_lock);
553 /* Now allocate the request buffers */
554 rc = ptlrpc_grow_req_bufs(service);
555 /* We shouldn't be under memory pressure at startup, so
556 * fail if we can't post all our buffers at this time. */
560 /* Now allocate pool of reply buffers */
561 /* Increase max reply size to next power of two */
562 service->srv_max_reply_size = 1;
563 while (service->srv_max_reply_size <
564 max_reply_size + SPTLRPC_MAX_PAYLOAD)
565 service->srv_max_reply_size <<= 1;
567 if (proc_entry != NULL)
568 ptlrpc_lprocfs_register_service(proc_entry, service);
570 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
571 service->srv_name, service->srv_req_portal);
575 ptlrpc_unregister_service(service);
580 * to actually free the request, must be called without holding svc_lock.
581 * note it's caller's responsibility to unlink req->rq_list.
583 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
585 LASSERT(atomic_read(&req->rq_refcount) == 0);
586 LASSERT(list_empty(&req->rq_timed_list));
588 /* DEBUG_REQ() assumes the reply state of a request with a valid
589 * ref will not be destroyed until that reference is dropped. */
590 ptlrpc_req_drop_rs(req);
592 sptlrpc_svc_ctx_decref(req);
594 if (req != &req->rq_rqbd->rqbd_req) {
595 /* NB request buffers use an embedded
596 * req if the incoming req unlinked the
597 * MD; this isn't one of them! */
598 OBD_FREE(req, sizeof(*req));
603 * drop a reference count of the request. if it reaches 0, we either
604 * put it into history list, or free it immediately.
606 static void ptlrpc_server_drop_request(struct ptlrpc_request *req)
608 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
609 struct ptlrpc_service *svc = rqbd->rqbd_service;
611 struct list_head *tmp;
612 struct list_head *nxt;
614 if (!atomic_dec_and_test(&req->rq_refcount))
617 spin_lock(&svc->srv_lock);
619 svc->srv_n_active_reqs--;
620 list_add(&req->rq_list, &rqbd->rqbd_reqs);
622 refcount = --(rqbd->rqbd_refcount);
624 /* request buffer is now idle: add to history */
625 list_del(&rqbd->rqbd_list);
626 list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
627 svc->srv_n_history_rqbds++;
629 /* cull some history?
630 * I expect only about 1 or 2 rqbds need to be recycled here */
631 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
632 rqbd = list_entry(svc->srv_history_rqbds.next,
633 struct ptlrpc_request_buffer_desc,
636 list_del(&rqbd->rqbd_list);
637 svc->srv_n_history_rqbds--;
639 /* remove rqbd's reqs from svc's req history while
640 * I've got the service lock */
641 list_for_each(tmp, &rqbd->rqbd_reqs) {
642 req = list_entry(tmp, struct ptlrpc_request,
644 /* Track the highest culled req seq */
645 if (req->rq_history_seq >
646 svc->srv_request_max_cull_seq)
647 svc->srv_request_max_cull_seq =
649 list_del(&req->rq_history_list);
652 spin_unlock(&svc->srv_lock);
654 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
655 req = list_entry(rqbd->rqbd_reqs.next,
656 struct ptlrpc_request,
658 list_del(&req->rq_list);
659 ptlrpc_server_free_request(req);
662 spin_lock(&svc->srv_lock);
664 * now all reqs including the embedded req has been
665 * disposed, schedule request buffer for re-use.
667 LASSERT(atomic_read(&rqbd->rqbd_req.rq_refcount) == 0);
668 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
671 spin_unlock(&svc->srv_lock);
672 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
673 /* If we are low on memory, we are not interested in history */
674 list_del(&req->rq_list);
675 list_del_init(&req->rq_history_list);
676 spin_unlock(&svc->srv_lock);
678 ptlrpc_server_free_request(req);
680 spin_unlock(&svc->srv_lock);
685 * to finish a request: stop sending more early replies, and release
686 * the request. should be called after we finished handling the request.
688 static void ptlrpc_server_finish_request(struct ptlrpc_request *req)
690 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
692 if (req->rq_export) {
693 class_export_put(req->rq_export);
694 req->rq_export = NULL;
697 if (req->rq_phase != RQ_PHASE_NEW) /* incorrect message magic */
698 DEBUG_REQ(D_INFO, req, "free req");
700 spin_lock(&svc->srv_at_lock);
701 req->rq_sent_final = 1;
702 list_del_init(&req->rq_timed_list);
703 if (req->rq_at_linked) {
704 struct ptlrpc_at_array *array = &svc->srv_at_array;
705 __u32 index = req->rq_at_index;
707 req->rq_at_linked = 0;
708 array->paa_reqs_count[index]--;
711 spin_unlock(&svc->srv_at_lock);
713 ptlrpc_server_drop_request(req);
716 /* This function makes sure dead exports are evicted in a timely manner.
717 This function is only called when some export receives a message (i.e.,
718 the network is up.) */
719 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
721 struct obd_export *oldest_exp;
722 time_t oldest_time, new_time;
728 /* Compensate for slow machines, etc, by faking our request time
729 into the future. Although this can break the strict time-ordering
730 of the list, we can be really lazy here - we don't have to evict
731 at the exact right moment. Eventually, all silent exports
732 will make it to the top of the list. */
734 /* Do not pay attention on 1sec or smaller renewals. */
735 new_time = cfs_time_current_sec() + extra_delay;
736 if (exp->exp_last_request_time + 1 /*second */ >= new_time)
739 exp->exp_last_request_time = new_time;
740 CDEBUG(D_HA, "updating export %s at "CFS_TIME_T" exp %p\n",
741 exp->exp_client_uuid.uuid,
742 exp->exp_last_request_time, exp);
744 /* exports may get disconnected from the chain even though the
745 export has references, so we must keep the spin lock while
746 manipulating the lists */
747 spin_lock(&exp->exp_obd->obd_dev_lock);
749 if (list_empty(&exp->exp_obd_chain_timed)) {
750 /* this one is not timed */
751 spin_unlock(&exp->exp_obd->obd_dev_lock);
755 list_move_tail(&exp->exp_obd_chain_timed,
756 &exp->exp_obd->obd_exports_timed);
758 oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
759 struct obd_export, exp_obd_chain_timed);
760 oldest_time = oldest_exp->exp_last_request_time;
761 spin_unlock(&exp->exp_obd->obd_dev_lock);
763 if (exp->exp_obd->obd_recovering) {
764 /* be nice to everyone during recovery */
769 /* Note - racing to start/reset the obd_eviction timer is safe */
770 if (exp->exp_obd->obd_eviction_timer == 0) {
771 /* Check if the oldest entry is expired. */
772 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
774 /* We need a second timer, in case the net was down and
775 * it just came back. Since the pinger may skip every
776 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
777 * we better wait for 3. */
778 exp->exp_obd->obd_eviction_timer =
779 cfs_time_current_sec() + 3 * PING_INTERVAL;
780 CDEBUG(D_HA, "%s: Think about evicting %s from "CFS_TIME_T"\n",
781 exp->exp_obd->obd_name, obd_export_nid2str(exp),
785 if (cfs_time_current_sec() >
786 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
787 /* The evictor won't evict anyone who we've heard from
788 * recently, so we don't have to check before we start
790 if (!ping_evictor_wake(exp))
791 exp->exp_obd->obd_eviction_timer = 0;
798 static int ptlrpc_check_req(struct ptlrpc_request *req)
800 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
801 req->rq_export->exp_conn_cnt)) {
802 DEBUG_REQ(D_ERROR, req,
803 "DROPPING req from old connection %d < %d",
804 lustre_msg_get_conn_cnt(req->rq_reqmsg),
805 req->rq_export->exp_conn_cnt);
808 if (unlikely(req->rq_export->exp_obd &&
809 req->rq_export->exp_obd->obd_fail)) {
810 /* Failing over, don't handle any more reqs, send
811 error response instead. */
812 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
813 req, req->rq_export->exp_obd->obd_name);
814 req->rq_status = -ENODEV;
822 static void ptlrpc_at_set_timer(struct ptlrpc_service *svc)
824 struct ptlrpc_at_array *array = &svc->srv_at_array;
827 spin_lock(&svc->srv_at_lock);
828 if (array->paa_count == 0) {
829 cfs_timer_disarm(&svc->srv_at_timer);
830 spin_unlock(&svc->srv_at_lock);
834 /* Set timer for closest deadline */
835 next = (__s32)(array->paa_deadline - cfs_time_current_sec() -
838 ptlrpc_at_timer((unsigned long)svc);
840 cfs_timer_arm(&svc->srv_at_timer, cfs_time_shift(next));
841 spin_unlock(&svc->srv_at_lock);
842 CDEBUG(D_INFO, "armed %s at %+ds\n", svc->srv_name, next);
845 /* Add rpc to early reply check list */
846 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
848 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
849 struct ptlrpc_request *rq = NULL;
850 struct ptlrpc_at_array *array = &svc->srv_at_array;
857 if (req->rq_no_reply)
860 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
863 spin_lock(&svc->srv_at_lock);
865 if (unlikely(req->rq_sent_final)) {
866 spin_unlock(&svc->srv_at_lock);
870 LASSERT(list_empty(&req->rq_timed_list));
872 index = req->rq_deadline % array->paa_size;
873 if (array->paa_reqs_count[index] > 0) {
874 /* latest rpcs will have the latest deadlines in the list,
875 * so search backward. */
876 list_for_each_entry_reverse(rq, &array->paa_reqs_array[index],
878 if (req->rq_deadline >= rq->rq_deadline) {
879 list_add(&req->rq_timed_list,
886 /* Add the request at the head of the list */
887 if (list_empty(&req->rq_timed_list))
888 list_add(&req->rq_timed_list, &array->paa_reqs_array[index]);
890 req->rq_at_linked = 1;
891 req->rq_at_index = index;
892 array->paa_reqs_count[index]++;
894 if (array->paa_count == 1 || array->paa_deadline > req->rq_deadline) {
895 array->paa_deadline = req->rq_deadline;
898 spin_unlock(&svc->srv_at_lock);
901 ptlrpc_at_set_timer(svc);
906 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req,
909 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
910 struct ptlrpc_request *reqcopy;
911 struct lustre_msg *reqmsg;
912 cfs_duration_t olddl = req->rq_deadline - cfs_time_current_sec();
917 /* deadline is when the client expects us to reply, margin is the
918 difference between clients' and servers' expectations */
919 DEBUG_REQ(D_ADAPTTO, req,
920 "%ssending early reply (deadline %+lds, margin %+lds) for "
921 "%d+%d", AT_OFF ? "AT off - not " : "",
922 olddl, olddl - at_get(&svc->srv_at_estimate),
923 at_get(&svc->srv_at_estimate), extra_time);
929 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), "
930 "not sending early reply. Consider increasing "
931 "at_early_margin (%d)?", olddl, at_early_margin);
933 /* Return an error so we're not re-added to the timed list. */
937 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
938 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, "
939 "but no AT support");
943 if (req->rq_export && req->rq_export->exp_in_recovery) {
944 /* don't increase server estimates during recovery, and give
945 clients the full recovery time. */
946 newdl = cfs_time_current_sec() +
947 req->rq_export->exp_obd->obd_recovery_timeout;
950 /* Fake our processing time into the future to ask the
951 clients for some extra amount of time */
952 extra_time += cfs_time_current_sec() -
953 req->rq_arrival_time.tv_sec;
954 at_add(&svc->srv_at_estimate, extra_time);
956 newdl = req->rq_arrival_time.tv_sec +
957 at_get(&svc->srv_at_estimate);
959 if (req->rq_deadline >= newdl) {
960 /* We're not adding any time, no need to send an early reply
961 (e.g. maybe at adaptive_max) */
962 DEBUG_REQ(D_WARNING, req, "Couldn't add any time ("
963 CFS_DURATION_T"/"CFS_DURATION_T"), "
964 "not sending early reply\n", olddl,
965 cfs_time_sub(newdl, cfs_time_current_sec()));
969 OBD_ALLOC(reqcopy, sizeof *reqcopy);
972 OBD_ALLOC(reqmsg, req->rq_reqlen);
974 OBD_FREE(reqcopy, sizeof *reqcopy);
979 reqcopy->rq_reply_state = NULL;
980 reqcopy->rq_rep_swab_mask = 0;
981 reqcopy->rq_pack_bulk = 0;
982 reqcopy->rq_pack_udesc = 0;
983 reqcopy->rq_packed_final = 0;
984 sptlrpc_svc_ctx_addref(reqcopy);
985 /* We only need the reqmsg for the magic */
986 reqcopy->rq_reqmsg = reqmsg;
987 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
989 if (req->rq_sent_final) {
990 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, "
991 "abort sending early reply\n");
996 reqcopy->rq_export = class_conn2export(
997 lustre_msg_get_handle(reqcopy->rq_reqmsg));
998 if (reqcopy->rq_export == NULL)
999 GOTO(out, rc = -ENODEV);
1002 class_export_rpc_get(reqcopy->rq_export);
1003 if (reqcopy->rq_export->exp_obd &&
1004 reqcopy->rq_export->exp_obd->obd_fail)
1005 GOTO(out_put, rc = -ENODEV);
1007 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
1011 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
1014 /* Adjust our own deadline to what we told the client */
1015 req->rq_deadline = newdl;
1016 req->rq_early_count++; /* number sent, server side */
1018 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
1021 /* Free the (early) reply state from lustre_pack_reply.
1022 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
1023 ptlrpc_req_drop_rs(reqcopy);
1026 class_export_rpc_put(reqcopy->rq_export);
1027 class_export_put(reqcopy->rq_export);
1029 sptlrpc_svc_ctx_decref(reqcopy);
1030 OBD_FREE(reqmsg, req->rq_reqlen);
1031 OBD_FREE(reqcopy, sizeof *reqcopy);
1035 /* Send early replies to everybody expiring within at_early_margin
1036 asking for at_extra time */
1037 static int ptlrpc_at_check_timed(struct ptlrpc_service *svc)
1039 struct ptlrpc_request *rq, *n;
1040 struct list_head work_list;
1041 struct ptlrpc_at_array *array = &svc->srv_at_array;
1044 time_t now = cfs_time_current_sec();
1045 cfs_duration_t delay;
1046 int first, counter = 0;
1049 spin_lock(&svc->srv_at_lock);
1050 if (svc->srv_at_check == 0) {
1051 spin_unlock(&svc->srv_at_lock);
1054 delay = cfs_time_sub(cfs_time_current(), svc->srv_at_checktime);
1055 svc->srv_at_check = 0;
1057 if (array->paa_count == 0) {
1058 spin_unlock(&svc->srv_at_lock);
1062 /* The timer went off, but maybe the nearest rpc already completed. */
1063 first = array->paa_deadline - now;
1064 if (first > at_early_margin) {
1065 /* We've still got plenty of time. Reset the timer. */
1066 spin_unlock(&svc->srv_at_lock);
1067 ptlrpc_at_set_timer(svc);
1071 /* We're close to a timeout, and we don't know how much longer the
1072 server will take. Send early replies to everyone expiring soon. */
1073 CFS_INIT_LIST_HEAD(&work_list);
1075 index = array->paa_deadline % array->paa_size;
1076 count = array->paa_count;
1078 count -= array->paa_reqs_count[index];
1079 list_for_each_entry_safe(rq, n, &array->paa_reqs_array[index],
1081 if (rq->rq_deadline <= now + at_early_margin) {
1082 list_move(&rq->rq_timed_list, &work_list);
1084 array->paa_reqs_count[index]--;
1086 rq->rq_at_linked = 0;
1090 /* update the earliest deadline */
1091 if (deadline == -1 || rq->rq_deadline < deadline)
1092 deadline = rq->rq_deadline;
1097 if (++index >= array->paa_size)
1100 array->paa_deadline = deadline;
1101 spin_unlock(&svc->srv_at_lock);
1103 /* we have a new earliest deadline, restart the timer */
1104 ptlrpc_at_set_timer(svc);
1106 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
1107 "replies\n", first, at_extra, counter);
1109 /* We're already past request deadlines before we even get a
1110 chance to send early replies */
1111 LCONSOLE_WARN("%s: This server is not able to keep up with "
1112 "request traffic (cpu-bound).\n", svc->srv_name);
1113 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
1114 "delay="CFS_DURATION_T"(jiff)\n",
1115 counter, svc->srv_n_queued_reqs, svc->srv_n_active_reqs,
1116 at_get(&svc->srv_at_estimate), delay);
1119 /* ptlrpc_server_finish_request may delete an entry out of
1121 spin_lock(&svc->srv_at_lock);
1122 while (!list_empty(&work_list)) {
1123 rq = list_entry(work_list.next, struct ptlrpc_request,
1125 list_del_init(&rq->rq_timed_list);
1126 /* if the entry is still in the worklist, it hasn't been
1127 deleted, and is safe to take a ref to keep the req around */
1128 atomic_inc(&rq->rq_refcount);
1129 spin_unlock(&svc->srv_at_lock);
1131 if (ptlrpc_at_send_early_reply(rq, at_extra) == 0)
1132 ptlrpc_at_add_timed(rq);
1134 ptlrpc_server_drop_request(rq);
1135 spin_lock(&svc->srv_at_lock);
1137 spin_unlock(&svc->srv_at_lock);
1143 * Put the request to the export list if the request may become
1144 * a high priority one.
1146 static int ptlrpc_hpreq_init(struct ptlrpc_service *svc,
1147 struct ptlrpc_request *req)
1152 if (svc->srv_hpreq_handler) {
1153 rc = svc->srv_hpreq_handler(req);
1157 if (req->rq_export && req->rq_ops) {
1158 spin_lock(&req->rq_export->exp_lock);
1159 list_add(&req->rq_exp_list, &req->rq_export->exp_queued_rpc);
1160 spin_unlock(&req->rq_export->exp_lock);
1166 /** Remove the request from the export list. */
1167 static void ptlrpc_hpreq_fini(struct ptlrpc_request *req)
1170 if (req->rq_export && req->rq_ops) {
1171 spin_lock(&req->rq_export->exp_lock);
1172 list_del_init(&req->rq_exp_list);
1173 spin_unlock(&req->rq_export->exp_lock);
1179 * Make the request a high priority one.
1181 * All the high priority requests are queued in a separate FIFO
1182 * ptlrpc_service::srv_request_hpq list which is parallel to
1183 * ptlrpc_service::srv_request_queue list but has a higher priority
1186 * \see ptlrpc_server_handle_request().
1188 static void ptlrpc_hpreq_reorder_nolock(struct ptlrpc_service *svc,
1189 struct ptlrpc_request *req)
1192 LASSERT(svc != NULL);
1193 spin_lock(&req->rq_lock);
1194 if (req->rq_hp == 0) {
1195 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1197 /* Add to the high priority queue. */
1198 list_move_tail(&req->rq_list, &svc->srv_request_hpq);
1200 if (opc != OBD_PING)
1201 DEBUG_REQ(D_NET, req, "high priority req");
1203 spin_unlock(&req->rq_lock);
1207 void ptlrpc_hpreq_reorder(struct ptlrpc_request *req)
1209 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
1212 spin_lock(&svc->srv_lock);
1213 /* It may happen that the request is already taken for the processing
1214 * but still in the export list, do not re-add it into the HP list. */
1215 if (req->rq_phase == RQ_PHASE_NEW)
1216 ptlrpc_hpreq_reorder_nolock(svc, req);
1217 spin_unlock(&svc->srv_lock);
1221 /** Check if the request if a high priority one. */
1222 static int ptlrpc_server_hpreq_check(struct ptlrpc_request *req)
1227 /* Check by request opc. */
1228 opc = lustre_msg_get_opc(req->rq_reqmsg);
1229 if (opc == OBD_PING)
1232 /* Perform request specific check. */
1233 if (req->rq_ops && req->rq_ops->hpreq_check)
1234 rc = req->rq_ops->hpreq_check(req);
1238 /** Check if a request is a high priority one. */
1239 static int ptlrpc_server_request_add(struct ptlrpc_service *svc,
1240 struct ptlrpc_request *req)
1245 rc = ptlrpc_server_hpreq_check(req);
1249 spin_lock(&svc->srv_lock);
1250 /* Before inserting the request into the queue, check if it is not
1251 * inserted yet, or even already handled -- it may happen due to
1252 * a racing ldlm_server_blocking_ast(). */
1253 if (req->rq_phase == RQ_PHASE_NEW && list_empty(&req->rq_list)) {
1255 ptlrpc_hpreq_reorder_nolock(svc, req);
1257 list_add_tail(&req->rq_list, &svc->srv_request_queue);
1259 spin_unlock(&svc->srv_lock);
1264 /* Only allow normal priority requests on a service that has a high-priority
1265 * queue if forced (i.e. cleanup), if there are other high priority requests
1266 * already being processed (i.e. those threads can service more high-priority
1267 * requests), or if there are enough idle threads that a later thread can do
1268 * a high priority request. */
1269 static int ptlrpc_server_allow_normal(struct ptlrpc_service *svc, int force)
1271 return force || !svc->srv_hpreq_handler || svc->srv_n_hpreq > 0 ||
1272 svc->srv_n_active_reqs < svc->srv_threads_running - 2;
1275 static struct ptlrpc_request *
1276 ptlrpc_server_request_get(struct ptlrpc_service *svc, int force)
1278 struct ptlrpc_request *req = NULL;
1281 if (ptlrpc_server_allow_normal(svc, force) &&
1282 !list_empty(&svc->srv_request_queue) &&
1283 (list_empty(&svc->srv_request_hpq) ||
1284 svc->srv_hpreq_count >= svc->srv_hpreq_ratio)) {
1285 req = list_entry(svc->srv_request_queue.next,
1286 struct ptlrpc_request, rq_list);
1287 svc->srv_hpreq_count = 0;
1288 } else if (!list_empty(&svc->srv_request_hpq)) {
1289 req = list_entry(svc->srv_request_hpq.next,
1290 struct ptlrpc_request, rq_list);
1291 svc->srv_hpreq_count++;
1296 static int ptlrpc_server_request_pending(struct ptlrpc_service *svc, int force)
1298 return ((ptlrpc_server_allow_normal(svc, force) &&
1299 !list_empty(&svc->srv_request_queue)) ||
1300 !list_empty(&svc->srv_request_hpq));
1303 /* Handle freshly incoming reqs, add to timed early reply list,
1304 pass on to regular request queue */
1306 ptlrpc_server_handle_req_in(struct ptlrpc_service *svc)
1308 struct ptlrpc_request *req;
1315 spin_lock(&svc->srv_lock);
1316 if (list_empty(&svc->srv_req_in_queue)) {
1317 spin_unlock(&svc->srv_lock);
1321 req = list_entry(svc->srv_req_in_queue.next,
1322 struct ptlrpc_request, rq_list);
1323 list_del_init (&req->rq_list);
1324 /* Consider this still a "queued" request as far as stats are
1326 spin_unlock(&svc->srv_lock);
1328 /* go through security check/transform */
1329 rc = sptlrpc_svc_unwrap_request(req);
1333 case SECSVC_COMPLETE:
1334 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
1342 /* Clear request swab mask; this is a new request */
1343 req->rq_req_swab_mask = 0;
1345 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
1347 CERROR("error unpacking request: ptl %d from %s x"LPU64"\n",
1348 svc->srv_req_portal, libcfs_id2str(req->rq_peer),
1353 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
1355 CERROR ("error unpacking ptlrpc body: ptl %d from %s x"
1356 LPU64"\n", svc->srv_req_portal,
1357 libcfs_id2str(req->rq_peer), req->rq_xid);
1362 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
1363 CERROR("wrong packet type received (type=%u) from %s\n",
1364 lustre_msg_get_type(req->rq_reqmsg),
1365 libcfs_id2str(req->rq_peer));
1369 switch(lustre_msg_get_opc(req->rq_reqmsg)) {
1372 req->rq_bulk_write = 1;
1376 req->rq_bulk_read = 1;
1380 CDEBUG(D_NET, "got req "LPU64"\n", req->rq_xid);
1382 req->rq_export = class_conn2export(
1383 lustre_msg_get_handle(req->rq_reqmsg));
1384 if (req->rq_export) {
1385 rc = ptlrpc_check_req(req);
1387 rc = sptlrpc_target_export_check(req->rq_export, req);
1389 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
1390 "illegal security flavor,");
1395 ptlrpc_update_export_timer(req->rq_export, 0);
1398 /* req_in handling should/must be fast */
1399 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
1400 DEBUG_REQ(D_WARNING, req, "Slow req_in handling "CFS_DURATION_T"s",
1401 cfs_time_sub(cfs_time_current_sec(),
1402 req->rq_arrival_time.tv_sec));
1404 /* Set rpc server deadline and add it to the timed list */
1405 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1406 MSGHDR_AT_SUPPORT) ?
1407 /* The max time the client expects us to take */
1408 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1409 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1410 if (unlikely(deadline == 0)) {
1411 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1415 ptlrpc_at_add_timed(req);
1416 rc = ptlrpc_hpreq_init(svc, req);
1420 /* Move it over to the request processing queue */
1421 rc = ptlrpc_server_request_add(svc, req);
1424 cfs_waitq_signal(&svc->srv_waitq);
1428 spin_lock(&svc->srv_lock);
1429 svc->srv_n_queued_reqs--;
1430 svc->srv_n_active_reqs++;
1431 spin_unlock(&svc->srv_lock);
1432 ptlrpc_server_finish_request(req);
1438 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
1439 struct ptlrpc_thread *thread)
1441 struct obd_export *export = NULL;
1442 struct ptlrpc_request *request;
1443 struct timeval work_start;
1444 struct timeval work_end;
1452 spin_lock(&svc->srv_lock);
1453 if (unlikely(!ptlrpc_server_request_pending(svc, 0) ||
1456 /* !@%$# liblustre only has 1 thread */
1457 atomic_read(&svc->srv_n_difficult_replies) != 0 &&
1459 svc->srv_n_active_reqs >= (svc->srv_threads_running - 1)))) {
1460 /* Don't handle regular requests in the last thread, in order * re
1461 * to handle difficult replies (which might block other threads)
1462 * as well as handle any incoming reqs, early replies, etc.
1463 * That means we always need at least 2 service threads. */
1464 spin_unlock(&svc->srv_lock);
1468 request = ptlrpc_server_request_get(svc, 0);
1469 if (request == NULL) {
1470 spin_unlock(&svc->srv_lock);
1474 opc = lustre_msg_get_opc(request->rq_reqmsg);
1475 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT))
1476 fail_opc = OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT;
1477 else if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
1478 fail_opc = OBD_FAIL_PTLRPC_HPREQ_TIMEOUT;
1480 if (unlikely(fail_opc)) {
1481 if (request->rq_export && request->rq_ops) {
1482 spin_unlock(&svc->srv_lock);
1483 OBD_FAIL_TIMEOUT(fail_opc, 4);
1484 spin_lock(&svc->srv_lock);
1485 request = ptlrpc_server_request_get(svc, 0);
1486 if (request == NULL) {
1487 spin_unlock(&svc->srv_lock);
1490 LASSERT(ptlrpc_server_request_pending(svc, 0));
1494 list_del_init(&request->rq_list);
1495 svc->srv_n_queued_reqs--;
1496 svc->srv_n_active_reqs++;
1500 /* The phase is changed under the lock here because we need to know
1501 * the request is under processing (see ptlrpc_hpreq_reorder()). */
1502 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
1503 spin_unlock(&svc->srv_lock);
1505 ptlrpc_hpreq_fini(request);
1507 if(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
1508 libcfs_debug_dumplog();
1510 do_gettimeofday(&work_start);
1511 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
1512 if (likely(svc->srv_stats != NULL)) {
1513 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
1515 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
1516 svc->srv_n_queued_reqs);
1517 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
1518 svc->srv_n_active_reqs);
1519 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
1520 at_get(&svc->srv_at_estimate));
1523 rc = lu_context_init(&request->rq_session,
1524 LCT_SESSION|LCT_REMEMBER|LCT_NOREF);
1526 CERROR("Failure to initialize session: %d\n", rc);
1529 request->rq_session.lc_thread = thread;
1530 request->rq_session.lc_cookie = 0x5;
1531 lu_context_enter(&request->rq_session);
1533 CDEBUG(D_NET, "got req "LPU64"\n", request->rq_xid);
1535 request->rq_svc_thread = thread;
1537 request->rq_svc_thread->t_env->le_ses = &request->rq_session;
1539 if (likely(request->rq_export)) {
1540 if (unlikely(ptlrpc_check_req(request)))
1542 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
1543 export = class_export_rpc_get(request->rq_export);
1546 /* Discard requests queued for longer than the deadline.
1547 The deadline is increased if we send an early reply. */
1548 if (cfs_time_current_sec() > request->rq_deadline) {
1549 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
1550 ": deadline "CFS_DURATION_T":"CFS_DURATION_T"s ago\n",
1551 libcfs_id2str(request->rq_peer),
1552 cfs_time_sub(request->rq_deadline,
1553 request->rq_arrival_time.tv_sec),
1554 cfs_time_sub(cfs_time_current_sec(),
1555 request->rq_deadline));
1556 goto put_rpc_export;
1559 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
1560 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1561 (request->rq_export ?
1562 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1563 (request->rq_export ?
1564 atomic_read(&request->rq_export->exp_refcount) : -99),
1565 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1566 libcfs_id2str(request->rq_peer),
1567 lustre_msg_get_opc(request->rq_reqmsg));
1569 OBD_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, obd_fail_val);
1571 rc = svc->srv_handler(request);
1573 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
1575 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
1576 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1577 (request->rq_export ?
1578 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1579 (request->rq_export ?
1580 atomic_read(&request->rq_export->exp_refcount) : -99),
1581 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1582 libcfs_id2str(request->rq_peer),
1583 lustre_msg_get_opc(request->rq_reqmsg));
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 do_gettimeofday(&work_end);
1603 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1604 CDEBUG(D_RPCTRACE, "request x"LPU64" opc %u from %s processed in "
1605 "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
1606 request->rq_xid, lustre_msg_get_opc(request->rq_reqmsg),
1607 libcfs_id2str(request->rq_peer), timediff,
1608 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
1609 request->rq_repmsg ? lustre_msg_get_transno(request->rq_repmsg) :
1610 request->rq_transno, request->rq_status,
1611 request->rq_repmsg ? lustre_msg_get_status(request->rq_repmsg):
1613 if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
1614 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
1615 int opc = opcode_offset(op);
1616 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
1617 LASSERT(opc < LUSTRE_MAX_OPCODES);
1618 lprocfs_counter_add(svc->srv_stats,
1619 opc + EXTRA_MAX_OPCODES,
1623 if (unlikely(request->rq_early_count)) {
1624 DEBUG_REQ(D_ADAPTTO, request,
1625 "sent %d early replies before finishing in "
1627 request->rq_early_count,
1628 cfs_time_sub(work_end.tv_sec,
1629 request->rq_arrival_time.tv_sec));
1633 spin_lock(&svc->srv_lock);
1636 spin_unlock(&svc->srv_lock);
1637 ptlrpc_server_finish_request(request);
1643 * An internal function to process a single reply state object.
1646 ptlrpc_handle_rs (struct ptlrpc_reply_state *rs)
1648 struct ptlrpc_service *svc = rs->rs_service;
1649 struct obd_export *exp;
1650 struct obd_device *obd;
1655 exp = rs->rs_export;
1658 LASSERT (rs->rs_difficult);
1659 LASSERT (rs->rs_scheduled);
1660 LASSERT (list_empty(&rs->rs_list));
1662 spin_lock (&exp->exp_lock);
1663 /* Noop if removed already */
1664 list_del_init (&rs->rs_exp_list);
1665 spin_unlock (&exp->exp_lock);
1667 /* Avoid exp_uncommitted_replies_lock contention if we 100% sure that
1668 * rs has been removed from the list already */
1669 if (!list_empty_careful(&rs->rs_obd_list)) {
1670 spin_lock(&exp->exp_uncommitted_replies_lock);
1671 list_del_init(&rs->rs_obd_list);
1672 spin_unlock(&exp->exp_uncommitted_replies_lock);
1675 spin_lock(&rs->rs_lock);
1677 been_handled = rs->rs_handled;
1680 nlocks = rs->rs_nlocks; /* atomic "steal", but */
1681 rs->rs_nlocks = 0; /* locks still on rs_locks! */
1683 if (nlocks == 0 && !been_handled) {
1684 /* If we see this, we should already have seen the warning
1685 * in mds_steal_ack_locks() */
1686 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
1689 rs->rs_xid, rs->rs_transno,
1690 lustre_msg_get_opc(rs->rs_msg),
1691 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1694 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1695 spin_unlock(&rs->rs_lock);
1697 if (!been_handled && rs->rs_on_net) {
1698 LNetMDUnlink(rs->rs_md_h);
1699 /* Ignore return code; we're racing with
1703 while (nlocks-- > 0)
1704 ldlm_lock_decref(&rs->rs_locks[nlocks],
1705 rs->rs_modes[nlocks]);
1707 spin_lock(&rs->rs_lock);
1710 rs->rs_scheduled = 0;
1712 if (!rs->rs_on_net) {
1714 spin_unlock(&rs->rs_lock);
1716 class_export_put (exp);
1717 rs->rs_export = NULL;
1718 ptlrpc_rs_decref (rs);
1719 atomic_dec (&svc->srv_outstanding_replies);
1720 if (atomic_dec_and_test(&svc->srv_n_difficult_replies) &&
1721 svc->srv_is_stopping)
1722 cfs_waitq_broadcast(&svc->srv_waitq);
1726 /* still on the net; callback will schedule */
1727 spin_unlock(&rs->rs_lock);
1734 * Check whether given service has a reply available for processing
1737 * \param svc a ptlrpc service
1738 * \retval 0 no replies processes
1739 * \retval 1 one reply processed
1742 ptlrpc_server_handle_reply(struct ptlrpc_service *svc)
1744 struct ptlrpc_reply_state *rs = NULL;
1747 spin_lock(&svc->srv_lock);
1748 if (!list_empty(&svc->srv_reply_queue)) {
1749 rs = list_entry(svc->srv_reply_queue.prev,
1750 struct ptlrpc_reply_state,
1752 list_del_init(&rs->rs_list);
1754 spin_unlock(&svc->srv_lock);
1756 ptlrpc_handle_rs(rs);
1760 /* FIXME make use of timeout later */
1762 liblustre_check_services (void *arg)
1764 int did_something = 0;
1766 struct list_head *tmp, *nxt;
1769 /* I'm relying on being single threaded, not to have to lock
1770 * ptlrpc_all_services etc */
1771 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
1772 struct ptlrpc_service *svc =
1773 list_entry (tmp, struct ptlrpc_service, srv_list);
1775 if (svc->srv_threads_running != 0) /* I've recursed */
1778 /* service threads can block for bulk, so this limits us
1779 * (arbitrarily) to recursing 1 stack frame per service.
1780 * Note that the problem with recursion is that we have to
1781 * unwind completely before our caller can resume. */
1783 svc->srv_threads_running++;
1786 rc = ptlrpc_server_handle_req_in(svc);
1787 rc |= ptlrpc_server_handle_reply(svc);
1788 rc |= ptlrpc_at_check_timed(svc);
1789 rc |= ptlrpc_server_handle_request(svc, NULL);
1790 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
1791 did_something |= rc;
1794 svc->srv_threads_running--;
1797 RETURN(did_something);
1799 #define ptlrpc_stop_all_threads(s) do {} while (0)
1801 #else /* __KERNEL__ */
1803 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
1804 void ptlrpc_daemonize(char *name)
1806 struct fs_struct *fs = current->fs;
1808 atomic_inc(&fs->count);
1809 cfs_daemonize(name);
1810 exit_fs(cfs_current());
1812 ll_set_fs_pwd(current->fs, init_task.fs->pwdmnt, init_task.fs->pwd);
1816 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
1818 int avail = svc->srv_nrqbd_receiving;
1819 int low_water = test_req_buffer_pressure ? 0 :
1820 svc->srv_nbuf_per_group/2;
1822 /* NB I'm not locking; just looking. */
1824 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
1825 * allowed the request history to grow out of control. We could put a
1826 * sanity check on that here and cull some history if we need the
1829 if (avail <= low_water)
1830 ptlrpc_grow_req_bufs(svc);
1833 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
1838 ptlrpc_retry_rqbds(void *arg)
1840 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
1842 svc->srv_rqbd_timeout = 0;
1843 return (-ETIMEDOUT);
1846 static int ptlrpc_main(void *arg)
1848 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
1849 struct ptlrpc_service *svc = data->svc;
1850 struct ptlrpc_thread *thread = data->thread;
1851 struct obd_device *dev = data->dev;
1852 struct ptlrpc_reply_state *rs;
1853 #ifdef WITH_GROUP_INFO
1854 struct group_info *ginfo = NULL;
1857 int counter = 0, rc = 0;
1860 ptlrpc_daemonize(data->name);
1862 #if defined(HAVE_NODE_TO_CPUMASK) && defined(CONFIG_NUMA)
1863 /* we need to do this before any per-thread allocation is done so that
1864 * we get the per-thread allocations on local node. bug 7342 */
1865 if (svc->srv_cpu_affinity) {
1868 for (cpu = 0, num_cpu = 0; cpu < num_possible_cpus(); cpu++) {
1869 if (!cpu_online(cpu))
1871 if (num_cpu == thread->t_id % num_online_cpus())
1875 set_cpus_allowed(cfs_current(), node_to_cpumask(cpu_to_node(cpu)));
1879 #ifdef WITH_GROUP_INFO
1880 ginfo = groups_alloc(0);
1886 set_current_groups(ginfo);
1887 put_group_info(ginfo);
1890 if (svc->srv_init != NULL) {
1891 rc = svc->srv_init(thread);
1896 rc = lu_context_init(&env.le_ctx,
1897 svc->srv_ctx_tags|LCT_REMEMBER|LCT_NOREF);
1901 thread->t_env = &env;
1902 env.le_ctx.lc_thread = thread;
1903 env.le_ctx.lc_cookie = 0x6;
1905 /* Alloc reply state structure for this one */
1906 OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
1912 /* Record that the thread is running */
1913 thread->t_flags = SVC_RUNNING;
1915 * wake up our creator. Note: @data is invalid after this point,
1916 * because it's allocated on ptlrpc_start_thread() stack.
1918 cfs_waitq_signal(&thread->t_ctl_waitq);
1920 thread->t_watchdog = lc_watchdog_add(GET_TIMEOUT(svc), NULL, NULL);
1922 spin_lock(&svc->srv_lock);
1923 svc->srv_threads_running++;
1924 list_add(&rs->rs_list, &svc->srv_free_rs_list);
1925 spin_unlock(&svc->srv_lock);
1926 cfs_waitq_signal(&svc->srv_free_rs_waitq);
1928 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
1929 svc->srv_threads_running);
1931 /* XXX maintain a list of all managed devices: insert here */
1933 while ((thread->t_flags & SVC_STOPPING) == 0) {
1934 /* Don't exit while there are replies to be handled */
1935 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
1936 ptlrpc_retry_rqbds, svc);
1938 lc_watchdog_disable(thread->t_watchdog);
1942 l_wait_event_exclusive (svc->srv_waitq,
1943 ((thread->t_flags & SVC_STOPPING) != 0) ||
1944 (!list_empty(&svc->srv_idle_rqbds) &&
1945 svc->srv_rqbd_timeout == 0) ||
1946 !list_empty(&svc->srv_req_in_queue) ||
1947 (ptlrpc_server_request_pending(svc, 0) &&
1948 (svc->srv_n_active_reqs <
1949 (svc->srv_threads_running - 1))) ||
1953 lc_watchdog_touch(thread->t_watchdog, GET_TIMEOUT(svc));
1955 ptlrpc_check_rqbd_pool(svc);
1957 if ((svc->srv_threads_started < svc->srv_threads_max) &&
1958 (svc->srv_n_active_reqs >= (svc->srv_threads_started - 1))){
1959 /* Ignore return code - we tried... */
1960 ptlrpc_start_thread(dev, svc);
1963 if (!list_empty(&svc->srv_req_in_queue)) {
1964 /* Process all incoming reqs before handling any */
1965 ptlrpc_server_handle_req_in(svc);
1966 /* but limit ourselves in case of flood */
1967 if (counter++ < 1000)
1972 if (svc->srv_at_check)
1973 ptlrpc_at_check_timed(svc);
1975 /* don't handle requests in the last thread */
1976 if (ptlrpc_server_request_pending(svc, 0) &&
1977 (svc->srv_n_active_reqs < (svc->srv_threads_running - 1))) {
1978 lu_context_enter(&env.le_ctx);
1979 ptlrpc_server_handle_request(svc, thread);
1980 lu_context_exit(&env.le_ctx);
1983 if (!list_empty(&svc->srv_idle_rqbds) &&
1984 ptlrpc_server_post_idle_rqbds(svc) < 0) {
1985 /* I just failed to repost request buffers. Wait
1986 * for a timeout (unless something else happens)
1987 * before I try again */
1988 svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
1989 CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
1990 svc->srv_nrqbd_receiving);
1994 lc_watchdog_delete(thread->t_watchdog);
1995 thread->t_watchdog = NULL;
1999 * deconstruct service specific state created by ptlrpc_start_thread()
2001 if (svc->srv_done != NULL)
2002 svc->srv_done(thread);
2004 lu_context_fini(&env.le_ctx);
2006 CDEBUG(D_NET, "service thread %d exiting: rc %d\n", thread->t_id, rc);
2008 spin_lock(&svc->srv_lock);
2009 svc->srv_threads_running--; /* must know immediately */
2011 thread->t_flags = SVC_STOPPED;
2013 cfs_waitq_signal(&thread->t_ctl_waitq);
2014 spin_unlock(&svc->srv_lock);
2019 struct ptlrpc_hr_args {
2022 struct ptlrpc_hr_service *hrs;
2025 static int hrt_dont_sleep(struct ptlrpc_hr_thread *t,
2026 struct list_head *replies)
2030 spin_lock(&t->hrt_lock);
2031 list_splice_init(&t->hrt_queue, replies);
2032 result = test_bit(HRT_STOPPING, &t->hrt_flags) ||
2033 !list_empty(replies);
2034 spin_unlock(&t->hrt_lock);
2038 static int ptlrpc_hr_main(void *arg)
2040 struct ptlrpc_hr_args * hr_args = arg;
2041 struct ptlrpc_hr_service *hr = hr_args->hrs;
2042 struct ptlrpc_hr_thread *t = &hr->hr_threads[hr_args->thread_index];
2043 char threadname[20];
2044 CFS_LIST_HEAD(replies);
2046 snprintf(threadname, sizeof(threadname),
2047 "ptlrpc_hr_%d", hr_args->thread_index);
2049 ptlrpc_daemonize(threadname);
2050 #if defined(HAVE_NODE_TO_CPUMASK)
2051 set_cpus_allowed(cfs_current(),
2052 node_to_cpumask(cpu_to_node(hr_args->cpu_index)));
2054 set_bit(HRT_RUNNING, &t->hrt_flags);
2055 cfs_waitq_signal(&t->hrt_wait);
2057 while (!test_bit(HRT_STOPPING, &t->hrt_flags)) {
2059 cfs_wait_event(t->hrt_wait, hrt_dont_sleep(t, &replies));
2060 while (!list_empty(&replies)) {
2061 struct ptlrpc_reply_state *rs;
2063 rs = list_entry(replies.prev,
2064 struct ptlrpc_reply_state,
2066 list_del_init(&rs->rs_list);
2067 ptlrpc_handle_rs(rs);
2071 clear_bit(HRT_RUNNING, &t->hrt_flags);
2072 complete(&t->hrt_completion);
2077 static int ptlrpc_start_hr_thread(struct ptlrpc_hr_service *hr, int n, int cpu)
2079 struct ptlrpc_hr_thread *t = &hr->hr_threads[n];
2080 struct ptlrpc_hr_args args;
2084 args.thread_index = n;
2085 args.cpu_index = cpu;
2088 rc = cfs_kernel_thread(ptlrpc_hr_main, (void*)&args,
2089 CLONE_VM|CLONE_FILES);
2091 complete(&t->hrt_completion);
2094 cfs_wait_event(t->hrt_wait, test_bit(HRT_RUNNING, &t->hrt_flags));
2100 static void ptlrpc_stop_hr_thread(struct ptlrpc_hr_thread *t)
2104 set_bit(HRT_STOPPING, &t->hrt_flags);
2105 cfs_waitq_signal(&t->hrt_wait);
2106 wait_for_completion(&t->hrt_completion);
2111 static void ptlrpc_stop_hr_threads(struct ptlrpc_hr_service *hrs)
2116 for (n = 0; n < hrs->hr_n_threads; n++)
2117 ptlrpc_stop_hr_thread(&hrs->hr_threads[n]);
2122 static int ptlrpc_start_hr_threads(struct ptlrpc_hr_service *hr)
2125 int n, cpu, threads_started = 0;
2128 LASSERT(hr != NULL);
2129 LASSERT(hr->hr_n_threads > 0);
2131 for (n = 0, cpu = 0; n < hr->hr_n_threads; n++) {
2132 #if defined(HAVE_NODE_TO_CPUMASK)
2133 while(!cpu_online(cpu)) {
2135 if (cpu >= num_possible_cpus())
2139 rc = ptlrpc_start_hr_thread(hr, n, cpu);
2145 if (threads_started == 0) {
2146 CERROR("No reply handling threads started\n");
2149 if (threads_started < hr->hr_n_threads) {
2150 CWARN("Started only %d reply handling threads from %d\n",
2151 threads_started, hr->hr_n_threads);
2152 hr->hr_n_threads = threads_started;
2157 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
2158 struct ptlrpc_thread *thread)
2160 struct l_wait_info lwi = { 0 };
2163 CDEBUG(D_RPCTRACE, "Stopping thread %p\n", thread);
2164 spin_lock(&svc->srv_lock);
2165 thread->t_flags = SVC_STOPPING;
2166 spin_unlock(&svc->srv_lock);
2168 cfs_waitq_broadcast(&svc->srv_waitq);
2169 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
2172 spin_lock(&svc->srv_lock);
2173 list_del(&thread->t_link);
2174 spin_unlock(&svc->srv_lock);
2176 OBD_FREE_PTR(thread);
2180 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
2182 struct ptlrpc_thread *thread;
2185 spin_lock(&svc->srv_lock);
2186 while (!list_empty(&svc->srv_threads)) {
2187 thread = list_entry(svc->srv_threads.next,
2188 struct ptlrpc_thread, t_link);
2190 spin_unlock(&svc->srv_lock);
2191 ptlrpc_stop_thread(svc, thread);
2192 spin_lock(&svc->srv_lock);
2195 spin_unlock(&svc->srv_lock);
2199 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc)
2204 /* We require 2 threads min - see note in
2205 ptlrpc_server_handle_request */
2206 LASSERT(svc->srv_threads_min >= 2);
2207 for (i = 0; i < svc->srv_threads_min; i++) {
2208 rc = ptlrpc_start_thread(dev, svc);
2209 /* We have enough threads, don't start more. b=15759 */
2213 CERROR("cannot start %s thread #%d: rc %d\n",
2214 svc->srv_thread_name, i, rc);
2215 ptlrpc_stop_all_threads(svc);
2221 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc)
2223 struct l_wait_info lwi = { 0 };
2224 struct ptlrpc_svc_data d;
2225 struct ptlrpc_thread *thread;
2230 CDEBUG(D_RPCTRACE, "%s started %d min %d max %d running %d\n",
2231 svc->srv_name, svc->srv_threads_started, svc->srv_threads_min,
2232 svc->srv_threads_max, svc->srv_threads_running);
2233 if (unlikely(svc->srv_threads_started >= svc->srv_threads_max) ||
2234 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
2235 svc->srv_threads_started == svc->srv_threads_min - 1))
2238 OBD_ALLOC_PTR(thread);
2241 cfs_waitq_init(&thread->t_ctl_waitq);
2243 spin_lock(&svc->srv_lock);
2244 if (svc->srv_threads_started >= svc->srv_threads_max) {
2245 spin_unlock(&svc->srv_lock);
2246 OBD_FREE_PTR(thread);
2249 list_add(&thread->t_link, &svc->srv_threads);
2250 id = svc->srv_threads_started++;
2251 spin_unlock(&svc->srv_lock);
2253 thread->t_svc = svc;
2255 sprintf(name, "%s_%02d", svc->srv_thread_name, id);
2261 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
2263 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
2264 * just drop the VM and FILES in ptlrpc_daemonize() right away.
2266 rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
2268 CERROR("cannot start thread '%s': rc %d\n", name, rc);
2270 spin_lock(&svc->srv_lock);
2271 list_del(&thread->t_link);
2272 --svc->srv_threads_started;
2273 spin_unlock(&svc->srv_lock);
2275 OBD_FREE(thread, sizeof(*thread));
2278 l_wait_event(thread->t_ctl_waitq,
2279 thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
2281 rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
2286 int ptlrpc_hr_init(void)
2289 int n_cpus = num_online_cpus();
2290 struct ptlrpc_hr_service *hr;
2294 LASSERT(ptlrpc_hr == NULL);
2296 size = offsetof(struct ptlrpc_hr_service, hr_threads[n_cpus]);
2297 OBD_ALLOC(hr, size);
2300 for (i = 0; i < n_cpus; i++) {
2301 struct ptlrpc_hr_thread *t = &hr->hr_threads[i];
2303 spin_lock_init(&t->hrt_lock);
2304 cfs_waitq_init(&t->hrt_wait);
2305 CFS_INIT_LIST_HEAD(&t->hrt_queue);
2306 init_completion(&t->hrt_completion);
2308 hr->hr_n_threads = n_cpus;
2312 RETURN(ptlrpc_start_hr_threads(hr));
2315 void ptlrpc_hr_fini(void)
2317 if (ptlrpc_hr != NULL) {
2318 ptlrpc_stop_hr_threads(ptlrpc_hr);
2319 OBD_FREE(ptlrpc_hr, ptlrpc_hr->hr_size);
2324 #endif /* __KERNEL__ */
2327 * Wait until all already scheduled replies are processed.
2329 static void ptlrpc_wait_replies(struct ptlrpc_service *svc)
2333 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10),
2335 rc = l_wait_event(svc->srv_waitq,
2336 atomic_read(&svc->srv_n_difficult_replies) == 0,
2340 CWARN("Unexpectedly long timeout %p\n", svc);
2344 int ptlrpc_unregister_service(struct ptlrpc_service *service)
2347 struct l_wait_info lwi;
2348 struct list_head *tmp;
2349 struct ptlrpc_reply_state *rs, *t;
2350 struct ptlrpc_at_array *array = &service->srv_at_array;
2353 service->srv_is_stopping = 1;
2354 cfs_timer_disarm(&service->srv_at_timer);
2356 ptlrpc_stop_all_threads(service);
2357 LASSERT(list_empty(&service->srv_threads));
2359 spin_lock (&ptlrpc_all_services_lock);
2360 list_del_init (&service->srv_list);
2361 spin_unlock (&ptlrpc_all_services_lock);
2363 ptlrpc_lprocfs_unregister_service(service);
2365 /* All history will be culled when the next request buffer is
2367 service->srv_max_history_rqbds = 0;
2369 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
2371 rc = LNetClearLazyPortal(service->srv_req_portal);
2374 /* Unlink all the request buffers. This forces a 'final' event with
2375 * its 'unlink' flag set for each posted rqbd */
2376 list_for_each(tmp, &service->srv_active_rqbds) {
2377 struct ptlrpc_request_buffer_desc *rqbd =
2378 list_entry(tmp, struct ptlrpc_request_buffer_desc,
2381 rc = LNetMDUnlink(rqbd->rqbd_md_h);
2382 LASSERT (rc == 0 || rc == -ENOENT);
2385 /* Wait for the network to release any buffers it's currently
2388 spin_lock(&service->srv_lock);
2389 rc = service->srv_nrqbd_receiving;
2390 spin_unlock(&service->srv_lock);
2395 /* Network access will complete in finite time but the HUGE
2396 * timeout lets us CWARN for visibility of sluggish NALs */
2397 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
2398 cfs_time_seconds(1), NULL, NULL);
2399 rc = l_wait_event(service->srv_waitq,
2400 service->srv_nrqbd_receiving == 0,
2402 if (rc == -ETIMEDOUT)
2403 CWARN("Service %s waiting for request buffers\n",
2407 /* schedule all outstanding replies to terminate them */
2408 spin_lock(&service->srv_lock);
2409 while (!list_empty(&service->srv_active_replies)) {
2410 struct ptlrpc_reply_state *rs =
2411 list_entry(service->srv_active_replies.next,
2412 struct ptlrpc_reply_state, rs_list);
2413 spin_lock(&rs->rs_lock);
2414 ptlrpc_schedule_difficult_reply(rs);
2415 spin_unlock(&rs->rs_lock);
2417 spin_unlock(&service->srv_lock);
2419 /* purge the request queue. NB No new replies (rqbds all unlinked)
2420 * and no service threads, so I'm the only thread noodling the
2421 * request queue now */
2422 while (!list_empty(&service->srv_req_in_queue)) {
2423 struct ptlrpc_request *req =
2424 list_entry(service->srv_req_in_queue.next,
2425 struct ptlrpc_request,
2428 list_del(&req->rq_list);
2429 service->srv_n_queued_reqs--;
2430 service->srv_n_active_reqs++;
2431 ptlrpc_server_finish_request(req);
2433 while (ptlrpc_server_request_pending(service, 1)) {
2434 struct ptlrpc_request *req;
2436 req = ptlrpc_server_request_get(service, 1);
2437 list_del(&req->rq_list);
2438 service->srv_n_queued_reqs--;
2439 service->srv_n_active_reqs++;
2440 ptlrpc_hpreq_fini(req);
2441 ptlrpc_server_finish_request(req);
2443 LASSERT(service->srv_n_queued_reqs == 0);
2444 LASSERT(service->srv_n_active_reqs == 0);
2445 LASSERT(service->srv_n_history_rqbds == 0);
2446 LASSERT(list_empty(&service->srv_active_rqbds));
2448 /* Now free all the request buffers since nothing references them
2450 while (!list_empty(&service->srv_idle_rqbds)) {
2451 struct ptlrpc_request_buffer_desc *rqbd =
2452 list_entry(service->srv_idle_rqbds.next,
2453 struct ptlrpc_request_buffer_desc,
2456 ptlrpc_free_rqbd(rqbd);
2459 ptlrpc_wait_replies(service);
2461 list_for_each_entry_safe(rs, t, &service->srv_free_rs_list, rs_list) {
2462 list_del(&rs->rs_list);
2463 OBD_FREE(rs, service->srv_max_reply_size);
2466 /* In case somebody rearmed this in the meantime */
2467 cfs_timer_disarm(&service->srv_at_timer);
2469 if (array->paa_reqs_array != NULL) {
2470 OBD_FREE(array->paa_reqs_array,
2471 sizeof(struct list_head) * array->paa_size);
2472 array->paa_reqs_array = NULL;
2475 if (array->paa_reqs_count != NULL) {
2476 OBD_FREE(array->paa_reqs_count,
2477 sizeof(__u32) * array->paa_size);
2478 array->paa_reqs_count= NULL;
2481 OBD_FREE_PTR(service);
2485 /* Returns 0 if the service is healthy.
2487 * Right now, it just checks to make sure that requests aren't languishing
2488 * in the queue. We'll use this health check to govern whether a node needs
2489 * to be shot, so it's intentionally non-aggressive. */
2490 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
2492 struct ptlrpc_request *request;
2493 struct timeval right_now;
2499 do_gettimeofday(&right_now);
2501 spin_lock(&svc->srv_lock);
2502 if (!ptlrpc_server_request_pending(svc, 1)) {
2503 spin_unlock(&svc->srv_lock);
2507 /* How long has the next entry been waiting? */
2508 if (list_empty(&svc->srv_request_queue))
2509 request = list_entry(svc->srv_request_hpq.next,
2510 struct ptlrpc_request, rq_list);
2512 request = list_entry(svc->srv_request_queue.next,
2513 struct ptlrpc_request, rq_list);
2514 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
2515 spin_unlock(&svc->srv_lock);
2517 if ((timediff / ONE_MILLION) > (AT_OFF ? obd_timeout * 3/2 :
2519 CERROR("%s: unhealthy - request has been waiting %lds\n",
2520 svc->srv_name, timediff / ONE_MILLION);