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;
188 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
190 struct ptlrpc_service *svc = rs->rs_service;
194 LASSERT (spin_is_locked (&svc->srv_lock));
196 LASSERT (rs->rs_difficult);
197 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
199 if (rs->rs_scheduled) { /* being set up or already notified */
204 rs->rs_scheduled = 1;
205 list_del (&rs->rs_list);
206 list_add (&rs->rs_list, &svc->srv_reply_queue);
207 cfs_waitq_signal (&svc->srv_waitq);
212 ptlrpc_commit_replies (struct obd_device *obd)
214 struct list_head *tmp;
215 struct list_head *nxt;
218 /* Find any replies that have been committed and get their service
219 * to attend to complete them. */
221 /* CAVEAT EMPTOR: spinlock ordering!!! */
222 spin_lock(&obd->obd_uncommitted_replies_lock);
224 list_for_each_safe (tmp, nxt, &obd->obd_uncommitted_replies) {
225 struct ptlrpc_reply_state *rs =
226 list_entry(tmp, struct ptlrpc_reply_state, rs_obd_list);
228 LASSERT (rs->rs_difficult);
230 if (rs->rs_transno <= obd->obd_last_committed) {
231 struct ptlrpc_service *svc = rs->rs_service;
233 spin_lock (&svc->srv_lock);
234 list_del_init (&rs->rs_obd_list);
235 ptlrpc_schedule_difficult_reply (rs);
236 spin_unlock (&svc->srv_lock);
240 spin_unlock(&obd->obd_uncommitted_replies_lock);
245 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
247 struct ptlrpc_request_buffer_desc *rqbd;
252 spin_lock(&svc->srv_lock);
254 if (list_empty (&svc->srv_idle_rqbds)) {
255 spin_unlock(&svc->srv_lock);
259 rqbd = list_entry(svc->srv_idle_rqbds.next,
260 struct ptlrpc_request_buffer_desc,
262 list_del (&rqbd->rqbd_list);
264 /* assume we will post successfully */
265 svc->srv_nrqbd_receiving++;
266 list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
268 spin_unlock(&svc->srv_lock);
270 rc = ptlrpc_register_rqbd(rqbd);
277 spin_lock(&svc->srv_lock);
279 svc->srv_nrqbd_receiving--;
280 list_del(&rqbd->rqbd_list);
281 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
283 /* Don't complain if no request buffers are posted right now; LNET
284 * won't drop requests because we set the portal lazy! */
286 spin_unlock(&svc->srv_lock);
291 struct ptlrpc_service *ptlrpc_init_svc_conf(struct ptlrpc_service_conf *c,
292 svc_handler_t h, char *name,
293 struct proc_dir_entry *proc_entry,
294 svcreq_printfn_t prntfn,
297 return ptlrpc_init_svc(c->psc_nbufs, c->psc_bufsize,
298 c->psc_max_req_size, c->psc_max_reply_size,
299 c->psc_req_portal, c->psc_rep_portal,
300 c->psc_watchdog_factor,
302 prntfn, c->psc_min_threads, c->psc_max_threads,
303 threadname, c->psc_ctx_tags);
305 EXPORT_SYMBOL(ptlrpc_init_svc_conf);
307 static void ptlrpc_at_timer(unsigned long castmeharder)
309 struct ptlrpc_service *svc = (struct ptlrpc_service *)castmeharder;
310 svc->srv_at_check = 1;
311 svc->srv_at_checktime = cfs_time_current();
312 cfs_waitq_signal(&svc->srv_waitq);
315 /* @threadname should be 11 characters or less - 3 will be added on */
316 struct ptlrpc_service *
317 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
318 int req_portal, int rep_portal, int watchdog_factor,
319 svc_handler_t handler, char *name,
320 cfs_proc_dir_entry_t *proc_entry,
321 svcreq_printfn_t svcreq_printfn,
322 int min_threads, int max_threads,
323 char *threadname, __u32 ctx_tags)
326 struct ptlrpc_service *service;
330 LASSERT (bufsize >= max_req_size + SPTLRPC_MAX_PAYLOAD);
331 LASSERT (ctx_tags != 0);
333 OBD_ALLOC_PTR(service);
337 /* First initialise enough for early teardown */
339 service->srv_name = name;
340 spin_lock_init(&service->srv_lock);
341 CFS_INIT_LIST_HEAD(&service->srv_threads);
342 cfs_waitq_init(&service->srv_waitq);
344 service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
345 service->srv_max_req_size = max_req_size + SPTLRPC_MAX_PAYLOAD;
346 service->srv_buf_size = bufsize;
347 service->srv_rep_portal = rep_portal;
348 service->srv_req_portal = req_portal;
349 service->srv_watchdog_factor = watchdog_factor;
350 service->srv_handler = handler;
351 service->srv_request_history_print_fn = svcreq_printfn;
352 service->srv_request_seq = 1; /* valid seq #s start at 1 */
353 service->srv_request_max_cull_seq = 0;
354 service->srv_threads_min = min_threads;
355 service->srv_threads_max = max_threads;
356 service->srv_thread_name = threadname;
357 service->srv_ctx_tags = ctx_tags;
359 rc = LNetSetLazyPortal(service->srv_req_portal);
362 CFS_INIT_LIST_HEAD(&service->srv_request_queue);
363 CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
364 CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
365 CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
366 CFS_INIT_LIST_HEAD(&service->srv_request_history);
367 CFS_INIT_LIST_HEAD(&service->srv_active_replies);
368 CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
369 CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
370 cfs_waitq_init(&service->srv_free_rs_waitq);
372 spin_lock_init(&service->srv_at_lock);
373 CFS_INIT_LIST_HEAD(&service->srv_req_in_queue);
374 CFS_INIT_LIST_HEAD(&service->srv_at_list);
375 cfs_timer_init(&service->srv_at_timer, ptlrpc_at_timer, service);
376 /* At SOW, service time should be quick; 10s seems generous. If client
377 timeout is less than this, we'll be sending an early reply. */
378 at_init(&service->srv_at_estimate, 10, 0);
380 spin_lock (&ptlrpc_all_services_lock);
381 list_add (&service->srv_list, &ptlrpc_all_services);
382 spin_unlock (&ptlrpc_all_services_lock);
384 /* Now allocate the request buffers */
385 rc = ptlrpc_grow_req_bufs(service);
386 /* We shouldn't be under memory pressure at startup, so
387 * fail if we can't post all our buffers at this time. */
391 /* Now allocate pool of reply buffers */
392 /* Increase max reply size to next power of two */
393 service->srv_max_reply_size = 1;
394 while (service->srv_max_reply_size <
395 max_reply_size + SPTLRPC_MAX_PAYLOAD)
396 service->srv_max_reply_size <<= 1;
398 if (proc_entry != NULL)
399 ptlrpc_lprocfs_register_service(proc_entry, service);
401 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
402 service->srv_name, service->srv_req_portal);
406 ptlrpc_unregister_service(service);
411 * to actually free the request, must be called without holding svc_lock.
412 * note it's caller's responsibility to unlink req->rq_list.
414 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
416 LASSERT(atomic_read(&req->rq_refcount) == 0);
417 LASSERT(list_empty(&req->rq_timed_list));
419 /* DEBUG_REQ() assumes the reply state of a request with a valid
420 * ref will not be destroyed until that reference is dropped. */
421 ptlrpc_req_drop_rs(req);
423 sptlrpc_svc_ctx_decref(req);
425 if (req != &req->rq_rqbd->rqbd_req) {
426 /* NB request buffers use an embedded
427 * req if the incoming req unlinked the
428 * MD; this isn't one of them! */
429 OBD_FREE(req, sizeof(*req));
434 * drop a reference count of the request. if it reaches 0, we either
435 * put it into history list, or free it immediately.
437 static void ptlrpc_server_drop_request(struct ptlrpc_request *req)
439 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
440 struct ptlrpc_service *svc = rqbd->rqbd_service;
442 struct list_head *tmp;
443 struct list_head *nxt;
445 if (!atomic_dec_and_test(&req->rq_refcount))
448 spin_lock(&svc->srv_lock);
450 svc->srv_n_active_reqs--;
451 list_add(&req->rq_list, &rqbd->rqbd_reqs);
453 refcount = --(rqbd->rqbd_refcount);
455 /* request buffer is now idle: add to history */
456 list_del(&rqbd->rqbd_list);
457 list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
458 svc->srv_n_history_rqbds++;
460 /* cull some history?
461 * I expect only about 1 or 2 rqbds need to be recycled here */
462 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
463 rqbd = list_entry(svc->srv_history_rqbds.next,
464 struct ptlrpc_request_buffer_desc,
467 list_del(&rqbd->rqbd_list);
468 svc->srv_n_history_rqbds--;
470 /* remove rqbd's reqs from svc's req history while
471 * I've got the service lock */
472 list_for_each(tmp, &rqbd->rqbd_reqs) {
473 req = list_entry(tmp, struct ptlrpc_request,
475 /* Track the highest culled req seq */
476 if (req->rq_history_seq >
477 svc->srv_request_max_cull_seq)
478 svc->srv_request_max_cull_seq =
480 list_del(&req->rq_history_list);
483 spin_unlock(&svc->srv_lock);
485 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
486 req = list_entry(rqbd->rqbd_reqs.next,
487 struct ptlrpc_request,
489 list_del(&req->rq_list);
490 ptlrpc_server_free_request(req);
493 spin_lock(&svc->srv_lock);
495 * now all reqs including the embedded req has been
496 * disposed, schedule request buffer for re-use.
498 LASSERT(atomic_read(&rqbd->rqbd_req.rq_refcount) == 0);
499 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
502 spin_unlock(&svc->srv_lock);
503 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
504 /* If we are low on memory, we are not interested in history */
505 list_del(&req->rq_list);
506 list_del_init(&req->rq_history_list);
507 spin_unlock(&svc->srv_lock);
509 ptlrpc_server_free_request(req);
511 spin_unlock(&svc->srv_lock);
516 * to finish a request: stop sending more early replies, and release
517 * the request. should be called after we finished handling the request.
519 static void ptlrpc_server_finish_request(struct ptlrpc_request *req)
521 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
523 if (req->rq_phase != RQ_PHASE_NEW) /* incorrect message magic */
524 DEBUG_REQ(D_INFO, req, "free req");
526 spin_lock(&svc->srv_at_lock);
527 req->rq_sent_final = 1;
528 list_del_init(&req->rq_timed_list);
529 spin_unlock(&svc->srv_at_lock);
531 ptlrpc_server_drop_request(req);
534 /* This function makes sure dead exports are evicted in a timely manner.
535 This function is only called when some export receives a message (i.e.,
536 the network is up.) */
537 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
539 struct obd_export *oldest_exp;
546 /* Compensate for slow machines, etc, by faking our request time
547 into the future. Although this can break the strict time-ordering
548 of the list, we can be really lazy here - we don't have to evict
549 at the exact right moment. Eventually, all silent exports
550 will make it to the top of the list. */
551 exp->exp_last_request_time = max(exp->exp_last_request_time,
552 cfs_time_current_sec() + extra_delay);
554 CDEBUG(D_HA, "updating export %s at "CFS_TIME_T" exp %p\n",
555 exp->exp_client_uuid.uuid,
556 exp->exp_last_request_time, exp);
558 /* exports may get disconnected from the chain even though the
559 export has references, so we must keep the spin lock while
560 manipulating the lists */
561 spin_lock(&exp->exp_obd->obd_dev_lock);
563 if (list_empty(&exp->exp_obd_chain_timed)) {
564 /* this one is not timed */
565 spin_unlock(&exp->exp_obd->obd_dev_lock);
570 list_move_tail(&exp->exp_obd_chain_timed,
571 &exp->exp_obd->obd_exports_timed);
573 oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
574 struct obd_export, exp_obd_chain_timed);
575 oldest_time = oldest_exp->exp_last_request_time;
576 spin_unlock(&exp->exp_obd->obd_dev_lock);
578 if (exp->exp_obd->obd_recovering) {
579 /* be nice to everyone during recovery */
584 /* Note - racing to start/reset the obd_eviction timer is safe */
585 if (exp->exp_obd->obd_eviction_timer == 0) {
586 /* Check if the oldest entry is expired. */
587 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
589 /* We need a second timer, in case the net was down and
590 * it just came back. Since the pinger may skip every
591 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
592 * we better wait for 3. */
593 exp->exp_obd->obd_eviction_timer =
594 cfs_time_current_sec() + 3 * PING_INTERVAL;
595 CDEBUG(D_HA, "%s: Think about evicting %s from "CFS_TIME_T"\n",
596 exp->exp_obd->obd_name, obd_export_nid2str(exp),
600 if (cfs_time_current_sec() >
601 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
602 /* The evictor won't evict anyone who we've heard from
603 * recently, so we don't have to check before we start
605 if (!ping_evictor_wake(exp))
606 exp->exp_obd->obd_eviction_timer = 0;
613 static int ptlrpc_check_req(struct ptlrpc_request *req)
615 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
616 req->rq_export->exp_conn_cnt)) {
617 DEBUG_REQ(D_ERROR, req,
618 "DROPPING req from old connection %d < %d",
619 lustre_msg_get_conn_cnt(req->rq_reqmsg),
620 req->rq_export->exp_conn_cnt);
623 if (unlikely(req->rq_export->exp_obd &&
624 req->rq_export->exp_obd->obd_fail)) {
625 /* Failing over, don't handle any more reqs, send
626 error response instead. */
627 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
628 req, req->rq_export->exp_obd->obd_name);
629 req->rq_status = -ENODEV;
637 static void ptlrpc_at_set_timer(struct ptlrpc_service *svc)
639 struct ptlrpc_request *rq;
642 spin_lock(&svc->srv_at_lock);
643 if (list_empty(&svc->srv_at_list)) {
644 cfs_timer_disarm(&svc->srv_at_timer);
645 spin_unlock(&svc->srv_at_lock);
649 /* Set timer for closest deadline */
650 rq = list_entry(svc->srv_at_list.next, struct ptlrpc_request,
652 next = (__s32)(rq->rq_deadline - cfs_time_current_sec() -
655 ptlrpc_at_timer((unsigned long)svc);
657 cfs_timer_arm(&svc->srv_at_timer, cfs_time_shift(next));
658 spin_unlock(&svc->srv_at_lock);
659 CDEBUG(D_INFO, "armed %s at %+ds\n", svc->srv_name, next);
662 /* Add rpc to early reply check list */
663 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
665 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
666 struct ptlrpc_request *rq;
672 if (req->rq_no_reply)
675 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
678 spin_lock(&svc->srv_at_lock);
680 if (unlikely(req->rq_sent_final)) {
681 spin_unlock(&svc->srv_at_lock);
685 LASSERT(list_empty(&req->rq_timed_list));
686 /* Add to sorted list. Presumably latest rpcs will have the latest
687 deadlines, so search backward. */
688 list_for_each_entry_reverse(rq, &svc->srv_at_list, rq_timed_list) {
689 if (req->rq_deadline >= rq->rq_deadline) {
690 list_add(&req->rq_timed_list, &rq->rq_timed_list);
696 /* Add to front if shortest deadline or list empty */
697 list_add(&req->rq_timed_list, &svc->srv_at_list);
699 /* Check if we're the head of the list */
700 found = (svc->srv_at_list.next == &req->rq_timed_list);
702 spin_unlock(&svc->srv_at_lock);
705 ptlrpc_at_set_timer(svc);
710 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req,
713 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
714 struct ptlrpc_request *reqcopy;
715 struct lustre_msg *reqmsg;
716 cfs_duration_t olddl = req->rq_deadline - cfs_time_current_sec();
721 /* deadline is when the client expects us to reply, margin is the
722 difference between clients' and servers' expectations */
723 DEBUG_REQ(D_ADAPTTO, req,
724 "%ssending early reply (deadline %+lds, margin %+lds) for "
725 "%d+%d", AT_OFF ? "AT off - not " : "",
726 olddl, olddl - at_get(&svc->srv_at_estimate),
727 at_get(&svc->srv_at_estimate), extra_time);
733 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), "
734 "not sending early reply. Consider increasing "
735 "at_early_margin (%d)?", olddl, at_early_margin);
737 /* Return an error so we're not re-added to the timed list. */
741 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
742 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, "
743 "but no AT support");
747 if (req->rq_export && req->rq_export->exp_in_recovery) {
748 /* don't increase server estimates during recovery, and give
749 clients the full recovery time. */
750 newdl = cfs_time_current_sec() +
751 req->rq_export->exp_obd->obd_recovery_timeout;
754 /* Fake our processing time into the future to ask the
755 clients for some extra amount of time */
756 extra_time += cfs_time_current_sec() -
757 req->rq_arrival_time.tv_sec;
758 at_add(&svc->srv_at_estimate, extra_time);
760 newdl = req->rq_arrival_time.tv_sec +
761 at_get(&svc->srv_at_estimate);
763 if (req->rq_deadline >= newdl) {
764 /* We're not adding any time, no need to send an early reply
765 (e.g. maybe at adaptive_max) */
766 DEBUG_REQ(D_WARNING, req, "Couldn't add any time ("
767 CFS_DURATION_T"/"CFS_DURATION_T"), "
768 "not sending early reply\n", olddl,
769 cfs_time_sub(newdl, cfs_time_current_sec()));
773 OBD_ALLOC(reqcopy, sizeof *reqcopy);
776 OBD_ALLOC(reqmsg, req->rq_reqlen);
778 OBD_FREE(reqcopy, sizeof *reqcopy);
783 reqcopy->rq_reply_state = NULL;
784 reqcopy->rq_rep_swab_mask = 0;
785 reqcopy->rq_pack_bulk = 0;
786 reqcopy->rq_pack_udesc = 0;
787 reqcopy->rq_packed_final = 0;
788 sptlrpc_svc_ctx_addref(reqcopy);
789 /* We only need the reqmsg for the magic */
790 reqcopy->rq_reqmsg = reqmsg;
791 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
793 if (req->rq_sent_final) {
794 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, "
795 "abort sending early reply\n");
800 reqcopy->rq_export = class_conn2export(
801 lustre_msg_get_handle(reqcopy->rq_reqmsg));
802 if (reqcopy->rq_export == NULL)
803 GOTO(out, rc = -ENODEV);
806 class_export_rpc_get(reqcopy->rq_export);
807 if (reqcopy->rq_export->exp_obd &&
808 reqcopy->rq_export->exp_obd->obd_fail)
809 GOTO(out_put, rc = -ENODEV);
811 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
815 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
818 /* Adjust our own deadline to what we told the client */
819 req->rq_deadline = newdl;
820 req->rq_early_count++; /* number sent, server side */
822 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
825 /* Free the (early) reply state from lustre_pack_reply.
826 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
827 ptlrpc_req_drop_rs(reqcopy);
830 class_export_rpc_put(reqcopy->rq_export);
831 class_export_put(reqcopy->rq_export);
833 sptlrpc_svc_ctx_decref(reqcopy);
834 OBD_FREE(reqmsg, req->rq_reqlen);
835 OBD_FREE(reqcopy, sizeof *reqcopy);
839 /* Send early replies to everybody expiring within at_early_margin
840 asking for at_extra time */
841 static int ptlrpc_at_check_timed(struct ptlrpc_service *svc)
843 struct ptlrpc_request *rq, *n;
844 struct list_head work_list;
845 time_t now = cfs_time_current_sec();
846 cfs_duration_t delay;
847 int first, counter = 0;
850 spin_lock(&svc->srv_at_lock);
851 if (svc->srv_at_check == 0) {
852 spin_unlock(&svc->srv_at_lock);
855 delay = cfs_time_sub(cfs_time_current(), svc->srv_at_checktime);
856 svc->srv_at_check = 0;
858 if (list_empty(&svc->srv_at_list)) {
859 spin_unlock(&svc->srv_at_lock);
863 /* The timer went off, but maybe the nearest rpc already completed. */
864 rq = list_entry(svc->srv_at_list.next, struct ptlrpc_request,
866 first = (int)(rq->rq_deadline - now);
867 if (first > at_early_margin) {
868 /* We've still got plenty of time. Reset the timer. */
869 spin_unlock(&svc->srv_at_lock);
870 ptlrpc_at_set_timer(svc);
874 /* We're close to a timeout, and we don't know how much longer the
875 server will take. Send early replies to everyone expiring soon. */
876 CFS_INIT_LIST_HEAD(&work_list);
877 list_for_each_entry_safe(rq, n, &svc->srv_at_list, rq_timed_list) {
878 if (rq->rq_deadline <= now + at_early_margin) {
879 list_move_tail(&rq->rq_timed_list, &work_list);
886 spin_unlock(&svc->srv_at_lock);
888 /* we have a new earliest deadline, restart the timer */
889 ptlrpc_at_set_timer(svc);
891 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
892 "replies\n", first, at_extra, counter);
894 /* We're already past request deadlines before we even get a
895 chance to send early replies */
896 LCONSOLE_WARN("%s: This server is not able to keep up with "
897 "request traffic (cpu-bound).\n", svc->srv_name);
898 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
899 "delay="CFS_DURATION_T"(jiff)\n",
900 counter, svc->srv_n_queued_reqs, svc->srv_n_active_reqs,
901 at_get(&svc->srv_at_estimate), delay);
904 /* ptlrpc_server_finish_request may delete an entry out of
906 spin_lock(&svc->srv_at_lock);
907 while (!list_empty(&work_list)) {
908 rq = list_entry(work_list.next, struct ptlrpc_request,
910 list_del_init(&rq->rq_timed_list);
911 /* if the entry is still in the worklist, it hasn't been
912 deleted, and is safe to take a ref to keep the req around */
913 atomic_inc(&rq->rq_refcount);
914 spin_unlock(&svc->srv_at_lock);
916 if (ptlrpc_at_send_early_reply(rq, at_extra) == 0)
917 ptlrpc_at_add_timed(rq);
919 ptlrpc_server_drop_request(rq);
920 spin_lock(&svc->srv_at_lock);
922 spin_unlock(&svc->srv_at_lock);
927 /* Handle freshly incoming reqs, add to timed early reply list,
928 pass on to regular request queue */
930 ptlrpc_server_handle_req_in(struct ptlrpc_service *svc)
932 struct ptlrpc_request *req;
939 spin_lock(&svc->srv_lock);
940 if (list_empty(&svc->srv_req_in_queue)) {
941 spin_unlock(&svc->srv_lock);
945 req = list_entry(svc->srv_req_in_queue.next,
946 struct ptlrpc_request, rq_list);
947 list_del_init (&req->rq_list);
948 /* Consider this still a "queued" request as far as stats are
950 spin_unlock(&svc->srv_lock);
952 /* go through security check/transform */
953 rc = sptlrpc_svc_unwrap_request(req);
957 case SECSVC_COMPLETE:
958 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
966 /* Clear request swab mask; this is a new request */
967 req->rq_req_swab_mask = 0;
969 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
971 CERROR("error unpacking request: ptl %d from %s x"LPU64"\n",
972 svc->srv_req_portal, libcfs_id2str(req->rq_peer),
977 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
979 CERROR ("error unpacking ptlrpc body: ptl %d from %s x"
980 LPU64"\n", svc->srv_req_portal,
981 libcfs_id2str(req->rq_peer), req->rq_xid);
986 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
987 CERROR("wrong packet type received (type=%u) from %s\n",
988 lustre_msg_get_type(req->rq_reqmsg),
989 libcfs_id2str(req->rq_peer));
993 CDEBUG(D_NET, "got req "LPD64"\n", req->rq_xid);
995 req->rq_export = class_conn2export(
996 lustre_msg_get_handle(req->rq_reqmsg));
997 if (req->rq_export) {
998 rc = ptlrpc_check_req(req);
1000 rc = sptlrpc_target_export_check(req->rq_export, req);
1002 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
1003 "illegal security flavor,");
1006 class_export_put(req->rq_export);
1007 req->rq_export = NULL;
1012 /* req_in handling should/must be fast */
1013 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
1014 DEBUG_REQ(D_WARNING, req, "Slow req_in handling "CFS_DURATION_T"s",
1015 cfs_time_sub(cfs_time_current_sec(),
1016 req->rq_arrival_time.tv_sec));
1018 /* Set rpc server deadline and add it to the timed list */
1019 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1020 MSGHDR_AT_SUPPORT) ?
1021 /* The max time the client expects us to take */
1022 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1023 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1024 if (unlikely(deadline == 0)) {
1025 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1029 ptlrpc_at_add_timed(req);
1031 /* Move it over to the request processing queue */
1032 spin_lock(&svc->srv_lock);
1033 list_add_tail(&req->rq_list, &svc->srv_request_queue);
1034 cfs_waitq_signal(&svc->srv_waitq);
1035 spin_unlock(&svc->srv_lock);
1039 spin_lock(&svc->srv_lock);
1040 svc->srv_n_queued_reqs--;
1041 svc->srv_n_active_reqs++;
1042 spin_unlock(&svc->srv_lock);
1043 ptlrpc_server_finish_request(req);
1049 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
1050 struct ptlrpc_thread *thread)
1052 struct obd_export *export = NULL;
1053 struct ptlrpc_request *request;
1054 struct timeval work_start;
1055 struct timeval work_end;
1062 spin_lock(&svc->srv_lock);
1063 if (unlikely(list_empty (&svc->srv_request_queue) ||
1066 /* !@%$# liblustre only has 1 thread */
1067 svc->srv_n_difficult_replies != 0 &&
1069 svc->srv_n_active_reqs >= (svc->srv_threads_running - 1)))) {
1070 /* Don't handle regular requests in the last thread, in order * re
1071 * to handle difficult replies (which might block other threads)
1072 * as well as handle any incoming reqs, early replies, etc.
1073 * That means we always need at least 2 service threads. */
1074 spin_unlock(&svc->srv_lock);
1078 request = list_entry (svc->srv_request_queue.next,
1079 struct ptlrpc_request, rq_list);
1080 list_del_init (&request->rq_list);
1081 svc->srv_n_queued_reqs--;
1082 svc->srv_n_active_reqs++;
1084 spin_unlock(&svc->srv_lock);
1086 if(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
1087 libcfs_debug_dumplog();
1089 do_gettimeofday(&work_start);
1090 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
1091 if (likely(svc->srv_stats != NULL)) {
1092 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
1094 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
1095 svc->srv_n_queued_reqs);
1096 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
1097 svc->srv_n_active_reqs);
1098 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
1099 at_get(&svc->srv_at_estimate));
1102 rc = lu_context_init(&request->rq_session,
1103 LCT_SESSION|LCT_REMEMBER|LCT_NOREF);
1105 CERROR("Failure to initialize session: %d\n", rc);
1108 request->rq_session.lc_thread = thread;
1109 request->rq_session.lc_cookie = 0x5;
1110 lu_context_enter(&request->rq_session);
1112 CDEBUG(D_NET, "got req "LPU64"\n", request->rq_xid);
1114 request->rq_svc_thread = thread;
1116 request->rq_svc_thread->t_env->le_ses = &request->rq_session;
1118 request->rq_export = class_conn2export(
1119 lustre_msg_get_handle(request->rq_reqmsg));
1121 if (likely(request->rq_export)) {
1122 if (unlikely(ptlrpc_check_req(request)))
1124 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
1125 export = class_export_rpc_get(request->rq_export);
1128 /* Discard requests queued for longer than the deadline.
1129 The deadline is increased if we send an early reply. */
1130 if (cfs_time_current_sec() > request->rq_deadline) {
1131 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
1132 ": deadline "CFS_DURATION_T":"CFS_DURATION_T"s ago\n",
1133 libcfs_id2str(request->rq_peer),
1134 cfs_time_sub(request->rq_deadline,
1135 request->rq_arrival_time.tv_sec),
1136 cfs_time_sub(cfs_time_current_sec(),
1137 request->rq_deadline));
1138 goto put_rpc_export;
1141 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
1143 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
1144 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1145 (request->rq_export ?
1146 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1147 (request->rq_export ?
1148 atomic_read(&request->rq_export->exp_refcount) : -99),
1149 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1150 libcfs_id2str(request->rq_peer),
1151 lustre_msg_get_opc(request->rq_reqmsg));
1153 OBD_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, obd_fail_val);
1155 rc = svc->srv_handler(request);
1157 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
1159 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
1160 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1161 (request->rq_export ?
1162 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1163 (request->rq_export ?
1164 atomic_read(&request->rq_export->exp_refcount) : -99),
1165 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1166 libcfs_id2str(request->rq_peer),
1167 lustre_msg_get_opc(request->rq_reqmsg));
1171 class_export_rpc_put(export);
1173 if (likely(request->rq_export != NULL))
1174 class_export_put(request->rq_export);
1176 lu_context_exit(&request->rq_session);
1177 lu_context_fini(&request->rq_session);
1179 if (unlikely(cfs_time_current_sec() > request->rq_deadline)) {
1180 DEBUG_REQ(D_WARNING, request, "Request x"LPU64" took longer "
1181 "than estimated ("CFS_DURATION_T":"CFS_DURATION_T"s);"
1182 " client may timeout.",
1183 request->rq_xid, cfs_time_sub(request->rq_deadline,
1184 request->rq_arrival_time.tv_sec),
1185 cfs_time_sub(cfs_time_current_sec(),
1186 request->rq_deadline));
1189 do_gettimeofday(&work_end);
1190 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1191 CDEBUG(D_RPCTRACE, "request x"LPU64" opc %u from %s processed in "
1192 "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
1193 request->rq_xid, lustre_msg_get_opc(request->rq_reqmsg),
1194 libcfs_id2str(request->rq_peer), timediff,
1195 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
1196 request->rq_repmsg ? lustre_msg_get_transno(request->rq_repmsg) :
1197 request->rq_transno, request->rq_status,
1198 request->rq_repmsg ? lustre_msg_get_status(request->rq_repmsg):
1200 if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
1201 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
1202 int opc = opcode_offset(op);
1203 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
1204 LASSERT(opc < LUSTRE_MAX_OPCODES);
1205 lprocfs_counter_add(svc->srv_stats,
1206 opc + EXTRA_MAX_OPCODES,
1210 if (unlikely(request->rq_early_count)) {
1211 DEBUG_REQ(D_ADAPTTO, request,
1212 "sent %d early replies before finishing in "
1214 request->rq_early_count,
1215 cfs_time_sub(work_end.tv_sec,
1216 request->rq_arrival_time.tv_sec));
1220 ptlrpc_server_finish_request(request);
1226 ptlrpc_server_handle_reply (struct ptlrpc_service *svc)
1228 struct ptlrpc_reply_state *rs;
1229 struct obd_export *exp;
1230 struct obd_device *obd;
1235 spin_lock(&svc->srv_lock);
1236 if (list_empty (&svc->srv_reply_queue)) {
1237 spin_unlock(&svc->srv_lock);
1241 rs = list_entry (svc->srv_reply_queue.next,
1242 struct ptlrpc_reply_state, rs_list);
1244 exp = rs->rs_export;
1247 LASSERT (rs->rs_difficult);
1248 LASSERT (rs->rs_scheduled);
1250 list_del_init (&rs->rs_list);
1252 /* Disengage from notifiers carefully (lock order - irqrestore below!)*/
1253 spin_unlock(&svc->srv_lock);
1255 spin_lock (&obd->obd_uncommitted_replies_lock);
1256 /* Noop if removed already */
1257 list_del_init (&rs->rs_obd_list);
1258 spin_unlock (&obd->obd_uncommitted_replies_lock);
1260 spin_lock (&exp->exp_lock);
1261 /* Noop if removed already */
1262 list_del_init (&rs->rs_exp_list);
1263 spin_unlock (&exp->exp_lock);
1265 spin_lock(&svc->srv_lock);
1267 been_handled = rs->rs_handled;
1270 nlocks = rs->rs_nlocks; /* atomic "steal", but */
1271 rs->rs_nlocks = 0; /* locks still on rs_locks! */
1273 if (nlocks == 0 && !been_handled) {
1274 /* If we see this, we should already have seen the warning
1275 * in mds_steal_ack_locks() */
1276 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
1279 rs->rs_xid, rs->rs_transno,
1280 lustre_msg_get_opc(rs->rs_msg),
1281 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1284 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1285 spin_unlock(&svc->srv_lock);
1287 if (!been_handled && rs->rs_on_net) {
1288 LNetMDUnlink(rs->rs_md_h);
1289 /* Ignore return code; we're racing with
1293 while (nlocks-- > 0)
1294 ldlm_lock_decref(&rs->rs_locks[nlocks],
1295 rs->rs_modes[nlocks]);
1297 spin_lock(&svc->srv_lock);
1300 rs->rs_scheduled = 0;
1302 if (!rs->rs_on_net) {
1304 svc->srv_n_difficult_replies--;
1305 if (svc->srv_n_difficult_replies == 0 && svc->srv_is_stopping)
1306 /* wake up threads that are being stopped by
1307 ptlrpc_unregister_service/ptlrpc_stop_threads
1308 and sleep waiting svr_n_difficult_replies == 0 */
1309 cfs_waitq_broadcast(&svc->srv_waitq);
1310 spin_unlock(&svc->srv_lock);
1312 class_export_put (exp);
1313 rs->rs_export = NULL;
1314 ptlrpc_rs_decref (rs);
1315 atomic_dec (&svc->srv_outstanding_replies);
1319 /* still on the net; callback will schedule */
1320 spin_unlock(&svc->srv_lock);
1325 /* FIXME make use of timeout later */
1327 liblustre_check_services (void *arg)
1329 int did_something = 0;
1331 struct list_head *tmp, *nxt;
1334 /* I'm relying on being single threaded, not to have to lock
1335 * ptlrpc_all_services etc */
1336 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
1337 struct ptlrpc_service *svc =
1338 list_entry (tmp, struct ptlrpc_service, srv_list);
1340 if (svc->srv_threads_running != 0) /* I've recursed */
1343 /* service threads can block for bulk, so this limits us
1344 * (arbitrarily) to recursing 1 stack frame per service.
1345 * Note that the problem with recursion is that we have to
1346 * unwind completely before our caller can resume. */
1348 svc->srv_threads_running++;
1351 rc = ptlrpc_server_handle_req_in(svc);
1352 rc |= ptlrpc_server_handle_reply(svc);
1353 rc |= ptlrpc_at_check_timed(svc);
1354 rc |= ptlrpc_server_handle_request(svc, NULL);
1355 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
1356 did_something |= rc;
1359 svc->srv_threads_running--;
1362 RETURN(did_something);
1364 #define ptlrpc_stop_all_threads(s) do {} while (0)
1366 #else /* __KERNEL__ */
1368 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
1369 void ptlrpc_daemonize(char *name)
1371 struct fs_struct *fs = current->fs;
1373 atomic_inc(&fs->count);
1374 cfs_daemonize(name);
1375 exit_fs(cfs_current());
1377 ll_set_fs_pwd(current->fs, init_task.fs->pwdmnt, init_task.fs->pwd);
1381 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
1383 int avail = svc->srv_nrqbd_receiving;
1384 int low_water = test_req_buffer_pressure ? 0 :
1385 svc->srv_nbuf_per_group/2;
1387 /* NB I'm not locking; just looking. */
1389 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
1390 * allowed the request history to grow out of control. We could put a
1391 * sanity check on that here and cull some history if we need the
1394 if (avail <= low_water)
1395 ptlrpc_grow_req_bufs(svc);
1398 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
1403 ptlrpc_retry_rqbds(void *arg)
1405 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
1407 svc->srv_rqbd_timeout = 0;
1408 return (-ETIMEDOUT);
1411 static int ptlrpc_main(void *arg)
1413 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
1414 struct ptlrpc_service *svc = data->svc;
1415 struct ptlrpc_thread *thread = data->thread;
1416 struct obd_device *dev = data->dev;
1417 struct ptlrpc_reply_state *rs;
1418 struct lc_watchdog *watchdog;
1419 #ifdef WITH_GROUP_INFO
1420 struct group_info *ginfo = NULL;
1423 int counter = 0, rc = 0;
1426 ptlrpc_daemonize(data->name);
1428 #if defined(HAVE_NODE_TO_CPUMASK) && defined(CONFIG_NUMA)
1429 /* we need to do this before any per-thread allocation is done so that
1430 * we get the per-thread allocations on local node. bug 7342 */
1431 if (svc->srv_cpu_affinity) {
1434 for (cpu = 0, num_cpu = 0; cpu < num_possible_cpus(); cpu++) {
1435 if (!cpu_online(cpu))
1437 if (num_cpu == thread->t_id % num_online_cpus())
1441 set_cpus_allowed(cfs_current(), node_to_cpumask(cpu_to_node(cpu)));
1445 #ifdef WITH_GROUP_INFO
1446 ginfo = groups_alloc(0);
1452 set_current_groups(ginfo);
1453 put_group_info(ginfo);
1456 if (svc->srv_init != NULL) {
1457 rc = svc->srv_init(thread);
1462 rc = lu_context_init(&env.le_ctx,
1463 svc->srv_ctx_tags|LCT_REMEMBER|LCT_NOREF);
1467 thread->t_env = &env;
1468 env.le_ctx.lc_thread = thread;
1469 env.le_ctx.lc_cookie = 0x6;
1471 /* Alloc reply state structure for this one */
1472 OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
1478 /* Record that the thread is running */
1479 thread->t_flags = SVC_RUNNING;
1481 * wake up our creator. Note: @data is invalid after this point,
1482 * because it's allocated on ptlrpc_start_thread() stack.
1484 cfs_waitq_signal(&thread->t_ctl_waitq);
1486 watchdog = lc_watchdog_add(max_t(int, obd_timeout, AT_OFF ? 0 :
1487 at_get(&svc->srv_at_estimate)) *
1488 svc->srv_watchdog_factor, NULL, NULL);
1490 spin_lock(&svc->srv_lock);
1491 svc->srv_threads_running++;
1492 list_add(&rs->rs_list, &svc->srv_free_rs_list);
1493 spin_unlock(&svc->srv_lock);
1494 cfs_waitq_signal(&svc->srv_free_rs_waitq);
1496 CDEBUG(D_NET, "service thread %d (#%d)started\n", thread->t_id,
1497 svc->srv_threads_running);
1499 /* XXX maintain a list of all managed devices: insert here */
1501 while ((thread->t_flags & SVC_STOPPING) == 0 ||
1502 svc->srv_n_difficult_replies != 0) {
1503 /* Don't exit while there are replies to be handled */
1504 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
1505 ptlrpc_retry_rqbds, svc);
1507 lc_watchdog_disable(watchdog);
1511 l_wait_event_exclusive (svc->srv_waitq,
1512 ((thread->t_flags & SVC_STOPPING) != 0 &&
1513 svc->srv_n_difficult_replies == 0) ||
1514 (!list_empty(&svc->srv_idle_rqbds) &&
1515 svc->srv_rqbd_timeout == 0) ||
1516 !list_empty(&svc->srv_req_in_queue) ||
1517 !list_empty(&svc->srv_reply_queue) ||
1518 (!list_empty(&svc->srv_request_queue) &&
1519 (svc->srv_n_active_reqs <
1520 (svc->srv_threads_running - 1))) ||
1524 lc_watchdog_touch_ms(watchdog, max_t(int, obd_timeout,
1526 at_get(&svc->srv_at_estimate)) *
1527 svc->srv_watchdog_factor);
1529 ptlrpc_check_rqbd_pool(svc);
1531 if ((svc->srv_threads_started < svc->srv_threads_max) &&
1532 (svc->srv_n_active_reqs >= (svc->srv_threads_started - 1))){
1533 /* Ignore return code - we tried... */
1534 ptlrpc_start_thread(dev, svc);
1537 if (!list_empty(&svc->srv_reply_queue))
1538 ptlrpc_server_handle_reply(svc);
1540 if (!list_empty(&svc->srv_req_in_queue)) {
1541 /* Process all incoming reqs before handling any */
1542 ptlrpc_server_handle_req_in(svc);
1543 /* but limit ourselves in case of flood */
1544 if (counter++ < 1000)
1549 if (svc->srv_at_check)
1550 ptlrpc_at_check_timed(svc);
1552 /* don't handle requests in the last thread */
1553 if (!list_empty (&svc->srv_request_queue) &&
1554 (svc->srv_n_active_reqs < (svc->srv_threads_running - 1))) {
1555 lu_context_enter(&env.le_ctx);
1556 ptlrpc_server_handle_request(svc, thread);
1557 lu_context_exit(&env.le_ctx);
1560 if (!list_empty(&svc->srv_idle_rqbds) &&
1561 ptlrpc_server_post_idle_rqbds(svc) < 0) {
1562 /* I just failed to repost request buffers. Wait
1563 * for a timeout (unless something else happens)
1564 * before I try again */
1565 svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
1566 CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
1567 svc->srv_nrqbd_receiving);
1571 lc_watchdog_delete(watchdog);
1575 * deconstruct service specific state created by ptlrpc_start_thread()
1577 if (svc->srv_done != NULL)
1578 svc->srv_done(thread);
1580 lu_context_fini(&env.le_ctx);
1582 CDEBUG(D_NET, "service thread %d exiting: rc %d\n", thread->t_id, rc);
1584 spin_lock(&svc->srv_lock);
1585 svc->srv_threads_running--; /* must know immediately */
1587 thread->t_flags = SVC_STOPPED;
1589 cfs_waitq_signal(&thread->t_ctl_waitq);
1590 spin_unlock(&svc->srv_lock);
1595 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
1596 struct ptlrpc_thread *thread)
1598 struct l_wait_info lwi = { 0 };
1601 CDEBUG(D_RPCTRACE, "Stopping thread %p\n", thread);
1602 spin_lock(&svc->srv_lock);
1603 thread->t_flags = SVC_STOPPING;
1604 spin_unlock(&svc->srv_lock);
1606 cfs_waitq_broadcast(&svc->srv_waitq);
1607 l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
1610 spin_lock(&svc->srv_lock);
1611 list_del(&thread->t_link);
1612 spin_unlock(&svc->srv_lock);
1614 OBD_FREE_PTR(thread);
1618 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
1620 struct ptlrpc_thread *thread;
1623 spin_lock(&svc->srv_lock);
1624 while (!list_empty(&svc->srv_threads)) {
1625 thread = list_entry(svc->srv_threads.next,
1626 struct ptlrpc_thread, t_link);
1628 spin_unlock(&svc->srv_lock);
1629 ptlrpc_stop_thread(svc, thread);
1630 spin_lock(&svc->srv_lock);
1633 spin_unlock(&svc->srv_lock);
1637 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc)
1642 /* We require 2 threads min - see note in
1643 ptlrpc_server_handle_request */
1644 LASSERT(svc->srv_threads_min >= 2);
1645 for (i = 0; i < svc->srv_threads_min; i++) {
1646 rc = ptlrpc_start_thread(dev, svc);
1647 /* We have enough threads, don't start more. b=15759 */
1651 CERROR("cannot start %s thread #%d: rc %d\n",
1652 svc->srv_thread_name, i, rc);
1653 ptlrpc_stop_all_threads(svc);
1659 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc)
1661 struct l_wait_info lwi = { 0 };
1662 struct ptlrpc_svc_data d;
1663 struct ptlrpc_thread *thread;
1668 CDEBUG(D_RPCTRACE, "%s started %d min %d max %d running %d\n",
1669 svc->srv_name, svc->srv_threads_started, svc->srv_threads_min,
1670 svc->srv_threads_max, svc->srv_threads_running);
1671 if (unlikely(svc->srv_threads_started >= svc->srv_threads_max) ||
1672 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
1673 svc->srv_threads_started == svc->srv_threads_min - 1))
1676 OBD_ALLOC_PTR(thread);
1679 cfs_waitq_init(&thread->t_ctl_waitq);
1681 spin_lock(&svc->srv_lock);
1682 if (svc->srv_threads_started >= svc->srv_threads_max) {
1683 spin_unlock(&svc->srv_lock);
1684 OBD_FREE_PTR(thread);
1687 list_add(&thread->t_link, &svc->srv_threads);
1688 id = svc->srv_threads_started++;
1689 spin_unlock(&svc->srv_lock);
1692 sprintf(name, "%s_%02d", svc->srv_thread_name, id);
1698 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
1700 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
1701 * just drop the VM and FILES in ptlrpc_daemonize() right away.
1703 rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
1705 CERROR("cannot start thread '%s': rc %d\n", name, rc);
1707 spin_lock(&svc->srv_lock);
1708 list_del(&thread->t_link);
1709 --svc->srv_threads_started;
1710 spin_unlock(&svc->srv_lock);
1712 OBD_FREE(thread, sizeof(*thread));
1715 l_wait_event(thread->t_ctl_waitq,
1716 thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
1718 rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
1723 int ptlrpc_unregister_service(struct ptlrpc_service *service)
1726 struct l_wait_info lwi;
1727 struct list_head *tmp;
1728 struct ptlrpc_reply_state *rs, *t;
1731 service->srv_is_stopping = 1;
1732 cfs_timer_disarm(&service->srv_at_timer);
1734 ptlrpc_stop_all_threads(service);
1735 LASSERT(list_empty(&service->srv_threads));
1737 spin_lock (&ptlrpc_all_services_lock);
1738 list_del_init (&service->srv_list);
1739 spin_unlock (&ptlrpc_all_services_lock);
1741 ptlrpc_lprocfs_unregister_service(service);
1743 /* All history will be culled when the next request buffer is
1745 service->srv_max_history_rqbds = 0;
1747 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
1749 rc = LNetClearLazyPortal(service->srv_req_portal);
1752 /* Unlink all the request buffers. This forces a 'final' event with
1753 * its 'unlink' flag set for each posted rqbd */
1754 list_for_each(tmp, &service->srv_active_rqbds) {
1755 struct ptlrpc_request_buffer_desc *rqbd =
1756 list_entry(tmp, struct ptlrpc_request_buffer_desc,
1759 rc = LNetMDUnlink(rqbd->rqbd_md_h);
1760 LASSERT (rc == 0 || rc == -ENOENT);
1763 /* Wait for the network to release any buffers it's currently
1766 spin_lock(&service->srv_lock);
1767 rc = service->srv_nrqbd_receiving;
1768 spin_unlock(&service->srv_lock);
1773 /* Network access will complete in finite time but the HUGE
1774 * timeout lets us CWARN for visibility of sluggish NALs */
1775 lwi = LWI_TIMEOUT(cfs_time_seconds(LONG_UNLINK), NULL, NULL);
1776 rc = l_wait_event(service->srv_waitq,
1777 service->srv_nrqbd_receiving == 0,
1779 if (rc == -ETIMEDOUT)
1780 CWARN("Service %s waiting for request buffers\n",
1784 /* schedule all outstanding replies to terminate them */
1785 spin_lock(&service->srv_lock);
1786 while (!list_empty(&service->srv_active_replies)) {
1787 struct ptlrpc_reply_state *rs =
1788 list_entry(service->srv_active_replies.next,
1789 struct ptlrpc_reply_state, rs_list);
1790 ptlrpc_schedule_difficult_reply(rs);
1792 spin_unlock(&service->srv_lock);
1794 /* purge the request queue. NB No new replies (rqbds all unlinked)
1795 * and no service threads, so I'm the only thread noodling the
1796 * request queue now */
1797 while (!list_empty(&service->srv_req_in_queue)) {
1798 struct ptlrpc_request *req =
1799 list_entry(service->srv_req_in_queue.next,
1800 struct ptlrpc_request,
1803 list_del(&req->rq_list);
1804 service->srv_n_queued_reqs--;
1805 service->srv_n_active_reqs++;
1806 ptlrpc_server_finish_request(req);
1808 while (!list_empty(&service->srv_request_queue)) {
1809 struct ptlrpc_request *req =
1810 list_entry(service->srv_request_queue.next,
1811 struct ptlrpc_request,
1814 list_del(&req->rq_list);
1815 service->srv_n_queued_reqs--;
1816 service->srv_n_active_reqs++;
1818 ptlrpc_server_finish_request(req);
1820 LASSERT(service->srv_n_queued_reqs == 0);
1821 LASSERT(service->srv_n_active_reqs == 0);
1822 LASSERT(service->srv_n_history_rqbds == 0);
1823 LASSERT(list_empty(&service->srv_active_rqbds));
1825 /* Now free all the request buffers since nothing references them
1827 while (!list_empty(&service->srv_idle_rqbds)) {
1828 struct ptlrpc_request_buffer_desc *rqbd =
1829 list_entry(service->srv_idle_rqbds.next,
1830 struct ptlrpc_request_buffer_desc,
1833 ptlrpc_free_rqbd(rqbd);
1836 /* wait for all outstanding replies to complete (they were
1837 * scheduled having been flagged to abort above) */
1838 while (atomic_read(&service->srv_outstanding_replies) != 0) {
1839 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
1841 rc = l_wait_event(service->srv_waitq,
1842 !list_empty(&service->srv_reply_queue), &lwi);
1843 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1846 ptlrpc_server_handle_reply(service);
1849 CWARN("Unexpectedly long timeout %p\n", service);
1852 list_for_each_entry_safe(rs, t, &service->srv_free_rs_list, rs_list) {
1853 list_del(&rs->rs_list);
1854 OBD_FREE(rs, service->srv_max_reply_size);
1857 /* In case somebody rearmed this in the meantime */
1858 cfs_timer_disarm(&service->srv_at_timer);
1860 OBD_FREE_PTR(service);
1864 /* Returns 0 if the service is healthy.
1866 * Right now, it just checks to make sure that requests aren't languishing
1867 * in the queue. We'll use this health check to govern whether a node needs
1868 * to be shot, so it's intentionally non-aggressive. */
1869 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
1871 struct ptlrpc_request *request;
1872 struct timeval right_now;
1878 do_gettimeofday(&right_now);
1880 spin_lock(&svc->srv_lock);
1881 if (list_empty(&svc->srv_request_queue)) {
1882 spin_unlock(&svc->srv_lock);
1886 /* How long has the next entry been waiting? */
1887 request = list_entry(svc->srv_request_queue.next,
1888 struct ptlrpc_request, rq_list);
1889 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
1890 spin_unlock(&svc->srv_lock);
1892 if ((timediff / ONE_MILLION) > (AT_OFF ? obd_timeout * 3/2 :
1894 CERROR("%s: unhealthy - request has been waiting %lds\n",
1895 svc->srv_name, timediff / ONE_MILLION);